/* ============================================================
   RESPONSIVE / MOBILE LAYOUT
   The reader HUD is ONE transport bar (#transportbar, css/reader.css) at every
   width — this file only re-SIZES it on small screens (bigger touch targets, the
   reading word dominating). There is no separate mobile bar or mobile sheet: the
   same controls, sized differently. The desktop UI (verified >640px) keeps its
   reader.css sizing untouched.
   ============================================================ */

@media (max-width: 640px), (orientation: landscape) and (max-height: 600px) {
  /* Applies to narrow phones (portrait) AND short landscape phones — the latter
     are wider than 640px and used to fall through to the desktop layout. */
  /* Use dynamic viewport height so the mobile URL bar / toolbar can't crop the
     reader, and pad for the notch / home indicator via safe-area insets. */
  html, body { height: 100dvh; }
  .reader.active {
    height: 100dvh;
    /* Go edge-to-edge at the TOP (fullscreen — use every pixel). The notch inset
       is applied to the topbar instead, so its background fills to the very top
       edge with no dead strip. Keep left/right insets. */
    padding: 0 env(safe-area-inset-right) 0 env(safe-area-inset-left);
  }

  /* ---- Top bar: compact, single tidy row ---- */
  .topbar {
    gap: 8px;
    padding: calc(8px + env(safe-area-inset-top)) 12px 8px;
    padding-right: calc(12px + env(safe-area-inset-right)); /* account moved into the tab bar — only the notch needs clearing */
    font-size: 12px;
  }
  .topbar .title { font-size: 14px; }
  /* The per-doc stats string is long; keep it from forcing horizontal scroll. */
  #docStats { display: none; }
  /* But KEEP the chapter / %-through / time-to-end readout on mobile (compact). */
  #chapterStats {
    display: block;
    font-size: 12px;
    color: var(--faint);
    font-variant-numeric: tabular-nums;
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .topbar .small-btn { height: 40px; padding: 0 12px; }

  /* ---- Setup screen: tighten gutters, clear the home indicator, and keep
         the URL input from collapsing to half-width on a narrow phone. ---- */
  .setup {
    margin: 0 auto;
    padding: 16px 16px calc(40px + env(safe-area-inset-bottom));
    padding-left: max(16px, env(safe-area-inset-left));
    padding-right: max(16px, env(safe-area-inset-right));
    gap: 18px;
  }
  /* Wordmark scales down on the narrowest phones, keeps 44px identity above. */
  .setup h1.logotype { font-size: clamp(34px, 11vw, 44px); }
  /* URL field takes the full first line; its buttons wrap to a second line
     instead of each grabbing ~50% and squishing the primary input. */
  .setup .row { flex-wrap: wrap; }
  .setup .row > input[type=text] { flex: 1 1 100%; }
  .setup .row > button { flex: 0 0 auto; }

  /* ---- The stage (reading word) must DOMINATE ---- */
  .stage {
    /* Big tap target = toggle play; the transport bar handles the rest. */
    min-height: 45dvh;
  }
  /* Cap the focus-word size so a long word can't overflow a 390px screen.
     The desktop --chunk-size px value is honoured up to ~13.5vw. */
  .word { font-size: min(calc(var(--chunk-size, 70) * 1px), 13.5vw); }
  /* Match desktop exactly: the lookahead preview is INLINE after the focus word
     (same line, same font size, muted) and extends off the right edge, clipped
     by .word-area's overflow — NEVER a separate, smaller line below. The focus
     word stays ORP-centred because renderWord() measures only the focus glyphs
     (pre+orp+post), so the trailing preview can run right without shoving it. */
  .word .upcoming { display: inline; }
  .upcoming-line { display: none; }
  .word {
    white-space: nowrap;
    max-width: none;
    text-align: center;
    justify-content: center;
    flex-wrap: nowrap;
    /* extra room so descenders (g/y/p) aren't clipped by word-area overflow */
    line-height: 1.3;
    padding-bottom: 0.12em;
  }
  /* A single word longer than the viewport (e.g. very long compounds) can't fit
     a 390px line at a readable size — let it break across lines so every
     character stays on-screen rather than spilling off either edge. */
  .word .pre, .word .orp, .word .post { overflow-wrap: anywhere; word-break: break-word; }
  .word-area { max-width: 100vw; overflow: hidden; }
  /* Book columns (paused) get sane mobile typography + full-width. */
  .context-above, .context { font-size: calc(var(--reading-size, 18) * 1px); line-height: 1.6; }
  .stage.paused .context-above p,
  .stage.paused .context p { padding: 0 14px; }

  /* ---- Transport bar: SAME controls, tighter rhythm so all six fit one row at
         360px without horizontal overflow. The labels on Play / More / Appearance
         collapse to icons (room is tight); the icons + WPM readout carry meaning. */
  .transportbar {
    gap: 4px;
    padding: 6px 6px calc(6px + env(safe-area-inset-bottom));
  }
  .tb-btn { height: 44px; min-width: 42px; padding: 0 6px; font-size: 13px; }
  .tb-step, .tb-hold { min-width: 40px; }
  .tb-play { min-width: 50px; height: 48px; padding: 0 8px; }
  /* Collapse the text labels to icon-only so all controls fit a 360px row. The
     icons (play/pause, ellipsis) + tooltips/aria-labels carry the meaning. */
  .tb-play > span, .tb-more > span { display: none; }
  .tb-more { padding: 0; min-width: 40px; }
  /* The unit is redundant on the tight bar (the −/＋ pill reads as speed). */
  .tb-wpm { padding: 1px; gap: 0; }
  .tb-wpm-read { padding: 0 3px; font-size: 15px; }
  .tb-wpm-unit { display: none; }
  .tb-wpm-nudge { width: 30px; height: 30px; }

  /* The Appearance / More sheets are the SUMMONED surfaces on mobile too (§2b),
     fixed full-width by their own rules; the only mobile tweak is giving the sheet
     body the touch-friendly field sizing. */
  .reader-settings-sheet .ctrl input[type=number],
  .reader-settings-sheet select,
  .ds-sheet .ctrl input[type=number],
  .ds-sheet #phraseJumpInput {
    height: 44px;
    font-size: 16px !important;
    flex: 1;
    min-width: 0;
  }
  .reader-settings-sheet .small-btn,
  .ds-sheet .small-btn {
    height: 44px; min-width: 44px; font-size: 15px; padding: 0 14px;
  }

  /* Keyboard-shortcut legend is meaningless on touch — hide it. */
  .meta-row { display: none; }

  /* Chapters panel sits above everything. */
  .chapters-panel { width: 88vw; }
}

/* Landscape phones have very little height. Compress vertical chrome and size the
   focus word to viewport HEIGHT so it never overflows; keep the bar usable. */
@media (orientation: landscape) and (max-height: 600px) {
  .topbar { padding: 4px 12px; }
  .word { font-size: min(calc(var(--chunk-size, 70) * 1px), 9vh); }
  .transportbar { padding-top: 4px; padding-bottom: calc(4px + env(safe-area-inset-bottom)); }
  .tb-btn { height: 42px; min-width: 42px; }
  .tb-play { height: 46px; min-width: 52px; }
}

/* Phones / tablets with a coarse (touch) pointer but a wider viewport still
   benefit from larger tap targets, without pulling in the narrow-screen relayout. */
@media (pointer: coarse) and (min-width: 641px) {
  .reader-settings-sheet .small-btn,
  .ds-sheet .small-btn { height: 40px; }
  .reader-settings-sheet .ctrl input[type=number],
  .ds-sheet .ctrl input[type=number] { font-size: 16px; }
}
