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

if (is_logged_in()) {
    header('Location: dashboard.php');
    exit;
}

$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 = '';

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

    if (empty($username) || empty($password)) {
        $error = 'Preencha todos os campos.';
    } elseif (login_user($username, $password)) {
        header('Location: dashboard.php');
        exit;
    } else {
        $error = 'Usuário ou senha inválidos.';
    }
}
?>
<!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 TV 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: 28px;
            color: var(--text);
            font-family: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
            background:
                radial-gradient(circle at 50% 34%, rgba(184,92,255,.32), transparent 31%),
                linear-gradient(135deg, rgba(17,0,33,.86), rgba(65,0,125,.58)),
                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%),
                linear-gradient(90deg, rgba(0,0,0,.38), transparent 45%, rgba(0,0,0,.26));
        }
        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: min(1080px, 100%);
            display: grid;
            grid-template-columns: 1fr 430px;
            gap: 28px;
            align-items: center;
        }
        .hero-copy {
            min-height: 520px;
            display: flex;
            flex-direction: column;
            justify-content: center;
            padding: 30px;
        }
        .eyebrow {
            display: inline-flex;
            align-items: center;
            gap: 8px;
            width: fit-content;
            padding: 10px 14px;
            border-radius: 999px;
            background: rgba(124,255,107,.12);
            border: 1px solid rgba(124,255,107,.22);
            color: #d9ffd4;
            font-weight: 900;
            font-size: 13px;
            letter-spacing: .05em;
            text-transform: uppercase;
        }
        .eyebrow::before { content: ""; width: 8px; height: 8px; border-radius: 99px; background: var(--green); box-shadow: 0 0 15px var(--green); }
        .hero-copy h1 {
            max-width: 620px;
            margin-top: 22px;
            font-size: clamp(34px, 5vw, 62px);
            line-height: 1.02;
            letter-spacing: -.05em;
        }
        .hero-copy p {
            max-width: 560px;
            margin-top: 16px;
            color: var(--muted);
            line-height: 1.6;
            font-size: 16px;
        }
        .features {
            display: flex;
            flex-wrap: wrap;
            gap: 10px;
            margin-top: 24px;
        }
        .features span {
            padding: 10px 13px;
            border: 1px solid var(--border);
            border-radius: 999px;
            background: rgba(255,255,255,.06);
            color: #fff;
            font-size: 13px;
            font-weight: 800;
        }
        .login-container {
            width: 100%;
            padding: 30px;
            border-radius: 30px;
            background: rgba(23, 3, 50, .78);
            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: 122px;
            height: 122px;
            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;
        }
        .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; }
        input {
            width: 100%;
            min-height: 52px;
            background: transparent;
            border: 0;
            color: #fff;
            outline: none;
            font-size: 15px;
        }
        input::placeholder { color: rgba(215,199,246,.62); }
        .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 {
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 9px;
            margin-top: 17px;
            color: var(--muted);
            font-size: 13px;
            font-weight: 700;
        }
        .secure-line b { color: var(--green); }
        @media (max-width: 900px) {
            body { padding: 18px; align-items: flex-start; }
            .login-shell { grid-template-columns: 1fr; gap: 16px; }
            .hero-copy { min-height: auto; padding: 18px 6px 6px; text-align: center; align-items: center; }
            .hero-copy p { font-size: 14px; }
            .features { justify-content: center; }
            .login-container { max-width: 430px; margin: 0 auto; padding: 24px; border-radius: 24px; }
        }
        @media (max-width: 480px) {
            body { padding: 12px; }
            .hero-copy h1 { font-size: 32px; }
            .features span { font-size: 12px; }
            .login-container { padding: 20px; }
            .logo-box { width: 96px; height: 96px; border-radius: 22px; }
        }
    </style>
</head>
<body>
    <div class="login-shell">
        <section class="hero-copy">
            <span class="eyebrow">Painel TV Express</span>
            <h1>Controle seu app com visual premium.</h1>
            <p>Gerencie DNS, logo, background, avisos, QR de suporte e usuários em um painel moderno, responsivo e combinando com o aplicativo.</p>
            <div class="features">
                <span>📱 Responsivo</span>
                <span>🖼️ Branding dinâmico</span>
                <span>📲 QR Suporte</span>
                <span>🔒 Acesso protegido</span>
            </div>
        </section>

        <main class="login-container">
            <div class="login-header">
                <div class="logo-box">
                    <img src="<?php echo htmlspecialchars($panelLogo); ?>" alt="TV Express">
                </div>
                <h1>Bem-vindo!</h1>
                <p>Entre com suas credenciais para continuar</p>
            </div>

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

            <form method="POST" autocomplete="on">
                <div class="form-group">
                    <label for="username">👤 Usuário</label>
                    <div class="input-wrap">
                        <span class="input-icon">●</span>
                        <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">
                        <span class="input-icon">●</span>
                        <input type="password"
                               id="password"
                               name="password"
                               placeholder="Digite sua senha"
                               autocomplete="current-password"
                               required>
                    </div>
                </div>

                <button type="submit" class="login-btn">🚀 ENTRAR NO PAINEL</button>
                <div class="secure-line"><b>🛡</b> Acesso seguro e protegido</div>
            </form>
        </main>
    </div>
</body>
</html>
