/* 全站 AI 右侧抽屉（worker-1 · 里程碑 4 T19）
   悬浮球基础样式在 style.css（Lead ownership）；本文件负责抽屉布局与消息样式。
   原生 CSS，无第三方依赖。 */

.ai-float { position: fixed; right: 20px; bottom: 20px; z-index: 900; }
.ai-float.open .ai-float-btn { display: none; }

/* ---------- 遮罩 ---------- */
.ai-drawer-backdrop {
  position: fixed; inset: 0; z-index: 940;
  background: rgba(17, 24, 39, .32);
  border: 0; padding: 0; margin: 0;
}
.ai-drawer-backdrop[hidden] { display: none; }
.ai-drawer-backdrop:focus { outline: none; }

/* ---------- 抽屉主体 ---------- */
.ai-drawer {
  position: fixed; top: 0; right: 0; bottom: 0; z-index: 950;
  width: min(440px, 100vw); max-width: 100vw;
  display: flex; flex-direction: column;
  background: #fff; color: var(--ink, #1f2430);
  border-left: 1px solid var(--line, #e5e7eb);
  box-shadow: -24px 0 48px rgba(17, 24, 39, .18);
  animation: ai-drawer-in .18s ease-out;
}
.ai-drawer[hidden] { display: none; }
@keyframes ai-drawer-in {
  from { transform: translateX(24px); opacity: .4; }
  to { transform: translateX(0); opacity: 1; }
}

/* T180 · 桌面端抽屉锚定悬浮球（位置由 ai-panel.js positionDrawer() 写入内联样式），
   进场用 scale(.9)→1 + opacity，transform-origin 指向悬浮球方向；移动端保持全屏侧栏。
   T198：内联 left/top/width/height 覆盖基础 fixed 侧栏；max-height 兜底小屏竖向空间。 */
.ai-drawer.is-anchored {
  right: auto; bottom: auto;
  max-height: calc(100vh - 24px);
  border-left: 0;
  border: 1px solid var(--line, #e5e7eb);
  border-radius: 16px;
  box-shadow: 0 24px 64px rgba(17, 24, 39, .24);
  overflow: hidden;
  animation: ai-drawer-pop .24s cubic-bezier(.2, .8, .2, 1);
}
@keyframes ai-drawer-pop {
  from { transform: scale(.9); opacity: .5; }
  to { transform: scale(1); opacity: 1; }
}
@media (max-width: 640px) {
  .ai-drawer.is-anchored {
    border-radius: 0;
    border: 0;
    border-left: 1px solid var(--line, #e5e7eb);
    overflow: visible;
    animation: ai-drawer-in .18s ease-out;
  }
}

/* ---------- 头部 ---------- */
.ai-drawer-head {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 16px; background: #111827; color: #f9fafb; flex-wrap: wrap;
}
.ai-drawer-title { display: flex; align-items: center; gap: 8px; min-width: 0; }
.ai-drawer-title strong { font-size: 15px; }
.ai-drawer-badge {
  display: inline-block; background: rgba(255, 255, 255, .16); color: #e5e7eb;
  border-radius: 999px; padding: 2px 8px; font-size: 11px; white-space: nowrap;
}
.ai-drawer-badge[hidden] { display: none; }
.ai-drawer-actions { display: flex; align-items: center; gap: 6px; margin-left: auto; }
.ai-drawer-actions .btn.small { padding: 5px 9px; font-size: 12px; }
.ai-drawer-actions .btn.secondary { background: rgba(255, 255, 255, .14); color: #f9fafb; }
.ai-drawer-actions .btn.secondary:hover { background: rgba(255, 255, 255, .24); }
.ai-drawer-actions .btn.secondary[aria-pressed="true"] { background: #93c5fd; color: #111827; }
.ai-drawer-close {
  background: none; border: 0; color: #d1d5db; font-size: 24px; line-height: 1;
  cursor: pointer; padding: 0 4px; margin-left: 2px;
}
.ai-drawer-close:hover { color: #fff; }

/* ---------- 提示条 ---------- */
.ai-drawer-alert {
  margin: 10px 16px 0; padding: 9px 12px; border-radius: 8px;
  background: #fef3c7; color: #92400e; font-size: 13px;
  display: flex; align-items: center; gap: 10px; justify-content: space-between;
  flex-wrap: wrap;
}
.ai-drawer-alert[hidden] { display: none; }
.ai-drawer-alert a { color: #92400e; font-weight: 600; text-decoration: underline; }
.ai-drawer-alert .ai-drawer-alert-text { flex: 1; min-width: 160px; line-height: 1.5; }
.ai-drawer-alert button {
  background: #f59e0b; color: #fff; border: 0; border-radius: 6px;
  padding: 4px 9px; font-size: 12px; cursor: pointer;
}

/* ---------- 主体 ---------- */
.ai-drawer-body { flex: 1; min-height: 0; display: flex; flex-direction: column; }

.ai-drawer-chat { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.ai-drawer-chat[hidden] { display: none; }

.ai-drawer-messages {
  flex: 1; min-height: 0; overflow-y: auto;
  padding: 14px 16px 18px; display: flex; flex-direction: column; gap: 9px;
  background: #f9fafb;
}
.ai-drawer-empty {
  margin: auto; text-align: center; color: var(--muted, #6b7280);
  font-size: 13px; line-height: 1.7; max-width: 280px;
}
.ai-drawer-empty strong { color: var(--ink, #1f2430); }

.ai-drawer-day {
  align-self: center; font-size: 11px; color: #6b7280;
  background: #eef2ff; border-radius: 999px; padding: 2px 10px; margin: 6px 0 2px;
}

.ai-drawer-msg { display: flex; flex-direction: column; max-width: 88%; }
.ai-drawer-msg.is-user { align-self: flex-end; align-items: flex-end; }
.ai-drawer-msg.is-bot { align-self: flex-start; align-items: flex-start; }
.ai-drawer-bubble {
  padding: 9px 12px; border-radius: 12px; font-size: 14px; line-height: 1.6;
  white-space: pre-wrap; word-break: break-word;
}
.ai-drawer-msg.is-user .ai-drawer-bubble {
  background: var(--brand, #2f5bea); color: #fff; border-bottom-right-radius: 4px;
}
.ai-drawer-msg.is-bot .ai-drawer-bubble {
  background: #eef2f7; color: var(--ink, #1f2430); border-bottom-left-radius: 4px;
}
.ai-drawer-msg.is-error .ai-drawer-bubble { background: #fee2e2; color: #991b1b; }
.ai-drawer-msg.is-pending .ai-drawer-bubble { opacity: .65; font-style: italic; }
.ai-drawer-msg-time { font-size: 10px; color: #9ca3af; margin-top: 3px; }

/* ---------- 历史 ---------- */
.ai-drawer-history { flex: 1; min-height: 0; overflow-y: auto; padding: 14px 16px 24px; }
.ai-drawer-history[hidden] { display: none; }
.ai-drawer-history-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 10px; margin-bottom: 10px;
}
.ai-drawer-group { margin-bottom: 16px; }
.ai-drawer-group-head {
  margin: 0 0 6px; font-size: 12px; font-weight: 600; color: #6b7280;
}
.ai-drawer-conv {
  display: flex; align-items: flex-start; gap: 8px; width: 100%;
  text-align: left; background: #fff; color: inherit; font: inherit;
  border: 1px solid var(--line, #e5e7eb); border-radius: 10px;
  padding: 9px 11px; margin: 0 0 6px; cursor: pointer;
}
.ai-drawer-conv:hover { border-color: #c7d2fe; background: #fbfcff; }
.ai-drawer-conv.is-active { border-color: var(--brand, #2f5bea); box-shadow: inset 0 0 0 1px var(--brand, #2f5bea); }
.ai-drawer-conv-main { flex: 1; min-width: 0; }
.ai-drawer-conv-title {
  display: block; font-size: 14px; white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis;
}
.ai-drawer-conv-meta {
  display: block; font-size: 11px; color: #6b7280; margin-top: 2px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.ai-drawer-conv-del {
  flex: 0 0 auto; background: #fff; color: var(--danger, #dc2626);
  border: 1px solid #fecaca; border-radius: 6px; font-size: 11px;
  padding: 3px 7px; cursor: pointer; white-space: nowrap;
}
.ai-drawer-conv-del:hover { background: #fef2f2; }
.ai-drawer-conv-del.is-confirm { background: var(--danger, #dc2626); color: #fff; border-color: transparent; }

/* ---------- 输入区 ---------- */
.ai-drawer-compose {
  border-top: 1px solid var(--line, #e5e7eb); background: #fff;
  padding: 10px 14px 12px; display: flex; flex-direction: column; gap: 8px;
}
.ai-drawer-compose textarea {
  width: 100%; resize: none; min-height: 44px; max-height: 160px;
  padding: 9px 11px; font: inherit; font-size: 14px; line-height: 1.5;
  color: var(--ink, #1f2430); background: #fff;
  border: 1px solid var(--line, #e5e7eb); border-radius: 8px;
}
.ai-drawer-compose textarea:focus {
  outline: 2px solid #bfdbfe; border-color: var(--brand, #2f5bea);
}
.ai-drawer-compose-row { display: flex; align-items: center; gap: 8px; }
.ai-drawer-hint { flex: 1; min-width: 0; font-size: 11px; line-height: 1.4; }
.ai-drawer-hint.is-warn { color: var(--warn, #b45309); }
.ai-drawer-compose .btn.small { padding: 7px 14px; }
.ai-drawer-compose .btn:disabled { opacity: .55; cursor: not-allowed; }

/* ---------- 移动端：全宽 ---------- */
@media (max-width: 640px) {
  .ai-float { right: 12px; bottom: 12px; }
  .ai-drawer { width: 100%; max-width: 100%; border-left: 0; }
  .ai-drawer-backdrop { background: rgba(17, 24, 39, .18); }
  .ai-drawer-head { padding: 10px 12px; }
  .ai-drawer-messages { padding: 12px; }
  .ai-drawer-msg { max-width: 92%; }
  .ai-drawer-compose { padding: 8px 12px 10px; }
  /* v0.7.5：移动端输入区内联文本 ≥16px，避免 iOS 聚焦自动放大 */
  .ai-drawer-compose textarea { font-size: 16px; }
}

/* ---------- v0.7.5：悬浮球拖动（pointer events，拖动/点击区分在 JS 内） ---------- */
.ai-float.dragging { z-index: 960; transition: none; }
.ai-float.dragging .ai-float-btn { cursor: grabbing; }
.ai-float-btn { touch-action: none; cursor: grab; user-select: none; -webkit-user-select: none; }

/* ---------- v1.0 T160：AI 参考图预览 ---------- */
.ai-ref-previews {
  display: flex; flex-wrap: wrap; gap: 8px;
  padding: 8px 12px 0;
}
.ai-ref-previews[hidden] { display: none; }
.ai-ref-item {
  position: relative; width: 52px; height: 52px; border-radius: 8px;
  overflow: hidden; border: 1px solid var(--line, #e5e7eb); background: #f8fafc;
  display: flex; align-items: center; justify-content: center;
}
.ai-ref-item img { width: 100%; height: 100%; object-fit: cover; display: block; }
.ai-ref-item.is-uploading img { opacity: .55; }
.ai-ref-item.is-error { border-color: var(--danger, #dc2626); }
.ai-ref-item.is-error img { opacity: .42; }
.ai-ref-spin {
  position: absolute; width: 18px; height: 18px; border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, .35); border-top-color: #fff;
  animation: ai-ref-spin .8s linear infinite;
}
@keyframes ai-ref-spin { to { transform: rotate(360deg); } }
.ai-ref-label {
  position: absolute; left: 0; right: 0; bottom: 0;
  font-size: 10px; line-height: 1.3; padding: 2px 3px;
  background: rgba(15, 23, 42, .62); color: #fff; text-align: center;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.ai-ref-item.is-ready .ai-ref-label { display: none; }
.ai-ref-remove {
  position: absolute; top: 0; right: 0; width: 16px; height: 16px;
  border: 0; border-radius: 0 0 0 8px; padding: 0;
  background: rgba(15, 23, 42, .66); color: #fff;
  font-size: 12px; line-height: 16px; cursor: pointer;
}
.ai-ref-remove:hover { background: rgba(220, 38, 38, .92); }
.ai-ref-status { margin: 6px 12px 0; font-size: 11px; line-height: 1.4; }
.ai-ref-status[hidden] { display: none; }
.ai-ref-status.is-warn { color: var(--warn, #b45309); }
.ai-drawer-compose-row { flex-wrap: wrap; }
.ai-drawer-compose-row .ai-drawer-hint { flex: 1 1 120px; }
#ai-drawer-ref-pick { flex: 0 0 auto; white-space: nowrap; }
@media (max-width: 640px) {
  .ai-ref-item { width: 46px; height: 46px; }
}

/* ---------- v1.0 T160：AI 回复内联图片（Markdown 站内媒体 → 加密流 + IDB 缓存） ---------- */
.ai-drawer-bubble .ai-drawer-rich-img {
  display: block; max-width: 100%; height: auto;
  margin: 6px 0; border-radius: 8px;
  border: 1px solid var(--line, #e5e7eb); background: #fff;
}
.ai-drawer-msg.is-user .ai-drawer-bubble .ai-drawer-rich-img { border-color: rgba(255, 255, 255, .5); }
