(function () {
    // Sort search table
    var sel = document.getElementById('stl-sort');
    var tbl = document.getElementById('stl-search-table');
    if (sel && tbl) {
        sel.addEventListener('change', function () {
            var rows = Array.from(tbl.querySelectorAll('tbody tr'));
            var dir = this.value;
            rows.sort(function (a, b) {
                var ia = parseInt(a.dataset.idx), ib = parseInt(b.dataset.idx);
                var ra = parseInt(a.dataset.results), rb = parseInt(b.dataset.results);
                switch (dir) {
                    case 'time-desc': return ia - ib;
                    case 'time-asc': return ib - ia;
                    case 'results-desc': return rb - ra || ia - ib;
                    case 'results-asc': return ra - rb || ia - ib;
                    default: return 0;
                }
            });
            rows.forEach(function (r) { tbl.querySelector('tbody').appendChild(r); });
        });
    }
})();

function stlCopy(id, btn) {
    var el = document.getElementById(id);
    navigator.clipboard.writeText(el.textContent).then(function () {
        btn.classList.add('stl-copy-btn--copied');
        setTimeout(function () {
            btn.classList.remove('stl-copy-btn--copied');
        }, 1500);
    });
}
