/* ═══════════════════════════════════════════════════
   DESIGN TOKENS & RESET
   ═══════════════════════════════════════════════════ */

:root {
  /* Typography */
  --font-ui: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  --font-reading: 'Lora', 'Georgia', serif;
  --font-mono: 'JetBrains Mono', monospace;

  /* Spacing */
  --space-xs: 4px;
  --space-sm: 8px;
  --space-md: 16px;
  --space-lg: 24px;
  --space-xl: 32px;
  --space-2xl: 48px;
  --space-3xl: 64px;
  --space-4xl: 96px;

  /* Sizing */
  --reading-width: 680px;
  --header-height: 56px;

  /* Transitions */
  --ease-out: cubic-bezier(0.16, 1, 0.3, 1);
  --ease-spring: cubic-bezier(0.34, 1.56, 0.64, 1);
  --duration-fast: 150ms;
  --duration-normal: 250ms;
  --duration-slow: 400ms;

  /* Radius */
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;
  --radius-xl: 24px;
  --radius-full: 9999px;

  /* Font size base for reader */
  --reader-font-size: 19px;
  --reader-line-height: 1.8;
}

/* ─── Dark Theme (default) ─── */
[data-theme="dark"] {
  --bg-primary: #0a0a0f;
  --bg-secondary: #12121a;
  --bg-tertiary: #1a1a26;
  --bg-elevated: #1e1e2e;
  --bg-hover: #252536;
  --bg-active: #2a2a3e;

  --text-primary: #e8e6f0;
  --text-secondary: #9e9bb0;
  --text-tertiary: #6b6882;
  --text-reading: #d4d2e0;

  --border-subtle: rgba(255, 255, 255, 0.06);
  --border-default: rgba(255, 255, 255, 0.1);
  --border-strong: rgba(255, 255, 255, 0.16);

  --accent: #6c8aff;
  --accent-soft: rgba(108, 138, 255, 0.12);
  --accent-glow: rgba(108, 138, 255, 0.25);

  --highlight-bg: rgba(108, 138, 255, 0.2);
  --highlight-border: rgba(108, 138, 255, 0.5);

  --popover-bg: #1a1a2e;
  --popover-border: rgba(108, 138, 255, 0.2);
  --popover-shadow: 0 20px 60px rgba(0, 0, 0, 0.6), 0 0 40px rgba(108, 138, 255, 0.08);

  --progress-bg: rgba(255, 255, 255, 0.06);
  --progress-fill: linear-gradient(90deg, #6c8aff, #a78bfa);

  --cover-overlay: rgba(0, 0, 0, 0.3);

  --scrollbar-thumb: rgba(255, 255, 255, 0.12);
  --scrollbar-thumb-hover: rgba(255, 255, 255, 0.2);
}

/* ─── Light Theme ─── */
[data-theme="light"] {
  --bg-primary: #faf9fc;
  --bg-secondary: #f3f2f7;
  --bg-tertiary: #eceaf2;
  --bg-elevated: #ffffff;
  --bg-hover: #eae8f0;
  --bg-active: #ddd9e8;

  --text-primary: #1a1725;
  --text-secondary: #5e5a6e;
  --text-tertiary: #8e8a9e;
  --text-reading: #2a2638;

  --border-subtle: rgba(0, 0, 0, 0.04);
  --border-default: rgba(0, 0, 0, 0.08);
  --border-strong: rgba(0, 0, 0, 0.14);

  --accent: #4f6ae6;
  --accent-soft: rgba(79, 106, 230, 0.08);
  --accent-glow: rgba(79, 106, 230, 0.15);

  --highlight-bg: rgba(79, 106, 230, 0.12);
  --highlight-border: rgba(79, 106, 230, 0.4);

  --popover-bg: #ffffff;
  --popover-border: rgba(79, 106, 230, 0.15);
  --popover-shadow: 0 20px 60px rgba(0, 0, 0, 0.12), 0 0 40px rgba(79, 106, 230, 0.06);

  --progress-bg: rgba(0, 0, 0, 0.06);
  --progress-fill: linear-gradient(90deg, #4f6ae6, #7c5ce0);

  --cover-overlay: rgba(0, 0, 0, 0.08);

  --scrollbar-thumb: rgba(0, 0, 0, 0.12);
  --scrollbar-thumb-hover: rgba(0, 0, 0, 0.22);
}

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

html {
  scroll-behavior: smooth;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

body {
  font-family: var(--font-ui);
  background: var(--bg-primary);
  color: var(--text-primary);
  line-height: 1.6;
  overflow-x: hidden;
  transition: background var(--duration-normal) var(--ease-out),
    color var(--duration-normal) var(--ease-out);
}

/* Custom scrollbar */
::-webkit-scrollbar {
  width: 8px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: var(--scrollbar-thumb);
  border-radius: var(--radius-full);
}

::-webkit-scrollbar-thumb:hover {
  background: var(--scrollbar-thumb-hover);
}

button {
  font-family: inherit;
  cursor: pointer;
  border: none;
  background: none;
  color: inherit;
}

a {
  color: inherit;
  text-decoration: none;
}

/* ─── View system ─── */
.view {
  display: none;
  min-height: 100vh;
}

.view.active {
  display: block;
  animation: fadeIn var(--duration-slow) var(--ease-out);
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(8px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* ═══════════════════════════════════════════════════
   SHARED COMPONENTS
   ═══════════════════════════════════════════════════ */

.icon-btn {
  width: 40px;
  height: 40px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-md);
  color: var(--text-secondary);
  transition: all var(--duration-fast) var(--ease-out);
}

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

.btn-ghost {
  padding: 8px 18px;
  border-radius: var(--radius-full);
  font-size: 14px;
  font-weight: 500;
  color: var(--text-secondary);
  border: 1px solid var(--border-default);
  transition: all var(--duration-fast) var(--ease-out);
}

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

/* Theme toggle — show/hide sun/moon */
[data-theme="dark"] .icon-sun {
  display: none;
}

[data-theme="dark"] .icon-moon {
  display: block;
}

[data-theme="light"] .icon-sun {
  display: block;
}

[data-theme="light"] .icon-moon {
  display: none;
}

.gradient-text {
  background: linear-gradient(135deg, #6c8aff 0%, #a78bfa 50%, #e879a8 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

/* ═══════════════════════════════════════════════════
   LIBRARY VIEW
   ═══════════════════════════════════════════════════ */

/* ─── Library Header ─── */
.library-header {
  position: sticky;
  top: 0;
  z-index: 100;
  background: rgba(10, 10, 15, 0.7);
  backdrop-filter: blur(20px) saturate(180%);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  border-bottom: 1px solid var(--border-subtle);
}

[data-theme="light"] .library-header {
  background: rgba(250, 249, 252, 0.75);
}

.library-header-inner {
  max-width: 1080px;
  margin: 0 auto;
  padding: 0 var(--space-lg);
  height: var(--header-height);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.logo {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
}

.logo-icon {
  color: var(--accent);
}

.logo-text {
  font-size: 18px;
  font-weight: 600;
  letter-spacing: -0.02em;
}

.library-header-actions {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
}

/* ─── Hero ─── */
.library-hero {
  position: relative;
  padding: var(--space-4xl) var(--space-lg) var(--space-3xl);
  text-align: center;
  overflow: hidden;
}

.hero-content {
  position: relative;
  z-index: 1;
  max-width: 640px;
  margin: 0 auto;
}

.hero-title {
  font-size: clamp(36px, 6vw, 56px);
  font-weight: 700;
  line-height: 1.15;
  letter-spacing: -0.03em;
  margin-bottom: var(--space-lg);
}

.hero-subtitle {
  font-size: clamp(16px, 2.2vw, 19px);
  color: var(--text-secondary);
  line-height: 1.7;
  max-width: 520px;
  margin: 0 auto;
}

.hero-glow {
  position: absolute;
  top: -60%;
  left: 50%;
  transform: translateX(-50%);
  width: 600px;
  height: 600px;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(108, 138, 255, 0.08) 0%, transparent 70%);
  pointer-events: none;
}

[data-theme="light"] .hero-glow {
  background: radial-gradient(circle, rgba(79, 106, 230, 0.06) 0%, transparent 70%);
}

/* ─── Sections ─── */
.library-section {
  max-width: 1080px;
  margin: 0 auto;
  padding: 0 var(--space-lg) var(--space-3xl);
}

.section-header {
  display: flex;
  align-items: center;
  gap: var(--space-md);
  margin-bottom: var(--space-xl);
}

.section-title {
  font-size: 22px;
  font-weight: 600;
  letter-spacing: -0.02em;
}

.section-badge {
  font-size: 12px;
  font-weight: 500;
  color: var(--accent);
  background: var(--accent-soft);
  padding: 4px 10px;
  border-radius: var(--radius-full);
}

/* ─── Book Grid ─── */
.book-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: var(--space-xl);
}

.book-card {
  text-align: left;
  border-radius: var(--radius-lg);
  padding: 0;
  transition: transform var(--duration-normal) var(--ease-spring);
  outline: none;
}

.book-card:hover {
  transform: translateY(-6px);
}

.book-card:focus-visible {
  box-shadow: 0 0 0 3px var(--accent-glow);
}

.book-cover {
  position: relative;
  aspect-ratio: 3 / 4.2;
  border-radius: var(--radius-lg);
  overflow: hidden;
  margin-bottom: var(--space-md);
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.25);
  transition: box-shadow var(--duration-normal) var(--ease-out);
}

.book-card:hover .book-cover {
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.35);
}

.cover-art {
  width: 100%;
  height: 100%;
  background: linear-gradient(160deg,
      hsl(var(--accent), 18%) 0%,
      hsl(var(--accent), 8%) 100%);
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  padding: var(--space-lg);
  position: relative;
}

.cover-pattern {
  position: absolute;
  inset: 0;
  opacity: 0.06;
  background-image:
    radial-gradient(circle at 20% 30%, hsl(var(--accent)) 1px, transparent 1px),
    radial-gradient(circle at 60% 70%, hsl(var(--accent)) 1px, transparent 1px),
    radial-gradient(circle at 80% 20%, hsl(var(--accent)) 1px, transparent 1px);
  background-size: 40px 40px, 60px 60px, 35px 35px;
}

.cover-text {
  display: flex;
  flex-direction: column;
  gap: var(--space-xs);
  position: relative;
  z-index: 1;
}

.cover-author {
  font-size: 11px;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  opacity: 0.7;
  color: #fff;
}

.cover-title {
  font-family: var(--font-reading);
  font-size: 20px;
  font-weight: 600;
  line-height: 1.3;
  color: #fff;
}

/* Progress ring on book cover */
.book-progress-ring {
  position: absolute;
  top: var(--space-md);
  right: var(--space-md);
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.book-progress-ring svg {
  width: 40px;
  height: 40px;
  transform: rotate(-90deg);
}

.progress-bg {
  fill: none;
  stroke: rgba(255, 255, 255, 0.15);
  stroke-width: 3;
}

.progress-fill {
  fill: none;
  stroke: #fff;
  stroke-width: 3;
  stroke-linecap: round;
  transition: stroke-dasharray var(--duration-slow) var(--ease-out);
}

.progress-label {
  position: absolute;
  font-size: 9px;
  font-weight: 600;
  color: #fff;
  letter-spacing: -0.02em;
}

.book-info {
  padding: 0 var(--space-xs);
}

.book-title {
  font-size: 15px;
  font-weight: 600;
  letter-spacing: -0.01em;
  margin-bottom: 2px;
}

.book-author {
  font-size: 13px;
  color: var(--text-secondary);
  margin-bottom: var(--space-xs);
}

.book-meta {
  font-size: 12px;
  color: var(--text-tertiary);
}

/* ─── Features ─── */
.features-section {
  max-width: 1080px;
  margin: 0 auto;
  padding: 0 var(--space-lg) var(--space-4xl);
}

.features-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: var(--space-lg);
}

.feature-card {
  padding: var(--space-xl);
  background: var(--bg-secondary);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  transition: border-color var(--duration-normal) var(--ease-out),
    transform var(--duration-normal) var(--ease-spring);
}

.feature-card:hover {
  border-color: var(--border-default);
  transform: translateY(-3px);
}

.feature-icon {
  width: 44px;
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: hsla(var(--fg), 0.1);
  color: hsl(var(--fg));
  border-radius: var(--radius-md);
  margin-bottom: var(--space-md);
}

.feature-card h3 {
  font-size: 16px;
  font-weight: 600;
  margin-bottom: var(--space-sm);
  letter-spacing: -0.01em;
}

.feature-card p {
  font-size: 14px;
  color: var(--text-secondary);
  line-height: 1.6;
}


/* ═══════════════════════════════════════════════════
   READER VIEW
   ═══════════════════════════════════════════════════ */

/* ─── Reader Header ─── */
.reader-header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 200;
  background: rgba(10, 10, 15, 0.8);
  backdrop-filter: blur(20px) saturate(180%);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  border-bottom: 1px solid var(--border-subtle);
  transition: transform var(--duration-normal) var(--ease-out),
    background var(--duration-normal) var(--ease-out);
}

[data-theme="light"] .reader-header {
  background: rgba(250, 249, 252, 0.82);
}

.reader-header.hidden {
  transform: translateY(-100%);
}

.reader-header-inner {
  max-width: 100%;
  margin: 0 auto;
  padding: 0 var(--space-md);
  height: var(--header-height);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.reader-header-left,
.reader-header-right {
  display: flex;
  align-items: center;
  gap: var(--space-xs);
  min-width: 120px;
}

.reader-header-right {
  justify-content: flex-end;
}

.reader-header-center {
  display: flex;
  flex-direction: column;
  align-items: center;
  line-height: 1.3;
}

.reader-book-title {
  font-size: 13px;
  font-weight: 600;
  letter-spacing: -0.01em;
}

.reader-chapter-title {
  font-size: 11px;
  color: var(--text-tertiary);
}

/* ─── Reading Progress Bar ─── */
.reading-progress-bar {
  height: 2px;
  background: var(--progress-bg);
}

.reading-progress-fill {
  height: 100%;
  background: var(--progress-fill);
  border-radius: 0 var(--radius-full) var(--radius-full) 0;
  transition: width 80ms linear;
}

/* ─── TOC Sidebar ─── */
.toc-sidebar {
  position: fixed;
  inset: 0;
  z-index: 300;
  pointer-events: none;
  visibility: hidden;
}

.toc-sidebar.open {
  pointer-events: auto;
  visibility: visible;
}

.toc-overlay {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  opacity: 0;
  transition: opacity var(--duration-normal) var(--ease-out);
}

.toc-sidebar.open .toc-overlay {
  opacity: 1;
}

.toc-panel {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  width: 320px;
  max-width: 85vw;
  background: var(--bg-elevated);
  border-right: 1px solid var(--border-subtle);
  transform: translateX(-100%);
  transition: transform var(--duration-normal) var(--ease-out);
  display: flex;
  flex-direction: column;
  box-shadow: 20px 0 60px rgba(0, 0, 0, 0.3);
}

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

.toc-header {
  padding: var(--space-md) var(--space-lg);
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border-subtle);
  min-height: var(--header-height);
}

.toc-header h2 {
  font-size: 16px;
  font-weight: 600;
}

.toc-list {
  overflow-y: auto;
  flex: 1;
  padding: var(--space-sm) 0;
}

.toc-item {
  display: block;
  padding: var(--space-sm) var(--space-lg);
  font-size: 14px;
  color: var(--text-secondary);
  transition: all var(--duration-fast) var(--ease-out);
  border-left: 3px solid transparent;
}

.toc-item:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.toc-item.active {
  color: var(--accent);
  border-left-color: var(--accent);
  background: var(--accent-soft);
  font-weight: 500;
}

/* ─── Reader Content ─── */
.reader-content {
  max-width: var(--reading-width);
  margin: 0 auto;
  padding: calc(var(--header-height) + var(--space-3xl)) var(--space-lg) var(--space-4xl);
}

.chapter-heading {
  font-family: var(--font-reading);
  font-size: clamp(28px, 5vw, 36px);
  font-weight: 600;
  letter-spacing: -0.02em;
  margin-bottom: var(--space-2xl);
  color: var(--text-primary);
  position: relative;
}

.chapter-heading::after {
  content: '';
  display: block;
  width: 48px;
  height: 3px;
  background: var(--accent);
  border-radius: var(--radius-full);
  margin-top: var(--space-md);
  opacity: 0.6;
}

.block {
  font-family: var(--font-reading);
  font-size: var(--reader-font-size);
  line-height: var(--reader-line-height);
  color: var(--text-reading);
  margin-bottom: var(--space-lg);
  text-indent: 1.5em;
  transition: font-size var(--duration-normal) var(--ease-out);
}

.block:first-of-type {
  text-indent: 0;
}

/* Scene dividers (from JSON divider blocks) */
.scene-divider {
  border: none;
  text-align: center;
  margin: var(--space-2xl) auto;
  width: 100%;
  position: relative;
  height: 1em;
}

.scene-divider::before {
  content: '* * *';
  font-family: var(--font-reading);
  font-size: 18px;
  color: var(--text-tertiary);
  letter-spacing: 0.4em;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

/* Highlightable spans */
.highlightable {
  border-radius: 3px;
  padding: 1px 0;
  transition: all var(--duration-fast) var(--ease-out);
  cursor: pointer;
}

.highlightable:hover {
  background: var(--highlight-bg);
  box-shadow: 0 0 0 3px var(--highlight-bg);
}

.highlightable.active {
  background: var(--highlight-bg);
  box-shadow: 0 0 0 3px var(--highlight-bg);
  border-bottom: 2px solid var(--highlight-border);
}

/* ─── Chapter Navigation ─── */
.chapter-nav {
  display: flex;
  justify-content: space-between;
  gap: var(--space-md);
  padding-top: var(--space-2xl);
  border-top: 1px solid var(--border-subtle);
  margin-top: var(--space-2xl);
}

.chapter-nav-btn {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  padding: var(--space-md) var(--space-lg);
  background: var(--bg-secondary);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  transition: all var(--duration-fast) var(--ease-out);
  color: var(--text-secondary);
  max-width: 48%;
}

.chapter-nav-btn:hover {
  background: var(--bg-hover);
  border-color: var(--border-default);
  color: var(--text-primary);
}

.chapter-nav-btn.next {
  margin-left: auto;
  text-align: right;
}

.chapter-nav-label {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.nav-dir {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-tertiary);
}

.nav-chapter {
  font-size: 14px;
  font-weight: 500;
}


/* ═══════════════════════════════════════════════════
   EXPLANATION POPOVER (Desktop)
   ═══════════════════════════════════════════════════ */
.explanation-popover {
  position: absolute;
  z-index: 400;
  width: 420px;
  max-width: calc(100vw - 32px);
  opacity: 0;
  visibility: hidden;
  transform: translateY(8px);
  transition: all var(--duration-normal) var(--ease-spring);
  pointer-events: none;
}

.explanation-popover.visible {
  opacity: 1;
  visibility: visible;
  transform: translateY(0);
  pointer-events: auto;
}

.popover-arrow {
  position: absolute;
  top: -6px;
  left: 32px;
  width: 12px;
  height: 12px;
  background: var(--popover-bg);
  border-top: 1px solid var(--popover-border);
  border-left: 1px solid var(--popover-border);
  transform: rotate(45deg);
  border-radius: 2px 0 0 0;
}

.popover-content {
  background: var(--popover-bg);
  border: 1px solid var(--popover-border);
  border-radius: var(--radius-lg);
  box-shadow: var(--popover-shadow);
  overflow: hidden;
}

.popover-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-md) var(--space-lg);
  border-bottom: 1px solid var(--border-subtle);
}

.popover-term {
  font-family: var(--font-reading);
  font-size: 16px;
  font-weight: 600;
  font-style: italic;
  color: var(--accent);
}

.popover-close {
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-sm);
  color: var(--text-tertiary);
  transition: all var(--duration-fast) var(--ease-out);
}

.popover-close:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.popover-body {
  padding: var(--space-lg);
}

.explanation-section {
  margin-bottom: var(--space-lg);
}

.explanation-section:last-child {
  margin-bottom: 0;
}

.explanation-label {
  font-family: var(--font-ui);
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-tertiary);
  margin-bottom: var(--space-sm);
  display: flex;
  align-items: center;
  gap: var(--space-xs);
}

.explanation-label svg {
  color: var(--accent);
}

.explanation-section p {
  font-family: var(--font-ui);
  font-size: 14px;
  line-height: 1.7;
  color: var(--text-secondary);
}

.explanation-section p strong {
  color: var(--text-primary);
  font-weight: 600;
}

.explanation-section p em {
  color: var(--accent);
  font-style: italic;
}

/* Loading state for popover */
.popover-loading {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  padding: var(--space-lg);
  color: var(--text-tertiary);
  font-size: 14px;
}

.loading-dots {
  display: flex;
  gap: 4px;
}

.loading-dots span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent);
  animation: loadingDot 1.4s ease-in-out infinite;
}

.loading-dots span:nth-child(2) {
  animation-delay: 0.2s;
}

.loading-dots span:nth-child(3) {
  animation-delay: 0.4s;
}

@keyframes loadingDot {

  0%,
  80%,
  100% {
    opacity: 0.2;
    transform: scale(0.8);
  }

  40% {
    opacity: 1;
    transform: scale(1);
  }
}


/* ═══════════════════════════════════════════════════
   BOTTOM SHEET (Mobile)
   ═══════════════════════════════════════════════════ */
.bottom-sheet {
  display: none;
}

@media (max-width: 768px) {
  .explanation-popover {
    display: none !important;
  }

  .bottom-sheet {
    display: block;
  }
}

.bottom-sheet {
  position: fixed;
  inset: 0;
  z-index: 500;
  pointer-events: none;
  visibility: hidden;
}

.bottom-sheet.open {
  pointer-events: auto;
  visibility: visible;
}

.bottom-sheet-overlay {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  opacity: 0;
  transition: opacity var(--duration-normal) var(--ease-out);
}

.bottom-sheet.open .bottom-sheet-overlay {
  opacity: 1;
}

.bottom-sheet-panel {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background: var(--bg-elevated);
  border-top: 1px solid var(--popover-border);
  border-radius: var(--radius-xl) var(--radius-xl) 0 0;
  transform: translateY(100%);
  transition: transform var(--duration-normal) var(--ease-out);
  max-height: 70vh;
  overflow-y: auto;
  box-shadow: 0 -20px 60px rgba(0, 0, 0, 0.3);
}

.bottom-sheet.open .bottom-sheet-panel {
  transform: translateY(0);
}

.bottom-sheet-handle {
  width: 36px;
  height: 4px;
  background: var(--border-strong);
  border-radius: var(--radius-full);
  margin: var(--space-sm) auto var(--space-xs);
}

.bottom-sheet-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-sm) var(--space-lg) var(--space-md);
  border-bottom: 1px solid var(--border-subtle);
}

.bottom-sheet-body {
  padding: var(--space-lg);
}


/* ═══════════════════════════════════════════════════
   FLOATING EXPLAIN BUTTON
   ═══════════════════════════════════════════════════ */
.explain-btn {
  position: absolute;
  z-index: 450;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 16px 7px 12px;
  font-family: var(--font-ui);
  font-size: 13px;
  font-weight: 600;
  color: #fff;
  background: linear-gradient(135deg, #6c8aff, #a78bfa);
  border: none;
  border-radius: var(--radius-full);
  cursor: pointer;
  box-shadow: 0 4px 16px rgba(108, 138, 255, 0.35),
    0 2px 4px rgba(0, 0, 0, 0.15);
  transform: translate(-50%, 0) scale(0.8);
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: all var(--duration-normal) var(--ease-spring);
  white-space: nowrap;
  user-select: none;
  -webkit-user-select: none;
}

.explain-btn.visible {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
  transform: translate(-50%, 0) scale(1);
}

.explain-btn:hover {
  box-shadow: 0 6px 24px rgba(108, 138, 255, 0.5),
    0 2px 6px rgba(0, 0, 0, 0.2);
  transform: translate(-50%, -1px) scale(1.03);
}

.explain-btn:active {
  transform: translate(-50%, 0) scale(0.97);
}

.explain-btn svg {
  flex-shrink: 0;
  opacity: 0.9;
}

[data-theme="light"] .explain-btn {
  background: linear-gradient(135deg, #4f6ae6, #7c5ce0);
  box-shadow: 0 4px 16px rgba(79, 106, 230, 0.3),
    0 2px 4px rgba(0, 0, 0, 0.1);
}

[data-theme="light"] .explain-btn:hover {
  box-shadow: 0 6px 24px rgba(79, 106, 230, 0.45),
    0 2px 6px rgba(0, 0, 0, 0.15);
}


/* ═══════════════════════════════════════════════════
   USER SELECTION HIGHLIGHT
   ═══════════════════════════════════════════════════ */

::selection {
  background: var(--highlight-bg);
  color: var(--text-primary);
}

::-moz-selection {
  background: var(--highlight-bg);
  color: var(--text-primary);
}


/* ═══════════════════════════════════════════════════
   RESPONSIVE DESIGN
   ═══════════════════════════════════════════════════ */

@media (max-width: 768px) {
  :root {
    --reader-font-size: 17px;
    --reader-line-height: 1.75;
    --reading-width: 100%;
  }

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

  .book-grid {
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: var(--space-md);
  }

  .cover-title {
    font-size: 16px;
  }

  .cover-author {
    font-size: 10px;
  }

  .reader-content {
    padding-left: var(--space-md);
    padding-right: var(--space-md);
  }

  .chapter-nav {
    flex-direction: column;
  }

  .chapter-nav-btn {
    max-width: 100%;
    width: 100%;
  }

  .chapter-nav-btn.next {
    flex-direction: row-reverse;
    text-align: left;
  }

  .reader-header-center .reader-book-title {
    font-size: 12px;
  }
}

@media (max-width: 768px) {
  /* Restored native selection */
}

@media (max-width: 480px) {
  .library-hero {
    padding: var(--space-2xl) var(--space-md) var(--space-xl);
  }

  .library-section,
  .features-section {
    padding-left: var(--space-md);
    padding-right: var(--space-md);
  }

  .book-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: var(--space-sm);
  }
}

@media (min-width: 1200px) {
  :root {
    --reader-font-size: 20px;
  }
}

/* ═══════════════════════════════════════════════════
   MOBILE ACTION BAR
   ═══════════════════════════════════════════════════ */

.mobile-action-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: var(--bg-elevated);
  border-top: 1px solid var(--border-subtle);
  padding: var(--space-sm) var(--space-md);
  padding-bottom: max(var(--space-sm), env(safe-area-inset-bottom));
  z-index: 1000;
  transform: translateY(100%);
  transition: transform 0.3s cubic-bezier(0.16, 1, 0.3, 1);
  box-shadow: 0 -4px 24px rgba(0, 0, 0, 0.15);
  display: flex;
  align-items: center;
  justify-content: center;
}

.mobile-action-bar.visible {
  transform: translateY(0);
}

.mab-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  max-width: 600px;
  gap: var(--space-md);
}

.mab-text {
  font-size: 14px;
  font-weight: 500;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
}

.mab-btn {
  padding: 8px 16px;
  font-size: 14px;
  white-space: nowrap;
  border-radius: 20px;
  margin: 0;
}