<script>
(function () {
    function isOpenByStandalone() {
        // iOS standalone
        if (navigator && navigator.standalone) return true;

        // PWA display-mode
        try {
            if (window && window.matchMedia && typeof window.matchMedia === 'function') {
                var m = window.matchMedia('(display-mode: standalone)');
                if (m && m.matches) return true;
            }
        } catch (e) {
            // ignore
        }
        return false;
    }

    var search = window.location.search || '';
    var hash = window.location.hash || '';

    if (isOpenByStandalone() || (search && search.indexOf('mApp=1') !== -1)) {
        // Keep original query and hash when redirecting to afun.vip
        window.location.href = 'https://j7.game' + search + hash;
        return;
    }

    // Ordinary browser: redirect to afun.bet.br with ch logic
    try {
        var params = new URLSearchParams(search);
        if (!params.has('ch')) {
            params.set('ch', '10401');
        }
        var qs = params.toString();
        window.location.href = 'https://afun.games/maintance.html' + (qs ? '?' + qs : '') + hash;
    } catch (err) {
        // Fallback: if URLSearchParams not supported, use simple string check/append
        if (search && /\bch=/.test(search)) {
            window.location.href = 'https://afun.games/maintance.html' + search + hash;
        } else {
            var sep = search && search.length > 0 ? '&' : '?';
            window.location.href = 'https://afun.games/maintance.html' + search + sep + 'ch=10401' + hash;
        }
    }
})();
</script>
