: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);
  --accent: #2a78d6;
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --good: #0ca30c;
  --good-text: #006300;
  --warning: #fab219;
  --serious: #ec835a;
  --critical: #d03b3b;
  --bad-text: #b02a2a;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #242423;
    --ink: #f0efec;
    --ink-2: #c3c2b7;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.10);
    --accent: #3987e5;
    --series-1: #3987e5;
    --series-2: #d95926;
    --good-text: #0ca30c;
    --bad-text: #e66767;
    --shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d; --surface: #1a1a19; --surface-2: #242423; --ink: #f0efec; --ink-2: #c3c2b7;
  --grid: #2c2c2a; --axis: #383835; --border: rgba(255, 255, 255, 0.10); --accent: #3987e5;
  --series-1: #3987e5; --series-2: #d95926; --good-text: #0ca30c; --bad-text: #e66767;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body {
  margin: 0;
  background: var(--page);
  color: var(--ink);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
}
button, input, select { font: inherit; color: inherit; }
code { font-size: 0.9em; }
.muted { color: var(--muted); }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.pos { color: var(--good-text); }
.neg { color: var(--bad-text); }

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 16px;
  padding: 10px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 650; white-space: nowrap; }
.tabs { display: flex; gap: 2px; overflow-x: auto; flex: 1; scrollbar-width: none; }
.tabs button {
  border: 0; background: none; padding: 6px 10px; border-radius: 6px;
  color: var(--ink-2); cursor: pointer; white-space: nowrap;
}
.tabs button:hover { background: var(--surface-2); }
.tabs button.active { background: var(--surface-2); color: var(--ink); font-weight: 600; }
.top-right { display: flex; align-items: center; gap: 10px; white-space: nowrap; }
.sync { font-size: 12px; color: var(--muted); }
.sync .dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 5px; }
.ghost { border: 1px solid var(--border); background: none; border-radius: 6px; padding: 4px 10px; cursor: pointer; }
.ghost:hover { background: var(--surface-2); }
button.primary {
  border: 0; background: var(--accent); color: #fff; border-radius: 6px; padding: 7px 14px; cursor: pointer; font-weight: 600;
}
button.primary:disabled { opacity: 0.5; cursor: default; }

.banner {
  margin: 12px 20px 0; padding: 8px 12px; border-radius: 8px;
  background: var(--surface-2); color: var(--ink-2); font-size: 13px;
}

.filters {
  display: flex; flex-wrap: wrap; align-items: end; gap: 14px;
  padding: 14px 20px 0;
}
.filters label { display: flex; flex-direction: column; gap: 3px; font-size: 12px; color: var(--muted); }
select, input[type=text], input[type=email], input[type=search] {
  background: var(--surface); border: 1px solid var(--border); border-radius: 6px; padding: 6px 8px; min-width: 150px;
}
.period-label { font-size: 12px; color: var(--muted); padding-bottom: 7px; }

main { padding: 16px 20px 40px; max-width: 1400px; }
h2 { font-size: 16px; margin: 26px 0 10px; }
h2:first-child { margin-top: 4px; }
.sub { font-size: 12px; color: var(--muted); font-weight: 400; margin-left: 6px; }

/* Stat tiles */
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(190px, calc(50% - 6px)), 1fr)); gap: 12px; }
.tile {
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 12px 14px;
}
.tile .label { font-size: 12px; color: var(--ink-2); }
.tile .value { font-size: 26px; font-weight: 600; margin-top: 2px; letter-spacing: -0.01em; }
.tile .note { font-size: 12px; color: var(--muted); margin-top: 2px; }
.tile.clickable { cursor: pointer; }
.tile.clickable:hover { border-color: var(--accent); }

.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(420px, 1fr)); gap: 16px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 14px; }
.card h3 { margin: 0 0 8px; font-size: 14px; }

/* Aging chips */
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  border: 1px solid var(--border); border-radius: 999px; padding: 4px 12px; background: var(--surface);
  cursor: pointer; font-size: 13px;
}
.chip.active { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
.chip .icon { width: 10px; height: 10px; border-radius: 50%; }
.chip b { font-variant-numeric: tabular-nums; }

/* Tables */
.table-wrap { overflow: auto; background: var(--surface); border: 1px solid var(--border); border-radius: 10px; max-height: 70vh; }
table { border-collapse: collapse; width: 100%; font-size: 13px; }
th, td { padding: 7px 10px; border-bottom: 1px solid var(--grid); text-align: left; vertical-align: top; }
th {
  position: sticky; top: 0; background: var(--surface); z-index: 1;
  font-weight: 600; color: var(--ink-2); cursor: pointer; user-select: none; white-space: nowrap;
}
th.sorted::after { content: " ▾"; color: var(--muted); }
th.sorted.asc::after { content: " ▴"; }
tbody tr:hover { background: var(--surface-2); }
tbody tr.clickable { cursor: pointer; }
tfoot td { font-weight: 600; background: var(--surface-2); position: sticky; bottom: 0; }
.skus { max-width: 260px; color: var(--ink-2); font-size: 12px; }
.table-tools { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin-bottom: 8px; }
.table-tools .count { color: var(--muted); font-size: 12px; }

.badge {
  display: inline-flex; align-items: center; gap: 5px; font-size: 12px; white-space: nowrap;
}
.badge .icon { width: 8px; height: 8px; border-radius: 50%; flex: none; }

/* Breakdown */
.breakdown { width: auto; }
.breakdown td, .breakdown th { border: 0; padding: 5px 20px 5px 0; position: static; cursor: default; }
.breakdown td:first-child { white-space: nowrap; }
.breakdown td.num, .breakdown th.num { min-width: 110px; }
.breakdown tbody tr:hover { background: none; }
.breakdown tr.total td { border-top: 1px solid var(--axis); font-weight: 650; padding-top: 8px; }
.breakdown .why { color: var(--muted); font-size: 12px; min-width: 240px; }

/* Charts */
.chart { width: 100%; height: 220px; }
.chart svg { display: block; width: 100%; height: 100%; overflow: visible; }
.chart .gridline { stroke: var(--grid); stroke-width: 1; }
.chart .baseline { stroke: var(--axis); stroke-width: 1; }
.chart .tick { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .bar { fill: var(--series-1); }
.chart .bar.alt { fill: var(--series-2); }
.chart .hit { fill: transparent; cursor: default; }
.chart .hit:hover + .bar, .chart .bar.hover { opacity: 0.8; }
.empty { color: var(--muted); padding: 24px; text-align: center; }

.tooltip {
  position: fixed; z-index: 50; pointer-events: none;
  background: var(--surface); color: var(--ink); border: 1px solid var(--border); box-shadow: var(--shadow);
  border-radius: 8px; padding: 7px 10px; font-size: 12px; max-width: 260px;
}
.tooltip b { font-variant-numeric: tabular-nums; }

/* Drawer */
.drawer { position: fixed; inset: 0; z-index: 40; background: rgba(0, 0, 0, 0.25); }
.drawer-inner {
  position: absolute; right: 0; top: 0; bottom: 0; width: min(560px, 100%);
  background: var(--surface); box-shadow: var(--shadow); overflow-y: auto; padding: 20px 22px;
}
.drawer .close { position: absolute; right: 14px; top: 14px; }
.drawer h1 { font-size: 20px; margin: 0 0 4px; }
.kv { display: grid; grid-template-columns: 140px 1fr; gap: 4px 12px; font-size: 13px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }

/* WWEX upload */
.dropzone {
  display: block;
  border: 2px dashed var(--axis); border-radius: 10px; padding: 28px; text-align: center; color: var(--ink-2);
  background: var(--surface); cursor: pointer;
}
.dropzone.drag { border-color: var(--accent); }
.mapping { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 10px; margin: 12px 0; }
.mapping label { display: flex; flex-direction: column; gap: 3px; font-size: 12px; color: var(--ink-2); }
.mapping select { min-width: 0; }
.note-box { background: var(--surface-2); border-radius: 8px; padding: 10px 12px; font-size: 13px; color: var(--ink-2); }

/* Login */
.login { position: fixed; inset: 0; z-index: 60; display: grid; place-items: center; background: var(--page); }
.login form { width: min(360px, calc(100% - 32px)); display: flex; flex-direction: column; gap: 10px; }
.login h1 { margin: 0; font-size: 20px; }

@media (max-width: 700px) {
  .topbar { flex-wrap: wrap; padding: 10px 16px; }
  .tabs { order: 3; flex-basis: 100%; }
  .filters, main { padding-left: 16px; padding-right: 16px; }
  .grid2 { grid-template-columns: 1fr; }
  .kv { grid-template-columns: 110px 1fr; }
}
