* { margin: 0; padding: 0; box-sizing: border-box; }
body {
    background: #f5f0ff;
    font-family: system-ui, -apple-system, 'Segoe UI', sans-serif;
    padding: 12px;
    min-height: 100vh;
}

/* ========== 橫向提示 ========== */
.orientation-warning {
    display: none;
    position: fixed;
    top: 0; left: 0;
    width: 100%; height: 100%;
    background: #4a1d8c;
    color: white;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    z-index: 10000;
    text-align: center;
    gap: 1rem;
}
.orientation-warning.show { display: flex; }
.rotate-icon {
    font-size: 3rem;
    animation: rotate 2s ease-in-out infinite;
}
@keyframes rotate {
    0% { transform: rotate(0deg); }
    50% { transform: rotate(90deg); }
    100% { transform: rotate(0deg); }
}
.dismiss-btn {
    background: rgba(255,255,255,0.2);
    border: 1px solid white;
    color: white;
    padding: 8px 20px;
    border-radius: 40px;
    cursor: pointer;
}

/* ========== 基礎樣式 ========== */
:root { --deep-purple: #4a1d8c; --deep-purple-light: #7c3aed; --red: #dc2626; --success-bg: #d4edda; --locked-bg: #f5f5f5; }
.card {
    background: white;
    border-radius: 24px;
    padding: 1rem;
    margin-bottom: 1rem;
    box-shadow: 0 2px 8px rgba(0,0,0,0.05);
}
.btn {
    border: none;
    background: var(--deep-purple);
    color: white;
    padding: 0.3rem 0.8rem;
    border-radius: 40px;
    font-weight: 600;
    cursor: pointer;
    font-size: clamp(0.6rem, 3.5vw, 0.85rem);
}
.btn-outline { background: white; color: var(--deep-purple); border: 1px solid var(--deep-purple); }
.btn-danger { background: #dc2626; }
.btn-small { padding: 0.2rem 0.6rem; font-size: clamp(0.55rem, 3vw, 0.7rem); }
.btn-icon { background: none; border: none; font-size: 1rem; cursor: pointer; padding: 4px 6px; }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }

/* ========== 設定彈窗 ========== */
.modal {
    display: none;
    position: fixed;
    top: 0; left: 0;
    width: 100%; height: 100%;
    background: rgba(0,0,0,0.8);
    justify-content: center;
    align-items: center;
    z-index: 1000;
}
.modal-content {
    background: white;
    width: 95%;
    max-width: 800px;
    border-radius: 28px;
    padding: 1rem;
    max-height: 90vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}
.settings-two-columns {
    display: flex;
    gap: 1.2rem;
    margin: 0.5rem 0 1rem 0;
}
.settings-left, .settings-right {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 0.8rem;
}
.setting-group {
    background: #f9f7ff;
    border-radius: 16px;
    padding: 0.6rem;
}
.setting-title {
    font-weight: 600;
    font-size: clamp(0.75rem, 3.5vw, 0.85rem);
    color: #2e0f5a;
    margin-bottom: 0.5rem;
    display: flex;
    align-items: center;
    gap: 5px;
}
.btn-row {
    display: flex;
    gap: 0.5rem;
    margin-bottom: 0.3rem;
}
.btn-row .setting-btn {
    flex: 1;
    text-align: center;
}
.setting-btn {
    background: white;
    border: 2px solid #e0d6f5;
    border-radius: 60px;
    padding: 0.35rem 0.2rem;
    font-size: clamp(0.65rem, 3vw, 0.75rem);
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
    color: #2e0f5a;
    display: flex;
    flex-direction: column;
    align-items: center;
    line-height: 1.3;
}
.setting-btn .stars { font-size: clamp(0.7rem, 3.5vw, 0.8rem); }
.setting-btn .label { font-size: clamp(0.55rem, 2.5vw, 0.65rem); }
.setting-btn.active {
    background: var(--deep-purple);
    border-color: var(--deep-purple);
    color: white;
}
.setting-btn.locked {
    opacity: 0.5;
    cursor: not-allowed;
    background: #f0f0f0;
}
.special-btn {
    background: var(--red);
    border: none;
    color: white;
    width: 100%;
    text-align: center;
    padding: 0.4rem;
    font-weight: bold;
    box-shadow: 0 1px 3px rgba(0,0,0,0.2);
}
.special-btn.locked {
    opacity: 0.5;
    cursor: not-allowed;
    background: #aaa;
}
.setting-hint {
    font-size: clamp(0.55rem, 2.5vw, 0.6rem);
    color: #999;
    margin-top: 0.2rem;
    display: flex;
    align-items: center;
    gap: 3px;
}
.dev-btn-full {
    background: #f59e0b;
    color: white;
    border: none;
    border-radius: 60px;
    padding: 0.5rem;
    font-size: clamp(0.7rem, 3.5vw, 0.75rem);
    cursor: pointer;
    width: 100%;
    margin-bottom: 0.5rem;
}
.action-btn-full {
    background: linear-gradient(135deg, var(--deep-purple), var(--deep-purple-light));
    color: white;
    border: none;
    border-radius: 60px;
    padding: 0.5rem;
    font-size: clamp(0.75rem, 3.5vw, 0.85rem);
    font-weight: bold;
    cursor: pointer;
    width: 100%;
    margin-bottom: 0.5rem;
}
.cancel-btn-full {
    background: white;
    color: #666;
    border: 1px solid #ddd;
    border-radius: 60px;
    padding: 0.5rem;
    font-size: clamp(0.75rem, 3.5vw, 0.85rem);
    cursor: pointer;
    width: 100%;
}

/* ========== 主介面 ========== */
.unit-group { margin-bottom: 0.5rem; }
.unit-header {
    background: #f9f7ff;
    border-radius: 20px;
    padding: 0.5rem 0.7rem;
    cursor: pointer;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-weight: bold;
    font-size: clamp(0.85rem, 4vw, 1rem);
    color: #2e0f5a;
}
.unit-header-left { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.unit-toggle { font-size: 0.9rem; }
.chapters-container { margin-left: 1rem; margin-top: 0.3rem; display: none; }
.chapters-container.open { display: block; }
.chapter-item {
    background: #fefcff;
    border-radius: 12px;
    padding: 0.35rem 0.6rem;
    margin-bottom: 0.25rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 0.3rem;
    border-left: 3px solid var(--deep-purple-light);
}
.chapter-name { font-size: clamp(0.65rem, 3.5vw, 0.8rem); font-weight: 500; flex: 2; min-width: 100px; }
.chapter-actions { display: flex; gap: 0.3rem; align-items: center; }

/* 進度條 */
.mastery-wrapper {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 8px;
    min-width: 130px;
}
.progress-bar-container {
    width: 100px;
    height: 10px;
    background: #ddd;
    border-radius: 20px;
    overflow: hidden;
}
.progress-bar-fill {
    height: 100%;
    background: #10b981;
    width: 0%;
    border-radius: 20px;
}
.mastery-text {
    font-size: clamp(0.6rem, 3vw, 0.7rem);
    min-width: 50px;
    text-align: right;
}

.nav-tabs { display: flex; gap: 0.3rem; margin-bottom: 0.8rem; flex-wrap: wrap; }
.tab {
    background: none; border: none; padding: 0.3rem 0.7rem; font-weight: 600; cursor: pointer;
    border-radius: 40px; font-size: clamp(0.65rem, 3.5vw, 0.8rem);
}
.tab.active { background: var(--deep-purple); color: white; }

#practicePanel, #myMistakesPanel, #pastMistakesPanel, #pinnedPanel, #historyPanel, #achievementsPanel {
    max-height: calc(100vh - 140px);
    overflow-y: auto;
}

.mistake-chapter-group { margin-bottom: 0.5rem; }
.mistake-chapter-header {
    background: #e9e4f5;
    border-radius: 14px;
    padding: 0.4rem 0.7rem;
    cursor: pointer;
    font-weight: bold;
    font-size: 0.85rem;
    display: flex;
    justify-content: space-between;
}
.mistake-questions { margin-left: 1rem; margin-top: 0.3rem; display: none; }
.mistake-questions.open { display: block; }
.mistake-question-item {
    background: #fefcff;
    border-radius: 12px;
    padding: 0.4rem 0.6rem;
    margin-bottom: 0.3rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 0.3rem;
}

.achievement-item {
    background: #fefcff;
    border-radius: 10px;
    padding: 8px 12px;
    margin-bottom: 6px;
    font-size: clamp(0.65rem, 3.5vw, 0.75rem);
    border-left: 3px solid #ddd;
}
.achievement-item.unlocked {
    background: #d4edda;
    border-left-color: #10b981;
}
.achievement-item.locked {
    background: #f5f5f5;
    opacity: 0.7;
}
.achievement-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 4px;
}
.achievement-progress {
    background: #e9e4f5;
    border-radius: 20px;
    padding: 8px 12px;
    margin-bottom: 16px;
}
.achievement-bar {
    background: #ddd;
    border-radius: 20px;
    height: 10px;
    margin-top: 6px;
    overflow: hidden;
}
.achievement-fill {
    background: linear-gradient(90deg, #fbbf24, #f59e0b);
    width: 0%;
    height: 100%;
    border-radius: 20px;
}
.progress-small {
    width: 100%;
    height: 6px;
    background: #ddd;
    border-radius: 10px;
    margin-top: 4px;
    overflow: hidden;
}
.progress-small-fill {
    height: 100%;
    background: #10b981;
    width: 0%;
    border-radius: 10px;
}
.achievement-desc {
    font-size: 0.65rem;
    color: #666;
    margin-top: 2px;
}

.points-rank-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: linear-gradient(135deg, #4a1d8c, #7c3aed);
    color: white;
    padding: 12px 16px;
    border-radius: 20px;
    margin-bottom: 16px;
}
.points-box {
    text-align: center;
}
.points-number {
    font-size: 1.8rem;
    font-weight: bold;
}
.points-label {
    font-size: 0.7rem;
    opacity: 0.8;
}
.rank-box {
    text-align: center;
}
.rank-number {
    font-size: 1.4rem;
    font-weight: bold;
}
.rank-label {
    font-size: 0.7rem;
    opacity: 0.8;
}

.unlock-cards-container {
    position: fixed;
    bottom: 20px;
    right: 20px;
    z-index: 10001;
    display: flex;
    flex-direction: column;
    gap: 10px;
    align-items: flex-end;
    pointer-events: none;
}
.unlock-card {
    background: linear-gradient(135deg, #fbbf24, #f59e0b, #4a1d8c, #7c3aed);
    background-size: 300% 300%;
    color: white;
    padding: 12px 20px;
    border-radius: 20px;
    min-width: 260px;
    max-width: 320px;
    box-shadow: 0 0 20px rgba(251, 191, 36, 0.8);
    border: 2px solid gold;
    animation: slideInRight 0.3s ease-out, fadeOut 4s ease-in-out 3.7s forwards, gradientShift 1s ease infinite;
    pointer-events: auto;
    font-weight: bold;
}
@keyframes slideInRight {
    from { transform: translateX(100%); opacity: 0; }
    to { transform: translateX(0); opacity: 1; }
}
@keyframes fadeOut {
    to { opacity: 0; transform: translateX(100%); visibility: hidden; }
}
@keyframes gradientShift {
    0% { background-position: 0% 0%; }
    50% { background-position: 100% 100%; }
    100% { background-position: 0% 0%; }
}
.unlock-flash {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: radial-gradient(circle, rgba(251, 191, 36, 0.8), rgba(74, 29, 140, 0.6), transparent);
    pointer-events: none;
    z-index: 10002;
    animation: flashFade 0.8s ease-out forwards;
}
@keyframes flashFade {
    0% { opacity: 1; }
    100% { opacity: 0; visibility: hidden; }
}

/* ========== 題目數量區域 - 兩行等寬佈局 ========== */
.count-buttons-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
    margin-bottom: 12px;
}

.count-buttons-row .setting-btn {
    width: 100%;
    text-align: center;
}

.count-row {
    display: grid;
    grid-template-columns: 1fr auto;
    align-items: center;
    gap: 16px;
}

.count-row .setting-btn {
    width: 100%;
    text-align: center;
}

.custom-input-wrapper {
    display: flex;
    align-items: center;
    gap: 6px;
    background: #f5f0ff;
    padding: 4px 12px;
    border-radius: 40px;
    border: 1px solid #e0d6f5;
    white-space: nowrap;
}

.custom-input-wrapper span {
    font-size: 0.75rem;
    color: #4a1d8c;
    font-weight: 500;
}

.custom-input-wrapper input {
    width: 60px;
    padding: 6px 4px;
    border: none;
    background: white;
    border-radius: 40px;
    text-align: center;
    font-size: 0.8rem;
    font-weight: 500;
    color: #4a1d8c;
}

.custom-input-wrapper input:focus {
    outline: 2px solid #7c3aed;
}

.custom-input-wrapper input:disabled {
    background: #f0f0f0;
    color: #999;
}

/* 手機版調整 */
@media (max-width: 640px) {
    .count-row {
        grid-template-columns: 1fr;
        gap: 10px;
    }
    
    .custom-input-wrapper {
        justify-content: center;
        width: fit-content;
        margin: 0 auto;
    }
}

/* ========== 題目彈窗 ========== */
.quiz-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 0.5rem;
    flex-wrap: wrap;
    gap: 0.3rem;
    flex-shrink: 0;
}
.question-nav {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    margin-bottom: 0.5rem;
    justify-content: center;
    flex-shrink: 0;
}
.q-nav-btn {
    width: 30px;
    height: 30px;
    border-radius: 50%;
    background: #e9e4f5;
    border: none;
    cursor: pointer;
    font-size: 0.7rem;
}
.q-nav-btn.current { background: var(--deep-purple); color: white; }
.q-nav-btn.answered { background: #10b981; color: white; }
.q-nav-btn.unanswered { background: #fbbf24; }
.q-nav-btn:disabled { opacity: 0.4; cursor: not-allowed; }
.quiz-body {
    flex: 1;
    margin-bottom: 0.5rem;
    min-height: 0;
    display: flex;
    flex-direction: column;
}
.question-text {
    font-size: clamp(0.85rem, 5vw, 1rem);
    font-weight: 500;
    margin-bottom: 0.8rem;
    line-height: 1.3;
    word-break: break-word;
}

.options-area {
    flex: 2;
}
.options-area.vertical .options-grid {
    display: flex;
    flex-direction: column;
    gap: 10px;
}
.options-area.grid .options-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px 20px;
}
.option-btn {
    display: block;
    width: 100%;
    text-align: left;
    background: #f3f0ff;
    margin: 0;
    padding: 0.5rem;
    border-radius: 40px;
    border: none;
    cursor: pointer;
    font-size: clamp(0.75rem, 3.5vw, 0.85rem);
    word-break: break-word;
    white-space: normal;
    line-height: 1.3;
}
.option-btn.selected {
    background: var(--deep-purple-light);
    color: white;
}

.image-area {
    position: relative;
    text-align: center;
}
.image-area img {
    max-width: 100%;
    max-height: 180px;
    object-fit: contain;
    cursor: pointer;
    border-radius: 8px;
    transition: transform 0.2s, box-shadow 0.2s;
}
.image-area img:hover {
    transform: scale(1.02);
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}
.image-area::after {
    content: "🔍";
    position: absolute;
    bottom: 8px;
    right: 8px;
    background: rgba(0,0,0,0.6);
    color: white;
    font-size: 0.7rem;
    padding: 2px 6px;
    border-radius: 20px;
    opacity: 0.7;
    pointer-events: none;
    transition: opacity 0.2s;
}
.image-area:hover::after {
    opacity: 1;
}

.quiz-layout {
    display: flex;
    gap: 24px;
    align-items: flex-start;
}
.quiz-layout .image-area {
    flex: 1;
    max-width: 220px;
}
.quiz-layout.no-image .image-area {
    display: none;
}
.quiz-layout.no-image .options-area {
    flex: 1;
}

@media (max-width: 640px) {
    .quiz-layout {
        flex-direction: column;
    }
    .quiz-layout .image-area {
        max-width: 100%;
        margin-bottom: 12px;
    }
    .quiz-layout .image-area img {
        max-height: 100px;
    }
    .options-area .options-grid {
        display: flex;
        flex-direction: column;
        gap: 8px;
    }
}

.quiz-footer {
    display: flex;
    justify-content: space-between;
    gap: 0.3rem;
    flex-wrap: wrap;
    margin-top: 0.5rem;
    padding-top: 0.5rem;
    border-top: 1px solid #e0d6f5;
    flex-shrink: 0;
}

.result-container { overflow-y: auto; max-height: 60vh; }

.result-summary-bar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 20px;
    background: linear-gradient(135deg, #e9e4f5, #f9f7ff);
    padding: 12px 16px;
    border-radius: 40px;
}
.result-progress {
    display: flex;
    align-items: center;
    gap: 12px;
    flex: 1;
    flex-wrap: wrap;
}
.result-progress span {
    font-weight: 600;
    font-size: 0.9rem;
    white-space: nowrap;
}
.big-progress-bar {
    flex: 1;
    min-width: 150px;
    height: 24px;
    background: #ddd;
    border-radius: 30px;
    overflow: hidden;
}
.big-progress-fill {
    height: 100%;
    width: 0%;
    border-radius: 30px;
    transition: width 0.3s ease;
}
.result-buttons {
    display: flex;
    gap: 10px;
}
.result-buttons .btn {
    padding: 6px 14px;
    font-size: 0.8rem;
}
.result-table { width: 100%; border-collapse: collapse; font-size: clamp(0.6rem, 3vw, 0.7rem); }
.result-table th, .result-table td { border: 1px solid #ddd; padding: 4px; text-align: center; }
.result-row-correct { background-color: #d4edda; }
.result-row-wrong { background-color: #f8d7da; }

#resultModal .modal-content {
    text-align: center;
}
#resultModal .modal-content h3 {
    text-align: center;
    width: 100%;
}
#closeResultBtn {
    display: block;
    margin: 20px auto 0 auto;
    width: fit-content;
    min-width: 80px;
}

.explain-content { overflow-y: auto; max-height: 60vh; }

.history-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.7rem;
    overflow-x: auto;
    display: block;
}
.history-table th, .history-table td {
    border: 1px solid #ddd;
    padding: 6px 4px;
    text-align: center;
    white-space: nowrap;
}
.export-btn { background: #10b981; margin-bottom: 12px; }

.collapsible {
    cursor: pointer;
    user-select: none;
}
.collapsible:hover { opacity: 0.8; }
.collapsible-content { display: block; }
.collapsible-content.collapsed { display: none; }

@keyframes blink {
    0% { background: #10b981; transform: scale(1); }
    50% { background: #f59e0b; transform: scale(1.05); }
    100% { background: #10b981; transform: scale(1); }
}
#submitAllBtn {
    transition: all 0.2s;
}

.results-card-list {
    display: flex;
    flex-direction: column;
    gap: 16px;
    margin-top: 16px;
}
.result-card {
    background: white;
    border-radius: 20px;
    padding: 16px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.08);
    border-left: 6px solid;
    transition: transform 0.2s, box-shadow 0.2s;
}
.result-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 20px rgba(0,0,0,0.12);
}
.result-card.correct {
    border-left-color: #10b981;
    background: linear-gradient(135deg, #f0fdf4 0%, #ffffff 100%);
}
.result-card.wrong {
    border-left-color: #dc2626;
    background: linear-gradient(135deg, #fef2f2 0%, #ffffff 100%);
}
.result-card-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 12px;
    gap: 12px;
}
.result-card-question {
    font-weight: 600;
    font-size: 1rem;
    line-height: 1.45;
    color: #1a1a2e;
    flex: 1;
    word-break: break-word;
}
.result-card-icon {
    font-size: 1.5rem;
    flex-shrink: 0;
}
.result-card-details {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    margin: 12px 0;
    padding: 10px 0;
    border-top: 1px solid #e9e4f5;
    border-bottom: 1px solid #e9e4f5;
}
.result-card-details span {
    background: #f5f0ff;
    padding: 6px 14px;
    border-radius: 30px;
    font-size: 0.85rem;
    color: #4a1d8c;
    font-weight: 500;
}
.result-card-actions {
    display: flex;
    justify-content: flex-end;
    margin-top: 8px;
}
.result-card-actions button {
    background: #f5f0ff;
    border: none;
    color: #4a1d8c;
    font-weight: 600;
    cursor: pointer;
    font-size: 0.85rem;
    padding: 8px 16px;
    border-radius: 40px;
    transition: all 0.2s;
}
.result-card-actions button:hover {
    background: #e9e4f5;
    transform: scale(1.02);
}

@media (max-width: 640px) {
    body { padding: 8px; }
    .unit-header { padding: 0.4rem 0.5rem; }
    .chapter-item { flex-direction: column; align-items: stretch; }
    .modal-content { width: 98%; padding: 0.6rem; }
    .mastery-wrapper { min-width: 110px; }
    .progress-bar-container { width: 80px; }
    .settings-two-columns { flex-direction: column; gap: 0.8rem; }
    .unlock-card { min-width: 220px; padding: 10px 16px; font-size: 0.8rem; }
    .points-number { font-size: 1.2rem; }
    .rank-number { font-size: 1rem; }
    .big-progress-bar { min-width: 120px; height: 20px; }
    .result-progress span { font-size: 0.8rem; }
    .result-summary-bar { flex-direction: column; align-items: stretch; }
    .result-buttons { justify-content: center; }
    .result-card { padding: 12px; }
    .result-card-question { font-size: 0.9rem; }
    .result-card-icon { font-size: 1.3rem; }
    .result-card-details span { font-size: 0.7rem; padding: 4px 10px; }
}

/* ========== 解鎖三星進度條 ========== */
.star3-progress-container {
    margin-top: 12px;
    padding: 10px;
    background: #f0edf8;
    border-radius: 16px;
}

.star3-progress-bar {
    width: 100%;
    height: 12px;
    background: #e0e0e0;
    border-radius: 20px;
    overflow: hidden;
}

.star3-progress-fill {
    height: 100%;
    background: linear-gradient(90deg, #10b981, #34d399);
    width: 0%;
    border-radius: 20px;
    transition: width 0.3s ease;
}

.star3-progress-fill.unlocked {
    background: linear-gradient(90deg, #10b981, #059669);
}

.star3-progress-text {
    font-size: 0.7rem;
    color: #4a1d8c;
    margin-top: 8px;
    text-align: center;
    font-weight: 500;
}

.star3-progress-text.unlocked {
    color: #059669;
}

/* 手機版進度條調整 */
@media (max-width: 640px) {
    .star3-progress-container {
        margin-top: 10px;
        padding: 8px;
    }
    .star3-progress-bar {
        height: 10px;
    }
    .star3-progress-text {
        font-size: 0.65rem;
    }
}