<?php
require_once __DIR__ . '/includes/config.php';
if (isLoggedIn()) redirect(ADMIN_URL . '/index.php');
$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = trim($_POST['username'] ?? '');
    $password = $_POST['password'] ?? '';
    if ($username && $password) {
        $st = db()->prepare("SELECT * FROM admin_users WHERE username=? LIMIT 1");
        $st->bind_param('s', $username);
        $st->execute();
        $user = $st->get_result()->fetch_assoc();
        if ($user && password_verify($password, $user['password'])) {
            $_SESSION['cy_admin_id']   = $user['id'];
            $_SESSION['cy_admin_user'] = $user['username'];
            $_SESSION['cy_admin_name'] = $user['full_name'];
            $_SESSION['cy_admin_role'] = $user['role'];
            db()->query("UPDATE admin_users SET last_login=NOW() WHERE id=".(int)$user['id']);
            redirect(ADMIN_URL . '/index.php');
        } else { $error = 'Invalid username or password.'; }
    } else { $error = 'Please enter both username and 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 — CY Consultancy Centre</title>
<link href="https://fonts.googleapis.com/css2?family=Outfit:wght@400;500;600;700;800&family=Playfair+Display:wght@700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
<style>
:root{
  --brand:#1a56db;--brand2:#1e3a8a;
  --text:#fff;--muted:rgba(255,255,255,0.55);
  --border:rgba(255,255,255,0.12);
  --input-bg:rgba(255,255,255,0.07);
  --danger:#f87171;
}
*,*::before,*::after{box-sizing:border-box;margin:0;padding:0;}
body{
  font-family:'Outfit',sans-serif;min-height:100vh;
  display:flex;align-items:center;justify-content:center;
  background:#050d1a;position:relative;overflow:hidden;
}
.bg{position:absolute;inset:0;z-index:0;}
.bg-img{position:absolute;inset:0;
  background:
    radial-gradient(ellipse 110% 80% at 5% 5%,  rgba(26,86,219,.38) 0%,transparent 50%),
    radial-gradient(ellipse 80% 110% at 95% 95%, rgba(30,58,138,.42) 0%,transparent 50%),
    radial-gradient(ellipse 60% 60% at 50% 50%,  rgba(99,102,241,.07) 0%,transparent 65%);
}
.bg-grid{position:absolute;inset:0;
  background-image:
    linear-gradient(rgba(255,255,255,.025) 1px,transparent 1px),
    linear-gradient(90deg,rgba(255,255,255,.025) 1px,transparent 1px);
  background-size:50px 50px;
}
.orb{position:absolute;border-radius:50%;filter:blur(90px);animation:pulse 7s ease-in-out infinite;}
.orb1{width:600px;height:600px;top:-200px;left:-200px;background:rgba(26,86,219,.18);animation-delay:0s;}
.orb2{width:450px;height:450px;bottom:-120px;right:-120px;background:rgba(99,102,241,.13);animation-delay:-3.5s;}
.orb3{width:300px;height:300px;top:50%;left:50%;transform:translate(-50%,-50%);background:rgba(245,158,11,.04);animation-delay:-1.5s;}
@keyframes pulse{0%,100%{transform:scale(1);}50%{transform:scale(1.08);}}

.wrap{position:relative;z-index:10;width:100%;max-width:420px;padding:24px;
  animation:up .55s cubic-bezier(.22,1,.36,1) both;}
@keyframes up{from{opacity:0;transform:translateY(28px);}to{opacity:1;transform:none;}}

.tag{text-align:center;margin-bottom:20px;}
.tag span{
  background:rgba(26,86,219,.18);border:1px solid rgba(26,86,219,.35);
  color:#93c5fd;font-size:10.5px;font-weight:700;letter-spacing:.14em;
  text-transform:uppercase;padding:5px 16px;border-radius:20px;
  backdrop-filter:blur(8px);
}

/* Glass card */
.card{
  background:rgba(255,255,255,.07);
  backdrop-filter:blur(28px) saturate(180%);
  -webkit-backdrop-filter:blur(28px) saturate(180%);
  border:1px solid rgba(255,255,255,.13);
  border-radius:24px;padding:36px 34px 30px;
  box-shadow:0 8px 40px rgba(0,0,0,.5),inset 0 1px 0 rgba(255,255,255,.1);
}

/* Brand */
.brand{display:flex;align-items:center;gap:12px;margin-bottom:26px;padding-bottom:22px;border-bottom:1px solid var(--border);}
.b-logo{width:44px;height:44px;object-fit:contain;border-radius:10px;flex-shrink:0;}
.b-fall{width:44px;height:44px;background:linear-gradient(135deg,var(--brand),var(--brand2));
  border-radius:12px;display:none;align-items:center;justify-content:center;
  font-family:'Playfair Display',serif;font-size:17px;font-weight:800;color:#fff;flex-shrink:0;
  box-shadow:0 4px 14px rgba(26,86,219,.4);}
.b-text h1{font-family:'Playfair Display',serif;font-size:16px;font-weight:700;color:#fff;}
.b-text p{font-size:11px;color:var(--muted);margin-top:2px;}

.title{margin-bottom:22px;}
.title h2{font-size:22px;font-weight:700;color:#fff;}
.title p{font-size:13px;color:var(--muted);margin-top:4px;}

.err{background:rgba(248,113,113,.12);color:#fca5a5;border:1px solid rgba(248,113,113,.3);
  border-radius:10px;padding:11px 14px;font-size:13px;font-weight:500;
  display:flex;align-items:center;gap:8px;margin-bottom:16px;
  animation:shake .4s ease;}
@keyframes shake{0%{transform:translateX(-5px);opacity:0;}50%{transform:translateX(4px);}100%{transform:translateX(0);opacity:1;}}

.fg{margin-bottom:14px;}
.fl{display:block;font-size:12px;font-weight:600;color:rgba(255,255,255,.65);margin-bottom:6px;letter-spacing:.03em;}
.iw{position:relative;}
.iw .fi{position:absolute;left:13px;top:50%;transform:translateY(-50%);color:rgba(255,255,255,.25);font-size:13px;pointer-events:none;z-index:1;}
.fi-input{
  width:100%;padding:11px 14px 11px 38px;
  background:var(--input-bg);
  border:1.5px solid rgba(255,255,255,.1);
  border-radius:11px;font-size:14px;
  font-family:'Outfit',sans-serif;color:#fff;outline:none;
  transition:border-color .15s,box-shadow .15s,background .15s;
}
.fi-input::placeholder{color:rgba(255,255,255,.22);}
.fi-input:focus{
  border-color:rgba(26,86,219,.7);
  background:rgba(26,86,219,.12);
  box-shadow:0 0 0 3px rgba(26,86,219,.18);
}
.eye{position:absolute;right:11px;top:50%;transform:translateY(-50%);
  background:none;border:none;cursor:pointer;color:rgba(255,255,255,.28);
  font-size:14px;padding:4px;z-index:2;transition:color .15s;}
.eye:hover{color:rgba(255,255,255,.7);}

.btn-login{
  width:100%;padding:13px;
  background:linear-gradient(135deg,var(--brand),var(--brand2));
  color:#fff;border:none;border-radius:11px;
  font-size:15px;font-weight:700;font-family:'Outfit',sans-serif;
  cursor:pointer;display:flex;align-items:center;justify-content:center;gap:8px;
  margin-top:8px;box-shadow:0 6px 24px rgba(26,86,219,.4);
  transition:opacity .15s,transform .15s;
}
.btn-login:hover{opacity:.9;transform:translateY(-1px);}
.btn-login:active{transform:none;}

.forgot{
  display:block;text-align:center;margin-top:14px;
  font-size:12.5px;color:rgba(255,255,255,.35);
  text-decoration:none;transition:color .15s;
}
.forgot:hover{color:#93c5fd;}

.ft{text-align:center;margin-top:18px;font-size:11px;color:rgba(255,255,255,.2);}
.ft a{color:rgba(147,197,253,.45);text-decoration:none;}
.ft a:hover{color:#93c5fd;}

@media(max-width:480px){.card{padding:28px 20px 24px;}.wrap{padding:16px;}}
</style>
</head>
<body>
<div class="bg">
  <div class="bg-img"></div>
  <div class="bg-grid"></div>
  <div class="orb orb1"></div>
  <div class="orb orb2"></div>
  <div class="orb orb3"></div>
</div>

<div class="wrap">
  <div class="tag"><span><i class="fas fa-shield-halved"></i> &nbsp;Admin Portal</span></div>

  <div class="card">
    <div class="brand">
      <img src="<?=e(SITE_URL)?>/assets/images/cyconsultancy.png" class="b-logo"
        onerror="this.style.display='none';document.getElementById('bf').style.display='flex';">
      <div class="b-fall" id="bf">CY</div>
      <div class="b-text">
        <h1>CY Consultancy Centre</h1>
        <p>Student Visa &amp; Immigration Specialists</p>
      </div>
    </div>

    <div class="title">
      <h2>Welcome back 👋</h2>
      <p>Sign in to your admin account</p>
    </div>

    <?php if($error):?>
    <div class="err"><i class="fas fa-circle-exclamation"></i><?=e($error)?></div>
    <?php endif;?>

    <form method="POST" autocomplete="off">
      <div class="fg">
        <label class="fl">Username</label>
        <div class="iw">
          <i class="fas fa-user fi"></i>
          <input type="text" name="username" class="fi-input"
            placeholder="Enter username"
            value="<?=e($_POST['username']??'')?>"
            required autofocus autocomplete="username">
        </div>
      </div>
      <div class="fg">
        <label class="fl">Password</label>
        <div class="iw">
          <i class="fas fa-lock fi"></i>
          <input type="password" id="pw" name="password" class="fi-input"
            placeholder="Enter password" required autocomplete="current-password">
          <button type="button" class="eye" onclick="tp()">
            <i class="fas fa-eye" id="ei"></i>
          </button>
        </div>
      </div>
      <button type="submit" class="btn-login">
        <i class="fas fa-right-to-bracket"></i> Sign In
      </button>
    </form>

    <a href="<?=e(ADMIN_URL)?>/forgot-password.php" class="forgot">
      <i class="fas fa-key" style="font-size:11px;margin-right:3px;"></i>Forgot password?
    </a>
  </div>

  <div class="ft">&copy; <?=date('Y')?> CY Consultancy Centre &nbsp;·&nbsp; <a href="<?=e(SITE_URL)?>">Visit Website</a></div>
</div>

<script>
function tp(){
  const i=document.getElementById('pw'),e=document.getElementById('ei');
  i.type=i.type==='password'?'text':'password';
  e.className=i.type==='password'?'fas fa-eye':'fas fa-eye-slash';
}
</script>
</body>
</html>
