/* ===== 基础与变量 ===== */
* { box-sizing: border-box; margin: 0; padding: 0; }
:root {
  --bg: #f4f6fa;
  --panel: #ffffff;
  --panel-2: #fafbfd;
  --border: #e6eaf0;
  --border-strong: #d4dae3;
  --text: #1a2233;
  --muted: #6b7689;
  --muted-2: #9aa3b5;
  --primary: #4f6ef7;
  --primary-hover: #3b5ae6;
  --primary-soft: #eef1fe;
  --danger: #ef4444;
  --danger-soft: #fee2e2;
  --success: #22c55e;
  --warning: #f59e0b;
  --user-bubble: #4f6ef7;
  --bot-bubble: #ffffff;
  --code-bg: #f0f2f5;
  --sidebar-w: 248px;
  --shadow: 0 1px 3px rgba(20,30,60,.06), 0 1px 2px rgba(20,30,60,.04);
  --shadow-lg: 0 10px 30px rgba(20,30,60,.12);
  --radius: 10px;
}
html, body { height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}
button { font-family: inherit; cursor: pointer; }
input, textarea, select { font-family: inherit; font-size: 14px; }
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-thumb { background: #cfd6e2; border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: #b8c2d2; }

/* ===== 应用布局 ===== */
.app {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  height: 100vh;
  overflow: hidden;
}

/* ===== 侧边栏 ===== */
.sidebar {
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.sidebar-head {
  padding: 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 10px;
}
.sidebar-head .logo {
  width: 32px; height: 32px;
  border-radius: 8px;
  background: linear-gradient(135deg, var(--primary), #7c3aed);
  color: #fff;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 15px;
}
.sidebar-head .title { font-weight: 600; font-size: 15px; }
.sidebar-head .sub { font-size: 11px; color: var(--muted); }

.nav { padding: 8px; display: flex; flex-direction: column; gap: 2px; }
.nav-item {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 12px;
  border: none; background: transparent;
  border-radius: 8px;
  color: var(--text);
  font-size: 14px;
  text-align: left;
  width: 100%;
  transition: background .15s;
}
.nav-item:hover { background: var(--panel-2); }
.nav-item.active { background: var(--primary-soft); color: var(--primary); font-weight: 500; }
.nav-item .icon { font-size: 16px; width: 18px; text-align: center; }

.sidebar-foot {
  margin-top: auto;
  padding: 10px 8px;
  border-top: 1px solid var(--border);
}
.new-chat-btn {
  width: 100%;
  padding: 10px;
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  display: flex; align-items: center; justify-content: center; gap: 6px;
}
.new-chat-btn:hover { background: var(--primary-hover); }

/* 会话列表 */
.session-list {
  flex: 1;
  overflow-y: auto;
  padding: 4px 8px;
}
.session-list .label {
  font-size: 11px; color: var(--muted-2);
  padding: 8px 12px 4px;
  text-transform: uppercase; letter-spacing: .5px;
}
.session-item {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 10px;
  border-radius: 8px;
  cursor: pointer;
  color: var(--text);
  font-size: 13px;
  transition: background .15s;
  position: relative;
}
.session-item:hover { background: var(--panel-2); }
.session-item.active { background: var(--primary-soft); color: var(--primary); }
.session-item .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.session-item .del {
  opacity: 0; border: none; background: transparent; color: var(--muted);
  font-size: 14px; padding: 2px 4px; border-radius: 4px;
}
.session-item:hover .del { opacity: 1; }
.session-item .del:hover { color: var(--danger); background: var(--danger-soft); }

/* ===== 主内容区 ===== */
.main { display: flex; flex-direction: column; overflow: hidden; min-width: 0; }
.topbar {
  height: 52px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: 12px;
  padding: 0 16px;
  flex-shrink: 0;
}
.topbar .menu-btn {
  display: none;
  border: none; background: transparent; font-size: 20px; color: var(--text);
}
.topbar .page-title { font-weight: 600; font-size: 15px; flex: 1; }
.topbar .status-pill {
  font-size: 11px; padding: 3px 10px; border-radius: 20px;
  display: flex; align-items: center; gap: 5px;
}
.status-pill.ok { background: #dcfce7; color: #15803d; }
.status-pill.warn { background: #fef3c7; color: #b45309; }
.status-pill.err { background: var(--danger-soft); color: var(--danger); }
.status-pill .dot { width: 6px; height: 6px; border-radius: 50%; background: currentColor; }

.content { flex: 1; overflow: hidden; position: relative; }
.page { display: none; height: 100%; overflow-y: auto; }
.page.active { display: block; }

/* ===== 聊天页 ===== */
.chat-page { display: none; height: 100%; flex-direction: column; }
.chat-page.active { display: flex; }
.chat-messages {
  flex: 1; overflow-y: auto;
  padding: 24px 16px 12px;
}
.messages-inner { max-width: 820px; margin: 0 auto; display: flex; flex-direction: column; gap: 18px; }

.msg { display: flex; gap: 12px; align-items: flex-start; }
.msg .avatar {
  width: 32px; height: 32px; border-radius: 8px; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 13px; font-weight: 600; color: #fff;
}
.msg.user .avatar { background: var(--primary); }
.msg.bot .avatar { background: #22c55e; }
.msg .msg-content { flex: 1; min-width: 0; }
.msg .msg-time {
  font-size: 11px;
  color: var(--muted-2);
  margin-bottom: 4px;
  margin-left: 2px;
}
.bubble {
  max-width: calc(100% - 48px);
  padding: 12px 14px;
  border-radius: 12px;
  line-height: 1.7;
  font-size: 14px;
  white-space: pre-wrap; word-break: break-word;
  position: relative;
}
.bubble .copy-btn {
  position: absolute;
  top: 6px;
  right: 6px;
  opacity: 0;
  border: none;
  background: rgba(0,0,0,0.1);
  color: var(--muted);
  font-size: 12px;
  padding: 3px 6px;
  border-radius: 4px;
  cursor: pointer;
  transition: opacity 0.15s;
}
.bubble:hover .copy-btn {
  opacity: 1;
}
.bubble .copy-btn:hover {
  background: rgba(0,0,0,0.2);
  color: var(--text);
}
.msg.user .bubble { background: var(--user-bubble); color: #fff; border-top-left-radius: 4px; }
.msg.bot .bubble { background: var(--bot-bubble); border: 1px solid var(--border); border-top-left-radius: 4px; }
.msg.bot .bubble p { margin: 0 0 8px; }
.msg.bot .bubble p:last-child { margin-bottom: 0; }
.msg.bot .bubble .md-h { margin: 10px 0 6px; font-weight: 600; line-height: 1.4; }
.msg.bot .bubble .md-h:first-child { margin-top: 0; }
.msg.bot .bubble h2.md-h { font-size: 16px; }
.msg.bot .bubble h3.md-h { font-size: 15px; }
.msg.bot .bubble h4.md-h { font-size: 14px; }
.msg.bot .bubble .md-code {
  background: var(--code-bg); border-radius: 6px; padding: 10px 12px;
  overflow-x: auto; margin: 8px 0; font-size: 12px; line-height: 1.5;
}
.msg.bot .bubble .md-code code { font-family: "SF Mono","Menlo","Consolas",monospace; }
.msg.bot .bubble .md-inline {
  background: var(--code-bg); border-radius: 3px; padding: 1px 5px;
  font-family: "SF Mono","Menlo","Consolas",monospace; font-size: 13px;
}
.msg.bot .bubble .md-ul, .msg.bot .bubble .md-ol { margin: 6px 0; padding-left: 22px; }
.msg.bot .bubble .md-ul li, .msg.bot .bubble .md-ol li { margin: 2px 0; }
.msg.bot .bubble a { color: var(--primary); text-decoration: underline; }
.msg.bot .bubble strong { font-weight: 600; }

.sources-box {
  max-width: 820px; margin: 0 auto 18px;
  border: 1px dashed var(--border-strong); border-radius: 8px;
  background: var(--panel-2);
}
.sources-box summary {
  padding: 10px 14px; font-size: 13px; color: var(--muted); cursor: pointer;
  list-style: none;
  display: flex;
  align-items: center;
  gap: 8px;
}
.sources-box summary:hover {
  background: rgba(0,0,0,0.02);
}
.sources-box .src-body { padding: 10px 14px 14px; display: flex; flex-direction: column; gap: 10px; }
.sources-box .src-item {
  background: var(--code-bg); border-radius: 8px;
  padding: 10px 12px;
  position: relative;
}
.sources-box .src-num {
  display: inline-block;
  background: var(--primary);
  color: #fff;
  font-size: 11px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 10px;
  margin-right: 8px;
}
.sources-box .src-copy-btn {
  position: absolute;
  top: 8px;
  right: 8px;
  font-size: 11px;
  color: var(--muted);
  cursor: pointer;
  padding: 2px 6px;
  border-radius: 4px;
  background: rgba(0,0,0,0.05);
}
.sources-box .src-copy-btn:hover {
  background: var(--primary);
  color: #fff;
}
.sources-box .src-text {
  font-size: 12px;
  color: var(--muted);
  white-space: pre-wrap;
  line-height: 1.6;
  margin-top: 6px;
}

.typing-cursor {
  display: inline-block; width: 7px; height: 15px;
  background: var(--primary); margin-left: 2px; vertical-align: middle;
  animation: blink 1s infinite;
}
@keyframes blink { 0%,50%{opacity:1} 51%,100%{opacity:0} }

.chat-empty {
  flex: 1; display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  color: var(--muted); text-align: center; padding: 40px 20px;
}
.chat-empty .big { font-size: 42px; margin-bottom: 14px; }
.chat-empty h2 { font-size: 18px; color: var(--text); margin-bottom: 6px; font-weight: 500; }
.chat-empty p { font-size: 13px; max-width: 360px; line-height: 1.7; }

/* 输入区 */
.chat-input {
  border-top: 1px solid var(--border);
  background: var(--panel);
  padding: 12px 16px;
  flex-shrink: 0;
}
.input-inner { max-width: 820px; margin: 0 auto; display: flex; gap: 10px; align-items: flex-end; }
.input-inner textarea {
  flex: 1; border: 1px solid var(--border); border-radius: 12px;
  padding: 11px 14px; resize: none; max-height: 140px; outline: none;
  line-height: 1.5; background: var(--panel); transition: border-color .15s;
}
.input-inner textarea:focus { border-color: var(--primary); }
.btn-send {
  background: var(--primary); color: #fff; border: none;
  border-radius: 12px; padding: 11px 22px; font-size: 14px; font-weight: 500;
  white-space: nowrap; transition: background .15s;
}
.btn-send:hover:not(:disabled) { background: var(--primary-hover); }
.btn-send:disabled { background: #b8c2d9; cursor: not-allowed; }
.btn-stop { background: var(--danger); }
.btn-stop:hover { background: #dc2626; }
.input-hint { max-width: 820px; margin: 6px auto 0; font-size: 11px; color: var(--muted-2); text-align: center; }

/* ===== 通用面板（知识库/设置/日志/仪表盘） ===== */
.panel-pad { padding: 24px; }
.panel-inner { max-width: 960px; margin: 0 auto; }
.section-title {
  font-size: 16px; font-weight: 600; margin-bottom: 4px;
}
.section-desc { font-size: 13px; color: var(--muted); margin-bottom: 16px; }

.card {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 18px; margin-bottom: 16px;
  box-shadow: var(--shadow);
}
.card h3 { font-size: 14px; font-weight: 600; margin-bottom: 12px; display: flex; align-items: center; gap: 8px; }

/* 表单 */
.form-row { margin-bottom: 14px; }
.form-row:last-child { margin-bottom: 0; }
.form-row label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 5px; font-weight: 500; }
.form-row label .req { color: var(--danger); }
.form-row input, .form-row textarea, .form-row select {
  width: 100%; border: 1px solid var(--border); border-radius: 8px;
  padding: 9px 12px; outline: none; transition: border-color .15s; background: var(--panel);
}
.form-row input:focus, .form-row textarea:focus, .form-row select:focus { border-color: var(--primary); }
.form-row textarea { resize: vertical; min-height: 80px; line-height: 1.5; }
.form-row .hint { font-size: 11px; color: var(--muted-2); margin-top: 4px; }
.form-grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

.btn {
  padding: 8px 16px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--panel); color: var(--text); font-size: 13px; font-weight: 500;
  transition: all .15s; display: inline-flex; align-items: center; gap: 6px;
}
.btn:hover { border-color: var(--primary); color: var(--primary); }
.btn.primary { background: var(--primary); color: #fff; border-color: var(--primary); }
.btn.primary:hover { background: var(--primary-hover); color: #fff; }
.btn.danger { color: var(--danger); border-color: var(--border); }
.btn.danger:hover { background: var(--danger-soft); border-color: var(--danger); }
.btn.small { padding: 5px 10px; font-size: 12px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }

/* 仪表盘统计卡 */
.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px,1fr)); gap: 14px; margin-bottom: 18px; }
.stat-card {
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; box-shadow: var(--shadow);
}
.stat-card .label { font-size: 12px; color: var(--muted); margin-bottom: 6px; }
.stat-card .value { font-size: 26px; font-weight: 700; }
.stat-card .value .unit { font-size: 13px; color: var(--muted); font-weight: 400; margin-left: 3px; }
.stat-card.ok .value { color: var(--success); }
.stat-card.warn .value { color: var(--warning); }
.stat-card.err .value { color: var(--danger); }

/* 文件列表 */
.file-table { width: 100%; border-collapse: collapse; }
.file-table th, .file-table td { text-align: left; padding: 10px 8px; border-bottom: 1px solid var(--border); font-size: 13px; }
.file-table th { color: var(--muted); font-weight: 500; font-size: 12px; }
.file-table tr:hover { background: var(--panel-2); }
.file-table .actions { display: flex; gap: 6px; justify-content: flex-end; }

.empty-state { text-align: center; padding: 40px 20px; color: var(--muted); }
.empty-state .icon { font-size: 36px; margin-bottom: 10px; }

/* 拖拽上传 */
#knowledgePage.drop-active .panel-inner,
#fileContent.drop-active {
  outline: 2px dashed var(--primary);
  outline-offset: -4px;
  background: rgba(59,130,246,0.06);
}
#fileContent.drop-active {
  outline-offset: -2px;
}

/* 日志 */
.log-list { font-family: "SF Mono", "Menlo", "Consolas", monospace; font-size: 12px; }
.log-line {
  padding: 6px 10px; border-bottom: 1px solid var(--border);
  display: flex; gap: 10px; align-items: flex-start;
}
.log-line:hover { background: var(--panel-2); }
.log-line .time { color: var(--muted-2); flex-shrink: 0; }
.log-line .level { flex-shrink: 0; width: 50px; font-weight: 600; }
.log-line .level.INFO { color: var(--primary); }
.log-line .level.WARNING { color: var(--warning); }
.log-line .level.ERROR { color: var(--danger); }
.log-line .msg { flex: 1; word-break: break-all; white-space: pre-wrap; }

/* 模态框 */
.modal-mask {
  position: fixed; inset: 0; background: rgba(15,22,40,.45);
  display: none; align-items: center; justify-content: center; z-index: 100;
  padding: 16px;
}
.modal-mask.show { display: flex; }
.modal {
  background: var(--panel); border-radius: 12px; width: 100%; max-width: 560px;
  max-height: 88vh; display: flex; flex-direction: column; box-shadow: var(--shadow-lg);
}
.modal-head { padding: 14px 18px; border-bottom: 1px solid var(--border); display: flex; align-items: center; justify-content: space-between; }
.modal-head h3 { font-size: 15px; font-weight: 600; }
.modal-head .close { border: none; background: transparent; font-size: 20px; color: var(--muted); line-height: 1; }
.modal-body { padding: 18px; overflow-y: auto; flex: 1; }
.modal-foot { padding: 12px 18px; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: 8px; }

/* Toast */
.toast-wrap { position: fixed; top: 16px; right: 16px; z-index: 200; display: flex; flex-direction: column; gap: 8px; }
.toast {
  padding: 10px 16px; border-radius: 8px; background: var(--text); color: #fff;
  font-size: 13px; box-shadow: var(--shadow-lg); animation: slidein .25s;
  max-width: 320px;
}
.toast.success { background: var(--success); }
.toast.error { background: var(--danger); }
.toast.warn { background: var(--warning); }
@keyframes slidein { from { transform: translateX(20px); opacity: 0; } to { transform: none; opacity: 1; } }

/* 遮罩（手机端抽屉） */
.drawer-mask {
  position: fixed; inset: 0; background: rgba(15,22,40,.4);
  display: none; z-index: 50;
}
.drawer-mask.show { display: block; }

/* ===== 登录页 ===== */
.login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}
.login-card {
  background: #fff;
  border-radius: 16px;
  padding: 40px 36px;
  width: 100%;
  max-width: 400px;
  box-shadow: 0 20px 60px rgba(0,0,0,.15);
}
.login-logo {
  text-align: center;
  margin-bottom: 28px;
}
.login-logo .logo-icon {
  width: 56px;
  height: 56px;
  border-radius: 14px;
  background: linear-gradient(135deg, var(--primary), #7c3aed);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 22px;
  margin: 0 auto 14px;
}
.login-logo h1 {
  font-size: 22px;
  font-weight: 700;
  margin-bottom: 4px;
}
.login-logo p {
  font-size: 13px;
  color: var(--muted);
}
.login-form .form-row { margin-bottom: 16px; }
.login-form .form-row label { font-size: 13px; }
.login-form .form-row input {
  padding: 11px 14px;
  font-size: 14px;
}
.btn.full { width: 100%; justify-content: center; padding: 12px; font-size: 15px; }
.login-hint {
  text-align: center;
  font-size: 12px;
  color: var(--muted-2);
  margin-top: 14px;
}

/* ===== 用户菜单 ===== */
.topbar-right {
  display: flex;
  align-items: center;
  gap: 12px;
}
.user-menu {
  position: relative;
}
.user-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 10px 4px 4px;
  border: 1px solid var(--border);
  border-radius: 20px;
  background: var(--panel);
  transition: border-color .15s;
}
.user-btn:hover { border-color: var(--primary); }
.user-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--primary), #7c3aed);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 600;
}
.user-name {
  font-size: 13px;
  font-weight: 500;
  max-width: 100px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.user-btn .arrow {
  font-size: 10px;
  color: var(--muted);
}
.user-dropdown {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow-lg);
  min-width: 180px;
  display: none;
  z-index: 80;
  overflow: hidden;
}
.user-dropdown.show { display: block; }
.user-info {
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
}
.user-info .name {
  font-weight: 600;
  font-size: 14px;
  margin-bottom: 2px;
}
.user-info .role {
  font-size: 12px;
  color: var(--muted);
}
.dropdown-divider {
  height: 1px;
  background: var(--border);
}
.dropdown-item {
  display: block;
  width: 100%;
  padding: 10px 14px;
  border: none;
  background: transparent;
  text-align: left;
  font-size: 13px;
  color: var(--text);
  cursor: pointer;
  transition: background .15s;
}
.dropdown-item:hover { background: var(--panel-2); }

/* ===== 用户管理 ===== */
.role-badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
}
.role-badge.admin {
  background: #fef3c7;
  color: #b45309;
}
.role-badge.user {
  background: #dbeafe;
  color: #1d4ed8;
}
.status-badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
}
.status-badge.active {
  background: #dcfce7;
  color: #15803d;
}
.status-badge.inactive {
  background: #f1f5f9;
  color: #64748b;
}
.file-table code {
  background: var(--code-bg);
  padding: 2px 6px;
  border-radius: 4px;
  font-family: "SF Mono", "Menlo", "Consolas", monospace;
  font-size: 12px;
}

/* ===== 响应式：手机 ===== */
@media (max-width: 768px) {
  .app { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; left: 0; top: 0; bottom: 0;
    width: var(--sidebar-w); z-index: 60;
    transform: translateX(-100%); transition: transform .25s;
  }
  .app.drawer-open .sidebar { transform: none; }
  .topbar .menu-btn { display: block; }
  .panel-pad { padding: 16px; }
  .form-grid-2 { grid-template-columns: 1fr; }
  .messages-inner { gap: 14px; }
  .chat-messages { padding: 16px 12px 8px; }
  .chat-input { padding: 10px 12px; }
  .btn-send { padding: 11px 16px; }
  .user-name { display: none; }
  .login-card { padding: 32px 24px; }
  .file-table th:nth-child(5), .file-table td:nth-child(5) { display: none; }
}
