<?php
session_start();
if (isset($_SESSION['mc_username'])) {
    header("Location: index.php");
    exit;
}

if ($_POST['username'] ?? '') {
    $user = trim($_POST['username']);
    if (preg_match('/^[a-zA-Z0-9_]{3,16}$/', $user)) {
        $_SESSION['mc_username'] = $user;
        header("Location: index.php");
        exit;
    } else {
        $error = "Invalid username!";
    }
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>Player Login • Reckon Store</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
        body{background:#0a0a1a;color:#0f0;font-family:'Courier New',courier;margin:0;display:flex;align-items:center;justify-content:center;height:100vh}
        .box{background:#111;border:4px solid #0f0;padding:50px;border-radius:20px;width:420px;text-align:center;box-shadow:0 0 60px #0f05}
        input{width:100%;padding:16px;margin:12px 0;background:#000;border:2px solid #0f0;color:#0f0;font-size:18px;border-radius:10px}
        button{width:100%;padding:18px;background:#0f0;color:#000;font-weight:bold;border:none;border-radius:10px;font-size:20px;cursor:pointer}
        .error{color:#f00;margin:15px 0;font-size:18px}
    </style>
</head>
<body>
<div class="box">
    <h1>PLAYER LOGIN</h1>
    <p>Enter your Minecraft username</p>
    <?php if(isset($error)) echo "<div class='error'>$error</div>"; ?>
    <form method="POST">
        <input type="text" name="username" placeholder="Notch" required autofocus>
        <button type="submit">LOGIN</button>
    </form>
    <br><a href="../index.php" style="color:#0f0">Back to Store</a>
</div>
</body>
</html>