// Componentes compartidos del CRM. Cargado primero — define globals usados por app/modules.

// === ICONOS (subset de lucide-react, stroke-based) ===
const Icon = ({ name, size = 16, className = "", strokeWidth = 1.75 }) => {
  const icons = {
    LayoutDashboard: <><rect x="3" y="3" width="7" height="9"/><rect x="14" y="3" width="7" height="5"/><rect x="14" y="12" width="7" height="9"/><rect x="3" y="16" width="7" height="5"/></>,
    BarChart3: <><path d="M3 3v18h18"/><path d="M7 16V9"/><path d="M12 16v-5"/><path d="M17 16V6"/></>,
    Building2: <><path d="M6 22V4a2 2 0 0 1 2-2h8a2 2 0 0 1 2 2v18Z"/><path d="M6 12H4a2 2 0 0 0-2 2v6a2 2 0 0 0 2 2h2"/><path d="M18 9h2a2 2 0 0 1 2 2v9a2 2 0 0 1-2 2h-2"/><path d="M10 6h4"/><path d="M10 10h4"/><path d="M10 14h4"/><path d="M10 18h4"/></>,
    Trophy: <><path d="M6 9H4.5a2.5 2.5 0 0 1 0-5H6"/><path d="M18 9h1.5a2.5 2.5 0 0 0 0-5H18"/><path d="M4 22h16"/><path d="M10 14.66V17c0 .55-.47.98-.97 1.21C7.85 18.75 7 20.24 7 22"/><path d="M14 14.66V17c0 .55.47.98.97 1.21C16.15 18.75 17 20.24 17 22"/><path d="M18 2H6v7a6 6 0 0 0 12 0V2Z"/></>,
    FileText: <><path d="M15 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7Z"/><path d="M14 2v4a2 2 0 0 0 2 2h4"/><path d="M10 9H8"/><path d="M16 13H8"/><path d="M16 17H8"/></>,
    Receipt: <><path d="M4 2v20l2-1 2 1 2-1 2 1 2-1 2 1 2-1 2 1V2l-2 1-2-1-2 1-2-1-2 1-2-1-2 1-2-1Z"/><path d="M16 8H8"/><path d="M16 12H8"/><path d="M13 16H8"/></>,
    Package: <><path d="m7.5 4.27 9 5.15"/><path d="M21 8a2 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"/><path d="M3.3 7 12 12l8.7-5"/><path d="M12 22V12"/></>,
    Truck: <><path d="M14 18V6a2 2 0 0 0-2-2H4a2 2 0 0 0-2 2v11a1 1 0 0 0 1 1h2"/><path d="M15 18H9"/><path d="M19 18h2a1 1 0 0 0 1-1v-3.65a1 1 0 0 0-.22-.624l-3.48-4.35A1 1 0 0 0 17.52 8H14"/><circle cx="17" cy="18" r="2"/><circle cx="7" cy="18" r="2"/></>,
    ShieldCheck: <><path d="M20 13c0 5-3.5 7.5-7.66 8.95a1 1 0 0 1-.67-.01C7.5 20.5 4 18 4 13V6a1 1 0 0 1 1-1c2 0 4.5-1.2 6.24-2.72a1.17 1.17 0 0 1 1.52 0C14.51 3.81 17 5 19 5a1 1 0 0 1 1 1Z"/><path d="m9 12 2 2 4-4"/></>,
    Headphones: <><path d="M3 14h3a2 2 0 0 1 2 2v3a2 2 0 0 1-2 2H4a1 1 0 0 1-1-1v-6a9 9 0 0 1 18 0v6a1 1 0 0 1-1 1h-2a2 2 0 0 1-2-2v-3a2 2 0 0 1 2-2h3"/></>,
    Settings: <><path d="M12.22 2h-.44a2 2 0 0 0-2 2v.18a2 2 0 0 1-1 1.73l-.43.25a2 2 0 0 1-2 0l-.15-.08a2 2 0 0 0-2.73.73l-.22.38a2 2 0 0 0 .73 2.73l.15.1a2 2 0 0 1 1 1.72v.51a2 2 0 0 1-1 1.74l-.15.09a2 2 0 0 0-.73 2.73l.22.38a2 2 0 0 0 2.73.73l.15-.08a2 2 0 0 1 2 0l.43.25a2 2 0 0 1 1 1.73V20a2 2 0 0 0 2 2h.44a2 2 0 0 0 2-2v-.18a2 2 0 0 1 1-1.73l.43-.25a2 2 0 0 1 2 0l.15.08a2 2 0 0 0 2.73-.73l.22-.39a2 2 0 0 0-.73-2.73l-.15-.08a2 2 0 0 1-1-1.74v-.5a2 2 0 0 1 1-1.74l.15-.09a2 2 0 0 0 .73-2.73l-.22-.38a2 2 0 0 0-2.73-.73l-.15.08a2 2 0 0 1-2 0l-.43-.25a2 2 0 0 1-1-1.73V4a2 2 0 0 0-2-2z"/><circle cx="12" cy="12" r="3"/></>,
    Brain: <><path d="M12 5a3 3 0 1 0-5.997.125 4 4 0 0 0-2.526 5.77 4 4 0 0 0 .556 6.588A4 4 0 1 0 12 18Z"/><path d="M12 5a3 3 0 1 1 5.997.125 4 4 0 0 1 2.526 5.77 4 4 0 0 1-.556 6.588A4 4 0 1 1 12 18Z"/><path d="M15 13a4.5 4.5 0 0 1-3-4 4.5 4.5 0 0 1-3 4"/></>,
    Sparkles: <><path d="M9.937 15.5A2 2 0 0 0 8.5 14.063l-6.135-1.582a.5.5 0 0 1 0-.962L8.5 9.936A2 2 0 0 0 9.937 8.5l1.582-6.135a.5.5 0 0 1 .963 0L14.063 8.5A2 2 0 0 0 15.5 9.937l6.135 1.581a.5.5 0 0 1 0 .964L15.5 14.063a2 2 0 0 0-1.437 1.437l-1.582 6.135a.5.5 0 0 1-.963 0z"/><path d="M20 3v4"/><path d="M22 5h-4"/><path d="M4 17v2"/><path d="M5 18H3"/></>,
    Search: <><circle cx="11" cy="11" r="8"/><path d="m21 21-4.3-4.3"/></>,
    Plus: <><path d="M5 12h14"/><path d="M12 5v14"/></>,
    ChevronDown: <><path d="m6 9 6 6 6-6"/></>,
    ChevronRight: <><path d="m9 18 6-6-6-6"/></>,
    ChevronLeft: <><path d="m15 18-6-6 6-6"/></>,
    ArrowLeft: <><path d="m12 19-7-7 7-7"/><path d="M19 12H5"/></>,
    ArrowRight: <><path d="M5 12h14"/><path d="m12 5 7 7-7 7"/></>,
    ArrowUpRight: <><path d="M7 7h10v10"/><path d="M7 17 17 7"/></>,
    MoreHorizontal: <><circle cx="12" cy="12" r="1"/><circle cx="19" cy="12" r="1"/><circle cx="5" cy="12" r="1"/></>,
    Calendar: <><path d="M8 2v4"/><path d="M16 2v4"/><rect width="18" height="18" x="3" y="4" rx="2"/><path d="M3 10h18"/></>,
    Clock: <><circle cx="12" cy="12" r="10"/><polyline points="12 6 12 12 16 14"/></>,
    TrendingUp: <><polyline points="22 7 13.5 15.5 8.5 10.5 2 17"/><polyline points="16 7 22 7 22 13"/></>,
    TrendingDown: <><polyline points="22 17 13.5 8.5 8.5 13.5 2 7"/><polyline points="16 17 22 17 22 11"/></>,
    Filter: <><polygon points="22 3 2 3 10 12.46 10 19 14 21 14 12.46 22 3"/></>,
    LayoutGrid: <><rect width="7" height="7" x="3" y="3" rx="1"/><rect width="7" height="7" x="14" y="3" rx="1"/><rect width="7" height="7" x="14" y="14" rx="1"/><rect width="7" height="7" x="3" y="14" rx="1"/></>,
    List: <><path d="M8 6h13"/><path d="M8 12h13"/><path d="M8 18h13"/><path d="M3 6h.01"/><path d="M3 12h.01"/><path d="M3 18h.01"/></>,
    Command: <><path d="M18 3a3 3 0 0 0-3 3v12a3 3 0 0 0 3 3 3 3 0 0 0 3-3 3 3 0 0 0-3-3H6a3 3 0 0 0-3 3 3 3 0 0 0 3 3 3 3 0 0 0 3-3V6a3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3h12a3 3 0 0 0 3-3 3 3 0 0 0-3-3z"/></>,
    Paperclip: <><path d="M13.234 20.252 21 12.3"/><path d="m16 6-8.414 8.586a2 2 0 0 0 0 2.828 2 2 0 0 0 2.828 0l8.414-8.586a4 4 0 0 0 0-5.656 4 4 0 0 0-5.656 0l-8.415 8.585a6 6 0 1 0 8.486 8.486"/></>,
    MapPin: <><path d="M20 10c0 4.993-5.539 10.193-7.399 11.799a1 1 0 0 1-1.202 0C9.539 20.193 4 14.993 4 10a8 8 0 0 1 16 0"/><circle cx="12" cy="10" r="3"/></>,
    User: <><path d="M19 21v-2a4 4 0 0 0-4-4H9a4 4 0 0 0-4 4v2"/><circle cx="12" cy="7" r="4"/></>,
    Users: <><path d="M16 21v-2a4 4 0 0 0-4-4H6a4 4 0 0 0-4 4v2"/><circle cx="9" cy="7" r="4"/><path d="M22 21v-2a4 4 0 0 0-3-3.87"/><path d="M16 3.13a4 4 0 0 1 0 7.75"/></>,
    Mail: <><rect width="20" height="16" x="2" y="4" rx="2"/><path d="m22 7-8.97 5.7a1.94 1.94 0 0 1-2.06 0L2 7"/></>,
    Phone: <><path d="M22 16.92v3a2 2 0 0 1-2.18 2 19.79 19.79 0 0 1-8.63-3.07 19.5 19.5 0 0 1-6-6 19.79 19.79 0 0 1-3.07-8.67A2 2 0 0 1 4.11 2h3a2 2 0 0 1 2 1.72 12.84 12.84 0 0 0 .7 2.81 2 2 0 0 1-.45 2.11L8.09 9.91a16 16 0 0 0 6 6l1.27-1.27a2 2 0 0 1 2.11-.45 12.84 12.84 0 0 0 2.81.7A2 2 0 0 1 22 16.92z"/></>,
    Link2: <><path d="M9 17H7A5 5 0 0 1 7 7h2"/><path d="M15 7h2a5 5 0 1 1 0 10h-2"/><line x1="8" x2="16" y1="12" y2="12"/></>,
    CheckCircle2: <><path d="M21.801 10A10 10 0 1 1 17 3.335"/><path d="m9 11 3 3L22 4"/></>,
    XCircle: <><circle cx="12" cy="12" r="10"/><path d="m15 9-6 6"/><path d="m9 9 6 6"/></>,
    AlertTriangle: <><path d="m21.73 18-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3Z"/><path d="M12 9v4"/><path d="M12 17h.01"/></>,
    ClipboardList: <><rect width="8" height="4" x="8" y="2" rx="1" ry="1"/><path d="M16 4h2a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h2"/><path d="M12 11h4"/><path d="M12 16h4"/><path d="M8 11h.01"/><path d="M8 16h.01"/></>,
    Ban: <><circle cx="12" cy="12" r="10"/><path d="m4.9 4.9 14.2 14.2"/></>,
    DollarSign: <><line x1="12" x2="12" y1="2" y2="22"/><path d="M17 5H9.5a3.5 3.5 0 0 0 0 7h5a3.5 3.5 0 0 1 0 7H6"/></>,
    CircleDollarSign: <><circle cx="12" cy="12" r="10"/><path d="M16 8h-6a2 2 0 1 0 0 4h4a2 2 0 1 1 0 4H8"/><path d="M12 18V6"/></>,
    PanelLeftClose: <><rect width="18" height="18" x="3" y="3" rx="2"/><path d="M9 3v18"/><path d="m16 15-3-3 3-3"/></>,
    Eye: <><path d="M2.062 12.348a1 1 0 0 1 0-.696 10.75 10.75 0 0 1 19.876 0 1 1 0 0 1 0 .696 10.75 10.75 0 0 1-19.876 0"/><circle cx="12" cy="12" r="3"/></>,
    Edit3: <><path d="M12 20h9"/><path d="M16.5 3.5a2.121 2.121 0 0 1 3 3L7 19l-4 1 1-4L16.5 3.5z"/></>,
    Download: <><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" x2="12" y1="15" y2="3"/></>,
    X: <><path d="M18 6 6 18"/><path d="m6 6 12 12"/></>,
    Sliders: <><line x1="4" x2="4" y1="21" y2="14"/><line x1="4" x2="4" y1="10" y2="3"/><line x1="12" x2="12" y1="21" y2="12"/><line x1="12" x2="12" y1="8" y2="3"/><line x1="20" x2="20" y1="21" y2="16"/><line x1="20" x2="20" y1="12" y2="3"/><line x1="2" x2="6" y1="14" y2="14"/><line x1="10" x2="14" y1="8" y2="8"/><line x1="18" x2="22" y1="16" y2="16"/></>,
    Upload: <><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" x2="12" y1="3" y2="15"/></>,
    Funnel: <><path d="M10 20a1 1 0 0 0 .4.8l2 1.5a1 1 0 0 0 1.6-.8V14l5.7-7.4A1 1 0 0 0 19 5H5a1 1 0 0 0-.7 1.6L10 14Z"/></>,
    SlidersHorizontal: <><line x1="21" x2="14" y1="4" y2="4"/><line x1="10" x2="3" y1="4" y2="4"/><line x1="21" x2="12" y1="12" y2="12"/><line x1="8" x2="3" y1="12" y2="12"/><line x1="21" x2="16" y1="20" y2="20"/><line x1="12" x2="3" y1="20" y2="20"/><line x1="14" x2="14" y1="2" y2="6"/><line x1="8" x2="8" y1="10" y2="14"/><line x1="16" x2="16" y1="18" y2="22"/></>,
    Tag: <><path d="M12.586 2.586A2 2 0 0 0 11.172 2H4a2 2 0 0 0-2 2v7.172a2 2 0 0 0 .586 1.414l8.704 8.704a2.426 2.426 0 0 0 3.42 0l6.58-6.58a2.426 2.426 0 0 0 0-3.42z"/><circle cx="7.5" cy="7.5" r=".5" fill="currentColor"/></>,
    Star: <><path d="M11.525 2.295a.53.53 0 0 1 .95 0l2.31 4.679a2.123 2.123 0 0 0 1.595 1.16l5.166.756a.53.53 0 0 1 .294.904l-3.736 3.638a2.123 2.123 0 0 0-.611 1.878l.882 5.14a.53.53 0 0 1-.771.56l-4.618-2.428a2.122 2.122 0 0 0-1.973 0L6.396 21.01a.53.53 0 0 1-.77-.56l.881-5.139a2.122 2.122 0 0 0-.611-1.879L2.16 9.795a.53.53 0 0 1 .294-.906l5.165-.755a2.122 2.122 0 0 0 1.597-1.16z"/></>,
    UserX: <><path d="M13.5 6.5a4 4 0 1 1-8 0 4 4 0 0 1 8 0Z"/><path d="M17 11 22 6"/><path d="m17 6 5 5"/><path d="M2 21a8 8 0 0 1 13.292-6"/></>,
    Cloud: <><path d="M17.5 19a4.5 4.5 0 1 0-1.41-8.775 5.5 5.5 0 0 0-10.836 1.984A4.5 4.5 0 0 0 6.5 19z"/></>,
    RefreshCw: <><path d="M3 12a9 9 0 0 1 9-9 9.75 9.75 0 0 1 6.74 2.74L21 8"/><path d="M21 3v5h-5"/><path d="M21 12a9 9 0 0 1-9 9 9.75 9.75 0 0 1-6.74-2.74L3 16"/><path d="M8 16H3v5"/></>,
    Check: <><path d="M20 6 9 17l-5-5"/></>,
    Inbox: <><polyline points="22 12 16 12 14 15 10 15 8 12 2 12"/><path d="M5.45 5.11 2 12v6a2 2 0 0 0 2 2h16a2 2 0 0 0 2-2v-6l-3.45-6.89A2 2 0 0 0 16.76 4H7.24a2 2 0 0 0-1.79 1.11z"/></>,
    ArrowDownNarrowWide: <><path d="m3 16 4 4 4-4"/><path d="M7 20V4"/><path d="M11 4h4"/><path d="M11 8h7"/><path d="M11 12h10"/></>,
    Target: <><circle cx="12" cy="12" r="10"/><circle cx="12" cy="12" r="6"/><circle cx="12" cy="12" r="2"/></>,
    History: <><path d="M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8"/><path d="M3 3v5h5"/><path d="M12 7v5l4 2"/></>,
    FileCheck2: <><path d="M4 22h14a2 2 0 0 0 2-2V7l-5-5H6a2 2 0 0 0-2 2v4"/><polyline points="14 2 14 8 20 8"/><path d="m3 15 2 2 4-4"/></>,
    FileSearch: <><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"/><circle cx="11.5" cy="14.5" r="2.5"/><path d="M13.25 16.25 15 18"/></>,
    FileQuestion: <><path d="M12 17h.01"/><path d="M15 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7Z"/><path d="M9.1 9a3 3 0 0 1 5.82 1c0 2-3 3-3 3"/></>,
    Info: <><circle cx="12" cy="12" r="10"/><path d="M12 16v-4"/><path d="M12 8h.01"/></>,
    Circle: <><circle cx="12" cy="12" r="10"/></>,
    RotateCcw: <><path d="M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8"/><path d="M3 3v5h5"/></>,
    ExternalLink: <><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"/></>,
    Wrench: <><path d="M14.7 6.3a1 1 0 0 0 0 1.4l1.6 1.6a1 1 0 0 0 1.4 0l3.77-3.77a6 6 0 0 1-7.94 7.94l-6.91 6.91a2.12 2.12 0 0 1-3-3l6.91-6.91a6 6 0 0 1 7.94-7.94l-3.76 3.76z"/></>,
    FileX: <><path d="M15 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7Z"/><polyline points="14 2 14 8 20 8"/><path d="m14.5 12.5-5 5"/><path d="m9.5 12.5 5 5"/></>,
  };

  return (
    <svg xmlns="http://www.w3.org/2000/svg" width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={strokeWidth} strokeLinecap="round" strokeLinejoin="round" className={className}>
      {icons[name] || null}
    </svg>
  );
};

// === STATUS BADGE ===
// Estados disponibles: vigente, evaluacion, inconformidad, adjudicado, perdido, anulado
// y estados genéricos del backend (nuevo, en_revision, promovido, descartado, archivado, etc.).
const ESTADOS_CATALOGO = {
  // Pipeline de concursos (zip)
  vigente:       { label: "Vigente",        emoji: "🆕", icon: "Sparkles",        bg: "#f1f5f9", fg: "#475569", border: "#e2e8f0" },
  evaluacion:    { label: "En Evaluación",  emoji: "📋", icon: "ClipboardList",   bg: "#fffbeb", fg: "#b45309", border: "#fde68a" },
  inconformidad: { label: "Inconformidad",  emoji: "⚠️", icon: "AlertTriangle",   bg: "#fff7ed", fg: "#c2410c", border: "#fed7aa" },
  adjudicado:    { label: "Adjudicado",     emoji: "✅", icon: "CheckCircle2",    bg: "#ecfdf5", fg: "#047857", border: "#a7f3d0" },
  perdido:       { label: "Perdido",        emoji: "❌", icon: "XCircle",         bg: "#fef2f2", fg: "#b91c1c", border: "#fecaca" },
  anulado:       { label: "Anulado",        emoji: "🚫", icon: "Ban",             bg: "#f9fafb", fg: "#4b5563", border: "#e5e7eb" },
  // Estados eventos_nog del backend albor
  nuevo:         { label: "Nuevo",          emoji: "✨", icon: "Sparkles",        bg: "#eff6ff", fg: "#1d4ed8", border: "#bfdbfe" },
  en_revision:   { label: "En revisión",    emoji: "👀", icon: "Eye",             bg: "#fffbeb", fg: "#b45309", border: "#fde68a" },
  promovido:     { label: "Promovido",      emoji: "🚀", icon: "ArrowUpRight",    bg: "#ecfdf5", fg: "#047857", border: "#a7f3d0" },
  descartado:    { label: "Descartado",     emoji: "🗑️", icon: "X",               bg: "#f9fafb", fg: "#6b7280", border: "#e5e7eb" },
  archivado:     { label: "Archivado",      emoji: "📦", icon: "Package",         bg: "#f5f5f4", fg: "#57534e", border: "#e7e5e4" },
  bandeja:       { label: "Bandeja",        emoji: "📥", icon: "Inbox",           bg: "#eef2ff", fg: "#4338ca", border: "#c7d2fe" },
  // Estados de tareas / soporte
  pendiente:     { label: "Pendiente",      emoji: "⏳", icon: "Clock",           bg: "#fffbeb", fg: "#b45309", border: "#fde68a" },
  en_progreso:   { label: "En progreso",    emoji: "🔄", icon: "RefreshCw",       bg: "#eff6ff", fg: "#1d4ed8", border: "#bfdbfe" },
  completada:    { label: "Completada",     emoji: "✓",  icon: "Check",           bg: "#ecfdf5", fg: "#047857", border: "#a7f3d0" },
  cancelada:     { label: "Cancelada",      emoji: "✕",  icon: "X",               bg: "#fef2f2", fg: "#b91c1c", border: "#fecaca" },
  // Facturación
  borrador:      { label: "Borrador",       emoji: "📝", icon: "Edit3",           bg: "#f3f4f6", fg: "#374151", border: "#e5e7eb" },
  enviada:       { label: "Enviada",        emoji: "📤", icon: "Upload",          bg: "#eff6ff", fg: "#1d4ed8", border: "#bfdbfe" },
  aceptada:      { label: "Aceptada",       emoji: "✅", icon: "CheckCircle2",    bg: "#ecfdf5", fg: "#047857", border: "#a7f3d0" },
  pagada:        { label: "Pagada",         emoji: "💰", icon: "CircleDollarSign",bg: "#ecfdf5", fg: "#047857", border: "#a7f3d0" },
  vencida:       { label: "Vencida",        emoji: "⏰", icon: "Clock",           bg: "#fef2f2", fg: "#b91c1c", border: "#fecaca" },
  // Niveles de log
  info:          { label: "Info",           emoji: "ℹ️", icon: "Info",            bg: "#eff6ff", fg: "#1d4ed8", border: "#bfdbfe" },
  warn:          { label: "Warn",           emoji: "⚠️", icon: "AlertTriangle",   bg: "#fffbeb", fg: "#b45309", border: "#fde68a" },
  error:         { label: "Error",          emoji: "❌", icon: "XCircle",         bg: "#fef2f2", fg: "#b91c1c", border: "#fecaca" },
  critical:      { label: "Crítico",        emoji: "🚨", icon: "AlertTriangle",   bg: "#fef2f2", fg: "#b91c1c", border: "#fecaca" },
  // Prioridades
  baja:          { label: "Baja",           emoji: "🔽", icon: "ChevronDown",     bg: "#eff6ff", fg: "#1d4ed8", border: "#bfdbfe" },
  media:         { label: "Media",          emoji: "▶️", icon: "Circle",          bg: "#fffbeb", fg: "#b45309", border: "#fde68a" },
  alta:          { label: "Alta",           emoji: "🔼", icon: "TrendingUp",      bg: "#fff7ed", fg: "#c2410c", border: "#fed7aa" },
  urgente:       { label: "Urgente",        emoji: "🔥", icon: "AlertTriangle",   bg: "#fef2f2", fg: "#b91c1c", border: "#fecaca" },
};

const StatusBadge = ({ estado, showEmoji = true }) => {
  if (!estado) return null;
  const s = ESTADOS_CATALOGO[estado] || { label: estado, emoji: "•", icon: "Circle", bg: "#f3f4f6", fg: "#374151", border: "#e5e7eb" };
  return (
    <span className="badge" style={{ background: s.bg, color: s.fg, borderColor: s.border }}>
      {showEmoji ? <span className="badge__emoji">{s.emoji}</span> : <Icon name={s.icon} size={11} />}
      {s.label}
    </span>
  );
};

// === AVATAR ===
const Avatar = ({ iniciales, color = "blue", size = 24 }) => {
  const colors = {
    blue: { bg: "oklch(0.93 0.04 250)", fg: "oklch(0.45 0.15 250)" },
    emerald: { bg: "oklch(0.93 0.06 160)", fg: "oklch(0.42 0.13 160)" },
    purple: { bg: "oklch(0.93 0.05 290)", fg: "oklch(0.45 0.14 290)" },
  };
  const c = colors[color] || colors.blue;
  return (
    <div className="avatar" style={{ width: size, height: size, background: c.bg, color: c.fg, fontSize: size * 0.42 }}>
      {iniciales}
    </div>
  );
};

// === Helpers de formato (compartidos en window) ===
window.fmtQ = (n) => "Q " + Number(n ?? 0).toLocaleString("es-GT", { minimumFractionDigits: 0, maximumFractionDigits: 0 });
window.fmtQFull = (n) => "Q " + Number(n ?? 0).toLocaleString("es-GT", { minimumFractionDigits: 2, maximumFractionDigits: 2 });
window.fmtFecha = (iso) => {
  if (!iso) return "—";
  const meses = ["ene","feb","mar","abr","may","jun","jul","ago","sep","oct","nov","dic"];
  const d = new Date(iso);
  if (Number.isNaN(d.getTime())) return iso;
  return `${d.getDate()} ${meses[d.getMonth()]} ${d.getFullYear()}`;
};
window.fmtFechaCorta = (iso) => {
  if (!iso) return "—";
  const meses = ["ene","feb","mar","abr","may","jun","jul","ago","sep","oct","nov","dic"];
  const d = new Date(iso);
  if (Number.isNaN(d.getTime())) return iso;
  return `${d.getDate()} ${meses[d.getMonth()]}`;
};
window.fmtFechaHora = (iso) => {
  if (!iso) return "—";
  const d = new Date(iso);
  if (Number.isNaN(d.getTime())) return iso;
  return d.toLocaleString("es-GT", { dateStyle: "short", timeStyle: "short" });
};
window.diasHasta = (iso) => {
  if (!iso) return null;
  const d = new Date(iso);
  if (Number.isNaN(d.getTime())) return null;
  const hoy = new Date();
  return Math.round((d - hoy) / (1000 * 60 * 60 * 24));
};
window.horasHasta = (iso) => {
  if (!iso) return null;
  const d = new Date(iso);
  if (Number.isNaN(d.getTime())) return null;
  return (d - new Date()) / (1000 * 60 * 60);
};

Object.assign(window, { Icon, StatusBadge, Avatar, ESTADOS_CATALOGO });
