:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --sunk: #f7f9fb;
  --ink: #1d2430;
  --muted: #6b7480;
  --line: #dde1e7;
  --accent: #2f6fed;
  --accent-dark: #2557bd;
  --accent-wash: rgba(47, 111, 237, 0.08);
  --good: #14894a;
  --bad: #c02a2a;
  --warn-ink: #8a6d0b;
  --warn-bg: #fdf3d0;
  --grid: #e6e9ee;
  --veil: rgba(255, 255, 255, 0.84);
}

/* Field data gets read at a desk and on a phone in the sun, so the app takes
 * the reader's theme rather than forcing a bright page on either. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #14171c;
    --panel: #1b1f26;
    --sunk: #21262e;
    --ink: #e7eaee;
    --muted: #9aa3ae;
    --line: #333a44;
    --accent: #5b8def;
    --accent-dark: #7ba4f3;
    --accent-wash: rgba(91, 141, 239, 0.14);
    --good: #3aa76d;
    --bad: #e2635f;
    --warn-ink: #e8c86a;
    --warn-bg: #3a3218;
    --grid: #2b313a;
    --veil: rgba(27, 31, 38, 0.84);
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  padding: 0 0 60px;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

header {
  padding: 22px 28px 18px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
header h1 { margin: 0; font-size: 21px; }
header .sub { margin: 4px 0 0; color: var(--muted); font-size: 14px; max-width: 70ch; }

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

.step, .panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 18px 20px 20px;
  margin-bottom: 18px;
}
.step h2 {
  margin: 0 0 12px;
  font-size: 16px;
  display: flex;
  align-items: center;
  gap: 9px;
}
.step .num {
  display: inline-grid;
  place-items: center;
  width: 23px;
  height: 23px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  font-size: 13px;
}

h3 { font-size: 14px; margin: 0 0 9px; }
.help { margin: 0 0 12px; color: var(--muted); font-size: 13.5px; max-width: 74ch; }
.hint { font-size: 12.5px; color: var(--muted); margin: 0; }

button {
  font: inherit;
  font-size: 14px;
  padding: 7px 14px;
  border-radius: 7px;
  border: 1px solid var(--accent);
  background: var(--accent);
  color: #fff;
  cursor: pointer;
}
button:hover:not(:disabled) { background: var(--accent-dark); }
button:disabled { opacity: .45; cursor: not-allowed; }
button.ghost { background: transparent; color: var(--ink); border-color: var(--line); }
button.ghost:hover:not(:disabled) { background: var(--accent-wash); }
button.small { font-size: 13px; padding: 5px 11px; }

.msg { color: var(--muted); font-size: 13.5px; }
.msg.ok { color: var(--good); }
.msg.bad { color: var(--bad); }
.msg.busy::after {
  content: "";
  display: inline-block;
  width: 9px;
  height: 9px;
  margin-left: 6px;
  border: 2px solid var(--line);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .7s linear infinite;
  vertical-align: -1px;
}
@keyframes spin { to { transform: rotate(360deg); } }

input[type="text"], input[type="date"], select {
  font: inherit;
  font-size: 14px;
  padding: 6px 9px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel);
  color: var(--ink);
}

/* Step 1. The two ways in sit side by side as equals: a drawn boundary and a
 * typed point are both complete answers to "which place?". */
.pick {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  gap: 16px;
  align-items: center;
}
@media (max-width: 820px) {
  .pick { grid-template-columns: 1fr; }
  .or { flex-direction: row !important; }
  .or::before, .or::after { height: 1px !important; width: 40px !important; }
}

.or {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 12.5px;
}
.or::before, .or::after {
  content: "";
  width: 1px;
  height: 34px;
  background: var(--line);
}

.drop {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 5px;
  padding: 30px 20px;
  height: 100%;
  min-height: 150px;
  border: 2px dashed var(--line);
  border-radius: 10px;
  background: var(--sunk);
  cursor: pointer;
  text-align: center;
  transition: border-color .15s, background .15s;
}
.drop:hover, .drop:focus-within { border-color: var(--accent); }
.drop.over { border-color: var(--accent); background: var(--accent-wash); }
.drop-main { font-size: 15px; color: var(--ink); }
.drop-sub { font-size: 13px; color: var(--muted); }
.drop input[type="file"] {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

.typed > label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.typed .row { display: flex; gap: 8px; margin-bottom: 9px; }
.typed .row input { flex: 1; min-width: 0; }
.typed .hint { margin-bottom: 10px; }

.examples {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  font-size: 12.5px;
  color: var(--muted);
}
.chip {
  font: inherit;
  font-size: 12.5px;
  padding: 4px 10px;
  border-radius: 20px;
  border: 1px solid var(--line);
  background: var(--sunk);
  color: var(--muted);
  cursor: pointer;
}
.chip:hover { border-color: var(--accent); color: var(--ink); background: var(--sunk); }

code {
  font-size: 12.5px;
  background: var(--sunk);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 0 4px;
}

/* A finished step, collapsed to one line. */
.done {
  max-width: 1180px;
  margin: 0 auto 14px;
  padding: 9px 14px;
  display: flex;
  align-items: center;
  gap: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--sunk);
  color: var(--muted);
  font-size: 13.5px;
}
.done .tick {
  display: inline-grid;
  place-items: center;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--good);
  color: #fff;
  font-size: 12px;
  font-weight: 600;
  flex: none;
}
.done .link {
  margin-left: auto;
  border: 0;
  background: none;
  padding: 0;
  font: inherit;
  color: var(--accent);
  cursor: pointer;
  text-decoration: underline;
}

/* The two tools are alternatives, not steps, so they read as tabs -- the
 * chosen place carries across both. */
.tabs {
  display: flex;
  gap: 4px;
  margin-bottom: -1px;
  padding-left: 6px;
}
.tab {
  border: 1px solid var(--line);
  border-bottom-color: transparent;
  border-radius: 8px 8px 0 0;
  background: var(--sunk);
  color: var(--muted);
  padding: 8px 16px;
  font-size: 14px;
}
.tab:hover:not([aria-selected="true"]) { background: var(--accent-wash); color: var(--ink); }
.tab[aria-selected="true"] {
  background: var(--panel);
  color: var(--ink);
  font-weight: 600;
  border-bottom-color: var(--panel);
}

.map-grid { display: grid; grid-template-columns: 1fr 300px; gap: 16px; align-items: start; }
@media (max-width: 900px) { .map-grid { grid-template-columns: 1fr; } }

.map {
  height: 460px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--sunk);
  z-index: 0;                /* keeps Leaflet panes under the page chrome */
}
.map-help { margin: 10px 0 0; }

/* Leaflet ships a white popup and attribution bar; in dark mode those glare,
 * so they are re-tinted from the same tokens as everything else. */
.leaflet-popup-content-wrapper, .leaflet-popup-tip,
.leaflet-control-layers, .leaflet-bar a {
  background: var(--panel);
  color: var(--ink);
}
.leaflet-bar a { border-bottom-color: var(--line); }
.leaflet-control-attribution {
  background: var(--panel) !important;
  color: var(--muted);
}
.leaflet-control-attribution a { color: var(--accent); }

.side {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px;
  background: var(--sunk);
}
.side dl { margin: 0; display: grid; grid-template-columns: 1fr auto; gap: 5px 10px; font-size: 13px; }
.side dt { color: var(--muted); }
.side dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }
.side dd.strong { font-weight: 600; }
.side hr { border: 0; border-top: 1px solid var(--line); margin: 12px 0; }
.side-actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-top: 12px; }

.rings { margin-top: 18px; }

.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: 8px; }
table { border-collapse: collapse; width: 100%; font-size: 13px; }
th, td { padding: 7px 11px; text-align: right; white-space: nowrap; }
th:first-child, td:first-child { text-align: left; }
thead th { background: var(--sunk); color: var(--muted); font-weight: 600; position: sticky; top: 0; }
tbody tr { border-top: 1px solid var(--line); }
tbody tr:hover { background: var(--accent-wash); }
td { font-variant-numeric: tabular-nums; }
td.missing { color: var(--warn-ink); }

.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: flex-end;
  margin-bottom: 4px;
}
.control { display: flex; flex-direction: column; gap: 4px; }
.control label { font-size: 12.5px; color: var(--muted); }

.chart-head {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  margin: 18px 0 4px;
}
.chart-head h3 { margin: 0; font-size: 15px; font-weight: 700; }
.readout { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; min-height: 20px; font-weight: 600; }
.readout b { color: var(--ink); font-weight: 600; }
.readout .swatch {
  display: inline-block;
  width: 9px;
  height: 9px;
  border-radius: 2px;
  margin-right: 4px;
}

.chart-wrap { border: 1px solid var(--line); border-radius: 8px; background: var(--sunk); padding: 6px; }
.chart { display: block; width: 100%; height: auto; touch-action: pan-y; }
/* Only once part of the range is off screen is there anywhere to drag to. */
.chart.pannable { cursor: grab; }
.chart.pannable:active { cursor: grabbing; }
.chart .grid { stroke: var(--grid); stroke-width: 1; }
/* Date labels carry their own size inline, worked out to fit the width; this
 * sets the size of the value ticks, which are always the same. */
.chart .tick { fill: var(--muted); font-size: 11px; font-family: inherit; font-weight: 600; }
.chart .axis-name { fill: var(--muted); font-size: 10px; font-family: inherit; font-weight: 600; }
/* The year tier carries the run of months above it, so it reads as the heading
 * of that run rather than as another tick. */
.chart .group-tick { fill: var(--ink); font-weight: 700; }
/* Heavier and darker than a gridline -- this is the one division on the axis
 * that actually means something changed (a new year), so it is drawn to stand
 * out from the ticks either side of it rather than blend in with them. */
.chart .tier { stroke: var(--accent); stroke-width: 2; }
.chart .cursor { stroke: var(--muted); stroke-width: 1; stroke-dasharray: 3 3; }
.chart .bar { opacity: .85; }
.chart .dot { opacity: .9; }
.chart-empty, .chart-unit { margin: 0; padding: 8px; font-size: 12.5px; color: var(--muted); text-align: center; }

.chart-foot {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px 14px;
  padding: 8px 4px 2px;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
}
.chart-legend { display: flex; flex-wrap: wrap; gap: 4px 14px; }
.legend-item { display: inline-flex; align-items: center; gap: 5px; }
.legend-item .swatch { width: 9px; height: 9px; border-radius: 2px; flex: none; }
.chart-hint { margin-left: auto; font-variant-numeric: tabular-nums; }

/* Full screen is where a long record is actually legible: the same chart, with
 * the room to label every month instead of every third one. The stage holds the
 * title, readout and legend as well, since a chart with no readout is no more
 * use large than small. */
#chart-stage:fullscreen {
  background: var(--bg);
  padding: 16px 22px 20px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
#chart-stage:fullscreen .chart-head { margin-top: 0; }
/* Not a flex row: the foot is a child of the wrap, and side by side it would
 * take the width the chart is here to use. */
#chart-stage:fullscreen .chart-wrap { flex: 1; position: relative; }

/* Full screen the legend moves into the corner and out of the way. It reads
 * through to the plot behind it and passes clicks and drags on, so the chart is
 * still draggable underneath -- only the reset button takes a press. */
#chart-stage:fullscreen .chart-foot {
  position: absolute;
  top: 10px;
  right: 14px;
  flex-direction: column;
  align-items: flex-start;
  gap: 3px;
  max-width: 270px;
  padding: 6px 9px 7px;
  font-size: 11px;
  line-height: 1.35;
  background: var(--veil);
  border: 1px solid var(--line);
  border-radius: 7px;
  pointer-events: none;
}
#chart-stage:fullscreen .chart-legend { flex-direction: column; gap: 2px; }
#chart-stage:fullscreen .chart-hint { margin-left: 0; }
#chart-stage:fullscreen .chart-foot button { pointer-events: auto; margin-top: 2px; }

.picker { margin-top: 14px; }
.picker .help { margin-bottom: 10px; }
.group { margin-bottom: 10px; }
.group-name { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; margin-bottom: 5px; }
.group-items { display: flex; flex-wrap: wrap; gap: 6px; }
.pill {
  font: inherit;
  font-size: 12.5px;
  padding: 5px 11px;
  border-radius: 20px;
  border: 1px solid var(--line);
  background: var(--sunk);
  color: var(--muted);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.pill:hover:not(.on) { border-color: var(--accent); color: var(--ink); background: var(--sunk); }
.pill.on { border-color: currentColor; color: var(--ink); font-weight: 600; background: var(--panel); }
.pill .swatch { width: 9px; height: 9px; border-radius: 50%; background: currentColor; opacity: 0; }
.pill.on .swatch { opacity: 1; }

.summary-head { margin: 20px 0 9px; font-size: 15px; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}

footer {
  max-width: 1180px;
  margin: 0 auto;
  padding: 0 20px;
  color: var(--muted);
  font-size: 13px;
}
footer a { color: var(--accent); }
