function showSuccess(message, duration = 5000) {
    showNotification(message, 'success', duration);
}

function showError(message, duration = 7000) {
    showNotification(message, 'error', duration);
}

function showInfo(message, duration = 5000) {
    showNotification(message, 'info', duration);
}

function showWarning(message, duration = 6000) {
    showNotification(message, 'warning', duration);
}

function showNotification(message, type, duration = 5000) {
    const existingNotification = document.querySelector(`.notification-${type}`);
    if (existingNotification) {
        existingNotification.remove();
    }
    
    const notification = document.createElement('div');
    notification.className = `notification notification-${type}`;
    
    let icon = '';
    switch(type) {
        case 'success':
            icon = '<i class="fas fa-check-circle"></i>';
            break;
        case 'error':
            icon = '<i class="fas fa-exclamation-circle"></i>';
            break;
        case 'warning':
            icon = '<i class="fas fa-exclamation-triangle"></i>';
            break;
        case 'info':
        default:
            icon = '<i class="fas fa-info-circle"></i>';
            break;
    }
    
    notification.innerHTML = `
        <div class="notification-content">
            <div class="notification-icon">${icon}</div>
            <span class="notification-message">${message}</span>
        </div>
        <button class="notification-close" onclick="closeNotification(this)">&times;</button>
    `;
    
    document.body.appendChild(notification);
    
    setTimeout(() => {
        if (notification.parentNode) {
            notification.classList.add('notification-fade-out');
            setTimeout(() => {
                if (notification.parentNode) {
                    notification.remove();
                }
            }, 300);
        }
    }, duration);
}

function closeNotification(button) {
    const notification = button.closest('.notification');
    if (notification) {
        notification.classList.add('notification-fade-out');
        setTimeout(() => {
            if (notification.parentNode) {
                notification.remove();
            }
        }, 300);
    }
}

function clearAllNotifications() {
    const notifications = document.querySelectorAll('.notification');
    notifications.forEach(notification => {
        notification.classList.add('notification-fade-out');
        setTimeout(() => {
            if (notification.parentNode) {
                notification.remove();
            }
        }, 300);
    });
}

function showNotificationStack(messages, type = 'info', duration = 5000) {
    messages.forEach((message, index) => {
        setTimeout(() => {
            showNotification(message, type, duration);
        }, index * 100);
    });
}

function showLoading(contextElement = null) {
    const existingLoading = document.querySelector('.loading-overlay');
    if (existingLoading) {
        existingLoading.remove();
    }
    
    const loading = document.createElement('div');
    if (contextElement) {
        loading.className = 'loading-contextual';
        loading.innerHTML = `<div class="loading-spinner-small"></div>`;
        contextElement.appendChild(loading);
    } else {
        loading.className = 'loading-overlay';
        loading.innerHTML = `
            <div class="loading-spinner"></div>
            <p>Loading...</p>
        `;
        document.body.appendChild(loading);
    }
}

function hideLoading() {
    const loading = document.querySelector('.loading-overlay') || document.querySelector('.loading-contextual');
    if (loading) {
        loading.remove();
    }
}