/* ── Light theme (default) ── */
:root {
  --font-family: Georgia, 'Times New Roman', serif;
  --font-size: 18px;
  --line-height: 1.7;
  --bg: #fafaf8;
  --text: #1a1a1a;
  --accent: #4a7fc1;
  --surface: #ffffff;
  --border: #e0ddd8;
  --muted: #888888;
  --scroll-speed: 1;
  --theme-transition: background-color 0.25s ease, color 0.25s ease, border-color 0.25s ease;
}

/* ── Dark theme tokens (system preference) ── */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1c1814;
    --text: #e8dfd0;
    --accent: #7aaee8;
    --surface: #242019;
    --border: #3a322b;
    --muted: #8c7f72;
  }
}

/* ── Dark theme tokens (manual override) ── */
:root[data-theme="dark"] {
  --bg: #1c1814;
  --text: #e8dfd0;
  --accent: #7aaee8;
  --surface: #242019;
  --border: #3a322b;
  --muted: #8c7f72;
}

*, *::before, *::after {
  box-sizing: border-box;
}

html {
  transition: var(--theme-transition);
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-family);
  font-size: var(--font-size);
  -webkit-text-size-adjust: 100%;
  touch-action: manipulation;
}

/* ── Screen management ── */

.screen {
  position: fixed;
  inset: 0;
  display: none;
  flex-direction: column;
}

.screen.active {
  display: flex;
}

/* ── Library screen ── */

#library-screen {
  background: var(--bg);
}

#library-screen header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  padding-top: max(16px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

#library-screen header h1 {
  margin: 0;
  font-size: 20px;
  font-weight: 600;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}

#add-book-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: 15px;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-weight: 500;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

#add-book-btn:active {
  opacity: 0.8;
}

#theme-toggle-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  padding: 0;
  background: none;
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--muted);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: color 0.2s ease, border-color 0.2s ease;
}

#theme-toggle-btn:active {
  opacity: 0.6;
}

#library-body {
  display: flex;
  flex-direction: column;
  flex: 1;
  overflow: hidden;
}

#library-list {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 0;
  margin: 0;
  list-style: none;
}

#library-empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: 1;
  gap: 12px;
  color: var(--muted);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  text-align: center;
  padding: 40px 24px;
}

#library-empty svg {
  opacity: 0.3;
}

#library-empty p {
  margin: 0;
  font-size: 16px;
}

/* ── Reader screen ── */

#reader-screen {
  background: var(--bg);
  overflow: hidden;
}

#reader-back-btn {
  position: absolute;
  top: max(12px, env(safe-area-inset-top));
  left: 12px;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  padding: 0;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border: none;
  border-radius: 50%;
  color: rgba(255, 255, 255, 0.9);
  cursor: pointer;
  opacity: 0.8;
  -webkit-tap-highlight-color: transparent;
}

#reader-back-btn:active {
  opacity: 1;
}

#speech-indicator {
  position: absolute;
  top: max(12px, env(safe-area-inset-top));
  right: 12px;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  padding: 0;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border: none;
  border-radius: 50%;
  color: rgba(255, 255, 255, 0.9);
  cursor: pointer;
  opacity: 0.8;
  -webkit-tap-highlight-color: transparent;
  transition: opacity 0.2s ease, background-color 0.2s ease;
}

#speech-indicator:active {
  opacity: 1;
}

#speech-indicator.speech-paused svg {
  color: rgba(255, 255, 255, 0.6);
}

#speech-indicator.speech-paused::after {
  content: '';
  position: absolute;
  width: 2px;
  height: 12px;
  background: rgba(255, 255, 255, 0.9);
  border-radius: 1px;
  transform: rotate(45deg);
  left: 50%;
  top: 50%;
  margin-left: -1px;
  margin-top: -6px;
}

#reader-content {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 20px 20px;
  padding-top: max(60px, calc(env(safe-area-inset-top) + 48px));
  padding-bottom: max(40px, env(safe-area-inset-bottom));
  line-height: var(--line-height);
  font-family: var(--font-family);
  font-size: var(--font-size);
  overscroll-behavior: none;
  touch-action: pan-y;
  -webkit-user-select: none;
  user-select: none;
}

/* Forces a GPU compositing layer so the browser doesn't repaint text on every scroll tick */
#reader-body {
  will-change: transform;
  transform: translateZ(0);
}

/* ── Reader typography ── */

#reader-body h1,
#reader-body h2,
#reader-body h3 {
  font-family: var(--font-family);
  font-weight: 700;
  line-height: 1.3;
  margin: 1.8em 0 0.5em;
  color: var(--text);
}

#reader-content h1 { font-size: 1.5em; }
#reader-content h2 { font-size: 1.25em; }
#reader-content h3 { font-size: 1.1em; }

#reader-body p {
  margin: 0 0 0.9em;
  color: var(--text);
}

#reader-body em     { font-style: italic; }
#reader-body strong { font-weight: 700; }

#reader-body img {
  display: block;
  max-width: 100%;
  height: auto;
  margin: 1.2em auto;
}

.speech-word {
  white-space: pre-wrap;
}

.speech-word-active {
  background: rgba(74, 127, 193, 0.18);
  border-radius: 6px;
}

#reader-body > *:first-child { margin-top: 0; }
#reader-body > *:last-child  { margin-bottom: 0; }

/* ── Find in book ── */

#find-bar {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: max(8px, env(safe-area-inset-top)) 8px 8px 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.08);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  transition: var(--theme-transition);
}

#find-input {
  flex: 1;
  min-width: 0;
  padding: 8px 12px;
  font-size: 16px; /* under 16px and iOS zooms the page when the field is focused */
  font-family: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1.5px solid var(--border);
  border-radius: 10px;
  outline: none;
  -webkit-appearance: none;
  appearance: none;
}

#find-input:focus {
  border-color: var(--accent);
}

#find-input::-webkit-search-cancel-button {
  -webkit-appearance: none;
}

#find-count {
  min-width: 3.5em;
  padding: 0 4px;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  color: var(--muted);
  text-align: center;
  white-space: nowrap;
}

#find-bar button {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  padding: 0;
  background: none;
  border: none;
  border-radius: 50%;
  color: var(--muted);
  font-size: 16px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

#find-bar button:active { opacity: 0.6; }
#find-bar button:disabled { opacity: 0.3; cursor: default; }

.find-match {
  background: rgba(240, 180, 40, 0.35);
  border-radius: 3px;
}

.find-current {
  background: var(--accent);
  color: #fff;
}

/* Prevent bounce scroll from interfering */
body.reading {
  overflow: hidden;
}

/* ── Hint toast ── */

#hint-toast {
  position: fixed;
  bottom: max(32px, env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%) translateY(6px);
  background: #1a1a1a;
  color: #ffffff;
  font-size: 15px;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  padding: 10px 20px;
  border-radius: 20px;
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.2s ease, transform 0.2s ease;
  z-index: 50;
}

#hint-toast.visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ── Compact autoscroll bar ── */

#autoscroll-bar {
  position: fixed;
  bottom: max(72px, calc(env(safe-area-inset-bottom) + 56px));
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 4px;
  background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-radius: 28px;
  padding: 6px 14px;
  z-index: 90;
}

#as-pause-btn,
#as-settings-btn {
  background: none;
  border: none;
  color: rgba(255, 255, 255, 0.92);
  cursor: pointer;
  padding: 6px 8px;
  display: flex;
  align-items: center;
  -webkit-tap-highlight-color: transparent;
}

/* ── Quick overlay menu ── */

#reader-menu {
  position: fixed;
  inset: 0;
  z-index: 150;
  display: flex;
  align-items: flex-end;
  pointer-events: none;
}

#reader-menu.open {
  pointer-events: auto;
}

#reader-menu-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  opacity: 0;
  transition: opacity 0.22s ease;
}

#reader-menu.open #reader-menu-backdrop {
  opacity: 1;
}

#reader-menu-panel {
  position: relative;
  width: 100%;
  background: var(--surface);
  border-radius: 16px 16px 0 0;
  padding: 10px 20px 14px;
  padding-bottom: max(14px, env(safe-area-inset-bottom));
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  transform: translateY(100%);
  transition: transform 0.25s cubic-bezier(0.32, 0.72, 0, 1);
}

#menu-handle {
  width: 36px;
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  margin: 0 auto 12px;
  cursor: grab;
  touch-action: none;
}

#reader-menu.open #reader-menu-panel {
  transform: translateY(0);
}

#menu-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}

#menu-header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

#menu-library-btn {
  display: flex;
  align-items: center;
  gap: 2px;
  height: 32px;
  padding: 0 12px 0 8px;
  background: var(--border);
  border: none;
  border-radius: 16px;
  font-size: 13px;
  font-family: inherit;
  font-weight: 600;
  color: var(--text);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

#reader-menu-close {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--border);
  border: none;
  border-radius: 50%;
  font-size: 14px;
  color: var(--muted);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

/* Icon-only header buttons share the close button's pill */
#menu-find-btn {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--border);
  border: none;
  border-radius: 50%;
  color: var(--muted);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

#menu-find-btn:active,
#reader-menu-close:active,
#menu-library-btn:active {
  opacity: 0.7;
}

/* ── Menu tabs ── */

#menu-tabs {
  display: flex;
  gap: 4px;
  padding: 3px;
  background: var(--bg);
  border-radius: 12px;
  margin-bottom: 4px;
}

.menu-tab {
  flex: 1;
  position: relative;
  padding: 8px 4px;
  background: none;
  border: none;
  border-radius: 9px;
  font-size: 14px;
  font-family: inherit;
  font-weight: 500;
  color: var(--muted);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.15s ease, color 0.15s ease;
}

.menu-tab.active {
  background: var(--surface);
  color: var(--text);
  font-weight: 600;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12);
}

/* A mode that's running gets a dot on its tab, so you can tell from any tab */
.menu-tab.on::after {
  content: '';
  position: absolute;
  top: 7px;
  right: 8px;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent);
}

#menu-body {
  max-height: 55vh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  /* Keep the slider thumbs and the list's first row clear of the tab bar */
  padding: 16px 2px 4px;
  margin: 0 -2px;
}

.menu-pane {
  display: none;
  flex-direction: column;
  gap: 22px;
}

.menu-pane.active {
  display: flex;
}

.pane-hint {
  margin: -6px 0 0;
  font-size: 13px;
  line-height: 1.4;
  color: var(--muted);
}

.setting-row {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.setting-row label,
.switch-row label {
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}

.setting-row input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
  height: 4px;
}

.switch-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.switch-row label {
  font-size: 15px;
  font-weight: 600;
  letter-spacing: 0;
  text-transform: none;
  color: var(--text);
  cursor: pointer;
}

.switch {
  appearance: none;
  -webkit-appearance: none;
  width: 46px;
  height: 28px;
  margin: 0;
  border-radius: 14px;
  background: var(--border);
  position: relative;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.2s ease;
}

.switch::after {
  content: '';
  position: absolute;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: #fff;
  top: 3px;
  left: 3px;
  transition: transform 0.2s ease;
  box-shadow: 0 1px 3px rgba(0,0,0,0.2);
}

.switch:checked {
  background: var(--accent);
}

.switch:checked::after {
  transform: translateX(18px);
}

.switch:disabled {
  opacity: 0.4;
  cursor: default;
}

/* ── Chapter list ── */

#chapter-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

#chapter-list li {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 8px;
  border-bottom: 1px solid var(--border);
  font-size: 15px;
  line-height: 1.35;
  color: var(--text);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

#chapter-list li:last-child {
  border-bottom: none;
}

#chapter-list li:active {
  background: var(--bg);
}

#chapter-list li.sub {
  padding-left: 24px;
  font-size: 14px;
  color: var(--muted);
}

#chapter-list li.current {
  color: var(--accent);
  font-weight: 600;
}

#chapter-list li.current::before {
  content: '';
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent);
  flex-shrink: 0;
  margin-left: -2px;
}

#font-selector {
  display: flex;
  gap: 8px;
}

.font-btn {
  flex: 1;
  padding: 10px 4px;
  background: var(--bg);
  color: var(--text);
  border: 1.5px solid var(--border);
  border-radius: 8px;
  font-size: 13px;
  font-family: inherit;
  font-weight: 500;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}

.font-btn.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

#tts-voice-select {
  width: 100%;
  background: var(--bg);
  color: var(--text);
  border: 1.5px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 15px;
  font-family: inherit;
  appearance: none;
  -webkit-appearance: none;
}

/* Highlight mode enables native text selection */
#reader-content.mode-highlight {
  -webkit-user-select: text;
  user-select: text;
}

/* ── Library list items ── */

#library-list li {
  display: flex;
  align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.15s ease;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}

#library-list li.deleting {
  background: rgba(220, 50, 50, 0.1);
}

.book-info {
  display: flex;
  flex-direction: column;
  gap: 3px;
  flex: 1;
  min-width: 0;
}

.book-title {
  font-size: 16px;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.book-author {
  font-size: 13px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.book-progress {
  margin-top: 8px;
  height: 3px;
  background: var(--border);
  border-radius: 2px;
  overflow: hidden;
}

.book-progress-fill {
  height: 100%;
  background: var(--accent);
  border-radius: 2px;
}

.book-date {
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
  padding-left: 12px;
  align-self: flex-start;
  margin-top: 2px;
}

.hidden {
  display: none !important;
}

/* ── Delete confirmation sheet ── */

#delete-overlay {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: flex;
  align-items: flex-end;
  pointer-events: none;
}

#delete-overlay.open {
  pointer-events: auto;
}

#delete-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  opacity: 0;
  transition: opacity 0.22s ease;
}

#delete-overlay.open #delete-backdrop {
  opacity: 1;
}

#delete-sheet {
  position: relative;
  width: 100%;
  background: var(--surface);
  border-radius: 16px 16px 0 0;
  padding: 28px 20px 16px;
  padding-bottom: max(16px, env(safe-area-inset-bottom));
  transform: translateY(100%);
  transition: transform 0.25s cubic-bezier(0.32, 0.72, 0, 1);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}

#delete-overlay.open #delete-sheet {
  transform: translateY(0);
}

#delete-sheet-msg {
  margin: 0 0 4px;
  font-size: 17px;
  font-weight: 600;
  color: var(--text);
}

#delete-sheet-sub {
  margin: 0 0 24px;
  font-size: 14px;
  color: var(--muted);
}

#delete-confirm-btn,
#delete-cancel-btn {
  display: block;
  width: 100%;
  padding: 14px;
  border: none;
  border-radius: 12px;
  font-size: 16px;
  font-family: inherit;
  font-weight: 500;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: opacity 0.15s ease;
}

#delete-confirm-btn:active,
#delete-cancel-btn:active {
  opacity: 0.7;
}

#delete-confirm-btn {
  background: #d9363e;
  color: #fff;
  margin-bottom: 10px;
}

#delete-cancel-btn {
  background: var(--border);
  color: var(--text);
}
