@import url('https://fonts.googleapis.com/css2?family=Lora:ital,wght@0,400;0,500;0,600;0,700;1,400;1,500&family=Lora:wght@400&display=swap');

/* ── CSS Variables ── */
:root {
  --bg: #faf8f5;
  --surface: #f3f0eb;
  --border: #ddd9d0;
  --text: #2c2c2c;
  --text-muted: #6b6560;
  --accent: #7a4f2e;
  --accent-light: #c4956a;
  --link: #5a3e28;
  --toc-active: #7a4f2e;
  --progress-bg: #e8e2d9;
  --progress-fill: #7a4f2e;
  --shadow: rgba(0,0,0,0.08);
  --sidebar-width: 260px;
}

[data-theme="dark"] {
  --bg: #1a1714;
  --surface: #242018;
  --border: #3a3530;
  --text: #e8e4dc;
  --text-muted: #9a9088;
  --accent: #c4956a;
  --accent-light: #d4a97e;
  --link: #c4956a;
  --toc-active: #c4956a;
  --progress-bg: #2e2a24;
  --progress-fill: #c4956a;
  --shadow: rgba(0,0,0,0.3);
}

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

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

body {
  font-family: 'Lora', Georgia, serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.8;
  transition: background 0.2s, color 0.2s;
}

/* ── Progress Bar ── */
#reading-progress {
  position: fixed;
  top: 0; left: 0;
  width: 100%;
  height: 3px;
  background: var(--progress-bg);
  z-index: 1000;
}

#reading-progress::before {
  content: '';
  position: absolute;
  top: 0; left: 0;
  height: 100%;
  width: var(--progress-pct, 0%);
  background: var(--progress-fill);
  transition: width 0.1s;
}

/* ── Header ── */
#site-header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  z-index: 200;
  padding: 0 1.5rem;
  height: 52px;
  display: flex;
  align-items: center;
  gap: 1rem;
}

#site-header .header-title {
  font-size: 1rem;
  font-weight: 600;
  color: var(--accent);
  text-decoration: none;
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#site-header .header-title:hover { color: var(--accent-light); }

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

/* ── Layout ── */
.page-wrapper {
  display: flex;
  max-width: 1100px;
  margin: 52px auto 0;
  padding: 0;
  min-height: calc(100vh - 52px);
}

/* ── TOC Sidebar ── */
#toc-sidebar {
  width: var(--sidebar-width);
  flex-shrink: 0;
  position: sticky;
  top: 52px;
  height: calc(100vh - 52px);
  overflow-y: auto;
  padding: 2rem 1rem 2rem 1.5rem;
  border-right: 1px solid var(--border);
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}

#toc-sidebar::-webkit-scrollbar { width: 4px; }
#toc-sidebar::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

#toc-sidebar h2 {
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--text-muted);
  margin-bottom: 1rem;
  padding-bottom: 0.5rem;
  border-bottom: 1px solid var(--border);
}

#toc-list {
  list-style: none;
}

#toc-list li { margin: 0; }

#toc-list a {
  display: block;
  font-size: 0.78rem;
  line-height: 1.4;
  color: var(--text-muted);
  text-decoration: none;
  padding: 0.3rem 0.6rem;
  border-left: 2px solid transparent;
  border-radius: 0 3px 3px 0;
  transition: color 0.15s, border-color 0.15s, background 0.15s;
}

#toc-list a:hover {
  color: var(--text);
  background: var(--surface);
}

#toc-list a.active {
  color: var(--toc-active);
  border-left-color: var(--toc-active);
  font-weight: 600;
  background: var(--surface);
}

/* ── Main Content ── */
#main-content {
  flex: 1;
  min-width: 0;
  padding: 3rem 3rem 5rem 3.5rem;
  max-width: 700px;
}

/* ── Typography ── */
#main-content h1 {
  font-size: 2.2rem;
  font-weight: 700;
  color: var(--accent);
  line-height: 1.2;
  margin-bottom: 0.5rem;
}

#main-content .subtitle,
#main-content h1 + p > em:only-child,
#main-content h1 + p:has(> em:only-child) {
  font-size: 1rem;
  font-style: italic;
  color: var(--text-muted);
  margin-bottom: 2.5rem;
  line-height: 1.5;
}

#main-content hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 2.5rem 0;
}

#main-content h2 {
  font-size: 1.45rem;
  font-weight: 700;
  color: var(--text);
  margin-top: 3.5rem;
  margin-bottom: 1.2rem;
  padding-top: 0.5rem;
  line-height: 1.3;
  scroll-margin-top: 70px;
}

#main-content h3 {
  font-size: 1.1rem;
  font-weight: 600;
  color: var(--text-muted);
  margin-top: 2rem;
  margin-bottom: 0.8rem;
  font-style: italic;
  scroll-margin-top: 70px;
}

#main-content p {
  margin-bottom: 1.4rem;
  color: var(--text);
}

#main-content em { font-style: italic; }
#main-content strong { font-weight: 600; }

#main-content blockquote {
  border-left: 3px solid var(--accent-light);
  padding: 0.5rem 0 0.5rem 1.5rem;
  margin: 1.5rem 0;
  color: var(--text-muted);
  font-style: italic;
}

#main-content ul, #main-content ol {
  padding-left: 1.8rem;
  margin-bottom: 1.4rem;
}

#main-content li { margin-bottom: 0.4rem; }

#main-content a { color: var(--link); }
#main-content a:hover { color: var(--accent-light); }

/* ── Drop Caps ── */
body.complete-text #main-content h2 + p::first-letter,
body.summary-text #main-content h2 + p::first-letter {
  float: left;
  font-size: 3.8rem;
  line-height: 0.82;
  font-weight: 700;
  color: var(--accent);
  margin-right: 0.08em;
  margin-top: 0.06em;
  font-family: 'Lora', Georgia, serif;
}

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

.chapter-nav-btn {
  display: flex;
  flex-direction: column;
  max-width: 45%;
  text-decoration: none;
  padding: 0.8rem 1rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text-muted);
  font-size: 0.78rem;
  transition: border-color 0.15s, background 0.15s;
}

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

.chapter-nav-btn .nav-label {
  font-size: 0.65rem;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  margin-bottom: 0.25rem;
  color: var(--accent);
}

.chapter-nav-btn .nav-title {
  font-weight: 600;
  color: var(--text);
  font-size: 0.82rem;
  line-height: 1.3;
}

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

/* ── Buttons ── */
.btn-icon {
  background: none;
  border: 1px solid var(--border);
  border-radius: 4px;
  width: 32px;
  height: 32px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  font-size: 0.95rem;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
  flex-shrink: 0;
}

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

/* ── Back to Top ── */
#back-to-top {
  position: fixed;
  bottom: 2rem;
  right: 2rem;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 50%;
  width: 42px;
  height: 42px;
  font-size: 1.1rem;
  cursor: pointer;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, background 0.15s;
  z-index: 300;
  display: flex;
  align-items: center;
  justify-content: center;
}

#back-to-top.visible {
  opacity: 1;
  pointer-events: auto;
}

#back-to-top:hover { background: var(--accent-light); }

/* ── TOC Toggle (mobile) ── */
#toc-toggle {
  display: none;
}

/* ── Index / Landing Page ── */
.index-wrapper {
  max-width: 680px;
  margin: 5rem auto;
  padding: 0 2rem;
  text-align: center;
}

.index-wrapper h1 {
  font-size: 2.5rem;
  color: var(--accent);
  margin-bottom: 0.5rem;
}

.index-wrapper .tagline {
  font-size: 1.05rem;
  font-style: italic;
  color: var(--text-muted);
  margin-bottom: 3rem;
  line-height: 1.6;
}

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 1.5rem;
  margin-top: 2rem;
}

.card {
  display: block;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 2rem 1.5rem;
  text-decoration: none;
  color: var(--text);
  transition: border-color 0.2s, box-shadow 0.2s, transform 0.1s;
  text-align: left;
}

.card:hover {
  border-color: var(--accent-light);
  box-shadow: 0 4px 16px var(--shadow);
  transform: translateY(-2px);
}

.card .card-icon { font-size: 2rem; margin-bottom: 0.75rem; }
.card .card-title { font-size: 1.1rem; font-weight: 700; color: var(--accent); margin-bottom: 0.5rem; }
.card .card-desc { font-size: 0.85rem; color: var(--text-muted); line-height: 1.6; }
.card .card-meta { font-size: 0.75rem; color: var(--text-muted); margin-top: 0.75rem; opacity: 0.75; }

/* ── Responsive ── */
@media (max-width: 800px) {
  html { font-size: 17px; }

  .page-wrapper { flex-direction: column; }

  #toc-sidebar {
    position: static;
    width: 100%;
    height: auto;
    border-right: none;
    border-bottom: 1px solid var(--border);
    padding: 1rem 1.5rem;
    display: none;
  }

  #toc-sidebar.open {
    display: block;
    position: fixed;
    top: 52px;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    overflow-y: auto;
    z-index: 150;
    background: var(--bg);
    border-bottom: none;
    padding: 1.5rem;
  }

  #toc-toggle { display: flex; }

  #main-content {
    padding: 2rem 1.5rem 4rem;
    max-width: 100%;
  }

  .card-grid { grid-template-columns: 1fr; }

  #back-to-top { bottom: 1rem; right: 1rem; }

  body.complete-text #main-content h2 + p::first-letter,
  body.summary-text #main-content h2 + p::first-letter {
    font-size: 3rem;
  }
}
