function formatTime(seconds) { return `00:${seconds.toString().padStart(2, '0')}`; } function startTimer() { if (isRunning) return; isRunning = true; startButton.textContent = 'Running...'; startButton.style.background = '#27ae60'; timerId = setInterval(() => { timeLeft--; timerDisplay.textContent = formatTime(timeLeft); if (timeLeft <= 0) { clearInterval(timerId); if (soundEnabled) { audio.play(); } resetTimer(); } }, 1000); } function resetTimer() { clearInterval(timerId); timeLeft = 10; isRunning = false; timerDisplay.textContent = formatTime(timeLeft); startButton.textContent = 'Start'; startButton.style.background = '#2ecc71'; } function toggleSound() { soundEnabled = !soundEnabled; soundButton.innerHTML = soundEnabled ? '🔊 Sound On' : '🔇 Sound Off'; soundButton.style.background = soundEnabled ? '#3498db' : '#95a5a6'; } // Handle keyboard shortcuts document.addEventListener('keydown', (e) => { if (e.code === 'Space') { e.preventDefault(); if (isRunning) { resetTimer(); } else { startTimer(); } } }); // Handle visibility change document.addEventListener('visibilitychange', () => { if (document.hidden && isRunning) { resetTimer(); } }); // Mobile touch feedback document.querySelectorAll('button').forEach(button => { button.addEventListener('touchstart', () => { button.style.transform = 'scale(0.95)'; }); button.addEventListener('touchend', () => { button.style.transform = 'scale(1)'; }); });