<?php /* studentlayout_bottom.php — closes wrappers + theme & drawer logic */ ?>
      </div><!-- /.wrap -->
    </section><!-- /.main -->
  </div><!-- /.eden-shell -->
</div><!-- /.eden-portal -->

<script>
  // Theme toggle (cookie: edenTheme)
  (function(){
    const root = document.getElementById('edenPortal');
    const btn  = document.getElementById('themeToggle');
    if (!root || !btn) return;

    const setCookie = (k,v,days)=>{ document.cookie = `${k}=${v};path=/;SameSite=Lax;max-age=${days*86400}`; };
    const getCookie = (k)=> document.cookie.split('; ').find(r=>r.startsWith(k+'='))?.split('=')[1];

    const saved = getCookie('edenTheme');
    if (saved) root.dataset.theme = saved;

    btn.addEventListener('click', ()=>{
      const next = root.dataset.theme === 'dark' ? 'light' : 'dark';
      root.dataset.theme = next;
      setCookie('edenTheme', next, 365);
    });
  }());

  // Drawer overlay (mobile)
  (function(){
    const root = document.getElementById('edenPortal');
    const sidebar = document.getElementById('sidebar');
    const toggle  = document.getElementById('sidebarToggle');
    const backdrop = document.getElementById('drawerBackdrop');

    if (!root || !sidebar || !toggle || !backdrop) return;

    function isMobile(){ return window.matchMedia('(max-width: 992px)').matches; }

    function openDrawer(){
      if (!isMobile()) return;
      root.classList.add('drawer-open');
      toggle.setAttribute('aria-expanded','true');
      backdrop.hidden = false;
      document.body.style.overflow = 'hidden'; // prevent background scroll
    }
    function closeDrawer(){
      root.classList.remove('drawer-open');
      toggle.setAttribute('aria-expanded','false');
      backdrop.hidden = true;
      document.body.style.overflow = '';
    }
    function toggleDrawer(){
      if (!isMobile()){ return; }
      if (root.classList.contains('drawer-open')) closeDrawer(); else openDrawer();
    }

    toggle.addEventListener('click', toggleDrawer);
    backdrop.addEventListener('click', closeDrawer);
    document.addEventListener('keydown', (e)=>{ if(e.key==='Escape') closeDrawer(); });

    // Auto-close when resizing back to desktop
    window.addEventListener('resize', ()=>{ if (!isMobile()) closeDrawer(); }, { passive:true });
  }());

  // Sidebar avatar fallback (show initials if image fails)
  (function(){
    const img = document.getElementById('sideAvatarImg');
    if (!img) return;
    img.addEventListener('error', ()=>{
      const box = document.getElementById('sideAvatarBox');
      if (box){ box.textContent = <?= json_encode($initials) ?>; }
    }, { once:true });
  }());
</script>
</body>
</html>
