/* ============================================================
   Chat Widget — 懸浮聊天小幫手樣式
   配色對齊系統主色系：#0B6E72（藍綠主色）、#0C2E30（深墨綠）
   - 固定定位右下角
   - 桌面 400px，手機（< 480px）全寬
   - 訪客訊息靠右（藍綠），小幫手訊息靠左（灰綠）
   - typing-indicator 跳動點點動畫
   ============================================================ */

:root {
    /* 主色系 */
    --chat-primary: #0B6E72;
    --chat-primary-dark: #0C2E30;
    --chat-accent: #7AADB0;

    /* 背景和文字 */
    --chat-bg-light: #F8FAFB;
    --chat-bg-white: #fff;
    --chat-text-primary: #0C2E30;
    --chat-text-light: #7AADB0;

    /* 訊息泡泡 */
    --chat-user-bg: #0B6E72;
    --chat-user-text: #fff;
    --chat-assistant-bg: #fff;
    --chat-assistant-border: #0B6E72;
    --chat-error-bg: #fef3c7;
    --chat-error-text: #92400e;

    /* 邊框和陰影 */
    --chat-border-light: #E8F2F3;
    --chat-border-medium: #D4E5E7;
    --chat-shadow-sm: 0 1px 4px rgba(12, 46, 48, 0.08);
    --chat-shadow-md: 0 8px 32px rgba(12, 46, 48, 0.18);
    --chat-shadow-lg: 0 12px 48px rgba(12, 46, 48, 0.25);

    /* 排版 */
    --chat-font-base: 0.95rem;
    --chat-font-small: 0.88rem;
    --chat-font-xs: 0.8rem;
    --chat-line-height: 1.6;
}

/* ── 懸浮 Toggle 按鈕（玻璃水泡 + 機器人吉祥物）───────────── */

.chat-toggle-btn {
    position: fixed;
    bottom: 24px;
    right: 24px;
    z-index: 9999;
    width: 88px;
    height: 88px;
    border-radius: 50%;
    border: 1.5px solid rgba(190, 245, 232, 0.6);
    background: radial-gradient(circle at 34% 28%, rgba(255, 255, 255, 0.55), rgba(127, 230, 208, 0.2) 40%, rgba(16, 110, 100, 0.7) 100%);
    box-shadow: 0 12px 30px rgba(6, 30, 26, 0.45), inset 0 0 22px rgba(255, 255, 255, 0.18);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    padding: 0;
    animation: chat-fab-glow 2.6s infinite;
    transition: transform 0.2s ease;
}

.chat-toggle-btn:hover {
    transform: scale(1.06);
}

.chat-toggle-btn:active {
    transform: scale(0.96);
}

/* 開啟後停止發光脈動 */
.chat-toggle-btn[aria-expanded="true"] {
    animation: none;
}

.chat-toggle-btn:focus-visible {
    outline: 3px solid var(--chat-accent);
    outline-offset: 3px;
}

/* 機器人吉祥物 */
.chat-robot {
    display: flex;
    align-items: center;
    justify-content: center;
}

.chat-robot svg {
    display: block;
}

.chat-robot-main {
    width: 62px;
    height: 62px;
    animation: chat-robot-float 3.2s ease-in-out infinite;
}

/* 旋轉葉輪天線 */
.chat-robot-ant {
    transform-box: view-box;
    transform-origin: 50px 12px;
    animation: chat-robot-spin 8s linear infinite;
}

/* 玻璃泡泡高光 */
.chat-fab-shine {
    position: absolute;
    top: 10px;
    left: 15px;
    width: 22px;
    height: 14px;
    border-radius: 50%;
    background: linear-gradient(135deg, rgba(255, 255, 255, 0.85), rgba(255, 255, 255, 0));
    transform: rotate(-20deg);
    pointer-events: none;
}

/* 上升水泡 */
.chat-fab-bubble {
    position: absolute;
    border-radius: 50%;
    pointer-events: none;
    background: radial-gradient(circle at 32% 28%, rgba(255, 255, 255, 0.9), rgba(190, 245, 232, 0.3) 60%, transparent);
    border: 1px solid rgba(190, 245, 232, 0.5);
}

.chat-fab-bubble-1 {
    bottom: 8px;
    left: 20px;
    width: 9px;
    height: 9px;
    animation: chat-fab-rise 4.2s ease-in infinite;
}

.chat-fab-bubble-2 {
    bottom: 6px;
    right: 22px;
    width: 6px;
    height: 6px;
    animation: chat-fab-rise 3.4s ease-in 0.8s infinite;
}

.chat-fab-bubble-3 {
    bottom: 10px;
    left: 42px;
    width: 5px;
    height: 5px;
    animation: chat-fab-rise 3s ease-in 1.6s infinite;
}

@keyframes chat-robot-float {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-4px); }
}

@keyframes chat-robot-spin {
    to { transform: rotate(360deg); }
}

@keyframes chat-fab-glow {
    0%, 100% {
        box-shadow: 0 12px 30px rgba(6, 30, 26, 0.45), inset 0 0 22px rgba(255, 255, 255, 0.18), 0 0 0 0 rgba(38, 175, 155, 0.5);
    }
    50% {
        box-shadow: 0 12px 30px rgba(6, 30, 26, 0.45), inset 0 0 22px rgba(255, 255, 255, 0.18), 0 0 0 12px rgba(38, 175, 155, 0);
    }
}

@keyframes chat-fab-rise {
    0% { transform: translateY(14px) scale(0.5); opacity: 0; }
    18% { opacity: 0.95; }
    78% { opacity: 0.95; }
    100% { transform: translateY(-70px) scale(1); opacity: 0; }
}

@media (prefers-reduced-motion: reduce) {
    .chat-toggle-btn,
    .chat-robot-main,
    .chat-robot-ant,
    .chat-fab-bubble {
        animation: none !important;
    }
}

/* ── 對話視窗 ─────────────────────────────────────────────── */

.chat-widget-dialog {
    position: fixed;
    bottom: 92px;
    right: 24px;
    z-index: 9998;
    width: 400px;
    height: 520px;
    border-radius: 12px;
    background-color: var(--chat-bg-white);
    box-shadow: var(--chat-shadow-lg);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    transform-origin: bottom right;
}

.chat-widget-dialog[hidden] {
    display: none;
}

/* ── 標題列 ───────────────────────────────────────────────── */

.chat-widget-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    background-color: var(--chat-primary-dark);
    color: var(--chat-user-text);
    flex-shrink: 0;
    border-bottom: 2px solid var(--chat-primary);
}

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

.chat-header-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    flex-shrink: 0;
    overflow: hidden;
    border: 1.5px solid rgba(190, 245, 232, 0.6);
    background: radial-gradient(circle at 34% 28%, rgba(255, 255, 255, 0.5), rgba(127, 230, 208, 0.2) 42%, rgba(16, 110, 100, 0.6) 100%);
    display: flex;
    align-items: center;
    justify-content: center;
}

.chat-header-avatar .chat-robot {
    width: 30px;
    height: 30px;
}

.chat-header-text {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.chat-widget-title {
    font-size: 0.9rem;
    font-weight: 600;
    letter-spacing: 0.01em;
    line-height: 1.2;
}

.chat-header-status {
    display: flex;
    align-items: center;
    gap: 4px;
    font-size: 0.72rem;
    color: #7AADB0;
    line-height: 1;
}

.chat-status-dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background-color: #4ade80;
    flex-shrink: 0;
}

.chat-close-btn {
    background: none;
    border: none;
    color: #fff;
    cursor: pointer;
    padding: 4px;
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0.7;
    transition: opacity 0.15s;
}

.chat-close-btn:hover {
    opacity: 1;
}

.chat-close-btn:focus-visible {
    outline: 3px solid var(--chat-accent);
    outline-offset: 2px;
    border-radius: 4px;
}

/* ── 訊息區 ───────────────────────────────────────────────── */

.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    scroll-behavior: smooth;
    background-color: #F5F8F9;
    background-image: linear-gradient(135deg, #F5F8F9 0%, #F1F6F8 100%);
}

/* 訊息泡泡基礎 */
.chat-message {
    max-width: 85%;
    padding: 11px 15px;
    border-radius: 12px;
    font-size: var(--chat-font-base);
    line-height: var(--chat-line-height);
    word-break: break-word;
    word-wrap: break-word;
    overflow-wrap: break-word;
}

/* 訪客訊息：靠右，藍綠色 */
.chat-message.user {
    align-self: flex-end;
    background-color: var(--chat-primary);
    color: #fff;
    border-bottom-right-radius: 3px;
}

/* 小幫手訊息：靠左，淡綠灰 */
.chat-message.assistant {
    align-self: flex-start;
    background-color: #fff;
    color: #0C2E30;
    border-bottom-left-radius: 3px;
    border-left: 3px solid var(--chat-primary);
    box-shadow: 0 1px 4px rgba(12, 46, 48, 0.08);
}

/* 允許助手訊息中的連結 */
.chat-message.assistant a {
    color: #0B6E72;
    text-decoration: underline;
}

/* 錯誤訊息：橘色背景 */
.chat-message.error {
    align-self: flex-start;
    background-color: #fef3c7;
    color: #92400e;
    border-bottom-left-radius: 3px;
}

/* ── 跳動點點 Typing Indicator ───────────────────────────── */

.typing-indicator {
    align-self: flex-start;
    display: flex;
    align-items: center;
    gap: 5px;
    padding: 10px 14px;
    background-color: #fff;
    border-radius: 12px;
    border-bottom-left-radius: 3px;
    border-left: 3px solid var(--chat-primary);
    box-shadow: 0 1px 4px rgba(12, 46, 48, 0.08);
}

.typing-indicator span {
    display: inline-block;
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background-color: #7AADB0;
    animation: typing-bounce 1.2s infinite ease-in-out;
}

.typing-indicator span:nth-child(2) {
    animation-delay: 0.2s;
}

.typing-indicator span:nth-child(3) {
    animation-delay: 0.4s;
}

/* 漸進式安撫文字（等待較久時顯示，例如伺服器端重試） */
.typing-indicator .typing-text {
    font-size: 0.8rem;
    color: #7AADB0;
    margin-left: 6px;
    white-space: nowrap;
}
.typing-indicator .typing-text:empty {
    display: none;
}

@keyframes typing-bounce {
    0%, 60%, 100% {
        transform: translateY(0);
        opacity: 0.5;
    }
    30% {
        transform: translateY(-6px);
        opacity: 1;
    }
}

/* ── 輸入區 ───────────────────────────────────────────────── */

.chat-input-area {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    padding: 10px 12px;
    border-top: 1px solid var(--chat-border-light);
    background-color: #fff;
    flex-shrink: 0;
}

.chat-input {
    flex: 1;
    resize: none;
    border: 1px solid var(--chat-border-medium);
    border-radius: 8px;
    padding: 8px 10px;
    font-size: 0.875rem;
    line-height: 1.4;
    font-family: inherit;
    color: #0C2E30;
    background-color: #F3F7F8;
    min-height: 38px;
    max-height: 96px;
    overflow-y: auto;
    transition: border-color 0.15s;
}

.chat-input:focus {
    outline: none;
    border-color: var(--chat-primary);
    background-color: var(--chat-bg-white);
    box-shadow: 0 0 0 3px rgba(11, 110, 114, 0.1);
}

.chat-input::placeholder {
    color: #7AADB0;
}

.chat-send-btn {
    width: 38px;
    height: 38px;
    flex-shrink: 0;
    border-radius: 8px;
    background-color: var(--chat-primary);
    color: #fff;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background-color 0.15s;
}

.chat-send-btn:hover:not(:disabled) {
    background-color: #0C2E30;
}

.chat-send-btn:disabled {
    background-color: #b8d4d6;
    cursor: not-allowed;
}

.chat-send-btn:focus-visible {
    outline: 3px solid var(--chat-accent);
    outline-offset: 2px;
}

/* ── 產品導覽連結按鈕 ─────────────────────────────────────── */

.chat-product-link {
    display: inline-block;
    align-self: flex-start;
    margin-top: 4px;
    padding: 7px 14px;
    background-color: var(--chat-primary);
    color: #fff;
    font-size: 0.9rem;
    font-weight: 500;
    border-radius: 6px;
    text-decoration: none;
    transition: all 0.2s ease;
}

.chat-product-link:hover {
    background-color: var(--chat-primary-dark);
    color: var(--chat-user-text);
    text-decoration: none;
    box-shadow: 0 2px 8px rgba(11, 110, 114, 0.25);
    transform: translateY(-1px);
}

/* ── 快速問題按鈕 ─────────────────────────────────────────── */

.chat-quick-questions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px;
    padding: 8px 12px;
    border-top: 1px solid var(--chat-border-light);
    flex-shrink: 0;
    background-color: #fff;
}

.chat-quick-questions.hidden {
    display: none;
}

.chat-quick-btn {
    background-color: #F3F7F8;
    border: 1px solid #D4E5E7;
    border-radius: 8px;
    padding: 8px 12px;
    font-size: 0.88rem;
    font-weight: 500;
    color: #0C2E30;
    cursor: pointer;
    text-align: left;
    line-height: 1.5;
    transition: all 0.2s ease;
}

.chat-quick-btn:hover {
    background-color: #E8F2F3;
    border-color: #0B6E72;
    color: #0B6E72;
    box-shadow: 0 2px 8px rgba(11, 110, 114, 0.15);
    transform: translateY(-1px);
}

.chat-quick-btn:focus-visible {
    outline: 3px solid var(--chat-accent);
    outline-offset: 2px;
}

/* ── RWD：平板（480px-768px） ────────────────────────────── */

@media (min-width: 480px) and (max-width: 768px) {
    .chat-widget-dialog {
        width: 90%;
        max-width: 500px;
        height: 65vh;
        bottom: 80px;
        right: 2%;
        left: auto;
    }

    .chat-toggle-btn {
        bottom: 20px;
        right: 20px;
    }
}

/* ── RWD：手機（< 480px） ────────────────────────────────── */

@media (max-width: 479px) {
    .chat-toggle-btn {
        bottom: 16px;
        right: 16px;
        width: 74px;
        height: 74px;
    }

    .chat-robot-main {
        width: 52px;
        height: 52px;
    }

    .chat-widget-dialog {
        bottom: 0;
        right: 0;
        left: 0;
        width: 100%;
        height: 70vh;
        border-radius: 16px 16px 0 0;
    }

    .chat-input {
        font-size: 16px;
    }
}
