:root {
  --bg: #111;
  --map-bg: #000000;
  --panel-bg: rgba(15,15,15,0.92);
  --panel-bg-soft: rgba(15,15,15,0.88);
  --text: #ddd;
  --text-dim: #ccc;
  --text-faint: #888;
  --border: rgba(255,255,255,0.07);
  --border-strong: rgba(255,255,255,0.15);
  --input-bg: rgba(255,255,255,0.06);
  --placeholder: #666;
  --popup-bg: rgba(4,4,4,0.95);
  --popup-border: rgba(255,255,255,0.1);
  --popup-shadow: rgba(0,0,0,0.8);
  --loader-bg: rgba(0,0,0,0.75);
}
[data-theme="light"] {
  --bg: #eef0f2;
  --map-bg: #eef0f2;
  --panel-bg: rgba(255,255,255,0.92);
  --panel-bg-soft: rgba(255,255,255,0.88);
  --text: #222;
  --text-dim: #444;
  --text-faint: #888;
  --border: rgba(0,0,0,0.08);
  --border-strong: rgba(0,0,0,0.15);
  --input-bg: rgba(0,0,0,0.04);
  --placeholder: #999;
  --popup-bg: rgba(255,255,255,0.97);
  --popup-border: rgba(0,0,0,0.1);
  --popup-shadow: rgba(0,0,0,0.25);
  --loader-bg: rgba(255,255,255,0.85);
}

* { margin: 0; padding: 0; box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg); -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
#map { width: 100vw; height: 100vh; }

#loader {
  position: fixed; inset: 0; background: var(--loader-bg);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  color: var(--text); z-index: 999; gap: 14px;
}
#loader p { font-size: 13px; opacity: 0.6; }
.spinner {
  width: 38px; height: 38px; border: 3px solid var(--border-strong);
  border-top-color: var(--text); border-radius: 50%; animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

#legend {
  position: fixed;
  bottom: calc(20px + env(safe-area-inset-bottom));
  left: calc(14px + env(safe-area-inset-left));
  background: var(--panel-bg); color: var(--text);
  border-radius: 10px; padding: 12px 13px; z-index: 10;
  width: 200px; backdrop-filter: blur(8px);
  border: 1px solid var(--border);
}
.legend-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 12px;
}
#legend h3 {
  font-size: 11px; text-transform: uppercase; letter-spacing: 0.09em;
  opacity: 0.5;
}

/* mobile-only retractable legend — hidden on desktop, see the media query below */
.legend-toggle {
  display: none;
  align-items: center; justify-content: center;
  width: 24px; height: 24px; border-radius: 50%; flex-shrink: 0;
  background: var(--input-bg); border: 1px solid var(--border-strong);
  color: var(--text-dim); padding: 0; cursor: pointer;
}
.legend-toggle-chevron {
  display: block; font-size: 12px; line-height: 1;
  transition: transform 0.25s ease;
}
#legend.legend-expanded .legend-toggle-chevron { transform: rotate(180deg); }
.legend-gradient {
  height: 12px;
  border-radius: 3px;
  background: linear-gradient(to right,
    #7b2d98 0%, #214e91 50%, #d6c41d 100%);
}
.legend-labels {
  display: flex; justify-content: space-between;
  font-size: 10px; opacity: 0.55; margin-top: 4px;
}
.legend-divider { border: none; border-top: 1px solid var(--border); margin: 12px 0; }

.year-filter-label { display: block; font-size: 11px; color: var(--text-dim); margin-bottom: 6px; }
.year-filter-row { display: flex; align-items: center; gap: 6px; }
.year-filter-input {
  width: 100%; min-width: 0; background: var(--input-bg); color: var(--text);
  border: 1px solid var(--border-strong); border-radius: 5px;
  padding: 5px 7px; font-size: 12px;
}
.year-filter-input::placeholder { color: var(--placeholder); }
.year-filter-sep { color: var(--text-faint); font-size: 11px; flex-shrink: 0; }

/* two native range inputs stacked on the same track, each showing only its
   thumb (the track itself is transparent — .year-range-track/-fill draw the
   visible bar underneath) so it reads as one dual-handle slider */
.year-range-slider { position: relative; height: 18px; margin: 8px 0; }
.year-range-track {
  position: absolute; top: 50%; left: 0; right: 0; height: 3px;
  transform: translateY(-50%); background: var(--border-strong); border-radius: 2px;
}
.year-range-fill {
  position: absolute; top: 50%; height: 3px;
  transform: translateY(-50%); background: #d6c41d; border-radius: 2px;
}
.year-range-slider input[type="range"] {
  position: absolute; top: 50%; left: 0; width: 100%; margin: 0;
  transform: translateY(-50%);
  -webkit-appearance: none; appearance: none;
  background: transparent; pointer-events: none;
}
.year-range-slider input[type="range"]::-webkit-slider-runnable-track { -webkit-appearance: none; height: 3px; background: transparent; }
.year-range-slider input[type="range"]::-moz-range-track { height: 3px; background: transparent; border: none; }
.year-range-slider input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none; pointer-events: auto;
  width: 13px; height: 13px; margin-top: -5px; border-radius: 50%;
  background: #d6c41d; border: 2px solid var(--panel-bg); cursor: pointer;
}
.year-range-slider input[type="range"]::-moz-range-thumb {
  pointer-events: auto; width: 11px; height: 11px; border-radius: 50%;
  background: #d6c41d; border: 2px solid var(--panel-bg); cursor: pointer;
}

.map-toggle {
  display: flex; align-items: center; gap: 7px;
  font-size: 11px; color: var(--text-dim); cursor: pointer; user-select: none;
}
.map-toggle + .map-toggle { margin-top: 8px; }
/* The checkbox stays in the DOM and keeps every native behaviour — tab
   focus, space to toggle, and the `change` event map.js listens on. It's
   only hidden visually; display:none would drop it out of the tab order
   and make the switch unreachable without a mouse. */
.map-toggle input {
  position: absolute; width: 1px; height: 1px; margin: -1px;
  overflow: hidden; clip-path: inset(50%); white-space: nowrap;
}
.map-switch {
  position: relative; flex: none; width: 28px; height: 16px;
  border-radius: 999px;
  background: var(--input-bg); border: 1px solid var(--border-strong);
  transition: background .16s ease, border-color .16s ease;
}
.map-switch::after {
  content: ''; position: absolute; top: 2px; left: 2px;
  width: 10px; height: 10px; border-radius: 50%;
  background: var(--text-faint);
  transition: transform .16s ease, background .16s ease;
}
.map-toggle input:checked + .map-switch {
  background: #d6c41d; border-color: #d6c41d;
}
/* 12px = the 26px inner width, less the 10px knob and its 2px left offset,
   so the knob lands the same distance from the right edge as it started
   from the left */
.map-toggle input:checked + .map-switch::after {
  transform: translateX(12px); background: #1a1a1a;
}
.map-toggle input:focus-visible + .map-switch {
  outline: 2px solid var(--border-strong); outline-offset: 2px;
}

/* ── popup ── */
.maplibregl-popup-content {
  background: var(--popup-bg);
  border: 1px solid var(--popup-border);
  border-radius: 10px;
  padding: 13px 15px;
  color: var(--text);
  font-size: 12px;
  min-width: 170px;
  box-shadow: 0 0 30px var(--popup-shadow);
  backdrop-filter: blur(10px);
}
.maplibregl-popup-tip { display: none; }
.maplibregl-popup { pointer-events: all; }

.p-year { font-size: 26px; font-weight: 700; line-height: 1.1; margin-bottom: 2px; }
.p-year.no-data { color: var(--text-faint); }
/* an era span is nine characters where an exact year is four — at the full 26px
   it wraps inside the popup's 170px minimum. Smaller also reads as less certain,
   which is the truth about it. No negative tracking here: the hyphen is already
   tight against the digits at this weight, and pulling it in further makes
   "1968-1991" read as a single number. */
.p-year.range { font-size: 21px; white-space: nowrap; }
.p-conf { font-size: 10px; margin-bottom: 8px; letter-spacing: 0.03em; }
.p-conf.known     { color: #39ff14; }
.p-conf.inferred  { color: #00b8cc; }
.p-conf.suggested { color: #39ff14; }
.p-conf.none      { color: var(--text-faint); }
.p-name  { font-size: 13px; font-weight: 600; margin-bottom: 3px; color: var(--text); }
.p-addr  { font-size: 11px; color: var(--text-faint); margin-bottom: 4px; }

/* suggest-a-year form — only rendered when the page calls initBuildingMap({editable: true}) */
.p-suggest-input {
  width: 66px; background: var(--input-bg); color: var(--text);
  border: 1px solid var(--border-strong); border-radius: 5px;
  padding: 4px 6px; font-size: 12px;
}
.p-suggest-input::placeholder { color: var(--placeholder); }
.p-suggest-approx {
  display: flex; align-items: center; gap: 4px;
  font-size: 10px; color: var(--text-faint); cursor: pointer; user-select: none;
}
.p-suggest-approx input { accent-color: #d68a00; }

/* edit form — name, address and year under one save button; editable: true only */
.p-edit {
  margin-top: 9px; padding-top: 9px;
  border-top: 1px solid var(--border);
  display: flex; flex-direction: column; gap: 6px;
}
.p-edit-year-row { display: flex; align-items: center; gap: 6px; }
.p-edit-name, .p-edit-street, .p-edit-number {
  background: var(--input-bg); color: var(--text);
  border: 1px solid var(--border-strong); border-radius: 5px;
  padding: 4px 6px; font-size: 12px; width: 100%; min-width: 0;
}
.p-edit-name::placeholder, .p-edit-street::placeholder, .p-edit-number::placeholder {
  color: var(--placeholder);
}
.p-edit-addr-row { display: flex; gap: 6px; }
.p-edit-street { flex: 2; }
.p-edit-number { flex: 1; }
.p-edit-save {
  align-self: flex-start;
  background: rgba(255,170,0,0.12); color: #d68a00;
  border: 1px solid rgba(255,170,0,0.3); border-radius: 5px;
  padding: 4px 8px; font-size: 11px; cursor: pointer;
}
.p-edit-save:hover { background: rgba(255,170,0,0.22); }
.p-edit-msg { font-size: 10px; color: var(--text-faint); }
.p-gmaps {
  display: inline-block; margin-top: 9px; padding-top: 9px;
  border-top: 1px solid var(--border);
  font-size: 11px; color: #0066ff; text-decoration: none; width: 100%;
}
.p-gmaps:hover { color: #00a2b8; }

#stats {
  position: fixed;
  top: calc(14px + env(safe-area-inset-top));
  right: calc(14px + env(safe-area-inset-right));
  background: var(--panel-bg-soft); color: var(--text-dim);
  border-radius: 8px; padding: 10px 14px; font-size: 11px;
  z-index: 10; backdrop-filter: blur(6px); line-height: 1.7;
  border: 1px solid var(--border);
  display: none;
}
#stats .stat-label { opacity: 0.5; }
/* same rule the legend draws, but the stats box is compact (10px padding,
   11px text) and the legend's 12px margins would leave it floating */
#stats .legend-divider { margin: 6px 0; }

/* ── mobile ── */
@media (max-width: 640px) {
  #legend {
    width: auto;
    left: calc(10px + env(safe-area-inset-left));
    right: calc(10px + env(safe-area-inset-right));
    bottom: calc(10px + env(safe-area-inset-bottom));
    padding: 11px 12px;
  }
  #stats {
    top: calc(10px + env(safe-area-inset-top));
    right: calc(10px + env(safe-area-inset-right));
    padding: 7px 10px; font-size: 10px; line-height: 1.5;
  }
  .maplibregl-popup { max-width: min(260px, calc(100vw - 32px)) !important; }

  /* retractable legend: slid down shows only the color scale; sliding it up
     (tapping the chevron) reveals the year filter and light-mode toggle */
  .legend-toggle { display: flex; }
  #legend-extra {
    max-height: 0; opacity: 0; overflow: hidden;
    transition: max-height 0.25s ease, opacity 0.2s ease;
  }
  #legend.legend-expanded #legend-extra { max-height: 280px; opacity: 1; }
}

/* ── touch targets — anything driven by a finger, regardless of screen size ── */
@media (pointer: coarse) {
  .maplibregl-popup-close-button { width: 30px; height: 30px; font-size: 18px; line-height: 30px; }

  .year-range-slider { height: 26px; }
  .year-range-slider input[type="range"]::-webkit-slider-thumb {
    width: 22px; height: 22px; margin-top: -9.5px;
  }
  .year-range-slider input[type="range"]::-moz-range-thumb {
    width: 20px; height: 20px;
  }

  .year-filter-input, .p-suggest-input,
  .p-edit-name, .p-edit-street, .p-edit-number {
    padding: 9px 10px; font-size: 16px; /* 16px avoids iOS auto-zoom-on-focus */
  }
  .p-edit-save {
    padding: 9px 13px; font-size: 13px;
  }
  /* a touch target rather than a pointer one */
  .map-switch { width: 34px; height: 20px; }
  .map-switch::after { width: 14px; height: 14px; }
  .map-toggle input:checked + .map-switch::after { transform: translateX(14px); }
  .p-suggest-approx input { width: 16px; height: 16px; }
}
