// Shared site chrome — design tokens, nav, footer, and prose primitives.
// Loaded before website.jsx (homepage) and before each static page (terms,
// licenses, support) so all pages reuse the same header/footer/design system.
// Follows the repo convention: define locally, expose via Object.assign(window).

const W_TEAL = '#0D7377';
const W_TEAL_DARK = '#0A5C5F';
const W_TEAL_LIGHT = '#E0F5F5';
const W_AMBER = '#F5A623';
const W_AMBER_LIGHT = '#FEF3E2';
const W_PURPLE = '#8B5CF6';
const W_PURPLE_LIGHT = '#F3EEFF';
const W_TEXT = '#1A1D21';
const W_TEXT_2 = '#6B7280';
const W_SEP = '#E5E7EB';
const W_MAX = 1120;

// Logo: homepage inlines it via inline-assets.js (window.LOGO_SRC); static
// pages fall back to the file. Absolute path so it resolves on /terms etc.
const LOGO = window.LOGO_SRC || '/assets/logo.png';

// Responsive hook — inline styles can't be targeted by CSS media queries,
// so we branch on viewport width at render time.
function useIsMobile(bp = 768) {
  const [m, setM] = React.useState(
    typeof window !== 'undefined' ? window.innerWidth < bp : false
  );
  React.useEffect(() => {
    const onResize = () => setM(window.innerWidth < bp);
    onResize();
    window.addEventListener('resize', onResize);
    return () => window.removeEventListener('resize', onResize);
  }, [bp]);
  return m;
}

function Wrap({ children, style }) {
  const m = useIsMobile();
  return <div style={{ maxWidth: W_MAX, margin: '0 auto', padding: m ? '0 20px' : '0 32px', ...style }}>{children}</div>;
}

function SectionTag({ color = W_TEAL, bg = W_TEAL_LIGHT, children }) {
  return (
    <div style={{ display: 'inline-flex', alignItems: 'center', gap: 6, background: bg, color, borderRadius: 999, padding: '6px 14px', fontSize: 13, fontWeight: 600, letterSpacing: 0.2 }}>
      {children}
    </div>
  );
}

function H2({ children, style }) {
  const m = useIsMobile();
  const base = (style && style.fontSize) || 40;
  const mob = m ? { fontSize: Math.min(28, base), letterSpacing: -0.5 } : null;
  return <h2 style={{ fontSize: 40, fontWeight: 700, letterSpacing: -1, lineHeight: 1.15, color: W_TEXT, ...style, ...mob }}>{children}</h2>;
}

function Lead({ children, style }) {
  return <p style={{ fontSize: 18, lineHeight: 1.6, color: W_TEXT_2, ...style }}>{children}</p>;
}

function StoreBadge({ kind }) {
  const isApple = kind === 'apple';
  return (
    <a href="/#download" aria-label={isApple ? 'Download on the App Store' : 'Get it on Google Play'} style={{
      display: 'inline-flex', alignItems: 'center', gap: 11, background: '#000', color: '#fff',
      borderRadius: 9, padding: '8px 16px', cursor: 'pointer', minWidth: 176, height: 56,
      border: '1px solid #4B5563', textDecoration: 'none', boxSizing: 'border-box',
    }}>
      {isApple ? (
        <svg width="27" height="32" viewBox="0 0 24 29" fill="#fff" aria-hidden="true">
          <path d="M19.86 15.24c.04 4.35 3.82 5.8 3.86 5.82-.03.1-.6 2.07-1.99 4.1-1.2 1.75-2.45 3.5-4.41 3.53-1.93.04-2.55-1.14-4.76-1.14-2.2 0-2.89 1.1-4.72 1.18-1.9.07-3.34-1.9-4.55-3.64C.82 21.52-1.06 14.98 1.48 10.6c1.26-2.17 3.52-3.55 5.97-3.58 1.86-.04 3.62 1.25 4.76 1.25 1.14 0 3.28-1.55 5.52-1.32.94.04 3.58.38 5.27 2.86-.14.08-3.15 1.84-3.14 5.43zM16.24 4.6c1-1.22 1.68-2.9 1.5-4.6-1.45.06-3.2.97-4.24 2.18-.93 1.07-1.75 2.8-1.53 4.44 1.62.13 3.26-.82 4.27-2.02z"/>
        </svg>
      ) : (
        <svg width="26" height="29" viewBox="0 0 26 29" aria-hidden="true">
          <defs>
            <linearGradient id="gp1" x1="0%" y1="0%" x2="100%" y2="50%"><stop offset="0%" stopColor="#00A0FF"/><stop offset="100%" stopColor="#00E2FF"/></linearGradient>
            <linearGradient id="gp2" x1="0%" y1="0%" x2="100%" y2="0%"><stop offset="0%" stopColor="#FFE000"/><stop offset="100%" stopColor="#FFA500"/></linearGradient>
            <linearGradient id="gp3" x1="0%" y1="0%" x2="100%" y2="100%"><stop offset="0%" stopColor="#FF3A44"/><stop offset="100%" stopColor="#C31162"/></linearGradient>
            <linearGradient id="gp4" x1="100%" y1="100%" x2="0%" y2="0%"><stop offset="0%" stopColor="#32A071"/><stop offset="100%" stopColor="#00F076"/></linearGradient>
          </defs>
          <path fill="url(#gp1)" d="M1.1.6C.7 1 .5 1.6.5 2.4v24.2c0 .8.2 1.4.6 1.8l.1.1 13.6-13.6v-.3L1.2.5l-.1.1z"/>
          <path fill="url(#gp2)" d="M19.3 19.5l-4.5-4.5v-.3l4.5-4.5.1.06 5.37 3.05c1.53.87 1.53 2.29 0 3.16l-5.37 3.05-.1.03z"/>
          <path fill="url(#gp3)" d="M19.4 19.44L14.8 14.85 1.1 28.5c.5.53 1.34.6 2.28.06l16.02-9.12"/>
          <path fill="url(#gp4)" d="M19.4 10.26L3.38.4C2.44-.14 1.6-.07 1.1.46l13.7 13.7 4.6-4.6z"/>
        </svg>
      )}
      <div style={{ display: 'flex', flexDirection: 'column', justifyContent: 'center' }}>
        <span style={{ fontSize: isApple ? 11 : 9.5, lineHeight: 1.1, opacity: 0.9, letterSpacing: isApple ? 0.1 : 0.6, textTransform: isApple ? 'none' : 'uppercase' }}>{isApple ? 'Download on the' : 'Get it on'}</span>
        <span style={{ fontSize: isApple ? 21 : 18.5, fontWeight: 500, lineHeight: 1.15, letterSpacing: -0.3, fontFamily: '-apple-system, BlinkMacSystemFont, Roboto, sans-serif' }}>{isApple ? 'App Store' : 'Google Play'}</span>
      </div>
    </a>
  );
}

// ─── Nav ─────────────────────────────────────────────────────
// Links are root-relative (/#how) so they work from the homepage AND from the
// static pages (they navigate home, then scroll).
const NAV_LINKS = [
  { href: '/#how', label: 'How it works' },
  { href: '/#features', label: 'Features' },
  { href: '/#ai', label: 'AI' },
  { href: '/#pricing', label: 'Pricing' },
  { href: '/#faq', label: 'FAQ' },
];

function SiteNav() {
  const m = useIsMobile();
  const [menuOpen, setMenuOpen] = React.useState(false);
  React.useEffect(() => { if (!m) setMenuOpen(false); }, [m]);
  return (
    <nav style={{ position: 'sticky', top: 0, zIndex: 100, background: 'rgba(250,251,252,0.9)', backdropFilter: 'blur(12px)', borderBottom: `1px solid ${W_SEP}` }}>
      <Wrap style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', height: m ? 56 : 64 }}>
        <a href="/" style={{ display: 'flex', alignItems: 'center', gap: 9, textDecoration: 'none' }}>
          <img src={LOGO} alt="BoxMind" style={{ width: 32, height: 32, borderRadius: 8, display: 'block' }}/>
          <span style={{ fontSize: 19, fontWeight: 700, color: W_TEAL, letterSpacing: -0.3 }}>BoxMind</span>
        </a>
        {m ? (
          <button
            aria-label={menuOpen ? 'Close menu' : 'Open menu'}
            aria-expanded={menuOpen}
            onClick={() => setMenuOpen(o => !o)}
            style={{ background: 'none', border: 'none', padding: 8, margin: -8, cursor: 'pointer', display: 'flex', alignItems: 'center', color: W_TEXT }}
          >
            <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round">
              {menuOpen ? (
                <><line x1="6" y1="6" x2="18" y2="18"/><line x1="18" y1="6" x2="6" y2="18"/></>
              ) : (
                <><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 style={{ display: 'flex', alignItems: 'center', gap: 28, fontSize: 14.5, fontWeight: 500 }}>
            {NAV_LINKS.map(l => <a key={l.href} href={l.href} style={{ color: W_TEXT_2 }}>{l.label}</a>)}
            <a href="/#download" style={{ background: W_TEAL, color: '#fff', borderRadius: 999, padding: '9px 20px', fontWeight: 600 }}>Get the app</a>
          </div>
        )}
      </Wrap>
      {m && menuOpen && (
        <div style={{ position: 'absolute', top: '100%', left: 0, right: 0, background: '#FAFBFC', borderBottom: `1px solid ${W_SEP}`, boxShadow: '0 12px 24px rgba(15,23,42,0.08)' }}>
          <div style={{ display: 'flex', flexDirection: 'column', padding: '6px 20px 18px', maxWidth: W_MAX, margin: '0 auto' }}>
            {NAV_LINKS.map(l => (
              <a key={l.href} href={l.href} onClick={() => setMenuOpen(false)} style={{ padding: '14px 4px', fontSize: 16, fontWeight: 500, color: W_TEXT, borderBottom: `1px solid ${W_SEP}` }}>{l.label}</a>
            ))}
            <a href="/#download" onClick={() => setMenuOpen(false)} style={{ marginTop: 16, background: W_TEAL, color: '#fff', borderRadius: 999, padding: '13px 0', textAlign: 'center', fontWeight: 600, fontSize: 15 }}>Get the app</a>
          </div>
        </div>
      )}
    </nav>
  );
}

// ─── Footer ──────────────────────────────────────────────────
const FOOTER_LINKS = [
  { href: '/privacy', label: 'Privacy' },
  { href: '/terms', label: 'Terms' },
  { href: '/support', label: 'Support' },
  { href: '/licenses', label: 'Licenses' },
];

function SiteFooter() {
  return (
    <footer style={{ borderTop: `1px solid ${W_SEP}`, padding: '40px 0', background: '#fff' }}>
      <Wrap style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', flexWrap: 'wrap', gap: 16 }}>
        <a href="/" style={{ display: 'flex', alignItems: 'center', gap: 8, textDecoration: 'none' }}>
          <img src={LOGO} alt="BoxMind" style={{ width: 26, height: 26, borderRadius: 7, display: 'block' }}/>
          <span style={{ fontSize: 15, fontWeight: 700, color: W_TEAL }}>BoxMind</span>
          <span style={{ fontSize: 13, color: W_TEXT_2, marginLeft: 8 }}>© 2026 Warp3D Studio</span>
        </a>
        <div style={{ display: 'flex', gap: 24, fontSize: 13.5, flexWrap: 'wrap' }}>
          {FOOTER_LINKS.map(l => <a key={l.href} href={l.href} style={{ color: W_TEXT_2 }}>{l.label}</a>)}
        </div>
      </Wrap>
    </footer>
  );
}

// ─── Prose primitives (for static content pages) ─────────────
function PageHead({ tag, title, sub }) {
  const m = useIsMobile();
  return (
    <div style={{ textAlign: 'center', maxWidth: 720, margin: '0 auto', paddingBottom: m ? 8 : 20 }}>
      {tag && <SectionTag>{tag}</SectionTag>}
      <h1 style={{ fontSize: m ? 32 : 44, fontWeight: 700, letterSpacing: -1, lineHeight: 1.12, color: W_TEXT, margin: '18px 0 12px' }}>{title}</h1>
      {sub && <Lead style={{ fontSize: m ? 15.5 : 17, maxWidth: 620, margin: '0 auto' }}>{sub}</Lead>}
    </div>
  );
}

function DocH2({ children }) {
  return <h2 style={{ fontSize: 21, fontWeight: 700, letterSpacing: -0.3, color: W_TEXT, margin: '38px 0 12px' }}>{children}</h2>;
}

function DocP({ children, style }) {
  return <p style={{ fontSize: 15.5, lineHeight: 1.7, color: W_TEXT_2, margin: '0 0 14px', ...style }}>{children}</p>;
}

function DocUL({ children }) {
  return <ul style={{ margin: '0 0 16px', paddingLeft: 22, display: 'flex', flexDirection: 'column', gap: 8 }}>{children}</ul>;
}

function DocLI({ children }) {
  return <li style={{ fontSize: 15.5, lineHeight: 1.6, color: W_TEXT_2 }}>{children}</li>;
}

// Standard page layout: sticky nav, centered content column, footer.
function PageShell({ children, maxWidth = 760 }) {
  const m = useIsMobile();
  return (
    <div>
      <SiteNav/>
      <main style={{ padding: m ? '40px 0 64px' : '64px 0 88px' }}>
        <Wrap style={{ maxWidth }}>{children}</Wrap>
      </main>
      <SiteFooter/>
    </div>
  );
}

Object.assign(window, {
  W_TEAL, W_TEAL_DARK, W_TEAL_LIGHT, W_AMBER, W_AMBER_LIGHT, W_PURPLE, W_PURPLE_LIGHT,
  W_TEXT, W_TEXT_2, W_SEP, W_MAX, LOGO,
  useIsMobile, Wrap, SectionTag, H2, Lead, StoreBadge,
  NAV_LINKS, SiteNav, FOOTER_LINKS, SiteFooter,
  PageHead, DocH2, DocP, DocUL, DocLI, PageShell,
});
