/* Spooky Pals · scene.css (v3) — the Ghostie lives inside its place (pairs with scene.js).
   Everything is scoped to .story-visual.has-scene, which scene.js adds only after
   assets/scenes/<id>.webp loaded. No class = the original look, untouched.
   .has-cutout = the border-less cutout loaded; without it the sticker stays (fallback).
   Animates only transform/opacity. No live blur: the background is pre-blurred in the file.
   Integration recipe (compositing basics): match the light (grade + rim on the lit side),
   ground it (contact shadow + occlusion), add air in front (haze), and give depth (parallax). */

.story-visual .scene-backdrop,
.story-visual .scene-ground,
.story-visual .scene-haze,
.story-visual .scene-fg,
.story-visual .scene-sparkles,
.story-visual .scene-cutout,
.story-visual .scene-grade,
.story-visual .scene-occlude,
.story-visual .scene-rim,
.story-visual .scene-wrap { display: none; }

/* The wrappers are neutral until a scene is on. */
.story-visual .scene-actor { display: contents; }
.story-visual .scene-body { display: contents; }

.story-visual.has-scene {
  overflow: hidden;
  isolation: isolate;
  perspective: 900px;
  /* Light that glows (rim, pools, motes, sparkles). colors-hd.css enriches it on P3 screens. */
  --scene-glow: var(--scene-light);
}

/* ---------- Far layer ---------- */
.story-visual.has-scene .scene-backdrop {
  display: block;
  position: absolute;
  inset: 0;
  z-index: 0;
  background: var(--scene-image) center var(--scene-focus-y, 45%) / cover no-repeat, #1d1828; /* focal point per place (scene.js) */
  pointer-events: none;
  transform: scale(1.07); /* room for the parallax shift, so no edge ever shows */
  animation: scene-fade .45s ease-out both;
  will-change: transform;
}
/* Vignette + floor darkness so the character owns the centre. */
.story-visual.has-scene .scene-backdrop::after {
  content: '';
  position: absolute;
  inset: 0;
  background:
    radial-gradient(ellipse 75% 62% at 50% 50%, transparent 42%, rgba(14, 9, 24, .62) 100%),
    linear-gradient(to bottom, rgba(14, 9, 24, .28), transparent 26%, transparent 70%, rgba(14, 9, 24, .4));
}

/* Labels sit on a dark photo now. */
.story-visual.has-scene .story-number,
.story-visual.has-scene .story-stamp {
  z-index: 5;
  color: #fff8ec;
  text-shadow: 0 1px 3px rgba(10, 6, 20, .7);
}

/* ---------- Character ---------- */
.story-visual.has-scene .scene-actor {
  display: block;
  position: relative;
  z-index: 2;
  width: 100%;
  transform-style: flat;
  transform-origin: 50% 85%;
  will-change: transform;
}
.story-visual.has-scene .scene-body {
  display: block;
  position: relative;
  line-height: 0;
  transform-origin: 50% 92%;
}
#story-dialog .story-visual.has-scene #story-image {
  display: block;
  position: relative;
  top: auto; /* extras.css gives this img top:65px for its sticky mode */
  margin: 0;
  animation: none;
  filter: drop-shadow(0 0 5px color-mix(in srgb, var(--scene-light) 75%, transparent)) drop-shadow(0 14px 10px rgba(10, 6, 20, .5));
}
.story-visual.has-scene img { animation: none; }

/* Cutout on: the sticker stays in the DOM (alt text, other scripts read its src) but is not painted. */
#story-dialog .story-visual.has-scene.has-cutout #story-image { opacity: 0; filter: none; }
.story-visual.has-scene.has-cutout .scene-cutout,
.story-visual.has-scene.has-cutout .scene-grade,
.story-visual.has-scene.has-cutout .scene-occlude,
.story-visual.has-scene.has-cutout .scene-wrap,
.story-visual.has-scene.has-cutout .scene-rim {
  display: block;
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}
.story-visual.has-scene.has-cutout .scene-cutout {
  object-fit: contain;
  /* soft cast shadow that follows the silhouette; no white sticker edge any more */
  filter: drop-shadow(0 10px 9px rgba(8, 5, 18, .42));
}
/* Masks: the cutout itself, sized exactly like the <img> (contain, centred). */
.story-visual.has-cutout .scene-grade,
.story-visual.has-cutout .scene-occlude,
.story-visual.has-cutout .scene-wrap,
.story-visual.has-cutout .scene-rim {
  -webkit-mask: var(--cutout) center / contain no-repeat;
  mask: var(--cutout) center / contain no-repeat;
}
/* 1. Colour grade toward the scene: light side warms/cools to the scene light, far side to its shade. */
.story-visual.has-cutout .scene-grade {
  background: linear-gradient(var(--light-angle, 160deg), var(--scene-light) 0%, transparent 48%, var(--scene-shade, #120c1e) 100%);
  mix-blend-mode: soft-light;
  opacity: .78;
}
/* 2. Ground occlusion: the lower body picks up the floor's darkness. */
.story-visual.has-cutout .scene-occlude {
  background: linear-gradient(to top, color-mix(in srgb, var(--scene-shade, #120c1e) 70%, transparent) 0%, transparent 26%);
  mix-blend-mode: multiply;
  opacity: .5; /* lighter than a solid body would get: the veiled hem already lets the floor through */
}
/* 2b. Light-wrap: the HD cutout is a little see-through toward the tail (ghost veil, baked into the
   file: outlines, eyes and props stay opaque). The place's light wraps the body's edges and hem so
   the see-through part reads as a luminous spirit, not a faded sticker. */
.story-visual.has-cutout .scene-wrap {
  background:
    radial-gradient(ellipse 68% 60% at 50% 36%, transparent 50%, color-mix(in oklab, var(--scene-glow) 60%, #fff) 100%),
    linear-gradient(to bottom, transparent 52%, color-mix(in oklab, var(--scene-glow) 45%, rgba(255, 255, 255, .7)) 100%);
  mix-blend-mode: soft-light; /* lifts the pale sheet, leaves the dark ink outline dark */
  opacity: .85;
}
/* 3. Rim light: the silhouette minus a copy shifted away from the light = a thin lit edge. */
.story-visual.has-cutout .scene-rim {
  background: var(--scene-glow);
  -webkit-mask-image: var(--cutout), var(--cutout);
  -webkit-mask-size: contain, contain;
  -webkit-mask-repeat: no-repeat, no-repeat;
  -webkit-mask-position: center, calc(50% + var(--rim-dx, 2px)) calc(50% + var(--rim-dy, 3px));
  -webkit-mask-composite: source-out;
  mask-image: var(--cutout), var(--cutout);
  mask-size: contain, contain;
  mask-repeat: no-repeat, no-repeat;
  mask-position: center, calc(50% + var(--rim-dx, 2px)) calc(50% + var(--rim-dy, 3px));
  mask-composite: subtract;
  mix-blend-mode: screen;
  opacity: .62;
}

/* ---------- Ground: lit pool + dark contact core under the art's lowest pixels ---------- */
.story-visual.has-scene .scene-ground {
  --ground-h: calc(var(--ground-w, 160px) * .22);
  display: block;
  position: absolute;
  z-index: 1;
  left: var(--ground-x, 50%);
  top: var(--ground-y, 85%);
  width: var(--ground-w, 160px);
  height: var(--ground-h);
  margin-left: calc(var(--ground-w, 160px) / -2);
  margin-top: calc(var(--ground-h) * -.5);
  border-radius: 50%;
  pointer-events: none;
  background:
    radial-gradient(closest-side, rgba(6, 4, 14, .78), rgba(6, 4, 14, .34) 46%, transparent 72%),
    radial-gradient(closest-side, color-mix(in srgb, var(--scene-light) 55%, transparent), transparent 100%);
}
.story-visual.has-scene .scene-ground::before { /* wide, faint pool of the scene's light */
  content: '';
  position: absolute;
  inset: -55% -50%;
  border-radius: 50%;
  background: radial-gradient(closest-side, var(--scene-glow), transparent);
  opacity: .26;
}
.story-visual.has-scene .scene-ground::after { /* tight contact line where the body meets the floor */
  content: '';
  position: absolute;
  left: 18%;
  right: 18%;
  top: 40%;
  height: 22%;
  border-radius: 50%;
  background: radial-gradient(closest-side, rgba(4, 2, 10, .85), transparent);
}

/* ---------- Air in front of the lower body ---------- */
.story-visual.has-scene .scene-haze {
  display: block;
  position: absolute;
  z-index: 3;
  left: -6%;
  right: -6%;
  top: calc(var(--ground-y, 85%) - var(--ground-w, 160px) * .42);
  height: calc(var(--ground-w, 160px) * .62);
  pointer-events: none;
  background: radial-gradient(ellipse 60% 50% at 50% 70%, color-mix(in srgb, var(--scene-light) 22%, rgba(30, 22, 48, .0)) 0%, transparent 100%),
    linear-gradient(to top, color-mix(in srgb, var(--scene-shade, #120c1e) 38%, transparent), transparent 70%);
  opacity: .9;
}

/* ---------- Foreground motes (closest layer, moves the most) ---------- */
.story-visual.has-scene.scene-alive .scene-fg { /* after first paint (scene.js), keeps the opening tap light */
  display: block;
  position: absolute;
  inset: -4%;
  z-index: 4;
  pointer-events: none;
  will-change: transform;
}
.story-visual.has-scene .scene-fg i {
  position: absolute;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: radial-gradient(closest-side, color-mix(in srgb, var(--scene-glow) 70%, #fff), transparent);
  opacity: 0;
  animation: scene-mote 9s ease-in-out infinite;
}
.scene-fg i:nth-child(1) { left: 12%; top: 70%; animation-delay: -1s; }
.scene-fg i:nth-child(2) { left: 26%; top: 38%; width: 4px; height: 4px; animation-delay: -4s; animation-duration: 11s; }
.scene-fg i:nth-child(3) { left: 78%; top: 62%; width: 9px; height: 9px; animation-delay: -2.5s; animation-duration: 12s; filter: none; }
.scene-fg i:nth-child(4) { left: 86%; top: 28%; animation-delay: -6s; }
.scene-fg i:nth-child(5) { left: 55%; top: 82%; width: 5px; height: 5px; animation-delay: -7.5s; animation-duration: 10s; }
.scene-fg i:nth-child(6) { left: 6%; top: 22%; width: 4px; height: 4px; animation-delay: -3.2s; animation-duration: 13s; }
.scene-fg i:nth-child(7) { left: 64%; top: 14%; width: 7px; height: 7px; animation-delay: -8.4s; animation-duration: 14s; }
@keyframes scene-mote {
  0%   { opacity: 0; transform: translate3d(0, 10px, 0); }
  30%  { opacity: .85; }
  70%  { opacity: .55; }
  100% { opacity: 0; transform: translate3d(14px, -34px, 0); }
}

/* ---------- Landing sparkles (inline SVG stars) ---------- */
.story-visual.has-scene .scene-sparkles {
  display: block;
  position: absolute;
  z-index: 4;
  left: var(--ground-x, 50%);
  top: calc(var(--ground-y, 85%) - var(--ground-w, 160px) * .9);
  width: 0;
  height: 0;
  pointer-events: none;
  color: var(--scene-glow);
}
.story-visual.has-scene .scene-sparkles i {
  position: absolute;
  display: block;
  width: 18px;
  height: 18px;
  opacity: 0;
  filter: drop-shadow(0 0 4px rgba(255, 245, 225, .8));
}
.scene-sparkles i svg { display: block; width: 100%; height: 100%; }
.scene-sparkles i:nth-child(1) { left: calc(var(--ground-w, 160px) * -.58); top: 10px; }
.scene-sparkles i:nth-child(2) { left: calc(var(--ground-w, 160px) * .45); top: -18px; width: 22px; height: 22px; }
.scene-sparkles i:nth-child(3) { left: calc(var(--ground-w, 160px) * .58); top: 40px; width: 11px; height: 11px; }
.scene-sparkles i:nth-child(4) { left: calc(var(--ground-w, 160px) * -.4); top: -26px; width: 24px; height: 24px; }

/* ---------- Entrance + idle (on the body, so cutout + light layers move together) ---------- */
.story-visual.has-scene.scene-enter .scene-body {
  animation: scene-drop .72s cubic-bezier(.3, .7, .4, 1) both, scene-bob 4.8s ease-in-out .72s infinite;
}
.story-visual.has-scene.scene-enter .scene-ground {
  animation: scene-ground-in .72s cubic-bezier(.3, .7, .4, 1) both, scene-ground-bob 4.8s ease-in-out .72s infinite;
}
.story-visual.has-scene.scene-enter .scene-sparkles i { animation: scene-twinkle .9s ease-out .3s both; }
.story-visual.has-scene.scene-enter .scene-sparkles i:nth-child(2) { animation-delay: .38s; }
.story-visual.has-scene.scene-enter .scene-sparkles i:nth-child(3) { animation-delay: .46s; }
.story-visual.has-scene.scene-enter .scene-sparkles i:nth-child(4) { animation-delay: .52s; }

@keyframes scene-fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes scene-drop {
  0%   { opacity: 0; transform: translateY(-38px) scale(.93, 1.07); }
  44%  { opacity: 1; transform: translateY(0) scale(1.08, .9); }
  64%  { transform: translateY(-7px) scale(.97, 1.04); }
  82%  { transform: translateY(0) scale(1.02, .985); }
  100% { opacity: 1; transform: none; }
}
@keyframes scene-bob {
  0%, 100% { transform: translateY(0); }
  50%      { transform: translateY(-6px); }
}
@keyframes scene-ground-in {
  0%   { opacity: 0; transform: scale(.35); }
  44%  { opacity: 1; transform: scale(1.12, 1.05); }
  100% { opacity: 1; transform: none; }
}
@keyframes scene-ground-bob {
  0%, 100% { transform: none; opacity: 1; }
  50%      { transform: scale(.92); opacity: .82; }
}
@keyframes scene-twinkle {
  0%   { opacity: 0; transform: scale(.2) rotate(-30deg); }
  40%  { opacity: 1; transform: scale(1.15) rotate(0); }
  100% { opacity: 0; transform: scale(.7) translateY(-10px) rotate(20deg); }
}

/* ---------- Desktop / tablet: the stage stays in view while the story scrolls ---------- */
@media (min-width: 681px) {
  .story-visual.has-scene {
    position: sticky;
    top: 0;
    align-self: start;
    align-items: center;
    height: calc(90vh - 2px);
    height: calc(90dvh - 2px);
    min-height: 540px;
  }
  #story-dialog .story-visual.has-scene #story-image { position: relative; top: auto; margin-top: 0; }
}

/* ---------- Phones (320–680) ---------- */
@media (max-width: 680px) {
  .story-visual.has-scene { min-height: 300px; max-height: none; height: 330px; }
  .story-visual.has-scene .story-stamp { left: 18px; right: 18px; text-align: left; }
}

/* ---------- Reduced motion: static picture, just a fade ---------- */
@media (prefers-reduced-motion: reduce) {
  .story-visual.has-scene.scene-enter .scene-body { animation: scene-fade .35s ease-out both; }
  .story-visual.has-scene.scene-enter .scene-ground { animation: scene-fade .35s ease-out both; }
  .story-visual.has-scene .scene-sparkles { display: none; }
  .story-visual.has-scene .scene-fg i { animation: none; opacity: .5; }
  .story-visual.has-scene .scene-backdrop,
  .story-visual.has-scene .scene-actor,
  .story-visual.has-scene .scene-fg { will-change: auto; }
}

/* ---------- Mini portrait: narration playing + picture scrolled away (phones) ---------- */
.scene-mini {
  position: fixed;
  z-index: 6;
  left: calc(14px + env(safe-area-inset-left, 0px));
  bottom: calc(16px + env(safe-area-inset-bottom, 0px));
  width: 64px;
  height: 64px;
  padding: 0;
  border: 2px solid color-mix(in srgb, var(--scene-glow, #f8dda8) 70%, #fff8ec);
  border-radius: 50%;
  overflow: hidden;
  background:
    radial-gradient(ellipse 60% 18% at 50% 92%, rgba(8, 5, 18, .55), transparent 70%),
    var(--scene-image, none) center 60% / cover no-repeat,
    #1d1828;
  box-shadow: 0 6px 16px rgba(14, 9, 24, .38), 0 0 0 4px rgba(255, 248, 236, .55);
  touch-action: manipulation;
  animation: scene-mini-in .32s cubic-bezier(.3, .7, .4, 1) both;
}
.scene-mini img { display: block; width: 100%; height: 100%; padding: 7px 6px 3px; object-fit: contain; object-position: 50% 100%; }
.scene-mini:focus-visible { outline: 3px solid #f8dda8; outline-offset: 3px; }
@keyframes scene-mini-in { from { opacity: 0; transform: translateY(10px) scale(.85); } to { opacity: 1; transform: none; } }
@media (prefers-reduced-motion: reduce) { .scene-mini { animation: none; } }

/* ---------- Challenge-arcade header portrait: same place, tiny diorama ---------- */
.challenge-arcade .ca-portrait.scene-portrait {
  padding: 5px 5px 7px;
  background:
    radial-gradient(ellipse 58% 16% at 50% 90%, rgba(8, 5, 18, .5), transparent 70%),
    radial-gradient(ellipse 80% 26% at 50% 90%, var(--scene-light), transparent 72%),
    var(--scene-image) center / cover no-repeat,
    #1d1828;
  box-shadow: inset 0 0 0 1.5px var(--scene-light), inset 0 0 14px rgba(10, 6, 20, .55);
  filter: drop-shadow(0 4px 6px rgba(20, 12, 30, .25));
}
