@font-face {
  font-family: "Budding Canela Text";
  src: url("assets/fonts/CanelaText-Regular-Web.woff2") format("woff2");
  font-weight: 400; font-style: normal; font-display: swap;
}
:root {
  color-scheme: light;
  --paper: #fcfaf3;
  --ink: #145f4d;
  --page: #f4edd8;
  --canela: "Budding Canela Text", Georgia, serif;
  --size: 300px; --depth: 22px; --r: 15px;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; overflow: hidden; background: var(--paper); }
body { display: flex; flex-direction: column; height: 100dvh; color: var(--ink); font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; -webkit-font-smoothing: antialiased; }
button { font: inherit; color: inherit; -webkit-tap-highlight-color: transparent; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }

/* Header */
.masthead { flex: none; display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: max(12px, env(safe-area-inset-top)) max(18px, env(safe-area-inset-right)) 6px max(18px, env(safe-area-inset-left)); }
.wordmark { display: block; height: 30px; width: auto; }
.view-switch { display: inline-flex; gap: 2px; padding: 3px; border: 1px solid #e0e4d8; border-radius: 40px; background: #f1f2e9; }
.view-switch button { min-height: 36px; padding: 0 12px; border: 0; border-radius: 30px; background: transparent; font-size: 12px; color: #7d8976; cursor: pointer; }
.view-switch button[aria-pressed="true"] { background: var(--paper); color: var(--ink); }
.view-switch button:focus-visible, .stage:focus-visible { outline: 2px solid #a86f2e; outline-offset: 2px; }

/* Scroll stage: native scrolling drives everything. */
.stage { position: relative; flex: 1 1 auto; min-height: 0; overflow: hidden; scrollbar-width: none; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; outline: none; }
.stage::-webkit-scrollbar { display: none; }
.stage.horizontal { overflow-x: auto; scroll-snap-type: x mandatory; touch-action: pan-x pinch-zoom; }
.stage.vertical { overflow-y: auto; scroll-snap-type: y mandatory; touch-action: pan-y pinch-zoom; }
.stage.free { scroll-snap-type: none; }
.stage.dragging { cursor: grabbing; user-select: none; }
.track { position: relative; }
.snap { position: absolute; left: 0; top: 0; width: 1px; height: 1px; scroll-snap-align: start; pointer-events: none; }
.scene { position: sticky; left: 0; top: 0; overflow: hidden; pointer-events: none; }
.rail { position: absolute; inset: 0; }

/* Books: square covers, square binding corners, rounded opening corners. */
.book { position: absolute; left: var(--left); top: var(--top); width: var(--size); height: var(--size); margin: 0; padding: 0; border: 0; background: none; transform-style: preserve-3d; transform-origin: 50% 50% 0; pointer-events: none; cursor: pointer; user-select: none; -webkit-user-select: none; }
.book.off { display: none; }
.book.flat { transform-style: flat; }
.book:focus-visible { outline: none; }
.book:focus-visible .front, .book:focus-visible .spine { outline: 2px solid #c79541; outline-offset: 3px; }
.front, .spine, .paper { position: absolute; backface-visibility: hidden; -webkit-backface-visibility: hidden; pointer-events: auto; }
.front { inset: 0; border-radius: 0 var(--r) var(--r) 0; overflow: hidden; background: var(--page); transform: translateZ(.15px); }
.front img { display: block; width: 100%; height: 100%; object-fit: cover; pointer-events: none; }
.front::after { content: ""; position: absolute; inset: 0; border-radius: inherit; border: 1px solid #00000012; pointer-events: none; }
.sheen { position: absolute; inset: 0; opacity: 0; pointer-events: none; background: linear-gradient(115deg, #ffffff0d 0%, #ffffff33 22%, #ffffffbf 44%, #ffffff59 58%, #ffffff14 100%); }
.wash { position: absolute; inset: 0; z-index: 1; opacity: 0; background: var(--paper); pointer-events: none; }

.spine { top: 0; left: calc(-1 * var(--depth)); width: var(--depth); height: 100%; transform-origin: 100% 50%; transform: rotateY(-90deg); background: var(--spine); color: var(--spine-ink); display: flex; align-items: center; justify-content: center; padding: 14px 2px; overflow: hidden; }
.spine::before { content: ""; position: absolute; inset: 6px 5px; border-block: 1px solid currentColor; opacity: .65; pointer-events: none; }
.spine::after { content: ""; position: absolute; inset: 0; background: linear-gradient(90deg, #0000001a, transparent 25%, #ffffff12 65%, #0000001f); pointer-events: none; }
.spine .title { writing-mode: vertical-rl; font-family: var(--canela); font-weight: 400; font-size: var(--spine-font); line-height: 1; letter-spacing: -.005em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-height: 100%; }

/* Paper faces only exist while a book turns. */
.paper { display: none; background: var(--page); }
.book.turning .paper { display: block; }
.paper.fore { top: 0; left: 100%; width: var(--depth); height: 100%; transform-origin: 0 50%; transform: rotateY(90deg); background: repeating-linear-gradient(90deg, #c8bea4 0 1px, var(--page) 1px 5px); }
.paper.ptop { left: 0; top: calc(-1 * var(--depth)); width: 100%; height: var(--depth); transform-origin: 50% 100%; transform: rotateX(90deg); }
.paper.pbottom { left: 0; top: 100%; width: 100%; height: var(--depth); transform-origin: 50% 0; transform: rotateX(-90deg); }
.paper.ptop, .paper.pbottom { background: repeating-linear-gradient(0deg, #c8bea4 0 1px, var(--page) 1px 5px); border-radius: 0 2px 2px 0; }

/* Resting states are flat (no 3D): a lone spine or a lone cover. */
.book.spine-only .front, .book.spine-only .paper { display: none; }
.book.spine-only .spine { left: calc(50% - var(--depth) / 2); transform: none; }
.book.front-only .spine { display: none; }
.stage.vertical .book.spine-only .spine { left: 0; top: calc(50% - var(--depth) / 2); width: 100%; height: var(--depth); padding: 0 14px; }
.stage.vertical .book.spine-only .spine::before { inset: 5px 6px; border-block: 0; border-inline: 1px solid currentColor; }
.stage.vertical .book.spine-only .spine::after { background: linear-gradient(0deg, #0000001a, transparent 25%, #ffffff12 65%, #0000001f); }
.stage.vertical .book.spine-only .title { writing-mode: horizontal-tb; line-height: 1.2; max-width: 100%; }

/* Vertical paper top on the revealed and receding covers. */
.top { position: absolute; left: 0; top: 0; width: 100%; height: 100%; overflow: visible; pointer-events: none; display: none; }
.book.has-top .top { display: block; }
.top .paper-top { fill: var(--page); }
.top .paper-wash { fill: var(--paper); opacity: var(--wash, 0); }

/* New Book cover and spine (Horizontal uses the shelf book itself). */
.new-cover { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px; color: #5b4473; background: linear-gradient(90deg, #e8dbf2 7px, #f4ecfa 7px); font-family: var(--canela); font-size: calc(var(--size) * .095); border: 1px solid #d8c8e8; border-radius: inherit; }
.new-cover svg { width: 22%; height: auto; }
.plus { fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }
.book.new-book .spine { --spine: #e3d3f0; --spine-ink: #4f3a66; }
.book.new-book .spine .title { display: inline-flex; align-items: center; gap: 6px; }
.book.new-book .spine .title svg { width: calc(var(--spine-font) * 1.05); height: auto; transform: rotate(90deg); }

/* New Book in Vertical: a rigid volume in its own camera. */
.creation { position: absolute; z-index: 500; top: 0; left: 50%; padding: 0; border: 0; background: none; color: #5b4473; pointer-events: auto; cursor: pointer; touch-action: pan-y; }
.creation.off { display: none; }
.creation:focus-visible { outline: 2px solid #c79541; outline-offset: 2px; border-radius: 8px; }
.creation .camera { position: absolute; inset: 0; perspective: 900px; perspective-origin: 50% var(--eye); pointer-events: none; }
.creation .vol { position: absolute; left: 0; top: var(--hinge); width: var(--size); height: var(--size); transform-origin: 50% 0 0; transform-style: preserve-3d; transform: rotateX(var(--angle)); }
.creation .face { position: absolute; left: 0; top: 0; width: 100%; height: 100%; backface-visibility: hidden; -webkit-backface-visibility: hidden; background: #f4ecfa; }
.creation .cover { position: absolute; inset: 0; border-radius: 0 var(--r) var(--r) 0; }
.creation .edge { height: 44px; display: flex; align-items: center; justify-content: center; gap: 8px; font-family: var(--canela); font-size: 17px; color: #5b4473; background: repeating-linear-gradient(180deg, #fbf6ee 0 2px, #ece3d4 2px 3px); border-block: 4px solid #cdb6e3; border-left: 6px solid #cdb6e3; border-right: 0; border-radius: 0 var(--r) var(--r) 0; box-shadow: inset -1px 0 0 #e2d6c3; transform-origin: 50% 0; transform: translateZ(-44px) rotateX(90deg); }
.creation .edge svg { width: 18px; height: 18px; }
.creation .edge > *, .creation .cover .new-cover > * { transition: none; }
.creation .edge > * { opacity: var(--edge-ink, 0); }
.creation .cover .new-cover > * { opacity: var(--cover-ink, 1); }
.creation .back { transform: translateZ(-44px) rotateY(180deg); border-radius: var(--r) 0 0 var(--r); }
.creation .binding { width: 44px; background: #e8dbf2; transform-origin: 0 50%; transform: translateZ(-44px) rotateY(-90deg); }
.creation .fore { width: 44px; left: 100%; transform-origin: 0 50%; transform: rotateY(90deg); background: repeating-linear-gradient(90deg, #d8c8e8 0 1px, #f4ecfa 1px 5px); }

/* Patch: a flat overlay at the settled cover's top-right corner. */
.patch { position: absolute; z-index: 900; left: 0; top: 0; width: var(--patch, 80px); height: var(--patch, 80px); pointer-events: none; display: none; }
.patch.on { display: block; }
.patch img { display: block; width: 100%; height: 100%; object-fit: contain; transform: rotate(9deg); filter: drop-shadow(0 2px 2.5px rgb(61 39 8 / .22)); opacity: 0; }
.patch.landing img { animation: patch-in 950ms 300ms cubic-bezier(.3, .8, .3, 1) both; }
.patch.leaving img { transition: opacity 110ms ease-out; opacity: 0 !important; }
@keyframes patch-in { 0% { opacity: 0; transform: rotate(0deg) scale(1.7); } 40% { opacity: 1; } 100% { opacity: 1; transform: rotate(9deg) scale(1); } }
@keyframes patch-out { from { opacity: 1; transform: rotate(9deg); } to { opacity: 0; transform: rotate(9deg); } }

.toast { position: fixed; z-index: 1000; left: 50%; bottom: max(22px, env(safe-area-inset-bottom)); transform: translate(-50%, 8px); padding: 10px 18px; border-radius: 22px; background: #2f2540e6; color: #fbf6ff; font-size: 14px; opacity: 0; pointer-events: none; transition: opacity 180ms ease-out, transform 180ms ease-out; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (prefers-reduced-motion: reduce) {
  .patch.landing img { animation: patch-fade 160ms ease-out both; transform: rotate(9deg); }
  .toast { transition: none; }
}
@keyframes patch-fade { from { opacity: 0; } to { opacity: 1; } }

/* A scrolled-past book keeps its patch, washed with the cover. */
.front { overflow: visible; }
.front img { border-radius: inherit; }
.bpatch { position: absolute; z-index: 2; bottom: 8px; right: 8px; width: clamp(64px, 24%, 88px); aspect-ratio: 1; display: none; transform-origin: 50% 50%; transform: rotate(9deg); filter: drop-shadow(0 2px 2.5px rgb(61 39 8 / .22)); pointer-events: none; }
.bpatch img { width: 100%; height: 100%; display: block; border-radius: 0; }
.bpatch i { position: absolute; inset: 0; background: var(--paper); opacity: var(--wash, 0); -webkit-mask-size: 100% 100%; mask-size: 100% 100%; }
.front > .wash { z-index: 1; border-radius: inherit; }

/* Vertical New Book, collapsed: show its spine like the stacked books (not the top edge). */
.creation .edge { background: #eadcf3 !important; color: #5b4473; border: 0 !important; border-radius: 0 !important; box-shadow: none !important; position: relative; }
.creation .edge::before { content: ""; position: absolute; inset: 5px 6px; border-inline: 1px solid currentColor; opacity: .65; pointer-events: none; }
.creation .edge::after { content: ""; position: absolute; inset: 0; background: linear-gradient(180deg, #0000001a, transparent 25%, #ffffff12 65%, #0000001f); pointer-events: none; }
.stage.vertical .book.new-book .spine .title svg { transform: none; }

/* Each book is its own compositor layer so turning and landing don't repaint. */
.book:not(.off) { will-change: transform; }
