<?php
session_start();
include("includes/db.php");

$err = "";

if ($_SERVER['REQUEST_METHOD'] == "POST") {
    // Using your original MD5 logic so you don't lose access
    $u = mysqli_real_escape_string($conn, $_POST['username']);
    $p = md5($_POST['password']);

    $q = $conn->query("SELECT * FROM admins WHERE username='$u' AND password='$p'");
    
    if ($q->num_rows > 0) {
        $user = $q->fetch_assoc();
        $_SESSION['admin_id'] = $user['id']; // Using the actual ID from DB
        header("Location: dashboard.php");
        exit();
    } else {
        $err = "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>Admin Login</title>
    <link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;600;700&display=swap" rel="stylesheet">
    <style>
        :root {
            --primary: #4f46e5;
            --primary-hover: #4338ca;
            --bg: #f8fafc;
        }

        * { box-sizing: border-box; margin: 0; padding: 0; }

        body {
            font-family: 'Plus Jakarta Sans', sans-serif;
            background-color: var(--bg);
            background-image: radial-gradient(at 0% 0%, hsla(253,16%,7%,1) 0, transparent 50%), 
                              radial-gradient(at 50% 0%, hsla(225,39%,30%,1) 0, transparent 50%);
            height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .login-card {
            background: white;
            padding: 2.5rem;
            border-radius: 1.5rem;
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 400px;
        }

        h2 { 
            font-size: 1.5rem; 
            font-weight: 700; 
            color: #1e293b; 
            margin-bottom: 0.5rem;
            text-align: center;
        }

        p.subtitle {
            color: #64748b;
            font-size: 0.875rem;
            text-align: center;
            margin-bottom: 2rem;
        }

        .error-msg {
            background: #fef2f2;
            color: #991b1b;
            padding: 0.75rem;
            border-radius: 0.75rem;
            margin-bottom: 1.5rem;
            font-size: 0.875rem;
            border: 1px solid #fee2e2;
            text-align: center;
        }

        .input-group { margin-bottom: 1.25rem; }

        label {
            display: block;
            font-size: 0.875rem;
            font-weight: 600;
            color: #334155;
            margin-bottom: 0.5rem;
        }

        input {
            width: 100%;
            padding: 0.75rem 1rem;
            border: 1px solid #e2e8f0;
            border-radius: 0.75rem;
            font-size: 1rem;
            transition: all 0.2s;
        }

        input:focus {
            outline: none;
            border-color: var(--primary);
            box-shadow: 0 0 0 4px rgba(79, 70, 229, 0.1);
        }

        button {
            width: 100%;
            padding: 0.75rem;
            background: var(--primary);
            color: white;
            border: none;
            border-radius: 0.75rem;
            font-size: 1rem;
            font-weight: 600;
            cursor: pointer;
            transition: background 0.2s;
            margin-top: 0.5rem;
        }

        button:hover { background: var(--primary-hover); }
    </style>
</head>
<body>

<div class="login-card">
    <h2>Admin Login</h2>
    <p class="subtitle">Enter your credentials to access the panel</p>
    
    <?php if($err): ?>
        <div class="error-msg"><?= $err ?></div>
    <?php endif; ?>

    <form method="post">
        <div class="input-group">
            <label>Username</label>
            <input name="username" placeholder="Enter username" required>
        </div>
        
        <div class="input-group">
            <label>Password</label>
            <input type="password" name="password" placeholder="••••••••" required>
        </div>

        <button type="submit">LOGIN</button>
    </form>
</div>

</body>
</html>