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

/* Math loading overlay */
.math-loading-overlay {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: var(--bg, #EEF2FF);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 9999;
}
.math-loading-spinner {
    width: 36px; height: 36px;
    border: 4px solid var(--border, #C7D2FE);
    border-top-color: var(--primary, #4F46E5);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.math-loading-text {
    margin-top: 12px;
    color: var(--text-dim, #6366F1);
    font-size: 14px;
}

:root {
    --bg: #EEF2FF;
    --bg-card: #ffffff;
    --bg-hover: #E0E7FF;
    --border: #C7D2FE;
    --text: #312E81;
    --text-dim: #6366F1;
    --text-muted: #818CF8;
    --primary: #4F46E5;
    --primary-light: #818CF8;
    --primary-bg: #EEF2FF;
    --success: #22C55E;
    --success-bg: #DCFCE7;
    --success-border: #86EFAC;
    --danger: #EF4444;
    --danger-bg: #FEE2E2;
    --danger-border: #FCA5A5;
    --warning: #F59E0B;
    --warning-bg: #FEF3C7;
    --warning-border: #FCD34D;
    --hint-1: #EEF2FF;
    --hint-2: #FFF7ED;
    --hint-3: #FDF2F8;
    --shadow-sm: 0 1px 3px rgba(79, 70, 229, 0.08);
    --shadow-md: 0 4px 12px rgba(79, 70, 229, 0.1);
    --shadow-lg: 0 12px 32px rgba(79, 70, 229, 0.12);
    --radius: 14px;
    --radius-lg: 18px;
}

body {
    background: var(--bg);
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft JhengHei", sans-serif;
    font-size: 16px;
    line-height: 1.7;
    -webkit-font-smoothing: antialiased;
}

/* Page transition */
main {
    padding: 20px;
    max-width: 900px;
    margin: 0 auto;
    animation: fadeSlideIn 0.35s ease-out;
}

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

@keyframes popIn {
    0% { opacity: 0; transform: scale(0.92); }
    70% { transform: scale(1.02); }
    100% { opacity: 1; transform: scale(1); }
}

@keyframes shakeWrong {
    0%, 100% { transform: translateX(0); }
    20% { transform: translateX(-8px); }
    40% { transform: translateX(8px); }
    60% { transform: translateX(-5px); }
    80% { transform: translateX(5px); }
}

@keyframes bounceCorrect {
    0% { transform: scale(1); }
    30% { transform: scale(1.06); }
    60% { transform: scale(0.97); }
    100% { transform: scale(1); }
}

/* Topbar */
.topbar {
    background: rgba(255, 255, 255, 0.85);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border-bottom: 1px solid var(--border);
    padding: 12px 20px;
    display: flex;
    align-items: center;
    gap: 20px;
    position: sticky;
    top: 0;
    z-index: 10;
    transition: box-shadow 0.2s;
}
.topbar:hover { box-shadow: var(--shadow-sm); }

.brand {
    font-size: 20px;
    font-weight: 800;
    color: var(--primary);
    letter-spacing: -0.3px;
}

.nav { display: flex; gap: 6px; flex: 1; }
.nav a {
    color: var(--text-dim);
    text-decoration: none;
    padding: 6px 14px;
    border-radius: 10px;
    font-size: 14px;
    font-weight: 500;
    transition: all 0.2s;
}
.nav a:hover {
    background: var(--bg-hover);
    color: var(--primary);
}
.nav a.active {
    background: var(--primary-bg);
    color: var(--primary);
    font-weight: 700;
}
.nav-report-link {
    color: var(--text-muted);
    text-decoration: none;
    font-size: 16px;
    padding: 4px;
}

.user-info {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 14px;
    color: var(--text-dim);
}
.btn-logout {
    color: var(--danger);
    text-decoration: none;
    font-size: 13px;
    font-weight: 500;
    padding: 4px 10px;
    border-radius: 8px;
    transition: background 0.2s;
}
.btn-logout:hover { background: var(--danger-bg); }

/* ===================== */
/* Login Page            */
/* ===================== */
.login-page {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg);
    padding: 20px;
}
.login-card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 18px;
    padding: 40px 32px;
    width: 100%;
    max-width: 380px;
    text-align: center;
    box-shadow: var(--shadow-md);
}
.login-brand {
    font-size: 26px;
    font-weight: 800;
    color: var(--primary);
    margin-bottom: 4px;
}
.login-subtitle {
    color: var(--text-dim);
    margin-bottom: 28px;
    font-size: 14px;
}
.login-card label {
    display: block;
    text-align: left;
    margin-bottom: 16px;
    font-size: 14px;
    font-weight: 600;
    color: var(--text);
}
.login-card input {
    width: 100%;
    padding: 11px 14px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 16px;
    margin-top: 6px;
    transition: border-color 0.2s;
    background: #fff;
}
.login-card input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.1);
}
.login-card button {
    width: 100%;
    margin-top: 8px;
    padding: 12px;
    font-size: 16px;
    font-weight: 600;
    border: none;
    border-radius: 8px;
    background: var(--primary);
    color: #fff;
    cursor: pointer;
    transition: background 0.2s;
}
.login-card button:hover { background: #4338CA; }

.error {
    background: var(--danger-bg);
    color: var(--danger);
    padding: 10px 14px;
    border-radius: 10px;
    margin-bottom: 18px;
    font-size: 14px;
    border: 1px solid var(--danger-border);
    animation: shakeWrong 0.4s ease-out;
}

/* ===================== */
/* Buttons               */
/* ===================== */
.btn-primary {
    display: inline-block;
    background: var(--primary);
    color: white;
    border: none;
    padding: 12px 28px;
    border-radius: 12px;
    font-size: 15px;
    font-weight: 700;
    cursor: pointer;
    text-decoration: none;
    text-align: center;
    width: 100%;
    transition: all 0.2s;
    box-shadow: 0 2px 8px rgba(79, 70, 229, 0.25);
}
.btn-primary:hover {
    background: var(--primary-light);
    transform: translateY(-1px);
    box-shadow: 0 4px 14px rgba(79, 70, 229, 0.3);
}
.btn-primary:active { transform: translateY(0); }
.btn-primary:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
    box-shadow: none;
}

.btn-big { padding: 16px 32px; font-size: 17px; }

.btn-secondary {
    display: inline-block;
    background: var(--bg-card);
    color: var(--text);
    border: 2px solid var(--border);
    padding: 10px 20px;
    border-radius: 12px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    text-decoration: none;
    transition: all 0.2s;
}
.btn-secondary:hover {
    border-color: var(--primary);
    color: var(--primary);
    background: var(--primary-bg);
}

.btn-hint {
    display: inline-block;
    background: transparent;
    color: var(--text-muted);
    border: 2px dashed var(--border);
    padding: 10px 20px;
    border-radius: 12px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    text-decoration: none;
    transition: all 0.2s;
}
.btn-hint:hover {
    border-color: var(--primary-light);
    color: var(--primary);
    background: var(--primary-bg);
}

/* ===================== */
/* Filters / Chips       */
/* ===================== */
.filters {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    margin-bottom: 20px;
}
.chip {
    padding: 8px 16px;
    border-radius: 24px;
    background: var(--bg-card);
    border: 2px solid var(--border);
    color: var(--text-dim);
    text-decoration: none;
    font-size: 13px;
    font-weight: 600;
    transition: all 0.2s;
    cursor: pointer;
}
.chip:hover {
    border-color: var(--primary-light);
    color: var(--primary);
    transform: translateY(-1px);
}
.chip.active {
    background: var(--primary);
    color: white;
    border-color: var(--primary);
    box-shadow: 0 2px 8px rgba(79, 70, 229, 0.3);
}

/* ===================== */
/* Progress Bar          */
/* ===================== */
.progress-summary {
    background: var(--bg-card);
    border: 2px solid var(--border);
    border-radius: var(--radius);
    padding: 16px 20px;
    margin-bottom: 20px;
    display: flex;
    align-items: center;
    gap: 16px;
    box-shadow: var(--shadow-sm);
}
.progress-bar-wrap {
    flex: 1;
    height: 12px;
    background: var(--bg);
    border-radius: 6px;
    overflow: hidden;
}
.progress-bar-fill {
    height: 100%;
    background: linear-gradient(90deg, var(--primary), var(--success));
    border-radius: 6px;
    transition: width 0.5s ease-out;
    min-width: 0;
}
.progress-text {
    font-size: 14px;
    font-weight: 700;
    color: var(--text);
    white-space: nowrap;
}

.empty { text-align: center; padding: 60px 20px; color: var(--text-dim); }
.empty p:first-child { font-size: 20px; font-weight: 600; margin-bottom: 8px; }
.hint-text { font-size: 13px; margin-top: 8px; }

/* ===================== */
/* Chapter Group         */
/* ===================== */
.chapter-group {
    margin-bottom: 24px;
}
.chapter-group-title {
    font-size: 15px;
    font-weight: 700;
    color: var(--primary);
    padding: 6px 0 10px;
    border-bottom: 2px solid var(--border);
    margin-bottom: 12px;
    display: flex;
    align-items: center;
    gap: 8px;
}
.chapter-group-count {
    font-size: 12px;
    font-weight: 600;
    background: var(--primary-bg);
    color: var(--primary);
    padding: 2px 10px;
    border-radius: 20px;
}

/* ===================== */
/* Question Grid         */
/* ===================== */
.question-grid {
    display: grid;
    gap: 12px;
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
}
.q-card {
    background: var(--bg-card);
    border: 2px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
    text-decoration: none;
    color: var(--text);
    transition: all 0.2s ease;
    border-left: 5px solid var(--border);
    box-shadow: var(--shadow-sm);
    position: relative;
}
.q-card:hover {
    border-color: var(--primary);
    border-left-color: var(--primary);
    transform: translateY(-3px);
    box-shadow: var(--shadow-md);
}
.q-card.done {
    border-left-color: var(--success);
    background: linear-gradient(135deg, #F0FDF4 0%, #FAFFFE 100%);
}
.q-card.wrong {
    border-left-color: var(--danger);
    background: linear-gradient(135deg, #FEF2F2 0%, #FFFAFA 100%);
}
.q-header { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.q-chapter {
    font-size: 11px;
    font-weight: 700;
    color: var(--primary);
    background: var(--primary-bg);
    padding: 3px 10px;
    border-radius: 8px;
    letter-spacing: 0.3px;
    text-transform: uppercase;
}
.q-type-tag {
    font-size: 11px;
    font-weight: 600;
    padding: 3px 8px;
    border-radius: 8px;
    background: var(--warning-bg);
    color: #92400E;
}
.q-type-tag.fill { background: #F0FDFA; color: #115E59; }

.q-number { font-size: 15px; font-weight: 700; }
.q-badge {
    font-size: 11px;
    padding: 3px 10px;
    border-radius: 12px;
    margin-left: auto;
    font-weight: 700;
}
.q-badge.correct { background: var(--success-bg); color: var(--success); }
.q-badge.wrong { background: var(--danger-bg); color: var(--danger); }
.q-badge.new { background: var(--primary-bg); color: var(--primary); }
.q-preview {
    font-size: 13px;
    color: var(--text-dim);
    line-height: 1.6;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}
.q-meta {
    font-size: 12px;
    color: var(--text-muted);
    margin-top: 8px;
    font-weight: 500;
}

/* ===================== */
/* Question View         */
/* ===================== */
.breadcrumb {
    display: flex;
    gap: 8px;
    align-items: center;
    margin-bottom: 16px;
    font-size: 14px;
    color: var(--text-dim);
}
.breadcrumb a {
    color: var(--primary);
    text-decoration: none;
    font-weight: 600;
    padding: 4px 10px;
    border-radius: 8px;
    transition: background 0.2s;
}
.breadcrumb a:hover { background: var(--primary-bg); }

.question-view {
    background: var(--bg-card);
    border: 2px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 28px;
    box-shadow: var(--shadow-md);
}
.q-title {
    font-size: 14px;
    color: var(--text-muted);
    margin-bottom: 12px;
    font-weight: 700;
    letter-spacing: 0.5px;
}
.q-content {
    font-size: 19px;
    line-height: 1.9;
    margin-bottom: 24px;
    white-space: pre-wrap;
    color: var(--text);
}
.q-image { margin: 16px 0; text-align: center; }
.q-image img {
    max-width: 100%;
    border-radius: var(--radius);
    border: 2px solid var(--border);
}
/* 5/21 加：math-content 內所有 img 限寬避免截斷（如 \overline{EH} 公式圖被裁）*/
.math-content img {
    max-width: 100%;
    height: auto;
    vertical-align: middle;
}

/* ===================== */
/* Options (clickable)   */
/* ===================== */
.options { margin-bottom: 24px; }
.option {
    padding: 14px 18px;
    margin: 10px 0;
    border-radius: var(--radius);
    background: var(--bg);
    border: 2px solid var(--border);
    font-size: 16px;
    display: flex;
    gap: 12px;
    align-items: flex-start;
    transition: all 0.2s;
}
.option.clickable { cursor: pointer; }
.option.clickable:hover {
    border-color: var(--primary);
    background: #EEF2FF;
    transform: translateX(4px);
    box-shadow: var(--shadow-sm);
}
.option.selected {
    border-color: var(--primary);
    background: #E0E7FF;
    box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.15);
}
.option-key {
    font-weight: 800;
    min-width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--primary);
    background: var(--primary-bg);
    border-radius: 10px;
    font-size: 14px;
    flex-shrink: 0;
}
.option-text {
    padding-top: 6px;
    line-height: 1.6;
}
.selected-correct {
    background: var(--success-bg) !important;
    border-color: var(--success) !important;
    animation: bounceCorrect 0.4s ease-out;
}
.selected-wrong {
    background: var(--danger-bg) !important;
    border-color: var(--danger) !important;
    animation: shakeWrong 0.4s ease-out;
}
.correct-answer {
    background: var(--success-bg);
    border-color: var(--success);
}

/* ===================== */
/* Answer Form           */
/* ===================== */
.answer-form { margin: 20px 0; }
.action-buttons {
    display: flex;
    gap: 12px;
    flex-wrap: wrap;
    align-items: stretch;
}
.action-buttons .btn-primary {
    flex: 2;
    min-width: 160px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px 24px;
    font-size: 17px;
}
.action-buttons .btn-hint {
    flex: 1;
    min-width: 120px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 14px 16px;
    font-size: 14px;
}

/* Keyword highlighting */
.kw-highlight {
    color: #DC2626;
    font-weight: 700;
    text-decoration: underline;
    text-decoration-color: #DC2626;
    text-underline-offset: 3px;
}

/* ===================== */
/* Results               */
/* ===================== */
.result {
    padding: 18px 20px;
    border-radius: var(--radius);
    margin: 18px 0;
    animation: popIn 0.35s ease-out;
}
.result h3 { font-size: 20px; margin-bottom: 6px; font-weight: 700; }
.result p { font-size: 15px; color: var(--text-dim); }

.result-correct {
    background: var(--success-bg);
    border: 2px solid var(--success-border);
}
.result-wrong {
    background: var(--danger-bg);
    border: 2px solid var(--danger-border);
    animation: shakeWrong 0.4s ease-out;
}
.result-final {
    background: var(--warning-bg);
    border: 2px solid var(--warning-border);
}

/* ===================== */
/* Hints                 */
/* ===================== */
.hint {
    margin: 14px 0;
    border-radius: var(--radius);
    padding: 16px 18px;
    animation: fadeSlideIn 0.3s ease-out;
}
.hint-1 {
    background: var(--hint-1);
    border: 2px solid #A5B4FC;
}
.hint-2 {
    background: var(--hint-2);
    border: 2px solid #FDBA74;
}
.hint-3 {
    background: var(--hint-3);
    border: 2px solid #F9A8D4;
}
.hint-label {
    font-size: 13px;
    font-weight: 700;
    margin-bottom: 8px;
    color: var(--text);
}
.hint-content {
    font-size: 16px;
    line-height: 1.8;
    white-space: pre-wrap;
}

.solution {
    margin: 14px 0;
    background: #F5F3FF;
    border: 2px solid #C4B5FD;
    border-radius: var(--radius);
    padding: 16px 18px;
    animation: fadeSlideIn 0.3s ease-out;
}

.next-stage { display: flex; gap: 10px; margin-top: 18px; flex-wrap: wrap; }

/* Math content */
.math-content .katex { font-size: 1.1em; }

/* ===================== */
/* Fill-in questions     */
/* ===================== */
.fillin-form { margin: 24px 0; }
.fillin-input-area { margin-bottom: 20px; }
.fillin-label { font-size: 16px; font-weight: 700; margin-bottom: 8px; }
.fillin-help {
    font-size: 13px;
    color: var(--text-muted);
    margin-bottom: 10px;
    background: var(--primary-bg);
    display: inline-block;
    padding: 4px 12px;
    border-radius: 8px;
}
.fillin-box {
    width: 100%;
    max-width: 450px;
    padding: 16px 20px;
    font-size: 22px;
    border: 3px solid var(--border);
    border-radius: var(--radius);
    font-family: 'KaTeX_Main', serif;
    transition: all 0.2s;
    background: #FAFAFE;
}
.fillin-box:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 4px rgba(79, 70, 229, 0.12);
    background: #fff;
}
.fillin-result {
    margin: 18px 0;
    padding: 18px;
    background: var(--bg);
    border-radius: var(--radius);
    border: 2px solid var(--border);
}
.fillin-result .fillin-label { margin-bottom: 8px; }
.correct-text { color: var(--success); font-weight: 700; }

/* ===================== */
/* Mobile Responsive     */
/* ===================== */
@media (max-width: 640px) {
    .topbar {
        flex-wrap: wrap;
        gap: 8px;
        padding: 10px 14px;
    }
    .brand { font-size: 18px; }
    .nav a { font-size: 13px; padding: 5px 10px; }

    main { padding: 14px; }

    .question-grid {
        grid-template-columns: 1fr;
        gap: 10px;
    }

    .question-view { padding: 18px 16px; }

    .q-content { font-size: 17px; line-height: 1.85; }

    .option { padding: 12px 14px; font-size: 15px; }
    .option-key { min-width: 32px; height: 32px; font-size: 13px; }

    .action-buttons { flex-direction: column; }
    .action-buttons .btn-primary,
    .action-buttons .btn-hint {
        flex: unset;
        width: 100%;
        min-width: unset;
    }

    .fillin-box {
        max-width: 100%;
        font-size: 20px;
        padding: 14px 16px;
    }

    .login-card {
        padding: 36px 28px;
        border-radius: 20px;
    }
    .login-card h1 { font-size: 26px; }

    .progress-summary {
        flex-direction: column;
        align-items: stretch;
        gap: 10px;
        padding: 14px 16px;
    }
    .progress-text { text-align: center; }

    .filters { gap: 6px; }
    .chip { padding: 6px 12px; font-size: 12px; }

    .result h3 { font-size: 18px; }
    .hint-content { font-size: 15px; }
}

/* Touch targets - minimum 44px */
@media (pointer: coarse) {
    .option { min-height: 48px; }
    .chip { min-height: 44px; display: flex; align-items: center; }
    .nav a { min-height: 44px; display: flex; align-items: center; }
    .btn-logout { min-height: 44px; display: flex; align-items: center; }
}

/* ===================== */
/* Teacher Section       */
/* ===================== */
.teacher-section {
    max-width: 960px;
    margin: 0 auto;
}
.teacher-section h1 {
    font-size: 24px;
    font-weight: 800;
    color: var(--primary);
    margin-bottom: 20px;
}
.teacher-section h2 {
    font-size: 18px;
    font-weight: 700;
    color: var(--text);
    margin: 28px 0 14px;
}
.teacher-actions {
    display: flex;
    gap: 12px;
    margin-bottom: 16px;
    flex-wrap: wrap;
}

/* ===================== */
/* Unified Button System */
/* ===================== */
.btn-action {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: var(--primary);
    color: white;
    border: 2px solid var(--primary);
    padding: 10px 22px;
    border-radius: 10px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    text-decoration: none;
    transition: all 0.2s;
    white-space: nowrap;
    min-height: 40px;
    line-height: 1.4;
}
.btn-action:hover {
    background: var(--primary-light);
    border-color: var(--primary-light);
    transform: translateY(-1px);
    box-shadow: 0 4px 14px rgba(79, 70, 229, 0.25);
}
.btn-action:active { transform: translateY(0); }

.btn-action-secondary {
    background: var(--bg-card);
    color: var(--text);
    border-color: var(--border);
}
.btn-action-secondary:hover {
    border-color: var(--primary);
    color: var(--primary);
    background: var(--primary-bg);
    box-shadow: none;
}

.btn-action-danger {
    background: var(--danger);
    border-color: var(--danger);
    color: white;
}
.btn-action-danger:hover {
    opacity: 0.85;
    border-color: var(--danger);
    box-shadow: none;
}

.btn-action-sm {
    padding: 6px 14px;
    font-size: 13px;
    min-height: 32px;
    border-radius: 8px;
}

.btn-action-lg {
    padding: 14px 32px;
    font-size: 16px;
}

.btn-action-active {
    background: var(--primary);
    color: white;
    border-color: var(--primary);
}

/* Flash messages */
.flash-success {
    background: var(--success-bg);
    color: #166534;
    border: 2px solid var(--success-border);
    padding: 14px 18px;
    border-radius: var(--radius);
    margin-bottom: 18px;
    font-weight: 600;
    animation: popIn 0.35s ease-out;
}
.flash-error {
    background: var(--danger-bg);
    color: var(--danger);
    border: 2px solid var(--danger-border);
    padding: 14px 18px;
    border-radius: var(--radius);
    margin-bottom: 18px;
    font-weight: 600;
    animation: shakeWrong 0.4s ease-out;
}

/* ===================== */
/* Unified Data Table    */
/* ===================== */
.data-table {
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    background: var(--bg-card);
    border-radius: var(--radius);
    overflow: hidden;
    box-shadow: var(--shadow-sm);
    border: 2px solid var(--border);
    margin-bottom: 16px;
}
.data-table th {
    background: var(--primary-bg);
    color: var(--primary);
    font-size: 13px;
    font-weight: 700;
    padding: 12px 16px;
    text-align: left;
    letter-spacing: 0.3px;
    white-space: nowrap;
    min-width: 60px;
}
.data-table td {
    padding: 10px 16px;
    font-size: 14px;
    border-top: 1px solid var(--border);
    vertical-align: middle;
}
.data-table tbody tr:hover {
    background: var(--bg-hover);
}
.data-table code {
    background: var(--bg);
    padding: 2px 8px;
    border-radius: 6px;
    font-size: 13px;
}

/* Status badges */
.status-badge {
    display: inline-block;
    padding: 3px 10px;
    border-radius: 8px;
    font-size: 12px;
    font-weight: 700;
}
.status-parsed {
    background: var(--warning-bg);
    color: #92400E;
}
.status-confirmed {
    background: var(--success-bg);
    color: #166534;
}
.status-error {
    background: var(--danger-bg);
    color: var(--danger);
}

/* Legacy compat — old class aliases */
.btn-small {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: var(--primary);
    color: white;
    border: 2px solid var(--primary);
    padding: 6px 14px;
    border-radius: 8px;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    text-decoration: none;
    transition: all 0.2s;
    min-height: 32px;
}
.btn-small:hover {
    background: var(--primary-light);
    transform: translateY(-1px);
}

/* Upload form */
.upload-form {
    background: var(--bg-card);
    border: 2px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 28px;
    box-shadow: var(--shadow-sm);
}
.form-group {
    margin-bottom: 20px;
}
.form-group label {
    display: block;
    font-size: 14px;
    font-weight: 700;
    color: var(--text);
    margin-bottom: 8px;
}
.form-input {
    width: 100%;
    padding: 12px 16px;
    border: 2px solid var(--border);
    border-radius: 12px;
    font-size: 16px;
    transition: all 0.2s;
    background: #FAFAFE;
}
.form-input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 4px rgba(79, 70, 229, 0.12);
    background: #fff;
}
.form-textarea {
    width: 100%;
    padding: 10px 14px;
    border: 2px solid var(--border);
    border-radius: 10px;
    font-size: 14px;
    font-family: inherit;
    resize: vertical;
    transition: all 0.2s;
    background: #FAFAFE;
}
.form-textarea:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 4px rgba(79, 70, 229, 0.12);
    background: #fff;
}
.form-hint {
    background: var(--hint-1);
    border: 1px solid #A5B4FC;
    border-radius: var(--radius);
    padding: 14px 18px;
    margin-bottom: 20px;
    font-size: 13px;
    color: var(--text-dim);
    line-height: 1.8;
}
.form-hint p { font-weight: 700; margin-bottom: 6px; }
.form-hint ul { padding-left: 20px; }
.form-hint li { margin-bottom: 4px; }
.form-actions {
    display: flex;
    gap: 12px;
    align-items: center;
}
.text-muted { color: var(--text-muted); font-size: 13px; }
.td-content {
    max-width: 200px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    color: var(--text-dim);
    font-size: 13px;
}

/* Preview page */
.preview-meta {
    font-size: 15px;
    color: var(--text-dim);
    margin-bottom: 20px;
    background: var(--bg-card);
    padding: 12px 18px;
    border-radius: var(--radius);
    border: 2px solid var(--border);
}
.preview-list {
    display: flex;
    flex-direction: column;
    gap: 14px;
    margin-bottom: 24px;
}
.preview-content {
    font-size: 15px;
    line-height: 1.8;
}
.preview-content p { margin-bottom: 8px; }
.q-text {
    white-space: pre-wrap;
    background: var(--bg);
    padding: 12px 16px;
    border-radius: 10px;
    margin: 8px 0;
    line-height: 1.8;
}
.q-options {
    margin: 8px 0;
    padding-left: 12px;
}
.option-row {
    padding: 4px 0;
    font-size: 14px;
}
.preview-answer {
    color: var(--primary);
    font-size: 15px;
}
.preview-missing {
    color: var(--danger) !important;
}
.preview-explanation {
    color: var(--text-dim);
    font-size: 14px;
}
.preview-hint-note {
    font-size: 12px;
    color: var(--text-muted);
    font-style: italic;
}
.q-type-tag.calc { background: #FDF2F8; color: #9D174D; }

/* ===================== */
/* Dashboard Stats       */
/* ===================== */
.stats-cards {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
    gap: 14px;
    margin-bottom: 24px;
}
.stat-card {
    background: var(--bg-card);
    border: 2px solid var(--border);
    border-radius: var(--radius);
    padding: 20px;
    text-align: center;
    box-shadow: var(--shadow-sm);
    transition: all 0.2s;
}
.stat-card:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-md);
}
.stat-card-sm { padding: 14px; }
.stat-number {
    font-size: 36px;
    font-weight: 800;
    color: var(--primary);
    line-height: 1.2;
}
.stat-card-sm .stat-number { font-size: 28px; }
.stat-label {
    font-size: 13px;
    font-weight: 600;
    color: var(--text-dim);
    margin-top: 4px;
}
.stat-pct {
    font-size: 12px;
    font-weight: 700;
    color: var(--text-muted);
    margin-top: 2px;
}

/* Accuracy bar (block) */
.accuracy-bar {
    height: 8px;
    background: var(--bg);
    border-radius: 4px;
    overflow: hidden;
    margin-top: 10px;
}
.accuracy-fill {
    height: 100%;
    border-radius: 4px;
    transition: width 0.5s ease-out;
    min-width: 0;
}

/* Accuracy bar (inline in table) */
.accuracy-bar-inline {
    display: inline-block;
    width: 60px;
    height: 8px;
    background: var(--bg);
    border-radius: 4px;
    overflow: hidden;
    vertical-align: middle;
    margin-right: 6px;
}
.accuracy-text {
    font-size: 13px;
    font-weight: 700;
    vertical-align: middle;
}

/* ===================== */
/* Responsive (tables)   */
/* ===================== */
@media (max-width: 640px) {
    .stats-cards {
        grid-template-columns: repeat(2, 1fr);
        gap: 10px;
    }
    .stat-number { font-size: 28px; }
    .stat-card-sm .stat-number { font-size: 22px; }
    .stat-card { padding: 14px; }
    .data-table { font-size: 13px; }
    .data-table th,
    .data-table td { padding: 8px 10px; }
    .td-content { max-width: 120px; }
    .accuracy-bar-inline { width: 40px; }
    .data-table { overflow-x: auto; display: block; }
    .teacher-actions { flex-direction: column; }
    .btn-action { width: 100%; }
}

/* ── Exam Grid ─────────────────────────────────────────────────────── */
.exam-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: 16px;
}

.exam-card {
    display: block;
    background: var(--bg-card);
    border: 2px solid var(--border);
    border-radius: var(--radius);
    padding: 20px;
    text-decoration: none;
    color: var(--text);
    transition: all 0.2s;
    box-shadow: var(--shadow-sm);
}
.exam-card:hover {
    border-color: var(--primary);
    box-shadow: var(--shadow-md);
    transform: translateY(-2px);
}
.exam-card.exam-done { border-color: var(--success); }
.exam-card.exam-started { border-color: var(--primary-light); }

.exam-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 8px;
}
.exam-header h3 {
    font-size: 17px;
    margin: 0;
}

.timer-badge {
    background: var(--warning-bg);
    color: var(--warning);
    font-size: 12px;
    padding: 2px 8px;
    border-radius: 12px;
    font-weight: 600;
}

.exam-meta {
    display: flex;
    gap: 12px;
    color: var(--text-muted);
    font-size: 14px;
    margin-bottom: 12px;
}

.exam-progress {
    margin-top: 8px;
}

.progress-text-sm {
    font-size: 13px;
    color: var(--text-dim);
    margin-top: 4px;
}

/* ── Exam Header Bar ─────────────────────────────────────────────── */
.exam-header-bar {
    margin-bottom: 20px;
}
.exam-header-bar h1 {
    font-size: 22px;
    margin: 8px 0 4px;
}
.exam-info {
    display: flex;
    gap: 16px;
    color: var(--text-muted);
    font-size: 14px;
    align-items: center;
}
.timer-display {
    background: var(--primary);
    color: white;
    padding: 4px 12px;
    border-radius: 8px;
    font-weight: 600;
    font-size: 15px;
}

/* ── Exam Complete ────────────────────────────────────────────────── */
.exam-complete {
    text-align: center;
    padding: 32px 0;
}
.complete-icon {
    font-size: 64px;
    margin: 16px 0;
}
.exam-complete h2 {
    margin-bottom: 24px;
    color: var(--primary);
}
.complete-actions {
    display: flex;
    gap: 12px;
    justify-content: center;
    margin-top: 24px;
    flex-wrap: wrap;
}

/* ── Exam Settings Form ──────────────────────────────────────────── */
.exam-settings-form {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 20px;
    margin-bottom: 24px;
}
.exam-settings-form .form-group {
    margin-bottom: 16px;
}
.exam-settings-form label {
    display: block;
    font-weight: 600;
    margin-bottom: 6px;
    color: var(--text);
}
.exam-settings-form input[type="text"],
.exam-settings-form input[type="number"],
.exam-settings-form select {
    width: 100%;
    max-width: 400px;
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 15px;
}
.exam-settings-form input[type="checkbox"] {
    margin-right: 8px;
}

.back-link {
    color: var(--primary);
    text-decoration: none;
    font-size: 14px;
}
.back-link:hover { text-decoration: underline; }

/* ── Exam Intro ──────────────────────────────────────────────────── */
.exam-intro { max-width: 600px; margin: 0 auto; }
.exam-intro-card {
    background: var(--bg-card);
    border: 2px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 32px;
    text-align: center;
    box-shadow: var(--shadow-md);
    margin-top: 16px;
}
.exam-intro-card h1 { font-size: 24px; margin-bottom: 20px; }
.exam-intro-meta {
    display: flex;
    justify-content: center;
    gap: 32px;
    margin-bottom: 24px;
}
.meta-item { text-align: center; }
.meta-label { display: block; font-size: 13px; color: var(--text-muted); }
.meta-value { display: block; font-size: 20px; font-weight: 700; color: var(--primary); }
.exam-intro-rules {
    text-align: left;
    background: var(--primary-bg);
    border-radius: var(--radius);
    padding: 16px 20px;
    margin-bottom: 24px;
}
.exam-intro-rules h3 { font-size: 15px; margin-bottom: 8px; color: var(--primary); }
.exam-intro-rules ul { padding-left: 20px; font-size: 14px; color: var(--text-dim); }
.exam-intro-rules li { margin-bottom: 4px; }
.exam-intro-actions { margin-top: 20px; }
.completed-summary { margin-bottom: 12px; color: var(--text-dim); }

/* ── Exam Flow (Sequential) ─────────────────────────────────────── */
.exam-flow { max-width: 800px; margin: 0 auto; }
.exam-progress-bar { margin-bottom: 16px; }
.exam-progress-info {
    display: flex;
    justify-content: space-between;
    font-size: 14px;
    color: var(--text-dim);
    margin-bottom: 6px;
}
.review-link {
    display: inline-block;
    margin-top: 6px;
    font-size: 13px;
    color: var(--primary);
    text-decoration: none;
}
.review-link:hover { text-decoration: underline; }

.q-type-inline {
    font-size: 13px;
    background: var(--primary-bg);
    color: var(--primary);
    padding: 2px 8px;
    border-radius: 6px;
    margin-left: 8px;
    font-weight: 600;
}

/* ── Review Mode ─────────────────────────────────────────────────── */
.review-nav {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;
    gap: 8px;
}
.review-badge {
    font-size: 12px;
    background: var(--warning-bg);
    color: var(--warning);
    padding: 2px 8px;
    border-radius: 6px;
    margin-left: 8px;
}
.review-mode { opacity: 0.95; }
.review-answer-block {
    background: var(--bg);
    border-radius: var(--radius);
    padding: 16px;
    margin: 16px 0;
}
.review-row { margin-bottom: 8px; }
.review-label { font-weight: 600; margin-right: 8px; }
.correct-text { color: var(--success); font-weight: 700; }
.wrong-text { color: var(--danger); font-weight: 700; }

/* ── Score Circle ────────────────────────────────────────────────── */
.score-circle {
    width: 160px;
    height: 160px;
    border-radius: 50%;
    background: var(--primary-bg);
    border: 4px solid var(--primary);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    margin: 24px auto;
}
.score-number { font-size: 42px; font-weight: 800; color: var(--primary); line-height: 1; }
.score-detail { font-size: 13px; color: var(--text-dim); margin-top: 4px; }

/* ── Results List ────────────────────────────────────────────────── */
.results-list { margin-bottom: 24px; }
.result-item {
    border: 1px solid var(--border);
    border-radius: 8px;
    margin-bottom: 6px;
    overflow: hidden;
}
.result-item-correct { border-left: 4px solid var(--success); }
.result-item-wrong { border-left: 4px solid var(--danger); }
.result-item-header {
    display: flex;
    align-items: center;
    padding: 10px 14px;
    cursor: pointer;
    gap: 8px;
    font-size: 14px;
    background: var(--bg-card);
}
.result-item-header:hover { background: var(--bg-hover); }
.result-icon { font-size: 16px; width: 24px; text-align: center; }
.result-item-correct .result-icon { color: var(--success); }
.result-item-wrong .result-icon { color: var(--danger); }
.result-num { font-weight: 700; color: var(--text-dim); min-width: 36px; }
.result-preview { flex: 1; color: var(--text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.result-hints { color: var(--warning); font-size: 13px; }
.result-toggle { color: var(--text-muted); font-size: 12px; margin-left: auto; }
.result-item-detail {
    padding: 12px 14px;
    background: var(--bg);
    border-top: 1px solid var(--border);
    font-size: 14px;
}
.result-answers { display: flex; gap: 24px; margin-bottom: 8px; }
.result-explanation {
    padding: 12px;
    background: var(--bg-card);
    border-radius: 6px;
    border-left: 3px solid var(--primary);
    margin-top: 8px;
}

/* ── Desktop/Mobile visibility ───────────────────────────────────── */
.mobile-only { display: none; }
.mobile-cards-container { display: none; }
.desktop-only { display: flex; }

@media (max-width: 768px) {
    .mobile-only { display: flex; }
    .desktop-only { display: none !important; }

    body { font-size: 15px; }
    main { padding: 12px; padding-bottom: 80px; }
    main.has-bottom-tab { padding-bottom: 80px; }

    /* Mobile top bar */
    .mobile-topbar {
        background: rgba(255,255,255,0.9);
        backdrop-filter: blur(12px);
        padding: 10px 16px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        border-bottom: 1px solid var(--border);
        position: sticky;
        top: 0;
        z-index: 10;
    }
    .brand-mini { font-size: 16px; font-weight: 800; color: var(--primary); }
    .mobile-user { font-size: 13px; color: var(--text-dim); display: flex; align-items: center; gap: 8px; }
    .btn-logout-mini { color: var(--danger); text-decoration: none; font-size: 12px; }

    /* Bottom tab bar */
    .bottom-tab {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        background: rgba(255,255,255,0.95);
        backdrop-filter: blur(12px);
        border-top: 1px solid var(--border);
        display: flex;
        justify-content: space-around;
        padding: 6px 0 env(safe-area-inset-bottom, 8px);
        z-index: 100;
    }
    .tab-item {
        display: flex;
        flex-direction: column;
        align-items: center;
        text-decoration: none;
        color: var(--text-muted);
        font-size: 11px;
        padding: 4px 12px;
        border-radius: 8px;
        min-width: 60px;
        transition: all 0.2s;
    }
    .tab-item.active {
        color: var(--primary);
        font-weight: 700;
    }
    .tab-svg { width: 22px; height: 22px; }
    .tab-label { margin-top: 2px; }

    /* Cards for mobile tables */
    .mobile-card {
        background: var(--bg-card);
        border: 1px solid var(--border);
        border-radius: var(--radius);
        padding: 14px 16px;
        margin-bottom: 10px;
        box-shadow: var(--shadow-sm);
    }
    .mobile-card-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 8px;
    }
    .mobile-card-name { font-weight: 700; font-size: 16px; }
    .mobile-card-badge { font-size: 12px; padding: 2px 8px; border-radius: 10px; }

    /* General mobile overrides */
    .btn-action, .btn-primary { min-height: 44px; font-size: 15px; }
    .stats-cards { grid-template-columns: repeat(2, 1fr); }
    .data-table { display: none; }
    .mobile-cards-container { display: block !important; }
    .teacher-section h1 { font-size: 22px; }
    .teacher-section h2 { font-size: 18px; }
}

/* ── Dashboard ───────────────────────────────────────────────────── */
.dashboard-welcome {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 24px;
    flex-wrap: wrap;
    gap: 12px;
}
.dashboard-welcome h1 { font-size: 22px; margin: 0; }

.exam-dashboard-card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 20px;
    margin-bottom: 16px;
    box-shadow: var(--shadow-sm);
}
.exam-dash-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;
    flex-wrap: wrap;
    gap: 8px;
}
.exam-dash-header h2 { margin: 0; font-size: 18px; }
.exam-dash-header a { color: var(--primary); text-decoration: none; }
.exam-dash-stats { display: flex; gap: 16px; font-size: 14px; color: var(--text-dim); }

/* Stacked bar chart */
.stacked-chart-container { overflow-x: auto; }
.stacked-chart {
    display: flex;
    align-items: flex-end;
    gap: 3px;
    min-height: 80px;
    padding: 8px 0;
}
.stacked-bar {
    display: flex;
    flex-direction: column;
    align-items: center;
    text-decoration: none;
    min-width: 24px;
    flex: 1;
}
.stacked-bar:hover { opacity: 0.8; }
.bar-label { font-size: 10px; color: var(--text-muted); margin-top: 4px; }
.bar-stack {
    display: flex;
    flex-direction: column-reverse;
    width: 100%;
    border-radius: 3px 3px 0 0;
    overflow: hidden;
}
.bar-seg { width: 100%; min-height: 1px; }

.chart-legend {
    display: flex;
    gap: 12px;
    flex-wrap: wrap;
    margin-top: 10px;
    font-size: 12px;
    color: var(--text-dim);
}
.legend-item { display: flex; align-items: center; gap: 4px; }
.legend-dot { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }

/* Activity list */
.activity-list { font-size: 14px; }
.activity-item {
    display: flex;
    gap: 8px;
    padding: 8px 0;
    border-bottom: 1px solid var(--border);
    align-items: baseline;
}
.activity-user { font-weight: 600; color: var(--primary); min-width: 60px; }
.activity-text { flex: 1; color: var(--text-dim); }
.activity-time { font-size: 12px; color: var(--text-muted); white-space: nowrap; }

@media (max-width: 768px) {
    .dashboard-welcome { flex-direction: column; align-items: flex-start; }
    .dashboard-welcome h1 { font-size: 18px; }
    .exam-dash-header { flex-direction: column; align-items: flex-start; }
    .stacked-bar { min-width: 16px; }
    .bar-label { font-size: 8px; }
}

/* ── Mastery Components ──────────────────────────────────────────── */
.mastery-tag {
    display: inline-block;
    padding: 3px 10px;
    border-radius: 12px;
    font-size: 12px;
    font-weight: 600;
    white-space: nowrap;
}

.mastery-dist-chart { margin: 16px 0 24px; }
.mastery-bar-row {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 8px;
}
.mastery-bar-label { font-size: 13px; font-weight: 600; min-width: 160px; text-align: right; }
.mastery-bar-track {
    flex: 1;
    height: 22px;
    background: var(--bg);
    border-radius: 6px;
    overflow: hidden;
}
.mastery-bar-fill { height: 100%; border-radius: 6px; transition: width 0.3s; min-width: 2px; }
.mastery-bar-count { font-size: 13px; color: var(--text-dim); min-width: 50px; }

.question-preview-box {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
    margin-bottom: 20px;
    box-shadow: var(--shadow-sm);
}

@media (max-width: 768px) {
    .mastery-bar-label { min-width: auto; font-size: 11px; text-align: left; }
    .mastery-bar-row { flex-wrap: wrap; }
}

/* ── Format Hint ─────────────────────────────────────────────────── */
.format-hint {
    background: #EFF6FF;
    color: #2563EB;
    border: 1px solid #BFDBFE;
    border-radius: 8px;
    padding: 8px 14px;
    font-size: 14px;
    font-weight: 600;
    margin-bottom: 8px;
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.01ms !important;
        transition-duration: 0.01ms !important;
    }
}
