:root {
  color-scheme: light;
  --surface: #fcfcfb;
  --panel: #f4f3f0;
  --line: #dddcd7;
  --text: #0b0b0b;
  --text-2: #52514e;
  --text-3: #75746f;
  --accent: #2a78d6;
  --nodata: #e6e5e1;
  --hatch: #b9b8b2;
  --border: #ffffff;
  --us: #75746f;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --surface: #1a1a19; --panel: #232322; --line: #3a3a37;
    --text: #f0efec; --text-2: #c3c2b7; --text-3: #9a998f;
    --accent: #3987e5; --nodata: #2c2c2a; --hatch: #5a5a55; --border: #1a1a19; --us: #9a998f;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --surface: #1a1a19; --panel: #232322; --line: #3a3a37;
  --text: #f0efec; --text-2: #c3c2b7; --text-3: #9a998f;
  --accent: #3987e5; --nodata: #2c2c2a; --hatch: #5a5a55; --border: #1a1a19; --us: #9a998f;
}

* { box-sizing: border-box; }
body {
  margin: 0; background: var(--surface); color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif;
  max-width: 1400px; margin-inline: auto; padding-block: 0 48px;
  padding-left: max(16px, env(safe-area-inset-left, 0px)); padding-right: max(16px, env(safe-area-inset-right, 0px));
}
h1 { font-size: 1.7rem; margin: 24px 0 4px; }
h2 { font-size: 1.15rem; margin: 0 0 4px; }
h3 { font-size: 1rem; margin: 16px 0 4px; }
a { color: var(--accent); }
.lede { color: var(--text-2); margin: 0 0 16px; }
.muted { color: var(--text-2); }
.small { font-size: .85rem; }

.linkBtn { font: inherit; font-size: .9rem; background: none; border: 0; color: var(--accent); cursor: pointer; padding: 8px 4px; }
.seg { display: inline-flex; }
.seg button, select, #play, #fopen, #fdone {
  font: inherit; font-size: .9rem; color: var(--text); background: var(--panel);
  border: 1px solid var(--line); border-radius: 999px; padding: 5px 12px; cursor: pointer;
}
.seg button { border-radius: 0; }
.seg button:first-child { border-radius: 999px 0 0 999px; }
.seg button:last-child { border-radius: 0 999px 999px 0; margin-left: -1px; }
.seg button[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: #fff; }
button:disabled { opacity: .45; cursor: not-allowed; }
select { border-radius: 8px; }
select:disabled { opacity: .5; }
.note { flex-basis: 100%; margin: 0; font-size: .9rem; color: var(--text-2); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Filter rail on the left (Viv patterns: accordion groups, Clear per group, phone drawer) */
.shell { display: grid; grid-template-columns: 240px minmax(0, 1fr); gap: 28px; border-top: 1px solid var(--line); }
.content { min-width: 0; }
.facets { position: sticky; top: 0; align-self: start; max-height: 100vh; overflow: auto; padding: 8px 6px 16px 0; }
.drawerTop, .drawerBottom, .fbar { display: none; }
.facet { border-bottom: 1px solid var(--line); padding: 8px 0; }
.facet > summary { list-style: none; display: flex; align-items: center; gap: 6px; min-height: 32px; cursor: pointer; font-weight: 600; }
.facet > summary::-webkit-details-marker { display: none; }
.facet__name { flex: 1; min-width: 0; }
.facet .chev { width: 24px; display: inline-grid; place-items: center; color: var(--text-3); transition: transform .15s; }
.facet[open] .chev { transform: rotate(180deg); }
.facet__clear { flex: none; width: 22px; height: 22px; border-radius: 50%; border: 0; background: var(--panel); color: var(--accent); font: 600 15px/1 system-ui, sans-serif; padding: 0; cursor: pointer; display: grid; place-items: center; }
.facet__clear:hover, .facet__clear:focus-visible { background: var(--accent); color: #fff; }
.facet ul { list-style: none; margin: 6px 0 0; padding: 0; }
.facet label { display: flex; gap: 8px; align-items: baseline; font-size: .92rem; padding: 3px 0; cursor: pointer; }
.facet input { accent-color: var(--accent); }
.facet .flab { flex: 1; min-width: 0; }
.fnote { font-size: .8rem; color: var(--text-2); margin: 4px 0; }
.facet__body { padding-bottom: 4px; }

.viewbar { display: flex; flex-wrap: wrap; gap: 10px 14px; align-items: center; padding: 12px 0 4px; }

@media (max-width: 900px) {
  .shell { grid-template-columns: 1fr; gap: 0; }
  .fbar { display: flex; gap: 8px; align-items: center; padding: 10px 0; border-top: 1px solid var(--line); }
  #fopen { min-height: 44px; padding-inline: 18px; }
  .facets { display: none; }
  .drawer-open { overflow: hidden; }
  .drawer-open .facets {
    display: flex; flex-direction: column; position: fixed; inset: 0; height: 100vh; height: 100dvh; z-index: 1100; max-height: none;
    background: var(--surface); padding: 0 max(16px, env(safe-area-inset-right, 0px)) 0 max(16px, env(safe-area-inset-left, 0px));
  }
  .drawer-open .drawerTop {
    display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; position: sticky; top: 0;
    background: var(--surface); border-bottom: 1px solid var(--line); padding-top: env(safe-area-inset-top, 0px); z-index: 1;
  }
  .drawerTop h2 { margin: 0; font-size: 1rem; }
  .drawerTop .linkBtn { justify-self: start; min-height: 44px; }
  #fclose { justify-self: end; width: 44px; height: 44px; border: 0; background: none; color: var(--text); font-size: 1.2rem; cursor: pointer; }
  .drawer-open #groups { flex: 1; overflow: auto; }
  .drawer-open .drawerBottom { display: block; position: sticky; bottom: 0; background: var(--surface); padding: 10px 0 max(10px, env(safe-area-inset-bottom, 0px)); border-top: 1px solid var(--line); }
  #fdone { width: 100%; min-height: 48px; background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
  .facet label { padding: 8px 0; }
}

.years { padding: 4px 0 16px; border-bottom: 1px solid var(--line); }
.yearHead { display: flex; align-items: baseline; gap: 12px; }
#play { width: 40px; height: 40px; padding: 0; border-radius: 50%; align-self: center; }
#yearOut { font-size: 2.4rem; font-weight: 700; font-variant-numeric: tabular-nums; }
#year { width: 100%; height: 36px; accent-color: var(--accent); cursor: pointer; margin: 4px 0 0; }
.ticks { position: relative; height: 18px; font-size: .75rem; color: var(--text-3); }
.ticks span { position: absolute; transform: translateX(-50%); white-space: nowrap; }
.ticks .break { color: var(--text-2); }
.ticks .break::before { content: ""; position: absolute; left: 50%; top: -26px; height: 22px; border-left: 2px dashed var(--text-3); }

.layout { display: grid; grid-template-columns: minmax(0, 1fr) 300px; gap: 24px; margin-top: 16px; }
@media (max-width: 1150px) { .layout { grid-template-columns: 1fr; } }
.mapWrap { margin: 0; position: relative; }
#map { width: 100%; height: auto; display: block; }
#map path.area { stroke: var(--border); stroke-width: .5; cursor: pointer; }
#map path.area:hover, #map path.area.sel { stroke: var(--text); stroke-width: 1.5; }
#map .states-mesh { fill: none; stroke: var(--border); stroke-width: 1.2; pointer-events: none; }
.legend { display: flex; flex-wrap: wrap; gap: 4px 14px; font-size: .8rem; color: var(--text-2); margin-top: 8px; align-items: center; }
.legend .title { width: 100%; font-weight: 600; color: var(--text); }
.legend .sw { display: inline-flex; align-items: center; gap: 5px; }
.legend i { width: 18px; height: 12px; border-radius: 2px; display: inline-block; }
.tip {
  position: absolute; pointer-events: none; background: var(--surface); color: var(--text);
  border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; font-size: .85rem;
  box-shadow: 0 4px 16px rgb(0 0 0 / .15); max-width: 260px;
}
.tip b { display: block; }
.side { border-left: 1px solid var(--line); padding-left: 24px; }
@media (max-width: 1150px) { .side { border-left: 0; padding-left: 0; } }
#trend, #ageChart { width: 100%; height: auto; display: block; overflow: visible; }
svg text { fill: var(--text-2); font-size: 11px; }
.axis line, .axis path { stroke: var(--line); }
.grid line { stroke: var(--line); }
.ages, .sources { margin-top: 32px; padding-top: 16px; border-top: 1px solid var(--line); }
.sources li { margin-bottom: 4px; }
