<?php
session_start();
require_once 'db.php';

$erro = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email = trim($_POST['email'] ?? '');
    $senha = $_POST['senha'] ?? '';

    $stmt = $pdo->prepare("SELECT * FROM superadmins WHERE email = ?");
    $stmt->execute([$email]);
    $user = $stmt->fetch(PDO::FETCH_ASSOC);

    if ($user && password_verify($senha, $user['senha'])) {
        $_SESSION['superadmin_id'] = $user['id'];
        $_SESSION['superadmin_nome'] = $user['nome'];
        $_SESSION['superadmin_email'] = $user['email'];
        header("Location: dashboard.php");
        exit;
    } else {
        $erro = "Email ou senha inválidos.";
    }
}
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <title>Login Super Admin</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- Google Fonts -->
    <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@700;400&display=swap" rel="stylesheet">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css">
    <style>
        body {
            margin: 0; padding: 0;
            font-family: 'Roboto', Arial, sans-serif;
            background: linear-gradient(135deg, #1876F2 0%, #5A99FF 85%);
            min-height: 100vh;
            display: flex; align-items: center; justify-content: center;
        }
        .login-container {
            background: #fff;
            border-radius: 15px;
            box-shadow: 0 6px 32px rgba(24,118,242,0.18);
            padding: 44px 38px 36px 38px;
            max-width: 370px;
            width: 100%;
            text-align: center;
            animation: fadeIn .8s;
        }
        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(40px);}
            to { opacity: 1; transform: none;}
        }
        .logo {
            font-size: 2.5rem;
            font-weight: 700;
            color: #1876F2;
            margin-bottom: 20px;
            letter-spacing: 0.04em;
        }
        h2 {
            font-size: 1.28rem;
            font-weight: 700;
            color: #1a2746;
            margin-bottom: 29px;
        }
        .login-form label {
            font-size: 1rem;
            color: #1a2746;
            font-weight: 600;
            display: block;
            margin-bottom: 7px;
            text-align: left;
        }
        .login-form input[type="email"],
        .login-form input[type="password"] {
            width: 100%;
            padding: 10px 13px;
            border-radius: 8px;
            border: 1px solid #e3e8f0;
            background: #f7faff;
            font-size: 1rem;
            margin-bottom: 17px;
            outline: none;
            box-sizing: border-box;
        }
        .login-form input:focus {
            border-color: #1876F2;
            background: #eaf4ff;
        }
        .login-form button {
            background: linear-gradient(90deg,#1876F2 70%, #5A99FF 100%);
            color: #fff;
            font-weight: 700;
            font-size: 1.09rem;
            border: none;
            border-radius: 8px;
            padding: 12px 0;
            width: 100%;
            cursor: pointer;
            transition: background 0.18s;
            margin-top: 5px;
            box-shadow: 0 2px 8px #1876f22a;
        }
        .login-form button:hover {
            background: linear-gradient(90deg,#1356b3 80%, #3a7be5 100%);
        }
        .erro {
            color: #d9534f;
            background: #ffeaea;
            border-radius: 7px;
            padding: 8px 0;
            margin-bottom: 15px;
            font-weight: 600;
        }
        .login-footer {
            margin-top: 30px;
            color: #7a8faf;
            font-size: .97rem;
        }
        .login-footer i {
            color: #1876F2;
            margin-right: 5px;
        }
        @media (max-width: 500px) {
            .login-container { max-width: 98vw; padding: 20px 4vw; }
        }
    </style>
</head>
<body>
    <div class="login-container">
        <div class="logo">
            <i class="fa-solid fa-user-shield"></i>
        </div>
        <h2>Área restrita – Super Admin</h2>
        <?php if ($erro): ?>
            <div class="erro"><?= htmlspecialchars($erro) ?></div>
        <?php endif; ?>
        <form class="login-form" method="post" autocomplete="off">
            <label for="email">Email</label>
            <input type="email" name="email" id="email" required autofocus>
            <label for="senha">Senha</label>
            <input type="password" name="senha" id="senha" required>
            <button type="submit"><i class="fa-solid fa-right-to-bracket"></i> Entrar</button>
        </form>
        <div class="login-footer">
            <i class="fa-solid fa-lock"></i> Acesso restrito ao painel administrativo
        </div>
    </div>
</body>
</html>