/* ============================================================
   ADMIN DASHBOARD — JS
   ============================================================ */

document.addEventListener('DOMContentLoaded', () => {

    /* =========================================================
       SIDEBAR TOGGLE (mobile)
    ========================================================= */

    const sidebar  = document.getElementById('sidebar');
    const overlay  = document.getElementById('sidebarOverlay');
    const hamburger = document.getElementById('hamburgerBtn');

    const openSidebar = () => {
        if (!sidebar) return;
        sidebar.classList.add('open');
        if (overlay) overlay.classList.add('active');
        document.body.style.overflow = 'hidden';
    };

    const closeSidebar = () => {
        if (!sidebar) return;
        sidebar.classList.remove('open');
        if (overlay) overlay.classList.remove('active');
        document.body.style.overflow = '';
    };

    if (hamburger) {
        hamburger.addEventListener('click', () => {
            sidebar.classList.contains('open')
                ? closeSidebar()
                : openSidebar();
        });
    }

    if (overlay) {
        overlay.addEventListener('click', closeSidebar);
    }

    /* =========================================================
       COUNTER ANIMATION
    ========================================================= */

    const counters = document.querySelectorAll('[data-count]');

    counters.forEach(el => {
        const target = parseInt(el.dataset.count, 10);
        if (isNaN(target) || target === 0) return;

        const duration = 1200;
        const start = performance.now();

        el.textContent = '0';

        const tick = (now) => {
            const elapsed = now - start;
            const progress = Math.min(elapsed / duration, 1);

            /* ease-out cubic */
            const ease = 1 - Math.pow(1 - progress, 3);
            const current = Math.round(target * ease);

            el.textContent = current;

            if (progress < 1) {
                requestAnimationFrame(tick);
            }
        };

        requestAnimationFrame(tick);
    });

});
