<?php
session_start();

$admin_username = 'admin';
$admin_password = 'moded123';

$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = trim($_POST['username'] ?? '');
    $password = trim($_POST['password'] ?? '');
    $remember = isset($_POST['remember']);

    if ($username === $admin_username && $password === $admin_password) {
        $_SESSION['admin'] = true;
        $_SESSION['admin_username'] = $username;

        if ($remember) {
            setcookie('saved_admin_username', $username, time() + (86400 * 30), '/', '', true, true);
        } else {
            setcookie('saved_admin_username', '', time() - 3600, '/', '', true, true);
        }

        header('Location: dashboard.php');
        exit;
    } else {
        $error = 'اسم المستخدم أو كلمة المرور غير صحيحة';
    }
}

$saved_username = $_COOKIE['saved_admin_username'] ?? '';
?>
<!doctype html>
<html lang="ar" dir="rtl">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>دخول الأدمن</title>

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

        body {
            margin: 0;
            min-height: 100vh;
            font-family: Tahoma, Arial, sans-serif;
            background:
                radial-gradient(circle at top right, rgba(22, 163, 74, .18), transparent 32%),
                radial-gradient(circle at bottom left, rgba(37, 99, 235, .14), transparent 35%),
                linear-gradient(135deg, #f8fafc, #eef2f7);
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 18px;
            color: #0f172a;
        }

        .login-wrap {
            width: 100%;
            max-width: 430px;
        }

        .login-card {
            background: rgba(255,255,255,.92);
            border: 1px solid rgba(255,255,255,.8);
            border-radius: 24px;
            box-shadow: 0 24px 60px rgba(15, 23, 42, .14);
            padding: 28px;
            backdrop-filter: blur(14px);
        }

        .logo {
            width: 76px;
            height: 76px;
            margin: 0 auto 14px;
            border-radius: 24px;
            background: linear-gradient(135deg, #16a34a, #0f766e);
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 32px;
            font-weight: 900;
            box-shadow: 0 14px 28px rgba(22, 163, 74, .3);
        }

        h1 {
            margin: 0 0 8px;
            text-align: center;
            font-size: 32px;
            font-weight: 900;
        }

        .subtitle {
            text-align: center;
            margin-bottom: 22px;
            color: #64748b;
            font-size: 15px;
        }

        .alert {
            background: #fee2e2;
            color: #991b1b;
            border: 1px solid #fecaca;
            padding: 12px;
            border-radius: 14px;
            text-align: center;
            margin-bottom: 16px;
            font-weight: 700;
        }

        label {
            display: block;
            margin-bottom: 8px;
            font-weight: 800;
            color: #334155;
        }

        .field {
            margin-bottom: 16px;
        }

        input[type="text"],
        input[type="password"] {
            width: 100%;
            height: 48px;
            border-radius: 14px;
            border: 1px solid #cbd5e1;
            padding: 0 14px;
            font-size: 17px;
            background: #f8fafc;
            outline: none;
            transition: .2s;
            direction: ltr;
            text-align: left;
        }

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

        .remember-row {
            display: flex;
            align-items: center;
            gap: 8px;
            margin: 4px 0 18px;
            color: #475569;
            font-size: 15px;
            font-weight: 700;
        }

        .remember-row input {
            width: 18px;
            height: 18px;
            accent-color: #16a34a;
        }

        button {
            width: 100%;
            height: 50px;
            border: 0;
            border-radius: 15px;
            background: linear-gradient(135deg, #16a34a, #15803d);
            color: white;
            font-size: 18px;
            font-weight: 900;
            cursor: pointer;
            box-shadow: 0 14px 26px rgba(22, 163, 74, .28);
            transition: .2s;
        }

        button:hover {
            transform: translateY(-1px);
            background: linear-gradient(135deg, #15803d, #166534);
        }

        .footer-note {
            margin-top: 16px;
            text-align: center;
            color: #94a3b8;
            font-size: 13px;
        }

        @media (max-width: 480px) {
            body {
                align-items: flex-start;
                padding-top: 70px;
            }

            .login-card {
                padding: 22px;
                border-radius: 20px;
            }

            h1 {
                font-size: 27px;
            }

            .logo {
                width: 64px;
                height: 64px;
                font-size: 27px;
            }

            input[type="text"],
            input[type="password"],
            button {
                height: 46px;
                font-size: 16px;
            }
        }

        @media (min-width: 900px) {
            .login-wrap {
                max-width: 460px;
            }

            .login-card {
                padding: 34px;
            }
        }
    </style>
</head>
<body>

<div class="login-wrap">
    <div class="login-card">
        <div class="logo">A</div>

        <h1>دخول الأدمن</h1>
        <div class="subtitle">لوحة التحكم الخاصة بالإدارة</div>

        <?php if ($error): ?>
            <div class="alert"><?= htmlspecialchars($error, ENT_QUOTES, 'UTF-8') ?></div>
        <?php endif; ?>

        <form method="post" autocomplete="on">
            <div class="field">
                <label for="username">اسم المستخدم</label>
                <input
                    type="text"
                    id="username"
                    name="username"
                    value="<?= htmlspecialchars($saved_username, ENT_QUOTES, 'UTF-8') ?>"
                    placeholder="admin"
                    required
                    autofocus
                    autocomplete="username"
                >
            </div>

            <div class="field">
                <label for="password">كلمة المرور</label>
                <input
                    type="password"
                    id="password"
                    name="password"
                    placeholder="••••••••"
                    required
                    autocomplete="current-password"
                >
            </div>

            <label class="remember-row">
                <input type="checkbox" name="remember" <?= $saved_username ? 'checked' : '' ?>>
                حفظ اسم المستخدم
            </label>

            <button type="submit">دخول</button>
        </form>

        <div class="footer-note">Responsive for mobile, tablet and desktop</div>
    </div>
</div>

</body>
</html>