/* ============================================================================
   Sales Analytics — world-class BI dashboard. Scoped to .sa-dash.
   "Universe" aesthetic: deep cosmic gradient + glassmorphism + glowing accents
   in dark; clean, airy, premium in light. Light/dark follow the app-global
   html.theme-dark class. All colors via local CSS vars so charts/grid re-theme.
   ========================================================================== */

.sa-dash {
    --sa-canvas: #eef2f9;
    --sa-canvas-2: #e6ecf6;
    --sa-surface: #ffffff;
    --sa-surface-2: #f7f9fd;
    --sa-ink: #101a30;
    --sa-ink-2: #33415c;
    --sa-muted: #6b7a93;
    --sa-line: #e3e9f3;
    --sa-accent: #3257f0;
    --sa-accent-2: #7c3aed;
    --sa-teal: #0ea5a4;

    /* categorical accents used by the KPI cards (match the JS chart palette) */
    --sa-violet: #8b5cf6;
    --sa-rose: #ec5e8a;
    --sa-amber: #f59e2c;
    --sa-green: #3dbe7a;
    --sa-sky: #23a6c4;
    --sa-indigo: #2563eb;

    --sa-pos: #12a367;
    --sa-neg: #e14b40;
    --sa-warn: #e08b18;
    --sa-glass: rgba(255,255,255,.7);
    --sa-glass-brd: rgba(255,255,255,.75);
    --sa-shadow: 0 10px 30px rgba(20,32,64,.09);
    --sa-shadow-sm: 0 4px 14px rgba(20,32,64,.07);
    --sa-shadow-lg: 0 20px 48px rgba(20,32,64,.14);
    --sa-glow: 0 0 0 rgba(0,0,0,0);
    --sa-radius: 18px;
    --sa-radius-sm: 12px;
    --sa-gap: clamp(12px,1.3vw,20px);
    --sa-bg: radial-gradient(1200px 700px at 12% -8%, #dfe8fb 0%, transparent 55%),
             radial-gradient(1100px 700px at 105% 0%, #e9e2fb 0%, transparent 50%),
             var(--sa-canvas);
    color: var(--sa-ink);
    background: var(--sa-bg);
    padding: var(--sa-gap);
    min-height: 100vh;
    font-feature-settings: "tnum" 1, "cv05" 1;
}

html.theme-dark .sa-dash {
    --sa-canvas: #070b16;
    --sa-canvas-2: #0b1120;
    --sa-surface: rgba(19,26,44,.72);
    --sa-surface-2: rgba(28,37,60,.6);
    --sa-ink: #eaf0fb;
    --sa-ink-2: #c2cde3;
    --sa-muted: #8595b3;
    --sa-line: rgba(120,140,190,.16);
    --sa-accent: #6d8bff;
    --sa-accent-2: #b07cff;
    --sa-teal: #2fd4d0;

    --sa-violet: #a78bfa;
    --sa-rose: #f27ca3;
    --sa-amber: #f6b45a;
    --sa-green: #5fd199;
    --sa-sky: #4fc0db;
    --sa-indigo: #4f7cf0;

    --sa-glass: rgba(18,26,46,.55);
    --sa-glass-brd: rgba(130,150,200,.18);
    --sa-shadow: 0 14px 40px rgba(0,0,0,.5);
    --sa-shadow-sm: 0 6px 18px rgba(0,0,0,.42);
    --sa-shadow-lg: 0 26px 60px rgba(0,0,0,.62);
    --sa-glow: 0 0 40px rgba(109,139,255,.22);
    --sa-bg: radial-gradient(1000px 620px at 8% -10%, #14224e 0%, transparent 52%),
             radial-gradient(1000px 640px at 108% -6%, #2a1550 0%, transparent 50%),
             radial-gradient(900px 900px at 50% 120%, #0a1a3a 0%, transparent 60%),
             var(--sa-canvas);
}

.sa-dash * { box-sizing: border-box; }
.sa-dash > * { animation: sa-in .5s cubic-bezier(.2,.7,.2,1) both; }
@keyframes sa-in { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: none; } }
@media (prefers-reduced-motion: reduce) { .sa-dash > * { animation: none; } }

/* ---- header ---- */
.sa-head { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; margin-bottom: var(--sa-gap); }
.sa-head h2 {
    margin: 0; font-size: clamp(1.25rem,.9rem+1.3vw,1.85rem); font-weight: 800; letter-spacing: -.03em; line-height: 1;
    background: linear-gradient(92deg, var(--sa-accent), var(--sa-accent-2) 60%, var(--sa-teal));
    -webkit-background-clip: text; background-clip: text; -webkit-text-fill-color: transparent;
}
.sa-head .sa-sub { color: var(--sa-muted); font-size: 13px; font-weight: 700; padding: 4px 12px; border-radius: 999px;
    background: var(--sa-surface); border: 1px solid var(--sa-line); box-shadow: var(--sa-shadow); }
.sa-head .sa-spacer { flex: 1 1 auto; }
.sa-saledate { color: var(--sa-muted); font-size: 12.5px; font-weight: 700; }

/* ---- filter bar (deliberately NOT sticky — it scrolls away with the page) ---- */
.sa-filters {
    display: grid; gap: 10px 12px; align-items: end; margin-bottom: var(--sa-gap);
    grid-template-columns: repeat(auto-fit, minmax(148px, 1fr));
    background: var(--sa-glass); border: 1px solid var(--sa-glass-brd);
    backdrop-filter: blur(16px) saturate(1.4); -webkit-backdrop-filter: blur(16px) saturate(1.4);
    border-radius: var(--sa-radius); padding: 14px; box-shadow: var(--sa-shadow-lg);
}
.sa-filters .sa-f { display: flex; flex-direction: column; gap: 5px; min-width: 0; }
.sa-filters label { font-size: 10.5px; font-weight: 800; color: var(--sa-muted); text-transform: uppercase; letter-spacing: .06em; }
.sa-filters .sa-actions { display: flex; gap: 8px; align-items: end; }
.sa-btn {
    border: 1px solid var(--sa-line); background: var(--sa-surface); color: var(--sa-ink);
    border-radius: 10px; padding: 9px 16px; font-weight: 800; font-size: 13px; cursor: pointer; white-space: nowrap;
    transition: transform .12s, box-shadow .12s, border-color .12s, color .12s;
}
.sa-btn:hover { border-color: var(--sa-accent); color: var(--sa-accent); transform: translateY(-1px); }
.sa-btn-primary { background: linear-gradient(92deg, var(--sa-accent), var(--sa-accent-2)); border: none; color: #fff; box-shadow: 0 8px 20px rgba(50,87,240,.35); }
.sa-btn-primary:hover { color: #fff; filter: brightness(1.08); transform: translateY(-1px); }

/* ---- active-filter chips ---------------------------------------------------
   Clamped to two rows by default so a long selection can never push the
   dashboard below the fold; expands on demand into its own capped scroll. */
.sa-chipsbar { display: flex; align-items: flex-start; gap: 8px; margin-bottom: var(--sa-gap); }
.sa-chips { display: flex; flex-wrap: wrap; gap: 7px; flex: 1 1 auto; min-width: 0;
    max-height: 66px; overflow: hidden; transition: max-height .18s ease; }
.sa-chips.expanded { max-height: 24vh; overflow-y: auto; padding-right: 4px; }
.sa-chip { display: inline-flex; align-items: center; gap: 7px; max-width: 260px;
    background: var(--sa-surface); border: 1px solid var(--sa-line); color: var(--sa-ink-2);
    border-radius: 999px; padding: 4px 12px; font-size: 12px; font-weight: 700; box-shadow: var(--sa-shadow-sm); }
.sa-chip > span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sa-chip b { cursor: pointer; color: var(--sa-muted); font-weight: 900; font-size: 14px; line-height: 1; flex: 0 0 auto; }
.sa-chip b:hover { color: var(--sa-neg); }
.sa-chip-clear { cursor: pointer; border-style: dashed; border-color: var(--sa-neg); color: var(--sa-neg); background: transparent; box-shadow: none; }
.sa-chip-clear:hover { background: color-mix(in srgb, var(--sa-neg) 10%, transparent); }
.sa-chips-more { flex: 0 0 auto; cursor: pointer; border: 1px solid var(--sa-accent); background: var(--sa-surface);
    color: var(--sa-accent); border-radius: 999px; padding: 4px 12px; font-size: 12px; font-weight: 800; white-space: nowrap; }
.sa-chips-more:hover { background: var(--sa-accent); color: #fff; }

/* ---- section heading ---- */
.sa-section { display: flex; align-items: center; gap: 10px; margin: 6px 2px 2px; }
.sa-section h3 { margin: 0; font-size: 13.5px; font-weight: 800; letter-spacing: .01em; text-transform: uppercase; color: var(--sa-ink-2); }
.sa-section .sa-dot { width: 8px; height: 8px; border-radius: 3px; background: linear-gradient(var(--sa-accent), var(--sa-accent-2)); box-shadow: var(--sa-glow); }
.sa-section .sa-rule { flex: 1 1 auto; height: 1px; background: linear-gradient(90deg, var(--sa-line), transparent); }

/* ---- hero KPIs ---- */
.sa-hero { display: grid; gap: var(--sa-gap); grid-template-columns: repeat(auto-fit, minmax(228px, 1fr)); margin-bottom: var(--sa-gap); }
.sa-herocard {
    position: relative; overflow: hidden; border-radius: var(--sa-radius); padding: 18px 20px; cursor: help;
    background: var(--sa-surface); border: 1px solid var(--sa-glass-brd); box-shadow: var(--sa-shadow-lg);
    backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
    display: flex; flex-direction: column; gap: 8px; min-height: 148px; transition: transform .16s, box-shadow .16s;
}
.sa-herocard:hover { transform: translateY(-3px); box-shadow: var(--sa-shadow-lg), var(--sa-glow); }
.sa-herocard::after { content: ""; position: absolute; right: -30px; top: -30px; width: 130px; height: 130px; border-radius: 50%;
    background: radial-gradient(circle, var(--kpi-accent, var(--sa-accent)) 0%, transparent 70%); opacity: .16; pointer-events: none; }
.sa-herocard .sa-h-top { display: flex; align-items: center; justify-content: space-between; }
.sa-herocard .sa-h-label { font-size: 12px; font-weight: 800; color: var(--sa-muted); text-transform: uppercase; letter-spacing: .05em; }
.sa-herocard .sa-h-icon { width: 40px; height: 40px; border-radius: 12px; display: grid; place-items: center; font-size: 19px;
    background: linear-gradient(140deg, color-mix(in srgb, var(--kpi-accent, var(--sa-accent)) 26%, transparent), transparent);
    border: 1px solid color-mix(in srgb, var(--kpi-accent, var(--sa-accent)) 30%, transparent); }
.sa-herocard .sa-h-value { font-size: clamp(1.7rem,1.2rem+1.4vw,2.4rem); font-weight: 900; letter-spacing: -.03em; line-height: 1; }
.sa-herocard .sa-h-foot { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-top: auto; }
.sa-herocard .sa-h-prev { font-size: 12px; color: var(--sa-muted); font-weight: 700; }
.sa-herocard .sa-spark { height: 34px; width: 46%; }

/* ---- hero card: achievement gauge (the Executive Overview tile) ---- */
.sa-hgauge { margin-top: auto; }
.sa-hgauge-track { position: relative; height: 9px; border-radius: 6px; background: var(--sa-canvas-2);
    overflow: hidden; box-shadow: inset 0 1px 2px rgba(0,0,0,.10); }
.sa-hgauge-fill { position: absolute; inset: 0 auto 0 0; border-radius: 6px; transition: width .6s cubic-bezier(.4,0,.2,1); }
.sa-hgauge-tick { position: absolute; top: -3px; bottom: -3px; width: 2px; border-radius: 2px; background: var(--sa-ink-2); opacity: .42; }
.sa-hgauge-legend { display: flex; justify-content: space-between; gap: 8px; margin-top: 8px; font-size: 11.5px; font-weight: 700; color: var(--sa-muted); }

/* ---- hero card: GOLM / GOLY bi-directional growth gauges ----
   Track midpoint is zero: the fill runs right for growth, left for decline. */
.sa-hg { margin-top: auto; display: flex; flex-direction: column; gap: 9px; }
.sa-hg-row { display: flex; flex-direction: column; gap: 4px; }
.sa-hg-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.sa-hg-label { font-size: 10.5px; font-weight: 800; color: var(--sa-muted); text-transform: uppercase; letter-spacing: .06em; }
.sa-hg-num { font-size: 15px; font-weight: 900; letter-spacing: -.02em; font-variant-numeric: tabular-nums; }
.sa-hg-track { position: relative; height: 7px; border-radius: 5px; background: var(--sa-canvas-2); overflow: hidden; box-shadow: inset 0 1px 2px rgba(0,0,0,.10); }
.sa-hg-fill { position: absolute; top: 0; bottom: 0; border-radius: 5px; transition: width .6s cubic-bezier(.4,0,.2,1); }
/* the zero marker sits above the fill so the sign is always readable */
.sa-hg-zero { position: absolute; left: 50%; top: -1px; bottom: -1px; width: 2px; margin-left: -1px; background: var(--sa-ink-2); opacity: .35; z-index: 1; }

/* ---- secondary KPI cards ---- */
.sa-kpis { display: grid; gap: var(--sa-gap); grid-template-columns: repeat(auto-fill, minmax(184px, 1fr)); margin-bottom: var(--sa-gap); }
.sa-kpi {
    position: relative; overflow: hidden; background: var(--sa-surface); border: 1px solid var(--sa-line);
    border-radius: var(--sa-radius-sm); padding: 13px 15px; box-shadow: var(--sa-shadow); cursor: help;
    display: flex; flex-direction: column; gap: 6px; min-height: 96px; transition: transform .16s, box-shadow .16s;
}
.sa-kpi:hover { transform: translateY(-2px); box-shadow: var(--sa-shadow-lg); }
.sa-kpi::before { content: ""; position: absolute; inset: 0 auto 0 0; width: 3px; background: linear-gradient(var(--kpi-accent, var(--sa-accent)), var(--sa-accent-2)); }
.sa-kpi .sa-kpi-top { display: flex; align-items: center; justify-content: space-between; }
.sa-kpi .sa-kpi-label { font-size: 11px; font-weight: 800; color: var(--sa-muted); text-transform: uppercase; letter-spacing: .04em; }
.sa-kpi .sa-kpi-icon { width: 28px; height: 28px; border-radius: 8px; display: grid; place-items: center; font-size: 14px;
    background: color-mix(in srgb, var(--kpi-accent, var(--sa-accent)) 15%, transparent); }
.sa-kpi .sa-kpi-value { font-size: clamp(1.05rem,.85rem+.7vw,1.4rem); font-weight: 800; letter-spacing: -.02em; line-height: 1.05; }
.sa-kpi .sa-kpi-foot { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-top: auto; }
.sa-kpi .sa-kpi-prev { font-size: 11px; color: var(--sa-muted); font-weight: 700; }
.sa-delta { font-size: 12px; font-weight: 900; display: inline-flex; align-items: center; gap: 3px; }
.sa-delta.up { color: var(--sa-pos); } .sa-delta.down { color: var(--sa-neg); } .sa-delta.flat { color: var(--sa-muted); }

/* ---- panels / chart grid ---- */
.sa-grid-1 { display: grid; grid-template-columns: minmax(0, 1fr); gap: var(--sa-gap); margin-bottom: var(--sa-gap); }
.sa-grid-2 { display: grid; gap: var(--sa-gap); grid-template-columns: repeat(auto-fit, minmax(360px, 1fr)); margin-bottom: var(--sa-gap); }
/* exactly two cards per row (6/6), regardless of screen width — collapses to 1 on small screens below */
.sa-grid-2c { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.sa-panel { position: relative; background: var(--sa-surface); border: 1px solid var(--sa-glass-brd); border-radius: var(--sa-radius);
    box-shadow: var(--sa-shadow); padding: 16px; min-width: 0; backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px); transition: box-shadow .16s; }
.sa-panel:hover { box-shadow: var(--sa-shadow-lg); }
.sa-panel h3 { margin: 0 0 12px; font-size: 14.5px; font-weight: 800; letter-spacing: -.01em; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.sa-panel h3 .sa-pill { font-size: 10.5px; font-weight: 800; color: var(--sa-muted); background: var(--sa-surface-2); border: 1px solid var(--sa-line); border-radius: 999px; padding: 2px 9px; text-transform: uppercase; letter-spacing: .04em; }

/* 0→100% progress preloader (mirrors the Pivot report bar) */
.sa-progress { position: relative; display: flex; align-items: center; gap: 12px; margin-bottom: var(--sa-gap);
    padding: 10px 16px; background: var(--sa-surface); border: 1px solid var(--sa-line); border-radius: var(--sa-radius-sm); box-shadow: var(--sa-shadow); }
.sa-progress__bar { position: relative; flex: 1; height: 9px; border-radius: 6px; background: var(--sa-canvas-2); overflow: hidden; }
.sa-progress__fill { position: absolute; top: 0; left: 0; height: 100%; width: 0; border-radius: 6px;
    background: linear-gradient(90deg, var(--sa-teal), var(--sa-accent)); transition: width .25s ease; }
.sa-progress__pct { font-size: 13px; font-weight: 800; color: var(--sa-teal); min-width: 42px; text-align: right; font-variant-numeric: tabular-nums; }
.sa-progress__label { font-size: 12.5px; font-weight: 600; color: var(--sa-muted); white-space: nowrap; font-variant-numeric: tabular-nums; }

.sa-chart { width: 100%; min-height: 300px; }
.sa-chart-sm { min-height: 260px; }
.sa-chart-tall { min-height: 340px; }

/* ---- pie companion metric table -------------------------------------------
   The donut answers "what share?"; this table answers "how much?" — Sales,
   Target, Ach, GOLM and GOLY per slice, without needing a hover. The colour
   swatch ties each row to its slice. */
.sa-mtable-wrap { margin-top: 12px; max-height: 232px; overflow: auto; border: 1px solid var(--sa-line); border-radius: var(--sa-radius-sm); }
.sa-mtable { width: 100%; border-collapse: collapse; font-size: 12px; font-variant-numeric: tabular-nums; }
.sa-mtable thead th {
    position: sticky; top: 0; z-index: 1; background: var(--sa-surface-2); color: var(--sa-muted);
    font-size: 10.5px; font-weight: 800; text-transform: uppercase; letter-spacing: .05em;
    text-align: right; padding: 7px 9px; border-bottom: 1px solid var(--sa-line); white-space: nowrap;
}
.sa-mtable thead th:first-child { text-align: left; }
.sa-mtable tbody td { padding: 6px 9px; text-align: right; border-bottom: 1px solid var(--sa-line); color: var(--sa-ink-2); white-space: nowrap; font-weight: 700; }
.sa-mtable tbody td:first-child { text-align: left; color: var(--sa-ink); max-width: 168px; overflow: hidden; text-overflow: ellipsis; }
.sa-mtable tbody tr:last-child td { border-bottom: 0; }
.sa-mtable tbody tr:hover td { background: var(--sa-surface-2); }
.sa-mtable tfoot td {
    position: sticky; bottom: 0; background: var(--sa-surface-2); font-weight: 900; color: var(--sa-ink);
    padding: 7px 9px; text-align: right; border-top: 1px solid var(--sa-line); white-space: nowrap;
}
.sa-mtable tfoot td:first-child { text-align: left; }
.sa-sw { display: inline-block; width: 9px; height: 9px; border-radius: 3px; margin-right: 7px; vertical-align: -1px; flex: 0 0 auto; }
/* GOLM/GOLY growth colours are applied inline by saGrowthHtml() — a class here would
   lose to `.sa-mtable tbody td`, which is exactly why those cells used to render grey. */

/* Any abbreviated figure (12.75M) whose exact value is available on hover. */
.sa-abs { cursor: help; text-decoration: underline dotted color-mix(in srgb, currentColor 35%, transparent); text-underline-offset: 3px; }

/* ---- hierarchy treemap ---- */
.sa-tm-bar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-bottom: 10px; }
.sa-tm-spacer { flex: 1 1 auto; }
.sa-btn-sm { padding: 5px 12px; font-size: 12px; border-radius: 8px; }
.sa-tm-crumb { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; font-size: 12.5px; min-height: 22px; }
.sa-tm-link { color: var(--sa-accent); font-weight: 700; text-decoration: none; }
.sa-tm-link:hover { text-decoration: underline; }
.sa-tm-here { font-weight: 800; color: var(--sa-ink); }
.sa-tm-sep { color: var(--sa-muted); opacity: .7; }
.sa-tm-next { font-size: 10.5px; font-weight: 800; text-transform: uppercase; letter-spacing: .05em;
    color: var(--sa-muted); background: var(--sa-surface-2); border: 1px solid var(--sa-line); border-radius: 999px; padding: 2px 9px; }
/* metric strip for whichever level is currently in view */
.sa-tm-stats { display: grid; gap: 8px; grid-template-columns: repeat(auto-fit, minmax(118px, 1fr)); margin-bottom: 10px; }
.sa-tm-stat { background: var(--sa-surface-2); border: 1px solid var(--sa-line); border-radius: var(--sa-radius-sm); padding: 7px 10px; min-width: 0; }
.sa-tm-stat span { display: block; font-size: 10px; font-weight: 800; text-transform: uppercase; letter-spacing: .05em; color: var(--sa-muted); }
.sa-tm-stat b { display: block; font-size: 13.5px; font-weight: 800; letter-spacing: -.01em; margin-top: 2px;
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sa-treemap { width: 100%; min-height: 430px; }
.sa-tm-note { font-size: 11.5px; opacity: .6; margin-top: 9px; }
@media (max-width: 640px) { .sa-tm-stats { grid-template-columns: repeat(auto-fit, minmax(96px, 1fr)); } }

/* ---- data grid panel ---- */
.sa-gridpanel { background: var(--sa-surface); border: 1px solid var(--sa-glass-brd); border-radius: var(--sa-radius); box-shadow: var(--sa-shadow); padding: 16px; backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px); }
.sa-gridpanel h3 { margin: 0 0 12px; font-size: 14.5px; font-weight: 800; }
#saGrid { min-height: 380px; }

/* ---- skeleton / empty ---- */
.sa-skel { position: relative; overflow: hidden; background: var(--sa-surface-2); border-radius: 10px; }
.sa-skel::after { content: ""; position: absolute; inset: 0; transform: translateX(-100%);
    background: linear-gradient(90deg, transparent, color-mix(in srgb, var(--sa-ink) 9%, transparent), transparent); animation: sa-shimmer 1.25s infinite; }
@keyframes sa-shimmer { 100% { transform: translateX(100%); } }
.sa-empty { color: var(--sa-muted); text-align: center; padding: 44px 10px; font-weight: 700; }

@media (max-width: 900px) {
    .sa-grid-2c { grid-template-columns: 1fr; }
}
@media (max-width: 640px) {
    .sa-kpis { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
    .sa-filters { grid-template-columns: 1fr 1fr; }
    .sa-grid-2, .sa-grid-2c { grid-template-columns: 1fr; }
    .sa-chipsbar { flex-direction: column; align-items: stretch; }
    .sa-chips { max-height: 34px; }
    .sa-chips.expanded { max-height: 30vh; }
    .sa-chip { max-width: 100%; }
    .sa-mtable-wrap { max-height: 260px; }
}
