document.addEventListener('DOMContentLoaded', function () {
    var toggle = document.getElementById('menuToggle');
    var sidebar = document.getElementById('sidebar');
    var overlay = document.getElementById('sidebarOverlay');

    function closeSidebar() {
        if (!sidebar) return;
        sidebar.classList.remove('open');
        if (overlay) overlay.classList.remove('visible');
        document.body.classList.remove('sidebar-open');
    }

    function openSidebar() {
        if (!sidebar) return;
        sidebar.classList.add('open');
        if (overlay) overlay.classList.add('visible');
        document.body.classList.add('sidebar-open');
    }

    if (toggle && sidebar) {
        toggle.addEventListener('click', function () {
            if (sidebar.classList.contains('open')) {
                closeSidebar();
            } else {
                openSidebar();
            }
        });
    }

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

    document.querySelectorAll('.tab-btn').forEach(function (btn) {
        btn.addEventListener('click', function () {
            var target = btn.getAttribute('data-tab');
            if (!target) return;
            document.querySelectorAll('.tab-btn').forEach(function (b) {
                b.classList.toggle('active', b.getAttribute('data-tab') === target);
            });
            document.querySelectorAll('.tab-panel').forEach(function (panel) {
                panel.classList.toggle('active', panel.id === 'tab-' + target);
            });
        });
    });

    window.copyDeviceId = function () {
        var el = document.getElementById('deviceIdCopy');
        if (!el) return;
        var text = el.textContent.trim();
        if (navigator.clipboard && navigator.clipboard.writeText) {
            navigator.clipboard.writeText(text).then(function () {
                alert('ID copiado com sucesso.');
            });
        } else {
            prompt('Copie o ID:', text);
        }
    };
});
