document.addEventListener('DOMContentLoaded',()=>{
 document.querySelectorAll('[data-accordion]').forEach(button=>{
   button.addEventListener('click',()=>{
     const item=button.closest('.accordion__item');
     const group=item.parentElement;
     group.querySelectorAll('.accordion__item').forEach(other=>{
       if(other!==item) other.classList.remove('is-open');
     });
     item.classList.toggle('is-open');
   });
 });
 document.querySelectorAll('[data-speak]').forEach(button=>{
   button.addEventListener('click',()=>{
     const word=button.dataset.speak||'';
     const audioPath=button.dataset.audio||'';
     if(audioPath){
       const audio=new Audio(audioPath);
       audio.play().catch(()=>speak(word));
     }else speak(word);
   });
 });
 const search=document.querySelector('[data-word-search]');
 if(search){
   search.addEventListener('input',()=>{
     const term=search.value.trim().toLowerCase();
     document.querySelectorAll('[data-word-card]').forEach(card=>{
       card.style.display=(card.dataset.search||'').includes(term)?'grid':'none';
     });
   });
 }
});
function speak(word){
 if(!('speechSynthesis' in window)){alert('Trình duyệt không hỗ trợ đọc từ.');return;}
 speechSynthesis.cancel();
 const u=new SpeechSynthesisUtterance(word);
 u.lang='en-US';u.rate=.72;
 speechSynthesis.speak(u);
}
