function showToast(message) {
    var x = document.getElementById("we-ai-toast");
    if(x) {
        x.innerHTML = message;
        x.style.visibility = "visible";
        setTimeout(function(){ x.style.visibility = "hidden"; }, 3000);
    }
}

function runAI() {
    var overlay = document.getElementById('we-ai-overlay');
    var bar = document.getElementById('we-ai-fill-bar');
    
    if(!overlay || !bar) return;

    overlay.style.display = 'flex';
    bar.style.width = "5%";
    
    var progress = 5;
    var interval = setInterval(function() {
        if(progress < 90) {
            progress += Math.random() * 5;
            bar.style.width = progress + "%";
        }
    }, 1000);

    jQuery.post(web_essential_ai_vars.ajax_url, {
        'action': 'we_ai_run_instant',
        'nonce': web_essential_ai_vars.nonce
    }, function(response) {
        clearInterval(interval);
        bar.style.width = "100%";
        
        setTimeout(function() {
            overlay.style.display = 'none';
            if(response.success) {
                showToast("✅ Content Generated Successfully!");
            } else {
                showToast("❌ Error: " + (response.data ? response.data.message : 'Unknown Error'));
            }
        }, 500);
    }).fail(function() {
        clearInterval(interval);
        overlay.style.display = 'none';
        showToast("❌ Connection Failed.");
    });
}
