/* ===== CSS变量定义 ===== */
:root {
  --bg-color: #f0f2f5;
  --bg-color-secondary: #ffffff;
  --text-primary: #1d2129;
  --text-secondary: #606770;
  --text-muted: #8a9199;
  --accent-color: #1877f2;
  --accent-hover: #166fe5;
  --success-color: #31a24c;
  --warning-color: #f7b928;
  --error-color: #fa383e;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.05);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.1);
  --border-radius: 12px;
  --border-radius-sm: 8px;
  --border-radius-lg: 18px;
  --transition: all 0.2s ease-in-out;
  --font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

[data-theme="dark"] {
  --bg-color: #18191a;
  --bg-color-secondary: #242526;
  --text-primary: #e4e6eb;
  --text-secondary: #b0b3b8;
  --text-muted: #8a9199;
  --accent-color: #2d88ff;
  --accent-hover: #1877f2;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.15);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.25);
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html, body {
  height: 100%;
  font-family: var(--font-family);
  background-color: var(--bg-color);
  color: var(--text-primary);
  transition: background-color 0.3s, color 0.3s;
  overflow: hidden;
}

.app-container {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* ===== 侧边栏 ===== */
.sidebar {
  width: 300px;
  background-color: var(--bg-color-secondary);
  border-right: 1px solid var(--border-color, #e0e0e0);
  display: flex;
  flex-direction: column;
  transition: var(--transition);
  padding: 20px;
}

[data-theme="dark"] .sidebar {
  border-right: 1px solid #393a3b;
}

.sidebar.collapsed {
  width: 0;
  padding: 20px 0;
  opacity: 0;
  pointer-events: none;
}

.sidebar-header {
  padding-bottom: 20px;
  border-bottom: 1px solid var(--border-color, #e0e0e0);
}
[data-theme="dark"] .sidebar-header {
  border-bottom: 1px solid #393a3b;
}

.sidebar-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.sidebar-header h3 {
  font-size: 18px;
  font-weight: 600;
}

#close-sidebar-btn {
    display: none; /* 默认隐藏 */
    background: transparent;
    border: none;
    font-size: 24px;
    cursor: pointer;
    color: var(--text-secondary);
}

.sidebar-content {
  flex: 1;
  overflow-y: auto;
  padding-top: 20px;
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.setting-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.setting-group label {
  font-size: 14px;
  font-weight: 500;
  color: var(--text-secondary);
}

.input-group {
  position: relative;
}

.input-group input,
#model-select {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border-color, #ccc);
  border-radius: var(--border-radius-sm);
  background-color: var(--bg-color);
  color: var(--text-primary);
  font-size: 14px;
  outline: none;
  transition: var(--transition);
}
[data-theme="dark"] .input-group input,
[data-theme="dark"] #model-select {
  border: 1px solid #393a3b;
}


.input-group input:focus,
#model-select:focus {
  border-color: var(--accent-color);
  box-shadow: 0 0 0 2px rgba(var(--accent-color), 0.2);
}

#model-select {
  -webkit-appearance: none;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='%238a9199' viewBox='0 0 16 16'%3E%3Cpath fill-rule='evenodd' d='M1.646 4.646a.5.5 0 0 1 .708 0L8 10.293l5.646-5.647a.5.5 0 0 1 .708.708l-6 6a.5.5 0 0 1-.708 0l-6-6a.5.5 0 0 1 0-.708z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
}

.input-group .icon-btn {
  position: absolute;
  right: 1px;
  top: 1px;
  height: calc(100% - 2px);
  width: 40px;
  background: transparent;
  border: none;
  cursor: pointer;
  color: var(--text-muted);
}

.slider-container {
  display: flex;
  align-items: center;
  gap: 12px;
}

#temperature-slider {
  flex: 1;
  height: 4px;
  background: #ddd;
  border-radius: 2px;
  outline: none;
  -webkit-appearance: none;
  appearance: none;
}
[data-theme="dark"] #temperature-slider {
  background: #444;
}

#temperature-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 16px;
  height: 16px;
  background: var(--accent-color);
  border-radius: 50%;
  cursor: pointer;
}

.setting-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.switch {
  position: relative;
  display: inline-block;
  width: 40px;
  height: 22px;
}

.switch input { display: none; }

.switch .slider {
  position: absolute;
  cursor: pointer;
  top: 0; left: 0; right: 0; bottom: 0;
  background-color: #ccc;
  transition: .4s;
  border-radius: 22px;
}

.switch .slider:before {
  position: absolute;
  content: "";
  height: 18px;
  width: 18px;
  left: 2px;
  bottom: 2px;
  background-color: white;
  transition: .4s;
  border-radius: 50%;
}

input:checked + .slider { background-color: var(--accent-color); }
input:checked + .slider:before { transform: translateX(18px); }

.sidebar-actions {
  margin-top: auto;
  padding-top: 20px;
  border-top: 1px solid var(--border-color, #e0e0e0);
  display: flex;
  flex-direction: column;
  gap: 12px;
}
[data-theme="dark"] .sidebar-actions {
  border-top: 1px solid #393a3b;
}

.btn-primary, .btn-secondary {
  padding: 10px;
  border-radius: var(--border-radius-sm);
  border: 1px solid transparent;
  cursor: pointer;
  font-weight: 600;
  transition: var(--transition);
}

.btn-primary {
  background-color: var(--accent-color);
  color: white;
}
.btn-primary:hover { background-color: var(--accent-hover); }

.btn-secondary {
  background-color: var(--bg-color);
  color: var(--text-primary);
  border: 1px solid var(--border-color, #ccc);
}
[data-theme="dark"] .btn-secondary {
  border-color: #393a3b;
}
.btn-secondary:hover { background-color: #e0e0e0; }
[data-theme="dark"] .btn-secondary:hover { background-color: #303132; }

/* ===== 主内容区 ===== */
.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  height: 100vh;
}

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 24px;
  border-bottom: 1px solid var(--border-color, #e0e0e0);
  background-color: var(--bg-color-secondary);
}
[data-theme="dark"] .header {
  border-bottom: 1px solid #393a3b;
}

.logo-section {
  display: flex;
  align-items: center;
  gap: 12px;
}

.sidebar-toggle-btn {
  width: 36px;
  height: 36px;
  border: none;
  background: transparent;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-secondary);
}
.sidebar-toggle-btn:hover { background-color: var(--bg-color); }
[data-theme="dark"] .sidebar-toggle-btn:hover { background-color: #303132; }

.logo-icon { font-size: 24px; }
.logo-section h1 { font-size: 20px; font-weight: 600; }
.status-indicator {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--success-color);
  animation: pulse 2s infinite;
}

.theme-toggle {
  width: 36px;
  height: 36px;
  border: none;
  background: transparent;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-secondary);
}
.theme-toggle:hover { background-color: var(--bg-color); }
[data-theme="dark"] .theme-toggle:hover { background-color: #303132; }
.theme-icon { font-size: 20px; }

.chat-container {
  flex: 1;
  overflow: hidden;
  position: relative;
}

.chat-wrapper {
  height: 100%;
  overflow-y: auto;
  padding: 24px;
}

.chat-box {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.message {
  display: flex;
  gap: 12px;
  max-width: 85%;
}

.message.user {
  align-self: flex-end;
  flex-direction: row-reverse;
}

.message.assistant { align-self: flex-start; }

.message-avatar .avatar-icon {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #eee;
  font-size: 18px;
}
[data-theme="dark"] .message-avatar .avatar-icon {
  background-color: #303132;
}
.message.user .message-avatar .avatar-icon { background-color: var(--accent-color); color: white; }

.message-content {
  background-color: var(--bg-color-secondary);
  border-radius: var(--border-radius-lg);
  padding: 12px 16px;
  box-shadow: var(--shadow-sm);
}

.message.user .message-content {
  background-color: var(--accent-color);
  color: white;
}

.message-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
  font-size: 12px;
  color: var(--text-muted);
}
.message.user .message-header { color: rgba(255, 255, 255, 0.8); }

.sender-name { font-weight: 600; }

.message-body {
  line-height: 1.6;
  word-wrap: break-word;
}
.message.user .message-body { color: white; }

/* Markdown 样式 */
.message-body h1, .message-body h2, .message-body h3 {
  margin-top: 1em;
  margin-bottom: 0.5em;
  font-weight: 600;
}
.message-body p { margin-bottom: 0.5em; }
.message-body ul, .message-body ol { padding-left: 20px; margin-bottom: 0.5em; }
.message-body blockquote {
  border-left: 3px solid #ccc;
  padding-left: 12px;
  margin: 1em 0;
  color: var(--text-secondary);
}
[data-theme="dark"] .message-body blockquote { border-color: #555; }
.message.user .message-body blockquote { border-color: rgba(255,255,255,0.5); color: inherit; }

.message-body pre {
  background-color: #f5f5f5;
  padding: 12px;
  border-radius: var(--border-radius-sm);
  margin: 1em 0;
  overflow-x: auto;
  font-size: 14px;
}
[data-theme="dark"] .message-body pre { background-color: #1c1e21; }
.message.user .message-body pre { background-color: rgba(0,0,0,0.2); }

.message-body code:not(pre > code) {
  background-color: #eee;
  padding: 2px 5px;
  border-radius: 4px;
  font-size: 0.9em;
}
[data-theme="dark"] .message-body code:not(pre > code) { background-color: #333; }
.message.user .message-body code:not(pre > code) { background-color: rgba(0,0,0,0.2); }

.message-images {
  margin-top: 12px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.message-image, .message-body img {
  width: 150px;
  height: 150px;
  object-fit: cover;
  border-radius: var(--border-radius-sm);
  cursor: pointer;
  transition: var(--transition);
  border: 1px solid var(--border-color, #e0e0e0);
  margin: 4px;
}

[data-theme="dark"] .message-image,
[data-theme="dark"] .message-body img {
  border-color: #393a3b;
}

.message-image:hover, .message-body img:hover {
  transform: scale(1.05);
  box-shadow: var(--shadow-md);
}

/* 输入区域 */
.input-section {
  padding: 16px 24px;
  border-top: 1px solid var(--border-color, #e0e0e0);
  background-color: var(--bg-color-secondary);
}
[data-theme="dark"] .input-section {
  border-top: 1px solid #393a3b;
}

.image-preview-container {
  padding: 0 0 12px;
  display: none;
}
.image-preview-container.active { display: block; }
.preview-wrapper { display: flex; gap: 8px; flex-wrap: wrap; }
.preview-item {
  position: relative;
  width: 60px;
  height: 60px;
  border-radius: var(--border-radius-sm);
  overflow: hidden;
}
.preview-item img { width: 100%; height: 100%; object-fit: cover; }
.remove-preview {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: rgba(0,0,0,0.6);
  color: white;
  border: none;
  cursor: pointer;
  font-size: 12px;
}

.input-container {
  display: flex;
  align-items: center;
  gap: 12px;
  background-color: var(--bg-color);
  border-radius: var(--border-radius-lg);
  padding: 8px;
}

.upload-btn, .send-btn {
  width: 36px;
  height: 36px;
  border: none;
  background: transparent;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-secondary);
  flex-shrink: 0;
}
.upload-btn:hover, .send-btn:hover { background-color: #ddd; }
[data-theme="dark"] .upload-btn:hover,
[data-theme="dark"] .send-btn:hover { background-color: #303132; }

.textarea-container {
  flex: 1;
  position: relative;
}

#message-input {
  width: 100%;
  border: none;
  background: transparent;
  resize: none;
  outline: none;
  padding: 8px;
  font-size: 15px;
  color: var(--text-primary);
  max-height: 150px;
}

.typing-indicator {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 8px 0; /* 给一点垂直空间 */
}
.typing-indicator.active { display: flex; } /* 保持flex布局 */
.typing-indicator span {
  width: 8px; height: 8px; /* 稍微调大一点 */
  background: var(--text-muted); /* 使用柔和一点的颜色 */
  border-radius: 50%;
  animation: typing 1.4s infinite ease-in-out;
}
.typing-indicator span:nth-child(1) { animation-delay: -0.32s; }
.typing-indicator span:nth-child(2) { animation-delay: -0.16s; }

.char-counter {
  text-align: right;
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 8px;
}

/* ===== 其他组件 ===== */
.lightbox {
  position: fixed; top: 0; left: 0; width: 100%; height: 100%;
  background: rgba(0, 0, 0, 0.8);
  display: none; align-items: center; justify-content: center;
  z-index: 2000;
}
.lightbox.active { display: flex; }
.lightbox-content { position: relative; max-width: 90%; max-height: 90%; }
.lightbox-content img { width: 100%; height: 100%; object-fit: contain; }
.lightbox-close {
  position: absolute; top: -40px; right: -10px;
  background: none; border: none; color: white; font-size: 30px; cursor: pointer;
}
.lightbox-nav {
  position: absolute; bottom: -40px; left: 50%;
  transform: translateX(-50%);
  display: flex; align-items: center; gap: 20px; color: white;
}
.nav-btn {
  background: rgba(255, 255, 255, 0.2); border: none; color: white;
  width: 40px; height: 40px; border-radius: 50%; cursor: pointer; font-size: 20px;
}

/* .loading-overlay 相关样式已被移除 */

.notification {
  position: fixed; top: 20px; right: 20px;
  padding: 10px 20px; border-radius: var(--border-radius-sm);
  color: white; font-weight: 500; z-index: 1001;
  box-shadow: var(--shadow-md);
}
.notification.success { background: var(--success-color); }
.notification.error { background: var(--error-color); }
.notification.warning { background: var(--warning-color); }

/* ===== 动画 ===== */
@keyframes pulse {
  50% { opacity: 0.5; }
}
@keyframes typing {
  0%, 80%, 100% { transform: scale(0.8); opacity: 0.5; }
  40% { transform: scale(1); opacity: 1; }
}
@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ===== 响应式 ===== */
@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    left: 0; top: 0;
    height: 100%;
    z-index: 100;
    transform: translateX(-100%);
    box-shadow: 2px 0 10px rgba(0,0,0,0.1);
  }
  .sidebar.collapsed {
    transform: translateX(-100%);
    box-shadow: none;
  }
  .sidebar:not(.collapsed) {
      transform: translateX(0);
  }

  #close-sidebar-btn {
      display: block; /* 在移动端显示 */
  }
  
  .main-content { width: 100%; }
}
