// scenes.jsx — Emmerske Efterskole tidslinje
// Ungdommelig tone, men med arkiv-æstetik: mørk baggrund, gyldne accenter.

const COLORS = {
  bg: '#141210',
  bgAlt: '#1c1916',
  paper: '#f4ead7',
  ink: '#0f0d0b',
  gold: '#d4a853',
  goldSoft: '#e8c37a',
  rust: '#c15a3a',
  sage: '#7a8b6a',
  muted: 'rgba(244, 234, 215, 0.55)',
  mutedMore: 'rgba(244, 234, 215, 0.3)',
  line: 'rgba(212, 168, 83, 0.35)',
};

const FONTS = {
  serif: "'Cormorant Garamond', 'EB Garamond', Georgia, serif",
  sans: "'Space Grotesk', 'Inter', system-ui, sans-serif",
  mono: "'JetBrains Mono', ui-monospace, monospace",
  display: "'Fraunces', 'Cormorant Garamond', Georgia, serif",
};

// ─── Shared UI bits ─────────────────────────────────────────────────────────

function GrainOverlay() {
  return (
    <div style={{
      position: 'absolute', inset: 0, pointerEvents: 'none',
      backgroundImage: `url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='200' height='200'><filter id='n'><feTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2'/><feColorMatrix values='0 0 0 0 1  0 0 0 0 0.92  0 0 0 0 0.75  0 0 0 0.35 0'/></filter><rect width='200' height='200' filter='url(%23n)'/></svg>")`,
      opacity: 0.08,
      mixBlendMode: 'overlay',
      zIndex: 10,
    }} />
  );
}

function Vignette() {
  return (
    <div style={{
      position: 'absolute', inset: 0, pointerEvents: 'none',
      background: 'radial-gradient(ellipse at center, transparent 40%, rgba(0,0,0,0.55) 100%)',
      zIndex: 9,
    }} />
  );
}

// Persistent timeline strip bottom — always visible, progresses through the whole video.
function TimelineStrip({ totalDuration, marks }) {
  const time = useTime();
  const progress = clamp(time / totalDuration, 0, 1);

  return (
    <div style={{
      position: 'absolute',
      left: 80, right: 80, bottom: 50,
      height: 40,
      zIndex: 5,
    }}>
      {/* Labels row */}
      <div style={{ position: 'relative', height: 18 }}>
        {marks.map((m, i) => {
          const x = m.pos * 100;
          const active = time >= m.activateAt;
          return (
            <div key={i} style={{
              position: 'absolute',
              left: `${x}%`,
              transform: 'translateX(-50%)',
              fontFamily: FONTS.mono,
              fontSize: 11,
              letterSpacing: '0.12em',
              color: active ? COLORS.gold : COLORS.mutedMore,
              transition: 'color 400ms',
              whiteSpace: 'nowrap',
            }}>
              {m.year}
            </div>
          );
        })}
      </div>

      {/* Track */}
      <div style={{ position: 'relative', height: 22, display: 'flex', alignItems: 'center' }}>
        {/* Base line */}
        <div style={{
          position: 'absolute', left: 0, right: 0, height: 1,
          background: 'rgba(212, 168, 83, 0.18)',
        }} />
        {/* Progress line */}
        <div style={{
          position: 'absolute', left: 0, width: `${progress * 100}%`, height: 1,
          background: COLORS.gold,
          boxShadow: '0 0 12px rgba(212,168,83,0.6)',
        }} />

        {/* Dots */}
        {marks.map((m, i) => {
          const x = m.pos * 100;
          const active = time >= m.activateAt;
          const isPast = time >= m.activateAt + 0.5;
          return (
            <div key={i} style={{
              position: 'absolute',
              left: `${x}%`,
              width: 10, height: 10,
              marginLeft: -5,
              borderRadius: '50%',
              background: isPast ? COLORS.gold : COLORS.bg,
              border: `1px solid ${active ? COLORS.gold : 'rgba(212, 168, 83, 0.35)'}`,
              transition: 'background 400ms, border-color 400ms, transform 400ms',
              transform: active && time < m.activateAt + 0.6 ? 'scale(1.6)' : 'scale(1)',
              boxShadow: active ? '0 0 16px rgba(212,168,83,0.7)' : 'none',
            }} />
          );
        })}

        {/* Playhead */}
        <div style={{
          position: 'absolute',
          left: `${progress * 100}%`,
          width: 2, height: 22,
          marginLeft: -1,
          background: COLORS.paper,
          boxShadow: '0 0 8px rgba(244,234,215,0.5)',
        }} />
      </div>
    </div>
  );
}

// Corner metadata block — date + chapter, always present
function ChapterMeta({ chapter, year, subtitle }) {
  return (
    <div style={{
      position: 'absolute',
      top: 50, left: 80,
      zIndex: 6,
      fontFamily: FONTS.mono,
      fontSize: 12,
      letterSpacing: '0.2em',
      textTransform: 'uppercase',
      color: COLORS.gold,
      display: 'flex', alignItems: 'center', gap: 14,
    }}>
      <div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
        <div style={{ width: 24, height: 1, background: COLORS.gold }} />
        <span>{chapter}</span>
      </div>
      <div style={{ color: COLORS.mutedMore }}>·</div>
      <div style={{ color: COLORS.paper, fontSize: 13 }}>{year}</div>
      {subtitle && (
        <>
          <div style={{ color: COLORS.mutedMore }}>·</div>
          <div style={{ color: COLORS.muted, textTransform: 'none', letterSpacing: '0.05em', fontFamily: FONTS.sans }}>{subtitle}</div>
        </>
      )}
    </div>
  );
}

// Title with animated entry: serif display, word-by-word fade/slide-up
function AnimatedHeadline({ text, y = 360, x = 80, size = 120, color = COLORS.paper, italic = false, delay = 0, startTime = 0 }) {
  const { localTime } = useSprite();
  const t = localTime - delay;
  const words = text.split(' ');

  return (
    <div style={{
      position: 'absolute',
      left: x, top: y,
      fontFamily: FONTS.display,
      fontSize: size,
      fontWeight: 400,
      fontStyle: italic ? 'italic' : 'normal',
      color,
      lineHeight: 0.95,
      letterSpacing: '-0.02em',
      maxWidth: 1400,
    }}>
      {words.map((w, i) => {
        const wDelay = i * 0.08;
        const wt = clamp((t - wDelay) / 0.6, 0, 1);
        const eased = Easing.easeOutCubic(wt);
        return (
          <span key={i} style={{
            display: 'inline-block',
            opacity: eased,
            transform: `translateY(${(1 - eased) * 24}px)`,
            marginRight: '0.3em',
            willChange: 'transform, opacity',
          }}>
            {w}
          </span>
        );
      })}
    </div>
  );
}

// ─── SCENE 1: Opening / Title card ──────────────────────────────────────────

function SceneOpening() {
  const { localTime, progress } = useSprite();
  const bgShift = Easing.easeOutCubic(clamp(localTime / 2, 0, 1));

  return (
    <>
      {/* Background building silhouette SVG */}
      <div style={{
        position: 'absolute', inset: 0,
        opacity: 0.12 + bgShift * 0.08,
        transform: `scale(${1 + bgShift * 0.04})`,
      }}>
        <BuildingSilhouette />
      </div>

      {/* Eyebrow */}
      <div style={{
        position: 'absolute',
        left: '50%', top: 280,
        transform: `translateX(-50%) translateY(${(1 - Easing.easeOutCubic(clamp(localTime / 0.8, 0, 1))) * 20}px)`,
        opacity: Easing.easeOutCubic(clamp(localTime / 0.8, 0, 1)),
        fontFamily: FONTS.mono,
        fontSize: 14,
        letterSpacing: '0.4em',
        textTransform: 'uppercase',
        color: COLORS.gold,
        display: 'flex', alignItems: 'center', gap: 20,
      }}>
        <div style={{ width: 60, height: 1, background: COLORS.gold }} />
        <span>Emmerske, Tønder</span>
        <div style={{ width: 60, height: 1, background: COLORS.gold }} />
      </div>

      {/* Big title */}
      <div style={{
        position: 'absolute',
        left: 0, right: 0, top: 340,
        textAlign: 'center',
        fontFamily: FONTS.display,
        fontSize: 180,
        fontWeight: 400,
        color: COLORS.paper,
        lineHeight: 0.9,
        letterSpacing: '-0.03em',
      }}>
        <div style={{
          opacity: Easing.easeOutCubic(clamp((localTime - 0.3) / 1.0, 0, 1)),
          transform: `translateY(${(1 - Easing.easeOutCubic(clamp((localTime - 0.3) / 1.0, 0, 1))) * 30}px)`,
        }}>
          Et hus, et kald,
        </div>
        <div style={{
          fontStyle: 'italic',
          color: COLORS.gold,
          opacity: Easing.easeOutCubic(clamp((localTime - 0.8) / 1.0, 0, 1)),
          transform: `translateY(${(1 - Easing.easeOutCubic(clamp((localTime - 0.8) / 1.0, 0, 1))) * 30}px)`,
        }}>
          en skole.
        </div>
      </div>

      {/* Subtitle */}
      <div style={{
        position: 'absolute',
        left: 0, right: 0, top: 720,
        textAlign: 'center',
        fontFamily: FONTS.sans,
        fontSize: 22,
        letterSpacing: '0.08em',
        color: COLORS.muted,
        textTransform: 'uppercase',
        opacity: Easing.easeOutCubic(clamp((localTime - 1.6) / 0.8, 0, 1)),
      }}>
        Tidslinje over Emmerske Bedehus & Efterskole
      </div>

      {/* Date range */}
      <div style={{
        position: 'absolute',
        left: 0, right: 0, top: 800,
        textAlign: 'center',
        fontFamily: FONTS.display,
        fontSize: 56,
        fontStyle: 'italic',
        color: COLORS.gold,
        letterSpacing: '0.02em',
        opacity: Easing.easeOutCubic(clamp((localTime - 2.0) / 0.8, 0, 1)),
      }}>
        1542 <span style={{ color: COLORS.mutedMore, margin: '0 20px' }}>—</span> i dag
      </div>
    </>
  );
}

function BuildingSilhouette() {
  // Hand-drawn SVG of the Emmerske schoolhouse / bedehus
  return (
    <svg viewBox="0 0 1920 1080" style={{ width: '100%', height: '100%' }}>
      {/* Ground line */}
      <line x1="0" y1="820" x2="1920" y2="820" stroke={COLORS.gold} strokeWidth="1" opacity="0.4" />

      {/* Main building - long house */}
      <g fill="none" stroke={COLORS.gold} strokeWidth="1.5" opacity="0.9">
        {/* Roof - thatched, peaked */}
        <path d="M 420 820 L 420 580 L 500 480 L 1420 480 L 1500 580 L 1500 820 Z" />
        {/* Roof thatch lines */}
        <path d="M 420 580 L 1500 580" />
        <path d="M 460 530 L 1460 530" opacity="0.5" />
        <path d="M 500 480 L 1420 480" opacity="0.6" />

        {/* Windows - 6 small sprossed */}
        {[0, 1, 2, 3, 4, 5].map(i => {
          const x = 520 + i * 145;
          return (
            <g key={i}>
              <rect x={x} y="650" width="80" height="100" />
              <line x1={x + 40} y1="650" x2={x + 40} y2="750" />
              <line x1={x} y1="700" x2={x + 80} y2="700" />
            </g>
          );
        })}

        {/* Door */}
        <rect x="920" y="680" width="80" height="140" />
        <circle cx="985" cy="750" r="2" fill={COLORS.gold} />

        {/* Chimney */}
        <rect x="700" y="440" width="40" height="80" />

        {/* Side wing */}
        <path d="M 1500 820 L 1500 660 L 1560 620 L 1720 620 L 1720 820" />

        {/* Klokkestabel - freestanding bell tower */}
        <g transform="translate(280, 620)">
          <line x1="0" y1="200" x2="0" y2="0" />
          <line x1="60" y1="200" x2="60" y2="0" />
          <line x1="0" y1="0" x2="60" y2="0" />
          <path d="M -10 0 L 30 -40 L 70 0 Z" />
          <rect x="15" y="40" width="30" height="40" />
        </g>
      </g>
    </svg>
  );
}

// ─── SCENE 2: 1542 — det forsvundne kapel ──────────────────────────────────

function Scene1542() {
  const { localTime } = useSprite();

  return (
    <>
      <ChapterMeta chapter="Kapitel I" year="1542" subtitle="Det forsvundne kapel" />

      {/* Ruins SVG */}
      <div style={{
        position: 'absolute',
        left: 120, top: 200,
        width: 700, height: 600,
        opacity: Easing.easeOutCubic(clamp(localTime / 1.0, 0, 1)),
      }}>
        <svg viewBox="0 0 700 600" style={{ width: '100%', height: '100%' }}>
          {/* Dashed outline of a ruined chapel */}
          <g fill="none" stroke={COLORS.gold} strokeWidth="1.5" strokeDasharray="6 8" opacity="0.7">
            <path d="M 100 500 L 100 250 L 150 200 L 550 200 L 600 250 L 600 500 Z" />
            <rect x="320" y="360" width="60" height="140" />
            <path d="M 100 250 L 600 250" strokeDasharray="0" opacity="0.3" />
          </g>
          {/* Scattered stones */}
          <g fill={COLORS.gold} opacity="0.4">
            <circle cx="80" cy="520" r="8" />
            <circle cx="620" cy="530" r="10" />
            <circle cx="350" cy="540" r="6" />
            <circle cx="200" cy="525" r="7" />
            <circle cx="480" cy="535" r="9" />
          </g>
          {/* Year stamp */}
          <text x="350" y="340" textAnchor="middle" fontFamily={FONTS.display} fontSize="140" fontStyle="italic" fill={COLORS.gold} opacity="0.2">
            1542
          </text>
        </svg>
      </div>

      {/* Right side: text */}
      <div style={{
        position: 'absolute',
        right: 80, top: 280,
        width: 760,
      }}>
        <AnimatedHeadline text="Kapellet rives ned." y={0} x={0} size={96} />
        <div style={{
          marginTop: 260,
          fontFamily: FONTS.serif,
          fontSize: 28,
          lineHeight: 1.5,
          color: COLORS.muted,
          maxWidth: 680,
          opacity: Easing.easeOutCubic(clamp((localTime - 1.4) / 0.8, 0, 1)),
          transform: `translateY(${(1 - Easing.easeOutCubic(clamp((localTime - 1.4) / 0.8, 0, 1))) * 16}px)`,
        }}>
          Efter kongeligt dekret forsvinder det middelalderlige kapel i Emmerske.
          Stedet står tomt. Men landsbyen venter — snart vender det hellige tilbage,
          denne gang med <em style={{ color: COLORS.goldSoft }}>bøger</em> ved siden af <em style={{ color: COLORS.goldSoft }}>bønnen</em>.
        </div>
      </div>
    </>
  );
}

// ─── SCENE 3: 1729 — Brorson kaldes ─────────────────────────────────────────

function Scene1729() {
  const { localTime } = useSprite();
  const t = localTime;

  return (
    <>
      <ChapterMeta chapter="Kapitel II" year="1729" subtitle="Pietismens stemme" />

      {/* Left: portrait placeholder — Brorson */}
      <div style={{
        position: 'absolute',
        left: 140, top: 220,
        width: 460, height: 580,
        opacity: Easing.easeOutCubic(clamp(t / 0.8, 0, 1)),
        transform: `translateY(${(1 - Easing.easeOutCubic(clamp(t / 0.8, 0, 1))) * 30}px)`,
      }}>
        <div style={{
          width: '100%', height: '100%',
          border: `1px solid ${COLORS.line}`,
          background: `linear-gradient(135deg, ${COLORS.bgAlt} 0%, #252017 100%)`,
          position: 'relative',
          display: 'flex', alignItems: 'center', justifyContent: 'center',
        }}>
          {/* Silhouette portrait */}
          <svg viewBox="0 0 200 260" style={{ width: '70%', height: '80%', opacity: 0.6 }}>
            <ellipse cx="100" cy="70" rx="42" ry="52" fill="none" stroke={COLORS.gold} strokeWidth="1.5" />
            <path d="M 40 260 Q 40 140 100 130 Q 160 140 160 260 Z" fill="none" stroke={COLORS.gold} strokeWidth="1.5" />
            {/* Collar */}
            <path d="M 80 180 L 100 170 L 120 180 L 115 200 L 85 200 Z" fill="none" stroke={COLORS.gold} strokeWidth="1.5" />
          </svg>
          {/* Placeholder tag */}
          <div style={{
            position: 'absolute', bottom: 20, left: 20,
            fontFamily: FONTS.mono, fontSize: 10,
            color: COLORS.mutedMore, letterSpacing: '0.2em',
          }}>[ PORTRÆT · PLADSHOLDER ]</div>
        </div>
        {/* Caption */}
        <div style={{
          marginTop: 16,
          fontFamily: FONTS.sans, fontSize: 13,
          color: COLORS.muted,
          letterSpacing: '0.1em', textTransform: 'uppercase',
        }}>
          Hans Adolph Brorson <span style={{ color: COLORS.mutedMore }}>· 1694–1764</span>
        </div>
      </div>

      {/* Right: text */}
      <div style={{ position: 'absolute', right: 80, top: 230, width: 1020 }}>
        <div style={{
          fontFamily: FONTS.display, fontSize: 180, fontStyle: 'italic',
          color: COLORS.gold, lineHeight: 0.8,
          opacity: Easing.easeOutCubic(clamp((t - 0.2) / 0.8, 0, 1)),
          transform: `translateY(${(1 - Easing.easeOutCubic(clamp((t - 0.2) / 0.8, 0, 1))) * 30}px)`,
        }}>
          1729
        </div>

        <div style={{ marginTop: 30 }}>
          <AnimatedHeadline text="Den danske trediepræst." y={0} x={0} size={78} delay={0.8} />
        </div>

        <div style={{
          marginTop: 200,
          fontFamily: FONTS.serif,
          fontSize: 28,
          lineHeight: 1.5,
          color: COLORS.muted,
          maxWidth: 900,
          opacity: Easing.easeOutCubic(clamp((t - 2.0) / 0.8, 0, 1)),
          transform: `translateY(${(1 - Easing.easeOutCubic(clamp((t - 2.0) / 0.8, 0, 1))) * 16}px)`,
        }}>
          Provst <strong style={{ color: COLORS.paper, fontWeight: 500 }}>Schrader</strong> og amtmand <strong style={{ color: COLORS.paper, fontWeight: 500 }}>Holstein</strong> opretter
          et nyt embede i Tønder. Det besættes af en ung pietist og salmedigter — <br/>
          <em style={{ color: COLORS.goldSoft }}>Hans Adolph Brorson</em>.
        </div>
      </div>
    </>
  );
}

// ─── SCENE 4: 1730 — bygningen rejses ───────────────────────────────────────

function Scene1730() {
  const { localTime } = useSprite();
  const t = localTime;
  // Build animation: roof drops in, walls rise, details
  const wallProg = Easing.easeOutCubic(clamp((t - 0.5) / 1.2, 0, 1));
  const roofProg = Easing.easeOutCubic(clamp((t - 1.4) / 0.8, 0, 1));
  const windowProg = Easing.easeOutCubic(clamp((t - 2.0) / 0.6, 0, 1));
  const stoneProg = Easing.easeOutCubic(clamp((t - 2.6) / 0.6, 0, 1));

  return (
    <>
      <ChapterMeta chapter="Kapitel III" year="1730" subtitle="Huset rejses" />

      {/* Large headline left */}
      <div style={{ position: 'absolute', left: 80, top: 200, width: 720 }}>
        <div style={{
          fontFamily: FONTS.mono, fontSize: 13,
          color: COLORS.gold, letterSpacing: '0.3em',
          opacity: Easing.easeOutCubic(clamp(t / 0.6, 0, 1)),
        }}>
          SKOLE · BEDEHUS · LANDSBYENS HJERTE
        </div>
        <div style={{ marginTop: 30 }}>
          <AnimatedHeadline text="Emmerske" y={0} x={0} size={130} delay={0.1} />
          <AnimatedHeadline text="Bedehus." y={130} x={0} size={130} color={COLORS.gold} italic delay={0.4} />
        </div>

        {/* Stone tablet inscription */}
        <div style={{
          marginTop: 380,
          opacity: Easing.easeOutCubic(clamp((t - 2.8) / 0.8, 0, 1)),
          transform: `translateY(${(1 - Easing.easeOutCubic(clamp((t - 2.8) / 0.8, 0, 1))) * 16}px)`,
        }}>
          <div style={{
            border: `1px solid ${COLORS.line}`,
            padding: '24px 32px',
            maxWidth: 620,
            background: 'rgba(212, 168, 83, 0.04)',
          }}>
            <div style={{
              fontFamily: FONTS.mono, fontSize: 11,
              color: COLORS.gold, letterSpacing: '0.25em',
              marginBottom: 12,
            }}>INDSKRIFT PÅ STENTAVLEN</div>
            <div style={{
              fontFamily: FONTS.display, fontSize: 28, fontStyle: 'italic',
              color: COLORS.paper, lineHeight: 1.3,
            }}>
              »Dieses Haus ist … gebaut 1730 …«
            </div>
            <div style={{
              fontFamily: FONTS.serif, fontSize: 18,
              color: COLORS.muted, marginTop: 8,
            }}>
              — indviet til undervisning i <em>Gottseligkeit</em> for alle sognebørn.
            </div>
          </div>
        </div>
      </div>

      {/* Right: animated building rising */}
      <div style={{
        position: 'absolute',
        right: 60, top: 180,
        width: 900, height: 620,
      }}>
        <svg viewBox="0 0 900 620" style={{ width: '100%', height: '100%' }}>
          {/* Ground */}
          <line x1="0" y1="560" x2="900" y2="560" stroke={COLORS.gold} strokeWidth="1" opacity="0.5" />

          {/* Walls rising from ground */}
          <g>
            <clipPath id="wallClip">
              <rect x="0" y={560 - 160 * wallProg} width="900" height="200" />
            </clipPath>
            <g clipPath="url(#wallClip)" fill="none" stroke={COLORS.paper} strokeWidth="1.5">
              <rect x="120" y="400" width="660" height="160" />
              {/* Side wing */}
              <rect x="780" y="440" width="90" height="120" />
            </g>
          </g>

          {/* Roof drops in from top */}
          <g style={{
            opacity: roofProg,
            transform: `translateY(${(1 - roofProg) * -80}px)`,
            transformOrigin: 'center',
          }}>
            <g fill="none" stroke={COLORS.gold} strokeWidth="1.5">
              <path d="M 120 400 L 200 320 L 700 320 L 780 400 Z" />
              {/* Thatch lines */}
              <path d="M 150 370 L 750 370" opacity="0.5" />
              <path d="M 180 340 L 720 340" opacity="0.4" />
              {/* Side wing roof */}
              <path d="M 780 440 L 810 400 L 840 400 L 870 440" />
              {/* Chimney */}
              <rect x="380" y="290" width="24" height="50" fill={COLORS.bg} />
            </g>
          </g>

          {/* Windows appear */}
          <g style={{ opacity: windowProg }}>
            {[0, 1, 2, 3, 4].map(i => {
              const x = 170 + i * 110;
              if (i === 2) return null; // skip for door
              return (
                <g key={i} fill="none" stroke={COLORS.gold} strokeWidth="1">
                  <rect x={x} y="450" width="60" height="80" />
                  <line x1={x + 30} y1="450" x2={x + 30} y2="530" />
                  <line x1={x} y1="490" x2={x + 60} y2="490" />
                </g>
              );
            })}
            {/* Door */}
            <g fill="none" stroke={COLORS.gold} strokeWidth="1.5">
              <rect x="390" y="470" width="60" height="90" />
              <circle cx="438" cy="520" r="1.5" fill={COLORS.gold} />
            </g>
          </g>

          {/* Freestanding bell tower */}
          <g style={{
            opacity: stoneProg,
            transform: `translateY(${(1 - stoneProg) * 20}px)`,
          }}>
            <g transform="translate(40, 400)" fill="none" stroke={COLORS.gold} strokeWidth="1.2">
              <line x1="0" y1="160" x2="0" y2="10" />
              <line x1="50" y1="160" x2="50" y2="10" />
              <path d="M -10 10 L 25 -25 L 60 10 Z" />
              <rect x="15" y="30" width="20" height="30" />
            </g>
          </g>

          {/* Year 1730 watermark */}
          <text x="450" y="610" textAnchor="middle"
            fontFamily={FONTS.mono} fontSize="10" fill={COLORS.mutedMore} letterSpacing="8">
            ANNO MDCCXXX
          </text>
        </svg>
      </div>
    </>
  );
}

// ─── SCENE 5: 1730s — Brorsons salmer ───────────────────────────────────────

function SceneBrorsonSong() {
  const { localTime } = useSprite();
  const t = localTime;

  return (
    <>
      <ChapterMeta chapter="Kapitel IV" year="1732" subtitle="Den første julesalme" />

      {/* Ambient musical notes floating */}
      <FloatingNotes />

      <div style={{
        position: 'absolute',
        left: 0, right: 0, top: 280,
        textAlign: 'center',
      }}>
        <div style={{
          fontFamily: FONTS.mono, fontSize: 13,
          color: COLORS.gold, letterSpacing: '0.35em',
          opacity: Easing.easeOutCubic(clamp(t / 0.6, 0, 1)),
        }}>
          FØRSTE GANG SUNGET I EMMERSKE
        </div>

        <div style={{ marginTop: 60, position: 'relative', height: 300 }}>
          <div style={{
            fontFamily: FONTS.display, fontSize: 144, fontStyle: 'italic',
            color: COLORS.paper, lineHeight: 1.0,
            letterSpacing: '-0.02em',
            opacity: Easing.easeOutCubic(clamp((t - 0.4) / 1.0, 0, 1)),
            transform: `translateY(${(1 - Easing.easeOutCubic(clamp((t - 0.4) / 1.0, 0, 1))) * 30}px)`,
          }}>
            »Her kommer, Jesus,
          </div>
          <div style={{
            fontFamily: FONTS.display, fontSize: 144, fontStyle: 'italic',
            color: COLORS.gold, lineHeight: 1.0,
            letterSpacing: '-0.02em',
            marginTop: 10,
            opacity: Easing.easeOutCubic(clamp((t - 1.2) / 1.0, 0, 1)),
            transform: `translateY(${(1 - Easing.easeOutCubic(clamp((t - 1.2) / 1.0, 0, 1))) * 30}px)`,
          }}>
            dine små«
          </div>
        </div>

        <div style={{
          marginTop: 80,
          fontFamily: FONTS.serif, fontSize: 24, fontStyle: 'italic',
          color: COLORS.muted,
          opacity: Easing.easeOutCubic(clamp((t - 2.4) / 0.8, 0, 1)),
        }}>
          — Brorson samler sognebørn søndag eftermiddag. Pietismens salmer
          bliver lyden af Emmerske Bedehus.
        </div>
      </div>
    </>
  );
}

function FloatingNotes() {
  const { localTime } = useSprite();
  const notes = [
    { x: 15, y: 30, delay: 0.2, size: 40 },
    { x: 85, y: 25, delay: 0.5, size: 32 },
    { x: 10, y: 75, delay: 0.8, size: 28 },
    { x: 90, y: 70, delay: 1.1, size: 36 },
    { x: 25, y: 85, delay: 1.4, size: 24 },
    { x: 75, y: 88, delay: 1.7, size: 30 },
  ];

  return (
    <>
      {notes.map((n, i) => {
        const t = localTime - n.delay;
        if (t < 0) return null;
        const drift = Math.sin(t * 0.8 + i) * 8;
        const opacity = Easing.easeOutCubic(clamp(t / 0.8, 0, 1)) * 0.5;
        return (
          <div key={i} style={{
            position: 'absolute',
            left: `${n.x}%`, top: `${n.y}%`,
            fontFamily: FONTS.display,
            fontSize: n.size,
            color: COLORS.gold,
            opacity,
            transform: `translateY(${drift}px) translateY(${-t * 10}px)`,
            willChange: 'transform, opacity',
          }}>
            ♪
          </div>
        );
      })}
    </>
  );
}

// ─── SCENE 6: 1835 — udvidelse ──────────────────────────────────────────────

function Scene1835() {
  const { localTime } = useSprite();
  const t = localTime;
  const extensionProg = Easing.easeOutCubic(clamp((t - 0.8) / 1.0, 0, 1));

  return (
    <>
      <ChapterMeta chapter="Kapitel V" year="1835" subtitle="Huset vokser" />

      {/* Left text */}
      <div style={{ position: 'absolute', left: 80, top: 260, width: 720 }}>
        <div style={{
          fontFamily: FONTS.display, fontSize: 160, fontStyle: 'italic',
          color: COLORS.gold, lineHeight: 0.9,
          opacity: Easing.easeOutCubic(clamp(t / 0.6, 0, 1)),
          transform: `translateY(${(1 - Easing.easeOutCubic(clamp(t / 0.6, 0, 1))) * 24}px)`,
        }}>
          1835
        </div>

        <div style={{
          marginTop: 24,
          fontFamily: FONTS.display, fontSize: 64, color: COLORS.paper,
          lineHeight: 1.1, fontWeight: 400,
          opacity: Easing.easeOutCubic(clamp((t - 0.4) / 0.8, 0, 1)),
        }}>
          Ny skolestue<br/>
          <em style={{ color: COLORS.goldSoft }}>mod øst.</em>
        </div>

        <div style={{
          marginTop: 40,
          fontFamily: FONTS.serif, fontSize: 26, color: COLORS.muted, lineHeight: 1.5, maxWidth: 640,
          opacity: Easing.easeOutCubic(clamp((t - 1.2) / 0.8, 0, 1)),
        }}>
          Hundrede år efter indvielsen vokser huset. Årstallet
          smedes i jern på vestgavlen — og står dér endnu.
        </div>
      </div>

      {/* Right: diagrammatic building with east extension */}
      <div style={{ position: 'absolute', right: 100, top: 260, width: 800, height: 500 }}>
        <svg viewBox="0 0 800 500" style={{ width: '100%', height: '100%' }}>
          {/* Original building */}
          <g fill="none" stroke={COLORS.paper} strokeWidth="1.5" opacity="0.85">
            <rect x="80" y="200" width="400" height="140" />
            <path d="M 80 200 L 160 120 L 400 120 L 480 200" />
            <path d="M 120 170 L 440 170" opacity="0.4" />
            {/* Windows */}
            {[0, 1, 2].map(i => (
              <rect key={i} x={120 + i * 80} y="240" width="40" height="60" />
            ))}
          </g>

          {/* "1730" label on original */}
          <text x="280" y="400" textAnchor="middle"
            fontFamily={FONTS.mono} fontSize="11" fill={COLORS.mutedMore} letterSpacing="4">
            OPRINDELIG · 1730
          </text>

          {/* Extension flying in from right */}
          <g style={{
            opacity: extensionProg,
            transform: `translateX(${(1 - extensionProg) * 100}px)`,
          }}>
            <g fill="none" stroke={COLORS.gold} strokeWidth="1.8">
              <rect x="480" y="220" width="220" height="120" />
              <path d="M 480 220 L 540 160 L 700 160 L 760 220" />
              <path d="M 520 195 L 720 195" opacity="0.4" />
              {[0, 1].map(i => (
                <rect key={i} x={520 + i * 80} y="255" width="40" height="55" />
              ))}
              {/* "1835" stamp */}
              <text x="590" y="305" textAnchor="middle"
                fontFamily={FONTS.display} fontSize="26" fontStyle="italic" fill={COLORS.gold}>
                1835
              </text>
            </g>
            <text x="590" y="400" textAnchor="middle"
              fontFamily={FONTS.mono} fontSize="11" fill={COLORS.gold} letterSpacing="4">
              NY SKOLEFLØJ
            </text>
          </g>

          {/* Dashed connection arrow */}
          <g style={{ opacity: extensionProg * 0.7 }}>
            <line x1="485" y1="270" x2="475" y2="270" stroke={COLORS.gold} strokeWidth="1" strokeDasharray="3 3" />
          </g>
        </svg>
      </div>
    </>
  );
}

// ─── SCENE 7: 1920 — Genforeningen ──────────────────────────────────────────

function Scene1920() {
  const { localTime } = useSprite();
  const t = localTime;
  const flagProg = Easing.easeOutCubic(clamp((t - 0.6) / 1.0, 0, 1));

  return (
    <>
      <ChapterMeta chapter="Kapitel VI" year="1920" subtitle="Genforeningen" />

      {/* Giant year on left */}
      <div style={{
        position: 'absolute', left: 80, top: 220,
        fontFamily: FONTS.display, fontSize: 380,
        fontStyle: 'italic', color: COLORS.gold, lineHeight: 0.85,
        opacity: Easing.easeOutCubic(clamp(t / 0.8, 0, 1)),
        letterSpacing: '-0.04em',
      }}>
        1920
      </div>

      {/* Flag/cross animation right */}
      <div style={{
        position: 'absolute', right: 160, top: 260,
        width: 380, height: 240,
        opacity: flagProg,
      }}>
        <svg viewBox="0 0 380 240" style={{ width: '100%', height: '100%' }}>
          <rect x="0" y="0" width="380" height="240" fill="none" stroke={COLORS.paper} strokeWidth="1" opacity="0.3" />
          {/* Dannebrog cross */}
          <rect x="0" y="100" width="380" height="40" fill={COLORS.rust} opacity="0.9" />
          <rect x="110" y="0" width="40" height="240" fill={COLORS.rust} opacity="0.9" />
        </svg>
      </div>

      {/* Text block */}
      <div style={{
        position: 'absolute', left: 80, top: 680,
        width: 1280,
        opacity: Easing.easeOutCubic(clamp((t - 1.4) / 0.8, 0, 1)),
        transform: `translateY(${(1 - Easing.easeOutCubic(clamp((t - 1.4) / 0.8, 0, 1))) * 20}px)`,
      }}>
        <div style={{
          fontFamily: FONTS.display, fontSize: 72, color: COLORS.paper,
          lineHeight: 1.05, fontWeight: 400,
        }}>
          Emmerske får sit eget <em style={{ color: COLORS.goldSoft }}>kirkedistrikt.</em>
        </div>
        <div style={{
          marginTop: 24,
          fontFamily: FONTS.serif, fontSize: 24, color: COLORS.muted,
          maxWidth: 1100, lineHeight: 1.5,
        }}>
          Efter Genforeningen får bedehuset sit eget menighedsråd. Dansk bliver sognets sprog,
          og Joakim Skovgaards altertavle finder sin plads.
        </div>
      </div>
    </>
  );
}

// ─── SCENE 8: 1943 + 1970 — krig og lukning ─────────────────────────────────

function SceneWarClose() {
  const { localTime } = useSprite();
  const t = localTime;

  return (
    <>
      <ChapterMeta chapter="Kapitel VII" year="1943 → 1970" subtitle="Krig. Stilhed. Tomme stole." />

      {/* Split layout: two half-scenes */}
      {/* 1943 left */}
      <div style={{
        position: 'absolute', left: 80, top: 220, width: 840, height: 680,
        borderRight: `1px solid ${COLORS.line}`,
        paddingRight: 60,
        opacity: Easing.easeOutCubic(clamp(t / 0.8, 0, 1)),
        transform: `translateY(${(1 - Easing.easeOutCubic(clamp(t / 0.8, 0, 1))) * 20}px)`,
      }}>
        <div style={{
          fontFamily: FONTS.display, fontSize: 200, fontStyle: 'italic',
          color: COLORS.gold, lineHeight: 0.85,
        }}>
          1943
        </div>
        <div style={{
          marginTop: 30,
          fontFamily: FONTS.display, fontSize: 56, color: COLORS.paper,
          lineHeight: 1.05,
        }}>
          Nordfløjen<br/>bygges under <em style={{ color: COLORS.goldSoft }}>krigen.</em>
        </div>
        <div style={{
          marginTop: 30,
          fontFamily: FONTS.serif, fontSize: 22, color: COLORS.muted, lineHeight: 1.5,
        }}>
          Selv under besættelsen udvides skolen. En tredje længe rejses mod nord.
          Huset gror videre.
        </div>
      </div>

      {/* 1970 right */}
      <div style={{
        position: 'absolute', right: 80, top: 220, width: 840, height: 680,
        paddingLeft: 60,
        opacity: Easing.easeOutCubic(clamp((t - 1.2) / 0.8, 0, 1)),
        transform: `translateY(${(1 - Easing.easeOutCubic(clamp((t - 1.2) / 0.8, 0, 1))) * 20}px)`,
      }}>
        <div style={{
          fontFamily: FONTS.display, fontSize: 200, fontStyle: 'italic',
          color: COLORS.rust, lineHeight: 0.85,
        }}>
          1970
        </div>
        <div style={{
          marginTop: 30,
          fontFamily: FONTS.display, fontSize: 56, color: COLORS.paper,
          lineHeight: 1.05,
        }}>
          Folkeskolen<br/><em style={{ color: COLORS.rust }}>lukker.</em>
        </div>
        <div style={{
          marginTop: 30,
          fontFamily: FONTS.serif, fontSize: 22, color: COLORS.muted, lineHeight: 1.5,
        }}>
          Efter kommunalreformen er der for få børn. Døren låses.
          Bænkene står tomme. Huset holder vejret i otte år.
        </div>

        {/* 240 years indicator */}
        <div style={{
          marginTop: 60,
          display: 'flex', alignItems: 'baseline', gap: 16,
          fontFamily: FONTS.mono, fontSize: 13, color: COLORS.gold,
          letterSpacing: '0.2em',
          opacity: Easing.easeOutCubic(clamp((t - 2.2) / 0.6, 0, 1)),
        }}>
          <span style={{
            fontFamily: FONTS.display, fontSize: 96, fontStyle: 'italic',
            color: COLORS.gold, letterSpacing: '-0.02em',
          }}>240</span>
          <span>ÅR SOM SKOLE · 1730–1970</span>
        </div>
      </div>
    </>
  );
}

// ─── SCENE 9: 1978 — Efterskolen åbner ──────────────────────────────────────

function Scene1978() {
  const { localTime } = useSprite();
  const t = localTime;

  return (
    <>
      <ChapterMeta chapter="Kapitel VIII" year="1978" subtitle="Dørene åbner igen" />

      {/* Big reveal: year pops first */}
      <div style={{
        position: 'absolute', left: 0, right: 0, top: 280,
        textAlign: 'center',
      }}>
        <div style={{
          fontFamily: FONTS.mono, fontSize: 14,
          color: COLORS.gold, letterSpacing: '0.4em',
          opacity: Easing.easeOutCubic(clamp(t / 0.6, 0, 1)),
        }}>
          EFTER OTTE ÅRS STILHED
        </div>

        <div style={{
          marginTop: 50,
          fontFamily: FONTS.display, fontSize: 320,
          fontStyle: 'italic', color: COLORS.gold,
          letterSpacing: '-0.04em', lineHeight: 0.85,
          opacity: Easing.easeOutBack(clamp((t - 0.4) / 1.0, 0, 1)),
          transform: `scale(${0.7 + 0.3 * Easing.easeOutBack(clamp((t - 0.4) / 1.0, 0, 1))})`,
        }}>
          1978
        </div>

        <div style={{
          marginTop: 40,
          fontFamily: FONTS.display, fontSize: 88, color: COLORS.paper,
          lineHeight: 1.0, letterSpacing: '-0.02em',
          opacity: Easing.easeOutCubic(clamp((t - 1.4) / 0.8, 0, 1)),
          transform: `translateY(${(1 - Easing.easeOutCubic(clamp((t - 1.4) / 0.8, 0, 1))) * 20}px)`,
        }}>
          <em>Emmerske Efterskole</em> åbner.
        </div>

        <div style={{
          marginTop: 30,
          fontFamily: FONTS.serif, fontSize: 26, color: COLORS.muted,
          maxWidth: 1100, margin: '30px auto 0',
          lineHeight: 1.5,
          opacity: Easing.easeOutCubic(clamp((t - 2.0) / 0.8, 0, 1)),
        }}>
          En selvejende efterskole for <span style={{ color: COLORS.goldSoft, fontStyle: 'italic' }}>ordblinde unge</span> flytter ind i de gamle skolelokaler.
          Huset finder en ny grund til at stå op om morgenen.
        </div>
      </div>
    </>
  );
}

// ─── SCENE 10: 2012 — kirken lukker ─────────────────────────────────────────

function Scene2012() {
  const { localTime } = useSprite();
  const t = localTime;

  return (
    <>
      <ChapterMeta chapter="Kapitel IX" year="2012" subtitle="Sidste gudstjeneste" />

      {/* Candle visual left */}
      <div style={{
        position: 'absolute', left: 180, top: 260,
        width: 300, height: 500,
      }}>
        <svg viewBox="0 0 300 500" style={{ width: '100%', height: '100%' }}>
          {/* Candle */}
          <rect x="130" y="200" width="40" height="280" fill="none" stroke={COLORS.paper} strokeWidth="1.2" opacity="0.8" />
          {/* Wick */}
          <line x1="150" y1="200" x2="150" y2="180" stroke={COLORS.paper} strokeWidth="1.5" />
          {/* Flame — pulse */}
          <g style={{
            transform: `translateY(${Math.sin(t * 6) * 2}px) scaleY(${1 + Math.sin(t * 8) * 0.05})`,
            transformOrigin: '150px 180px',
            opacity: Easing.easeOutCubic(clamp(t / 0.8, 0, 1)),
          }}>
            <ellipse cx="150" cy="160" rx="12" ry="24" fill={COLORS.gold} opacity="0.9" />
            <ellipse cx="150" cy="165" rx="7" ry="14" fill={COLORS.goldSoft} />
            <ellipse cx="150" cy="170" rx="3" ry="6" fill={COLORS.paper} />
          </g>
          {/* Glow */}
          <circle cx="150" cy="170" r="80" fill={COLORS.gold} opacity="0.08" />
          <circle cx="150" cy="170" r="140" fill={COLORS.gold} opacity="0.04" />
        </svg>
      </div>

      {/* Right: text */}
      <div style={{ position: 'absolute', right: 80, top: 280, width: 1040 }}>
        <div style={{
          fontFamily: FONTS.display, fontSize: 240, fontStyle: 'italic',
          color: COLORS.paper, lineHeight: 0.85, letterSpacing: '-0.03em',
          opacity: Easing.easeOutCubic(clamp(t / 0.8, 0, 1)),
          transform: `translateY(${(1 - Easing.easeOutCubic(clamp(t / 0.8, 0, 1))) * 30}px)`,
        }}>
          2012
        </div>
        <div style={{
          marginTop: 40,
          fontFamily: FONTS.display, fontSize: 64, color: COLORS.gold,
          lineHeight: 1.05, fontStyle: 'italic',
          opacity: Easing.easeOutCubic(clamp((t - 0.6) / 0.8, 0, 1)),
        }}>
          2. december<br/>
          <span style={{ color: COLORS.paper, fontStyle: 'normal', fontWeight: 400 }}>— den sidste gudstjeneste.</span>
        </div>
        <div style={{
          marginTop: 40,
          fontFamily: FONTS.serif, fontSize: 24, color: COLORS.muted,
          lineHeight: 1.5, maxWidth: 900,
          opacity: Easing.easeOutCubic(clamp((t - 1.4) / 0.8, 0, 1)),
        }}>
          Emmerske sogn lægges sammen med Tønder. Efter 282 år som bedehus
          slukkes alterlysene. Men huset bliver stående — nu helt efterskolens.
        </div>
      </div>
    </>
  );
}

// ─── SCENE 11: I dag ────────────────────────────────────────────────────────

function SceneToday() {
  const { localTime } = useSprite();
  const t = localTime;

  // Rows of "students" dots
  const rows = 6, cols = 22;
  const studentDelay = (r, c) => 0.8 + (r * cols + c) * 0.02;

  return (
    <>
      <ChapterMeta chapter="Kapitel X" year="I dag" subtitle="Huset lever videre" />

      <div style={{ position: 'absolute', left: 80, top: 220, width: 1760 }}>
        <div style={{
          fontFamily: FONTS.mono, fontSize: 14, color: COLORS.gold,
          letterSpacing: '0.4em',
          opacity: Easing.easeOutCubic(clamp(t / 0.6, 0, 1)),
        }}>
          ANNO {new Date().getFullYear()}
        </div>

        <div style={{ marginTop: 30 }}>
          <AnimatedHeadline text="Stråtaget står" y={0} x={0} size={140} delay={0.2} />
          <AnimatedHeadline text="endnu." y={140} x={0} size={140} color={COLORS.gold} italic delay={0.6} />
        </div>

        <div style={{
          marginTop: 340,
          display: 'flex', alignItems: 'flex-start', gap: 80,
        }}>
          {/* Left column: prose */}
          <div style={{
            flex: 1, maxWidth: 640,
            fontFamily: FONTS.serif, fontSize: 24, color: COLORS.muted,
            lineHeight: 1.55,
            opacity: Easing.easeOutCubic(clamp((t - 1.6) / 0.8, 0, 1)),
          }}>
            I 1985 blev bygningerne fredet. I dag fyldes klasseværelser,
            gange og gymnastiksal af <em style={{ color: COLORS.goldSoft }}>unge ordblinde</em>, der
            finder ord, venskaber og selvtillid i de samme sale, Brorson
            engang sang i.
          </div>

          {/* Right column: stat grid — dots representing students */}
          <div style={{
            flex: 1,
            opacity: Easing.easeOutCubic(clamp((t - 0.6) / 0.6, 0, 1)),
          }}>
            <div style={{
              fontFamily: FONTS.mono, fontSize: 11, color: COLORS.gold,
              letterSpacing: '0.25em', marginBottom: 20,
            }}>
              EMMERSKE EFTERSKOLE · NUTID
            </div>

            <div style={{
              display: 'grid',
              gridTemplateColumns: `repeat(${cols}, 1fr)`,
              gap: 6, maxWidth: 520,
            }}>
              {Array.from({ length: rows * cols }, (_, i) => {
                const r = Math.floor(i / cols);
                const c = i % cols;
                const d = studentDelay(r, c);
                const appear = Easing.easeOutBack(clamp((t - d) / 0.4, 0, 1));
                return (
                  <div key={i} style={{
                    width: 12, height: 12,
                    borderRadius: '50%',
                    background: COLORS.gold,
                    opacity: appear * 0.85,
                    transform: `scale(${appear})`,
                  }} />
                );
              })}
            </div>

            <div style={{
              marginTop: 20,
              fontFamily: FONTS.sans, fontSize: 13, color: COLORS.muted,
              letterSpacing: '0.08em',
            }}>
              hver prik er en historie, der endnu ikke er skrevet.
            </div>
          </div>
        </div>
      </div>
    </>
  );
}

// ─── SCENE 12: Outro ────────────────────────────────────────────────────────

function SceneOutro() {
  const { localTime } = useSprite();
  const t = localTime;

  return (
    <>
      {/* Background silhouette */}
      <div style={{ position: 'absolute', inset: 0, opacity: 0.12 }}>
        <BuildingSilhouette />
      </div>

      <div style={{
        position: 'absolute', inset: 0,
        display: 'flex', flexDirection: 'column',
        alignItems: 'center', justifyContent: 'center',
        textAlign: 'center',
      }}>
        <div style={{
          fontFamily: FONTS.mono, fontSize: 14,
          color: COLORS.gold, letterSpacing: '0.4em',
          opacity: Easing.easeOutCubic(clamp(t / 0.8, 0, 1)),
        }}>
          1542 · 1730 · 1835 · 1920 · 1943 · 1970 · 1978 · 2012 · I DAG
        </div>

        <div style={{
          marginTop: 60,
          fontFamily: FONTS.display, fontSize: 140,
          color: COLORS.paper, lineHeight: 0.95,
          letterSpacing: '-0.03em', fontWeight: 400,
          opacity: Easing.easeOutCubic(clamp((t - 0.4) / 1.0, 0, 1)),
          transform: `translateY(${(1 - Easing.easeOutCubic(clamp((t - 0.4) / 1.0, 0, 1))) * 30}px)`,
        }}>
          Næsten <em style={{ color: COLORS.gold }}>300 år</em>
        </div>
        <div style={{
          fontFamily: FONTS.display, fontSize: 100,
          color: COLORS.muted, lineHeight: 1.0, fontStyle: 'italic',
          marginTop: 10,
          opacity: Easing.easeOutCubic(clamp((t - 1.0) / 1.0, 0, 1)),
          transform: `translateY(${(1 - Easing.easeOutCubic(clamp((t - 1.0) / 1.0, 0, 1))) * 30}px)`,
        }}>
          under det samme tag.
        </div>

        <div style={{
          marginTop: 80,
          display: 'flex', alignItems: 'center', gap: 16,
          opacity: Easing.easeOutCubic(clamp((t - 1.8) / 0.8, 0, 1)),
        }}>
          <div style={{ width: 60, height: 1, background: COLORS.gold }} />
          <div style={{
            fontFamily: FONTS.sans, fontSize: 22,
            color: COLORS.paper, letterSpacing: '0.2em',
            textTransform: 'uppercase',
          }}>
            Emmerske Efterskole
          </div>
          <div style={{ width: 60, height: 1, background: COLORS.gold }} />
        </div>
      </div>
    </>
  );
}

// ─── Main timeline composition ──────────────────────────────────────────────

// Scene schedule (in seconds) — total ~110s
const TIMELINE = [
  { id: 'opening',   start: 0,    end: 7,    Component: SceneOpening },
  { id: '1542',      start: 7,    end: 15,   Component: Scene1542 },
  { id: '1729',      start: 15,   end: 24,   Component: Scene1729 },
  { id: '1730',      start: 24,   end: 36,   Component: Scene1730 },
  { id: 'brorson',   start: 36,   end: 46,   Component: SceneBrorsonSong },
  { id: '1835',      start: 46,   end: 55,   Component: Scene1835 },
  { id: '1920',      start: 55,   end: 64,   Component: Scene1920 },
  { id: 'warclose',  start: 64,   end: 75,   Component: SceneWarClose },
  { id: '1978',      start: 75,   end: 86,   Component: Scene1978 },
  { id: '2012',      start: 86,   end: 95,   Component: Scene2012 },
  { id: 'today',     start: 95,   end: 106,  Component: SceneToday },
  { id: 'outro',     start: 106,  end: 115,  Component: SceneOutro },
];

const TOTAL_DURATION = 115;

// Timeline-strip marks (shown from end of opening onward)
const MARKS = [
  { year: '1542', pos: 0.00, activateAt: 7 },
  { year: '1729', pos: 0.10, activateAt: 15 },
  { year: '1730', pos: 0.20, activateAt: 24 },
  { year: '1732', pos: 0.32, activateAt: 36 },
  { year: '1835', pos: 0.43, activateAt: 46 },
  { year: '1920', pos: 0.54, activateAt: 55 },
  { year: '1943', pos: 0.63, activateAt: 64 },
  { year: '1978', pos: 0.75, activateAt: 75 },
  { year: '2012', pos: 0.85, activateAt: 86 },
  { year: 'I DAG', pos: 1.0, activateAt: 95 },
];

function TimelineVideo() {
  const time = useTime();
  // Hide chapter strip during opening & outro for cinematic effect
  const showStrip = time >= 7 && time <= 106;

  return (
    <>
      {/* Background grain layer */}
      <GrainOverlay />
      <Vignette />

      {/* Scenes */}
      {TIMELINE.map(({ id, start, end, Component }) => (
        <Sprite key={id} start={start} end={end}>
          <Component />
        </Sprite>
      ))}

      {/* Persistent timeline strip */}
      {showStrip && (
        <TimelineStrip totalDuration={TOTAL_DURATION} marks={MARKS} />
      )}

      {/* Fixed brand mark, bottom right (except opening) */}
      {time >= 7 && time <= 106 && (
        <div style={{
          position: 'absolute',
          bottom: 50, right: 80,
          fontFamily: FONTS.mono, fontSize: 11,
          color: COLORS.mutedMore, letterSpacing: '0.25em',
          textTransform: 'uppercase',
          zIndex: 7,
        }}>
          Emmerske Efterskole
        </div>
      )}
    </>
  );
}

Object.assign(window, {
  TimelineVideo, COLORS, FONTS, TOTAL_DURATION,
});
