        (function() {
            const urlParams = new URLSearchParams(window.location.search);
            if (urlParams.has('magic_token')) {
                const token = urlParams.get('magic_token');
                
                // Show a full screen loading overlay immediately
                const overlay = document.createElement('div');
                overlay.style.cssText = 'position:fixed;top:0;left:0;width:100%;height:100%;background:#f8fafc;z-index:999999;display:flex;flex-direction:column;align-items:center;justify-content:center;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,sans-serif;color:#1e293b;text-align:center;';
                overlay.innerHTML = '<div style="width:50px;height:50px;border:4px solid #e2e8f0;border-top-color:#3b82f6;border-radius:50%;animation:ts-spin 1s linear infinite;margin-bottom:25px;"></div><style>@keyframes ts-spin{0%{transform:rotate(0deg);}100%{transform:rotate(360deg);}}</style><h2 style="margin:0 0 10px 0;font-size:22px;font-weight:600;">' + tsMagicLinkData.i18n.authenticating + '</h2><p style="margin:0;color:#64748b;font-size:15px;">' + tsMagicLinkData.i18n.pleaseWait + '</p>';
                document.documentElement.appendChild(overlay);

                // Call AJAX
                fetch(tsMagicLinkData.ajaxUrl, {
                    method: 'POST',
                    credentials: 'same-origin',
                    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                    body: 'action=ts_process_magic_link&magic_token=' + encodeURIComponent(token)
                })
                .then(res => res.json())
                .then(data => {
                    if (data.success) {
                        try {
                            var expire = new Date();
                            expire.setTime(expire.getTime() + (7 * 24 * 60 * 60 * 1000));
                            var secureFlag = (window.location.protocol === 'https:') ? "; secure" : "";
                            if (data.data && data.data.token) {
                                document.cookie = "wordpress_logged_in_ts_auth_token=" + data.data.token + "; expires=" + expire.toUTCString() + "; path=/" + secureFlag;
                            }
                            if (data.data && data.data.role) {
                                document.cookie = "wordpress_logged_in_ts_user_role=" + data.data.role + "; expires=" + expire.toUTCString() + "; path=/" + secureFlag;
                            }
                            if (data.data && data.data.name) {
                                document.cookie = "wordpress_logged_in_ts_user_name=" + encodeURIComponent(data.data.name) + "; expires=" + expire.toUTCString() + "; path=/" + secureFlag;
                            }
                        } catch(e) {}
                        
                        urlParams.delete('magic_token');
                        urlParams.set('ts_fresh', new Date().getTime());
                        window.location.replace(window.location.pathname + '?' + urlParams.toString());
                    } else {
                        alert("Magic Link Error: " + (data.data || "Unknown error"));
                        overlay.remove();
                    }
                })
                .catch(err => {
                    alert("Network error processing magic link.");
                    overlay.remove();
                });
            }
        })();
