<?php
require_once __DIR__ . '/db.php';

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

$error = $_GET['error'] ?? '';
$success = $_GET['success'] ?? '';
$logoVersion = @filemtime(__DIR__ . '/mainpp.png') ?: time();
?>
<!DOCTYPE html>
<html lang="ms">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Forgot Password - Dashboard Audit MAINPP</title>
    <link rel="stylesheet" href="styles.css">
    <style>
        body { background: #f8fafc; }
        .auth-shell {
            min-height: 100vh;
            display: grid;
            grid-template-columns: minmax(360px, 1fr) minmax(420px, 1fr);
            background: #fff;
        }
        .auth-visual {
            position: relative;
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            overflow: hidden;
            background:
                radial-gradient(circle at 54% 76%, rgba(255, 230, 0, 0.28) 0, rgba(255, 230, 0, 0.08) 12%, transparent 29%),
                radial-gradient(circle at 20% 18%, rgba(0, 218, 67, 0.32) 0, transparent 32%),
                linear-gradient(145deg, #031512 0%, #06241f 42%, #0b5138 100%);
        }
        .auth-visual::before {
            content: "";
            position: absolute;
            inset: 0;
            background:
                linear-gradient(90deg, rgba(255,255,255,0.04) 1px, transparent 1px),
                linear-gradient(0deg, rgba(255,255,255,0.035) 1px, transparent 1px);
            background-size: 54px 54px;
            opacity: 0.28;
        }
        .auth-visual::after {
            content: "";
            position: absolute;
            left: 50%;
            bottom: 16%;
            width: 220px;
            height: 34px;
            border-radius: 999px;
            background: rgba(1, 8, 7, 0.42);
            filter: blur(18px);
            transform: translateX(-50%);
            animation: shadowPulse 4.8s ease-in-out infinite;
        }
        .visual-logo-wrap {
            position: relative;
            width: min(340px, 56vw);
            aspect-ratio: 1;
            display: flex;
            align-items: center;
            justify-content: center;
            z-index: 1;
        }
        .visual-logo-glow {
            position: absolute;
            inset: 8%;
            border-radius: 50%;
            background:
                radial-gradient(circle, rgba(255, 230, 0, 0.22) 0, transparent 56%),
                radial-gradient(circle, rgba(0, 218, 67, 0.18) 0, transparent 68%);
            filter: blur(10px);
            animation: logoGlow 5.2s ease-in-out infinite;
        }
        .visual-logo-card {
            width: 100%;
            height: 100%;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 34px;
            animation: logoFloat 4.8s ease-in-out infinite;
            transform-origin: center;
        }
        .visual-logo-card img {
            width: 100%;
            height: 100%;
            object-fit: contain;
            filter: drop-shadow(0 26px 34px rgba(0, 0, 0, 0.32));
        }

        .visual-company-name {
            position: absolute;
            left: 50%;
            top: calc(50% + min(170px, 28vw));
            width: min(440px, 78%);
            transform: translateX(-50%);
            z-index: 2;
            color: rgba(255, 255, 255, 0.94);
            text-align: center;
            font-size: 22px;
            line-height: 1.32;
            font-weight: 900;
            letter-spacing: 0;
            text-shadow: 0 8px 24px rgba(0, 0, 0, 0.42);
        }
        .visual-company-name::after {
            content: "";
            display: block;
            width: 96px;
            height: 3px;
            margin: 12px auto 0;
            border-radius: 999px;
            background: linear-gradient(90deg, transparent, #ffe600, #00da43, transparent);
            opacity: 0.9;
        }
        @keyframes logoFloat {
            0%, 100% { transform: translateY(0) rotate(0deg) scale(1); }
            35% { transform: translateY(-14px) rotate(-1.2deg) scale(1.02); }
            70% { transform: translateY(6px) rotate(1deg) scale(0.995); }
        }
        @keyframes logoGlow {
            0%, 100% { opacity: 0.72; transform: scale(0.96); }
            50% { opacity: 1; transform: scale(1.08); }
        }
        @keyframes shadowPulse {
            0%, 100% { opacity: 0.52; transform: translateX(-50%) scaleX(0.88); }
            45% { opacity: 0.32; transform: translateX(-50%) scaleX(1.16); }
            70% { opacity: 0.46; transform: translateX(-50%) scaleX(0.98); }
        }
        .auth-panel {
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 48px 32px;
            background: #fbfcfd;
        }
        .auth-card { width: 100%; max-width: 430px; }
        .auth-brand { text-align: center; margin-bottom: 28px; }
        .auth-brand .logo-mark {
            margin: 0 auto 20px;
            width: 76px;
            height: 76px;
            border-radius: 50%;
            background: #f1f5f9;
            padding: 9px;
        }
        .auth-brand h1 {
            color: #090d12;
            font-size: 38px;
            line-height: 1.08;
            margin-bottom: 8px;
            letter-spacing: 0;
        }
        .auth-brand p { color: #5f6b7a; line-height: 1.5; font-size: 14px; }
        .auth-card label { color: #1f2937; font-weight: 700; margin-bottom: 9px; }
        .auth-card input {
            height: 48px;
            border: 1px solid #d8dee7;
            border-radius: 10px;
            background: #f7f8fa;
            box-shadow: 0 10px 18px rgba(15, 23, 42, 0.10);
            transition: border-color 0.2s ease, box-shadow 0.2s ease, background 0.2s ease;
        }
        .auth-card input:focus {
            outline: none;
            border-color: #16a34a;
            background: #fff;
            box-shadow: 0 0 0 4px rgba(22, 163, 74, 0.12), 0 12px 22px rgba(15, 23, 42, 0.12);
        }
        .auth-card .button-row { display: block; margin-top: 28px; }
        .auth-card .primary-btn {
            width: 100%;
            border-radius: 999px;
            padding: 14px 18px;
            background: linear-gradient(180deg, #1c1c1c 0%, #050505 100%);
            color: #fff;
            box-shadow: 0 12px 22px rgba(0, 0, 0, 0.28);
        }
        .auth-card .primary-btn:hover { background: linear-gradient(180deg, #0f3d2c 0%, #061c16 100%); }
        .login-row {
            margin-top: 24px;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 8px;
            color: #2d3748;
            font-size: 14px;
            font-weight: 600;
        }
        .auth-link { color: #0b7f3a; text-decoration: none; font-weight: 800; }
        .auth-link:hover { text-decoration: underline; }
        @media (max-width: 860px) {
            .auth-shell { grid-template-columns: 1fr; }
            .auth-visual { min-height: 280px; }
            .visual-logo-wrap { width: min(240px, 68vw); }
            .visual-company-name {
                top: calc(50% + 105px);
                font-size: 16px;
                width: min(350px, 88%);
            }

            .auth-panel { min-height: auto; padding: 34px 22px 42px; }
            .auth-brand h1 { font-size: 34px; }
        }
    </style>
</head>
<body>
<div class="auth-shell">
    <div class="auth-visual" aria-hidden="true">
        <div class="visual-logo-wrap">
            <div class="visual-logo-glow"></div>
            <div class="visual-logo-card">
                <img src="mainpp.png?v=<?= urlencode((string) $logoVersion) ?>" alt="">
            </div>
            <div class="visual-company-name">Majlis Agama Islam Negeri Pulau Pinang</div>
        </div>
    </div>
    <div class="auth-panel">
        <div class="auth-card">
            <div class="auth-brand">
                <div class="logo-mark"><img src="mainpp.png?v=<?= urlencode((string) $logoVersion) ?>" alt="Logo MAINPP"></div>
                <h1>Lupa Kata Laluan</h1>
                <p>Masukkan emel dan nombor kad pengenalan yang berdaftar. Kod verifikasi akan dihantar ke emel anda.</p>
            </div>

            <?php if ($success === 'code_sent'): ?>
                <div class="status-message status-success">Kod verifikasi telah dihantar ke emel anda.</div>
            <?php endif; ?>

            <?php if ($error === 'not_found'): ?>
                <div class="status-message status-danger">Emel dan nombor kad pengenalan tidak sepadan dalam rekod sistem.</div>
            <?php elseif ($error === 'inactive_account'): ?>
                <div class="status-message status-danger">Akaun ini tidak aktif. Sila hubungi pentadbir sistem.</div>
            <?php elseif ($error === 'mail_failed'): ?>
                <div class="status-message status-danger">Kod tidak dapat dihantar. Sila semak konfigurasi SMTP dan cuba lagi.</div>
            <?php elseif ($error === 'invalid_input'): ?>
                <div class="status-message status-danger">Sila isi emel dan nombor kad pengenalan.</div>
            <?php endif; ?>

            <form action="proses_forgot_password.php" method="POST">
                <div class="form-grid">
                    <div class="full-width">
                        <label for="emel">Emel</label>
                        <input type="email" id="emel" name="emel" required>
                    </div>
                    <div class="full-width">
                        <label for="ic">No. Kad Pengenalan</label>
                        <input type="text" id="ic" name="ic" placeholder="Contoh: 900101019999" required>
                    </div>
                </div>
                <div class="button-row">
                    <button type="submit" class="primary-btn">Hantar Kod</button>
                </div>
                <div class="login-row">
                    <span>Ingat kata laluan?</span>
                    <a href="login.php" class="auth-link">Log Masuk</a>
                </div>
            </form>
        </div>
    </div>
</div>
</body>
</html>
