(function(){
  function ready(fn){if(document.readyState!=='loading')fn();else document.addEventListener('DOMContentLoaded',fn);}
  ready(function(){
    var qtyEl=document.querySelector('[data-qty]'); var qty=1;
    var addBtn=document.querySelector('.js-add-qty');
    var base=addBtn?parseFloat((document.querySelector('[data-line-price]')||{}).textContent.replace('$',''))||0:0;
    function refresh(){if(qtyEl)qtyEl.textContent=qty;var lp=document.querySelector('[data-line-price]');if(lp)lp.textContent=IGET.money(base*qty);}
    var dec=document.querySelector('.js-qty-dec'),inc=document.querySelector('.js-qty-inc');
    if(dec)dec.onclick=function(){qty=Math.max(1,qty-1);refresh();};
    if(inc)inc.onclick=function(){qty+=1;refresh();};
    if(addBtn)addBtn.onclick=function(){IGET.addToCart(addBtn.getAttribute('data-id'),qty);};
    document.querySelectorAll('[data-tab]').forEach(function(btn){
      btn.addEventListener('click',function(){
        document.querySelectorAll('[data-tab]').forEach(function(b){b.classList.remove('active');});
        btn.classList.add('active');
        var t=btn.getAttribute('data-tab');
        document.querySelectorAll('[data-tabpanel]').forEach(function(p){p.hidden=p.getAttribute('data-tabpanel')!==t;});
      });
    });
  });
})();
