* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    /* MOBILE ONLY: Optimize for touch */
    -webkit-tap-highlight-color: transparent;
    -webkit-touch-callout: none;
}

:root {
    --primary-color: #4A90E2;
    --primary-light: #6BA3E8;
    --primary-dark: #2C5FA8;
    --secondary-color: #F8FAFC;
    --bg-color: #FFFFFF;
    --text-color: #1E293B;
    --text-secondary: #64748B;
    --card-bg: #F1F5F9;
    --success-color: #34D399;
    --success-bg: #047857;
    --error-color: #F87171;
    --error-bg: #B91C1C;
    --warning-color: #F59E0B;
    --border-color: #E2E8F0;
    --option-bg: #1E3F73;
    --option-bg-hover: #2C5FA8;
    --option-border: rgba(255, 255, 255, 0.18);
    --border-radius: 16px;
    --shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Helvetica Neue', Arial, sans-serif;
    background: var(--bg-color);
    color: var(--text-color);
    line-height: 1.6;
    overflow-x: hidden;
    padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
    /* MOBILE ONLY: Max width for mobile-first design */
    max-width: 600px;
    margin: 0 auto;
    min-height: 100vh;
}

/* Telegram Theme Colors */
body.dark-theme {
    --bg-color: #0F172A;
    --text-color: #F1F5F9;
    --text-secondary: #94A3B8;
    --card-bg: #1E293B;
    --border-color: #334155;
    --success-color: #34D399;
    --success-bg: #0F5132;
    --error-color: #F87171;
    --error-bg: #7F1D1D;
    --option-bg: #1B2D4A;
    --option-bg-hover: #274772;
    --option-border: rgba(255, 255, 255, 0.14);
    --shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.3), 0 2px 4px -1px rgba(0, 0, 0, 0.2);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.3), 0 4px 6px -2px rgba(0, 0, 0, 0.2);
}

.hidden {
    display: none !important;
}

/* Loading Screen */
.loading-screen {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    background: var(--bg-color);
    z-index: 1000;
}

.spinner {
    width: 50px;
    height: 50px;
    border: 4px solid var(--card-bg);
    border-top-color: var(--primary-color);
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

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

/* Header */
.header {
    position: relative;
    text-align: center;
    padding: 20px 16px 12px;
    background: linear-gradient(180deg, var(--card-bg), transparent);
    color: var(--text-color);
    margin-bottom: 12px;
}

.header-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 56px;
    height: 56px;
    border-radius: 16px;
    background: rgba(74, 144, 226, 0.12);
    margin: 0 auto 8px;
}

.header-icon-svg {
    display: block;
    width: 28px;
    height: 28px;
    stroke-width: 2.5;
    color: var(--primary-color);
}

.header h1 {
    font-size: 20px;
    margin-bottom: 4px;
    font-weight: 700;
    color: var(--primary-color);
}

.subtitle {
    font-size: 13px;
    color: var(--text-secondary);
    font-weight: 400;
}

/* Settings button in header */
.settings-btn {
    position: absolute;
    inset-inline-start: 12px;
    top: 12px;
    width: 36px;
    height: 36px;
    border-radius: 18px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: var(--card-bg);
    border: 1px solid var(--border-color);
    color: var(--text-color);
    box-shadow: var(--shadow);
}

.header .settings-btn i {
    width: 20px;
    height: 20px;
}

/* Container */
.container {
    max-width: 600px;
    margin: 0 auto;
    padding: 0 15px 20px;
}

/* User Info */
.user-info {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px;
    background: var(--card-bg);
    border-radius: var(--border-radius);
    margin-bottom: 16px;
    box-shadow: var(--shadow);
}

.avatar {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: var(--primary-color);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 24px;
    color: white;
    flex-shrink: 0;
}

.avatar-icon {
    width: 22px;
    height: 22px;
    stroke-width: 2;
}

.user-details h3 {
    font-size: 16px;
    margin-bottom: 2px;
    font-weight: 600;
}

.user-details p {
    font-size: 13px;
    opacity: 0.7;
    color: var(--text-secondary);
}

#score {
    font-weight: bold;
    color: var(--primary-color);
}

/* Start Section */
.start-section {
    margin-bottom: 24px;
}

/* Test Mode Selector */
.test-mode-selector {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 10px;
    margin-bottom: 16px;
}

.test-mode-card {
    background: var(--card-bg);
    border: 2px solid var(--border-color);
    border-radius: 16px;
    padding: 12px;
    cursor: pointer;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    transition: all 0.25s ease;
    position: relative;
    overflow: hidden;
    text-align: center;
}

.test-mode-card::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(135deg, var(--primary-color), var(--primary-light));
    opacity: 0;
    transition: opacity 0.3s ease;
}

.test-mode-card:hover {
    border-color: var(--primary-color);
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(74, 144, 226, 0.15);
}

.test-mode-card.active {
    border-color: transparent;
    background: linear-gradient(135deg, var(--primary-color), var(--primary-light));
    color: #fff;
}

.test-mode-card.active::before {
    opacity: 1;
}

.test-mode-card.disabled {
    opacity: 0.5;
    cursor: not-allowed;
    pointer-events: none;
}

.test-mode-card > * {
    position: relative;
    z-index: 1;
}

.mode-icon {
    width: 48px;
    height: 48px;
    background: rgba(74, 144, 226, 0.1);
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.test-mode-card.active .mode-icon {
    background: rgba(255, 255, 255, 0.2);
}

/* Bookmark button in question header */
.bookmark-btn {
    position: absolute;
    inset-inline-start: 12px;
    top: 0;
    width: 32px;
    height: 32px;
    border-radius: 8px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: var(--card-bg);
    border: 1.5px solid var(--border-color);
    color: var(--text-secondary);
    cursor: pointer;
    transition: all 0.2s ease;
}

.bookmark-btn:hover {
    background: rgba(74, 144, 226, 0.08);
    border-color: var(--primary-color);
    color: var(--primary-color);
}

.bookmark-btn.saved {
    background: var(--primary-color);
    border-color: var(--primary-color);
    color: white;
}

.bookmark-btn.saved:hover {
    background: var(--primary-dark);
}

.bookmark-btn i {
    width: 18px;
    height: 18px;
    stroke-width: 2;
}

.bookmark-btn.saved i {
    fill: white;
}

.mode-icon i {
    width: 24px;
    height: 24px;
    color: var(--primary-color);
    stroke-width: 2.25;
}

.test-mode-card.active .mode-icon i {
    color: white;
}

.mistakes-icon {
    background: rgba(239, 68, 68, 0.1);
}

.test-mode-card.active .mistakes-icon {
    background: rgba(255, 255, 255, 0.2);
}

.saved-icon {
    background: rgba(245, 158, 11, 0.1);
}

.test-mode-card.active .saved-icon {
    background: rgba(255, 255, 255, 0.2);
}

.mode-content {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.mode-content h4 {
    font-size: 13px;
    font-weight: 600;
    margin: 0;
    line-height: 1.2;
}

.mode-count {
    font-size: 11px;
    opacity: 0.7;
    margin: 0;
}

/* Question Count Modal */
.question-count-modal {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
}

.question-count-modal.hidden {
    display: none;
}

.modal-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(4px);
    animation: fadeIn 0.2s ease;
}

.modal-content {
    position: relative;
    background: var(--card-bg);
    border-radius: 20px;
    padding: 24px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3);
    max-width: 320px;
    width: 100%;
    animation: slideUp 0.3s ease;
    z-index: 1001;
}

.modal-content h3 {
    text-align: center;
    margin: 0 0 20px 0;
    font-size: 18px;
    font-weight: 700;
    color: var(--text-color);
}

.count-options-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
}

.count-option {
    background: var(--bg-color);
    border: 2px solid var(--border-color);
    border-radius: 16px;
    padding: 20px 16px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
    cursor: pointer;
    transition: all 0.25s ease;
}

.count-option:hover {
    border-color: var(--primary-color);
    background: rgba(74, 144, 226, 0.08);
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(74, 144, 226, 0.15);
}

.count-option:active {
    transform: translateY(0);
}

.count-number {
    font-size: 28px;
    font-weight: 800;
    color: var(--primary-color);
    line-height: 1;
}

.count-label {
    font-size: 13px;
    color: var(--text-secondary);
    font-weight: 500;
}

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

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

.btn-start {
    width: 100%;
    background: linear-gradient(135deg, var(--primary-color), var(--primary-dark));
    border: none;
    border-radius: var(--border-radius);
    padding: 24px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 20px;
    box-shadow: 0 12px 24px rgba(74, 144, 226, 0.25);
    transition: transform 0.2s ease, box-shadow 0.2s ease, background 0.2s ease;
    margin-bottom: 20px;
}

.btn-start:hover {
    transform: translateY(-2px);
    box-shadow: 0 16px 30px rgba(74, 144, 226, 0.32);
}

.btn-start:active {
    transform: translateY(0);
}

.btn-start-icon {
    width: 64px;
    height: 64px;
    background: rgba(255, 255, 255, 0.22);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    backdrop-filter: blur(10px);
}

.btn-start-icon i {
    width: 36px;
    height: 36px;
    color: white;
    stroke-width: 2;
}

.btn-start-content {
    text-align: right;
    flex: 1;
    color: white;
}

.btn-start-content h3 {
    font-size: 22px;
    font-weight: 700;
    margin-bottom: 4px;
}

.btn-start-content p {
    font-size: 14px;
    opacity: 0.9;
}

.test-info {
    display: flex;
    justify-content: space-around;
    gap: 12px;
    padding: 16px;
    background: var(--card-bg);
    border-radius: var(--border-radius);
}

.info-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    flex: 1;
}

.info-item i {
    width: 28px;
    height: 28px;
    color: var(--primary-color);
    stroke-width: 2.25;
}

.info-item span {
    font-size: 13px;
    color: var(--text-secondary);
    font-weight: 500;
}

.categories h2 {
    font-size: 20px;
    margin-bottom: 15px;
    text-align: center;
}

.category-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 15px;
    max-width: 400px;
    margin: 0 auto;
}

.category-btn {
    background: var(--card-bg);
    border: 2px solid transparent;
    border-radius: var(--border-radius);
    padding: 20px 15px;
    cursor: pointer;
    transition: all 0.3s ease;
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
    box-shadow: var(--shadow);
    /* MOBILE ONLY: Touch optimization */
    min-height: 120px;
    touch-action: manipulation;
    user-select: none;
    -webkit-user-select: none;
}

.category-btn:hover {
    border-color: var(--primary-color);
    background: var(--bg-color);
}

.category-btn .icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 56px;
    height: 56px;
    background: var(--primary-color);
    border-radius: 12px;
    margin-bottom: 12px;
    color: white;
}

.category-btn .icon i {
    width: 32px;
    height: 32px;
    stroke-width: 2;
}

.category-btn .title {
    font-size: 16px;
    font-weight: bold;
    margin-bottom: 5px;
    display: block;
}

.category-btn .desc {
    font-size: 12px;
    opacity: 0.7;
    display: block;
}

.category-btn.disabled {
    opacity: 0.5;
    cursor: not-allowed;
    background: var(--card-bg);
}

.category-btn.disabled:hover {
    border-color: transparent;
    transform: none;
    box-shadow: var(--shadow);
}

/* Test Screen */
.test-screen {
    animation: fadeIn 0.3s ease-in;
    padding-top: 2vh; /* تحريك المحتوى للأسفل بنسبة 2% */
    margin-top: 0;
}

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

.progress-text {
    font-size: 16px;
    font-weight: bold;
    color: var(--primary-color);
    padding-inline-start: 48px;
}

.question-container {
    background: var(--card-bg);
    border-radius: var(--border-radius);
    padding: 12px;
    margin-bottom: 12px;
    box-shadow: var(--shadow);
    max-height: 80vh;
    overflow-y: auto;
}

.question-header {
    position: relative;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    padding: 0 10px 12px 10px;
    border-bottom: 1px solid var(--border-color);
}

.timer {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 16px;
    font-weight: 600;
    color: var(--primary-color);
}

.timer i {
    width: 18px;
    height: 18px;
    stroke-width: 2;
}

.question-text {
    font-size: 20px;
    line-height: 1.8;
    margin-bottom: 20px;
    text-align: justify;
    font-weight: 500;
}

/* Question Image Container */
.question-image-container {
    width: 100%;
    height: 266px;
    margin-top: -10px;
    margin-bottom: 12px;
    margin-left: -12px;
    margin-right: -12px;
    width: calc(100% + 24px);
    border-radius: 0;
    overflow: hidden;
    background: transparent;
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
}

.question-image-container.loading::after {
    content: '';
    position: absolute;
    width: 40px;
    height: 40px;
    border: 4px solid rgba(255, 255, 255, 0.1);
    border-top-color: var(--primary-color);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

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

.question-image {
    width: 100%;
    height: 100%;
    object-fit: contain;
    border: none;
    border-radius: var(--border-radius);
    display: block;
    transition: opacity 0.3s ease;
}

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

.answer-btn {
    background: var(--option-bg);
    border: 1px solid var(--option-border);
    border-radius: var(--border-radius);
    padding: 12px 16px;
    cursor: pointer;
    transition: all 0.3s ease;
    text-align: right;
    font-size: 16px;
    font-weight: 500;
    line-height: 1.4;
    color: #FFFFFF;
    /* MOBILE ONLY: Touch optimization */
    min-height: 48px;
    touch-action: manipulation;
    user-select: none;
    -webkit-user-select: none;
}

.answer-btn:hover {
    background: var(--option-bg-hover);
    border-color: rgba(255, 255, 255, 0.25);
    transform: translateX(-3px);
}

.answer-btn.selected {
    background: var(--primary-color);
    color: #FFFFFF;
    border-color: rgba(255, 255, 255, 0.35);
}

.answer-btn.correct {
    background: var(--success-bg);
    color: #FFFFFF;
    border-color: var(--success-color);
    border-width: 3px;
    font-weight: 600;
}

.answer-btn.incorrect {
    background: var(--error-bg);
    color: #FFFFFF;
    border-color: var(--error-color);
    border-width: 3px;
    font-weight: 600;
}

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

/* Test Actions */
.test-actions {
    display: flex;
    gap: 8px;
    margin-top: 12px;
    margin-bottom: 0; /* لإزالة المسافة السفلية */
}

.btn {
    flex: 1;
    padding: 12px;
    border: none;
    border-radius: var(--border-radius);
    font-size: 15px;
    font-weight: bold;
    cursor: pointer;
    transition: all 0.3s ease;
    /* MOBILE ONLY: Optimize touch targets (min 44x44px per Apple/Google guidelines) */
    min-height: 44px;
    touch-action: manipulation;
    user-select: none;
    -webkit-user-select: none;
}

.btn-primary {
    background: var(--primary-color);
    color: white;
    box-shadow: var(--shadow);
}

.btn-primary:hover:not(:disabled) {
    background: var(--primary-dark);
    transform: translateY(-2px);
}

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

.btn-secondary {
    background: var(--card-bg);
    color: var(--text-color);
    border: 2px solid var(--border-color);
}

.btn-secondary:hover {
    background: var(--bg-color);
    border-color: var(--primary-color);
    color: var(--primary-color);
    transform: translateY(-2px);
}

.btn-share {
    background: #667eea;
    color: white;
}

.btn-share:hover {
    background: #5568d3;
}

/* Results Screen */
.results-screen {
    animation: fadeIn 0.5s ease-in;
    text-align: center;
    position: relative;
    overflow: hidden;
}

.results-header {
    margin-bottom: 30px;
    position: relative;
    z-index: 2;
}

#confetti-canvas {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 1;
}

.results-header h2 {
    font-size: 28px;
    color: var(--primary-color);
    margin: 0;
    padding: 20px 0;
}

.results-stats {
    display: flex;
    flex-direction: row;
    align-items: stretch;
    justify-content: space-between;
    gap: 10px;
    margin-bottom: 20px;
}

.stat-card {
    background: var(--card-bg);
    border-radius: 14px;
    padding: 14px 12px;
    box-shadow: var(--shadow);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
    flex: 1;
    min-width: 0;
}

.stat-card .stat-icon {
    width: 22px;
    height: 22px;
    stroke-width: 2.25;
}

.results-stats .stat-card:nth-child(1) .stat-icon { color: var(--success-color); }
.results-stats .stat-card:nth-child(2) .stat-icon { color: var(--error-color); }
.results-stats .stat-card:nth-child(3) .stat-icon { color: var(--primary-color); }

.stat-value {
    font-weight: 800;
    font-size: clamp(18px, 6vw, 26px);
    color: var(--primary-color);
    line-height: 1.1;
}

.stat-label {
    font-size: 12px;
    opacity: 0.8;
    text-align: center;
}

/* Per-card colors */
.results-stats .stat-card:nth-child(1) .stat-value { color: var(--success-color); }
.results-stats .stat-card:nth-child(2) .stat-value { color: var(--error-color); }
.results-stats .stat-card:nth-child(3) .stat-value { color: var(--primary-color); }

.results-message {
    background: var(--card-bg);
    border-radius: var(--border-radius);
    padding: 20px;
    margin-bottom: 30px;
    text-align: right;
    line-height: 1.8;
    box-shadow: var(--shadow);
}

.results-actions {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

/* Responsive Design - Mobile First */
@media (max-width: 480px) {
    .category-grid {
        grid-template-columns: 1fr 1fr;
    }
    
    /* Keep 3-column layout for test mode cards on mobile */
    .test-mode-selector {
        gap: 8px;
    }
    
    .test-mode-card {
        padding: 10px 8px;
        gap: 6px;
    }
    
    .mode-icon {
        width: 40px;
        height: 40px;
    }
    
    .mode-icon i {
        width: 20px;
        height: 20px;
    }
    
    .mode-content h4 {
        font-size: 11px;
    }
    
    .mode-count {
        font-size: 10px;
    }
    
    /* Keep results horizontal on mobile, reduce gaps/padding */
    .results-stats { gap: 8px; }
    .stat-card { padding: 12px 10px; gap: 4px; }
    .stat-value { font-size: clamp(18px, 6vw, 24px); }
    
    .header h1 {
        font-size: 24px;
    }
    
    .question-text {
        font-size: 18px;
    }
}

/* MOBILE ONLY: Desktop view optimization (for web.telegram.org) */
@media (min-width: 601px) {
    html {
        background: #f0f0f0;
    }
    
    body {
        box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
        border-radius: 0;
    }
    
    /* Ensure mobile-like experience on desktop */
    .container {
        max-width: 100%;
    }
}

/* Reduced Motion for low-end devices */
body.reduced-motion * {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
}

/* Review Navigation Buttons (for mistakes/saved mode) */
.review-navigation {
    display: flex;
    gap: 12px;
    padding: 0 20px 20px;
    margin-top: 20px; /* مسافة كافية من test-actions */
    direction: rtl;
}

.review-navigation .btn {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 12px 16px;
}

.review-navigation .btn-outline {
    background: var(--card-bg);
    color: var(--primary-color);
    border: 2px solid var(--primary-color);
}

.review-navigation .btn-outline:hover {
    background: var(--primary-color);
    color: white;
}

.review-navigation .btn-outline:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    background: var(--card-bg);
    color: var(--text-secondary);
    border-color: var(--border-color);
}

.review-navigation .btn i {
    width: 20px;
    height: 20px;
}

.review-navigation.hidden {
    display: none;
}
