/* ============================================================
   Shared UI primitives — icons + small components
   Exported to window for use across builder & sheet.
   ============================================================ */
const { useState, useRef, useEffect } = React;

/* ---- minimalist line icons ---- */
const Ic = {
  check:  (p)=><svg viewBox="0 0 24 24" width="1em" height="1em" fill="none" stroke="currentColor" strokeWidth="2.4" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M20 6 9 17l-5-5"/></svg>,
  chevR:  (p)=><svg viewBox="0 0 24 24" width="1em" height="1em" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="m9 18 6-6-6-6"/></svg>,
  chevL:  (p)=><svg viewBox="0 0 24 24" width="1em" height="1em" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="m15 18-6-6 6-6"/></svg>,
  dice:   (p)=><svg viewBox="0 0 24 24" width="1em" height="1em" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}><rect x="3" y="3" width="18" height="18" rx="3"/><circle cx="8" cy="8" r="1.3" fill="currentColor" stroke="none"/><circle cx="16" cy="16" r="1.3" fill="currentColor" stroke="none"/><circle cx="12" cy="12" r="1.3" fill="currentColor" stroke="none"/></svg>,
  info:   (p)=><svg viewBox="0 0 24 24" width="1em" height="1em" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}><circle cx="12" cy="12" r="9"/><path d="M12 16v-4M12 8h.01"/></svg>,
  sparkle:(p)=><svg viewBox="0 0 24 24" width="1em" height="1em" fill="currentColor" {...p}><path d="M12 2l1.8 5.6L19.5 9l-5.7 1.4L12 16l-1.8-5.6L4.5 9l5.7-1.4z"/></svg>,
  reset:  (p)=><svg viewBox="0 0 24 24" width="1em" height="1em" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M3 12a9 9 0 1 0 3-6.7L3 8"/><path d="M3 3v5h5"/></svg>,
  sheet:  (p)=><svg viewBox="0 0 24 24" width="1em" height="1em" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M14 3H7a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V8z"/><path d="M14 3v5h5M9 13h6M9 17h4"/></svg>,
  edit:   (p)=><svg viewBox="0 0 24 24" width="1em" height="1em" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M12 20h9"/><path d="M16.5 3.5a2.1 2.1 0 0 1 3 3L7 19l-4 1 1-4z"/></svg>,
  print:  (p)=><svg viewBox="0 0 24 24" width="1em" height="1em" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M6 9V2h12v7M6 18H4a2 2 0 0 1-2-2v-3a2 2 0 0 1 2-2h16a2 2 0 0 1 2 2v3a2 2 0 0 1-2 2h-2M6 14h12v8H6z"/></svg>,
  shield: (p)=><svg viewBox="0 0 24 24" width="1em" height="1em" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"/></svg>,
  heart:  (p)=><svg viewBox="0 0 24 24" width="1em" height="1em" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M19 14c1.5-1.5 3-3.2 3-5.5A3.5 3.5 0 0 0 12 6 3.5 3.5 0 0 0 2 8.5c0 2.3 1.5 4 3 5.5l7 7z"/></svg>,
  book:     (p)=><svg viewBox="0 0 24 24" width="1em" height="1em" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M4 19.5A2.5 2.5 0 0 1 6.5 17H20"/><path d="M6.5 2H20v20H6.5A2.5 2.5 0 0 1 4 19.5v-15A2.5 2.5 0 0 1 6.5 2z"/></svg>,
  download: (p)=><svg viewBox="0 0 24 24" width="1em" height="1em" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="7 10 12 15 17 10"/><line x1="12" y1="15" x2="12" y2="3"/></svg>,
  upload:   (p)=><svg viewBox="0 0 24 24" width="1em" height="1em" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="17 8 12 3 7 8"/><line x1="12" y1="3" x2="12" y2="15"/></svg>,
};

const STEP_META = [
  { key:"class",      label:"Class" },
  { key:"species",    label:"Species" },
  { key:"background", label:"Background" },
  { key:"abilities",  label:"Ability Scores" },
  { key:"skills",     label:"Skills" },
  { key:"equipment",  label:"Equipment" },
  { key:"spells",     label:"Spells" },
  { key:"review",     label:"Review" },
];

function StepHeader({ eyebrow, title, sub }){
  return (
    <div>
      <div className="step-eyebrow">{eyebrow}</div>
      <h1 className="step-title">{title}</h1>
      {sub && <p className="step-sub">{sub}</p>}
    </div>
  );
}

function SectionCap({ children }){
  return <div className="section-cap"><span>{children}</span><span className="ln"></span></div>;
}

function OptionCard({ emblem, name, meta, blurb, tags, selected, onClick, noCheck }){
  return (
    <button className={"opt" + (selected ? " sel" : "")} onClick={onClick}>
      {!noCheck && <span className="o-check"><Ic.check/></span>}
      <div className="o-top">
        {emblem && <span className="o-emb">{emblem}</span>}
        <div>
          <div className="o-name">{name}</div>
          {meta && <div className="o-meta">{meta}</div>}
        </div>
      </div>
      {blurb && <div className="o-blurb">{blurb}</div>}
      {tags && tags.length>0 && (
        <div className="o-tags">{tags.map((t,i)=>
          <span key={i} className={"tag"+(t.acc?" acc":"")}>{t.label||t}</span>)}
        </div>
      )}
    </button>
  );
}

function Chip({ label, on, disabled, dot, onClick }){
  return (
    <button className={"chip"+(on?" on":"")+(disabled?" disabled":"")}
      onClick={disabled?undefined:onClick} disabled={disabled}>
      {dot && <span className="dot"></span>}{label}
    </button>
  );
}

function Callout({ children, amber }){
  return <div className={"callout"+(amber?" amber":"")}><span className="ico"><Ic.info/></span><div>{children}</div></div>;
}

function Budget({ label, value, total, over }){
  const pct = Math.max(0, Math.min(100, (value/total)*100));
  return (
    <div className={"budget"+(over?" over":"")}>
      <span>{label}</span>
      <b className="mono">{value}</b>
      <span className="bar"><i style={{width:pct+"%"}}></i></span>
    </div>
  );
}

Object.assign(window, { Ic, STEP_META, StepHeader, SectionCap, OptionCard, Chip, Callout, Budget,
  useState, useRef, useEffect });
