<?php
require_once __DIR__ . '/auth.php';
if (is_logged_in()) { header('Location: index.php'); exit; }

$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (attempt_login($_POST['username'] ?? '', $_POST['password'] ?? '')) {
        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">
<title>Admin Login</title>
<style>
*{box-sizing:border-box;margin:0;padding:0}
body{font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',sans-serif;background:#f5f4f0;min-height:100vh;display:flex;align-items:center;justify-content:center;padding:1rem}
.card{background:#fff;border:1px solid #e5e3de;border-radius:14px;padding:2.5rem 2rem;width:100%;max-width:380px}
.logo{font-size:22px;font-weight:600;color:#1a1a1a;letter-spacing:-0.5px;margin-bottom:6px}
.sub{font-size:13px;color:#888;margin-bottom:2rem}
label{display:block;font-size:12px;font-weight:500;color:#555;margin-bottom:5px;letter-spacing:0.3px}
input[type=text],input[type=password]{width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:8px;font-size:14px;outline:none;transition:border-color 0.15s;margin-bottom:1rem}
input:focus{border-color:#1a1a1a}
button{width:100%;padding:11px;background:#1a1a1a;color:#fff;border:none;border-radius:8px;font-size:14px;font-weight:500;cursor:pointer;margin-top:4px}
button:hover{background:#333}
.err{font-size:13px;color:#c0392b;background:#fdf0f0;border:1px solid #f5c6c6;border-radius:7px;padding:9px 12px;margin-bottom:1rem}
</style>
</head>
<body>
<div class="card">
  <div class="logo">Grove Admin</div>
  <div class="sub">QR Menu Management</div>
  <?php if($error): ?><div class="err"><?= htmlspecialchars($error) ?></div><?php endif ?>
  <form method="POST">
    <label>Username</label>
    <input type="text" name="username" autocomplete="username" required>
    <label>Password</label>
    <input type="password" name="password" autocomplete="current-password" required>
    <button type="submit">Sign in</button>
  </form>
</div>
</body>
</html>
