<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors',1);

require_once "../db.php";

$error = "";

if(isset($_POST['login'])){
  $email = trim($_POST['email']);
  $pass  = trim($_POST['password']);

  $stmt = $pdo->prepare("SELECT * FROM admins WHERE email = ? LIMIT 1");
  $stmt->execute([$email]);
  $admin = $stmt->fetch(PDO::FETCH_ASSOC);

  if($admin){
    if($pass === $admin['password']){
      $_SESSION['admin_id']   = $admin['id'];
      $_SESSION['admin_name'] = $admin['name'];

      header("Location: main.php");
      exit;
    }else{
      $error = "Invalid password";
    }
  }else{
    $error = "Invalid email";
  }
}
?>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Admin Login</title>

<style>
*{box-sizing:border-box}

body{
  margin:0;
  min-height:100vh;
  font-family:system-ui;
  background:linear-gradient(135deg,#020617,#0f172a);
  display:flex;
  align-items:center;
  justify-content:center;
  padding:16px; /* ⭐ prevents edge touching */
}

.login-card{
  width:100%;
  max-width:380px;
  background:#ffffff;
  border-radius:20px;
  padding:28px 24px;
  box-shadow:0 20px 50px rgba(0,0,0,.35);
}

.login-card h2{
  text-align:center;
  margin-bottom:20px;
  font-size:22px;
  color:#020617;
}

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

input{
  width:100%;
  padding:14px;
  border-radius:14px;
  border:1px solid #cbd5e1;
  font-size:15px;
  outline:none;
  transition:.2s;
}

input:focus{
  border-color:#16a34a;
  box-shadow:0 0 0 2px rgba(22,163,74,.15);
}

button{
  width:100%;
  padding:14px;
  border-radius:16px;
  border:none;
  background:#16a34a;
  color:#fff;
  font-size:16px;
  font-weight:600;
  cursor:pointer;
  transition:.2s;
}

button:hover{
  background:#15803d;
}

.error{
  background:#fee2e2;
  color:#991b1b;
  padding:12px;
  border-radius:12px;
  margin-bottom:14px;
  text-align:center;
  font-size:14px;
}

.footer-text{
  text-align:center;
  margin-top:16px;
  font-size:13px;
  color:#64748b;
}

@media(max-width:480px){
  .login-card{
    padding:24px 18px;
  }
}
</style>
</head>
<body>

<div class="login-card">
  <h2>Admin Login</h2>

  <?php if($error): ?>
    <div class="error"><?= htmlspecialchars($error) ?></div>
  <?php endif; ?>

  <form method="post">
    <div class="form-group">
      <input type="email" name="email" placeholder="Admin Email" required>
    </div>
    <div class="form-group">
      <input type="password" name="password" placeholder="Password" required>
    </div>
    <button name="login">Login</button>
  </form>

  <div class="footer-text">
    © <?= date("Y") ?> Grocery Admin Panel
  </div>
</div>

</body>
</html>
