(function(){
  function ready(fn){if(document.readyState!=='loading')fn();else document.addEventListener('DOMContentLoaded',fn);}
  ready(function(){
    var root=document.getElementById('cart-root'); if(!root)return;
    function render(){
      var lines=IGET.cartLines(); var t=IGET.totals();
      if(!lines.length){
        root.innerHTML='<div class="empty-state"><div style="font-size:42px">🛒</div><div class="big">Your cart is empty</div>'+
          '<p>Browse our IGET range and add your favourites.</p><a class="btn btn-cta" style="margin-top:18px" href="/iget-vape-shop.html">Shop now →</a></div>';
        return;
      }
      var items=lines.map(function(l){var sub=(l.puffs?l.puffs.toLocaleString()+' puffs':'Authentic IGET');return '<div class="citem"><div class="ci-img"><img src="'+l.image+'" alt="'+IGET.esc(l.name)+' IGET vape Australia" title="'+IGET.esc(l.name)+' — genuine IGET vape, buy online in Australia" loading="lazy"></div>'+
        '<div><div class="ci-name">'+IGET.esc(l.name)+'</div><div class="ci-flav">'+sub+'</div>'+
        '<div class="price" style="margin-top:6px;color:var(--accent)">'+IGET.money(l.price)+' each</div></div>'+
        '<div class="ci-right"><div class="qty-sm"><button data-dec="'+l.id+'" aria-label="Decrease">−</button><span>'+l.qty+'</span><button data-inc="'+l.id+'" aria-label="Increase">+</button></div>'+
        '<div class="price">'+IGET.money(l.lineTotal)+'</div><button class="ci-remove" data-rm="'+l.id+'">Remove</button></div></div>';}).join('');
      var cp=t.coupon;
      var couponBlock='<div class="coupon-row"><input class="input" id="coupon-code" placeholder="Coupon code" value="'+(cp?IGET.esc(cp.code):'')+'">'+
        (cp?'<button class="btn btn-line btn-sm" id="coupon-clear">Remove</button>':'<button class="btn btn-ghost btn-sm" id="coupon-apply">Apply</button>')+'</div>';
      var ship='<div class="ship-opts">'+IGET.shipOptions().map(function(o){var on=o.id===t.method;return '<label class="ship-opt'+(on?' active':'')+'"><input type="radio" name="ship" value="'+o.id+'"'+(on?' checked':'')+'><span class="so-l">'+o.label+' <span class="muted">('+o.eta+')</span></span><span class="so-p">'+((o.id==='express'&&t.free)?'FREE':IGET.money(o.price))+'</span></label>';}).join('')+'</div>';
      var summary='<div class="summary"><h3>Order Summary</h3>'+IGET.shipBanner(t)+IGET.minBanner(t)+
        '<div class="sline"><span>Subtotal</span><span class="price">'+IGET.money(t.subtotal)+'</span></div>'+
        (t.discount>0?'<div class="sline"><span>Discount'+(cp?' ('+IGET.esc(cp.code)+')':'')+'</span><span class="price" style="color:var(--ok)">-'+IGET.money(t.discount)+'</span></div>':'')+
        '<div class="sc-note ok" style="margin-top:12px">Pay with crypto at checkout for an extra 15% off</div>'+
        '<div style="margin:12px 0 6px;font-weight:700;font-size:12px;text-transform:uppercase;letter-spacing:.04em">Shipping method</div>'+ship+
        '<div class="sline"><span>Shipping</span><span class="price">'+(t.free?'<span style="color:var(--ok)">FREE</span>':IGET.money(t.shipping))+'</span></div>'+
        couponBlock+
        '<div class="sline total"><span>Total</span><span class="price">'+IGET.money(t.total)+'</span></div>'+
        (t.meetsMin?'<a class="btn btn-cta" style="width:100%;margin-top:18px;padding:14px" href="/checkout.html">Proceed to Checkout →</a>'
                   :'<button class="btn btn-cta" disabled style="width:100%;margin-top:18px;padding:14px">Add '+IGET.money(t.remainingForMin)+' to check out</button>')+
        '<a class="btn btn-line" style="width:100%;margin-top:10px" href="/iget-vape-shop.html">Continue shopping</a></div>';
      root.innerHTML='<div class="cart-layout"><div class="cart-items">'+items+'</div>'+summary+'</div>';
      root.querySelectorAll('[data-dec]').forEach(function(b){b.onclick=function(){var id=b.getAttribute('data-dec');var it=IGET.getCart().filter(function(i){return i.id===id;})[0];IGET.setQty(id,(it?it.qty:1)-1);render();};});
      root.querySelectorAll('[data-inc]').forEach(function(b){b.onclick=function(){var id=b.getAttribute('data-inc');var it=IGET.getCart().filter(function(i){return i.id===id;})[0];IGET.setQty(id,(it?it.qty:1)+1);render();};});
      root.querySelectorAll('[data-rm]').forEach(function(b){b.onclick=function(){IGET.removeFromCart(b.getAttribute('data-rm'));render();};});
      root.querySelectorAll('input[name="ship"]').forEach(function(r){r.onchange=function(){IGET.setShip(r.value);render();};});
      var ap=document.getElementById('coupon-apply');if(ap)ap.onclick=function(){if(IGET.applyCoupon(document.getElementById('coupon-code').value))render();};
      var cl=document.getElementById('coupon-clear');if(cl)cl.onclick=function(){IGET.clearCoupon();render();};
    }
    render();
  });
})();
