/* ==========================================
   全域共用與主選單樣式
   ========================================== */
:root {
    --bg-color: #F0F2F5;
    --primary: #9b59b6;
    --primary-dark: #8e44ad;
    --accent: #f1c40f;
    --user-msg: #34A853;
    --ai-msg: #FFFFFF;
}

* { box-sizing: border-box; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; -webkit-tap-highlight-color: transparent; }
input, textarea { -webkit-user-select: auto !important; user-select: auto !important; -webkit-touch-callout: default !important; }

body, html { margin: 0; padding: 0; height: 100dvh; width: 100vw; font-family: "Microsoft JhengHei", "PingFang TC", sans-serif; background-color: var(--bg-color); display: flex; flex-direction: column; overflow: hidden; }

/* 全域導覽列 */
#global-top-bar {
    flex: 0 0 auto; display: flex; justify-content: space-between; align-items: center;
    padding: 10px 20px; background-color: var(--primary); color: white; z-index: 100; height: 50px; box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
.header-title { font-size: 1.2rem; font-weight: bold; display: flex; align-items: center; gap: 10px; }
.btn-group { display: flex; gap: 10px; align-items: center; }
.top-btn {
    background: #fff; border: 1px solid #ddd; border-radius: 8px; width: 36px; height: 36px;
    font-size: 20px; cursor: pointer; display: flex; justify-content: center; align-items: center; color: #333; box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
#systemStatus { color: var(--accent); font-size: 14px; font-weight: bold; }

/* 視圖切換控制器 */
.app-view { display: none; flex-direction: column; flex: 1; height: calc(100dvh - 50px); overflow: hidden; width: 100%; }
.app-view.active { display: flex; }

/* 主選單視圖 */
#view-menu { justify-content: center; align-items: center; gap: 30px; background: var(--bg-color); }
.menu-btn {
    width: 80%; max-width: 400px; padding: 30px; border-radius: 20px; border: none;
    font-size: 2rem; font-weight: bold; cursor: pointer; box-shadow: 0 8px 15px rgba(0,0,0,0.1);
    transition: transform 0.2s; display: flex; align-items: center; justify-content: center; gap: 20px;
}
.menu-btn:active { transform: scale(0.95); }
.btn-voice { background: #4A90E2; color: white; }
.btn-zhuyin { background: #34A853; color: white; }
.btn-spell { background: #E74C3C; color: white; }

/* ==========================================
   App 1 & 2: 共用聊天區樣式
   ========================================== */
.chat-history {
    flex: 1 1 auto; min-height: 0; padding: 10px 20px; overflow-y: auto;
    display: flex; flex-direction: column; gap: 15px; scroll-behavior: smooth; background-color: var(--bg-color);
}
.message { max-width: 85%; padding: 15px 22px; border-radius: 20px; font-size: 28px; line-height: 1.5; word-wrap: break-word; box-shadow: 0 3px 8px rgba(0,0,0,0.08); color: #1C1E21; }
.user-msg { align-self: flex-end; background-color: var(--user-msg); color: white; border-bottom-right-radius: 5px; }
.ai-msg { align-self: flex-start; background-color: var(--ai-msg); border-bottom-left-radius: 5px; }
.info-msg { align-self: center; background-color: transparent; color: #888; font-size: 18px; text-align: center; box-shadow: none; padding: 5px; margin: 0; }

/* App 1: 語音陪伴專屬 */
#view-voice #voice-area { flex: 0 0 auto; background: white; padding: 20px 10px; display: flex; flex-direction: column; align-items: center; box-shadow: 0 -4px 15px rgba(0,0,0,0.05); border-radius: 30px 30px 0 0; }
#view-voice #mic-btn { width: 110px; height: 110px; border-radius: 50%; background: #4A90E2; color: white; font-size: 45px; border: none; box-shadow: 0 8px 20px rgba(74,144,226,0.4); display: flex; justify-content: center; align-items: center; transition: all 0.3s; }
#view-voice #mic-btn.listening { background: #E74C3C; box-shadow: 0 0 0 15px rgba(231,76,60,0.2); animation: pulse-red 1.5s infinite; }
#view-voice #mic-btn.thinking { background: #9B59B6; box-shadow: 0 8px 20px rgba(155, 89, 182, 0.4); animation: pulse-purple 2s infinite; cursor: wait; }
#view-voice #interim-text { min-height: 30px; font-size: 24px; margin-bottom: 10px; font-weight: bold; color: #333; text-align: center; width: 90%; word-wrap: break-word; }
@keyframes pulse-red { 0% { transform: scale(0.95); box-shadow: 0 0 0 0 rgba(231,76,60,0.5); } 70% { transform: scale(1.05); box-shadow: 0 0 0 20px rgba(231,76,60,0); } 100% { transform: scale(0.95); box-shadow: 0 0 0 0 rgba(231,76,60,0); } }
@keyframes pulse-purple { 0% { transform: scale(0.95); box-shadow: 0 0 0 0 rgba(155, 89, 182, 0.6); } 50% { transform: scale(1.05); box-shadow: 0 0 0 15px rgba(155, 89, 182, 0); } 100% { transform: scale(0.95); box-shadow: 0 0 0 0 rgba(155, 89, 182, 0); } }

/* App 2: 注音打字專屬 */
#view-zhuyin #input-area { flex: 0 0 auto; background: #D1D4D9; display: flex; flex-direction: column; padding-top: 5px; box-shadow: 0 -2px 10px rgba(0,0,0,0.1); }
#view-zhuyin #candidate-bar { height: 60px; display: flex; align-items: center; padding: 0 10px; overflow-x: auto; white-space: nowrap; gap: 10px; }
#view-zhuyin .candidate-btn { padding: 8px 20px; font-size: 28px; background: #fff; border: none; border-radius: 8px; color: #000; box-shadow: 0 1px 3px rgba(0,0,0,0.15); }
#view-zhuyin .candidate-btn:active { background: #E0E0E0; transform: scale(0.95); }
#view-zhuyin #compose-bar { display: flex; padding: 8px 10px; gap: 12px; align-items: center; }
#view-zhuyin #text-input { flex: 1; font-size: 32px; background: white; border-radius: 12px; height: 55px; padding: 0 15px; display: flex; align-items: center; overflow-x: auto; white-space: nowrap; border: 1px solid #ccc;}
#view-zhuyin #current-zhuyin { color: #0084FF; border-bottom: 3px solid #0084FF; margin-left: 2px; }
#view-zhuyin #send-btn { background: #34A853; color: white; border: none; border-radius: 12px; padding: 0 25px; height: 55px; font-size: 26px; font-weight: bold; cursor: pointer; white-space: nowrap; }
#view-zhuyin #keyboard { padding: 8px 8px 15px 8px; display: flex; flex-direction: column; gap: 8px; user-select: none; }
#view-zhuyin .key-row { display: flex; justify-content: center; gap: 6px; }
#view-zhuyin .key-btn { flex: 1; max-width: 9%; height: 65px; font-size: 34px; font-weight: bold; background: #fff; border: none; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.2); display: flex; justify-content: center; align-items: center; color: #000; }
#view-zhuyin .key-btn:active { background: #E0E0E0; }
#view-zhuyin .key-btn.del { background: #A4ABB3; color: white; font-size: 26px; }
#view-zhuyin .key-btn.spacebar { flex: 6; max-width: 60%; font-size: 28px; color: #666; background: #fff; }

/* App 3: 拼音特訓專屬 */
#view-spell { background: var(--bg-color); }
#view-spell .status-bar { display: flex; justify-content: space-between; padding: 8px 25px; background: white; font-weight: bold; font-size: 1.2rem; border-bottom: 2px solid #ddd; flex-shrink: 0;}
#view-spell .target-card { background: white; width: 100%; flex: 1; min-height: 0; border-radius: 20px; padding: 15px; box-shadow: 0 8px 20px rgba(0,0,0,0.08); display: flex; flex-direction: column; border: 3px solid #e8eaed; position: relative; overflow-y: auto; overflow-x: hidden; scroll-behavior: smooth;}
#view-spell .sentence-progress { font-size: 2.5rem; font-weight: bold; text-align: center; margin-bottom: 5px; color: #bdc3c7; letter-spacing: 4px; border-bottom: 2px dashed #eee; padding-bottom: 10px; display: flex; justify-content: center; flex-wrap: wrap; gap: 5px; flex-shrink: 0;}
#view-spell .sentence-progress span { transition: all 0.3s; }
#view-spell .sentence-progress span.completed { color: #2ecc71; }
#view-spell .sentence-progress span.current { color: var(--primary); font-size: 3.2rem; text-decoration: underline; text-underline-offset: 8px; text-decoration-color: var(--accent); text-decoration-thickness: 5px; }
#view-spell .focus-area { display: flex; justify-content: center; align-items: center; gap: 20px; flex: 1; margin-top: 5px; min-height: 0;}
#view-spell .focus-char { font-size: 9rem; font-weight: 900; color: var(--primary); line-height: 1; text-shadow: 2px 2px 10px rgba(0,0,0,0.05); flex-shrink: 0;}
#view-spell .vertical-zhuyin { display: flex; flex-direction: column; gap: 8px; justify-content: center; }
#view-spell .slot { width: 65px; height: 70px; border: 4px dashed #bdc3c7; border-radius: 12px; display: flex; justify-content: center; align-items: center; font-size: 3.5rem; font-weight: 900; color: #7f8c8d; background: #fdfdfd; transition: all 0.3s; overflow: hidden; flex-shrink: 0;}
#view-spell .slot.active { border-color: var(--primary); border-style: solid; background: #f5eef8; color: var(--primary); box-shadow: 0 0 15px rgba(155, 89, 182, 0.4); transform: scale(1.1); z-index: 2; }
#view-spell .slot.filled { border-color: #2ecc71; border-style: solid; background: #eafaf1; color: #2ecc71; }
#view-spell .tone-mark { display: inline-block; position: relative; top: 0.12em; transform: scale(1.4); line-height: 1; }
#view-spell .options-area { width: 100%; min-height: 140px; flex-shrink: 0; display: flex; flex-wrap: wrap; justify-content: center; align-items: center; gap: 15px 25px; padding-bottom: 5px; }
#view-spell .option-btn { flex: 0 0 auto; width: 120px; height: 110px; border-radius: 25px; border: none; font-size: 4rem; font-weight: 900; background: white; color: #34495e; box-shadow: 0 8px 0 #bdc3c7, 0 12px 20px rgba(0,0,0,0.15); cursor: pointer; transition: transform 0.1s, box-shadow 0.1s; display: flex; justify-content: center; align-items: center; }
#view-spell .option-btn:active { transform: translateY(8px); box-shadow: 0 0 0 #bdc3c7, 0 4px 8px rgba(0,0,0,0.1); }
#view-spell .option-btn:disabled { opacity: 0.7; pointer-events: none; }
#view-spell .option-btn.wrong { animation: shake 0.4s; background: #fadbd8; color: #e74c3c; box-shadow: 0 8px 0 #e74c3c; }
#view-spell .controls { width: 100%; display: flex; justify-content: center; align-items: center; height: 70px; flex-shrink: 0; padding-bottom: max(10px, env(safe-area-inset-bottom)); }
#view-spell .ctrl-btn { width: 90%; max-width: 500px; height: 60px; border-radius: 18px; border: none; background: var(--accent); font-size: 2rem; font-weight: bold; color: #8e44ad; box-shadow: 0 5px 0 rgba(0,0,0,0.1); cursor: pointer; transition: all 0.1s; display: flex; justify-content: center; align-items: center; gap: 10px; }
#view-spell .ctrl-btn:active { transform: translateY(5px); box-shadow: none; }
#view-spell .level-up-overlay { position: absolute; top:0; left:0; right:0; bottom:0; background: rgba(255,255,255,0.95); z-index: 50; display: none; flex-direction: column; justify-content: center; align-items: center; border-radius: 20px; }
#view-spell .level-up-text { font-size: 4rem; color: var(--accent); font-weight: 900; text-shadow: 2px 2px 5px rgba(0,0,0,0.2); text-align: center; line-height: 1.3; }

@keyframes shake { 0%, 100% {transform: translateX(0);} 25% {transform: translateX(-12px);} 50% {transform: translateX(12px);} 75% {transform: translateX(-12px);} }

/* 橫向平板優化 */
@media (max-height: 800px) and (orientation: landscape) {
    #global-top-bar { height: 40px; padding: 5px 15px; }
    .app-view { height: calc(100dvh - 40px); }
    #view-menu { flex-direction: row; }
    .menu-btn { width: 30%; height: 120px; font-size: 1.5rem; flex-direction: column; gap: 10px; padding: 20px; }
    
    #view-voice #mic-btn { width: 70px; height: 70px; font-size: 30px; }
    #view-voice .message { font-size: 22px; padding: 10px 18px; }
    
    #view-zhuyin .message { font-size: 26px; padding: 12px 20px; }
    #view-zhuyin #candidate-bar { height: 55px; gap: 12px; }
    #view-zhuyin .candidate-btn { font-size: 26px; padding: 6px 16px; }
    #view-zhuyin #text-input { height: 50px; font-size: 30px; }
    #view-zhuyin #send-btn { height: 50px; font-size: 24px; padding: 0 20px; }
    #view-zhuyin #keyboard { padding: 5px 8px 10px 8px; gap: 6px; }
    #view-zhuyin .key-row { gap: 6px; }
    #view-zhuyin .key-btn { height: 55px; font-size: 32px; border-radius: 8px; }
    
    #view-spell .status-bar { padding: 5px 20px; }
    #view-spell .target-card { padding: 10px 15px; border-radius: 15px;}
    #view-spell .sentence-progress { font-size: 2.2rem; margin-bottom: 0px; padding-bottom: 5px; letter-spacing: 2px;}
    #view-spell .sentence-progress span.current { font-size: 2.8rem; text-underline-offset: 6px; }
    #view-spell .focus-area { gap: 25px; margin-top: 0; }
    #view-spell .focus-char { font-size: 8.5rem; }
    #view-spell .vertical-zhuyin { gap: 8px; }
    #view-spell .slot { width: 70px; height: 70px; font-size: 3.5rem; border-width: 4px; border-radius: 12px;}
    #view-spell .options-area { min-height: 120px; gap: 15px 25px; padding-bottom: 0;}
    #view-spell .option-btn { height: 100px; width: 110px; font-size: 4rem; box-shadow: 0 8px 0 #bdc3c7, 0 10px 15px rgba(0,0,0,0.15); border-radius: 20px; }
    #view-spell .option-btn:active { transform: translateY(8px); box-shadow: 0 0 0 #bdc3c7; }
    #view-spell .controls { height: 60px; padding-bottom: 5px;}
    #view-spell .ctrl-btn { height: 50px; font-size: 1.8rem; border-radius: 15px;}
}