/* ============================================
   Book Reader Web App - Main Stylesheet
   ============================================ */

/* CSS Variables */
:root {
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --primary-light: #dbeafe;
  --secondary: #f97316;
  --secondary-dark: #ea580c;
  --accent: #10b981;
  --accent-dark: #059669;
  --bg: #f8fafc;
  --bg-card: #ffffff;
  --bg-sidebar: #1e293b;
  --text-primary: #0f172a;
  --text-secondary: #475569;
  --text-muted: #94a3b8;
  --text-inverse: #f8fafc;
  --border: #e2e8f0;
  --border-dark: #cbd5e1;
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.08);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.1);
  --shadow-lg: 0 10px 30px rgba(0,0,0,0.15);
  --radius-sm: 6px;
  --radius-md: 12px;
  --radius-lg: 20px;
  --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --font-serif: 'Georgia', 'Times New Roman', serif;
  --transition: 0.2s ease;
}

/* Dark Mode Variables */
body.dark {
  --bg: #0f172a;
  --bg-card: #1e293b;
  --bg-sidebar: #0f172a;
  --text-primary: #f1f5f9;
  --text-secondary: #94a3b8;
  --text-muted: #64748b;
  --border: #334155;
  --border-dark: #475569;
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.3);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.4);
  --shadow-lg: 0 10px 30px rgba(0,0,0,0.5);
}

/* Reset & Base */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html { font-size: 16px; scroll-behavior: smooth; }

body {
  font-family: var(--font-sans);
  background: var(--bg);
  color: var(--text-primary);
  min-height: 100vh;
  transition: background var(--transition), color var(--transition);
  line-height: 1.6;
}

/* ============================================
   LAYOUT
   ============================================ */

.app-wrapper {
  display: flex;
  min-height: 100vh;
}

/* Sidebar */
.sidebar {
  width: 260px;
  background: var(--bg-sidebar);
  color: var(--text-inverse);
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0; left: 0; bottom: 0;
  z-index: 100;
  transition: transform var(--transition);
  overflow-y: auto;
}

.sidebar-logo {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 24px 20px;
  border-bottom: 1px solid rgba(255,255,255,0.1);
}

.sidebar-logo .logo-icon {
  width: 40px; height: 40px;
  background: var(--primary);
  border-radius: var(--radius-sm);
  display: flex; align-items: center; justify-content: center;
  font-size: 20px;
}

.sidebar-logo .logo-text {
  font-size: 16px;
  font-weight: 700;
  letter-spacing: -0.3px;
}

.sidebar-logo .logo-sub {
  font-size: 11px;
  color: rgba(255,255,255,0.5);
}

.sidebar-nav {
  padding: 16px 12px;
  flex: 1;
}

.nav-section-title {
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 1.2px;
  text-transform: uppercase;
  color: rgba(255,255,255,0.35);
  padding: 12px 10px 6px;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  color: rgba(255,255,255,0.65);
  font-size: 14px;
  font-weight: 500;
  transition: all var(--transition);
  text-decoration: none;
  margin-bottom: 2px;
}

.nav-item:hover, .nav-item.active {
  background: rgba(255,255,255,0.1);
  color: #fff;
}

.nav-item.active {
  background: var(--primary);
  color: #fff;
}

.nav-item .nav-icon { font-size: 18px; width: 20px; text-align: center; }

.sidebar-footer {
  padding: 16px 12px;
  border-top: 1px solid rgba(255,255,255,0.1);
}

/* Main Content */
.main-content {
  flex: 1;
  margin-left: 260px;
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

/* Top Bar */
.topbar {
  background: var(--bg-card);
  border-bottom: 1px solid var(--border);
  padding: 0 28px;
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky;
  top: 0; z-index: 50;
  box-shadow: var(--shadow-sm);
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 16px;
}

.topbar-title {
  font-size: 20px;
  font-weight: 700;
  color: var(--text-primary);
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

.btn-icon {
  width: 38px; height: 38px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--bg-card);
  color: var(--text-secondary);
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  font-size: 18px;
  transition: all var(--transition);
}

.btn-icon:hover {
  background: var(--primary-light);
  color: var(--primary);
  border-color: var(--primary);
}

/* Page Container */
.page-container {
  flex: 1;
  padding: 28px;
  overflow-y: auto;
}

/* ============================================
   BUTTONS
   ============================================ */

.btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 10px 20px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  border: none;
  transition: all var(--transition);
  text-decoration: none;
  white-space: nowrap;
}

.btn-primary {
  background: var(--primary);
  color: #fff;
}
.btn-primary:hover { background: var(--primary-dark); transform: translateY(-1px); box-shadow: var(--shadow-md); }

.btn-secondary {
  background: var(--secondary);
  color: #fff;
}
.btn-secondary:hover { background: var(--secondary-dark); transform: translateY(-1px); }

.btn-accent {
  background: var(--accent);
  color: #fff;
}
.btn-accent:hover { background: var(--accent-dark); transform: translateY(-1px); }

.btn-outline {
  background: transparent;
  color: var(--primary);
  border: 2px solid var(--primary);
}
.btn-outline:hover { background: var(--primary-light); }

.btn-ghost {
  background: transparent;
  color: var(--text-secondary);
  border: 1px solid var(--border);
}
.btn-ghost:hover { background: var(--bg); color: var(--text-primary); }

.btn-lg { padding: 14px 28px; font-size: 16px; border-radius: var(--radius-md); }
.btn-sm { padding: 6px 14px; font-size: 12px; }
.btn-block { width: 100%; justify-content: center; }

.btn:disabled { opacity: 0.5; cursor: not-allowed; transform: none !important; }

/* ============================================
   CARDS
   ============================================ */

.card {
  background: var(--bg-card);
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
  overflow: hidden;
  transition: box-shadow var(--transition);
}

.card:hover { box-shadow: var(--shadow-md); }

.card-header {
  padding: 20px 24px 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.card-title {
  font-size: 16px;
  font-weight: 700;
  color: var(--text-primary);
}

.card-body { padding: 20px 24px; }

/* ============================================
   HOME SCREEN
   ============================================ */

.hero-section {
  background: linear-gradient(135deg, var(--primary) 0%, #7c3aed 100%);
  border-radius: var(--radius-lg);
  padding: 40px;
  color: white;
  margin-bottom: 28px;
  position: relative;
  overflow: hidden;
}

.hero-section::before {
  content: '📚';
  position: absolute;
  right: 40px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 100px;
  opacity: 0.15;
}

.hero-title {
  font-size: 28px;
  font-weight: 800;
  margin-bottom: 10px;
  line-height: 1.2;
}

.hero-subtitle {
  font-size: 16px;
  opacity: 0.85;
  max-width: 500px;
  margin-bottom: 24px;
}

.hero-actions {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}

.hero-btn {
  padding: 12px 24px;
  border-radius: var(--radius-sm);
  font-weight: 700;
  font-size: 14px;
  cursor: pointer;
  border: none;
  transition: all var(--transition);
  display: flex;
  align-items: center;
  gap: 8px;
}

.hero-btn-primary {
  background: white;
  color: var(--primary);
}
.hero-btn-primary:hover { transform: translateY(-2px); box-shadow: 0 6px 20px rgba(0,0,0,0.2); }

.hero-btn-secondary {
  background: rgba(255,255,255,0.2);
  color: white;
  border: 2px solid rgba(255,255,255,0.4);
  backdrop-filter: blur(10px);
}
.hero-btn-secondary:hover { background: rgba(255,255,255,0.3); transform: translateY(-2px); }

/* Upload Area */
.upload-area {
  border: 2px dashed var(--border-dark);
  border-radius: var(--radius-md);
  padding: 48px 24px;
  text-align: center;
  cursor: pointer;
  transition: all var(--transition);
  background: var(--bg-card);
  position: relative;
}

.upload-area:hover, .upload-area.dragover {
  border-color: var(--primary);
  background: var(--primary-light);
}

.upload-icon { font-size: 52px; margin-bottom: 16px; display: block; }
.upload-title { font-size: 18px; font-weight: 700; color: var(--text-primary); margin-bottom: 8px; }
.upload-subtitle { font-size: 14px; color: var(--text-secondary); margin-bottom: 20px; }

.upload-formats {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
}

.format-badge {
  padding: 4px 10px;
  background: var(--primary-light);
  color: var(--primary);
  border-radius: 20px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

body.dark .format-badge {
  background: rgba(37, 99, 235, 0.2);
  color: #93c5fd;
}

/* Quick Actions */
.quick-actions {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 16px;
  margin-bottom: 28px;
}

.action-card {
  background: var(--bg-card);
  border-radius: var(--radius-md);
  padding: 20px;
  border: 1px solid var(--border);
  cursor: pointer;
  transition: all var(--transition);
  text-align: center;
}

.action-card:hover {
  border-color: var(--primary);
  transform: translateY(-2px);
  box-shadow: var(--shadow-md);
}

.action-icon {
  font-size: 36px;
  margin-bottom: 12px;
  display: block;
}

.action-title { font-size: 14px; font-weight: 700; color: var(--text-primary); margin-bottom: 4px; }
.action-desc { font-size: 12px; color: var(--text-secondary); }

/* Section Header */
.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

.section-title {
  font-size: 18px;
  font-weight: 700;
  color: var(--text-primary);
  display: flex;
  align-items: center;
  gap: 8px;
}

/* Recent Books Grid */
.books-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 16px;
}

.book-card {
  background: var(--bg-card);
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  overflow: hidden;
  cursor: pointer;
  transition: all var(--transition);
}

.book-card:hover {
  transform: translateY(-4px);
  box-shadow: var(--shadow-lg);
  border-color: var(--primary);
}

.book-cover {
  height: 140px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 48px;
  position: relative;
}

.book-format-tag {
  position: absolute;
  top: 8px;
  right: 8px;
  padding: 2px 8px;
  border-radius: 20px;
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
}

.book-info { padding: 12px; }
.book-name { font-size: 13px; font-weight: 600; color: var(--text-primary); margin-bottom: 4px; line-height: 1.3; }
.book-meta { font-size: 11px; color: var(--text-muted); }

/* Empty State */
.empty-state {
  text-align: center;
  padding: 48px 24px;
  color: var(--text-muted);
}
.empty-icon { font-size: 64px; margin-bottom: 16px; display: block; opacity: 0.5; }
.empty-title { font-size: 18px; font-weight: 600; margin-bottom: 8px; color: var(--text-secondary); }
.empty-desc { font-size: 14px; }

/* ============================================
   READING SCREEN
   ============================================ */

#readingScreen {
  display: flex;
  flex-direction: column;
  height: calc(100vh - 64px);
  max-height: calc(100vh - 64px);
  width: 100%;
  position: relative;
  overflow: hidden;
}

/* Reading body — scrolls independently, TTS bar stays fixed at bottom */
#readingBody {
  flex: 1;
  overflow-y: auto;
  min-height: 0;
}

#pdfContainer {
  min-height: 500px;
}

#textContentContainer {
  min-height: 100%;
  padding: 40px;
  background: var(--bg, #f8fafc);
}

/* Ensure text container is visible when explicitly shown */
#textContentContainer[style*="display: block"],
#textContentContainer[style*="display:block"] {
  visibility: visible !important;
  opacity: 1 !important;
}

#textContent {
  display: block !important;
  max-width: 780px;
  margin: 0 auto;
  background: #ffffff !important;
  border-radius: 12px;
  padding: 48px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.08);
  font-family: Georgia, "Times New Roman", serif;
  font-size: 18px;
  line-height: 1.85;
  white-space: pre-wrap;
  word-break: break-word;
  color: #0f172a !important;
  min-height: 200px;
}

body.dark #textContent {
  background: #1e293b !important;
  color: #e2e8f0 !important;
}

body.dark #textContentContainer {
  background: #0f172a;
}

body.dark #textContent {
  background: #1e293b;
  color: #e2e8f0;
}

#imageOcrContainer {
  min-height: 500px;
  display: flex;
}

.reading-topbar {
  background: var(--bg-card);
  border-bottom: 1px solid var(--border);
  padding: 0 24px;
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
  box-shadow: var(--shadow-sm);
}

.reading-title {
  font-size: 16px;
  font-weight: 700;
  color: var(--text-primary);
  max-width: 400px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.reading-actions { display: flex; gap: 8px; align-items: center; }

/* PDF Viewer */
#pdf-container {
  flex: 1;
  overflow-y: auto;
  background: #525659;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 24px;
  gap: 16px;
}

#pdf-container canvas {
  box-shadow: var(--shadow-lg);
  max-width: 100%;
}

/* Text Content Viewer */
#text-content-container {
  flex: 1;
  overflow-y: auto;
  background: var(--bg);
  padding: 40px;
}

#text-content {
  max-width: 780px;
  margin: 0 auto;
  background: var(--bg-card);
  border-radius: var(--radius-md);
  padding: 48px;
  box-shadow: var(--shadow-md);
  font-family: var(--font-serif);
  font-size: 18px;
  line-height: 1.85;
  color: var(--text-primary);
  white-space: pre-wrap;
  word-break: break-word;
}

body.dark #text-content {
  background: var(--bg-card);
}

/* Highlighted word during TTS */
#text-content .tts-highlight {
  background: #fef08a;
  color: #713f12;
  border-radius: 3px;
  padding: 0 2px;
}

body.dark #text-content .tts-highlight {
  background: #713f12;
  color: #fef08a;
}

/* Image viewer (for OCR) */
#image-ocr-container {
  flex: 1;
  display: flex;
  gap: 0;
  overflow: hidden;
}

.image-panel {
  flex: 0 0 45%;
  overflow-y: auto;
  background: #525659;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 24px;
}

.image-panel img {
  max-width: 100%;
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-lg);
}

.text-panel {
  flex: 1;
  overflow-y: auto;
  padding: 32px;
  background: var(--bg);
}

#ocr-text {
  font-family: var(--font-serif);
  font-size: 16px;
  line-height: 1.8;
  color: var(--text-primary);
  white-space: pre-wrap;
  word-break: break-word;
}

/* TTS Controls Bar — always visible at bottom of reading screen */
.tts-bar {
  background: var(--bg-card);
  border-top: 1px solid var(--border);
  padding: 10px 28px 14px;
  flex-shrink: 0;
  box-shadow: 0 -4px 12px rgba(0,0,0,0.06);
  z-index: 20;
}

/* Progress Row */
.tts-progress-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
  max-width: 900px;
  margin-left: auto;
  margin-right: auto;
}

.tts-time {
  font-size: 12px;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  min-width: 36px;
  text-align: center;
}

.tts-progress-pct {
  font-size: 12px;
  font-weight: 600;
  color: var(--primary);
  min-width: 36px;
  text-align: right;
}

.tts-progress-track {
  flex: 1;
  position: relative;
  height: 6px;
  background: var(--border);
  border-radius: 99px;
  cursor: pointer;
}

.tts-progress-fill {
  position: absolute;
  left: 0; top: 0;
  height: 100%;
  background: linear-gradient(90deg, var(--primary), #7c3aed);
  border-radius: 99px;
  pointer-events: none;
  transition: width 0.3s ease;
}

.tts-progress-seek {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  cursor: pointer;
  margin: 0;
  padding: 0;
  /* Make the hit area taller for easier clicking */
  transform: scaleY(4);
}

.tts-progress-track:hover .tts-progress-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--primary), #7c3aed);
}

.tts-progress-track:hover {
  height: 8px;
}

.tts-controls {
  display: flex;
  align-items: center;
  gap: 16px;
  max-width: 900px;
  margin: 0 auto;
  flex-wrap: wrap;
}

.tts-play-controls {
  display: flex;
  align-items: center;
  gap: 8px;
}

.tts-btn {
  width: 42px; height: 42px;
  border-radius: 50%;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  transition: all var(--transition);
  background: var(--bg);
  color: var(--text-secondary);
  border: 1px solid var(--border);
}

.tts-btn:hover { background: var(--primary-light); color: var(--primary); border-color: var(--primary); }

.tts-btn-play {
  width: 52px; height: 52px;
  background: var(--primary);
  color: white;
  font-size: 22px;
  border-color: var(--primary);
  box-shadow: 0 4px 12px rgba(37,99,235,0.4);
}
.tts-btn-play:hover { background: var(--primary-dark); transform: scale(1.05); }
.tts-btn-play.playing { background: var(--secondary); border-color: var(--secondary); }

.tts-divider {
  width: 1px;
  height: 36px;
  background: var(--border);
}

.tts-slider-group {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1;
  min-width: 180px;
}

.tts-slider-label {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  white-space: nowrap;
  min-width: 50px;
}

.tts-slider {
  flex: 1;
  -webkit-appearance: none;
  height: 4px;
  background: var(--border);
  border-radius: 4px;
  outline: none;
  cursor: pointer;
}

.tts-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 16px; height: 16px;
  border-radius: 50%;
  background: var(--primary);
  cursor: pointer;
  box-shadow: 0 2px 6px rgba(37,99,235,0.4);
  transition: transform var(--transition);
}

.tts-slider::-webkit-slider-thumb:hover { transform: scale(1.2); }

.tts-slider-value {
  font-size: 12px;
  font-weight: 700;
  color: var(--primary);
  min-width: 32px;
  text-align: right;
}

.tts-status {
  font-size: 12px;
  color: var(--text-muted);
  font-style: italic;
  min-width: 120px;
}

.tts-status.playing { color: var(--accent); }
.tts-status.paused { color: var(--secondary); }

/* Language Selector */
.lang-select {
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-card);
  color: var(--text-primary);
  font-size: 13px;
  cursor: pointer;
  outline: none;
  max-width: 140px;
}

/* ============================================
   LOADING & PROGRESS
   ============================================ */

.loading-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  backdrop-filter: blur(6px);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  gap: 20px;
  color: white;
}

.spinner {
  width: 52px; height: 52px;
  border: 4px solid rgba(255,255,255,0.2);
  border-top-color: white;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

.loading-text { font-size: 16px; font-weight: 600; }
.loading-sub { font-size: 13px; opacity: 0.75; }

.progress-bar-wrap {
  width: 280px;
  height: 6px;
  background: rgba(255,255,255,0.2);
  border-radius: 6px;
  overflow: hidden;
}

.progress-bar-fill {
  height: 100%;
  background: white;
  border-radius: 6px;
  transition: width 0.3s ease;
}

/* ============================================
   MODAL
   ============================================ */

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 500;
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--transition);
}

.modal-overlay.open {
  opacity: 1;
  pointer-events: all;
}

.modal {
  background: var(--bg-card);
  border-radius: var(--radius-lg);
  padding: 32px;
  max-width: 480px;
  width: 90%;
  box-shadow: var(--shadow-lg);
  transform: scale(0.95);
  transition: transform var(--transition);
}

.modal-overlay.open .modal { transform: scale(1); }

.modal-title { font-size: 20px; font-weight: 700; margin-bottom: 8px; }
.modal-desc { font-size: 14px; color: var(--text-secondary); margin-bottom: 24px; }
.modal-actions { display: flex; gap: 12px; justify-content: flex-end; }

/* ============================================
   TOAST NOTIFICATIONS
   ============================================ */

#toast-container {
  position: fixed;
  bottom: 24px; right: 24px;
  z-index: 9999;
  display: flex;
  flex-direction: column;
  gap: 10px;
  align-items: flex-end;
}

.toast {
  background: var(--bg-card);
  color: var(--text-primary);
  padding: 12px 18px;
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  font-size: 14px;
  font-weight: 500;
  display: flex;
  align-items: center;
  gap: 10px;
  border-left: 4px solid var(--primary);
  animation: slideIn 0.3s ease;
  max-width: 320px;
}

.toast.success { border-color: var(--accent); }
.toast.error { border-color: #ef4444; }
.toast.warning { border-color: var(--secondary); }

@keyframes slideIn {
  from { transform: translateX(100%); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}

/* ============================================
   SETTINGS PAGE
   ============================================ */

.settings-section { margin-bottom: 28px; }
.settings-section-title {
  font-size: 14px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: var(--text-muted);
  margin-bottom: 12px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border);
}

.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 0;
  border-bottom: 1px solid var(--border);
}

.settings-row:last-child { border-bottom: none; }

.settings-label { font-size: 14px; font-weight: 500; color: var(--text-primary); }
.settings-desc { font-size: 12px; color: var(--text-muted); margin-top: 2px; }

/* Toggle Switch */
.toggle {
  position: relative;
  width: 46px; height: 26px;
}

.toggle input { opacity: 0; width: 0; height: 0; }

.toggle-slider {
  position: absolute;
  cursor: pointer;
  inset: 0;
  background: var(--border-dark);
  border-radius: 26px;
  transition: var(--transition);
}

.toggle-slider::before {
  content: '';
  position: absolute;
  width: 20px; height: 20px;
  left: 3px; bottom: 3px;
  background: white;
  border-radius: 50%;
  transition: var(--transition);
  box-shadow: var(--shadow-sm);
}

.toggle input:checked + .toggle-slider { background: var(--primary); }
.toggle input:checked + .toggle-slider::before { transform: translateX(20px); }

/* Font Size Selector */
.font-size-group {
  display: flex;
  gap: 8px;
}

.font-size-btn {
  padding: 6px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-card);
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
  transition: all var(--transition);
}

.font-size-btn.active, .font-size-btn:hover {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}

/* ============================================
   RESPONSIVE
   ============================================ */

.hamburger {
  display: none;
  width: 38px; height: 38px;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 5px;
  cursor: pointer;
  border: none;
  background: none;
  padding: 0;
}

.hamburger span {
  width: 22px;
  height: 2px;
  background: var(--text-primary);
  border-radius: 2px;
  transition: all 0.3s;
}

.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 99;
}

@media (max-width: 768px) {
  .sidebar {
    transform: translateX(-100%);
    z-index: 100;
  }

  .sidebar.open { transform: translateX(0); }

  .sidebar-overlay.open { display: block; }

  .main-content { margin-left: 0; }

  .hamburger { display: flex; }

  .hero-section::before { display: none; }

  .hero-title { font-size: 22px; }

  .quick-actions { grid-template-columns: repeat(2, 1fr); }

  .books-grid { grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); }

  .tts-controls { gap: 10px; }

  .tts-slider-group { min-width: 120px; }

  #text-content { padding: 24px; font-size: 16px; }

  #image-ocr-container { flex-direction: column; }
  .image-panel { flex: none; height: 250px; }

  .page-container { padding: 16px; }
}

@media (max-width: 480px) {
  .tts-slider-group { display: none; }
  .quick-actions { grid-template-columns: 1fr 1fr; }
}

/* ============================================
   READING AREA WRAPPER (for AI panel split layout)
   ============================================ */

.reading-area-wrapper {
  flex: 1;
  display: flex;
  flex-direction: row;
  min-height: 0;
  overflow: hidden;
  position: relative;
}

/* When AI panel is open, reading body narrows */
.reading-area-wrapper.ai-open #readingBody {
  flex: 1 1 0;
  min-width: 0;
}

/* ============================================
   AI CHAT TOGGLE BUTTON (in reading topbar)
   ============================================ */

.ai-chat-toggle-btn {
  position: relative;
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  transition: all var(--transition);
}

.ai-chat-toggle-btn:hover {
  background: #eff6ff;
  border-color: var(--primary);
  color: var(--primary);
}

.ai-chat-toggle-btn.active {
  background: var(--primary);
  border-color: var(--primary);
  color: white;
  box-shadow: 0 2px 8px rgba(37,99,235,0.4);
}

/* ============================================
   AI CHAT PANEL
   ============================================ */

.ai-chat-panel {
  width: 0;
  min-width: 0;
  background: var(--bg-card);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: width 0.35s cubic-bezier(0.4, 0, 0.2, 1),
              min-width 0.35s cubic-bezier(0.4, 0, 0.2, 1);
  flex-shrink: 0;
}

.ai-chat-panel.open {
  width: 380px;
  min-width: 320px;
}

/* Panel Header */
.ai-chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 18px;
  border-bottom: 1px solid var(--border);
  background: linear-gradient(135deg, #2563eb 0%, #7c3aed 100%);
  color: white;
  flex-shrink: 0;
}

.ai-chat-header-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.ai-chat-icon {
  font-size: 28px;
  background: rgba(255,255,255,0.2);
  width: 44px;
  height: 44px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.ai-chat-title {
  font-size: 15px;
  font-weight: 700;
  color: white;
}

.ai-chat-subtitle {
  font-size: 11px;
  color: rgba(255,255,255,0.75);
  max-width: 180px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 2px;
}

.ai-chat-header-btn {
  width: 30px;
  height: 30px;
  border-radius: 8px;
  border: 1px solid rgba(255,255,255,0.3);
  background: rgba(255,255,255,0.15);
  color: white;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  transition: background var(--transition);
}

.ai-chat-header-btn:hover {
  background: rgba(255,255,255,0.3);
}

/* API Key Setup Screen */
.ai-key-setup {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 32px 24px;
  gap: 14px;
  text-align: center;
}

.ai-key-icon {
  font-size: 48px;
  margin-bottom: 4px;
}

.ai-key-title {
  font-size: 16px;
  font-weight: 700;
  color: var(--text-primary);
}

.ai-key-desc {
  font-size: 13px;
  color: var(--text-secondary);
  line-height: 1.5;
  max-width: 280px;
}

.ai-key-input-field {
  width: 100%;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--text-primary);
  font-size: 14px;
  outline: none;
  transition: border-color var(--transition);
}

.ai-key-input-field:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37,99,235,0.15);
}

.ai-key-link {
  font-size: 12px;
  color: var(--primary);
  text-decoration: none;
}

.ai-key-link:hover {
  text-decoration: underline;
}

/* Chat Messages */
.ai-chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 14px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  scroll-behavior: smooth;
}

.ai-msg {
  display: flex;
  align-items: flex-start;
  gap: 10px;
}

.ai-msg-assistant {
  flex-direction: row;
}

.ai-msg-user {
  flex-direction: row-reverse;
}

.ai-msg-avatar {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: linear-gradient(135deg, #2563eb 0%, #7c3aed 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  flex-shrink: 0;
  box-shadow: 0 2px 8px rgba(37,99,235,0.3);
}

.ai-msg-avatar-user {
  background: linear-gradient(135deg, #f97316 0%, #ec4899 100%);
  box-shadow: 0 2px 8px rgba(249,115,22,0.3);
}

.ai-msg-bubble {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 12px 12px 12px 3px;
  padding: 12px 14px;
  font-size: 13.5px;
  line-height: 1.65;
  color: var(--text-primary);
  max-width: calc(100% - 50px);
  word-break: break-word;
}

.ai-msg-bubble p {
  margin: 0;
}

.ai-msg-bubble p + p {
  margin-top: 8px;
}

.ai-msg-bubble strong {
  font-weight: 700;
  color: var(--text-primary);
}

.ai-msg-bubble-user {
  background: linear-gradient(135deg, #2563eb 0%, #7c3aed 100%);
  border-color: transparent;
  color: white;
  border-radius: 12px 12px 3px 12px;
}

/* Typing indicator */
.ai-typing {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 4px 2px;
}

.ai-typing span {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text-muted);
  animation: aiTypingBounce 1.2s infinite ease-in-out;
}

.ai-typing span:nth-child(2) { animation-delay: 0.2s; }
.ai-typing span:nth-child(3) { animation-delay: 0.4s; }

@keyframes aiTypingBounce {
  0%, 80%, 100% { transform: scale(0.8); opacity: 0.5; }
  40%           { transform: scale(1.2); opacity: 1; }
}

/* Suggestion Chips */
.ai-suggestions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 10px 14px;
  border-top: 1px solid var(--border);
  background: var(--bg);
  flex-shrink: 0;
}

.ai-suggestion-chip {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 99px;
  padding: 5px 12px;
  font-size: 12px;
  color: var(--primary);
  cursor: pointer;
  transition: all var(--transition);
  white-space: nowrap;
}

.ai-suggestion-chip:hover {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(37,99,235,0.25);
}

/* Chat Input Row */
.ai-chat-input-row {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  padding: 12px 14px;
  border-top: 1px solid var(--border);
  background: var(--bg-card);
  flex-shrink: 0;
}

.ai-chat-input {
  flex: 1;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 20px;
  background: var(--bg);
  color: var(--text-primary);
  font-size: 14px;
  font-family: var(--font-sans);
  outline: none;
  resize: none;
  line-height: 1.5;
  transition: border-color var(--transition), box-shadow var(--transition);
  min-height: 40px;
  max-height: 120px;
  overflow-y: auto;
}

.ai-chat-input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37,99,235,0.15);
}

.ai-chat-input::placeholder {
  color: var(--text-muted);
}

.ai-chat-input:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.ai-send-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--primary);
  border: none;
  color: white;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: all var(--transition);
  box-shadow: 0 2px 8px rgba(37,99,235,0.4);
}

.ai-send-btn:hover {
  background: var(--primary-dark);
  transform: scale(1.08);
  box-shadow: 0 4px 12px rgba(37,99,235,0.5);
}

.ai-send-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

/* Settings API key input */
.api-key-input {
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--text-primary);
  font-size: 13px;
  width: 200px;
  outline: none;
  transition: border-color var(--transition);
}

.api-key-input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37,99,235,0.15);
}

/* ============================================
   RESUME READING BANNER
   ============================================ */

.resume-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  background: linear-gradient(135deg, #eff6ff 0%, #f5f3ff 100%);
  border: 1px solid #bfdbfe;
  border-left: 4px solid var(--primary);
  border-radius: 10px;
  padding: 14px 18px;
  margin: 16px 20px 0;
  animation: resumeSlideIn 0.35s ease;
  flex-wrap: wrap;
  flex-shrink: 0;
  position: sticky;
  top: 0;
  z-index: 15;
  box-shadow: 0 2px 12px rgba(37,99,235,0.12);
}

body.dark .resume-banner {
  background: linear-gradient(135deg, rgba(37,99,235,0.12) 0%, rgba(124,58,237,0.12) 100%);
  border-color: rgba(37,99,235,0.4);
}

@keyframes resumeSlideIn {
  from { opacity: 0; transform: translateY(-12px); }
  to   { opacity: 1; transform: translateY(0); }
}

.resume-banner-left {
  display: flex;
  align-items: center;
  gap: 12px;
  flex: 1;
  min-width: 0;
}

.resume-icon {
  font-size: 28px;
  flex-shrink: 0;
}

.resume-title {
  font-size: 14px;
  font-weight: 700;
  color: var(--text-primary);
  margin-bottom: 2px;
}

.resume-sub {
  font-size: 12px;
  color: var(--text-secondary);
}

.resume-banner-right {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.resume-btn {
  padding: 7px 16px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  border: none;
  transition: all var(--transition);
  white-space: nowrap;
}

.resume-btn-primary {
  background: var(--primary);
  color: white;
  box-shadow: 0 2px 8px rgba(37,99,235,0.3);
}

.resume-btn-primary:hover {
  background: var(--primary-dark);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(37,99,235,0.4);
}

.resume-btn-ghost {
  background: transparent;
  color: var(--text-secondary);
  border: 1px solid var(--border);
}

.resume-btn-ghost:hover {
  background: var(--bg);
  color: var(--text-primary);
}

.resume-banner-close {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  transition: all var(--transition);
  flex-shrink: 0;
}

.resume-banner-close:hover {
  background: var(--border);
  color: var(--text-primary);
}

/* ============================================
   BOOK CARD READING PROGRESS
   ============================================ */

.book-progress-bar {
  height: 4px;
  background: var(--border);
  overflow: hidden;
}

.book-progress-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--primary), #7c3aed);
  border-radius: 0;
  transition: width 0.3s ease;
}

.book-progress-label {
  position: absolute;
  bottom: 8px;
  left: 8px;
}

.book-progress-pct {
  background: rgba(37,99,235,0.85);
  color: white;
  font-size: 10px;
  font-weight: 700;
  padding: 2px 7px;
  border-radius: 99px;
  letter-spacing: 0.3px;
  backdrop-filter: blur(4px);
}

.book-progress-done {
  background: rgba(16,185,129,0.9);
  color: white;
  font-size: 10px;
  font-weight: 700;
  padding: 2px 7px;
  border-radius: 99px;
}

/* Responsive: AI panel on mobile */
@media (max-width: 768px) {
  .ai-chat-panel.open {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    width: 100vw !important;
    min-width: unset !important;
    max-width: 100vw;
    z-index: 200;
    border-left: none;
  }
}

/* ============================================
   UTILITIES
   ============================================ */

.hidden { display: none !important; }
.flex { display: flex; }
.flex-col { flex-direction: column; }
.items-center { align-items: center; }
.justify-between { justify-content: space-between; }
.gap-2 { gap: 8px; }
.gap-4 { gap: 16px; }
.mb-4 { margin-bottom: 16px; }
.mb-6 { margin-bottom: 24px; }
.mt-4 { margin-top: 16px; }
.text-center { text-align: center; }
.text-muted { color: var(--text-muted); font-size: 13px; }
.font-bold { font-weight: 700; }

/* Format colors */
.fmt-pdf { background: #fee2e2; color: #dc2626; }
.fmt-epub { background: #fef3c7; color: #d97706; }
.fmt-txt { background: #dcfce7; color: #16a34a; }
.fmt-doc { background: #dbeafe; color: #2563eb; }
.fmt-img { background: #fae8ff; color: #9333ea; }
.fmt-other { background: var(--border); color: var(--text-muted); }

body.dark .fmt-pdf { background: rgba(220,38,38,0.2); }
body.dark .fmt-epub { background: rgba(217,119,6,0.2); }
body.dark .fmt-txt { background: rgba(22,163,74,0.2); }
body.dark .fmt-doc { background: rgba(37,99,235,0.2); }
body.dark .fmt-img { background: rgba(147,51,234,0.2); }