// ML Trinity — three pillars with motion SVGs
const PillarShrink = ({ accent }) => {
  const t = window.ATTO_TOKENS;
  const c = accent === 'blue' ? t.blue : t.gold;
  return (
    <svg viewBox="0 0 400 300" width="100%" style={{ display: 'block' }}>
      <circle cx="90" cy="150" r="68" fill="none" stroke={t.ink} strokeWidth="1" opacity="0.35" />
      <circle cx="90" cy="150" r="52" fill="none" stroke={t.ink} strokeWidth="1" opacity="0.5" />
      <circle cx="90" cy="150" r="36" fill="none" stroke={t.ink} strokeWidth="1.2" opacity="0.7" />
      <text x="90" y="156" textAnchor="middle" fontFamily="var(--atto-mono)" fontSize="18" fontWeight="700" fill={t.ink}>70B</text>
      <text x="90" y="250" textAnchor="middle" fontFamily="var(--atto-mono)" fontSize="14" fill={t.fadedText} letterSpacing="0.12em">CLOUD · FP16</text>

      <line x1="170" y1="150" x2="250" y2="150" stroke={t.ink} strokeWidth="0.8" strokeDasharray="3 3" opacity="0.4" />
      {[0, 0.3, 0.6].map((o, i) => (
        <circle key={i} cy="150" r="3.5" fill={c}>
          <animate attributeName="cx" from="170" to="250" dur="2.4s" begin={`${o * -2.4}s`} repeatCount="indefinite" />
          <animate attributeName="r" values="5;3.5;1.8" dur="2.4s" begin={`${o * -2.4}s`} repeatCount="indefinite" />
          <animate attributeName="opacity" values="1;0.7;0" dur="2.4s" begin={`${o * -2.4}s`} repeatCount="indefinite" />
        </circle>
      ))}

      <circle cx="310" cy="150" r="30" fill="none" stroke={c} strokeWidth="1.8">
        <animate attributeName="r" values="26;32;26" dur="2s" repeatCount="indefinite" />
      </circle>
      <circle cx="310" cy="150" r="18" fill={c}>
        <animate attributeName="opacity" values="0.65;1;0.65" dur="2s" repeatCount="indefinite" />
      </circle>
      <text x="310" y="155" textAnchor="middle" fontFamily="var(--atto-mono)" fontSize="14" fill={t.ivory} fontWeight="700">4B</text>
      <text x="310" y="250" textAnchor="middle" fontFamily="var(--atto-mono)" fontSize="14" fill={t.fadedText} letterSpacing="0.12em">EDGE · INT4</text>

      <line x1="40" y1="272" x2="360" y2="272" stroke={t.ink} strokeWidth="0.5" opacity="0.3" />
      <text x="200" y="290" textAnchor="middle" fontFamily="var(--atto-mono)" fontSize="14" fill={t.fadedText} letterSpacing="0.12em">
        4-BIT QUANTIZATION · 100 TOK/S ON NPU
      </text>
    </svg>
  );
};

const PillarMemory = ({ accent }) => {
  const t = window.ATTO_TOKENS;
  const c = accent === 'blue' ? t.blue : t.gold;
  return (
    <svg viewBox="0 0 400 300" width="100%" style={{ display: 'block' }}>
      <text x="24" y="32" fontFamily="var(--atto-mono)" fontSize="14" fill={t.fadedText} letterSpacing="0.1em">BEFORE · STATELESS</text>
      <text x="24" y="196" fontFamily="var(--atto-mono)" fontSize="14" fill={t.fadedText} letterSpacing="0.1em">AFTER · AI HARNESS</text>

      <g>
        {Array.from({ length: 42 }).map((_, i) => (
          <rect key={i} x={24 + i * 8.5} y={50} width="5.5" height="40"
            fill={t.ink} opacity={0.25 + (i % 3) * 0.15} />
        ))}
      </g>
      <text x="382" y="76" fontFamily="var(--atto-mono)" fontSize="13" fontWeight="600" fill={t.ink} textAnchor="end">240 tok</text>

      {[0, 1, 2, 3, 4, 5].map(i => (
        <line key={i} x1={70 + i * 56} y1="100" x2={70 + i * 56} y2="180"
          stroke={c} strokeWidth="1.2" strokeDasharray="2 2" opacity="0.55">
          <animate attributeName="stroke-dashoffset" from="0" to="-8" dur="1.2s" repeatCount="indefinite" />
        </line>
      ))}
      {[0, 1, 2, 3, 4, 5].map(i => (
        <polygon key={`a${i}`} points={`${70 + i * 56 - 5},174 ${70 + i * 56},184 ${70 + i * 56 + 5},174`}
          fill={c} opacity="0.9" />
      ))}

      <g>
        {Array.from({ length: 8 }).map((_, i) => (
          <rect key={i} x={24 + i * 11} y={210} width="8" height="40" fill={c}
            opacity={0.55 + (i % 3) * 0.15}>
            <animate attributeName="opacity" values="0.4;0.95;0.4"
              dur={`${2 + (i % 3)}s`} begin={`${i * 0.15}s`} repeatCount="indefinite" />
          </rect>
        ))}
      </g>
      <text x="382" y="236" fontFamily="var(--atto-mono)" fontSize="13" fontWeight="600" fill={c} textAnchor="end">24 tok · 90% ↓</text>

      <line x1="24" y1="272" x2="376" y2="272" stroke={t.ink} strokeWidth="0.5" opacity="0.3" />
      <text x="200" y="290" textAnchor="middle" fontFamily="var(--atto-mono)" fontSize="14" fill={t.fadedText} letterSpacing="0.12em">
        CURATE · COMPRESS · RECALL
      </text>
    </svg>
  );
};

const PillarReliable = ({ accent }) => {
  const t = window.ATTO_TOKENS;
  const c = accent === 'blue' ? t.blue : t.gold;
  return (
    <svg viewBox="0 0 400 300" width="100%" style={{ display: 'block' }}>
      <path d="M 34 200 Q 120 188 200 150 Q 256 122 310 160 Q 356 184 380 138"
        fill="none" stroke={t.ink} strokeWidth="1.2" opacity="0.18" strokeDasharray="3 3" />
      <path d="M 34 200 Q 120 188 200 150 Q 256 122 300 200 Q 344 250 380 238"
        fill="none" stroke={t.ink} strokeWidth="1.2" opacity="0.18" strokeDasharray="3 3" />

      <path d="M 34 200 Q 120 188 200 150 Q 256 122 310 115 Q 352 110 380 106"
        fill="none" stroke={c} strokeWidth="3">
        <animate attributeName="stroke-dasharray" from="0 700" to="700 0" dur="3s" repeatCount="indefinite" />
      </path>

      <circle cx="256" cy="122" r="7" fill="none" stroke={c} strokeWidth="1.6">
        <animate attributeName="r" values="6;14;6" dur="2s" repeatCount="indefinite" />
        <animate attributeName="opacity" values="1;0.2;1" dur="2s" repeatCount="indefinite" />
      </circle>
      <circle cx="256" cy="122" r="4" fill={c} />

      <circle r="6" fill={c}>
        <animateMotion dur="4s" repeatCount="indefinite" path="M 34 200 Q 120 188 200 150 Q 256 122 310 115 Q 352 110 380 106" />
      </circle>

      <g>
        <circle cx="310" cy="160" r="7" fill="none" stroke={t.ink} strokeWidth="1.3" opacity="0.45" />
        <line x1="305" y1="155" x2="315" y2="165" stroke={t.ink} strokeWidth="1.2" opacity="0.55" />
        <line x1="315" y1="155" x2="305" y2="165" stroke={t.ink} strokeWidth="1.2" opacity="0.55" />
      </g>
      <g>
        <circle cx="300" cy="200" r="7" fill="none" stroke={t.ink} strokeWidth="1.3" opacity="0.45" />
        <line x1="295" y1="195" x2="305" y2="205" stroke={t.ink} strokeWidth="1.2" opacity="0.55" />
        <line x1="305" y1="195" x2="295" y2="205" stroke={t.ink} strokeWidth="1.2" opacity="0.55" />
      </g>

      <line x1="34" y1="40" x2="34" y2="260" stroke={t.ink} strokeWidth="0.5" opacity="0.3" />
      <line x1="34" y1="260" x2="380" y2="260" stroke={t.ink} strokeWidth="0.5" opacity="0.3" />
      <text x="44" y="56" fontFamily="var(--atto-mono)" fontSize="13" fill={t.fadedText} letterSpacing="0.12em">LATENT TRAJECTORY</text>
      <text x="200" y="290" textAnchor="middle" fontFamily="var(--atto-mono)" fontSize="14" fill={t.fadedText} letterSpacing="0.12em">
        HD-NDE · +14% DETECTION
      </text>
    </svg>
  );
};

const Trinity = ({ accent }) => {
  const t = window.ATTO_TOKENS;
  const pillars = [
    { num: 'I', name: 'Edge-Cheap',
      kicker: 'Small Language Models',
      body: 'Under 4B parameters. 4-bit quantization. Production-grade inference on the NPU in your pocket. Near-zero API cost, 100% private by default.',
      comp: PillarShrink },
    { num: 'II', name: 'Memory-Smart',
      kicker: 'AI Harness',
      body: 'Standard LLMs are stateless. Our AI Harness curates and compresses context across sessions — shrinking windows by 60–90%, making every cloud fallback maximally efficient.',
      comp: PillarMemory },
    { num: 'III', name: 'Reliable',
      kicker: 'HD-NDE Hallucination Guard',
      body: "Hallucination Detection via Neural Differential Equations. We monitor the model's latent trajectory in real-time — 14% better than classifiers.",
      comp: PillarReliable },
  ];

  return (
    <section id="research" className="atto-trinity" style={{
      padding: '160px 48px 140px',
      borderTop: `1px solid ${t.rule}`,
      background: t.ivorySoft,
      fontFamily: 'var(--atto-sans)',
    }}>
      <div className="atto-section-meta" style={{ display: 'flex', justifyContent: 'space-between',
        fontFamily: 'var(--atto-mono)', fontSize: 13, color: t.fadedText,
        letterSpacing: '0.08em', marginBottom: 48 }}>
        <span>/ 03 — THE ML TRINITY</span>
        <span>THREE PILLARS · ONE STACK</span>
      </div>

      <h2 style={{
        fontSize: 'clamp(40px, 4.8vw, 68px)',
        lineHeight: 1.02, letterSpacing: '-0.03em', fontWeight: 600,
        color: t.ink, margin: '0 0 24px 0', maxWidth: 980, textWrap: 'balance',
      }}>
        The architecture of{' '}
        <span style={{ fontFamily: 'var(--atto-serif)', fontStyle: 'italic', fontWeight: 400 }}>
          efficient
        </span>{' '}intelligence.
      </h2>
      <p style={{ fontSize: 20, color: t.mutedText, maxWidth: 680, lineHeight: 1.55,
        margin: '0 0 80px 0', textWrap: 'pretty' }}>
        India cannot win a brute-force hardware race. It must win the efficiency race.
        AttoFlow's stack rests on three pillars — each a deliberate refusal of waste.
      </p>

      <div className="atto-trinity-grid" style={{ display: 'grid', gridTemplateColumns: 'repeat(3, 1fr)', gap: 32 }}>
        {pillars.map((p, i) => (
          <div key={p.num} style={{
            background: t.ivory,
            border: `1px solid ${t.rule}`,
            borderRadius: 20,
            padding: 32,
            display: 'flex', flexDirection: 'column',
            transition: 'transform 300ms, box-shadow 300ms',
          }}
          onMouseEnter={e => { e.currentTarget.style.transform = 'translateY(-4px)';
            e.currentTarget.style.boxShadow = '0 24px 48px -24px rgba(10,10,10,0.18)'; }}
          onMouseLeave={e => { e.currentTarget.style.transform = 'translateY(0)';
            e.currentTarget.style.boxShadow = 'none'; }}>
            <div style={{ marginBottom: 20 }}>
              <p.comp accent={accent} />
            </div>
            <div style={{ display: 'flex', alignItems: 'baseline', gap: 12, marginBottom: 10 }}>
              <span style={{ fontFamily: 'var(--atto-serif)', fontSize: 24,
                fontStyle: 'italic', color: (accent === 'blue' ? t.blue : t.gold) }}>{p.num}</span>
              <span style={{ fontFamily: 'var(--atto-mono)', fontSize: 12,
                color: t.fadedText, letterSpacing: '0.12em' }}>{p.kicker.toUpperCase()}</span>
            </div>
            <h3 style={{ fontSize: 28, fontWeight: 600, letterSpacing: '-0.025em',
              color: t.ink, margin: '0 0 12px' }}>{p.name}</h3>
            <p style={{ fontSize: 18, lineHeight: 1.55, color: t.mutedText, margin: 0,
              textWrap: 'pretty' }}>{p.body}</p>
          </div>
        ))}
      </div>
    </section>
  );
};

window.Trinity = Trinity;
