:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f2f1ed;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --ring: rgba(11, 11, 11, 0.10);
  --accent: #2a78d6;
  --accent-wash: rgba(42, 120, 214, 0.12);
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
  --font: system-ui, -apple-system, "Segoe UI", sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #232321;
    --ink: #f0efec;
    --ink-2: #c3c2b7;
    --muted: #898781;
    --grid: #2c2c2a;
    --axis: #383835;
    --ring: rgba(255, 255, 255, 0.10);
    --accent: #3987e5;
    --accent-wash: rgba(57, 135, 229, 0.18);
    --shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d; --surface: #1a1a19; --surface-2: #232321; --ink: #f0efec; --ink-2: #c3c2b7;
  --muted: #898781; --grid: #2c2c2a; --axis: #383835; --ring: rgba(255, 255, 255, 0.10);
  --accent: #3987e5; --accent-wash: rgba(57, 135, 229, 0.18); --shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; background: var(--page); color: var(--ink); font: 13px/1.4 var(--font); }
#map { position: fixed; inset: 0; }

/* ---------- side panel */
#panel {
  position: fixed; top: 12px; left: 12px; bottom: 12px; width: 340px; z-index: 3;
  display: flex; flex-direction: column; background: var(--surface); border-radius: 12px;
  box-shadow: var(--shadow), 0 0 0 1px var(--ring); overflow: hidden;
}
#panel header { padding: 12px 14px 10px; border-bottom: 1px solid var(--grid); }
.brand { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
h1 { font-size: 16px; margin: 0; letter-spacing: -0.01em; }
h2 { font-size: 12px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--ink-2); margin: 0 0 8px; font-weight: 600; }
h2 .hint { text-transform: none; letter-spacing: 0; font-weight: 400; color: var(--muted); margin-left: 4px; }
.scroll { overflow-y: auto; flex: 1; }
section { padding: 12px 14px; border-bottom: 1px solid var(--grid); }
section:last-child { border-bottom: 0; }
.note { color: var(--muted); font-size: 12px; margin: 6px 0 0; }
.about p { color: var(--ink-2); font-size: 12px; margin: 0 0 6px; }
button.icon { border: 0; background: transparent; color: var(--ink-2); font-size: 18px; width: 28px; height: 28px; border-radius: 6px; cursor: pointer; }
button.icon:hover { background: var(--surface-2); }
#collapse { display: none; }
button.link { border: 0; background: none; color: var(--accent); padding: 4px 0; cursor: pointer; font: inherit; }

.search { position: relative; }
#q { width: 100%; padding: 8px 10px; border-radius: 8px; border: 1px solid var(--axis); background: var(--page); color: var(--ink); font: inherit; }
#q:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
#sugg { position: absolute; left: 0; right: 0; top: 100%; margin: 4px 0 0; padding: 4px; list-style: none; background: var(--surface); border-radius: 8px; box-shadow: var(--shadow), 0 0 0 1px var(--ring); z-index: 5; }
#sugg li { padding: 6px 8px; border-radius: 6px; cursor: pointer; }
#sugg li small { color: var(--muted); margin-left: 6px; }
#sugg li[aria-selected="true"], #sugg li:hover { background: var(--accent-wash); }

/* segmented metric picker */
.seg { display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; }
.seg button { padding: 6px 4px; border-radius: 7px; border: 1px solid var(--grid); background: var(--page); color: var(--ink-2); cursor: pointer; font: inherit; }
.seg button[aria-checked="true"] { background: var(--accent-wash); border-color: var(--accent); color: var(--ink); font-weight: 600; }

.legend { display: flex; flex-wrap: wrap; gap: 0; margin-top: 10px; }
.legend .sw { flex: 1; min-width: 24px; }
.legend .sw i { display: block; height: 10px; }
.legend .sw:first-child i { border-radius: 3px 0 0 3px; }
.legend .sw:last-child i { border-radius: 0 3px 3px 0; }
.legend .sw span { display: block; font-size: 10px; color: var(--muted); margin-top: 3px; white-space: nowrap; font-variant-numeric: tabular-nums; }
.legend .nodata { flex: 0 0 auto; margin-left: 8px; }
.legend .nodata i { width: 10px; height: 10px; border-radius: 50%; display: inline-block; background: #9a9893; vertical-align: middle; }

/* filter rows: histogram + dual range */
.frow { margin-bottom: 12px; }
.frow .top { display: flex; justify-content: space-between; align-items: baseline; }
.frow .name { font-weight: 600; }
.frow .val { color: var(--ink-2); font-variant-numeric: tabular-nums; font-size: 12px; }
.frow .val.on { color: var(--accent); font-weight: 600; }
.frow canvas { width: 100%; height: 34px; display: block; margin-top: 4px; }
.dual { position: relative; height: 18px; }
.dual input { position: absolute; left: 0; right: 0; width: 100%; top: 0; margin: 0; pointer-events: none; appearance: none; -webkit-appearance: none; background: none; height: 18px; }
.dual input::-webkit-slider-thumb { pointer-events: auto; -webkit-appearance: none; width: 14px; height: 14px; border-radius: 50%; background: var(--surface); border: 2px solid var(--accent); cursor: grab; }
.dual input::-moz-range-thumb { pointer-events: auto; width: 12px; height: 12px; border-radius: 50%; background: var(--surface); border: 2px solid var(--accent); cursor: grab; }
.dual .track { position: absolute; left: 7px; right: 7px; top: 8px; height: 2px; background: var(--grid); border-radius: 2px; }
.dual .fill { position: absolute; top: 8px; height: 2px; background: var(--accent); }
.frow .missing { font-size: 11px; color: var(--muted); }

.chips { display: flex; flex-wrap: wrap; gap: 4px; margin: 6px 0 10px; }
.chips button { border: 1px solid var(--grid); background: var(--page); color: var(--ink-2); border-radius: 999px; padding: 3px 9px; cursor: pointer; font: inherit; font-size: 12px; }
.chips button[aria-pressed="true"] { background: var(--accent-wash); border-color: var(--accent); color: var(--ink); }
.chips button small { color: var(--muted); margin-left: 3px; font-variant-numeric: tabular-nums; }
.row { display: flex; gap: 8px; align-items: center; margin-bottom: 4px; }
.row.indent, .legend.indent { margin-left: 22px; margin-bottom: 8px; }
.row .lbl { font-weight: 600; }
.xy label { flex: 1; min-width: 0; display: flex; gap: 6px; align-items: center; color: var(--ink-2); }
select { flex: 1; min-width: 0; padding: 4px 6px; border-radius: 6px; border: 1px solid var(--axis); background: var(--page); color: var(--ink); font: inherit; }
.toggle { display: flex; align-items: center; gap: 8px; margin: 6px 0; cursor: pointer; }
.toggle input { accent-color: var(--accent); }
.range-row { display: flex; align-items: center; gap: 8px; color: var(--ink-2); margin-top: 6px; }
.range-row input { flex: 1; accent-color: var(--accent); }
#scatter { width: 100%; height: 220px; display: block; margin-top: 6px; cursor: crosshair; touch-action: none; }

/* ---------- zoom ladder */
#ladder { position: fixed; right: 12px; bottom: 150px; z-index: 2; display: flex; flex-direction: column; gap: 2px; background: var(--surface); border-radius: 10px; padding: 4px; box-shadow: var(--shadow), 0 0 0 1px var(--ring); }
#ladder button { border: 0; background: none; color: var(--ink-2); font: inherit; font-size: 12px; text-align: left; padding: 5px 10px 5px 22px; border-radius: 6px; cursor: pointer; position: relative; }
#ladder button::before { content: ""; position: absolute; left: 9px; top: 50%; width: 6px; height: 6px; margin-top: -3px; border-radius: 50%; background: var(--axis); }
#ladder button.on { color: var(--ink); font-weight: 600; background: var(--accent-wash); }
#ladder button.on::before { background: var(--accent); }
#ladder button:hover { background: var(--surface-2); }

#busy { position: fixed; top: 12px; left: 50%; transform: translateX(-50%); z-index: 4; background: var(--surface); color: var(--ink-2); padding: 6px 12px; border-radius: 999px; box-shadow: var(--shadow), 0 0 0 1px var(--ring); display: flex; gap: 8px; align-items: center; font-size: 12px; }
.spin { width: 12px; height: 12px; border: 2px solid var(--grid); border-top-color: var(--accent); border-radius: 50%; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

#tip { position: fixed; z-index: 6; pointer-events: none; background: var(--surface); color: var(--ink); padding: 7px 9px; border-radius: 8px; box-shadow: var(--shadow), 0 0 0 1px var(--ring); font-size: 12px; max-width: 280px; }
#tip b { display: block; margin-bottom: 2px; }
#tip .kv { color: var(--ink-2); font-variant-numeric: tabular-nums; }

/* ---------- detail card */
#detail { position: fixed; top: 12px; right: 12px; width: 330px; max-height: calc(100% - 24px); overflow-y: auto; z-index: 3; background: var(--surface); border-radius: 12px; box-shadow: var(--shadow), 0 0 0 1px var(--ring); padding: 14px; }
#detail-close { position: absolute; top: 8px; right: 8px; }
#detail h3 { margin: 0 28px 2px 0; font-size: 15px; }
#detail .sub { color: var(--muted); margin-bottom: 10px; }
#detail dl { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; margin: 0 0 12px; }
#detail dt { color: var(--muted); }
#detail dd { margin: 0; font-variant-numeric: tabular-nums; }
#detail h4 { margin: 12px 0 6px; font-size: 12px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--ink-2); }
#detail .links a { color: var(--accent); margin-right: 10px; font-size: 12px; }
.badge { display: inline-block; padding: 1px 7px; border-radius: 4px; color: #fff; font-weight: 700; }
.bar { position: relative; height: 8px; background: var(--grid); border-radius: 4px; margin: 6px 0 2px; }
.bar i { position: absolute; left: 0; top: 0; bottom: 0; border-radius: 4px; }
.bar b { position: absolute; top: -3px; bottom: -3px; width: 2px; background: var(--ink); }
svg.spark text { fill: var(--muted); font-size: 10px; font-variant-numeric: tabular-nums; }

.maplibregl-ctrl-group { background: var(--surface) !important; box-shadow: var(--shadow), 0 0 0 1px var(--ring) !important; }
.maplibregl-ctrl-group button + button { border-top-color: var(--grid) !important; }
.maplibregl-ctrl button .maplibregl-ctrl-icon { filter: var(--icon-filter, none); }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) { --icon-filter: invert(1); } }
.maplibregl-ctrl-attrib { background: color-mix(in srgb, var(--surface) 80%, transparent) !important; color: var(--ink-2); }
.maplibregl-ctrl-attrib a { color: var(--ink-2) !important; }

/* ---------- phone: panel becomes a bottom sheet */
@media (max-width: 720px) {
  #panel { top: auto; left: 0; right: 0; bottom: 0; width: auto; max-height: 62vh; border-radius: 14px 14px 0 0; }
  #panel.collapsed .scroll { display: none; }
  #collapse { display: inline-block; }
  #panel.collapsed #collapse { transform: rotate(180deg); }
  #detail { left: 16px; right: 16px; width: auto; top: 16px; max-height: 45vh; }
  #ladder { bottom: auto; top: 70px; right: 10px; }
  #ladder button { padding: 4px 8px 4px 20px; }
  .maplibregl-ctrl-bottom-right { display: none; }
}

.blk { transition: opacity .2s; }
.blk + .blk { margin-top: 14px; }
.blk.dim { opacity: 0.5; }
.blk.dim:hover { opacity: 1; }
.legend .sw span { text-align: left; margin-left: -2px; }
.legend .sw:first-child span { margin-left: 0; }
.frow.empty canvas, .frow.empty .missing { display: none; }
.frow.empty { margin-bottom: 6px; }

/* ---------- query bar + summary */
.h2 { display: block; font-size: 12px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--ink-2); margin: 0 0 6px; font-weight: 600; }
#fq { width: 100%; padding: 9px 10px; border-radius: 8px; border: 1px solid var(--axis); background: var(--page); color: var(--ink); font: 13px/1.3 ui-monospace, SFMono-Regular, Menlo, monospace; }
#fq:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.note.err { color: #d03b3b; }
.summary { margin-top: 10px; font-size: 13px; line-height: 1.5; }
.summary b { font-size: 18px; font-variant-numeric: tabular-nums; }
.summary .muted, .muted { color: var(--muted); }
.summary .warn { margin-top: 6px; padding: 6px 8px; border-radius: 6px; background: color-mix(in srgb, #fab219 16%, transparent); color: var(--ink-2); font-size: 12px; }
.seg3 { grid-template-columns: repeat(3, 1fr); margin-bottom: 10px; }
.lbl-row { margin: 4px 0 6px; }
.hint { color: var(--muted); font-size: 12px; font-weight: 400; margin-left: 6px; }

/* ---------- filter rows with min/max */
.frow .minmax { display: flex; align-items: center; gap: 4px; color: var(--muted); }
.frow .minmax input, .frow .minmax select { width: 74px; flex: 0 0 auto; padding: 2px 5px; border-radius: 5px; border: 1px solid var(--grid); background: var(--page); color: var(--ink); font: inherit; font-size: 12px; font-variant-numeric: tabular-nums; }
.frow .minmax select { width: 64px; }
.frow.on .name { color: var(--accent); }
.frow.on .minmax input, .frow.on .minmax select { border-color: var(--axis); }
.frow canvas { cursor: pointer; }

/* ---------- detail: which filters this address meets */
.verdict { list-style: none; padding: 0; margin: 0 0 10px; display: grid; gap: 2px; }
.verdict li span:first-child { display: inline-block; width: 16px; font-weight: 700; }
.verdict .ok span:first-child { color: #0ca30c; }
.verdict .no span:first-child { color: #d03b3b; }
.verdict .unk span:first-child { color: var(--muted); }
#detail .big { font-size: 20px; font-weight: 600; font-variant-numeric: tabular-nums; }
#scatter { height: 220px; }
