* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

button, .checkbox-item, .character-card, .magic-item, .character-display, .coins,
.option-btn, .magic-cost, .wrong-answer-item {
    touch-action: manipulation;
    user-select: none;
    -webkit-user-select: none;
}

body {
    font-family: 'Arial', 'Microsoft YaHei', sans-serif;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    min-height: 100vh;
    min-height: 100dvh;   /* dvh accounts for iOS Safari's dynamic toolbar */
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 10px;
    padding-top: max(10px, env(safe-area-inset-top));
    padding-bottom: max(10px, env(safe-area-inset-bottom));
    padding-left: max(10px, env(safe-area-inset-left));
    padding-right: max(10px, env(safe-area-inset-right));
    overflow-x: hidden;
}

.container {
    background: white;
    border-radius: 20px;
    box-shadow: 0 20px 60px rgba(0,0,0,0.3);
    max-width: 500px;
    width: 100%;
    overflow: hidden;
}

.header {
    background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
    padding: 16px 20px;
    color: white;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}

.header h1 {
    font-size: 22px;
    margin: 0;
    cursor: pointer;
    user-select: none;
    flex: 1;
    text-align: center;
    min-width: 0;
}

.character-display {
    position: relative;
    background: rgba(255, 255, 255, 0.3);
    border-radius: 50%;
    width: 48px;
    height: 48px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 28px;
    cursor: pointer;
    transition: transform 0.3s, background 0.3s;
    border: 2px solid rgba(255, 255, 255, 0.5);
    flex-shrink: 0;
}

.character-display:hover {
    transform: scale(1.1);
    background: rgba(255, 255, 255, 0.5);
}

.coins {
    background: rgba(255, 215, 0, 0.3);
    padding: 6px 12px;
    border-radius: 50px;
    font-size: 16px;
    font-weight: bold;
    color: #fff;
    border: 2px solid rgba(255, 237, 78, 0.5);
    display: flex;
    align-items: center;
    gap: 5px;
    cursor: pointer;
    transition: transform 0.3s;
    flex-shrink: 0;
}

.coins:hover {
    transform: scale(1.1);
}

.content {
    padding: 20px;
}

.character-select, .grade-select, .quiz-area, .magic-shop, .quiz-summary {
    display: none;
}

.character-select.active, .grade-select.active, .quiz-area.active, .magic-shop.active, .quiz-summary.active {
    display: block;
}

.character-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 15px;
    margin-top: 20px;
}

.character-card {
    background: linear-gradient(135deg, #ffecd2 0%, #fcb69f 100%);
    border-radius: 15px;
    padding: 20px;
    text-align: center;
    cursor: pointer;
    transition: transform 0.3s, box-shadow 0.3s;
    border: 3px solid transparent;
}

.character-card:hover {
    transform: translateY(-5px);
    box-shadow: 0 10px 25px rgba(0,0,0,0.2);
}

.character-card.selected {
    border-color: #f5576c;
    background: linear-gradient(135deg, #a8edea 0%, #fed6e3 100%);
}

/* Secret (deity) characters — a golden look that sets them apart once unlocked. */
.character-card.secret-card {
    background: linear-gradient(135deg, #fceabb 0%, #f8b500 100%);
}

.character-card.secret-card.selected {
    border-color: #f5576c;
    background: linear-gradient(135deg, #ffe29f 0%, #ffa99f 100%);
}

.character-icon {
    font-size: 48px;
    margin-bottom: 10px;
}

.character-name {
    font-size: 18px;
    font-weight: bold;
    color: #333;
}

.character-ability {
    font-size: 12px;
    color: #666;
    margin-top: 5px;
}

.filter-section {
    margin: 20px 0;
}

.filter-label {
    font-weight: bold;
    margin-bottom: 10px;
    color: #333;
}

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

.checkbox-item {
    background: #f0f0f0;
    padding: 8px 15px;
    border-radius: 20px;
    cursor: pointer;
    transition: all 0.3s;
    user-select: none;
}

.checkbox-item.selected {
    background: #667eea;
    color: white;
}

.btn {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    border: none;
    padding: 8px 20px;
    font-size: 16px;
    cursor: pointer;
    transition: transform 0.3s;
    width: 100%;
    margin-top: 20px;
}

.btn:hover {
    transform: scale(1.05);
}

.btn:focus-visible,
.character-display:focus-visible,
.coins:focus-visible,
.character-card:focus-visible,
.checkbox-item:focus-visible,
.option-btn:focus-visible,
.quiz-character:focus-visible,
.magic-cost:focus-visible,
.wrong-answer-item:focus-visible,
.textbook-speaker:focus-visible {
    outline: 3px solid #ffca28;
    outline-offset: 3px;
    box-shadow: 0 0 0 4px rgba(102, 126, 234, 0.25);
}

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

/* Magic shop entry button — tertiary action, outlined so it doesn't compete
   with the primary button on each screen. */
.btn-magic {
    background: transparent;
    color: #f5576c;
    border: 2px solid #f5576c;
    margin-top: 10px;
}

.btn-magic:hover {
    background: rgba(245, 87, 108, 0.1);
    color: #f5576c;
}

.btn-magic:disabled {
    opacity: 0.4;
    border-color: #ccc;
    color: #999;
}

/* Section heading shared by all screens. */
.section-title {
    text-align: center;
    margin-bottom: 20px;
}

/* Initially-hidden filter sections (units, lessons). */
.hidden {
    display: none;
}

.quiz-stats {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 10px;
    margin-bottom: 10px;
}

.quiz-stat {
    text-align: center;
    padding: 10px;
    background: #f5f5f5;
    border-radius: 10px;
}

.quiz-stat-value {
    font-size: 20px;
    font-weight: bold;
    margin-bottom: 3px;
}

.quiz-stat-value.correct {
    color: #4caf50;
}

.quiz-stat-value.incorrect {
    color: #f44336;
}

.quiz-stat-value.remaining {
    color: #999;
}

.quiz-stat-value.total {
    color: #667eea;
}

.quiz-stat-label {
    font-size: 11px;
    color: #666;
}

.progress-bar-container {
    width: 100%;
    height: 15px;
    background: #e0e0e0;
    border-radius: 15px;
    overflow: hidden;
    margin-bottom: 15px;
    display: flex;
}

.progress-segment {
    height: 100%;
    transition: width 0.3s ease;
    width: 0;
}

.progress-remaining {
    width: 100%;
    background: #e0e0e0;
}

.progress-correct {
    background: linear-gradient(90deg, #4caf50 0%, #66bb6a 100%);
}

.progress-incorrect {
    background: linear-gradient(90deg, #f44336 0%, #ef5350 100%);
}

/* The quizzed character is tappable: tap to swap it in-place for the original
   textbook sentence, tap again to swap back. */
.quiz-character {
    font-size: 55px;
    text-align: center;
    margin: 10px 0;
    color: #333;
    text-shadow: 2px 2px 4px rgba(0,0,0,0.1);
    cursor: pointer;
    position: relative;
    border-radius: 14px;
    padding: 10px;
    transition: background 0.2s;
    -webkit-tap-highlight-color: transparent;
}

.quiz-character:hover {
    background: rgba(102, 126, 234, 0.06);
}

/* Text mode: showing the textbook sentence in place of the single character. */
.quiz-character.showing-text {
    font-size: 24px;
    line-height: 1.6;
    text-shadow: none;
    text-align: left;
    background: #f4f8f4;
    border: 1px solid #d6e4dc;
    border-left: 4px solid #5a8f7b;
    display: flex;
    align-items: flex-start;
    gap: 10px;
}

.quiz-character .textbook-phrase { flex: 1; color: #333; }
.quiz-character .textbook-highlight { color: #f5576c; font-weight: bold; }
.quiz-character .textbook-speaker {
    flex-shrink: 0;
    cursor: pointer;
    font-size: 24px;
    user-select: none;
}
.quiz-character .textbook-empty { flex: 1; font-size: 18px; color: #888; }

/* Swap-in animation when toggling between character and sentence. */
.quiz-character.swap-anim { animation: charSwap 0.32s ease; }

@keyframes charSwap {
    0%   { opacity: 0; transform: perspective(400px) rotateX(85deg) scale(0.92); }
    100% { opacity: 1; transform: perspective(400px) rotateX(0) scale(1); }
}

/* Idle hint: pulsing ripple rings + a gentle bob, nudging the user that the
   character can be tapped to reveal the textbook sentence. */
.quiz-character.hint-tap { animation: hintBob 1.8s ease-in-out infinite; }

.quiz-character.hint-tap::before,
.quiz-character.hint-tap::after {
    content: '';
    position: absolute;
    left: 50%;
    top: 50%;
    width: 90px;
    height: 90px;
    margin-left: -45px;
    margin-top: -45px;
    border: 2px solid rgba(102, 126, 234, 0.55);
    border-radius: 50%;
    transform: scale(0.4);
    opacity: 0;
    pointer-events: none;
    animation: tapRipple 1.8s ease-out infinite;
}

.quiz-character.hint-tap::after { animation-delay: 0.9s; }

@keyframes tapRipple {
    0%   { transform: scale(0.4); opacity: 0.7; }
    100% { transform: scale(1.7); opacity: 0; }
}

@keyframes hintBob {
    0%, 100% { transform: scale(1); }
    50%      { transform: scale(1.08); }
}

/* Reverse mode (看义选字): the prompt shows the pinyin + meaning clue instead
   of a tappable character. */
.quiz-character.clue-mode {
    cursor: default;
    font-size: 22px;
    text-shadow: none;
    padding: 14px 10px;
}
.quiz-character.clue-mode:hover { background: transparent; }
.clue-pinyin { display: block; font-size: 30px; font-weight: bold; color: #667eea; }
.clue-english { display: block; font-size: 18px; color: #666; margin-top: 6px; }

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

/* Reverse mode answer options are big characters, laid out two-up. */
.quiz-options.char-grid {
    grid-template-columns: 1fr 1fr;
}
.option-btn.char-option {
    text-align: center;
    padding: 18px 15px;
}
.option-btn.char-option.correct,
.option-btn.char-option.incorrect {
    padding-left: 15px;   /* keep the glyph centered; the ✓/✗ sits at the edge */
}
.char-option-glyph {
    font-size: 40px;
    color: #333;
}
.option-btn.char-option.correct .char-option-glyph,
.option-btn.char-option.incorrect .char-option-glyph {
    color: white;
}

.option-btn {
    background: white;
    border: 2px solid #ddd;
    padding: 15px;
    border-radius: 10px;
    cursor: pointer;
    transition: all 0.3s;
    text-align: left;
    position: relative;
}

.option-btn:hover {
    border-color: #667eea;
    transform: translateX(5px);
}

.option-btn.correct,
.option-btn.incorrect {
    padding-left: 44px;
}

.option-btn.correct {
    background: #4caf50;
    color: white;
    border-color: #4caf50;
    animation: pulse 0.5s;
}

.option-btn.correct::before {
    content: '✓';
    position: absolute;
    left: 14px;
    top: 50%;
    transform: translateY(-50%);
    font-size: 22px;
    font-weight: bold;
    color: white;
}

.option-btn.incorrect {
    background: #f44336;
    color: white;
    border-color: #f44336;
    animation: shake 0.5s;
}

.option-btn.incorrect::before {
    content: '✗';
    position: absolute;
    left: 14px;
    top: 50%;
    transform: translateY(-50%);
    font-size: 22px;
    font-weight: bold;
    color: white;
}

.option-btn:disabled {
    cursor: not-allowed;
}

.pinyin {
    font-size: 16px;
    font-weight: bold;
    color: #667eea;
}

.option-btn.correct .pinyin,
.option-btn.incorrect .pinyin {
    color: white;
}

.english {
    font-size: 14px;
    color: #666;
    margin-top: 5px;
}

.option-btn.correct .english,
.option-btn.incorrect .english {
    color: rgba(255,255,255,0.9);
}

/* During an unanswered question, keep the path focused: no next/back/shop
   controls competing with the answer choices. They return after an answer. */
.next-question,
.quiz-area:not(.answered) .secondary-quiz-action {
    display: none;
}

.next-question.show {
    display: block;
}

.magic-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 15px;
    margin-top: 20px;
}

.magic-item {
    background: linear-gradient(135deg, #ffecd2 0%, #fcb69f 100%);
    border-radius: 15px;
    padding: 15px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    transition: all 0.3s;
}

.magic-item.unlocked {
    background: linear-gradient(135deg, #a8edea 0%, #fed6e3 100%);
    border: 2px solid #f5576c;
}

.magic-info {
    flex: 1;
}

.magic-title {
    font-weight: bold;
    font-size: 16px;
    color: #333;
}

.magic-desc {
    font-size: 12px;
    color: #666;
    margin-top: 5px;
}

.magic-cost {
    background: #667eea;
    color: white;
    border: none;
    padding: 8px 15px;
    border-radius: 20px;
    font: inherit;
    font-weight: bold;
    cursor: pointer;
    transition: transform 0.3s;
}

.magic-cost:hover {
    transform: scale(1.1);
}

.magic-cost.disabled {
    background: #ccc;
    cursor: not-allowed;
}

.magic-cost.use-magic {
    background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
}

.summary-stats {
    display: flex;
    justify-content: space-around;
    margin: 20px 0;
    padding: 20px;
    background: #f5f5f5;
    border-radius: 10px;
}

.summary-stat {
    text-align: center;
}

.summary-value {
    font-size: 32px;
    font-weight: bold;
    color: #667eea;
}

.summary-value.correct-color {
    color: #4caf50;
}

.summary-value.incorrect-color {
    color: #f44336;
}

.summary-label {
    font-size: 14px;
    color: #666;
    margin-top: 5px;
}

.wrong-answers-section {
    margin-top: 20px;
}

.wrong-answers-title {
    font-weight: bold;
    font-size: 18px;
    color: #333;
    margin-bottom: 15px;
    text-align: center;
}

.wrong-answer-item {
    background: #fff5f5;
    border-left: 4px solid #f44336;
    padding: 15px 64px 15px 15px;
    margin-bottom: 10px;
    border-radius: 8px;
    position: relative;
    cursor: pointer;
    transition: all 0.3s;
}

.wrong-answer-item:hover {
    background: #ffe5e5;
    transform: translateX(5px);
}

.wrong-answer-char {
    font-size: 36px;
    margin-bottom: 8px;
    color: #333;
}

.wrong-answer-speaker {
    position: absolute;
    right: 15px;
    top: 50%;
    transform: translateY(-50%);
    font-size: 24px;
    color: #f44336;
    padding: 10px 14px;
    margin: -10px -14px -10px 0;
    min-width: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.wrong-answer-pinyin {
    font-size: 16px;
    font-weight: bold;
    color: #f44336;
    margin-bottom: 4px;
}

.wrong-answer-english {
    font-size: 14px;
    color: #666;
}

.wrong-answer-context {
    font-size: 12px;
    color: #8a6d3b;
    margin-top: 6px;
}

.wrong-answer-phrase {
    font-size: 13px;
    color: #555;
    line-height: 1.5;
    margin-top: 6px;
}

/* Shown when the quiz finishes with zero wrong answers. */
.perfect-message {
    text-align: center;
    font-size: 20px;
    color: #4caf50;
    margin: 20px 0;
}

/* Shown when loading the character set fails. */
.loading-error {
    color: #f44336;
    text-align: center;
}

.celebration {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 100px;
    z-index: 1000;
    pointer-events: none;
    opacity: 0;
    animation: celebrate 1s ease-out;
}

@keyframes celebrate {
    0% { opacity: 1; transform: translate(-50%, -50%) scale(0) rotate(0deg); }
    50% { opacity: 1; transform: translate(-50%, -50%) scale(1.2) rotate(180deg); }
    100% { opacity: 0; transform: translate(-50%, -50%) scale(1) rotate(360deg); }
}

@keyframes pulse {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.05); }
}

@keyframes shake {
    0%, 100% { transform: translateX(0); }
    25% { transform: translateX(-10px); }
    75% { transform: translateX(10px); }
}

.back-btn {
    background: #999;
    padding: 8px 20px;
    font-size: 16px;
    margin-top: 10px;
}

.magic-animation {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 999;
    display: none;
}

.magic-animation.active {
    display: block;
}

.particle {
    position: absolute;
    font-size: 30px;
    animation: float 5s ease-out forwards;
}

@keyframes float {
    0% {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
    100% {
        opacity: 0;
        transform: translateY(-200px) scale(1.5);
    }
}

.magic-message {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
    color: white;
    padding: 30px 50px;
    border-radius: 20px;
    font-size: 24px;
    font-weight: bold;
    z-index: 1001;
    pointer-events: none;
    opacity: 0;
    box-shadow: 0 10px 40px rgba(0,0,0,0.3);
}

.magic-message.show {
    animation: showMessage 5s ease-out;
}

@keyframes showMessage {
    0% { opacity: 0; transform: translate(-50%, -50%) scale(0.5); }
    10% { opacity: 1; transform: translate(-50%, -50%) scale(1.1); }
    20% { transform: translate(-50%, -50%) scale(1); }
    80% { opacity: 1; transform: translate(-50%, -50%) scale(1); }
    100% { opacity: 0; transform: translate(-50%, -50%) scale(0.5); }
}

.speaker-icon {
    position: absolute;
    right: 0;
    top: 0;
    bottom: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 14px;
    min-width: 44px;
    font-size: 20px;
    cursor: pointer;
}

.character-display::after {
    content: '↻';
    position: absolute;
    bottom: -4px;
    right: -4px;
    background: white;
    color: #f5576c;
    font-size: 12px;
    font-weight: bold;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2px 4px rgba(0,0,0,0.2);
    pointer-events: none;
}

.character-display.hint-pulse {
    animation: hintPulse 1.4s ease-in-out infinite;
}

@keyframes hintPulse {
    0%, 100% { box-shadow: 0 0 0 0 rgba(255, 255, 255, 0.7); }
    50% { box-shadow: 0 0 0 12px rgba(255, 255, 255, 0); }
}

.toast-container {
    position: fixed;
    left: 50%;
    bottom: max(20px, env(safe-area-inset-bottom, 20px));
    transform: translateX(-50%);
    z-index: 2500;
    display: flex;
    flex-direction: column;
    gap: 8px;
    align-items: center;
    pointer-events: none;
    width: calc(100% - 40px);
    max-width: 420px;
}

.toast {
    background: rgba(40, 40, 50, 0.95);
    color: white;
    padding: 12px 20px;
    border-radius: 12px;
    font-size: 15px;
    box-shadow: 0 6px 20px rgba(0,0,0,0.25);
    opacity: 0;
    transform: translateY(20px);
    transition: opacity 0.25s ease, transform 0.25s ease;
    pointer-events: auto;
    text-align: center;
    max-width: 100%;
}

.toast.show {
    opacity: 1;
    transform: translateY(0);
}

.toast.warn { background: rgba(230, 140, 30, 0.96); }
.toast.error { background: rgba(200, 50, 50, 0.96); }
.toast.success { background: rgba(60, 160, 80, 0.96); }

.loading-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
    padding: 40px 20px;
    color: #667eea;
}

.loading-spinner {
    width: 40px;
    height: 40px;
    border: 4px solid #e0e0e0;
    border-top-color: #667eea;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

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

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.001ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.001ms !important;
    }
    .loading-spinner { animation: none; border-top-color: transparent; }
    .character-display.hint-pulse { animation: none; }
    .particle { display: none; }
}

@media (orientation: landscape) and (max-height: 500px) {
    body { align-items: flex-start; }
    .container { margin: 10px 0; }
}

.prompt-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 2000;
    padding: 20px;
}

.prompt-overlay.active {
    display: flex;
}

.prompt-box {
    background: white;
    border-radius: 16px;
    padding: 24px;
    width: 100%;
    max-width: 360px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}

.prompt-title {
    font-size: 18px;
    font-weight: bold;
    color: #333;
    margin-bottom: 16px;
    text-align: center;
}

.prompt-input {
    width: 100%;
    padding: 12px 14px;
    font-size: 16px;
    border: 2px solid #ddd;
    border-radius: 10px;
    outline: none;
    transition: border-color 0.2s;
}

.prompt-input:focus {
    border-color: #667eea;
}

.prompt-buttons {
    display: flex;
    gap: 10px;
    margin-top: 16px;
}

.prompt-buttons .btn {
    margin-top: 0;
}
