/* global React */

// =====================================================================
// SHARED — real navigation chrome used by every page.
// SiteNav links between actual .html files (no SPA, no design canvas).
// SitePageHero is the default editorial header for interior pages.
// =====================================================================

// =====================================================================
// AMBROSIO VOICE — endpoint público de callback (Ambrosio Voice service).
//
// Esta URL apunta al tunnel de Cloudflare Quick Tunnel del servicio
// ambrosio-voice (Ubuntu 192.168.0.165 → puerto interno + cloudflared).
// La URL CAMBIA cuando se reinicia el tunnel — actualizar aquí cuando
// pase. Solución definitiva: Named Tunnel `ambrosio.comfortek.es`.
//
// Spec completa: AMBROSIO_WEB_CALLBACK_INTEGRATION.md
// =====================================================================
const AMBROSIO_CALLBACK_URL = 'https://hamburg-nirvana-spring-immigration.trycloudflare.com/web/callback-request';

const NAV_ITEMS = [
  { href: 'index.html',       label: 'Home',         key: 'home' },
  { href: 'servicios.html',   label: 'Servicios',    key: 'servicios' },
  { href: 'ia.html',          label: 'IA',           key: 'ia' },
  { href: 'proyectos.html',   label: 'Proyectos',    key: 'proyectos' },
  { href: 'tecnologias.html', label: 'Tecnologías',  key: 'tecnologias' },
  { href: 'proceso.html',     label: 'Proceso',      key: 'proceso' },
  { href: 'nosotros.html',    label: 'Nosotros',     key: 'nosotros' },
  { href: 'contacto.html',    label: 'Contacto',     key: 'contacto' },
];

// SiteNav — used at the top of every page. Highlights the current page.
// Sticks to the top with a translucent dark blur once the user scrolls.
const SiteNav = ({ active = 'home', overlay = false }) => {
  const [scrolled, setScrolled] = React.useState(false);
  const [open, setOpen] = React.useState(false);

  React.useEffect(() => {
    const onScroll = () => setScrolled(window.scrollY > 24);
    onScroll();
    window.addEventListener('scroll', onScroll, { passive: true });
    return () => window.removeEventListener('scroll', onScroll);
  }, []);

  const bg = overlay && !scrolled
    ? 'transparent'
    : 'rgba(7,7,10,0.78)';
  const border = (overlay && !scrolled) ? 'transparent' : 'var(--line)';

  return (
    <header style={{
      position: overlay ? 'absolute' : 'sticky',
      top: 0, left: 0, right: 0, zIndex: 60,
      background: bg,
      backdropFilter: bg === 'transparent' ? 'none' : 'blur(20px) saturate(140%)',
      WebkitBackdropFilter: bg === 'transparent' ? 'none' : 'blur(20px) saturate(140%)',
      borderBottom: `1px solid ${border}`,
      transition: 'background .25s, border-color .25s',
    }}>
      <div className="site-nav-row" style={{
        maxWidth: 1320, margin: '0 auto',
        padding: '18px clamp(20px, 4vw, 56px)',
        display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: 24,
      }}>
        <a href="index.html" className="site-nav-logo-link" style={{ textDecoration: 'none', display: 'inline-flex' }}>
          <window.Logo size={44} withText={true} tagline={false} />
        </a>

        <nav className="site-nav-desktop" style={{ display: 'flex', gap: 32 }}>
          {NAV_ITEMS.slice(1).map(it => (
            <a key={it.key} href={it.href} aria-current={active === it.key ? 'page' : undefined}
              style={{
                color: active === it.key ? 'var(--text-0)' : 'var(--text-1)',
                textDecoration: 'none', fontSize: 14, fontWeight: 400,
                position: 'relative', padding: '4px 0',
              }}>
              {it.label}
              {active === it.key && (
                <span style={{
                  position: 'absolute', left: 0, right: 0, bottom: -2, height: 2,
                  background: 'var(--brand-orange)',
                  boxShadow: '0 0 8px rgba(255,106,26,0.6)',
                }} />
              )}
            </a>
          ))}
        </nav>

        <div className="site-nav-actions" style={{ display: 'flex', gap: 12, alignItems: 'center', flexShrink: 0 }}>
          <span className="mono site-nav-phone" style={{ fontSize: 12, color: 'var(--text-2)', whiteSpace: 'nowrap' }}>+34 610 246 360</span>
          <button type="button" onClick={() => window.openCallbackModal && window.openCallbackModal()} className="btn-ambrosio site-nav-cta" style={{ padding: '10px 16px 10px 12px', fontSize: 13, gap: 10, border: 'none' }}>
            <span className="pulse" style={{ width: 22, height: 22 }}>
              <svg width="11" height="11" viewBox="0 0 14 14" fill="none"><path d="M12.5 9.7v2.1c0 .5-.5 1-1 1C5.7 12.5 1.5 8.3 1.5 2.5c0-.5.5-1 1-1h2.1c.5 0 .9.3 1 .8l.5 2.2c.1.4 0 .7-.3 1L4.7 6.6c1 1.8 2.6 3.4 4.4 4.4l1.1-1.1c.3-.3.6-.4 1-.3l2.2.5c.4.1.7.5.7 1z" stroke="white" strokeWidth="1.5" strokeLinejoin="round" fill="none"/></svg>
            </span>
            <span className="site-nav-cta-label">Recibir llamada</span>
          </button>
        </div>

        <button className="site-nav-burger" aria-label="Menú" aria-expanded={open} onClick={() => setOpen(o => !o)}
          style={{ background: 'transparent', border: '1px solid var(--line-strong)', color: 'white', padding: 10, borderRadius: 10, cursor: 'pointer', display: 'none' }}>
          <svg width="16" height="16" viewBox="0 0 16 16" fill="none">
            {open
              ? <path d="M3 3L13 13M13 3L3 13" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round"/>
              : <path d="M2 4H14M2 8H14M2 12H14" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round"/>
            }
          </svg>
        </button>
      </div>

      {/* Mobile menu */}
      {open && (
        <div className="site-nav-mobile-menu" style={{
          borderTop: '1px solid var(--line)',
          background: 'rgba(7,7,10,0.95)',
          padding: '12px clamp(20px, 4vw, 56px) 20px',
          display: 'grid', gap: 4,
        }}>
          {NAV_ITEMS.slice(1).map(it => (
            <a key={it.key} href={it.href} style={{
              display: 'block', padding: '12px 0',
              color: active === it.key ? 'var(--brand-orange)' : 'var(--text-0)',
              textDecoration: 'none', fontSize: 17, fontWeight: 500,
              borderBottom: '1px solid var(--line)',
            }}>{it.label}</a>
          ))}
        </div>
      )}

      <style>{`
        /* Compact zone: hide phone earlier, tighten nav gaps */
        @media (max-width: 1180px) {
          .site-nav-phone { display: none !important; }
          .site-nav-desktop { gap: 22px !important; }
        }
        /* Mobile: collapse to burger */
        @media (max-width: 960px) {
          .site-nav-desktop { display: none !important; }
          .site-nav-burger { display: inline-flex !important; }
        }
        @media (min-width: 961px) {
          .site-nav-mobile-menu { display: none !important; }
        }
        /* Mobile reorder: burger LEFT · logo centered · CTA RIGHT */
        @media (max-width: 720px) {
          .site-nav-row { gap: 10px !important; }
          .site-nav-burger { order: -1; flex-shrink: 0; }
          .site-nav-logo-link { order: 0; flex: 1; justify-content: center; }
          .site-nav-actions { order: 1; gap: 0 !important; }
        }
        /* Very tight mobile: shrink CTA so it fits next to logo + burger */
        @media (max-width: 480px) {
          .site-nav-cta { padding: 8px 12px 8px 10px !important; font-size: 12px !important; }
          .site-nav-cta-label { display: none !important; }
        }
      `}</style>
    </header>
  );
};

// =====================================================================
// SitePageHero — interior-page header. Editorial, restrained.
// Used by every page except home (home has its own custom Hero).
// =====================================================================

const SitePageHero = ({ eyebrow, title, italic, kicker, children }) => (
  <section className="cmf-page-hero" style={{
    position: 'relative',
    padding: 'clamp(72px, 10vw, 160px) clamp(20px, 4vw, 56px) clamp(48px, 6vw, 96px)',
    overflow: 'hidden',
    borderBottom: '1px solid var(--line)',
    background: 'linear-gradient(180deg, #0a0a12 0%, var(--bg-0) 100%)',
  }}>
    {/* warm + cool blobs */}
    <div style={{ position: 'absolute', top: '-30%', right: '-10%', width: 700, height: 700, background: 'radial-gradient(circle, rgba(255,106,26,0.18), transparent 60%)', filter: 'blur(60px)', pointerEvents: 'none' }} />
    <div style={{ position: 'absolute', bottom: '-30%', left: '-10%', width: 700, height: 700, background: 'radial-gradient(circle, rgba(25,194,214,0.13), transparent 60%)', filter: 'blur(60px)', pointerEvents: 'none' }} />
    {/* dot grid */}
    <div className="dot-grid" style={{ position: 'absolute', inset: 0, opacity: 0.5, pointerEvents: 'none' }} />

    <div className="cmf-page-hero-inner" style={{ position: 'relative', maxWidth: 1320, margin: '0 auto' }}>
      <div className="eyebrow" style={{ marginBottom: 24 }}><span>{eyebrow}</span></div>
      <h1 style={{
        fontSize: 'clamp(36px, 7vw, 96px)',
        lineHeight: 1,
        letterSpacing: '-0.035em',
        fontWeight: 500,
        textWrap: 'balance',
        maxWidth: 1100,
      }}>
        {title}
        {italic && (<>{' '}<span className="serif" style={{ fontStyle: 'italic', fontWeight: 400, letterSpacing: '-0.02em' }}>{italic}</span></>)}
      </h1>
      {kicker && (
        <p className="cmf-page-hero-kicker" style={{ fontSize: 'clamp(14.5px, 1.4vw, 19px)', lineHeight: 1.55, color: 'var(--text-1)', maxWidth: 720, marginTop: 24, textWrap: 'pretty' }}>{kicker}</p>
      )}
      {children && <div style={{ marginTop: 32 }}>{children}</div>}
    </div>
  </section>
);

// =====================================================================
// SitePagePlaceholder — clearly-marked section reserved for real content.
// Better than fake content. Used wherever we can't make up details.
// =====================================================================

const Placeholder = ({ label, kind = 'foto', height = 280, note }) => (
  <div style={{
    position: 'relative',
    minHeight: height,
    border: '1px dashed rgba(255,106,26,0.35)',
    borderRadius: 12,
    background: 'repeating-linear-gradient(135deg, rgba(255,106,26,0.04) 0 12px, rgba(255,106,26,0.08) 12px 24px)',
    display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center',
    color: 'rgba(255,180,140,0.85)', textAlign: 'center', padding: 24, gap: 8,
  }}>
    <span className="mono" style={{ fontSize: 11, letterSpacing: '0.16em', color: 'rgba(255,140,80,0.7)', textTransform: 'uppercase' }}>
      [{kind.toUpperCase()} REAL PENDIENTE]
    </span>
    <span style={{ fontSize: 14, color: 'rgba(255,210,180,0.9)', fontWeight: 500 }}>{label}</span>
    {note && <span style={{ fontSize: 12, color: 'rgba(255,200,170,0.6)', maxWidth: 360 }}>{note}</span>}
  </div>
);

// =====================================================================
// PageShell — wraps page content with Nav + Footer + MobileFloatingCTA
// =====================================================================

// =====================================================================
// CallbackModal — lead capture (DEMO MODE).
//
// ⚠️  ESTE FORMULARIO ESTÁ EN MODO DEMO / MOCK — NO envía nada todavía.
//     No hay backend conectado. No hay Twilio. Ambrosio NO recibe la
//     solicitud todavía. La UI valida campos y muestra confirmación,
//     pero el lead se descarta al cerrar el modal.
//
// TODO PRODUCCIÓN:
//   Conectar este formulario con POST /api/leads/callback para guardar
//   el lead y avisar a Ambrosio. Hasta entonces, mantener copy en modo
//   demo y NO prometer una llamada real.
//
// Open from anywhere with: window.openCallbackModal()
// (or dispatch the 'comfortek:callback' CustomEvent on window)
// =====================================================================

const CallbackModal = () => {
  const [open, setOpen] = React.useState(false);
  // status: idle | submitting | success_live | success_pending
  const [status, setStatus] = React.useState('idle');
  const [form, setForm] = React.useState({ name: '', phone: '', kind: 'vivienda', message: '' });
  const [errors, setErrors] = React.useState({});
  const [serverMsg, setServerMsg] = React.useState(null);   // mensaje 4xx/429 mostrado bajo el form
  const [successMsg, setSuccessMsg] = React.useState(null); // mensaje del server al 200
  const dialogRef = React.useRef(null);
  const firstFieldRef = React.useRef(null);

  // Wire up global open API
  React.useEffect(() => {
    const handler = () => setOpen(true);
    window.openCallbackModal = handler;
    window.addEventListener('comfortek:callback', handler);
    return () => window.removeEventListener('comfortek:callback', handler);
  }, []);

  // Esc to close, focus first field on open, lock body scroll
  React.useEffect(() => {
    if (!open) return;
    const onKey = (e) => { if (e.key === 'Escape') close(); };
    document.addEventListener('keydown', onKey);
    document.body.style.overflow = 'hidden';
    setTimeout(() => firstFieldRef.current?.focus(), 60);
    return () => {
      document.removeEventListener('keydown', onKey);
      document.body.style.overflow = '';
    };
  }, [open]);

  const close = () => {
    setOpen(false);
    // Reset state after the close animation finishes
    setTimeout(() => {
      setStatus('idle');
      setErrors({});
      setServerMsg(null);
      setSuccessMsg(null);
      setForm({ name: '', phone: '', kind: 'vivienda', message: '' });
    }, 250);
  };

  const validate = () => {
    const e = {};
    if (!form.name.trim()) e.name = 'Nombre obligatorio';
    const digits = form.phone.replace(/\D/g, '');
    if (!form.phone.trim()) e.phone = 'Teléfono obligatorio';
    else if (digits.length < 9) e.phone = 'Mínimo 9 dígitos';
    setErrors(e);
    return Object.keys(e).length === 0;
  };

  // Normaliza teléfono a E.164 español (+34...) antes de enviarlo al backend.
  // Ambrosio Voice llama desde +1 EE.UU., el server requiere prefijo país.
  // El usuario puede escribir "612345678" o "612 345 678" o "+34 612..." y
  // siempre llega como +34XXXXXXXXX al endpoint.
  const normalizePhoneES = (raw) => {
    let p = (raw || '').replace(/[^\d+]/g, '');
    if (!p) return '';
    if (p.startsWith('+')) return p;                              // ya tiene país
    if (p.startsWith('0034')) return '+' + p.slice(2);            // 0034XXXXXXXXX → +34
    if (p.startsWith('34') && p.length === 11) return '+' + p;    // 34XXXXXXXXX → +34
    if (p.length === 9) return '+34' + p;                         // 9 dígitos → asumir España
    if (p.length >= 9) return '+34' + p;                          // fallback España (server validará)
    return p;
  };

  const submit = async (ev) => {
    ev.preventDefault();
    if (!validate()) return;
    setServerMsg(null);
    setStatus('submitting');

    // ── BACKEND HOOK — Ambrosio Voice ────────────────────────────────
    // Endpoint: AMBROSIO_CALLBACK_URL (cabecera del archivo).
    // Spec: AMBROSIO_WEB_CALLBACK_INTEGRATION.md
    //
    // Comportamiento:
    //   200 → success_live (mensaje del server)
    //   400/429 → mostrar data.message bajo el form (NO ir a éxito)
    //   red/CORS/5xx → success_pending (degradación elegante; no
    //                  prometemos llamada, dirigimos a tel/WhatsApp)
    //
    // El frontend NO embeber credenciales. Endpoint público con
    // CORS restringido a comfortek.es y rate limit por IP.
    // ────────────────────────────────────────────────────────────────
    const sourcePage = (typeof location !== 'undefined' && location.pathname)
      ? location.pathname.split('/').pop().replace('.html', '') || 'home'
      : 'unknown';
    const payload = {
      name: form.name.trim(),
      phone: normalizePhoneES(form.phone),         // +34 forzado si falta
      project_type: form.kind === 'vivienda' ? 'Vivienda' : 'Negocio',
      message: form.message.trim() || undefined,
      source: 'web · ' + sourcePage + ' · callback-modal',
    };

    try {
      const res = await fetch(AMBROSIO_CALLBACK_URL, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(payload),
      });
      let data = {};
      try { data = await res.json(); } catch (_) { /* ignora json parse */ }

      if (res.ok) {
        setSuccessMsg(data.message || 'Solicitud recibida. Comfortek te contactará lo antes posible.');
        setStatus('success_live');
        return;
      }

      // 400 / 429 → mostrar mensaje del server bajo el form
      if (res.status === 400 || res.status === 422 || res.status === 429) {
        setServerMsg(data.message || 'No se ha podido enviar. Revisa los datos y vuelve a intentarlo.');
        setStatus('idle');
        return;
      }

      // 5xx u otros → degradación elegante
      throw new Error('server-error-' + res.status);
    } catch (err) {
      // Red caída, CORS, tunnel reiniciado, 5xx, etc.
      setStatus('success_pending');
    }
  };

  if (!open) return null;

  const overlay = {
    position: 'fixed', inset: 0, zIndex: 200,
    background: 'rgba(4,4,8,0.72)',
    backdropFilter: 'blur(12px) saturate(140%)',
    WebkitBackdropFilter: 'blur(12px) saturate(140%)',
    display: 'flex', alignItems: 'center', justifyContent: 'center',
    padding: 20, animation: 'cbk-fade .22s ease',
  };

  return (
    <div style={overlay} onClick={(e) => { if (e.target === e.currentTarget) close(); }}>
      <div ref={dialogRef} role="dialog" aria-modal="true" aria-labelledby="cbk-title"
        style={{
          width: '100%', maxWidth: 520, position: 'relative',
          background: 'linear-gradient(180deg, #0e0e16 0%, #08080d 100%)',
          border: '1px solid var(--line-strong)',
          borderRadius: 18,
          boxShadow: '0 32px 80px rgba(0,0,0,0.6), inset 0 1px 0 rgba(255,255,255,0.05)',
          overflow: 'hidden',
          animation: 'cbk-pop .28s cubic-bezier(.2,.9,.3,1.1)',
        }}>
        {/* Top accent line */}
        <div style={{ height: 2, background: 'var(--grad-warm, linear-gradient(90deg,#ff5a0a,#ff8a3d))' }} />

        {/* Close */}
        <button onClick={close} aria-label="Cerrar"
          style={{
            position: 'absolute', top: 14, right: 14, width: 34, height: 34,
            border: '1px solid var(--line)', background: 'rgba(255,255,255,0.03)',
            color: 'var(--text-1)', borderRadius: 999, cursor: 'pointer',
            display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
          }}>
          <svg width="12" height="12" viewBox="0 0 12 12" fill="none"><path d="M2 2L10 10M10 2L2 10" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round"/></svg>
        </button>

        {(status === 'success_live' || status === 'success_pending') ? (
          <SuccessView onClose={close} mode={status} message={successMsg} />
        ) : (
          <form onSubmit={submit} noValidate style={{ padding: '36px 32px 28px' }}>
            {/* Ambrosio strip */}
            <div style={{ display: 'flex', alignItems: 'center', gap: 12, marginBottom: 20 }}>
              <div style={{
                width: 36, height: 36, borderRadius: 999, flexShrink: 0,
                background: 'linear-gradient(135deg, #19c2d6 0%, #0a8fa3 100%)',
                color: 'white', display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
                fontWeight: 600, fontSize: 14, boxShadow: '0 6px 20px rgba(25,194,214,0.35)',
              }}>A</div>
              <div>
                <div style={{ fontSize: 13, fontWeight: 500, color: 'var(--text-0)' }}>Ambrosio</div>
                <div className="mono" style={{ fontSize: 10, color: 'var(--brand-cyan, #19c2d6)', letterSpacing: '0.14em' }}>● EN LÍNEA · ASISTENTE COMERCIAL</div>
              </div>
            </div>

            <h2 id="cbk-title" style={{
              fontSize: 32, fontWeight: 500, letterSpacing: '-0.02em',
              lineHeight: 1.05, marginBottom: 10, color: 'var(--text-0)',
            }}>Déjanos tus datos.</h2>
            <p style={{ fontSize: 14.5, lineHeight: 1.55, color: 'var(--text-1)', marginBottom: 26, textWrap: 'pretty' }}>
              Comfortek te contactará para entender tu proyecto.
            </p>

            <Field label="Nombre" required error={errors.name}>
              <input ref={firstFieldRef} type="text" value={form.name} disabled={status === 'submitting'}
                onChange={(e) => setForm(f => ({ ...f, name: e.target.value }))}
                placeholder="Tu nombre"
                className="cbk-input"
                style={inputStyle(!!errors.name)} />
            </Field>

            <Field label="Teléfono" required error={errors.phone}>
              <input type="tel" inputMode="tel" value={form.phone} disabled={status === 'submitting'}
                onChange={(e) => setForm(f => ({ ...f, phone: e.target.value }))}
                placeholder="+34 600 000 000"
                className="cbk-input"
                style={inputStyle(!!errors.phone)} />
            </Field>

            <Field label="Tipo de proyecto" required>
              <div role="radiogroup" style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 10 }}>
                {[
                  { v: 'vivienda', label: 'Vivienda', accent: '#ff6a1a' },
                  { v: 'negocio', label: 'Negocio', accent: '#19c2d6' },
                ].map(opt => {
                  const active = form.kind === opt.v;
                  return (
                    <button key={opt.v} type="button" role="radio" aria-checked={active}
                      onClick={() => setForm(f => ({ ...f, kind: opt.v }))}
                      disabled={status === 'submitting'}
                      style={{
                        padding: '14px 16px', borderRadius: 10, cursor: 'pointer',
                        border: `1px solid ${active ? opt.accent : 'var(--line-strong)'}`,
                        background: active ? `${opt.accent}14` : 'rgba(255,255,255,0.02)',
                        color: active ? 'var(--text-0)' : 'var(--text-1)',
                        fontSize: 14, fontWeight: 500, textAlign: 'left',
                        display: 'flex', alignItems: 'center', gap: 10,
                        transition: 'border-color .15s, background .15s',
                      }}>
                      <span style={{
                        width: 14, height: 14, borderRadius: 999,
                        border: `1.5px solid ${active ? opt.accent : 'var(--text-3, #5a5a6a)'}`,
                        display: 'inline-flex', alignItems: 'center', justifyContent: 'center', flexShrink: 0,
                      }}>
                        {active && <span style={{ width: 6, height: 6, borderRadius: 999, background: opt.accent }} />}
                      </span>
                      {opt.label}
                    </button>
                  );
                })}
              </div>
            </Field>

            <Field label="Mensaje" optional>
              <textarea value={form.message} disabled={status === 'submitting'}
                onChange={(e) => setForm(f => ({ ...f, message: e.target.value }))}
                placeholder="Cuéntanos brevemente qué necesitas (opcional)"
                rows={3}
                className="cbk-input"
                style={{ ...inputStyle(false), resize: 'vertical', minHeight: 84, fontFamily: 'inherit' }} />
            </Field>

            {serverMsg && (
              <div role="alert" style={{
                marginBottom: 12, padding: '10px 14px',
                borderRadius: 10,
                border: '1px solid rgba(255,106,26,0.45)',
                background: 'rgba(255,106,26,0.10)',
                color: '#ffb085', fontSize: 13, lineHeight: 1.45,
              }}>
                {serverMsg}
              </div>
            )}

            <button type="submit" className="btn-ambrosio" disabled={status === 'submitting'}
              style={{
                width: '100%', justifyContent: 'center', marginTop: 8,
                padding: '14px 20px', fontSize: 14,
                opacity: status === 'submitting' ? 0.85 : 1,
                cursor: status === 'submitting' ? 'progress' : 'pointer',
              }}>
              {status === 'submitting' ? (
                <>
                  <Spinner />
                  <span>Enviando solicitud…</span>
                </>
              ) : (
                <>
                  <span className="pulse" style={{ width: 22, height: 22 }}>
                    <svg width="11" height="11" viewBox="0 0 14 14" fill="none"><path d="M12.5 9.7v2.1c0 .5-.5 1-1 1C5.7 12.5 1.5 8.3 1.5 2.5c0-.5.5-1 1-1h2.1c.5 0 .9.3 1 .8l.5 2.2c.1.4 0 .7-.3 1L4.7 6.6c1 1.8 2.6 3.4 4.4 4.4l1.1-1.1c.3-.3.6-.4 1-.3l2.2.5c.4.1.7.5.7 1z" stroke="white" strokeWidth="1.5" strokeLinejoin="round" fill="none"/></svg>
                  </span>
                  Enviar solicitud
                </>
              )}
            </button>

            <p style={{ marginTop: 14, fontSize: 11, color: 'var(--text-3, #6a6a7a)', textAlign: 'center', lineHeight: 1.5 }}>
              Al enviar aceptas que Comfortek te contacte sobre tu consulta. Tus datos no se comparten con terceros.
            </p>
          </form>
        )}
      </div>

      <style>{`
        @keyframes cbk-fade { from { opacity: 0 } to { opacity: 1 } }
        @keyframes cbk-pop  { from { opacity: 0; transform: translateY(8px) scale(.98) } to { opacity: 1; transform: translateY(0) scale(1) } }
        @keyframes cbk-spin { to { transform: rotate(360deg) } }
      `}</style>
    </div>
  );
};

const inputStyle = (hasError) => ({
  width: '100%',
  padding: '12px 14px',
  background: 'rgba(255,255,255,0.025)',
  border: `1px solid ${hasError ? '#ff6a1a' : 'var(--line-strong)'}`,
  borderRadius: 10,
  color: 'var(--text-0)',
  fontSize: 14,
  fontFamily: 'inherit',
  outline: 'none',
  transition: 'border-color .15s, background .15s',
  boxSizing: 'border-box',
});

const Field = ({ label, required, optional, error, children }) => (
  <label style={{ display: 'block', marginBottom: 16 }}>
    <span style={{
      display: 'flex', justifyContent: 'space-between', alignItems: 'baseline',
      fontSize: 12, color: 'var(--text-2)', marginBottom: 6, letterSpacing: '0.02em',
    }}>
      <span>{label}{required && <span style={{ color: 'var(--brand-orange, #ff6a1a)' }}> *</span>}</span>
      {optional && <span style={{ fontSize: 11, color: 'var(--text-3, #6a6a7a)' }}>opcional</span>}
      {error && <span className="mono" style={{ fontSize: 10, color: '#ff7a3d', letterSpacing: '0.06em' }}>{error.toUpperCase()}</span>}
    </span>
    {children}
  </label>
);

const Spinner = () => (
  <span style={{
    display: 'inline-block', width: 16, height: 16, marginRight: 4,
    border: '2px solid rgba(255,255,255,0.3)', borderTopColor: 'white',
    borderRadius: 999, animation: 'cbk-spin 0.7s linear infinite',
  }} />
);

// Horario en el que Ambrosio llama automáticamente (Europe/Madrid).
// Fuera de este rango el backend deja la llamada para la mañana siguiente.
// Si Toni cambia el rango en el backend, ajustar aquí también.
const AMBROSIO_DAY_START_HOUR = 8;   // 08:00
const AMBROSIO_DAY_END_HOUR = 22;    // 22:00
const isAmbrosioActiveHourLocal = () => {
  const h = new Date().getHours();
  return h >= AMBROSIO_DAY_START_HOUR && h < AMBROSIO_DAY_END_HOUR;
};

const SuccessView = ({ onClose, mode, message }) => {
  const isPending = mode === 'success_pending';
  const isActive = isAmbrosioActiveHourLocal();

  // Colores según modo
  const iconBg = isPending
    ? 'linear-gradient(135deg, #ff8a3d 0%, #ff5a0a 100%)'
    : 'linear-gradient(135deg, #19c2d6 0%, #0a8fa3 100%)';
  const iconShadow = isPending
    ? '0 12px 40px rgba(255,138,61,0.40)'
    : '0 12px 40px rgba(25,194,214,0.45)';
  const badgeText = isPending
    ? 'ENVÍO PENDIENTE'
    : (isActive ? 'AMBROSIO TE LLAMA · 1 MIN' : 'AMBROSIO TE LLAMA POR LA MAÑANA');
  const badgeColor = isPending ? '#ffb085' : 'var(--brand-cyan, #19c2d6)';
  const badgeBorder = isPending ? 'rgba(255,106,26,0.45)' : 'var(--line-strong)';
  const title = isPending ? 'No hemos podido enviar tu solicitud.' : 'Recibido.';

  return (
    <div style={{ padding: '48px 32px 36px', textAlign: 'center' }}>
      <div style={{
        width: 64, height: 64, borderRadius: 999, margin: '0 auto 24px',
        background: iconBg,
        display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
        boxShadow: iconShadow,
      }}>
        {isPending ? (
          <svg width="28" height="28" viewBox="0 0 28 28" fill="none">
            <path d="M14 8V14M14 19V19.5" stroke="white" strokeWidth="2.5" strokeLinecap="round" />
          </svg>
        ) : (
          <svg width="28" height="28" viewBox="0 0 28 28" fill="none">
            <path d="M6 14.5L11.5 20L22 8" stroke="white" strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round"
              style={{ strokeDasharray: 40, strokeDashoffset: 40, animation: 'cbk-check .5s .15s forwards ease-out' }} />
          </svg>
        )}
      </div>
      <div className="mono" style={{
        display: 'inline-block', fontSize: 10, letterSpacing: '0.18em',
        color: badgeColor, padding: '4px 10px',
        border: '1px solid ' + badgeBorder, borderRadius: 999,
        marginBottom: 20,
      }}>{badgeText}</div>
      <h2 style={{ fontSize: 26, fontWeight: 500, letterSpacing: '-0.02em', marginBottom: 12, color: 'var(--text-0)' }}>
        {title}
      </h2>
      <p style={{ fontSize: 15, color: 'var(--text-1)', lineHeight: 1.6, maxWidth: 420, margin: '0 auto 24px', textWrap: 'pretty' }}>
        {isPending
          ? 'No hemos podido enviar tu solicitud ahora mismo. Llámanos al +34 610 246 360 o escríbenos por WhatsApp y lo solucionamos en directo.'
          : <>Ambrosio te llama en <strong style={{ color: 'var(--text-0)' }}>menos de 1 minuto</strong> desde el <strong style={{ color: 'var(--text-0)' }}>+1 (229) 489-4838</strong>. Si no contestas, Toni te escribe por WhatsApp.</>}
      </p>
      <div style={{ display: 'flex', gap: 8, justifyContent: 'center', flexWrap: 'wrap', marginTop: isPending ? 16 : 4 }}>
        {isPending && (
          <a href="https://wa.me/34601920198?text=Hola%20Ambrosio%2C%20me%20gustar%C3%ADa%20informaci%C3%B3n%20sobre%20dom%C3%B3tica."
            target="_blank" rel="noopener noreferrer"
            style={{
              padding: '11px 18px', borderRadius: 999, textDecoration: 'none',
              background: 'linear-gradient(135deg, #25D366 0%, #1ea34c 100%)',
              color: 'white', fontWeight: 600, fontSize: 13,
              display: 'inline-flex', alignItems: 'center', gap: 8,
              boxShadow: '0 6px 18px rgba(37,211,102,0.35)',
            }}>
            <svg width="14" height="14" viewBox="0 0 24 24" fill="white"><path d="M20.52 3.48A11.93 11.93 0 0 0 12 0C5.37 0 0 5.37 0 12c0 2.11.55 4.16 1.6 5.97L0 24l6.18-1.62A11.94 11.94 0 0 0 12 24c6.63 0 12-5.37 12-12 0-3.21-1.25-6.22-3.48-8.52zM12 22c-1.85 0-3.66-.5-5.24-1.44l-.37-.22-3.87 1.02 1.03-3.77-.24-.38A9.94 9.94 0 0 1 2 12c0-5.52 4.48-10 10-10s10 4.48 10 10-4.48 10-10 10z"/></svg>
            WhatsApp
          </a>
        )}
        <button onClick={onClose}
          style={{
            padding: '11px 22px', fontSize: 13,
            background: 'transparent', color: 'var(--text-1)',
            border: '1px solid var(--line-strong)', borderRadius: 999, cursor: 'pointer',
          }}>
          Cerrar
        </button>
      </div>
      <style>{`@keyframes cbk-check { to { stroke-dashoffset: 0 } }`}</style>
    </div>
  );
};

// =====================================================================
// PageShell — wraps page content with Nav + Footer + CallbackModal
// =====================================================================

const PageShell = ({ active, navOverlay = false, children }) => (
  <div style={{ background: 'var(--bg-0)', color: 'var(--text-0)', fontFamily: 'var(--font-sans)', minHeight: '100vh', display: 'flex', flexDirection: 'column' }}>
    <SiteNav active={active} overlay={navOverlay} />
    <main style={{ flex: 1 }}>{children}</main>
    <window.Footer />
    <CallbackModal />
  </div>
);

Object.assign(window, { SiteNav, SitePageHero, PageShell, Placeholder, NAV_ITEMS, CallbackModal });
