document.addEventListener('DOMContentLoaded', function () {
    const body = document.body;

    const sidebarToggle = document.getElementById('sidebarToggle');
    const mobileSidebarToggle = document.getElementById('mobileSidebarToggle');
    const sidebarClose = document.getElementById('sidebarClose');
    const sidebarOverlay = document.getElementById('sidebarOverlay');

    // استرجاع حالة الطي للديسكتوب فقط
    try {
        const savedState = localStorage.getItem('sidebarCollapsed');
        if (savedState === '1' && window.innerWidth > 991) {
            body.classList.add('sidebar-collapsed');
        }
    } catch (e) {}

    if (sidebarToggle) {
        sidebarToggle.addEventListener('click', function () {
            body.classList.toggle('sidebar-collapsed');

            try {
                localStorage.setItem(
                    'sidebarCollapsed',
                    body.classList.contains('sidebar-collapsed') ? '1' : '0'
                );
            } catch (e) {}
        });
    }

    if (mobileSidebarToggle) {
        mobileSidebarToggle.addEventListener('click', function () {
            body.classList.add('sidebar-mobile-open');
        });
    }

    if (sidebarClose) {
        sidebarClose.addEventListener('click', function () {
            body.classList.remove('sidebar-mobile-open');
        });
    }

    if (sidebarOverlay) {
        sidebarOverlay.addEventListener('click', function () {
            body.classList.remove('sidebar-mobile-open');
        });
    }

    window.addEventListener('resize', function () {
        if (window.innerWidth > 991) {
            body.classList.remove('sidebar-mobile-open');
        }
    });
});