{{-- Open menus mount on document.body (not inside overflow-hidden table wrappers). Fixed+z-index only while floating. --}}
<style>
    [data-admin-fixed-action-menu] {
        min-width: 12rem;
        max-width: min(100vw - 1rem, 20rem);
    }
    [data-admin-fixed-action-menu][data-admin-fixed-menu-floating="1"] {
        position: fixed !important;
        z-index: 2147483647 !important;
        pointer-events: auto !important;
        margin: 0 !important;
        /* Top layer above cards, sticky header (z-40), modals */
        box-shadow: 0 25px 50px -12px rgb(0 0 0 / 0.25);
    }
</style>
<script>
(function () {
    if (window.__adminFixedActionMenuReady) {
        return;
    }
    window.__adminFixedActionMenuReady = true;

    /** Single open menu (for reposition on scroll/resize without closing). */
    var adminFixedMenuOpenState = { id: null, button: null };

    function adminFixedActionMenuStripPositioning(menu) {
        if (!menu) {
            return;
        }
        menu.removeAttribute('data-admin-fixed-menu-floating');
        ['position', 'z-index', 'top', 'left', 'right', 'bottom', 'pointer-events'].forEach(function (p) {
            menu.style.removeProperty(p);
        });
    }

    /** Reparent to document.body so menus escape overflow-hidden / stacking on table cards (#main-content). */
    function adminFixedActionMenuDetachToBody(menu) {
        if (!menu || menu.dataset.adminFixedMenuInBody === '1') {
            return;
        }
        menu._adminFixedMenuParent = menu.parentNode;
        menu._adminFixedMenuNext = menu.nextSibling;
        document.body.appendChild(menu);
        menu.dataset.adminFixedMenuInBody = '1';
    }

    function adminFixedActionMenuRestoreDom(menu) {
        if (!menu || menu.dataset.adminFixedMenuInBody !== '1') {
            return;
        }
        adminFixedActionMenuStripPositioning(menu);
        var p = menu._adminFixedMenuParent;
        var n = menu._adminFixedMenuNext;
        if (p) {
            try {
                if (n && n.parentNode === p) {
                    p.insertBefore(menu, n);
                } else {
                    p.appendChild(menu);
                }
            } catch (e) {
                p.appendChild(menu);
            }
        }
        delete menu._adminFixedMenuParent;
        delete menu._adminFixedMenuNext;
        delete menu.dataset.adminFixedMenuInBody;
    }

    function adminFixedActionMenuResolveButton(menuId, event) {
        var btn = document.querySelector('[data-admin-fixed-menu-trigger="' + menuId + '"]');
        if (btn) {
            return btn;
        }
        if (event && event.currentTarget) {
            return event.currentTarget;
        }
        return null;
    }

    window.adminFixedActionMenuCloseAll = function () {
        adminFixedMenuOpenState.id = null;
        adminFixedMenuOpenState.button = null;
        document.querySelectorAll('[data-admin-fixed-action-menu]').forEach(function (menu) {
            menu.classList.add('hidden');
            adminFixedActionMenuStripPositioning(menu);
            adminFixedActionMenuRestoreDom(menu);
        });
    };

    window.adminFixedActionMenuPosition = function (menu, button) {
        requestAnimationFrame(function () {
            if (!menu || !button || menu.classList.contains('hidden')) {
                return;
            }
            var rect = button.getBoundingClientRect();
            var margin = 6;
            var mw = menu.offsetWidth || 192;
            var left = rect.right - mw;
            if (left < margin) {
                left = margin;
            }
            if (left + mw > window.innerWidth - margin) {
                left = Math.max(margin, window.innerWidth - mw - margin);
            }
            var top = rect.bottom + margin;
            var mh = menu.offsetHeight || 1;
            if (top + mh > window.innerHeight - margin && rect.top > mh + margin) {
                top = rect.top - mh - margin;
            }
            if (top < margin) {
                top = margin;
            }
            menu.style.setProperty('top', top + 'px', 'important');
            menu.style.setProperty('left', left + 'px', 'important');
        });
    };

    window.adminFixedActionMenuToggle = function (event, menuId) {
        var ev = event || window.event;
        if (ev && ev.stopPropagation) {
            ev.stopPropagation();
        }
        var menu = document.getElementById(menuId);
        var btn = adminFixedActionMenuResolveButton(menuId, ev);
        if (!menu) {
            return;
        }
        var wasHidden = menu.classList.contains('hidden');
        document.querySelectorAll('[data-admin-fixed-action-menu]').forEach(function (m) {
            if (m.id !== menuId) {
                m.classList.add('hidden');
                adminFixedActionMenuStripPositioning(m);
                adminFixedActionMenuRestoreDom(m);
            }
        });
        if (!wasHidden) {
            menu.classList.add('hidden');
            adminFixedActionMenuStripPositioning(menu);
            adminFixedActionMenuRestoreDom(menu);
            adminFixedMenuOpenState.id = null;
            adminFixedMenuOpenState.button = null;
            return;
        }
        adminFixedActionMenuDetachToBody(menu);
        menu.setAttribute('data-admin-fixed-menu-floating', '1');
        menu.style.setProperty('position', 'fixed', 'important');
        menu.style.setProperty('z-index', '2147483647', 'important');
        menu.style.setProperty('pointer-events', 'auto', 'important');
        menu.classList.remove('hidden');
        adminFixedMenuOpenState.id = menuId;
        adminFixedMenuOpenState.button = btn;
        /* layout: hidden used display:none — need dimensions before first position */
        void menu.offsetWidth;
        if (btn) {
            window.adminFixedActionMenuPosition(menu, btn);
            requestAnimationFrame(function () {
                window.adminFixedActionMenuPosition(menu, btn);
                requestAnimationFrame(function () {
                    window.adminFixedActionMenuPosition(menu, btn);
                });
            });
        }
    };

    document.addEventListener('click', function (e) {
        if (e.target.closest('[data-admin-fixed-menu-trigger]')) {
            return;
        }
        if (e.target.closest('[data-admin-fixed-action-menu]')) {
            if (e.target.closest('a[href], button')) {
                setTimeout(function () {
                    window.adminFixedActionMenuCloseAll();
                }, 150);
            }
            return;
        }
        window.adminFixedActionMenuCloseAll();
    }, true);

    function adminFixedActionMenuRepositionOpen() {
        var id = adminFixedMenuOpenState.id;
        var btn = adminFixedMenuOpenState.button;
        if (!id || !btn) {
            return;
        }
        var menu = document.getElementById(id);
        if (!menu || menu.classList.contains('hidden')) {
            return;
        }
        window.adminFixedActionMenuPosition(menu, btn);
    }

    window.addEventListener('resize', function () {
        adminFixedActionMenuRepositionOpen();
    });

    function attachMainScrollReposition() {
        var main = document.querySelector('#main-content main');
        if (main) {
            main.addEventListener('scroll', adminFixedActionMenuRepositionOpen, { passive: true });
        }
    }
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', attachMainScrollReposition);
    } else {
        attachMainScrollReposition();
    }
})();
</script>
