/* ============================================================
   ICONOS (Lucide, stroke 1.75) + ÁTOMOS UI compartidos
   ============================================================ */
function Ico({ d, size = 16, sw = 1.75, fill = "none" }) {
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill={fill} stroke="currentColor"
      strokeWidth={sw} strokeLinecap="round" strokeLinejoin="round" style={{ display: "block" }}>
      {d}
    </svg>
  );
}

const ICONS = {
  refresh:  <><path d="M3 12a9 9 0 0 1 15-6.7L21 8" /><path d="M21 3v5h-5" /><path d="M21 12a9 9 0 0 1-15 6.7L3 16" /><path d="M3 21v-5h5" /></>,
  chevron:  <path d="m6 9 6 6 6-6" />,
  check:    <path d="M20 6 9 17l-5-5" />,
  up:       <path d="M12 19V5M5 12l7-7 7 7" />,
  down:     <path d="M12 5v14M5 12l7 7 7-7" />,
  wallet:   <><path d="M19 7V5a2 2 0 0 0-2-2H5a2 2 0 0 0 0 4h15a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V5" /><path d="M18 12a1 1 0 0 0 0 2h3v-2Z" /></>,
  target:   <><circle cx="12" cy="12" r="9" /><circle cx="12" cy="12" r="5" /><circle cx="12" cy="12" r="1.4" fill="currentColor" stroke="none" /></>,
  tag:      <><path d="M12.6 2.6 21 11a2 2 0 0 1 0 2.8l-7.2 7.2a2 2 0 0 1-2.8 0L2.6 12.6A2 2 0 0 1 2 11.2V4a2 2 0 0 1 2-2h7.2a2 2 0 0 1 1.4.6Z" /><circle cx="7.5" cy="7.5" r="1.3" fill="currentColor" stroke="none" /></>,
  eye:      <><path d="M2 12s3.5-7 10-7 10 7 10 7-3.5 7-10 7-10-7-10-7Z" /><circle cx="12" cy="12" r="3" /></>,
  trend:    <><path d="m3 17 6-6 4 4 8-8" /><path d="M17 7h4v4" /></>,
  trophy:   <><path d="M6 9a6 6 0 0 0 12 0V4H6Z" /><path d="M6 5H3v2a3 3 0 0 0 3 3M18 5h3v2a3 3 0 0 1-3 3" /><path d="M10 20h4M12 15v5M9 22h6" /></>,
  alert:    <><path d="M12 3 2 20h20L12 3Z" /><path d="M12 10v5M12 18h.01" /></>,
  inbox:    <><path d="M3 12h5l2 3h4l2-3h5" /><path d="M5 5h14l3 7v6a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2v-6Z" /></>,
  filter:   <path d="M3 5h18l-7 8v6l-4-2v-4Z" />,
  x:        <path d="M18 6 6 18M6 6l12 12" />,
  layers:   <><path d="m12 2 9 5-9 5-9-5 9-5Z" /><path d="m3 12 9 5 9-5M3 17l9 5 9-5" /></>,
  bars:     <><path d="M3 21h18" /><rect x="5" y="11" width="3.2" height="7" /><rect x="10.4" y="7" width="3.2" height="11" /><rect x="15.8" y="13" width="3.2" height="5" /></>,
  calendar: <><rect x="3" y="4" width="18" height="18" rx="0" /><path d="M3 9h18M8 2v4M16 2v4" /></>,
  pause:    <><rect x="6" y="5" width="4" height="14" /><rect x="14" y="5" width="4" height="14" /></>,
  spark:    <path d="M12 3v4M12 17v4M3 12h4M17 12h4M6 6l2.5 2.5M15.5 15.5 18 18M18 6l-2.5 2.5M8.5 15.5 6 18" />,
  google:   <><path d="M20.3 12.2c0-.6-.1-1.3-.2-1.9H12v3.5h4.7c-.2 1.1-.9 2-1.9 2.7v2.2h3.1c1.8-1.7 2.4-4.1 2.4-6.5Z" fill="currentColor" stroke="none"/><path d="M12 21c2.4 0 4.5-.8 6-2.2l-3-2.3c-.8.5-1.8.9-3 .9-2.3 0-4.3-1.6-5-3.7H3.9v2.4C5.4 19.1 8.5 21 12 21Z" fill="currentColor" stroke="none"/><path d="M7 13.7c-.2-.6-.3-1.1-.3-1.7s.1-1.1.3-1.7V7.9H3.9C3.3 9.1 3 10.5 3 12s.3 2.9.9 4.1L7 13.7Z" fill="currentColor" stroke="none"/><path d="M12 7.5c1.3 0 2.4.5 3.3 1.3l2.5-2.5C16.4 4.8 14.4 4 12 4 8.5 4 5.4 5.9 3.9 7.9L7 10.3c.7-2.1 2.7-2.8 5-2.8Z" fill="currentColor" stroke="none"/></>,
};

function Delta({ factor, invert = false, neutral = false }) {
  const pct = (factor - 1) * 100;
  const isUp = pct >= 0;
  const good = invert ? !isUp : isUp;
  const cls  = neutral ? "muted" : Math.abs(pct) < 0.05 ? "muted" : good ? "up" : "down";
  return (
    <span className={`delta ${cls}`}>
      <Ico d={isUp ? ICONS.up : ICONS.down} size={12} sw={2.4} />
      {Math.abs(pct).toFixed(1).replace(".", ",")}%
    </span>
  );
}

function Sem({ cpl, withText = true, pill = false }) {
  const s = salud(cpl);
  if (pill) {
    return (
      <span className="sem-pill" style={{ borderColor: s.color, color: s.color }}>
        <span className="bead" style={{ background: s.color, width: 8, height: 8, borderRadius: 999 }} />
        {s.label}
      </span>
    );
  }
  return (
    <span className="sem">
      <span className="bead" style={{ background: s.color }} />
      {withText && <span className="txt" style={{ color: s.color }}>{s.label}</span>}
    </span>
  );
}

function EstadoChip({ estado }) {
  const activa = estado === "Activa" || estado === "Activo";
  return (
    <span className={`estado ${activa ? "activa" : "pausada"}`}>
      <span className="b" />
      {estado}
    </span>
  );
}

// Leyenda actualizada con los umbrales del cliente
function SaludLegend() {
  return (
    <div className="legend">
      <span className="li"><span className="bead" style={{ background: "var(--success)" }} /> &lt; $100 Saludable</span>
      <span className="li"><span className="bead" style={{ background: "var(--warning)" }} /> $100–$250 Atención</span>
      <span className="li"><span className="bead" style={{ background: "var(--danger)"  }} /> &gt; $250 Crítico</span>
    </div>
  );
}

Object.assign(window, { Ico, ICONS, Delta, Sem, EstadoChip, SaludLegend });
