﻿/* 全局悬浮机器人对话壳（仅 UI，不接业务）
 *
 * 挂载：#appShell 内部末尾。未登录时 #appShell 为 display:none，整块不渲染；
 *       showLogin() 移除 .visible 后自动一起消失，不需要额外写显隐 JS。
 * 层级：900（图标）/ 901（面板）—— 压住页面内容（最高 80），被弹框（1000 / 1100 / 2000）压住。
 * 位置：右下角 bottom:96px，给 .crm-toast（right:18 / bottom:18 / z-index:3000）让位。
 * 命名：统一 ais- 前缀。project-new.html 里已有 .ai-panel，避免撞名。
 */

/* ---------- 悬浮图标 ---------- */

#appShell .ais-fab {
  position: fixed;
  right: 24px;
  bottom: 96px;
  width: 56px;
  height: 56px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 900;
  box-shadow: 0 8px 22px rgba(8, 145, 178, 0.34);
  transition: transform 0.16s ease, box-shadow 0.16s ease, background-color 0.16s ease;
}
#appShell .ais-fab:hover {
  background: var(--primary-hover);
  transform: translateY(-2px);
  box-shadow: 0 12px 26px rgba(8, 145, 178, 0.4);
}
#appShell .ais-fab:active { transform: translateY(0); }
#appShell .ais-fab:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 3px;
}
#appShell .ais-fab svg { width: 26px; height: 26px; display: block; }
#appShell .ais-fab .ais-ico-close { display: none; }
#appShell .ais-fab[aria-expanded="true"] .ais-ico-bot { display: none; }
#appShell .ais-fab[aria-expanded="true"] .ais-ico-close { display: block; }

/* ---------- 对话面板 ---------- */

#appShell .ais-panel {
  position: fixed;
  right: 24px;
  bottom: 164px;
  width: 480px;
  max-width: calc(100vw - 48px);
  height: 520px;
  max-height: calc(100vh - 200px);
  display: flex;
  flex-direction: column;
  background: var(--surface, #fff);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: 0 24px 56px rgba(15, 23, 42, 0.2);
  z-index: 901;
  overflow: hidden;
}
#appShell .ais-panel[hidden] { display: none; }

#appShell .ais-head {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 48px;
  padding: 0 8px 0 14px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
#appShell .ais-title {
  flex: 1;
  min-width: 0;
  font-size: var(--fs-cell, 13px);
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
#appShell .ais-close {
  width: 28px;
  height: 28px;
  flex: 0 0 28px;
  padding: 0;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
#appShell .ais-close:hover { background: rgba(148, 163, 184, 0.16); color: var(--text); }
#appShell .ais-close svg { width: 15px; height: 15px; display: block; }

#appShell .ais-msgs {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  scrollbar-width: thin;
}
#appShell .ais-msg {
  max-width: 84%;
  padding: 9px 12px;
  border-radius: 12px;
  font-size: var(--fs-cell, 13px);
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
}
#appShell .ais-msg-user {
  align-self: flex-end;
  background: var(--primary);
  color: #fff;
  border-bottom-right-radius: 4px;
}
#appShell .ais-msg-bot {
  align-self: flex-start;
  background: #f1f5f9;
  color: var(--text);
  border-bottom-left-radius: 4px;
}

/* ---------- 主 Agent 排好的正文 ----------
 * 空行分段、**加粗**、多行多列排成表格；单元格和加粗里的字只当文字。
 * .ais-msg 的 pre-wrap 在这里改回 normal，段落之间才不会多出空白行。
 */
#appShell .ais-answer {
  white-space: normal;
  max-width: 100%;
}
#appShell .ais-answer-p { margin: 0 0 8px; }
#appShell .ais-answer-p:last-child { margin-bottom: 0; }
#appShell .ais-answer strong { font-weight: 700; }
#appShell .ais-answer-table {
  width: 100%;
  margin: 0 0 8px;
  border-collapse: collapse;
  font-size: var(--fs-label, 12px);
  line-height: 1.5;
}
#appShell .ais-answer-table th,
#appShell .ais-answer-table td {
  border: 1px solid var(--border);
  padding: 4px 8px;
  text-align: left;
  vertical-align: top;
  word-break: break-word;
}
#appShell .ais-answer-table th {
  background: rgba(148, 163, 184, 0.14);
  font-weight: 600;
}
#appShell .ais-answer-table:last-child { margin-bottom: 0; }
html[data-theme="dark"] #appShell .ais-answer-table th { background: rgba(148, 163, 184, 0.2); }

/* ---------- 输入区 ---------- */

#appShell .ais-foot {
  flex-shrink: 0;
  padding: 10px 12px 12px;
  border-top: 1px solid var(--border);
}
#appShell .ais-hint {
  display: none;
  margin: 0 0 8px;
  font-size: var(--fs-label, 12px);
  line-height: 1.5;
  color: var(--danger);
}
#appShell .ais-hint.show { display: block; }

/* ---------- 录音条（只在录音时出现：红点 + 音波 + 秒数 + 取消） ----------
 * 高 30，和输入框留 8 的间距；音波柱高由 JS 按实时音量写内联 height。
 * .ais-rec 设了 display:flex，必须补 [hidden]，否则 el.hidden 不生效。
 */
#appShell .ais-rec {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 30px;
  margin: 0 0 8px;
  padding: 0 8px;
  border: 1px solid rgba(185, 28, 28, 0.2);
  border-radius: 8px;
  background: rgba(185, 28, 28, 0.05);
}
#appShell .ais-rec[hidden] { display: none; }
#appShell .ais-rec-dot {
  flex: 0 0 7px;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--danger);
  animation: ais-blink 1s ease-in-out infinite;
}
@keyframes ais-blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.2; }
}
#appShell .ais-rec-wave {
  flex: 1;
  min-width: 0;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  overflow: hidden;
}
#appShell .ais-rec-bar {
  display: block;
  flex: 0 0 3px;
  width: 3px;
  height: 3px;
  border-radius: 2px;
  background: var(--danger);
  opacity: 0.7;
}
#appShell .ais-rec-time {
  flex: 0 0 auto;
  font-size: var(--fs-label, 12px);
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  color: var(--danger);
}
#appShell .ais-rec-done,
#appShell .ais-rec-cancel {
  flex: 0 0 auto;
  height: 22px;
  padding: 0 6px;
  border: 0;
  border-radius: 5px;
  background: transparent;
  color: var(--text-secondary);
  font-family: inherit;
  font-size: var(--fs-label, 12px);
  cursor: pointer;
}
#appShell .ais-rec-done:hover {
  color: var(--primary);
  background: var(--primary-light, rgba(8, 145, 178, 0.14));
}
#appShell .ais-rec-cancel:hover {
  color: var(--danger);
  background: rgba(185, 28, 28, 0.1);
}
html[data-theme="dark"] #appShell .ais-rec {
  border-color: rgba(248, 113, 113, 0.32);
  background: rgba(185, 28, 28, 0.18);
}
html[data-theme="dark"] #appShell .ais-rec-bar,
html[data-theme="dark"] #appShell .ais-rec-dot {
  background: color-mix(in srgb, var(--danger) 55%, #fff);
}
html[data-theme="dark"] #appShell .ais-rec-time {
  color: color-mix(in srgb, var(--danger) 45%, #fff);
}

#appShell .ais-box {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}
#appShell .ais-input {
  flex: 1;
  min-width: 0;
  min-height: 38px;
  max-height: 120px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface, #fff);
  color: var(--text);
  font-family: inherit;
  font-size: var(--fs-cell, 13px);
  line-height: 1.5;
  resize: none;
  outline: none;
  box-sizing: border-box;
  overflow-y: auto;
}
#appShell .ais-input::placeholder { color: var(--text-secondary); }
#appShell .ais-input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-light, rgba(8, 145, 178, 0.14));
}

#appShell .ais-mic,
#appShell .ais-send {
  width: 36px;
  height: 36px;
  flex: 0 0 36px;
  padding: 0;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 1px;
}
#appShell .ais-mic {
  border: 1px solid var(--border);
  background: var(--surface, #fff);
  color: var(--text-secondary);
}
#appShell .ais-mic:hover:not(:disabled) {
  color: var(--primary);
  border-color: var(--primary);
}
#appShell .ais-mic:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}
#appShell .ais-mic svg,
#appShell .ais-send svg { width: 17px; height: 17px; display: block; }
#appShell .ais-mic.listening {
  background: var(--danger);
  border-color: var(--danger);
  color: #fff;
  animation: ais-pulse 1.2s ease-out infinite;
}
@keyframes ais-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(185, 28, 28, 0.35); }
  50% { box-shadow: 0 0 0 6px rgba(185, 28, 28, 0); }
}
#appShell .ais-send {
  border: 1px solid var(--primary);
  background: var(--primary);
  color: #fff;
}
#appShell .ais-send:hover { background: var(--primary-hover); border-color: var(--primary-hover); }
#appShell .ais-send:focus-visible,
#appShell .ais-mic:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

/* ---------- 业务卡片（C02 一句话记跟进，由 ai-assistant-flow.js 渲染） ----------
 * 按钮高 28 = 令牌契约里的 btn-sm 口径（第 10 章已知缺口：小按钮写死 28，不跟随 --btn-h）。
 * 颜色全部取令牌，不新造色值。
 */

#appShell .ais-card {
  max-width: 100%;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
#appShell .ais-card-text {
  margin: 0;
  font-size: var(--fs-cell, 13px);
  line-height: 1.6;
  color: var(--text);
  word-break: break-word;
}
#appShell .ais-card-note {
  margin: 0;
  font-size: var(--fs-label, 12px);
  line-height: 1.5;
  color: var(--text-secondary);
}
#appShell .ais-card-note.is-error { color: var(--danger); }
#appShell .ais-card.is-muted { opacity: 0.72; }
#appShell .ais-card.is-done {
  border-color: var(--primary);
  background: var(--primary-light);
}

#appShell .ais-actions {
  display: flex;
  gap: 8px;
}
#appShell .ais-btn {
  height: 30px;
  min-width: 70px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--surface, #fff);
  color: var(--text);
  font-family: inherit;
  font-size: var(--fs-label, 12px);
  line-height: 1;
  cursor: pointer;
  white-space: nowrap;
}
#appShell .ais-btn:hover:not(:disabled) {
  border-color: var(--primary);
  color: var(--primary);
}
#appShell .ais-btn:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}
#appShell .ais-btn-primary {
  border-color: var(--primary);
  background: var(--primary);
  color: #fff;
}
#appShell .ais-btn-primary:hover:not(:disabled) {
  border-color: var(--primary-hover);
  background: var(--primary-hover);
  color: #fff;
}
#appShell .ais-btn:disabled { opacity: 0.55; cursor: not-allowed; }

#appShell .ais-picks {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
#appShell .ais-pick {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface, #fff);
  font-family: inherit;
  text-align: left;
  cursor: pointer;
}
#appShell .ais-pick:hover:not(:disabled) {
  border-color: var(--primary);
  background: var(--primary-light);
}
#appShell .ais-pick:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 1px;
}
#appShell .ais-pick:disabled { opacity: 0.55; cursor: not-allowed; }
#appShell .ais-pick-name {
  font-size: var(--fs-cell, 13px);
  font-weight: 600;
  color: var(--text);
}
#appShell .ais-pick-sub {
  font-size: var(--fs-label, 12px);
  color: var(--text-secondary);
}

/* ---------- 聊天记录 ----------
 * 面板一打开就是一条消息时间线，沿用气泡本身的样式；这里只有空状态。
 */

#appShell .ais-hist-empty {
  margin: 24px 0;
  text-align: center;
  font-size: var(--fs-cell, 13px);
  color: var(--text-secondary);
}

/* 看历史时输入区只读 */
#appShell .ais-input:disabled {
  background: rgba(148, 163, 184, 0.08);
  color: var(--text-secondary);
  cursor: not-allowed;
}
#appShell .ais-send:disabled { opacity: 0.5; cursor: not-allowed; }

/* ---------- 深色主题 ---------- */

html[data-theme="dark"] #appShell .ais-msg-bot { background: rgba(148, 163, 184, 0.16); }
html[data-theme="dark"] #appShell .ais-panel { box-shadow: 0 24px 56px rgba(0, 0, 0, 0.5); }
html[data-theme="dark"] #appShell .ais-close:hover { background: rgba(148, 163, 184, 0.2); }
html[data-theme="dark"] #appShell .ais-btn,
html[data-theme="dark"] #appShell .ais-pick { background: rgba(148, 163, 184, 0.12); }
html[data-theme="dark"] #appShell .ais-btn-primary { background: var(--primary); }
html[data-theme="dark"] #appShell .ais-card.is-done { background: rgba(8, 145, 178, 0.16); }

/* ---------- 窄屏 ---------- */

@media (max-width: 640px) {
  #appShell .ais-fab { right: 16px; bottom: 88px; }
  #appShell .ais-panel {
    left: 12px;
    right: 12px;
    width: auto;
    bottom: 152px;
    height: min(480px, calc(100vh - 200px));
  }
}

/* 确认卡上的渠道选择（N08）：改的是入队 value 的 kind，不新增接口 */
#appShell .ais-kinds {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 2px;
}
#appShell .ais-kind {
  height: 24px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface, #fff);
  color: var(--text-secondary);
  font-family: inherit;
  font-size: var(--fs-label, 12px);
  line-height: 1;
  cursor: pointer;
  white-space: nowrap;
}
#appShell .ais-kind:hover { border-color: var(--primary); color: var(--primary); }
#appShell .ais-kind.is-on {
  border-color: var(--primary);
  background: var(--primary-light);
  color: var(--primary);
  font-weight: 600;
}
#appShell .ais-kind:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
