/* =========================================================================
   Right to Roam — styles
   -------------------------------------------------------------------------
   Modular vanilla CSS, organised as: tokens, reset, layout, map surface,
   hover card, legend, region table, responsive. Tier colours are NOT defined
   here — they come from js/access-data.js so the dictionary stays the single
   source of truth; this file only styles the containers they sit in.

   The map commits to one light surface. The tier palette is validated against
   that surface (lightness band, chroma floor, all-pairs colour-vision
   separation); an automatic dark flip would break that validation, so the
   page declares `color-scheme: light` rather than half-supporting dark.
   ========================================================================= */

/* ---------- tokens ---------- */
:root {
  color-scheme: light;

  --surface-map: #e9eef2;   /* ocean; the surface the palette was validated on */
  --surface-1: #ffffff;
  --surface-2: #f4f6f8;
  --ink-primary: #10181f;
  --ink-secondary: #4a5661;
  --ink-muted: #5f6b75;   /* 5.46:1 on white — small text has to clear 4.5:1 */
  --line: #d6dee5;
  --line-strong: #10181f;

  --shadow-sm: 0 1px 2px rgba(16, 24, 31, .08), 0 2px 8px rgba(16, 24, 31, .06);
  --shadow-md: 0 2px 6px rgba(16, 24, 31, .10), 0 12px 32px rgba(16, 24, 31, .14);

  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 14px;

  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;

  --header-height: 60px;
  --z-chrome: 900;      /* header, legend, drill bar — above Leaflet panes */
  --z-overlay: 1100;    /* hover card, side panel, toast */
}

/* ---------- reset ---------- */
*, *::before, *::after { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.45;
  color: var(--ink-primary);
  background: var(--surface-map);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}

h1, h2, p { margin: 0; }

:focus-visible {
  outline: 2px solid var(--line-strong);
  outline-offset: 2px;
}

/* ---------- accessibility helpers ---------- */

/* Visible to screen readers, not on screen. */
.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

/* The map is a visual view; this is the route to the equivalent text. */
.skip-link {
  position: absolute;
  top: 8px;
  left: 8px;
  z-index: 2000;
  padding: 10px 14px;
  font-size: 13px;
  font-weight: 600;
  color: var(--ink-primary);
  background: var(--surface-1);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-md);
  transform: translateY(-160%);
  transition: transform .15s ease;
}
.skip-link:focus { transform: none; }

/* ---------- shell ---------- */
.app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
}

.app-header {
  position: relative;
  z-index: var(--z-chrome);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  min-height: var(--header-height);
  padding: 10px max(16px, env(safe-area-inset-right)) 10px max(16px, env(safe-area-inset-left));
  background: var(--surface-1);
  border-bottom: 1px solid var(--line);
}

.app-header__title {
  font-size: 17px;
  font-weight: 650;
  letter-spacing: -0.01em;
}

.app-header__subtitle {
  font-size: 12.5px;
  color: var(--ink-secondary);
}

.app-header__actions { display: flex; gap: 8px; flex: 0 0 auto; }

/* ---------- buttons ---------- */
.btn {
  font: inherit;
  font-size: 13px;
  font-weight: 550;
  color: var(--ink-primary);
  background: var(--surface-1);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 7px 12px;
  cursor: pointer;
  transition: background-color .12s ease, border-color .12s ease;
}
.btn:hover { background: var(--surface-2); border-color: #bcc7d0; }
.btn:active { background: #e7ebef; }

.btn--ghost { background: transparent; }
.btn--ghost:hover { background: rgba(16, 24, 31, .06); }

.btn--icon {
  font-size: 20px;
  line-height: 1;
  padding: 2px 9px 5px;
}

/* ---------- drill-down bar ---------- */
.drill-bar {
  position: relative;
  z-index: var(--z-chrome);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 16px;
  background: var(--surface-2);
  border-bottom: 1px solid var(--line);
  animation: slide-down .18s ease-out;
}
.drill-bar[hidden] { display: none; }

.drill-bar__label { font-size: 13px; }
.drill-bar__detail { color: var(--ink-secondary); margin-left: 6px; }

@keyframes slide-down {
  from { opacity: 0; transform: translateY(-6px); }
  to   { opacity: 1; transform: none; }
}

/* ---------- map ---------- */
.map-shell {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
}

#map {
  position: absolute;
  inset: 0;
  background: var(--surface-map);
  cursor: grab;
}
#map.is-dragging { cursor: grabbing; }

/* Leaflet paints polygons as SVG paths; transitions here keep hover and
   drill-down changes smooth without touching the DOM. */
.leaflet-container .region-path {
  transition: fill-opacity .12s ease, stroke-width .12s ease;
}
.leaflet-container .region-path--interactive { cursor: pointer; }

.leaflet-container { background: var(--surface-map); font-family: var(--font); }
.leaflet-control-zoom a { color: var(--ink-primary); }

/* ---------- hover card ---------- */
.info-card {
  position: absolute;
  top: 0;
  left: 0;
  z-index: var(--z-overlay);
  width: min(300px, calc(100vw - 32px));
  padding: 12px 14px 13px;
  background: var(--surface-1);
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
  pointer-events: none;           /* never steals the pointer from the map */
  will-change: transform;         /* the card is moved with transform only */
}

/* Clicking a region pins the card: it stops following the pointer, accepts
   clicks so the reference link is reachable, and takes focus. */
.info-card.is-selected {
  pointer-events: auto;
  border-color: var(--line-strong);
  box-shadow: 0 2px 6px rgba(16, 24, 31, .12), 0 16px 40px rgba(16, 24, 31, .18);
}
.info-card.is-selected:focus { outline: 2px solid var(--line-strong); outline-offset: 2px; }

/* Pinned from the region list, where there is no pointer position to keep. */
.info-card.is-anchored {
  top: 12px;
  right: 12px;
  left: auto;
}

.info-card__close {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 26px;
  height: 26px;
  font: inherit;
  font-size: 17px;
  line-height: 1;
  color: var(--ink-secondary);
  background: none;
  border: 0;
  border-radius: var(--radius-sm);
  cursor: pointer;
}
.info-card__close:hover { background: var(--surface-2); color: var(--ink-primary); }
.info-card:not(.is-selected) .info-card__close { display: none; }

.info-card__meta {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 4px 10px;
  margin-top: 10px;
  padding-top: 9px;
  border-top: 1px solid var(--line);
  font-size: 11.5px;
  color: var(--ink-muted);
}

.info-card__source { color: var(--ink-primary); font-weight: 550; }
.info-card__source[hidden] { display: none; }
.info-card:not(.is-selected) .info-card__source-link { pointer-events: none; }

.source-link { color: inherit; text-underline-offset: 2px; }
.source-link:hover { text-decoration-thickness: 2px; }
.source-kind {
  font-weight: 400;
  color: var(--ink-muted);
}

.info-card__head {
  display: flex;
  align-items: flex-start;
  gap: 9px;
  margin-bottom: 8px;
}

.info-card__swatch {
  flex: 0 0 auto;
  width: 14px;
  height: 14px;
  margin-top: 2px;
  border-radius: 4px;
  box-shadow: inset 0 0 0 1px rgba(16, 24, 31, .18);
}

.info-card__region {
  font-size: 14.5px;
  font-weight: 650;
  letter-spacing: -0.01em;
}

.info-card__tier {
  font-size: 12px;
  font-weight: 550;
  color: var(--ink-secondary);
}

.info-card__law {
  font-size: 12.5px;
  font-weight: 550;
  color: var(--ink-primary);
  margin-bottom: 4px;
}

.info-card__description {
  font-size: 12.5px;
  color: var(--ink-secondary);
}

.info-card__water {
  margin-top: 9px;
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 12px;
  color: var(--ink-secondary);
}
.info-card__water::before {
  content: "";
  flex: 0 0 auto;
  width: 18px;
  height: 0;
  border-top: 2px solid #6fe0ff;
  box-shadow: 0 0 0 1.5px rgba(14, 27, 46, .55);
}
.info-card__water[hidden] { display: none; }

.info-card__hint {
  margin-top: 9px;
  padding-top: 8px;
  border-top: 1px solid var(--line);
  font-size: 12px;
  font-weight: 550;
  color: var(--ink-primary);
}

/* Touch devices: the card is pinned inside the map area instead of chasing a
   finger. JS adds .is-pinned from the same `(hover: none)` query it uses to
   decide whether to track the pointer, so the two can never disagree. */
.info-card.is-pinned {
  top: 12px;
  left: 12px;
  right: 12px;
  width: auto;
  transform: none !important;
}

/* ---------- legend ---------- */
.legend {
  position: absolute;
  left: max(16px, env(safe-area-inset-left));
  bottom: max(16px, env(safe-area-inset-bottom));
  z-index: var(--z-chrome);
  width: 252px;
  background: var(--surface-1);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  overflow: hidden;
}

.legend__toggle {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  padding: 10px 12px;
  font: inherit;
  font-size: 12px;
  font-weight: 650;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--ink-secondary);
  background: none;
  border: 0;
  cursor: pointer;
}

.legend__chevron {
  width: 8px;
  height: 8px;
  border-right: 1.5px solid currentColor;
  border-bottom: 1.5px solid currentColor;
  transform: rotate(45deg) translate(-2px, -2px);
  transition: transform .15s ease;
}
.legend__toggle[aria-expanded="false"] .legend__chevron {
  transform: rotate(-135deg) translate(-1px, -1px);
}

.legend__body {
  padding: 0 12px 10px;
  /* The legend grew a tier explainer and a toggle; on a short window it scrolls
     rather than running off the top of the screen. */
  max-height: calc(100dvh - 210px);
  overflow-y: auto;
}
.legend__body[hidden] { display: none; }

.legend__list { list-style: none; margin: 0; padding: 0; }

.legend__item {
  display: flex;
  align-items: flex-start;
  gap: 9px;
  padding: 4px 0;
}

.legend__swatch {
  flex: 0 0 auto;
  width: 14px;
  height: 14px;
  margin-top: 2px;
  border-radius: 4px;
  box-shadow: inset 0 0 0 1px rgba(16, 24, 31, .18);
}

.legend__label { font-size: 12.5px; font-weight: 550; }
.legend__blurb { font-size: 11.5px; line-height: 1.35; color: var(--ink-secondary); }

.legend__option {
  display: flex;
  align-items: center;
  gap: 7px;
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid var(--line);
  font-size: 12px;
  cursor: pointer;
}
.legend__option input { margin: 0; cursor: pointer; }

.legend__water-glyph {
  flex: 0 0 auto;
  width: 16px;
  height: 0;
  border-top: 2px solid #6fe0ff;
  box-shadow: 0 0 0 1.5px rgba(14, 27, 46, .55);
}

.legend__note {
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid var(--line);
  font-size: 11px;
  color: var(--ink-muted);
}

/* ---------- region table ---------- */
.table-panel {
  position: fixed;
  top: 0;
  right: 0;
  z-index: var(--z-overlay);
  display: flex;
  flex-direction: column;
  width: min(520px, 100vw);
  height: 100dvh;
  background: var(--surface-1);
  border-left: 1px solid var(--line);
  box-shadow: var(--shadow-md);
  animation: slide-in .18s ease-out;
}
.table-panel[hidden] { display: none; }

@keyframes slide-in {
  from { transform: translateX(16px); opacity: 0; }
  to   { transform: none; opacity: 1; }
}

.table-panel__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
}
.table-panel__head h2 { font-size: 15px; font-weight: 650; }
.table-panel__hint { font-size: 11.5px; color: var(--ink-muted); }

.table-panel__scroll { overflow: auto; flex: 1 1 auto; }

.region-table {
  width: 100%;
  table-layout: fixed;   /* the three columns must share the panel, not overflow it */
  border-collapse: collapse;
  font-size: 12.5px;
}

.region-table th:nth-child(1), .region-table td:nth-child(1) { width: 26%; }
.region-table th:nth-child(2), .region-table td:nth-child(2) { width: 30%; }

.region-table th {
  position: sticky;
  top: 0;
  text-align: left;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--ink-muted);
  background: var(--surface-1);
  padding: 8px 14px;
  border-bottom: 1px solid var(--line);
}

.region-table td {
  padding: 9px 14px;
  border-bottom: 1px solid var(--surface-2);
  vertical-align: top;
}

.region-table tbody tr { cursor: pointer; }
.region-table tbody tr:hover { background: var(--surface-2); }

.region-table__name { font-weight: 600; }
.region-table__parent { display: block; font-weight: 400; color: var(--ink-muted); }
.region-table__law { color: var(--ink-primary); font-weight: 550; }
.region-table__description {
  margin-top: 3px;
  font-weight: 400;
  color: var(--ink-secondary);
}
.region-table__source { margin-top: 5px; font-size: 11.5px; color: var(--ink-muted); }

.tier-tag {
  display: inline-flex;
  align-items: flex-start;
  gap: 6px;
}
.tier-tag__dot {
  flex: 0 0 auto;
  margin-top: 3px;
  width: 10px;
  height: 10px;
  border-radius: 3px;
  box-shadow: inset 0 0 0 1px rgba(16, 24, 31, .18);
}

/* ---------- toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  z-index: calc(var(--z-overlay) + 10);
  max-width: min(420px, calc(100vw - 32px));
  padding: 10px 14px;
  font-size: 12.5px;
  color: #fff;
  background: #1d262e;
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-md);
}
.toast[hidden] { display: none; }

/* ---------- responsive ---------- */
@media (max-width: 720px) {
  .app-header { gap: 10px; }
  .app-header__titles { min-width: 0; }
  .app-header__subtitle { font-size: 12px; }

  .legend {
    left: 8px;
    right: 8px;
    width: auto;
    bottom: max(8px, env(safe-area-inset-bottom));
  }

  /* Lift the zoom buttons clear of the full-width legend. */
  .leaflet-bottom.leaflet-right .leaflet-control-zoom {
    margin-bottom: calc(max(8px, env(safe-area-inset-bottom)) + 48px);
  }

  .drill-bar { flex-wrap: wrap; }
}



@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .001ms !important;
    transition-duration: .001ms !important;
  }
}
