/* Mobile overrides for m.quake.run.
   Loaded after every desktop stylesheet, so these rules win. The page is the
   desktop page; this only adjusts what a phone needs. */

/* ---------- hamburger ---------- */

.m-nav-btn {
  flex: none;
  width: 40px;
  height: 40px;
  display: none; /* shown at phone widths only, below */
  place-items: center;
  font-size: 19px;
  line-height: 1;
  color: var(--ink);
  background: var(--bg-raised);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  cursor: pointer;
}
.m-nav-btn:active { background: var(--bg-sunken); }
.m-nav-btn:focus-visible { outline: 2px solid var(--magma-strong); outline-offset: 2px; }

/* ---------- drawer ---------- */

.m-drawer {
  position: fixed;
  inset: 0;
  /* Above Leaflet (panes/controls reach 1000) and the fixed bottom nav. */
  z-index: 3000;
  display: flex;
  justify-content: flex-end;
  background: #04080cbb;
}
.m-drawer[hidden] { display: none; }

.m-drawer-panel {
  width: min(86vw, 340px);
  height: 100%;
  display: flex;
  flex-direction: column;
  background: var(--bg-raised);
  border-left: 1px solid var(--line-strong);
  box-shadow: -12px 0 40px #0006;
  animation: m-slide 0.18s ease-out;
}
@keyframes m-slide { from { transform: translateX(8%); } }

.m-drawer-head {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: calc(12px + env(safe-area-inset-top)) 14px 12px;
  border-bottom: 1px solid var(--line);
  font-size: 14px;
  font-weight: 500;
  color: var(--ink);
}
.m-drawer-head .m-nav-btn { display: grid; }

.m-drawer-body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 14px 14px calc(18px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.m-drawer-group { display: flex; flex-direction: column; gap: 7px; }

.m-drawer-label {
  font-size: 10.5px;
  font-weight: 600;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  color: var(--ink-faint);
}

/* The moved controls arrive with desktop sizing; make them full width and
   touch-sized inside the drawer. */
.m-drawer-body label,
.m-drawer-body .layer-menu { width: 100%; }

.m-drawer-body select,
.m-drawer-body .terms-button {
  width: 100%;
  min-height: 44px;
  font-size: 14px;
}

.m-drawer-body .field-inline > span { display: block; }

.m-drawer-body .layer-menu summary { display: none; } /* always expanded here */
.m-drawer-body .layer-options {
  position: static;
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: 100%;
  padding: 0;
  border: 0;
  background: none;
  box-shadow: none;
}
.m-drawer-body .range-select { width: 100%; display: flex; flex-wrap: wrap; }
.m-drawer-body .range-select button { flex: 1 0 auto; min-height: 40px; }

body.m-drawer-open { overflow: hidden; }

/* ---------- phone layout ---------- */

@media (max-width: 760px) {
  .m-nav-btn { display: grid; }

  /* These now live in the drawer; hide the originals' slots in the bar. */
  .filter-bar > label.field-inline,
  .filter-bar > .layer-menu { display: none; }

  /* Terms moves into the drawer, so the masthead keeps only status + theme. */
  .masthead-right > .terms-button { display: none; }

  /* The time-range buttons stay on the map screen — they are the control
     people reach for most, and they fit. */
  .filter-bar {
    flex-wrap: wrap;
    gap: 10px;
    padding: 10px 12px;
  }
  .filter-bar .range-select { width: 100%; display: flex; }
  .filter-bar .range-select button { flex: 1; min-height: 40px; }
  .summary-bar { width: 100%; gap: 14px; font-size: 11.5px; }

  /* Map gets the space the removed overlay buttons freed up. */
  .map-frame { min-height: 52vh; }
}

/* "Fit region" and "Show activity": hidden rather than removed from the HTML,
   because map-view.js binds click handlers to both without null checks and
   would throw during init if they were absent. The activity panel itself stays
   reachable — it is simply always open on mobile. */
#mapReset,
#showActivity { display: none !important; }

/* The moved controls carry their own inline label, which duplicates the
   drawer's group heading. Keep the heading and drop the inline one. */
.m-drawer-body .field-inline > span { display: none; }

/* ---------- 3D Depth: controls behind a button ---------- */

/* The desktop's own phone rule stacks the whole sidebar above a 420px stage,
   which leaves the 3D scene as a sliver at the bottom. On a phone the scene is
   the view; the controls belong behind a button. */
@media (max-width: 760px) {
  #panel-depth3d .d3-layout {
    display: block;
    position: relative;
    height: 100%;
    min-height: 0;
    overflow: hidden;
  }

  /* Sidebar becomes a sheet, slid off-screen until opened. */
  #panel-depth3d .d3-sidebar {
    position: absolute;
    inset: 0 0 0 auto;
    z-index: 1200;
    width: min(86vw, 340px);
    display: flex;
    flex-direction: column;
    gap: 12px;
    overflow-y: auto;
    padding: 14px 14px calc(18px + env(safe-area-inset-bottom));
    background: var(--bg-raised);
    border-left: 1px solid var(--line-strong);
    box-shadow: -12px 0 40px #0006;
    transform: translateX(101%);
    transition: transform 0.18s ease-out;
    visibility: hidden;
  }
  #panel-depth3d.d3-open .d3-sidebar {
    transform: none;
    visibility: visible;
  }

  /* Restore what the desktop phone rule hides, now that there is room. */
  #panel-depth3d .d3-sidebar h2 { display: block; }
  #panel-depth3d .d3-sidebar .d3-list-title,
  #panel-depth3d .d3-sidebar .d3-list { display: block; }
  #panel-depth3d .d3-sidebar .d3-list { max-height: 34vh; overflow-y: auto; }

  #panel-depth3d .d3-field select,
  #panel-depth3d .d3-buttons button { min-height: 42px; }
  #panel-depth3d .d3-buttons { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }

  /* Stage fills the panel and centres the scene. */
  #panel-depth3d .d3-stage {
    position: absolute;
    inset: 0;
    height: auto;
    min-height: 0;
    flex: initial;
  }
  #panel-depth3d #depth3dCanvas { width: 100%; height: 100%; }

  /* depth3d-view.js calls renderer.setSize(w, h, false) — the `false` tells
     three.js NOT to write CSS width/height, so the canvas falls back to its
     pixel attributes (w*dpr) and renders at 2x the container on a retina
     phone, pushing three-quarters of the scene off-screen. Pin it to the
     holder so the drawing buffer stays hi-dpi but the element fits. */
  #panel-depth3d #depth3dCanvas > canvas {
    width: 100% !important;
    height: 100% !important;
    display: block;
  }

  /* Scrim behind the open sheet. */
  #panel-depth3d .d3-scrim {
    position: absolute;
    inset: 0;
    z-index: 1100;
    background: #04080cbb;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.18s;
  }
  #panel-depth3d.d3-open .d3-scrim { opacity: 1; pointer-events: auto; }

  /* The button that opens it. */
  .d3-panel-btn {
    position: absolute;
    top: 10px;
    right: 10px;
    z-index: 1000;
    min-height: 38px;
    padding: 8px 13px;
    display: inline-flex;
    align-items: center;
    gap: 7px;
    font: 500 12px var(--font-ui);
    color: var(--ink);
    background: color-mix(in srgb, var(--bg-raised) 94%, transparent);
    border: 1px solid var(--line-strong);
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow);
    cursor: pointer;
  }
  #panel-depth3d.d3-open .d3-panel-btn { display: none; }

  /* Close button inside the sheet. */
  .d3-sheet-close {
    align-self: flex-end;
    width: 38px;
    height: 38px;
    display: grid;
    place-items: center;
    font-size: 17px;
    color: var(--ink);
    background: var(--bg);
    border: 1px solid var(--line);
    border-radius: var(--radius-sm);
    cursor: pointer;
  }

  /* Overlay text that assumed a wide stage. */
  #panel-depth3d .globe-hint {
    font-size: 9px;
    line-height: 1.45;
    left: 10px;
    right: 10px;
    bottom: 10px;
  }
  #panel-depth3d #d3Legend { bottom: 52px; }
  #panel-depth3d .d3-scale { top: 58px; left: 10px; right: 10px; font-size: 9px; }
  #panel-depth3d .d3-detail { top: 58px; }

  /* The hint block is two lines on a narrow screen and eats the bottom of the
     stage; keep it to one compact line so the scene has the full height. */
  #panel-depth3d .globe-hint {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    padding: 5px 9px;
  }
}

/* ---------- sound alerts dialog ---------- */

.m-alerts {
  position: fixed;
  inset: 0;
  z-index: 3100;
  display: flex;
  background: #04080cd0;
}
.m-alerts[hidden] { display: none; }

.m-alerts-panel {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  background: var(--bg-raised);
}

.m-alerts-head {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: calc(14px + env(safe-area-inset-top)) 16px 13px;
  border-bottom: 1px solid var(--line);
}
.m-alerts-head h2 { margin: 0; font-size: 19px; font-weight: 600; color: var(--ink); }
.m-alerts-head h2 span { color: var(--magma); }
.m-alerts-head .m-nav-btn { display: grid; }

.m-alerts-body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 6px 16px calc(22px + env(safe-area-inset-bottom));
}

.m-arow {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 0;
  border-bottom: 1px solid var(--line);
}
.m-arow-main { flex: 1; min-width: 0; }
.m-arow-title { font-size: 14px; color: var(--ink); }
.m-arow-sub { font-size: 11.5px; line-height: 1.5; color: var(--ink-faint); }

.m-ah {
  margin: 20px 0 3px;
  font: 600 11px var(--font-ui);
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--ink-soft);
}
.m-asub { margin: 0 0 4px; font-size: 11.5px; line-height: 1.5; color: var(--ink-faint); }

.m-ahint {
  margin: 16px 0 0;
  font-size: 11.5px;
  line-height: 1.6;
  color: var(--ink-faint);
}
.m-ahint strong { color: var(--ink-soft); }

/* Toggle switch: the checkbox stays for keyboard and screen-reader use, the
   span is the visible control. */
.m-sw { flex: none; position: relative; display: inline-block; }
.m-sw input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
}
.m-sw span {
  display: block;
  width: 48px;
  height: 29px;
  border-radius: 999px;
  background: var(--bg-sunken);
  border: 1px solid var(--line-strong);
  transition: background 0.15s, border-color 0.15s;
}
.m-sw span::after {
  content: '';
  display: block;
  width: 23px;
  height: 23px;
  margin: 2px;
  border-radius: 50%;
  background: var(--ink-soft);
  transition: transform 0.15s, background 0.15s;
}
.m-sw input:checked + span {
  background: color-mix(in srgb, var(--magma) 30%, var(--bg-sunken));
  border-color: var(--magma);
}
.m-sw input:checked + span::after {
  transform: translateX(19px);
  background: var(--magma);
}
.m-sw input:focus-visible + span { outline: 2px solid var(--magma-strong); outline-offset: 2px; }

.m-sel {
  flex: none;
  max-width: 42%;
  min-height: 40px;
  padding: 7px 9px;
  font-size: 13px;
  color: var(--ink);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
}

.m-play {
  flex: none;
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  font-size: 12px;
  color: var(--magma);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  cursor: pointer;
}
.m-play:active { background: var(--bg-sunken); }

.m-install {
  flex: none;
  min-height: 40px;
  padding: 9px 18px;
  font: 600 13px var(--font-ui);
  color: #062019;
  background: var(--magma);
  border: 0;
  border-radius: var(--radius-sm);
  cursor: pointer;
}

/* An armed bell is tinted so the state reads from the header. */
#mBell.is-on { color: var(--magma); border-color: var(--magma); }

/* ---------- bottom nav: 7 tabs over two rows ---------- */

/* The desktop's phone rule sets a single-row 58px height and a 4-column grid.
   With 7 tabs that wraps to two rows which overflow the bar — the second row
   sat off-screen and its tabs could not be tapped at all. The `gap:4px` from
   the base rule also pushed the grid wider than the viewport. */
@media (max-width: 760px) {
  .view-nav {
    /* Anchored to the VISUAL viewport: on mobile Safari/Chrome innerHeight
       includes the collapsible browser chrome, so a bar positioned against it
       renders below the visible area and its second row cannot be tapped.
       100dvh tracks the visible height instead. */
    top: auto;
    bottom: 0;
    height: calc(92px + env(safe-area-inset-bottom));
    max-height: calc(100dvh - 120px);
    grid-template-columns: repeat(4, 1fr);
    grid-auto-rows: minmax(0, 1fr);
    gap: 0;
    padding: 0 4px env(safe-area-inset-bottom);
    box-sizing: border-box;
    overflow: hidden;
  }

  .view-nav .view-tab {
    min-width: 0;
    padding: 0 4px;
    font-size: 11px;
    line-height: 1.25;
    white-space: normal;
    overflow-wrap: anywhere;
  }

  /* The shell reserves space for the bar; it must match the taller bar or the
     last rows of content hide behind it. */
  .shell { padding-bottom: calc(92px + env(safe-area-inset-bottom)); }
}
