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

/* CSS 變數 - 預設深色主題 */
:root {
    --bg-primary: #1a1a2e;
    --bg-secondary: #16213e;
    --bg-tertiary: #0f0f23;
    --text-primary: #eee;
    --text-muted: #888;
    --text-secondary: #ccc;
    --accent-primary: #c084fc;
    --accent-secondary: #f59e0b;
    --correct-char: #c084fc;
    --incorrect-char: #f87171;
    --border-color: #16213e;
    --border-secondary: #333;
    --border-tertiary: #555;
    --border-hover: #888;
    --scrollbar-bg: #0f0f23;
    --scrollbar-thumb: #c084fc;
    --white: #fff;
    --black: #000;
    --achievement-gradient-start: #f59e0b;
    --achievement-gradient-end: #d97706;
    --achievement-top5-start: #a855f7;
    --achievement-top5-end: #7c3aed;
    --leaderboard-current-start: #c084fc;
    --leaderboard-current-end: #a855f7;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Microsoft JhengHei', 'PingFang TC', sans-serif;
    background-color: var(--bg-primary);
    color: var(--text-primary);
    min-height: 100vh;
    margin: 0;
    padding: 0;
}

/* 應用程式佈局 */
.app-layout {
    display: flex;
    min-height: 100vh;
}

/* 左側邊欄 */
.sidebar {
    width: 280px;
    background-color: var(--bg-secondary);
    padding: 1.5rem;
    overflow-y: auto;
    border-right: 1px solid var(--border-secondary);
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
}

.sidebar-title {
    font-size: 1.5rem;
    margin: 0 0 0.5rem 0;
    color: var(--text-primary);
}

.sidebar-section {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.sidebar-section h3 {
    font-size: 0.9rem;
    color: var(--text-muted);
    margin: 0;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

/* 主要內容區 */
.main-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: center;
    padding: 2rem;
    max-width: 900px;
    margin: 0 auto;
    width: 100%;
}

/* 啟動載入畫面 */
#loading-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: var(--bg-primary);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 9999;
    transition: opacity 0.3s ease-out;
}

#loading-overlay.fade-out {
    opacity: 0;
    pointer-events: none;
}

.loading-content {
    text-align: center;
}

.loading-spinner {
    width: 50px;
    height: 50px;
    border: 4px solid var(--bg-secondary);
    border-top-color: var(--accent-primary);
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin: 0 auto 1.5rem;
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

#loading-status {
    color: var(--text-muted);
    font-size: 1rem;
}

h1 {
    text-align: center;
    margin-bottom: 2rem;
    color: var(--white);
    font-size: 2rem;
}

/* 模式切換按鈕 */
.mode-switch {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
}

.mode-btn {
    padding: 0.5rem 1.5rem;
    font-size: 1rem;
    border: 2px solid var(--accent-primary);
    background-color: transparent;
    color: var(--accent-primary);
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.2s;
}

.mode-btn:hover {
    background-color: rgba(192, 132, 252, 0.1);
}

.mode-btn.active {
    background-color: var(--accent-primary);
    color: var(--bg-primary);
}

/* 內容模式切換按鈕 (句子/文章) */
.content-mode-switch {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
}

/* 字碼類型切換按鈕 */
.code-type-switch {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
}

.code-type-btn {
    padding: 0.3rem 0.8rem;
    font-size: 0.85rem;
    border: 1px solid var(--border-tertiary);
    background-color: transparent;
    color: var(--text-muted);
    border-radius: 16px;
    cursor: pointer;
    transition: all 0.2s;
}

.code-type-btn:hover {
    border-color: var(--accent-primary);
    color: var(--text-primary);
}

.code-type-btn.active {
    border-color: var(--accent-primary);
    background-color: rgba(192, 132, 252, 0.1);
    color: var(--accent-primary);
}

/* 主題切換按鈕 */
.theme-switch {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
}

.content-mode-btn {
    padding: 0.4rem 1rem;
    font-size: 0.9rem;
    border: 1px solid #666;
    background-color: transparent;
    color: var(--text-muted);
    border-radius: 20px;
    cursor: pointer;
    transition: all 0.2s;
}

.content-mode-btn:hover {
    border-color: var(--border-hover);
    color: var(--text-secondary);
}

.content-mode-btn.active {
    border-color: var(--accent-secondary);
    background-color: rgba(245, 158, 11, 0.1);
    color: var(--accent-secondary);
}

#text-display {
    background-color: var(--bg-secondary);
    padding: 1.5rem;
    border-radius: 8px;
    font-size: 1.4rem;
    line-height: 2;
    margin-bottom: 1.5rem;
    min-height: 120px;
    width: 100%;
    max-width: 800px;
}

/* 文章模式下的文字顯示 */
#text-display.article-mode {
    max-height: 400px;
    overflow-y: auto;
    font-size: 1.2rem;
    line-height: 1.8;
}

#text-display.article-mode::-webkit-scrollbar {
    width: 8px;
}

#text-display.article-mode::-webkit-scrollbar-track {
    background: var(--scrollbar-bg);
    border-radius: 4px;
}

#text-display.article-mode::-webkit-scrollbar-thumb {
    background: var(--scrollbar-thumb);
    border-radius: 4px;
}

/* 文章模式下的輸入框 */
#input-area.article-mode {
    height: 200px;
}

/* 字碼模式下的文字顯示 */
#text-display.code-mode {
    font-size: 2rem;
    letter-spacing: 0.5rem;
    font-family: 'Microsoft JhengHei', 'PingFang TC', sans-serif;
    line-height: 2.5;
}

/* 錯誤閃爍動畫 */
@keyframes flash-error {
    0%, 100% { background-color: transparent; }
    50% { background-color: var(--incorrect-char); }
}

.char.flash-error {
    animation: flash-error 0.3s ease-in-out;
}

#text-display .char {
    transition: color 0.1s;
}

#text-display .loading {
    color: var(--text-muted);
    font-style: italic;
}

#text-display .char.correct {
    color: var(--correct-char);
}

#text-display .char.incorrect {
    color: var(--incorrect-char);
    text-decoration: underline;
}

#text-display .char.current {
    border-bottom: 2px solid var(--white);
}

#input-area {
    width: 100%;
    max-width: 800px;
    height: 100px;
    padding: 1rem;
    font-size: 1.2rem;
    border: 2px solid var(--border-color);
    border-radius: 8px;
    background-color: var(--bg-tertiary);
    color: var(--text-primary);
    resize: none;
    outline: none;
    font-family: inherit;
}

#input-area:focus {
    border-color: var(--accent-primary);
}

#input-area:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* 提示文字區域 */
.hint-section {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.75rem;
    margin-top: 1rem;
    width: 100%;
    max-width: 800px;
}

.hint {
    text-align: center;
    color: var(--text-muted);
    font-size: 0.9rem;
}

.hint kbd {
    background-color: var(--border-secondary);
    padding: 0.2rem 0.5rem;
    border-radius: 4px;
    font-family: monospace;
    border: 1px solid var(--border-tertiary);
}

/* 編碼顯示選項 */
.encoding-option {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    color: #aaa;
    font-size: 0.9rem;
    cursor: pointer;
    user-select: none;
}

.encoding-option input[type="checkbox"] {
    width: 18px;
    height: 18px;
    cursor: pointer;
    accent-color: var(--accent-primary);
}

.encoding-option:hover {
    color: var(--white);
}

/* ===== 虛擬鍵盤 ===== */

.virtual-keyboard {
    width: 100%;
    max-width: 700px;
    margin: 1rem auto 0;
    display: flex;
    flex-direction: column;
    gap: 4px;
    user-select: none;
}

.kb-row {
    display: flex;
    justify-content: center;
    gap: 4px;
}

.kb-row-top {
    padding-left: 20px;
}

.kb-row-home {
    padding-left: 30px;
}

.kb-row-bottom {
    padding-left: 50px;
}

.kb-key {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: 48px;
    height: 48px;
    background-color: var(--bg-secondary);
    border: 2px solid var(--border-secondary);
    border-radius: 6px;
    color: var(--text-muted);
    transition: border-color 0.15s, background-color 0.15s;
    flex-shrink: 0;
}

.kb-label {
    font-size: 0.6rem;
    color: var(--text-muted);
    opacity: 0.6;
    line-height: 1;
}

.kb-code {
    font-size: 0.95rem;
    color: var(--text-primary);
    line-height: 1.2;
    font-family: 'Microsoft JhengHei', 'PingFang TC', sans-serif;
}

.kb-key:has(.kb-code:empty) {
    opacity: 0.3;
}

.kb-key.kb-wrong {
    border-color: var(--incorrect-char);
    background-color: rgba(248, 113, 113, 0.25);
}

.kb-key.kb-correct {
    border-color: #4ade80;
    background-color: rgba(74, 222, 128, 0.25);
}

#results {
    background-color: var(--bg-secondary);
    padding: 1.5rem;
    border-radius: 8px;
    margin-top: 1.5rem;
    text-align: center;
    width: 100%;
    max-width: 800px;
}

#results h2 {
    margin-bottom: 1rem;
    color: var(--accent-primary);
}

#results p {
    font-size: 1.25rem;
    margin: 0.5rem 0;
}

#results span {
    font-weight: bold;
    color: var(--accent-primary);
}

#restart-btn {
    display: block;
    width: 100%;
    max-width: 800px;
    padding: 1rem;
    margin-top: 1.5rem;
    font-size: 1.1rem;
    background-color: var(--accent-primary);
    color: var(--bg-primary);
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-weight: bold;
}

#restart-btn:hover {
    background-color: #a855f7;
}

.hidden {
    display: none !important;
}

/* 編碼提示框（側邊欄版本） */
#encoding-hint.sidebar-encoding-hint {
    position: static;
    background-color: var(--bg-tertiary);
    border: 2px solid var(--accent-primary);
    border-radius: 12px;
    padding: 1rem;
    margin-bottom: 1.5rem;
}

.encoding-title {
    font-size: 0.85rem;
    color: var(--text-muted);
    margin-bottom: 0.5rem;
    text-align: center;
}

.encoding-char {
    font-size: 2.5rem;
    text-align: center;
    color: var(--accent-primary);
    margin-bottom: 0.75rem;
    padding-bottom: 0.5rem;
    border-bottom: 1px solid var(--border-secondary);
}

.encoding-list {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.encoding-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.encoding-label {
    font-size: 0.85rem;
    color: var(--text-muted);
}

.encoding-value {
    font-size: 1rem;
    color: var(--text-primary);
    font-family: 'Consolas', 'Monaco', monospace;
    background-color: var(--bg-tertiary);
    padding: 0.2rem 0.5rem;
    border-radius: 4px;
}

/* 成就標示 */
.achievement {
    background-color: var(--bg-tertiary);
    padding: 0.75rem 1rem;
    border-radius: 8px;
    margin-bottom: 1rem;
    font-size: 1.1rem;
}

.achievement.new-record {
    background: linear-gradient(135deg, var(--achievement-gradient-start) 0%, var(--achievement-gradient-end) 100%);
    color: var(--black);
    animation: pulse 0.5s ease-in-out 3;
}

.achievement.top-five {
    background: linear-gradient(135deg, var(--achievement-top5-start) 0%, var(--achievement-top5-end) 100%);
    color: var(--white);
}

@keyframes pulse {

    0%,
    100% {
        transform: scale(1);
    }

    50% {
        transform: scale(1.02);
    }
}

/* 排行榜 */
#leaderboard {
    margin-top: 1.5rem;
    padding-top: 1rem;
    border-top: 1px solid var(--border-secondary);
}

#leaderboard h3 {
    margin-bottom: 0.75rem;
    font-size: 1rem;
    color: var(--text-secondary);
}

#leaderboard-list {
    list-style: none;
    text-align: left;
    padding: 0;
}

#leaderboard-list li {
    padding: 0.5rem 0.75rem;
    margin: 0.25rem 0;
    background-color: var(--bg-tertiary);
    border-radius: 6px;
    font-size: 0.95rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

#leaderboard-list li.current {
    background: linear-gradient(135deg, var(--leaderboard-current-start) 0%, var(--leaderboard-current-end) 100%);
    color: var(--black);
    font-weight: bold;
}

#leaderboard-list li.empty {
    color: var(--text-muted);
    font-style: italic;
    justify-content: center;
}

#leaderboard-list .timestamp {
    font-size: 0.75rem;
    color: var(--text-muted);
    margin-left: auto;
}

#leaderboard-list li.current .timestamp {
    color: var(--bg-tertiary);
}

#leaderboard-list .detail {
    font-size: 0.8rem;
    opacity: 0.7;
    margin-left: 0.25rem;
}

#leaderboard-list .current-badge {
    font-size: 0.8rem;
    color: var(--black);
    font-weight: normal;
}

/* 響應式設計 */
@media (max-width: 768px) {
    .app-layout {
        flex-direction: column;
    }

    .sidebar {
        width: 100%;
        border-right: none;
        border-bottom: 1px solid var(--border-secondary);
    }

    .main-content {
        padding: 1rem;
    }

    #encoding-hint.sidebar-encoding-hint {
        margin-top: 1rem;
    }

    .virtual-keyboard {
        gap: 3px;
    }

    .kb-row {
        gap: 3px;
    }

    .kb-key {
        width: 28px;
        height: 36px;
    }

    .kb-label {
        font-size: 0.45rem;
    }

    .kb-code {
        font-size: 0.7rem;
    }

    .kb-row-top {
        padding-left: 10px;
    }

    .kb-row-home {
        padding-left: 16px;
    }

    .kb-row-bottom {
        padding-left: 26px;
    }
}

/* ===== 主題定義 ===== */

/* 深色主題（預設） - 使用 :root 的預設變數 */
body.theme-dark {
    /* 使用預設的深色主題變數 */
}

/* 灰階主題 */
body.theme-grayscale {
    --bg-primary: #2a2a2a;
    --bg-secondary: #1f1f1f;
    --bg-tertiary: #151515;
    --text-primary: #e0e0e0;
    --text-muted: #909090;
    --text-secondary: #c0c0c0;
    --accent-primary: #b0b0b0;
    --accent-secondary: #a0a0a0;
    --correct-char: #d0d0d0;
    --incorrect-char: #808080;
    --border-color: #1f1f1f;
    --border-secondary: #404040;
    --border-tertiary: #606060;
    --border-hover: #909090;
    --scrollbar-bg: #151515;
    --scrollbar-thumb: #b0b0b0;
    --white: #ffffff;
    --black: #000000;
    --achievement-gradient-start: #a0a0a0;
    --achievement-gradient-end: #808080;
    --achievement-top5-start: #888888;
    --achievement-top5-end: #606060;
    --leaderboard-current-start: #b0b0b0;
    --leaderboard-current-end: #909090;
}

/* 高對比黑白主題 */
body.theme-contrast {
    --bg-primary: #000000;
    --bg-secondary: #0a0a0a;
    --bg-tertiary: #000000;
    --text-primary: #ffffff;
    --text-muted: #cccccc;
    --text-secondary: #eeeeee;
    --accent-primary: #ffffff;
    --accent-secondary: #ffffff;
    --correct-char: #ffffff;
    --incorrect-char: #999999;
    --border-color: #333333;
    --border-secondary: #1a1a1a;
    --border-tertiary: #444444;
    --border-hover: #666666;
    --scrollbar-bg: #000000;
    --scrollbar-thumb: #ffffff;
    --white: #ffffff;
    --black: #000000;
    --achievement-gradient-start: #ffffff;
    --achievement-gradient-end: #cccccc;
    --achievement-top5-start: #ffffff;
    --achievement-top5-end: #dddddd;
    --leaderboard-current-start: #ffffff;
    --leaderboard-current-end: #eeeeee;
}

/* 淺色主題 */
body.theme-light {
    --bg-primary: #ffffff;
    --bg-secondary: #f5f5f5;
    --bg-tertiary: #e8e8e8;
    --text-primary: #1a1a1a;
    --text-muted: #666666;
    --text-secondary: #333333;
    --accent-primary: #9333ea;
    --accent-secondary: #2563eb;
    --correct-char: #9333ea;
    --incorrect-char: #dc2626;
    --border-color: #d1d5db;
    --border-secondary: #e5e7eb;
    --border-tertiary: #9ca3af;
    --border-hover: #6b7280;
    --scrollbar-bg: #e8e8e8;
    --scrollbar-thumb: #9333ea;
    --white: #ffffff;
    --black: #000000;
    --achievement-gradient-start: #fbbf24;
    --achievement-gradient-end: #f59e0b;
    --achievement-top5-start: #a855f7;
    --achievement-top5-end: #7c3aed;
    --leaderboard-current-start: #9333ea;
    --leaderboard-current-end: #7c3aed;
}