// Capability + Process + CTA + Footer.

const CapabilityInner = ({ t }) => {
  const pressed = t > 0.02;
  const fillFor = (i) => Math.max(0.18, Math.min(1, t * 1.4 - i * 0.06));

  const traits = [
    'naturalness', 'prosody', 'turn-taking', 'emotional fidelity', 'voice-native AI',
  ];

  return (
    <div className="ex-capability" style={{
      padding: '110px 40px 60px',
      background: 'var(--ex-ink)',
      color: 'var(--ex-cream)',
      borderBottom: '1px solid rgba(240,238,233,0.1)',
      flex: 1, display: 'flex', flexDirection: 'column',
      justifyContent: 'center',
      overflow: 'hidden',
    }}>
      <div style={{ maxWidth: 1320, margin: '0 auto', width: '100%' }}>
        <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'flex-end', marginBottom: 40 }}>
          <div>
            <div className="hero-meta" style={{ position: 'static', marginBottom: 16, color: 'rgba(240,238,233,0.6)' }}>
              CAPABILITY · /003
            </div>
            <h2 style={{
              fontFamily: 'var(--ex-serif)', fontWeight: 300,
              fontSize: 64, lineHeight: 1.05, letterSpacing: '-0.025em',
              margin: 0, color: 'var(--ex-cream)',
            }}>
              More data, <em style={{ fontStyle: 'italic', color: 'var(--ex-signal)' }}>more capability.</em>
            </h2>
            <p style={{ maxWidth: 540, marginTop: 18, fontSize: 16, lineHeight: 1.55, color: 'rgba(240,238,233,0.7)' }}>
              Each dimension of human voice is a feed into the model.
            </p>
          </div>
        </div>

        {/* Pipeline diagram: feeds -> model -> output */}
        <div className="ex-capability-grid" style={{
          display: 'grid', gridTemplateColumns: '1.2fr auto 1fr', gap: 40,
          alignItems: 'center',
          padding: '20px 0 0',
        }}>
          {/* Feeds */}
          <div style={{ display: 'flex', flexDirection: 'column', gap: 14 }}>
            {traits.map((trait, i) => {
              const fill = fillFor(i);
              return (
                <div key={trait} style={{ display: 'flex', alignItems: 'center', gap: 14 }}>
                  <div className="ex-capability-trait" style={{
                    fontFamily: 'var(--ex-mono)', fontSize: 11,
                    letterSpacing: '0.06em', textTransform: 'uppercase',
                    width: 170, textAlign: 'right',
                    color: t > i * 0.08 ? 'var(--ex-cream)' : 'rgba(240,238,233,0.5)',
                  }}>{trait}</div>
                  <div style={{ flex: 1, height: 22, position: 'relative' }}>
                    <div style={{
                      position: 'absolute', inset: 0,
                      border: '1px solid rgba(240,238,233,0.15)',
                      borderRadius: 2,
                    }}/>
                    {/* moving fill */}
                    <div style={{
                      position: 'absolute', left: 0, top: 0, bottom: 0,
                      width: `${fill * 100}%`,
                      background: t > i * 0.08
                        ? `linear-gradient(90deg, oklch(0.68 0.22 30 / 0.4), oklch(0.68 0.22 30))`
                        : 'rgba(240,238,233,0.08)',
                      transition: 'background 0.4s',
                    }}/>
                    {/* moving dots */}
                    {t > i * 0.08 && Array.from({ length: 6 }).map((_, j) => {
                      const phase = (t * 0.7 + j * 0.16) % 1;
                      return (
                        <div key={j} style={{
                          position: 'absolute',
                          left: `${phase * 100}%`,
                          top: '50%', transform: 'translate(-50%, -50%)',
                          width: 4, height: 4, borderRadius: '50%',
                          background: 'var(--ex-cream)',
                          opacity: phase < 0.92 ? 1 : (1 - phase) * 12,
                        }}/>
                      );
                    })}
                  </div>
                </div>
              );
            })}
            <div style={{
              marginTop: 14,
              fontFamily: 'var(--ex-mono)', fontSize: 10,
              color: 'rgba(240,238,233,0.5)',
              letterSpacing: '0.08em', textTransform: 'uppercase',
            }}>
              {pressed ? '● TRAINING DATA STREAMING' : '○ STREAMS IDLE'}
            </div>
          </div>

          {/* Model */}
          <div style={{
            width: 220, height: 220,
            border: '1px solid rgba(240,238,233,0.2)',
            borderRadius: 6,
            position: 'relative',
            display: 'grid', placeItems: 'center',
            background: t > 0.4 ? 'oklch(0.68 0.22 30 / 0.08)' : 'transparent',
            transition: 'background 0.4s',
          }}>
            {/* concentric activation rings */}
            {[0, 1, 2].map(i => (
              <div key={i} style={{
                position: 'absolute',
                inset: 20 + i * 26,
                border: '1px solid rgba(240,238,233,0.18)',
                borderRadius: '50%',
                opacity: t > 0.3 ? 1 : 0.4,
                transform: `scale(${1 + Math.sin(t * 4 + i) * 0.04 * (t > 0.3 ? 1 : 0)})`,
                transition: 'opacity 0.4s',
              }}/>
            ))}
            <div style={{ textAlign: 'center', zIndex: 1 }}>
              <div style={{
                fontFamily: 'var(--ex-mono)', fontSize: 9,
                letterSpacing: '0.1em', opacity: 0.5,
              }}>VOICE MODEL</div>
              <div style={{
                fontFamily: 'var(--ex-serif)', fontWeight: 300,
                fontSize: 44, lineHeight: 1, marginTop: 6,
                color: t > 0.4 ? 'var(--ex-signal)' : 'var(--ex-cream)',
                transition: 'color 0.4s',
              }}>{(t * 100).toFixed(0)}%</div>
              <div style={{
                fontFamily: 'var(--ex-mono)', fontSize: 9,
                letterSpacing: '0.1em', opacity: 0.5, marginTop: 6,
              }}>TRAINING</div>
            </div>
            {/* corner ticks */}
            {[[0,0],[1,0],[0,1],[1,1]].map(([x,y], i) => (
              <span key={i} style={{
                position: 'absolute',
                left: x ? 'auto' : -1, right: x ? -1 : 'auto',
                top: y ? 'auto' : -1, bottom: y ? -1 : 'auto',
                width: 8, height: 8,
                borderLeft: x ? 0 : `1px solid ${t > 0.4 ? 'var(--ex-signal)' : 'rgba(240,238,233,0.4)'}`,
                borderRight: x ? `1px solid ${t > 0.4 ? 'var(--ex-signal)' : 'rgba(240,238,233,0.4)'}` : 0,
                borderTop: y ? 0 : `1px solid ${t > 0.4 ? 'var(--ex-signal)' : 'rgba(240,238,233,0.4)'}`,
                borderBottom: y ? `1px solid ${t > 0.4 ? 'var(--ex-signal)' : 'rgba(240,238,233,0.4)'}` : 0,
              }}/>
            ))}
          </div>

          {/* Output */}
          <div style={{ display: 'flex', flexDirection: 'column', gap: 18 }}>
            <div style={{
              fontFamily: 'var(--ex-mono)', fontSize: 10,
              letterSpacing: '0.08em', textTransform: 'uppercase',
              color: 'rgba(240,238,233,0.5)',
            }}>OUTPUT VOICE</div>
            {/* synthesized waveform — quality grows with t */}
            <svg viewBox="0 0 360 80" style={{ width: '100%', height: 80 }}>
              <path d={synthWave(360, 80, t)}
                fill="none"
                stroke={t > 0.3 ? 'oklch(0.68 0.22 30)' : 'rgba(240,238,233,0.3)'}
                strokeWidth="1.4"/>
            </svg>
            <div style={{
              fontFamily: 'var(--ex-serif)', fontWeight: 300,
              fontSize: 32, lineHeight: 1.15, letterSpacing: '-0.015em',
              color: 'var(--ex-cream)',
            }}>
              <span style={{ opacity: t > 0.1 ? 1 : 0.3, transition: 'opacity 0.4s' }}>flat</span>
              <span style={{ opacity: 0.3, margin: '0 12px' }}>→</span>
              <span style={{ opacity: t > 0.4 ? 1 : 0.3, transition: 'opacity 0.4s' }}>natural</span>
              <span style={{ opacity: 0.3, margin: '0 12px' }}>→</span>
              <em style={{
                fontStyle: 'italic',
                color: t > 0.7 ? 'var(--ex-signal)' : 'rgba(240,238,233,0.4)',
                transition: 'color 0.4s',
              }}>human</em>
            </div>
            <div style={{
              fontFamily: 'var(--ex-mono)', fontSize: 10,
              color: 'rgba(240,238,233,0.5)',
              letterSpacing: '0.06em',
            }}>
              {t < 0.3 ? 'monotone · robotic' : t < 0.6 ? 'fluent · readable' : t < 0.85 ? 'expressive · natural' : 'warm · indistinguishable'}
            </div>
          </div>
        </div>
      </div>
    </div>
  );
};

const Capability = () => (
  <PinnedScrub scrubLength={1.5} background="var(--ex-ink)" sectionId="capability">
    {(t) => <CapabilityInner t={t}/>}
  </PinnedScrub>
);

function synthWave(w, h, t) {
  const pts = 80;
  let d = `M 0 ${h/2}`;
  for (let i = 1; i <= pts; i++) {
    const x = (i / pts) * w;
    const phase = i * 0.4 + t * 8;
    // Higher t = more harmonics, more variation
    const amp = (h / 2 - 6) * (0.3 + t * 0.7);
    const env = Math.sin((i / pts) * Math.PI);
    let y = h/2;
    y += Math.sin(phase) * amp * env * 0.5;
    if (t > 0.2) y += Math.sin(phase * 2.4) * amp * env * 0.3 * Math.min(1, (t - 0.2) * 4);
    if (t > 0.5) y += Math.sin(phase * 5.1 + 1) * amp * env * 0.18 * Math.min(1, (t - 0.5) * 4);
    d += ` L ${x.toFixed(1)} ${y.toFixed(1)}`;
  }
  return d;
}

const Process = () => {
  const steps = [
    { n: '01', name: 'Scope',    desc: '30-minute call to confirm languages, domains, annotation depth, and delivery format.', meta: 'DAY 0 · ~30 MIN' },
    { n: '02', name: 'Sample',   desc: 'Representative cut delivered inside 48 hours. Listen, inspect, request adjustments.',  meta: 'DAY 1–2 · 48 H' },
    { n: '03', name: 'Contract', desc: 'Licensing terms locked. Custom collection programs kick off in parallel if in scope.',  meta: 'DAY 3–5' },
    { n: '04', name: 'Deliver',  desc: 'Audio, transcripts, and annotation layers shipped to your cloud. Ongoing support included.', meta: 'DAY 6–14' },
  ];
  return (
    <PinnedScrub scrubLength={1.6} background="var(--ex-cream)" sectionId="process">
      {(t) => {
        // Sequence: header settles 0 → 0.1, then four steps light up 0.1→0.85, dwell 0.85→1.
        // Each step has its own peak: step i lights up around (0.1 + i * 0.18).
        const stepProgress = (i) => {
          const start = 0.10 + i * 0.18;
          const end = start + 0.18;
          if (t < start) return 0;
          if (t > end) return 1;
          return (t - start) / (end - start);
        };
        // Connector line fill — a continuous bar that grows across all 4 steps
        const lineFill = Math.max(0, Math.min(1, (t - 0.10) / 0.72));
        const headDone = Math.min(1, t / 0.08);

        return (
          <section className="ex-section-padded" style={{ padding: '110px 40px 60px', flex: 1, display: 'flex', flexDirection: 'column', justifyContent: 'center', minHeight: 0 }}>
            <div style={{ maxWidth: 1320, margin: '0 auto', width: '100%' }}>
              <div style={{
                display: 'flex', justifyContent: 'space-between', alignItems: 'flex-end', marginBottom: 60,
                opacity: headDone, transform: `translateY(${(1 - headDone) * 8}px)`,
                transition: 'transform 0.2s',
              }}>
                <div>
                  <div className="hero-meta" style={{ position: 'static', marginBottom: 16 }}>PROCESS · /004</div>
                  <h2 style={{
                    fontFamily: 'var(--ex-serif)', fontWeight: 300,
                    fontSize: 64, lineHeight: 1.05, letterSpacing: '-0.025em', margin: 0,
                    maxWidth: 720,
                  }}>
                    From request to delivery in <em style={{ fontStyle: 'italic', color: 'var(--ex-signal)' }}>under two weeks.</em>
                  </h2>
                </div>
                <p style={{ maxWidth: 360, margin: 0, fontSize: 15, lineHeight: 1.55, color: 'var(--ex-ink-2)', textAlign: 'right' }}>
                  Every engagement starts with a scoped sample cut. If the cut is right, we move to full delivery on your infrastructure.
                </p>
              </div>

              {/* Connector bar */}
              <div style={{ position: 'relative', height: 2, background: 'var(--ex-rule)', marginBottom: 0 }}>
                <div style={{
                  position: 'absolute', left: 0, top: 0, bottom: 0,
                  width: `${lineFill * 100}%`,
                  background: 'var(--ex-signal)',
                  boxShadow: '0 0 12px var(--ex-signal-glow)',
                  transition: 'width 0.05s linear',
                }}/>
                {steps.map((_, i) => {
                  // Circle lights up only when the connector line actually
                  // reaches it. Each circle sits at i/3 of the bar.
                  const pos = i / 3;
                  // Smooth crossfade across a tiny window centered on pos so
                  // the dot fills exactly as the line head passes through.
                  const span = 0.012;
                  const reach = Math.max(0, Math.min(1, (lineFill - (pos - span)) / (span * 2)));
                  return (
                    <div key={i} style={{
                      position: 'absolute', top: '50%', left: `${pos * 100}%`,
                      transform: 'translate(-50%, -50%)',
                      width: 14, height: 14, borderRadius: '50%',
                      background: reach > 0.5 ? 'var(--ex-signal)' : 'var(--ex-cream)',
                      border: `2px solid ${reach > 0.05 ? `color-mix(in oklch, var(--ex-rule-strong), var(--ex-signal) ${reach * 100}%)` : 'var(--ex-rule-strong)'}`,
                      boxShadow: reach > 0.5 ? `0 0 ${8 + reach * 12}px var(--ex-signal-glow)` : 'none',
                      transition: 'background 0.05s linear, box-shadow 0.05s linear',
                    }}/>
                  );
                })}
              </div>

              <div className="ex-process-steps" style={{ display: 'grid', gridTemplateColumns: 'repeat(4, 1fr)', gap: 0 }}>
                {steps.map((s, i) => {
                  const lit = stepProgress(i);
                  return (
                    <div key={s.n} style={{
                      padding: '32px 24px 24px 24px',
                      borderRight: i < 3 ? '1px solid var(--ex-rule)' : 'none',
                      position: 'relative',
                      opacity: 0.35 + lit * 0.65,
                      transition: 'opacity 0.2s',
                    }}>
                      <div className="hero-meta" style={{
                        position: 'static', marginBottom: 14,
                        color: lit > 0.5 ? 'var(--ex-signal)' : 'var(--ex-mute)',
                        transition: 'color 0.2s',
                      }}>{s.n} · STEP</div>
                      <h3 style={{
                        fontFamily: 'var(--ex-serif)', fontWeight: 300,
                        fontSize: 40, lineHeight: 1, letterSpacing: '-0.015em',
                        margin: '0 0 14px',
                      }}>{s.name}</h3>
                      <p style={{ fontSize: 14, lineHeight: 1.55, color: 'var(--ex-ink-2)', margin: '0 0 20px', maxWidth: 280 }}>
                        {s.desc}
                      </p>
                      <div style={{
                        fontFamily: 'var(--ex-mono)', fontSize: 10,
                        letterSpacing: '0.08em', textTransform: 'uppercase',
                        color: lit > 0.5 ? 'var(--ex-signal)' : 'var(--ex-mute)',
                        transition: 'color 0.2s',
                      }}>
                        {lit > 0.95 ? '✓ ' : lit > 0.05 ? '● ' : '○ '}{s.meta}
                      </div>
                    </div>
                  );
                })}
              </div>
            </div>
          </section>
        );
      }}
    </PinnedScrub>
  );
};

const CTA = () => (
  <section className="ex-cta" style={{
    padding: '140px 40px',
    background: 'var(--ex-ink)',
    color: 'var(--ex-cream)',
    position: 'relative',
  }}>
    {/* Top edge marker — coral hairline, the section break */}
    <div style={{ position: 'absolute', top: 0, left: 0, right: 0, height: 2, background: 'var(--ex-signal)', boxShadow: '0 0 24px var(--ex-signal-glow)' }}/>
    <div className="ex-cta-grid" style={{
      maxWidth: 1320, margin: '0 auto',
      display: 'grid', gridTemplateColumns: '1.2fr 1fr', gap: 60,
      alignItems: 'center',
    }}>
      <div>
        <h2 style={{
          fontFamily: 'var(--ex-serif)', fontWeight: 300,
          fontSize: 84, lineHeight: 1, letterSpacing: '-0.03em',
          margin: 0, color: 'var(--ex-cream)',
        }}>
          Request a <em style={{ fontStyle: 'italic', color: 'var(--ex-signal)' }}>sample.</em>
        </h2>
        <p style={{
          fontSize: 18, lineHeight: 1.5, color: 'rgba(240,238,233,0.7)',
          margin: '24px 0 36px', maxWidth: 520,
        }}>
          Share your training requirements and we'll deliver a representative sample cut of the corpus within 48 hours.
        </p>
        <a href="mailto:hello@extrian.com" style={{
          display: 'inline-flex', alignItems: 'center', gap: 14,
          padding: '18px 26px',
          background: 'var(--ex-signal)', color: 'var(--ex-ink)',
          fontFamily: 'var(--ex-mono)', fontSize: 12,
          letterSpacing: '0.08em', textTransform: 'uppercase',
          textDecoration: 'none', borderRadius: 4,
          fontWeight: 500,
        }}>
          <span style={{
            display: 'inline-block', width: 8, height: 8, borderRadius: '50%',
            background: 'var(--ex-ink)',
          }}/>
          hello@extrian.com →
        </a>
      </div>
      <div style={{
        border: '1px solid rgba(240,238,233,0.15)',
        borderRadius: 6,
        padding: 28,
        background: 'rgba(240,238,233,0.04)',
        fontFamily: 'var(--ex-mono)', fontSize: 11,
        lineHeight: 1.7,
        color: 'var(--ex-cream)',
      }}>
        <div style={{
          fontSize: 9, letterSpacing: '0.1em', textTransform: 'uppercase',
          color: 'rgba(240,238,233,0.5)', marginBottom: 14,
          paddingBottom: 12, borderBottom: '1px solid rgba(240,238,233,0.12)',
        }}>SAMPLE_REQUEST.JSON</div>
        <pre style={{ margin: 0, whiteSpace: 'pre-wrap', color: 'var(--ex-cream)' }}>
{`{
  "languages": ["es-MX", "es-AR"],
  "domains": ["conversation", "emotional"],
  "annotation": ["transcript", "prosody",
                 "paralinguistic"],
  "duration_hours": 200,
  "speakers": { "min_unique": 80 },
  "delivery": "s3://your-bucket/extrian/",
  "turnaround_hours": 48
}`}
        </pre>
      </div>
    </div>
  </section>
);

const Footer = () => (
  <footer style={{
    background: 'var(--ex-ink)',
    color: 'var(--ex-cream)',
    padding: '64px 40px 32px',
  }}>
    <div className="ex-footer" style={{
      maxWidth: 1320, margin: '0 auto',
      display: 'grid', gridTemplateColumns: '2fr 1fr 1fr 1fr', gap: 40,
      paddingBottom: 40,
      borderBottom: '1px solid rgba(240,238,233,0.12)',
    }}>
      <div>
        <div style={{
          fontFamily: 'var(--ex-mono)', fontSize: 13, fontWeight: 500,
          letterSpacing: '0.02em', display: 'flex', alignItems: 'center', gap: 8, marginBottom: 16,
        }}>
          <span style={{
            display: 'inline-block', width: 14, height: 14, borderRadius: 3,
            background: 'var(--ex-cream)', position: 'relative',
          }}>
            <span style={{
              position: 'absolute', inset: 3,
              background: 'var(--ex-signal)', borderRadius: 1,
            }}/>
          </span>
          EXTRIAN
        </div>
        <p style={{
          fontFamily: 'var(--ex-serif)', fontWeight: 300,
          fontSize: 24, lineHeight: 1.25, letterSpacing: '-0.01em',
          margin: 0, color: 'var(--ex-cream)', maxWidth: 380,
        }}>
          An applied audio lab building the datasets for voice AI.
        </p>
      </div>
      {[
        { h: 'CONTACT', items: ['hello@extrian.com', 'careers@extrian.com'] },
        { h: 'SECTIONS', items: ['Annotation', 'Domains', 'Capability', 'Process'] },
        { h: 'LEGAL', items: ['Privacy', 'Licensing', '© 2026 Extrian'] },
      ].map(c => (
        <div key={c.h}>
          <div style={{
            fontFamily: 'var(--ex-mono)', fontSize: 10,
            letterSpacing: '0.1em', opacity: 0.5, marginBottom: 16,
          }}>{c.h}</div>
          {c.items.map(it => (
            <div key={it} style={{
              fontFamily: 'var(--ex-mono)', fontSize: 12,
              lineHeight: 2, color: 'rgba(240,238,233,0.85)',
            }}>{it}</div>
          ))}
        </div>
      ))}
    </div>
    <div style={{
      maxWidth: 1320, margin: '0 auto',
      paddingTop: 32,
      display: 'flex', justifyContent: 'space-between',
      fontFamily: 'var(--ex-mono)', fontSize: 10,
      letterSpacing: '0.08em', textTransform: 'uppercase',
      color: 'rgba(240,238,233,0.5)',
    }}>
      <span>○ STATUS · ALL SYSTEMS NORMAL</span>
      <span>BUILT IN SAN FRANCISCO · OPERATING WORLDWIDE</span>
      <span>v4.2.1</span>
    </div>
  </footer>
);

window.Capability = Capability;
window.Process = Process;
window.CTA = CTA;
window.Footer = Footer;
