(() => {
  const $ = (s, root=document) => root.querySelector(s);
  const $$ = (s, root=document) => [...root.querySelectorAll(s)];
  window.TibaLeta = {
    geolocate(latTarget='latitude', lngTarget='longitude', statusTarget='location-status') {
      const status = document.getElementById(statusTarget);
      if (!navigator.geolocation) { if(status) status.textContent='Location is not supported by this browser.'; return; }
      if(status) status.textContent='Getting your location…';
      navigator.geolocation.getCurrentPosition(({coords}) => {
        const lat=document.getElementById(latTarget), lng=document.getElementById(lngTarget);
        if(lat) lat.value=coords.latitude.toFixed(7); if(lng) lng.value=coords.longitude.toFixed(7);
        if(status) status.textContent='Location added securely for this request.';
      }, () => { if(status) status.textContent='We could not access your location. You can enter your address manually.'; }, {enableHighAccuracy:true, timeout:10000, maximumAge:300000});
    },
    toggleSidebar(){ const el=document.getElementById('portal-sidebar'); if(el) el.classList.toggle('open'); },
    closeSidebar(){ const el=document.getElementById('portal-sidebar'); if(el) el.classList.remove('open'); }
  };
  $$('[data-menu-toggle]').forEach(btn=>btn.addEventListener('click',()=>TibaLeta.toggleSidebar()));
  $$('[data-dismiss]').forEach(btn=>btn.addEventListener('click',()=>btn.closest('[role=alert]')?.remove()));
})();
