:root {
  --bg: #ffffff;
  --panel: #f7f7f5;
  --ink: #1a1a1a;
  --muted: #6b6b6b;
  --line: #e0e0dc;
  --accent: #14625b;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14141a;
    --panel: #1c1c24;
    --ink: #ececf0;
    --muted: #9a9aa5;
    --line: #2e2e3a;
    --accent: #4dd0c0;
  }
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body { height: 100%; }

body {
  font: 14px/1.5 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--ink);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

#app { display: flex; height: 100vh; }

#map { flex: 1; min-width: 0; }

#panel {
  width: 320px;
  flex-shrink: 0;
  background: var(--panel);
  border-right: 1px solid var(--line);
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

#panel header h1 {
  font-size: 19px;
  font-weight: 650;
  letter-spacing: -0.01em;
}

#metro-sub { color: var(--muted); font-size: 13px; margin-top: 2px; }

#modes { display: flex; gap: 6px; }

#modes button {
  flex: 1;
  padding: 7px 10px;
  font: inherit;
  font-size: 13px;
  color: var(--muted);
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 7px;
  cursor: pointer;
  transition: all .15s;
}

#modes button:hover { color: var(--ink); }

#modes button.active {
  color: var(--bg);
  background: var(--accent);
  border-color: var(--accent);
  font-weight: 600;
}

h2 {
  font-size: 11px;
  font-weight: 650;
  text-transform: uppercase;
  letter-spacing: .07em;
  color: var(--muted);
  margin-bottom: 9px;
}

.legend-row {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 3px 0;
  font-size: 13px;
}

h2 .hint {
  font-weight: 400;
  text-transform: none;
  letter-spacing: 0;
  opacity: .75;
}

button.legend-row.pick {
  width: 100%;
  font: inherit;
  font-size: 13px;
  color: var(--ink);
  background: transparent;
  border: 0;
  border-radius: 5px;
  padding: 3px 6px;
  margin-left: -6px;
  cursor: pointer;
  text-align: left;
  transition: background .12s;
}

button.legend-row.pick:hover { background: rgba(128, 128, 128, .16); }

button.back {
  margin-top: 10px;
  font: inherit;
  font-size: 12px;
  color: var(--muted);
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 4px 9px;
  cursor: pointer;
}

button.back:hover { color: var(--ink); }

.swatch {
  width: 15px; height: 15px;
  border-radius: 3px;
  flex-shrink: 0;
  border: 1px solid rgba(128,128,128,.35);
}

.ramp {
  height: 11px;
  border-radius: 3px;
  border: 1px solid var(--line);
  margin-bottom: 5px;
}

.ramp-labels {
  display: flex;
  justify-content: space-between;
  font-size: 11px;
  color: var(--muted);
}

table { width: 100%; border-collapse: collapse; font-size: 13px; }

td { padding: 3px 0; }

td.val { text-align: right; font-variant-numeric: tabular-nums; color: var(--muted); }

td.name { display: flex; align-items: center; gap: 8px; }

#readout {
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  padding: 14px 0;
}

#readout h2 { margin-bottom: 4px; }
#readout h2 span { font-family: ui-monospace, monospace; text-transform: none; letter-spacing: 0; }

.ro-pop { font-size: 13px; color: var(--muted); margin-bottom: 9px; }
.ro-div { margin-top: 9px; font-size: 13px; color: var(--muted); }
.ro-div strong { color: var(--ink); }

footer {
  margin-top: auto;
  padding-top: 14px;
  border-top: 1px solid var(--line);
  font-size: 11.5px;
  line-height: 1.45;
  color: var(--muted);
}

footer p + p { margin-top: 5px; }
footer .note { font-style: italic; opacity: .85; }

/* ---- view switcher, top-right over the map ---- */

#map { position: relative; }

#viewbar {
  position: absolute;
  top: 12px; right: 56px;
  z-index: 5;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 10px 11px;
  box-shadow: 0 2px 10px rgba(0,0,0,.13);
  display: flex;
  flex-direction: column;
  gap: 7px;
  min-width: 172px;
}

.vb-label {
  font-size: 10.5px;
  font-weight: 650;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--muted);
}

.vb-row { display: flex; gap: 4px; }

.vb-row button {
  flex: 1;
  padding: 5px 8px;
  font: inherit;
  font-size: 12px;
  color: var(--muted);
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 6px;
  cursor: pointer;
  transition: all .13s;
}

.vb-row button:hover { color: var(--ink); }

.vb-row button.active {
  color: var(--bg);
  background: var(--accent);
  border-color: var(--accent);
  font-weight: 600;
}

#vb-group-wrap { display: flex; flex-direction: column; gap: 4px; }

#vb-metric-wrap { display: flex; flex-direction: column; gap: 4px; }

#vb-measure, #vb-group, #vb-metric, #vb-dataset {
  font: inherit;
  font-size: 12.5px;
  color: var(--ink);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 5px 7px;
  cursor: pointer;
}

/* ---- click popup ---- */

#popup {
  position: fixed;
  z-index: 20;
  width: 236px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 12px 13px 11px;
  box-shadow: 0 4px 18px rgba(0,0,0,.22);
  font-size: 13px;
  pointer-events: auto;
}

#popup h3 {
  font-size: 12.5px;
  font-weight: 650;
  margin-bottom: 3px;
  padding-right: 16px;
}

#popup h3 span:last-child {
  font-family: ui-monospace, monospace;
  font-weight: 400;
  font-size: 11px;
  color: var(--muted);
}

#pop-summary { color: var(--muted); font-size: 12px; margin-bottom: 8px; }

#pop-table { width: 100%; border-collapse: collapse; font-size: 12.5px; }
#pop-table td { padding: 2px 0; }
#pop-table td.num { text-align: right; font-variant-numeric: tabular-nums; }
#pop-table td.pct { text-align: right; font-variant-numeric: tabular-nums; color: var(--muted); width: 46px; }

/* The six-bar distribution shape under each housing median. Bars grow from a
   shared baseline, so the row reads as a histogram rather than a stack.
   Height is set for a fixed 0-100% bar scale: the median area's tallest band
   holds 48%, so the box needs enough room for that to read as half-full. */
.dist {
  display: flex;
  align-items: flex-end;
  gap: 2px;
  height: 34px;
  margin: 4px 0 1px;
}
.dist .distbar {
  flex: 1;
  height: 100%;
  display: flex;
  align-items: flex-end;
  background: rgba(128, 128, 128, .10);
  border-radius: 1px;
}
.dist .distbar i { display: block; width: 100%; background: #3d94a8; border-radius: 1px; }
.dist-labels {
  display: flex;
  justify-content: space-between;
  font-size: 10.5px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

#pop-foot {
  margin-top: 8px;
  padding-top: 7px;
  border-top: 1px solid var(--line);
  font-size: 11.5px;
  color: var(--muted);
}

#popup-close {
  position: absolute;
  top: 6px; right: 7px;
  width: 18px; height: 18px;
  line-height: 1;
  font-size: 15px;
  color: var(--muted);
  background: transparent;
  border: 0;
  border-radius: 4px;
  cursor: pointer;
}

#popup-close:hover { color: var(--ink); background: rgba(128,128,128,.18); }

.maplibregl-popup-content {
  font: inherit;
  font-size: 12px;
  padding: 7px 10px;
  border-radius: 6px;
}

@media (max-width: 780px) {
  #app { flex-direction: column-reverse; }
  #panel { width: 100%; height: 46vh; border-right: 0; border-top: 1px solid var(--line); }
  #map { height: 54vh; }
}

.legend-note {
  margin-top: 7px;
  font-size: 11px;
  line-height: 1.4;
  color: var(--muted);
}

#vb-groupset-wrap { display: flex; flex-direction: column; gap: 4px; }

#vb-groupset { display: flex; flex-direction: column; gap: 1px; }

#vb-groupset label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  cursor: pointer;
  padding: 1px 0;
}

#vb-groupset input { margin: 0; cursor: pointer; }

#vb-groupset .sw {
  width: 10px; height: 10px;
  border-radius: 2px;
  border: 1px solid rgba(128,128,128,.4);
}

/* The HTML `hidden` attribute only sets `display: none` as a low-priority
 * default, so any `display` rule on the same element silently defeats it —
 * which is what left the diversity checkboxes on screen under Population. */
[hidden] { display: none !important; }

/* ---- hover descriptions ---- */

#tip {
  position: fixed;
  z-index: 40;
  max-width: 250px;
  background: var(--ink);
  color: var(--bg);
  font-size: 11.5px;
  line-height: 1.45;
  padding: 7px 9px;
  border-radius: 6px;
  box-shadow: 0 3px 12px rgba(0, 0, 0, .3);
  pointer-events: none;
  opacity: 0;
  transition: opacity .12s;
}

#tip.on { opacity: 1; }

#tip strong { display: block; margin-bottom: 2px; }

[data-tip] { cursor: help; }
.vb-row [data-tip], select[data-tip] { cursor: pointer; }

/* ---- compass, bottom-left over the map ---- */

#compass {
  position: absolute;
  left: 14px; bottom: 30px;
  z-index: 6;
  width: 54px; height: 54px;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: var(--panel);
  box-shadow: 0 2px 10px rgba(0,0,0,.16);
  cursor: grab;
  display: grid;
  place-items: center;
  transition: box-shadow .15s, border-color .15s;
}

#compass:hover { border-color: var(--accent); box-shadow: 0 3px 14px rgba(0,0,0,.24); }
#compass:active { cursor: grabbing; }

#compass svg { display: block; }

.c-face { fill: none; stroke: var(--line); stroke-width: 1.5; }
.c-n { fill: #d1495b; }
.c-s { fill: var(--muted); opacity: .55; }

#c-label {
  position: absolute;
  bottom: -17px;
  font-size: 10px;
  font-weight: 650;
  letter-spacing: .04em;
  color: var(--muted);
  white-space: nowrap;
}
