/* Focus mode reuses the reader and clocks; only presentation changes. */
body.focus-mode {
  --focus-nav-space: 0px;
  --focus-word-center: calc((100dvh - var(--focus-nav-space)) / 2);
  overflow: hidden;
}
.focus-mode .toolbar,
.focus-mode .settings-panel { display: none; }
.focus-mode #settingsDrawer .settings-panel { display: block; }

/* Reuse the main pace inputs while paused; keep the word's viewport anchor. */
.focus-mode .controls { display: contents; }
.focus-mode .controls > :not(.pace-section),
.focus-mode.is-playing .pace-section { display: none; }
.focus-mode .pace-section {
  position: fixed;
  top: max(10px, env(safe-area-inset-top));
  left: max(16px, env(safe-area-inset-left));
  right: max(64px, calc(env(safe-area-inset-right) + 64px));
  z-index: 55;
  width: auto;
  max-width: 480px;
  height: 78px;
  margin: 0 auto;
  padding: 0;
  border: 0;
  background: var(--bg);
}
.focus-mode .pace-control {
  --pace-ruler-height: 60px;
  grid-template-columns: 120px minmax(0, 1fr);
  grid-template-rows: auto;
  height: 60px;
  gap: 8px;
}
.focus-mode .pace-value {
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 4px;
  height: 44px;
}
.focus-mode .pace-value input { width: 64px; height: 44px; font-size: 26px; }
.focus-mode .has-pace-dial .pace-presets { display: none; }

body.focus-mode #readingStage {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: block;
  width: 100%;
  height: 100%;
  height: 100dvh;
  min-height: 0;
  margin: 0;
  border-radius: 0;
  background: var(--bg);
}
.focus-mode #readingStage:focus-visible { outline-offset: -6px; }
.focus-mode #readingStage .caption {
  inset: max(64px, env(safe-area-inset-top)) max(22px, env(safe-area-inset-right))
    max(64px, env(safe-area-inset-bottom)) max(22px, env(safe-area-inset-left));
}

/* The slot collapses, but must not hide its fixed-position context child. */
.focus-mode .sentence-slot {
  height: 0;
  min-height: 0;
  flex-basis: 0;
  margin: 0;
  overflow: visible;
}
.focus-mode .sentence-panel {
  position: fixed;
  top: calc(50% + 72px);
  bottom: max(16px, env(safe-area-inset-bottom));
  left: max(16px, env(safe-area-inset-left));
  right: max(16px, env(safe-area-inset-right));
  z-index: 55;
  width: auto;
  height: auto;
  min-height: 44px;
  max-width: 640px;
  max-height: 260px;
  margin: 0 auto;
  padding: 8px 14px 0;
  border: 0;
  background: var(--bg);
}
.focus-mode .sentence-context { min-height: 0; text-align: center; }
.focus-mode.has-sentence-context #readingStage .caption {
  inset: auto max(22px, env(safe-area-inset-right)) auto max(22px, env(safe-area-inset-left));
  top: 50%;
  display: block;
  max-height: 112px;
  transform: translateY(-50%);
}

/* Critical feedback remains reachable without moving the word or Exit. */
.focus-mode .notice-slot {
  position: fixed;
  top: max(96px, calc(env(safe-area-inset-top) + 92px));
  left: max(16px, env(safe-area-inset-left));
  right: max(16px, env(safe-area-inset-right));
  z-index: 60;
  width: auto;
  max-width: 640px;
  height: 44px;
  margin: 0 auto;
  padding: 0;
  pointer-events: none;
}
.focus-mode .notice-slot .file-notice,
.focus-mode .notice-slot .high-rate-note { display: none; }
.focus-mode .notice-slot .error-banner,
.focus-mode .notice-slot .reader-message { pointer-events: auto; }
.focus-mode .notice-slot .reader-message { background: var(--bg); }

#focusExitButton {
  position: fixed;
  top: max(10px, env(safe-area-inset-top));
  right: max(10px, env(safe-area-inset-right));
  z-index: 60;
  width: 44px;
  height: 44px;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
}
#focusExitButton:hover, #focusExitButton:focus-visible {
  background: var(--paper);
  color: var(--text);
}
#focusHint {
  position: fixed;
  bottom: max(18px, env(safe-area-inset-bottom));
  left: 16px;
  right: 16px;
  z-index: 60;
  margin: 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.6;
  text-align: center;
  pointer-events: none;
}
.focus-mode:not(.is-playing) #focusHint { display: none; }

@media (max-height: 420px) {
  .focus-mode .sentence-panel {
    top: calc(50% + 57px);
    bottom: max(5px, env(safe-area-inset-bottom));
    padding-top: 0;
  }
  .focus-mode .sentence-context { font-size: 13px; line-height: 1.5; }
  .focus-mode.has-sentence-context #readingStage .caption { max-height: 90px; }
  .focus-mode .notice-slot {
    top: max(96px, calc(env(safe-area-inset-top) + 92px));
    height: 32px;
  }
  .focus-mode .notice-slot .error-banner,
  .focus-mode .notice-slot .reader-message {
    padding-block: 4px;
    font-size: 11px;
    line-height: 16px;
  }
}

/* The same navigation stays available in focus. Its measured height includes
   safe-area padding; the word is centred in the space above it in both states. */
.has-mobile-nav body.focus-mode { --focus-nav-space: var(--mobile-nav-space); }
.has-mobile-nav body.focus-mode #readingStage {
  bottom: var(--focus-nav-space);
  height: calc(100dvh - var(--focus-nav-space));
}
.has-mobile-nav .focus-mode #focusExitButton { display: none; }

/* The paused ruler and replay stay within reach without moving the word. */
@media (max-width: 600px), (max-width: 960px) and (max-height: 500px) {
  body.focus-mode {
    --focus-footer-bottom: calc(12px + env(safe-area-inset-bottom));
    --focus-footer-side: max(16px, calc((100% - 480px) / 2));
    --focus-caption-edge: 96px;
  }
  .has-mobile-nav body.focus-mode { --focus-footer-bottom: calc(var(--focus-nav-space) + 8px); }
  .focus-mode .pace-section {
    top: auto;
    bottom: calc(var(--focus-footer-bottom) + 56px);
    left: max(16px, env(safe-area-inset-left));
    right: max(16px, env(safe-area-inset-right));
    height: 122px;
  }
  .focus-mode .pace-control {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: 44px 60px;
    height: 104px;
    gap: 0;
  }
  .focus-mode .pace-value { height: 44px; }
  .focus-mode .pace-value input { width: 76px; height: 44px; font-size: 28px; }

  .focus-mode .sentence-panel {
    top: max(calc(60px + env(safe-area-inset-top)), calc(var(--focus-word-center) - 260px));
    bottom: calc(100dvh - var(--focus-word-center) + 72px);
    min-height: 0;
    max-height: none;
    grid-template-rows: minmax(0, 1fr);
    padding-top: 0;
  }
  .focus-mode .replay-sentence {
    position: fixed;
    bottom: var(--focus-footer-bottom);
    left: max(var(--focus-footer-side), env(safe-area-inset-left));
    z-index: 60;
    width: 160px;
    max-width: calc(100% - max(var(--focus-footer-side), env(safe-area-inset-left)) - max(var(--focus-footer-side), env(safe-area-inset-right)) - 112px);
    min-width: 0;
    min-height: 48px;
    height: 48px;
    padding: 8px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--bg);
    font-size: 13px;
  }
  .has-mobile-nav .focus-mode .replay-sentence {
    left: 50%;
    right: auto;
    max-width: calc(100% - 32px);
    transform: translateX(-50%);
  }
  #focusExitButton {
    top: auto;
    bottom: var(--focus-footer-bottom);
    right: max(var(--focus-footer-side), env(safe-area-inset-right));
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    width: 104px;
    height: 48px;
    padding: 8px;
    border: 1px solid var(--line);
    background: var(--bg);
    font-size: 12px;
  }
  #focusExitButton svg { width: 18px; height: 18px; }
  #focusExitButton .focus-exit-label { display: inline; white-space: nowrap; }
  #focusExitButton:active,
  .focus-mode .replay-sentence:active {
    background: var(--paper);
    color: var(--text);
  }

  .focus-mode .notice-slot,
  .focus-mode .notice-slot:has(.resume-notice:not([hidden])) {
    top: calc(8px + env(safe-area-inset-top));
    height: 48px;
  }
  .focus-mode #readingStage .caption {
    inset: var(--focus-caption-edge) max(22px, env(safe-area-inset-right))
      var(--focus-caption-edge) max(22px, env(safe-area-inset-left));
  }
  #focusHint {
    bottom: calc(var(--focus-footer-bottom) + 12px);
    left: max(16px, env(safe-area-inset-left));
    right: max(16px, env(safe-area-inset-right));
  }
}

/* A compact number/ruler row leaves room on smaller portrait phones. */
@media (max-width: 600px) and (max-height: 650px) {
  .focus-mode .pace-section { height: 78px; }
  .focus-mode .pace-control { height: 60px; }
  .focus-mode .pace-control {
    grid-template-columns: 120px minmax(0, 1fr);
    grid-template-rows: auto;
    gap: 8px;
  }
}

/* Short viewports, including a raised keyboard, use one reachable row. */
@media (max-width: 960px) and (max-height: 500px) {
  body.focus-mode {
    --focus-footer-side: 16px;
    --focus-caption-edge: 64px;
  }
  .focus-mode .pace-section {
    bottom: var(--focus-footer-bottom);
    right: calc(max(16px, env(safe-area-inset-right)) + 168px);
    height: 78px;
  }
  .focus-mode .pace-control {
    grid-template-columns: 72px minmax(0, 1fr);
    grid-template-rows: auto;
    height: 60px;
    gap: 6px;
  }
  .focus-mode .pace-value {
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 60px;
    gap: 0;
  }
  .focus-mode .pace-value input { width: 72px; height: 36px; font-size: 26px; text-align: center; }
  .focus-mode .pace-value > span { font-size: 10px; line-height: 14px; }
  .has-mobile-nav .focus-mode .replay-sentence {
    left: auto;
    right: max(16px, env(safe-area-inset-right));
    width: 156px;
    transform: none;
  }
  .focus-mode .sentence-panel {
    top: calc(56px + env(safe-area-inset-top));
    bottom: calc(100dvh - var(--focus-word-center) + 66px);
  }
}

/* Narrow short views keep a useful dial beside a two-line replay target. */
@media (max-width: 600px) and (max-height: 500px) {
  .has-mobile-nav .focus-mode .pace-section {
    right: calc(max(16px, env(safe-area-inset-right)) + 108px);
  }
  .has-mobile-nav .focus-mode .pace-control { grid-template-columns: 64px minmax(0, 1fr); }
  .has-mobile-nav .focus-mode .pace-value input { width: 64px; }
  .has-mobile-nav .focus-mode .replay-sentence {
    width: 96px; padding: 6px 8px; line-height: 16px; white-space: normal;
  }
}

@media (max-width: 960px) and (max-height: 360px) {
  .focus-mode .pace-section { height: 66px; }
  .focus-mode .pace-control,
  .focus-mode .pace-value { height: 48px; }
  .focus-mode .pace-control { --pace-ruler-height: 48px; }
  .focus-mode .pace-value input { height: 32px; }
  .focus-mode .pace-tick { top: 8px; }
  .focus-mode .pace-tick.major-tick::before { height: 20px; }
  .focus-mode .pace-tick-label { top: 24px; font-size: 10px; line-height: 12px; }
  .focus-mode .pace-indicator { top: 2px; }
  .focus-mode .sentence-panel {
    top: calc(56px + env(safe-area-inset-top));
    bottom: calc(100dvh - var(--focus-word-center) + 56px);
  }
  /* A raised keyboard can leave little context space. Reclaim the empty
     feedback slot, while real errors, messages and resume actions keep priority. */
  .focus-mode:not(:has(#errorBanner:not([hidden]), #readerMessage:not([hidden]), #resumeNotice:not([hidden]))) .sentence-panel {
    top: calc(8px + env(safe-area-inset-top));
  }
  .focus-mode .notice-slot { top: calc(8px + env(safe-area-inset-top)); }
  .focus-mode.has-sentence-context #readingStage .caption {
    max-height: 80px;
  }
}
