/* Migraine Tracker — light/dark tokens follow the fleet data-viz palette.
   Every colour is defined on bare :root first, then re-declared under both the
   OS media query and the [data-theme] scope, so neither can leave a role
   undefined. */

:root {
  color-scheme: light;

  --surface-1:      #fcfcfb;   /* cards, chart surface */
  --plane:          #f9f9f7;   /* page behind the cards */
  --text-primary:   #0b0b0b;
  --text-secondary: #52514e;
  --text-muted:     #898781;
  --grid:           #e1e0d9;
  --axis:           #c3c2b7;
  --border:         rgba(11, 11, 11, 0.10);

  --series-1:       #2a78d6;   /* pressure line */
  --diverge-fall:   #d03b3b;   /* pressure falling */
  --diverge-rise:   #2a78d6;   /* pressure rising */
  --diverge-mid:    #f0efec;

  --good:           #0ca30c;
  --warning:        #fab219;
  --serious:        #ec835a;
  --critical:       #d03b3b;

  --band-opacity: 0.14;   /* risk shading over the chart surface */

  --radius: 12px;
  --pad: 16px;
  --font: system-ui, -apple-system, "Segoe UI", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --surface-1:      #1a1a19;
    --plane:          #0d0d0d;
    --text-primary:   #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted:     #898781;
    --grid:           #2c2c2a;
    --axis:           #383835;
    --border:         rgba(255, 255, 255, 0.10);
    --series-1:       #3987e5;
    --diverge-fall:   #d03b3b;
    --diverge-rise:   #3987e5;
    --diverge-mid:    #383835;
    /* the same alpha reads as murky olive over near-black; dark needs more */
    --band-opacity:   0.22;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --surface-1:      #1a1a19;
  --plane:          #0d0d0d;
  --text-primary:   #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted:     #898781;
  --grid:           #2c2c2a;
  --axis:           #383835;
  --border:         rgba(255, 255, 255, 0.10);
  --series-1:       #3987e5;
  --diverge-fall:   #d03b3b;
  --diverge-rise:   #3987e5;
  --diverge-mid:    #383835;
  --band-opacity:   0.22;
}

* { box-sizing: border-box; }

/* The gate, the four views and several panels are toggled with the `hidden`
   attribute, but they also carry class rules that set `display`. A class
   selector outranks the UA sheet's `[hidden] { display: none }`, so without
   this every view renders stacked on the page at once. */
[hidden] { display: none !important; }

body {
  margin: 0;
  font-family: var(--font);
  background: var(--plane);
  color: var(--text-primary);
  font-size: 15px;
  line-height: 1.45;
  -webkit-text-size-adjust: 100%;
  padding-bottom: env(safe-area-inset-bottom);
}

h1, h2 { margin: 0; font-weight: 600; }
.muted { color: var(--text-muted); }
.hint { font-size: 13px; margin: 6px 0 12px; }
.error { color: var(--critical); font-size: 14px; margin: 4px 0 12px; }

/* ── sign-in gate ─────────────────────────────────────────────────────── */
.gate {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px 16px;
}
.gate-card { width: 100%; max-width: 360px; }
.gate-title { font-size: 22px; }
.gate-sub { font-size: 14px; margin: 4px 0 20px; }

/* ── chrome ───────────────────────────────────────────────────────────── */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: calc(10px + env(safe-area-inset-top)) var(--pad) 10px;
  background: var(--surface-1);
  border-bottom: 1px solid var(--border);
}
.topbar-left { display: flex; align-items: center; gap: 10px; min-width: 0; }
.topbar-right { display: flex; gap: 4px; flex: none; }
.brand { font-weight: 600; white-space: nowrap; }
@media (max-width: 520px) { .brand { display: none; } }

.loc-select {
  font: inherit;
  color: inherit;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 8px;
  max-width: 62vw;
}

.tabs {
  position: sticky;
  top: 52px;
  z-index: 19;
  display: flex;
  gap: 2px;
  padding: 6px var(--pad);
  background: var(--surface-1);
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
}
.tab {
  flex: 1;
  min-width: 72px;
  font: inherit;
  color: var(--text-secondary);
  background: transparent;
  border: 0;
  border-radius: 8px;
  padding: 8px 10px;
  cursor: pointer;
}
.tab.is-active { background: var(--plane); color: var(--text-primary); font-weight: 600; }

main { padding: var(--pad); max-width: 860px; margin: 0 auto; }
.view { display: grid; gap: 14px; }

.card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: var(--pad);
}
.card-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 10px;
  flex-wrap: wrap;
}
.card-title { font-size: 15px; }
.card-note { font-size: 13px; }

.foot { padding: 8px var(--pad) 28px; font-size: 12px; text-align: center; max-width: 860px; margin: 0 auto; }

/* ── risk hero ────────────────────────────────────────────────────────── */
.risk-head { display: flex; align-items: center; gap: 12px; }
.risk-icon { font-size: 26px; line-height: 1; flex: none; }
.risk-level { margin: 0; font-size: 18px; font-weight: 600; }
.risk-phrase { margin: 2px 0 0; font-size: 14px; }
.risk-score { margin-left: auto; text-align: right; flex: none; }
.hero-figure { font-size: 40px; font-weight: 600; line-height: 1; }
.hero-unit { font-size: 13px; }
.risk-when { margin: 12px 0 0; font-size: 14px; color: var(--text-secondary); }

#risk-card.level-calm     .risk-icon { color: var(--good); }
#risk-card.level-watch    .risk-icon { color: var(--warning); }
#risk-card.level-elevated .risk-icon { color: var(--serious); }
#risk-card.level-high     .risk-icon { color: var(--critical); }

/* ── stat tiles ───────────────────────────────────────────────────────── */
.tiles { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
@media (min-width: 620px) { .tiles { grid-template-columns: repeat(4, 1fr); } }
.tile {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
}
.tile-label { font-size: 12px; color: var(--text-muted); display: block; }
.tile-value { font-size: 22px; font-weight: 600; font-variant-numeric: tabular-nums; }
.tile-unit { font-size: 12px; color: var(--text-muted); margin-left: 2px; }
.tile-fall .tile-value { color: var(--diverge-fall); }
.tile-rise .tile-value { color: var(--diverge-rise); }

/* ── charts ───────────────────────────────────────────────────────────── */
.chart { position: relative; width: 100%; touch-action: pan-y; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }

.key { display: flex; flex-wrap: wrap; gap: 12px; margin-top: 10px; font-size: 12px; color: var(--text-secondary); }
.key-item { display: inline-flex; align-items: center; gap: 6px; }
.key-swatch { width: 12px; height: 12px; border-radius: 3px; flex: none; }
.key-line { width: 16px; height: 0; border-top: 2px solid var(--series-1); flex: none; }
.key-line.is-dashed { border-top-style: dashed; }

.tooltip {
  position: absolute;
  z-index: 5;
  pointer-events: none;
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 12px;
  line-height: 1.5;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.14);
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}
.tooltip strong { font-weight: 600; }
.tooltip .tt-row { display: flex; justify-content: space-between; gap: 14px; }

/* ── data table ───────────────────────────────────────────────────────── */
.table-wrap { overflow-x: auto; margin-top: 12px; }
table { border-collapse: collapse; width: 100%; font-size: 13px; }
th, td { text-align: right; padding: 6px 8px; border-bottom: 1px solid var(--grid); font-variant-numeric: tabular-nums; }
th:first-child, td:first-child { text-align: left; font-variant-numeric: normal; }
th { color: var(--text-muted); font-weight: 500; }

/* ── forms & buttons ──────────────────────────────────────────────────── */
.field { display: block; margin-bottom: 12px; }
.field > span { display: block; font-size: 13px; color: var(--text-secondary); margin-bottom: 4px; }
input, select, textarea {
  font: inherit;
  width: 100%;
  color: var(--text-primary);
  background: var(--plane);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 9px 10px;
}
input[type="range"] { padding: 0; background: transparent; border: 0; }
input:focus-visible, select:focus-visible, textarea:focus-visible, button:focus-visible {
  outline: 2px solid var(--series-1);
  outline-offset: 2px;
}
.check { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; font-size: 14px; }
.check input { width: auto; }
.grid-2 { display: grid; grid-template-columns: repeat(2, 1fr); gap: 0 12px; }
@media (max-width: 460px) { .grid-2 { grid-template-columns: 1fr; } }
.row { display: flex; gap: 8px; align-items: center; }
.row.wrap { flex-wrap: wrap; }
.row input { flex: 1; }

.btn {
  font: inherit;
  cursor: pointer;
  color: var(--text-primary);
  background: var(--plane);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 9px 14px;
  min-height: 40px;
}
.btn:disabled { opacity: 0.5; cursor: default; }
.btn-primary { background: var(--series-1); border-color: var(--series-1); color: #fff; font-weight: 600; }
.btn-ghost { background: transparent; }
.btn-wide { width: 100%; }
.btn-danger { color: var(--critical); }

.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg-btn { font: inherit; border: 0; background: transparent; color: var(--text-secondary); padding: 6px 12px; cursor: pointer; min-height: 34px; }
.seg-btn.is-active { background: var(--plane); color: var(--text-primary); font-weight: 600; }

/* ── lists ────────────────────────────────────────────────────────────── */
.list-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 0;
  border-bottom: 1px solid var(--grid);
}
.list-row:last-child { border-bottom: 0; }
.list-main { flex: 1; min-width: 0; }
.list-title { font-size: 14px; }
.list-sub { font-size: 12px; color: var(--text-muted); }
.pill {
  font-size: 11px;
  padding: 2px 7px;
  border-radius: 999px;
  border: 1px solid var(--border);
  color: var(--text-secondary);
  white-space: nowrap;
}
.pill-primary { border-color: var(--series-1); color: var(--series-1); }
.loc-form { margin-top: 12px; display: grid; gap: 8px; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(20px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 50;
  max-width: min(92vw, 420px);
  background: var(--text-primary);
  color: var(--surface-1);
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 14px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.22);
}
.toast.is-error { background: var(--critical); color: #fff; }

/* Chart-key swatches. These carry no meaning on their own — each sits beside
   its word — but they must match the band colours the chart paints. Declared
   here rather than as inline styles so the strict CSP needs no style-src
   exception. */
.key-swatch.level-watch    { background: var(--warning);  opacity: 0.45; }
.key-swatch.level-elevated { background: var(--serious);  opacity: 0.45; }
.key-swatch.level-high     { background: var(--critical); opacity: 0.45; }
.key-swatch.is-episode {
  background: var(--critical);
  border-radius: 0;
  clip-path: polygon(50% 0, 100% 100%, 0 100%);
}

.tiles.is-stats { margin-bottom: 12px; }
.table-wrap.is-spaced { margin-top: 14px; }
