
const { useState, useEffect, useRef } = React;

// ─── Style tokens (tipografía movida a CSS: clases .t-h2, .t-h3, .t-body, etc.) ──
const S = {
  body: {},
  bodyDark: {},
  bodyInv: {},
  caps: {},
  capsGold: {},
  h2: {},
  h2inv: {},
  h3: {},
  serif: {},
};

// ─── Utilities ───────────────────────────────────────────────

function useScrollIn(delay = 0) {
  const ref = useRef(null);
  useEffect(() => {
    const el = ref.current;
    if (!el) return;
    el.style.opacity = '0';
    el.style.transform = 'translateY(28px)';
    el.style.transition = `opacity 0.75s ease ${delay}ms, transform 0.75s ease ${delay}ms`;
    const obs = new IntersectionObserver(([e]) => {
      if (e.isIntersecting) {
        el.style.opacity = '1';
        el.style.transform = 'translateY(0)';
        obs.disconnect();
      }
    }, { threshold: 0.1 });
    obs.observe(el);
    return () => obs.disconnect();
  }, []);
  return ref;
}

function ImgPlaceholder({ height = 300, label = 'imagen', style = {}, dark = false }) {
  return (
    <div style={{
      width: '100%', height,
      background: dark
        ? 'repeating-linear-gradient(135deg, #2e2416 0px, #2e2416 2px, #261d0f 2px, #261d0f 14px)'
        : 'repeating-linear-gradient(135deg, #EDE5D8 0px, #EDE5D8 2px, #F5F0E8 2px, #F5F0E8 14px)',
      display: 'flex', alignItems: 'center', justifyContent: 'center',
      color: dark ? 'rgba(255,255,255,0.25)' : '#A09180',
      fontFamily: 'monospace', fontSize: 11, letterSpacing: '0.08em',
      ...style
    }}>[ {label} ]</div>
  );
}

function Label({ children, style = {}, light = false }) {
  return (
    <div style={{
      fontFamily: 'var(--font-sans)', fontSize: 10, fontWeight: 600,
      letterSpacing: '0.2em', textTransform: 'uppercase',
      color: light ? 'rgba(196,169,107,0.9)' : 'var(--gold)',
      ...style
    }}>{children}</div>
  );
}

function GoldLine({ style = {} }) {
  return <div style={{ width: 40, height: 1, background: 'var(--gold)', ...style }} />;
}

function BtnPrimary({ children, onClick, href, style = {} }) {
  const base = {
    display: 'inline-flex', alignItems: 'center', gap: 8,
    background: 'var(--gold)', color: '#FAF7F2',
    fontFamily: 'var(--font-sans)', fontSize: 14, fontWeight: 600,
    letterSpacing: '0.15em', textTransform: 'uppercase', padding: '15px 32px',
    border: 'none', cursor: 'pointer', textDecoration: 'none',
    transition: 'opacity 0.2s, transform 0.2s', ...style
  };
  const handlers = {
    onMouseEnter: e => { e.currentTarget.style.opacity = '0.88'; },
    onMouseLeave: e => { e.currentTarget.style.opacity = '1'; },
  };
  if (href) return <a href={href} target="_blank" rel="noopener" style={base} {...handlers}>{children}</a>;
  return <button onClick={onClick} style={base} {...handlers}>{children}</button>;
}

function BtnOutline({ children, onClick, style = {}, light = false }) {
  return (
    <button onClick={onClick} style={{
      display: 'inline-flex', alignItems: 'center', gap: 8,
      background: 'transparent',
      color: light ? '#FAF7F2' : 'var(--gold)',
      fontFamily: 'var(--font-sans)', fontSize: 14, fontWeight: 600,
      letterSpacing: '0.15em', textTransform: 'uppercase', padding: '14px 32px',
      border: `1px solid ${light ? 'rgba(255,255,255,0.5)' : 'var(--gold)'}`,
      cursor: 'pointer', transition: 'all 0.2s', ...style
    }}
      onMouseEnter={e => { e.currentTarget.style.background = light ? 'rgba(255,255,255,0.08)' : 'var(--gold-pale)'; }}
      onMouseLeave={e => { e.currentTarget.style.background = 'transparent'; }}
    >{children}</button>
  );
}

// ─── NavBar ──────────────────────────────────────────────────

function NavBar({ currentPage, setCurrentPage }) {
  const [menuOpen, setMenuOpen] = useState(false);
  const [scrolled, setScrolled] = useState(false);

  useEffect(() => {
    const h = () => setScrolled(window.scrollY > 50);
    window.addEventListener('scroll', h);
    return () => window.removeEventListener('scroll', h);
  }, []);

  useEffect(() => {
    document.body.style.overflow = menuOpen ? 'hidden' : '';
    return () => { document.body.style.overflow = ''; };
  }, [menuOpen]);

  const items = [
    { id: 'treatments', label: 'Tratamientos' },
    { id: 'results', label: 'Resultados' },
    { id: 'about', label: 'Nosotros' },
    { id: 'experience', label: 'Experiencia LANA' },
    { id: 'contact', label: 'Contacto' },
  ];

  const go = (id) => { setCurrentPage(id); setMenuOpen(false); };
  const waLink = `https://wa.me/543413348415?text=${encodeURIComponent('Hola, quisiera reservar una consulta en LANA')}`;

  return (
    <>
      <nav aria-label="Navegación principal" style={{
        position: 'fixed', top: 0, left: 0, right: 0, zIndex: 1000,
        background: scrolled ? 'rgba(250,247,242,0.96)' : 'transparent',
        backdropFilter: scrolled ? 'blur(12px)' : 'none',
        boxShadow: scrolled ? '0 1px 0 rgba(139,107,61,0.1)' : 'none',
        transition: 'all 0.35s', padding: '0 max(40px, 5vw)',
        height: 70, display: 'flex', alignItems: 'center', justifyContent: 'space-between',
      }}>
        <button onClick={() => go('home')} style={{ background: 'none', border: 'none', cursor: 'pointer', padding: 0, lineHeight: 0 }}>
          <img src="uploads/logo.png" alt="LANA" style={{ height: 56, width: 56, objectFit: 'contain' }} />
        </button>

        {/* Desktop */}
        <div className="nav-desktop" style={{ display: 'flex', gap: 28, alignItems: 'center' }}>
          {items.map(it => (
            <button key={it.id} onClick={() => go(it.id)} style={{
              background: 'none', border: 'none', cursor: 'pointer',
              fontFamily: 'var(--font-sans)', fontSize: 12, fontWeight: 500,
              letterSpacing: '0.12em', textTransform: 'uppercase',
              color: currentPage === it.id ? 'var(--gold)' : 'var(--dark)',
              borderBottom: currentPage === it.id ? '1px solid var(--gold)' : '1px solid transparent',
              paddingBottom: 2, transition: 'color 0.2s',
            }}>{it.label}</button>
          ))}
          <BtnPrimary href={waLink} style={{ fontSize: 14, padding: '10px 20px', marginLeft: 8 }}>
            <svg width="14" height="14" 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 00-3.48-8.413z" /></svg>
            Reservar consulta
          </BtnPrimary>
        </div>

        {/* Mobile toggle */}
        <button className="nav-mobile-btn" onClick={() => setMenuOpen(v => !v)} style={{
          display: 'none', background: 'none', border: 'none', cursor: 'pointer',
          color: 'var(--dark)', fontSize: 22, lineHeight: 1, padding: 4,
        }}>
          {menuOpen ? '✕' : '☰'}
        </button>
      </nav>

      {/* Mobile menu */}
      <div style={{
        position: 'fixed', top: 70, left: 0, right: 0, bottom: 0,
        background: '#FAF7F2', zIndex: 999, overflowY: 'auto',
        display: menuOpen ? 'flex' : 'none',
        flexDirection: 'column', alignItems: 'center', justifyContent: 'center', gap: 36,
      }}>
        {[{ id: 'home', label: 'Inicio' }, ...items].map(it => (
          <button key={it.id} onClick={() => go(it.id)} style={{
            background: 'none', border: 'none', cursor: 'pointer',
            fontFamily: 'var(--font-serif)', fontSize: 30, fontWeight: 400, fontStyle: 'italic',
            color: currentPage === it.id ? 'var(--gold)' : 'var(--dark)',
          }}>{it.label}</button>
        ))}
        <BtnPrimary href={waLink} style={{ marginTop: 8 }}>
          Reservar consulta
        </BtnPrimary>
      </div>
    </>
  );
}

// ─── Footer ──────────────────────────────────────────────────

function Footer({ setCurrentPage }) {
  return (
    <footer style={{ background: 'var(--dark)', color: 'rgba(255,255,255,0.6)', fontFamily: 'var(--font-sans)', fontSize: 12 }}>
      <div style={{ maxWidth: 1100, margin: '0 auto', padding: '72px max(40px,5vw) 48px', display: 'grid', gridTemplateColumns: 'repeat(auto-fit, minmax(180px, 1fr))', gap: 56 }}>
        <div>
          <img src="uploads/logo.png" alt="LANA" style={{ height: 48, marginBottom: 20 }} />
          <p style={{ lineHeight: 1.9, color: 'rgba(255,255,255,0.4)', fontSize: 14, maxWidth: 220 }}>Medicina estética de autor. Resultados naturales con protocolo médico personalizado.</p>
        </div>
        <div>
          <div style={{ color: 'rgba(255,255,255,0.9)', fontWeight: 600, letterSpacing: '0.15em', textTransform: 'uppercase', fontSize: 14, marginBottom: 20 }}>Páginas</div>
          {[['treatments', 'Tratamientos'], ['results', 'Resultados'], ['about', 'Nosotros'], ['experience', 'Experiencia LANA'], ['contact', 'Contacto']].map(([id, label]) => (
            <button key={id} onClick={() => setCurrentPage(id)} style={{ display: 'block', background: 'none', border: 'none', cursor: 'pointer', color: 'rgba(255, 255, 255, 1)', fontFamily: 'var(--font-sans)', fontSize: 12, marginBottom: 10, padding: 0, textAlign: 'left', transition: 'color 0.2s' }}
              onMouseEnter={e => e.currentTarget.style.color = 'var(--gold-light)'}
              onMouseLeave={e => e.currentTarget.style.color = 'rgba(255,255,255,0.5)'}
            >{label}</button>
          ))}
        </div>
        <div>
          <div style={{ color: 'rgba(255,255,255,0.9)', fontWeight: 600, letterSpacing: '0.15em', textTransform: 'uppercase', fontSize: 16, marginBottom: 20 }}>Contacto</div>
          <address style={{ fontStyle: 'normal', color: 'rgba(255,255,255,0.5)', lineHeight: 2.1 }}>
            CONSULTORIOS ANDA<br />Paraguay 1526<br />Rosario, Santa Fe, Argentina<br />
            <a href="https://wa.me/543413348415" style={{ color: 'var(--white)', textDecoration: 'none' }}>+54 341 334-8415</a>
          </address>
        </div>
        <div>
          <div style={{ color: 'rgba(255, 255, 255, 1)', fontWeight: 600, letterSpacing: '0.15em', textTransform: 'uppercase', fontSize: 16, marginBottom: 20 }}>Horarios</div>
          <div style={{ color: 'rgba(255, 255, 255, 1)', lineHeight: 2.1 }}>
            Lun — Vie<br />
            9:00 — 19:00<br />
            Sábados con turno
          </div>
        </div>
      </div>
      <div style={{ borderTop: '1px solid rgba(255,255,255,0.07)', padding: '24px max(40px,5vw)', textAlign: 'center', color: 'rgba(255, 255, 255, 1)', fontSize: 14, letterSpacing: '0.08em' }}>
        © {new Date().getFullYear()} LANA Medicina Estética — Rosario, Argentina
      </div>
    </footer>
  );
}

// ─── Sticky WhatsApp ──────────────────────────────────────────

function WhatsAppButton() {
  return (
    <a href={`https://wa.me/543413348415?text=${encodeURIComponent('Hola, quisiera reservar una consulta en LANA')}`}
      target="_blank" rel="noopener"
      title="Escribinos por WhatsApp"
      style={{
        position: 'fixed', bottom: 28, right: 28, zIndex: 9000,
        width: 56, height: 56, borderRadius: '50%', background: '#25D366',
        display: 'flex', alignItems: 'center', justifyContent: 'center',
        boxShadow: '0 4px 24px rgba(37,211,102,0.35)', textDecoration: 'none',
        transition: 'transform 0.25s, box-shadow 0.25s',
      }}
      onMouseEnter={e => { e.currentTarget.style.transform = 'scale(1.1)'; e.currentTarget.style.boxShadow = '0 6px 28px rgba(37,211,102,0.5)'; }}
      onMouseLeave={e => { e.currentTarget.style.transform = 'scale(1)'; e.currentTarget.style.boxShadow = '0 4px 24px rgba(37,211,102,0.35)'; }}
    >
      <svg width="26" height="26" viewBox="0 0 24 24" fill="white"><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 00-3.48-8.413z" /></svg>
    </a>
  );
}

// ─── Section wrapper ──────────────────────────────────────────

function Section({ children, style = {} }) {
  return (
    <section style={{ padding: '96px max(40px,5vw)', maxWidth: 1100, margin: '0 auto', ...style }}>
      {children}
    </section>
  );
}

Object.assign(window, {
  useScrollIn, ImgPlaceholder, Label, GoldLine,
  S, BtnPrimary, BtnOutline, NavBar, Footer, WhatsAppButton, Section
});
