/* Aetrain UI tokens + base styles. The polish pass is Phase 3 — for now
   this preserves the legacy look-and-feel exactly. Custom properties are
   declared at :root so component CSS (and future themes) can read them. */

:root {
  --ae-bg: #0b0f19;
  --ae-bg-2: #121827;
  --ae-bg-3: #1a2035;
  --ae-fg: #f1f5f9;
  --ae-fg-muted: #94a3b8;
  --ae-fg-dim: #475569;
  --ae-fg-soft: #64748b;
  --ae-fg-strong: #e2e8f0;
  --ae-border: #1e293b;
  --ae-border-soft: #232b42;
  --ae-accent: #f59e0b;
  --ae-accent-deep: #b45309;
  --ae-danger: #f43f5e;
  --ae-success: #10b981;
  --ae-suggest: #818cf8;
  --ae-font-display: "Outfit", sans-serif;
  --ae-font-mono: "JetBrains Mono", monospace;
  --ae-radius-sm: 3px;
  --ae-radius-md: 6px;
  --ae-radius-pill: 999px;
}

html, body { width: 100%; height: 100%; overflow: hidden; }
* { margin: 0; padding: 0; box-sizing: border-box; }
body { background: var(--ae-bg); color: var(--ae-fg); font-family: var(--ae-font-display); overflow: hidden; }

#app { position: fixed; inset: 0; display: grid; grid-template-columns: clamp(18rem, 28vw, 24rem) minmax(0, 1fr); min-width: 0; overflow: hidden; }
/* Custom-element wrappers must be transparent to the grid so #side and #map
   become direct grid items. Without `display: contents` they default to
   `display: inline` and the grid sees a single inline child instead of the
   sidebar + map columns. */
ae-app, ae-sidebar { display: contents; }

#side { width: auto; min-width: 0; min-height: 0; background: var(--ae-bg-2); border-right: 1px solid var(--ae-border); display: grid; grid-template-rows: minmax(0, 1fr) auto; overflow: hidden; z-index: 1000; }
.side-main { min-height: 0; display: flex; flex-direction: column; overflow-y: auto; scrollbar-width: thin; scrollbar-color: var(--ae-border-soft) transparent; }
.side-main::-webkit-scrollbar { width: 3px; }
.side-main::-webkit-scrollbar-thumb { background: var(--ae-border-soft); border-radius: 3px; }

.sh { padding: 1.125rem 1.375rem .875rem; border-bottom: 1px solid var(--ae-border); }
.sh h1 { font-family: var(--ae-font-mono); font-size: 15px; font-weight: 600; color: var(--ae-accent); letter-spacing: 2px; text-transform: uppercase; }
.sh p { font-size: 11px; color: var(--ae-fg-muted); margin-top: 3px; font-weight: 300; }

.source-meta { margin-top: 8px; font-size: 10px; line-height: 1.4; color: var(--ae-fg-soft); }

/* Hard error state shown by ae-app when the dataset fails to load. The
   shell never mounts in this case; the error card replaces the children
   of #app entirely. */
.ae-load-error {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: var(--ae-bg);
  padding: 1.5rem;
  z-index: 9000;
}
.ae-load-error-card {
  max-width: 32rem;
  background: var(--ae-bg-2);
  border: 1px solid var(--ae-border);
  border-radius: var(--ae-radius-md);
  padding: 1.75rem 1.75rem 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.875rem;
  font-family: var(--ae-font-display);
}
.ae-load-error-card h1 { font-family: var(--ae-font-mono); font-size: 14px; color: var(--ae-danger); letter-spacing: 1px; text-transform: uppercase; margin: 0; }
.ae-load-error-card p { font-size: 13px; color: var(--ae-fg-soft); line-height: 1.5; margin: 0; }
.ae-load-error-detail { font-family: var(--ae-font-mono); font-size: 11px; color: var(--ae-fg-muted); background: var(--ae-bg); border: 1px solid var(--ae-border); border-radius: var(--ae-radius-md); padding: .625rem .75rem; margin: 0; white-space: pre-wrap; word-break: break-word; max-height: 8rem; overflow: auto; }
.ae-load-error-card .btn { align-self: flex-start; padding: .5rem 1.25rem; }

.stats { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: .5rem; padding: .625rem 1.375rem; border-bottom: 1px solid var(--ae-border); background: var(--ae-bg-3); flex-shrink: 0; }
.st { flex: 1; text-align: center; }
.st b { font-family: var(--ae-font-mono); font-size: 18px; color: var(--ae-accent); display: block; }
.st small { font-size: 8px; color: var(--ae-fg-dim); text-transform: uppercase; letter-spacing: .8px; }

.filters { padding: .75rem 1.375rem; border-bottom: 1px solid var(--ae-border); background: var(--ae-bg-3); flex-shrink: 0; }
.filters summary { font-size: 11px; color: var(--ae-fg-muted); cursor: pointer; font-weight: 500; letter-spacing: .5px; text-transform: uppercase; list-style: none; display: flex; align-items: center; gap: 6px; user-select: none; }
.filters summary::before { content: '▸'; font-size: 9px; transition: transform .2s; }
.filters[open] summary::before { transform: rotate(90deg); }
.fc { margin-top: 10px; display: flex; flex-direction: column; gap: 10px; }
.fg { display: flex; align-items: center; gap: 10px; }
.fg label { font-size: 10px; color: var(--ae-fg-muted); width: 56px; flex-shrink: 0; text-transform: uppercase; letter-spacing: .3px; }
.fg input[type=range] { flex: 1; -webkit-appearance: none; height: 4px; background: var(--ae-border-soft); border-radius: 2px; outline: none; }
.fg input[type=range]::-webkit-slider-thumb { -webkit-appearance: none; width: 14px; height: 14px; border-radius: 50%; background: var(--ae-accent); cursor: pointer; border: 2px solid var(--ae-bg); }
.fg .fv { font-family: var(--ae-font-mono); font-size: 11px; color: var(--ae-accent); width: 42px; text-align: right; flex-shrink: 0; cursor: pointer; user-select: none; }
.fg .fv:hover { text-decoration: underline; text-underline-offset: 2px; }
.fg .fv-input { font-family: var(--ae-font-mono); font-size: 11px; color: var(--ae-accent); width: 42px; text-align: right; flex-shrink: 0; background: transparent; border: none; border-bottom: 1px solid var(--ae-accent); outline: none; padding: 0; margin: 0; -moz-appearance: textfield; }
.fg .fv-input::-webkit-outer-spin-button, .fg .fv-input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.fi { font-size: 10px; color: var(--ae-fg-dim); margin-top: 4px; font-style: italic; }

.sb { padding: .625rem 1.375rem; border-bottom: 1px solid var(--ae-border); position: relative; flex-shrink: 0; }
.sb input { width: 100%; padding: 9px 12px 9px 14px; background: var(--ae-bg); border: 1px solid var(--ae-border); border-radius: var(--ae-radius-md); color: var(--ae-fg); font-family: var(--ae-font-display); font-size: 13px; outline: none; }
.sb input:focus { border-color: var(--ae-accent); }
.sb input::placeholder { color: var(--ae-fg-dim); }

#sr { position: absolute; top: 100%; left: 1.375rem; right: 1.375rem; background: var(--ae-bg-2); border: 1px solid var(--ae-border); border-top: none; border-radius: 0 0 var(--ae-radius-md) var(--ae-radius-md); max-height: 260px; overflow-y: auto; z-index: 2000; display: none; }
.sri { padding: 7px 12px; cursor: pointer; font-size: 12px; display: flex; align-items: center; gap: 8px; }
.sri:hover { background: var(--ae-bg-3); }
.sri .sn { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sri .sc { font-size: 10px; color: var(--ae-fg-dim); }
.sri .sq { display: flex; gap: 2px; flex-shrink: 0; }
.sri .sq i { display: inline-block; width: 5px; height: 5px; border-radius: 50%; background: var(--ae-fg-dim); }
.sri .sq i.on { background: var(--ae-accent); }
.sri.act { opacity: .45; }
.sri.act::after { content: '\2713'; color: var(--ae-accent); font-weight: 700; margin-left: 4px; }

#tl { flex: 1 1 auto; min-height: 0; padding: 4px 0; }
.ts { display: flex; align-items: flex-start; padding: 8px 22px; gap: 10px; position: relative; }
.ts:hover { background: rgba(245, 158, 11, .03); }
.tn { width: 24px; height: 24px; border-radius: 50%; background: var(--ae-accent); color: var(--ae-bg); font-family: var(--ae-font-mono); font-size: 11px; font-weight: 600; display: flex; align-items: center; justify-content: center; flex-shrink: 0; }
.ti { flex: 1; min-width: 0; }
.ti .cn { font-size: 13px; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ti .cc { font-size: 10px; color: var(--ae-fg-dim); }
.ti .tt { display: inline-flex; align-items: center; gap: 3px; font-family: var(--ae-font-mono); font-size: 10px; color: var(--ae-success); margin-top: 3px; background: rgba(16, 185, 129, .08); padding: 1px 7px; border-radius: var(--ae-radius-sm); }
.ti .tt.err { color: var(--ae-danger); background: rgba(244, 63, 94, .08); }
.tx { width: 32px; height: 32px; min-width: 32px; min-height: 32px; border: none; background: transparent; color: var(--ae-fg-dim); cursor: pointer; border-radius: var(--ae-radius-sm); font-size: 14px; display: flex; align-items: center; justify-content: center; flex-shrink: 0; margin-top: -2px; }
.tx:hover { background: rgba(244, 63, 94, .12); color: var(--ae-danger); }
.tx:focus-visible { outline: 2px solid var(--ae-accent); outline-offset: 2px; }
.tcon { position: absolute; left: 33px; top: -4px; width: 2px; height: 12px; background: var(--ae-accent); opacity: .2; }
.suggest { margin: 2px 22px 8px 46px; padding: 6px 10px; background: rgba(129, 140, 248, .06); border: 1px solid rgba(129, 140, 248, .12); border-radius: var(--ae-radius-md); font-size: 11px; color: var(--ae-suggest); cursor: pointer; transition: all .15s; display: flex; align-items: center; gap: 6px; }
.suggest:hover { background: rgba(129, 140, 248, .12); border-color: rgba(129, 140, 248, .25); }
.suggest:focus-visible { outline: 2px solid var(--ae-suggest); outline-offset: 2px; }
.suggest .sg-n { font-weight: 600; }
.suggest .sg-i { font-size: 10px; color: var(--ae-fg-muted); margin-left: auto; flex-shrink: 0; }
ae-trip-list[data-revealed="true"] .suggest { animation: ae-suggestion-reveal 120ms ease-out both; }
@keyframes ae-suggestion-reveal {
  from { opacity: 0; transform: translateY(-2px); }
  to { opacity: 1; transform: translateY(0); }
}
@media (prefers-reduced-motion: reduce) {
  ae-trip-list[data-revealed="true"] .suggest { animation: none; }
}
.sri.kbnav { background: var(--ae-bg-3); outline: 1px solid var(--ae-accent); outline-offset: -1px; }

#empty { padding: 2.25rem 1.375rem; text-align: center; color: var(--ae-fg-dim); font-size: 12px; line-height: 1.7; font-weight: 300; }
#empty .icon { font-size: 28px; margin-bottom: 10px; opacity: .3; }

.actions { padding: .75rem 1.375rem calc(.75rem + env(safe-area-inset-bottom, 0px)); border-top: 1px solid var(--ae-border); display: flex; gap: .5rem; flex-shrink: 0; background: var(--ae-bg-2); min-height: 58px; }
.btn { flex: 1; padding: 9px; border: none; border-radius: var(--ae-radius-md); font-family: var(--ae-font-display); font-size: 12px; font-weight: 600; cursor: pointer; }
.bp { background: var(--ae-accent); color: var(--ae-bg); }
.bp:hover { background: var(--ae-accent-deep); }
.bd { background: rgba(244, 63, 94, .1); color: var(--ae-danger); }
.bd:hover { background: rgba(244, 63, 94, .18); }

#map { width: 100%; height: 100%; min-width: 0; min-height: 0; background: var(--ae-bg); }

/* Top-right map debug controls. Sits inside the right grid column via
   absolute positioning relative to the #app grid. The perf HUD docks
   below it (see perf-hud.ts top offset). */
ae-debug-toggles {
  position: absolute;
  top: 12px;
  right: 12px;
  z-index: 850;
  display: block;
  pointer-events: auto;
}
.ae-debug-toggles {
  display: inline-flex;
  align-items: stretch;
  gap: 4px;
  padding: 4px;
  background: rgba(11, 15, 25, 0.9);
  border: 1px solid var(--ae-border);
  border-radius: 6px;
  backdrop-filter: blur(4px);
  font-family: 'JetBrains Mono', ui-monospace, SFMono-Regular, monospace;
  font-size: 10px;
  line-height: 1;
}
.ae-debug-toggle {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 5px 8px;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 4px;
  color: var(--ae-fg-dim);
  font-family: inherit;
  font-size: 9px;
  cursor: pointer;
  transition: background-color .12s, color .12s, border-color .12s;
  min-width: 48px;
}
.ae-debug-toggle:hover { background: rgba(245, 158, 11, .08); color: var(--ae-fg); }
.ae-debug-toggle:focus-visible { outline: 2px solid var(--ae-accent); outline-offset: 2px; }
.ae-debug-toggle.is-active { background: rgba(245, 158, 11, .15); color: var(--ae-accent); border-color: rgba(245, 158, 11, .35); }
.ae-debug-label { text-transform: uppercase; letter-spacing: .5px; opacity: .7; }
.ae-debug-state { color: inherit; font-weight: 600; }
.ae-debug-toggle.is-active .ae-debug-state { color: var(--ae-accent); }
.ae-debug-badge {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 5px 8px;
  border-radius: 4px;
  border: 1px solid transparent;
  font-size: 9px;
  background: transparent;
  color: var(--ae-fg-dim);
  cursor: default;
  min-width: 48px;
}
.ae-debug-badge.engine-wasm { color: #38bdf8; border-color: rgba(56, 189, 248, .3); }
.ae-debug-badge.engine-js-fallback { color: var(--ae-danger); border-color: rgba(244, 63, 94, .3); }

@media (max-width: 800px) {
  ae-debug-toggles { top: 8px; right: 8px; }
  .ae-debug-toggle, .ae-debug-badge { min-width: 42px; padding: 4px 6px; }
}

.leaflet-container { background: var(--ae-bg) !important; font-family: var(--ae-font-display) !important; }
.leaflet-control-zoom a { background: var(--ae-bg-2) !important; color: var(--ae-fg) !important; border-color: var(--ae-border) !important; width: 30px !important; height: 30px !important; line-height: 30px !important; font-size: 15px !important; }
.leaflet-control-zoom a:hover { background: var(--ae-bg-3) !important; color: var(--ae-accent) !important; }
.leaflet-control-zoom { border: 1px solid var(--ae-border) !important; border-radius: var(--ae-radius-md) !important; overflow: hidden; }
.leaflet-control-zoom a:first-child { border-bottom: 1px solid var(--ae-border) !important; }
.leaflet-control-attribution { background: rgba(11, 15, 25, .85) !important; color: var(--ae-fg-dim) !important; font-size: 9px !important; }
.leaflet-control-attribution a { color: var(--ae-accent) !important; }
.leaflet-control-layers { background: var(--ae-bg-2) !important; border: 1px solid var(--ae-border) !important; border-radius: var(--ae-radius-md) !important; color: var(--ae-fg) !important; font-size: 11px !important; }
.leaflet-control-layers-toggle { background-color: var(--ae-bg-2) !important; border: 1px solid var(--ae-border) !important; border-radius: var(--ae-radius-md) !important; width: 30px !important; height: 30px !important; }
.leaflet-control-layers label { color: var(--ae-fg) !important; }
.leaflet-tooltip { font-family: var(--ae-font-display) !important; }
.city-lbl { background: transparent !important; border: none !important; box-shadow: none !important; color: var(--ae-fg-muted) !important; font-size: 10px !important; font-family: var(--ae-font-display) !important; white-space: nowrap !important; padding: 0 !important; pointer-events: none !important; }
.city-lbl.top { color: var(--ae-fg-strong) !important; font-weight: 500 !important; font-size: 11px !important; }
.city-lbl.trip-lbl { color: var(--ae-accent) !important; font-weight: 600 !important; font-size: 12px !important; font-family: var(--ae-font-mono) !important; text-shadow: 0 0 6px rgba(0, 0, 0, .8), 0 0 3px rgba(0, 0, 0, .9) !important; }
.city-lbl::before { display: none !important; }

.dual-range { position: relative; flex: 1; height: 20px; }
.dual-range input[type=range] { position: absolute; top: 0; left: 0; width: 100%; -webkit-appearance: none; background: transparent; pointer-events: none; height: 20px; margin: 0; }
.dual-range input[type=range]::-webkit-slider-thumb { -webkit-appearance: none; width: 16px; height: 16px; border-radius: 50%; background: var(--ae-accent); cursor: pointer; border: 2px solid var(--ae-bg); pointer-events: all; position: relative; z-index: 2; box-shadow: 0 0 4px rgba(0, 0, 0, .4); }
.dual-range input[type=range]::-moz-range-thumb { width: 16px; height: 16px; border-radius: 50%; background: var(--ae-accent); cursor: pointer; border: 2px solid var(--ae-bg); pointer-events: all; position: relative; z-index: 2; }
.dual-track { position: absolute; top: 9px; left: 0; right: 0; height: 4px; background: var(--ae-border-soft); border-radius: 2px; pointer-events: none; }
.dual-fill { position: absolute; top: 9px; height: 4px; background: var(--ae-accent); border-radius: 2px; pointer-events: none; opacity: .4; }

#citycount { position: absolute; bottom: 12px; right: 56px; z-index: 800; background: var(--ae-bg-2); border: 1px solid var(--ae-border); border-radius: var(--ae-radius-md); padding: 5px 10px; font-family: var(--ae-font-mono); font-size: 10px; color: var(--ae-fg-muted); pointer-events: none; max-width: min(16rem, calc(100vw - 24px)); }
#citycount b { color: var(--ae-accent); }

@media (max-width: 1100px) {
  #app { grid-template-columns: clamp(17rem, 32vw, 22rem) minmax(0, 1fr); }
  .sh, .stats, .filters, .sb, .actions { padding-left: 1rem; padding-right: 1rem; }
  .stats { gap: .375rem; }
  #sr { left: 1rem; right: 1rem; }
}
@media (max-width: 800px) {
  body { overflow: hidden; }
  /* On narrow viewports the sidebar sits below the map. The map takes the
     primary row; the sidebar scrolls independently in the row beneath it.
     Grid `order` flips the visual stacking without touching the DOM order
     (which keeps tab order: sidebar first → map second). */
  #app { grid-template-columns: 1fr; grid-template-rows: minmax(0, 1fr) minmax(18rem, 50dvh); }
  #app > #map { order: 0; }
  #app > aside#side, #app > #side { order: 1; width: 100%; min-width: 100%; height: auto; max-height: none; border-right: none; border-top: 1px solid var(--ae-border); border-bottom: none; }
  .stats { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  #citycount { right: 12px; bottom: 12px; max-width: calc(100vw - 24px); }
  /* Search dropdown becomes self-scrolling so long result lists don't
     push other panels offscreen on touch devices. */
  #sr { max-height: 50vh; overflow-y: auto; }
  /* Bigger tap targets on touch. */
  .tx { width: 36px; height: 36px; min-width: 36px; min-height: 36px; font-size: 16px; }
}
@media (max-width: 560px) {
  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .fg { gap: .625rem; }
  .fg label { width: 3rem; }
  .actions { grid-template-columns: 1fr; display: grid; }
  .btn { width: 100%; }
}
