@import url('https://fonts.googleapis.com/css2?family=Lora:ital,wght@0,400;0,600;1,400&family=Inter:wght@400;500;600;700&display=swap');

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

:root {
  --bg: #faf8f5;
  --surface: #ffffff;
  --text: #1a1a1a;
  --text-secondary: #6b7280;
  --text-tertiary: #9ca3af;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --accent-light: rgba(37,99,235,0.12);
  --highlight-word: rgba(37,99,235,0.55);
  --border: #e5e7eb;
  --border-light: #f3f4f6;
  --danger: #ef4444;
  --danger-hover: #dc2626;
  --success: #10b981;
  --radius: 14px;
  --radius-sm: 8px;
  --radius-xs: 5px;
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.06);
  --shadow-md: 0 4px 16px rgba(0,0,0,0.08);
  --shadow-lg: 0 8px 30px rgba(0,0,0,0.12);
  --font-reader: 'Lora', Georgia, serif;
  --font-ui: 'Inter', system-ui, -apple-system, sans-serif;
  --reader-font-size: 1.1rem;
  --reader-line-height: 1.8;
  --reader-max-width: 65ch;
  --tts-bar-height: 140px;
  --header-height: 56px;
  color-scheme: light;
}

[data-theme="dark"] {
  --bg: #0f0f0f;
  --surface: #1a1a1a;
  --text: #e8e6e1;
  --text-secondary: #9ca3af;
  --text-tertiary: #6b7280;
  --accent: #60a5fa;
  --accent-hover: #93bbfd;
  --accent-light: rgba(96,165,250,0.15);
  --highlight-word: rgba(96,165,250,0.5);
  --border: #2d2d2d;
  --border-light: #222;
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.3);
  --shadow-md: 0 4px 16px rgba(0,0,0,0.4);
  --shadow-lg: 0 8px 30px rgba(0,0,0,0.5);
  color-scheme: dark;
}

[data-theme="sepia"] {
  --bg: #f4ede3;
  --surface: #ede3d4;
  --text: #3d2b1f;
  --text-secondary: #6b5a4e;
  --text-tertiary: #8a7b6e;
  --accent: #8b5e3c;
  --accent-hover: #6d4930;
  --accent-light: rgba(139,94,60,0.15);
  --highlight-word: rgba(139,94,60,0.45);
  --border: #d4c4b0;
  --border-light: #e0d5c5;
}

html { font-size: 16px; -webkit-text-size-adjust: 100%; }
body {
  font-family: var(--font-ui);
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  min-height: 100dvh;
  overflow-x: hidden;
  transition: background 0.3s, color 0.3s;
}

/* ─── Utility ───────────────────────────── */
.hidden { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0,0,0,0); border: 0; }

/* ─── View containers ───────────────────── */
.view { display: none; min-height: 100vh; min-height: 100dvh; }
.view.active { display: flex; flex-direction: column; height: 100vh; height: 100dvh; overflow: hidden; }

/* ─── LIBRARY VIEW ──────────────────────── */
.library-header {
  position: sticky; top: 0; z-index: 100;
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
}
.library-header h1 {
  font-size: 1.35rem; font-weight: 700;
  background: linear-gradient(135deg, var(--accent), #7c3aed);
  -webkit-background-clip: text; -webkit-text-fill-color: transparent;
  background-clip: text;
}
.library-header .storage-badge {
  font-size: 0.7rem; color: var(--text-tertiary);
  padding: 3px 8px; background: var(--border-light);
  border-radius: 20px; margin-left: 10px;
}
.btn-add {
  width: 44px; height: 44px; border-radius: 50%;
  background: var(--accent); color: #fff; border: none;
  font-size: 1.5rem; cursor: pointer; display: flex;
  align-items: center; justify-content: center;
  box-shadow: var(--shadow-md);
  transition: transform 0.2s, background 0.2s;
}
.btn-add:hover { background: var(--accent-hover); transform: scale(1.08); }
.btn-add:active { transform: scale(0.95); }

.library-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 16px; padding: 20px;
  flex: 1;
}
@media(min-width:600px){ .library-grid { grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); } }
@media(min-width:900px){ .library-grid { grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 20px; } }

/* Book card */
.book-card {
  background: var(--surface);
  border-radius: var(--radius);
  overflow: hidden; cursor: pointer;
  border: 1px solid var(--border);
  transition: transform 0.2s, box-shadow 0.2s;
  position: relative;
}
.book-card:hover { transform: translateY(-3px); box-shadow: var(--shadow-md); }
.book-card:active { transform: translateY(0); }
.book-cover {
  aspect-ratio: 3/4; display: flex;
  align-items: center; justify-content: center;
  font-size: 3rem; font-weight: 700; color: #fff;
  font-family: var(--font-reader);
  position: relative; overflow: hidden;
}
.book-cover::after {
  content: ''; position: absolute; inset: 0;
  background: linear-gradient(180deg, transparent 50%, rgba(0,0,0,0.3));
}
.book-info {
  padding: 10px 12px;
}
.book-title {
  font-size: 0.82rem; font-weight: 600;
  line-height: 1.3; display: -webkit-box;
  -webkit-line-clamp: 2; -webkit-box-orient: vertical;
  overflow: hidden;
}
.book-meta {
  display: flex; align-items: center; gap: 6px;
  margin-top: 5px; font-size: 0.68rem;
  color: var(--text-tertiary);
}
.book-progress-bar {
  height: 3px; background: var(--border-light);
  border-radius: 2px; margin-top: 8px; overflow: hidden;
}
.book-progress-bar .fill {
  height: 100%; background: var(--accent);
  border-radius: 2px; transition: width 0.3s;
}
.book-menu-btn {
  position: absolute; top: 6px; right: 6px; z-index: 2;
  width: 32px; height: 32px; border-radius: 50%;
  background: rgba(0,0,0,0.4); border: none;
  color: #fff; cursor: pointer; font-size: 1rem;
  display: flex; align-items: center; justify-content: center;
  opacity: 0; transition: opacity 0.2s;
}
.book-card:hover .book-menu-btn { opacity: 1; }
.book-menu {
  position: absolute; top: 40px; right: 6px; z-index: 10;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius-sm); box-shadow: var(--shadow-lg);
  min-width: 140px; overflow: hidden;
}
.book-menu button {
  display: block; width: 100%; padding: 10px 14px;
  border: none; background: none; text-align: left;
  font-size: 0.82rem; color: var(--text); cursor: pointer;
  font-family: var(--font-ui);
}
.book-menu button:hover { background: var(--accent-light); }
.book-menu .delete-btn { color: var(--danger); }
.book-menu .delete-btn:hover { background: rgba(239,68,68,0.1); }

/* Empty state */
.empty-state {
  flex: 1; display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  padding: 40px 20px; text-align: center;
}
.empty-state svg { width: 120px; height: 120px; margin-bottom: 24px; opacity: 0.6; }
.empty-state h2 { font-size: 1.2rem; margin-bottom: 8px; }
.empty-state p { color: var(--text-secondary); font-size: 0.9rem; max-width: 280px; margin-bottom: 20px; }
.btn-primary {
  padding: 12px 28px; border-radius: 50px;
  background: var(--accent); color: #fff; border: none;
  font-size: 0.95rem; font-weight: 600; cursor: pointer;
  font-family: var(--font-ui);
  transition: background 0.2s, transform 0.15s;
}
.btn-primary:hover { background: var(--accent-hover); }
.btn-primary:active { transform: scale(0.97); }

/* Drop zone overlay */
.drop-overlay {
  position: fixed; inset: 0; z-index: 999;
  background: rgba(37,99,235,0.1);
  backdrop-filter: blur(4px);
  display: none; align-items: center; justify-content: center;
  border: 3px dashed var(--accent);
}
.drop-overlay.active { display: flex; }
.drop-overlay-inner {
  text-align: center; color: var(--accent);
}
.drop-overlay-inner svg { width: 64px; height: 64px; margin-bottom: 12px; }
.drop-overlay-inner p { font-size: 1.1rem; font-weight: 600; }

/* Chrome banner */
.chrome-banner {
  background: linear-gradient(90deg, #fef3c7, #fde68a);
  color: #92400e; padding: 10px 20px;
  font-size: 0.8rem; text-align: center;
  display: flex; align-items: center; justify-content: center; gap: 8px;
}
.chrome-banner a { color: #92400e; font-weight: 600; }

/* ─── READER VIEW ───────────────────────── */
.reader-header {
  position: sticky; top: 0; z-index: 100;
  display: flex; align-items: center; gap: 8px;
  padding: 8px 12px; height: var(--header-height);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  backdrop-filter: blur(12px);
}
.btn-back {
  width: 40px; height: 40px; border-radius: 50%;
  background: none; border: none; color: var(--text);
  font-size: 1.2rem; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
.btn-back:hover { background: var(--accent-light); }
.reader-title {
  flex: 1; font-size: 0.9rem; font-weight: 600;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.reader-actions { display: flex; gap: 4px; }
.reader-actions button {
  width: 36px; height: 36px; border-radius: 50%;
  background: none; border: none; color: var(--text-secondary);
  font-size: 1.1rem; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
.reader-actions button:hover { background: var(--accent-light); color: var(--text); }

.reader-body { flex: 1; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.reader-content {
  font-family: var(--font-reader);
  font-size: var(--reader-font-size);
  line-height: var(--reader-line-height);
  max-width: var(--reader-max-width);
  margin: 0 auto;
  padding: 24px 20px calc(var(--tts-bar-height) + 24px);
  color: var(--text);
  word-wrap: break-word;
  overflow-wrap: break-word;
}
[data-width="narrow"] .reader-content { max-width: 50ch; }
[data-width="wide"] .reader-content { max-width: 80ch; }
[data-font="sans"] .reader-content { font-family: var(--font-ui); }
[data-font="mono"] .reader-content { font-family: 'Courier New', Courier, monospace; }

.reader-content p { margin-bottom: 1.2em; }
.reader-content h2 { font-size: 1.4em; margin: 2em 0 0.6em; font-weight: 700; }
.reader-content h3 { font-size: 1.2em; margin: 1.6em 0 0.5em; font-weight: 600; }
.reader-content li { margin-left: 1.5em; margin-bottom: 0.5em; list-style: disc; }
.reader-content blockquote { border-left: 3px solid var(--accent); padding-left: 1em; margin: 1em 0; color: var(--text-secondary); font-style: italic; }
.reader-content [data-chunk-index] { cursor: pointer; border-radius: 4px; }
.reader-content [data-chunk-index]:hover { background: var(--accent-light); }

/* TTS highlights */
.chunk-active {
  background: var(--accent-light);
  border-radius: 4px;
  transition: background 0.2s;
}
.word-active {
  background: var(--highlight-word);
  color: #fff;
  border-radius: 3px;
  padding: 1px 3px;
  margin: -1px -3px;
}
@media(prefers-reduced-motion: reduce) {
  .chunk-active, .word-active { transition: none; }
}

/* Back to position button */
.btn-return-to-pos, .btn-goto-bookmark {
  position: absolute;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background-color: var(--accent);
  color: white;
  border: none;
  border-radius: 20px;
  padding: 8px 16px;
  font-size: 0.85rem;
  font-weight: 500;
  box-shadow: 0 4px 12px rgba(0,0,0,0.2);
  cursor: pointer;
  opacity: 0;
  pointer-events: none;
  transition: all 0.3s ease;
  z-index: 10;
}

.btn-return-to-pos.visible {
  opacity: 1;
  pointer-events: auto;
  transform: translateX(-50%) translateY(0);
}

.btn-goto-bookmark {
  bottom: 120px;
  background-color: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  opacity: 1;
  pointer-events: auto;
  transform: translateX(-50%) translateY(0);
}

.btn-goto-bookmark.hidden {
  display: none;
}

.btn-goto-bookmark:hover {
  background-color: var(--bg);
}

#btn-bookmark {
  background: none;
  border: none;
  font-size: 1.2rem;
  cursor: pointer;
  opacity: 0.7;
  transition: opacity 0.2s;
  padding: 4px;
}
#btn-bookmark:hover {
  opacity: 1;
}

/* ─── TTS BAR ───────────────────────────── */
.tts-bar {
  position: fixed; bottom: 0; left: 0; right: 0;
  z-index: 200;
  background: var(--surface);
  border-top: 1px solid var(--border);
  box-shadow: 0 -2px 20px rgba(0,0,0,0.08);
  padding: 10px 16px 14px;
  display: flex; flex-direction: column; gap: 8px;
}
.tts-progress {
  display: flex; align-items: center; gap: 10px;
  font-size: 0.7rem; color: var(--text-tertiary);
}
.tts-progress input[type="range"] {
  flex: 1; height: 4px; -webkit-appearance: none;
  background: var(--border); border-radius: 2px; outline: none;
}
.tts-progress input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none; width: 14px; height: 14px;
  border-radius: 50%; background: var(--accent); cursor: pointer;
  border: 2px solid var(--surface);
}
.tts-controls {
  display: flex; align-items: center; justify-content: space-between;
  gap: 6px;
}
.tts-main-controls {
  display: flex; align-items: center; gap: 4px;
}
.tts-btn {
  width: 42px; height: 42px; border-radius: 50%;
  background: none; border: none; color: var(--text);
  font-size: 1.1rem; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: background 0.15s;
}
.tts-btn:hover { background: var(--accent-light); }
.tts-btn-play {
  width: 52px; height: 52px;
  background: var(--accent); color: #fff;
  font-size: 1.3rem; box-shadow: var(--shadow-sm);
}
.tts-btn-play:hover { background: var(--accent-hover); }
.tts-secondary-controls {
  display: flex; align-items: center; gap: 4px;
}
.tts-speed-btn {
  padding: 4px 10px; border-radius: 20px;
  background: var(--border-light); border: 1px solid var(--border);
  font-size: 0.72rem; font-weight: 600;
  color: var(--text-secondary); cursor: pointer;
  font-family: var(--font-ui);
}
.tts-speed-btn.active { background: var(--accent-light); color: var(--accent); border-color: var(--accent); }
.tts-voice-select {
  padding: 4px 8px; border-radius: var(--radius-xs);
  border: 1px solid var(--border); background: var(--surface);
  color: var(--text); font-size: 0.72rem;
  font-family: var(--font-ui); max-width: 140px;
  cursor: pointer;
}
.tts-status {
  font-size: 0.68rem; color: var(--text-tertiary);
  text-align: center; padding-top: 2px;
}

/* ─── SETTINGS PANEL ────────────────────── */
.settings-overlay {
  position: fixed; inset: 0; z-index: 300;
  background: rgba(0,0,0,0.4);
  display: none; align-items: flex-end; justify-content: center;
}
.settings-overlay.active { display: flex; }
.settings-panel {
  background: var(--surface);
  border-radius: var(--radius) var(--radius) 0 0;
  width: 100%; max-width: 480px;
  padding: 20px; max-height: 70vh; overflow-y: auto;
  animation: slideUp 0.3s ease;
}
@keyframes slideUp { from { transform: translateY(100%); } to { transform: translateY(0); } }
.settings-panel h3 {
  font-size: 1rem; margin-bottom: 16px;
  display: flex; align-items: center; justify-content: space-between;
}
.settings-panel h3 button {
  background: none; border: none; color: var(--text-secondary);
  font-size: 1.2rem; cursor: pointer;
}
.setting-group { margin-bottom: 18px; }
.setting-label { font-size: 0.78rem; color: var(--text-secondary); margin-bottom: 8px; font-weight: 500; }
.setting-options { display: flex; gap: 8px; flex-wrap: wrap; }
.setting-opt {
  padding: 8px 16px; border-radius: var(--radius-sm);
  border: 1px solid var(--border); background: var(--surface);
  color: var(--text); font-size: 0.82rem; cursor: pointer;
  font-family: var(--font-ui); transition: all 0.15s;
}
.setting-opt.active { background: var(--accent-light); border-color: var(--accent); color: var(--accent); font-weight: 600; }
.setting-opt:hover { border-color: var(--accent); }
.font-size-control { display: flex; align-items: center; gap: 12px; }
.font-size-control button {
  width: 36px; height: 36px; border-radius: 50%;
  border: 1px solid var(--border); background: var(--surface);
  color: var(--text); font-size: 1rem; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
.font-size-control button:hover { background: var(--accent-light); }
.font-size-control span { font-size: 0.85rem; font-weight: 600; min-width: 50px; text-align: center; }

/* ─── UPLOAD MODAL ──────────────────────── */
.modal-overlay {
  position: fixed; inset: 0; z-index: 400;
  background: rgba(0,0,0,0.5);
  display: none; align-items: center; justify-content: center;
  padding: 20px;
}
.modal-overlay.active { display: flex; }
.modal {
  background: var(--surface);
  border-radius: var(--radius);
  width: 100%; max-width: 400px;
  padding: 28px 24px;
  box-shadow: var(--shadow-lg);
  animation: modalIn 0.25s ease;
}
@keyframes modalIn { from { transform: scale(0.95); opacity: 0; } to { transform: scale(1); opacity: 1; } }
.modal h3 { font-size: 1.05rem; margin-bottom: 16px; }
.progress-steps { display: flex; flex-direction: column; gap: 10px; }
.progress-step {
  display: flex; align-items: center; gap: 10px;
  font-size: 0.85rem; color: var(--text-secondary);
}
.progress-step.active { color: var(--accent); font-weight: 500; }
.progress-step.done { color: var(--success); }
.progress-step .icon { width: 20px; text-align: center; }
.progress-step.active .icon::after { content: '⏳'; }
.progress-step.done .icon::after { content: '✓'; }
.progress-step.pending .icon::after { content: '○'; }
.progress-step.error .icon::after { content: '✗'; color: var(--danger); }
.modal-actions { display: flex; gap: 10px; margin-top: 20px; justify-content: flex-end; }
.btn-secondary {
  padding: 10px 20px; border-radius: 50px;
  background: var(--border-light); color: var(--text);
  border: none; font-size: 0.85rem; cursor: pointer;
  font-family: var(--font-ui);
}
.btn-secondary:hover { background: var(--border); }

/* Confirm dialog */
.confirm-content { font-size: 0.9rem; margin-bottom: 16px; line-height: 1.5; }
.btn-danger {
  padding: 10px 20px; border-radius: 50px;
  background: var(--danger); color: #fff; border: none;
  font-size: 0.85rem; cursor: pointer; font-family: var(--font-ui);
}
.btn-danger:hover { background: var(--danger-hover); }

/* ─── FILE INPUT ────────────────────────── */
.file-input { display: none; }

/* ─── FOCUS / A11Y ──────────────────────── */
:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
button, select, input { touch-action: manipulation; }

/* ─── SCROLLBAR ─────────────────────────── */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-tertiary); }
