(function () {
    var cfg = window.paintopsAutoOpenCfg || {};
    var want = cfg.section || '';
    var wantEntry = cfg.entry || '';
    if (!want) { return; }
    var slug = function (t) { return t.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, ''); };
    var scrollTo = null;
    document.querySelectorAll('details.paintops-settings-section > summary').forEach(function (sum) {
        // Summary text may include the right-aligned meta span; the
        // section title is the leading text, so match on prefix.
        if (slug(sum.textContent).indexOf(want) === 0) {
            sum.parentElement.open = true;
            scrollTo = sum;
        }
    });
    if (wantEntry) {
        document.querySelectorAll('details.paintops-settings-entry > summary').forEach(function (sum) {
            if (slug(sum.textContent).indexOf(wantEntry) === 0) {
                sum.parentElement.open = true;
                var section = sum.parentElement.closest('details.paintops-settings-section');
                if (section) { section.open = true; }
                scrollTo = sum;
            }
        });
    }
    if (scrollTo) { scrollTo.scrollIntoView({ behavior: 'smooth', block: 'start' }); }
})();
