:root {
  --bg-canvas: #f5f7fb;
  --bg-surface: #ffffff;
  --bg-subtle: #eef2f8;
  --bg-tint: #eff6ff;
  --border-subtle: #e2e8f0;
  --border-strong: #cbd5e1;
  --ink-primary: #0f172a;
  --ink-secondary: #475569;
  --ink-muted: #64748b;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --accent-soft: #dbeafe;
  --indicator-end: #d96b86;
  --success: #059669;
  --success-bg: #ecfdf5;
  --warn: #b45309;
  --warn-bg: #fffbeb;
  --danger: #dc2626;
  --danger-bg: #fef2f2;
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 14px;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.05);
  --shadow-md: 0 4px 14px rgba(15, 23, 42, 0.08);
  --shadow-lg: 0 18px 48px rgba(15, 23, 42, 0.18);
  --font-sans: "Plus Jakarta Sans", "PingFang SC", "Microsoft YaHei", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --font-mono: "JetBrains Mono", "Cascadia Code", Consolas, monospace;
  --topbar-h: 52px;
  --sidebar-w: 300px;
}

*, *::before, *::after { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg-canvas);
  color: var(--ink-primary);
  font-family: var(--font-sans);
  font-size: 14px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}

button, input, textarea, select { font-family: inherit; font-size: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
h1, h2, h3 { margin: 0; }
.muted { color: var(--ink-muted); font-size: 13px; margin: 0 0 12px; }

/* ---------------- Buttons ---------------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 36px;
  padding: 0 14px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  background: transparent;
  font-weight: 600;
  white-space: nowrap;
  transition: background 0.12s, border-color 0.12s, color 0.12s;
}
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn-sm { height: 30px; padding: 0 10px; font-size: 13px; }
.btn-lg { height: 42px; padding: 0 26px; font-size: 15px; }
.btn-block { width: 100%; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--accent-hover); }
.btn-secondary { background: #fff; border-color: var(--border-strong); color: var(--ink-primary); }
.btn-secondary:hover:not(:disabled) { border-color: var(--accent); color: var(--accent); }
.btn-ghost { color: var(--ink-secondary); }
.btn-ghost:hover:not(:disabled) { background: var(--bg-subtle); color: var(--ink-primary); }
.btn-success { background: var(--success); color: #fff; }
.btn-success:hover:not(:disabled) { background: #047857; }

/* ---------------- Badges ---------------- */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  height: 22px;
  padding: 0 8px;
  border-radius: 999px;
  background: var(--bg-subtle);
  color: var(--ink-secondary);
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
}
.badge-blue { background: var(--accent-soft); color: var(--accent-hover); }
.badge-green { background: var(--success-bg); color: var(--success); }
.badge-amber { background: var(--warn-bg); color: var(--warn); }
.badge-red { background: var(--danger-bg); color: var(--danger); }

/* ---------------- App layout ---------------- */
.app {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  grid-template-rows: var(--topbar-h) 1fr;
  grid-template-areas: "top top" "side main";
  height: 100vh;
}

.topbar {
  grid-area: top;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 18px;
  background: #fff;
  border-bottom: 1px solid var(--border-subtle);
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark {
  width: 26px; height: 26px; border-radius: 50%;
  border: 2.5px solid var(--accent);
  position: relative; flex-shrink: 0;
  background: linear-gradient(135deg, #fff 0%, #eff6ff 100%);
}
.brand-mark::after {
  content: ""; width: 7px; height: 7px; border-radius: 50%;
  background: var(--indicator-end);
  position: absolute; top: -3px; right: -2px; border: 1.5px solid #fff;
}
.brand-title { font-weight: 700; font-size: 15px; letter-spacing: 0.01em; }
.topbar-right { display: flex; align-items: center; gap: 6px; }

.status-pill {
  display: inline-flex; align-items: center; gap: 6px;
  height: 26px; padding: 0 10px; margin-right: 6px;
  border-radius: 999px; font-size: 12.5px; font-weight: 600;
  background: var(--bg-subtle); color: var(--ink-muted);
}
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: currentColor; }
.status-pill.ok { background: var(--success-bg); color: var(--success); }
.status-pill.bad { background: var(--danger-bg); color: var(--danger); }

/* ---------------- Sidebar ---------------- */
.sidebar {
  grid-area: side;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 14px 12px;
  background: #fff;
  border-right: 1px solid var(--border-subtle);
  min-height: 0;
}
.search-input {
  width: 100%;
  height: 34px;
  padding: 0 10px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: #fff;
}
.thread-list { flex: 1; overflow-y: auto; margin: 0 -4px; padding: 0 4px; }
.thread-item {
  display: block;
  width: 100%;
  text-align: left;
  padding: 10px 10px;
  margin-bottom: 2px;
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
}
.thread-item:hover { background: var(--bg-subtle); }
.thread-item.active { background: var(--accent-soft); }
.thread-item-top { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.thread-item-name { font-weight: 700; font-size: 13.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.thread-item-time { font-size: 11.5px; color: var(--ink-muted); flex-shrink: 0; }
.thread-item-subj { font-size: 12.5px; color: var(--ink-secondary); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.thread-item-meta { display: flex; gap: 6px; margin-top: 4px; align-items: center; }
.thread-item-meta .badge { height: 19px; font-size: 11px; padding: 0 6px; }
.thread-empty { padding: 24px 8px; text-align: center; color: var(--ink-muted); font-size: 13px; }

/* ---------------- Workspace ---------------- */
.workspace { grid-area: main; display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.thread-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 12px 28px;
  background: #fff;
  border-bottom: 1px solid var(--border-subtle);
}
.thread-header h1 { font-size: 17px; font-weight: 700; }
.thread-header-sub { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin-top: 4px; font-size: 12.5px; color: var(--ink-muted); }
.workspace-scroll { flex: 1; overflow-y: auto; padding: 20px 28px 48px; }
.workspace-scroll > * { max-width: 1080px; margin-left: auto; margin-right: auto; }

/* Menus */
.menu-wrap { position: relative; }
.menu {
  position: absolute; right: 0; top: calc(100% + 4px); z-index: 30;
  min-width: 200px; padding: 6px;
  background: #fff; border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md); box-shadow: var(--shadow-md);
}
.menu-wide { min-width: 360px; }
.menu button {
  display: block; width: 100%; text-align: left;
  padding: 8px 10px; border: 0; border-radius: var(--radius-sm); background: transparent;
  font-size: 13.5px;
}
.menu button:hover { background: var(--bg-subtle); }
.menu button.danger { color: var(--danger); }
.menu hr { border: 0; border-top: 1px solid var(--border-subtle); margin: 4px 0; }
.menu .menu-sub { display: block; font-size: 12px; color: var(--ink-muted); }

/* Memory card */
.memory-card {
  background: #fff; border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md); margin-bottom: 16px;
}
.memory-card > summary {
  list-style: none; cursor: pointer; padding: 10px 14px;
  display: flex; gap: 10px; align-items: baseline; font-weight: 700; font-size: 13.5px;
}
.memory-card > summary::-webkit-details-marker { display: none; }
.memory-card > summary::before { content: "▸"; color: var(--ink-muted); }
.memory-card[open] > summary::before { content: "▾"; }
.memory-brief { font-weight: 400; color: var(--ink-secondary); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.memory-card[open] .memory-brief { display: none; }
.memory-body { padding: 0 14px 14px; display: grid; gap: 10px; }
.memory-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px 18px; }
.memory-label { font-size: 12px; font-weight: 700; color: var(--ink-muted); margin-bottom: 4px; }
.tags { display: flex; flex-wrap: wrap; gap: 5px; }
.tag { font-size: 12px; padding: 2px 8px; border-radius: 5px; background: var(--bg-subtle); color: var(--ink-secondary); }
.tag-empty { font-size: 12px; color: var(--ink-muted); }
.memory-note { font-size: 12.5px; background: var(--bg-tint); color: #1e40af; padding: 6px 10px; border-radius: var(--radius-sm); }

/* Timeline */
.timeline { display: flex; flex-direction: column; gap: 10px; margin-bottom: 16px; }
.timeline:empty { display: none; }
.msg {
  background: #fff;
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  width: 88%;
}
.msg.ours { align-self: flex-end; background: #f7faff; border-color: #cfe0fb; }
.msg-head {
  display: flex; justify-content: space-between; align-items: center; gap: 10px;
  padding: 10px 14px; cursor: pointer;
}
.msg-who { font-weight: 700; font-size: 13.5px; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.msg-time { font-size: 12px; color: var(--ink-muted); white-space: nowrap; }
.msg-preview { padding: 0 14px 10px; color: var(--ink-secondary); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin-top: -4px; }
.msg-body { padding: 0 14px 12px; white-space: pre-wrap; font-size: 13.5px; line-height: 1.6; }
.msg-subject { font-size: 12.5px; color: var(--ink-muted); margin-bottom: 6px; }
.msg-foot { display: flex; justify-content: flex-end; gap: 4px; padding: 0 10px 8px; }
.msg.collapsed .msg-body, .msg.collapsed .msg-foot { display: none; }
.msg:not(.collapsed) .msg-preview { display: none; }
.timeline-more { align-self: center; }

/* Welcome */
.welcome {
  background: #fff; border: 1px dashed var(--border-strong);
  border-radius: var(--radius-lg); padding: 40px 48px; margin-top: 40px;
}
.welcome h2 { font-size: 19px; margin-bottom: 16px; }
.welcome ol { margin: 0; padding-left: 20px; color: var(--ink-secondary); line-height: 2; }

/* Composer */
.composer {
  background: #fff;
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  padding: 18px 20px;
  box-shadow: var(--shadow-sm);
}
.composer-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }
.composer-head h2 { font-size: 16px; }
.paste-box {
  width: 100%;
  min-height: 220px;
  resize: vertical;
  padding: 12px 14px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-md);
  line-height: 1.6;
  background: #fff;
}
.paste-box:focus { border-color: var(--accent); outline: none; box-shadow: 0 0 0 3px var(--accent-soft); }

.parsed-row {
  display: grid;
  grid-template-columns: 1fr 1.2fr 1fr;
  gap: 8px 12px;
  margin-top: 10px;
}
.parsed-row label, .advanced-grid label, .field { display: flex; flex-direction: column; gap: 3px; }
.parsed-row label > span, .advanced-grid label > span, .field > span { font-size: 12px; font-weight: 600; color: var(--ink-muted); }
.parsed-row .wide, .advanced-grid .wide { grid-column: 1 / -1; }
.parsed-row input, .advanced-grid input, .advanced-grid select, .field input, .field textarea {
  height: 34px; padding: 0 10px;
  border: 1px solid var(--border-strong); border-radius: var(--radius-sm); background: #fff;
}
.field textarea { height: auto; padding: 8px 10px; resize: vertical; line-height: 1.5; }
.field { margin-bottom: 12px; }
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 0 14px; }

.advanced { margin-top: 12px; }
.advanced > summary { cursor: pointer; font-size: 13px; font-weight: 600; color: var(--ink-secondary); }
.advanced-brief { font-weight: 400; color: var(--ink-muted); margin-left: 6px; }
.advanced-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px 12px; margin-top: 10px; }

.composer-actions { display: flex; justify-content: flex-end; align-items: center; gap: 10px; margin-top: 16px; }
.gen-offline-hint { color: var(--danger); font-size: 13px; margin-right: auto; }

.match-banner {
  display: flex; justify-content: space-between; align-items: center; gap: 10px;
  padding: 10px 12px; margin-bottom: 10px;
  background: var(--bg-tint); border: 1px solid #bfdbfe; border-radius: var(--radius-md);
  font-size: 13.5px; color: #1e3a8a;
}
.match-banner .actions { display: flex; gap: 6px; flex-shrink: 0; }

/* Loading */
.loading-card {
  display: flex; align-items: center; gap: 16px;
  background: #fff; border: 1px solid var(--border-subtle); border-radius: var(--radius-lg);
  padding: 22px 24px;
}
.loading-title { font-weight: 700; }
.loading-sub { color: var(--ink-muted); font-size: 13px; }
.spinner {
  width: 28px; height: 28px; border-radius: 50%;
  border: 3px solid var(--accent-soft); border-top-color: var(--accent);
  animation: spin 0.9s linear infinite; flex-shrink: 0;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Results */
.results { display: flex; flex-direction: column; gap: 12px; }
.results > * { width: 100%; }
.diag {
  background: #fff; border: 1px solid var(--border-subtle); border-radius: var(--radius-md);
  padding: 12px 16px;
}
.diag-line { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.diag-summary { margin: 8px 0 0; color: var(--ink-primary); font-size: 13.5px; }
.diag-alert { margin-top: 8px; padding: 8px 10px; border-radius: var(--radius-sm); font-size: 13px; }
.diag-alert.L2 { background: var(--warn-bg); color: var(--warn); }
.diag-alert.L3 { background: var(--danger-bg); color: var(--danger); }

.version-bar { display: flex; justify-content: space-between; align-items: flex-end; border-bottom: 1px solid var(--border-subtle); }
.version-tabs { display: flex; gap: 2px; }
.version-tab {
  border: 0; background: transparent; padding: 10px 16px 9px;
  border-bottom: 2.5px solid transparent; margin-bottom: -1px;
  font-weight: 600; color: var(--ink-secondary); display: flex; gap: 6px; align-items: center;
}
.version-tab:hover { color: var(--ink-primary); }
.version-tab.active { color: var(--accent); border-bottom-color: var(--accent); }
.version-tab .rec { font-size: 11px; background: var(--accent); color: #fff; padding: 1px 6px; border-radius: 4px; }
.version-bar-right { display: flex; gap: 4px; padding-bottom: 6px; }

.version-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
.vcard {
  background: #fff; border: 1px solid var(--border-subtle); border-radius: var(--radius-lg);
  padding: 16px 18px; display: flex; flex-direction: column; gap: 10px;
}
.vcard-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; }
.vcard-title { font-weight: 700; }
.vcard-sub { font-size: 12.5px; color: var(--ink-muted); }
.vcard input.subject {
  width: 100%; height: 38px; padding: 0 12px; font-weight: 600;
  border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
}
.vcard textarea.body {
  width: 100%; min-height: 380px; resize: vertical; padding: 12px 14px;
  border: 1px solid var(--border-strong); border-radius: var(--radius-sm); line-height: 1.65;
}
.version-grid .vcard textarea.body { min-height: 320px; font-size: 13px; }
.vcard input.subject:focus, .vcard textarea.body:focus { border-color: var(--accent); outline: none; box-shadow: 0 0 0 3px var(--accent-soft); }
.vcard-actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.vcard-actions .spacer { flex: 1; }
.refine-row { display: flex; gap: 8px; }
.refine-row input { flex: 1; height: 36px; padding: 0 10px; border: 1px solid var(--border-strong); border-radius: var(--radius-sm); }
.side-notes { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.version-grid .side-notes { grid-template-columns: 1fr; }
.note-box { background: var(--bg-canvas); border-radius: var(--radius-sm); padding: 10px 12px; font-size: 12.5px; color: var(--ink-secondary); white-space: pre-wrap; }
.note-box b { color: var(--ink-primary); display: block; margin-bottom: 4px; white-space: normal; }
.att-line { display: flex; gap: 8px; white-space: normal; padding: 2px 0; }
.att-line .name { font-family: var(--font-mono); font-size: 12px; color: var(--accent-hover); }
.audit { font-size: 12.5px; }
.audit > summary { cursor: pointer; color: var(--ink-secondary); }
.audit ul { margin: 6px 0 0; padding-left: 18px; color: var(--ink-secondary); }
.audit li.fail { color: var(--warn); }

.evidence { background: #fff; border: 1px solid var(--border-subtle); border-radius: var(--radius-md); }
.evidence > summary { cursor: pointer; padding: 10px 14px; font-size: 13px; font-weight: 600; color: var(--ink-secondary); }
.evidence-body { padding: 0 14px 14px; display: flex; flex-direction: column; gap: 10px; }
.ev-title { font-size: 12.5px; font-weight: 700; color: var(--ink-muted); margin-top: 6px; }
.ev-item { border: 1px solid var(--border-subtle); border-radius: var(--radius-sm); padding: 10px 12px; font-size: 12.5px; }
.ev-item-head { display: flex; justify-content: space-between; gap: 8px; margin-bottom: 6px; flex-wrap: wrap; }
.pair-cols { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.pair-cols > div { white-space: pre-wrap; background: var(--bg-canvas); border-radius: var(--radius-sm); padding: 8px 10px; max-height: 260px; overflow: auto; }
.pair-cols > div.reply { background: #f0f7ff; }

/* ---------------- Overlays ---------------- */
.overlay {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(15, 23, 42, 0.42);
  display: flex; justify-content: center; align-items: flex-start;
  padding: 5vh 20px;
  overflow-y: auto;
}
.overlay-panel {
  background: #fff; border-radius: var(--radius-lg); box-shadow: var(--shadow-lg);
  width: min(1180px, 100%); padding: 20px 24px;
}
.overlay-md { width: min(820px, 100%); }
.overlay-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.overlay-head h2 { font-size: 17px; }
.overlay-head-actions { display: flex; gap: 6px; }
.overlay-foot { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; }

.import-row { border: 1px solid var(--border-subtle); border-radius: var(--radius-md); padding: 10px 12px; margin: 10px 0; display: grid; gap: 8px; }
.import-row-head { display: flex; gap: 8px; align-items: center; }
.import-row select, .import-row input { height: 32px; padding: 0 8px; border: 1px solid var(--border-strong); border-radius: var(--radius-sm); background: #fff; }
.import-row input.imp-subj { flex: 1; }
.import-row textarea { width: 100%; min-height: 90px; padding: 8px 10px; border: 1px solid var(--border-strong); border-radius: var(--radius-sm); resize: vertical; }

.kb-stats { display: flex; gap: 18px; flex-wrap: wrap; font-size: 13px; color: var(--ink-secondary); margin-bottom: 12px; }
.kb-stats b { color: var(--ink-primary); font-size: 15px; margin-right: 3px; }
.kb-toolbar { display: flex; gap: 12px; align-items: center; margin-bottom: 14px; }
.kb-toolbar .search-input { max-width: 320px; }
.seg { display: inline-flex; background: var(--bg-subtle); border-radius: var(--radius-sm); padding: 3px; gap: 2px; }
.seg button { border: 0; background: transparent; padding: 5px 12px; border-radius: 5px; font-size: 13px; font-weight: 600; color: var(--ink-secondary); }
.seg button.active { background: #fff; color: var(--ink-primary); box-shadow: var(--shadow-sm); }
.kb-content { display: grid; gap: 10px; }
.kb-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.kb-item { border: 1px solid var(--border-subtle); border-radius: var(--radius-md); padding: 12px 14px; font-size: 13px; display: grid; gap: 6px; }
.kb-item ul { margin: 0; padding-left: 18px; color: var(--ink-secondary); }

/* ---------------- Toast ---------------- */
.toast {
  position: fixed; left: 50%; bottom: 28px; transform: translate(-50%, 20px);
  background: var(--ink-primary); color: #fff; padding: 10px 18px; border-radius: var(--radius-md);
  font-size: 13.5px; opacity: 0; pointer-events: none; transition: all 0.2s; z-index: 80; max-width: 80vw;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------------- Login ---------------- */
.login-body { display: flex; align-items: center; justify-content: center; min-height: 100vh; }
.login-card {
  width: 360px; background: #fff; border: 1px solid var(--border-subtle); border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md); padding: 28px; display: grid; gap: 4px;
}
.login-card .brand { margin-bottom: 18px; }
.login-card .field input { height: 40px; }
.login-error { color: var(--danger); font-size: 13px; min-height: 20px; }

@media (max-width: 1100px) {
  :root { --sidebar-w: 240px; }
  .version-grid { grid-template-columns: 1fr; }
  .parsed-row, .advanced-grid { grid-template-columns: 1fr 1fr; }
}
