/* AI-SWaMP · "A lit chart of the river basin at night". Rules and token meanings: ../DESIGN.md */

:root {
  color-scheme: light;
  --sans: "Instrument Sans", sans-serif;
  --mono: "Spline Sans Mono", monospace;
  --r-tag: 6px;
  --r-ctl: 10px;
  --r-panel: 14px;

  --bg: #f1f6f6;
  --surface: #ffffff;
  --surface-2: #e2ecec;
  --ink: #0d1d20;
  --muted: #4a5e62;
  --faint: #8a9a9c;
  --line: #d2e0e0;
  --focus: #0b6e69;
  --accent: #12a39b;
  --accent-ink: #0b6e69;
  --on-accent: #ffffff;
  --ok: #13663a;
  --ok-bg: #dff3e6;
  --bad: #a3271f;
  --bad-bg: #fbe3e0;
  --shadow: 0 1px 2px rgba(13, 29, 32, .08), 0 6px 20px rgba(13, 29, 32, .10);
  --scrim: rgba(13, 29, 32, .40);
  --glass-bg: rgba(255, 255, 255, .62);
  --glass-hover: rgba(226, 236, 236, .78);
  --glass-border: rgba(255, 255, 255, .70);
  --glass-edge: rgba(13, 29, 32, .10);
  --glass-highlight: rgba(255, 255, 255, .85);

  --map-land: #eef3f2;
  --map-land-2: #e2eae9;
  --map-water: #b9e0dc;
  --map-river: #4fb9b2;
  --map-road: #d6e0df;
  --map-label: #4a5e62;
  --map-mask: #0d1d20;
  --map-mask-opacity: .10;
  --map-ws-fill: .06;
  --map-glow: 0;
  --st-ai: #f2b200;
  --st-stroke: #5a3d00;
  --st-llda: #b4541a;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0b1417;
    --surface: #101c20;
    --surface-2: #17272c;
    --ink: #e4f0f0;
    --muted: #9db3b6;
    --faint: #6f8689;
    --line: #22363b;
    --focus: #3fd0c9;
    --accent: #3fd0c9;
    --accent-ink: #3fd0c9;
    --on-accent: #06201f;
    --ok: #7fe0a6;
    --ok-bg: #12301f;
    --bad: #ff9c91;
    --bad-bg: #3a1714;
    --shadow: 0 1px 2px rgba(0, 0, 0, .5), 0 8px 28px rgba(0, 0, 0, .55);
    --scrim: rgba(0, 0, 0, .60);
    --glass-bg: rgba(16, 28, 32, .58);
    --glass-hover: rgba(23, 39, 44, .78);
    --glass-border: rgba(255, 255, 255, .10);
    --glass-edge: rgba(0, 0, 0, .35);
    --glass-highlight: rgba(255, 255, 255, .10);
    --map-land: #0b1417;
    --map-land-2: #0f1c20;
    --map-water: #0f3434;
    --map-river: #2aa8a1;
    --map-road: #182a2f;
    --map-label: #7f9a9d;
    --map-mask: #000000;
    --map-mask-opacity: .45;
    --map-ws-fill: .05;
    --map-glow: 1;
    --st-ai: #ffe066;
    --st-stroke: #101c20;
    --st-llda: #f0a35e;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0b1417;
  --surface: #101c20;
  --surface-2: #17272c;
  --ink: #e4f0f0;
  --muted: #9db3b6;
  --faint: #6f8689;
  --line: #22363b;
  --focus: #3fd0c9;
  --accent: #3fd0c9;
  --accent-ink: #3fd0c9;
  --on-accent: #06201f;
  --ok: #7fe0a6;
  --ok-bg: #12301f;
  --bad: #ff9c91;
  --bad-bg: #3a1714;
  --shadow: 0 1px 2px rgba(0, 0, 0, .5), 0 8px 28px rgba(0, 0, 0, .55);
  --scrim: rgba(0, 0, 0, .60);
  --glass-bg: rgba(16, 28, 32, .58);
  --glass-hover: rgba(23, 39, 44, .78);
  --glass-border: rgba(255, 255, 255, .10);
  --glass-edge: rgba(0, 0, 0, .35);
  --glass-highlight: rgba(255, 255, 255, .10);
  --map-land: #0b1417;
  --map-land-2: #0f1c20;
  --map-water: #0f3434;
  --map-river: #2aa8a1;
  --map-road: #182a2f;
  --map-label: #7f9a9d;
  --map-mask: #000000;
  --map-mask-opacity: .45;
  --map-ws-fill: .05;
  --map-glow: 1;
  --st-ai: #ffe066;
  --st-stroke: #101c20;
  --st-llda: #f0a35e;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 400 15px/1.45 var(--sans);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}
button { font: inherit; color: inherit; cursor: pointer; }
button:disabled { cursor: not-allowed; }
p, h1, h2, h3 { margin: 0; }
h1, h2, h3 { font-weight: 700; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
a { color: var(--accent-ink); text-underline-offset: 2px; }
[data-icon] svg, .ico svg { display: block; }
.num { font-family: var(--mono); font-variant-numeric: tabular-nums; font-weight: 500; }

#map { position: fixed; inset: 0; background: var(--map-land); }

/* ---------- Floating chrome ---------- */
.chrome {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-panel);
  box-shadow: var(--shadow);
}
/* Liquid glass: the floating controls on the map (title, tool buttons, map buttons, legend).
   Panels, menus and dialogs stay solid so long text stays easy to read. */
.glass {
  background: var(--glass-bg);
  border: 1px solid var(--glass-border);
  box-shadow: inset 0 1px 0 var(--glass-highlight), 0 0 0 1px var(--glass-edge), var(--shadow);
  -webkit-backdrop-filter: blur(16px) saturate(1.6); backdrop-filter: blur(16px) saturate(1.6);
}
.iconbtn.glass { background: var(--glass-bg); }
.zoom.glass .iconbtn { background: transparent; }
.iconbtn.glass:hover, .zoom.glass .iconbtn:hover { background: var(--glass-hover); }
.zoom.glass .iconbtn + .iconbtn { border-top-color: var(--glass-edge); }
.legend.glass .legend-item:hover { background: var(--glass-hover); }

.brand {
  position: fixed; top: 16px; left: 16px; z-index: 3;
  display: flex; align-items: center; gap: 12px;
  min-height: 44px; padding: 0 8px 0 14px; width: 380px; border-radius: var(--r-ctl);
}
.brand-name { font-size: 17px; font-weight: 700; letter-spacing: .01em; line-height: 1.15; white-space: nowrap; } /* never break "AI-SWaMP" at its hyphen */
.brand .tag { margin-left: auto; }

.tag {
  display: inline-block; font-size: 12px; font-weight: 600; letter-spacing: .02em; line-height: 1;
  padding: 4px 6px; border-radius: var(--r-tag); background: var(--surface-2); color: var(--muted);
  white-space: nowrap;
}
.eyebrow { font-size: 12px; font-weight: 600; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); }

.tools { position: fixed; top: 16px; right: 16px; z-index: 3; display: flex; gap: 8px; }
.iconbtn {
  width: 44px; height: 44px; display: grid; place-items: center; font-size: 22px;
  border-radius: var(--r-ctl); color: var(--ink); background: var(--surface);
}
.iconbtn.chrome { border-radius: var(--r-ctl); }
button.iconbtn:not(.chrome) { border: 0; }
.iconbtn:hover { background: var(--surface-2); }
.iconbtn[aria-expanded="true"] { background: var(--surface-2); color: var(--accent-ink); }

.popover {
  position: fixed; top: 72px; right: 16px; z-index: 4; width: 290px; padding: 12px;
  display: grid; gap: 4px;
  /* Stop above the map buttons (193 px tall, 24 px from the bottom, plus a 16 px gap); scroll inside if needed */
  max-height: calc(100dvh - 72px - 233px); overflow-y: auto; overscroll-behavior: contain;
}
.popover .eyebrow { margin: 6px 4px 2px; }
.popover .eyebrow:first-child { margin-top: 0; }
.check {
  display: flex; align-items: center; gap: 10px; min-height: 44px; padding: 0 6px; border-radius: var(--r-ctl);
  cursor: pointer;
}
.check:hover { background: var(--surface-2); }
.check input { width: 18px; height: 18px; margin: 0; accent-color: var(--accent-ink); }
.check.is-soon { color: var(--muted); cursor: default; }
.check.is-soon:hover { background: none; }
.check .tag { margin-left: auto; }
.menuitem {
  display: flex; align-items: center; gap: 10px; min-height: 44px; padding: 0 8px; border: 0; background: none;
  border-radius: var(--r-ctl); text-align: left; font-size: 15px;
}
.menuitem:hover { background: var(--surface-2); }
.menuitem [data-icon] { font-size: 20px; color: var(--muted); }
.menuitem .tag { margin-left: auto; }

.key { display: inline-block; flex: none; width: 12px; height: 12px; border-radius: 3px; border: 2px dashed var(--line); }
.key-ai { border: 0; border-radius: 50%; background: var(--st-ai); box-shadow: inset 0 0 0 1.5px var(--st-stroke); }
.key-llda { border: 0; border-radius: 2px; background: var(--st-llda); }
.key-ws { width: 16px; height: 10px; border: 2px solid var(--accent); border-radius: 3px; }

.seg { display: flex; gap: 3px; padding: 3px; border-radius: var(--r-ctl); background: var(--surface-2); }
.seg button {
  flex: 1; min-height: 44px; border: 0; background: transparent; color: var(--muted);
  font-weight: 600; font-size: 14px; border-radius: calc(var(--r-ctl) - 3px);
}
.seg button[aria-checked="true"], .seg button[aria-pressed="true"] { background: var(--surface); color: var(--ink); box-shadow: 0 0 0 1px var(--line); }

.mapctl { position: fixed; right: 16px; bottom: 24px; z-index: 2; display: grid; gap: 8px; justify-items: end; }
.zoom { display: grid; overflow: hidden; border-radius: var(--r-ctl); }
.zoom .iconbtn { border-radius: 0; }
.zoom .iconbtn + .iconbtn { border-top: 1px solid var(--line); }

/* Legend: always visible; each item shows or hides its layer (same switches as in Layers) */
.legend { position: fixed; left: 412px; bottom: 24px; z-index: 2; display: flex; gap: 2px; padding: 3px; border-radius: var(--r-ctl); }
.legend-item {
  display: inline-flex; align-items: center; gap: 7px; min-height: 44px; padding: 0 12px; border: 0; border-radius: calc(var(--r-ctl) - 3px);
  background: none; color: var(--ink); font-size: 14px; font-weight: 600;
}
.legend-item:hover { background: var(--surface-2); }
.legend-item[aria-pressed="false"] { color: var(--muted); font-weight: 400; text-decoration: line-through; }
.legend-item[aria-pressed="false"] .key { opacity: .35; }

/* Map credit: always visible and faint, in the map's bottom-right corner, under the map buttons (OpenStreetMap guidelines) */
.credit {
  position: fixed; right: 16px; bottom: 4px; z-index: 1; margin: 0; pointer-events: none;
  font-size: 11px; color: var(--map-label); text-shadow: 0 0 3px var(--map-land), 0 0 6px var(--map-land);
}
.key-mask { border: 0; background: var(--map-mask); opacity: .5; }


.tooltip {
  position: fixed; z-index: 6; pointer-events: none; padding: 8px 10px; border-radius: var(--r-ctl);
  font-size: 13px; max-width: 260px;
}
.tooltip strong { display: block; font-size: 14px; }
.tooltip span { color: var(--muted); }
.toast { position: fixed; left: 50%; transform: translateX(-50%); bottom: 24px; z-index: 7; padding: 10px 14px; border-radius: var(--r-ctl); font-size: 14px; width: max-content; max-width: calc(100vw - 32px); }

/* MapLibre: hide its own small controls (ours are 44 px). The credit line is ours (.credit). */
.maplibregl-ctrl-top-right { display: none; }
.maplibregl-user-location-dot, .maplibregl-user-location-dot::before { background-color: var(--accent); }
.maplibregl-user-location-dot::after { border-color: var(--surface); }

/* ---------- Panel / bottom sheet ---------- */
.sheet {
  position: fixed; z-index: 3; left: 16px; top: 72px; bottom: 16px; width: 380px;
  display: flex; flex-direction: column; overflow: hidden;
}
.sheet-head { display: none; }
.modules {
  display: flex; gap: 4px; padding: 8px; border-bottom: 1px solid var(--line);
  overflow-x: auto; scrollbar-width: none; flex: none;
}
.modules::-webkit-scrollbar { display: none; }
.module {
  flex: none; display: inline-flex; align-items: center; gap: 6px; min-height: 44px; padding: 0 12px;
  border: 0; border-radius: var(--r-ctl); background: none; color: var(--muted); font-weight: 600; font-size: 14px;
}
.module:hover { background: var(--surface-2); }
.module[aria-pressed="true"] { background: var(--surface-2); color: var(--ink); }
.panel { flex: 1; overflow-y: auto; padding: 16px; overscroll-behavior: contain; }

.panel h1 { font-size: 20px; line-height: 1.2; }
.lede { color: var(--muted); margin: 4px 0 14px; }
.search {
  display: flex; align-items: center; gap: 8px; min-height: 44px; padding: 0 12px; margin-bottom: 8px;
  border: 1px solid var(--line); border-radius: var(--r-ctl); background: var(--bg); color: var(--muted);
}
.search:focus-within { border-color: var(--focus); }
.search input { flex: 1; min-width: 0; min-height: 42px; border: 0; background: none; color: var(--ink); font: inherit; outline: none; }
.search input::placeholder { color: var(--muted); }
.group { border: 1px solid var(--line); border-radius: var(--r-ctl); margin-top: 8px; }
.group-title {
  display: flex; align-items: center; gap: 8px; min-height: 48px; padding: 0 10px 0 12px;
  list-style: none; cursor: pointer; border-radius: var(--r-ctl);
}
.group-title::-webkit-details-marker { display: none; }
.group-title:hover { background: var(--surface-2); }
.group-title .count { margin-left: auto; color: var(--muted); font-size: 13px; }
.group-title .chev { font-size: 20px; color: var(--muted); transition: transform .2s ease; }
.group[open] .group-title .chev { transform: rotate(180deg); }
.group[open] .group-title { border-radius: var(--r-ctl) var(--r-ctl) 0 0; border-bottom: 1px solid var(--line); }
.group .list { padding: 4px; }
.list { list-style: none; margin: 0; padding: 0; }
.list button {
  width: 100%; min-height: 44px; display: flex; align-items: center; gap: 10px; padding: 0 8px;
  border: 0; border-radius: var(--r-ctl); background: none; text-align: left;
}
.list button:hover, .list button.is-hover { background: var(--surface-2); }
.list .meta { margin-left: auto; color: var(--muted); font-size: 13px; }
.list .key { margin: 0 2px; }

.back {
  display: inline-flex; align-items: center; gap: 4px; min-height: 44px; padding: 0 10px 0 4px; margin: -8px 0 4px -4px;
  border: 0; background: none; color: var(--accent-ink); font-weight: 600; border-radius: var(--r-ctl);
}
.back [data-icon] { font-size: 20px; }
.station-head { display: flex; gap: 10px; align-items: flex-start; }
.station-head .key { margin-top: 7px; }
.station-sub { color: var(--muted); font-size: 13px; margin-top: 2px; }
.facts { display: grid; grid-template-columns: auto 1fr; gap: 2px 12px; margin: 12px 0 14px; font-size: 13px; color: var(--muted); }
.facts dd { margin: 0; color: var(--ink); word-break: break-word; }
.note {
  margin: 0 0 14px; padding: 10px 12px; border-radius: var(--r-ctl); background: var(--surface-2);
  color: var(--ink); font-size: 13px;
}
.range { margin-bottom: 6px; }
.caption { color: var(--muted); font-size: 13px; margin: 6px 0 12px; }

.param { border: 1px solid var(--line); border-radius: var(--r-ctl); padding: 12px; margin-bottom: 10px; background: var(--surface); }
.param-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.param-head h3 { font-size: 15px; font-weight: 600; }
.param-head .unit { color: var(--muted); font-size: 13px; font-weight: 400; }
.param .latest { font-size: 13px; color: var(--muted); }
.param .limit { color: var(--muted); font-size: 13px; margin: 2px 0 8px; }
.param svg.chart { display: block; width: 100%; height: auto; overflow: visible; }
.chart .grid { stroke: var(--line); stroke-width: 1; }
.chart .base { stroke: var(--faint); stroke-width: 1; }
.chart .axis { fill: var(--muted); font: 500 11px var(--mono); }
.chart .limit-line { stroke: var(--bad); stroke-width: 1.5; stroke-dasharray: 4 3; }
.chart .limit-label { fill: var(--muted); font: 600 11px var(--sans); }
.chart .series { fill: none; stroke: var(--accent-ink); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .dot { fill: var(--accent-ink); stroke: var(--surface); stroke-width: 2; }
.chart .dot.hot { fill: var(--bad); }
.empty {
  display: grid; place-items: center; min-height: 72px; padding: 8px 12px; border-radius: var(--r-ctl);
  background: var(--surface-2); color: var(--muted); font-size: 13px; text-align: center;
}
.status { display: inline-flex; align-items: center; gap: 4px; font-size: 12px; font-weight: 600; padding: 3px 6px; border-radius: var(--r-tag); }
.status.bad { color: var(--bad); background: var(--bad-bg); }
.status.ok { color: var(--ok); background: var(--ok-bg); }

.soon-card { border: 1px dashed var(--faint); border-radius: var(--r-ctl); padding: 16px; display: grid; gap: 10px; }
.soon-card h2 { font-size: 15px; display: flex; align-items: center; gap: 8px; }
.soon-card ul { margin: 0; padding-left: 18px; color: var(--muted); }
.fake {
  min-height: 44px; display: flex; align-items: center; padding: 0 12px; border-radius: var(--r-ctl);
  background: var(--surface-2); color: var(--muted); font-size: 14px;
}

.help-note { color: var(--muted); font-size: 13px; margin: 0 4px 8px; }
.help-note:last-child { margin-bottom: 4px; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: 44px; padding: 0 16px;
  border: 1px solid var(--line); border-radius: var(--r-ctl); background: var(--surface); color: var(--ink); font-weight: 600; font-size: 15px;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent-ink); border-color: var(--accent-ink); color: var(--on-accent); }
.btn [data-icon] { font-size: 20px; }
.file-btn { position: relative; overflow: hidden; }
.file-btn input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }

/* ---------- Dialog ---------- */
dialog {
  width: min(520px, calc(100vw - 32px)); max-height: calc(100dvh - 48px); padding: 0;
  border: 1px solid var(--line); border-radius: var(--r-panel); background: var(--surface); color: var(--ink); box-shadow: var(--shadow);
}
dialog::backdrop { background: var(--scrim); }
.dialog-body { padding: 20px; display: grid; gap: 12px; }
.dialog-body h2 { font-size: 20px; display: flex; align-items: center; gap: 8px; }
.dialog-body p { color: var(--muted); }
.dialog-actions { display: flex; justify-content: flex-end; }
.admin-row { border: 1px solid var(--line); border-radius: var(--r-ctl); padding: 12px; display: grid; gap: 8px; }
.admin-row h3 { font-size: 15px; display: flex; align-items: center; gap: 8px; }
.admin-row p { font-size: 13px; }
.row-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.upload-status { font-size: 13px; color: var(--muted); }
.upload-status.is-error { color: var(--bad); }

/* ---------- Phones: map on top, the panel becomes a bottom sheet ---------- */
@media (max-width: 820px) {
  .brand { top: 12px; left: 12px; width: auto; padding: 0 14px; }
  .brand .tag { display: none; }
  .tools { top: 12px; right: 12px; gap: 6px; }
  .popover { top: 64px; left: 12px; right: 12px; width: auto; max-height: calc(100dvh - var(--sheet-h) - 76px); overflow-y: auto; overscroll-behavior: contain; }

  /* Three heights: "peek" shows only the handle row and the module chips */
  body { --sheet-h: 46dvh; }
  body[data-sheet="peek"] { --sheet-h: calc(98px + env(safe-area-inset-bottom)); }
  body[data-sheet="full"] { --sheet-h: 88dvh; }
  .sheet {
    left: 0; right: 0; bottom: 0; top: auto; width: auto; height: var(--sheet-h);
    border-radius: var(--r-panel) var(--r-panel) 0 0; border-bottom: 0;
    padding-bottom: env(safe-area-inset-bottom);
    transition: height .25s ease;
  }
  body.is-dragging .sheet { transition: none; }
  .sheet-head { display: flex; align-items: center; flex: none; padding: 0 4px 0 48px; }
  .grab { flex: 1; display: grid; place-items: center; height: 44px; border: 0; background: none; touch-action: none; cursor: grab; }
  .grab span { width: 40px; height: 5px; border-radius: 3px; background: var(--faint); }
  .collapse-btn { color: var(--muted); }
  .collapse-btn svg { transition: transform .2s ease; }
  body[data-sheet="peek"] .collapse-btn svg { transform: rotate(180deg); }
  .modules { padding: 0 8px 8px; }
  .panel { padding: 12px 16px 24px; }
  body[data-sheet="peek"] .panel { visibility: hidden; } /* only the headers show; keeps hidden rows out of tab order */

  .mapctl, .legend { transition: opacity .2s; }
  .credit { left: 12px; right: 12px; bottom: calc(var(--sheet-h) + 4px); text-align: right; }
  .mapctl { right: 12px; bottom: calc(var(--sheet-h) + 22px); }
  /* Phone legend: a slim strip (the glass comes from .glass) so more of the map shows. Items look 32 px tall;
     an invisible ::after keeps each hit area at 44 px. */
  .legend {
    left: 12px; bottom: calc(var(--sheet-h) + 22px); max-width: calc(100vw - 24px - 44px - 8px); padding: 2px; gap: 0;
  }
  .legend-item { position: relative; min-height: 32px; padding: 0 8px; font-size: 12px; gap: 5px; }
  .legend-item::after { content: ""; position: absolute; inset: -6px 0; }
  .legend .key { width: 10px; height: 10px; }
  .legend .key-ws { width: 13px; height: 8px; }
  .popover .check.is-soon { display: none; } /* keeps Layers short enough to fit above the sheet */
  /* The top menus cover the map buttons on a phone; hide those until the menu closes */
  body[data-pop] :is(.mapctl, .legend) { visibility: hidden; }
  body[data-sheet="full"] :is(.mapctl, .legend) { opacity: 0; pointer-events: none; }
  .toast { top: 72px; bottom: auto; }

  dialog { width: 100vw; max-width: 100vw; margin: auto 0 0; border-radius: var(--r-panel) var(--r-panel) 0 0; border-bottom: 0; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
