/* ============================================================
   Out of Many, One People — reader
   Typography-first. Chrome recedes; the page is the interface.
   ============================================================ */

:root {
  --paper:        #faf8f4;
  --paper-edge:   #f2eee6;
  --ink:          #1c1a17;
  --ink-soft:     #55504a;
  --ink-faint:    #8d867c;
  --rule:         #e0dad0;
  --accent:       #7a2e26;      /* oxblood */
  --accent-soft:  #f0e2df;
  --highlight:    rgba(214, 178, 88, 0.32);
  --shadow:       rgba(28, 26, 23, 0.08);

  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua",
           Georgia, "Times New Roman", serif;
  --sans:  ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI",
           Helvetica, Arial, sans-serif;

  --measure: 34rem;
  --speed: 220ms;
}

:root[data-theme="sepia"] {
  --paper: #f4ecdc;  --paper-edge: #ece2ce; --ink: #33291b;
  --ink-soft: #6a5c46; --ink-faint: #948671; --rule: #ddd0b6;
  --accent: #8a4a1f; --accent-soft: #ecdcc8;
  --highlight: rgba(190, 145, 60, 0.34);
}

:root[data-theme="night"] {
  --paper: #14151a;  --paper-edge: #101116; --ink: #d9d6d0;
  --ink-soft: #a09a91; --ink-faint: #6f6a63; --rule: #2a2c34;
  --accent: #d08c72; --accent-soft: #2b2119;
  --highlight: rgba(216, 170, 90, 0.28);
  --shadow: rgba(0, 0, 0, 0.5);
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--serif);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}

body { display: flex; flex-direction: column; }

button, input, select, textarea {
  font: inherit; color: inherit;
  background: none; border: none; outline: none;
}
button { cursor: pointer; }

::selection { background: var(--highlight); }

/* ---------- Top bar ------------------------------------------------ */

.topbar {
  position: relative;
  z-index: 40;
  display: flex;
  align-items: center;
  gap: .25rem;
  padding: .55rem clamp(.6rem, 2vw, 1.4rem);
  border-bottom: 1px solid var(--rule);
  background: color-mix(in srgb, var(--paper) 88%, transparent);
  backdrop-filter: saturate(140%) blur(12px);
  flex: none;
}

.brand {
  display: flex; flex-direction: column; gap: .05rem;
  margin-right: auto; min-width: 0; padding-right: .75rem;
}
.brand .title {
  font-size: .95rem; font-weight: 600; letter-spacing: .005em;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.brand .chapter {
  font-family: var(--sans);
  font-size: .68rem; letter-spacing: .09em; text-transform: uppercase;
  color: var(--ink-faint);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

.tools { display: flex; align-items: center; gap: .15rem; }

.icon-btn {
  width: 2.1rem; height: 2.1rem;
  display: grid; place-items: center;
  border-radius: .45rem;
  color: var(--ink-soft);
  transition: background var(--speed), color var(--speed);
}
.icon-btn:hover { background: var(--paper-edge); color: var(--ink); }
.icon-btn[aria-pressed="true"], .icon-btn.active {
  background: var(--accent-soft); color: var(--accent);
}
.icon-btn svg { width: 1.05rem; height: 1.05rem; stroke-width: 1.6; }

.divider-v { width: 1px; height: 1.3rem; background: var(--rule); margin: 0 .3rem; }

/* ---------- Layout -------------------------------------------------- */

.stage { position: relative; flex: 1; min-height: 0; display: flex; }

/* #viewer is the scroller and spans the window, so the scrollbar sits at the
   page edge; #bookcol is the centred reading measure inside it. */
#viewer {
  flex: 1; min-width: 0;
  width: 100%;
  position: relative;
  overflow-y: auto;
  overflow-x: hidden;
  scrollbar-gutter: stable;
}

#bookcol {
  max-width: 38rem;
  margin: 0 auto;
  padding: 3rem 1.4rem 7rem;
  font-size: calc(1.06rem * var(--book-scale, 1));
  line-height: var(--book-lead, 1.65);
  color: var(--ink);
}

/* ---------- book typography ---------------------------------------- */

.bsec + .bsec { margin-top: 5rem; }

#bookcol p {
  margin: 0 0 1em;
  hyphens: auto; -webkit-hyphens: auto;
}
#bookcol h1, #bookcol h2, #bookcol h3, #bookcol h4 {
  font-weight: 600; line-height: 1.25;
  margin: 1.8em 0 .7em;
}
#bookcol h1 { font-size: 1.55em; margin-top: 0; }
#bookcol h2 { font-size: 1.2em; }
#bookcol h3 { font-size: 1.05em; }
#bookcol blockquote {
  margin: 1.2em 0 1.2em 1em; padding-left: 1em;
  font-style: italic; color: var(--ink-soft);
  border-left: 2px solid var(--rule);
}
#bookcol a { color: inherit; text-decoration-color: var(--ink-faint); }
#bookcol img { max-width: 100%; height: auto; }
#bookcol hr { border: none; border-top: 1px solid var(--rule); margin: 2.5em auto; width: 60%; }
#bookcol table {
  width: 100%; border-collapse: collapse;
  font-size: .88em; margin: 1.2em 0;
}
#bookcol td, #bookcol th { border: 1px solid var(--rule); padding: .35em .55em; text-align: left; }
#bookcol .footnotes { font-size: .85em; color: var(--ink-soft); }
#bookcol sup { line-height: 0; }

/* The title page sits alone, centred, like a printed half-title. */
#bookcol section[data-href$="title_page.xhtml"] {
  min-height: 62vh;
  padding-top: 16vh;
  text-align: center;
}
#bookcol section[data-href$="title_page.xhtml"] h1 { font-size: 2em; margin-bottom: 1.2rem; }
#bookcol section[data-href$="title_page.xhtml"] p { color: var(--ink-soft); }

/* highlights, note underlines, search flash */
#bookcol mark.hl { color: inherit; padding: 0; border-radius: 2px; }
#bookcol mark.nt {
  color: inherit; background: transparent; padding: 0;
  border-bottom: 2px dotted var(--accent);
}
#bookcol mark.flash {
  color: inherit; padding: 0;
  background: var(--highlight);
  border-radius: 2px;
}

/* chapter chevrons — compact and inset so they never cover the scrollbar */
.turn {
  position: absolute; top: 50%; transform: translateY(-50%);
  width: 2.6rem; height: 2.6rem;
  border-radius: 50%;
  z-index: 10; border: none; background: transparent;
  display: grid; place-items: center;
  color: var(--ink-faint); opacity: .4;
  transition: opacity var(--speed), background var(--speed), color var(--speed);
}
.turn:hover { opacity: 1; background: var(--paper-edge); color: var(--ink); }
.turn.prev { left: 1.1rem; }
.turn.next { right: 1.9rem; }   /* extra inset clears the scrollbar */
.turn svg { width: 1.35rem; height: 1.35rem; stroke-width: 1.5; }
.turn[disabled] { pointer-events: none; opacity: 0 !important; }

/* On narrow screens the gutters vanish; chapters are reached from Contents. */
@media (max-width: 900px) { .turn { display: none; } }

/* ---------- Progress ------------------------------------------------ */

.progress {
  flex: none;
  display: flex; align-items: center; gap: .9rem;
  padding: .5rem clamp(.7rem, 2.4vw, 1.5rem) .6rem;
  border-top: 1px solid var(--rule);
  font-family: var(--sans); font-size: .7rem;
  color: var(--ink-faint); letter-spacing: .04em;
  background: var(--paper);
}
.progress .track {
  position: relative; flex: 1; height: 3px;
  background: var(--rule); border-radius: 2px; cursor: pointer;
}
.progress .pct { min-width: 3.2rem; text-align: right; font-variant-numeric: tabular-nums; }
.progress .left { min-width: 7rem; }

/* ---------- Panel (drawer) ------------------------------------------ */

.panel {
  position: fixed; top: 0; right: 0; bottom: 0;
  width: min(24rem, 88vw);
  z-index: 60;
  background: var(--paper);
  border-left: 1px solid var(--rule);
  box-shadow: -18px 0 42px -30px var(--shadow);
  transform: translateX(100%);
  transition: transform 300ms cubic-bezier(.22,.61,.36,1);
  display: flex; flex-direction: column;
}
.panel.open { transform: none; }

.panel-head {
  display: flex; align-items: center; gap: .5rem;
  padding: .85rem 1rem .7rem;
  border-bottom: 1px solid var(--rule);
}
.panel-head h2 {
  margin: 0; margin-right: auto;
  font-family: var(--sans); font-size: .72rem; font-weight: 600;
  letter-spacing: .14em; text-transform: uppercase; color: var(--ink-faint);
}

.panel-body { flex: 1; overflow-y: auto; padding: .35rem 0 2rem; }

.scrim {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(20,18,16,.28);
  opacity: 0; pointer-events: none;
  transition: opacity var(--speed);
}
.scrim.show { opacity: 1; pointer-events: auto; }

/* ---------- Lists inside panels ------------------------------------- */

.toc-item {
  display: block; width: 100%; text-align: left;
  padding: .5rem 1.1rem; line-height: 1.4;
  font-size: .88rem; color: var(--ink-soft);
  border-left: 2px solid transparent;
  transition: background var(--speed), color var(--speed), border-color var(--speed);
}
.toc-item:hover { background: var(--paper-edge); color: var(--ink); }
.toc-item.current { border-left-color: var(--accent); color: var(--ink); font-weight: 600; }
.toc-item.depth-1 { padding-left: 2rem; font-size: .82rem; color: var(--ink-faint); }

.entry {
  position: relative;
  padding: .75rem 2.4rem .8rem 1.1rem;
  border-bottom: 1px solid var(--rule);
  cursor: pointer;
  transition: background var(--speed);
}
.entry:hover { background: var(--paper-edge); }
.entry .meta {
  font-family: var(--sans); font-size: .63rem; letter-spacing: .1em;
  text-transform: uppercase; color: var(--ink-faint); margin-bottom: .3rem;
  display: flex; gap: .5rem; align-items: center;
}
.entry .quote {
  font-size: .87rem; line-height: 1.5; color: var(--ink);
  display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical;
  overflow: hidden;
}
.entry .quote::before { content: "\201C"; color: var(--ink-faint); }
.entry .quote::after  { content: "\201D"; color: var(--ink-faint); }
.entry .note {
  margin-top: .45rem; padding-left: .6rem;
  border-left: 2px solid var(--accent-soft);
  font-size: .82rem; line-height: 1.5; color: var(--ink-soft);
  white-space: pre-wrap;
}
.entry .del {
  position: absolute; top: .55rem; right: .5rem;
  width: 1.6rem; height: 1.6rem; border-radius: .35rem;
  display: grid; place-items: center;
  color: var(--ink-faint); opacity: 0;
  transition: opacity var(--speed), background var(--speed), color var(--speed);
}
.entry:hover .del { opacity: 1; }
.entry .del:hover { background: var(--accent-soft); color: var(--accent); }
.entry .del svg { width: .85rem; height: .85rem; stroke-width: 1.8; }

.swatch { width: .55rem; height: .55rem; border-radius: 50%; flex: none; }

/* ---------- Search --------------------------------------------------- */

.search-bar { padding: .8rem 1rem; border-bottom: 1px solid var(--rule); }
.search-bar input {
  width: 100%; padding: .55rem .7rem;
  font-family: var(--sans); font-size: .85rem;
  background: var(--paper-edge);
  border: 1px solid var(--rule); border-radius: .45rem;
  transition: border-color var(--speed);
}
.search-bar input:focus { border-color: var(--accent); }
.search-status {
  font-family: var(--sans); font-size: .68rem; letter-spacing: .08em;
  text-transform: uppercase; color: var(--ink-faint);
  padding: .55rem 1.1rem .2rem;
}
.hit { display: block; width: 100%; text-align: left; }
.hit mark { background: var(--highlight); color: inherit; padding: 0 .1em; border-radius: 2px; }

/* ---------- Settings ------------------------------------------------- */

.setting { padding: .9rem 1.1rem; border-bottom: 1px solid var(--rule); }
.setting h3 {
  margin: 0 0 .6rem;
  font-family: var(--sans); font-size: .66rem; font-weight: 600;
  letter-spacing: .13em; text-transform: uppercase; color: var(--ink-faint);
}
.choices { display: flex; gap: .4rem; flex-wrap: wrap; }
.choice {
  padding: .38rem .7rem; border-radius: .4rem;
  border: 1px solid var(--rule);
  font-family: var(--sans); font-size: .78rem; color: var(--ink-soft);
  transition: all var(--speed);
}
.choice:hover { border-color: var(--ink-faint); color: var(--ink); }
.choice.on { background: var(--accent); border-color: var(--accent); color: #fff; }
:root[data-theme="night"] .choice.on { color: #14151a; }

.stepper { display: flex; align-items: center; gap: .55rem; }
.stepper button {
  width: 2rem; height: 2rem; border-radius: .4rem;
  border: 1px solid var(--rule); color: var(--ink-soft);
  display: grid; place-items: center; font-size: 1rem;
  transition: all var(--speed);
}
.stepper button:hover { border-color: var(--ink-faint); color: var(--ink); }
.stepper .val {
  font-family: var(--sans); font-size: .8rem; color: var(--ink-soft);
  min-width: 4rem; text-align: center; font-variant-numeric: tabular-nums;
}

.btn-row { display: flex; gap: .5rem; flex-wrap: wrap; }
.btn {
  padding: .48rem .8rem; border-radius: .4rem;
  border: 1px solid var(--rule);
  font-family: var(--sans); font-size: .78rem; color: var(--ink-soft);
  transition: all var(--speed);
}
.btn:hover { border-color: var(--accent); color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
:root[data-theme="night"] .btn.primary { color: #14151a; }
.hint {
  font-family: var(--sans); font-size: .72rem; line-height: 1.55;
  color: var(--ink-faint); margin-top: .55rem;
}

/* ---------- Selection popover ---------------------------------------- */

.popover {
  position: fixed; z-index: 70;
  display: none; align-items: center; gap: .2rem;
  padding: .3rem;
  background: var(--paper);
  border: 1px solid var(--rule); border-radius: .55rem;
  box-shadow: 0 12px 32px -12px var(--shadow), 0 2px 6px -2px var(--shadow);
}
.popover.show { display: flex; }
.popover button {
  width: 1.9rem; height: 1.9rem; border-radius: .38rem;
  display: grid; place-items: center; color: var(--ink-soft);
  transition: background var(--speed), color var(--speed);
}
.popover button:hover { background: var(--paper-edge); color: var(--ink); }
.popover button svg { width: 1rem; height: 1rem; stroke-width: 1.7; }
.pen { width: 1.05rem; height: 1.05rem; border-radius: 50%; border: 1px solid var(--rule); }

/* ---------- Note composer -------------------------------------------- */

.composer {
  position: fixed; z-index: 80; inset: auto;
  left: 50%; top: 50%; transform: translate(-50%, -50%);
  width: min(30rem, 90vw);
  background: var(--paper);
  border: 1px solid var(--rule); border-radius: .7rem;
  box-shadow: 0 30px 70px -30px var(--shadow);
  padding: 1.1rem; display: none;
}
.composer.show { display: block; }
.composer blockquote {
  margin: 0 0 .8rem; padding-left: .75rem;
  border-left: 2px solid var(--accent-soft);
  font-size: .88rem; line-height: 1.55; color: var(--ink-soft);
  max-height: 7rem; overflow-y: auto;
}
.composer textarea {
  width: 100%; min-height: 6.5rem; resize: vertical;
  padding: .6rem .7rem; font-family: var(--sans); font-size: .86rem;
  line-height: 1.55; background: var(--paper-edge);
  border: 1px solid var(--rule); border-radius: .45rem;
}
.composer textarea:focus { border-color: var(--accent); }
.composer .actions { display: flex; justify-content: flex-end; gap: .5rem; margin-top: .75rem; }

/* ---------- Empty / loading ------------------------------------------ */

.empty {
  padding: 2.5rem 1.4rem; text-align: center;
  font-family: var(--sans); font-size: .8rem; line-height: 1.6;
  color: var(--ink-faint);
}

.loading {
  position: fixed; inset: 0; z-index: 200;
  display: grid; place-items: center;
  background: var(--paper);
  transition: opacity 400ms;
}
.loading.gone { opacity: 0; pointer-events: none; }
.loading .mark {
  text-align: center;
  font-size: 1.05rem; letter-spacing: .02em; color: var(--ink-soft);
}
.loading .mark small {
  display: block; margin-top: .6rem;
  font-family: var(--sans); font-size: .68rem; letter-spacing: .16em;
  text-transform: uppercase; color: var(--ink-faint);
}

.toast {
  position: fixed; left: 50%; bottom: 4.5rem; transform: translateX(-50%) translateY(1rem);
  z-index: 90;
  padding: .5rem .9rem; border-radius: .45rem;
  background: var(--ink); color: var(--paper);
  font-family: var(--sans); font-size: .78rem;
  opacity: 0; pointer-events: none;
  transition: opacity var(--speed), transform var(--speed);
}
.toast.show { opacity: .95; transform: translateX(-50%); }

@media (max-width: 640px) {
  .brand .title { font-size: .85rem; }
  .progress .left { display: none; }
}

@media print { .topbar, .progress, .panel, .scrim, .turn { display: none !important; } }

/* clib integration: retain the original reading layout and complete controls. */
html, body { height: 100%; height: 100dvh; }
:root { --ink-faint: #706960; }
:root[data-theme="sepia"] { --ink-faint: #766953; }
:root[data-theme="night"] { --ink-faint: #9c968e; }
:root[data-theme="night"] #bookcol mark.hl { color: #ffffff; }
[hidden] { display: none !important; }
.library-link { display: inline-flex; align-items: center; min-height: 44px; padding-inline: .4rem; margin-right: .6rem; color: var(--ink-soft); font: .8rem var(--sans); text-underline-offset: .25em; }
.icon-btn { width: 44px; height: 44px; min-width: 44px; min-height: 44px; }
.icon-btn svg { width: 20px; height: 20px; }
.icon-btn:disabled { opacity: .4; cursor: default; }
:where(button, a, input, textarea, [tabindex]):focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; transition: none; }
#viewer { overflow-x: auto; }
#viewer:focus-visible { outline-offset: -3px; }
#bookcol { min-width: 0; overflow-wrap: anywhere; }
#bookcol svg { max-width: 100%; height: auto; }
#bookcol section[data-href$="cover.xhtml"] svg { display: block; width: min(100%, 24rem); max-height: 75vh; margin: auto; }
.legacy-anchor { display: block; height: 0; width: 0; }
.book-table { max-width: 100%; overflow-x: auto; }
.book-table table { min-width: 28rem; }
.turn { width: 44px; height: 44px; }
.mobile-turn { display: none; }
.progress { min-height: 52px; padding-block: .15rem max(.15rem, env(safe-area-inset-bottom)); }
.progress .pct { min-width: 4ch; }
.progress .track { appearance: auto; -webkit-appearance: auto; accent-color: var(--accent); min-width: 32px; height: 44px; margin: 0; cursor: pointer; }
.panel { transform: none; visibility: hidden; opacity: 0; pointer-events: none; transition: opacity 120ms; }
.panel.open { visibility: visible; opacity: 1; pointer-events: auto; }
.panel-head h2 { color: var(--ink-soft); font-size: .8rem; }
.panel-body { padding-bottom: max(2rem, env(safe-area-inset-bottom)); }
.toc-item, .choice, .btn { min-height: 44px; }
.toc-item.depth-1 { color: var(--ink-soft); }
.choice, .btn { display: inline-flex; align-items: center; justify-content: center; text-decoration: none; line-height: 1.5; max-width: 100%; }
.stepper button { width: 44px; height: 44px; min-width: 44px; }
.entry { cursor: default; padding-right: 3.4rem; }
.entry-open { display: block; width: 100%; text-align: left; padding: 0; }
.entry .meta, .hint, .search-status { color: var(--ink-soft); }
.entry .del { width: 44px; height: 44px; opacity: 1; }
.hit { cursor: pointer; }
.hit .quote, .entry-open .quote { display: block; overflow: visible; -webkit-line-clamp: unset; }
.entry-open .note { display: block; }
.entry-open .quote.plain { font-family: var(--sans); font-size: .8rem; color: var(--ink-soft); }
.revisions { display: grid; gap: 1.1rem; margin: 0; padding: 0; list-style: none; }
.revision { font: .8rem/1.6 var(--sans); }
.revision > div:first-child { display: flex; align-items: center; justify-content: space-between; gap: .6rem; flex-wrap: wrap; }
.revision a { color: var(--ink-soft); min-height: 44px; display: inline-flex; align-items: center; text-underline-offset: .25em; }
.revision a[aria-current] { color: var(--ink); font-weight: 600; }
.revision time { font-size: .72rem; color: var(--ink-soft); }
.revision-actions { display: flex; align-items: center; flex-wrap: wrap; gap: .75rem; }
.revision-actions button { min-height: 44px; padding: .5rem .75rem; border-radius: .4rem; background: var(--paper-edge); font: .78rem/1.5 var(--sans); max-width: 100%; }
.revision-actions button:disabled { opacity: .65; }
.copy-status { display: block; color: var(--ink-soft); font-size: .72rem; }
.copy-status:empty { display: none; }
.popover { max-width: calc(100vw - 16px); padding: 4px; gap: 0; }
.popover button { width: 44px; height: 44px; }
.popover .divider-v { margin-inline: 4px; }
.pen { width: 20px; height: 20px; }
[data-color="gold"] .pen { background: #e3c25f; }
[data-color="rose"] .pen { background: #e0a3a3; }
[data-color="sea"] .pen { background: #9fc3c9; }
[data-color="olive"] .pen { background: #b6c69a; }
.composer { max-height: calc(100dvh - 2rem); overflow-y: auto; }
.toast { transform: translateX(-50%); max-width: calc(100vw - 2rem); line-height: 1.5; transition: opacity 120ms; }
.toast.show { transform: translateX(-50%); }
.loading { transition: opacity 160ms; }
.book-error { padding-top: 2rem; }
.book-error .btn { margin: .4rem .4rem 0 0; }
.noscript-reader { position: fixed; inset: 0; z-index: 201; display: grid; place-content: center; gap: 1rem; padding: 2rem; background: var(--paper); font: 1rem/1.6 var(--sans); }

@media (max-width: 640px) {
  .topbar { flex-wrap: wrap; padding: .35rem .5rem; }
  .brand { flex: 1; }
  .brand .chapter { letter-spacing: .025em; text-transform: none; font-size: .75rem; color: var(--ink-soft); }
  .tools { flex-basis: 100%; justify-content: space-around; }
  .tools .divider-v { display: none; }
  .tools .icon-btn { flex: 1 1 44px; max-width: 64px; width: auto; }
  #bookcol { padding: 2rem 1.25rem 4rem; }
  .progress { gap: 8px; padding-inline: 8px; }
  .progress .pct { letter-spacing: 0; }
  .mobile-turn { display: inline-grid; place-items: center; width: 44px; height: 44px; min-width: 44px; font: 1.25rem var(--sans); color: var(--ink-soft); }
  .mobile-turn:disabled { opacity: .4; }
  .panel { width: min(24rem, calc(100vw - 1rem)); }
  .panel-head { padding: .65rem .75rem; }
  .setting { padding-inline: .9rem; }
  .revision > div:first-child { display: block; }
  .revision time { display: block; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; scroll-behavior: auto !important; }
}
@media (forced-colors: active) {
  .choice.on { outline: 2px solid Highlight; }
  .entry .del, .icon-btn { color: ButtonText; }
}

