(function(){
  var drawer=document.getElementById('sideDrawer'), back=document.getElementById('drawerBackdrop');
  function openD(){ if(drawer){drawer.classList.add('open'); if(back) back.classList.add('show');} }
  function closeD(){ if(drawer){drawer.classList.remove('open'); if(back) back.classList.remove('show');} }
  ['catMenuBtn','mobileMenuBtn'].forEach(function(id){ var b=document.getElementById(id); if(b)b.addEventListener('click',openD); });
  var dc=document.getElementById('drawerClose'); if(dc)dc.addEventListener('click',closeD); if(back)back.addEventListener('click',closeD);

  var langDrop=document.getElementById('languageDropdown');
  if(langDrop){
    var btn=langDrop.querySelector('.language-current');
    if(btn) btn.addEventListener('click',function(e){
      e.stopPropagation();
      if(langDrop.tagName && langDrop.tagName.toLowerCase()==='details'){
        /* details element opens itself without JS; this line keeps old CSS compatibility */
        setTimeout(function(){
          if(langDrop.hasAttribute('open')) langDrop.classList.add('open');
          else langDrop.classList.remove('open');
        }, 0);
      } else {
        langDrop.classList.toggle('open');
      }
    });
    var menu=langDrop.querySelector('.language-menu');
    if(menu) menu.addEventListener('click',function(e){ e.stopPropagation(); });
    document.addEventListener('click',function(){
      langDrop.classList.remove('open');
      if(langDrop.tagName && langDrop.tagName.toLowerCase()==='details') langDrop.removeAttribute('open');
    });
    document.addEventListener('keydown',function(e){
      if(e.key==='Escape'){
        langDrop.classList.remove('open');
        if(langDrop.tagName && langDrop.tagName.toLowerCase()==='details') langDrop.removeAttribute('open');
      }
    });
  }

  var si=document.getElementById('smartSearchInput'), pi=0;
  if(si && !si.value){
    var raw=si.getAttribute('data-placeholders')||'Search fans|Search TV|Search mixer|Search laptop';
    var placeholders=raw.split('|');
    setInterval(function(){ pi=(pi+1)%placeholders.length; si.setAttribute('placeholder', placeholders[pi]); },1500);
  }

  var slides=document.querySelectorAll('.hero-slide'), dots=document.querySelectorAll('.hero-dots button'), idx=0;
  function show(i){ if(!slides.length)return; slides[idx].classList.remove('active'); slides[idx].classList.add('leaving'); if(dots[idx])dots[idx].classList.remove('active'); idx=i; slides[idx].classList.remove('leaving'); slides[idx].classList.add('active'); if(dots[idx])dots[idx].classList.add('active'); setTimeout(function(){slides.forEach(function(s,n){if(n!==idx)s.classList.remove('leaving');});},500); }
  if(slides.length>1){ dots.forEach(function(d,i){ d.addEventListener('click',function(){show(i);}); }); setInterval(function(){show((idx+1)%slides.length);},1000); }
})();
$(document).on('click','.add-to-cart-btn',function(e){
  e.preventDefault(); var pid=$(this).data('id');
  $.post('add_to_cart.php',{product_id:pid},function(res){
    if(res && res.status==='ok'){
      $('#cartCountTop,#cartCountFloat,#cartCountMobile').text(res.count);
      $('#toast').text('تمت إضافة المنتج إلى السلة').fadeIn(160); setTimeout(function(){ $('#toast').fadeOut(180); },1300);
    } else {
      $('#toast').text('تعذر إضافة المنتج').fadeIn(160); setTimeout(function(){ $('#toast').fadeOut(180); },1300);
    }
  },'json').fail(function(){ $('#toast').text('خطأ اتصال بالسلة').fadeIn(160); setTimeout(function(){ $('#toast').fadeOut(180); },1300); });
});
