/* ===== 字体导入 ===== */
@import url('https://fonts.googleapis.com/css2?family=DM+Sans:wght@400;500;700&family=Space+Grotesk:wght@400;500;600;700&display=swap');

/* ===== CSS 变量（主题） ===== */
:root {
  /* 字体 */
  --font-display: 'Space Grotesk', 'DM Sans', sans-serif;
  --font-body: 'DM Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;

  /* 品牌色 */
  --color-primary: #2563EB;
  --color-primary-hover: #1E40AF;
  --color-primary-light: #EFF6FF;

  /* 背景 */
  --bg-page: #F0F2F5;
  --bg-card: #FFFFFF;
  --bg-header: #1E293B;

  /* 文字 */
  --text-primary: #1E293B;
  --text-secondary: #475569;     /* slate-600 — 白底对比度 ~5.1:1 ✅ WCAG AA */
  --text-white: #FFFFFF;

  /* 气泡 */
  --bubble-ai-bg: #F1F5F9;
  --bubble-user-bg: #2563EB;

  /* 辅助 */
  --border-color: #E2E8F0;
  --shadow-card: 0 2px 12px rgba(0, 0, 0, 0.08);
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
}

/* ===== 暗色模式 ===== */
/* ===== 重置 ===== */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

/* ===== 焦点状态 ===== */
:focus-visible {
  outline: 2.5px solid var(--color-primary);
  outline-offset: 2px;
}

.send-btn:focus-visible,
.upload-btn:focus-visible,
.clear-btn:focus-visible,
.remove-file-btn:focus-visible {
  border-radius: var(--radius-md);
}

.chat-input:focus-visible {
  border-color: var(--color-primary);
  outline: 2.5px solid var(--color-primary);
  outline-offset: 0;
}

body {
  font-family: var(--font-body);
  background: var(--bg-page);
  color: var(--text-primary);
  height: 100vh;
  overflow: hidden;
  line-height: 1.5;
}

/* ===== 布局容器 ===== */
.app-layout {
  display: flex;
  height: 100vh;
  width: 100%;
}

.main-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

/* ===== 侧栏切换按钮 ===== */
.sidebar-toggle {
  background: none;
  border: none;
  color: rgba(255,255,255,0.8);
  font-size: 20px;
  cursor: pointer;
  padding: 2px 6px;
  border-radius: 6px;
  transition: background 0.2s, color 0.2s;
  flex-shrink: 0;
  display: none;
}

.sidebar-toggle:hover {
  background: rgba(255,255,255,0.1);
  color: #fff;
}

/* ===== 顶栏 ===== */
.chat-header {
  background: var(--bg-header);
  color: var(--text-white);
  padding: 14px 20px;
  flex-shrink: 0;
  z-index: 10;
}

.header-content {
  display: flex;
  align-items: center;
  gap: 10px;
}

.header-icon {
  font-size: 20px;
  opacity: 0.9;
}

.header-title {
  font-family: var(--font-display);
  font-size: 16px;
  font-weight: 600;
  letter-spacing: 0.5px;
}

.clear-btn {
  margin-left: auto;
  background: none;
  border: 1px solid rgba(255, 255, 255, 0.3);
  color: rgba(255, 255, 255, 0.8);
  padding: 5px 12px;
  border-radius: 8px;
  font-size: 13px;
  cursor: pointer;
  transition: all 0.2s;
  font-family: inherit;
  white-space: nowrap;
}

.clear-btn:hover {
  background: rgba(255, 255, 255, 0.1);
  color: #fff;
  border-color: rgba(255, 255, 255, 0.5);
}

.clear-btn:active {
  transform: scale(0.96);
}

/* ===== 消息区域 ===== */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px 16px;
  max-width: 800px;
  width: 100%;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* ===== 消息气泡 ===== */
.message {
  display: flex;
  gap: 10px;
  max-width: 85%;
  animation: messageIn 0.3s ease-out;
}

@keyframes messageIn {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* AI 消息：左对齐 */
.message-ai {
  align-self: flex-start;
}

/* 用户消息：右对齐 */
.message-user {
  align-self: flex-end;
  flex-direction: row-reverse;
}

/* ===== 头像 ===== */
.avatar {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 15px;
  flex-shrink: 0;
}

.avatar-ai {
  background: var(--color-primary-light);
  color: var(--color-primary);
}

.avatar-ai::before {
  content: "A";
  font-weight: 600;
  font-size: 15px;
  font-family: var(--font-display);
}

.avatar-user {
  background: #DBEAFE;
  color: var(--color-primary);
}

.avatar-user::before {
  content: "U";
  font-weight: 600;
  font-size: 15px;
  font-family: var(--font-display);
}

/* ===== 消息内容 ===== */
.message-content {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.bubble {
  padding: 12px 16px;
  font-size: 16px;
  line-height: 1.6;
  word-wrap: break-word;
  overflow-wrap: break-word;
}

.bubble-ai {
  background: var(--bubble-ai-bg);
  color: var(--text-primary);
  border-radius: 4px var(--radius-lg) var(--radius-lg) var(--radius-lg);
}

.bubble-user {
  background: var(--bubble-user-bg);
  color: var(--text-white);
  border-radius: var(--radius-lg) 4px var(--radius-lg) var(--radius-lg);
}

/* AI 消息内 Markdown 样式 */
.bubble-ai p {
  margin-bottom: 8px;
}

.bubble-ai p:last-child {
  margin-bottom: 0;
}

.bubble-ai code {
  background: #E2E8F0;
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 13px;
}

.bubble-ai pre {
  background: #1E293B;
  color: #E2E8F0;
  padding: 12px;
  border-radius: var(--radius-sm);
  overflow-x: auto;
  margin: 8px 0;
}

.bubble-ai pre code {
  background: none;
  padding: 0;
  color: inherit;
}

.bubble-ai ul,
.bubble-ai ol {
  padding-left: 20px;
  margin: 8px 0;
}

.bubble-ai a {
  color: var(--color-primary);
  text-decoration: underline;
}

.bubble-ai blockquote {
  border-left: 3px solid var(--color-primary);
  padding-left: 12px;
  color: var(--text-secondary);
  margin: 8px 0;
}

/* ===== 思考链折叠 ===== */
.thinking-details {
  margin-top: 8px;
  font-size: 13px;
}

.thinking-summary {
  color: var(--text-secondary);
  cursor: pointer;
  user-select: none;
  padding: 4px 0;
}

.thinking-summary:hover {
  color: var(--text-primary);
}

.thinking-content {
  margin-top: 6px;
  padding: 10px 12px;
  background: #f1f5f9;
  border: 1px solid #e2e8f0;
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  font-size: 13px;
  line-height: 1.5;
  white-space: pre-wrap;
}

/* ===== 打字光标 ===== */
.typing-cursor {
  display: inline-block;
  width: 2px;
  height: 16px;
  background: var(--color-primary);
  margin-left: 2px;
  animation: blink 0.8s step-end infinite;
  vertical-align: text-bottom;
}

@keyframes blink {
  50% { opacity: 0; }
}

/* ===== 加载指示器 ===== */
.typing-indicator {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text-secondary);
  font-size: 14px;
  padding: 8px 0;
}

.typing-dots {
  display: flex;
  gap: 4px;
}

.typing-dots span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-secondary);
  animation: dotBounce 1.4s ease-in-out infinite;
}

.typing-dots span:nth-child(2) {
  animation-delay: 0.2s;
}

.typing-dots span:nth-child(3) {
  animation-delay: 0.4s;
}

@keyframes dotBounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
  30% { transform: translateY(-4px); opacity: 1; }
}

/* ===== 底部输入区 ===== */
.chat-input-area {
  background: var(--bg-card);
  border-top: 1px solid var(--border-color);
  padding: 12px 16px;
  flex-shrink: 0;
}

.input-wrapper {
  max-width: 800px;
  margin: 0 auto;
  display: flex;
  gap: 10px;
  align-items: center;
}

.chat-input {
  flex: 1;
  padding: 12px 16px;
  border: 1.5px solid var(--border-color);
  border-radius: var(--radius-md);
  font-size: 15px;
  outline: none;
  transition: border-color 0.2s;
  font-family: inherit;
}

.chat-input:focus {
  border-color: var(--color-primary);
}

.chat-input:disabled {
  background: #F8FAFC;
  cursor: not-allowed;
}

.send-btn {
  width: 44px;
  height: 44px;
  border: none;
  border-radius: var(--radius-md);
  background: var(--color-primary);
  color: var(--text-white);
  font-size: 18px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s, transform 0.1s;
  flex-shrink: 0;
}

.send-btn:hover {
  background: var(--color-primary-hover);
}

.send-btn:active {
  transform: scale(0.95);
}

.send-btn:disabled {
  background: #94A3B8;
  cursor: not-allowed;
  transform: none;
}

.send-icon {
  transition: transform 0.2s;
}

/* loading 时隐藏箭头字符，显示纯 CSS spinner */
.send-btn.loading .send-icon {
  font-size: 0;
  display: inline-block;
  width: 18px;
  height: 18px;
  border: 2.5px solid rgba(255, 255, 255, 0.3);
  border-top-color: #fff;
  border-radius: 50%;
  animation: sendSpinner 0.6s linear infinite;
}

@keyframes sendSpinner {
  to { transform: rotate(360deg); }
}

/* ===== 上传按钮 ===== */
.upload-btn {
  width: 44px;
  height: 44px;
  border: none;
  border-radius: var(--radius-md);
  background: transparent;
  color: var(--text-secondary);
  font-size: 20px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.2s, color 0.2s;
}

.upload-btn:hover {
  background: var(--color-primary-light);
  color: var(--color-primary);
}

.upload-btn:active {
  transform: scale(0.95);
}

/* ===== 文件预览区域（多文件） ===== */
.preview-area {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 12px;
  margin-bottom: 8px;
  background: #F8FAFC;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  max-width: 800px;
  margin-left: auto;
  margin-right: auto;
  overflow-x: auto;
  overflow-y: hidden;
  animation: previewSlideIn 0.25s ease-out;
}

@keyframes previewSlideIn {
  from {
    opacity: 0;
    transform: translateY(-10px);
    max-height: 0;
    padding-top: 0;
    padding-bottom: 0;
    margin-bottom: 0;
  }
  to {
    opacity: 1;
    transform: translateY(0);
    max-height: 100px;
  }
}

/* 退出动画 */
.preview-area.removing {
  animation: previewSlideOut 0.2s ease-in forwards;
  pointer-events: none;
}

@keyframes previewSlideOut {
  to {
    opacity: 0;
    transform: translateY(-10px);
    max-height: 0;
    padding-top: 0;
    padding-bottom: 0;
    margin-bottom: 0;
  }
}

/* 预览列表 — 横向滚动 */
.preview-list {
  display: flex;
  gap: 8px;
  align-items: center;
}

.preview-item {
  position: relative;
  width: 48px;
  height: 48px;
  border-radius: 6px;
  overflow: hidden;
  flex-shrink: 0;
  background: #E2E8F0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.preview-item img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.preview-item .file-icon {
  font-size: 22px;
  line-height: 1;
}

.remove-file-btn {
  position: absolute;
  top: -5px;
  right: -5px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  border: none;
  background: #EF4444;
  color: white;
  font-size: 11px;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
  transition: background 0.15s;
}

.remove-file-btn:hover {
  background: #DC2626;
}

/* 扩展触控区域至 ~44×44px（不改变视觉尺寸） */
.remove-file-btn::before {
  content: '';
  position: absolute;
  top: -12px;
  right: -12px;
  left: -12px;
  bottom: -12px;
  border-radius: 50%;
}

/* ===== 消息中的上传文件（用户侧） ===== */
.uploaded-images {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
}

.uploaded-images .uploaded-image img {
  max-width: 120px;
  max-height: 120px;
  border-radius: 8px;
  cursor: pointer;
  border: 1px solid var(--border-color);
  display: block;
  object-fit: cover;
}

.uploaded-file {
  margin-top: 4px;
  font-size: 13px;
  color: var(--text-secondary);
  padding: 6px 10px;
  background: rgba(255,255,255,0.15);
  border-radius: var(--radius-sm);
  white-space: nowrap;
}

/* ===== 错误提示 ===== */
.error-banner {
  background: #FEF2F2;
  color: #DC2626;
  padding: 10px 16px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  text-align: center;
  animation: messageIn 0.3s ease-out;
}

/* ===== 侧栏 ===== */
.sidebar {
  width: 280px;
  min-width: 280px;
  background: var(--bg-card);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  height: 100vh;
  z-index: 20;
}

.sidebar-header {
  padding: 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border-color);
  flex-shrink: 0;
}

.sidebar-title {
  font-family: var(--font-display);
  font-size: 15px;
  font-weight: 600;
  color: var(--text-primary);
  display: flex;
  align-items: center;
  gap: 8px;
}

.sidebar-title::before {
  content: '';
  display: inline-block;
  width: 3px;
  height: 16px;
  background: var(--color-primary);
  border-radius: 2px;
}

.new-conv-btn {
  width: 32px;
  height: 32px;
  border: none;
  border-radius: 8px;
  background: var(--color-primary);
  color: white;
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s, transform 0.15s;
  flex-shrink: 0;
}

.new-conv-btn:hover {
  background: var(--color-primary-hover);
}

.new-conv-btn:active {
  transform: scale(0.92);
}

.conversation-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.conversation-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.15s;
  margin-bottom: 2px;
  position: relative;
  border-left: 3px solid transparent;
}

.conversation-item:hover {
  background: var(--color-primary-light);
}

.conversation-item.active {
  background: var(--color-primary-light);
  border-left-color: var(--color-primary);
}

.conv-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--color-primary-light);
  color: var(--color-primary);
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 14px;
  flex-shrink: 0;
}

.conv-info {
  flex: 1;
  min-width: 0;
}

.conv-title {
  font-size: 14px;
  font-weight: 500;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.conv-time {
  font-size: 11px;
  color: var(--text-secondary);
  margin-top: 2px;
}

.conv-actions {
  display: none;
  gap: 4px;
  flex-shrink: 0;
}

.conversation-item:hover .conv-actions {
  display: flex;
}

.conv-action-btn {
  width: 28px;
  height: 28px;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--text-secondary);
  font-size: 13px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, color 0.15s;
}

.conv-action-btn:hover {
  background: var(--border-color);
  color: var(--text-primary);
}

.conv-action-btn.delete:hover {
  background: #FEE2E2;
  color: #DC2626;
}

/* 侧栏 — 暗色模式适配 */
@media (prefers-color-scheme: dark) {
  .sidebar {
    background: var(--bg-card);
  }
  .conv-action-btn:hover {
    background: #334155;
    color: var(--text-primary);
  }
  .conv-action-btn.delete:hover {
    background: #3B1A1A;
    color: #FCA5A5;
  }
}

/* 桌面端正常显示侧栏 */
@media (min-width: 600px) {
  .sidebar-toggle {
    display: none;
  }
}

/* 移动端：侧栏浮层模式 */
@media (max-width: 599px) {
  .sidebar {
    position: fixed;
    left: -280px;
    top: 0;
    bottom: 0;
    z-index: 100;
    transition: left 0.25s ease;
    box-shadow: 2px 0 12px rgba(0,0,0,0.15);
  }

  .sidebar.sidebar-visible {
    left: 0;
  }

  .sidebar-toggle {
    display: block;
  }

  .sidebar-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.4);
    z-index: 90;
    display: none;
  }

  .sidebar-backdrop.visible {
    display: block;
  }
}

/* ===== 响应式 ===== */
@media (max-width: 600px) {
  .chat-header {
    padding: 12px 16px;
  }

  .chat-messages {
    padding: 16px 12px;
    gap: 12px;
  }

  .message {
    max-width: 92%;
  }

  .bubble {
    padding: 10px 14px;
    font-size: 14px;
  }

  .chat-input-area {
    padding: 10px 12px;
  }

  .chat-input {
    padding: 10px 14px;
    font-size: 14px;
  }

  .send-btn {
    width: 40px;
    height: 40px;
    font-size: 16px;
  }

  .upload-btn {
    width: 40px;
    height: 40px;
    font-size: 18px;
  }

  .uploaded-images .uploaded-image img {
    max-width: 90px;
    max-height: 90px;
  }

  .preview-area {
    padding: 6px 10px;
  }

  .preview-item {
    width: 40px;
    height: 40px;
  }
}

/* ===== 侧栏空状态引导 ===== */
.sidebar-empty {
  padding: 32px 16px;
  text-align: center;
  color: var(--text-secondary);
}
.sidebar-empty-icon {
  font-size: 36px;
  margin-bottom: 12px;
}
.sidebar-empty-title {
  font-size: 15px;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 8px;
}
.sidebar-empty-desc {
  font-size: 13px;
  margin-bottom: 16px;
}
.sidebar-empty-hint {
  font-size: 12px;
  line-height: 1.8;
  opacity: 0.7;
}
.sidebar-empty-hint strong {
  background: var(--primary-color);
  color: #fff;
  display: inline-block;
  width: 20px;
  height: 20px;
  line-height: 20px;
  border-radius: 4px;
  font-size: 14px;
  text-align: center;
}

/* ===== 离线提示横幅 ===== */
.offline-banner {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px;
  margin: 0 16px 12px;
  background: #FEF3C7;
  border: 1px solid #FCD34D;
  border-radius: 8px;
  color: #92400E;
  font-size: 13px;
  line-height: 1.4;
}
.offline-banner .offline-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #EF4444;
  flex-shrink: 0;
  animation: pulse-dot 2s ease-in-out infinite;
}
.offline-banner .offline-close {
  margin-left: auto;
  cursor: pointer;
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
  font-size: 14px;
  flex-shrink: 0;
  opacity: 0.6;
}
.offline-banner .offline-close:hover {
  opacity: 1;
  background: rgba(0,0,0,0.1);
}
@keyframes pulse-dot {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.3; }
}

/* ===== 暗色模式 ===== */
@media (prefers-color-scheme: dark) {
  :root {
    --color-primary: #3B82F6;
    --color-primary-hover: #60A5FA;
    --color-primary-light: #1E3A5F;
    --bg-page: #0B1120;
    --bg-card: #1E293B;
    --bg-header: #0F172A;
    --text-primary: #F1F5F9;
    --text-secondary: #94A3B8;       /* 暗底对比度 ~6.5:1 ✅ */
    --text-white: #FFFFFF;
    --bubble-ai-bg: #1E293B;
    --bubble-user-bg: #2563EB;
    --border-color: #334155;
    --shadow-card: 0 2px 12px rgba(0, 0, 0, 0.4);
    --color-destructive: #EF4444;
  }

  /* 暗色下头像背景色调整 */
  .avatar-ai { background: #1E3A5F; }
  .avatar-user { background: #1E3A5F; }

  /* 暗色下输入框调整 */
  .chat-input { background: #1E293B; color: var(--text-primary); }
  .chat-input::placeholder { color: var(--text-secondary); }

  /* 暗色下预览区 */
  .preview-area { background: #1E293B; }

  /* 暗色下思考链 */
  .thinking-content {
    background: #1a1f2e;
    border: 1px solid #2a3040;
  }
  .thinking-summary { color: #8899bb; }

  /* 暗色下代码块 */
  .bubble-ai pre { background: #0B1120; }
  .bubble-ai code { background: #334155; }

  /* 暗色下错误提示 */
  .error-banner { background: #3B1A1A; color: #FCA5A5; }

  /* 暗色下已上传文件 */
  .uploaded-file { background: rgba(0,0,0,0.3); }

  /* 暗色下离线提示 */
  .offline-banner { background: #3B2E0A; border-color: #7C5A00; color: #FDE68A; }
}
