// Shared chrome: logo, nav, footer, theme toggle.
// Loaded on every page.

const CobaltLogo = ({ size = 22 }) => {
  // 18 radial bars, every 3rd longer+thicker. Monochrome currentColor.
  const bars = [];
  const r1 = 5.5;          // inner radius
  const r2Short = 9;       // short bar end
  const r2Long = 10.5;     // long bar end
  for (let i = 0; i < 18; i++) {
    const angle = (i * 20 - 90) * (Math.PI / 180);
    const long = i % 3 === 0;
    const r2 = long ? r2Long : r2Short;
    const w = long ? 1.6 : 1;
    const x1 = 12 + Math.cos(angle) * r1;
    const y1 = 12 + Math.sin(angle) * r1;
    const x2 = 12 + Math.cos(angle) * r2;
    const y2 = 12 + Math.sin(angle) * r2;
    bars.push(
      <line key={i} x1={x1} y1={y1} x2={x2} y2={y2} stroke="currentColor" strokeWidth={w} strokeLinecap="round" />
    );
  }
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" aria-hidden="true">
      <circle cx="12" cy="12" r="4" stroke="currentColor" strokeWidth="1.4" fill="none" />
      {bars}
    </svg>
  );
};

const CobaltWordmark = ({ size = 22 }) => (
  <div className="nav-logo">
    <CobaltLogo size={size} />
    <span>Cobalt</span>
  </div>
);

const Icon = {
  chev: () => <svg width="10" height="10" viewBox="0 0 10 10" fill="none"><path d="M2 3.5L5 6.5L8 3.5" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round"/></svg>,
  arrow: () => <svg width="14" height="14" viewBox="0 0 14 14" fill="none"><path d="M3 7h8M7.5 3.5L11 7l-3.5 3.5" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round"/></svg>,
  bank: () => <svg width="16" height="16" viewBox="0 0 16 16" fill="none"><path d="M8 2L2 5v1h12V5L8 2zM3 7v5M13 7v5M6 7v5M10 7v5M2 13h12" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round"/></svg>,
  card: () => <svg width="16" height="16" viewBox="0 0 16 16" fill="none"><rect x="2" y="4" width="12" height="9" rx="1.5" stroke="currentColor" strokeWidth="1.3"/><path d="M2 7h12" stroke="currentColor" strokeWidth="1.3"/></svg>,
  coin: () => <svg width="16" height="16" viewBox="0 0 16 16" fill="none"><circle cx="8" cy="8" r="5.5" stroke="currentColor" strokeWidth="1.3"/><path d="M8 5v6M6 7h3.5a1 1 0 010 2H6" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round"/></svg>,
  send: () => <svg width="16" height="16" viewBox="0 0 16 16" fill="none"><path d="M13.5 2.5L7 9M13.5 2.5l-4 11-2.5-4.5-4.5-2.5 11-4z" stroke="currentColor" strokeWidth="1.3" strokeLinejoin="round"/></svg>,
  doc: () => <svg width="16" height="16" viewBox="0 0 16 16" fill="none"><path d="M3 2h7l3 3v9H3V2z" stroke="currentColor" strokeWidth="1.3" strokeLinejoin="round"/><path d="M10 2v3h3M6 8h4M6 11h4" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round"/></svg>,
  book: () => <svg width="16" height="16" viewBox="0 0 16 16" fill="none"><path d="M3 3h4a2 2 0 012 2v8a1 1 0 00-1-1H3V3zM13 3H9a2 2 0 00-2 2v8a1 1 0 011-1h5V3z" stroke="currentColor" strokeWidth="1.3" strokeLinejoin="round"/></svg>,
  vault: () => <svg width="16" height="16" viewBox="0 0 16 16" fill="none"><rect x="2" y="3" width="12" height="10" rx="1" stroke="currentColor" strokeWidth="1.3"/><circle cx="8" cy="8" r="2.5" stroke="currentColor" strokeWidth="1.3"/><path d="M8 5.5V4M8 12v-1.5M5.5 8H4M12 8h-1.5" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round"/></svg>,
  shield: () => <svg width="16" height="16" viewBox="0 0 16 16" fill="none"><path d="M8 2l5 2v5c0 3-2.5 5-5 5s-5-2-5-5V4l5-2z" stroke="currentColor" strokeWidth="1.3" strokeLinejoin="round"/><path d="M6 8l1.5 1.5L10.5 6.5" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round" strokeLinejoin="round"/></svg>,
  code: () => <svg width="16" height="16" viewBox="0 0 16 16" fill="none"><path d="M5 4L2 8l3 4M11 4l3 4-3 4M9.5 3l-3 10" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round" strokeLinejoin="round"/></svg>,
  people: () => <svg width="16" height="16" viewBox="0 0 16 16" fill="none"><circle cx="6" cy="6" r="2.5" stroke="currentColor" strokeWidth="1.3"/><circle cx="11.5" cy="7" r="2" stroke="currentColor" strokeWidth="1.3"/><path d="M2 13c0-2 1.8-3.5 4-3.5s4 1.5 4 3.5M10.5 13c0-1.6 1.3-2.8 3-2.8" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round"/></svg>,
  sun: () => <svg width="16" height="16" viewBox="0 0 16 16" fill="none"><circle cx="8" cy="8" r="3" stroke="currentColor" strokeWidth="1.4"/><path d="M8 1.5v1.5M8 13v1.5M14.5 8H13M3 8H1.5M12.7 3.3l-1 1M4.3 11.7l-1 1M12.7 12.7l-1-1M4.3 4.3l-1-1" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round"/></svg>,
  moon: () => <svg width="16" height="16" viewBox="0 0 16 16" fill="none"><path d="M13 9.5A5.5 5.5 0 016.5 3a.5.5 0 00-.7-.5A6.5 6.5 0 1013.5 10.2a.5.5 0 00-.5-.7z" stroke="currentColor" strokeWidth="1.4" strokeLinejoin="round"/></svg>,
  check: () => <svg width="14" height="14" viewBox="0 0 14 14" fill="none"><path d="M3 7l2.5 2.5L11 4" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"/></svg>,
  plus: () => <svg width="12" height="12" viewBox="0 0 12 12" fill="none"><path d="M6 2v8M2 6h8" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round"/></svg>,
  bolt: () => <svg width="16" height="16" viewBox="0 0 16 16" fill="none"><path d="M9 1L2 9h4.5L7 15l7-8H9.5L9 1z" stroke="currentColor" strokeWidth="1.3" strokeLinejoin="round"/></svg>,
  globe: () => <svg width="16" height="16" viewBox="0 0 16 16" fill="none"><circle cx="8" cy="8" r="6" stroke="currentColor" strokeWidth="1.3"/><path d="M2 8h12M8 2c2 2.5 2 9.5 0 12M8 2c-2 2.5-2 9.5 0 12" stroke="currentColor" strokeWidth="1.3"/></svg>,
  sparkles: () => <svg width="16" height="16" viewBox="0 0 16 16" fill="none"><path d="M6.5 1.5L7.7 5.3 11.5 6.5 7.7 7.7 6.5 11.5 5.3 7.7 1.5 6.5 5.3 5.3z" stroke="currentColor" strokeWidth="1.2" strokeLinejoin="round"/><path d="M12 9.5L12.6 11.4 14.5 12 12.6 12.6 12 14.5 11.4 12.6 9.5 12 11.4 11.4z" stroke="currentColor" strokeWidth="1" strokeLinejoin="round"/></svg>,
};

const products = [
  { href: "products/banking.html", icon: "bank", label: "Banking", sub: "AUD accounts & sub-accounts" },
  { href: "products/payments.html", icon: "send", label: "Payments", sub: "NPP, Osko, PayID" },
  { href: "products/cards.html", icon: "card", label: "Cards", sub: "Virtual cards with controls" },
  { href: "products/getpaid.html", icon: "sparkles", label: "Get Paid", sub: "AI collections, payment pages, reconciliation" },
  { href: "products/crypto.html", icon: "coin", label: "Stablecoins", sub: "Cross-border in seconds" },
  { href: "products/invoicing.html", icon: "doc", label: "Invoicing", sub: "GST-compliant, PayID, BPAY" },
  { href: "products/accounting.html", icon: "book", label: "Accounting", sub: "Xero, MYOB, QBO, NetSuite" },
  { href: "products/treasury.html", icon: "vault", label: "Treasury", sub: "Yield on idle capital" },
  { href: "products/rewards.html", icon: "bolt", label: "Rewards", sub: "Points on every dollar moved" },
];

const company = [
  { href: "about.html", icon: "people", label: "About", sub: "Why Cobalt exists" },
  { href: "contact.html", icon: "send", label: "Contact", sub: "Get in touch" },
];

const Nav = ({ active = "", path = "" }) => {
  // path = "" for root pages, "../" for /products pages
  const p = path;
  const [open, setOpen] = React.useState(false);

  React.useEffect(() => {
    document.body.style.overflow = open ? 'hidden' : '';
    return () => { document.body.style.overflow = ''; };
  }, [open]);

  return (
    <nav className="nav">
      <div className="nav-inner">
        <a href={`${p}index.html`} style={{display:'flex', alignItems:'center'}}>
          <CobaltWordmark />
        </a>
        <div className="nav-links hide-md">
          <div className="nav-link-wrap">
            <a className={`nav-link ${active==='products' ? 'active':''}`} href="#">
              Products <span className="chev"><Icon.chev/></span>
            </a>
            <div className="nav-dropdown" style={{minWidth: 330}}>
              {products.map(pr => (
                <a key={pr.label} className="nav-dd-item" href={`${p}${pr.href}`}>
                  <span className="ico">{Icon[pr.icon]()}</span>
                  <span>
                    <div className="lbl">{pr.label}</div>
                    <div className="sub">{pr.sub}</div>
                  </span>
                </a>
              ))}
            </div>
          </div>
          <a className={`nav-link ${active==='pricing' ? 'active':''}`} href={`${p}pricing.html`}>Pricing</a>
          <div className="nav-link-wrap">
            <a className={`nav-link ${active==='company' ? 'active':''}`} href="#">
              Company <span className="chev"><Icon.chev/></span>
            </a>
            <div className="nav-dropdown">
              {company.map(c => (
                <a key={c.label} className="nav-dd-item" href={`${p}${c.href}`}>
                  <span className="ico">{Icon[c.icon]()}</span>
                  <span>
                    <div className="lbl">{c.label}</div>
                    <div className="sub">{c.sub}</div>
                  </span>
                </a>
              ))}
            </div>
          </div>
        </div>
        <div className="nav-cta">
          <a className="btn btn-ghost btn-sm hide-sm" href="#">Sign in</a>
          <a className="btn btn-primary btn-sm" href="https://cobaltfi.app">Open account</a>
          <button
            type="button"
            className="nav-burger show-md"
            aria-label={open ? 'Close menu' : 'Open menu'}
            aria-expanded={open}
            onClick={() => setOpen(o => !o)}
          >
            <span className={`burger-icon ${open ? 'open' : ''}`}>
              <span/><span/><span/>
            </span>
          </button>
        </div>
      </div>
      <div className={`mobile-menu ${open ? 'open' : ''}`} onClick={() => setOpen(false)}>
        <div className="mobile-menu-inner" onClick={e => e.stopPropagation()}>
          <div className="mm-section">
            <div className="mm-head">Products</div>
            {products.map(pr => (
              <a key={pr.label} className="mm-item" href={`${p}${pr.href}`}>
                <span className="ico">{Icon[pr.icon]()}</span>
                <span className="mm-item-text">
                  <span className="mm-lbl">{pr.label}</span>
                  <span className="mm-sub">{pr.sub}</span>
                </span>
              </a>
            ))}
          </div>
          <div className="mm-section">
            <a className="mm-link" href={`${p}pricing.html`}>Pricing</a>
            {company.map(c => (
              <a key={c.label} className="mm-link" href={`${p}${c.href}`}>{c.label}</a>
            ))}
          </div>
          <div className="mm-section mm-cta">
            <a className="btn btn-ghost btn-lg" href="#">Sign in</a>
            <a className="btn btn-primary btn-lg" href="https://cobaltfi.app">Open account <Icon.arrow/></a>
          </div>
        </div>
      </div>
    </nav>
  );
};

const Footer = ({ path = "" }) => {
  const p = path;
  return (
    <footer className="footer">
      <div className="wrap">
        <div className="footer-grid">
          <div>
            <h2 className="footer-big">Issue your own<br/>invoices with <span style={{color:'var(--cobalt)'}}>Cobalt</span>.</h2>
            <a className="btn btn-primary" href="https://cobaltfi.app">
              Open an account <Icon.arrow/>
            </a>
          </div>
          <div>
            <h5>Products</h5>
            <ul>
              {products.map(pr => <li key={pr.label}><a href={`${p}${pr.href}`}>{pr.label}</a></li>)}
            </ul>
          </div>
          <div>
            <h5>Company</h5>
            <ul>
              <li><a href={`${p}about.html`}>About</a></li>
              <li><a href={`${p}contact.html`}>Contact</a></li>
              <li><a href="#">Careers</a></li>
            </ul>
          </div>
          <div>
            <h5>Legal</h5>
            <ul>
              <li><a href="#">Terms of service</a></li>
              <li><a href="#">Privacy policy</a></li>
            </ul>
          </div>
          <div>
            <h5>Social</h5>
            <ul>
              <li><a href="#">X / Twitter</a></li>
              <li><a href="#">LinkedIn</a></li>
              <li><a href="#">GitHub</a></li>
              <li><a href="#">Changelog</a></li>
            </ul>
          </div>
        </div>
        <div className="footer-bottom">
          <div style={{display:'flex', alignItems:'center', gap: 10}}>
            <CobaltLogo size={18}/>
            <span>© 2026 Cobalt · Concept site · Not a regulated financial product</span>
          </div>
          <div className="mono">Designed in Sydney, Australia</div>
        </div>
      </div>
    </footer>
  );
};

Object.assign(window, { CobaltLogo, CobaltWordmark, Nav, Footer, Icon, cobaltProducts: products });
