<?php
declare(strict_types=1);
require_once __DIR__ . '/auth.php';

if (lk_is_admin()) {
    header('Location: /');
    exit;
}

$err = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $user = trim((string)($_POST['username'] ?? ''));
    $pass = (string)($_POST['password'] ?? '');
    $rem  = !empty($_POST['remember']);
    if ($user === '' || $pass === '') {
        $err = 'Username and password required.';
    } elseif (!lk_login($user, $pass, $rem)) {
        $err = 'Invalid credentials.';
    } else {
        header('Location: /');
        exit;
    }
}
?><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>LudoKing Bot — Admin Login</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700;900&display=swap" rel="stylesheet">
<style>
*,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
:root{
  --bg:    #0b1120;
  --card:  #111c35;
  --bd:    rgba(255,255,255,.08);
  --txt:   #e2e8f0;
  --muted: rgba(226,232,240,.55);
  --a1:    #10b981;
  --a2:    #059669;
  --gold:  #fbbf24;
  --bad:   #ef4444;
}
body{min-height:100dvh;display:flex;align-items:center;justify-content:center;background:var(--bg);font-family:'Inter',sans-serif;color:var(--txt);padding:20px;}
body::before{
  content:'';position:fixed;inset:0;pointer-events:none;z-index:0;
  background:
    radial-gradient(ellipse 60% 50% at 20% 20%, rgba(16,185,129,.12), transparent),
    radial-gradient(ellipse 50% 60% at 80% 80%, rgba(251,191,36,.08), transparent);
}
.card{
  position:relative;z-index:1;
  background:var(--card);
  border:1px solid var(--bd);
  border-radius:24px;
  padding:36px 32px 32px;
  width:100%;max-width:400px;
  box-shadow:0 32px 80px rgba(0,0,0,.55);
}
.logo{text-align:center;margin-bottom:28px;}
.logo .icon{
  display:inline-flex;align-items:center;justify-content:center;
  width:64px;height:64px;border-radius:20px;
  background:linear-gradient(135deg,var(--a1),var(--a2));
  font-size:28px;margin-bottom:12px;
  box-shadow:0 8px 28px rgba(16,185,129,.35);
}
.logo h1{font-size:22px;font-weight:900;color:var(--txt);}
.logo p{font-size:13px;color:var(--muted);margin-top:4px;}
label{display:block;font-size:13px;font-weight:700;color:var(--muted);margin-bottom:6px;}
.inp{
  width:100%;padding:12px 16px;background:rgba(255,255,255,.05);
  border:1px solid var(--bd);border-radius:12px;color:var(--txt);
  font-size:15px;font-family:inherit;outline:none;transition:border-color .2s;
}
.inp:focus{border-color:var(--a1);}
.field{margin-bottom:18px;}
.remember{display:flex;align-items:center;gap:8px;font-size:13px;color:var(--muted);margin-bottom:24px;cursor:pointer;}
.remember input{width:16px;height:16px;accent-color:var(--a1);}
.btn{
  width:100%;padding:14px;border:none;border-radius:14px;cursor:pointer;font-size:16px;font-weight:900;font-family:inherit;
  background:linear-gradient(135deg,var(--a1),var(--a2));
  color:#fff;box-shadow:0 8px 24px rgba(16,185,129,.35);
  transition:box-shadow .18s,transform .18s;
}
.btn:hover{box-shadow:0 12px 34px rgba(16,185,129,.48);transform:translateY(-1px);}
.err{background:rgba(239,68,68,.12);border:1px solid rgba(239,68,68,.28);color:#fca5a5;border-radius:12px;padding:12px 16px;font-size:13px;font-weight:700;margin-bottom:18px;}
</style>
</head>
<body>
<div class="card">
  <div class="logo">
    <div class="icon">♟</div>
    <h1>LudoKing Bot</h1>
    <p>Admin Panel Login</p>
  </div>
  <?php if ($err !== ''): ?>
    <div class="err"><?= lk_e($err) ?></div>
  <?php endif; ?>
  <form method="post">
    <div class="field">
      <label for="username">Username</label>
      <input class="inp" type="text" id="username" name="username" autocomplete="username" required>
    </div>
    <div class="field">
      <label for="password">Password</label>
      <input class="inp" type="password" id="password" name="password" autocomplete="current-password" required>
    </div>
    <label class="remember">
      <input type="checkbox" name="remember" value="1"> Remember me for <?= LK_AUTH_DAYS ?> days
    </label>
    <button class="btn" type="submit">Login →</button>
  </form>
</div>
</body>
</html>
