<?php
if (session_status() === PHP_SESSION_NONE) { session_start(); }

// Redirect if accessed directly without a session
if (!isset($_SESSION['last_score'])) { 
    header("Location: dashboard.php"); 
    exit(); 
}

$score = $_SESSION['last_score'];
$title = $_SESSION['last_exam_title'];

// Clear session data so the result isn't "re-submitted" on refresh
unset($_SESSION['last_score']);
unset($_SESSION['last_exam_title']);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Exam Submitted | CBT System</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700;800&display=swap" rel="stylesheet">
    <style>
        :root {
            --primary: #0f172a;
            --accent: #2563eb;
            --success: #10b981;
            --bg: #f8fafc;
        }

        * { box-sizing: border-box; margin: 0; padding: 0; }

        body {
            font-family: 'Plus Jakarta Sans', sans-serif;
            background: var(--bg);
            background-image: 
                radial-gradient(at 0% 0%, rgba(37, 99, 235, 0.05) 0px, transparent 50%),
                radial-gradient(at 100% 100%, rgba(16, 185, 129, 0.05) 0px, transparent 50%);
            display: flex;
            align-items: center;
            justify-content: center;
            height: 100vh;
            color: var(--primary);
        }

        .completion-card {
            background: white;
            padding: 60px 40px;
            border-radius: 40px;
            box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.08);
            text-align: center;
            max-width: 550px;
            width: 90%;
            border: 1px solid rgba(226, 232, 240, 0.8);
            position: relative;
            overflow: hidden;
            animation: slideUp 0.6s cubic-bezier(0.16, 1, 0.3, 1);
        }

        @keyframes slideUp {
            from { opacity: 0; transform: translateY(30px); }
            to { opacity: 1; transform: translateY(0); }
        }

        .success-check {
            width: 80px;
            height: 80px;
            background: #ecfdf5;
            color: var(--success);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 35px;
            margin: 0 auto 30px;
            animation: scaleIn 0.5s delay 0.2s both;
        }

        @keyframes scaleIn {
            from { transform: scale(0); }
            to { transform: scale(1); }
        }

        h1 {
            font-size: 28px;
            font-weight: 800;
            margin-bottom: 15px;
            letter-spacing: -0.5px;
        }

        p {
            color: #64748b;
            font-size: 18px;
            line-height: 1.6;
            margin-bottom: 30px;
        }

        .score-display {
            background: #f1f5f9;
            padding: 30px;
            border-radius: 24px;
            margin-bottom: 40px;
            border: 1px solid var(--border);
            transition: 0.3s;
        }

        .score-label {
            display: block;
            text-transform: uppercase;
            font-size: 12px;
            font-weight: 800;
            color: #94a3b8;
            letter-spacing: 1px;
            margin-bottom: 10px;
        }

        .score-value {
            font-size: 48px;
            font-weight: 800;
            color: var(--accent);
            display: block;
        }

        .exam-name {
            font-weight: 700;
            color: var(--primary);
        }

        .btn-home {
            background: var(--primary);
            color: white;
            padding: 18px 40px;
            border-radius: 18px;
            text-decoration: none;
            font-weight: 700;
            font-size: 16px;
            display: inline-flex;
            align-items: center;
            gap: 12px;
            transition: 0.3s all ease;
            box-shadow: 0 10px 15px -3px rgba(15, 23, 42, 0.2);
        }

        .btn-home:hover {
            background: #1e293b;
            transform: translateY(-3px);
            box-shadow: 0 20px 25px -5px rgba(15, 23, 42, 0.2);
        }

        .confetti-decoration {
            position: absolute;
            top: -20px;
            right: -20px;
            font-size: 100px;
            opacity: 0.05;
            transform: rotate(15deg);
        }
    </style>
</head>
<body>

    <div class="completion-card">
        <div class="confetti-decoration"><i class="fa fa-graduation-cap"></i></div>
        
        <div class="success-check">
            <i class="fa fa-check"></i>
        </div>

        <h1>Exam Submitted!</h1>
        <p>Thank you for submitting your exam. You have completed <span class="exam-name"><?= htmlspecialchars($title) ?></span>.</p>
        
        <div class="score-display">
            <span class="score-label">Your Final Score</span>
            <span class="score-value"><?= number_format($score, 2) ?></span>
        </div>

        <a href="dashboard.php" class="btn-home">
            <i class="fa fa-house"></i> Return to Dashboard
        </a>
    </div>

</body>
</html>