/* ===== 全局 ===== */
:root {
  --bg: #f5f7fa;
  --surface: #fff;
  --primary: #4f7cff;
  --primary-dim: #3a62d6;
  --text: #1f2937;
  --muted: #6b7280;
  --border: #e5e7eb;
  --shadow: 0 4px 20px rgba(0,0,0,0.06);
  --radius: 14px;
  --red: #ef4444;
  --orange: #f59e0b;
  --green: #22c55e;
  --header-h: 56px;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans SC", sans-serif; color: var(--text); background: var(--bg); -webkit-text-size-adjust: 100%; -webkit-tap-highlight-color: transparent; }
button { font-family: inherit; cursor: pointer; touch-action: manipulation; -webkit-tap-highlight-color: transparent; }
input, textarea, select { font-family: inherit; }
.hidden { display: none !important; }
/* 防止移动端横向溢出 */
body { overflow-x: hidden; overscroll-behavior-y: none; }

/* ===== 登录页 ===== */
.login-view { min-height: 100vh; min-height: 100dvh; min-height: -webkit-fill-available; display: flex; align-items: center; justify-content: center; padding: calc(24px + env(safe-area-inset-top)) 24px calc(24px + env(safe-area-inset-bottom)); }
.login-card { width: 100%; max-width: 400px; background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 32px 28px; }
.login-brand { text-align: center; margin-bottom: 24px; }
.login-brand h1 { margin: 10px 0 4px; font-size: 22px; }
.login-brand p { margin: 0; color: var(--muted); font-size: 14px; }
.field { margin-bottom: 16px; }
.field label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 6px; }
.field input { width: 100%; padding: 10px 12px; border: 1px solid var(--border); border-radius: 10px; outline: none; transition: border .2s, box-shadow .2s; font-size: 16px; }
.field input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(79,124,255,0.12); }
.login-error { color: var(--red); font-size: 13px; min-height: 18px; margin-bottom: 10px; }
.btn-primary { background: var(--primary); color: #fff; border: none; padding: 10px 16px; border-radius: 10px; font-weight: 600; transition: background .2s; }
.btn-primary:hover { background: var(--primary-dim); }
.btn-primary:disabled { opacity: .7; }
.btn-block { width: 100%; font-size: 15px; }
.login-tip { text-align: center; color: var(--muted); font-size: 12px; margin-top: 14px; }

/* ===== 应用布局 ===== */
.app-view { display: flex; height: 100vh; height: 100dvh; height: -webkit-fill-available; overflow: hidden; }
.sidebar { width: 260px; background: var(--surface); border-right: 1px solid var(--border); display: flex; flex-direction: column; }
.sidebar-head { padding: 14px 16px 10px; border-bottom: 1px solid var(--border); }
.brand-mini { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 15px; margin-bottom: 10px; }
.btn-new { width: 100%; font-size: 14px; }
.conv-list { flex: 1; overflow-y: auto; padding: 8px; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; }
.conv-empty { padding: 20px; text-align: center; color: var(--muted); font-size: 13px; }
.conv-item { display: flex; align-items: center; gap: 8px; padding: 8px 10px; border-radius: 10px; cursor: pointer; transition: background .15s; position: relative; }
.conv-item:hover { background: #f3f4f6; }
.conv-item.active { background: #eef2ff; }
.conv-icon { flex-shrink: 0; color: var(--muted); }
.conv-title { flex: 1; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; font-size: 13.5px; }
.conv-actions { display: none; gap: 2px; }
.conv-item:hover .conv-actions, .conv-item.active .conv-actions { display: flex; }
.icon-btn { background: none; border: none; padding: 4px; border-radius: 6px; color: var(--muted); display: inline-flex; align-items: center; justify-content: center; }
.icon-btn:hover { background: rgba(0,0,0,0.06); color: var(--text); }
.icon-btn.mini { padding: 2px; }

.sidebar-foot { border-top: 1px solid var(--border); padding: 10px; }
.side-menu-btn { width: 100%; text-align: left; padding: 8px 10px; border-radius: 8px; border: none; background: transparent; color: var(--text); font-size: 13px; display: flex; align-items: center; gap: 8px; }
.side-menu-btn:hover { background: #f3f4f6; }
.user-box { display: flex; align-items: center; gap: 8px; margin-top: 8px; padding-top: 8px; border-top: 1px solid var(--border); }
.avatar { width: 28px; height: 28px; 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; }
.user-meta { flex: 1; min-width: 0; }
.user-name { font-size: 13px; font-weight: 600; }
.user-role { font-size: 11px; color: var(--muted); }
.sidebar-mask { position: fixed; inset: 0; background: rgba(0,0,0,0.25); z-index: 20; display: none; }

.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.topbar { height: calc(var(--header-h) + env(safe-area-inset-top)); padding-top: env(safe-area-inset-top); background: var(--surface); border-bottom: 1px solid var(--border); display: flex; align-items: center; gap: 10px; padding-left: 16px; padding-right: 16px; flex-shrink: 0; }
.topbar-title { font-weight: 700; font-size: 15px; }
.topbar-right { margin-left: auto; }
.kb-badge { font-size: 12px; color: var(--muted); background: #f3f4f6; padding: 4px 8px; border-radius: 20px; }

/* ===== 聊天面板 ===== */
.chat-panel { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.chat-scroll { flex: 1; overflow-y: auto; padding: 16px 16px 8px; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; }
.welcome-box { max-width: 640px; margin: 20px auto; text-align: center; color: var(--muted); }
.welcome-box h2 { color: var(--text); margin: 12px 0 6px; }
.example-title { margin: 16px 0 8px; font-size: 13px; color: var(--muted); }
.example-list { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; }
.example-chip { background: #fff; border: 1px solid var(--border); color: var(--text); padding: 6px 10px; border-radius: 20px; font-size: 13px; cursor: pointer; transition: border-color .2s, box-shadow .2s; }
.example-chip:hover { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(79,124,255,0.08); }

.msg { display: flex; gap: 10px; margin-bottom: 14px; }
.msg.user { flex-direction: row-reverse; }
.msg-avatar { width: 32px; height: 32px; border-radius: 10px; display: flex; align-items: center; justify-content: center; font-size: 12px; font-weight: 700; flex-shrink: 0; }
.msg-avatar.assistant { background: #eef2ff; color: var(--primary); }
.msg-avatar.user { background: var(--primary); color: #fff; }
.msg-body { max-width: 80%; min-width: 0; }
.msg-content { padding: 10px 14px; border-radius: 14px; font-size: 14.5px; line-height: 1.65; word-break: break-word; background: #fff; border: 1px solid var(--border); }
.msg.user .msg-content { background: var(--primary); color: #fff; border-color: var(--primary); }
.msg-content.stream { animation: pulse 1.5s infinite; }
@keyframes pulse { 0% { opacity: .95; } 50% { opacity: .7; } 100% { opacity: .95; } }

.cite-chip { display: inline-block; background: #eef2ff; color: var(--primary); border: 1px solid #dbe4ff; border-radius: 6px; padding: 0 4px; font-size: 12px; cursor: pointer; margin: 0 1px; vertical-align: middle; }
.cite-chip:hover { background: #dce5ff; }
.msg-citations { margin-top: 8px; }
.cite-title { font-size: 12px; color: var(--muted); margin-bottom: 6px; }
.cite-card { background: #fff; border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; margin-bottom: 8px; }
.cite-head { display: flex; align-items: center; gap: 8px; margin-bottom: 4px; }
.cite-id { width: 18px; height: 18px; border-radius: 50%; background: var(--primary); color: #fff; font-size: 11px; display: inline-flex; align-items: center; justify-content: center; }
.cite-file { font-size: 12px; font-weight: 600; }
.cite-score { font-size: 11px; color: var(--muted); margin-left: auto; }
.cite-snippet { font-size: 12px; color: var(--muted); line-height: 1.5; }

.input-area { padding: 10px 16px calc(12px + env(safe-area-inset-bottom)); background: var(--surface); border-top: 1px solid var(--border); }
.input-box { display: flex; align-items: flex-end; gap: 8px; background: #f3f4f6; border-radius: 12px; padding: 8px 10px; }
.input-box textarea { flex: 1; border: none; background: transparent; outline: none; resize: none; font-size: 16px; line-height: 1.5; max-height: 160px; padding: 4px; }
.send-btn { width: 36px; height: 36px; border-radius: 10px; background: var(--primary); color: #fff; border: none; display: flex; align-items: center; justify-content: center; transition: background .2s; }
.send-btn:hover { background: var(--primary-dim); }
.send-btn.loading { opacity: .7; }
.input-tip { text-align: center; font-size: 11px; color: var(--muted); margin-top: 6px; }

/* ===== 通用面板 ===== */
.panel { flex: 1; overflow-y: auto; padding: 16px; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; }
.panel-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.panel-head h3 { margin: 0; font-size: 17px; }

/* 知识库面板 */
.upload-zone { border: 2px dashed var(--border); border-radius: var(--radius); padding: 24px; text-align: center; cursor: pointer; transition: border-color .2s, background .2s; margin-bottom: 16px; }
.upload-zone:hover, .upload-zone.dragover { border-color: var(--primary); background: #f8faff; }
.kb-stats { font-size: 13px; color: var(--muted); margin-bottom: 12px; }

/* 表格 */
.data-table { width: 100%; border-collapse: collapse; font-size: 13px; background: var(--surface); border-radius: var(--radius); overflow: hidden; }
.data-table th, .data-table td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); white-space: nowrap; }
.data-table th { background: #f9fafb; font-weight: 600; color: var(--muted); }
.data-table tr:last-child td { border-bottom: none; }
.td-empty { text-align: center; color: var(--muted); padding: 24px; }
.badge { font-size: 11px; padding: 3px 8px; border-radius: 20px; display: inline-block; }
.badge.ready, .badge.INFO { background: #dcfce7; color: #166534; }
.badge.parsing { background: #fef9c3; color: #854d0e; }
.badge.failed, .badge.ERROR { background: #fee2e2; color: #991b1b; }
.badge.WARN { background: #ffedd5; color: #9a3412; }
.btn-sm { font-size: 12px; padding: 4px 8px; border-radius: 6px; }
.btn-danger { background: var(--red); color: #fff; border: none; }
.btn-danger:hover { background: #dc2626; }
.btn-ghost { background: transparent; color: var(--muted); border: 1px solid var(--border); padding: 6px 12px; border-radius: 8px; }
.err-hint { font-size: 11px; color: var(--red); margin-top: 4px; max-width: 220px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* 日志 */
.log-filters { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 12px; align-items: center; }
.log-filters select, .log-filters input { padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; font-size: 13px; outline: none; }
.table-wrap { overflow-x: auto; border-radius: var(--radius); border: 1px solid var(--border); }
.log-table td { white-space: nowrap; }
.log-table .log-detail { white-space: normal; min-width: 200px; max-width: 400px; overflow: hidden; text-overflow: ellipsis; }
.log-pager { display: flex; align-items: center; gap: 10px; margin-top: 12px; justify-content: center; }

/* ===== Toast ===== */
.toast { position: fixed; top: calc(16px + env(safe-area-inset-top)); right: calc(16px + env(safe-area-inset-right)); left: calc(16px + env(safe-area-inset-left)); max-width: calc(100vw - 32px - env(safe-area-inset-left) - env(safe-area-inset-right)); margin: 0 auto; text-align: center; background: var(--text); color: #fff; padding: 10px 14px; border-radius: 10px; font-size: 13px; z-index: 100; box-shadow: var(--shadow); transform: translateY(-8px); opacity: 0; transition: all .25s; word-break: break-word; }
.toast.show { transform: translateY(0); opacity: 1; }

/* 上传状态 */
.spinner { display: inline-block; width: 14px; height: 14px; border: 2px solid #c7d2fe; border-top-color: var(--primary); border-radius: 50%; animation: spin .6s linear infinite; vertical-align: middle; margin-right: 6px; }
@keyframes spin { to { transform: rotate(360deg); } }
.upload-zone.uploading { border-color: var(--primary); background: #f8faff; cursor: wait; }
.upload-zone.uploading svg { opacity: .5; }

/* ===== 响应式 ===== */
@media (max-width: 768px) {
  .mobile-only { display: inline-flex !important; }
  .sidebar { position: fixed; left: 0; top: 0; bottom: 0; z-index: 30; transform: translateX(-100%); transition: transform .25s; max-width: 85vw; }
  .sidebar.open { transform: translateX(0); }
  .sidebar-mask { display: block; }
  .main { width: 100%; }
  .msg-body { max-width: 85%; }
  .panel { padding: 12px; }
  .example-list { justify-content: flex-start; }
  .log-filters select, .log-filters input { flex: 1 1 120px; }
  /* 移动端表格横向滚动，避免溢出 */
  .data-table { font-size: 12px; }
  .data-table th, .data-table td { padding: 8px 10px; }
  /* 触摸目标最小尺寸 */
  .icon-btn { min-width: 36px; min-height: 36px; }
  .conv-actions { display: flex; }
  /* 登录卡片紧凑化 */
  .login-card { padding: 24px 20px; }
  .login-brand h1 { font-size: 20px; }
}
@media (min-width: 769px) {
  .mobile-only { display: none !important; }
  .sidebar-mask { display: none !important; }
}
