/* =========================================================
   中文故事學堂 UI v2
   Mobile-first
   ========================================================= */

:root {
    --primary: #6c4cff;
    --primary-dark: #5034d7;
    --primary-soft: #eeeaff;

    --accent: #ffb020;
    --success: #15965f;
    --danger: #d14343;

    --background: #f6f7fb;
    --surface: #ffffff;
    --surface-soft: #f8f7ff;

    --text: #202238;
    --muted: #6d7285;
    --border: #e7e8f0;

    --shadow-sm: 0 4px 16px rgba(35, 38, 70, 0.06);
    --shadow-md: 0 12px 35px rgba(35, 38, 70, 0.10);

    --radius-sm: 12px;
    --radius-md: 18px;
    --radius-lg: 26px;

    --page-width: 1080px;

    --safe-top: env(safe-area-inset-top, 0px);
    --safe-right: env(safe-area-inset-right, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --safe-left: env(safe-area-inset-left, 0px);
}

/* ---------- Reset ---------- */

*,
*::before,
*::after {
    box-sizing: border-box;
}

html {
    min-width: 320px;
    background: var(--background);
    font-size: 16px;
    scroll-behavior: smooth;
    -webkit-text-size-adjust: 100%;
}

body {
    min-height: 100vh;
    min-height: 100dvh;
    margin: 0;
    background: var(--background);
    color: var(--text);
    font-family:
        -apple-system,
        BlinkMacSystemFont,
        "Segoe UI",
        "PingFang TC",
        "Noto Sans TC",
        "Microsoft JhengHei",
        sans-serif;
    line-height: 1.6;
    overflow-wrap: anywhere;
}

img {
    display: block;
    max-width: 100%;
}

a {
    color: inherit;
    text-decoration: none;
}

button,
input,
textarea,
select {
    font: inherit;
}

button,
a,
label {
    -webkit-tap-highlight-color: transparent;
}

button,
.primary-link,
.option {
    touch-action: manipulation;
}

button {
    cursor: pointer;
}

h1,
h2,
h3,
p {
    overflow-wrap: break-word;
}

h1 {
    margin: 0;
    font-size: clamp(1.8rem, 7vw, 3.2rem);
    line-height: 1.2;
    letter-spacing: -0.025em;
}

h2 {
    font-size: clamp(1.35rem, 5vw, 1.9rem);
    line-height: 1.3;
}

h3 {
    line-height: 1.5;
}

/* ---------- Common ---------- */

.eyebrow {
    margin: 0 0 6px;
    color: var(--primary);
    font-size: 0.78rem;
    font-weight: 900;
    letter-spacing: 0.1em;
    text-transform: uppercase;
}

.logo {
    display: grid;
    width: 64px;
    height: 64px;
    margin: 0 auto 18px;
    border-radius: 20px;
    background:
        linear-gradient(
            145deg,
            var(--primary),
            #8d74ff
        );
    color: #ffffff;
    place-items: center;
    font-size: 2rem;
    font-weight: 900;
    box-shadow: 0 12px 28px rgba(108, 76, 255, 0.28);
}

.primary-button,
.primary-link {
    display: inline-flex;
    width: 100%;
    min-height: 52px;
    padding: 12px 22px;
    border: 0;
    border-radius: 16px;
    background:
        linear-gradient(
            135deg,
            var(--primary),
            #8063ff
        );
    color: #ffffff;
    align-items: center;
    justify-content: center;
    font-weight: 850;
    line-height: 1.3;
    text-align: center;
    box-shadow: 0 8px 20px rgba(108, 76, 255, 0.22);
    transition:
        transform 150ms ease,
        box-shadow 150ms ease,
        opacity 150ms ease;
}

.primary-button:active,
.primary-link:active {
    transform: scale(0.98);
    box-shadow: 0 4px 12px rgba(108, 76, 255, 0.18);
}

.primary-button:disabled {
    cursor: not-allowed;
    opacity: 0.65;
}

.alert {
    margin-bottom: 18px;
    padding: 14px 16px;
    border-radius: 14px;
    font-size: 0.94rem;
    line-height: 1.65;
}

.alert ul {
    margin: 8px 0 0;
    padding-left: 20px;
}

.alert-success {
    background: #e8f8f0;
    color: #09683f;
}

.alert-error {
    background: #ffeded;
    color: #9c2929;
}

/* =========================================================
   Landing page
   ========================================================= */

.landing {
    display: grid;
    min-height: 100vh;
    min-height: 100dvh;
    padding:
        calc(24px + var(--safe-top))
        calc(16px + var(--safe-right))
        calc(24px + var(--safe-bottom))
        calc(16px + var(--safe-left));
    place-items: center;
    background:
        radial-gradient(
            circle at 10% 5%,
            #e8e1ff 0,
            transparent 35%
        ),
        radial-gradient(
            circle at 95% 95%,
            #dff6ee 0,
            transparent 32%
        ),
        var(--background);
}

.hero {
    width: 100%;
    max-width: 920px;
    padding: clamp(26px, 6vw, 52px);
    border: 1px solid rgba(255, 255, 255, 0.9);
    border-radius: var(--radius-lg);
    background: rgba(255, 255, 255, 0.93);
    box-shadow: var(--shadow-md);
    text-align: center;
    backdrop-filter: blur(16px);
}

.intro {
    max-width: 660px;
    margin: 16px auto 26px;
    color: var(--muted);
    font-size: clamp(1rem, 3.5vw, 1.15rem);
    line-height: 1.8;
}

.features {
    display: grid;
    gap: 12px;
    margin: 24px 0;
}

.feature {
    display: grid;
    min-height: 108px;
    padding: 18px;
    border-radius: 18px;
    background: var(--surface-soft);
    grid-template-columns: 48px 1fr;
    align-items: center;
    text-align: left;
}

.feature span {
    grid-row: span 2;
    font-size: 1.8rem;
}

.feature strong {
    font-size: 1rem;
}

.feature small {
    color: var(--muted);
    line-height: 1.5;
}

.status {
    margin: 14px 0 0;
    color: var(--muted);
    font-size: 0.82rem;
}

/* =========================================================
   Auth
   ========================================================= */

.auth-page {
    display: grid;
    min-height: 100vh;
    min-height: 100dvh;
    padding:
        calc(20px + var(--safe-top))
        calc(16px + var(--safe-right))
        calc(20px + var(--safe-bottom))
        calc(16px + var(--safe-left));
    place-items: center;
    background:
        radial-gradient(
            circle at top,
            #eae5ff,
            transparent 44%
        ),
        var(--background);
}

.auth-card {
    width: 100%;
    max-width: 500px;
    padding: clamp(22px, 6vw, 38px);
    border-radius: var(--radius-lg);
    background: var(--surface);
    box-shadow: var(--shadow-md);
}

.auth-card h1 {
    text-align: center;
}

.auth-intro {
    margin: 10px 0 24px;
    color: var(--muted);
    text-align: center;
}

.auth-form {
    display: grid;
    gap: 17px;
}

.auth-form label {
    display: grid;
    gap: 7px;
    font-size: 0.94rem;
    font-weight: 750;
}

.auth-form input,
.auth-form select {
    width: 100%;
    min-height: 52px;
    padding: 10px 14px;
    border: 1.5px solid #d8dbe6;
    border-radius: 14px;
    background: #ffffff;
    color: var(--text);
    font-size: 1rem;
    outline: none;
    transition:
        border-color 150ms ease,
        box-shadow 150ms ease;
}

.auth-form input:focus,
.auth-form select:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 4px rgba(108, 76, 255, 0.12);
}

.auth-form small {
    color: var(--muted);
    font-weight: 400;
}

.back-link {
    display: block;
    min-height: 44px;
    margin-top: 16px;
    padding: 10px;
    color: var(--muted);
    text-align: center;
}

/* =========================================================
   App header
   ========================================================= */

.app-body {
    padding-bottom: var(--safe-bottom);
}

.app-header {
    position: sticky;
    z-index: 100;
    top: 0;
    display: flex;
    min-height: calc(62px + var(--safe-top));
    padding:
        calc(8px + var(--safe-top))
        calc(14px + var(--safe-right))
        8px
        calc(14px + var(--safe-left));
    border-bottom: 1px solid rgba(224, 226, 236, 0.85);
    background: rgba(255, 255, 255, 0.9);
    align-items: center;
    justify-content: space-between;
    backdrop-filter: blur(18px);
}

.brand {
    display: flex;
    min-width: 0;
    gap: 9px;
    align-items: center;
    font-size: 0.94rem;
    font-weight: 850;
}

.brand-logo {
    display: grid;
    width: 38px;
    height: 38px;
    flex: 0 0 38px;
    border-radius: 12px;
    background: var(--primary);
    color: #ffffff;
    place-items: center;
    font-weight: 900;
}

.app-header nav {
    display: flex;
    gap: 12px;
    align-items: center;
    color: var(--muted);
    font-size: 0.85rem;
}

.app-header nav a {
    display: inline-flex;
    min-height: 40px;
    padding: 8px 10px;
    border-radius: 10px;
    color: var(--primary-dark);
    align-items: center;
    font-weight: 750;
}

/* =========================================================
   Dashboard
   ========================================================= */

.dashboard {
    width: min(
        var(--page-width),
        calc(100% - 28px - var(--safe-left) - var(--safe-right))
    );
    margin: 18px auto 60px;
}

.welcome-panel {
    display: grid;
    padding: clamp(22px, 6vw, 34px);
    border-radius: 24px;
    background:
        linear-gradient(
            145deg,
            #6041ed,
            #8b70ff
        );
    color: #ffffff;
    gap: 18px;
    box-shadow: 0 14px 30px rgba(88, 59, 220, 0.2);
}

.welcome-panel .eyebrow {
    color: #e8e2ff;
}

.welcome-panel h1 {
    font-size: clamp(1.7rem, 7vw, 2.7rem);
}

.welcome-panel p {
    margin-bottom: 0;
    color: #f0edff;
}

.grade-badge {
    display: inline-grid;
    width: max-content;
    min-width: 92px;
    padding: 12px 18px;
    border: 1px solid rgba(255, 255, 255, 0.35);
    border-radius: 16px;
    background: rgba(255, 255, 255, 0.14);
    place-items: center;
    font-size: 1.2rem;
    font-weight: 900;
}

.grade-badge small {
    font-size: 0.72rem;
    font-weight: 650;
}

.stats-grid {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 9px;
    margin: 14px 0;
}

.stat-card {
    display: grid;
    min-width: 0;
    padding: 14px 6px;
    border-radius: 17px;
    background: var(--surface);
    box-shadow: var(--shadow-sm);
    text-align: center;
}

.stat-card span {
    font-size: 1.45rem;
}

.stat-card strong {
    margin-top: 4px;
    font-size: clamp(0.95rem, 4vw, 1.35rem);
}

.stat-card small {
    color: var(--muted);
    font-size: 0.68rem;
    line-height: 1.3;
}

.daily-lesson-card {
    display: grid;
    padding: 20px;
    border-radius: 22px;
    background: var(--surface);
    gap: 17px;
    box-shadow: var(--shadow-sm);
}

.lesson-icon {
    display: grid;
    width: 58px;
    height: 58px;
    border-radius: 18px;
    background: var(--primary-soft);
    place-items: center;
    font-size: 1.8rem;
}

.lesson-summary h2 {
    margin: 3px 0 8px;
}

.lesson-summary p {
    color: var(--muted);
}

.lesson-summary ul {
    display: flex;
    margin: 14px 0 0;
    padding: 0;
    gap: 7px;
    flex-wrap: wrap;
    list-style: none;
}

.lesson-summary li {
    padding: 6px 10px;
    border-radius: 999px;
    background: var(--primary-soft);
    color: var(--primary-dark);
    font-size: 0.75rem;
    font-weight: 750;
}

.lesson-action {
    display: grid;
    gap: 9px;
}

.lesson-action > span {
    color: var(--muted);
    font-size: 0.8rem;
    text-align: center;
}

.lesson-action form {
    width: 100%;
}

.history-section {
    margin-top: 15px;
    padding: 20px;
    border-radius: 22px;
    background: var(--surface);
    box-shadow: var(--shadow-sm);
}

.history-section h2 {
    margin-top: 0;
}

.empty-state {
    padding: 28px 18px;
    border-radius: 17px;
    background: var(--surface-soft);
    color: var(--muted);
    text-align: center;
}

.empty-state span {
    font-size: 2rem;
}

.attempt-list {
    display: grid;
}

.attempt-list article {
    display: flex;
    padding: 15px 3px;
    border-bottom: 1px solid var(--border);
    gap: 12px;
    align-items: center;
    justify-content: space-between;
}

.attempt-list article:last-child {
    border-bottom: 0;
}

.attempt-list article div {
    display: grid;
}

.attempt-list small {
    color: var(--muted);
}

/* =========================================================
   Lesson
   ========================================================= */

.lesson-container,
.result-container {
    width: min(
        900px,
        calc(100% - 24px - var(--safe-left) - var(--safe-right))
    );
    margin: 14px auto 50px;
}

.lesson-heading {
    padding: 23px 20px;
    border-radius: 22px;
    background:
        linear-gradient(
            145deg,
            #6041ed,
            #8b70ff
        );
    color: #ffffff;
    box-shadow: 0 12px 28px rgba(88, 59, 220, 0.18);
}

.lesson-heading .eyebrow {
    color: #e8e2ff;
}

.lesson-heading p:last-child {
    margin-bottom: 0;
    color: #f0edff;
}

.vocabulary-box,
.story-card,
.questions-card,
.answer-review {
    margin-top: 14px;
    padding: 20px 16px;
    border-radius: 22px;
    background: var(--surface);
    box-shadow: var(--shadow-sm);
}

.vocabulary-box h2,
.story-card > h2,
.answer-review > h2 {
    margin-top: 0;
}

/*
 * 手機：詞語卡左右滑動
 */
.vocabulary-grid {
    display: grid;
    margin-right: -16px;
    padding-right: 16px;
    grid-auto-columns: minmax(230px, 82%);
    grid-auto-flow: column;
    gap: 11px;
    overflow-x: auto;
    overscroll-behavior-inline: contain;
    scroll-snap-type: inline mandatory;
    scrollbar-width: none;
}

.vocabulary-grid::-webkit-scrollbar {
    display: none;
}

.vocabulary-grid article {
    min-height: 165px;
    padding: 17px;
    border: 1px solid #ebe8ff;
    border-radius: 17px;
    background: var(--surface-soft);
    scroll-snap-align: start;
}

.vocabulary-grid strong {
    color: var(--primary-dark);
    font-size: 1.25rem;
}

.vocabulary-grid p {
    margin: 7px 0;
}

.vocabulary-grid small {
    color: var(--muted);
    line-height: 1.65;
}

.story-section {
    padding: 18px 0;
    border-bottom: 1px solid var(--border);
}

.story-section:last-child {
    border-bottom: 0;
}

.story-section h3 {
    margin: 0 0 9px;
    color: var(--primary-dark);
    font-size: 1.08rem;
}

.story-section p {
    margin: 0;
    color: #292c3d;
    font-size: clamp(1.05rem, 4.3vw, 1.18rem);
    line-height: 2;
    letter-spacing: 0.02em;
}

.questions-heading {
    padding-bottom: 8px;
}

.questions-heading h2 {
    margin: 0;
}

.task-progress {
    position: sticky;
    z-index: 30;
    top: calc(63px + var(--safe-top));
    margin: 0 -16px;
    padding: 11px 16px;
    border-top: 1px solid var(--border);
    border-bottom: 1px solid var(--border);
    background: rgba(255, 255, 255, 0.94);
    backdrop-filter: blur(12px);
}

.task-progress-text {
    display: flex;
    margin-bottom: 7px;
    color: var(--muted);
    justify-content: space-between;
    font-size: 0.76rem;
    font-weight: 700;
}

.task-progress-track {
    height: 7px;
    border-radius: 999px;
    background: #e8e6f0;
    overflow: hidden;
}

.task-progress-bar {
    display: block;
    width: 0;
    height: 100%;
    border-radius: inherit;
    background:
        linear-gradient(
            90deg,
            var(--primary),
            #9a7fff
        );
    transition: width 250ms ease;
}

.question-block {
    display: grid;
    padding: 24px 0;
    border-bottom: 1px solid var(--border);
    gap: 12px;
}

.question-number {
    display: grid;
    width: 38px;
    height: 38px;
    border-radius: 12px;
    background: var(--primary-soft);
    color: var(--primary-dark);
    place-items: center;
    font-weight: 900;
}

.question-content h3 {
    margin: 0 0 14px;
    font-size: 1.05rem;
}

.option-list {
    display: grid;
    gap: 10px;
}

.option {
    display: grid;
    min-height: 58px;
    padding: 13px 14px;
    border: 1.5px solid #dcdeea;
    border-radius: 15px;
    background: #ffffff;
    grid-template-columns: 24px 1fr;
    gap: 10px;
    align-items: center;
    cursor: pointer;
    transition:
        border-color 140ms ease,
        background 140ms ease,
        transform 140ms ease;
}

.option:active {
    transform: scale(0.99);
}

.option:has(input:checked) {
    border-color: var(--primary);
    background: var(--primary-soft);
    box-shadow: 0 0 0 3px rgba(108, 76, 255, 0.09);
}

.option input {
    width: 20px;
    height: 20px;
    margin: 0;
    accent-color: var(--primary);
}

.option span {
    line-height: 1.55;
}

.question-content textarea {
    width: 100%;
    min-height: 140px;
    padding: 15px;
    border: 1.5px solid #d9dce7;
    border-radius: 15px;
    background: #ffffff;
    color: var(--text);
    font-size: 1rem;
    line-height: 1.8;
    outline: none;
    resize: vertical;
}

.question-content textarea:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 4px rgba(108, 76, 255, 0.11);
}

.question-help {
    display: block;
    margin-top: 8px;
    color: var(--muted);
    font-size: 0.78rem;
}

.submit-area {
    position: sticky;
    z-index: 40;
    bottom: 0;
    margin:
        20px -16px
        calc(-20px - var(--safe-bottom));
    padding:
        12px 16px
        calc(12px + var(--safe-bottom));
    border-top: 1px solid rgba(220, 222, 234, 0.9);
    background: rgba(255, 255, 255, 0.95);
    box-shadow: 0 -10px 24px rgba(35, 38, 70, 0.08);
    backdrop-filter: blur(16px);
}

.submit-area p {
    display: none;
}

.submit-area .primary-button {
    min-height: 56px;
}

/* =========================================================
   Result
   ========================================================= */

.result-summary {
    display: grid;
    padding: 24px 20px;
    border-radius: 22px;
    background:
        linear-gradient(
            145deg,
            #6041ed,
            #8b70ff
        );
    color: #ffffff;
    gap: 18px;
    box-shadow: 0 12px 28px rgba(88, 59, 220, 0.2);
}

.result-summary .eyebrow {
    color: #e8e2ff;
}

.result-summary h1 {
    font-size: 2rem;
}

.result-score {
    display: grid;
    width: 106px;
    height: 106px;
    border: 4px solid rgba(255, 255, 255, 0.38);
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.14);
    place-items: center;
    font-size: 1.75rem;
    font-weight: 900;
}

.review-item {
    display: grid;
    padding: 22px 0;
    border-bottom: 1px solid var(--border);
    gap: 10px;
}

.review-item:last-child {
    border-bottom: 0;
}

.review-status {
    font-size: 1.7rem;
}

.review-content h3 {
    margin: 0 0 12px;
    font-size: 1rem;
}

.review-content p {
    line-height: 1.7;
}

.correct-answer {
    color: var(--success);
}

.feedback-text {
    padding: 13px 14px;
    border-radius: 13px;
    background: var(--surface-soft);
    color: #555a6d;
    font-size: 0.9rem;
}

.student-sentence {
    padding: 14px;
    border-left: 4px solid var(--primary);
    border-radius: 0 12px 12px 0;
    background: var(--surface-soft);
    line-height: 1.8;
}

.result-actions {
    position: sticky;
    z-index: 30;
    bottom: 0;
    margin-top: 16px;
    padding:
        12px 0
        calc(12px + var(--safe-bottom));
    background:
        linear-gradient(
            transparent,
            var(--background) 28%
        );
}

/* =========================================================
   Tablet / Desktop
   ========================================================= */

@media (min-width: 700px) {
    .primary-button,
    .primary-link {
        width: auto;
    }

    .features {
        grid-template-columns: repeat(3, 1fr);
    }

    .feature {
        min-height: 150px;
        grid-template-columns: 1fr;
        text-align: center;
    }

    .feature span {
        grid-row: auto;
    }

    .app-header {
        padding-right:
            max(24px, calc((100% - var(--page-width)) / 2));
        padding-left:
            max(24px, calc((100% - var(--page-width)) / 2));
    }

    .dashboard {
        margin-top: 28px;
    }

    .welcome-panel {
        grid-template-columns: 1fr auto;
        align-items: center;
    }

    .grade-badge {
        min-width: 115px;
        min-height: 115px;
        border-radius: 50%;
        align-content: center;
    }

    .stats-grid {
        gap: 15px;
        margin: 20px 0;
    }

    .stat-card {
        padding: 22px;
    }

    .stat-card small {
        font-size: 0.8rem;
    }

    .daily-lesson-card {
        padding: 27px;
        grid-template-columns: auto 1fr auto;
        align-items: center;
    }

    .lesson-action {
        min-width: 165px;
    }

    .vocabulary-box,
    .story-card,
    .questions-card,
    .answer-review {
        padding: 28px;
    }

    .vocabulary-grid {
        margin-right: 0;
        padding-right: 0;
        grid-template-columns: repeat(2, 1fr);
        grid-auto-flow: row;
        overflow: visible;
    }

    .question-block {
        grid-template-columns: 42px 1fr;
        gap: 16px;
    }

    .submit-area {
        margin-right: -28px;
        margin-bottom: -28px;
        margin-left: -28px;
        padding-right: 28px;
        padding-left: 28px;
    }

    .result-summary {
        grid-template-columns: auto 1fr;
        padding: 32px;
        align-items: center;
    }

    .review-item {
        grid-template-columns: 42px 1fr;
    }
}

@media (min-width: 980px) {
    .vocabulary-grid {
        grid-template-columns: repeat(3, 1fr);
    }

    .lesson-heading {
        padding: 34px;
    }
}

/* ---------- Accessibility ---------- */

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        scroll-behavior: auto !important;
        transition-duration: 0.01ms !important;
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
    }
}