(function () {
    if (window.__MELON_LITE_SCROLL_TOP_INITED) return;
    window.__MELON_LITE_SCROLL_TOP_INITED = true;

    const SHOW_AFTER = 360;
    let ticking = false;

    function scrollCandidates() {
        return [
            document.querySelector('.content-body'),
            document.querySelector('.main-content'),
            document.querySelector('.app-container'),
            document.scrollingElement || document.documentElement,
            document.documentElement,
            document.body
        ].filter(Boolean);
    }

    function getScrollContainer() {
        const candidates = scrollCandidates();
        for (const node of candidates) {
            if (node && node.scrollHeight > node.clientHeight + 8 && node.scrollTop > 0) return node;
        }
        return document.scrollingElement || document.documentElement;
    }

    function currentScrollTop() {
        const values = scrollCandidates().map(function (node) {
            return node ? Number(node.scrollTop || 0) : 0;
        });
        values.push(window.pageYOffset || 0);
        return Math.max.apply(Math, values);
    }

    function updateButton() {
        ticking = false;
        const btn = document.getElementById('scroll-top-btn');
        if (!btn) return;
        const visible = currentScrollTop() > SHOW_AFTER && !document.body.classList.contains('expand-player-open');
        btn.hidden = !visible;
        btn.classList.toggle('is-visible', visible);
    }

    function requestUpdate() {
        if (ticking) return;
        ticking = true;
        window.requestAnimationFrame(updateButton);
    }

    function scrollToTop() {
        const behavior = window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches ? 'auto' : 'smooth';
        scrollCandidates().forEach(function (node) {
            if (node && node.scrollTop > 0 && typeof node.scrollTo === 'function') {
                node.scrollTo({ top: 0, behavior });
            }
        });
        window.scrollTo({ top: 0, behavior });
        requestUpdate();
    }

    function bindScrollTopButton() {
        const btn = document.getElementById('scroll-top-btn');
        if (!btn || btn.dataset.scrollTopBound === '1') return;
        btn.dataset.scrollTopBound = '1';
        btn.addEventListener('click', function (event) {
            event.preventDefault();
            scrollToTop();
        });
    }

    function bindScrollSources() {
        if (document.documentElement.dataset.scrollTopSourcesBound === '1') return;
        document.documentElement.dataset.scrollTopSourcesBound = '1';
        window.addEventListener('scroll', requestUpdate, { passive: true });
        window.addEventListener('resize', requestUpdate, { passive: true });
        document.addEventListener('scroll', requestUpdate, { passive: true, capture: true });
    }

    function initScrollTop() {
        bindScrollTopButton();
        bindScrollSources();
        requestUpdate();
    }

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', initScrollTop);
    } else {
        initScrollTop();
    }
    document.addEventListener('melonlite:page-ready', initScrollTop);
})();
