<?php
declare(strict_types=1);

require __DIR__ . '/auth.php';

if (admin_is_logged_in()) {
    header('Location: index.php', true, 302);
    exit;
}

$error = '';
if (($_SERVER['REQUEST_METHOD'] ?? 'GET') === 'POST') {
    if (!admin_csrf_check($_POST['_csrf'] ?? null)) {
        $error = 'Sessão expirada. Tente novamente.';
    } elseif (admin_login((string) ($_POST['password'] ?? ''))) {
        header('Location: index.php', true, 303);
        exit;
    } else {
        $wait = admin_lock_seconds_left();
        $error = $wait > 0
            ? "Muitas tentativas. Aguarde {$wait}s."
            : 'Senha incorreta.';
    }
}
?>
<!doctype html>
<html lang="pt-BR">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="robots" content="noindex,nofollow">
<title>Painel · <?= h(BRAND_NAME) ?></title>
<?php require __DIR__ . '/styles.php'; ?>
</head>
<body>
<div style="min-height:100vh;display:flex;align-items:center;justify-content:center;padding:24px">
  <div style="width:100%;max-width:360px">
    <div class="logo" style="justify-content:center;margin-bottom:22px;font-size:18px">
      <span class="dot"></span><?= h(BRAND_NAME) ?> <span class="muted" style="font-weight:500">Leads</span>
    </div>

    <form method="post" class="card" style="margin:0">
      <div class="card-b">
        <h1 style="font-size:17px;font-weight:700;letter-spacing:-.02em">Acesso restrito</h1>
        <p class="small muted" style="margin-top:6px">Informe a senha do painel para continuar.</p>

        <?php if ($error !== ''): ?>
          <div class="flash flash-err"><?= h($error) ?></div>
        <?php endif; ?>

        <div class="fld" style="margin-top:18px">
          <label for="password">Senha</label>
          <input type="password" id="password" name="password" required autofocus autocomplete="current-password" style="width:100%">
        </div>

        <input type="hidden" name="_csrf" value="<?= h(admin_csrf_token()) ?>">
        <button class="btn btn-pri" type="submit" style="width:100%;justify-content:center;margin-top:16px;padding:11px">Entrar</button>
      </div>
    </form>

    <p class="xs muted" style="text-align:center;margin-top:16px">Painel de acompanhamento de leads e FTDs.</p>
  </div>
</div>
</body>
</html>
