/* RINAU HEX — Sections (TopBar, Header, Hero, Features, About, Team, Partners, Contact, Footer) */

const { useState, useEffect, useRef } = React;
const I = window.RHX_ICONS;

/* ============== TOP BAR ============== */
function TopBar() {
  const { lang, setLang, theme, toggleTheme } = window.useApp();
  const t = window.RHX.ui[lang];
  return (
    <div className="hidden md:block bg-[#06090F] text-gray-400 border-b border-white/5">
      <div className="max-w-7xl mx-auto px-6 lg:px-10 flex items-center justify-between h-9 text-[11px] font-mono tracking-wider">
        <div className="flex items-center gap-6">
          <window.AdmButton/>
          <span className="flex items-center gap-2 text-teal-400"><span className="w-1.5 h-1.5 bg-[#14B8A6] rotate-45"></span>// RHX-S.A.S — CIIU 6201</span>
          <span className="flex items-center gap-2"><I.Phone size={11}/>{t.topbar.phone}</span>
          <span className="flex items-center gap-2"><I.Mail size={11}/>{t.topbar.email}</span>
        </div>
        <div className="flex items-center gap-5">
          <span className="flex items-center gap-2"><I.MapPin size={11}/>{t.topbar.addr}</span>
          <button onClick={()=>setLang(lang==='es'?'en':'es')} className="flex items-center gap-1.5 hover:text-[#14B8A6] transition uppercase">
            <I.Globe size={11}/>{lang.toUpperCase()} / {lang==='es'?'EN':'ES'}
          </button>
          <button onClick={toggleTheme} className="flex items-center gap-1.5 hover:text-[#14B8A6] transition">
            {theme==='dark'?<I.Sun size={11}/>:<I.Moon size={11}/>}
            <span>{theme==='dark'?'LIGHT':'DARK'}</span>
          </button>
        </div>
      </div>
    </div>
  );
}

/* ============== HEADER ============== */
function Header({ onQuote, cartCount }) {
  const { lang, setLang, theme, toggleTheme } = window.useApp();
  const t = window.RHX.ui[lang];
  const [scrolled, setScrolled] = useState(false);
  const [open, setOpen] = useState(false);
  const [active, setActive] = useState('home');

  useEffect(() => {
    const onScroll = () => {
      setScrolled(window.scrollY > 30);
      const sections = ['home','about','services','team','partners','contact'];
      let cur = 'home';
      sections.forEach(id => {
        const el = document.getElementById(id);
        if (el && el.getBoundingClientRect().top < 120) cur = id;
      });
      setActive(cur);
    };
    window.addEventListener('scroll', onScroll);
    onScroll();
    return () => window.removeEventListener('scroll', onScroll);
  }, []);

  const links = [
    { id:'home', label:t.nav.home },
    { id:'about', label:t.nav.about },
    { id:'services', label:t.nav.services },
    { id:'team', label:t.nav.team },
    { id:'partners', label:t.nav.partners },
    { id:'contact', label:t.nav.contact },
  ];

  return (
    <header className={`sticky top-0 z-50 transition-all duration-300 ${
      scrolled
        ? 'bg-white/95 dark:bg-[#0A0F1A]/95 backdrop-blur-md border-b border-gray-200/60 dark:border-white/5 shadow-sm shadow-black/5'
        : 'bg-white dark:bg-[#0A0F1A] border-b border-gray-200/40 dark:border-white/5'
    }`}>
      <div className="max-w-7xl mx-auto px-6 lg:px-10 flex items-center justify-between h-20">
        {/* Logo */}
        <a href="#home" className="flex items-center gap-3 group">
          <div className="relative w-10 h-10 flex items-center justify-center">
            <svg viewBox="0 0 40 40" className="w-10 h-10 text-[#14B8A6] group-hover:rotate-[30deg] transition-transform duration-500">
              <polygon points="20,2 36,11 36,29 20,38 4,29 4,11" fill="none" stroke="currentColor" strokeWidth="1.5"/>
              <g fill="currentColor">
                <rect x="11" y="12" width="2.8" height="16"/>
                <rect x="26.2" y="12" width="2.8" height="16"/>
                <rect x="11" y="19" width="18" height="2.8"/>
                <polygon points="13.8,12 22.5,12 22.5,14.6 13.8,14.6"/>
                <polygon points="20,12 22.5,12 22.5,19 20,19"/>
                <polygon points="15.4,21.8 19.6,21.8 22.5,28 18.3,28"/>
              </g>
            </svg>
          </div>
          <div className="leading-none">
            <div className="font-display font-semibold text-[15px] text-gray-900 dark:text-white tracking-tight">RINAU<span className="text-[#14B8A6]">HEX</span></div>
            <div className="font-mono text-[9px] text-gray-500 dark:text-gray-500 tracking-[0.2em] mt-1 uppercase">{t.footer.slogan}</div>
          </div>
        </a>

        {/* Desktop nav */}
        <nav className="hidden lg:flex items-center gap-1">
          {links.map(l => (
            <a key={l.id} href={`#${l.id}`} className={`relative px-4 py-2 text-[13px] font-medium tracking-wide transition-colors ${
              active === l.id
                ? 'text-[#14B8A6]'
                : 'text-gray-700 dark:text-gray-300 hover:text-[#14B8A6] dark:hover:text-[#14B8A6]'
            }`}>
              {l.label}
              {active === l.id && (
                <span className="absolute left-1/2 -translate-x-1/2 bottom-0.5 w-1 h-1 bg-[#14B8A6] rotate-45"></span>
              )}
            </a>
          ))}
        </nav>

        {/* Right cluster */}
        <div className="flex items-center gap-3">
          {/* Mobile only lang/theme */}
          <div className="md:hidden flex items-center gap-2">
            <button onClick={()=>setLang(lang==='es'?'en':'es')} className="p-2 text-gray-700 dark:text-gray-300"><I.Globe size={16}/></button>
            <button onClick={toggleTheme} className="p-2 text-gray-700 dark:text-gray-300">{theme==='dark'?<I.Sun size={16}/>:<I.Moon size={16}/>}</button>
          </div>

          <button onClick={onQuote} className="relative group hidden sm:flex items-center gap-2.5 bg-[#0A0F1A] dark:bg-[#14B8A6] text-white px-5 py-3 font-medium text-[12px] uppercase tracking-[0.18em] hover:bg-[#14B8A6] dark:hover:bg-[#0D9488] transition-colors">
            <I.ShoppingCart size={14}/>
            <span>{t.cta.quote}</span>
            {cartCount > 0 && (
              <span className="absolute -top-1.5 -right-1.5 bg-[#EF4444] text-white text-[10px] font-bold w-5 h-5 rounded-full flex items-center justify-center font-mono">{cartCount}</span>
            )}
          </button>

          <button onClick={()=>setOpen(!open)} className="lg:hidden p-2 text-gray-700 dark:text-gray-300">
            {open ? <I.X size={20}/> : <I.Menu size={20}/>}
          </button>
        </div>
      </div>

      {/* Mobile menu */}
      {open && (
        <div className="lg:hidden border-t border-gray-200 dark:border-white/5 bg-white dark:bg-[#0A0F1A]">
          <div className="px-6 py-4 flex flex-col gap-1">
            {links.map(l => (
              <a key={l.id} href={`#${l.id}`} onClick={()=>setOpen(false)} className="py-3 text-sm font-medium text-gray-700 dark:text-gray-300 border-b border-gray-100 dark:border-white/5">
                {l.label}
              </a>
            ))}
          </div>
        </div>
      )}
    </header>
  );
}

/* ============== HERO ============== */
function Hero({ onQuote }) {
  window.useAdmin();
  const { lang } = window.useApp();
  const t = window.RHX.ui[lang].hero;

  return (
    <section id="home"
      className="rhx-hero relative bg-[#0A0F1A] text-white overflow-hidden">
      {/* Ambient orb layer (non-interactive) */}
      <div className="absolute inset-0 pointer-events-none">
        <div className="absolute top-1/4 right-0 w-[500px] h-[500px] bg-[#14B8A6]/8 blur-[160px] rounded-full"></div>
        <div className="absolute bottom-0 left-0 w-[420px] h-[420px] bg-[#0B132B] blur-[140px] rounded-full"></div>
      </div>
      {/* Cybernetic Particle Constellation — interactive shader-style backdrop */}
      <div className="absolute inset-0">
        <window.ParticleConstellation/>
      </div>
      {/* Bottom fade to seam into next section */}
      <div className="absolute inset-0 bg-gradient-to-b from-transparent via-transparent to-[#0A0F1A] pointer-events-none"></div>

      {/* Constellation legend (bottom-left) */}
      <div className="absolute left-6 lg:left-10 bottom-6 hidden md:flex items-center gap-3 font-mono text-[10px] text-gray-500 tracking-widest pointer-events-none z-[5]">
        <span className="w-2 h-2 bg-[#14B8A6] rotate-45"></span>
        <span>// CYBERNETIC.PARTICLE.CONSTELLATION — MOVE / CLICK</span>
      </div>

      {/* Side rails */}
      <div className="absolute left-0 top-0 bottom-0 w-px bg-gradient-to-b from-transparent via-[#14B8A6]/30 to-transparent hidden md:block pointer-events-none"></div>
      <div className="absolute right-0 top-0 bottom-0 w-px bg-gradient-to-b from-transparent via-[#14B8A6]/30 to-transparent hidden md:block pointer-events-none"></div>

      <div className="relative max-w-7xl mx-auto px-6 lg:px-10 py-24 lg:py-32 grid lg:grid-cols-12 gap-12 items-center">
        <div className="lg:col-span-7 space-y-8 relative z-10">
          <div className="flex items-center gap-3 text-[11px] font-mono tracking-[0.24em] text-[#14B8A6] uppercase rhx-fade-up" style={{animationDelay:'.05s'}}>
            <span className="w-8 h-px bg-[#14B8A6]"></span>
            <I.Sparkles size={12}/>
            <span>{t.kicker}</span>
          </div>
          <h1 className="font-display font-medium text-[clamp(48px,7vw,96px)] leading-[0.95] tracking-[-0.025em] rhx-fade-up" style={{animationDelay:'.1s'}}>
            {t.title1}<br/>
            <span className="text-[#14B8A6]">{t.title2}</span>
          </h1>
          <p className="text-gray-300 text-lg lg:text-xl leading-relaxed max-w-2xl font-light rhx-fade-up" style={{animationDelay:'.2s'}}>{t.desc}</p>
          <div className="flex flex-wrap items-center gap-4 pt-4 rhx-fade-up" style={{animationDelay:'.3s'}}>
            <a href="#services" className="group inline-flex items-center gap-3 bg-[#14B8A6] hover:bg-[#0D9488] text-white px-7 py-4 rounded-xl font-medium text-[13px] uppercase tracking-[0.18em] transition-all hover:translate-y-[-2px] hover:shadow-lg hover:shadow-[#14B8A6]/30">
              {t.cta1}
              <I.ArrowRight size={16} className="group-hover:translate-x-1 transition-transform"/>
            </a>
            <button onClick={onQuote} className="inline-flex items-center gap-3 border border-white/20 hover:border-[#14B8A6] hover:text-[#14B8A6] text-white px-7 py-4 rounded-xl font-medium text-[13px] uppercase tracking-[0.18em] transition-all hover:translate-y-[-2px]">
              {t.cta2}
              <I.ShoppingCart size={16}/>
            </button>
          </div>
        </div>

        {/* Schematic visual — static hex core; sits in front of particle constellation */}
        <div className="lg:col-span-5 relative aspect-square hidden lg:block rhx-zoom pointer-events-none" style={{animationDelay:'.15s'}}>
          <svg viewBox="0 0 400 400" className="w-full h-full">
            <defs>
              <radialGradient id="glow" cx="50%" cy="50%" r="50%">
                <stop offset="0%" stopColor="#14B8A6" stopOpacity="0.35"/>
                <stop offset="100%" stopColor="#14B8A6" stopOpacity="0"/>
              </radialGradient>
            </defs>
            {/* Central glow — static */}
            <circle cx="200" cy="200" r="180" fill="url(#glow)"/>

            {/* Concentric rings — static */}
            <g fill="none" stroke="#14B8A6">
              <circle cx="200" cy="200" r="180" strokeWidth="0.8" opacity="0.4"/>
              <circle cx="200" cy="200" r="140" strokeWidth="0.6" opacity="0.3" strokeDasharray="3 8"/>
              <circle cx="200" cy="200" r="100" strokeWidth="0.6" opacity="0.35"/>
            </g>

            {/* Static axes */}
            <g stroke="#14B8A6" strokeWidth="0.5" opacity="0.35" strokeDasharray="2 4">
              <line x1="0" y1="200" x2="400" y2="200"/>
              <line x1="200" y1="0" x2="200" y2="400"/>
              <line x1="0" y1="0" x2="400" y2="400"/>
              <line x1="400" y1="0" x2="0" y2="400"/>
            </g>

            {/* Hex core — static */}
            <polygon points="200,80 304,140 304,260 200,320 96,260 96,140" fill="#0B132B" stroke="#14B8A6" strokeWidth="2"/>
            {/* Inner RH monogram — static */}
            <g fill="#F2F5FA" transform="translate(143,148) scale(0.52)">
              <rect x="0" y="0" width="20" height="200"/>
              <rect x="180" y="0" width="20" height="200"/>
              <rect x="0" y="90" width="200" height="20"/>
              <rect x="20" y="0" width="100" height="20"/>
              <rect x="100" y="0" width="20" height="90"/>
              <polygon points="40,110 80,110 140,200 100,200"/>
            </g>

            {/* Vertex nodes — static */}
            <g fill="#EF4444">
              <circle cx="200" cy="80" r="6"/>
              <circle cx="304" cy="140" r="6"/>
              <circle cx="304" cy="260" r="6"/>
              <circle cx="200" cy="320" r="6"/>
              <circle cx="96" cy="260" r="6"/>
              <circle cx="96" cy="140" r="6"/>
            </g>

            <g fontFamily="JetBrains Mono" fontSize="9" fill="#14B8A6" letterSpacing="1">
              <text x="208" y="78">N₁</text><text x="312" y="138">N₂</text><text x="312" y="268">N₃</text>
              <text x="208" y="338">N₄</text><text x="60" y="268">N₅</text><text x="60" y="138">N₆</text>
            </g>
            <g fontFamily="JetBrains Mono" fontSize="8" fill="#5B6577">
              <text x="20" y="20">// SCHEMATIC 01</text>
              <text x="20" y="380">// HEX-CORE / RH</text>
              <text x="285" y="380">v.2026</text>
            </g>
          </svg>
        </div>
      </div>

      {/* Bottom scroll cue */}
      <div className="absolute left-1/2 -translate-x-1/2 bottom-6 flex flex-col items-center gap-2 text-gray-500 font-mono text-[10px] tracking-widest pointer-events-none">
        <span className="rhx-float">SCROLL</span>
        <div className="w-px h-8 bg-gradient-to-b from-[#14B8A6] to-transparent"></div>
      </div>
    </section>
  );
}

/* ============== FEATURES STRIP ============== */
function Features() {
  window.useAdmin();
  const { lang } = window.useApp();
  const t = window.RHX.ui[lang].features;
  return (
    <section className="bg-white dark:bg-[#06090F] border-b border-gray-100 dark:border-white/5">
      <div className="max-w-7xl mx-auto px-6 lg:px-10 py-20 lg:py-24">
        <div className="text-center mb-16">
          <div className="text-[11px] font-mono tracking-[0.24em] text-[#14B8A6] uppercase mb-4">// 01 / Principios</div>
          <h2 className="font-display font-medium text-3xl lg:text-5xl text-gray-900 dark:text-white tracking-tight">{t.title}</h2>
          <p className="text-gray-500 dark:text-gray-400 mt-3 text-sm">{t.sub}</p>
        </div>
        <div className="grid sm:grid-cols-2 lg:grid-cols-4 gap-px bg-gray-100 dark:bg-white/5 border border-gray-100 dark:border-white/5">
          {t.items.map((it, i) => {
            const Icon = I[it.i] || I.Cpu;
            return (
              <div key={i} className="group bg-white dark:bg-[#0A0F1A] p-8 lg:p-10 transition-colors hover:bg-gray-50 dark:hover:bg-[#0B132B]">
                <div className="flex items-start justify-between mb-8">
                  <div className="w-12 h-12 border border-[#14B8A6]/30 flex items-center justify-center text-[#14B8A6] group-hover:bg-[#14B8A6] group-hover:text-white transition-all">
                    <Icon size={22}/>
                  </div>
                  <span className="font-mono text-[10px] text-gray-400 dark:text-gray-600 tracking-wider">0{i+1}</span>
                </div>
                <h3 className="font-display font-medium text-lg text-gray-900 dark:text-white mb-3 tracking-tight">{it.t}</h3>
                <p className="text-[13px] text-gray-500 dark:text-gray-400 leading-relaxed">{it.d}</p>
              </div>
            );
          })}
        </div>
      </div>
    </section>
  );
}

/* ============== ABOUT ============== */
function About() {
  window.useAdmin();
  const { lang } = window.useApp();
  const t = window.RHX.ui[lang].about;
  return (
    <section id="about" className="bg-gray-50 dark:bg-[#0A0F1A] relative overflow-hidden">
      {/* Blueprint pattern */}
      <svg className="absolute inset-0 w-full h-full opacity-[0.04]" preserveAspectRatio="xMidYMid slice">
        <defs>
          <pattern id="bp-about" width="40" height="40" patternUnits="userSpaceOnUse">
            <path d="M 40 0 L 0 0 0 40" fill="none" stroke="currentColor" strokeWidth="1" className="text-gray-900 dark:text-white"/>
          </pattern>
        </defs>
        <rect width="100%" height="100%" fill="url(#bp-about)"/>
      </svg>

      <div className="relative max-w-7xl mx-auto px-6 lg:px-10 py-24 lg:py-32 grid lg:grid-cols-12 gap-16 items-center">
        {/* Visual */}
        <div className="lg:col-span-5 relative group">
          <div className="relative aspect-[4/5] bg-[#0B132B] border border-gray-200 dark:border-white/10 rounded-2xl overflow-hidden rhx-card-hover">
            {/* Tech schematic — fully static */}
            <svg viewBox="0 0 400 500" className="w-full h-full">
              <defs>
                <pattern id="bp-detail" width="20" height="20" patternUnits="userSpaceOnUse">
                  <path d="M 20 0 L 0 0 0 20" fill="none" stroke="#14B8A6" strokeWidth="0.4" opacity="0.3"/>
                </pattern>
                <radialGradient id="fig-glow" cx="50%" cy="44%" r="40%">
                  <stop offset="0%" stopColor="#14B8A6" stopOpacity="0.28"/>
                  <stop offset="100%" stopColor="#14B8A6" stopOpacity="0"/>
                </radialGradient>
              </defs>
              <rect width="100%" height="100%" fill="url(#bp-detail)"/>
              <circle cx="200" cy="220" r="160" fill="url(#fig-glow)"/>

              {/* Concentric hexagons — static, centered */}
              <polygon points="200,80 320,150 320,290 200,360 80,290 80,150" fill="none" stroke="#14B8A6" strokeWidth="2" opacity="0.8"/>
              <polygon points="200,140 270,180 270,260 200,300 130,260 130,180" fill="none" stroke="#14B8A6" strokeWidth="1.5" opacity="0.5"/>
              <polygon points="200,180 240,205 240,235 200,260 160,235 160,205" fill="#14B8A6" opacity="0.18"/>
              <polygon points="200,180 240,205 240,235 200,260 160,235 160,205" fill="none" stroke="#14B8A6" strokeWidth="1"/>

              {/* Center marker — static */}
              <circle cx="200" cy="220" r="4" fill="#EF4444"/>
              <circle cx="200" cy="220" r="10" fill="none" stroke="#EF4444" strokeWidth="1" opacity="0.55"/>
              <circle cx="200" cy="220" r="16" fill="none" stroke="#EF4444" strokeWidth="0.6" opacity="0.3"/>

              {/* Static connector dashes */}
              <g stroke="#14B8A6" strokeWidth="0.6" strokeDasharray="2 4" opacity="0.5">
                <line x1="0" y1="220" x2="80" y2="220"/>
                <line x1="320" y1="220" x2="400" y2="220"/>
                <line x1="200" y1="0" x2="200" y2="80"/>
                <line x1="200" y1="360" x2="200" y2="500"/>
              </g>

              {/* Vertex dots — static */}
              <g fill="#14B8A6">
                <circle cx="200" cy="80" r="3.5"/>
                <circle cx="320" cy="150" r="3.5"/>
                <circle cx="320" cy="290" r="3.5"/>
                <circle cx="200" cy="360" r="3.5"/>
                <circle cx="80" cy="290" r="3.5"/>
                <circle cx="80" cy="150" r="3.5"/>
              </g>

              <g fontFamily="JetBrains Mono" fontSize="9" fill="#14B8A6" letterSpacing="1">
                <text x="20" y="30">// FIG.A — CORE.STRUCTURE</text>
                <text x="20" y="480">REV / 2026.05</text>
                <text x="280" y="480">RHX-VBG-001</text>
              </g>
            </svg>
            {/* Corner brackets — expand only on card hover */}
            <span className="absolute top-4 left-4 w-4 h-4 border-t border-l border-[#14B8A6] group-hover:w-6 group-hover:h-6 transition-all"></span>
            <span className="absolute top-4 right-4 w-4 h-4 border-t border-r border-[#14B8A6] group-hover:w-6 group-hover:h-6 transition-all"></span>
            <span className="absolute bottom-4 left-4 w-4 h-4 border-b border-l border-[#14B8A6] group-hover:w-6 group-hover:h-6 transition-all"></span>
            <span className="absolute bottom-4 right-4 w-4 h-4 border-b border-r border-[#14B8A6] group-hover:w-6 group-hover:h-6 transition-all"></span>
          </div>
          {/* Floating data card — static (no bobbing) */}
          <div className="absolute -bottom-6 -right-6 bg-white dark:bg-[#0B132B] border border-gray-200 dark:border-white/10 rounded-xl p-5 shadow-xl hidden md:block">
            <div className="font-mono text-[10px] text-[#14B8A6] tracking-widest mb-2">// METHOD</div>
            <div className="font-display font-medium text-sm text-gray-900 dark:text-white">RHX-CONVERGENCE™</div>
            <div className="font-mono text-[10px] text-gray-500 mt-1">v.2026.5</div>
          </div>
        </div>

        {/* Text */}
        <div className="lg:col-span-7 space-y-8">
          <div className="flex items-center gap-3 text-[11px] font-mono tracking-[0.24em] text-[#14B8A6] uppercase">
            <span className="w-8 h-px bg-[#14B8A6]"></span>
            <span>// 02 / {t.kicker}</span>
          </div>
          <h2 className="font-display font-medium text-3xl lg:text-5xl text-gray-900 dark:text-white tracking-tight leading-[1.05] text-balance">{t.title}</h2>
          <div className="space-y-5 text-gray-600 dark:text-gray-400 text-base leading-relaxed max-w-2xl">
            <p>{t.p1}</p>
            {t.history.map((p,i)=>(<p key={i}>{p}</p>))}
          </div>

          <blockquote className="border-l-2 border-[#14B8A6] pl-5 py-1 text-lg lg:text-xl font-display font-medium text-gray-900 dark:text-white leading-snug text-balance max-w-2xl">
            {t.quote}
          </blockquote>

          {/* Stats */}
          <div className="grid grid-cols-2 lg:grid-cols-4 gap-px bg-gray-200 dark:bg-white/10 border border-gray-200 dark:border-white/10 mt-10">
            {t.stats.map((s,i) => (
              <div key={i} className="bg-gray-50 dark:bg-[#0A0F1A] p-6">
                <div className="font-display font-medium text-3xl text-[#14B8A6] tracking-tight">{s.n}</div>
                <div className="font-mono text-[10px] text-gray-500 dark:text-gray-500 tracking-widest uppercase mt-1">{s.l}</div>
              </div>
            ))}
          </div>
        </div>
      </div>
    </section>
  );
}

/* ============== TEAM ============== */
function Team() {
  const { lang } = window.useApp();
  const t = window.RHX.ui[lang].team;
  const data = window.RHX.teamData[lang];
  return (
    <section id="team" className="bg-white dark:bg-[#06090F]">
      <div className="max-w-7xl mx-auto px-6 lg:px-10 py-24 lg:py-32">
        <div className="text-center mb-16 max-w-3xl mx-auto">
          <div className="text-[11px] font-mono tracking-[0.24em] text-[#14B8A6] uppercase mb-4">// 04 / {t.kicker}</div>
          <h2 className="font-display font-medium text-3xl lg:text-5xl text-gray-900 dark:text-white tracking-tight leading-tight text-balance">{t.title}</h2>
        </div>

        <div className="grid lg:grid-cols-12 gap-12 items-start">
          <div className="lg:col-span-7 space-y-6">
            {data.content.map((p,i)=>(
              <p key={i} className="text-gray-600 dark:text-gray-400 text-base lg:text-lg leading-relaxed font-light">{p}</p>
            ))}
            <blockquote className="border-l-2 border-[#14B8A6] pl-5 py-1 text-lg lg:text-xl font-display font-medium text-gray-900 dark:text-white leading-snug text-balance">
              {data.quote}
            </blockquote>
          </div>
          <div className="lg:col-span-5 space-y-3">
            {t.tags.map((tag,i)=>{
              const Icon = I[tag.i] || I.Users;
              return (
                <div key={i} className="group flex items-center gap-5 p-5 border border-gray-200 dark:border-white/10 bg-gray-50 dark:bg-[#0A0F1A] hover:border-[#14B8A6] hover:bg-white dark:hover:bg-[#0B132B] transition-all">
                  <div className="w-12 h-12 border border-[#14B8A6]/30 flex items-center justify-center text-[#14B8A6] group-hover:bg-[#14B8A6] group-hover:text-white transition-colors shrink-0">
                    <Icon size={20}/>
                  </div>
                  <div>
                    <div className="font-mono text-[9px] text-gray-500 tracking-widest uppercase">{String(i+1).padStart(2,'0')} · Expertise</div>
                    <div className="font-display font-medium text-base text-gray-900 dark:text-white tracking-tight mt-1">{tag.l}</div>
                  </div>
                  <I.ArrowUpRight size={14} className="ml-auto text-gray-400 group-hover:text-[#14B8A6] transition-colors"/>
                </div>
              );
            })}
          </div>
        </div>
      </div>
    </section>
  );
}

/* ============== PARTNERS — carousel + logo slider ============== */
function PartnerCard({ p, t, lang }) {
  const { theme } = window.useApp();
  const Icon = I[p.icon] || I.Building;
  const logoSrc = p.logo ? (theme==='dark' ? (p.logo.dark||p.logo.light) : (p.logo.light||p.logo.dark)) : null;
  return (
    <div className={`group relative h-full bg-white dark:bg-[#0B132B] border ${p.placeholder?'border-dashed border-gray-300 dark:border-white/15':'border-gray-200 dark:border-white/10'} rounded-2xl p-8 lg:p-10 hover:border-[#14B8A6]/50 transition-all overflow-hidden`}>
      <span className="absolute top-0 right-0 w-12 h-12 border-t-2 border-r-2 border-[#14B8A6]/0 group-hover:border-[#14B8A6] transition-colors rounded-tr-2xl"></span>
      <div className="flex items-center justify-between mb-8">
        <span className={`font-mono text-[10px] tracking-[0.18em] uppercase rounded-full px-3 py-1.5 ${p.placeholder?'text-gray-500 dark:text-gray-500 bg-gray-100 dark:bg-white/5':'text-[#14B8A6] bg-[#14B8A6]/10'}`}>{p.badge}</span>
        <span className="font-mono text-[10px] text-gray-400 dark:text-gray-600">// {p.id.toUpperCase()}</span>
      </div>
      <div className="flex items-start gap-5 mb-6">
        <div className={`w-14 h-14 rounded-xl border flex items-center justify-center shrink-0 overflow-hidden ${p.placeholder?'border-gray-300 dark:border-white/10 text-gray-400 dark:text-gray-600':'border-[#14B8A6]/30 text-[#14B8A6]'}`}>
          {logoSrc ? <img src={logoSrc} alt={p.name[lang]} className="w-full h-full object-contain p-1.5"/> : <Icon size={24}/>}
        </div>
        <div>
          <h3 className={`font-display font-medium text-xl tracking-tight leading-tight ${p.placeholder?'text-gray-500 dark:text-gray-400':'text-gray-900 dark:text-white'}`}>{p.name[lang]}</h3>
          <div className="font-mono text-[10px] text-gray-500 dark:text-gray-500 tracking-wider uppercase mt-2">{p.type[lang]}</div>
        </div>
      </div>
      <p className="text-[13px] text-gray-600 dark:text-gray-400 leading-relaxed mb-8">{p.description[lang]}</p>
      <div className="border-t border-gray-200 dark:border-white/10 pt-6">
        <div className="font-mono text-[10px] text-gray-500 dark:text-gray-600 uppercase tracking-widest flex items-center gap-2 mb-3">
          <I.Target size={11} className="text-[#14B8A6]"/>{t.workstreams}
        </div>
        <div className="flex flex-wrap gap-2">
          {p.features[lang].map((f,j)=>(
            <span key={j} className={`text-[11px] rounded-full px-3 py-1.5 font-medium border ${p.placeholder?'text-gray-500 dark:text-gray-500 bg-gray-50 dark:bg-white/5 border-gray-200 dark:border-white/10':'text-gray-800 dark:text-gray-200 bg-gray-50 dark:bg-[#11182B] border-gray-200 dark:border-white/10'}`}>{f}</span>
          ))}
        </div>
      </div>
    </div>
  );
}

function PartnerLogoMark({ p, active, onClick, lang }) {
  const { theme } = window.useApp();
  const logoSrc = p.logo ? (theme==='dark' ? (p.logo.dark||p.logo.light) : (p.logo.light||p.logo.dark)) : null;
  return (
    <button onClick={onClick} title={p.name[lang]} className={`group shrink-0 flex flex-col items-center gap-2 transition-all ${active?'opacity-100':'opacity-60 hover:opacity-100'}`}>
      <div className={`relative w-[120px] h-[72px] rounded-xl border flex items-center justify-center transition-colors ${
        active
          ? 'border-[#14B8A6] bg-[#14B8A6]/10'
          : p.placeholder
            ? 'border-dashed border-gray-300 dark:border-white/15 bg-white/50 dark:bg-[#0B132B]/50 hover:border-[#14B8A6]/50'
            : 'border-gray-200 dark:border-white/10 bg-white dark:bg-[#0B132B] hover:border-[#14B8A6]/50'
      }`}>
        {/* Logo mark — uploaded image or hex fallback */}
        {logoSrc ? (
          <img src={logoSrc} alt={p.name[lang]} className="max-w-[88px] max-h-[56px] object-contain"/>
        ) : (
          <svg viewBox="0 0 80 60" className={`w-14 h-10 ${active?'text-[#14B8A6]':p.placeholder?'text-gray-400 dark:text-gray-600':'text-gray-700 dark:text-gray-300 group-hover:text-[#14B8A6]'} transition-colors`}>
            <polygon points="40,4 70,21 70,49 40,56 10,49 10,21" fill="none" stroke="currentColor" strokeWidth="1.5"/>
            <text x="40" y="36" textAnchor="middle" fontFamily="Space Grotesk" fontSize="14" fontWeight="600" fill="currentColor">{p.mark}</text>
          </svg>
        )}
        {active && <span className="absolute -bottom-1 left-1/2 -translate-x-1/2 w-1.5 h-1.5 bg-[#14B8A6] rotate-45"></span>}
      </div>
      <span className={`font-mono text-[9px] tracking-widest uppercase ${active?'text-[#14B8A6]':'text-gray-500 dark:text-gray-500'}`}>{p.badge}</span>
    </button>
  );
}

function Partners() {
  window.useAdmin();
  const { lang } = window.useApp();
  const t = window.RHX.ui[lang].partners;
  const partners = window.RHX.partnersData;
  const [idx, setIdx] = useState(0);
  const [perView, setPerView] = useState(2);

  useEffect(()=>{
    const update = () => setPerView(window.innerWidth < 1024 ? 1 : 2);
    update();
    window.addEventListener('resize', update);
    return ()=>window.removeEventListener('resize', update);
  }, []);

  const maxIdx = Math.max(0, partners.length - perView);
  const safeIdx = Math.min(idx, maxIdx);
  const prev = () => setIdx(i => Math.max(0, i - 1));
  const next = () => setIdx(i => Math.min(maxIdx, i + 1));
  const jumpTo = (i) => setIdx(Math.min(maxIdx, Math.max(0, i - Math.floor(perView/2))));

  // visible window indices [safeIdx ... safeIdx+perView-1] for highlighting active in slider
  const visibleSet = new Set();
  for (let k = safeIdx; k < safeIdx + perView; k++) visibleSet.add(k);

  return (
    <section id="partners" className="bg-gray-50 dark:bg-[#0A0F1A] relative overflow-hidden">
      <div className="max-w-7xl mx-auto px-6 lg:px-10 py-24 lg:py-32">
        {/* Header */}
        <div className="grid lg:grid-cols-12 gap-12 mb-12 items-end">
          <div className="lg:col-span-7 space-y-5">
            <div className="text-[11px] font-mono tracking-[0.24em] text-[#14B8A6] uppercase">// 05 / {t.kicker}</div>
            <h2 className="font-display font-medium text-3xl lg:text-5xl text-gray-900 dark:text-white tracking-tight leading-[1.05] text-balance">{t.title}</h2>
          </div>
          <div className="lg:col-span-5 flex items-end justify-between gap-6">
            <p className="text-sm text-gray-500 dark:text-gray-400 leading-relaxed flex-1">{t.sub}</p>
            {/* Carousel controls */}
            <div className="flex items-center gap-2 shrink-0">
              <button onClick={prev} disabled={safeIdx===0}
                className="w-11 h-11 rounded-full border border-gray-300 dark:border-white/15 text-gray-700 dark:text-gray-300 flex items-center justify-center hover:border-[#14B8A6] hover:text-[#14B8A6] disabled:opacity-30 disabled:cursor-not-allowed transition-colors">
                <I.ArrowLeft size={16}/>
              </button>
              <button onClick={next} disabled={safeIdx>=maxIdx}
                className="w-11 h-11 rounded-full bg-[#14B8A6] text-white flex items-center justify-center hover:bg-[#0D9488] disabled:opacity-30 disabled:cursor-not-allowed transition-colors">
                <I.ArrowRight size={16}/>
              </button>
            </div>
          </div>
        </div>

        {/* Carousel */}
        <div className="relative overflow-hidden">
          <div className="flex transition-transform duration-500 ease-out" style={{ transform:`translateX(calc(${-safeIdx*100/perView}% - ${safeIdx*12}px))`, gap:'24px' }}>
            {partners.map(p=>(
              <div key={p.id} style={{ flex:`0 0 calc(${100/perView}% - ${(perView-1)*12/perView}px)` }}>
                <PartnerCard p={p} t={t} lang={lang}/>
              </div>
            ))}
          </div>
        </div>

        {/* Carousel position indicator */}
        <div className="flex items-center justify-center gap-2 mt-6 mb-12">
          <span className="font-mono text-[10px] text-gray-500 tracking-widest mr-2">
            {String(safeIdx+1).padStart(2,'0')} — {String(Math.min(safeIdx+perView, partners.length)).padStart(2,'0')} / {String(partners.length).padStart(2,'0')}
          </span>
          {Array.from({length: maxIdx+1}).map((_,i)=>(
            <button key={i} onClick={()=>setIdx(i)} className={`h-1 transition-all ${i===safeIdx?'w-8 bg-[#14B8A6]':'w-3 bg-gray-300 dark:bg-white/15 hover:bg-[#14B8A6]/50'}`}></button>
          ))}
        </div>

        {/* Logo slider */}
        <div className="relative bg-white dark:bg-[#0B132B] border border-gray-200 dark:border-white/10 rounded-2xl p-6 lg:p-8 mb-10">
          <div className="flex items-center justify-between mb-5">
            <div className="font-mono text-[10px] text-[#14B8A6] uppercase tracking-widest">// CLIENT.GRID — {lang==='es'?'Selecciona para destacar arriba':'Click to feature above'}</div>
            <div className="font-mono text-[10px] text-gray-500 dark:text-gray-500 tracking-widest">{partners.length} {lang==='es'?'aliados':'partners'}</div>
          </div>
          <div className="flex items-center gap-5 overflow-x-auto pb-2 -mx-2 px-2" style={{scrollbarWidth:'thin'}}>
            {partners.map((p,i)=>(
              <PartnerLogoMark key={p.id} p={p} active={visibleSet.has(i)} onClick={()=>jumpTo(i)} lang={lang}/>
            ))}
          </div>
        </div>

        {/* Integrity card */}
        <div className="relative bg-[#0B132B] text-white p-10 lg:p-14 rounded-2xl overflow-hidden">
          <svg className="absolute inset-0 w-full h-full opacity-[0.06]" preserveAspectRatio="xMidYMid slice">
            <defs>
              <pattern id="hex-int" width="60" height="52" patternUnits="userSpaceOnUse">
                <polygon points="30,2 58,16 58,40 30,54 2,40 2,16" fill="none" stroke="#14B8A6" strokeWidth="1"/>
              </pattern>
            </defs>
            <rect width="100%" height="100%" fill="url(#hex-int)"/>
          </svg>
          <div className="relative grid lg:grid-cols-12 gap-8 items-center">
            <div className="lg:col-span-8 space-y-5">
              <div className="inline-flex items-center gap-2 bg-[#14B8A6]/15 border border-[#14B8A6]/30 px-3 py-1.5">
                <I.Award size={12} className="text-[#14B8A6]"/>
                <span className="font-mono text-[10px] tracking-[0.18em] text-[#14B8A6] uppercase">{t.footerCard.tag}</span>
              </div>
              <h3 className="font-display font-medium text-2xl lg:text-3xl tracking-tight leading-[1.15] text-balance">{t.footerCard.title}</h3>
              <p className="text-sm text-gray-300 leading-relaxed max-w-2xl">{t.footerCard.desc}</p>
            </div>
            <div className="lg:col-span-4 flex justify-end">
              <a href="#contact" className="group inline-flex items-center gap-3 bg-[#14B8A6] hover:bg-[#0D9488] text-white px-6 py-4 font-medium text-[12px] uppercase tracking-[0.18em] transition-colors">
                {window.RHX.ui[lang].hero.cta2}
                <I.ArrowRight size={14} className="group-hover:translate-x-1 transition-transform"/>
              </a>
            </div>
          </div>
        </div>
      </div>
    </section>
  );
}

/* ============== CONTACT ============== */
function Contact() {
  window.useAdmin();
  const { lang } = window.useApp();
  const t = window.RHX.ui[lang].contact;
  return (
    <section id="contact" className="bg-white dark:bg-[#06090F]">
      <div className="max-w-7xl mx-auto px-6 lg:px-10 py-24 lg:py-32">
        <div className="text-center mb-16 max-w-3xl mx-auto">
          <div className="text-[11px] font-mono tracking-[0.24em] text-[#14B8A6] uppercase mb-4">// 06 / {t.kicker}</div>
          <h2 className="font-display font-medium text-3xl lg:text-5xl text-gray-900 dark:text-white tracking-tight leading-tight text-balance">{t.title}</h2>
          <p className="text-gray-500 dark:text-gray-400 mt-4 text-sm leading-relaxed">{t.sub}</p>
        </div>

        <div className="grid lg:grid-cols-12 gap-12">
          {/* Info column */}
          <div className="lg:col-span-5 space-y-8">
            {t.info.map((it,i)=>{
              const Icon = I[it.i] || I.Mail;
              return (
                <div key={i} className="flex gap-5 group">
                  <div className="w-12 h-12 border border-[#14B8A6]/30 flex items-center justify-center text-[#14B8A6] shrink-0 group-hover:bg-[#14B8A6] group-hover:text-white transition-colors">
                    <Icon size={18}/>
                  </div>
                  <div>
                    <div className="font-mono text-[10px] text-gray-400 dark:text-gray-500 uppercase tracking-widest mb-1">// {String(i+1).padStart(2,'0')} · {it.t}</div>
                    <div className="text-gray-900 dark:text-white text-[15px] font-medium">{it.v}</div>
                  </div>
                </div>
              );
            })}
            <div className="border-t border-gray-100 dark:border-white/10 pt-8">
              <div className="font-mono text-[10px] text-gray-400 dark:text-gray-500 uppercase tracking-widest mb-3">// HORARIO</div>
              <div className="grid grid-cols-2 gap-3 text-[13px]">
                <div className="text-gray-600 dark:text-gray-400">Lun — Vie</div>
                <div className="text-gray-900 dark:text-white font-mono">08:00 — 18:00</div>
                <div className="text-gray-600 dark:text-gray-400">Sáb</div>
                <div className="text-gray-900 dark:text-white font-mono">09:00 — 13:00</div>
              </div>
            </div>
          </div>

          {/* Form */}
          <div className="lg:col-span-7 bg-gray-50 dark:bg-[#0A0F1A] border border-gray-200 dark:border-white/10 p-8 lg:p-10">
            <div className="grid md:grid-cols-2 gap-5">
              <Field label={t.labels.name}/>
              <Field label={t.labels.email} type="email"/>
              <Field label={t.labels.org} full/>
            </div>
            <div className="mt-5 space-y-2">
              <label className="font-mono text-[10px] text-gray-500 dark:text-gray-500 uppercase tracking-widest">{t.labels.msg}</label>
              <textarea rows={6} className="w-full p-4 bg-white dark:bg-[#0B132B] border border-gray-200 dark:border-white/10 text-[14px] text-gray-900 dark:text-white focus:border-[#14B8A6] outline-none transition-colors resize-none"></textarea>
            </div>
            <button className="mt-6 w-full md:w-auto inline-flex items-center justify-center gap-3 bg-[#14B8A6] hover:bg-[#0D9488] text-white px-8 py-4 font-medium text-[12px] uppercase tracking-[0.18em] transition-colors">
              {t.send}
              <I.Send size={14}/>
            </button>
          </div>
        </div>
      </div>
    </section>
  );
}
function Field({label, type='text', full}){
  return (
    <div className={`space-y-2 ${full?'md:col-span-2':''}`}>
      <label className="font-mono text-[10px] text-gray-500 dark:text-gray-500 uppercase tracking-widest">{label}</label>
      <input type={type} className="w-full p-4 bg-white dark:bg-[#0B132B] border border-gray-200 dark:border-white/10 text-[14px] text-gray-900 dark:text-white focus:border-[#14B8A6] outline-none transition-colors"/>
    </div>
  );
}

/* ============== FOOTER ============== */
function Footer() {
  window.useAdmin();
  const { lang } = window.useApp();
  const t = window.RHX.ui[lang].footer;
  const services = window.RHX.services.slice(0, 6);
  return (
    <footer className="bg-[#06090F] text-gray-400 border-t border-white/5">
      <div className="max-w-7xl mx-auto px-6 lg:px-10 pt-20 pb-12">
        <div className="grid md:grid-cols-2 lg:grid-cols-12 gap-12 mb-16">
          {/* Brand col */}
          <div className="lg:col-span-4 space-y-6">
            <a href="#home" className="flex items-center gap-3">
              <svg viewBox="0 0 40 40" className="w-10 h-10 text-[#14B8A6]">
                <polygon points="20,2 36,11 36,29 20,38 4,29 4,11" fill="none" stroke="currentColor" strokeWidth="1.5"/>
                <g fill="currentColor">
                  <rect x="11" y="12" width="2.8" height="16"/>
                  <rect x="26.2" y="12" width="2.8" height="16"/>
                  <rect x="11" y="19" width="18" height="2.8"/>
                  <polygon points="13.8,12 22.5,12 22.5,14.6 13.8,14.6"/>
                  <polygon points="20,12 22.5,12 22.5,19 20,19"/>
                  <polygon points="15.4,21.8 19.6,21.8 22.5,28 18.3,28"/>
                </g>
              </svg>
              <div className="leading-none">
                <div className="font-display font-semibold text-base text-white tracking-tight">RINAU<span className="text-[#14B8A6]">HEX</span> S.A.S.</div>
                <div className="font-mono text-[9px] text-gray-500 tracking-[0.2em] mt-1 uppercase">{t.slogan}</div>
              </div>
            </a>
            <p className="text-[13px] text-gray-500 leading-relaxed max-w-sm">
              {lang==='es'
                ? 'Agencia tecnológica especializada en ingeniería aplicada, inteligencia artificial y arquitecturas de servidores.'
                : 'Technology agency specialized in applied engineering, artificial intelligence and server architectures.'}
            </p>
            <div className="flex gap-2">
              {['Linkedin','Github','Twitter'].map(s=>{
                const Icon = I[s];
                return <a key={s} href="#" className="w-9 h-9 border border-white/10 flex items-center justify-center text-gray-500 hover:bg-[#14B8A6] hover:text-white hover:border-[#14B8A6] transition-colors"><Icon size={14}/></a>;
              })}
            </div>
          </div>

          {/* Nav col */}
          <div className="lg:col-span-2 space-y-5">
            <div className="font-mono text-[10px] text-[#14B8A6] uppercase tracking-widest">// {t.linksBlock}</div>
            <ul className="space-y-3 text-[13px]">
              {t.links.map((l,i)=>(
                <li key={i}><a href={l.h} className="hover:text-[#14B8A6] transition-colors">{l.l}</a></li>
              ))}
            </ul>
          </div>

          {/* Services col */}
          <div className="lg:col-span-3 space-y-5">
            <div className="font-mono text-[10px] text-[#14B8A6] uppercase tracking-widest">// {t.servicesBlock}</div>
            <ul className="space-y-3 text-[13px]">
              {services.map(s=>(
                <li key={s.id}><a href="#services" className="hover:text-[#14B8A6] transition-colors">{s.title[lang]}</a></li>
              ))}
            </ul>
          </div>

          {/* Contact col */}
          <div className="lg:col-span-3 space-y-5">
            <div className="font-mono text-[10px] text-[#14B8A6] uppercase tracking-widest">// {t.contactBlock}</div>
            <ul className="space-y-4 text-[13px]">
              <li className="flex items-start gap-3"><I.MapPin size={14} className="text-[#14B8A6] mt-1 shrink-0"/><span>Calle 70 D # 108 A 71<br/>Of. 302, Bogotá, Colombia</span></li>
              <li className="flex items-center gap-3"><I.Phone size={14} className="text-[#14B8A6] shrink-0"/><span>+57 311 249 4694</span></li>
              <li className="flex items-center gap-3"><I.Mail size={14} className="text-[#14B8A6] shrink-0"/><span>contacto@rinauhex.com.co</span></li>
            </ul>
          </div>
        </div>

        {/* Bottom bar */}
        <div className="pt-8 border-t border-white/5 flex flex-col md:flex-row justify-between items-start md:items-center gap-4 font-mono text-[10px] tracking-widest uppercase text-gray-500">
          <div>© 2026 RINAU HEX S.A.S. · {t.rights}</div>
          <div>{t.ciiu}</div>
          <div className="flex gap-5">
            {t.legal.map((l,i)=>(<a key={i} href={l.h} className="hover:text-[#14B8A6] transition-colors">{l.l}</a>))}
          </div>
        </div>
      </div>
    </footer>
  );
}

Object.assign(window, { TopBar, Header, Hero, Features, About, Team, Partners, Contact, Footer });
