<?php
// --- CUSTOM SESSION PATH FIX START (লগইন লুপ এবং /tmp এরর সমাধান) ---
$sessionPath = __DIR__ . '/sessions';
if (!file_exists($sessionPath)) { 
    @mkdir($sessionPath, 0755, true); 
}
if (is_writable($sessionPath)) {
    ini_set('session.save_path', $sessionPath);
}
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}
// --- CUSTOM SESSION PATH FIX END ---

include 'config.php';

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

$error = "";
if(isset($_POST['login'])) {
    $email = mysqli_real_escape_string($conn, $_POST['email']);
    $password = $_POST['password'];

    $result = $conn->query("SELECT * FROM users WHERE email='$email'");
    if($result->num_rows > 0) {
        $user = $result->fetch_assoc();
        if(password_verify($password, $user['password'])) {
            $_SESSION['user_id'] = $user['id'];
            $_SESSION['email'] = $user['email'];
            header("Location: index.php");
            exit();
        } else { $error = "Invalid Credentials!"; }
    } else { $error = "User not found!"; }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>👑 TAMIM 1280 👑</title>
    <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Orbitron&family=Rajdhani:wght@500&display=swap">
    <style>
        :root { --primary: #00eaff; --bg: #050505; }
        body { background: var(--bg); color: white; font-family: 'Rajdhani', sans-serif; display: flex; justify-content: center; align-items: center; height: 100vh; margin:0; }
        .login-box { width: 320px; padding: 40px; background: rgba(10,20,30,0.9); border: 1px solid var(--primary); border-radius: 10px; text-align: center; }
        h2 { font-family: 'Orbitron'; color: var(--primary); text-shadow: 0 0 10px var(--primary); }
        input { width: 100%; padding: 10px; margin: 10px 0; background: #000; border: 1px solid #333; color: #fff; border-left: 3px solid var(--primary); outline:none;}
        button { width: 100%; padding: 10px; background: transparent; border: 1px solid var(--primary); color: var(--primary); cursor: pointer; font-family: 'Orbitron'; transition: 0.3s; }
        button:hover { background: var(--primary); color: #000; }
        .error { color: #ff3333; font-size: 14px; margin-top: 10px; }
    </style>
</head>
<body>
    <div class="login-box">
        <h2>👑 TAMIM 1280 👑</h2>
        <form method="POST">
            <input type="email" name="email" placeholder="Email Address" required>
            <input type="password" name="password" placeholder="Password" required>
            <button type="submit" name="login">LOGIN</button>
        </form>
        <?php if($error) echo "<div class='error'>$error</div>"; ?>
    </div>
</body>
</html>