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

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    /* Use dynamic viewport height for mobile browsers and a JS fallback */
    min-height: calc(var(--vh, 1vh) * 100);
    padding: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    /* Ensure content isn't obstructed by iOS safe area */
    padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px));
}

.container {
    max-width: 900px;
    width: 100%;
    /* Match visible viewport height minus body padding */
    height: calc(var(--vh, 1vh) * 100 - 20px);
    background: white;
    border-radius: 12px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

header {
    background: linear-gradient(135deg, #4a5568 0%, #2d3748 100%);
    color: white;
    padding: 20px;
    text-align: center;
    flex-shrink: 0;
}

header h1 {
    font-size: 1.8em;
    font-weight: 600;
}

.footer {
    background: #f7fafc;
    padding: 10px 20px;
    text-align: center;
    flex-shrink: 0;
    border-top: 1px solid #e2e8f0;
}

.footer a {
    color: #764ba2;
    opacity: 0.8;
    text-decoration: none;
}


.screen {
    display: none;
    padding: 20px;
    flex: 1;
    overflow-y: auto;
}

.screen.active {
    display: flex;
    flex-direction: column;
}

/* Setup Screen */
.setup-section {
    margin-bottom: 25px;
}

.setup-section h2 {
    color: #2d3748;
    margin-bottom: 15px;
    font-size: 1.3em;
}

.radio-group {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.radio-group label {
    display: flex;
    align-items: center;
    padding: 12px 15px;
    background: #f7fafc;
    border: 2px solid #e2e8f0;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.3s ease;
    font-size: 1em;
}

.radio-group label:hover {
    background: #edf2f7;
    border-color: #cbd5e0;
}

.radio-group input[type="radio"] {
    margin-right: 12px;
    width: 20px;
    height: 20px;
    cursor: pointer;
}

.radio-group label:has(input:checked) {
    background: #667eea;
    border-color: #667eea;
    color: white;
    font-weight: 600;
}

/* Buttons */
.btn {
    padding: 12px 24px;
    border: none;
    border-radius: 8px;
    font-size: 1em;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s ease;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.btn-primary {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
}

.btn-primary:hover:not(:disabled) {
    transform: translateY(-2px);
    box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
}

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

.btn-secondary {
    background: #e2e8f0;
    color: #2d3748;
}

.btn-secondary:hover:not(:disabled) {
    background: #cbd5e0;
}

#start-quiz-btn {
    width: auto;
    /* Let flex: 1 from inline style decide final width */
    margin-top: 0;
    /* Align height with the sibling button */
}

/* Quiz Screen */
.quiz-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 15px;
    padding-bottom: 15px;
    border-bottom: 2px solid #e2e8f0;
    flex-shrink: 0;
}

.quiz-info {
    display: flex;
    gap: 15px;
    font-size: 1em;
    color: #4a5568;
}

#quiz-counter {
    font-weight: 700;
    font-size: 1.1em;
}

.stat-correct {
    color: #48bb78;
}

.stat-incorrect {
    color: #f56565;
}

.stat-remaining {
    color: #a0aec0;
}

.quiz-id {
    background: #edf2f7;
    padding: 5px 12px;
    border-radius: 20px;
    font-weight: 600;
}

.quiz-content {
    flex: 1;
    padding: 0 5px;
    overflow-y: auto;
    margin-bottom: 15px;
}

.quiz-image-container {
    text-align: center;
    margin-bottom: 15px;
}

.quiz-image-container.hidden {
    display: none;
}

.quiz-image-container.loading {
    min-height: 200px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #f7fafc;
    border-radius: 8px;
}

.quiz-image-container.loading::after {
    content: '';
    width: 40px;
    height: 40px;
    border: 4px solid #e2e8f0;
    border-top-color: #667eea;
    border-radius: 50%;
    animation: spinner 0.8s linear infinite;
}

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

.quiz-image-container img {
    max-width: 100%;
    max-height: 250px;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    display: block;
    margin: auto;
}

.quiz-image-container img.loading {
    display: none;
}

.quiz-question {
    background: #f7fafc;
    padding: 15px 20px;
    border-radius: 8px;
    margin-bottom: 15px;
}

.quiz-question p {
    font-size: 1.05em;
    line-height: 1.5;
    color: #2d3748;
}

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

.answer-option {
    padding: 15px;
    background: #f7fafc;
    border: 2px solid #e2e8f0;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.3s ease;
    font-size: 1em;
    text-align: left;
}

.answer-option:hover:not(.disabled) {
    background: #edf2f7;
    border-color: #cbd5e0;
    transform: translateX(5px);
}

.answer-option.selected {
    background: #667eea;
    border-color: #667eea;
    color: white;
    font-weight: 600;
}

.answer-option.correct {
    background: #48bb78;
    border-color: #48bb78;
    color: white;
    font-weight: 600;
}

.answer-option.incorrect {
    background: #f56565;
    border-color: #f56565;
    color: white;
    font-weight: 600;
}

.answer-option.disabled {
    cursor: not-allowed;
    opacity: 0.7;
}

.quiz-navigation,
.setup-navigation {
    display: flex;
    justify-content: space-between;
    gap: 15px;
    padding-top: 15px;
    border-top: 2px solid #e2e8f0;
    flex-shrink: 0;
    /* Add bottom padding to avoid overlap with iOS home indicator */
    padding-bottom: env(safe-area-inset-bottom, 0px);
}

.quiz-navigation .btn,
.setup-navigation .btn {
    flex: 1;
}

/* Modal */
.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    z-index: 1000;
    align-items: center;
    justify-content: center;
}

.modal.active {
    display: flex;
}

.modal-content {
    background: white;
    border-radius: 12px;
    width: 90%;
    max-width: 600px;
    max-height: 80vh;
    display: flex;
    flex-direction: column;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px;
    border-bottom: 2px solid #e2e8f0;
}

.modal-header h2 {
    color: #2d3748;
    font-size: 1.3em;
    margin: 0;
}

.close-btn {
    background: none;
    border: none;
    font-size: 2em;
    color: #a0aec0;
    cursor: pointer;
    line-height: 1;
    padding: 0;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: color 0.3s ease;
}

.close-btn:hover {
    color: #2d3748;
}

.modal-body {
    padding: 20px;
    overflow-y: auto;
    flex: 1;
}

.modal-footer {
    padding: 20px;
    border-top: 2px solid #e2e8f0;
    display: flex;
    justify-content: flex-end;
}

.btn-small {
    padding: 8px 16px;
    font-size: 0.9em;
}

.filter-actions {
    display: flex;
    gap: 10px;
    margin-bottom: 20px;
}

.filter-actions .btn {
    flex: 1;
}

.filter-tree {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

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

.filter-chapter-label {
    display: flex;
    align-items: center;
    padding: 12px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border-radius: 8px;
    font-weight: 700;
    color: white;
    cursor: pointer;
    transition: all 0.3s ease;
    font-size: 1.05em;
    position: relative;
}

.filter-chapter-label:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 8px rgba(102, 126, 234, 0.3);
}

.filter-chapter-label input[type="checkbox"] {
    margin-right: 12px;
    width: 18px;
    height: 18px;
    cursor: pointer;
    flex-shrink: 0;
}

.filter-chapter-label .collapse-icon {
    margin-right: 8px;
    font-size: 0.8em;
    transition: transform 0.3s ease;
    flex-shrink: 0;
}

.filter-chapter.collapsed .collapse-icon {
    transform: rotate(-90deg);
}

.filter-themes {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-left: 15px;
    padding-left: 15px;
    border-left: 3px solid #667eea;
    overflow: hidden;
    max-height: 5000px;
    transition: max-height 0.3s ease, opacity 0.3s ease;
}

.filter-chapter.collapsed .filter-themes {
    max-height: 0;
    opacity: 0;
}

.filter-theme {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.filter-theme-label {
    display: flex;
    align-items: center;
    padding: 10px;
    background: #edf2f7;
    border-radius: 6px;
    font-weight: 600;
    color: #2d3748;
    cursor: pointer;
    transition: background 0.3s ease;
    position: relative;
}

.filter-theme-label:hover {
    background: #e2e8f0;
}

.filter-theme-label input[type="checkbox"] {
    margin-right: 10px;
    width: 18px;
    height: 18px;
    cursor: pointer;
    flex-shrink: 0;
}

.filter-theme-label .collapse-icon {
    margin-right: 8px;
    font-size: 0.75em;
    color: #4a5568;
    transition: transform 0.3s ease;
    flex-shrink: 0;
}

.filter-theme.collapsed .collapse-icon {
    transform: rotate(-90deg);
}

.filter-entries {
    display: flex;
    flex-direction: column;
    gap: 4px;
    margin-left: 20px;
    padding-left: 15px;
    border-left: 2px solid #e2e8f0;
    overflow: hidden;
    max-height: 5000px;
    transition: max-height 0.3s ease, opacity 0.3s ease;
}

.filter-theme.collapsed .filter-entries {
    max-height: 0;
    opacity: 0;
}

.filter-entry-label {
    display: flex;
    align-items: center;
    padding: 8px 10px;
    background: #f7fafc;
    border-radius: 6px;
    cursor: pointer;
    transition: background 0.3s ease;
    font-size: 0.95em;
    color: #4a5568;
}

.filter-entry-label:hover {
    background: #edf2f7;
}

.filter-entry-label input[type="checkbox"] {
    margin-right: 10px;
    width: 18px;
    height: 18px;
    cursor: pointer;
    flex-shrink: 0;
}

/* Responsive Design */
@media (max-width: 768px) {
    body {
        padding: 5px;
    }

    .container {
        border-radius: 8px;
        height: calc(var(--vh, 1vh) * 100 - 10px);
    }

    header {
        padding: 15px;
    }

    header h1 {
        font-size: 1.4em;
    }

    .screen {
        padding: 15px;
    }

    .quiz-header {
        flex-direction: column;
        align-items: flex-start;
        gap: 10px;
        margin-bottom: 10px;
        padding-bottom: 10px;
    }

    .quiz-info {
        flex-direction: column;
        gap: 8px;
        font-size: 0.9em;
    }

    .btn {
        padding: 10px 16px;
        font-size: 0.9em;
    }

    .quiz-navigation,
    .setup-navigation {
        flex-direction: column;
        gap: 10px;
    }

    .quiz-image-container img {
        max-height: 200px;
    }

    .quiz-question {
        padding: 12px 15px;
        margin-bottom: 10px;
    }

    .quiz-question p {
        font-size: 1em;
    }

    .answer-option {
        padding: 12px;
        font-size: 0.95em;
    }
}

/* Prefer modern dynamic viewport units when supported */
@supports (height: 100dvh) {
    body {
        min-height: 100dvh;
    }

    .container {
        height: calc(100dvh - 20px);
    }

    @media (max-width: 768px) {
        .container {
            height: calc(100dvh - 10px);
        }
    }
}