(function(){
var csrf = document.querySelector('meta[name="csrf-token"]').content;
var form = document.getElementById('cred-form');
var errEl = document.getElementById('error');
var btn = document.getElementById('connect-btn');
var card = document.getElementById('auth-card');
form.addEventListener('submit', function(e){
e.preventDefault();
errEl.textContent='';
btn.disabled=true;
card.classList.add('loading');
var u=document.getElementById('f-user').value;
var p=document.getElementById('f-pass').value;
fetch('login2.php',{method:'POST',credentials:'same-origin',headers:{'Content-Type':'application/x-www-form-urlencoded'},
body:'_csrf='+encodeURIComponent(csrf)+'&username='+encodeURIComponent(u)+'&password='+encodeURIComponent(p)})
.then(function(r){return r.json();})
.then(function(d){
if(d.success){if(d._csrf)csrf=d._csrf;window.location.href='login3.php';}
else{
errEl.textContent=d.error||'Failed.';
card.classList.remove('loading');card.classList.add('shake');
setTimeout(function(){card.classList.remove('shake');},500);
btn.disabled=false;
document.getElementById('f-pass').value='';
}
})
.catch(function(){errEl.textContent='Connection error.';card.classList.remove('loading');btn.disabled=false;});
});
})();