<?php

declare(strict_types=1);

error_reporting(E_ALL);
ini_set('display_errors', '0');
ini_set('log_errors', '1');

include_once __DIR__ . '/../password.login.php';

const ADMIN_CSRF_NAMESPACE = 'admin_panel';

function adminIsHttpsRequest(): bool
{
    if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') {
        return true;
    }

    if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strtolower((string) $_SERVER['HTTP_X_FORWARDED_PROTO']) === 'https') {
        return true;
    }

    if (isset($_SERVER['HTTP_CF_VISITOR']) && str_contains(strtolower((string) $_SERVER['HTTP_CF_VISITOR']), '"scheme":"https"')) {
        return true;
    }

    return (int) ($_SERVER['SERVER_PORT'] ?? 0) === 443;
}

function startAdminSession(): void
{
    if (session_status() === PHP_SESSION_ACTIVE) {
        return;
    }

    session_name('sslmgr_admin');
    session_set_cookie_params([
        'lifetime' => 0,
        'path' => '/',
        'secure' => adminIsHttpsRequest(),
        'httponly' => true,
        'samesite' => 'Strict',
    ]);
    session_start();
}

function adminEsc(string $value): string
{
    return htmlspecialchars($value, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
}

function adminCsrfToken(): string
{
    if (!isset($_SESSION['csrf'][ADMIN_CSRF_NAMESPACE]) || !is_string($_SESSION['csrf'][ADMIN_CSRF_NAMESPACE])) {
        $_SESSION['csrf'][ADMIN_CSRF_NAMESPACE] = bin2hex(random_bytes(32));
    }

    return $_SESSION['csrf'][ADMIN_CSRF_NAMESPACE];
}

function adminHasValidCsrf(?string $token): bool
{
    $storedToken = $_SESSION['csrf'][ADMIN_CSRF_NAMESPACE] ?? null;

    return is_string($token) && is_string($storedToken) && hash_equals($storedToken, $token);
}

function showLoginPasswordProtect(string $errorMsg, string $nonce, string $csrfToken): never
{
    header('Location: /login.php');
    exit;
}

startAdminSession();

$nonce = base64_encode(random_bytes(18));
$csrfToken = adminCsrfToken();

header('Content-Type: text/html; charset=utf-8');
header('X-Content-Type-Options: nosniff');
header('Referrer-Policy: same-origin');
header('X-Frame-Options: SAMEORIGIN');
header('Permissions-Policy: geolocation=(), microphone=(), camera=()');
header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
header('Pragma: no-cache');
header(
    "Content-Security-Policy: default-src 'self'; "
    . "base-uri 'self'; "
    . "form-action 'self'; "
    . "frame-ancestors 'self'; "
    . "img-src 'self' data: https:; "
    . "font-src 'self' data: https:; "
    . "style-src 'self' 'nonce-{$nonce}' https://fonts.googleapis.com https://use.fontawesome.com https://cdnjs.cloudflare.com; "
    . "script-src 'self' 'nonce-{$nonce}'; "
    . "connect-src 'self';",
);

if (isset($_GET['logout'])) {
    unset($_SESSION['admin_authenticated']);
    session_regenerate_id(true);
    header('Location: /login.php');
    exit;
}

if (empty($_SESSION['admin_authenticated'])) {
    showLoginPasswordProtect('', $nonce, $csrfToken);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>ADMIN PANEL</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="csrf-token" content="<?= adminEsc($csrfToken); ?>">
    <link href="/favicon.ico" rel="icon" type="image/x-icon">
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Caveat:wght@500;600&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="../dist/bootstrap.min.css" type="text/css" media="all">
    <link href="../dist/flags.css" rel="stylesheet">

    <style nonce="<?= adminEsc($nonce); ?>">:root{color-scheme:light;--bg:#f8fafd;--surface:#f0f3f8;--panel:rgba(255,255,255,.72);--panel2:#fff;--line:#e2e8f0;--line2:rgba(68,71,70,.07);--text:#444746;--muted:#6e7370;--strong:#1f2221;--primary:#444746;--primary2:#1f2221;--accent:#444746;--accent-h:#1f2221;--accent-s:rgba(68,71,70,.10);--on:#fff;--bad:#6e7370;--good:#444746;--warn:#6e7370;--radius:.3rem;--shadow:none;--shadow-modal:none;--font:Inter,ui-sans-serif,system-ui,-apple-system,BlinkMacSystemFont,"Segoe UI",Arial,sans-serif;--mono:"SFMono-Regular",Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace;--panel-soft:var(--panel2);--panel-raised:var(--panel);--panel-fade:rgba(255,255,255,.7);--line-soft:var(--line2);--text-strong:var(--strong);--dark:var(--strong);--light:var(--bg);--accent-hover:var(--accent-h);--accent-soft:var(--accent-s);--on-accent:var(--on);--danger:var(--bad);--danger-soft:rgba(108,117,125,.10);--success:var(--good);--success-soft:rgba(73,80,87,.10);--ok:var(--good);--ok-soft:rgba(73,80,87,.10);--blue-soft:var(--accent-s);--blue-line:rgba(68,71,70,.28);--nav:#fff;--btn:var(--text);--stroke:var(--line);--inverse:var(--strong);--faded:var(--bg);--font-cursive:cursive;--font-mono:monospace}@media (prefers-color-scheme:dark){:root{color-scheme:dark;--bg:#212529;--surface:#343a40;--panel:rgba(33,37,41,.95);--panel2:#343a40;--line:#495057;--line2:rgba(248,249,250,.06);--text:#e9ecef;--muted:#6e7370;--strong:#f8f9fa;--primary:#e9ecef;--primary2:#f8f9fa;--accent:#495057;--accent-h:#adb5bd;--accent-s:rgba(173,181,189,.12);--on:#212529;--bad:#6e7370;--good:#adb5bd;--warn:#6e7370;--shadow:none;--shadow-modal:none;--panel-soft:var(--panel2);--panel-raised:var(--panel);--panel-fade:rgba(33,37,41,.8);--line-soft:var(--line2);--text-strong:var(--strong);--accent-hover:var(--accent-h);--accent-soft:var(--accent-s);--on-accent:var(--on);--danger:var(--bad);--danger-soft:rgba(108,117,125,.12);--success:var(--good);--success-soft:rgba(173,181,189,.12);--ok:var(--good);--ok-soft:rgba(173,181,189,.12);--blue-soft:var(--accent-s);--blue-line:rgba(173,181,189,.30);--nav:#212529;--btn:var(--text);--stroke:var(--line)}}*{box-sizing:border-box}html,body{width:100%;height:100dvh;margin:0;}html{background:var(--bg);zoom:1.1;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale;text-rendering:optimizeLegibility}body{font-family:var(--font);padding:58px 8px 18px!important;background:radial-gradient(circle at 50% 8%,rgba(255,90,10,.12),transparent 28rem),radial-gradient(circle at 10% 80%,rgba(15,23,42,.06),transparent 22rem),#f8fafc!important;color:var(--text)!important;font-family:monospace!important;font-size:13px;line-height:1.45;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale;text-rendering:optimizeLegibility}a{color:inherit;text-decoration:none}.container{width:100%;max-width:1180px;margin:0 auto}code{font-family:Consolas,Monaco,'Courier New',monospace;font-size:11.5px;color:var(--text);background:var(--panel-soft);border:1px solid var(--line-soft);border-radius:var(--radius);padding:1px 4px}.navbar.navbar-default{min-height:44px;border:0!important;border-bottom:0!important;background:#fff!important;box-shadow:var(--shadow)!important}.navbar .container{max-width:1180px}.navbar-brand{height:44px!important;padding:12px 10px!important;color:var(--text-strong)!important;font-size:13px;line-height:20px}.navbar-nav>li>a{padding-top:12px!important;padding-bottom:12px!important;color:var(--text)!important;font-size:12px}.navbar-nav>li>a:hover,.navbar-default .navbar-nav>.active>a,.navbar-default .navbar-nav>.active>a:focus,.navbar-default .navbar-nav>.active>a:hover{background:var(--accent)!important;color:var(--on-accent)!important}.navbar-toggle{margin-top:5px!important;margin-bottom:5px!important;border-color:var(--line)!important;border-radius:var(--radius)!important;background:var(--panel)!important}.navbar-toggle .icon-bar{background:var(--text)!important}.panel,.panel-default,.well,.modal-content{border: 1px solid var(--line) !important;border-radius:var(--radius)!important;background:var(--panel)!important;color:var(--text)!important;box-shadow:var(--shadow)!important;overflow:hidden}.panel-heading,.panel-footer,.modal-header,.modal-footer{padding:8px 10px!important;background:#fff!important;border:0!important;color:var(--text-strong)!important}.panel-body,.modal-body{padding:10px!important;background:#fff!important;color:var(--text)!important}.pull-right{display:flex;align-items:center;gap:6px}.modal-content{box-shadow:var(--shadow-modal)!important}.modal-title{font-size:14px;font-weight:700;color:var(--text-strong)}.close{color:var(--text)!important;opacity:.78;text-shadow:none}.form-group{margin-bottom:8px}.control-label,label{margin-bottom:4px;color:var(--text-strong);font-size:12px}.form-control,input[type=text],input[type=number],input[type=password],input[type=search],select,textarea{height:31px;min-height:31px;border:1px solid var(--line)!important;border-radius:var(--radius)!important;background:var(--panel)!important;color:var(--text)!important;font-family:inherit!important;font-size:12px!important;line-height:1.4!important;box-shadow:none!important; }.form-control:focus,input:focus,select:focus,textarea:focus{outline:none;border-color:var(--line)!important;box-shadow:none!important}.form-control::placeholder,textarea::placeholder{color:color-mix(in srgb,var(--text) 54%,transparent)}.form-control[readonly],input[readonly],textarea[readonly]{background:var(--panel-soft)!important;color:var(--text)!important}textarea.form-control{height:auto;min-height:114px;resize:none}.input-group-addon{height:31px;padding:5px 8px;background:transparent!important;border-color:var(--line)!important;color:var(--text)!important;border-radius:var(--radius)!important;font-size:11.5px}.btn,button,.btn-sm,.btn-xs{display:inline-flex;align-items:center;justify-content:center;gap:5px;border:1px solid var(--line)!important;border-radius:var(--radius)!important;background:var(--panel)!important;color:var(--text)!important;font-size:11.5px;font-weight:700;line-height:1.2;box-shadow:none!important;outline:none;transition:background .12s ease,border-color .12s ease,color .12s ease}.btn:hover,button:hover,.btn-sm:hover,.btn-xs:hover{background:var(--panel-soft)!important;border-color:var(--line)!important;color:var(--text-strong)!important}.btn-primary,.btn-primary:focus,.btn-primary:active,.btn.active{background:var(--accent)!important;border-color:var(--accent)!important;color:var(--on-accent)!important}.btn-primary:hover{background:var(--accent-hover)!important;border-color:var(--accent-hover)!important;color:var(--on-accent)!important}.btn-danger,.btn-danger:focus{background:var(--danger-soft)!important;border-color:rgba(108,117,125,.34)!important;color:var(--danger)!important}.btn-danger:hover{background:rgba(108,117,125,.16)!important;color:var(--danger)!important}.glyphicon{top:1px;color:currentColor}.action-ico{display:inline-flex;align-items:center;justify-content:center;flex-shrink:0}.action-ico svg{display:block}.table-responsive{border:0!important}.table{width:100%;margin:4px 0 0;border-collapse:separate;border-spacing:0;border:0;border-radius:var(--radius);overflow:hidden;background:var(--panel)!important}.table>thead>tr>th{position:sticky;top:0;z-index:20;padding:7px 8px!important;border-bottom:1px solid var(--line)!important;background:#f5f5f5!important;color:var(--text-strong)!important;font-size:10.5px!important;line-height:1.2;text-transform:uppercase;letter-spacing:.035em;white-space:nowrap}.table>tbody>tr>td{padding:6px 8px!important;border-top:1px solid var(--line-soft)!important;color:var(--text);font-size:11.5px;vertical-align:middle;word-break:break-word;background:var(--panel)!important}.table-hover>tbody>tr:hover>td{background:var(--panel-soft)!important}.bootgrid-table th>.column-header-anchor{color:var(--text-strong)!important}.bootgrid-header,.bootgrid-footer{margin:6px 0!important;color:var(--text)!important}.bootgrid-header .search .form-control{height:31px}.bootgrid-header .actionBar{text-align:right}.pagination>li>a,.pagination>li>span{border-color:var(--line)!important;background:var(--panel)!important;color:var(--text)!important}.pagination>.active>a,.pagination>.active>span{background:var(--accent)!important;border-color:var(--accent)!important;color:var(--on-accent)!important}.dropdown-menu{border:0!important;border-radius:var(--radius)!important;background:#fff!important;box-shadow:var(--shadow-modal)!important}.dropdown-menu>li>a{color:var(--text)!important;font-size:12px}.dropdown-menu>li>a:hover{background:var(--panel-soft)!important;color:var(--text-strong)!important}.alert{border-radius:var(--radius)!important}.alert-danger,.error{background:var(--danger-soft)!important;border-color:rgba(108,117,125,.28)!important;color:var(--danger)!important}.alert-success,.success{background:var(--blue-soft)!important;border-color:var(--blue-line)!important;color:var(--text)!important}.label{border-radius:var(--radius)!important}.label-default{background:var(--panel-soft)!important;color:var(--text)!important;border:1px solid var(--line)}blockquote{margin:0 0 10px;padding:7px 10px;border-left:2px solid var(--line)!important;background:var(--panel-fade);border-radius:var(--radius);font-size:12px}.text-warning{margin:0 0 2px;color:var(--text-strong)!important;font-weight:700}.text-muted{color:var(--text)!important}.flag{border-radius:var(--radius)}.admin-toast-root{position:fixed;right:14px;bottom:14px;z-index:10050;display:flex;flex-direction:column;gap:5px;align-items:flex-end}.admin-toast{font-family:var(--mono, ui-monospace, SFMono-Regular, Menlo, Consolas, monospace);max-width:300px;min-width:160px;padding:9px 12px;border:1px solid var(--line);border-left:3px solid var(--accent);border-radius:var(--radius);background:var(--panel2);color:var(--text);box-shadow:0 2px 12px rgba(0,0,0,.10);font-size:12px;line-height:1.4;animation:_t-in .16s ease both}.admin-toast strong{display:block;font-weight:700;color:var(--text-strong);margin-bottom:1px}.admin-toast--error{border-left-color:#8f6a6a;background:rgba(143,106,106,.13)!important;border-color:rgba(143,106,106,.28)!important;color:var(--text-strong)}.admin-toast--error strong{color:var(--text-strong)}.admin-toast--success{border-left-color:var(--accent);background:var(--panel2)}.admin-toast--info{border-left-color:var(--muted)}.admin-toast--removing{animation:_t-out .16s ease both!important}@keyframes _t-in{from{opacity:0;transform:translateX(12px)}to{opacity:1;transform:translateX(0)}}@keyframes _t-out{to{opacity:0;transform:translateX(12px)}}.admin-fallback-backdrop{position:fixed;inset:0;z-index:10040;display:flex;align-items:center;justify-content:center;padding:12px;background:rgba(33,37,41,.36)}.admin-fallback-dialog{width:min(420px,100%);border:1px solid var(--line);border-radius:var(--radius);background:#fff;color:var(--text);box-shadow:var(--shadow-modal);overflow:hidden}.admin-fallback-head{padding:10px;border-bottom:1px solid var(--line-soft);font-weight:800;color:var(--text-strong);background:#fff}.admin-fallback-body{padding:10px;font-size:12px;background:#fff}.admin-fallback-actions{display:flex;gap:6px;justify-content:flex-end;padding:10px;border-top:1px solid var(--line-soft);background:#fff}@media screen and (max-width:768px){body{padding:58px 6px 12px!important;font-size:12.5px}.container{width:100%!important;max-width:100%!important;padding-left:6px!important;padding-right:6px!important}.panel-heading,.panel-body,.panel-footer{padding:7px!important;background:#fff!important}.navbar .container{padding-left:8px!important;padding-right:8px!important}.navbar-collapse{border-color:var(--line)!important;background:var(--panel)!important}.pull-right{float:none!important;justify-content:flex-end}.input-group{display:block}.input-group>.form-control,.input-group>.input-group-addon,.input-group>.input-group-btn,.input-group-btn>.btn{display:block;width:100%!important}.input-group-addon{border-bottom:0!important;border-radius:var(--radius) var(--radius) 0 0!important}.input-group>.form-control{border-radius:0 0 var(--radius) var(--radius)!important}.input-group-btn>.btn{margin-top:5px}.table{display:block;overflow-x:auto;white-space:nowrap}.modal-dialog{width:auto!important;margin:8px}}#tbl_trackers{table-layout:auto;width:100%}#tbl_trackers th:nth-child(1),#tbl_trackers td:nth-child(1){width:50px;text-align:center;padding-left:4px!important;padding-right:4px!important}#tbl_trackers th:nth-child(2),#tbl_trackers td:nth-child(2){width:120px}#tbl_trackers th:nth-child(3),#tbl_trackers td:nth-child(3){width:120px}#tbl_trackers th:nth-child(5),#tbl_trackers td:nth-child(5){width:120px}.td-num{display:block;font-size:11.5px;font-weight:600;font-family:Consolas,Monaco,monospace;color:color-mix(in srgb,var(--text) 44%,transparent);text-align:center}.td-tracker{display:inline-block;width:100%;padding:2px 8px;border-radius:.3rem;border:1px solid var(--line);background:var(--panel-soft);font-family:Consolas,Monaco,monospace;font-size:11.5px;font-weight:700;letter-spacing:.03em;white-space:nowrap}.td-subdomain{display:inline-block;padding:2px 8px;border-radius:.3rem!important;width:100%;border:1px solid var(--line);background:var(--panel-soft);font-family:Consolas,Monaco,monospace;font-size:11.5px;font-weight:700;letter-spacing:.03em;white-space:nowrap}.td-url{display:block;max-width:100%;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;font-family:Consolas,Monaco,monospace;font-size:11.5px}.td-pass{font-family:Consolas,Monaco,monospace;font-size:11.5px;color:color-mix(in srgb,var(--text) 60%,transparent)}
/* bootgrid toolbar search/select fix */.navbar-default .navbar-nav>li>a:hover,.navbar-default .navbar-nav>li>a:focus{background:color-mix(in srgb,var(--accent) 5%,transparent)!important;color:var(--text-strong)!important;box-shadow:none!important}.navbar-default .navbar-nav>.active>a,.navbar-default .navbar-nav>.active>a:hover,.navbar-default .navbar-nav>.active>a:focus{background:transparent!important;color:var(--text-strong)!important;box-shadow:none!important;border-bottom:2px solid var(--accent)!important;padding-bottom:10px!important}.panel,.panel-default{overflow:visible!important}.panel-body{overflow:visible!important}.table-responsive{overflow-x:auto!important;overflow-y:visible!important}.bootgrid-header{position:relative!important;z-index:80!important;display:block!important;margin:8px 0 7px!important;color:var(--text)!important}.bootgrid-header .actionBar{display:flex!important;align-items:center!important;justify-content:flex-end!important;gap:8px!important;float:none!important;width:100%!important;margin:0!important;text-align:right!important;white-space:nowrap!important}.bootgrid-header .search{position:relative!important;display:inline-flex!important;align-items:center!important;float:none!important;width:220px!important;min-width:220px!important;max-width:260px!important;margin:0!important;vertical-align:middle!important}.bootgrid-header .search .input-group{display:flex!important;align-items:stretch!important;width:100%!important;border-collapse:separate!important}.bootgrid-header .search .input-group-addon{display:flex!important;align-items:center!important;justify-content:center!important;flex:0 0 34px!important;width:34px!important;min-width:34px!important;height:32px!important;padding:0!important;border:1px solid var(--line)!important;border-right:0!important;border-radius:var(--radius) 0 0 var(--radius)!important;background:var(--panel-soft)!important;color:var(--text)!important;line-height:1!important}.bootgrid-header .search .input-group-addon .glyphicon{top:0!important;font-size:12px!important}.bootgrid-header .search .search-field,.bootgrid-header .search .form-control{display:block!important;flex:1 1 auto!important;width:100%!important;height:32px!important;min-height:32px!important;padding:5px 9px!important;border:1px solid var(--line)!important;border-left:0!important;border-radius:0 var(--radius) var(--radius) 0!important;background:var(--panel)!important;color:var(--text)!important;font-size:12px!important;line-height:1.4!important;box-shadow:none!important}.bootgrid-header .search .search-field:focus,.bootgrid-header .search .form-control:focus{border-color:var(--line)!important;border-left:0!important;box-shadow:none!important}.bootgrid-header .actions{position:relative!important;display:inline-flex!important;align-items:center!important;gap:6px!important;float:none!important;margin:0!important;vertical-align:middle!important}.bootgrid-header .actions>.btn-group{position:relative!important;display:inline-flex!important;float:none!important;margin:0!important;vertical-align:middle!important}.bootgrid-header .actions .btn,.bootgrid-header .actions .dropdown-toggle{display:inline-flex!important;align-items:center!important;justify-content:center!important;gap:6px!important;height:32px!important;min-width:38px!important;padding:5px 10px!important;line-height:1.2!important;border-radius:var(--radius)!important}.bootgrid-header .actions .dropdown-toggle{min-width:64px!important}.bootgrid-header .actions .dropdown-toggle .caret{margin-left:2px!important}.bootgrid-header .actions .dropdown-menu{position:absolute!important;top:calc(100% + 4px)!important;right:0!important;left:auto!important;z-index:3000!important;display:none;min-width:112px!important;width:112px!important;margin:0!important;padding:4px!important;border:1px solid var(--line)!important;border-radius:var(--radius)!important;background:#fff!important;box-shadow:var(--shadow-modal)!important;list-style:none!important}.bootgrid-header .actions .open>.dropdown-menu{display:block!important}.bootgrid-header .actions .dropdown-menu>li{display:block!important;float:none!important;width:100%!important;margin:0!important;padding:0!important;text-align:left!important}.bootgrid-header .actions .dropdown-menu>li>a{display:block!important;width:100%!important;min-width:0!important;padding:6px 9px!important;border-radius:var(--radius)!important;background:transparent!important;color:var(--text)!important;font-size:12px!important;line-height:1.25!important;text-align:left!important;white-space:nowrap!important}.bootgrid-header .actions .dropdown-menu>li>a:hover,.bootgrid-header .actions .dropdown-menu>li>a:focus{background:var(--panel-soft)!important;color:var(--text-strong)!important}.bootgrid-header .actions .dropdown-menu>.active>a,.bootgrid-header .actions .dropdown-menu>.active>a:hover,.bootgrid-header .actions .dropdown-menu>.active>a:focus{background:var(--accent-soft)!important;color:var(--text-strong)!important;box-shadow:none!important}.bootgrid-footer{position:relative!important;z-index:20!important}.bootgrid-footer .pagination{margin:0!important}@media screen and (max-width:768px){.bootgrid-header .actionBar{align-items:stretch!important;justify-content:stretch!important;flex-wrap:wrap!important;gap:6px!important}.bootgrid-header .search{width:100%!important;min-width:0!important;max-width:none!important;flex:1 0 100%!important}.bootgrid-header .actions{margin-left:auto!important}.bootgrid-header .actions .dropdown-menu{right:0!important;left:auto!important}}
.tbl-search-group{width:200px}.ddt{position:relative;display:inline-block}.ddt-btn{min-width:52px!important;height:31px!important;padding:4px 8px!important;gap:4px!important;justify-content:space-between!important}.ddt-menu{display:none;position:absolute;top:calc(100% + 3px);left:0;z-index:3000;min-width:62px;padding:3px;list-style:none;margin:0;border:1px solid var(--line);border-radius:var(--radius);background:#fff!important;box-shadow:var(--shadow-modal)}.ddt-menu.open{display:block}.ddt-menu li a{display:block;padding:5px 10px;border-radius:var(--radius);color:var(--text);font-size:12px;font-weight:600;line-height:1.3;text-decoration:none;white-space:nowrap;transition:background .1s;background:#fff!important}.ddt-menu li+li{border-top:1px solid rgba(68,71,70,.06);margin:2px 4px 0}.ddt-menu li a:hover{background:#444746!important;color:#fff!important}.ddt-menu li a.ddt-active{background:var(--accent)!important;color:var(--on-accent)!important}.ddt-caret{margin-left:3px;opacity:.5;vertical-align:middle}.table-scroll{overflow-x:auto}.th-action{text-align:right!important}.pagination-compact{margin:0!important}.td-action{text-align:right!important;white-space:nowrap!important}.td-empty{text-align:center!important;padding:18px!important;color:var(--text)!important}input:focus,textarea:focus,select:focus,button:focus,.form-control:focus,.btn:focus,a:focus{outline:none!important}.app-sign{text-align:right;padding:3px 0 2px}.app-sign footer{font-family:'Caveat','Dancing Script',cursive;font-size:14px;font-weight:600;color:#444746;text-decoration:none;letter-spacing:.02em;line-height:1;opacity:.85}select hr{border:none;border-top:1px solid rgba(68,71,70,.06)!important;color:rgba(68,71,70,.06)!important;opacity:.45;margin:1px 4px}input[type=url]{font-family:Consolas,Monaco,"Courier New",monospace!important}#gen_url,#sm_url,#edit_gen_url,#edit_sm_url{font-family:Consolas,Monaco,"Courier New",monospace!important}body::before{content:'NGIX\2022 XCTD';position:fixed;top:12px;left:14px;z-index:0;font:600 11px/1 Inter,ui-sans-serif,system-ui,-apple-system,sans-serif;letter-spacing:.24em;text-transform:uppercase;color:rgba(68,71,70,.06);pointer-events:none}body::after{content:'';position:fixed;inset:0;z-index:0;pointer-events:none;background-image:url('/assets/favicon.svg'),url('/assets/bg.png');background-repeat:no-repeat,no-repeat;background-position:right 14px bottom 12px,center center;background-size:28px 28px,cover;opacity:.28;filter:saturate(.68) contrast(.84) brightness(1.03) blur(.32px);transform:translateZ(0)}</style>
<style nonce="<?= adminEsc($nonce); ?>">/* final action icon render fix */#tbl_trackers th:last-child,#tbl_trackers td:last-child{text-align:center!important;white-space:nowrap!important;width:108px!important;min-width:108px!important;max-width:108px!important}.bootgrid-table td:last-child{overflow:visible!important}.action-btn,.bootgrid-table .command-edit,.bootgrid-table .command-delete{display:inline-flex!important;align-items:center!important;justify-content:center!important;width:32px!important;height:26px!important;min-width:32px!important;min-height:26px!important;padding:0!important;margin:0 2px!important;border:1px solid var(--line)!important;border-radius:var(--radius)!important;background:var(--panel)!important;color:var(--text)!important;font-size:0!important;line-height:1!important;text-indent:0!important;vertical-align:middle!important;opacity:1!important;visibility:visible!important;box-shadow:none!important;overflow:visible!important}.action-btn:hover,.bootgrid-table .command-edit:hover{background:var(--accent-soft)!important;border-color:var(--line)!important;color:var(--text-strong)!important}.bootgrid-table .command-delete{color:var(--danger)!important}.bootgrid-table .command-delete:hover{background:var(--danger-soft)!important;border-color:color-mix(in srgb,var(--danger) 34%,var(--line))!important;color:var(--danger)!important}.bootgrid-table .command-edit[disabled],.bootgrid-table .command-delete[disabled]{opacity:.58!important;cursor:not-allowed!important}.action-btn .action-ico,.bootgrid-table .command-edit .action-ico,.bootgrid-table .command-delete .action-ico{display:inline-flex!important;align-items:center!important;justify-content:center!important;width:16px!important;height:16px!important;min-width:16px!important;min-height:16px!important;color:currentColor!important;opacity:1!important;visibility:visible!important;overflow:visible!important;pointer-events:none!important}.action-btn svg,.bootgrid-table .command-edit svg,.bootgrid-table .command-delete svg{display:block!important;width:16px!important;height:16px!important;min-width:16px!important;min-height:16px!important;overflow:visible!important;color:currentColor!important;opacity:1!important;visibility:visible!important;pointer-events:none!important}.action-btn svg *,.bootgrid-table .command-edit svg *,.bootgrid-table .command-delete svg *{vector-effect:non-scaling-stroke!important;stroke:currentColor!important;stroke-width:2!important;stroke-linecap:round!important;stroke-linejoin:round!important;fill:none!important;opacity:1!important;visibility:visible!important}.btn-icon svg,.btn-primary svg{display:block!important;width:13px!important;height:13px!important;fill:currentColor!important;color:currentColor!important;opacity:1!important;visibility:visible!important}input:focus,textarea:focus,select:focus,button:focus,.form-control:focus,.btn:focus,a:focus{outline:none!important}</style>
<style nonce="<?= adminEsc($nonce); ?>">
:root{}.table>thead>tr>th{font-family:var(--font-mono-old)!important;font-size:10.5px!important}.table>tbody>tr>td{font-family:var(--font-mono-old)!important;font-size:12px!important}.form-control,input,select,textarea,.btn,button{font-family:var(--font-mono-old)!important;font-size:11.5px!important}
.table{width:100%;border-collapse:collapse;font-size:12.5px;margin:0!important;border:0!important;border-radius:0!important;background:transparent!important}
.table>thead>tr>th{height:28px;padding:0 8px!important;text-align:left;font-size:11px;font-weight:700;color:var(--text-strong);text-transform:uppercase;letter-spacing:.06em;border-bottom:1px solid var(--line)!important;border-top:0!important;background:var(--panel-soft)!important;white-space:nowrap;position:static}
.table>tbody>tr>td{padding:4px 8px!important;vertical-align:middle;border-bottom:1px solid var(--line-soft)!important;border-top:0!important;font-size:12px;font-weight:400;color:var(--text)!important;word-break:break-word;background:transparent!important}
.table>tbody>tr:last-child>td{border-bottom:0!important}
.table-hover>tbody>tr:hover>td{background:var(--panel-soft)!important}
.table-striped>tbody>tr:nth-of-type(odd)>td{background:transparent!important}
.tbl-toolbar{display:flex;align-items:center;justify-content:space-between;gap:8px;padding:8px 12px;flex-wrap:wrap;border-bottom:1px solid #eee;background:#f5f5f5}
.tbl-toolbar-left{display:flex;align-items:center;gap:6px}
.tbl-footer{display:flex;align-items:center;justify-content:space-between;gap:8px;padding:8px 12px;border-top:1px solid #eee;font-size:12px;color:var(--text);flex-wrap:wrap;background:#f5f5f5}
#tbl_trackers td.url-cell{max-width:220px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
.form-control:focus,input:focus,select:focus,textarea:focus{outline:none;color:var(--text-strong)!important}
.panel,.panel-default{box-shadow:none!important}
.modal-content{box-shadow:none!important}
.navbar-default .navbar-nav>li>a{ }
.navbar-default .navbar-nav>li>a:hover,.navbar-default .navbar-nav>li>a:focus{box-shadow:none!important;background:transparent!important;color:var(--text-strong)!important}
.navbar-default .navbar-nav>.active>a,.navbar-default .navbar-nav>.active>a:hover,.navbar-default .navbar-nav>.active>a:focus{background:transparent!important;color:var(--text-strong)!important;box-shadow:none!important;border-bottom:2px solid var(--accent)!important;padding-bottom:10px!important}
input:focus,textarea:focus,select:focus,button:focus,.form-control:focus,.btn:focus,a:focus{outline:none!important}
.form-control:focus,input:focus,select:focus,textarea:focus{box-shadow:none!important}.bootgrid-header .search .search-field:focus,.bootgrid-header .search .form-control:focus{box-shadow:none!important}*:focus,*:focus-visible{outline:none!important;box-shadow:none!important}.navbar-nav>li>a{line-height:20px!important}.navbar-btn.btn-link{height:44px!important;margin:0!important;padding:12px 10px!important;border:0!important;background:transparent!important;color:var(--text)!important;text-decoration:none!important;box-shadow:none!important}.navbar-btn.btn-link:hover{background:transparent!important;color:var(--text-strong)!important}.table>tfoot>tr>th{padding:7px 12px!important;border-top:1px solid var(--line)!important;border-bottom:0!important;background:#f5f5f5!important;color:var(--text-strong)!important;font-size:11px!important;font-weight:600;text-transform:uppercase;letter-spacing:.06em}
/* ngix xctd image backdrop */
.panel.panel-default:has(> .ngix-image-backdrop){position:relative!important;overflow:hidden!important;isolation:isolate!important;background:rgba(255,255,255,.74)!important}
.panel.panel-default>.ngix-image-backdrop{position:absolute!important;inset:0!important;z-index:0!important;pointer-events:none!important;background-image:linear-gradient(rgba(255,255,255,.82),rgba(255,255,255,.82)),url('/dist/ngix-backdrop.png')!important;background-repeat:no-repeat,no-repeat!important;background-position:center center,center center!important;background-size:100% 100%,cover!important;opacity:.42!important;filter:saturate(.72) contrast(.86) blur(.22px)!important;transform:translateZ(0)!important}
.panel.panel-default:has(> .ngix-image-backdrop)>*:not(.ngix-image-backdrop){position:relative!important;}
.panel.panel-default:has(> .ngix-image-backdrop)>.tbl-toolbar,.panel.panel-default:has(> .ngix-image-backdrop)>.tbl-footer{background:rgba(245,245,245,.82)!important}
.panel.panel-default:has(> .ngix-image-backdrop)>.tbl-toolbar{backdrop-filter:saturate(110%) blur(1px)!important}
.panel.panel-default:has(> .ngix-image-backdrop) .table{position:relative!important;z-index:3!important;background:rgba(255,255,255,.46)!important}
.panel.panel-default:has(> .ngix-image-backdrop) .table>thead>tr>th{background:rgba(245,245,245,.86)!important}
.panel.panel-default:has(> .ngix-image-backdrop) .table>tbody>tr>td{background:rgba(255,255,255,.30)!important}
.panel.panel-default:has(> .ngix-image-backdrop) .table.table-hover>tbody>tr:hover>td{background:rgba(245,245,245,.72)!important}
@media screen and (max-width:768px){.panel.panel-default>.ngix-image-backdrop{background-size:100% 100%,cover!important;background-position:center center,center center!important;opacity:.34!important}}</style>
</head>
<body>
<script nonce="<?= adminEsc($nonce); ?>" src="../dist/jquery-1.11.1.min.js"></script>
<script nonce="<?= adminEsc($nonce); ?>" src="../dist/bootstrap.min.js"></script>
<script nonce="<?= adminEsc($nonce); ?>">
(function ($) {
    'use strict';

    window.adminPanelCsrfToken = <?= json_encode($csrfToken, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT); ?>;

    $.ajaxSetup({
        headers: {
            'X-CSRF-Token': window.adminPanelCsrfToken
        }
    });
}(jQuery));
</script>
<script nonce="<?= adminEsc($nonce); ?>">
(function (window, document) {
    'use strict';

    function ensureToastRoot() {
        var root = document.querySelector('.admin-toast-root');
        if (root) {
            return root;
        }
        root = document.createElement('div');
        root.className = 'admin-toast-root';
        root.setAttribute('aria-live', 'polite');
        document.body.appendChild(root);
        return root;
    }

    function showToast(type, title, text) {
        var root = ensureToastRoot();
        var item = document.createElement('div');
        var normalizedType = type === 'error' ? 'error' : (type === 'success' ? 'success' : 'info');
        item.className = 'admin-toast admin-toast--' + normalizedType;
        if (title) {
            var strong = document.createElement('strong');
            strong.textContent = title;
            item.appendChild(strong);
        }
        if (text) {
            var span = document.createElement('span');
            span.textContent = text;
            item.appendChild(span);
        }
        root.appendChild(item);
        window.setTimeout(function () {
            item.classList.add('admin-toast--removing');
            window.setTimeout(function () {
                if (item.parentNode) item.parentNode.removeChild(item);
            }, 180);
        }, 2600);
    }

    function confirmDialog(options) {
        return new Promise(function (resolve) {
            var backdrop = document.createElement('div');
            backdrop.className = 'admin-fallback-backdrop';

            var dialog = document.createElement('div');
            dialog.className = 'admin-fallback-dialog';

            var head = document.createElement('div');
            head.className = 'admin-fallback-head';
            head.textContent = options.title || 'Confirm action';

            var body = document.createElement('div');
            body.className = 'admin-fallback-body';
            body.textContent = options.text || 'Continue?';

            var actions = document.createElement('div');
            actions.className = 'admin-fallback-actions';

            var cancel = document.createElement('button');
            cancel.type = 'button';
            cancel.className = 'btn btn-default btn-sm';
            cancel.textContent = options.cancelButtonText || 'Cancel';

            var ok = document.createElement('button');
            ok.type = 'button';
            ok.className = 'btn btn-primary btn-sm';
            ok.textContent = options.confirmButtonText || 'OK';

            function close(value) {
                if (backdrop.parentNode) {
                    backdrop.parentNode.removeChild(backdrop);
                }
                resolve({ value: value, isConfirmed: value });
            }

            cancel.addEventListener('click', function () {
                close(false);
            });
            ok.addEventListener('click', function () {
                close(true);
            });

            actions.appendChild(cancel);
            actions.appendChild(ok);
            dialog.appendChild(head);
            dialog.appendChild(body);
            dialog.appendChild(actions);
            backdrop.appendChild(dialog);
            document.body.appendChild(backdrop);
            ok.focus();
        });
    }

    if (!window.Swal || typeof window.Swal.fire !== 'function') {
        window.Swal = {
            fire: function (argOne, argTwo, argThree) {
                var options = typeof argOne === 'object' ? argOne : {
                    title: String(argOne || ''),
                    text: String(argTwo || ''),
                    type: String(argThree || 'info')
                };

                if (options.showCancelButton) {
                    return confirmDialog(options);
                }

                showToast(options.type || options.icon || 'info', options.title || '', options.text || '');
                return {
                    then: function (callback) {
                        if (typeof callback === 'function') {
                            window.setTimeout(function () {
                                callback({ value: true, isConfirmed: true });
                            }, 0);
                        }
                        return this;
                    }
                };
            }
        };
    }
}(window, document));
</script>

<?php
$page_name = dirname(__FILE__);
$each_page_name = explode('/', $page_name);
$data = explode('.', end($each_page_name));
$team = strtoupper($data[0]);
$requestBaseUrl = (adminIsHttpsRequest() ? 'https' : 'http') . '://' . (string) ($_SERVER['HTTP_HOST'] ?? '') . '/';
$requestBaseUrlAttr = adminEsc($requestBaseUrl);
$requestBaseUrlJs = json_encode($requestBaseUrl, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT);
$teamJs = json_encode($team, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT);
?>
<div role="navigation" class="navbar navbar-default navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button data-target=".navbar-collapse" data-toggle="collapse" class="navbar-toggle" type="button">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a href="#" class="navbar-brand"><strong>Admin Panel</strong></a>
            </div>
            <div class="navbar-collapse collapse navbar-right">
                <ul class="nav navbar-nav">
                    <li class="active"><a href="#"><strong>Dashboard</strong></a></li>
                    <li><a href="/campaigns/"><strong>Campaigns</strong></a></li>
                    <li><a href="/addondomain/"><strong>Addon Domain</strong></a></li>
                    <!--<li><a href="/shortlinks/"><strong>Shortlinks</strong></a></li>-->
                    <li><a href="?logout" id="btn-logout" class="logout-btn"><strong>Logout</strong></a></li>
                </ul>
            </div>
            <!--/.nav-collapse -->
        </div>
    </div>
    <div class="container">
        <div class="panel panel-default">
            <div class="ngix-image-backdrop" aria-hidden="true"></div>
            <div class="tbl-toolbar">
                <div class="tbl-toolbar-left">
                    <div class="input-group tbl-search-group">
                        <span class="input-group-addon"><span class="glyphicon glyphicon-search"></span></span>
                        <input type="text" class="form-control" id="tbl-search" placeholder="Search...">
                    </div>
                    <div class="ddt" id="ddt-rowcount">
                        <button type="button" class="btn btn-xs ddt-btn" id="ddt-rowcount-btn"><span class="ddt-label">25</span><svg class="ddt-caret" viewBox="0 0 10 6" width="10" height="6" fill="currentColor" aria-hidden="true" focusable="false"><path d="M0 0l5 6 5-6z"/></svg></button>
                        <ul class="ddt-menu" id="ddt-rowcount-menu">
                            <li><a href="#" data-val="25" class="ddt-active">25</a></li>
                            <li><a href="#" data-val="50">50</a></li>
                            <li><a href="#" data-val="-1">All</a></li>
                        </ul>
                    </div>
                    <input type="hidden" id="tbl-rowcount" value="25">
                </div>
                <button type="button" class="btn btn-sm btn-primary btn-icon" id="command-add" data-row-id="0">
                    <svg viewBox="0 0 24 24" width="13" height="13" stroke="currentColor" fill="none" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false"><path d="M12 5v14"/><path d="M5 12h14"/></svg> Create Generate</button>
            </div>
            <div class="table-scroll">
                <table id="tbl_trackers" class="table table-hover">
                    <thead>
                        <tr>
                            <th>Empid</th>
                            <th>Tracker</th>
                            <th>Password</th>
                            <th>Generate URL</th>
                            <th>TEAM</th>
                            <th class="th-action">Action</th>
                        </tr>
                    </thead>
                    <tbody></tbody>
                </table>
            </div>
            <div class="tbl-footer">
                <span id="tbl-info"></span>
                <ul class="pagination pagination-compact" id="tbl-pagination"></ul>
            </div>
        </div>
        <div class="app-sign"><footer>Ngix · xctd</footer></div>
    </div>
    <div id="add_model" class="modal fade" data-keyboard="false" data-backdrop="static">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title">Create Generate</h4>
                </div>
                <div class="modal-body">
                    <blockquote>
                    <p class="text-warning">ADD UserID:</p>
                    </blockquote> 
                    <form method="post" id="frm_add">
                        <input type="hidden" value="add" name="action" id="action">
                        <div class="form-group">
                            <label for="salary" class="control-label">Tracker:</label>
                            <input type="text" placeholder="{tracker}" class="form-control" id="sub_id" name="sub_id" required="true" autofocus="" autocomplete="off"/>
                        </div>
                        <div class="form-group">
                            <label for="salary" class="control-label">Password:</label>
                            <input type="text" placeholder="{password}" class="form-control" id="password" name="password" required="true"/>
                        </div>
                        <div class="form-group">
                            <label for="salary" class="control-label">Generate URL:</label>
                            <input readonly="readonly" type="text" class="form-control" id="gen_url" name="gen_url" placeholder="<?= $requestBaseUrlAttr; ?>"/>
                        </div>
                        <div class="form-group">
                            <label for="salary" class="control-label">TEAM:</label>
                            <input type="text" placeholder="{smartlink}" class="form-control" id="sm_url" name="sm_url" required="true" autocomplete="off" readonly/>
                        </div>
                        </div>
                        <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button type="button" id="btn_add" class="btn btn-primary">Save</button>
                </div>
                </form>
            </div>
        </div>
    </div>
    <div id="edit_model" class="modal fade" data-keyboard="false" data-backdrop="static">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title">Edit Generate</h4>
                </div>
                <div class="modal-body">
                    <form method="post" id="frm_edit">
                        <input type="hidden" value="edit" name="action" id="action">
                        <input type="hidden" value="0" name="edit_id" id="edit_id">
                        <div class="form-group">
                            <label for="salary" class="control-label">Tracker:</label>
                            <input type="text" class="form-control" id="edit_sub_id" name="edit_sub_id" required="true" autofocus autocomplete="off"/>
                        </div>
                        <div class="form-group">
                            <label for="salary" class="control-label">Password:</label>
                            <input type="text" class="form-control" id="edit_password" name="edit_password" placeholder="Leave blank to keep current password" autocomplete="off" />
                        </div>
                        <div class="form-group">
                            <label for="salary" class="control-label">Generate URL:</label>
                            <input type="text" class="form-control" id="edit_gen_url" name="edit_gen_url" readonly="readonly"/>
                        </div>
                        <div class="form-group">
                            <label for="salary" class="control-label">TEAM:</label>
                            <input type="text" class="form-control" id="edit_sm_url" name="edit_sm_url" autocomplete="off" readonly/>
                        </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button type="button" id="btn_edit" class="btn btn-primary">Save</button>
                </div>
                </form>
            </div>
        </div>
    </div>
    <script nonce="<?= adminEsc($nonce); ?>">
    $(document).ready(function() {
        function svgIcon(type) {
            var a = ' width="14" height="14" viewBox="0 0 24 24" stroke="currentColor" fill="none" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" focusable="false"';
            if (type === 'delete') {
                return '<span class="action-ico" aria-hidden="true"><svg' + a + '><path d="M4 7h16"/><path d="M10 11v6"/><path d="M14 11v6"/><path d="M6 7l1 13h10l1-13"/><path d="M9 7V4h6v3"/></svg></span>';
            }
            if (type === 'eye') {
                return '<span class="action-ico" aria-hidden="true"><svg' + a + '><path d="M1 12s4-7 11-7 11 7 11 7-4 7-11 7-11-7-11-7z"/><circle cx="12" cy="12" r="3"/></svg></span>';
            }
            if (type === 'eye-off') {
                return '<span class="action-ico" aria-hidden="true"><svg' + a + '><path d="M17.94 17.94A10.07 10.07 0 0 1 12 20c-7 0-11-8-11-8a18.45 18.45 0 0 1 5.06-5.94M9.9 4.24A9.12 9.12 0 0 1 12 4c7 0 11 8 11 8a18.5 18.5 0 0 1-2.16 3.19m-6.72-1.07a3 3 0 1 1-4.24-4.24"/><line x1="1" y1="1" x2="23" y2="23"/></svg></span>';
            }
            return '<span class="action-ico" aria-hidden="true"><svg' + a + '><path d="M4 20h4l11-11-4-4L4 16v4z"/><path d="M14 6l4 4"/></svg></span>';
        }
        function escHtml(v) {
            return String(v === null || v === undefined ? '' : v)
                .replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
        }

        function ajaxErrorText(xhr) {
            var response = xhr && xhr.responseJSON ? xhr.responseJSON : null;
            var code = response && response.error ? String(response.error) : 'request-failed';
            if (xhr && xhr.status === 403) { return 'Unauthorized session. Please login again.'; }
            if (xhr && xhr.status === 419) { return 'Session expired. Reload the page and try again.'; }
            if (xhr && xhr.status === 422) { return 'Invalid or incomplete input.'; }
            if (xhr && xhr.status === 405) { return 'Invalid request method.'; }
            if (xhr && xhr.status >= 500) { return 'Server error: ' + code + '. Check PHP error_log.'; }
            return 'Request failed: ' + code;
        }

        function handleAjaxFailure(xhr) {
            Swal.fire({ allowOutsideClick: false, type: 'error', title: 'Request failed', text: ajaxErrorText(xhr) });
        }

        var serv = <?= $requestBaseUrlJs; ?>;
        var adminPanelCsrfToken = window.adminPanelCsrfToken || '';
        var state = { current: 1, rowCount: 25, search: '', rows: [] };

        document.getElementById('sub_id').onkeyup = function() {
            document.getElementById('gen_url').value = serv + this.value.toUpperCase();
        };
        document.getElementById('edit_sub_id').onkeyup = function() {
            document.getElementById('edit_gen_url').value = serv + this.value.toUpperCase();
        };

        function renderRow(row, idx) {
            var url = row.gen_url === null || row.gen_url === undefined ? '' : String(row.gen_url);
            var smUrl = row.sm_url === null || row.sm_url === undefined ? '' : String(row.sm_url);
            var pass = row.password_plain === null || row.password_plain === undefined ? '' : String(row.password_plain);
            return '<tr>' +
                '<td><span class="td-num">' + (idx + 1) + '</span></td>' +
                '<td><span class="td-tracker">' + escHtml(row.sub_id) + '</span></td>' +
                '<td><span class="td-pass" data-pass="' + escHtml(pass) + '" data-shown="0" title="Click the eye to show/hide">••••••</span> ' +
                    '<button type="button" class="btn btn-xs btn-default command-toggle-pass" title="Show/Hide" aria-label="Show or hide password hash">' + svgIcon('eye') + '</button></td>' +
                '<td><span class="td-url" title="' + escHtml(url) + '">' + escHtml(url) + '</span></td>' +
                '<td><span class="td-url" title="' + escHtml(smUrl) + '">' + escHtml(smUrl) + '</span></td>' +
                '<td class="td-action">' +
                    '<button type="button" class="btn btn-xs btn-default action-btn command-edit" data-row-id="' + escHtml(row.id) + '" title="Edit" aria-label="Edit">' + svgIcon('edit') + '</button> ' +
                    '<button type="button" class="btn btn-xs btn-default action-btn command-delete" data-row-id="' + escHtml(row.id) + '" title="Delete" aria-label="Delete">' + svgIcon('delete') + '</button>' +
                '</td></tr>';
        }

        function renderPagination(total, current, rowCount) {
            if (rowCount === -1 || total === 0) { $('#tbl-pagination').empty(); return; }
            var pages = Math.ceil(total / rowCount);
            if (pages <= 1) { $('#tbl-pagination').empty(); return; }
            var html = '';
            html += '<li class="' + (current <= 1 ? 'disabled' : '') + '"><a href="#" data-page="' + (current - 1) + '">&laquo;</a></li>';
            var start = Math.max(1, current - 2), end = Math.min(pages, current + 2);
            if (start > 1) { html += '<li><a href="#" data-page="1">1</a></li>' + (start > 2 ? '<li class="disabled"><span>…</span></li>' : ''); }
            for (var p = start; p <= end; p++) {
                html += '<li class="' + (p === current ? 'active' : '') + '"><a href="#" data-page="' + p + '">' + p + '</a></li>';
            }
            if (end < pages) { html += (end < pages - 1 ? '<li class="disabled"><span>…</span></li>' : '') + '<li><a href="#" data-page="' + pages + '">' + pages + '</a></li>'; }
            html += '<li class="' + (current >= pages ? 'disabled' : '') + '"><a href="#" data-page="' + (current + 1) + '">&raquo;</a></li>';
            $('#tbl-pagination').html(html);
            $('#tbl-pagination').off('click', 'a').on('click', 'a', function(e) {
                e.preventDefault();
                var pg = parseInt($(this).data('page'));
                if (!isNaN(pg) && pg >= 1 && pg <= pages && pg !== state.current) {
                    state.current = pg; loadData();
                }
            });
        }

        function bindRowActions() {
            $('#tbl_trackers').find('.command-toggle-pass').off('click').on('click', function() {
                var span = $(this).siblings('.td-pass');
                if (span.attr('data-shown') === '1') {
                    span.text('••••••').attr('data-shown', '0');
                    $(this).html(svgIcon('eye'));
                } else {
                    var v = span.attr('data-pass') || '';
                    span.text(v === '' ? '—' : v).attr('data-shown', '1');
                    $(this).html(svgIcon('eye-off'));
                }
            });
            $('#tbl_trackers').find('.command-edit').off('click').on('click', function() {
                var rowId = $(this).data('row-id');
                var row = null;
                for (var i = 0; i < state.rows.length; i++) {
                    if (String(state.rows[i].id) === String(rowId)) { row = state.rows[i]; break; }
                }
                if (!row) { Swal.fire({ allowOutsideClick: false, type: 'error', title: 'Oops...', text: 'No row selected.' }); return; }
                $('#edit_id').val(row.id);
                $('#edit_sub_id').val(row.sub_id);
                $('#edit_password').val('');
                $('#edit_gen_url').val(row.gen_url);
                $('#edit_sm_url').val(row.sm_url);
                $('#edit_model').modal('show');
            });
            $('#tbl_trackers').find('.command-delete').off('click').on('click', function() {
                var rowId = $(this).data('row-id');
                Swal.fire({
                    title: 'Are you sure?',
                    text: "You won't be able to revert this!",
                    type: 'warning',
                    showCancelButton: true,
                    allowOutsideClick: false,
                    confirmButtonColor: '#6c757d',
                    cancelButtonColor: '#6c757d',
                    confirmButtonText: 'Yes, delete it!'
                }).then(function(result) {
                    if (result.value) {
                        $.ajax({
                            type: 'POST', url: 'response.php',
                            data: { id: rowId, action: 'delete', csrf_token: adminPanelCsrfToken },
                            dataType: 'json',
                            success: function() {
                                Swal.fire('Deleted!', 'Your file has been deleted.', 'success');
                                loadData();
                            },
                            error: handleAjaxFailure
                        });
                    }
                });
            });
        }

        function loadData() {
            $.ajax({
                type: 'POST', url: 'response.php',
                data: { current: state.current, rowCount: state.rowCount, searchPhrase: state.search },
                dataType: 'json',
                success: function(data) {
                    state.rows = data.rows || [];
                    var total = data.total || 0;
                    var html = '';
                    for (var i = 0; i < state.rows.length; i++) { html += renderRow(state.rows[i], i); }
                    $('#tbl_trackers tbody').html(html || '<tr><td colspan="6" class="td-empty">No data</td></tr>');
                    var from = total === 0 ? 0 : ((state.current - 1) * (state.rowCount === -1 ? total : state.rowCount)) + 1;
                    var to = state.rowCount === -1 ? total : Math.min(state.current * state.rowCount, total);
                    $('#tbl-info').text('Showing ' + from + ' to ' + to + ' of ' + total + ' entries');
                    renderPagination(total, state.current, state.rowCount);
                    bindRowActions();
                },
                error: handleAjaxFailure
            });
        }

        var searchTimer;
        $('#tbl-search').on('input', function() {
            clearTimeout(searchTimer);
            var val = $(this).val();
            searchTimer = setTimeout(function() { state.search = val; state.current = 1; loadData(); }, 300);
        });
        $('#tbl-rowcount').on('change', function() {
            state.rowCount = parseInt(this.value); state.current = 1; loadData();
        });
        (function(){var btn=document.getElementById('ddt-rowcount-btn'),menu=document.getElementById('ddt-rowcount-menu'),inp=document.getElementById('tbl-rowcount');if(!btn||!menu||!inp)return;btn.addEventListener('click',function(e){e.stopPropagation();menu.classList.toggle('open');});menu.addEventListener('click',function(e){var a=e.target.closest?e.target.closest('a'):(e.target.tagName==='A'?e.target:null);if(!a)return;e.preventDefault();var v=a.getAttribute('data-val');menu.querySelectorAll('a').forEach(function(el){el.classList.remove('ddt-active');});a.classList.add('ddt-active');btn.querySelector('.ddt-label').textContent=a.textContent;inp.value=v;menu.classList.remove('open');$(inp).trigger('change');});document.addEventListener('click',function(){menu.classList.remove('open');});})();

        function ajaxAction(action) {
            var data = $("#frm_" + action).serializeArray();
            data.push({ name: 'csrf_token', value: adminPanelCsrfToken });
            $.ajax({
                type: 'POST', url: 'response.php',
                data: data, dataType: 'json',
                success: function() {
                    $('#' + action + '_model').modal('hide');
                    loadData();
                },
                error: handleAjaxFailure
            });
        }

        $("#command-add").on('click', function() {
            $("#sub_id").val('');
            $("#password").val(btoa(+new Date).substr(-7, 5));
            $("#gen_url").val('');
            $("#sm_url").val(<?= $teamJs; ?>);
            $('#add_model').modal('show');
        });
        $("#btn_add").on('click', function() {
            if ($.trim($("#sub_id").val()) === "" || $.trim($("#gen_url").val()) === "") {
                Swal.fire({ allowOutsideClick: false, type: 'error', title: 'Oops...', text: 'Something went wrong! {Required all fields}' });
                return false;
            }
            ajaxAction('add');
        });
        $("#btn_edit").on('click', function() { ajaxAction('edit'); });

        $('#sub_id, #edit_sub_id').on('keypress', function(event) {
            var regex = new RegExp("^[a-zA-Z0-9]+$");
            var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
            if (!regex.test(key)) { event.preventDefault(); return false; }
        });

        loadData();
    });
    document.addEventListener('click', function(e) {
        var target = e.target;
        var btn = target && typeof target.closest === 'function' ? target.closest('#btn-logout') : null;

        if (!btn) {
            return;
        }

        e.preventDefault();

        var href = btn.getAttribute('href') || btn.getAttribute('data-href') || '/logout';
        var toast = document.getElementById('logout-toast');

        if (toast) {
            toast.classList.add('show');
        }

        window.setTimeout(function() {
            window.location.href = href;
        }, 900);
    });
    </script>
<style nonce="<?= adminEsc($nonce); ?>">#logout-toast{position:fixed;right:14px;bottom:14px;z-index:10060;display:none;padding:9px 12px;border:1px solid var(--line);border-left:3px solid var(--accent);border-radius:var(--radius);background:var(--panel2);color:var(--text);box-shadow:0 2px 12px rgba(0,0,0,.10);font-size:12px}#logout-toast.show{display:block;animation:_t-in .16s ease both}input:focus,textarea:focus,select:focus,button:focus,.form-control:focus,.btn:focus,a:focus{outline:none!important}/* ── Extended components ── */.card{border:1px solid var(--line);background:var(--panel);border-radius:var(--radius)}.btn-ghost{background:var(--faded)!important;color:var(--inverse)!important;border-color:var(--line)!important;box-shadow:none!important}.btn-ghost:hover{background:var(--panel-soft)!important;color:var(--text-strong)!important}.chip{display:inline-flex;align-items:center;padding:2px 8px;border:1px solid var(--line-soft);background:var(--panel-soft);border-radius:var(--radius);font-size:11px;color:var(--text);line-height:1.4}.toast{position:fixed;right:18px;bottom:18px;z-index:10070;display:none;align-items:center;gap:8px;background:var(--inverse);color:var(--faded);padding:10px 12px;font-size:12px;border:1px solid var(--line-soft);border-radius:var(--radius);box-shadow:var(--shadow-modal)}.toast.show{display:flex}canvas{display:block;width:100%;height:430px;border:1px solid var(--line);background:var(--panel);border-radius:var(--radius)}</style>
<style nonce="<?= adminEsc($nonce); ?>">.admin-toast-root{right:14px!important;bottom:14px!important;gap:6px!important;z-index:10070!important}.admin-toast{font-family:var(--mono, ui-monospace, SFMono-Regular, Menlo, Consolas, monospace);position:relative!important;display:grid!important;grid-template-columns:3px minmax(0,1fr)!important;column-gap:9px!important;align-items:start!important;min-width:190px!important;max-width:320px!important;padding:8px 10px 8px 0!important;border:1px solid var(--line)!important;border-radius:var(--radius)!important;background:#fff!important;color:var(--text)!important;box-shadow:0 4px 14px rgba(31,34,33,.08)!important;font-size:12px!important;line-height:1.35!important}.admin-toast:before{content:"";grid-row:1/3;width:3px;align-self:stretch;background:var(--muted);border-radius:var(--radius) 0 0 var(--radius)}.admin-toast strong{margin:0 0 1px!important;color:var(--text-strong)!important;font-size:12px!important;line-height:1.3}.admin-toast span{min-width:0;color:var(--text)!important;opacity:.84;word-break:break-word}.admin-toast--success:before{background:var(--success)!important}.admin-toast--error:before{background:var(--danger)!important}.admin-toast--info:before{background:var(--muted)!important}.admin-toast--removing{animation:_t-out .14s ease both!important}.toast{background:#fff!important;color:var(--text)!important;border:1px solid var(--line)!important;box-shadow:0 4px 14px rgba(31,34,33,.08)!important}#logout-toast{right:14px!important;bottom:14px!important;padding:8px 10px!important;border:1px solid var(--line)!important;border-left:3px solid var(--accent)!important;border-radius:var(--radius)!important;background:#fff!important;color:var(--text-strong)!important;box-shadow:0 4px 14px rgba(31,34,33,.08)!important;font-size:12px!important;line-height:1.35!important}</style>
<style nonce="<?= adminEsc($nonce); ?>">.command-toggle-pass,.command-toggle-pass:hover,.command-toggle-pass:focus,.command-toggle-pass:active{border:0!important;background:transparent!important;box-shadow:none!important;color:var(--text)!important}.command-toggle-pass{width:24px!important;height:24px!important;min-width:24px!important;min-height:24px!important;padding:0!important;margin:0 0 0 4px!important;vertical-align:middle!important}.command-toggle-pass:hover,.command-toggle-pass:focus{color:var(--text-strong)!important}.command-toggle-pass .action-ico,.command-toggle-pass svg{width:15px!important;height:15px!important;min-width:15px!important;min-height:15px!important}
/* rows-per-page (.ddt) dropdown fix: tbl-toolbar has backdrop-filter (a stacking context) that traps the menu's z-index behind the table (z-index:3); lift the whole toolbar above the table so the open menu renders on top */
.tbl-toolbar{position:relative!important;z-index:30!important}</style>
<div id="logout-toast">Logging out…</div>
<style nonce="<?= adminEsc($nonce); ?>">
/* NGIX patch: fullscreen background/backdrop override */
html,body{min-height:100svh!important}
body{background-color:var(--bg,#f8fafc)!important;background-attachment:fixed!important}
body::after{content:""!important;position:fixed!important;inset:0!important;width:100vw!important;min-width:100vw!important;height:100vh!important;height:100dvh!important;z-index:0!important;pointer-events:none!important;background-image:url('/assets/favicon.svg'),url('/assets/bg.png')!important;background-repeat:no-repeat,no-repeat!important;background-position:right 14px bottom 12px,center center!important;background-size:28px 28px,cover!important;opacity:.30!important;filter:saturate(.68) contrast(.84) brightness(1.03) blur(.32px)!important;transform:translateZ(0)!important}
body>.container,body>.app-shell,body>.navbar,body>.panel,body>.panel_m,main,.container,.navbar,.panel,.panel_m,.app-footer{position:relative;z-index:1}
.panel_m.panel>.ngix-image-backdrop{position:fixed!important;inset:0!important;width:100vw!important;min-width:100vw!important;height:100vh!important;height:100dvh!important;z-index:0!important;pointer-events:none!important;background-repeat:no-repeat,no-repeat!important;background-position:center center,center center!important;background-size:100% 100%,cover!important;opacity:.40!important;filter:saturate(.72) contrast(.86) blur(.22px)!important;transform:translateZ(0)!important}
.panel_m.panel>.panel-heading,.panel_m.panel>.panel-body{position:relative!important;z-index:2!important}
@media (prefers-color-scheme:dark){body::after{opacity:.24!important;filter:saturate(.62) contrast(.78) brightness(.88) blur(.34px)!important}.panel_m.panel>.ngix-image-backdrop{opacity:.34!important;filter:saturate(.66) contrast(.80) blur(.24px)!important}}
@media screen and (max-width:768px){body::after{background-position:right 10px bottom 10px,center center!important;background-size:24px 24px,cover!important;opacity:.24!important}.panel_m.panel>.ngix-image-backdrop{background-size:100% 100%,cover!important;opacity:.30!important}}

/* NGIX patch: viewport/navbar correction */
html{width:100%!important;min-width:100%!important;max-width:none!important;min-height:100%!important;margin:0!important;padding:0!important;overflow-x:hidden!important;background:var(--bg,#f8fafc)!important;zoom:1!important}
body{width:100%!important;min-width:100vw!important;max-width:none!important;min-height:100vh!important;min-height:100svh!important;min-height:100dvh!important;margin:0!important;overflow-x:hidden!important;background-color:var(--bg,#f8fafc)!important;background-attachment:fixed!important}
body::after{top:0!important;right:0!important;bottom:0!important;left:0!important;inset:0!important;width:100vw!important;min-width:100vw!important;max-width:none!important;height:100vh!important;height:100svh!important;height:100dvh!important;background-size:28px 28px,cover!important;background-position:right 14px bottom 12px,center center!important}
.navbar.navbar-fixed-top,.navbar-fixed-top{position:fixed!important;top:0!important;right:0!important;left:0!important;width:100vw!important;max-width:none!important;margin:0!important;border-radius:0!important;transform:none!important;z-index:1055!important}
body>.navbar.navbar-fixed-top{position:fixed!important;z-index:1055!important}.navbar.navbar-default.navbar-fixed-top{top:0!important}
.panel_m.panel{overflow:visible!important;isolation:auto!important}.panel_m.panel>.ngix-image-backdrop,.ngix-image-backdrop{position:fixed!important;top:0!important;right:0!important;bottom:0!important;left:0!important;width:100vw!important;min-width:100vw!important;max-width:none!important;height:100vh!important;height:100svh!important;height:100dvh!important;z-index:0!important;background-size:100% 100%,cover!important;background-position:center center,center center!important;pointer-events:none!important}.panel_m.panel>.panel-heading,.panel_m.panel>.panel-body,.panel_m.panel>.table-responsive{position:relative!important;z-index:2!important}
@supports (height:100lvh){body{min-height:100lvh!important}body::after,.panel_m.panel>.ngix-image-backdrop,.ngix-image-backdrop{height:100lvh!important;min-height:100lvh!important}}
@media screen and (max-width:768px){html{zoom:1!important}.navbar.navbar-fixed-top,.navbar-fixed-top{top:0!important;width:100vw!important}.panel_m.panel>.ngix-image-backdrop,.ngix-image-backdrop{width:100vw!important;height:100svh!important;height:100dvh!important;min-height:100svh!important;background-size:100% 100%,cover!important}}
</style>
<style nonce="<?= adminEsc($nonce); ?>">
/* NGIX patch: disable image/background right-click */
img,picture,svg,canvas,.ngix-image-backdrop,.image-protect,.app-banner,.brand-logo,.logo{
  -webkit-user-drag:none!important;
  user-drag:none!important;
  -webkit-touch-callout:none!important;
  -webkit-user-select:none!important;
  user-select:none!important;
}
</style>
<script nonce="<?= adminEsc($nonce); ?>">
/* NGIX patch: disable image/background right-click */
(function(){
    'use strict';

    var protectedSelector = 'img,picture,svg,canvas,.ngix-image-backdrop,.image-protect,.app-banner,.brand-logo,.logo';
    var editableSelector = 'input,textarea,select,[contenteditable="true"],[contenteditable=""]';

    function closest(node, selector) {
        return node && typeof node.closest === 'function' ? node.closest(selector) : null;
    }

    function hasProtectedBackground(node) {
        var current = node && node.nodeType === 1 ? node : null;

        while (current && current !== document.documentElement) {
            if (current.classList && (
                current.classList.contains('ngix-image-backdrop')
                || current.classList.contains('image-protect')
                || current.classList.contains('app-banner')
                || current.classList.contains('brand-logo')
            )) {
                return true;
            }

            var backgroundImage = window.getComputedStyle(current).backgroundImage;
            if (backgroundImage && backgroundImage !== 'none' && backgroundImage.indexOf('url(') !== -1) {
                return true;
            }

            if (current === document.body) {
                break;
            }

            current = current.parentElement;
        }

        return false;
    }

    function isProtectedTarget(node) {
        if (!node || closest(node, editableSelector)) {
            return false;
        }

        return !!closest(node, protectedSelector) || hasProtectedBackground(node);
    }

    function blockImageAction(event) {
        if (!isProtectedTarget(event.target)) {
            return;
        }

        event.preventDefault();
        event.stopPropagation();
    }

    function hardenImages() {
        document.querySelectorAll('img,picture,svg,canvas').forEach(function(node) {
            node.setAttribute('draggable', 'false');
        });
    }

    document.addEventListener('contextmenu', blockImageAction, true);
    document.addEventListener('dragstart', blockImageAction, true);

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', hardenImages, {once: true});
        return;
    }

    hardenImages();
}());
</script>

<style nonce="<?= adminEsc($nonce); ?>">
/* NGIX patch: public modal/backdrop/logout spinner correction */
html {
  zoom: 1 !important;
  width: 100% !important;
  min-width: 100% !important;
  overflow-x: hidden !important;
}
body {
  width: 100% !important;
  min-width: 100vw !important;
  max-width: none !important;
  overflow-x: hidden !important;
}
body.modal-open {
  overflow: hidden !important;
  padding-right: 0 !important;
}
.modal-backdrop {
  position: fixed !important;
  inset: 0 !important;
  width: 100vw !important;
  max-width: none !important;
  height: 100vh !important;
  height: 100dvh !important;
  margin: 0 !important;
  background: #000 !important;
  z-index: 1060 !important;
}
.modal-backdrop.in {
  opacity: .48 !important;
}
.modal {
  position: fixed !important;
  inset: 0 !important;
  width: 100vw !important;
  max-width: none !important;
  height: 100vh !important;
  height: 100dvh !important;
  margin: 0 !important;
  padding: 58px 12px 14px !important;
  overflow-x: hidden !important;
  overflow-y: auto !important;
  text-align: center !important;
  z-index: 1070 !important;
  -webkit-overflow-scrolling: touch !important;
}
.modal.fade .modal-dialog,
.modal.in .modal-dialog {
  transform: none !important;
}
.modal-dialog {
  display: block !important;
  width: auto !important;
  max-width: min(560px, calc(100vw - 24px)) !important;
  margin: 0 auto !important;
  text-align: left !important;
}
.modal-wide {
  max-width: min(860px, calc(100vw - 24px)) !important;
}
#cf_config_modal .modal-dialog {
  max-width: min(440px, calc(100vw - 24px)) !important;
}
#cf_ns_modal .modal-dialog {
  max-width: min(480px, calc(100vw - 24px)) !important;
}
.modal-content {
  width: 100% !important;
  max-height: calc(100dvh - 76px) !important;
  display: flex !important;
  flex-direction: column !important;
  overflow: hidden !important;
}
.modal-header,
.modal-footer {
  flex: 0 0 auto !important;
}
.modal-body {
  flex: 1 1 auto !important;
  overflow-y: auto !important;
  max-height: calc(100dvh - 158px) !important;
}
.modal-footer {
  display: flex !important;
  align-items: center !important;
  justify-content: flex-end !important;
  gap: 7px !important;
  flex-wrap: wrap !important;
}
.navbar.navbar-fixed-top,
.navbar-fixed-top {
  position: fixed !important;
  top: 0 !important;
  right: 0 !important;
  left: 0 !important;
  width: 100% !important;
  max-width: 100% !important;
  border-bottom: 1px solid var(--line) !important;
  margin: 0 !important;
  padding-right: 0 !important;
  z-index: 1055 !important;
}
#btn-logout,
.logout-btn {
  display: inline-flex !important;
  align-items: center !important;
  justify-content: center !important;
  gap: 6px !important;
  min-height: 31px !important;
  text-decoration: none !important;
  white-space: nowrap !important;
}
.navbar-nav > li > #btn-logout,
.navbar-nav > li > .logout-btn {
  height: 44px !important;
  padding: 12px 10px !important;
  margin: 0 !important;
  border: 0 !important;
  background: transparent !important;
  color: var(--text) !important;
}
.navbar-nav > li > #btn-logout:hover,
.navbar-nav > li > .logout-btn:hover {
  background: transparent !important;
  color: var(--text-strong) !important;
}
#btn-logout.is-loading,
.logout-btn.is-loading,
.btn.is-loading {
  pointer-events: none !important;
  opacity: .78 !important;
}
.btn-spin {
  display: inline-block !important;
  flex: 0 0 auto !important;
  vertical-align: -2px !important;
  animation: btn-spin .7s linear infinite !important;
}
@keyframes btn-spin {
  to { transform: rotate(360deg); }
}
@supports (height: 100lvh) {
  .modal,
  .modal-backdrop {
    height: 100lvh !important;
  }
  .modal-content {
    max-height: calc(100lvh - 76px) !important;
  }
  .modal-body {
    max-height: calc(100lvh - 158px) !important;
  }
}
@media screen and (max-width: 560px) {
  .modal {
    padding: 50px 8px 10px !important;
  }
  .modal-dialog,
  .modal-wide,
  #cf_config_modal .modal-dialog,
  #cf_ns_modal .modal-dialog {
    max-width: calc(100vw - 16px) !important;
  }
  .modal-content {
    max-height: calc(100dvh - 60px) !important;
  }
  .modal-body {
    max-height: calc(100dvh - 138px) !important;
  }
}
</style>

<script nonce="<?= adminEsc($nonce); ?>">
/* NGIX patch: public logout/button spinner correction */
(function() {
    'use strict';

    var spinner = '<svg class="btn-spin" viewBox="0 0 16 16" width="12" height="12" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" aria-hidden="true"><circle cx="8" cy="8" r="6" stroke-opacity=".3"></circle><path d="M8 2a6 6 0 0 1 6 6"></path></svg> ';

    function closest(node, selector) {
        return node && typeof node.closest === 'function' ? node.closest(selector) : null;
    }

    function setLoading(button, label) {
        if (!button || button.dataset.ngixLoading === '1') {
            return;
        }

        button.dataset.ngixLoading = '1';
        button.dataset.ngixOriginalHtml = button.innerHTML;
        button.classList.add('is-loading');
        button.setAttribute('aria-busy', 'true');
        button.innerHTML = spinner + label;
    }

    function ensureLogoutToast() {
        var toast = document.getElementById('logout-toast');
        if (toast) {
            return toast;
        }

        toast = document.createElement('div');
        toast.id = 'logout-toast';
        toast.textContent = 'Logging out…';
        document.body.appendChild(toast);
        return toast;
    }

    document.addEventListener('click', function(event) {
        var button = closest(event.target, '#btn-logout,.logout-btn');
        if (!button) {
            return;
        }

        var href = button.getAttribute('href') || button.getAttribute('data-href') || '?logout';
        if (href === '#' || href === '') {
            return;
        }

        event.preventDefault();
        event.stopImmediatePropagation();

        if (button.dataset.ngixSubmitting === '1') {
            return;
        }

        button.dataset.ngixSubmitting = '1';
        setLoading(button, 'Logout…');

        var toast = ensureLogoutToast();
        toast.classList.add('show');

        window.setTimeout(function() {
            window.location.href = href;
        }, 650);
    }, true);
}());
</script>

<style nonce="<?= adminEsc($nonce); ?>">
/* NGIX patch: public panel backdrop deglitch */
.panel.panel-default,
.panel:has(> .ngix-image-backdrop) {
  position: relative !important;
  overflow: hidden !important;
  isolation: isolate !important;
}
.panel.panel-default > .ngix-image-backdrop,
.panel > .ngix-image-backdrop {
  position: absolute !important;
  inset: 0 !important;
  width: 100% !important;
  min-width: 0 !important;
  max-width: 100% !important;
  height: 100% !important;
  min-height: 100% !important;
  max-height: none !important;
  z-index: 0 !important;
  pointer-events: none !important;
  background-repeat: no-repeat, no-repeat !important;
  background-position: center center, center center !important;
  background-size: 100% 100%, min(980px, 120%) auto !important;
  opacity: .24 !important;
  filter: saturate(.72) contrast(.86) blur(.18px) !important;
  transform: none !important;
}
.panel.panel-default > *:not(.ngix-image-backdrop),
.panel:has(> .ngix-image-backdrop) > *:not(.ngix-image-backdrop) {
  position: relative !important;
  z-index: 2 !important;
}
.modal,
.modal * {
  isolation: auto;
}
@media screen and (max-width: 768px) {
  .panel.panel-default > .ngix-image-backdrop,
  .panel > .ngix-image-backdrop {
    background-size: 100% 100%, min(760px, 135%) auto !important;
    opacity: .20 !important;
  }
}
</style>
<style nonce="<?= adminEsc($nonce); ?>">
/* NGIX patch: public modal/dropdown z-order final fix */
html,
body {
  overflow-x: hidden !important;
}
body.modal-open {
  overflow: hidden !important;
  padding-right: 0 !important;
}
/* Bootstrap backdrop is attached to body. Keep it below the modal, but above the page. */
.modal-backdrop {
  position: fixed !important;
  inset: 0 !important;
  width: 100vw !important;
  max-width: none !important;
  height: 100vh !important;
  height: 100dvh !important;
  margin: 0 !important;
  background: #000 !important;
  z-index: 12000 !important;
}
.modal-backdrop.in,
.modal-backdrop.fade.in {
  opacity: .48 !important;
}
/* Modals are moved to body by JS below; this prevents parent stacking contexts from putting the backdrop above them. */
.modal {
  position: fixed !important;
  inset: 0 !important;
  width: 100vw !important;
  max-width: none !important;
  height: 100vh !important;
  height: 100dvh !important;
  margin: 0 !important;
  padding: 58px 12px 14px !important;
  overflow-x: hidden !important;
  overflow-y: auto !important;
  text-align: center !important;
  z-index: 12010 !important;
  -webkit-overflow-scrolling: touch !important;
}
.modal.in,
.modal.show {
  display: block !important;
}
.modal.fade .modal-dialog,
.modal.in .modal-dialog,
.modal.show .modal-dialog {
  transform: none !important;
}
.modal-dialog {
  position: relative !important;
  z-index: 12020 !important;
  display: block !important;
  width: auto !important;
  max-width: min(560px, calc(100vw - 24px)) !important;
  margin: 0 auto !important;
  text-align: left !important;
}
.modal-wide {
  max-width: min(860px, calc(100vw - 24px)) !important;
}
#cf_config_modal .modal-dialog {
  max-width: min(440px, calc(100vw - 24px)) !important;
}
#cf_ns_modal .modal-dialog {
  max-width: min(480px, calc(100vw - 24px)) !important;
}
.modal-content {
  position: relative !important;
  z-index: 12030 !important;
  width: 100% !important;
  max-height: none !important;
  display: block !important;
  overflow: visible !important;
}
.modal-header,
.modal-footer,
.modal-body {
  position: relative !important;
}
.modal-body {
  max-height: none !important;
  overflow: visible !important;
}
.modal-footer {
  display: flex !important;
  align-items: center !important;
  justify-content: flex-end !important;
  gap: 7px !important;
  flex-wrap: wrap !important;
}
/* Dropdowns must not be clipped by panel/modal overflow. */
.panel,
.panel-default,
.panel.panel-default,
.panel:has(> .ngix-image-backdrop),
.modal-content,
.modal-body,
.bootgrid-header,
.bootgrid-header .actionBar,
.bootgrid-header .actions,
.tbl-toolbar,
.ddt,
.dropdown,
.btn-group {
  overflow: visible !important;
}
.tbl-toolbar,
.bootgrid-header,
.bootgrid-header .actionBar,
.bootgrid-header .actions,
.ddt.open,
.dropdown.open,
.btn-group.open {
  position: relative !important;
  z-index: 12100 !important;
}
.dropdown-menu,
.open > .dropdown-menu,
.ddt-menu,
.ddt-menu.open,
.modal .dropdown-menu,
.modal .open > .dropdown-menu,
.modal .ddt-menu,
.modal .ddt-menu.open,
.bootgrid-header .actions .dropdown-menu,
.bootgrid-header .actions .open > .dropdown-menu {
  z-index: 12120 !important;
  overflow: visible !important;
}
.modal .dropdown-menu,
.modal .ddt-menu,
.modal .ddt-menu.open {
  z-index: 12140 !important;
}
/* The decorative panel backdrop stays scoped to its panel and never sits above controls/dropdowns. */
.panel.panel-default > .ngix-image-backdrop,
.panel > .ngix-image-backdrop {
  position: absolute !important;
  inset: 0 !important;
  width: 100% !important;
  min-width: 0 !important;
  max-width: 100% !important;
  height: 100% !important;
  min-height: 100% !important;
  z-index: 0 !important;
  pointer-events: none !important;
  background-repeat: no-repeat, no-repeat !important;
  background-position: center center, center center !important;
  background-size: 100% 100%, min(980px, 120%) auto !important;
  opacity: .22 !important;
  filter: saturate(.72) contrast(.86) blur(.18px) !important;
  transform: none !important;
}
.panel.panel-default > *:not(.ngix-image-backdrop),
.panel:has(> .ngix-image-backdrop) > *:not(.ngix-image-backdrop) {
  position: relative !important;
  z-index: 2 !important;
}
#logout-toast,
.admin-toast-root,
.toast {
  z-index: 13000 !important;
}
@supports (height: 100lvh) {
  .modal,
  .modal-backdrop {
    height: 100lvh !important;
  }
}
@media screen and (max-width: 560px) {
  .modal {
    padding: 50px 8px 10px !important;
  }
  .modal-dialog,
  .modal-wide,
  #cf_config_modal .modal-dialog,
  #cf_ns_modal .modal-dialog {
    max-width: calc(100vw - 16px) !important;
  }
}
</style>
<script nonce="<?= adminEsc($nonce); ?>">
/* NGIX patch: move public modals to body to avoid backdrop covering modal */
(function() {
    'use strict';

    function moveModalsToBody() {
        document.querySelectorAll('.modal').forEach(function(modal) {
            if (modal.parentElement !== document.body) {
                document.body.appendChild(modal);
            }
        });
    }

    function normalizeDropdowns() {
        document.addEventListener('click', function(event) {
            var button = event.target && typeof event.target.closest === 'function'
                ? event.target.closest('.ddt-btn,[data-toggle="dropdown"]')
                : null;

            if (!button) {
                return;
            }

            var holder = button.closest('.ddt,.dropdown,.btn-group,.actions');
            if (holder) {
                holder.style.position = 'relative';
                holder.style.zIndex = '12100';
            }
        }, true);
    }

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', function() {
            moveModalsToBody();
            normalizeDropdowns();
        }, {once: true});
        return;
    }

    moveModalsToBody();
    normalizeDropdowns();
}());
</script>
</body></html>
