/* Design Tokens */
:root {
  --bg-dark: #07090e;
  --sidebar-bg: rgba(14, 18, 27, 0.95);
  --panel-bg: rgba(22, 28, 45, 0.45);
  --border-light: rgba(255, 255, 255, 0.08);
  --text-main: #f3f4f6;
  --text-dim: #9ca3af;
  --purple-glow: rgba(168, 85, 247, 0.35);
  --cyan-glow: rgba(6, 182, 212, 0.35);
  --gradient-accent: linear-gradient(135deg, #a855f7, #06b6d4);
  --font-display: 'Outfit', 'Noto Sans TC', sans-serif;
  --shadow-main: 0 15px 35px -5px rgba(0, 0, 0, 0.6), 0 10px 15px -8px rgba(0, 0, 0, 0.6);
  --transition-fast: all 0.2s ease;
  --transition-smooth: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* Base Reset */
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: var(--font-display);
  background-color: var(--bg-dark);
  color: var(--text-main);
  height: 100vh;
  overflow: hidden;
}

/* App Container Layout */
.app-container {
  display: flex;
  height: 100vh;
  width: 100vw;
}

/* Sidebar styling */
.sidebar {
  width: 320px;
  background-color: var(--sidebar-bg);
  border-right: 1px solid var(--border-light);
  display: flex;
  flex-direction: column;
  padding: 24px;
  gap: 24px;
  flex-shrink: 0;
  z-index: 10;
  box-shadow: 10px 0 30px rgba(0, 0, 0, 0.5);
}

.sidebar-brand {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid var(--border-light);
  padding-bottom: 16px;
}

.sidebar-brand h2 {
  font-size: 1.35rem;
  font-weight: 800;
  background: linear-gradient(to right, #fff, #a1a1aa);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  letter-spacing: 0.05em;
}

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

.version-tag {
  background: var(--gradient-accent);
  padding: 4px 10px;
  border-radius: 8px;
  font-size: 0.72rem;
  font-weight: 600;
  color: #ffffff;
  display: inline-block;
  box-shadow: 0 0 10px rgba(6, 182, 212, 0.25);
}

/* Drag & Drop Zone */
.drop-zone {
  border: 2px dashed rgba(255, 255, 255, 0.15);
  border-radius: 16px;
  padding: 24px;
  text-align: center;
  background-color: rgba(255, 255, 255, 0.02);
  transition: var(--transition-smooth);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
}

.drop-zone.dragover {
  border-color: #06b6d4;
  background-color: rgba(6, 182, 212, 0.04);
  box-shadow: 0 0 20px rgba(6, 182, 212, 0.15);
  transform: scale(1.02);
}

.upload-icon {
  width: 48px;
  height: 48px;
  fill: var(--text-dim);
  transition: var(--transition-fast);
}

.drop-zone:hover .upload-icon {
  fill: #06b6d4;
  transform: translateY(-4px);
}

.drop-text {
  font-size: 0.9rem;
  color: var(--text-dim);
  font-weight: 500;
}

.or-divider {
  font-size: 0.75rem;
  color: rgba(255, 255, 255, 0.25);
  text-transform: uppercase;
  letter-spacing: 0.1em;
}

.upload-btn {
  background-color: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.1);
  color: #fff;
  padding: 8px 20px;
  border-radius: 8px;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: var(--transition-fast);
}

.upload-btn:hover {
  background-color: rgba(255, 255, 255, 0.1);
  border-color: rgba(255, 255, 255, 0.2);
}

/* Info Cards */
.book-info-card, .progress-info-card {
  background-color: rgba(255, 255, 255, 0.02);
  border: 1px solid var(--border-light);
  border-radius: 16px;
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.book-info-card h3, .progress-info-card h3 {
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--text-dim);
  font-weight: 700;
}

.info-row {
  display: flex;
  justify-content: space-between;
  font-size: 0.85rem;
}

.info-row .label {
  color: var(--text-dim);
}

.info-row .value {
  color: #fff;
  font-weight: 600;
  max-width: 160px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Reading Progress UI */
.progress-metric {
  display: flex;
  align-items: baseline;
  gap: 8px;
  margin-top: 4px;
}

.progress-metric span:first-child {
  font-size: 2.25rem;
  font-weight: 800;
  background: linear-gradient(135deg, #a855f7, #06b6d4);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.metric-label {
  font-size: 0.8rem;
  color: var(--text-dim);
  font-weight: 500;
}

/* Main Panel Layout */
.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  height: 100%;
  position: relative;
  background: radial-gradient(circle at top right, rgba(168, 85, 247, 0.06), transparent 50%),
              radial-gradient(circle at bottom left, rgba(6, 182, 212, 0.04), transparent 40%);
}

/* Top bar with TTS settings */
.top-bar {
  padding: 24px 40px;
  background-color: rgba(11, 13, 20, 0.45);
  border-bottom: 1px solid var(--border-light);
  display: flex;
  justify-content: space-between;
  align-items: center;
  backdrop-filter: blur(20px);
  z-index: 5;
}

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

.chapter-selector-wrapper {
  display: flex;
  align-items: center;
  gap: 8px;
  background: rgba(30, 41, 59, 0.6);
  border: 1px solid rgba(255, 255, 255, 0.12);
  padding: 6px 12px;
  border-radius: 10px;
}

.chapter-select-label {
  font-size: 0.8rem;
  color: var(--primary-light);
  font-weight: 600;
  white-space: nowrap;
  flex-shrink: 0;
}

.chapter-select-dropdown {
  background: transparent;
  border: none;
  color: #ffffff;
  font-size: 0.85rem;
  font-weight: 500;
  outline: none;
  cursor: pointer;
  max-width: 260px;
  flex: 1;
  min-width: 0;
}
.header-welcome h1 {
  font-size: 1.4rem;
  font-weight: 800;
  color: #fff;
}

.header-welcome .subtitle {
  font-size: 0.8rem;
  color: var(--text-dim);
  margin-top: 4px;
}

.tts-settings {
  display: flex;
  gap: 24px;
  align-items: center;
}

.control-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.control-group label {
  font-size: 0.72rem;
  color: var(--text-dim);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.custom-select {
  background-color: rgba(255, 255, 255, 0.04);
  border: 1px solid var(--border-light);
  color: #fff;
  padding: 8px 12px;
  border-radius: 8px;
  outline: none;
  font-family: var(--font-display);
  font-size: 0.85rem;
  cursor: pointer;
  min-width: 200px;
  transition: var(--transition-fast);
}

.custom-select:focus {
  border-color: #06b6d4;
  box-shadow: 0 0 10px rgba(6, 182, 212, 0.2);
}

input[type="range"] {
  -webkit-appearance: none;
  width: 100px;
  height: 5px;
  border-radius: 9999px;
  background: rgba(255, 255, 255, 0.08);
  outline: none;
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: #06b6d4;
  cursor: pointer;
  box-shadow: 0 0 8px #06b6d4;
  transition: var(--transition-fast);
}

input[type="range"]::-webkit-slider-thumb:hover {
  transform: scale(1.25);
}

/* Reader Viewport */
.reader-viewport {
  flex: 1;
  overflow-y: auto;
  padding: 40px 12%;
  padding-bottom: 150px;
}

.reader-viewport::-webkit-scrollbar {
  width: 8px;
}

.reader-viewport::-webkit-scrollbar-thumb {
  background-color: rgba(255, 255, 255, 0.06);
  border-radius: 9999px;
}

.book-meta-header {
  margin-bottom: 36px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
  padding-bottom: 20px;
}

.book-meta-header h2 {
  font-size: 2.2rem;
  font-weight: 800;
  background: linear-gradient(135deg, #fff, var(--text-dim));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

/* Welcome screen styling */
.welcome-screen {
  text-align: center;
  padding: 80px 40px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
}

.welcome-screen h3 {
  font-size: 1.75rem;
  font-weight: 800;
  color: #fff;
}

.welcome-screen p {
  font-size: 1.05rem;
  color: var(--text-dim);
  max-width: 500px;
  line-height: 1.8;
}

.action-box {
  margin-top: 16px;
}

.secondary-btn {
  background: linear-gradient(135deg, rgba(168, 85, 247, 0.15), rgba(6, 182, 212, 0.15));
  border: 1px solid rgba(6, 182, 212, 0.3);
  color: #fff;
  padding: 12px 28px;
  border-radius: 12px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: var(--transition-smooth);
}

.secondary-btn:hover {
  border-color: #06b6d4;
  transform: translateY(-2px);
  box-shadow: 0 5px 20px rgba(6, 182, 212, 0.2);
}

/* Article Styling */
.story-article {
  font-size: 1.15rem;
  line-height: 2.2;
  letter-spacing: 0.04em;
  color: #d1d5db;
}

.story-article p {
  margin-bottom: 30px;
  padding: 12px 24px;
  border-radius: 10px;
  border-left: 3px solid transparent;
  transition: var(--transition-smooth);
}

.story-article p:hover {
  background-color: rgba(255, 255, 255, 0.01);
  cursor: pointer;
}

.story-article p.active-paragraph {
  background-color: rgba(6, 182, 212, 0.06);
  border-left-color: #06b6d4;
  color: #fff;
  transform: translateX(4px);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
}

/* Bottom Player Control Bar */
.player-controls {
  position: absolute;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  width: calc(100% - 80px);
  max-width: 800px;
  background: var(--panel-bg);
  border: 1px solid var(--border-light);
  box-shadow: var(--shadow-main);
  backdrop-filter: blur(24px);
  border-radius: 24px;
  padding: 20px 32px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  z-index: 100;
  transition: var(--transition-smooth);
}

.progress-container {
  display: flex;
  align-items: center;
  gap: 16px;
}

.progress-bar-wrapper {
  flex: 1;
  height: 8px;
  background-color: rgba(255, 255, 255, 0.08);
  border-radius: 9999px;
  overflow: hidden;
}

.progress-bar {
  height: 100%;
  background: var(--gradient-accent);
  border-radius: 9999px;
  box-shadow: 0 0 10px #06b6d4;
  width: 0%;
  transition: width 0.3s ease;
}

.progress-stats {
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--text-dim);
  min-width: 90px;
  text-align: right;
}

.buttons-container {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 28px;
}

.ctrl-btn {
  background: transparent;
  border: none;
  outline: none;
  cursor: pointer;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: var(--transition-smooth);
  border: 1px solid rgba(255, 255, 255, 0.04);
}

.ctrl-btn svg {
  width: 18px;
  height: 18px;
  fill: var(--text-dim);
  transition: var(--transition-fast);
}

.ctrl-btn:hover {
  background-color: rgba(255, 255, 255, 0.06);
  border-color: rgba(255, 255, 255, 0.12);
}

.ctrl-btn:hover svg {
  fill: #fff;
}

.play-main {
  width: 56px;
  height: 56px;
  background: var(--gradient-accent);
  border: none;
  box-shadow: 0 4px 15px rgba(6, 182, 212, 0.35);
}

.play-main svg {
  width: 24px;
  height: 24px;
  fill: #fff;
}

.play-main:hover {
  transform: scale(1.08);
  box-shadow: 0 6px 22px rgba(6, 182, 212, 0.55);
}

.play-main:active {
  transform: scale(0.95);
}

.mobile-upload-group {
  display: none;
}

/* Responsive adjustments */
@media (max-width: 950px) {
  .app-container {
    flex-direction: column;
  }
  .sidebar {
    width: 100%;
    height: auto;
    border-right: none;
    border-bottom: 1px solid var(--border-light);
    padding: 16px;
    gap: 16px;
  }
  .top-bar {
    flex-direction: column;
    gap: 16px;
    padding: 16px 20px;
  }
  .tts-settings {
    width: 100%;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 12px;
  }
  .reader-viewport {
    padding: 24px 20px;
    padding-bottom: 160px;
  }
  .player-controls {
    width: calc(100% - 40px);
    bottom: 16px;
  }
}

@media (max-width: 768px) {
  /* Bookshelf Mobile Layout: Exactly 2 Books Per Row */
  .books-grid {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 14px !important;
    padding-bottom: 40px;
  }
  .bookshelf-container {
    padding: 16px !important;
  }
  .bookshelf-header {
    flex-direction: column !important;
    align-items: flex-start !important;
    gap: 12px !important;
  }
  /* Mobile Touch Target Optimizations for Mobile Screens */
  .book-settings-toggle-btn, .delete-book-btn {
    width: 44px !important;
    height: 44px !important;
    top: 10px !important;
  }
  .book-settings-toggle-btn svg, .delete-book-btn svg {
    width: 22px !important;
    height: 22px !important;
  }
  .book-card-play-btn {
    width: 48px !important;
    height: 48px !important;
    bottom: 12px !important;
    right: 12px !important;
  }
  .book-card-play-btn svg {
    width: 22px !important;
    height: 22px !important;
  }

  /* Reader Page Top Bar & Controls (Clean 2-Row Vertical Stack on Mobile - Zero Overlap) */
  .top-bar-left {
    width: 100% !important;
    display: flex !important;
    flex-direction: column !important;
    align-items: stretch !important;
    gap: 10px !important;
  }
  .back-btn {
    width: 100% !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    padding: 10px 16px !important;
    font-size: 0.92rem !important;
    box-sizing: border-box !important;
    margin: 0 !important;
  }
  .back-btn svg {
    width: 18px !important;
    height: 18px !important;
  }
  .chapter-selector-wrapper {
    width: 100% !important;
    display: flex !important;
    align-items: center !important;
    box-sizing: border-box !important;
    padding: 8px 14px !important;
    margin: 0 !important;
  }
  .chapter-select-label {
    flex-shrink: 0 !important;
    font-size: 0.88rem !important;
    white-space: nowrap !important;
  }
  .chapter-select-dropdown {
    flex: 1 !important;
    min-width: 0 !important;
    font-size: 0.88rem !important;
  }

  /* Bottom Audio Player Controls */
  .ctrl-btn {
    width: 50px !important;
    height: 50px !important;
  }
  .ctrl-btn svg {
    width: 24px !important;
    height: 24px !important;
  }
  .play-main {
    width: 64px !important;
    height: 64px !important;
  }
  .play-main svg {
    width: 28px !important;
    height: 28px !important;
  }

  /* Settings Overlay Panel Buttons */
  .change-cover-btn {
    padding: 12px 18px !important;
    font-size: 0.9rem !important;
    border-radius: 10px !important;
  }
  .settings-close-btn {
    padding: 6px 14px !important;
    font-size: 0.85rem !important;
  }
  .custom-select-small {
    padding: 8px 10px !important;
    font-size: 0.88rem !important;
    max-width: 100% !important;
  }
}

/* Debug Card & Console */
.debug-card {
  background-color: rgba(0, 0, 0, 0.25);
  border: 1px solid var(--border-light);
  border-radius: 16px;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 160px;
}

.debug-console {
  font-family: monospace;
  font-size: 0.72rem;
  color: #a1a1aa;
  overflow-y: auto;
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.debug-console::-webkit-scrollbar {
  width: 4px;
}

.debug-console::-webkit-scrollbar-thumb {
  background-color: rgba(255, 255, 255, 0.05);
  border-radius: 9999px;
}

.log-line {
  padding: 2px 4px;
  border-radius: 4px;
  word-break: break-all;
  line-height: 1.3;
}

.log-info {
  color: #a1a1aa;
}

.log-play {
  color: #38bdf8;
}

.log-end {
  color: #34d399;
}

.log-error {
  color: #f87171;
  background-color: rgba(248, 113, 113, 0.08);
}

/* Mobile Optimization (Screens <= 650px) */
@media (max-width: 650px) {
  /* Hide sidebar completely on mobile to maximize reading space */
  .sidebar {
    display: none !important;
  }
  
  /* Fix iOS Safari 100vh bugs and manage overall layout */
  .app-container {
    height: 100vh;
    height: -webkit-fill-available;
    flex-direction: column;
    overflow: hidden;
  }
  
  .main-content {
    flex: 1;
    height: 100%;
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }
  
  /* Make the reader viewport fill space and scroll natively on iOS Safari */
  .reader-viewport {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 20px 16px 150px 16px;
  }
  
  .book-meta-header {
    margin-bottom: 20px;
    padding-bottom: 12px;
  }
  .book-meta-header h2 {
    font-size: 1.45rem;
  }
  
  .reader-viewport p {
    font-size: 1.15rem;
    margin-bottom: 1.3em;
    line-height: 1.8;
    color: #e4e4e7; /* Light high-contrast color for reading */
  }
  
  .welcome-screen {
    padding: 40px 16px;
    gap: 12px;
  }
  .welcome-screen h3 {
    font-size: 1.3rem;
  }
  .welcome-screen p {
    font-size: 0.85rem;
  }
  
  /* Show and style mobile upload button in top-bar */
  .mobile-upload-group {
    display: flex !important;
    margin-top: 4px;
  }
  
  .mobile-upload-btn {
    display: inline-block;
    background: var(--gradient-accent);
    color: #fff !important;
    text-align: center;
    padding: 10px 16px;
    border-radius: 10px;
    font-weight: 600;
    font-size: 0.82rem;
    cursor: pointer;
    border: 1px solid rgba(255, 255, 255, 0.1);
    box-shadow: 0 4px 12px rgba(6, 182, 212, 0.25);
    transition: var(--transition-fast);
  }
  
  .mobile-upload-btn:active {
    transform: scale(0.96);
  }
  
  .top-bar {
    padding-top: calc(40px + env(safe-area-inset-top)) !important;
    padding-left: 16px !important;
    padding-right: 16px !important;
    padding-bottom: 12px !important;
    gap: 12px;
    border-bottom: 1px solid var(--border-light);
  }
  
  .tts-settings {
    flex-direction: column;
    align-items: stretch;
    width: 100%;
    gap: 10px;
  }
  
  .control-group {
    width: 100%;
    display: flex;
    flex-direction: column;
    gap: 4px;
  }
  
  .custom-select {
    min-width: 100%;
    font-size: 0.82rem;
    padding: 10px;
  }
  
  .slider-wrapper {
    width: 100%;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
  }
  
  input[type="range"] {
    flex: 1;
    width: auto !important;
  }
  
  /* Redesign bottom controls to float comfortably above iOS navigation bar */
  .player-controls {
    position: fixed !important;
    transform: none !important;
    width: calc(100% - 32px) !important;
    left: 16px !important;
    right: 16px !important;
    bottom: calc(16px + env(safe-area-inset-bottom)) !important; /* Spacing for iOS Home Indicator */
    padding: 12px 16px !important;
    border-radius: 20px !important;
    background: rgba(15, 23, 42, 0.85) !important;
    border: 1px solid rgba(255, 255, 255, 0.08) !important;
    backdrop-filter: blur(25px) !important;
    -webkit-backdrop-filter: blur(25px) !important;
    box-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.5) !important;
    gap: 10px !important;
    flex-direction: column !important;
  }
  
  .progress-bar-container {
    width: 100%;
    order: -1; /* Place progress bar on top */
    margin-bottom: 4px;
  }
  
  .progress-stats {
    font-size: 0.72rem;
    min-width: auto;
  }
  
  .buttons-container {
    width: 100%;
    justify-content: space-around;
    gap: 10px;
  }
  
  .ctrl-btn {
    width: 40px;
    height: 40px;
  }
  .ctrl-btn svg {
    width: 14px;
    height: 14px;
  }
  
  .play-main {
    width: 50px;
    height: 50px;
  }
  .play-main svg {
    width: 22px;
    height: 22px;
  }
}

/* Bookshelf Layout & Styles */
.bookshelf-container {
  flex: 1;
  padding: 40px 6%;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 32px;
}

.bookshelf-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  padding-bottom: 24px;
  gap: 20px;
  flex-wrap: wrap;
}

.bookshelf-title h1 {
  font-size: 2rem;
  font-weight: 800;
  background: var(--gradient-accent);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  margin-bottom: 6px;
}

.bookshelf-title .subtitle {
  font-size: 0.9rem;
  color: var(--text-dim);
}

.bookshelf-upload-btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  background: var(--gradient-accent);
  color: #fff;
  padding: 12px 24px;
  border-radius: 12px;
  font-weight: 600;
  font-size: 0.9rem;
  cursor: pointer;
  box-shadow: 0 4px 15px rgba(6, 182, 212, 0.3);
  transition: var(--transition-smooth);
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.bookshelf-upload-btn svg {
  width: 20px;
  height: 20px;
  fill: currentColor;
}

.bookshelf-upload-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(6, 182, 212, 0.45);
}

.bookshelf-upload-btn:active {
  transform: translateY(0);
}

/* Books Grid */
.books-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 28px;
  padding-bottom: 60px;
}

.empty-shelf {
  grid-column: 1 / -1;
  text-align: center;
  padding: 80px 40px;
  background: rgba(255, 255, 255, 0.02);
  border: 2px dashed rgba(255, 255, 255, 0.08);
  border-radius: 20px;
}

/* Book Card Design (Uniform 2:3 ratio common ground size) */
.book-card {
  position: relative;
  background: rgba(30, 41, 59, 0.4);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.25);
  aspect-ratio: 2 / 3;
  display: flex;
  flex-direction: column;
  transition: var(--transition-smooth);
}

.book-card:hover {
  transform: translateY(-6px);
  border-color: rgba(6, 182, 212, 0.3);
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.35);
}

.book-cover-wrapper {
  position: relative;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 16px;
  box-sizing: border-box;
  overflow: hidden;
  background: #0f172a;
}

.book-cover-art {
  width: 64px;
  height: 84px;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 2px 4px 10px rgba(0, 0, 0, 0.3);
  position: relative;
}

.cover-letter {
  font-family: var(--font-display);
  font-size: 2.2rem;
  font-weight: 800;
  color: #fff;
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.5);
}

.book-cover-meta {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.book-cover-meta h3 {
  font-size: 1.15rem;
  font-weight: 700;
  color: #fff;
  line-height: 1.4;
  word-break: break-all;
}

.chapters-count {
  font-size: 0.8rem;
  color: #06b6d4;
  font-weight: 600;
}

.book-chapters-drawer {
  padding: 20px;
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 12px;
  background: rgba(15, 23, 42, 0.2);
}

.book-chapters-drawer h4 {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.chapters-list-ul {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 180px;
  overflow-y: auto;
  padding-right: 4px;
}

.chapters-list-ul::-webkit-scrollbar {
  width: 4px;
}

.chapters-list-ul::-webkit-scrollbar-thumb {
  background-color: rgba(255, 255, 255, 0.08);
  border-radius: 99px;
}

.chapter-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  background: rgba(255, 255, 255, 0.02);
  border: 1px solid rgba(255, 255, 255, 0.04);
  border-radius: 10px;
  cursor: pointer;
  transition: var(--transition-fast);
}

.chapter-item:hover {
  background: rgba(6, 182, 212, 0.08);
  border-color: rgba(6, 182, 212, 0.25);
  transform: translateX(4px);
}

.chapter-play-btn {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: rgba(6, 182, 212, 0.15);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: var(--transition-fast);
}

.chapter-item:hover .chapter-play-btn {
  background: #06b6d4;
}

.chapter-play-btn svg {
  width: 10px;
  height: 10px;
  fill: #06b6d4;
  transition: var(--transition-fast);
}

.chapter-item:hover .chapter-play-btn svg {
  fill: #fff;
}

.chapter-title-text {
  font-size: 0.85rem;
  color: #e2e8f0;
  font-weight: 500;
}

/* Reader Container */
.reader-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  height: 100%;
  overflow: hidden;
}

/* Back Button */
.back-btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid var(--border-light);
  color: var(--text-dim);
  padding: 8px 16px;
  border-radius: 10px;
  cursor: pointer;
  font-weight: 600;
  font-size: 0.85rem;
  transition: var(--transition-fast);
}

.back-btn svg {
  width: 16px;
  height: 16px;
  fill: currentColor;
}

.back-btn:hover {
  background: rgba(255, 255, 255, 0.08);
  border-color: rgba(255, 255, 255, 0.2);
  color: #fff;
}

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

.chapter-indicator {
  font-size: 0.9rem;
  color: #fff;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 250px;
}

/* Additional mobile bookshelf styling */
@media (max-width: 650px) {
  .bookshelf-container {
    padding-top: calc(40px + env(safe-area-inset-top)) !important;
    padding-left: 16px !important;
    padding-right: 16px !important;
    padding-bottom: 20px !important;
  }
  .bookshelf-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 16px;
  }
  .bookshelf-upload-btn {
    width: 100%;
    justify-content: center;
  }
  .books-grid {
    grid-template-columns: 1fr;
    gap: 20px;
  }
  .chapter-indicator {
    display: none;
  }
  .top-bar-left {
    width: auto;
  }
}

/* Delete Book Button */
.delete-book-btn {
  position: absolute;
  top: 12px;
  right: 12px;
  background: rgba(239, 68, 68, 0.15);
  border: none;
  border-radius: 50%;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s ease;
  z-index: 10;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}
.delete-book-btn svg {
  width: 16px;
  height: 16px;
  fill: #ef4444;
  transition: fill 0.2s ease;
}
.delete-book-btn:hover {
  background: rgba(239, 68, 68, 0.9);
  transform: scale(1.08);
}
.delete-book-btn:hover svg {
  fill: #ffffff;
}

/* Settings Toggle Button */
.book-settings-toggle-btn {
  position: absolute;
  top: 12px;
  left: 12px;
  background: rgba(255, 255, 255, 0.1);
  border: none;
  border-radius: 50%;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s ease;
  z-index: 10;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}
.book-settings-toggle-btn svg {
  width: 16px;
  height: 16px;
  fill: rgba(255, 255, 255, 0.8);
  transition: all 0.3s ease;
}
.book-settings-toggle-btn:hover {
  background: rgba(255, 255, 255, 0.25);
  transform: scale(1.08);
}
.book-settings-toggle-btn:hover svg {
  fill: #ffffff;
  transform: rotate(45deg);
}

/* Book Card Settings Panel Overlay (Inside Gear Settings Button) */
.book-card-settings {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(15, 23, 42, 0.97);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  z-index: 25;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  padding: 16px;
  box-sizing: border-box;
  overflow-y: auto;
  transform: translateY(-100%);
  opacity: 0;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  pointer-events: none;
}
.book-card.show-settings .book-card-settings {
  transform: translateY(0);
  opacity: 1;
  pointer-events: auto;
}
.settings-overlay-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  padding-bottom: 6px;
}
.settings-overlay-header h4 {
  margin: 0;
  font-size: 0.9rem;
  color: var(--primary-light);
}
.settings-close-btn {
  background: rgba(255, 255, 255, 0.1);
  border: none;
  color: var(--text-light);
  font-size: 0.75rem;
  cursor: pointer;
  padding: 4px 10px;
  border-radius: 6px;
  transition: all 0.2s;
}
.settings-close-btn:hover {
  color: #fff;
  background: var(--primary);
}
.card-setting-item {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 12px;
}
.card-setting-item label {
  font-size: 0.78rem;
  color: var(--text-dim);
  font-weight: 500;
  margin-bottom: 0;
}

.card-voice-settings-row select {
  flex: 1;
}

.speed-inline {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.75rem;
  color: var(--text-dim);
  white-space: nowrap;
}

.speed-inline input[type="range"] {
  width: 70px;
}
.custom-select-small {
  background: rgba(15, 23, 42, 0.6);
  border: 1px solid rgba(255, 255, 255, 0.1);
  color: var(--text-light);
  padding: 4px 6px;
  border-radius: 6px;
  font-size: 0.76rem;
  outline: none;
  cursor: pointer;
  max-width: 140px;
  transition: border-color 0.2s;
}
.custom-select-small:focus {
  border-color: var(--primary);
}
.book-speed-slider {
  width: 90px;
  margin: 0 !important;
  cursor: pointer;
}
.book-speed-val {
  color: var(--primary-light);
  font-weight: 700;
}

/* Hide Settings inside Reader Top Bar */
#reader-view .tts-settings {
  display: none !important;
}

/* Sidebar Resume Playback Button */
.sidebar-resume-btn {
  width: 100%;
  margin-top: 12px;
  padding: 10px 14px;
  background: linear-gradient(135deg, var(--primary), #3b82f6);
  border: none;
  border-radius: 8px;
  color: #ffffff;
  font-weight: 600;
  font-size: 0.82rem;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(59, 130, 246, 0.3);
  transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}
.sidebar-resume-btn svg {
  width: 16px;
  height: 16px;
  fill: #ffffff;
}
.sidebar-resume-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(59, 130, 246, 0.45);
  background: linear-gradient(135deg, #2563eb, #1d4ed8);
}
.sidebar-resume-btn:active {
  transform: translateY(0);
}

/* Per-Book Resume Play Button on Cover (32px Circle) */
.book-card-play-btn {
  position: absolute;
  bottom: 12px;
  right: 12px;
  background: linear-gradient(135deg, var(--primary), #3b82f6);
  border: none;
  border-radius: 50%;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s ease;
  z-index: 10;
  box-shadow: 0 4px 12px rgba(59, 130, 246, 0.4);
}
.book-card-play-btn svg {
  width: 14px;
  height: 14px;
  fill: #ffffff;
  margin-left: 2px; /* Center play triangle */
  transition: transform 0.2s ease;
}
.book-card-play-btn:hover {
  background: linear-gradient(135deg, #2563eb, #1d4ed8);
  transform: scale(1.1);
  box-shadow: 0 6px 16px rgba(59, 130, 246, 0.6);
}
.book-card-play-btn:active {
  transform: scale(0.95);
}

/* Book Cover Image Support (100% Complete Display without Cropping) */
.cover-bg-blur {
  position: absolute;
  top: -10%;
  left: -10%;
  width: 120%;
  height: 120%;
  background-size: cover;
  background-position: center;
  filter: blur(25px) brightness(0.4);
  opacity: 0.85;
  z-index: 1;
}

.book-cover-img {
  position: relative;
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  object-fit: contain;
  z-index: 2;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
  border-radius: 6px;
}
.book-cover-wrapper.has-custom-cover .book-cover-art {
  display: none;
}
.book-cover-wrapper.has-custom-cover .book-cover-meta {
  z-index: 3;
}

/* Cover Modal Styling */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(0, 0, 0, 0.75);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  box-sizing: border-box;
}
.modal-card {
  background: #0f172a;
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 20px;
  width: 100%;
  max-width: 580px;
  max-height: 85vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.6);
  overflow: hidden;
  animation: modalFadeIn 0.3s ease-out;
}
@keyframes modalFadeIn {
  from { opacity: 0; transform: scale(0.95); }
  to { opacity: 1; transform: scale(1); }
}
.modal-header {
  padding: 16px 20px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.modal-header h3 {
  margin: 0;
  font-size: 1.05rem;
  color: var(--primary-light);
}
.modal-close-btn {
  background: transparent;
  border: none;
  color: var(--text-dim);
  font-size: 1.5rem;
  cursor: pointer;
  line-height: 1;
  transition: color 0.2s;
}
.modal-close-btn:hover {
  color: #ffffff;
}
.modal-body {
  padding: 20px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.cover-search-box, .cover-url-box {
  display: flex;
  gap: 10px;
}
.cover-search-box input, .cover-url-box input {
  flex: 1;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.15);
  color: #ffffff;
  padding: 8px 12px;
  border-radius: 8px;
  font-size: 0.85rem;
  outline: none;
}
.cover-search-box input:focus, .cover-url-box input:focus {
  border-color: var(--primary);
}
.cover-results-header {
  font-size: 0.82rem;
  color: var(--text-dim);
  margin-top: 4px;
  display: flex;
  justify-content: space-between;
}
.cover-results-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
  gap: 12px;
  max-height: 360px;
  overflow-y: auto;
  padding: 4px;
}
.cover-result-item {
  position: relative;
  width: 100%;
  height: 150px;
  border-radius: 8px;
  overflow: hidden;
  border: 2px solid rgba(255, 255, 255, 0.1);
  cursor: pointer;
  transition: all 0.2s ease;
  background: #1e293b;
  box-sizing: border-box;
}
.cover-result-item img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}
.cover-item-badge {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(15, 23, 42, 0.85);
  color: var(--text-dim);
  font-size: 0.65rem;
  padding: 2px 4px;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.cover-result-item:hover {
  transform: scale(1.04);
  border-color: var(--primary);
  box-shadow: 0 4px 12px rgba(59, 130, 246, 0.4);
}
.change-cover-btn {
  width: 100%;
  margin-top: 10px;
  background: rgba(59, 130, 246, 0.15);
  border: 1px solid rgba(59, 130, 246, 0.3);
  color: var(--primary-light);
  padding: 6px 10px;
  border-radius: 6px;
  font-size: 0.76rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
}
.change-cover-btn:hover {
  background: rgba(59, 130, 246, 0.35);
  color: #ffffff;
}

/* Pre-generate TTS Button & Progress Bar Styling */
.pregenerate-tts-btn {
  width: 100%;
  margin-top: 8px;
  background: linear-gradient(135deg, rgba(168, 85, 247, 0.2), rgba(6, 182, 212, 0.2));
  border: 1px solid rgba(6, 182, 212, 0.4);
  color: #06b6d4;
  padding: 8px 12px;
  border-radius: 8px;
  font-size: 0.8rem;
  font-weight: 700;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition: var(--transition-smooth);
}

.pregenerate-tts-btn:hover {
  background: linear-gradient(135deg, rgba(168, 85, 247, 0.4), rgba(6, 182, 212, 0.4));
  border-color: #06b6d4;
  color: #ffffff;
  box-shadow: 0 0 12px rgba(6, 182, 212, 0.3);
  transform: translateY(-1px);
}

.pregenerate-tts-btn.is-active {
  background: rgba(239, 68, 68, 0.2);
  border-color: rgba(239, 68, 68, 0.5);
  color: #ef4444;
}

.pregenerate-progress-container {
  margin-top: 10px;
  background: rgba(15, 23, 42, 0.85);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 10px;
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.pregen-progress-label {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.75rem;
}

.pregen-status-text {
  color: var(--text-dim);
  font-weight: 500;
  max-width: 80%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.pregen-percentage {
  color: #06b6d4;
  font-weight: 800;
  font-size: 0.85rem;
}

.pregen-bar-wrapper {
  height: 8px;
  width: 100%;
  background: rgba(255, 255, 255, 0.08);
  border-radius: 9999px;
  overflow: hidden;
}

.pregen-bar-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, #a855f7, #06b6d4);
  border-radius: 9999px;
  transition: width 0.25s ease-out;
  box-shadow: 0 0 10px rgba(6, 182, 212, 0.5);
}

.pregen-bar-fill.complete {
  background: linear-gradient(90deg, #10b981, #34d399);
  box-shadow: 0 0 10px rgba(16, 185, 129, 0.5);
}

/* Mobile & Desktop Version Badge Styling */
.title-with-badge {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.version-badge {
  background: linear-gradient(135deg, #a855f7, #06b6d4);
  color: #ffffff;
  font-size: 0.72rem;
  font-weight: 700;
  padding: 3px 10px;
  border-radius: 20px;
  letter-spacing: 0.03em;
  box-shadow: 0 0 10px rgba(6, 182, 212, 0.35);
  display: inline-block;
  vertical-align: middle;
}

/* Card Front Pre-generation Footer (Always Visible on Book Cover) */
.card-pregen-footer {
  position: absolute;
  bottom: 12px;
  left: 12px;
  right: 12px;
  z-index: 10;
  display: flex;
  flex-direction: column;
  gap: 6px;
  pointer-events: auto;
}

.card-pregen-footer .pregenerate-tts-btn {
  margin-top: 0;
  backdrop-filter: blur(12px);
  background: rgba(15, 23, 42, 0.85);
  border: 1px solid rgba(6, 182, 212, 0.5);
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.4);
}

.card-pregen-footer .pregenerate-tts-btn:hover {
  background: linear-gradient(135deg, rgba(168, 85, 247, 0.6), rgba(6, 182, 212, 0.6));
  box-shadow: 0 0 16px rgba(6, 182, 212, 0.5);
}

.card-pregen-footer .pregenerate-progress-container {
  margin-top: 0;
  backdrop-filter: blur(12px);
  background: rgba(15, 23, 42, 0.9);
  border: 1px solid rgba(255, 255, 255, 0.15);
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.5);
}



