const Tech = () => {
  const tc = L('tech');
  const techIcons = ['meta','qr','lock','bolt'];
  const techItems = tc.items.map((it, i) => ({ ...it, icon: techIcons[i] }));
  return (
    <section id="tech" className="section-pad" style={{ position:'relative', overflow:'hidden' }}>
      <div style={{
        position:'absolute', inset:0,
        background:'radial-gradient(800px 600px at 80% 50%, rgba(0,228,122,0.06), transparent 60%)',
        pointerEvents:'none'
      }}/>
      <div className="container" style={{ position:'relative' }}>
        <div style={{ display:'grid', gridTemplateColumns:'1fr 1.2fr', gap:80, alignItems:'center' }} className="tech-grid">

          <div>
            <div className="eyebrow" style={{ marginBottom:14 }}>{tc.eyebrow}</div>
            <h2 className="display" style={{ fontSize:'clamp(36px, 4.4vw, 58px)', margin:0 }}>
              {tc.h2a}<br/>
              <span className="serif" style={{ color:'var(--green)' }}>{tc.h2b}</span>
            </h2>
            <p style={{ fontSize:17, color:'var(--ink-2)', marginTop:22, lineHeight:1.6 }}>
              {parseInline(tc.sub).map((seg, i) => {
                if (typeof seg === 'string' && seg.startsWith('|') && seg.endsWith('|')) {
                  return <strong key={i} style={{ color:'var(--ink)' }}>{seg.slice(1,-1)}</strong>;
                }
                return <span key={i}>{seg}</span>;
              })}
            </p>

            <div style={{ display:'flex', flexDirection:'column', gap:14, marginTop:32 }}>
              {techItems.map((it, i) => (
                <div key={i} style={{
                  display:'flex', alignItems:'center', gap:14,
                  padding:'14px 16px', borderRadius:12,
                  background:'rgba(255,255,255,0.02)', border:'1px solid var(--line)'
                }}>
                  <div style={{
                    width:34, height:34, borderRadius:9,
                    background:'var(--green-soft)', color:'var(--green)',
                    display:'grid', placeItems:'center'
                  }}>
                    <Icon name={it.icon} size={16}/>
                  </div>
                  <div>
                    <div style={{ fontSize:15, fontWeight:500 }}>{it.label}</div>
                    <div style={{ fontSize:12.5, color:'var(--ink-3)', marginTop:2 }}>{it.sub}</div>
                  </div>
                </div>
              ))}
            </div>
          </div>

          <TechDiagram/>
        </div>
      </div>
      <style>{`
        @media (max-width: 980px) {
          .tech-grid { grid-template-columns: 1fr !important; gap:48px !important; }
        }
      `}</style>
    </section>
  );
};

const TechDiagram = () => {
  // central animated diagram showing data flow
  return (
    <div style={{
      position:'relative', width:'100%', aspectRatio:'1/1', maxWidth:520, margin:'0 auto',
      borderRadius:24, overflow:'hidden',
      border:'1px solid var(--line)',
      background:
        'radial-gradient(circle at 50% 50%, rgba(0,228,122,0.08), transparent 60%),'+
        'linear-gradient(180deg, rgba(255,255,255,0.02), rgba(0,0,0,0))',
      padding:28
    }}>
      <div className="grid-bg" style={{ opacity:0.5 }}/>

      <svg viewBox="0 0 460 460" style={{ width:'100%', height:'100%', position:'relative', zIndex:1 }}>
        <defs>
          <linearGradient id="flow" x1="0" x2="1">
            <stop offset="0" stopColor="#00E47A" stopOpacity="0"/>
            <stop offset="0.5" stopColor="#00E47A" stopOpacity="1"/>
            <stop offset="1" stopColor="#00E47A" stopOpacity="0"/>
          </linearGradient>
          <radialGradient id="core">
            <stop offset="0%" stopColor="#00ff88"/>
            <stop offset="100%" stopColor="#00a55a"/>
          </radialGradient>
        </defs>

        {/* connection lines */}
        {[
          [80, 90, 230, 230],   // wpp
          [380, 90, 230, 230],  // insta
          [80, 370, 230, 230],  // crm
          [380, 370, 230, 230], // calendar
        ].map(([x1,y1,x2,y2], i) => (
          <g key={i}>
            <line x1={x1} y1={y1} x2={x2} y2={y2} stroke="rgba(255,255,255,0.08)" strokeWidth="1" strokeDasharray="3 3"/>
            <circle r="3" fill="#00E47A">
              <animateMotion dur="2.5s" repeatCount="indefinite" begin={`${i*0.5}s`}
                path={`M${x1},${y1} L${x2},${y2}`}/>
            </circle>
          </g>
        ))}

        {/* core */}
        <circle cx="230" cy="230" r="68" fill="rgba(0,228,122,0.05)" stroke="rgba(0,228,122,0.3)" strokeWidth="1"/>
        <circle cx="230" cy="230" r="44" fill="url(#core)" filter="drop-shadow(0 0 30px rgba(0,228,122,0.6))"/>
        <g transform="translate(230,230)">
          <path d="M-2 -16 -14 4 -2 4 2 16 14 -4 2 -4 z" fill="#001509"/>
        </g>
        <text x="230" y="320" textAnchor="middle" fill="var(--ink-2)" style={{ fontSize:13, fontFamily:"'Geist Mono',monospace" }}>
          ZapFlux Core
        </text>
        <text x="230" y="338" textAnchor="middle" fill="var(--ink-4)" style={{ fontSize:10.5, fontFamily:"'Geist Mono',monospace" }}>
          {L('tech').coreSub}
        </text>
      </svg>

      {/* corner nodes */}
      <Node style={{ top:30, left:30 }} icon="whatsapp" label="WhatsApp" sub="Cloud API"/>
      <Node style={{ top:30, right:30 }} icon="instagram" label="Instagram" sub="Direct"/>
      <Node style={{ bottom:30, left:30 }} icon="stack" label="CRM" sub="Webhooks"/>
      <Node style={{ bottom:30, right:30 }} icon="users" label={L('tech').nodeTeam} sub={L('tech').nodeHandoff}/>

      <div style={{
        position:'absolute', top:14, right:14,
        padding:'5px 9px', borderRadius:7,
        background:'rgba(0,228,122,0.1)', border:'1px solid rgba(0,228,122,0.25)',
        fontSize:11, color:'var(--green)', fontFamily:"'Geist Mono',monospace",
        display:'flex', alignItems:'center', gap:6
      }}>
        <span className="dot live" style={{ width:5, height:5, borderRadius:'50%', background:'var(--green)' }}/>
        live
      </div>
    </div>
  );
};

const Node = ({ style, icon, label, sub }) => (
  <div style={{
    position:'absolute', ...style,
    width:96, padding:'10px 8px', borderRadius:12,
    background:'rgba(8,12,10,0.85)', backdropFilter:'blur(10px)',
    border:'1px solid rgba(255,255,255,0.08)',
    display:'flex', flexDirection:'column', alignItems:'center', gap:4
  }}>
    <div style={{
      width:30, height:30, borderRadius:8,
      background:'rgba(0,228,122,0.12)', color:'var(--green)',
      display:'grid', placeItems:'center'
    }}>
      <Icon name={icon} size={15}/>
    </div>
    <div style={{ fontSize:11.5, fontWeight:500 }}>{label}</div>
    <div style={{ fontSize:10, color:'var(--ink-4)', fontFamily:"'Geist Mono',monospace" }}>{sub}</div>
  </div>
);

window.Tech = Tech;
