/*
  FlipBook base: https://codepen.io/rokobuljan/pen/jORNEyz
  Intro inspiration: https://mchtyldzx.github.io/interactive-book-intro/

  This build:
  - Desktop/tablets/phones: the same 3D flipbook + intro.
  - Phones in portrait show an overlay asking to rotate to landscape.
  - Pages are rendered from the provided PDF into /assets/pages/*.webp
*/

:root {
  /* A robust unit fallback: cqmin when available, otherwise vmin */
  --u: 1vmin;
  --pageRatio: 1.4142; /* A4 height/width */

  /* Safe-area insets (used by JS to keep the book inside notches/cutouts) */
  --safe-top: env(safe-area-inset-top);
  --safe-right: env(safe-area-inset-right);
  --safe-bottom: env(safe-area-inset-bottom);
  --safe-left: env(safe-area-inset-left);

  /* Float amplitude (smaller on touch devices) */
  --floatY: -10px;
}
@supports (width: 1cqmin) {
  :root { --u: 1cqmin; }
}

@media (pointer: coarse) {
  :root { --floatY: -6px; }
}

* { box-sizing: border-box; }
html, body {
  height: 100%;
  width: 100%;
  overflow: hidden;
  overscroll-behavior: none;
}

html {
  background: #05070b;
}

body {
  margin: 0;

  /* Stable fullscreen on mobile (handles address bars better than 100vh) */
  height: 100vh;
  height: 100svh;
  height: 100dvh;
  min-height: 100vh;

  /* Prevent accidental scroll / rubber-band */
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;

  /* 3D */
  perspective: 1100px;

  font: 1em/1.4 "Poppins", system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif;
  color: hsl(200 30% 96%);

  background:
    radial-gradient(circle at 50% 25%, hsl(215 30% 22% / 0.7), transparent 55%),
    radial-gradient(circle at 20% 80%, hsl(190 80% 30% / 0.25), transparent 45%),
    radial-gradient(circle at 80% 75%, hsl(50 90% 55% / 0.12), transparent 50%),
    linear-gradient(180deg, hsl(220 25% 10%), hsl(220 25% 6%));
}

/*
  Touch interaction:
  - On phones we use swipes to adjust the book "pose" (parallax position).
  - Taps are reserved for page turning.
*/
@media (pointer: coarse) {
  .scene { touch-action: none; }
}

/* Scene layers */
.scene {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: grid;
  place-items: center;
  isolation: isolate;
  overflow: hidden;
}

@supports (overflow: clip) {
  .scene { overflow: clip; }
}

.fit {
  --fitTx: 0px;
  --fitTy: 0px;
  transform: translate3d(var(--fitTx), var(--fitTy), 0);
  transform-style: preserve-3d;
  will-change: transform;
  transition: transform 600ms cubic-bezier(.2, .9, .2, 1);
}

.fitScale {
  --fit: 1;
  transform: translateZ(0) scale(var(--fit));
  transform-origin: 50% 50%;
  transform-style: preserve-3d;
  will-change: transform;
  transition: transform 600ms cubic-bezier(.2, .9, .2, 1);
}

.entrance {
  transform-style: preserve-3d;
  will-change: transform, opacity, filter;
}

.float {
  transform-style: preserve-3d;
  animation: float 6.5s ease-in-out infinite;
}

@keyframes float {
  0%, 100% { transform: translate3d(0, 0, 0); }
  50%      { transform: translate3d(0, var(--floatY), 0); }
}

.parallax {
  --px: 0deg;
  --py: 0deg;
  --mx: 0px;
  --my: 0px;
  transform-style: preserve-3d;
  transform: translate3d(var(--mx), var(--my), 0) rotateX(var(--py)) rotateY(var(--px));
  will-change: transform;
}

/* Intro states */
body.preload .fit { visibility: hidden; }

body.is-intro .page { pointer-events: none; }
body.is-interactive .page { pointer-events: all; }

@keyframes introIn {
  0% {
    opacity: 0;
    filter: blur(16px);
    transform:
      translate3d(0, 70px, 0)
      translateZ(-800px)
      rotateX(78deg)
      rotateY(-28deg)
      scale(0.62);
  }
  60% {
    opacity: 1;
    filter: blur(0px);
    transform:
      translate3d(0, -8px, 0)
      translateZ(28px)
      rotateX(23deg)
      rotateY(6deg)
      scale(1.04);
  }
  100% {
    opacity: 1;
    filter: blur(0px);
    transform:
      translate3d(0, 0, 0)
      translateZ(0px)
      rotateX(28deg)
      rotateY(0deg)
      scale(1);
  }
}

body.is-ready .entrance {
  animation: introIn 1700ms cubic-bezier(.2, .95, .2, 1) both;
}

/* During measuring we force the final pose (no animations/transitions) */
body.is-measuring .fit,
body.is-measuring .fitScale,
body.is-measuring .entrance,
body.is-measuring .float,
body.is-measuring .parallax,
body.is-measuring .book,
body.is-measuring .page {
  transition: none !important;
  animation: none !important;
}

body.is-measuring .entrance {
  opacity: 1 !important;
  filter: none !important;
  transform:
    translate3d(0, 0, 0)
    translateZ(0px)
    rotateX(28deg)
    rotateY(0deg)
    scale(1) !important;
}

/* Atmospheric overlays */
.dust {
  position: absolute;
  inset: -30%;
  pointer-events: none;
  z-index: 0;
  opacity: 0.35;
  background:
    radial-gradient(circle, rgba(255,255,255,0.12) 0 1px, transparent 2px) 0 0 / 140px 140px,
    radial-gradient(circle, rgba(255,255,255,0.08) 0 1px, transparent 2px) 40px 30px / 180px 180px,
    radial-gradient(circle, rgba(255,255,255,0.06) 0 1px, transparent 2px) 10px 70px / 220px 220px;
  filter: blur(0.2px);
  animation: dustDrift 18s linear infinite;
}

@keyframes dustDrift {
  from { transform: translate3d(0, 0, 0); }
  to   { transform: translate3d(-120px, -80px, 0); }
}

.spotlight {
  position: absolute;
  inset: -20%;
  pointer-events: none;
  z-index: 0;
  background:
    radial-gradient(circle at 50% 35%, rgba(255, 230, 180, 0.22), transparent 55%),
    radial-gradient(circle at 52% 40%, rgba(160, 210, 255, 0.10), transparent 60%);
  filter: blur(24px);
  mix-blend-mode: screen;
  opacity: 0.7;
}

.vignette {
  position: absolute;
  inset: -1px;
  pointer-events: none;
  z-index: 2;
  background: radial-gradient(circle at 50% 40%, transparent 45%, rgba(0,0,0,0.7) 100%);
  opacity: 0.55;
}

/* HUD */
.hud {
  position: fixed;
  left: 50%;
  bottom: calc(18px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 50;

  display: inline-flex;
  align-items: center;
  gap: 10px;

  padding: 10px 14px;
  border-radius: 999px;

  color: rgba(255,255,255,0.92);
  background: rgba(0,0,0,0.35);
  border: 1px solid rgba(255,255,255,0.14);
  backdrop-filter: blur(10px);

  font-size: 14px;
  line-height: 1.1;
}

/* Mobile: hide the bottom page HUD completely */
body.is-phone .hud {
  display: none;
}

.hud__dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: rgba(255,255,255,0.75);
  box-shadow: 0 0 16px rgba(255,255,255,0.55);
  animation: hudPulse 1400ms ease-in-out infinite;
}

@keyframes hudPulse {
  0%, 100% { transform: scale(0.9); opacity: 0.6; }
  50% { transform: scale(1.1); opacity: 1; }
}

body.is-ready .hud__dot { animation-duration: 2200ms; }

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
  .float { animation: none; }
  .dust { animation: none; }
  body.is-ready .entrance { animation-duration: 1ms; }
  .fit { transition: none; }
}

/*
  ---------------------------------------------------------------------------
  FlipBook styles (adapted from the original CodePen)
  ---------------------------------------------------------------------------
*/

.book {
  position: relative;
  display: flex;

  /* Base page width (fit-scaler adapts the final size).
     IMPORTANT: use vw (not vmin) so the book doesn't become tiny on landscape phones. */
  width: clamp(220px, 46vw, 560px);

  pointer-events: none;
  transform-style: preserve-3d;

  transition: translate 1s;
  translate: calc(min(var(--c), 1) * 50%) 0%;

  /* Incline on the X axis for pages preview */
  rotate: 1 0 0 30deg;

  margin: 0;
}

.page {
  /* Visual thickness of the book. Smaller = more realistic, especially on mobile. */
  --thickness: 1.10;
  flex: none;
  display: flex;
  width: 100%;

  /* Aspect ratio gives us stable measuring even before images load */
  aspect-ratio: 1 / var(--pageRatio);

  pointer-events: all;
  user-select: none;
  -webkit-user-select: none;

  transform-style: preserve-3d;
  transform-origin: left center;
  transition:
    transform 1s,
    rotate 1s ease-in calc((min(var(--i), var(--c)) - max(var(--i), var(--c))) * 50ms);

  translate: calc(var(--i) * -100%) 0px 0px;
  transform: translateZ(calc((var(--c) - var(--i) - 0.5) * (var(--thickness) * 0.23 * var(--u))));
  rotate: 0 1 0 calc(clamp(0, var(--c) - var(--i), 1) * -180deg);

  box-shadow: 0em 0.5em 1em -0.2em #00000030;
  border-radius: 12px;
}

.front,
.back {
  position: relative;
  flex: none;
  width: 100%;
  height: 100%;
  backface-visibility: hidden;
  overflow: hidden;
  background-color: #fff;
  color: #111;
  translate: 0px; /* firefox backface fix */
}

.back {
  translate: -100% 0;
  rotate: 0 1 0 180deg;
}

.front,
.back {
  padding: 0;
  border: 1px solid rgba(0,0,0,0.12);
}

.front { border-radius: 8px 14px 14px 8px; }
.back  { border-radius: 14px 8px 8px 14px; }

.page.is-cover .front,
.page.is-cover .back,
.page.is-backcover .front,
.page.is-backcover .back {
  border-color: rgba(0,0,0,0.22);
}

.page img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: #fff;
  display: block;
  -webkit-user-drag: none;
}

/* Better touch feel when someone uses the 3D book on tablet */
/* We rely on tap/click to flip (no swipe navigation) */
.book { touch-action: manipulation; }

/* Slightly thinner stack on touch devices */
@media (pointer: coarse) {
  .page { --thickness: 0.72; }
}

/*
  ---------------------------------------------------------------------------
  Rotate overlay (phones in portrait)
  ---------------------------------------------------------------------------
*/

.rotateOverlay {
  position: fixed;
  inset: 0;
  z-index: 80;

  display: grid;
  place-items: center;

  padding:
    calc(24px + env(safe-area-inset-top))
    calc(24px + env(safe-area-inset-right))
    calc(24px + env(safe-area-inset-bottom))
    calc(24px + env(safe-area-inset-left));

  background:
    radial-gradient(circle at 50% 30%, rgba(255,255,255,0.08), transparent 55%),
    rgba(5, 7, 11, 0.76);

  backdrop-filter: blur(18px);

  opacity: 0;
  pointer-events: none;
  transition: opacity 220ms ease;
}

body.needs-rotate .rotateOverlay {
  opacity: 1;
  pointer-events: auto;
}

body.needs-rotate .hud {
  opacity: 0;
  pointer-events: none;
}

body.needs-rotate .page {
  pointer-events: none;
}

.rotateOverlay__card {
  width: min(360px, 88vw);
  padding: 18px 18px 16px;
  border-radius: 22px;

  border: 1px solid rgba(255,255,255,0.14);
  background: rgba(0,0,0,0.35);
  box-shadow: 0 18px 60px rgba(0,0,0,0.45);

  display: grid;
  place-items: center;
  gap: 14px;
  text-align: center;
}

.rotateOverlay__phone {
  width: 124px;
  height: 232px;
  border-radius: 28px;
  position: relative;
  border: 2px solid rgba(255,255,255,0.65);
  box-shadow:
    0 12px 40px rgba(0,0,0,0.55),
    inset 0 0 0 2px rgba(255,255,255,0.06);
  transform-origin: 50% 62%;
  animation: phoneRotate 1500ms cubic-bezier(.2,.9,.2,1) infinite;
}

.rotateOverlay__phone::before {
  content: "";
  position: absolute;
  left: 50%;
  top: 12px;
  transform: translateX(-50%);
  width: 44px;
  height: 7px;
  border-radius: 999px;
  background: rgba(255,255,255,0.28);
}

.rotateOverlay__screen {
  position: absolute;
  left: 12px;
  right: 12px;
  top: 24px;
  bottom: 16px;
  border-radius: 18px;
  overflow: hidden;
  background:
    linear-gradient(180deg, rgba(0,0,0,0.10), transparent),
    url("./assets/pages/page-001.webp") center / cover no-repeat;
  filter: saturate(1.05) contrast(1.05);
}

@keyframes phoneRotate {
  0%, 18%   { transform: rotate(0deg) scale(1); }
  50%       { transform: rotate(90deg) scale(1.02); }
  82%, 100% { transform: rotate(0deg) scale(1); }
}

.rotateOverlay__title {
  font-weight: 700;
  font-size: 16px;
  letter-spacing: 0.2px;
}

.rotateOverlay__sub {
  font-size: 13px;
  opacity: 0.86;
}

/* On small phones we make HUD smaller */
@media (max-width: 420px) {
  .hud {
    font-size: 13px;
    padding: 9px 12px;
  }
}
