:root {
  color-scheme: light;
  --bg: #f3efe4;
  --bg-2: #e7e0d0;
  --surface: #fffdf8;
  --ink: #1b1914;
  --muted: #5c564c;
  --line: #ddd4c2;
  --accent: #1f4f45;
  --accent-hover: #163f37;
  --accent-2: #6e4c22;
  --on-accent: #f4fbf8;
  --water: #d5e4e0;
  --map-stroke: #fffdf8;
  --map-stroke-strong: #1b1914;
  --scale-0: #f3e6cf;
  --scale-1: #6ea194;
  --scale-2: #143f36;
  --bar-track: #efe8da;
  --hover: #f4efe6;
  --selected: #e4f2ec;
  --selected-strong: #d3ebe2;
  --shadow: 0 16px 40px rgba(70, 48, 12, 0.06);
  --race-white: #cfc6b6;
  --race-black: #2f5f68;
  --race-asian: #c8922e;
  --race-hispanic: #c46a45;
  --race-other: #7e96b0;
  --focus: #6e4c22;
  --serif: "Iowan Old Style", Palatino, "Palatino Linotype", "Book Antiqua", Georgia, "Noto Serif", serif;
  --sans: "Avenir Next", "Segoe UI", ui-sans-serif, system-ui, "Noto Sans", sans-serif;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0e1311;
  --bg-2: #090c0b;
  --surface: #171d1a;
  --ink: #e8f0ea;
  --muted: #a8b5ab;
  --line: #2c3833;
  --accent: #e6c27a;
  --accent-hover: #f0d7a4;
  --accent-2: #e6c27a;
  --on-accent: #1c160c;
  --water: #0c1917;
  --map-stroke: #0e1311;
  --map-stroke-strong: #f6edd4;
  --scale-0: #1a3330;
  --scale-1: #3f8f7c;
  --scale-2: #e6c27a;
  --bar-track: #24302b;
  --hover: #1e2723;
  --selected: #24362f;
  --selected-strong: #2d453c;
  --shadow: 0 16px 40px rgba(0, 0, 0, 0.28);
  --race-white: #d9d3c7;
  --race-black: #7eb8b0;
  --race-asian: #e6c27a;
  --race-hispanic: #e08a68;
  --race-other: #9bb0c8;
  --focus: #e6c27a;
}

* { box-sizing: border-box; }

html {
  scroll-behavior: smooth;
  -webkit-tap-highlight-color: transparent;
}

@media (pointer: fine) {
  html { overscroll-behavior: none; }
}

body {
  margin: 0;
  background:
    radial-gradient(900px 420px at 0% -10%, var(--bg-2), transparent 60%),
    var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 16px;
  line-height: 1.5;
  overflow-x: clip;
}

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after {
    transition: none !important;
    animation: none !important;
  }
}

button, input { font: inherit; color: inherit; }
button, a { cursor: pointer; touch-action: manipulation; }
a { color: var(--accent-2); text-underline-offset: 3px; }
a:hover { color: var(--ink); }
:focus { outline: none; }
:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 3px;
}

h1, h2, .section-title, #detail {
  scroll-margin-top: 16px;
}

h1, h2, .hero-num, .section-title, .bay-pop strong, .story strong, .rank-badge strong {
  font-family: var(--serif);
  font-weight: 500;
  letter-spacing: -0.03em;
  text-wrap: balance;
}

.lede, .about, .fine, .rankline, .foot, .story span:last-child {
  text-wrap: pretty;
}

.skip {
  position: absolute;
  left: 12px;
  top: -48px;
  background: var(--ink);
  color: var(--bg);
  padding: 8px 12px;
  border-radius: 8px;
  z-index: 5;
}
.skip:focus { top: max(12px, env(safe-area-inset-top)); }

.wrap {
  max-width: 1120px;
  margin: 0 auto;
  padding: 20px max(22px, env(safe-area-inset-right)) max(72px, env(safe-area-inset-bottom)) max(22px, env(safe-area-inset-left));
}

.top {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  grid-template-areas:
    "kicker theme"
    "title pop"
    "lede lede";
  column-gap: 28px;
  align-items: center;
  margin-bottom: 14px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--line);
}
.top > * { min-width: 0; }

.kicker { grid-area: kicker; }
h1 { grid-area: title; }
.lede { grid-area: lede; }
.theme-btn { grid-area: theme; justify-self: end; }
.bay-pop { grid-area: pop; }

h1 {
  margin: 0.06em 0 0;
  font-size: clamp(2.05rem, 4vw, 2.85rem);
  line-height: 0.96;
}

.lede { margin: 6px 0 0; max-width: 42rem; color: var(--muted); }
.kicker {
  margin: 0;
  font-size: 0.72rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--accent-2);
  font-weight: 650;
}
.bay-pop { margin: 0; color: var(--muted); text-align: right; }
.bay-pop strong {
  display: block;
  color: var(--ink);
  font-size: clamp(1.8rem, 3vw, 2.35rem);
  line-height: 0.95;
  font-weight: 500;
  font-variant-numeric: tabular-nums;
}
.bay-pop span {
  display: block;
  margin-top: 4px;
  font-size: 0.72rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  font-weight: 650;
}

.theme-btn {
  min-height: 44px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--surface);
  padding: 0 14px;
  transition: background-color 0.15s ease, border-color 0.15s ease, color 0.15s ease;
}
.theme-btn:hover { border-color: var(--accent); background: var(--hover); }
.theme-btn:active { transform: translateY(1px); }

.toolbar { margin: 0 0 8px; }
.toolbar-label {
  margin: 0 0 6px;
  font-size: 0.72rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--muted);
  font-weight: 700;
}

.pills {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  min-width: 0;
  padding: 0;
}
.pills button {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  padding: 0 14px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--surface);
  transition: background-color 0.15s ease, color 0.15s ease, border-color 0.15s ease, transform 0.15s ease;
}
.pills button:hover { background: var(--hover); border-color: var(--accent); }
.pills button:active { transform: translateY(1px); }
.pills button[aria-checked="true"] {
  background: var(--accent);
  color: var(--on-accent);
  border-color: var(--accent);
  font-weight: 650;
}
.pills button[aria-checked="true"]:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
  color: var(--on-accent);
}

.about { margin: 0 0 10px; color: var(--muted); max-width: 48rem; }

.stage {
  display: grid;
  grid-template-columns: minmax(0, 1.25fr) minmax(280px, 0.85fr);
  gap: 0;
  align-items: stretch;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 20px;
  box-shadow: var(--shadow);
}
.stage > * { min-width: 0; }

.panel {
  background: transparent;
  border: 0;
  border-radius: 0;
  box-shadow: none;
  padding: 12px;
}
.rank-panel { border-left: 1px solid var(--line); }

.map-frame { position: relative; }
.map-svg {
  width: 100%;
  height: auto;
  display: block;
  background: var(--water);
  border-radius: 12px;
}
@media (min-width: 861px) {
  .map-svg { height: min(440px, calc(100dvh - 460px)); }
}
.map-svg path.county {
  stroke: var(--map-stroke);
  stroke-width: 1.4px;
  vector-effect: non-scaling-stroke;
  cursor: pointer;
  transition: opacity 0.15s linear;
}
.map-svg path.county[aria-pressed="true"],
.map-svg path.county:hover,
.map-svg path.county:focus-visible {
  stroke: var(--map-stroke-strong);
  stroke-width: 2.8px;
}
.map-svg .label {
  font-size: 13px;
  font-family: var(--sans);
  font-weight: 650;
  fill: #f7f4ec;
  stroke: #10211c;
  stroke-width: 3.5px;
  paint-order: stroke;
  pointer-events: none;
  user-select: none;
}

.map-status {
  position: absolute;
  z-index: 1;
  left: 10px;
  bottom: 10px;
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0;
  max-width: calc(100% - 20px);
  padding: 6px 12px 6px 8px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
  pointer-events: none;
}
.map-status-name { font-weight: 650; }
.map-status-value {
  font-variant-numeric: tabular-nums;
  font-weight: 650;
}
.swatch {
  width: 12px;
  height: 12px;
  border-radius: 99px;
  flex: 0 0 auto;
  box-shadow: inset 0 0 0 1px rgba(16, 33, 28, 0.25);
}

.legend {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  gap: 10px;
  align-items: center;
  margin-top: 10px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  font-size: 0.85rem;
}
.legend-bar {
  height: 10px;
  border-radius: 99px;
  background: linear-gradient(90deg, var(--scale-0), var(--scale-1) 50%, var(--scale-2));
}

.panel-head { padding: 2px 4px 0; }
.panel-head h2 { margin: 0; font-size: 1.45rem; }
.hint { margin: 2px 0 6px; color: var(--muted); font-size: 0.82rem; }

.filter {
  display: flex;
  flex-direction: column;
  gap: 6px;
  color: var(--muted);
  font-size: 0.85rem;
  margin: 0 4px 8px;
}
.filter:focus-within { color: var(--ink); }
.filter input {
  min-height: 44px;
  width: 100%;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--bg);
  padding: 0 12px;
  transition: border-color 0.15s ease, background-color 0.15s ease;
}
.filter input:hover { border-color: var(--accent); }
.filter input:focus-visible { border-color: var(--accent); }
.filter input::placeholder { color: var(--muted); }

.rank-list { display: flex; flex-direction: column; }
.rank-row {
  display: grid;
  grid-template-columns: 1.4rem minmax(0, 1fr) auto;
  gap: 8px;
  align-items: center;
  width: 100%;
  text-align: left;
  background: transparent;
  border: 0;
  border-radius: 10px;
  padding: 2px 8px;
  min-height: 36px;
  transition: background-color 0.15s ease;
}
.rank-row:hover { background: var(--hover); }
.rank-row[aria-pressed="true"] {
  background: var(--selected);
  box-shadow: inset 3px 0 0 var(--accent);
}
.rank-row[aria-pressed="true"]:hover { background: var(--selected-strong); }
.place { color: var(--muted); font-variant-numeric: tabular-nums; }
.rank-main { min-width: 0; display: flex; flex-direction: column; gap: 4px; }
.rank-name { font-weight: 650; overflow-wrap: anywhere; }
.rank-value { font-variant-numeric: tabular-nums; font-size: 0.92rem; font-weight: 650; }
.bar { display: block; height: 7px; border-radius: 99px; background: var(--bar-track); overflow: hidden; }
.bar > span { display: block; height: 100%; min-width: 2px; border-radius: 99px; }

.empty { margin: 12px 8px; color: var(--muted); overflow-wrap: anywhere; }
.text-btn {
  display: inline;
  min-height: 44px;
  margin-left: 6px;
  padding: 0;
  border: 0;
  background: none;
  color: var(--accent);
  font-weight: 650;
  text-decoration: underline;
  text-underline-offset: 3px;
}
.text-btn:hover { color: var(--ink); }

#app[aria-busy="true"]::before {
  content: "";
  display: block;
  height: 180px;
  margin-bottom: 12px;
  border-radius: 20px;
  border: 1px solid var(--line);
  background: var(--surface);
  animation: pulse 1.1s ease infinite;
}
@keyframes pulse {
  50% { opacity: 0.55; }
}

.detail { margin-top: 48px; animation: detail-in 0.32s ease; }
@keyframes detail-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: none; }
}
.detail-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  gap: 16px;
}
.detail-head h2 { margin: 0; font-size: clamp(1.8rem, 3vw, 2.5rem); }
.hero-num {
  margin: 4px 0 0;
  font-size: clamp(2.2rem, 5vw, 3.3rem);
  line-height: 1;
  font-variant-numeric: tabular-nums;
}
.rankline { margin: 6px 0 0; color: var(--muted); }
.rank-badge {
  margin: 0;
  min-width: 4.75rem;
  text-align: center;
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 16px;
  padding: 10px 12px 8px;
  box-shadow: var(--shadow);
}
.rank-badge strong {
  display: block;
  font-size: 1.8rem;
  line-height: 1;
  font-variant-numeric: tabular-nums;
}
.rank-badge span { color: var(--muted); font-size: 0.8rem; }

.facts {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
  margin-top: 16px;
}
.fact {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 14px 14px 12px;
  background: var(--surface);
}
.fact h3, .race-block h3, .wealth h3 {
  margin: 0 0 10px;
  font-size: 0.72rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--muted);
  font-weight: 700;
  font-family: var(--sans);
}
.fact dl { margin: 0; display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 7px 12px; }
.fact dt { color: var(--muted); overflow-wrap: anywhere; }
.fact dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; font-weight: 650; }

.tag {
  display: inline-block;
  margin-left: 6px;
  font-size: 0.68rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--accent-2);
}

.race-block, .wealth, .cities { margin-top: 12px; }
.race-block, .wealth {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 14px;
}
.race { display: flex; height: 14px; border-radius: 99px; overflow: hidden; background: var(--bar-track); }
.race i { display: block; height: 100%; }
.race-legend {
  list-style: none;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(148px, 1fr));
  gap: 8px 12px;
  padding: 12px 0 0;
  margin: 0;
  font-variant-numeric: tabular-nums;
}
.race-legend li { display: flex; align-items: center; gap: 6px; min-width: 0; }
.race-legend i { width: 10px; height: 10px; border-radius: 99px; display: block; flex: 0 0 auto; }
.fine { margin: 8px 0 0; color: var(--muted); font-size: 0.85rem; }
.wealth h3 { margin-bottom: 8px; }
.wealth .bar { height: 10px; margin: 0 0 4px; }
.wealth .fine { margin-bottom: 0; }

.section-title { font-size: 1.7rem; margin: 36px 0 12px; }
.stories { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; }
.story {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 6px;
  text-align: left;
  min-height: 100%;
  min-width: 0;
  border-radius: 16px;
  border: 1px solid var(--line);
  background: var(--surface);
  padding: 14px;
  transform-origin: center;
  transition: transform 0.18s ease, border-color 0.18s ease, background-color 0.18s ease;
}
.story:hover { transform: translateY(-2px); border-color: var(--accent); }
.story:active { transform: translateY(0); }
.story[aria-pressed="true"] {
  background: var(--selected);
  border-color: var(--accent);
  box-shadow: inset 0 3px 0 var(--accent);
}
.story strong {
  font-size: 1.15rem;
  line-height: 1.2;
}
.story span:last-child { color: var(--muted); font-size: 0.92rem; overflow-wrap: anywhere; }

.city-note { margin-top: 0; }
.cities {
  padding: 8px 14px 12px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 16px;
  overflow-x: auto;
}
.city-table { width: 100%; border-collapse: collapse; }
.city-table th, .city-table td {
  text-align: left;
  padding: 0 8px;
  border-top: 1px solid var(--line);
  vertical-align: middle;
  font-weight: inherit;
}
.city-table thead th {
  border-top: 0;
  font-size: 0.72rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  font-weight: 700;
  padding-top: 6px;
  padding-bottom: 6px;
}
.city-table .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.linkish {
  background: none;
  border: 0;
  padding: 6px 0;
  min-height: 44px;
  text-align: left;
  font-weight: 650;
  text-decoration: underline;
  text-decoration-color: var(--line);
  text-underline-offset: 3px;
  transition: color 0.15s ease, text-decoration-color 0.15s ease;
}
.linkish:hover { color: var(--accent); text-decoration-color: var(--accent); }

.foot {
  margin-top: 36px;
  padding-top: 16px;
  border-top: 1px solid var(--line);
  color: var(--muted);
  font-size: 0.85rem;
  max-width: 72ch;
}
.foot p { margin: 0 0 8px; }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

@media (max-width: 980px) {
  .stories { grid-template-columns: 1fr 1fr; }
}

@media (max-width: 860px) {
  .stage, .facts { grid-template-columns: 1fr; }
  .rank-panel { border-left: 0; border-top: 1px solid var(--line); }
}

@media (pointer: coarse) {
  .rank-row { min-height: 44px; }
}

@media (max-width: 640px) {
  .wrap { padding: 16px max(16px, env(safe-area-inset-right)) max(48px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left)); }
  .top {
    grid-template-areas:
      "kicker theme"
      "title title"
      "lede lede"
      "pop pop";
    column-gap: 12px;
  }
  .top .kicker { font-size: 0.68rem; letter-spacing: 0.06em; white-space: nowrap; }
  .theme-btn { padding: 0 12px; }
  .bay-pop { text-align: left; margin-top: 8px; }
  .bay-pop strong { font-size: 1.7rem; }
  .lede { margin-top: 4px; }
  .toolbar { margin-bottom: 6px; }
  .about { margin-bottom: 8px; }
  .pills {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 6px;
  }
  .pills button {
    min-width: 0;
    padding: 4px 2px;
    font-size: 0.7rem;
    letter-spacing: -0.02em;
    line-height: 1.15;
    white-space: normal;
    text-align: center;
  }
  @media (max-width: 359px) {
    .pills { grid-template-columns: 1fr 1fr; }
    .pills button { font-size: 0.85rem; letter-spacing: 0; padding: 0 8px; }
  }
  .stories { grid-template-columns: 1fr; }
  .map-svg .label { font-size: 11px; }
  .map-status { left: 8px; bottom: 8px; max-width: calc(100% - 16px); }
  .rank-row { min-height: 44px; }
  .detail-head { align-items: flex-start; }
  .facts { gap: 10px; }
  .cities { padding-left: 10px; padding-right: 10px; overflow-x: visible; }
  .city-table { table-layout: fixed; font-size: 0.88rem; }
  .city-table th, .city-table td { padding-left: 4px; padding-right: 4px; }
  .city-table thead th.num { white-space: normal; line-height: 1.2; }
  .city-table th:nth-child(1), .city-table td:nth-child(1) { width: 28%; }
  .city-table th:nth-child(2), .city-table td:nth-child(2) { width: 24%; }
  .city-table th:nth-child(3), .city-table td:nth-child(3) { width: 22%; }
  .city-table th:nth-child(4), .city-table td:nth-child(4) { width: 26%; }
}
