// INTERACCIÓN — 14 sigilos (palo ámbar). LA ESCUCHA ya está en sigils.jsx.
// 1 CONTACTO · 2 SEÑAL · 3 RETROALIMENTACIÓN · 4 COOPERACIÓN · 5 CONFLICTO
// 6 NEGOCIACIÓN · 7 CONTAGIO · 8 COMPETENCIA · 9 INTERCAMBIO · 10 RED
// 11 EL MENSAJERO · 12 EL CATALIZADOR · 13 LA ESCUCHA [✓] · 14 EL VÍNCULO

const I_SVG = ({ children, size = 300 }) => (
  <svg viewBox={`0 0 ${size} ${size}`} width="100%" height="100%">{children}</svg>
);
const _ir = (s) => {
  let a = s >>> 0;
  return () => { a = (a + 0x6D2B79F5) >>> 0; let t = a; t = Math.imul(t ^ (t >>> 15), t | 1); t ^= t + Math.imul(t ^ (t >>> 7), t | 61); return ((t ^ (t >>> 14)) >>> 0) / 4294967296; };
};
const ArrI = ({ id, color }) => (
  <marker id={id} markerWidth="6" markerHeight="6" refX="5" refY="3" orient="auto"><path d="M 0 0 L 6 3 L 0 6 Z" fill={color}/></marker>
);

// 1 · CONTACTO — two circles just touching
const Sigil_CONTACTO = ({ color, size = 300 }) => {
  const cx = size/2, cy = size/2;
  return <I_SVG size={size}>
    <circle cx={cx-50} cy={cy} r={50} fill="none" stroke={color} strokeWidth={1.4}/>
    <circle cx={cx+50} cy={cy} r={50} fill="none" stroke={color} strokeWidth={1.4}/>
    <circle cx={cx-50} cy={cy} r={42} fill={color} fillOpacity={0.06}/>
    <circle cx={cx+50} cy={cy} r={42} fill={color} fillOpacity={0.06}/>
    {/* contact point highlight */}
    <circle cx={cx} cy={cy} r={6} fill={color}/>
    <circle cx={cx} cy={cy} r={14} fill="none" stroke={color} strokeWidth={0.6} strokeDasharray="2 2"/>
    <text x={cx-50} y={cy+4} fontSize={11} fill={color} textAnchor="middle" fontFamily="serif" fontStyle="italic" opacity={0.7}>A</text>
    <text x={cx+50} y={cy+4} fontSize={11} fill={color} textAnchor="middle" fontFamily="serif" fontStyle="italic" opacity={0.7}>B</text>
    <text x={cx} y={cy-22} fontSize={9} fill={color} textAnchor="middle" fontFamily="serif" fontStyle="italic" opacity={0.9}>×</text>
  </I_SVG>;
};

// 2 · SEÑAL — pulse traveling between sender and receiver
const Sigil_SENAL = ({ color, size = 300 }) => {
  const cx = size/2, cy = size/2;
  return <I_SVG size={size}>
    <defs><ArrI id="aci2" color={color}/></defs>
    {/* sender */}
    <rect x={30} y={cy-18} width={36} height={36} fill="none" stroke={color} strokeWidth={1.2}/>
    {/* receiver */}
    <rect x={size-66} y={cy-18} width={36} height={36} fill="none" stroke={color} strokeWidth={1.2}/>
    {/* wavefronts traveling left → right */}
    {[80, 120, 165, 210].map((x, i) => (
      <g key={i} transform={`translate(${x} ${cy})`}>
        <path d="M -8 -16 Q 8 0 -8 16" fill="none" stroke={color} strokeWidth={1.2-i*0.18} opacity={1-i*0.18}/>
      </g>
    ))}
    {/* direction arrow */}
    <line x1={66} y1={cy} x2={size-66} y2={cy} stroke={color} strokeWidth={0.5} strokeDasharray="1 3" markerEnd="url(#aci2)" opacity={0.55}/>
    <text x={48} y={cy+3} fontSize={9} fill={color} textAnchor="middle" fontFamily="monospace" letterSpacing="2" opacity={0.7}>TX</text>
    <text x={size-48} y={cy+3} fontSize={9} fill={color} textAnchor="middle" fontFamily="monospace" letterSpacing="2" opacity={0.7}>RX</text>
  </I_SVG>;
};

// 3 · RETROALIMENTACIÓN — closed loop with +/- sign
const Sigil_RETROALIMENTACION = ({ color, size = 300 }) => {
  const cx = size/2, cy = size/2;
  return <I_SVG size={size}>
    <defs><ArrI id="aci3" color={color}/></defs>
    {/* box: system */}
    <rect x={cx-40} y={cy-22} width={80} height={44} fill="none" stroke={color} strokeWidth={1.2}/>
    <text x={cx} y={cy+5} fontSize={10} fill={color} textAnchor="middle" fontFamily="serif" fontStyle="italic">sistema</text>
    {/* in arrow */}
    <line x1={cx-100} y1={cy} x2={cx-44} y2={cy} stroke={color} strokeWidth={1.2} markerEnd="url(#aci3)"/>
    {/* out arrow */}
    <line x1={cx+40} y1={cy} x2={cx+100} y2={cy} stroke={color} strokeWidth={1.2} markerEnd="url(#aci3)"/>
    {/* feedback loop */}
    <path d={`M ${cx+90} ${cy} Q ${cx+115} ${cy} ${cx+115} ${cy-50} Q ${cx+115} ${cy-78} ${cx-90} ${cy-78} Q ${cx-115} ${cy-78} ${cx-115} ${cy-25} Q ${cx-115} ${cy} ${cx-94} ${cy}`} fill="none" stroke={color} strokeWidth={1} markerEnd="url(#aci3)"/>
    {/* +/- label on loop */}
    <circle cx={cx} cy={cy-78} r={11} fill="#000" stroke={color} strokeWidth={1}/>
    <text x={cx} y={cy-74} fontSize={14} fill={color} textAnchor="middle" fontFamily="serif" fontWeight={700}>±</text>
  </I_SVG>;
};

// 4 · COOPERACIÓN — two arrows joining into one larger
const Sigil_COOPERACION = ({ color, size = 300 }) => {
  const cx = size/2;
  return <I_SVG size={size}>
    <defs><ArrI id="aci4" color={color}/></defs>
    {/* two input arrows */}
    <line x1={40} y1={80} x2={cx-20} y2={size/2-12} stroke={color} strokeWidth={1.4}/>
    <line x1={40} y1={size-80} x2={cx-20} y2={size/2+12} stroke={color} strokeWidth={1.4}/>
    <circle cx={40} cy={80} r={5} fill={color}/>
    <circle cx={40} cy={size-80} r={5} fill={color}/>
    {/* meeting node */}
    <circle cx={cx-12} cy={size/2} r={10} fill="none" stroke={color} strokeWidth={1}/>
    <text x={cx-12} y={size/2+4} fontSize={11} fill={color} textAnchor="middle" fontFamily="serif" fontWeight={700}>+</text>
    {/* output — bigger arrow */}
    <line x1={cx} y1={size/2} x2={size-30} y2={size/2} stroke={color} strokeWidth={2.5} markerEnd="url(#aci4)"/>
    <text x={size-30} y={size/2-12} fontSize={9} fill={color} textAnchor="end" fontFamily="serif" fontStyle="italic">2+2 &gt; 4</text>
  </I_SVG>;
};

// 5 · CONFLICTO — two opposing arrows
const Sigil_CONFLICTO = ({ color, size = 300 }) => {
  const cx = size/2, cy = size/2;
  return <I_SVG size={size}>
    <defs><ArrI id="aci5" color={color}/></defs>
    {/* left arrow → */}
    <line x1={40} y1={cy} x2={cx-20} y2={cy} stroke={color} strokeWidth={2.2} markerEnd="url(#aci5)"/>
    {/* right arrow ← */}
    <line x1={size-40} y1={cy} x2={cx+20} y2={cy} stroke={color} strokeWidth={2.2} markerEnd="url(#aci5)"/>
    {/* collision burst */}
    <g transform={`translate(${cx} ${cy})`}>
      {Array.from({length:12}).map((_,i)=>{
        const a = (i/12)*Math.PI*2;
        return <line key={i} x1={Math.cos(a)*8} y1={Math.sin(a)*8} x2={Math.cos(a)*22} y2={Math.sin(a)*22} stroke={color} strokeWidth={0.6}/>;
      })}
      <circle r={6} fill={color}/>
    </g>
    <text x={cx-65} y={cy-22} fontSize={10} fill={color} textAnchor="middle" fontFamily="serif" fontStyle="italic">A</text>
    <text x={cx+65} y={cy-22} fontSize={10} fill={color} textAnchor="middle" fontFamily="serif" fontStyle="italic">B</text>
  </I_SVG>;
};

// 6 · NEGOCIACIÓN — both arrows curve to meet at compromise
const Sigil_NEGOCIACION = ({ color, size = 300 }) => {
  const cx = size/2, cy = size/2;
  return <I_SVG size={size}>
    <defs><ArrI id="aci6" color={color}/></defs>
    {/* axes */}
    <line x1={30} y1={size-50} x2={size-20} y2={size-50} stroke={color} strokeWidth={0.4} opacity={0.5}/>
    <line x1={40} y1={30} x2={40} y2={size-50} stroke={color} strokeWidth={0.4} opacity={0.5}/>
    {/* A's curve */}
    <path d={`M 40 60 Q 60 100 ${cx} ${cy}`} fill="none" stroke={color} strokeWidth={1.3}/>
    {/* B's curve */}
    <path d={`M ${size-30} ${size-80} Q ${size-80} ${size-100} ${cx} ${cy}`} fill="none" stroke={color} strokeWidth={1.3}/>
    {/* meeting point */}
    <circle cx={cx} cy={cy} r={7} fill={color}/>
    <circle cx={cx} cy={cy} r={15} fill="none" stroke={color} strokeWidth={0.6} strokeDasharray="2 2"/>
    <text x={cx} y={cy-22} fontSize={9} fill={color} textAnchor="middle" fontFamily="serif" fontStyle="italic">acuerdo</text>
    <text x={32} y={50} fontSize={9} fill={color} textAnchor="middle" fontFamily="serif" fontStyle="italic" opacity={0.85}>A*</text>
    <text x={size-18} y={size-78} fontSize={9} fill={color} textAnchor="middle" fontFamily="serif" fontStyle="italic" opacity={0.85}>B*</text>
  </I_SVG>;
};

// 7 · CONTAGIO — spreading wave through network
const Sigil_CONTAGIO = ({ color, size = 300 }) => {
  const cx = size/2, cy = size/2;
  const r = _ir(7);
  // generate nodes
  const N = 22;
  const nodes = Array.from({length:N}).map((_,i)=>({
    x: 30 + r()*(size-60),
    y: 30 + r()*(size-60),
  }));
  const center = nodes[0];
  const dists = nodes.map(n => Math.hypot(n.x-center.x, n.y-center.y));
  return <I_SVG size={size}>
    {/* edges to nearest neighbors */}
    {nodes.map((n,i) => nodes.slice(i+1).map((m,j) => {
      const d = Math.hypot(n.x-m.x, n.y-m.y);
      if (d < 70) return <line key={`${i}-${j}`} x1={n.x} y1={n.y} x2={m.x} y2={m.y} stroke={color} strokeWidth={0.3} opacity={0.4}/>;
      return null;
    }))}
    {/* nodes — filled by distance (infection front) */}
    {nodes.map((n,i) => {
      const infected = dists[i] < 80;
      return <circle key={i} cx={n.x} cy={n.y} r={infected ? 5 : 4} fill={infected ? color : '#000'} stroke={color} strokeWidth={1}/>;
    })}
    {/* origin marker */}
    <circle cx={center.x} cy={center.y} r={12} fill="none" stroke={color} strokeWidth={1.2}/>
    {/* expanding ring */}
    <circle cx={center.x} cy={center.y} r={80} fill="none" stroke={color} strokeWidth={0.8} strokeDasharray="3 3"/>
  </I_SVG>;
};

// 8 · COMPETENCIA — Lotka-Volterra-like predator/prey oscillation
const Sigil_COMPETENCIA = ({ color, size = 300 }) => {
  const cy = size/2;
  return <I_SVG size={size}>
    {/* axes */}
    <line x1={30} y1={size-40} x2={size-20} y2={size-40} stroke={color} strokeWidth={0.4} opacity={0.5}/>
    <line x1={40} y1={30} x2={40} y2={size-40} stroke={color} strokeWidth={0.4} opacity={0.5}/>
    {/* prey */}
    <path d={Array.from({length:120}).map((_,i)=>{
      const x = 40 + (i/119)*(size-60);
      const y = cy + Math.sin(i*0.13)*40;
      return `${i===0?'M':'L'} ${x.toFixed(1)} ${y.toFixed(1)}`;
    }).join(' ')} fill="none" stroke={color} strokeWidth={1.5}/>
    {/* predator — phase-shifted */}
    <path d={Array.from({length:120}).map((_,i)=>{
      const x = 40 + (i/119)*(size-60);
      const y = cy + Math.sin(i*0.13 - 1.2)*30;
      return `${i===0?'M':'L'} ${x.toFixed(1)} ${y.toFixed(1)}`;
    }).join(' ')} fill="none" stroke={color} strokeWidth={1.5} strokeDasharray="3 3" opacity={0.8}/>
    <text x={size-30} y={cy-50} fontSize={9} fill={color} textAnchor="end" fontFamily="serif" fontStyle="italic" opacity={0.85}>presa</text>
    <text x={size-30} y={cy-36} fontSize={9} fill={color} textAnchor="end" fontFamily="serif" fontStyle="italic" opacity={0.7}>—— depredador</text>
  </I_SVG>;
};

// 9 · INTERCAMBIO — reciprocal swap of objects
const Sigil_INTERCAMBIO = ({ color, size = 300 }) => {
  const cx = size/2, cy = size/2;
  return <I_SVG size={size}>
    <defs><ArrI id="aci9" color={color}/></defs>
    {/* two figures */}
    <circle cx={cx-65} cy={cy} r={22} fill="none" stroke={color} strokeWidth={1.3}/>
    <circle cx={cx+65} cy={cy} r={22} fill="none" stroke={color} strokeWidth={1.3}/>
    <text x={cx-65} y={cy+5} fontSize={11} fill={color} textAnchor="middle" fontFamily="serif" fontStyle="italic">A</text>
    <text x={cx+65} y={cy+5} fontSize={11} fill={color} textAnchor="middle" fontFamily="serif" fontStyle="italic">B</text>
    {/* upper arrow A → B with object */}
    <path d={`M ${cx-45} ${cy-12} Q ${cx} ${cy-50} ${cx+45} ${cy-12}`} fill="none" stroke={color} strokeWidth={1.2} markerEnd="url(#aci9)"/>
    <rect x={cx-6} y={cy-44} width={12} height={8} fill={color}/>
    {/* lower arrow B → A with different object */}
    <path d={`M ${cx+45} ${cy+12} Q ${cx} ${cy+50} ${cx-45} ${cy+12}`} fill="none" stroke={color} strokeWidth={1.2} markerEnd="url(#aci9)"/>
    <circle cx={cx} cy={cy+44} r={5} fill={color}/>
  </I_SVG>;
};

// 10 · RED — multi-cluster graph
const Sigil_RED = ({ color, size = 300 }) => {
  const r = _ir(10);
  // 3 clusters
  const clusters = [
    { cx: 80,  cy: 80,  n: 6 },
    { cx: 220, cy: 90,  n: 5 },
    { cx: 150, cy: 220, n: 6 },
  ];
  const nodes = [];
  clusters.forEach(cl => {
    for (let i = 0; i < cl.n; i++) {
      const a = (i/cl.n)*Math.PI*2 + r();
      nodes.push({
        x: cl.cx + Math.cos(a)*(15+r()*15),
        y: cl.cy + Math.sin(a)*(15+r()*15),
        cluster: cl,
      });
    }
  });
  return <I_SVG size={size}>
    {/* intra-cluster edges */}
    {nodes.map((n,i) => nodes.slice(i+1).map((m,j) => {
      if (n.cluster === m.cluster && Math.hypot(n.x-m.x, n.y-m.y) < 40) {
        return <line key={`${i}-${j}`} x1={n.x} y1={n.y} x2={m.x} y2={m.y} stroke={color} strokeWidth={0.5} opacity={0.6}/>;
      }
      return null;
    }))}
    {/* inter-cluster bridges (few) */}
    <line x1={clusters[0].cx+20} y1={clusters[0].cy+10} x2={clusters[1].cx-20} y2={clusters[1].cy} stroke={color} strokeWidth={0.8} opacity={0.7}/>
    <line x1={clusters[1].cx-10} y1={clusters[1].cy+20} x2={clusters[2].cx+10} y2={clusters[2].cy-20} stroke={color} strokeWidth={0.8} opacity={0.7}/>
    <line x1={clusters[0].cx+5} y1={clusters[0].cy+25} x2={clusters[2].cx-30} y2={clusters[2].cy-15} stroke={color} strokeWidth={0.8} opacity={0.7}/>
    {/* nodes */}
    {nodes.map((n,i)=>(<circle key={i} cx={n.x} cy={n.y} r={3.5} fill={color}/>))}
  </I_SVG>;
};

// 11 · EL MENSAJERO (Sota) — figure crossing a boundary
const Sigil_MENSAJERO = ({ color, size = 300 }) => {
  const cx = size/2, cy = size/2;
  return <I_SVG size={size}>
    {/* the boundary */}
    <line x1={cx} y1={30} x2={cx} y2={size-30} stroke={color} strokeWidth={1.4} strokeDasharray="4 3"/>
    {/* two regions */}
    <rect x={30} y={50} width={cx-30-10} height={size-100} fill={color} fillOpacity={0.04} stroke={color} strokeWidth={0.3}/>
    <rect x={cx+10} y={50} width={size-cx-40} height={size-100} fill={color} fillOpacity={0.08} stroke={color} strokeWidth={0.3}/>
    <text x={cx-25} y={70} fontSize={10} fill={color} textAnchor="middle" fontFamily="serif" fontStyle="italic" opacity={0.6}>aquí</text>
    <text x={cx+25} y={70} fontSize={10} fill={color} textAnchor="middle" fontFamily="serif" fontStyle="italic" opacity={0.6}>allá</text>
    {/* figure on the boundary, walking right */}
    <g transform={`translate(${cx} ${cy})`}>
      {/* head */}
      <circle cx={0} cy={-32} r={10} fill="none" stroke={color} strokeWidth={1.4}/>
      {/* body */}
      <line x1={0} y1={-22} x2={0} y2={14} stroke={color} strokeWidth={1.4}/>
      {/* arms — one carrying message */}
      <line x1={0} y1={-10} x2={-14} y2={2} stroke={color} strokeWidth={1.2}/>
      <line x1={0} y1={-10} x2={18} y2={-2} stroke={color} strokeWidth={1.2}/>
      {/* message scroll */}
      <rect x={14} y={-8} width={10} height={12} fill={color}/>
      {/* legs walking */}
      <line x1={0} y1={14} x2={-10} y2={40} stroke={color} strokeWidth={1.2}/>
      <line x1={0} y1={14} x2={14} y2={36} stroke={color} strokeWidth={1.2}/>
    </g>
    {/* footsteps left behind */}
    <ellipse cx={cx-30} cy={cy+44} rx={4} ry={2} fill={color} opacity={0.5}/>
    <ellipse cx={cx-50} cy={cy+50} rx={4} ry={2} fill={color} opacity={0.3}/>
  </I_SVG>;
};

// 12 · EL CATALIZADOR (Caballero) — lowering activation barrier (different angle than CATÁLISIS in EMERGENCIA)
const Sigil_CATALIZADOR = ({ color, size = 300 }) => {
  const cx = size/2, cy = size/2;
  return <I_SVG size={size}>
    <defs><ArrI id="aci12" color={color}/></defs>
    {/* two reactants approaching */}
    <circle cx={50} cy={cy} r={14} fill="none" stroke={color} strokeWidth={1.2}/>
    <text x={50} y={cy+4} fontSize={10} fill={color} textAnchor="middle" fontFamily="serif" fontStyle="italic">A</text>
    <circle cx={size-50} cy={cy} r={14} fill="none" stroke={color} strokeWidth={1.2}/>
    <text x={size-50} y={cy+4} fontSize={10} fill={color} textAnchor="middle" fontFamily="serif" fontStyle="italic">B</text>
    {/* the catalyst — Y-shape gripping both */}
    <path d={`M ${cx} ${cy-50} L ${cx} ${cy-20} M ${cx} ${cy-20} L ${cx-18} ${cy-8} M ${cx} ${cy-20} L ${cx+18} ${cy-8}`} fill="none" stroke={color} strokeWidth={1.5}/>
    <circle cx={cx-18} cy={cy-8} r={4} fill={color}/>
    <circle cx={cx+18} cy={cy-8} r={4} fill={color}/>
    {/* attraction arrows */}
    <path d={`M 64 ${cy} Q ${cx-30} ${cy+10} ${cx-20} ${cy-6}`} fill="none" stroke={color} strokeWidth={0.6} markerEnd="url(#aci12)" strokeDasharray="2 2"/>
    <path d={`M ${size-64} ${cy} Q ${cx+30} ${cy+10} ${cx+20} ${cy-6}`} fill="none" stroke={color} strokeWidth={0.6} markerEnd="url(#aci12)" strokeDasharray="2 2"/>
    {/* result fast */}
    <text x={cx} y={cy+50} fontSize={10} fill={color} textAnchor="middle" fontFamily="serif" fontStyle="italic">A·B</text>
    <text x={cx} y={cy+70} fontSize={8} fill={color} textAnchor="middle" fontFamily="monospace" letterSpacing="2" opacity={0.7}>FASTER, INTACT</text>
  </I_SVG>;
};

// 14 · EL VÍNCULO (Rey) — interlocked chain links / knot
const Sigil_VINCULO = ({ color, size = 300 }) => {
  const cx = size/2, cy = size/2;
  return <I_SVG size={size}>
    {/* two interlocking torus rings */}
    <g>
      <ellipse cx={cx-30} cy={cy} rx={50} ry={28} fill="none" stroke={color} strokeWidth={2.4}/>
      <ellipse cx={cx-30} cy={cy} rx={36} ry={16} fill="none" stroke={color} strokeWidth={1}/>
    </g>
    <g>
      {/* the right ring drawn over the left at top, under at bottom — illusion */}
      <path d={`M ${cx+80} ${cy} A 50 28 0 0 1 ${cx-20} ${cy}`} fill="none" stroke={color} strokeWidth={2.4}/>
      <path d={`M ${cx-20} ${cy} A 50 28 0 0 1 ${cx+80} ${cy}`} fill="none" stroke={color} strokeWidth={2.4}/>
      <ellipse cx={cx+30} cy={cy} rx={36} ry={16} fill="none" stroke={color} strokeWidth={1}/>
    </g>
    {/* tension marks */}
    {[[cx-20, cy], [cx-3, cy]].map(([x,y],i)=>(
      <g key={i}>
        <line x1={x-3} y1={y-7} x2={x+3} y2={y-7} stroke={color} strokeWidth={0.5}/>
        <line x1={x-3} y1={y+7} x2={x+3} y2={y+7} stroke={color} strokeWidth={0.5}/>
      </g>
    ))}
  </I_SVG>;
};

Object.assign(PILOT_SIGILS, {
  'CONTACTO':          Sigil_CONTACTO,
  'SEÑAL':             Sigil_SENAL,
  'RETROALIMENTACIÓN': Sigil_RETROALIMENTACION,
  'COOPERACIÓN':       Sigil_COOPERACION,
  'CONFLICTO':         Sigil_CONFLICTO,
  'NEGOCIACIÓN':       Sigil_NEGOCIACION,
  'CONTAGIO':          Sigil_CONTAGIO,
  'COMPETENCIA':       Sigil_COMPETENCIA,
  'INTERCAMBIO':       Sigil_INTERCAMBIO,
  'RED':               Sigil_RED,
  'EL MENSAJERO':      Sigil_MENSAJERO,
  'EL CATALIZADOR':    Sigil_CATALIZADOR,
  'EL VÍNCULO':        Sigil_VINCULO,
});
