// Scroll-driven intro scenes — original compositions

// Scene 1: Globe with cat on Belgium
function SceneGlobe({ progress, theme }) {
  const rot = progress * 20; // slight rotation
  const stroke = theme === 'dark' ? '#f4f1e8' : '#0a0a0a';
  return (
    <svg viewBox="0 0 1200 900" className="scene-svg" preserveAspectRatio="xMidYMid slice">
      <defs>
        <radialGradient id="globe-grad" cx="45%" cy="40%">
          <stop offset="0%" stopColor={theme === 'dark' ? '#1a1a1a' : '#f4f1e8'}/>
          <stop offset="100%" stopColor={theme === 'dark' ? '#050505' : '#d9d4c4'}/>
        </radialGradient>
        <pattern id="stars" width="100" height="100" patternUnits="userSpaceOnUse">
          <circle cx="20" cy="30" r="0.8" fill={stroke} opacity="0.5"/>
          <circle cx="70" cy="60" r="0.5" fill={stroke} opacity="0.3"/>
          <circle cx="45" cy="85" r="0.6" fill={stroke} opacity="0.4"/>
        </pattern>
      </defs>
      <rect width="1200" height="900" fill="url(#stars)"/>
      {/* globe */}
      <g transform={`translate(600 450) rotate(${rot})`}>
        <circle r="280" fill="url(#globe-grad)" stroke={stroke} strokeWidth="2"/>
        {/* continents — abstract blobs */}
        <g fill={stroke} opacity="0.85">
          {/* Africa/Europe */}
          <path d="M-40 -100 Q-20 -120 20 -110 Q50 -90 40 -60 Q50 -20 30 20 Q10 60 -20 80 Q-50 70 -60 40 Q-70 0 -60 -40 Q-60 -80 -40 -100 Z"/>
          {/* Asia bulge */}
          <path d="M40 -110 Q100 -130 160 -100 Q200 -60 180 -20 Q160 10 120 20 Q80 10 60 -20 Q40 -60 40 -110 Z"/>
          {/* Americas */}
          <path d="M-180 -80 Q-150 -100 -120 -80 Q-110 -40 -130 0 Q-150 60 -170 80 Q-200 60 -210 20 Q-210 -40 -180 -80 Z"/>
        </g>
        {/* Belgium marker (small dot on Europe) */}
        <circle cx="-5" cy="-75" r="4" fill="#ff2e4d"/>
        <circle cx="-5" cy="-75" r="10" fill="none" stroke="#ff2e4d" strokeWidth="1.5" opacity="0.6"/>
        {/* meridians */}
        <ellipse rx="280" ry="100" fill="none" stroke={stroke} strokeWidth="0.5" opacity="0.4"/>
        <ellipse rx="280" ry="180" fill="none" stroke={stroke} strokeWidth="0.5" opacity="0.3"/>
        <ellipse rx="100" ry="280" fill="none" stroke={stroke} strokeWidth="0.5" opacity="0.3"/>
      </g>
      {/* oversized cat perched on Belgium */}
      <g transform={`translate(520 180) scale(1.2)`}>
        <CatSit size={200} color={stroke}/>
      </g>
      {/* floating text fragments */}
      <text x="80" y="120" fill={stroke} fontFamily="Anton" fontSize="44" opacity={1 - progress*2} letterSpacing="2">ONE CAT.</text>
      <text x="80" y="170" fill={stroke} fontFamily="Anton" fontSize="44" opacity={1 - progress*2} letterSpacing="2">ONE PLANET.</text>
      <text x="80" y="220" fill="#ff2e4d" fontFamily="Anton" fontSize="44" opacity={1 - progress*2} letterSpacing="2">ONE MISSION.</text>
    </svg>
  );
}

// Scene 2: Running across the globe
function SceneRun({ progress, theme }) {
  const stroke = theme === 'dark' ? '#f4f1e8' : '#0a0a0a';
  const t = progress; // 0..1 across arc
  // Arc from Belgium (left side) to Seoul (right side) over globe curve
  const x = 150 + t * 900;
  const y = 400 - Math.sin(t * Math.PI) * 180;
  const angle = Math.cos(t * Math.PI) * -25;
  return (
    <svg viewBox="0 0 1200 900" className="scene-svg" preserveAspectRatio="xMidYMid slice">
      {/* globe bg — pushed down, cat runs across top */}
      <g transform="translate(600 900)">
        <circle r="620" fill={theme === 'dark' ? '#0f0f0f' : '#ede8dc'} stroke={stroke} strokeWidth="2"/>
        <g fill={stroke} opacity="0.5">
          <path d="M-300 -300 Q-200 -400 -50 -380 Q100 -360 180 -280 Q200 -180 100 -150 Q-50 -120 -200 -180 Q-320 -220 -300 -300 Z"/>
          <path d="M200 -450 Q380 -480 480 -380 Q500 -280 400 -250 Q280 -260 220 -340 Q180 -420 200 -450 Z"/>
        </g>
        {/* path/trail */}
        <path d={`M ${-450 + 450} ${-380} Q 0 ${-620} 450 ${-380}`} fill="none" stroke="#ff2e4d" strokeWidth="3" strokeDasharray="8 12" opacity="0.8"/>
        {/* Belgium dot */}
        <circle cx="-450" cy="-380" r="6" fill="#ff2e4d"/>
        <text x="-480" y="-400" fill={stroke} fontFamily="JetBrains Mono" fontSize="16">BRUSSELS</text>
        {/* Seoul dot */}
        <circle cx="450" cy="-380" r="6" fill={t > 0.8 ? '#f7d046' : '#ff2e4d'}/>
        <text x="420" y="-400" fill={stroke} fontFamily="JetBrains Mono" fontSize="16">SEOUL</text>
      </g>
      {/* running cat */}
      <g transform={`translate(${x} ${y}) rotate(${angle})`}>
        <CatRun size={180} color={stroke}/>
      </g>
      {/* motion lines */}
      <g stroke={stroke} strokeWidth="2" opacity="0.4">
        <line x1={x - 120} y1={y + 10} x2={x - 200} y2={y + 10}/>
        <line x1={x - 100} y1={y + 30} x2={x - 160} y2={y + 30}/>
        <line x1={x - 110} y1={y - 10} x2={x - 180} y2={y - 10}/>
      </g>
      {/* km counter */}
      <text x="80" y="100" fill="#ff2e4d" fontFamily="JetBrains Mono" fontSize="14">DIST_TRAVELED</text>
      <text x="80" y="140" fill={stroke} fontFamily="Anton" fontSize="56">{Math.round(t * 8950).toLocaleString()} KM</text>
    </svg>
  );
}

// Scene 3: Plunge toward city
function SceneDive({ progress, theme }) {
  const stroke = theme === 'dark' ? '#f4f1e8' : '#0a0a0a';
  const bg = theme === 'dark' ? '#050505' : '#ede8dc';
  const catY = 100 + progress * 500;
  const catScale = 0.6 + progress * 1.2;
  return (
    <svg viewBox="0 0 1200 900" className="scene-svg" preserveAspectRatio="xMidYMid slice">
      <rect width="1200" height="900" fill={bg}/>
      {/* speed lines radiating */}
      <g stroke={stroke} strokeWidth="1" opacity="0.3">
        {Array.from({length: 30}).map((_, i) => {
          const a = (i / 30) * Math.PI * 2;
          const r1 = 200 + progress * 300;
          const r2 = 500 + progress * 400;
          return <line key={i} x1={600 + Math.cos(a)*r1} y1={catY + Math.sin(a)*r1} x2={600 + Math.cos(a)*r2} y2={catY + Math.sin(a)*r2}/>;
        })}
      </g>
      {/* distant city glow */}
      <g transform={`translate(600 ${700 + (1 - progress) * 200}) scale(${0.3 + progress * 0.7})`}>
        <rect x="-300" y="0" width="600" height="10" fill="#ff2e4d" opacity="0.4"/>
        <g fill={stroke} opacity="0.6">
          <rect x="-280" y="-120" width="40" height="120"/>
          <rect x="-220" y="-80" width="30" height="80"/>
          <rect x="-170" y="-180" width="50" height="180"/>
          <rect x="-100" y="-150" width="45" height="150"/>
          <rect x="-40" y="-200" width="60" height="200"/>
          <rect x="40" y="-140" width="40" height="140"/>
          <rect x="100" y="-180" width="50" height="180"/>
          <rect x="170" y="-100" width="35" height="100"/>
          <rect x="220" y="-130" width="45" height="130"/>
        </g>
      </g>
      {/* diving cat */}
      <g transform={`translate(${600 - 80 * catScale} ${catY}) scale(${catScale})`}>
        <CatDive size={160} color={stroke}/>
      </g>
      <text x="80" y="100" fill="#ff2e4d" fontFamily="JetBrains Mono" fontSize="14">ALT</text>
      <text x="80" y="140" fill={stroke} fontFamily="Anton" fontSize="56">{Math.round(12000 * (1 - progress)).toLocaleString()} M</text>
      <text x="80" y="170" fill={stroke} fontFamily="JetBrains Mono" fontSize="12" opacity="0.6">DESCENDING · SEOUL</text>
    </svg>
  );
}

// Scene 4: City blocks assembling like puzzle
function SceneAssemble({ progress, theme }) {
  const stroke = theme === 'dark' ? '#f4f1e8' : '#0a0a0a';
  const bg = theme === 'dark' ? '#0a0a0a' : '#ede8dc';
  // blocks scatter from random offsets, converge at progress = 1
  const blocks = [
    { x: 200, y: 500, w: 80, h: 120, dx: -400, dy: -200, r: 25 },
    { x: 300, y: 500, w: 60, h: 200, dx: 300, dy: -400, r: -40 },
    { x: 380, y: 500, w: 100, h: 150, dx: -200, dy: 300, r: 15 },
    { x: 500, y: 500, w: 70, h: 250, dx: 400, dy: -300, r: -30 },
    { x: 590, y: 500, w: 90, h: 180, dx: -500, dy: -150, r: 35 },
    { x: 700, y: 500, w: 80, h: 220, dx: 200, dy: 400, r: 20 },
    { x: 800, y: 500, w: 60, h: 140, dx: 500, dy: 200, r: -45 },
    { x: 880, y: 500, w: 100, h: 170, dx: -350, dy: -450, r: 25 },
  ];
  return (
    <svg viewBox="0 0 1200 900" className="scene-svg" preserveAspectRatio="xMidYMid slice">
      <rect width="1200" height="900" fill={bg}/>
      {/* ground line */}
      <line x1="0" y1="650" x2="1200" y2="650" stroke={stroke} strokeWidth="1" opacity={progress}/>
      {blocks.map((b, i) => {
        const p = Math.min(1, Math.max(0, (progress - i * 0.04) * 1.4));
        const ex = 1 - p;
        const tx = b.x + b.dx * ex;
        const ty = b.y - b.h + b.dy * ex;
        const rot = b.r * ex;
        return (
          <g key={i} transform={`translate(${tx} ${ty}) rotate(${rot})`} opacity={0.3 + p * 0.7}>
            <rect width={b.w} height={b.h} fill={stroke}/>
            {/* windows */}
            {Array.from({length: Math.floor(b.h / 25)}).map((_, j) => (
              <rect key={j} x="8" y={10 + j * 25} width="10" height="10" fill="#ff2e4d" opacity="0.7"/>
            ))}
            {Array.from({length: Math.floor(b.h / 25)}).map((_, j) => (
              <rect key={`b${j}`} x={b.w - 18} y={10 + j * 25} width="10" height="10" fill="#f7d046" opacity="0.5"/>
            ))}
          </g>
        );
      })}
      {/* diving cat still mid-air, smaller */}
      <g transform={`translate(560 ${100 + progress * 300}) scale(${0.8 + progress * 0.4})`} opacity={1 - progress * 0.5}>
        <CatDive size={100} color={stroke}/>
      </g>
      <text x="80" y="100" fill="#ff2e4d" fontFamily="JetBrains Mono" fontSize="14">CITY_ASSEMBLE</text>
      <text x="80" y="140" fill={stroke} fontFamily="Anton" fontSize="56">SEOUL.EXE</text>
      <text x="80" y="170" fill={stroke} fontFamily="JetBrains Mono" fontSize="12" opacity="0.6">{Math.round(progress * 100)}% LOADED</text>
    </svg>
  );
}

// Scene 5: Approaching/entering the building (Layer41)
function SceneBuilding({ progress, theme }) {
  const stroke = theme === 'dark' ? '#f4f1e8' : '#0a0a0a';
  const bg = theme === 'dark' ? '#0a0a0a' : '#ede8dc';
  const zoom = 1 + progress * 8; // rapid zoom into door
  return (
    <svg viewBox="0 0 1200 900" className="scene-svg" preserveAspectRatio="xMidYMid slice">
      <rect width="1200" height="900" fill={bg}/>
      <g transform={`translate(600 450) scale(${zoom}) translate(-600 -450)`}>
        {/* building facade — industrial */}
        <rect x="300" y="200" width="600" height="500" fill={stroke} opacity="0.9"/>
        {/* grid of windows */}
        {Array.from({length: 6}).map((_, row) => (
          Array.from({length: 8}).map((_, col) => (
            <rect key={`${row}-${col}`} x={320 + col * 70} y={220 + row * 70} width="50" height="50" fill={bg} stroke="#ff2e4d" strokeWidth="1"/>
          ))
        ))}
        {/* door — rectangle with glow, centered */}
        <rect x="560" y="550" width="80" height="140" fill="#ff2e4d"/>
        <rect x="568" y="558" width="64" height="124" fill={bg}/>
        <text x="600" y="630" fill="#ff2e4d" fontFamily="Anton" fontSize="14" textAnchor="middle">LAYER41</text>
        {/* graffiti tag on wall */}
        <g transform="translate(340 460)">
          <rect width="110" height="40" fill="#f7d046" opacity="0.9" transform="rotate(-5)"/>
          <text x="8" y="26" fontFamily="Anton" fontSize="22" fill={stroke} transform="rotate(-5)">CHANOIRS</text>
        </g>
      </g>
      {/* vignette darkening as we zoom */}
      <radialGradient id="vig">
        <stop offset="60%" stopColor={bg} stopOpacity="0"/>
        <stop offset="100%" stopColor={bg} stopOpacity={progress}/>
      </radialGradient>
      <rect width="1200" height="900" fill="url(#vig)"/>
    </svg>
  );
}

Object.assign(window, { SceneGlobe, SceneRun, SceneDive, SceneAssemble, SceneBuilding });
