/* mobile-map.css — phone layout, part 1: "map first" (phones, and portrait
   touch tablets: (max-width: 640px), (pointer: coarse) and (orientation:
   portrait) and (max-width: 1100px); the same list is in mobile-sheet.css,
   mobile-touch.css and the mobile-*.js files). Nothing here applies
   outside it, and
   the rules that hide desktop controls also need html.mmap-on (set by the
   script), so the desktop controls stay usable if the script fails. */

@media (max-width: 640px), (pointer: coarse) and (orientation: portrait) and (max-width: 1100px), (pointer: coarse) and (orientation: landscape) and (max-height: 500px), (max-width: 950px) and (orientation: landscape) and (max-height: 500px) {
  :root {
    --mmap-top: calc(52px + env(safe-area-inset-top, 0px));   /* top bar */
    --mmap-glass: linear-gradient(color-mix(in srgb, var(--glass) 66%, transparent), color-mix(in srgb, var(--glass) 66%, transparent)), var(--glass-floor);
    --mmap-solid: color-mix(in srgb, var(--surface2) 97%, transparent);
  }
  #app-root { position: relative; }

  /* ── Compact top bar ─────────────────────────────────────── */
  #topbar {
    height: var(--mmap-top); padding: env(safe-area-inset-top, 0px) 12px 0 16px; gap: 10px;
    padding-left: max(16px, env(safe-area-inset-left, 0px)); padding-right: max(12px, env(safe-area-inset-right, 0px));
  }
  .tb-mid { display: none; }
  /* brand: Andromeida logo (16px; dark theme turns it white, styles.css),
     a thin upright rule, then "Coastline" over the region name */
  .mmap-on .tb-brand .tb-app { display: none; }
  .mmap-on .tb-brand { gap: 10px; }
  .mmap-on .tb-brand .tb-logo { height: 16px; }
  .mmap-on .tb-brand .brand-rule { display: block; width: 1px; height: 22px; background: var(--border); }
  .mmap-brand {
    display: flex; flex-direction: column; justify-content: center; gap: 1px; min-width: 0;
    color: var(--fg); white-space: nowrap;
  }
  .mmap-title {
    min-width: 0; font: 650 13.5px/1.2 var(--font); letter-spacing: -.01em;
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  }
  .mmap-title-x { display: none; }
  .mmap-region {
    min-width: 0; font: 500 11px/1.25 var(--font); letter-spacing: 0; color: var(--fg2);
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  }
  .mmap-region:empty { display: none; }
  /* portrait tablets: a 20px logo and the full title */
  @media (min-width: 641px) and (orientation: portrait) {
    .mmap-on .tb-brand { gap: 12px; }
    .mmap-on .tb-brand .tb-logo { height: 20px; }
    .mmap-on .tb-brand .brand-rule { height: 26px; }
    .mmap-title { font-size: 14.5px; }
    .mmap-title-x { display: inline; }
    .mmap-region { font-size: 11.5px; }
  }

  .mmap-round {
    flex: none; width: 44px; height: 44px; padding: 0; border-radius: 50%; cursor: pointer;
    display: grid; place-items: center; color: var(--fg);
    background: var(--mmap-glass);
    -webkit-backdrop-filter: var(--card-backdrop); backdrop-filter: var(--card-backdrop);
    border: 1px solid var(--line); box-shadow: var(--shadow);
    -webkit-tap-highlight-color: transparent;
  }
  .mmap-round svg { width: 21px; height: 21px; display: block; }
  .mmap-round:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
  .mmap-round[aria-expanded="true"] { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }
  .mmap-more { width: 44px; height: 44px; }

  /* ⋯ menu: hangs from the top bar (inside its z-index 80 layer) */
  .mmap-menu {
    position: absolute; z-index: 50; top: calc(100% - 4px); right: max(12px, env(safe-area-inset-right, 0px));
    width: min(260px, calc(100vw - 24px)); padding: 8px; border-radius: 18px;
    display: flex; flex-direction: column; gap: 2px;
    background: var(--surface2); border: 1px solid var(--line); box-shadow: var(--shadow-lg);
    transform-origin: top right; animation: mmap-pop .16s cubic-bezier(.2, .7, .2, 1);
  }
  .mmap-menu[hidden] { display: none; }
  .mmap-mi { display: flex; align-items: center; gap: 10px; min-height: 44px; padding: 0 10px; border-radius: 12px; font: 500 13.5px var(--font); color: var(--fg); }
  .mmap-mi-email { min-height: 34px; padding-top: 4px; font-size: 12px; color: var(--fg2); overflow: hidden; }
  .mmap-mi-email:has(#user-email:empty) { display: none; }
  .mmap-menu #user-email {
    display: block !important; max-width: 100% !important; font-size: 12px; color: var(--fg2);
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  }
  .mmap-mi-theme { cursor: pointer; justify-content: space-between; }
  .mmap-mi-theme:hover { background: color-mix(in srgb, var(--fg) 6%, transparent); }
  .mmap-mi-btns { display: flex; gap: 8px; padding: 6px 2px 2px; margin-top: 4px; border-top: 1px solid var(--border); }
  .mmap-menu #btn-setup, .mmap-menu #logout-btn { flex: 1; min-height: 44px; padding: 0 12px; border-radius: 12px; font-size: 13.5px; }
  .mmap-menu #logout-btn { color: var(--danger); }

  /* ── Stats: a slim strip of four under the top bar ──────── */
  #stats {
    position: absolute; z-index: 20; top: calc(var(--mmap-top) + 6px);
    left: max(12px, env(safe-area-inset-left, 0px)); right: max(12px, env(safe-area-inset-right, 0px));
    height: 44px; flex-wrap: nowrap; align-items: center; justify-content: space-around;
    border-radius: 14px; border: 1px solid var(--line); box-shadow: var(--shadow);
    background: var(--mmap-glass);
    -webkit-backdrop-filter: var(--card-backdrop); backdrop-filter: var(--card-backdrop);
    pointer-events: none;
  }
  #stats[hidden] { display: none; }
  #stats .stat { flex: 1 1 0; min-width: 0; padding: 0 4px; border: 0; text-align: center; }
  #stats .stat:not(:has(#st-seabed, #st-alt, #st-swath, #st-track)) { display: none; }
  #stats .stat-label { font: 500 9.5px var(--font); letter-spacing: .04em; overflow: hidden; text-overflow: ellipsis; }
  #stats .stat-val { font: 600 13px var(--mono); color: var(--fg); }
  #stats .stat-val.warn { color: var(--warn); }
  #stats .stat-val.danger { color: var(--danger); }

  /* ── Map layers: one round button, a labelled menu on tap ─ */
  .mmap-on #right {
    top: 58px; bottom: auto; right: max(12px, env(safe-area-inset-right, 0px)); left: auto;
    width: auto; align-items: flex-end; gap: 8px; z-index: 20;
  }
  .mmap-on #right > div:not([id]) { display: none; }          /* the flexible spacer */
  .mmap-layers-fab { width: 46px; height: 46px; }
  .mmap-on #layers.layers-bar { display: none; }
  .mmap-on body.mmap-layers-open #right { z-index: 50; }
  .mmap-on body.mmap-layers-open #layers.layers-bar {
    display: flex; flex-direction: column; align-items: stretch; align-self: flex-end;
    width: min(240px, calc(100vw - 24px)); padding: 8px; gap: 2px; border-radius: 18px;
    background: var(--surface2); border: 1px solid var(--line); box-shadow: var(--shadow-lg);
    transform-origin: top right; animation: mmap-pop .16s cubic-bezier(.2, .7, .2, 1);
  }
  .mmap-on #layers .ib-group { flex-direction: column; gap: 2px; }
  .mmap-on #layers .ib-sep { width: auto; height: 1px; margin: 4px 8px; }
  .mmap-on #layers .ib {
    display: flex; align-items: center; gap: 11px; width: 100%; height: auto; min-height: 44px;
    padding: 0 10px; border-radius: 12px; text-align: left;
    font: 500 13.5px var(--font); color: var(--fg); background: transparent; box-shadow: none;
  }
  .mmap-on #layers .ib svg { width: 19px; height: 19px; flex: none; color: var(--fg2); }
  .mmap-on #layers .ib::before { content: attr(title); order: 1; flex: 1; min-width: 0; }
  .mmap-on #layers .ib::after { content: ""; order: 2; font-weight: 700; color: var(--accent2); }
  .mmap-on #layers .ib[aria-pressed="true"],
  .mmap-on #layers .ib-toggle:has(input:checked),
  .mmap-on #layers #btn-heatlegend[aria-pressed="true"]:not(:disabled) {
    background: color-mix(in srgb, var(--accent2) 14%, transparent); color: var(--fg); box-shadow: none;
  }
  .mmap-on #layers .ib[aria-pressed="true"] svg,
  .mmap-on #layers .ib-toggle:has(input:checked) svg,
  .mmap-on #layers #btn-heatlegend[aria-pressed="true"]:not(:disabled) svg { color: var(--accent2); }
  .mmap-on #layers .ib[aria-pressed="true"]::after,
  .mmap-on #layers .ib-toggle:has(input:checked)::after,
  .mmap-on #layers #btn-heatlegend[aria-pressed="true"]:not(:disabled)::after { content: "\2713"; }
  .mmap-on #layers .ib:hover:not(:disabled) { background: color-mix(in srgb, var(--fg) 6%, transparent); color: var(--fg); }
  .mmap-on #layers .ib[aria-pressed="true"]:hover,
  .mmap-on #layers .ib-toggle:has(input:checked):hover { background: color-mix(in srgb, var(--accent2) 20%, transparent); color: var(--fg); }
  .mmap-on #layers #btn-heatlegend:disabled { opacity: .45; }

  /* ── Legend: a chip at the bottom-left, the full strip on tap ─ */
  .mmap-on #legend-dock {
    left: max(12px, env(safe-area-inset-left, 0px)) !important; right: max(12px, env(safe-area-inset-right, 0px)) !important;
    bottom: calc(var(--m-sheet-h, 64px) + 12px) !important;
    justify-content: flex-start; z-index: 20;
  }
  .mmap-legend-chip {
    pointer-events: auto; height: 44px; padding: 0 14px; border-radius: 99px; cursor: pointer;
    display: inline-flex; align-items: center; gap: 8px; font: 600 12px var(--font); color: var(--fg);
    background: var(--mmap-glass);
    -webkit-backdrop-filter: var(--card-backdrop); backdrop-filter: var(--card-backdrop);
    border: 1px solid var(--line); box-shadow: var(--shadow);
    -webkit-tap-highlight-color: transparent;
  }
  .mmap-legend-chip:focus-visible, .mmap-legend-x:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
  .mmap-mini { width: 40px; height: 7px; border-radius: 9px; flex: none; background: linear-gradient(90deg, #e2f3f8, #97d0e4, #3f95c5, #1f5791, #102b51); }
  .mmap-on #legend-dock .legend-strip { display: none; }
  .mmap-on body.mmap-legend-open #legend-dock .legend-strip {
    display: flex; position: relative; width: 100%; min-width: 0; padding: 12px 46px 12px 14px;
    background: var(--mmap-glass); animation: mmap-pop .16s cubic-bezier(.2, .7, .2, 1); transform-origin: bottom left;
  }
  .mmap-on body.mmap-legend-open .mmap-legend-chip { display: none; }
  .mmap-legend-x {
    position: absolute; top: 2px; right: 2px; width: 44px; height: 44px; border: 0; border-radius: 50%;
    background: transparent; color: var(--fg2); font: 20px/1 var(--font); cursor: pointer;
  }
  .mmap-legend-x:hover { color: var(--fg); }

  /* ── Things placed under the new top bar / strip ───────── */
  #toasts { top: calc(var(--mmap-top) + 6px); width: calc(100vw - 24px); }
  .mmap-on #sim-chip { top: 58px; right: auto; left: max(12px, env(safe-area-inset-left, 0px)); max-width: calc(100% - 90px); z-index: 19; }
  .mmap-on #sim-chip .sc-left { overflow: hidden; text-overflow: ellipsis; }
  #sim-edge { z-index: 21; }

  @keyframes mmap-pop { from { opacity: 0; transform: scale(.96); } }
}

@media (max-width: 640px) and (prefers-reduced-motion: reduce), (pointer: coarse) and (orientation: portrait) and (max-width: 1100px) and (prefers-reduced-motion: reduce), (pointer: coarse) and (orientation: landscape) and (max-height: 500px) and (prefers-reduced-motion: reduce), (max-width: 950px) and (orientation: landscape) and (max-height: 500px) and (prefers-reduced-motion: reduce) {
  .mmap-menu, .mmap-on body.mmap-layers-open #layers.layers-bar, .mmap-on body.mmap-legend-open #legend-dock .legend-strip { animation: none; }
}

/* Integration: the 3D view and the Mission summary float over the map on
   phones too. Give them the screen width under the stats strip, above the
   legend chip and below the sheet; they stay draggable by their title bar. */
@media (max-width: 640px), (pointer: coarse) and (orientation: portrait) and (max-width: 1100px), (pointer: coarse) and (orientation: landscape) and (max-height: 500px), (max-width: 950px) and (orientation: landscape) and (max-height: 500px) {
  #v3d:not(.big):not(.moved), #ms-summary:not(.moved) {
    left: 10px !important; right: 10px !important; width: auto; top: 108px; bottom: auto;
  }
  /* the 3D panel starts under the Layers button (map top 58-104); the
     crosshair button moves beside Layers while it is open (mobile-touch.css) */
  #v3d:not(.big):not(.moved) { top: 114px; }
  #v3d:not(.big), #ms-summary { z-index: 25; }
  #v3d:not(.big) { height: min(54vh, 440px); min-height: 260px; max-height: none; }
  /* the desktop rules (styles.css, min-width 641px) also reach portrait tablets */
  #v3d { min-width: 0; max-width: none; }
  #ms-summary { max-height: calc(100vh - var(--m-sheet-h, 64px) - 130px); }
  #v3d.big { left: 10px; right: 10px; top: 114px; bottom: calc(var(--m-sheet-h, 64px) + 10px); height: auto; z-index: 25; }
  /* finger-sized 3D controls */
  #v3d .head { row-gap: 6px; }
  #v3d #btn-3d-big, #v3d #btn-3d-close { min-width: 44px; min-height: 44px; padding: 0 10px; font-size: 16px; border-radius: 12px; }
  #v3d .mini input { height: 44px; min-width: 64px; font-size: 16px; border-radius: 12px; }
  #v3d .seg button { min-height: 40px; padding: 0 12px; font-size: 12.5px; }
  /* the title's ⓘ: same 16 px circle, a 44 px hit area (clip, not hide,
     so the ::after can reach outside the circle) */
  #v3d .head h3 .info { overflow: clip; overflow-clip-margin: 14px; }
  #v3d .head h3 .info::after { content: ""; position: absolute; left: 50%; top: 50%; width: 44px; height: 44px; transform: translate(-50%, -50%); border-radius: 50%; }
}
/* phone width: title with ⤢ ✕ on the first row, the settings under it */
@media (max-width: 640px) {
  #v3d .head h3 { flex: 1 1 auto; order: -2; min-width: 0; }
  #v3d .head #btn-3d-big, #v3d .head #btn-3d-close { order: -1; }
  #v3d .head .seg { flex: 1 1 100%; display: flex; }
  #v3d .head .seg button { flex: 1; }
  #v3d .mini .info { display: none; }
  #v3d .mini { flex: 1 1 0; gap: 8px; }
  #v3d .mini input { flex: 1; width: auto; min-width: 0; }
}


/* No top-edge progress bar or progress chip on phones: the Simulate/Stop
   button in the sheet fills up instead, and the "complete / stopped"
   notifications still appear when a run ends. */
@media (max-width: 640px), (pointer: coarse) and (orientation: portrait) and (max-width: 1100px), (pointer: coarse) and (orientation: landscape) and (max-height: 500px), (max-width: 950px) and (orientation: landscape) and (max-height: 500px) {
  #sim-edge, #sim-chip { display: none !important; }
}

/* ── Phone landscape (a phone on its side, at most 500 px tall) ──────
   The tab bar is a rail on the left and the sheet a side panel on the
   right (mobile-sheet.css/.js). The map starts after the rail; the four
   stats sit inside the one-row top bar; Layers, the legend and the 3D
   panel keep clear of the side panel through --m-sheet-w (0 when it is
   closed). --mland-rail / --mland-side are the CSS widths of the rail and
   the panel (mobile-sheet.js publishes the measured --m-rail-w too).
   Every rule also needs html.mland-on (zero specificity via :where()):
   mobile-sheet.js decides landscape, and keeps portrait while a field is
   typed into on an upright phone whose keyboard shrinks the page. */
@media (pointer: coarse) and (orientation: landscape) and (max-height: 500px), (max-width: 950px) and (orientation: landscape) and (max-height: 500px) {
  :root:where(.mland-on) {
    --mmap-top: calc(48px + env(safe-area-inset-top, 0px));
    --mland-rail: calc(64px + env(safe-area-inset-left, 0px));
    --mland-side: calc(clamp(300px, 40vw, 360px) + env(safe-area-inset-right, 0px));
    --mland-stats-w: clamp(240px, calc(100vw - 440px), 400px);
    --mland-ease: .25s cubic-bezier(.2, .8, .2, 1);
  }
  html:where(.mland-on).msheet-on #map-wrap { margin-left: var(--mland-rail); }

  /* top bar: brand on the left, stats in the middle, ⋯ on the right */
  .mmap-on:where(.mland-on) .tb-brand { max-width: calc(100% - var(--mland-stats-w) - 64px); }
  /* one row: small logo | Coastline  region */
  .mmap-on:where(.mland-on) .tb-brand { gap: 9px; }
  .mmap-on:where(.mland-on) .tb-brand .tb-logo { height: 14px; }
  .mmap-on:where(.mland-on) .tb-brand .brand-rule { height: 18px; }
  :where(html.mland-on) .mmap-brand { flex-direction: row; align-items: baseline; gap: 7px; }
  :where(html.mland-on) .mmap-title { flex: none; font-size: 14px; }
  :where(html.mland-on) #stats {
    z-index: 81; top: calc(env(safe-area-inset-top, 0px) + 5px); height: 38px;
    left: auto; right: calc(max(12px, env(safe-area-inset-right, 0px)) + 54px); width: var(--mland-stats-w);
    border-radius: 12px; border-color: var(--border); box-shadow: none;
    background: color-mix(in srgb, var(--fg) 4%, transparent);
    -webkit-backdrop-filter: none; backdrop-filter: none;
  }
  :where(html.mland-on) #stats .stat-label { font-size: 9px; }
  :where(html.mland-on) #stats .stat-val { font-size: 12.5px; }

  /* Layers: top-right of the free map, left of the side panel */
  .mmap-on:where(.mland-on) #right { top: 8px; right: calc(var(--m-sheet-w, 0px) + max(12px, env(safe-area-inset-right, 0px))); transition: right var(--mland-ease); }
  .mmap-on:where(.mland-on) body.mmap-layers-open #layers.layers-bar {
    max-height: calc(100vh - var(--mmap-top) - 72px); overflow-y: auto; overscroll-behavior: contain;
  }

  /* legend chip: bottom-left of the map (the rail is outside it) */
  .mmap-on:where(.mland-on) #legend-dock {
    left: 12px !important; right: calc(var(--m-sheet-w, 0px) + max(12px, env(safe-area-inset-right, 0px))) !important;
    bottom: max(12px, env(safe-area-inset-bottom, 0px)) !important;
    transition: right var(--mland-ease);
  }
  /* the expanded legend gets only the map left of the open side panel
     (~280-430 px): when that is narrow it drops the row titles, as in
     portrait (the bars get their width; the strip keeps its two short
     rows, so it stays clear of the crosshair button above it) and, when
     very narrow, the middle caption of the route heat bar; narrowest
     (667 px phones with the panel open): Land goes under the depth bar */
  .mmap-on:where(.mland-on) body.mmap-legend-open #legend-dock .legend-strip { container: mland-legend / inline-size; }
  @container mland-legend (max-width: 560px) {
    .ls-label { display: none; }
  }
  @container mland-legend (max-width: 380px) {
    .ls-heat #cbar-mid { display: none; }
  }
  @container mland-legend (max-width: 260px) {   /* the depth bar on its own line, Land under it */
    .ls-base { flex-wrap: wrap; row-gap: 6px; }
    .ls-base > .ls-depth { flex: 1 0 100%; }
  }

  /* 3D panel (not the full-screen one, mobile-views.css): the whole free
     map area, left of the side panel (which folds away when 3D opens,
     mobile-sheet.js); its controls on one compact row that scrolls
     sideways if it must, so the picture keeps ~250 px of height */
  :where(html.mland-on) #v3d:not(.big):not(.moved):not(.mviews-fs), :where(html.mland-on) #v3d.big:not(.mviews-fs) {
    left: 10px !important; right: calc(var(--m-sheet-w, 0px) + max(10px, env(safe-area-inset-right, 0px))) !important;
    top: 8px; bottom: max(8px, env(safe-area-inset-bottom, 0px)); height: auto; min-height: 0; max-height: none;
    transition: right var(--mland-ease);
  }
  :where(html.mland-on) #v3d:not(.mviews-fs) .head {
    flex: none; flex-wrap: nowrap; row-gap: 0; gap: 6px; overflow-x: auto; overflow-y: hidden;
    scrollbar-width: none; overscroll-behavior-x: contain;
  }
  :where(html.mland-on) #v3d:not(.mviews-fs) .head::-webkit-scrollbar { display: none; }
  :where(html.mland-on) #v3d:not(.mviews-fs) .head h3 { flex: none; order: 0; margin-right: auto; white-space: nowrap; font-size: 13px; }
  :where(html.mland-on) #v3d:not(.mviews-fs) .head .seg { flex: none; display: flex; }
  :where(html.mland-on) #v3d:not(.mviews-fs) .head .seg button { flex: none; min-height: 40px; padding: 0 10px; }
  :where(html.mland-on) #v3d:not(.mviews-fs) .head #btn-3d-big, :where(html.mland-on) #v3d:not(.mviews-fs) .head #btn-3d-close { order: 0; flex: none; min-width: 40px; min-height: 40px; }
  :where(html.mland-on) #v3d:not(.mviews-fs) .mini { flex: none; white-space: nowrap; gap: 6px; }
  :where(html.mland-on) #v3d:not(.mviews-fs) .mini .info { display: none; }
  :where(html.mland-on) #v3d:not(.mviews-fs) .mini input { flex: none; width: 66px; min-width: 0; height: 40px; }
}
@media (pointer: coarse) and (orientation: landscape) and (max-height: 500px) and (prefers-reduced-motion: reduce), (max-width: 950px) and (orientation: landscape) and (max-height: 500px) and (prefers-reduced-motion: reduce) {
  .mmap-on #right, .mmap-on #legend-dock, #v3d { transition: none !important; }
}
