(function () {
                if (window.__nexdirectoryGlobalAuthModalInit) {
                    return;
                }
                window.__nexdirectoryGlobalAuthModalInit = true;

                var modal = document.getElementById('nexdirectory-global-auth-modal');
                if (!modal) {
                    return;
                }
                var dialog = modal.querySelector('.nexdirectory-global-auth-modal__dialog');
                var tabButtons = Array.prototype.slice.call(modal.querySelectorAll('[data-nexdirectory-auth-tab-trigger]'));
                var panels = Array.prototype.slice.call(modal.querySelectorAll('[data-nexdirectory-auth-panel]'));
                var redirectInputs = Array.prototype.slice.call(modal.querySelectorAll('.nexdirectory-global-auth-redirect'));
                var loginPath = String(modal.getAttribute('data-login-path') || '').trim();
                var claimPath = String(modal.getAttribute('data-claim-path') || '').trim();
                var defaultRedirect = String(modal.getAttribute('data-default-redirect') || window.location.href).trim();
                var closeTimer = null;
                var isOpen = false;

                function normalizePath(pathValue) {
                    var path = String(pathValue || '').trim();
                    if (!path) {
                        return '';
                    }
                    if (path.charAt(0) !== '/') {
                        path = '/' + path;
                    }
                    path = path.replace(/\/{2,}/g, '/');
                    if (path.length > 1) {
                        path = path.replace(/\/+$/, '');
                    }
                    return path;
                }

                loginPath = normalizePath(loginPath);
                claimPath = normalizePath(claimPath);

                function parseSafeUrl(input) {
                    try {
                        var parsed = new URL(String(input || ''), window.location.href);
                        if (parsed.origin !== window.location.origin) {
                            return null;
                        }
                        return parsed;
                    } catch (error) {
                        return null;
                    }
                }

                function setTab(nextTab) {
                    var allowed = ['login', 'signup', 'forgot'];
                    var tab = allowed.indexOf(String(nextTab || '').trim()) >= 0 ? String(nextTab || '').trim() : 'login';
                    tabButtons.forEach(function (button) {
                        var key = String(button.getAttribute('data-nexdirectory-auth-tab-trigger') || '').trim();
                        var active = key === tab;
                        button.classList.toggle('is-active', active);
                        button.setAttribute('aria-selected', active ? 'true' : 'false');
                        button.setAttribute('tabindex', active ? '0' : '-1');
                    });
                    panels.forEach(function (panel) {
                        var key = String(panel.getAttribute('data-nexdirectory-auth-panel') || '').trim();
                        if (key === tab) {
                            panel.removeAttribute('hidden');
                        } else {
                            panel.setAttribute('hidden', 'hidden');
                        }
                    });
                }

                function setRedirect(redirectTo) {
                    var parsed = parseSafeUrl(redirectTo);
                    var value = parsed ? parsed.toString() : window.location.href;
                    redirectInputs.forEach(function (input) {
                        input.value = value;
                    });
                }

                function openModal(tab, redirectTo) {
                    window.clearTimeout(closeTimer);
                    setTab(tab);
                    setRedirect(redirectTo || defaultRedirect);
                    modal.hidden = false;
                    requestAnimationFrame(function () {
                        modal.classList.add('is-open');
                    });
                    document.documentElement.classList.add('nexdirectory-auth-modal-open');
                    document.body.classList.add('nexdirectory-auth-modal-open');
                    isOpen = true;
                    window.setTimeout(function () {
                        var field = modal.querySelector('[data-nexdirectory-auth-panel]:not([hidden]) input[type="text"], [data-nexdirectory-auth-panel]:not([hidden]) input[type="email"]');
                        if (field && typeof field.focus === 'function') {
                            field.focus();
                        }
                    }, 80);
                }

                function closeModal() {
                    if (!isOpen) {
                        return;
                    }
                    modal.classList.remove('is-open');
                    document.documentElement.classList.remove('nexdirectory-auth-modal-open');
                    document.body.classList.remove('nexdirectory-auth-modal-open');
                    isOpen = false;
                    closeTimer = window.setTimeout(function () {
                        if (!isOpen) {
                            modal.hidden = true;
                        }
                    }, 240);
                }

                window.nexdirectoryOpenAuthModal = function (options) {
                    var opts = options && typeof options === 'object' ? options : {};
                    openModal(String(opts.tab || 'login'), String(opts.redirectTo || defaultRedirect));
                };

                modal.querySelectorAll('[data-nexdirectory-auth-close]').forEach(function (button) {
                    button.addEventListener('click', function (event) {
                        event.preventDefault();
                        closeModal();
                    });
                });

                tabButtons.forEach(function (button) {
                    button.addEventListener('click', function () {
                        setTab(String(button.getAttribute('data-nexdirectory-auth-tab-trigger') || 'login'));
                    });
                    button.addEventListener('keydown', function (event) {
                        if (event.key !== 'ArrowLeft' && event.key !== 'ArrowRight' && event.key !== 'Home' && event.key !== 'End') {
                            return;
                        }
                        event.preventDefault();
                        var currentIndex = Math.max(0, tabButtons.indexOf(button));
                        var nextIndex = currentIndex;
                        if (event.key === 'Home') {
                            nextIndex = 0;
                        } else if (event.key === 'End') {
                            nextIndex = tabButtons.length - 1;
                        } else if (event.key === 'ArrowRight') {
                            nextIndex = (currentIndex + 1) % tabButtons.length;
                        } else {
                            nextIndex = (currentIndex - 1 + tabButtons.length) % tabButtons.length;
                        }
                        var nextButton = tabButtons[nextIndex];
                        if (nextButton) {
                            setTab(String(nextButton.getAttribute('data-nexdirectory-auth-tab-trigger') || 'login'));
                            nextButton.focus();
                        }
                    });
                });

                modal.querySelectorAll('[data-nexdirectory-auth-switch]').forEach(function (button) {
                    button.addEventListener('click', function () {
                        setTab(String(button.getAttribute('data-nexdirectory-auth-switch') || 'login'));
                    });
                });

                modal.addEventListener('click', function (event) {
                    if (event.target === modal) {
                        closeModal();
                    }
                });

                if (dialog) {
                    dialog.addEventListener('click', function (event) {
                        event.stopPropagation();
                    });
                }

                document.addEventListener('keydown', function (event) {
                    if (event.key === 'Escape') {
                        closeModal();
                    }
                });

                document.addEventListener('click', function (event) {
                    var directTrigger = event.target.closest('[data-nexdirectory-auth-open="1"]');
                    var anchor = event.target.closest('a[href]');
                    var trigger = directTrigger || anchor;
                    if (!trigger) {
                        return;
                    }
                    if (event.defaultPrevented || event.button !== 0 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) {
                        return;
                    }
                    if (anchor && (anchor.hasAttribute('download') || String(anchor.getAttribute('target') || '') === '_blank')) {
                        return;
                    }

                    var href = String((trigger.getAttribute('href') || '')).trim();
                    var parsed = parseSafeUrl(href);
                    if (!parsed) {
                        return;
                    }

                    var path = normalizePath(parsed.pathname);
                    var isLoginRoute = !!(loginPath && path === loginPath);
                    var isClaimRoute = !!(!document.body.classList.contains('logged-in') && claimPath && path === claimPath);
                    var shouldIntercept = !!directTrigger || isLoginRoute || isClaimRoute;
                    if (!shouldIntercept) {
                        return;
                    }

                    event.preventDefault();
                    var tab = String(trigger.getAttribute('data-nexdirectory-auth-tab') || parsed.searchParams.get('nexdirectory_auth') || 'login').trim();
                    if (tab !== 'signup' && tab !== 'forgot' && tab !== 'login') {
                        tab = 'login';
                    }
                    var redirectTo = String(trigger.getAttribute('data-nexdirectory-auth-redirect') || parsed.searchParams.get('redirect_to') || '').trim();
                    if (!redirectTo) {
                        redirectTo = isClaimRoute ? parsed.toString() : window.location.href;
                    }
                    openModal(tab, redirectTo);
                });
            })();
