:root {
  --bg-primary: #0f1117;
  --bg-secondary: #161922;
  --bg-tertiary: #1f2430;
  --text-primary: #e6edf3;
  --text-secondary: #8b949e;
  --text-muted: #6e7681;
  --border-color: #30363d;
  --accent-color: #8a63d2;
  --accent-glow: rgba(138, 99, 210, 0.25);
  --active-cue-bg: rgba(138, 99, 210, 0.15);
  --active-cue-border: #a87ffb;
  --font-serif: 'Lora', Georgia, 'Times New Roman', serif;
  --font-sans: 'Plus Jakarta Sans', system-ui, -apple-system, sans-serif;
  --font-display: 'Cinzel', serif;
  --font-mono: 'JetBrains Mono', monospace;
  --reader-font-size: 19px;
  --reader-max-width: 780px;

  /* Character Role Palette */
  --role-narrator: #94a3b8;
  --role-an-khue: #c084fc;
  --role-vu-dinh-mac: #60a5fa;
  --role-tan-phong: #fbbf24;
  --role-thanh-lam: #f472b6;
  --role-minh-triet: #34d399;
  --role-ba-nam-ha: #2dd4bf;
  --role-sfx: #fb923c;
  --role-inner: #a78bfa;
}

body.theme-sepia {
  --bg-primary: #f4ecd8;
  --bg-secondary: #ebdcc0;
  --bg-tertiary: #dfccad;
  --text-primary: #2d261e;
  --text-secondary: #5c4e3e;
  --text-muted: #8c7860;
  --border-color: #d1bda2;
  --accent-color: #8c531b;
  --accent-glow: rgba(140, 83, 27, 0.2);
  --active-cue-bg: rgba(140, 83, 27, 0.12);
  --active-cue-border: #8c531b;
  --role-narrator: #64748b;
  --role-an-khue: #7e22ce;
  --role-vu-dinh-mac: #1d4ed8;
  --role-tan-phong: #b45309;
  --role-thanh-lam: #be185d;
  --role-minh-triet: #047857;
  --role-ba-nam-ha: #0f766e;
}

body.theme-light {
  --bg-primary: #ffffff;
  --bg-secondary: #f6f8fa;
  --bg-tertiary: #eaeef2;
  --text-primary: #1f2328;
  --text-secondary: #59636e;
  --text-muted: #818b98;
  --border-color: #d0d7de;
  --accent-color: #6366f1;
  --accent-glow: rgba(99, 102, 241, 0.2);
  --active-cue-bg: rgba(99, 102, 241, 0.1);
  --active-cue-border: #4f46e5;
  --role-narrator: #475569;
  --role-an-khue: #9333ea;
  --role-vu-dinh-mac: #2563eb;
  --role-tan-phong: #d97706;
  --role-thanh-lam: #db2777;
  --role-minh-triet: #059669;
  --role-ba-nam-ha: #0d9488;
}

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

body {
  background-color: var(--bg-primary);
  color: var(--text-primary);
  font-family: var(--font-sans);
  line-height: 1.6;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  transition: background-color 0.25s ease, color 0.25s ease;
}

/* Header */
.studio-header {
  background-color: var(--bg-secondary);
  border-bottom: 1px solid var(--border-color);
  padding: 0.75rem 1.5rem;
  position: sticky;
  top: 0;
  z-index: 100;
}

.header-inner {
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.brand-link {
  text-decoration: none;
}

.brand-badge {
  font-size: 0.75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  background: linear-gradient(135deg, #a855f7, #6366f1);
  color: #ffffff;
  padding: 0.25rem 0.6rem;
  border-radius: 4px;
}

.divider {
  color: var(--text-muted);
}

.book-title {
  font-family: var(--font-display);
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--text-primary);
  letter-spacing: 0.04em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.btn-icon {
  background: var(--bg-tertiary);
  border: 1px solid var(--border-color);
  color: var(--text-primary);
  width: 34px;
  height: 34px;
  border-radius: 6px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.85rem;
  font-weight: 600;
  transition: all 0.15s ease;
}

.btn-icon:hover {
  border-color: var(--accent-color);
  transform: translateY(-1px);
}

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

.btn-comic-nav {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  background: var(--bg-tertiary);
  border: 1px solid var(--border-color);
  color: var(--text-primary);
  text-decoration: none;
  font-size: 0.8rem;
  font-weight: 600;
  padding: 0.4rem 0.75rem;
  border-radius: 6px;
  transition: all 0.15s ease;
}

.btn-comic-nav:hover {
  border-color: var(--accent-color);
  color: var(--accent-color);
}

/* Toolbar */
.reader-toolbar {
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border-color);
  padding: 0.5rem 1.5rem;
}

.toolbar-inner {
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 0.75rem;
}

.mode-selector {
  display: flex;
  background: var(--bg-tertiary);
  padding: 3px;
  border-radius: 8px;
  border: 1px solid var(--border-color);
  gap: 2px;
}

.mode-tab {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  background: transparent;
  border: none;
  color: var(--text-secondary);
  padding: 0.4rem 0.85rem;
  font-size: 0.85rem;
  font-weight: 600;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.2s ease;
}

.mode-tab:hover {
  color: var(--text-primary);
}

.mode-tab.active {
  background: var(--bg-primary);
  color: var(--text-primary);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
}

.chapter-nav {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.btn-nav {
  background: var(--bg-tertiary);
  border: 1px solid var(--border-color);
  color: var(--text-primary);
  padding: 0.4rem 0.75rem;
  font-size: 0.8rem;
  font-weight: 600;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.15s ease;
}

.btn-nav:hover:not(:disabled) {
  border-color: var(--accent-color);
}

.btn-nav:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

#chapter-select {
  background: var(--bg-tertiary);
  border: 1px solid var(--border-color);
  color: var(--text-primary);
  padding: 0.4rem 0.75rem;
  font-size: 0.85rem;
  border-radius: 6px;
  outline: none;
  cursor: pointer;
  max-width: 260px;
}

/* Audio Player Bar */
.audio-bar {
  background: var(--bg-secondary);
  border-bottom: 2px solid var(--accent-color);
  padding: 0.75rem 1.5rem;
  animation: slideDown 0.2s ease;
}

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

.audio-bar-inner {
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 1rem;
}

.audio-meta {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  font-size: 0.85rem;
}

.audio-status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text-muted);
}

.audio-status-dot.active {
  background: #22c55e;
  box-shadow: 0 0 8px #22c55e;
}

.audio-time {
  font-family: var(--font-mono);
  color: var(--text-secondary);
  font-size: 0.8rem;
}

.audio-player-wrapper {
  flex: 1;
  min-width: 240px;
  max-width: 500px;
}

.audio-player-wrapper audio {
  width: 100%;
  height: 36px;
}

.audio-extra-controls {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.btn-secondary {
  background: var(--bg-tertiary);
  border: 1px solid var(--border-color);
  color: var(--text-secondary);
  padding: 0.35rem 0.65rem;
  font-size: 0.75rem;
  border-radius: 5px;
  text-decoration: none;
  cursor: pointer;
}

.btn-secondary:hover {
  color: var(--text-primary);
  border-color: var(--accent-color);
}

#playback-speed {
  background: var(--bg-tertiary);
  border: 1px solid var(--border-color);
  color: var(--text-primary);
  padding: 0.35rem 0.5rem;
  font-size: 0.75rem;
  border-radius: 5px;
}

/* Main Reader Shell */
.reader-shell {
  flex: 1;
  width: 100%;
  max-width: var(--reader-max-width);
  margin: 0 auto;
  padding: 2.5rem 1.5rem 4rem;
}

.reader-header-block {
  text-align: center;
  margin-bottom: 2.5rem;
  padding-bottom: 2rem;
  border-bottom: 1px solid var(--border-color);
}

.chapter-eyebrow {
  font-family: var(--font-mono);
  font-size: 0.85rem;
  letter-spacing: 0.15em;
  color: var(--accent-color);
  text-transform: uppercase;
  font-weight: 600;
  margin-bottom: 0.5rem;
}

.chapter-heading {
  font-family: var(--font-display);
  font-size: 1.85rem;
  line-height: 1.35;
  color: var(--text-primary);
  margin-bottom: 1rem;
}

.chapter-metrics {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.6rem;
  flex-wrap: wrap;
}

.badge {
  display: inline-flex;
  align-items: center;
  padding: 0.2rem 0.55rem;
  border-radius: 4px;
  font-size: 0.75rem;
  font-family: var(--font-mono);
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  color: var(--text-secondary);
}

/* Cast Legend */
.cast-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem;
  margin-bottom: 2rem;
  padding: 0.85rem 1rem;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 8px;
}

.cast-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.25rem 0.55rem;
  border-radius: 20px;
  font-size: 0.75rem;
  font-weight: 600;
  background: var(--bg-tertiary);
  border: 1px solid var(--border-color);
}

.cast-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
}

/* Reader Content Base */
.reader-content {
  position: relative;
}

.loading-spinner {
  text-align: center;
  padding: 4rem 1rem;
  color: var(--text-muted);
}

.spinner {
  width: 36px;
  height: 36px;
  border: 3px solid var(--border-color);
  border-top-color: var(--accent-color);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin: 0 auto 1rem;
}

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

/* ==========================================================================
   MODE 1: LITERARY VIEW (Chế độ Văn học)
   ========================================================================== */
.mode-literary {
  font-family: var(--font-serif);
  font-size: var(--reader-font-size);
  line-height: 1.85;
}

.mode-literary .reading-unit {
  position: relative;
  margin-bottom: 1.25rem;
  text-align: justify;
  transition: background 0.15s ease, border 0.15s ease;
  border-radius: 4px;
  padding: 2px 4px;
}

.mode-literary .unit-dialogue {
  color: var(--text-primary);
}

.mode-literary .unit-narrative {
  color: var(--text-secondary);
}

.mode-literary .unit-inner {
  font-style: italic;
  color: var(--text-secondary);
}

.mode-literary .unit-sfx {
  font-family: var(--font-mono);
  font-size: 0.9em;
  color: var(--role-sfx);
  font-style: italic;
}

/* Hover pill in literary view */
.mode-literary .unit-pill {
  position: absolute;
  top: -12px;
  right: 8px;
  display: none;
  align-items: center;
  gap: 4px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border-color);
  padding: 1px 6px;
  border-radius: 10px;
  font-family: var(--font-sans);
  font-size: 0.65rem;
  font-weight: 600;
  color: var(--text-primary);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.25);
  pointer-events: none;
  z-index: 10;
}

.mode-literary .reading-unit:hover {
  background: rgba(255, 255, 255, 0.03);
}

.mode-literary .reading-unit:hover .unit-pill {
  display: inline-flex;
}

/* ==========================================================================
   MODE 2: SCREENPLAY VIEW (Chế độ Kịch bản)
   ========================================================================== */
.mode-screenplay {
  font-family: var(--font-sans);
  font-size: calc(var(--reader-font-size) - 1px);
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.mode-screenplay .reading-unit {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  padding: 0.85rem 1.1rem;
  border-radius: 8px;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  position: relative;
  transition: all 0.2s ease;
}

.mode-screenplay .unit-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  margin-bottom: 0.25rem;
}

.mode-screenplay .speaker-badge {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.8rem;
  font-weight: 700;
  letter-spacing: 0.02em;
}

.mode-screenplay .speaker-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
}

.mode-screenplay .cue-time {
  font-family: var(--font-mono);
  font-size: 0.7rem;
  color: var(--text-muted);
}

.mode-screenplay .unit-body {
  line-height: 1.6;
}

.mode-screenplay .unit-dialogue {
  border-left: 3px solid var(--accent-color);
  padding-left: 0.75rem;
  margin-left: 0.2rem;
}

.mode-screenplay .unit-narrative {
  background: transparent;
  border: none;
  padding: 0.4rem 0;
  color: var(--text-secondary);
  font-style: italic;
  font-size: 0.92em;
}

.mode-screenplay .unit-inner {
  border-left: 3px dashed var(--role-inner);
  background: rgba(167, 139, 250, 0.05);
  padding: 0.5rem 0.8rem;
  border-radius: 0 6px 6px 0;
}

.mode-screenplay .unit-inner .unit-body::before {
  content: "💭 (Độc thoại) ";
  color: var(--role-inner);
  font-weight: 600;
}

.mode-screenplay .unit-sfx {
  background: rgba(251, 146, 60, 0.08);
  border: 1px dashed var(--role-sfx);
  padding: 0.5rem 0.8rem;
  border-radius: 6px;
  font-family: var(--font-mono);
  font-size: 0.85em;
  color: var(--role-sfx);
}

.mode-screenplay .unit-sfx .unit-body::before {
  content: "🔊 [Âm cảnh] ";
  font-weight: 700;
}

/* ==========================================================================
   MODE 3: AUDIO SYNC VIEW (Nghe đồng bộ Karaoke)
   ========================================================================== */
.mode-audio {
  font-family: var(--font-serif);
  font-size: var(--reader-font-size);
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
}

.mode-audio .reading-unit {
  padding: 0.75rem 1rem;
  border-radius: 8px;
  border: 1px solid transparent;
  border-left: 4px solid var(--border-color);
  background: var(--bg-secondary);
  cursor: pointer;
  transition: all 0.25s ease;
  position: relative;
}

.mode-audio .reading-unit:hover {
  border-color: var(--border-color);
  background: var(--bg-tertiary);
}

.mode-audio .unit-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.35rem;
}

.mode-audio .speaker-badge {
  font-family: var(--font-sans);
  font-size: 0.75rem;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
}

.mode-audio .cue-time {
  font-family: var(--font-mono);
  font-size: 0.75rem;
  color: var(--text-muted);
}

.mode-audio .unit-body {
  line-height: 1.7;
}

/* Active Highlight Karaoke */
.reading-unit.is-active-cue {
  background: var(--active-cue-bg) !important;
  border-left-color: var(--active-cue-border) !important;
  border-color: var(--active-cue-border) !important;
  box-shadow: 0 0 15px var(--accent-glow);
  transform: scale(1.01);
}

.reading-unit.is-active-cue .unit-body {
  color: #ffffff !important;
  font-weight: 500;
}

body.theme-light .reading-unit.is-active-cue .unit-body,
body.theme-sepia .reading-unit.is-active-cue .unit-body {
  color: var(--text-primary) !important;
}

/* Footer Navigation */
.chapter-footer {
  margin-top: 3.5rem;
  padding-top: 2rem;
  border-top: 1px solid var(--border-color);
}

.chapter-footer-nav {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
}

.btn-nav.large {
  padding: 0.75rem 1.5rem;
  font-size: 0.95rem;
}

.audio-bar[hidden] {
  display: none !important;
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal[hidden] {
  display: none !important;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
  backdrop-filter: blur(4px);
}

.modal-card {
  position: relative;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  padding: 1.5rem;
  border-radius: 12px;
  max-width: 480px;
  width: 90%;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
}

.modal-card h3 {
  margin-bottom: 0.5rem;
  font-size: 1.1rem;
}

.modal-card p {
  color: var(--text-secondary);
  font-size: 0.85rem;
  margin-bottom: 1rem;
}

.input-text {
  width: 100%;
  background: var(--bg-primary);
  border: 1px solid var(--border-color);
  color: var(--text-primary);
  padding: 0.65rem 0.85rem;
  border-radius: 6px;
  font-size: 0.85rem;
  margin-bottom: 1.25rem;
  outline: none;
}

.input-text:focus {
  border-color: var(--accent-color);
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
}

.btn-primary {
  background: var(--accent-color);
  color: #fff;
  border: none;
  padding: 0.5rem 1rem;
  border-radius: 6px;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
}

/* Mobile Responsive */
@media (max-width: 768px) {
  .studio-header {
    padding: 0.5rem 1rem;
  }
  .book-title {
    font-size: 0.95rem;
    max-width: 140px;
  }
  .reader-toolbar {
    padding: 0.5rem 1rem;
  }
  .toolbar-inner {
    flex-direction: column;
    align-items: stretch;
  }
  .mode-selector {
    width: 100%;
    justify-content: space-between;
  }
  .mode-tab {
    flex: 1;
    justify-content: center;
    padding: 0.4rem 0.5rem;
    font-size: 0.75rem;
  }
  .chapter-nav {
    justify-content: space-between;
  }
  #chapter-select {
    flex: 1;
    max-width: none;
  }
  .reader-shell {
    padding: 1.5rem 1rem 3rem;
  }
  .chapter-heading {
    font-size: 1.4rem;
  }
  .audio-bar {
    padding: 0.65rem 1rem;
  }
  .audio-bar-inner {
    flex-direction: column;
    align-items: stretch;
  }
  .audio-player-wrapper {
    max-width: none;
  }
}
