<?php
/**
 * Countdown timer — place RIGHT AFTER the timer HTML (before other scripts).
 * Uses $timerSeconds when set. Vanilla JS only.
 */
$__timerSec = isset($timerSeconds) ? (int) $timerSeconds : 59;
if ($__timerSec < 1) {
    $__timerSec = 59;
}
$__mm = str_pad((string) floor($__timerSec / 60), 2, '0', STR_PAD_LEFT);
$__ss = str_pad((string) ($__timerSec % 60), 2, '0', STR_PAD_LEFT);
?>
<script>
(function () {
    try {
        var total = <?= $__timerSec ?>;
        var left = total;
        var circle = document.getElementById('timerProgress');
        var label = document.getElementById('timerText');
        var C = 2 * Math.PI * 52;

        function pad(n) { return (n < 10 ? '0' : '') + n; }

        function paint() {
            if (label) label.textContent = pad(Math.floor(left / 60)) + ':' + pad(left % 60);
            if (!circle) return;
            var off = C * (1 - (left / total));
            circle.setAttribute('stroke-dasharray', C);
            circle.setAttribute('stroke-dashoffset', off);
        }

        if (label && !label.textContent) label.textContent = '<?= $__mm ?>:<?= $__ss ?>';
        paint();

        setInterval(function () {
            if (left > 0) left -= 1;
            paint();
        }, 1000);
    } catch (e) {}
})();
</script>
