:root {
  --bg: #f3f4f6;
  --panel: #ffffff;
  --panel-2: #f8fafc;
  --text: #15171a;
  --muted: #6b7280;
  --line: #e5e7eb;
  --accent: #111827;
  --accent-soft: #e5e7eb;
  --focus: #ef4444;
  --shadow: 0 20px 45px rgba(15, 23, 42, 0.08);
}


body.paper {
  --bg: #e8dec5;
  --panel: #f8f0dc;
  --panel-2: #f1e5c8;
  --text: #382f25;
  --muted: #786a56;
  --line: #d8c7a3;
  --accent: #4b3b2b;
  --accent-soft: #e5d6b7;
  --focus: #a74431;
  --shadow: 0 18px 40px rgba(82, 62, 35, 0.10);
}

body.dark {
  --bg: #090b0f;
  --panel: #11141a;
  --panel-2: #171b22;
  --text: #f5f7fa;
  --muted: #9aa2ad;
  --line: #242a33;
  --accent: #f8fafc;
  --accent-soft: #242a33;
  --shadow: 0 20px 45px rgba(0, 0, 0, 0.35);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  transition: background 180ms ease, color 180ms ease;
}

body.paper {
  background-color: var(--bg);
  background-image:
    radial-gradient(circle at 18% 22%, rgba(93, 72, 44, 0.035) 0 1px, transparent 1.4px),
    radial-gradient(circle at 72% 64%, rgba(255, 255, 255, 0.16) 0 1px, transparent 1.5px);
  background-size: 19px 19px, 27px 27px;
}

button, input, textarea { font: inherit; }
button { cursor: pointer; }

.app-shell {
  width: min(1000px, calc(100% - 32px));
  margin: 0 auto;
  padding: 32px 0 56px;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--muted);
  font-weight: 800;
  font-size: 0.72rem;
  letter-spacing: 0.18em;
}

h1, h2, p { margin-top: 0; }
h1 { margin-bottom: 0; font-size: clamp(1.7rem, 4vw, 2.4rem); }
h2 { margin-bottom: 4px; font-size: 1.15rem; }
.section-heading p { color: var(--muted); margin-bottom: 0; }

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 22px;
  box-shadow: var(--shadow);
}

.import-card { padding: 20px; margin-bottom: 18px; }
.reader-card { padding: 22px; }

.section-heading {
  display: flex;
  gap: 16px;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

textarea {
  width: 100%;
  min-height: 150px;
  resize: vertical;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--panel-2);
  color: var(--text);
  padding: 16px;
  outline: none;
}

textarea:focus { border-color: var(--muted); }

.file-btn, .primary-btn, .secondary-btn, .ghost-btn, .icon-btn {
  border: 0;
  border-radius: 12px;
  padding: 10px 14px;
  font-weight: 750;
}

.file-btn, .primary-btn {
  background: var(--accent);
  color: var(--bg);
}

.file-btn { display: inline-flex; align-items: center; white-space: nowrap; }
.file-btn input { display: none; }
.secondary-btn { background: var(--accent-soft); color: var(--text); }
.ghost-btn, .icon-btn { background: transparent; color: var(--text); border: 1px solid var(--line); }
.icon-btn { width: 42px; height: 42px; padding: 0; }

.import-actions { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 12px; }

.reader-meta {
  display: flex;
  justify-content: space-between;
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 650;
}

.word-stage {
  min-height: 240px;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.word-display {
  position: relative;
  width: 100%;
  height: 1em;
  font-size: 64px;
  font-weight: 800;
  letter-spacing: -0.04em;
  line-height: 1;
  text-align: center;
  white-space: nowrap;
}

/*
  In focus mode the word is not centered as a whole. The .orp-word starts at
  the exact horizontal midpoint of the stage and JavaScript translates it by
  the rendered centre of the highlighted glyph. This makes the red focus
  letter occupy the same screen position for every word.
*/
.word-display.orp-active {
  text-align: left;
}

.orp-word {
  position: absolute;
  left: 50%;
  top: 50%;
  white-space: nowrap;
  transform-origin: left center;
}

.focus-letter { color: var(--focus); }

.focus-guide {
  position: absolute;
  left: 50%;
  width: 1px;
  height: 14px;
  background: var(--muted);
  opacity: 0.35;
}
.focus-guide-left { top: 54px; }
.focus-guide-right { bottom: 54px; }

.progress-track {
  height: 8px;
  background: var(--accent-soft);
  border-radius: 99px;
  overflow: hidden;
}

.progress-bar {
  width: 0%;
  height: 100%;
  background: var(--accent);
  border-radius: inherit;
  transition: width 100ms linear;
}

.controls-row {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 12px;
}
.main-controls { margin: 24px 0; }

.round-btn, .play-btn {
  border: 1px solid var(--line);
  color: var(--text);
  background: var(--panel-2);
  border-radius: 50%;
}
.round-btn { width: 48px; height: 48px; font-size: 1.6rem; }
.play-btn {
  width: 66px;
  height: 66px;
  font-size: 1.35rem;
  background: var(--accent);
  color: var(--bg);
  border: 0;
}

.settings-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 18px;
  padding-top: 8px;
}

.settings-grid label > span {
  display: block;
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 700;
  margin-bottom: 8px;
}

.inline-setting {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 12px;
}
input[type="range"] { width: 100%; accent-color: var(--accent); }
.inline-setting strong { font-size: 0.85rem; min-width: 88px; text-align: right; }

.toggle-row {
  display: flex;
  gap: 20px;
  flex-wrap: wrap;
  border-top: 1px solid var(--line);
  margin-top: 20px;
  padding-top: 18px;
}
.toggle-label { display: flex; gap: 8px; align-items: center; color: var(--muted); font-size: 0.9rem; font-weight: 650; }
.toggle-label input { accent-color: var(--accent); }

.stats-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
  margin-top: 16px;
}
.stat-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 16px;
}
.stat-card span { display: block; color: var(--muted); font-size: 0.8rem; margin-bottom: 6px; }
.stat-card strong { font-size: 1.05rem; }

@media (max-width: 680px) {
  .app-shell { width: min(100% - 20px, 1000px); padding-top: 18px; }
  .section-heading { align-items: flex-start; }
  .settings-grid, .stats-row { grid-template-columns: 1fr; }
  .word-stage { min-height: 210px; }
  .word-display { font-size: 52px; }
  .round-btn { width: 44px; height: 44px; }
  .main-controls { gap: 9px; }
}

/* Book import + chapter navigation */
.import-card.dragging {
  outline: 2px dashed var(--muted);
  outline-offset: 4px;
}

.file-status {
  display: flex;
  align-items: center;
  gap: 9px;
  min-height: 38px;
  margin: -2px 0 14px;
  padding: 9px 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--panel-2);
  color: var(--muted);
  font-size: 0.86rem;
  font-weight: 650;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #94a3b8;
  flex: 0 0 auto;
}

.file-status[data-state="loading"] .status-dot {
  background: #f59e0b;
  animation: statusPulse 900ms ease-in-out infinite alternate;
}

.file-status[data-state="success"] .status-dot { background: #22c55e; }
.file-status[data-state="error"] { color: #dc2626; border-color: rgba(220, 38, 38, 0.25); }
.file-status[data-state="error"] .status-dot { background: #ef4444; }

@keyframes statusPulse {
  from { opacity: 0.4; transform: scale(0.85); }
  to { opacity: 1; transform: scale(1.15); }
}

.book-info {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: center;
  margin-bottom: 14px;
  padding: 13px 14px;
  border: 1px solid var(--line);
  border-radius: 14px;
}

.book-info > div {
  min-width: 0;
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 2px 9px;
  align-items: baseline;
}

.book-info strong {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.book-info #bookAuthor {
  grid-column: 2;
  color: var(--muted);
  font-size: 0.82rem;
}

.book-info > span:last-child {
  color: var(--muted);
  font-size: 0.82rem;
  white-space: nowrap;
}

.book-format {
  grid-row: 1 / span 2;
  align-self: center;
  padding: 4px 7px;
  border-radius: 7px;
  background: var(--accent-soft);
  color: var(--text);
  font-size: 0.66rem;
  font-weight: 850;
  letter-spacing: 0.08em;
}

.reader-toolbar {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 18px;
}

.reader-toolbar .reader-meta {
  margin-left: auto;
  gap: 18px;
}

.chapter-control {
  min-width: min(420px, 62%);
}

.chapter-control > span {
  display: block;
  margin-bottom: 6px;
  color: var(--muted);
  font-size: 0.74rem;
  font-weight: 750;
}

.chapter-control select {
  width: 100%;
  min-height: 38px;
  padding: 7px 34px 7px 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel-2);
  color: var(--text);
  font: inherit;
  font-size: 0.85rem;
  outline: none;
}

.format-note {
  max-width: 760px;
  margin: 15px auto 0;
  color: var(--muted);
  font-size: 0.78rem;
  text-align: center;
}

@media (max-width: 680px) {
  .book-info { align-items: flex-start; }
  .book-info > span:last-child { display: none; }
  .reader-toolbar { display: block; }
  .chapter-control { min-width: 0; width: 100%; margin-bottom: 12px; }
  .reader-toolbar .reader-meta { display: flex; }
}

/* Persistent book library */
.visually-hidden {
  position: fixed;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

.topbar-actions { display: flex; align-items: center; gap: 10px; }
.theme-select {
  min-height: 42px;
  padding: 8px 30px 8px 11px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--panel);
  color: var(--text);
  font: inherit;
  font-size: 0.86rem;
  font-weight: 700;
  outline: none;
}
.theme-select:focus { border-color: var(--muted); }
.library-card { padding: 22px; margin-bottom: 18px; }
.library-heading {
  display: flex;
  justify-content: space-between;
  align-items: end;
  gap: 20px;
  margin-bottom: 18px;
}
.library-heading h2 { display: flex; align-items: center; gap: 8px; margin: 0; font-size: 1.35rem; }
.count-pill {
  min-width: 27px;
  padding: 3px 8px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--muted);
  font-size: 0.72rem;
  text-align: center;
}
.library-tools { display: flex; gap: 9px; flex: 1; justify-content: flex-end; }
.search-input, .sort-select {
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 11px;
  background: var(--panel-2);
  color: var(--text);
  outline: none;
  padding: 8px 11px;
}
.search-input { width: min(280px, 100%); }
.sort-select { max-width: 180px; }
.search-input:focus, .sort-select:focus { border-color: var(--muted); }

.library-label {
  margin: 0 0 9px;
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 800;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}
.continue-section { margin-bottom: 22px; }
.continue-book {
  display: grid;
  grid-template-columns: 74px 1fr auto;
  gap: 16px;
  align-items: center;
  border: 1px solid var(--line);
  border-radius: 17px;
  background: var(--panel-2);
  padding: 12px;
}
.continue-cover { width: 74px; height: 98px; border-radius: 9px; overflow: hidden; background: var(--accent-soft); }
.continue-cover img, .book-cover img { width: 100%; height: 100%; object-fit: cover; display: block; }
.continue-info { min-width: 0; }
.continue-info h3 { margin: 0 0 3px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.continue-info p { margin: 0 0 11px; color: var(--muted); font-size: 0.84rem; }
.continue-actions { display: flex; gap: 8px; }

.mini-progress { height: 5px; background: var(--line); border-radius: 99px; overflow: hidden; margin-bottom: 6px; }
.mini-progress > span { display: block; height: 100%; background: var(--accent); border-radius: inherit; }
.progress-caption { color: var(--muted); font-size: 0.74rem; font-weight: 700; }

.library-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(155px, 1fr));
  gap: 16px;
}
.book-card {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--panel-2);
  overflow: hidden;
  transition: transform 150ms ease, border-color 150ms ease, box-shadow 150ms ease;
}
.book-card:hover { transform: translateY(-2px); border-color: var(--muted); box-shadow: 0 10px 24px rgba(15, 23, 42, 0.08); }
.book-card.active { outline: 2px solid var(--accent); outline-offset: 2px; }
.book-open-area { width: 100%; padding: 0; border: 0; background: transparent; color: inherit; text-align: left; }
.book-cover {
  position: relative;
  aspect-ratio: 2 / 3;
  background: linear-gradient(145deg, var(--accent-soft), var(--panel));
  overflow: hidden;
}
.cover-placeholder {
  height: 100%;
  display: grid;
  place-items: center;
  padding: 18px;
  text-align: center;
  background: linear-gradient(145deg, var(--accent), #475569);
  color: var(--bg);
  font-weight: 850;
  line-height: 1.1;
  font-size: 1rem;
  word-break: break-word;
}
.cover-format {
  position: absolute;
  top: 8px;
  right: 8px;
  padding: 4px 6px;
  border-radius: 6px;
  background: rgba(0, 0, 0, 0.64);
  color: white;
  font-size: 0.58rem;
  font-weight: 850;
  letter-spacing: 0.08em;
}
.book-copy { padding: 11px 11px 7px; }
.book-title { font-size: 0.91rem; font-weight: 800; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.book-author { min-height: 17px; margin-top: 2px; color: var(--muted); font-size: 0.75rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.book-copy .mini-progress { margin-top: 10px; }
.book-actions { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; padding: 5px 10px 10px; }
.book-action {
  min-height: 31px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  color: var(--muted);
  font-size: 0.7rem;
  font-weight: 750;
}
.book-action:hover { color: var(--text); }
.book-action.danger:hover { color: #dc2626; border-color: rgba(220,38,38,.35); }
.library-empty { text-align: center; padding: 38px 20px 42px; }
.library-empty[hidden] { display: none; }
.empty-icon { font-size: 2.3rem; color: var(--muted); margin-bottom: 8px; }
.library-empty h3 { margin: 0 0 6px; }
.library-empty p { max-width: 460px; margin: 0 auto 16px; color: var(--muted); }
.empty-filter { grid-column: 1 / -1; color: var(--muted); text-align: center; padding: 28px 10px; }

.paste-panel { margin-top: 14px; }
.paste-panel summary { cursor: pointer; color: var(--muted); font-size: 0.84rem; font-weight: 750; margin-bottom: 12px; }

@media (max-width: 760px) {
  .library-heading { align-items: stretch; flex-direction: column; }
  .library-tools { justify-content: stretch; }
  .search-input { width: 100%; flex: 1; }
  .continue-book { grid-template-columns: 58px 1fr; }
  .continue-cover { width: 58px; height: 82px; }
  .continue-actions { grid-column: 1 / -1; }
  .continue-actions .primary-btn, .continue-actions .ghost-btn { flex: 1; }
}

@media (max-width: 520px) {
  .topbar-actions .primary-btn { padding: 9px 10px; font-size: 0.82rem; }
  .library-tools { flex-direction: column; }
  .sort-select { max-width: none; width: 100%; }
  .library-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 11px; }
  .library-card { padding: 16px; }
}


/* Normal page-reading mode */
.reading-mode-switch {
  display: inline-flex;
  padding: 4px;
  margin: 0 auto 16px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--panel-2);
}
.reading-mode-btn {
  border: 0;
  border-radius: 9px;
  padding: 8px 14px;
  background: transparent;
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 800;
}
.reading-mode-btn.active {
  background: var(--panel);
  color: var(--text);
  box-shadow: 0 2px 8px rgba(15, 23, 42, 0.08);
}
.word-reader-panel[hidden],
.normal-reader-panel[hidden] { display: none !important; }

.normal-reader-panel { margin-top: 14px; }
.normal-reader-topline {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 20px;
  padding: 4px 4px 14px;
  border-bottom: 1px solid var(--line);
}
.normal-reader-topline h2 { margin: 0; font-size: 1.25rem; }
.normal-font-control {
  display: grid;
  grid-template-columns: auto minmax(110px, 190px) auto;
  align-items: center;
  gap: 9px;
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 750;
}
.normal-font-control input { min-width: 110px; }
.normal-font-control strong { min-width: 46px; text-align: right; color: var(--text); }
.normal-reader-scroll {
  height: min(64vh, 720px);
  min-height: 420px;
  overflow: auto;
  overscroll-behavior: contain;
  scroll-behavior: auto;
  padding: clamp(28px, 5vw, 62px) clamp(20px, 8vw, 90px);
  margin: 0 -4px;
  outline: none;
  scrollbar-gutter: stable;
}
.normal-reader-text {
  width: min(100%, 720px);
  margin: 0 auto;
  color: var(--text);
  font-family: Georgia, "Times New Roman", serif;
  font-size: 21px;
  line-height: 1.72;
  letter-spacing: 0.005em;
}
.normal-reader-text p { margin: 0 0 1.15em; }
.normal-reader-text h3 {
  margin: 0 0 1.25em;
  font-family: inherit;
  font-size: 1.32em;
  line-height: 1.35;
}
.normal-word {
  border-radius: 4px;
  transition: background 90ms ease, box-shadow 90ms ease;
  cursor: text;
}
.normal-word.current {
  background: color-mix(in srgb, var(--focus) 16%, transparent);
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--focus) 10%, transparent);
}
.normal-reader-footer {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 12px;
  border-top: 1px solid var(--line);
  padding-top: 14px;
}
.normal-reader-footer button:first-child { justify-self: start; }
.normal-reader-footer button:last-child { justify-self: end; }
.normal-reader-footer button:disabled { opacity: 0.35; cursor: not-allowed; }
#normalPositionLabel { color: var(--muted); font-size: 0.8rem; font-weight: 750; }

@media (max-width: 680px) {
  .reading-mode-switch { width: 100%; }
  .reading-mode-btn { flex: 1; }
  .normal-reader-topline { align-items: stretch; flex-direction: column; gap: 12px; }
  .normal-font-control { grid-template-columns: auto 1fr auto; }
  .normal-reader-scroll { height: 62vh; min-height: 360px; padding: 28px 13px 42px; }
  .normal-reader-text { line-height: 1.68; }
  .normal-reader-footer { grid-template-columns: 1fr 1fr; }
  #normalPositionLabel { grid-column: 1 / -1; grid-row: 1; text-align: center; }
  .normal-reader-footer button:first-child { grid-column: 1; grid-row: 2; }
  .normal-reader-footer button:last-child { grid-column: 2; grid-row: 2; }
}

/* Reader view modes */
.reader-card { position: relative; }
.reader-view-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-bottom: 10px;
}
.view-mode-btn {
  min-height: 38px;
  padding: 8px 12px;
  font-size: 0.82rem;
}

/* Focus View fills the webpage only. Browser fullscreen is a separate, optional
   action exposed by the Fullscreen button. */
body.focus-view {
  overflow: hidden;
}
body.focus-view .app-shell {
  width: 100%;
  height: var(--focus-visible-height, 100dvh);
  max-width: none;
  padding: 0;
  margin: 0;
}
body.focus-view .topbar,
body.focus-view .library-card,
body.focus-view .import-card,
body.focus-view .stats-row,
body.focus-view .format-note {
  display: none !important;
}
body.focus-view .reader-card {
  width: 100%;
  height: var(--focus-visible-height, 100dvh);
  min-height: var(--focus-visible-height, 100dvh);
  margin: 0;
  padding: clamp(16px, 3vw, 34px);
  border: 0;
  border-radius: 0;
  box-shadow: none;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto auto;
  background: var(--bg);
}
body.focus-view .reader-view-actions {
  position: relative;
  z-index: 3;
  margin: 0;
}
body.focus-view #focusViewBtn { display: none; }
body.focus-view #fullscreenBtn,
body.focus-view #fullPageBtn { display: inline-flex; }
body.focus-view .reading-mode-switch,
body.focus-view .reader-toolbar,
body.focus-view .settings-grid,
body.focus-view .toggle-row {
  display: none !important;
}
body.focus-view .word-stage {
  min-height: 0;
  height: 100%;
}
body.focus-view .word-display {
  font-size: clamp(52px, 8vw, 112px);
}
body.focus-view .progress-track {
  width: min(760px, 88vw);
  margin: 0 auto;
}
body.focus-view .main-controls {
  margin: clamp(18px, 3vh, 30px) 0 0;
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
body.focus-view .round-btn {
  width: clamp(46px, 6vw, 58px);
  height: clamp(46px, 6vw, 58px);
}
body.focus-view .play-btn {
  width: clamp(66px, 8vw, 82px);
  height: clamp(66px, 8vw, 82px);
}

@media (max-width: 680px) {
  body.focus-view .reader-card { padding: 14px 12px 18px; }
  body.focus-view .reader-view-actions { padding-top: env(safe-area-inset-top, 0px); }
  body.focus-view .word-display { font-size: clamp(48px, 15vw, 78px); }
  body.focus-view .main-controls { gap: 8px; }
  body.focus-view .round-btn { width: 44px; height: 44px; }
  body.focus-view .play-btn { width: 64px; height: 64px; }
}

/* Dedicated Library -> Reader navigation */
.app-view[hidden] { display: none !important; }
.library-view,
.reader-page { width: 100%; }

.reader-page-header {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 18px;
  margin-bottom: 18px;
}

.back-library-btn { white-space: nowrap; }
.reader-book-heading { min-width: 0; text-align: center; }
.reader-book-heading h1 {
  margin: 0;
  font-size: clamp(1.25rem, 3vw, 1.8rem);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.reader-book-author {
  min-height: 18px;
  margin: 0 0 3px;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.reader-header-actions { display: flex; align-items: center; gap: 9px; }
.reader-page .reader-card { margin: 0; }

@media (max-width: 680px) {
  .reader-page-header {
    grid-template-columns: auto minmax(0, 1fr) auto;
    gap: 8px;
  }
  .reader-header-actions .theme-select { display: none; }
  .back-library-btn,
  .reader-header-actions .view-mode-btn { padding: 9px 10px; font-size: 0.78rem; }
  .reader-book-heading h1 { font-size: 1.05rem; }
  .reader-book-author { font-size: 0.68rem; }
}

/* Focus View is a reader-only webpage mode. It no longer represents the whole app. */
body.focus-view .reader-page-header,
body.focus-view .reader-stats {
  display: none !important;
}
body.focus-view #readerView { display: block !important; }

body.focus-view #wordReaderPanel { display: block !important; }
body.focus-view #normalReaderPanel { display: none !important; }

/* The RSVP controls are wrapped as one panel now that Page Reading exists.
   In distraction-free Focus View the wrapper itself owns the full-height grid. */
body.focus-view .reader-card {
  grid-template-rows: auto minmax(0, 1fr);
}
body.focus-view #wordReaderPanel {
  display: grid !important;
  min-height: 0;
  grid-template-rows: minmax(0, 1fr) auto auto;
}

/* ================================================================
   FocusRead V1 reader tools: bookmarks, search, navigation, sheets
   ================================================================ */
button:disabled { opacity: 0.38; cursor: not-allowed; }

.reader-page-header {
  position: sticky;
  top: 0;
  z-index: 20;
  padding: 10px 0;
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(14px);
}

.reader-header-actions {
  justify-content: flex-end;
  flex-wrap: wrap;
}
.reader-action-btn {
  min-height: 40px;
  display: inline-flex;
  gap: 6px;
  align-items: center;
  justify-content: center;
  padding: 8px 10px;
  font-size: 0.78rem;
}
.reader-action-btn.is-active {
  border-color: color-mix(in srgb, var(--focus) 50%, var(--line));
  color: var(--focus);
  background: color-mix(in srgb, var(--focus) 8%, transparent);
}

.section-navigation {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: end;
  gap: 9px;
  width: min(720px, 100%);
  margin: 0 auto 13px;
}
.section-navigation .chapter-control {
  min-width: 0;
  width: 100%;
}
.section-jump-btn {
  min-width: 42px;
  min-height: 40px;
  padding: 8px 12px;
}

.seek-panel {
  width: min(760px, 100%);
  margin: 0 auto 12px;
  padding: 11px 13px 9px;
  border: 1px solid var(--line);
  border-radius: 13px;
  background: var(--panel-2);
}
.seek-labels {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 5px;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 720;
}
.seek-labels span {
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.seek-labels strong { color: var(--text); flex: 0 0 auto; }
.progress-range {
  width: 100%;
  height: 24px;
  margin: 0;
  cursor: pointer;
  accent-color: var(--accent);
}
.reader-toolbar {
  width: min(760px, 100%);
  margin: 0 auto;
}
.reader-toolbar .reader-meta { width: 100%; }

/* Slide-over reader panels */
.panel-backdrop {
  position: fixed;
  inset: 0;
  z-index: 80;
  background: rgba(5, 8, 12, 0.38);
  backdrop-filter: blur(2px);
}
.panel-backdrop[hidden], .reader-sheet[hidden] { display: none !important; }
.reader-sheet {
  position: fixed;
  z-index: 90;
  top: 0;
  right: 0;
  width: min(420px, 94vw);
  height: 100dvh;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-left: 1px solid var(--line);
  box-shadow: -24px 0 60px rgba(15, 23, 42, 0.20);
  padding-top: env(safe-area-inset-top, 0px);
  animation: sheetIn 150ms ease-out;
}
@keyframes sheetIn {
  from { transform: translateX(18px); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}
body.sheet-open { overflow: hidden; }
.sheet-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 20px 20px 14px;
  border-bottom: 1px solid var(--line);
}
.sheet-header h2 { margin: 0; font-size: 1.3rem; }
.sheet-header .eyebrow { margin-bottom: 2px; }
.sheet-close { flex: 0 0 auto; font-size: 1.3rem; }
.sheet-body {
  flex: 1;
  overflow: auto;
  padding: 18px 20px calc(24px + env(safe-area-inset-bottom, 0px));
}
.settings-sheet-body { display: grid; align-content: start; gap: 20px; }
.sheet-field > span {
  display: block;
  margin-bottom: 8px;
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 750;
}
.sheet-field .theme-select { width: 100%; background: var(--panel-2); }
.settings-toggles {
  display: grid;
  gap: 13px;
  padding-top: 17px;
  border-top: 1px solid var(--line);
}
.sheet-help { margin: 0; color: var(--muted); font-size: 0.78rem; line-height: 1.5; }
.sheet-search-input {
  width: 100%;
  min-height: 46px;
  padding: 11px 13px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--panel-2);
  color: var(--text);
  outline: none;
}
.sheet-search-input:focus { border-color: var(--muted); }
.sheet-summary { margin: 11px 2px 13px; color: var(--muted); font-size: 0.76rem; font-weight: 700; }
.search-results, .bookmarks-list { display: grid; gap: 9px; }
.search-result {
  width: 100%;
  display: grid;
  gap: 5px;
  padding: 12px 13px;
  text-align: left;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--panel-2);
  color: var(--text);
}
.search-result strong { font-size: 0.8rem; }
.search-result span { color: var(--muted); font-size: 0.8rem; line-height: 1.45; }
.search-result:hover { border-color: var(--muted); }
.sheet-empty {
  min-height: 180px;
  display: grid;
  place-content: center;
  gap: 6px;
  text-align: center;
  color: var(--muted);
}
.sheet-empty strong { color: var(--text); }
.bookmark-item {
  position: relative;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  border: 1px solid var(--line);
  border-radius: 13px;
  background: var(--panel-2);
  overflow: hidden;
}
.bookmark-open {
  min-width: 0;
  padding: 13px;
  border: 0;
  background: transparent;
  color: var(--text);
  text-align: left;
}
.bookmark-topline { display: flex; gap: 10px; justify-content: space-between; margin-bottom: 6px; }
.bookmark-topline strong { min-width: 0; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; font-size: 0.82rem; }
.bookmark-topline span { color: var(--muted); font-size: 0.72rem; font-weight: 800; }
.bookmark-open p { margin: 0 0 7px; color: var(--muted); font-size: 0.78rem; line-height: 1.42; }
.bookmark-open small { color: var(--muted); font-size: 0.68rem; }
.bookmark-delete {
  width: 42px;
  border: 0;
  border-left: 1px solid var(--line);
  background: transparent;
  color: var(--muted);
  font-size: 1.2rem;
}
.bookmark-delete:hover { color: #dc2626; background: color-mix(in srgb, #dc2626 6%, transparent); }

/* Settings were moved out of the reading surface and into the drawer. */
.word-reader-panel > .settings-grid,
.word-reader-panel > .toggle-row { display: none !important; }

/* PWA install affordance */
#installAppBtn[hidden] { display: none !important; }

/* Mobile-first reader refinements */
@media (max-width: 760px) {
  body { overscroll-behavior-y: contain; }
  button, select, input { touch-action: manipulation; }
  .app-shell {
    width: 100%;
    padding: max(10px, env(safe-area-inset-top, 0px)) 10px calc(28px + env(safe-area-inset-bottom, 0px));
  }
  .topbar {
    gap: 10px;
    align-items: flex-start;
  }
  .topbar-actions {
    gap: 6px;
    flex-wrap: wrap;
    justify-content: flex-end;
  }
  .topbar-actions .theme-select { min-height: 38px; padding-top: 6px; padding-bottom: 6px; }
  #installAppBtn { padding: 8px 9px; font-size: 0.75rem; }

  .reader-page-header {
    grid-template-columns: auto minmax(0, 1fr);
    gap: 8px;
    padding: 6px 0 9px;
    margin-bottom: 8px;
  }
  .reader-book-heading { text-align: left; }
  .reader-header-actions {
    grid-column: 1 / -1;
    display: grid;
    grid-template-columns: repeat(5, minmax(0, 1fr));
    width: 100%;
    gap: 5px;
  }
  .reader-action-btn {
    min-width: 0;
    min-height: 43px;
    padding: 7px 4px;
    font-size: 1rem;
  }
  .reader-action-btn .action-label { display: none; }
  .desktop-label { display: none; }
  .back-library-btn { min-width: 42px; min-height: 40px; font-size: 1rem; }

  .reader-card {
    padding: 14px 11px 16px;
    border-radius: 17px;
  }
  .reading-mode-switch { margin-bottom: 11px; }
  .section-navigation { gap: 6px; margin-bottom: 9px; }
  .chapter-control > span { display: none; }
  .chapter-control select { min-height: 42px; font-size: 0.8rem; }
  .section-jump-btn { min-height: 42px; min-width: 42px; }
  .seek-panel { padding: 8px 10px 6px; margin-bottom: 8px; }
  .seek-labels { font-size: 0.66rem; }
  .reader-meta { font-size: 0.74rem; }
  .word-stage { min-height: min(42vh, 340px); }
  .word-display { font-size: clamp(44px, 14vw, 68px); }
  .main-controls {
    position: sticky;
    bottom: max(5px, env(safe-area-inset-bottom, 0px));
    z-index: 8;
    margin: 15px -3px 0;
    padding: 8px;
    border: 1px solid var(--line);
    border-radius: 999px;
    background: color-mix(in srgb, var(--panel) 90%, transparent);
    backdrop-filter: blur(12px);
    box-shadow: 0 12px 30px rgba(15,23,42,.12);
  }
  .round-btn { width: 46px; height: 46px; }
  .play-btn { width: 60px; height: 60px; }
  .reader-stats { display: none; }

  .normal-reader-panel { margin-top: 7px; }
  .normal-reader-topline { padding: 2px 4px 9px; }
  .normal-reader-scroll {
    height: calc(100dvh - 300px);
    min-height: 330px;
    padding: 24px 10px 70px;
    margin: 0;
    scrollbar-gutter: auto;
  }
  .normal-reader-text { line-height: 1.7; }

  .reader-sheet {
    top: auto;
    bottom: 0;
    width: 100%;
    height: min(82dvh, 720px);
    border-left: 0;
    border-top: 1px solid var(--line);
    border-radius: 20px 20px 0 0;
    padding-bottom: env(safe-area-inset-bottom, 0px);
    box-shadow: 0 -18px 55px rgba(15,23,42,.23);
    animation: sheetUp 160ms ease-out;
  }
  @keyframes sheetUp {
    from { transform: translateY(22px); opacity: 0; }
    to { transform: translateY(0); opacity: 1; }
  }
  .sheet-header { padding: 15px 16px 11px; }
  .sheet-body { padding: 14px 16px calc(18px + env(safe-area-inset-bottom, 0px)); }
}

@media (max-width: 420px) {
  .library-grid { gap: 8px; }
  .book-copy { padding: 9px 8px 6px; }
  .book-actions { padding: 4px 7px 8px; gap: 4px; }
  .book-action { font-size: 0.64rem; }
  .main-controls { gap: 5px; }
  .round-btn { width: 42px; height: 42px; }
  .play-btn { width: 56px; height: 56px; }
}

/* In distraction-free Focus View, hide navigation and app panels as well. */
body.focus-view .section-navigation,
body.focus-view .seek-panel,
body.focus-view .reader-header-actions { display: none !important; }

@media (max-width: 760px) {
  body.focus-view .main-controls {
    position: static;
    border: 0;
    background: transparent;
    box-shadow: none;
    backdrop-filter: none;
    padding-left: 0;
    padding-right: 0;
  }
}

/* Release candidate: accessibility + import feedback */
.skip-link {
  position: fixed;
  left: 12px;
  top: 12px;
  z-index: 9999;
  transform: translateY(-180%);
  padding: 10px 14px;
  border-radius: 10px;
  background: var(--text);
  color: var(--panel);
  font-weight: 800;
}
.skip-link:focus { transform: translateY(0); }

:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 3px;
}

.import-progress {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px 12px;
  align-items: center;
  margin-top: 12px;
}
.import-progress[hidden] { display: none; }
.import-progress-track {
  height: 9px;
  overflow: hidden;
  border-radius: 999px;
  background: var(--border);
}
.import-progress-bar {
  width: 0;
  height: 100%;
  border-radius: inherit;
  background: var(--accent);
  transition: width 140ms ease;
}
.import-progress-text {
  color: var(--muted);
  font-size: 0.88rem;
  font-weight: 700;
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    scroll-behavior: auto !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* Mobile Focus View: keep the RSVP word visually central while anchoring
   playback controls near the bottom edge of the phone. The progress bar sits
   just above the controls. */
@media (max-width: 760px) {
  body.focus-view .reader-card {
    grid-template-rows: auto minmax(0, 1fr);
    padding: max(10px, env(safe-area-inset-top, 0px)) 12px 0;
  }

  body.focus-view #wordReaderPanel {
    display: block !important;
    position: relative;
    min-height: 0;
    width: 100%;
    height: 100%;
    padding: 0 0 calc(112px + env(safe-area-inset-bottom, 0px));
  }

  body.focus-view .word-stage {
    position: absolute;
    inset: 0 0 calc(112px + env(safe-area-inset-bottom, 0px));
    width: 100%;
    height: auto;
    min-height: 0;
  }

  body.focus-view .progress-track {
    position: absolute;
    left: 50%;
    bottom: calc(88px + env(safe-area-inset-bottom, 0px));
    width: min(88vw, 560px);
    margin: 0;
    transform: translateX(-50%);
  }

  body.focus-view .main-controls {
    position: absolute;
    left: 0;
    right: 0;
    bottom: max(12px, env(safe-area-inset-bottom, 0px));
    margin: 0;
    padding: 0;
  }
}

@media (max-height: 650px) and (max-width: 760px) {
  body.focus-view #wordReaderPanel {
    padding-bottom: calc(98px + env(safe-area-inset-bottom, 0px));
  }

  body.focus-view .word-stage {
    bottom: calc(98px + env(safe-area-inset-bottom, 0px));
  }

  body.focus-view .progress-track {
    bottom: calc(76px + env(safe-area-inset-bottom, 0px));
  }

  body.focus-view .main-controls {
    bottom: max(8px, env(safe-area-inset-bottom, 0px));
  }
}


/* Mobile browser Focus View fix: size the reader against the *visible* viewport
   (address/tab bars included) rather than the larger 100vh layout viewport.
   The Focus/Fullscreen actions float over the reader so they do not consume a
   grid row or push the word/control layout downward. */
@media (max-width: 760px) {
  body.focus-view {
    height: var(--focus-visible-height, 100dvh);
    min-height: var(--focus-visible-height, 100dvh);
  }

  body.focus-view .app-shell,
  body.focus-view #readerView,
  body.focus-view .reader-page,
  body.focus-view .reader-card {
    height: var(--focus-visible-height, 100dvh);
    min-height: var(--focus-visible-height, 100dvh);
    max-height: var(--focus-visible-height, 100dvh);
  }

  body.focus-view .reader-card {
    position: relative;
    grid-template-rows: minmax(0, 1fr);
    padding: 0;
    overflow: hidden;
  }

  body.focus-view .reader-view-actions {
    position: absolute;
    z-index: 8;
    top: max(8px, env(safe-area-inset-top, 0px));
    left: 8px;
    right: 8px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin: 0;
    padding: 0;
    pointer-events: none;
  }

  body.focus-view .reader-view-actions > button {
    pointer-events: auto;
    min-height: 38px;
    padding: 7px 10px;
    background: color-mix(in srgb, var(--bg) 88%, transparent);
    backdrop-filter: blur(10px);
  }

  body.focus-view #wordReaderPanel {
    height: 100%;
    min-height: 0;
    padding-bottom: calc(104px + env(safe-area-inset-bottom, 0px));
  }

  body.focus-view .word-stage {
    inset: 0 0 calc(104px + env(safe-area-inset-bottom, 0px));
  }

  body.focus-view .progress-track {
    bottom: calc(80px + env(safe-area-inset-bottom, 0px));
  }

  body.focus-view .main-controls {
    bottom: max(8px, env(safe-area-inset-bottom, 0px));
  }
}
