// ========= ModoLar — Componentes compartilhados =========
/* global React */
const { useState, useEffect, useRef, useMemo } = React;

// ====== Ícones (inline SVG — sem depender de CDN) ======
const Icon = {
  Search: (p) => <svg width={p.size||20} height={p.size||20} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><circle cx="11" cy="11" r="7"/><path d="m21 21-4.3-4.3"/></svg>,
  Cart: (p) => <svg width={p.size||20} height={p.size||20} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><circle cx="9" cy="21" r="1"/><circle cx="19" cy="21" r="1"/><path d="M2.05 2.05h2l2.66 12.42a2 2 0 0 0 2 1.58h9.78a2 2 0 0 0 1.95-1.57l1.65-7.43H5.12"/></svg>,
  Heart: (p) => <svg width={p.size||20} height={p.size||20} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M19 14c1.49-1.46 3-3.21 3-5.5A5.5 5.5 0 0 0 16.5 3c-1.76 0-3 .5-4.5 2-1.5-1.5-2.74-2-4.5-2A5.5 5.5 0 0 0 2 8.5c0 2.3 1.5 4.05 3 5.5l7 7Z"/></svg>,
  WhatsApp: (p) => <svg width={p.size||20} height={p.size||20} 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-.51a12.8 12.8 0 0 0-.57-.01c-.198 0-.52.074-.792.372s-1.04 1.016-1.04 2.479 1.065 2.876 1.213 3.074c.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 0 1-5.031-1.378l-.361-.214-3.741.982.998-3.648-.235-.374a9.86 9.86 0 0 1-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 0 1 2.893 6.994c-.003 5.45-4.437 9.884-9.885 9.884m8.413-18.297A11.815 11.815 0 0 0 12.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 0 0 5.683 1.448h.005c6.554 0 11.89-5.335 11.893-11.893A11.821 11.821 0 0 0 20.465 3.488"/></svg>,
  Menu: (p) => <svg width={p.size||20} height={p.size||20} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round"><path d="M3 12h18M3 6h18M3 18h18"/></svg>,
  Close: (p) => <svg width={p.size||20} height={p.size||20} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round"><path d="M18 6 6 18M6 6l12 12"/></svg>,
  Arrow: (p) => <svg width={p.size||20} height={p.size||20} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M5 12h14M13 5l7 7-7 7"/></svg>,
  ArrowLeft: (p) => <svg width={p.size||20} height={p.size||20} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M19 12H5M12 19l-7-7 7-7"/></svg>,
  ArrowRight: (p) => <svg width={p.size||20} height={p.size||20} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M5 12h14M12 5l7 7-7 7"/></svg>,
  Check: (p) => <svg width={p.size||20} height={p.size||20} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round"><polyline points="20 6 9 17 4 12"/></svg>,
  Truck: (p) => <svg width={p.size||20} height={p.size||20} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M10 17h4V5H2v12h3"/><path d="M20 17h2v-3.34a4 4 0 0 0-1.17-2.83L19 9h-5"/><path d="M14 17h1"/><circle cx="7.5" cy="17.5" r="2.5"/><circle cx="17.5" cy="17.5" r="2.5"/></svg>,
  Shield: (p) => <svg width={p.size||20} height={p.size||20} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M20 13c0 5-3.5 7.5-7.66 8.95a1 1 0 0 1-.67-.01C7.5 20.5 4 18 4 13V6a1 1 0 0 1 1-1c2 0 4.5-1.2 6.24-2.72a1.17 1.17 0 0 1 1.52 0C14.51 3.81 17 5 19 5a1 1 0 0 1 1 1z"/></svg>,
  Wallet: (p) => <svg width={p.size||20} height={p.size||20} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M20 12V8H6a2 2 0 0 1-2-2c0-1.1.9-2 2-2h12v4"/><path d="M4 6v12c0 1.1.9 2 2 2h14v-4"/><path d="M18 12a2 2 0 0 0 0 4h4v-4Z"/></svg>,
  Flag: (p) => <svg width={p.size||20} height={p.size||20} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M4 15s1-1 4-1 5 2 8 2 4-1 4-1V3s-1 1-4 1-5-2-8-2-4 1-4 1z"/><line x1="4" y1="22" x2="4" y2="15"/></svg>,
  Star: (p) => <svg width={p.size||16} height={p.size||16} viewBox="0 0 24 24" fill="currentColor"><polygon points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26"/></svg>,
  StarHalf: (p) => <svg width={p.size||16} height={p.size||16} viewBox="0 0 24 24" fill="currentColor"><path d="M12 2 8.91 8.26 2 9.27l5 4.87-1.18 6.88L12 17.77z" /><path fill="none" stroke="currentColor" strokeWidth="2" d="m12 2 3.09 6.26L22 9.27l-5 4.87 1.18 6.88L12 17.77"/></svg>,
  Plus: (p) => <svg width={p.size||20} height={p.size||20} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round"><path d="M5 12h14M12 5v14"/></svg>,
  Minus: (p) => <svg width={p.size||20} height={p.size||20} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round"><path d="M5 12h14"/></svg>,
  Sliders: (p) => <svg width={p.size||20} height={p.size||20} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><line x1="4" y1="21" x2="4" y2="14"/><line x1="4" y1="10" x2="4" y2="3"/><line x1="12" y1="21" x2="12" y2="12"/><line x1="12" y1="8" x2="12" y2="3"/><line x1="20" y1="21" x2="20" y2="16"/><line x1="20" y1="12" x2="20" y2="3"/><line x1="1" y1="14" x2="7" y2="14"/><line x1="9" y1="8" x2="15" y2="8"/><line x1="17" y1="16" x2="23" y2="16"/></svg>,
  Phone: (p) => <svg width={p.size||16} height={p.size||16} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M22 16.92v3a2 2 0 0 1-2.18 2 19.79 19.79 0 0 1-8.63-3.07 19.5 19.5 0 0 1-6-6 19.79 19.79 0 0 1-3.07-8.67A2 2 0 0 1 4.11 2h3a2 2 0 0 1 2 1.72 12.84 12.84 0 0 0 .7 2.81 2 2 0 0 1-.45 2.11L8.09 9.91a16 16 0 0 0 6 6l1.27-1.27a2 2 0 0 1 2.11-.45 12.84 12.84 0 0 0 2.81.7A2 2 0 0 1 22 16.92z"/></svg>,
  Box: (p) => <svg width={p.size||20} height={p.size||20} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M21 8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.73l7 4a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16Z"/><path d="m3.3 7 8.7 5 8.7-5"/><path d="M12 22V12"/></svg>,
  Sparkle: (p) => <svg width={p.size||20} height={p.size||20} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M9.94 14.06 2 22l7.94-7.94M20.2 16l-2 2-4-4 2-2M15 5l4-3 1 1-3 4M12 2v3M21 12h-3M4.93 4.93l2.12 2.12M19.07 4.93 16.95 7.05"/></svg>,
  Ruler: (p) => <svg width={p.size||20} height={p.size||20} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M21.3 8.7 15.3 2.7a1 1 0 0 0-1.4 0l-11 11a1 1 0 0 0 0 1.4l6 6a1 1 0 0 0 1.4 0l11-11a1 1 0 0 0 0-1.4zM7.5 12.5l2 2M10.5 9.5l2 2M13.5 6.5l2 2M4.5 15.5l2 2"/></svg>
};

// Stars rendering
function Stars({ value = 5, size = 14 }) {
  const full = Math.floor(value);
  const hasHalf = value - full >= 0.5;
  return (
    <span className="stars" style={{color: 'var(--orange)'}}>
      {Array.from({length: 5}).map((_, i) => {
        if (i < full) return <Icon.Star key={i} size={size}/>;
        if (i === full && hasHalf) return <Icon.StarHalf key={i} size={size}/>;
        return <span key={i} style={{color: 'var(--ink-200)'}}><Icon.Star size={size}/></span>;
      })}
    </span>
  );
}

// ====== Logo component ======
function Logo({ variant = 'cinza', height = 28, onClick }) {
  const src = variant === 'branco' ? 'assets/logo-branco.png' : variant === 'laranja' ? 'assets/logo-laranja.png' : 'assets/logo-cinza.png';
  return <img src={src} alt="ModoLar" style={{height}} onClick={onClick}/>;
}

// ====== Marquee announcement ======
function Marquee() {
  const items = [
    { i: <Icon.Truck size={14}/>, t: 'Frete grátis em toda região de Campinas' },
    { i: <Icon.Wallet size={14}/>, t: 'Pagamento na entrega' },
    { i: <Icon.Box size={14}/>, t: 'Entrega em até 3 dias úteis' },
    { i: <Icon.Shield size={14}/>, t: 'Garantia de 1 ano' },
    { i: <Icon.Flag size={14}/>, t: 'Tecnologia 100% brasileira' },
  ];
  const loop = [...items, ...items, ...items, ...items];
  return (
    <div className="marquee">
      <div className="marquee-track">
        {loop.map((it, i) => (
          <span key={i}>{it.i}<span className="marquee-dot"/>{it.t}</span>
        ))}
      </div>
    </div>
  );
}

// ====== Header ======
function Header({ route, navigate, cartCount, openCart }) {
  return (
    <>
      <Marquee/>
      <header className="header">
        <div className="shell header-row">
          <div className="logo-wrap" onClick={() => navigate({view:'home'})}>
            <Logo variant="cinza" height={30}/>
          </div>
          <nav className="nav">
            <a className={route.view === 'home' ? 'active' : ''} onClick={() => navigate({view:'home'})}>Início</a>
            <a className={route.view === 'category' && route.catId === 'sofa-modular' ? 'active' : ''} onClick={() => navigate({view:'category', catId:'sofa-modular'})}>Sofás Modulares</a>
            <a className={route.view === 'category' && route.catId === 'sofa-cama' ? 'active' : ''} onClick={() => navigate({view:'category', catId:'sofa-cama'})}>Sofás-Cama</a>
            <a className={route.view === 'category' && route.catId === 'poltrona' ? 'active' : ''} onClick={() => navigate({view:'category', catId:'poltrona'})}>Poltronas</a>
          </nav>
          <div className="header-actions">
            <span className="phone-pill">
              <Icon.Phone size={14}/>(19) 97046-803
            </span>
            <button className="icon-btn" aria-label="Buscar"><Icon.Search/></button>
            <button className="icon-btn" aria-label="Carrinho" onClick={openCart}>
              <Icon.Cart/>
              {cartCount > 0 && <span className="cart-badge">{cartCount}</span>}
            </button>
          </div>
        </div>
      </header>
    </>
  );
}

// ====== Footer ======
function Footer({ navigate }) {
  return (
    <footer className="footer">
      <div className="shell footer-grid">
        <div>
          <Logo variant="branco" height={32}/>
          <p style={{marginTop: 16, opacity: 0.7, fontSize: 14, lineHeight: 1.6, maxWidth: 300}}>
            Sofás prensados com tecnologia brasileira. Entregamos na sua casa em até 3 dias em toda região de Campinas.
          </p>
          <div style={{display:'flex', gap: 12, marginTop: 20}}>
            <a href="#" style={{width: 36, height: 36, borderRadius: 99, background:'rgba(255,255,255,.08)', display:'grid', placeItems:'center'}} aria-label="Instagram">
              <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><rect x="2" y="2" width="20" height="20" rx="5"/><circle cx="12" cy="12" r="4"/><circle cx="18" cy="6" r="1" fill="currentColor"/></svg>
            </a>
            <a href="#" style={{width: 36, height: 36, borderRadius: 99, background:'rgba(255,255,255,.08)', display:'grid', placeItems:'center'}} aria-label="WhatsApp">
              <Icon.WhatsApp size={16}/>
            </a>
          </div>
        </div>
        <div>
          <h5>Catálogo</h5>
          <div className="footer-links">
            <a onClick={() => navigate({view:'category', catId:'sofa-modular'})}>Sofás Modulares</a>
            <a onClick={() => navigate({view:'category', catId:'sofa-cama'})}>Sofás-Cama</a>
            <a onClick={() => navigate({view:'category', catId:'poltrona'})}>Poltronas</a>
          </div>
        </div>
        <div>
          <h5>Atendimento</h5>
          <div className="footer-links">
            <a href="#">Central de ajuda</a>
            <a href="#">Política de trocas</a>
            <a href="#">Entrega e frete</a>
            <a href="#">Garantia</a>
          </div>
        </div>
        <div>
          <h5>Contato</h5>
          <div className="footer-links" style={{gap: 14}}>
            <div style={{display:'flex', alignItems:'center', gap: 10, color:'#fff', opacity:1}}>
              <Icon.WhatsApp size={14}/>
              <span style={{fontWeight: 600}}>(19) 97046-803</span>
            </div>
            <div style={{opacity: 0.7}}>Seg–Sex • 9h às 18h</div>
            <div style={{opacity: 0.7}}>Sáb • 9h às 13h</div>
            <div style={{opacity: 0.7, marginTop: 8}}>Campinas — SP</div>
          </div>
        </div>
      </div>
      <div className="shell footer-bottom">
        <span>© 2026 ModoLar Sofás Prensados. Todos os direitos reservados.</span>
        <span>Feito com laranja em Campinas 🧡</span>
      </div>
    </footer>
  );
}

// Expose to window for other Babel scripts
Object.assign(window, { Icon, Stars, Logo, Marquee, Header, Footer });
