:root {
  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", Consolas, monospace;

  --sea: #dfe8ec;
  --land: #f4f2ec;
  --land-edge: #b9c3c6;
  --border-line: #a7b0b3;
  --grid: rgba(60, 80, 95, 0.08);

  --surface: rgba(255, 255, 255, 0.94);
  --surface-solid: #ffffff;
  --surface-2: #f2f4f5;
  --text: #17222b;
  --muted: #5d6b75;
  --line: rgba(23, 34, 43, 0.12);
  --shadow: 0 1px 2px rgba(16, 28, 38, 0.12), 0 6px 20px rgba(16, 28, 38, 0.1);
  --accent: #1f6feb;
  --barb: #2c4a63;

  --ok: #1f9d6a;
  --marginal: #e08a00;
  --fail: #d33f3f;
  --unknown: #8b96a0;
  --ok-bg: #e3f5ed;
  --marginal-bg: #fdf0db;
  --fail-bg: #fbe5e5;
  --unknown-bg: #eceff1;

  --better: #12865a;
  --worse: #c4471b;
  --steady: #4f6b85;

  color-scheme: light;
}

/* Night: low luminance to protect dark adaptation. */
:root[data-mode="night"] {
  --sea: #0b1218;
  --land: #17212a;
  --land-edge: #2b3945;
  --border-line: #3a4a57;
  --grid: rgba(150, 175, 195, 0.06);

  --surface: rgba(16, 24, 31, 0.94);
  --surface-solid: #10181f;
  --surface-2: #18232c;
  --text: #d5dde3;
  --muted: #8796a2;
  --line: rgba(213, 221, 227, 0.12);
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.5), 0 8px 24px rgba(0, 0, 0, 0.4);
  --accent: #f2b33d;
  --barb: #9fb3c4;

  --ok: #3fb984;
  --marginal: #f0a43a;
  --fail: #ef5f5f;
  --unknown: #6f7c87;
  --ok-bg: #12291f;
  --marginal-bg: #2e2312;
  --fail-bg: #331819;
  --unknown-bg: #1d262e;

  --better: #4cc592;
  --worse: #f08a5d;
  --steady: #8ea6bd;

  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: var(--font);
  color: var(--text);
  background: var(--sea);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  font-variant-numeric: tabular-nums;
}
button { font: inherit; color: inherit; }
h1, h2, h3, p { margin: 0; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

/* ---------- Map ---------- */
.map { position: fixed; inset: 0; touch-action: none; cursor: grab; user-select: none; -webkit-user-select: none; }
.map.dragging { cursor: grabbing; }
.map-base, .map-marks { position: absolute; inset: 0; width: 100%; height: 100%; }
.map-marks { pointer-events: none; }
.map-tags { position: absolute; inset: 0; pointer-events: none; }
.sea { fill: var(--sea); }
.land { fill: var(--land); stroke: var(--land-edge); stroke-width: 0.8px; vector-effect: non-scaling-stroke; stroke-linejoin: round; }
.lake { fill: var(--sea); stroke: var(--land-edge); stroke-width: 0.6px; vector-effect: non-scaling-stroke; }
.border { fill: none; stroke: var(--border-line); stroke-width: 1px; stroke-dasharray: 4 3; vector-effect: non-scaling-stroke; }
.grid { fill: none; stroke: var(--grid); stroke-width: 1px; vector-effect: non-scaling-stroke; }
.leader { stroke: var(--muted); stroke-width: 1; opacity: 0.7; }
.anchor { fill: var(--surface-solid); stroke: var(--text); stroke-width: 1.2; }
.barb { fill: none; stroke: var(--barb); stroke-width: 1.6; stroke-linecap: round; }
.pennant { fill: var(--barb); }

/* ---------- Station tag ---------- */
.tag {
  --c: var(--unknown);
  --cbg: var(--unknown-bg);
  position: absolute; left: 0; top: 0;
  display: grid; justify-items: start;
  min-width: 62px;
  padding: 0 0 3px;
  border: 2px solid var(--c);
  border-radius: 9px;
  background: var(--surface-solid);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.16);
  pointer-events: auto;
  cursor: pointer;
  line-height: 1.15;
  text-align: left;
  will-change: transform;
  transition: box-shadow 0.15s;
}
.tag.static { position: static; pointer-events: none; }
.tag:hover { box-shadow: 0 0 0 3px color-mix(in srgb, var(--c) 30%, transparent), 0 2px 6px rgba(0, 0, 0, 0.2); z-index: 2; }
.tag:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; z-index: 3; }
.tag.selected { box-shadow: 0 0 0 4px color-mix(in srgb, var(--c) 45%, transparent), 0 4px 14px rgba(0, 0, 0, 0.25); z-index: 4; }
.tag .cap {
  justify-self: stretch;
  display: flex; align-items: center; gap: 4px;
  padding: 1px 6px 2px 4px;
  margin-bottom: 2px;
  background: var(--c);
  color: #fff;
  font-size: 10.5px; font-weight: 700; letter-spacing: 0.04em;
  white-space: nowrap;
}
:root[data-mode="night"] .tag .cap { color: #0b1218; }
.tag .cap .icon { font-size: 10px; width: 10px; text-align: center; }
.tag .row { display: flex; align-items: baseline; gap: 2px; padding: 0 6px; white-space: nowrap; }
.tag b { font-size: 14px; font-weight: 700; }
.tag b.none { color: var(--muted); font-weight: 500; }
.tag small { font-size: 10px; color: var(--muted); margin-right: 3px; }
.tag em { font-style: normal; font-size: 11px; font-weight: 600; color: var(--muted); }
.trend { font-style: normal; font-weight: 800; font-size: 12px; margin-left: 2px; }
.trend.better { color: var(--better); }
.trend.worse { color: var(--worse); }
.trend.steady { color: var(--steady); }

.s-ok { --c: var(--ok); --cbg: var(--ok-bg); }
.s-marginal { --c: var(--marginal); --cbg: var(--marginal-bg); }
.s-fail { --c: var(--fail); --cbg: var(--fail-bg); }
.s-unknown, .s-nodata { --c: var(--unknown); --cbg: var(--unknown-bg); }
.tag.s-nodata { opacity: 0.75; }

/* Zoomed out: drop caption and units, keep the two numbers. */
.compact .tag { min-width: 0; border-radius: 7px; padding: 1px 0 2px; border-top-width: 5px; }
.compact .tag .cap, .compact .tag small, .compact .tag em { display: none; }
.compact .tag .row { padding: 0 4px; }
.compact .tag b { font-size: 11.5px; }
.compact .tag .trend { font-size: 10px; }

/* ---------- Floating chrome ---------- */
.card {
  background: var(--surface);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: var(--shadow);
}
#topbar {
  position: fixed; top: 0; left: 0; right: 0;
  display: flex; justify-content: space-between; align-items: flex-start; gap: 10px;
  padding: max(12px, env(safe-area-inset-top)) 12px 0;
  pointer-events: none;
}
#topbar > * { pointer-events: auto; }
.brand { padding: 10px 14px 12px; min-width: 220px; }
.title-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
h1 { font-size: 11px; font-weight: 700; letter-spacing: 0.12em; text-transform: uppercase; color: var(--muted); }
.obs-time { font-size: 30px; font-weight: 750; letter-spacing: -0.02em; line-height: 1.1; margin-top: 2px; }
.obs-meta { font-size: 12.5px; color: var(--muted); margin-top: 1px; }
.obs-meta.warn { color: var(--fail); font-weight: 600; }
.summary { display: flex; flex-wrap: wrap; gap: 6px; list-style: none; margin: 10px 0 0; padding: 0; }
.summary li {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 3px 9px 3px 7px; border-radius: 999px;
  background: var(--cbg); color: var(--text);
  font-size: 13px; font-weight: 700;
}
.summary .dot { width: 9px; height: 9px; border-radius: 50%; background: var(--c); }

.controls { padding: 8px; display: grid; gap: 6px; justify-items: stretch; }
.segmented { display: grid; grid-template-columns: repeat(3, 1fr); background: var(--surface-2); border-radius: 9px; padding: 3px; gap: 2px; }
.segmented button {
  border: 0; background: transparent; border-radius: 7px; padding: 6px 12px;
  font-size: 13px; font-weight: 600; color: var(--muted); cursor: pointer;
}
.segmented button[aria-pressed="true"] { background: var(--surface-solid); color: var(--text); box-shadow: 0 1px 2px rgba(0, 0, 0, 0.15); }
.mode-note { font-size: 11.5px; color: var(--muted); text-align: center; }
.text-btn { border: 0; background: none; color: var(--accent); font-size: 12.5px; font-weight: 600; cursor: pointer; padding: 4px; }
.text-btn:hover { text-decoration: underline; }

.icon-btn {
  display: inline-grid; place-items: center;
  width: 36px; height: 36px; border: 0; border-radius: 10px;
  background: transparent; color: var(--text); cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); }
.icon-btn.small { width: 28px; height: 28px; color: var(--muted); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
#refresh.spinning svg { animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.zoom { position: fixed; right: 12px; bottom: max(16px, env(safe-area-inset-bottom)); display: grid; padding: 4px; gap: 2px; }

/* ---------- Detail panel ---------- */
.panel {
  position: fixed; top: 12px; right: 12px; bottom: 12px;
  width: 380px;
  display: flex; flex-direction: column;
  background: var(--surface-solid);
  border: 1px solid var(--line);
  border-top: 5px solid var(--c);
  border-radius: 16px;
  box-shadow: var(--shadow);
  overflow: hidden;
  z-index: 10;
  animation: slide-in 0.18s ease-out;
}
.panel[hidden] { display: none; }
@keyframes slide-in { from { transform: translateX(16px); opacity: 0; } }
.panel-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; padding: 14px 10px 10px 18px; }
.panel h2 { font-size: 20px; font-weight: 750; letter-spacing: -0.01em; }
.panel h2 .code { font-size: 13px; font-weight: 700; color: var(--muted); letter-spacing: 0.06em; margin-left: 4px; }
.panel .sub { font-size: 12.5px; color: var(--muted); margin-top: 2px; }
.panel-body { overflow-y: auto; padding: 0 18px 18px; overscroll-behavior: contain; }
.verdict { display: grid; gap: 4px; padding: 12px 14px; border-radius: 12px; background: var(--cbg); font-size: 14px; font-weight: 600; }
.verdict small { font-size: 11.5px; color: var(--muted); font-weight: 500; }
.pill { justify-self: start; display: inline-block; padding: 2px 9px; border-radius: 999px; background: var(--c); color: #fff; font-size: 11.5px; font-weight: 800; letter-spacing: 0.04em; text-transform: uppercase; }
:root[data-mode="night"] .pill { color: #0b1218; }

.kpis { display: grid; grid-template-columns: 1fr 1fr; gap: 1px; margin: 14px 0 6px; background: var(--line); border: 1px solid var(--line); border-radius: 12px; overflow: hidden; }
.kpis > div { background: var(--surface-solid); padding: 10px 12px; display: grid; gap: 2px; align-content: start; }
.kpis dt { font-size: 11px; font-weight: 700; letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted); }
.kpis dd { margin: 0; font-size: 19px; font-weight: 700; letter-spacing: -0.01em; }
.kpis .unit { font-size: 13px; color: var(--muted); font-weight: 600; }
.kpis .trend { font-size: 12px; font-weight: 700; margin: 0; }

.panel section { margin-top: 14px; }
.panel h3 { font-size: 11px; font-weight: 700; letter-spacing: 0.08em; text-transform: uppercase; color: var(--muted); margin: 12px 0 4px; display: flex; align-items: center; gap: 8px; }
.chart { width: 100%; height: auto; display: block; overflow: visible; }
.chart .line { fill: none; stroke: var(--text); stroke-width: 1.6; stroke-linejoin: round; }
.chart .pt { fill: var(--surface-solid); stroke: var(--text); stroke-width: 1.2; }
.chart .pt.recent { fill: var(--c); stroke: var(--c); }
.chart .limit { stroke: var(--muted); stroke-width: 1; stroke-dasharray: 2 3; opacity: 0.5; }
.chart .zone.fail { fill: var(--fail-bg); }
.chart .zone.marginal { fill: var(--marginal-bg); }
.chart .pt.over { fill: var(--surface-solid); }
.chart .limit-label { fill: var(--muted); font-size: 9.5px; text-anchor: end; }
.chart .tick { fill: var(--muted); font-size: 9.5px; text-anchor: middle; }
.chart .assessed { fill: none; stroke: var(--c); stroke-width: 1.2; stroke-dasharray: 3 2; }
.empty { font-size: 13px; color: var(--muted); }
.code-block {
  margin: 0; padding: 10px 12px; border-radius: 10px; background: var(--surface-2);
  font-family: var(--mono); font-size: 12.5px; line-height: 1.5; white-space: pre-wrap; word-break: break-word;
}
.code-block.outdated { color: var(--muted); text-decoration: line-through; text-decoration-color: color-mix(in srgb, var(--fail) 60%, transparent); }
.badge { font-size: 10.5px; padding: 1px 7px; border-radius: 999px; letter-spacing: 0.02em; text-transform: none; }
.badge.warn { background: var(--fail-bg); color: var(--fail); }

details.table { margin-top: 16px; border: 1px solid var(--line); border-radius: 12px; }
details.table summary { cursor: pointer; padding: 10px 12px; font-size: 13.5px; font-weight: 650; }
.table-wrap { overflow-x: auto; border-top: 1px solid var(--line); }
details.table table { border-collapse: collapse; font-size: 12px; white-space: nowrap; }
details.table th, details.table td { padding: 4px 8px; text-align: right; border-bottom: 1px solid var(--line); }
details.table thead th { position: sticky; top: 0; background: var(--surface-2); font-weight: 700; }
details.table tbody th { position: sticky; left: 0; background: var(--surface-solid); text-align: left; font-weight: 600; color: var(--muted); }
details.table td:last-child { font-weight: 700; }
.source { margin-top: 14px; font-size: 11.5px; color: var(--muted); }

/* ---------- Legend ---------- */
dialog {
  width: min(560px, calc(100vw - 24px)); max-height: calc(100vh - 48px);
  padding: 0; border: 1px solid var(--line); border-radius: 16px;
  background: var(--surface-solid); color: var(--text); box-shadow: var(--shadow);
}
dialog::backdrop { background: rgba(10, 18, 24, 0.45); }
.legend { padding: 16px 20px 20px; font-size: 14px; line-height: 1.5; }
.legend header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }
.legend h2 { font-size: 18px; font-weight: 750; }
.legend h3 { font-size: 12px; font-weight: 700; letter-spacing: 0.08em; text-transform: uppercase; color: var(--muted); margin: 18px 0 6px; }
.legend p { margin: 6px 0; }
.legend-sample .tag { flex: none; }
.legend-sample { display: flex; gap: 16px; align-items: flex-start; padding: 12px; background: var(--surface-2); border-radius: 12px; }
.legend-sample ol { margin: 0; padding-left: 18px; font-size: 13px; color: var(--muted); }
.legend-status, .legend-trend { list-style: none; padding: 0; margin: 8px 0; display: grid; gap: 6px; }
.legend-status li { display: flex; gap: 10px; align-items: flex-start; }
.swatch { flex: none; display: grid; place-items: center; width: 22px; height: 22px; border-radius: 6px; background: var(--c); color: #fff; font-size: 12px; font-weight: 800; }
.legend-trend { grid-auto-flow: column; justify-content: start; gap: 18px; }
table.limits { border-collapse: collapse; margin: 10px 0; font-size: 13.5px; }
table.limits caption { text-align: left; font-weight: 700; padding-bottom: 4px; }
table.limits th, table.limits td { padding: 4px 18px 4px 0; text-align: left; border-bottom: 1px solid var(--line); }

/* ---------- Small screens: panel becomes a bottom sheet ---------- */
@media (max-width: 759px) {
  #topbar { padding-left: 8px; padding-right: 8px; }
  .brand { flex: 1; min-width: 0; padding: 8px 12px 10px; position: relative; }
  .brand h1 { display: none; }
  .brand .title-row { position: absolute; top: 6px; right: 6px; }
  .obs-time { font-size: 24px; margin: 0; }
  .obs-meta { font-size: 12px; padding-right: 28px; }
  .summary { margin-top: 6px; gap: 4px; }
  .summary li { font-size: 12px; padding: 2px 8px 2px 6px; }
  /* Mode and legend move to the bottom, within thumb reach. */
  .controls {
    position: fixed; left: 8px; bottom: max(16px, env(safe-area-inset-bottom));
    grid-auto-flow: column; align-items: center; padding: 4px; gap: 2px;
  }
  .segmented button { padding: 7px 10px; }
  .mode-note, .wide-only { display: none; }
  .zoom { right: 8px; }
  .panel {
    top: auto; left: 0; right: 0; bottom: 0; width: auto; max-height: 62vh;
    border-radius: 16px 16px 0 0; border-bottom: 0;
    padding-bottom: env(safe-area-inset-bottom);
    animation-name: slide-up;
  }
  .panel:not([hidden]) ~ .zoom { display: none; }
  @keyframes slide-up { from { transform: translateY(24px); opacity: 0; } }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
