function WhatsAppBusinessCard({ settings }) {
  const number = (settings.whatsapp_number || '').replace(/\D/g, '');
  const display = settings.whatsapp_display || settings.phone || '';
  const hours = settings.whatsapp_hours || 'Mon–Fri 9:00–18:00 GMT';
  const replySla = settings.whatsapp_reply_sla || '2hr';
  const status = settings.whatsapp_status || 'online';
  const company = settings.company_name || 'Lupipak';
  if (!number) return null;

  const statusDot = status === 'online' ? '#25D366' : status === 'away' ? '#E8A74A' : '#8B857A';
  const statusText = status === 'online' ? `REPLY ${replySla.toUpperCase()}` : status === 'away' ? `AWAY · ${replySla.toUpperCase()}` : 'OFFLINE';

  const defaultMsg = (settings.whatsapp_default_message || "Hi {{company}} — I'd like to ask about packaging options.")
    .replace(/\{\{company\}\}/g, company);
  const link = `https://wa.me/${number}?text=${encodeURIComponent(defaultMsg)}`;

  return (
    <a href={link} target="_blank" rel="noopener noreferrer"
      className="fade-up"
      style={{
        display: 'grid', gridTemplateColumns: 'auto 1fr auto', gap: 16, alignItems: 'center',
        marginTop: 32, padding: '16px 18px',
        background: 'linear-gradient(95deg, #E9F7EE 0%, var(--paper) 60%)',
        border: '.5px solid rgba(37,211,102,0.3)',
        borderRadius: 14,
        textDecoration: 'none', color: 'inherit',
        animationDelay: '0.24s',
        transition: 'transform .2s, box-shadow .2s',
      }}
      onMouseEnter={(e) => { e.currentTarget.style.transform = 'translateY(-2px)'; e.currentTarget.style.boxShadow = 'var(--shadow-md)'; }}
      onMouseLeave={(e) => { e.currentTarget.style.transform = 'translateY(0)'; e.currentTarget.style.boxShadow = 'none'; }}
    >
      <span style={{
        width: 44, height: 44, borderRadius: '50%',
        background: '#25D366',
        display: 'flex', alignItems: 'center', justifyContent: 'center',
        boxShadow: '0 0 0 4px rgba(37,211,102,0.18)',
        flexShrink: 0,
      }}>
        <svg width="22" height="22" viewBox="0 0 24 24" fill="#fff" aria-hidden="true">
          <path d="M17.472 14.382c-.297-.149-1.758-.867-2.03-.966-.273-.099-.471-.148-.67.15-.197.297-.767.966-.94 1.164-.173.198-.347.223-.644.074-.297-.148-1.254-.462-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.297-.497.1-.198.05-.371-.025-.52-.074-.148-.669-1.611-.916-2.207-.241-.579-.487-.5-.669-.51-.173-.007-.371-.009-.57-.009-.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.226 1.36.194 1.872.118.571-.085 1.758-.719 2.006-1.413.247-.694.247-1.289.173-1.413-.074-.124-.272-.198-.57-.347z"/>
          <path d="M12.003 2.001h-.005A9.96 9.96 0 0 0 2 12a9.96 9.96 0 0 0 1.913 5.857L2 22l4.27-1.89A9.96 9.96 0 0 0 12.003 22 9.96 9.96 0 0 0 22 12.002 9.96 9.96 0 0 0 12.003 2zm5.827 14.116c-.244.692-1.222 1.266-2.001 1.435-.534.114-1.231.205-3.58-.776-2.975-1.233-4.891-4.255-5.04-4.453-.15-.198-1.213-1.612-1.213-3.075 0-1.463.762-2.182 1.034-2.479.272-.298.593-.372.792-.372.198 0 .397.002.57.01.181.01.429-.07.67.509.247.596.842 2.058.916 2.207.074.148.124.322.025.52-.1.198-.149.322-.297.496-.149.174-.313.388-.447.52-.149.15-.303.31-.13.608.173.297.77 1.271 1.653 2.058 1.136 1.013 2.093 1.327 2.39 1.476.297.149.471.124.644-.074.174-.198.744-.867.941-1.164.197-.298.395-.25.669-.149.272.1 1.733.817 2.03.966.297.149.495.223.57.347.074.124.074.719-.173 1.413z"/>
        </svg>
      </span>
      <div style={{ minWidth: 0 }}>
        <div style={{ display: 'flex', alignItems: 'center', gap: 8, flexWrap: 'wrap' }}>
          <span className="mono" style={{ fontSize: 10, letterSpacing: '0.08em', textTransform: 'uppercase', color: '#075E54' }}>
            WhatsApp Business
          </span>
          <span className="mono" style={{ fontSize: 10, padding: '2px 7px', borderRadius: 20, background: 'rgba(37,211,102,0.15)', color: '#075E54', letterSpacing: '0.04em', display: 'inline-flex', alignItems: 'center', gap: 5 }}>
            <span style={{ width: 5, height: 5, borderRadius: '50%', background: statusDot }}/>
            VERIFIED · {statusText}
          </span>
        </div>
        <div style={{ fontSize: 16, fontWeight: 500, letterSpacing: '-0.01em', marginTop: 2 }}>
          Chat directly with our team
        </div>
        <div className="mono" style={{ fontSize: 11.5, color: 'var(--muted)', marginTop: 2, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>
          {display}{display ? ' · ' : ''}{hours}
        </div>
      </div>
      <span style={{
        display: 'inline-flex', alignItems: 'center', gap: 6,
        padding: '8px 14px', borderRadius: 10,
        background: '#25D366', color: '#fff', fontSize: 12.5, fontWeight: 500,
        flexShrink: 0,
      }}>
        Chat now <ArrowRight size={12}/>
      </span>
    </a>
  );
}

function Homepage({ onSelect, accent, onAdmin, sectors, settings }) {
  const [query, setQuery] = React.useState('');
  const [hover, setHover] = React.useState(null);
  const [time, setTime] = React.useState(new Date());
  const isMobile = useMedia('(max-width: 900px)');

  React.useEffect(() => {
    const t = setInterval(() => setTime(new Date()), 1000);
    return () => clearInterval(t);
  }, []);

  const filtered = (sectors || []).filter(s =>
    s.label.toLowerCase().includes(query.toLowerCase()) ||
    (s.sub || '').toLowerCase().includes(query.toLowerCase())
  );

  const hhmm = time.toLocaleTimeString('en-GB', { hour: '2-digit', minute: '2-digit' });
  const cityLabel = settings.city_label || 'LONDON';
  const companyName = settings.company_name || 'Lupipak Ltd';
  const companyShort = companyName.toUpperCase();

  return (
    <div style={{ minHeight: '100vh', display: 'flex', flexDirection: 'column' }}>
      <TopBar onLogo={() => window.scrollTo({ top: 0, behavior: 'smooth' })} right={<>
        <span className="mono hide-mobile" style={{ fontSize: 11, letterSpacing: '0.05em' }}>{cityLabel} · {hhmm}</span>
        <span className="hide-mobile" style={{ width: 1, height: 12, background: 'var(--line-2)' }} />
        <a className="hide-mobile" style={{ color: 'var(--ink)', textDecoration: 'none', fontSize: 13 }}>Catalogue</a>
        <a className="hide-mobile" style={{ color: 'var(--ink)', textDecoration: 'none', fontSize: 13 }}>Case studies</a>
        <a className="hide-mobile" style={{ color: 'var(--ink)', textDecoration: 'none', fontSize: 13 }}>Contact</a>
        <Button variant="quiet" size="sm">Sign in</Button>
      </>} />

      {/* Meta strip */}
      <div className="stack-mobile" style={{
        display: 'flex', justifyContent: 'space-between', alignItems: 'center',
        padding: isMobile ? '12px 16px' : '14px 32px',
        fontSize: 11, color: 'var(--muted)',
        borderBottom: '.5px solid var(--line)',
        gap: 8,
      }}>
        <span className="mono" style={{ letterSpacing: '0.06em', textTransform: 'uppercase' }}>
          № 001 · {settings.tagline || 'Visual Packaging Builder'}
        </span>
        <div style={{ display: 'flex', gap: 14, flexWrap: 'wrap' }}>
          <span className="mono" style={{ letterSpacing: '0.04em' }}>🇬🇧 UK · 🇪🇺 EU · 🇹🇷 TR</span>
          <span className="mono hide-mobile" style={{ letterSpacing: '0.04em' }}>Est. lead time {settings.lead_time_days || '12–18 days'}</span>
        </div>
      </div>

      <main className="two-col" style={{ flex: 1 }}>
        {/* Left: question */}
        <section className="pad-lg" style={{
          padding: '72px 56px 56px 56px',
          display: 'flex', flexDirection: 'column', justifyContent: 'space-between',
          borderRight: isMobile ? 'none' : '.5px solid var(--line)',
          borderBottom: isMobile ? '.5px solid var(--line)' : 'none',
        }}>
          <div>
            <div className="mono fade-up" style={{ fontSize: 11, letterSpacing: '0.08em', textTransform: 'uppercase', color: 'var(--muted)', marginBottom: isMobile ? 20 : 32 }}>
              Step 01 · Tell us about your kitchen
            </div>

            <h1 className="fade-up responsive-h1" style={{
              fontFamily: 'var(--font-serif)', fontStyle: 'italic',
              fontSize: 'clamp(48px, 6vw, 88px)', fontWeight: 400,
              lineHeight: 1.02, letterSpacing: '-0.025em',
              margin: '0 0 24px 0',
              animationDelay: '0.06s',
            }}>
              What are you<br/>
              <span style={{ fontStyle: 'normal', fontFamily: 'var(--font-sans)', fontWeight: 300 }}>selling?</span>
            </h1>

            <p className="fade-up" style={{
              fontSize: 16, color: 'var(--ink-2)', lineHeight: 1.5, maxWidth: 440,
              margin: '0 0 32px 0', animationDelay: '0.12s',
            }}>
              Build a complete packaging set in minutes. Pick your sector, configure quantities, get a tailored quote from our team within 24 hours.
            </p>

            {/* Search */}
            <div className="fade-up" style={{ position: 'relative', maxWidth: 440, animationDelay: '0.18s' }}>
              <svg width="14" height="14" viewBox="0 0 16 16" fill="none" style={{
                position: 'absolute', left: 18, top: '50%', transform: 'translateY(-50%)', color: 'var(--muted)',
              }}>
                <circle cx="7" cy="7" r="5" stroke="currentColor" strokeWidth="1.2"/>
                <path d="M11 11 L14 14" stroke="currentColor" strokeWidth="1.2" strokeLinecap="round"/>
              </svg>
              <input
                value={query}
                onChange={(e) => setQuery(e.target.value)}
                placeholder="Search sector — e.g. burger, pizza, döner…"
                style={{
                  width: '100%', padding: '16px 18px 16px 44px',
                  fontFamily: 'inherit',
                  background: 'var(--paper)',
                  border: '.5px solid var(--line-2)',
                  borderRadius: 12, outline: 'none',
                }}
              />
              {query && filtered.length > 0 && (
                <div style={{
                  position: 'absolute', top: '100%', left: 0, right: 0, marginTop: 6,
                  background: 'var(--paper)', borderRadius: 12, border: '.5px solid var(--line-2)',
                  boxShadow: 'var(--shadow-md)', overflow: 'hidden', zIndex: 5,
                }} className="fade-in">
                  {filtered.map(s => (
                    <div
                      key={s.id}
                      onClick={() => onSelect(s.id)}
                      style={{ padding: '12px 18px', fontSize: 14, borderBottom: '.5px solid var(--line)', display: 'flex', justifyContent: 'space-between', cursor: 'pointer' }}
                    >
                      <span>{s.label}</span>
                      <span className="mono" style={{ color: 'var(--muted)', fontSize: 11 }}>{s.kcal} products</span>
                    </div>
                  ))}
                </div>
              )}
            </div>
          </div>

          {/* WhatsApp Business card */}
          <WhatsAppBusinessCard settings={settings} />

          {/* Stats */}
          <div className="hide-mobile" style={{ display: 'flex', gap: 56, marginTop: 48, paddingTop: 32, borderTop: '.5px solid var(--line)' }}>
            <Metric label="Active clients" value="840+" />
            <Metric label="Sectors" value={(sectors || []).length.toString()} />
            <Metric label="Lead time" value={settings.lead_time_days || '12–18 days'} />
            <Metric label="HQ" value={settings.address_city || 'London'} />
          </div>
        </section>

        {/* Right: sector grid */}
        <section className="pad-lg" style={{
          padding: '72px 56px 56px 56px',
          display: 'flex', flexDirection: 'column', gap: 12,
        }}>
          <div className="mono fade-up" style={{
            display: 'flex', justifyContent: 'space-between',
            fontSize: 11, letterSpacing: '0.06em', textTransform: 'uppercase', color: 'var(--muted)', marginBottom: 8,
          }}>
            <span>Select sector</span>
            <span>{(sectors || []).length} · A to E</span>
          </div>

          {(sectors || []).map((s, i) => (
            <div
              key={s.id}
              onClick={() => onSelect(s.id)}
              onMouseEnter={() => setHover(s.id)}
              onMouseLeave={() => setHover(null)}
              className="fade-up"
              style={{
                animationDelay: `${0.1 + i * 0.06}s`,
                display: 'grid', gridTemplateColumns: '28px 1fr auto auto',
                alignItems: 'center', gap: 16,
                padding: '20px 20px',
                background: hover === s.id ? 'var(--ink)' : 'var(--paper)',
                color: hover === s.id ? 'var(--paper)' : 'var(--ink)',
                borderRadius: 14,
                border: '.5px solid var(--line-2)',
                transition: 'all .25s cubic-bezier(.2,.8,.2,1)',
                transform: hover === s.id ? 'translateX(-4px)' : 'translateX(0)',
                cursor: 'pointer',
              }}
            >
              <span className="mono" style={{ fontSize: 11, opacity: 0.6 }}>
                0{i + 1}
              </span>
              <div style={{ display: 'flex', flexDirection: 'column', gap: 2 }}>
                <span style={{ fontSize: 16, fontWeight: 500, letterSpacing: '-0.015em' }}>{s.label}</span>
                <span style={{ fontSize: 12.5, opacity: 0.65 }}>{s.sub}</span>
              </div>
              <span className="mono hide-mobile" style={{ fontSize: 11, opacity: 0.6 }}>
                {s.kcal} items
              </span>
              <div style={{
                width: 32, height: 32, borderRadius: '50%',
                display: 'flex', alignItems: 'center', justifyContent: 'center',
                background: hover === s.id ? accent : 'transparent',
                color: hover === s.id ? '#fff' : 'var(--ink)',
                border: hover === s.id ? 'none' : '.5px solid var(--line-2)',
                transition: 'all .25s',
              }}>
                <ArrowRight size={12} />
              </div>
            </div>
          ))}

          <div className="mono" style={{
            marginTop: 8, fontSize: 11, color: 'var(--muted)',
            letterSpacing: '0.04em', display: 'flex', alignItems: 'center', gap: 8,
          }}>
            <span style={{ width: 20, height: 1, background: 'var(--line-2)' }} />
            <span>Don't see your sector? <span style={{ color: 'var(--ink)', textDecoration: 'underline', textUnderlineOffset: 3 }}>Talk to sales →</span></span>
          </div>
        </section>
      </main>

      {/* Footer strip */}
      <footer className="stack-mobile" style={{
        display: 'flex', justifyContent: 'space-between', alignItems: 'center',
        padding: isMobile ? '14px 16px 100px' : '18px 32px',
        fontSize: 11, color: 'var(--muted)',
        borderTop: '.5px solid var(--line)', fontFamily: 'var(--font-mono)',
        letterSpacing: '0.04em',
        gap: 10,
      }}>
        <span>{settings.footer_note || companyShort + ' · 2026'}</span>
        <span className="hide-mobile">Press SPACE or pick a sector to begin</span>
        <span style={{ display: 'inline-flex', gap: 14 }}>
          <button onClick={onAdmin} style={{ background: 'transparent', border: 0, color: 'var(--muted)', fontFamily: 'inherit', fontSize: 11, letterSpacing: '0.04em', cursor: 'pointer' }}>
            Back office ↗
          </button>
          <span>v1.0 · Builder</span>
        </span>
      </footer>
    </div>
  );
}

Object.assign(window, { Homepage });
