// Authentication UI management let pendingAction = null; // Initialize auth UI when DOM is loaded document.addEventListener('DOMContentLoaded', function() { initAuthUI(); updateNavbar(); }); // Initialize authentication UI function initAuthUI() { // Modal functionality const modal = document.getElementById('loginModal'); const closeBtn = document.getElementById('modalClose'); const demoBtn = document.getElementById('demoLoginBtn'); if (closeBtn) { closeBtn.addEventListener('click', closeModal); } if (demoBtn) { demoBtn.addEventListener('click', handleDemoLogin); } // Close modal on backdrop click if (modal) { modal.addEventListener('click', (e) => { if (e.target === modal) { closeModal(); } }); } // Close modal on Escape key document.addEventListener('keydown', (e) => { if (e.key === 'Escape' && isModalOpen()) { closeModal(); } }); } // Show login modal function showLoginModal(action = null) { pendingAction = action; const modal = document.getElementById('loginModal'); if (modal) { modal.style.display = 'flex'; document.body.style.overflow = 'hidden'; // Focus management const focusableElements = modal.querySelectorAll('button, a'); if (focusableElements.length > 0) { focusableElements[0].focus(); } } } // Close login modal function closeModal() { const modal = document.getElementById('loginModal'); if (modal) { modal.style.display = 'none'; document.body.style.overflow = ''; pendingAction = null; } } // Check if modal is open function isModalOpen() { const modal = document.getElementById('loginModal'); return modal && modal.style.display === 'flex'; } // Handle demo login from modal async function handleDemoLogin() { try { const result = await auth.loginAsDemo(); if (result.success) { auth.showToast(`Welcome, ${result.user.name}!`, 'success'); closeModal(); updateNavbar(); // Execute pending action if any if (pendingAction && typeof pendingAction === 'function') { pendingAction(); } } else { auth.showToast(result.error || 'Demo login failed', 'error'); } } catch (error) { auth.showToast('An error occurred. Please try again.', 'error'); } } // Update navbar based on auth state function updateNavbar() { const navActions = document.querySelector('.nav-actions'); if (!navActions) return; if (auth.isAuthenticated()) { const user = auth.getUser(); // Create user dropdown navActions.innerHTML = `