/* ---------- Design tokens ---------- */
:root {
  --bg: #faf7f0;
  --bg-elevated: #ffffff;
  --text: #2b2620;
  --text-soft: #6b6255;
  --text-faint: #9b9284;
  --border: #e6ddcc;
  --border-strong: #d8cbb0;
  --accent: #8a3324;
  --accent-soft: #f1e3d8;
  --link: #7a2f22;
  --highlight: #f6d98a;
  --highlight-text: #2b2620;
  --shadow: 0 8px 30px rgba(60, 45, 20, 0.12);
  --sidebar-w: 320px;
  --serif: Georgia, 'Iowan Old Style', 'Palatino Linotype', 'Book Antiqua', Palatino, serif;
  --sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  --reading-font: var(--serif);
  --reading-scale: 1;
  --content-w: 46rem;
}

:root[data-theme="sepia"] {
  --bg: #efe3c8;
  --bg-elevated: #f7ecd4;
  --text: #3a2f1d;
  --text-soft: #6f5f42;
  --text-faint: #9c8b68;
  --border: #ddcb9f;
  --border-strong: #cbb787;
  --accent: #8a4a24;
  --accent-soft: #e6d4ac;
  --link: #7a3f1e;
  --highlight: #d9a441;
  --highlight-text: #2b2011;
  --shadow: 0 8px 30px rgba(50, 35, 10, 0.18);
}

:root[data-theme="dark"] {
  --bg: #1b1a17;
  --bg-elevated: #242220;
  --text: #e8e1d3;
  --text-soft: #b3a893;
  --text-faint: #7c7364;
  --border: #3a3630;
  --border-strong: #4a453c;
  --accent: #d68b5d;
  --accent-soft: #332a22;
  --link: #e2a377;
  --highlight: #6b5427;
  --highlight-text: #f5e6bf;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  height: 100%;
}
body {
  font-family: var(--sans);
  -webkit-font-smoothing: antialiased;
}
button { font-family: inherit; }
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; display: block; }

/* ---------- Layout ---------- */
.app {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  min-height: 100dvh;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 10px;
  height: 56px;
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 20;
  flex-shrink: 0;
}
.topbar-title {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  line-height: 1.15;
  padding: 6px 2px;
  border: none;
  background: none;
  text-align: left;
  cursor: pointer;
  border-radius: 8px;
  font: inherit;
  transition: background .15s;
}
.topbar-title:hover { background: var(--accent-soft); }
.topbar-title:hover .title-main { color: var(--accent); }
.title-main {
  font-family: var(--serif);
  font-weight: 700;
  font-size: 1.05rem;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.title-sub {
  font-size: 0.75rem;
  color: var(--text-soft);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.topbar-actions { display: flex; gap: 2px; }

.icon-btn {
  border: none;
  background: transparent;
  color: var(--text-soft);
  width: 40px;
  height: 40px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  text-decoration: none;
  flex-shrink: 0;
  transition: background .15s, color .15s;
}
.icon-btn:hover { background: var(--accent-soft); color: var(--accent); }
.icon-btn.active { background: var(--accent-soft); color: var(--accent); }
.icon-text {
  font-family: var(--serif);
  font-weight: 700;
  font-size: 0.95rem;
  line-height: 1;
}

.icon-pop-wrap { position: relative; }
.icon-popover {
  display: none;
  position: absolute;
  top: calc(100% + 8px);
  right: -6px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 10px;
  z-index: 30;
}
.icon-popover.open { display: block; }
.icon-popover .segmented { border-color: var(--border-strong); }

.body-row {
  display: flex;
  flex: 1;
  min-height: 0;
}

/* ---------- Sidebar ---------- */
/* position:fixed (not sticky) so the panel's height/position is anchored
   purely to the viewport — sticky + calc(100vh) has a well-known bug in
   WebKit/Safari where the box isn't correctly re-measured until a page-level
   scroll event fires, which made the last chapters unreachable by scrolling
   the sidebar's own list. */
.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: var(--bg-elevated);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 56px;
  left: 0;
  height: calc(100vh - 56px);
  height: calc(100dvh - 56px);
  overflow: hidden;
  z-index: 10;
}
.sidebar-filter {
  padding: 12px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.sidebar-filter input {
  width: 100%;
  padding: 9px 12px;
  border-radius: 8px;
  border: 1px solid var(--border-strong);
  background: var(--bg);
  color: var(--text);
  font-size: 0.85rem;
}
.sidebar-filter input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.sidebar-scroll {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  padding: 6px 0 24px;
}

.nav-frontmatter {
  padding: 2px 8px 10px;
  border-bottom: 1px dashed var(--border);
  margin-bottom: 6px;
}
.nav-fm-link {
  display: block;
  width: 100%;
  text-align: left;
  background: none;
  border: none;
  color: var(--text-soft);
  font-size: 0.86rem;
  padding: 7px 10px;
  border-radius: 8px;
  cursor: pointer;
  font-style: italic;
}
.nav-fm-link:hover { background: var(--accent-soft); color: var(--accent); }
.nav-fm-link.current { background: var(--accent-soft); color: var(--accent); font-weight: 600; }

.nav-book { margin-bottom: 2px; }
.nav-book-head {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  background: none;
  border: none;
  color: var(--text);
  padding: 10px 12px;
  cursor: pointer;
  text-align: left;
}
.nav-book-head:hover { background: var(--accent-soft); }
.nav-book-head .chev { transition: transform .18s; color: var(--text-faint); flex-shrink: 0; width: 14px; height: 14px; }
.nav-book-head.open .chev { transform: rotate(90deg); }
.nav-book-title {
  font-family: var(--serif);
  font-weight: 700;
  font-size: 0.92rem;
}
.nav-book-sub {
  display: block;
  font-size: 0.7rem;
  color: var(--text-faint);
  font-weight: 400;
  margin-top: 1px;
}
.nav-chapters {
  display: none;
  padding: 2px 4px 8px 14px;
}
.nav-chapters.open { display: block; }
.nav-chapter {
  display: flex;
  gap: 8px;
  width: 100%;
  text-align: left;
  background: none;
  border: none;
  color: var(--text-soft);
  padding: 6px 10px;
  border-radius: 7px;
  cursor: pointer;
  font-size: 0.83rem;
  line-height: 1.3;
}
.nav-chapter:hover { background: var(--accent-soft); color: var(--accent); }
.nav-chapter.current { background: var(--accent); color: #fff; }
.nav-chapter .num { flex-shrink: 0; opacity: .65; font-variant-numeric: tabular-nums; min-width: 1.4em; }
.nav-chapter mark { background: var(--highlight); color: var(--highlight-text); border-radius: 2px; }

.overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.35);
  z-index: 15;
}

/* ---------- Reader ---------- */
.reader {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 0 20px 60px;
  margin-left: var(--sidebar-w);
}
.chapter {
  width: 100%;
  max-width: var(--content-w);
  padding-top: 34px;
  font-family: var(--reading-font);
  font-size: calc(1.0625rem * var(--reading-scale));
  line-height: 1.75;
  color: var(--text);
}
.chapter-kicker {
  font-family: var(--sans);
  text-transform: uppercase;
  letter-spacing: .08em;
  font-size: 0.72rem;
  color: var(--accent);
  font-weight: 600;
  margin-bottom: 8px;
}
.chapter-title {
  font-size: calc(1.55em);
  line-height: 1.3;
  margin: 0 0 6px;
  font-weight: 700;
}
.chapter-speaker {
  font-style: italic;
  color: var(--text-soft);
  margin: 0 0 22px;
  font-size: 0.95em;
}
.chapter-epigraph {
  border-left: 3px solid var(--border-strong);
  padding: 2px 0 2px 18px;
  margin: 0 0 30px;
  color: var(--text-soft);
  font-style: italic;
}
.chapter-epigraph .verse { margin: 0 0 8px; }

.verse {
  margin: 0 0 14px;
  position: relative;
  cursor: default;
}
.verse.is-target { animation: flash-highlight 2.2s ease; }
@keyframes flash-highlight {
  0% { background: var(--highlight); }
  100% { background: transparent; }
}
.vnum {
  display: inline-block;
  font-family: var(--sans);
  font-size: 0.62em;
  font-weight: 700;
  color: var(--accent);
  vertical-align: super;
  margin-right: 0.35em;
  cursor: pointer;
  user-select: none;
  padding: 1px 3px;
  border-radius: 4px;
}
.vnum:hover { background: var(--accent-soft); }
.vtext mark { background: var(--highlight); color: var(--highlight-text); border-radius: 2px; padding: 0 1px; }
.star-btn {
  border: none;
  background: none;
  color: var(--text-faint);
  cursor: pointer;
  vertical-align: middle;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  padding: 0;
  margin-left: 2px;
  opacity: 0.55;
  transition: opacity .12s, color .12s, background .12s;
}
.verse:hover .star-btn, .star-btn:hover { opacity: 1; }
.star-btn:hover { color: var(--accent); background: var(--accent-soft); }
.star-btn svg { width: 15px; height: 15px; }
.star-btn.active {
  opacity: 1;
  color: var(--accent);
  background: var(--accent-soft);
}
.star-btn.active svg { width: 16px; height: 16px; fill: var(--accent); }

.chapter-note {
  font-family: var(--serif);
  font-style: italic;
  text-align: center;
  color: var(--accent);
  margin: 30px 0 18px;
  font-size: 0.95em;
}
.chapter-footnote {
  font-family: var(--sans);
  font-size: 0.78em;
  color: var(--text-faint);
  border-top: 1px solid var(--border);
  margin-top: 30px;
  padding-top: 12px;
}

.frontmatter p { margin: 0 0 16px; text-align: justify; }
.frontmatter .chapter-title { margin-bottom: 24px; }

.chapter-nav {
  width: 100%;
  max-width: var(--content-w);
  display: flex;
  justify-content: space-between;
  gap: 10px;
  margin-top: 30px;
  padding-top: 20px;
  border-top: 1px solid var(--border);
  font-family: var(--sans);
}
.nav-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--bg-elevated);
  border: 1px solid var(--border-strong);
  color: var(--text);
  padding: 10px 16px;
  border-radius: 10px;
  cursor: pointer;
  font-size: 0.85rem;
  max-width: 48%;
}
.nav-btn:hover { border-color: var(--accent); color: var(--accent); }
.nav-btn:disabled { opacity: 0.35; cursor: default; pointer-events: none; }
.nav-btn span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav-btn svg { flex-shrink: 0; width: 16px; height: 16px; }

/* ---------- Panels (search / bookmarks / settings) ---------- */
.panel-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(20, 15, 5, .4);
  z-index: 50;
  align-items: flex-start;
  justify-content: center;
  padding: 8vh 16px 16px;
}
.panel-overlay.open { display: flex; }
.panel {
  background: var(--bg-elevated);
  color: var(--text);
  width: 100%;
  max-width: 640px;
  max-height: 80vh;
  border-radius: 16px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  font-family: var(--sans);
}
.panel-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.panel-header h2 { margin: 0; font-size: 1.05rem; flex: 1; font-family: var(--serif); }
.search-box {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--bg);
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  padding: 0 10px;
}
.search-box svg { width: 17px; height: 17px; color: var(--text-faint); flex-shrink: 0; }
.search-box input {
  border: none;
  background: transparent;
  color: var(--text);
  padding: 10px 4px;
  flex: 1;
  min-width: 0;
  font-size: 0.95rem;
}
.search-box input:focus { outline: none; }
.search-meta {
  padding: 8px 16px;
  font-size: 0.78rem;
  color: var(--text-faint);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.search-results, .bookmarks-list { overflow-y: auto; padding: 8px; }
.search-hit, .bookmark-item {
  display: block;
  width: 100%;
  text-align: left;
  background: none;
  border: none;
  border-radius: 10px;
  padding: 10px 12px;
  cursor: pointer;
  color: var(--text);
}
.search-hit:hover, .bookmark-item:hover { background: var(--accent-soft); }
.hit-loc {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--accent);
  font-weight: 600;
  margin-bottom: 3px;
}
.hit-text {
  font-family: var(--serif);
  font-size: 0.95rem;
  line-height: 1.5;
  color: var(--text-soft);
}
.hit-text mark { background: var(--highlight); color: var(--highlight-text); border-radius: 2px; }
.empty-state {
  padding: 30px 16px;
  text-align: center;
  color: var(--text-faint);
  font-size: 0.88rem;
}
.bookmark-item { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; }
.bookmark-remove { flex-shrink: 0; background: none; border: none; color: var(--text-faint); cursor: pointer; width: 26px; height: 26px; border-radius: 6px; }
.bookmark-remove:hover { background: var(--border); color: var(--accent); }
.bookmark-remove svg { width: 14px; height: 14px; }

/* Clickable Scripture citations inside verse text */
.scripture-ref {
  cursor: pointer;
  color: var(--link);
  text-decoration: underline;
  text-decoration-style: dotted;
  text-decoration-color: var(--border-strong);
  text-underline-offset: 3px;
  border-radius: 3px;
  transition: background .12s, color .12s;
}
.scripture-ref:hover {
  color: var(--accent);
  background: var(--accent-soft);
  text-decoration-color: var(--accent);
}
.scripture-body { padding: 14px 18px 20px; overflow-y: auto; }
.scripture-part { margin-bottom: 20px; }
.scripture-part:last-child { margin-bottom: 0; }
.scripture-part-label {
  font-family: var(--sans);
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--accent);
  font-weight: 600;
  margin-bottom: 8px;
}
.scripture-verse {
  font-family: var(--serif);
  font-size: 1.02rem;
  line-height: 1.65;
  color: var(--text);
  margin: 0 0 8px;
}
.scripture-verse .vnum {
  vertical-align: super;
  font-size: 0.65em;
  font-weight: 700;
  color: var(--accent);
  margin-right: 0.35em;
}

.settings-body { padding: 16px; display: flex; flex-direction: column; gap: 20px; }
.setting-group { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.setting-label { font-size: 0.88rem; color: var(--text-soft); font-weight: 600; }
.font-size-control { display: flex; align-items: center; gap: 10px; }
.chip-btn {
  border: 1px solid var(--border-strong);
  background: var(--bg);
  color: var(--text);
  width: 34px;
  height: 34px;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 700;
}
.chip-btn:hover { border-color: var(--accent); color: var(--accent); }
#fontSizeVal { font-variant-numeric: tabular-nums; min-width: 3.2em; text-align: center; font-size: 0.85rem; color: var(--text-soft); }
.segmented { display: flex; border: 1px solid var(--border-strong); border-radius: 9px; overflow: hidden; }
.seg-btn {
  border: none;
  background: var(--bg);
  color: var(--text-soft);
  padding: 8px 14px;
  cursor: pointer;
  font-size: 0.82rem;
  border-right: 1px solid var(--border-strong);
}
.seg-btn:last-child { border-right: none; }
.seg-btn.active { background: var(--accent); color: #fff; }

.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--text);
  color: var(--bg);
  padding: 10px 18px;
  border-radius: 30px;
  font-family: var(--sans);
  font-size: 0.85rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
  z-index: 100;
  box-shadow: var(--shadow);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* content width variants */
:root[data-width="narrow"] { --content-w: 36rem; }
:root[data-width="wide"] { --content-w: 58rem; }

/* ---------- Responsive ---------- */
@media (max-width: 860px) {
  .sidebar {
    position: fixed;
    left: -100%;
    top: 56px;
    height: calc(100vh - 56px);
    height: calc(100dvh - 56px);
    width: min(86vw, 340px);
    z-index: 16;
    transition: left .22s ease;
    box-shadow: var(--shadow);
  }
  .app.nav-open .sidebar { left: 0; }
  .app.nav-open .overlay { display: block; }
  .reader { padding: 0 14px 50px; margin-left: 0; }
  .chapter { padding-top: 22px; }
}

@media (max-width: 480px) {
  .panel { max-height: 88vh; border-radius: 14px 14px 0 0; }
  .panel-overlay { padding: 0; align-items: flex-end; }
  .nav-btn span { max-width: 90px; }
}

@media (max-width: 420px) {
  .topbar { gap: 4px; padding: 0 6px; }
  .icon-btn { width: 34px; height: 34px; }
  .icon-btn svg { width: 18px; height: 18px; }
  .title-main { font-size: 0.95rem; }
}
