// scenes-1-3.jsx — Hook, Definition, Formula
// Globals: React, Easing, clamp, interpolate, animate, Sprite, useSprite, useTime, useTimeline
//          PlayingCard, ChipStack, ChipPile, Pill, Caption

// ═════════════════════════════════════════════════════════════════════════════
// SCENE 1 — HOOK (0 → 14s)
// "¿Cuántos botes caben en tu stack?"
// ═════════════════════════════════════════════════════════════════════════════
function Scene1_Hook() {
  return (
    <Sprite start={0} end={14}>
      {({ localTime }) => {
        const t = localTime;
        // Big SPR letters fade-in stagger
        const letters = ['S', 'P', 'R'];
        const titleY = interpolate([0, 0.8], [40, 0], Easing.easeOutCubic)(clamp(t/1.2, 0, 1));

        // Question fades in at t=2.6
        const qOp = clamp((t - 2.6) / 0.6, 0, 1);
        // Subtitle line at t=4.5
        const sOp = clamp((t - 4.5) / 0.6, 0, 1);

        // Background "stack of pots" hint - 5 small piles growing
        const pilesT = clamp((t - 6) / 4, 0, 1);

        // Exit
        const exitT = clamp((t - 12.5) / 1.5, 0, 1);
        const exitOp = 1 - exitT;

        return (
          <div style={{ position: 'absolute', inset: 0, opacity: exitOp }}>
            {/* Ambient backdrop */}
            <div style={{
              position: 'absolute', inset: 0,
              background: 'radial-gradient(ellipse at 50% 40%, rgba(212,162,74,0.08) 0%, rgba(0,0,0,0) 55%), #0a0c14',
            }}/>

            {/* SPR mega type */}
            <div style={{
              position: 'absolute',
              top: 280, left: 0, right: 0,
              textAlign: 'center',
              transform: `translateY(${titleY}px)`,
            }}>
              <div style={{
                fontFamily: 'Inter, system-ui, sans-serif',
                fontSize: 280,
                fontWeight: 800,
                letterSpacing: '-0.05em',
                lineHeight: 0.9,
                color: '#f3f1ea',
                display: 'inline-flex',
                gap: 0,
              }}>
                {letters.map((L, i) => {
                  const lt = clamp((t - 0.2 - i * 0.18) / 0.5, 0, 1);
                  const op = Easing.easeOutCubic(lt);
                  const dy = (1 - op) * 80;
                  return (
                    <span key={i} style={{
                      display: 'inline-block',
                      opacity: op,
                      transform: `translateY(${dy}px)`,
                      color: i === 1 ? '#d4a24a' : '#f3f1ea',
                    }}>{L}</span>
                  );
                })}
              </div>
              <div style={{
                fontFamily: 'JetBrains Mono, monospace',
                fontSize: 16,
                letterSpacing: '0.32em',
                textTransform: 'uppercase',
                color: 'rgba(212,162,74,0.85)',
                marginTop: 24,
                opacity: clamp((t - 1.4) / 0.6, 0, 1),
              }}>Stack-to-Pot Ratio</div>
            </div>

            {/* Big question */}
            <div style={{
              position: 'absolute',
              top: 760, left: 0, right: 0,
              textAlign: 'center',
              opacity: qOp,
              fontFamily: 'Inter, system-ui, sans-serif',
              fontSize: 44,
              fontWeight: 300,
              color: '#f3f1ea',
              letterSpacing: '-0.01em',
              lineHeight: 1.2,
            }}>
              ¿Cuántos <span style={{
                fontWeight: 600,
                color: '#d4a24a',
                fontStyle: 'italic',
              }}>botes</span> caben en tu stack?
            </div>

            {/* Subtitle */}
            <div style={{
              position: 'absolute',
              top: 880, left: 0, right: 0,
              textAlign: 'center',
              opacity: sOp,
              fontFamily: 'Inter, system-ui, sans-serif',
              fontSize: 18,
              color: 'rgba(243,241,234,0.55)',
              letterSpacing: '0.04em',
            }}>
              La medida exacta del riesgo relativo en cada calle
            </div>

            {/* Decorative pile counter at bottom */}
            <div style={{
              position: 'absolute',
              bottom: 80, left: 0, right: 0,
              display: 'flex', justifyContent: 'center', alignItems: 'flex-end', gap: 60,
              opacity: clamp((t - 6) / 0.8, 0, 1),
            }}>
              {[1, 2, 4, 8, 15].map((n, i) => {
                const showT = clamp(pilesT * 5 - i, 0, 1);
                return (
                  <div key={i} style={{
                    display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 12,
                    opacity: showT,
                    transform: `translateY(${(1 - showT) * 12}px)`,
                  }}>
                    <div style={{
                      fontFamily: 'JetBrains Mono, monospace',
                      fontSize: 22,
                      color: i < 2 ? '#4fd1c5' : i < 4 ? '#e8b95c' : '#e0556a',
                      fontVariantNumeric: 'tabular-nums',
                    }}>{n}×</div>
                    <div style={{
                      width: 40,
                      height: 30 + n * 6,
                      maxHeight: 140,
                      background: `linear-gradient(180deg, ${i < 2 ? '#4fd1c5' : i < 4 ? '#e8b95c' : '#e0556a'} 0%, transparent 100%)`,
                      borderRadius: 4,
                      opacity: 0.35,
                    }}/>
                  </div>
                );
              })}
            </div>
          </div>
        );
      }}
    </Sprite>
  );
}

// ═════════════════════════════════════════════════════════════════════════════
// SCENE 2 — DEFINITION (14 → 36s)
// "Un SPR de 4 = stack vale 4 botes"
// ═════════════════════════════════════════════════════════════════════════════
function Scene2_Definition() {
  return (
    <Sprite start={14} end={36}>
      {({ localTime }) => {
        const t = localTime;
        // Stack on left grows
        const stackCount = Math.floor(interpolate([0, 2.5], [0, 16], Easing.easeOutCubic)(clamp(t/2.5, 0, 1)) * (clamp(t/2.5, 0, 1)));
        // Actually simpler:
        const stackProg = Easing.easeOutCubic(clamp(t / 2.2, 0, 1));
        const stackVisible = Math.round(16 * stackProg);

        // Pot pile appears at t=3
        const potT = clamp((t - 3) / 1.5, 0, 1);
        const potCount = Math.round(20 * Easing.easeOutCubic(potT));

        // Equation overlay at t=6
        const eqOp = clamp((t - 6) / 0.6, 0, 1);

        // SPR result counter at t=8
        const sprT = clamp((t - 8) / 2, 0, 1);
        const sprValue = (4 * Easing.easeOutCubic(sprT)).toFixed(1);

        // Caption transitions
        const captions = [
          { start: 0.3, end: 5.5, text: 'Imagina tu stack restante…', sub: 'Definición' },
          { start: 5.6, end: 11, text: '…y el bote actual. El SPR los compara.', sub: 'Definición' },
          { start: 11.1, end: 18, text: 'Un SPR de 4 significa: tu stack es 4 veces el bote.', sub: 'Lectura' },
          { start: 18.1, end: 22, text: 'Con SPR 1, stack y bote son iguales — un solo all-in los junta.', sub: 'Lectura' },
        ];

        // SPR=1 callout at t=14
        const showSpr1 = t > 14 && t < 22;
        const spr1Op = showSpr1 ? clamp((t - 14) / 0.6, 0, 1) * clamp((22 - t) / 0.6, 0, 1) : 0;

        // Exit fade
        const exitOp = clamp((22 - t) / 1.2, 0, 1);

        return (
          <div style={{ position: 'absolute', inset: 0, opacity: exitOp }}>
            {/* Headline */}
            <div style={{
              position: 'absolute',
              top: 130, left: 0, right: 0,
              textAlign: 'center',
              opacity: clamp(t / 0.6, 0, 1),
            }}>
              <div style={{
                fontFamily: 'JetBrains Mono, monospace',
                fontSize: 12,
                letterSpacing: '0.32em',
                textTransform: 'uppercase',
                color: 'rgba(212,162,74,0.8)',
                marginBottom: 12,
              }}>01 · La idea</div>
              <div style={{
                fontFamily: 'Inter, system-ui, sans-serif',
                fontSize: 56,
                fontWeight: 600,
                letterSpacing: '-0.02em',
                color: '#f3f1ea',
              }}>Stack ÷ Bote = SPR</div>
            </div>

            {/* Stack visualization */}
            <div style={{
              position: 'absolute',
              top: 360, left: 360,
              width: 340, height: 480,
              display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'flex-end',
            }}>
              <div style={{
                fontFamily: 'JetBrains Mono, monospace',
                fontSize: 12,
                letterSpacing: '0.18em',
                textTransform: 'uppercase',
                color: 'rgba(243,241,234,0.5)',
                marginBottom: 20,
              }}>Tu stack</div>
              <div style={{ position: 'relative', width: 80, height: 360 }}>
                <ChipStack
                  count={stackVisible}
                  x={40} y={360}
                  chipSize={48}
                  color="#d4a24a" edge="#8a6520"
                  label={`${(stackVisible * 6).toFixed(0)}€`}
                />
              </div>
            </div>

            {/* Divider */}
            <div style={{
              position: 'absolute',
              top: 540, left: '50%',
              transform: 'translateX(-50%)',
              fontFamily: 'Inter, system-ui, sans-serif',
              fontSize: 80,
              fontWeight: 200,
              color: 'rgba(243,241,234,0.25)',
              opacity: clamp((t - 2.5) / 0.6, 0, 1),
            }}>÷</div>

            {/* Pot pile */}
            <div style={{
              position: 'absolute',
              top: 360, left: 1220,
              width: 340, height: 480,
              display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center',
              opacity: clamp((t - 3) / 0.6, 0, 1),
            }}>
              <div style={{
                fontFamily: 'JetBrains Mono, monospace',
                fontSize: 12,
                letterSpacing: '0.18em',
                textTransform: 'uppercase',
                color: 'rgba(243,241,234,0.5)',
                marginBottom: 30,
              }}>El bote</div>
              <div style={{ position: 'relative', width: 200, height: 160 }}>
                <ChipPile count={potCount} x={100} y={80} radius={70} color="#4fd1c5" edge="#2a8780" label={`${(potCount * 1.2).toFixed(0)}€`}/>
              </div>
            </div>

            {/* SPR result */}
            <div style={{
              position: 'absolute',
              top: 700, left: 0, right: 0,
              textAlign: 'center',
              opacity: eqOp,
            }}>
              <div style={{
                fontFamily: 'Inter, system-ui, sans-serif',
                fontSize: 18,
                color: 'rgba(243,241,234,0.5)',
                letterSpacing: '0.18em',
                textTransform: 'uppercase',
              }}>SPR =</div>
              <div style={{
                fontFamily: 'Inter, system-ui, sans-serif',
                fontSize: 140,
                fontWeight: 700,
                letterSpacing: '-0.04em',
                color: '#d4a24a',
                lineHeight: 1,
                fontVariantNumeric: 'tabular-nums',
                marginTop: 8,
              }}>
                {sprValue}
              </div>
            </div>

            {/* SPR=1 inline callout */}
            {spr1Op > 0 && (
              <div style={{
                position: 'absolute',
                top: 700, left: 0, right: 0,
                textAlign: 'center',
                opacity: spr1Op,
              }}>
                <div style={{
                  fontFamily: 'Inter, system-ui, sans-serif',
                  fontSize: 18,
                  color: 'rgba(243,241,234,0.5)',
                  letterSpacing: '0.18em',
                  textTransform: 'uppercase',
                }}>SPR = 1.0 · stack = bote</div>
                <div style={{
                  fontFamily: 'Inter, system-ui, sans-serif',
                  fontSize: 32,
                  color: 'rgba(243,241,234,0.7)',
                  marginTop: 16,
                  fontWeight: 300,
                }}>Un solo all-in y los stacks están en juego.</div>
              </div>
            )}

            {/* Captions */}
            {captions.map((c, i) => (
              t >= c.start && t <= c.end && (
                <Sprite key={i} start={14 + c.start} end={14 + c.end}>
                  <Caption text={c.text} sub={c.sub} />
                </Sprite>
              )
            ))}
          </div>
        );
      }}
    </Sprite>
  );
}

// ═════════════════════════════════════════════════════════════════════════════
// SCENE 3 — FORMULA (36 → 62s)
// SPR = Stack efectivo restante ÷ Tamaño del bote al inicio de la calle
// ═════════════════════════════════════════════════════════════════════════════
function Scene3_Formula() {
  return (
    <Sprite start={36} end={62}>
      {({ localTime }) => {
        const t = localTime;

        // Phase 1: formula assembly (0 → 6s)
        const part1 = clamp(t / 1.0, 0, 1);
        const part2 = clamp((t - 1.2) / 1.0, 0, 1);
        const part3 = clamp((t - 2.4) / 1.0, 0, 1);

        // Phase 2: highlight "efectivo" + "al inicio" (6 → 12s)
        const showHL = t > 6 && t < 13;
        const hl1 = clamp((t - 6) / 0.5, 0, 1) * clamp((13 - t) / 0.5, 0, 1);
        const hl2 = clamp((t - 8) / 0.5, 0, 1) * clamp((13 - t) / 0.5, 0, 1);

        // Phase 3: example walkthrough panel (13 → 26s)
        const showEx = t > 13;
        const exOp = clamp((t - 13) / 0.6, 0, 1);

        const exitOp = clamp((26 - t) / 1.2, 0, 1);

        return (
          <div style={{ position: 'absolute', inset: 0, opacity: exitOp }}>
            {/* Header */}
            <div style={{
              position: 'absolute',
              top: 130, left: 0, right: 0,
              textAlign: 'center',
              opacity: clamp(t / 0.4, 0, 1),
            }}>
              <div style={{
                fontFamily: 'JetBrains Mono, monospace',
                fontSize: 12,
                letterSpacing: '0.32em',
                textTransform: 'uppercase',
                color: 'rgba(212,162,74,0.8)',
                marginBottom: 12,
              }}>02 · La fórmula</div>
              <div style={{
                fontFamily: 'Inter, system-ui, sans-serif',
                fontSize: 48,
                fontWeight: 500,
                letterSpacing: '-0.02em',
                color: '#f3f1ea',
              }}>Cómo se calcula, exactamente</div>
            </div>

            {/* Formula panel */}
            <div style={{
              position: 'absolute',
              top: 320, left: '50%',
              transform: 'translateX(-50%)',
              padding: '40px 60px',
              background: 'rgba(20,24,38,0.6)',
              border: '1px solid rgba(255,255,255,0.08)',
              borderRadius: 16,
              display: 'flex', alignItems: 'center', gap: 28,
              fontFamily: 'Inter, system-ui, sans-serif',
              fontSize: 38,
              fontWeight: 500,
              letterSpacing: '-0.01em',
            }}>
              <span style={{
                opacity: part1,
                color: '#d4a24a',
                fontWeight: 700,
                fontFamily: 'JetBrains Mono, monospace',
                fontSize: 46,
              }}>SPR</span>
              <span style={{ opacity: part1, color: 'rgba(243,241,234,0.5)' }}>=</span>
              <span style={{
                opacity: part2,
                color: '#f3f1ea',
                position: 'relative',
                background: hl1 > 0.05 ? `rgba(78,209,197,${0.18 * hl1})` : 'transparent',
                padding: '6px 12px',
                borderRadius: 8,
                transition: 'background 200ms',
              }}>
                Stack efectivo restante
              </span>
              <span style={{ opacity: part2, color: 'rgba(243,241,234,0.5)', fontSize: 50 }}>÷</span>
              <span style={{
                opacity: part3,
                color: '#f3f1ea',
                background: hl2 > 0.05 ? `rgba(212,162,74,${0.18 * hl2})` : 'transparent',
                padding: '6px 12px',
                borderRadius: 8,
                transition: 'background 200ms',
              }}>
                Bote al inicio de la calle
              </span>
            </div>

            {/* Highlight callouts */}
            <div style={{
              position: 'absolute',
              top: 460, left: 540,
              opacity: hl1,
              maxWidth: 320,
            }}>
              <div style={{
                fontFamily: 'JetBrains Mono, monospace',
                fontSize: 11,
                letterSpacing: '0.18em',
                color: '#4fd1c5',
                textTransform: 'uppercase',
                marginBottom: 6,
              }}>← Efectivo</div>
              <div style={{
                fontSize: 16,
                color: 'rgba(243,241,234,0.65)',
                lineHeight: 1.4,
              }}>El stack más pequeño en juego — no el tuyo si tu rival tiene menos.</div>
            </div>
            <div style={{
              position: 'absolute',
              top: 460, right: 380,
              opacity: hl2,
              maxWidth: 320,
              textAlign: 'right',
            }}>
              <div style={{
                fontFamily: 'JetBrains Mono, monospace',
                fontSize: 11,
                letterSpacing: '0.18em',
                color: '#d4a24a',
                textTransform: 'uppercase',
                marginBottom: 6,
              }}>Al inicio →</div>
              <div style={{
                fontSize: 16,
                color: 'rgba(243,241,234,0.65)',
                lineHeight: 1.4,
              }}>Antes de las apuestas de esta calle. Se recalcula al pasar al turn y al river.</div>
            </div>

            {/* Example walkthrough panel */}
            {showEx && (
              <div style={{
                position: 'absolute',
                top: 620, left: '50%',
                transform: 'translateX(-50%)',
                width: 1280,
                opacity: exOp,
                background: 'rgba(20,24,38,0.5)',
                border: '1px solid rgba(255,255,255,0.06)',
                borderRadius: 12,
                padding: '28px 40px',
              }}>
                <div style={{
                  display: 'flex', alignItems: 'center', gap: 16, marginBottom: 20,
                }}>
                  <div style={{
                    fontFamily: 'JetBrains Mono, monospace',
                    fontSize: 11,
                    letterSpacing: '0.2em',
                    textTransform: 'uppercase',
                    color: '#d4a24a',
                  }}>Ejemplo</div>
                  <div style={{
                    fontSize: 18,
                    color: 'rgba(243,241,234,0.85)',
                    fontWeight: 500,
                  }}>NL100 · 100€ efectivos · CO open 2.5€, BTN call</div>
                </div>

                <div style={{
                  display: 'grid',
                  gridTemplateColumns: 'repeat(4, 1fr)',
                  gap: 24,
                  fontFamily: 'JetBrains Mono, monospace',
                }}>
                  {[
                    { label: 'Stack restante', val: '97.5€', delay: 14, color: '#4fd1c5' },
                    { label: 'Bote al flop', val: '6.5€', delay: 16, color: '#d4a24a' },
                    { label: 'Cálculo', val: '97.5 ÷ 6.5', delay: 18, color: '#f3f1ea' },
                    { label: 'SPR resultante', val: '≈ 15', delay: 20, color: '#e0556a', big: true },
                  ].map((item, i) => {
                    const op = clamp((t - item.delay) / 0.6, 0, 1);
                    return (
                      <div key={i} style={{
                        opacity: op,
                        transform: `translateY(${(1 - op) * 12}px)`,
                      }}>
                        <div style={{
                          fontSize: 11,
                          letterSpacing: '0.16em',
                          textTransform: 'uppercase',
                          color: 'rgba(243,241,234,0.45)',
                          marginBottom: 8,
                        }}>{item.label}</div>
                        <div style={{
                          fontSize: item.big ? 44 : 32,
                          color: item.color,
                          fontWeight: item.big ? 700 : 500,
                          letterSpacing: '-0.02em',
                          fontVariantNumeric: 'tabular-nums',
                        }}>{item.val}</div>
                      </div>
                    );
                  })}
                </div>

                {/* Verdict */}
                <div style={{
                  marginTop: 24,
                  paddingTop: 20,
                  borderTop: '1px solid rgba(255,255,255,0.06)',
                  display: 'flex', alignItems: 'center', gap: 12,
                  opacity: clamp((t - 22) / 0.6, 0, 1),
                  fontSize: 18,
                  color: 'rgba(243,241,234,0.7)',
                }}>
                  <span style={{
                    padding: '4px 10px',
                    background: 'rgba(224,85,106,0.18)',
                    border: '1px solid rgba(224,85,106,0.4)',
                    color: '#e0556a',
                    fontFamily: 'JetBrains Mono, monospace',
                    fontSize: 11,
                    letterSpacing: '0.14em',
                    textTransform: 'uppercase',
                    borderRadius: 4,
                  }}>Stack profundo</span>
                  <span>Quince botes por delante. La aventura empieza ahora.</span>
                </div>
              </div>
            )}
          </div>
        );
      }}
    </Sprite>
  );
}

Object.assign(window, { Scene1_Hook, Scene2_Definition, Scene3_Formula });
