// Site chrome shared by every route: brand mark, nav, footer, section wrappers, app-store links.

const APP_STORE_URL = 'https://apps.apple.com/ro/app/id0000000000';

function openAppStore() { window.open(APP_STORE_URL, '_blank', 'noopener'); }

function appShot(name) { return 'assets/app-' + name + ((window.__siteLang === 'RO') ? '-ro' : '') + '.png'; }

// Phone shot with bezel-shaped skeleton until the image decodes, then fades in.

// Shared brand mark — accent badge with the antler logo
function BrandMark({ T, size = 44, dark = false }) {
  return (
    <div style={{
      width: size, height: size, borderRadius: size * 0.30,
      background: dark ? T.accent : T.accent,
      display: 'flex', alignItems: 'center', justifyContent: 'center',
      boxShadow: SH(`0 6px 16px color-mix(in oklab, ${T.accent} 30%, transparent)`),
    }}>
      <img src="assets/antler-white.png" alt="" style={{ width: size * 0.62, height: size * 0.62, objectFit: 'contain', marginTop: size * 0.03 }} />
    </div>
  );
}

// ── Screen 1 · Start ─────────────────────────────────────────

// Phone shot with bezel-shaped skeleton until the image decodes, then fades in.
function ShotImg({ src, alt, height }) {
  const [ok, setOk] = React.useState(false);
  const ref = React.useRef(null);
  React.useEffect(() => { const el = ref.current; if (el && el.complete && el.naturalWidth) setOk(true); }, [src]);
  React.useEffect(() => { setOk(false); }, [src]);
  return (
    <div className={'hb-shot-wrap' + (ok ? ' is-loaded' : '')} style={{ height, aspectRatio: '429 / 901' }}>
      <img ref={ref} src={src} alt={alt} loading="lazy" decoding="async" onLoad={() => setOk(true)} />
    </div>
  );
}

function LangToggle({ T, lang, setLang }) {
  return (
    <div style={{ display: 'flex', alignItems: 'center', gap: 2, padding: 3, borderRadius: 99, background: T.tint(7), border: `1px solid ${T.line}`, flexShrink: 0 }}>
      {['EN', 'RO'].map((l) => {
        const on = lang === l;
        return (
          <span key={l} onClick={() => setLang(l)} className={on ? '' : 'hb-nav'} style={{
            padding: '5px 11px', borderRadius: 99, fontSize: 12, fontWeight: on ? 800 : 650, letterSpacing: 0.4, cursor: 'pointer',
            color: on ? '#fff' : T.mut,
            background: on ? `linear-gradient(150deg, ${T.heroA} 0%, ${T.heroDeep} 100%)` : 'transparent',
          }}>{l}</span>
        );
      })}
    </div>
  );
}

// Real anchors, so links can be opened in a new tab, copied, and crawled.
// A plain left click is intercepted and routed in-page instead of reloading.
function SiteLink({ to, doc, nav, children, className, style }) {
  const href = pathForRoute(to, doc);
  const onClick = (e) => {
    if (e.metaKey || e.ctrlKey || e.shiftKey || e.altKey || e.button !== 0) return;
    e.preventDefault();
    nav(to, doc);
  };
  return (
    <a href={href} onClick={onClick} className={className}
       style={{ textDecoration: 'none', cursor: 'pointer', ...style }}>{children}</a>
  );
}

function SiteNav({ T, page, nav, lang, setLang }) {
  const links = [['home', 'Product'], ['features', 'Features'], ['pricing', 'Pricing'], ['contact', 'Contact'], ['legal', 'Legal']];
  return (
    <div style={{ position: 'sticky', top: 0, zIndex: 40, background: T.glass, backdropFilter: 'blur(14px)', borderBottom: `1px solid ${T.line}` }}>
      <div style={{ maxWidth: 1100, margin: '0 auto', padding: '14px 28px', display: 'flex', alignItems: 'center', gap: 18, flexWrap: 'wrap' }}>
        <SiteLink to="home" nav={nav} style={{ display: 'flex', alignItems: 'center', gap: 11 }}>
          <BrandMark T={T} size={36} />
          <div style={{ display: 'flex', flexDirection: 'column' }}>
            <span style={{ fontFamily: T.df, fontSize: 16.5, fontWeight: 700, color: T.ink, letterSpacing: 0.2 }}>Venator</span>
            <span style={{ fontSize: 10, fontWeight: 600, color: T.mut, letterSpacing: 0.6, textTransform: 'uppercase', whiteSpace: 'nowrap' }}>Exam prep</span>
          </div>
        </SiteLink>
        <nav style={{ display: 'flex', alignItems: 'center', gap: 4, flexWrap: 'wrap', margin: '0 auto' }}>
          {links.map(([id, l]) => (
            <SiteLink key={id} to={id} nav={nav} className="hb-nav" style={{
              padding: '8px 14px', borderRadius: 99, fontSize: 13.5,
              fontWeight: page === id ? 750 : 600,
              color: page === id ? T.deep : T.mut,
              background: page === id ? T.tint(9) : 'transparent',
            }}>{l}</SiteLink>
          ))}
        </nav>
        <LangToggle T={T} lang={lang} setLang={setLang} />
      </div>
    </div>
  );
}

// Floating app-store button, mirroring MakerBubble in the opposite corner.
// One button for now; it becomes a small menu when Android ships.
function DownloadBubble({ T }) {
  return (
    <div style={{ position: 'fixed', right: 24, bottom: 26, zIndex: 60 }}>
      <Btn T={T} h={46} fs={14.5} icon="download" onClick={openAppStore}
           style={{ padding: '0 20px', boxShadow: SH(`0 10px 26px color-mix(in oklab, ${T.accent} 34%, transparent)`) }}>
        Download for iPhone
      </Btn>
    </div>
  );
}

function SiteFooter({ T, nav }) {
  const col = { display: 'flex', flexDirection: 'column', gap: 9 };
  const head = { fontSize: 11.5, fontWeight: 800, letterSpacing: 0.9, textTransform: 'uppercase', color: T.faint };
  const link = (label, page) => (
    <SiteLink to={page} nav={nav} className="hb-link" style={{ fontSize: 13.5, fontWeight: 600, color: T.mut }}>{label}</SiteLink>
  );
  return (
    <div style={{ borderTop: `1px solid ${T.line}`, background: T.card }}>
      <div style={{ maxWidth: 1100, margin: '0 auto', padding: '44px 28px 30px' }}>
        <div style={{ display: 'grid', gridTemplateColumns: 'repeat(auto-fit, minmax(200px, 1fr))', gap: 32 }}>
          <div style={{ ...col, gap: 12 }}>
            <div style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
              <BrandMark T={T} size={34} />
              <span style={{ fontFamily: T.df, fontSize: 16, fontWeight: 700, color: T.ink }}>Venator</span>
            </div>
            <p style={{ margin: 0, fontSize: 13, lineHeight: 1.5, color: T.mut, maxWidth: 260, textWrap: 'pretty' }}>
              The smart way to prepare for the Romanian hunter's permit exam.
            </p>
          </div>
          <div style={col}>
            <span style={head}>Product</span>
            {link('Features', 'features')}
            {link('Pricing', 'pricing')}
            {link('Contact', 'contact')}
          </div>
          <div style={col}>
            <span style={head}>Legal</span>
            <SiteLink to="legal" doc="terms" nav={nav} className="hb-link" style={{ fontSize: 13.5, fontWeight: 600, color: T.mut }}>Terms of Service</SiteLink>
            <SiteLink to="legal" doc="privacy" nav={nav} className="hb-link" style={{ fontSize: 13.5, fontWeight: 600, color: T.mut }}>Privacy Policy</SiteLink>
            <SiteLink to="legal" doc="cookies" nav={nav} className="hb-link" style={{ fontSize: 13.5, fontWeight: 600, color: T.mut }}>Cookie Policy</SiteLink>
          </div>
        </div>
        <div style={{ height: 1, background: T.line, margin: '30px 0 18px' }}></div>
        <div style={{ display: 'flex', justifyContent: 'space-between', gap: 12, flexWrap: 'wrap' }}>
          <span style={{ fontSize: 12, color: T.faint, fontWeight: 550 }}>© 2026 Venator. All rights reserved.</span>
          <span style={{ fontSize: 12, color: T.faint, fontWeight: 550 }}>Study app built on the official exam materials.</span>
        </div>
      </div>
    </div>
  );
}

const MAKER_LINKS = [
  { href: 'https://alexvasiliu.me', label: 'Website', icon: <svg width="16" height="16" viewBox="0 0 24 24" fill="none"><circle cx="12" cy="12" r="8.6" stroke="currentColor" strokeWidth="1.7" /><path d="M3.4 12h17.2M12 3.4c2.5 2.6 2.5 15.4 0 17.2M12 3.4c-2.5 2.6-2.5 15.4 0 17.2" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" /></svg> },
  { href: 'https://www.linkedin.com/in/alex-vasiliu/', label: 'LinkedIn', icon: <svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor"><path d="M4.6 9.1h2.85V19.4H4.6zM6.03 4.3a1.66 1.66 0 1 1 0 3.32 1.66 1.66 0 0 1 0-3.32zM9.9 9.1h2.73v1.42h.04c.38-.71 1.32-1.46 2.72-1.46 2.91 0 3.45 1.84 3.45 4.22V19.4h-2.85v-5.42c0-1.29-.24-2.6-1.8-2.6-1.5 0-1.72 1.11-1.72 2.52V19.4H9.9z" /></svg> },
  { href: 'https://www.fiverr.com/alexandruvasili', label: 'Fiverr', icon: <svg width="16" height="16" viewBox="0 0 24 24" fill="none"><path d="M4.4 8.8h4.2M6.5 8.8V19M6.5 8.8V7.4c0-1.6 1.3-2.9 2.9-2.9h1.1" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" /><path d="M14.6 8.8V19" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" /><circle cx="14.6" cy="5.9" r="1.25" fill="currentColor" /></svg> },
  { href: 'mailto:alexg.vasiliu@gmail.com', label: 'Email', icon: <svg width="16" height="16" viewBox="0 0 24 24" fill="none"><rect x="3.2" y="5.4" width="17.6" height="13.2" rx="2.4" stroke="currentColor" strokeWidth="1.7" /><path d="M4.4 7.4l7.6 5.6 7.6-5.6" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round" /></svg> },
];

function MakerBubble({ T }) {
  const [open, setOpen] = React.useState(false);
  const R = 92;
  const angles = [4, 34, 64, 94];
  return (
    <div className={'hb-bub' + (open ? ' on' : '')} style={{ pointerEvents: open ? 'auto' : 'none' }}
      onMouseLeave={() => setOpen(false)} onBlur={(e) => { if (!e.currentTarget.contains(e.relatedTarget)) setOpen(false); }}>
      {MAKER_LINKS.map((l, i) => {
        const ext = !l.href.startsWith('mailto:');
        const a = (angles[i] * Math.PI) / 180;
        return (
          <a key={l.label} className="hb-blob" href={l.href} target={ext ? '_blank' : undefined} rel={ext ? 'noopener' : undefined} aria-label={l.label} onFocus={() => setOpen(true)}
            style={{ '--bx': `${Math.round(Math.cos(a) * R)}px`, '--by': `${-Math.round(Math.sin(a) * R)}px`, background: `linear-gradient(150deg, ${T.accent} 0%, ${T.deep} 100%)`, color: '#fff', boxShadow: `0 6px 16px color-mix(in oklab, ${T.accent} 28%, transparent)` }}>
            {l.icon}
            <span className="hb-blob-tip">{l.label}</span>
          </a>
        );
      })}
      <div className="hb-bub-core" tabIndex={0} onMouseEnter={() => setOpen(true)} onFocus={() => setOpen(true)}
        style={{ background: `linear-gradient(150deg, ${T.accent} 0%, ${T.deep} 100%)`, color: '#fff', fontFamily: T.df }}>
        <span className="hb-bub-init">AV</span>
        <span className="hb-bub-full" style={{ fontSize: 16, fontWeight: 800, letterSpacing: -0.4 }}>Alex<br />Vasiliu</span>
      </div>
    </div>
  );
}

function MakerLink({ T, href, label, children }) {
  const ext = !href.startsWith('mailto:');
  return (
    <a className="hb-maker" href={href} target={ext ? '_blank' : undefined} rel={ext ? 'noopener' : undefined} title={label} aria-label={label} style={{ width: 32, height: 32, borderRadius: 99, border: `1px solid ${T.line}`, background: T.tint(4), color: T.mut, display: 'inline-flex', alignItems: 'center', justifyContent: 'center', textDecoration: 'none' }}>{children}</a>
  );
}

function SiteSection({ children, bg, style }) {
  return (
    <div style={{ background: bg || 'transparent' }}>
      <div style={{ maxWidth: 1100, margin: '0 auto', padding: '64px 28px', ...style }}>{children}</div>
    </div>
  );
}

// `level` only changes the tag, not the look: a page whose title is this block
// (Legal) needs it to be the h1, everything else keeps h2.
function SiteH2({ T, children, sub, center = false, level = 2 }) {
  const Heading = level === 1 ? 'h1' : 'h2';
  return (
    <div style={{ marginBottom: 34, maxWidth: 620, ...(center ? { marginLeft: 'auto', marginRight: 'auto', textAlign: 'center' } : {}) }}>
      <Heading style={{ margin: 0, fontFamily: T.df, fontSize: 'clamp(24px, 4.8vw, 31px)', fontWeight: 700, color: T.ink, letterSpacing: -0.4, lineHeight: 1.15, textWrap: 'pretty' }}>{children}</Heading>
      {sub && <p style={{ margin: '10px 0 0', fontSize: 15.5, lineHeight: 1.5, color: T.mut, textWrap: 'pretty' }}>{sub}</p>}
    </div>
  );
}

function Eyebrow({ T, children, light = false }) {
  return <span style={{ display: 'block', fontSize: 12, fontWeight: 800, letterSpacing: 1.4, textTransform: 'uppercase', color: light ? 'rgba(255,255,255,0.75)' : T.deep }}>{children}</span>;
}

function PageIntro({ T, eyebrow, title, sub }) {
  return (
    <div style={{ textAlign: 'center', maxWidth: 640, margin: '0 auto 44px' }}>
      {eyebrow && <Eyebrow T={T}>{eyebrow}</Eyebrow>}
      <h1 style={{ margin: '12px 0 0', fontFamily: T.df, fontSize: 'clamp(30px, 6.6vw, 44px)', fontWeight: 700, letterSpacing: -0.8, lineHeight: 1.08, textWrap: 'pretty', color: T.ink }}>{title}</h1>
      <p style={{ margin: '14px auto 0', fontSize: 15.5, lineHeight: 1.55, color: T.mut, maxWidth: 600, textWrap: 'pretty' }}>{sub}</p>
    </div>
  );
}

Object.assign(window, { SiteLink, DownloadBubble, APP_STORE_URL, openAppStore, appShot, BrandMark, ShotImg, LangToggle, SiteNav, SiteFooter, MakerBubble, MakerLink, SiteSection, SiteH2, Eyebrow, PageIntro });
