function formatUptime(seconds) {
    if (seconds < 0) return "0s";

    const days = Math.floor(seconds / (3600 * 24));
    const hours = Math.floor((seconds % (3600 * 24)) / 3600);
    const minutes = Math.floor((seconds % 3600) / 60);
    const secs = Math.floor(seconds % 60);

    const parts = [];
    if (days > 0) parts.push(`${days}d`);
    if (hours > 0) parts.push(`${hours}h`);
    if (minutes > 0) parts.push(`${minutes}m`);
    parts.push(`${secs}s`);

    return parts.join(" ");
}

function updateUptime() {
    const el = document.getElementById("uptime-display");
    if (!el) return;

    const startTimestamp = parseInt(el.getAttribute("data-start-timestamp"), 10);
    if (isNaN(startTimestamp)) return;

    const now = Date.now();
    const elapsedSeconds = (now - startTimestamp) / 1000;

    el.textContent = formatUptime(elapsedSeconds);
}

document.addEventListener("DOMContentLoaded", () => {
    // Update immediately to prevent stale content flash if possible
    updateUptime();
    // Update every second
    setInterval(updateUptime, 1000);
});
