:root {
  --ink: #1d2521;
  --muted: #5f6b65;
  --line: #dfe4e1;
  --bg: #ffffff;
  --bg-soft: #f4f6f5;
  --brand: #1f3a2e;
  --accent: #d9480f;
  --panel-w: 360px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--ink);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--bg-soft); }
a { color: #1c6e8c; }

#map { position: absolute; inset: 0 0 0 var(--panel-w); }

#panel {
  position: absolute; top: 0; left: 0; bottom: 0; width: var(--panel-w);
  display: flex; flex-direction: column;
  background: var(--bg); border-right: 1px solid var(--line);
  z-index: 2;
}
#sheet-handle { display: none; }

header { padding: 14px 16px 0; border-bottom: 1px solid var(--line); }
h1 { margin: 0 0 10px; font-size: 1.15rem; color: var(--brand); letter-spacing: .01em; }
h1 small { font-weight: 500; font-size: .7rem; text-transform: uppercase; color: var(--muted); margin-left: 4px; }

#tabs { display: flex; gap: 2px; }
#tabs button {
  flex: 1; padding: 9px 4px 10px; border: 0; background: none; cursor: pointer;
  font: inherit; font-size: .92rem; font-weight: 600; color: var(--muted);
  border-bottom: 3px solid transparent;
}
#tabs button[aria-selected="true"] { color: var(--brand); border-bottom-color: var(--brand); }
#tabs button:hover { color: var(--ink); }

#panel-body { flex: 1; overflow-y: auto; padding: 4px 16px 16px; }
footer { padding: 10px 16px; font-size: .72rem; color: var(--muted); border-top: 1px solid var(--line); line-height: 1.4; }

.intro { color: var(--muted); font-size: .88rem; line-height: 1.45; margin: 12px 0 4px; }

.group h2 {
  font-size: .72rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted);
  margin: 18px 0 6px;
}
.toggle { display: flex; align-items: center; gap: 9px; padding: 5px 0; font-size: .9rem; cursor: pointer; }
.toggle input { margin: 0; accent-color: var(--brand); }
.toggle .count { margin-left: auto; color: var(--muted); font-size: .78rem; font-variant-numeric: tabular-nums; }
.swatch { width: 14px; height: 14px; flex: none; border-radius: 50%; border: 1.5px solid #fff; box-shadow: 0 0 0 1px rgba(0,0,0,.25); }
.swatch.line { border-radius: 2px; height: 4px; border: 0; box-shadow: none; }
.swatch.area { border-radius: 3px; }
.swatch.map { border-radius: 3px; background: repeating-linear-gradient(45deg, #c9b38a, #c9b38a 3px, #e9dcc0 3px, #e9dcc0 6px); }
.opacity { display: flex; align-items: center; gap: 8px; margin: 0 0 6px 23px; font-size: .75rem; color: var(--muted); }
.opacity input { flex: 1; accent-color: var(--brand); }
.opacity[hidden] { display: none; }
.legend { margin: 2px 0 6px 23px; display: grid; grid-template-columns: 1fr 1fr; gap: 3px 10px; font-size: .75rem; color: var(--muted); }
.legend span { display: flex; align-items: center; gap: 5px; }
.legend i { width: 11px; height: 11px; border-radius: 2px; border: 1px solid rgba(0,0,0,.2); flex: none; }

.stories { list-style: none; padding: 0; margin: 0; }
.stories li button {
  display: block; width: 100%; text-align: left; border: 0; background: none; cursor: pointer; font: inherit;
  padding: 7px 0; border-bottom: 1px solid var(--line); font-size: .86rem; line-height: 1.35;
}
.stories li button:hover strong { text-decoration: underline; }
.stories small { display: block; color: var(--muted); font-size: .74rem; margin-top: 2px; }

#detail {
  margin: 12px 0 4px; padding: 12px 14px; border-radius: 10px;
  background: var(--bg-soft); border: 1px solid var(--line); position: relative;
}
:is(#detail, .info) .kind { font-size: .68rem; font-weight: 700; text-transform: uppercase; letter-spacing: .07em; color: var(--muted); }
:is(#detail, .info) h3 { margin: 3px 26px 6px 0; font-size: 1.02rem; line-height: 1.3; }
:is(#detail, .info) p { margin: 6px 0; font-size: .86rem; line-height: 1.45; }
:is(#detail, .info) .meta { color: var(--muted); font-size: .78rem; }
#detail .close { position: absolute; top: 8px; right: 8px; border: 0; background: none; font-size: 1.3rem; line-height: 1; cursor: pointer; color: var(--muted); }
:is(#detail, .info) .actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.btn { display: inline-block; padding: 7px 12px; border-radius: 7px; font-size: .84rem; font-weight: 600; text-decoration: none; border: 1px solid var(--line); background: #fff; color: var(--ink); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }

#loading {
  position: absolute; top: 12px; left: calc(var(--panel-w) + 12px); z-index: 3;
  background: #fff; padding: 6px 12px; border-radius: 6px; font-size: .82rem; box-shadow: 0 1px 4px rgba(0,0,0,.2);
}
#loading[hidden] { display: none; }

/* Phones: panel becomes a bottom sheet */
@media (max-width: 700px) {
  #map { inset: 0; }
  #loading { left: 12px; }
  #panel {
    top: auto; width: auto; right: 0; height: 52dvh;
    border-right: 0; border-top: 1px solid var(--line);
    border-radius: 14px 14px 0 0; box-shadow: 0 -2px 12px rgba(0,0,0,.15);
    transition: height .2s ease;
    padding-bottom: env(safe-area-inset-bottom);
  }
  #panel.collapsed-mobile { height: 154px; }
  #panel.collapsed-mobile #panel-body, #panel.collapsed-mobile footer { visibility: hidden; }
  #sheet-handle { display: block; width: 100%; height: 18px; border: 0; background: none; cursor: pointer; padding: 6px 0 0; }
  #sheet-handle span { display: block; width: 40px; height: 4px; border-radius: 2px; background: #c3cac6; margin: 0 auto; }
  header { padding-top: 2px; }
  h1 { font-size: 1rem; margin-bottom: 4px; }
  footer { display: none; }
  .mapboxgl-ctrl-bottom-right, .mapboxgl-ctrl-bottom-left { bottom: 154px; }
}
.zoomhint { font-size: .7rem; }
.zoomhint[hidden] { display: none; }
.swatch-icon { width: 16px; height: 18px; flex: none; }
.raster-extra[hidden] { display: none; }
.raster-extra .opacity { margin-bottom: 4px; }
.legend-head { grid-column: 1 / -1; font-size: .68rem; text-transform: uppercase; letter-spacing: .05em; margin-top: 4px; color: var(--ink); font-weight: 600; }
.legend i.hatch { background-image: repeating-linear-gradient(45deg, transparent 0 2px, rgba(255,255,255,.9) 2px 4px); }

/* Byline beside the title */
.title-row { display: flex; flex-wrap: wrap; align-items: baseline; justify-content: space-between; gap: 0 10px; margin-bottom: 10px; }
.title-row h1 { margin: 0; white-space: nowrap; }
.byline { flex: none; font-size: .78rem; color: var(--muted); text-decoration: none; }
.byline:hover { color: var(--brand); text-decoration: underline; }
@media (max-width: 700px) {
  .title-row { margin-bottom: 4px; }
}

/* Map toolbar: basemap switcher */
#map-toolbar { position: absolute; top: 10px; left: calc(var(--panel-w) + 10px); z-index: 3; display: flex; gap: 8px; }
#basemap-switch { display: flex; background: #fff; border-radius: 6px; box-shadow: 0 0 0 2px rgba(0,0,0,.1); overflow: hidden; }
#basemap-switch button { border: 0; background: none; padding: 6px 11px; font: inherit; font-size: .8rem; font-weight: 600; color: var(--muted); cursor: pointer; }
#basemap-switch button[aria-pressed="true"] { background: var(--brand); color: #fff; }
#loading { top: 50px; }
@media (max-width: 700px) {
  #map-toolbar { left: 10px; }
}

/* Photos in the detail panel */
.photos { margin: 8px 0 6px; }
.photo-main { display: block; border-radius: 8px; overflow: hidden; background: #e9ecea; }
.photo-main img { display: block; width: 100%; height: auto; max-height: 55vh; object-fit: contain; }  /* whole photo, never cropped */
.photos figcaption { font-size: .74rem; color: var(--muted); margin-top: 4px; }
.photo-strip { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.photo-strip button { flex: none; padding: 0; border: 2px solid transparent; border-radius: 6px; overflow: hidden; cursor: pointer; background: none; line-height: 0; }
.photo-strip button.active { border-color: var(--brand); }
.photo-strip img { width: 56px; height: 42px; object-fit: cover; }

/* Collapsible legends */
details > summary { cursor: pointer; list-style: none; display: flex; align-items: center; gap: 6px; }
details > summary::-webkit-details-marker { display: none; }
details > summary::before { content: "\25B8"; font-size: .7rem; color: var(--muted); transition: transform .15s; }
details[open] > summary::before { transform: rotate(90deg); }
.group details > summary h2 { margin: 0; }
.group > details { margin: 18px 0 6px; }
.group > details > summary { margin-bottom: 6px; }
.legend-row { cursor: default; }
.legend-box { margin: 2px 0 6px 23px; }
.legend-box > summary { font-size: .75rem; color: var(--muted); padding: 2px 0; }
.legend-box .legend { margin-left: 0; }
.legend-img { display: block; width: 100%; max-width: 280px; height: auto; margin: 4px 0; border-radius: 4px; }

.swatch-icon.story { width: 18px; height: 18px; }

/* Data tab */
.viz-ctl { display: flex; align-items: center; gap: 10px; margin: 10px 0; font-size: .85rem; }
.viz-ctl > span { flex: none; width: 82px; font-size: .72rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
.viz-ctl select { flex: 1; min-width: 0; font: inherit; padding: 5px 6px; border: 1px solid var(--line); border-radius: 6px; background: var(--bg); }
.viz-pick select { font-weight: 600; color: var(--brand); }
.viz-about, .viz-note { font-size: .8rem; color: var(--muted); line-height: 1.45; margin: 6px 0 10px; }
.viz-note { margin-top: 14px; font-size: .72rem; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 6px; overflow: hidden; }
.seg button { border: 0; background: var(--bg); font: inherit; font-size: .8rem; padding: 5px 10px; cursor: pointer; color: var(--muted); }
.seg button + button { border-left: 1px solid var(--line); }
.seg button[aria-pressed="true"] { background: var(--brand); color: #fff; }
.year-slider { flex: 1; }
.year-slider input { width: 100%; margin: 0; accent-color: var(--brand); }
.year-slider .ticks { display: flex; justify-content: space-between; }
.year-slider .ticks button { border: 0; background: none; font: inherit; font-size: .78rem; padding: 2px 0; cursor: pointer; color: var(--muted); }
.year-slider .ticks button[aria-pressed="true"] { color: var(--ink); font-weight: 700; }
.viz-summary { background: var(--bg-soft); border-radius: 6px; padding: 8px 10px; font-size: .8rem; line-height: 1.5; margin: 10px 0; }
.viz-legend { margin: 8px 0 4px; }
.chart-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin: 16px 0 6px; }
.chart-head h2 { margin: 0; font-size: .72rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); line-height: 1.35; }
.chart-head .seg button { padding: 3px 8px; font-size: .72rem; }
.bars { display: flex; flex-direction: column; }
.bar-row { display: grid; grid-template-columns: 104px 1fr 78px; align-items: center; gap: 6px; border: 0; background: none;
  font: inherit; font-size: .76rem; padding: 1px 2px; cursor: pointer; text-align: left; border-radius: 3px; }
.bar-row:hover, .bar-row.hover { background: #fff3bf; }
.bar-row.sel { background: #ffe066; font-weight: 600; }
.bar-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-track { position: relative; height: 11px; }
.bar-track i { position: absolute; top: 0; bottom: 0; border-radius: 2px; box-shadow: inset 0 0 0 1px rgba(0,0,0,.12); }
.bar-track b { position: absolute; top: -2px; bottom: -2px; width: 1px; background: #868e96; }
.bar-val { text-align: right; color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }
:is(#detail, .info) h4 { margin: 12px 0 4px; font-size: .72rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
.stack { display: flex; align-items: center; gap: 8px; margin: 3px 0; }
.stack-label { width: 34px; font-size: .75rem; color: var(--muted); font-variant-numeric: tabular-nums; }
.stack-bar { flex: 1; display: flex; height: 14px; border-radius: 3px; overflow: hidden; background: var(--bg-soft); }
.stack-bar i { height: 100%; }
.stack-key { margin: 6px 0 4px; }
#detail .vote-line { margin: 0 0 6px 42px; font-size: .75rem; color: var(--muted); }
dl.stats { display: grid; grid-template-columns: 1fr auto; gap: 2px 10px; font-size: .8rem; margin: 12px 0 0; }
dl.stats dt { color: var(--muted); }
dl.stats dt.cur, dl.stats dt.cur + dd { color: var(--ink); font-weight: 700; }
dl.stats dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }
.viz-tip .mapboxgl-popup-content { padding: 6px 9px; font-size: .8rem; line-height: 1.4; box-shadow: 0 2px 8px rgba(0,0,0,.2); }
.viz-tip small { color: var(--muted); }

/* Feature card in a map popup (map tabs) */
.info .mapboxgl-popup-content { padding: 12px 14px; border-radius: 10px; box-shadow: 0 3px 14px rgba(0,0,0,.25);
  max-height: min(60vh, 460px); overflow-y: auto; }
.info .mapboxgl-popup-close-button { font-size: 1.3rem; padding: 2px 8px; color: var(--muted); }
.info h3 { margin-right: 18px; }
.info .mapboxgl-popup-content { width: 300px; max-width: calc(100vw - 40px); box-sizing: border-box; }
.info .photo-main img { max-height: 200px; }

/* Search: under the title, results drop over the panel */
#search { position: relative; margin-bottom: 8px; }
#search-input {
  width: 100%; padding: 8px 10px; border: 1px solid var(--line); border-radius: 6px;
  background: var(--bg-soft); color: var(--ink); font: inherit; font-size: .9rem;
}
#search-input:focus { outline: 2px solid var(--brand); outline-offset: -1px; background: var(--bg); }
#search-results {
  position: absolute; top: 100%; left: 0; right: 0; z-index: 5; margin: 4px 0 0; padding: 4px 0; list-style: none;
  max-height: min(60vh, 420px); overflow-y: auto; background: var(--bg); border: 1px solid var(--line); border-radius: 8px;
  box-shadow: 0 4px 18px rgba(0,0,0,.18);
}
#search-results[hidden] { display: none; }
#search-results li { padding: 7px 10px; cursor: pointer; }
#search-results li:hover, #search-results li[aria-selected="true"] { background: var(--bg-soft); }
#search-results strong { display: block; font-size: .88rem; font-weight: 600; }
#search-results small { color: var(--muted); font-size: .75rem; }
#search-results .none { color: var(--muted); font-size: .85rem; cursor: default; }
@media (max-width: 700px) {
  #search { margin-bottom: 4px; }
  #search-input { font-size: 1rem; }  /* under 16px, iPhones zoom the page on focus */
  #search-results { max-height: 34dvh; }
}

/* Several stories at one spot: the popup lists them */
.story-picks { list-style: none; margin: 4px 0 0; padding: 0; max-height: 260px; overflow-y: auto; }
.story-picks button { display: block; width: 100%; padding: 7px 4px; border: 0; border-top: 1px solid var(--line); background: none; font: inherit; text-align: left; cursor: pointer; }
.story-picks li:first-child button { border-top: 0; }
.story-picks button:hover { background: var(--bg-soft); }
.story-picks strong { display: block; font-size: .86rem; line-height: 1.3; }
.story-picks small { color: var(--muted); font-size: .74rem; }
.all-stories { display: block; margin: 0 0 6px; padding: 0; border: 0; background: none; font: inherit; font-size: .78rem; color: #1c6e8c; cursor: pointer; }
.all-stories:hover { text-decoration: underline; }
