/* frontend-react/src/components/Mid/ChatBox/ChatBox.css */
.chat-box {
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 100%;
  height: 100%;
  position: relative;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: var(--spacing-lg);
  display: flex;
  flex-direction: column;
  gap: var(--spacing-sm);
  background-color: var(--color-bg-subtle); /* 跟随主题的背景色 */
  min-height: 0; /* 确保 flex 子元素可以收缩 */
}

/* 滚动条美化 */
.chat-messages::-webkit-scrollbar {
  width: 8px;
}

.chat-messages::-webkit-scrollbar-track {
  background: transparent;
}

.chat-messages::-webkit-scrollbar-thumb {
  background-color: rgba(0, 0, 0, 0.2);
  border-radius: 4px;
}

.chat-messages::-webkit-scrollbar-thumb:hover {
  background-color: rgba(0, 0, 0, 0.3);
}

.message {
  max-width: 100%; /* SillyTavern 风格：占满宽度 */
  padding: var(--spacing-md) var(--spacing-lg);
  border-radius: 0; /* 去掉圆角 */
  line-height: 1.8;
  position: relative; /* ✅ 为绝对定位的 swipe 按钮提供定位上下文 */
  transition: background-color 0.2s ease;
  font-size: 1rem;
  margin: 0;
  font-family: var(--font-body); /* 正文使用微软雅黑 */
}

/* 用户消息 - 右侧对齐，用色彩区分 */
.message.user {
  align-self: stretch;
  background: linear-gradient(to right, rgba(102, 126, 234, 0.08), rgba(102, 126, 234, 0.15));
  color: var(--color-text-primary);
  border-left: 3px solid var(--color-accent);
}

/* AI 消息 - 左侧对齐，用色彩区分 */
/* 参考 Discord/Slack/Notion 的夜间模式配色 */
.message.ai {
  align-self: stretch;
  /* 日间模式：半透明白色背景 */
  background-color: rgba(255, 255, 255, 0.6);
  color: var(--color-text-primary);
  border-left: 3px solid transparent;
}

/* 深色主题下的 AI 消息配色优化 */
[data-color-theme='dark'] .message.ai {
  /* 使用深灰色背景，避免纯白色刺眼 */
  /* 参考 VS Code / Discord 的深色模式 */
  background-color: rgba(45, 45, 48, 0.5);
  color: var(--color-text-primary);
}

.message-container {
  display: flex;
  flex-direction: column;
  gap: var(--spacing-xs);
  max-width: 100%;
}

.message-header {
  display: flex;
  align-items: center;
  gap: var(--spacing-sm);
  margin-bottom: var(--spacing-xs);
  opacity: 0.7;
}

.message-name {
  font-weight: 600;
  font-size: 0.85rem;
  color: inherit;
  font-family: var(--font-ui); /* 角色名使用微软雅黑 UI */
}

.message-id {
  font-size: 0.75rem;
  color: inherit;
  opacity: 0.6;
}

.message-toolbar {
  margin-left: auto;
  opacity: 0;
  transition: opacity 0.2s ease;
}

.message:hover .message-toolbar {
  opacity: 1;
}

.toolbar-buttons {
  display: flex;
  gap: var(--spacing-xs);
}

.toolbar-button {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 0.9rem;
  padding: var(--spacing-xs) var(--spacing-sm);
  border-radius: var(--radius-sm);
  transition: all var(--transition-fast);
  color: var(--color-text-muted);
  opacity: 0.6;
}

.toolbar-button:hover {
  background-color: rgba(0, 0, 0, 0.05);
  color: var(--color-accent);
  opacity: 1;
}

.message-content {
  position: relative;
  min-height: calc(1lh + var(--spacing-xs) * 2); /* ✅ 确保编辑时保持高度 */
}

.bubble {
  padding: var(--spacing-xs) 0;
  border-radius: 0;
  line-height: 1.7;
  word-wrap: break-word;
  overflow-wrap: break-word;
}

.message.user .bubble {
  background: transparent;
  color: var(--color-text-primary);
}

.message.ai .bubble {
  background: transparent;
  color: var(--color-text-primary);
}

/* 纯文本模式 */
.plain-text {
  white-space: pre-wrap;
  word-wrap: break-word;
  line-height: 1.8;
  font-size: 1rem;
}

.edit-container {
  display: flex;
  flex-direction: column;
  gap: var(--spacing-md);
  width: 100%;
  min-height: calc(1lh + var(--spacing-xs) * 2); /* ✅ 至少保持一行文本的高度 */
}

.edit-textarea {
  width: 100%;
  min-height: calc(1lh + var(--spacing-xs) * 2); /* ✅ 与 bubble 的 padding 保持一致 */
  max-height: none; /* 不限制最大高度 */
  padding: var(--spacing-xs) 0; /* 与 bubble 保持一致 */
  border: 2px solid var(--color-accent); /* ✅ 添加边框提示编辑状态 */
  border-radius: var(--radius-sm);
  resize: vertical;
  font-family: inherit;
  font-size: 1rem;
  line-height: 1.7;
  background-color: rgba(102, 126, 234, 0.05); /* ✅ 轻微背景色区分编辑状态 */
  color: var(--color-text-primary);
  outline: none;
  box-sizing: border-box; /* ✅ 确保宽度计算正确 */
}

.edit-textarea:focus {
  background-color: rgba(102, 126, 234, 0.05); /* 聚焦时轻微背景色 */
}

.edit-buttons {
  display: flex;
  justify-content: flex-end;
  gap: var(--spacing-sm);
  margin-top: var(--spacing-xs);
}

.cancel-button, .save-button {
  padding: var(--spacing-xs) var(--spacing-md);
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-weight: 500;
  font-size: 0.85rem;
  transition: all 0.2s;
}

.cancel-button {
  background-color: rgba(0, 0, 0, 0.05);
  color: var(--color-text-secondary);
}

.cancel-button:hover {
  background-color: rgba(0, 0, 0, 0.1);
  color: var(--color-text-primary);
}

.save-button {
  background-color: #667eea;
  color: white;
}

.save-button:hover {
  background-color: #5568d3;
  transform: translateY(-1px);
  box-shadow: 0 2px 4px rgba(102, 126, 234, 0.3);
}

/* ==================== Swipe Controls - 绝对定位到两侧 ==================== */

.swipe-controls-absolute {
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  transform: translateY(-50%);
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 var(--spacing-sm); /* ✅ 在 padding 区域内 */
  pointer-events: none; /* ✅ 让点击事件穿透到消息内容 */
  z-index: 10;
}

.swipe-button-side {
  background: rgba(255, 255, 255, 0.15); /* ✅ 半透明背景 */
  backdrop-filter: blur(8px); /* ✅ 毛玻璃效果 */
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: var(--radius-sm);
  cursor: pointer;
  padding: 6px 12px;
  font-size: 0.9rem;
  color: var(--color-text-secondary);
  transition: all 0.2s ease;
  min-width: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0; /* ✅ 默认隐藏 */
  pointer-events: auto; /* ✅ 按钮本身可以接收点击 */
}

/* 深色主题下的 swipe 按钮 */
[data-color-theme='dark'] .swipe-button-side {
  background: rgba(0, 0, 0, 0.3);
  border-color: rgba(255, 255, 255, 0.1);
}

/* ✅ hover 消息时显示按钮 */
.message:hover .swipe-button-side {
  opacity: 0.7;
}

.swipe-button-side:hover:not(:disabled) {
  background-color: rgba(102, 126, 234, 0.2); /* ✅ hover 时更明显 */
  border-color: var(--color-accent);
  color: var(--color-accent);
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  opacity: 1 !important; /* ✅ hover 时完全不透明 */
}

.swipe-button-side:active:not(:disabled) {
  transform: translateY(0);
}

.swipe-button-side:disabled {
  opacity: 0.2 !important;
  cursor: not-allowed;
  background-color: rgba(0, 0, 0, 0.05);
}

.swipe-counter-side {
  font-size: 0.75rem;
  color: var(--color-text-secondary);
  font-weight: 600;
  min-width: 40px;
  text-align: center;
  padding: 2px 6px;
  background-color: rgba(0, 0, 0, 0.05);
  border-radius: var(--radius-sm);
  opacity: 0; /* ✅ 默认隐藏 */
  pointer-events: none;
  transition: opacity 0.2s ease;
}

/* ✅ hover 消息时显示页码 */
.message:hover .swipe-counter-side {
  opacity: 0.6;
}

/* ==================== Input Area - Fixed at Bottom ==================== */

.chat-input-wrapper {
  position: sticky;
  bottom: 0;
  left: 0;
  right: 0;
  background-color: var(--color-bg-primary); /* 跟随主题 */
  padding: var(--spacing-md) var(--spacing-lg);
  z-index: var(--z-divider); /* ✅ 基础层 - 分割线层级 */
  border-top: 1px solid var(--color-border-light);
}

.input-container {
  display: flex;
  align-items: center; /* 垂直居中对齐 */
  gap: var(--spacing-sm);
  padding: var(--spacing-xs) var(--spacing-sm);
  background-color: var(--color-bg-elevated); /* 极浅色，跟随主题 */
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md); /* 适中的圆角 */
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06); /* 轻量级阴影 */
  transition: all 0.2s ease;
}

.input-container:focus-within {
  border-color: var(--color-accent);
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.options-wrapper {
  position: relative;
  flex-shrink: 0;
  display: flex;
  align-items: center;
}

.options-toggle {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: none;
  border-radius: var(--radius-md);
  color: var(--color-text-muted);
  font-size: 1.3rem;
  cursor: pointer;
  transition: all 0.2s ease;
}

.options-toggle:hover {
  background-color: rgba(102, 126, 234, 0.08);
  color: var(--color-accent);
}

.options-toggle.active {
  background-color: rgba(102, 126, 234, 0.12);
  color: var(--color-accent);
}

.chat-options {
  position: absolute;
  bottom: calc(100% + var(--spacing-sm));
  left: 0;
  background: var(--color-bg-elevated);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
  padding: var(--spacing-sm);
  min-width: 220px;
  z-index: var(--z-dropdown-menu); /* ✅ 组件层 - 下拉菜单 */
  backdrop-filter: blur(10px);
}

/* 选项组标题 */
.option-group-title {
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  padding: var(--spacing-xs) var(--spacing-sm);
  margin-bottom: var(--spacing-xs);
}

/* 渲染模式按钮 */
.render-mode-btn {
  width: 100%;
  padding: var(--spacing-sm) var(--spacing-md);
  background: transparent;
  color: var(--color-text-primary);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  cursor: pointer;
  font-size: 0.85rem;
  font-weight: 500;
  transition: all 0.15s ease;
  text-align: left;
  display: flex;
  align-items: center;
  gap: var(--spacing-sm);
  margin-bottom: var(--spacing-sm);
}

.render-mode-btn:hover {
  background-color: rgba(102, 126, 234, 0.08);
  border-color: var(--color-accent);
  color: var(--color-accent);
}

.render-mode-btn:active {
  transform: scale(0.98);
}

/* 复选框选项 - 简化样式 */
.option-checkbox {
  display: flex;
  align-items: center;
  gap: var(--spacing-sm);
  padding: var(--spacing-sm) var(--spacing-md);
  cursor: pointer;
  border-radius: var(--radius-md);
  transition: all 0.15s ease;
  margin-bottom: var(--spacing-xs);
}

.option-checkbox:hover {
  background-color: rgba(102, 126, 234, 0.06);
}

.option-checkbox input[type="checkbox"] {
  display: none;
}

.checkmark {
  width: 16px;
  height: 16px;
  border: 2px solid var(--color-border);
  border-radius: 3px;
  position: relative;
  transition: all 0.15s ease;
  flex-shrink: 0;
}

.option-checkbox:hover .checkmark {
  border-color: var(--color-accent);
}

.option-checkbox input:checked + .checkmark {
  background-color: var(--color-accent);
  border-color: var(--color-accent);
}

.option-checkbox input:checked + .checkmark::after {
  content: '';
  position: absolute;
  left: 4px;
  top: 1px;
  width: 4px;
  height: 8px;
  border: solid white;
  border-width: 0 2px 2px 0;
  transform: rotate(45deg);
}

.option-label {
  font-size: 0.85rem;
  color: var(--color-text-primary);
  user-select: none;
  flex: 1;
}

/* 分隔线 */
.option-divider {
  height: 1px;
  background-color: var(--color-border);
  margin: var(--spacing-sm) 0;
  opacity: 0.5;
}

/* 切换聊天按钮 */
.switch-chat-btn {
  width: 100%;
  padding: var(--spacing-sm) var(--spacing-md);
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  border-radius: var(--radius-md);
  cursor: pointer;
  font-size: 0.85rem;
  font-weight: 500;
  transition: all 0.15s ease;
  box-shadow: 0 2px 4px rgba(102, 126, 234, 0.2);
}

.switch-chat-btn:hover {
  background: linear-gradient(135deg, #5568d3 0%, #6a3f8f 100%);
  transform: translateY(-1px);
  box-shadow: 0 4px 8px rgba(102, 126, 234, 0.3);
}

.switch-chat-btn:active {
  transform: translateY(0);
}

.chat-input-area {
  flex: 1;
  min-width: 0;
  position: relative;
  display: flex;
  align-items: center;
}

.message-input {
  width: 100%;
  padding: var(--spacing-sm) var(--spacing-md);
  border: none;
  border-radius: var(--radius-md);
  background-color: transparent;
  color: var(--color-text-primary);
  font-size: 0.95rem;
  resize: none;
  overflow-y: auto;
  min-height: 36px;
  max-height: 300px;
  transition: all 0.15s ease;
  font-family: inherit;
  line-height: 1.6;
  display: block;
  
  /* ✅ 业界标准：使用 field-sizing 实现自适应高度 */
  /* 支持此属性的浏览器会自动根据内容调整高度 */
  field-sizing: content;
  
  /* ✅ 兼容性方案：为不支持 field-sizing 的浏览器提供回退 */
  height: auto;
}

.message-input:focus {
  outline: none;
  background-color: rgba(102, 126, 234, 0.03);
}

.message-input::placeholder {
  color: var(--color-text-muted);
  opacity: 0.5;
}

.send-button {
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: all 0.2s ease;
  font-size: 1.3rem;
  line-height: 1;
  flex-shrink: 0;
  box-shadow: 0 2px 4px rgba(102, 126, 234, 0.2);
  align-self: center; /* 确保按钮垂直居中 */
}

.send-button:hover:not(:disabled) {
  background: linear-gradient(135deg, #5568d3 0%, #6a3f8f 100%);
  transform: translateY(-1px);
  box-shadow: 0 4px 8px rgba(102, 126, 234, 0.3);
}

.send-button:active:not(:disabled) {
  transform: translateY(0);
}

.send-button:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.send-button.stopping {
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
  animation: pulse 1.5s infinite;
}

@keyframes pulse {
  0%, 100% {
    opacity: 1;
  }
  50% {
    opacity: 0.7;
  }
}

.send-button svg {
  display: none;
}

.loading, .error {
  text-align: center;
  padding: var(--spacing-xl);
  color: var(--color-text-muted);
}

.error {
  color: var(--color-error);
}

/* 聊天选择器弹窗 */
.chat-selector-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: var(--z-modal-overlay); /* ✅ 弹窗层 - 遮罩 */
}

.chat-selector-modal {
  background: var(--color-bg-primary);
  border-radius: var(--radius-lg);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
  max-width: 600px;
  width: 90%;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
}

.chat-selector-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: var(--spacing-lg) var(--spacing-xl);
  border-bottom: 1px solid var(--color-border);
}

.chat-selector-header h3 {
  margin: 0;
  font-size: 1rem;
  color: var(--color-text-primary);
  font-weight: 600;
}

.close-btn {
  background: none;
  border: none;
  font-size: 1.5rem;
  color: var(--color-text-muted);
  cursor: pointer;
  padding: 0;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color var(--transition-fast);
  border-radius: var(--radius-sm);
}

.close-btn:hover {
  color: var(--color-error);
  background-color: var(--color-bg-tertiary);
}

.chat-selector-body {
  flex: 1;
  overflow-y: auto;
  padding: var(--spacing-lg);
}

/* 新建聊天按钮区域 */
.chat-selector-actions {
  margin-bottom: var(--spacing-md);
  padding-bottom: var(--spacing-md);
  border-bottom: 1px solid var(--color-border);
}

.new-chat-btn {
  width: 100%;
  padding: var(--spacing-md) var(--spacing-lg);
  background: var(--gradient-primary);
  color: white;
  border: none;
  border-radius: var(--radius-md);
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition-fast);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--spacing-xs);
}

.new-chat-btn:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-md);
  filter: brightness(1.1);
}

.new-chat-btn:active {
  transform: translateY(0);
}

.chat-list {
  display: flex;
  flex-direction: column;
  gap: var(--spacing-sm);
}

.chat-option {
  padding: var(--spacing-md);
  background: var(--color-bg-secondary);
  border: 2px solid var(--color-border);
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: all var(--transition-fast);
  position: relative; /* ✅ 为删除按钮定位 */
  display: flex;
  align-items: center;
  gap: var(--spacing-sm);
}

.chat-option:hover {
  border-color: var(--color-accent);
  background: var(--color-bg-tertiary);
}

.chat-option-content {
  cursor: pointer;
  flex: 1; /* ✅ 占据剩余空间 */
}

/* ✅ 删除按钮样式 */
.chat-delete-btn {
  background: none;
  border: none;
  font-size: 1.2rem;
  cursor: pointer;
  padding: var(--spacing-xs) var(--spacing-sm);
  border-radius: var(--radius-sm);
  opacity: 0; /* 默认隐藏 */
  transition: all var(--transition-fast);
  color: var(--color-text-muted);
}

.chat-option:hover .chat-delete-btn {
  opacity: 0.6; /* hover 时显示 */
}

.chat-delete-btn:hover {
  opacity: 1 !important;
  background: rgba(255, 77, 77, 0.15);
  color: #ff4d4d;
  transform: scale(1.1);
}

.chat-option-name {
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--color-text-primary);
  margin-bottom: var(--spacing-xs);
}

.chat-option-preview {
  font-size: 0.85rem;
  color: var(--color-text-muted);
  line-height: 1.4;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.empty-chats {
  text-align: center;
  padding: var(--spacing-xl) var(--spacing-lg);
  color: var(--color-text-muted);
}

.empty-chats p {
  margin: 0;
  font-size: 0.9rem;
}

/* ✅ 右键菜单样式已移至全局 context-menu.css */
