/* ============================================================
   CargoPro — Mobile Layout (v2 overhaul)
   Loads AFTER tokens.css, styles.css, ui.css so it can override
   desktop rules. Scoped primarily to @media (max-width: 768px).

   Design goals:
   - Map-first: the star map is always visible on launch.
   - 4-tab bottom nav: Plan · Cargo · Route · More.
   - Floating action button for the one-primary-action at any time.
   - Compact route chip at top of map when a route exists.
   - More bottom-sheet holds Scan / GA / Import / Export / Reset / Theme / About.
   ============================================================ */

/* ── Mobile-only chrome (hidden on desktop, present in DOM) ─── */
#mobile-tabbar,
#mobile-backdrop,
#mobile-more-sheet,
#mobile-fab,
#mobile-route-chip { display: none; }

/* ────────────────────────────────────────────────────────────
   PRIMARY MOBILE BREAKPOINT
   ──────────────────────────────────────────────────────────── */
@media (max-width: 768px) {

  /* ── Viewport ────────────────────────────────────────────── */
  :root {
    --topbar-h: 52px;
    --mobile-tabbar-h: 62px;
    --mobile-gutter: 12px;
    --mobile-radius: 14px;
    --mobile-shadow: 0 10px 28px rgba(0, 0, 0, 0.42);
  }

  html, body {
    height: 100%;
    overflow: hidden;
    overscroll-behavior: none;
    -webkit-text-size-adjust: 100%;
    touch-action: manipulation;
  }

  .app {
    height: 100vh;
    height: 100dvh;
    padding-top: env(safe-area-inset-top, 0);
    padding-bottom: calc(var(--mobile-tabbar-h) + env(safe-area-inset-bottom, 0));
  }

  body.mobile-panel-open { overflow: hidden; }

  /* ── Top bar — simplified ────────────────────────────────── */
  .topbar {
    padding: 0;
    padding-left: env(safe-area-inset-left, 0);
    padding-right: env(safe-area-inset-right, 0);
    background: var(--bg-1);
    border-bottom: 1px solid var(--line-2);
  }

  .topbar-brand {
    padding: 0 14px;
    gap: 10px;
    border-right: 1px solid var(--line-2);
  }
  .topbar-brand-name { display: inline-flex; font-size: 11px; letter-spacing: 0.06em; }
  .topbar-brand-version { display: none; }

  .topbar-actions {
    padding-right: 8px;
    gap: 2px;
  }

  .topbar-btn {
    width: 40px;
    height: 40px;
    min-width: 40px;
  }

  /* On mobile the More sheet owns About / Clear-cache / Theme.
     Hide the topbar buttons that duplicate them + the panel-toggle icons. */
  .topbar-btn[title="About / Disclaimer"],
  .topbar-btn[title="Clear price cache"],
  .topbar-btn[title="Toggle theme"],
  .topbar-btn[title="Toggle setup panel"],
  .topbar-btn[title="Toggle route panel"] {
    display: none !important;
  }

  /* ── Workbench + map ─────────────────────────────────────── */
  .workbench {
    position: relative;
    flex: 1 1 auto;
    min-height: 0;
  }

  #map-area,
  .map-wrap {
    flex: 1 1 auto;
    width: 100%;
    min-width: 0;
  }

  #starmap {
    touch-action: none; /* map.js handles pan/pinch */
  }

  /* ── Side panels — full-height slide-in drawers ─────────── */
  #left-panel,
  #left-panel.open,
  #route-panel,
  #route-panel.open {
    position: fixed !important;
    top: calc(var(--topbar-h) + env(safe-area-inset-top, 0)) !important;
    bottom: calc(var(--mobile-tabbar-h) + env(safe-area-inset-bottom, 0)) !important;
    height: auto !important;
    max-height: none !important;
    margin: 0 !important;
    z-index: 120;
    box-shadow: var(--mobile-shadow);
    transition: transform 0.28s cubic-bezier(.2, .7, .2, 1) !important;
    border-top: none !important;
    border-bottom: none !important;
    overflow: hidden;
    display: flex !important;
    flex-direction: column !important;
  }

  #left-panel,
  #left-panel.open {
    left: 0 !important;
    right: auto !important;
    width: min(88vw, 380px) !important;
    min-width: 0 !important;
    max-width: min(88vw, 380px) !important;
    opacity: 1 !important;
    visibility: visible !important;
    border-left: none !important;
    border-right: 1px solid var(--line-2) !important;
    flex: 0 0 auto !important;
  }
  #left-panel { transform: translateX(-102%) !important; }
  #left-panel.open { transform: translateX(0) !important; }

  #route-panel,
  #route-panel.open {
    right: 0 !important;
    left: auto !important;
    width: min(94vw, 420px) !important;
    min-width: 0 !important;
    max-width: min(94vw, 420px) !important;
    opacity: 1 !important;
    visibility: visible !important;
    border-right: none !important;
    border-left: 1px solid var(--line-2) !important;
    flex: 0 0 auto !important;
  }
  #route-panel { transform: translateX(102%) !important; }
  #route-panel.open { transform: translateX(0) !important; }

  #left-panel.setup-options-mode {
    height: auto !important;
    max-height: none !important;
    bottom: calc(var(--mobile-tabbar-h) + env(safe-area-inset-bottom, 0)) !important;
    margin: 0 !important;
    align-self: auto !important;
  }

  .sc-panel-content {
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    padding-bottom: 14px;
  }

  /* ── Backdrop ────────────────────────────────────────────── */
  #mobile-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.52);
    backdrop-filter: blur(3px);
    -webkit-backdrop-filter: blur(3px);
    z-index: 110;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.22s ease;
  }
  body.mobile-panel-open #mobile-backdrop {
    opacity: 1;
    pointer-events: auto;
  }

  /* ── Desktop bottom bar hidden on mobile ─────────────────── */
  #bottom-bar { display: none !important; }

  .panel-pull-tab,
  .panel-pull-tab-left,
  .panel-pull-tab-right { display: none !important; }

  /* ─────────────────────────────────────────────────────────
     MOBILE TAB BAR — 4 tabs, bigger icons, cleaner active state
     ─────────────────────────────────────────────────────── */
  #mobile-tabbar {
    display: flex;
    position: fixed;
    left: 0; right: 0; bottom: 0;
    height: calc(var(--mobile-tabbar-h) + env(safe-area-inset-bottom, 0));
    padding-bottom: env(safe-area-inset-bottom, 0);
    padding-left: env(safe-area-inset-left, 0);
    padding-right: env(safe-area-inset-right, 0);
    background: color-mix(in srgb, var(--bg-1) 92%, transparent);
    backdrop-filter: saturate(160%) blur(14px);
    -webkit-backdrop-filter: saturate(160%) blur(14px);
    border-top: 1px solid var(--line-2);
    z-index: 130;
    align-items: stretch;
    box-shadow: 0 -6px 24px rgba(0, 0, 0, 0.28);
  }

  .mobile-tab {
    flex: 1 1 0;
    min-width: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 4px;
    padding: 8px 4px;
    background: transparent;
    border: none;
    color: var(--text-2);
    font-family: var(--font-ui);
    font-size: 10.5px;
    font-weight: 600;
    letter-spacing: 0.02em;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    position: relative;
    transition: color 0.15s ease, transform 0.08s ease;
  }

  .mobile-tab:active { transform: scale(0.96); }

  .mobile-tab-icon {
    width: 26px;
    height: 26px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: currentColor;
    transition: transform 0.15s ease;
  }
  .mobile-tab-icon svg {
    width: 24px;
    height: 24px;
    stroke-width: 1.8;
  }

  .mobile-tab-label {
    line-height: 1;
    opacity: 0.9;
  }

  .mobile-tab.active {
    color: var(--accent);
  }
  .mobile-tab.active .mobile-tab-icon {
    transform: translateY(-1px);
  }
  .mobile-tab.active::before {
    content: "";
    position: absolute;
    top: 0;
    left: 26%;
    right: 26%;
    height: 3px;
    background: var(--accent);
    border-radius: 0 0 3px 3px;
  }

  .mobile-tab-badge {
    position: absolute;
    top: 4px;
    right: calc(50% - 20px);
    min-width: 16px;
    height: 16px;
    padding: 0 5px;
    background: var(--warn);
    color: #0a0a0a;
    border-radius: 8px;
    font-family: var(--font-mono);
    font-size: 9px;
    font-weight: 700;
    line-height: 16px;
    text-align: center;
  }

  /* ─────────────────────────────────────────────────────────
     MOBILE ROUTE CHIP — at top-center of map when route exists
     ─────────────────────────────────────────────────────── */
  #mobile-route-chip {
    position: fixed;
    top: calc(var(--topbar-h) + env(safe-area-inset-top, 0) + 10px);
    left: 50%;
    transform: translateX(-50%);
    z-index: 90;
    display: inline-flex;
    align-items: center;
    gap: 10px;
    padding: 9px 12px 9px 14px;
    min-height: 40px;
    max-width: calc(100vw - 28px);
    background: var(--bg-3);
    border: 1px solid var(--line-2);
    border-radius: 999px;
    color: var(--text-0);
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.35);
    cursor: pointer;
    font-family: var(--font-ui);
    -webkit-tap-highlight-color: transparent;
    transition: transform 0.15s ease, opacity 0.15s ease;
  }
  #mobile-route-chip:active { transform: translateX(-50%) scale(0.98); }

  .mobile-route-chip-dot {
    width: 8px; height: 8px;
    border-radius: 50%;
    background: var(--accent);
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent);
    flex-shrink: 0;
    animation: sc-pulse-dot 2.2s ease-in-out infinite;
  }
  @keyframes sc-pulse-dot {
    0%, 100% { box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent); }
    50%      { box-shadow: 0 0 0 6px color-mix(in srgb, var(--accent) 0%, transparent); }
  }

  .mobile-route-chip-text {
    display: flex;
    flex-direction: column;
    gap: 1px;
    min-width: 0;
    text-align: left;
  }
  .mobile-route-chip-title {
    font-size: 12px;
    font-weight: 600;
    color: var(--text-0);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 56vw;
  }
  .mobile-route-chip-meta {
    font-size: 10px;
    color: var(--text-2);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 56vw;
  }
  .mobile-route-chip-arrow {
    width: 14px;
    height: 14px;
    color: var(--text-2);
    flex-shrink: 0;
  }

  /* ─────────────────────────────────────────────────────────
     MOBILE FAB — primary action
     ─────────────────────────────────────────────────────── */
  #mobile-fab {
    position: fixed;
    right: calc(var(--mobile-gutter) + env(safe-area-inset-right, 0));
    bottom: calc(var(--mobile-tabbar-h) + env(safe-area-inset-bottom, 0) + 16px);
    z-index: 125;
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 14px 18px;
    min-height: 52px;
    border: none;
    border-radius: 26px;
    background: var(--accent);
    color: #041914;
    font-family: var(--font-ui);
    font-size: 13px;
    font-weight: 700;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    box-shadow: 0 10px 24px rgba(0, 0, 0, 0.35),
                0 0 0 1px color-mix(in srgb, var(--accent) 40%, transparent);
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    transition: transform 0.12s ease, box-shadow 0.18s ease, background 0.15s ease;
  }
  #mobile-fab svg {
    width: 18px;
    height: 18px;
  }
  #mobile-fab:active {
    transform: scale(0.96);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  }
  #mobile-fab.is-setup {
    background: var(--info);
    color: #051220;
    box-shadow: 0 10px 24px rgba(0, 0, 0, 0.35),
                0 0 0 1px color-mix(in srgb, var(--info) 40%, transparent);
  }
  #mobile-fab.is-calculate {
    background: var(--warn);
    color: #1a1200;
    box-shadow: 0 10px 24px rgba(0, 0, 0, 0.35),
                0 0 0 1px color-mix(in srgb, var(--warn) 40%, transparent);
  }
  #mobile-fab.is-route-ready {
    background: var(--accent);
    color: #041914;
  }

  /* Hide FAB when any panel or sheet is open to keep focus clear */
  body.mobile-panel-open #mobile-fab { opacity: 0; pointer-events: none; }

  /* Hide chip when any drawer overlays the map */
  body.mobile-panel-open #mobile-route-chip { opacity: 0; pointer-events: none; }

  /* ─────────────────────────────────────────────────────────
     MOBILE MORE SHEET
     ─────────────────────────────────────────────────────── */
  #mobile-more-sheet {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 180;
    pointer-events: none;
  }
  #mobile-more-sheet.open { pointer-events: auto; }

  .mobile-sheet-panel {
    position: absolute;
    left: 0; right: 0; bottom: 0;
    background: var(--bg-1);
    border-top-left-radius: 18px;
    border-top-right-radius: 18px;
    border-top: 1px solid var(--line-2);
    box-shadow: 0 -20px 40px rgba(0, 0, 0, 0.5);
    padding-bottom: env(safe-area-inset-bottom, 0);
    transform: translateY(100%);
    transition: transform 0.28s cubic-bezier(.2, .7, .2, 1);
    max-height: 82vh;
    max-height: calc(100dvh - var(--topbar-h) - 24px);
    display: flex;
    flex-direction: column;
  }
  #mobile-more-sheet.open .mobile-sheet-panel {
    transform: translateY(0);
  }
  .mobile-sheet-handle {
    width: 40px;
    height: 4px;
    margin: 8px auto 0;
    border-radius: 2px;
    background: var(--line-3);
    flex-shrink: 0;
  }
  .mobile-sheet-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 16px 4px;
    flex-shrink: 0;
  }
  .mobile-sheet-title {
    font-family: var(--font-ui);
    font-size: 14px;
    font-weight: 600;
    color: var(--text-0);
    letter-spacing: 0.02em;
  }
  .mobile-sheet-close {
    width: 36px;
    height: 36px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    border: none;
    color: var(--text-2);
    cursor: pointer;
    border-radius: 8px;
    -webkit-tap-highlight-color: transparent;
  }
  .mobile-sheet-close:active { background: var(--bg-3); }

  .mobile-sheet-body {
    padding: 4px 8px 18px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    display: flex;
    flex-direction: column;
    gap: 2px;
    flex: 1 1 auto;
    min-height: 0;
  }

  .mobile-sheet-item {
    display: flex;
    align-items: center;
    gap: 14px;
    padding: 12px 12px;
    border-radius: 10px;
    background: transparent;
    border: none;
    text-align: left;
    color: var(--text-0);
    font-family: var(--font-ui);
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    min-height: 56px;
    transition: background 0.12s ease;
  }
  .mobile-sheet-item:active { background: var(--bg-3); }

  .mobile-sheet-item-icon {
    width: 36px;
    height: 36px;
    flex-shrink: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: var(--bg-3);
    border-radius: 10px;
    color: var(--accent);
  }
  .mobile-sheet-item-icon svg { width: 18px; height: 18px; }

  .mobile-sheet-item-danger .mobile-sheet-item-icon {
    color: var(--bad);
    background: var(--bad-tint);
  }
  .mobile-sheet-item-danger .mobile-sheet-item-title { color: var(--bad); }

  .mobile-sheet-item-text {
    display: flex;
    flex-direction: column;
    gap: 2px;
    min-width: 0;
    flex: 1 1 auto;
  }
  .mobile-sheet-item-title {
    font-size: 14px;
    font-weight: 600;
    color: var(--text-0);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .mobile-sheet-item-sub {
    font-size: 11px;
    color: var(--text-2);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .mobile-sheet-divider {
    height: 1px;
    background: var(--line-1);
    margin: 8px 12px;
    flex-shrink: 0;
  }

  /* ─────────────────────────────────────────────────────────
     Map chrome — clean + unobtrusive on mobile
     ─────────────────────────────────────────────────────── */
  .map-legend { display: none !important; }

  .sc-map-icons {
    top: 10px;
    right: 10px;
    gap: 6px;
    max-width: calc(100% - 20px);
    flex-wrap: wrap;
    justify-content: flex-end;
  }
  .sc-map-icon-btn {
    width: 40px;
    height: 40px;
    font-size: 15px;
    background: color-mix(in srgb, var(--bg-3) 88%, transparent);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
  }

  /* System selector lives bottom-LEFT so it doesn't fight the FAB at
     bottom-right. The map legend is hidden at this breakpoint, so the
     bottom-left strip is otherwise free. */
  #system-selector {
    left: calc(var(--mobile-gutter) + env(safe-area-inset-left, 0));
    right: auto;
    bottom: calc(var(--mobile-tabbar-h) + env(safe-area-inset-bottom, 0) + 14px);
    max-width: calc(100vw - var(--mobile-gutter) * 2 - 160px);
    gap: 4px;
    flex-wrap: wrap;
    justify-content: flex-start;
  }
  .sys-btn {
    padding: 8px 12px;
    font-size: 10px;
    min-height: 36px;
  }

  /* Map label-visibility legend overlaps the system selector at this
     breakpoint — hide it. (styles.css already hides at ≤640px; we
     extend coverage to all phone widths.) */
  .map-legend { display: none !important; }

  /* ─────────────────────────────────────────────────────────
     Tap-target enforcement (Phase 6 a11y)
     Every interactive control needs ≥44×44 logical px on phones
     so finger taps land reliably. The audit at Phase 6 found a
     handful of legacy desktop sizes (icons 38px, close buttons
     26px, system selector 36px) — the rules below normalise
     them. Visual icon size stays where it is; the extra padding
     just expands the hit zone.
     ─────────────────────────────────────────────────────── */
  .sc-map-icon-btn,
  #distToolBtn,
  #gaSettingsMapBtn { min-width: var(--tap); min-height: var(--tap); }
  .sys-btn         { min-height: var(--tap); padding: 10px 14px; }
  /* `.right-tab-btn` and the route .btn-sm pieces have explicit `height`
     declarations in styles.css; `height: auto` lets `min-height` win. */
  .rpanel-tabs    { height: auto !important; }
  .right-tab-btn  { height: auto !important; min-height: var(--tap); }
  .fp-close,
  .mobile-sheet-close,
  .modal-close-btn { min-width: var(--tap); min-height: var(--tap); }
  #route-panel .btn-sm,
  .route-actions-secondary .btn-sm,
  .setup-options-reset-btn,
  .route-run-btn,
  #routePrimaryBtn { min-height: var(--tap) !important; height: auto !important; }

  /* ─────────────────────────────────────────────────────────
     Scroll containers — make every modal / drawer / sheet body
     scrollable on phones. The desktop layouts often delegate
     scroll to inner panes that lose their height constraint
     when the layout reflows to single-column on mobile, so we
     restore scroll on the outer body and let inner panes flow.
     ─────────────────────────────────────────────────────── */

  /* Setup-flow modal: desktop relies on .setup-flow-main scrolling
     inside a fixed-height layout. On mobile the layout reflows to
     a single column, so the inner overflow hides content. Move
     scroll up to the modal-body. */
  .modal.setup-flow-modal .modal-body {
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch;
  }
  .setup-flow-layout {
    height: auto !important;
    min-height: 0 !important;
  }
  .setup-flow-sidebar {
    overflow: visible !important;
    max-height: none !important;
  }
  .setup-flow-main {
    overflow-y: visible !important;
    height: auto !important;
    padding-right: 0 !important;
  }

  /* Plan drawer (left-panel.setup-options-mode): inner content
     can exceed the panel height — make .sc-panel-body / .sc-panel-content
     the scroll container. */
  #left-panel .sc-panel-body {
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch;
    flex: 1 1 auto;
    min-height: 0;
  }
  #left-panel .sc-panel-content {
    overflow-y: visible;
    height: auto;
  }

  /* ─────────────────────────────────────────────────────────
     Hauling HUD mode (body.is-hud)
     Triggered while the user is actively running a route. Hides the
     top brand bar, mutes map ornaments, and expands the route chip
     into a large thumb-friendly progress card pinned to the top.
     The bottom tab bar stays so the user can still access Map / Plan /
     Route / Menu, but its labels collapse to icons-only for compactness.
     ─────────────────────────────────────────────────────── */
  body.is-hud .topbar { display: none !important; }
  body.is-hud .app    { padding-top: env(safe-area-inset-top, 0) !important; }
  body.is-hud .sc-map-icons,
  body.is-hud .map-legend { opacity: 0.35; transition: opacity var(--t-mid); }
  body.is-hud .sc-map-icons:hover,
  body.is-hud .sc-map-icons:focus-within { opacity: 1; }

  body.is-hud #mobile-tabbar .mobile-tab-label { display: none; }
  body.is-hud #mobile-tabbar { height: calc(48px + env(safe-area-inset-bottom, 0)); }

  /* Bigger route chip in HUD mode — pinned top-center, prominent */
  body.is-hud #mobile-route-chip {
    top: calc(env(safe-area-inset-top, 0) + 10px) !important;
    left: 10px !important;
    right: 10px !important;
    transform: none !important;
    width: auto !important;
    max-width: none !important;
    padding: 14px 18px !important;
    border-radius: 14px !important;
    background: var(--accent) !important;
    color: var(--bg-0) !important;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  }
  body.is-hud #mobile-route-chip .mobile-route-chip-title { font-size: 16px !important; font-weight: 700 !important; }
  body.is-hud #mobile-route-chip .mobile-route-chip-meta  { font-size: 12px !important; opacity: 0.8; }
  body.is-hud #mobile-route-chip .mobile-route-chip-arrow { color: currentColor; }

  /* Subtle "exit HUD" affordance — long-press the chip to leave */
  body.is-hud #mobile-route-chip::after {
    content: 'long-press to exit HUD';
    position: absolute;
    bottom: -18px;
    left: 50%;
    transform: translateX(-50%);
    font: 500 10px var(--font-mono);
    color: var(--text-2);
    text-transform: uppercase;
    letter-spacing: 0.06em;
    opacity: 0.7;
    pointer-events: none;
  }

  /* Route panel: the entire active sub-panel scrolls as one on mobile.
     Desktop uses a two-scroll-container pattern (hero/metrics fixed at
     top, timeline scrolls inside #route-details-container) which clips
     the timeline to whatever space is left after the hero+metrics. On a
     phone that leaves only ~70px for the timeline. Flatten the layout
     so hero/metrics/actions/timeline all scroll together inside the
     .right-panel container. */
  #route-panel .right-panel,
  #route-panel .right-panel.active {
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch;
    min-height: 0;
  }
  #route-panel #route-details-container {
    flex: 0 0 auto !important;
    overflow: visible !important;
    min-height: 0;
  }
  #route-panel #routeDetails {
    overflow: visible !important;
    max-height: none !important;
  }

  #map-search-overlay {
    left: 10px !important;
    right: 10px !important;
    top: 10px !important;
    width: auto !important;
    max-width: none !important;
  }

  /* ─────────────────────────────────────────────────────────
     Form controls — touch-friendly, no iOS auto-zoom
     ─────────────────────────────────────────────────────── */
  input[type="text"],
  input[type="number"],
  input[type="password"],
  input[type="search"],
  input[type="email"],
  input[type="tel"],
  select,
  textarea {
    font-size: 16px !important;
    padding: 12px 13px !important;
    min-height: 46px;
    border-radius: 8px;
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    background-clip: padding-box;
  }

  select {
    background-image:
      linear-gradient(45deg, transparent 50%, var(--text-2) 50%),
      linear-gradient(-45deg, transparent 50%, var(--text-2) 50%);
    background-position:
      calc(100% - 16px) 50%,
      calc(100% - 11px) 50%;
    background-size: 5px 5px, 5px 5px;
    background-repeat: no-repeat;
    padding-right: 32px !important;
  }

  .btn {
    min-height: 46px;
    padding: 12px 16px;
    font-size: 13.5px;
  }
  .btn-sm {
    min-height: 40px;
    padding: 10px 13px;
    font-size: 12px;
  }
  .btn-icon {
    min-width: 42px;
    min-height: 42px;
    padding: 0;
  }
  .btn-full { width: 100%; }

  .btn-row {
    flex-wrap: wrap;
    gap: 6px;
  }
  .btn-row .btn {
    flex: 1 1 calc(33% - 6px);
    min-width: 0;
  }

  /* ─────────────────────────────────────────────────────────
     Setup / Missions / Scanner tab content tweaks
     ─────────────────────────────────────────────────────── */
  .fp-section { padding: 14px 14px; }

  .start-location-list,
  .location-search-results { max-height: 50vh; }

  .start-location-system-tabs {
    gap: 6px;
    flex-wrap: wrap;
  }

  .route-mode-grid {
    grid-template-columns: 1fr;
    gap: 8px;
  }
  .route-mode-card { padding: 13px; }
  .route-mode-title { font-size: 13px; }

  .ship-info {
    flex-wrap: wrap;
    gap: 4px;
  }

  .mission-list { gap: 10px; }
  .mission-card { padding: 12px; }

  .drop-zone {
    padding: 30px 14px;
    min-height: 150px;
  }
  .dz-text { font-size: 13px; }
  .preview-image {
    max-height: 220px;
    margin-top: 8px;
  }
  .scanner-log {
    font-size: 11px;
    max-height: 140px;
    overflow-y: auto;
  }

  /* ─────────────────────────────────────────────────────────
     Route panel polish
     ─────────────────────────────────────────────────────── */
  .fp-header {
    height: 46px;
    padding: 0 14px;
  }

  .right-tab-btn {
    min-height: var(--tap);
    height: auto;
    padding: 10px 14px;
    font-size: 12px;
  }

  .route-hero {
    grid-template-columns: 1fr;
    gap: 12px;
    padding: 16px 14px 12px;
  }
  .route-headline { font-size: 18px; }
  .route-brief { font-size: 12.5px; }

  .route-metrics,
  .route-advanced-grid {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 8px;
  }
  .metric-box { padding: 11px 12px; }
  .metric-label { font-size: 9.5px; }
  .metric-value { font-size: 18px; }

  .route-actions { gap: 8px; }
  .route-actions-secondary {
    flex-wrap: wrap;
    gap: 6px;
  }
  .route-actions-secondary .btn {
    flex: 1 1 calc(50% - 6px);
    min-width: 0;
  }

  .route-focus-card { padding: 13px; }

  .route-stop-summary-row { grid-template-columns: 1fr !important; }
  .route-stop-badges { justify-content: flex-start; flex-wrap: wrap; }
  .route-stop-line-item,
  .route-stop-footer { flex-direction: column; align-items: flex-start; }

  /* ─────────────────────────────────────────────────────────
     Modals — bottom sheets
     ─────────────────────────────────────────────────────── */
  .modal-overlay,
  #graph-modal,
  #entity-tree-modal {
    align-items: flex-end;
    padding: 0 !important;
  }

  .modal,
  .graph-modal,
  .entity-tree-modal,
  .setup-flow-modal,
  .search-launcher-modal {
    width: 100vw !important;
    max-width: 100vw !important;
    min-width: 0 !important;
    max-height: 92vh !important;
    max-height: calc(100dvh - 32px) !important;
    height: auto !important;
    margin-bottom: 0 !important;
    border-radius: 18px 18px 0 0 !important;
    border-left: none !important;
    border-right: none !important;
    border-bottom: none !important;
    box-shadow: 0 -12px 32px rgba(0, 0, 0, 0.45);
    animation: sc-slide-up-modal 0.26s cubic-bezier(.2, .7, .2, 1);
  }
  @keyframes sc-slide-up-modal {
    from { transform: translateY(100%); }
    to   { transform: translateY(0); }
  }

  .modal-header {
    padding: 14px 16px 12px;
    position: sticky;
    top: 0;
    background: var(--bg-1);
    z-index: 3;
    border-top-left-radius: 18px;
    border-top-right-radius: 18px;
  }
  .modal-header h3 { font-size: 15px; }

  .modal-body {
    padding: 14px 16px calc(22px + env(safe-area-inset-bottom, 0));
    max-height: none;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  .modal-footer {
    padding: 12px 16px calc(12px + env(safe-area-inset-bottom, 0));
    flex-wrap: wrap;
    gap: 8px;
  }

  .modal::before {
    content: "";
    display: block;
    width: 40px;
    height: 4px;
    border-radius: 2px;
    background: var(--line-3);
    margin: 8px auto 0;
    flex-shrink: 0;
  }

  .entity-tree-layout,
  .entity-tree-modal .modal-body {
    flex-direction: column !important;
    height: auto !important;
    display: flex !important;
  }
  .entity-tree-panel {
    border-right: none !important;
    border-bottom: 1px solid var(--line-2);
    width: 100% !important;
    max-height: 45vh;
    overflow-y: auto;
  }
  .entity-tree-details {
    width: 100% !important;
    max-height: 45vh;
    overflow-y: auto;
    border-top: 1px solid var(--line-2);
  }
  .entity-tree-toolbar {
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
  }
  .entity-tree-toolbar-actions {
    flex-wrap: wrap;
    gap: 6px;
    justify-content: flex-start;
  }
  .entity-tree-stat-grid { grid-template-columns: 1fr !important; }

  .setup-flow-main,
  .search-launcher,
  .search-launcher-form {
    grid-template-columns: 1fr !important;
  }
  .search-launcher-column-controls {
    border-right: none !important;
    border-bottom: 1px solid var(--line-2) !important;
  }
  .search-launcher-card-graph {
    min-height: 260px;
  }

  /* ─────────────────────────────────────────────────────────
     Toasts — sit above the tabbar
     ─────────────────────────────────────────────────────── */
  #toast-container {
    left: 12px !important;
    right: 12px !important;
    bottom: calc(var(--mobile-tabbar-h) + env(safe-area-inset-bottom, 0) + 16px) !important;
    top: auto !important;
    max-width: none !important;
    width: auto !important;
  }
  .toast {
    width: 100% !important;
    max-width: none !important;
  }

  /* ─────────────────────────────────────────────────────────
     Suppress hover states on touch
     ─────────────────────────────────────────────────────── */
  .btn:hover,
  .sc-map-icon-btn:hover,
  .topbar-btn:hover,
  .mobile-tab:hover,
  .mission-card:hover,
  .mobile-sheet-item:hover {
    background: inherit;
  }
}

/* ────────────────────────────────────────────────────────────
   Extra-small phones
   ──────────────────────────────────────────────────────────── */
@media (max-width: 380px) {
  :root {
    --topbar-h: 50px;
    --mobile-tabbar-h: 60px;
  }
  .topbar-brand { padding: 0 12px; gap: 8px; }
  .topbar-brand-name { font-size: 10.5px; }
  .topbar-btn { width: 38px; height: 38px; }
  .mobile-tab { font-size: 10px; }
  .mobile-tab-icon { width: 24px; height: 24px; }
  .mobile-tab-icon svg { width: 22px; height: 22px; }
  .sc-map-icon-btn { width: 38px; height: 38px; }
  #mobile-fab {
    padding: 13px 16px;
    font-size: 12.5px;
  }
  .route-metrics,
  .route-advanced-grid {
    grid-template-columns: 1fr !important;
  }
}

/* ────────────────────────────────────────────────────────────
   Landscape mobile
   ──────────────────────────────────────────────────────────── */
@media (max-width: 900px) and (max-height: 520px) and (orientation: landscape) {
  :root {
    --topbar-h: 42px;
    --mobile-tabbar-h: 50px;
  }
  .mobile-tab {
    flex-direction: row;
    gap: 8px;
    padding: 4px 8px;
    font-size: 11px;
  }
  .mobile-tab-icon { width: 20px; height: 20px; }
  .mobile-tab-icon svg { width: 18px; height: 18px; }
  #left-panel,
  #route-panel {
    top: calc(var(--topbar-h) + env(safe-area-inset-top, 0)) !important;
    bottom: calc(var(--mobile-tabbar-h) + env(safe-area-inset-bottom, 0)) !important;
  }
  #mobile-fab {
    bottom: calc(var(--mobile-tabbar-h) + env(safe-area-inset-bottom, 0) + 10px);
  }
  #mobile-route-chip {
    top: calc(var(--topbar-h) + env(safe-area-inset-top, 0) + 6px);
  }
}

/* ────────────────────────────────────────────────────────────
   Hover-capable — light hover effect on tabs + sheet rows
   ──────────────────────────────────────────────────────────── */
@media (hover: hover) and (pointer: fine) {
  .mobile-tab:hover:not(.active) {
    color: var(--text-0);
    background: var(--bg-3);
  }
  .mobile-sheet-item:hover {
    background: var(--bg-3);
  }
}

/* ────────────────────────────────────────────────────────────
   Light-theme tuning for mobile chrome
   ──────────────────────────────────────────────────────────── */
[data-theme="light"] #mobile-route-chip {
  background: var(--bg-1);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
}
[data-theme="light"] .mobile-sheet-panel {
  box-shadow: 0 -12px 40px rgba(0, 0, 0, 0.18);
}
[data-theme="light"] .mobile-sheet-item-icon {
  background: var(--bg-2);
}
[data-theme="light"] #mobile-fab {
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.18),
              0 0 0 1px color-mix(in srgb, var(--accent) 40%, transparent);
}
