<?php
require_once 'auth.php';
include 'db.php';
requireLogin($conn);

// Semak status & role
$user_email = $_SESSION['email'] ?? '';
$check_status = mysqli_prepare($conn, "SELECT status, role FROM users WHERE email = ?");
mysqli_stmt_bind_param($check_status, "s", $user_email);
mysqli_stmt_execute($check_status);
$status_result = mysqli_stmt_get_result($check_status);
$user_data = mysqli_fetch_assoc($status_result);

if (!$user_data || $user_data['status'] !== 'approved') {
    destroyActiveSession();
    header("Location: login.php?error=account_blocked");
    exit();
}

if (!isset($_SESSION['role'])) {
    $_SESSION['role'] = $user_data['role'] ?? 'user';
}

$role = $_SESSION['role'];
$user_id = (int) ($_SESSION['user_id'] ?? 0);

$success_message = '';
$error_message = '';

// Hardcoded modules from UAT text file
$checklist_modules = [
    'MODUL LOGIN' => [
        ['title' => 'Adakah pengguna boleh memasukkan username dan password untuk log masuk?', 'expected' => 'Pengguna berjaya masuk ke dalam sistem jika maklumat betul'],
        ['title' => 'Adakah sistem memaparkan mesej ralat jika username atau password salah?', 'expected' => 'Mesej ralat dipaparkan'],
        ['title' => 'Adakah sistem menghalang pengguna daripada mengakses sistem tanpa login?', 'expected' => 'Pengguna diarahkan ke halaman login'],
        ['title' => 'Adakah fungsi "Ingat Saya" pada halaman login berfungsi dengan baik?', 'expected' => 'Emel pengguna dipaparkan semula mengikut tetapan selepas fungsi Ingat Saya digunakan'],
        ['title' => 'Adakah pengguna boleh logout daripada sistem?', 'expected' => 'Sesi pengguna berjaya ditamatkan.'],
    ],
    'MODUL DASHBOARD' => [
        ['title' => 'Adakah dashboard berjaya dipaparkan selepas pengguna login?', 'expected' => 'Dashboard dipaparkan dengan lengkap.'],
        ['title' => 'Adakah jumlah rekod Fail Office dan Fail Personal dipaparkan dengan betul?', 'expected' => 'Jumlah Fail Office dan Fail Personal adalah sama dengan data sebenar dalam sistem'],
        ['title' => 'Adakah paparan statistik dashboard menunjukkan maklumat fail dan surat yang tepat?', 'expected' => 'Statistik dipaparkan berdasarkan rekod fail dan surat sebenar dalam sistem'],
        ['title' => 'Adakah menu navigasi sistem boleh digunakan?', 'expected' => 'Semua menu boleh diakses dan berfungsi dengan baik.'],
    ],
    'MODUL PENDAFTARAN FAIL' => [
        ['title' => 'Adakah pengguna boleh menambah rekod fail baharu?', 'expected' => 'Rekod fail berjaya disimpan ke dalam sistem.'],
        ['title' => 'Adakah sistem menyemak maklumat wajib sebelum penyimpanan dibuat?', 'expected' => 'Sistem meminta pengguna melengkapkan maklumat'],
        ['title' => 'Adakah maklumat fail yang dimasukkan disimpan dengan tepat?', 'expected' => 'Maklumat fail berjaya direkodkan dengan tepat.'],
        ['title' => 'Adakah sistem menjana nombor rujukan fail mengikut nombor mula yang dipilih pengguna?', 'expected' => 'Nombor rujukan fail dijana secara unik dan bersambung daripada nombor mula yang ditetapkan.'],
        ['title' => 'Adakah mesej berjaya dipaparkan selepas rekod disimpan?', 'expected' => 'Sistem memaparkan mesej berjaya selepas rekod disimpan.'],
    ],
    'MODUL PAPARAN SENARAI FAIL' => [
        ['title' => 'Adakah semua rekod fail dipaparkan dalam senarai?', 'expected' => 'Semua rekod fail dipaparkan.'],
        ['title' => 'Adakah maklumat fail yang dipaparkan adalah tepat?', 'expected' => 'Maklumat yang dipaparkan adalah tepat berdasarkan rekod sistem.'],
        ['title' => 'Adakah fungsi pagination berfungsi dengan baik?', 'expected' => 'Pengguna boleh melihat halaman lain'],
        ['title' => 'Adakah pengguna boleh melihat maklumat terperinci fail?', 'expected' => 'Maklumat lengkap fail berjaya dipaparkan.'],
    ],
    'MODUL KEMASKINI FAIL' => [
        ['title' => 'Adakah pengguna boleh mengubah maklumat fail?', 'expected' => 'Maklumat fail berjaya dikemaskini.'],
        ['title' => 'Adakah perubahan maklumat fail dipaparkan semula selepas dikemaskini?', 'expected' => 'Data fail terkini dipaparkan pada rekod yang sama tanpa mencipta rekod baharu.'],
        ['title' => 'Adakah sistem melakukan semakan sebelum kemaskini?', 'expected' => 'Sistem melakukan validasi dan meminta pengguna melengkapkan maklumat.'],
    ],
    'MODUL PADAM FAIL' => [
        ['title' => 'Adakah pengguna boleh memadam rekod fail?', 'expected' => 'Rekod berjaya dipadam'],
        ['title' => 'Adakah sistem memaparkan pengesahan sebelum proses padam dilakukan?', 'expected' => 'Popup pengesahan dipaparkan'],
        ['title' => 'Adakah rekod yang dipadam tidak lagi dipaparkan dalam senarai aktif?', 'expected' => 'Rekod dikeluarkan daripada senarai aktif dan hanya dipaparkan di bahagian Data Dipadam jika berkaitan.'],
    ],
    'MODUL CARIAN FAIL' => [
        ['title' => 'Adakah pengguna boleh mencari fail menggunakan kata kunci?', 'expected' => 'Sistem memaparkan rekod yang berkaitan.'],
        ['title' => 'Adakah carian berdasarkan nombor fail berfungsi?', 'expected' => 'Fail yang sepadan berjaya dipaparkan.'],
        ['title' => 'Adakah sistem memaparkan mesej jika tiada rekod dijumpai?', 'expected' => 'Sistem memaparkan mesej "Tiada rekod dijumpai".'],
    ],
    'MODUL PROFIL PENGGUNA' => [
        ['title' => 'Adakah pengguna boleh melihat maklumat profil sendiri?', 'expected' => 'Maklumat profil pengguna berjaya dipaparkan.'],
        ['title' => 'Adakah pengguna boleh mengemaskini maklumat profil yang dibenarkan sahaja?', 'expected' => 'Maklumat profil yang dibenarkan berjaya dikemaskini tanpa mengubah Bahagian dan Jawatan pengguna.'],
        ['title' => 'Adakah pengguna boleh menukar kata laluan?', 'expected' => 'Kata laluan berjaya ditukar.'],
        ['title' => 'Adakah pengguna boleh mendaftarkan tandatangan pengguna?', 'expected' => 'Tandatangan pengguna berjaya disimpan dalam sistem.'],
        ['title' => 'Adakah pengguna boleh melihat tandatangan yang telah didaftarkan?', 'expected' => 'Tandatangan dipaparkan dengan betul.'],
        ['title' => 'Adakah pengguna boleh mengubah tandatangan sedia ada?', 'expected' => 'Tandatangan baharu berjaya dikemaskini.'],
        ['title' => 'Adakah sistem meminta pengesahan emel sebelum perubahan tandatangan?', 'expected' => 'Sistem meminta pengesahan emel sebelum perubahan dibuat.'],
        ['title' => 'Adakah OTP dihantar ke emel pengguna untuk pengesahan?', 'expected' => 'OTP berjaya diterima melalui emel.'],
        ['title' => 'Adakah sistem menolak OTP yang salah atau tamat tempoh?', 'expected' => 'Sistem memaparkan mesej ralat OTP tidak sah.'],
    ],
    'MODUL KESELAMATAN SISTEM' => [
        ['title' => 'Adakah sistem hanya membenarkan pengguna yang telah login mengakses sistem?', 'expected' => 'Akses tanpa login disekat'],
        ['title' => 'Adakah halaman dalaman dilindungi daripada akses terus melalui URL?', 'expected' => 'Sistem mengarahkan pengguna ke halaman login.'],
        ['title' => 'Adakah sistem menamatkan sesi selepas logout?', 'expected' => 'Sesi pengguna ditamatkan.'],
        ['title' => 'Adakah fungsi OTP melindungi perubahan maklumat sensitif?', 'expected' => 'Pengesahan tambahan diperlukan.'],
    ],
    'MODUL PENGURUSAN SURAT' => [
        ['title' => 'Adakah pengguna boleh membuka modul surat?', 'expected' => 'Modul surat berjaya dipaparkan.'],
        ['title' => 'Adakah pengguna boleh memilih jenis surat dan mencipta surat baharu?', 'expected' => 'Jenis surat yang dipilih dibuka dan surat baharu berjaya dicipta.'],
        ['title' => 'Adakah pengguna boleh memasukkan maklumat surat seperti perkara, penerima, tarikh dan kandungan?', 'expected' => 'Maklumat surat berjaya disimpan dan dipaparkan semula dengan tepat.'],
        ['title' => 'Adakah editor surat membenarkan pengguna menaip dan mengubah kandungan surat?', 'expected' => 'Kandungan surat boleh diubah dengan baik.'],
        ['title' => 'Adakah format surat seperti font, saiz tulisan, jarak perenggan dan alignment berfungsi?', 'expected' => 'Format surat dipaparkan dengan betul.'],
        ['title' => 'Adakah pengguna boleh menyimpan surat sebagai draf?', 'expected' => 'Surat berjaya disimpan sebagai draf.'],
        ['title' => 'Adakah pengguna boleh mengemaskini surat yang telah disimpan?', 'expected' => 'Perubahan surat berjaya disimpan.'],
        ['title' => 'Adakah pengguna boleh menghantar surat kepada pihak yang terlibat untuk semakan, tandatangan atau kelulusan?', 'expected' => 'Surat berjaya dihantar kepada pengguna atau bahagian yang betul mengikut flow sistem.'],
        ['title' => 'Adakah status surat berubah mengikut tindakan dalam aliran kelulusan?', 'expected' => 'Status seperti Disediakan, Disahkan, Ditandatangani, Diterima, Lulus atau Ditolak dipaparkan dengan tepat mengikut tindakan.'],
        ['title' => 'Adakah surat boleh dijana dalam format PDF/cetakan?', 'expected' => 'Surat berjaya dijana dalam format yang ditetapkan.'],
        ['title' => 'Adakah tandatangan pengguna dimasukkan ke dalam surat dengan betul?', 'expected' => 'Tandatangan pengguna dipaparkan pada kedudukan yang betul.'],
        ['title' => 'Adakah sistem menghalang kehilangan data ketika surat belum disimpan?', 'expected' => 'Amaran dipaparkan kepada pengguna'],
    ],
];

// Create a mapping of question keys to unique IDs (1 to 53)
$question_id_map = [];
$global_qidx = 1;
foreach ($checklist_modules as $module => $questions) {
    foreach ($questions as $qidx => $q) {
        $qkey = $module . '_' . $qidx;
        $question_id_map[$qkey] = $global_qidx;
        $global_qidx++;
    }
}

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['submit_uat']) || isset($_POST['simpan_uat'])) {
        // Save user info
        $department = mysqli_real_escape_string($conn, $_POST['department'] ?? '');
        $position = mysqli_real_escape_string($conn, $_POST['position'] ?? '');
        
        $check_user_info = mysqli_prepare($conn, "SELECT id FROM uat_user_info WHERE user_id = ?");
        mysqli_stmt_bind_param($check_user_info, "i", $user_id);
        mysqli_stmt_execute($check_user_info);
        $existing_user_info = mysqli_stmt_get_result($check_user_info);
        
        if (mysqli_num_rows($existing_user_info) > 0) {
            $update_user_info = mysqli_prepare($conn, "UPDATE uat_user_info SET department = ?, position = ?, completed_at = CURRENT_TIMESTAMP WHERE user_id = ?");
            mysqli_stmt_bind_param($update_user_info, "ssi", $department, $position, $user_id);
            mysqli_stmt_execute($update_user_info);
        } else {
            $insert_user_info = mysqli_prepare($conn, "INSERT INTO uat_user_info (user_id, department, position) VALUES (?, ?, ?)");
            mysqli_stmt_bind_param($insert_user_info, "iss", $user_id, $department, $position);
            mysqli_stmt_execute($insert_user_info);
        }
        
        // Save responses
        if (isset($_POST['responses']) && is_array($_POST['responses'])) {
            foreach ($_POST['responses'] as $qkey => $response) {
                $status = mysqli_real_escape_string($conn, $response['status'] ?? 'belum_test');
                if (!in_array($status, ['pass', 'fail', 'tidak_berkenaan', 'belum_test'], true)) {
                    $status = 'belum_test';
                }
                $notes = mysqli_real_escape_string($conn, $response['notes'] ?? '');
                
                $qid = $question_id_map[$qkey] ?? 0;
                if ($qid <= 0) continue;
                
                $check_response = mysqli_prepare($conn, "SELECT id FROM uat_responses WHERE user_id = ? AND question_id = ?");
                mysqli_stmt_bind_param($check_response, "ii", $user_id, $qid);
                mysqli_stmt_execute($check_response);
                $existing = mysqli_stmt_get_result($check_response);
                
                if (mysqli_num_rows($existing) > 0) {
                    $row = mysqli_fetch_assoc($existing);
                    $update_response = mysqli_prepare($conn, "UPDATE uat_responses SET status = ?, notes = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?");
                    mysqli_stmt_bind_param($update_response, "ssi", $status, $notes, $row['id']);
                    mysqli_stmt_execute($update_response);
                } else {
                    $insert_response = mysqli_prepare($conn, "INSERT INTO uat_responses (user_id, question_id, status, notes) VALUES (?, ?, ?, ?)");
                    mysqli_stmt_bind_param($insert_response, "iiss", $user_id, $qid, $status, $notes);
                    mysqli_stmt_execute($insert_response);
                }
            }
        }
        
        $success_message = isset($_POST['simpan_uat']) ? 'Kemajuan UAT anda telah disimpan!' : 'UAT anda telah berjaya disimpan!';
    }
}

// Get user info
$user_info_stmt = mysqli_prepare($conn, "SELECT * FROM uat_user_info WHERE user_id = ?");
mysqli_stmt_bind_param($user_info_stmt, "i", $user_id);
mysqli_stmt_execute($user_info_stmt);
$user_info_result = mysqli_stmt_get_result($user_info_stmt);
$user_info = mysqli_fetch_assoc($user_info_result);

// Get user's existing responses keyed by question_id (1 to 53)
$user_responses = [];
$responses_stmt = mysqli_prepare($conn, "SELECT question_id, status, notes FROM uat_responses WHERE user_id = ?");
mysqli_stmt_bind_param($responses_stmt, "i", $user_id);
mysqli_stmt_execute($responses_stmt);
$responses_result = mysqli_stmt_get_result($responses_stmt);
while ($row = mysqli_fetch_assoc($responses_result)) {
    $user_responses[(int)$row['question_id']] = $row;
}

// Calculate module statistics from hardcoded modules
$module_stats = [];
foreach ($checklist_modules as $module => $questions) {
    $total = count($questions);
    $pass = 0;
    $fail = 0;
    $tidak_berkenaan = 0;
    $belum = 0;
    
    foreach ($questions as $qidx => $q) {
        $qkey = $module . '_' . $qidx;
        $qid = $question_id_map[$qkey];
        $response = $user_responses[$qid] ?? null;
        $status = $response['status'] ?? 'belum_test';
        if ($status === 'pass') {
            $pass++;
        } elseif ($status === 'fail') {
            $fail++;
        } elseif ($status === 'tidak_berkenaan') {
            $tidak_berkenaan++;
        } else {
            $belum++;
        }
    }
    
    $module_stats[$module] = [
        'total' => $total,
        'pass' => $pass,
        'fail' => $fail,
        'tidak_berkenaan' => $tidak_berkenaan,
        'belum' => $belum
    ];
}

// Get current user details for default values
$current_user_stmt = mysqli_prepare($conn, "SELECT fullname, department, position FROM users WHERE id = ?");
mysqli_stmt_bind_param($current_user_stmt, "i", $user_id);
mysqli_stmt_execute($current_user_stmt);
$current_user = mysqli_fetch_assoc(mysqli_stmt_get_result($current_user_stmt));

// Get all tester responses for superadmin view (including superadmins)
$tester_responses = [];
$tester_detail_reports = [];
$total_questions = 0;
foreach ($checklist_modules as $module => $questions) {
    $total_questions += count($questions);
}

if ($role === 'superadmin') {
    $testers_stmt = mysqli_prepare($conn, "
        SELECT 
            ui.user_id,
            u.fullname,
            ui.department,
            ui.position,
            ui.completed_at,
            SUM(CASE WHEN r.status = 'pass' THEN 1 ELSE 0 END) as pass_count,
            SUM(CASE WHEN r.status = 'fail' THEN 1 ELSE 0 END) as fail_count,
            SUM(CASE WHEN r.status = 'tidak_berkenaan' THEN 1 ELSE 0 END) as tidak_berkenaan_count,
            SUM(CASE WHEN r.status = 'belum_test' THEN 1 ELSE 0 END) as belum_count
        FROM uat_user_info ui
        JOIN users u ON ui.user_id = u.id
        LEFT JOIN uat_responses r ON ui.user_id = r.user_id
        GROUP BY ui.user_id, u.fullname, ui.department, ui.position, ui.completed_at
        ORDER BY ui.completed_at DESC
    ");
    mysqli_stmt_execute($testers_stmt);
    $testers_result = mysqli_stmt_get_result($testers_stmt);
    while ($row = mysqli_fetch_assoc($testers_result)) {
        $tester_responses[] = $row;
    }

    $tester_response_rows = [];
    $detail_stmt = mysqli_prepare($conn, "SELECT question_id, status, notes FROM uat_responses WHERE user_id = ?");

    foreach ($tester_responses as $tester) {
        $detail_user_id = (int) $tester['user_id'];
        mysqli_stmt_bind_param($detail_stmt, "i", $detail_user_id);
        mysqli_stmt_execute($detail_stmt);
        $detail_result = mysqli_stmt_get_result($detail_stmt);
        $tester_response_rows[$detail_user_id] = [];

        while ($detail_row = mysqli_fetch_assoc($detail_result)) {
            $tester_response_rows[$detail_user_id][(int) $detail_row['question_id']] = $detail_row;
        }
    }

    foreach ($tester_responses as $tester) {
        $detail_user_id = (int) $tester['user_id'];
        $pass_count = (int) ($tester['pass_count'] ?? 0);
        $fail_count = (int) ($tester['fail_count'] ?? 0);
        $tidak_berkenaan_count = (int) ($tester['tidak_berkenaan_count'] ?? 0);
        $belum_count = (int) ($tester['belum_count'] ?? 0);

        if ($belum_count > 0) {
            $overall_status = 'belum_lengkap';
            $overall_status_label = 'Belum Lengkap';
        } elseif ($fail_count > 0) {
            $overall_status = 'gagal';
            $overall_status_label = 'Gagal';
        } elseif ($tidak_berkenaan_count > 0) {
            $overall_status = 'lulus_dengan_isu';
            $overall_status_label = 'Lulus dengan Isu';
        } else {
            $overall_status = 'lulus';
            $overall_status_label = 'Lulus';
        }

        $modules = [];
        foreach ($checklist_modules as $module => $questions) {
            $module_questions = [];

            foreach ($questions as $qidx => $question) {
                $qkey = $module . '_' . $qidx;
                $qid = $question_id_map[$qkey] ?? 0;
                $response = $tester_response_rows[$detail_user_id][$qid] ?? null;
                $response_status = $response['status'] ?? 'belum_test';

                $module_questions[] = [
                    'number' => $qid,
                    'title' => $question['title'],
                    'expected' => $question['expected'],
                    'status' => $response_status,
                    'status_label' => [
                        'pass' => 'PASS',
                        'fail' => 'FAIL',
                        'tidak_berkenaan' => 'N/A',
                        'belum_test' => 'BELUM LENGKAP',
                    ][$response_status] ?? 'BELUM LENGKAP',
                    'notes' => trim((string) ($response['notes'] ?? '')),
                ];
            }

            $modules[] = [
                'name' => $module,
                'questions' => $module_questions,
            ];
        }

        $tester_detail_reports[$detail_user_id] = [
            'name' => $tester['fullname'] ?? '-',
            'department' => $tester['department'] ?? '-',
            'position' => $tester['position'] ?? '-',
            'submitted_at' => !empty($tester['completed_at']) ? date('d/m/Y H:i', strtotime($tester['completed_at'])) : '-',
            'overall_status' => $overall_status,
            'overall_status_label' => $overall_status_label,
            'modules' => $modules,
        ];
    }
}
?>
<!DOCTYPE html>
<html lang="ms">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>UAT | MAINPP WORKSPACE</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <?php renderAppShellAssets(); ?>
    <style>
        :root {
            --primary: #0077b6; --primary-dark: #005f99; --secondary: #00b4d8;
            --deep-blue: #023e5f;
            --superadmin: #6c5ce7; --superadmin-dark: #5b4cdb;
            --bg-light: rgba(245, 250, 253, 0.72); --bg-card: rgba(255, 255, 255, 0.68);
            --text-main: #163447; --text-muted: #2d5367; --border: rgba(255, 255, 255, 0.48);
            --sidebar-width: 280px; --success: #1a9955; --danger: #dc3545; --warning: #ffc107;
            --glass: rgba(255, 255, 255, 0.58); --glass-strong: rgba(255, 255, 255, 0.76);
            --shadow-soft: 0 24px 70px rgba(2, 62, 95, 0.16);
        }
        * { box-sizing: border-box; margin: 0; padding: 0; font-family: 'Segoe UI', 'Inter', Tahoma, Geneva, Verdana, sans-serif; }
        body {
            min-height: 100vh;
            display: flex;
            color: var(--text-main);
            background:
                linear-gradient(135deg, rgba(245, 250, 253, 0.90), rgba(222, 246, 252, 0.76)),
                url('assets/auth-bg.png') center/cover no-repeat fixed;
            overflow-x: hidden;
        }

        body::before,
        body::after {
            content: "";
            position: fixed;
            pointer-events: none;
            z-index: 0;
        }

        body::before {
            width: 42vw;
            height: 42vw;
            right: -16vw;
            top: -18vw;
            border-radius: 50%;
            background: radial-gradient(circle, rgba(0, 180, 216, 0.24), transparent 62%);
            filter: blur(4px);
        }

        body::after {
            width: 48vw;
            height: 48vw;
            left: 120px;
            bottom: -30vw;
            border-radius: 46px;
            background:
                linear-gradient(155deg, rgba(255, 255, 255, 0.20), rgba(255, 255, 255, 0.03)),
                repeating-linear-gradient(0deg, transparent 0 34px, rgba(0, 119, 182, 0.08) 35px 36px);
            border: 1px solid rgba(255, 255, 255, 0.32);
            transform: rotate(-9deg);
        }

        .sidebar {
            width: var(--sidebar-width);
            background:
                linear-gradient(180deg, rgba(2, 62, 95, 0.94), rgba(0, 119, 182, 0.84)),
                radial-gradient(circle at 22% 8%, rgba(0, 180, 216, 0.28), transparent 32%);
            border-right: 1px solid rgba(255, 255, 255, 0.22);
            position: fixed;
            height: 100vh;
            padding: 20px 0;
            overflow-y: auto;
            overflow-x: hidden;
            z-index: 1000;
            box-shadow: 18px 0 44px rgba(2, 38, 60, 0.20);
            transition: width 0.3s ease;
            display: flex;
            flex-direction: column;
            backdrop-filter: blur(18px) saturate(1.25);
            -webkit-backdrop-filter: blur(18px) saturate(1.25);
        }
        .sidebar.collapsed { width: 70px; }
        .sidebar-header {
            text-align: center;
            padding: 0 20px 20px;
            border-bottom: 1px solid rgba(255, 255, 255, 0.16);
            margin-bottom: 20px;
            white-space: nowrap;
            overflow: hidden;
            background: transparent !important;
        }
        .sidebar.collapsed .sidebar-header h3, .sidebar.collapsed .sidebar-header p, .sidebar.collapsed .menu-text { display: none; }
        .sidebar-header img { width: 54px; margin-bottom: 10px; transition: margin 0.3s ease; filter: drop-shadow(0 10px 18px rgba(0,0,0,0.25)); }
        .sidebar.collapsed .sidebar-header img { margin: 0 auto 10px; }
        .sidebar-header h3 { font-size: 1rem; font-weight: 800; color: #fff; margin-bottom: 4px; letter-spacing: 0.01em; }
        .sidebar-header p { font-size: 0.75rem; color: rgba(255,255,255,0.72); }
        .role-badge { display: inline-block; padding: 3px 10px; border-radius: 12px; font-size: 0.7rem; font-weight: 600; margin-left: 8px; }
        .role-badge.user { background: #e6f4ff; color: var(--primary); }
        .role-badge.admin { background: #d4edda; color: var(--success); }
        .role-badge.superadmin { background: linear-gradient(90deg, var(--superadmin), var(--superadmin-dark)); color: #fff; }
        .sidebar-menu { list-style: none; padding: 0 10px; display: flex; flex-direction: column; flex: 1; }
        .sidebar-section { padding: 10px 18px 6px; color: rgba(255,255,255,0.58); font-size: 0.72rem; font-weight: 800; text-transform: uppercase; letter-spacing: 0.06em; }
        .sidebar-spacer { margin-top: auto; }
        .sidebar-menu li a { display: flex; align-items: center; gap: 12px; padding: 12px 18px; color: rgba(255,255,255,0.88); text-decoration: none; border-radius: 10px; margin-bottom: 5px; transition: all 0.2s ease; font-size: 0.9rem; white-space: nowrap; }
        .sidebar-menu li a:hover, .sidebar-menu li a.active { background: rgba(255,255,255,0.18); color: #fff; box-shadow: inset 0 0 0 1px rgba(255,255,255,0.18); }
        .sidebar-menu li a i { width: 20px; text-align: center; font-size: 1.1rem; }
        .sidebar-menu .dropdown { position: relative; }
        .sidebar-menu .dropdown-toggle { display: flex; align-items: center; justify-content: space-between; width: 100%; padding: 12px 18px; color: rgba(255,255,255,0.88); background: none; border: none; border-radius: 10px; cursor: pointer; font-size: 0.9rem; text-align: left; transition: all 0.2s ease; }
        .sidebar-menu .dropdown-toggle:hover, .sidebar-menu .dropdown-toggle.active { background: rgba(255,255,255,0.18); color: #fff; box-shadow: inset 0 0 0 1px rgba(255,255,255,0.18); }
        .sidebar-menu .dropdown-toggle i.chevron { transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1); font-size: 0.8rem; }
        .sidebar-menu .dropdown.active .dropdown-toggle i.chevron { transform: rotate(180deg); }
        .sidebar-menu .dropdown-menu { display: flex; flex-direction: column; padding-left: 42px; margin-top: 4px; max-height: 0; opacity: 0; overflow: hidden; transition: max-height 0.3s cubic-bezier(0.4, 0, 0.2, 1), opacity 0.25s ease; }
        .sidebar-menu .dropdown.active .dropdown-menu { max-height: 260px; opacity: 1; }
        .sidebar-menu .dropdown-menu a { padding: 8px 18px; color: rgba(255,255,255,0.78); text-decoration: none; border-radius: 8px; font-size: 0.85rem; display: flex; align-items: center; gap: 8px; opacity: 0; transform: translateY(-5px); transition: opacity 0.2s ease, transform 0.2s ease; }
        .sidebar-menu .dropdown.active .dropdown-menu a { opacity: 1; transform: translateY(0); }
        .sidebar-menu .dropdown-menu a:hover, .sidebar-menu .dropdown-menu a.active { background: rgba(255,255,255,0.16); color: #fff; }
        .sidebar.collapsed + .main { margin-left: 70px; }
        .sidebar-toggle { position: absolute; right: 10px; top: 20px; width: 28px; height: 28px; background: rgba(255,255,255,0.20); color: #fff; border: 1px solid rgba(255,255,255,0.28); border-radius: 50%; cursor: pointer; display: flex; align-items: center; justify-content: center; font-size: 0.7rem; box-shadow: 0 10px 22px rgba(0,0,0,0.14); transition: transform 0.3s ease; z-index: 1001; }
        .sidebar.collapsed .sidebar-toggle { transform: rotate(180deg); }
        .main { position: relative; z-index: 1; margin-left: var(--sidebar-width); flex: 1; padding: 28px 32px; transition: margin-left 0.3s ease; }
        .top-bar {
            background: var(--glass);
            padding: 18px 24px;
            border-radius: 18px;
            box-shadow: var(--shadow-soft);
            margin-bottom: 18px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            border: 1px solid var(--border);
            backdrop-filter: blur(22px) saturate(1.25);
            -webkit-backdrop-filter: blur(22px) saturate(1.25);
        }
        .top-bar h2 { color: var(--primary-dark); font-size: 1.35rem; font-weight: 850; display: flex; align-items: center; gap: 10px; }
        .top-bar h2 i { color: var(--secondary); }
        .uat-container {
            background: var(--glass);
            border-radius: 22px;
            padding: 30px;
            box-shadow: var(--shadow-soft);
            border: 1px solid var(--border);
            backdrop-filter: blur(24px) saturate(1.25);
        }
        .uat-layout {
            display: grid;
            grid-template-columns: 1fr 380px;
            gap: 25px;
        }
        .uat-layout.full-width {
            grid-template-columns: 1fr;
        }
        .uat-form-column {
            min-width: 0;
        }
        .uat-responses-column {
            background: rgba(255, 255, 255, 0.5);
            border-radius: 18px;
            padding: 20px;
            border: 1px solid rgba(0, 119, 182, 0.15);
            height: fit-content;
            position: sticky;
            top: 20px;
        }
        .uat-section {
            margin-bottom: 30px;
        }
        .glass-card {
            background: rgba(255, 255, 255, 0.6);
            border: 1px solid rgba(255, 255, 255, 0.4);
            border-radius: 18px;
            padding: 25px;
            box-shadow: 0 8px 24px rgba(2, 62, 95, 0.12);
            backdrop-filter: blur(20px) saturate(1.2);
            margin-bottom: 25px;
        }
        .uat-section-title {
            color: var(--primary-dark);
            font-size: 1.25rem;
            font-weight: 800;
            margin-bottom: 20px;
            padding-bottom: 10px;
            border-bottom: 2px solid rgba(0, 119, 182, 0.2);
            display: flex;
            align-items: center;
            gap: 10px;
        }
        .uat-section-title i { color: var(--secondary); }
        .form-group { margin-bottom: 20px; }
        .form-label {
            display: block;
            color: var(--text-main);
            font-weight: 600;
            margin-bottom: 8px;
            font-size: 0.95rem;
        }
        .form-control {
            width: 100%;
            padding: 12px 16px;
            border: 1px solid rgba(255, 255, 255, 0.4);
            border-radius: 10px;
            font-size: 0.95rem;
            background: rgba(255, 255, 255, 0.45);
            backdrop-filter: blur(10px);
            -webkit-backdrop-filter: blur(10px);
            transition: all 0.2s ease;
            color: #1a1a2e;
        }
        .form-control:focus {
            outline: none;
            border-color: var(--primary);
            box-shadow: 0 0 0 3px rgba(0, 119, 182, 0.15);
            background: rgba(255, 255, 255, 0.6);
        }
        textarea.form-control { resize: vertical; min-height: 100px; }
        .question-card {
            background: rgba(255, 255, 255, 0.7);
            border: 1px solid rgba(0, 119, 182, 0.15);
            border-radius: 14px;
            padding: 20px;
            margin-bottom: 15px;
            transition: all 0.2s ease;
        }
        .question-card:hover {
            box-shadow: 0 8px 20px rgba(2, 62, 95, 0.1);
        }
        .module-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
            padding-bottom: 15px;
            border-bottom: 2px solid rgba(0, 119, 182, 0.2);
        }
        .module-title {
            color: var(--primary-dark);
            font-size: 1.25rem;
            font-weight: 600;
            display: flex;
            align-items: center;
        }
        .module-stats {
            display: flex;
            gap: 15px;
            align-items: center;
        }
        .stat-item {
            display: flex;
            align-items: center;
            gap: 6px;
            font-size: 0.9rem;
            font-weight: 600;
        }
        .stat-badge {
            display: inline-block;
            padding: 4px 10px;
            border-radius: 12px;
            font-size: 0.85rem;
            font-weight: 700;
        }
        .stat-badge.total {
            background: rgba(0, 119, 182, 0.15);
            color: var(--primary);
        }
        .stat-badge.pass {
            background: rgba(26, 153, 85, 0.15);
            color: var(--success);
        }
        .stat-badge.isu {
            background: rgba(220, 53, 69, 0.15);
            color: var(--danger);
        }
        .stat-badge.belum {
            background: rgba(108, 117, 125, 0.15);
            color: #6c757d;
        }
        .module-actions {
            display: flex;
            gap: 10px;
        }
        .btn-module {
            padding: 8px 16px;
            border: none;
            border-radius: 8px;
            font-size: 0.85rem;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.2s ease;
        }
        .btn-pass {
            background: rgba(26, 153, 85, 0.15);
            color: var(--success);
            border: 1px solid rgba(26, 153, 85, 0.3);
        }
        .btn-pass:hover {
            background: rgba(26, 153, 85, 0.25);
        }
        .btn-reset {
            background: rgba(220, 53, 69, 0.15);
            color: var(--danger);
            border: 1px solid rgba(220, 53, 69, 0.3);
        }
        .btn-reset:hover {
            background: rgba(220, 53, 69, 0.25);
        }
        .test-item-title {
            color: var(--primary-dark);
            font-size: 1.1rem;
            font-weight: 600;
            margin-bottom: 12px;
            display: flex;
            align-items: center;
            gap: 10px;
        }
        .status-badge-item {
            display: inline-block;
            padding: 4px 12px;
            border-radius: 12px;
            font-size: 0.8rem;
            font-weight: 600;
        }
        .status-badge-item.belum-test {
            background: rgba(108, 117, 125, 0.15);
            color: #6c757d;
        }
        .status-badge-item.pass {
            background: rgba(26, 153, 85, 0.15);
            color: var(--success);
        }
        .status-badge-item.isu {
            background: rgba(220, 53, 69, 0.15);
            color: var(--danger);
        }
        .status-badge-item.fail {
            background: rgba(220, 53, 69, 0.15);
            color: var(--danger);
        }
        .status-badge-item.tidak_berkenaan {
            background: rgba(108, 117, 125, 0.15);
            color: #6c757d;
        }
        .test-description {
            color: var(--text-main);
            font-size: 0.95rem;
            margin-bottom: 10px;
            line-height: 1.5;
        }
        .expected-result {
            color: var(--text-muted);
            font-size: 0.9rem;
            margin-bottom: 15px;
            font-style: italic;
            background: rgba(26, 153, 85, 0.08);
            padding: 10px 14px;
            border-radius: 8px;
            border-left: 3px solid var(--success);
        }
        .expected-result::before {
            content: "✓ ";
            font-weight: 600;
            color: var(--success);
            font-style: normal;
        }
        .test-result-section {
            display: flex;
            gap: 20px;
            align-items: flex-start;
            margin-top: 15px;
            padding-top: 15px;
            border-top: 1px solid rgba(0, 119, 182, 0.15);
        }
        .result-dropdown {
            flex: 0 0 auto;
        }
        .result-dropdown select {
            padding: 8px 12px;
            border: 1px solid rgba(255, 255, 255, 0.4);
            border-radius: 8px;
            font-size: 0.9rem;
            background: rgba(255, 255, 255, 0.45);
            backdrop-filter: blur(10px);
            -webkit-backdrop-filter: blur(10px);
            cursor: pointer;
            color: #1a1a2e;
        }
        .result-dropdown select:focus {
            outline: none;
            border-color: var(--primary);
            box-shadow: 0 0 0 3px rgba(0, 119, 182, 0.15);
            background: rgba(255, 255, 255, 0.6);
        }
        .result-notes {
            flex: 1;
        }
        .result-notes textarea {
            width: 100%;
            padding: 10px 14px;
            border: 1px solid rgba(255, 255, 255, 0.4);
            border-radius: 8px;
            font-size: 0.9rem;
            background: rgba(255, 255, 255, 0.45);
            backdrop-filter: blur(10px);
            -webkit-backdrop-filter: blur(10px);
            resize: vertical;
            min-height: 80px;
            color: #1a1a2e;
        }
        .result-notes textarea:focus {
            outline: none;
            border-color: var(--primary);
            box-shadow: 0 0 0 3px rgba(0, 119, 182, 0.15);
            background: rgba(255, 255, 255, 0.6);
        }
        .responses-header {
            margin-bottom: 20px;
        }
        .responses-header h3 {
            color: var(--primary-dark);
            font-size: 1.1rem;
            font-weight: 800;
            margin-bottom: 15px;
            display: flex;
            align-items: center;
            gap: 8px;
        }
        .search-filter-bar {
            display: flex;
            flex-wrap: wrap;
            gap: 10px;
            margin-bottom: 15px;
            width: 100%;
        }
        .search-filter-bar input,
        .search-filter-bar select {
            flex: 1 1 0;
            min-width: 0;
            width: 100%;
            padding: 10px 14px;
            border: 1px solid rgba(0, 119, 182, 0.3);
            border-radius: 8px;
            font-size: 0.9rem;
            background: rgba(255, 255, 255, 0.9);
        }
        .search-filter-bar input:focus,
        .search-filter-bar select:focus {
            outline: none;
            border-color: var(--primary);
            box-shadow: 0 0 0 3px rgba(0, 119, 182, 0.1);
        }
        .tester-summary {
            display: grid;
            grid-template-columns: repeat(4, minmax(0, 1fr));
            gap: 10px;
            margin-bottom: 14px;
        }
        .tester-summary-item {
            background: rgba(255, 255, 255, 0.62);
            border: 1px solid rgba(0, 119, 182, 0.16);
            border-radius: 12px;
            padding: 12px;
            backdrop-filter: blur(12px);
            -webkit-backdrop-filter: blur(12px);
            text-align: center;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
        }
        .tester-summary-item small {
            display: block;
            color: var(--text-muted);
            font-size: 0.72rem;
            margin-bottom: 6px;
            font-weight: 600;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }
        .tester-summary-item strong {
            color: var(--primary-dark);
            font-size: 1.4rem;
            font-weight: 700;
            line-height: 1;
        }
        .tester-list-shell {
            max-height: 600px;
            overflow-y: auto;
            padding-right: 6px;
            display: flex;
            flex-direction: column;
            gap: 12px;
        }
        .tester-list-shell::-webkit-scrollbar {
            width: 8px;
        }
        .tester-list-shell::-webkit-scrollbar-thumb {
            background: rgba(0, 119, 182, 0.24);
            border-radius: 999px;
        }
        .tester-card {
            background: rgba(255, 255, 255, 0.88);
            border: 1px solid rgba(0, 119, 182, 0.18);
            border-radius: 14px;
            padding: 20px;
            transition: all 0.2s ease;
            box-shadow: 0 8px 22px rgba(2, 62, 95, 0.08);
        }
        .tester-card:hover {
            transform: translateY(-1px);
            box-shadow: 0 12px 26px rgba(2, 62, 95, 0.12);
            background: rgba(255, 255, 255, 0.94);
        }
        .tester-card-top {
            display: flex;
            align-items: flex-start;
            justify-content: space-between;
            gap: 14px;
            margin-bottom: 14px;
        }
        .tester-name {
            color: #0f172a;
            font-weight: 500;
            font-size: 1.05rem;
            line-height: 1.35;
        }
        .tester-time {
            color: #667085;
            font-size: 0.92rem;
            line-height: 1.4;
            white-space: nowrap;
        }
        .tester-status {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            padding: 7px 12px;
            border-radius: 8px;
            font-size: 0.82rem;
            font-weight: 500;
            white-space: nowrap;
            box-shadow: 0 3px 8px rgba(15, 23, 42, 0.08);
        }
        .tester-status.belum_lengkap {
            background: rgba(108, 117, 125, 0.15);
            color: #6c757d;
        }
        .tester-status.lulus {
            background: rgba(26, 153, 85, 0.15);
            color: var(--success);
        }
        .tester-status.lulus_dengan_isu {
            background: rgba(255, 193, 7, 0.15);
            color: #e6a800;
        }
        .tester-status.gagal {
            background: rgba(220, 53, 69, 0.15);
            color: var(--danger);
        }
        .tester-stat-row {
            display: grid;
            grid-template-columns: repeat(4, minmax(0, 1fr));
            gap: 10px;
            margin-bottom: 14px;
        }
        .tester-stat {
            border-radius: 8px;
            padding: 13px 12px;
            font-size: 0.95rem;
            font-weight: 500;
            text-align: left;
        }
        .tester-stat.pass {
            background: rgba(16, 185, 129, 0.12);
            color: #047857;
        }
        .tester-stat.fail {
            background: rgba(244, 63, 94, 0.10);
            color: #be123c;
        }
        .tester-stat.block {
            background: rgba(245, 158, 11, 0.10);
            color: #b45309;
        }
        .btn-view-detail {
            width: 100%;
            min-height: 40px;
            padding: 9px 12px;
            background: rgba(255, 255, 255, 0.92);
            color: #111827;
            border: 1px solid rgba(148, 163, 184, 0.52);
            border-radius: 8px;
            font-size: 0.9rem;
            font-weight: 700;
            cursor: pointer;
            transition: all 0.2s ease;
        }
        .btn-view-detail:hover {
            background: rgba(232, 244, 255, 0.92);
            border-color: rgba(0, 119, 182, 0.34);
            transform: translateY(-1px);
        }
        .tester-detail-panel {
            display: none;
            max-height: 340px;
            overflow-y: auto;
            margin-top: 14px;
            padding: 14px;
            border-radius: 10px;
            background: rgba(241, 245, 249, 0.72);
        }
        .tester-card.detail-open .tester-detail-panel {
            display: block;
        }
        .tester-detail-panel::-webkit-scrollbar {
            width: 8px;
        }
        .tester-detail-panel::-webkit-scrollbar-thumb {
            background: rgba(100, 116, 139, 0.48);
            border-radius: 999px;
        }
        .uat-report-module {
            margin-bottom: 16px;
        }
        .uat-report-module-title {
            color: #0f172a;
            font-size: 0.9rem;
            font-weight: 800;
            margin: 8px 0 10px;
        }
        .uat-answer-card {
            background: rgba(255, 255, 255, 0.94);
            border-radius: 8px;
            padding: 16px;
            margin-bottom: 12px;
            box-shadow: 0 6px 14px rgba(15, 23, 42, 0.06);
        }
        .uat-answer-card h4 {
            color: #0f172a;
            font-size: 0.96rem;
            margin-bottom: 8px;
            line-height: 1.35;
        }
        .uat-answer-text,
        .uat-answer-expected,
        .uat-answer-notes {
            color: #667085;
            line-height: 1.45;
            margin-bottom: 10px;
        }
        .uat-answer-label {
            display: block;
            color: #475569;
            font-size: 0.72rem;
            font-weight: 800;
            text-transform: uppercase;
            margin-bottom: 4px;
        }
        .uat-detail-status-badge {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            gap: 6px;
            width: fit-content;
            padding: 7px 12px;
            border-radius: 8px;
            font-size: 0.82rem;
            font-weight: 800;
        }
        .uat-detail-status-badge.pass,
        .uat-detail-status-badge.lulus {
            background: rgba(26, 153, 85, 0.14);
            color: var(--success);
        }
        .uat-detail-status-badge.fail,
        .uat-detail-status-badge.gagal {
            background: rgba(220, 53, 69, 0.13);
            color: var(--danger);
        }
        .uat-detail-status-badge.tidak_berkenaan,
        .uat-detail-status-badge.lulus_dengan_isu {
            background: rgba(108, 117, 125, 0.14);
            color: #5f6871;
        }
        .uat-detail-status-badge.belum_test,
        .uat-detail-status-badge.belum_lengkap {
            background: rgba(0, 119, 182, 0.12);
            color: var(--primary-dark);
        }
        .uat-detail-empty {
            color: var(--text-muted);
            font-style: italic;
        }
        .global-actions {
            display: flex;
            gap: 10px;
            margin-bottom: 20px;
            padding-bottom: 15px;
            border-bottom: 1px solid rgba(0, 119, 182, 0.15);
        }
        .btn-global {
            flex: 1;
            padding: 10px 16px;
            border: none;
            border-radius: 8px;
            font-size: 0.9rem;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.2s ease;
        }
        .btn-all-pass {
            background: rgba(26, 153, 85, 0.15);
            color: var(--success);
            border: 1px solid rgba(26, 153, 85, 0.3);
        }
        .btn-all-pass:hover {
            background: rgba(26, 153, 85, 0.25);
        }
        .btn-reset-all {
            background: rgba(220, 53, 69, 0.15);
            color: var(--danger);
            border: 1px solid rgba(220, 53, 69, 0.3);
        }
        .btn-reset-all:hover {
            background: rgba(220, 53, 69, 0.25);
        }
        @media (max-width: 1200px) {
            .uat-layout {
                grid-template-columns: 1fr;
            }
            .uat-responses-column {
                position: static;
            }
            .tester-list-shell {
                max-height: 520px;
            }
        }
        @media (max-width: 992px) {
            .sidebar { width: 70px; }
            .sidebar .sidebar-header h3, .sidebar .sidebar-header p, .sidebar .menu-text, .sidebar .role-badge, .sidebar .dropdown-menu, .sidebar .sidebar-section { display: none; }
            .main { margin-left: 70px; }
            .sidebar-toggle { display: none; }
            .tester-summary {
                grid-template-columns: repeat(2, minmax(0, 1fr));
            }
            .search-filter-bar {
                flex-direction: column;
            }
            .tester-card {
                padding: 16px;
            }
            .tester-card-top {
                flex-direction: column;
            }
            .tester-stat-row {
                grid-template-columns: 1fr;
            }
            .tester-detail-panel {
                max-height: 320px;
            }
        }
        .sidebar {
            overflow: hidden;
        }
        .sidebar-header {
            flex: 0 0 auto;
        }
        .sidebar-menu {
            flex: 1 1 auto;
            min-height: 0;
            overflow-y: auto;
            overflow-x: hidden;
            padding-bottom: 0;
            overscroll-behavior: contain;
            scrollbar-width: thin;
            scrollbar-color: #8f8f8f transparent;
        }
        .sidebar-menu::-webkit-scrollbar {
            width: 10px;
        }
        .sidebar-menu::-webkit-scrollbar-track {
            background: transparent;
        }
        .sidebar-menu::-webkit-scrollbar-thumb {
            background: #8f8f8f;
            border-radius: 999px;
            border: 2px solid transparent;
            background-clip: content-box;
        }
        .sidebar-menu::-webkit-scrollbar-button {
            display: none;
            width: 0;
            height: 0;
        }
        .main .btn,
        .main .tab-btn,
        .main .action-btn,
        .main .btn-action,
        .main .filter-btn,
        .main .button,
        .main .generate-closing-btn,
        .main .attachment-remove-btn,
        .main .page-add-btn,
        .main .page-delete-btn,
        .main .delete-current-page-btn,
        .main .form-actions a,
        .main .form-actions button,
        .main .filter-actions a,
        .main .filter-actions button,
        .main .document-command a,
        .main .document-command button,
        .main table a[class*="btn"],
        .main table button[class*="btn"],
        .main input[type="submit"],
        .main input[type="button"] {
            transition: transform 0.2s ease, filter 0.2s ease, box-shadow 0.2s ease, background-color 0.2s ease, border-color 0.2s ease !important;
            will-change: transform;
        }
        .main .btn:hover:not(:disabled),
        .main .tab-btn:hover:not(:disabled),
        .main .action-btn:hover:not(:disabled),
        .main .btn-action:hover:not(:disabled),
        .main .filter-btn:hover:not(:disabled),
        .main .button:hover:not(:disabled),
        .main .generate-closing-btn:hover:not(:disabled),
        .main .attachment-remove-btn:hover:not(:disabled),
        .main .page-add-btn:hover:not(:disabled),
        .main .page-delete-btn:hover:not(:disabled),
        .main .delete-current-page-btn:hover:not(:disabled),
        .main .form-actions a:hover,
        .main .form-actions button:hover:not(:disabled),
        .main .filter-actions a:hover,
        .main .filter-actions button:hover:not(:disabled),
        .main .document-command a:hover,
        .main .document-command button:hover:not(:disabled),
        .main table a[class*="btn"]:hover,
        .main table button[class*="btn"]:hover:not(:disabled),
        .main input[type="submit"]:hover:not(:disabled),
        .main input[type="button"]:hover:not(:disabled) {
            transform: translateY(-1px);
            filter: brightness(1.04);
            box-shadow: 0 14px 28px rgba(0, 119, 182, 0.18);
        }
        .main .btn:active:not(:disabled),
        .main .tab-btn:active:not(:disabled),
        .main .action-btn:active:not(:disabled),
        .main .btn-action:active:not(:disabled),
        .main .filter-btn:active:not(:disabled),
        .main .button:active:not(:disabled),
        .main .generate-closing-btn:active:not(:disabled),
        .main .attachment-remove-btn:active:not(:disabled),
        .main .page-add-btn:active:not(:disabled),
        .main .page-delete-btn:active:not(:disabled),
        .main .delete-current-page-btn:active:not(:disabled),
        .main .form-actions a:active,
        .main .form-actions button:active:not(:disabled),
        .main .filter-actions a:active,
        .main .filter-actions button:active:not(:disabled),
        .main .document-command a:active,
        .main .document-command button:active:not(:disabled),
        .main table a[class*="btn"]:active,
        .main table button[class*="btn"]:active:not(:disabled),
        .main input[type="submit"]:active:not(:disabled),
        .main input[type="button"]:active:not(:disabled) {
            transform: translateY(0);
            filter: brightness(0.98);
        }
        .btn-submit {
            background: linear-gradient(90deg, var(--primary), var(--secondary));
            color: #fff;
            border: none;
            padding: 12px 20px;
            border-radius: 10px;
            font-size: 0.92rem;
            font-weight: 700;
            cursor: pointer;
            transition: all 0.2s ease;
            box-shadow: 0 10px 20px rgba(0, 119, 182, 0.2);
            display: inline-flex;
            align-items: center;
            gap: 8px;
        }
        .btn-submit:hover {
            transform: translateY(-2px);
            box-shadow: 0 14px 28px rgba(0, 119, 182, 0.3);
        }
        .btn-simpan {
            background: rgba(255,255,255,0.76);
            color: var(--primary-dark);
            border: 1px solid rgba(0,119,182,0.18);
            padding: 12px 20px;
            border-radius: 10px;
            font-size: 0.92rem;
            font-weight: 700;
            cursor: pointer;
            transition: all 0.2s ease;
            box-shadow: 0 10px 20px rgba(0,119,182,0.08);
            display: inline-flex;
            align-items: center;
            gap: 8px;
        }
        .btn-simpan:hover {
            background: rgba(255,255,255,0.95);
            box-shadow: 0 14px 28px rgba(0,119,182,0.15);
        }
        .alert {
            padding: 16px 20px;
            border-radius: 12px;
            margin-bottom: 20px;
            font-weight: 600;
        }
        .alert-success {
            background: rgba(26, 153, 85, 0.15);
            color: var(--success);
            border: 1px solid rgba(26, 153, 85, 0.3);
        }
        .alert-error {
            background: rgba(220, 53, 69, 0.15);
            color: var(--danger);
            border: 1px solid rgba(220, 53, 69, 0.3);
        }
        .uat-users-table {
            width: 100%;
            border-collapse: collapse;
            background: rgba(255, 255, 255, 0.8);
            border-radius: 14px;
            overflow: hidden;
            margin-top: 20px;
        }
        .uat-users-table th {
            background: rgba(0, 119, 182, 0.1);
            color: var(--primary-dark);
            font-weight: 800;
            padding: 14px 16px;
            text-align: left;
        }
        .uat-users-table td {
            padding: 14px 16px;
            border-bottom: 1px solid rgba(0, 119, 182, 0.1);
        }
        .uat-users-table tr:hover td {
            background: rgba(231, 248, 252, 0.5);
        }
        .status-badge {
            display: inline-block;
            padding: 6px 12px;
            border-radius: 20px;
            font-size: 0.85rem;
            font-weight: 600;
        }
        .status-completed {
            background: rgba(26, 153, 85, 0.15);
            color: var(--success);
        }
        .page-footer { text-align: center; padding: 20px; color: var(--text-muted); font-size: 0.8rem; margin-top: 30px; }
        @media (max-width: 992px) {
            .sidebar { width: 70px; }
            .sidebar .sidebar-header h3, .sidebar .sidebar-header p, .sidebar .menu-text { display: none; }
            .main { margin-left: 70px; }
        }
    </style>
</head>
<body class="role-<?= htmlspecialchars($role) ?>">

    <aside class="sidebar" id="sidebar">
        <button class="sidebar-toggle" id="sidebarToggle" title="Tutup/Buka Menu">
            <i class="fas fa-chevron-left"></i>
        </button>
        <div class="sidebar-header">
            <img src="mainpp.png" alt="Logo MAINPP">
            <h3>MAINPP WORKSPACE</h3>
        </div>
        <ul class="sidebar-menu">
            <?php renderUnifiedSidebarMenu($conn, $role, 'UAT.php'); ?>
        </ul>
    </aside>

    <main class="main">
        <div class="top-bar">
            <h2><i class="fas fa-clipboard-check"></i> User Acceptance Testing (UAT)</h2>
            <?php renderTopBarUserMenu($conn, $role, 'UAT.php'); ?>
        </div>

        <?php
        renderBreadcrumb([
            ['label' => 'UAT', 'url' => 'UAT.php', 'current' => true]
        ]);
        ?>

        <div class="uat-container">
            <?php if ($success_message): ?>
                <div class="alert alert-success">
                    <i class="fas fa-check-circle"></i> <?= htmlspecialchars($success_message) ?>
                </div>
            <?php endif; ?>

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

            <div class="uat-layout <?= $role !== 'superadmin' ? 'full-width' : '' ?>">
                <!-- Left Column: UAT Form -->
                <div class="uat-form-column">
                    <form method="POST" action="">
                        <div class="glass-card">
                            <h3 class="uat-section-title">
                                Maklumat Pengguna
                            </h3>
                            <div class="form-group">
                                <label class="form-label">Nama Penuh</label>
                                <input type="text" class="form-control" value="<?= htmlspecialchars($current_user['fullname'] ?? '') ?>" readonly>
                            </div>
                            <div class="form-group">
                                <label class="form-label">Bahagian</label>
                                <input type="text" class="form-control" value="<?= htmlspecialchars($current_user['department'] ?? '') ?>" readonly>
                            </div>
                            <div class="form-group">
                                <label class="form-label">Jawatan</label>
                                <input type="text" name="position" class="form-control" value="<?= htmlspecialchars($user_info['position'] ?? $current_user['position'] ?? '') ?>" placeholder="Masukkan jawatan anda" readonly>
                            </div>
                        </div>

                        <div class="glass-card">
                            <h3 class="uat-section-title">
                                Checklist Modul
                            </h3>

                            <?php foreach ($checklist_modules as $module => $questions): ?>
                                <?php $stats = $module_stats[$module]; ?>
                                <div class="module-header">
                                    <div class="module-title">
                                        <?= htmlspecialchars($module) ?>
                                    </div>
                                    <div class="module-stats">
                                        <div class="stat-item">
                                            <span class="stat-badge total"><?= count($questions) ?> item</span>
                                        </div>
                                        <div class="stat-item">
                                            <span class="stat-badge pass">Pass <?= $stats['pass'] ?></span>
                                        </div>
                                        <div class="stat-item">
                                            <span class="stat-badge isu">Issue <?= $stats['tidak_berkenaan'] ?></span>
                                        </div>
                                        <div class="stat-item">
                                            <span class="stat-badge belum">Belum <?= $stats['belum'] ?></span>
                                        </div>
                                    </div>
                                </div>
                                
                                <?php foreach ($questions as $qidx => $question): ?>
                                    <?php $qkey = $module . '_' . $qidx; ?>
                                    <?php $qid = $question_id_map[$qkey]; ?>
                                    <?php $existing_response = $user_responses[$qid] ?? null; ?>
                                    <?php $current_status = $existing_response['status'] ?? 'belum_test'; ?>
                                    <div class="question-card" data-status="<?= $current_status ?>" id="question_<?= $qid ?>">
                                        <div class="test-item-title">
                                            Soalan <?= $qid ?>: <?= htmlspecialchars($question['title']) ?>
                                        </div>
                                        <?php if ($question['expected']): ?>
                                            <div class="expected-result">
                                                Expected: <?= htmlspecialchars($question['expected']) ?>
                                            </div>
                                        <?php endif; ?>
                                        
                                        <div class="test-result-section">
                                            <div class="result-dropdown">
                                                <label style="display: block; margin-bottom: 5px; font-weight: 600; font-size: 0.9rem;">Keputusan</label>
                                                <select name="responses[<?= htmlspecialchars($qkey) ?>][status]">
                                                    <option value="pass" <?= $current_status === 'pass' ? 'selected' : '' ?>>Pass</option>
                                                    <option value="fail" <?= $current_status === 'fail' ? 'selected' : '' ?>>Fail</option>
                                                    <option value="tidak_berkenaan" <?= $current_status === 'tidak_berkenaan' ? 'selected' : '' ?>>Tidak Berkenaan</option>
                                                    <option value="belum_test" <?= $current_status === 'belum_test' ? 'selected' : '' ?>>Belum Test</option>
                                                </select>
                                            </div>
                                            <div class="result-notes">
                                                <label style="display: block; margin-bottom: 5px; font-weight: 600; font-size: 0.9rem;">Catatan isu jika ada</label>
                                                <textarea name="responses[<?= htmlspecialchars($qkey) ?>][notes]" placeholder="Masukkan nota jika ada isu..."><?= htmlspecialchars($existing_response['notes'] ?? '') ?></textarea>
                                            </div>
                                        </div>
                                    </div>
                                <?php endforeach; ?>
                            <?php endforeach; ?>
                        </div>

                        <div style="text-align: center; margin-top: 30px; display: flex; gap: 15px; justify-content: center;">
                            <button type="submit" name="simpan_uat" class="btn-simpan">
                                <i class="fas fa-save"></i> Simpan
                            </button>
                            <button type="submit" name="submit_uat" class="btn-submit">
                                <i class="fas fa-paper-plane"></i> Hantar Keputusan UAT
                            </button>
                        </div>
                    </form>
                </div>

                <!-- Right Column: Tester Responses -->
                <?php if ($role === 'superadmin'): ?>
                <div class="uat-responses-column">
                    <div class="responses-header">
                        <h3><i class="fas fa-users"></i> Senarai Tester</h3>
                    </div>

                    <div class="tester-summary" id="testerSummary">
                        <div class="tester-summary-item">
                            <small>Jumlah Tester</small>
                            <strong id="sumTotal">0</strong>
                        </div>
                        <div class="tester-summary-item">
                            <small>Lulus</small>
                            <strong id="sumLulus">0</strong>
                        </div>
                        <div class="tester-summary-item">
                            <small>Gagal</small>
                            <strong id="sumGagal">0</strong>
                        </div>
                        <div class="tester-summary-item">
                            <small>Belum Lengkap</small>
                            <strong id="sumBelum">0</strong>
                        </div>
                    </div>
                    
                    <div class="search-filter-bar">
                        <input type="text" id="searchTester" placeholder="Cari tester/sesi...">
                        <select id="filterStatus">
                            <option value="">Semua Status</option>
                            <option value="lulus">Lulus</option>
                            <option value="lulus_dengan_isu">Lulus dengan Isu</option>
                            <option value="gagal">Gagal</option>
                            <option value="belum_lengkap">Belum Lengkap</option>
                        </select>
                    </div>

                    <div id="testerList" class="tester-list-shell">
                        <?php if (empty($tester_responses)): ?>
                            <p style="color: var(--text-muted); text-align: center; padding: 20px;">Tiada respons UAT lagi.</p>
                        <?php else: ?>
                            <?php foreach ($tester_responses as $tester): ?>
                                <?php 
                                    $pass_count = (int) ($tester['pass_count'] ?? 0);
                                    $fail_count = (int) ($tester['fail_count'] ?? 0);
                                    $tidak_berkenaan_count = (int) ($tester['tidak_berkenaan_count'] ?? 0);
                                    $belum_count = (int) ($tester['belum_count'] ?? 0);
                                    
                                    if ($belum_count > 0) {
                                        $status = 'belum_lengkap';
                                        $status_label = 'Belum Lengkap';
                                    } elseif ($fail_count > 0) {
                                        $status = 'gagal';
                                        $status_label = 'Gagal';
                                    } elseif ($tidak_berkenaan_count > 0) {
                                        $status = 'lulus_dengan_isu';
                                        $status_label = 'Lulus dengan Isu';
                                    } else {
                                        $status = 'lulus';
                                        $status_label = 'Lulus';
                                    }
                                    $completed_timestamp = strtotime($tester['completed_at']);
                                    $detail_report = $tester_detail_reports[(int) $tester['user_id']] ?? null;
                                ?>
                                <div class="tester-card" data-name="<?= strtolower(htmlspecialchars($tester['fullname'])) ?>" data-status="<?= $status ?>" data-date="<?= $completed_timestamp ?>">
                                    <div class="tester-card-top">
                                        <div>
                                            <div class="tester-name"><?= htmlspecialchars($tester['fullname']) ?></div>
                                            <div class="tester-time">#<?= (int) $tester['user_id'] ?> &middot; <?= date('j/n/Y, g:i:s A', $completed_timestamp) ?></div>
                                        </div>
                                        <div class="tester-status <?= $status ?>"><?= $status_label ?></div>
                                    </div>

                                    <div class="tester-stat-row">
                                        <div class="tester-stat pass">Pass<br><?= $pass_count ?></div>
                                        <div class="tester-stat fail">Fail<br><?= $fail_count ?></div>
                                        <div class="tester-stat block">N/A<br><?= $tidak_berkenaan_count ?></div>
                                        <div class="tester-stat belum" style="background: rgba(108, 117, 125, 0.10); color: #6c757d;">Belum<br><?= $belum_count ?></div>
                                    </div>

                                    <button class="btn-view-detail" type="button" onclick="toggleTesterDetail(this)">
                                        Lihat Detail
                                    </button>

                                    <div class="tester-detail-panel">
                                        <?php if ($detail_report): ?>
                                            <?php foreach ($detail_report['modules'] as $module): ?>
                                                <section class="uat-report-module">
                                                    <div class="uat-report-module-title"><?= htmlspecialchars($module['name']) ?></div>
                                                    <?php foreach ($module['questions'] as $question): ?>
                                                        <div class="uat-answer-card">
                                                            <div style="display:flex;justify-content:space-between;gap:12px;align-items:flex-start;">
                                                                <h4>Soalan <?= (int)$question['number'] ?>: <?= htmlspecialchars($question['title']) ?></h4>
                                                                <span class="uat-detail-status-badge <?= htmlspecialchars($question['status']) ?>"><?= htmlspecialchars($question['status_label']) ?></span>
                                                            </div>
                                                            <?php if (!empty($question['expected'])): ?>
                                                                <div class="uat-answer-expected"><?= htmlspecialchars($question['expected']) ?></div>
                                                            <?php endif; ?>
                                                            <?php if (!empty($question['notes'])): ?>
                                                                <div class="uat-answer-notes" style="white-space: pre-line; margin-top: 8px;"><strong>Catatan:</strong><br><?= htmlspecialchars($question['notes']) ?></div>
                                                            <?php endif; ?>
                                                        </div>
                                                    <?php endforeach; ?>
                                                </section>
                                            <?php endforeach; ?>
                                        <?php endif; ?>
                                    </div>
                                </div>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </div>
                </div>
                <?php endif; ?>
            </div>
        </div>

        <div class="page-footer">
            Hakcipta Terpelihara &copy; <?= date('Y') ?> Majlis Agama Islam Negeri Pulau Pinang.
        </div>
    </main>

    <script>
        // Track unsaved changes
        let formChanged = false;
        const uatForm = document.querySelector('form[method="POST"]');

        // Save initial form state
        function saveInitialState() {
            const formElements = uatForm.querySelectorAll('select, textarea, input');
            formElements.forEach(element => {
                element.dataset.initialValue = element.value;
            });
        }

        // Check if form has changed
        function hasFormChanged() {
            const formElements = uatForm.querySelectorAll('select, textarea, input');
            for (let element of formElements) {
                if (element.value !== element.dataset.initialValue) {
                    return true;
                }
            }
            return false;
        }

        // Show warning before leaving page
        function showUnsavedWarning(e) {
            if (hasFormChanged()) {
                e.preventDefault();
                e.returnValue = 'Anda mempunyai perubahan yang belum disimpan. Data akan hilang jika anda meninggalkan halaman ini.';
                return e.returnValue;
            }
        }

        // Initialize form tracking
        saveInitialState();

        // Track changes to form elements
        uatForm.addEventListener('change', function() {
            formChanged = hasFormChanged();
        });

        uatForm.addEventListener('input', function() {
            formChanged = hasFormChanged();
        });

        // Show warning on page unload
        window.addEventListener('beforeunload', showUnsavedWarning);

        // Clear warning when form is submitted
        uatForm.addEventListener('submit', function() {
            window.removeEventListener('beforeunload', showUnsavedWarning);
        });

        // Sidebar Toggle
        document.getElementById('sidebarToggle').addEventListener('click', function() {
            document.getElementById('sidebar').classList.toggle('collapsed');
        });
        
        // Dropdown Menu Toggle
        document.querySelectorAll('.sidebar-menu .dropdown-toggle').forEach(toggle => {
            toggle.addEventListener('click', function(e) {
                e.stopPropagation();
                const dropdown = this.closest('.dropdown');
                dropdown.classList.toggle('active');
            });
        });
        
        // Close dropdowns when clicking outside
        document.addEventListener('click', function() {
            document.querySelectorAll('.sidebar-menu .dropdown.active').forEach(dropdown => {
                dropdown.classList.remove('active');
            });
        });
        
        // Search, filter and sort functionality
        const searchTesterInput = document.getElementById('searchTester');
        const filterStatusSelect = document.getElementById('filterStatus');
        const testerList = document.getElementById('testerList');

        if (searchTesterInput && filterStatusSelect && testerList) {
            searchTesterInput.addEventListener('input', filterTesters);
            filterStatusSelect.addEventListener('change', filterTesters);
        }

        function updateTesterSummary() {
            const testerCards = document.querySelectorAll('.tester-card');
            const sumTotal = document.getElementById('sumTotal');
            const sumLulus = document.getElementById('sumLulus');
            const sumGagal = document.getElementById('sumGagal');
            const sumBelum = document.getElementById('sumBelum');

            if (!sumTotal || !sumLulus || !sumGagal || !sumBelum) {
                return;
            }

            let total = 0;
            let lulus = 0;
            let gagal = 0;
            let belum = 0;

            testerCards.forEach(card => {
                total++;

                if (card.dataset.status === 'lulus') {
                    lulus++;
                } else if (card.dataset.status === 'gagal') {
                    gagal++;
                } else if (card.dataset.status === 'belum_lengkap') {
                    belum++;
                }
            });

            sumTotal.textContent = total;
            sumLulus.textContent = lulus;
            sumGagal.textContent = gagal;
            sumBelum.textContent = belum;
        }
        
        function filterTesters() {
            if (!searchTesterInput || !filterStatusSelect) {
                return;
            }

            const searchTerm = searchTesterInput.value.toLowerCase();
            const statusFilter = filterStatusSelect.value;
            const testerCards = document.querySelectorAll('.tester-card');
            
            testerCards.forEach(card => {
                const name = card.dataset.name;
                const status = card.dataset.status;
                
                const matchesSearch = name.includes(searchTerm);
                const matchesStatus = statusFilter === '' || status === statusFilter;
                
                card.style.display = matchesSearch && matchesStatus ? '' : 'none';
            });
        }

        function toggleTesterDetail(button) {
            const card = button.closest('.tester-card');
            if (!card) {
                return;
            }

            const isOpen = card.classList.toggle('detail-open');
            button.textContent = isOpen ? 'Tutup Detail' : 'Lihat Detail';

            if (isOpen) {
                document.querySelectorAll('.tester-card.detail-open').forEach(openCard => {
                    if (openCard !== card) {
                        openCard.classList.remove('detail-open');
                        const openButton = openCard.querySelector('.btn-view-detail');
                        if (openButton) {
                            openButton.textContent = 'Lihat Detail';
                        }
                    }
                });
            }
        }

        updateTesterSummary();
        
        // Resume from last unanswered question
        (function() {
            const unanswered = document.querySelector('.question-card[data-status="belum_test"]');
            if (unanswered) {
                unanswered.scrollIntoView({ behavior: 'smooth', block: 'center' });
                unanswered.style.transition = 'box-shadow 0.5s ease';
                unanswered.style.boxShadow = '0 0 0 3px rgba(0, 119, 182, 0.4)';
                setTimeout(function() {
                    unanswered.style.boxShadow = '';
                }, 3000);
            }
        })();
    </script>
    <?php renderSessionTimeoutScript(); ?>
</body>
</html>
