:root {
  color-scheme: light;
  --surface-1: #fcfcfb;
  --page-plane: #f9f9f7;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #898781;
  --gridline: #e1e0d9;
  --baseline: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --track: #e1e0d9;
  --fill-good: #0ca30c;
  --required-bar: #898781;
  --accent-tint: #cde2fb;
  --excess-color: #2a78d6;
  /* Map: one categorical colour per region (blue / orange / aqua / violet). The map is always
     white, so these are the light-mode values in BOTH themes (deliberately not overridden under
     dark): that four-colour set passes the palette validator for every pairing on white, which
     the dark-mode values do not at four colours. Marker size, the popups and the legend labels
     carry the same information, so colour is never the only cue. */
  --map-region-1: #2a78d6;
  --map-region-2: #eb6834;
  --map-region-3: #1baf7a;
  --map-region-4: #4a3aa7;
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --surface-1: #1a1a19;
    --page-plane: #0d0d0d;
    --text-primary: #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted: #898781;
    --gridline: #2c2c2a;
    --baseline: #383835;
    --border: rgba(255, 255, 255, 0.10);
    --track: #2c2c2a;
    --fill-good: #0ca30c;
    --required-bar: #6b6a64;
    --accent-tint: #184f95;
    --excess-color: #3987e5;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--page-plane);
  color: var(--text-primary);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  padding-bottom: 48px;
}

.page-header {
  background: var(--surface-1);
  border-bottom: 1px solid var(--border);
}

.page-header-inner {
  max-width: 980px;
  margin: 0 auto;
  padding: 20px 20px;
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  flex-wrap: wrap;
  gap: 8px;
}

.header-side {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 6px;
}

.brand-mark-chip {
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: #ffffff;
  overflow: hidden;
}

.brand-mark {
  display: block;
  width: 34px;
  height: 34px;
  object-fit: cover;
}

h1 {
  font-size: 1.4rem;
  margin: 0 0 2px;
}

.subtitle {
  margin: 0;
  color: var(--text-secondary);
  font-size: 0.9rem;
}

.updated {
  color: var(--text-muted);
  font-size: 0.8rem;
}

main {
  max-width: 980px;
  margin: 0 auto;
  padding: 20px;
}

.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
  align-items: flex-end;
  margin-bottom: 20px;
}

.control-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.control-label {
  font-size: 0.75rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.segmented {
  display: flex;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}

.segmented button {
  border: none;
  background: var(--surface-1);
  color: var(--text-secondary);
  padding: 7px 12px;
  font-size: 0.85rem;
  cursor: pointer;
  border-right: 1px solid var(--border);
}

.segmented button:last-child { border-right: none; }

.segmented button:hover { background: var(--track); }

.segmented button.active {
  background: var(--text-primary);
  color: var(--surface-1);
}

.segmented button:focus-visible,
select:focus-visible,
table a:focus-visible {
  outline: 2px solid var(--fill-good);
  outline-offset: 2px;
}

select#riverSort {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 10px;
  font-size: 0.85rem;
  background: var(--surface-1);
  color: var(--text-primary);
}

.stat-tiles {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 12px;
  margin-bottom: 24px;
}

.stat-tile {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.stat-tile-accent .stat-value { color: var(--fill-good); }
.stat-tile-excess .stat-value { color: var(--excess-color); }

.stat-label {
  font-size: 0.75rem;
  color: var(--text-muted);
}

.stat-value {
  font-size: 1.6rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.panel {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 18px 20px;
  margin-bottom: 20px;
}

.panel h2 {
  font-size: 1rem;
  margin: 0 0 14px;
}

.panel-caption {
  margin: -8px 0 14px;
  color: var(--text-muted);
  font-size: 0.8rem;
}

.chart-wrap { position: relative; }

.table-wrap { overflow-x: auto; }

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.88rem;
}

thead th {
  text-align: left;
  color: var(--text-muted);
  font-weight: 500;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  padding: 6px 10px;
  border-bottom: 1px solid var(--gridline);
}

th.num, td.num { text-align: right; }

td {
  padding: 8px 10px;
  border-bottom: 1px solid var(--gridline);
  font-variant-numeric: tabular-nums;
  vertical-align: middle;
}

tbody tr:last-child td { border-bottom: none; }

/* Region summary: the region matching the Region filter gets a left accent bar
   (not a background, which would swallow the progress-bar track), and the
   all-regions total row sits under a heavier rule. */
tr.is-selected td:first-child {
  box-shadow: inset 3px 0 0 var(--fill-good);
  font-weight: 600;
}

tr.total-row td {
  font-weight: 600;
  border-top: 1px solid var(--baseline);
  border-bottom: none;
}

.bar-col { width: 180px; }

.bar-track {
  background: var(--track);
  border-radius: 4px;
  height: 10px;
  width: 100%;
  overflow: hidden;
}

.bar-fill {
  background: var(--fill-good);
  height: 100%;
  border-radius: 4px 0 0 4px;
}

.loading-row, .empty-row {
  text-align: center;
  color: var(--text-muted);
  padding: 24px 0;
}

.footnote {
  color: var(--text-muted);
  font-size: 0.8rem;
  line-height: 1.5;
  max-width: 760px;
}

.footnote p {
  margin: 0 0 8px;
}

.footnote p:last-child {
  margin-bottom: 0;
}

.footnote code {
  background: var(--track);
  padding: 1px 5px;
  border-radius: 4px;
  font-size: 0.78rem;
}

/* Year-by-year detail: a river row opens to show its years. */
.river-row { cursor: pointer; }
.inst-cell { white-space: nowrap; }
.river-row:hover td { background: var(--page-plane); }

.row-toggle {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  margin: 0;
  padding: 0;
  border: none;
  background: none;
  font: inherit;
  color: inherit;
  text-align: left;
  cursor: pointer;
}

.row-toggle:focus-visible {
  outline: 2px solid var(--fill-good);
  outline-offset: 3px;
  border-radius: 2px;
}

.chev {
  flex-shrink: 0;
  width: 0;
  height: 0;
  border-top: 4px solid transparent;
  border-bottom: 4px solid transparent;
  border-left: 6px solid var(--text-muted);
  transition: transform 0.15s;
}

.row-toggle[aria-expanded="true"] .chev { transform: rotate(90deg); }

.detail-row > td {
  padding: 4px 10px 14px 24px;
  background: var(--page-plane);
}

/* Compact rows so a 10-year window fits without an inner scrollbar; only long
   "All years" lists scroll inside this box. */
.year-table-wrap {
  max-height: 480px;
  overflow: auto;
}

.year-table { font-size: 0.82rem; }

.year-table td { padding: 5px 10px; }

.year-table thead th {
  position: sticky;
  top: 0;
  background: var(--page-plane);
}

.year-table .no-data td { color: var(--text-muted); }

@media (prefers-reduced-motion: reduce) {
  .chev { transition: none; }
}

.river-map {
  height: 460px;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  /* The tiles only draw land (the sea is transparent), so this is the map's white
     background in both light and dark mode. */
  background: #ffffff;
}

.river-map.leaflet-container {
  font: inherit;
}

.leaflet-popup-content-wrapper,
.leaflet-popup-tip {
  background: var(--surface-1);
  color: var(--text-primary);
}

.leaflet-popup-content {
  margin: 10px 14px;
  font-size: 0.85rem;
  line-height: 1.5;
}

.river-map .leaflet-bar a,
.river-map .leaflet-bar a:hover {
  background-color: var(--surface-1);
  color: var(--text-primary);
  border-bottom-color: var(--border);
}

.river-map .leaflet-control-attribution {
  background: var(--surface-1);
  color: var(--text-secondary);
}

.river-map .leaflet-control-attribution a {
  color: inherit;
  text-decoration: underline;
}

.map-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 18px;
  margin-top: 10px;
  font-size: 0.8rem;
  color: var(--text-secondary);
}

.legend-item {
  display: inline-flex;
  align-items: center;
}

.legend-dot {
  display: inline-block;
  width: 11px;
  height: 11px;
  border-radius: 50%;
  margin-right: 6px;
  /* The dot colours are chosen for the white map; the ring keeps them visible on a dark page. */
  box-shadow: 0 0 0 1.5px #ffffff;
}

.legend-note { color: var(--text-muted); }

@media (max-width: 720px) {
  .river-map { height: 340px; }
  .bar-col { width: 100px; }
}
