// Pyrovolt — Shared UI components
// Logo, Nav, Footer, ProductVisual (inline SVG hero product renders)

const FOOTER_EMAIL = window.PYROVOLT_CONTACT_EMAIL || 'info@pyrovolt.es';

const Logo = ({ size = 22, dark = false }) => (
  <img
    src={dark ? '/assets/logo-white.png' : '/assets/logo-black.png'}
    alt="Pyrovolt"
    style={{ height: size, width: 'auto', display: 'block' }}
  />
);

const LogoMark = ({ size = 20 }) => (
  <svg viewBox="0 0 130 200" style={{ height: size, width: 'auto' }} fill="currentColor" aria-label="Pyrovolt">
    <path d="M0 10 L90 10 L130 100 L90 190 L0 190 L40 100 Z M30 60 L60 60 L80 100 L60 140 L30 140 L50 100 Z"/>
  </svg>
);

// ─────────────────── NAV ───────────────────
function Nav({ currentPage, navigate, isDark, theme = 'dark', onThemeToggle, language = 'ESP', onLanguageChange, variant = 'default' }) {
  const [scrolled, setScrolled] = React.useState(false);
  const [langOpen, setLangOpen] = React.useState(false);
  const langRef = React.useRef(null);
  React.useEffect(() => {
    const onScroll = () => setScrolled(window.scrollY > 20);
    window.addEventListener('scroll', onScroll);
    onScroll();
    return () => window.removeEventListener('scroll', onScroll);
  }, []);
  React.useEffect(() => {
    const onPointerDown = (event) => {
      if (!langRef.current || langRef.current.contains(event.target)) return;
      setLangOpen(false);
    };
    const onKeyDown = (event) => {
      if (event.key === 'Escape') setLangOpen(false);
    };
    window.addEventListener('pointerdown', onPointerDown);
    window.addEventListener('keydown', onKeyDown);
    return () => {
      window.removeEventListener('pointerdown', onPointerDown);
      window.removeEventListener('keydown', onKeyDown);
    };
  }, []);

  const items = [
    { id: 'chargers', label: 'Cargadores' },
    { id: 'storage', label: 'Almacenamiento' },
    { id: 'mt', label: 'MT y Distribución' },
    { id: 'app', label: 'App' },
    { id: 'company', label: 'Empresa' },
    { id: 'news', label: 'Recursos' },
  ];
  const languages = ['CAT', 'EN', 'ESP', 'IT', 'FR', 'DE'];
  const nextTheme = theme === 'dark' ? 'light' : 'dark';
  const isCoverCategory = variant !== 'product-detail' && ['chargers', 'storage', 'mt'].includes(currentPage);
  const effectiveDark = variant === 'product-detail' && theme === 'light'
    ? !scrolled
    : isCoverCategory
      ? !scrolled
      : isDark;

  return (
    <nav className={`nav ${scrolled ? 'scrolled' : ''}`} data-dark={effectiveDark ? 'true' : 'false'} data-variant={variant} data-current={currentPage}>
      <a href="#home" onClick={(e) => { e.preventDefault(); navigate('home'); }} className="nav-logo" style={{ flex: '0 0 auto' }}>
        <Logo size={22} dark={effectiveDark} />
      </a>
      <div className="nav-links" style={{ position: 'absolute', left: '50%', top: '50%', transform: 'translate(-50%, -50%)' }}>
        {items.map(it => (
          <a key={it.id} href={'#' + it.id}
             onClick={(e) => { e.preventDefault(); navigate(it.id); }}
             className={`nav-link ${currentPage === it.id ? 'active' : ''}`}>
            {it.label}
          </a>
        ))}
      </div>
      <div className="nav-right" style={{ flex: '0 0 auto', display: 'flex', alignItems: 'center', gap: 4 }}>
        {/* Language selector — minimal text trigger */}
        <div ref={langRef} className={`nav-lang-minimal ${langOpen ? 'is-open' : ''}`} aria-label="Seleccionar idioma" data-no-i18n>
          <button
            className="nav-ctrl nav-ctrl-lang"
            type="button"
            aria-haspopup="true"
            aria-expanded={langOpen ? 'true' : 'false'}
            onClick={() => setLangOpen(open => !open)}
          >
            <svg viewBox="0 0 16 16" aria-hidden="true" style={{ width: 13, height: 13, flexShrink: 0, opacity: .7 }}>
              <circle cx="8" cy="8" r="6.2" fill="none" stroke="currentColor" strokeWidth="1.3"/>
              <path d="M8 1.8C9.5 4 10.3 6 10.3 8s-.8 4-2.3 6.2M8 1.8C6.5 4 5.7 6 5.7 8s.8 4 2.3 6.2M1.8 8h12.4" fill="none" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round"/>
            </svg>
            <span>{language}</span>
            <svg viewBox="0 0 10 10" aria-hidden="true" className="nav-ctrl-chevron">
              <path d="M2.5 3.5 5 6.5 7.5 3.5" fill="none" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round" strokeLinejoin="round"/>
            </svg>
          </button>
          <div className="nav-lang-dropdown" role="menu">
            <div className="nav-lang-grid">
              {languages.map(code => (
                <button
                  key={code}
                  type="button"
                  className={`nav-lang-opt${language === code ? ' is-active' : ''}`}
                  onClick={() => { onLanguageChange && onLanguageChange(code); setLangOpen(false); }}
                  role="menuitem"
                >
                  {code}
                </button>
              ))}
            </div>
          </div>
        </div>

        {/* Separator */}
        <span style={{ width: 1, height: 16, background: 'currentColor', opacity: .12, flexShrink: 0, margin: '0 2px' }} aria-hidden="true" />

        {/* Theme toggle — icon only */}
        <button
          className="nav-ctrl nav-ctrl-theme"
          type="button"
          onClick={() => onThemeToggle && onThemeToggle()}
          aria-pressed={theme === 'light' ? 'true' : 'false'}
          aria-label={`Cambiar a tema ${nextTheme === 'dark' ? 'oscuro' : 'claro'}`}
          title={`Tema ${theme === 'dark' ? 'oscuro' : 'claro'}`}
        >
          {theme === 'dark' ? (
            <svg viewBox="0 0 18 18" aria-hidden="true" style={{ width: 15, height: 15 }}>
              <path d="M13 14.5A6.5 6.5 0 0 1 4.5 6a5.5 5.5 0 1 0 8.5 8.5Z" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinejoin="round"/>
            </svg>
          ) : (
            <svg viewBox="0 0 18 18" aria-hidden="true" style={{ width: 15, height: 15 }}>
              <circle cx="9" cy="9" r="3.4" fill="none" stroke="currentColor" strokeWidth="1.4"/>
              <path d="M9 1.5v1.8M9 14.7v1.8M1.5 9h1.8M14.7 9h1.8M3.8 3.8 5 5M13 13l1.2 1.2M14.2 3.8 13 5M5 13l-1.2 1.2" fill="none" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round"/>
            </svg>
          )}
        </button>

        {/* Separator */}
        <span style={{ width: 1, height: 16, background: 'currentColor', opacity: .12, flexShrink: 0, margin: '0 2px' }} aria-hidden="true" />

        <a className="nav-ctrl nav-ctrl-contact" href="#contact"
           onClick={(e) => { e.preventDefault(); navigate('contact'); }}>
          Contacto
        </a>
      </div>
    </nav>
  );
}

// ─────────────────── FOOTER ───────────────────
function LegacyFooter({ navigate }) {
  return (
    <footer style={{
      borderTop: '1px solid var(--line)',
      padding: 'var(--pad-x)',
      paddingTop: 80, paddingBottom: 48,
      marginTop: 40,
    }}>
      <div style={{ display: 'grid', gridTemplateColumns: 'repeat(12, 1fr)', gap: 40, marginBottom: 80 }}>
        <div style={{ gridColumn: 'span 6' }}>
          <div className="h-eyebrow" style={{ marginBottom: 24 }}>
            <span style={{ fontFamily: 'var(--font-serif)', fontStyle: 'italic', textTransform: 'none', letterSpacing: 0, fontSize: 18, color: 'var(--fg-dim)' }}>Energy Made Simple.</span>
          </div>
          <div className="h-title" style={{ fontSize: 'clamp(36px, 5vw, 72px)', marginBottom: 24 }}>
            ¿Electrificamos<br/>tu proyecto?
          </div>
          <a className="btn btn-primary btn-lg" href="#contact"
             onClick={(e) => { e.preventDefault(); navigate('contact'); }}>
            Habla con nosotros
          </a>
        </div>
        <div style={{ gridColumn: 'span 2' }}>
          <div className="h-eyebrow" style={{ marginBottom: 16 }}>Producto</div>
          <FLink onClick={() => navigate('chargers')}>Cargadores EV</FLink>
          <FLink onClick={() => navigate('storage')}>BESS · Contenedor</FLink>
          <FLink onClick={() => navigate('storage')}>ESS · Compacto</FLink>
        </div>
        <div style={{ gridColumn: 'span 2' }}>
          <div className="h-eyebrow" style={{ marginBottom: 16 }}>Empresa</div>
          <FLink onClick={() => navigate('company')}>Sobre Pyrovolt</FLink>
          <FLink to="news" onClick={() => navigate('news')}>Recursos</FLink>
          <FLink onClick={() => navigate('contact')}>Contacto</FLink>
        </div>
        <div style={{ gridColumn: 'span 2' }}>
          <div className="h-eyebrow" style={{ marginBottom: 16 }}>Legal</div>
          <FLink to="privacy" onClick={() => navigate('privacy')}>Privacidad</FLink>
          <FLink to="cookies" onClick={() => navigate('cookies')}>Cookies</FLink>
          <FLink to="legal" onClick={() => navigate('legal')}>Avisos</FLink>
        </div>
      </div>
      <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'end', paddingTop: 32, borderTop: '1px solid var(--line)' }}>
        <Logo size={14} />
        <div className="t-mono" style={{ color: 'var(--fg-mute)' }}>
          © 2026 Pyrovolt · Engineered in Europe
        </div>
      </div>
    </footer>
  );
}

function Footer({ navigate }) {
  const openCookiePreferences = () => {
    try { localStorage.removeItem('pv-cookie-consent'); } catch (error) {}
    window.location.reload();
  };

  return (
    <footer className="pv-footer">
      <div className="pv-footer-top">
        <div className="pv-footer-cta">
          <div className="pv-footer-kicker">Energy Made Simple.</div>
          <h2>Hablemos de tu infraestructura.</h2>
          <p>
            Recarga DC, almacenamiento BESS y media tensión coordinados desde el primer dimensionamiento hasta la puesta en marcha.
          </p>
          <a className="pv-footer-action" href="#contact"
             onClick={(e) => { e.preventDefault(); navigate('contact'); }}>
            Preparar consulta
          </a>
        </div>

        <div className="pv-footer-nav">
          <div>
            <h3>Soluciones</h3>
            <FLink to="chargers" onClick={() => navigate('chargers')}>Recarga DC</FLink>
            <FLink to="storage" onClick={() => navigate('storage')}>Almacenamiento BESS</FLink>
            <FLink to="mt" onClick={() => navigate('mt')}>MT y distribución</FLink>
          </div>
          <div>
            <h3>Pyrovolt</h3>
            <FLink to="company" onClick={() => navigate('company')}>Empresa</FLink>
            <FLink to="app" onClick={() => navigate('app')}>App</FLink>
            <FLink to="news" onClick={() => navigate('news')}>Recursos</FLink>
            <FLink to="contact" onClick={() => navigate('contact')}>Contacto</FLink>
          </div>
          <div>
            <h3>Legal</h3>
            <FLink to="privacy" onClick={() => navigate('privacy')}>Privacidad</FLink>
            <FLink to="cookies" onClick={() => navigate('cookies')}>Cookies</FLink>
            <FLink to="legal" onClick={() => navigate('legal')}>Aviso Legal</FLink>
            <FLink to="terms" onClick={() => navigate('terms')}>Términos</FLink>
            <button type="button" className="pv-footer-cookie-link" onClick={openCookiePreferences}>Configurar cookies</button>
          </div>
          <div className="pv-footer-newsletter">
            <h3>Newsletter</h3>
            <p>Recibe recursos técnicos y novedades de producto. Sin ruido comercial.</p>
            {window.NewsletterForm ? <window.NewsletterForm compact /> : null}
          </div>
        </div>
      </div>

      <div className="pv-footer-mega" aria-hidden="true">PYROVOLT</div>

      <div className="pv-footer-bottom">
        <Logo size={16} />
        <div className="pv-footer-bottom-center">
          <span>© 2026 Pyrovolt. Todos los derechos reservados.</span>
          <a href={`mailto:${FOOTER_EMAIL}`} className="pv-footer-email">{FOOTER_EMAIL}</a>
        </div>
        <div className="pv-footer-social">
          <a
            href="https://www.linkedin.com/company/pyrovolt"
            target="_blank"
            rel="noopener noreferrer"
            className="pv-footer-social-link"
            aria-label="Pyrovolt en LinkedIn"
          >
            <svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true">
              <path d="M20.447 20.452h-3.554v-5.569c0-1.328-.027-3.037-1.852-3.037-1.853 0-2.136 1.445-2.136 2.939v5.667H9.351V9h3.414v1.561h.046c.477-.9 1.637-1.85 3.37-1.85 3.601 0 4.267 2.37 4.267 5.455v6.286zM5.337 7.433a2.062 2.062 0 0 1-2.063-2.065 2.064 2.064 0 1 1 2.063 2.065zm1.782 13.019H3.555V9h3.564v11.452zM22.225 0H1.771C.792 0 0 .774 0 1.729v20.542C0 23.227.792 24 1.771 24h20.451C23.2 24 24 23.227 24 22.271V1.729C24 .774 23.2 0 22.222 0h.003z"/>
            </svg>
          </a>
        </div>
      </div>
    </footer>
  );
}

const FOOTER_ROUTE_PATHS = {
  home: '#home',
  chargers: '#chargers',
  storage: '#storage',
  mt: '#mt',
  app: '#app',
  company: '#company',
  contact: '#contact',
  news: '#news',
  privacy: '#privacy',
  cookies: '#cookies',
  legal: '#legal',
  terms: '#terms',
};

const FLink = ({ children, onClick, to, href }) => (
  <a href={href || (to ? (FOOTER_ROUTE_PATHS[to] || `#${to}`) : '/')} onClick={(e) => { if (onClick) { e.preventDefault(); onClick(); } }}
     style={{ display: 'block', fontSize: 14, color: 'var(--fg-dim)', padding: '6px 0', transition: 'color 160ms' }}
     onMouseEnter={(e) => e.currentTarget.style.color = 'var(--fg)'}
     onMouseLeave={(e) => e.currentTarget.style.color = 'var(--fg-dim)'}>
    {children}
  </a>
);

// ─────────────────── SHARED PRODUCT-PAGE COMPONENTS ───────────────────

// Italic Fraunces emphasis — shorthand
const It = ({ children, style }) => (
  <em className="it" style={style}>{children}</em>
);

// Render a string with `*word*` segments converted to italic Fraunces
function renderItalic(str) {
  if (!str) return null;
  if (typeof str !== 'string') return str;
  const parts = str.split(/(\*[^*]+\*)/g).filter(Boolean);
  return parts.map((p, i) => p.startsWith('*') && p.endsWith('*')
    ? <It key={i}>{p.slice(1, -1)}</It>
    : <React.Fragment key={i}>{p}</React.Fragment>);
}

// Get a line-style icon by key. Safe even if Icons is not yet defined.
function getIcon(key, props = {}) {
  const ic = (typeof Icons !== 'undefined' && Icons && Icons[key]) ? Icons[key] : null;
  if (!ic) return <DefaultIcon/>;
  return React.cloneElement(ic, { width: props.width || 22, height: props.height || 22 });
}

// Breadcrumb (← Volver a [section])
function Breadcrumb({ to, label, navigate }) {
  return (
    <a href={FOOTER_ROUTE_PATHS[to] || `#${to}`} onClick={e => { e.preventDefault(); navigate(to); }}
       style={{ display: 'inline-flex', alignItems: 'center', gap: 8, fontSize: 13, fontFamily: 'var(--font-mono)', color: 'var(--fg-dim)', padding: '8px 0', transition: 'color 160ms' }}
       onMouseEnter={e => e.currentTarget.style.color = 'var(--fg)'}
       onMouseLeave={e => e.currentTarget.style.color = 'var(--fg-dim)'}>
      ← Volver a {label}
    </a>
  );
}

// Spec chip row (e.g. "30 kW · 150-1000 € · CCS2")
function SpecChips({ chips }) {
  return (
    <div style={{ display: 'flex', gap: 12, flexWrap: 'wrap' }}>
      {chips.map((c, i) => (
        <div key={i} style={{
          display: 'inline-flex', alignItems: 'center', gap: 8,
          padding: '8px 16px', borderRadius: 999,
          background: 'var(--bg-soft)', border: '1px solid var(--line)',
          fontSize: 13, fontFamily: 'var(--font-mono)', color: 'var(--fg)',
          letterSpacing: 0.4,
        }}>
          {c.icon && <span style={{ color: 'var(--accent)' }}>{c.icon}</span>}
          <span>{c.label}</span>
        </div>
      ))}
    </div>
  );
}

// Feature strip — editorial bar, no card backgrounds
function FeatureRow({ items }) {
  return (
    <div className="pv-detail-feature-strip">
      {items.map((it, i) => (
        <div key={i} className="pv-detail-feature-item">
          <div className="pv-detail-feature-icon">
            {typeof it.icon === 'string' ? getIcon(it.icon) : (it.icon || <DefaultIcon/>)}
          </div>
          <div className="pv-detail-feature-title">{it.title}</div>
          <div className="pv-detail-feature-desc">{it.desc}</div>
        </div>
      ))}
    </div>
  );
}

function DetailBlueprint({ kind = 'charger', title, lead, metrics = [] }) {
  const presets = {
    charger: {
      eyebrow: 'Arquitectura de recarga',
      title: title || 'Potencia distribuida, controlada desde una sola cabina.',
      lead: lead || 'La energía se reparte entre surtidores según demanda real, reduciendo obra civil y manteniendo potencia disponible para cada vehículo.',
      nodes: ['Red MT', 'Cabina', 'Surtidores', 'Vehículos'],
      rail: 'OCPP · CCS2 · Power sharing',
    },
    storage: {
      eyebrow: 'Arquitectura BESS',
      title: title || 'Almacenamiento conectado a renovables, red y operación.',
      lead: lead || 'PCS, BMS y EMS coordinan la energía disponible para hibridación, peak shaving, respaldo y servicios de red.',
      nodes: ['Renovable', 'PCS', 'BESS', 'SCADA'],
      rail: 'LFP · EMS · Grid-forming',
    },
    mt: {
      eyebrow: 'Arquitectura MT',
      title: title || 'Conexión, protección y transformación en una cadena limpia.',
      lead: lead || 'Celdas, transformadores y protecciones se integran para llevar potencia de media tensión al punto exacto de consumo.',
      nodes: ['Celdas', 'Trafo', 'BT', 'Control'],
      rail: 'SF6-free · IEC · Protecciones',
    },
  };
  const data = presets[kind] || presets.charger;
  const usableMetrics = (metrics || []).slice(0, 3);

  return (
    <section className={'pv-detail-blueprint-band pv-detail-blueprint-' + kind}>
      <div className="container pv-detail-blueprint-inner">
        <div className="pv-detail-blueprint-copy">
          <div className="pv-detail-kicker">{data.eyebrow}</div>
          <h2 className="pv-detail-h2">{data.title}</h2>
          <p>{data.lead}</p>
        </div>
        <div className="pv-detail-blueprint-diagram" aria-label={data.eyebrow}>
          <div className="pv-detail-blueprint-rail">
            {data.nodes.map((node, i) => (
              <div key={node} className="pv-detail-blueprint-node" style={{ '--i': i }}>
                <span>{String(i + 1).padStart(2, '0')}</span>
                <b>{node}</b>
              </div>
            ))}
          </div>
          <div className="pv-detail-blueprint-meta">{data.rail}</div>
          <div className="pv-detail-blueprint-metrics">
            {usableMetrics.map((met, i) => (
              <div key={i}>
                <strong>{met.value}</strong>
                <span>{met.label}</span>
              </div>
            ))}
          </div>
        </div>
      </div>
    </section>
  );
}

const DefaultIcon = () => (
  <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round">
    <circle cx="12" cy="12" r="9"/>
    <path d="M12 7v5l3 2"/>
  </svg>
);

// Line-icon library — reusable across the site
const Icons = {
  bolt: (
    <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round">
      <path d="M13 2L4 14h7l-1 8 9-12h-7l1-8z"/>
    </svg>
  ),
  battery: (
    <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round">
      <rect x="2" y="7" width="18" height="10" rx="1.5"/>
      <path d="M22 11v2"/><path d="M6 10v4M9 10v4M12 10v4"/>
    </svg>
  ),
  shield: (
    <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round">
      <path d="M12 2l8 4v6c0 5-3.5 8.5-8 10-4.5-1.5-8-5-8-10V6l8-4z"/>
    </svg>
  ),
  layers: (
    <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round">
      <path d="M12 2L2 8l10 6 10-6-10-6z"/><path d="M2 14l10 6 10-6"/>
    </svg>
  ),
  cpu: (
    <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round">
      <rect x="5" y="5" width="14" height="14" rx="2"/><rect x="9" y="9" width="6" height="6"/>
      <path d="M9 1v3M15 1v3M9 20v3M15 20v3M1 9h3M1 15h3M20 9h3M20 15h3"/>
    </svg>
  ),
  network: (
    <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round">
      <circle cx="12" cy="5" r="2.5"/><circle cx="5" cy="19" r="2.5"/><circle cx="19" cy="19" r="2.5"/>
      <path d="M12 7v4M12 11l-5 6M12 11l5 6"/>
    </svg>
  ),
  charger: (
    <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round">
      <rect x="6" y="2" width="12" height="20" rx="2"/>
      <path d="M9 7h6M9 11h6"/><circle cx="10" cy="17" r="1.5"/><circle cx="14" cy="17" r="1.5"/>
    </svg>
  ),
  leaf: (
    <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round">
      <path d="M21 3c-7 0-13 4-15 11-1 4 1 7 4 7 7 0 13-9 11-18z"/><path d="M5 21c2-5 6-9 12-12"/>
    </svg>
  ),
  expand: (
    <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round">
      <path d="M3 8V3h5M21 8V3h-5M3 16v5h5M21 16v5h-5"/>
    </svg>
  ),
  settings: (
    <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round">
      <circle cx="12" cy="12" r="3"/>
      <path d="M19 12a7 7 0 0 0-.1-1.2l2-1.6-2-3.4-2.4.9a7 7 0 0 0-2-1.2L14 3h-4l-.5 2.5a7 7 0 0 0-2 1.2l-2.4-.9-2 3.4 2 1.6A7 7 0 0 0 5 12a7 7 0 0 0 .1 1.2l-2 1.6 2 3.4 2.4-.9a7 7 0 0 0 2 1.2L10 21h4l.5-2.5a7 7 0 0 0 2-1.2l2.4.9 2-3.4-2-1.6c.06-.4.1-.8.1-1.2z"/>
    </svg>
  ),
  thermal: (
    <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round">
      <path d="M14 4a2 2 0 0 0-4 0v10a4 4 0 1 0 4 0V4z"/>
    </svg>
  ),
  cycle: (
    <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round">
      <path d="M3 12a9 9 0 0 1 15.5-6.5L21 8"/><path d="M21 4v4h-4"/>
      <path d="M21 12a9 9 0 0 1-15.5 6.5L3 16"/><path d="M3 20v-4h4"/>
    </svg>
  ),
};

// Bar chart for capacities/power ranges
function BarChart({ bars, max, dark = true, accent }) {
  const m = max || Math.max(...bars.map(b => b.v));
  const c = accent || 'var(--accent)';
  return (
    <div style={{ display: 'flex', alignItems: 'flex-end', gap: 8, height: 140, padding: '0 4px' }}>
      {bars.map((b, i) => {
        const h = (b.v / m) * 100;
        return (
          <div key={i} style={{ flex: 1, display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 8, height: '100%', justifyContent: 'flex-end' }}>
            <div style={{ fontFamily: 'var(--font-mono)', fontSize: 10, color: 'var(--fg-dim)', opacity: 0.8 }}>{b.v}{b.unit || ''}</div>
            <div style={{ width: '100%', height: `${h}%`, background: c, borderRadius: 3, opacity: b.dim ? 0.4 : 1, boxShadow: b.dim ? 'none' : `0 0 16px var(--accent-glow)` }}/>
            <div style={{ fontFamily: 'var(--font-mono)', fontSize: 10, color: 'var(--fg-mute)' }}>{b.label}</div>
          </div>
        );
      })}
    </div>
  );
}

// Specs table — 2-col grid
function SpecsTable({ groups }) {
  return (
    <div className="pv-spec-table">
      {groups.map((g, gi) => (
        <React.Fragment key={gi}>
          <div className="pv-spec-group-title">{g.title}</div>
          <table className="pv-spec-table-inner">
            <tbody>
              {g.rows.map(([k, v], ri) => (
                <tr key={ri} className="pv-spec-row">
                  <td className="pv-spec-key">{k}</td>
                  <td className="pv-spec-value">{v}</td>
                </tr>
              ))}
            </tbody>
          </table>
        </React.Fragment>
      ))}
    </div>
  );
}

// CTA strip — bottom of every product page
function CTAStrip({ navigate, title = '¿No sabes qué solución se adapta mejor a tu proyecto?', cta = 'Pedir asesoramiento' }) {
  return (
    <section className="container" style={{ padding: 'var(--pad-x)', paddingTop: 60, paddingBottom: 60 }}>
      <div className="cta-strip" style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', flexWrap: 'wrap', gap: 24 }}>
        <h3 style={{ position: 'relative', fontFamily: 'var(--font-display)', fontSize: 'clamp(22px, 2.6vw, 32px)', fontWeight: 500, letterSpacing: '-0.02em', maxWidth: '24ch', color: '#FFFFFF', lineHeight: 1.2 }}>
          {title}
        </h3>
        <a href="/contact" onClick={e => { e.preventDefault(); navigate('contact'); }}
           style={{ position: 'relative', padding: '14px 28px', borderRadius: 999, background: '#FFFFFF', color: 'var(--accent-deep)', fontWeight: 600, fontSize: 14, transition: 'transform 160ms' }}
           onMouseEnter={e => e.currentTarget.style.transform = 'translateY(-2px)'}
           onMouseLeave={e => e.currentTarget.style.transform = 'translateY(0)'}>
          {cta} →
        </a>
      </div>
    </section>
  );
}

// Energy coil signature — appears at the bottom of the home and key pages
function EnergyCoil({ width = 200 }) {
  return (
    <svg viewBox="0 0 300 60" style={{ width, height: 'auto' }}>
      <defs>
        <linearGradient id="coilGrad" x1="0" x2="1">
          <stop offset="0%" stopColor="var(--accent-deep)"/>
          <stop offset="50%" stopColor="var(--accent)"/>
          <stop offset="100%" stopColor="var(--accent-soft)"/>
        </linearGradient>
      </defs>
      <path d="M2 30 Q20 30 30 30 T60 30 Q80 30 90 10 T120 50 Q140 30 150 30 T180 30 Q200 30 210 10 T240 50 Q260 30 270 30 T298 30"
            fill="none" stroke="url(#coilGrad)" strokeWidth="2.4" strokeLinecap="round"/>
    </svg>
  );
}

// Big italic signature with coil — used on home bottom
function MadeSimpleSignature({ size = 'clamp(48px, 8vw, 120px)' }) {
  return (
    <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 24, flexWrap: 'wrap' }}>
      <h2 style={{ fontFamily: 'var(--font-display)', fontSize: size, fontWeight: 500, letterSpacing: '-0.035em', lineHeight: 1, margin: 0 }}>
        Energy <em className="it" style={{ background: 'linear-gradient(135deg, var(--accent-soft), var(--accent))', WebkitBackgroundClip: 'text', WebkitTextFillColor: 'transparent', backgroundClip: 'text' }}>Made</em> Simple.
      </h2>
      <EnergyCoil width={220}/>
    </div>
  );
}

// ─────────────────── PRODUCT VISUALS (inline SVG) ───────────────────
// Stylized renders of the actual Pyrovolt product forms — used as hero imagery.
// Intentionally minimal/architectural. No photo-realism; treat as a placeholder-but-considered.

function ChargerCompact({ w = 400, accent }) {
  const a = accent || 'var(--accent)';
  return (
    <svg viewBox="0 0 400 560" style={{ width: w, height: 'auto' }}>
      <defs>
        <linearGradient id="cc1" x1="0" x2="0" y1="0" y2="1">
          <stop offset="0%" stopColor="#2A2A2A"/>
          <stop offset="100%" stopColor="#0F0F0F"/>
        </linearGradient>
      </defs>
      {/* Shadow */}
      <ellipse cx="200" cy="540" rx="150" ry="10" fill="#000" opacity="0.4"/>
      {/* Body */}
      <rect x="90" y="40" width="220" height="480" rx="18" fill="url(#cc1)" stroke="#3A3A3A" strokeWidth="1"/>
      {/* Top screen */}
      <rect x="110" y="70" width="180" height="110" rx="8" fill="#0A0A0A"/>
      <rect x="120" y="85" width="60" height="4" rx="2" fill={a} opacity="0.9"/>
      <rect x="120" y="95" width="90" height="10" rx="2" fill="#F4F2ED"/>
      <rect x="120" y="112" width="130" height="4" rx="2" fill="#6B6963"/>
      <rect x="120" y="152" width="40" height="18" rx="4" fill={a}/>
      {/* Brand stripe */}
      <rect x="90" y="200" width="220" height="2" fill={a}/>
      {/* Connector área */}
      <rect x="120" y="230" width="160" height="130" rx="12" fill="#0A0A0A" stroke="#2A2A2A"/>
      <circle cx="200" cy="295" r="38" fill="#1A1A1A" stroke="#3A3A3A" strokeWidth="2"/>
      <circle cx="200" cy="295" r="22" fill="#0A0A0A"/>
      <circle cx="186" cy="288" r="4" fill="#6B6963"/>
      <circle cx="214" cy="288" r="4" fill="#6B6963"/>
      <rect x="192" y="300" width="16" height="6" rx="2" fill="#6B6963"/>
      {/* Cable */}
      <path d="M 200 360 Q 200 420 150 440 Q 100 460 90 500" stroke="#1A1A1A" strokeWidth="14" fill="none" strokeLinecap="round"/>
      <path d="M 200 360 Q 200 420 150 440 Q 100 460 90 500" stroke="#2A2A2A" strokeWidth="10" fill="none" strokeLinecap="round"/>
      {/* Label */}
      <rect x="110" y="380" width="180" height="110" rx="8" fill="none" stroke="#242424"/>
      <text x="120" y="405" fill="#A6A39C" fontFamily="var(--font-mono)" fontSize="10" letterSpacing="1">COMPACT DC · 30 kW</text>
      <rect x="120" y="418" width="8" height="60" fill={a}/>
      <text x="140" y="440" fill="#F4F2ED" fontFamily="var(--font-display)" fontSize="28" fontWeight="700">30</text>
      <text x="190" y="440" fill="#A6A39C" fontFamily="var(--font-mono)" fontSize="11">kW</text>
      <text x="140" y="470" fill="#6B6963" fontFamily="var(--font-mono)" fontSize="9">CCS2 · OCPP</text>
    </svg>
  );
}

function ChargerHPC({ w = 400, accent }) {
  const a = accent || 'var(--accent)';
  return (
    <svg viewBox="0 0 400 720" style={{ width: w, height: 'auto' }}>
      <defs>
        <linearGradient id="hpc1" x1="0" x2="0" y1="0" y2="1">
          <stop offset="0%" stopColor="#222"/>
          <stop offset="100%" stopColor="#0A0A0A"/>
        </linearGradient>
      </defs>
      <ellipse cx="200" cy="700" rx="170" ry="10" fill="#000" opacity="0.4"/>
      {/* Body */}
      <rect x="60" y="30" width="280" height="660" rx="22" fill="url(#hpc1)" stroke="#3A3A3A"/>
      {/* Vent stripes */}
      {[0,1,2,3,4,5,6,7].map(i => (
        <rect key={i} x="80" y={60 + i*8} width="240" height="2" fill="#1A1A1A"/>
      ))}
      {/* Screen */}
      <rect x="90" y="160" width="220" height="150" rx="8" fill="#050505"/>
      <text x="106" y="190" fill={a} fontFamily="var(--font-mono)" fontSize="10" letterSpacing="1">● CONNECTED</text>
      <text x="106" y="230" fill="#F4F2ED" fontFamily="var(--font-display)" fontSize="42" fontWeight="700">400</text>
      <text x="200" y="230" fill="#A6A39C" fontFamily="var(--font-mono)" fontSize="14">kW</text>
      <rect x="106" y="250" width="188" height="3" rx="1.5" fill="#1A1A1A"/>
      <rect x="106" y="250" width="120" height="3" rx="1.5" fill={a}/>
      <text x="106" y="275" fill="#6B6963" fontFamily="var(--font-mono)" fontSize="10">62% · 18 min restantes</text>
      {/* Accent stripe */}
      <rect x="60" y="330" width="280" height="2" fill={a}/>
      {/* Dual connectors */}
      <g>
        <rect x="90" y="360" width="100" height="180" rx="10" fill="#080808" stroke="#2A2A2A"/>
        <circle cx="140" cy="420" r="32" fill="#1A1A1A" stroke="#3A3A3A"/>
        <circle cx="140" cy="420" r="18" fill="#050505"/>
        <text x="100" y="510" fill="#6B6963" fontFamily="var(--font-mono)" fontSize="9">CCS2 · A</text>
        <rect x="100" y="520" width="30" height="4" fill={a}/>
      </g>
      <g>
        <rect x="210" y="360" width="100" height="180" rx="10" fill="#080808" stroke="#2A2A2A"/>
        <circle cx="260" cy="420" r="32" fill="#1A1A1A" stroke="#3A3A3A"/>
        <circle cx="260" cy="420" r="18" fill="#050505"/>
        <text x="220" y="510" fill="#6B6963" fontFamily="var(--font-mono)" fontSize="9">CCS2 · B</text>
      </g>
      {/* Base */}
      <rect x="60" y="580" width="280" height="110" rx="4" fill="#0A0A0A" stroke="#242424"/>
      <text x="80" y="620" fill="#A6A39C" fontFamily="var(--font-mono)" fontSize="10" letterSpacing="1">HPC · 320–800 kW</text>
      <text x="80" y="660" fill="#6B6963" fontFamily="var(--font-mono)" fontSize="9">LIQUID COOLED · 500 A</text>
    </svg>
  );
}

function ChargerModular({ w = 400, accent }) {
  const a = accent || 'var(--accent)';
  return (
    <svg viewBox="0 0 400 640" style={{ width: w, height: 'auto' }}>
      <ellipse cx="200" cy="620" rx="150" ry="10" fill="#000" opacity="0.4"/>
      <defs>
        <linearGradient id="mod1" x1="0" x2="0" y1="0" y2="1">
          <stop offset="0%" stopColor="#1E1E1E"/>
          <stop offset="100%" stopColor="#0A0A0A"/>
        </linearGradient>
      </defs>
      <rect x="80" y="40" width="240" height="570" rx="16" fill="url(#mod1)" stroke="#3A3A3A"/>
      {/* Modular power stack indicators */}
      {[0,1,2,3].map(i => (
        <g key={i}>
          <rect x="100" y={70 + i*50} width="200" height="38" rx="4" fill="#0A0A0A" stroke="#242424"/>
          <circle cx="112" cy={89 + i*50} r="3" fill={i < 3 ? a : "#2A2A2A"}/>
          <text x="126" y={94 + i*50} fill="#A6A39C" fontFamily="var(--font-mono)" fontSize="10">PWR MODULE {i+1}</text>
          <text x="270" y={94 + i*50} fill={i < 3 ? "#F4F2ED" : "#6B6963"} fontFamily="var(--font-mono)" fontSize="10" textAnchor="end">{i < 3 ? "80 kW" : "—"}</text>
        </g>
      ))}
      <rect x="80" y="290" width="240" height="2" fill={a}/>
      {/* Display */}
      <rect x="100" y="310" width="200" height="120" rx="8" fill="#050505"/>
      <text x="120" y="350" fill="#F4F2ED" fontFamily="var(--font-display)" fontSize="48" fontWeight="700">240</text>
      <text x="230" y="350" fill={a} fontFamily="var(--font-mono)" fontSize="14">kW</text>
      <text x="120" y="380" fill="#6B6963" fontFamily="var(--font-mono)" fontSize="10">3 MODULES ACTIVE</text>
      <rect x="120" y="395" width="160" height="2" fill="#1A1A1A"/>
      <rect x="120" y="395" width="120" height="2" fill={a}/>
      {/* Connector */}
      <rect x="130" y="450" width="140" height="140" rx="10" fill="#080808" stroke="#2A2A2A"/>
      <circle cx="200" cy="520" r="40" fill="#1A1A1A" stroke="#3A3A3A" strokeWidth="2"/>
      <circle cx="200" cy="520" r="24" fill="#050505"/>
      <text x="148" y="478" fill="#A6A39C" fontFamily="var(--font-mono)" fontSize="9" letterSpacing="1">MODULAR DC</text>
    </svg>
  );
}

function ChargerDistributed({ w = 800, accent }) {
  const a = accent || 'var(--accent)';
  return (
    <svg viewBox="0 0 800 560" style={{ width: w, height: 'auto' }}>
      <defs>
        <linearGradient id="dist1" x1="0" x2="0" y1="0" y2="1">
          <stop offset="0%" stopColor="#1E1E1E"/>
          <stop offset="100%" stopColor="#0A0A0A"/>
        </linearGradient>
      </defs>
      <ellipse cx="400" cy="540" rx="340" ry="8" fill="#000" opacity="0.4"/>
      {/* Power cabinet (left) */}
      <g>
        <rect x="40" y="80" width="200" height="440" rx="10" fill="url(#dist1)" stroke="#3A3A3A"/>
        {[0,1,2,3,4,5].map(i => (
          <rect key={i} x="60" y={110 + i*30} width="160" height="20" rx="3" fill="#0A0A0A" stroke="#242424"/>
        ))}
        {[0,1,2,3,4,5].map(i => (
          <circle key={i} cx="72" cy={120 + i*30} r="2.5" fill={i < 5 ? a : "#2A2A2A"}/>
        ))}
        <rect x="60" y="300" width="160" height="60" rx="6" fill="#050505"/>
        <text x="72" y="325" fill={a} fontFamily="var(--font-mono)" fontSize="9" letterSpacing="1">POWER CABINET</text>
        <text x="72" y="348" fill="#F4F2ED" fontFamily="var(--font-display)" fontSize="22" fontWeight="700">1250 kW</text>
        <rect x="60" y="380" width="160" height="120" rx="4" fill="#080808" stroke="#242424"/>
        <text x="72" y="410" fill="#6B6963" fontFamily="var(--font-mono)" fontSize="9">96% eff · 400V AC in</text>
      </g>
      {/* Connecting bus lines */}
      <g stroke={a} strokeWidth="1.5" fill="none" opacity="0.9">
        <path d="M 240 200 L 320 200 L 320 180"/>
        <path d="M 240 300 L 440 300"/>
        <path d="M 240 400 L 560 400 L 560 380"/>
      </g>
      <circle cx="240" cy="200" r="3" fill={a}/>
      <circle cx="240" cy="300" r="3" fill={a}/>
      <circle cx="240" cy="400" r="3" fill={a}/>
      {/* Dispenser 1 */}
      <Dispenser x={290} label="1 MW" accent={a} subtitle="MCS"/>
      {/* Dispenser 2 */}
      <Dispenser x={410} label="500 kW" accent={a} subtitle="CCS2 · A+B"/>
      {/* Dispenser 3 */}
      <Dispenser x={530} label="400 kW" accent={a} subtitle="CCS2"/>
      {/* Dispenser 4 */}
      <Dispenser x={650} label="400 kW" accent={a} subtitle="CCS2"/>
    </svg>
  );
}

const Dispenser = ({ x, label, accent, subtitle }) => (
  <g>
    <rect x={x} y="180" width="100" height="340" rx="8" fill="#1A1A1A" stroke="#3A3A3A"/>
    <rect x={x+10} y="200" width="80" height="60" rx="4" fill="#050505"/>
    <text x={x+18} y={224} fill={accent} fontFamily="var(--font-mono)" fontSize="8" letterSpacing="1">● READY</text>
    <text x={x+18} y={247} fill="#F4F2ED" fontFamily="var(--font-display)" fontSize="15" fontWeight="700">{label}</text>
    <rect x={x+10} y="280" width="80" height="3" fill={accent}/>
    <circle cx={x+50} cy={340} r="26" fill="#0A0A0A" stroke="#3A3A3A"/>
    <circle cx={x+50} cy={340} r="14" fill="#050505"/>
    <text x={x+50} y={400} textAnchor="middle" fill="#6B6963" fontFamily="var(--font-mono)" fontSize="8">{subtitle}</text>
    <path d={`M ${x+50} 380 Q ${x+50} 450 ${x+20} 470`} stroke="#2A2A2A" strokeWidth="6" fill="none" strokeLinecap="round"/>
  </g>
);

function BESSContainer({ w = 800, accent }) {
  const a = accent || 'var(--accent)';
  return (
    <svg viewBox="0 0 800 400" style={{ width: w, height: 'auto' }}>
      <defs>
        <linearGradient id="bess1" x1="0" x2="0" y1="0" y2="1">
          <stop offset="0%" stopColor="#1E1E1E"/>
          <stop offset="100%" stopColor="#0A0A0A"/>
        </linearGradient>
      </defs>
      <ellipse cx="400" cy="380" rx="340" ry="8" fill="#000" opacity="0.4"/>
      {/* Container body */}
      <rect x="40" y="80" width="720" height="280" rx="4" fill="url(#bess1)" stroke="#3A3A3A"/>
      {/* Corrugation */}
      {Array.from({length: 36}).map((_, i) => (
        <line key={i} x1={60 + i*19} y1="90" x2={60 + i*19} y2="350" stroke="#0A0A0A" strokeWidth="1"/>
      ))}
      {/* Door panels */}
      <rect x="80" y="120" width="140" height="200" rx="2" fill="none" stroke="#2A2A2A" strokeWidth="1.5"/>
      <rect x="580" y="120" width="140" height="200" rx="2" fill="none" stroke="#2A2A2A" strokeWidth="1.5"/>
      {/* Center branding / display */}
      <rect x="280" y="160" width="240" height="120" rx="8" fill="#050505" stroke="#2A2A2A"/>
      <text x="300" y="190" fill={a} fontFamily="var(--font-mono)" fontSize="10" letterSpacing="1">● GRID CONNECTED</text>
      <text x="300" y="228" fill="#F4F2ED" fontFamily="var(--font-display)" fontSize="32" fontWeight="700">6.26</text>
      <text x="380" y="228" fill="#A6A39C" fontFamily="var(--font-mono)" fontSize="12">MWh</text>
      <rect x="300" y="240" width="200" height="3" fill="#1A1A1A"/>
      <rect x="300" y="240" width="150" height="3" fill={a}/>
      <text x="300" y="262" fill="#6B6963" fontFamily="var(--font-mono)" fontSize="9">SoC 76% · charging · 2.4 MW</text>
      {/* Corners */}
      <rect x="40" y="80" width="20" height="20" fill="none" stroke="#3A3A3A"/>
      <rect x="740" y="80" width="20" height="20" fill="none" stroke="#3A3A3A"/>
      <rect x="40" y="340" width="20" height="20" fill="none" stroke="#3A3A3A"/>
      <rect x="740" y="340" width="20" height="20" fill="none" stroke="#3A3A3A"/>
      {/* Labels */}
      <text x="60" y="70" fill="#6B6963" fontFamily="var(--font-mono)" fontSize="10" letterSpacing="1">PYROVOLT BESS · 20 FT · 6.26 MWh</text>
    </svg>
  );
}

function ESSCompact({ w = 400, accent }) {
  const a = accent || 'var(--accent)';
  return (
    <svg viewBox="0 0 400 520" style={{ width: w, height: 'auto' }}>
      <defs>
        <linearGradient id="ess1" x1="0" x2="0" y1="0" y2="1">
          <stop offset="0%" stopColor="#1E1E1E"/>
          <stop offset="100%" stopColor="#0A0A0A"/>
        </linearGradient>
      </defs>
      <ellipse cx="200" cy="500" rx="150" ry="8" fill="#000" opacity="0.4"/>
      <rect x="80" y="40" width="240" height="440" rx="14" fill="url(#ess1)" stroke="#3A3A3A"/>
      {/* Vents */}
      {Array.from({length: 12}).map((_, i) => (
        <line key={i} x1="100" y1={70 + i*4} x2="300" y2={70 + i*4} stroke="#0A0A0A" strokeWidth="1"/>
      ))}
      {/* Middle display */}
      <rect x="100" y="180" width="200" height="140" rx="10" fill="#050505" stroke="#242424"/>
      <text x="120" y="210" fill={a} fontFamily="var(--font-mono)" fontSize="10" letterSpacing="1">● ONLINE</text>
      <text x="120" y="250" fill="#F4F2ED" fontFamily="var(--font-display)" fontSize="40" fontWeight="700">261</text>
      <text x="230" y="250" fill="#A6A39C" fontFamily="var(--font-mono)" fontSize="12">kWh</text>
      <rect x="120" y="265" width="160" height="3" fill="#1A1A1A"/>
      <rect x="120" y="265" width="110" height="3" fill={a}/>
      <text x="120" y="290" fill="#6B6963" fontFamily="var(--font-mono)" fontSize="10">125 kW · 68% SoC</text>
      {/* Module indicators */}
      {[0,1,2,3,4].map(i => (
        <rect key={i} x={100 + i*42} y="360" width="32" height="70" rx="3" fill="#0A0A0A" stroke="#242424"/>
      ))}
      {[0,1,2,3,4].map(i => (
        <rect key={i} x={100 + i*42} y={430 - (10 + i*8)} width="32" height={10 + i*8} rx="3" fill={a} opacity={0.8}/>
      ))}
      <text x="100" y="460" fill="#6B6963" fontFamily="var(--font-mono)" fontSize="9" letterSpacing="1">ESS COMPACT · 5 MODULES</text>
    </svg>
  );
}

Object.assign(window, {
  Logo, LogoMark, Nav, Footer, FLink,
  ChargerCompact, ChargerModular, ChargerHPC, ChargerDistributed,
  BESSContainer, ESSCompact,
  // Shared product-page components
  It, renderItalic, getIcon,
  Breadcrumb, SpecChips, FeatureRow, DetailBlueprint, Icons, BarChart, SpecsTable,
  CTAStrip, EnergyCoil, MadeSimpleSignature,
});
