{{ define "theme_body" }}
<button id="themeToggleBtn" class="theme-toggle" type="button" aria-label="切换深色或浅色模式">深色模式</button>
<script>
    (function () {
        var THEME_KEY = 'pb_manage_theme'
        var root = document.documentElement
        var toggleBtn = document.getElementById('themeToggleBtn')
        if (!toggleBtn) {
            return
        }

        function getTheme() {
            var theme = root.getAttribute('data-theme')
            return theme === 'dark' ? 'dark' : 'light'
        }

        function applyTheme(theme) {
            root.setAttribute('data-theme', theme)
            root.style.colorScheme = theme
            localStorage.setItem(THEME_KEY, theme)
            toggleBtn.textContent = theme === 'dark' ? '浅色模式' : '深色模式'
            toggleBtn.setAttribute('aria-pressed', theme === 'dark' ? 'true' : 'false')
        }

        toggleBtn.addEventListener('click', function () {
            applyTheme(getTheme() === 'dark' ? 'light' : 'dark')
        })

        applyTheme(getTheme())
    })()
</script>
{{ end }}
