:root {
  --bg: #f7f1e6;
  --paper: #fffaf0;
  --text: #241f19;
  --muted: #6b604f;
  --rule: #ded0ba;
  --accent: #7f4f24;
  --accent-text: #fffaf0;
  --shadow: rgba(42, 29, 16, 0.12);
  --font-scale: 1;
}

html[data-theme="light"] {
  --bg: #f5f5f2;
  --paper: #fff;
  --text: #171717;
  --muted: #65625d;
  --rule: #dedbd4;
  --accent: #6b4a2d;
  --accent-text: #fff;
  --shadow: rgba(0, 0, 0, 0.1);
}

html[data-theme="dark"] {
  --bg: #14110e;
  --paper: #201b16;
  --text: #eee4d5;
  --muted: #baa98f;
  --rule: #3a3027;
  --accent: #e4b879;
  --accent-text: #1d1510;
  --shadow: rgba(0, 0, 0, 0.35);
}

* { box-sizing: border-box; }

html { scroll-behavior: smooth; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Georgia, "Times New Roman", serif;
  line-height: 1.72;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 1rem;
  min-height: 68px;
  padding: 0.7rem clamp(1rem, 3vw, 2rem);
  background: color-mix(in srgb, var(--paper) 92%, transparent);
  border-bottom: 1px solid var(--rule);
  box-shadow: 0 8px 24px var(--shadow);
  backdrop-filter: blur(14px);
}

.brand {
  display: grid;
  line-height: 1.15;
  min-width: 0;
}

.library-link {
  color: var(--muted);
  border: 1px solid var(--rule);
  border-radius: 6px;
  padding: 0.42rem 0.62rem;
  font: 700 0.8rem system-ui, sans-serif;
  text-decoration: none;
}

.library-link:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.brand strong {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 1rem;
}

.kicker,
.eyebrow {
  color: var(--muted);
  font-family: system-ui, sans-serif;
  font-size: 0.75rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.reader-tools {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 0.45rem;
  margin-left: auto;
}

.control-group {
  display: flex;
  align-items: center;
  gap: 0.12rem;
  overflow-x: auto;
  border: 1px solid var(--rule);
  border-radius: 8px;
  background: color-mix(in srgb, var(--paper) 78%, var(--bg));
  padding: 0.18rem;
}

button {
  color: var(--text);
  background: transparent;
  border: 0;
  border-radius: 6px;
  padding: 0.45rem 0.65rem;
  font: 600 0.82rem system-ui, sans-serif;
  cursor: pointer;
}

.icon-only {
  min-width: 2.25rem;
  padding-inline: 0.55rem;
  font-size: 0.95rem;
  line-height: 1;
}

select {
  max-width: 10rem;
  color: var(--text);
  background: transparent;
  border: 0;
  border-radius: 6px;
  padding: 0.45rem 0.35rem;
  font: 600 0.82rem system-ui, sans-serif;
}

button:hover,
button.active {
  color: var(--accent);
}

.control-group button {
  color: var(--text);
}

.control-group select {
  color: var(--text);
}

.control-group button.active {
  color: var(--accent-text);
  background: var(--accent);
  box-shadow: 0 1px 4px var(--shadow);
}

.control-group button:not(.active):hover {
  background: color-mix(in srgb, var(--accent) 10%, transparent);
}

.icon-button { display: none; }

.font-size {
  min-width: 3.3rem;
  color: var(--muted);
  text-align: center;
  font: 700 0.78rem system-ui, sans-serif;
}

.shell {
  display: grid;
  grid-template-columns: minmax(220px, 300px) minmax(0, 1fr);
  gap: clamp(1rem, 3vw, 3rem);
  width: min(1320px, 100%);
  margin: 0 auto;
  padding: clamp(1rem, 3vw, 2rem);
}

.toc {
  position: sticky;
  top: 92px;
  align-self: start;
  max-height: calc(100vh - 110px);
  overflow: auto;
  border-right: 1px solid var(--rule);
  padding-right: 1rem;
}

.toc-header {
  margin-bottom: 0.75rem;
  color: var(--muted);
  font: 700 0.78rem system-ui, sans-serif;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.toc a {
  display: block;
  color: var(--muted);
  text-decoration: none;
  font: 0.95rem/1.35 system-ui, sans-serif;
  padding: 0.42rem 0;
}

.toc a:hover,
.toc a.current { color: var(--accent); }

body.outline-hidden .shell {
  display: block;
  width: min(880px, 100%);
}

body.outline-hidden .toc {
  display: none;
}

.reader {
  width: min(100%, 760px);
  margin: 0 auto;
}

.cover {
  min-height: min(72vh, 680px);
  display: grid;
  align-content: center;
  padding: clamp(2rem, 8vw, 5rem) 0;
  border-bottom: 1px solid var(--rule);
}

.cover h1 {
  max-width: 12ch;
  margin: 0.25rem 0 1rem;
  font-size: clamp(3rem, 11vw, 6.8rem);
  line-height: 0.92;
}

.cover p:not(.eyebrow) {
  max-width: 30rem;
  margin: 0;
  color: var(--muted);
  font-size: 1.25rem;
}

.cover span {
  margin-top: 2rem;
  color: var(--accent);
  font: 700 0.9rem system-ui, sans-serif;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.book-section {
  padding: clamp(2.5rem, 6vw, 5rem) 0;
  border-bottom: 1px solid var(--rule);
}

.book-section h1,
.book-section h2 {
  margin: 0 0 1.5rem;
  font-size: clamp(2rem, 6vw, 3.7rem);
  line-height: 1.02;
  letter-spacing: 0;
}

.book-section p {
  margin: 0 0 1.15rem;
  font-size: calc(1.12rem * var(--font-scale));
}

.book-section p + p {
  text-indent: 1.4em;
}

.standalone {
  color: var(--muted);
  font-style: italic;
  text-indent: 0 !important;
}

.reader-mark {
  color: inherit;
  background: color-mix(in srgb, var(--accent) 34%, transparent);
  border-radius: 3px;
  padding: 0.02em 0.04em;
}

.reader-mark.has-note {
  border-bottom: 2px solid var(--accent);
  cursor: help;
}

.note-pill {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 1.35em;
  height: 1.35em;
  margin-left: 0.2em;
  color: var(--accent-text);
  background: var(--accent);
  border-radius: 999px;
  font: 700 0.72rem system-ui, sans-serif;
  vertical-align: middle;
}

.selection-tooltip {
  position: absolute;
  z-index: 30;
  display: none;
  gap: 0.18rem;
  padding: 0.2rem;
  background: var(--paper);
  border: 1px solid var(--rule);
  border-radius: 8px;
  box-shadow: 0 12px 30px var(--shadow);
}

.selection-tooltip.visible {
  display: flex;
}

.selection-tooltip button {
  color: var(--text);
  background: transparent;
  border-radius: 6px;
  padding: 0.45rem 0.6rem;
}

.selection-tooltip button:hover {
  color: var(--accent-text);
  background: var(--accent);
}

.back-to-top {
  position: fixed;
  right: 1rem;
  bottom: 1rem;
  display: none;
  width: 42px;
  height: 42px;
  border-radius: 999px;
  background: var(--paper);
  border: 1px solid var(--rule);
  box-shadow: 0 10px 24px var(--shadow);
  font-size: 1.2rem;
  line-height: 1;
}

.back-to-top.visible { display: block; }

.focus-exit {
  position: fixed;
  top: 1rem;
  right: 1rem;
  z-index: 20;
  display: none;
  background: var(--paper);
  border: 1px solid var(--rule);
  box-shadow: 0 10px 24px var(--shadow);
}

body.focus-mode .topbar,
body.focus-mode .toc,
body.focus-mode .back-to-top {
  display: none !important;
}

body.focus-mode .shell {
  display: block;
  width: min(880px, 100%);
  padding-top: 0;
}

body.focus-mode .cover {
  min-height: 34vh;
}

body.focus-mode .focus-exit {
  display: block;
}

.library-page {
  min-height: 100vh;
  background: var(--bg);
}

.library-header {
  display: flex;
  align-items: center;
  gap: 2rem;
  justify-content: space-between;
  width: min(1120px, 100%);
  margin: 0 auto;
  padding: clamp(2rem, 7vw, 5rem) clamp(1rem, 4vw, 2rem) clamp(1.5rem, 4vw, 3rem);
}

.library-brand {
  display: flex;
  align-items: center;
  gap: clamp(1rem, 3vw, 1.5rem);
}

.library-brand img {
  flex: 0 0 auto;
  width: clamp(64px, 10vw, 96px);
  height: clamp(64px, 10vw, 96px);
  border: 1px solid color-mix(in srgb, #d7a84f 50%, var(--rule));
  border-radius: 20px;
  background: var(--paper);
  box-shadow: 0 18px 40px var(--shadow), 0 0 0 6px color-mix(in srgb, #3f1515 8%, transparent);
}

.library-header h1 {
  margin: 0.1rem 0 0.75rem;
  font-size: clamp(3.2rem, 12vw, 7rem);
  line-height: 0.9;
  color: #3f1515;
}

.library-header p:not(.eyebrow) {
  max-width: 42rem;
  margin: 0;
  color: var(--muted);
  font-size: 1.2rem;
}

.library-main {
  width: min(1120px, 100%);
  margin: 0 auto;
  padding: 0 clamp(1rem, 4vw, 2rem) clamp(3rem, 8vw, 6rem);
}

.library-section-heading {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.8rem 0;
  border-top: 3px solid #6b3f25;
  color: var(--muted);
  font: 700 0.8rem system-ui, sans-serif;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.library-section-heading strong {
  color: #3f1515;
  font-size: 0.78rem;
}

.library-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 1rem;
  padding-top: 1rem;
}

.book-card {
  min-height: 280px;
  display: grid;
  align-content: end;
  gap: 0.8rem;
  padding: 1.35rem;
  color: #fffaf0;
  background:
    linear-gradient(180deg, color-mix(in srgb, #d7a84f 18%, transparent), transparent 46%),
    #6b3f25;
  border: 1px solid color-mix(in srgb, #d7a84f 48%, #6b3f25);
  border-radius: 8px;
  box-shadow: 0 14px 38px var(--shadow);
  text-decoration: none;
  transition: transform 160ms ease, border-color 160ms ease, box-shadow 160ms ease;
}

.book-card:hover {
  transform: translateY(-2px);
  border-color: #d7a84f;
  box-shadow: 0 18px 44px color-mix(in srgb, #3f1515 18%, transparent);
}

.book-card span {
  color: #d7a84f;
  font: 700 0.8rem system-ui, sans-serif;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.book-card strong {
  font-size: 2rem;
  line-height: 0.98;
}

.book-card p {
  margin: 0;
  color: color-mix(in srgb, #fffaf0 78%, #d7a84f);
  font: 1rem/1.45 system-ui, sans-serif;
}

html[data-theme="dark"] .library-header h1,
html[data-theme="dark"] .library-section-heading strong {
  color: #e4b879;
}

html[data-theme="dark"] .library-brand img {
  background: #fffaf0;
}

@media (max-width: 820px) {
  .topbar {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr);
    gap: 0.55rem 0.75rem;
    align-items: center;
    padding: 0.6rem 0.75rem;
  }
  .icon-button { display: inline-flex; align-items: center; justify-content: center; }
  .brand strong { max-width: none; }
  .reader-tools {
    grid-column: 1 / -1;
    width: 100%;
    max-width: none;
    justify-content: flex-start;
    flex-wrap: nowrap;
    gap: 0.4rem;
    margin-left: 0;
    overflow-x: auto;
    padding-bottom: 0.1rem;
    scrollbar-width: none;
  }
  .reader-tools::-webkit-scrollbar { display: none; }
  .control-group {
    flex: 0 0 auto;
    max-width: none;
  }
  button {
    padding: 0.4rem 0.55rem;
    font-size: 0.78rem;
  }
  select {
    max-width: 8rem;
    padding-block: 0.4rem;
    font-size: 0.78rem;
  }
  .font-size {
    min-width: 2.8rem;
    font-size: 0.76rem;
  }
  .shell { display: block; padding: 1rem; }
  .toc {
    position: fixed;
    inset: 120px 0 auto 0;
    z-index: 9;
    display: none;
    max-height: calc(100vh - 120px);
    padding: 1rem;
    background: var(--paper);
    border-right: 0;
    border-bottom: 1px solid var(--rule);
    box-shadow: 0 18px 32px var(--shadow);
  }
  .toc.open { display: block; }
  .reader { width: 100%; }
  .library-link { display: none; }
  .library-header {
    display: grid;
    gap: 1rem;
  }
  .library-brand {
    align-items: flex-start;
  }
  .book-section p {
    font-size: calc(1.05rem * var(--font-scale));
    line-height: 1.76;
  }
}

@media (max-width: 520px) {
  .topbar { padding-inline: 0.6rem; }
  .kicker { font-size: 0.68rem; }
  .brand strong { font-size: 0.95rem; }
  .library-brand {
    display: grid;
  }
  .control-group button[data-theme] { padding-inline: 0.48rem; }
  .cover h1 { font-size: clamp(2.7rem, 16vw, 4.5rem); }
}
