// Top sections: Nav + Hero + Marquee

const { useState: useS1, useEffect: useE1, useRef: useR1 } = React;

// ---------------- Nav ----------------
function Nav({ onCotizar }) {
  const [scrolled, setScrolled] = useS1(false);
  const { progress } = usePageScroll();
  useE1(() => {
    const h = () => setScrolled(window.scrollY > 20);
    window.addEventListener('scroll', h);
    return () => window.removeEventListener('scroll', h);
  }, []);
  return (
    <>
      {/* Global scroll progress bar at top of viewport */}
      <div style={{
        position: 'fixed', top: 0, left: 0, right: 0, height: 2, zIndex: 101,
        background: 'transparent', pointerEvents: 'none',
      }}>
        <div style={{
          height: '100%', width: `${progress * 100}%`,
          background: 'linear-gradient(90deg, var(--coral-deep), var(--coral))',
          boxShadow: '0 0 10px rgba(236,106,77,0.4)',
          transition: 'width 0.05s linear',
        }}/>
      </div>
    <nav style={{
      position: 'fixed', top: 20, left: '50%', transform: 'translateX(-50%)',
      zIndex: 100, width: 'min(1100px, calc(100vw - 32px))',
      display: 'flex', alignItems: 'center', justifyContent: 'space-between',
      padding: '10px 12px 10px 22px',
      background: scrolled ? 'rgba(250,246,242,0.85)' : 'rgba(250,246,242,0.55)',
      backdropFilter: 'blur(20px)', WebkitBackdropFilter: 'blur(20px)',
      border: '1px solid var(--line)',
      borderRadius: 999,
      boxShadow: scrolled ? '0 8px 32px -12px rgba(26,20,16,0.14)' : '0 2px 12px -4px rgba(26,20,16,0.06)',
      transition: 'all 0.3s ease',
    }}>
      <a href="#" style={{ display: 'flex', alignItems: 'center', textDecoration: 'none' }}>
        <HomliLogo height={26} isoColor="var(--coral)" wordColor="var(--ink)" />
      </a>
      <div style={{ display: 'flex', alignItems: 'center', gap: 28 }} className="nav-links">
        {[
          { l: 'Servicios', href: '#sec-0' },
          { l: 'Cotiza', href: '#sec-3' },
          { l: 'Cómo funciona', href: '#sec-1' },
          { l: 'Por qué Homli', href: '#sec-2' },
        ].map((item, i) => (
          <a key={i} href={item.href} style={{
            fontSize: 14, color: 'var(--ink-2)', textDecoration: 'none', fontWeight: 500,
            transition: 'color 0.2s',
          }} onMouseEnter={e => e.target.style.color = 'var(--coral)'} onMouseLeave={e => e.target.style.color = 'var(--ink-2)'}>{item.l}</a>
        ))}
      </div>
      <button onClick={onCotizar} style={{
        background: 'var(--ink)', color: 'var(--cream)', border: 'none',
        padding: '10px 18px', borderRadius: 999, fontSize: 13, fontWeight: 500,
        cursor: 'pointer', display: 'flex', alignItems: 'center', gap: 6,
        fontFamily: 'var(--font-body)',
        transition: 'transform 0.15s, background 0.2s',
      }}
      onMouseEnter={e => { e.currentTarget.style.transform = 'scale(1.03)'; e.currentTarget.style.background = 'var(--coral)'; }}
      onMouseLeave={e => { e.currentTarget.style.transform = 'scale(1)'; e.currentTarget.style.background = 'var(--ink)'; }}
      >
        WhatsApp
        <svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round">
          <path d="M5 12h14M13 5l7 7-7 7"/>
        </svg>
      </button>
      <style>{`
        @media (max-width: 720px) {
          .nav-links { display: none !important; }
        }
      `}</style>
    </nav>
    </>
  );
}

// ---------------- Hero Visual: real photo + WhatsApp bubbles ----------------
function HeroVisual() {
  const photoP = useMouseParallax(12);
  const bubbleP = useMouseParallax(24);
  return (
    <div className="hero-visual" style={{
      position: 'relative', width: '100%', maxWidth: 480, margin: '0 auto',
    }}>
      {/* Pulsing coral glow */}
      <div aria-hidden style={{
        position: 'absolute', inset: '-12%',
        background: 'radial-gradient(circle at 50% 50%, rgba(236,106,77,0.35), transparent 60%)',
        filter: 'blur(60px)',
        animation: 'heroPhotoGlow 6s ease-in-out infinite',
        pointerEvents: 'none',
      }}/>
      {/* Photo container */}
      <div style={{
        position: 'relative',
        aspectRatio: '4/5',
        borderRadius: 28,
        overflow: 'hidden',
        boxShadow: 'var(--shadow-lift)',
        transform: `translate(${photoP.x}px, ${photoP.y}px)`,
        transition: 'transform 0.4s cubic-bezier(0.2, 0.8, 0.2, 1)',
      }}>
        <img src="uploads/hogar_mas_calido.jpg" alt="Sala de estar cálida al atardecer con sofá crema y luz ámbar"
          style={{ width: '100%', height: '100%', objectFit: 'cover', display: 'block' }}/>
      </div>

      {/* Bubbles */}
      <div className="hero-bubbles" style={{
        position: 'absolute', inset: 0, pointerEvents: 'none',
        transform: `translate(${bubbleP.x}px, ${bubbleP.y}px)`,
        transition: 'transform 0.4s cubic-bezier(0.2, 0.8, 0.2, 1)',
      }}>
        {/* Bubble 1: user */}
        <div className="bubble b1" style={{
          position: 'absolute', top: '8%', left: '-6%',
          background: 'white', padding: '10px 14px',
          borderRadius: '14px 14px 14px 4px',
          boxShadow: '0 8px 24px -8px rgba(0,0,0,0.15)',
          display: 'flex', alignItems: 'center', gap: 8,
          fontSize: 13, color: 'var(--ink)', maxWidth: 240,
        }}>
          <div style={{
            width: 24, height: 24, borderRadius: '50%', background: 'var(--coral)',
            color: 'white', display: 'flex', alignItems: 'center', justifyContent: 'center',
            fontFamily: 'var(--font-display)', fontWeight: 600, fontSize: 12, flexShrink: 0,
          }}>C</div>
          <span>Hola Homli, necesito limpieza el sábado</span>
        </div>

        {/* Bubble 2: homli */}
        <div className="bubble b2" style={{
          position: 'absolute', top: '38%', right: '-4%',
          background: '#D9FDD3', padding: '10px 14px',
          borderRadius: '14px 14px 4px 14px',
          boxShadow: '0 8px 24px -8px rgba(0,0,0,0.15)',
          fontSize: 13, color: 'var(--ink)', maxWidth: 220,
          display: 'flex', alignItems: 'center', gap: 6,
        }}>
          <span>Listo, $95.000. ¿Confirmamos?</span>
          <svg width="14" height="10" viewBox="0 0 16 11" fill="none" style={{ flexShrink: 0 }}>
            <path d="M1 5.5L4.5 9L10 2" stroke="#34B7F1" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"/>
            <path d="M5 5.5L8.5 9L14 2" stroke="#34B7F1" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"/>
          </svg>
        </div>

        {/* Bubble 3: pin + text */}
        <div className="bubble b3" style={{
          position: 'absolute', bottom: '12%', left: '-8%',
          background: 'white', padding: '10px 14px',
          borderRadius: '14px 14px 14px 4px',
          boxShadow: '0 8px 24px -8px rgba(0,0,0,0.15)',
          display: 'flex', alignItems: 'center', gap: 8,
          fontSize: 13, color: 'var(--ink)', maxWidth: 240,
        }}>
          <svg width="14" height="14" viewBox="0 0 24 24" fill="var(--coral)" style={{ flexShrink: 0 }}>
            <path d="M12 2C8 2 5 5 5 9c0 5 7 13 7 13s7-8 7-13c0-4-3-7-7-7zm0 9.5a2.5 2.5 0 1 1 0-5 2.5 2.5 0 0 1 0 5z"/>
          </svg>
          <span>Camila va en camino, llega en 12 min</span>
        </div>
      </div>

      <style>{`
        @keyframes heroPhotoGlow {
          0%, 100% { transform: scale(1); opacity: 0.35; }
          50% { transform: scale(1.08); opacity: 0.5; }
        }
        @keyframes bubbleCycle1 {
          0%, 33.33% { opacity: 1; transform: translateY(0) scale(1); }
          37.78%, 100% { opacity: 0; transform: translateY(-6px) scale(0.97); }
        }
        @keyframes bubbleCycle2 {
          0%, 33.33% { opacity: 0; transform: translateY(6px) scale(0.97); }
          37.78%, 66.66% { opacity: 1; transform: translateY(0) scale(1); }
          71.11%, 100% { opacity: 0; transform: translateY(-6px) scale(0.97); }
        }
        @keyframes bubbleCycle3 {
          0%, 66.66% { opacity: 0; transform: translateY(6px) scale(0.97); }
          71.11%, 100% { opacity: 1; transform: translateY(0) scale(1); }
        }
        .bubble { transition: opacity 0.4s ease; }
        .b1 { animation: bubbleCycle1 9s ease-in-out infinite; }
        .b2 { animation: bubbleCycle2 9s ease-in-out infinite; }
        .b3 { animation: bubbleCycle3 9s ease-in-out infinite; }
        @media (max-width: 720px) {
          .hero-bubbles { display: none !important; }
        }
        @media (prefers-reduced-motion: reduce) {
          .b1, .b2, .b3 { animation: none !important; opacity: 1 !important; }
        }
      `}</style>
    </div>
  );
}

// ---------------- Hero ----------------
function Hero({ onCotizar, onWhatsapp, heroHeadline }) {
  const { scrollY } = usePageScroll();
  // Hero scroll-driven: as user scrolls down, copy fades + lifts, blobs drift
  const heroProg = Math.min(1, scrollY / 700);  // 0..1 over first 700px

  return (
    <section className="hero-sec" style={{
      position: 'relative',
      padding: '140px 32px 80px',
      display: 'flex', alignItems: 'center',
      overflow: 'hidden',
    }}>
      <div aria-hidden style={{
        position: 'absolute', top: -200, left: -200, width: 700, height: 700,
        background: 'radial-gradient(circle, rgba(236,106,77,0.18), transparent 60%)',
        filter: 'blur(40px)', pointerEvents: 'none',
        transform: `translate(${heroProg * 80}px, ${heroProg * 60}px) scale(${1 + heroProg * 0.3})`,
      }}/>
      <div aria-hidden style={{
        position: 'absolute', top: 100, right: -100, width: 500, height: 500,
        background: 'radial-gradient(circle, rgba(127,167,199,0.14), transparent 60%)',
        filter: 'blur(40px)', pointerEvents: 'none',
        transform: `translate(${-heroProg * 60}px, ${heroProg * 80}px)`,
      }}/>

      <div style={{
        maxWidth: 1280, margin: '0 auto', width: '100%',
        display: 'grid', gridTemplateColumns: '1.1fr 1fr', gap: 60,
        alignItems: 'center', position: 'relative', zIndex: 2,
      }} className="hero-grid">
        <div style={{
          maxWidth: 620,
          transform: `translateY(${heroProg * -40}px)`,
          opacity: 1 - heroProg * 0.5,
        }}>
          <Reveal delay={0}>
            <div style={{
              display: 'inline-flex', alignItems: 'center', gap: 10,
              padding: '6px 14px 6px 8px', background: 'var(--cream)',
              border: '1px solid var(--line)', borderRadius: 999,
              fontSize: 13, color: 'var(--ink-2)', marginBottom: 24,
              boxShadow: 'var(--shadow-card)',
            }}>
              <div style={{ display: 'flex', gap: 2 }}>
                {[0,1,2,3,4].map(i => (
                  <svg key={i} width="14" height="14" viewBox="0 0 24 24" fill="var(--sun)">
                    <path d="M12 2l3.09 6.26L22 9.27l-5 4.87 1.18 6.88L12 17.77l-6.18 3.25L7 14.14 2 9.27l6.91-1.01L12 2z"/>
                  </svg>
                ))}
              </div>
              <span style={{ fontWeight: 500 }}>4.9/5</span>
              <span style={{ color: 'var(--mute)' }}>· 5.000+ servicios prestados</span>
            </div>
          </Reveal>

          <Reveal delay={100}>
            <h1 style={{
              fontFamily: 'var(--font-display)', fontWeight: 500,
              fontSize: 'clamp(44px, 6vw, 78px)', lineHeight: 0.98,
              letterSpacing: '-0.035em', color: 'var(--ink)', marginBottom: 22,
            }}>
              Tu hogar en <em style={{
                fontStyle: 'italic', fontWeight: 400, color: 'var(--coral)',
                position: 'relative', display: 'inline-block',
              }}>
                buenas
                <svg style={{ position: 'absolute', bottom: -8, left: 0, width: '100%', height: 10 }} viewBox="0 0 200 10" preserveAspectRatio="none">
                  <path d="M 2 6 Q 50 2 100 6 T 198 6" fill="none" stroke="var(--coral)" strokeWidth="2" strokeLinecap="round" opacity="0.5" className="underline-draw"/>
                </svg>
              </em> manos.
            </h1>
          </Reveal>

          <Reveal delay={200}>
            <p style={{
              fontSize: 18, lineHeight: 1.55, color: 'var(--mute)',
              maxWidth: 480, marginBottom: 32, letterSpacing: '-0.01em',
            }}>
              Limpieza, fontanería, electricidad y mucho más, con técnicos verificados
              y una cotización por WhatsApp en minutos. Sin complicaciones.
            </p>
          </Reveal>

          <Reveal delay={300}>
            <div style={{ display: 'flex', gap: 12, flexWrap: 'wrap', alignItems: 'center' }}>
              <button onClick={onWhatsapp} style={{
                background: 'var(--coral)', color: 'white', border: 'none',
                padding: '16px 24px', borderRadius: 14, fontSize: 15, fontWeight: 500,
                cursor: 'pointer', display: 'flex', alignItems: 'center', gap: 10,
                fontFamily: 'var(--font-body)',
                boxShadow: '0 10px 30px -8px rgba(236,106,77,0.5), inset 0 1px 0 rgba(255,255,255,0.3)',
                transition: 'all 0.2s cubic-bezier(0.2, 0.8, 0.2, 1)',
              }}
              onMouseEnter={e => { e.currentTarget.style.transform = 'translateY(-2px) scale(1.02)'; e.currentTarget.style.boxShadow = '0 16px 40px -8px rgba(236,106,77,0.6), inset 0 1px 0 rgba(255,255,255,0.3)'; }}
              onMouseLeave={e => { e.currentTarget.style.transform = 'translateY(0) scale(1)'; e.currentTarget.style.boxShadow = '0 10px 30px -8px rgba(236,106,77,0.5), inset 0 1px 0 rgba(255,255,255,0.3)'; }}
              >
                <svg width="18" height="18" viewBox="0 0 24 24" fill="currentColor">
                  <path d="M17.472 14.382c-.297-.149-1.758-.867-2.03-.967-.273-.099-.471-.148-.67.15-.197.297-.767.966-.94 1.164-.173.199-.347.223-.644.075-.297-.15-1.255-.463-2.39-1.475-.883-.788-1.48-1.761-1.653-2.059-.173-.297-.018-.458.13-.606.134-.133.298-.347.446-.52.149-.174.198-.298.298-.497.099-.198.05-.371-.025-.52-.075-.149-.669-1.612-.916-2.207-.242-.579-.487-.5-.669-.51-.173-.008-.371-.01-.57-.01-.198 0-.52.074-.792.372-.272.297-1.04 1.016-1.04 2.479 0 1.462 1.065 2.875 1.213 3.074.149.198 2.096 3.2 5.077 4.487.709.306 1.262.489 1.694.625.712.227 1.36.195 1.871.118.571-.085 1.758-.719 2.006-1.413.248-.694.248-1.289.173-1.413-.074-.124-.272-.198-.57-.347m-5.421 7.403h-.004a9.87 9.87 0 01-5.031-1.378l-.361-.214-3.741.982.998-3.648-.235-.374a9.86 9.86 0 01-1.51-5.26c.001-5.45 4.436-9.884 9.888-9.884 2.64 0 5.122 1.03 6.988 2.898a9.825 9.825 0 012.893 6.994c-.003 5.45-4.437 9.884-9.885 9.884m8.413-18.297A11.815 11.815 0 0012.05 0C5.495 0 .16 5.335.157 11.892c0 2.096.547 4.142 1.588 5.945L.057 24l6.305-1.654a11.882 11.882 0 005.683 1.448h.005c6.554 0 11.89-5.335 11.893-11.893A11.821 11.821 0 0020.885 3.488"/>
                </svg>
                Cotizar por WhatsApp
              </button>
              <button onClick={onCotizar} style={{
                background: 'transparent', color: 'var(--ink)',
                border: '1px solid var(--line-2)',
                padding: '16px 22px', borderRadius: 14, fontSize: 15, fontWeight: 500,
                cursor: 'pointer', display: 'flex', alignItems: 'center', gap: 8,
                fontFamily: 'var(--font-body)',
                transition: 'all 0.2s',
              }}
              onMouseEnter={e => { e.currentTarget.style.background = 'var(--cream-2)'; e.currentTarget.style.borderColor = 'var(--ink)'; }}
              onMouseLeave={e => { e.currentTarget.style.background = 'transparent'; e.currentTarget.style.borderColor = 'var(--line-2)'; }}
              >
                Ver servicios
                <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round">
                  <path d="M6 9l6 6 6-6"/>
                </svg>
              </button>
            </div>
          </Reveal>
        </div>

        <div className="hero-visual-wrap" style={{
          transform: `translateY(${heroProg * 60}px)`,
          opacity: 1 - heroProg * 0.4,
        }}>
          <HeroVisual />
        </div>
      </div>

      <style>{`
        .hero-sec { min-height: 100vh; min-height: 100dvh; }
        @media (max-width: 860px) {
          .hero-grid { grid-template-columns: 1fr !important; }
        }
        @media (max-width: 720px) {
          .hero-visual-wrap { display: none; }
        }
        @media (max-width: 480px) {
          .hero-sec { padding: 116px 20px 64px !important; }
        }
        @keyframes drawline {
          from { stroke-dashoffset: 200; }
          to { stroke-dashoffset: 0; }
        }
        .underline-draw {
          stroke-dasharray: 200;
          stroke-dashoffset: 200;
          animation: drawline 1.2s ease-out 0.7s forwards;
        }
      `}</style>
    </section>
  );
}

// ---------------- Reveal helper ----------------
function Reveal({ children, delay = 0, as: Tag = 'div' }) {
  const [ref, inView] = useInView();
  return (
    <Tag ref={ref} style={{
      opacity: inView ? 1 : 0,
      transform: inView ? 'translateY(0)' : 'translateY(20px)',
      transition: `opacity 0.7s ease ${delay}ms, transform 0.7s cubic-bezier(0.2, 0.8, 0.2, 1) ${delay}ms`,
    }}>{children}</Tag>
  );
}

// ---------------- Marquee (replaces SocialProof) ----------------
function Marquee() {
  const services = ['Limpieza regular', 'Limpieza profunda', 'Cortinas y persianas', 'Fontanería', 'Cerrajería', 'Electricidad', 'Carpintería', 'Obra civil', 'Fumigación', 'Jardinería'];
  const benefits = ['Insumos incluidos', 'Garantía 48 horas', 'Técnicos verificados', 'Pago al finalizar', 'Cotización en minutos', 'Mismo equipo cada semana', 'Sin letra pequeña', 'Pago contraentrega'];

  const maskCss = {
    maskImage: 'linear-gradient(to right, transparent 0, black 8%, black 92%, transparent 100%)',
    WebkitMaskImage: 'linear-gradient(to right, transparent 0, black 8%, black 92%, transparent 100%)',
  };

  const Diamond = () => (
    <svg aria-hidden width="8" height="8" viewBox="0 0 10 10" style={{ margin: '0 28px', flexShrink: 0 }}>
      <rect x="1.5" y="1.5" width="7" height="7" transform="rotate(45 5 5)" fill="var(--coral)"/>
    </svg>
  );

  return (
    <section className="marquee-sec" style={{
      padding: '32px 0',
      background: 'var(--ink)',
      borderTop: '1px solid var(--line-2)',
      borderBottom: '1px solid var(--line-2)',
      overflow: 'hidden',
    }}>
      {/* Row 1: services */}
      <div style={{ ...maskCss, overflow: 'hidden', marginBottom: 18 }}>
        <div className="mq-track mq-row1" style={{
          display: 'flex', width: 'max-content',
        }}>
          {[...services, ...services].map((s, i) => (
            <div key={i} style={{ display: 'flex', alignItems: 'center' }}>
              <span style={{
                fontFamily: 'var(--font-display)', fontSize: 22, fontWeight: 400,
                color: 'var(--cream)', letterSpacing: '-0.01em', whiteSpace: 'nowrap',
              }}>{s}</span>
              <Diamond />
            </div>
          ))}
        </div>
      </div>

      {/* Row 2: benefits */}
      <div style={{ ...maskCss, overflow: 'hidden' }}>
        <div className="mq-track mq-row2" style={{
          display: 'flex', width: 'max-content',
        }}>
          {[...benefits, ...benefits].map((b, i) => (
            <div key={i} style={{ display: 'flex', alignItems: 'center' }}>
              <span style={{
                fontFamily: 'var(--font-body)', fontSize: 13, fontWeight: 500,
                color: 'rgba(250,246,242,0.6)', letterSpacing: '0.12em', textTransform: 'uppercase',
                whiteSpace: 'nowrap',
              }}>{b}</span>
              <Diamond />
            </div>
          ))}
        </div>
      </div>

      <style>{`
        @keyframes mqRow1 { 0% { transform: translateX(0);} 100% { transform: translateX(-50%);} }
        @keyframes mqRow2 { 0% { transform: translateX(-50%);} 100% { transform: translateX(0);} }
        .mq-row1 { animation: mqRow1 40s linear infinite; }
        .mq-row2 { animation: mqRow2 50s linear infinite; }
        .marquee-sec:hover .mq-track { animation-play-state: paused; }
        @media (prefers-reduced-motion: reduce) {
          .mq-row1, .mq-row2 { animation: none !important; }
        }
      `}</style>
    </section>
  );
}

Object.assign(window, { Nav, Hero, HeroVisual, Reveal, Marquee });
