// Cart count update
function updateCartCount() {
    fetch('/api/cart.php?action=count')
        .then(r => r.json())
        .then(d => {
            const el = document.getElementById('cart-count');
            if (el) el.textContent = d.count;
        });
}

// Debounce helper
function debounce(fn, delay) {
    let timer;
    return function(...args) {
        clearTimeout(timer);
        timer = setTimeout(() => fn.apply(this, args), delay);
    };
}

// Quantity input handler
document.querySelectorAll('.qty-input').forEach(input => {
    input.addEventListener('change', function() {
        if (this.value < 1) this.value = 1;
    });
});

// Auto-dismiss alerts
document.addEventListener('DOMContentLoaded', function() {
    document.querySelectorAll('.alert-dismissible').forEach(el => {
        setTimeout(() => {
            el.style.transition = 'opacity 0.3s';
            el.style.opacity = '0';
            setTimeout(() => el.remove(), 300);
        }, 5000);
    });
});

// Search form enhancement
const searchInput = document.querySelector('input[name="q"]');
if (searchInput) {
    searchInput.addEventListener('input', debounce(function() {
        if (this.value.length >= 2) {
            // Could add live search suggestions here
        }
    }, 300));
}
