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

$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    checkCsrf();
    $u = $_POST['username'] ?? '';
    $p = $_POST['password'] ?? '';
    if ($u === ADMIN_USERNAME && password_verify($p, ADMIN_PASSWORD_HASH)) {
        session_regenerate_id(true);
        $_SESSION['admin_logged_in'] = true;
        header('Location: index.php');
        exit;
    }
    $error = 'Invalid username or password';
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>License Admin Login</title>
<style>
  :root{--brand:#6dc82c;--bg:#0f1115;--card:#171a21;--border:#262b36;--text:#e7e9ee;--text2:#9aa3b2;}
  *{box-sizing:border-box;}
  body{margin:0;min-height:100vh;display:flex;align-items:center;justify-content:center;background:var(--bg);color:var(--text);font-family:-apple-system,Segoe UI,Roboto,Arial,sans-serif;}
  .card{width:100%;max-width:360px;background:var(--card);border:1px solid var(--border);border-radius:14px;padding:32px;}
  h1{font-size:18px;margin:0 0 4px;}
  p.sub{color:var(--text2);font-size:13px;margin:0 0 24px;}
  label{display:block;font-size:12px;color:var(--text2);margin-bottom:6px;font-weight:600;text-transform:uppercase;letter-spacing:.03em;}
  input{width:100%;padding:11px 12px;margin-bottom:16px;border-radius:8px;border:1px solid var(--border);background:#0d0f14;color:var(--text);font-size:14px;}
  input:focus{outline:none;border-color:var(--brand);}
  button{width:100%;padding:12px;border:0;border-radius:8px;background:var(--brand);color:#0b1200;font-weight:700;font-size:14px;cursor:pointer;}
  button:hover{filter:brightness(1.08);}
  .error{background:rgba(239,68,68,.1);border:1px solid rgba(239,68,68,.3);color:#f87171;padding:10px 12px;border-radius:8px;font-size:13px;margin-bottom:16px;}
</style>
</head>
<body>
  <form class="card" method="post">
    <h1>License Admin</h1>
    <p class="sub">Sign in to manage licenses</p>
    <?php if ($error): ?><div class="error"><?= e($error) ?></div><?php endif; ?>
    <?= csrfField() ?>
    <label>Username</label>
    <input type="text" name="username" autofocus required>
    <label>Password</label>
    <input type="password" name="password" required>
    <button type="submit">Sign In</button>
  </form>
</body>
</html>
