// scenes-3-4.jsx — DeftForge scenes 3 (4 steps) + 4 (split-screen: old way vs new way)

// ── Scene 3 — Four steps ───────────────────────────────────────────────────
function Scene3FourSteps() {
  const { localTime } = useSprite();

  const steps = [
    { n: '01', title: 'discover', desc: '30-min call. We map the manual work eating your hours.', accent: COLORS.ember },
    { n: '02', title: 'build',    desc: 'End-to-end. No status meetings. No slideware.',          accent: COLORS.ember },
    { n: '03', title: 'refine',   desc: 'Your team uses it. We iterate fast. No ceremony.',      accent: COLORS.ember },
    { n: '04', title: 'decide',   desc: 'Pay only if it earned its place. Walk away anytime.',   accent: COLORS.ember },
  ];

  const stepDur = 2.0;
  const idx = Math.min(steps.length - 1, Math.floor(localTime / stepDur));
  const stepT = (localTime - idx * stepDur) / stepDur;
  const step = steps[idx];

  const enterT = clamp(stepT / 0.25, 0, 1);
  const exitT = clamp((stepT - 0.85) / 0.15, 0, 1);

  return (
    <div style={{
      position: 'absolute', inset: 0,
      background: COLORS.bg,
      overflow: 'hidden',
      display: 'flex',
      flexDirection: 'column',
    }}>
      <div style={{
        position: 'absolute', left: 80, right: 80, top: 60,
        display: 'flex', gap: 12,
      }}>
        {steps.map((s, i) => {
          const isPast = i < idx;
          const isCur = i === idx;
          const fillPct = isPast ? 1 : isCur ? clamp(stepT, 0, 1) : 0;
          return (
            <div key={i} style={{ flex: 1 }}>
              <div style={{
                fontFamily: FONT_MONO, fontSize: 12,
                color: isCur || isPast ? COLORS.fg : COLORS.dim,
                letterSpacing: '0.16em',
                marginBottom: 8, opacity: 0.9,
              }}>
                {s.n} · {s.title}
              </div>
              <div style={{ height: 2, background: COLORS.faint, position: 'relative', overflow: 'hidden' }}>
                <div style={{
                  position: 'absolute', inset: 0,
                  background: COLORS.ember,
                  transform: `scaleX(${fillPct})`,
                  transformOrigin: 'left',
                  transition: 'transform 100ms linear',
                }} />
              </div>
            </div>
          );
        })}
      </div>

      <div style={{ flex: 1, display: 'flex', alignItems: 'center', padding: '0 120px' }}>
        <div style={{
          opacity: clamp(1 - exitT, 0, 1),
          transform: `translateX(${lerp(-40, 0, Easing.easeOutCubic(enterT)) + lerp(0, -20, exitT)}px)`,
          width: '100%',
        }}>
          <div style={{
            fontFamily: FONT_MONO, fontSize: 48,
            color: step.accent, letterSpacing: '0.04em',
            marginBottom: 32, opacity: enterT,
          }}>
            ↳ {step.n}
          </div>
          <div style={{
            fontFamily: FONT_SANS, fontWeight: 800, fontSize: 220,
            letterSpacing: '-0.05em', color: COLORS.fg, lineHeight: 0.95,
            display: 'inline-flex', alignItems: 'baseline',
            transform: `translateY(${lerp(40, 0, Easing.easeOutBack(enterT))}px)`,
          }}>
            <span>{step.title}</span>
            <span style={{ color: step.accent }}>.</span>
          </div>
          <div style={{
            marginTop: 40, fontFamily: FONT_SANS, fontWeight: 400,
            fontSize: 30, color: COLORS.dim, maxWidth: 900,
            opacity: clamp((stepT - 0.25) / 0.2, 0, 1),
            letterSpacing: '-0.01em',
          }}>
            {step.desc}
          </div>
        </div>
      </div>

      <div style={{
        position: 'absolute', left: 80, bottom: 60,
        fontFamily: FONT_MONO, fontSize: 14, color: COLORS.dim,
        letterSpacing: '0.16em', textTransform: 'uppercase',
      }}>
        process · {step.n}/04
      </div>
      <div style={{
        position: 'absolute', right: 80, bottom: 60,
        fontFamily: FONT_MONO, fontSize: 14, color: COLORS.dim,
        letterSpacing: '0.16em', textTransform: 'uppercase',
      }}>
        no retainer · no contract
      </div>

      <Grain opacity={0.05} />
    </div>
  );
}


// ── Scene 4 — Split screen: the old way vs the new way ────────────────────
// Designed for small-business viewers: the painful left side shows late-night
// manual work (overflowing inbox, sticky notes, growing spreadsheet, clock).
// The clean right side shows their forged system handling it calmly.
//
// Beats:
//   0.0 → 0.4s   "the old way" / "the new way" labels appear
//   0.4 → 4.5s   Both sides animate; left grows chaotic, right stays calm
//   4.5 → 6.5s   Hold on the contrast; closing line appears
function Scene4SplitScreen() {
  const { localTime, progress } = useSprite();
  const dur = 6.5;

  const labelsT = clamp(localTime / 0.4, 0, 1);
  const contentT = clamp((localTime - 0.4) / 0.4, 0, 1);
  const closingT = clamp((localTime - 4.8) / 0.6, 0, 1);

  // Left side chaos intensity (builds over time)
  const chaosT = clamp((localTime - 0.4) / 4.0, 0, 1);

  return (
    <div style={{
      position: 'absolute', inset: 0,
      background: COLORS.bg,
      overflow: 'hidden',
      display: 'flex',
    }}>
      {/* ── LEFT: The old way ─────────────────────────────────────────── */}
      <div style={{
        flex: 1,
        position: 'relative',
        background: '#0e0a08',
        borderRight: `1px solid ${COLORS.faint}`,
        overflow: 'hidden',
        opacity: contentT,
      }}>
        <SideLabel
          eyebrow="without deftforge"
          title="the old way"
          mono
          color="#a86b54"
          opacity={labelsT}
        />

        <OldWayPanel chaosT={chaosT} localTime={localTime} />

        {/* Red tint that intensifies */}
        <div style={{
          position: 'absolute', inset: 0,
          background: 'radial-gradient(ellipse at 50% 60%, rgba(180, 50, 30, 0.18), transparent 70%)',
          opacity: chaosT,
          pointerEvents: 'none',
        }} />
      </div>

      {/* ── RIGHT: The new way ────────────────────────────────────────── */}
      <div style={{
        flex: 1,
        position: 'relative',
        background: COLORS.bg,
        overflow: 'hidden',
        opacity: contentT,
      }}>
        <SideLabel
          eyebrow="with deftforge"
          title="the new way"
          color={COLORS.ember}
          opacity={labelsT}
        />

        <NewWayPanel localTime={localTime} />

        {/* Subtle ember halo */}
        <div style={{
          position: 'absolute', inset: 0,
          background: 'radial-gradient(ellipse at 50% 70%, rgba(255, 107, 53, 0.10), transparent 70%)',
          pointerEvents: 'none',
        }} />
      </div>

      {/* Center divider — animated vertical line */}
      <div style={{
        position: 'absolute',
        left: '50%', top: 0, bottom: 0,
        width: 1,
        background: COLORS.faint,
        transform: 'translateX(-50%)',
      }}>
        <div style={{
          position: 'absolute',
          left: '50%', top: '50%',
          transform: 'translate(-50%, -50%)',
          width: 12, height: 12,
          background: COLORS.ember,
          opacity: contentT * 0.9,
          boxShadow: `0 0 24px ${COLORS.ember}`,
          borderRadius: 2,
          rotate: '45deg',
        }} />
      </div>

      {/* Closing line — bottom center */}
      <div style={{
        position: 'absolute',
        left: 0, right: 0, bottom: 60,
        textAlign: 'center',
        opacity: closingT,
        transform: `translateY(${lerp(12, 0, Easing.easeOutCubic(closingT))}px)`,
      }}>
        <div style={{
          fontFamily: FONT_MONO,
          fontSize: 14,
          color: COLORS.dim,
          letterSpacing: '0.18em',
          textTransform: 'uppercase',
          marginBottom: 12,
        }}>
          same work · 1/40th the time
        </div>
        <div style={{
          fontFamily: FONT_SANS,
          fontWeight: 700,
          fontSize: 56,
          color: COLORS.fg,
          letterSpacing: '-0.03em',
        }}>
          Built for small teams. <span style={{ color: COLORS.ember }}>Built around you.</span>
        </div>
      </div>

      <Grain opacity={0.05} />
    </div>
  );
}

function SideLabel({ eyebrow, title, color, opacity, mono }) {
  return (
    <div style={{
      position: 'absolute',
      left: 64, top: 64,
      opacity,
    }}>
      <div style={{
        fontFamily: FONT_MONO, fontSize: 13,
        color, letterSpacing: '0.18em',
        textTransform: 'uppercase', marginBottom: 12,
        opacity: 0.85,
      }}>
        ▸ {eyebrow}
      </div>
      <div style={{
        fontFamily: FONT_SANS, fontWeight: 800,
        fontSize: 64, color: '#f5f5f4',
        letterSpacing: '-0.04em', lineHeight: 1,
      }}>
        {title}<span style={{ color }}>.</span>
      </div>
    </div>
  );
}

// ── OLD WAY ────────────────────────────────────────────────────────────────
// Visual cocktail of small-business pain:
//   - Overflowing inbox counter (climbs)
//   - Spreadsheet rows piling up with errors
//   - Sticky notes scattered with "do this", "remember", "URGENT"
//   - Wall clock ticking past hours
function OldWayPanel({ chaosT, localTime }) {
  // Inbox counter climbs aggressively
  const unread = 47 + Math.floor(localTime * 32);
  // Hours worked counter
  const hours = (localTime * 0.95).toFixed(1);

  // Sticky notes — fixed positions, shake intensity grows
  const stickies = [
    { x: 60, y: 280, rot: -6, color: '#f4d35e', text: 'reconcile\nstripe vs.\nledger', urgent: false },
    { x: 250, y: 340, rot: 4, color: '#ee9b6b', text: 'follow up\n— sarah?', urgent: false },
    { x: 70, y: 480, rot: 8, color: '#f4d35e', text: 'tax docs\nDUE FRI', urgent: true },
    { x: 240, y: 520, rot: -4, color: '#fbb13c', text: 'invoice\n#0427\nlate', urgent: true },
    { x: 130, y: 660, rot: 2, color: '#f4d35e', text: 'call vendor\nre: PO mix-up', urgent: false },
    { x: 290, y: 720, rot: -10, color: '#ee9b6b', text: 'send Q3\nreport', urgent: false },
  ];

  const shake = chaosT * 1.5;

  return (
    <div style={{
      position: 'absolute',
      left: 0, right: 0, top: 220, bottom: 180,
    }}>
      {/* Inbox card — top right */}
      <div style={{
        position: 'absolute',
        right: 60, top: 0,
        width: 320,
        background: '#1a1310',
        border: '1px solid #3a2820',
        borderRadius: 6,
        padding: '20px 24px',
        transform: `translate(${Math.sin(localTime * 13) * shake}px, ${Math.cos(localTime * 11) * shake}px) rotate(${-1 + Math.sin(localTime * 2) * 0.5}deg)`,
      }}>
        <div style={{
          fontFamily: FONT_MONO, fontSize: 12,
          color: '#a86b54', letterSpacing: '0.16em',
          textTransform: 'uppercase', marginBottom: 12,
        }}>
          inbox · unread
        </div>
        <div style={{
          fontFamily: FONT_SANS, fontWeight: 800,
          fontSize: 96, color: '#f0a070',
          letterSpacing: '-0.04em', lineHeight: 1,
          fontVariantNumeric: 'tabular-nums',
        }}>
          {unread}
        </div>
        <div style={{
          marginTop: 8,
          fontFamily: FONT_MONO, fontSize: 13,
          color: '#a86b54',
        }}>
          ↑ growing
        </div>
      </div>

      {/* Wall clock — bottom right */}
      <div style={{
        position: 'absolute',
        right: 60, bottom: 0,
        width: 320,
        background: '#1a1310',
        border: '1px solid #3a2820',
        borderRadius: 6,
        padding: '20px 24px',
        transform: `rotate(${1 + Math.cos(localTime * 1.7) * 0.4}deg)`,
      }}>
        <div style={{
          fontFamily: FONT_MONO, fontSize: 12,
          color: '#a86b54', letterSpacing: '0.16em',
          textTransform: 'uppercase', marginBottom: 12,
        }}>
          hours this week
        </div>
        <div style={{
          fontFamily: FONT_SANS, fontWeight: 800,
          fontSize: 96, color: '#f0a070',
          letterSpacing: '-0.04em', lineHeight: 1,
          fontVariantNumeric: 'tabular-nums',
        }}>
          {62 + Math.floor(localTime * 1.2)}
        </div>
        <div style={{
          marginTop: 8,
          fontFamily: FONT_MONO, fontSize: 13,
          color: '#a86b54',
        }}>
          on manual work
        </div>
      </div>

      {/* Sticky note pile — left side */}
      <div style={{
        position: 'absolute',
        left: 0, top: 0, bottom: 0,
        width: 460,
      }}>
        {stickies.map((s, i) => {
          const appearT = clamp((localTime - 0.6 - i * 0.25) / 0.4, 0, 1);
          if (appearT <= 0) return null;
          const wobble = Math.sin(localTime * (3 + i * 0.7) + i) * shake * 0.5;
          return (
            <div key={i} style={{
              position: 'absolute',
              left: s.x,
              top: s.y,
              width: 130,
              height: 130,
              background: s.color,
              boxShadow: '4px 6px 16px rgba(0,0,0,0.5)',
              transform: `rotate(${s.rot + wobble * 0.3}deg) scale(${Easing.easeOutBack(appearT)})`,
              opacity: appearT,
              padding: 14,
              fontFamily: 'Caveat, "Comic Sans MS", cursive',
              fontSize: s.urgent ? 22 : 19,
              fontWeight: s.urgent ? 700 : 500,
              color: s.urgent ? '#a02020' : '#3a2510',
              lineHeight: 1.15,
              whiteSpace: 'pre-line',
              textTransform: s.urgent ? 'uppercase' : 'none',
            }}>
              {s.text}
            </div>
          );
        })}
      </div>
    </div>
  );
}

// ── NEW WAY ────────────────────────────────────────────────────────────────
// One clean automation panel showing tasks flowing in and being handled
// calmly. Same task names as OldWayPanel pain points — direct comparison.
function NewWayPanel({ localTime }) {
  const TASKS_PER_SECOND = 1.2;
  const completedCount = Math.floor(localTime * TASKS_PER_SECOND);

  const taskNames = [
    'reconcile-stripe',
    'inbox-triage',
    'invoice-followup',
    'vendor-po-check',
    'tax-doc-collect',
    'q3-report-draft',
    'lead-tag',
    'send-followup',
  ];

  // Slot system — newest task at top, exactly N visible
  const SLOTS = 5;
  const activeWindow = SLOTS / TASKS_PER_SECOND;
  const activeTasks = [];
  const newestIdx = Math.floor(localTime * TASKS_PER_SECOND);
  for (let k = 0; k < SLOTS; k++) {
    const i = newestIdx - k;
    if (i < 0) continue;
    const startTime = i / TASKS_PER_SECOND;
    const t = (localTime - startTime) / activeWindow;
    if (t < 0 || t > 1) continue;
    activeTasks.push({ i, t, slot: k, name: taskNames[i % taskNames.length] });
  }

  return (
    <div style={{
      position: 'absolute',
      left: 60, right: 60, top: 220, bottom: 180,
      display: 'flex', flexDirection: 'column', gap: 20,
    }}>
      {/* Header card */}
      <div style={{
        background: COLORS.panel,
        border: `1px solid ${COLORS.faint}`,
        borderRadius: 8,
        padding: '20px 24px',
        display: 'flex',
        justifyContent: 'space-between',
        alignItems: 'center',
      }}>
        <div>
          <div style={{
            fontFamily: FONT_MONO, fontSize: 12,
            color: COLORS.dim, letterSpacing: '0.16em',
            textTransform: 'uppercase', marginBottom: 6,
          }}>
            your system
          </div>
          <div style={{
            fontFamily: FONT_SANS, fontWeight: 700,
            fontSize: 24, color: COLORS.fg,
            letterSpacing: '-0.02em',
          }}>
            ops/automated
          </div>
        </div>
        <div style={{
          display: 'flex', alignItems: 'center', gap: 10,
          fontFamily: FONT_MONO, fontSize: 14,
          color: COLORS.ember, letterSpacing: '0.06em',
        }}>
          <span style={{
            width: 10, height: 10, borderRadius: '50%',
            background: COLORS.ember,
            boxShadow: `0 0 12px ${COLORS.ember}`,
            opacity: 0.6 + 0.4 * (Math.sin(localTime * 4) * 0.5 + 0.5),
          }} />
          running
        </div>
      </div>

      {/* Task stream */}
      <div style={{
        flex: 1,
        background: COLORS.panel,
        border: `1px solid ${COLORS.faint}`,
        borderRadius: 8,
        padding: '20px 24px',
        position: 'relative',
        overflow: 'hidden',
      }}>
        {activeTasks.map(({ i, t, slot, name }) => {
          const opacity = t < 0.08 ? t / 0.08 : t > 0.92 ? (1 - t) / 0.08 : 1;
          const isDone = t > 0.5;
          const animSlot = slot - (1 - Easing.easeOutCubic(Math.min(1, t / 0.15))) * 1;
          const rowH = 60;
          return (
            <div key={i} style={{
              position: 'absolute',
              top: 16 + animSlot * rowH,
              left: 24, right: 24,
              display: 'flex', alignItems: 'center', gap: 16,
              fontFamily: FONT_MONO, fontSize: 18,
              color: isDone ? COLORS.dim : COLORS.fg,
              opacity,
              transition: 'top 220ms cubic-bezier(0.4, 0, 0.2, 1)',
            }}>
              <span style={{
                width: 10, height: 10,
                background: isDone ? COLORS.ember : COLORS.fg,
                borderRadius: '50%',
                flexShrink: 0,
                boxShadow: isDone ? `0 0 8px ${COLORS.ember}` : 'none',
              }} />
              <span style={{
                width: 220,
                textDecoration: isDone ? 'line-through' : 'none',
              }}>
                {name}
              </span>
              <div style={{
                flex: 1, height: 4,
                background: COLORS.faint,
                borderRadius: 2,
                overflow: 'hidden',
                marginRight: 16,
              }}>
                <div style={{
                  height: '100%',
                  width: `${Math.min(100, (t / 0.5) * 100)}%`,
                  background: COLORS.ember,
                  transition: 'width 80ms linear',
                }} />
              </div>
              <span style={{
                fontSize: 13,
                color: isDone ? COLORS.ember : COLORS.dim,
                width: 60, textAlign: 'right',
                fontVariantNumeric: 'tabular-nums',
                letterSpacing: '0.04em',
              }}>
                {isDone ? 'DONE' : `${String(Math.floor((t / 0.5) * 100)).padStart(2, '0')}%`}
              </span>
            </div>
          );
        })}
      </div>

      {/* Bottom stat row */}
      <div style={{ display: 'flex', gap: 16 }}>
        <SmallStat label="hours saved / wk" value={Math.floor(localTime * 8 + 28)} accent={COLORS.ember} />
        <SmallStat label="inbox · unread" value={3} accent={COLORS.fg} />
        <SmallStat label="auto-handled" value={completedCount + 142} accent={COLORS.fg} />
      </div>
    </div>
  );
}

function SmallStat({ label, value, accent }) {
  return (
    <div style={{
      flex: 1,
      background: COLORS.panel,
      border: `1px solid ${COLORS.faint}`,
      borderRadius: 8,
      padding: '16px 20px',
    }}>
      <div style={{
        fontFamily: FONT_MONO, fontSize: 11,
        color: COLORS.dim, letterSpacing: '0.16em',
        textTransform: 'uppercase', marginBottom: 8,
      }}>
        {label}
      </div>
      <div style={{
        fontFamily: FONT_SANS, fontWeight: 700,
        fontSize: 44, color: accent,
        letterSpacing: '-0.03em', lineHeight: 1,
        fontVariantNumeric: 'tabular-nums',
      }}>
        {value}
      </div>
    </div>
  );
}

// Backwards-compat alias (in case anything still imports the old name)
const Scene4Working = Scene4SplitScreen;

Object.assign(window, {
  Scene3FourSteps, Scene4SplitScreen, Scene4Working,
  OldWayPanel, NewWayPanel, SideLabel, SmallStat,
});
