/**
 * @name: chat.css
 * @description: 前台線上對話浮動元件樣式
 * @version: 1.0.0
 */

/* ================================
   浮動按鈕 (FAB)
   ================================ */
.chat-fab {
    position: fixed;
    bottom: 28px;
    right: 28px;
    width: 56px;
    height: 56px;
    border-radius: 50%;
    border: none;
    background: var(--primary-color);
    color: var(--text-inverse);
    font-size: 1.4rem;
    cursor: pointer;
    box-shadow: var(--shadow-lg);
    z-index: 1040;
    transition: transform var(--transition-speed), box-shadow var(--transition-speed), opacity var(--transition-speed);
    display: flex;
    align-items: center;
    justify-content: center;
}

.chat-fab:hover {
    transform: scale(1.08);
    box-shadow: 0 6px 24px rgba(0, 0, 0, 0.25);
}

.chat-fab:active {
    transform: scale(0.95);
}

.chat-fab.hidden {
    opacity: 0;
    pointer-events: none;
    transform: scale(0.5);
}

/* 未讀 badge */
.chat-fab-badge {
    position: absolute;
    top: -4px;
    right: -4px;
    min-width: 20px;
    height: 20px;
    border-radius: 10px;
    background: #dc3545;
    color: #fff;
    font-size: 0.7rem;
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 5px;
    line-height: 1;
}

/* ================================
   對話視窗 (小視窗模式)
   ================================ */
.chat-window {
    position: fixed;
    bottom: 96px;
    right: 28px;
    width: 380px;
    height: 520px;
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    border-radius: 16px;
    box-shadow: var(--shadow-lg);
    z-index: 1041;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    transition: all 0.35s cubic-bezier(0.4, 0, 0.2, 1);
    transform-origin: bottom right;
}

/* 進場動畫 */
.chat-window.chat-entering {
    animation: chatSlideIn 0.3s cubic-bezier(0.4, 0, 0.2, 1) forwards;
}

.chat-window.chat-leaving {
    animation: chatSlideOut 0.25s cubic-bezier(0.4, 0, 0.2, 1) forwards;
}

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

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

/* 放大模式 */
.chat-window.chat-expanded {
    bottom: 50%;
    right: 50%;
    transform: translate(50%, 50%);
    width: 850px;
    height: 700px;
    border-radius: 12px;
    transform-origin: center center;
}

/* 遮罩 */
.chat-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.45);
    z-index: 1040;
    backdrop-filter: blur(2px);
    transition: opacity 0.3s ease;
}

/* ================================
   頂部工具列
   ================================ */
.chat-window-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    background: var(--primary-color);
    color: var(--text-inverse);
    flex-shrink: 0;
}

.chat-window-title {
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 600;
    font-size: 0.95rem;
}

.chat-window-actions {
    display: flex;
    gap: 4px;
}

.chat-header-btn {
    background: transparent;
    border: none;
    color: var(--text-inverse);
    width: 32px;
    height: 32px;
    border-radius: 6px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.9rem;
    transition: background var(--transition-speed);
}

.chat-header-btn:hover {
    background: rgba(255, 255, 255, 0.2);
}

/* ================================
   訊息區域
   ================================ */
.chat-window-body {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    background: var(--bg-secondary);
}

[data-theme="dark"] .chat-window-body {
    background: var(--bg-tertiary);
}

/* 歡迎訊息 */
.chat-welcome {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: var(--text-tertiary);
    text-align: center;
    gap: 12px;
}

.chat-welcome i {
    font-size: 2.5rem;
    opacity: 0.5;
}

.chat-welcome p {
    margin: 0;
    font-size: 0.9rem;
    line-height: 1.5;
}

/* 訊息氣泡包裝 */
.chat-bubble-wrapper {
    display: flex;
    flex-direction: column;
    max-width: 80%;
}

.chat-bubble-wrapper.sent {
    align-self: flex-end;
    align-items: flex-end;
}

.chat-bubble-wrapper.received {
    align-self: flex-start;
    align-items: flex-start;
}

/* 訊息氣泡 */
.chat-bubble {
    padding: 10px 14px;
    border-radius: 16px;
    word-wrap: break-word;
    position: relative;
    font-size: 0.88rem;
    line-height: 1.5;
}

.chat-bubble-wrapper.sent .chat-bubble {
    background: var(--primary-color);
    color: var(--text-inverse);
    border-bottom-right-radius: 4px;
}

.chat-bubble-wrapper.received .chat-bubble {
    background: var(--bg-primary);
    color: var(--text-primary);
    border: 1px solid var(--border-color);
    border-bottom-left-radius: 4px;
}

[data-theme="dark"] .chat-bubble-wrapper.received .chat-bubble {
    background: var(--bg-secondary);
}

.chat-bubble-sender {
    font-size: 0.72rem;
    font-weight: 600;
    margin-bottom: 3px;
    opacity: 0.75;
}

.chat-bubble-content {
    white-space: pre-wrap;
    word-break: break-word;
}

/* 時間顯示在泡泡外側右下方 */
.chat-bubble-time {
    font-size: 0.66rem;
    color: var(--text-tertiary);
    margin-top: 3px;
    padding: 0 4px;
}

/* 附件顯示 */
.chat-bubble-attachments {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-top: 6px;
}

.chat-bubble-attachment {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 3px 8px;
    border-radius: 6px;
    font-size: 0.75rem;
    background: rgba(255, 255, 255, 0.15);
    text-decoration: none;
    color: inherit;
    transition: background var(--transition-speed);
}

.chat-bubble-wrapper.received .chat-bubble-attachment {
    background: var(--bg-tertiary);
}

.chat-bubble-attachment:hover {
    background: rgba(255, 255, 255, 0.3);
}

.chat-bubble-wrapper.received .chat-bubble-attachment:hover {
    background: var(--bg-secondary);
}

/* 圖片附件在泡泡內預覽 */
.chat-bubble-img-link {
    display: inline-block;
    border-radius: 8px;
    overflow: hidden;
    line-height: 0;
    transition: opacity var(--transition-speed);
}

.chat-bubble-img-link:hover {
    opacity: 0.85;
}

.chat-bubble-img {
    width: 160px;
    height: 160px;
    object-fit: cover;
    border-radius: 8px;
    cursor: pointer;
}

/* 貼上預覽（輸入區域上方） */
.chat-paste-preview {
    position: relative;
    display: inline-block;
}

.chat-paste-preview img {
    width: 80px;
    height: 80px;
    object-fit: cover;
    border-radius: 6px;
    border: 2px solid var(--border-color);
}

.chat-paste-preview .chat-attachment-remove {
    position: absolute;
    top: -6px;
    right: -6px;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: #dc3545;
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.65rem;
    cursor: pointer;
    border: none;
    padding: 0;
}

/* ================================
   附件預覽列
   ================================ */
.chat-attachments-preview {
    display: flex;
    gap: 6px;
    padding: 8px 12px;
    background: var(--bg-secondary);
    border-top: 1px solid var(--border-color);
    flex-wrap: wrap;
    flex-shrink: 0;
}

.chat-attachment-item {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 4px 8px;
    border-radius: 6px;
    font-size: 0.75rem;
    background: var(--bg-tertiary);
    color: var(--text-secondary);
    max-width: 150px;
}

.chat-attachment-item span {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.chat-attachment-remove {
    background: none;
    border: none;
    color: var(--text-tertiary);
    cursor: pointer;
    padding: 0;
    font-size: 0.8rem;
    line-height: 1;
    flex-shrink: 0;
}

.chat-attachment-remove:hover {
    color: #dc3545;
}

/* ================================
   輸入區域
   ================================ */
.chat-window-footer {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    padding: 10px 12px 4px;
    background: var(--bg-primary);
    border-top: 1px solid var(--border-color);
    flex-shrink: 0;
}

[data-theme="dark"] .chat-window-footer {
    background: var(--bg-secondary);
}

.chat-attach-btn {
    background: transparent;
    border: none;
    color: var(--text-tertiary);
    font-size: 1.2rem;
    cursor: pointer;
    padding: 6px;
    border-radius: 6px;
    transition: color var(--transition-speed), background var(--transition-speed);
    flex-shrink: 0;
}

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

.chat-input {
    flex: 1;
    border: 1px solid var(--border-color);
    border-radius: 10px;
    padding: 8px 12px;
    font-size: 0.88rem;
    resize: none;
    max-height: calc(1.4em * 3 + 18px);  /* 小型模式最多 3 行 */
    line-height: 1.4;
    background: var(--bg-secondary);
    color: var(--text-primary);
    outline: none;
    transition: border-color var(--transition-speed), box-shadow var(--transition-speed), max-height var(--transition-speed);
    overflow-y: auto;
}

/* 大型模式輸入框最多 6 行 */
.chat-expanded .chat-input {
    max-height: calc(1.4em * 6 + 18px);
}

.chat-input:focus {
    border-color: var(--primary-color);
    box-shadow: 0 0 0 2px rgba(195, 13, 35, 0.15);
}

[data-theme="dark"] .chat-input:focus {
    box-shadow: 0 0 0 2px rgba(212, 175, 55, 0.15);
}

.chat-send-btn {
    background: var(--primary-color);
    border: none;
    color: var(--text-inverse);
    width: 36px;
    height: 36px;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.9rem;
    transition: transform var(--transition-speed), background var(--transition-speed);
    flex-shrink: 0;
}

.chat-send-btn:hover {
    transform: scale(1.08);
}

.chat-send-btn:active {
    transform: scale(0.92);
}

.chat-send-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}

.chat-footer-hint {
    text-align: center;
    padding: 0 12px 8px;
    background: var(--bg-primary);
    flex-shrink: 0;
}

[data-theme="dark"] .chat-footer-hint {
    background: var(--bg-secondary);
}

.chat-footer-hint small {
    color: var(--text-tertiary);
    font-size: 0.7rem;
}

/* ================================
   載入狀態
   ================================ */
.chat-loading {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    color: var(--text-tertiary);
}

.chat-loading-dots {
    display: inline-flex;
    gap: 4px;
}

.chat-loading-dots span {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--text-tertiary);
    animation: chatDotPulse 1.2s infinite;
}

.chat-loading-dots span:nth-child(2) { animation-delay: 0.2s; }
.chat-loading-dots span:nth-child(3) { animation-delay: 0.4s; }

@keyframes chatDotPulse {
    0%, 80%, 100% { opacity: 0.3; transform: scale(0.8); }
    40% { opacity: 1; transform: scale(1); }
}

/* ================================
   日期分隔線
   ================================ */
.chat-date-divider {
    display: flex;
    align-items: center;
    gap: 12px;
    margin: 8px 0;
    color: var(--text-tertiary);
    font-size: 0.72rem;
}

.chat-date-divider::before,
.chat-date-divider::after {
    content: '';
    flex: 1;
    height: 1px;
    background: var(--border-color);
}

/* ================================
   響應式
   ================================ */
@media (max-width: 480px) {
    .chat-window {
        bottom: 0;
        right: 0;
        width: 100%;
        height: 100%;
        border-radius: 0;
    }

    .chat-window.chat-expanded {
        width: 100%;
        height: 100%;
        bottom: 0;
        right: 0;
        transform: none;
        border-radius: 0;
    }

    .chat-fab {
        bottom: 16px;
        right: 16px;
        width: 50px;
        height: 50px;
        font-size: 1.2rem;
    }
}

/* ================================================================
   超級 AI 助理模式
   ================================================================ */

/* FAB 按鈕 - AI 漸層 */
.chat-fab-ai {
    background: linear-gradient(135deg, #6366f1, #8b5cf6);
    box-shadow: 0 4px 16px rgba(99, 102, 241, 0.4);
}

.chat-fab-ai:hover {
    box-shadow: 0 6px 24px rgba(99, 102, 241, 0.5);
}

/* Header - AI 漸層 */
.chat-header-ai {
    background: linear-gradient(135deg, #6366f1, #8b5cf6);
}

/* AI 視窗稍微寬一點 */
.chat-window-ai {
    width: 420px;
    height: 560px;
}

.chat-window-ai.chat-expanded {
    width: 900px;
    height: 720px;
}

/* 發送按鈕 AI 漸層 */
.chat-send-btn-ai {
    background: linear-gradient(135deg, #6366f1, #8b5cf6);
}

/* 歡迎畫面 - AI 快捷按鈕 */
.chat-ai-quick-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    justify-content: center;
    margin-top: 12px;
    padding: 0 12px;
}

.chat-ai-quick-btn {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 7px 14px;
    border-radius: 20px;
    border: 1px solid var(--border-color);
    background: var(--bg-primary);
    color: var(--text-secondary);
    font-size: 0.78rem;
    cursor: pointer;
    transition: all var(--transition-speed);
    white-space: nowrap;
}

.chat-ai-quick-btn:hover {
    background: linear-gradient(135deg, #6366f1, #8b5cf6);
    color: #fff;
    border-color: transparent;
    transform: translateY(-1px);
    box-shadow: 0 3px 12px rgba(99, 102, 241, 0.3);
}

.chat-ai-quick-btn i {
    font-size: 0.85rem;
}

/* AI 回覆泡泡 - Markdown 內容 */
.chat-bubble-ai {
    max-width: 100%;
}

.chat-ai-content {
    white-space: normal;
    word-break: break-word;
    line-height: 1.6;
}

.chat-ai-content p {
    margin: 0 0 8px;
}

.chat-ai-content p:last-child {
    margin-bottom: 0;
}

.chat-ai-content ul,
.chat-ai-content ol {
    margin: 4px 0 8px;
    padding-left: 20px;
}

.chat-ai-content li {
    margin-bottom: 2px;
}

.chat-ai-content code {
    padding: 1px 5px;
    border-radius: 4px;
    font-size: 0.82em;
    background: var(--bg-tertiary);
    color: #e83e8c;
    font-family: 'Consolas', 'Monaco', monospace;
}

[data-theme="dark"] .chat-ai-content code {
    color: #f0abfc;
}

.chat-ai-content pre {
    margin: 8px 0;
    padding: 10px 12px;
    border-radius: 8px;
    background: var(--bg-tertiary);
    overflow-x: auto;
    font-size: 0.82em;
    line-height: 1.5;
}

[data-theme="dark"] .chat-ai-content pre {
    background: #1e1e2e;
}

.chat-ai-content pre code {
    padding: 0;
    background: transparent;
    color: inherit;
}

.chat-ai-content h1, .chat-ai-content h2, .chat-ai-content h3,
.chat-ai-content h4, .chat-ai-content h5, .chat-ai-content h6 {
    margin: 12px 0 6px;
    font-weight: 600;
    line-height: 1.3;
}

.chat-ai-content h1 { font-size: 1.1em; }
.chat-ai-content h2 { font-size: 1.05em; }
.chat-ai-content h3 { font-size: 1em; }

.chat-ai-content blockquote {
    margin: 8px 0;
    padding: 6px 12px;
    border-left: 3px solid #6366f1;
    background: var(--bg-tertiary);
    border-radius: 0 6px 6px 0;
    color: var(--text-secondary);
    font-size: 0.9em;
}

.chat-ai-content table {
    width: 100%;
    border-collapse: collapse;
    margin: 8px 0;
    font-size: 0.85em;
}

.chat-ai-content th,
.chat-ai-content td {
    padding: 5px 8px;
    border: 1px solid var(--border-color);
    text-align: left;
}

.chat-ai-content th {
    background: var(--bg-tertiary);
    font-weight: 600;
}

.chat-ai-content a {
    color: #6366f1;
    text-decoration: none;
}

.chat-ai-content a:hover {
    text-decoration: underline;
}

/* 錯誤泡泡 */
.chat-bubble-error {
    background: #fef2f2 !important;
    border: 1px solid #fecaca !important;
    color: #dc2626 !important;
}

[data-theme="dark"] .chat-bubble-error {
    background: #451a1a !important;
    border-color: #7f1d1d !important;
    color: #fca5a5 !important;
}

/* AI 思考中指示器 */
.chat-ai-thinking-indicator {
    padding: 4px 0;
    display: flex;
    align-items: center;
    gap: 8px;
}

/* ================================
   工具呼叫結果區塊
   ================================ */
.chat-ai-tool-block {
    margin: 6px 12px;
    padding: 10px 14px;
    border-radius: 10px;
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    font-size: 0.82rem;
    max-width: 85%;
}

[data-theme="dark"] .chat-ai-tool-block {
    background: var(--bg-secondary);
}

.chat-ai-tool-label {
    font-weight: 600;
    color: var(--text-secondary);
    margin-bottom: 6px;
    display: flex;
    align-items: center;
    gap: 4px;
    font-size: 0.75rem;
}

.chat-ai-tool-meta {
    color: var(--text-tertiary);
    font-size: 0.78rem;
    line-height: 1.4;
    margin-top: 4px;
}

/* 圖片工具結果 */
.chat-ai-tool-image {
    border-color: #c4b5fd;
}

.chat-ai-generated-image {
    max-width: 100%;
    max-height: 240px;
    border-radius: 8px;
    cursor: pointer;
    transition: opacity var(--transition-speed);
    display: block;
    margin-top: 4px;
}

.chat-ai-generated-image:hover {
    opacity: 0.85;
}

/* 搜尋工具結果 */
.chat-ai-tool-search {
    border-color: #93c5fd;
}

.chat-ai-search-results {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.chat-ai-search-item {
    padding: 4px 0;
    border-bottom: 1px solid var(--border-color);
}

.chat-ai-search-item:last-child {
    border-bottom: none;
    padding-bottom: 0;
}

.chat-ai-search-title {
    color: #6366f1;
    text-decoration: none;
    font-weight: 500;
    font-size: 0.8rem;
}

.chat-ai-search-title:hover {
    text-decoration: underline;
}

.chat-ai-search-snippet {
    color: var(--text-tertiary);
    font-size: 0.75rem;
    line-height: 1.4;
    margin-top: 2px;
}

/* 資料庫工具結果 */
.chat-ai-tool-db {
    border-color: #86efac;
}

/* 網頁擷取結果 */
.chat-ai-tool-fetch {
    border-color: #fcd34d;
}

/* 工具錯誤 */
.chat-ai-tool-error {
    border-color: #fca5a5;
    background: #fef2f2;
}

[data-theme="dark"] .chat-ai-tool-error {
    background: #451a1a;
    border-color: #7f1d1d;
}

/* ================================
   AI 模式響應式
   ================================ */
@media (max-width: 480px) {
    .chat-window-ai {
        width: 100%;
        height: 100%;
    }

    .chat-window-ai.chat-expanded {
        width: 100%;
        height: 100%;
    }

    .chat-ai-quick-actions {
        flex-direction: column;
        align-items: stretch;
    }

    .chat-ai-quick-btn {
        justify-content: center;
    }
}

/* ================================
 * 代理黏著欄 & 對話列表
 * ================================ */

.chat-agent-bar {
    background: var(--bs-primary-bg-subtle, #e0e7ff);
    color: var(--bs-primary-text-emphasis, #3730a3);
    padding: 4px 12px;
    font-size: 0.78rem;
    display: flex;
    align-items: center;
    border-bottom: 1px solid var(--bs-border-color-translucent, #dee2e6);
}

[data-theme="dark"] .chat-agent-bar {
    background: rgba(99, 102, 241, 0.15);
    color: #a5b4fc;
}

.chat-agent-bar button {
    color: inherit;
    font-size: 0.85rem;
}

/* 對話列表面板 */
.chat-topic-panel {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 10;
    background: var(--bs-body-bg, #fff);
    display: flex;
    flex-direction: column;
}

.chat-topic-panel-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 14px;
    font-size: 0.9rem;
    font-weight: 600;
    border-bottom: 1px solid var(--bs-border-color, #dee2e6);
}

.chat-topic-panel-header button {
    color: var(--bs-body-color, #333);
    text-decoration: none;
}

.chat-topic-list {
    flex: 1;
    overflow-y: auto;
    padding: 4px 0;
}

.chat-topic-item {
    padding: 10px 14px;
    cursor: pointer;
    border-bottom: 1px solid var(--bs-border-color-translucent, #f0f0f0);
    transition: background 0.15s;
}

.chat-topic-item:hover {
    background: var(--bs-tertiary-bg, #f8f9fa);
}

[data-theme="dark"] .chat-topic-item:hover {
    background: rgba(255,255,255,0.05);
}

.chat-topic-item.active {
    background: var(--bs-primary-bg-subtle, #e0e7ff);
}

[data-theme="dark"] .chat-topic-item.active {
    background: rgba(99, 102, 241, 0.15);
}

.chat-topic-title {
    font-size: 0.85rem;
    display: inline;
    word-break: break-word;
}

.chat-topic-delete {
    opacity: 0;
    transition: opacity 0.15s;
}

.chat-topic-item:hover .chat-topic-delete {
    opacity: 1;
}

/* 代理欄工具執行動畫 */
.agent-bar-tools {
    font-size: 0.75rem;
}

.agent-tool-active {
    color: var(--bs-warning-text-emphasis, #cc9a06);
}

.spin-animation {
    display: inline-block;
    animation: spin 1s linear infinite;
}

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

.tool-name-pulse {
    animation: toolPulse 1.5s ease-in-out infinite;
}

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

.status-pulse {
    animation: toolPulse 2s ease-in-out infinite;
    color: var(--bs-info-text-emphasis, #087990);
}

.agent-bar-status {
    font-size: 0.72rem;
    opacity: 0.85;
}