// scenes-intro.jsx — Scene 1 (intro) and Scene 2 (walker)

// ─────────────────────────────────────────────────────────────────────────────
// Shared palette (declared once, used everywhere)
// ─────────────────────────────────────────────────────────────────────────────
const PAL = {
  paper: '#f3ede0',         // warm cream
  paperDeep: '#e8e0cf',
  ink: '#1a1814',           // warm black
  inkSoft: '#5a544a',
  pop: 'oklch(64% 0.16 38)',  // terracotta
  popSoft: 'oklch(82% 0.07 38)',
  leaf: 'oklch(58% 0.09 145)', // muted green
  sky: 'oklch(82% 0.04 230)',  // pale blue
};

const FONT_DISPLAY = '"Newsreader", "Times New Roman", serif';
const FONT_MONO = '"JetBrains Mono", ui-monospace, monospace';
const FONT_SANS = '"Inter", system-ui, sans-serif';

// ─────────────────────────────────────────────────────────────────────────────
// SCENE 1 — Intro title card (0 → 3s)
// ─────────────────────────────────────────────────────────────────────────────
function SceneIntro() {
  const { localTime, progress } = useSprite();
  const L = useLang();

  // Wordmark drops in
  const titleY = animate({ from: -40, to: 0, start: 0, end: 0.6, ease: Easing.easeOutBack })(localTime);
  const titleOpacity = animate({ from: 0, to: 1, start: 0, end: 0.5 })(localTime);

  // Underline draws
  const lineWidth = animate({ from: 0, to: 360, start: 0.5, end: 1.1, ease: Easing.easeOutCubic })(localTime);

  // Subtitle reveals
  const subY = animate({ from: 12, to: 0, start: 0.7, end: 1.2, ease: Easing.easeOutCubic })(localTime);
  const subOp = animate({ from: 0, to: 1, start: 0.7, end: 1.2 })(localTime);

  // Tag pills
  const pill1 = animate({ from: 0, to: 1, start: 1.1, end: 1.5 })(localTime);
  const pill2 = animate({ from: 0, to: 1, start: 1.3, end: 1.7 })(localTime);
  const pill3 = animate({ from: 0, to: 1, start: 1.5, end: 1.9 })(localTime);

  // Floating word ornaments
  const wordsT = localTime;

  // Exit
  const exitT = animate({ from: 0, to: 1, start: 2.6, end: 3.0, ease: Easing.easeInCubic })(localTime);
  const exitY = -exitT * 30;
  const exitOp = 1 - exitT;

  return (
    <div style={{
      position: 'absolute', inset: 0,
      background: PAL.paper,
      overflow: 'hidden',
      opacity: exitOp,
      transform: `translateY(${exitY}px)`,
    }}>
      {/* Paper grain dots */}
      <Grain />

      {/* Floating German words in background */}
      <FloatingWord text="der" x={180} y={140} delay={0.2} t={wordsT} color={PAL.popSoft} size={64} rot={-8} />
      <FloatingWord text="die" x={1500} y={180} delay={0.35} t={wordsT} color={PAL.popSoft} size={64} rot={6} />
      <FloatingWord text="das" x={1620} y={820} delay={0.5} t={wordsT} color={PAL.popSoft} size={64} rot={-4} />
      <FloatingWord text="Hallo" x={140} y={840} delay={0.6} t={wordsT} color={PAL.popSoft} size={56} rot={5} />
      <FloatingWord text="Brot" x={1400} y={500} delay={0.75} t={wordsT} color={PAL.popSoft} size={48} rot={-3} />
      <FloatingWord text="Wald" x={240} y={520} delay={0.85} t={wordsT} color={PAL.popSoft} size={48} rot={4} />

      {/* Center stack */}
      <div style={{
        position: 'absolute',
        left: '50%',
        top: '50%',
        transform: 'translate(-50%, -50%)',
        textAlign: 'center',
      }}>
        {/* Vokki logo (real icon) above wordmark */}
        <div style={{
          width: 200, height: 200,
          margin: '0 auto 16px',
          opacity: titleOpacity,
          transform: `translateY(${titleY}px)`,
        }}>
          <img src="vokki-icon.png" alt="Vokki" style={{
            width: '100%', height: '100%',
            objectFit: 'contain',
            display: 'block',
            borderRadius: 36,
          }} />
        </div>
        {/* Wordmark */}
        <div style={{
          fontFamily: FONT_DISPLAY,
          fontSize: 220,
          fontWeight: 500,
          fontStyle: 'italic',
          color: PAL.ink,
          letterSpacing: '-0.04em',
          lineHeight: 0.9,
          opacity: titleOpacity,
          transform: `translateY(${titleY}px)`,
        }}>
          {L.intro_title}
        </div>

        {/* Underline */}
        <div style={{
          height: 4,
          width: lineWidth,
          background: PAL.pop,
          margin: '12px auto 0',
          borderRadius: 2,
        }} />

        {/* Subtitle */}
        <div style={{
          fontFamily: FONT_MONO,
          fontSize: 22,
          color: PAL.inkSoft,
          marginTop: 32,
          opacity: subOp,
          transform: `translateY(${subY}px)`,
          letterSpacing: '0.12em',
          textTransform: 'uppercase',
        }}>
          {L.intro_sub}
        </div>

        {/* Pills */}
        <div style={{
          display: 'flex', gap: 12, justifyContent: 'center',
          marginTop: 36,
        }}>
          <Pill text={L.pill_bilingual} opacity={pill1} />
          <Pill text={L.pill_audio} opacity={pill2} />
          <Pill text={L.pill_stories} opacity={pill3} />
        </div>
      </div>
    </div>
  );
}

function Pill({ text, opacity }) {
  return (
    <div style={{
      padding: '10px 20px',
      border: `1.5px solid ${PAL.ink}`,
      borderRadius: 999,
      fontFamily: FONT_SANS,
      fontSize: 18,
      fontWeight: 500,
      color: PAL.ink,
      opacity,
      transform: `scale(${0.7 + opacity * 0.3})`,
      background: PAL.paper,
    }}>
      {text}
    </div>
  );
}

function FloatingWord({ text, x, y, delay, t, color, size = 48, rot = 0 }) {
  const op = animate({ from: 0, to: 1, start: delay, end: delay + 0.4 })(t);
  const drift = Math.sin((t - delay) * 1.5) * 6;
  return (
    <div style={{
      position: 'absolute',
      left: x, top: y,
      fontFamily: FONT_DISPLAY,
      fontStyle: 'italic',
      fontSize: size,
      color,
      opacity: op,
      transform: `rotate(${rot}deg) translateY(${drift}px)`,
      pointerEvents: 'none',
    }}>
      {text}
    </div>
  );
}

function Grain() {
  // CSS-only paper texture — repeating tiny dots
  return (
    <div style={{
      position: 'absolute', inset: 0,
      backgroundImage: 'radial-gradient(circle at 1px 1px, rgba(26,24,20,0.06) 1px, transparent 0)',
      backgroundSize: '4px 4px',
      pointerEvents: 'none',
    }} />
  );
}

function FoxMascot({ t = 0, dark = false }) {
  // Real Vokki — orange head, dark inner ears, V-tuft, big sparkle eyes,
  // white snout/cheeks, tiny smile with fang outline.
  const wag = Math.sin(t * 4) * 3;
  const blinkPhase = (t * 0.7) % 4;
  const blink = blinkPhase > 3.85;
  return <VokkiFace t={t} wag={wag} blink={blink} />;
}

function VokkiFace({ t = 0, wag = 0, blink = false }) {
  const FOX = '#ee6a2c';        // bright orange (matches icon)
  const FOX_DEEP = '#3d2a5c';   // deep purple-navy inner ear
  const WHITE = '#fdfaf2';
  const INK = '#1f1736';
  const CHEEK = '#f6c280';      // warm tan
  const BROW = '#c84d18';
  const V_BLUE = '#4258bd';
  const eyeY = blink ? 0 : 1;
  const eyeH = blink ? 1 : 7;
  const eyeRY = blink ? 0.5 : 9;
  const eyeOff = blink ? 0 : 0;

  return (
    <svg viewBox="0 0 200 200" width="100%" height="100%" style={{ display: 'block', overflow: 'visible' }}>
      {/* LEFT EAR — orange triangle with dark navy inner */}
      <g transform={`rotate(${wag * 0.3} 60 60)`}>
        <path d="M 38 38 L 56 14 L 78 70 Z" fill={FOX} />
        <path d="M 52 42 L 60 26 L 72 64 Z" fill={FOX_DEEP} />
      </g>
      {/* RIGHT EAR */}
      <g transform={`rotate(${-wag * 0.3} 140 60)`}>
        <path d="M 162 38 L 144 14 L 122 70 Z" fill={FOX} />
        <path d="M 148 42 L 140 26 L 128 64 Z" fill={FOX_DEEP} />
      </g>

      {/* V-TUFT on forehead — blue */}
      <path d="M 88 36 L 100 60 L 112 36 L 106 36 L 100 50 L 94 36 Z"
        fill={V_BLUE} />

      {/* HEAD — domed orange */}
      <path
        d="M 40 100
           Q 40 50 100 50
           Q 160 50 160 100
           L 160 130
           Q 160 145 145 145
           L 55 145
           Q 40 145 40 130 Z"
        fill={FOX}
      />

      {/* BROW shadows — slightly darker orange */}
      <ellipse cx="74" cy="98" rx="14" ry="6" fill={BROW} opacity="0.7" />
      <ellipse cx="126" cy="98" rx="14" ry="6" fill={BROW} opacity="0.7" />

      {/* EYE PATCHES — tan/cream rounded shapes around eyes */}
      <ellipse cx="74" cy="112" rx="16" ry="14" fill={CHEEK} />
      <ellipse cx="126" cy="112" rx="16" ry="14" fill={CHEEK} />

      {/* WHITE SNOUT/CHEEK area — wide rounded shape covering bottom of head */}
      <path
        d="M 100 110
           Q 56 110 50 138
           Q 46 158 64 168
           Q 82 176 100 174
           Q 118 176 136 168
           Q 154 158 150 138
           Q 144 110 100 110 Z"
        fill={WHITE}
      />

      {/* EYES — big dark ovals */}
      <ellipse cx="74" cy={114 + eyeOff} rx="6.5" ry={eyeRY} fill={INK} />
      <ellipse cx="126" cy={114 + eyeOff} rx="6.5" ry={eyeRY} fill={INK} />
      {/* Eye sparkles — two per eye (big + small) */}
      {!blink && (
        <>
          <circle cx="76.5" cy="111" r="2.4" fill={WHITE} />
          <circle cx="71" cy="117" r="1.1" fill={WHITE} />
          <circle cx="128.5" cy="111" r="2.4" fill={WHITE} />
          <circle cx="123" cy="117" r="1.1" fill={WHITE} />
        </>
      )}

      {/* NOSE — dark rounded triangle */}
      <path d="M 100 138
               Q 92 138 92 144
               Q 92 150 100 150
               Q 108 150 108 144
               Q 108 138 100 138 Z"
        fill={INK} />

      {/* MOUTH — fox M-smile */}
      <path
        d="M 100 150 L 100 156
           M 100 156 Q 100 164 90 164 Q 86 164 84 161
           M 100 156 Q 100 164 110 164 Q 114 164 116 161"
        stroke={INK} strokeWidth="2.5" fill="none" strokeLinecap="round" strokeLinejoin="round"
      />
    </svg>
  );
}

// ─────────────────────────────────────────────────────────────────────────────
// SCENE 2 — Walker in the park (3 → 7s) [localTime 0..4]
// ─────────────────────────────────────────────────────────────────────────────
function SceneWalker() {
  const { localTime } = useSprite();
  const L = useLang();
  const t = localTime;

  // Walker walks left → middle, holds, walks off right
  const walkerX = interpolate(
    [0, 0.6, 3.4, 4.0],
    [-200, 760, 760, 2000],
    Easing.easeInOutCubic
  )(t);

  // Bounce while walking
  const bouncing = (t < 0.6 || t > 3.4);
  const bounce = bouncing ? Math.abs(Math.sin(t * 12)) * 8 : Math.abs(Math.sin(t * 4)) * 2;

  // Speech bubble pair appears after walker arrives
  const bubble1Op = animate({ from: 0, to: 1, start: 0.9, end: 1.3, ease: Easing.easeOutBack })(t);
  const bubble2Op = animate({ from: 0, to: 1, start: 1.5, end: 1.9, ease: Easing.easeOutBack })(t);
  const bubble3Op = animate({ from: 0, to: 1, start: 2.1, end: 2.5, ease: Easing.easeOutBack })(t);
  const bubble4Op = animate({ from: 0, to: 1, start: 2.7, end: 3.1, ease: Easing.easeOutBack })(t);

  // Bubbles fade as walker leaves
  const bubblesFade = animate({ from: 1, to: 0, start: 3.3, end: 3.7 })(t);

  // Sun rises slightly
  const sunY = animate({ from: 220, to: 200, start: 0, end: 4, ease: Easing.linear })(t);

  // Caption
  const capOp = animate({ from: 0, to: 1, start: 0.4, end: 0.9 })(t) * animate({ from: 1, to: 0, start: 3.2, end: 3.7 })(t);

  return (
    <div style={{
      position: 'absolute', inset: 0,
      background: `linear-gradient(180deg, ${PAL.sky} 0%, ${PAL.paper} 60%)`,
      overflow: 'hidden',
    }}>
      <Grain />

      {/* Sun */}
      <div style={{
        position: 'absolute',
        left: 1440, top: sunY,
        width: 140, height: 140,
        background: PAL.popSoft,
        borderRadius: '50%',
        boxShadow: `0 0 80px ${PAL.popSoft}`,
      }} />

      {/* Distant hills */}
      <svg style={{ position: 'absolute', left: 0, bottom: 240, width: '100%', height: 220 }} viewBox="0 0 1920 220" preserveAspectRatio="none">
        <path d="M0,220 L0,140 Q200,60 480,120 Q760,180 1040,80 Q1320,0 1600,100 Q1800,160 1920,120 L1920,220 Z" fill={PAL.paperDeep} opacity="0.7"/>
      </svg>

      {/* River — winding through middle */}
      <svg style={{ position: 'absolute', left: 0, top: 580, width: '100%', height: 90, pointerEvents: 'none' }}
        viewBox="0 0 1920 90" preserveAspectRatio="none">
        <path d="M 0 60 Q 240 40 480 55 Q 720 70 960 50 Q 1200 30 1440 55 Q 1680 75 1920 50 L 1920 90 L 0 90 Z"
          fill="oklch(78% 0.06 220)" opacity="0.55" />
        <path d="M 0 64 Q 240 44 480 59 Q 720 74 960 54 Q 1200 34 1440 59 Q 1680 79 1920 54"
          stroke="oklch(70% 0.08 220)" strokeWidth="2" fill="none" opacity="0.5" />
        {/* ripples */}
        <ellipse cx="200" cy="70" rx="22" ry="2" fill="oklch(92% 0.02 220)" opacity="0.6" />
        <ellipse cx="640" cy="64" rx="18" ry="2" fill="oklch(92% 0.02 220)" opacity="0.6" />
        <ellipse cx="1100" cy="60" rx="22" ry="2" fill="oklch(92% 0.02 220)" opacity="0.6" />
        <ellipse cx="1500" cy="68" rx="18" ry="2" fill="oklch(92% 0.02 220)" opacity="0.6" />
      </svg>

      {/* Trees row — back (more) */}
      <Tree x={60} y={620} h={130} color={PAL.leaf} dim={0.65}/>
      <Tree x={220} y={640} h={150} color={PAL.leaf} dim={0.7}/>
      <Tree x={400} y={630} h={120} color={PAL.leaf} dim={0.7}/>
      <Tree x={560} y={650} h={135} color={PAL.leaf} dim={0.7}/>
      <Tree x={760} y={640} h={140} color={PAL.leaf} dim={0.65}/>
      <Tree x={1080} y={630} h={130} color={PAL.leaf} dim={0.7}/>
      <Tree x={1280} y={650} h={150} color={PAL.leaf} dim={0.7}/>
      <Tree x={1460} y={620} h={140} color={PAL.leaf} dim={0.7}/>
      <Tree x={1640} y={640} h={130} color={PAL.leaf} dim={0.7}/>
      <Tree x={1800} y={630} h={150} color={PAL.leaf} dim={0.65}/>

      {/* Ground */}
      <div style={{
        position: 'absolute',
        left: 0, right: 0, bottom: 0,
        height: 240,
        background: PAL.paperDeep,
      }}>
        {/* Path */}
        <div style={{
          position: 'absolute',
          left: 0, right: 0, top: 80,
          height: 6,
          background: `repeating-linear-gradient(90deg, ${PAL.inkSoft} 0 24px, transparent 24px 48px)`,
          opacity: 0.3,
        }} />
      </div>

      {/* Trees row — front (more) */}
      <Tree x={40} y={680} h={200} color={PAL.leaf}/>
      <Tree x={340} y={690} h={170} color={PAL.leaf}/>
      <Tree x={1180} y={685} h={185} color={PAL.leaf}/>
      <Tree x={1500} y={690} h={175} color={PAL.leaf}/>
      <Tree x={1820} y={690} h={190} color={PAL.leaf}/>

      {/* Walker */}
      <Walker x={walkerX} y={620 - bounce} t={t} />

      {/* Speech bubbles */}
      <Bubble x={walkerX + 180} y={420} text={L.walker_de1} lang="DE" op={bubble1Op * bubblesFade} pop />
      <Bubble x={walkerX + 360} y={460} text={L.en1} lang={L.lang_label || 'EN'} op={bubble2Op * bubblesFade} />
      <Bubble x={walkerX + 200} y={300} text={L.walker_de2} lang="DE" op={bubble3Op * bubblesFade} pop />
      <Bubble x={walkerX + 380} y={340} text={L.en2} lang={L.lang_label || 'EN'} op={bubble4Op * bubblesFade} />

      {/* Caption */}
      <div style={{
        position: 'absolute',
        left: 80, bottom: 80,
        fontFamily: FONT_DISPLAY,
        fontStyle: 'italic',
        fontSize: 56,
        color: PAL.ink,
        opacity: capOp,
        maxWidth: 600,
        lineHeight: 1.05,
      }}>
        {L.walker_caption_l1}<br/>{L.walker_caption_l2}
      </div>
    </div>
  );
}

function Tree({ x, y, h, color, dim = 1 }) {
  return (
    <div style={{ position: 'absolute', left: x, top: y - h, opacity: dim }}>
      {/* trunk */}
      <div style={{
        position: 'absolute',
        left: '50%', bottom: 0,
        transform: 'translateX(-50%)',
        width: h * 0.08,
        height: h * 0.4,
        background: PAL.inkSoft,
      }} />
      {/* foliage = stacked circles */}
      <div style={{
        position: 'absolute',
        left: '50%', bottom: h * 0.3,
        transform: 'translateX(-50%)',
        width: h * 0.9,
        height: h * 0.9,
        background: color,
        borderRadius: '50%',
      }} />
      <div style={{
        position: 'absolute',
        left: '30%', bottom: h * 0.5,
        width: h * 0.55,
        height: h * 0.55,
        background: color,
        borderRadius: '50%',
        opacity: 0.85,
      }} />
    </div>
  );
}

function Walker({ x, y, t }) {
  // Person with headphones — back to the human walker.
  const swing = Math.sin(t * 14) * 22;
  const headBob = Math.sin(t * 14) * 2;
  const PAPER = PAL.paper;
  const INK = PAL.ink;
  const SKIN = 'oklch(78% 0.06 50)';
  const HAIR = 'oklch(28% 0.04 50)';
  const COAT = 'oklch(48% 0.08 250)';
  const PANTS = 'oklch(38% 0.04 250)';
  const HP = INK;
  const HP_PAD = PAL.pop;

  return (
    <div style={{ position: 'absolute', left: x, top: y, width: 1, height: 1 }}>
      <svg viewBox="-100 -200 200 210" width="200" height="210" style={{
        position: 'absolute', left: -100, top: -200, overflow: 'visible',
      }}>
        {/* Back leg */}
        <rect x="-14" y="-50" width="14" height="50" rx="4" fill={PANTS}
          transform={`rotate(${-swing} -7 -50)`} />
        {/* Front leg */}
        <rect x="0" y="-50" width="14" height="50" rx="4" fill={PANTS}
          transform={`rotate(${swing} 7 -50)`} />
        {/* Shoes */}
        <ellipse cx="-7" cy="0" rx="12" ry="5" fill={INK}
          transform={`rotate(${-swing} -7 -50)`} />
        <ellipse cx="7" cy="0" rx="12" ry="5" fill={INK}
          transform={`rotate(${swing} 7 -50)`} />

        {/* Coat / torso */}
        <path d="M -32 -130 Q -32 -140 -22 -142 L 22 -142 Q 32 -140 32 -130 L 36 -50 Q 36 -44 30 -44 L -30 -44 Q -36 -44 -36 -50 Z"
          fill={COAT} />

        {/* Scarf */}
        <path d="M -22 -142 Q 0 -134 22 -142 L 24 -132 Q 0 -126 -24 -132 Z"
          fill={PAL.pop} />

        {/* Arm swinging */}
        <rect x="22" y="-130" width="13" height="50" rx="6" fill={COAT}
          transform={`rotate(${swing * 0.6} 28 -130)`} />
        {/* Other arm tucked */}
        <rect x="-35" y="-130" width="13" height="50" rx="6" fill={COAT}
          transform={`rotate(${-swing * 0.6} -28 -130)`} />

        {/* Neck */}
        <rect x="-7" y="-152" width="14" height="14" fill={SKIN} />

        {/* Head */}
        <g transform={`translate(0 ${headBob})`}>
          <ellipse cx="0" cy="-170" rx="26" ry="28" fill={SKIN} />
          {/* Hair */}
          <path d="M -26 -180 Q -26 -200 0 -200 Q 26 -200 26 -180 Q 26 -172 22 -168 Q 16 -176 0 -176 Q -16 -176 -22 -168 Q -26 -172 -26 -180 Z"
            fill={HAIR} />
          {/* Ear (just one visible) */}
          <ellipse cx="-22" cy="-168" rx="3" ry="5" fill={SKIN} />

          {/* Eyes — both visible, ¾ profile */}
          <circle cx="-4" cy="-170" r="2" fill={INK} />
          <circle cx="10" cy="-170" r="2" fill={INK} />
          {/* Smile */}
          <path d="M -2 -160 Q 6 -156 14 -160" stroke={INK} strokeWidth="1.6" fill="none" strokeLinecap="round" />

          {/* HEADPHONES — band + cup */}
          <path d="M -28 -188 Q 0 -210 28 -188"
            stroke={HP} strokeWidth="6" fill="none" strokeLinecap="round" />
          {/* Left cup (visible side) */}
          <ellipse cx="-26" cy="-170" rx="8" ry="11" fill={HP} />
          <ellipse cx="-26" cy="-170" rx="5" ry="8" fill={HP_PAD} />
          {/* Right cup peek */}
          <ellipse cx="26" cy="-170" rx="6" ry="9" fill={HP} />
        </g>
      </svg>
    </div>
  );
}

function Bubble({ x, y, text, lang, op, pop = false }) {
  return (
    <div style={{
      position: 'absolute',
      left: x, top: y,
      opacity: op,
      transform: `scale(${0.7 + op * 0.3})`,
      transformOrigin: 'bottom left',
    }}>
      <div style={{
        background: pop ? PAL.ink : PAL.paper,
        color: pop ? PAL.paper : PAL.ink,
        border: `2px solid ${PAL.ink}`,
        borderRadius: '20px 20px 20px 4px',
        padding: '14px 22px',
        fontFamily: FONT_DISPLAY,
        fontSize: 38,
        fontStyle: 'italic',
        position: 'relative',
        whiteSpace: 'nowrap',
      }}>
        <span style={{
          position: 'absolute',
          top: -10, left: 14,
          fontFamily: FONT_MONO,
          fontSize: 11,
          letterSpacing: '0.12em',
          background: pop ? PAL.pop : PAL.ink,
          color: PAL.paper,
          padding: '2px 8px',
          borderRadius: 999,
          fontStyle: 'normal',
        }}>{lang}</span>
        {text}
      </div>
    </div>
  );
}

Object.assign(window, { SceneIntro, SceneWalker, PAL, FONT_DISPLAY, FONT_MONO, FONT_SANS, Grain });
