// BENGALI E-COMMERCE — MAIN JS v4
document.addEventListener('DOMContentLoaded', () => {

    // ── Hamburger ──
    const hamburger = document.getElementById('hamburger');
    const mainNav   = document.getElementById('mainNav');
    if (hamburger && mainNav) {
        hamburger.addEventListener('click', () => {
            const open = mainNav.classList.toggle('open');
            hamburger.classList.toggle('active', open);
        });
        document.addEventListener('click', e => {
            if (!hamburger.contains(e.target) && !mainNav.contains(e.target)) {
                mainNav.classList.remove('open');
                hamburger.classList.remove('active');
            }
        });
    }

    // ── Auto Carousel (tablet/desktop only) ──
    if (window.innerWidth >= 600) {
        document.querySelectorAll('.auto-carousel').forEach(track => {
            let raf, pos = 0;
            function step() {
                const max = track.scrollWidth - track.clientWidth;
                if (max <= 0) return;
                pos += 0.5;
                if (pos >= max) pos = 0;
                track.scrollLeft = pos;
                raf = requestAnimationFrame(step);
            }
            raf = requestAnimationFrame(step);
            track.addEventListener('pointerenter', () => cancelAnimationFrame(raf));
            track.addEventListener('pointerleave', () => { raf = requestAnimationFrame(step); });
        });
    }

    // ── Confirm delete ──
    document.querySelectorAll('[data-confirm]').forEach(el => {
        el.addEventListener('click', function(e) {
            if (!confirm(this.dataset.confirm || 'আপনি কি নিশ্চিত?')) e.preventDefault();
        });
    });

    // ── Auto hide alerts ──
    setTimeout(() => {
        document.querySelectorAll('.alert').forEach(a => {
            a.style.transition = 'opacity .5s';
            a.style.opacity = '0';
            setTimeout(() => a.remove(), 500);
        });
    }, 5000);
});
