/* AI 板块样式：会话侧栏 + 聊天主区 + 模型管理面板
   颜色一律引用 app.css 的设计令牌；消息用 pre-wrap 保留换行 */

/* 二级页签 .ai-tabs / .ai-tab 已无对应 DOM（模型管理迁至密钥板块），
   样式统一走 ui.css 的 .tabs / .tab，这里只保留页签面板的隐藏规则。 */
.ai-pane[hidden] { display: none; }

.ai-layout {
  position: relative;
  display: flex;
  gap: 12px;
  /* 空会话时整页不出现滚动条：高度按视口剩余空间算，不再被 min-height 撑破 */
  height: calc(100vh - 240px);
  min-height: 0;
  overflow: hidden;
}

/* ── 历史会话：左侧抽拉抽屉（默认收起，点「历史对话」滑出） ── */
.ai-side {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  z-index: var(--z-rise);
  width: 260px;
  display: flex;
  flex-direction: column;
  border: 1px solid var(--line, var(--line));
  border-radius: 10px;
  background: var(--card, var(--white));
  overflow: hidden;
  transform: translateX(calc(-100% - 8px));
  transition: transform .25s ease;
  box-shadow: var(--shadow-2);
}
/* 抽屉开关：checkbox 驱动，纯 CSS 生效（JS 挂掉也能点开） */
.ai-side-open {
  position: absolute;
  left: 0;
  top: 0;
  width: 1px;
  height: 1px;
  margin: 0;
  opacity: 0;
  pointer-events: none;
}
.ai-side-open:checked ~ .ai-side { transform: translateX(0); }

/* 展开时的透明遮罩：点任意空白处 = 点遮罩 label = 原生取消勾选并收起 */
.ai-side-backdrop {
  display: none;
  position: absolute;
  inset: 0;
  z-index: calc(var(--z-rise) - 1);
  cursor: default;
}
.ai-side-open:checked ~ .ai-side-backdrop { display: block; }
.ai-side-open:checked ~ .ai-main .ai-drawer-btn {
  color: var(--brand, var(--brand));
  border-color: var(--brand, var(--brand));
}
.ai-side__acts {
  display: flex;
  gap: 8px;
  padding: 10px;
  border-bottom: 1px solid var(--line, var(--line));
}
.ai-side__acts .btn { flex: 1; }
.ai-side__close {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border: 1px solid var(--line, var(--line-2));
  border-radius: 6px;
  background: var(--white);
  color: var(--muted);
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
  user-select: none;
}
.ai-side__close:hover { color: var(--ink-2); background: var(--bg-soft); }
.ai-side__foot { padding: 10px; border-top: 1px solid var(--line, var(--line)); }
.ai-side__foot .btn { width: 100%; }

.ai-sessions {
  list-style: none;
  margin: 0;
  padding: 6px;
  flex: 1;
  overflow-y: auto;
}
.ai-sessions__empty {
  padding: 16px 8px;
  color: var(--muted-2);
  text-align: center;
  font-size: 13px;
}
.ai-session {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 10px;
  border-radius: 8px;
  cursor: pointer;
  color: var(--ink-2);
}
.ai-session:hover { background: var(--bg-soft); }
.ai-session.is-on { background: var(--brand-soft); color: var(--brand); }
.ai-session__body {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.ai-session__title {
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.ai-session__meta {
  font-size: 11px;
  color: var(--muted-2);
  white-space: nowrap;
}
.ai-session__del {
  border: 0;
  background: transparent;
  color: var(--muted-2);
  font-size: 15px;
  cursor: pointer;
  padding: 0 2px;
}
.ai-session__del:hover { color: var(--bad-mid); }

.ai-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  border: 1px solid var(--line, var(--line));
  border-radius: 10px;
  background: var(--card, var(--white));
  overflow: hidden;
}
.ai-main__bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--line, var(--line));
}
.ai-drawer-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 28px;
  padding: 0 10px;
  border: 1px solid var(--line, var(--line-2));
  border-radius: 6px;
  background: var(--white);
  color: var(--ink-2);
  font-size: 13px;
  cursor: pointer;
  user-select: none;
  transition: background .15s, color .15s, border-color .15s;
}
.ai-drawer-btn:hover { background: var(--bg-soft); }
.ai-drawer-btn[aria-expanded="true"] { color: var(--brand, var(--brand)); border-color: var(--brand, var(--brand)); }
.ai-status { font-size: 12px; color: var(--muted-2); }

/* 输入区：模型选择紧贴输入框上方 */
.ai-compose {
  border-top: 1px solid var(--line, var(--line));
  background: var(--white);
}
.ai-compose__head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px 0;
}
.ai-compose__head label { font-size: 13px; color: var(--muted); }
.ai-compose__head select {
  min-width: 180px;
  padding: 5px 8px;
  border: 1px solid var(--line, var(--line-2));
  border-radius: 6px;
  font-size: 13px;
}

.ai-msgs {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: var(--bg);
}
.ai-msg {
  max-width: 78%;
  padding: 9px 12px;
  border-radius: 10px;
  font-size: 14px;
  line-height: 1.6;
}
.ai-msg__text {
  white-space: pre-wrap;
  word-break: break-word;
}
.ai-msg__time {
  margin-top: 4px;
  font-size: 11px;
  line-height: 1.4;
  color: var(--muted-2);
  text-align: right;
}
.ai-msg--user {
  align-self: flex-end;
  background: var(--brand);
  color: var(--white);
  border-bottom-right-radius: 2px;
}
.ai-msg--user .ai-msg__time { color: var(--on-brand-muted); }
/* 带图片的用户消息：不要整块蓝底，改白底 + 线框，图片自己再带一圈线框 */
.ai-msg--user.ai-msg--pic {
  background: var(--white);
  color: var(--ink);
  border: 1px solid var(--line);
}
.ai-msg--user.ai-msg--pic .ai-msg__time { color: var(--muted-2); }
.ai-msg--assistant {
  align-self: flex-start;
  background: var(--white);
  border: 1px solid var(--line);
  border-bottom-left-radius: 2px;
}
/* 识别失败的对话同样留在记录里，用浅红气泡区分 */
.ai-msg--error {
  background: var(--bad-soft-2);
  border: 1px solid var(--bad-line-2);
  color: var(--bad-deep);
}
.ai-msg--error .ai-msg__time { color: var(--bad-ink); }
.ai-msg--typing {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 14px 16px;
}
.ai-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--muted-2);
  animation: ai-blink 1.2s infinite ease-in-out;
}
.ai-dot:nth-child(2) { animation-delay: 0.2s; }
.ai-dot:nth-child(3) { animation-delay: 0.4s; }
@keyframes ai-blink {
  0%, 80%, 100% { opacity: 0.2; transform: translateY(0); }
  40% { opacity: 1; transform: translateY(-3px); }
}

/* 待发送图片预览 */
.ai-attach {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px 0;
}
.ai-attach[hidden] { display: none; }
.ai-attach__thumb {
  position: relative;
  width: 88px;
  height: 88px;
  border: 1px solid var(--line, var(--line));
  border-radius: 8px;
  overflow: hidden;
  background: var(--white);
}
.ai-attach__thumb img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.ai-attach__del {
  position: absolute;
  top: 3px;
  right: 3px;
  width: 22px;
  height: 22px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: var(--scrim-strong);
  color: var(--white);
  font-size: 15px;
  line-height: 1;
  cursor: pointer;
}
.ai-attach__del:hover { background: var(--scrim-deep); }
.ai-attach__meta { font-size: 12px; color: var(--muted-2); }

/* 输入框左侧的图片按钮 */
.ai-imgbtn {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  padding: 0;
  border: 1px solid var(--line, var(--line-2));
  border-radius: 8px;
  background: var(--white);
  color: var(--muted);
  cursor: pointer;
  transition: border-color .15s ease, color .15s ease;
}
.ai-imgbtn:hover { border-color: var(--brand); color: var(--brand); }
.ai-imgbtn svg { width: 20px; height: 20px; }

/* 气泡内的图片：先图后文 */
.ai-msg__img {
  display: block;
  max-width: 100%;
  max-height: 260px;
  margin-bottom: 6px;
  border: 1px solid var(--line-2);
  border-radius: 8px;
  background: var(--white);
  cursor: zoom-in;
}
.ai-msg__img:hover { border-color: var(--muted-2); }

.ai-input {
  display: flex;
  gap: 8px;
  padding: 10px 14px;
  border-top: 1px solid var(--line, var(--line));
  align-items: flex-end;
}
.ai-input textarea {
  flex: 1;
  resize: none;
  min-height: 40px;
  max-height: 140px;
  padding: 9px 10px;
  border: 1px solid var(--line, var(--line-2));
  border-radius: 8px;
  font-size: 14px;
  font-family: inherit;
  line-height: 1.5;
}

.ai-manage {
  padding: 12px 14px;
  border: 1px solid var(--line, var(--line));
  border-radius: 10px;
  background: var(--card, var(--white));
  max-height: calc(100vh - 220px);
  overflow-y: auto;
}
.ai-manage__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}
.ai-manage__head h3 { margin: 0; font-size: 15px; }
.ai-models { list-style: none; margin: 0 0 12px; padding: 0; }
.ai-models__empty { color: var(--muted-2); font-size: 13px; padding: 8px 0; }
.ai-model {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 0;
  border-bottom: 1px dashed var(--line);
}
.ai-model.is-off .ai-model__info { color: var(--muted-2); }
.ai-model__info { font-size: 13px; }
.ai-model__acts { display: flex; gap: 6px; }
.ai-model__acts .btn { padding: 4px 10px; font-size: 12px; }

/* ── 「AI 知识库调用」默认模型（全表唯一，仅管理员可设） ── */
.ai-kbdef {
  margin-bottom: 14px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg-soft);
}
.ai-kbdef__title { font-size: 13px; font-weight: 600; color: var(--ink-2); margin-bottom: 8px; }
.ai-kbdef__row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.ai-kbdef__sel {
  flex: 1 1 220px;
  min-width: 180px;
  padding: 7px 9px;
  border: 1px solid var(--line, var(--line-2));
  border-radius: 6px;
  font-size: 13px;
  font-family: inherit;
  background: var(--white);
}
.ai-kbdef__row .btn { white-space: nowrap; padding: 6px 12px; font-size: 12px; }
.ai-kbdef__hint {
  margin-top: 8px;
  font-size: 12px;
  color: var(--muted-2);
  line-height: 1.5;
  word-break: break-all;
}
.ai-kbdef__hint.is-warn { color: var(--warn-mid); }
.ai-kbdef__hint.is-ok { color: var(--good-deeper); }
.ai-model__badge {
  display: inline-block;
  margin-left: 8px;
  padding: 1px 6px;
  border-radius: 4px;
  background: var(--brand-soft);
  color: var(--brand-2);
  font-size: 11px;
  vertical-align: middle;
}

/* 新增 / 编辑上游接入：正文统一走 .ui-form / .ui-input / .ui-select（见 ui.css），
   这里只保留「apollo- 固定前缀」这一业务组合控件 */
.ai-model-form__name {
  display: flex;
  align-items: stretch;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--card);
  overflow: hidden;
  transition: border-color .15s ease, box-shadow .15s ease;
}
.ai-model-form__name:focus-within {
  border-color: var(--brand);
  box-shadow: var(--focus-ring);
}
.ai-model-form__prefix {
  display: flex;
  align-items: center;
  padding: 0 var(--space-3);
  background: var(--bg-soft);
  border-right: 1px solid var(--line);
  font-size: 13px;
  color: var(--muted);
  white-space: nowrap;
  user-select: none;
}
.ai-model-form__name .ui-input {
  flex: 1 1 auto;
  width: auto;
  min-width: 0;
  border: 0;
  border-radius: 0;
  background: transparent;
}
.ai-model-form__name .ui-input:focus { box-shadow: none; }
/* 上游渠道表格：密钥明细（Secret Id / 密钥 / 接口地址）只在详情弹窗展示 */
.qr-module .qr-table--ai { min-width: 1220px; }
.qr-module .qr-table--ai th { white-space: nowrap; }
/* 备注列：超长省略，悬停看完整内容 */
.ai-model-remark span {
  display: inline-block;
  max-width: 160px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  vertical-align: top;
}

/* 下游请求调用总开关：按钮与状态提示，空间不足时自动折成两行 */
.ai-manage__footer {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 12px;
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px dashed var(--line);
}
.ai-manage__footer .btn { white-space: nowrap; }
.ai-manage__footer-hint {
  flex: 1 1 220px;
  font-size: 12px;
  line-height: 1.5;
  color: var(--muted-2);
  word-break: break-all;
}
.ai-manage__footer-hint.is-on { color: var(--good-deeper); }
.ai-manage__footer-hint.is-off { color: var(--warn-deep); font-weight: 600; }

/* ── 消息图片：点开后全屏查看 ── */
.ai-lightbox {
  position: fixed;
  inset: 0;
  z-index: var(--z-lightbox);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 32px;
  background: var(--scrim-photo);
  cursor: zoom-out;
}
.ai-lightbox[hidden] { display: none; }
.ai-lightbox img {
  max-width: 92vw;
  max-height: 88vh;
  border-radius: 8px;
  background: var(--white);
  box-shadow: 0 20px 60px var(--scrim-soft);
  cursor: default;
}
.ai-lightbox__close {
  position: fixed;
  top: 18px;
  right: 22px;
  width: 38px;
  height: 38px;
  border: 0;
  border-radius: 50%;
  background: rgba(255, 255, 255, .18);
  color: var(--white);
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
}
.ai-lightbox__close:hover { background: rgba(255, 255, 255, .3); }
.ai-lightbox-lock { overflow: hidden; }

@media (max-width: 760px) {
  .ai-layout { height: auto; }
  .ai-side { width: min(280px, 86vw); }
  .ai-msgs { min-height: 300px; }
  .ai-lightbox { padding: 16px; }
}
