(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 langDrops=document.querySelectorAll('.language-dropdown');
  if(langDrops && langDrops.length){
    langDrops.forEach(function(langDrop){
      var btn=langDrop.querySelector('.language-current');
      if(btn) btn.addEventListener('click',function(e){
        e.stopPropagation();
        setTimeout(function(){
          if(langDrop.hasAttribute('open')) langDrop.classList.add('open');
          else langDrop.classList.remove('open');
        }, 0);
      });
      var menu=langDrop.querySelector('.language-menu');
      if(menu) menu.addEventListener('click',function(e){ e.stopPropagation(); });
    });
    document.addEventListener('click',function(){
      langDrops.forEach(function(langDrop){
        langDrop.classList.remove('open');
        if(langDrop.tagName && langDrop.tagName.toLowerCase()==='details') langDrop.removeAttribute('open');
      });
    });
    document.addEventListener('keydown',function(e){
      if(e.key==='Escape'){
        langDrops.forEach(function(langDrop){
          langDrop.classList.remove('open');
          if(langDrop.tagName && langDrop.tagName.toLowerCase()==='details') langDrop.removeAttribute('open');
        });
      }
    });
  }

  var searchInputs=document.querySelectorAll('#smartSearchInput,.mobile-search-box input[name="q"]');
  searchInputs.forEach(function(si){
    var 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); });
});
