/* spa.css — single-page-app shell for the reading library.
 *
 * Self-contained: the SPA's own chrome (top bar, reader viewport) lives here.
 * Per-book <style> is injected scoped under #reader-content at runtime, so
 * book typography is not duplicated here. The portal's existing inline styles
 * in index.html continue to drive #view-home.
 */

/* ── Layout: two stacked views, only one visible at a time ─────────── */
#view-home, #view-reader { display: none; }
#view-home.active, #view-reader.active { display: block; }

/* When a book is open, hide the portal's container chrome (header/footer)
 * so the reader owns the viewport. The portal is toggled by .rl-reading on
 * <body>. */
body.rl-reading .container > header,
body.rl-reading .container > footer { display: none; }

/* ── Top bar ───────────────────────────────────────────────────────── */
.rl-topbar {
  position: sticky;
  top: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.5rem 1rem;
  background: rgba(13, 17, 28, 0.92);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid rgba(34, 211, 238, 0.18);
  color: #e2e8f0;
  font-family: "Inter", system-ui, -apple-system, sans-serif;
  min-height: 3.25rem;
}

.rl-topbar .rl-home-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2.25rem;
  height: 2.25rem;
  flex: 0 0 auto;
  border: 1px solid rgba(148, 163, 184, 0.25);
  border-radius: 8px;
  background: rgba(30, 41, 59, 0.5);
  color: #cbd5e1;
  cursor: pointer;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.rl-topbar .rl-home-btn:hover {
  background: rgba(34, 211, 238, 0.15);
  color: #22d3ee;
  border-color: rgba(34, 211, 238, 0.4);
}
.rl-topbar .rl-home-btn svg { width: 1.15rem; height: 1.15rem; }

.rl-topbar .rl-book-title {
  font-size: 0.95rem;
  font-weight: 600;
  color: #f1f5f9;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 40%;
}
.rl-topbar .rl-sep {
  color: rgba(148, 163, 184, 0.4);
  flex: 0 0 auto;
}
.rl-topbar .rl-chapter {
  font-size: 0.85rem;
  color: #94a3b8;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}
.rl-topbar .rl-chapter .rl-chapter-num {
  color: #22d3ee;
  font-weight: 600;
  margin-right: 0.4rem;
}
.rl-topbar .rl-chapter .rl-chapter-title { color: #cbd5e1; }
.rl-topbar .rl-chapter .rl-chapter-placeholder { color: rgba(148, 163, 184, 0.5); font-style: italic; }

.rl-topbar .rl-spacer { flex: 1 1 auto; }

/* ── Reader viewport ──────────────────────────────────────────────── */
.rl-reader {
  /* The scroll container. The page itself never scrolls; this does. */
  height: calc(100vh - 3.25rem);
  overflow-y: auto;
  overflow-x: hidden;
  scroll-behavior: smooth;
  background: #0b1120;
}
.rl-reader:focus { outline: none; }

#reader-content {
  max-width: 52rem;
  margin: 0 auto;
  padding: 2rem 1.5rem 6rem;
  color: #e2e8f0;
  font-family: "Iowan Old Style", "Palatino Linotype", "Book Antiqua", Georgia, serif;
  font-size: 1.0625rem;
  line-height: 1.75;
}
#reader-content:empty::before {
  content: "Select a book from the catalog to begin reading.";
  display: block;
  color: rgba(148, 163, 184, 0.5);
  font-style: italic;
  text-align: center;
  margin-top: 4rem;
}

/* Reader loading state */
.rl-reader.rl-loading::after {
  content: "Loading…";
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: #22d3ee;
  font-family: "Inter", sans-serif;
  font-size: 0.95rem;
  letter-spacing: 0.05em;
}

/* ── Per-book style scoping ─────────────────────────────────────────
 * Book <style> is rewritten at load time so every selector is prefixed
 * with #reader-content. This keeps book typography contained and prevents
 * it from leaking into the top bar or chatbot. A few base resets ensure
 * book content reads well against the dark reader background. */
#reader-content h1, #reader-content h2, #reader-content h3 {
  color: #f1f5f9;
  line-height: 1.3;
  margin-top: 2rem;
  margin-bottom: 0.75rem;
}
#reader-content h1 { font-size: 1.9rem; }
#reader-content h2 { font-size: 1.4rem; color: #22d3ee; }
#reader-content h3 { font-size: 1.15rem; }
#reader-content p { margin: 0 0 1rem; }
#reader-content a { color: #22d3ee; }
#reader-content a:hover { color: #67e8f9; }
#reader-content img { max-width: 100%; height: auto; border-radius: 4px; }
#reader-content blockquote {
  border-left: 3px solid rgba(34, 211, 238, 0.4);
  margin: 1rem 0;
  padding: 0.5rem 0 0.5rem 1rem;
  color: #cbd5e1;
  font-style: italic;
}
#reader-content table { border-collapse: collapse; margin: 1rem 0; }
#reader-content th, #reader-content td {
  border: 1px solid rgba(148, 163, 184, 0.25);
  padding: 0.4rem 0.6rem;
}
#reader-content code, #reader-content pre {
  font-family: "SF Mono", "Cascadia Code", Consolas, monospace;
  font-size: 0.9em;
}
#reader-content pre {
  background: rgba(15, 23, 42, 0.6);
  padding: 0.75rem;
  border-radius: 6px;
  overflow-x: auto;
}

/* Section anchor offset so headings aren't hidden behind the sticky top bar
 * when navigating to #chapter-... anchors. */
#reader-content section[id] { scroll-margin-top: 3.5rem; }

/* Book markdown section chrome */
#reader-content .rl-preamble { margin-bottom: 2.5rem; }
#reader-content .rl-preamble .book-sub,
#reader-content .book-sub { color: #94a3b8; font-style: italic; margin-bottom: 2rem; }
#reader-content .section-kicker,
#reader-content .chapter-number {
  color: #94a3b8;
  text-transform: uppercase;
  letter-spacing: 0.14em;
  font-size: 0.75rem;
  margin-bottom: 0.5rem;
}
#reader-content section.epub-section,
#reader-content section.chapter {
  margin: 4rem 0;
  padding-top: 2rem;
  border-top: 1px solid rgba(148, 163, 184, 0.2);
}
#reader-content section.chapter:first-of-type,
#reader-content section.epub-section:first-of-type { border-top: none; }
#reader-content .offline-chart-container {
  width: 100%;
  border: 1px solid rgba(148, 163, 184, 0.2);
  border-radius: 8px;
  padding: 1.5rem 1rem 1rem 0.5rem;
  margin: 2rem 0;
  background: rgba(255, 255, 255, 0.02);
}
#reader-content svg { max-width: 100%; height: auto; }

/* ── Responsive ───────────────────────────────────────────────────── */
@media (max-width: 640px) {
  .rl-topbar { padding: 0.4rem 0.6rem; gap: 0.5rem; }
  .rl-topbar .rl-book-title { max-width: 30%; font-size: 0.85rem; }
  .rl-topbar .rl-chapter { font-size: 0.78rem; }
  #reader-content { padding: 1.25rem 1rem 5rem; font-size: 1rem; }
  .rl-reader { height: calc(100vh - 3rem); }
}
