/* ===== MOBILE RESPONSIVE ===== */
@media (max-width: 768px) {
    /* Remove backdrop-filter on mobile (GPU/battery savings) */
    .quiz-modal-overlay {
        backdrop-filter: none;
        -webkit-backdrop-filter: none;
        background: rgba(0, 0, 0, 0.6); /* slightly darker to compensate for no blur */
    }

    /* Mobile: Bottom sheet slide-up pattern (like Word Stories) */
    .quiz-modal {
        /* Override centered positioning - anchor to bottom */
        top: auto !important;
        left: 0 !important;
        right: 0 !important;
        bottom: 0 !important;
        width: 100% !important;
        max-width: 100% !important;
        max-height: 90vh;
        min-height: auto;
        /* Slide up from bottom (use translate3d for GPU acceleration) */
        -webkit-transform: translate3d(0, 100%, 0);
        transform: translate3d(0, 100%, 0);
        /* Bottom sheet border radius */
        border-radius: 24px 24px 0 0;
        border-left: none;
        border-right: none;
        border-bottom: none;
        /* Smoother spring-like animation */
        -webkit-transition: -webkit-transform 0.4s cubic-bezier(0.16, 1, 0.3, 1),
                            opacity 0.3s ease,
                            visibility 0.3s ease;
        transition: transform 0.4s cubic-bezier(0.16, 1, 0.3, 1),
                    opacity 0.3s ease,
                    visibility 0.3s ease;
        /* Bottom sheet shadow */
        box-shadow: 0 -20px 60px rgba(0, 0, 0, 0.15);
        /* Enable smooth touch scrolling */
        -webkit-overflow-scrolling: touch;
    }

    .quiz-modal.active {
        -webkit-transform: translate3d(0, 0, 0) !important;
        transform: translate3d(0, 0, 0) !important;
    }

    /* Drag handle indicator for swipe-to-close affordance */
    .quiz-modal::before {
        content: '';
        position: absolute;
        top: 8px;
        left: 50%;
        transform: translateX(-50%);
        width: 36px;
        height: 4px;
        background: rgba(0, 0, 0, 0.15);
        border-radius: 2px;
        z-index: 10;
    }

    /* Add top padding to account for drag handle */
    .quiz-modal-header {
        padding: 20px 16px 14px;
    }

    .quiz-start-screen {
        padding: 24px 20px;
        gap: 16px;
        max-height: calc(90vh - 80px);
    }

    .quiz-question-screen,
    .quiz-results-screen {
        padding: 12px 16px;
        max-height: calc(90vh - 80px);
        gap: 8px;
    }

    .quiz-hero {
        gap: 6px;
    }

    .quiz-start-icon,
    .quiz-results-icon {
        font-size: 2.5em;
        margin-bottom: 4px;
    }

    .quiz-start-icon-img {
        width: 90px;
        height: 90px;
        padding: 10px;
    }

    .quiz-start-title {
        font-size: 1.5em;
    }

    .quiz-results-title {
        font-size: 1.3em;
    }

    .quiz-pool-badge {
        font-size: 0.85em;
        padding: 5px 12px;
    }

    .quiz-settings {
        gap: 14px;
    }

    .quiz-config-card {
        padding: 12px 14px;
        gap: 8px;
        border-radius: 14px;
    }

    .quiz-config-label {
        font-size: 0.82em;
    }

    .quiz-length-buttons {
        border-radius: 10px;
        padding: 3px;
        gap: 3px;
    }

    .quiz-length-btn {
        padding: 10px 6px;
        font-size: 0.9em;
        border-radius: 8px;
    }

    .quiz-mode-buttons {
        gap: 8px;
    }

    .quiz-mode-btn {
        padding: 14px 10px;
        border-radius: 10px;
    }

    .quiz-mode-icon {
        font-size: 1.4em;
    }

    .quiz-mode-label {
        font-size: 0.85em;
    }

    .quiz-mode-desc {
        font-size: 0.7em;
    }

    /* Options panel - Mobile */
    .quiz-options-panel {
        padding: 12px;
        gap: 12px;
    }

    .quiz-filter-btn {
        padding: 6px 8px;
        font-size: 0.75em;
    }

    .quiz-start-btn {
        padding: 14px 32px;
        font-size: 1em;
        margin-top: 2px;
    }

    .quiz-question-text {
        font-size: 1.25em;
        padding: 10px 16px;
        /* Prevent CJK character-level line breaks — the desktop absolute-positioning
           ruby hack uses display:inline on <ruby>, and without this, mobile browsers
           can break between every character, causing one-char-per-line stacking.
           keep-all limits breaks to spaces/punctuation; break-word is the safety valve
           if a segment is still too wide. */
        word-break: keep-all;
        overflow-wrap: break-word;
    }

    /* Mobile ruby: no display override needed. Quiz uses <span> elements
       (not <ruby>/<rt>), so the desktop inline + absolute positioning from
       quiz-question.css works correctly on mobile too. Only container
       adjustments (line-height, padding) differ for smaller screens. */
    .quiz-question-text .ruby-rt {
        font-size: 0.6em;
    }

    .quiz-question-text:has(.ruby-group) {
        line-height: 2.0;
        padding-top: 16px;
    }

    .quiz-answer-btn:has(.ruby-group) {
        padding-top: 16px;
    }

    .quiz-answer-btn .quiz-answer-text:has(.ruby-group) {
        line-height: 2.0;
    }

    .answer-card-word:has(.ruby-group) {
        line-height: 2.0;
        padding-top: 6px;
    }

    .quiz-answers {
        display: grid;
        grid-template-columns: 1fr;
        gap: 10px;
    }

    .quiz-answer-btn {
        padding: 16px 18px;
        font-size: 1.15em;
    }

    /* Mobile: Tap-to-expand answer cards */
    .quiz-answer-card {
        transition: background 0.3s ease, border-color 0.3s ease, transform 0.2s ease;
        cursor: pointer;
    }

    .quiz-stat-value {
        font-size: 1.4em;
    }

    .quiz-stat-label {
        font-size: 0.6em;
    }

    .quiz-score-value {
        font-size: 2.5em;
    }

    .quiz-results-stats {
        gap: 16px;
        padding: 12px 0;
    }

    .quiz-results-stat-value {
        font-size: 1.1em;
    }

    .quiz-results-stat-label {
        font-size: 0.65em;
    }

    .quiz-breakdown-item {
        padding: 8px 12px;
        flex-direction: column;
        align-items: stretch;
        gap: 6px;
    }

    .quiz-breakdown-label {
        font-size: 0.8em;
        white-space: normal;
        flex: none;
    }

    .quiz-breakdown-bar {
        flex: none;
        width: 100%;
    }

    .quiz-breakdown-score {
        align-self: flex-end;
        margin-top: -20px;
    }

    .quiz-weak-item {
        padding: 8px 12px;
        flex-direction: column;
        align-items: flex-start;
        gap: 4px;
    }

    .quiz-timer-container {
        margin-bottom: 12px;
    }

    .quiz-timer-ring {
        width: 70px;
        height: 70px;
    }

    .timer-seconds {
        font-size: 1.5em;
    }

    .timer-label {
        font-size: 0.55em;
    }

    .time-bonus {
        font-size: 0.9em;
    }

    .speed-streak {
        padding: 3px 8px;
    }

    .quiz-results-actions {
        flex-direction: column;
    }

    .quiz-retry-btn,
    .quiz-close-btn {
        width: 100%;
    }

    /* Hint Bar - Mobile: plain text links, 44px touch target via min-height */
    .quiz-hint-bar {
        width: fit-content;
        flex-wrap: wrap;
        gap: 4px 16px;
    }

    .quiz-hint-btn {
        padding: 4px 2px;
        min-height: 44px;
    }

    .quiz-hint-text {
        font-size: 0.88em;
    }

    .quiz-navigation {
        gap: 10px;
    }

    /* Feedback - Mobile: Tighter padding for readability */
    .feedback-overlay-content {
        padding: 16px 12px;
    }

    .feedback-banner-body {
        padding: 14px 12px;
    }

    .feedback-insight-flow {
        font-size: 14px;
    }

    .feedback-compare-item {
        padding: 10px 8px;
    }

    /* Feedback Cards - Mobile: Tighter spacing */
    .feedback-comparison {
        gap: 10px;
    }

    .feedback-card {
        padding: 14px 16px;
        border-radius: 12px;
    }

    .feedback-card-content {
        gap: 8px;
    }

    .feedback-word-jp {
        font-size: 1.4em;
    }

    .feedback-card-badge {
        top: 10px;
        right: 10px;
        width: 24px;
        height: 24px;
        font-size: 12px;
    }

    .feedback-card-label {
        font-size: 10px;
        margin-bottom: 6px;
    }

    .feedback-why {
        padding: 14px 16px;
        gap: 12px;
        margin-top: 14px;
    }

    .feedback-why-icon {
        font-size: 18px;
    }

    .feedback-why-text {
        font-size: 17px;
    }

    .feedback-title {
        font-size: 24px;
    }

    .feedback-icon {
        width: 46px;
        height: 46px;
        font-size: 22px;
    }

    /* Feedback Footer - Mobile: Stack vertically */
    .feedback-footer {
        flex-direction: column;
        gap: 12px;
        align-items: stretch;
    }

    .feedback-mascot-inline {
        justify-content: center;
        text-align: center;
    }

    .feedback-continue-btn {
        width: 100%;
        padding: 14px 24px;
    }

    /* Word Story Hint - Mobile */
    .feedback-story-toggle {
        padding: 12px 14px;
        font-size: 15px;
    }
    .feedback-story-bubble-text {
        font-size: 16px;
    }

    /* Shoji layout — Mobile overrides */
    .feedback-shoji {
        padding: 18px 16px 16px 22px;
    }

    .feedback-shoji-word {
        font-size: 1.7em;
    }

    .feedback-shoji-compare {
        gap: 12px;
    }

    .feedback-shoji-compare-word {
        font-size: 1.35em;
    }

    .feedback-shoji-insight {
        font-size: 17px;
    }

    /* Collapsible Answers - Mobile */
    .answers-collapsible-header {
        padding: 10px 14px;
    }

    .collapsible-title {
        font-size: 14px;
    }

    .answers-collapsible .expand-hint {
        font-size: 0.65rem;
    }

    .answers-collapsible-content {
        padding: 0 10px 10px;
        gap: 6px;
    }

    .answers-collapsible.collapsed .answers-collapsible-content {
        padding: 0 10px;
    }
}

/* Extra small screens — keep labels, shrink text */
@media (max-width: 400px) {
    .quiz-hint-bar {
        gap: 4px 8px;
    }

    .quiz-hint-btn {
        font-size: 0.78em;
    }

    .quiz-hint-btn .label-en {
        display: none;
    }
}

/* ===== MOBILE UX IMPROVEMENTS ===== */

/* Safe area insets for notched phones (iPhone X+) */
@supports (padding: env(safe-area-inset-bottom)) {
    .quiz-modal {
        padding-bottom: env(safe-area-inset-bottom);
    }

    @media (max-width: 768px) {
        .quiz-modal {
            /* Extra padding for notched devices */
            padding-bottom: max(16px, env(safe-area-inset-bottom));
            padding-left: max(16px, env(safe-area-inset-left));
            padding-right: max(16px, env(safe-area-inset-right));
        }

        /* Navigation row needs extra bottom padding on notched phones */
        .quiz-navigation {
            padding-bottom: max(8px, env(safe-area-inset-bottom));
        }
    }
}

/* Minimum 44px touch targets for mobile (Apple HIG / WCAG) */
@media (max-width: 768px) {
    /* Hint text links — 44px touch target */
    .quiz-hint-btn {
        min-height: 44px;
        padding: 4px 2px;
    }

    /* Length/count buttons - ensure 44px minimum */
    .quiz-length-btn {
        min-height: 44px;
        padding: 12px 8px;
    }

    /* Navigation arrows - ensure 44px minimum */
    .quiz-nav-btn {
        min-width: 44px;
        min-height: 44px;
        padding: 12px 16px;
    }

    /* Mode buttons - ensure minimum touch target */
    .quiz-mode-btn {
        min-height: 60px;
    }
}

/* Focus-visible indicators for accessibility */
.quiz-answer-btn:focus-visible,
.quiz-hint-btn:focus-visible,
.quiz-nav-btn:focus-visible,
.quiz-length-btn:focus-visible,
.quiz-mode-btn:focus-visible,
.quiz-retry-btn:focus-visible,
.quiz-close-btn:focus-visible,
.quiz-start-btn:focus-visible,
.quiz-resume-btn:focus-visible {
    outline: 2px solid var(--quiz-primary);
    outline-offset: 2px;
}

/* Ensure proper focus ring visibility on dark backgrounds */
.quiz-answer-card:focus-visible {
    outline: 2px solid var(--quiz-primary);
    outline-offset: 2px;
}

/* Remove default focus outlines (we're using focus-visible) */
.quiz-answer-btn:focus:not(:focus-visible),
.quiz-hint-btn:focus:not(:focus-visible),
.quiz-nav-btn:focus:not(:focus-visible),
.quiz-length-btn:focus:not(:focus-visible),
.quiz-mode-btn:focus:not(:focus-visible) {
    outline: none;
}

/* Visual swipe feedback animation */
.quiz-question-screen.swipe-left {
    animation: swipeLeftFeedback 0.2s ease-out;
}

.quiz-question-screen.swipe-right {
    animation: swipeRightFeedback 0.2s ease-out;
}

@keyframes swipeLeftFeedback {
    0% { transform: translateX(0); }
    50% { transform: translateX(-8px); }
    100% { transform: translateX(0); }
}

@keyframes swipeRightFeedback {
    0% { transform: translateX(0); }
    50% { transform: translateX(8px); }
    100% { transform: translateX(0); }
}

/* ===== DASHBOARD + SCORE RING MOBILE ===== */
@media (max-width: 768px) {
    /* Score ring smaller on mobile */
    .quiz-score-ring-wrap {
        width: 100px;
        height: 100px;
    }

    .quiz-score-ring {
        width: 100px;
        height: 100px;
    }

    .quiz-score-value {
        font-size: 1.8em;
    }

    /* Stat cards tighter */
    .quiz-stat-cards {
        gap: 6px;
    }

    .quiz-stat-card {
        padding: 10px 6px;
    }

    .quiz-stat-card-value {
        font-size: 1.1em;
    }

    /* Dashboard streak calendar tighter */
    .dashboard-streak-calendar {
        gap: 5px;
    }

    .streak-day-circle {
        width: 32px;
        height: 32px;
        font-size: 0.75em;
    }

    /* Dashboard mastery grid */
    .dashboard-mastery-grid {
        gap: 6px;
    }

    .mastery-count {
        font-size: 1.3em;
    }

    .mastery-label {
        text-align: center;
        line-height: 1.3;
    }

    .mastery-label .label-en {
        display: block;
        margin-left: 0;
    }

    .quiz-dashboard-screen {
        padding: 20px 16px;
    }
}

/* ===== LANDSCAPE PHONE ===== */
@media (max-width: 768px) and (orientation: landscape) {
    /* Bottom sheet uses full height in landscape (short viewport) */
    .quiz-modal {
        max-height: 100vh;
        border-radius: 0;
    }

    .quiz-start-screen,
    .quiz-question-screen,
    .quiz-results-screen {
        max-height: calc(100vh - 70px);
        padding: 12px 16px;
    }

    /* Compact progress section */
    .quiz-progress-section {
        padding: 8px 16px;
    }

    .quiz-progress-row {
        margin-bottom: 6px;
    }

    /* Answers in 2-column grid to use horizontal space */
    .quiz-answers {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 8px;
    }

    .quiz-answer-btn {
        padding: 12px 14px;
        font-size: 0.9em;
    }

    /* Compact header */
    .quiz-modal-header {
        padding: 10px 16px;
    }

    .quiz-avatar {
        width: 36px;
        height: 36px;
        font-size: 1.3rem;
    }

    /* Tighter navigation */
    .quiz-navigation {
        padding: 8px 16px;
    }

    /* Feedback cards side-by-side */
    .feedback-comparison {
        flex-direction: row;
        gap: 8px;
    }

    .feedback-footer {
        flex-direction: row;
    }

    /* Hide drag handle in landscape (full screen) */
    .quiz-modal::before {
        display: none;
    }
}

/* Respect reduced motion preferences */
@media (prefers-reduced-motion: reduce) {
    .quiz-question-screen.swipe-left,
    .quiz-question-screen.swipe-right {
        animation: none;
    }

    /* Disable slide transitions */
    .quiz-question-screen,
    .quiz-question-screen.slide-exit-left,
    .quiz-question-screen.slide-enter-right,
    .quiz-question-screen.slide-enter-active {
        transition: none !important;
        transform: none !important;
        opacity: 1 !important;
    }
}
