/* ==========================================================================
   Map pages — full-screen map, header card, one collapsible side panel
   that turns into a bottom sheet on narrow screens.
   ========================================================================== */

.map-page {
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  --panel-w: 320px;
  --edge: 16px;
}

.map-canvas {
  position: fixed;
  inset: 0;
  z-index: 0;
  background: var(--bg);
}

/* Leaflet theming -------------------------------------------------------- */
.leaflet-container {
  font-family: var(--font-sans);
  background: var(--bg);
}

.leaflet-container a {
  color: var(--accent-text);
}

/* Keyless dark fallback: OpenStreetMap tiles recoloured to a dark palette */
.tiles-dark {
  filter: invert(1) hue-rotate(180deg) brightness(0.9) contrast(0.85) saturate(0.6);
}

/* The base reset caps media at 100% width; Leaflet's canvas is larger on purpose. */
.leaflet-container canvas,
.leaflet-container svg {
  max-width: none !important;
  max-height: none !important;
}

.leaflet-control-zoom.leaflet-bar,
.leaflet-bar {
  border: 1px solid var(--border) !important;
  border-radius: var(--radius-md) !important;
  overflow: hidden;
  box-shadow: var(--shadow-panel) !important;
}

.leaflet-bar a,
.leaflet-bar a:hover {
  width: 40px !important;
  height: 40px !important;
  line-height: 40px !important;
  background: var(--surface-overlay) !important;
  color: var(--text) !important;
  border-bottom-color: var(--border) !important;
  -webkit-backdrop-filter: var(--blur);
  backdrop-filter: var(--blur);
}

.leaflet-bar a:hover {
  background: var(--bg-raised) !important;
}

.leaflet-bar a.leaflet-disabled {
  color: var(--text-faint) !important;
}

.leaflet-control-scale-line {
  background: var(--surface-overlay);
  border-color: var(--text-muted);
  color: var(--text);
}

.leaflet-control-attribution {
  max-width: calc(100vw - 2 * var(--edge));
  background: var(--surface-overlay) !important;
  color: var(--text-muted);
  font-size: 11px;
  border-top-left-radius: var(--radius-sm);
}

.leaflet-control-attribution a {
  color: var(--text);
}

.leaflet-popup-content-wrapper,
.leaflet-popup-tip {
  background: var(--bg-raised);
  color: var(--text);
  box-shadow: var(--shadow-panel);
}

.leaflet-popup-content-wrapper {
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
}

.leaflet-popup-content {
  margin: 14px 18px;
  font-size: var(--fs-sm);
  line-height: 1.5;
}

.leaflet-popup-content p {
  margin: 0.25em 0;
}

.leaflet-container a.leaflet-popup-close-button {
  color: var(--text-muted);
  width: 30px;
  height: 30px;
  font-size: 20px;
  line-height: 28px;
}

.leaflet-tooltip {
  background: var(--bg-raised);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-panel);
  font-size: var(--fs-xs);
}

.leaflet-tooltip-top::before { border-top-color: var(--bg-raised); }
.leaflet-tooltip-bottom::before { border-bottom-color: var(--bg-raised); }
.leaflet-tooltip-left::before { border-left-color: var(--bg-raised); }
.leaflet-tooltip-right::before { border-right-color: var(--bg-raised); }

.popup-title {
  display: block;
  margin-bottom: 0.35em;
  font-size: var(--fs-base);
  font-weight: 600;
  color: var(--project-accent-text);
}

.popup-row {
  display: block;
  color: var(--text-muted);
}

.popup-row b {
  color: var(--text);
  font-weight: 600;
}

/* Keep Leaflet's own controls clear of our overlays */
.map-page .leaflet-top.leaflet-left {
  top: calc(var(--map-header-h, 0px) + var(--edge));
}

.map-page .leaflet-top.leaflet-right {
  top: calc(var(--edge) - 10px);
}

.map-page .leaflet-left {
  left: calc(var(--edge) - 10px);
}

.map-page .leaflet-right {
  right: calc(var(--panel-w) + var(--edge) * 2 - 10px);
}

/* Header card ------------------------------------------------------------ */
.map-header {
  position: fixed;
  top: var(--edge);
  left: var(--edge);
  z-index: var(--z-map-ui);
  width: min(440px, calc(100vw - var(--panel-w) - var(--edge) * 3));
  padding: var(--space-4) var(--space-5) var(--space-5);
  border-radius: var(--radius-lg);
  border: 1px solid var(--border);
  background: var(--surface-overlay);
  -webkit-backdrop-filter: var(--blur);
  backdrop-filter: var(--blur);
  box-shadow: var(--shadow-panel);
}

.map-header__top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  margin-bottom: var(--space-4);
}

.map-header .back-link {
  background: var(--surface);
}

.map-header__title {
  font-size: var(--fs-md);
  line-height: 1.25;
}

.map-header__title::before {
  content: "";
  display: block;
  width: 28px;
  height: 3px;
  margin-bottom: var(--space-3);
  border-radius: 3px;
  background: var(--project-accent);
}

.map-header__subtitle {
  margin-top: var(--space-2);
  font-size: var(--fs-sm);
  line-height: 1.45;
}

.map-header__actions {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  margin-top: var(--space-4);
}

/* Side panel ------------------------------------------------------------ */
.map-panel {
  position: fixed;
  top: var(--edge);
  right: var(--edge);
  bottom: calc(var(--edge) + 18px);
  z-index: var(--z-map-ui);
  display: flex;
  flex-direction: column;
  width: var(--panel-w);
  max-height: calc(100dvh - var(--edge) * 2 - 18px);
  height: max-content;
  border-radius: var(--radius-lg);
  border: 1px solid var(--border);
  background: var(--surface-overlay);
  -webkit-backdrop-filter: var(--blur);
  backdrop-filter: var(--blur);
  box-shadow: var(--shadow-panel);
  transition: transform var(--dur) var(--ease-out);
}

.map-panel__toggle {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  width: 100%;
  min-height: 52px;
  padding: var(--space-3) var(--space-5);
  color: var(--text);
  font-size: var(--fs-sm);
  font-weight: 600;
  text-align: left;
  border-radius: var(--radius-lg);
}

.map-panel__toggle:hover {
  background: var(--surface);
}

.map-panel__toggle .icon-chevron {
  transition: transform var(--dur) var(--ease-out);
}

.map-panel__toggle[aria-expanded="false"] .icon-chevron {
  transform: rotate(180deg);
}

.map-panel__label {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
}

.map-panel__body {
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 0 var(--space-5) var(--space-5);
}

.map-panel__body[hidden] {
  display: none;
}

.panel-section + .panel-section {
  margin-top: var(--space-5);
  padding-top: var(--space-4);
  border-top: 1px solid var(--border);
}

.panel-section__title {
  margin-bottom: var(--space-2);
  color: var(--text-muted);
  font-size: var(--fs-xs);
  font-weight: 600;
}

.panel-note {
  margin-top: var(--space-2);
  font-size: var(--fs-xs);
  line-height: 1.5;
  color: var(--text-faint);
}

.panel-section p {
  font-size: var(--fs-xs);
  line-height: 1.55;
}

/* Legend rows (checkbox or radio + swatch + text) */
.legend-list {
  display: grid;
  gap: 2px;
  list-style: none;
}

.legend-row {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  min-height: 38px;
  padding: 0.3rem var(--space-2);
  margin-inline: calc(var(--space-2) * -1);
  border-radius: var(--radius-sm);
  font-size: var(--fs-sm);
  color: var(--text);
  cursor: pointer;
  user-select: none;
}

.legend-row:hover {
  background: var(--surface);
}

.legend-row input {
  width: 16px;
  height: 16px;
  flex: none;
  accent-color: var(--project-accent);
  cursor: pointer;
}

.legend-row:has(input:not(:checked)) .swatch,
.legend-row:has(input:not(:checked)) .legend-row__text {
  opacity: 0.45;
}

.legend-row__text {
  flex: 1;
  line-height: 1.3;
}

.legend-row__count {
  color: var(--text-faint);
  font-size: var(--fs-xs);
  font-variant-numeric: tabular-nums;
}

.swatch {
  width: 18px;
  height: 18px;
  flex: none;
  border-radius: 5px;
  background: var(--swatch, #888);
  border: 1px solid rgba(255, 255, 255, 0.25);
}

.swatch--dot {
  border-radius: 50%;
}

.swatch--line {
  height: 0;
  border: 0;
  border-top: 3px dashed var(--swatch, #888);
  border-radius: 0;
}

.swatch--outline {
  background: transparent;
  border: 2px dashed var(--swatch, #888);
}

.panel-actions {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  margin-top: var(--space-3);
}

/* Basemap switcher ------------------------------------------------------ */
.basemap-switch {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: var(--space-2);
  border: 0;
}

.basemap-switch legend {
  margin-bottom: var(--space-2);
  color: var(--text-muted);
  font-size: var(--fs-xs);
  font-weight: 600;
  grid-column: 1 / -1;
}

.basemap-option {
  position: relative;
}

.basemap-option input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.basemap-option label {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  min-height: 40px;
  padding: 0.35rem 0.6rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface);
  font-size: var(--fs-xs);
  font-weight: 500;
  cursor: pointer;
  transition: border-color var(--dur-fast) ease, background var(--dur-fast) ease;
}

.basemap-option label:hover {
  border-color: var(--border-strong);
}

.basemap-option input:checked + label {
  border-color: var(--project-accent);
  background: color-mix(in srgb, var(--project-accent) 14%, transparent);
}

.basemap-option input:focus-visible + label {
  outline: 2px solid var(--accent-text);
  outline-offset: 2px;
}

.basemap-thumb {
  width: 22px;
  height: 22px;
  flex: none;
  border-radius: 5px;
  border: 1px solid rgba(127, 127, 127, 0.4);
}

.basemap-thumb--dark { background: linear-gradient(135deg, #2b2b2e 50%, #46464a 50%); }
.basemap-thumb--light { background: linear-gradient(135deg, #f2f2f0 50%, #dcdcd8 50%); }
.basemap-thumb--voyager { background: linear-gradient(135deg, #f5efe3 45%, #a6d3e6 45%, #a6d3e6 60%, #d9e8c7 60%); }
.basemap-thumb--satellite { background: linear-gradient(135deg, #2f4a2a 40%, #6b6a4b 40%, #6b6a4b 62%, #1f3a5c 62%); }
.basemap-thumb--osm { background: linear-gradient(135deg, #f2efe9 50%, #aad3df 50%); }

/* Loader ---------------------------------------------------------------- */
.map-loader {
  position: fixed;
  inset: 0;
  z-index: var(--z-loader);
  display: grid;
  place-content: center;
  justify-items: center;
  gap: var(--space-4);
  background: var(--bg);
  transition: opacity var(--dur-slow) ease, visibility 0s linear var(--dur-slow);
}

.map-loader[data-done] {
  opacity: 0;
  visibility: hidden;
}

.map-loader__spinner {
  width: 46px;
  height: 46px;
  border-radius: 50%;
  border: 3px solid var(--border);
  border-top-color: var(--project-accent);
  animation: spin 0.9s linear infinite;
}

.map-loader__text {
  color: var(--text-muted);
  font-size: var(--fs-sm);
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* Toast ----------------------------------------------------------------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--edge) + 28px);
  z-index: var(--z-toast);
  width: max-content;
  max-width: min(460px, calc(100vw - 2 * var(--edge)));
  padding: var(--space-3) var(--space-5);
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: var(--bg-raised);
  color: var(--text);
  font-size: var(--fs-sm);
  box-shadow: var(--shadow-panel);
  transform: translate(-50%, 0);
  animation: toast-in var(--dur) var(--ease-out);
}

.toast[hidden] {
  display: none;
}

@keyframes toast-in {
  from { opacity: 0; transform: translate(-50%, 12px); }
}

/* Narrow screens: compact header, panel becomes a bottom sheet ---------- */
@media (max-width: 760px) {
  .map-page {
    --edge: 10px;
    --sheet-peek: 56px;
  }

  .map-header {
    width: calc(100vw - var(--edge) * 2);
    padding: var(--space-3) var(--space-4);
  }

  .map-header__top {
    margin-bottom: var(--space-2);
  }

  .map-header__title {
    font-size: var(--fs-base);
  }

  .map-header__title::before {
    display: none;
  }

  .map-header__subtitle {
    display: none;
  }

  .map-header__actions {
    margin-top: var(--space-3);
  }

  .map-panel {
    top: auto;
    left: var(--edge);
    right: var(--edge);
    bottom: var(--edge);
    width: auto;
    max-height: 58dvh;
    border-radius: var(--radius-lg);
  }

  .map-panel__toggle {
    min-height: var(--sheet-peek);
  }

  .map-page .leaflet-right {
    right: 0;
  }

  .map-page .leaflet-top.leaflet-right,
  .map-page .leaflet-top.leaflet-left {
    top: calc(var(--map-header-h, 0px) + var(--edge));
  }

  .map-page .leaflet-bottom {
    bottom: calc(var(--sheet-peek) + var(--edge) + 6px);
  }

  .map-page .leaflet-control-attribution {
    font-size: 10px;
  }

  .toast {
    bottom: calc(var(--sheet-peek) + var(--edge) * 2 + 32px);
  }
}
