// Reusable UI Components
const Modal = ({ isOpen, onClose, title, children, maxWidth = "max-w-6xl", zIndex = "z-50" }) => {
    if (!isOpen) return null;
    return (
        <div className={`fixed inset-0 ${zIndex} flex items-center justify-center p-4 backdrop-blur-sm bg-slate-900/60 transition-all duration-300`} onClick={onClose}>
            <div className={`bg-white rounded-3xl shadow-2xl w-full ${maxWidth} flex flex-col max-h-[90vh] overflow-hidden animate-fade-in border border-white/50`} onClick={(e) => e.stopPropagation()}>
                <div className="px-8 py-6 border-b border-slate-100 flex justify-between items-center bg-white/80 backdrop-blur sticky top-0 z-10">
                    <h3 className="text-2xl font-bold text-slate-800 tracking-tight">{title}</h3>
                    <button onClick={onClose} className="p-2 bg-slate-50 hover:bg-slate-100 rounded-full text-slate-400 hover:text-slate-600 transition"><Icon name="X" size={20} /></button>
                </div>
                <div className="p-8 overflow-y-auto bg-slate-50/50 h-full">{children}</div>
            </div>
        </div>
    );
};

const Card = ({ children, className = "" }) => (
    <div className={`bg-white rounded-3xl shadow-soft border border-slate-100 p-6 transition-all hover:shadow-lg hover:-translate-y-1 ${className}`}>
        {children}
    </div>
);

const Badge = ({ children, color = "blue" }) => {
    const c = {
        blue: "bg-blue-50 text-blue-700 border-blue-100",
        slate: "bg-slate-100 text-slate-600 border-slate-200"
    };
    return <span className={`px-3 py-1 rounded-full text-xs font-bold border ${c[color] || c.slate}`}>{children}</span>;
};

const CustomTooltip = ({ active, payload, label }) => {
    if (active && payload && payload.length) {
        return (
            <div className="bg-white p-4 rounded-xl shadow-xl border border-slate-100 text-sm">
                <p className="font-bold text-slate-800 mb-2">{label}</p>
                {payload.map((e, i) => (
                    <div key={i} className="flex items-center gap-2 mb-1">
                        <div className="w-2 h-2 rounded-full" style={{ backgroundColor: e.color }}></div>
                        <span className="text-slate-500">{e.name}:</span>
                        <span className="font-bold text-slate-700">{e.value}</span>
                    </div>
                ))}
            </div>
        );
    }
    return null;
};

const StatCard = ({ title, value, percentage, iconName, color, onClick, isClickable = false, size = "normal", isActive = false }) => (
    <div onClick={onClick} className={`relative overflow-hidden bg-white rounded-3xl p-6 border transition-all duration-300 group ${isClickable ? 'cursor-pointer hover:shadow-glow' : ''} ${isActive ? 'border-blue-400 ring-2 ring-blue-100 shadow-glow' : 'border-white/60 shadow-soft hover:border-blue-200'} ${size === 'small' ? 'h-36' : 'h-full'}`}>
        <div className={`absolute -right-6 -top-6 w-24 h-24 rounded-full opacity-5 transition-transform group-hover:scale-110 ${color.replace('text-', 'bg-')}`}></div>
        <div className="flex justify-between items-start mb-4 relative z-10">
            <div className={`p-3.5 rounded-2xl ${color.replace('text-', 'bg-').replace('600', '50').replace('500', '50').replace('700', '50')} ${color}`}>
                <Icon name={iconName} size={size === 'small' ? 20 : 24} />
            </div>
            {percentage != null && (
                <div className={`flex items-center gap-1 px-2.5 py-1 rounded-lg text-sm font-bold ${color.includes('red') ? 'bg-red-50 text-red-600' : 'bg-emerald-50 text-emerald-600'}`}>
                    {percentage}%
                </div>
            )}
        </div>
        <div className="relative z-10">
            <p className="text-xs font-bold text-slate-400 uppercase tracking-wider mb-1">{title}</p>
            <h3 className={`font-extrabold text-slate-800 tracking-tight ${size === 'small' ? 'text-2xl' : 'text-4xl'}`}>{value}</h3>
        </div>
    </div>
);
