// v70 — Gradient Mesh (Stripe-like, con grain + counters + gradient borders)
const V70Mesh = ({ data, lang, setLang = () => {} }) => {
  const { useState, useEffect, useRef } = React;
  const t = (es, en) => lang === 'es' ? es : en;

  // Animated counters
  const [counts, setCounts] = useState({ projects: 0, years: 0, stack: 0 });
  const targets = { projects: data.projects.length, years: new Date().getFullYear() - 2019, stack: data.stack.flatMap(s => s.items).length };
  const hasAnimated = useRef(false);

  useEffect(() => {
    const observer = new IntersectionObserver(entries => {
      entries.forEach(e => {
        if (e.isIntersecting && !hasAnimated.current) {
          hasAnimated.current = true;
          const duration = 1800;
          const steps = 60;
          const interval = duration / steps;
          let step = 0;
          const timer = setInterval(() => {
            step++;
            const progress = step / steps;
            const ease = 1 - Math.pow(1 - progress, 3);
            setCounts({
              projects: Math.round(targets.projects * ease),
              years: Math.round(targets.years * ease),
              stack: Math.round(targets.stack * ease),
            });
            if (step >= steps) clearInterval(timer);
          }, interval);
        }
      });
    }, { threshold: 0.3 });
    const el = document.getElementById('mesh-stats');
    if (el) observer.observe(el);
    return () => observer.disconnect();
  }, []);

  return (
    <div style={{ background: '#060b18', color: '#e2e8f0', minHeight: '100vh', fontFamily: '"Inter", system-ui, sans-serif', position: 'relative', overflowX: 'hidden' }}>
      <style>{`
        @import url('https://fonts.googleapis.com/css2?family=Inter:wght@200;300;400;600;700;900&display=swap');

        /* Grain texture overlay */
        .ms-grain {
          position: fixed; inset: 0; z-index: 1; pointer-events: none; opacity: 0.035;
          background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 512 512' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='g'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.75' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23g)'/%3E%3C/svg%3E");
          background-size: 256px;
        }

        /* Animated mesh background orbs */
        @keyframes msOrb1 {
          0%,100% { transform: scale(1) translate(0,0); }
          33%  { transform: scale(1.3) translate(8%,6%); }
          66%  { transform: scale(0.9) translate(-5%,10%); }
        }
        @keyframes msOrb2 {
          0%,100% { transform: scale(1) translate(0,0); }
          50%  { transform: scale(1.4) translate(-10%,-8%); }
        }
        @keyframes msOrb3 {
          0%,100% { transform: scale(1) translate(0,0); }
          40%  { transform: scale(1.2) translate(6%,-12%); }
          80%  { transform: scale(0.95) translate(-4%,5%); }
        }
        .ms-orb {
          position: fixed; border-radius: 50%; filter: blur(100px);
          pointer-events: none; mix-blend-mode: screen; opacity: 0.45; z-index: 0;
        }

        /* Gradient border card */
        .ms-card {
          position: relative; border-radius: 20px; padding: 1px;
          background: linear-gradient(135deg, rgba(99,102,241,0.4), rgba(168,85,247,0.2), rgba(99,102,241,0.1));
          transition: 0.3s;
        }
        .ms-card-inner {
          background: rgba(10,14,30,0.85);
          border-radius: 19px;
          padding: 40px;
          height: 100%;
          backdrop-filter: blur(20px);
          -webkit-backdrop-filter: blur(20px);
        }
        .ms-card:hover {
          background: linear-gradient(135deg, rgba(99,102,241,0.8), rgba(168,85,247,0.5), rgba(236,72,153,0.4));
          transform: translateY(-4px);
          box-shadow: 0 20px 60px rgba(99,102,241,0.2), 0 8px 20px rgba(0,0,0,0.4);
        }
        .ms-card:hover .ms-card-inner {
          background: rgba(10,14,30,0.9);
        }

        /* Subtle shimmer on cards */
        @keyframes msShimmer {
          from { background-position: 200% center; }
          to { background-position: -200% center; }
        }
        .ms-tag {
          display: inline-flex; align-items: center;
          padding: 5px 14px; border-radius: 100px;
          font-size: 13px; font-weight: 500;
          background: rgba(99,102,241,0.15);
          border: 1px solid rgba(99,102,241,0.25);
          color: #a5b4fc;
          transition: 0.2s;
        }
        .ms-tag:hover { background: rgba(99,102,241,0.25); border-color: rgba(99,102,241,0.5); }

        .ms-btn {
          display: inline-flex; align-items: center; gap: 8px;
          padding: 14px 28px; border-radius: 12px; font-size: 15px; font-weight: 600;
          text-decoration: none; transition: 0.2s; letter-spacing: 0.2px; cursor: pointer;
        }

        .ms-section-label {
          font-size: 12px; font-weight: 600; letter-spacing: 3px;
          text-transform: uppercase; color: rgba(165,180,252,0.6); margin-bottom: 16px;
        }

        .ms-stat-num {
          font-size: 64px; font-weight: 900; line-height: 1;
          background: linear-gradient(135deg, #fff, #a5b4fc);
          background-clip: text; -webkit-background-clip: text; -webkit-text-fill-color: transparent;
        }

        .ms-divider {
          height: 1px;
          background: linear-gradient(90deg, transparent, rgba(99,102,241,0.3), transparent);
          margin: 48px 0;
        }

        @keyframes msPulse {
          0%,100% { opacity: 0.6; }
          50% { opacity: 1; }
        }
        .ms-dot { width: 8px; height: 8px; border-radius: 50%; background: #6366f1; animation: msPulse 2s ease infinite; }
      `}</style>

      {/* Grain */}
      <div className="ms-grain" />

      {/* Mesh orbs */}
      <div className="ms-orb" style={{ width: '70vw', height: '70vw', background: '#4338ca', top: '-20%', right: '-15%', animation: 'msOrb1 20s ease-in-out infinite' }} />
      <div className="ms-orb" style={{ width: '60vw', height: '60vw', background: '#7c3aed', bottom: '-15%', left: '-15%', animation: 'msOrb2 25s ease-in-out infinite' }} />
      <div className="ms-orb" style={{ width: '50vw', height: '50vw', background: '#be185d', top: '40%', left: '30%', animation: 'msOrb3 18s ease-in-out infinite' }} />

      <div style={{ position: 'relative', zIndex: 2, maxWidth: 1280, margin: '0 auto', padding: '0 24px' }}>

        {/* Nav */}
        <nav style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', padding: '24px 0', borderBottom: '1px solid rgba(99,102,241,0.15)' }}>
          <div style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
            <div className="ms-dot" />
            <span style={{ fontWeight: 700, fontSize: 15, letterSpacing: 0.5, color: '#f1f5f9' }}>
              {data.nameShort || data.name.split(' ').map(w => w[0]).join('')}
            </span>
          </div>
          <div style={{ display: 'flex', alignItems: 'center', gap: 20 }}>
            <span style={{ fontSize: 13, color: 'rgba(255,255,255,0.4)', fontWeight: 500 }}>{data.location}</span>
            <LangToggle lang={lang} setLang={setLang} theme="dark" />
          </div>
        </nav>

        {/* Hero */}
        <section style={{ padding: '100px 0 80px', maxWidth: 880 }}>
          <div className="ms-section-label">Portfolio — {new Date().getFullYear()}</div>
          <h1 style={{ fontSize: 'clamp(48px, 7vw, 100px)', fontWeight: 900, margin: '0 0 24px', lineHeight: 1,
            letterSpacing: -2, color: '#f8fafc' }}>
            {data.name}
          </h1>
          <div style={{ fontSize: 'clamp(20px, 2.5vw, 28px)', fontWeight: 300, color: 'rgba(165,180,252,0.9)',
            marginBottom: 24, letterSpacing: -0.5 }}>
            {t(data.role_es, data.role_en)}
          </div>
          <p style={{ fontSize: 18, lineHeight: 1.75, color: 'rgba(226,232,240,0.65)', maxWidth: 600, marginBottom: 48 }}>
            {t(data.tagline_es, data.tagline_en)}
          </p>
          <div style={{ display: 'flex', gap: 12, flexWrap: 'wrap' }}>
            <a href={`mailto:${data.email}`} className="ms-btn"
              style={{ background: 'linear-gradient(135deg, #6366f1, #8b5cf6)', color: '#fff',
                boxShadow: '0 4px 20px rgba(99,102,241,0.4)' }}>
              {t('Contactar', 'Get in Touch')} →
            </a>
            <a href="#projects" className="ms-btn"
              style={{ background: 'rgba(255,255,255,0.06)', color: '#e2e8f0', border: '1px solid rgba(255,255,255,0.1)' }}>
              {t('Ver Proyectos', 'View Projects')}
            </a>
          </div>
        </section>

        <div className="ms-divider" />

        {/* Stats */}
        <section id="mesh-stats" style={{ display: 'grid', gridTemplateColumns: 'repeat(auto-fit, minmax(180px, 1fr))', gap: 32, marginBottom: 0 }}>
          {[
            { num: counts.projects, label: t('Proyectos', 'Projects'), suffix: '+' },
            { num: counts.years, label: t('Años de exp.', 'Years exp.'), suffix: '+' },
            { num: counts.stack, label: t('Tecnologías', 'Technologies'), suffix: '' },
          ].map((stat, i) => (
            <div key={i} style={{ padding: '32px 0' }}>
              <div className="ms-stat-num">{stat.num}{stat.suffix}</div>
              <div style={{ fontSize: 14, color: 'rgba(226,232,240,0.5)', marginTop: 8, fontWeight: 500 }}>{stat.label}</div>
            </div>
          ))}
        </section>

        <div className="ms-divider" />

        {/* About + Stack */}
        <section style={{ display: 'grid', gridTemplateColumns: 'repeat(auto-fit, minmax(360px, 1fr))', gap: 24, marginBottom: 80 }}>
          <div className="ms-card">
            <div className="ms-card-inner">
              <div className="ms-section-label">{t('Sobre Mí', 'About')}</div>
              <h2 style={{ fontSize: 26, fontWeight: 700, marginBottom: 20, color: '#f1f5f9', letterSpacing: -0.5 }}>
                {t('Hola, soy', 'Hi, I\'m')} {data.name.split(' ')[0]}
              </h2>
              {(lang === 'es' ? data.about_es : data.about_en).map((p, i) => (
                <p key={i} style={{ fontSize: 15, lineHeight: 1.75, color: 'rgba(226,232,240,0.7)', marginBottom: 12 }}>{p}</p>
              ))}
            </div>
          </div>

          <div className="ms-card">
            <div className="ms-card-inner">
              <div className="ms-section-label">Stack</div>
              <h2 style={{ fontSize: 26, fontWeight: 700, marginBottom: 24, color: '#f1f5f9', letterSpacing: -0.5 }}>
                {t('Tecnologías', 'Technologies')}
              </h2>
              {data.stack.map(g => (
                <div key={g.group_es} style={{ marginBottom: 20 }}>
                  <div style={{ fontSize: 11, fontWeight: 600, letterSpacing: 2, textTransform: 'uppercase',
                    color: 'rgba(165,180,252,0.5)', marginBottom: 10 }}>
                    {t(g.group_es, g.group_en)}
                  </div>
                  <div style={{ display: 'flex', flexWrap: 'wrap', gap: 8 }}>
                    {g.items.map(item => <span key={item} className="ms-tag">{item}</span>)}
                  </div>
                </div>
              ))}
            </div>
          </div>
        </section>

        {/* Projects */}
        <section id="projects" style={{ marginBottom: 80 }}>
          <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: 40 }}>
            <div>
              <div className="ms-section-label">{t('Portafolio', 'Portfolio')}</div>
              <h2 style={{ fontSize: 40, fontWeight: 900, margin: 0, color: '#f1f5f9', letterSpacing: -1 }}>
                {t('Proyectos', 'Projects')}
              </h2>
            </div>
          </div>
          <div style={{ display: 'grid', gridTemplateColumns: 'repeat(auto-fit, minmax(340px, 1fr))', gap: 24 }}>
            {data.projects.map((p, i) => (
              <div key={p.id} className="ms-card">
                <div className="ms-card-inner">
                  <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'flex-start', marginBottom: 16 }}>
                    <span className="ms-tag">{t(p.kind_es, p.kind_en)}</span>
                    <span style={{ fontSize: 12, color: 'rgba(165,180,252,0.5)', fontWeight: 600 }}>{p.year}</span>
                  </div>
                  <h3 style={{ fontSize: 22, fontWeight: 700, margin: '0 0 12px', color: '#f1f5f9', letterSpacing: -0.3, lineHeight: 1.2 }}>
                    {t(p.name_es, p.name_en)}
                  </h3>
                  <p style={{ fontSize: 14, lineHeight: 1.7, color: 'rgba(226,232,240,0.6)', marginBottom: 20 }}>
                    {t(p.short_es, p.short_en)}
                  </p>
                  <div style={{ display: 'flex', flexWrap: 'wrap', gap: 6 }}>
                    {p.tags.map(tag => <span key={tag} className="ms-tag" style={{ fontSize: 12 }}>{tag}</span>)}
                  </div>
                </div>
              </div>
            ))}
          </div>
        </section>

        <div className="ms-divider" />

        {/* Footer / CTA */}
        <footer style={{ padding: '80px 0 120px', textAlign: 'center' }}>
          <div className="ms-section-label" style={{ textAlign: 'center' }}>{t('Contacto', 'Contact')}</div>
          <h2 style={{ fontSize: 'clamp(36px, 5vw, 64px)', fontWeight: 900, margin: '12px 0 20px',
            color: '#f8fafc', letterSpacing: -2, lineHeight: 1 }}>
            {t('Construyamos algo increíble', "Let's build something great")}
          </h2>
          <p style={{ fontSize: 17, color: 'rgba(226,232,240,0.5)', marginBottom: 40, maxWidth: 420, margin: '0 auto 40px' }}>
            {data.email}
          </p>
          <div style={{ display: 'flex', justifyContent: 'center', gap: 12, flexWrap: 'wrap' }}>
            <a href={`mailto:${data.email}`} className="ms-btn"
              style={{ background: 'linear-gradient(135deg, #6366f1, #8b5cf6)', color: '#fff',
                boxShadow: '0 4px 24px rgba(99,102,241,0.45)' }}>
              {t('Enviar mensaje', 'Send a message')} →
            </a>
            <a href={`https://linkedin.com/in/${data.linkedin}`} target="_blank" rel="noopener noreferrer" className="ms-btn"
              style={{ background: 'rgba(255,255,255,0.06)', color: '#e2e8f0', border: '1px solid rgba(255,255,255,0.1)' }}>
              LinkedIn
            </a>
          </div>
        </footer>

      </div>
    </div>
  );
};
window.V70Mesh = V70Mesh;
