/* ---------- Theme tokens ---------- */
:root {
  --bg: #faf7f1;
  --bg-raised: #ffffff;
  --bg-sunken: #f1ece2;
  --ink: #2b2620;
  --ink-soft: #6e6557;
  --ink-faint: #9c9284;
  --accent: #8a6d3b;
  --accent-strong: #6f5527;
  --accent-soft: rgba(138, 109, 59, 0.12);
  --border: rgba(43, 38, 32, 0.12);
  --highlight: rgba(212, 175, 55, 0.28);
  --shadow: 0 10px 30px rgba(43, 38, 32, 0.08);
  --serif: 'Iowan Old Style', 'Palatino Linotype', Palatino, Georgia, 'Times New Roman', serif;
  --sans: ui-sans-serif, system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
}

[data-theme='dark'] {
  --bg: #171411;
  --bg-raised: #201c18;
  --bg-sunken: #12100d;
  --ink: #e9e2d6;
  --ink-soft: #a89d8c;
  --ink-faint: #7c7264;
  --accent: #c9a35c;
  --accent-strong: #ddbb77;
  --accent-soft: rgba(201, 163, 92, 0.14);
  --border: rgba(233, 226, 214, 0.14);
  --highlight: rgba(212, 175, 55, 0.22);
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    --bg: #171411;
    --bg-raised: #201c18;
    --bg-sunken: #12100d;
    --ink: #e9e2d6;
    --ink-soft: #a89d8c;
    --ink-faint: #7c7264;
    --accent: #c9a35c;
    --accent-strong: #ddbb77;
    --accent-soft: rgba(201, 163, 92, 0.14);
    --border: rgba(233, 226, 214, 0.14);
    --highlight: rgba(212, 175, 55, 0.22);
    --shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
  }
}

* { box-sizing: border-box; }

html { scroll-behavior: smooth; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--serif);
  font-size: 17px;
  line-height: 1.72;
  -webkit-font-smoothing: antialiased;
}

/* ---------- Header ---------- */
.topbar {
  position: fixed;
  top: 0; left: 0; right: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.55rem 0.9rem;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
  font-family: var(--sans);
}

.topbar .brand {
  font-family: var(--serif);
  font-size: 1rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  white-space: nowrap;
  color: var(--ink);
  text-decoration: none;
  margin-right: auto;
}
.topbar .brand small {
  display: block;
  font-family: var(--sans);
  font-size: 0.62rem;
  font-weight: 500;
  letter-spacing: 0.22em;
  text-transform: uppercase;
  color: var(--accent);
}

.icon-btn {
  appearance: none;
  border: 1px solid transparent;
  background: none;
  color: var(--ink-soft);
  width: 2.3rem;
  height: 2.3rem;
  border-radius: 0.6rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex: none;
}
.icon-btn:hover { background: var(--accent-soft); color: var(--ink); }
.icon-btn svg { width: 1.25rem; height: 1.25rem; }

.searchbox {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.35rem 0.9rem;
  width: min(30rem, 42vw);
  transition: border-color 0.15s;
}
.searchbox:focus-within { border-color: var(--accent); }
.searchbox svg { width: 1rem; height: 1rem; color: var(--ink-faint); flex: none; }
.searchbox input {
  border: none;
  outline: none;
  background: none;
  color: var(--ink);
  font-family: var(--sans);
  font-size: 0.88rem;
  width: 100%;
}
.searchbox input::placeholder { color: var(--ink-faint); }

/* ---------- Layout ---------- */
.layout {
  display: flex;
  padding-top: 3.6rem;
  min-height: 100vh;
}

/* ---------- Sidebar ---------- */
.sidebar {
  width: 19rem;
  flex: none;
  position: sticky;
  top: 3.6rem;
  height: calc(100vh - 3.6rem);
  overflow-y: auto;
  padding: 1.2rem 0.9rem 3rem;
  border-right: 1px solid var(--border);
  font-family: var(--sans);
  scrollbar-width: thin;
}

.sidebar .toc-label {
  font-size: 0.64rem;
  font-weight: 600;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  color: var(--ink-faint);
  padding: 0 0.7rem 0.5rem;
}

.chapter-item { margin-bottom: 1px; }

.chapter-link {
  display: flex;
  gap: 0.6rem;
  align-items: baseline;
  width: 100%;
  text-align: left;
  border: none;
  background: none;
  color: var(--ink-soft);
  font-family: var(--sans);
  font-size: 0.85rem;
  line-height: 1.35;
  padding: 0.42rem 0.7rem;
  border-radius: 0.55rem;
  cursor: pointer;
}
.chapter-link:hover { background: var(--accent-soft); color: var(--ink); }
.chapter-link .num {
  flex: none;
  width: 1.4rem;
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--ink-faint);
  font-variant-numeric: tabular-nums;
}
.chapter-item.active > .chapter-link { color: var(--ink); font-weight: 600; background: var(--accent-soft); }
.chapter-item.active > .chapter-link .num { color: var(--accent); }

.section-list {
  margin: 0.15rem 0 0.55rem;
  padding-left: 2rem;
  display: none;
}
.chapter-item.active .section-list { display: block; }

.section-link {
  display: block;
  width: 100%;
  text-align: left;
  border: none;
  background: none;
  color: var(--ink-soft);
  font-family: var(--sans);
  font-size: 0.8rem;
  line-height: 1.35;
  padding: 0.3rem 0.6rem;
  border-left: 2px solid var(--border);
  border-radius: 0 0.45rem 0.45rem 0;
  cursor: pointer;
}
.section-link:hover { color: var(--ink); background: var(--accent-soft); }
.section-link.current { color: var(--accent-strong); border-left-color: var(--accent); font-weight: 600; }

.drawer-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 45;
  background: rgba(0, 0, 0, 0.45);
}

/* ---------- Reading pane ---------- */
.content {
  flex: 1;
  min-width: 0;
  padding: 2.5rem 1.4rem 5rem;
}

.page {
  max-width: 42rem;
  margin: 0 auto;
}

.chapter-kicker {
  font-family: var(--sans);
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.24em;
  text-transform: uppercase;
  color: var(--accent);
  margin: 0 0 0.6rem;
}

h1.chapter-title {
  font-size: clamp(1.7rem, 4.5vw, 2.4rem);
  font-weight: 600;
  line-height: 1.2;
  letter-spacing: 0.005em;
  margin: 0 0 2.2rem;
}

h2.section-heading {
  font-size: 1.28rem;
  font-weight: 600;
  line-height: 1.3;
  margin: 2.8rem 0 1.1rem;
  color: var(--ink);
}
h2.section-heading::after {
  content: '';
  display: block;
  width: 2.6rem;
  height: 2px;
  margin-top: 0.55rem;
  background: var(--accent);
  opacity: 0.55;
  border-radius: 2px;
}

.para {
  position: relative;
  margin: 0 0 1.15rem;
  scroll-margin-top: 5.5rem;
}
.para.verse {
  white-space: pre-wrap;
  tab-size: 3;
}
.para .pnum {
  position: absolute;
  left: -2.6rem;
  top: 0.35em;
  font-family: var(--sans);
  font-size: 0.62rem;
  font-weight: 500;
  color: var(--ink-faint);
  opacity: 0;
  transition: opacity 0.15s;
  user-select: none;
}
.para:hover .pnum { opacity: 1; }

.para.flash {
  animation: flash-bg 2.4s ease-out;
  border-radius: 0.4rem;
}
@keyframes flash-bg {
  0%, 35% { background: var(--highlight); box-shadow: 0 0 0 8px var(--highlight); }
  100% { background: transparent; box-shadow: 0 0 0 8px transparent; }
}

/* ---------- Chapter prev/next ---------- */
.chapter-nav {
  display: flex;
  gap: 1rem;
  margin-top: 4rem;
  padding-top: 1.6rem;
  border-top: 1px solid var(--border);
  font-family: var(--sans);
}
.chapter-nav a {
  flex: 1;
  text-decoration: none;
  color: var(--ink);
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: 0.8rem;
  padding: 0.85rem 1.1rem;
  transition: border-color 0.15s, transform 0.15s;
}
.chapter-nav a:hover { border-color: var(--accent); transform: translateY(-1px); }
.chapter-nav a.next { text-align: right; }
.chapter-nav .dir {
  display: block;
  font-size: 0.66rem;
  font-weight: 600;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--ink-faint);
  margin-bottom: 0.25rem;
}
.chapter-nav .label { font-size: 0.88rem; font-weight: 600; line-height: 1.3; }

/* ---------- Search results ---------- */
.search-head { margin-bottom: 1.8rem; }
.search-head .chapter-kicker { margin-bottom: 0.4rem; }
.search-head h1 {
  font-size: clamp(1.3rem, 3.5vw, 1.7rem);
  font-weight: 600;
  line-height: 1.35;
  margin: 0 0 0.5rem;
}
.search-head .meta {
  font-family: var(--sans);
  font-size: 0.82rem;
  color: var(--ink-soft);
}

.result-card {
  display: block;
  width: 100%;
  text-align: left;
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: 0.9rem;
  padding: 1.05rem 1.2rem 0.95rem;
  margin-bottom: 0.9rem;
  cursor: pointer;
  color: var(--ink);
  font-family: var(--serif);
  font-size: 1rem;
  line-height: 1.65;
  transition: border-color 0.15s, transform 0.15s;
}
.result-card:hover { border-color: var(--accent); transform: translateY(-1px); }

.result-crumb {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-family: var(--sans);
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  color: var(--accent-strong);
  margin-bottom: 0.55rem;
}
.result-crumb .sep { color: var(--ink-faint); font-weight: 400; }
.result-crumb .pn { color: var(--ink-faint); font-weight: 500; }

.result-text {
  display: -webkit-box;
  -webkit-line-clamp: 7;
  -webkit-box-orient: vertical;
  overflow: hidden;
  margin: 0;
}
.result-card.expanded .result-text { -webkit-line-clamp: unset; }

.result-meter {
  margin-top: 0.75rem;
  height: 3px;
  border-radius: 2px;
  background: var(--accent-soft);
  overflow: hidden;
}
.result-meter i {
  display: block;
  height: 100%;
  background: var(--accent);
  border-radius: 2px;
}

/* ---------- Search status / model loading ---------- */
.search-status {
  font-family: var(--sans);
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: 0.9rem;
  padding: 1.3rem 1.4rem;
  color: var(--ink-soft);
  font-size: 0.88rem;
  line-height: 1.55;
}
.search-status .bar {
  margin-top: 0.8rem;
  height: 5px;
  border-radius: 3px;
  background: var(--accent-soft);
  overflow: hidden;
}
.search-status .bar i {
  display: block;
  height: 100%;
  width: 0%;
  background: var(--accent);
  border-radius: 3px;
  transition: width 0.2s;
}
.search-status.error { border-color: #b4552d; color: #b4552d; }

.spinner {
  display: inline-block;
  width: 0.85rem;
  height: 0.85rem;
  border: 2px solid var(--accent-soft);
  border-top-color: var(--accent);
  border-radius: 50%;
  margin-right: 0.45rem;
  vertical-align: -0.12em;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Mobile ---------- */
@media (max-width: 860px) {
  body { font-size: 16.5px; }

  .topbar { padding: 0.5rem 0.6rem; }
  .topbar .brand small { display: none; }
  .topbar .brand { font-size: 0.92rem; }

  .searchbox {
    order: 10;
    width: 100%;
  }
  .topbar { flex-wrap: wrap; }
  .layout { padding-top: 6.1rem; }

  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    height: 100vh;
    z-index: 50;
    background: var(--bg);
    box-shadow: var(--shadow);
    transform: translateX(-105%);
    transition: transform 0.22s ease;
    width: min(20rem, 85vw);
    padding-top: 1.4rem;
  }
  body.drawer-open .sidebar { transform: translateX(0); }
  body.drawer-open .drawer-backdrop { display: block; }
  body.drawer-open { overflow: hidden; }

  .content { padding: 1.6rem 1.15rem 4rem; }
  .para .pnum { display: none; }
  .para { scroll-margin-top: 7.5rem; }
  h1.chapter-title { margin-bottom: 1.6rem; }
  .chapter-nav { flex-direction: column; gap: 0.7rem; }
  .chapter-nav a.next { text-align: left; }
}

@media (min-width: 861px) {
  #menuBtn { display: none; }
}
