:root {
  --surface: #fcfcfb;
  --page: #f9f9f7;
  --raised: #ffffff;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #898781;
  --line: #e1e0d9;
  --line-2: #c3c2b7;
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --hover: #f0efec;
  --c-settlement: #2a78d6;
  --c-hill: #eb6834;
  --c-water: #1baf7a;
  --c-land: #eda100;
  --c-camp: #e87ba4;
  --c-fort: #008300;
  --dot-ring: #0b0b0b;
  color-scheme: light;
  --f-dravidian: #2a78d6; --f-indo-aryan: #eb6834; --f-perso-arabic: #1baf7a;
  --f-austroasiatic: #eda100; --f-tibeto-burman: #e87ba4;
  --seq-1: #cde2fb; --seq-2: #9ec5f4; --seq-3: #6da7ec; --seq-4: #3987e5; --seq-5: #256abf; --seq-6: #104281;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --surface: #1a1a19; --page: #0d0d0d; --raised: #232322;
    --ink: #f0efec; --ink-2: #c3c2b7; --muted: #898781;
    --line: #2c2c2a; --line-2: #383835; --accent: #3987e5; --hover: #2c2c2a;
    --c-settlement: #3987e5; --c-hill: #d95926; --c-water: #199e70;
    --c-land: #c98500; --c-camp: #d55181; --c-fort: #008300;
    --dot-ring: #0d0d0d;
    color-scheme: dark;
  
    --f-dravidian: #3987e5; --f-indo-aryan: #d95926; --f-perso-arabic: #199e70;
    --f-austroasiatic: #c98500; --f-tibeto-burman: #d55181;
    --seq-1: #104281; --seq-2: #184f95; --seq-3: #256abf; --seq-4: #3987e5; --seq-5: #6da7ec; --seq-6: #b7d3f6;
  }
}
:root[data-theme="dark"] {
  --surface: #1a1a19; --page: #0d0d0d; --raised: #232322;
  --ink: #f0efec; --ink-2: #c3c2b7; --muted: #898781;
  --line: #2c2c2a; --line-2: #383835; --accent: #3987e5; --hover: #2c2c2a;
  --c-settlement: #3987e5; --c-hill: #d95926; --c-water: #199e70;
  --c-land: #c98500; --c-camp: #d55181; --c-fort: #008300;
  --dot-ring: #0d0d0d;
  color-scheme: dark;

  --f-dravidian: #3987e5; --f-indo-aryan: #d95926; --f-perso-arabic: #199e70;
  --f-austroasiatic: #c98500; --f-tibeto-burman: #d55181;
  --seq-1: #104281; --seq-2: #184f95; --seq-3: #256abf; --seq-4: #3987e5; --seq-5: #6da7ec; --seq-6: #b7d3f6;
  }

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--page);
  color: var(--ink);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
button, input, select { font: inherit; color: inherit; }

#app { display: flex; height: 100vh; height: 100dvh; }

#panel {
  width: 380px; flex: none;
  display: flex; flex-direction: column;
  background: var(--surface);
  border-right: 1px solid var(--line);
  min-height: 0;
}
#app.collapsed #panel { display: none; }

.brand { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px 10px; }
.wordmark { display: flex; align-items: center; gap: 9px; color: inherit; text-decoration: none; border-radius: 6px; }
.wordmark:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }
.logo { width: 30px; height: 30px; flex: none; }
.brand h1 { font-size: 21px; margin: 0; font-weight: 750; letter-spacing: -0.025em; line-height: 1; }
.brand h1 span { font-weight: 400; color: var(--ink-2); }

.search { display: flex; gap: 6px; padding: 4px 16px 10px; }
.search input { flex: 1; min-width: 0; }
.hint.search-hint { margin: -4px 16px 10px; }
input[type="search"], select {
  border: 1px solid var(--line-2); background: var(--raised);
  border-radius: 8px; padding: 7px 9px; height: 36px;
}
input[type="search"]:focus, select:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.tabs { display: flex; gap: 2px; padding: 0 12px; border-bottom: 1px solid var(--line); }
.tabs button {
  border: 0; background: none; padding: 9px 10px; cursor: pointer;
  color: var(--ink-2); border-bottom: 2px solid transparent; margin-bottom: -1px;
}
.tabs button[aria-selected="true"] { color: var(--ink); border-bottom-color: var(--accent); font-weight: 600; }
.badge {
  display: inline-block; min-width: 20px; padding: 0 6px; border-radius: 10px;
  background: var(--hover); color: var(--ink-2); font-size: 12px; font-variant-numeric: tabular-nums;
}

.tab { flex: 1; overflow-y: auto; padding: 12px 16px 16px; min-height: 0; }
.row { display: flex; gap: 8px; }
.row > * { flex: 1; min-width: 0; }
.field { display: flex; flex-direction: column; gap: 3px; font-size: 12px; color: var(--ink-2); margin-bottom: 10px; }
.field select { color: var(--ink); width: 100%; }
.field-title { font-size: 12px; font-weight: 600; color: var(--ink-2); text-transform: uppercase; letter-spacing: 0.04em; margin: 6px 0 6px; }
.hint { font-size: 12px; color: var(--muted); margin: 4px 0 10px; }
.muted { color: var(--muted); }
.check { display: block; font-size: 13px; margin: 4px 0 12px; }
.check input { margin: 0 6px 0 0; vertical-align: -2px; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  border: 1px solid var(--line-2); background: var(--raised); border-radius: 999px;
  padding: 3px 10px; cursor: pointer; font-size: 12.5px;
}
.chip[aria-pressed="true"] { background: var(--ink); color: var(--surface); border-color: var(--ink); }
.chip .n { color: var(--muted); font-variant-numeric: tabular-nums; margin-left: 4px; }
.chip[aria-pressed="true"] .n { color: inherit; opacity: 0.7; }
.chip.tier-broad { border-style: dashed; }
.chip.tier-contested { border-style: dotted; }
.tier-preset { font-size: 12.5px; margin-left: 4px; align-self: center; }

.group-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-top: 8px; flex-wrap: wrap; }
.group-head .field-title { margin: 0; }
.group-head .field-title .muted { text-transform: none; letter-spacing: 0; font-weight: 400; margin-left: 4px; }
.group-actions { display: flex; gap: 6px; }
.group-actions .chip:hover { background: var(--hover); }
#groups-empty { margin: 10px 0 0; color: var(--ink-2); }
.link { border: 0; background: none; color: var(--accent); cursor: pointer; padding: 0; }

.family-head { display: flex; align-items: center; gap: 8px; margin: 14px 0 4px; padding: 6px; border-top: 1px solid var(--line); }
.family-head label { display: flex; align-items: center; gap: 8px; font-weight: 650; cursor: pointer; flex: 1; }
.family-head input { margin: 0; }
.family-head .muted { font-size: 12px; font-variant-numeric: tabular-nums; }
.family-head .expand { border: 0; background: none; cursor: pointer; color: var(--muted); width: 24px; height: 24px; border-radius: 6px; }
.group-row .swatch { width: 8px; height: 8px; align-self: center; }
.swatch { width: 10px; height: 10px; border-radius: 50%; flex: none; box-shadow: 0 0 0 1px var(--dot-ring); }

.group { border-radius: 8px; }
.group-row { display: flex; align-items: center; gap: 8px; padding: 5px 6px; border-radius: 8px; }
.group-row:hover { background: var(--hover); }
.group-row input { margin: 0; }
.group-row label { flex: 1; min-width: 0; cursor: pointer; display: flex; gap: 6px; align-items: baseline; }
.group-row .lbl { font-weight: 600; white-space: nowrap; }
.group-row .mean { color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.group-row .n { font-variant-numeric: tabular-nums; color: var(--ink-2); font-size: 12.5px; }
.group-row .expand { border: 0; background: none; cursor: pointer; color: var(--muted); width: 24px; height: 24px; border-radius: 6px; }
.group-row .expand:hover { background: var(--line); }
.group.empty .group-row { opacity: 0.45; }
.variants { padding: 4px 6px 10px 30px; }
.variants .chips { margin-bottom: 8px; }
.variants .more { font-size: 12.5px; }

.results-head { align-items: center; margin-bottom: 8px; }
.results-head select { flex: none; width: auto; }
.result {
  display: block; width: 100%; text-align: left; border: 0; background: none; cursor: pointer;
  padding: 7px 6px; border-bottom: 1px solid var(--line); border-radius: 0;
}
.result:hover { background: var(--hover); }
.result .nm { font-weight: 600; }
.result .nm b { text-decoration: underline; text-decoration-thickness: 2px; text-underline-offset: 2px; }
.result .sub { color: var(--muted); font-size: 12px; }
.result .dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; box-shadow: 0 0 0 1px var(--dot-ring); }
.result .dot.ring { background: transparent !important; box-shadow: inset 0 0 0 2px currentColor; }

table.stats { width: 100%; border-collapse: collapse; font-size: 13px; }
table.stats th { text-align: left; font-weight: 600; color: var(--ink-2); font-size: 12px; border-bottom: 1px solid var(--line-2); padding: 4px 4px; }
table.stats td { padding: 4px 4px; border-bottom: 1px solid var(--line); vertical-align: top; }
table.stats td.num, table.stats th.num { text-align: right; font-variant-numeric: tabular-nums; }
.bar { height: 6px; border-radius: 3px; background: var(--accent); margin-top: 3px; }

.export { margin-top: 16px; padding-top: 12px; border-top: 1px solid var(--line); }
.btn {
  border: 1px solid var(--accent); background: var(--accent); color: var(--accent-ink);
  border-radius: 8px; padding: 7px 12px; cursor: pointer; font-weight: 600;
}
.btn.secondary { background: transparent; color: var(--accent); }
.btn:disabled { opacity: 0.5; cursor: default; }


.sources { font-size: 11.5px; color: var(--muted); padding: 8px 16px 12px; border-top: 1px solid var(--line); }
.sources a { color: inherit; }

#map-wrap { flex: 1; position: relative; min-width: 0; }
#map { position: absolute; inset: 0; background: var(--page); }
.leaflet-container { font: inherit; }
.icon-btn { border: 1px solid var(--line-2); background: var(--raised); border-radius: 8px; height: 32px; min-width: 32px; cursor: pointer; }
.icon-btn.floating { position: absolute; top: 12px; left: 56px; z-index: 1000; padding: 0 12px; box-shadow: 0 1px 4px rgba(0,0,0,.2); }

.legend {
  position: absolute; left: 12px; bottom: 24px; z-index: 1000;
  background: var(--surface); border: 1px solid var(--line); border-radius: 10px;
  padding: 8px 10px; font-size: 12px; box-shadow: 0 1px 4px rgba(0,0,0,.15); max-width: calc(100% - 24px);
}
.legend .item { display: flex; align-items: center; gap: 6px; margin: 2px 0; }
.legend .ring { width: 10px; height: 10px; border-radius: 50%; box-shadow: inset 0 0 0 2px var(--ink-2); }
.legend hr { border: 0; border-top: 1px solid var(--line); margin: 5px 0; }
.legend-toggle { display: none; font-size: 12px; }

.loading {
  position: absolute; inset: 0; z-index: 2000; display: grid; place-items: center;
  background: color-mix(in srgb, var(--page) 80%, transparent); font-weight: 600;
}
.loading[hidden] { display: none; }

.leaflet-popup-content-wrapper, .leaflet-popup-tip { background: var(--surface); color: var(--ink); }
.leaflet-popup-content { margin: 12px 14px; min-width: 220px; }
.pop h4 { margin: 0 0 2px; font-size: 15px; }
.pop h4 b { text-decoration: underline; text-decoration-thickness: 2px; text-underline-offset: 2px; }
.pop .sub { color: var(--ink-2); font-size: 12.5px; }
.pop .tag { display: inline-block; font-size: 11.5px; border: 1px solid var(--line-2); border-radius: 999px; padding: 0 7px; margin: 6px 4px 0 0; }
.pop .warn { color: var(--ink-2); font-size: 12px; margin-top: 6px; }
.pop .acts { margin-top: 8px; display: flex; flex-wrap: wrap; gap: 10px; font-size: 12.5px; }
.pop a, .pop .link { color: var(--accent); }

@media (max-width: 760px) {
  #app { flex-direction: column-reverse; }
  #panel { width: 100%; height: 55%; border-right: 0; border-top: 1px solid var(--line); }
  #map-wrap { flex: 1; min-height: 45%; }
  .brand { padding: 10px 16px 4px; }
  .legend { bottom: 44px; left: 8px; font-size: 11px; padding: 4px 8px; }
  .legend-toggle { display: block; }
  .legend:not(.open) .item, .legend:not(.open) hr { display: none; }
  .sources { display: none; }
  .icon-btn.floating { left: auto; right: 12px; }
}

.segmented { display: flex; border: 1px solid var(--line-2); border-radius: 10px; padding: 3px; gap: 3px; margin-bottom: 12px; background: var(--page); }
.segmented button { flex: 1; border: 0; background: transparent; border-radius: 7px; padding: 6px 8px; cursor: pointer; font-weight: 600; }
.segmented button .muted { font-weight: 400; font-size: 11.5px; }
.segmented button[aria-pressed="true"] { background: var(--raised); box-shadow: 0 1px 2px rgba(0,0,0,.15); }
.view-switch { display: flex; gap: 4px; margin-bottom: 6px; }
.view-switch .chip { padding: 2px 10px; font-size: 12px; }
.legend .cell { width: 12px; height: 12px; border-radius: 2px; flex: none; }
.legend select { height: 26px; padding: 2px 6px; font-size: 12px; margin-left: 4px; }
.legend .cell.hot1 { background: transparent; box-shadow: inset 0 0 0 1.2px var(--ink); }
.legend .cell.hot2 { background: transparent; box-shadow: inset 0 0 0 2.5px var(--ink); }
.legend input[type="checkbox"] { margin: 0 4px 0 0; }

/* story */
.story h2 { font-size: 22px; margin: 2px 0 8px; letter-spacing: -0.01em; }
.story-steps { display: flex; gap: 6px; margin: 2px 0 8px; }
.story-steps .step { flex: 1; height: 4px; border: 0; border-radius: 2px; background: var(--line-2); cursor: pointer; padding: 0; }
.story-steps .step[aria-current="true"] { background: var(--accent); }
.story-count { font-size: 12px; }
.story-body { font-size: 14.5px; line-height: 1.55; }
.story-body p { margin: 0 0 10px; }
.story-body ul { padding-left: 18px; margin: 0 0 10px; }
.story-body li { margin-bottom: 6px; }
.story-evidence { background: var(--hover); border-radius: 10px; padding: 10px 12px; margin: 12px 0; }
.story-evidence .ev-line { display: flex; align-items: baseline; gap: 8px; font-size: 13px; margin-bottom: 4px; }
.story-evidence .ev-line b { font-size: 17px; font-variant-numeric: tabular-nums; }
.story-evidence .ev-line > span:first-child { width: 96px; flex: none; }
.story-evidence .ev-yes { color: var(--accent); font-weight: 600; }
.story-evidence .stats { margin-top: 8px; }
.story-evidence .hint { margin-bottom: 0; }
.story-nav { justify-content: space-between; margin: 4px 0 6px; }

.legend .ramp { display: grid; grid-template-columns: repeat(4, 1fr); gap: 2px; margin: 4px 0 2px; }
.legend .ramp span { height: 10px; border-radius: 2px; }
.legend .ramp-labels { display: grid; grid-template-columns: repeat(4, 1fr); font-size: 11px; color: var(--muted); margin-bottom: 4px; }
.legend .item.muted { max-width: 230px; }

/* word pairs */
.pairs { display: flex; flex-direction: column; gap: 6px; }
.pair { display: block; width: 100%; text-align: left; background: var(--raised); border: 1px solid var(--line); border-radius: 10px; padding: 9px 11px; cursor: pointer; }
.pair:hover { background: var(--hover); }
.pair.open { border-color: var(--accent); cursor: default; }
.pair-title { font-weight: 650; font-size: 15px; }
.pair-sides { display: flex; flex-direction: column; gap: 6px; margin: 8px 0 2px; }
.pair-side { display: grid; grid-template-columns: 12px 1fr auto; column-gap: 7px; align-items: baseline; font-size: 13px; }
.pair-side .swatch { width: 10px; height: 10px; border-radius: 50%; align-self: center; }
.pair-side .num { font-variant-numeric: tabular-nums; font-weight: 600; }
.pair-side .pair-top { grid-column: 2 / 4; font-size: 12px; }
.pair .hint { display: block; margin: 6px 0 0; }
