:root {
  --bg: #ffffff;
  --fg: #1f2937;
  --muted: #6b7280;
  --border: #e5e7eb;
  --primary: #1d4ed8;
  --accent: #f59e0b;
  --shadow: 0 6px 18px rgba(15, 23, 42, 0.08);
  --header-h: 68px;
  --controls-w: 460px;
  --footer-h: 36px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--fg);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 16px;
  line-height: 1.5;
}

#topbar {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: var(--header-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  z-index: 10;
}

#topbar h1 {
  font-size: 20px;
  font-weight: 600;
  margin: 0;
  letter-spacing: -0.01em;
}

.brand .meta {
  display: block;
  font-size: 13px;
  color: var(--muted);
  margin-top: 2px;
}

#toggle-controls {
  display: none;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 14px;
  font-size: 14px;
  cursor: pointer;
}

#controls {
  position: fixed;
  top: var(--header-h);
  right: 0;
  width: var(--controls-w);
  bottom: var(--footer-h);
  background: var(--bg);
  border-left: 1px solid var(--border);
  padding: 22px 24px;
  overflow-y: auto;
  z-index: 9;
  box-shadow: var(--shadow);
}

.control-group {
  margin-bottom: 22px;
}

.control-group label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 8px;
}

.control-group select {
  width: 100%;
  padding: 11px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  font-size: 15px;
  font-weight: 500;
}

#granularity {
  display: flex;
  gap: 16px;
  font-size: 15px;
}

#granularity label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 400;
  text-transform: none;
  letter-spacing: 0;
  margin: 0;
  cursor: pointer;
}

#granularity input[type="radio"] {
  width: 16px;
  height: 16px;
}

.legend {
  margin-top: 8px;
  padding: 16px 18px;
  background: #f9fafb;
  border-radius: 10px;
  border: 1px solid var(--border);
}

.legend-title {
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  font-weight: 600;
  margin-bottom: 10px;
}

.legend-question {
  margin: 0 0 16px 0;
  padding: 12px 14px;
  background: #ffffff;
  border-left: 4px solid var(--primary);
  border-radius: 4px;
  font-size: 15px;
  font-style: italic;
  color: var(--fg);
  line-height: 1.5;
  quotes: "“" "”";
}

.legend-question::before { content: open-quote; margin-right: 2px; }
.legend-question::after { content: close-quote; margin-left: 2px; }

.legend-bar {
  height: 14px;
  border-radius: 4px;
  background: linear-gradient(to right, #f7fbff, #08306b);
}

.legend-labels {
  display: flex;
  justify-content: space-between;
  font-size: 13px;
  color: var(--muted);
  margin-top: 6px;
}

.legend-note {
  font-size: 13px;
  color: var(--muted);
  margin-top: 14px;
  line-height: 1.55;
}

.legend-swatches {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.legend-swatch {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 15px;
}

.swatch-pct {
  margin-left: auto;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  color: var(--fg);
  font-size: 14px;
}

.swatch-dot {
  display: inline-block;
  width: 16px;
  height: 16px;
  border-radius: 4px;
  flex: 0 0 auto;
  box-shadow: 0 0 0 1px rgba(0,0,0,0.08);
}

.swatch-label {
  flex: 1 1 auto;
  color: var(--fg);
  line-height: 1.3;
}

#map-container {
  position: fixed;
  top: var(--header-h);
  left: 0;
  right: var(--controls-w);
  bottom: var(--footer-h);
}

#map { width: 100%; height: 100%; }

#error-banner {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.95);
  text-align: center;
  z-index: 20;
}

#error-banner[hidden],
#popup[hidden] {
  display: none !important;
}

#error-banner button {
  margin-top: 12px;
  padding: 8px 16px;
  background: var(--primary);
  color: white;
  border: 0;
  border-radius: 6px;
  cursor: pointer;
}

#popup {
  position: fixed;
  bottom: 56px;
  left: 16px;
  max-width: 440px;
  min-width: 320px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 16px 18px;
  box-shadow: var(--shadow);
  font-size: 13px;
  z-index: 11;
}

#popup h3 {
  margin: 0 0 6px 0;
  font-size: 14px;
}

#popup .meta {
  color: var(--muted);
  font-size: 11px;
  margin-bottom: 8px;
}

#popup ul {
  list-style: none;
  padding: 0;
  margin: 8px 0 0 0;
}

#popup ul li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 24px;
  padding: 5px 0;
  border-top: 1px dashed var(--border);
}

#popup ul li > span:first-child {
  display: flex;
  align-items: center;
  flex: 1 1 auto;
  min-width: 0;
}

#popup .pct {
  font-weight: 600;
  flex: 0 0 auto;
  white-space: nowrap;
}

#footer {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: var(--footer-h);
  background: var(--bg);
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  font-size: 13px;
  color: var(--muted);
  z-index: 8;
}

#footer .sep { color: var(--border); }

#footer a {
  color: var(--primary);
  text-decoration: none;
}

#footer a:hover { text-decoration: underline; }

/* Print mode (PDF) */
body.print-mode #topbar,
body.print-mode #controls,
body.print-mode #footer,
body.print-mode #toggle-controls {
  display: none !important;
}

body.print-mode #map-container {
  inset: 0;
}

/* Mobile */
@media (max-width: 768px) {
  :root { --controls-w: 0; }
  #toggle-controls { display: inline-block; }
  #controls {
    width: 90%;
    max-width: 320px;
    right: 0;
    transform: translateX(100%);
    transition: transform 0.25s ease;
  }
  #controls.open { transform: translateX(0); }
  #map-container { right: 0; }
  #popup {
    left: 8px;
    right: 8px;
    max-width: none;
    bottom: 40px;
  }
}
