/* 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; }
/* The person switch (.who-btn, under "person switch" below) is flex: none so
   it never loses a shrink fight with the location dropdown and renders "Ch…". */

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

.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; }

/* ── outlook hero (more rules under "outlook" below) ─────────────────── */
.risk-head { display: flex; align-items: center; gap: 12px; }
.risk-icon { font-size: 26px; line-height: 1; flex: none; }

.outlook-card.level-calm     .risk-icon { color: var(--good); }
.outlook-card.level-watch    .risk-icon { color: var(--warning); }
.outlook-card.level-elevated .risk-icon { color: var(--serious); }
.outlook-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; }

/* ── person picker ────────────────────────────────────────────────────── */
.pick-label { margin: 18px 0 10px; font-weight: 600; }
.pick-people { display: grid; gap: 10px; }
.pick-btn { font-size: 17px; padding: 14px; min-height: 52px; }

/* ── install steps ────────────────────────────────────────────────────── */
.steps { margin: 10px 0 0; padding-left: 20px; font-size: 14px; color: var(--text-secondary); }
.steps li { margin-bottom: 6px; }
.steps:empty { display: none; }

/* ── outlook ──────────────────────────────────────────────────────────── */
.outlook-kicker { margin: 0; font-size: 12px; }
.outlook-title { margin: 1px 0 0; font-size: 19px; line-height: 1.3; font-weight: 600; }
.outlook-why { margin: 12px 0 0; font-size: 14px; color: var(--text-secondary); }
.outlook-record { margin: 14px 0 0; font-size: 13px; color: var(--text-muted); }

.outlook-card { border-left-width: 4px; }
.outlook-card.level-calm     { border-left-color: var(--good); }
.outlook-card.level-watch    { border-left-color: var(--warning); }
.outlook-card.level-elevated { border-left-color: var(--serious); }
.outlook-card.level-high     { border-left-color: var(--critical); }

.facts { margin: 14px 0 0; display: grid; gap: 0; border-top: 1px solid var(--grid); }
.fact {
  display: grid;
  grid-template-columns: minmax(8.5em, 40%) 1fr;
  gap: 12px;
  padding: 8px 0;
  border-bottom: 1px solid var(--grid);
}
.fact dt { font-size: 13px; color: var(--text-muted); }
.fact dd { margin: 0; font-size: 14px; font-weight: 600; }

/* ── day by day ───────────────────────────────────────────────────────── */
.days { border-top: 1px solid var(--grid); }
.day { border-bottom: 1px solid var(--grid); }
.day-sum {
  position: relative;
  display: grid;
  grid-template-columns: 1fr auto;
  column-gap: 10px;
  row-gap: 1px;
  padding: 10px 24px 10px 0;
  list-style: none;
}
summary.day-sum { cursor: pointer; }
summary.day-sum::-webkit-details-marker { display: none; }
summary.day-sum::after {
  content: '›';
  position: absolute;
  right: 4px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 20px;
  line-height: 1;
  color: var(--text-muted);
  transition: transform 0.15s ease;
}
details[open] > summary.day-sum::after { transform: translateY(-50%) rotate(90deg); }

.day-name { font-size: 15px; font-weight: 600; }
.day-date { font-size: 12px; font-weight: 400; color: var(--text-muted); }
.day-stage { grid-column: 1 / -1; font-size: 13px; color: var(--text-secondary); }
.stage-risk .day-stage,
.stage-prodrome .day-stage { color: var(--text-primary); font-weight: 600; }
.day.is-quiet .day-stage { color: var(--text-muted); }

.chance {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
}
.chance-dot { width: 9px; height: 9px; border-radius: 50%; flex: none; background: var(--good); }
.chance.level-watch    .chance-dot { background: var(--warning); }
.chance.level-elevated .chance-dot { background: var(--serious); }
.chance.level-high     .chance-dot { background: var(--critical); }

.todo { margin: 0 0 12px; padding-left: 20px; font-size: 14px; color: var(--text-secondary); }
.todo li { margin-bottom: 6px; }
.todo li::marker { color: var(--text-muted); }

.fine { font-size: 12px; margin: 12px 0 0; }

.section-head { padding: 8px 2px 0; }
.section-title { font-size: 15px; }
.section-note { font-size: 13px; margin: 2px 0 0; }

/* ── person switch ────────────────────────────────────────────────────── */
/* The name in the top bar is the switch. Styled as a chip with the swap mark
   so it reads as tappable, not as a label. */
.who-btn {
  flex: none;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font: inherit;
  font-weight: 600;
  white-space: nowrap;
  color: var(--text-primary);
  background: var(--plane);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 6px 12px;
  min-height: 36px;
  cursor: pointer;
}
.who-btn:disabled { opacity: 0.6; cursor: default; }
.who-swap { color: var(--series-1); font-size: 14px; }

.view-group { display: grid; gap: 14px; }

.seg-people,
.seg-show { display: flex; width: 100%; }
.seg-people { margin-top: 10px; }
.seg-people .seg-btn,
.seg-show .seg-btn { flex: 1; min-height: 44px; font-size: 15px; }
.seg-show { background: var(--surface-1); }

/* ── more than one condition ──────────────────────────────────────────── */
/* One line per condition under the day name: "Spine · Flare risk building  ● High". */
.day-sum.is-multi { grid-template-columns: 1fr; row-gap: 3px; }
.day-line {
  display: grid;
  grid-template-columns: 1fr auto;
  column-gap: 10px;
  align-items: baseline;
}
.day-line .day-stage { grid-column: auto; }
.day-cond { font-weight: 600; color: var(--text-primary); }
.plan-head { margin: 2px 0 6px; font-size: 13px; font-weight: 600; }

.fine.safety { color: var(--text-secondary); }
