:root {
    --purple-50: #f5f3ff;
    --purple-100: #ede9fe;
    --purple-200: #ddd6fe;
    --purple-300: #c4b5fd;
    --purple-400: #a78bfa;
    --purple-500: #8b5cf6;
    --purple-600: #7c3aed;
    --purple-700: #6d28d9;
    --purple-800: #5b21b6;
    --purple-900: #4c1d95;
    --purple-950: #2e1065;

    --bg: #faf8ff;
    --bg-alt: #f3eefe;
    --surface: #ffffff;
    --text: #1f1933;
    --text-muted: #6b6486;
    --border: #e6def7;
    --shadow-sm: 0 2px 6px rgba(124, 58, 237, 0.08);
    --shadow-md: 0 8px 24px rgba(124, 58, 237, 0.12);
    --shadow-lg: 0 20px 60px rgba(76, 29, 149, 0.18);
    --success: #10b981;
    --danger: #ef4444;
    --warning: #f59e0b;
    --radius: 14px;
    --radius-sm: 8px;
    --radius-lg: 22px;
}

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

html, body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Inter", system-ui, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.55;
    -webkit-font-smoothing: antialiased;
    min-height: 100%;
}

body {
    background:
        radial-gradient(circle at 0% 0%, rgba(167, 139, 250, 0.18) 0%, transparent 45%),
        radial-gradient(circle at 100% 100%, rgba(109, 40, 217, 0.14) 0%, transparent 45%),
        var(--bg);
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

.container {
    max-width: 1180px;
    margin: 0 auto;
    padding: 0 22px;
    width: 100%;
}

.main {
    flex: 1;
    padding: 32px 0 60px;
}

.topbar {
    background: linear-gradient(135deg, var(--purple-700), var(--purple-500));
    color: #fff;
    box-shadow: var(--shadow-md);
    position: sticky;
    top: 0;
    z-index: 100;
}

.topbar-inner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 64px;
}

.brand {
    display: flex;
    align-items: center;
    gap: 12px;
    color: #fff;
    text-decoration: none;
    font-weight: 700;
    font-size: 18px;
}

.brand-mark {
    width: 36px;
    height: 36px;
    border-radius: 10px;
    background: rgba(255, 255, 255, 0.18);
    backdrop-filter: blur(10px);
    display: grid;
    place-items: center;
    font-weight: 800;
    font-size: 18px;
}

.topnav {
    display: flex;
    gap: 6px;
    align-items: center;
}

.topnav a {
    color: #fff;
    text-decoration: none;
    padding: 8px 14px;
    border-radius: 999px;
    transition: background 0.2s;
    font-weight: 500;
    font-size: 14px;
}

.topnav a:hover { background: rgba(255, 255, 255, 0.16); }
.topnav a.logout { background: rgba(255, 255, 255, 0.12); }

.footer {
    padding: 22px 0;
    color: var(--text-muted);
    font-size: 13px;
    text-align: center;
    border-top: 1px solid var(--border);
    background: rgba(255, 255, 255, 0.5);
}

.alert {
    padding: 14px 18px;
    border-radius: var(--radius);
    margin: 16px 0;
    font-weight: 500;
}
.alert-error { background: #fee2e2; color: #991b1b; border: 1px solid #fca5a5; }
.alert-success { background: #d1fae5; color: #065f46; border: 1px solid #6ee7b7; }

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 10px 18px;
    border: 0;
    border-radius: 999px;
    font-weight: 600;
    font-size: 14px;
    cursor: pointer;
    text-decoration: none;
    transition: transform 0.05s, box-shadow 0.2s, background 0.2s;
    font-family: inherit;
}
.btn:active { transform: translateY(1px); }
.btn-primary {
    background: linear-gradient(135deg, var(--purple-600), var(--purple-500));
    color: #fff;
    box-shadow: var(--shadow-md);
}
.btn-primary:hover { box-shadow: 0 12px 30px rgba(124, 58, 237, 0.32); }
.btn-success { background: linear-gradient(135deg, #10b981, #059669); color: #fff; box-shadow: var(--shadow-sm); }
.btn-ghost { background: transparent; color: var(--purple-700); border: 1px solid var(--border); }
.btn-ghost:hover { background: var(--purple-50); }
.btn-danger { background: #fee2e2; color: #b91c1c; }
.btn-danger:hover { background: #fecaca; }
.btn-sm { padding: 6px 12px; font-size: 13px; }
.btn-lg { padding: 14px 28px; font-size: 16px; }

.hero { padding: 36px 0; }

.hero-card {
    background: var(--surface);
    border-radius: var(--radius-lg);
    padding: 48px;
    box-shadow: var(--shadow-lg);
    max-width: 820px;
    margin: 0 auto;
    border: 1px solid var(--border);
}

.hero-card h1 {
    font-size: 34px;
    background: linear-gradient(135deg, var(--purple-700), var(--purple-500));
    -webkit-background-clip: text;
    background-clip: text;
    color: transparent;
    margin-bottom: 8px;
}

.hero-sub { color: var(--text-muted); margin-bottom: 28px; }

.start-form { display: flex; flex-direction: column; gap: 22px; }

.field { display: flex; flex-direction: column; gap: 8px; }
.field label { font-weight: 600; font-size: 14px; color: var(--text); }
.field input[type=text], .field input[type=password], .field select, .field textarea {
    padding: 14px 16px;
    border-radius: var(--radius);
    border: 2px solid var(--border);
    background: #fff;
    font-size: 15px;
    transition: border-color 0.2s, box-shadow 0.2s;
    font-family: inherit;
}
.field input:focus, .field select:focus, .field textarea:focus {
    outline: 0;
    border-color: var(--purple-400);
    box-shadow: 0 0 0 4px rgba(167, 139, 250, 0.18);
}

.test-list { display: grid; gap: 12px; grid-template-columns: 1fr; }
@media (min-width: 700px) { .test-list { grid-template-columns: repeat(2, 1fr); } }

.test-card {
    position: relative;
    cursor: pointer;
    border-radius: var(--radius);
    border: 2px solid var(--border);
    padding: 18px;
    transition: all 0.2s;
    background: #fff;
}
.test-card:hover { border-color: var(--purple-300); transform: translateY(-2px); box-shadow: var(--shadow-md); }
.test-card input { position: absolute; opacity: 0; }
.test-card input:checked + .test-card-inner { color: var(--purple-800); }
.test-card:has(input:checked) { border-color: var(--purple-500); background: var(--purple-50); }

.test-card-title { font-weight: 700; font-size: 16px; }
.test-card-desc { color: var(--text-muted); font-size: 13px; margin-top: 4px; }
.test-card-meta { margin-top: 10px; display: flex; gap: 12px; flex-wrap: wrap; font-size: 12px; color: var(--purple-700); }

.empty { padding: 40px 20px; text-align: center; color: var(--text-muted); border: 2px dashed var(--border); border-radius: var(--radius); }

.login-page { display: flex; justify-content: center; padding: 60px 20px; }
.login-card {
    background: var(--surface);
    padding: 40px;
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    width: 100%;
    max-width: 420px;
    display: flex;
    flex-direction: column;
    gap: 18px;
    border: 1px solid var(--border);
}
.login-card h1 { font-size: 22px; margin-bottom: 6px; color: var(--purple-800); }

.admin-page { padding: 12px 0 60px; }
.page-title { font-size: 28px; margin-bottom: 24px; color: var(--purple-900); }
.section-title { font-size: 18px; margin: 28px 0 14px; color: var(--purple-800); }
.back-link { display: inline-block; margin-bottom: 14px; color: var(--purple-700); text-decoration: none; font-size: 14px; }
.back-link:hover { text-decoration: underline; }

.stats-grid { display: grid; gap: 16px; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); margin-bottom: 22px; }
.stat-card {
    background: var(--surface);
    border-radius: var(--radius);
    padding: 22px;
    box-shadow: var(--shadow-sm);
    border: 1px solid var(--border);
}
.stat-value {
    font-size: 32px;
    font-weight: 700;
    background: linear-gradient(135deg, var(--purple-700), var(--purple-500));
    -webkit-background-clip: text;
    background-clip: text;
    color: transparent;
}
.stat-label { color: var(--text-muted); font-size: 13px; margin-top: 4px; }

.card { background: var(--surface); border-radius: var(--radius); padding: 22px; box-shadow: var(--shadow-sm); border: 1px solid var(--border); margin-bottom: 22px; }
.card h3 { margin-bottom: 8px; color: var(--purple-800); }
.muted { color: var(--text-muted); font-size: 13px; }

.upload-form { display: flex; gap: 12px; flex-wrap: wrap; align-items: center; margin-top: 14px; }
.upload-form input[type=file] { padding: 10px; border: 2px dashed var(--border); border-radius: var(--radius-sm); background: var(--purple-50); }

.table-wrap { overflow-x: auto; background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow-sm); border: 1px solid var(--border); }
.data-table { width: 100%; border-collapse: collapse; }
.data-table th, .data-table td { padding: 12px 14px; text-align: left; border-bottom: 1px solid var(--border); font-size: 14px; }
.data-table th { background: var(--purple-50); color: var(--purple-800); font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: 0.04em; }
.data-table tr:last-child td { border-bottom: 0; }
.data-table tr:hover td { background: var(--purple-50); }
.data-table .actions { display: flex; gap: 6px; flex-wrap: wrap; }
.data-table code { background: var(--purple-100); padding: 2px 8px; border-radius: 6px; font-size: 12px; color: var(--purple-700); }

.badge { display: inline-block; padding: 3px 10px; border-radius: 999px; font-size: 12px; font-weight: 600; }
.badge-success { background: #d1fae5; color: #065f46; }
.badge-fail { background: #fee2e2; color: #991b1b; }
.badge-muted { background: #e5e7eb; color: #4b5563; }

.cheat-badge { display: inline-block; padding: 2px 10px; border-radius: 999px; font-weight: 700; font-size: 12px; }
.cheat-low { background: #d1fae5; color: #065f46; }
.cheat-mid { background: #fef3c7; color: #92400e; }
.cheat-high { background: #fee2e2; color: #991b1b; }

.grade-pill {
    display: inline-grid;
    place-items: center;
    width: 32px;
    height: 32px;
    border-radius: 10px;
    font-weight: 800;
    font-size: 16px;
    color: #fff;
    box-shadow: var(--shadow-sm);
}
.grade-2 { background: linear-gradient(135deg, #ef4444, #dc2626); }
.grade-3 { background: linear-gradient(135deg, #f59e0b, #d97706); }
.grade-4 { background: linear-gradient(135deg, #8b5cf6, #7c3aed); }
.grade-5 { background: linear-gradient(135deg, #10b981, #059669); }

.result-grade-big {
    font-size: 110px;
    font-weight: 900;
    line-height: 0.95;
    letter-spacing: -2px;
}
.result-sub { margin-top: 8px; font-size: 14px; opacity: 0.92; }

.filter-bar { display: flex; gap: 10px; align-items: center; margin-bottom: 14px; flex-wrap: wrap; }
.filter-bar select { padding: 8px 14px; border-radius: var(--radius-sm); border: 1px solid var(--border); background: #fff; }
.filter-spacer { flex: 1; }

.test-shell { user-select: none; -webkit-user-select: none; }
.test-shell.unselectable * { user-select: none !important; -webkit-user-select: none !important; }
.test-shell.unselectable input, .test-shell.unselectable textarea { user-select: text !important; -webkit-user-select: text !important; }

.test-header {
    background: linear-gradient(135deg, var(--purple-700), var(--purple-500));
    color: #fff;
    padding: 18px 0;
    margin-bottom: 22px;
    box-shadow: var(--shadow-md);
}
.test-header-inner { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 14px; }
.test-info { display: flex; flex-direction: column; gap: 4px; }
.test-title { font-weight: 700; font-size: 18px; }
.test-student { font-size: 13px; opacity: 0.85; }
.test-stats { display: flex; gap: 10px; }
.stat-pill {
    background: rgba(255, 255, 255, 0.18);
    backdrop-filter: blur(8px);
    border-radius: 999px;
    padding: 6px 14px;
    font-size: 13px;
    font-weight: 600;
}
.stat-pill.timer.danger { background: #ef4444; }

.progress-bar { height: 8px; background: var(--border); border-radius: 999px; overflow: hidden; margin-bottom: 24px; }
.progress-fill { height: 100%; background: linear-gradient(90deg, var(--purple-500), var(--purple-700)); transition: width 0.3s; width: 0; }

.test-body { padding: 0 0 40px; }

.question-area { background: var(--surface); border-radius: var(--radius-lg); padding: 36px; box-shadow: var(--shadow-md); border: 1px solid var(--border); margin-bottom: 22px; min-height: 320px; }
.question-text { font-size: 20px; font-weight: 600; line-height: 1.5; color: var(--purple-900); margin-bottom: 22px; }

.option-list { display: flex; flex-direction: column; gap: 10px; }
.option-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 14px 18px;
    border: 2px solid var(--border);
    border-radius: var(--radius);
    cursor: pointer;
    background: #fff;
    transition: all 0.15s;
    font-size: 15px;
}
.option-item:hover { border-color: var(--purple-300); background: var(--purple-50); }
.option-item input { width: 20px; height: 20px; accent-color: var(--purple-600); }
.option-item.checked { border-color: var(--purple-500); background: var(--purple-50); }

.text-answer { width: 100%; padding: 14px 16px; border-radius: var(--radius); border: 2px solid var(--border); font-size: 15px; font-family: inherit; }
.text-answer:focus { border-color: var(--purple-400); outline: 0; box-shadow: 0 0 0 4px rgba(167, 139, 250, 0.18); }

.tf-row { display: flex; gap: 14px; }
.tf-row .option-item { flex: 1; justify-content: center; padding: 18px; font-size: 16px; font-weight: 600; }

.matching-table { width: 100%; border-collapse: collapse; }
.matching-table td { padding: 8px 4px; vertical-align: middle; }
.matching-left { padding: 12px 16px; border-radius: var(--radius); background: var(--purple-50); border: 1px solid var(--purple-200); font-weight: 500; }
.matching-table select { width: 100%; padding: 12px 14px; border-radius: var(--radius); border: 2px solid var(--border); font-size: 14px; background: #fff; }
.matching-table select:focus { border-color: var(--purple-400); outline: 0; }

.ordering-list { display: flex; flex-direction: column; gap: 8px; }
.ordering-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 14px 16px;
    background: #fff;
    border: 2px solid var(--border);
    border-radius: var(--radius);
    cursor: grab;
    user-select: none;
    transition: all 0.15s;
}
.ordering-item:active { cursor: grabbing; }
.ordering-item.dragging { opacity: 0.4; }
.ordering-item .handle { color: var(--purple-400); font-size: 18px; }
.ordering-item .ord-num {
    width: 28px; height: 28px; border-radius: 999px;
    background: var(--purple-100); color: var(--purple-700);
    display: grid; place-items: center; font-weight: 700; font-size: 13px;
}
.ordering-item:hover { border-color: var(--purple-300); }

.fill-blank-text { font-size: 16px; line-height: 2.4; word-spacing: 4px; }
.fill-blank-text input {
    display: inline-block;
    min-width: 120px;
    padding: 6px 12px;
    border: 2px solid var(--border);
    border-radius: 8px;
    font-size: 15px;
    font-family: inherit;
    background: #fff;
}
.fill-blank-text input:focus { border-color: var(--purple-500); outline: 0; }

.test-controls { display: flex; gap: 12px; justify-content: space-between; margin-bottom: 22px; }

.question-nav {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    background: var(--surface);
    padding: 14px;
    border-radius: var(--radius);
    border: 1px solid var(--border);
    box-shadow: var(--shadow-sm);
}
.qnav-btn {
    width: 38px; height: 38px;
    border-radius: 10px;
    border: 2px solid var(--border);
    background: #fff;
    color: var(--text-muted);
    font-weight: 600;
    font-size: 13px;
    cursor: pointer;
    transition: all 0.15s;
}
.qnav-btn:hover { border-color: var(--purple-400); }
.qnav-btn.answered { background: var(--purple-100); border-color: var(--purple-300); color: var(--purple-800); }
.qnav-btn.current { background: linear-gradient(135deg, var(--purple-600), var(--purple-500)); color: #fff; border-color: transparent; box-shadow: var(--shadow-sm); }

.overlay {
    position: fixed; inset: 0;
    background: rgba(46, 16, 101, 0.9);
    backdrop-filter: blur(8px);
    display: grid;
    place-items: center;
    z-index: 1000;
}
.overlay-box {
    background: #fff;
    border-radius: var(--radius-lg);
    padding: 42px;
    max-width: 480px;
    text-align: center;
    box-shadow: var(--shadow-lg);
}
.overlay-box h2 { color: var(--purple-800); margin-bottom: 12px; font-size: 22px; }
.overlay-box p { color: var(--text-muted); margin-bottom: 22px; }
.overlay-box.warning h2 { color: var(--warning); }

.loader {
    width: 50px; height: 50px;
    border: 4px solid var(--purple-100);
    border-top-color: var(--purple-600);
    border-radius: 50%;
    margin: 0 auto 20px;
    animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.result-page { padding: 30px 0; }
.result-card { background: var(--surface); border-radius: var(--radius-lg); overflow: hidden; box-shadow: var(--shadow-lg); border: 1px solid var(--border); max-width: 760px; margin: 0 auto 22px; }
.result-header { padding: 50px 30px; text-align: center; color: #fff; }
.result-header.passed { background: linear-gradient(135deg, #10b981, #059669); }
.result-header.failed { background: linear-gradient(135deg, #ef4444, #dc2626); }
.result-percent { font-size: 64px; font-weight: 800; line-height: 1; }
.result-status { font-size: 18px; opacity: 0.95; margin-top: 6px; }

.result-grid { display: grid; gap: 0; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }
.result-stat { padding: 18px 22px; border-bottom: 1px solid var(--border); border-right: 1px solid var(--border); }
.result-stat .label { color: var(--text-muted); font-size: 12px; text-transform: uppercase; letter-spacing: 0.04em; }
.result-stat .value { color: var(--purple-900); font-weight: 600; font-size: 16px; margin-top: 4px; word-break: break-word; }
.result-stat .value.cheat-high { color: var(--danger); }
.result-stat .value.cheat-mid { color: var(--warning); }

.result-actions { padding: 26px; text-align: center; }

.answers-list { display: flex; flex-direction: column; gap: 12px; }
.answer-card { border-radius: var(--radius); padding: 18px; border-left: 4px solid var(--border); background: var(--surface); box-shadow: var(--shadow-sm); border: 1px solid var(--border); }
.answer-card.correct { border-left-color: var(--success); }
.answer-card.partial { border-left-color: var(--warning); }
.answer-card.wrong { border-left-color: var(--danger); }
.answer-header { display: flex; gap: 12px; align-items: flex-start; margin-bottom: 10px; }
.qnum { background: var(--purple-100); color: var(--purple-700); padding: 2px 10px; border-radius: 6px; font-weight: 700; font-size: 13px; }
.qtext { flex: 1; font-weight: 600; }
.qpoints { font-weight: 700; color: var(--purple-700); }
.answer-body { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; padding-top: 8px; border-top: 1px dashed var(--border); }
@media (max-width: 720px) { .answer-body { grid-template-columns: 1fr; } }
.answer-label { font-size: 12px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.04em; margin-bottom: 4px; }
.answer-content { background: var(--purple-50); padding: 10px 14px; border-radius: var(--radius-sm); font-size: 14px; }

.events-log { background: var(--surface); border-radius: var(--radius); padding: 14px; border: 1px solid var(--border); font-family: ui-monospace, monospace; font-size: 12px; max-height: 360px; overflow-y: auto; }
.event-row { padding: 4px 0; border-bottom: 1px solid var(--border); display: flex; gap: 10px; }
.event-row:last-child { border-bottom: 0; }
.event-time { color: var(--text-muted); }
.event-type { font-weight: 600; color: var(--purple-700); }
.event-details { color: var(--text-muted); }

@media print { .topbar, .footer, .test-controls, .question-nav { display: none !important; } }
