/* =====================================================================
   AI 助手悬浮球 + 侧滑面板 样式
   来源：从 base.html 第 979-1148 行拆分（2026-06-21）
   大白话：这是右下角 AI 助手悬浮球和侧滑面板的样式。
   包含：悬浮球外观/脉冲动画、侧滑面板布局、消息气泡、输入框、加载动画等。
   ===================================================================== */

/* ===== AI助手悬浮球 ===== */
.ai-fab {
    position: fixed;
    bottom: 28px;
    right: 28px;
    z-index: 1040;
    cursor: pointer;
    width: 56px;
    height: 56px;
    transition: transform 0.3s cubic-bezier(0.4,0,0.2,1);
}
.ai-fab:hover { transform: scale(1.1); }
.ai-fab:active { transform: scale(0.95); }
.ai-fab.ai-fab--hidden { transform: scale(0); opacity: 0; pointer-events: none; }
.ai-fab__core { position: relative; z-index: 2; }
.ai-fab__svg { display: block; box-shadow: var(--taste-shadow-md, 0 4px 12px -2px rgba(9,9,11,0.06)); border-radius: 50%; }
.ai-fab__ring {
    position: absolute; top: 50%; left: 50%;
    width: 56px; height: 56px;
    margin-top: -28px; margin-left: -28px;
    border-radius: 50%;
    border: 2px solid rgba(64, 150, 255,0.3);
    animation: aiFabPulse 2.5s ease-out infinite;
    z-index: 1;
}
.ai-fab__ring--outer { animation-delay: 1.2s; }
@keyframes aiFabPulse {
    0% { transform: scale(1); opacity: 0.6; }
    100% { transform: scale(1.8); opacity: 0; }
}
@media (max-width: 768px) {
    .ai-fab { bottom: 80px; right: 16px; width: 48px; height: 48px; }
    .ai-fab__svg { width: 48px; height: 48px; }
    .ai-fab__ring { width: 48px; height: 48px; margin-top: -24px; margin-left: -24px; }
}

/* ===== AI侧滑面板 ===== */
.ai-panel { position: fixed; top: 0; right: 0; bottom: 0; left: 0; z-index: 1050; pointer-events: none; font-family: var(--font-family-base); }
.ai-panel.ai-panel--open { pointer-events: auto; }
.ai-panel__overlay {
    position: absolute; top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.25); backdrop-filter: blur(2px);
    opacity: 0; transition: opacity 0.3s ease;
}
.ai-panel--open .ai-panel__overlay { opacity: 1; }
.ai-panel__body {
    position: absolute; top: 0; right: 0; bottom: 0;
    width: 420px; max-width: 100vw;
    background: #fff;
    display: flex; flex-direction: column;
    transform: translateX(100%);
    transition: transform 0.35s cubic-bezier(0.4,0,0.2,1);
    box-shadow: -4px 0 24px rgba(9,9,11,0.12);
}
.ai-panel--open .ai-panel__body { transform: translateX(0); }
@media (max-width: 480px) { .ai-panel__body { width: 100vw; } }

.ai-panel__header {
    display: flex; align-items: center; justify-content: space-between;
    padding: 12px 16px;
    border-bottom: 1px solid #e8e8e8;
    background: var(--color-text-primary);
    flex-shrink: 0;
}
.ai-panel__header-left { display: flex; align-items: center; gap: 10px; }
.ai-panel__header-icon { display: flex; }
.ai-panel__header-title { color: #e0e6ff; font-size: 0.95rem; font-weight: 600; letter-spacing: 0.5px; }
.ai-panel__header-actions { display: flex; align-items: center; gap: 6px; }
.ai-panel__channel-select {
    padding: 3px 8px; border-radius: 6px; border: 1px solid rgba(64, 150, 255,0.3);
    background: rgba(64, 150, 255,0.08); color: #b0c4ff; font-size: 0.78rem;
    cursor: pointer;
}
.ai-panel__channel-select:focus { border-color: var(--color-primary-hover); box-shadow: 0 0 0 2px rgba(64, 150, 255,0.15); }
.ai-panel__btn {
    width: 28px; height: 28px; border-radius: 6px; border: none;
    background: rgba(255,255,255,0.08); color: #b0c4ff;
    cursor: pointer; display: flex; align-items: center; justify-content: center;
    font-size: 0.8rem; transition: all 0.15s;
}
.ai-panel__btn:hover { background: rgba(255,255,255,0.15); color: #fff; }
.ai-panel__btn--close:hover { background: rgba(255,77,79,0.3); color: #ff7875; }

.ai-panel__messages {
    flex: 1; overflow-y: auto; padding: 16px;
    display: flex; flex-direction: column; gap: 12px;
    background: #f7f8fc;
}
.ai-panel__messages::-webkit-scrollbar { width: 4px; }
.ai-panel__messages::-webkit-scrollbar-thumb { background: #d0d0d0; border-radius: 2px; }

.ai-panel__welcome { text-align: center; padding: 40px 16px; color: var(--color-text-tertiary); }
.ai-panel__welcome-icon { margin-bottom: 12px; }
.ai-panel__welcome-title { font-size: 1.05rem; font-weight: 600; color: var(--color-text-primary); margin-bottom: 6px; }
.ai-panel__welcome-desc { font-size: 0.85rem; line-height: 1.6; }

.ai-panel__msg { display: flex; gap: 8px; max-width: 92%; animation: aiMsgIn 0.25s ease; }
.ai-panel__msg--user { align-self: flex-end; flex-direction: row-reverse; }
.ai-panel__msg--ai { align-self: flex-start; }
@keyframes aiMsgIn { from { opacity:0; transform:translateY(8px); } to { opacity:1; transform:translateY(0); } }

.ai-panel__msg-avatar {
    width: 30px; height: 30px; border-radius: 50%;
    display: flex; align-items: center; justify-content: center;
    flex-shrink: 0; font-size: 0.8rem;
}
.ai-panel__msg--user .ai-panel__msg-avatar { background: var(--color-primary); color: var(--color-text-inverse); }
.ai-panel__msg--ai .ai-panel__msg-avatar { background: linear-gradient(135deg, var(--color-primary-hover), var(--color-primary-active)); color: var(--color-text-inverse); }

.ai-panel__msg-bubble {
    padding: 10px 14px; border-radius: 12px;
    font-size: 0.85rem; line-height: 1.7;
    white-space: pre-wrap; word-wrap: break-word; overflow-wrap: break-word;
    max-width: 100%; min-width: 0;
}
.ai-panel__msg--user .ai-panel__msg-bubble {
    background: var(--color-primary); color: var(--color-text-inverse); border-bottom-right-radius: 4px;
}
.ai-panel__msg--ai .ai-panel__msg-bubble {
    background: var(--color-bg-primary); color: var(--color-text-primary); border: 1px solid var(--color-border); border-bottom-left-radius: 4px;
}
.ai-panel__msg-meta {
    margin-top: 6px; padding-top: 6px; border-top: 1px dashed var(--color-border);
    font-size: 0.72rem; color: var(--color-text-tertiary); display: flex; align-items: center; gap: 10px;
}
.ai-panel__msg-sources {
    margin-top: 8px; padding-top: 8px; border-top: 1px dashed var(--color-border);
}
.ai-panel__msg-sources-title { font-size: 0.75rem; color: var(--color-text-tertiary); margin-bottom: 4px; }
.ai-panel__source-tag {
    display: inline-flex; align-items: center; gap: 3px;
    padding: 2px 8px; background: var(--color-primary-light); color: var(--color-primary);
    border-radius: 12px; font-size: 0.7rem; margin-right: 4px; margin-bottom: 3px;
}

.ai-panel__msg--loading .ai-panel__msg-bubble {
    display: flex; align-items: center; gap: 6px; color: #8c8c8c;
}
.ai-panel__typing-dot {
    width: 6px; height: 6px; background: #8c8c8c; border-radius: 50%;
    animation: aiTyping 1.4s infinite;
}
.ai-panel__typing-dot:nth-child(2) { animation-delay: 0.2s; }
.ai-panel__typing-dot:nth-child(3) { animation-delay: 0.4s; }
@keyframes aiTyping { 0%,60%,100%{transform:translateY(0)} 30%{transform:translateY(-8px)} }

/* ========================================================================
   2026-06-27 P0：AI 助手 markdown 渲染样式
   大白话：原来 AI 助手用自写正则渲染，没支持表格语法，
   现在 JS 改用 marked.js + GFM，CSS 也需要补齐所有 markdown 元素样式。
   作用域：.ai-panel__msg-bubble 内的所有 markdown 元素。
   设计原则：颜色用设计令牌（不硬编码），与知识库 chat 页面 .md-content 保持一致。
   ======================================================================== */

/* 段落 */
.ai-panel__msg-bubble p {
    margin: 6px 0;
    word-break: break-word;
}

/* 标题 */
.ai-panel__msg-bubble h1,
.ai-panel__msg-bubble h2,
.ai-panel__msg-bubble h3,
.ai-panel__msg-bubble h4 {
    font-weight: 700;
    margin: 10px 0 6px;
    color: var(--color-text-primary, #1f2329);
}
.ai-panel__msg-bubble h1 { font-size: 1.1rem; }
.ai-panel__msg-bubble h2 {
    font-size: 1.05rem;
    border-bottom: 2px solid var(--color-primary, #1677ff);
    padding-bottom: 4px;
}
.ai-panel__msg-bubble h3 { font-size: 1rem; }
.ai-panel__msg-bubble h4 { font-size: 0.95rem; font-weight: 600; }

/* 链接 */
.ai-panel__msg-bubble a {
    color: var(--color-primary, #1677ff);
    text-decoration: underline;
    word-break: break-all;
}
.ai-panel__msg-bubble a:hover { opacity: 0.8; }

/* 图片 */
.ai-panel__msg-bubble img {
    max-width: 100%;
    height: auto;
    border-radius: 6px;
    margin: 6px 0;
}

/* 引用块 */
.ai-panel__msg-bubble blockquote {
    border-left: 3px solid var(--color-primary, #1677ff);
    padding: 4px 12px;
    margin: 6px 0;
    color: var(--color-text-secondary, #595959);
    background: var(--color-bg-secondary, #f5f5f5);
    border-radius: 0 4px 4px 0;
}
.ai-panel__msg-bubble blockquote p { margin: 4px 0; }

/* 分隔线 */
.ai-panel__msg-bubble hr {
    border: none;
    border-top: 1px solid var(--color-border, #e8e8e8);
    margin: 12px 0;
}

/* 删除线 */
.ai-panel__msg-bubble del {
    color: var(--color-text-tertiary, #8c8c8c);
}

/* 列表 */
.ai-panel__msg-bubble ul,
.ai-panel__msg-bubble ol {
    padding-left: 22px;
    margin: 6px 0;
}
.ai-panel__msg-bubble ul li {
    list-style: disc;
    margin-bottom: 3px;
    line-height: 1.7;
}
.ai-panel__msg-bubble ol li {
    list-style: decimal;
    margin-bottom: 3px;
    line-height: 1.7;
}

/* 加粗 / 斜体 */
.ai-panel__msg-bubble strong { font-weight: 700; color: var(--color-text-primary, #1f2329); }
.ai-panel__msg-bubble em { font-style: italic; }

/* 代码块 */
.ai-panel__msg-bubble pre {
    background: var(--color-bg-secondary, #f5f5f5);
    border: 1px solid var(--color-border, #e8e8e8);
    border-radius: 6px;
    padding: 10px 12px;
    margin: 8px 0;
    overflow-x: auto;
    font-size: 0.8rem;
    line-height: 1.5;
}
.ai-panel__msg-bubble pre code {
    font-family: Consolas, Monaco, 'Courier New', monospace;
    color: var(--color-text-primary, #1f2329);
}

/* 行内代码 */
.ai-panel__msg-bubble code {
    background: var(--color-bg-tertiary, #f0f0f0);
    padding: 1px 5px;
    border-radius: 3px;
    font-size: 0.83em;
    font-family: Consolas, Monaco, 'Courier New', monospace;
    color: var(--color-text-primary, #1f2329);
}

/* ===== 表格样式（核心修复） =====
   大白话：marked 输出原生 <table><thead><tbody><tr><th><td>，
   没有任何 class，这里用元素选择器直接生效。
   - 表格容器：display:block + overflow-x:auto 让长表格横向滚动
   - 边框合并：border-collapse 避免双线
   - 表头：背景色 + 加粗
   - 单元格：边框 + 内边距 + 顶部对齐
   - 斑马纹：偶数行浅背景，提升可读性
   - 鼠标悬停：高亮当前行，方便对照 */
.ai-panel__msg-bubble table {
    border-collapse: collapse;
    width: 100%;
    margin: 8px 0;
    font-size: 0.82rem;
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}
.ai-panel__msg-bubble thead {
    background: var(--color-bg-secondary, #f5f5f5);
}
.ai-panel__msg-bubble th {
    border: 1px solid var(--color-border, #e8e8e8);
    padding: 6px 10px;
    text-align: left;
    font-weight: 600;
    color: var(--color-text-primary, #1f2329);
    white-space: nowrap;
}
.ai-panel__msg-bubble td {
    border: 1px solid var(--color-border, #e8e8e8);
    padding: 5px 10px;
    text-align: left;
    vertical-align: top;
    color: var(--color-text-primary, #1f2329);
}
/* 斑马纹：偶数行浅背景 */
.ai-panel__msg-bubble tbody tr:nth-child(even) {
    background: var(--color-bg-secondary, #f5f5f5);
}
/* 鼠标悬停高亮 */
.ai-panel__msg-bubble tbody tr:hover {
    background: var(--color-primary-light, #e6f4ff);
}
/* 表格内嵌段落/列表不要额外 margin，避免错位 */
.ai-panel__msg-bubble td p,
.ai-panel__msg-bubble td ul,
.ai-panel__msg-bubble td ol {
    margin: 2px 0;
}

/* ===== 相关表格卡片样式（2026-06-27 P0） =====
   大白话：AI 助手浮窗的 related_tables 渲染成可折叠卡片，
   点击 header 展开/收起，箭头旋转 180°。
   与知识库 chat 页面的 .message__table-card 保持一致的设计语言。 */
.ai-panel__table-card {
    margin-bottom: 6px;
    border: 1px solid var(--color-border, #e8e8e8);
    border-radius: 6px;
    overflow: hidden;
}
.ai-panel__table-header {
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 6px 10px;
    background: var(--color-bg-secondary, #f5f5f5);
    color: var(--color-text-primary, #1f2329);
    font-size: 0.8rem;
    user-select: none;
    transition: background 0.15s ease;
}
.ai-panel__table-header:hover {
    background: var(--color-primary-light, #e6f4ff);
}
.ai-panel__table-header i {
    font-size: 0.75rem;
}
.ai-panel__table-header .bi-chevron-down {
    transition: transform 0.2s ease;
}
/* 展开时箭头旋转 180° */
.ai-panel__table-card.expanded .bi-chevron-down {
    transform: rotate(180deg);
}
.ai-panel__table-body {
    display: none;
    padding: 8px 10px;
    border-top: 1px solid var(--color-border, #e8e8e8);
    overflow-x: auto;
    background: #fff;
}
.ai-panel__table-card.expanded .ai-panel__table-body {
    display: block;
}

.ai-panel__input-area {
    padding: 10px 14px; border-top: 1px solid #e8e8e8; background: #fff; flex-shrink: 0;
}
.ai-panel__input-wrapper {
    display: flex; align-items: flex-end; gap: 8px;
    background: var(--color-bg-tertiary); border: 1px solid #dee0e3; border-radius: 10px;
    padding: 6px 10px; transition: border-color 0.2s, box-shadow 0.2s;
}
.ai-panel__input-wrapper:focus-within {
    border-color: var(--color-primary-hover); box-shadow: 0 0 0 3px rgba(64, 150, 255,0.1);
}
.ai-panel__input {
    flex: 1; border: none; background: transparent; resize: none;
    font-size: 0.85rem; line-height: 1.5; max-height: 80px; min-height: 22px;
    font-family: inherit; color: var(--color-text-primary);
}
.ai-panel__input::placeholder { color: #b0b0b0; }
.ai-panel__send-btn {
    width: 32px; height: 32px; border-radius: 50%;
    background: linear-gradient(135deg,var(--color-primary-hover),var(--color-primary-active)); color: white;
    border: none; cursor: pointer; display: flex; align-items: center; justify-content: center;
    font-size: 0.8rem; flex-shrink: 0; transition: opacity 0.2s;
}
.ai-panel__send-btn:disabled { opacity: 0.4; cursor: not-allowed; }

/* =====================================================================
   无障碍：尊重 prefers-reduced-motion（2026-06-21 新增）
   大白话：当用户在系统设置里开启了"减少动画"，就关闭所有装饰性动画，
   只保留必要的状态变化（如显示/隐藏），避免引起前庭功能不适。
   ===================================================================== */
@media (prefers-reduced-motion: reduce) {
    /* 1. 关闭悬浮球脉冲动画（装饰性，无功能意义） */
    .ai-fab__ring {
        animation: none;
    }

    /* 2. 关闭消息入场动画（直接显示，不滑入） */
    .ai-panel__msg {
        animation: none;
    }

    /* 3. 关闭打字 dots 动画（直接静态显示） */
    .ai-panel__typing-dot {
        animation: none;
    }

    /* 4. 简化过渡动画（保留状态变化，但去掉位移和缩放） */
    .ai-fab {
        transition: opacity 0.1s ease;
    }
    .ai-fab:hover,
    .ai-fab:active {
        transform: none;
    }
    .ai-panel__overlay {
        transition: opacity 0.1s ease;
    }
    .ai-panel__body {
        transition: opacity 0.1s ease;
    }
    .ai-panel--open .ai-panel__body {
        transform: none;
    }
}
