/* Theme tokens */
:root {
  color-scheme: light;
  --bg: #f4f3ee;
  --paper: #fffefa;
  --text: #252d2b;
  --muted: #667068;
  --line: #dce0d5;
  --accent: #a9462b;
  --soft: #f4e5da;
  --green: #536c58;
  --button: #273b32;
  --button-text: #fffefa;
  --track: #dce0d5;
  --focus-color: #8a4800;
  --stage-height: clamp(196px, 30svh, 240px);
  --sentence-height: 104px;
}
[data-theme="dark"] {
  color-scheme: dark;
  --bg: #191f1d;
  --paper: #222a26;
  --text: #eeeee4;
  --muted: #a2ac9f;
  --line: #3a453c;
  --accent: #efa886;
  --soft: #49352b;
  --green: #b0c49f;
  --button: #c9d7b9;
  --button-text: #1c281f;
  --track: #414c42;
  --focus-color: #f0b36e;
}

/* Base and shared controls */
* { box-sizing: border-box; }
html { scrollbar-gutter: stable; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; }
button { cursor: pointer; color: inherit; border: 0; }
button:disabled { cursor: default; opacity: .4; }
button, a, input, select, textarea, summary { -webkit-tap-highlight-color: transparent; }
button:focus-visible, a:focus-visible, input:focus-visible, select:focus-visible,
textarea:focus-visible, summary:focus-visible, [tabindex]:focus-visible {
  outline: 2px solid var(--focus-color);
  outline-offset: 3px;
}
a { color: var(--green); }
svg {
  width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.6;
  stroke-linecap: round; stroke-linejoin: round; flex-shrink: 0;
}
.button {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 44px; padding: 10px 14px; border-radius: 8px; font-size: 12px; font-weight: 550;
}
.open-button { background: var(--button); color: var(--button-text); }
.open-button svg { width: 17px; height: 17px; }
.secondary-button { background: transparent; border: 1px solid var(--line); }
.icon-button {
  display: grid; place-items: center; width: 44px; height: 44px; flex: 0 0 44px;
  border-radius: 8px; background: transparent; color: var(--muted);
}
.text-button {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  min-height: 44px; padding: 8px 6px; background: none; color: var(--muted);
  font-size: 12px; text-underline-offset: 4px;
}
.text-button svg { width: 15px; height: 15px; }
@media (hover: hover) {
  .icon-button:hover { background: var(--line); color: var(--text); }
  .secondary-button:hover { background: var(--paper); border-color: var(--muted); }
  .text-button:hover { text-decoration: underline; color: var(--text); }
  .file-info:hover, .toolbar-button:hover, .setting-switch:not(:disabled):hover {
    background: var(--paper); border-color: var(--muted);
  }
  .profile-editor > summary:hover, .app-extras > summary:hover { color: var(--text); }
}

/* Stable reader stack: conditional content belongs inside reserved slots. */
.app {
  width: min(100%, 800px); margin: 0 auto; position: relative;
  padding: max(12px, env(safe-area-inset-top)) max(26px, env(safe-area-inset-right))
    max(16px, env(safe-area-inset-bottom)) max(26px, env(safe-area-inset-left));
}
.reader-screen, .reader { display: flex; flex-direction: column; min-width: 0; }
.reader { position: relative; }
.toolbar {
  display: grid; grid-template-columns: minmax(0, 1fr) 112px 80px 92px;
  align-items: center; gap: 10px; height: max(64px, 5.333em); flex: 0 0 max(64px, 5.333em);
  font-size: 12px;
  border-bottom: 1px solid var(--line);
}
.file-info {
  display: flex; align-items: center; gap: 8px; min-width: 0; min-height: 44px;
  padding: 4px 8px; border: 1px solid var(--line); border-radius: 7px;
  background: none; text-align: left;
}
.file-info svg { width: 16px; height: 16px; color: var(--green); }
.file-copy { display: grid; min-width: 0; }
.transcript-action {
  color: var(--green); font-size: 11px; line-height: 15px; font-weight: 550;
  text-decoration: underline; text-underline-offset: 3px;
}
.file-info strong {
  min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  font-size: 13px; line-height: 18px; font-weight: 550;
}
.toolbar-button {
  min-width: 0; padding-inline: 6px; gap: 5px;
  border: 1px solid var(--line); background: none; color: var(--text);
}
.toolbar-button svg { width: 16px; height: 16px; }
.toolbar-button > span, #uploadLabel { min-width: 0; overflow-wrap: anywhere; }
#uploadButton { width: 112px; padding-inline: 10px; }
.notice-slot {
  position: absolute; inset: 0 0 auto; z-index: 2;
  width: min(100%, 600px); height: 44px; min-width: 0; margin: 0 auto;
  overflow-y: auto; overscroll-behavior: contain; scrollbar-width: thin;
  pointer-events: none;
}
.file-notice, .error-banner, .high-rate-note, .reader-message {
  margin: 0; padding: 4px 10px; border-radius: 6px;
  background: var(--soft); color: var(--accent);
  font-size: 12px; line-height: 18px; overflow-wrap: anywhere;
}
.notice-slot > p { pointer-events: auto; }
.reader-message { background: none; color: var(--muted); text-align: center; }
.resume-notice {
  display: grid; grid-template-columns: minmax(0, 1fr) auto auto; align-items: center; gap: 4px;
  min-height: 44px; padding-inline: 8px; border-radius: 6px;
  background: var(--bg); color: var(--muted); font-size: 12px; line-height: 18px;
  pointer-events: auto;
}
.resume-notice > span { min-width: 0; overflow-wrap: anywhere; }
.resume-notice button { min-height: 44px; padding-inline: 8px; font-size: 12px; }
.resume-notice #resumeReadingButton { color: var(--text); font-weight: 600; }
.notice-slot:has(.resume-notice:not([hidden])) .file-notice,
.notice-slot:has(.resume-notice:not([hidden])) .high-rate-note,
.notice-slot:has(.resume-notice:not([hidden])) .reader-message { display: none; }
.focus-mode .notice-slot:has(.resume-notice:not([hidden])) { height: 44px; }
.notice-slot:has(.error-banner:not([hidden])) .file-notice,
.notice-slot:has(.error-banner:not([hidden])) .high-rate-note,
.notice-slot:has(.error-banner:not([hidden])) .reader-message,
.notice-slot:has(.reader-message:not([hidden])) .file-notice,
.notice-slot:has(.reader-message:not([hidden])) .high-rate-note,
.notice-slot:has(.high-rate-note:not([hidden])) .file-notice { display: none; }
.stage {
  position: relative; display: block; width: 100%;
  height: var(--stage-height); min-height: 196px; flex: 0 0 var(--stage-height);
  overflow: hidden; border-radius: 10px; background: none; touch-action: manipulation;
}
.stage:focus-visible { outline-offset: -4px; }
.word {
  position: absolute; top: 50%; left: 50%; white-space: pre;
  font-size: 52px; font-weight: 500; line-height: 1.35; letter-spacing: 0;
  transform-origin: 0 50%; user-select: none; font-kerning: normal; font-variant-ligatures: none;
}
.anchored #wordAnchor, .accent-preview { color: var(--focus-color); }
.focus-guide {
  display: none; position: absolute; left: 50%; width: 1px; height: 11px;
  background: var(--focus-color); opacity: .55; pointer-events: none;
}
.anchored .focus-guide { display: block; }
.anchored.guides-off .focus-guide, .caption-mode .focus-guide { display: none; }
.top-guide { top: calc(50% - 62px); }
.bottom-guide { top: calc(50% + 51px); }
.caption {
  position: absolute; inset: 48px 8px; display: flex; align-items: center; justify-content: center;
  align-items: safe center;
  padding: 4px 0; white-space: normal; overflow-wrap: anywhere; overflow-y: auto;
  line-height: 1.5; text-align: center; font-weight: 500;
}
.sentence-slot {
  width: min(100%, 600px); height: var(--sentence-height);
  flex: 0 0 var(--sentence-height); margin: 0 auto;
}
.sentence-panel {
  display: grid; grid-template-rows: minmax(0, 1fr) 44px;
  width: 100%; height: 100%; margin: 0; padding: 4px 12px 0;
}
.sentence-context {
  position: relative; min-height: 0; margin: 0; overflow-y: auto;
  overscroll-behavior: contain; overflow-wrap: anywhere; scrollbar-width: thin;
  color: var(--muted); font-size: 15px; font-weight: 400; letter-spacing: 0; line-height: 1.6; text-align: left;
}
.sentence-context mark {
  background: transparent; color: var(--focus-color); font-weight: 600;
  text-decoration: underline; text-decoration-thickness: 2px; text-underline-offset: 3px;
}
.replay-sentence { justify-self: center; min-width: 150px; }

/* Progress and transport keep the same tracks in text and audio modes. */
.controls { width: min(100%, 480px); margin: 0 auto; padding-top: 4px; }
.reader-context { display: contents; }
.mobile-transcript-slot { display: none; }
.progress-meta {
  display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 12px;
  height: 20px; color: var(--muted); font-size: 11px; line-height: 20px; font-variant-numeric: tabular-nums;
}
.progress-meta > span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.progress-meta > span:last-child { text-align: right; }
input[type="range"] {
  display: block; width: 100%; height: 44px; margin: 0;
  appearance: none; -webkit-appearance: none; cursor: pointer; background: transparent; --fill: 0%;
}
input[type="range"]::-webkit-slider-runnable-track {
  height: 4px; border-radius: 4px;
  background: linear-gradient(to right, var(--accent) 0 var(--fill), var(--track) var(--fill) 100%);
}
input[type="range"]::-moz-range-track { height: 4px; background: var(--track); border-radius: 4px; }
input[type="range"]::-moz-range-progress { height: 4px; background: var(--accent); border-radius: 4px; }
input[type="range"]::-webkit-slider-thumb {
  appearance: none; -webkit-appearance: none; width: 16px; height: 16px; margin-top: -6px;
  background: var(--accent); border: 3px solid var(--bg); border-radius: 50%; box-shadow: 0 0 0 1px var(--accent);
}
input[type="range"]::-moz-range-thumb {
  width: 10px; height: 10px; background: var(--accent);
  border: 3px solid var(--bg); border-radius: 50%; box-shadow: 0 0 0 1px var(--accent);
}
.transport {
  display: grid; grid-template-columns: 44px 160px 44px;
  justify-content: center; align-items: center; gap: 24px; height: 56px;
}
.play-button {
  display: grid; grid-template-columns: 16px minmax(0, 1fr); align-items: center; gap: 10px;
  width: 160px; height: 48px; padding: 10px 28px; border-radius: 9px;
  background: var(--button); color: var(--button-text); font-size: 14px; font-weight: 550;
}
.play-button svg { width: 16px; height: 16px; fill: currentColor; stroke-width: 0; }
.pace-section { margin-top: 4px; padding-top: 4px; border-top: 1px solid var(--line); }
.pace-control {
  display: grid; grid-template-columns: minmax(0, 1fr);
  align-items: center; gap: 0; min-width: 0;
}
.pace-value { display: grid; grid-template-columns: 86px 52px; align-items: baseline; justify-content: center; gap: 6px; min-width: 0; }
.pace-value input {
  width: 86px; height: 48px; padding: 0; appearance: textfield; -moz-appearance: textfield;
  border: 0; border-radius: 4px; background: none; color: var(--focus-color);
  font-size: 32px; font-weight: 550; letter-spacing: -.5px; font-variant-numeric: tabular-nums; text-align: right;
}
.pace-value input::-webkit-outer-spin-button, .pace-value input::-webkit-inner-spin-button {
  -webkit-appearance: none; margin: 0;
}
.pace-value > span { color: var(--muted); font-size: 11px; letter-spacing: .5px; white-space: nowrap; }
.pace-average {
  height: 18px; margin: 0; overflow: hidden; text-overflow: ellipsis;
  color: var(--muted); font-size: 11px; line-height: 18px;
  font-variant-numeric: tabular-nums; text-align: center; white-space: nowrap;
}
/* Reserve the same space when audio is off; mode and rate changes must not
   move the dial, presets, or reading anchor. */
.pace-average:empty { visibility: hidden; }

/* Inline customization */
.settings-panel {
  margin-top: 24px; padding: 16px 0 24px; border-top: 1px solid var(--line); scroll-margin-top: 16px;
}
.settings-heading { display: flex; align-items: center; justify-content: space-between; gap: 16px; min-height: 44px; }
.settings-heading h2 { margin: 0; font-size: 16px; font-weight: 550; letter-spacing: -.2px; }
.settings-grid { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 20px 28px; margin-top: 8px; }
.setting-section { min-width: 0; padding-top: 18px; border-top: 1px solid var(--line); scroll-margin-top: 16px; }
.settings-panel h3 { margin: 0 0 10px; font-size: 12px; font-weight: 550; }
.settings-panel p { color: var(--muted); font-size: 12px; line-height: 1.6; }
.settings-panel .setting-help { margin: 7px 0 0; font-size: 11px; line-height: 18px; }
.settings-panel .dialog-error { color: var(--accent); font-size: 12px; line-height: 18px; }
.setting-label {
  display: flex; align-items: center; justify-content: space-between;
  margin: 0; font-size: 12px; font-weight: 550; line-height: 18px;
}
.setting-label > span { color: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.segmented, .font-options { display: flex; gap: 6px; }
.segmented button, .font-options button {
  min-width: 0; min-height: 44px; flex: 1; padding: 6px;
  border: 1px solid var(--line); border-radius: 7px; background: none; color: var(--muted); font-size: 12px;
}
.segmented button.selected, .font-options button.selected {
  color: var(--text); border-color: var(--green); background: var(--paper);
}
.size-control { margin: 0 0 7px; }
.font-options [data-font="serif"] { font-family: Georgia, serif; }
.font-options [data-font="mono"] { font-family: ui-monospace, monospace; }
.inline-field {
  display: flex; align-items: center; gap: 12px; margin-top: 12px; min-height: 44px; font-size: 12px;
}
.inline-field > label { flex: 1; font-weight: 500; }
.settings-panel select, .profile-edit-row input {
  min-height: 44px; min-width: 0; max-width: 100%; padding: 8px 10px;
  border: 1px solid var(--line); border-radius: 7px; background: var(--bg); color: var(--text); font-size: 14px;
}
.profile-edit-row input { font-size: 16px; }
.inline-field select { width: 124px; flex-shrink: 0; }
.inline-field .accent-preview { min-width: 18px; font-size: 25px; line-height: 1; text-align: center; }
.toggle-section {
  grid-column: 1 / -1; display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 16px 28px;
}
.toggle-row { position: relative; display: flex; align-items: center; gap: 12px; min-height: 44px; cursor: pointer; }
.toggle-row > span:first-child { flex: 1; display: grid; gap: 4px; }
.toggle-row strong { font-size: 12px; font-weight: 550; }
.toggle-row small { color: var(--muted); font-size: 11px; line-height: 18px; }
.toggle-row input {
  position: absolute; right: 0; z-index: 1; width: 44px; height: 44px; margin: 0; opacity: 0; cursor: pointer;
}
.switch {
  position: relative; width: 34px; height: 20px; border-radius: 12px; background: var(--track); flex-shrink: 0;
}
.switch::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 14px; height: 14px;
  border-radius: 50%; background: var(--paper); transition: transform .12s;
}
.toggle-row input:checked + .switch { background: var(--green); }
.toggle-row input:checked + .switch::after { transform: translateX(14px); }
.toggle-row input:focus-visible + .switch { outline: 2px solid var(--focus-color); outline-offset: 4px; }
.toggle-row:has(input:disabled) { cursor: default; opacity: .55; }
.setting-switch {
  display: grid; grid-template-columns: minmax(0, 1fr) 2.25em 34px;
  align-items: center; gap: 8px; width: 100%; min-width: 0; min-height: 44px;
  padding: 8px; border: 1px solid var(--line); border-radius: 7px;
  background: none; color: var(--text); font-size: 12px; line-height: 1.5; text-align: left;
}
.setting-switch > span:first-child { min-width: 0; overflow-wrap: anywhere; font-weight: 550; }
.setting-switch .switch-state { color: var(--muted); text-align: right; }
.setting-switch[aria-checked="true"] .switch { background: var(--green); }
.setting-switch[aria-checked="true"] .switch::after { transform: translateX(14px); }
.setting-switch:disabled { opacity: .55; }
.setting-context-slot {
  min-width: 0; block-size: 3em; margin-top: 4px; font-size: 12px; line-height: 1.5;
  overflow-y: auto; overscroll-behavior: contain; scrollbar-width: thin;
}
.setting-context-slot .setting-help { margin: 0; font-size: inherit; line-height: inherit; }
.guides-row { margin-top: 10px; }
.pause-setting .inline-field { margin-top: 0; }
.pause-setting select:disabled { opacity: .65; cursor: default; }
.theme-row { grid-column: 1 / -1; width: 100%; max-width: 480px; }

/* Pace and local audio */
.wpm-presets-section, .ramp-section, .audio-section { grid-column: 1 / -1; }
.wpm-presets-heading { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 44px; max-width: 600px; }
.wpm-presets-heading h3 { margin: 0; }
.wpm-presets-heading button { flex-shrink: 0; }
.wpm-preset-fields { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 12px; max-width: 600px; margin-top: 4px; }
.settings-panel .wpm-preset-status { block-size: 36px; max-width: 600px; overflow-y: auto; overscroll-behavior: contain; scrollbar-width: thin; }
#rampUnit { height: 18px; }
.ramp-fields {
  display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; max-width: 480px; margin-top: 10px;
}
.wpm-preset-fields label, .ramp-fields label { display: grid; gap: 6px; min-width: 0; color: var(--muted); font-size: 12px; }
.wpm-preset-fields input, .ramp-fields input {
  width: 100%; min-width: 0; min-height: 44px; padding: 8px 10px; border: 1px solid var(--line);
  border-radius: 7px; background: var(--bg); color: var(--text); font-size: 16px; font-variant-numeric: tabular-nums;
}
.ramp-footer {
  display: grid; grid-template-columns: minmax(0, 1fr) 112px; gap: 12px; align-items: center;
  max-width: 480px; height: 48px; color: var(--green); font-size: 11px; font-variant-numeric: tabular-nums;
}
#rampStatus { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ramp-section .setting-help { max-width: 600px; }
.ramp-section .pace-feedback {
  height: 36px; margin-top: 0; overflow-y: auto; overscroll-behavior: contain; scrollbar-width: thin;
}
.pace-feedback .setting-help { margin: 0; }
.audio-file-row {
  display: grid; grid-template-columns: minmax(0, 1fr) 118px 64px; align-items: center; gap: 10px; height: 44px;
}
.audio-file-row > span {
  min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--muted); font-size: 12px;
}
#audioUploadButton { width: 118px; padding-inline: 8px; }
.audio-remove-slot, #audioRemoveButton { width: 64px; height: 44px; }
.audio-feedback-slot {
  height: 60px; padding-top: 6px; overflow-y: auto; overscroll-behavior: contain; scrollbar-width: thin;
}
.audio-feedback-slot #audioHelp, .audio-feedback-slot #audioError { margin: 0; }
.audio-feedback-slot:has(#audioError:not([hidden])) #audioHelp { display: none; }
.audio-mode-controls {
  display: grid; grid-template-columns: repeat(2, minmax(0, 1fr));
  align-items: stretch; gap: 8px; max-width: 480px; min-height: 44px; margin-top: 8px;
}
.audio-options { max-width: 480px; }
.audio-options .setting-label { margin-bottom: 7px; }
.audio-options select { width: 100%; }

/* Local profiles and persistence feedback */
.profile-section { grid-column: 1 / -1; }
.profile-load-row, .profile-edit-row {
  display: grid; grid-template-columns: minmax(0, 1fr) 84px;
  align-items: end; gap: 8px; max-width: 480px;
}
.profile-edit-row { grid-template-columns: minmax(0, 1fr) 58px; }
.profile-edit-row > label {
  display: grid; gap: 7px; min-width: 0; color: var(--muted); font-size: 12px;
}
.profile-load-row select, .profile-edit-row input { width: 100%; }
#profileNewButton { width: 58px; height: 44px; padding-inline: 8px; }
.profile-editor { max-width: 480px; margin-top: 4px; }
.profile-editor > summary, .app-extras > summary {
  min-height: 44px; padding: 12px 2px; border-radius: 4px;
  color: var(--muted); font-size: 12px; line-height: 20px; cursor: pointer;
}
.profile-editor[open] > summary, .app-extras[open] > summary { color: var(--text); }
.profile-actions { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; max-width: 480px; margin-top: 8px; }
.profile-actions button { min-width: 0; padding-inline: 6px; white-space: normal; overflow-wrap: anywhere; line-height: 1.4; }
.profile-feedback-slot {
  height: 40px; padding-top: 4px; overflow-y: auto; overscroll-behavior: contain; scrollbar-width: thin;
}
.profile-feedback-slot #profileStatus, .profile-feedback-slot #preferencesStatus { margin: 0; font-size: 12px; line-height: 18px; }
.profile-feedback-slot:has(#preferencesStatus:not([hidden])) #profileStatus { display: none; }
.settings-panel .profile-error, .settings-panel .persistence-status { color: var(--accent); }
.notice-slot [tabindex]:focus-visible, .audio-feedback-slot [tabindex]:focus-visible,
.profile-feedback-slot [tabindex]:focus-visible, .install-status-slot [tabindex]:focus-visible {
  outline-offset: -2px;
}

/* Installation and secondary actions */
.app-extras { margin-top: 8px; border-top: 1px solid var(--line); }
.settings-panel .pwa-section { padding-top: 4px; border-top: 0; }
.install-row {
  display: grid; grid-template-columns: minmax(0, 1fr) 128px; gap: 12px; align-items: center; height: 44px;
}
.install-row h3 { margin: 0; }
.install-action-slot, #installButton { width: 128px; height: 44px; }
#installHelp { height: 54px; overflow-y: auto; overscroll-behavior: contain; scrollbar-width: thin; }
.install-status-slot {
  height: 60px; padding-top: 4px; overflow-y: auto; overscroll-behavior: contain; scrollbar-width: thin;
}
.install-status-slot #offlineStatus, .install-status-slot #pwaUpdateNote { margin: 0; }
.settings-panel #offlineStatus { color: var(--green); }
.install-status-slot:has(#pwaUpdateNote:not([hidden])) #offlineStatus { display: none; }
.settings-actions {
  display: flex; align-items: center; gap: 8px 18px; flex-wrap: wrap;
  margin-top: 16px; padding-top: 10px; border-top: 1px solid var(--line);
}
.settings-actions .text-button { font-size: 12px; }
.sample-action-slot { width: 88px; height: 44px; }
#sampleButton { width: 88px; }
#resetDefaultsButton { margin-left: auto; }
.settings-panel .privacy-note { margin: 8px 0 0; font-size: 11px; }

/* Dialogs: transcript, paste and help only; customization remains inline. */
.modal {
  width: calc(100% - 28px); max-width: 460px;
  max-height: calc(100vh - 32px); max-height: calc(100dvh - 32px);
  padding: 20px 23px; overflow: auto; overscroll-behavior: contain;
  scroll-padding-block: 76px 20px;
  border: 1px solid var(--line); border-radius: 14px;
  background: var(--paper); color: var(--text); box-shadow: 0 20px 80px #0003;
}
.modal::backdrop { background: #101a1570; backdrop-filter: blur(3px); }
.modal-top {
  position: sticky; top: 0; z-index: 2;
  display: flex; align-items: center; justify-content: space-between; gap: 15px;
  background: var(--paper); box-shadow: 0 1px 0 var(--line);
}
.modal h2 { min-width: 0; margin: 0; font-size: 18px; font-weight: 600; letter-spacing: -.4px; overflow-wrap: anywhere; }
.close-button {
  width: 44px; height: 44px; flex: 0 0 44px; border-radius: 8px;
  background: transparent; color: var(--muted); font-size: 25px; font-weight: 300;
}
.close-button:hover { background: var(--line); }
.modal-top .close-button:focus-visible { outline-offset: -3px; }
.modal p { color: var(--muted); font-size: 13px; line-height: 1.7; }
.modal h3 { margin: 20px 0 10px; font-size: 13px; font-weight: 550; }
.modal textarea {
  width: 100%; min-width: 0; min-height: 96px; height: 220px; height: clamp(96px, 32dvh, 220px);
  padding: 12px; border: 1px solid var(--line); border-radius: 8px;
  background: var(--bg); color: var(--text); font-family: ui-monospace, monospace;
  font-size: 16px; line-height: 1.6; resize: vertical;
}
.modal-actions { display: flex; flex-wrap: wrap; align-items: center; justify-content: flex-end; gap: 8px 12px; margin-top: 12px; }
.modal-actions button { max-width: 100%; white-space: normal; overflow-wrap: anywhere; }
.modal .dialog-error { height: 40px; margin: 8px 0 0; overflow-y: auto; color: var(--accent); font-size: 12px; line-height: 20px; }
.modal .privacy-note { margin: 10px 0 0; padding-top: 12px; border-top: 1px solid var(--line); font-size: 11px; }
.transcript-modal { max-width: 650px; }
.transcript-modal[open] {
  display: flex; flex-direction: column;
  height: min(720px, calc(100vh - 32px)); height: min(720px, calc(100dvh - 32px));
  overflow: hidden;
}
.transcript-modal > :not(.cue-list) { flex: 0 0 auto; }
.modal .transcript-source {
  max-height: 4.5em; margin: 0 0 10px; overflow-y: auto; overscroll-behavior: contain; scrollbar-width: thin;
  font-size: 12px; line-height: 1.5; overflow-wrap: anywhere;
}
.modal .transcript-source:focus-visible { outline-offset: -2px; }
.transcript-tools {
  display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 8px; align-items: center;
  min-height: 48px; margin-top: 12px; border-top: 1px solid var(--line); color: var(--muted); font-size: 11px;
}
.transcript-tools > span { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.transcript-tools > div { display: flex; gap: 10px; }
.transcript-tools .text-button { font-size: 11px; }
.cue-list {
  position: relative; flex: 1 1 auto; min-height: 0;
  margin: 4px -8px 0; overflow: auto; overscroll-behavior: contain;
}
.cue {
  display: grid; grid-template-columns: minmax(48px, max-content) minmax(0, 1fr); align-items: baseline; gap: 10px;
  width: 100%; padding: 11px 8px; border-radius: 6px; background: none;
  font-size: 14px; line-height: 1.65; text-align: left;
}
.cue:hover { background: var(--bg); }
.cue.current { background: var(--soft); }
.cue-time { color: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; white-space: nowrap; }
.cue-text { overflow-wrap: anywhere; }
.modal .transcript-foot { margin-bottom: 0; font-size: 11px; }
.research-links { display: grid; gap: 14px; margin: 20px 0; font-size: 12px; line-height: 1.6; }
.research-links a { text-underline-offset: 3px; }
#researchDialog h3 + p { margin-top: 0; }

/* Drag feedback is an overlay and cannot move the reader. */
.app.dragging::after {
  content: "Drop a VTT, audio, or both"; position: fixed; inset: 8px; z-index: 90;
  display: grid; place-items: center; border: 2px dashed var(--accent); border-radius: 12px;
  background: var(--paper); color: var(--text); font-size: 17px; pointer-events: none;
}
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; }
[hidden] { display: none !important; }

/* Responsive changes depend on viewport width, not playback or file state. */
@media (max-width: 600px) {
  :root { --sentence-height: 108px; }
  .app {
    padding-top: max(8px, env(safe-area-inset-top));
    padding-right: max(16px, env(safe-area-inset-right));
    padding-left: max(16px, env(safe-area-inset-left));
  }
  .toolbar {
    grid-template-columns: 112px minmax(0, 1fr) minmax(0, 1fr);
    grid-template-rows: max(48px, 4em) max(48px, 4em);
    align-items: stretch; gap: 4px 8px; height: auto; min-height: 112px; flex-basis: auto;
    padding: 5px 0 6px;
  }
  /* Fill the stable viewport while leaving the document free to scroll.
     The controls cannot cover settings or a field above the soft keyboard. */
  .reader-screen {
    min-height: calc(100vh - max(8px, env(safe-area-inset-top)) - 12px - env(safe-area-inset-bottom));
    min-height: calc(100svh - max(8px, env(safe-area-inset-top)) - 12px - env(safe-area-inset-bottom));
  }
  .reader { flex: 1; }
  .reader > .stage { flex: 1 0 var(--stage-height); }
  .controls { flex: 0 0 auto; }
  .toolbar .button, .toolbar .file-info, .replay-sentence { min-height: 48px; }
  .file-info { grid-column: 1 / -1; grid-row: 1; }
  #uploadButton { grid-column: 1; grid-row: 2; }
  #focusButton { grid-column: 2; grid-row: 2; }
  #settingsButton { grid-column: 3; grid-row: 2; }
  .file-info strong { font-size: 12px; }
  .sentence-panel { grid-template-rows: minmax(0, 1fr) 48px; padding-inline: 10px; }
  .sentence-context { font-size: 14px; }
  .transport { grid-template-columns: 48px 160px 48px; gap: 16px; height: 64px; }
  .transport .icon-button { width: 48px; height: 48px; }
  .play-button { height: 52px; }
  #progress, #wpmRange, .pace-value input { height: 48px; }
  #progress::-webkit-slider-thumb, #wpmRange::-webkit-slider-thumb { width: 24px; height: 24px; margin-top: -10px; }
  #progress::-moz-range-thumb, #wpmRange::-moz-range-thumb { width: 18px; height: 18px; }
  .settings-grid, .toggle-section { grid-template-columns: minmax(0, 1fr); }
  .settings-grid { row-gap: 18px; }
  .wpm-preset-fields { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .settings-panel { margin-top: 20px; }
  .settings-panel select, .profile-edit-row input { font-size: 16px; }
  .audio-file-row { gap: 8px; }
  .audio-mode-controls { grid-template-columns: minmax(0, 1fr); }
  .audio-feedback-slot { height: 78px; }
  #installHelp { height: 72px; }
  .settings-actions { gap: 6px 14px; }
  #resetDefaultsButton { margin-left: 0; }
  .modal { padding: 16px 19px; border-radius: 12px; }
  .transcript-tools { grid-template-columns: minmax(0, 1fr); gap: 0; padding-top: 8px; }
  .transcript-tools > span { line-height: 20px; }
  .transcript-tools > div { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
  .cue { grid-template-columns: minmax(42px, max-content) minmax(0, 1fr); gap: 8px; font-size: 13px; }
}
@media (max-width: 360px) {
  .toolbar-button svg { display: none; }
  .transport { grid-template-columns: 48px 144px 48px; gap: 12px; }
  .play-button { width: 144px; padding-inline: 20px; }
  .audio-file-row { grid-template-columns: minmax(0, 1fr) 112px 60px; gap: 6px; }
  #audioUploadButton { width: 112px; }
  .audio-remove-slot, #audioRemoveButton { width: 60px; }
  .modal { padding-inline: 16px; }
}
@media (max-height: 500px) {
  .modal { padding-block: 8px; scroll-padding-block: 60px 8px; }
  .modal .transcript-source { max-height: 3em; margin-bottom: 4px; }
  .transcript-tools { margin-top: 4px; }
  .modal .transcript-foot { margin-top: 6px; }
}
/* Extremely short windows still allow every row to be reached. */
@media (max-height: 299px) {
  .transcript-modal[open] { overflow-y: auto; }
  .transcript-modal .cue-list { flex: 0 0 120px; min-height: 120px; }
}
/* Press feedback changes paint only, never dimensions or the word anchor. */
.play-button:not(:disabled):active, .open-button:not(:disabled):active {
  box-shadow: inset 0 0 0 2px var(--button-text);
}
.icon-button:not(:disabled):active, .text-button:not(:disabled):active,
.toolbar-button:not(:disabled):active, .secondary-button:not(:disabled):active {
  background: var(--soft); color: var(--accent);
}
.focus-exit-label { display: none; }

/* Responsive placement reuses the existing controls and their listeners.
   The measured bar height also covers safe areas and larger text settings. */
:root { --mobile-nav-space: calc(65px + env(safe-area-inset-bottom)); }
.mobile-nav { display: none; }
html.has-mobile-nav {
  scroll-padding-bottom: calc(var(--mobile-nav-space) + 8px);
}
.has-mobile-nav .app {
  padding-top: max(8px, env(safe-area-inset-top));
  padding-bottom: calc(var(--mobile-nav-space) + 8px);
}
.has-mobile-nav .reader-screen { min-height: 0; }
.has-mobile-nav .toolbar { display: none; }

/* Only the working drawer layout becomes an app-sized reader. If dialogs are
   unavailable, inline settings retain the normal scrollable document fallback.
   The border-box owns safe areas and nav space once; intrinsic control sizes
   can still grow the page for a raised keyboard or extreme text zoom. */
html.has-mobile-nav.has-settings-drawer { scrollbar-gutter: auto; }
.has-mobile-nav.has-settings-drawer body:not(.focus-mode) .app {
  display: flex; flex-direction: column;
  min-height: 100vh; min-height: 100dvh;
}
.has-mobile-nav.has-settings-drawer body:not(.focus-mode) .reader-screen,
.has-mobile-nav.has-settings-drawer body:not(.focus-mode) .reader { flex: 1 0 auto; }

/* Transcript access shares the progress row near the playback controls. The
   live button and its filename are retained, without a separate top card. */
.has-mobile-nav body:not(.focus-mode) .reader-context {
  display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  align-items: center; gap: 12px; height: 44px;
}
.has-mobile-nav body:not(.focus-mode) .mobile-transcript-slot { display: block; min-width: 0; }
.has-mobile-nav #mobileTranscriptSlot #transcriptButton {
  width: 100%; min-width: 0; min-height: 44px; height: 44px;
  margin: 0; padding: 4px 6px; border-color: transparent;
}
.has-mobile-nav #mobileTranscriptSlot .file-info strong { font-size: 12px; }
.has-mobile-nav body:not(.focus-mode) .progress-meta {
  grid-template-columns: minmax(0, 1fr); grid-template-rows: 20px 20px;
  gap: 0; height: 40px; text-align: right;
}
.has-mobile-nav body:not(.focus-mode) .notice-slot {
  position: relative; inset: auto; flex: 0 0 44px; height: 44px;
}
.has-mobile-nav body:not(.focus-mode) .reader > .stage {
  height: auto; min-height: 136px; flex: 1 0 136px;
}
.has-mobile-nav body:not(.focus-mode) .caption { inset: 10px 8px; }
.has-mobile-nav body:not(.focus-mode) .sentence-slot { height: 72px; flex-basis: 72px; }
.has-mobile-nav body:not(.focus-mode) .sentence-panel {
  grid-template-columns: minmax(0, 1fr) 104px; grid-template-rows: minmax(0, 1fr);
  align-items: center; gap: 8px; padding: 4px 0;
}
.has-mobile-nav body:not(.focus-mode) .sentence-context { align-self: stretch; font-size: 14px; line-height: 1.5; }
.has-mobile-nav body:not(.focus-mode) .replay-sentence {
  min-width: 0; width: 104px; min-height: 44px; padding: 6px;
  line-height: 16px; white-space: normal;
}
.has-mobile-nav body:not(.focus-mode) #progress { height: 44px; }
.has-mobile-nav body:not(.focus-mode) .transport { height: 56px; }
.has-mobile-nav body:not(.focus-mode) .play-button { height: 48px; }
.has-mobile-nav #mobileNav {
  position: fixed; inset: auto 0 0; z-index: 70;
  display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 4px;
  padding: 8px max(12px, env(safe-area-inset-right), calc((100% - 800px) / 2))
    calc(8px + env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left), calc((100% - 800px) / 2));
  border-top: 1px solid var(--line); background: var(--paper);
}
#mobileNav > button {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  grid-column: auto; grid-row: auto; gap: 4px;
  width: 100%; min-width: 0; min-height: 48px; height: auto;
  margin: 0; padding: 4px 2px; border: 0; border-radius: 8px;
  background: transparent; color: var(--muted);
  font-size: 12px; line-height: 16px; font-weight: 500;
  text-align: center; text-decoration: none; white-space: normal; overflow-wrap: anywhere;
}
#mobileNav button svg { display: block; width: 18px; height: 18px; flex: 0 0 18px; }
#settingsPanel #calibrationButton .pace-test-icon { display: none; }
#mobileNav #calibrationButton small,
#mobileNav #calibrationButton .settings-action-chevron { display: none; }
#mobileNav #calibrationButton strong { font: inherit; }
#mobileNav #calibrationButton > span { min-width: 0; }
#mobileNav > button:focus-visible { outline-offset: 0; }
#mobileNav > button[aria-current="page"], #mobileNav > button[aria-pressed="true"],
#mobileNav > button[aria-expanded="true"] { color: var(--focus-color); background: var(--soft); }
#mobileNav > button:not(:disabled):active { background: var(--soft); color: var(--accent); box-shadow: none; }
@media (hover: hover) {
  #mobileNav > button:not(:disabled):hover { background: var(--soft); color: var(--accent); text-decoration: none; }
}

/* Keep play and the ruler reachable on shorter phones without shrinking tap
   targets. The exact value and dial remain; presets are optional shortcuts. */
@media (max-height: 740px) {
  .has-mobile-nav .pace-control {
    --pace-ruler-height: 60px;
    grid-template-columns: 120px minmax(0, 1fr); height: 60px; gap: 8px;
  }
  .has-mobile-nav .pace-value { display: flex; align-items: baseline; justify-content: center; height: 44px; gap: 4px; }
  .has-mobile-nav .pace-value input { width: 64px; height: 44px; font-size: 26px; }
  .has-mobile-nav .has-pace-dial .pace-presets { display: none; }
}

/* Short phones keep full touch targets while the word area yields space.
   All tracks are independent of play/pause, audio, and sentence visibility. */
@media (max-height: 640px) {
  .has-mobile-nav body:not(.focus-mode) .reader > .stage { min-height: 124px; flex-basis: 124px; }
  .has-mobile-nav body:not(.focus-mode) .sentence-slot { height: 56px; flex-basis: 56px; }
  .has-mobile-nav body:not(.focus-mode) .transport { height: 52px; }
  .has-mobile-nav body:not(.focus-mode) .pace-section { padding-top: 2px; }
  .has-mobile-nav body:not(.focus-mode) .pace-control { --pace-ruler-height: 48px; height: 48px; }
  .has-mobile-nav body:not(.focus-mode) .pace-tick { top: 8px; }
  .has-mobile-nav body:not(.focus-mode) .pace-tick.major-tick::before { height: 20px; }
  .has-mobile-nav body:not(.focus-mode) .pace-tick-label { top: 24px; font-size: 11px; line-height: 12px; }
  .has-mobile-nav body:not(.focus-mode) .pace-indicator { top: 2px; }
}

/* Landscape uses available width instead of forcing two full vertical stacks
   into a phone's short height. Very small keyboard/zoom views may still scroll. */
@media (min-width: 480px) and (max-width: 960px) and (max-height: 500px) {
  .has-mobile-nav.has-settings-drawer body:not(.focus-mode) .reader {
    display: grid; grid-template-columns: minmax(0, 1fr) minmax(240px, 44%);
    grid-template-rows: 44px minmax(124px, 1fr) 56px; column-gap: 20px;
  }
  .has-mobile-nav.has-settings-drawer body:not(.focus-mode) .notice-slot { grid-column: 1; grid-row: 1; }
  .has-mobile-nav.has-settings-drawer body:not(.focus-mode) .reader > .stage { grid-column: 1; grid-row: 2; }
  .has-mobile-nav.has-settings-drawer body:not(.focus-mode) .sentence-slot { grid-column: 1; grid-row: 3; }
  .has-mobile-nav.has-settings-drawer body:not(.focus-mode) .controls {
    grid-column: 2; grid-row: 1 / 4; align-self: end; min-width: 0;
  }
  .has-mobile-nav.has-settings-drawer body:not(.focus-mode) .transport {
    grid-template-columns: 44px minmax(0, 1fr) 44px; gap: 8px;
  }
  .has-mobile-nav.has-settings-drawer body:not(.focus-mode) .transport .icon-button { width: 44px; height: 44px; }
  .has-mobile-nav.has-settings-drawer body:not(.focus-mode) .play-button { width: 100%; padding-inline: 12px; }
  .has-mobile-nav.has-settings-drawer body:not(.focus-mode) .sentence-panel { grid-template-columns: minmax(0, 1fr) 80px; }
  .has-mobile-nav.has-settings-drawer body:not(.focus-mode) .replay-sentence { width: 80px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    scroll-behavior: auto !important;
    transition: none !important;
    animation: none !important;
  }
}
