:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface-1: #fcfcfb;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.1);
  --dim: #d9d8d2;
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --series-4: #eda100;
  --series-5: #e87ba4;
  --series-6: #008300;
  --series-7: #4a3aa7;
  --series-8: #e34948;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface-1: #1a1a19;
    --text-primary: #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted: #898781;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.1);
    --dim: #3a3a37;
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --series-4: #c98500;
    --series-5: #d55181;
    --series-6: #008300;
    --series-7: #9085e9;
    --series-8: #e66767;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface-1: #1a1a19;
  --text-primary: #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted: #898781;
  --grid: #2c2c2a;
  --axis: #383835;
  --border: rgba(255, 255, 255, 0.1);
  --dim: #3a3a37;
  --series-1: #3987e5;
  --series-2: #d95926;
  --series-3: #199e70;
  --series-4: #c98500;
  --series-5: #d55181;
  --series-6: #008300;
  --series-7: #9085e9;
  --series-8: #e66767;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--page);
  color: var(--text-primary);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
}

.viz-root { max-width: 1280px; margin: 0 auto; padding: 24px 16px 48px; }

h1 { font-size: 24px; margin: 0 0 4px; }
h2 { font-size: 18px; margin: 0 0 4px; }
p { margin: 0; }
.muted { color: var(--text-secondary); }
.page-head { margin-bottom: 24px; }
.title-row { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; }
#theme-toggle { flex: none; white-space: nowrap; }
.page-foot { margin-top: 32px; padding-top: 12px; border-top: 1px solid var(--grid); font-size: 13px; }
.refresh-row { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; margin-top: 10px; font-size: 13px; }
button:disabled { opacity: 0.5; cursor: default; }
a { color: inherit; }

.card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 10px;
}

/* Controls: one row above the chart */
.controls { display: flex; flex-wrap: wrap; gap: 12px 20px; align-items: end; margin: 16px 0 12px; }
.control { display: flex; flex-direction: column; gap: 4px; }
.control-label { font-size: 12px; color: var(--text-secondary); }
.segmented { display: flex; flex-wrap: wrap; gap: 4px; }

button, select, input {
  font: inherit;
  color: var(--text-primary);
  background: var(--surface-1);
  border: 1px solid var(--axis);
  border-radius: 6px;
  padding: 6px 10px;
}
button { cursor: pointer; }
button:hover { border-color: var(--text-muted); }
button[aria-pressed="true"] { background: var(--text-primary); color: var(--surface-1); border-color: var(--text-primary); }
input[type="search"] { min-width: 220px; }
:focus-visible { outline: 2px solid var(--series-1); outline-offset: 2px; }

/* Legend */
.legend { display: flex; flex-wrap: wrap; gap: 8px 24px; align-items: center; min-height: 34px; margin-bottom: 12px; font-size: 13px; color: var(--text-secondary); }
.legend-group { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.ramp { width: 160px; height: 10px; border-radius: 5px; }
.swatch { width: 10px; height: 10px; border-radius: 50%; display: inline-block; flex: none; }
.theme-chip { display: inline-flex; align-items: center; gap: 6px; padding: 3px 9px; font-size: 13px; border-radius: 999px; }
.theme-chip .count { color: var(--text-muted); }
.theme-chip[aria-pressed="true"] .count { color: inherit; opacity: 0.7; }
.size-key { display: inline-flex; align-items: center; gap: 6px; }
.size-key svg { display: block; }

/* Map and detail panel */
.map-layout { display: grid; grid-template-columns: minmax(0, 1fr) 320px; gap: 16px; align-items: start; }
.map-wrap { position: relative; padding: 4px; }
#map { display: block; width: 100%; height: auto; touch-action: manipulation; }

.dot { stroke: var(--surface-1); stroke-width: 2; transition: opacity 0.15s; }
.dot.dimmed { opacity: 0.14; }
.dot.active { stroke: var(--text-primary); stroke-width: 2.5; }
.link { stroke: var(--text-muted); stroke-width: 1.5; fill: none; }
.map-label { font-size: 12px; fill: var(--text-primary); paint-order: stroke; stroke: var(--surface-1); stroke-width: 4px; stroke-linejoin: round; pointer-events: none; }
.map-label.region-label { font-size: 11.5px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.07em; fill: var(--text-secondary); }
.map-label.region-label.fine { font-size: 11.5px; text-transform: none; letter-spacing: 0; }

.tooltip {
  position: absolute; z-index: 2; pointer-events: none; max-width: 280px;
  background: var(--surface-1); border: 1px solid var(--border); border-radius: 8px;
  padding: 8px 10px; font-size: 13px; box-shadow: 0 4px 16px rgba(0, 0, 0, 0.18);
}
.tooltip .value { font-weight: 600; font-size: 15px; }
.tooltip .title { margin-top: 2px; }
.tooltip .sub { color: var(--text-secondary); margin-top: 2px; }

.detail { padding: 16px; font-size: 14px; }
.detail h3 { font-size: 16px; margin: 0 0 4px; }
.detail a { color: inherit; }
.detail .stats { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 12px; margin: 14px 0; }
.detail .stat-value { font-size: 20px; font-weight: 600; line-height: 1.2; }
.detail .stat-label { font-size: 12px; color: var(--text-secondary); }
.detail .theme-line { display: flex; align-items: center; gap: 6px; margin-bottom: 10px; }
.detail .excerpt { color: var(--text-secondary); margin-bottom: 14px; }
.detail h4 { font-size: 12px; font-weight: 600; color: var(--text-secondary); margin: 0 0 6px; }
.detail ul { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; }
.detail li button { width: 100%; text-align: left; }

.how { margin-top: 24px; max-width: 760px; font-size: 14px; }
.how h3 { font-size: 15px; margin: 0 0 8px; }
.how ol { margin: 0 0 8px; padding-left: 20px; display: grid; gap: 6px; }

/* Table view */
.table-view { margin-top: 20px; }
.table-view summary { cursor: pointer; color: var(--text-secondary); }
.table-scroll { overflow-x: auto; margin-top: 10px; }
table { border-collapse: collapse; width: 100%; font-size: 13px; }
th, td { text-align: left; padding: 6px 10px; border-bottom: 1px solid var(--grid); vertical-align: top; }
th { color: var(--text-secondary); font-weight: 600; white-space: nowrap; }
th button { border: 0; padding: 0; background: none; font-weight: 600; color: inherit; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
td a { color: inherit; }

@media (max-width: 900px) {
  .map-layout { grid-template-columns: 1fr; }
  input[type="search"] { min-width: 0; width: 100%; }
}
