*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:      #1d201d;
  --surface: #252827;
  --border:  rgba(108, 218, 211, 0.15);
  --text:    #ffffff;
  --muted:   #6d8885;
  --teal:    #6cdad3;
  --yellow:  #f5e400;
  --radius:  0px;
  --font:    'Public Sans', -apple-system, sans-serif;

  /* map layer colors */
  --c-source:  #6d8885;
  --c-applied: #6cdad3;
  --c-scaled:  #f5e400;
}

html, body { height: 100%; }

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.5;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

/* ── Header ─────────────────────────────────────────────────── */
#header {
  padding: 9px 18px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 18px;
  flex-shrink: 0;
  background: var(--bg);
}

#header-title {
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--bg);
  background: var(--yellow);
  padding: 3px 7px;
  white-space: nowrap;
  flex-shrink: 0;
}

#header-tagline {
  font-size: 16px;
  font-weight: 300;
  color: var(--muted);
  flex: 1;
}
#header-tagline strong { font-weight: 700; }

/* ── Header actions (theme + palette) ────────────────────────────── */
#header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
  margin-left: auto;
}

#light-btn {
  width: 30px; height: 30px;
  background: transparent;
  border: 1.5px solid rgba(108, 218, 211, 0.25);
  border-radius: var(--radius);
  color: var(--muted);
  font-size: 14px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color 0.15s, border-color 0.15s;
}
#light-btn:hover { color: var(--text); border-color: var(--teal); }

#palette-picker { display: flex; gap: 5px; align-items: center; }

.palette-btn {
  width: 16px; height: 16px;
  border-radius: 0;
  border: 2px solid transparent;
  cursor: pointer;
  padding: 0;
  transition: transform 0.1s, outline 0.1s;
  outline: 2px solid transparent;
  outline-offset: 2px;
}
.palette-btn:hover { transform: scale(1.2); }
.palette-btn.active { outline-color: var(--text); }

/* ── Layers toggle active state ───────────────────────────────────── */
#map-controls button#layers-btn.active {
  color: var(--yellow) !important;
  border-color: var(--yellow) !important;
}

/* ── Light theme ──────────────────────────────────────────────────── */
body.light {
  --bg:      #f2f4f2;
  --surface: #e8ebe8;
  --text:    #1d201d;
  --muted:   #5a7a77;
  --border:  rgba(0, 0, 0, 0.12);
}
body.light .leaflet-container { background: #f2f4f2 !important; }
body.light #legend,
body.light #shape-menu,
body.light #zoom-pct,
body.light #map-controls button:not(.shape-opt) {
  background: rgba(242, 244, 242, 0.97) !important;
}
body.light .leaflet-control-zoom a { background: var(--surface) !important; color: var(--text) !important; }
body.light .leaflet-control-attribution { background: rgba(242,244,242,0.8) !important; }
/* accent text contrast in light mode is handled by applyPalette() using aLight/bLight */
#applied-label { color: var(--teal); }
#source-label  { color: var(--yellow); }

/* ── Subheader ───────────────────────────────────────────────── */

/* ── Controls ────────────────────────────────────────────────── */
#controls {
  display: flex;
  align-items: flex-start;
  gap: 11px;
  padding: 11px 18px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  background: var(--bg);
}

.city-panel { flex: 1; min-width: 0; }

.panel-label {
  font-size: 11px;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--muted);
  margin-bottom: 6px;
  display: flex;
  align-items: center;
  gap: 6px;
}
.panel-hint {
  font-weight: 300;
  text-transform: none;
  letter-spacing: 0;
  font-size: 12px;
}

.dot { width: 9px; height: 9px; border-radius: 0; flex-shrink: 0; }
.source-dot  { background: var(--yellow); }
.applied-dot { background: var(--teal); }

/* Search */
.search-wrap { position: relative; }

.clear-btn {
  position: absolute;
  right: 7px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  color: var(--muted);
  font-size: 17px;
  line-height: 1;
  cursor: pointer;
  padding: 0 2px;
  display: none;
  transition: color 0.1s;
  z-index: 1;
}
.clear-btn.visible { display: block; }
.clear-btn:hover { color: var(--text); }

.search-wrap input {
  width: 100%;
  padding: 7px 28px 7px 11px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 15px;
  font-family: var(--font);
  outline: none;
  background: var(--surface);
  color: var(--text);
  transition: border-color 0.15s;
}
.search-wrap input::placeholder { color: var(--muted); }
.search-wrap input:focus { border-color: var(--teal); border-left: 2px solid var(--teal); }

.suggestions {
  position: absolute;
  top: calc(100% + 2px);
  left: 0; right: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 4px 16px rgba(0,0,0,0.5);
  list-style: none;
  z-index: 1000;
  display: none;
  max-height: 242px;
  overflow-y: auto;
}
.suggestions.open { display: block; }

.suggestion-item {
  padding: 8px 11px;
  cursor: pointer;
  font-size: 14px;
  line-height: 1.3;
  border-bottom: 1px solid rgba(255,255,255,0.04);
  border-left: 2px solid transparent;
  transition: border-color 0.1s, background 0.1s;
}
.suggestion-item:last-child { border-bottom: none; }
.suggestion-item:hover { background: rgba(108, 218, 211, 0.08); border-left-color: var(--teal); }
.suggestion-item .city-name { font-weight: 600; color: var(--text); }
.suggestion-item .city-detail { color: var(--muted); font-size: 13px; font-weight: 300; }

.pinned-item { border-left: 2px solid var(--yellow) !important; background: rgba(245, 228, 0, 0.03); }
.pinned-item:hover { background: rgba(245, 228, 0, 0.09) !important; border-left-color: var(--yellow) !important; }
.pinned-star { color: var(--yellow); font-size: 11px; margin-right: 5px; }
.pinned-subtitle { font-size: 11px; color: var(--muted); font-weight: 300; margin-top: 1px; padding-left: 18px; }

/* Scope toggle */
.scope-toggle { display: flex; gap: 3px; margin-top: 6px; }

.scope-btn {
  padding: 4px 11px;
  border: 1.5px solid rgba(108, 218, 211, 0.2);
  border-radius: var(--radius);
  background: transparent;
  font-size: 11px;
  font-family: var(--font);
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  cursor: pointer;
  color: var(--muted);
  transition: all 0.1s;
}
.scope-btn:hover { color: var(--text); border-color: rgba(108, 218, 211, 0.5); }
#source-panel .scope-btn.active {
  background: var(--yellow);
  border-color: var(--yellow);
  color: var(--bg);
  font-weight: 800;
}
#applied-panel .scope-btn.active {
  background: var(--teal);
  border-color: var(--teal);
  color: var(--bg);
  font-weight: 800;
}

/* City meta */
.city-meta { margin-top: 6px; font-size: 13px; color: var(--muted); min-height: 20px; font-weight: 300; }
.meta-note { font-size: 11px; color: var(--muted); font-style: italic; font-weight: 300; margin-top: 3px; line-height: 1.4; }
.meta-vals { display: flex; gap: 11px; }
.meta-vals strong { color: var(--text); font-weight: 700; }

/* Center column (swap only) */
#center-col {
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  margin-top: 22px;
}

#swap-btn {
  width: 36px; height: 36px;
  border: none;
  border-radius: var(--radius);
  background: var(--teal);
  cursor: pointer;
  font-size: 18px;
  font-weight: 800;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--bg);
  transition: background 0.15s;
}
#swap-btn:hover { background: #85e8e2; }

/* Shape picker */
#shape-picker { position: relative; flex-shrink: 0; }

#shape-toggle {
  /* inherits #map-controls button styles */
}
#shape-toggle.active {
  background: var(--yellow) !important;
  border-color: var(--yellow) !important;
  color: var(--bg) !important;
}

#shape-menu {
  position: absolute;
  right: calc(100% + 8px);
  bottom: 0;
  background: rgba(29, 32, 29, 0.97);
  border: 1.5px solid rgba(108, 218, 211, 0.2);
  backdrop-filter: blur(8px);
  display: flex;
  flex-direction: column;
  z-index: 600;
  box-shadow: -4px 4px 16px rgba(0,0,0,0.5);
  min-width: 170px;
}
#shape-menu.hidden { display: none; }

.shape-opt {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 14px;
  background: transparent;
  border: none;
  border-bottom: 1px solid rgba(255,255,255,0.04);
  border-left: 2px solid transparent;
  font-size: 13px;
  font-family: var(--font);
  font-weight: 400;
  color: var(--muted);
  cursor: pointer;
  text-align: left;
  transition: all 0.1s;
  white-space: nowrap;
}
.shape-menu-divider { height: 1px; background: rgba(108, 218, 211, 0.15); margin: 2px 0; }
.shape-opt:last-child { border-bottom: none; }
.shape-opt:hover { color: var(--text); background: rgba(108, 218, 211, 0.07); border-left-color: var(--teal); }
.shape-opt.active { color: var(--yellow); font-weight: 700; border-left-color: var(--yellow); }
.shape-opt span { font-size: 12px; font-weight: inherit; }

/* ── Map ─────────────────────────────────────────────────────── */
#map-wrap { flex: 1; position: relative; min-height: 0; border: 4px solid var(--teal); }
#map { width: 100%; height: 100%; }

/* No text stroke on any map elements */
.leaflet-container,
.leaflet-container * {
  -webkit-text-stroke: 0;
  text-stroke: 0;
  paint-order: normal;
}

/* Leaflet tile seam fix */
.leaflet-container { background: var(--bg) !important; }
img.leaflet-tile { border: 0 !important; outline: none !important; margin: 0 !important; padding: 0 !important; }
.leaflet-tile-container { will-change: transform; }
.leaflet-map-pane { will-change: transform; }

/* Leaflet overrides for dark theme */
.leaflet-control-zoom a {
  background: var(--surface) !important;
  color: var(--text) !important;
  border-color: var(--border) !important;
}
.leaflet-control-zoom a:hover { background: var(--bg) !important; }
.leaflet-control-attribution {
  background: rgba(29, 32, 29, 0.8) !important;
  color: var(--muted) !important;
  font-family: var(--font) !important;
  font-size: 11px !important;
}
.leaflet-control-attribution a { color: var(--muted) !important; }

/* Map controls (zoom slider + shape + expand) */
#map-controls {
  position: absolute;
  right: 14px;
  top: 14px;
  bottom: 40px;
  z-index: 500;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

#map-controls button:not(.shape-opt) {
  width: 33px;
  height: 33px;
  background: rgba(29, 32, 29, 0.95);
  border: 1.5px solid rgba(108, 218, 211, 0.2);
  border-radius: var(--radius);
  color: var(--muted);
  font-size: 18px;
  font-weight: 800;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  backdrop-filter: blur(4px);
  transition: color 0.15s, border-color 0.15s, background 0.15s;
}
#map-controls button:not(.shape-opt):hover { color: var(--teal); border-color: var(--teal); }

#expand-btn { margin-top: 4px; }

.map-ctrl-divider {
  width: 22px;
  height: 1.5px;
  background: rgba(108, 218, 211, 0.15);
  flex-shrink: 0;
  margin: 4px 0;
}

#zoom-pct {
  width: 33px;
  height: 24px;
  background: rgba(29, 32, 29, 0.95);
  border: 1.5px solid rgba(108, 218, 211, 0.2);
  border-radius: var(--radius);
  color: var(--muted);
  font-size: 10px;
  font-family: var(--font);
  font-weight: 700;
  text-align: center;
  outline: none;
  flex-shrink: 0;
  cursor: text;
  backdrop-filter: blur(4px);
}
#zoom-pct:focus { border-color: var(--teal); color: var(--text); }
#zoom-pct::-webkit-outer-spin-button,
#zoom-pct::-webkit-inner-spin-button { -webkit-appearance: none; }
#zoom-pct { -moz-appearance: textfield; }

#zoom-slider {
  flex: 1;
  writing-mode: vertical-lr;
  direction: rtl;
  -webkit-appearance: slider-vertical;
  appearance: slider-vertical;
  width: 4px;
  cursor: pointer;
  accent-color: var(--yellow);
  background: transparent;
}

/* Legend */
#legend {
  position: absolute;
  bottom: 28px;
  left: 14px;
  background: rgba(29, 32, 29, 0.95);
  border: 1.5px solid rgba(108, 218, 211, 0.15);
  border-radius: var(--radius);
  padding: 9px 13px;
  z-index: 500;
  font-size: 13px;
  backdrop-filter: blur(4px);
}
#legend.hidden { display: none; }

.legend-item { display: flex; align-items: center; gap: 9px; padding: 2px 0; }
.legend-swatch { width: 22px; height: 3px; border-radius: 0; flex-shrink: 0; }
.applied-swatch { background: var(--c-applied); }
.scaled-swatch  { background: var(--c-scaled); }

/* ── Stats panel ─────────────────────────────────────────────── */
#stats-panel {
  border-top: 1px solid var(--border);
  padding: 9px 18px;
  display: flex;
  align-items: center;
  gap: 18px;
  flex-shrink: 0;
  background: var(--bg);
}
.stat-placeholder { font-size: 13px; color: var(--muted); font-weight: 300; font-style: italic; }

#stats-content { flex: 1; }

.stat-row { display: flex; gap: 22px; flex-wrap: wrap; margin-bottom: 2px; }
.stat-col { font-size: 13px; color: var(--muted); font-weight: 300; }
.stat-col .stat-name { font-size: 13px; font-weight: 800; letter-spacing: 0.03em; color: var(--text); }
.stat-col strong { color: var(--yellow); font-weight: 700; }

.stat-result { font-size: 14px; color: var(--muted); font-weight: 300; }
.stat-result strong { color: var(--yellow); font-weight: 800; }
.pop-delta { font-size: 13px; font-weight: 700; }
.pop-up   { color: var(--teal); }
.pop-down { color: var(--yellow); }

#unit-btn {
  padding: 8px 13px;
  background: transparent;
  color: var(--muted);
  border: 1.5px solid rgba(108, 218, 211, 0.2);
  border-radius: var(--radius);
  font-size: 12px;
  font-family: var(--font);
  font-weight: 700;
  letter-spacing: 0.06em;
  cursor: pointer;
  flex-shrink: 0;
  transition: all 0.15s;
}
#unit-btn:hover { color: var(--text); border-color: rgba(108, 218, 211, 0.5); }
#unit-btn.imperial { color: var(--yellow); border-color: var(--yellow); }
#unit-btn.metric   { color: var(--teal);   border-color: var(--teal); }

#export-btn {
  padding: 8px 18px;
  background: var(--teal);
  color: var(--bg);
  border: none;
  border-radius: var(--radius);
  font-size: 12px;
  font-family: var(--font);
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.15s;
}
#export-btn:hover { background: #85e8e2; }

/* ── Stat notes ──────────────────────────────────────────────────── */
.stat-note {
  font-size: 11px;
  color: var(--muted);
  font-weight: 300;
  font-style: italic;
  margin-top: 3px;
  line-height: 1.4;
}

/* ── Footer ──────────────────────────────────────────────────────── */
#footer {
  padding: 5px 18px;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  flex-shrink: 0;
  background: var(--bg);
  font-size: 11px;
  color: var(--muted);
  font-weight: 300;
}
#footer-credit { flex-shrink: 0; white-space: nowrap; }
#footer a { color: var(--muted); text-decoration: none; }
#footer a:hover { color: var(--teal); }

/* ── Collapse controls button (hidden on desktop) ─────────────── */
#collapse-controls-btn {
  display: none;
}

/* ── Mobile ───────────────────────────────────────────────────── */
@media (max-width: 600px) {
  /* Header: wrap tagline below, bigger title */
  #header {
    flex-wrap: wrap;
    gap: 4px 12px;
    padding: 7px 12px;
  }
  #header-title {
    font-size: 12px;
    padding: 3px 8px;
  }
  #header-tagline {
    order: 3;
    flex-basis: 100%;
    font-size: 15px;
  }
  #header-actions { order: 2; }

  /* Controls: use grid so swap btn sits beside source input */
  #controls {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 6px 8px;
    padding: 9px 12px;
  }
  #source-panel  { grid-column: 1; }
  #center-col {
    grid-column: 2;
    grid-row: 1;
    margin-top: 22px;
    align-self: start;
  }
  #applied-panel { grid-column: 1 / -1; }
  #swap-btn {
    width: 36px;
    height: 36px;
    font-size: 16px;
  }

  /* Stats panel: wrap buttons below text */
  #stats-panel {
    flex-wrap: wrap;
    gap: 8px;
    padding: 9px 12px;
  }
  #stats-content { flex-basis: 100%; }
  .stat-result { font-size: 13px; }

  /* Footer: stack on very narrow screens */
  #footer {
    flex-wrap: wrap;
    justify-content: center;
    gap: 2px 12px;
    padding: 5px 12px;
    text-align: center;
  }

  /* Map controls: hide slider (pinch-to-zoom on touch), compact buttons */
  #map-controls {
    right: 8px;
    top: 8px;
    bottom: auto;
    gap: 3px;
  }
  #map-controls button:not(.shape-opt) {
    width: 32px;
    height: 32px;
    font-size: 16px;
  }
  #zoom-slider { display: none; }
  #zoom-pct { display: none; }

  /* Legend: hide on mobile (colors explained in stats text) */
  #legend { display: none !important; }

  /* Map: ensure minimum usable height on mobile */
  #map-wrap { min-height: 45vh; }

  /* Collapse controls toggle */
  #collapse-controls-btn {
    display: block;
    width: 100%;
    padding: 4px 12px;
    background: var(--surface);
    border: none;
    border-bottom: 1px solid var(--border);
    color: var(--muted);
    font-family: var(--font);
    font-size: 10px;
    font-weight: 400;
    letter-spacing: 0.06em;
    cursor: pointer;
    flex-shrink: 0;
    text-align: center;
    text-transform: uppercase;
  }
  #collapse-controls-btn:active { opacity: 0.8; }

  /* When collapsed, hide header and controls */
  #app.controls-collapsed #header,
  #app.controls-collapsed #controls {
    display: none;
  }
}

/* Utility */
.loading { color: var(--muted); font-style: italic; }
.error-msg { color: #ff6b6b; }
