/* =====================================================================
   MORIBUND — Opening Credits effects (styles for fx.js)
   Loaded after credits.css on the home, universe, books and reading pages.

   Adapted from React Bits (https://reactbits.dev): DecryptedText,
   SplitFlapText, HalftoneReveal, DitherVeil, Dither, LightRays, Noise,
   StarBorder, CircularText.
   Copyright (c) 2026 David Haz. MIT + Commons Clause
   (full licence text: third-party-licenses.txt).

   Black, white and grey only. Every effect has a still version for
   prefers-reduced-motion; without JavaScript none of this applies.
   ===================================================================== */

.fx-sr {
  position: absolute !important; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
}

/* ---------- 1. Credits text ---------- */
.fx-word { display: inline-block; white-space: nowrap; }

/* Decrypted letters: the real letter holds the width; a scrambled one is drawn over it. */
.fx-c { position: relative; display: inline-block; transition: opacity .6s cubic-bezier(.22, 1, .36, 1); }
.fx-c.is-x { transition: opacity 50ms linear; }
.fx-c.is-x > .fx-gh { visibility: hidden; }
.fx-c.is-x::after {
  content: attr(data-g); position: absolute; inset: 0; display: flex; justify-content: center;
  white-space: pre; transform: scaleX(var(--sx, 1));
}

/* Split-flap letters, "minimal" board: no tile, just the hairline split and the flap. */
.fx-t {
  position: relative; display: inline-block; perspective: 6em;
  -webkit-mask: linear-gradient(#000 0 0) top / 100% calc(50% - .028em) no-repeat, linear-gradient(#000 0 0) bottom / 100% calc(50% - .028em) no-repeat;
  mask: linear-gradient(#000 0 0) top / 100% calc(50% - .028em) no-repeat, linear-gradient(#000 0 0) bottom / 100% calc(50% - .028em) no-repeat;
}
.fx-t.is-done { -webkit-mask: none; mask: none; }
.fx-t > .fx-gh { visibility: hidden; }
.fx-h, .fx-f { position: absolute; left: 0; width: 100%; height: 50%; overflow: hidden; pointer-events: none; }
.fx-top, .fx-fr { top: 0; }
.fx-bot, .fx-bk { bottom: 0; }
.fx-ch { position: absolute; left: 0; width: 100%; height: 200%; display: flex; align-items: center; justify-content: center; white-space: pre; }
.fx-top .fx-ch, .fx-fr .fx-ch { top: 0; }
.fx-bot .fx-ch, .fx-bk .fx-ch { bottom: 0; }
.fx-f { display: none; }
.fx-t.is-flip .fx-f { display: block; }
.fx-fr { transform-origin: 50% 100%; }
.fx-bk { transform-origin: 50% 0; transform: rotateX(90deg); }
.fx-t.is-done > .fx-gh { visibility: visible; }
.fx-t.is-done > .fx-h { display: none; }

/* ---------- 2. Dither and halftone frames ---------- */
/* Halftone dots over each chapter frame; the graded image shows through as they fade. */
.fx-halftone { position: absolute; inset: 0; width: 100%; height: 100%; display: block; pointer-events: none; }
/* The closing star: a 1-bit veil drawn over the same art, same size and mask. */
canvas.closing-art.fx-veil { image-rendering: pixelated; image-rendering: crisp-edges; filter: none; }
/* Inner pages: a veil over the letterboxed scene behind the title. */
.fx-scene {
  position: absolute; top: 0; left: 0; z-index: -1; width: 100%; height: 70vh; height: min(70svh, 620px);
  pointer-events: none; image-rendering: pixelated; image-rendering: crisp-edges;
}
html[data-theme="light"] .fx-scene { display: none; }

/* ---------- 3. Projector beam and film grain ---------- */
.fx-rays {
  position: absolute; inset: 0; z-index: -1; width: 100%; height: 100%; display: block;
  pointer-events: none; mix-blend-mode: screen; opacity: 0;
}
/* Moving grain: the tile layer is larger than the screen and jumps about, so it never repaints. */
.site-noise.fx-grain {
  inset: -256px 0 0 -256px; background-size: 256px 256px; background-repeat: repeat;
  opacity: .06; will-change: transform;
}
html[data-theme="light"] .site-noise.fx-grain { opacity: .045; }

/* ---------- 4. The prologue button and the seal ---------- */
/* A star of light laps the border of the prologue button. */
.btn.fx-star { overflow: visible; }
.fx-star-light { position: absolute; top: -48px; left: -48px; z-index: 1; width: calc(100% + 96px); height: calc(100% + 96px); pointer-events: none; }

/* "MORIBUND · BOOK ONE ·" turning around the M stamp. */
.fx-seal {
  --seal: 84px;
  position: absolute; width: var(--seal); height: var(--seal); pointer-events: none;
  color: var(--text); font-family: var(--sub); font-weight: 400; font-size: .76rem; line-height: 1;
  letter-spacing: 0; text-transform: uppercase; text-shadow: 0 0 8px #000, 0 0 2px #000;
}
.fx-seal-ring { position: absolute; inset: 0; }
.fx-seal-l { position: absolute; inset: 0; display: flex; justify-content: center; white-space: pre; }
.fx-seal-m {
  position: absolute; inset: 0; margin: auto; width: 22px; height: 27px; background: currentColor;
  -webkit-mask: url("assets/brand/monogram.svg") center / contain no-repeat; mask: url("assets/brand/monogram.svg") center / contain no-repeat;
}
@keyframes fx-seal-turn { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: no-preference) {
  .fx-seal-ring { animation: fx-seal-turn 28s linear infinite; }
  .fx-seal.is-off .fx-seal-ring { animation-play-state: paused; }
}
@media (min-width: 900px) {
  .fx-seal { --seal: 112px; font-size: .86rem; }
  .fx-seal-m { width: 28px; height: 34px; }
}

/* The opening: the seal is stamped inside the top-right corner of the letterboxed frame. */
.opening-layout > .opening-seal { left: auto; right: var(--gutter); margin: 0; top: calc(var(--frame-top) + 12px); }
@media (max-height: 700px) and (max-width: 899px) { .opening-layout > .opening-seal { --seal: 72px; font-size: .7rem; } }
/* Small or short phones: a smaller stamp tucked into the corner, clear of the title card. */
@media (max-width: 380px), (max-height: 620px) and (max-width: 899px) {
  .opening-layout > .opening-seal { --seal: 62px; font-size: .58rem; top: calc(var(--frame-top) + 8px); }
  .opening-seal .fx-seal-m { width: 16px; height: 20px; }
}
/* Without the scroll engine the opening is one still screen; the seal sits in the same corner. */
html:not(.cinema) .opening-seal { position: absolute; }

/* Books page: the seal overlaps the top-right corner of the Book I cover. */
.cover-frame > .fx-seal { top: calc(var(--seal) * -.36); right: calc(var(--seal) * -.36); z-index: 2; }
/* On paper the seal prints by difference: black on the page, white where it crosses the dark cover. */
html[data-theme="light"] .book-landing .fx-seal { color: #fff; mix-blend-mode: difference; text-shadow: none; }
