// Scenes for the Cutflux sign-up explainer
// All scenes assume Stage @ 1280x720, duration ~30s

const sceneStyles = {
  // Color tokens lifted from the screenshots: deep navy/black bg, electric blue, lightning amber
  bg: '#05060B',
  panel: '#0E1422',
  panelBorder: 'rgba(96, 142, 220, 0.18)',
  text: '#E8EEF8',
  textDim: '#7C8AA6',
  blue: '#4A8BE8',
  blueDeep: '#1E3A6B',
  amber: '#F5A623',
  red: '#E25555',
  green: '#3FCB7E',
};

// ── Helpers ─────────────────────────────────────────────────────────────────
const Bolt = ({ size = 64, color = sceneStyles.amber }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none">
    <path d="M13 2L4.5 13.5h6L9 22l8.5-11.5h-6L13 2z" fill={color} />
  </svg>
);

const LogoMark = ({ size = 64, glow = 0 }) => (
  <div style={{
    width: size, height: size,
    borderRadius: size * 0.22,
    background: `linear-gradient(135deg, ${sceneStyles.blue} 0%, ${sceneStyles.blueDeep} 100%)`,
    display: 'flex', alignItems: 'center', justifyContent: 'center',
    boxShadow: `0 0 ${40 + glow * 60}px rgba(74, 139, 232, ${0.3 + glow * 0.5})`,
  }}>
    <Bolt size={size * 0.55} color="#fff" />
  </div>
);

// Subtle animated grid backdrop
function GridBackdrop() {
  const t = useTime();
  const opacity = 0.04 + 0.02 * Math.sin(t * 0.8);
  return (
    <div style={{
      position: 'absolute', inset: 0,
      backgroundImage:
        `linear-gradient(${sceneStyles.blue} 1px, transparent 1px), ` +
        `linear-gradient(90deg, ${sceneStyles.blue} 1px, transparent 1px)`,
      backgroundSize: '64px 64px',
      opacity,
      maskImage: 'radial-gradient(ellipse at center, black 30%, transparent 75%)',
      WebkitMaskImage: 'radial-gradient(ellipse at center, black 30%, transparent 75%)',
    }} />
  );
}

// Beat-synced pulse: returns a 0-1 envelope on a beat (~120bpm = 0.5s)
function beatEnv(t, bpm = 120, phase = 0) {
  const period = 60 / bpm;
  const x = ((t + phase) % period) / period; // 0..1
  // Sharp attack, exponential decay
  return Math.pow(1 - x, 4);
}

// ── Scene 1: Logo intro (0–4s) ─────────────────────────────────────────────
function SceneLogo() {
  const { progress, localTime } = useSprite();
  // Logo scale-in
  const logoScale = interpolate([0, 0.4, 1], [0.6, 1.05, 1], Easing.easeOutBack)(progress);
  const logoOpacity = interpolate([0, 0.2], [0, 1], Easing.easeOutQuad)(progress);
  // Title typewriter
  const fullTitle = 'Cutflux';
  const titleProgress = interpolate([0.35, 0.85], [0, 1], Easing.easeInOutQuad)(progress);
  const titleChars = Math.floor(titleProgress * fullTitle.length);
  const subOpacity = interpolate([0.7, 1], [0, 1], Easing.easeOutQuad)(progress);
  const exit = interpolate([0.85, 1], [1, 0], Easing.easeInQuad)(progress);

  return (
    <div style={{
      position: 'absolute', inset: 0,
      display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center',
      gap: 24, opacity: exit,
    }}>
      <div style={{ transform: `scale(${logoScale})`, opacity: logoOpacity }}>
        <LogoMark size={120} glow={Math.sin(localTime * 4) * 0.5 + 0.5} />
      </div>
      <div style={{
        fontSize: 56, fontWeight: 700, letterSpacing: '-0.02em',
        color: sceneStyles.text, fontFamily: 'Inter, system-ui, sans-serif',
      }}>
        {fullTitle.slice(0, titleChars)}
        <span style={{
          display: 'inline-block', width: 3, height: 48,
          background: sceneStyles.blue, marginLeft: 4,
          opacity: Math.floor(localTime * 3) % 2,
          verticalAlign: 'middle',
        }} />
      </div>
      <div style={{
        fontSize: 18, color: sceneStyles.textDim, opacity: subOpacity,
        fontFamily: 'JetBrains Mono, monospace', letterSpacing: '0.1em', textTransform: 'uppercase',
      }}>
        video + song → beat-synced edit
      </div>
    </div>
  );
}

// ── Scene 2: Drop your clips (4–10s) ───────────────────────────────────────
const CLIP_COLORS = [
  ['#4A8BE8', '#1E3A6B'],
  ['#E25555', '#5C1E1E'],
  ['#F5A623', '#5C3D0E'],
  ['#3FCB7E', '#1E5C36'],
  ['#A463E8', '#3D1E5C'],
];

function ClipTile({ idx, total, progress, dropProgress }) {
  // Each clip has its own stagger
  const stagger = idx * 0.08;
  const local = clamp((progress - stagger) / Math.max(0.01, 1 - stagger - 0.1), 0, 1);
  // Fly from off-screen sides
  const fromLeft = idx % 2 === 0;
  const startX = fromLeft ? -300 : 1500;
  const targetX = 100 + (idx % 5) * 210;
  const targetY = 120 + Math.floor(idx / 5) * 140;
  const x = interpolate([0, 1], [startX, targetX], Easing.easeOutCubic)(local);
  const y = targetY + Math.sin(local * Math.PI) * -20; // slight arc
  const rot = interpolate([0, 1], [fromLeft ? -15 : 15, 0], Easing.easeOutCubic)(local);

  // Drop into upload zone
  const consumeStart = 0.7;
  const consume = clamp((dropProgress - 0.4 - idx * 0.05) / 0.3, 0, 1);
  const cx = interpolate([0, 1], [targetX, 540], Easing.easeInQuad)(consume);
  const cy = interpolate([0, 1], [targetY, 380], Easing.easeInQuad)(consume);
  const cs = interpolate([0, 1], [1, 0.2], Easing.easeInQuad)(consume);
  const co = interpolate([0, 0.8, 1], [1, 1, 0], Easing.linear)(consume);

  const finalX = consume > 0 ? cx : x;
  const finalY = consume > 0 ? cy : y;
  const scale = consume > 0 ? cs : 1;
  const op = consume > 0 ? co : local;

  return (
    <div style={{
      position: 'absolute', left: finalX, top: finalY,
      width: 200, height: 120, borderRadius: 12,
      transform: `rotate(${rot}deg) scale(${scale})`,
      opacity: op,
      boxShadow: `0 12px 32px rgba(0,0,0,0.5)`,
      border: '1px solid rgba(255,255,255,0.12)',
      display: 'flex', alignItems: 'center', justifyContent: 'center',
      overflow: 'hidden',
    }}>
      {/* Original art fills the tile */}
      <div style={{ position: 'absolute', inset: 0 }}>
        <ClipArt idx={idx} />
      </div>
      {/* play triangle */}
      <div style={{
        position: 'relative', zIndex: 2,
        width: 36, height: 36, borderRadius: '50%',
        background: 'rgba(255,255,255,0.92)',
        display: 'flex', alignItems: 'center', justifyContent: 'center',
        boxShadow: '0 4px 12px rgba(0,0,0,0.3)',
      }}>
        <div style={{
          width: 0, height: 0,
          borderLeft: '11px solid #000',
          borderTop: '7px solid transparent',
          borderBottom: '7px solid transparent',
          marginLeft: 3,
        }} />
      </div>
      {/* timecode */}
      <div style={{
        position: 'absolute', bottom: 6, right: 8, zIndex: 2,
        fontFamily: 'JetBrains Mono, monospace', fontSize: 10,
        color: 'rgba(255,255,255,0.95)', letterSpacing: '0.05em',
        background: 'rgba(0,0,0,0.5)', padding: '2px 5px', borderRadius: 3,
      }}>00:0{idx + 1}</div>
    </div>
  );
}

function SceneClips() {
  const { progress } = useSprite();
  const headerOp = interpolate([0, 0.15], [0, 1], Easing.easeOutQuad)(progress);
  const headerExit = interpolate([0.9, 1], [1, 0], Easing.linear)(progress);
  const clipsProgress = interpolate([0.05, 0.6], [0, 1], Easing.linear)(progress);
  const dropProgress = interpolate([0.5, 1], [0, 1], Easing.linear)(progress);
  // Upload zone pulse
  const zonePulse = clamp((progress - 0.5) / 0.5, 0, 1);
  const zoneOp = interpolate([0, 0.15], [0, 1], Easing.easeOutQuad)(progress);
  const exit = interpolate([0.92, 1], [1, 0], Easing.easeInQuad)(progress);

  return (
    <div style={{ position: 'absolute', inset: 0, opacity: exit }}>
      {/* Header */}
      <div style={{
        position: 'absolute', top: 60, left: 0, right: 0, textAlign: 'center',
        opacity: headerOp * headerExit,
      }}>
        <div style={{
          fontSize: 14, color: sceneStyles.blue, letterSpacing: '0.3em', textTransform: 'uppercase',
          fontFamily: 'JetBrains Mono, monospace', marginBottom: 12,
        }}>Step 01</div>
        <div style={{
          fontSize: 52, fontWeight: 700, color: sceneStyles.text,
          fontFamily: 'Inter, system-ui, sans-serif', letterSpacing: '-0.02em',
        }}>Drop your clips</div>
      </div>

      {/* Upload zone */}
      <div style={{
        position: 'absolute', left: 440, top: 320, width: 400, height: 160,
        borderRadius: 16,
        background: sceneStyles.panel,
        border: `2px dashed ${zonePulse > 0 ? sceneStyles.blue : sceneStyles.panelBorder}`,
        display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center',
        gap: 8, opacity: zoneOp,
        boxShadow: zonePulse > 0
          ? `0 0 ${40 + 30 * Math.sin(zonePulse * 8)}px rgba(74, 139, 232, 0.4)`
          : 'none',
        transition: 'box-shadow 0.1s',
      }}>
        <div style={{ fontSize: 32 }}>
          <svg width="40" height="40" viewBox="0 0 24 24" fill="none">
            <path d="M12 4v12m0-12l-5 5m5-5l5 5M4 18h16" stroke={sceneStyles.blue} strokeWidth="2" strokeLinecap="round" />
          </svg>
        </div>
        <div style={{ color: sceneStyles.text, fontSize: 18, fontWeight: 600 }}>Choose video</div>
        <div style={{ color: sceneStyles.textDim, fontSize: 12, fontFamily: 'JetBrains Mono, monospace' }}>
          mp4 · mov · mkv · webm
        </div>
      </div>

      {/* Clips flying in */}
      {[0,1,2,3,4,5,6,7].map(i => (
        <ClipTile key={i} idx={i} total={8} progress={clipsProgress} dropProgress={dropProgress} />
      ))}
    </div>
  );
}

// ── Scene 3: Pick a song (10–16s) ──────────────────────────────────────────
function Waveform({ progress, beatTime }) {
  const bars = 64;
  const drawn = Math.floor(progress * bars);
  return (
    <div style={{
      display: 'flex', alignItems: 'center', gap: 3, height: 80, width: 720,
    }}>
      {Array.from({ length: bars }).map((_, i) => {
        const isDrawn = i < drawn;
        // pseudo-random but deterministic amplitude
        const seed = Math.sin(i * 12.9898) * 43758.5453;
        const baseAmp = 0.25 + Math.abs(seed - Math.floor(seed)) * 0.75;
        // amplify on beats — every 8th bar is a kick
        const isKick = i % 8 === 0;
        const beatBoost = isKick ? beatEnv(beatTime, 120) * 0.4 : 0;
        const amp = isDrawn ? Math.min(1, baseAmp + beatBoost) : 0;
        return (
          <div key={i} style={{
            width: 6, height: `${amp * 100}%`,
            background: isKick ? sceneStyles.blue : 'rgba(74, 139, 232, 0.55)',
            borderRadius: 3,
            transition: 'height 0.05s',
            boxShadow: isKick && beatBoost > 0.3 ? `0 0 12px ${sceneStyles.blue}` : 'none',
          }} />
        );
      })}
    </div>
  );
}

function SceneSong() {
  const { progress, localTime } = useSprite();
  const headerOp = interpolate([0, 0.15], [0, 1], Easing.easeOutQuad)(progress);
  const cardOp = interpolate([0.1, 0.3], [0, 1], Easing.easeOutQuad)(progress);
  const cardY = interpolate([0.1, 0.3], [40, 0], Easing.easeOutCubic)(progress);
  const waveProgress = interpolate([0.3, 0.8], [0, 1], Easing.easeOutQuad)(progress);
  const exit = interpolate([0.92, 1], [1, 0], Easing.easeInQuad)(progress);

  return (
    <div style={{ position: 'absolute', inset: 0, opacity: exit }}>
      <div style={{
        position: 'absolute', top: 60, left: 0, right: 0, textAlign: 'center',
        opacity: headerOp,
      }}>
        <div style={{
          fontSize: 14, color: sceneStyles.blue, letterSpacing: '0.3em', textTransform: 'uppercase',
          fontFamily: 'JetBrains Mono, monospace', marginBottom: 12,
        }}>Step 02</div>
        <div style={{
          fontSize: 52, fontWeight: 700, color: sceneStyles.text,
          fontFamily: 'Inter, system-ui, sans-serif', letterSpacing: '-0.02em',
        }}>Pick a song</div>
      </div>

      {/* Song card */}
      <div style={{
        position: 'absolute', left: 240, top: 260, width: 800,
        background: sceneStyles.panel,
        border: `1px solid ${sceneStyles.panelBorder}`,
        borderRadius: 16, padding: 24,
        opacity: cardOp,
        transform: `translateY(${cardY}px)`,
        boxShadow: `0 20px 60px rgba(0,0,0,0.5)`,
      }}>
        <div style={{
          display: 'flex', alignItems: 'center', gap: 16, marginBottom: 20,
        }}>
          {/* Album art */}
          <div style={{
            width: 64, height: 64, borderRadius: 10,
            background: `linear-gradient(135deg, ${sceneStyles.blue}, ${sceneStyles.amber})`,
            display: 'flex', alignItems: 'center', justifyContent: 'center',
            boxShadow: `0 0 ${20 + beatEnv(localTime, 120) * 30}px rgba(74, 139, 232, 0.6)`,
          }}>
            <svg width="28" height="28" viewBox="0 0 24 24" fill="#fff">
              <path d="M9 18V5l12-2v13" stroke="#fff" strokeWidth="2" fill="none" strokeLinecap="round" />
              <circle cx="6" cy="18" r="3" />
              <circle cx="18" cy="16" r="3" />
            </svg>
          </div>
          <div style={{ flex: 1 }}>
            <div style={{ color: sceneStyles.text, fontSize: 22, fontWeight: 600 }}>
              Untitled Beat
            </div>
            <div style={{ color: sceneStyles.textDim, fontSize: 13, fontFamily: 'JetBrains Mono, monospace', marginTop: 4 }}>
              120 BPM · 0:30 · synced
            </div>
          </div>
          <div style={{
            padding: '6px 12px', borderRadius: 6,
            background: 'rgba(63, 203, 126, 0.15)',
            color: sceneStyles.green,
            fontSize: 11, fontFamily: 'JetBrains Mono, monospace',
            letterSpacing: '0.1em', textTransform: 'uppercase',
          }}>
            ● beat detected
          </div>
        </div>
        {/* Waveform */}
        <Waveform progress={waveProgress} beatTime={localTime} />
        {/* Beat markers */}
        <div style={{
          display: 'flex', justifyContent: 'space-between', marginTop: 16,
          fontFamily: 'JetBrains Mono, monospace', fontSize: 11, color: sceneStyles.textDim,
        }}>
          <span>0:00</span>
          <span style={{ color: sceneStyles.blue }}>● kick · ● snare · ● drop</span>
          <span>0:30</span>
        </div>
      </div>
    </div>
  );
}

// ── Scene 4: AI cuts to the beat (16–22s) ──────────────────────────────────
function SceneCut() {
  const { progress, localTime } = useSprite();
  const headerOp = interpolate([0, 0.15], [0, 1], Easing.easeOutQuad)(progress);
  const timelineOp = interpolate([0.1, 0.3], [0, 1], Easing.easeOutQuad)(progress);
  const exit = interpolate([0.92, 1], [1, 0], Easing.easeInQuad)(progress);

  // Playhead sweeps across timeline
  const playhead = clamp((progress - 0.2) / 0.7, 0, 1);

  // Clips on timeline — each has start and length (in fraction of timeline)
  const clips = [
    { start: 0.00, len: 0.12, c: 0 },
    { start: 0.12, len: 0.10, c: 1 },
    { start: 0.22, len: 0.08, c: 2 },
    { start: 0.30, len: 0.14, c: 3 },
    { start: 0.44, len: 0.10, c: 4 },
    { start: 0.54, len: 0.16, c: 0 },
    { start: 0.70, len: 0.12, c: 2 },
    { start: 0.82, len: 0.18, c: 1 },
  ];

  // Which clip is "active" (playhead inside)
  const activeClip = clips.findIndex(c => playhead >= c.start && playhead < c.start + c.len);

  const timelineWidth = 1080;
  const timelineX = 100;
  const timelineY = 380;

  // Beat markers — 8 kicks across 30s timeline
  const beats = [0.0, 0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875];

  // Beat flash sync — flash whenever playhead crosses a beat
  const nearestBeat = beats.reduce((closest, b) =>
    Math.abs(b - playhead) < Math.abs(closest - playhead) ? b : closest, 0);
  const beatFlash = clamp(1 - Math.abs(playhead - nearestBeat) * 30, 0, 1);

  return (
    <div style={{ position: 'absolute', inset: 0, opacity: exit }}>
      <div style={{
        position: 'absolute', top: 60, left: 0, right: 0, textAlign: 'center',
        opacity: headerOp,
      }}>
        <div style={{
          fontSize: 14, color: sceneStyles.amber, letterSpacing: '0.3em', textTransform: 'uppercase',
          fontFamily: 'JetBrains Mono, monospace', marginBottom: 12,
        }}>Step 03 · instant</div>
        <div style={{
          fontSize: 52, fontWeight: 700, color: sceneStyles.text,
          fontFamily: 'Inter, system-ui, sans-serif', letterSpacing: '-0.02em',
        }}>Cut to the beat in seconds</div>
      </div>

      {/* Preview window */}
      <div style={{
        position: 'absolute', left: 380, top: 180, width: 520, height: 160, borderRadius: 14,
        background: sceneStyles.panel,
        border: '1px solid rgba(255,255,255,0.1)',
        opacity: timelineOp,
        boxShadow: `0 0 ${20 + beatFlash * 60}px rgba(74, 139, 232, ${0.2 + beatFlash * 0.6})`,
        overflow: 'hidden',
      }}>
        {/* Live art for the active clip */}
        <div style={{ position: 'absolute', inset: 0 }}>
          {activeClip >= 0 && <ClipArt idx={clips[activeClip].c} />}
        </div>
        {/* Live badge */}
        <div style={{
          position: 'absolute', top: 12, left: 14, zIndex: 2,
          fontFamily: 'JetBrains Mono, monospace', fontSize: 11,
          color: '#fff', letterSpacing: '0.15em',
          background: 'rgba(0,0,0,0.55)', padding: '4px 8px', borderRadius: 4,
          backdropFilter: 'blur(4px)',
        }}>
          ● LIVE · CLIP {activeClip >= 0 ? String(activeClip + 1).padStart(2, '0') : '--'}
        </div>
        {/* Beat flash overlay */}
        <div style={{
          position: 'absolute', inset: 0,
          background: 'rgba(255,255,255,0.2)',
          opacity: beatFlash * 0.6,
          pointerEvents: 'none',
        }} />
      </div>

      {/* Timeline track */}
      <div style={{
        position: 'absolute', left: timelineX, top: timelineY, width: timelineWidth,
        opacity: timelineOp,
      }}>
        {/* Track label */}
        <div style={{
          fontFamily: 'JetBrains Mono, monospace', fontSize: 11, color: sceneStyles.textDim,
          letterSpacing: '0.15em', marginBottom: 8, textTransform: 'uppercase',
        }}>VIDEO TRACK</div>
        {/* Track BG */}
        <div style={{
          position: 'relative', height: 64, borderRadius: 8,
          background: 'rgba(255,255,255,0.04)',
          border: '1px solid rgba(255,255,255,0.06)',
        }}>
          {/* Beat grid lines */}
          {beats.map((b, i) => (
            <div key={i} style={{
              position: 'absolute', left: `${b * 100}%`, top: -4, bottom: -4, width: 1,
              background: sceneStyles.blue, opacity: 0.25,
            }} />
          ))}
          {/* Clips */}
          {clips.map((c, i) => {
            const appear = clamp((progress - 0.15 - i * 0.04) / 0.1, 0, 1);
            const [c1, c2] = CLIP_COLORS[c.c];
            return (
              <div key={i} style={{
                position: 'absolute',
                left: `${c.start * 100}%`,
                width: `calc(${c.len * 100}% - 2px)`,
                top: 4, bottom: 4,
                borderRadius: 5,
                background: `linear-gradient(135deg, ${c1}, ${c2})`,
                opacity: appear,
                transform: `scaleY(${appear})`,
                boxShadow: i === activeClip ? `0 0 16px ${c1}` : 'none',
                border: i === activeClip ? '1px solid rgba(255,255,255,0.5)' : '1px solid rgba(255,255,255,0.08)',
                transition: 'box-shadow 0.05s',
              }} />
            );
          })}
          {/* Playhead */}
          {progress > 0.2 && (
            <div style={{
              position: 'absolute', left: `${playhead * 100}%`, top: -8, bottom: -8, width: 2,
              background: sceneStyles.amber,
              boxShadow: `0 0 12px ${sceneStyles.amber}`,
            }} >
              <div style={{
                position: 'absolute', top: -6, left: -5, width: 12, height: 12,
                background: sceneStyles.amber, transform: 'rotate(45deg)', borderRadius: 2,
              }} />
            </div>
          )}
        </div>
        {/* Audio track */}
        <div style={{
          fontFamily: 'JetBrains Mono, monospace', fontSize: 11, color: sceneStyles.textDim,
          letterSpacing: '0.15em', margin: '20px 0 8px', textTransform: 'uppercase',
        }}>AUDIO · 120 BPM</div>
        <div style={{
          position: 'relative', height: 40, borderRadius: 6,
          background: 'rgba(74, 139, 232, 0.08)',
          border: '1px solid rgba(74, 139, 232, 0.15)',
          display: 'flex', alignItems: 'center', padding: '0 4px', gap: 2,
        }}>
          {Array.from({ length: 80 }).map((_, i) => {
            const seed = Math.sin(i * 7.13) * 43758.5453;
            const baseAmp = 0.3 + Math.abs(seed - Math.floor(seed)) * 0.7;
            const isKick = i % 10 === 0;
            const isPast = (i / 80) < playhead;
            return (
              <div key={i} style={{
                flex: 1, height: `${baseAmp * 80}%`,
                background: isPast
                  ? (isKick ? sceneStyles.blue : 'rgba(74, 139, 232, 0.7)')
                  : 'rgba(74, 139, 232, 0.25)',
                borderRadius: 1,
              }} />
            );
          })}
        </div>
      </div>
    </div>
  );
}

// ── Scene 5: Output (22–26s) ───────────────────────────────────────────────
function ScenePhone() {
  const { progress, localTime } = useSprite();
  const headerOp = interpolate([0, 0.15], [0, 1], Easing.easeOutQuad)(progress);
  const phoneScale = interpolate([0, 0.4], [0.7, 1], Easing.easeOutBack)(progress);
  const phoneOp = interpolate([0, 0.2], [0, 1], Easing.easeOutQuad)(progress);
  const exit = interpolate([0.9, 1], [1, 0], Easing.easeInQuad)(progress);

  // Inside phone, cycle through art frames to imply video playback
  const frameCycle = Math.floor(localTime * 3) % ART_COMPONENTS.length;

  const beat = beatEnv(localTime, 120);

  return (
    <div style={{ position: 'absolute', inset: 0, opacity: exit }}>
      <div style={{
        position: 'absolute', top: 60, left: 0, right: 0, textAlign: 'center',
        opacity: headerOp,
      }}>
        <div style={{
          fontSize: 14, color: sceneStyles.green, letterSpacing: '0.3em', textTransform: 'uppercase',
          fontFamily: 'JetBrains Mono, monospace', marginBottom: 12,
        }}>Step 04 · done</div>
        <div style={{
          fontSize: 52, fontWeight: 700, color: sceneStyles.text,
          fontFamily: 'Inter, system-ui, sans-serif', letterSpacing: '-0.02em',
        }}>Export the edit</div>
      </div>

      {/* Phone */}
      <div style={{
        position: 'absolute', left: '50%', top: 180,
        width: 280, height: 480,
        marginLeft: -140,
        borderRadius: 36,
        background: '#000',
        border: '8px solid #1a1a1a',
        boxShadow: `0 30px 80px rgba(0,0,0,0.7), 0 0 ${30 + beat * 40}px rgba(74, 139, 232, 0.3)`,
        opacity: phoneOp,
        transform: `scale(${phoneScale})`,
        overflow: 'hidden',
      }}>
        {/* Notch */}
        <div style={{
          position: 'absolute', top: 0, left: '50%', marginLeft: -50,
          width: 100, height: 22, borderRadius: '0 0 14px 14px',
          background: '#000', zIndex: 2,
        }} />
        {/* Video frame */}
        <div style={{
          position: 'absolute', inset: 0,
          display: 'flex', alignItems: 'center', justifyContent: 'center',
          overflow: 'hidden',
        }}>
          {/* Live art frame */}
          <div style={{ position: 'absolute', inset: 0 }}>
            <ClipArt idx={frameCycle} />
          </div>
          {/* Beat flash */}
          <div style={{
            position: 'absolute', inset: 0, background: '#fff',
            opacity: beat * 0.3,
          }} />
          {/* Watermark logo */}
          <div style={{
            position: 'absolute', top: 40, left: 16, display: 'flex', gap: 8, alignItems: 'center',
          }}>
            <LogoMark size={28} />
          </div>
          {/* Center content */}
          <div style={{
            position: 'relative', zIndex: 2,
            color: '#fff', fontFamily: 'JetBrains Mono, monospace', fontSize: 13,
            letterSpacing: '0.2em', textTransform: 'uppercase',
            background: 'rgba(0,0,0,0.5)', padding: '8px 14px', borderRadius: 6,
            backdropFilter: 'blur(8px)',
          }}>
            CLIP {String(frameCycle + 1).padStart(2, '0')} / 06
          </div>
          {/* Progress bar */}
          <div style={{
            position: 'absolute', bottom: 24, left: 16, right: 16, height: 3,
            background: 'rgba(255,255,255,0.3)', borderRadius: 2, overflow: 'hidden',
          }}>
            <div style={{
              height: '100%', width: `${(localTime % 4) / 4 * 100}%`,
              background: '#fff',
            }} />
          </div>
        </div>
      </div>

      {/* Side annotations */}
      <div style={{
        position: 'absolute', left: 100, top: 280, opacity: phoneOp,
        fontFamily: 'JetBrains Mono, monospace', fontSize: 12, color: sceneStyles.textDim,
        letterSpacing: '0.1em',
      }}>
        <div style={{ color: sceneStyles.green, marginBottom: 6 }}>● 480p · ready</div>
        <div>14 cuts · 30s</div>
        <div style={{ marginTop: 6 }}>kick-aligned</div>
      </div>
      <div style={{
        position: 'absolute', right: 100, top: 320, opacity: phoneOp, textAlign: 'right',
        fontFamily: 'JetBrains Mono, monospace', fontSize: 12, color: sceneStyles.textDim,
        letterSpacing: '0.1em',
      }}>
        <div style={{ color: sceneStyles.blue, marginBottom: 6 }}>● beat flash on</div>
        <div>cut style: auto</div>
        <div style={{ marginTop: 6 }}>cinematic intro</div>
      </div>
    </div>
  );
}

// ── Scene 6: CTA — Sign Up Now (26–30s) ────────────────────────────────────
function SceneCTA() {
  const { progress, localTime } = useSprite();
  const titleOp = interpolate([0, 0.2], [0, 1], Easing.easeOutQuad)(progress);
  const titleY = interpolate([0, 0.3], [30, 0], Easing.easeOutCubic)(progress);
  const subOp = interpolate([0.15, 0.4], [0, 1], Easing.easeOutQuad)(progress);
  const btnOp = interpolate([0.3, 0.55], [0, 1], Easing.easeOutQuad)(progress);
  const btnScale = interpolate([0.3, 0.55, 0.7], [0.8, 1.05, 1], Easing.easeOutBack)(progress);
  const benefitsOp = interpolate([0.55, 0.75], [0, 1], Easing.easeOutQuad)(progress);

  // Pulsing button
  const pulse = 1 + Math.sin(localTime * 3) * 0.02;
  const beat = beatEnv(localTime, 120);

  return (
    <div style={{
      position: 'absolute', inset: 0,
      display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center',
      gap: 24,
    }}>
      {/* Logo */}
      <div style={{ opacity: titleOp, transform: `translateY(${titleY}px) scale(${1 + beat * 0.05})` }}>
        <LogoMark size={72} glow={beat} />
      </div>

      {/* Headline */}
      <div style={{
        opacity: titleOp, transform: `translateY(${titleY}px)`,
        fontSize: 64, fontWeight: 800, color: sceneStyles.text,
        fontFamily: 'Inter, system-ui, sans-serif', letterSpacing: '-0.03em',
        textAlign: 'center', lineHeight: 1.05,
      }}>
        Beat-synced edits.<br />
        <span style={{
          background: `linear-gradient(135deg, ${sceneStyles.blue}, ${sceneStyles.amber})`,
          WebkitBackgroundClip: 'text', backgroundClip: 'text', WebkitTextFillColor: 'transparent',
        }}>
          In one click.
        </span>
      </div>

      <div style={{
        opacity: subOp,
        fontSize: 18, color: sceneStyles.textDim,
        fontFamily: 'JetBrains Mono, monospace', letterSpacing: '0.1em', textTransform: 'uppercase',
      }}>
        free · runs in your browser · no install
      </div>

      {/* CTA Button */}
      <button style={{
        marginTop: 16,
        opacity: btnOp,
        transform: `scale(${btnScale * pulse})`,
        padding: '20px 56px',
        fontSize: 24, fontWeight: 700,
        fontFamily: 'Inter, system-ui, sans-serif',
        color: '#fff', border: 'none',
        borderRadius: 14, cursor: 'pointer',
        background: `linear-gradient(135deg, ${sceneStyles.blue} 0%, ${sceneStyles.blueDeep} 100%)`,
        boxShadow: `0 10px 40px rgba(74, 139, 232, 0.5), 0 0 ${20 + beat * 50}px rgba(74, 139, 232, ${0.3 + beat * 0.4})`,
        display: 'flex', alignItems: 'center', gap: 12,
        letterSpacing: '0.02em',
      }}>
        <Bolt size={28} color="#F5A623" />
        Sign Up Now
        <span style={{
          display: 'inline-block', transform: `translateX(${Math.sin(localTime * 4) * 4}px)`,
        }}>→</span>
      </button>

      {/* Benefits */}
      <div style={{
        opacity: benefitsOp, marginTop: 20,
        display: 'flex', gap: 32,
        fontFamily: 'JetBrains Mono, monospace', fontSize: 12, color: sceneStyles.textDim,
        letterSpacing: '0.1em', textTransform: 'uppercase',
      }}>
        <span>✓ no card</span>
        <span>✓ unlimited edits</span>
        <span>✓ save to gallery</span>
      </div>
    </div>
  );
}

// ── Master composition ─────────────────────────────────────────────────────
function ExplainerVideo() {
  return (
    <>
      <GridBackdrop />
      <Sprite start={0} end={4}><SceneLogo /></Sprite>
      <Sprite start={4} end={10}><SceneClips /></Sprite>
      <Sprite start={10} end={16}><SceneSong /></Sprite>
      <Sprite start={16} end={22}><SceneCut /></Sprite>
      <Sprite start={22} end={26}><ScenePhone /></Sprite>
      <Sprite start={26} end={32}><SceneCTA /></Sprite>

      {/* Persistent corner brand mark, after intro */}
      <PersistentBrand />
    </>
  );
}

function PersistentBrand() {
  const t = useTime();
  if (t < 4 || t > 26) return null;
  const op = interpolate([4, 4.5, 25, 26], [0, 0.7, 0.7, 0], Easing.linear)(t);
  return (
    <div style={{
      position: 'absolute', top: 32, left: 32,
      display: 'flex', alignItems: 'center', gap: 10,
      opacity: op,
    }}>
      <LogoMark size={32} />
      <span style={{
        color: sceneStyles.text, fontSize: 16, fontWeight: 600,
        fontFamily: 'Inter, system-ui, sans-serif',
      }}>Cutflux</span>
    </div>
  );
}

Object.assign(window, {
  ExplainerVideo, sceneStyles,
});
