/* River Brain: tokens first, then layout, then components. Light and dark are both
   designed (dark uses its own validated steps of the same hues, not an inversion). */
:root {
  color-scheme: light;
  --surface-0: #f4f3f0;      /* page */
  --surface-1: #fcfcfb;      /* cards, chart surface */
  --surface-2: #efeee9;      /* hover, controls */
  --line: #e4e3df;           /* hairlines, grid */
  --line-strong: #c9c8c2;
  --text-1: #0b0b0b;
  --text-2: #52514e;
  --text-3: #75746f;
  --accent: #2a78d6;
  /* one hue per physical component (validated: scripts/validate_palette.js) */
  --c-tide: #2a78d6;
  --c-bonneville: #eb6834;
  --c-willamette: #1baf7a;
  --c-sandy: #eda100;
  --c-spring_neap: #4a3aa7;
  --c-ocean: #e87ba4;
  --c-unexplained: #9a9993;
  --c-observed: #0b0b0b;
  --c-nws: #75746f;
  /* status: reserved, always shipped with an icon + words */
  --ok: #1a7f37;
  --warn: #9a6700;
  --bad: #c62828;
  --band: rgba(42, 120, 214, 0.14);
  --radius: 12px;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04), 0 1px 1px rgba(0, 0, 0, 0.03);
}
@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --surface-0: #121211; --surface-1: #1a1a19; --surface-2: #262624;
    --line: #2e2e2c; --line-strong: #454542;
    --text-1: #ffffff; --text-2: #c3c2b7; --text-3: #9a9990;
    --accent: #3987e5;
    --c-tide: #3987e5; --c-bonneville: #d95926; --c-willamette: #199e70; --c-sandy: #c98500;
    --c-spring_neap: #9085e9; --c-ocean: #d55181; --c-unexplained: #8a8983; --c-observed: #ffffff;
    --c-nws: #9a9990;
    --ok: #4ac26b; --warn: #d4a72c; --bad: #ff6b6b;
    --band: rgba(57, 135, 229, 0.22);
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --surface-0: #121211; --surface-1: #1a1a19; --surface-2: #262624;
  --line: #2e2e2c; --line-strong: #454542;
  --text-1: #ffffff; --text-2: #c3c2b7; --text-3: #9a9990;
  --accent: #3987e5;
  --c-tide: #3987e5; --c-bonneville: #d95926; --c-willamette: #199e70; --c-sandy: #c98500;
  --c-spring_neap: #9085e9; --c-ocean: #d55181; --c-unexplained: #8a8983; --c-observed: #ffffff;
  --c-nws: #9a9990;
  --ok: #4ac26b; --warn: #d4a72c; --bad: #ff6b6b;
  --band: rgba(57, 135, 229, 0.22);
  --shadow: none;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--surface-0); color: var(--text-1);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
a { color: var(--accent); }
h1, h2, h3 { line-height: 1.25; margin: 0; }
h2 { font-size: 1.05rem; font-weight: 650; }
h3 { font-size: 0.95rem; font-weight: 600; }
p { margin: 0.4em 0; }
small, .muted { color: var(--text-2); }
.faint { color: var(--text-3); }
.num { font-variant-numeric: tabular-nums; }

/* ---- shell ---- */
header.top {
  position: sticky; top: 0; z-index: 10; background: var(--surface-0);
  border-bottom: 1px solid var(--line);
}
.top-inner { max-width: 820px; margin: 0 auto; padding: 10px 16px 0; }
.brand { display: flex; align-items: baseline; gap: 10px; }
.brand b { font-size: 1.05rem; }
nav.tabs { display: flex; gap: 2px; overflow-x: auto; scrollbar-width: none; margin-top: 6px; }
nav.tabs::-webkit-scrollbar { display: none; }
nav.tabs a {
  padding: 8px 12px; border-radius: 8px 8px 0 0; color: var(--text-2); text-decoration: none;
  white-space: nowrap; font-weight: 550; border-bottom: 2px solid transparent;
}
nav.tabs a[aria-current="page"] { color: var(--text-1); border-bottom-color: var(--accent); }
main { max-width: 820px; margin: 0 auto; padding: 16px; display: grid; gap: 16px; }
section.view { display: grid; gap: 16px; }
section.view[hidden] { display: none; }
footer { max-width: 820px; margin: 0 auto; padding: 8px 16px 40px; color: var(--text-3); font-size: 0.85rem; }

.card {
  background: var(--surface-1); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 16px; box-shadow: var(--shadow); min-width: 0;
}
.card-head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; flex-wrap: wrap; margin-bottom: 8px; }
.grid2 { display: grid; gap: 16px; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); }

/* ---- hero ---- */
.hero { display: grid; gap: 4px; }
.hero .value { font-size: 3.4rem; font-weight: 650; line-height: 1; letter-spacing: -0.02em; }
.hero .value small { font-size: 1.1rem; font-weight: 500; color: var(--text-2); letter-spacing: 0; }
.hero .dir { font-size: 1.15rem; font-weight: 550; }
.dir-arrow { display: inline-block; width: 1.1em; text-align: center; }

/* ---- segmented control ---- */
.seg { display: inline-flex; background: var(--surface-2); border-radius: 8px; padding: 2px; }
.seg button {
  font: inherit; font-size: 0.85rem; border: 0; background: transparent; color: var(--text-2);
  padding: 4px 10px; border-radius: 6px; cursor: pointer;
}
.seg button[aria-pressed="true"] { background: var(--surface-1); color: var(--text-1); font-weight: 600; box-shadow: var(--shadow); }
.seg button:focus-visible, nav.tabs a:focus-visible, select:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
select { font: inherit; font-size: 0.9rem; padding: 4px 8px; border-radius: 8px; border: 1px solid var(--line-strong);
  background: var(--surface-1); color: var(--text-1); }

/* ---- why-it-moved diverging bars ---- */
.bars { display: grid; gap: 6px; margin-top: 6px; }
.bar-row { display: grid; grid-template-columns: minmax(7.5em, 11em) 1fr; align-items: center; gap: 10px; }
.bar-row .lab { font-size: 0.9rem; display: flex; align-items: center; gap: 6px; min-width: 0; }
.bar-row .lab span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.key { width: 10px; height: 10px; border-radius: 3px; flex: none; }
.track { position: relative; height: 22px; }
.track::before { content: ""; position: absolute; left: 50%; top: -3px; bottom: -3px; width: 1px; background: var(--line-strong); }
.bar { position: absolute; top: 3px; height: 16px; min-width: 2px; }
.bar.pos { left: 50%; border-radius: 0 4px 4px 0; }
.bar.neg { right: 50%; border-radius: 4px 0 0 4px; }
.bar-val { position: absolute; top: 0; line-height: 22px; font-size: 0.85rem; font-variant-numeric: tabular-nums; color: var(--text-1); white-space: nowrap; }
.bar-row.total { border-top: 1px solid var(--line); padding-top: 6px; }
.bar-row.total .lab { font-weight: 650; }
.bar-row:hover .bar { filter: brightness(1.08); }

/* ---- lists and chips ---- */
.tides { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 8px; }
.tide { background: var(--surface-2); border-radius: 10px; padding: 8px 10px; }
.tide .t { font-weight: 600; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { background: var(--surface-2); border-radius: 999px; padding: 4px 12px; font-size: 0.88rem; }
.chip b { font-weight: 650; }

.banner { border-radius: 10px; padding: 10px 12px; background: color-mix(in srgb, var(--warn) 12%, var(--surface-1));
  border: 1px solid color-mix(in srgb, var(--warn) 40%, transparent); font-size: 0.92rem; }
.status { display: inline-flex; align-items: center; gap: 4px; font-size: 0.85rem; }
.status i { font-style: normal; font-weight: 700; }
.status.ok i { color: var(--ok); } .status.stale i { color: var(--warn); } .status.missing i { color: var(--bad); }
.fresh { display: flex; flex-wrap: wrap; gap: 4px 14px; }

/* ---- tables ---- */
table { border-collapse: collapse; width: 100%; font-size: 0.9rem; }
th, td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { color: var(--text-2); font-weight: 600; font-size: 0.82rem; }
td.r, th.r { text-align: right; font-variant-numeric: tabular-nums; }
.table-wrap { overflow-x: auto; }
.meter { position: relative; height: 8px; background: color-mix(in srgb, var(--accent) 18%, var(--surface-2)); border-radius: 4px; min-width: 60px; }
.meter > i { position: absolute; left: 0; top: 0; bottom: 0; background: var(--accent); border-radius: 4px; }
.meter > b { position: absolute; top: -3px; bottom: -3px; width: 2px; background: var(--text-2); }

/* ---- tiles ---- */
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; }
.tile { background: var(--surface-2); border-radius: 10px; padding: 10px 12px; }
.tile .lab { font-size: 0.82rem; color: var(--text-2); }
.tile .v { font-size: 1.5rem; font-weight: 650; line-height: 1.2; }
.tile .sub { font-size: 0.8rem; color: var(--text-3); }

/* ---- charts ---- */
.chart { position: relative; width: 100%; touch-action: pan-y; }
.chart svg { display: block; width: 100%; overflow: visible; }
.chart svg:focus { outline: none; }
.chart svg:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 4px; }
.chart .grid line { stroke: var(--line); stroke-width: 1; }
.chart .axis text { fill: var(--text-3); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .ref line { stroke: var(--text-3); stroke-width: 1; }
.chart .ref text { fill: var(--text-2); font-size: 11px; }
.chart .now line { stroke: var(--text-2); stroke-width: 1; }
.chart .now text { fill: var(--text-2); font-size: 11px; }
.chart .cross { stroke: var(--text-2); stroke-width: 1; }
.chart .dot { stroke: var(--surface-1); stroke-width: 2; }
.legend { display: flex; flex-wrap: wrap; gap: 4px 14px; font-size: 0.85rem; color: var(--text-2); margin: 2px 0 6px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.lkey { width: 16px; height: 0; border-top: 2px solid; display: inline-block; }
.lkey.dash { border-top-style: dashed; }
.lkey.area { height: 10px; border: 0; border-radius: 2px; }
.tip {
  position: absolute; pointer-events: none; z-index: 5; background: var(--surface-1); border: 1px solid var(--line-strong);
  border-radius: 8px; padding: 6px 8px; font-size: 0.8rem; box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12); min-width: 130px;
}
.tip .when { color: var(--text-2); margin-bottom: 2px; }
.tip .row { display: flex; align-items: center; gap: 6px; white-space: nowrap; }
.tip .row b { font-variant-numeric: tabular-nums; min-width: 4.2em; }
.tip .row span { color: var(--text-2); }
.smalls { display: grid; gap: 8px; }
.small-title { font-size: 0.85rem; color: var(--text-1); display: flex; gap: 6px; align-items: center; margin-top: 6px; }

.equation { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size: 0.85rem; background: var(--surface-2);
  padding: 10px 12px; border-radius: 8px; overflow-x: auto; white-space: pre; }
details summary { cursor: pointer; color: var(--text-2); }
.loading { color: var(--text-3); }

/* Narrow screens: label above a full-width bar track, so bars have room to be read. */
@media (max-width: 560px) {
  .bar-row { grid-template-columns: 1fr; gap: 0; }
  .bars { gap: 4px; }
  .bar-row.total { padding-top: 8px; }
}

/* ================= Guide (long-form article) ================= */
.article { max-width: 760px; margin: 0 auto; padding: 16px 16px 60px; font-size: 1.02rem; line-height: 1.65; }
.article h1 { font-size: 2rem; letter-spacing: -0.01em; margin: 8px 0 6px; }
.article .lede { font-size: 1.15rem; color: var(--text-2); margin: 0 0 12px; }
.article h2 { font-size: 1.35rem; margin: 44px 0 10px; padding-top: 12px; border-top: 1px solid var(--line); scroll-margin-top: 90px; }
.article h2 .n { color: var(--text-3); font-weight: 500; margin-right: 6px; }
.article h3 { font-size: 1.08rem; margin: 26px 0 6px; scroll-margin-top: 90px; }
.article p { margin: 0.6em 0; }
.article ul, .article ol { padding-left: 1.3em; margin: 0.5em 0; }
.article li { margin: 0.3em 0; }
.article code { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size: 0.88em; background: var(--surface-2); padding: 1px 5px; border-radius: 4px; }
.article figure { margin: 18px 0; }
.article figure img { width: 100%; height: auto; display: block; border-radius: 10px; border: 1px solid var(--line); background: #fcfcfb; }
.article figcaption { font-size: 0.86rem; color: var(--text-2); margin-top: 6px; line-height: 1.5; }
.article .card { margin: 16px 0; }
.article table { font-size: 0.88rem; }
.meta { color: var(--text-3); font-size: 0.9rem; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; margin: 18px 0; }
.stat { background: var(--surface-1); border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; }
.stat .v { font-size: 1.6rem; font-weight: 650; line-height: 1.15; }
.stat .l { font-size: 0.84rem; color: var(--text-2); line-height: 1.35; margin-top: 2px; }
.toc { background: var(--surface-1); border: 1px solid var(--line); border-radius: 12px; padding: 12px 16px; margin: 16px 0; }
.toc ol { margin: 6px 0 0; columns: 2 220px; column-gap: 24px; }
.toc a { text-decoration: none; }
.toc a:hover { text-decoration: underline; }
.callout { border-left: 4px solid var(--accent); background: var(--surface-1); border-radius: 0 10px 10px 0; padding: 10px 14px; margin: 16px 0; }
.callout.learn { border-left-color: var(--c-willamette); }
.callout.limit { border-left-color: var(--warn); }
.callout b.t { display: block; font-size: 0.82rem; letter-spacing: 0.04em; text-transform: uppercase; color: var(--text-2); margin-bottom: 2px; }
.diagram { width: 100%; height: auto; display: block; }
.diagram .river { stroke: var(--c-tide); stroke-width: 10; stroke-linecap: round; fill: none; opacity: 0.35; }
.diagram .tick { stroke: var(--text-3); stroke-width: 1; }
.diagram .node { fill: var(--surface-1); stroke: var(--text-1); stroke-width: 2; }
.diagram .node.home { fill: var(--accent); stroke: var(--surface-1); }
.diagram text { fill: var(--text-1); font-size: 12px; font-family: system-ui, sans-serif; }
.diagram text.sub { fill: var(--text-2); font-size: 11px; }
.diagram text.small { fill: var(--text-3); font-size: 10.5px; }
.diagram .box { fill: var(--surface-1); stroke: var(--line-strong); stroke-width: 1; }
.diagram .box.hl { stroke: var(--accent); stroke-width: 1.5; }
.diagram .arrow { stroke: var(--text-2); stroke-width: 1.5; fill: none; marker-end: url(#ah); }
.diagram .arrowhead { fill: var(--text-2); }
.diagram .flow-bon { stroke: var(--c-bonneville); stroke-width: 2; fill: none; marker-end: url(#ahb); }
.diagram .flow-tide { stroke: var(--c-tide); stroke-width: 2; fill: none; marker-end: url(#aht); }
.diagram .flow-wil { stroke: var(--c-willamette); stroke-width: 2; fill: none; marker-end: url(#ahw); }
.diagram .flow-sandy { stroke: var(--c-sandy); stroke-width: 2; fill: none; marker-end: url(#ahs); }
.explorer-controls { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin: 8px 0; }
.explorer-controls input[type=range] { flex: 1; min-width: 180px; accent-color: var(--accent); }
.readout { font-variant-numeric: tabular-nums; font-weight: 600; }
.xy text { fill: var(--text-3); font-size: 11px; }
.xy .grid line { stroke: var(--line); }
.xy .lbl { fill: var(--text-2); font-size: 11px; }
dl.gloss dt { font-weight: 650; margin-top: 10px; }
dl.gloss dd { margin: 2px 0 0; color: var(--text-2); }
.backtop { font-size: 0.85rem; }

/* ---- refresh: pull indicator, header button, toast ---- */
html, body { overscroll-behavior-y: contain; }   /* our own pull-to-refresh replaces the browser's */
.brand { align-items: center; }
.icon-btn {
  margin-left: auto; font: inherit; font-size: 1.15rem; line-height: 1; width: 34px; height: 34px;
  border-radius: 50%; border: 1px solid var(--line); background: var(--surface-1); color: var(--text-2); cursor: pointer;
}
.icon-btn:hover { color: var(--text-1); }
.icon-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.is-refreshing .icon-btn span { display: inline-block; animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.pull {
  position: fixed; left: 50%; top: 64px; z-index: 20; margin-left: -80px; width: 160px; text-align: center;
  background: var(--surface-1); border: 1px solid var(--line-strong); border-radius: 999px; padding: 6px 10px;
  font-size: 0.85rem; color: var(--text-2); box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
  opacity: 0; pointer-events: none; transition: opacity 0.15s;
}
.pull.visible { opacity: 1; }
.pull .pull-arrow { display: inline-block; transition: transform 0.15s; }
.pull.armed { color: var(--text-1); border-color: var(--accent); }
.pull.armed .pull-arrow { transform: rotate(180deg); }
.toast {
  position: fixed; left: 16px; right: 16px; bottom: calc(16px + env(safe-area-inset-bottom)); z-index: 30;
  max-width: 480px; margin: 0 auto; background: var(--text-1); color: var(--surface-1);
  border-radius: 12px; padding: 10px 14px; font-size: 0.9rem; box-shadow: 0 6px 24px rgba(0, 0, 0, 0.2);
}
.toast[hidden] { display: none; }
@media (prefers-reduced-motion: reduce) { .is-refreshing .icon-btn span { animation: none; } }
