/* ---------- tokens ---------- */
:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface-1: #fcfcfb;
  --surface-2: #f3f2ee;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #6f6d68;
  --axis-muted: #898781;
  --grid: #e1e0d9;
  --baseline: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --border-strong: rgba(11, 11, 11, 0.18);
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --good-ink: #006300;
  --bad-ink: #b42323;
  --warn-ink: #8a5a00;
  --good-bg: rgba(12, 163, 12, 0.10);
  --bad-bg: rgba(208, 59, 59, 0.10);
  --warn-bg: rgba(250, 178, 25, 0.16);
  --changed-bg: rgba(42, 120, 214, 0.08);
  --series-1: #2a78d6; --series-2: #eb6834; --series-3: #1baf7a; --series-4: #eda100;
  --series-5: #e87ba4; --series-6: #008300; --series-7: #4a3aa7; --series-8: #e34948;
  --series-0: #898781;
  --ord-1: #86b6ef; --ord-2: #5598e7; --ord-3: #2a78d6; --ord-4: #1c5cab; --ord-5: #104281;
  --total-ink: #0b0b0b;
  --radius: 10px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d; --surface-1: #1a1a19; --surface-2: #232321;
    --text-primary: #ffffff; --text-secondary: #c3c2b7; --text-muted: #9a988f; --axis-muted: #898781;
    --grid: #2c2c2a; --baseline: #383835; --border: rgba(255,255,255,0.10); --border-strong: rgba(255,255,255,0.18);
    --accent: #3987e5; --accent-ink: #ffffff;
    --good-ink: #3fc23f; --bad-ink: #f07a7a; --warn-ink: #fab219;
    --good-bg: rgba(12,163,12,0.16); --bad-bg: rgba(208,59,59,0.18); --warn-bg: rgba(250,178,25,0.14); --changed-bg: rgba(57,135,229,0.14);
    --series-1: #3987e5; --series-2: #d95926; --series-3: #199e70; --series-4: #c98500;
    --series-5: #d55181; --series-6: #008300; --series-7: #9085e9; --series-8: #e66767;
    --series-0: #898781;
    --ord-1: #184f95; --ord-2: #256abf; --ord-3: #3987e5; --ord-4: #6da7ec; --ord-5: #9ec5f4;
    --total-ink: #ffffff;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d; --surface-1: #1a1a19; --surface-2: #232321;
  --text-primary: #ffffff; --text-secondary: #c3c2b7; --text-muted: #9a988f; --axis-muted: #898781;
  --grid: #2c2c2a; --baseline: #383835; --border: rgba(255,255,255,0.10); --border-strong: rgba(255,255,255,0.18);
  --accent: #3987e5; --accent-ink: #ffffff;
  --good-ink: #3fc23f; --bad-ink: #f07a7a; --warn-ink: #fab219;
  --good-bg: rgba(12,163,12,0.16); --bad-bg: rgba(208,59,59,0.18); --warn-bg: rgba(250,178,25,0.14); --changed-bg: rgba(57,135,229,0.14);
  --series-1: #3987e5; --series-2: #d95926; --series-3: #199e70; --series-4: #c98500;
  --series-5: #d55181; --series-6: #008300; --series-7: #9085e9; --series-8: #e66767;
  --series-0: #898781;
  --ord-1: #184f95; --ord-2: #256abf; --ord-3: #3987e5; --ord-4: #6da7ec; --ord-5: #9ec5f4;
  --total-ink: #ffffff;
}

/* ---------- base ---------- */
* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--page); color: var(--text-primary); font: 14px/1.45 var(--font); }
button, input, select, textarea { font: inherit; color: inherit; }
select { max-width: 100%; }
h1, h2, h3 { margin: 0; font-weight: 600; }
h2 { font-size: 15px; }
h3 { font-size: 13px; }
a { color: var(--accent); }
.muted { color: var(--text-muted); }
.small { font-size: 12px; }
.num { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
.hidden { display: none !important; }
.view-only .edit-only { display: none !important; } /* body.view-only: logged in with the view account */
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- header / tabs ---------- */
.app-header {
  display: flex; align-items: center; gap: 16px; flex-wrap: wrap;
  padding: 12px 24px; background: var(--surface-1); border-bottom: 1px solid var(--border);
}
.brand { display: flex; flex-direction: column; margin-right: 8px; }
.brand h1 { font-size: 16px; }
.brand span { font-size: 12px; color: var(--text-muted); }
.tabs { display: flex; gap: 2px; flex-wrap: wrap; flex: 1; }
.tab {
  border: 0; background: transparent; padding: 7px 12px; border-radius: 8px; cursor: pointer; color: var(--text-secondary);
}
.tab:hover { background: var(--surface-2); }
.tab[aria-selected="true"] { background: var(--surface-2); color: var(--text-primary); font-weight: 600; }
.tab .badge { margin-left: 6px; }
.header-actions { display: flex; gap: 8px; align-items: center; }
.user-box { display: flex; gap: 8px; align-items: center; white-space: nowrap; }
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: min(340px, 100%); display: flex; flex-direction: column; gap: 12px; }
.login-card h1 { font-size: 18px; }

main { padding: 16px 24px 48px; max-width: 1480px; margin: 0 auto; }
.view { display: none; }
.view.active { display: block; }

/* ---------- controls ---------- */
.btn {
  border: 1px solid var(--border-strong); background: var(--surface-1); padding: 6px 12px; border-radius: 8px; cursor: pointer;
  display: inline-flex; align-items: center; gap: 6px; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.06); }
.btn.danger { color: var(--bad-ink); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn:disabled { opacity: 0.5; cursor: default; }
.btn.icon { padding: 6px 8px; }
select, input[type="text"], input[type="password"], input[type="month"], input[type="number"], textarea {
  background: var(--surface-1); border: 1px solid var(--border-strong); border-radius: 8px; padding: 6px 8px; min-width: 0;
}
label.field { display: flex; flex-direction: column; gap: 3px; font-size: 12px; color: var(--text-secondary); }
.seg { display: inline-flex; border: 1px solid var(--border-strong); border-radius: 8px; overflow: hidden; }
.seg button { border: 0; background: var(--surface-1); padding: 5px 10px; cursor: pointer; color: var(--text-secondary); }
.seg button + button { border-left: 1px solid var(--border); }
.seg button[aria-pressed="true"] { background: var(--surface-2); color: var(--text-primary); font-weight: 600; }

/* ---------- filter bar ---------- */
.filters {
  position: sticky; top: 0; z-index: 5; display: flex; flex-wrap: wrap; gap: 10px 16px; align-items: flex-end;
  padding: 12px 14px; margin: 0 0 16px; background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--radius);
}
.filters .group { display: flex; flex-direction: column; gap: 4px; }
.filters .group > .lbl { font-size: 12px; color: var(--text-muted); }
.filters .row { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px; border-radius: 999px; cursor: pointer;
  border: 1px solid var(--border-strong); background: var(--surface-1); color: var(--text-secondary); font-size: 13px;
}
.chip .dot { width: 10px; height: 10px; border-radius: 50%; background: var(--c); flex: none; }
.chip[aria-pressed="false"] { opacity: 0.55; }
.chip[aria-pressed="false"] .dot { background: transparent; box-shadow: inset 0 0 0 2px var(--c); }
.chip[aria-pressed="true"] { color: var(--text-primary); }

/* ---------- KPI tiles ---------- */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(215px, 100%), 1fr)); gap: 12px; margin-bottom: 16px; }
.tile { background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; }
.tile .label { font-size: 12px; color: var(--text-secondary); display: flex; justify-content: space-between; gap: 8px; }
.tile .value { font-size: 24px; font-weight: 600; margin: 4px 0 6px; letter-spacing: -0.01em; }
.tile .deltas { display: flex; flex-direction: column; gap: 2px; font-size: 12px; }
.tile .sub { font-size: 12px; color: var(--text-muted); margin-top: 6px; border-top: 1px solid var(--border); padding-top: 6px; }
.delta { display: flex; flex-wrap: wrap; align-items: baseline; column-gap: 6px; }
.delta b { font-weight: 600; white-space: nowrap; }
.delta .ctx { white-space: nowrap; }
.delta.good b { color: var(--good-ink); }
.delta.bad b { color: var(--bad-ink); }
.delta.flat b { color: var(--text-secondary); }
.delta .ctx { color: var(--text-muted); }

/* ---------- cards & grid ---------- */
.grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.grid .wide { grid-column: 1 / -1; }
@media (max-width: 1000px) { .grid { grid-template-columns: minmax(0, 1fr); } }
.card { background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px 16px; min-width: 0; }
.card-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; margin-bottom: 10px; flex-wrap: wrap; }
.card-head .titles { min-width: 0; }
.card-head .sub { font-size: 12px; color: var(--text-muted); margin-top: 2px; }
.card-head .tools { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.chart-box { position: relative; height: 320px; transition: opacity .15s; }
.chart-box.tall { height: 380px; }
.chart-box.loading { opacity: 0.5; }
.empty { display: grid; place-items: center; height: 100%; color: var(--text-muted); font-size: 13px; text-align: center; padding: 16px; }
.table-view { max-height: 360px; overflow: auto; border: 1px solid var(--border); border-radius: 8px; }

/* ---------- tables ---------- */
table.data { border-collapse: collapse; width: 100%; font-size: 13px; }
table.data th, table.data td { padding: 6px 10px; border-bottom: 1px solid var(--border); text-align: left; }
table.data thead th { position: sticky; top: 0; background: var(--surface-2); font-weight: 600; font-size: 12px; color: var(--text-secondary); z-index: 1; white-space: nowrap; }
table.data td.num, table.data th.num { text-align: right; font-variant-numeric: tabular-nums; }
table.data tbody tr:hover { background: var(--surface-2); }
table.data tfoot td { font-weight: 600; background: var(--surface-2); }
table.data td.clickable { cursor: pointer; }
table.data td.clickable:hover { text-decoration: underline; }
.scroll-x { overflow-x: auto; }
.neg { color: var(--bad-ink); }
.swatch { display: inline-block; width: 10px; height: 10px; border-radius: 50%; background: var(--c); margin-right: 6px; vertical-align: -1px; }
.spark { display: block; }

/* ---------- input grid ---------- */
.entry-bar { display: flex; gap: 12px; align-items: flex-end; flex-wrap: wrap; margin-bottom: 12px; }
table.entry input { width: 100%; min-width: 92px; text-align: right; font-variant-numeric: tabular-nums; padding: 5px 7px; }
table.entry input.note { text-align: left; min-width: 140px; }
table.entry td { padding: 4px 6px; vertical-align: middle; }
table.entry tbody th { white-space: nowrap; min-width: 150px; font-weight: 600; }
table.entry .row-status { display: block; font-weight: 400; margin-top: 2px; }
table.entry input::placeholder { color: var(--axis-muted); opacity: 0.55; font-style: italic; }
table.entry tr.changed { background: var(--changed-bg); }
table.entry tr.invalid { background: var(--bad-bg); }
table.entry input.bad { border-color: var(--bad-ink); }
table.entry .calc { color: var(--text-secondary); font-size: 12px; }
.row-status { font-size: 11px; color: var(--text-muted); white-space: nowrap; }

/* ---------- misc ---------- */
.badge { display: inline-block; font-size: 11px; padding: 1px 7px; border-radius: 999px; background: var(--surface-2); color: var(--text-secondary); border: 1px solid var(--border); font-weight: 500; }
.badge.warn { background: var(--warn-bg); color: var(--warn-ink); border-color: transparent; }
.badge.bad { background: var(--bad-bg); color: var(--bad-ink); border-color: transparent; }
.badge.good { background: var(--good-bg); color: var(--good-ink); border-color: transparent; }
.notice { padding: 10px 12px; border-radius: 8px; background: var(--surface-2); border: 1px solid var(--border); font-size: 13px; margin: 0 0 12px; }
.notice.warn { background: var(--warn-bg); border-color: transparent; }
.notice.bad { background: var(--bad-bg); border-color: transparent; }
.notice.good { background: var(--good-bg); border-color: transparent; }
.stack { display: flex; flex-direction: column; gap: 16px; }
.inline { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 10px; align-items: end; }
ul.issues { list-style: none; margin: 0; padding: 0; }
ul.issues li { padding: 8px 0; border-bottom: 1px solid var(--border); display: flex; gap: 10px; align-items: baseline; }
ul.issues li .at { font-family: ui-monospace, Consolas, monospace; font-size: 12px; color: var(--text-muted); white-space: nowrap; }
.toast {
  position: fixed; right: 20px; bottom: 20px; max-width: 420px; padding: 10px 14px; border-radius: 8px; z-index: 50;
  background: var(--text-primary); color: var(--surface-1); box-shadow: 0 6px 24px rgba(0,0,0,.2); font-size: 13px;
}
.toast.bad { background: var(--bad-ink); color: #fff; }
.chart-tip-ink { color: var(--text-primary); }

@media (max-width: 700px) {
  .app-header, main { padding-left: 16px; padding-right: 16px; }
  .filters { position: static; }
  .tile .value { font-size: 20px; }
  .chart-box { height: 280px; }
}
@media print {
  .app-header .tabs, .header-actions, .filters, .card-head .tools { display: none !important; }
  .card, .tile { break-inside: avoid; }
}

/* ---------- dashboard v2: answer first, details on demand ---------- */
#view-dashboard > .section { margin-bottom: 16px; }
.kpi-foot { margin: -6px 0 16px; font-size: 12px; color: var(--text-muted); }
.tile .value-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.filters .nowrap { flex-wrap: nowrap; }
.filters .group.outlets { flex: 1 1 360px; }

/* status pill — always a word + a dot, never colour alone */
.pill {
  display: inline-flex; align-items: center; gap: 5px; padding: 1px 8px 1px 6px; border-radius: 999px;
  font-size: 12px; font-weight: 600; white-space: nowrap; line-height: 1.6;
}
.pill::before { content: ''; width: 8px; height: 8px; border-radius: 50%; background: var(--pc); flex: none; }
.pill.ok    { --pc: #0ca30c; background: var(--good-bg); color: var(--good-ink); }
.pill.watch { --pc: #fab219; background: var(--warn-bg); color: var(--warn-ink); }
.pill.high  { --pc: #ec835a; background: var(--bad-bg); color: var(--bad-ink); }
.pill.loss  { --pc: #d03b3b; background: var(--bad-bg); color: var(--bad-ink); }
.pill.info  { --pc: var(--axis-muted); background: var(--surface-2); color: var(--text-secondary); }

ul.attention { list-style: none; margin: 0; padding: 0; }
ul.attention li { display: flex; gap: 10px; align-items: baseline; flex-wrap: wrap; padding: 7px 0; border-bottom: 1px solid var(--border); }
ul.attention li:last-child { border-bottom: 0; }
ul.attention .pill { min-width: 84px; }

table.score th.sortable button { all: unset; cursor: pointer; }
table.score th.sortable button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
table.score th .arrow { font-size: 10px; margin-left: 3px; }
table.score .pill { font-variant-numeric: tabular-nums; }

.multi-title { margin: 18px 0 8px; font-size: 13px; }
.multi { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 12px; }
.mini { border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px 4px; min-width: 0; }
.mini-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; font-size: 13px; flex-wrap: wrap; }
.mini-head .since { font-size: 11px; color: var(--text-muted); }
.mini-val { font-size: 12px; color: var(--text-secondary); font-variant-numeric: tabular-nums; }
.mini-chart { position: relative; height: 120px; }

table.heat { border-collapse: separate; border-spacing: 2px; font-size: 12px; }
table.heat th { font-weight: 600; color: var(--text-secondary); padding: 4px 6px; white-space: nowrap; text-align: center; font-size: 11px; }
table.heat th.row { text-align: left; position: sticky; left: 0; background: var(--surface-1); z-index: 1; font-size: 12px; color: var(--text-primary); }
table.heat td { min-width: 44px; padding: 6px 4px; text-align: center; border-radius: 4px; font-variant-numeric: tabular-nums; }
table.heat td.high { box-shadow: inset 0 0 0 2px var(--warn-ink); }
table.heat td.loss { box-shadow: inset 0 0 0 2px var(--bad-ink); font-weight: 700; }
table.heat td.none { color: var(--text-muted); }
table.heat tr.total th, table.heat tr.total td { font-weight: 700; }

details.more { margin-top: 16px; }
details.more > summary {
  cursor: pointer; padding: 12px 14px; background: var(--surface-1); border: 1px solid var(--border);
  border-radius: var(--radius); font-weight: 600;
}
details.more[open] > summary { margin-bottom: 16px; }

@media (max-width: 700px) {
  .app-header { gap: 8px; }
  .brand { flex: 1; }
  .tabs { order: 3; flex: 1 0 100%; flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; }
  .tab { white-space: nowrap; }
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  .tile { padding: 10px 12px; }
  .tile .label { flex-direction: column; gap: 0; }
  .filters { gap: 10px; }
  .filters .group { min-width: 0; max-width: 100%; }
  .filters .group.outlets { flex-basis: 100%; }
  .seg button { padding: 5px 8px; }
  .multi { grid-template-columns: minmax(0, 1fr); }
  .card { padding: 12px; }
}
