// Numo Paper — Docs shared components
// Top nav, page shell, side nav, section/stage/spec helpers.
// Every doc page imports this.

const { useState: dsUseState, useMemo: dsUseMemo, useEffect: dsUseEffect } = React;

// --- Brand lockup (inline so pages are self-contained) ---
const DSLockup = ({ height = 20 }) => (
  <svg height={height} viewBox="0 0 269.626 94.423" style={{ display: 'block' }} xmlns="http://www.w3.org/2000/svg">
    <path d="M 38.746 83.276 L 0 83.276 L 0 8.389 L 38.746 37.529 Z" fill="#3D3C5F" />
    <path d="M 42.718 0 L 81.464 0 L 81.464 94.423 L 42.718 65.282 Z" fill="#3D3C5F" />
    <path d="M 103.448 67.798 L 103.448 36.431 L 109.254 36.431 C 109.84 36.431 110.329 36.563 110.722 36.825 C 111.115 37.087 111.392 37.481 111.553 38.005 L 112.099 39.819 C 112.703 39.256 113.327 38.731 113.974 38.248 C 114.619 37.762 115.309 37.354 116.045 37.022 C 116.781 36.689 117.578 36.427 118.435 36.235 C 119.293 36.043 120.224 35.948 121.233 35.948 C 122.947 35.948 124.464 36.247 125.785 36.84 C 127.106 37.436 128.22 38.261 129.128 39.32 C 130.035 40.379 130.72 41.64 131.184 43.102 C 131.648 44.564 131.88 46.152 131.88 47.865 L 131.88 67.798 L 122.503 67.798 L 122.503 47.865 C 122.503 46.333 122.15 45.138 121.445 44.281 C 120.739 43.424 119.7 42.995 118.329 42.995 C 117.301 42.995 116.333 43.218 115.426 43.661 C 114.517 44.105 113.651 44.7 112.824 45.445 L 112.824 67.798 Z" fill="#3D3C5F" />
    <path d="M 147.306 36.432 L 147.306 56.334 C 147.306 57.888 147.659 59.087 148.365 59.935 C 149.071 60.782 150.109 61.205 151.48 61.205 C 152.509 61.205 153.472 60.993 154.369 60.57 C 155.266 60.146 156.139 59.552 156.985 58.786 L 156.985 36.432 L 166.363 36.432 L 166.363 67.799 L 160.555 67.799 C 159.385 67.799 158.618 67.274 158.256 66.226 L 157.682 64.411 C 157.076 64.996 156.451 65.526 155.806 65.999 C 155.16 66.473 154.47 66.875 153.734 67.208 C 152.998 67.542 152.206 67.804 151.36 67.995 C 150.513 68.187 149.585 68.284 148.577 68.284 C 146.862 68.284 145.345 67.985 144.025 67.39 C 142.703 66.795 141.589 65.964 140.683 64.896 C 139.774 63.827 139.089 62.565 138.625 61.115 C 138.161 59.662 137.93 58.07 137.93 56.334 L 137.93 36.432 Z" fill="#3D3C5F" />
    <path d="M 173.62 67.798 L 173.62 36.431 L 179.429 36.431 C 180.012 36.431 180.502 36.563 180.895 36.825 C 181.288 37.087 181.566 37.481 181.727 38.005 L 182.24 39.698 C 182.765 39.154 183.311 38.654 183.874 38.201 C 184.438 37.748 185.049 37.354 185.705 37.022 C 186.36 36.689 187.066 36.427 187.821 36.235 C 188.579 36.043 189.411 35.948 190.317 35.948 C 192.213 35.948 193.782 36.438 195.02 37.416 C 196.26 38.394 197.204 39.698 197.848 41.333 C 198.373 40.363 199.014 39.537 199.769 38.852 C 200.527 38.167 201.343 37.611 202.219 37.188 C 203.097 36.764 204.024 36.452 205.002 36.25 C 205.981 36.049 206.954 35.948 207.921 35.948 C 209.715 35.948 211.309 36.215 212.701 36.75 C 214.092 37.284 215.257 38.061 216.195 39.078 C 217.132 40.097 217.843 41.347 218.326 42.829 C 218.812 44.312 219.053 45.99 219.053 47.865 L 219.053 67.798 L 209.676 67.798 L 209.676 47.865 C 209.676 44.619 208.285 42.995 205.502 42.995 C 204.231 42.995 203.178 43.416 202.34 44.251 C 201.503 45.088 201.085 46.293 201.085 47.865 L 201.085 67.798 L 191.709 67.798 L 191.709 47.865 C 191.709 46.07 191.356 44.811 190.649 44.085 C 189.8 43.211 188.471 42.862 186.656 43.041 C 186.458 43.061 186.256 43.103 186.067 43.169 C 184.955 43.561 183.932 44.208 182.997 45.112 L 182.997 67.798 Z" fill="#3D3C5F" />
    <path d="M 239.681 61.325 C 241.839 61.325 243.427 60.565 244.445 59.042 C 245.464 57.52 245.973 55.206 245.973 52.1 C 245.973 48.995 245.464 46.686 244.445 45.174 C 243.427 43.661 241.839 42.905 239.681 42.905 C 237.463 42.905 235.839 43.661 234.811 45.174 C 233.783 46.686 233.269 48.995 233.269 52.1 C 233.269 55.206 233.783 57.52 234.811 59.042 C 235.839 60.565 237.463 61.325 239.681 61.325 Z M 239.681 35.948 C 242.061 35.948 244.228 36.321 246.185 37.067 C 248.141 37.813 249.819 38.882 251.221 40.273 C 252.622 41.664 253.711 43.354 254.487 45.34 C 255.264 47.326 255.652 49.559 255.652 52.04 C 255.652 54.541 255.264 56.794 254.487 58.8 C 253.711 60.807 252.622 62.51 251.221 63.912 C 249.819 65.314 248.141 66.392 246.185 67.149 C 244.228 67.905 242.061 68.283 239.681 68.283 C 237.281 68.283 235.098 67.905 233.133 67.149 C 231.167 66.392 229.472 65.314 228.051 63.912 C 226.63 62.51 225.53 60.807 224.755 58.8 C 223.978 56.794 223.59 54.541 223.59 52.04 C 223.59 49.559 223.978 47.326 224.755 45.34 C 225.53 43.354 226.63 41.664 228.051 40.273 C 229.472 38.882 231.167 37.813 233.133 37.067 C 235.098 36.321 237.281 35.948 239.681 35.948 Z" fill="#3D3C5F" />
    <path d="M 258.979 63.05 C 258.979 62.324 259.114 61.649 259.387 61.024 C 259.659 60.399 260.032 59.855 260.507 59.39 C 260.979 58.926 261.54 58.558 262.184 58.286 C 262.83 58.014 263.536 57.878 264.302 57.878 C 265.048 57.878 265.743 58.014 266.389 58.286 C 267.034 58.558 267.599 58.926 268.083 59.39 C 268.566 59.855 268.945 60.399 269.217 61.024 C 269.49 61.649 269.626 62.324 269.626 63.05 C 269.626 63.777 269.49 64.457 269.217 65.092 C 268.945 65.727 268.566 66.276 268.083 66.74 C 267.599 67.205 267.034 67.568 266.389 67.829 C 265.743 68.091 265.048 68.222 264.302 68.222 C 263.536 68.222 262.83 68.091 262.184 67.829 C 261.54 67.568 260.979 67.205 260.507 66.74 C 260.032 66.276 259.659 65.727 259.387 65.092 C 259.114 64.457 258.979 63.777 258.979 63.05 Z" fill="#F4D386" />
  </svg>
);

// ---- Top nav ----
const DSNav = ({ current }) => {
  const links = [
    { id: 'overview',     href: 'index.html',       label: 'Overview' },
    { id: 'foundations',  href: 'foundations.html', label: 'Foundations' },
    { id: 'components',   href: 'components.html',  label: 'Components' },
    { id: 'patterns',     href: 'patterns.html',    label: 'Patterns' },
    { id: 'guidelines',   href: 'guidelines.html',  label: 'Guidelines' },
  ];
  return (
    <nav className="ds-nav">
      <div className="ds-nav-inner">
        <a className="ds-nav-brand" href="index.html">
          <DSLockup height={18} />
          <span className="ds-nav-brand-tag">/&nbsp;paper</span>
        </a>
        <div className="ds-nav-links">
          {links.map(l => (
            <a key={l.id} href={l.href} className={`ds-nav-link ${current === l.id ? 'is-active' : ''}`}>
              {l.label}
            </a>
          ))}
        </div>
        <div className="ds-nav-meta">
          <span className="ds-nav-version">v1.0</span>
          <span>Design &amp; eng reference</span>
        </div>
      </div>
    </nav>
  );
};

// ---- Page shell with optional side-nav ----
const DSPage = ({ sections, active, onActive, children }) => (
  <div className="ds-page">
    <aside className="ds-sidenav">
      {sections.map((s, i) => (
        s.group
          ? <div key={`g-${i}`} className="ds-sidenav-group">{s.group}</div>
          : <a key={s.id} href={`#${s.id}`}
               className={`ds-sidenav-link ${active === s.id ? 'is-active' : ''}`}
               onClick={() => onActive?.(s.id)}>{s.label}</a>
      ))}
    </aside>
    <main className="ds-content">{children}</main>
  </div>
);

// ---- Hero ----
const DSHero = ({ eyebrow, title, lede }) => (
  <header className="ds-hero">
    {eyebrow && <div className="ds-hero-eyebrow">{eyebrow}</div>}
    <h1 className="ds-hero-title">{title}</h1>
    {lede && <p className="ds-hero-lede">{lede}</p>}
  </header>
);

// ---- Section ----
const DSSection = ({ id, title, lede, children }) => (
  <section className="ds-section" id={id}>
    <header className="ds-section-head">
      <h2 className="ds-section-title">{title}</h2>
      {lede && <p className="ds-section-lede">{lede}</p>}
    </header>
    {children}
  </section>
);

const DSSubsection = ({ title }) => <h3 className="ds-subsection-title">{title}</h3>;

const DSP = ({ children }) => <p className="ds-p">{children}</p>;

// ---- Stage: a card wrapping a live example ----
const DSStage = ({ label, sunk, children, style }) => (
  <div className={`ds-stage ${sunk ? 'ds-stage-sunk' : ''}`} style={style}>
    {label && <div className="ds-stage-label">{label}</div>}
    {children}
  </div>
);

// ---- Spec table ----
const DSSpec = ({ rows, columns = ['Token', 'Value', 'Notes'] }) => (
  <table className="ds-spec">
    <thead>
      <tr>{columns.map(c => <th key={c}>{c}</th>)}</tr>
    </thead>
    <tbody>
      {rows.map((r, i) => (
        <tr key={i}>
          {r.map((cell, j) => (
            <td key={j} className={j === 0 || (typeof cell === 'string' && /^[#\-\d.]|px|em|ms|var\(/.test(cell)) ? 'mono' : ''}>
              {cell}
            </td>
          ))}
        </tr>
      ))}
    </tbody>
  </table>
);

// ---- Swatch ----
const DSSwatch = ({ color, name, value }) => (
  <div className="ds-swatch">
    <span className="ds-swatch-chip" style={{ background: color }} />
    <div>
      <div className="ds-swatch-label">{name}</div>
      <div className="ds-swatch-value">{value || color}</div>
    </div>
  </div>
);

// ---- Callouts ----
const DSCallout = ({ kind = 'note', title, children }) => (
  <div className={`ds-callout ds-callout-${kind}`}>
    <div>
      {title && <div className="ds-callout-title">{title}</div>}
      <div>{children}</div>
    </div>
  </div>
);

// ---- State label ----
const DSStateLabel = ({ children }) => <div className="ds-state-label">{children}</div>;

// ---- Mount ----
const dsMount = (El) => {
  const root = ReactDOM.createRoot(document.getElementById('root'));
  root.render(<El />);
};

Object.assign(window, {
  DSLockup, DSNav, DSPage, DSHero, DSSection, DSSubsection,
  DSP, DSStage, DSSpec, DSSwatch, DSCallout, DSStateLabel, dsMount,
});
