/* Layout ownership lives here. Mobile/tablet is the base; desktop starts at 1024px.
   Layers: map 0, controls/radar 100, sheet 300, header 400, navigation HUD 600. */
:root {
  --sidebar-w: 400px;
  --map-left: 0px;
  --sheet-height: 156px;
  --keyboard-inset: 0px;
  --header-top: max(12px, env(safe-area-inset-top));
  --chrome-top: calc(var(--header-top) + 68px);
}
.app-container {
  display: block;
  height: 100vh;
  height: 100dvh;
  isolation: isolate;
}
.map-container {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  min-height: 0;
  z-index: 0;
}
.app-header {
  position: absolute;
  top: var(--header-top);
  left: max(12px, env(safe-area-inset-left));
  right: max(12px, env(safe-area-inset-right));
  height: 56px;
  padding: 6px 8px;
  gap: 8px;
  border: 1px solid var(--border-light);
  border-radius: 18px;
  background: var(--bg-overlay);
  box-shadow: var(--shadow-md);
  backdrop-filter: blur(16px);
  z-index: 400;
}
.app-header .sidebar-logo-text, .app-header .auth-user-name { display: none; }
.app-header .sidebar-logo { flex-shrink: 0; }
.mobile-search-btn {
  display: flex;
  align-items: center;
  gap: 9px;
  min-width: 0;
  flex: 1;
  text-align: left;
  font-size: .875rem;
}
.mobile-search-btn svg { flex-shrink: 0; color: var(--primary-light); }
.mobile-search-btn span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.auth-header-actions { flex-shrink: 0; }
.app-header .auth-user-badge { padding: 0; justify-content: center; }

/* Deliberately no whole-sheet touch-action override: its content scrolls normally. */
.sidebar {
  position: absolute;
  inset: auto 0 var(--keyboard-inset);
  width: 100%;
  min-width: 0;
  height: var(--sheet-height);
  max-height: none;
  margin: 0;
  border: 1px solid var(--border-light);
  border-bottom: 0;
  border-radius: 24px 24px 0 0;
  box-shadow: 0 -8px 32px rgb(0 0 0 / .28);
  z-index: 300;
  overflow: hidden;
  transition: height 220ms var(--ease), bottom 180ms var(--ease);
}
.sidebar.nav-hidden { display: none; }
.sheet-handle {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 8px 20px 10px;
  width: 100%;
  flex-shrink: 0;
  touch-action: none;
  user-select: none;
  cursor: grab;
}
.sheet-grip { width: 36px; height: 4px; border-radius: 4px; background: var(--text-3); }
.sheet-heading { width: 100%; display: flex; align-items: center; gap: 8px; text-align: left; font-size: .85rem; font-weight: 650; }
#sheet-summary { flex: 1; min-width: 0; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
#sheet-state-label { flex-shrink: 0; color: var(--text-2); font-size: .7rem; }
.sidebar-scroll { min-height: 0; overscroll-behavior-y: contain; padding-bottom: 12px; }
.sidebar[data-snap="peek"] .sidebar-scroll { visibility: hidden; overflow: hidden; }
.sidebar[data-snap="half"] .sidebar-scroll { display: flex; flex-direction: column; }
.sidebar[data-snap="half"] .sidebar-scroll > * { flex-shrink: 0; }
.sidebar[data-snap="half"] .search-section { order: 1; }
.sheet-actions {
  flex-shrink: 0;
  padding: 10px 20px max(12px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  background: var(--bg-surface);
}
.app-container.is-navigating .sheet-actions { display: none; }
.sheet-actions .calculate-btn { min-height: 48px; }
.app-container.is-dragging .sidebar, .app-container.is-dragging .radar-control-bar { transition: none; }
.app-container.is-dragging .sheet-handle { cursor: grabbing; }

/* App-owned controls have real 44px targets, including embedded location actions. */
#app button, #app [role="button"], .auth-header-actions button {
  min-width: 44px;
  min-height: 44px;
}
#app button:focus-visible, #app input:focus-visible, .toggle-row:focus-within {
  outline: 2px solid var(--primary-light);
  outline-offset: 3px;
}
.toggle-row { min-height: 44px; }
.field-input-wrap { min-width: 0; gap: 2px; }
.location-field { gap: 8px; }
.field-input-wrap .pick-map-btn {
  position: static;
  flex: 0 0 44px;
  width: 44px;
  height: 44px;
  background: var(--glass);
  border: 1px solid var(--border);
}
.location-input { flex: 1; width: 0; min-width: 0; height: 48px; padding: 10px; font-size: 16px; }
.route-connector { min-height: 44px; }
.swap-btn { position: static; margin-left: auto; }
.mode-tabs { gap: 4px; }
.mode-tab { padding-inline: 4px; font-size: .72rem; }
.map-overlay-controls {
  top: var(--chrome-top);
  bottom: auto;
  right: max(12px, env(safe-area-inset-right));
  display: grid;
  grid-template-columns: repeat(2, 44px);
  gap: 8px;
  z-index: 100;
  pointer-events: none;
}
.map-ctrl-btn { width: 44px; height: 44px; padding: 0; justify-content: center; pointer-events: auto; }
.map-ctrl-btn span { display: none; }
.map-ctrl-btn:hover { transform: none; }
.app-container.controls-occluded .map-overlay-controls { visibility: hidden; pointer-events: none; }
.app-container.controls-occluded .map-ctrl-btn { pointer-events: none; }
.route-chip { display: none; } /* The mobile summary is in the sheet handle. */

/* Compact player follows the sheet's actual height, including during a drag. */
.radar-control-bar {
  left: max(12px, env(safe-area-inset-left));
  right: max(12px, env(safe-area-inset-right));
  bottom: calc(var(--sheet-height) + var(--keyboard-inset) + 8px);
  z-index: 100;
  transform: none;
  visibility: hidden;
  transition: bottom 220ms var(--ease), opacity 180ms var(--ease);
}
.radar-control-bar.visible { visibility: visible; }
.radar-bar-inner {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  gap: 2px 8px;
  padding: 6px 10px;
  border: 1px solid var(--border-light);
  border-radius: 16px;
}
.radar-bar-inner > * { order: 0; }
.radar-bar-label { grid-column: 1; min-width: 0; }
.radar-bar-title { font-size: .66rem; letter-spacing: .025em; }
.radar-bar-controls { grid-column: 2; grid-row: 1; }
#radar-prev-btn, #radar-next-btn { display: none; }
.radar-timeline-toggle { grid-column: 3; grid-row: 1; color: var(--text-2); border-radius: 10px; font-size: .7rem; }
.radar-bar-timestamp { grid-column: 1 / 3; margin: 0; padding: 0; border: 0; font-size: .65rem; }
.radar-bar-source { display: block; grid-column: 3; font-size: .55rem; text-align: right; }
.radar-bar-timeline { display: none; grid-column: 1 / -1; width: 100%; min-width: 0; }
.radar-control-bar.timeline-open .radar-bar-timeline { display: flex; }
.radar-slider-wrap { margin: 0; padding: 0; min-height: 44px; }
.radar-slider, .radar-slider:hover, .radar-control-bar.scrubbing .radar-slider {
  height: 44px;
  background: transparent;
}
.radar-slider::-webkit-slider-runnable-track { height: 6px; border-radius: 6px; background: #374151; }
.radar-slider::-webkit-slider-thumb { margin-top: -7px; width: 20px; height: 20px; }
.radar-slider::-moz-range-track { height: 6px; border-radius: 6px; background: #374151; }
.radar-slider::-moz-range-thumb { width: 20px; height: 20px; }
.app-container.radar-occluded .radar-control-bar { visibility: hidden; pointer-events: none; }
.pick-mode-banner { top: var(--chrome-top); left: 12px; right: 120px; transform: none; z-index: 200; animation: none; }
.pick-mode-inner { white-space: normal; border-radius: 14px; padding: 8px 12px; gap: 8px; font-size: .75rem; }
.toast-container { top: calc(var(--header-top) + 60px); left: 12px; right: 12px; max-width: 340px; }
#nav-hud { left: 0; }
.app-container.is-navigating .app-header { display: none; }
.app-container.is-navigating .map-overlay-controls { top: 140px; }
.app-container.is-navigating .radar-control-bar { visibility: hidden; pointer-events: none; }
#sidebar-toggle-btn { top: 145px; bottom: auto; left: 12px; z-index: 650; }
.app-container.is-navigating .sidebar:not(.nav-hidden) { z-index: 660; }
.app-container.is-navigating .sidebar:not(.nav-hidden) ~ #nav-hud { visibility: hidden; }
.app-container.is-navigating .sidebar:not(.nav-hidden) ~ #nav-hud #nav-stop-btn { visibility: visible; }

/* Desktop: one 400px column and one flexible map, both viewport height. */
@media (min-width: 1024px) {
  :root { --map-left: var(--sidebar-w); }
  .app-container { display: grid; grid-template-columns: var(--map-left) minmax(0, 1fr); grid-template-rows: 92px minmax(0, 1fr); }
  .app-header {
    position: relative;
    inset: auto;
    grid-column: 1;
    grid-row: 1;
    height: auto;
    width: var(--sidebar-w);
    border-radius: 0;
    padding: 16px 20px;
    box-shadow: none;
    background: var(--bg-surface);
  }
  .app-header .sidebar-logo-text, .app-header .auth-user-name { display: flex; }
  .mobile-search-btn, .sheet-handle { display: none; }
  .sidebar {
    position: relative;
    inset: auto;
    grid-column: 1;
    grid-row: 2;
    width: var(--sidebar-w);
    height: 100%;
    border-radius: 0;
    border-top: 0;
    transition: none;
  }
  .sidebar-scroll, .sidebar[data-snap="half"] .sidebar-scroll { display: block; }
  .sidebar[data-snap="peek"] .sidebar-scroll { visibility: visible; overflow-y: auto; }
  .map-container { position: relative; inset: auto; grid-column: 2; grid-row: 1 / -1; width: 100%; height: 100%; min-width: 0; }
  .map-overlay-controls { top: 20px; right: 20px; display: flex; }
  .map-ctrl-btn { width: auto; padding: 8px 14px; justify-content: flex-start; }
  .map-ctrl-btn span { display: inline; }
  .radar-control-bar { left: calc(var(--map-left) + 16px); right: 16px; bottom: max(24px, env(safe-area-inset-bottom)); }
  .radar-bar-inner { display: flex; flex-wrap: wrap; gap: 8px; padding: 8px 12px; }
  .radar-bar-timeline { display: flex; flex: 1 1 150px; width: auto; }
  .radar-bar-timestamp { padding: 4px; font-size: .7rem; }
  .radar-bar-source { display: block; }
  #radar-prev-btn, #radar-next-btn { display: flex; }
  .radar-timeline-toggle { display: none; }
  .route-chip { display: flex; top: 20px; bottom: auto; left: calc(var(--map-left) + 20px); transform: none; animation: none; }
  .pick-mode-banner { left: calc(var(--map-left) + 20px); right: 160px; top: 76px; }
  #nav-hud { left: var(--map-left); }
  .app-container:has(.sidebar.nav-hidden) { --map-left: 0px; }
  .app-container:has(.sidebar.nav-hidden) .app-header { display: none; }
  .app-container.is-navigating .app-header { display: flex; }
  .app-container.is-navigating:has(.sidebar.nav-hidden) .app-header { display: none; }
  #sidebar-toggle-btn.nav-expanded { left: calc(var(--sidebar-w) + 12px); }
  .toast-container { top: 16px; left: auto; right: 160px; }
}
@media (prefers-reduced-motion: reduce) {
  .sidebar, .radar-control-bar { transition: none; }
}
