// ORDEN — 14 sigilos (palo azul). LA DISTINCIÓN ya está en sigils.jsx.
// 1 DISTINCIÓN [✓] · 2 REPETICIÓN · 3 SIMETRÍA · 4 PATRÓN · 5 GRADIENTE
// 6 PROTOCOLO · 7 ESTRUCTURA · 8 INVARIANTE · 9 NORMA · 10 CRISTAL
// 11 EL CLASIFICADOR · 12 EL CARTÓGRAFO · 13 LA REGLA · 14 EL ALGORITMO

const O_SVG = ({ children, size = 300 }) => (
  <svg viewBox={`0 0 ${size} ${size}`} width="100%" height="100%">{children}</svg>
);
const ArrO = ({ 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>
);

// 2 · REPETICIÓN — same glyph tiled across
const Sigil_REPETICION = ({ color, size = 300 }) => {
  return <O_SVG size={size}>
    {Array.from({length:5}).map((_,row)=>(
      Array.from({length:5}).map((_,col)=>{
        const x = 40 + col*48;
        const y = 40 + row*48;
        return <g key={`${row}-${col}`} transform={`translate(${x} ${y})`}>
          <circle r={12} fill="none" stroke={color} strokeWidth={1}/>
          <line x1={-12} y1={0} x2={12} y2={0} stroke={color} strokeWidth={0.6}/>
          <line x1={0} y1={-12} x2={0} y2={12} stroke={color} strokeWidth={0.6}/>
        </g>;
      })
    ))}
  </O_SVG>;
};

// 3 · SIMETRÍA — mirrored figure with explicit axis
const Sigil_SIMETRIA = ({ color, size = 300 }) => {
  const cx = size/2, cy = size/2;
  return <O_SVG size={size}>
    {/* axis */}
    <line x1={cx} y1={30} x2={cx} y2={size-30} stroke={color} strokeWidth={0.6} strokeDasharray="4 3" opacity={0.7}/>
    {/* left figure */}
    <path d={`M ${cx-80} ${cy+40} L ${cx-60} ${cy-20} L ${cx-20} ${cy-50} L ${cx-10} ${cy+40} Z`} fill={color} fillOpacity={0.18} stroke={color} strokeWidth={1.2}/>
    <circle cx={cx-50} cy={cy} r={6} fill={color}/>
    {/* right figure — mirrored */}
    <path d={`M ${cx+80} ${cy+40} L ${cx+60} ${cy-20} L ${cx+20} ${cy-50} L ${cx+10} ${cy+40} Z`} fill={color} fillOpacity={0.18} stroke={color} strokeWidth={1.2}/>
    <circle cx={cx+50} cy={cy} r={6} fill={color}/>
    {/* mirror reflection arrows */}
    {[-60, -30, 0, 30].map((dy, i) => (
      <g key={i}>
        <line x1={cx-15} y1={cy+dy} x2={cx-3} y2={cy+dy} stroke={color} strokeWidth={0.4} opacity={0.5}/>
        <line x1={cx+3} y1={cy+dy} x2={cx+15} y2={cy+dy} stroke={color} strokeWidth={0.4} opacity={0.5}/>
      </g>
    ))}
    <text x={cx} y={20} fontSize={9} fill={color} textAnchor="middle" fontFamily="serif" fontStyle="italic" opacity={0.7}>σ</text>
  </O_SVG>;
};

// 4 · PATRÓN — tile repeating across diagonal — Alexander-style
const Sigil_PATRON = ({ color, size = 300 }) => {
  return <O_SVG size={size}>
    {Array.from({length:6}).map((_,row)=>(
      Array.from({length:6}).map((_,col)=>{
        const x = 30 + col*42;
        const y = 30 + row*42;
        const odd = (row + col) % 2;
        return <g key={`${row}-${col}`} transform={`translate(${x} ${y})`}>
          {odd ? (
            <path d="M 0 0 L 20 0 L 10 18 Z" fill="none" stroke={color} strokeWidth={1}/>
          ) : (
            <rect x={2} y={2} width={16} height={16} fill="none" stroke={color} strokeWidth={1}/>
          )}
        </g>;
      })
    ))}
  </O_SVG>;
};

// 5 · GRADIENTE — gradient bar with direction
const Sigil_GRADIENTE = ({ color, size = 300 }) => {
  const cx = size/2, cy = size/2;
  return <O_SVG size={size}>
    <defs>
      <linearGradient id="gradO5" x1="0" y1="0" x2="1" y2="0">
        <stop offset="0%" stopColor={color} stopOpacity="0.05"/>
        <stop offset="100%" stopColor={color} stopOpacity="1"/>
      </linearGradient>
      <ArrO id="aco5" color={color}/>
    </defs>
    {/* gradient bar */}
    <rect x={30} y={cy-40} width={size-60} height={80} fill="url(#gradO5)" stroke={color} strokeWidth={1}/>
    {/* arrow showing direction */}
    <line x1={50} y1={cy+60} x2={size-50} y2={cy+60} stroke={color} strokeWidth={1.5} markerEnd="url(#aco5)"/>
    {/* gridlines for value reading */}
    {[0, 1, 2, 3, 4].map(i => (
      <line key={i} x1={30 + i*(size-60)/4} y1={cy-40} x2={30 + i*(size-60)/4} y2={cy+40} stroke="#000" strokeWidth={0.3} opacity={0.4}/>
    ))}
    <text x={30} y={cy-50} fontSize={10} fill={color} fontFamily="serif" fontStyle="italic">bajo</text>
    <text x={size-30} y={cy-50} fontSize={10} fill={color} textAnchor="end" fontFamily="serif" fontStyle="italic">alto</text>
    <text x={cx} y={cy+78} fontSize={9} fill={color} textAnchor="middle" fontFamily="serif" fontStyle="italic">∇</text>
  </O_SVG>;
};

// 6 · PROTOCOLO — finite-state handshake diagram
const Sigil_PROTOCOLO = ({ color, size = 300 }) => {
  const cx = size/2, cy = size/2;
  return <O_SVG size={size}>
    <defs><ArrO id="aco6" color={color}/></defs>
    {/* 3 states */}
    {[[cx-80, cy], [cx+80, cy], [cx, cy+70]].map(([x,y],i)=>(
      <g key={i}>
        <circle cx={x} cy={y} r={22} fill="none" stroke={color} strokeWidth={1.2}/>
        <text x={x} y={y+4} fontSize={11} fill={color} textAnchor="middle" fontFamily="serif" fontStyle="italic">{['S₁','S₂','S₃'][i]}</text>
      </g>
    ))}
    {/* transitions with labels */}
    <path d={`M ${cx-58} ${cy} Q ${cx} ${cy-30} ${cx+58} ${cy}`} fill="none" stroke={color} strokeWidth={1} markerEnd="url(#aco6)"/>
    <text x={cx} y={cy-26} fontSize={8} fill={color} textAnchor="middle" fontFamily="monospace" letterSpacing="1">ack</text>
    <path d={`M ${cx+58} ${cy+8} Q ${cx+30} ${cy+50} ${cx+10} ${cy+62}`} fill="none" stroke={color} strokeWidth={1} markerEnd="url(#aco6)"/>
    <text x={cx+40} y={cy+50} fontSize={8} fill={color} textAnchor="middle" fontFamily="monospace" letterSpacing="1">data</text>
    <path d={`M ${cx-10} ${cy+62} Q ${cx-30} ${cy+50} ${cx-58} ${cy+8}`} fill="none" stroke={color} strokeWidth={1} markerEnd="url(#aco6)"/>
    <text x={cx-40} y={cy+50} fontSize={8} fill={color} textAnchor="middle" fontFamily="monospace" letterSpacing="1">syn</text>
    {/* start */}
    <line x1={cx-130} y1={cy} x2={cx-103} y2={cy} stroke={color} strokeWidth={1} markerEnd="url(#aco6)"/>
  </O_SVG>;
};

// 7 · ESTRUCTURA — relational framework / lattice
const Sigil_ESTRUCTURA = ({ color, size = 300 }) => {
  const cx = size/2, cy = size/2;
  // diamond lattice
  return <O_SVG size={size}>
    {/* top, mid, bottom nodes */}
    <circle cx={cx} cy={50} r={9} fill="none" stroke={color} strokeWidth={1.4}/>
    <circle cx={cx-70} cy={cy} r={9} fill="none" stroke={color} strokeWidth={1.4}/>
    <circle cx={cx} cy={cy} r={9} fill="none" stroke={color} strokeWidth={1.4}/>
    <circle cx={cx+70} cy={cy} r={9} fill="none" stroke={color} strokeWidth={1.4}/>
    <circle cx={cx} cy={size-50} r={9} fill="none" stroke={color} strokeWidth={1.4}/>
    <circle cx={cx-50} cy={cy-50} r={6} fill="none" stroke={color} strokeWidth={1}/>
    <circle cx={cx+50} cy={cy-50} r={6} fill="none" stroke={color} strokeWidth={1}/>
    <circle cx={cx-50} cy={cy+50} r={6} fill="none" stroke={color} strokeWidth={1}/>
    <circle cx={cx+50} cy={cy+50} r={6} fill="none" stroke={color} strokeWidth={1}/>
    {/* connections (Hasse-like) */}
    <line x1={cx} y1={59} x2={cx-46} y2={cy-54} stroke={color} strokeWidth={0.9}/>
    <line x1={cx} y1={59} x2={cx+46} y2={cy-54} stroke={color} strokeWidth={0.9}/>
    <line x1={cx-54} y1={cy-46} x2={cx-65} y2={cy-7} stroke={color} strokeWidth={0.9}/>
    <line x1={cx-46} y1={cy-44} x2={cx-7} y2={cy-6} stroke={color} strokeWidth={0.9}/>
    <line x1={cx+54} y1={cy-46} x2={cx+65} y2={cy-7} stroke={color} strokeWidth={0.9}/>
    <line x1={cx+46} y1={cy-44} x2={cx+7} y2={cy-6} stroke={color} strokeWidth={0.9}/>
    <line x1={cx-65} y1={cy+8} x2={cx-54} y2={cy+44} stroke={color} strokeWidth={0.9}/>
    <line x1={cx-7} y1={cy+6} x2={cx-46} y2={cy+44} stroke={color} strokeWidth={0.9}/>
    <line x1={cx+65} y1={cy+8} x2={cx+54} y2={cy+44} stroke={color} strokeWidth={0.9}/>
    <line x1={cx+7} y1={cy+6} x2={cx+46} y2={cy+44} stroke={color} strokeWidth={0.9}/>
    <line x1={cx-46} y1={cy+54} x2={cx} y2={size-58} stroke={color} strokeWidth={0.9}/>
    <line x1={cx+46} y1={cy+54} x2={cx} y2={size-58} stroke={color} strokeWidth={0.9}/>
  </O_SVG>;
};

// 8 · INVARIANTE — shape rotating, marker stays fixed
const Sigil_INVARIANTE = ({ color, size = 300 }) => {
  const cx = size/2, cy = size/2;
  return <O_SVG size={size}>
    {/* four rotated copies of a triangle, sharing a fixed centroid */}
    {[0, 90, 180, 270].map((rot,i)=>(
      <g key={i} transform={`translate(${cx} ${cy}) rotate(${rot})`}>
        <path d="M 0 -60 L 50 30 L -50 30 Z" fill="none" stroke={color} strokeWidth={1 - i*0.15} opacity={1 - i*0.2}/>
      </g>
    ))}
    {/* the invariant — center marker */}
    <circle cx={cx} cy={cy} r={10} fill={color}/>
    <circle cx={cx} cy={cy} r={18} fill="none" stroke={color} strokeWidth={0.6} strokeDasharray="2 2"/>
    {/* rotation arrow */}
    <path d={`M ${cx+80} ${cy-12} A 80 80 0 0 1 ${cx+76} ${cy+20}`} fill="none" stroke={color} strokeWidth={1} markerEnd="url(#aco8)"/>
    <defs><ArrO id="aco8" color={color}/></defs>
    <text x={cx+90} y={cy+8} fontSize={10} fill={color} fontFamily="serif" fontStyle="italic">θ</text>
  </O_SVG>;
};

// 9 · NORMA — parallel rows with limit marker
const Sigil_NORMA = ({ color, size = 300 }) => {
  const cx = size/2;
  return <O_SVG size={size}>
    {/* horizontal rows representing samples */}
    {Array.from({length:9}).map((_,i)=>{
      const y = 50 + i*24;
      const center = cx;
      const dev = [4, -2, 1, -1, 2, 0, -3, 0, 1][i] * 4;
      return <g key={i}>
        <line x1={50} y1={y} x2={size-50} y2={y} stroke={color} strokeWidth={0.3} opacity={0.4}/>
        <circle cx={center+dev} cy={y} r={3.5} fill={color}/>
      </g>;
    })}
    {/* the norm — vertical line */}
    <line x1={cx} y1={40} x2={cx} y2={size-40} stroke={color} strokeWidth={1.5}/>
    {/* tolerance band */}
    <line x1={cx-20} y1={40} x2={cx-20} y2={size-40} stroke={color} strokeWidth={0.5} strokeDasharray="3 2" opacity={0.7}/>
    <line x1={cx+20} y1={40} x2={cx+20} y2={size-40} stroke={color} strokeWidth={0.5} strokeDasharray="3 2" opacity={0.7}/>
    <text x={cx} y={32} fontSize={11} fill={color} textAnchor="middle" fontFamily="serif" fontStyle="italic">μ</text>
    <text x={cx-20} y={size-26} fontSize={9} fill={color} textAnchor="middle" fontFamily="monospace" opacity={0.7}>−σ</text>
    <text x={cx+20} y={size-26} fontSize={9} fill={color} textAnchor="middle" fontFamily="monospace" opacity={0.7}>+σ</text>
  </O_SVG>;
};

// 10 · CRISTAL — perfect square lattice
const Sigil_CRISTAL_ORDEN = ({ color, size = 300 }) => {
  return <O_SVG size={size}>
    {Array.from({length:7}).map((_,row)=>(
      Array.from({length:7}).map((_,col)=>{
        const x = 40 + col*36;
        const y = 40 + row*36;
        return <g key={`${row}-${col}`}>
          <circle cx={x} cy={y} r={3.5} fill={color}/>
          {col < 6 && <line x1={x} y1={y} x2={x+36} y2={y} stroke={color} strokeWidth={0.5} opacity={0.7}/>}
          {row < 6 && <line x1={x} y1={y} x2={x} y2={y+36} stroke={color} strokeWidth={0.5} opacity={0.7}/>}
        </g>;
      })
    ))}
    {/* unit cell highlight */}
    <rect x={148} y={148} width={36} height={36} fill="none" stroke={color} strokeWidth={1.2} strokeDasharray="3 2"/>
  </O_SVG>;
};

// 11 · EL CLASIFICADOR (Sota) — Venn diagram
const Sigil_CLASIFICADOR = ({ color, size = 300 }) => {
  const cx = size/2, cy = size/2;
  return <O_SVG size={size}>
    <circle cx={cx-30} cy={cy-15} r={60} fill={color} fillOpacity={0.12} stroke={color} strokeWidth={1.2}/>
    <circle cx={cx+30} cy={cy-15} r={60} fill={color} fillOpacity={0.12} stroke={color} strokeWidth={1.2}/>
    <circle cx={cx} cy={cy+30} r={60} fill={color} fillOpacity={0.12} stroke={color} strokeWidth={1.2}/>
    <text x={cx-55} y={cy-30} fontSize={11} fill={color} textAnchor="middle" fontFamily="serif" fontStyle="italic">A</text>
    <text x={cx+55} y={cy-30} fontSize={11} fill={color} textAnchor="middle" fontFamily="serif" fontStyle="italic">B</text>
    <text x={cx} y={cy+62} fontSize={11} fill={color} textAnchor="middle" fontFamily="serif" fontStyle="italic">C</text>
    <text x={cx} y={cy+6} fontSize={10} fill={color} textAnchor="middle" fontFamily="serif" fontStyle="italic" opacity={0.85}>A∩B∩C</text>
  </O_SVG>;
};

// 12 · EL CARTÓGRAFO (Caballero) — map with grid + location pin
const Sigil_CARTOGRAFO = ({ color, size = 300 }) => {
  const cx = size/2, cy = size/2;
  return <O_SVG size={size}>
    {/* outer frame */}
    <rect x={30} y={30} width={size-60} height={size-60} fill="none" stroke={color} strokeWidth={1.2}/>
    {/* graticule */}
    {[1, 2, 3, 4].map(i => (
      <g key={i}>
        <line x1={30 + i*(size-60)/5} y1={30} x2={30 + i*(size-60)/5} y2={size-30} stroke={color} strokeWidth={0.3} opacity={0.4}/>
        <line x1={30} y1={30 + i*(size-60)/5} x2={size-30} y2={30 + i*(size-60)/5} stroke={color} strokeWidth={0.3} opacity={0.4}/>
      </g>
    ))}
    {/* coastlines / contours */}
    <path d="M 50 130 Q 100 100 130 140 Q 170 180 220 150 Q 240 130 250 160" fill="none" stroke={color} strokeWidth={1}/>
    <path d="M 60 200 Q 110 180 140 210 Q 180 240 230 220" fill="none" stroke={color} strokeWidth={0.7} opacity={0.7}/>
    {/* compass rose */}
    <g transform={`translate(${size-65} 65)`}>
      <circle r={18} fill="none" stroke={color} strokeWidth={0.6}/>
      <path d="M 0 -18 L 4 0 L 0 18 L -4 0 Z" fill={color}/>
      <text y={-22} textAnchor="middle" fontSize={9} fill={color} fontFamily="serif">N</text>
    </g>
    {/* location pin */}
    <path d={`M ${cx} ${cy} L ${cx-6} ${cy-12} A 8 8 0 1 1 ${cx+6} ${cy-12} Z`} fill={color}/>
    <circle cx={cx} cy={cy-15} r={3} fill="#000"/>
  </O_SVG>;
};

// 13 · LA REGLA (Reina) — formal rule notation with turnstile
const Sigil_REGLA = ({ color, size = 300 }) => {
  const cx = size/2, cy = size/2;
  return <O_SVG size={size}>
    {/* rule format: A ⊢ B */}
    <text x={cx-65} y={cy+8} fontSize={36} fill={color} textAnchor="middle" fontFamily="serif" fontStyle="italic">A</text>
    {/* turnstile ⊢ */}
    <g transform={`translate(${cx-15} ${cy})`}>
      <line x1={0} y1={-22} x2={0} y2={22} stroke={color} strokeWidth={2.5}/>
      <line x1={0} y1={0} x2={28} y2={0} stroke={color} strokeWidth={2.5}/>
    </g>
    <text x={cx+45} y={cy+8} fontSize={36} fill={color} textAnchor="middle" fontFamily="serif" fontStyle="italic">B</text>
    {/* fine print */}
    <text x={cx} y={cy-58} fontSize={9} fill={color} textAnchor="middle" fontFamily="monospace" letterSpacing="2" opacity={0.7}>SI A ENTONCES B</text>
    <line x1={cx-60} y1={cy+50} x2={cx+60} y2={cy+50} stroke={color} strokeWidth={1}/>
    {/* below: instances */}
    <text x={cx} y={cy+72} fontSize={9} fill={color} textAnchor="middle" fontFamily="monospace" letterSpacing="2" opacity={0.85}>∀x · P(x) → Q(x)</text>
  </O_SVG>;
};

// 14 · EL ALGORITMO (Rey) — flowchart with loop
const Sigil_ALGORITMO = ({ color, size = 300 }) => {
  const cx = size/2, cy = size/2;
  return <O_SVG size={size}>
    <defs><ArrO id="aco14" color={color}/></defs>
    {/* start */}
    <rect x={cx-35} y={40} width={70} height={28} rx={14} fill="none" stroke={color} strokeWidth={1.2}/>
    <text x={cx} y={58} fontSize={9} fill={color} textAnchor="middle" fontFamily="monospace" letterSpacing="2">INICIO</text>
    {/* arrow */}
    <line x1={cx} y1={68} x2={cx} y2={92} stroke={color} strokeWidth={1} markerEnd="url(#aco14)"/>
    {/* process */}
    <rect x={cx-40} y={94} width={80} height={28} fill="none" stroke={color} strokeWidth={1.2}/>
    <text x={cx} y={112} fontSize={9} fill={color} textAnchor="middle" fontFamily="monospace" letterSpacing="2">PASO</text>
    <line x1={cx} y1={122} x2={cx} y2={146} stroke={color} strokeWidth={1} markerEnd="url(#aco14)"/>
    {/* decision diamond */}
    <path d={`M ${cx} 146 L ${cx+34} 170 L ${cx} 194 L ${cx-34} 170 Z`} fill="none" stroke={color} strokeWidth={1.2}/>
    <text x={cx} y={174} fontSize={9} fill={color} textAnchor="middle" fontFamily="serif" fontStyle="italic">¿ok?</text>
    {/* yes → end */}
    <line x1={cx} y1={194} x2={cx} y2={218} stroke={color} strokeWidth={1} markerEnd="url(#aco14)"/>
    <text x={cx+8} y={210} fontSize={8} fill={color} fontFamily="monospace" opacity={0.7}>sí</text>
    {/* end */}
    <rect x={cx-35} y={220} width={70} height={28} rx={14} fill="none" stroke={color} strokeWidth={1.2}/>
    <text x={cx} y={238} fontSize={9} fill={color} textAnchor="middle" fontFamily="monospace" letterSpacing="2">FIN</text>
    {/* loop back: no */}
    <path d={`M ${cx-34} 170 L ${cx-80} 170 L ${cx-80} 108 L ${cx-40} 108`} fill="none" stroke={color} strokeWidth={1} markerEnd="url(#aco14)"/>
    <text x={cx-72} y={166} fontSize={8} fill={color} fontFamily="monospace" opacity={0.7}>no</text>
  </O_SVG>;
};

Object.assign(PILOT_SIGILS, {
  'REPETICIÓN':     Sigil_REPETICION,
  'SIMETRÍA':       Sigil_SIMETRIA,
  'PATRÓN':         Sigil_PATRON,
  'GRADIENTE':      Sigil_GRADIENTE,
  'PROTOCOLO':      Sigil_PROTOCOLO,
  'ESTRUCTURA':     Sigil_ESTRUCTURA,
  'INVARIANTE':     Sigil_INVARIANTE,
  'NORMA':          Sigil_NORMA,
  'CRISTAL':        Sigil_CRISTAL_ORDEN,
  'EL CLASIFICADOR':Sigil_CLASIFICADOR,
  'EL CARTÓGRAFO':  Sigil_CARTOGRAFO,
  'LA REGLA':       Sigil_REGLA,
  'EL ALGORITMO':   Sigil_ALGORITMO,
});
