/* ── Tokens ──────────────────────────────────────────────────────────────
   Light values are the default; the dark set is declared twice — once under
   prefers-color-scheme for the OS setting, once under [data-theme] so the
   in-page toggle wins in both directions. */
:root {
  color-scheme: light;
  --plane: #f9f9f7;
  --surface: #fcfcfb;
  --surface-sunk: #f2f1ed;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --empty: #e7e6e0;

  /* AQI semantic-heat ramp — monotone lightness, CVD-validated per surface */
  --aqi-good: #63c776;
  --aqi-moderate: #ac9c00;
  --aqi-usg: #c36200;
  --aqi-unhealthy: #be222d;
  --aqi-very: #792291;
  --aqi-hazardous: #760025;

  --radius: 10px;
  --shadow: 0 1px 2px rgba(11, 11, 11, .05), 0 8px 24px -16px rgba(11, 11, 11, .18);
  --cell: 13px;
  --cell-gap: 3px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --plane: #0d0d0d;
    --surface: #1a1a19;
    --surface-sunk: #232322;
    --text-primary: #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted: #898781;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.10);
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --empty: #2c2c2a;
    --aqi-good: #73e087;
    --aqi-moderate: #c2b000;
    --aqi-usg: #dd7000;
    --aqi-unhealthy: #d53c40;
    --aqi-very: #8f39a7;
    --aqi-hazardous: #9a0b35;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --plane: #0d0d0d;
  --surface: #1a1a19;
  --surface-sunk: #232322;
  --text-primary: #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted: #898781;
  --grid: #2c2c2a;
  --axis: #383835;
  --border: rgba(255, 255, 255, 0.10);
  --series-1: #3987e5;
  --series-2: #d95926;
  --series-3: #199e70;
  --empty: #2c2c2a;
  --aqi-good: #73e087;
  --aqi-moderate: #c2b000;
  --aqi-usg: #dd7000;
  --aqi-unhealthy: #d53c40;
  --aqi-very: #8f39a7;
  --aqi-hazardous: #9a0b35;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--plane);
  color: var(--text-primary);
  font: 15px/1.55 system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
}

.wrap { max-width: 1180px; margin: 0 auto; padding: 0 24px 80px; }

/* ── Masthead ─────────────────────────────────────────────────────────── */
.masthead { padding: 44px 0 24px; }
.eyebrow {
  font-size: 12px; letter-spacing: .09em; text-transform: uppercase;
  color: var(--text-muted); margin: 0 0 10px;
}
h1 { font-size: clamp(28px, 4.2vw, 42px); line-height: 1.1; margin: 0 0 12px; letter-spacing: -.02em; }
.dek { margin: 0; max-width: 62ch; color: var(--text-secondary); font-size: 16px; }

/* ── Filter row — one row above everything it scopes ──────────────────── */
.filters {
  position: sticky; top: 0; z-index: 30;
  display: flex; flex-wrap: wrap; gap: 10px; align-items: center;
  padding: 12px 0; margin-bottom: 22px;
  background: color-mix(in srgb, var(--plane) 88%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.filter-label { font-size: 12px; color: var(--text-muted); margin-right: 2px; }
/* A labelled set of chips wraps as one unit, so the label never ends up
   stranded on the line above the controls it names. */
.filter-group { display: inline-flex; align-items: center; gap: 10px; }
.chip {
  appearance: none; cursor: pointer;
  font: inherit; font-size: 13px; line-height: 1;
  padding: 8px 13px; min-height: 32px;
  border-radius: 999px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text-secondary);
  transition: background .12s, color .12s, border-color .12s;
}
.chip:hover { border-color: var(--axis); color: var(--text-primary); }
.chip[aria-pressed="true"] {
  background: var(--text-primary); color: var(--plane); border-color: var(--text-primary);
}
.chip:focus-visible { outline: 2px solid var(--series-1); outline-offset: 2px; }
.spacer { flex: 1 1 auto; }

/* ── Cards ────────────────────────────────────────────────────────────── */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 22px 24px;
  box-shadow: var(--shadow);
}
section { margin-bottom: 34px; }
.section-head { margin: 0 0 4px; font-size: 20px; letter-spacing: -.01em; }
.section-note { margin: 0 0 16px; color: var(--text-secondary); font-size: 14px; max-width: 68ch; }
.card-title { margin: 0 0 2px; font-size: 15px; }
.card-note { margin: 0 0 18px; font-size: 13px; color: var(--text-muted); }

/* ── Stat tiles — the number is the chart ─────────────────────────────── */
.tiles { display: grid; gap: 14px; grid-template-columns: repeat(auto-fit, minmax(178px, 1fr)); }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px 20px; }
.tile-label { font-size: 12px; color: var(--text-muted); text-transform: uppercase; letter-spacing: .07em; margin-bottom: 8px; }
.tile-value { font-size: 34px; line-height: 1.05; letter-spacing: -.02em; }
.tile-value .unit { font-size: 15px; color: var(--text-secondary); letter-spacing: 0; }
.tile-sub { margin-top: 7px; font-size: 13px; color: var(--text-secondary); }
.swatch-dot { display: inline-block; width: 9px; height: 9px; border-radius: 2px; margin-right: 6px; vertical-align: baseline; }

/* ── Calendar ─────────────────────────────────────────────────────────── */
.year-block { margin-bottom: 26px; }
.year-head { display: flex; align-items: baseline; gap: 14px; flex-wrap: wrap; margin-bottom: 10px; }
.year-num { font-size: 22px; letter-spacing: -.01em; }
.year-meta { font-size: 13px; color: var(--text-secondary); }
.year-meta b { font-weight: 600; color: var(--text-primary); }

/* The year's band mix. Deliberately narrow and captioned: a full-width bar
   sitting on top of a time grid reads as that grid's axis, which it is not. */
.year-mix { display: flex; align-items: center; gap: 10px; margin: 0 0 14px 36px; max-width: 430px; }
.year-mix-label { font-size: 11px; color: var(--text-muted); white-space: nowrap; }
.year-mix .stack { flex: 1 1 auto; height: 11px; }

.cal-scroll { overflow-x: auto; padding-bottom: 6px; }
.cal { display: inline-grid; grid-template-columns: 30px auto; gap: 6px; }
.cal-months {
  display: grid; grid-auto-rows: 15px;
  grid-column: 2; font-size: 11px; color: var(--text-muted);
}
.cal-month { grid-row: 1; white-space: nowrap; }
.cal-dows {
  display: grid; grid-template-rows: repeat(7, var(--cell)); gap: var(--cell-gap);
  grid-column: 1; grid-row: 2; font-size: 10px; color: var(--text-muted);
}
.cal-dows span { line-height: var(--cell); }
.cal-grid {
  grid-column: 2; grid-row: 2;
  display: grid; grid-auto-flow: column;
  grid-template-rows: repeat(7, var(--cell));
  gap: var(--cell-gap);
}
.cell {
  width: var(--cell); height: var(--cell);
  border-radius: 3px; background: var(--empty);
  padding: 0; border: 0; cursor: pointer;
}
.cell.blank { background: transparent; cursor: default; }
.cell.missing { background: var(--empty); cursor: default; }
.cell:focus-visible { outline: 2px solid var(--text-primary); outline-offset: 2px; }
.cell.is-active { box-shadow: 0 0 0 2px var(--surface), 0 0 0 4px var(--text-primary); }

/* Wider hit area than the 13px mark, without disturbing the grid. */
.cell-hit { position: relative; }
.cell-hit::after { content: ""; position: absolute; inset: -6px; }

/*
 * Spike marker — a notch clipped out of the cell's top-right corner.
 *
 * Encoded as shape, not color: the notch is painted in the surface behind the
 * grid, so it reads the same whichever two bands a day sits between, stays
 * legible at 13px, and never adds a color the ramp legend does not explain.
 */
.notch { position: relative; }
.notch::before {
  content: ""; position: absolute; z-index: 1;
  top: 0; right: 0; width: 6px; height: 6px;
  background: var(--surface);
  clip-path: polygon(100% 0, 0 0, 100% 100%);
}

/* ── Legend (mandatory for the heat ramp) ─────────────────────────────── */
.legend { display: flex; flex-wrap: wrap; gap: 6px 16px; align-items: center; margin-top: 14px; font-size: 12px; color: var(--text-secondary); }
.legend-item { display: flex; align-items: center; gap: 7px; }
.legend-sw { width: 13px; height: 13px; border-radius: 3px; }

/* ── Charts (plain HTML/CSS marks) ────────────────────────────────────── */
.grid-2 { display: grid; gap: 18px; grid-template-columns: repeat(auto-fit, minmax(330px, 1fr)); }
.span-all { grid-column: 1 / -1; }

.stack { display: flex; height: 15px; border-radius: 3px; overflow: hidden; gap: 2px; background: transparent; }
.stack > span { display: block; height: 100%; border-radius: 2px; }

.bars { display: grid; gap: 9px; }
.bar-row { display: grid; grid-template-columns: 52px 1fr 62px; gap: 12px; align-items: center; font-size: 13px; }
.bar-row.wide { grid-template-columns: 42px 1fr 96px; }
.bar-row .k { color: var(--text-secondary); }
.bar-row .v { text-align: right; color: var(--text-secondary); font-variant-numeric: tabular-nums; }
.bar-track { background: var(--surface-sunk); border-radius: 3px; height: 13px; }
.bar-fill { height: 13px; border-radius: 3px; background: var(--series-1); }

.hgram { display: flex; align-items: flex-end; gap: 2px; height: 150px; }
.hgram-col { flex: 1 1 0; display: flex; flex-direction: column; justify-content: flex-end; min-width: 0; }
.hgram-col i { display: block; border-radius: 3px 3px 0 0; background: var(--series-1); }
.hgram-axis { display: flex; gap: 2px; margin-top: 7px; font-size: 10px; color: var(--text-muted); }
.hgram-axis span { flex: 1 1 0; text-align: center; min-width: 0; }

/* Seasonality grid: month × year, colored on the same AQI ramp */
.season { overflow-x: auto; }
.season-grid { display: grid; gap: 3px; font-size: 11px; min-width: 520px; }
.season-grid .hd { color: var(--text-muted); text-align: center; padding-bottom: 2px; }
.season-grid .rowlab { color: var(--text-secondary); padding-right: 8px; font-variant-numeric: tabular-nums; }
.season-cell { height: 26px; border-radius: 3px; background: var(--empty); display: flex; align-items: center; justify-content: center; font-size: 11px; color: #fff; font-variant-numeric: tabular-nums; }
.season-cell.empty { color: var(--text-muted); }

/* ── Insights ─────────────────────────────────────────────────────────── */
.insights { display: grid; gap: 14px; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); }
.insight { background: var(--surface); border: 1px solid var(--border); border-left: 3px solid var(--series-1); border-radius: var(--radius); padding: 17px 19px; }
.insight h3 { margin: 0 0 7px; font-size: 15px; letter-spacing: -.005em; }
.insight p { margin: 0; font-size: 14px; color: var(--text-secondary); }

/* ── Tables (the WCAG-clean twin of every chart) ──────────────────────── */
.table-wrap { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; font-size: 13px; }
th, td { text-align: left; padding: 8px 12px; border-bottom: 1px solid var(--grid); white-space: nowrap; }
th { color: var(--text-muted); font-weight: 500; font-size: 12px; text-transform: uppercase; letter-spacing: .06em; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
tbody tr:hover { background: var(--surface-sunk); }
[hidden] { display: none !important; }

/* ── Tooltip ──────────────────────────────────────────────────────────── */
#tooltip {
  position: fixed; z-index: 60; pointer-events: none;
  max-width: 270px; padding: 11px 13px;
  background: var(--surface); color: var(--text-primary);
  border: 1px solid var(--border); border-radius: 8px;
  box-shadow: 0 4px 22px rgba(0, 0, 0, .22);
  font-size: 13px; opacity: 0; transition: opacity .1s;
}
#tooltip.on { opacity: 1; }
.tt-date { font-weight: 600; margin-bottom: 3px; }
.tt-aqi { display: flex; align-items: center; gap: 7px; margin-bottom: 8px; }
.tt-aqi b { font-size: 19px; }
.tt-cat { font-size: 12px; color: var(--text-secondary); }
.tt-list { display: grid; grid-template-columns: auto auto; gap: 2px 12px; font-size: 12px; color: var(--text-secondary); }
.tt-list .r { text-align: right; font-variant-numeric: tabular-nums; }
.tt-lead { margin-top: 7px; font-size: 11px; color: var(--text-muted); }

/* The day's worst moment, sat directly under its 24-hour mean for comparison.
   The negative top margin closes the gap .tt-aqi leaves for the pollutant
   list, without changing spacing on days that never spiked. */
.tt-peak { display: flex; align-items: center; gap: 7px; margin: -4px 0 8px; }
.tt-peak b { font-size: 15px; }
.tt-when { margin-left: auto; font-size: 11px; color: var(--text-muted); white-space: nowrap; }
.muted { color: var(--text-muted); }

/* ── Status / footer ──────────────────────────────────────────────────── */
.status { padding: 40px 0; color: var(--text-secondary); font-size: 14px; }
.status .bar { height: 3px; background: var(--surface-sunk); border-radius: 3px; overflow: hidden; margin-top: 12px; max-width: 320px; }
.status .bar i { display: block; height: 100%; background: var(--series-1); width: 0; transition: width .25s; }
.notice { border-left: 3px solid var(--series-2); background: var(--surface); border-radius: var(--radius); padding: 14px 17px; font-size: 13px; color: var(--text-secondary); margin-bottom: 22px; }
footer { border-top: 1px solid var(--border); padding-top: 22px; color: var(--text-muted); font-size: 13px; }
footer a { color: var(--text-secondary); }
.loading-dim { opacity: .45; transition: opacity .2s; }

@media (max-width: 640px) {
  .wrap { padding: 0 16px 60px; }
  .masthead { padding: 28px 0 18px; }
}
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
