<script>
    (function () {
        var key = 'basketloot_portal_theme';
        var stored = null;
        try { stored = localStorage.getItem(key); } catch (error) {}
        var theme = stored === 'light' || stored === 'dark' ? stored : 'dark';
        document.documentElement.setAttribute('data-portal-theme', theme);

        window.BasketlootTheme = {
            key: key,
            current: function () { return document.documentElement.getAttribute('data-portal-theme') || 'dark'; },
            apply: function (nextTheme, persist) {
                var next = nextTheme === 'light' ? 'light' : 'dark';
                document.documentElement.setAttribute('data-portal-theme', next);
                document.documentElement.style.colorScheme = next;
                document.querySelectorAll('[data-theme-toggle]').forEach(function (button) {
                    var isLight = next === 'light';
                    button.setAttribute('aria-pressed', isLight ? 'true' : 'false');
                    button.setAttribute('aria-label', isLight ? 'Switch to dark theme' : 'Switch to light theme');
                    button.setAttribute('title', isLight ? 'Switch to dark theme' : 'Switch to light theme');
                    var label = button.querySelector('[data-theme-label]');
                    if (label) label.textContent = isLight ? 'Dark' : 'Light';
                });
                if (persist !== false) {
                    try { localStorage.setItem(key, next); } catch (error) {}
                }
            }
        };

        document.addEventListener('DOMContentLoaded', function () {
            window.BasketlootTheme.apply(window.BasketlootTheme.current(), false);
        });

    })();
</script>
