/* The Library — warm, book-like, typography-forward. Light + dark. */

:root {
  --parchment: #FDF8EE;
  --paper: #FFFDF7;
  --ink: #2B2118;
  --ink-soft: #5C4F41;
  --ink-faint: #8A7A67;
  --leather: #78716C;
  --spine: #92400E;
  --amber: #B45309;
  --amber-bright: #D97706;
  --line: #E8DFCC;
  --line-soft: #F0E9D8;
  --mark: #FDE9C0;
  --mark-ink: #6B3E09;
  --card: #FFFDF7;
  --shadow: 0 1px 2px rgba(60, 42, 20, .05), 0 4px 16px rgba(60, 42, 20, .06);
  --shadow-lift: 0 2px 4px rgba(60, 42, 20, .08), 0 12px 32px rgba(60, 42, 20, .12);
  --focus-ring: 0 0 0 3px rgba(217, 119, 6, .35);
  --radius: 10px;
  --content-w: 46rem;
  --font-display: "Cormorant Garamond", Georgia, "Times New Roman", serif;
  --font-body: "Crimson Pro", Georgia, "Iowan Old Style", serif;
  --player-h: 4.5rem;
}

:root[data-theme="dark"] {
  --parchment: #1C1712;
  --paper: #241E17;
  --ink: #EDE4D3;
  --ink-soft: #C4B49C;
  --ink-faint: #94866F;
  --leather: #A8A29E;
  --spine: #D9A05B;
  --amber: #E8A33D;
  --amber-bright: #F0B254;
  --line: #3A3128;
  --line-soft: #2E2720;
  --mark: #4A3416;
  --mark-ink: #F5C97B;
  --card: #241E17;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 16px rgba(0, 0, 0, .25);
  --shadow-lift: 0 2px 4px rgba(0, 0, 0, .4), 0 12px 32px rgba(0, 0, 0, .45);
  --focus-ring: 0 0 0 3px rgba(240, 178, 84, .4);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --parchment: #1C1712;
    --paper: #241E17;
    --ink: #EDE4D3;
    --ink-soft: #C4B49C;
    --ink-faint: #94866F;
    --leather: #A8A29E;
    --spine: #D9A05B;
    --amber: #E8A33D;
    --amber-bright: #F0B254;
    --line: #3A3128;
    --line-soft: #2E2720;
    --mark: #4A3416;
    --mark-ink: #F5C97B;
    --card: #241E17;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 16px rgba(0, 0, 0, .25);
    --shadow-lift: 0 2px 4px rgba(0, 0, 0, .4), 0 12px 32px rgba(0, 0, 0, .45);
    --focus-ring: 0 0 0 3px rgba(240, 178, 84, .4);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { scroll-behavior: smooth; }
@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}

body {
  margin: 0;
  background: var(--parchment);
  color: var(--ink);
  font-family: var(--font-body);
  font-size: 1.125rem;
  line-height: 1.65;
  -webkit-font-smoothing: antialiased;
  padding-bottom: calc(var(--player-h) + 2rem);
}

.visually-hidden {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}

:focus-visible { outline: none; box-shadow: var(--focus-ring); border-radius: 6px; }

::selection { background: var(--mark); color: var(--mark-ink); }

/* ---------- Header ---------- */
.site-header { border-bottom: 1px solid var(--line); background: var(--parchment); }
.header-inner {
  max-width: var(--content-w); margin: 0 auto; padding: 1.1rem 1.25rem .9rem;
  display: flex; align-items: baseline; gap: 1rem; flex-wrap: wrap;
}
.wordmark {
  font-family: var(--font-display); font-weight: 700; font-size: 1.55rem;
  color: var(--ink); text-decoration: none; letter-spacing: .01em;
  display: inline-flex; align-items: center; gap: .5rem;
}
.wordmark-icon { width: 1.25rem; height: 1.25rem; color: var(--spine); transform: translateY(2px); }
.tagline { margin: 0; color: var(--ink-faint); font-size: .95rem; font-style: italic; flex: 1; }
.theme-toggle {
  margin-left: auto; background: none; border: 1px solid var(--line); border-radius: 999px;
  width: 2.4rem; height: 2.4rem; display: grid; place-items: center; cursor: pointer;
  color: var(--ink-soft); transition: border-color .2s, color .2s; align-self: center;
}
.theme-toggle:hover { border-color: var(--amber); color: var(--amber); }
.theme-toggle svg { width: 1.1rem; height: 1.1rem; }
:root[data-theme="dark"] .icon-moon { display: none; }
:root:not([data-theme="dark"]) .icon-sun { display: none; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .icon-moon { display: none; }
  :root:not([data-theme="light"]) .icon-sun { display: block; }
}

/* ---------- Layout ---------- */
.content { max-width: var(--content-w); margin: 0 auto; padding: 1.5rem 1.25rem 3rem; }

/* ---------- Mode tabs ---------- */
.mode-tabs { display: inline-flex; gap: 1.25rem; margin-bottom: 1rem; }
.mode-tab {
  background: none; border: none; padding: .25rem 0; cursor: pointer;
  font-family: var(--font-display); font-size: 1.2rem; font-weight: 600;
  color: var(--ink-faint); border-bottom: 2px solid transparent;
  transition: color .2s, border-color .2s;
}
.mode-tab:hover { color: var(--ink-soft); }
.mode-tab.active { color: var(--ink); border-bottom-color: var(--amber); }

/* ---------- Search ---------- */
.search-form { display: flex; gap: .6rem; }
.search-box {
  flex: 1; position: relative; display: flex; align-items: center;
  background: var(--paper); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); transition: border-color .2s, box-shadow .2s;
}
.search-box:focus-within { border-color: var(--amber); box-shadow: var(--focus-ring); }
.search-icon { width: 1.15rem; height: 1.15rem; margin-left: .9rem; color: var(--ink-faint); flex: none; }
#searchInput {
  flex: 1; border: none; background: none; padding: .85rem .75rem; min-width: 0;
  font-family: var(--font-body); font-size: 1.1rem; color: var(--ink);
}
#searchInput:focus { outline: none; box-shadow: none; }
#searchInput::placeholder { color: var(--ink-faint); font-style: italic; }
.slash-hint {
  margin-right: .8rem; border: 1px solid var(--line); border-radius: 5px;
  padding: 0 .45rem; font-size: .8rem; color: var(--ink-faint);
  font-family: var(--font-body); background: var(--line-soft);
}
.search-box:focus-within .slash-hint { visibility: hidden; }
.search-submit {
  border: none; border-radius: var(--radius); padding: 0 1.4rem; cursor: pointer;
  background: var(--spine); color: #FFF8EC; font-family: var(--font-display);
  font-size: 1.1rem; font-weight: 600; letter-spacing: .02em;
  transition: background .2s, transform .1s; box-shadow: var(--shadow);
}
.search-submit:hover { background: var(--amber); }
.search-submit:active { transform: scale(.97); }

/* ---------- Filters ---------- */
.filters { display: flex; gap: .75rem; margin-top: .9rem; align-items: center; flex-wrap: wrap; }
.segmented { display: inline-flex; border: 1px solid var(--line); border-radius: 999px; overflow: hidden; background: var(--paper); }
.segmented button {
  border: none; background: none; padding: .42rem 1rem; cursor: pointer;
  font-family: var(--font-body); font-size: .95rem; color: var(--ink-soft);
  display: inline-flex; align-items: center; gap: .4rem; transition: background .2s, color .2s;
}
.segmented button + button { border-left: 1px solid var(--line); }
.segmented button svg { width: .95rem; height: .95rem; }
.segmented button:hover { color: var(--ink); }
.segmented button.active { background: var(--spine); color: #FFF8EC; }
.author-filter select {
  border: 1px solid var(--line); border-radius: 999px; background: var(--paper);
  color: var(--ink-soft); padding: .42rem 1rem; font-family: var(--font-body);
  font-size: .95rem; cursor: pointer;
}

/* ---------- Hero / empty ---------- */
.hero { text-align: center; padding: 3.5rem 1rem 2rem; }
.hero h1 {
  font-family: var(--font-display); font-weight: 600; font-size: clamp(1.9rem, 5vw, 2.6rem);
  margin: 0 0 .75rem; line-height: 1.2;
}
.hero p { color: var(--ink-soft); max-width: 34rem; margin: 0 auto; }
.stats-line { color: var(--ink-faint); font-size: .95rem; margin-top: 1rem; font-style: italic; }
.try-row {
  margin-top: 1.6rem; display: flex; gap: .5rem; justify-content: center;
  align-items: center; flex-wrap: wrap;
}
.try-label { color: var(--ink-faint); font-style: italic; font-size: .95rem; }
.try-chip {
  border: 1px solid var(--line); background: var(--paper); color: var(--ink-soft);
  border-radius: 999px; padding: .35rem .9rem; cursor: pointer;
  font-family: var(--font-body); font-size: .92rem; font-style: italic;
  transition: border-color .2s, color .2s, box-shadow .2s;
}
.try-chip:hover { border-color: var(--amber); color: var(--amber); box-shadow: var(--shadow); }

.empty-state, .error-state { text-align: center; padding: 3rem 1rem; color: var(--ink-soft); }
.empty-state svg, .error-state svg { width: 2.2rem; height: 2.2rem; color: var(--ink-faint); margin-bottom: .6rem; }
.error-state { color: #A3412C; }
:root[data-theme="dark"] .error-state { color: #E88A70; }

/* ---------- Results ---------- */
.results-head {
  display: flex; justify-content: space-between; align-items: baseline;
  margin: 1.6rem 0 .8rem; color: var(--ink-faint); font-size: .95rem; font-style: italic;
}
.result-list { list-style: none; margin: 0; padding: 0; display: grid; gap: .8rem; }
.result-card {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 1rem 1.15rem; box-shadow: var(--shadow); cursor: pointer;
  transition: border-color .2s, box-shadow .2s, transform .15s;
}
.result-card:hover { border-color: var(--amber); box-shadow: var(--shadow-lift); transform: translateY(-1px); }
.result-card:focus-visible { border-color: var(--amber); }
.result-top { display: flex; align-items: center; gap: .55rem; flex-wrap: wrap; }
.result-kind {
  display: inline-flex; align-items: center; gap: .35rem; font-size: .82rem;
  color: var(--amber); border: 1px solid currentColor; border-radius: 999px;
  padding: .1rem .6rem; letter-spacing: .03em;
}
.result-kind svg { width: .8rem; height: .8rem; }
.result-kind.kind-text { color: var(--leather); }
.result-title { font-family: var(--font-display); font-weight: 600; font-size: 1.18rem; line-height: 1.3; }
.result-sub { color: var(--ink-faint); font-size: .9rem; margin-top: .15rem; }
.result-snippet { margin: .55rem 0 0; color: var(--ink-soft); font-size: 1.02rem; }
.result-snippet mark, .reader-body mark, .ask-answer-text mark {
  background: var(--mark); color: var(--mark-ink); border-radius: 3px; padding: 0 .12em;
}
.result-time { font-variant-numeric: tabular-nums; }

/* deep search */
.deep-status { margin: 1.4rem 0 .5rem; color: var(--ink-faint); font-style: italic; }
.deep-queries { display: flex; flex-wrap: wrap; gap: .4rem; margin-bottom: 1rem; }
.deep-chip {
  border: 1px solid var(--line); border-radius: 999px; padding: .18rem .7rem;
  font-size: .82rem; font-style: italic; color: var(--ink-faint); background: var(--paper);
}
.result-why {
  margin: .5rem 0 0; font-size: .95rem; font-style: italic; color: var(--mark-ink);
  border-left: 3px solid var(--amber-bright); padding-left: .7rem;
}

/* skeletons */
.skeleton-list { display: grid; gap: .8rem; margin-top: 1.6rem; }
.skeleton {
  height: 6.2rem; border-radius: var(--radius); border: 1px solid var(--line-soft);
  background: linear-gradient(100deg, var(--line-soft) 40%, var(--paper) 50%, var(--line-soft) 60%);
  background-size: 200% 100%; animation: shimmer 1.3s infinite linear;
}
@keyframes shimmer { to { background-position: -200% 0; } }

/* ---------- Ask ---------- */
.ask-answer {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 1.4rem 1.5rem; margin-top: 1.4rem;
}
.ask-answer-text { font-size: 1.1rem; }
.ask-answer-text p { margin: 0 0 .8em; }
.ask-answer-text p:last-child { margin-bottom: 0; }
.ask-answer-text ul { margin: 0 0 .8em; padding-left: 1.3em; }
.ask-answer-text li { margin-bottom: .45em; }
.cite {
  display: inline-block; background: none; border: none; padding: 0 .1em; cursor: pointer;
  color: var(--amber); font-weight: 600; font-family: inherit; font-size: .85em;
  vertical-align: baseline; text-decoration: none;
}
.cite:hover { text-decoration: underline; }
.ask-status { color: var(--ink-faint); font-style: italic; }
.ask-cursor { display: inline-block; width: .5em; animation: blink 1s steps(1) infinite; color: var(--amber); }
@keyframes blink { 50% { opacity: 0; } }
.ask-excerpts { margin-top: 1.4rem; border-top: 1px solid var(--line); padding-top: 1rem; }
.ask-excerpts h3 {
  font-family: var(--font-display); font-size: 1.05rem; margin: 0 0 .6rem;
  color: var(--ink-soft); font-weight: 600; letter-spacing: .03em;
}
.excerpt-item {
  display: flex; gap: .6rem; padding: .5rem .4rem; border-radius: 8px; cursor: pointer;
  transition: background .15s;
}
.excerpt-item:hover { background: var(--line-soft); }
.excerpt-num { color: var(--amber); font-weight: 600; flex: none; }
.excerpt-body { font-size: .95rem; color: var(--ink-soft); }
.excerpt-body strong { color: var(--ink); font-weight: 600; }

/* ---------- Reader ---------- */
.reader-overlay {
  position: fixed; inset: 0; z-index: 100; background: rgba(30, 20, 8, .55);
  display: grid; place-items: stretch; padding: 0;
  animation: fadeIn .2s ease-out;
}
@keyframes fadeIn { from { opacity: 0; } }
.reader {
  background: var(--parchment); margin: 0 auto; width: 100%; max-width: 52rem;
  height: 100%; overflow-y: auto; box-shadow: var(--shadow-lift);
  display: flex; flex-direction: column;
}
.reader-head {
  position: sticky; top: 0; background: var(--parchment); z-index: 2;
  display: flex; justify-content: space-between; align-items: flex-start; gap: 1rem;
  padding: 1.4rem 2rem 1rem; border-bottom: 1px solid var(--line);
}
.reader-head h2 { font-family: var(--font-display); font-size: 1.6rem; margin: 0; line-height: 1.25; }
.reader-byline { margin: .3rem 0 0; color: var(--ink-faint); font-style: italic; font-size: .95rem; }
.reader-close {
  flex: none; background: none; border: 1px solid var(--line); border-radius: 999px;
  width: 2.5rem; height: 2.5rem; display: grid; place-items: center; cursor: pointer;
  color: var(--ink-soft); transition: border-color .2s, color .2s;
}
.reader-close:hover { border-color: var(--amber); color: var(--amber); }
.reader-close svg { width: 1.1rem; height: 1.1rem; }
.reader-body {
  padding: 1.6rem 2rem 4rem; max-width: 42rem; margin: 0 auto; width: 100%;
  font-size: 1.15rem; line-height: 1.75;
}
.reader-body p { margin: 0 0 1em; }
.reader-body p.passage-para {
  background: linear-gradient(90deg, color-mix(in srgb, var(--mark) 45%, transparent), transparent 92%);
  border-left: 3px solid var(--amber-bright);
  padding: .35em .6em .35em .9em; margin-left: -.9em; border-radius: 0 6px 6px 0;
}

/* ---------- Player ---------- */
.player {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 90;
  min-height: var(--player-h);
  background: var(--paper); border-top: 1px solid var(--line);
  box-shadow: 0 -4px 24px rgba(50, 35, 15, .1);
  display: flex; align-items: center; gap: .6rem;
  padding: .55rem max(1rem, env(safe-area-inset-right)) calc(.55rem + env(safe-area-inset-bottom)) max(1rem, env(safe-area-inset-left));
}
.player-btn {
  background: none; border: none; cursor: pointer; color: var(--ink-soft);
  width: 2.6rem; height: 2.6rem; border-radius: 999px; display: grid; place-items: center;
  position: relative; transition: color .2s, background .2s; flex: none;
}
.player-btn:hover { color: var(--amber); background: var(--line-soft); }
.player-btn svg { width: 1.3rem; height: 1.3rem; }
.player-play {
  background: var(--spine); color: #FFF8EC; width: 3rem; height: 3rem;
}
.player-play:hover { background: var(--amber); color: #FFF8EC; }
.player.playing .icon-play { display: none; }
.player:not(.playing) .icon-pause { display: none; }
.skip-num {
  position: absolute; inset: 0; display: grid; place-items: center;
  font-size: .55rem; font-weight: 600; padding-top: .15rem;
}
.player-meta { flex: 1; min-width: 0; }
.player-title {
  margin: 0 0 .1rem; font-family: var(--font-display); font-weight: 600; font-size: 1rem;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.player-time { display: flex; align-items: center; gap: .55rem; font-size: .82rem; color: var(--ink-faint); font-variant-numeric: tabular-nums; }
#playerScrub {
  flex: 1; appearance: none; height: 4px; border-radius: 2px; background: var(--line);
  cursor: pointer; accent-color: var(--amber-bright);
}
#playerScrub::-webkit-slider-thumb {
  appearance: none; width: 14px; height: 14px; border-radius: 50%;
  background: var(--amber-bright); border: 2px solid var(--paper); box-shadow: var(--shadow);
}
#playerScrub::-moz-range-thumb {
  width: 12px; height: 12px; border-radius: 50%; background: var(--amber-bright);
  border: 2px solid var(--paper);
}
.player-speed {
  flex: none; background: none; border: 1px solid var(--line); border-radius: 999px;
  padding: .3rem .7rem; cursor: pointer; color: var(--ink-soft);
  font-family: var(--font-body); font-size: .85rem; font-variant-numeric: tabular-nums;
  transition: border-color .2s, color .2s;
}
.player-speed:hover { border-color: var(--amber); color: var(--amber); }

/* ---------- Responsive ---------- */
@media (max-width: 640px) {
  body { font-size: 1.06rem; }
  .header-inner { padding: .9rem 1rem .7rem; }
  .tagline { display: none; }
  .content { padding: 1.1rem 1rem 3rem; }
  .search-submit { display: none; }
  .hero { padding: 2.2rem .25rem 1.2rem; }
  .reader-head { padding: 1rem 1.1rem .8rem; }
  .reader-body { padding: 1.2rem 1.1rem 4rem; }
  .player { gap: .3rem; }
  .player-time span { font-size: .75rem; }
  #playerBack, #playerFwd { display: none; }
  .slash-hint { display: none; }
}
