/* ============================================================
   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 .fill {
  position: absolute; inset: 0 auto 0 0; width: 0%;
  background: var(--accent); border-radius: 2px;
  transition: width var(--speed);
}
.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; } }
