/* 重置和基礎樣式 */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    background: #f0f0f0;
    height: 100vh;
    overflow: hidden;
    color: #333;
}

.chat-app {
    height: 100vh;
    display: flex;
    flex-direction: column;
    background: #fff;
}

/* 頂部導航欄 */
.chat-header {
    background: linear-gradient(135deg, #2196f3, #1976d2);
    color: white;
    padding: 0;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    position: relative;
    z-index: 100;
}

.header-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 16px;
}

.chat-info {
    display: flex;
    align-items: center;
    gap: 12px;
}

.avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.2);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
}

.chat-details h1 {
    font-size: 16px;
    font-weight: 600;
    margin: 0;
}

.status-text {
    font-size: 12px;
    opacity: 0.8;
    margin-top: 2px;
    display: block;
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 8px;
}

.clear-history-btn {
    background: rgba(255, 255, 255, 0.2);
    border: none;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s ease;
    color: white;
}

.clear-history-btn:hover {
    background: rgba(255, 255, 255, 0.3);
    transform: scale(1.05);
}

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

.clear-history-btn svg {
    width: 16px;
}

/* 清除緩存按鈕樣式 */
.clear-cache-btn {
    background: rgba(255, 255, 255, 0.2);
    border: none;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s ease;
    color: white;
}

.clear-cache-btn:hover {
    background: rgba(255, 255, 255, 0.3);
    transform: scale(1.05);
}

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

.clear-cache-btn svg {
    width: 16px;
    height: 16px;
}
    height: 16px;
}

.status-indicator {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background-color: #ff4444;
    animation: pulse 2s infinite;
}

.status-indicator.connected {
    background-color: #4caf50;
}

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

/* 聊天訊息區域 */
.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    background: #f0f0f0;
    scroll-behavior: smooth;
}

.message-group {
    margin-bottom: 16px;
}

.message {
    display: flex;
    margin-bottom: 8px;
    animation: slideInUp 0.3s ease-out;
}

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

/* AI 訊息 (左側) */
.ai-message {
    justify-content: flex-start;
}

.ai-message .message-bubble {
    background: white;
    border-radius: 18px 18px 18px 4px;
    padding: 12px 16px;
    max-width: 70%;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
    position: relative;
}

/* 使用者訊息 (右側) */
.user-message {
    justify-content: flex-end;
}

.user-message .message-bubble {
    background: linear-gradient(135deg, #2196f3, #1976d2);
    color: white;
    border-radius: 18px 18px 4px 18px;
    padding: 12px 16px;
    max-width: 70%;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
    position: relative;
}

.message-content {
    font-size: 14px;
    line-height: 1.4;
    word-wrap: break-word;
    margin-bottom: 4px;
}

.message-time {
    font-size: 11px;
    opacity: 0.6;
    text-align: right;
}

/* 系統訊息 */
.system-message {
    justify-content: center;
    margin: 16px 0;
}

.system-message .message-bubble {
    background: rgba(33, 150, 243, 0.1);
    color: #1976d2;
    border-radius: 12px;
    padding: 8px 12px;
    font-size: 12px;
    text-align: center;
    max-width: 200px;
    border-left: 3px solid #2196f3;
}

/* 錯誤訊息 */
.error-message {
    justify-content: center;
    margin: 16px 0;
}

.error-message .message-bubble {
    background: #ffebee;
    color: #d32f2f;
    border-radius: 12px;
    padding: 8px 12px;
    font-size: 12px;
    text-align: center;
    max-width: 300px;
    border-left: 3px solid #d32f2f;
}

/* 載入動畫 */
.loading-message {
    justify-content: flex-start;
}

.loading-message .message-bubble {
    background: white;
    border-radius: 18px 18px 18px 4px;
    padding: 12px 16px;
    max-width: 120px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.loading {
    display: flex;
    align-items: center;
    gap: 8px;
    color: #666;
    font-size: 12px;
}

.loading-dots {
    display: flex;
    gap: 3px;
}

.loading-dots span {
    width: 4px;
    height: 4px;
    border-radius: 50%;
    background-color: #999;
    animation: loading 1.4s infinite ease-in-out;
}

.loading-dots span:nth-child(1) { animation-delay: -0.32s; }
.loading-dots span:nth-child(2) { animation-delay: -0.16s; }

@keyframes loading {
    0%, 80%, 100% {
        transform: scale(0);
        opacity: 0.5;
    }
    40% {
        transform: scale(1);
        opacity: 1;
    }
}

/* 底部輸入區域 */
.chat-input-area {
    background: white;
    border-top: 1px solid #e0e0e0;
    padding: 12px 16px;
    position: relative;
}

.input-container {
    display: flex;
    align-items: flex-end;
    gap: 8px;
}

.input-wrapper {
    flex: 1;
    display: flex;
    align-items: flex-end;
    gap: 8px;
    background: #f8f9fa;
    border-radius: 20px;
    padding: 8px 12px;
    border: 1px solid #e0e0e0;
    transition: border-color 0.3s ease;
}

.input-wrapper:focus-within {
    border-color: #2196f3;
    box-shadow: 0 0 0 2px rgba(33, 150, 243, 0.1);
}

#message-input {
    flex: 1;
    border: none;
    background: transparent;
    font-size: 14px;
    line-height: 1.4;
    resize: none;
    outline: none;
    font-family: inherit;
    max-height: 100px;
    min-height: 20px;
}

#message-input::placeholder {
    color: #999;
}

.send-button {
    background: linear-gradient(135deg, #2196f3, #1976d2);
    color: white;
    border: none;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s ease;
    flex-shrink: 0;
    box-shadow: 0 2px 4px rgba(33, 150, 243, 0.2);
    position: relative;
}

.send-button:hover:not(:disabled) {
    background: linear-gradient(135deg, #1976d2, #1565c0);
    transform: scale(1.05);
    box-shadow: 0 2px 8px rgba(33, 150, 243, 0.3);
}

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

.send-button:disabled {
    background: #ccc;
    cursor: not-allowed;
    transform: none;
    box-shadow: none;
}

.send-button svg {
    width: 18px;
    height: 18px;
}

/* 圖片容器樣式 */
.image-container {
    margin-top: 8px;
    width: 100%;
    overflow: hidden;
}

.image-scroll-container {
    display: flex;
    gap: 8px;
    overflow-x: auto;
    overflow-y: hidden;
    padding: 4px 0;
    scrollbar-width: thin;
    scrollbar-color: rgba(0, 0, 0, 0.2) transparent;
}

.image-scroll-container::-webkit-scrollbar {
    height: 4px;
}

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

.image-scroll-container::-webkit-scrollbar-thumb {
    background: rgba(0, 0, 0, 0.2);
    border-radius: 2px;
}

.image-scroll-container::-webkit-scrollbar-thumb:hover {
    background: rgba(0, 0, 0, 0.3);
}

.image-wrapper {
    flex-shrink: 0;
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    transition: transform 0.2s ease;
}

.image-wrapper:hover {
    transform: scale(1.02);
}

.message-image {
    width: 120px;
    height: 120px;
    object-fit: cover;
    display: block;
    border-radius: 8px;
}

.image-error {
    width: 120px;
    height: 120px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #f5f5f5;
    color: #999;
    font-size: 12px;
    border-radius: 8px;
    border: 1px dashed #ddd;
}

/* 產品容器樣式 */
.product-container {
    margin-top: 12px;
    width: 100%;
    overflow: hidden;
}

.product-scroll-container {
    display: flex;
    gap: 12px;
    overflow-x: auto;
    overflow-y: hidden;
    padding: 4px 0;
    scrollbar-width: thin;
    scrollbar-color: rgba(0, 0, 0, 0.2) transparent;
}

.product-scroll-container::-webkit-scrollbar {
    height: 4px;
}

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

.product-scroll-container::-webkit-scrollbar-thumb {
    background: rgba(0, 0, 0, 0.2);
    border-radius: 2px;
}

.product-scroll-container::-webkit-scrollbar-thumb:hover {
    background: rgba(0, 0, 0, 0.3);
}

.product-card {
    flex-shrink: 0;
    width: 280px;
    background: #fff;
    border-radius: 12px;
    padding: 16px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    border: 1px solid #e0e0e0;
    transition: all 0.3s ease;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.product-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
    border-color: #2196f3;
}

.product-name {
    font-size: 16px;
    font-weight: 600;
    color: #333;
    margin: 0;
    line-height: 1.3;
}

.product-price {
    font-size: 18px;
    font-weight: 700;
    color: #2196f3;
    margin: 0;
}

.product-description {
    font-size: 14px;
    color: #666;
    line-height: 1.4;
    margin: 0;
    flex-grow: 1;
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.product-link {
    display: inline-block;
    background: linear-gradient(135deg, #2196f3, #1976d2);
    color: white;
    text-decoration: none;
    padding: 8px 16px;
    border-radius: 6px;
    font-size: 14px;
    font-weight: 500;
    text-align: center;
    transition: all 0.2s ease;
    margin-top: auto;
}

.product-link:hover {
    background: linear-gradient(135deg, #1976d2, #1565c0);
    transform: translateY(-1px);
    box-shadow: 0 4px 8px rgba(33, 150, 243, 0.3);
}

.product-link:active {
    transform: translateY(0);
}

/* 產品圖片樣式 */
.product-image-container {
    width: 100%;
    height: 200px;
    border-radius: 8px;
    overflow: hidden;
    margin-bottom: 12px;
    background: #f5f5f5;
    display: flex;
    align-items: center;
    justify-content: center;
}

.product-image {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: transform 0.3s ease;
}

.product-image:hover {
    transform: scale(1.05);
}

.product-image-error {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #f5f5f5;
    color: #999;
    font-size: 14px;
    border-radius: 8px;
    border: 1px dashed #ddd;
}

/* 滾動條樣式 */
.chat-messages::-webkit-scrollbar {
    width: 6px;
}

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

.chat-messages::-webkit-scrollbar-thumb {
    background: rgba(33, 150, 243, 0.3);
    border-radius: 3px;
}

.chat-messages::-webkit-scrollbar-thumb:hover {
    background: rgba(33, 150, 243, 0.5);
}

/* 響應式設計 */
@media (max-width: 768px) {
    .header-content {
        padding: 10px 12px;
    }
    
    .chat-messages {
        padding: 12px;
    }
    
    .chat-input-area {
        padding: 10px 12px;
    }
    
    /* 產品卡片響應式設計 */
    .product-card {
        width: 240px;
        padding: 12px;
    }
    
    .product-image-container {
        height: 160px;
        margin-bottom: 8px;
    }
    
    .product-name {
        font-size: 14px;
    }
    
    .product-price {
        font-size: 16px;
    }
    
    .product-description {
        font-size: 12px;
        -webkit-line-clamp: 2;
    }
    
    .product-link {
        padding: 6px 12px;
        font-size: 12px;
    }
    
    .ai-message .message-bubble,
    .user-message .message-bubble {
        max-width: 85%;
    }
}
}

@media (max-width: 480px) {
    .avatar {
        width: 36px;
        height: 36px;
        font-size: 18px;
    }
    
    .chat-details h1 {
        font-size: 15px;
    }
    
    .message-content {
        font-size: 13px;
    }
    
    .ai-message .message-bubble,
    .user-message .message-bubble {
        max-width: 90%;
        padding: 10px 14px;
    }
}

/* 打字指示器 */
.typing-indicator {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 8px 12px;
    background: white;
    border-radius: 18px 18px 18px 4px;
    max-width: 60px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.typing-dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: #2196f3;
    animation: typing 1.4s infinite ease-in-out;
}

.typing-dot:nth-child(1) { animation-delay: -0.32s; }
.typing-dot:nth-child(2) { animation-delay: -0.16s; }

@keyframes typing {
    0%, 80%, 100% {
        transform: scale(0);
        opacity: 0.5;
    }
    40% {
        transform: scale(1);
        opacity: 1;
    }
}

/* Markdown 渲染樣式 */
.ai-message .message-content {
    line-height: 1.6;
}

.ai-message .message-content h2 {
    font-size: 18px;
    font-weight: 600;
    margin: 16px 0 8px 0;
    color: #1976d2;
    border-bottom: 2px solid #e3f2fd;
    padding-bottom: 4px;
}

.ai-message .message-content h3 {
    font-size: 16px;
    font-weight: 600;
    margin: 12px 0 6px 0;
    color: #1976d2;
}

.ai-message .message-content strong {
    font-weight: 600;
    color: #1976d2;
}

.ai-message .message-content em {
    font-style: italic;
    color: #666;
}

.ai-message .message-content a {
    color: #2196f3;
    text-decoration: none;
    border-bottom: 1px solid transparent;
    transition: border-color 0.2s ease;
}

.ai-message .message-content a:hover {
    border-bottom-color: #2196f3;
}

.ai-message .message-content .list-item {
    display: flex;
    align-items: flex-start;
    margin: 8px 0;
    padding: 4px 0;
}

.ai-message .message-content .list-number {
    font-weight: 600;
    color: #1976d2;
    margin-right: 8px;
    min-width: 20px;
    display: inline-block;
}

.ai-message .message-content .list-bullet {
    color: #1976d2;
    margin-right: 8px;
    font-size: 16px;
    line-height: 1;
}

.ai-message .message-content br {
    margin: 4px 0;
}

/* 產品推薦卡片樣式 */
.product-recommendation {
    background: #f8f9fa;
    border-radius: 8px;
    padding: 12px;
    margin: 8px 0;
    border-left: 4px solid #2196f3;
}

.product-recommendation h4 {
    color: #1976d2;
    margin: 0 0 8px 0;
    font-size: 14px;
    font-weight: 600;
}

.product-recommendation p {
    margin: 4px 0;
    font-size: 13px;
    color: #666;
}

.product-recommendation .product-link {
    display: inline-block;
    margin-top: 8px;
    padding: 6px 12px;
    background: #2196f3;
    color: white;
    text-decoration: none;
    border-radius: 4px;
    font-size: 12px;
    transition: background-color 0.2s ease;
}

.product-recommendation .product-link:hover {
    background: #1976d2;
}

/* 比較表格樣式 */
.ai-message .message-content table {
    width: 100%;
    border-collapse: collapse;
    margin: 16px 0;
    background: white;
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    font-size: 13px;
    table-layout: auto;
    word-wrap: break-word;
    max-width: 100%;
    min-width: 300px;
}

.ai-message .message-content table thead th {
    background: linear-gradient(135deg, #2196f3, #1976d2);
    color: white;
    padding: 12px 8px;
    text-align: center;
    font-weight: 600;
    font-size: 14px;
    border: none;
}

.ai-message .message-content table tbody td {
    padding: 10px 8px;
    text-align: center;
    border-bottom: 1px solid #e0e0e0;
    vertical-align: middle;
    word-wrap: break-word;
    overflow-wrap: break-word;
    hyphens: auto;
}

.ai-message .message-content table tbody tr:last-child td {
    border-bottom: none;
}

.ai-message .message-content table tbody tr:nth-child(even) {
    background-color: #f8f9fa;
}

.ai-message .message-content table tbody tr:hover {
    background-color: #e3f2fd;
    transition: background-color 0.2s ease;
}

/* 向後兼容舊的表格結構 */
.ai-message .message-content table th {
    background: linear-gradient(135deg, #2196f3, #1976d2);
    color: white;
    padding: 12px 8px;
    text-align: center;
    font-weight: 600;
    font-size: 14px;
    border: none;
}

.ai-message .message-content table td {
    padding: 10px 8px;
    text-align: center;
    border-bottom: 1px solid #e0e0e0;
    vertical-align: middle;
}

.ai-message .message-content table tr:last-child td {
    border-bottom: none;
}

.ai-message .message-content table tr:nth-child(even) {
    background-color: #f8f9fa;
}

.ai-message .message-content table tr:hover {
    background-color: #e3f2fd;
    transition: background-color 0.2s ease;
}

/* 勝出標記樣式 */
.ai-message .message-content table .winner {
    color: #4caf50;
    font-weight: 600;
    font-size: 14px;
}

.ai-message .message-content table .loser {
    color: #f44336;
    font-weight: 600;
    font-size: 14px;
}

.ai-message .message-content table .neutral {
    color: #666;
    font-weight: 500;
}

/* 比較項目列樣式 */
.ai-message .message-content table td:first-child {
    text-align: left;
    font-weight: 600;
    color: #1976d2;
    background-color: #f5f5f5;
    width: 20%;
}

.ai-message .message-content table td:nth-child(2) {
    width: 30%;
}

.ai-message .message-content table td:nth-child(3) {
    width: 30%;
}

.ai-message .message-content table td:nth-child(4) {
    width: 20%;
    text-align: center;
}

/* 響應式表格 */
@media (max-width: 768px) {
    .ai-message .message-content table {
        font-size: 12px;
        table-layout: auto;
        min-width: 250px;
    }
    
    .ai-message .message-content table th,
    .ai-message .message-content table td {
        padding: 8px 6px;
        word-wrap: break-word;
        overflow-wrap: break-word;
        hyphens: auto;
    }
    
    .ai-message .message-content table th {
        font-size: 13px;
    }
    
    .ai-message .message-content table td:first-child {
        width: auto;
        min-width: 80px;
    }
    
    .ai-message .message-content table td:nth-child(2),
    .ai-message .message-content table td:nth-child(3) {
        width: auto;
        min-width: 120px;
    }
    
    .ai-message .message-content table td:nth-child(4) {
        width: auto;
        min-width: 60px;
    }
}

@media (max-width: 480px) {
    .ai-message .message-content table {
        font-size: 11px;
        table-layout: auto;
    }
    
    .ai-message .message-content table th,
    .ai-message .message-content table td {
        padding: 6px 4px;
        word-wrap: break-word;
        overflow-wrap: break-word;
    }
    
    .ai-message .message-content table th {
        font-size: 12px;
    }
    
    .ai-message .message-content table td:first-child {
        width: auto;
        min-width: 60px;
    }
    
    .ai-message .message-content table td:nth-child(2),
    .ai-message .message-content table td:nth-child(3) {
        width: auto;
        min-width: 100px;
    }
    
    .ai-message .message-content table td:nth-child(4) {
        width: auto;
        min-width: 50px;
    }
}