/* === 全局重置 === */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  --bg-primary: #212121;
  --bg-secondary: #171717;
  --bg-tertiary: #2f2f2f;
  --bg-hover: #3a3a3a;
  --text-primary: #ececec;
  --text-secondary: #b4b4b4;
  --text-muted: #808080;
  --accent: #10a37f;
  --accent-hover: #0e8c6b;
  --border: #424242;
  --bubble-user: #10a37f;
  --bubble-assistant: #2f2f2f;
  --shadow: 0 1px 3px rgba(0,0,0,0.3);
  --radius: 10px;
  --sidebar-width: 260px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Noto Sans', Helvetica, Arial, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  min-height: 100vh;
}

/* === 布局 === */
#app {
  display: flex;
  height: 100dvh;
}

/* === 侧边栏 === */
#sidebar {
  width: var(--sidebar-width);
  background: var(--bg-secondary);
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--border);
  flex-shrink: 0;
}

.sidebar-header {
  padding: 14px;
  border-bottom: 1px solid var(--border);
}

.sidebar-header-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  width: 100%;
}

.logo {
  font-size: 19px;
  font-weight: 700;
  margin-bottom: 10px;
  color: var(--text-primary);
}

.new-chat-btn {
  flex: 1;
  padding: 8px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: transparent;
  color: var(--text-primary);
  font-size: 13px;
  cursor: pointer;
  transition: all 0.2s;
}

.new-chat-btn:hover {
  background: var(--bg-hover);
}

/* 模型面板折叠切换按钮 */
.model-panel-toggle {
  width: 36px;
  height: 36px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.model-panel-toggle:hover {
  background: var(--bg-hover);
  border-color: var(--accent);
  color: var(--text-primary);
}

/* 模型折叠面板 */
.model-panel {
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}

.model-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 10px;
  cursor: pointer;
  transition: background 0.2s;
  user-select: none;
}

.model-panel-header:hover {
  background: var(--bg-hover);
}

.model-panel-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-primary);
}

.model-panel-arrow {
  color: var(--text-muted);
  display: flex;
  align-items: center;
  transition: transform 0.3s ease;
}

.model-panel.collapsed .model-panel-arrow {
  transform: rotate(-90deg);
}

.model-panel-body {
  padding: 6px 10px 10px;
  transition: max-height 0.3s ease, opacity 0.3s ease;
  max-height: 200px;
  opacity: 1;
  overflow: hidden;
}

.model-panel.collapsed .model-panel-body {
  max-height: 0;
  opacity: 0;
  padding-top: 0;
  padding-bottom: 0;
}

.sidebar-footer .model-select-wrapper {
  margin-top: 0;
}

/* 移动端模型切换悬浮按钮 */
.mobile-model-toggle {
  display: none;
  position: fixed;
  bottom: 100px;
  right: 16px;
  z-index: 100;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  border: none;
  background: var(--accent);
  color: white;
  cursor: grab;
  box-shadow: 0 4px 16px rgba(0,0,0,0.45);
  align-items: center;
  justify-content: center;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.mobile-model-toggle:active {
  cursor: grabbing;
}

.mobile-model-toggle.dragging {
  transition: none !important;
  transform: scale(1.1);
  box-shadow: 0 6px 24px rgba(0,0,0,0.55);
}

.mobile-model-toggle::after {
  content: '';
  position: absolute;
  top: -8px;
  left: -8px;
  right: -8px;
  bottom: -8px;
  border-radius: 50%;
}

/* 移动端响应式 */
@media (max-width: 768px) {
  :root {
    --sidebar-width: 260px;
  }

  #sidebar {
    position: fixed;
    top: 0;
    left: 0;
    height: 100vh;
    z-index: 200;
    transform: translateX(-100%);
    transition: transform 0.3s ease;
    box-shadow: 4px 0 20px rgba(0,0,0,0.5);
  }

  #sidebar.open {
    transform: translateX(0);
  }

  #sidebar.open::before {
    content: '';
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0,0,0,0.5);
    z-index: -1;
  }

  #main {
    margin-left: 0 !important;
    width: 100%;
  }

  .mobile-model-toggle {
    display: flex;
  }

  .mobile-model-bar {
    display: block;
  }

  .welcome h2 {
    font-size: 20px;
  }

  #messages {
    padding: 12px 16px;
  }

  #inputArea {
    padding: 0 16px 16px;
  }
}

/* 移动端顶部模型选择栏 */
.mobile-model-bar {
  display: none;
  flex-shrink: 0;
  padding: 8px 16px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
}

.mobile-model-bar-inner {
  display: flex;
  align-items: center;
  gap: 8px;
}

.mobile-model-bar-label {
  font-size: 13px;
  color: var(--text-muted);
  white-space: nowrap;
  flex-shrink: 0;
}

.mobile-model-bar select {
  flex: 1;
  padding: 6px 8px;
  background: var(--bg-tertiary);
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 14px;
  cursor: pointer;
  outline: none;
}

.mobile-model-bar select:focus {
  border-color: var(--accent);
}

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

.empty-history {
  color: var(--text-muted);
  font-size: 13px;
  text-align: center;
  padding: 20px;
}

/* 历史记录列表项 */
.history-item {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px;
  padding: 8px 10px;
  margin-bottom: 2px;
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.15s;
}

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

.history-item.active {
  background: rgba(16, 163, 127, 0.12);
  border-left: 2px solid var(--accent);
}

.history-item-title {
  flex: 1;
  font-size: 13px;
  color: var(--text-primary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}

.history-item-time {
  font-size: 11px;
  color: var(--text-muted);
  white-space: nowrap;
}

.history-item-delete {
  flex-shrink: 0;
  width: 20px;
  height: 20px;
  display: none;
  align-items: center;
  justify-content: center;
  border: none;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  border-radius: 4px;
  transition: background 0.15s;
  padding: 0;
}

.history-item:hover .history-item-delete {
  display: flex;
}

.history-item-delete:hover {
  background: rgba(244, 67, 54, 0.15);
  color: #f44336;
}

.sidebar-footer {
  padding: 10px 14px;
  border-top: 1px solid var(--border);
}

.model-select-wrapper select {
  width: 100%;
  padding: 6px 8px;
  background: var(--bg-tertiary);
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 14px;
  cursor: pointer;
  outline: none;
}

.model-select-wrapper select:focus {
  border-color: var(--accent);
}

.model-desc {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 2px;
  padding: 0 2px;
  min-height: 14px;
  transition: color 0.2s;
}

/* === 用户区域（侧边栏底部）=== */
.user-area {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  background: var(--bg-tertiary);
  border-radius: 8px;
  margin-bottom: 8px;
  border: 1px solid var(--border);
}

.user-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.user-name {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-primary);
}

.user-balance {
  font-size: 14px;
  font-weight: 700;
  color: var(--accent);
}

.user-btn {
  padding: 6px 12px;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 6px;
  font-size: 12px;
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
}

.user-btn:hover {
  opacity: 0.9;
}

.login-area {
  padding: 6px 10px;
  margin-bottom: 6px;
  text-align: center;
}

.login-link {
  color: var(--accent);
  text-decoration: none;
  font-size: 13px;
  font-weight: 500;
}

.login-link:hover {
  text-decoration: underline;
}

.cost-info {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid var(--border);
}

.error-message {
  color: #f44336;
  font-size: 14px;
}

/* === 主聊天区 === */
#main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}

#chatContainer {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 0;
  -webkit-overflow-scrolling: touch;
}

#messages {
  max-width: 768px;
  margin: 0 auto;
  padding: 12px 16px;
  min-height: 0;
}

/* === 欢迎页 === */
.welcome {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 55vh;
  text-align: center;
}

.welcome-icon {
  margin-bottom: 14px;
}

.welcome h2 {
  font-size: 22px;
  font-weight: 600;
  margin-bottom: 6px;
}

.welcome p {
  color: var(--text-muted);
  font-size: 14px;
}

.welcome-footer {
  font-size: 13px;
  color: var(--text-muted);
  margin-top: 8px;
}

.welcome-footer a {
  color: var(--accent);
  text-decoration: none;
}

.welcome-footer a:hover {
  text-decoration: underline;
}

/* === 消息气泡 === */
.message {
  display: flex;
  justify-content: flex-start;
  gap: 10px;
  margin-bottom: 4px;
  animation: fadeIn 0.3s ease;
}

.message:last-child {
  margin-bottom: 0;
}

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

/* 头像 - 始终在右侧 */
.message-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  align-self: flex-end;
  order: 2;
}

.message.user .message-avatar {
  background: var(--bubble-user);
}

.message.assistant .message-avatar {
  background: #555;
  order: 0;
}

/* 气泡容器 - 占满剩余空间 */
.message-content-wrapper {
  max-width: 75%;
  padding: 8px 14px;
  word-wrap: break-word;
  white-space: pre-wrap;
  line-height: 1.5;
  font-size: 15px;
  order: 1;
}

/* 用户气泡 - 绿色，右对齐 */
.message.user {
  justify-content: flex-end;
}

.message.user .message-content-wrapper {
  background: var(--bubble-user);
  color: #fff;
  border-radius: 18px 4px 18px 18px;
}

/* AI气泡 - 灰色，左对齐 */
.message.assistant .message-content-wrapper {
  background: var(--bubble-assistant);
  color: var(--text-primary);
  border-radius: 4px 18px 18px 18px;
}

/* 气泡内的段落间距 */
.message-content-wrapper p {
  margin-bottom: 8px;
}

.message-content-wrapper p:last-child {
  margin-bottom: 0;
}

/* Markdown 样式 */
.message-content-wrapper code {
  background: rgba(255,255,255,0.1);
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 13px;
  font-family: 'JetBrains Mono', 'Fira Code', monospace;
}

.message.user .message-content-wrapper code {
  background: rgba(0,0,0,0.2);
}

.message-content-wrapper pre {
  background: #1a1a1a;
  padding: 16px;
  border-radius: 8px;
  overflow-x: auto;
  margin: 12px 0;
  border: 1px solid rgba(255,255,255,0.1);
}

.message-content-wrapper pre code {
  background: transparent;
  padding: 0;
  font-size: 13px;
}

.message-content-wrapper ul,
.message-content-wrapper ol {
  padding-left: 24px;
  margin: 8px 0;
}

.message-content-wrapper blockquote {
  border-left: 3px solid var(--accent);
  padding-left: 12px;
  color: var(--text-secondary);
  margin: 8px 0;
}

.message-content-wrapper table {
  border-collapse: collapse;
  width: 100%;
  margin: 12px 0;
}

.message-content-wrapper th,
.message-content-wrapper td {
  border: 1px solid var(--border);
  padding: 8px 12px;
  text-align: left;
}

.message-content-wrapper th {
  background: var(--bg-tertiary);
  font-weight: 600;
}

/* === 消息中的图片 === */
.message-image {
  max-width: 300px;
  max-height: 200px;
  border-radius: 8px;
  margin: 8px 0;
  cursor: pointer;
}

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

@keyframes blink {
  0%, 50% { opacity: 1; }
  51%, 100% { opacity: 0; }
}

/* === 输入区 === */
#inputArea {
  padding: 0 16px 16px;
  max-width: 768px;
  margin: 0 auto;
  width: 100%;
}

#imagePreview {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: var(--bg-tertiary);
  border-radius: 8px 8px 0 0;
  margin-bottom: 0;
}

#imagePreview img {
  width: 60px;
  height: 60px;
  object-fit: cover;
  border-radius: 6px;
}

#removeImageBtn {
  background: transparent;
  border: none;
  color: var(--text-secondary);
  font-size: 18px;
  cursor: pointer;
  padding: 4px;
  line-height: 1;
}

#removeImageBtn:hover {
  color: #ff4444;
}

.input-wrapper {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 6px 10px;
  transition: border-color 0.2s;
}

.input-wrapper:focus-within {
  border-color: var(--accent);
}

.tool-btn {
  background: transparent;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 4px;
  line-height: 1;
  transition: color 0.2s;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.tool-btn:hover {
  color: var(--text-primary);
}

#messageInput {
  flex: 1;
  background: transparent;
  border: none;
  color: var(--text-primary);
  font-size: 15px;
  font-family: inherit;
  resize: none;
  outline: none;
  line-height: 1.5;
  max-height: 200px;
  min-height: 24px;
}

#messageInput::placeholder {
  color: var(--text-muted);
}

.send-btn {
  width: 36px;
  height: 36px;
  border-radius: 8px;
  border: none;
  background: var(--accent);
  color: white;
  cursor: pointer;
  transition: background 0.2s;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

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

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

.footer-note {
  text-align: center;
  color: var(--text-muted);
  font-size: 11px;
  margin-top: 6px;
}

/* === 隐藏 === */
.hidden {
  display: none !important;
}

/* === 滚动条 === */
::-webkit-scrollbar {
  width: 6px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
  background: var(--text-muted);
}
