/* ===== 基础 ===== */
* { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --primary: #4f46e5;
  --primary-dark: #4338ca;
  --primary-light: #eef2ff;
  --danger: #dc2626;
  --success: #16a34a;
  --text: #1f2937;
  --text-light: #6b7280;
  --border: #e5e7eb;
  --bg: #f3f4f6;
  --card-bg: #ffffff;
  --radius: 10px;
}

body {
  font-family: "Microsoft YaHei", "PingFang SC", "Segoe UI", system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
}

.app {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 16px 40px;
}

/* ===== 顶部栏 ===== */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
  padding: 16px 0 12px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 20px;
}

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

.brand .logo { font-size: 28px; }
.brand h1 { font-size: 20px; font-weight: 700; }

.badge {
  background: var(--primary-light);
  color: var(--primary-dark);
  font-size: 12px;
  padding: 2px 10px;
  border-radius: 20px;
  font-weight: 600;
}

.tabs {
  display: flex;
  gap: 6px;
  background: #e5e7eb;
  padding: 4px;
  border-radius: 10px;
}

.tab-btn {
  border: none;
  background: transparent;
  padding: 8px 18px;
  border-radius: 8px;
  font-size: 14px;
  cursor: pointer;
  color: var(--text-light);
  font-weight: 500;
  transition: all .15s;
}

.tab-btn:hover { color: var(--text); }
.tab-btn.active {
  background: var(--card-bg);
  color: var(--primary);
  box-shadow: 0 1px 3px rgba(0,0,0,.1);
  font-weight: 600;
}

/* ===== 面板 ===== */
.panel {
  background: var(--card-bg);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 16px;
  box-shadow: 0 1px 3px rgba(0,0,0,.06);
  border: 1px solid var(--border);
}

.panel-header { margin-bottom: 14px; }
.panel-header h2 { font-size: 16px; font-weight: 700; margin-bottom: 4px; }
.hint { font-size: 13px; color: var(--text-light); }
.row-between { display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 8px; }

/* ===== Tab 内容 ===== */
.tab-content { display: none; }
.tab-content.active { display: block; }

/* ===== 上传区 ===== */
.upload-zone {
  border: 2px dashed #c7cbe8;
  border-radius: var(--radius);
  padding: 36px 20px;
  text-align: center;
  background: #fafaff;
  transition: all .2s;
  cursor: pointer;
}
.upload-zone.dragover {
  border-color: var(--primary);
  background: var(--primary-light);
}
.upload-icon { font-size: 40px; margin-bottom: 8px; }
.upload-zone p { margin: 6px 0; }

.upload-progress { margin-top: 12px; }
.progress-bar {
  height: 8px;
  background: #e5e7eb;
  border-radius: 4px;
  overflow: hidden;
}
.progress-fill {
  height: 100%;
  width: 0%;
  background: var(--primary);
  border-radius: 4px;
  transition: width .3s;
}

/* ===== 按钮 ===== */
.btn {
  border: none;
  border-radius: 8px;
  padding: 8px 16px;
  font-size: 14px;
  cursor: pointer;
  font-weight: 500;
  transition: all .15s;
  font-family: inherit;
}
.btn-primary {
  background: var(--primary);
  color: #fff;
}
.btn-primary:hover { background: var(--primary-dark); }
.btn-ghost {
  background: #f3f4f6;
  color: var(--text);
  border: 1px solid var(--border);
}
.btn-ghost:hover { background: #e5e7eb; }
.btn-danger { background: #fee2e2; color: var(--danger); }
.btn-danger:hover { background: #fecaca; }
.btn-lg { padding: 12px 28px; font-size: 15px; }
.btn-sm { padding: 4px 10px; font-size: 12px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* ===== 表单 ===== */
.manual-form { display: flex; flex-direction: column; gap: 10px; }
.manual-form input, .manual-form textarea,
.settings-form input, .settings-form select,
.search-input, .qa-message, .qa-extra {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 14px;
  font-family: inherit;
  resize: vertical;
  background: #fff;
  color: var(--text);
}
.manual-form input:focus, .manual-form textarea:focus,
.settings-form input:focus, .search-input:focus,
.qa-message:focus, .qa-extra:focus {
  outline: 2px solid var(--primary);
  outline-offset: -1px;
  border-color: transparent;
}
.form-actions { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }

/* ===== 知识库列表 ===== */
.list-tools { display: flex; gap: 8px; align-items: center; }
.search-input { width: 220px; }

.kb-item {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px 14px;
  margin-bottom: 8px;
  transition: background .15s;
}
.kb-item:hover { background: #f9fafb; }
.kb-item-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  cursor: pointer;
}
.kb-item-title {
  font-weight: 600;
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.kb-item-meta {
  font-size: 12px;
  color: var(--text-light);
  display: flex;
  gap: 10px;
  flex-shrink: 0;
  align-items: center;
}
.kb-item-actions { display: flex; gap: 6px; flex-shrink: 0; }
.kb-item-body {
  display: none;
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px dashed var(--border);
  font-size: 13px;
  color: var(--text);
  background: #fafafa;
  border-radius: 6px;
  padding: 10px;
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 300px;
  overflow-y: auto;
}
.kb-item.open .kb-item-body { display: block; }
.kb-item.open .expand-arrow { transform: rotate(90deg); }
.expand-arrow { display: inline-block; transition: transform .15s; margin-right: 6px; font-size: 11px; }

.tag {
  display: inline-block;
  background: #f0fdf4;
  color: #166534;
  font-size: 11px;
  padding: 1px 8px;
  border-radius: 10px;
  margin-right: 4px;
}

.empty-tip {
  text-align: center;
  color: var(--text-light);
  padding: 40px 20px;
  font-size: 14px;
}

.kb-list { margin-top: 8px; }

/* ===== 分类筛选 ===== */
.category-filter {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 12px;
}
.cat-btn {
  border: 1px solid var(--border);
  background: #fff;
  color: var(--text-light);
  border-radius: 20px;
  padding: 5px 14px;
  font-size: 13px;
  cursor: pointer;
  transition: all .15s;
  font-family: inherit;
}
.cat-btn:hover {
  border-color: var(--primary);
  color: var(--primary);
}
.cat-btn.active {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
  font-weight: 600;
}
.cat-tag {
  display: inline-block;
  background: var(--primary-light);
  color: var(--primary-dark);
  font-size: 11px;
  padding: 1px 8px;
  border-radius: 10px;
  margin-right: 4px;
  font-weight: 600;
}

/* ===== 问答布局 ===== */
.qa-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
  padding: 12px 16px;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 16px;
}
.qa-toolbar-left, .qa-toolbar-right {
  display: flex;
  align-items: center;
  gap: 16px;
}

.qa-layout {
  display: grid;
  grid-template-columns: 1fr 360px;
  gap: 16px;
  align-items: start;
}

/* 聊天区 */
.chat-area {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  height: calc(100vh - 260px);
  min-height: 420px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.chat-welcome {
  text-align: center;
  color: var(--text-light);
  padding: 60px 20px;
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
}
.chat-welcome-icon { font-size: 48px; }

.msg {
  display: flex;
  flex-direction: column;
  max-width: 85%;
}
.msg.user { align-self: flex-end; align-items: flex-end; }
.msg.ai { align-self: flex-start; align-items: flex-start; }
.msg-sender {
  font-size: 11px;
  color: var(--text-light);
  margin-bottom: 4px;
  padding: 0 4px;
}
.msg-bubble {
  padding: 10px 14px;
  border-radius: 12px;
  font-size: 14px;
  line-height: 1.7;
  white-space: pre-wrap;
  word-break: break-word;
}
.msg.user .msg-bubble {
  background: var(--primary);
  color: #fff;
  border-bottom-right-radius: 4px;
}
.msg.ai .msg-bubble {
  background: #f3f4f6;
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}
.msg.ai.thinking .msg-bubble { color: var(--text-light); font-style: italic; }
.msg-copy {
  margin-top: 4px;
  font-size: 11px;
  background: none;
  border: none;
  color: var(--primary);
  cursor: pointer;
  padding: 2px 6px;
}
.msg-copy:hover { text-decoration: underline; }
.msg-model {
  font-size: 11px;
  color: var(--text-light);
  margin-top: 2px;
  padding: 0 4px;
}

/* 输入栏 */
.chat-input-bar {
  display: flex;
  gap: 10px;
  padding: 12px;
  border-top: 1px solid var(--border);
  background: #fafafa;
  align-items: flex-end;
}
.chat-input-extra {
  display: flex;
  gap: 8px;
  align-items: flex-end;
  flex-shrink: 0;
}
.chat-input {
  flex: 1;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 14px;
  font-family: inherit;
  resize: none;
  background: #fff;
}
.chat-input:focus { outline: 2px solid var(--primary); outline-offset: -1px; border-color: transparent; }
.btn-send { align-self: flex-end; padding: 10px 24px; }

/* 图片预览 */
.image-preview-container {
  padding: 8px 12px 0;
  background: #fafafa;
  border-top: 1px dashed var(--border);
}
.image-preview {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px;
  background: #fff;
}
.image-preview img {
  max-height: 80px;
  max-width: 160px;
  border-radius: 4px;
  object-fit: contain;
}
.image-remove {
  padding: 2px 8px;
  font-size: 12px;
  color: var(--danger);
}
.msg-img {
  max-width: 240px;
  max-height: 180px;
  border-radius: 8px;
  margin-top: 6px;
  object-fit: contain;
  display: block;
}

/* 右侧知识库侧边栏 */
.kb-sidebar {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
}
.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}
.sidebar-header h3 { font-size: 14px; font-weight: 700; }
.empty-tip-sm {
  color: var(--text-light);
  font-size: 12px;
  text-align: center;
  padding: 20px 10px;
}

.qa-message { min-height: 160px; }
.qa-extra { width: 100%; border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; font-size: 13px; font-family: inherit; resize: vertical; }

.qa-controls {
  display: flex;
  align-items: center;
  gap: 14px;
  flex-wrap: wrap;
}
.select-label {
  font-size: 13px;
  color: var(--text-light);
  display: flex;
  align-items: center;
  gap: 6px;
}
.select-label select {
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 8px;
  font-size: 13px;
  background: #fff;
}
.checkbox-label {
  font-size: 13px;
  display: flex;
  align-items: center;
  gap: 5px;
  color: var(--text);
  cursor: pointer;
}

.matched-list { display: flex; flex-direction: column; gap: 8px; max-height: 300px; overflow-y: auto; }
.matched-item {
  border: 1px solid var(--border);
  border-left: 4px solid var(--primary);
  border-radius: 6px;
  padding: 8px 10px;
  font-size: 12px;
  background: #fff;
}
.matched-item.manual {
  border-left-color: #f59e0b;
}
.matched-item-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  margin-bottom: 4px;
}
.matched-item-title { font-weight: 600; font-size: 12px; }
.matched-item-content {
  color: var(--text-light);
  max-height: 60px;
  overflow-y: auto;
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 11px;
}
.matched-item input[type="checkbox"] { cursor: pointer; }

.reply-box {
  background: #f8fafc;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 14px;
  min-height: 200px;
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 14px;
  line-height: 1.7;
}
.reply-actions { display: flex; gap: 6px; }
.reply-loading {
  color: var(--primary);
  font-weight: 500;
  animation: pulse 1.2s infinite;
}
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: .4; } }

/* ===== 设置 ===== */
.settings-layout {
  display: grid;
  grid-template-columns: 1.2fr 1fr;
  gap: 16px;
  align-items: start;
}
.settings-form { display: flex; flex-direction: column; gap: 14px; }
.form-group { display: flex; flex-direction: column; gap: 5px; position: relative; }
.form-group label { font-size: 13px; color: var(--text-light); font-weight: 500; }
.form-group input { padding: 9px 12px; }
.form-group #set-apikey { padding-right: 70px; }
#btn-toggle-key {
  position: absolute;
  right: 6px;
  top: 30px;
}
.test-result {
  margin-top: 10px;
  padding: 10px 14px;
  border-radius: 8px;
  font-size: 13px;
  display: none;
}
.test-result.ok {
  display: block;
  background: #f0fdf4;
  color: #166534;
  border: 1px solid #bbf7d0;
}
.test-result.err {
  display: block;
  background: #fef2f2;
  color: #991b1b;
  border: 1px solid #fecaca;
}

.quickref ol, .quickref ul { margin: 6px 0 10px 20px; font-size: 13px; color: var(--text); }
.quickref li { margin-bottom: 4px; }
.quickref h3 { font-size: 14px; margin-top: 10px; color: var(--text); }

/* ===== Toast ===== */
.toast {
  position: fixed;
  top: 20px;
  right: 20px;
  background: #111827;
  color: #fff;
  padding: 12px 20px;
  border-radius: 8px;
  font-size: 14px;
  z-index: 9999;
  opacity: 0;
  transform: translateY(-10px);
  transition: all .25s;
  pointer-events: none;
  max-width: 400px;
  box-shadow: 0 4px 12px rgba(0,0,0,.2);
}
.toast.show { opacity: 1; transform: translateY(0); }
.toast.success { background: #16a34a; }
.toast.error { background: #dc2626; }

/* ===== 底部 ===== */
.footer {
  text-align: center;
  color: var(--text-light);
  font-size: 12px;
  padding: 20px 0;
}

/* ===== 响应式 ===== */
@media (max-width: 900px) {
  .qa-layout, .settings-layout { grid-template-columns: 1fr; }
  .topbar { flex-direction: column; align-items: flex-start; }
  .search-input { width: 100%; }
  .list-tools { width: 100%; }
  .chat-area { height: 60vh; }
}

/* ===== 弹窗 Modal ===== */
.modal[hidden] {
  display: none !important;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
}
.modal-overlay {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,.4);
}
.modal-content {
  position: relative;
  background: var(--card-bg);
  border-radius: 12px;
  width: 90%;
  max-width: 700px;
  max-height: 85vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 8px 32px rgba(0,0,0,.2);
}
.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}
.modal-header h3 { font-size: 16px; font-weight: 700; }
.modal-body {
  padding: 16px 20px;
  overflow-y: auto;
  flex: 1;
}
.modal-body .form-group { margin-bottom: 12px; }
.modal-body .form-group label { font-size: 13px; color: var(--text-light); display: block; margin-bottom: 4px; }
.modal-body input, .modal-body textarea {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 14px;
  font-family: inherit;
}
.modal-body textarea { resize: vertical; }
.modal-footer {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  padding: 12px 20px;
  border-top: 1px solid var(--border);
}