<?php
$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (!checkRateLimit('login', 10, 300)) {
        $error = 'Too many login attempts from your location. Please try again later.';
    } else {
        $username = sanitizeString($_POST['username'] ?? '', 100);
        $password = $_POST['password'] ?? '';

        if (!$username || !$password) {
            $error = 'Please enter both username and password.';
        } elseif (strlen($password) > 200) {
            $error = 'Invalid credentials.';
        } elseif (!checkLoginThrottle($username)) {
            $error = 'Account temporarily locked due to too many failed attempts. Please try again in 15 minutes.';
        } else {
            $db = getDB();
            $stmt = $db->prepare("SELECT * FROM admin_users WHERE username = ?");
            $stmt->execute([$username]);
            $user = $stmt->fetch();
            if ($user && $user['active'] && password_verify($password, $user['password_hash'])) {
                recordLoginAttempt($username, true);
                $_SESSION['admin_logged_in'] = true;
                $_SESSION['admin_user'] = $user['username'];
                $_SESSION['admin_role'] = $user['role'] ?? 'admin';
                $_SESSION['admin_id'] = $user['id'];
                $_SESSION['admin_full_name'] = $user['full_name'] ?? '';
                $_SESSION['_last_activity'] = time();
                session_regenerate_id(true);
                $db->prepare("UPDATE admin_users SET last_login = CURRENT_TIMESTAMP WHERE id = ?")->execute([$user['id']]);
                logAudit('login', 'admin_users', $user['id'], 'Admin login successful');
                header('Location: /admin');
                exit;
            } else {
                if ($user) {
                    recordLoginAttempt($username, false);
                }
                $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.0">
    <title>Admin Login - Alpha Genomics</title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }

        body {
            font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
            background: #0f172a;
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            position: relative;
            overflow: hidden;
            -webkit-font-smoothing: antialiased;
        }

        body::before {
            content: '';
            position: absolute;
            top: -50%;
            left: -50%;
            width: 200%;
            height: 200%;
            background: radial-gradient(ellipse at 30% 20%, rgba(59,130,246,0.12) 0%, transparent 50%),
                        radial-gradient(ellipse at 70% 80%, rgba(139,92,246,0.08) 0%, transparent 50%);
            animation: bgFloat 20s ease-in-out infinite alternate;
        }

        @keyframes bgFloat {
            0% { transform: translate(0, 0) rotate(0deg); }
            100% { transform: translate(-2%, -2%) rotate(2deg); }
        }

        .login-wrapper {
            position: relative;
            z-index: 1;
            width: 100%;
            max-width: 420px;
            padding: 20px;
        }

        .login-card {
            background: rgba(255,255,255,0.97);
            border-radius: 20px;
            padding: 44px 40px;
            box-shadow: 0 25px 80px rgba(0,0,0,0.25), 0 0 0 1px rgba(255,255,255,0.05);
            backdrop-filter: blur(20px);
        }

        .login-logo {
            text-align: center;
            margin-bottom: 8px;
        }

        .login-logo .logo-icon {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            width: 56px;
            height: 56px;
            background: linear-gradient(135deg, #3b82f6, #6366f1);
            border-radius: 16px;
            margin-bottom: 16px;
            box-shadow: 0 8px 24px rgba(59,130,246,0.3);
        }

        .login-logo .logo-icon i {
            font-size: 24px;
            color: #fff;
        }

        .login-card h2 {
            text-align: center;
            color: #0f172a;
            margin-bottom: 6px;
            font-size: 24px;
            font-weight: 800;
            letter-spacing: -0.03em;
        }

        .login-card .subtitle {
            text-align: center;
            color: #94a3b8;
            margin-bottom: 32px;
            font-size: 14px;
            font-weight: 400;
        }

        .form-group {
            margin-bottom: 22px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-weight: 600;
            font-size: 13px;
            color: #334155;
            letter-spacing: -0.01em;
        }

        .input-wrapper {
            position: relative;
        }

        .input-wrapper i {
            position: absolute;
            left: 16px;
            top: 50%;
            transform: translateY(-50%);
            color: #94a3b8;
            font-size: 14px;
            transition: color 0.2s;
        }

        .form-group input {
            width: 100%;
            padding: 13px 16px 13px 44px;
            border: 1.5px solid #e2e8f0;
            border-radius: 10px;
            font-size: 14px;
            font-family: inherit;
            color: #0f172a;
            background: #f8fafc;
            transition: all 0.2s ease;
        }

        .form-group input:focus {
            outline: none;
            border-color: #3b82f6;
            background: #fff;
            box-shadow: 0 0 0 3px rgba(59,130,246,0.1);
        }

        .form-group input:focus + i,
        .form-group input:focus ~ i { color: #3b82f6; }

        .form-group input::placeholder { color: #94a3b8; }

        .btn-login {
            width: 100%;
            padding: 14px;
            background: linear-gradient(135deg, #3b82f6, #2563eb);
            color: #fff;
            border: none;
            border-radius: 10px;
            font-size: 15px;
            font-weight: 700;
            font-family: inherit;
            cursor: pointer;
            transition: all 0.2s ease;
            box-shadow: 0 4px 16px rgba(59,130,246,0.35);
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 8px;
            letter-spacing: -0.01em;
        }

        .btn-login:hover {
            background: linear-gradient(135deg, #2563eb, #1d4ed8);
            box-shadow: 0 6px 24px rgba(59,130,246,0.4);
            transform: translateY(-1px);
        }

        .btn-login:active {
            transform: translateY(0);
        }

        .error {
            background: #fef2f2;
            color: #991b1b;
            padding: 13px 16px;
            border-radius: 10px;
            margin-bottom: 22px;
            font-size: 13px;
            text-align: center;
            border: 1px solid #fecaca;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 8px;
            font-weight: 500;
        }

        .error::before {
            content: '\f06a';
            font-family: 'Font Awesome 6 Free';
            font-weight: 900;
            color: #ef4444;
            font-size: 14px;
        }

        .back-link {
            text-align: center;
            margin-top: 24px;
            padding-top: 20px;
            border-top: 1px solid #f1f5f9;
        }

        .back-link a {
            color: #64748b;
            font-size: 13px;
            text-decoration: none;
            font-weight: 500;
            transition: color 0.2s;
            display: inline-flex;
            align-items: center;
            gap: 6px;
        }

        .back-link a:hover { color: #3b82f6; }

        @media (max-width: 480px) {
            .login-card { padding: 32px 24px; }
        }
    </style>
</head>
<body>
    <div class="login-wrapper">
        <div class="login-card">
            <div class="login-logo">
                <div class="logo-icon"><i class="fas fa-dna"></i></div>
            </div>
            <h2>Admin Login</h2>
            <p class="subtitle">Alpha Genomics Content Management</p>
            <?php if ($error): ?>
                <div class="error"><?= htmlspecialchars($error) ?></div>
            <?php endif; ?>
            <form method="post">
                <div class="form-group">
                    <label for="username">Username</label>
                    <div class="input-wrapper">
                        <input type="text" id="username" name="username" required placeholder="Enter your username" autocomplete="username">
                        <i class="fas fa-user"></i>
                    </div>
                </div>
                <div class="form-group">
                    <label for="password">Password</label>
                    <div class="input-wrapper">
                        <input type="password" id="password" name="password" required placeholder="Enter your password" autocomplete="current-password">
                        <i class="fas fa-lock"></i>
                    </div>
                </div>
                <button type="submit" class="btn-login">Sign In <i class="fas fa-arrow-right"></i></button>
            </form>
            <div class="back-link">
                <a href="/"><i class="fas fa-arrow-left"></i> Back to Website</a>
            </div>
        </div>
    </div>
</body>
</html>
