/* ─────────────────────────────────────────────────────
   BOOK MODAL — shared spread gallery
   Extracted from lab.html so multiple pages share one
   implementation. Lab and library both include this.
   Assumes the page defines: --paper, --ink, --red,
   --mono, --serif, --ease, --muted, --rule.
   ───────────────────────────────────────────────────── */

/* Generic modal infrastructure (also used by lab's toy modal) */
/* z-index 9450 (2026-09-30): above the phone Index button (9000) and the ADHD switch (9400), which
   floated over the reader; below the phone sheet (9500) and the stickers (9600). */
.modal { position: fixed; inset: 0; background: rgba(14,14,12,0.94); display: none; align-items: stretch; justify-content: center; z-index: 9450; padding: clamp(1rem, 3vh, 2rem); opacity: 0; transition: opacity .25s var(--ease); overscroll-behavior: contain; }
.modal[aria-hidden="false"] { display: flex; opacity: 1; }
.modal-frame { background: var(--paper); width: 100%; max-width: 1280px; display: flex; flex-direction: column; border: 1px solid var(--ink); box-shadow: 0 30px 80px rgba(0,0,0,0.5); overflow: hidden; }
.modal-bar { display: flex; align-items: center; justify-content: space-between; padding: 0.7rem 1rem; border-bottom: 1px solid var(--rule); background: var(--paper); gap: 1rem; }
.modal-title { font-family: var(--mono); font-size: var(--mono-a); letter-spacing: var(--mono-a-track); text-transform: uppercase; color: var(--ink); flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.modal-meta { font-family: var(--serif); font-style: italic; font-size: 0.85rem; letter-spacing: 0.01em; color: var(--muted); white-space: nowrap; padding: 0 0.6rem; }
.modal-actions { display: flex; gap: 0.4rem; flex-shrink: 0; }
.modal-action { font-family: var(--serif); font-weight: 400; font-size: 0.9rem; letter-spacing: 0.06em; font-variant-caps: small-caps; text-transform: lowercase; background: var(--ink); border: 1px solid var(--ink); color: var(--paper); padding: 0.5rem 0.8rem; cursor: pointer; text-decoration: none; transition: background .2s var(--ease); }
.modal-action:hover { background: var(--red); border-color: var(--red); color: var(--paper); }
.modal-action.ghost { background: var(--paper); color: var(--ink); }
.modal-action.ghost:hover { background: var(--ink); color: var(--paper); }

/* Book-specific: spread stage */
.spread-stage { flex: 1; min-height: 60vh; touch-action: pinch-zoom; background: #1d1d1b; position: relative; display: flex; align-items: center; justify-content: center; padding: 1rem clamp(2rem, 6vw, 4rem); overflow: hidden; gap: 0; }
/* In 2-up, a single 1px line of the dark stage shows as the spine */
.spread-stage[data-mode="double"] { gap: 1px; }

.spread-stage .spread-img { max-width: 100%; max-height: 100%; width: auto; height: auto; object-fit: contain; display: block; box-shadow: 0 10px 40px rgba(0,0,0,0.5); user-select: none; -webkit-user-drag: none; opacity: 0; transition: opacity .35s var(--ease); }
.spread-stage .spread-img.loaded { opacity: 1; }
.spread-stage .spread-img.errored { opacity: 0.6; outline: 2px solid var(--red); outline-offset: 4px; }
.spread-stage .spread-img.errored::after { content: "spread failed to load"; color: var(--red); }

.spread-stage[data-mode="single"] .spread-img-2 { display: none; }
.spread-stage[data-mode="double"] .spread-img,
.spread-stage[data-mode="double"] .spread-img-2 { max-width: 50%; }
.spread-stage[data-mode="double"] .spread-img-2.alone { display: none; }

#bookModal .modal-frame:fullscreen,
#bookModal .modal-frame:-webkit-full-screen { max-width: 100%; width: 100%; height: 100vh; max-height: 100vh; border: 0; }
#bookModal .modal-frame:fullscreen .spread-stage,
#bookModal .modal-frame:-webkit-full-screen .spread-stage { min-height: 0; flex: 1; }

.spread-nav { position: absolute; top: 50%; transform: translateY(-50%); width: 48px; height: 48px; border: 1px solid rgba(244,241,234,0.4); background: rgba(14,14,12,0.6); color: var(--paper); cursor: pointer; font-size: 1.4rem; display: grid; place-items: center; transition: background .2s var(--ease), border-color .2s var(--ease); }
.spread-nav:hover { background: var(--paper); color: var(--ink); border-color: var(--paper); }
.spread-nav.prev { left: 1rem; }
.spread-nav.next { right: 1rem; }
.spread-nav:disabled { opacity: 0.25; cursor: not-allowed; }

.spread-counter { position: absolute; bottom: 0.8rem; right: 1rem; font-family: var(--mono); font-size: var(--mono-b); letter-spacing: var(--mono-b-track); color: var(--paper); opacity: 0.7; }

.spread-thumbs { background: var(--ink); border-top: 1px solid var(--ink); padding: 0.6rem 0.8rem; overflow-x: auto; overflow-y: hidden; touch-action: pan-x; overscroll-behavior-x: contain; -webkit-overflow-scrolling: touch; display: flex; gap: 0.35rem; scrollbar-width: thin; scrollbar-color: rgba(244,241,234,0.3) transparent; }
.spread-thumbs::-webkit-scrollbar { height: 6px; }
.spread-thumbs::-webkit-scrollbar-thumb { background: rgba(244,241,234,0.3); border-radius: 3px; }
.spread-thumb { flex-shrink: 0; width: 36px; height: 48px; border: 1px solid transparent; cursor: pointer; padding: 0; background: rgba(244,241,234,0.08); position: relative; transition: border-color .2s var(--ease), transform .2s var(--ease); }
.spread-thumb:hover { transform: translateY(-2px); }
.spread-thumb.active { border-color: var(--red); transform: translateY(-3px); }
.spread-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; opacity: 0.65; }
.spread-thumb.active img { opacity: 1; }
.spread-thumbs .label { display: inline-flex; align-items: center; padding: 0 0.6rem; flex-shrink: 0; font-family: var(--mono); font-size: var(--mono-c); letter-spacing: var(--mono-c-track); text-transform: uppercase; color: rgba(244,241,234,0.5); }

/* Cover-only fallback (book has no exported spreads) */
.cover-fallback { flex: 1; min-height: 60vh; background: #1d1d1b; display: grid; place-items: center; padding: 2rem; }
/* [hidden] must win over display:grid, or the hidden fallback still takes flex space and shoves a lone cover off-centre */
.cover-fallback[hidden] { display: none; }
.cover-fallback-inner { display: flex; flex-direction: column; align-items: center; gap: 1.4rem; max-width: 320px; text-align: center; }
.cover-fallback img { max-width: 100%; box-shadow: 0 10px 40px rgba(0,0,0,0.5); user-select: none; -webkit-user-drag: none; }
.cover-fallback p { color: var(--paper); font-family: var(--serif); font-style: italic; font-size: 0.95rem; opacity: 0.8; }
/* Books by others (2026-09-29): author line in mono caps, the "where I am in it" line in the serif. */
.cover-fallback #coverFallbackBy { font-family: var(--mono); font-style: normal; font-size: 0.72rem; letter-spacing: 0.06em; text-transform: uppercase; opacity: 0.7; margin: 0; }
.cover-fallback #coverFallbackWhere { margin: -0.6rem 0 0; }
.cover-fallback p[hidden] { display: none; }

/* 2026-09-29: the reader always shows the real pointer. Pages that draw their own cursor
   (body { cursor: none } + a .cursor div) lost it entirely in full screen, where only the
   reader is painted, so there was nothing to click out with. */
#bookModal, #bookModal * { cursor: default !important; }
#bookModal button:not(:disabled), #bookModal a, #bookModal .spread-thumb { cursor: pointer !important; }
#bookModal .spread-nav:disabled { cursor: not-allowed !important; }
/* In full screen the dark stage around the pages is the way out. */
#bookModal.is-full .spread-stage { cursor: zoom-out !important; }
#bookModal.is-full .spread-img, #bookModal.is-full .spread-img-2 { cursor: default !important; }
#bookModal.is-full .spread-nav { cursor: pointer !important; }
body:has(#bookModal[aria-hidden="false"]) .cursor#cursor { display: none !important; }

/* ─────────────────────────────────────────────────────
   PHONES (2026-09-30). Under 641px the reader takes the whole screen:
   no backdrop margin, no frame border. The bar wraps to two lines (title,
   then counter + actions) so Close is never clipped off the right edge;
   book-modal.js pins the stage to one page at a time here, because two
   pages side by side on a phone were 136px wide. Prev / next sit in a row
   under the page with the counter between them, so nothing covers the
   image. Safe areas are respected (viewport-fit=cover pages).
   ───────────────────────────────────────────────────── */
@media (max-width: 640px) {
  .modal { padding: 0; }
  .modal-frame { max-width: none; width: 100%; height: 100%; min-height: 0; border: 0; box-shadow: none; }
  #bookModal .modal-bar { flex-wrap: wrap; gap: 0.35rem 0.5rem; padding: calc(0.55rem + env(safe-area-inset-top, 0px)) 0.75rem 0.5rem; }
  #bookModal .modal-title { flex: 1 1 100%; min-height: 0; }
  #bookModal .modal-meta { padding: 0; margin-right: auto; font-size: 0.85rem; }
  #bookModal .modal-actions { gap: 0.3rem; margin-left: auto; }
  .modal-action { min-height: 44px; padding: 0.4rem 0.6rem; font-size: 0.85rem; letter-spacing: 0.04em; }
  .spread-stage { min-height: 0; padding: 0.6rem 0.6rem 3.5rem; }
  .spread-nav { top: auto; bottom: 0.55rem; transform: none; width: 44px; height: 44px; font-size: 1.3rem; }
  .spread-nav.prev { left: 0.6rem; }
  .spread-nav.next { right: 0.6rem; }
  .spread-counter { right: 50%; bottom: 1.45rem; transform: translateX(50%); white-space: nowrap; }
  .spread-thumbs { padding: 0.5rem 0.6rem calc(0.5rem + env(safe-area-inset-bottom, 0px)); }
  .spread-thumb { width: 34px; height: 44px; }
  .cover-fallback { min-height: 0; padding: 1.2rem; }
  .cover-fallback img { max-height: 60vh; width: auto; }
}
/* Phones on their side: the same full-screen frame, everything a notch tighter. */
@media (max-height: 520px) {
  .modal { padding: 0; }
  .modal-frame { max-width: none; width: 100%; height: 100%; min-height: 0; border: 0; box-shadow: none; }
  .modal-bar { padding: 0.35rem 0.8rem; }
  .modal-action { padding: 0.3rem 0.6rem; font-size: 0.85rem; }
  .spread-stage { min-height: 0; padding: 0.5rem 3.6rem; }
  .spread-nav { width: 40px; height: 40px; }
  .spread-nav.prev { left: 0.6rem; }
  .spread-nav.next { right: 0.6rem; }
  .spread-counter { bottom: 0.4rem; }
  .spread-thumbs { padding: 0.35rem 0.6rem; }
  .spread-thumb { width: 27px; height: 36px; }
  .cover-fallback { min-height: 0; padding: 1rem; }
  .cover-fallback img { max-height: 55vh; width: auto; }
}
