document.addEventListener('click',e=>{
  const chip=e.target.closest('.chip');
  if(chip){chip.parentElement.querySelectorAll('.chip').forEach(x=>x.classList.remove('active'));chip.classList.add('active')}
});

(function(){
  const popup=document.getElementById('announcementPopup');
  if(!popup) return;
  const id=popup.dataset.id;
  const frequency=popup.dataset.frequency || 'once_session';
  const today=new Date().toISOString().slice(0,10);
  const sessionKey='nutugu_popup_session_'+id;
  const dayKey='nutugu_popup_day_'+id;
  let shouldShow=true;
  const isMobile=window.matchMedia('(max-width:820px)').matches;
  if(isMobile && popup.dataset.mobile==='0') shouldShow=false;
  if(!isMobile && popup.dataset.desktop==='0') shouldShow=false;
  if(frequency==='once_session' && sessionStorage.getItem(sessionKey)) shouldShow=false;
  if(frequency==='once_day' && localStorage.getItem(dayKey)===today) shouldShow=false;
  if(!shouldShow) popup.classList.add('hidden');
  const close=()=>{
    popup.classList.add('hidden');
    if(frequency==='once_session') sessionStorage.setItem(sessionKey,'1');
    if(frequency==='once_day') localStorage.setItem(dayKey,today);
  };
  popup.querySelector('.popup-close')?.addEventListener('click',close);
  popup.addEventListener('click',e=>{ if(e.target===popup) close(); });
})();
