/**
 * Tokens and layout.
 *
 * Every colour is a custom property. Waiver colours are not defined here: they
 * arrive per element as --swatch / --accent, set from palette.js.
 *
 * The palette in palette.js is shipped as hex because the map renderer does not
 * parse oklch(). The tokens below are CSS-only and may use modern syntax; convert
 * any value moved between the two files.
 */

:root {
  /* Matches the map's background colour in map.js; a mismatch shows as a seam. */
  --bg-primary: #0b0e14;
  --bg-surface: #151a24;
  --bg-raised: #1e2531;

  --text-primary: #e6e9ef;
  --text-muted: #98a2b3;

  --border: #2a3140;
  --focus: #7cc4ff;

  --radius: 10px;
  --gap: 12px;

  --legend-width: 320px;
  /* The panel is opaque so text contrast has a single background to meet. */
  --bg-panel: #0e121a;
  --rule-line: #1b212c;
  --text-meta: #7d8799;
  --font-mono: ui-monospace, "SF Mono", "Cascadia Mono", Menlo, Consolas, "DejaVu Sans Mono", monospace;

  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg-primary);
  color: var(--text-primary);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
}

#app { height: 100%; }

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

/* ---------------------------------------------------------------- selector */

.selector {
  max-width: 640px;
  margin: 0 auto;
  padding: max(32px, env(safe-area-inset-top)) 20px 40px;
}

.selector h1 {
  font-size: 24px;
  font-weight: 600;
  margin: 0 0 24px;
}

.selector-empty { color: var(--text-muted); }

.selector-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: var(--gap);
}

.selector-carrier {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 14px 16px;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: inherit;
  text-decoration: none;
}

a.selector-carrier:hover { background: var(--bg-raised); }

/* Registered but not enabled: shown greyed and inert rather than hidden. */
.selector-carrier.is-disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

/* The logo is a mask so it takes the text colour; brand navies are unreadable
   on the dark surface. */
.selector-logo {
  flex: none;
  width: 28px;
  height: 28px;
  background-color: currentColor;
  -webkit-mask: var(--logo) center / contain no-repeat;
  mask: var(--logo) center / contain no-repeat;
}

.selector-logo[hidden] { display: none; }

.selector-code {
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  letter-spacing: 0.04em;
  min-width: 2.5ch;
}

.selector-name { color: var(--text-muted); }

/* --------------------------------------------------------------------- map */

.map-shell {
  position: relative;
  height: 100%;
}

.map-canvas {
  position: absolute;
  inset: 0;
}

/* Desktop: the legend is docked, so the canvas starts to its right. */
.map-canvas { left: var(--legend-width); }

/* ------------------------------------------------------------------ legend */
/* Desktop: a full-height docked panel, always open. Mobile: a bottom sheet. */

.legend {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  width: var(--legend-width);
  display: flex;
  flex-direction: column;
  background: var(--bg-panel);
  border-right: 1px solid var(--border);
  padding-top: env(safe-area-inset-top);
  overflow: hidden;
}

/* The header is a toggle only on mobile, where app.js sets role="button". */
.legend-header {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto auto;
  align-items: center;
  column-gap: 12px;
  padding: 16px;
  border-bottom: 1px solid var(--border);
  color: var(--text-primary);
}
.legend-header[role="button"] { cursor: pointer; }
.legend-header[role="button"]:focus-visible { outline-offset: -2px; }

.legend-brand {
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
}

.legend-logo {
  width: 32px;
  height: 32px;
  background-color: currentColor;
  -webkit-mask: var(--logo) center / contain no-repeat;
  mask: var(--logo) center / contain no-repeat;
}
.legend-logo[hidden] { display: none; }

/* Fallback for a carrier with no logo. */
.legend-badge {
  min-width: 36px;
  padding: 6px 4px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font: 700 12px/1 var(--font-mono);
  letter-spacing: 0.06em;
  text-align: center;
}

.legend-carrier {
  display: flex;
  flex-direction: column;
  min-width: 0;
}
.legend-carrier-name {
  font-size: 15px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.legend-carrier-code {
  font: 12px/1.3 var(--font-mono);
  letter-spacing: 0.06em;
  color: var(--text-muted);
}

.legend-count {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
}
.legend-count-n {
  font-size: 28px;
  font-weight: 700;
  line-height: 1;
  font-variant-numeric: tabular-nums;
}
.legend-count-label {
  margin-top: 2px;
  font-size: 11px;
  color: var(--text-muted);
  white-space: nowrap;
}

/* Mobile only; the desktop panel is always open. */
.legend-chevron {
  display: none;
  width: 7px;
  height: 7px;
  margin: 0 4px;
  border-right: 2px solid var(--text-muted);
  border-bottom: 2px solid var(--text-muted);
  transform: translateY(2px) rotate(-135deg);
  transition: transform 150ms ease;
}
.legend.is-collapsed .legend-chevron { transform: rotate(45deg); }
.legend.is-collapsed .legend-header { border-bottom: 0; }

.legend-handle { display: none; }

/* --peek is the visible height of the collapsed sheet. app.js measures
   --peek-content and reads the result back from .sheet-gauge. */
:root { --peek: calc(var(--peek-content, 76px) + env(safe-area-inset-bottom)); }
.sheet-gauge {
  position: absolute;
  left: 0;
  bottom: 0;
  width: 0;
  height: var(--peek);
  visibility: hidden;
  pointer-events: none;
}

.legend-search {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
}
.legend-search input {
  width: 100%;
  padding: 8px 10px;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text-primary);
  font: inherit;
  font-size: 13.5px;
}
.legend-search input::placeholder { color: var(--text-muted); }
.legend.is-collapsed .legend-search { display: none; }

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

.legend-list {
  flex: 1;
  min-height: 0;
  list-style: none;
  margin: 0;
  padding: 6px 8px;
  overflow-y: auto;
}

.legend.is-collapsed .legend-list { display: none; }

.legend-item {
  display: grid;
  grid-template-columns: 3px 28px minmax(0, 1fr) auto;
  align-items: center;
  /* 10px keeps long names from truncating on a 320px-wide phone. */
  column-gap: 10px;
  min-height: 64px;
  padding: 0 12px 0 0;
  /* Transparent so the selected state's border does not shift layout. */
  border: 1px solid transparent;
  border-radius: 8px;
  overflow: hidden;
  color: var(--text-primary);
  cursor: pointer;
}
.legend-item + .legend-item { margin-top: 4px; }

.legend-item:hover { background: var(--bg-surface); }

/* Inset so the panel's overflow does not clip the ring. */
.legend-item:focus-visible { outline: 2px solid var(--focus); outline-offset: -2px; }

.legend-rule {
  align-self: stretch;
  background: var(--swatch);
}

/* Icons stay monochrome; colour identifies the waiver, not a category. */
.legend-icon {
  width: 28px;
  height: 28px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.75;
}

.legend-text {
  display: flex;
  flex-direction: column;
  min-width: 0;
  padding: 10px 0;
}

.legend-name {
  font-size: 14px;
  font-weight: 600;
  line-height: 1.3;
  color: var(--text-primary);
  /* Wraps to at most two lines rather than truncating to one; a single-line
     fit depends too much on the platform's fonts. */
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  overflow: hidden;
  overflow-wrap: anywhere;
}

.legend-airports {
  font-size: 12.5px;
  line-height: 1.35;
  color: var(--text-muted);
}

.legend-window {
  font: 11.5px/1.35 var(--font-mono);
  font-variant-numeric: tabular-nums;
  color: var(--text-muted);
  white-space: nowrap;
}

.legend-row-chevron {
  width: 7px;
  height: 7px;
  margin-right: 2px;
  border-right: 2px solid var(--text-muted);
  border-bottom: 2px solid var(--text-muted);
  transform: rotate(-45deg);
}

/* Selected row: a tint and border from --swatch, the same value its pins use.
   The tint strength is covered by a contrast test. */
.legend-item.is-selected {
  background: color-mix(in srgb, var(--swatch) 18%, var(--bg-panel));
  border-color: var(--swatch);
}

.legend-empty {
  margin: 0;
  padding: 14px;
  color: var(--text-muted);
}

/* ------------------------------------------------------------- seed banner */

/* See buildSeedBanner() in app.js. */
.seed-banner {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  z-index: 10;
  padding: 8px 14px calc(8px + env(safe-area-inset-top));
  background: #4a2f0b;
  border-bottom: 1px solid #8a5a12;
  color: #ffd9a0;
  font-size: 13px;
  font-weight: 600;
  text-align: center;
}

/* Push down everything the banner would cover, including the map controls. */
.seed-banner ~ .map-shell .legend { padding-top: calc(38px + env(safe-area-inset-top)); }
.seed-banner ~ .map-shell .mapboxgl-ctrl-top-right,
.seed-banner ~ .map-shell .mapboxgl-ctrl-top-left { margin-top: calc(34px + env(safe-area-inset-top)); }
.seed-banner ~ .selector { padding-top: calc(56px + env(safe-area-inset-top)); }

/* ---------------------------------------------------- notices, empty, fail */

.notice,
.empty-state {
  position: absolute;
  /* Centred on the canvas, not the shell: the docked panel takes the left. */
  left: calc(50% + var(--legend-width) / 2);
  transform: translateX(-50%);
  bottom: 48px;
  display: flex;
  align-items: center;
  gap: 10px;
  max-width: min(560px, calc(100% - 32px));
  padding: 10px 14px;
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.notice-dismiss {
  appearance: none;
  background: none;
  border: 0;
  color: var(--text-muted);
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
}

.failure {
  max-width: 560px;
  margin: 0 auto;
  padding: 48px 20px;
}

.failure h1 { font-size: 20px; }
.failure p { color: var(--text-muted); }

/* ----------------------------------------------- attribution and wordmark */

/* The attribution must stay legible and uncollapsed; let it wrap rather than
   run past the viewport edge. */
.mapboxgl-ctrl-bottom-right { max-width: min(100%, 460px); }

/* Two classes to match Mapbox's own selector specificity; a single class loses.
   Opaque so its contrast does not depend on the map underneath. */
.mapboxgl-ctrl.mapboxgl-ctrl-attrib {
  background-color: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 0;
  padding: 3px 8px;
  line-height: 1.5;
}

/* Mapbox positions controls with a two-class selector; a single-class margin
   rule would be silently outranked. */
.mapboxgl-ctrl-bottom-right .mapboxgl-ctrl-attrib,
.mapboxgl-ctrl-bottom-left .mapboxgl-ctrl-attrib {
  margin: 0 10px calc(10px + env(safe-area-inset-bottom)) 10px;
}

.mapboxgl-ctrl-attrib,
.mapboxgl-ctrl-attrib-inner,
.mapboxgl-ctrl-attrib a {
  color: var(--text-muted);
  font-size: 11px;
}

.mapboxgl-ctrl-attrib a { text-decoration: none; }
.mapboxgl-ctrl-attrib a:hover { color: var(--text-primary); text-decoration: underline; }

/* Compact zoom and compass buttons. Same specificity as Mapbox's rule; this
   sheet loads later, so it wins. */
.mapboxgl-ctrl-group button {
  width: 29px;
  height: 29px;
}

/* The Mapbox wordmark is intentionally unstyled: it may be repositioned but
   must not be hidden or recoloured. */

/* ------------------------------------------------------------------ popups */

.mapboxgl-popup-content {
  background: var(--bg-panel);
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-radius: 0;
  /* No padding so the colour rule runs flush down the left edge. */
  padding: 0;
}

/* Mapbox colours the tip per anchor with a two-class selector, so each anchor
   is overridden explicitly. */
.mapboxgl-popup-anchor-bottom .mapboxgl-popup-tip,
.mapboxgl-popup-anchor-bottom-left .mapboxgl-popup-tip,
.mapboxgl-popup-anchor-bottom-right .mapboxgl-popup-tip { border-top-color: var(--bg-panel); }
.mapboxgl-popup-anchor-top .mapboxgl-popup-tip,
.mapboxgl-popup-anchor-top-left .mapboxgl-popup-tip,
.mapboxgl-popup-anchor-top-right .mapboxgl-popup-tip { border-bottom-color: var(--bg-panel); }
.mapboxgl-popup-anchor-left .mapboxgl-popup-tip { border-right-color: var(--bg-panel); }
.mapboxgl-popup-anchor-right .mapboxgl-popup-tip { border-left-color: var(--bg-panel); }

.mapboxgl-popup-close-button {
  color: var(--text-muted);
  font-size: 18px;
  padding: 2px 8px;
}

/* Same treatment as a legend row: full-height colour rule, monochrome icon. */
.popup {
  display: grid;
  grid-template-columns: 3px minmax(0, 1fr);
  column-gap: 12px;
}

.popup-rule { background: var(--accent); }

/* Only the head row clears the close button, so the monospace travel window
   fits on one line. */
.popup-body { padding: 12px 14px 12px 0; }

.popup-head {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 6px;
  padding-right: 20px;
  color: var(--text-primary);
}

.popup-icon {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: var(--text-muted);
  stroke-width: 2;
}

.popup-code {
  font: 700 22px/1 var(--font-mono);
  letter-spacing: 0.06em;
}

.popup-airport {
  margin: 0 0 2px;
  color: var(--text-primary);
  font-size: 13px;
  font-weight: 500;
}

.popup-place {
  margin: 0 0 8px;
  color: var(--text-muted);
  font-size: 13px;
}

.popup-name {
  margin: 0 0 10px;
  font-weight: 600;
  /* Every palette slot meets 4.5:1 contrast on --bg-panel. */
  color: var(--accent);
}

.popup-dates {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 2px 12px;
  margin: 0;
  font-size: 13px;
}

.popup-dates dt { color: var(--text-muted); }
.popup-dates dd {
  margin: 0;
  font: 12px/1.5 var(--font-mono);
  font-variant-numeric: tabular-nums;
}

/* --------------------------------------------------------------- footer  */

#notice {
  position: fixed;
  /* Lifted 32px to keep clear of the map's wordmark, which sits bottom-left. */
  bottom: calc(32px + env(safe-area-inset-bottom));
  left: 0;
  padding: 4px 10px;
  font-size: 11px;
  color: var(--text-muted);
  pointer-events: none;
}

/* Desktop map view: follow the wordmark to the canvas's bottom-left. */
body:has(.map-shell) #notice { left: var(--legend-width); }

/* ------------------------------------------------------------------ mobile */

@media (max-width: 720px) {
  /* This breakpoint is duplicated in app.js's matchMedia; change both. */
  .map-canvas { left: 0; }
  .notice,
  .empty-state { left: 50%; }

  /* Bottom sheet with two detents: expanded (75% of the shell) and collapsed
     (only --peek visible). sheet.js moves it between detents during a drag. */
  .legend {
    top: auto;
    bottom: 0;
    left: 0;
    right: 0;
    width: auto;
    height: 75%;
    padding-top: 0;
    padding-bottom: env(safe-area-inset-bottom);
    border: 0;
    border-top: 1px solid var(--border);
    border-radius: var(--radius) var(--radius) 0 0;
    z-index: 3;
    transform: translateY(0);
    transition: transform 220ms ease;
  }
  .legend.is-collapsed { transform: translateY(calc(100% - var(--peek))); }
  .legend.is-dragging { transition: none; }
  .seed-banner ~ .map-shell .legend { padding-top: 0; top: auto; }

  .legend-handle {
    display: block;
    flex: none;
    height: 20px;
    touch-action: none;
    cursor: grab;
  }
  .legend-handle::before {
    content: "";
    display: block;
    width: 36px;
    height: 4px;
    margin: 8px auto 0;
    border-radius: 2px;
    background: var(--border);
  }

  .legend-header {
    padding: 0 14px 12px;
    touch-action: none;
  }
  .legend-count-n { font-size: 22px; }

  /* The chevron points the way the sheet will move. */
  .legend-chevron { display: block; transform: translateY(2px) rotate(45deg); }
  .legend.is-collapsed .legend-chevron { transform: translateY(2px) rotate(-135deg); }

  /* Keep the map's attribution, wordmark and #notice above the sheet.
     app.js updates --sheet-h on every detent change. */
  .map-shell .mapboxgl-ctrl-bottom-left,
  .map-shell .mapboxgl-ctrl-bottom-right {
    bottom: var(--sheet-h, 0px);
    transition: bottom 220ms ease;
  }
  body:has(.map-shell) #notice {
    left: 0;
    bottom: calc(32px + var(--sheet-h, 0px));
  }
  .notice,
  .empty-state { bottom: calc(48px + var(--sheet-h, 0px)); }

  /* Airport detail sheet. While open it replaces the waiver sheet, which is
     hidden in place and keeps its detent. */
  .legend.is-behind-detail { visibility: hidden; }
  .detail-sheet {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 4;
    max-height: 60%;
    overflow-y: auto;
    padding-bottom: env(safe-area-inset-bottom);
    background: var(--bg-panel);
    border-top: 1px solid var(--border);
    border-radius: var(--radius) var(--radius) 0 0;
    color: var(--text-primary);
  }
  .detail-handle {
    height: 20px;
    touch-action: none;
  }
  .detail-handle::before {
    content: "";
    display: block;
    width: 36px;
    height: 4px;
    margin: 8px auto 0;
    border-radius: 2px;
    background: var(--border);
  }
  .detail-close {
    position: absolute;
    top: 10px;
    right: 10px;
    width: 32px;
    height: 32px;
    appearance: none;
    background: none;
    border: 0;
    color: var(--text-muted);
    font-size: 22px;
    line-height: 1;
    cursor: pointer;
  }
  .detail-body { padding: 0 16px 8px; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
