/* ============================================================
   AI 聊天面板样式 — 暖纸设计系统
   Apple 风格 spring 动画
   ============================================================ */

/* --- 浮动触发按钮 --- */
.chat-fab {
  position: fixed;
  bottom: 28px;
  right: 28px;
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background: var(--clay);
  color: var(--clay-ink);
  border: none;
  cursor: pointer;
  z-index: 140;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow-lg);
  transition: transform .35s var(--spring), opacity .2s var(--ease);
  -webkit-tap-highlight-color: transparent;
}
.chat-fab:hover {
  transform: scale(1.08);
}
.chat-fab:active {
  transform: scale(.94);
}
/* 面板打开时隐藏按钮 */
.chat-fab.is-hidden {
  transform: scale(0);
  opacity: 0;
  pointer-events: none;
}
.chat-fab svg {
  width: 22px;
  height: 22px;
}

/* --- 聊天面板 --- */
.chat-panel {
  position: fixed;
  bottom: 92px;
  right: 28px;
  width: 380px;
  height: 520px;
  max-height: calc(100vh - 120px);
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-lg);
  z-index: 150;
  display: flex;
  flex-direction: column;
  overflow: hidden;

  /* 初始隐藏 + Apple spring 入场预备 */
  opacity: 0;
  transform: scale(.92);
  pointer-events: none;
  transition: opacity .35s var(--ease), transform .42s var(--spring);
  transform-origin: bottom right;
}
/* 打开状态 */
.chat-panel.is-open {
  opacity: 1;
  transform: scale(1);
  pointer-events: auto;
}

/* --- 面板 Header --- */
.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
}
.chat-title {
  font-size: 15px;
  font-weight: 600;
  color: var(--ink);
}
.chat-header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}
.chat-header-actions button {
  background: none;
  border: none;
  color: var(--ink-3);
  cursor: pointer;
  font-size: 13px;
  padding: 4px 10px;
  border-radius: 999px;
  transition: color .2s, background .2s;
}
.chat-header-actions button:hover {
  color: var(--ink);
  background: var(--card-2);
}
.chat-header-actions .chat-close-btn {
  font-size: 20px;
  padding: 4px 8px;
  line-height: 1;
}

/* --- 消息列表 --- */
.chat-body {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  overscroll-behavior: contain;
}
/* 首次空态 */
.chat-body.is-empty {
  align-items: center;
  justify-content: center;
}

/* --- 消息气泡通用 --- */
.chat-msg {
  display: flex;
  gap: 10px;
  max-width: 88%;
  animation: chatMsgIn .3s var(--spring);
}
@keyframes chatMsgIn {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}
.chat-msg.user {
  align-self: flex-end;
  flex-direction: row-reverse;
}
.chat-msg.assistant {
  align-self: flex-start;
}

/* AI 头像 */
.chat-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  flex-shrink: 0;
  overflow: hidden;
  background: var(--card-2);
}
.chat-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* 气泡 */
.chat-bubble {
  padding: 10px 14px;
  border-radius: var(--r-sm);
  font-size: 13.5px;
  line-height: 1.65;
  color: var(--ink);
  word-break: break-word;
  position: relative;
}
.chat-msg.user .chat-bubble {
  background: var(--clay-soft);
  border: 1px solid var(--clay-line);
  border-radius: var(--r-sm) 4px var(--r-sm) var(--r-sm);
}
.chat-msg.assistant .chat-bubble {
  background: var(--card-2);
  border: 1px solid var(--line);
  border-radius: 4px var(--r-sm) var(--r-sm) var(--r-sm);
}

/* AI 回复中的「提示词」高亮 */
.chat-prompt-highlight {
  display: block;
  margin: 10px 0 6px;
  padding: 12px 14px;
  background: var(--paper);
  border-left: 3px solid var(--clay);
  border-radius: 0 var(--r-sm) var(--r-sm) 0;
  font-size: 13px;
  line-height: 1.55;
  color: var(--ink);
}

/* 操作按钮行（填入/复制/重新生成） */
.chat-actions {
  display: flex;
  gap: 8px;
  margin-top: 10px;
  flex-wrap: wrap;
}
.chat-action-btn {
  font-size: 12px;
  padding: 5px 12px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--ink-2);
  cursor: pointer;
  transition: all .2s var(--ease);
  white-space: nowrap;
}
.chat-action-btn:hover {
  border-color: var(--clay-line);
  color: var(--clay-deep);
  background: var(--clay-soft);
}
.chat-action-btn.fill-btn {
  background: var(--clay);
  color: var(--clay-ink);
  border-color: var(--clay);
}
.chat-action-btn.fill-btn:hover {
  background: var(--clay-deep);
}

/* --- 面板 Footer --- */
.chat-footer {
  padding: 10px 14px 12px;
  border-top: 1px solid var(--line);
  flex-shrink: 0;
}
.chat-footer-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}
.chat-footer-hint {
  text-align: center;
  font-size: 10.5px;
  color: var(--ink-4);
  margin-top: 6px;
  user-select: none;
}
.chat-footer textarea {
  flex: 1;
  resize: none;
  border: 1px solid var(--line);
  border-radius: var(--r);
  padding: 10px 12px;
  font-size: 13.5px;
  font-family: inherit;
  line-height: 1.5;
  background: var(--card-2);
  color: var(--ink);
  outline: none;
  min-height: 42px;
  max-height: 100px;
  transition: border-color .2s;
}
.chat-footer textarea:focus {
  border-color: var(--clay-line);
  caret-color: var(--clay);
}
.chat-footer textarea::placeholder {
  color: var(--ink-4);
}

/* 发送按钮 */
.chat-send-btn {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  border: none;
  background: var(--clay);
  color: var(--clay-ink);
  cursor: pointer;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all .2s var(--ease);
}
.chat-send-btn:hover {
  background: var(--clay-deep);
  transform: scale(1.05);
}
.chat-send-btn:active {
  transform: scale(.95);
}
.chat-send-btn:disabled,
.chat-send-btn.is-sending {
  background: var(--line-2);
  color: var(--ink-3);
  cursor: not-allowed;
  pointer-events: none;
}

/* 发送中旋转动画 */
.chat-send-btn.is-sending svg {
  animation: chatSpin .8s linear infinite;
}

/* 暂停按钮 */
.chat-send-btn.is-stopping {
  background: var(--err);
  color: #fff;
}
.chat-send-btn.is-stopping:hover {
  background: #d45a3e;
}

/* --- 思维链（可折叠推理过程） --- */
.chat-thinking {
  margin-bottom: 8px;
  border: 1px solid var(--line);
  border-radius: var(--r-sm);
  overflow: hidden;
  font-size: 12.5px;
}
.chat-thinking-summary {
  padding: 8px 12px;
  background: var(--card-2);
  color: var(--ink-2);
  cursor: pointer;
  user-select: none;
  font-weight: 500;
  transition: background .2s;
  list-style: none;  /* 隐藏默认三角形 */
}
.chat-thinking-summary::-webkit-details-marker {
  display: none;
}
.chat-thinking-summary::before {
  content: '▸';
  display: inline-block;
  margin-right: 6px;
  transition: transform .3s var(--ease);
  font-size: 10px;
}
.chat-thinking[open] .chat-thinking-summary::before {
  transform: rotate(90deg);
}
.chat-thinking-summary:hover {
  background: var(--paper);
}
.chat-thinking-content {
  padding: 10px 14px;
  color: var(--ink-2);
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 160px;
  overflow-y: auto;
  font-style: italic;
  opacity: .85;
  animation: thinkSlideIn .3s var(--ease);
}
@keyframes thinkSlideIn {
  from { max-height: 0; opacity: 0; padding-top: 0; padding-bottom: 0; }
  to   { max-height: 160px; opacity: .85; padding-top: 10px; padding-bottom: 10px; }
}

/* --- Markdown 渲染 --- */
.chat-bubble strong {
  font-weight: 600;
  color: var(--ink);
}
.chat-bubble em {
  font-style: italic;
  color: var(--ink-2);
}
.chat-bubble code {
  background: var(--paper);
  padding: 1px 5px;
  border-radius: 3px;
  font-size: 12px;
  font-family: 'SF Mono', 'Fira Code', monospace;
}
.chat-bubble hr {
  border: none;
  border-top: 1px solid var(--line);
  margin: 10px 0;
}

/* --- Agent 选项 pill --- */
.chat-options-wrap {
  margin: 10px 0 4px;
  display: flex;
  flex-wrap: wrap;
  gap: 7px;
}
.chat-option-btn {
  font-size: 12.5px;
  padding: 6px 15px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--card-2);
  color: var(--ink-2);
  cursor: pointer;
  font-family: inherit;
  white-space: nowrap;
  transition: all .2s var(--ease);
}
.chat-option-btn:hover {
  border-color: var(--ink-3);
  color: var(--ink);
}
/* 选中 — 全冻结，选中的最深 */
.chat-options-wrap.is-chosen .chat-option-btn {
  pointer-events: none;
  background: var(--card-2);
  border-color: var(--line);
  color: var(--ink-4);
}
.chat-options-wrap.is-chosen .chat-option-btn.chosen {
  background: var(--ink);
  border-color: var(--ink);
  color: var(--paper);
}
/* "其他" 按钮样式 — 虚线边框区分 */
.chat-option-other {
  border-style: dashed;
  color: var(--ink-3);
}

/* --- 不完整消息标记 --- */
.chat-msg.incomplete .chat-bubble::after {
  content: '（已中断）';
  display: block;
  margin-top: 6px;
  font-size: 11px;
  color: var(--ink-3);
  font-style: italic;
}
@keyframes chatSpin {
  to { transform: rotate(360deg); }
}

/* --- 流式打字光标指示器 --- */
.chat-cursor {
  display: inline-block;
  width: 3px;
  height: 1.1em;
  background: #e07b5a;
  vertical-align: text-bottom;
  margin-left: 1px;
  border-radius: 1px;
  animation: chatBlink .7s step-end infinite;
  box-shadow: 0 0 4px rgba(224, 123, 90, 0.5);
}
@keyframes chatBlink {
  50% { opacity: 0.15; }
}

/* --- 加载占位：三个跳动圆点 --- */
.chat-loading-dots {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 2px 0;
}
.chat-loading-dots i {
  display: inline-block;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--ink-3);
  animation: chatDotBounce 1.2s ease-in-out infinite;
}
.chat-loading-dots i:nth-child(2) { animation-delay: .15s; }
.chat-loading-dots i:nth-child(3) { animation-delay: .3s; }
@keyframes chatDotBounce {
  0%, 60%, 100% { transform: translateY(0); opacity: .3; }
  30% { transform: translateY(-6px); opacity: 1; }
}

/* --- 错误消息 --- */
.chat-msg.error .chat-bubble {
  background: rgba(192, 87, 61, 0.08);
  border-color: rgba(192, 87, 61, 0.2);
  color: var(--err);
}

/* --- 暗色主题：头像背景提亮 --- */
[data-theme="dark"] .chat-avatar {
  background: rgba(255,255,255,.15);
}
[data-theme="dark"] .chat-avatar img {
  filter: invert(1) brightness(2);
}

/* ============================================================
   移动端适配
   ============================================================ */
@media (max-width: 900px) {
  .chat-fab {
    bottom: 18px;
    right: 18px;
    width: 46px;
    height: 46px;
  }
  .chat-panel {
    bottom: 0;
    right: 0;
    left: 0;
    width: 100%;
    height: 70vh;
    max-height: 70vh;
    border-radius: var(--r-lg) var(--r-lg) 0 0;
    transform: translateY(100%);
    transition: transform .4s var(--spring), opacity .3s var(--ease);
    transform-origin: bottom center;
  }
  .chat-panel.is-open {
    transform: translateY(0);
  }
  /* 移动端加底部安全区 */
  .chat-footer {
    padding-bottom: max(12px, env(safe-area-inset-bottom));
  }
}
