* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: #1f2933;
  background: #f5f7fa;
}
/* Header, then the sidebar + map filling the rest (the header can wrap to two lines when narrow) */
body { display: flex; flex-direction: column; }

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  background: #1d3557;
  color: #fff;
}
header h1 { margin: 0; font-size: 1.2rem; }
.toggle { font-size: 0.9rem; cursor: pointer; }
.header-controls { display: flex; align-items: center; gap: 16px; flex-wrap: wrap; justify-content: flex-end; }
.toggle select { font: inherit; font-size: 0.85rem; padding: 1px 4px; border-radius: 3px; border: none; }

main {
  display: flex;
  flex: 1;
  min-height: 0;
}

#sidebar {
  width: 360px;
  overflow-y: auto;
  padding: 12px 16px;
  background: #fff;
  border-right: 1px solid #d9e2ec;
}
#sidebar h2 { font-size: 1rem; margin: 4px 0 8px; }

#riverSearch {
  width: 100%;
  margin: 0 0 8px;
  padding: 6px 8px;
  font: inherit;
  font-size: 0.9rem;
  border: 1px solid #d9e2ec;
  border-radius: 4px;
}
#riverSearch:focus { outline: 2px solid #1d3557; outline-offset: -1px; }

/* CFS range filter */
#cfsFilter, #pctFilter, #lpFilter, #peakFilter, #lowFilter, #basinFilter {
  margin: 0 0 10px;
  padding: 6px 10px 8px;
  border: 1px solid #d9e2ec;
  border-radius: 4px;
}
#pctFilter:disabled, #lpFilter:disabled { opacity: 0.5; }
#lpFilter .filter-note { margin: 4px 0 0; font-size: 0.72rem; line-height: 1.3; color: #627d98; }
/* Basin checkboxes, two columns */
#basinChecks { display: grid; grid-template-columns: 1fr 1fr; gap: 2px 10px; margin-bottom: 2px; }
#basinChecks label { display: flex; align-items: center; gap: 6px; font-size: 0.85rem; cursor: pointer; padding: 2px 0; }
#basinChecks .swatch { width: 14px; height: 4px; border-radius: 2px; flex: none; }
#cfsFilter legend, #pctFilter legend, #lpFilter legend, #peakFilter legend, #lowFilter legend, #basinFilter legend { font-size: 0.8rem; font-weight: 600; color: #486581; padding: 0 4px; }
#cfsRange, .range-filter .range { display: flex; align-items: center; gap: 6px; color: #829ab1; }
#cfsRange input, .range-filter .range input {
  width: 70px;
  padding: 4px 6px;
  font: inherit; font-size: 0.85rem;
  border: 1px solid #d9e2ec; border-radius: 4px;
}
#cfsRange input:focus, .range-filter .range input:focus { outline: 2px solid #1d3557; outline-offset: -1px; }
.link-btn {
  margin-top: 4px; padding: 0; border: none; background: none;
  font: inherit; font-size: 0.75rem; color: #1d3557; text-decoration: underline; cursor: pointer;
}

#riverList { list-style: none; margin: 0; padding: 0; }
#riverList li {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 4px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.9rem;
}
#riverList li:hover { background: #eef2f7; }
/* The river or section shown alone on the map */
#riverList li.selected { background: #dce8f7; font-weight: 600; }
#riverList li.basin-header {
  margin-top: 10px; padding: 6px 4px 5px; border-bottom: 1px solid #d9e2ec; border-radius: 0;
  font-size: 0.9rem; font-weight: 600; letter-spacing: 0.05em; text-transform: uppercase;
  color: #334e68; cursor: pointer; user-select: none;
}
#riverList li.basin-header::after {
  content: "▾"; margin-left: auto; color: #829ab1; font-size: 0.8rem; width: 10px; flex: none;
}
#riverList li.basin-header.collapsed::after { content: "▸"; }
#riverList li.basin-header:first-child { margin-top: 0; }
#riverList li.basin-header:hover { background: #eef2f7; }
#riverList .swatch {
  width: 14px; height: 4px; border-radius: 2px; flex: none;
}
#riverList .flow { margin-left: auto; color: #829ab1; font-size: 0.8rem; white-space: nowrap; }
/* Percent of median, after the flow; fixed width so the columns line up */
#riverList .pct.doubtful { color: #9aa5b1; font-style: italic; }
#riverList .pct { flex: 0 1 56px; min-width: max-content; text-align: right; color: #486581; font-size: 0.75rem; white-space: nowrap; }

/* Per-gauge sections under a river with several gauges (collapsed until clicked) */
#riverList li[hidden] { display: none; }
#riverList li.has-sections::after {
  content: "▸"; color: #829ab1; font-size: 0.75rem; width: 10px; flex: none;
}
#riverList li.has-sections.expanded::after { content: "▾"; }
#riverList li.section-row { padding: 3px 4px 3px 26px; font-size: 0.8rem; color: #486581; }
#riverList .section-dot {
  width: 8px; height: 8px; border: 2px solid; border-radius: 50%; background: #fff; flex: none;
}
.popup .section-name { color: #486581; font-style: italic; }

.note { font-size: 0.8rem; color: #627d98; margin-top: 16px; }
.note.ok { color: #2f6f3e; }
.note.warn { color: #9a5b00; }

.popup-flow { font-size: 1.1rem !important; font-weight: 700; color: #1d3557; }
.stale {
  font-size: 0.7rem; font-weight: 600; color: #9a5b00;
  background: #fff3d6; padding: 1px 5px; border-radius: 3px;
}
#riverList .flow.stale-text { color: #c08a2c; }

#map { flex: 1; }

/* River name labels on the map */
/* Flow on each section while one river is shown alone */
.cfs-label {
  background: #1d3557; color: #fff; border: none; border-radius: 3px;
  font-size: 11px; font-weight: 700; padding: 1px 5px; box-shadow: 0 1px 2px rgba(16, 42, 67, 0.3);
}
.cfs-label.leaflet-tooltip-top::before { border-top-color: #1d3557; }

.river-label {
  background: rgba(255, 255, 255, 0.85);
  border: none;
  box-shadow: none;
  font-size: 11px;
  font-weight: 600;
  color: #1d3557;
  padding: 1px 4px;
}
.river-label::before { display: none; }
/* River name + flow following the mouse over a line */
.hover-name {
  background: #fff; border: 1px solid #bcccdc; border-radius: 4px; box-shadow: 0 1px 3px rgba(16, 42, 67, 0.25);
  font-size: 12px; font-weight: 600; color: #1d3557; padding: 2px 7px; white-space: nowrap;
}
/* Shown only on phones (see the phone styles below) */
.phone-only { display: none; }

.popup h3 { margin: 0 0 4px; font-size: 1rem; }
.popup p { margin: 2px 0; font-size: 0.85rem; }

/* Basin legend folds to its title when its title is clicked */
.basin-legend .legend-title { cursor: pointer; user-select: none; }
.basin-legend .legend-title::after { content: " ▾"; color: #829ab1; }
.basin-legend.folded .legend-title::after { content: " ▸"; }
.basin-legend.folded .legend-body { display: none; }
.basin-legend .legend-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.basin-legend .legend-close { display: none; }
/* Phones (upright, or sideways and short): a clear ✕ to fold the open legend, and the folded legend
   looks like a button */
@media (max-width: 700px), (orientation: landscape) and (max-height: 500px) {
  .basin-legend:not(.folded) .legend-close {
    display: block; flex: none; width: 34px; height: 34px; margin: -2px -6px 0 0; padding: 0;
    border: 1px solid #bcccdc; border-radius: 4px; background: #f0f4f8; color: #334e68;
    font-size: 16px; line-height: 32px; cursor: pointer;
  }
  .basin-legend.folded .legend-title { display: inline-block; padding: 6px 2px; font-size: 13px; }
}

/* "Showing only ..." bar above the river list, and the links at the foot of each popup */
.focus-chip {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  margin: 0 0 8px; padding: 6px 10px; border-radius: 4px;
  background: #e8f0fb; color: #1d3557; font-size: 0.85rem;
}
.focus-chip .link-btn { margin: 0; white-space: nowrap; }
.focus-chip[hidden] { display: none; } /* display: flex above would otherwise keep it showing */
.popup .popup-links { margin-top: 6px; font-size: 0.8rem; }

/* Rising / falling arrows (sidebar and popups) */
.trend.up { color: #2b6cb0; }
.trend.down { color: #c05621; }
.trend.flat { color: #829ab1; }
#riverList .flow .trend { font-size: 0.7rem; }
.popup .trend-line { font-size: 0.8rem; color: #486581; }

/* 7-day chart in popups */
.flow-chart { margin: 6px 0 4px; }
.flow-chart svg { display: block; }
.chart-line { fill: none; stroke: #1d3557; stroke-width: 1.8; stroke-linejoin: round; }
.chart-median { stroke: #829ab1; stroke-width: 1; stroke-dasharray: 4 3; }
.chart-label { font-size: 9px; fill: #829ab1; }
.popup .chart-note { margin: 0; font-size: 0.75rem; color: #627d98; }

/* Legend: colour by Basin / Median (percent of median) / Low → peak */
.legend-switch { display: flex; margin: 4px 0 6px; }
.legend-switch button {
  flex: 1; font: inherit; font-size: 11px; padding: 2px 8px; cursor: pointer;
  border: 1px solid #bcccdc; background: #fff; color: #334e68;
}
.legend-switch button:first-child { border-radius: 3px 0 0 3px; }
.legend-switch button:last-child { border-radius: 0 3px 3px 0; }
.legend-switch button:not(:first-child) { border-left: none; }
.legend-switch button { white-space: nowrap; }
.legend-switch button.active { background: #1d3557; border-color: #1d3557; color: #fff; }
.legend-switch button:disabled { opacity: 0.45; cursor: not-allowed; }

/* Share button at the foot of popups */
.popup .share-btn {
  font: inherit; font-size: 0.8rem; font-weight: 600; padding: 4px 12px; cursor: pointer;
  border: 1px solid #1d3557; border-radius: 4px; background: #1d3557; color: #fff;
  box-shadow: 0 1px 2px rgba(16, 42, 67, 0.25);
}
.popup .share-btn::before { content: "↗ "; }
.popup .share-btn:hover { background: #2a4a75; border-color: #2a4a75; }
.popup .share-row { display: flex; gap: 6px; flex-wrap: wrap; }

/* Share icon on sidebar rows: always takes its space (so columns line up) but only shows on the
   selected row, on hover (mouse) and while the row has keyboard focus */
#riverList .row-share {
  flex: none; width: 22px; height: 22px; padding: 0; margin-left: 2px;
  font: inherit; font-size: 0.8rem; font-weight: 700; line-height: 20px; cursor: pointer;
  border: 1px solid #1d3557; border-radius: 4px; background: #1d3557; color: #fff;
  visibility: hidden;
}
#riverList li.selected .row-share, #riverList li:focus-within .row-share { visibility: visible; }
@media (hover: hover) { #riverList li:hover .row-share { visibility: visible; } }
#riverList .row-share:hover { background: #2a4a75; }

/* Favourite star on river rows: ★ always shows on favourites; ☆ shows like the share icon */
#riverList .row-star {
  flex: none; width: 22px; height: 22px; padding: 0; margin-left: 2px;
  font: inherit; font-size: 0.95rem; line-height: 20px; cursor: pointer;
  border: none; background: none; color: #829ab1; visibility: hidden;
}
#riverList .row-star.on { color: #d69e2e; visibility: visible; }
#riverList li.selected .row-star, #riverList li:focus-within .row-star { visibility: visible; }
@media (hover: hover) { #riverList li:hover .row-star { visibility: visible; } }
#riverList .row-star:hover { color: #d69e2e; }
/* The Favourites group at the top of the list */
#riverList li.fav-header .fav-mark { color: #d69e2e; margin-right: 6px; }
/* Popup button */
.popup .fav-toggle {
  font: inherit; font-size: 0.8rem; font-weight: 600; padding: 4px 10px; cursor: pointer;
  border: 1px solid #bcccdc; border-radius: 4px; background: #fff; color: #334e68;
}
.popup .fav-toggle.on { border-color: #d69e2e; color: #975a16; background: #fffaf0; }
.popup .fav-toggle:hover { background: #f5f7fa; }
/* Long names wrap rather than pushing the flow, percent and share columns off the row */
/* Names take the room left over and wrap between words; a word only breaks when it's wider than
   all the room the row has left (a long name deep in the tree). Flow and percent never wrap. */
#riverList li > span:nth-child(2) { flex: 1 1 auto; min-width: 0; overflow-wrap: break-word; }
@media (max-width: 700px) { #riverList .pct { flex-basis: 48px; } }

/* Keyboard focus on sidebar rows, the legend title and other controls */
#riverList li:focus-visible, .legend-title:focus-visible, .share-btn:focus-visible,
.legend-switch button:focus-visible, .link-btn:focus-visible, #filters > summary:focus-visible {
  outline: 2px solid #1d3557; outline-offset: -2px;
}

/* Map info box: selection + readings time, shown only on phones in Map only mode */
.map-info {
  display: none; max-width: 230px; padding: 6px 10px; border-radius: 4px;
  background: rgba(255, 255, 255, 0.95); box-shadow: 0 1px 4px rgba(16, 42, 67, 0.3);
  font-size: 12px; color: #334e68; line-height: 1.4;
}
.map-info .map-focus, .map-info .map-query { display: flex; flex-wrap: wrap; align-items: baseline; gap: 2px 8px;
  padding-bottom: 4px; margin-bottom: 4px; border-bottom: 1px solid #d9e2ec; color: #1d3557; }
.map-info .map-focus[hidden], .map-info .map-query[hidden] { display: none; }
.map-info .map-focus .link-btn, .map-info .map-query .link-btn { margin: 0; padding: 6px 0; font-size: 12px; font-weight: 600; }
.map-info .map-updated { color: #627d98; }
.map-info .map-updated.stale-time { color: #9a5b00; font-weight: 600; }

/* Header link to the About page */
.about-link { color: #fff; font-weight: 600; text-decoration: none; }
.about-link:hover, .about-link:focus-visible { text-decoration: underline; }

.mobile-only { display: none; }
/* Filters fold behind a "Filters" button (bigger on phones, below) */
#filters > summary {
  display: list-item; cursor: pointer; padding: 6px 2px; margin-bottom: 4px;
  font-size: 0.9rem; font-weight: 600; color: #334e68;
}

@media (max-width: 700px) {
  main { flex-direction: column; }
  /* A short strip: search, Filters and one row of the list (scroll for more), so the map gets the
     rest; it grows while the filters are open */
  #sidebar { width: 100%; height: 136px; flex: none; border-right: none; border-bottom: 1px solid #d9e2ec; padding: 8px 12px; }
  #sidebar:has(#filters[open]) { height: 45%; }
  #map { height: auto; flex: 1; }

  /* One-row header: the title is left to the browser tab, controls are big enough to tap */
  header { padding: 4px 10px; justify-content: flex-end; }
  header h1, .hide-on-phone { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
  .header-controls { gap: 12px; flex-wrap: nowrap; }
  .toggle { display: inline-flex; align-items: center; gap: 6px; min-height: 44px; white-space: nowrap; }
  .toggle input[type="checkbox"] { width: 20px; height: 20px; margin: 0; }
  .toggle select { min-height: 36px; padding: 4px 6px; }

  /* 16 px text in boxes: iPhones zoom the page in when you tap a box with smaller text */
  input, select, #riverSearch, #cfsRange input, .range-filter .range input, .toggle select { font-size: 16px; }
  #basinChecks label { min-height: 36px; }
  #basinChecks input { width: 18px; height: 18px; margin: 0; }

  #sidebar h2 { display: none; }
  #filters > summary {
    display: list-item; cursor: pointer; padding: 10px 4px; min-height: 44px; box-sizing: border-box;
    font-size: 0.9rem; font-weight: 600; color: #334e68;
  }
  .link-btn { padding: 8px 0; font-size: 0.85rem; }
  /* Filter boxes: Clear on the same row as the Min/Max boxes, so more of the filters fit on screen */
  #cfsFilter, .range-filter { position: relative; }
  #cfsFilter > .link-btn, .range-filter > .link-btn { position: absolute; right: 10px; top: 9px; margin: 0; }

  /* Bigger map buttons, and popups that scroll instead of covering the map */
  .leaflet-touch .leaflet-bar a { width: 44px; height: 44px; line-height: 44px; }
  .leaflet-container a.leaflet-popup-close-button { width: 36px; height: 36px; font: 26px/36px Tahoma, Verdana, sans-serif; }
  .leaflet-popup-content { max-height: 40vh; overflow-y: auto; margin-right: 28px; }
  /* With the sidebar showing the map is about half the screen, so popups get shorter */
  body:not(.map-only) .leaflet-popup-content { max-height: 24vh; }
  .mobile-only { display: inline; }
  .phone-only { display: inline; }
  /* "Line width" as a small caption above its menu, so it takes no extra width */
  .width-toggle { flex-direction: column; align-items: flex-start; gap: 0; }
  .width-toggle .phone-only { font-size: 10px; line-height: 1.1; opacity: 0.85; }
  .width-toggle select { min-height: 30px; padding: 2px 6px; }
  /* "Map only": the map takes the whole screen below the header */
  body.map-only #sidebar { display: none; }
  body.map-only .map-info { display: block; }
  body.map-only #map { height: auto; flex: 1; }
}

/* Caveat shown under a gauge's details (e.g. a rough calculated flow) */
.popup .popup-note { color: #9a5b00; font-size: 0.8rem; font-style: italic; }

/* ---------- Phones held sideways (short, wide screens): the river list sits beside the map, as on
   a computer, and folds away to the side with the ◀ / ▶ Rivers button. "Map only" isn't used here. */
.sidebar-fold { display: none; }
@media (orientation: landscape) and (max-height: 500px) {
  main { flex-direction: row; position: relative; }
  #sidebar, body.map-only #sidebar {
    display: block; width: 280px; height: auto; flex: none;
    border-right: 1px solid #d9e2ec; border-bottom: none; padding: 8px 12px;
  }
  #sidebar:has(#filters[open]) { height: auto; }
  #map, body.map-only #map { height: auto; flex: 1; }
  body.sidebar-folded #sidebar { display: none; }
  .mobile-only { display: none; }
  /* The info box (selection, readings time) and the legend follow the fold, not "Map only" */
  body.map-only .basin-legend { display: block; }
  .map-info, body.map-only .map-info { display: none; }
  body.sidebar-folded .map-info { display: block; }

  .sidebar-fold {
    display: block; position: absolute; z-index: 1001; top: 8px; left: 280px;
    min-width: 32px; min-height: 40px; padding: 0 8px;
    font: inherit; font-size: 0.85rem; font-weight: 600; cursor: pointer;
    background: #fff; color: #1d3557; border: 1px solid #bcccdc; border-left: none;
    border-radius: 0 6px 6px 0; box-shadow: 1px 1px 3px rgba(16, 42, 67, 0.2);
  }
  body.sidebar-folded .sidebar-fold { left: 0; }
  .sidebar-fold:focus-visible { outline: 2px solid #1d3557; outline-offset: -2px; }
  /* Keep the zoom buttons clear of the fold button */
  body:not(.sidebar-folded) .leaflet-top.leaflet-left { margin-left: 44px; }
  body.sidebar-folded .leaflet-top.leaflet-left { margin-top: 48px; }
}

/* Age of an estimate that describes hours ago (sidebar) */
#riverList .flow .est-age { color: #9a5b00; font-size: 0.7rem; }
/* Calculated flows: the formula folds away in the popup */
.popup .calc-how { margin: 4px 0; font-size: 0.85rem; }
.popup .calc-how summary { cursor: pointer; color: #1d3557; font-weight: 600; padding: 2px 0; }
.popup .calc-how p { margin: 4px 0 2px; }

/* Legend note while line width shows median flow (lines use basin colours) */
.legend-median-note { margin: 0 0 6px; max-width: 190px; font-size: 11px; line-height: 1.35; color: #486581; }

/* Search on the map: only when the sidebar (and its search box) is hidden */
.map-search { display: none; }
.map-search-open {
  display: block; width: 44px; height: 44px; padding: 0; border: none; border-radius: 4px;
  background: #fff; font-size: 18px; cursor: pointer;
}
.map-search.open .map-search-open { display: none; }
.map-search-bar[hidden] { display: none; }
.map-search-bar { display: flex; align-items: center; gap: 4px; background: #fff; border-radius: 4px; padding: 4px; }
.map-search-input {
  width: min(260px, calc(100vw - 110px)); font: inherit; font-size: 16px; padding: 8px;
  border: 1px solid #bcccdc; border-radius: 4px;
}
.map-search-close {
  width: 40px; height: 40px; flex: none; border: none; background: none; font-size: 18px; cursor: pointer; color: #334e68;
}
@media (max-width: 700px) {
  body.map-only .map-search { display: block; }
}
@media (orientation: landscape) and (max-height: 500px) {
  .map-search, body.map-only .map-search { display: none; }
  body.sidebar-folded .map-search { display: block; }
}
