// Act 4 — Offer · accept · end card (0:45 – 1:00)

// ── Scene 10: Offer composed (45-50s) ──────────────────────────────────
function Scene10() {
  const { localTime } = useSprite();
  const sendRef = React.useRef(null);
  const bodyRef = React.useRef(null);
  const [target, setTarget] = React.useState({ x: 1400, y: 620 });

  React.useLayoutEffect(() => {
    if (sendRef.current && bodyRef.current) {
      const p = offsetWithin(sendRef.current, bodyRef.current);
      setTarget({ x: p.x + p.w * 0.45, y: p.y + p.h * 0.5 });
    }
  }, [localTime > 1.5]);

  // Paper fade-in 0..0.5s
  const paperT = Easing.easeOutCubic(clamp(localTime / 0.5, 0, 1));

  // Salary count-up 0.3..1.8s
  const countT = Easing.easeOutCubic(clamp((localTime - 0.3) / 1.5, 0, 1));
  const salary = Math.round(countT * 7500);
  const formatted = salary.toLocaleString('en-IE');

  // Total package: salary * 12 + equity + bonus ≈ 112k
  const packageT = Easing.easeOutCubic(clamp((localTime - 1.0) / 1.2, 0, 1));
  const pkg = Math.round(packageT * 112);

  // CTA states
  const hovering = localTime > 2.6 && localTime < 3.6;
  const pressed  = localTime > 3.6 && localTime < 3.9;

  // Cursor: fades in, approaches the SEND OFFER button, presses at 3.6s
  const cursorX = interpolate([0, 3.0, 3.6], [target.x + 140, target.x + 30, target.x], Easing.easeOutCubic)(localTime);
  const cursorY = interpolate([0, 3.0, 3.6], [target.y + 100, target.y + 20, target.y], Easing.easeOutCubic)(localTime);
  const cursorOpacity = clamp((localTime - 2.2) / 0.4, 0, 1);
  const rippleT = clamp((localTime - 3.6) / 0.55, 0, 1);

  const terms = [
    { k: 'Start',    v: 'Mon · 8 Jun 2026',        icon: 'M4 5h16M4 10h16M4 15h16M4 20h16' },
    { k: 'Location', v: 'Reykjavík · hybrid 2/3',  icon: 'M12 21s-7-7.5-7-12a7 7 0 1 1 14 0c0 4.5-7 12-7 12z|M12 11.5a2.5 2.5 0 1 0 0-5 2.5 2.5 0 0 0 0 5z' },
    { k: 'Equity',   v: '0.15% · 4 yr vest',       icon: 'M4 12l5 5L20 6' },
  ];

  return (
    <div style={{ position: 'absolute', inset: 0, background: '#ebe7dc' }}>
      <AppWindow active="Jobs" width={1760} height={900} x={80} y={40}
        title="Offer · Ragna Eiríksdóttir"
        right={<Pill kind="gray" size="md">Draft · pre-filled</Pill>}>
        <div ref={bodyRef} style={{
          padding: '20px 48px 28px', position: 'relative',
          height: '100%', boxSizing: 'border-box',
          display: 'flex', flexDirection: 'column',
        }}>
          {/* Letter card */}
          <div style={{
            flex: 1, display: 'flex', flexDirection: 'column',
            background: '#fff',
            border: '1px solid rgba(0,0,0,0.06)',
            borderRadius: 16,
            boxShadow: '0 1px 2px rgba(0,0,0,0.04), 0 24px 48px -32px rgba(20,20,40,0.25)',
            padding: '28px 44px',
            position: 'relative',
            opacity: paperT,
            transform: `translateY(${(1 - paperT) * 10}px)`,
            overflow: 'hidden',
          }}>
            {/* Accent band at top */}
            <div style={{
              position: 'absolute', top: 0, left: 0, right: 0, height: 4,
              background: `linear-gradient(90deg, ${WP.brand} 0%, ${WP.purple} 100%)`,
            }} />

            {/* Letter header */}
            <div style={{ display: 'flex', alignItems: 'flex-start', gap: 20, marginBottom: 22 }}>
              <div style={{ flex: 1 }}>
                <div style={{
                  fontSize: 11, fontWeight: 700, color: WP.brand,
                  letterSpacing: '2.5px', textTransform: 'uppercase', marginBottom: 6,
                }}>Offer · Fjörður Labs</div>
                <div style={{ fontSize: 30, fontWeight: 700, color: WP.ink, letterSpacing: '-0.015em', lineHeight: 1.1 }}>
                  Ocean Systems Engineer
                </div>
                <div style={{ fontSize: 13, color: WP.textDim, marginTop: 6 }}>
                  Prepared for Ragna Eiríksdóttir · Apr 2026
                </div>
              </div>
              {/* Recipient chip */}
              <div style={{
                display: 'flex', alignItems: 'center', gap: 10,
                padding: '10px 14px 10px 10px',
                background: WP.brandLighter, borderRadius: 999,
              }}>
                <div style={{
                  width: 32, height: 32, borderRadius: '50%',
                  background: WP.brand, color: '#fff',
                  display: 'flex', alignItems: 'center', justifyContent: 'center',
                  fontSize: 11, fontWeight: 700,
                }}>RE</div>
                <div style={{ display: 'flex', flexDirection: 'column', lineHeight: 1.1 }}>
                  <span style={{ fontSize: 13, fontWeight: 700, color: WP.ink }}>Ragna Eiríksdóttir</span>
                  <span style={{ fontSize: 11, color: WP.textDim }}>ragna@opusfutura.com</span>
                </div>
              </div>
            </div>

            {/* Salary + package */}
            <div style={{
              display: 'flex', alignItems: 'flex-end', gap: 40,
              paddingBottom: 22,
              borderBottom: '1px solid rgba(0,0,0,0.06)',
              marginBottom: 22,
            }}>
              <div style={{ flex: 1 }}>
                <div style={{
                  fontSize: 11, fontWeight: 700, color: WP.textDim,
                  letterSpacing: '1.5px', textTransform: 'uppercase', marginBottom: 10,
                }}>Base salary</div>
                <div style={{
                  fontSize: 88, fontWeight: 700, color: WP.ink,
                  letterSpacing: '-0.035em', lineHeight: 0.95, fontVariantNumeric: 'tabular-nums',
                }}>
                  €{formatted}
                  <span style={{ fontSize: 24, fontWeight: 400, color: WP.textDim, marginLeft: 10 }}>/ mo</span>
                </div>
              </div>
              <div style={{ textAlign: 'right', paddingBottom: 8 }}>
                <div style={{
                  fontSize: 11, fontWeight: 700, color: WP.textDim,
                  letterSpacing: '1.5px', textTransform: 'uppercase', marginBottom: 6,
                }}>Total package</div>
                <div style={{
                  fontSize: 28, fontWeight: 700, color: WP.brand,
                  letterSpacing: '-0.01em', fontVariantNumeric: 'tabular-nums',
                }}>
                  €{pkg}k <span style={{ fontSize: 15, color: WP.textDim, fontWeight: 400 }}>/ yr</span>
                </div>
                <div style={{ fontSize: 11, color: WP.textDim, marginTop: 2 }}>
                  Salary · bonus · equity
                </div>
              </div>
            </div>

            {/* Terms */}
            <div style={{ display: 'grid', gridTemplateColumns: 'repeat(3,1fr)', gap: 14, marginBottom: 22 }}>
              {terms.map(({ k, v }) => (
                <div key={k} style={{
                  padding: '16px 18px', borderRadius: 12,
                  background: '#fafaf5',
                  border: '1px solid rgba(0,0,0,0.05)',
                  position: 'relative', overflow: 'hidden',
                }}>
                  <div style={{
                    position: 'absolute', left: 0, top: 12, bottom: 12, width: 3,
                    background: WP.brand, borderRadius: '0 3px 3px 0',
                  }} />
                  <div style={{
                    fontSize: 11, color: WP.textDim,
                    letterSpacing: '1.5px', textTransform: 'uppercase', fontWeight: 700,
                  }}>{k}</div>
                  <div style={{ fontSize: 16, fontWeight: 600, color: WP.ink, marginTop: 4 }}>{v}</div>
                </div>
              ))}
            </div>

            {/* Benefits strip */}
            <div style={{
              display: 'flex', alignItems: 'center', gap: 26, flexWrap: 'wrap',
              marginBottom: 28,
            }}>
              <div style={{
                fontSize: 11, fontWeight: 700, color: WP.textDim,
                letterSpacing: '1.5px', textTransform: 'uppercase',
              }}>Includes</div>
              {[
                'Health · dental · vision',
                '30 days paid leave',
                'Remote work setup',
                'Annual learning budget',
              ].map((b, i) => (
                <div key={i} style={{
                  display: 'inline-flex', alignItems: 'center', gap: 8,
                  fontSize: 13, color: WP.text, fontWeight: 500,
                }}>
                  <svg width="14" height="14" viewBox="0 0 24 24" fill="none" style={{ flexShrink: 0 }}>
                    <path d="M5 12.5l4.5 4.5L19 7.5" stroke={WP.green} strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round"/>
                  </svg>
                  {b}
                </div>
              ))}
            </div>

            {/* CTA footer */}
            <div style={{
              marginTop: 'auto',
              paddingTop: 20,
              borderTop: '1px solid rgba(0,0,0,0.06)',
              display: 'flex', alignItems: 'center', gap: 16,
            }}>
              <div style={{
                fontSize: 12, color: WP.textDim, flex: 1,
                display: 'inline-flex', alignItems: 'center', gap: 8,
              }}>
                <svg width="14" height="14" viewBox="0 0 24 24" fill="none">
                  <rect x="4" y="10" width="16" height="10" rx="2" stroke={WP.textDim} strokeWidth="2"/>
                  <path d="M8 10V7a4 4 0 0 1 8 0v3" stroke={WP.textDim} strokeWidth="2"/>
                </svg>
                Encrypted · signed by Fjörður Labs
              </div>
              <div style={{
                padding: '14px 22px', fontSize: 13, fontWeight: 600,
                color: WP.text, background: 'transparent',
                border: '1px solid rgba(0,0,0,0.12)', borderRadius: 10,
              }}>Save draft</div>
              <div ref={sendRef} style={{
                padding: '14px 30px', fontSize: 15, fontWeight: 700, letterSpacing: '0.6px',
                color: '#fff',
                background: `linear-gradient(180deg, ${WP.brandLight} 0%, ${WP.brand} 100%)`,
                borderRadius: 10,
                display: 'inline-flex', alignItems: 'center', gap: 10,
                transform: pressed ? 'translateY(2px) scale(0.99)' : hovering ? 'translateY(-2px)' : 'translateY(0)',
                boxShadow: pressed
                  ? '0 2px 4px rgba(82,49,255,0.4)'
                  : hovering
                  ? '0 14px 28px -6px rgba(82,49,255,0.55)'
                  : '0 8px 20px -6px rgba(82,49,255,0.4)',
                transition: 'all 120ms',
              }}>
                Send offer
                <svg width="16" height="16" viewBox="0 0 24 24" fill="none">
                  <path d="M5 12h14M13 6l6 6-6 6" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round"/>
                </svg>
              </div>
            </div>
          </div>

          <ClickRipple x={cursorX + 4} y={cursorY + 4} t={rippleT} />
          <Cursor x={cursorX} y={cursorY} rotate={-8} pressed={pressed} style={{ opacity: cursorOpacity }} />
        </div>
      </AppWindow>
    </div>
  );
}

// ── Scene 11: Accepted (50-55s) ────────────────────────────────────────
function Scene11() {
  const { localTime } = useSprite();

  // Check scales in 0..0.7
  const checkT = Easing.easeOutBack(clamp(localTime / 0.7, 0, 1));

  // KPI count-ups
  const kpiT = clamp((localTime - 0.8) / 0.8, 0, 1);
  const days = Math.round(kpiT * 19);
  const matches = Math.round(kpiT * 12);
  const cvs = Math.round(kpiT * 0);

  // Confetti
  const confetti = Math.max(0, localTime - 0.4);

  return (
    <div style={{ position: 'absolute', inset: 0, background: '#ebe7dc' }}>
      <AppWindow active="Jobs" width={1760} height={900} x={80} y={40}
        title="Ocean Systems Engineer"
        right={<Pill kind="green" size="lg">● COMPLETED</Pill>}>
        <div style={{
          height: '100%', display: 'flex', flexDirection: 'column',
          alignItems: 'center', justifyContent: 'center', gap: 20,
          position: 'relative', overflow: 'hidden',
        }}>
          {/* Confetti */}
          {confetti > 0 && Array.from({ length: 22 }).map((_, i) => {
            const seed = (i * 9301 + 49297) % 233280 / 233280;
            const delay = (i % 8) * 0.05;
            const t = Math.max(0, confetti - delay);
            const fallY = t * 700;
            const drift = Math.sin(t * 2 + i) * 30;
            const spin = t * 540;
            const colors = [WP.brand, WP.purple, WP.yellow, WP.green, '#ffd101'];
            return (
              <div key={i} style={{
                position: 'absolute', left: `${seed * 100}%`,
                top: -30,
                width: 10, height: 16,
                background: colors[i % colors.length],
                transform: `translate(${drift}px, ${fallY}px) rotate(${spin}deg)`,
                opacity: Math.max(0, 1 - t / 4),
              }} />
            );
          })}

          <div style={{
            width: 160, height: 160, borderRadius: '50%',
            background: WP.green,
            display: 'flex', alignItems: 'center', justifyContent: 'center',
            transform: `scale(${checkT})`,
            boxShadow: '0 0 0 24px rgba(82,185,99,0.18)',
          }}>
            <svg width="80" height="80" viewBox="0 0 24 24" fill="none">
              <path d="M5 12.5l4.5 4.5L19 7.5" stroke="#fff" strokeWidth="3" strokeLinecap="round" strokeLinejoin="round"/>
            </svg>
          </div>

          <div style={{ fontSize: 40, fontWeight: 700, color: WP.ink, letterSpacing: '-0.02em', opacity: checkT }}>
            Offer accepted.
          </div>
          <div style={{ fontSize: 18, color: WP.textDim, opacity: checkT }}>
            Ragna starts Monday, 8 June.
          </div>

          <div style={{
            display: 'flex', gap: 48, marginTop: 20,
            opacity: kpiT,
          }}>
            {[
              { big: days, unit: 'days' },
              { big: matches, unit: 'matches' },
            ].map((k, i) => (
              <div key={i} style={{ textAlign: 'center' }}>
                <div style={{
                  fontSize: 56, fontWeight: 700, color: WP.brand,
                  letterSpacing: '-0.03em', lineHeight: 1,
                  fontVariantNumeric: 'tabular-nums',
                }}>{k.big}</div>
                <div style={{ fontSize: 13, color: WP.textDim, marginTop: 6, letterSpacing: '1.5px', textTransform: 'uppercase', fontWeight: 600 }}>
                  {k.unit}
                </div>
              </div>
            ))}
          </div>
        </div>
      </AppWindow>
    </div>
  );
}

// ── Scene 12: End card (55-60s) ────────────────────────────────────────
function Scene12() {
  const { localTime } = useSprite();

  const logoT = Easing.easeOutBack(clamp(localTime / 0.5, 0, 1));
  const wordT = clamp((localTime - 0.4) / 0.4, 0, 1);
  const urlT = clamp((localTime - 0.8) / 0.4, 0, 1);
  const tagT = clamp((localTime - 1.3) / 0.4, 0, 1);

  // (no drift — keep the end card still)
  const drift = 0;

  return (
    <div style={{
      position: 'absolute', inset: 0,
      background: `radial-gradient(ellipse at center, ${WP.brandLight} 0%, ${WP.brand} 60%, #3a1fcc 100%)`,
      display: 'flex', alignItems: 'center', justifyContent: 'center',
      overflow: 'hidden',
    }}>
      {/* Subtle dot grid */}
      <div style={{
        position: 'absolute', inset: 0,
        backgroundImage: 'radial-gradient(rgba(255,255,255,0.08) 1.5px, transparent 1.5px)',
        backgroundSize: '32px 32px',
        opacity: 0.5,
      }} />

      <div style={{ textAlign: 'center', color: '#fff', position: 'relative', transform: `translateY(${drift}px)` }}>
        {/* Logo mark */}
        <div style={{
          width: 140, height: 140, borderRadius: 28,
          background: '#fff',
          margin: '0 auto 28px',
          display: 'flex', alignItems: 'center', justifyContent: 'center',
          transform: `scale(${logoT})`,
          boxShadow: '0 24px 48px -16px rgba(0,0,0,0.35)',
        }}>
          <img
            src="assets/of_logo_2.png"
            alt="Opus Futura"
            style={{ width: 92, height: 92, objectFit: 'contain', display: 'block' }}
          />
        </div>

        <div style={{
          fontSize: 76, fontWeight: 700, lineHeight: 1.0,
          letterSpacing: '-0.02em', opacity: wordT,
          transform: `translateY(${(1-wordT) * 14}px)`,
        }}>
          Opus Futura<br/>
          <span style={{ fontWeight: 300, fontStyle: 'italic' }}>Workplace.</span>
        </div>

        <div style={{
          fontSize: 16, fontWeight: 600, letterSpacing: '3px',
          marginTop: 32, opacity: urlT * 0.85,
          textTransform: 'uppercase',
        }}>
          opusfutura.com / workplace
        </div>

        {/* Yellow tagline card */}
        <div style={{
          marginTop: 56,
          display: 'inline-block',
          background: WP.yellow, color: WP.ink,
          padding: '20px 36px',
          fontSize: 28, fontWeight: 700,
          letterSpacing: '-0.005em',
          boxShadow: '8px 8px 0 rgba(0,0,0,0.28)',
          opacity: tagT,
          transform: `translateY(${(1-tagT) * 20}px) rotate(${-1 + tagT * 1}deg)`,
        }}>
          Match people to what they <i>want</i> to do.
        </div>
      </div>
    </div>
  );
}

Object.assign(window, { Scene10, Scene11, Scene12 });
