:root {
  --paper: #F6F7FA;
  --surface: #FFFFFF;
  --surface-2: #EDF0F5;
  --ink: #16223E;
  --ink-muted: #5C6B85;
  --line: #DEE3EC;
  --accent: #C41E3A;
  --accent-ink: #FFFFFF;
  --good: #1E8A5F;
  --good-bg: #E3F3EC;
  --warn: #B7791E;
  --warn-bg: #FBF0DC;
  --bad: #C41E3A;
  --bad-bg: #F8E4E8;
  --series-wm: #2E6F8E;
  --series-mm: #E0923C;
  --series-total: #16223E;
  --season-winter: #3B6FA0;
  --season-spring: #4C9A63;
  --season-summer: #D9A02C;
  --season-fall: #A8412E;
  --shadow: 0 1px 2px rgba(22, 34, 62, 0.06), 0 1px 1px rgba(22, 34, 62, 0.04);
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Code", "Roboto Mono", Consolas, monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --paper: #0C1220;
    --surface: #151D30;
    --surface-2: #1C2540;
    --ink: #E8ECF6;
    --ink-muted: #94A3C2;
    --line: #2A3454;
    --accent: #FF5470;
    --accent-ink: #1A0810;
    --good: #3FBE8B;
    --good-bg: #163329;
    --warn: #E0A83C;
    --warn-bg: #332711;
    --bad: #FF6B7F;
    --bad-bg: #33141B;
    --series-wm: #5FA8CF;
    --series-mm: #F0A75C;
    --series-total: #E8ECF6;
    --season-winter: #6FA8D9;
    --season-spring: #6FC98A;
    --season-summer: #E8C05C;
    --season-fall: #D9705A;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 1px 1px rgba(0, 0, 0, 0.2);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --paper: #0C1220;
  --surface: #151D30;
  --surface-2: #1C2540;
  --ink: #E8ECF6;
  --ink-muted: #94A3C2;
  --line: #2A3454;
  --accent: #FF5470;
  --accent-ink: #1A0810;
  --good: #3FBE8B;
  --good-bg: #163329;
  --warn: #E0A83C;
  --warn-bg: #332711;
  --bad: #FF6B7F;
  --bad-bg: #33141B;
  --series-wm: #5FA8CF;
  --series-mm: #F0A75C;
  --series-total: #E8ECF6;
  --season-winter: #6FA8D9;
  --season-spring: #6FC98A;
  --season-summer: #E8C05C;
  --season-fall: #D9705A;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 1px 1px rgba(0, 0, 0, 0.2);
  color-scheme: dark;
}

:root[data-theme="light"] {
  --paper: #F6F7FA;
  --surface: #FFFFFF;
  --surface-2: #EDF0F5;
  --ink: #16223E;
  --ink-muted: #5C6B85;
  --line: #DEE3EC;
  --accent: #C41E3A;
  --accent-ink: #FFFFFF;
  --good: #1E8A5F;
  --good-bg: #E3F3EC;
  --warn: #B7791E;
  --warn-bg: #FBF0DC;
  --bad: #C41E3A;
  --bad-bg: #F8E4E8;
  --series-wm: #2E6F8E;
  --series-mm: #E0923C;
  --series-total: #16223E;
  --season-winter: #3B6FA0;
  --season-spring: #4C9A63;
  --season-summer: #D9A02C;
  --season-fall: #A8412E;
  --shadow: 0 1px 2px rgba(22, 34, 62, 0.06), 0 1px 1px rgba(22, 34, 62, 0.04);
  color-scheme: light;
}

* { box-sizing: border-box; }

html, body { margin: 0; padding: 0; }

body {
  background: var(--paper);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); }
button:focus-visible, input:focus-visible, th[role="button"]:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.num { font-variant-numeric: tabular-nums; font-family: var(--mono); }

/* ---------- Header ---------- */

header.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 10;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 700;
  font-size: 15px;
}

.brand .mark {
  width: 26px;
  height: 26px;
  border-radius: 6px;
  background: var(--accent);
  color: var(--accent-ink);
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--mono);
  font-size: 12px;
  font-weight: 700;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 12px;
}

.freshness {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 1px;
  font-family: var(--mono);
  color: var(--ink-muted);
  line-height: 1.3;
}

#freshness-date { font-size: 12px; }
#freshness-time { font-size: 10.5px; opacity: 0.75; }

#theme-toggle {
  font-family: var(--sans);
  font-size: 13px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  color: var(--ink);
  border-radius: 6px;
  padding: 6px 10px;
  cursor: pointer;
}
#theme-toggle:hover { border-color: var(--ink-muted); }

/* ---------- Page ---------- */

main {
  max-width: 1180px;
  margin: 0 auto;
  padding: 28px 24px 80px;
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.page-title {
  font-size: 24px;
  font-weight: 700;
  margin: 4px 0 0;
  text-wrap: balance;
}

.page-sub {
  color: var(--ink-muted);
  font-size: 14px;
  margin: 0;
}

/* ---------- KPI strip ---------- */

.kpi-strip {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1px;
  background: var(--line);
  border: 1px solid var(--line);
  border-radius: 10px;
  overflow: hidden;
  box-shadow: var(--shadow);
}

.kpi {
  background: var(--surface);
  padding: 16px 18px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 0;
}

.kpi-label {
  font-family: var(--mono);
  font-size: 10.5px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--ink-muted);
}

.kpi-value {
  font-family: var(--mono);
  font-size: 26px;
  font-weight: 700;
  line-height: 1.1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.kpi-detail {
  font-size: 12.5px;
  color: var(--ink-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.kpi-detail.up { color: var(--good); }
.kpi-detail.down { color: var(--bad); }

/* ---------- Cards ---------- */

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: var(--shadow);
  padding: 18px 20px 20px;
}

.card-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 14px;
}

.card-head h2 {
  font-size: 15px;
  margin: 0 0 3px;
}

.card-head p {
  margin: 0;
  font-size: 12.5px;
  color: var(--ink-muted);
}

.card-body { overflow-x: auto; }

/* ---------- Chart controls (view/metric toggles, detail checkbox) ---------- */

.chart-controls {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.detail-toggle {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12.5px;
  color: var(--ink-muted);
  cursor: pointer;
  user-select: none;
}

.detail-toggle input[type="checkbox"] {
  accent-color: var(--accent);
  width: 14px;
  height: 14px;
  cursor: pointer;
}

/* ---------- Segmented control ---------- */

.segmented {
  display: inline-flex;
  border: 1px solid var(--line);
  border-radius: 7px;
  overflow: hidden;
  flex-shrink: 0;
}

.segmented button {
  font-family: var(--sans);
  font-size: 12.5px;
  padding: 6px 11px;
  border: none;
  background: var(--surface);
  color: var(--ink-muted);
  cursor: pointer;
  border-right: 1px solid var(--line);
}
.segmented button:last-child { border-right: none; }
.segmented button:hover { background: var(--surface-2); }
.segmented button.active {
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
}

/* ---------- Heatmap ---------- */

table.heatmap {
  border-collapse: collapse;
  width: 100%;
  min-width: 480px;
}

table.heatmap th, table.heatmap td {
  padding: 5px 8px;
  text-align: center;
  font-size: 12px;
}

table.heatmap thead th {
  font-family: var(--mono);
  font-size: 10.5px;
  letter-spacing: 0.05em;
  color: var(--ink-muted);
  text-transform: uppercase;
  font-weight: 600;
}

table.heatmap tbody th {
  font-family: var(--mono);
  text-align: right;
  padding-right: 12px;
  color: var(--ink);
  font-weight: 600;
  white-space: nowrap;
}

table.heatmap td {
  font-family: var(--mono);
  border-radius: 5px;
  transition: outline-color 0.1s ease;
}

table.heatmap td.cell {
  cursor: default;
}

table.heatmap td.cell:hover {
  outline: 1.5px solid var(--accent);
  outline-offset: -1.5px;
}

table.heatmap td.empty {
  color: var(--ink-muted);
  opacity: 0.4;
}

table.heatmap td .cell-main { display: block; }
table.heatmap td .cell-detail {
  display: block;
  font-size: 9.5px;
  opacity: 0.75;
  margin-top: 1px;
}

/* ---------- Column chart (bar-per-season, alternative to the heatmap) ---------- */

.colchart-scroll {
  overflow-x: auto;
}

.colchart {
  display: flex;
  align-items: flex-end;
  gap: 18px;
  min-width: 640px;
  height: 220px;
  padding: 0 4px;
  border-bottom: 1px solid var(--line);
}

.colchart .year-group {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  flex: 1 0 auto;
  min-width: 44px;
}

.colchart .bars {
  display: flex;
  align-items: flex-end;
  gap: 3px;
  height: 180px;
}

.colchart .bar {
  width: 10px;
  border-radius: 2px 2px 0 0;
  position: relative;
  min-height: 1px;
}

.colchart .bar-stack {
  display: flex;
  flex-direction: column-reverse;
  width: 10px;
  border-radius: 2px 2px 0 0;
  overflow: hidden;
}

.colchart .bar-seg { width: 100%; }

.colchart .year-label {
  font-family: var(--mono);
  font-size: 10.5px;
  color: var(--ink-muted);
  white-space: nowrap;
}

.heatmap-legend {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 12px;
  font-size: 11.5px;
  color: var(--ink-muted);
}

.heatmap-legend .swatch-scale {
  display: flex;
  gap: 2px;
}

.heatmap-legend .swatch-scale span {
  width: 14px;
  height: 10px;
  border-radius: 2px;
}

/* ---------- Bar charts (category / demographics) ---------- */

.barchart { display: flex; flex-direction: column; gap: 10px; }

.barchart .row {
  display: grid;
  grid-template-columns: 130px 1fr 64px;
  align-items: center;
  gap: 12px;
  font-size: 12.5px;
}

.barchart .row .label { color: var(--ink); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.barchart .track {
  position: relative;
  height: 20px;
  background: var(--surface-2);
  border-radius: 4px;
  overflow: hidden;
  display: flex;
}

.barchart .segment {
  height: 100%;
}

.barchart .value {
  font-family: var(--mono);
  text-align: right;
  color: var(--ink-muted);
}

/* ---------- Fill-rate strip ---------- */

.fillrate { display: flex; flex-direction: column; gap: 12px; }

.fillrate .row {
  display: grid;
  grid-template-columns: 1fr 120px 56px;
  align-items: center;
  gap: 12px;
  font-size: 12.5px;
}

.fillrate .ev-name { color: var(--ink); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.fillrate .ev-year { color: var(--ink-muted); font-family: var(--mono); font-size: 11px; margin-left: 6px; }

.fillrate .track {
  position: relative;
  height: 8px;
  background: var(--surface-2);
  border-radius: 4px;
}

.fillrate .fill {
  position: absolute;
  top: 0; left: 0; bottom: 0;
  border-radius: 4px;
}

.fillrate .pct {
  font-family: var(--mono);
  text-align: right;
  color: var(--ink-muted);
}
.fillrate .pct.hot { color: var(--bad); font-weight: 700; }
.fillrate .pct.warm { color: var(--warn); font-weight: 700; }

/* ---------- Legend chips ---------- */

.legend {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
  font-size: 12px;
  color: var(--ink-muted);
}
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { width: 10px; height: 10px; border-radius: 2px; display: inline-block; }

/* ---------- Table ---------- */

.table-controls {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 12px;
}

.table-controls input[type="search"] {
  font-family: var(--sans);
  font-size: 13px;
  padding: 7px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
  color: var(--ink);
  min-width: 220px;
}

.table-controls select {
  font-family: var(--sans);
  font-size: 13px;
  padding: 7px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
  color: var(--ink);
}

.table-scroll {
  max-height: 460px;
  overflow-y: auto;
  overflow-x: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
}

table.events {
  border-collapse: collapse;
  width: 100%;
  min-width: 780px;
  font-size: 12.5px;
}

table.events thead th {
  position: sticky;
  top: 0;
  background: var(--surface-2);
  text-align: left;
  padding: 8px 12px;
  font-family: var(--mono);
  font-size: 10.5px;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--ink-muted);
  cursor: pointer;
  white-space: nowrap;
  user-select: none;
  border-bottom: 1px solid var(--line);
}
table.events thead th:hover { color: var(--ink); }
table.events thead th.sorted { color: var(--accent); }

table.events td {
  padding: 7px 12px;
  border-bottom: 1px solid var(--line);
  white-space: nowrap;
}
table.events td.num { text-align: right; }
table.events tbody tr:last-child td { border-bottom: none; }
table.events tbody tr:hover { background: var(--surface-2); }

.category-pill {
  display: inline-block;
  font-family: var(--sans);
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 20px;
  background: var(--surface-2);
  color: var(--ink-muted);
}

.results-count {
  font-size: 12px;
  color: var(--ink-muted);
  margin-top: 8px;
}

/* ---------- Footer ---------- */

footer {
  max-width: 1180px;
  margin: 0 auto;
  padding: 0 24px 40px;
  font-size: 12px;
  color: var(--ink-muted);
  font-family: var(--mono);
}

/* ---------- Loading / empty ---------- */

.loading, .error {
  padding: 40px 20px;
  text-align: center;
  color: var(--ink-muted);
  font-size: 13px;
}
.error { color: var(--bad); }

.two-col {
  display: grid;
  grid-template-columns: 1.1fr 1fr;
  gap: 24px;
}

@media (max-width: 760px) {
  .kpi-strip { grid-template-columns: 1fr; }
  .barchart .row, .fillrate .row { grid-template-columns: 100px 1fr 50px; }
  .two-col { grid-template-columns: 1fr; }
  header.topbar { padding: 10px 16px; }
  .brand span:last-child { display: none; }
  #freshness-date { font-size: 11px; }
  #freshness-time { font-size: 9.5px; }
  main { padding: 20px 16px 60px; }
}
