/* =====================================================================
   Layout and shared primitives
   =====================================================================

   Layers: base (reset, primitives) < components (each component's own CSS,
   adopted by its module) < everything unlayered here (the layout). So a
   reset can never undo a component, and the layout can always place one.

   Mobile first. Three layouts:
     < 768px   map fills the screen; the panel is a bottom sheet; the
               sidebar is a drawer; filters scroll sideways under the header
     768-991   map and a 340px panel column; the sidebar is still a drawer
     >= 992    sidebar column 240px <-> 64px rail, map, panel 380px (closable)
   The two column widths are @property lengths (theme.css), so they animate,
   and app-shell calls map.resize() on transitionend. */

@layer base, components;

@layer base {
  *, *::before, *::after { box-sizing: border-box; }
  html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
  html, body { margin: 0; height: 100%; }
  body {
    font: 400 15px/1.5 var(--font-body);
    color: var(--ink);
    background: var(--bg);
    overflow: hidden;            /* the page never scrolls; the panel does */
    -webkit-font-smoothing: antialiased;
  }
  h1, h2, h3, p, ul, ol, figure { margin: 0; }
  ul, ol { padding: 0; }
  a { color: var(--accent); text-underline-offset: 2px; }
  button { font: inherit; color: inherit; }
  [hidden] { display: none !important; }
  svg.icon { flex: none; display: block; }
  ui-button { display: inline-flex; }

  /* Source and licence, at the end of the panel on every screen. ODbL needs
     the OpenStreetMap credit; MapLibre's own attribution stays on the map. */
  .attribution {
    padding: 14px 16px 18px; border-top: 1px solid var(--hairline);
    font-size: 12px; line-height: 1.5; color: var(--muted);
  }
  .attribution a { color: var(--ink-2); }

  a, button, summary, [role="switch"] {
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
  }

  /* One focus ring for everything: visible on both themes, never removed. */
  :focus-visible {
    outline: 2px solid var(--focus);
    outline-offset: 2px;
    border-radius: var(--radius-sm);
  }

  .visually-hidden {
    position: absolute !important; width: 1px; height: 1px; padding: 0; margin: -1px;
    overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
  }

  .display { font-family: var(--font-display); letter-spacing: 0.005em; }
  .num { font-variant-numeric: tabular-nums; }
  .muted { color: var(--muted); }

  /* ---- buttons: the primitive <ui-button> renders, also used directly ---- */
  .btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 8px;
    min-height: var(--tap); min-width: var(--tap); padding: 0 14px;
    font-weight: 600; font-size: 14px; line-height: 1.1; text-decoration: none;
    border-radius: var(--radius); border: 1px solid var(--hairline-strong);
    background: var(--surface); color: var(--ink);
    cursor: pointer; user-select: none;
    transition: background-color 0.15s ease, border-color 0.15s ease, color 0.15s ease, transform 0.1s ease;
  }
  .btn:hover { border-color: var(--ink-2); background: var(--raised); }
  .btn:active { transform: translateY(1px); }
  .btn[disabled], .btn[aria-disabled="true"] { opacity: 0.55; cursor: not-allowed; transform: none; }
  .btn--solid { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
  .btn--solid:hover { background: var(--accent); border-color: var(--accent); filter: brightness(1.08); }
  .btn--ghost { background: transparent; border-color: transparent; color: var(--ink-2); }
  .btn--ghost:hover { background: var(--raised); border-color: transparent; color: var(--ink); }
  .btn--icon { padding: 0; width: var(--tap); }
  .btn--small { min-height: 36px; padding: 0 10px; font-size: 13px; }
  /* A small button still gets a 44px target: the ::after reaches past its edges. */
  .btn--small { position: relative; }
  .btn--small::after { content: ""; position: absolute; inset: -4px; }
}

/* Tap feedback where there is no hover. */
@media (hover: none) {
  .btn:active { opacity: 0.75; }
}

/* ---- skip link ------------------------------------------------------------ */
.skip-link {
  position: fixed; left: 12px; top: -100px; z-index: 100;
  padding: 10px 14px; border-radius: var(--radius);
  background: var(--accent); color: var(--on-accent); font-weight: 600; text-decoration: none;
}
.skip-link:focus { top: calc(var(--safe-top) + 8px); }

/* =====================================================================
   Layout: phone (base)
   ===================================================================== */

app-shell { display: block; height: 100%; --toolbar-height: 58px; }

app-header {
  position: fixed; top: 0; left: 0; right: 0; z-index: 40;
  height: var(--header-total);
}

.stage { position: fixed; inset: 0; z-index: 0; }
.map { position: absolute; inset: 0; background: var(--bg); }

/* Filters: a row of chips under the header that scrolls inside itself, so the
   page never gets a horizontal scrollbar at 360px. */
.map-toolbar {
  position: absolute; z-index: 10;
  top: calc(var(--header-total) + 8px); left: 0; right: 0;
  padding: 0 max(12px, var(--safe-right)) 0 max(12px, var(--safe-left));
  overflow-x: auto; overscroll-behavior-x: contain; scrollbar-width: none;
  pointer-events: none;          /* the map stays draggable beside the chips */
}
.map-toolbar::-webkit-scrollbar { display: none; }
.map-toolbar > * { pointer-events: auto; }

/* Same 12px side margins as the filter row, and the same gap below it, so
   the two floating controls line up. */
map-legend {
  position: absolute; z-index: 10;
  right: max(12px, var(--safe-right));
  top: calc(var(--header-total) + 8px + var(--toolbar-height) + 8px);
}

/* The reopen button only exists where the panel can be closed (>= 768px). */
.panel-reopen { display: none; }

bottom-sheet { z-index: 30; }

/* MapLibre's own controls clear the header and the sheet. */
.stage .maplibregl-ctrl-top-right { top: calc(var(--header-total) + 116px); right: var(--safe-right); }
.stage .maplibregl-ctrl-bottom-right,
.stage .maplibregl-ctrl-bottom-left { bottom: var(--map-bottom-inset, 0px); transition: bottom var(--dur) var(--ease); }
/* Pinch zooms on a phone; the +/- buttons are for a mouse. */
@media (max-width: 767.98px) {
  .stage .maplibregl-ctrl-top-right .maplibregl-ctrl-group { display: none; }
}

/* =====================================================================
   Tablet: map and panel side by side
   ===================================================================== */
@media (min-width: 768px) {
  app-shell {
    --panel-size: 340px;            /* what the panel's content is laid out at */
    --panel-width: var(--panel-size); /* the column, which animates to 0 when closed */
    display: grid;
    grid-template-columns: minmax(0, 1fr) var(--panel-width);
    grid-template-rows: var(--header-total) minmax(0, 1fr);
    height: 100dvh;
    transition: --sidebar-width var(--dur) var(--ease), --panel-width var(--dur) var(--ease);
  }
  app-header { position: relative; grid-column: 1 / -1; grid-row: 1; }
  .stage { position: relative; inset: auto; grid-column: 1; grid-row: 2; min-width: 0; overflow: hidden; }
  bottom-sheet { grid-column: 2; grid-row: 2; }

  .map-toolbar { top: 12px; left: 12px; right: 64px; padding: 0; }
  map-legend { top: auto; right: auto; left: 12px; bottom: 28px; }
  .stage .maplibregl-ctrl-top-right { top: 0; }

  :root[data-panel="closed"] app-shell { --panel-width: 0px; }
  :root[data-panel="closed"] .panel-reopen { display: inline-flex; }
  .panel-reopen { position: absolute; z-index: 10; right: 10px; top: 10px; box-shadow: var(--shadow-sm); }
  :root[data-panel="closed"] .stage .maplibregl-ctrl-top-right { top: 54px; }
}

@media (min-width: 1200px) {
  app-shell { --panel-size: var(--panel-open); --panel-width: var(--panel-size); }
}

/* =====================================================================
   Desktop: sidebar column (240 <-> 64)
   ===================================================================== */
@media (min-width: 992px) {
  app-shell {
    --sidebar-width: var(--sidebar-expanded);
    grid-template-columns: var(--sidebar-width) minmax(0, 1fr) var(--panel-width);
  }
  :root[data-sidebar="collapsed"] app-shell { --sidebar-width: var(--sidebar-rail); }
  side-nav { grid-column: 1; grid-row: 2; }
  .stage { grid-column: 2; }
  bottom-sheet { grid-column: 3; }
}

/* =====================================================================
   MapLibre in both themes
   ===================================================================== */
.maplibregl-map { font: inherit; }
.maplibregl-ctrl-group {
  background: var(--surface); border: 1px solid var(--hairline); box-shadow: var(--shadow-sm) !important;
  border-radius: var(--radius) !important; overflow: hidden;
}
.maplibregl-ctrl-group button { width: 36px; height: 36px; }
.maplibregl-ctrl-group button + button { border-top: 1px solid var(--hairline); }
.maplibregl-ctrl-group button:hover { background-color: var(--raised) !important; }
/* The control icons are dark images; flip them on the dark theme. */
:root:not(.light-mode) .maplibregl-ctrl-icon { filter: invert(0.92); }
.maplibregl-ctrl-attrib { background: color-mix(in srgb, var(--surface) 88%, transparent) !important; color: var(--ink-2); }
.maplibregl-ctrl-attrib a, .maplibregl-ctrl-attrib-inner { color: var(--ink-2); }
.maplibregl-ctrl-attrib.maplibregl-compact { min-height: 24px; }
:root:not(.light-mode) .maplibregl-ctrl-attrib-button { filter: invert(0.92); }
.maplibregl-popup-content {
  padding: 10px 12px; border-radius: var(--radius); background: var(--surface); color: var(--ink);
  box-shadow: var(--shadow); border: 1px solid var(--hairline);
}
.maplibregl-popup-anchor-bottom .maplibregl-popup-tip,
.maplibregl-popup-anchor-bottom-left .maplibregl-popup-tip,
.maplibregl-popup-anchor-bottom-right .maplibregl-popup-tip { border-top-color: var(--surface); }
.maplibregl-popup-anchor-top .maplibregl-popup-tip,
.maplibregl-popup-anchor-top-left .maplibregl-popup-tip,
.maplibregl-popup-anchor-top-right .maplibregl-popup-tip { border-bottom-color: var(--surface); }
.maplibregl-popup-anchor-left .maplibregl-popup-tip { border-right-color: var(--surface); }
.maplibregl-popup-anchor-right .maplibregl-popup-tip { border-left-color: var(--surface); }

/* =====================================================================
   Preferences
   ===================================================================== */
/* Reduced motion removes what moves on its own: entrance staggers, the
   skeleton shimmer, smooth scrolling, the map's camera flights (map.js).
   It keeps the short transitions a visitor starts (sidebar, panel, drawer,
   switches, filter thumb): under 0.35s, never looping, and they show where
   things went, which helps orientation. Killing those too is what made the
   whole interface jump on a Windows machine with animation effects off. */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important; animation-iteration-count: 1 !important;
    scroll-behavior: auto !important;
  }
}

/* Glass becomes solid where translucency is unsupported, unwanted, or where the
   visitor asked for more contrast (Safari has no reduced-transparency query;
   Increase Contrast travels with Reduce Transparency on Apple systems). */
@supports not ((backdrop-filter: blur(1px)) or (-webkit-backdrop-filter: blur(1px))) {
  :root { --glass: var(--surface); }
}
@media (prefers-reduced-transparency: reduce) {
  :root, :root.light-mode { --glass: var(--surface); --glass-blur: 0px; --glass-border: var(--hairline); }
  .side-nav__scrim { -webkit-backdrop-filter: none; backdrop-filter: none; }
}
@media (prefers-contrast: more) {
  :root, :root.light-mode { --glass: var(--surface); --glass-blur: 0px; --glass-border: var(--hairline-strong); --hairline: var(--hairline-strong); }
}

@media (forced-colors: active) {
  .btn, .chip { border: 1px solid ButtonText; }
}
