// ─── Sidebar ─── function initSidebar() { document.querySelectorAll('.ni[data-pg]').forEach(el => { el.addEventListener('click', function(e) { e.preventDefault(); window.location.href = this.getAttribute('href'); }); }); document.querySelectorAll('.nst').forEach(el => { el.addEventListener('click', function() { this.classList.toggle('on'); }); }); } initSidebar(); // ─── Modal Helpers ─── function openModal(id) { const el = document.getElementById(id); if (el) el.classList.add('open'); } function closeModal(id) { const el = document.getElementById(id); if (el) el.classList.remove('open'); } document.addEventListener('keydown', function(e) { if (e.key !== 'Escape') return; document.querySelectorAll('.modal-bg.open').forEach(el => el.classList.remove('open')); }); // ─── Global Time Control ─── function openTimeModal() { const display = document.getElementById('sim-time-display'); if (!display) return; const modal = document.getElementById('time-modal'); const input = document.getElementById('time-input'); if (modal && input) { input.value = display.textContent.trim().replace(' ', 'T'); modal.classList.add('open'); } } function closeTimeModal() { const modal = document.getElementById('time-modal'); if (modal) modal.classList.remove('open'); } async function applyTime() { const input = document.getElementById('time-input'); if (!input || !input.value) return; await setSimTimeAndReload(input.value.replace('T', ' ') + ':00'); } async function shiftTime(days) { const display = document.getElementById('sim-time-display'); if (!display) return; const d = new Date(display.textContent.trim().replace(' ', 'T') + 'Z'); d.setUTCDate(d.getUTCDate() + days); await setSimTimeAndReload(d.toISOString().slice(0, 10) + ' 09:00'); } async function resetTime() { await setSimTimeAndReload('2060-03-12 09:00'); } function toggleTheme() { const root = document.documentElement; const current = root.dataset.theme || 'dark'; const next = current === 'dark' ? 'light' : 'dark'; root.dataset.theme = next; root.style.colorScheme = next; try { localStorage.setItem('ksp-theme', next); } catch(e) {} } async function setSimTimeAndReload(timeStr) { try { await fetch('/api/v1/sim-time', { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({sim_time: timeStr}) }); } catch(e) {} location.reload(); }