// scene-act2.jsx — Act II: Phase 1 Charter notebook sequence
// Total ~20s. Beats:
//  0.0–1.5   pipeline tracker tucks in at top, ① Charter pulses
//  1.5–4.5   notebook page slides in from right; PHASE 1 header types
//  4.5–8.0   PROBLEM STATEMENT section header types + body excerpt
//  8.0–11.5  GOAL STATEMENT types in below it
// 11.5–16.0  VOC: three quote cards stagger in
// 16.0–20.0  notebook lifts and slides off right (handoff)

function SceneAct2() {
  return (
    <Timeline duration={24800} sceneId="act2" label="Act II — Phase 1: Charter">
      <CaptionScene>
        <div className="scene-bg scene-cream-bg" />

        <PhaseIntroCard
          from={0}
          num="01"
          name="Charter"
          who="😍 Human only"
          tone="cream"
          summary="Frame the problem. Set the goal. Capture the voice of the customer."
        />

        <TimeShift offset={4800}>
          <Act2PipelineTracker />
          <Act2Notebook />

          <SceneCaption text="This sets direction. Everything else builds on it." from={2200} typeDur={1500} hold={2000} />
          <SceneCaption text="Real quotes. Real users. These become features." from={13800} typeDur={1500} hold={1900} />
          <SceneCaption text="Now hand it to Claude." from={17500} typeDur={1100} hold={1600} />
        </TimeShift>
      </CaptionScene>
    </Timeline>
  );
}

function Act2PipelineTracker() {
  const t = useT();
  const op = Math.min(1, Math.max(0, (t - 200) / 600));
  return (
    <div style={{
      position: 'absolute', top: 0, left: 0, right: 0, height: 84,
      background: 'rgba(245,240,232,0.95)',
      borderBottom: '1px solid var(--cream-rule)',
      display: 'flex', alignItems: 'center', justifyContent: 'center',
      gap: 18, fontFamily: 'var(--font-mono)', fontSize: 22,
      color: 'var(--ink-soft)',
      opacity: op,
      zIndex: 5,
    }}>
      {['Charter','BRD','FFD','Maps','Code','UAT'].map((p, i) => {
        const active = i === 0;
        return (
          <React.Fragment key={p}>
            <span style={{ display: 'inline-flex', alignItems: 'center', gap: 10 }}>
              <span style={{
                width: 16, height: 16, borderRadius: '50%',
                background: active ? 'var(--orange-hot)' : 'var(--cream-rule)',
                border: `2px solid ${active ? 'var(--orange-hot)' : 'var(--ink-faint)'}`,
                boxShadow: active ? '0 0 0 6px rgba(212,107,62,0.18)' : 'none',
              }} />
              <span style={{
                color: active ? 'var(--orange-hot)' : 'inherit',
                fontWeight: active ? 700 : 400,
                letterSpacing: '0.04em',
              }}>{i+1}. {p}</span>
            </span>
            {i < 5 && <span style={{ color: 'var(--ink-faint)' }}>→</span>}
          </React.Fragment>
        );
      })}
    </div>
  );
}

function Act2Notebook() {
  const t = useT();
  // Slide in from right between 1.5–2.5s
  const slideIn = Math.min(1, Math.max(0, (t - 1500) / 1100));
  const slideEased = 1 - Math.pow(1 - slideIn, 2);
  // Lift out 17.0–20.0s
  const slideOut = Math.min(1, Math.max(0, (t - 17000) / 3000));
  const slideOutEased = Math.pow(slideOut, 2);

  const tx = (1 - slideEased) * 1200 + slideOutEased * 1500;
  const opacity = Math.min(slideEased, 1 - slideOutEased * 0.95);

  return (
    <div className="notebook" style={{
      position: 'absolute',
      left: 200, top: 130,
      width: 1520, height: 880,
      transform: `translateX(${tx}px) translateY(${slideOutEased * -40}px) rotate(${slideOutEased * 2}deg)`,
      opacity,
      padding: '60px 100px',
      boxSizing: 'border-box',
      zIndex: 2,
    }}>
      <div className="notebook__ribbon" />

      {/* PHASE header */}
      <div style={{ marginBottom: 36, opacity: t > 2400 ? 1 : 0 }}>
        <div className="phase-header" style={{ fontSize: 22 }}>
          <Typewriter text="PHASE 1: PROJECT CHARTER" from={2400} duration={900} caret={false} />
        </div>
        <div className="phase-header__rule" style={{
          maxWidth: 540,
          width: t > 3300 ? Math.min(540, (t - 3300) * 1.2) : 0,
          transition: 'none',
        }} />
        <div style={{
          fontFamily: 'var(--font-mono)',
          fontSize: 18, color: 'var(--ink-faint)',
          letterSpacing: '0.16em', marginTop: 14,
          opacity: t > 3700 ? 1 : 0,
        }}>
          😍 HUMAN ONLY
        </div>
      </div>

      {/* PROBLEM STATEMENT */}
      <Section
        title="PROBLEM STATEMENT"
        hint="What's the pain?"
        body="Brokers run on a 60-day escrow clock. Every day of uncertainty is a day closer to a failed deal."
        headerStart={4800}
        bodyStart={5800}
      />

      {/* GOAL STATEMENT */}
      <div style={{ marginTop: 40 }}>
        <Section
          title="GOAL STATEMENT"
          hint="What does success look like?"
          body="Speed to Terms — deliver a credible term summary to the borrower as fast as possible."
          headerStart={8400}
          bodyStart={9400}
        />
      </div>

      {/* VOC quotes */}
      <div style={{ marginTop: 44 }}>
        <SectionHeader title="VOICE OF CUSTOMER" hint="What do users actually say?" startMs={11800} />
        <div style={{ display: 'flex', gap: 24, marginTop: 22 }}>
          {[
            { q: 'I don\u2019t want to be asked for the same thing twice.', from: 12700 },
            { q: 'Why did you go dark on me?',                          from: 12950 },
            { q: 'Who has the ball right now?',                         from: 13200 },
          ].map((c, i) => {
            const op = Math.min(1, Math.max(0, (t - c.from) / 500));
            const ty = (1 - op) * 16;
            return (
              <div key={i} className="quote-card" style={{
                flex: 1, opacity: op,
                transform: `translateY(${ty}px)`,
                fontSize: 20, padding: '20px 22px',
              }}>
                <span className="qmark">"</span>
                {c.q}
              </div>
            );
          })}
        </div>
      </div>
    </div>
  );
}

function SectionHeader({ title, hint, startMs }) {
  const t = useT();
  // Gate hint visibility behind title typewriter completion (~650ms)
  const hintReady = t > startMs + 500;
  const hintOp = hintReady ? Math.min(1, (t - (startMs + 500)) / 350) : 0;
  return (
    <div className="section-head" style={{ fontSize: 24, alignItems: 'baseline', flexWrap: 'nowrap' }}>
      <span style={{
        display: 'inline-flex', alignItems: 'baseline', gap: 14,
        flexShrink: 0, whiteSpace: 'nowrap',
      }}>
        <span style={{ color: 'var(--ink)', fontWeight: 700 }}>
          <Typewriter text={title} from={startMs} duration={650} caret={false} />
        </span>
        <span style={{
          fontFamily: 'var(--font-serif)',
          fontStyle: 'italic',
          textTransform: 'none',
          letterSpacing: 'normal',
          fontSize: 22,
          color: 'var(--ink-soft)',
          fontWeight: 400,
          opacity: hintOp,
          whiteSpace: 'nowrap',
        }}>
          ({hint})
        </span>
      </span>
      <span className="section-head__rule" style={{ flex: 1, minWidth: 60 }} />
    </div>
  );
}

function Section({ title, hint, body, headerStart, bodyStart }) {
  return (
    <div>
      <SectionHeader title={title} hint={hint} startMs={headerStart} />
      <div style={{
        marginTop: 16,
        fontFamily: 'var(--font-serif)',
        fontSize: 28,
        color: 'var(--ink)',
        lineHeight: 1.5,
        maxWidth: 1100,
      }}>
        <Typewriter text={body} from={bodyStart} duration={1700} caret blinkAfter />
      </div>
    </div>
  );
}

window.SceneAct2 = SceneAct2;
