:root {
  color-scheme: light;
  --bg: #f6f5f2;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --border: #dedcd6;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #74736d;
  --accent: #2a78d6;
  --accent-ink: #1c5cab;
  --accent-soft: #cde2fb;
  --highlight: #eb6834;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 6px 24px rgba(0,0,0,.08);
  --radius: 10px;
  --top: 60px;
  --left-w: 320px;
  --right-w: 360px;
  font-family: Inter, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121211;
    --surface: #1a1a19;
    --surface-2: #242422;
    --border: #383835;
    --text: #f0efec;
    --text-2: #c3c2b7;
    --muted: #9a9890;
    --accent: #3987e5;
    --accent-ink: #86b6ef;
    --accent-soft: #184f95;
    --highlight: #f08a5d;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 6px 24px rgba(0,0,0,.4);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121211;
  --surface: #1a1a19;
  --surface-2: #242422;
  --border: #383835;
  --text: #f0efec;
  --text-2: #c3c2b7;
  --muted: #9a9890;
  --accent: #3987e5;
  --accent-ink: #86b6ef;
  --accent-soft: #184f95;
  --highlight: #f08a5d;
  --shadow: 0 1px 2px rgba(0,0,0,.4), 0 6px 24px rgba(0,0,0,.4);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); }
body { font-size: 14px; line-height: 1.45; overflow: hidden; -webkit-font-smoothing: antialiased; }
button, input, select { font: inherit; color: inherit; }
a { color: var(--accent-ink); }
svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
h1, h2, h3 { margin: 0; letter-spacing: -0.01em; }

/* ---------- top bar ---------- */
.topbar {
  position: fixed; inset: 0 0 auto 0; height: var(--top); z-index: 20;
  display: flex; align-items: center; gap: 16px; padding: 0 16px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.logo { width: 30px; height: 30px; fill: var(--accent); stroke: none; }
.logo .logo-wave { fill: none; stroke: var(--accent-ink); stroke-width: 3; }
.brand h1 { font-size: 16px; font-weight: 700; white-space: nowrap; }
.tagline { margin: 0; font-size: 12px; color: var(--muted); white-space: nowrap; }
.search { position: relative; flex: 1; max-width: 560px; margin-left: auto; display: flex; align-items: center; }
.search > svg { position: absolute; left: 12px; color: var(--muted); pointer-events: none; }
.search input {
  width: 100%; height: 38px; padding: 0 12px 0 38px; border-radius: 19px;
  border: 1px solid var(--border); background: var(--surface-2); outline: none;
}
.search input:focus { border-color: var(--accent); background: var(--surface); }
.search-results {
  position: absolute; top: 44px; left: 0; right: 0; max-height: min(70vh, 520px); overflow: auto;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 6px;
}
.search-results h4 { margin: 8px 8px 4px; font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.search-results .res {
  display: flex; gap: 10px; align-items: baseline; width: 100%; text-align: left; padding: 7px 8px;
  border: 0; background: none; border-radius: 6px; cursor: pointer;
}
.search-results .res:hover, .search-results .res[aria-selected="true"] { background: var(--surface-2); }
.search-results .res .t { font-weight: 500; }
.search-results .res .m { color: var(--muted); font-size: 12px; margin-left: auto; white-space: nowrap; }
.search-results .empty { padding: 10px; color: var(--muted); }
.actions { display: flex; align-items: center; gap: 6px; }
.btn {
  display: inline-flex; align-items: center; gap: 6px; height: 36px; padding: 0 12px;
  border: 1px solid var(--border); border-radius: 8px; background: var(--surface); cursor: pointer; font-weight: 500;
}
.btn:hover { background: var(--surface-2); }
.icon-btn {
  display: inline-grid; place-items: center; width: 36px; height: 36px; border-radius: 8px;
  border: 1px solid transparent; background: none; cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); }
.icon-btn .fill { fill: currentColor; stroke: none; }
.menu-wrap { position: relative; }
.menu {
  position: absolute; right: 0; top: 42px; width: 280px; z-index: 30; padding: 6px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
}
.menu button, .menu a {
  display: block; width: 100%; text-align: left; padding: 8px 10px; border: 0; background: none;
  border-radius: 6px; cursor: pointer; text-decoration: none; color: var(--text); font-weight: 500;
}
.menu button:hover, .menu a:hover { background: var(--surface-2); }
.menu small { display: block; font-weight: 400; color: var(--muted); font-size: 12px; }
.menu button:disabled { opacity: .45; cursor: not-allowed; }
.only-mobile { display: none; }

/* ---------- layout ---------- */
.layout { position: fixed; inset: var(--top) 0 0 0; }
#map { position: absolute; inset: 0; }
.panel {
  position: absolute; top: 12px; bottom: 12px; z-index: 10; overflow: auto;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow);
}
.panel.left { left: 12px; width: var(--left-w); padding: 4px 0; }
.panel.right { right: 12px; width: var(--right-w); padding: 16px; }
.panel.right .close { position: absolute; top: 8px; right: 8px; }
.card { padding: 12px 16px; border-bottom: 1px solid var(--border); }
.card:last-of-type { border-bottom: 0; }
.card h2 { font-size: 13px; font-weight: 600; margin-bottom: 8px; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 6px; }
.card-head h2 { margin: 0; }
.hint { margin: 0 0 8px; color: var(--text-2); font-size: 12px; }
.note { margin: 8px 0 0; color: var(--muted); font-size: 12px; }
.sub { color: var(--muted); font-size: 12px; font-weight: 400; }
.seg { display: inline-flex; padding: 2px; border-radius: 8px; background: var(--surface-2); gap: 2px; }
.seg.wide { display: flex; width: 100%; margin-bottom: 8px; }
.seg.wide button { flex: 1; }
.seg button { border: 0; background: none; padding: 5px 10px; border-radius: 6px; cursor: pointer; font-size: 12px; font-weight: 500; color: var(--text-2); }
.seg button[aria-checked="true"] { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgba(0,0,0,.12); }
.row { display: flex; align-items: center; gap: 8px; padding: 5px 0; cursor: pointer; }
.row.slider { color: var(--text-2); font-size: 12px; }
.row.slider input { flex: 1; accent-color: var(--accent); }
.row input[type="checkbox"], .row input[type="radio"] { accent-color: var(--accent); width: 16px; height: 16px; margin: 0; }

.legend { list-style: none; margin: 0; padding: 0; }
.legend li { display: flex; align-items: center; gap: 8px; padding: 4px 0; }
.legend label { display: flex; align-items: center; gap: 8px; flex: 1; cursor: pointer; }
.legend .sw { width: 22px; height: 14px; border-radius: 3px; flex: none; box-shadow: inset 0 0 0 1px rgba(0,0,0,.12); }
.legend .lbl { font-weight: 500; }
.legend .chance { margin-left: auto; color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }
.legend li.off .sw { opacity: .25; }
.legend li.off .lbl { color: var(--muted); }
.legend.compact li { padding: 2px 0; font-size: 12px; }
.legend.compact .sw { width: 14px; height: 14px; }

.filters { display: flex; gap: 6px; margin-bottom: 6px; }
.filters select, #f-text {
  flex: 1; min-width: 0; height: 32px; border: 1px solid var(--border); border-radius: 6px; background: var(--surface-2); padding: 0 8px;
}
#f-text { width: 100%; margin-bottom: 6px; }
.huc-list { list-style: none; margin: 0; padding: 0; max-height: 280px; overflow: auto; }
.huc-list li button {
  display: flex; align-items: center; gap: 8px; width: 100%; padding: 6px 4px; border: 0; background: none; text-align: left; border-radius: 6px; cursor: pointer;
}
.huc-list li button:hover { background: var(--surface-2); }
.huc-list .code { font-variant-numeric: tabular-nums; color: var(--muted); font-size: 12px; }
.huc-list .nm { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dot { width: 9px; height: 9px; border-radius: 50%; flex: none; }
.disclaimer { margin: 0; padding: 10px 16px 14px; font-size: 12px; color: var(--muted); }

/* ---------- details ---------- */
#details h2 { font-size: 18px; font-weight: 700; padding-right: 36px; }
#details .kicker { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; margin-bottom: 4px; }
#details .subtitle { color: var(--text-2); margin: 2px 0 12px; }
.badge { display: inline-flex; align-items: center; gap: 6px; padding: 2px 8px; border-radius: 10px; background: var(--surface-2); font-size: 12px; font-weight: 500; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; margin: 12px 0; font-size: 13px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; font-variant-numeric: tabular-nums; }
.section-title { font-size: 12px; font-weight: 600; color: var(--text-2); text-transform: uppercase; letter-spacing: .06em; margin: 16px 0 6px; }
.tbl { width: 100%; border-collapse: collapse; font-size: 13px; font-variant-numeric: tabular-nums; }
.tbl th, .tbl td { padding: 5px 6px; text-align: right; border-bottom: 1px solid var(--border); }
.tbl th:first-child, .tbl td:first-child { text-align: left; }
.tbl th { color: var(--muted); font-weight: 500; font-size: 12px; }
.tbl tbody tr:hover { background: var(--surface-2); }
.tbl th.sortable { cursor: pointer; user-select: none; }
.tbl th.sortable:hover { color: var(--text); }
.downloads { display: grid; gap: 6px; margin-top: 8px; }
.downloads a, .downloads button {
  display: flex; align-items: center; gap: 8px; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px;
  text-decoration: none; color: var(--text); background: var(--surface); cursor: pointer; text-align: left; font-weight: 500;
}
.downloads a:hover, .downloads button:hover { background: var(--surface-2); }
.downloads small { margin-left: auto; color: var(--muted); font-weight: 400; }
.actions-row { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 8px; }
.msg { padding: 10px 12px; border-radius: 8px; background: var(--surface-2); color: var(--text-2); font-size: 13px; }

/* ---------- charts ---------- */
.chart { position: relative; margin: 4px 0 8px; }
.chart svg { width: 100%; height: auto; display: block; overflow: visible; stroke: none; fill: none; }
.chart svg text { stroke: none; font-weight: 400; }
.chart .grid line { stroke: var(--border); stroke-width: 1; }
.chart .axis text { fill: var(--muted); font-size: 11px; }
.chart .bar { fill: var(--accent); }
.chart .bar:hover, .chart .bar.hot { fill: var(--accent-ink); }
.chart .line { stroke: var(--accent); stroke-width: 2; fill: none; }
.chart .pt { fill: var(--accent); stroke: var(--surface); stroke-width: 2; }
.chart .hit { fill: transparent; cursor: default; }
.tip {
  position: absolute; pointer-events: none; z-index: 5; padding: 6px 8px; border-radius: 6px; font-size: 12px; white-space: nowrap;
  background: var(--text); color: var(--surface); transform: translate(-50%, calc(-100% - 8px));
}

/* ---------- sheets ---------- */
dialog.sheet {
  width: min(960px, calc(100vw - 32px)); max-height: calc(100vh - 48px); padding: 0; border: 1px solid var(--border);
  border-radius: 14px; background: var(--surface); color: var(--text); box-shadow: var(--shadow);
}
dialog.sheet::backdrop { background: rgba(0,0,0,.35); }
.sheet-head { position: sticky; top: 0; display: flex; align-items: center; justify-content: space-between; padding: 14px 20px; background: var(--surface); border-bottom: 1px solid var(--border); z-index: 2; }
.sheet-head h2 { font-size: 18px; }
.sheet-body { padding: 16px 20px 24px; overflow: auto; max-height: calc(100vh - 120px); }
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 10px; margin-bottom: 16px; }
.tile { padding: 12px 14px; border: 1px solid var(--border); border-radius: 10px; background: var(--surface); }
.tile .v { font-size: 24px; font-weight: 700; font-variant-numeric: tabular-nums; letter-spacing: -0.02em; }
.tile .k { color: var(--muted); font-size: 12px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin: 16px 0 8px; }
.tabs button { border: 0; background: none; padding: 8px 12px; cursor: pointer; font-weight: 500; color: var(--text-2); border-bottom: 2px solid transparent; }
.tabs button[aria-selected="true"] { color: var(--text); border-bottom-color: var(--accent); }
.table-wrap { max-height: 420px; overflow: auto; }
.prose { font-size: 14px; max-width: 760px; }
.prose h3 { font-size: 15px; margin: 20px 0 6px; }
.prose p, .prose li { color: var(--text-2); }
.prose strong { color: var(--text); }
.callout { padding: 12px 14px; border-radius: 10px; border: 1px solid var(--highlight); background: color-mix(in srgb, var(--highlight) 10%, var(--surface)); color: var(--text); }
.cite { white-space: pre-wrap; padding: 10px 12px; border-radius: 8px; background: var(--surface-2); font-size: 12px; }
.muted { color: var(--muted); }
.dict th, .dict td { text-align: left !important; vertical-align: top; }
.dict code { font-size: 12px; }

.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 40; padding: 10px 14px; border-radius: 8px; background: var(--text); color: var(--surface); box-shadow: var(--shadow); }

/* ---------- map overrides ---------- */
.maplibregl-ctrl-group { background: var(--surface) !important; border: 1px solid var(--border); box-shadow: var(--shadow) !important; }
.maplibregl-ctrl-group button + button { border-top-color: var(--border) !important; }
:root[data-theme="dark"] .maplibregl-ctrl-icon, .dark-map .maplibregl-ctrl-icon { filter: invert(1); }
.maplibregl-ctrl-attrib { background: color-mix(in srgb, var(--surface) 85%, transparent) !important; color: var(--text-2); }
.maplibregl-ctrl-attrib a { color: var(--text-2) !important; }
.maplibregl-ctrl-scale { background: color-mix(in srgb, var(--surface) 80%, transparent); color: var(--text); border-color: var(--text-2); }
.maplibregl-popup-content { background: var(--surface); color: var(--text); border-radius: 8px; box-shadow: var(--shadow); padding: 8px 10px; font-size: 12px; }
.maplibregl-popup-tip { border-top-color: var(--surface) !important; border-bottom-color: var(--surface) !important; }
.maplibregl-ctrl-bottom-left { left: calc(var(--left-w) + 24px); }

/* ---------- return-period slider ---------- */
.rp-slider { margin: 4px 0 6px; }
.rp-head { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; flex-wrap: wrap; }
.rp-head strong { white-space: nowrap; }
.rp-head .chance { white-space: nowrap; }
.rp-head .sw { width: 22px; height: 14px; border-radius: 3px; flex: none; }
.rp-head .chance { margin-left: auto; color: var(--muted); font-size: 12px; }
.icon-btn.small { width: 30px; height: 30px; border: 1px solid var(--border); }
.icon-btn .play { fill: currentColor; stroke: none; }
#rp-range { width: 100%; accent-color: var(--accent); margin: 0; }
.rp-ticks { display: flex; justify-content: space-between; font-size: 11px; color: var(--muted); padding: 0 2px; font-variant-numeric: tabular-nums; }
.rp-ticks span { width: 24px; text-align: center; }
.rp-ticks span:first-child { text-align: left; }
.rp-ticks span:last-child { text-align: right; }
#rp-desc { margin-top: 6px; }

/* ---------- panel chrome ---------- */
.panel.left .collapse { position: absolute; top: 6px; right: 6px; z-index: 2; width: 28px; height: 28px; }
.panel.left .card:first-of-type .card-head { padding-right: 30px; }
.panel.left.collapsed { display: none; }
.fab {
  position: absolute; top: 12px; left: 12px; z-index: 11; display: inline-flex; align-items: center; gap: 6px; height: 38px; padding: 0 12px;
  border: 1px solid var(--border); border-radius: 19px; background: var(--surface); box-shadow: var(--shadow); cursor: pointer; font-weight: 500;
}
.mini-legend {
  position: absolute; left: 12px; bottom: 28px; z-index: 11; padding: 8px 10px; border-radius: 10px; background: var(--surface);
  border: 1px solid var(--border); box-shadow: var(--shadow); font-size: 11px; color: var(--text-2);
}
.mini-legend .bar { display: flex; height: 10px; width: 210px; border-radius: 3px; overflow: hidden; margin: 4px 0 3px; }
.mini-legend .bar span { flex: 1; }
.mini-legend .lab { display: flex; justify-content: space-between; width: 210px; font-variant-numeric: tabular-nums; }
.loading {
  position: absolute; top: 0; left: 0; right: 0; height: 2px; z-index: 15; overflow: hidden; pointer-events: none;
}
.loading::after {
  content: ""; position: absolute; top: 0; bottom: 0; width: 30%; background: var(--accent); animation: load 1.1s ease-in-out infinite;
}
@keyframes load { from { left: -30%; } to { left: 100%; } }
@media (prefers-reduced-motion: reduce) { .loading::after { animation: none; left: 0; width: 100%; opacity: .5; } }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-ink); }
:root[data-theme="dark"] .btn.primary, .dark-map .btn.primary { color: #0b0b0b; }

/* ---------- zoomed-out legend ---------- */
.share-legend { margin-top: 10px; padding-top: 10px; border-top: 1px dashed var(--border); }
.share-title { font-size: 12px; font-weight: 500; color: var(--text-2); margin-bottom: 6px; }
.share-bar { display: flex; height: 12px; border-radius: 3px; overflow: hidden; }
.share-bar span { flex: 1; }
.share-lab { display: flex; justify-content: space-between; font-size: 11px; color: var(--muted); margin-top: 3px; font-variant-numeric: tabular-nums; }
.share-lab span { flex: 1; text-align: left; transform: translateX(-30%); }
.share-lab span:first-child { transform: none; }
.share-legend .hint { margin: 6px 0 0; }

/* ---------- intro ---------- */
dialog.intro {
  width: min(460px, calc(100vw - 32px)); padding: 22px 24px 18px; border: 1px solid var(--border); border-radius: 16px;
  background: var(--surface); color: var(--text); box-shadow: var(--shadow);
}
dialog.intro::backdrop { background: rgba(0,0,0,.3); }
dialog.intro h2 { font-size: 20px; margin-bottom: 8px; }
dialog.intro p { color: var(--text-2); margin: 0 0 12px; }
.intro-list { list-style: none; padding: 0; margin: 0 0 12px; display: grid; gap: 10px; }
.intro-list li { display: flex; gap: 10px; align-items: flex-start; color: var(--text-2); }
.intro-list .n { flex: none; width: 22px; height: 22px; border-radius: 50%; display: grid; place-items: center; font-size: 12px; font-weight: 700; background: var(--accent-soft); color: var(--accent-ink); }
.intro-list strong { color: var(--text); }
.intro-actions { display: flex; justify-content: flex-end; }
.small { font-size: 12px; }
.search-results .res .ic { color: var(--muted); }
.search-results .res .ic svg { width: 14px; height: 14px; }

/* ---------- small screens ---------- */
@media (max-width: 1100px) {
  .btn span { display: none; }
  .btn { padding: 0 9px; }
  .tagline { display: none; }
}
@media (max-width: 760px) {
  :root { --top: 104px; }
  .topbar { flex-wrap: wrap; height: var(--top); padding: 8px 16px; gap: 8px; align-content: center; }
  .brand { flex: 1; }
  .brand h1 { font-size: 15px; }
  .search { order: 3; flex-basis: 100%; max-width: none; margin: 0; }
  .only-mobile { display: inline-grid; }
  .panel.left { left: 0; right: 0; top: auto; bottom: 0; width: auto; max-height: 62vh; border-radius: 14px 14px 0 0; transform: translateY(105%); transition: transform .2s; }
  .panel.left.open { transform: none; }
  .panel.right { left: 0; right: 0; top: auto; bottom: 0; width: auto; max-height: 60vh; border-radius: 14px 14px 0 0; }
  .maplibregl-ctrl-bottom-left { left: 0; }
  .grid2 { grid-template-columns: 1fr; }
  .seg button { min-height: 40px; }
  .brand { min-width: 0; overflow: hidden; }
  .brand h1 { overflow: hidden; text-overflow: ellipsis; }
  .actions { gap: 2px; }
  .actions .btn { border-color: transparent; padding: 0 7px; }
  .row { padding: 9px 0; }
  .legend li { padding: 6px 0; }
  .huc-list li button { padding: 10px 4px; }
  .panel.left .collapse, .fab { display: none !important; }
  .mini-legend { left: 10px; bottom: 40px; padding: 6px 8px; }
  .mini-legend .bar, .mini-legend .lab { width: 170px; }
  .panel.left.collapsed { display: block; }
  #rp-range { height: 32px; }
}
@media (max-width: 480px) {
  .brand h1, .brand .tagline { display: none; }
  #btn-share { display: none; }
  dialog.sheet { width: 100vw; max-width: 100vw; max-height: 100vh; height: 100vh; margin: 0; border-radius: 0; }
}
