* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    font-family: 'Microsoft JhengHei', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
}

body {
    background-color: #eef2f7; /* 淺藍灰色背景 */
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    padding: 20px;
}

.main-container {
    position: relative; /* 為絕對定位子元素準備 */
    width: 100%;
    max-width: 700px;
    height: 90vh;
    display: flex;
    flex-direction: column;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    border-radius: 8px;
    overflow: hidden;
    background-color: #ffffff;
}

.chat-container {
    width: 100%;
    max-width: 700px; /* 稍微寬一點 */
    background-color: #ffffff; /* 白色背景 */
    border-radius: 8px; /* 圓角 */
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1); /* 更明顯的陰影 */
    overflow: hidden;
    display: flex;
    flex-direction: column;
    height: 90vh; /* 佔據更多垂直空間 */
}

.chat-header {
    background-color: #4a90e2; /* 藍色標題背景 */
    color: white;
    padding: 15px 20px 15px 50px; /* 增加左側內邊距給按鈕留出空間 */
    font-size: 1.1rem;
    font-weight: bold;
    display: flex; /* 使用彈性布局 */
    align-items: center; /* 垂直居中 */
    justify-content: center; /* 水平居中 */
    position: relative; /* 保留相對定位 */
}

.chat-header h1 {
    font-size: 1.1rem; /* 標題字體大小 */
    margin: 0; /* 移除可能影響置中的 margin */
}

.chat-messages {
    flex: 1;
    min-height: 0; /* 允許在 flex 容器中正確收縮並啟用滾動 */
    overflow-y: auto;
    padding: 20px; /* 內邊距 */
    display: flex;
    flex-direction: column;
    position: relative; /* 使 resetBtn 相對定位，確保此行存在且正確 */
    padding-bottom: 80px;
}

.message {
    margin-bottom: 15px;
    max-width: 80%; /* 調整最大寬度，讓氣泡更寬但不超出 */
    overflow-wrap: break-word; /* 防止長單詞或長文本溢出 */
    word-break: break-word; /* 強制在必要時斷行 */
    padding: 12px 16px; /* 調整內邊距 */
    border-radius: 18px; /* 更大的圓角，類似氣泡 */
    word-wrap: break-word;
    line-height: 1.5;
    position: relative; /* 為偽元素定位準備 */
    white-space: pre-wrap; /* 保留空格和換行 */
}

.user-message {
    background-color: #dcf8c6; /* 用戶訊息背景綠色 */
    align-self: flex-end; /* 靠右對齊 */
    margin-left: 40px; /* 確保與左側有間距 */
    border-bottom-right-radius: 2px; /* 氣泡尖角 */
    box-sizing: border-box; /* 包含內邊距和邊框在內，避免寬度計算錯誤 */
}

.ai-message {
    background-color: #f1f0f0; /* AI訊息背景灰色 */
    align-self: flex-start; /* 靠左對齊 */
    margin-right: 40px; /* 確保與右側有間距 */
    border-bottom-left-radius: 2px; /* 氣泡尖角 */
    transition: padding 0.2s ease, background-color 0.3s ease;
    min-height: auto; /* 移除固定最小高度，讓高度自適應內容 */
    box-sizing: border-box; /* 包含內邊距和邊框在內，避免寬度計算錯誤 */
}

/* 警告狀態的AI消息樣式 */
.ai-message.connection-warning {
    background-color: #fff3cd; /* 淺黃色背景表示警告 */
    border-left: 3px solid #ffc107; /* 黃色邊框 */
    animation: pulse-warning 1.5s infinite;
}

@keyframes pulse-warning {
    0% { box-shadow: 0 0 0 0 rgba(255, 193, 7, 0.4); }
    70% { box-shadow: 0 0 0 6px rgba(255, 193, 7, 0); }
    100% { box-shadow: 0 0 0 0 rgba(255, 193, 7, 0); }
}

/* 嘗試添加氣泡尖角效果 (使用偽元素) */
.user-message::after {
    content: '';
    position: absolute;
    bottom: 0;
    right: -7px; /* 位置 */
    border-width: 5px 5px 0;
    border-style: solid;
    border-color: #dcf8c6 transparent transparent transparent;
}

.ai-message::after {
    content: '';
    position: absolute;
    bottom: 0;
    left: -7px; /* 位置 */
    border-width: 5px 5px 0;
    border-style: solid;
    border-color: #f1f0f0 transparent transparent transparent;
    transform: rotate(270deg); /* 旋轉尖角方向 */
}

.ai-message.connection-warning::after {
    border-color: #fff3cd transparent transparent transparent;
}

.chat-input {
    display: flex;
    padding: 15px 20px; /* 增加左右內邊距 */
    background-color: #ffffff; /* 白色背景 */
    border-top: 1px solid #eee;
    align-items: center;
}

#userInput {
    flex: 1;
    padding: 10px 15px; /* 調整內邊距 */
    border: 1px solid #ddd;
    border-radius: 20px; /* 圓角輸入框 */
    outline: none;
    margin-right: 10px; /* 與按鈕間距 */
    font-size: 1rem;
    transition: border-color 0.3s;
}

#userInput:focus {
    border-color: #4a90e2;
    box-shadow: 0 0 0 2px rgba(74, 144, 226, 0.1);
}

#sendBtn {
    padding: 8px 15px;
    background-color: #4a90e2; /* 藍色按鈕 */
    color: white;
    border: none;
    border-radius: 20px; /* 圓角按鈕 */
    cursor: pointer;
    font-size: 1rem;
    transition: background-color 0.3s ease; /* 過渡效果 */
}

#sendBtn:hover {
    background-color: #357abd; /* hover 顏色變深 */
}

/* 用戶輸入狀態的發送按鈕 */
#sendBtn.user-typing {
    background-color: #48bb78; /* 綠色表示用戶正在輸入 */
    transform: scale(1.05);
}

/* 錯誤訊息樣式 */
.error-message {
    color: #f44336; /* 紅色 */
    font-size: 0.9rem;
    margin-top: 5px;
}

/* 光標樣式 */
.cursor {
    display: inline-block;
    width: 2px;
    height: 1.2em;
    background-color: #333;
    margin-left: 2px;
    animation: blink 1s step-end infinite;
    vertical-align: middle;
    border-radius: 1px;
    position: relative;
    top: 1px;
}

@keyframes blink {
    from, to { opacity: 1; }
    50% { opacity: 0; }
}

#resetBtn {
    position: absolute; /* 絕對定位 */
    left: 15px; /* 調整位置，確保在標頭左側區域內 */
    top: 8px;
    font-size: 1.5rem; /* 調整圖示大小 */
    color: white; /* 恢復白色 */
    background-color: transparent;
    border: none;
    padding: 2px; /* 縮小按鈕內邊距 */
    cursor: pointer;
    z-index: 5; /* 確保可點擊 */
    margin-right: 1000px; /* 增加右側間距，與標題保持距離 */
}

#resetBtn:hover {
    color: #e0e0e0; /* 滑鼠懸停時輕微變色 */
}

@media (max-width: 768px) {
    /* 此處可添加響應式樣式 */
    .message {
        max-width: 85%; /* 在小屏幕上擴大消息寬度 */
    }

    #resetBtn {
        bottom: 15px; /* 在小螢幕上調整位置 */
        right: 15px; /* 在小螢幕上調整位置 */
    }
}

/* 為打字機效果添加字符出現動畫 */
@keyframes typeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

/* 指示AI正在思考的樣式 */
.thinking {
    font-style: italic;
    color: #888;
    animation: pulse 1.5s infinite;
}

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

/* 優化禁用狀態的發送按鈕 */
#sendBtn:disabled {
    background-color: #cccccc;
    cursor: not-allowed;
    opacity: 0.7;
}

/* 強調文本 - 移除或修改此樣式，因為我們不再顯示「生成已取消」等強調文本 */
.ai-message em {
    /* 這個樣式已不再需要，但為了避免影響其他可能使用em的地方，
       我們保留這個選擇器但將其特殊樣式移除 */
    font-style: normal;
    color: inherit;
}

/* 聊天訊息中的超連結樣式 */
.chat-link {
    color: #4a90e2; /* 藍色超連結 */
    text-decoration: underline;
    cursor: pointer;
    transition: color 0.3s ease, text-decoration 0.3s ease;
    word-break: break-all; /* 確保長URL能夠換行 */
    display: inline-block;
    max-width: 100%;
}

.chat-link:hover {
    color: #357abd; /* 滑鼠懸停時顏色變深 */
    text-decoration: none;
}

.chat-link:visited {
    color: #6b46c1; /* 已訪問連結的顏色 */
}

/* 用戶訊息中的超連結 */
.user-message .chat-link {
    color: #2d7d32; /* 綠色系超連結，與用戶訊息背景協調 */
}

.user-message .chat-link:hover {
    color: #1b5e20; /* 滑鼠懸停時顏色變深 */
}

.user-message .chat-link:visited {
    color: #4a148c; /* 已訪問連結的顏色 */
}

/* AI訊息中的超連結 */
.ai-message .chat-link {
    color: #1976d2; /* 藍色系超連結，與AI訊息背景協調 */
}

.ai-message .chat-link:hover {
    color: #0d47a1; /* 滑鼠懸停時顏色變深 */
}

.ai-message .chat-link:visited {
    color: #4a148c; /* 已訪問連結的顏色 */
}
