/* ==============================
   Google Fonts
   ============================== */
@import url('https://fonts.googleapis.com/css2?family=Noto+Sans+TC:wght@400;500;700;900&family=Inter:wght@400;500;600;700&display=swap');

/* ==============================
   CSS Variables
   ============================== */
:root {
    --font-sans: 'Inter', 'Noto Sans TC', -apple-system, BlinkMacSystemFont, sans-serif;

    /* Light Theme */
    --bg: #f0f4ff;
    --surface: #ffffff;
    --surface-alt: #f7f9ff;
    --border: #e2e8f4;

    --text-primary: #0f172a;
    --text-secondary: #64748b;
    --text-on-accent: #ffffff;

    --accent: #4f46e5;
    --accent-light: #eef2ff;
    --accent-gradient: linear-gradient(135deg, #6366f1 0%, #4f46e5 100%);

    --success: #10b981;
    --success-bg: #f0fdf4;
    --danger: #ef4444;
    --danger-bg: #fef2f2;

    --header-bg: rgba(240, 244, 255, 0.88);
    --shadow-sm: 0 1px 4px rgba(79, 70, 229, 0.06);
    --shadow-md: 0 4px 20px rgba(79, 70, 229, 0.12);
    --shadow-lg: 0 12px 40px rgba(79, 70, 229, 0.18);

    --radius-sm: 10px;
    --radius-md: 16px;
    --radius-lg: 24px;
    --radius-full: 999px;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #0a0e1a;
        --surface: #141824;
        --surface-alt: #1a1f30;
        --border: #252d42;

        --text-primary: #f1f5f9;
        --text-secondary: #8892aa;

        --accent: #818cf8;
        --accent-light: #1e1b4b;
        --accent-gradient: linear-gradient(135deg, #818cf8 0%, #6366f1 100%);

        --success: #34d399;
        --success-bg: #022c22;
        --danger: #f87171;
        --danger-bg: #450a0a;

        --header-bg: rgba(10, 14, 26, 0.90);
        --shadow-sm: 0 1px 4px rgba(0,0,0,0.3);
        --shadow-md: 0 4px 20px rgba(0,0,0,0.4);
        --shadow-lg: 0 12px 40px rgba(0,0,0,0.5);
    }
}

/* ==============================
   Reset & Base
   ============================== */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    -webkit-tap-highlight-color: transparent;
}

html {
    scroll-behavior: smooth;
}

html {
    height: -webkit-fill-available;
}

body {
    font-family: var(--font-sans);
    background-color: var(--bg);
    color: var(--text-primary);
    overflow-x: hidden;
    /* 不在 body 加 padding-top — header 已用 calc(safe-area + 14px) 處理，重複會推太遠 */
    padding-bottom: env(safe-area-inset-bottom);
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
    min-height: 100vh;
    min-height: -webkit-fill-available;
    min-height: 100dvh;
    line-height: 1.6;
}

/* ==============================
   Header
   ============================== */
.app-header {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    background: var(--header-bg);
    backdrop-filter: blur(24px) saturate(180%);
    -webkit-backdrop-filter: blur(24px) saturate(180%);
    z-index: 1000;
    padding: calc(env(safe-area-inset-top) + 14px) 20px 14px;
    border-bottom: 1px solid var(--border);
}

#app-title {
    font-size: 16px;
    font-weight: 700;
    text-align: center;
    background: var(--accent-gradient);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    letter-spacing: 0.3px;
    margin-bottom: 10px;
}

/* Progress — 用 visibility 而非 display，確保 header 高度固定不跳動 */
#progress-container {
    visibility: hidden;
    opacity: 0;
    transition: opacity 0.25s ease;
}
#progress-container.active {
    visibility: visible;
    opacity: 1;
}

#progress-text {
    font-size: 11px;
    font-weight: 600;
    color: var(--text-secondary);
    text-align: center;
    margin-bottom: 6px;
    letter-spacing: 0.5px;
    text-transform: uppercase;
}

.progress-bar-bg {
    height: 5px;
    background-color: var(--border);
    border-radius: var(--radius-full);
    overflow: hidden;
}

.progress-bar-fill {
    height: 100%;
    background: var(--accent-gradient);
    width: 0%;
    transition: width 0.4s cubic-bezier(0.4, 0, 0.2, 1);
    border-radius: var(--radius-full);
}

/* ==============================
   Main Content
   ============================== */
.app-content {
    /* 95px = header 的固定高度（含 title + progress bar，不含 safe-area） */
    margin-top: calc(env(safe-area-inset-top) + 95px);
    padding: 16px 16px calc(env(safe-area-inset-bottom) + 20px);
    min-height: calc(100dvh - calc(env(safe-area-inset-top) + 95px));
}

/* ==============================
   Screens
   ============================== */
.screen {
    display: none;
}

.screen.active {
    display: block;
    animation: screenFadeIn 0.4s cubic-bezier(0.4, 0, 0.2, 1) forwards;
}

@keyframes screenFadeIn {
    from { opacity: 0; }
    to   { opacity: 1; }
}

/* ==============================
   Cards
   ============================== */
.card {
    background: var(--surface);
    border-radius: var(--radius-lg);
    padding: 28px 24px;
    box-shadow: var(--shadow-md);
    border: 1px solid var(--border);
}

/* ==============================
   Start Screen
   ============================== */
.start-card {
    text-align: center;
    margin-top: 6vh;
    padding: 36px 28px;
    position: relative;
    overflow: hidden;
}

.start-card::before {
    content: '';
    position: absolute;
    top: -60px;
    right: -60px;
    width: 200px;
    height: 200px;
    background: var(--accent-gradient);
    border-radius: 50%;
    opacity: 0.07;
    pointer-events: none;
}

.start-card::after {
    content: '';
    position: absolute;
    bottom: -40px;
    left: -40px;
    width: 140px;
    height: 140px;
    background: var(--accent-gradient);
    border-radius: 50%;
    opacity: 0.05;
    pointer-events: none;
}

.icon-container {
    font-size: 72px;
    margin-bottom: 20px;
    animation: iconFloat 3s ease-in-out infinite;
    display: inline-block;
}

@keyframes iconFloat {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-8px); }
}

.start-card h2 {
    font-size: 30px;
    font-weight: 900;
    margin-bottom: 10px;
    line-height: 1.25;
    letter-spacing: -0.5px;
}

.total-badge {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    background: var(--accent-light);
    color: var(--accent);
    border-radius: var(--radius-full);
    padding: 6px 18px;
    font-size: 14px;
    font-weight: 600;
    margin-bottom: 30px;
}

.start-desc {
    color: var(--text-secondary);
    font-size: 14px;
    margin-bottom: 28px;
    line-height: 1.7;
}

/* ==============================
   Buttons
   ============================== */
button {
    border: none;
    outline: none;
    cursor: pointer;
    font-family: var(--font-sans);
    font-size: 16px;
    font-weight: 600;
    border-radius: var(--radius-md);
    padding: 16px 24px;
    width: 100%;
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    letter-spacing: 0.1px;
}

button:active:not(:disabled) {
    transform: scale(0.96);
}

button:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

.primary-btn {
    background: var(--accent-gradient);
    color: #fff;
    box-shadow: 0 4px 14px rgba(99, 102, 241, 0.35);
}

.primary-btn:hover:not(:disabled) {
    box-shadow: 0 6px 20px rgba(99, 102, 241, 0.45);
    transform: translateY(-1px);
}

.secondary-btn {
    background: var(--surface-alt);
    color: var(--text-primary);
    border: 1px solid var(--border);
}

.secondary-btn:hover:not(:disabled) {
    background: var(--border);
}

.text-btn {
    background: transparent;
    color: var(--accent);
    padding: 8px 12px;
    width: auto;
    font-size: 15px;
}

/* ==============================
   Quiz Screen
   ============================== */
.quiz-card {
    padding: 24px 20px;
    /* 讓 quiz 卡片底部不被固定的 action-bar 遮住 */
    margin-bottom: calc(env(safe-area-inset-bottom) + 90px);
}

#question-header {
    display: flex;
    align-items: center;
    gap: 8px;
    color: var(--text-secondary);
    font-size: 12px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 1px;
    margin-bottom: 16px;
}

#question-header::before {
    content: '';
    display: inline-block;
    width: 4px;
    height: 16px;
    background: var(--accent-gradient);
    border-radius: 2px;
}

#question-text {
    font-size: 18px;
    font-weight: 700;
    line-height: 1.5;
    margin-bottom: 24px;
    color: var(--text-primary);
}

.options-list {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.option-btn {
    background: var(--surface-alt);
    color: var(--text-primary);
    text-align: left;
    padding: 14px 18px;
    border-radius: var(--radius-md);
    font-weight: 500;
    font-size: 15px;
    border: 2px solid var(--border);
    display: flex;
    align-items: flex-start;
    gap: 12px;
    transition: all 0.18s ease;
}

.option-btn:hover:not(:disabled) {
    border-color: var(--accent);
    background: var(--accent-light);
    transform: translateX(3px);
}

.opt-letter {
    flex-shrink: 0;
    width: 28px;
    height: 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    background: var(--accent-light);
    color: var(--accent);
    font-weight: 700;
    font-size: 13px;
    margin-top: 0px;
}

/* Correct / Wrong */
.option-btn.correct {
    background: var(--success-bg) !important;
    border-color: var(--success) !important;
    animation: correctPop 0.35s cubic-bezier(0.34, 1.56, 0.64, 1);
}
.option-btn.correct .opt-letter {
    background: var(--success);
    color: #fff;
}

.option-btn.wrong {
    background: var(--danger-bg) !important;
    border-color: var(--danger) !important;
    animation: wrongShake 0.4s ease;
}
.option-btn.wrong .opt-letter {
    background: var(--danger);
    color: #fff;
}

@keyframes correctPop {
    0%   { transform: scale(1); }
    50%  { transform: scale(1.025); }
    100% { transform: scale(1); }
}

@keyframes wrongShake {
    0%, 100% { transform: translateX(0); }
    20% { transform: translateX(-6px); }
    40% { transform: translateX(6px); }
    60% { transform: translateX(-4px); }
    80% { transform: translateX(4px); }
}

/* Action Bar — sticky at bottom so it's always accessible on iPhone */
.action-bar {
    display: none; /* 預設隱藏，只有 quiz 時透過 .active 顯示 */
}

.action-bar.active {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 16px calc(env(safe-area-inset-bottom) + 12px);
    background: var(--header-bg);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border-top: 1px solid var(--border);
    gap: 12px;
    z-index: 999;
}
.action-bar button {
    flex: 1;
    padding: 14px;
}

.feedback-indicator {
    font-weight: 700;
    font-size: 22px;
    text-align: center;
    min-width: 48px;
    transition: all 0.2s ease;
}
.feedback-indicator.correct { color: var(--success); }
.feedback-indicator.wrong   { color: var(--danger); }

/* ==============================
   Result Screen
   ============================== */
.result-card {
    text-align: center;
    padding: 36px 24px;
}

.result-card h2 {
    font-size: 26px;
    font-weight: 900;
    margin-bottom: 24px;
    letter-spacing: -0.3px;
}

.score-circle {
    position: relative;
    width: 160px;
    height: 160px;
    border-radius: 50%;
    background: conic-gradient(var(--accent) var(--pct, 0%), var(--border) 0%);
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0 auto 20px;
    box-shadow: var(--shadow-lg);
    transition: --pct 1s ease;
}

.score-circle::before {
    content: '';
    position: absolute;
    inset: 12px;
    border-radius: 50%;
    background: var(--surface);
}

.score-inner {
    position: relative;
    z-index: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
}

#score-text {
    font-size: 28px;
    font-weight: 900;
    color: var(--accent);
    line-height: 1;
}

.score-label {
    font-size: 11px;
    font-weight: 600;
    color: var(--text-secondary);
    letter-spacing: 0.5px;
    margin-top: 2px;
}

#score-message {
    font-size: 17px;
    margin-bottom: 28px;
    color: var(--text-secondary);
    font-weight: 500;
}

.score-emoji {
    font-size: 36px;
    display: block;
    margin-bottom: 10px;
    animation: iconFloat 2s ease-in-out infinite;
}

/* ==============================
   Review Screen
   ============================== */
.header-back {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 20px;
    padding: 0 4px;
}

.header-back h2 {
    font-size: 20px;
    font-weight: 800;
}

.review-item {
    background: var(--surface);
    border-radius: var(--radius-md);
    padding: 18px;
    margin-bottom: 14px;
    border: 1px solid var(--border);
    box-shadow: var(--shadow-sm);
    border-left: 4px solid var(--danger);
    animation: screenFadeIn 0.3s ease forwards;
}

.review-question {
    font-weight: 700;
    margin-bottom: 12px;
    font-size: 15px;
    color: var(--text-primary);
    line-height: 1.5;
}

.review-yr-ans {
    display: flex;
    align-items: center;
    gap: 6px;
    color: var(--danger);
    font-size: 14px;
    font-weight: 500;
    margin-bottom: 6px;
}

.review-yr-ans::before { content: '✗'; font-weight: 700; }

.review-cr-ans {
    display: flex;
    align-items: center;
    gap: 6px;
    color: var(--success);
    font-size: 14px;
    font-weight: 600;
}

.review-cr-ans::before { content: '✓'; font-weight: 700; }

/* ==============================
   Stats Screen
   ============================== */
.stats-card {
    margin-bottom: 16px;
}

.stats-card h3 {
    font-size: 17px;
    font-weight: 700;
    color: var(--accent);
    margin-bottom: 16px;
    padding-bottom: 10px;
    border-bottom: 2px solid var(--border);
    display: flex;
    align-items: center;
    gap: 8px;
}

.history-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 13px 0;
    border-bottom: 1px solid var(--border);
}

.history-item:last-child { border-bottom: none; }

.history-date {
    font-weight: 500;
    font-size: 14px;
    color: var(--text-secondary);
}

.history-score {
    font-weight: 700;
    color: var(--success);
    font-size: 15px;
}

.history-empty {
    color: var(--text-secondary);
    text-align: center;
    padding: 24px 0;
    font-size: 14px;
}

.mistake-item {
    background: var(--danger-bg);
    border-radius: var(--radius-sm);
    padding: 14px 16px;
    margin-bottom: 10px;
    border-left: 4px solid var(--danger);
}

.mistake-count {
    font-weight: 700;
    color: var(--danger);
    margin-bottom: 6px;
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.mistake-q {
    font-size: 14px;
    line-height: 1.5;
    color: var(--text-primary);
}

/* ==============================
   Responsive — Desktop shows centered phone column
   ============================== */
@media (min-width: 540px) {
    body {
        background: #12111a;
    }
    .app-header {
        max-width: 430px;
        left: 50%;
        transform: translateX(-50%);
        right: auto;
        border-radius: 0;
        border-left: 1px solid var(--border);
        border-right: 1px solid var(--border);
    }
    .app-content {
        max-width: 430px;
        margin-left: auto;
        margin-right: auto;
        min-height: 100vh;
        background: var(--bg);
    }
    .action-bar.active {
        max-width: 430px;
        left: 50%;
        transform: translateX(-50%);
        right: auto;
        border-left: 1px solid var(--border);
        border-right: 1px solid var(--border);
    }
}
