<?php
if(session_status()===PHP_SESSION_NONE) session_start();
include(__DIR__.'/../config/db.php');
require_once __DIR__.'/../includes/rate_limit.php';
$error='';
if($_POST){
  $retry = rate_limit_check('admin_login', $_SERVER['REMOTE_ADDR'] ?? 'unknown', 5, 300);
  if ($retry !== null) {
    rate_limit_respond($retry, 'html');
  }
  $u=trim($_POST['username']);
  $p=$_POST['password'];
  $s=$conn->prepare("SELECT * FROM admins WHERE username=?");
  $s->bind_param("s",$u); $s->execute();
  $r=$s->get_result();
  if($r->num_rows>0){
    $row=$r->fetch_assoc();
    if(password_verify($p,$row['password'])){
      session_regenerate_id(true); // prevent session fixation attack
      $_SESSION['admin']=$row;
      header("Location:dashboard.php");
      exit();
    }
    else $error="Invalid username or password.";
  } else $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 — Perna Events</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700&display=swap" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.css" rel="stylesheet">
<style>
*,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
:root{--bg:#0d0f14;--surface:#13161e;--border:#232839;--accent:#e8a020;--text:#dde1ee;--muted:#6b7394;--red:#e05252;--font:'Plus Jakarta Sans',sans-serif}
body{font-family:var(--font);background:var(--bg);color:var(--text);min-height:100vh;display:flex;align-items:center;justify-content:center;padding:20px}
body::before{content:'';position:fixed;inset:0;background-image:radial-gradient(circle at 20% 50%,rgba(232,160,32,.06) 0,transparent 55%),radial-gradient(circle at 80% 20%,rgba(75,158,255,.05) 0,transparent 50%);pointer-events:none}
.wrap{width:100%;max-width:380px;position:relative;z-index:1}
.logo{text-align:center;margin-bottom:32px}
.logo-icon{width:48px;height:48px;background:var(--accent);border-radius:13px;display:inline-grid;place-items:center;margin-bottom:12px}
.logo-icon svg{width:22px;height:22px;fill:#000}
.logo h1{font-size:18px;font-weight:700;letter-spacing:.3px}
.logo p{font-size:12.5px;color:var(--muted);margin-top:4px}
.card{background:var(--surface);border:1px solid var(--border);border-radius:14px;padding:28px}
.lbl{display:block;font-size:11px;font-weight:700;text-transform:uppercase;letter-spacing:.8px;color:var(--muted);margin-bottom:6px}
.input-wrap{position:relative;margin-bottom:14px}
.input-wrap i{position:absolute;left:13px;top:50%;transform:translateY(-50%);color:var(--muted);font-size:15px;pointer-events:none}
.input-wrap input{width:100%;background:var(--bg);border:1px solid var(--border);color:var(--text);border-radius:8px;padding:11px 13px 11px 38px;font-size:14px;font-family:var(--font);transition:border-color .15s}
.input-wrap input:focus{outline:none;border-color:var(--accent);box-shadow:0 0 0 3px rgba(232,160,32,.1)}
.input-wrap input::placeholder{color:var(--muted)}
.btn{width:100%;background:var(--accent);color:#000;border:none;border-radius:8px;padding:12px;font-size:14px;font-weight:700;font-family:var(--font);cursor:pointer;margin-top:4px;transition:opacity .15s}
.btn:hover{opacity:.88}
.err{background:rgba(224,82,82,.1);border:1px solid rgba(224,82,82,.3);color:var(--red);padding:10px 14px;border-radius:8px;font-size:13px;margin-bottom:16px;display:flex;align-items:center;gap:8px}
</style>
</head>
<body>
<div class="wrap">
  <div class="logo">
    <div class="logo-icon"><svg viewBox="0 0 20 20"><path d="M10 2l2.4 4.9 5.4.8-3.9 3.8.9 5.3L10 14.4l-4.8 2.5.9-5.3L2.2 7.7l5.4-.8z"/></svg></div>
    <h1>Perna Events</h1>
    <p>Admin Portal</p>
  </div>
  <div class="card">
    <?php if($error): ?>
    <div class="err"><i class="bi bi-exclamation-circle-fill"></i><?= htmlspecialchars($error) ?></div>
    <?php endif; ?>
    <form method="POST">
      <label class="lbl">Username</label>
      <div class="input-wrap"><i class="bi bi-person-fill"></i><input type="text" name="username" placeholder="admin" required autocomplete="username"></div>
      <label class="lbl">Password</label>
      <div class="input-wrap"><i class="bi bi-lock-fill"></i><input type="password" name="password" placeholder="••••••••" required autocomplete="current-password"></div>
      <button class="btn">Sign in <i class="bi bi-arrow-right-short"></i></button>
    </form>
  </div>
</div>
</body></html>
