/* =======================================
   Modern UI — Optical Exam System
   ======================================= */

@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap');

/* ---------- Design Tokens ---------- */
:root {
    /* Primary */
    --primary-50:  #eef2ff;
    --primary-100: #e0e7ff;
    --primary-200: #c7d2fe;
    --primary-400: #818cf8;
    --primary-color: #6366f1;
    --primary-dark: #4f46e5;
    --primary-700: #4338ca;

    /* Secondary */
    --secondary-color: #8b5cf6;

    /* Semantic */
    --success-color: #10b981;
    --success-light: #d1fae5;
    --danger-color: #ef4444;
    --danger-light: #fee2e2;
    --warning-color: #f59e0b;
    --warning-light: #fef3c7;
    --info-color: #0ea5e9;
    --info-light: #e0f2fe;

    /* Neutrals */
    --dark-color: #0f172a;
    --light-color: #f8fafc;
    --gray-50:  #f8fafc;
    --gray-100: #f1f5f9;
    --gray-200: #e2e8f0;
    --gray-300: #cbd5e1;
    --gray-400: #94a3b8;
    --gray-500: #64748b;
    --gray-600: #475569;
    --gray-700: #334155;
    --gray-800: #1e293b;

    /* Surfaces */
    --bg-app: #f0f2f8;
    --card-bg: #ffffff;
    --card-border: rgba(0, 0, 0, 0.06);
    --glass-bg: rgba(255, 255, 255, 0.72);
    --glass-border: rgba(255, 255, 255, 0.3);

    /* Shadows */
    --shadow-xs: 0 1px 2px rgba(0,0,0,0.04);
    --shadow-sm: 0 1px 3px rgba(0,0,0,0.06), 0 1px 2px rgba(0,0,0,0.04);
    --shadow-md: 0 4px 12px rgba(0,0,0,0.07);
    --shadow-lg: 0 12px 32px rgba(0,0,0,0.1);
    --shadow-primary: 0 4px 14px rgba(99, 102, 241, 0.35);

    /* Radii */
    --radius-sm: 10px;
    --radius-md: 14px;
    --radius-lg: 20px;
    --radius-xl: 24px;
    --radius-full: 9999px;

    /* Typography */
    --font-family: 'Inter', system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;

    /* Transitions */
    --transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    --transition-bounce: all 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);

    /* Layout */
    --nav-height: 68px;
    --safe-bottom: env(safe-area-inset-bottom, 0px);
}

/* ---------- Reset & Base ---------- */
*,
*::before,
*::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

.hidden {
    display: none !important;
}

html {
    font-size: 16px;
    -webkit-tap-highlight-color: transparent;
    scroll-behavior: smooth;
    -webkit-text-size-adjust: 100%;
}

body {
    font-family: var(--font-family);
    background: var(--bg-app);
    min-height: 100vh;
    min-height: 100dvh;
    color: var(--dark-color);
    line-height: 1.6;
    letter-spacing: -0.011em;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* ---------- App Container ---------- */
.app-container {
    max-width: 600px;
    margin: 0 auto;
    min-height: 100vh;
    min-height: 100dvh;
    background: var(--bg-app);
    display: flex;
    flex-direction: column;
    position: relative;
}

/* ---------- OpenCV Loading Banner ---------- */
.opencv-loading-banner {
    background: var(--warning-light);
    color: var(--gray-800);
    padding: 10px 16px;
    text-align: center;
    font-size: 0.8rem;
    font-weight: 600;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    border-bottom: 1px solid rgba(245, 158, 11, 0.2);
}

.opencv-loading-banner.ready {
    background: var(--success-light);
    color: #065f46;
    border-bottom-color: rgba(16, 185, 129, 0.2);
}

.opencv-loading-banner.error {
    background: var(--danger-light);
    color: #991b1b;
    border-bottom-color: rgba(239, 68, 68, 0.2);
}

.opencv-loading-banner.hidden {
    display: none;
}

/* ---------- Header ---------- */
.app-header {
    background: linear-gradient(135deg, var(--primary-color) 0%, var(--secondary-color) 100%);
    color: white;
    padding: 18px 20px 16px;
    text-align: center;
    position: relative;
    overflow: hidden;
}

.app-header::before {
    content: '';
    position: absolute;
    inset: 0;
    background: radial-gradient(circle at 30% 50%, rgba(255,255,255,0.12) 0%, transparent 60%);
    pointer-events: none;
}

.app-header h1 {
    font-size: 1.35rem;
    font-weight: 800;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    letter-spacing: -0.03em;
    position: relative;
}

.app-header .subtitle {
    font-size: 0.78rem;
    opacity: 0.85;
    margin-top: 3px;
    font-weight: 500;
    letter-spacing: 0.02em;
    position: relative;
}

/* ---------- Navigation (Bottom Fixed) ---------- */
.main-nav {
    display: flex;
    position: fixed;
    bottom: 0;
    left: 50%;
    transform: translateX(-50%);
    width: 100%;
    max-width: 600px;
    background: var(--glass-bg);
    backdrop-filter: blur(20px) saturate(180%);
    -webkit-backdrop-filter: blur(20px) saturate(180%);
    border-top: 1px solid var(--glass-border);
    box-shadow: 0 -4px 24px rgba(0, 0, 0, 0.06);
    z-index: 100;
    padding-bottom: var(--safe-bottom);
}

.nav-btn {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 8px 4px 10px;
    border: none;
    background: transparent;
    color: var(--gray-400);
    font-size: 0.6rem;
    font-weight: 600;
    cursor: pointer;
    transition: var(--transition);
    gap: 3px;
    position: relative;
    min-height: 56px;
    -webkit-tap-highlight-color: transparent;
}

.nav-btn i {
    font-size: 1.15rem;
    transition: var(--transition);
}

.nav-btn span {
    transition: var(--transition);
    line-height: 1;
}

.nav-btn:hover {
    color: var(--primary-color);
}

.nav-btn.active {
    color: var(--primary-color);
    background: transparent;
    border-bottom: none;
}

.nav-btn.active i {
    transform: scale(1.15);
}

.nav-btn.active::before {
    content: '';
    position: absolute;
    top: 0;
    left: 50%;
    transform: translateX(-50%);
    width: 32px;
    height: 3px;
    background: var(--primary-color);
    border-radius: 0 0 3px 3px;
}

/* ---------- Main Content ---------- */
.main-content {
    flex: 1;
    padding: 16px 16px calc(var(--nav-height) + var(--safe-bottom) + 16px);
    overflow-y: auto;
}

.page {
    display: none;
}

.page.active {
    display: block;
    animation: pageIn 0.35s cubic-bezier(0.25, 0.46, 0.45, 0.94);
}

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

/* ---------- Cards ---------- */
.welcome-card,
.form-card,
.scan-card,
.results-card {
    background: var(--card-bg);
    border-radius: var(--radius-lg);
    padding: 22px;
    box-shadow: var(--shadow-sm);
    border: 1px solid var(--card-border);
}

.welcome-card h2,
.form-card h2,
.scan-card h2,
.results-card h2 {
    font-size: 1.15rem;
    font-weight: 700;
    color: var(--dark-color);
    margin-bottom: 18px;
    display: flex;
    align-items: center;
    gap: 10px;
    letter-spacing: -0.02em;
}

.welcome-card h2 i,
.form-card h2 i,
.scan-card h2 i,
.results-card h2 i {
    color: var(--primary-color);
    font-size: 1.1rem;
}

/* ---------- Exam List ---------- */
.exam-list {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.exam-item {
    background: var(--gray-50);
    border-radius: var(--radius-md);
    padding: 14px 16px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    transition: var(--transition);
    border: 1px solid transparent;
}

.exam-item:hover {
    background: var(--primary-50);
    border-color: var(--primary-200);
    transform: translateY(-1px);
    box-shadow: var(--shadow-xs);
}

.exam-item-info h3 {
    font-size: 0.95rem;
    font-weight: 600;
    color: var(--dark-color);
    margin-bottom: 3px;
}

.exam-item-info p {
    font-size: 0.78rem;
    color: var(--gray-500);
}

.exam-item-actions {
    display: flex;
    gap: 6px;
}

.exam-item-actions button {
    width: 36px;
    height: 36px;
    border-radius: var(--radius-sm);
    border: none;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: var(--transition);
    font-size: 0.8rem;
}

.exam-item-actions button:active {
    transform: scale(0.92);
}

.btn-icon-primary {
    background: var(--primary-50);
    color: var(--primary-color);
}

.btn-icon-primary:hover {
    background: var(--primary-100);
}

.btn-icon-secondary {
    background: var(--gray-100);
    color: var(--gray-600);
}

.btn-icon-danger {
    background: var(--danger-light);
    color: var(--danger-color);
}

.btn-icon-danger:hover {
    background: #fecaca;
}

.btn-icon-success {
    background: var(--success-light);
    color: var(--success-color);
}

.btn-icon-success:hover {
    background: #a7f3d0;
}

.empty-state {
    text-align: center;
    padding: 48px 24px;
    color: var(--gray-500);
}

.empty-state i {
    font-size: 3rem;
    margin-bottom: 16px;
    opacity: 0.3;
    color: var(--gray-400);
}

.empty-state p {
    margin-bottom: 20px;
    font-size: 0.9rem;
}

/* ---------- Forms ---------- */
.exam-form {
    display: flex;
    flex-direction: column;
    gap: 18px;
}

.form-group {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.form-group label {
    font-size: 0.82rem;
    font-weight: 600;
    color: var(--gray-600);
    display: flex;
    align-items: center;
    gap: 6px;
}

.form-group label i {
    color: var(--gray-400);
    font-size: 0.85rem;
}

.form-group input,
.form-group select {
    padding: 12px 14px;
    border: 1.5px solid var(--gray-200);
    border-radius: var(--radius-sm);
    font-size: 0.95rem;
    font-family: var(--font-family);
    transition: var(--transition);
    background: var(--card-bg);
    color: var(--dark-color);
    appearance: none;
    -webkit-appearance: none;
}

.form-group select {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%2364748b' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 14px center;
    padding-right: 36px;
}

.form-group input:focus,
.form-group select:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px var(--primary-100);
}

.form-group input::placeholder {
    color: var(--gray-400);
}

.wrong-penalty-input {
    display: flex;
    align-items: center;
    gap: 10px;
}

.wrong-penalty-input input {
    width: 80px;
    text-align: center;
}

.wrong-penalty-input .penalty-hint {
    font-size: 0.8rem;
    color: var(--gray-500);
}

.form-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
}

.checkbox-group,
.radio-group {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
}

.checkbox-label,
.radio-label {
    display: flex;
    align-items: center;
    gap: 8px;
    cursor: pointer;
    padding: 10px 16px;
    background: var(--gray-50);
    border-radius: var(--radius-sm);
    transition: var(--transition);
    border: 1.5px solid transparent;
    font-weight: 500;
    font-size: 0.9rem;
}

.checkbox-label:hover,
.radio-label:hover {
    background: var(--primary-50);
    border-color: var(--primary-200);
}

.checkbox-label input,
.radio-label input {
    width: 18px;
    height: 18px;
    accent-color: var(--primary-color);
}

.form-hint {
    display: block;
    font-size: 0.75rem;
    color: var(--gray-500);
    margin-top: 4px;
}

/* ---------- Buttons ---------- */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 12px 22px;
    border: none;
    border-radius: var(--radius-sm);
    font-size: 0.92rem;
    font-weight: 600;
    font-family: var(--font-family);
    cursor: pointer;
    transition: var(--transition);
    letter-spacing: -0.01em;
    -webkit-tap-highlight-color: transparent;
}

.btn:active:not(:disabled) {
    transform: scale(0.97);
}

.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.btn-primary {
    background: linear-gradient(135deg, var(--primary-color), var(--primary-dark));
    color: white;
    box-shadow: var(--shadow-primary);
}

.btn-primary:hover:not(:disabled) {
    box-shadow: 0 6px 20px rgba(99, 102, 241, 0.4);
    transform: translateY(-1px);
}

.btn-secondary {
    background: var(--gray-100);
    color: var(--gray-700);
}

.btn-secondary:hover:not(:disabled) {
    background: var(--gray-200);
}

.btn-danger {
    background: var(--danger-color);
    color: white;
}

.btn-danger:hover:not(:disabled) {
    background: #dc2626;
}

.btn-success {
    background: var(--success-color);
    color: white;
}

.btn-success:hover:not(:disabled) {
    background: #059669;
}

.btn-info {
    background: var(--info-color);
    color: white;
}

.btn-info:hover:not(:disabled) {
    background: #0284c7;
}

.btn-warning {
    background: var(--warning-color);
    color: #78350f;
}

.btn-warning:hover:not(:disabled) {
    background: #d97706;
}

.btn-block {
    width: 100%;
}

.btn-sm {
    padding: 8px 14px;
    font-size: 0.82rem;
    border-radius: 8px;
}

.button-group {
    display: flex;
    flex-direction: column;
    gap: 10px;
    margin-top: 18px;
}

/* After-save action buttons */
.after-save-actions {
    background: var(--gray-50);
    border: 1px solid var(--gray-200);
    border-radius: var(--radius-md);
    padding: 14px;
    animation: slideUp 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);
}

.after-save-label {
    display: block;
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--gray-500);
    margin-bottom: 10px;
    text-align: center;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.after-save-buttons {
    display: flex;
    gap: 8px;
}

.after-save-buttons .btn {
    flex: 1;
    font-size: 0.78rem;
    padding: 10px 8px;
}

.btn-accent {
    background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
    color: white;
    box-shadow: var(--shadow-primary);
}

.btn-danger-outline {
    background: transparent;
    color: var(--danger-color);
    border: 1.5px solid var(--danger-color);
}

.btn-danger-outline:hover {
    background: var(--danger-color);
    color: white;
}

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

/* ---------- Answer Key Form ---------- */
.exam-info-banner {
    background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
    color: white;
    padding: 14px 18px;
    border-radius: var(--radius-md);
    margin-bottom: 16px;
    font-size: 0.88rem;
    position: relative;
    overflow: hidden;
}

.exam-info-banner::before {
    content: '';
    position: absolute;
    inset: 0;
    background: radial-gradient(circle at 80% 20%, rgba(255,255,255,0.1) 0%, transparent 50%);
    pointer-events: none;
}

.booklet-tabs {
    display: flex;
    gap: 8px;
    margin-bottom: 16px;
    overflow-x: auto;
    padding-bottom: 4px;
    -webkit-overflow-scrolling: touch;
}

.booklet-tab {
    padding: 10px 22px;
    border: 1.5px solid var(--gray-200);
    border-radius: var(--radius-full);
    background: white;
    cursor: pointer;
    font-weight: 600;
    font-size: 0.85rem;
    transition: var(--transition);
    white-space: nowrap;
    font-family: var(--font-family);
}

.booklet-tab:hover {
    border-color: var(--primary-200);
    background: var(--primary-50);
}

.booklet-tab.active {
    border-color: var(--primary-color);
    background: var(--primary-color);
    color: white;
    box-shadow: var(--shadow-primary);
}

.answer-key-form {
    display: flex;
    flex-direction: column;
    gap: 0;
    padding: 0;
    background: var(--card-bg);
    border-radius: var(--radius-md);
    border: 1px solid var(--card-border);
    overflow: hidden;
}

.answer-key-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 12px;
    background: white;
    border-radius: var(--radius-sm);
    border: 1px solid var(--gray-200);
}

.answer-key-item .question-num {
    font-size: 0.9rem;
    font-weight: 700;
    color: var(--dark-color);
    min-width: 30px;
    text-align: center;
}

.answer-key-item .answer-options {
    display: flex;
    gap: 8px;
    flex: 1;
}

.answer-key-item .answer-option {
    flex: 1;
    padding: 10px;
    border: 2px solid var(--gray-200);
    border-radius: var(--radius-sm);
    background: white;
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    transition: var(--transition);
    color: var(--dark-color);
}

.answer-key-item .answer-option:hover {
    border-color: var(--primary-200);
    background: var(--primary-50);
}

.answer-key-item .answer-option.selected {
    background: var(--primary-color);
    color: white;
    border-color: var(--primary-color);
}

/* ---------- Answer Key Grid ---------- */
.answer-key-grid {
    border: 1px solid var(--gray-200);
    border-radius: var(--radius-md);
    overflow: hidden;
    margin-bottom: 16px;
}

.ak-header-row {
    display: flex;
    align-items: center;
    background: linear-gradient(135deg, var(--primary-color), var(--primary-dark));
    color: white;
    padding: 10px 0;
    font-weight: 700;
    font-size: 0.82rem;
}

.ak-header-num {
    min-width: 44px;
    text-align: center;
    flex-shrink: 0;
}

.ak-header-opt {
    flex: 1;
    text-align: center;
}

.ak-row {
    display: flex;
    align-items: center;
    padding: 5px 0;
    border-bottom: 1px solid var(--gray-100);
    transition: background 0.15s;
}

.ak-row:last-child {
    border-bottom: none;
}

.ak-row-even {
    background: var(--gray-50);
}

.ak-row:hover {
    background: var(--primary-50);
}

.ak-num {
    min-width: 44px;
    text-align: center;
    font-weight: 700;
    font-size: 0.85rem;
    color: var(--gray-600);
    flex-shrink: 0;
}

.ak-options {
    display: flex;
    flex: 1;
    gap: 0;
}

.ak-option {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    padding: 6px 4px;
    transition: all 0.15s;
    position: relative;
}

.ak-option input {
    position: absolute;
    opacity: 0;
    width: 0;
    height: 0;
}

.ak-option span {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 34px;
    height: 34px;
    border-radius: 50%;
    border: 2px solid var(--gray-300);
    font-weight: 600;
    font-size: 0.82rem;
    color: var(--gray-500);
    background: white;
    transition: var(--transition-bounce);
}

.ak-option:hover span {
    border-color: var(--primary-400);
    color: var(--primary-color);
    background: var(--primary-50);
}

.ak-option.selected span {
    background: var(--primary-color);
    color: white;
    border-color: var(--primary-color);
    box-shadow: 0 2px 8px rgba(99, 102, 241, 0.35);
    transform: scale(1.08);
}

/* ---------- Answer Sheet Preview ---------- */
.booklet-select-container {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 16px;
}

.booklet-select-container select {
    flex: 1;
    padding: 10px 14px;
    border: 1.5px solid var(--gray-200);
    border-radius: var(--radius-sm);
    font-size: 0.95rem;
    font-family: var(--font-family);
    background: white;
    appearance: none;
    -webkit-appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%2364748b' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 12px center;
    padding-right: 34px;
}

.answer-sheet-preview {
    background: white;
    border: 1px solid var(--gray-200);
    border-radius: var(--radius-md);
    padding: 16px;
    overflow-x: auto;
    text-align: center;
}

.answer-sheet-preview canvas {
    max-width: 100%;
    height: auto;
    border-radius: 4px;
}

/* ---------- Form Generator ---------- */
.section-desc {
    font-size: 0.88rem;
    color: var(--gray-500);
    margin-bottom: 18px;
    line-height: 1.6;
}

.section-desc i {
    color: var(--info-color);
}

.fg-preview-area {
    margin-top: 22px;
    padding-top: 18px;
    border-top: 1px solid var(--gray-200);
}

.fg-preview-area.hidden {
    display: none;
}

.fg-preview-area h3 {
    margin-bottom: 14px;
    font-size: 1rem;
    font-weight: 600;
    color: var(--gray-700);
}

.fg-canvas-wrapper {
    background: white;
    border: 1px solid var(--gray-200);
    border-radius: var(--radius-md);
    padding: 16px;
    overflow-x: auto;
    text-align: center;
    margin-bottom: 16px;
}

.fg-canvas-wrapper canvas {
    max-width: 100%;
    height: auto;
    border-radius: 4px;
}

/* (answer-sheet HTML-based styles kept for print template) */
.answer-sheet {
    margin: 0 auto;
    padding: 10px;
    background: white;
    font-family: Arial, sans-serif;
    border: 2px solid #000;
    box-sizing: border-box;
}

.sheet-horizontal { width: 20cm; }
.sheet-vertical { width: 12cm; }

.sheet-header-top {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-bottom: 8px;
    border-bottom: 2px solid #000;
    margin-bottom: 10px;
}

.exam-title {
    font-size: 0.9rem;
    font-weight: bold;
    margin: 0;
    flex: 1;
}

.header-right {
    display: flex;
    align-items: center;
    gap: 8px;
}

.qr-code-container #qr-code { display: inline-block; }

.qr-code-block {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 12px 0;
    border-top: 1px solid #ddd;
    margin-top: 12px;
}

.qr-code-large { display: inline-block; }
.qr-code-large img { max-width: 100%; height: auto; }

.qr-code-label {
    font-size: 0.65rem;
    font-weight: 500;
    color: #333;
    text-align: center;
    margin: 0;
    letter-spacing: 0.5px;
}

.header-logo {
    max-width: 50px;
    max-height: 50px;
    object-fit: contain;
}

.sheet-body {
    display: flex;
    gap: 10px;
    align-items: stretch;
}

.column {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.column-1 { width: 150px; justify-content: space-between; }

.info-item { display: flex; flex-direction: column; gap: 3px; }
.info-item label { font-size: 0.65rem; font-weight: bold; }
.info-input { border-bottom: 1px solid #000; height: 16px; }
.info-item.signature { height: 48px; display: flex; flex-direction: column; }
.signature-area { border-bottom: 1px solid #000; flex: 1; min-height: 12px; }

.column-2 { width: 180px; }

.booklet-block,
.marking-block,
.number-coding-block {
    border: 1.5px solid #000;
    padding: 6px;
    border-radius: 3px;
}

.block-label {
    font-size: 0.65rem;
    font-weight: bold;
    display: block;
    margin-bottom: 4px;
}

.booklet-options { display: flex; gap: 8px; justify-content: center; }

.booklet-option {
    width: 30px; height: 30px;
    border: 2px solid #000; border-radius: 50%;
    display: flex; align-items: center; justify-content: center;
    font-size: 0.85rem; font-weight: bold;
}

.marking-row { display: flex; align-items: center; gap: 6px; margin-bottom: 4px; }
.marking-row:last-child { margin-bottom: 0; }
.marking-label { font-size: 0.6rem; font-weight: bold; min-width: 50px; }
.marking-examples { display: flex; gap: 4px; }

.bubble-demo {
    width: 18px; height: 18px;
    border: 1.5px solid #000; border-radius: 50%;
    display: flex; align-items: center; justify-content: center;
    font-size: 0.7rem;
}
.bubble-demo.correct { background: #000; color: white; }

.number-manual-boxes { display: flex; gap: 2px; margin-bottom: 3px; }
.manual-box { width: 16px; height: 18px; border: 1.5px solid #000; background: white; }
.number-bubbles { display: flex; gap: 2px; }
.digit-column { display: flex; flex-direction: column; align-items: center; gap: 1px; }

.digit-bubble {
    width: 16px; height: 16px;
    border: 1px solid #000; border-radius: 50%;
    display: flex; align-items: center; justify-content: center;
    font-size: 0.5rem; font-weight: bold;
}

.column-3 { flex: 1; }

.column-title {
    font-size: 0.7rem; font-weight: bold; text-align: center;
    border-bottom: 1.5px solid #000; padding-bottom: 1px; margin: 0 0 1px 0;
}

.answers-list {
    display: flex; flex-direction: column; gap: 2px;
    flex-wrap: wrap; max-height: calc(10 * 60px); height: 450px;
    align-content: flex-start;
}

.answer-row { display: flex; align-items: center; gap: 4px; height: 20px; }
.answer-row .q-num { font-size: 0.6rem; font-weight: bold; min-width: 18px; text-align: right; }
.answer-options { display: flex; gap: 3px; }

.answer-bubble {
    width: 16px; height: 16px;
    border: 1.3px solid #000; border-radius: 50%;
    display: flex; align-items: center; justify-content: center;
    font-size: 0.55rem; font-weight: bold;
}

.answer-bubble.marked-answer { background-color: #4CAF50; border-color: #2E7D32; }
.answer-bubble.marked-answer .marked-indicator { color: white; font-size: 0.7rem; font-weight: bold; }

/* ---------- Scanner ---------- */
.scan-mode-select {
    margin-bottom: 20px;
}

.scan-hint {
    font-size: 0.82rem;
    color: var(--gray-500);
    line-height: 1.5;
    margin-top: 14px;
    padding: 12px;
    background: var(--info-light);
    border-radius: var(--radius-sm);
    border-left: 3px solid var(--info-color);
}

.scan-hint i {
    color: var(--info-color);
}

.divider {
    display: flex;
    align-items: center;
    margin: 16px 0;
    color: var(--gray-400);
}

.divider::before,
.divider::after {
    content: '';
    flex: 1;
    height: 1px;
    background: var(--gray-200);
}

.divider span {
    padding: 0 12px;
    font-size: 0.82rem;
    font-weight: 500;
}

.scanner-container {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: black;
    z-index: 1000;
    display: flex;
    flex-direction: column;
}

.scanner-container.hidden {
    display: none;
}

.scanner-header {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    padding: 16px 20px;
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    color: white;
    display: flex;
    justify-content: space-between;
    align-items: center;
    z-index: 10;
    font-weight: 600;
}

#optical-video {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.marker-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 5;
}

.scanner-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: none;
}

.scanner-frame {
    width: 250px;
    height: 250px;
    border: 3px solid var(--primary-400);
    border-radius: var(--radius-lg);
    box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.5);
}

.close-scanner {
    position: absolute;
    bottom: 30px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 10;
}

.scanner-controls {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    display: flex;
    justify-content: center;
    padding: 24px 20px calc(24px + var(--safe-bottom));
    background: linear-gradient(transparent, rgba(0,0,0,0.6));
    z-index: 10;
}

.capture-btn {
    position: static;
    transform: none;
    z-index: 10;
    padding: 16px 32px;
    font-size: 1rem;
    border-radius: var(--radius-full);
    animation: pulseBtn 2s ease-in-out infinite;
}

@keyframes pulseBtn {
    0%, 100% { box-shadow: 0 0 0 0 rgba(99, 102, 241, 0.5); }
    50% { box-shadow: 0 0 0 12px rgba(99, 102, 241, 0); }
}

.detection-status {
    position: absolute;
    bottom: 100px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 10;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 20px;
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border-radius: var(--radius-full);
    color: white;
    font-size: 0.82rem;
    font-weight: 600;
    white-space: nowrap;
}

.scanner-guide {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 6;
}

.corner-guide {
    position: absolute;
    width: 28px;
    height: 28px;
    border-color: rgba(99, 102, 241, 0.7);
    border-style: solid;
    border-width: 0;
}

.corner-guide.top-left {
    top: 12%;
    left: 8%;
    border-top-width: 3px;
    border-left-width: 3px;
    border-top-left-radius: 6px;
}

.corner-guide.top-right {
    top: 12%;
    right: 8%;
    border-top-width: 3px;
    border-right-width: 3px;
    border-top-right-radius: 6px;
}

.corner-guide.bottom-left {
    bottom: 12%;
    left: 8%;
    border-bottom-width: 3px;
    border-left-width: 3px;
    border-bottom-left-radius: 6px;
}

.corner-guide.bottom-right {
    bottom: 12%;
    right: 8%;
    border-bottom-width: 3px;
    border-right-width: 3px;
    border-bottom-right-radius: 6px;
}

/* ---------- Debug Preview ---------- */
.scan-buttons-row {
    display: flex;
    gap: 10px;
    margin-top: 14px;
}

.scan-buttons-row .btn {
    flex: 1;
}

.file-preview-container {
    margin-top: 16px;
    background: var(--gray-50);
    border-radius: var(--radius-md);
    padding: 16px;
    text-align: center;
    border: 1px solid var(--gray-200);
}

.file-preview-container.hidden {
    display: none;
}

.file-preview-image {
    max-width: 100%;
    max-height: 400px;
    border: 1px solid var(--gray-200);
    border-radius: var(--radius-sm);
    margin-bottom: 14px;
    object-fit: contain;
}

.file-preview-actions {
    display: flex;
    gap: 12px;
    justify-content: center;
}

.debug-preview-container {
    margin-bottom: 16px;
    background: var(--gray-50);
    border-radius: var(--radius-sm);
    padding: 12px;
    text-align: center;
    border: 1px solid var(--gray-200);
}

.debug-preview-container.hidden {
    display: none;
}

.debug-canvas {
    max-width: 100%;
    height: auto;
    border-radius: 6px;
    margin-bottom: 8px;
}

/* ---------- Flagged Questions ---------- */
.flagged-questions {
    background: var(--warning-light);
    border: 1px solid rgba(245, 158, 11, 0.25);
    border-radius: var(--radius-md);
    padding: 14px;
    margin-top: 14px;
}

.flagged-questions.hidden {
    display: none;
}

.flagged-questions h4 {
    font-size: 0.88rem;
    color: #92400e;
    margin-bottom: 10px;
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 700;
}

.flag-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 12px;
    margin-bottom: 6px;
    background: white;
    border-radius: 8px;
    font-size: 0.82rem;
    font-weight: 500;
}

.flag-item:last-child {
    margin-bottom: 0;
}

.flag-item .flag-type {
    font-size: 0.72rem;
    padding: 3px 8px;
    border-radius: var(--radius-full);
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

.flag-type.multi-mark {
    background: var(--danger-light);
    color: var(--danger-color);
}

.flag-type.low-confidence {
    background: var(--warning-light);
    color: #92400e;
}

.flag-type.empty {
    background: var(--gray-100);
    color: var(--gray-600);
}

/* ---------- Result Edit Inputs ---------- */
.result-edit-input,
.result-edit-select {
    padding: 8px 12px;
    border: 1.5px solid var(--gray-200);
    border-radius: var(--radius-sm);
    font-size: 0.92rem;
    font-weight: 600;
    font-family: var(--font-family);
    min-width: 120px;
    text-align: center;
    background: white;
}

.result-edit-input:focus,
.result-edit-select:focus {
    border-color: var(--primary-color);
    outline: none;
    box-shadow: 0 0 0 3px var(--primary-100);
}

/* ---------- Scan Result ---------- */
.scan-result {
    background: var(--card-bg);
    border-radius: var(--radius-lg);
    padding: 22px;
    margin-top: 20px;
    border: 1px solid var(--card-border);
    box-shadow: var(--shadow-sm);
}

.scan-result.hidden {
    display: none;
}

.scan-result h3 {
    display: flex;
    align-items: center;
    gap: 8px;
    color: var(--success-color);
    margin-bottom: 18px;
    font-weight: 700;
}

.result-details {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.result-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 14px;
    background: var(--gray-50);
    border-radius: var(--radius-sm);
}

.result-item .label {
    color: var(--gray-500);
    font-weight: 500;
    font-size: 0.88rem;
}

.result-item .value {
    font-weight: 700;
}

.result-stats {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 10px;
}

.result-stats .stat {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 14px 8px;
    background: var(--gray-50);
    border-radius: var(--radius-md);
    border: 1px solid var(--card-border);
}

.result-stats .stat i {
    font-size: 1.1rem;
    margin-bottom: 6px;
}

.result-stats .stat span {
    font-size: 1.6rem;
    font-weight: 800;
    letter-spacing: -0.03em;
}

.result-stats .stat small {
    font-size: 0.72rem;
    color: var(--gray-500);
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-top: 2px;
}

.result-stats .correct {
    color: var(--success-color);
    background: var(--success-light);
    border-color: rgba(16, 185, 129, 0.15);
}

.result-stats .wrong {
    color: var(--danger-color);
    background: var(--danger-light);
    border-color: rgba(239, 68, 68, 0.15);
}

.result-stats .empty {
    color: var(--gray-500);
}

.result-summary {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
}

.summary-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 14px 16px;
    background: var(--primary-50);
    border-radius: var(--radius-md);
    border: 1px solid var(--primary-100);
}

.summary-item .label {
    font-weight: 600;
    color: var(--gray-600);
    font-size: 0.85rem;
}

.summary-item .highlight {
    font-size: 1.4rem;
    font-weight: 800;
    color: var(--primary-color);
    letter-spacing: -0.03em;
}

/* ---------- Results Page ---------- */
.results-summary {
    margin: 16px 0;
}

.results-summary.hidden {
    display: none;
}

.summary-stats {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 8px;
}

.stat-box {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 14px 8px;
    background: var(--card-bg);
    border-radius: var(--radius-md);
    text-align: center;
    border: 1px solid var(--card-border);
    box-shadow: var(--shadow-xs);
}

.stat-box i {
    font-size: 1.1rem;
    color: var(--primary-color);
    margin-bottom: 6px;
}

.stat-box span {
    font-size: 1.3rem;
    font-weight: 800;
    color: var(--dark-color);
    letter-spacing: -0.03em;
}

.stat-box small {
    font-size: 0.68rem;
    color: var(--gray-500);
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    margin-top: 2px;
}

.results-table-container {
    overflow-x: auto;
    margin: 16px 0;
    border-radius: var(--radius-md);
    border: 1px solid var(--gray-200);
    background: var(--card-bg);
}

.results-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.82rem;
}

.results-table th,
.results-table td {
    padding: 12px 10px;
    text-align: center;
    border-bottom: 1px solid var(--gray-100);
}

.results-table th {
    background: var(--gray-50);
    font-weight: 700;
    color: var(--gray-600);
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    position: sticky;
    top: 0;
}

.results-table tbody tr {
    transition: background 0.15s;
}

.results-table tbody tr:hover {
    background: var(--primary-50);
}

.results-table tbody tr:last-child td {
    border-bottom: none;
}

.results-table .btn-icon-danger {
    width: 30px;
    height: 30px;
    font-size: 0.72rem;
    border-radius: 8px;
}

/* ---------- Toast ---------- */
.toast {
    position: fixed;
    bottom: calc(var(--nav-height) + var(--safe-bottom) + 20px);
    left: 50%;
    transform: translateX(-50%) translateY(20px);
    padding: 12px 24px;
    background: var(--gray-800);
    color: white;
    border-radius: var(--radius-full);
    font-size: 0.85rem;
    font-weight: 600;
    z-index: 2000;
    opacity: 0;
    transition: all 0.35s cubic-bezier(0.25, 0.46, 0.45, 0.94);
    pointer-events: none;
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    box-shadow: var(--shadow-lg);
    max-width: calc(100% - 40px);
    text-align: center;
}

.toast.show {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

.toast.success {
    background: var(--success-color);
}

.toast.error {
    background: var(--danger-color);
}

/* ---------- Loading Overlay ---------- */
.loading-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(255, 255, 255, 0.85);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 3000;
}

.loading-overlay.hidden {
    display: none;
}

.spinner {
    width: 44px;
    height: 44px;
    border: 3px solid var(--gray-200);
    border-top-color: var(--primary-color);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

.loading-overlay p {
    margin-top: 16px;
    color: var(--gray-600);
    font-weight: 600;
    font-size: 0.9rem;
}

/* ---------- Modal ---------- */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(15, 23, 42, 0.5);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    display: flex;
    align-items: flex-end;
    justify-content: center;
    z-index: 2500;
    padding: 0;
}

.modal.hidden {
    display: none;
}

.modal-content {
    background: white;
    border-radius: var(--radius-xl) var(--radius-xl) 0 0;
    width: 100%;
    max-width: 600px;
    max-height: 85vh;
    overflow-y: auto;
    animation: modalSlideUp 0.35s cubic-bezier(0.25, 0.46, 0.45, 0.94);
    padding-bottom: var(--safe-bottom);
}

@keyframes modalSlideUp {
    from {
        opacity: 0;
        transform: translateY(100%);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px 22px 16px;
    border-bottom: 1px solid var(--gray-100);
    position: sticky;
    top: 0;
    background: white;
    border-radius: var(--radius-xl) var(--radius-xl) 0 0;
}

.modal-header::before {
    content: '';
    position: absolute;
    top: 8px;
    left: 50%;
    transform: translateX(-50%);
    width: 36px;
    height: 4px;
    background: var(--gray-300);
    border-radius: 2px;
}

.modal-header h3 {
    font-size: 1.05rem;
    font-weight: 700;
}

.modal-close {
    background: var(--gray-100);
    border: none;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    font-size: 1rem;
    color: var(--gray-500);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: var(--transition);
}

.modal-close:hover {
    background: var(--gray-200);
    color: var(--gray-700);
}

.modal-body {
    padding: 20px 22px;
}

.modal-footer {
    padding: 16px 22px 20px;
    border-top: 1px solid var(--gray-100);
    display: flex;
    gap: 10px;
    justify-content: flex-end;
}

/* ---------- Account Section ---------- */
.account-section {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

/* ---------- Print ---------- */
@media print {
    body {
        background: white;
    }
    
    .app-header,
    .main-nav,
    .button-group,
    .booklet-select-container,
    .opencv-loading-banner {
        display: none !important;
    }
    
    .main-content {
        padding: 0;
    }
    
    .form-card {
        box-shadow: none;
        padding: 0;
        border: none;
    }
    
    .answer-sheet-preview {
        border: none;
        padding: 0;
    }
    
    .answer-sheet-preview canvas {
        max-width: 100%;
        border: none;
    }
}

/* ---------- Responsive ---------- */
@media (max-width: 480px) {
    .form-row {
        grid-template-columns: 1fr;
    }
    
    .summary-stats {
        grid-template-columns: repeat(2, 1fr);
    }
    
    .answers-grid {
        grid-template-columns: repeat(2, 1fr);
    }
    
    .answer-key-form {
        grid-template-columns: repeat(4, 1fr);
    }

    .ak-option span {
        width: 30px;
        height: 30px;
        font-size: 0.78rem;
    }

    .ak-num {
        min-width: 36px;
        font-size: 0.78rem;
    }

    .scan-buttons-row {
        flex-direction: column;
    }

    .after-save-buttons {
        flex-direction: column;
    }

    .result-summary {
        grid-template-columns: 1fr;
    }

    .welcome-card,
    .form-card,
    .scan-card,
    .results-card {
        padding: 18px;
        border-radius: var(--radius-md);
    }
}

@media (max-width: 360px) {
    .nav-btn span {
        display: none;
    }

    .nav-btn {
        padding: 10px 4px 12px;
    }

    .nav-btn i {
        font-size: 1.3rem;
    }

    .main-content {
        padding: 12px 12px calc(var(--nav-height) + var(--safe-bottom) + 12px);
    }

    .app-header h1 {
        font-size: 1.15rem;
    }

    .app-header .subtitle {
        font-size: 0.7rem;
    }

    .stat-box span {
        font-size: 1.1rem;
    }

    .stat-box small {
        font-size: 0.6rem;
    }
}
