:root {
  color-scheme: light;
  --surface-0: #f4f3f0;
  --surface-1: #fcfcfb;
  --surface-2: #f0efec;
  --border: #e2e1dc;
  --grid: #e8e7e3;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #7a7974;
  --accent: #2a78d6;
  --water: #2a78d6;
  --dam: #eb6834;
  --ground: #a8a69f;
  --warn-bg: #fdf3dc;
  --warn-ink: #6b4a00;
  --err-bg: #fbe4e3;
  --err-ink: #8a1c1b;
  --panel-w: 400px;
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --surface-0: #121211;
    --surface-1: #1a1a19;
    --surface-2: #262624;
    --border: #383835;
    --grid: #2e2e2c;
    --text-primary: #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted: #8f8e86;
    --accent: #3987e5;
    --water: #3987e5;
    --dam: #d95926;
    --ground: #6f6e68;
    --warn-bg: #3a2f12;
    --warn-ink: #f3d58a;
    --err-bg: #3d1a1a;
    --err-ink: #f4b4b3;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body { background: var(--surface-0); color: var(--text-primary); }

#map { position: fixed; inset: 0 var(--panel-w) 0 0; }

#panel {
  position: fixed; top: 0; right: 0; bottom: 0; width: var(--panel-w);
  overflow-y: auto; background: var(--surface-1);
  border-left: 1px solid var(--border);
  padding: 0 18px 32px;
}
#panel header {
  position: sticky; top: 0; z-index: 2; background: var(--surface-1);
  display: flex; align-items: center; gap: 8px;
  padding: 14px 0 10px; border-bottom: 1px solid var(--border); margin-bottom: 12px;
}
#panel header .units { margin-left: auto; }
.panel-toggle {
  flex: none; display: grid; place-items: center; width: 28px; height: 28px; padding: 0;
  border: 1px solid var(--border); border-radius: 6px; background: transparent;
  color: var(--text-secondary); cursor: pointer;
}
.panel-toggle:hover { background: var(--surface-2); color: var(--text-primary); }
.panel-toggle svg { transition: transform .15s; }
/* Chevron points the way the panel will move: right to collapse, left to expand. */
body.collapsed .panel-toggle svg { transform: rotate(180deg); }
body.collapsed #panel { overflow: hidden; }
body.collapsed #panel > :not(header) { display: none; }
body.collapsed #panel header { border-bottom: 0; margin-bottom: 0; }
@media (min-width: 761px) {
  /* Collapsed on desktop: a narrow strip holding only the toggle. */
  body.collapsed { --panel-w: 48px; }
  body.collapsed #panel { padding: 0 9px; }
  body.collapsed #panel h1, body.collapsed .units { display: none; }
}
h1 { font-size: 17px; margin: 0; font-weight: 650; }
h2 {
  font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em;
  color: var(--text-secondary); margin: 22px 0 8px;
}
p, ol { margin: 0 0 10px; }
ol { padding-left: 20px; }
.muted { color: var(--text-muted); }
.small { font-size: 12px; }

.units { display: flex; border: 1px solid var(--border); border-radius: 6px; overflow: hidden; }
.units button {
  border: 0; background: transparent; color: var(--text-secondary);
  padding: 4px 10px; font: inherit; font-size: 12px; cursor: pointer;
}
.units button.on { background: var(--surface-2); color: var(--text-primary); font-weight: 600; }

.btn {
  font: inherit; font-size: 13px; padding: 7px 12px; border-radius: 6px; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface-1); color: var(--text-primary);
}
.btn:hover:not(:disabled) { background: var(--surface-2); }
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); background: var(--accent); }
.btn.primary.active { background: var(--text-primary); border-color: var(--text-primary); color: var(--surface-1); }
.intro-draw { display: block; width: 100%; margin: 4px 0 14px; text-align: center; }
.btn.small { font-size: 12px; padding: 5px 9px; }

.map-tools {
  position: fixed; top: 12px; left: 56px; z-index: 1000;
  display: flex; flex-direction: column; gap: 8px; max-width: min(360px, calc(100vw - var(--panel-w) - 120px));
}
.map-tools .btn { box-shadow: 0 1px 4px rgba(0,0,0,.2); }
.btn-row { display: flex; gap: 8px; }
.search input {
  width: 100%; font: inherit; padding: 7px 10px; border-radius: 6px;
  border: 1px solid var(--border); background: var(--surface-1); color: var(--text-primary);
  box-shadow: 0 1px 4px rgba(0,0,0,.2);
}

.depth-legend {
  width: 180px; background: var(--surface-1); color: var(--text-primary);
  border-radius: 6px; padding: 8px 10px; box-shadow: 0 1px 4px rgba(0,0,0,.2);
  font: 12px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
}
.dl-title { color: var(--text-secondary); margin-bottom: 4px; }
.dl-bar {
  height: 10px; border-radius: 2px;
  background: linear-gradient(90deg, #cde2fb, #86b6ef, #3987e5, #256abf, #184f95, #0d366b);
}
.dl-ticks { display: flex; justify-content: space-between; color: var(--text-secondary); margin-top: 2px; font-variant-numeric: tabular-nums; }

.field { margin-bottom: 10px; }
.field label { display: block; font-size: 12px; color: var(--text-secondary); margin-bottom: 4px; }
.field.btns { display: flex; gap: 6px; flex-wrap: wrap; }
.crest-row, .inline { display: flex; align-items: center; gap: 8px; }
.crest-row input[type=range] { flex: 1; min-width: 0; accent-color: var(--accent); }
input[type=number] {
  width: 88px; font: inherit; padding: 5px 7px; border-radius: 5px;
  border: 1px solid var(--border); background: var(--surface-1); color: var(--text-primary);
  font-variant-numeric: tabular-nums;
}
.unit { color: var(--text-muted); font-size: 12px; min-width: 14px; }
select {
  font: inherit; padding: 4px 6px; border-radius: 5px;
  border: 1px solid var(--border); background: var(--surface-1); color: var(--text-primary);
}
.freeboard { margin-bottom: 10px; font-size: 13px; }
.freeboard summary { cursor: pointer; color: var(--text-secondary); }
.freeboard summary b { font-weight: 600; color: var(--text-primary); }
.freeboard .field { margin: 8px 0 0; }
.stepper {
  display: inline-flex; border: 1px solid var(--border); border-radius: 5px; overflow: hidden;
  background: var(--surface-1);
}
.stepper input {
  width: 66px; font: inherit; padding: 5px 7px; border: 0; background: transparent;
  color: var(--text-primary); font-variant-numeric: tabular-nums;
}
.stepper .spin { display: flex; flex-direction: column; border-left: 1px solid var(--border); }
.stepper .spin button {
  flex: 1; display: grid; place-items: center; width: 22px; padding: 0; border: 0;
  background: transparent; color: var(--text-secondary); cursor: pointer;
}
.stepper .spin button + button { border-top: 1px solid var(--border); }
.stepper .spin button:hover { background: var(--surface-2); color: var(--text-primary); }
.freeboard p { margin: 6px 0 0; }
.check { display: flex; gap: 6px; align-items: center; font-size: 13px; color: var(--text-secondary); cursor: pointer; }

.status {
  margin: 12px 0; padding: 9px 12px; border-radius: 6px; background: var(--surface-2);
  color: var(--text-secondary); display: flex; gap: 10px; align-items: center;
}
.status::before {
  content: ""; width: 14px; height: 14px; border-radius: 50%; flex: none;
  border: 2px solid var(--border); border-top-color: var(--accent);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.error { margin: 12px 0; padding: 9px 12px; border-radius: 6px; background: var(--err-bg); color: var(--err-ink); }
.warnings { margin: 12px 0; display: flex; flex-direction: column; gap: 6px; }
.warnings div { padding: 8px 12px; border-radius: 6px; background: var(--warn-bg); color: var(--warn-ink); font-size: 13px; }
.warnings div::before { content: "⚠ "; }


.stats { display: grid; grid-template-columns: 1fr minmax(auto, 55%); margin: 0; }
.stats dt, .stats dd { padding: 5px 0; border-bottom: 1px solid var(--grid); margin: 0; }
.stats dt { color: var(--text-secondary); }
.stats dd { text-align: right; font-variant-numeric: tabular-nums; font-weight: 500; }
.stats dd .sub { display: block; font-size: 11px; color: var(--text-muted); font-weight: 400; }
.stats dd .sub a { color: var(--accent); }
/* The tip is placed from the left edge of the label, not the icon, so it stays in the panel. */
.stats dt.has-info { display: flex; align-items: center; gap: 5px; position: relative; }
.info {
  flex: none; width: 16px; height: 16px; padding: 0;
  border: 0; border-radius: 50%; background: transparent; color: var(--text-muted); cursor: help;
}
.info svg { display: block; width: 100%; height: 100%; fill: currentColor; }
.info:hover, .info:focus-visible { color: var(--text-secondary); }
.info::after {
  content: attr(data-tip); position: absolute; z-index: 10; top: calc(100% - 2px); left: 0; width: max-content; max-width: min(280px, calc(100vw - 32px));
  background: var(--surface-1); color: var(--text-primary); border: 1px solid var(--border);
  border-radius: 6px; padding: 6px 9px; box-shadow: 0 2px 8px rgba(0,0,0,.18);
  font: 12px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif; text-align: left;
  visibility: hidden; opacity: 0; transition: opacity .12s;
}
.info:hover::after, .info:focus::after { visibility: visible; opacity: 1; }

.chart { margin: 14px 0 0; }
.chart figcaption { font-size: 12px; color: var(--text-secondary); margin-bottom: 4px; }
.chart-box { width: 100%; }
.chart-box svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart text { fill: var(--text-muted); font-size: 10px; font-variant-numeric: tabular-nums; }
.chart .gridline { stroke: var(--grid); stroke-width: 1; }
.chart .axis { stroke: var(--border); stroke-width: 1; }
.chart .ground { fill: var(--ground); fill-opacity: .35; stroke: var(--ground); stroke-width: 1.5; }
.chart .damfill { fill: var(--dam); }
.chart .waterfill { fill: var(--water); fill-opacity: .12; }
.chart .waterline, .chart .series { stroke: var(--water); stroke-width: 2; fill: none; stroke-linejoin: round; stroke-linecap: round; }
.chart .series-area { fill: var(--water); fill-opacity: .1; }
.chart .bar { fill: var(--water); }
.chart .bar.hover { fill-opacity: .75; }
.chart .marker { fill: var(--water); stroke: var(--surface-1); stroke-width: 2; }
.chart .crosshair { stroke: var(--text-muted); stroke-width: 1; }

.tooltip {
  position: fixed; z-index: 3000; pointer-events: none;
  background: var(--surface-1); color: var(--text-primary); border: 1px solid var(--border);
  border-radius: 6px; padding: 6px 9px; font-size: 12px; box-shadow: 0 2px 8px rgba(0,0,0,.18);
  font-variant-numeric: tabular-nums; white-space: nowrap;
}
.tooltip .t-head { color: var(--text-secondary); margin-bottom: 2px; }
.tooltip .t-row { display: flex; gap: 8px; align-items: center; }
.tooltip .key { width: 10px; height: 3px; border-radius: 1px; display: inline-block; }

.method { margin-top: 22px; font-size: 13px; color: var(--text-secondary); }
.method summary { cursor: pointer; font-weight: 600; }
.method + .method { margin-top: 10px; }
.method ul { padding-left: 18px; }
.method li { margin: 4px 0; }
.disclaimer { margin-top: 14px; }

.vertex-icon {
  width: 12px; height: 12px; border-radius: 50%;
  background: #fff; border: 2px solid #c2410c; box-shadow: 0 0 0 1px rgba(0,0,0,.25);
}
.spill-icon {
  width: 16px; height: 16px; border-radius: 50%; background: #e34948;
  border: 2px solid #fff; box-shadow: 0 0 0 1px rgba(0,0,0,.35);
}
.leaflet-container { background: var(--surface-2); }
.leaflet-container.drawing { cursor: crosshair; }

@media (max-width: 760px) {
  :root { --panel-w: 0px; }
  #map { inset: 0 0 45vh 0; }
  #panel { top: 55vh; width: 100%; border-left: 0; border-top: 1px solid var(--border); padding: 0 16px 24px; }
  .map-tools { left: 52px; right: 60px; max-width: none; }
  .leaflet-bottom.leaflet-left { bottom: var(--attr-h, 0px); }
  /* Collapsed on mobile: only the header row stays, and the map takes the freed height. */
  .panel-toggle svg { transform: rotate(90deg); }
  body.collapsed { --panel-w: 0px; }
  body.collapsed .panel-toggle svg { transform: rotate(-90deg); }
  body.collapsed #map { bottom: var(--panel-h, 54px); }
  body.collapsed #panel { top: auto; padding-bottom: 0; }
}

.depth-popup { cursor: pointer; }
.depth-popup .leaflet-popup-content-wrapper {
  background: var(--surface-1); color: var(--text-primary); border: 1px solid var(--border);
  border-radius: 6px; box-shadow: 0 2px 8px rgba(0,0,0,.18);
}
.depth-popup .leaflet-popup-content {
  margin: 6px 9px; font-size: 12px; line-height: 1.5; font-variant-numeric: tabular-nums; white-space: nowrap;
}
.depth-popup .t-row { display: flex; justify-content: space-between; gap: 12px; }
.depth-popup .leaflet-popup-tip { background: var(--surface-1); }
