/* Voice Chat 樣式 - 階段 1（手機優化版） */

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

:root {
    --bg-primary: #0f0f23;
    --bg-secondary: #1a1a2e;
    --bg-tertiary: #252544;
    --accent: #6366f1;
    --accent-hover: #818cf8;
    --text-primary: #e2e8f0;
    --text-secondary: #94a3b8;
    --text-muted: #64748b;
    --border: #334155;
    --user-bubble: #6366f1;
    --assistant-bubble: #1e293b;
    /* iOS 安全區 */
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
}

html, body {
    height: 100%;
    overflow: hidden;
    /* 防止 iOS 橡皮筋效果 */
    overscroll-behavior: none;
    -webkit-overflow-scrolling: touch;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang TC", "Microsoft JhengHei", "Helvetica Neue", sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    font-size: 16px; /* iOS 自動縮放防護 */
    /* 處理 iOS 100vh 問題 */
    min-height: 100vh;
    min-height: -webkit-fill-available;
}

.app {
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh; /* 動態視口高度，解決手機瀏覽器地址欄問題 */
    max-width: 900px;
    margin: 0 auto;
}

/* ===== 頂部狀態欄 ===== */

.header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 16px;
    padding-top: calc(12px + var(--safe-top));
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

.header-left {
    display: flex;
    align-items: center;
    gap: 12px;
    flex: 1;
    min-width: 0;
}

.header h1 {
    font-size: 17px;
    font-weight: 600;
    color: var(--text-primary);
    white-space: nowrap;
}

.status {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 3px 8px;
    background: var(--bg-tertiary);
    border-radius: 10px;
    font-size: 11px;
    color: var(--text-secondary);
    white-space: nowrap;
}

.status-dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: #f59e0b;
    animation: pulse 1.5s infinite;
    flex-shrink: 0;
}

.status.connected .status-dot {
    background: #10b981;
}

.status.error .status-dot {
    background: #ef4444;
    animation: none;
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.4; }
}

.version {
    font-size: 11px;
    color: var(--text-muted);
    white-space: nowrap;
}

/* ===== 聊天區域 ===== */

.chat-container {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    -webkit-overflow-scrolling: touch;
}

.chat-container::-webkit-scrollbar {
    width: 4px;
}

.chat-container::-webkit-scrollbar-track {
    background: transparent;
}

.chat-container::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 2px;
}

.message {
    display: flex;
    gap: 8px;
    animation: slideIn 0.3s ease-out;
}

@keyframes slideIn {
    from {
        opacity: 0;
        transform: translateY(8px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    background: var(--bg-tertiary);
    flex-shrink: 0;
}

.message-user {
    flex-direction: row-reverse;
}

.message-user .avatar {
    background: var(--accent);
}

.message-content {
    max-width: calc(100% - 50px);
    display: flex;
    flex-direction: column;
    gap: 3px;
    min-width: 0;
}

.message-user .message-content {
    align-items: flex-end;
}

.message-text {
    padding: 10px 14px;
    border-radius: 16px;
    background: var(--assistant-bubble);
    line-height: 1.5;
    white-space: pre-wrap;
    word-wrap: break-word;
    overflow-wrap: break-word;
    font-size: 15px;
}

.message-user .message-text {
    background: var(--user-bubble);
    color: white;
    border-bottom-right-radius: 4px;
}

.message-assistant .message-text {
    border-bottom-left-radius: 4px;
}

.message-text ul {
    margin: 6px 0;
    padding-left: 18px;
}

.message-text li {
    margin: 3px 0;
}

.message-time {
    font-size: 10px;
    color: var(--text-muted);
    padding: 0 4px;
}

/* 打字指示器 */
.typing-indicator {
    display: flex;
    gap: 4px;
    padding: 14px 16px;
    align-items: center;
}

.typing-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--text-secondary);
    animation: typingBounce 1.4s infinite;
}

.typing-dot:nth-child(2) { animation-delay: 0.2s; }
.typing-dot:nth-child(3) { animation-delay: 0.4s; }

@keyframes typingBounce {
    0%, 60%, 100% {
        transform: translateY(0);
        opacity: 0.4;
    }
    30% {
        transform: translateY(-5px);
        opacity: 1;
    }
}

/* ===== 輸入區域 ===== */

.input-area {
    padding: 12px 12px;
    padding-bottom: calc(20px + var(--safe-bottom));
    background: var(--bg-secondary);
    border-top: 1px solid var(--border);
    flex-shrink: 0;
}

.input-wrapper {
    display: flex;
    gap: 8px;
    align-items: flex-end;
    background: var(--bg-tertiary);
    border-radius: 20px;
    padding: 6px 6px 6px 14px;
    border: 1px solid var(--border);
    transition: border-color 0.2s;
}

.input-wrapper:focus-within {
    border-color: var(--accent);
}

#message-input {
    flex: 1;
    min-width: 0;
    background: transparent;
    border: none;
    outline: none;
    color: var(--text-primary);
    font-size: 16px; /* iOS 不會自動縮放 */
    font-family: inherit;
    resize: none;
    max-height: 100px;
    line-height: 1.4;
    padding: 6px 0;
}

#message-input::placeholder {
    color: var(--text-muted);
}

.send-button {
    background: var(--accent);
    color: white;
    border: none;
    border-radius: 50%;
    width: 36px;
    height: 36px;
    font-size: 18px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s, transform 0.1s;
    flex-shrink: 0;
}

.send-button:hover:not(:disabled) {
    background: var(--accent-hover);
}

.send-button:active:not(:disabled) {
    transform: scale(0.92);
}

.send-button:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.send-icon {
    display: inline-block;
    transform: translateX(1px);
}

.input-hint {
    text-align: right;
    font-size: 10px;
    color: var(--text-muted);
    margin-top: 4px;
    padding-right: 4px;
    height: 14px;
}

/* ===== 桌面端適配 ===== */

@media (min-width: 641px) {
    .header {
        padding: 16px 24px;
        padding-top: 16px;
    }

    .header h1 {
        font-size: 18px;
    }

    .status {
        padding: 4px 12px;
        font-size: 12px;
    }

    .status-dot {
        width: 8px;
        height: 8px;
    }

    .chat-container {
        padding: 24px;
        gap: 16px;
    }

    .message {
        gap: 12px;
    }

    .avatar {
        width: 36px;
        height: 36px;
        font-size: 20px;
    }

    .message-content {
        max-width: 70%;
    }

    .message-text {
        padding: 12px 16px;
        font-size: 15px;
    }

    .input-area {
        padding: 16px 24px;
        padding-bottom: calc(24px + var(--safe-bottom));
    }

    .input-wrapper {
        border-radius: 16px;
        padding: 8px 8px 8px 16px;
    }

    .send-button {
        border-radius: 12px;
        width: auto;
        height: 36px;
        padding: 0 16px;
        gap: 6px;
    }

    .send-button::before {
        content: "發送";
        font-size: 14px;
        font-weight: 500;
        margin-right: 4px;
    }

    .version {
        font-size: 12px;
    }
}

/* ===== 語音控制 ===== */

.voice-toggle {
    background: transparent;
    border: none;
    font-size: 18px;
    cursor: pointer;
    padding: 4px 8px;
    border-radius: 8px;
    transition: background 0.2s;
}

.voice-toggle:hover {
    background: var(--bg-tertiary);
}

/* 語音選擇面板 */
.voice-selector {
    position: fixed;
    top: 70px;
    right: 20px;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 12px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
    z-index: 100;
    max-width: 280px;
    max-height: 70vh;
    overflow-y: auto;
}

.voice-selector.hidden {
    display: none;
}

.voice-selector-title {
    font-size: 13px;
    color: var(--text-secondary);
    margin-bottom: 8px;
    padding: 0 4px;
}

.voice-item {
    display: block;
    width: 100%;
    background: transparent;
    border: none;
    color: var(--text-primary);
    padding: 10px 12px;
    border-radius: 8px;
    cursor: pointer;
    text-align: left;
    margin-bottom: 4px;
    transition: background 0.2s;
}

.voice-item:hover {
    background: var(--bg-tertiary);
}

.voice-item.active {
    background: var(--accent);
    color: white;
}

.voice-name {
    font-size: 14px;
    font-weight: 500;
    margin-bottom: 2px;
}

.voice-desc {
    font-size: 11px;
    opacity: 0.7;
}

/* 播放中指示器 */
.playing-indicator {
    position: fixed;
    bottom: 90px;
    left: 50%;
    transform: translateX(-50%);
    background: var(--accent);
    color: white;
    padding: 8px 16px;
    border-radius: 20px;
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 13px;
    box-shadow: 0 4px 12px rgba(99, 102, 241, 0.4);
    z-index: 50;
    animation: slideUp 0.3s ease-out;
}

.playing-indicator.fade-out {
    animation: fadeOut 0.3s ease-out forwards;
}

.playing-icon {
    font-size: 14px;
}

.playing-waves {
    display: flex;
    gap: 2px;
    align-items: center;
    height: 14px;
}

.playing-waves span {
    width: 2px;
    background: white;
    border-radius: 1px;
    animation: wave 0.8s ease-in-out infinite;
}

.playing-waves span:nth-child(1) { height: 60%; animation-delay: 0s; }
.playing-waves span:nth-child(2) { height: 100%; animation-delay: 0.2s; }
.playing-waves span:nth-child(3) { height: 70%; animation-delay: 0.4s; }
.playing-waves span:nth-child(4) { height: 50%; animation-delay: 0.1s; }

@keyframes wave {
    0%, 100% { transform: scaleY(1); }
    50% { transform: scaleY(0.4); }
}

@keyframes slideUp {
    from { transform: translateX(-50%) translateY(20px); opacity: 0; }
    to { transform: translateX(-50%) translateY(0); opacity: 1; }
}

@keyframes fadeOut {
    to { transform: translateX(-50%) translateY(20px); opacity: 0; }
}

/* 重新播放按鈕 */
.message-meta {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 0 4px;
}

.replay-btn {
    background: transparent;
    border: none;
    font-size: 12px;
    cursor: pointer;
    padding: 2px 4px;
    border-radius: 4px;
    color: var(--text-muted);
    transition: color 0.2s, background 0.2s;
}

.replay-btn:hover {
    color: var(--text-primary);
    background: var(--bg-tertiary);
}

/* Toast 提示 */
.toast {
    position: fixed;
    bottom: 90px;
    left: 50%;
    transform: translateX(-50%) translateY(20px);
    background: var(--bg-tertiary);
    color: var(--text-primary);
    padding: 10px 20px;
    border-radius: 20px;
    font-size: 13px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    z-index: 200;
    opacity: 0;
    transition: opacity 0.3s, transform 0.3s;
}

.toast.show {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

/* ===== 小屏手機適配（iPhone SE 等） ===== */

@media (max-width: 380px) {
    .header h1 {
        font-size: 15px;
    }

    .status {
        font-size: 10px;
        padding: 2px 6px;
    }

    .message-text {
        font-size: 14px;
        padding: 8px 12px;
    }

    #message-input {
        font-size: 15px;
    }
}
