// scenes.jsx — Scenes 1, 2, 3 (Obsidian Ridge design system)
//
// Timeline (100s total):
//  0  – 10   Scene1Hook    — $16.6B counter w/ ambient ticker
// 10  – 23   Scene2AIEmail — AI-perfect wire-fraud email + cursor
// 23  – 44   Scene3Arup    — Hong Kong deepfake video call
// 44  – 60   Scene4Vectors
// 60  – 70   Scene5OneIn3
// 70  – 82   Scene6Fix
// 82  – 94   Scene7Inside  — what's in the program
// 94  – 100  Scene8CTA

// ─── Ambient ticker — small mono lines drifting up behind the counter ────
function AmbientTicker() {
  const t = useTime();
  const items = [
    'Investment fraud · $6.57B',
    'BEC · $2.77B',
    'Tech-support · $1.46B',
    'Personal-data breach · $1.45B',
    'Ransomware · 44% of breaches',
    'Identity theft · 358k complaints',
    'Pig-butchering · $5.8B',
    '+33% YoY',
    'Median time-to-click · 21 s',
    'Elder fraud · $4.88B',
    '989,123 attacks / quarter',
    'AI-generated lures · ↑↑↑',
    'Token theft · +146% YoY',
    'Help-desk vishing · +442%',
  ];
  const totalHeight = 1080;
  const lineSpacing = 110;
  const speed = 22;
  return (
    <div style={{
      position: 'absolute', inset: 0, overflow: 'hidden',
      pointerEvents: 'none', opacity: 0.22,
    }}>
      {items.map((txt, i) => {
        const baseY = i * lineSpacing - 60;
        const y = ((baseY - t * speed) % (totalHeight + 200) + totalHeight + 200) % (totalHeight + 200) - 100;
        const x = (i % 2 === 0) ? 80 : 1920 - 600;
        return (
          <div key={i} style={{
            position: 'absolute', left: x, top: y,
            fontFamily: FF.mono, fontSize: 14, letterSpacing: '0.18em',
            color: OR.inkFaint, textTransform: 'uppercase', whiteSpace: 'nowrap',
          }}>
            <span style={{ color: OR.gold, opacity: 0.5 }}>{String(i + 1).padStart(2, '0')}</span>
            &nbsp;&nbsp;{txt}
          </div>
        );
      })}
    </div>
  );
}

// ─── Faceted ridge silhouette in background ────────────────────────────────
function RidgeSilhouette({ opacity = 0.10 }) {
  return (
    <svg width="1920" height="1080" style={{ position: 'absolute', inset: 0, opacity }} viewBox="0 0 1920 1080" preserveAspectRatio="none">
      {/* low-poly facets */}
      <polygon fill="#1a1410" points="0,820 240,640 380,720 540,580 720,680 880,540 1080,660 1280,580 1420,700 1620,560 1780,680 1920,580 1920,1080 0,1080" />
      <polygon fill="#0a0805" points="0,900 200,820 340,860 500,760 660,840 820,720 980,820 1140,740 1320,820 1500,740 1660,820 1820,760 1920,810 1920,1080 0,1080" />
    </svg>
  );
}

// ═════════════════════════════════════════════════════════════════════════
// Scene 1 — $16.6 BILLION (0 → 10)
// ═════════════════════════════════════════════════════════════════════════
function Scene1Hook() {
  const start = 0, end = 10.0;
  const t = useTime();
  const local = t - start;

  const val = useCountUp({ from: 0, to: 16_600_000_000, start: 1.0, end: 7.0, ease: Easing.easeOutExpo });
  const breathe = 1 + Math.sin(t * 1.4) * 0.004;
  const push = interpolate([0, 9], [1.025, 1.0], Easing.easeOutCubic)(local);

  const eb   = interpolate([0.3, 1.0, 9.4, 9.9], [0, 1, 1, 0], Easing.linear)(local);
  const numO = interpolate([0.6, 1.1, 9.4, 9.9], [0, 1, 1, 0], Easing.linear)(local);
  const sub  = interpolate([7.4, 8.1, 9.4, 9.9], [0, 1, 1, 0], Easing.linear)(local);

  return (
    <Sprite start={start} end={end}>
      <div style={{ position: 'absolute', inset: 0, background: OR.bg, transform: `scale(${push})`, transformOrigin: 'center' }} />
      <RidgeSilhouette />
      <AmbientTicker />

      {/* PCB ornament corners */}
      <div style={{ position: 'absolute', left: 60, top: 60, opacity: eb }}>
        <PCBTrace kind="top-left" size={140} opacity={0.35} />
      </div>
      <div style={{ position: 'absolute', right: 60, top: 60, opacity: eb }}>
        <PCBTrace kind="top-right" size={140} opacity={0.35} />
      </div>

      <div style={{ opacity: eb }}>
        <Eyebrow x={120} y={180}>FBI IC3 · 2024 Annual Report</Eyebrow>
      </div>

      {/* giant counter */}
      <div style={{
        position: 'absolute', left: 0, right: 0, top: 360,
        textAlign: 'center',
        fontFamily: FF.sans,
        fontWeight: 700, fontSize: 230, lineHeight: 0.9,
        color: OR.ink, letterSpacing: '-0.04em',
        fontVariantNumeric: 'tabular-nums',
        opacity: numO,
        transform: `scale(${breathe})`,
        textShadow: '0 0 80px rgba(214,164,95,0.10)',
      }}>
        {fmtMoney(val)}
      </div>

      <div style={{
        position: 'absolute', left: 0, right: 0, top: 640,
        textAlign: 'center',
        fontFamily: FF.mono,
        fontSize: 22, letterSpacing: '0.36em',
        color: OR.inkDim, textTransform: 'uppercase',
        opacity: numO,
      }}>
        U.S. losses to internet crime · one year
      </div>

      {/* subtitle — Cinzel monumental */}
      <div style={{
        position: 'absolute', left: 0, right: 0, top: 800,
        textAlign: 'center',
        fontFamily: FF.display,
        fontWeight: 600, fontSize: 56,
        color: OR.gold, letterSpacing: '0.12em',
        textTransform: 'uppercase',
        opacity: sub,
      }}>
        A 33% jump · in a single year
      </div>

      <SourceChip text="Source · FBI Internet Crime Complaint Center" x={120} y={1000} />
    </Sprite>
  );
}

// ═════════════════════════════════════════════════════════════════════════
// Scene 2 — The AI-perfect email (10 → 23)
// ═════════════════════════════════════════════════════════════════════════
function Scene2AIEmail() {
  const start = 10.0, end = 23.0;
  const t = useTime();
  const local = t - start;

  const cardY = interpolate([0, 0.9, 12.0, 12.8], [80, 0, 0, -32], Easing.easeOutCubic)(local);
  const cardO = interpolate([0, 0.7, 12.0, 12.8], [0, 1, 1, 0], Easing.linear)(local);

  const fullBody = `Hi Sarah — quick favor before I board.\nI need you to wire the deposit for the\nNovaPharm acquisition today. Wire details\nattached. Keep this confidential for now,\nlegal is finalizing the press release.\n\nThanks,\nMichael`;
  const chars = Math.floor(interpolate([1.4, 7.0], [0, fullBody.length], Easing.linear)(local));
  const shown = fullBody.slice(0, Math.max(0, chars));

  const tagO = interpolate([8.5, 9.2, 12.0, 12.6], [0, 1, 1, 0], Easing.linear)(local);

  // cursor lands just BELOW the typo so it doesn't block the text
  const cx = interpolate([7.0, 8.4], [1700, 740], Easing.easeInOutCubic)(local);
  const cy = interpolate([7.0, 8.4], [900,  402], Easing.easeInOutCubic)(local);
  const cursorO = interpolate([6.8, 7.2, 12.0, 12.4], [0, 1, 1, 0], Easing.linear)(local);

  return (
    <Sprite start={start} end={end}>
      <div style={{ position: 'absolute', inset: 0, background: OR.bg }} />

      <div style={{ opacity: cardO * 0.9 }}>
        <Eyebrow x={120} y={140}>Inbox · 09:47 AM · CEO</Eyebrow>
      </div>

      <div style={{
        position: 'absolute',
        left: 200, top: 220 + cardY,
        width: 1520, minHeight: 660,
        background: '#0b0b0b',
        border: `1px solid ${OR.line}`,
        borderRadius: 4,
        boxShadow: '0 30px 80px rgba(0,0,0,0.8)',
        opacity: cardO,
        padding: '36px 56px 56px',
        fontFamily: FF.sans,
        color: OR.ink,
      }}>
        <div style={{ display: 'flex', alignItems: 'baseline', gap: 18, marginBottom: 28, borderBottom: `1px solid ${OR.line}`, paddingBottom: 22 }}>
          <div style={{ fontSize: 36, fontWeight: 600, letterSpacing: '-0.01em', flex: 1 }}>
            Wire confirmation needed — confidential
          </div>
          <div style={{ fontFamily: FF.mono, fontSize: 14, color: OR.gold, letterSpacing: '0.22em', textTransform: 'uppercase' }}>
            ◆ Flagged
          </div>
        </div>

        <div style={{ display: 'flex', gap: 20, marginBottom: 8, fontSize: 22 }}>
          <div style={{ width: 100, color: OR.inkDim }}>From</div>
          <div style={{ color: OR.ink, fontWeight: 500 }}>
            Michael Chen, CEO
            <span style={{ color: OR.inkFaint, marginLeft: 12, fontFamily: FF.mono, fontSize: 18 }}>
              &lt;mchen@northhriver-corp.com&gt;
            </span>
          </div>
        </div>
        <div style={{ display: 'flex', gap: 20, marginBottom: 32, fontSize: 22 }}>
          <div style={{ width: 100, color: OR.inkDim }}>To</div>
          <div style={{ color: OR.ink }}>Sarah Pham, Controller</div>
        </div>

        <div style={{
          fontSize: 30, lineHeight: 1.5,
          color: OR.ink, whiteSpace: 'pre-wrap',
          minHeight: 360,
        }}>
          {shown}
          <span style={{
            display: 'inline-block', width: 12, height: 32, marginLeft: 2,
            background: OR.gold, verticalAlign: 'text-bottom',
            opacity: Math.sin(t * 8) > 0 ? 1 : 0,
          }} />
        </div>
      </div>

      {/* circle on the typo — arrow only shown until the cursor arrives, then fades. */}
      <svg width="1920" height="1080" style={{
        position: 'absolute', inset: 0, opacity: tagO, pointerEvents: 'none',
      }}>
        <ellipse cx="713" cy="365" rx="74" ry="20" stroke={OR.gold} strokeWidth="2.2" fill="none" />
      </svg>

      <div style={{
        position: 'absolute', left: 920, top: 345,
        opacity: tagO,
        transform: `translate(${(1 - tagO) * 12}px, -50%)`,
        marginTop: 8,
      }}>
        <div style={{
          background: OR.gold, color: '#0b0b0b',
          fontFamily: FF.mono,
          fontSize: 16, fontWeight: 600, letterSpacing: '0.14em',
          padding: '10px 16px', borderRadius: 0,
          textTransform: 'uppercase',
          boxShadow: '0 10px 30px rgba(0,0,0,0.5)',
          whiteSpace: 'nowrap',
        }}>
          northhriver — not northriver
        </div>
        {/* arrow from tag to ellipse — only visible BEFORE cursor arrives */}
        <svg width="160" height="3" style={{
          position: 'absolute', left: -134, top: 22,
          opacity: 1 - cursorO,
        }}>
          <path d="M 160 1 L 16 1" stroke={OR.gold} strokeWidth="2.4" fill="none" />
          <polygon points="16,-4 4,1 16,6" fill={OR.gold} />
        </svg>
      </div>

      <div style={{ opacity: cursorO }}>
        <CursorTip x={cx} y={cy} size={32} />
      </div>

      <SourceChip text="Source · Verizon 2025 DBIR" x={120} y={1000} />
    </Sprite>
  );
}

// ═════════════════════════════════════════════════════════════════════════
// Scene 3 — Hong Kong deepfake (23 → 44)
// ═════════════════════════════════════════════════════════════════════════
function Scene3Arup() {
  const start = 23.0, end = 44.0;
  const t = useTime();
  const local = t - start;

  const locO       = interpolate([0.2, 1.0, 2.4, 3.0], [0, 1, 1, 0], Easing.linear)(local);
  const tileShow   = (i) => {
    const a = 3.2 + i * 0.85;
    return interpolate([a, a + 0.7], [0, 1], Easing.easeOutCubic)(local);
  };
  const tilesWrapO = interpolate([3.2, 6.8, 17.0, 18.4], [0, 1, 1, 0], Easing.linear)(local);
  const tileDim    = interpolate([11.5, 12.2, 15.6, 16.0], [1, 0.18, 0.18, 1], Easing.linear)(local);

  const capRec     = interpolate([6.8, 7.5, 10.6, 11.3], [0, 1, 1, 0], Easing.linear)(local);

  const wireO      = interpolate([11.8, 12.6, 15.6, 16.0], [0, 1, 1, 0], Easing.linear)(local);
  const wireVal    = useCountUp({ from: 0, to: 25_600_000, start: start + 12.4, end: start + 14.4, ease: Easing.easeOutExpo });

  const glitch     = interpolate([16.0, 17.8], [0, 1], Easing.easeInQuad)(local);
  const noiseO     = interpolate([17.6, 18.3, 18.4, 18.6], [0, 1, 1, 0], Easing.linear)(local);

  const finalO     = interpolate([18.4, 19.0, 20.4, 20.8], [0, 1, 1, 0], Easing.linear)(local);

  const portraits = [
    { initials: 'MK', label: 'Michael Kwok', role: 'CFO · UK',          hue: 38  },
    { initials: 'DL', label: 'David Lin',    role: 'Finance · HK',      hue: 22  },
    { initials: 'PS', label: 'Priya Shah',   role: 'Legal',             hue: 45  },
    { initials: 'TR', label: 'Tom Reilly',   role: 'Director · M&A',    hue: 30  },
  ];

  const tileGeom = [
    { x: 440,  y: 200, w: 480, h: 320 },
    { x: 1000, y: 200, w: 480, h: 320 },
    { x: 440,  y: 540, w: 480, h: 320 },
    { x: 1000, y: 540, w: 480, h: 320 },
  ];

  return (
    <Sprite start={start} end={end}>
      <div style={{ position: 'absolute', inset: 0, background: OR.bg }} />

      {/* location title */}
      <div style={{
        position: 'absolute', left: 0, right: 0, top: 420,
        textAlign: 'center', opacity: locO,
      }}>
        <div style={{
          fontFamily: FF.mono,
          fontSize: 22, letterSpacing: '0.4em', color: OR.gold,
          textTransform: 'uppercase', marginBottom: 24,
        }}>
          ▌ Case File · ARUP
        </div>
        <div style={{
          fontFamily: FF.display,
          fontWeight: 700, fontSize: 140, color: OR.ink,
          letterSpacing: '0.06em', lineHeight: 1,
          textTransform: 'uppercase',
        }}>
          Hong Kong · 2024
        </div>
        <div style={{
          fontFamily: FF.italic, fontStyle: 'italic',
          fontSize: 40, fontWeight: 400, color: OR.inkDim, marginTop: 32,
          letterSpacing: '-0.005em',
        }}>
          A finance employee joins a video call.
        </div>
      </div>

      {/* call-frame eyebrow */}
      <div style={{
        position: 'absolute', left: 440, top: 140,
        opacity: Math.min(tilesWrapO, 1) * 0.9,
        display: 'flex', alignItems: 'center', gap: 14,
        fontFamily: FF.mono,
        fontSize: 15, letterSpacing: '0.24em',
        color: OR.inkDim, textTransform: 'uppercase',
      }}>
        <span style={{ width: 8, height: 8, borderRadius: 0, background: OR.gold, boxShadow: `0 0 10px ${OR.gold}`, transform: 'rotate(45deg)' }} />
        Live · Internal · Acquisition wire review
      </div>

      <div style={{ opacity: tilesWrapO }}>
        {portraits.map((p, i) => {
          const g = tileGeom[i];
          return (
            <div key={i} style={{ opacity: tileShow(i) * tileDim }}>
              <PortraitTile
                x={g.x} y={g.y} w={g.w} h={g.h}
                label={p.label} role={p.role}
                initials={p.initials} hue={p.hue}
                glitch={glitch}
                talking={glitch < 0.4}
              />
            </div>
          );
        })}
      </div>

      <div style={{
        position: 'absolute', left: 0, right: 0, top: 905,
        textAlign: 'center', opacity: capRec,
      }}>
        <div style={{
          fontFamily: FF.italic, fontStyle: 'italic',
          fontSize: 42, color: OR.ink, letterSpacing: '-0.005em',
        }}>
          He recognized every face on the call.
        </div>
      </div>

      {noiseO > 0 && (
        <div style={{
          position: 'absolute', left: 440, top: 200, width: 1040, height: 660,
          opacity: noiseO,
          backgroundImage: "url(\"data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='200' height='200'><filter id='n'><feTurbulence baseFrequency='1.4' numOctaves='2' stitchTiles='stitch' seed='3'/><feColorMatrix values='0 0 0 0 0.84 0 0 0 0 0.64 0 0 0 0 0.37 0 0 0 1 0'/></filter><rect width='100%' height='100%' filter='url(%23n)'/></svg>\")",
          backgroundSize: '200px 200px',
          mixBlendMode: 'screen',
        }} />
      )}

      {/* wire transfer counter */}
      <div style={{
        position: 'absolute', left: 0, right: 0, top: 400,
        textAlign: 'center', opacity: wireO,
      }}>
        <div style={{
          fontFamily: FF.mono,
          fontSize: 20, letterSpacing: '0.36em', color: OR.gold,
          textTransform: 'uppercase', marginBottom: 22,
        }}>
          15 wire transfers · one day
        </div>
        <div style={{
          fontFamily: FF.sans,
          fontWeight: 700, fontSize: 200, color: OR.ink,
          letterSpacing: '-0.04em', lineHeight: 1,
          fontVariantNumeric: 'tabular-nums',
          textShadow: '0 4px 40px rgba(0,0,0,0.8)',
        }}>
          {fmtMoney(wireVal)}
        </div>
      </div>

      {/* FINAL stark line */}
      <div style={{
        position: 'absolute', left: 0, right: 0, top: 380,
        textAlign: 'center', opacity: finalO,
      }}>
        <div style={{
          fontFamily: FF.mono,
          fontSize: 22, letterSpacing: '0.4em', color: OR.gold,
          textTransform: 'uppercase', marginBottom: 36,
        }}>
          ◣ The realization
        </div>
        <div style={{
          fontFamily: FF.display,
          fontWeight: 700, fontSize: 130, color: OR.ink,
          letterSpacing: '0.04em', lineHeight: 1.0,
          textTransform: 'uppercase',
        }}>
          Every face<br/>
          <span style={{ color: OR.gold }}>was a deepfake.</span>
        </div>
      </div>

      <SourceChip text="CNN · Hong Kong Police · 2024" x={120} y={1000} />
    </Sprite>
  );
}

Object.assign(window, { Scene1Hook, Scene2AIEmail, Scene3Arup });
