// Shared UI components, icons, helpers

// ──────────────────────────────────────── Icons (inline SVG, stroke)
const Icon = {
  Chat: () => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="M21 11.5a8.38 8.38 0 0 1-.9 3.8 8.5 8.5 0 0 1-7.6 4.7 8.38 8.38 0 0 1-3.8-.9L3 21l1.9-5.7a8.38 8.38 0 0 1-.9-3.8 8.5 8.5 0 0 1 4.7-7.6 8.38 8.38 0 0 1 3.8-.9h.5a8.48 8.48 0 0 1 8 8v.5z"/></svg>,
  Edit: () => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7"/><path d="M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5z"/></svg>,
  Tag: () => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="M20.59 13.41l-7.17 7.17a2 2 0 0 1-2.83 0L2 12V2h10l8.59 8.59a2 2 0 0 1 0 2.82z"/><line x1="7" y1="7" x2="7.01" y2="7"/></svg>,
  Globe: () => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><circle cx="12" cy="12" r="10"/><line x1="2" y1="12" x2="22" y2="12"/><path d="M12 2a15.3 15.3 0 0 1 4 10 15.3 15.3 0 0 1-4 10 15.3 15.3 0 0 1-4-10 15.3 15.3 0 0 1 4-10z"/></svg>,
  Box: () => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="M21 16V8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.73l7 4a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16z"/><polyline points="3.27 6.96 12 12.01 20.73 6.96"/><line x1="12" y1="22.08" x2="12" y2="12"/></svg>,
  User: () => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"/><circle cx="12" cy="7" r="4"/></svg>,
  Pin: () => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="M21 10c0 7-9 13-9 13s-9-6-9-13a9 9 0 0 1 18 0z"/><circle cx="12" cy="10" r="3"/></svg>,
  Doc: () => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/><polyline points="14 2 14 8 20 8"/></svg>,
  Chart: () => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><line x1="18" y1="20" x2="18" y2="10"/><line x1="12" y1="20" x2="12" y2="4"/><line x1="6" y1="20" x2="6" y2="14"/></svg>,
  Mic: () => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="M12 1a3 3 0 0 0-3 3v8a3 3 0 0 0 6 0V4a3 3 0 0 0-3-3z"/><path d="M19 10v2a7 7 0 0 1-14 0v-2"/><line x1="12" y1="19" x2="12" y2="23"/><line x1="8" y1="23" x2="16" y2="23"/></svg>,
  Send: () => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><line x1="22" y1="2" x2="11" y2="13"/><polygon points="22 2 15 22 11 13 2 9 22 2"/></svg>,
  Spark: () => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><polygon points="13 2 3 14 12 14 11 22 21 10 12 10 13 2"/></svg>,
  Search: () => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><circle cx="11" cy="11" r="8"/><line x1="21" y1="21" x2="16.65" y2="16.65"/></svg>,
  Plus: () => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></svg>,
  X: () => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/></svg>,
  Check: () => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><polyline points="20 6 9 17 4 12"/></svg>,
  Warn: () => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="M10.29 3.86L1.82 18a2 2 0 0 0 1.71 3h16.94a2 2 0 0 0 1.71-3L13.71 3.86a2 2 0 0 0-3.42 0z"/><line x1="12" y1="9" x2="12" y2="13"/><line x1="12" y1="17" x2="12.01" y2="17"/></svg>,
  Arrow: () => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><line x1="5" y1="12" x2="19" y2="12"/><polyline points="12 5 19 12 12 19"/></svg>,
  Bell: () => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="M18 8A6 6 0 0 0 6 8c0 7-3 9-3 9h18s-3-2-3-9"/><path d="M13.73 21a2 2 0 0 1-3.46 0"/></svg>,
  Grid: () => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><rect x="3" y="3" width="7" height="7"/><rect x="14" y="3" width="7" height="7"/><rect x="14" y="14" width="7" height="7"/><rect x="3" y="14" width="7" height="7"/></svg>,
  Camera: () => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="M23 19a2 2 0 0 1-2 2H3a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h4l2-3h6l2 3h4a2 2 0 0 1 2 2z"/><circle cx="12" cy="13" r="4"/></svg>,
  Download: () => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><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>,
  Share: () => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><circle cx="18" cy="5" r="3"/><circle cx="6" cy="12" r="3"/><circle cx="18" cy="19" r="3"/><line x1="8.59" y1="13.51" x2="15.42" y2="17.49"/><line x1="15.41" y1="6.51" x2="8.59" y2="10.49"/></svg>,
};

// ──────────────────────────────────────── Small reusable pieces
function KPI({ label, value, sub, subType }) {
  return (
    <div className="kpi">
      <div className="kpi-label">{label}</div>
      <div className="kpi-value">{value}</div>
      {sub && <div className={"kpi-sub " + (subType || "")}>{sub}</div>}
    </div>
  );
}

function Badge({ children, variant = "neutral" }) {
  return <span className={"badge " + variant}>{children}</span>;
}

function HBar({ value, max, color, fillClass }) {
  const pct = Math.min(100, (value / max) * 100);
  return (
    <div style={{ display: "flex", alignItems: "center", gap: 10 }}>
      <div style={{ flex: 1, height: 10, background: "var(--surface-2)", borderRadius: 4, overflow: "hidden", position: "relative" }}>
        <div style={{ width: pct + "%", height: "100%", background: color || "var(--accent)", borderRadius: 4, transition: "width 700ms ease" }} />
      </div>
    </div>
  );
}

function Progress({ pct, variant }) {
  return (
    <div className="progress">
      <div className={"progress-fill " + (variant || "")} style={{ width: Math.min(100, pct) + "%" }} />
    </div>
  );
}

// Product illustrations (SVG placeholders estilizados de packaging)
function ProductIllu({ kind = "cartridge", color = "#e6eef5", accent = "var(--accent)", label }) {
  const bg = color;
  if (kind === "aerosol") {
    return (
      <div className="prod-illu" style={{ background: `linear-gradient(160deg, ${bg}, #fff)` }}>
        <svg viewBox="0 0 120 160" xmlns="http://www.w3.org/2000/svg">
          <defs>
            <linearGradient id={"ag"+label} x1="0" x2="1"><stop offset="0" stopColor="#fff" stopOpacity=".3"/><stop offset=".5" stopColor="#fff" stopOpacity="0"/><stop offset="1" stopColor="#000" stopOpacity=".15"/></linearGradient>
          </defs>
          <rect x="42" y="8" width="36" height="14" rx="3" fill="#4a4a52"/>
          <rect x="38" y="22" width="44" height="10" rx="2" fill="#2a2a33"/>
          <rect x="28" y="32" width="64" height="118" rx="6" fill={accent}/>
          <rect x="28" y="32" width="64" height="118" rx="6" fill={`url(#ag${label})`}/>
          <rect x="34" y="60" width="52" height="34" rx="2" fill="#fff" opacity=".92"/>
          <rect x="38" y="66" width="44" height="4" rx="1" fill={accent} opacity=".7"/>
          <rect x="38" y="74" width="28" height="3" rx="1" fill="#999"/>
          <rect x="38" y="80" width="36" height="3" rx="1" fill="#999"/>
          <rect x="38" y="86" width="20" height="3" rx="1" fill="#999"/>
        </svg>
      </div>
    );
  }
  if (kind === "tub") {
    return (
      <div className="prod-illu" style={{ background: `linear-gradient(160deg, ${bg}, #fff)` }}>
        <svg viewBox="0 0 160 140" xmlns="http://www.w3.org/2000/svg">
          <ellipse cx="80" cy="24" rx="58" ry="10" fill="#2a2a33"/>
          <path d="M22 24 L28 128 Q28 134 34 134 L126 134 Q132 134 132 128 L138 24 Z" fill={accent}/>
          <rect x="36" y="50" width="88" height="56" rx="3" fill="#fff" opacity=".92"/>
          <rect x="42" y="58" width="76" height="6" rx="1" fill={accent}/>
          <rect x="42" y="70" width="48" height="4" rx="1" fill="#888"/>
          <rect x="42" y="78" width="60" height="4" rx="1" fill="#888"/>
          <rect x="42" y="92" width="40" height="8" rx="1" fill={accent} opacity=".7"/>
        </svg>
      </div>
    );
  }
  // default: cartridge
  return (
    <div className="prod-illu" style={{ background: `linear-gradient(160deg, ${bg}, #fff)` }}>
      <svg viewBox="0 0 200 110" xmlns="http://www.w3.org/2000/svg">
        <defs>
          <linearGradient id={"cg"+label} x1="0" y1="0" x2="0" y2="1"><stop offset="0" stopColor="#fff" stopOpacity=".35"/><stop offset=".5" stopColor="#fff" stopOpacity="0"/><stop offset="1" stopColor="#000" stopOpacity=".2"/></linearGradient>
        </defs>
        <rect x="10" y="28" width="160" height="54" rx="6" fill={accent}/>
        <rect x="10" y="28" width="160" height="54" rx="6" fill={`url(#cg${label})`}/>
        <rect x="20" y="38" width="120" height="34" rx="2" fill="#fff" opacity=".94"/>
        <rect x="26" y="44" width="80" height="5" rx="1" fill={accent}/>
        <rect x="26" y="53" width="60" height="3" rx="1" fill="#888"/>
        <rect x="26" y="59" width="80" height="3" rx="1" fill="#888"/>
        <rect x="26" y="65" width="40" height="3" rx="1" fill="#888"/>
        <rect x="170" y="38" width="18" height="34" rx="2" fill="#2a2a33"/>
        <path d="M188 44 L196 50 L196 60 L188 66 Z" fill="#2a2a33"/>
      </svg>
    </div>
  );
}

// Expose to window so other babel files can use them
Object.assign(window, { Icon, KPI, Badge, HBar, Progress, ProductIllu });
