/* ==========================================================================
   The Canon Shelf — quiet-luxury library at dusk
   Warm walnut · brass · ink-on-paper. All values trace to root tokens;
   uncommented literals are structural constants (radii, breakpoints).
   Fonts are declared in fonts.css — never redeclare @font-face here.
   ========================================================================== */

/* --- 1. Tokens ------------------------------------------------------------ */
:root {
  /* materials */
  --wood-hi: #8a6140;
  --wood-lo: #5d3f28;
  --brass: #c9a86a;
  --brass-dim: #8a744d;
  --room: #141009;
  --veil: rgba(10, 8, 5, 0.92);

  /* reading surfaces & inks (both themes declared up front) */
  --paper-light: #f6f1e6;
  --ink-light: #2a2118;
  --ink-soft-light: #6b5d4b;
  --paper-dark: #1b1613;
  --ink-dark: #ddd2bf;
  --ink-soft-dark: #9a8b74;

  /* active reading palette — flipped by [data-theme="dark"] */
  --paper: var(--paper-light);
  --ink: var(--ink-light);
  --ink-soft: var(--ink-soft-light);

  --accent: var(--brass);

  /* type */
  --font-display: 'Fraunces', Georgia, serif;
  --font-text: 'Source Serif 4', Georgia, serif;
  --font-ui: 'Inter', system-ui, sans-serif;

  /* motion */
  --ease: cubic-bezier(0.22, 1, 0.36, 1);

  /* glass panel recipe shared by whisper / toc */
  --glass-bg: rgba(20, 15, 10, 0.82);
  --glass-border: rgba(201, 168, 106, 0.28);   /* brass @ 28% */

  /* user zoom factor for fixed pagination (JS writes inline) */
  --type-scale: 1;
}

/* dark reading theme: swap only the page/ink pair */
[data-theme='dark'] {
  --paper: var(--paper-dark);
  --ink: var(--ink-dark);
  --ink-soft: var(--ink-soft-dark);
}

/* --- 2. Reset & base ------------------------------------------------------ */
*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html,
body {
  height: 100%;
  overflow: hidden;
  background: var(--room);
}

body {
  font-family: var(--font-ui);
  color: var(--ink-dark);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

button {
  font: inherit;
  color: inherit;
  background: none;
  border: none;
  cursor: pointer;
}

[hidden] { display: none !important; }

:focus-visible { outline: 2px solid var(--brass); outline-offset: 3px; }

::selection { background: var(--brass); color: #141009; }   /* brass on ink */

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

/* --- 3. Loading veil ------------------------------------------------------- */
#veil {
  position: fixed;
  inset: 0;
  z-index: 90;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 18px;
  background: var(--veil);
  transition: opacity 900ms var(--ease);
}

#veil .wordmark {
  font-family: var(--font-display);
  font-style: italic;
  font-weight: 500;
  font-size: clamp(28px, 5vw, 44px);
  letter-spacing: 0.02em;
  color: var(--paper-light);
}

#veil .rule {
  width: 64px;
  height: 1px;
  background: var(--brass);
  opacity: 0.7;
}

#veil .status {
  font-family: var(--font-ui);
  font-size: 11px;
  font-weight: 500;
  letter-spacing: 0.22em;
  text-transform: uppercase;
  color: var(--ink-soft-dark);
}

#veil .pulse {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--brass);
  animation: veil-breathe 2.4s ease-in-out infinite;
}

@keyframes veil-breathe {
  0%, 100% { opacity: 0.25; transform: scale(0.85); }
  50%      { opacity: 1;    transform: scale(1); }
}

/* JS may put .is-ready on the veil itself or on <body> — accept either */
#veil.is-ready,
body.is-ready #veil {
  opacity: 0;
  pointer-events: none;
}

/* --- 4. Stage & canvas ----------------------------------------------------- */
#stage {
  position: fixed;
  inset: 0;
}

#stage canvas {
  display: block;
  width: 100%;
  height: 100%;
}

/* dusk falloff toward the room edges */
#stage .vignette {
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: radial-gradient(120% 90% at 50% 40%, transparent 55%, rgba(0, 0, 0, 0.55));
}

/* faint film grain over the whole stage (two offset dot lattices) */
#stage .grain {
  position: absolute;
  inset: 0;
  pointer-events: none;
  opacity: 0.05;
  background-image:
    repeating-radial-gradient(circle at 17% 29%, rgba(255, 255, 255, 0.35) 0 1px, transparent 1px 3px),
    repeating-radial-gradient(circle at 73% 61%, rgba(0, 0, 0, 0.45) 0 1px, transparent 1px 4px);
  background-size: 137px 137px, 173px 173px;  /* coprime sizes avoid visible tiling */
}

/* shelf-edge sheen sweep while a book flies home (class toggled by JS) */
@keyframes return-shimmer {
  from { transform: translateX(-100%); }
  to   { transform: translateX(100%); }
}

.book-returning::after {
  content: '';
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: linear-gradient(100deg, transparent 30%, rgba(201, 168, 106, 0.12) 50%, transparent 70%);
  animation: return-shimmer 900ms var(--ease) 1;
}

/* --- 5. Hover card ("book whisper") ----------------------------------------
   Anchored near cursor on desktop; JS may pin bottom-center on touch (.touch-anchor). */
#whisper {
  position: fixed;
  z-index: 60;
  max-width: 320px;
  padding: 18px 22px;
  border: 1px solid var(--glass-border);
  border-radius: 14px;
  background: var(--glass-bg);
  -webkit-backdrop-filter: blur(14px);
  backdrop-filter: blur(14px);
  box-shadow:
    0 24px 48px rgba(0, 0, 0, 0.45),
    inset 0 1px 0 rgba(255, 255, 255, 0.04);
  color: var(--paper-light);
  opacity: 0;
  transform: translateY(8px);
  transition: opacity 260ms var(--ease), transform 260ms var(--ease);
  pointer-events: none;
}

#whisper.is-visible {
  opacity: 1;
  transform: translateY(0);
}

#whisper .w-title {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 22px;
  line-height: 1.2;
}

#whisper .w-meta {
  margin-top: 6px;
  font-family: var(--font-ui);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--brass);
}

#whisper .w-desc {
  margin-top: 10px;
  font-family: var(--font-text);
  font-style: italic;
  font-size: 14px;
  line-height: 1.5;
  color: var(--ink-soft-dark);
}

#whisper .w-micro {
  margin-top: 12px;
  font-family: var(--font-ui);
  font-size: 10px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--brass-dim);
}

/* touch variant: pinned above bottom chrome, centered */
#whisper.touch-anchor {
  left: 50% !important;
  top: auto !important;
  bottom: calc(96px + env(safe-area-inset-bottom, 0px));
  transform: translate(-50%, 8px);
}

#whisper.touch-anchor.is-visible { transform: translate(-50%, 0); }

/* --- 6. Reader shell -------------------------------------------------------- */
#reader {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  visibility: hidden;
  opacity: 0;
  transition: opacity 320ms var(--ease), visibility 0s linear 320ms;
}

#reader.open {
  visibility: visible;
  opacity: 1;
  transition: opacity 320ms var(--ease);
}

.spread {
  display: flex;
  filter: drop-shadow(0 34px 60px rgba(0, 0, 0, 0.55));
}

.page {
  position: relative;
  width: min(44vw, 560px);
  aspect-ratio: 5 / 7;
  max-height: 82vh;
  overflow: hidden;
  background: var(--paper);
  box-shadow:
    inset 0 0 42px rgba(93, 63, 40, 0.07),           /* aged-paper warmth */
    inset -14px 0 26px -18px rgba(58, 38, 20, 0.55); /* spine shading     */
  transition: background 300ms var(--ease);
}

/* outer corners rounded, binding-side corners square */
.page:first-child { border-radius: 4px 0 0 4px; }
.page:last-child  { border-radius: 0 4px 4px 0; }
.spread > .page:only-child { border-radius: 4px; }

/* real-binding crease between the two leaves */
.spread > .page + .page::before,
.spread > .page:first-child:not(:only-child)::after {
  content: '';
  position: absolute;
  width: 34px;                    /* crease depth */
  pointer-events: none;
}

.spread > .page + .page::before {
  inset: 0 auto 0 0;
  background: linear-gradient(to right, rgba(58, 38, 20, 0.22), rgba(58, 38, 20, 0.06) 55%, transparent);
}

.spread > .page:first-child:not(:only-child)::after {
  inset: 0 0 0 auto;
  background: linear-gradient(to left, rgba(58, 38, 20, 0.22), rgba(58, 38, 20, 0.06) 55%, transparent);
}

.page-content {
  height: 100%;
  padding: 7% 8%;
  font-family: var(--font-text);
  font-size: calc(var(--type-scale, 1) * clamp(13px, 1.35vw, 17px));
  line-height: 1.58;
  text-align: justify;
  hyphens: auto;
  -webkit-hyphens: auto;
  color: var(--ink);
  overflow-wrap: break-word;
}

.page-content em { font-style: italic; }

/* --- block vocabulary emitted by the pipeline ------------------------------ */

.blk-ch {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 0.82em;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  color: var(--brass-dim);
  margin-bottom: 1.2em;
}

/* short brass rule preceding chapter heads */
.blk-ch::before {
  content: '';
  display: block;
  width: 28px;
  height: 1px;
  margin-bottom: 0.9em;
  background: var(--brass);
}

.blk-h1 {
  font-family: var(--font-display);
  font-weight: 500;
  font-size: 1.7em;
  line-height: 1.25;
  text-align: center;
  margin: 0.6em 0 1em;
}

.blk-p { text-indent: 1.4em; }

.blk-p.no-indent,
.blk-ch + .blk-p,
.blk-h1 + .blk-p { text-indent: 0; }

.blk-v {
  text-align: left;
  text-indent: 0;
  padding-left: 2em;
  white-space: pre-line;          /* verse keeps its own breaks */
}

.blk-sp { height: 1em; }

/* --- 7. Reader chrome (top & bottom bars) -----------------------------------
   Auto-hide: JS adds .chrome-hidden after idle. */

.chrome {
  position: absolute;
  left: 0;
  right: 0;
  z-index: 45;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 18px;
  padding-top: calc(12px + env(safe-area-inset-top, 0px));
  transition: opacity 400ms var(--ease);
  background: linear-gradient(to bottom, rgba(10, 8, 5, 0.55), transparent);
}

.chrome.chrome-hidden {
  opacity: 0;
  pointer-events: none;
}

.chrome-top { top: 0; }

.chrome-bottom {
  bottom: 0;
  padding-bottom: calc(12px + env(safe-area-inset-bottom, 0px));
  background: linear-gradient(to top, rgba(10, 8, 5, 0.55), transparent);
}

.chrome .spacer { flex: 1; }

.reader-title {
  flex: 1;
  min-width: 0;
  text-align: center;
  font-family: var(--font-display);
  font-weight: 500;
  font-size: 16px;
  letter-spacing: 0.01em;
  color: var(--paper-light);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ghost buttons: 40px targets, hairline brass ring, warm hover fill */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 40px;
  min-height: 40px;
  padding: 0 10px;
  border: 1px solid rgba(201, 168, 106, 0.35);   /* brass @ 35% */
  border-radius: 999px;
  background: transparent;
  color: var(--paper-light);
  font-family: var(--font-ui);
  font-size: 13px;
  letter-spacing: 0.04em;
  transition: background-color 200ms var(--ease), border-color 200ms var(--ease), color 200ms var(--ease);
}

.btn:hover {
  background: rgba(201, 168, 106, 0.12);         /* brass @ 12% */
  border-color: var(--brass);
}

.btn svg {
  width: 16px;
  height: 16px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.75;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* circular back button with chevron glyph (inline mask, no icon dependency) */
.btn-back::before {
  content: '';
  width: 16px;
  height: 16px;
  background: currentColor;
  mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M10 3 5 8l5 5' fill='none' stroke='%23000' stroke-width='1.75' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") center / contain no-repeat;
}

/* progress readout: label + thin brass-filled track */
.progress {
  display: flex;
  align-items: center;
  gap: 12px;
}

.progress-label {
  font-family: var(--font-ui);
  font-size: 11px;
  font-weight: 500;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--ink-soft-dark);
  white-space: nowrap;
}

.progress-track {
  width: 160px;
  height: 2px;
  border-radius: 1px;
  background: rgba(201, 168, 106, 0.22);
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  width: 0%;
  background: var(--brass);
  transition: width 240ms var(--ease);
}

/* text-size stepper (A− / A+) */
.type-step {
  display: flex;
  align-items: center;
  border: 1px solid rgba(201, 168, 106, 0.35);
  border-radius: 999px;
  overflow: hidden;
}

.type-step .btn {
  border: none;
  border-radius: 0;
  min-width: 40px;
}

.type-step .btn + .btn { border-left: 1px solid rgba(201, 168, 106, 0.25); }

.type-step .btn[data-step='-'] { font-size: 12px; }
.type-step .btn[data-step='+'] { font-size: 15px; }

/* --- 8. TOC drawer ----------------------------------------------------------- */
#toc {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  z-index: 80;
  width: min(360px, 88vw);
  display: flex;
  flex-direction: column;
  padding: 24px 0 calc(24px + env(safe-area-inset-bottom, 0px));
  background: var(--glass-bg);
  border-left: 1px solid var(--glass-border);
  -webkit-backdrop-filter: blur(14px);
  backdrop-filter: blur(14px);
  transform: translateX(100%);
  transition: transform 380ms var(--ease);
}

#toc.open { transform: translateX(0); }

.toc-head {
  padding: 0 24px 16px;
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 13px;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  color: var(--brass);
  border-bottom: 1px solid var(--glass-border);
}

.toc-list {
  list-style: none;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 8px 0;
}

.toc-item {
  display: flex;
  align-items: baseline;
  gap: 12px;
  width: 100%;
  padding: 11px 24px;
  border-left: 2px solid transparent;
  text-align: left;
  transition: background-color 160ms var(--ease);
}

.toc-item:hover { background: rgba(201, 168, 106, 0.1); }

.toc-item.current {
  border-left-color: var(--brass);
  background: rgba(201, 168, 106, 0.08);
}

.toc-num {
  min-width: 2em;
  font-family: var(--font-ui);
  font-size: 10px;
  letter-spacing: 0.1em;
  color: var(--brass-dim);
}

.toc-label {
  flex: 1;
  font-family: var(--font-text);
  font-size: 15px;
  color: var(--paper-light);
}

.toc-page {
  font-family: var(--font-ui);
  font-size: 11px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--ink-soft-dark);
}

/* click-to-close scrim behind the drawer */
.scrim {
  position: fixed;
  inset: 0;
  z-index: 70;
  background: rgba(10, 8, 5, 0.5);
  opacity: 0;
  pointer-events: none;
  transition: opacity 380ms var(--ease);
}

.scrim.open {
  opacity: 1;
  pointer-events: auto;
}

/* --- 9. Flight layer (shelf → reader cross-fade stand-in) --------------------
   JS drives transforms/keyframes; CSS supplies look only. */
#flight {
  position: fixed;
  inset: 0;
  z-index: 85;
  pointer-events: none;
}

.flight-book {
  position: absolute;
  top: 0;
  left: 0;
  width: 120px;
  height: 168px;
  border-radius: 3px 8px 8px 3px;   /* flat fore-edge, rounded outer spine */
  background-size: cover;
  background-position: center;
  box-shadow: 0 30px 60px rgba(0, 0, 0, 0.5);
  transform-style: preserve-3d;
  will-change: transform;
}

/* --- 10. Reduced motion & small screens -------------------------------------- */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }

  #veil .pulse { animation: none; }
}

@media (max-width: 819px) {
  .spread { flex-direction: column; }

  .page {
    width: 92vw;
    aspect-ratio: auto;      /* height-driven single leaf */
    height: 76vh;
    max-height: none;
    border-radius: 4px !important;
  }

  .spread > .page + .page::before,
  .spread > .page:first-child:not(:only-child)::after { display: none; }

  .page-content {
    font-size: calc(var(--type-scale, 1) * clamp(13px, 3.6vw, 16px));
    padding: 8% 7%;
  }

  .chrome {
    padding-left: 12px;
    padding-right: 12px;
  }

  .reader-title { font-size: 14px; }

  .progress-track { width: 96px; }

  .progress-label { letter-spacing: 0.06em; }
}
