:root {
    --primary-color: #2563eb;
    --bg-body: #f8fafc;
    --text-main: #0f172a;
    --text-sub: #64748b;

    --status-green: #dcfce7;
    --status-yellow: #fef9c3;
    --status-red: #fee2e2;

    --cell-width: 44px;
    --cell-height: 38px;
    --header-height: 60px;
}

body.mode-en {
    --cell-width: 68px;
}

body.mode-zh {
    --cell-width: 44px;
}

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

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background-color: var(--bg-body);
    color: var(--text-main);
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: column;
    min-height: 100vh;
}

/* Navigation */
.nav-bar {
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    width: 100%;
    height: var(--header-height);
    border-bottom: 1px solid #e2e8f0;
    position: sticky;
    top: 0;
    z-index: 100;
    display: flex;
    justify-content: center;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}

.nav-container {
    display: flex;
    gap: 4px;
    padding: 0 8px;
    overflow-x: auto;
    width: 100%;
    max-width: 900px;
    align-items: center;
    justify-content: center;
    scrollbar-width: none;
}

.nav-container::-webkit-scrollbar {
    display: none;
}

.nav-btn {
    padding: 8px 16px;
    font-size: 0.95rem;
    border: none;
    background: transparent;
    color: var(--text-sub);
    cursor: pointer;
    font-weight: 600;
    border-radius: 6px;
    white-space: nowrap;
    transition: all 0.2s;
    flex-shrink: 0;
}

.nav-btn.active {
    color: var(--primary-color);
    background-color: #eff6ff;
}

.nav-btn:hover {
    background-color: #f1f5f9;
    color: var(--primary-color);
}

/* Layout */
.container {
    max-width: 800px;
    width: 95%;
    margin: 20px auto 40px auto;
    flex: 1;
    transition: max-width 0.3s ease;
}

.container.wide-mode {
    max-width: 100%;
    width: 100%;
    padding: 0 16px;
}

/* Common UI Elements */
.action-btn {
    padding: 8px 14px;
    border-radius: 8px;
    border: 1px solid #cbd5e1;
    background: white;
    color: var(--text-main);
    font-size: 0.9rem;
    font-weight: 500;
    margin: 4px;
    cursor: pointer;
    transition: all 0.2s;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.action-btn:active {
    transform: scale(0.98);
}

.action-btn:hover {
    border-color: var(--primary-color);
    color: var(--primary-color);
    background-color: #eff6ff;
}

.btn-primary {
    background: var(--primary-color);
    color: white;
    border: none;
    padding: 12px 32px;
    font-size: 1rem;
    border-radius: 99px;
    cursor: pointer;
    box-shadow: 0 4px 6px rgba(37, 99, 235, 0.2);
    transition: all 0.2s;
    font-weight: 600;
}

.btn-primary:active {
    transform: scale(0.96);
}

.btn-primary:hover {
    background: #1d4ed8;
    transform: translateY(-1px);
}

.btn-danger {
    color: #dc2626;
    border-color: #fca5a5;
    background: #fff;
}

.btn-danger:hover {
    background: #fef2f2;
    border-color: #dc2626;
}

.btn-sm {
    padding: 4px 10px;
    font-size: 0.85rem;
    border-radius: 6px;
    border: 1px solid #cbd5e1;
    background: #fff;
    margin-left: 4px;
    cursor: pointer;
}

.btn-sm:hover {
    border-color: var(--primary-color);
    color: var(--primary-color);
}

.hidden {
    display: none !important;
}

/* --- Control Panel --- */
.control-panel {
    background: white;
    padding: 20px;
    border-radius: 12px;
    border: 1px solid #e2e8f0;
    margin-bottom: 20px;
    text-align: center;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.02);
    max-width: 600px;
    margin-left: auto;
    margin-right: auto;
}

.container.wide-mode .control-panel {
    max-width: 100%;
}

.control-row {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 12px;
    width: 100%;
}

.control-row label {
    white-space: nowrap;
    margin: 0;
}

.card-area,
.card-area-test {
    background: white;
    padding: 40px 20px 30px 20px;
    border-radius: 24px;
    border: 1px solid #e2e8f0;
    text-align: center;
    margin: 20px auto;
    max-width: 600px;
    min-height: 320px;
    box-shadow: 0 10px 30px -10px rgba(0, 0, 0, 0.08);
    position: relative;
    cursor: pointer;
    transition: all 0.3s ease;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}

.card-area:hover {
    transform: translateY(-5px);
    box-shadow: 0 20px 40px -10px rgba(0, 0, 0, 0.12);
}

.question {
    font-size: 6rem;
    margin-bottom: 10px;
    font-weight: 400;
    line-height: 1.2;
    color: #1e293b;
}

/* --- 記憶翻牌的答案區 (支援換行) --- */
.answer-text {
    font-size: 2rem;
    color: #dc2626;
    display: none;
    font-weight: 700;
    margin: 10px 0 20px 0;
    white-space: pre-wrap;
    word-wrap: break-word;
    overflow-wrap: break-word;
    max-width: 100%;
    line-height: 1.3;
    padding: 0 15px;
}

.answer-text.show {
    display: block;
    animation: popIn 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

@keyframes popIn {
    from {
        transform: scale(0.9);
        opacity: 0;
    }

    to {
        transform: scale(1);
        opacity: 1;
    }
}

/* --- 測驗輸入框 (Textarea 樣式) --- */
#test-input {
    width: 90%;
    max-width: 400px;
    min-height: 60px;
    padding: 10px;
    font-size: 1.2rem;
    text-align: center;
    border: 2px solid #e2e8f0;
    border-radius: 8px;
    outline: none;
    background: #f8fafc;
    resize: none;
    white-space: pre-wrap;
    word-break: break-word;
    font-family: inherit;
    overflow-y: auto;
    margin-top: 15px;
}

#test-input:focus {
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
    background: #fff;
}

/* --- 測驗正確答案區 (支援換行) --- */
#test-correct-msg {
    white-space: pre-wrap;
    word-wrap: break-word;
    overflow-wrap: break-word;
    max-width: 100%;
    line-height: 1.4;
    font-size: 1.2rem;
    color: #16a34a;
    margin-top: 10px;
    padding: 0 10px;
}


/* --- Grading Buttons --- */
.grading-btns {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 16px;
    margin-top: auto;
    width: 100%;
    padding-top: 20px;
    animation: fadeIn 0.3s;
}

.grading-btn {
    flex: 1;
    padding: 12px 0;
    font-size: 1rem;
    font-weight: 700;
    border-radius: 50px;
    border: none;
    cursor: pointer;
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    max-width: 110px;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
    display: flex;
    align-items: center;
    justify-content: center;
}

.grading-btn:active {
    transform: scale(0.95);
}

.btn-grade-red {
    background: #fee2e2;
    color: #ef4444;
}

.btn-grade-red:hover {
    background: #fca5a5;
    color: white;
    box-shadow: 0 5px 15px rgba(239, 68, 68, 0.3);
}

.btn-grade-yellow {
    background: #fef9c3;
    color: #d97706;
}

.btn-grade-yellow:hover {
    background: #fde047;
    color: #b45309;
    box-shadow: 0 5px 15px rgba(245, 158, 11, 0.3);
}

.btn-grade-green {
    background: #dcfce7;
    color: #16a34a;
}

.btn-grade-green:hover {
    background: #4ade80;
    color: #065f46;
    box-shadow: 0 5px 15px rgba(34, 197, 94, 0.3);
}

/* --- Ghost Button --- */
.btn-ghost {
    background: transparent;
    border: 2px solid #e2e8f0;
    color: #94a3b8;
    padding: 10px 24px;
    border-radius: 99px;
    font-weight: 600;
    margin-top: 10px;
    cursor: pointer;
    transition: 0.2s;
}

.btn-ghost:hover {
    border-color: #cbd5e1;
    color: #64748b;
    background: #f8fafc;
}

/* --- Matrix Table --- */
.matrix-wrapper {
    overflow: auto;
    max-height: 75vh;
    border: 1px solid #cbd5e1;
    border-radius: 8px;
    background: white;
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
    position: relative;
    padding-right: 2px;
    width: fit-content;
    max-width: 100%;
    margin: 0 auto;
}

.matrix-table {
    border-collapse: separate;
    border-spacing: 0;
    width: max-content;
    table-layout: fixed;
    margin-bottom: 2px;
}

.matrix-table th,
.matrix-table td {
    padding: 0;
    border-right: 1px solid #e2e8f0;
    border-bottom: 1px solid #e2e8f0;
    text-align: center;
    width: var(--cell-width);
    min-width: var(--cell-width);
    max-width: var(--cell-width);
    height: var(--cell-height);
    overflow: hidden;
    white-space: nowrap;
    background: #fff;
    transition: background-color 0.1s;
    vertical-align: middle;
}

.matrix-table td {
    position: relative;
}

.matrix-table thead th {
    position: sticky;
    top: 0;
    z-index: 40;
    /* Increased from 10 to be above .is-selected (20) */
    background: #f1f5f9;
    color: #334155;
    font-weight: bold;
    border-bottom: 2px solid #cbd5e1;
}

.matrix-table tbody th {
    position: sticky;
    left: 0;
    z-index: 40;
    /* Increased from 5 to be above .is-selected (20) */
    background: #f1f5f9;
    font-weight: bold;
    border-right: 2px solid #cbd5e1;
}

.matrix-table thead th:first-child {
    position: sticky;
    left: 0;
    top: 0;
    z-index: 50;
    /* Increased from 20 to be above everything */
    background: #e2e8f0;
    border-right: 2px solid #cbd5e1;
    border-bottom: 2px solid #cbd5e1;
}

.matrix-input {
    width: 100%;
    height: 100%;
    border: none;
    outline: none;
    background: transparent;
    text-align: center;
    font-size: 13px;
    color: #0f172a;
    padding: 0;
    margin: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 0;
    font-family: inherit;
}

.matrix-input:focus {
    background: rgba(255, 255, 255, 0.9);
    box-shadow: inset 0 0 0 2px var(--primary-color);
    font-weight: 600;
}

.header-input {
    width: 100%;
    height: 100%;
    background: transparent;
    border: none;
    text-align: center;
    font-weight: 700;
    color: #334155;
    cursor: pointer;
    outline: none;
    font-size: 0.85rem;
    padding: 0;
    margin: 0;
    border-radius: 0;
    display: block;
    line-height: var(--cell-height);
}

.diagonal-wrapper {
    display: flex;
    flex-direction: column;
    height: 100%;
    width: 100%;
    overflow: hidden;
    position: relative;
}

.diagonal-wrapper .matrix-input {


    height: 60%;
    font-size: 12px;
    display: block;
    line-height: 22px;
}

.btn-diagonal-check {
    height: 40%;
    width: 100%;
    font-size: 10px;
    padding: 0;
    margin: 0;
    border: none;
    border-top: 1px solid #e2e8f0;
    background: #f8fafc;
    color: var(--primary-color);
    cursor: pointer;
    line-height: 1;
    display: flex;
    align-items: center;
    justify-content: center;
}

.btn-diagonal-check:hover {
    background: var(--primary-color);
    color: white;
}

.status-green {
    background-color: var(--status-green) !important;
}

.status-yellow {
    background-color: var(--status-yellow) !important;
}

.status-red {
    background-color: var(--status-red) !important;
}

.highlight-guide {
    background-color: #e0f2fe !important;
}

.is-selected {
    box-shadow: inset 0 0 0 3px var(--primary-color) !important;
    position: relative;
    z-index: 20;
}

/* Grid View */
.grid-container {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
    gap: 10px;
}

.pair-item {
    background: white;
    padding: 10px;
    border-radius: 8px;
    border: 1px solid #e2e8f0;
    text-align: center;
    transition: transform 0.2s;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}

.pair-item:hover {
    border-color: var(--primary-color);
    transform: translateY(-2px);
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
}

.pair-label {
    font-size: 0.9rem;
    color: var(--text-sub);
    margin-bottom: 4px;
    font-weight: 500;
}

.pair-input {
    width: 100%;
    padding: 6px;
    text-align: center;
    border: 1px solid #cbd5e1;
    border-radius: 6px;
    font-size: 1rem;
    -webkit-appearance: none;
}

.pair-input:focus {
    border-color: var(--primary-color);
    outline: none;
    box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.1);
}

/* Other UI */
.view-section {
    display: none;
    animation: fadeIn 0.3s ease;
    padding-bottom: 60px;
}

.view-section.active {
    display: block;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(5px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.dropdown-wrapper {
    position: relative;
    display: inline-block;
    width: 100%;
    max-width: 300px;
}

.dropdown-content {
    display: none;
    position: absolute;
    background: white;
    border: 1px solid #e2e8f0;
    z-index: 999;
    padding: 12px;
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
    width: 100%;
    text-align: left;
    border-radius: 8px;
    left: 0;
    top: 110%;
}

.dropdown-content.show {
    display: block;
}

.checkbox-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 8px;
    max-height: 220px;
    overflow-y: auto;
}

/* Footer */
.main-footer {
    background: #0f172a;
    color: #94a3b8;
    padding: 30px 20px;
    text-align: center;
    margin-top: auto;
    font-size: 0.9rem;
}

.social-icons {
    display: flex;
    justify-content: center;
    gap: 20px;
    margin-bottom: 20px;
}

.icon-link svg {
    width: 24px;
    height: 24px;
    fill: #cbd5e1;
    transition: 0.2s;
}

.icon-link:hover svg {
    fill: white;
    transform: scale(1.1);
}


.text-success {
    color: #16a34a !important;
}

.text-warning {
    color: #b45309 !important;
}

.text-danger {
    color: #dc2626 !important;
}

/* --- 資料備份區塊優化樣式 --- */
.backup-container {
    display: flex;
    flex-direction: column;
    gap: 24px;
    padding: 10px 0;
}

.backup-group {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.backup-group-label {
    font-size: 0.95rem;
    font-weight: 600;
    color: #475569;
    margin-bottom: 4px;
}

/* 匯出區塊 */
.export-controls {
    display: flex;
    gap: 12px;
    align-items: stretch;
}

.styled-select-wrapper {
    flex: 2;
    position: relative;
    background: white;
    border-radius: 8px;
    border: 1px solid #cbd5e1;
}

.styled-select {
    width: 100%;
    height: 100%;
    padding: 12px;
    border: none;
    background: transparent;
    font-size: 1rem;
    color: #334155;
    outline: none;
    cursor: pointer;
    appearance: none;
    -webkit-appearance: none;
}

.select-arrow {
    position: absolute;
    right: 14px;
    top: 50%;
    transform: translateY(-50%);
    pointer-events: none;
    color: #94a3b8;
    font-size: 0.8rem;
}

.btn-export {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
}

/* Toggle Switch */
.switch {
    position: relative;
    display: inline-block;
    width: 40px;
    height: 22px;
}

.switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: #cbd5e1;
    transition: .4s;
}

.slider:before {
    position: absolute;
    content: "";
    height: 16px;
    width: 16px;
    left: 3px;
    bottom: 3px;
    background-color: white;
    transition: .4s;
}

input:checked+.slider {
    background-color: var(--primary-color);
}

input:focus+.slider {
    box-shadow: 0 0 1px var(--primary-color);
}

input:checked+.slider:before {
    transform: translateX(18px);
}

/* Rounded sliders */
.slider.round {
    border-radius: 34px;
}

.slider.round:before {
    border-radius: 50%;
}

/* Read-only styling */
.matrix-input:read-only,
.header-input:read-only {
    cursor: default;
    color: #475569;
}

.matrix-input:read-only:focus {
    box-shadow: none;
    background: transparent;
}



/* 匯入區塊 */
.import-card {
    background-color: #f8fafc;
    border: 1px solid #e2e8f0;
    /* [修改] 這裡改成實線了 */
    border-radius: 12px;
    padding: 20px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 15px;
}

.import-controls {
    display: flex;
    gap: 10px;
    align-items: center;
    width: 100%;
    justify-content: center;
}

.file-input {
    width: 100%;
    flex: 1;
    max-width: 300px;
    padding: 8px;
    background: white;
    border: 1px solid #e2e8f0;
    border-radius: 6px;
    font-size: 0.9rem;
    color: #64748b;
}

.btn-import {
    width: 100%;
    max-width: 300px;
    background-color: #dcfce7;
    color: #166534;
    border-color: #86efac;
}

.btn-import:hover {
    background-color: #bbf7d0;
}

/* 危險區塊 */
.danger-zone {
    margin-top: 20px;
    padding-top: 24px;
    border-top: 1px solid #f1f5f9;
    text-align: center;
}

.btn-full-danger {
    width: 100%;
    padding: 12px;
}

/* --- Matrix Mode Grading Toolbar --- */
.matrix-toolbar {
    display: flex;
    gap: 8px;
    align-items: center;
    flex-wrap: wrap;
    background: #f8fafc;
    padding: 8px;
    border-radius: 8px;
    border: 1px solid #e2e8f0;
}

.matrix-toolbar span {
    font-size: 0.9rem;
    color: #64748b;
    font-weight: 600;
    margin-right: 8px;
}

.btn-grade {
    padding: 6px 12px;
    border-radius: 6px;
    border: 1px solid transparent;
    cursor: pointer;
    font-size: 0.85rem;
    font-weight: 500;
    transition: all 0.2s;
}

.bg-green {
    background: #dcfce7;
    color: #166534;
    border-color: #86efac;
}

.bg-yellow {
    background: #fef9c3;
    color: #854d0e;
    border-color: #fde047;
}

.bg-red {
    background: #fee2e2;
    color: #991b1b;
    border-color: #fca5a5;
}

.bg-gray {
    background: #e2e8f0;
    color: #475569;
    border-color: #cbd5e1;
}

.bg-green:hover {
    background: #bbf7d0;
}

.bg-yellow:hover {
    background: #fef08a;
}

.bg-red:hover {
    background: #fecaca;
}

.bg-gray:hover {
    background: #cbd5e1;
}

.status-gray {
    background-color: #e2e8f0 !important;
    color: #94a3b8 !important;
    border-color: #cbd5e1 !important;
}

.pair-input.status-gray {
    background-color: #e2e8f0;
    color: #94a3b8;
}

/* Mobile */
@media (max-width: 768px) {
    :root {
        --header-height: 52px;
    }

    .nav-container {
        justify-content: flex-start;
        padding: 0 12px;
    }

    .nav-btn {
        padding: 6px 12px;
        font-size: 0.9rem;
    }

    .container {
        width: 100%;
        padding: 0 12px;
        margin: 15px auto;
    }

    .container.wide-mode {
        padding: 0 10px;
    }

    .control-panel {
        padding: 15px;
        margin-bottom: 15px;
        max-width: 100%;
    }

    .card-area,
    .card-area-test {
        padding: 25px 15px;
    }

    .question {
        font-size: 4rem;
    }

    .answer-text {
        font-size: 1.5rem;
    }

    .pair-input,
    .matrix-input,
    #test-input {
        font-size: 16px;
    }

    .grid-container {
        grid-template-columns: repeat(3, 1fr);
        gap: 8px;
    }

    .btn-primary {
        width: 100%;
        padding: 14px;
    }

    .dropdown-wrapper {
        max-width: 100%;
    }

    .checkbox-grid {
        grid-template-columns: repeat(4, 1fr);
    }

    #test-input {
        width: 100%;
        max-width: none;
    }

    /* [修改] 手機版匯入區塊強制上下排列 */
    .import-controls {
        flex-direction: column !important;
        align-items: stretch !important;
        gap: 10px !important;
    }

    .file-input,
    .btn-import {
        width: 100% !important;
        max-width: 100% !important;
        margin: 0 !important;
        flex: none !important;
    }

    .btn-import {
        padding: 12px 0 !important;
    }
}