/* ═══════════════════════════════════════════════════════════════════════════
   Responsive — phone/tablet breakpoints and safe-area handling.
   Load LAST so these override layout.css and components.css.
   ═══════════════════════════════════════════════════════════════════════════ */

@media (max-width: 768px) {
  :root { --sidebar-w: min(100vw, 380px); }
  html, body { font-size: 13px; }

  #sidebar {
    position: absolute; top: 0; left: 0; height: 100%; height: 100dvh;
    z-index: 900; box-shadow: 0 0 60px rgba(0, 0, 0, 0.6);
    transform: translateX(0); overflow: visible;
    transition: transform .3s ease, width .3s ease, min-width .3s ease;
  }
  #sidebar.collapsed {
    width: var(--sidebar-w); min-width: var(--sidebar-w); transform: translateX(-100%);
  }
  #sidebar-scroll {
    padding: 0 18px calc(120px + env(safe-area-inset-bottom));
    -webkit-overflow-scrolling: touch;
  }
  #logo-area { padding-top: calc(28px + env(safe-area-inset-top)); }

  /* Thumb-friendly controls */
  .chk-row input[type=checkbox] { width: 20px; height: 20px; }
  .chk-row label { font-size: 12px; }
  .section-title { font-size: 11px; }
  .section-title svg { width: 13px; height: 13px; }
  input[type=range] { height: 6px; padding: 13px 0; background-clip: content-box; }
  input[type=range]::-webkit-slider-thumb { width: 22px; height: 22px; }
  input[type=range]::-moz-range-thumb { width: 22px; height: 22px; }
  select { font-size: 12px; padding: 10px 12px; min-height: 44px; }
  .snd-row input, .snd-row select { font-size: 14px; padding: 10px 12px; min-height: 44px; }
  .snd-btn, #afd-btn, .icon-btn { padding: 10px 14px; font-size: 11px; min-height: 40px; }
  .pct { font-size: 11px; }
  .layer-grid { grid-template-columns: 1fr; }
  #search-input { font-size: 16px; min-height: 44px; }  /* 16px stops iOS zoom-on-focus */
  .search-item { padding: 11px 12px; font-size: 12px; }
  .alert-item { padding: 9px 10px; }
  .alert-item .ai-event { font-size: 12px; }
  #theme-toggle { width: 40px; height: 40px; top: calc(10px + env(safe-area-inset-top)); }
  #theme-toggle svg { width: 18px; height: 18px; }
  #loop-play { width: 40px; height: 40px; }

  /* Toggle button — bigger tap target. While the drawer is OPEN this tab is
     hidden: on a phone the drawer is closed by tapping the map, so the tab
     would be a second control for the same thing, sitting underneath it. */
  #toggle-btn { width: 30px; height: 64px; font-size: 18px; }
  #toggle-btn[aria-expanded="true"] { display: none; }

  /* The map overlays are not desktop-only any more. They used to be hidden
     outright on a phone — the cameras, the stream, the SPC key and the dBZ
     scale — which left sidebar switches that silently did nothing and, once
     the scale's own checkbox was replaced by a fold, no way to read the
     reflectivity or stain colours on a phone at all.

     Everything is reachable now. What changes on a small screen is the SIZE
     and the resting state, not whether the feature exists: the camera column
     is already folded by default, and the scale folds to one line. */

  /* The camera column: as wide as the screen allows, leaving room for its own
     tab, and clear of the alert bar above it. */
  #right-stack {
    /* Clear of its own tab, not under it. On the desktop the tab overlaps the
       column's edge harmlessly because the column is wide and the overlap
       lands on its border; at this width it would sit on a camera frame. */
    right: calc(32px + env(safe-area-inset-right));
    width: min(300px, calc(100vw - 44px));
    top: calc(64px + env(safe-area-inset-top));
    max-height: calc(100% - 140px - env(safe-area-inset-top));
  }
  #right-collapse {
    top: calc(64px + env(safe-area-inset-top));
    width: 24px; min-height: 104px;          /* thumb target */
  }
  #right-collapse .rc-label { font-size: 9px; }

  /* The tools tab, the same treatment as its twin above it. */
  #zoom-tools-collapse { width: 24px; min-height: 104px; }

  /* The dBZ + stain scale: full width at the bottom rather than a centred
     card sized against a sidebar that is not there. Both widths, because the
     wide variant's desktop rule subtracts 300px it does not have here. */
  #radar-legend,
  #radar-legend.rl-wide {
    left: calc(12px + env(safe-area-inset-left));
    right: calc(12px + env(safe-area-inset-right));
    width: auto; min-width: 0; max-width: none;
    transform: none;
    bottom: calc(14px + env(safe-area-inset-bottom));
  }
  #radar-legend-collapse { width: 24px; height: 24px; font-size: 12px; }
  /* Docked across the whole bottom edge on a phone — a floating strip with a
     gutter either side reads as a card that failed to close. */
  #radar-legend.collapsed {
    left: 0; right: 0; bottom: 0;
    border-radius: 10px 10px 0 0; border-bottom: 0;
    padding: 9px 38px calc(9px + env(safe-area-inset-bottom)) 14px;
  }

  /* Everything else along the bottom edge stacks above the scale, measured
     rather than guessed — --legend-h is published by js/ui/radarlegend.js and
     follows the panel through nothing / one scale / two / folded. */
  #zoom-tools,
  #zoom-tools-collapse {
    bottom: calc(var(--legend-h, 0px) + 26px + env(safe-area-inset-bottom));
  }
  #spc-legend { bottom: calc(var(--legend-h, 0px) + 28px + env(safe-area-inset-bottom)); }

  /* Leaflet's attribution sits in the bottom-right corner, which on a phone is
     underneath a full-width scale. It is a licensing line, not decoration, so
     it rides above the scale too. */
  .leaflet-bottom.leaflet-right { bottom: calc(var(--legend-h, 0px) + 2px); }
  #spc-legend-close { width: 26px; height: 26px; font-size: 13px; }

  /* The access gate. 16px is not a style choice: below it iOS zooms the page
     when the field takes focus, and the zoom does not come back. */
  .wp-gate-field { font-size: 16px; min-height: 44px; }
  .wp-gate-btn { min-height: 46px; font-size: 13px; }
  .wp-gate-box { padding: 26px 22px; }

  /* Compact time panel */
  #time-panel {
    width: auto; min-width: 140px; max-width: 46vw; padding: 10px 12px; transform: scale(1);
    right: calc(10px + env(safe-area-inset-right));
    bottom: calc(60px + env(safe-area-inset-bottom));
  }
  .tval  { font-size: 20px; }
  .tval2 { font-size: 14px; }
  #tdiv  { margin: 6px 0; }

  /* Alert bar: tighter, horizontally scrollable */
  #alert-inner {
    padding-left: calc(12px + env(safe-area-inset-left));
    padding-right: calc(12px + env(safe-area-inset-right));
    overflow-x: auto; -webkit-overflow-scrolling: touch; justify-content: flex-start;
  }
  #alert-inner::-webkit-scrollbar { display: none; }
  .abadge { padding: 0 12px; flex-shrink: 0; }

  /* Bigger tap targets for the zoom tools on a phone. */
  #zoom-tools button { width: 40px; height: 36px; font-size: 18px; }
  #zoom-tools .zoom-view { height: 30px; font-size: 10px; }

  /* The header clocks would blow out the alert bar on a phone; the alert
     counts matter more in that space. */
  .hdr-clock { display: none; }
  #cams-collapse, #yt-collapse { min-height: 40px; }

  /* Sounding modal: stack vertically */
  #snd-body { flex-direction: column; }
  #snd-right { width: 100%; border-left: none; border-top: 1px solid var(--accent-line); max-height: 40%; }
  #snd-canvas-wrap { flex: 1 1 60%; }
  #snd-header { padding: calc(8px + env(safe-area-inset-top)) 16px 8px; }
  #snd-close { width: 40px; height: 40px; font-size: 18px; }
  #snd-title { font-size: 14px; }

  /* Shortcuts overlay is keyboard-oriented — hide the trigger's usefulness note */
  #shortcuts-card { padding: 16px 18px; }
  .sc-grid { grid-template-columns: auto 1fr; gap: 6px 10px; }

  /* Leaflet popups slightly larger */
  .leaflet-popup-content { font-size: 13px !important; }
  .ptitle { font-size: 14px; }
  .prow { font-size: 12px; }

  /* Backdrop tap layer when the sidebar is open */
  #sidebar:not(.collapsed) ~ #main::before {
    content: ""; position: absolute; inset: 0;
    background: var(--scrim); z-index: 850; pointer-events: auto;
  }
}

@media (max-width: 768px) and (orientation: landscape) {
  #time-panel { display: none; }
  #logo-area { padding: 14px 20px 10px; }
  .dsoc-big-letter, .dsoc-radar-o, .dsoc-radar-o svg { font-size: 48px; width: 48px; height: 48px; }
}
