<?php
require_once __DIR__ . '/auth.php';

$session_user = current_user();
if (is_logged_in()) {
    if ($session_user && (int)($session_user['is_active'] ?? 0) === 1 && !user_panel_is_expired($session_user)) {
        header('Location: list_dns.php');
        exit;
    }
    logout_user();
}

$branding = get_branding();
$logoUrl = trim((string)($branding['logo_url'] ?? ''));
$bgUrl = trim((string)($branding['background_url'] ?? ''));
$panelLogo = 'uploads/panel_tvexpress_logo.png';
$panelBackground = 'uploads/panel_tvexpress_bg.jpg';

$error = isset($_GET['expired']) ? 'Painel VENCIDO, Renove AGORA com Admin!' : '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = str_in($_POST['username'] ?? '');
    $password = $_POST['password'] ?? '';

    if (empty($username) || empty($password)) {
        $error = 'Preencha todos os campos.';
    } else {
        $login = login_user($username, $password);
        if (!empty($login['ok'])) {
            header('Location: list_dns.php');
            exit;
        }
        $error = $login['message'] ?? 'Usuario ou senha invalidos.';
    }
}
?>
<!DOCTYPE html>
<html lang="pt-BR">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover">
    <meta name="theme-color" content="#2c075f">
    <title>Login - Painel VODs Express</title>

    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        :root {
            --purple-1: #140026;
            --purple-2: #2b075a;
            --purple-3: #8d2cff;
            --purple-4: #b85cff;
            --green: #7CFF6B;
            --text: #ffffff;
            --muted: #d7c7f6;
            --border: rgba(223,205,255,.18);
            --shadow: 0 28px 80px rgba(0,0,0,.42);
        }

        html,
        body {
            min-height: 100%;
        }

        body {
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 22px;
            color: var(--text);
            font-family: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
            background:
                radial-gradient(circle at 50% 28%, rgba(184,92,255,.32), transparent 32%),
                linear-gradient(135deg, rgba(17,0,33,.88), rgba(65,0,125,.62)),
                url('<?php echo htmlspecialchars($panelBackground, ENT_QUOTES, 'UTF-8'); ?>') center center / cover fixed no-repeat,
                #140026;
            overflow-x: hidden;
        }

        body::before,
        body::after {
            content: "";
            position: fixed;
            pointer-events: none;
            z-index: 0;
        }

        body::before {
            inset: 0;
            background:
                radial-gradient(circle at 12% 22%, rgba(124,255,107,.08), transparent 18%),
                radial-gradient(circle at 88% 78%, rgba(184,92,255,.14), transparent 24%),
                linear-gradient(90deg, rgba(0,0,0,.34), transparent 48%, rgba(0,0,0,.28));
        }

        body::after {
            width: 160px;
            height: 160px;
            right: 9%;
            bottom: 12%;
            border-radius: 999px;
            background: rgba(124,255,107,.10);
            filter: blur(48px);
        }

        .login-shell {
            position: relative;
            z-index: 1;
            width: 100%;
            max-width: 440px;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .login-container {
            width: 100%;
            padding: 30px;
            border-radius: 30px;
            background: rgba(23, 3, 50, .82);
            border: 1px solid rgba(223,205,255,.24);
            box-shadow: var(--shadow);
            backdrop-filter: blur(18px);
            animation: slideUp .45s ease;
        }

        @keyframes slideUp {
            from {
                opacity: 0;
                transform: translateY(22px) scale(.985);
            }

            to {
                opacity: 1;
                transform: translateY(0) scale(1);
            }
        }

        .login-header {
            text-align: center;
            margin-bottom: 26px;
        }

        .logo-box {
            width: 80px;
            height: 80px;
            margin: 0 auto 14px;
            display: grid;
            place-items: center;
            border-radius: 28px;
            background: rgba(255,255,255,.06);
            border: 1px solid rgba(255,255,255,.12);
            box-shadow:
                inset 0 1px 0 rgba(255,255,255,.08),
                0 18px 45px rgba(0,0,0,.28);
        }

        .logo-box img {
            width: 100%;
            height: 100%;
            object-fit: contain;
            border-radius: 24px;
        }

        .login-header h1 {
            font-size: 28px;
            line-height: 1.2;
            margin-bottom: 8px;
        }

        .login-header p {
            color: var(--muted);
            font-size: 14px;
        }

        .error-message {
            background: rgba(255, 67, 105, .15);
            color: #ffd7df;
            padding: 13px 15px;
            border-radius: 16px;
            margin-bottom: 18px;
            font-size: 14px;
            border: 1px solid rgba(255, 67, 105, .30);
        }

        .form-group {
            margin-bottom: 16px;
        }

        .form-group label {
            display: flex;
            gap: 8px;
            align-items: center;
            margin-bottom: 9px;
            color: #fff;
            font-weight: 900;
            font-size: 14px;
        }

        .input-wrap {
            display: flex;
            align-items: center;
            gap: 10px;
            min-height: 54px;
            border: 1px solid rgba(223,205,255,.28);
            border-radius: 17px;
            background: rgba(0,0,0,.25);
            padding: 0 14px;
            transition: .18s ease;
            overflow: hidden;
        }

        .input-wrap:focus-within {
            border-color: rgba(184,92,255,.85);
            box-shadow: 0 0 0 4px rgba(184,92,255,.18);
            background: rgba(255,255,255,.06);
        }

        .input-icon {
            color: var(--green);
            font-size: 18px;
            width: 24px;
            text-align: center;
            flex-shrink: 0;
        }

        input {
            width: 100%;
            min-height: 52px;
            background: transparent !important;
            border: 0;
            color: #fff !important;
            outline: none;
            font-size: 15px;
            box-shadow: none !important;
            caret-color: #fff;
        }

        input::placeholder {
            color: rgba(215,199,246,.62);
        }

        input:-webkit-autofill,
        input:-webkit-autofill:hover,
        input:-webkit-autofill:focus,
        input:-webkit-autofill:active {
            -webkit-text-fill-color: #ffffff !important;
            caret-color: #ffffff !important;
            box-shadow: 0 0 0 1000px transparent inset !important;
            -webkit-box-shadow: 0 0 0 1000px rgba(0,0,0,0) inset !important;
            transition: background-color 9999s ease-in-out 0s;
        }

        .login-btn {
            width: 100%;
            min-height: 56px;
            margin-top: 8px;
            background: linear-gradient(135deg, #b85cff 0%, #8d2cff 48%, #5a18cd 100%);
            color: #fff;
            border: 1px solid rgba(255,255,255,.18);
            border-radius: 18px;
            font-size: 16px;
            font-weight: 950;
            letter-spacing: .02em;
            cursor: pointer;
            box-shadow: 0 18px 42px rgba(141,44,255,.34);
            transition: transform .18s ease, box-shadow .18s ease;
        }

        .login-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 22px 55px rgba(141,44,255,.45);
        }

        .secure-line {
            margin-top: 17px;
            text-align: center;
        }

        .secure-line a {
            color: #ffffff;
            font-size: 14px;
            font-weight: 900;
            text-decoration: none;
        }

        .secure-line a:hover {
            text-decoration: underline;
        }

        @media (max-width: 480px) {
            body {
                padding: 14px;
            }

            .login-container {
                padding: 22px;
                border-radius: 24px;
            }

            .logo-box {
                width: 96px;
                height: 96px;
            }

            .login-header h1 {
                font-size: 24px;
            }
        }

        @media (max-width: 360px) {
            .login-container {
                padding: 18px;
            }

            .logo-box {
                width: 86px;
                height: 86px;
            }
        }
    </style>
</head>

<body>
    <div class="login-shell">
        <main class="login-container">
            <div class="login-header">
                <div class="logo-box">
                    <img src="<?php echo htmlspecialchars($panelLogo, ENT_QUOTES, 'UTF-8'); ?>" alt="TV Express">
                </div>

                <p>Painel VODs Express</p>
            </div>

            <?php if ($error): ?>
                <div class="error-message">⚠️ <?php echo htmlspecialchars($error, ENT_QUOTES, 'UTF-8'); ?></div>
            <?php endif; ?>

            <form method="POST" autocomplete="on">
                <div class="form-group">
                    <label for="username">Usuário</label>
                    <div class="input-wrap">
                        <input type="text"
                               id="username"
                               name="username"
                               placeholder="Digite seu usuário"
                               autocomplete="username"
                               required
                               autofocus>
                    </div>
                </div>

                <div class="form-group">
                    <label for="password">Senha</label>
                    <div class="input-wrap">
                        <input type="password"
                               id="password"
                               name="password"
                               placeholder="Digite sua senha"
                               autocomplete="current-password"
                               required>
                    </div>
                </div>

                <button type="submit" class="login-btn">Entrar</button>

                <div class="secure-line">
                    <a href="https://t.me/storeart_digital" target="_blank" rel="noopener noreferrer">+ StoreArt</a>
                </div>
            </form>
        </main>
    </div>
</body>
</html>
