:root {
  --bg: #f6f7f9;
  --panel: rgba(255, 255, 255, 0.94);
  --text: #1b1f24;
  --muted: #5d6670;
  --line: #dde1e6;
  --accent: #2563eb;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e1116;
    --panel: rgba(22, 26, 32, 0.94);
    --text: #e8ebef;
    --muted: #98a2ad;
    --line: #2c333c;
    --accent: #6ea8ff;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0e1116;
  --panel: rgba(22, 26, 32, 0.94);
  --text: #e8ebef;
  --muted: #98a2ad;
  --line: #2c333c;
  --accent: #6ea8ff;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
}
#map { position: absolute; inset: var(--shell-h) 0 var(--nav-h) 0; background: var(--bg); }

#panel {
  position: absolute;
  top: calc(var(--shell-h) + 12px);
  left: 12px;
  width: 340px;
  max-height: calc(100% - var(--shell-h) - var(--nav-h) - 24px);
  overflow-y: auto;
  background: var(--panel);
  backdrop-filter: blur(6px);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 14px 16px;
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.15);
}
#panel-handle { display: none; }
#panel-now { padding-bottom: 2px; }
#panel-more { padding-top: 8px; }
.credits { margin-top: 12px; border-top: 1px solid var(--line); padding-top: 10px; }
.credits summary { cursor: pointer; color: var(--text); font-weight: 600; }
.credits p { margin: 8px 0 0; }
.map-loading {
  position: absolute; z-index: 2; top: calc(var(--shell-h) + 16px); left: 50%; transform: translateX(-50%); margin: 0;
  padding: 8px 14px; border: 1px solid var(--line); border-radius: 999px; background: var(--panel); white-space: nowrap;
}
.map-loading[hidden] { display: none; }
/* Phone: the panel is a drawer that peeks (region, total, mix), then pulls up. --peek is its
   peeking height, from app.js: the map frames itself above it and its credit sits on top of it. */
.maplibregl-ctrl-bottom-right, .maplibregl-ctrl-bottom-left { bottom: var(--peek, 0px); }
@media (max-width: 640px) {
  #panel {
    position: fixed; z-index: 5; top: auto; bottom: var(--nav-h); left: 0; right: 0; width: auto; max-height: 70vh;
    border-width: 1px 0 0; border-radius: 16px 16px 0 0; padding: 0 16px 10px;
    background: var(--card); backdrop-filter: none;
  }
  #panel-handle {
    position: sticky; top: 0; z-index: 1; display: flex; align-items: center; justify-content: center; width: 100%; height: 28px;
    padding: 0; border: 0; background: var(--card); cursor: pointer; touch-action: none;
  }
  #panel-handle::before { content: ""; width: 40px; height: 5px; border-radius: 3px; background: var(--line); }
  #panel.collapsed #panel-more { display: none; }
  #panel.collapsed #status { max-height: 1.45em; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
  #panel header { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 6px 8px; align-items: center; padding-bottom: 6px; }
  #panel header .row-between { display: contents; }
  #panel h1, .scope label > span { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
  #view-toggle { grid-column: 2; grid-row: 1; }
  .scope { grid-column: 1; grid-row: 1; margin: 0; }
  #status { grid-column: 1 / -1; font-size: 12px; color: var(--muted); }
  #panel-now { padding: 8px 0 4px; cursor: pointer; }
  #panel-now .mixbar { margin: 6px 0; }
  .segmented button { min-height: 44px; font-size: 13px; }
  .scope select { min-height: 44px; font-size: 16px; }
  .legend li { min-height: 44px; }
  .inactive-toggle { min-height: 44px; }
  .credits summary { padding: 14px 0; }
}

h1 { font-size: 17px; margin: 0 0 2px; }
h2 { font-size: 14px; margin: 0; }
section { border-top: 1px solid var(--line); padding: 12px 0; }
header { padding-bottom: 10px; }
header p { margin: 0; }
footer { border-top: 1px solid var(--line); padding-top: 10px; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.hint { font-size: 12px; color: var(--muted); margin: 6px 0 0; }
.inactive-toggle { display: flex; align-items: center; gap: 8px; margin-top: 8px; cursor: pointer; }
.inactive-toggle input { width: 16px; height: 16px; margin: 0; }
.big { font-size: 20px; font-weight: 600; font-variant-numeric: tabular-nums; }
.row-between { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
#unmapped { margin-top: 2px; }
#us-page-link { margin: 0 0 8px; }
#demo-banner { margin-top: 6px; padding: 6px 8px; border-radius: 6px; background: #f2c23033; font-size: 12px; }

.mixbar { display: flex; height: 12px; border-radius: 6px; overflow: hidden; margin: 10px 0; background: var(--line); }
.mixbar span { display: block; height: 100%; }

.legend { list-style: none; margin: 0; padding: 0; }
.legend li {
  display: grid;
  grid-template-columns: 12px 1fr auto auto;
  gap: 8px;
  align-items: center;
  padding: 3px 4px;
  border-radius: 4px;
  cursor: pointer;
  font-variant-numeric: tabular-nums;
}
.legend li:hover { background: var(--line); }
.legend li.off { opacity: 0.35; }
.legend .sw { width: 12px; height: 12px; border-radius: 50%; }
.legend .pct { color: var(--muted); width: 3.2em; text-align: right; }

#d-close { background: none; border: none; color: var(--muted); font-size: 20px; cursor: pointer; line-height: 1; }
#d-spark { width: 100%; height: 70px; display: block; margin-top: 4px; }
#d-units { width: 100%; border-collapse: collapse; font-size: 12px; margin-top: 8px; font-variant-numeric: tabular-nums; }
#d-units td { padding: 2px 0; border-bottom: 1px solid var(--line); }
#d-units td:not(:first-child) { text-align: right; }
#d-meta, #d-range { margin: 2px 0; }
#d-monthly h3 { font-size: 12px; margin: 14px 0 0; color: var(--muted); font-weight: 600; }
#d-monthly svg { width: 100%; display: block; margin-top: 6px; }
#d-monthly .axis-row { display: flex; justify-content: space-between; font-size: 11px; color: var(--muted); margin-top: 2px; }
#d-monthly table { width: 100%; border-collapse: collapse; font-size: 12px; margin-top: 6px; font-variant-numeric: tabular-nums; }
#d-monthly td { padding: 2px 0; border-bottom: 1px solid var(--line); }
#d-monthly td:last-child { text-align: right; }

.maplibregl-popup-content { background: var(--panel); color: var(--text); padding: 6px 10px; font-size: 12px; }
.maplibregl-popup-tip { display: none; }

/* ------------------------------------------------------------ header controls */
.segmented { display: inline-flex; border: 1px solid var(--line); border-radius: 6px; overflow: hidden; }
.segmented button {
  padding: 3px 9px;
  border: 0;
  background: none;
  color: var(--muted);
  font: inherit;
  font-size: 12px;
  cursor: pointer;
}
.segmented button[aria-pressed="true"] { background: var(--accent); color: #fff; }
.scope { display: flex; gap: 8px; margin: 8px 0; }
.scope label { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 3px; font-size: 12px; color: var(--muted); }
.scope label[hidden] { display: none; }
.scope select {
  width: 100%;
  font: inherit;
  font-size: 13px;
  color: var(--text);
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 5px 6px;
}

/* ------------------------------------------------------------ dashboard */
:root { --card: #ffffff; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) { --card: #1a1a19; }
}
:root[data-theme="dark"] { --card: #1a1a19; }
#dashboard {
  position: absolute;
  inset: var(--shell-h) 0 var(--nav-h) 364px;
  overflow-y: auto;
  background: var(--bg);
  padding: 16px;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
  align-content: start;
}
#dashboard[hidden] { display: none; }
body.dashboard-view .maplibregl-control-container { display: none; }
@media (max-width: 1100px) { #dashboard { grid-template-columns: minmax(0, 1fr); } }
@media (max-width: 640px) { #dashboard { inset: var(--shell-h) 0 calc(var(--nav-h) + var(--peek, 0px)) 0; padding: 12px; } }

.dash-card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 14px 16px;
  min-width: 0;
}
.dash-card h2 { font-size: 14px; margin: 0 0 10px; }
.dash-card h2 + .dash-sub { margin: -6px 0 10px; }
.dash-wide, .dash-figures { grid-column: 1 / -1; }
@media (min-width: 1101px) { .dash-figures { display: flex; gap: 28px; align-items: flex-end; flex-wrap: wrap; } }
.dash-label { font-size: 12px; color: var(--muted); }
.dash-sub { font-size: 12px; color: var(--muted); margin: 2px 0 0; }
.dash-hero-value { font-size: 52px; font-weight: 600; line-height: 1.05; letter-spacing: -0.5px; }
.dash-tiles { display: flex; gap: 12px; flex-wrap: wrap; margin-top: 10px; }
.dash-tile { flex: 1 1 140px; border-left: 1px solid var(--line); padding: 2px 0 2px 12px; }
.dash-tile-value { font-size: 22px; font-weight: 600; margin-top: 2px; }
.dash-empty { color: var(--muted); font-size: 13px; margin: 8px 0; }
.num { font-variant-numeric: tabular-nums; }
.sw { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 6px; vertical-align: -1px; flex: none; }

/* donut */
.dash-donut { display: flex; align-items: center; gap: 18px; flex-wrap: wrap; }
.donut-svg { width: 200px; height: 200px; flex: none; }
.donut-slices path { stroke: var(--card); stroke-width: 2; transition: opacity 0.12s; }
.donut-total { font-size: 17px; font-weight: 600; fill: var(--text); text-anchor: middle; }
.donut-caption { font-size: 11px; fill: var(--muted); text-anchor: middle; }
.dash-key { list-style: none; margin: 0; padding: 0; font-size: 13px; }
.dash-key li { display: flex; align-items: center; gap: 4px; padding: 3px 0; }
.dash-key li .num { margin-left: auto; padding-left: 16px; color: var(--muted); }
.dash-key.inline { display: flex; flex-wrap: wrap; gap: 4px 14px; margin-bottom: 8px; font-size: 12px; }
.dash-donut.hovering .donut-slices path:not(.on), .dash-donut.hovering .dash-key li:not(.on) { opacity: 0.35; }

/* bars */
.bar-row { display: grid; grid-template-columns: 104px 1fr 72px; align-items: center; gap: 8px; padding: 5px 0; cursor: default; }
.bar-row:hover { background: color-mix(in srgb, var(--line) 40%, transparent); border-radius: 4px; }
.bar-label { font-size: 13px; display: flex; align-items: center; white-space: nowrap; }
.bar-track { height: 12px; }
.bar { display: block; height: 100%; min-width: 3px; border-radius: 0 4px 4px 0; }
.bar.tiny { border-radius: 0; }
.bar-value { font-size: 13px; text-align: right; color: var(--muted); }

/* mix over time */
.dash-timeline svg { display: block; max-width: 100%; }
.dash-timeline .grid { stroke: var(--line); stroke-width: 1; }
.dash-timeline .axis { font-size: 11px; fill: var(--muted); font-variant-numeric: tabular-nums; }
.dash-timeline .hit { fill: transparent; }
.dash-timeline .col rect:not(.hit) { transition: opacity 0.12s; }
.dash-timeline.hovering .col:not(.on) rect:not(.hit) { opacity: 0.4; }
.stale { opacity: 0.5; transition: opacity 0.2s; }

/* table */
.dash-table-wrap { overflow-x: auto; }
.dash-table { width: 100%; border-collapse: collapse; font-size: 13px; font-variant-numeric: tabular-nums; }
.dash-table th { text-align: right; font-weight: 500; color: var(--muted); font-size: 12px; padding: 4px 8px; border-bottom: 1px solid var(--line); }
.dash-table td { text-align: right; padding: 5px 8px; border-bottom: 1px solid var(--line); }
.dash-table th:first-child, .dash-table td:first-child { text-align: left; padding-left: 0; }
.dash-table tfoot td { font-weight: 600; border-bottom: 0; }

/* tooltip */
#dash-tip {
  position: fixed;
  z-index: 10;
  pointer-events: none;
  background: var(--card);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 12px;
  line-height: 1.5;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
  max-width: 260px;
}
#dash-tip[hidden] { display: none; }
#dash-tip .num { float: right; padding-left: 12px; }

/* Pulsing dot: data is being fetched */
.pulse {
  display: inline-block; width: 8px; height: 8px; margin-right: 6px; border-radius: 50%;
  background: var(--accent); vertical-align: 0; animation: pulse 1.2s ease-in-out infinite;
}
@keyframes pulse { 0%, 100% { opacity: 0.25; transform: scale(0.8); } 50% { opacity: 1; transform: scale(1.1); } }
@media (prefers-reduced-motion: reduce) { .pulse { animation: none; } }
