
(function(){
  function ensureLoader(){
    if(document.getElementById('xcvpnLoading')) return;
    var d=document.createElement('div');
    d.id='xcvpnLoading';
    d.innerHTML='<div class="xcvpn-loader-box"><div class="xcvpn-spinner"></div><b>Installing...</b><br><small>Please wait. Installing… Please wait.</small></div>';
    document.body.appendChild(d);
  }
  window.xcvpnShowLoading=function(){
    ensureLoader();
    document.getElementById('xcvpnLoading').classList.add('show');
  };
  window.xcvpnCopy=function(text){
    if(!text) return;
    if(navigator.clipboard){navigator.clipboard.writeText(text);}
    else{
      var t=document.createElement('textarea'); t.value=text;
      document.body.appendChild(t); t.select(); document.execCommand('copy'); t.remove();
    }
  };
  document.addEventListener('submit',function(e){
    var f=e.target;
    if(f && (f.classList.contains('xcvpn-long-job') || f.dataset.loading==='1')){
      window.xcvpnShowLoading();
    }
  },true);
  document.addEventListener('DOMContentLoaded',function(){
    // Prevent old Bootstrap backdrop weirdness from blocking visible buttons after modal close
    document.body.addEventListener('hidden.bs.modal',function(){
      document.querySelectorAll('.modal-backdrop').forEach(function(b){b.remove();});
      document.body.classList.remove('modal-open');
      document.body.style.paddingRight='';
    });
  });
})();
