// === Reusable atoms / icons ===

const Icon = ({ name, size = 14 }) => {
  const paths = {
    search: <><circle cx="11" cy="11" r="7"/><path d="m20 20-3.5-3.5"/></>,
    chevron: <path d="m6 9 6 6 6-6"/>,
    chevronR: <path d="m9 6 6 6-6 6"/>,
    arrow: <path d="M5 12h14m-6-6 6 6-6 6"/>,
    arrowUp: <path d="M12 19V5m-7 7 7-7 7 7"/>,
    arrowDown: <path d="M12 5v14m7-7-7 7-7-7"/>,
    grid: <><rect x="3" y="3" width="7" height="7"/><rect x="14" y="3" width="7" height="7"/><rect x="3" y="14" width="7" height="7"/><rect x="14" y="14" width="7" height="7"/></>,
    list: <><line x1="8" y1="6" x2="21" y2="6"/><line x1="8" y1="12" x2="21" y2="12"/><line x1="8" y1="18" x2="21" y2="18"/><circle cx="4" cy="6" r="1"/><circle cx="4" cy="12" r="1"/><circle cx="4" cy="18" r="1"/></>,
    bell: <path d="M6 8a6 6 0 0 1 12 0c0 7 3 9 3 9H3s3-2 3-9m6 13a3 3 0 0 0 3-3H9a3 3 0 0 0 3 3"/>,
    settings: <><circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 1 1-2.83 2.83l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 1 1-4 0v-.09a1.65 1.65 0 0 0-1-1.51 1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 1 1-2.83-2.83l.06-.06a1.65 1.65 0 0 0 .33-1.82 1.65 1.65 0 0 0-1.51-1H3a2 2 0 1 1 0-4h.09a1.65 1.65 0 0 0 1.51-1 1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 1 1 2.83-2.83l.06.06a1.65 1.65 0 0 0 1.82.33h0a1.65 1.65 0 0 0 1-1.51V3a2 2 0 1 1 4 0v.09a1.65 1.65 0 0 0 1 1.51h0a1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 1 1 2.83 2.83l-.06.06a1.65 1.65 0 0 0-.33 1.82v0a1.65 1.65 0 0 0 1.51 1H21a2 2 0 1 1 0 4h-.09a1.65 1.65 0 0 0-1.51 1z"/></>,
    download: <path d="M12 3v12m-5-5 5 5 5-5M5 21h14"/>,
    plus: <><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></>,
    sun: <><circle cx="12" cy="12" r="4"/><path d="M12 2v2m0 16v2m10-10h-2M4 12H2m15.5-7.5L16 6m-8 12-1.5 1.5m11 0L16 18M8 6 6.5 4.5"/></>,
    moon: <path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"/>,
    flame: <path d="M8.5 14.5A2.5 2.5 0 0 0 11 12c0-1.38-.5-2-1-3-1.072-2.143-.224-4.054 2-6 .5 2.5 2 4.9 4 6.5 2 1.6 3 3.5 3 5.5a7 7 0 1 1-14 0c0-1.153.433-2.294 1-3a2.5 2.5 0 0 0 2.5 2.5z"/>,
    target: <><circle cx="12" cy="12" r="9"/><circle cx="12" cy="12" r="5"/><circle cx="12" cy="12" r="1"/></>,
    clock: <><circle cx="12" cy="12" r="9"/><path d="M12 7v5l3 2"/></>,
    eye: <><path d="M2 12s3-7 10-7 10 7 10 7-3 7-10 7-10-7-10-7z"/><circle cx="12" cy="12" r="3"/></>,
    external: <><path d="M15 3h6v6"/><path d="M10 14 21 3"/><path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6"/></>,
    filter: <path d="M22 3H2l8 9.46V19l4 2v-8.54L22 3z"/>,
    sparkles: <path d="M12 3 14 10l7 2-7 2-2 7-2-7-7-2 7-2z"/>,
  };
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round">
      {paths[name]}
    </svg>
  );
};

const Sparkline = ({ data, color = 'var(--navy)', w = 80, h = 18, fill = false, dotLast = false }) => {
  if (!data || data.length < 2) return null;
  const min = Math.min(...data), max = Math.max(...data);
  const range = max - min || 1;
  const step = w / (data.length - 1);
  const pts = data.map((v, i) => [i * step, h - ((v - min) / range) * (h - 2) - 1]);
  const d = pts.map((p, i) => (i === 0 ? `M${p[0]},${p[1]}` : `L${p[0]},${p[1]}`)).join(' ');
  const area = `${d} L${w},${h} L0,${h} Z`;
  return (
    <svg width={w} height={h} className="spark-cell" style={{overflow: 'visible'}}>
      {fill && <path d={area} fill={color} opacity="0.12"/>}
      <path d={d} stroke={color} strokeWidth="1.4" fill="none" strokeLinecap="round" strokeLinejoin="round"/>
      {dotLast && <circle cx={pts[pts.length-1][0]} cy={pts[pts.length-1][1]} r="2" fill={color}/>}
    </svg>
  );
};

// generate a believable but stable trend
const trend = (seed, len = 14, base = 50, variance = 8) => {
  let r = seed * 9301 + 49297;
  return Array.from({length: len}, (_, i) => {
    r = (r * 9301 + 49297) % 233280;
    return base + Math.sin(i / 2 + seed) * variance + (r / 233280) * variance - variance/2;
  });
};

const HealthRing = ({ value, size = 36, stroke = 3 }) => {
  const r = (size - stroke) / 2;
  const c = 2 * Math.PI * r;
  const off = c - (value / 100) * c;
  const color = value >= 75 ? 'var(--green)' : value >= 55 ? 'var(--gold)' : 'var(--coral)';
  return (
    <div className="ring" style={{width: size, height: size}}>
      <svg width={size} height={size}>
        <circle cx={size/2} cy={size/2} r={r} fill="none" stroke="var(--line)" strokeWidth={stroke}/>
        <circle cx={size/2} cy={size/2} r={r} fill="none" stroke={color} strokeWidth={stroke} strokeDasharray={c} strokeDashoffset={off} strokeLinecap="round"/>
      </svg>
      <div className="lbl">{value}</div>
    </div>
  );
};

const LLMDot = ({ k }) => {
  const map = { claude: 'C', gpt: 'G', gemini: 'g', perplex: 'P' };
  return <span className={`llm-dot ${k}`}>{map[k]}</span>;
};

const Delta = ({ d }) => {
  const f = window.fmtDelta(d);
  return <span className={`delta ${f.cls} mono`}>{f.txt}</span>;
};

const Pill = ({ children, tone = '', solid = false, onClick, active }) => (
  <span className={`pill ${tone} ${solid ? 'solid' : ''} ${onClick ? 'tab' : ''} ${active ? 'active' : ''}`} onClick={onClick}>
    {children}
  </span>
);

const Fav = ({ init, size = '' }) => (
  <span className={`fav ${size}`}>{init}</span>
);

const Btn = ({ children, primary, ghost, sm, onClick, ...rest }) => (
  <button className={`btn ${primary ? 'primary' : ''} ${ghost ? 'ghost' : ''} ${sm ? 'sm' : ''}`} onClick={onClick} {...rest}>{children}</button>
);

Object.assign(window, { Icon, Sparkline, trend, HealthRing, LLMDot, Delta, Pill, Fav, Btn });
