// ── BiviaSystemMockup — Replaces bivia-pillars in the bivia-grid right column
function BiviaSystemMockup() {
  const wrapperRef = useRef(null);
  const bgRef      = useRef(null);
  const fgRef      = useRef(null);

  const [isMobile, setIsMobile] = useState(() =>
    typeof window !== 'undefined' && window.innerWidth <= 768
  );

  useEffect(() => {
    const check = () => setIsMobile(window.innerWidth <= 768);
    window.addEventListener('resize', check);
    return () => window.removeEventListener('resize', check);
  }, []);

  useEffect(() => {
    const bg      = bgRef.current;
    const fg      = fgRef.current;
    const wrapper = wrapperRef.current;
    if (!bg || !fg || !wrapper) return;

    // Fade in on scroll into view
    gsap.fromTo(wrapper,
      { opacity: 0, y: 28 },
      { opacity: 1, y: 0, duration: 0.9, ease: 'power3.out',
        scrollTrigger: { trigger: wrapper, start: 'top 88%', toggleActions: 'play none none none' } }
    );

    // Parallax: background slides up, foreground slides down
    gsap.to(bg, {
      y: -30, ease: 'none',
      scrollTrigger: { trigger: wrapper, start: 'top bottom', end: 'bottom top', scrub: 1.2 }
    });
    gsap.to(fg, {
      y: 26, ease: 'none',
      scrollTrigger: { trigger: wrapper, start: 'top bottom', end: 'bottom top', scrub: 1.2 }
    });
  }, []);

  const h = isMobile ? 360 : 490;

  return (
    <div ref={wrapperRef} style={{ position: 'relative', width: '100%', height: h + 'px', opacity: 0 }}>

      {/* ── Background card — dark, system-flow diagram ──────────────── */}
      <div ref={bgRef} style={{
        position: 'absolute',
        top: '22px',
        right: isMobile ? '0' : '-5%',
        width: isMobile ? '85%' : '82%',
        height: '60%',
        background: 'rgba(7,8,10,0.94)',
        borderRadius: '20px',
        border: '1px solid rgba(200,184,149,0.07)',
        overflow: 'hidden',
        padding: '18px 16px',
      }}>
        <div style={{
          fontSize: '8px', letterSpacing: '0.18em', textTransform: 'uppercase',
          color: 'rgba(200,184,149,0.4)', marginBottom: '14px', fontFamily: 'Sora,sans-serif',
        }}>
          Ecosistema digital
        </div>

        <svg viewBox="0 0 220 130" fill="none" style={{ width: '100%' }} xmlns="http://www.w3.org/2000/svg">
          {/* Horizontal connectors */}
          <line x1="54" y1="40" x2="86" y2="40" stroke="rgba(200,184,149,0.2)" strokeWidth="1" strokeDasharray="3 3"/>
          <line x1="134" y1="40" x2="166" y2="40" stroke="rgba(200,184,149,0.2)" strokeWidth="1" strokeDasharray="3 3"/>
          {/* Vertical drops */}
          <line x1="30"  y1="56" x2="30"  y2="88" stroke="rgba(200,184,149,0.1)" strokeWidth="1"/>
          <line x1="110" y1="56" x2="110" y2="88" stroke="rgba(200,184,149,0.1)" strokeWidth="1"/>
          <line x1="190" y1="56" x2="190" y2="88" stroke="rgba(200,184,149,0.1)" strokeWidth="1"/>

          {/* Phase 01 */}
          <rect x="6"   y="24" width="48" height="32" rx="7" fill="rgba(200,184,149,0.04)" stroke="rgba(200,184,149,0.2)" strokeWidth="1"/>
          <text x="30"  y="37" textAnchor="middle" fill="#C8B895" fontSize="7.5" fontFamily="Sora,sans-serif" fontWeight="600">01</text>
          <text x="30"  y="49" textAnchor="middle" fill="rgba(200,184,149,0.5)" fontSize="5.5" fontFamily="Sora,sans-serif">DIAGNÓST.</text>

          {/* Phase 02 */}
          <rect x="86"  y="24" width="48" height="32" rx="7" fill="rgba(200,184,149,0.04)" stroke="rgba(200,184,149,0.2)" strokeWidth="1"/>
          <text x="110" y="37" textAnchor="middle" fill="#C8B895" fontSize="7.5" fontFamily="Sora,sans-serif" fontWeight="600">02</text>
          <text x="110" y="49" textAnchor="middle" fill="rgba(200,184,149,0.5)" fontSize="5.5" fontFamily="Sora,sans-serif">ARQUIT.</text>

          {/* Phase 03 — highlighted */}
          <rect x="166" y="24" width="48" height="32" rx="7" fill="rgba(200,184,149,0.09)" stroke="rgba(200,184,149,0.38)" strokeWidth="1"/>
          <text x="190" y="37" textAnchor="middle" fill="#C8B895" fontSize="7.5" fontFamily="Sora,sans-serif" fontWeight="600">03</text>
          <text x="190" y="49" textAnchor="middle" fill="rgba(200,184,149,0.55)" fontSize="5.5" fontFamily="Sora,sans-serif">EJEC.</text>

          {/* Output nodes */}
          <rect x="12"  y="88" width="36" height="18" rx="4" fill="rgba(200,184,149,0.03)" stroke="rgba(200,184,149,0.1)" strokeWidth="1"/>
          <text x="30"  y="100" textAnchor="middle" fill="rgba(200,184,149,0.38)" fontSize="5.5" fontFamily="Sora,sans-serif">Roadmap</text>

          <rect x="92"  y="88" width="36" height="18" rx="4" fill="rgba(200,184,149,0.03)" stroke="rgba(200,184,149,0.1)" strokeWidth="1"/>
          <text x="110" y="100" textAnchor="middle" fill="rgba(200,184,149,0.38)" fontSize="5.5" fontFamily="Sora,sans-serif">Sistema</text>

          <rect x="172" y="88" width="36" height="18" rx="4" fill="rgba(200,184,149,0.06)" stroke="rgba(200,184,149,0.2)" strokeWidth="1"/>
          <text x="190" y="100" textAnchor="middle" fill="rgba(200,184,149,0.6)" fontSize="5.5" fontFamily="Sora,sans-serif">✓ Activo</text>
        </svg>
      </div>

      {/* ── Foreground card — glassy dashboard ───────────────────────── */}
      <div ref={fgRef} style={{
        position: 'absolute',
        bottom: 0,
        left: 0,
        width: isMobile ? '85%' : '82%',
        height: '73%',
        background: 'rgba(255,255,255,0.025)',
        borderRadius: '20px',
        backdropFilter: 'blur(14px)',
        WebkitBackdropFilter: 'blur(14px)',
        border: '1px solid rgba(200,184,149,0.16)',
        overflow: 'hidden',
        padding: '20px 18px',
      }}>
        {/* Header */}
        <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: '12px' }}>
          <span style={{
            fontSize: '10px', fontFamily: 'Sora,sans-serif', fontWeight: 700,
            letterSpacing: '0.14em', color: '#C8B895', textTransform: 'uppercase',
          }}>Sistema activo</span>
          <span style={{ fontSize: '9px', color: '#6B9E6B', fontWeight: 500 }}>● En línea</span>
        </div>
        <div style={{ height: '1px', background: 'rgba(200,184,149,0.1)', marginBottom: '12px' }}/>

        {/* Integration rows */}
        {['CRM integrado', 'Pipelines de datos', 'Flujos automáticos', 'Dashboard BI'].map((item, i, arr) => (
          <div key={i} style={{
            display: 'flex', justifyContent: 'space-between', alignItems: 'center',
            padding: '7px 0',
            borderBottom: i < arr.length - 1 ? '1px solid rgba(200,184,149,0.06)' : 'none',
            fontSize: '11px',
          }}>
            <span style={{ color: 'var(--stone)' }}>{item}</span>
            <span style={{ color: '#6B9E6B', fontSize: '9.5px' }}>● activo</span>
          </div>
        ))}

        {/* Uptime bar */}
        <div style={{ marginTop: '14px' }}>
          <div style={{ display: 'flex', justifyContent: 'space-between', fontSize: '9.5px', marginBottom: '5px' }}>
            <span style={{ color: 'var(--stone)', letterSpacing: '0.1em', textTransform: 'uppercase' }}>Uptime</span>
            <span style={{ color: '#C8B895', fontWeight: 600 }}>99.8%</span>
          </div>
          <div style={{ height: '2px', background: 'rgba(200,184,149,0.1)', borderRadius: '2px' }}>
            <div style={{ width: '99.8%', height: '100%', background: 'linear-gradient(90deg, #6B5C35, #C8B895)', borderRadius: '2px' }}/>
          </div>
        </div>
      </div>

    </div>
  );
}
