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

:root {
    --bg: #fafaf8;
    --text: #1a1a1a;
    --primary: #2563eb;
    --primary-light: #dbeafe;
    --vocab-bg: #dbeafe;
    --vocab-border: #3b82f6;
    --grammar-bg: #fef3c7;
    --grammar-border: #f59e0b;
    --correct: #16a34a;
    --correct-bg: #dcfce7;
    --wrong: #dc2626;
    --wrong-bg: #fee2e2;
    --muted: #6b7280;
    --card-bg: #ffffff;
    --border: #e5e7eb;
    --radius: 10px;
    --shadow: 0 1px 3px rgba(0,0,0,.08);
    --shadow-lg: 0 4px 12px rgba(0,0,0,.1);
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #111113;
        --text: #e4e4e7;
        --card-bg: #1c1c1f;
        --border: #2e2e32;
        --muted: #9ca3af;
        --primary-light: #1e3a5f;
        --vocab-bg: #1e3a5f;
        --grammar-bg: #3b2f1a;
        --correct-bg: #14352a;
        --wrong-bg: #3b1a1a;
        --shadow: 0 1px 3px rgba(0,0,0,.3);
        --shadow-lg: 0 4px 12px rgba(0,0,0,.4);
    }
}

html { font-size: 16px; -webkit-text-size-adjust: 100%; }
body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
    background: var(--bg);
    color: var(--text);
    min-height: 100dvh;
    overflow-x: hidden;
}

/* ── Header ── */
.header {
    position: sticky; top: 0; z-index: 100;
    background: var(--primary);
    color: #fff;
    padding: 10px 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
}
.header-title { font-size: 1.1rem; font-weight: 700; letter-spacing: -.02em; }
.header-stats { display: flex; gap: 12px; font-size: .82rem; font-weight: 600; }

/* ── Bottom Tab Bar ── */
.tab-bar {
    position: fixed; bottom: 0; left: 0; right: 0; z-index: 100;
    display: flex;
    background: var(--card-bg);
    border-top: 1px solid var(--border);
    padding-bottom: env(safe-area-inset-bottom, 0);
}
.tab {
    flex: 1;
    display: flex; flex-direction: column; align-items: center; gap: 2px;
    padding: 8px 4px 6px;
    border: none; background: none;
    color: var(--muted);
    font-size: .65rem; font-weight: 500;
    cursor: pointer;
    min-height: 52px;
    -webkit-tap-highlight-color: transparent;
}
.tab-icon { font-size: 1.25rem; line-height: 1; }
.tab.active { color: var(--primary); font-weight: 700; }

/* ── Main Content ── */
.content {
    flex: 1;
    padding: 12px 12px 72px;
    max-width: 720px;
    margin: 0 auto;
    width: 100%;
}
@media (min-width: 480px) { .content { padding: 16px 16px 80px; } }

/* ── Cards ── */
.card {
    background: var(--card-bg);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 14px;
    margin-bottom: 10px;
}
.card h3 { font-size: .95rem; margin-bottom: 6px; }

/* ── Section List ── */
.section-item {
    display: flex; align-items: center; gap: 10px;
    padding: 12px 14px;
    background: var(--card-bg);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    margin-bottom: 8px;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
}
.section-item:active { transform: scale(.98); }
.section-num {
    width: 30px; height: 30px;
    border-radius: 50%;
    background: var(--border);
    color: var(--muted);
    display: flex; align-items: center; justify-content: center;
    font-weight: 700; font-size: .8rem;
    flex-shrink: 0;
}
.section-num.done { background: var(--correct); color: #fff; }
.section-info { flex: 1; min-width: 0; }
.section-title { font-weight: 600; font-size: .9rem; }
.section-summary { font-size: .75rem; color: var(--muted); margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.section-arrow { color: var(--muted); font-size: 1.1rem; flex-shrink: 0; }

/* ── Back button ── */
.back-btn {
    display: inline-flex; align-items: center; gap: 4px;
    color: var(--primary); font-size: .88rem; font-weight: 600;
    border: none; background: none; cursor: pointer;
    padding: 8px 0; margin-bottom: 8px;
    min-height: 44px;
}

/* ── Reading View ── */
.section-header {
    display: flex; align-items: center; gap: 8px;
    margin-bottom: 12px;
}
.section-header h2 { flex: 1; font-size: 1.05rem; font-weight: 700; }
.tts-btn {
    width: 44px; height: 44px;
    border: none; border-radius: 50%;
    background: var(--primary-light);
    color: var(--primary);
    font-size: 1.2rem;
    cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    flex-shrink: 0;
}
.tts-btn.playing { background: var(--wrong-bg); color: var(--wrong); }
.tts-settings-btn {
    width: 36px; height: 36px;
    border: none; border-radius: 50%;
    background: var(--bg-alt, #f0f0f0);
    color: var(--text-muted, #888);
    font-size: 1rem;
    cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    flex-shrink: 0;
}
.tts-settings-btn:hover { background: var(--primary-light); color: var(--primary); }
.tts-settings h3 { margin: 0 0 12px; font-size: 1rem; }
.tts-label { display: block; font-size: 0.85rem; color: var(--text-muted, #888); margin-bottom: 4px; }
.tts-select {
    width: 100%; padding: 8px; border-radius: var(--radius, 8px);
    border: 1px solid var(--border, #ddd); font-size: 0.9rem;
    background: var(--card-bg, #fff); color: var(--text, #222);
}
.tts-range { width: 100%; margin-top: 4px; accent-color: var(--primary); }
.tts-test-btn {
    padding: 10px 20px; border: none; border-radius: var(--radius, 8px);
    background: var(--primary); color: #fff;
    font-size: 0.9rem; font-weight: 600; cursor: pointer; width: 100%;
}

.reading-text {
    font-size: 1rem; line-height: 1.75;
    background: var(--card-bg);
    border-radius: var(--radius);
    padding: 16px;
    box-shadow: var(--shadow);
}
.reading-text p { margin-bottom: .8em; }
.reading-text p:last-child { margin-bottom: 0; }
.reading-text .speaker {
    font-weight: 700;
    color: var(--primary);
    display: block;
    margin-top: .6em;
    font-size: .9rem;
    letter-spacing: .03em;
}
.reading-text .speaker-lui { color: var(--grammar-border); }

.vocab-highlight {
    background: var(--vocab-bg);
    border-bottom: 2px solid var(--vocab-border);
    border-radius: 3px;
    padding: 0 2px;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
}
.grammar-highlight {
    border-bottom: 2px dashed var(--grammar-border);
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
}
.tts-active { background: #fef08a; border-radius: 3px; }

.para-tts {
    opacity: 0; width: 28px; height: 28px;
    border: none; background: none;
    font-size: .9rem; cursor: pointer;
    vertical-align: middle; margin-left: 4px;
    transition: opacity .2s;
}
.reading-text p:hover .para-tts,
.reading-text p:focus-within .para-tts { opacity: .6; }

/* ── Summary collapsible ── */
.summary-toggle {
    width: 100%;
    padding: 12px 14px;
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    text-align: left;
    font-size: .88rem; font-weight: 600;
    color: var(--text);
    cursor: pointer;
    margin: 12px 0 8px;
    display: flex; align-items: center; justify-content: space-between;
}
.summary-body {
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-top: none;
    border-radius: 0 0 var(--radius) var(--radius);
    padding: 12px 14px;
    font-size: .88rem;
    line-height: 1.6;
    color: var(--muted);
    margin-top: -10px;
    margin-bottom: 12px;
}

/* ── Popup / Tooltip ── */
.popup-overlay {
    position: fixed; inset: 0; z-index: 200;
    background: rgba(0,0,0,.3);
    display: flex; align-items: flex-end; justify-content: center;
    padding: 16px;
}
.popup {
    background: var(--card-bg);
    border-radius: 14px 14px 14px 14px;
    padding: 20px;
    max-width: 400px; width: 100%;
    box-shadow: var(--shadow-lg);
    max-height: 60vh;
    overflow-y: auto;
}
.popup-word { font-size: 1.3rem; font-weight: 700; }
.popup-pos { font-size: .75rem; color: var(--muted); margin-left: 6px; }
.popup-de { font-size: 1.05rem; margin-top: 6px; }
.popup-divider { border: none; border-top: 1px solid var(--border); margin: 10px 0; }
.popup-context { font-size: .85rem; color: var(--muted); line-height: 1.5; }
.popup-tts {
    margin-top: 10px;
    width: 44px; height: 44px;
    border: none; border-radius: 50%;
    background: var(--primary-light);
    color: var(--primary);
    font-size: 1.1rem; cursor: pointer;
}
.popup-phen {
    font-size: .7rem; text-transform: uppercase; font-weight: 700;
    background: var(--grammar-bg); color: var(--grammar-border);
    padding: 2px 8px; border-radius: 99px;
    display: inline-block; margin-bottom: 8px;
}
.popup-rule {
    background: var(--primary-light);
    border-left: 3px solid var(--primary);
    padding: 8px 10px; border-radius: 0 6px 6px 0;
    font-size: .82rem; line-height: 1.4;
    margin-top: 8px;
}

/* ── Quiz (one-at-a-time) ── */
.quiz-wrap { text-align: center; }
.quiz-progress { font-size: .8rem; color: var(--muted); margin-bottom: 12px; }
.quiz-question {
    font-size: 1rem; font-weight: 600; line-height: 1.5;
    margin-bottom: 16px; text-align: left;
}
.quiz-opt {
    display: block; width: 100%;
    padding: 14px 16px; margin-bottom: 8px;
    border: 2px solid var(--border);
    border-radius: var(--radius);
    background: var(--card-bg);
    text-align: left; font-size: .92rem;
    color: var(--text);
    cursor: pointer; min-height: 48px;
    -webkit-tap-highlight-color: transparent;
    transition: border-color .15s;
}
.quiz-opt:active { border-color: var(--primary); }
.quiz-opt.correct { border-color: var(--correct); background: var(--correct-bg); }
.quiz-opt.wrong { border-color: var(--wrong); background: var(--wrong-bg); }
.quiz-opt.disabled { pointer-events: none; }
.quiz-feedback {
    padding: 12px; border-radius: var(--radius);
    font-size: .85rem; line-height: 1.5;
    margin-top: 8px;
}
.quiz-feedback.correct { background: var(--correct-bg); border-left: 3px solid var(--correct); }
.quiz-feedback.wrong { background: var(--wrong-bg); border-left: 3px solid var(--wrong); }
.quiz-result {
    text-align: center; padding: 24px 0;
}
.quiz-result .big { font-size: 2rem; font-weight: 700; }
.quiz-start-btn {
    display: block; width: 100%;
    padding: 14px; margin-top: 16px;
    border: none; border-radius: var(--radius);
    background: var(--primary); color: #fff;
    font-size: .95rem; font-weight: 600;
    cursor: pointer; min-height: 48px;
}

/* ── Vocab Tab ── */
.vocab-overview {
    text-align: center; padding: 20px 0;
}
.vocab-due-count { font-size: 2.2rem; font-weight: 700; color: var(--primary); }
.vocab-due-label { font-size: .88rem; color: var(--muted); margin-top: 4px; }
.box-bars {
    display: flex; gap: 6px; align-items: flex-end;
    height: 80px; margin: 16px 0;
    justify-content: center;
}
.box-bar {
    width: 40px; border-radius: 4px 4px 0 0;
    background: var(--primary);
    min-height: 4px;
    position: relative;
    transition: height .3s;
}
.box-bar-label {
    position: absolute; bottom: -18px; left: 0; right: 0;
    font-size: .65rem; color: var(--muted); text-align: center;
}
.box-bar-count {
    position: absolute; top: -16px; left: 0; right: 0;
    font-size: .65rem; color: var(--text); text-align: center; font-weight: 600;
}
.learn-btn {
    display: block; width: 100%; max-width: 300px; margin: 16px auto 0;
    padding: 14px; border: none; border-radius: var(--radius);
    background: var(--primary); color: #fff;
    font-size: .95rem; font-weight: 600;
    cursor: pointer; min-height: 48px;
}
.learn-btn:disabled { opacity: .4; cursor: default; }

.flashcard {
    background: var(--card-bg);
    border-radius: 14px;
    box-shadow: var(--shadow-lg);
    padding: 24px 20px;
    text-align: center;
    margin-bottom: 12px;
    min-height: 160px;
    display: flex; flex-direction: column;
    align-items: center; justify-content: center;
}
.flashcard-word { font-size: 1.6rem; font-weight: 700; margin-bottom: 4px; }
.flashcard-pos {
    font-size: .7rem; background: var(--primary); color: #fff;
    padding: 2px 8px; border-radius: 99px;
    margin-bottom: 10px;
}
.flashcard-example {
    font-size: .82rem; color: var(--muted); font-style: italic;
    max-width: 360px; line-height: 1.5; margin-top: 6px;
}

/* ── Grammar Tab ── */
.phen-item {
    display: flex; align-items: center; justify-content: space-between;
    padding: 14px;
    background: var(--card-bg);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    margin-bottom: 8px;
    cursor: pointer;
}
.phen-item:active { transform: scale(.98); }
.phen-tag {
    font-size: .72rem; text-transform: uppercase; font-weight: 700;
    background: var(--grammar-bg); color: #92400e;
    padding: 3px 10px; border-radius: 99px;
}
@media (prefers-color-scheme: dark) {
    .phen-tag { color: var(--grammar-border); }
}
.phen-count { font-size: .8rem; color: var(--muted); }

.grammar-card {
    background: var(--card-bg);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 14px; margin-bottom: 10px;
}
.grammar-sentence {
    font-style: italic; font-size: .95rem; line-height: 1.6;
    margin-bottom: 8px;
}
.grammar-explanation {
    font-size: .82rem; color: var(--muted); line-height: 1.5;
    margin-bottom: 8px;
}
.grammar-rule {
    background: var(--primary-light);
    border-left: 3px solid var(--primary);
    padding: 8px 10px; border-radius: 0 6px 6px 0;
    font-size: .82rem; line-height: 1.4;
}

/* ── Exercise ── */
.exercise {
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 14px; margin-top: 10px;
}
.ex-type {
    font-size: .65rem; text-transform: uppercase; font-weight: 700;
    background: #e0e7ff; color: var(--primary);
    padding: 2px 8px; border-radius: 99px;
    display: inline-block; margin-bottom: 8px;
}
@media (prefers-color-scheme: dark) {
    .ex-type { background: #1e3a5f; }
}
.ex-prompt { font-size: .9rem; line-height: 1.5; margin-bottom: 10px; }
.ex-input {
    width: 100%; padding: 10px 12px;
    border: 2px solid var(--border);
    border-radius: 8px; font-size: 1rem;
    background: var(--bg); color: var(--text);
    outline: none;
}
.ex-input:focus { border-color: var(--primary); }
.ex-input.correct { border-color: var(--correct); background: var(--correct-bg); }
.ex-input.wrong { border-color: var(--wrong); background: var(--wrong-bg); }
.ex-check {
    display: block; width: 100%;
    padding: 11px; margin-top: 8px;
    border: none; border-radius: var(--radius);
    background: var(--primary); color: #fff;
    font-weight: 600; font-size: .9rem;
    cursor: pointer; min-height: 44px;
}
.ex-check:disabled { opacity: .4; }
.ex-hint {
    font-size: .78rem; color: var(--muted); font-style: italic;
    margin-top: 6px; cursor: pointer;
}
.ex-hint::before { content: '💡 '; }
.ex-feedback {
    padding: 10px; border-radius: var(--radius);
    font-size: .82rem; line-height: 1.4; margin-top: 8px;
}
.ex-feedback.correct { background: var(--correct-bg); border-left: 3px solid var(--correct); }
.ex-feedback.wrong { background: var(--wrong-bg); border-left: 3px solid var(--wrong); }

/* ── Progress Tab ── */
.stat-grid {
    display: grid; grid-template-columns: 1fr 1fr;
    gap: 10px; margin-bottom: 14px;
}
.stat-card {
    background: var(--card-bg);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 14px; text-align: center;
}
.stat-value { font-size: 1.8rem; font-weight: 700; color: var(--primary); }
.stat-label { font-size: .75rem; color: var(--muted); margin-top: 2px; }

.progress-bar { background: var(--border); border-radius: 99px; height: 8px; overflow: hidden; margin-top: 6px; }
.progress-fill { height: 100%; background: var(--primary); border-radius: 99px; transition: width .3s; }

.section-check-list { list-style: none; }
.section-check-list li {
    padding: 6px 0; font-size: .85rem;
    border-bottom: 1px solid var(--border);
    display: flex; align-items: center; gap: 8px;
}

/* ── Text Indicator ── */
.text-indicator {
    background: var(--card-bg);
    border-bottom: 1px solid var(--border);
    padding: 6px 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: .8rem;
    color: var(--muted);
}
.text-indicator button {
    border: none; background: none;
    color: var(--primary); font-size: 1.1rem;
    cursor: pointer; padding: 4px 8px; min-height: 36px;
}

/* ── Library Card ── */
.library-card {
    background: var(--card-bg);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 16px; margin-bottom: 10px;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
}
.library-card:active { transform: scale(.98); }
.level-badge {
    display: inline-block;
    font-size: .65rem; font-weight: 700;
    color: #fff; padding: 2px 8px; border-radius: 99px;
}

/* ── Utility ── */
.hidden { display: none !important; }
.text-muted { color: var(--muted); }
.text-sm { font-size: .82rem; }
.mt-8 { margin-top: 8px; }
.mt-16 { margin-top: 16px; }
.mb-8 { margin-bottom: 8px; }
.mb-16 { margin-bottom: 16px; }
.text-center { text-align: center; }
