/*
 * Motion library: page transitions and content reveals.
 * Driven by public/js/shared/motion.js; loaded by the viewer and by the
 * admin portal (for live previews).
 *
 * Page transitions: the stage stacks the old and the new page in one grid
 * cell. The old page gets .pt-leave, the new one .pt-enter, both plus
 * .pt-<name>.
 *
 * Back is a rewind: [data-back] plays the very same animation backwards in
 * time. The page that leaves takes the keyframes, timing and delay the
 * page had when it came in (reversed), the page that returns takes the
 * ones it had when it left – so a page that sank from the centre to the
 * bottom comes back up from the bottom to the centre. --nav-dir stays 1.
 */

/* ======================================================================
   Page transitions
   ====================================================================== */

.pt-enter,
.pt-leave {
  animation-duration: var(--pt-ms, 700ms);
  animation-fill-mode: both;
  animation-timing-function: var(--ease-in-out);
  will-change: transform, opacity;
}

.pt-leave {
  pointer-events: none;
}

[data-transition='cube'],
[data-transition='flip'] {
  perspective: 1800px;
}

[data-transition='cube'] .pt-enter,
[data-transition='cube'] .pt-leave,
[data-transition='flip'] .pt-enter,
[data-transition='flip'] .pt-leave {
  backface-visibility: hidden;
}

[data-transition='push'],
[data-transition='cube'] {
  overflow-x: clip;
}

/* Glide: the content reveal carries the movement. */
.pt-leave.pt-glide { animation-name: glide-out; animation-duration: 420ms; }
.pt-enter.pt-glide { animation-name: soft-in; animation-duration: 300ms; }

@keyframes glide-out {
  to { opacity: 0; transform: translateX(calc(var(--nav-dir, 1) * -70px)) scale(0.98); }
}

@keyframes soft-in {
  from { opacity: 0; }
}

/* Push */
.pt-leave.pt-push { animation-name: push-out; }
.pt-enter.pt-push { animation-name: push-in; }

@keyframes push-out {
  to { transform: translateX(calc(var(--nav-dir, 1) * -110%)); opacity: 0.2; }
}

@keyframes push-in {
  from { transform: translateX(calc(var(--nav-dir, 1) * 110%)); opacity: 0.4; }
}

/* Cinematic depth */
.pt-leave.pt-depth { animation-name: depth-out; }
.pt-enter.pt-depth { animation-name: depth-in; animation-timing-function: var(--ease-out); }

@keyframes depth-out {
  to { opacity: 0; transform: scale(0.84); filter: blur(6px) brightness(0.7); }
}

@keyframes depth-in {
  from { opacity: 0; transform: scale(1.16); filter: blur(12px); }
}

/* 3D cube: faces hinge on the shared edge (--o-out / --o-in, mirrored when going back). */
.pt-leave.pt-cube { animation-name: cube-out; transform-origin: var(--o-out, 100%) 50%; }
.pt-enter.pt-cube { animation-name: cube-in; transform-origin: var(--o-in, 0%) 50%; }

@keyframes cube-out {
  to { transform: translateX(calc(var(--nav-dir, 1) * -100%)) rotateY(calc(var(--nav-dir, 1) * -90deg)); opacity: 0.3; }
}

@keyframes cube-in {
  from { transform: translateX(calc(var(--nav-dir, 1) * 100%)) rotateY(calc(var(--nav-dir, 1) * 90deg)); opacity: 0.3; }
}

/* 3D flip: first half turns the old page away, second half brings the new one in. */
.pt-leave.pt-flip {
  animation-name: flip-out;
  animation-duration: calc(var(--pt-ms, 900ms) / 2);
  animation-timing-function: cubic-bezier(0.55, 0, 1, 0.45);
}

.pt-enter.pt-flip {
  animation-name: flip-in;
  animation-duration: calc(var(--pt-ms, 900ms) / 2);
  animation-delay: calc(var(--pt-ms, 900ms) / 2);
  animation-timing-function: var(--ease-out);
}

@keyframes flip-out {
  to { transform: rotateY(calc(var(--nav-dir, 1) * -90deg)) scale(0.9); opacity: 0.5; }
}

@keyframes flip-in {
  from { transform: rotateY(calc(var(--nav-dir, 1) * 90deg)) scale(0.9); opacity: 0.5; }
}

/* Iris */
.pt-leave.pt-iris { animation-name: iris-out; }
.pt-enter.pt-iris { animation-name: iris-in; }

@keyframes iris-out {
  to { opacity: 0; transform: scale(0.94); }
}

@keyframes iris-in {
  from { clip-path: circle(0% at 50% 50%); }
  to { clip-path: circle(75% at 50% 50%); }
}

/* Light wipe: a glowing edge sweeps across and uncovers the page. */
.pt-leave.pt-wipe { animation-name: dissolve-out; }
.pt-enter.pt-wipe { animation-name: wipe-in; }

.pt-enter.pt-wipe::after {
  content: '';
  position: absolute;
  top: -8%;
  bottom: -8%;
  width: 2px;
  background: var(--accent);
  box-shadow: 0 0 22px 6px rgba(var(--accent-rgb), 0.55);
  animation: wipe-edge var(--pt-ms, 820ms) var(--ease-in-out) both;
  pointer-events: none;
}

@keyframes wipe-in {
  from { clip-path: inset(-10% 100% -10% 0); }
  to { clip-path: inset(-10% 0 -10% 0); }
}

@keyframes wipe-edge {
  from { left: 0; opacity: 1; }
  85% { opacity: 1; }
  to { left: 100%; opacity: 0; }
}

/* Dissolve */
.pt-leave.pt-dissolve { animation-name: dissolve-out; }
.pt-enter.pt-dissolve { animation-name: dissolve-in; }

@keyframes dissolve-out {
  to { opacity: 0; filter: blur(8px); }
}

@keyframes dissolve-in {
  from { opacity: 0; filter: blur(8px); transform: scale(1.02); }
}

/* Fly-through */
.pt-leave.pt-through { animation-name: through-out; animation-timing-function: cubic-bezier(0.5, 0, 0.75, 0); }
.pt-enter.pt-through { animation-name: through-in; animation-timing-function: var(--ease-out); }

@keyframes through-out {
  to { opacity: 0; transform: scale(1.6); filter: blur(12px); }
}

@keyframes through-in {
  from { opacity: 0; transform: scale(0.7); filter: blur(6px); }
}

/*
 * Back = rewind for the classic transitions. In a rewind the page that
 * came in last lies on top, as it did at the end of the forward move.
 * A part that ran from d to d+D (of the total T) runs backwards from
 * T-(d+D) to T-d, hence the delays below.
 */
[data-back] .pt-leave {
  z-index: 1;
}

[data-back] .pt-leave.pt-glide { animation-name: soft-in; animation-direction: reverse; animation-duration: 300ms; animation-delay: 220ms; }
[data-back] .pt-enter.pt-glide { animation-name: glide-out; animation-direction: reverse; animation-duration: 420ms; animation-delay: 100ms; }

[data-back] .pt-leave.pt-push { animation-name: push-in; animation-direction: reverse; }
[data-back] .pt-enter.pt-push { animation-name: push-out; animation-direction: reverse; }

[data-back] .pt-leave.pt-depth { animation-name: depth-in; animation-direction: reverse; animation-timing-function: var(--ease-out); }
[data-back] .pt-enter.pt-depth { animation-name: depth-out; animation-direction: reverse; animation-timing-function: var(--ease-in-out); }

[data-back] .pt-leave.pt-cube { animation-name: cube-in; animation-direction: reverse; transform-origin: 0% 50%; }
[data-back] .pt-enter.pt-cube { animation-name: cube-out; animation-direction: reverse; transform-origin: 100% 50%; }

[data-back] .pt-leave.pt-flip {
  animation-name: flip-in;
  animation-direction: reverse;
  animation-delay: 0ms;
  animation-timing-function: var(--ease-out);
}

[data-back] .pt-enter.pt-flip {
  animation-name: flip-out;
  animation-direction: reverse;
  animation-delay: calc(var(--pt-ms, 900ms) / 2);
  animation-timing-function: cubic-bezier(0.55, 0, 1, 0.45);
}

[data-back] .pt-leave.pt-iris { animation-name: iris-in; animation-direction: reverse; }
[data-back] .pt-enter.pt-iris { animation-name: iris-out; animation-direction: reverse; }

[data-back] .pt-leave.pt-wipe { animation-name: wipe-in; animation-direction: reverse; }
[data-back] .pt-enter.pt-wipe { animation-name: dissolve-out; animation-direction: reverse; }

/* The glowing edge belongs to the page that is uncovered or covered again. */
[data-back] .pt-leave.pt-wipe::after {
  content: '';
  position: absolute;
  top: -8%;
  bottom: -8%;
  width: 2px;
  background: var(--accent);
  box-shadow: 0 0 22px 6px rgba(var(--accent-rgb), 0.55);
  animation: wipe-edge var(--pt-ms, 820ms) var(--ease-in-out) reverse both;
  pointer-events: none;
}

[data-back] .pt-enter.pt-wipe::after {
  content: none;
}

[data-back] .pt-leave.pt-dissolve { animation-name: dissolve-in; animation-direction: reverse; }
[data-back] .pt-enter.pt-dissolve { animation-name: dissolve-out; animation-direction: reverse; }

/*
 * Travelling transitions: like a camera moving between two places. The
 * background moves along (see Background.travel). Both pages share the
 * timeline, so the old place is still visible while the new one arrives.
 *
 *   travel  – pull back, pan sideways, land
 *   ascend  – pull back, rise to the place above, land
 *   descend – pull back, sink to the place below, land
 *   orbit   – swing around the scene in 3D
 *   voyage  – the place drops away, the next flies in from the distance
 *   warp    – fly past the place, the next spins in from hyperspace
 *
 * Back ([data-back]) plays exactly the same animation backwards: each page
 * takes the other page's keyframes, timing and delay, reversed in time.
 */
[data-transition='travel'],
[data-transition='ascend'],
[data-transition='descend'],
[data-transition='orbit'],
[data-transition='voyage'],
[data-transition='warp'] {
  overflow: clip;
  overflow-clip-margin: 48px;
}

[data-transition='orbit'] {
  perspective: 1600px;
}

/* ---- travel ---- */
.pt-leave.pt-travel { animation-name: travel-out; animation-timing-function: cubic-bezier(0.55, 0, 0.35, 1); }
.pt-enter.pt-travel { animation-name: travel-in; animation-timing-function: cubic-bezier(0.55, 0, 0.35, 1); }
[data-back] .pt-leave.pt-travel { animation-name: travel-in; animation-direction: reverse; }
[data-back] .pt-enter.pt-travel { animation-name: travel-out; animation-direction: reverse; }

@keyframes travel-out {
  0% { transform: none; opacity: 1; }
  32% { transform: translateX(calc(var(--flip, 1) * -6%)) scale(0.88); opacity: 1; filter: none; }
  100% { transform: translateX(calc(var(--flip, 1) * -78%)) scale(0.82); opacity: 0; filter: blur(5px); }
}

@keyframes travel-in {
  0% { transform: translateX(calc(var(--flip, 1) * 78%)) scale(0.82); opacity: 0; filter: blur(5px); }
  62% { transform: translateX(calc(var(--flip, 1) * 5%)) scale(0.88); opacity: 1; filter: none; }
  100% { transform: none; opacity: 1; }
}

/* ---- ascend / descend (--rise: 1 = up, -1 = down) ---- */
.pt-ascend { --rise: 1; }
.pt-descend { --rise: -1; }
.pt-leave.pt-ascend,
.pt-leave.pt-descend { animation-name: rise-out; animation-timing-function: cubic-bezier(0.55, 0, 0.35, 1); }
.pt-enter.pt-ascend,
.pt-enter.pt-descend { animation-name: rise-in; animation-timing-function: cubic-bezier(0.55, 0, 0.35, 1); }
[data-back] .pt-leave.pt-ascend,
[data-back] .pt-leave.pt-descend { animation-name: rise-in; animation-direction: reverse; }
[data-back] .pt-enter.pt-ascend,
[data-back] .pt-enter.pt-descend { animation-name: rise-out; animation-direction: reverse; }

@keyframes rise-out {
  0% { transform: none; opacity: 1; }
  32% { transform: translateY(calc(var(--rise) * 5%)) scale(0.88); opacity: 1; filter: none; }
  100% { transform: translateY(calc(var(--rise) * 70%)) scale(0.8); opacity: 0; filter: blur(5px); }
}

@keyframes rise-in {
  0% { transform: translateY(calc(var(--rise) * -70%)) scale(0.8); opacity: 0; filter: blur(5px); }
  62% { transform: translateY(calc(var(--rise) * -4%)) scale(0.88); opacity: 1; filter: none; }
  100% { transform: none; opacity: 1; }
}

/* ---- orbit ---- */
.pt-leave.pt-orbit { animation-name: orbit-out; animation-timing-function: cubic-bezier(0.5, 0, 0.3, 1); }
.pt-enter.pt-orbit { animation-name: orbit-in; animation-timing-function: cubic-bezier(0.5, 0, 0.3, 1); }
[data-back] .pt-leave.pt-orbit { animation-name: orbit-in; animation-direction: reverse; }
[data-back] .pt-enter.pt-orbit { animation-name: orbit-out; animation-direction: reverse; }

@keyframes orbit-out {
  0% { transform: none; opacity: 1; }
  100% { transform: translateX(calc(var(--flip, 1) * -58%)) translateZ(-420px) rotateY(calc(var(--flip, 1) * 55deg)); opacity: 0; filter: blur(3px); }
}

@keyframes orbit-in {
  0% { transform: translateX(calc(var(--flip, 1) * 58%)) translateZ(-420px) rotateY(calc(var(--flip, 1) * -55deg)); opacity: 0; filter: blur(3px); }
  100% { transform: none; opacity: 1; }
}

/* ---- voyage (old page: 0–50 %, new page: 30–100 % of the time) ---- */
.pt-leave.pt-voyage {
  animation-name: voyage-out;
  animation-duration: calc(var(--pt-ms, 1500ms) * 0.5);
  animation-timing-function: cubic-bezier(0.6, 0, 0.9, 0.4);
}

.pt-enter.pt-voyage {
  animation-name: voyage-in;
  animation-duration: calc(var(--pt-ms, 1500ms) * 0.7);
  animation-delay: calc(var(--pt-ms, 1500ms) * 0.3);
  animation-timing-function: cubic-bezier(0.15, 0.7, 0.2, 1);
}

[data-back] .pt-leave.pt-voyage {
  animation-name: voyage-in;
  animation-direction: reverse;
  animation-duration: calc(var(--pt-ms, 1500ms) * 0.7);
  animation-delay: 0ms;
  animation-timing-function: cubic-bezier(0.15, 0.7, 0.2, 1);
}

[data-back] .pt-enter.pt-voyage {
  animation-name: voyage-out;
  animation-direction: reverse;
  animation-duration: calc(var(--pt-ms, 1500ms) * 0.5);
  animation-delay: calc(var(--pt-ms, 1500ms) * 0.5);
  animation-timing-function: cubic-bezier(0.6, 0, 0.9, 0.4);
}

@keyframes voyage-out {
  to { transform: perspective(1200px) translateY(18%) rotateX(24deg) scale(0.55); opacity: 0; filter: blur(6px); }
}

@keyframes voyage-in {
  0% { transform: scale(0.18); opacity: 0; filter: blur(14px) brightness(1.6); }
  45% { opacity: 1; }
  100% { transform: none; opacity: 1; filter: none; }
}

/* ---- warp (old page: 0–45 %, new page: 35–100 % of the time) ---- */
.pt-leave.pt-warp {
  animation-name: warp-out;
  animation-duration: calc(var(--pt-ms, 1700ms) * 0.45);
  animation-timing-function: cubic-bezier(0.7, 0, 1, 0.5);
}

.pt-enter.pt-warp {
  animation-name: warp-in;
  animation-duration: calc(var(--pt-ms, 1700ms) * 0.65);
  animation-delay: calc(var(--pt-ms, 1700ms) * 0.35);
  animation-timing-function: cubic-bezier(0.1, 0.75, 0.2, 1);
}

[data-back] .pt-leave.pt-warp {
  animation-name: warp-in;
  animation-direction: reverse;
  animation-duration: calc(var(--pt-ms, 1700ms) * 0.65);
  animation-delay: 0ms;
  animation-timing-function: cubic-bezier(0.1, 0.75, 0.2, 1);
}

[data-back] .pt-enter.pt-warp {
  animation-name: warp-out;
  animation-direction: reverse;
  animation-duration: calc(var(--pt-ms, 1700ms) * 0.45);
  animation-delay: calc(var(--pt-ms, 1700ms) * 0.55);
  animation-timing-function: cubic-bezier(0.7, 0, 1, 0.5);
}

@keyframes warp-out {
  to { transform: scale(2.4) rotate(10deg); opacity: 0; filter: blur(10px) brightness(1.5); }
}

@keyframes warp-in {
  0% { transform: scale(0.05) rotate(-40deg); opacity: 0; filter: blur(16px) brightness(1.8); }
  40% { opacity: 1; }
  100% { transform: none; opacity: 1; filter: none; }
}

/* ---- fly-through played backwards ---- */
[data-back] .pt-leave.pt-through { animation-name: through-in; animation-direction: reverse; animation-timing-function: var(--ease-out); }
[data-back] .pt-enter.pt-through { animation-name: through-out; animation-direction: reverse; animation-timing-function: cubic-bezier(0.5, 0, 0.75, 0); }

/* ======================================================================
   Content reveals
   ====================================================================== */

[data-reveal] {
  animation-duration: 0.75s;
  animation-timing-function: var(--ease-out);
  animation-fill-mode: both;
  animation-delay: var(--delay, 0ms);
}

.reveal-rise { animation-name: reveal-rise; }
.reveal-zoom { animation-name: reveal-zoom; }
.reveal-slide { animation-name: reveal-slide; }
.reveal-focus { animation-name: reveal-focus; animation-duration: 1.15s; }

@keyframes reveal-rise {
  from { opacity: 0; transform: translateY(26px); }
}

@keyframes reveal-zoom {
  from { opacity: 0; transform: scale(0.86); filter: blur(6px); }
}

@keyframes reveal-slide {
  from { opacity: 0; transform: translateX(calc(var(--nav-dir, 1) * 90px)); }
}

/* Apple-keynote style: out of a blur, letters drawing together. */
@keyframes reveal-focus {
  from { opacity: 0; filter: blur(16px); letter-spacing: 0.2em; transform: scale(1.03); }
  60% { opacity: 1; }
}

/* Word by word */
.rv-word {
  display: inline-block;
  transform-origin: 50% 100%;
  animation: rv-word 0.85s var(--ease-out) both;
  animation-delay: var(--delay, 0ms);
}

@keyframes rv-word {
  from { opacity: 0; transform: translateY(0.55em) rotateX(-60deg); filter: blur(6px); }
}

/* Letter cascade */
.rv-letter {
  display: inline-block;
  animation: rv-letter 0.6s var(--ease-out) both;
  animation-delay: var(--delay, 0ms);
}

@keyframes rv-letter {
  from { opacity: 0; transform: translateY(-0.45em) scale(1.5); filter: blur(4px); }
}

/* Typewriter caret */
.rv-typing::after {
  content: '';
  display: inline-block;
  width: 0.07em;
  height: 0.95em;
  margin-left: 0.06em;
  vertical-align: -0.1em;
  background: var(--accent);
  animation: caret 0.9s steps(1) infinite;
}

@keyframes caret {
  50% { opacity: 0; }
}

/* Mask reveal */
.reveal-mask {
  animation: reveal-mask 1s var(--ease-in-out) both;
  animation-delay: var(--delay, 0ms);
}

@keyframes reveal-mask {
  from { clip-path: inset(-20% 100% -20% 0); }
  to { clip-path: inset(-20% 0 -20% 0); }
}

/* ======================================================================
   Chapter title screen
   ====================================================================== */

.chapter-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: 14px;
  min-height: 52vh;
  justify-content: center;
}

.chapter-number {
  font-family: var(--font-display);
  font-size: clamp(5rem, 14vw, 11rem);
  font-weight: 700;
  line-height: 0.9;
  background: linear-gradient(160deg, var(--accent), var(--accent2));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  animation: chapter-number 1.4s var(--ease-out) both;
}

@keyframes chapter-number {
  from { opacity: 0; transform: scale(1.6); filter: blur(18px); letter-spacing: 0.3em; }
}

.chapter-eyebrow {
  font-size: 12px;
  text-transform: uppercase;
  color: var(--accent);
}

.chapter-title {
  font-size: clamp(2rem, 4.6vw, 3.6rem);
  font-weight: 700;
}

.chapter-steps {
  list-style: none;
  padding: 0;
  margin: 18px 0 0;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
}

.chapter-steps li {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 14px;
  border-radius: 999px;
  font-size: 13.5px;
  background: rgba(var(--surface-rgb), 0.6);
  border: 1px solid rgba(var(--accent-rgb), 0.22);
}

.chapter-steps .mono {
  color: var(--accent);
  font-size: 11.5px;
}

@media (prefers-reduced-motion: reduce) {
  .pt-enter,
  .pt-leave,
  .rv-word,
  .rv-letter,
  .reveal-mask,
  .chapter-number {
    animation: none !important;
  }
}
