// Animated / imagery card visuals for homepage sections
// Uses GSAP where available, falls back to CSS where not.

// Industry card background — abstract layered geometry per-sector
function IndustryBg({ kind, dark }) {
  const base = dark ? 'rgba(244,243,241,0.08)' : 'rgba(11,11,14,0.06)';
  const accent = 'var(--crimson)';
  if (kind === 'fintech') {
    // Stacked bars/ledger lines
    return (
      <svg viewBox="0 0 200 260" preserveAspectRatio="xMidYMid slice" style={svgBgStyle} aria-hidden>
        <g opacity="0.9">
          {[...Array(14)].map((_, i) => (
            <rect key={i} x={20 + (i%7)*22} y={40 + Math.floor(i/7)*100} width="14" height={40 + (i*11)%60} fill={i===2?accent:base}/>
          ))}
        </g>
        <line x1="0" y1="230" x2="200" y2="230" stroke={base} strokeWidth="1" strokeDasharray="3 4"/>
        <circle cx="164" cy="36" r="16" fill="none" stroke={accent} strokeWidth="1.5"/>
        <circle cx="164" cy="36" r="6" fill={accent}/>
      </svg>
    );
  }
  if (kind === 'saude') {
    // Heart-rate waveform + concentric rings
    return (
      <svg viewBox="0 0 200 260" preserveAspectRatio="xMidYMid slice" style={svgBgStyle} aria-hidden>
        <circle cx="40" cy="60" r="60" fill="none" stroke={base} strokeWidth="1"/>
        <circle cx="40" cy="60" r="36" fill="none" stroke={base} strokeWidth="1"/>
        <circle cx="40" cy="60" r="16" fill={accent} opacity="0.25"/>
        <path d="M10 190 L40 190 L55 170 L70 210 L90 150 L110 220 L130 190 L200 190"
              fill="none" stroke={accent} strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"/>
      </svg>
    );
  }
  if (kind === 'varejo') {
    // Grid of squares (inventory / SKUs)
    return (
      <svg viewBox="0 0 200 260" preserveAspectRatio="xMidYMid slice" style={svgBgStyle} aria-hidden>
        {[...Array(5)].map((_, r) => (
          [...Array(4)].map((__, c) => {
            const on = (r*4+c) % 7 === 3 || (r===2&&c===2);
            return <rect key={`${r}-${c}`} x={24 + c*40} y={30 + r*44} width="28" height="28" rx="2"
                         fill={on ? accent : 'none'} stroke={on?'none':base} strokeWidth="1" opacity={on?0.9:1}/>;
          })
        ))}
      </svg>
    );
  }
  // energia — grid lines + bolt
  return (
    <svg viewBox="0 0 200 260" preserveAspectRatio="xMidYMid slice" style={svgBgStyle} aria-hidden>
      <g stroke={base} strokeWidth="1">
        {[0,1,2,3,4,5].map(i => <line key={`v${i}`} x1={i*40} y1="0" x2={i*40} y2="260"/>)}
        {[0,1,2,3,4,5,6].map(i => <line key={`h${i}`} x1="0" y1={i*40} x2="200" y2={i*40}/>)}
      </g>
      <path d="M110 40 L70 140 L100 140 L80 220 L140 110 L110 110 Z"
            fill={accent}/>
    </svg>
  );
}

const svgBgStyle = {
  position: 'absolute',
  inset: 0,
  width: '100%',
  height: '100%',
  opacity: 0.85,
  pointerEvents: 'none'
};

// ========================================================
// Case visuals (homepage "O que sai da bigorna") — animated
// ========================================================
function CaseVisual({ kind }) {
  // Kind: 'fintech' | 'saude' | 'varejo'
  if (kind === 'fintech') return <CaseFintech />;
  if (kind === 'saude')   return <CaseSaude />;
  return <CaseVarejo />;
}

// 1) Fintech — 42 services migrating, orbiting nodes around a core
function CaseFintech() {
  const ref = React.useRef(null);
  React.useEffect(() => {
    if (!window.gsap || !ref.current) return;
    const ctx = gsap.context(() => {
      gsap.to('.cf-orbit', { rotate: 360, transformOrigin: '50% 50%', duration: 22, repeat: -1, ease: 'none' });
      gsap.to('.cf-orbit-b', { rotate: -360, transformOrigin: '50% 50%', duration: 30, repeat: -1, ease: 'none' });
      gsap.to('.cf-node', { scale: 1.4, duration: 1.2, repeat: -1, yoyo: true, ease: 'sine.inOut', stagger: { each: 0.08, from: 'random' } });
      gsap.fromTo('.cf-pulse', { opacity: 0.6, scale: 0.3 }, { opacity: 0, scale: 1, duration: 2.4, repeat: -1, ease: 'power2.out', stagger: 0.8 });
    }, ref);
    return () => ctx.revert();
  }, []);
  return (
    <div ref={ref} style={caseStage}>
      <svg viewBox="-100 -100 200 200" style={svgFill} aria-hidden>
        {/* Pulse rings */}
        {[0,1,2].map(i => (
          <circle key={i} className="cf-pulse" cx="0" cy="0" r="30" fill="none" stroke="var(--crimson)" strokeWidth="1.5"/>
        ))}
        {/* Inner orbit */}
        <g className="cf-orbit">
          <circle cx="0" cy="0" r="42" fill="none" stroke="rgba(244,243,241,0.12)" strokeWidth="1" strokeDasharray="2 4"/>
          {[0,1,2,3,4,5].map(i => {
            const a = (i / 6) * Math.PI * 2;
            return <circle key={i} className="cf-node" cx={Math.cos(a)*42} cy={Math.sin(a)*42} r="3.2" fill="var(--crimson)"/>;
          })}
        </g>
        {/* Outer orbit */}
        <g className="cf-orbit-b">
          <circle cx="0" cy="0" r="72" fill="none" stroke="rgba(244,243,241,0.10)" strokeWidth="1"/>
          {[0,1,2,3,4,5,6,7].map(i => {
            const a = (i / 8) * Math.PI * 2 + 0.2;
            return <circle key={i} className="cf-node" cx={Math.cos(a)*72} cy={Math.sin(a)*72} r="2.4" fill="rgba(244,243,241,0.8)"/>;
          })}
        </g>
        {/* Core */}
        <circle cx="0" cy="0" r="14" fill="var(--crimson)"/>
        <circle cx="0" cy="0" r="22" fill="none" stroke="var(--crimson)" strokeWidth="1" opacity="0.5"/>
      </svg>
      <div style={caseLabel}>42 serviços · 0 downtime</div>
    </div>
  );
}

// 2) Saúde — EKG waveform + listening pulse
function CaseSaude() {
  const ref = React.useRef(null);
  React.useEffect(() => {
    if (!window.gsap || !ref.current) return;
    const ctx = gsap.context(() => {
      const path = ref.current.querySelector('.cs-wave');
      if (path) {
        const len = path.getTotalLength();
        gsap.set(path, { strokeDasharray: len, strokeDashoffset: len });
        gsap.to(path, { strokeDashoffset: 0, duration: 3.2, repeat: -1, ease: 'none' });
      }
      gsap.to('.cs-bar', {
        scaleY: () => 0.3 + Math.random() * 1.8,
        transformOrigin: '50% 50%',
        duration: 0.5, repeat: -1, yoyo: true, ease: 'sine.inOut',
        stagger: { each: 0.06, from: 'center' }
      });
      gsap.to('.cs-ring', { scale: 1.15, opacity: 0.2, duration: 1.6, repeat: -1, yoyo: true, ease: 'sine.inOut', transformOrigin: '50% 50%' });
    }, ref);
    return () => ctx.revert();
  }, []);
  return (
    <div ref={ref} style={caseStage}>
      <svg viewBox="0 0 200 200" style={svgFill} aria-hidden>
        {/* Listening rings (top-left) */}
        <g transform="translate(42 56)">
          <circle className="cs-ring" r="18" fill="none" stroke="var(--crimson)" strokeWidth="1" opacity="0.5"/>
          <circle className="cs-ring" r="12" fill="none" stroke="var(--crimson)" strokeWidth="1.2" opacity="0.7"/>
          <circle r="6" fill="var(--crimson)"/>
        </g>
        {/* EKG trace */}
        <path className="cs-wave"
              d="M10 120 L50 120 L62 98 L76 142 L94 78 L112 150 L130 120 L190 120"
              fill="none" stroke="var(--crimson)" strokeWidth="2.2" strokeLinecap="round" strokeLinejoin="round"/>
        {/* Spectrum bars (bottom) */}
        <g transform="translate(0 170)">
          {[...Array(16)].map((_, i) => (
            <rect key={i} className="cs-bar" x={14 + i*11} y={-14} width="4" height="14" fill="rgba(244,243,241,0.7)"/>
          ))}
        </g>
      </svg>
      <div style={caseLabel}>120 clínicas · triagem ao vivo</div>
    </div>
  );
}

// 3) Varejo — checkout funnel / speed bars + shrinking abandonment
function CaseVarejo() {
  const ref = React.useRef(null);
  React.useEffect(() => {
    if (!window.gsap || !ref.current) return;
    const ctx = gsap.context(() => {
      gsap.fromTo('.cv-progress', { scaleX: 0 }, {
        scaleX: 1, duration: 2.2, ease: 'power3.inOut', repeat: -1, yoyo: true, transformOrigin: '0 50%'
      });
      gsap.fromTo('.cv-drop', { y: -30, opacity: 0 }, {
        y: 120, opacity: 1, duration: 2.4, repeat: -1, ease: 'power2.in',
        stagger: { each: 0.35, repeat: -1 }
      });
      gsap.to('.cv-num', {
        innerText: 22, duration: 2, ease: 'power2.out', snap: { innerText: 1 }, repeat: -1, yoyo: true
      });
    }, ref);
    return () => ctx.revert();
  }, []);
  return (
    <div ref={ref} style={caseStage}>
      <svg viewBox="0 0 200 200" style={svgFill} aria-hidden>
        {/* Funnel shape */}
        <path d="M20 40 L180 40 L130 110 L130 170 L70 170 L70 110 Z"
              fill="none" stroke="rgba(244,243,241,0.2)" strokeWidth="1.5"/>
        {/* Dropping items */}
        {[0,1,2,3,4].map(i => (
          <rect key={i} className="cv-drop" x={50 + i*22} y="20" width="8" height="8" fill="var(--crimson)" rx="1"/>
        ))}
        {/* Progress bars (3 tracks, "3× faster") */}
        <g transform="translate(20 148)">
          {[0,1,2].map(i => (
            <g key={i} transform={`translate(0 ${i*10})`}>
              <rect x="0" y="0" width="160" height="4" fill="rgba(244,243,241,0.1)" rx="2"/>
              <rect className="cv-progress" x="0" y="0" width="160" height="4" fill="var(--crimson)" rx="2"
                    style={{ transformOrigin: '0 50%' }}/>
            </g>
          ))}
        </g>
      </svg>
      <div style={caseLabel}>checkout 3× · −22% abandono</div>
    </div>
  );
}

const caseStage = {
  position: 'absolute', inset: 0,
  display: 'grid', placeItems: 'center',
  overflow: 'hidden'
};
const svgFill = { width: '82%', height: '82%', overflow: 'visible' };
const caseLabel = {
  position: 'absolute', bottom: 16, left: 18,
  fontFamily: 'var(--mono)', fontSize: 10, letterSpacing: '0.12em',
  textTransform: 'uppercase', opacity: 0.55
};

// ========================================================
// Insights card visuals — three distinct "thumbnails" (by kind)
// ========================================================
function InsightVisual({ kind }) {
  if (kind === 'ensaio')     return <InsightEssay />;
  if (kind === 'pesquisa')   return <InsightResearch />;
  return <InsightInterview />;
}

// ========================================================
// Insights card visuals by CATEGORY — one per theme
// ========================================================
function InsightVisualByCat({ cat }) {
  if (cat === 'Arquitetura') return <InsightCatArquitetura />;
  if (cat === 'IA')          return <InsightCatIA />;
  if (cat === 'Design')      return <InsightCatDesign />;
  if (cat === 'Fintech')     return <InsightCatFintech />;
  if (cat === 'Cloud')       return <InsightCatCloud />;
  if (cat === 'Saúde')       return <InsightCatSaude />;
  return <InsightEssay />;
}

// Arquitetura — layered stack blocks + animated data flow arrows
function InsightCatArquitetura() {
  const ref = React.useRef(null);
  React.useEffect(() => {
    if (!window.gsap || !ref.current) return;
    const ctx = gsap.context(() => {
      gsap.to('.ia-arrow', { y: 12, opacity: 0, duration: 1.1, repeat: -1, ease: 'power2.in', stagger: 0.22 });
      gsap.to('.ia-layer', { boxShadow: '0 0 0 1px var(--crimson)', duration: 1.4, repeat: -1, yoyo: true, ease: 'sine.inOut', stagger: 0.3 });
      gsap.fromTo('.ia-dot', { x: 0 }, { x: 160, duration: 2.2, repeat: -1, ease: 'none', stagger: 0.55 });
    }, ref);
    return () => ctx.revert();
  }, []);
  const layers = ['UI / Frontend', 'API Gateway', 'Serviços', 'Banco de Dados'];
  const colors = ['var(--crimson)', 'rgba(244,243,241,0.55)', 'rgba(244,243,241,0.35)', 'rgba(244,243,241,0.2)'];
  return (
    <div ref={ref} style={insightStage}>
      <svg viewBox="0 0 240 160" preserveAspectRatio="xMidYMid slice" style={{ ...svgFill, overflow: 'visible' }} aria-hidden>
        {layers.map((l, i) => (
          <g key={l}>
            <rect x="20" y={18 + i * 32} width="200" height="20" rx="4" fill={colors[i]} opacity={0.85}/>
            <text x="30" y={32 + i * 32} fill="rgba(11,11,14,0.85)" fontSize="8" fontFamily="monospace">{l}</text>
            {i < 3 && (
              <>
                <line x1="120" y1={38 + i * 32} x2="120" y2={48 + i * 32} stroke="rgba(244,243,241,0.3)" strokeWidth="1" strokeDasharray="2 2"/>
                <polygon className="ia-arrow" points="116,46 120,52 124,46" fill="var(--crimson)" opacity="0.9"/>
              </>
            )}
          </g>
        ))}
        <circle className="ia-dot" cx="20" cy="8" r="3" fill="var(--crimson)" opacity="0.8"/>
        <circle className="ia-dot" cx="20" cy="8" r="3" fill="rgba(244,243,241,0.6)" opacity="0.6"/>
        <line x1="20" y1="8" x2="220" y2="8" stroke="rgba(244,243,241,0.15)" strokeWidth="1"/>
      </svg>
    </div>
  );
}

// IA — neural network nodes + pulsing connections
function InsightCatIA() {
  const ref = React.useRef(null);
  React.useEffect(() => {
    if (!window.gsap || !ref.current) return;
    const ctx = gsap.context(() => {
      gsap.to('.ii-node', { scale: 1.5, duration: () => 0.8 + Math.random(), repeat: -1, yoyo: true, ease: 'sine.inOut', transformOrigin: '50% 50%', stagger: { each: 0.15, from: 'random' } });
      gsap.to('.ii-edge', { opacity: () => 0.1 + Math.random() * 0.7, duration: () => 0.6 + Math.random() * 0.8, repeat: -1, yoyo: true, ease: 'sine.inOut', stagger: { each: 0.1, from: 'random' } });
      gsap.fromTo('.ii-signal', { strokeDashoffset: 80 }, { strokeDashoffset: 0, duration: 1.8, repeat: -1, ease: 'none', stagger: 0.4 });
    }, ref);
    return () => ctx.revert();
  }, []);
  const layers = [[30], [70, 110], [30, 70, 110, 150], [70, 110], [90]];
  const ys = [20, 55, 90, 125, 155];
  const nodes = layers.flatMap((xs, li) => xs.map(x => ({ x, y: ys[li], li })));
  const edges = [];
  for (let li = 0; li < layers.length - 1; li++) {
    layers[li].forEach(x1 => layers[li + 1].forEach(x2 => edges.push({ x1, y1: ys[li], x2, y2: ys[li + 1] })));
  }
  return (
    <div ref={ref} style={insightStage}>
      <svg viewBox="0 0 180 175" preserveAspectRatio="xMidYMid meet" style={{ width: '80%', height: '80%' }} aria-hidden>
        {edges.map((e, i) => (
          <line key={i} className="ii-edge" x1={e.x1} y1={e.y1} x2={e.x2} y2={e.y2} stroke="rgba(244,243,241,0.25)" strokeWidth="1"/>
        ))}
        {edges.slice(0, 4).map((e, i) => (
          <line key={`s${i}`} className="ii-signal" x1={e.x1} y1={e.y1} x2={e.x2} y2={e.y2}
                stroke="var(--crimson)" strokeWidth="1.5" strokeDasharray="8 72" opacity="0.9"/>
        ))}
        {nodes.map((n, i) => (
          <circle key={i} className="ii-node" cx={n.x} cy={n.y} r={n.li === 2 ? 7 : 5}
                  fill={n.li === 2 ? 'var(--crimson)' : 'rgba(244,243,241,0.75)'}/>
        ))}
      </svg>
    </div>
  );
}

// Design — animated wireframe grid + color palette
function InsightCatDesign() {
  const ref = React.useRef(null);
  React.useEffect(() => {
    if (!window.gsap || !ref.current) return;
    const ctx = gsap.context(() => {
      gsap.to('.id-cursor', { x: 28, y: 18, duration: 2.4, repeat: -1, yoyo: true, ease: 'power2.inOut' });
      gsap.to('.id-swatch', { scale: 1.2, duration: 0.8, repeat: -1, yoyo: true, ease: 'sine.inOut', transformOrigin: '50% 50%', stagger: 0.18 });
      gsap.fromTo('.id-block', { opacity: 0.2 }, { opacity: 0.9, duration: 1.2, repeat: -1, yoyo: true, ease: 'power2.inOut', stagger: { amount: 1.5, from: 'start' } });
    }, ref);
    return () => ctx.revert();
  }, []);
  return (
    <div ref={ref} style={insightStage}>
      <svg viewBox="0 0 240 160" preserveAspectRatio="xMidYMid slice" style={svgFill} aria-hidden>
        {/* Wireframe layout */}
        <rect x="16" y="16" width="130" height="16" rx="3" fill="rgba(244,243,241,0.12)" stroke="rgba(244,243,241,0.2)" strokeWidth="0.8"/>
        <rect x="16" y="40" width="80" height="90" rx="3" fill="rgba(244,243,241,0.06)" stroke="rgba(244,243,241,0.18)" strokeWidth="0.8"/>
        <rect x="104" y="40" width="42" height="42" rx="3" fill="rgba(244,243,241,0.06)" stroke="rgba(244,243,241,0.18)" strokeWidth="0.8"/>
        {[0,1,2,3].map(i => <rect key={i} className="id-block" x="104" y={90 + i * 11} width={20 + i * 5} height="7" rx="2" fill="rgba(244,243,241,0.25)"/>)}
        {/* Color palette */}
        {['var(--crimson)', '#B01128', 'rgba(244,243,241,0.8)', 'rgba(244,243,241,0.4)', '#2A2A30'].map((c, i) => (
          <circle key={i} className="id-swatch" cx={162 + i * 14} cy={24} r="6" fill={c}/>
        ))}
        {/* Cursor */}
        <g className="id-cursor">
          <polygon points="32,108 36,120 38,115 43,118 40,110 45,108" fill="rgba(244,243,241,0.9)" stroke="rgba(11,11,14,0.5)" strokeWidth="0.5"/>
        </g>
        {/* Grid dots */}
        {[0,1,2,3,4].map(r => [0,1,2,3,4,5].map(c => (
          <circle key={`${r}-${c}`} cx={164 + c * 12} cy={50 + r * 18} r="1.5" fill="rgba(244,243,241,0.2)"/>
        )))}
      </svg>
    </div>
  );
}

// Fintech — candlestick chart with animated price action
function InsightCatFintech() {
  const ref = React.useRef(null);
  React.useEffect(() => {
    if (!window.gsap || !ref.current) return;
    const ctx = gsap.context(() => {
      gsap.fromTo('.if-candle', { scaleY: 0.4 }, { scaleY: 1, duration: 1.4, repeat: -1, yoyo: true, ease: 'power2.inOut', transformOrigin: '50% 100%', stagger: { each: 0.12, from: 'random' } });
      gsap.to('.if-line', { strokeDashoffset: -200, duration: 4, repeat: -1, ease: 'none' });
      gsap.to('.if-price', { y: -6, duration: 1.8, repeat: -1, yoyo: true, ease: 'sine.inOut' });
    }, ref);
    return () => ctx.revert();
  }, []);
  const candles = [
    { x: 24, open: 90, close: 60, high: 55, low: 100, bull: true },
    { x: 48, open: 72, close: 95, high: 65, low: 100, bull: false },
    { x: 72, open: 84, close: 70, high: 65, low: 100, bull: true },
    { x: 96, open: 65, close: 50, high: 42, low: 70, bull: true },
    { x: 120, open: 62, close: 80, high: 55, low: 85, bull: false },
    { x: 144, open: 75, close: 58, high: 50, low: 80, bull: true },
    { x: 168, open: 52, close: 38, high: 30, low: 58, bull: true },
    { x: 192, open: 40, close: 55, high: 32, low: 60, bull: false },
  ];
  return (
    <div ref={ref} style={insightStage}>
      <svg viewBox="0 0 240 140" preserveAspectRatio="xMidYMid slice" style={svgFill} aria-hidden>
        <line x1="12" y1="120" x2="228" y2="120" stroke="rgba(244,243,241,0.15)" strokeWidth="1"/>
        {[30, 60, 90].map(y => <line key={y} x1="12" y1={y} x2="228" y2={y} stroke="rgba(244,243,241,0.07)" strokeWidth="1" strokeDasharray="3 5"/>)}
        {candles.map((c, i) => (
          <g key={i}>
            <line x1={c.x} y1={c.high} x2={c.x} y2={c.low} stroke={c.bull ? 'var(--crimson)' : 'rgba(244,243,241,0.4)'} strokeWidth="1.2"/>
            <rect className="if-candle" x={c.x - 7} y={Math.min(c.open, c.close)} width="14" height={Math.abs(c.open - c.close) || 2}
                  fill={c.bull ? 'var(--crimson)' : 'rgba(244,243,241,0.75)'}/>
          </g>
        ))}
        <polyline className="if-line" points="24,90 48,72 72,84 96,65 120,62 144,75 168,52 192,40"
                  fill="none" stroke="var(--crimson)" strokeWidth="1.5" strokeDasharray="4 3" opacity="0.6"/>
        <g className="if-price">
          <rect x="196" y="30" width="36" height="14" rx="3" fill="var(--crimson)"/>
          <text x="214" y="40" textAnchor="middle" fill="white" fontSize="7" fontFamily="monospace">+12.4%</text>
        </g>
      </svg>
    </div>
  );
}

// Cloud — topology mesh with animated packet flow
function InsightCatCloud() {
  const ref = React.useRef(null);
  React.useEffect(() => {
    if (!window.gsap || !ref.current) return;
    const ctx = gsap.context(() => {
      gsap.to('.ic-node', { scale: 1.3, duration: () => 1 + Math.random(), repeat: -1, yoyo: true, ease: 'sine.inOut', transformOrigin: '50% 50%', stagger: { each: 0.2, from: 'random' } });
      gsap.fromTo('.ic-packet', { strokeDashoffset: 100 }, { strokeDashoffset: 0, duration: 2, repeat: -1, ease: 'none', stagger: 0.35 });
      gsap.to('.ic-ring', { scale: 2, opacity: 0, duration: 2.2, repeat: -1, ease: 'power2.out', transformOrigin: '50% 50%', stagger: 0.7 });
    }, ref);
    return () => ctx.revert();
  }, []);
  const nodes = [
    { x: 120, y: 75, r: 10, main: true },
    { x: 50, y: 30, r: 6 }, { x: 190, y: 30, r: 6 },
    { x: 30, y: 100, r: 6 }, { x: 200, y: 110, r: 6 },
    { x: 80, y: 140, r: 6 }, { x: 160, y: 140, r: 6 },
  ];
  const edges = [[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[1,2],[3,5],[4,6]];
  return (
    <div ref={ref} style={insightStage}>
      <svg viewBox="0 0 240 170" preserveAspectRatio="xMidYMid slice" style={svgFill} aria-hidden>
        {edges.map(([a, b], i) => (
          <line key={i} x1={nodes[a].x} y1={nodes[a].y} x2={nodes[b].x} y2={nodes[b].y}
                stroke="rgba(244,243,241,0.15)" strokeWidth="1"/>
        ))}
        {edges.slice(0, 5).map(([a, b], i) => (
          <line key={`p${i}`} className="ic-packet" x1={nodes[a].x} y1={nodes[a].y} x2={nodes[b].x} y2={nodes[b].y}
                stroke="var(--crimson)" strokeWidth="1.5" strokeDasharray="6 94" opacity="0.9"/>
        ))}
        {nodes.map((n, i) => (
          <g key={i}>
            {n.main && <circle className="ic-ring" cx={n.x} cy={n.y} r={n.r} fill="none" stroke="var(--crimson)" strokeWidth="1" opacity="0.6"/>}
            <circle className="ic-node" cx={n.x} cy={n.y} r={n.r}
                    fill={n.main ? 'var(--crimson)' : 'rgba(244,243,241,0.75)'}/>
          </g>
        ))}
      </svg>
    </div>
  );
}

// Saúde — ECG waveform + animated DNA helix
function InsightCatSaude() {
  const ref = React.useRef(null);
  React.useEffect(() => {
    if (!window.gsap || !ref.current) return;
    const ctx = gsap.context(() => {
      const path = ref.current.querySelector('.is-wave');
      if (path) {
        const len = path.getTotalLength();
        gsap.set(path, { strokeDasharray: len, strokeDashoffset: len });
        gsap.to(path, { strokeDashoffset: 0, duration: 2.8, repeat: -1, ease: 'none' });
      }
      gsap.to('.is-dna-a', { y: 14, duration: 1.2, repeat: -1, yoyo: true, ease: 'sine.inOut', stagger: 0.1 });
      gsap.to('.is-dna-b', { y: -14, duration: 1.2, repeat: -1, yoyo: true, ease: 'sine.inOut', stagger: 0.1 });
      gsap.to('.is-cross', { opacity: 0.4, scale: 0.8, duration: 1.6, repeat: -1, yoyo: true, ease: 'sine.inOut', transformOrigin: '50% 50%' });
    }, ref);
    return () => ctx.revert();
  }, []);
  return (
    <div ref={ref} style={insightStage}>
      <svg viewBox="0 0 240 160" preserveAspectRatio="xMidYMid slice" style={svgFill} aria-hidden>
        {/* ECG */}
        <path className="is-wave"
              d="M10 80 L44 80 L56 50 L70 110 L88 30 L106 120 L120 80 L220 80"
              fill="none" stroke="var(--crimson)" strokeWidth="2.2" strokeLinecap="round" strokeLinejoin="round"/>
        {/* DNA helix */}
        {[0,1,2,3,4,5,6].map(i => (
          <g key={i}>
            <circle className="is-dna-a" cx={148 + i * 12} cy={60 + Math.sin(i * 1.1) * 20} r="4" fill="var(--crimson)" opacity="0.85"/>
            <circle className="is-dna-b" cx={148 + i * 12} cy={100 + Math.sin(i * 1.1 + Math.PI) * 20} r="4" fill="rgba(244,243,241,0.65)"/>
            <line x1={148 + i * 12} y1={60 + Math.sin(i * 1.1) * 20} x2={148 + i * 12} y2={100 + Math.sin(i * 1.1 + Math.PI) * 20}
                  stroke="rgba(244,243,241,0.2)" strokeWidth="1" strokeDasharray="2 2"/>
          </g>
        ))}
        {/* Medical cross */}
        <g className="is-cross">
          <rect x="18" y="110" width="4" height="14" rx="1" fill="var(--crimson)"/>
          <rect x="13" y="115" width="14" height="4" rx="1" fill="var(--crimson)"/>
        </g>
      </svg>
    </div>
  );
}

// Essay — dissolving microservice grid → monolith shape
function InsightEssay() {
  const ref = React.useRef(null);
  React.useEffect(() => {
    if (!window.gsap || !ref.current) return;
    const ctx = gsap.context(() => {
      gsap.to('.ie-cell', {
        opacity: () => 0.2 + Math.random() * 0.8,
        duration: 1.2, repeat: -1, yoyo: true, ease: 'sine.inOut',
        stagger: { amount: 1.6, from: 'random' }
      });
      gsap.to('.ie-line', { x: 40, duration: 4, repeat: -1, ease: 'none' });
    }, ref);
    return () => ctx.revert();
  }, []);
  return (
    <div ref={ref} style={insightStage}>
      <svg viewBox="0 0 240 160" preserveAspectRatio="xMidYMid slice" style={svgFill} aria-hidden>
        <defs>
          <pattern id="ie-p" x="0" y="0" width="40" height="40" patternUnits="userSpaceOnUse">
            <line className="ie-line" x1="-40" y1="0" x2="0" y2="40" stroke="rgba(244,243,241,0.15)" strokeWidth="1"/>
          </pattern>
        </defs>
        <rect width="240" height="160" fill="url(#ie-p)"/>
        {[0,1,2,3,4,5].map(r => (
          [0,1,2,3,4,5,6,7,8].map(c => (
            <rect key={`${r}-${c}`} className="ie-cell"
                  x={8 + c*26} y={8 + r*25} width="18" height="18"
                  fill={(r+c)%4===0 ? 'var(--crimson)' : 'rgba(244,243,241,0.12)'} rx="1"/>
          ))
        ))}
      </svg>
    </div>
  );
}

// Research — stacked chart / bars growing
function InsightResearch() {
  const ref = React.useRef(null);
  React.useEffect(() => {
    if (!window.gsap || !ref.current) return;
    const ctx = gsap.context(() => {
      gsap.fromTo('.ir-bar',
        { scaleY: 0 },
        { scaleY: 1, duration: 1.6, ease: 'power3.out', stagger: 0.08, transformOrigin: '50% 100%',
          repeat: -1, yoyo: true, repeatDelay: 0.8 });
      gsap.to('.ir-dot', { cy: '-=8', duration: 1.4, repeat: -1, yoyo: true, ease: 'sine.inOut', stagger: 0.1 });
    }, ref);
    return () => ctx.revert();
  }, []);
  return (
    <div ref={ref} style={insightStage}>
      <svg viewBox="0 0 240 160" preserveAspectRatio="xMidYMid slice" style={svgFill} aria-hidden>
        {/* Baseline */}
        <line x1="16" y1="136" x2="224" y2="136" stroke="rgba(244,243,241,0.2)" strokeWidth="1"/>
        {/* Bars */}
        {[40, 70, 55, 90, 110, 75, 100, 128, 88].map((h, i) => (
          <rect key={i} className="ir-bar"
                x={24 + i*22} y={136 - h} width="14" height={h}
                fill={i===7 ? 'var(--crimson)' : 'rgba(244,243,241,0.75)'}/>
        ))}
        {/* Trend line dots */}
        <polyline points="31,96 53,66 75,81 97,46 119,26 141,61 163,36 185,8 207,48"
                  fill="none" stroke="var(--crimson)" strokeWidth="1.5" opacity="0.5"/>
        {[31,53,75,97,119,141,163,185,207].map((x, i) => (
          <circle key={i} className="ir-dot" cx={x} cy={[96,66,81,46,26,61,36,8,48][i]} r="2.5" fill="var(--crimson)"/>
        ))}
      </svg>
    </div>
  );
}

// Interview — waveform + two overlapping circles (conversation)
function InsightInterview() {
  const ref = React.useRef(null);
  React.useEffect(() => {
    if (!window.gsap || !ref.current) return;
    const ctx = gsap.context(() => {
      gsap.to('.ii-bar', {
        scaleY: () => 0.2 + Math.random() * 2,
        transformOrigin: '50% 50%',
        duration: 0.4, repeat: -1, yoyo: true, ease: 'sine.inOut',
        stagger: { each: 0.04 }
      });
      gsap.to('.ii-circle-a', { x: 8, duration: 2.6, repeat: -1, yoyo: true, ease: 'sine.inOut' });
      gsap.to('.ii-circle-b', { x: -8, duration: 2.6, repeat: -1, yoyo: true, ease: 'sine.inOut' });
    }, ref);
    return () => ctx.revert();
  }, []);
  return (
    <div ref={ref} style={insightStage}>
      <svg viewBox="0 0 240 160" preserveAspectRatio="xMidYMid slice" style={svgFill} aria-hidden>
        {/* Overlapping circles */}
        <g opacity="0.9">
          <circle className="ii-circle-a" cx="90" cy="80" r="44" fill="none" stroke="var(--crimson)" strokeWidth="1.5"/>
          <circle className="ii-circle-b" cx="150" cy="80" r="44" fill="none" stroke="rgba(244,243,241,0.5)" strokeWidth="1.5"/>
        </g>
        {/* Center waveform */}
        <g transform="translate(120 80)">
          {[...Array(28)].map((_, i) => {
            const x = (i - 14) * 6;
            return <rect key={i} className="ii-bar" x={x - 1.5} y={-14} width="3" height="28"
                         fill={i%4===0 ? 'var(--crimson)' : 'rgba(244,243,241,0.8)'}/>;
          })}
        </g>
      </svg>
    </div>
  );
}

const insightStage = {
  position: 'absolute', inset: 0,
  display: 'grid', placeItems: 'center',
  overflow: 'hidden'
};

Object.assign(window, { IndustryBg, CaseVisual, InsightVisual, InsightVisualByCat });
