/* ===== QUESTION CAFE STYLES ===== */
        .screen { display: none; }
        /* Opacity only — a transform here would become the containing block for
           the fixed action bars and knock them out of place mid-animation. */
        .screen.active { display: block; animation: fadeIn 0.35s cubic-bezier(0.4, 0, 0.2, 1); }

        @keyframes fadeIn {
            from { opacity: 0; }
            to { opacity: 1; }
        }

        .subject-hero {
            text-align: center;
            padding: 2.5rem 0 1.5rem;
        }
        .subject-hero .hero-logo {
            width: 100px;
            height: auto;
            margin-bottom: 1.25rem;
            filter: drop-shadow(0 0 16px var(--accent-glow));
            animation: floatLogo 3s ease-in-out infinite;
        }
        @keyframes floatLogo {
            0%, 100% { transform: translateY(0) rotate(0deg); }
            25% { transform: translateY(-6px) rotate(1deg); }
            75% { transform: translateY(-3px) rotate(-1deg); }
        }
        .subject-hero h1 {
            font-size: 1.9rem;
            font-weight: 800;
            color: var(--text-primary);
            margin-bottom: 0.4rem;
            letter-spacing: -0.5px;
        }
        .subject-hero p {
            font-size: 0.9rem;
            color: var(--text-muted);
            margin-bottom: 0.5rem;
        }

        .subject-grid {
            display: flex;
            flex-direction: column;
            gap: 0.75rem;
            margin-top: 1.5rem;
            padding-bottom: 2rem;
        }
        .subject-rect {
            display: flex;
            align-items: center;
            gap: 1.25rem;
            padding: 1.25rem 1.5rem;
            background: linear-gradient(135deg, var(--bg-card), rgba(255,255,255,0.02));
            border: 1.5px solid var(--border);
            border-radius: var(--radius-lg);
            cursor: pointer;
            transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
            position: relative;
            overflow: hidden;
            backdrop-filter: blur(10px);
        }
        .subject-rect::before {
            content: '';
            position: absolute;
            top: 0;
            left: -100%;
            width: 100%;
            height: 100%;
            background: linear-gradient(90deg, transparent, rgba(255,255,255,0.06), transparent);
            transition: left 0.7s ease;
        }
        .subject-rect:hover::before { left: 100%; }
        .subject-rect:hover {
            border-color: var(--accent);
            transform: translateX(8px) scale(1.01);
            box-shadow: var(--shadow-glow), 0 8px 32px rgba(0,0,0,0.2);
        }
        .subject-rect .rect-glow {
            position: absolute;
            left: 0;
            top: 0;
            bottom: 0;
            width: 4px;
            background: var(--accent);
            opacity: 0;
            transition: opacity 0.3s ease;
            border-radius: var(--radius-lg) 0 0 var(--radius-lg);
        }
        .subject-rect:hover .rect-glow {
            opacity: 1;
            box-shadow: 0 0 20px var(--accent-glow);
        }
        .subject-rect .rect-emoji {
            font-size: 2.2rem;
            width: 56px;
            height: 56px;
            display: flex;
            align-items: center;
            justify-content: center;
            background: var(--bg-primary);
            border: 1.5px solid var(--border);
            border-radius: 16px;
            flex-shrink: 0;
            transition: all 0.3s ease;
            z-index: 1;
        }
        .subject-rect:hover .rect-emoji {
            background: linear-gradient(135deg, var(--accent), var(--accent-light));
            border-color: var(--accent);
            transform: scale(1.1) rotate(-6deg);
            box-shadow: 0 0 20px var(--accent-glow);
        }
        .subject-rect .rect-info { flex: 1; z-index: 1; }
        .subject-rect .rect-name {
            font-size: 1.05rem;
            font-weight: 700;
            color: var(--text-primary);
            margin-bottom: 0.2rem;
            transition: color 0.2s;
        }
        .subject-rect:hover .rect-name { color: var(--accent); }
        .subject-rect .rect-meta {
            font-size: 0.8rem;
            color: var(--text-muted);
            display: flex;
            align-items: center;
            gap: 0.5rem;
        }
        .subject-rect .rect-meta .dot {
            width: 6px;
            height: 6px;
            background: var(--accent);
            border-radius: 50%;
            opacity: 0.6;
        }
        .subject-rect .rect-arrow {
            font-size: 1.3rem;
            color: var(--text-muted);
            transition: all 0.3s ease;
            opacity: 0.5;
            z-index: 1;
        }
        .subject-rect:hover .rect-arrow {
            color: var(--accent);
            transform: translateX(6px);
            opacity: 1;
        }

        .chapter-list {
            display: flex;
            flex-direction: column;
            gap: 0.75rem;
            margin-top: 1.5rem;
            padding-bottom: 2rem;
        }
        .chapter-row {
            display: flex;
            align-items: center;
            gap: 1rem;
            padding: 1rem 1.25rem;
            background: var(--bg-card);
            border: 1.5px solid var(--border);
            border-radius: var(--radius-md);
            cursor: pointer;
            transition: all 0.35s cubic-bezier(0.4, 0, 0.2, 1);
            position: relative;
            overflow: hidden;
        }
        .chapter-row::before {
            content: '';
            position: absolute;
            top: 0;
            left: -100%;
            width: 100%;
            height: 100%;
            background: linear-gradient(90deg, transparent, rgba(255,255,255,0.05), transparent);
            transition: left 0.6s ease;
        }
        .chapter-row:hover::before { left: 100%; }
        .chapter-row:hover {
            border-color: var(--accent);
            padding-left: 1.75rem;
            transform: translateX(6px);
            box-shadow: 0 4px 24px rgba(0,0,0,0.15);
        }
        .chapter-row:hover .chapter-title { color: var(--accent); }
        .chapter-icon { 
            font-size: 1.6rem; 
            width: 48px;
            height: 48px;
            display: flex;
            align-items: center;
            justify-content: center;
            background: var(--bg-primary);
            border: 1.5px solid var(--border);
            border-radius: 14px;
            flex-shrink: 0;
            transition: all 0.3s ease;
        }
        .chapter-row:hover .chapter-icon {
            background: linear-gradient(135deg, var(--accent), var(--accent-light));
            border-color: var(--accent);
            transform: scale(1.08) rotate(-4deg);
            box-shadow: 0 0 15px var(--accent-glow);
        }
        .chapter-info { flex: 1; }
        .chapter-title {
            font-size: 0.95rem;
            font-weight: 700;
            color: var(--text-primary);
            transition: color 0.2s;
        }
        .chapter-meta {
            font-size: 0.78rem;
            color: var(--text-muted);
            margin-top: 0.15rem;
        }
        .chapter-arrow {
            color: var(--text-muted);
            font-size: 1.1rem;
            transition: all 0.3s ease;
            opacity: 0.5;
        }
        .chapter-row:hover .chapter-arrow {
            color: var(--accent);
            transform: translateX(6px);
            opacity: 1;
        }

        /* ===== MODE SELECTION ===== */
        .mode-selection {
            text-align: center;
            padding: 2rem 0;
        }
        .mode-selection h2 {
            font-size: 1.4rem;
            font-weight: 800;
            color: var(--text-primary);
            margin-bottom: 0.5rem;
        }
        .mode-selection p {
            font-size: 0.9rem;
            color: var(--text-muted);
            margin-bottom: 2rem;
        }
        .mode-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
            gap: 1rem;
            margin-bottom: 2rem;
        }
        .mode-card {
            background: linear-gradient(135deg, var(--bg-card), rgba(255,255,255,0.02));
            border: 1.5px solid var(--border);
            border-radius: var(--radius-lg);
            padding: 1.75rem 1.5rem;
            cursor: pointer;
            transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
            text-align: center;
            position: relative;
            overflow: hidden;
        }
        .mode-card::before {
            content: '';
            position: absolute;
            top: 0;
            left: -100%;
            width: 100%;
            height: 100%;
            background: linear-gradient(90deg, transparent, rgba(255,255,255,0.06), transparent);
            transition: left 0.7s ease;
        }
        .mode-card:hover::before { left: 100%; }
        .mode-card:hover {
            border-color: var(--accent);
            transform: translateY(-6px);
            box-shadow: var(--shadow-glow), 0 12px 40px rgba(0,0,0,0.2);
        }
        .mode-card .mode-icon {
            font-size: 2.5rem;
            margin-bottom: 1rem;
            transition: transform 0.3s ease;
        }
        .mode-card:hover .mode-icon { transform: scale(1.15); }
        .mode-card .mode-name {
            font-size: 1.1rem;
            font-weight: 700;
            color: var(--text-primary);
            margin-bottom: 0.5rem;
        }
        .mode-card .mode-desc {
            font-size: 0.82rem;
            color: var(--text-muted);
            line-height: 1.6;
        }
        .mode-card .mode-badge {
            display: inline-block;
            margin-top: 0.75rem;
            padding: 0.25rem 0.75rem;
            background: rgba(0,212,170,0.1);
            border: 1px solid var(--accent);
            border-radius: 50px;
            font-size: 0.7rem;
            color: var(--accent);
            font-weight: 700;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }

        /* ===== TEST CONFIG ===== */
        .test-config {
            background: linear-gradient(135deg, var(--bg-card), rgba(255,255,255,0.02));
            border: 1.5px solid var(--border);
            border-radius: var(--radius-lg);
            padding: 1.75rem;
            margin-bottom: 1.5rem;
        }
        .test-config h3 {
            font-size: 1rem;
            font-weight: 700;
            color: var(--text-primary);
            margin-bottom: 1.25rem;
        }
        /* The page itself scrolls; the action bar is pinned to the viewport,
           so the list must NOT have its own inner scroll area. */
        .topic-select-list {
            display: flex;
            flex-direction: column;
            gap: 0.5rem;
            margin-bottom: 0.5rem;
        }
        .topic-select-item {
            display: flex;
            align-items: center;
            gap: 0.75rem;
            padding: 0.75rem 1rem;
            background: var(--bg-primary);
            border: 1.5px solid var(--border);
            border-radius: var(--radius-sm);
            cursor: pointer;
            transition: all 0.2s ease;
        }
        .topic-select-item:hover {
            border-color: var(--accent);
            background: var(--bg-hover);
        }
        .topic-select-item.selected {
            border-color: var(--accent);
            background: rgba(0, 212, 170, 0.08);
        }
        .topic-select-item input[type="checkbox"] {
            width: 18px;
            height: 18px;
            accent-color: var(--accent);
            cursor: pointer;
        }
        .topic-select-item label {
            flex: 1;
            font-size: 0.9rem;
            color: var(--text-secondary);
            cursor: pointer;
            font-weight: 500;
        }
        .topic-select-item.selected label {
            color: var(--accent);
            font-weight: 600;
        }
        .topic-meta-count {
            font-size: 0.75rem;
            color: var(--text-muted);
            background: var(--bg-card);
            padding: 0.2rem 0.5rem;
            border-radius: 4px;
        }
        .question-count-selector { margin-bottom: 1.5rem; }
        .question-count-selector label {
            display: block;
            font-size: 0.85rem;
            color: var(--text-muted);
            margin-bottom: 0.5rem;
            font-weight: 600;
        }
        .count-slider-wrap {
            display: flex;
            align-items: center;
            gap: 1rem;
        }
        .count-slider {
            flex: 1;
            -webkit-appearance: none;
            appearance: none;
            height: 6px;
            background: var(--border);
            border-radius: 3px;
            outline: none;
        }
        .count-slider::-webkit-slider-thumb {
            -webkit-appearance: none;
            appearance: none;
            width: 20px;
            height: 20px;
            background: var(--accent);
            border-radius: 50%;
            cursor: pointer;
            box-shadow: 0 0 10px var(--accent-glow);
        }
        .count-value {
            font-size: 1.1rem;
            font-weight: 700;
            color: var(--accent);
            min-width: 40px;
            text-align: center;
        }
        .config-actions {
            display: flex;
            gap: 0.75rem;
        }
        .config-actions .btn { flex: 1; }

        /* Action bar pinned to the bottom of the viewport, so Start/Cancel are
           always reachable without scrolling past the whole topic list. */
        .sticky-actions {
            position: fixed;
            left: 0;
            right: 0;
            bottom: 0;
            z-index: 500;
            display: flex;
            gap: 0.75rem;
            /* Full-width bar whose contents stay inside the 720px page column. */
            padding: 0.75rem max(1.5rem, calc(50% - 360px)) calc(0.75rem + env(safe-area-inset-bottom));
            background: var(--bg-secondary);
            border-top: 1.5px solid var(--border);
            box-shadow: 0 -6px 24px rgba(0, 0, 0, 0.22);
        }
        .sticky-actions .btn { flex: 1; min-width: 130px; }
        /* Reserve room so the last row is never hidden behind the bar. */
        .screen.has-fixed-bar { padding-bottom: 110px; }

        /* ===== STICKY QUIZ HEAD (header + progress bar move together) ===== */
        .quiz-sticky-head {
            position: sticky;
            top: var(--nav-h, 57px);
            z-index: 100;
            background: var(--bg-primary);
            padding-bottom: 0.5rem;
            margin-bottom: 1.5rem;
        }
        .quiz-sticky-head .quiz-header { margin-bottom: 0.5rem; }
        .quiz-sticky-head .progress-track { margin-bottom: 0; }

        /* ===== STUDY HISTORY ===== */
        .history-list {
            display: flex;
            flex-direction: column;
            gap: 0.85rem;
            padding-bottom: 2rem;
        }
        .history-card {
            padding: 1.1rem 1.25rem;
            background: linear-gradient(135deg, var(--bg-card), rgba(255,255,255,0.02));
            border: 1.5px solid var(--border);
            border-left: 4px solid var(--accent);
            border-radius: var(--radius-md);
            cursor: pointer;
            transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        }
        .history-card:hover {
            border-color: var(--accent);
            border-left-color: var(--accent);
            transform: translateX(5px);
            box-shadow: 0 4px 20px rgba(0,0,0,0.16);
        }
        .history-card-top {
            display: flex;
            align-items: center;
            justify-content: space-between;
            gap: 0.5rem;
            margin-bottom: 0.6rem;
        }
        .history-mode {
            font-size: 0.72rem;
            font-weight: 700;
            color: var(--accent);
            background: var(--accent-glow);
            border: 1px solid var(--accent);
            border-radius: 50px;
            padding: 0.2rem 0.65rem;
            white-space: nowrap;
        }
        .history-time {
            font-size: 0.72rem;
            color: var(--text-subtle);
            white-space: nowrap;
        }
        .history-chapter {
            font-size: 1rem;
            font-weight: 700;
            color: var(--text-primary);
            line-height: 1.3;
        }
        .history-file {
            font-size: 0.76rem;
            color: var(--text-muted);
            margin-top: 0.15rem;
        }
        .history-topics {
            display: flex;
            flex-wrap: wrap;
            gap: 0.35rem;
            margin-top: 0.65rem;
        }
        .history-topic-chip {
            font-size: 0.72rem;
            font-weight: 600;
            color: var(--text-secondary);
            background: var(--bg-primary);
            border: 1px solid var(--border);
            border-radius: 6px;
            padding: 0.18rem 0.5rem;
        }
        .history-progress {
            display: flex;
            align-items: center;
            gap: 0.75rem;
            margin-top: 0.85rem;
        }
        .history-progress-track {
            flex: 1;
            height: 5px;
            background: var(--border);
            border-radius: 3px;
            overflow: hidden;
        }
        .history-progress-fill {
            height: 100%;
            background: linear-gradient(90deg, var(--accent-dark), var(--accent));
            border-radius: 3px;
        }
        .history-position {
            font-size: 0.72rem;
            color: var(--text-muted);
            font-weight: 600;
            white-space: nowrap;
        }
        .history-actions {
            display: flex;
            align-items: center;
            gap: 0.5rem;
            margin-top: 0.9rem;
        }
        .history-actions .btn { flex: 1; }
        .history-delete {
            width: 38px;
            height: 38px;
            flex-shrink: 0;
            display: flex;
            align-items: center;
            justify-content: center;
            background: var(--bg-primary);
            border: 1.5px solid var(--border);
            border-radius: var(--radius-sm);
            color: var(--text-muted);
            font-size: 0.95rem;
            cursor: pointer;
            transition: all var(--transition-fast);
        }
        .history-delete:hover {
            border-color: var(--error);
            color: var(--error);
        }

        .history-empty {
            text-align: center;
            padding: 3rem 1.5rem;
            background: var(--bg-card);
            border: 1.5px dashed var(--border);
            border-radius: var(--radius-lg);
        }
        .history-empty-icon { font-size: 2.75rem; margin-bottom: 0.75rem; }
        .history-empty h3 {
            font-size: 1.05rem;
            font-weight: 700;
            color: var(--text-primary);
            margin-bottom: 0.4rem;
        }
        .history-empty p {
            font-size: 0.86rem;
            color: var(--text-muted);
            max-width: 340px;
            margin: 0 auto 1.5rem;
            line-height: 1.6;
        }

        /* ===== QUIZ HEADER ===== */
        .quiz-header {
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding: 0.75rem 1rem;
            margin-bottom: 1rem;
            border: 1px solid var(--border);
            gap: 0.75rem;
            flex-wrap: wrap;
            background: var(--bg-secondary);
            box-shadow: 0 4px 12px rgba(0,0,0,0.15);
            border-radius: var(--radius-md);
        }
        .quiz-back {
            display: flex;
            align-items: center;
            gap: 0.4rem;
            color: var(--text-muted);
            font-size: 0.85rem;
            cursor: pointer;
            transition: all 0.2s;
            padding: 0.4rem 0.75rem;
            border-radius: 8px;
            white-space: nowrap;
        }
        .quiz-back:hover { 
            color: var(--accent); 
            background: var(--bg-hover);
        }
        .grid-toggle-btn {
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 0.3rem;
            padding: 0.5rem 0.875rem;
            background: var(--bg-card);
            border: 1.5px solid var(--border);
            border-radius: var(--radius-md);
            color: var(--text-muted);
            font-size: 0.8rem;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            white-space: nowrap;
        }
        .grid-toggle-btn:hover {
            border-color: var(--accent);
            color: var(--accent);
            box-shadow: 0 0 15px var(--accent-glow);
        }
        .grid-toggle-btn.active {
            border-color: var(--accent);
            color: var(--accent);
            background: rgba(0, 212, 170, 0.08);
        }
        .grid-toggle-btn .grid-icon {
            display: grid;
            grid-template-columns: repeat(2, 1fr);
            gap: 2px;
            width: 14px;
            height: 14px;
        }
        .grid-toggle-btn .grid-icon span {
            background: currentColor;
            border-radius: 1px;
            width: 100%;
            height: 100%;
        }
        .quiz-title-wrap {
            text-align: center;
            flex: 1;
            min-width: 120px;
        }
        .quiz-title {
            font-size: 0.85rem;
            font-weight: 700;
            color: var(--accent);
            text-shadow: 0 0 10px var(--accent-glow);
        }
        .quiz-topic-name {
            font-size: 0.72rem;
            color: var(--text-muted);
            margin-top: 0.15rem;
            font-weight: 500;
        }
        .quiz-progress {
            font-size: 0.8rem;
            color: var(--text-muted);
            font-weight: 600;
            background: var(--bg-card);
            padding: 0.35rem 0.75rem;
            border-radius: 50px;
            border: 1px solid var(--border);
            white-space: nowrap;
        }
        .quiz-timer {
            font-size: 0.85rem;
            font-weight: 700;
            color: var(--accent);
            background: var(--bg-card);
            padding: 0.35rem 0.75rem;
            border-radius: 50px;
            border: 1px solid var(--border);
            white-space: nowrap;
            font-variant-numeric: tabular-nums;
        }
        .quiz-timer.warning {
            color: var(--warning);
            border-color: var(--warning);
            animation: pulse 1s infinite;
        }
        .quiz-timer.danger {
            color: var(--error);
            border-color: var(--error);
            animation: pulse 0.5s infinite;
        }
        @keyframes pulse {
            0%, 100% { opacity: 1; }
            50% { opacity: 0.6; }
        }

        .progress-track {
            width: 100%;
            height: 5px;
            background: var(--border);
            border-radius: 3px;
            margin-bottom: 2rem;
            overflow: hidden;
            box-shadow: inset 0 1px 3px rgba(0,0,0,0.2);
        }
        .progress-fill {
            height: 100%;
            background: linear-gradient(90deg, var(--accent), var(--accent-light), var(--accent));
            background-size: 200% 100%;
            border-radius: 3px;
            transition: width 0.5s cubic-bezier(0.4, 0, 0.2, 1);
            box-shadow: 0 0 12px var(--accent-glow);
            animation: shimmer 2s linear infinite;
        }
        @keyframes shimmer {
            0% { background-position: 200% 0; }
            100% { background-position: -200% 0; }
        }

        /* ===== QUESTION GRID OVERLAY ===== */
        .question-grid-overlay {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0,0,0,0.6);
            backdrop-filter: blur(8px);
            -webkit-backdrop-filter: blur(8px);
            z-index: 1050;
            opacity: 0;
            transition: opacity 0.3s ease;
        }
        .question-grid-overlay.active {
            display: block;
            opacity: 1;
        }
        .question-grid-panel {
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%) scale(0.9);
            width: 90%;
            max-width: 500px;
            max-height: 80vh;
            background: var(--bg-primary);
            border: 1.5px solid var(--border);
            border-radius: var(--radius-lg);
            z-index: 1051;
            display: none;
            flex-direction: column;
            overflow: hidden;
            box-shadow: 0 20px 60px rgba(0,0,0,0.4);
            opacity: 0;
            transition: all 0.35s cubic-bezier(0.4, 0, 0.2, 1);
        }
        .question-grid-panel.active {
            display: flex;
            opacity: 1;
            transform: translate(-50%, -50%) scale(1);
        }
        .grid-panel-header {
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding: 1rem 1.25rem;
            border-bottom: 1px solid var(--border);
            background: var(--bg-card);
        }
        .grid-panel-header h3 {
            font-size: 1rem;
            font-weight: 700;
            color: var(--text-primary);
        }
        .grid-panel-header .grid-close {
            width: 32px;
            height: 32px;
            display: flex;
            align-items: center;
            justify-content: center;
            background: var(--bg-primary);
            border: 1.5px solid var(--border);
            border-radius: 8px;
            color: var(--text-muted);
            font-size: 1.2rem;
            cursor: pointer;
            transition: all 0.2s;
        }
        .grid-panel-header .grid-close:hover {
            border-color: var(--error);
            color: var(--error);
            transform: rotate(90deg);
        }
        .grid-panel-body {
            padding: 1.25rem;
            overflow-y: auto;
            flex: 1;
        }
        .grid-panel-body::-webkit-scrollbar { width: 6px; }
        .grid-panel-body::-webkit-scrollbar-track { background: transparent; }
        .grid-panel-body::-webkit-scrollbar-thumb {
            background: var(--border);
            border-radius: 3px;
        }
        /* Context line: which file / chapter these questions came from */
        .grid-context {
            padding: 0.75rem 1rem;
            margin-bottom: 1rem;
            background: var(--bg-card);
            border: 1px solid var(--border);
            border-left: 3px solid var(--accent);
            border-radius: var(--radius-sm);
        }
        .grid-context .gc-crumb {
            font-size: 0.72rem;
            color: var(--text-muted);
            text-transform: uppercase;
            letter-spacing: 0.8px;
            font-weight: 700;
        }
        .grid-context .gc-chapter {
            font-size: 0.95rem;
            font-weight: 700;
            color: var(--text-primary);
            margin-top: 0.15rem;
        }
        .grid-context .gc-meta {
            font-size: 0.76rem;
            color: var(--text-muted);
            margin-top: 0.2rem;
        }

        /* Legend so the colour coding is self-explanatory */
        .grid-legend {
            display: flex;
            flex-wrap: wrap;
            gap: 0.75rem;
            padding-bottom: 0.9rem;
            margin-bottom: 0.9rem;
            border-bottom: 1px solid var(--border);
        }
        .grid-legend span {
            display: inline-flex;
            align-items: center;
            gap: 0.35rem;
            font-size: 0.72rem;
            color: var(--text-muted);
            font-weight: 600;
        }
        .grid-legend i {
            width: 11px;
            height: 11px;
            border-radius: 3px;
            border: 1.5px solid var(--border);
            background: var(--bg-card);
        }
        .grid-legend i.lg-current { background: var(--accent); border-color: var(--accent); }
        .grid-legend i.lg-answered { background: var(--success-bg); border-color: var(--success); }
        .grid-legend i.lg-reviewed { background: var(--warning-bg); border-color: var(--warning); }
        .grid-legend i.lg-bookmarked { background: rgba(148, 154, 166, 0.3); border-color: var(--text-muted); }
        .grid-legend i.lg-correct { background: rgba(39, 174, 96, 0.3); border-color: var(--success); }
        .grid-legend i.lg-wrong { background: rgba(231, 76, 60, 0.3); border-color: var(--error); }

        .grid-topic-section { margin-bottom: 1.25rem; }
        .grid-topic-section:last-child { margin-bottom: 0; }
        .grid-topic-title {
            display: flex;
            align-items: baseline;
            justify-content: space-between;
            gap: 0.5rem;
            font-size: 0.78rem;
            font-weight: 700;
            color: var(--accent);
            text-transform: uppercase;
            letter-spacing: 1px;
            margin-bottom: 0.6rem;
            padding: 0 0.25rem 0.4rem;
            border-bottom: 1px dashed var(--border);
        }
        .grid-topic-title .gt-count {
            font-size: 0.7rem;
            font-weight: 600;
            color: var(--text-muted);
            letter-spacing: 0;
            text-transform: none;
            white-space: nowrap;
        }
        .grid-num.reviewed {
            border-color: var(--warning);
            color: var(--warning);
            background: var(--warning-bg);
        }
        .grid-nums {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(44px, 1fr));
            gap: 0.5rem;
        }
        .grid-num {
            aspect-ratio: 1;
            display: flex;
            align-items: center;
            justify-content: center;
            background: var(--bg-card);
            border: 1.5px solid var(--border);
            border-radius: 10px;
            color: var(--text-secondary);
            font-size: 0.85rem;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.25s ease;
        }
        .grid-num:hover {
            border-color: var(--accent);
            color: var(--accent);
            transform: scale(1.1);
            box-shadow: 0 0 12px var(--accent-glow);
        }
        .grid-num.current {
            background: linear-gradient(135deg, var(--accent), var(--accent-light));
            border-color: var(--accent);
            color: var(--bg-primary);
            box-shadow: 0 0 15px var(--accent-glow);
            font-weight: 700;
        }
        .grid-num.answered {
            border-color: var(--success);
            color: var(--success);
            background: rgba(39, 174, 96, 0.08);
        }
        .grid-num.revealed {
            border-color: var(--warning);
            color: var(--warning);
            background: rgba(243, 156, 18, 0.08);
        }

        /* Bookmarked / correct / wrong — layered after .answered/.revealed so they
           take priority when a question is both "done" and gradeable. Bookmarked +
           graded shows a diagonal split so neither state is lost. */
        .grid-num.bookmarked {
            border-color: var(--text-muted);
            color: var(--text-primary);
            background: rgba(148, 154, 166, 0.16);
        }
        .grid-num.correct {
            border-color: var(--success);
            color: var(--success);
            background: rgba(39, 174, 96, 0.14);
        }
        .grid-num.wrong {
            border-color: var(--error);
            color: var(--error);
            background: rgba(231, 76, 60, 0.14);
        }
        .grid-num.bookmarked.correct {
            border-color: var(--text-muted);
            color: var(--text-primary);
            background: linear-gradient(135deg, rgba(148, 154, 166, 0.4) 50%, rgba(39, 174, 96, 0.4) 50%);
        }
        .grid-num.bookmarked.wrong {
            border-color: var(--text-muted);
            color: var(--text-primary);
            background: linear-gradient(135deg, rgba(148, 154, 166, 0.4) 50%, rgba(231, 76, 60, 0.4) 50%);
        }
        /* The currently-open question should always read clearly, regardless of
           whatever other states also apply to it. */
        .grid-num.current.bookmarked,
        .grid-num.current.correct,
        .grid-num.current.wrong {
            background: linear-gradient(135deg, var(--accent), var(--accent-light));
            border-color: var(--accent);
            color: var(--bg-primary);
            box-shadow: 0 0 15px var(--accent-glow);
        }

        /* ===== RESPONSIVE (this file had no breakpoints at all) ===== */
        @media (max-width: 600px) {
            /* Quiz header — keep Back + title + Grid on one row but compact */
            .quiz-header {
                padding: 0.55rem 0.7rem;
                gap: 0.4rem;
                border-radius: var(--radius-sm);
            }
            .quiz-back {
                padding: 0.35rem 0.55rem;
                font-size: 0.78rem;
                gap: 0.25rem;
            }
            .quiz-title-wrap {
                min-width: 0;
                flex: 1;
            }
            .quiz-title {
                font-size: 0.75rem;
                white-space: nowrap;
                overflow: hidden;
                text-overflow: ellipsis;
            }
            .quiz-topic-name {
                font-size: 0.65rem;
                white-space: nowrap;
                overflow: hidden;
                text-overflow: ellipsis;
            }
            .grid-toggle-btn {
                padding: 0.4rem 0.65rem;
                font-size: 0.75rem;
                gap: 0.2rem;
                white-space: nowrap;
                flex-shrink: 0;
            }
            .quiz-progress {
                font-size: 0.72rem;
                padding: 0.28rem 0.55rem;
                flex-shrink: 0;
            }
            .quiz-timer {
                font-size: 0.75rem;
                padding: 0.28rem 0.55rem;
                flex-shrink: 0;
            }

            /* Chapter list cards */
            .chapter-row { padding: 0.85rem 1rem; gap: 0.75rem; }
            .chapter-icon { width: 42px; height: 42px; font-size: 1.4rem; border-radius: 12px; }
            .chapter-title { font-size: 0.9rem; }

            /* Subject rects (subject list page) */
            .subject-rect { padding: 1rem 1.25rem; gap: 1rem; }
            .subject-rect .rect-emoji { width: 46px; height: 46px; font-size: 1.9rem; border-radius: 12px; }
            .subject-rect .rect-name { font-size: 0.95rem; }
            .subject-rect .rect-meta { font-size: 0.75rem; }

            /* History cards */
            .history-card { padding: 0.9rem 1rem; }
            .history-chapter { font-size: 0.92rem; }

            /* Topic select list items */
            .topic-select-item { padding: 0.6rem 0.85rem; gap: 0.6rem; }
            .topic-select-item label { font-size: 0.85rem; }
        }

        @media (max-width: 480px) {
            .sticky-actions { gap: 0.5rem; padding-left: 1rem; padding-right: 1rem; }
            .sticky-actions .btn { min-width: 0; font-size: 0.85rem; padding: 0.7rem 0.75rem; }
            .config-actions { gap: 0.5rem; }
            .quiz-back, .grid-toggle-btn { padding: 0.55rem 0.75rem; }
            .grid-legend { gap: 0.5rem; }
            .grid-legend span { font-size: 0.68rem; }
            .history-card-top { flex-wrap: wrap; }
            .history-actions { flex-wrap: wrap; }
            .history-actions .btn { flex: 1 1 100%; }
            .chapter-row { padding: 0.85rem 1rem; gap: 0.75rem; }
            .chapter-icon { width: 40px; height: 40px; font-size: 1.35rem; }

            /* Quiz header goes to 2-row layout on the smallest phones */
            .quiz-header {
                flex-wrap: wrap;
            }
            .quiz-title-wrap {
                order: 3;
                width: 100%;
                text-align: left;
                margin-top: 0.1rem;
            }
        }


