/* ========== AI Chat 样式 ========== */

/* AI 页面整体布局 */
#page-ai {
  display: flex;
  height: 100%;
  overflow: hidden;
  position: relative;
}

/* ========== 会话侧边栏 ========== */
.chat-sidebar {
  width: 240px;
  flex-shrink: 0;
  background: var(--bg);
  border-right: 1px solid var(--border-light);
  display: flex;
  flex-direction: column;
  height: 100%;
  overflow: hidden;
}

.chat-sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 14px 10px;
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
  border-bottom: 1px solid var(--border-light);
  flex-shrink: 0;
}

.new-chat-btn {
  width: 30px;
  height: 30px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--text-secondary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.15s;
}

.new-chat-btn:hover {
  border-color: var(--primary);
  color: var(--primary);
  background: var(--primary-light);
}

.chat-session-list {
  flex: 1;
  overflow-y: auto;
  padding: 6px;
}

.chat-session-list::-webkit-scrollbar {
  width: 4px;
}

.chat-session-list::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 2px;
}

.chat-session-empty {
  text-align: center;
  color: var(--text-muted);
  font-size: 12px;
  padding: 24px 0;
}

.chat-session-item {
  display: flex;
  align-items: center;
  padding: 10px 10px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background 0.15s;
  gap: 4px;
}

.chat-session-item:hover {
  background: var(--bg-hover);
}

.chat-session-item.active {
  background: var(--primary-light);
}

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

.chat-session-title-text {
  font-size: 13px;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.4;
}

.chat-session-item.active .chat-session-title-text {
  color: var(--primary);
  font-weight: 500;
}

.chat-session-time {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 2px;
}

.chat-session-delete {
  width: 24px;
  height: 24px;
  border: none;
  background: none;
  color: var(--text-muted);
  font-size: 16px;
  cursor: pointer;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: all 0.15s;
  flex-shrink: 0;
}

.chat-session-item:hover .chat-session-delete {
  opacity: 1;
}

.chat-session-delete:hover {
  background: var(--danger, #ef4444);
  color: #fff;
}

/* 侧边栏遮罩（手机端） */
.chat-sidebar-overlay {
  display: none;
}

/* ========== 聊天主区域 ========== */
.chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  height: 100%;
  overflow: hidden;
}

/* 聊天顶栏 */
.chat-header {
  display: flex;
  align-items: center;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border-light);
  gap: 10px;
  flex-shrink: 0;
  background: var(--bg);
}

.chat-sidebar-toggle {
  display: none; /* PC端隐藏 */
  width: 32px;
  height: 32px;
  border: none;
  background: none;
  font-size: 18px;
  color: var(--text-secondary);
  cursor: pointer;
  border-radius: var(--radius-sm);
  align-items: center;
  justify-content: center;
}

.chat-sidebar-toggle:hover {
  background: var(--bg-hover);
}

.chat-session-title {
  flex: 1;
  font-size: 15px;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.new-chat-btn-header {
  width: 32px;
  height: 32px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--text-secondary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.15s;
}

.new-chat-btn-header:hover {
  border-color: var(--primary);
  color: var(--primary);
  background: var(--primary-light);
}

/* 聊天容器 */
.chat-container {
  display: flex;
  flex-direction: column;
  flex: 1;
  overflow: hidden;
}

/* 消息列表 */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 0;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.chat-messages::-webkit-scrollbar {
  width: 4px;
}

.chat-messages::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 2px;
}

/* 消息行 */
.chat-message {
  display: flex;
  gap: 8px;
  padding: 0 4px;
}

.chat-message-user {
  justify-content: flex-end;
}

.chat-message-ai {
  justify-content: flex-start;
}

.chat-message-system {
  justify-content: center;
}

.chat-message-tool {
  justify-content: center;
  padding: 0 40px;
}

/* AI 头像 */
.chat-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 700;
  flex-shrink: 0;
}

/* 气泡 */
.chat-bubble {
  max-width: 80%;
  padding: 10px 14px;
  border-radius: var(--radius-lg);
  font-size: 14px;
  line-height: 1.6;
  word-break: break-word;
}

.chat-bubble-user {
  background: var(--primary);
  color: #fff;
  border-bottom-right-radius: 4px;
}

.chat-bubble-ai {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border-light);
  border-bottom-left-radius: 4px;
  box-shadow: var(--shadow-sm);
}

.chat-bubble-text {
  min-height: 1em;
}

.chat-bubble-text pre {
  background: var(--bg-secondary);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  overflow-x: auto;
  margin: 6px 0;
  font-size: 13px;
}

.chat-bubble-text code {
  font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
  font-size: 13px;
}

.chat-bubble-text .inline-code {
  background: var(--bg-secondary);
  padding: 1px 5px;
  border-radius: 3px;
}

.chat-bubble-text ul {
  padding-left: 18px;
  margin: 4px 0;
}

.chat-bubble-text li {
  margin: 2px 0;
}

.chat-bubble-text strong {
  font-weight: 600;
}

/* 系统消息 */
.chat-system-text {
  font-size: 12px;
  color: var(--text-muted);
  background: var(--bg-secondary);
  padding: 6px 14px;
  border-radius: 20px;
  max-width: 80%;
  text-align: center;
}

/* Tool 消息 */
.chat-tool-wrap {
  width: 100%;
  background: var(--bg-secondary);
  border-radius: var(--radius-sm);
  font-size: 12px;
  overflow: hidden;
}

.chat-tool-header {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  cursor: pointer;
  color: var(--text-secondary);
  user-select: none;
}

.chat-tool-header:hover {
  background: var(--bg-hover);
}

.chat-tool-icon {
  font-size: 12px;
}

.chat-tool-name {
  font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
  font-weight: 500;
}

.chat-tool-status {
  margin-left: auto;
  font-size: 11px;
}

.chat-tool-toggle {
  font-size: 10px;
  transition: transform 0.2s;
}

.chat-tool-result {
  display: none;
  padding: 8px 10px;
  border-top: 1px solid var(--border-light);
  font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
  font-size: 11px;
  color: var(--text-secondary);
  max-height: 200px;
  overflow-y: auto;
  white-space: pre-wrap;
  word-break: break-all;
}

.chat-tool-wrap.expanded .chat-tool-toggle {
  transform: rotate(90deg);
}

.chat-tool-wrap.expanded .chat-tool-result {
  display: block;
}

/* 打字动画 */
.chat-typing {
  display: flex;
  gap: 4px;
  padding: 4px 0;
}

.chat-typing span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-muted);
  animation: chatTyping 1.2s infinite;
}

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

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

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

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

/* 快捷按钮 */
.chat-shortcuts {
  display: flex;
  gap: 8px;
  padding: 0 0 8px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.chat-shortcuts::-webkit-scrollbar {
  display: none;
}

.chat-shortcut {
  flex-shrink: 0;
  padding: 5px 12px;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--bg);
  color: var(--text-secondary);
  font-size: 12px;
  cursor: pointer;
  transition: all 0.15s;
  white-space: nowrap;
}

.chat-shortcut:hover {
  border-color: var(--primary);
  color: var(--primary);
  background: var(--primary-light);
}

/* 输入行 */
.chat-input-row {
  display: flex;
  gap: 8px;
  align-items: flex-end;
}

.chat-input {
  flex: 1;
  resize: none;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 10px 14px;
  font-size: 14px;
  line-height: 1.4;
  max-height: 120px;
  min-height: 40px;
  outline: none;
  transition: border-color 0.15s;
  background: var(--bg);
  color: var(--text);
}

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

.chat-input::placeholder {
  color: var(--text-muted);
}

.chat-send-btn {
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s, opacity 0.15s;
}

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

.chat-send-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* ========== Responsive: Mobile ========== */
@media (max-width: 768px) {
  /* 侧边栏变为抽屉 */
  .chat-sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: 280px;
    z-index: 1000;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    box-shadow: none;
  }

  .chat-sidebar.open {
    transform: translateX(0);
    box-shadow: 4px 0 20px rgba(0, 0, 0, 0.15);
  }

  .chat-sidebar-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.4);
    z-index: 999;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.25s ease;
  }

  .chat-sidebar-overlay.open {
    display: block;
    opacity: 1;
    pointer-events: auto;
  }

  .chat-sidebar-toggle {
    display: flex;
  }

  .chat-container {
    height: calc(100vh - var(--topbar-height) - var(--bottomtab-height) - 24px - 46px);
    max-height: calc(100vh - var(--topbar-height) - var(--bottomtab-height) - 24px - 46px);
  }

  .chat-bubble {
    max-width: 88%;
  }

  .chat-message-tool {
    padding: 0 8px;
  }
}
