/**
 * Immersive single-volume book layout
 * Cross-ref: book_design_guidelines D1,D2,D4,D7 + BOOK_DESIGN UX Part 9 (deep read = minimal chrome)
 */

.book-export-missing {
  margin: 1.5rem auto 2rem;
  max-width: 42rem;
  padding: 1rem 1.25rem;
  border: 1px solid var(--color-border, #c9b89a);
  border-radius: 4px;
  font-size: 0.95rem;
  line-height: 1.5;
  color: var(--color-text-muted, #5c5348);
  background: var(--color-surface-raised, #faf6ef);
}

.book-body {
  background: var(--bg-page);
}

.book-ribbon {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 30;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 0.25rem 0.45rem;
  padding: 0.38rem 0.75rem;
  font-family: var(--font-ui);
  font-size: 0.625rem;
  letter-spacing: 0.04em;
  line-height: 1.3;
  background: color-mix(in srgb, var(--bg-page) 80%, transparent);
  color: var(--text-chrome);
  border-bottom: none;
  box-shadow: none;
}

.book-ribbon__copyright {
  font-weight: 500;
  color: inherit;
}

.book-ribbon__sep {
  color: inherit;
  opacity: 0.45;
}

.book-ribbon__meta {
  text-transform: uppercase;
  letter-spacing: 0.07em;
  font-size: 0.5625rem;
  color: inherit;
}

/* Reading progress — non-intrusive (NNG: support scan, don't compete with text) */
.read-progress {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  z-index: 25;
  background: transparent;
  pointer-events: none;
}

.book-ribbon ~ .read-progress {
  top: 1.5rem;
}

.book-ribbon ~ .book-main .title-spread {
  padding-top: 3.25rem;
}

.read-progress-bar {
  display: block;
  height: 100%;
  width: 0%;
  background: linear-gradient(
    90deg,
    var(--read-progress),
    color-mix(in srgb, var(--read-progress-end) 88%, var(--read-progress))
  );
  transition: width 0.15s ease-out;
}

/* Minimal floating controls — not a site header */
.book-controls {
  position: fixed;
  bottom: 1.25rem;
  right: 1.25rem;
  z-index: 20;
  display: flex;
  gap: 0.35rem;
  padding: 0.35rem 2.9rem 0.35rem 0.35rem;
  border-radius: 999px;
  background: color-mix(in srgb, var(--bg-surface) 92%, transparent);
  border: 1px solid var(--border);
  box-shadow: 0 4px 24px color-mix(in srgb, var(--text-primary) 8%, transparent);
  backdrop-filter: blur(8px);
  transform-origin: right center;
  transition: transform 0.35s cubic-bezier(0.4, 0, 0.2, 1), opacity 0.25s ease;
}

.book-controls.is-hidden {
  transform: scaleX(0);
  opacity: 0;
  pointer-events: none;
}

/* Persistent toggle — overlaps right edge of controls, always visible */
.book-controls-toggle {
  position: fixed;
  bottom: 1.25rem;
  right: 1.25rem;
  z-index: 21;
  width: 2.7rem;
  height: 2.7rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: color-mix(in srgb, var(--bg-surface) 92%, transparent);
  backdrop-filter: blur(8px);
  box-shadow: 0 4px 24px color-mix(in srgb, var(--text-primary) 8%, transparent);
  color: var(--text-muted);
  cursor: pointer;
  padding: 0;
  transition: background 0.18s ease;
}

/* Drawn − / + so both glyphs share the same visual weight (not em-dash vs +) */
.book-controls-toggle::before,
.book-controls-toggle::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  background: currentColor;
  border-radius: 1px;
  transform: translate(-50%, -50%);
}

.book-controls-toggle::before {
  width: 0.7rem;
  height: 2px;
}

.book-controls-toggle.is-collapsed::after {
  width: 2px;
  height: 0.7rem;
}

.book-controls-toggle:hover {
  background: color-mix(in srgb, var(--bg-surface) 100%, transparent);
}

.book-controls-toggle.is-spinning {
  animation: controls-spin 0.35s ease-out forwards;
}

@keyframes controls-spin {
  from { transform: rotate(0deg); }
  to   { transform: rotate(180deg); }
}

.book-control-btn {
  font-family: var(--font-ui);
  font-size: 0.75rem;
  min-width: 2rem;
  height: 2rem;
  padding: 0 0.4rem;
  border: 1px solid transparent;
  border-radius: 999px;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
}

.book-control-btn:hover,
.book-control-btn[aria-pressed="true"] {
  border-color: color-mix(in srgb, var(--ink-manuscript-sepia) 55%, transparent);
  color: var(--ink-manuscript-sepia);
}

/* Slide-out TOC — outside 65ch measure (C12 / D7) */
.book-toc {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: min(18rem, 85vw);
  z-index: 40;
  background: var(--bg-surface);
  border-right: 1px solid var(--border);
  box-shadow: 4px 0 24px color-mix(in srgb, var(--text-primary) 10%, transparent);
  overflow-y: auto;
  transform: translateX(-100%);
  transition: transform 0.25s ease;
}

.book-toc.is-open {
  transform: translateX(0);
}

.book-toc[hidden] {
  display: block;
  visibility: hidden;
}

.book-toc.is-open[hidden] {
  visibility: visible;
}

.book-toc-inner {
  padding: 2rem 1.25rem;
  font-family: var(--font-ui);
}

.book-toc-title {
  font-size: 0.6875rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  margin: 0 0 1rem;
}

.book-toc-list {
  list-style: none;
  padding: 0;
  margin: 0;
  font-size: 0.875rem;
  line-height: 1.4;
}

.book-toc-list li {
  margin-bottom: 0.5rem;
}

.book-toc-list a {
  color: var(--text-muted);
  text-decoration: none;
}

.book-toc-list a.is-active,
.book-toc-list a:hover {
  color: var(--ink-manuscript-sepia);
}

/* Main book column — Chaparro margins + 65ch measure */
.book-main {
  padding: 0 var(--pad-mobile) 6rem;
}

@media (min-width: 768px) {
  .book-main {
    padding-left: var(--pad-desktop);
    padding-right: var(--pad-desktop);
  }
}

/* Title spread — feels like opening a cover */
.title-spread {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 4rem var(--pad-mobile);
  background:
    radial-gradient(ellipse at 50% 0%, color-mix(in srgb, var(--ink-manuscript-sepia) 8%, transparent), transparent 55%),
    var(--bg-page);
  border-bottom: 1px solid var(--border);
}

.title-spread-inner {
  width: min(100%, 38rem);
  max-width: none;
  margin-inline: auto;
  padding-inline: 0.15rem;
}

.title-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 0.45rem 0.55rem;
  margin: 0 0 2rem;
  font-family: var(--font-ui);
  font-size: 0.8125rem;
  color: var(--text-muted);
  line-height: 1;
}

.title-meta-chip {
  display: inline-flex;
  align-items: center;
}

.title-edition {
  font-family: var(--font-ui);
  font-size: 0.8125rem;
  color: var(--text-muted);
  margin: 0 0 2rem;
}

.living-indicator {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  line-height: 1;
}

.living-indicator::before {
  content: "";
  flex-shrink: 0;
  width: 6px;
  height: 6px;
  margin-top: 0;
  border-radius: 50%;
  background: var(--living-pulse);
  animation: pulse 2s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.45; transform: scale(0.92); }
}

.title-main {
  font-size: clamp(1.5rem, 4.8vw, 2.35rem);
  line-height: 1.18;
  font-weight: 700;
  margin: 0 0 1rem;
  letter-spacing: -0.02em;
  text-wrap: balance;
}

.title-sub {
  font-size: 1.125rem;
  line-height: 1.5;
  color: var(--text-muted);
  margin: 0 auto 2rem;
  max-width: 36ch;
  font-style: italic;
  text-wrap: balance;
}

.title-author {
  font-family: var(--font-ui);
  font-size: 0.9375rem;
  margin: 0 0 3rem;
}

.title-author a {
  color: inherit;
  text-decoration: none;
  border-bottom: 1px solid color-mix(in srgb, var(--text-muted) 35%, transparent);
  transition: color 0.2s ease, border-color 0.2s ease;
}

.title-author a:hover {
  color: var(--ink-manuscript-sepia);
  border-bottom-color: color-mix(in srgb, var(--ink-manuscript-sepia) 55%, transparent);
}

.title-enter {
  font-family: var(--font-ui);
  font-size: 0.875rem;
  color: var(--text-muted);
  margin: 0;
}

/* GPT cover art — inside title spread, below Read the book, above bottom rule */
.title-spread .book-cover-spread {
  margin: 2rem auto 0;
  padding: 0;
  text-align: center;
}

/* Inline TOC — book endpaper feel */
.book-toc-inline {
  max-width: var(--measure);
  margin: 0 auto 2rem;
  padding: 2rem 0 0;
}

.book-cover-magnify {
  position: relative;
  display: inline-block;
  max-width: min(22rem, 85vw);
  margin: 0 auto;
  overflow: visible;
}

.book-cover-hero {
  margin: 0;
}

.book-cover-hero img {
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 1024 / 1536;
  border-radius: 3px;
  border: 1px solid var(--border);
  box-shadow:
    0 1px 0 color-mix(in srgb, #fff 40%, transparent),
    0 14px 42px color-mix(in srgb, var(--text-primary) 10%, transparent);
}

.cover-edition-caption {
  margin: 0.5rem 0 0;
  font-size: 0.8125rem;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.02em;
  color: var(--text-muted);
  font-style: normal;
}

/* Brass-hand magnifier — fine pointer + hover only */
@media (hover: hover) and (pointer: fine) {
  .book-cover-magnify.is-magnify-active {
    cursor: none;
  }

  .cover-magnifier-lens {
    --mag-size: 10.45rem;
    --mag-radius: calc(var(--mag-size) / 2);
    position: absolute;
    width: var(--mag-size);
    height: var(--mag-size);
    pointer-events: none;
    z-index: 20;
    overflow: visible;
    opacity: 0;
    transform: scale(0.88);
    transition:
      opacity 0.16s ease,
      transform 0.16s ease;
  }

  .cover-magnifier-lens.is-visible {
    opacity: 1;
    transform: scale(1);
  }

  .cover-magnifier-head {
    position: absolute;
    inset: 0;
    z-index: 2;
    pointer-events: none;
  }

  .cover-magnifier-frame {
    position: absolute;
    inset: 0;
    border-radius: 50%;
    border: 5px solid #b8860b;
    box-shadow:
      0 0 0 1px color-mix(in srgb, #5c4010 55%, transparent),
      0 10px 28px color-mix(in srgb, #000 32%, transparent);
    pointer-events: none;
  }

  .cover-magnifier-glass {
    position: absolute;
    inset: 5px;
    border-radius: 50%;
    background-repeat: no-repeat;
    background-color: #f8f4ea;
    overflow: hidden;
    box-shadow:
      inset 0 2px 14px color-mix(in srgb, #fff 55%, transparent),
      inset 0 -6px 12px color-mix(in srgb, #000 14%, transparent);
  }

  .cover-magnifier-glass::after {
    content: "";
    position: absolute;
    inset: 0;
    border-radius: 50%;
    background: linear-gradient(
      135deg,
      color-mix(in srgb, #fff 42%, transparent) 0%,
      transparent 38%,
      transparent 62%,
      color-mix(in srgb, #fff 12%, transparent) 100%
    );
    pointer-events: none;
  }

  /* Pivot at lens center; arm starts on the rim (~5 o'clock) and extends outside */
  .cover-magnifier-arm-mount {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 0;
    height: 0;
    z-index: 1;
    transform: rotate(-40deg);
    pointer-events: none;
  }

  .cover-magnifier-arm {
    position: absolute;
    left: -0.34rem;
    top: calc(var(--mag-radius) - 4px);
    display: flex;
    flex-direction: column;
    align-items: center;
  }

  .cover-magnifier-neck {
    display: block;
    width: 0.68rem;
    height: 0.85rem;
    margin-bottom: -3px;
    border-radius: 0.12rem 0.12rem 0.04rem 0.04rem;
    background: linear-gradient(180deg, #c9a227 0%, #8b6914 100%);
    box-shadow:
      inset 0 1px 0 color-mix(in srgb, #fff 30%, transparent),
      1px 2px 4px color-mix(in srgb, #000 28%, transparent);
    border: 1px solid color-mix(in srgb, #5c4010 75%, transparent);
  }

  .cover-magnifier-handle {
    display: block;
    width: 0.82rem;
    height: 4.1rem;
    border-radius: 0.32rem;
    background: linear-gradient(
      95deg,
      #5c4010 0%,
      #c9a227 38%,
      #e8c547 55%,
      #8b6914 100%
    );
    box-shadow:
      2px 4px 8px color-mix(in srgb, #000 38%, transparent),
      inset 0 1px 0 color-mix(in srgb, #fff 28%, transparent);
    border: 1px solid color-mix(in srgb, #4a3010 70%, transparent);
  }
}

.toc-heading {
  font-family: var(--font-ui);
  font-size: 0.6875rem;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--text-muted);
  margin: 0 0 1.5rem;
}

.toc-inline-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.toc-inline-list li {
  border-bottom: 1px solid color-mix(in srgb, var(--border) 70%, transparent);
}

.toc-inline-list li:last-child {
  border-bottom: 1px solid var(--border);
}

.toc-inline-list a {
  display: grid;
  grid-template-columns: 2.5rem 1fr;
  gap: 1rem;
  padding: 0.85rem 0;
  text-decoration: none;
  color: var(--text-primary);
  font-family: var(--font-ui);
  font-size: 0.9375rem;
}

.toc-inline-list a:hover {
  color: var(--ink-manuscript-sepia);
}

.toc-num {
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

/* Continuous text block */
.book-text {
  max-width: var(--measure);
  margin: 0 auto;
}

/* Part divider — one rule above label; no double stack at TOC handoff */
.part-spread {
  margin: 3rem 0 2.5rem;
  padding: 2rem 0 0;
  text-align: center;
  border-top: 1px solid var(--border);
}

.book-text > .part-spread:first-child {
  margin-top: 0;
  padding-top: 2.5rem;
  border-top: none;
}

.part-spread-label {
  font-family: var(--font-ui);
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.book-chapter {
  margin-bottom: var(--gap-section);
  scroll-margin-top: 2rem;
}

.book-chapter h1 {
  font-size: var(--size-h1);
  line-height: 1.2;
  margin: 0 0 1.5rem;
  padding-top: 1rem;
}

.book-chapter h3 {
  font-size: var(--size-h3);
  margin: 2rem 0 0.75rem;
}

.book-chapter p {
  margin: 0 0 var(--gap-paragraph);
}

.book-chapter dfn {
  font-style: normal;
  font-weight: 700;
}

/* Back matter — spectacle zone (D7) */
.backmatter {
  max-width: min(52rem, 100%);
  margin: 5rem auto 0;
  padding-top: 2rem;
}

.backmatter-heading {
  font-size: var(--size-h2);
  margin: 0 0 0.75rem;
}

.backmatter-lead {
  color: var(--text-muted);
  margin: 0 0 2rem;
  max-width: var(--measure);
}

.backmatter-subhead,
.pipeline-subhead {
  font-family: var(--font-ui);
  font-size: 1rem;
  margin: 2.5rem 0 1rem;
}

.pipeline-theater--embedded {
  margin-bottom: 2rem;
}

.colophon {
  margin-top: 4rem;
  padding-top: 2rem;
  border-top: 1px solid var(--border);
  font-family: var(--font-ui);
  font-size: 0.8125rem;
  color: var(--text-muted);
}

.colophon-meta {
  margin-top: 0.5rem;
}

@media (min-width: 900px) {
  .book-text,
  .book-toc-inline {
    margin-left: auto;
    margin-right: auto;
  }
}

/* ─── Floating chapter sidenav — transparent, numbers + dot, titles on hover */
.book-sidenav {
  position: fixed;
  top: 4rem;
  left: 1rem;
  z-index: 15;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.35s ease;
}

.book-ribbon ~ .book-sidenav {
  top: 5.5rem;
}

.book-sidenav.is-visible {
  opacity: 1;
  pointer-events: auto;
}

.book-sidenav-link {
  display: flex;
  align-items: center;
  padding: 0.3rem 0.4rem 0.3rem 0.5rem;
  border-left: 2px solid transparent;
  border-radius: 0 4px 4px 0;
  text-decoration: none;
  color: var(--text-muted);
  font-family: var(--font-ui);
  font-size: 0.75rem;
  white-space: nowrap;
  overflow: hidden;
  transition: color 0.18s ease, background 0.18s ease, border-color 0.18s ease,
    padding 0.18s ease;
}

/* Hover a specific item — manuscript stripe + solid background + full title */
.book-sidenav-link:hover {
  color: var(--ink-manuscript);
  background: var(--bg-page);
  border-left-color: var(--ink-manuscript-sepia);
  padding-left: 0.7rem;
  position: relative;
  z-index: 1;
  box-shadow: 0 1px 8px color-mix(in srgb, var(--ink-manuscript-sepia) 10%, transparent);
}

/* Hovered item expands to full chapter title — beats nav-wide 7rem rule */
.book-sidenav:hover .book-sidenav-link:hover .sidenav-label {
  max-width: 18rem;
  transition: max-width 0.22s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.book-sidenav-link.is-active {
  color: var(--ink-manuscript-sepia);
  border-left-color: color-mix(in srgb, var(--ink-manuscript-sepia) 55%, transparent);
}

.book-sidenav-link:focus {
  outline: none;
}

.book-sidenav-link:focus-visible {
  outline: none;
  color: var(--ink-manuscript);
  border-left-color: var(--ink-manuscript-sepia);
  background: color-mix(in srgb, var(--bg-page) 92%, var(--parchment-warm));
}

.book-toc-list a:focus-visible,
.toc-inline-list a:focus-visible {
  outline: none;
  color: var(--ink-manuscript-sepia);
}

.book-control-btn:focus-visible,
.book-controls-toggle:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 2px;
}

/* Number — always visible */
.sidenav-num {
  font-variant-numeric: tabular-nums;
  font-weight: 500;
  min-width: 1rem;
  text-align: right;
  flex-shrink: 0;
  opacity: 0.65;
}

/* Dot separator after number — always visible, acts as teaser */
.sidenav-num::after {
  content: " ·";
  opacity: 0.45;
  font-weight: 400;
  margin-left: 0.1rem;
}

/* Label: hidden by default, slides in when nav is hovered */
.sidenav-label {
  max-width: 0;
  opacity: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin-left: 0;
  transition: max-width 0.28s ease, opacity 0.22s ease, margin-left 0.28s ease;
}

/* Hover whole nav → reveal all titles at once; 7rem keeps labels in left margin */
.book-sidenav:hover .sidenav-label {
  max-width: 7rem;
  opacity: 1;
  margin-left: 0.4rem;
}

/* Hide below 1100px — below that threshold the left margin is too narrow at A++ font */
@media (max-width: 1099px) {
  .book-sidenav {
    display: none;
  }
}

@media print {
  .book-controls,
  .book-toc,
  .book-sidenav,
  .read-progress,
  .book-ribbon,
  .title-enter,
  .cover-ink-vignette,
  .backmatter {
    display: none !important;
  }

  .title-spread {
    min-height: auto;
    page-break-after: always;
  }

  .book-cover-spread {
    padding-top: 2rem;
    page-break-after: always;
  }

  .book-cover-hero {
    max-width: 16rem;
    margin-inline: auto;
  }

  .cover-magnifier-lens {
    display: none !important;
  }
}
