(function(){
  function buildNav(){
    const nav = document.getElementById('pwa_nav');
    if (!nav) return;
    const role = (localStorage.getItem('role')||'Guest').toLowerCase();
    let items = [{id:'dashboard',label:'Home'},{id:'customers',label:'Customers'},{id:'tasks',label:'Tasks'},{id:'followups',label:'Followups'},{id:'me',label:'Me'}];
    if (role==='captain') { items = [{id:'dashboard',label:'Home'},{id:'customers',label:'Customers'},{id:'team',label:'Team'},{id:'tasks',label:'Tasks'},{id:'me',label:'Me'}]; }
    else if (role==='statehead'){ items = [{id:'dashboard',label:'Home'},{id:'kpi',label:'KPI'},{id:'customers',label:'Customers'},{id:'me',label:'Me'}]; }
    else if (role==='admin'){ items = [{id:'dashboard',label:'Dashboard'},{id:'customers',label:'Customers'},{id:'tasks',label:'Tasks'},{id:'me',label:'Me'}]; }
    nav.innerHTML='';
    items.forEach(it=>{ const b=document.createElement('button'); b.textContent=it.label; b.onclick = ()=>{ location.hash = '#'+it.id; }; nav.appendChild(b); });
  }
  document.addEventListener('DOMContentLoaded', buildNav);
  window.addEventListener('storage', buildNav);
})();