// website/shell.jsx — shared UI for Xaduno marketing site

const BRAND_GRAD = 'linear-gradient(135deg, #B45EE8 0%, #E14A8D 50%, #5FCAF9 100%)';
const BRAND_GRAD_TIGHT = 'linear-gradient(135deg, #B45EE8 0%, #E14A8D 100%)';

function XWordmark({ size = 28, color = '#0F172A', dot = true }) {
  return (
    <div style={{
      display: 'inline-flex', alignItems: 'baseline', gap: size * 0.08,
      fontFamily: 'Poppins, Inter, sans-serif',
      fontWeight: 700, fontSize: size, lineHeight: 1,
      letterSpacing: '-0.02em', color,
    }}>
      <span>XADUNO</span>
      {dot && (
        <span style={{
          display: 'inline-block',
          width: size * 0.22, height: size * 0.22, borderRadius: '50%',
          background: BRAND_GRAD_TIGHT,
          transform: `translateY(${size * 0.02}px)`,
        }}></span>
      )}
    </div>
  );
}

function SwissPill({ children = 'Hosting in der Schweiz', dark = false }) {
  return (
    <div className={dark ? 'pill dark' : 'pill'}>
      <svg width="14" height="14" viewBox="0 0 32 32" aria-hidden="true">
        <rect width="32" height="32" rx="4" fill="#DC2626"/>
        <rect x="13" y="6" width="6" height="20" fill="#FEFEFE"/>
        <rect x="6" y="13" width="20" height="6" fill="#FEFEFE"/>
      </svg>
      {children}
    </div>
  );
}

function ArrowRight({ size = 14 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
      <path d="M5 12h14M13 5l7 7-7 7"/>
    </svg>
  );
}

function Check({ size = 14, color = '#16A34A' }) {
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth="3" strokeLinecap="round" strokeLinejoin="round">
      <polyline points="20 6 9 17 4 12"/>
    </svg>
  );
}

function Dash({ size = 14, color = '#CBD5E1' }) {
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth="3" strokeLinecap="round" strokeLinejoin="round">
      <line x1="5" y1="12" x2="19" y2="12"/>
    </svg>
  );
}

// Top nav with active state based on hash route
function TopNav({ route, setRoute }) {
  const [funkOpen, setFunkOpen] = React.useState(false);
  const [mobileOpen, setMobileOpen] = React.useState(false);
  const funkRef = React.useRef(null);
  const closeTimer = React.useRef(null);

  const openFunk = () => {
    if (closeTimer.current) clearTimeout(closeTimer.current);
    setFunkOpen(true);
  };
  const closeFunkSoon = () => {
    closeTimer.current = setTimeout(() => setFunkOpen(false), 120);
  };

  // Close on click outside (for click-to-open behavior)
  React.useEffect(() => {
    const onClick = (e) => {
      if (funkRef.current && !funkRef.current.contains(e.target)) setFunkOpen(false);
    };
    document.addEventListener('mousedown', onClick);
    return () => document.removeEventListener('mousedown', onClick);
  }, []);

  const funktionen = [
    { id: 'ats',          label: 'Bewerbungsmanagement', tag: 'ATS',
      desc: 'Pipeline, Team-Feedback, Dokumente anfordern.', tone: '#335CFF' },
    { id: 'multiposting', label: 'Multiposting', tag: 'Reichweite',
      desc: 'Eine Stelle, 100+ Plattformen, ein Klick.', tone: '#6941C6' },
    { id: 'dossier',      label: 'Mitarbeiterdossier', tag: 'HRIS',
      desc: 'Stammdaten, Verträge, Mitarbeitergespräche.', tone: '#F97316' },
  ];

  const funktionenActive = funktionen.some(f => f.id === route);

  return (
    <React.Fragment>
    <nav className="nav">
      <div className="nav-inner">
        <a href="#home" onClick={(e) => { e.preventDefault(); setRoute('home'); }} style={{ display: 'inline-flex' }}>
          <XWordmark size={26} />
        </a>
        <div className="nav-links">
          {/* Funktionen dropdown */}
          <div
            ref={funkRef}
            onMouseEnter={openFunk}
            onMouseLeave={closeFunkSoon}
            style={{ position: 'relative' }}>
            <button
              type="button"
              onClick={() => setFunkOpen(o => !o)}
              className={`nav-link ${funktionenActive ? 'active' : ''}`}
              style={{
                display: 'inline-flex', alignItems: 'center', gap: 6,
                background: funktionenActive || funkOpen ? 'var(--bg-sub)' : 'transparent',
                border: 'none', cursor: 'pointer',
              }}>
              Funktionen
              <svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round"
                style={{ transform: funkOpen ? 'rotate(180deg)' : 'none', transition: 'transform 200ms ease' }}>
                <polyline points="6 9 12 15 18 9"/>
              </svg>
            </button>

            {funkOpen && (
              <div style={{
                position: 'absolute', top: 'calc(100% + 8px)', left: '50%',
                transform: 'translateX(-50%)',
                width: 380,
                background: 'var(--bg-white)',
                border: '1px solid var(--stroke-soft)',
                borderRadius: 16,
                boxShadow: 'var(--shadow-md)',
                padding: 8,
                zIndex: 60,
                animation: 'xaduno-dd 180ms ease',
              }}>
                <style>{`@keyframes xaduno-dd { from { opacity: 0; transform: translate(-50%, -4px); } to { opacity: 1; transform: translate(-50%, 0); } }`}</style>
                {funktionen.map(f => (
                  <a key={f.id} href={`#${f.id}`}
                    onClick={(e) => { e.preventDefault(); setRoute(f.id); setFunkOpen(false); }}
                    style={{
                      display: 'flex', alignItems: 'flex-start', gap: 14,
                      padding: '14px 14px', borderRadius: 12,
                      textDecoration: 'none',
                      transition: 'background 150ms ease',
                      cursor: 'pointer',
                      background: route === f.id ? 'var(--bg-sub)' : 'transparent',
                    }}
                    onMouseEnter={(e) => { if (route !== f.id) e.currentTarget.style.background = 'var(--bg-weak)'; }}
                    onMouseLeave={(e) => { if (route !== f.id) e.currentTarget.style.background = 'transparent'; }}>
                    <div style={{
                      width: 36, height: 36, borderRadius: 10,
                      background: `${f.tone}15`, color: f.tone,
                      display: 'flex', alignItems: 'center', justifyContent: 'center',
                      flexShrink: 0,
                    }}>
                      {f.id === 'ats' && <NavIconPipeline />}
                      {f.id === 'multiposting' && <NavIconShare />}
                      {f.id === 'dossier' && <NavIconUser />}
                    </div>
                    <div style={{ flex: 1, minWidth: 0 }}>
                      <div style={{
                        display: 'flex', alignItems: 'center', gap: 8, marginBottom: 4,
                      }}>
                        <div style={{
                          fontFamily: 'Poppins', fontWeight: 600, fontSize: 14,
                          color: 'var(--text-strong)', letterSpacing: '-0.01em',
                        }}>{f.label}</div>
                        <div style={{
                          padding: '2px 6px', borderRadius: 4,
                          background: `${f.tone}15`, color: f.tone,
                          fontFamily: 'Inter', fontSize: 9, fontWeight: 700,
                          letterSpacing: '0.05em', textTransform: 'uppercase',
                        }}>{f.tag}</div>
                      </div>
                      <div style={{
                        fontFamily: 'Inter', fontSize: 12, color: 'var(--text-sub)',
                        lineHeight: 1.45,
                      }}>{f.desc}</div>
                    </div>
                  </a>
                ))}
              </div>
            )}
          </div>

          <a href="#preise"
            onClick={(e) => { e.preventDefault(); setRoute('preise'); }}
            className={`nav-link ${route === 'preise' ? 'active' : ''}`}>
            Preise
          </a>
          <a href="#uns"
            onClick={(e) => { e.preventDefault(); setRoute('uns'); }}
            className={`nav-link ${route === 'uns' ? 'active' : ''}`}>
            Über uns
          </a>
        </div>
        <a href="#demo" className="btn btn-primary">
          Demo anfragen
          <ArrowRight size={13} />
        </a>

        {/* Hamburger button — mobile only */}
        <button className="nav-hamburger" onClick={() => setMobileOpen(o => !o)} aria-label="Menu">
          {mobileOpen ? (
            <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round">
              <line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/>
            </svg>
          ) : (
            <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round">
              <line x1="3" y1="6" x2="21" y2="6"/><line x1="3" y1="12" x2="21" y2="12"/><line x1="3" y1="18" x2="21" y2="18"/>
            </svg>
          )}
        </button>
      </div>
    </nav>

    {/* Mobile overlay menu */}
    <div className={`nav-mobile-overlay${mobileOpen ? ' open' : ''}`}>
        <div className="nav-mobile-header">
          <XWordmark size={24} />
          <button className="nav-hamburger" style={{ display: 'flex' }} onClick={() => setMobileOpen(false)} aria-label="Schliessen">
            <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round">
              <line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/>
            </svg>
          </button>
        </div>
        <div className="nav-mobile-links">
          {[
            { id: 'home', label: 'Home' },
            { id: 'ats', label: 'Bewerbungsmanagement' },
            { id: 'multiposting', label: 'Multiposting' },
            { id: 'dossier', label: 'Mitarbeiterdossier' },
            { id: 'preise', label: 'Preise' },
            { id: 'uns', label: 'Über uns' },
          ].map(item => (
            <button key={item.id}
              className={`nav-mobile-link${route === item.id ? ' active' : ''}`}
              onClick={() => { setRoute(item.id); setMobileOpen(false); }}>
              {item.label}
            </button>
          ))}
        </div>
        <div className="nav-mobile-footer">
          <a href="#demo" className="btn btn-primary" onClick={() => setMobileOpen(false)}>
            Demo anfragen <ArrowRight size={13} />
          </a>
        </div>
      </div>
    </React.Fragment>
  );
}

function NavIconPipeline() {
  // Candidate profile card — avatar + info lines + footer rows (ATS)
  return (
    <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round">
      <rect x="3" y="4" width="18" height="16" rx="2.5"/>
      <circle cx="8.5" cy="10" r="2"/>
      <line x1="13" y1="9" x2="18" y2="9"/>
      <line x1="13" y1="11.5" x2="17" y2="11.5"/>
      <line x1="6" y1="15.5" x2="18" y2="15.5"/>
      <line x1="6" y1="17.5" x2="13" y2="17.5"/>
    </svg>
  );
}
function NavIconShare() {
  // Hub-and-spoke — a central node broadcasting to four corner endpoints (Multiposting)
  return (
    <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round">
      <circle cx="12" cy="12" r="3"/>
      <line x1="9.6" y1="9.6" x2="5.5" y2="5.5"/>
      <line x1="14.4" y1="9.6" x2="18.5" y2="5.5"/>
      <line x1="9.6" y1="14.4" x2="5.5" y2="18.5"/>
      <line x1="14.4" y1="14.4" x2="18.5" y2="18.5"/>
      <circle cx="4" cy="4" r="1.6"/>
      <circle cx="20" cy="4" r="1.6"/>
      <circle cx="4" cy="20" r="1.6"/>
      <circle cx="20" cy="20" r="1.6"/>
    </svg>
  );
}
function NavIconUser() {
  // Folder/dossier with a person silhouette inside (HRIS)
  return (
    <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round">
      <path d="M3 7v12a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2V9a2 2 0 0 0-2-2h-7l-2-3H5a2 2 0 0 0-2 2z"/>
      <circle cx="12" cy="13" r="1.8"/>
      <path d="M9 18a3 3 0 0 1 6 0"/>
    </svg>
  );
}

function Footer() {
  return (
    <footer className="footer">
      <div className="container footer-grid">
        <div>
          <XWordmark size={28} color="#FEFEFE" />
          <p style={{ fontFamily: 'Inter', fontSize: 13, lineHeight: 1.55, color: 'rgba(254,254,254,0.6)', marginTop: 16, maxWidth: 280 }}>
            Das Recruiting- und Mitarbeiter-System für Schweizer KMU. Entwickelt von SPRUNG. in Visp.
          </p>
          <div style={{ marginTop: 20 }}>
            <SwissPill dark>Hosting in der Schweiz</SwissPill>
          </div>
        </div>
        <div>
          <h5>Produkt</h5>
          <a href="#home">Home</a>
          <a href="#ats">ATS</a>
          <a href="#multiposting">Multiposting</a>
          <a href="#dossier">Mitarbeiterdossier</a>
          <a href="#preise">Preise</a>
        </div>
        <div>
          <h5>Unternehmen</h5>
          <a href="#uns">Über Xaduno</a>
          <a href="#kontakt">Kontakt</a>
          <a href="#demo">Demo anfragen</a>
        </div>
        <div>
          <h5>Rechtliches</h5>
          <a href="#datenschutz">Datenschutz</a>
          <a href="#impressum">Impressum</a>
          <a href="#">AGB</a>
        </div>
      </div>
      <div className="container" style={{ borderTop: '1px solid rgba(254,254,254,0.10)', marginTop: 48, paddingTop: 24, display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
        <div style={{ fontFamily: 'Inter', fontSize: 12, color: 'rgba(254,254,254,0.5)' }}>
          © 2025 SPRUNG. · Bahnhofplatz 1a, 3930 Visp
        </div>
        <div style={{ fontFamily: 'Inter', fontSize: 12, color: 'rgba(254,254,254,0.5)' }}>
          xaduno.ch
        </div>
      </div>
    </footer>
  );
}

// Generic feature item w/ icon
function FeatureItem({ icon, title, desc }) {
  return (
    <div className="card feature-card">
      <div style={{
        width: 44, height: 44, borderRadius: 12,
        background: 'var(--bg-sub)',
        display: 'flex', alignItems: 'center', justifyContent: 'center',
        color: 'var(--text-strong)', marginBottom: 18,
      }}>
        {icon}
      </div>
      <div className="h-card" style={{ marginBottom: 8 }}>{title}</div>
      <div className="body" style={{ color: 'var(--text-sub)' }}>{desc}</div>
    </div>
  );
}

// 3-step process row
function StepRow({ steps }) {
  return (
    <div className="grid-3">
      {steps.map((s, i) => (
        <div key={i} className="card">
          <div style={{
            display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
            width: 36, height: 36, borderRadius: 999,
            background: BRAND_GRAD, color: '#FEFEFE',
            fontFamily: 'Poppins', fontWeight: 600, fontSize: 14,
            marginBottom: 18,
          }}>
            {String(i + 1).padStart(2, '0')}
          </div>
          <div className="h-card" style={{ marginBottom: 8 }}>{s.title}</div>
          <div className="body" style={{ color: 'var(--text-sub)' }}>{s.desc}</div>
        </div>
      ))}
    </div>
  );
}

// CTA block
function CTABlock({ headline, accent, sub, btnLabel = 'Demo anfragen' }) {
  return (
    <section className="section dark" style={{ position: 'relative', overflow: 'hidden' }}>
      <div style={{
        position: 'absolute', top: -200, right: -200, width: 600, height: 600,
        borderRadius: '50%', filter: 'blur(140px)', opacity: 0.4,
        background: 'radial-gradient(circle, #B45EE8 0%, #E14A8D 40%, transparent 70%)',
      }}></div>
      <div className="container" style={{ position: 'relative', textAlign: 'center' }}>
        <h2 className="h-section" style={{ maxWidth: 800, margin: '0 auto 20px' }}>
          {headline}{' '}
          {accent && <span className="gradient-word">{accent}</span>}
        </h2>
        {sub && <p className="lead" style={{ maxWidth: 640, margin: '0 auto 32px' }}>{sub}</p>}
        <a href="#demo" className="btn btn-on-dark">{btnLabel} <ArrowRight size={13} /></a>
      </div>
    </section>
  );
}

Object.assign(window, {
  XWordmark, SwissPill, ArrowRight, Check, Dash,
  TopNav, Footer, FeatureItem, StepRow, CTABlock,
  BRAND_GRAD, BRAND_GRAD_TIGHT,
});
