/* Mobile-only chrome: bottom-sheet handle + gear button / popover menu.
   The base rules declare `display: none` so the controls stay invisible on
   desktop; the @media block at the bottom of this file flips them visible
   and also re-lays out the sidebar as a bottom sheet. */

/* ── Bottom-sheet handle (mobile only) ──
   Lives inside #sidebar. Hidden on desktop. The wrapper is 44px tall for
   a comfortable tap target; the visible grab pill is 36×4px centred. */
#sheet-handle {
  display: none;
  width: 100%;
  min-height: 44px; /* comfortable tap target */
  padding: 12px 0 10px;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  gap: 2px;
  cursor: grab;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  background: transparent;
  border-bottom: 1px solid rgba(255,255,255,0.05);
}
#sheet-handle:active { cursor: grabbing; background: rgba(255,255,255,0.04); }
#sheet-handle .sheet-grab {
  width: 36px;
  height: 4px;
  border-radius: 2px;
  background: rgba(255,255,255,0.35);
}
#sheet-handle .sheet-chevron {
  display: none; /* reserved for future state indicator */
  font-size: 10px;
  color: rgba(255,255,255,0.4);
  transition: transform 180ms ease-out;
}
#sidebar.expanded #sheet-handle .sheet-chevron { transform: rotate(180deg); }

/* ── Gear button + menu (mobile only) ── */
#gear-btn {
  display: none;
  position: fixed;
  top: calc(12px + env(safe-area-inset-top));
  right: calc(12px + env(safe-area-inset-right));
  width: 44px;
  height: 44px;
  align-items: center;
  justify-content: center;
  background: rgba(15, 17, 23, 0.85);
  border: 1px solid rgba(255,255,255,0.1);
  backdrop-filter: blur(8px);
  border-radius: 50%;
  color: #e2e8f0;
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
  z-index: 1002;
  font-family: inherit;
  touch-action: manipulation;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
}
#gear-btn:hover,
#gear-btn:active { background: rgba(255,255,255,0.18); }
/* Highlight the button while the menu is open so the user knows where the
   popover is anchored. aria-expanded is maintained by the click handler. */
#gear-btn[aria-expanded="true"] {
  background: rgba(96,165,250,0.15);
  border-color: rgba(96,165,250,0.5);
  color: var(--color-api);
}
#gear-menu {
  position: fixed;
  top: calc(64px + env(safe-area-inset-top));
  right: calc(12px + env(safe-area-inset-right));
  width: 240px;
  max-width: calc(100vw - 24px);
  background: rgba(15, 17, 23, 0.95);
  border: 1px solid rgba(255,255,255,0.1);
  backdrop-filter: blur(8px);
  border-radius: 8px;
  padding: 8px;
  z-index: 1002;
  opacity: 0;
  pointer-events: none;
  transform: translateY(-4px);
  transition: opacity 160ms ease-out, transform 160ms ease-out;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
#gear-menu.open {
  opacity: 1;
  pointer-events: auto;
  transform: translateY(0);
}
#gear-menu .legend,
#gear-menu #trail-mode,
#gear-menu #fade-toggle,
#gear-menu #unit-selector {
  width: 100%;
  min-height: 44px; /* touch-target standard */
  padding: 8px 10px;
  font-size: 13px;
}
#gear-menu .legend {
  display: flex;
  align-items: center;
  gap: 8px;
  color: rgba(255,255,255,0.7);
}

/* ── Mobile overrides ──
   Pure CSS, applied the moment the viewport matches — no JS needed. JS
   only toggles .expanded / .dragging to drive state transitions. */
@media (max-width: 768px) {
  /* Status bar sits between Leaflet's top-left zoom stack (≈ 40px wide
     at 10px margin) and the top-right gear button (44px wide at 12px
     margin). Left/right trim leaves a 12px gutter on each side.
     Top offset respects the iOS notch via env(safe-area-inset-top). */
  #status-bar {
    top: calc(12px + env(safe-area-inset-top));
    left: calc(64px + env(safe-area-inset-left));
    right: calc(68px + env(safe-area-inset-right));
    max-width: none;
    margin: 0;
    padding: 6px 10px;
    gap: 8px;
    font-size: 12px;
    justify-content: center;
    overflow: hidden;
    white-space: nowrap;
  }
  #aircraft-count { overflow: hidden; text-overflow: ellipsis; }

  /* Hide the inline status-bar controls until JS re-parents them into
     #gear-menu. Once they're inside #gear-menu these selectors no longer
     match, so they become visible there. Prevents a layout flash on load. */
  #status-bar .legend,
  #status-bar #trail-mode,
  #status-bar #fade-toggle,
  #status-bar #unit-selector { display: none; }

  #gear-btn { display: flex; }
  #sheet-handle { display: flex; }

  /* Sidebar becomes a bottom sheet. Peek default = 140px + safe-area inset;
     this shows the handle, header and the first 1-2 rows so users can see
     there's a list to pull up. .expanded goes to ~75vh. padding-bottom
     keeps list content clear of the iOS home indicator.
     .dragging disables the transition for live finger-tracking. */
  #sidebar {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    width: auto;
    height: calc(140px + env(safe-area-inset-bottom));
    padding-bottom: env(safe-area-inset-bottom);
    border-left: none;
    border-top: 1px solid rgba(255,255,255,0.1);
    border-radius: 14px 14px 0 0;
    box-shadow: 0 -8px 24px rgba(0,0,0,0.35);
    z-index: 1000;
    transition: height 220ms ease-out, transform 220ms ease-out;
  }
  #sidebar.expanded { height: 75vh; }
  #sidebar.dragging { transition: none; }

  /* Hide permanent callsign tooltips — they pile up on a small viewport.
     Ring and POI labels remain visible. Aircraft details are still reachable
     via tap → popup. */
  .ac-label { display: none !important; }

  /* Ensure popups never exceed the viewport width. */
  .leaflet-popup-content-wrapper { max-width: calc(100vw - 40px) !important; }
}
