const statusEl = document.getElementById('status');
const btn = document.getElementById('btn-request');

btn.addEventListener('click', () => {
    statusEl.textContent = 'Requesting permission...';
    btn.disabled = true;

    navigator.mediaDevices.getUserMedia({ audio: true })
        .then(stream => {
            stream.getTracks().forEach(track => track.stop());
            statusEl.textContent = '🎉 Permission Granted! You can now close this tab and talk to Pidgey via the sidebar mic.';
            statusEl.className = 'status success';
            btn.style.display = 'none';
        })
        .catch(err => {
            console.error("Mic error:", err);
            statusEl.innerHTML = '❌ <b>Permission denied.</b><br><br>If you see a lock icon (🔒) in the URL bar, click it and set Microphone to "Allow".';
            statusEl.className = 'status error';
            btn.disabled = false;
        });
});
