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

session_start();
require_once 'includes/config.php';
require_once 'includes/functions.php';

// Jika sudah login, redirect ke dashboard sesuai role
if (isLoggedIn()) {
    if (isAdmin()) {
        redirect('admin/dashboard.php');
    } elseif (isAdminSoal()) {
        redirect('admin_soal/dashboard.php');
    } elseif (isPenguji()) {
        redirect('penguji/dashboard_penguji.php');
    } elseif (isReviewer()) {
        redirect('reviewer/dashboard_reviewer.php');
    } else {
        // Default redirect untuk user biasa
        redirect('dashboard.php');
    }
    exit;
}

$error = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $username = sanitize($_POST['username']);
    $password = $_POST['password'];
    
    if (empty($username) || empty($password)) {
        $error = "Username dan password harus diisi!";
    } else {
        // Cek di tabel users_preview terlebih dahulu
        $stmt = $pdo->prepare("SELECT * FROM users_preview WHERE username = ?");
        $stmt->execute([$username]);
        $user = $stmt->fetch();
        
        // Jika tidak ditemukan di users_preview, coba di tabel users
        if (!$user) {
            $stmt = $pdo->prepare("SELECT * FROM users WHERE username = ?");
            $stmt->execute([$username]);
            $user = $stmt->fetch();
        }
        
        if ($user && password_verify($password, $user['password'])) {
            // Cek status user
            if (isset($user['status']) && $user['status'] == 'inactive') {
                $error = "Akun Anda dinonaktifkan. Silakan hubungi administrator.";
                error_log("Login attempt for inactive user: " . $username);
            } else {
                // Login berhasil dan akun aktif
                $_SESSION['user_id'] = $user['id'];
                $_SESSION['user_role'] = $user['role'];
                $_SESSION['user_name'] = $user['nama_lengkap'] ?? $user['username'];
                $_SESSION['user_email'] = $user['email'] ?? '';
                
                // Log aktivitas login
                error_log("User logged in: " . $username . " with role: " . $user['role']);
                
                // Redirect berdasarkan role
                $redirect_url = 'dashboard.php';
                switch ($user['role']) {
                    case 'admin':
                        $redirect_url = 'admin/dashboard.php';
                        break;
                    case 'admin_soal':
                        $redirect_url = 'admin_soal/dashboard.php';
                        break;
                    case 'penguji':
                        $redirect_url = 'penguji/dashboard_penguji.php';
                        break;
                    case 'reviewer':
                        $redirect_url = 'reviewer/dashboard_reviewer.php';
                        break;
                    default:
                        $redirect_url = 'dashboard.php';
                        break;
                }
                
                // Cek apakah folder dan file dashboard reviewer ada
                if ($user['role'] == 'reviewer') {
                    $reviewer_dashboard = __DIR__ . '/reviewer/dashboard_reviewer.php';
                    if (!file_exists($reviewer_dashboard)) {
                        // Jika file belum ada, buat folder dan file default
                        if (!is_dir(__DIR__ . '/reviewer')) {
                            mkdir(__DIR__ . '/reviewer', 0755, true);
                        }
                        // Buat file dashboard_reviewer.php sederhana
                        $dashboard_content = '<?php
session_start();
require_once "../includes/config.php";
require_once "../includes/functions.php";

// Cek apakah user sudah login dan memiliki akses reviewer
if (!isset($_SESSION["user_id"]) || $_SESSION["user_role"] != "reviewer") {
    header("Location: ../login.php");
    exit;
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Dashboard Reviewer - SOCA OSCE System</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { font-family: "Segoe UI", Arial, sans-serif; background: #f0f4f8; display: flex; min-height: 100vh; }
        .sidebar { width: 260px; background: linear-gradient(180deg, #1a2332 0%, #0f1724 100%); color: #fff; padding: 0; position: fixed; height: 100vh; overflow-y: auto; z-index: 100; transition: all 0.3s ease; }
        .sidebar-header { padding: 25px 20px 20px; border-bottom: 1px solid rgba(255,255,255,0.08); text-align: center; }
        .sidebar-header h2 { font-size: 20px; font-weight: 700; letter-spacing: 1px; background: linear-gradient(135deg, #60a5fa, #a78bfa); -webkit-background-clip: text; -webkit-text-fill-color: transparent; }
        .sidebar-header p { font-size: 12px; color: rgba(255,255,255,0.5); margin-top: 4px; letter-spacing: 2px; text-transform: uppercase; }
        .sidebar-menu { list-style: none; padding: 15px 0; }
        .sidebar-menu li { margin: 2px 12px; }
        .sidebar-menu li a { display: flex; align-items: center; gap: 14px; padding: 12px 16px; color: rgba(255,255,255,0.6); text-decoration: none; border-radius: 12px; transition: all 0.3s ease; font-size: 14px; font-weight: 500; }
        .sidebar-menu li a:hover { background: rgba(255,255,255,0.08); color: #fff; transform: translateX(4px); }
        .sidebar-menu li a.active { background: linear-gradient(135deg, rgba(96,165,250,0.2), rgba(167,139,250,0.2)); color: #60a5fa; box-shadow: inset 3px 0 0 #60a5fa; }
        .sidebar-menu .menu-divider { height: 1px; background: rgba(255,255,255,0.06); margin: 12px 20px; }
        .sidebar-menu .menu-label { padding: 10px 20px 6px; font-size: 10px; text-transform: uppercase; letter-spacing: 1.5px; color: rgba(255,255,255,0.25); font-weight: 600; }
        .main-content { margin-left: 260px; flex: 1; padding: 30px 35px 40px; min-height: 100vh; }
        .header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 30px; flex-wrap: wrap; gap: 15px; }
        .header-left h1 { font-size: 28px; font-weight: 700; color: #1a2332; }
        .header-left .subtitle { font-size: 14px; color: #6b7a8f; margin-top: 4px; }
        .header-right .user-badge { display: flex; align-items: center; gap: 10px; background: #fff; padding: 8px 16px 8px 12px; border-radius: 50px; box-shadow: 0 2px 10px rgba(0,0,0,0.06); }
        .header-right .user-badge .avatar { width: 36px; height: 36px; border-radius: 50%; background: linear-gradient(135deg, #60a5fa, #a78bfa); display: flex; align-items: center; justify-content: center; color: #fff; font-weight: 600; font-size: 16px; }
        .header-right .user-badge .user-name { font-weight: 600; font-size: 14px; color: #1a2332; }
        .header-right .user-badge .user-role { font-size: 12px; color: #6b7a8f; }
        .btn-logout { background: #fee2e2; color: #dc2626; border: none; padding: 8px 16px; border-radius: 50px; font-size: 13px; font-weight: 600; cursor: pointer; transition: all 0.3s ease; text-decoration: none; display: inline-flex; align-items: center; gap: 8px; }
        .btn-logout:hover { background: #fecaca; }
        .card { background: #fff; border-radius: 16px; padding: 24px 28px; margin-bottom: 25px; box-shadow: 0 2px 12px rgba(0,0,0,0.05); border: 1px solid rgba(0,0,0,0.04); }
        .card-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 18px; flex-wrap: wrap; gap: 10px; }
        .card-header h2 { font-size: 18px; font-weight: 700; color: #1a2332; display: flex; align-items: center; gap: 10px; }
        .card-header h2 i { color: #60a5fa; }
        .greeting-card { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: #fff; border-radius: 16px; padding: 30px 35px; margin-bottom: 25px; }
        .greeting-card h2 { font-size: 24px; font-weight: 700; }
        .greeting-card p { opacity: 0.9; margin-top: 6px; }
        .stats-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 16px; }
        .stat-card { background: #fff; border-radius: 14px; padding: 20px 22px; box-shadow: 0 2px 12px rgba(0,0,0,0.05); border: 1px solid rgba(0,0,0,0.04); transition: all 0.3s ease; }
        .stat-card:hover { transform: translateY(-3px); box-shadow: 0 6px 24px rgba(0,0,0,0.08); }
        .stat-card .stat-number { font-size: 28px; font-weight: 700; color: #1a2332; }
        .stat-card .stat-label { font-size: 13px; color: #6b7a8f; font-weight: 500; }
        .stat-card .stat-icon { float: right; font-size: 28px; opacity: 0.3; }
        .stat-card.purple .stat-icon { color: #7c3aed; }
        .stat-card.green .stat-icon { color: #059669; }
        .stat-card.blue .stat-icon { color: #2563eb; }
        .stat-card.orange .stat-icon { color: #d97706; }
        .btn { padding: 8px 18px; border: none; border-radius: 8px; font-size: 13px; font-weight: 600; cursor: pointer; transition: all 0.3s ease; display: inline-flex; align-items: center; gap: 6px; text-decoration: none; }
        .btn-primary { background: linear-gradient(135deg, #2563eb, #7c3aed); color: #fff; }
        .btn-primary:hover { transform: translateY(-2px); box-shadow: 0 4px 20px rgba(37,99,235,0.3); }
        .btn-secondary { background: #f1f5f9; color: #475569; }
        .btn-secondary:hover { background: #e2e8f0; }
        .hamburger { display: none; background: none; border: none; font-size: 24px; color: #1a2332; cursor: pointer; padding: 8px; }
        @media (max-width: 768px) { .sidebar { width: 0; transform: translateX(-100%); } .sidebar.open { width: 280px; transform: translateX(0); } .main-content { margin-left: 0; padding: 15px; } .hamburger { display: block; } .header { flex-direction: column; align-items: flex-start; } .stats-grid { grid-template-columns: 1fr 1fr; } }
        @media (max-width: 480px) { .stats-grid { grid-template-columns: 1fr; } .header-left h1 { font-size: 22px; } }
    </style>
</head>
<body>
    <div class="sidebar" id="sidebar">
        <div class="sidebar-header">
            <h2>SOCA/OSCE</h2>
            <p>Reviewer Panel</p>
        </div>
        <ul class="sidebar-menu">
            <li class="menu-label">Main Menu</li>
            <li><a href="dashboard_reviewer.php" class="active"><i class="fas fa-th-large"></i> Dashboard</a></li>
            <li class="menu-divider"></li>
            <li class="menu-label">Review</li>
            <li><a href="review_soal_teori_s1.php"><i class="fas fa-book-open"></i> Review Soal Teori S1</a></li>
            <li><a href="review_soal_teori.php"><i class="fas fa-book"></i> Review Soal Teori</a></li>
            <li><a href="review_soal_praktik.php"><i class="fas fa-clipboard-check"></i> Review Soal Praktik</a></li>
            <li><a href="preview_aspek_penilaian.php"><i class="fas fa-eye"></i> Preview Aspek Penilaian</a></li>
            <li class="menu-divider"></li>
            <li><a href="riwayat_review.php"><i class="fas fa-history"></i> Riwayat Review</a></li>
            <li><a href="../logout.php"><i class="fas fa-sign-out-alt"></i> Logout</a></li>
        </ul>
    </div>

    <div class="main-content">
        <div class="header">
            <div class="header-left">
                <button class="hamburger" id="hamburgerBtn"><i class="fas fa-bars"></i></button>
                <h1>Dashboard Reviewer</h1>
                <div class="subtitle">Selamat datang di panel reviewer</div>
            </div>
            <div class="header-right">
                <div class="user-badge">
                    <div class="avatar"><?php echo strtoupper(substr($_SESSION["user_name"] ?? "R", 0, 1)); ?></div>
                    <div>
                        <div class="user-name"><?php echo $_SESSION["user_name"] ?? "Reviewer"; ?></div>
                        <div class="user-role">Reviewer</div>
                    </div>
                </div>
                <a href="../logout.php" class="btn-logout"><i class="fas fa-sign-out-alt"></i> Logout</a>
            </div>
        </div>

        <div class="greeting-card">
            <h2>Selamat datang, <?php echo $_SESSION["user_name"] ?? "Reviewer"; ?>! 👋</h2>
            <p>Anda dapat mereview soal teori dan praktik untuk memastikan kualitas soal sebelum digunakan dalam ujian.</p>
        </div>

        <div class="stats-grid">
            <div class="stat-card purple">
                <div class="stat-icon"><i class="fas fa-book-open"></i></div>
                <div class="stat-number">0</div>
                <div class="stat-label">Soal Teori S1</div>
            </div>
            <div class="stat-card blue">
                <div class="stat-icon"><i class="fas fa-book"></i></div>
                <div class="stat-number">0</div>
                <div class="stat-label">Soal Teori</div>
            </div>
            <div class="stat-card green">
                <div class="stat-icon"><i class="fas fa-clipboard-check"></i></div>
                <div class="stat-number">0</div>
                <div class="stat-label">Soal Praktik</div>
            </div>
            <div class="stat-card orange">
                <div class="stat-icon"><i class="fas fa-check-circle"></i></div>
                <div class="stat-number">0</div>
                <div class="stat-label">Sudah Direview</div>
            </div>
        </div>

        <div class="card">
            <div class="card-header">
                <h2><i class="fas fa-tasks"></i> Tugas Review</h2>
            </div>
            <p style="color: #6b7a8f; text-align: center; padding: 30px 0;">Belum ada tugas review yang diberikan.</p>
        </div>
    </div>

    <script>
        document.getElementById("hamburgerBtn").addEventListener("click", function() {
            document.getElementById("sidebar").classList.toggle("open");
        });
    </script>
</body>
</html>';
                        file_put_contents($reviewer_dashboard, $dashboard_content);
                    }
                }
                
                redirect($redirect_url);
                exit;
            }
        } else {
            $error = "Username atau password salah!";
        }
    }
}

?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <title>Login | OSCE System - FKG UMS</title>
    <!-- Google Fonts + Font Awesome -->
    <link href="https://fonts.googleapis.com/css2?family=Inter:opsz,wght@14..32,300;14..32,400;14..32,500;14..32,600;14..32,700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Inter', 'Segoe UI', sans-serif;
            background: linear-gradient(145deg, #f0f4ff 0%, #e9eefe 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 1.5rem;
            position: relative;
            overflow-x: hidden;
        }

        /* decorative abstract shapes */
        body::before {
            content: "";
            position: absolute;
            top: -20%;
            left: -10%;
            width: 500px;
            height: 500px;
            background: radial-gradient(circle, rgba(102,126,234,0.15) 0%, rgba(118,75,162,0.05) 80%);
            border-radius: 50%;
            pointer-events: none;
            z-index: 0;
        }

        body::after {
            content: "";
            position: absolute;
            bottom: -15%;
            right: -10%;
            width: 550px;
            height: 550px;
            background: radial-gradient(circle, rgba(118,75,162,0.12) 0%, rgba(102,126,234,0.03) 80%);
            border-radius: 50%;
            pointer-events: none;
            z-index: 0;
        }

        .login-container {
            width: 100%;
            max-width: 460px;
            position: relative;
            z-index: 2;
            animation: fadeSlideUp 0.7s ease-out;
        }

        @keyframes fadeSlideUp {
            0% {
                opacity: 0;
                transform: translateY(30px);
            }
            100% {
                opacity: 1;
                transform: translateY(0);
            }
        }

        .glass-card {
            background: rgba(255, 255, 255, 0.96);
            backdrop-filter: blur(0px);
            border-radius: 2rem;
            padding: 2rem 2rem 2.2rem;
            box-shadow: 0 25px 45px -12px rgba(0, 0, 0, 0.25), 0 8px 18px rgba(0, 0, 0, 0.05);
            border: 1px solid rgba(255, 255, 255, 0.5);
            transition: transform 0.25s ease, box-shadow 0.3s ease;
        }

        .glass-card:hover {
            transform: translateY(-3px);
            box-shadow: 0 30px 55px -14px rgba(0, 0, 0, 0.3);
        }

        /* header section */
        .brand-header {
            text-align: center;
            margin-bottom: 1.8rem;
        }

        .logo-circle {
            width: 70px;
            height: 70px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 28px;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0 auto 1rem auto;
            box-shadow: 0 10px 18px -8px rgba(102,126,234,0.4);
        }

        .logo-circle i {
            font-size: 2.5rem;
            color: white;
        }

        .brand-header h1 {
            font-size: 1.9rem;
            font-weight: 700;
            background: linear-gradient(135deg, #2c3e66 0%, #4a2c6d 100%);
            background-clip: text;
            -webkit-background-clip: text;
            color: transparent;
            letter-spacing: -0.3px;
        }

        .brand-header .sub {
            color: #6c7a8e;
            font-size: 0.9rem;
            font-weight: 500;
            margin-top: 0.3rem;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 6px;
        }

        .brand-header .sub i {
            font-size: 0.75rem;
            color: #7c5e9a;
        }

        /* alert message */
        .alert-modern {
            padding: 0.9rem 1rem;
            border-radius: 1.2rem;
            margin-bottom: 1.8rem;
            font-size: 0.85rem;
            font-weight: 500;
            display: flex;
            align-items: center;
            gap: 12px;
            backdrop-filter: blur(4px);
            animation: shakeLight 0.4s ease;
        }

        @keyframes shakeLight {
            0%,100%{ transform: translateX(0);}
            25%{ transform: translateX(-5px);}
            75%{ transform: translateX(5px);}
        }

        .alert-error {
            background: #fff2f0;
            border-left: 4px solid #e5484d;
            color: #b91c2c;
            box-shadow: 0 2px 8px rgba(229,72,77,0.1);
        }

        .alert-error i {
            font-size: 1.1rem;
            color: #e5484d;
        }

        /* form styling */
        .form-group {
            margin-bottom: 1.4rem;
        }

        .form-group label {
            display: block;
            font-size: 0.85rem;
            font-weight: 600;
            color: #1e2a3e;
            margin-bottom: 0.5rem;
            letter-spacing: -0.2px;
        }

        .input-wrapper {
            position: relative;
            display: flex;
            align-items: center;
        }

        .input-wrapper i.input-icon {
            position: absolute;
            left: 18px;
            font-size: 1.1rem;
            color: #9aa6b9;
            transition: color 0.2s;
            pointer-events: none;
        }

        .input-wrapper input {
            width: 100%;
            padding: 0.9rem 1rem 0.9rem 2.8rem;
            font-size: 0.95rem;
            font-family: 'Inter', monospace;
            background: #f9fafc;
            border: 1.5px solid #e2e8f0;
            border-radius: 1.2rem;
            transition: all 0.25s ease;
            color: #111827;
            font-weight: 500;
        }

        .input-wrapper input:focus {
            outline: none;
            border-color: #8b5cf6;
            background: #ffffff;
            box-shadow: 0 0 0 4px rgba(139, 92, 246, 0.15);
        }

        .input-wrapper input:hover {
            border-color: #cbd5e1;
        }

        /* toggle password (optional but nice) */
        .toggle-password {
            position: absolute;
            right: 18px;
            background: none;
            border: none;
            cursor: pointer;
            color: #94a3b8;
            font-size: 1rem;
            transition: color 0.2s;
            z-index: 3;
        }

        .toggle-password:hover {
            color: #667eea;
        }

        /* login button */
        .login-btn {
            width: 100%;
            background: linear-gradient(105deg, #667eea 0%, #7c5e9a 100%);
            border: none;
            padding: 0.9rem;
            border-radius: 1.5rem;
            font-weight: 600;
            font-size: 1rem;
            color: white;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 12px;
            cursor: pointer;
            transition: all 0.3s ease;
            margin-top: 0.5rem;
            letter-spacing: 0.3px;
            box-shadow: 0 8px 18px -8px rgba(102,126,234,0.4);
        }

        .login-btn i {
            font-size: 1rem;
            transition: transform 0.2s;
        }

        .login-btn:hover {
            background: linear-gradient(105deg, #5a73e0 0%, #6a4c8c 100%);
            transform: translateY(-2px);
            box-shadow: 0 18px 28px -12px rgba(102,126,234,0.5);
        }

        .login-btn:active {
            transform: translateY(1px);
        }

        /* footer */
        .footer-note {
            text-align: center;
            margin-top: 2rem;
            font-size: 0.75rem;
            color: #6c7a91;
            border-top: 1px solid #edf2f7;
            padding-top: 1.5rem;
            display: flex;
            justify-content: center;
            gap: 8px;
            flex-wrap: wrap;
        }

        .footer-note span {
            font-weight: 500;
            color: #4f46e5;
        }

        /* responsive touches */
        @media (max-width: 480px) {
            .glass-card {
                padding: 1.8rem 1.5rem;
            }
            .brand-header h1 {
                font-size: 1.6rem;
            }
            .logo-circle {
                width: 60px;
                height: 60px;
            }
            .logo-circle i {
                font-size: 2rem;
            }
        }
    </style>
</head>
<body>
    <div class="login-container">
        <div class="glass-card">
            <div class="brand-header">
                <div class="logo-circle">
                    <i class="fas fa-tooth"></i>
                </div>
                <h1>OSCE</h1>
                <div class="sub">
                    <i class="fas fa-graduation-cap"></i>
                    <span>Sistem Penilaian Kompetensi Klinis</span>
                </div>
            </div>

            <!-- pesan error dinamis dari PHP akan muncul di sini dengan gaya modern -->
            <?php if (!empty($error)): ?>
                <div class="alert-modern alert-error">
                    <i class="fas fa-exclamation-triangle"></i>
                    <span><?php echo htmlspecialchars($error); ?></span>
                </div>
            <?php endif; ?>

            <form method="POST" class="login-form" id="loginForm">
                <div class="form-group">
                    <label for="username"><i class="far fa-user-circle" style="margin-right: 6px;"></i> Username</label>
                    <div class="input-wrapper">
                        <i class="fas fa-user input-icon"></i>
                        <input type="text" id="username" name="username" 
                               placeholder="Masukkan username" 
                               value="<?php echo isset($_POST['username']) ? htmlspecialchars($_POST['username']) : ''; ?>"
                               autocomplete="off" required>
                    </div>
                </div>

                <div class="form-group">
                    <label for="password"><i class="fas fa-key"></i> Password</label>
                    <div class="input-wrapper">
                        <i class="fas fa-lock input-icon"></i>
                        <input type="password" id="password" name="password" 
                               placeholder="Kata sandi" required>
                        <button type="button" class="toggle-password" id="togglePwdBtn" tabindex="-1">
                            <i class="far fa-eye-slash"></i>
                        </button>
                    </div>
                </div>

                <button type="submit" class="login-btn">
                    <i class="fas fa-arrow-right-to-bracket"></i> Masuk ke Dashboard
                </button>
            </form>

            <div class="footer-note">
                <i class="fas fa-shield-alt"></i> 
                <span>OSCE</span> · Fakultas Kedokteran Gigi UMS
            </div>
        </div>
    </div>

    <!-- tambahan javascript kecil untuk toggle password + subtle effect (non-intrusive) -->
    <script>
        (function() {
            // toggle visibility untuk password
            const pwdField = document.getElementById('password');
            const toggleBtn = document.getElementById('togglePwdBtn');
            if (pwdField && toggleBtn) {
                toggleBtn.addEventListener('click', function(e) {
                    e.preventDefault();
                    const type = pwdField.getAttribute('type') === 'password' ? 'text' : 'password';
                    pwdField.setAttribute('type', type);
                    // ganti icon
                    const icon = toggleBtn.querySelector('i');
                    if (icon) {
                        if (type === 'text') {
                            icon.classList.remove('fa-eye-slash');
                            icon.classList.add('fa-eye');
                        } else {
                            icon.classList.remove('fa-eye');
                            icon.classList.add('fa-eye-slash');
                        }
                    }
                });
            }

            // efek smooth focus & validasi sederhana client-side (opsional)
            const form = document.getElementById('loginForm');
            if (form) {
                form.addEventListener('submit', function(e) {
                    const username = document.getElementById('username')?.value.trim();
                    const password = document.getElementById('password')?.value;
                    if (!username || !password) {
                        if (!username) {
                            e.preventDefault();
                            showTemporaryMessage('Username tidak boleh kosong', 'error');
                        } else if (!password) {
                            e.preventDefault();
                            showTemporaryMessage('Password tidak boleh kosong', 'error');
                        }
                    }
                });
            }

            function showTemporaryMessage(msg, type) {
                const container = document.querySelector('.glass-card');
                const oldAlert = container.querySelector('.client-alert-temp');
                if(oldAlert) oldAlert.remove();
                const alertDiv = document.createElement('div');
                alertDiv.className = 'alert-modern alert-error client-alert-temp';
                alertDiv.innerHTML = `<i class="fas fa-exclamation-circle"></i><span>${msg}</span>`;
                const formElem = document.querySelector('.login-form');
                if(formElem) formElem.parentNode.insertBefore(alertDiv, formElem);
                setTimeout(() => {
                    if(alertDiv) alertDiv.style.opacity = '0';
                    setTimeout(() => { if(alertDiv) alertDiv.remove(); }, 400);
                }, 2500);
            }
        })();
    </script>
</body>
</html>