/* ============================================================
   AI ASSISTANT CHAT WIDGET — ai-chat.css
   宁波莫斯化工有限公司 | MOS-AGRO.COM
   ============================================================ */

/* ---- 整体容器（固定在右下角） ---- */
.ai-chat-widget {
    position: fixed;
    bottom: 28px;
    right: 28px;
    z-index: 9990;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 12px;
    font-family: 'Noto Sans SC', 'Roboto', sans-serif;
    /* 关闭状态不阻挡点击 */
    pointer-events: none;
}
.ai-chat-widget.is-open {
    pointer-events: auto;
}

/* ---- 悬浮启动按钮 ---- */
.ai-chat-launcher {
    position: relative;
    width: 58px;
    height: 58px;
    border-radius: 50%;
    background: linear-gradient(135deg, #2d7a35 0%, #1a4a1f 100%);
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    box-shadow: 0 6px 24px rgba(26, 74, 31, 0.45), 0 2px 8px rgba(0,0,0,0.18);
    transition: transform 0.3s cubic-bezier(.34,1.56,.64,1), box-shadow 0.3s ease, background 0.3s ease;
    outline: none;
    flex-shrink: 0;
    order: 2; /* 始终在底部 */
    pointer-events: auto; /* 确保启动按钮始终可点击 */
}
.ai-chat-launcher:hover {
    transform: scale(1.10) translateY(-2px);
    box-shadow: 0 10px 32px rgba(26, 74, 31, 0.55), 0 4px 12px rgba(0,0,0,0.22);
    background: linear-gradient(135deg, #35913e 0%, #1f5a26 100%);
}
.ai-chat-launcher:focus-visible {
    outline: 3px solid rgba(45,122,53,0.5);
    outline-offset: 3px;
}

/* 启动按钮图标切换 */
.ai-launcher-icon {
    position: absolute;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: opacity 0.25s ease, transform 0.25s ease;
}
.ai-launcher-icon--open  { opacity: 1; transform: scale(1) rotate(0deg); }
.ai-launcher-icon--close { opacity: 0; transform: scale(0.5) rotate(-90deg); font-size: 1.3rem; }

/* 聊天框打开时切换图标 */
.ai-chat-widget.is-open .ai-launcher-icon--open  { opacity: 0; transform: scale(0.5) rotate(90deg); }
.ai-chat-widget.is-open .ai-launcher-icon--close { opacity: 1; transform: scale(1) rotate(0deg); }

/* 未读消息徽章 */
.ai-launcher-badge {
    position: absolute;
    top: -2px;
    right: -2px;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: #f5a623;
    color: #fff;
    font-size: 0.68rem;
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 2px solid #fff;
    transition: opacity 0.3s ease, transform 0.3s ease;
    animation: ai-badge-pulse 2s ease-in-out infinite;
}
.ai-launcher-badge.hidden {
    opacity: 0;
    transform: scale(0);
    pointer-events: none;
}
@keyframes ai-badge-pulse {
    0%, 100% { transform: scale(1); }
    50%       { transform: scale(1.15); }
}

/* ---- 聊天面板 ---- */
.ai-chat-panel {
    order: 1; /* 在按钮上方 */
    width: 360px;
    max-height: 520px;
    background: #fff;
    border-radius: 20px;
    box-shadow: 0 16px 56px rgba(0,0,0,0.18), 0 4px 16px rgba(0,0,0,0.10);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    /* 默认隐藏 */
    opacity: 0;
    transform: translateY(20px) scale(0.96);
    pointer-events: none;
    transition: opacity 0.32s cubic-bezier(.4,0,.2,1),
                transform 0.32s cubic-bezier(.34,1.56,.64,1);
    transform-origin: bottom right;
}
/* 打开状态 */
.ai-chat-widget.is-open .ai-chat-panel {
    opacity: 1;
    transform: translateY(0) scale(1);
    pointer-events: auto;
}

/* ---- 面板头部 ---- */
.ai-chat-header {
    background: linear-gradient(135deg, #2d7a35 0%, #1a4a1f 100%);
    padding: 16px 18px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
}
.ai-chat-header-info {
    display: flex;
    align-items: center;
    gap: 12px;
}
.ai-chat-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: rgba(255,255,255,0.15);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    border: 2px solid rgba(255,255,255,0.25);
}
.ai-chat-header-text {
    display: flex;
    flex-direction: column;
    gap: 2px;
}
.ai-chat-name {
    font-size: 0.95rem;
    font-weight: 700;
    color: #fff;
    line-height: 1.2;
    font-family: 'Montserrat', sans-serif;
}
.ai-chat-status {
    font-size: 0.75rem;
    color: rgba(255,255,255,0.8);
    display: flex;
    align-items: center;
    gap: 5px;
}
.ai-status-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: #7ddb82;
    flex-shrink: 0;
    animation: ai-status-blink 2.5s ease-in-out infinite;
}
@keyframes ai-status-blink {
    0%, 100% { opacity: 1; }
    50%       { opacity: 0.4; }
}
.ai-chat-minimize {
    background: rgba(255,255,255,0.12);
    border: none;
    color: rgba(255,255,255,0.85);
    width: 30px;
    height: 30px;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.8rem;
    transition: background 0.2s ease, color 0.2s ease;
    flex-shrink: 0;
}
.ai-chat-minimize:hover {
    background: rgba(255,255,255,0.25);
    color: #fff;
}

/* ---- 消息区域 ---- */
.ai-chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px 16px 8px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    scroll-behavior: smooth;
    min-height: 180px;
}
.ai-chat-messages::-webkit-scrollbar { width: 4px; }
.ai-chat-messages::-webkit-scrollbar-track { background: transparent; }
.ai-chat-messages::-webkit-scrollbar-thumb { background: #d0e8d2; border-radius: 4px; }

/* 消息气泡 */
.ai-msg {
    display: flex;
    gap: 8px;
    animation: ai-msg-in 0.3s ease both;
}
@keyframes ai-msg-in {
    from { opacity: 0; transform: translateY(10px); }
    to   { opacity: 1; transform: translateY(0); }
}
.ai-msg--bot { align-items: flex-start; }
/* 文本列占满除头像外的宽度，避免 max-width:% 相对「收缩后的父级」算出过窄的气泡 */
.ai-msg--bot > div:last-child {
    flex: 1;
    min-width: 0;
}
.ai-msg--user { flex-direction: row-reverse; align-items: flex-start; }
.ai-msg--user > div {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
}

.ai-msg-avatar {
    width: 30px;
    height: 30px;
    border-radius: 50%;
    background: linear-gradient(135deg, #2d7a35, #1a4a1f);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    font-size: 0.75rem;
    color: #fff;
    margin-top: 2px;
}
.ai-msg-avatar svg { width: 18px; height: 18px; }

.ai-msg-bubble {
    max-width: 78%;
    padding: 10px 14px;
    border-radius: 16px;
    font-size: 0.875rem;
    line-height: 1.55;
    word-break: break-word;
}
.ai-msg--bot .ai-msg-bubble {
    background: #f0f7f1;
    color: #1a1a2e;
    border-bottom-left-radius: 4px;
    border: 1px solid #dde8da;
}
.ai-msg--user .ai-msg-bubble {
    background: linear-gradient(135deg, #2d7a35, #1a4a1f);
    color: #fff;
    border-bottom-right-radius: 4px;
}

/* Markdown 标题样式 */
.ai-msg-bubble h2,
.ai-msg-bubble h3,
.ai-msg-bubble h4 {
    margin: 8px 0 4px;
    font-weight: 700;
    color: #1a4a1f;
}
.ai-msg-bubble h2 { font-size: 1rem; }
.ai-msg-bubble h3 { font-size: 0.95rem; }
.ai-msg-bubble h4 { font-size: 0.9rem; }
.ai-msg-bubble h2:first-child,
.ai-msg-bubble h3:first-child,
.ai-msg-bubble h4:first-child {
    margin-top: 0;
}
.ai-msg-bubble ul {
    margin: 6px 0;
    padding-left: 18px;
    list-style: disc;
}
.ai-msg-bubble li {
    margin: 3px 0;
}

/* 打字动画 */
.ai-typing-indicator {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 12px 14px;
}
.ai-typing-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: #2d7a35;
    animation: ai-typing 1.2s ease-in-out infinite;
}
.ai-typing-dot:nth-child(2) { animation-delay: 0.2s; }
.ai-typing-dot:nth-child(3) { animation-delay: 0.4s; }
@keyframes ai-typing {
    0%, 80%, 100% { transform: scale(0.7); opacity: 0.4; }
    40%           { transform: scale(1.1); opacity: 1; }
}

/* 时间戳 */
.ai-msg-time {
    font-size: 0.68rem;
    color: #aaa;
    margin-top: 3px;
    padding: 0 4px;
    align-self: flex-end;
}
.ai-msg--user .ai-msg-time { text-align: right; }

/* ---- 快捷问题按钮 ---- */
.ai-chat-quick {
    padding: 0 14px 10px;
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    flex-shrink: 0;
    transition: opacity 0.3s ease, max-height 0.3s ease;
    max-height: 80px;
    overflow: hidden;
}
.ai-chat-quick.hidden {
    opacity: 0;
    max-height: 0;
    padding-bottom: 0;
    pointer-events: none;
}
.ai-quick-btn {
    background: #e8f5e9;
    border: 1px solid #c8e6c9;
    color: #2d7a35;
    padding: 5px 12px;
    border-radius: 50px;
    font-size: 0.78rem;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.2s ease, color 0.2s ease, border-color 0.2s ease, transform 0.2s ease;
    white-space: nowrap;
    font-family: 'Noto Sans SC', sans-serif;
}
.ai-quick-btn:hover {
    background: #2d7a35;
    color: #fff;
    border-color: #2d7a35;
    transform: translateY(-1px);
}

/* ---- 输入区域 ---- */
.ai-chat-input-wrap {
    padding: 10px 14px 10px;
    border-top: 1px solid #eef2ee;
    display: flex;
    align-items: flex-end;
    gap: 8px;
    flex-shrink: 0;
    background: #fff;
}
.ai-chat-input {
    flex: 1;
    border: 1.5px solid #dde8da;
    border-radius: 12px;
    padding: 9px 13px;
    font-size: 0.875rem;
    font-family: 'Noto Sans SC', sans-serif;
    color: #1a1a2e;
    resize: none;
    outline: none;
    line-height: 1.5;
    max-height: 100px;
    overflow-y: auto;
    transition: border-color 0.2s ease, box-shadow 0.2s ease;
    background: #fafcfa;
}
.ai-chat-input:focus {
    border-color: #2d7a35;
    box-shadow: 0 0 0 3px rgba(45,122,53,0.12);
    background: #fff;
}
.ai-chat-input::placeholder { color: #aab8aa; }
.ai-chat-send {
    width: 38px;
    height: 38px;
    border-radius: 50%;
    background: linear-gradient(135deg, #2d7a35, #1a4a1f);
    border: none;
    color: #fff;
    font-size: 0.85rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: transform 0.2s ease, opacity 0.2s ease, box-shadow 0.2s ease;
    box-shadow: 0 3px 10px rgba(26,74,31,0.35);
}
.ai-chat-send:disabled {
    opacity: 0.4;
    cursor: not-allowed;
    transform: none;
    box-shadow: none;
}
.ai-chat-send:not(:disabled):hover {
    transform: scale(1.1) translateY(-1px);
    box-shadow: 0 5px 14px rgba(26,74,31,0.45);
}

/* 底部备注 */
.ai-chat-footer-note {
    text-align: center;
    font-size: 0.68rem;
    color: #bbb;
    padding: 0 14px 10px;
    flex-shrink: 0;
}

/* ---- 错误消息样式 ---- */
.ai-msg--error .ai-msg-bubble {
    background: #fff3f3;
    border-color: #ffd0d0;
    color: #c0392b;
}

/* ---- 响应式：移动端 ---- */
@media (max-width: 480px) {
    .ai-chat-widget {
        bottom: 16px;
        right: 16px;
    }
    .ai-chat-panel {
        width: calc(100vw - 32px);
        max-height: 70vh;
        border-radius: 16px;
    }
    .ai-chat-launcher {
        width: 52px;
        height: 52px;
    }
}

/* 与 back-to-top 按钮错开 */
.back-to-top {
    bottom: 28px;
    right: 100px; /* 向左偏移，避免与聊天按钮重叠 */
}
@media (max-width: 480px) {
    .back-to-top { right: 84px; bottom: 16px; }
}
