/* global React, SectionHead2 */
// NUON multi-page · page-specific sections

const { useState: useS2, useEffect: useE2, useRef: useR2 } = React;

// ============================================================
// HOME TILES (short home version)
// ============================================================
function HomeTiles({ copy }) {
  return (
    <section className="section">
      <div className="container">
        <div className="home-tiles">
          {copy.tiles.map((t, i) => (
            <a key={i} href={t.href} className="tile" style={{ "--accent": t.color }}>
              <div style={{ display: "flex", justifyContent: "space-between", alignItems: "center" }}>
                <div className="tile__overline" style={{ color: t.color }}>{t.overline}</div>
                <div className="tile__num">{t.num} / 03</div>
              </div>
              <h3 className="tile__title">{t.title}</h3>
              <p className="tile__body">{t.body}</p>
              <div className="tile__link" style={{ color: t.color }}>
                {t.overline === "Producto" || t.overline === "Product" ? "Ver producto →" : t.overline === "Mercados" || t.overline === "Markets" ? "Ver mercados →" : "Ver casos →"}
              </div>
            </a>
          ))}
        </div>
      </div>
    </section>
  );
}

// ============================================================
// HOME MANIFIESTO STRIP
// ============================================================
function HomeManifest({ copy }) {
  return (
    <section className="home-manifest">
      <div className="container">
        <div className="home-manifest__inner">
          <div>
            <span className="home-manifest__overline">{copy.manifiesto_overline}</span>
            <p className="home-manifest__quote">
              {copy.manifiesto_quote_pre}<em>{copy.manifiesto_quote_em}</em>{copy.manifiesto_quote_post}
            </p>
          </div>
          <div>
            <a className="btn btn--ondark btn--lg" href="empresa.html">
              <span>{copy.manifiesto_btn}</span>
              <span className="arr"></span>
            </a>
          </div>
        </div>
      </div>
    </section>
  );
}

// ============================================================
// ARCHITECTURE (Producto)
// ============================================================
function Architecture({ copy }) {
  return (
    <section className="section">
      <div className="container">
        <SectionHead2 overline={copy.overline} title={copy.title} lede={copy.lede} />
        <div className="arch">
          <div className="arch__layers">
            {copy.layers.map((l, i) => (
              <div key={i} className="arch__layer" style={{ "--accent": l.color, borderLeftColor: l.color }}>
                <div className="arch__layer-num" style={{ color: l.color }}>{l.num}</div>
                <div className="arch__layer-name">{l.name}</div>
                <div className="arch__layer-desc">{l.desc}</div>
              </div>
            ))}
          </div>
          <div className="arch__diagram">
            <ArchDiagram />
          </div>
        </div>
      </div>
    </section>
  );
}

// Architecture diagram — animated event bus with packets flowing
function ArchDiagram() {
  return (
    <svg className="arch__diagram-svg" viewBox="0 0 600 580" preserveAspectRatio="xMidYMid meet">
      <defs>
        <linearGradient id="bus-grad" x1="0" y1="0" x2="0" y2="1">
          <stop offset="0%" stopColor="#76D6FF" />
          <stop offset="25%" stopColor="#0096FF" />
          <stop offset="50%" stopColor="#7A81FF" />
          <stop offset="75%" stopColor="#D883FF" />
          <stop offset="100%" stopColor="#FDBFE8" />
        </linearGradient>
        <marker id="arch-arr" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="6" markerHeight="6" orient="auto-start-reverse">
          <path d="M0,0 L10,5 L0,10 z" fill="currentColor" />
        </marker>
      </defs>

      {/* Event bus (vertical line) */}
      <rect x="290" y="60" width="20" height="460" fill="url(#bus-grad)" opacity="0.16" rx="10" />
      <line x1="300" y1="60" x2="300" y2="520" stroke="url(#bus-grad)" strokeWidth="2" />
      <text x="300" y="48" textAnchor="middle" fontFamily="IBM Plex Mono, monospace" fontSize="10" letterSpacing="1.5" fill="#0096FF">[ EVENT BUS ]</text>
      <text x="300" y="540" textAnchor="middle" fontFamily="IBM Plex Mono, monospace" fontSize="10" letterSpacing="1" fill="#6B7A96">↻ 1 Hz · 7M pts/min</text>

      {/* Flowing packets on the bus */}
      {[0, 1, 2, 3, 4].map((i) => (
        <circle key={i} cx="300" cy="60" r="3" fill="#FFFFFF" stroke="#0096FF" strokeWidth="1">
          <animate attributeName="cy" from="60" to="520" dur="3.6s" begin={`${i * 0.8}s`} repeatCount="indefinite" />
          <animate attributeName="opacity" values="0;1;1;0" keyTimes="0;0.1;0.9;1" dur="3.6s" begin={`${i * 0.8}s`} repeatCount="indefinite" />
        </circle>
      ))}

      {/* Layers (right side) */}
      {[
        { y: 100, name: "L5 · SETTLEMENT", color: "#FDBFE8" },
        { y: 190, name: "L4 · DISPATCH", color: "#D883FF" },
        { y: 280, name: "L3 · OPTIMIZATION", color: "#7A81FF" },
        { y: 370, name: "L2 · FORECAST", color: "#0096FF" },
        { y: 460, name: "L1 · TELEMETRY", color: "#76D6FF" },
      ].map((l, i) => (
        <g key={i}>
          {/* connection line */}
          <line x1="310" y1={l.y} x2="380" y2={l.y} stroke={l.color} strokeWidth="1.5" markerEnd="url(#arch-arr)" style={{ color: l.color }} className="flow-anim" />
          {/* layer card */}
          <rect x="380" y={l.y - 26} width="180" height="52" rx="8" fill="#FFFFFF" stroke={l.color} strokeWidth="1" />
          <text x="392" y={l.y - 8} fontFamily="IBM Plex Mono, monospace" fontSize="10" letterSpacing="1.4" fill={l.color}>{l.name}</text>
          <circle cx="544" cy={l.y - 12} r="3" fill={l.color}>
            <animate attributeName="opacity" values="1;0.3;1" dur="1.6s" begin={`${i * 0.3}s`} repeatCount="indefinite" />
          </circle>
          <text x="392" y={l.y + 10} fontFamily="IBM Plex Mono, monospace" fontSize="9" letterSpacing="1.2" fill="#6B7A96">↻ ACTIVE</text>
        </g>
      ))}

      {/* Sources (left side) */}
      {[
        { y: 100, name: "ERP · Cliente" },
        { y: 190, name: "Gateway BESS" },
        { y: 280, name: "Inversor solar" },
        { y: 370, name: "Mercado spot" },
        { y: 460, name: "Sensor demanda" },
      ].map((s, i) => (
        <g key={i}>
          <rect x="40" y={s.y - 18} width="160" height="36" rx="6" fill="#F4F8FD" stroke="#B0C0D5" strokeWidth="0.8" />
          <circle cx="56" cy={s.y} r="3" fill="#0096FF">
            <animate attributeName="opacity" values="1;0.3;1" dur="2s" begin={`${i * 0.25}s`} repeatCount="indefinite" />
          </circle>
          <text x="68" y={s.y + 3.5} fontFamily="IBM Plex Mono, monospace" fontSize="9" letterSpacing="1" fill="#3D4E6B">{s.name}</text>
          <line x1="200" y1={s.y} x2="290" y2={s.y} stroke="#9DAEC4" strokeWidth="0.7" strokeDasharray="3 2" />
        </g>
      ))}

      {/* Top label */}
      <text x="40" y="36" fontFamily="IBM Plex Mono, monospace" fontSize="10" letterSpacing="1.4" fill="#6B7A96">FUENTES · 2,341</text>
      <text x="560" y="36" textAnchor="end" fontFamily="IBM Plex Mono, monospace" fontSize="10" letterSpacing="1.4" fill="#6B7A96">CAPAS · 5</text>
    </svg>
  );
}

// ============================================================
// SPECS TABLE
// ============================================================
function Specs({ copy }) {
  return (
    <section className="section" style={{ background: "var(--ink-50)" }}>
      <div className="container">
        <SectionHead2 overline={copy.overline} title={copy.title} />
        <div className="specs">
          {copy.items.map(([label, num, unit, sub], i) => (
            <div className="spec" key={i}>
              <div className="spec__label">{label}</div>
              <div className="spec__value">{num}<span className="u">{unit}</span></div>
              <div className="spec__sub">{sub}</div>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

// ============================================================
// API + CODE
// ============================================================
function ApiSection({ copy }) {
  // syntax-highlight the code (minimal: comments, strings, keywords, function calls)
  const codeWithSyntax = (raw) => {
    const lines = raw.split("\n");
    return lines.map((line, i) => {
      // simple syntax coloring
      let result = [];
      const trimmed = line;
      if (trimmed.trim().startsWith("#")) {
        result.push(<span key="c" className="com">{trimmed}</span>);
      } else {
        // tokenize roughly
        const tokens = trimmed.split(/("[^"]*"|\b(?:import|from|def|for|in|print|f|client)\b)/);
        tokens.forEach((tok, j) => {
          if (!tok) return;
          if (tok.startsWith('"')) {
            result.push(<span key={`${i}-${j}`} className="str">{tok}</span>);
          } else if (["import", "from", "def", "for", "in", "client"].includes(tok)) {
            result.push(<span key={`${i}-${j}`} className="kw">{tok}</span>);
          } else if (tok === "print" || tok === "f") {
            result.push(<span key={`${i}-${j}`} className="fn">{tok}</span>);
          } else {
            result.push(<React.Fragment key={`${i}-${j}`}>{tok}</React.Fragment>);
          }
        });
      }
      return <div key={i}>{result.length ? result : "\u00A0"}</div>;
    });
  };

  return (
    <section className="section" id="api">
      <div className="container">
        <SectionHead2 overline={copy.overline} title={copy.title} lede={copy.lede} />
        <div className="api">
          <div className="api__intro">
            <div className="api__bullets">
              {copy.bullets.map((b, i) => <div className="api__bullet" key={i}>{b}</div>)}
            </div>
          </div>
          <div className="api__code">
            <div className="api__code-head">
              <div className="api__code-dots">
                <span></span><span></span><span></span>
              </div>
              <div className="api__code-file">cycle_listener.py</div>
            </div>
            <pre>{codeWithSyntax(copy.code)}</pre>
          </div>
        </div>
      </div>
    </section>
  );
}

// ============================================================
// COVERAGE MAP
// ============================================================
function CoverageMap({ copy }) {
  return (
    <section className="section" style={{ background: "var(--ink-50)" }}>
      <div className="container">
        <SectionHead2 overline={copy.overline} title={copy.title} lede={copy.lede} />
        <div className="coverage-map">
          <div className="coverage-map__inner">
            <svg viewBox="0 0 1200 540" preserveAspectRatio="xMidYMid meet">
              <defs>
                <radialGradient id="cm-pulse" cx="50%" cy="50%" r="50%">
                  <stop offset="0%" stopColor="#0096FF" stopOpacity="0.4" />
                  <stop offset="100%" stopColor="#0096FF" stopOpacity="0" />
                </radialGradient>
              </defs>

              {/* Decorative regions — simplified continents */}
              <g opacity="0.5">
                {/* América */}
                <path d="M 220 80 Q 280 60 340 100 L 360 180 Q 320 240 280 280 L 240 360 Q 220 420 240 470 L 280 510 Q 220 510 200 460 L 180 380 Q 160 320 180 240 L 200 160 Z"
                  fill="none" stroke="#B0C0D5" strokeWidth="0.7" strokeDasharray="3 3" />
                {/* Europa */}
                <path d="M 760 100 Q 820 80 880 110 L 920 160 Q 940 200 920 230 L 880 250 Q 820 240 780 220 L 740 180 Q 720 140 760 100 Z"
                  fill="none" stroke="#B0C0D5" strokeWidth="0.7" strokeDasharray="3 3" />
              </g>

              {/* Lat/lng grid */}
              <g opacity="0.25" stroke="#94A3BD" strokeWidth="0.5">
                {[60, 120, 180, 240, 300, 360, 420, 480].map(y => (
                  <line key={y} x1="20" y1={y} x2="1180" y2={y} strokeDasharray="2 4" />
                ))}
                {[100, 250, 400, 550, 700, 850, 1000, 1150].map(x => (
                  <line key={x} x1={x} y1="40" x2={x} y2="500" strokeDasharray="2 4" />
                ))}
              </g>

              {/* Country labels */}
              <text x="250" y="60" fontFamily="IBM Plex Mono, monospace" fontSize="11" letterSpacing="2" fill="#0096FF">[ AMÉRICA · 3 MERCADOS ]</text>
              <text x="820" y="80" fontFamily="IBM Plex Mono, monospace" fontSize="11" letterSpacing="2" fill="#0096FF">[ IBÉRICO · 1 MERCADO ]</text>

              {/* Nodes — Colombia */}
              <g>
                <NodeDot cx={240} cy={180} label="Bogotá" mw="312" color="#0096FF" delay={0} />
                <NodeDot cx={220} cy={210} label="Floridablanca" color="#0096FF" delay={0.2} small />
                <NodeDot cx={230} cy={230} label="Medellín" color="#0096FF" delay={0.4} small />
                <NodeDot cx={210} cy={270} label="Cali" color="#0096FF" delay={0.6} small />
                <NodeDot cx={250} cy={150} label="Cartagena" color="#0096FF" delay={0.8} small />
                <text x="220" y="135" fontFamily="IBM Plex Mono, monospace" fontSize="10" letterSpacing="1" fill="#0096FF">CO</text>
              </g>

              {/* Nodes — Chile */}
              <g>
                <NodeDot cx={250} cy={380} label="Antofagasta" mw="218" color="#7A81FF" delay={0.1} />
                <NodeDot cx={260} cy={420} label="Atacama" color="#7A81FF" delay={0.3} small />
                <NodeDot cx={270} cy={450} label="Santiago" color="#7A81FF" delay={0.5} small />
                <NodeDot cx={275} cy={480} label="Concepción" color="#7A81FF" delay={0.7} small />
                <NodeDot cx={245} cy={355} label="Coquimbo" color="#7A81FF" delay={0.9} small />
                <text x="250" y="340" fontFamily="IBM Plex Mono, monospace" fontSize="10" letterSpacing="1" fill="#7A81FF">CL</text>
              </g>

              {/* Nodes — México */}
              <g>
                <NodeDot cx={140} cy={150} label="Monterrey" mw="192" color="#D883FF" delay={0.15} />
                <NodeDot cx={150} cy={185} label="Querétaro" color="#D883FF" delay={0.35} small />
                <NodeDot cx={155} cy={210} label="CDMX" color="#D883FF" delay={0.55} small />
                <NodeDot cx={130} cy={170} label="Guadalajara" color="#D883FF" delay={0.75} small />
                <NodeDot cx={120} cy={130} label="Tijuana" color="#D883FF" delay={0.95} small />
                <text x="125" y="110" fontFamily="IBM Plex Mono, monospace" fontSize="10" letterSpacing="1" fill="#D883FF">MX</text>
              </g>

              {/* Nodes — España */}
              <g>
                <NodeDot cx={820} cy={170} label="Madrid" mw="125" color="#FDBFE8" delay={0.2} />
                <NodeDot cx={860} cy={160} label="Barcelona" color="#FDBFE8" delay={0.4} small />
                <NodeDot cx={810} cy={210} label="Sevilla" color="#FDBFE8" delay={0.6} small />
                <NodeDot cx={840} cy={195} label="Valencia" color="#FDBFE8" delay={0.8} small />
                <NodeDot cx={800} cy={135} label="Bilbao" color="#FDBFE8" delay={1.0} small />
                <text x="800" y="115" fontFamily="IBM Plex Mono, monospace" fontSize="10" letterSpacing="1" fill="#FDBFE8">ES</text>
              </g>

              {/* Connection lines between major hubs (going through the core) */}
              <g fill="none" stroke="#0096FF" strokeWidth="0.8" strokeDasharray="3 4" opacity="0.45">
                <path d="M 240 180 Q 400 280 250 380" />
                <path d="M 240 180 Q 180 170 140 150" />
                <path d="M 240 180 Q 500 130 820 170" />
                <path d="M 140 150 Q 480 100 820 170" />
                <path d="M 250 380 Q 530 300 820 170" />
              </g>

              {/* Total summary box */}
              <g transform="translate(960 360)">
                <rect x="0" y="0" width="220" height="130" rx="10" fill="rgba(255,255,255,0.96)" stroke="rgba(0,150,255,0.18)" strokeWidth="1" />
                <text x="20" y="28" fontFamily="IBM Plex Mono, monospace" fontSize="10" letterSpacing="1.4" fill="#0096FF">[ TOTAL · TIEMPO REAL ]</text>
                <text x="20" y="68" fontFamily="Bebas Neue, sans-serif" fontSize="44" letterSpacing="-0.5" fill="#0D1428">847<tspan fontFamily="Montserrat, sans-serif" fontSize="14" fontWeight="400" fill="#6B7A96"> MW</tspan></text>
                <text x="20" y="92" fontFamily="IBM Plex Mono, monospace" fontSize="10" letterSpacing="1" fill="#6B7A96">2,341 ACTIVOS · 4 MERCADOS</text>
                <text x="20" y="110" fontFamily="IBM Plex Mono, monospace" fontSize="10" letterSpacing="1" fill="#34d8d0">↻ 99.97% UPTIME · 12M</text>
              </g>
            </svg>

            <div className="coverage-legend">
              <div className="head">[ NODOS · TIEMPO REAL ]</div>
              <div className="row"><span className="dot" style={{ background: "#0096FF" }}></span><span>Colombia</span><span className="num">06</span></div>
              <div className="row"><span className="dot" style={{ background: "#7A81FF" }}></span><span>Chile</span><span className="num">05</span></div>
              <div className="row"><span className="dot" style={{ background: "#D883FF" }}></span><span>México</span><span className="num">05</span></div>
              <div className="row"><span className="dot" style={{ background: "#FDBFE8" }}></span><span>España</span><span className="num">05</span></div>
            </div>
          </div>
        </div>
      </div>
    </section>
  );
}

function NodeDot({ cx, cy, label, mw, color, delay = 0, small = false }) {
  const r = small ? 4 : 7;
  return (
    <g>
      <circle cx={cx} cy={cy} r={r * 4} fill={color} opacity="0.12">
        <animate attributeName="r" values={`${r * 2};${r * 5};${r * 2}`} dur="3s" begin={`${delay}s`} repeatCount="indefinite" />
        <animate attributeName="opacity" values="0.25;0;0.25" dur="3s" begin={`${delay}s`} repeatCount="indefinite" />
      </circle>
      <circle cx={cx} cy={cy} r={r} fill={color} />
      <circle cx={cx} cy={cy} r={r - 2} fill="#fff" />
      {!small && (
        <>
          <text x={cx + 12} y={cy - 4} fontFamily="IBM Plex Mono, monospace" fontSize="9" letterSpacing="1" fill="#3D4E6B">{label.toUpperCase()}</text>
          {mw && <text x={cx + 12} y={cy + 8} fontFamily="Bebas Neue, sans-serif" fontSize="14" fill={color}>{mw} MW</text>}
        </>
      )}
    </g>
  );
}

// ============================================================
// REG GRID (regulatory frameworks)
// ============================================================
function RegFrameworks({ copy }) {
  return (
    <section className="section">
      <div className="container">
        <SectionHead2 overline={copy.overline} title={copy.title} />
        <div className="reg-grid">
          {copy.items.map((c, i) => (
            <div className="reg-card" key={i}>
              <div className="reg-card__head">
                <div className="reg-card__flag">{c.flag}</div>
                <div className="reg-card__name">{c.name}</div>
              </div>
              <div className="reg-card__body">{c.body}</div>
              <div className="reg-card__regs">
                {c.regs.map((r, j) => <span className="reg-card__reg" key={j}>{r}</span>)}
              </div>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

// ============================================================
// TIMELINE
// ============================================================
function Timeline({ copy, withQuarter = true }) {
  return (
    <section className="section" style={{ background: "var(--ink-50)" }}>
      <div className="container">
        <SectionHead2 overline={copy.overline} title={copy.title} />
        <div className="timeline">
          {copy.events.map((e, i) => (
            <div className="tl-event" key={i}>
              <div className="tl-event__when">
                <span className="year">{e.year}</span>
                {withQuarter ? e.q : ""}
              </div>
              <div>
                {e.title && <div className="tl-event__title">{e.title}</div>}
                <div className="tl-event__body">{e.text || e.body}</div>
              </div>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

// ============================================================
// CASOS ALL (6 case studies)
// ============================================================
function CasosAll({ baseItems, extraItems }) {
  const all = [...baseItems, ...extraItems];
  return (
    <section className="section">
      <div className="container">
        <div className="casos--all">
          {all.map((c, i) => (
            <article className="caso" key={i}>
              <div className="caso__visual" style={{ background: `linear-gradient(135deg, ${c.color}10 0%, #ffffff 100%)` }}>
                <CasoMiniIllust idx={i % 6} color={c.color} />
                <span className="caso__industry">{c.industry}</span>
                <span className="caso__country">↻ {c.country}</span>
              </div>
              <div className="caso__body">
                <h3 className="caso__title">{c.title}</h3>
                <p className="caso__quote">"{c.quote}"</p>
                <div className="caso__metric">
                  <div className="caso__metric-num grad">{c.metric[0]}</div>
                  <div className="caso__metric-label">{c.metric[1]}</div>
                </div>
              </div>
            </article>
          ))}
        </div>
      </div>
    </section>
  );
}

function CasoMiniIllust({ idx, color }) {
  if (idx === 0) {
    return (
      <svg viewBox="0 0 400 200" preserveAspectRatio="xMidYMid meet">
        <path d="M 200 80 L 290 125 L 290 165 L 200 120 Z" fill="#D4DEEB" stroke="#9DAEC4" strokeWidth="0.7" />
        <path d="M 200 80 L 110 125 L 110 165 L 200 120 Z" fill="#E4ECF5" stroke="#9DAEC4" strokeWidth="0.7" />
        <path d="M 200 40 L 290 85 L 200 120 L 110 85 Z" fill="#FFFFFF" stroke="#9DAEC4" strokeWidth="0.7" />
        <path d="M 200 40 L 290 85 L 280 90 L 195 47 Z" fill={color} opacity="0.85" />
      </svg>
    );
  }
  if (idx === 1) {
    return (
      <svg viewBox="0 0 400 200" preserveAspectRatio="xMidYMid meet">
        {[0, 1, 2, 3].map((i) => (
          <g key={i}>
            <rect x={100 + i * 50} y="60" width="34" height="100" fill="#F1F5FA" stroke="#9DAEC4" strokeWidth="0.7" rx="2" />
            {[0, 1, 2, 3, 4].map((j) => (
              <rect key={j} x={104 + i * 50} y={68 + j * 18} width="26" height="2" fill={color} opacity={0.5 + (j % 2) * 0.4} />
            ))}
          </g>
        ))}
      </svg>
    );
  }
  if (idx === 2) {
    // EV
    return (
      <svg viewBox="0 0 400 200" preserveAspectRatio="xMidYMid meet">
        <line x1="40" y1="160" x2="360" y2="160" stroke="#B0C0D5" strokeWidth="1" />
        {[60, 130, 200, 270, 340].map((x, i) => (
          <g key={i}>
            <rect x={x - 24} y="115" width="48" height="30" fill="#F1F5FA" stroke="#9DAEC4" strokeWidth="0.7" rx="4" />
            <circle cx={x - 14} cy="148" r="5" fill="#3D4E6B" />
            <circle cx={x + 14} cy="148" r="5" fill="#3D4E6B" />
            <line x1={x} y1="115" x2={x} y2="90" stroke={color} strokeWidth="1.5" />
            <circle cx={x} cy="85" r="4" fill={color} />
          </g>
        ))}
      </svg>
    );
  }
  if (idx === 3) {
    // Mining — pit isometric
    return (
      <svg viewBox="0 0 400 200" preserveAspectRatio="xMidYMid meet">
        <path d="M 80 60 L 320 60 L 280 100 L 120 100 Z" fill="#F1F5FA" stroke="#9DAEC4" strokeWidth="0.7" />
        <path d="M 120 100 L 280 100 L 240 140 L 160 140 Z" fill="#E4ECF5" stroke="#9DAEC4" strokeWidth="0.7" />
        <path d="M 160 140 L 240 140 L 200 180 Z" fill={color} opacity="0.85" />
        <circle cx="100" cy="55" r="4" fill={color}>
          <animate attributeName="opacity" values="1;0.3;1" dur="1.6s" repeatCount="indefinite" />
        </circle>
        <circle cx="300" cy="55" r="4" fill={color}>
          <animate attributeName="opacity" values="1;0.3;1" dur="1.6s" begin="0.4s" repeatCount="indefinite" />
        </circle>
      </svg>
    );
  }
  if (idx === 4) {
    // Agro — pump
    return (
      <svg viewBox="0 0 400 200" preserveAspectRatio="xMidYMid meet">
        {[0, 1, 2].map((i) => (
          <g key={i}>
            <circle cx={100 + i * 100} cy="120" r="32" fill="none" stroke="#9DAEC4" strokeWidth="0.8" />
            <circle cx={100 + i * 100} cy="120" r="14" fill={color} opacity="0.5" />
            <line x1={100 + i * 100} y1="120" x2={100 + i * 100 + 20} y2="120" stroke={color} strokeWidth="2">
              <animateTransform attributeName="transform" type="rotate" from="0" to="360" dur={`${1.5 + i * 0.5}s`} begin={`${i * 0.2}s`} repeatCount="indefinite" />
            </line>
          </g>
        ))}
      </svg>
    );
  }
  // Retail — buildings
  return (
    <svg viewBox="0 0 400 200" preserveAspectRatio="xMidYMid meet">
      {[60, 140, 220, 300].map((x, i) => (
        <g key={i}>
          <rect x={x - 24} y={80 + i * 5} width="48" height={80 - i * 5} fill="#F1F5FA" stroke="#9DAEC4" strokeWidth="0.7" />
          <rect x={x - 24} y={80 + i * 5} width="48" height="4" fill={color} />
          {[0, 1, 2].map((r) => [0, 1].map((c) => (
            <rect key={`${r}-${c}`} x={x - 18 + c * 18} y={90 + r * 14 + i * 5} width="10" height="6" fill="#D4DEEB" />
          )))}
        </g>
      ))}
    </svg>
  );
}

// ============================================================
// INDUSTRY BREAKDOWN (bars)
// ============================================================
function Breakdown({ copy }) {
  const max = Math.max(...copy.items.map(([, v]) => v));
  const total = copy.items.reduce((s, [, v]) => s + v, 0);
  return (
    <section className="section" style={{ background: "var(--ink-50)" }}>
      <div className="container">
        <SectionHead2 overline={copy.overline} title={copy.title} lede={`Total · ${total} MW`} />
        <div className="breakdown">
          {copy.items.map(([label, value, color], i) => (
            <div className="bd-row" key={i} style={{ "--accent": color }}>
              <div className="bd-row__label">{label}</div>
              <div className="bd-row__bar">
                <div className="bd-row__fill" style={{ width: `${(value / max) * 100}%`, background: color }}></div>
              </div>
              <div className="bd-row__value">{value}<span className="u"> MW</span></div>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

// ============================================================
// LOGOS STRIP
// ============================================================
function LogosStrip({ title, logos }) {
  return (
    <section className="section">
      <div className="container">
        <div className="mono-label" style={{ marginBottom: 24 }}>↻ {title.toUpperCase()}</div>
        <div className="logos-strip">
          {logos.map((l, i) => <div className="logo-cell" key={i}>{l.toUpperCase()}</div>)}
        </div>
      </div>
    </section>
  );
}

// ============================================================
// BENEFITS (Carreras)
// ============================================================
function Benefits({ copy }) {
  return (
    <section className="section" style={{ background: "var(--ink-50)" }}>
      <div className="container">
        <SectionHead2 overline={copy.overline} title={copy.title} />
        <div className="benefits">
          {copy.items.map((b, i) => (
            <div className="benefit" key={i}>
              <div className="benefit__num">0{i + 1} / 0{copy.items.length}</div>
              <div className="benefit__title">{b.title}</div>
              <div className="benefit__body">{b.body}</div>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

// ============================================================
// PROCESS STEPS
// ============================================================
function Process({ copy }) {
  return (
    <section className="section">
      <div className="container">
        <SectionHead2 overline={copy.overline} title={copy.title} />
        <div className="process">
          {copy.steps.map((s, i) => (
            <div className="process-step" key={i}>
              <div className="process-step__num">{s.num}</div>
              <div className="process-step__title">{s.title}</div>
              <div className="process-step__sub">{s.sub}</div>
              <div className="process-step__time">↻ {s.time}</div>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

// ============================================================
// TESTIMONIO
// ============================================================
function Testimonio({ quote, author }) {
  return (
    <section className="testimonio">
      <div className="container">
        <p className="testimonio__quote">"{quote}"</p>
        <div className="testimonio__author">— {author}</div>
      </div>
    </section>
  );
}

// ============================================================
// FEATURED INSIGHT
// ============================================================
function FeaturedInsight({ copy, lang }) {
  return (
    <article className="featured-insight">
      <div className="featured-insight__inner">
        <div>
          <div className="insight__meta">
            <span className="insight__tag">{copy.tag}</span>
            <span className="insight__date">{copy.date}</span>
            <span className="featured-insight__readtime">↻ {copy.readtime}</span>
          </div>
          <h3 className="featured-insight__title">{copy.title}</h3>
          <p className="featured-insight__body">{copy.body}</p>
          <div className="featured-insight__author">{copy.author}</div>
        </div>
        <div>
          <a className="btn btn--primary btn--lg" href="#">
            <span>{lang === "es" ? "Leer artículo completo" : "Read full article"}</span>
            <span className="arr"></span>
          </a>
        </div>
      </div>
    </article>
  );
}

// ============================================================
// INSIGHTS GRID (filterable)
// ============================================================
function InsightsGrid({ items, filters }) {
  const [filter, setFilter] = useS2(filters[0]);
  const allLabel = filters[0]; // "Todos" / "All"
  const visible = filter === allLabel ? items : items.filter((it) => it.tag === filter);
  return (
    <>
      <div className="insight-filters">
        {filters.map((f) => (
          <button key={f} className={`insight-filter ${filter === f ? "is-active" : ""}`} onClick={() => setFilter(f)}>
            {f}
          </button>
        ))}
      </div>
      <div className="blog">
        {visible.map((p, i) => (
          <article className="insight" key={i}>
            <div className="insight__meta">
              <span className="insight__tag">{p.tag}</span>
              <span className="insight__date">{p.date}</span>
            </div>
            <h3 className="insight__title">{p.title}</h3>
            <p className="insight__body">{p.body}</p>
            <span className="insight__link">Leer nota →</span>
          </article>
        ))}
      </div>
    </>
  );
}

// ============================================================
// CONTACT FORM
// ============================================================
function ContactForm({ copy, lang }) {
  const [submitted, setSubmitted] = useS2(false);
  const submit = (e) => {
    e.preventDefault();
    setSubmitted(true);
  };
  if (submitted) {
    return (
      <div className="form-card">
        <div className="form-success is-on">
          <div className="form-success__icon">
            <svg width="28" height="28" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
              <polyline points="20 6 9 17 4 12" />
            </svg>
          </div>
          <div className="form-success__title">
            <span className="grad">{lang === "es" ? "Recibido" : "Received"}</span>.
          </div>
          <p className="form-success__body">
            {lang === "es"
              ? "Un ingeniero le escribirá en las próximas 24 horas hábiles. La conversación inicial dura 30 minutos."
              : "An engineer will write to you within the next 24 business hours. The initial conversation lasts 30 minutes."}
          </p>
        </div>
      </div>
    );
  }
  return (
    <form className="form-card" onSubmit={submit}>
      <div className="form-card__head">
        <h3 className="form-card__title">{copy.form_title}</h3>
        <div className="form-card__meta">{copy.form_meta}</div>
      </div>
      <div className="form-grid">
        <div className="field">
          <label>{copy.fields.name}</label>
          <input type="text" required defaultValue="" placeholder="—" />
        </div>
        <div className="field">
          <label>{copy.fields.email}</label>
          <input type="email" required placeholder="—" />
        </div>
        <div className="field">
          <label>{copy.fields.company}</label>
          <input type="text" required placeholder="—" />
        </div>
        <div className="field">
          <label>{copy.fields.role}</label>
          <input type="text" placeholder="—" />
        </div>
        <div className="field">
          <label>{copy.fields.country}</label>
          <select defaultValue="">
            <option value="" disabled>—</option>
            {copy.countries.map((c, i) => <option key={i} value={c}>{c}</option>)}
          </select>
        </div>
        <div className="field">
          <label>{copy.fields.assets}</label>
          <select defaultValue="">
            <option value="" disabled>—</option>
            {copy.asset_options.map((c, i) => <option key={i} value={c}>{c}</option>)}
          </select>
        </div>
        <div className="field full">
          <label>{copy.fields.message}</label>
          <textarea rows="4" placeholder="—"></textarea>
        </div>
      </div>
      <div className="form-submit">
        <div className="mono-meta">↻ {lang === "es" ? "RESPUESTA 24H HÁBILES" : "24H BUSINESS RESPONSE"}</div>
        <button className="btn btn--primary btn--lg" type="submit">
          <span>{copy.fields.submit}</span>
          <span className="arr"></span>
        </button>
      </div>
    </form>
  );
}

// ============================================================
// OFFICES
// ============================================================
function Offices({ copy }) {
  return (
    <section className="section" style={{ background: "var(--ink-50)" }}>
      <div className="container">
        <SectionHead2 overline={copy.overline} title={copy.title} />
        <div className="offices">
          {copy.items.map((o, i) => (
            <div className="office" key={i}>
              <div className="office__country">↻ {o.country}</div>
              <div className="office__city">{o.city}</div>
              <div className="office__tz">{o.tz}</div>
              <div className="office__address">{o.address}</div>
              <div className="office__role">{o.role}</div>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

// ============================================================
// ALT CHANNELS (Contacto side)
// ============================================================
function AltChannels({ copy }) {
  return (
    <div>
      <div className="mono-label" style={{ marginBottom: 16 }}>{copy.overline}</div>
      <div className="alt-list">
        {copy.items.map((row, i) => (
          <div className="alt-row" key={i}>
            <span className="l">{row.label}</span>
            <span className="v">{row.value}</span>
          </div>
        ))}
      </div>
    </div>
  );
}

// ============================================================
// INVESTORS STRIP
// ============================================================
function Investors({ copy }) {
  return (
    <section className="section" style={{ background: "var(--ink-50)" }}>
      <div className="container">
        <SectionHead2 overline={copy.overline} title={copy.title} />
        <div className="investors">
          {copy.items.map((i, idx) => <div className="investor-cell" key={idx}>{i.toUpperCase()}</div>)}
        </div>
      </div>
    </section>
  );
}

// ============================================================
// ADVISORS (uses persona style)
// ============================================================
function Advisors({ copy }) {
  return (
    <section className="section">
      <div className="container">
        <SectionHead2 overline={copy.overline} title={copy.title} />
        <div className="advisors">
          {copy.people.map(([name, role, loc], i) => {
            const initials = name.split(" ").map(n => n[0]).join("").slice(0, 2);
            return (
              <div className="persona" key={i}>
                <div className="persona__photo">
                  <div className="persona__initials">{initials}</div>
                </div>
                <div className="persona__body">
                  <div className="persona__name">{name}</div>
                  <div className="persona__role">{role}</div>
                  <div className="persona__loc">↻ {loc}</div>
                </div>
              </div>
            );
          })}
        </div>
      </div>
    </section>
  );
}

Object.assign(window, {
  HomeTiles, HomeManifest, Architecture, Specs, ApiSection, CoverageMap,
  RegFrameworks, Timeline, CasosAll, Breakdown, LogosStrip, Benefits,
  Process, Testimonio, FeaturedInsight, InsightsGrid, ContactForm,
  Offices, AltChannels, Investors, Advisors,
});
