// Ring Facet shared design system
// Loaded by every page via: <script type="text/babel" src="shared/rf.jsx"></script>
// Requires: React, ReactDOM already on window. window.__clerkPk + window.__clerkReady set before this.

const P = {
  paper: '#F5F5F4', card: '#FFFFFF', ink: '#0A0B0D',
  ink90: 'rgba(10,11,13,0.92)', ink60: 'rgba(10,11,13,0.62)',
  ink40: 'rgba(10,11,13,0.40)', ink15: 'rgba(10,11,13,0.13)', ink08: 'rgba(10,11,13,0.08)',
  sans: '"Inter Tight", -apple-system, system-ui, sans-serif',
  mono: '"IBM Plex Mono", ui-monospace, monospace',
};

function PMono({ children, style }) {
  return <span style={{ fontFamily: P.mono, fontSize: 11, letterSpacing: 0.4, ...style }}>{children}</span>;
}

function PBtn({ children, primary, ghost, size = 'md', style, ...rest }) {
  const sz = { sm: { p: '8px 14px', f: 12.5 }, md: { p: '12px 18px', f: 13.5 }, lg: { p: '14px 22px', f: 14 } }[size];
  const base = { display: 'inline-flex', alignItems: 'center', gap: 8, padding: sz.p, fontFamily: P.sans, fontSize: sz.f, fontWeight: 500, letterSpacing: -0.1, cursor: 'pointer', borderRadius: 8, border: 'none', lineHeight: 1, textDecoration: 'none' };
  const tone = primary
    ? { background: P.ink, color: P.paper }
    : ghost
      ? { background: 'transparent', color: P.ink }
      : { background: P.card, color: P.ink, boxShadow: `inset 0 0 0 1px ${P.ink15}` };
  if (rest.href) return <a {...rest} style={{ ...base, ...tone, ...style }}>{children}</a>;
  return <button {...rest} style={{ ...base, ...tone, ...style }}>{children}</button>;
}

function PMark({ size = 22, dark }) {
  const c = dark ? P.paper : P.ink;
  const w = Math.round(size * 304 / 240);
  return (
    <svg width={w} height={size} viewBox="0 0 304 240">
      <path fill={c} d="M39.530842,195.883224C36.282921,204.070221 33.055267,212.265335 29.777617,220.440399C28.366095,223.960999 29.045757,225.700775 33.275211,225.678833C52.764465,225.577698 72.254776,225.588730 91.744118,225.682129C95.094978,225.698181 96.815269,224.374390 98.000458,221.252747C101.066803,213.176376 104.468513,205.227432 107.729820,197.224945C110.773277,189.757004 113.808365,182.285675 117.297928,173.707703C118.714073,176.494339 119.650429,178.038284 120.314888,179.691513C125.708038,193.110168 131.153549,206.509567 136.346298,220.005920C137.947479,224.167526 140.292847,225.811462 144.886337,225.749496C163.206757,225.502304 181.532455,225.661591 199.856171,225.639496C206.337158,225.631683 207.038376,224.560043 204.695038,218.728470C188.017395,177.225220 171.285782,135.743317 154.775085,94.173721C152.939560,89.552376 150.399048,87.871475 145.505280,87.931725C127.684135,88.151108 109.858505,88.028236 92.034637,87.993797C82.956413,87.976257 82.949341,87.934319 79.646797,96.151329C66.379539,129.161438 53.117085,162.173477 39.530842,195.883224M150.042313,79.562851C151.289230,85.620102 155.186401,86.878944 160.972397,86.772118C179.455795,86.430870 197.949585,86.627373 216.439163,86.682152C218.337479,86.687775 220.433792,86.056633 222.825424,88.288170C211.644089,116.259315 200.354523,144.501236 188.814346,173.370056C191.515076,173.542496 193.143402,173.735794 194.771866,173.736832C213.594727,173.748901 232.419342,173.590424 251.239227,173.823013C255.886810,173.880478 258.007050,172.072128 259.642273,167.938004C269.671417,142.582458 279.904572,117.307373 290.143677,92.035423C291.400940,88.932190 291.547638,86.087357 290.266785,82.927879C282.894257,64.741852 275.594391,46.525829 268.393677,28.271135C266.902130,24.489876 264.506378,22.925777 260.447235,22.933540C218.637421,23.013500 176.827347,22.966909 135.017441,23.022621C129.385651,23.030125 128.276535,24.948019 130.384689,30.244200C136.834778,46.448334 143.277420,62.655430 150.042313,79.562851z"/>
    </svg>
  );
}

function PPlaceholder({ label, height = 320, dark, style }) {
  const fg = dark ? 'rgba(245,245,244,0.10)' : 'rgba(10,11,13,0.06)';
  const bg = dark ? P.ink : '#EDEDEB';
  const tx = dark ? 'rgba(245,245,244,0.55)' : P.ink40;
  return (
    <div style={{ height, width: '100%', position: 'relative', background: `repeating-linear-gradient(90deg, ${fg} 0 1px, transparent 1px 18px), ${bg}`, display: 'flex', alignItems: 'center', justifyContent: 'center', ...style }}>
      <span style={{ fontFamily: P.mono, fontSize: 11, letterSpacing: 0.4, color: tx }}>{label}</span>
    </div>
  );
}

function useBreakpoint() {
  const [w, setW] = React.useState(window.innerWidth);
  React.useEffect(() => {
    const h = () => setW(window.innerWidth);
    window.addEventListener('resize', h);
    return () => window.removeEventListener('resize', h);
  }, []);
  return { isMobile: w < 768, isTablet: w < 1024, isCompactNav: w < 1280 };
}

function useClerkUser() {
  const [user, setUser] = React.useState(window.__clerkUser || null);
  React.useEffect(() => {
    const h = () => setUser(window.__clerkUser || null);
    window.addEventListener('clerkUserChanged', h);
    return () => window.removeEventListener('clerkUserChanged', h);
  }, []);
  return user;
}

function PNav({ active }) {
  const links = [['Solutions','solutions.html'],['Pricing','pricing.html'],['Docs','docs.html'],['Contact','contact.html']];
  const user = useClerkUser();
  const { isMobile, isTablet, isCompactNav } = useBreakpoint();
  const useMobileNav = isTablet;
  const navPadding = isCompactNav ? '8px 8px' : '8px 14px';
  const navLinkStyle = (isActive) => ({
    fontFamily: P.sans, fontSize: isCompactNav ? 13.5 : 14, color: isActive ? P.ink : P.ink60, textDecoration: 'none',
    fontWeight: isActive ? 600 : 400, padding: navPadding, borderRadius: 8,
    background: isActive ? P.ink08 : 'transparent', border: isActive ? `1px solid ${P.ink15}` : '1px solid transparent',
    whiteSpace: 'nowrap',
  });
  const strongLinkStyle = (isActive) => ({
    ...navLinkStyle(isActive),
    fontWeight: 600,
    color: isActive ? P.ink : P.ink,
  });
  const mobileLinkStyle = (isActive) => ({
    fontFamily: P.sans, fontSize: 15, color: isActive ? P.ink : P.ink60, textDecoration: 'none',
    fontWeight: isActive ? 500 : 400, padding: '10px 8px', borderRadius: 6, background: isActive ? P.ink08 : 'transparent',
  });
  const [open, setOpen] = React.useState(false);
  const [productsOpen, setProductsOpen] = React.useState(false);
  const signIn  = () => window.__clerkReady?.then(c => c.redirectToSignIn({ redirectUrl: window.location.href })).catch(console.error);
  const signUp  = () => window.__clerkReady?.then(c => c.redirectToSignUp({ redirectUrl: window.location.href })).catch(console.error);
  const signOut = () => window.__clerkReady?.then(c => c.signOut()).catch(console.error);
  const productsActive = active === 'Products' || active === 'Ring Builder' || active === 'Jewel Shots';
  const closeMenus = () => { setOpen(false); setProductsOpen(false); };
  React.useEffect(() => {
    document.body.style.overflow = (open && useMobileNav) ? 'hidden' : '';
    return () => { document.body.style.overflow = ''; };
  }, [open, useMobileNav]);
  return (
    <div style={{ borderBottom: `1px solid ${P.ink15}`, background: P.paper, position: 'sticky', top: 0, zIndex: 10 }}>
      <div style={{ maxWidth: 1320, margin: '0 auto', padding: useMobileNav ? '12px 16px' : '18px 40px', display: 'flex', alignItems: 'center' }}>
        <a href="index.html" onClick={closeMenus} style={{ display: 'flex', alignItems: 'center', gap: 10, textDecoration: 'none', flex: useMobileNav ? 1 : '0 0 220px', width: useMobileNav ? 'auto' : 220, minWidth: useMobileNav ? 0 : 220, flexShrink: 0 }}>
          <PMark size={28} />
          <span style={{ fontFamily: P.sans, fontWeight: 600, fontSize: 18, letterSpacing: -0.4, color: P.ink, whiteSpace: 'nowrap' }}>Ring Facet</span>
          {!useMobileNav && !isCompactNav && <span style={{ display: 'inline-flex', alignItems: 'center', gap: 5, marginLeft: 8, padding: '3px 8px', background: P.ink08, borderRadius: 4, border: `1px solid ${P.ink15}` }}><span style={{ fontFamily: P.mono, fontSize: 9, fontWeight: 700, letterSpacing: 0.8, padding: '1px 5px', background: P.ink, color: P.paper, borderRadius: 3 }}>NEW</span><span style={{ fontFamily: P.mono, fontSize: 10.5, letterSpacing: 0.2, color: P.ink60 }}>v1.3</span></span>}
        </a>
        {!useMobileNav && (
          <nav style={{ display: 'flex', flex: 1, justifyContent: 'center', gap: 2, margin: isCompactNav ? '0 12px' : '0 24px', alignItems: 'center', minWidth: 0, flexWrap: 'nowrap' }}>
            <a href="index.html" style={navLinkStyle(active === 'Home')}>Home</a>
            <a href="demo.html" style={navLinkStyle(active === 'Demo')}>Demo</a>
            <div onMouseEnter={()=>setProductsOpen(true)} onMouseLeave={()=>setProductsOpen(false)} style={{ position: 'relative' }}>
              <button onFocus={()=>setProductsOpen(true)} onClick={()=>setProductsOpen(o=>!o)} style={{ ...navLinkStyle(productsActive), cursor: 'pointer', background: productsActive ? P.ink08 : 'transparent', display: 'inline-flex', alignItems: 'center', gap: 7 }}>
                <span>Products</span><span style={{ color: productsActive ? P.ink : P.ink60, background: productsActive ? P.card : P.ink08, border: `1px solid ${P.ink15}`, borderRadius: 999, padding: '2px 6px', fontFamily: P.mono, fontSize: 9.5, letterSpacing: 0.2 }}>Browse</span>
              </button>
              {productsOpen && (
                <div style={{ position: 'absolute', top: '100%', left: '50%', transform: 'translateX(-50%)', width: 420, background: P.card, border: `1px solid ${P.ink15}`, borderRadius: 12, boxShadow: '0 18px 50px rgba(10,11,13,0.14)', padding: '18px 10px 10px', zIndex: 20 }}>
                  {[
                    ['Ring Builder','3D ring configurator for Shopify and custom storefronts.','ring-builder.html','demo.html'],
                    ['Jewel Shots','Product renders, videos, and 3D embeds from CAD files.','jewel-shots-product.html','demo.html?product=jewel-shots'],
                  ].map(([name, desc, href, demoHref]) => (
                    <div key={name} style={{ display: 'grid', gridTemplateColumns: '1fr auto', gap: 12, padding: 14, borderRadius: 8 }}>
                      <a href={href} onClick={closeMenus} style={{ textDecoration: 'none', color: P.ink }}>
                        <div style={{ fontFamily: P.sans, fontSize: 15, fontWeight: 600, marginBottom: 4 }}>{name}</div>
                        <div style={{ fontFamily: P.sans, fontSize: 13, color: P.ink60, lineHeight: 1.35 }}>{desc}</div>
                      </a>
                      <a href={demoHref} onClick={closeMenus} style={{ alignSelf: 'center', fontFamily: P.sans, fontSize: 12.5, color: P.ink, textDecoration: 'none', padding: '7px 10px', borderRadius: 6, border: `1px solid ${P.ink15}`, whiteSpace: 'nowrap' }}>Demo →</a>
                    </div>
                  ))}
                </div>
              )}
            </div>
            {links.map(([n, href]) => (
              <a key={n} href={href} style={navLinkStyle(n === active)}>{n}</a>
            ))}
            <a href="ring-builder-app.html" style={strongLinkStyle(active === 'Ring Builder App')}>My Ring Builder</a>
            <a href="jewel-shots.html" style={strongLinkStyle(active === 'My Models')}>My Models</a>
          </nav>
        )}
        {!useMobileNav && (
          <div style={{ display: 'flex', alignItems: 'center', gap: 12, marginLeft: 'auto' }}>
            {user ? (<>
              <a href="account.html" style={{ fontFamily: P.sans, fontSize: 13, color: P.ink60, textDecoration: 'none' }}>{user.firstName || user.emailAddresses?.[0]?.emailAddress?.split('@')[0]}</a>
              <PBtn size="sm" onClick={signOut}>Sign out</PBtn>
            </>) : (<>
              <button onClick={signIn} style={{ fontFamily: P.sans, fontSize: 13, color: P.ink60, background: 'transparent', border: 'none', cursor: 'pointer', padding: '8px 4px' }}>Sign in</button>
              <PBtn primary size="sm" onClick={signUp}>Start free →</PBtn>
            </>)}
          </div>
        )}
        {useMobileNav && <button aria-label="Menu" aria-expanded={open} onClick={() => setOpen(o => !o)} style={{ background: P.card, border: `1px solid ${P.ink15}`, borderRadius: 8, cursor: 'pointer', width: 38, height: 38, color: P.ink, fontSize: 20, lineHeight: 1 }}>{open ? '✕' : '☰'}</button>}
      </div>
      {useMobileNav && open && (
        <div style={{ borderTop: `1px solid ${P.ink15}`, background: P.paper, padding: '12px 16px', display: 'flex', flexDirection: 'column', gap: 2, maxHeight: 'calc(100dvh - 62px)', overflowY: 'auto' }}>
          <a href="index.html" onClick={closeMenus} style={mobileLinkStyle(active === 'Home')}>Home</a>
          <a href="demo.html" onClick={closeMenus} style={mobileLinkStyle(active === 'Demo')}>Demo</a>
          <div style={{ padding: '8px 8px 4px', fontFamily: P.mono, fontSize: 10.5, color: P.ink40 }}>PRODUCTS</div>
          <a href="ring-builder.html" onClick={closeMenus} style={mobileLinkStyle(active === 'Ring Builder')}>Ring Builder</a>
          <a href="demo.html" onClick={closeMenus} style={{ ...mobileLinkStyle(false), paddingLeft: 20, fontSize: 13.5 }}>Ring Builder demo →</a>
          <a href="jewel-shots-product.html" onClick={closeMenus} style={mobileLinkStyle(active === 'Jewel Shots')}>Jewel Shots</a>
          <a href="demo.html?product=jewel-shots" onClick={closeMenus} style={{ ...mobileLinkStyle(false), paddingLeft: 20, fontSize: 13.5 }}>Jewel Shots demo →</a>
          {links.map(([n, href]) => (
            <a key={n} href={href} onClick={closeMenus} style={mobileLinkStyle(n === active)}>{n}</a>
          ))}
          <a href="ring-builder-app.html" onClick={closeMenus} style={{ ...mobileLinkStyle(active === 'Ring Builder App'), fontWeight: 600, color: P.ink }}>My Ring Builder</a>
          <a href="jewel-shots.html" onClick={closeMenus} style={{ ...mobileLinkStyle(active === 'My Models'), fontWeight: 600, color: P.ink }}>My Models</a>
          <div style={{ borderTop: `1px solid ${P.ink08}`, paddingTop: 12, marginTop: 8, display: 'flex', flexDirection: 'column', gap: 8 }}>
            {user ? (<>
              <a href="account.html" style={{ fontFamily: P.sans, fontSize: 14, color: P.ink60, padding: '8px 8px', textDecoration: 'none' }}>{user.firstName || user.emailAddresses?.[0]?.emailAddress?.split('@')[0]}</a>
              <PBtn primary style={{ justifyContent: 'center' }} onClick={signOut}>Sign out</PBtn>
            </>) : (<>
              <button onClick={signIn} style={{ fontFamily: P.sans, fontSize: 14, color: P.ink60, background: 'transparent', border: 'none', textAlign: 'left', padding: '8px 8px', cursor: 'pointer' }}>Sign in</button>
              <PBtn primary style={{ justifyContent: 'center' }} onClick={signUp}>Start free →</PBtn>
            </>)}
          </div>
        </div>
      )}
    </div>
  );
}

function PFooter() {
  const cols = [
    ['Product',   ['Ring Builder','Jewel Shots','My Models','Pricing']],
    ['Solutions', ['Engagement rings','Shopify jewelers','Product renders','Diamond feeds']],
    ['Resources', ['Docs','FAQ','Blog','Support']],
    ['Company',   ['About','Contact','Privacy']],
  ];
  const { isMobile } = useBreakpoint();
  const linkMap = {
    'Ring Builder': 'ring-builder.html',
    'Jewel Shots': 'jewel-shots-product.html',
    'My Models': 'jewel-shots.html',
    Pricing: 'pricing.html',
    'Engagement rings': 'solutions.html#engagement-rings',
    'Shopify jewelers': 'solutions.html#shopify-jewelers',
    'Product renders': 'solutions.html#product-renders',
    'Diamond feeds': 'solutions.html#lab-grown-diamonds',
    Docs: 'docs.html',
    FAQ: 'faq.html',
    Blog: 'blog.html',
    Support: 'support.html',
    About: 'about.html',
    Contact: 'contact.html',
    Privacy: 'privacy.html',
  };
  return (
    <footer style={{ background: P.ink, color: P.paper, padding: isMobile ? '40px 16px 24px' : '64px 40px 32px' }}>
      <div style={{ maxWidth: 1320, margin: '0 auto' }}>
        <div style={{ display: 'grid', gridTemplateColumns: isMobile ? '1fr 1fr' : '1.5fr repeat(4, 1fr)', gap: isMobile ? 24 : 40, marginBottom: isMobile ? 32 : 48 }}>
          <div style={{ gridColumn: isMobile ? '1 / -1' : 'auto' }}>
            <div style={{ display: 'flex', alignItems: 'center', gap: 10, marginBottom: 14 }}>
              <PMark size={22} dark />
              <span style={{ fontFamily: P.sans, fontWeight: 600, fontSize: 17 }}>Ring Facet</span>
            </div>
            <p style={{ fontFamily: P.sans, fontSize: 13, color: 'rgba(245,245,244,0.6)', lineHeight: 1.6, maxWidth: 280, margin: 0 }}>Configurator infrastructure for jewelry commerce. Shopify-native, headless-ready, and built for teams selling configurable jewelry online.</p>
          </div>
          {cols.map(([title, items]) => (
            <div key={title}>
              <div style={{ fontFamily: P.mono, fontSize: 10.5, color: 'rgba(245,245,244,0.45)', marginBottom: 14, letterSpacing: 0.5 }}>{title.toLowerCase()}</div>
              <ul style={{ listStyle: 'none', padding: 0, margin: 0, display: 'flex', flexDirection: 'column', gap: 9 }}>
                {items.map(item => (
                  <li key={item} style={{ fontFamily: P.sans, fontSize: 13, color: 'rgba(245,245,244,0.78)' }}>
                    <a href={linkMap[item]} style={{ color: 'rgba(245,245,244,0.78)', textDecoration: 'none' }}>{item}</a>
                  </li>
                ))}
              </ul>
            </div>
          ))}
        </div>
        <div style={{ display: 'flex', alignItems: 'center', flexDirection: isMobile ? 'column' : 'row', justifyContent: 'space-between', gap: isMobile ? 8 : 0, paddingTop: 24, borderTop: '1px solid rgba(245,245,244,0.12)' }}>
          <PMono style={{ color: 'rgba(245,245,244,0.45)' }}>© 2026 Ringfacet, Inc. · All builds reserved.</PMono>
          <div style={{ display: 'flex', gap: 6, alignItems: 'center' }}>
            <div style={{ width: 6, height: 6, borderRadius: 3, background: '#22C55E' }} />
            <PMono style={{ color: 'rgba(245,245,244,0.6)' }}>All systems operational</PMono>
          </div>
        </div>
      </div>
    </footer>
  );
}
