/* Tokens: the dataviz reference palette (validated), light and dark selected separately. */
:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f3f2ee;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --series-1: #2a78d6;   /* blue: this period */
  --series-2: #eb6834;   /* orange */
  --series-3: #1baf7a;   /* aqua */
  --prior: #c3c2b7;      /* prior period: de-emphasis gray */
  --spark: #c3c2b7;
  --good-text: #006300;
  --bad-text: #b42f2f;
  --good: #0ca30c;
  --warning: #fab219;
  --critical: #d03b3b;
  --wash: rgba(42, 120, 214, 0.10);
  --hover: rgba(11, 11, 11, 0.04);
  --shadow: 0 1px 2px rgba(11, 11, 11, 0.04), 0 8px 24px rgba(11, 11, 11, 0.04);
}
@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --page: #0d0d0d; --surface: #1a1a19; --surface-2: #232321;
    --ink: #ffffff; --ink-2: #c3c2b7; --muted: #898781; --grid: #2c2c2a; --axis: #383835;
    --border: rgba(255, 255, 255, 0.10);
    --series-1: #3987e5; --series-2: #d95926; --series-3: #199e70;
    --prior: #5a5955; --spark: #5a5955;
    --good-text: #0ca30c; --bad-text: #e66767;
    --wash: rgba(57, 135, 229, 0.14); --hover: rgba(255, 255, 255, 0.05);
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d; --surface: #1a1a19; --surface-2: #232321;
  --ink: #ffffff; --ink-2: #c3c2b7; --muted: #898781; --grid: #2c2c2a; --axis: #383835;
  --border: rgba(255, 255, 255, 0.10);
  --series-1: #3987e5; --series-2: #d95926; --series-3: #199e70;
  --prior: #5a5955; --spark: #5a5955;
  --good-text: #0ca30c; --bad-text: #e66767;
  --wash: rgba(57, 135, 229, 0.14); --hover: rgba(255, 255, 255, 0.05);
  --shadow: none;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--page);
  color: var(--ink);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
}
.muted { color: var(--muted); }
.small { font-size: 12px; }
.label { font-size: 13px; color: var(--ink-2); font-weight: 500; }

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px max(16px, calc((100vw - 1280px) / 2 + 24px));
  background: color-mix(in srgb, var(--page) 85%, transparent);
  backdrop-filter: saturate(180%) blur(12px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand-mark { flex: none; width: 20px; height: 20px; border-radius: 50%; border: 3px solid var(--series-1); box-shadow: inset 0 0 0 3px var(--wash); }
.brand-name { font-weight: 700; letter-spacing: 0.06em; }
.brand-sep { color: var(--axis); }
.client-name { font-weight: 600; text-transform: capitalize; }
.pill { font-size: 12px; color: var(--ink-2); background: var(--surface-2); border: 1px solid var(--border); border-radius: 999px; padding: 2px 10px; white-space: nowrap; }
.topbar-right { display: flex; align-items: center; gap: 12px; }
.icon-btn { display: inline-grid; place-items: center; width: 32px; height: 32px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface); color: var(--ink-2); cursor: pointer; }
.icon-btn:hover { background: var(--hover); color: var(--ink); }
.icon-btn:focus-visible, .seg:focus-visible, button:focus-visible, input:focus-visible { outline: 2px solid var(--series-1); outline-offset: 2px; }

.page { max-width: 1280px; margin: 0 auto; padding: 20px 24px 48px; display: grid; gap: 16px; }

.notice { display: flex; align-items: center; gap: 8px; color: var(--ink-2); font-size: 13px; padding: 8px 12px; border-radius: 10px; background: var(--surface-2); border: 1px solid var(--border); }
.notice svg { color: var(--muted); flex: none; }

/* Filters: one row above everything they scope */
.filters { display: flex; flex-wrap: wrap; align-items: center; gap: 12px 16px; }
.segmented { display: inline-flex; padding: 3px; gap: 2px; border-radius: 10px; background: var(--surface-2); border: 1px solid var(--border); }
.seg { border: 0; background: transparent; color: var(--ink-2); font: inherit; font-weight: 500; padding: 6px 12px; border-radius: 7px; cursor: pointer; white-space: nowrap; }
.seg:hover { color: var(--ink); }
.seg[aria-selected="true"] { background: var(--surface); color: var(--ink); box-shadow: 0 1px 2px rgba(0,0,0,.08); border: 1px solid var(--border); padding: 5px 11px; }
.seg .tag { font-size: 11px; color: var(--muted); margin-left: 6px; font-weight: 500; }
.coverage { display: flex; flex-wrap: wrap; gap: 6px; margin-left: auto; }
.chip { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--ink-2); padding: 3px 9px; border-radius: 999px; border: 1px solid var(--border); background: var(--surface); }
.chip .dot { width: 8px; height: 8px; border-radius: 50%; }
.chip .dot.full { background: var(--good); }
.chip .dot.partial { background: var(--warning); }
.chip .dot.none { background: var(--critical); }

/* Cards */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 16px 18px; box-shadow: var(--shadow); min-width: 0; }
.card-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; margin-bottom: 8px; flex-wrap: wrap; }
.card-title { font-weight: 600; font-size: 15px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }

/* Hero */
.hero-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; flex-wrap: wrap; }
.hero-value { font-size: 48px; font-weight: 600; letter-spacing: -0.02em; line-height: 1.1; margin: 4px 0; }
.hero-sub { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

/* Legend */
.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 12px; color: var(--ink-2); }
.legend .key { display: inline-flex; align-items: center; gap: 6px; }
.legend .swatch { width: 10px; height: 10px; border-radius: 3px; }
.legend .line { width: 14px; height: 2px; border-radius: 1px; }

/* KPI tiles */
.kpis { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 14px 16px 10px; display: grid; grid-template-rows: auto auto auto 34px; gap: 2px; box-shadow: var(--shadow); min-width: 0; }
.kpi .value { font-size: 24px; font-weight: 600; letter-spacing: -0.01em; }
.kpi .value.na { color: var(--muted); }
.kpi .foot { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; min-height: 18px; }
.kpi .spark { width: 100%; height: 34px; }
.kpi .cov { font-size: 11px; color: var(--muted); }
.delta { display: inline-flex; align-items: center; gap: 3px; font-size: 12px; font-weight: 600; }
.delta.good { color: var(--good-text); }
.delta.bad { color: var(--bad-text); }
.delta.flat { color: var(--muted); }
.delta svg { width: 10px; height: 10px; }

/* Charts */
.chart { position: relative; width: 100%; }
.chart svg { display: block; width: 100%; height: 100%; overflow: visible; }
.chart text { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .gridline { stroke: var(--grid); stroke-width: 1; shape-rendering: crispEdges; }
.chart .baseline { stroke: var(--axis); stroke-width: 1; shape-rendering: crispEdges; }
.chart .crosshair { stroke: var(--axis); stroke-width: 1; shape-rendering: crispEdges; pointer-events: none; }
.chart .gap-day { fill: var(--surface-2); }
.chart .empty { fill: var(--muted); font-size: 12px; }

.tooltip { position: fixed; z-index: 50; pointer-events: none; opacity: 0; transition: opacity .08s; background: var(--surface); color: var(--ink); border: 1px solid var(--border); border-radius: 10px; padding: 8px 10px; font-size: 12px; box-shadow: 0 8px 24px rgba(0,0,0,.16); min-width: 140px; }
.tooltip.show { opacity: 1; }
.tooltip .t-title { color: var(--muted); margin-bottom: 4px; }
.tooltip .t-row { display: grid; grid-template-columns: 14px 1fr; align-items: center; column-gap: 6px; }
.tooltip .t-key { width: 12px; height: 2px; border-radius: 1px; }
.tooltip .t-val { font-weight: 600; font-variant-numeric: tabular-nums; }
.tooltip .t-name { color: var(--ink-2); grid-column: 2; margin-top: -2px; }

/* Product table */
.table-tools { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.search { display: inline-flex; align-items: center; gap: 6px; padding: 5px 10px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface-2); color: var(--muted); }
.search input { border: 0; outline: 0; background: transparent; color: var(--ink); font: inherit; width: 200px; }
.check { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--ink-2); cursor: pointer; }
.table-wrap { overflow-x: auto; margin: 0 -18px -16px; }
.ptable { width: 100%; border-collapse: collapse; font-size: 13px; }
.ptable th { position: sticky; top: 0; background: var(--surface); text-align: right; font-weight: 500; color: var(--muted); font-size: 12px; padding: 8px 12px; border-bottom: 1px solid var(--grid); white-space: nowrap; cursor: pointer; user-select: none; }
.ptable th:first-child, .ptable td:first-child { text-align: left; padding-left: 18px; }
.ptable th:last-child, .ptable td:last-child { padding-right: 18px; }
.ptable th[aria-sort] { color: var(--ink); }
.ptable th .arrow { font-size: 10px; margin-left: 3px; }
.ptable td { padding: 9px 12px; text-align: right; border-bottom: 1px solid var(--grid); font-variant-numeric: tabular-nums; white-space: nowrap; }
.ptable tr.fam { cursor: pointer; }
.ptable tr.fam:hover td, .ptable tr.sku:hover td { background: var(--hover); }
.ptable tr.sku { cursor: pointer; }
.ptable tr.sku td:first-child { padding-left: 42px; color: var(--ink-2); }
.ptable .pname { display: flex; align-items: center; gap: 8px; min-width: 260px; max-width: 420px; }
.ptable .pname .txt { overflow: hidden; text-overflow: ellipsis; }
.ptable .pname .sub { display: block; font-size: 11px; color: var(--muted); }
.ptable .caret { width: 14px; flex: none; color: var(--muted); transition: transform .12s; }
.ptable tr.open .caret { transform: rotate(90deg); }
.ptable .na { color: var(--muted); }
.ptable .share { display: inline-flex; align-items: center; gap: 8px; justify-content: flex-end; }
.ptable .bar { display: inline-block; width: 56px; height: 6px; border-radius: 3px; background: var(--surface-2); overflow: hidden; }
.ptable .bar i { display: block; height: 100%; background: var(--series-1); border-radius: 0 3px 3px 0; }
.badge { font-size: 10px; font-weight: 600; letter-spacing: .02em; color: var(--ink-2); background: var(--surface-2); border: 1px solid var(--border); border-radius: 4px; padding: 0 4px; }

/* Drawer */
.drawer { position: fixed; top: 0; right: 0; bottom: 0; width: min(440px, 100vw); z-index: 40; background: var(--surface); border-left: 1px solid var(--border); padding: 18px; overflow-y: auto; transform: translateX(100%); transition: transform .18s ease; display: grid; align-content: start; gap: 14px; }
.drawer.open { transform: none; box-shadow: -12px 0 32px rgba(0,0,0,.12); }
.drawer-head { display: flex; justify-content: space-between; gap: 12px; }
.drawer-head .card-title { word-break: break-all; }
.d-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.d-stat { background: var(--surface-2); border-radius: 10px; padding: 8px 10px; }
.d-stat .v { font-size: 16px; font-weight: 600; }
.scrim { position: fixed; inset: 0; z-index: 30; background: rgba(0,0,0,.18); opacity: 0; pointer-events: none; transition: opacity .18s; }
.scrim.show { opacity: 1; pointer-events: auto; }

.foot { padding: 4px 2px; }
.loading .chart, .loading .kpi, .loading #ptable { opacity: .45; transition: opacity .15s; }

@media (max-width: 1000px) {
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid-2 { grid-template-columns: 1fr; }
  .coverage { margin-left: 0; }
}
@media (max-width: 560px) {
  .page { padding: 16px 16px 40px; }
  .hero-value { font-size: 40px; }
  .search input { width: 140px; }
  .client-name, .brand-sep, #as-of { display: none; }
}

/* Buttons and the download menu */
.btn { display: inline-flex; align-items: center; gap: 6px; height: 32px; padding: 0 12px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface); color: var(--ink); font: inherit; font-size: 13px; font-weight: 500; cursor: pointer; }
.btn:hover { background: var(--hover); }
.btn.ghost { background: transparent; color: var(--ink-2); }
.dl { position: relative; }
.menu { position: absolute; right: 0; top: 38px; z-index: 30; min-width: 240px; background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 4px; box-shadow: 0 8px 24px rgba(0,0,0,.16); display: grid; }
.menu[hidden] { display: none; }
.menu button { text-align: left; border: 0; background: transparent; color: var(--ink); font: inherit; font-size: 13px; padding: 8px 10px; border-radius: 6px; cursor: pointer; }
.menu button:hover, .menu button:focus-visible { background: var(--hover); outline: none; }
@media (max-width: 560px) { #user, .btn.ghost { display: none; } }
