:root {
  --bg: #f8fafc;
  --surface: #ffffff;
  --border: #e2e8f0;
  --text: #0f172a;
  --muted: #64748b;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --purple: #7c3aed;
  --purple-hover: #6d28d9;
  --green: #059669;
  --red: #dc2626;
  --radius: 10px;
}

* {
  box-sizing: border-box;
  scrollbar-width: thin;
  scrollbar-color: rgba(148, 163, 184, 0.45) transparent;
}
*::-webkit-scrollbar { width: 6px; height: 6px; }
*::-webkit-scrollbar-track { background: transparent; }
*::-webkit-scrollbar-thumb {
  background: rgba(148, 163, 184, 0.4);
  border-radius: 999px;
}
*::-webkit-scrollbar-thumb:hover {
  background: rgba(100, 116, 139, 0.65);
}
html, body {
  margin: 0; padding: 0; height: 100%;
  background: var(--bg); color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Segoe UI", sans-serif;
  font-size: 14px; line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.accent { color: var(--primary); }
.icon-md { width: 20px; height: 20px; flex-shrink: 0; }
.icon-lg { width: 24px; height: 24px; color: var(--primary); }

.btn {
  display: inline-flex; align-items: center; justify-content: center;
  padding: 8px 14px; border-radius: 8px; border: 1px solid transparent;
  font-size: 13px; font-weight: 600; cursor: pointer; text-decoration: none;
  transition: background 0.15s ease, border-color 0.15s ease;
}
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--primary-hover); }
.btn-purple { background: var(--purple); color: #fff; }
.btn-purple:hover:not(:disabled) { background: var(--purple-hover); }
.btn-ghost { background: #fff; border-color: var(--border); color: #334155; }
.btn-ghost:hover:not(:disabled) { background: #f1f5f9; }
.btn-sm { padding: 5px 10px; font-size: 12px; }
.btn-xs { padding: 3px 8px; font-size: 11px; }
.full-width { width: 100%; }

.auth-gate {
  min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 20px;
}
.auth-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
  padding: 28px; width: 100%; max-width: 400px; box-shadow: 0 8px 24px rgba(15, 23, 42, 0.06);
}
.brand-row { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
.brand-row h1 { margin: 0; font-size: 18px; }
.token-form { display: flex; flex-direction: column; gap: 10px; margin-top: 16px; }
.token-form input, .ask-form input, .sidebar-head input {
  width: 100%; padding: 9px 12px; border: 1px solid var(--border); border-radius: 8px;
  font-size: 13px; outline: none; background: #fff;
}
.token-form input:focus, .ask-form input:focus, .sidebar-head input:focus {
  border-color: var(--primary);
}
.sso-entry { margin-top: 12px; }
.error-text { color: var(--red); font-size: 12px; margin-top: 10px; }

.app-shell { display: flex; flex-direction: column; height: 100vh; }
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 10px 20px; background: var(--surface); border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.topbar-left, .topbar-right, .stats-strip { display: flex; align-items: center; gap: 10px; }
.brand-title { font-weight: 700; font-size: 15px; }
.badge {
  display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 11px; font-weight: 600;
}
.badge-soft { background: #eff6ff; color: var(--primary); }
.badge-green { background: #ecfdf5; color: var(--green); }
.badge-purple { background: #f5f3ff; color: var(--purple); }
.badge-gray { background: #f1f5f9; color: #475569; }
.endpoint-badge {
  max-width: 220px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-weight: 500;
}

.stat-pill {
  display: flex; align-items: baseline; gap: 6px; padding: 4px 10px;
  background: #f8fafc; border: 1px solid var(--border); border-radius: 8px;
}
.stat-label { font-size: 11px; color: var(--muted); }
.stat-pill strong { font-size: 13px; }

.workspace { display: flex; flex: 1; min-height: 0; }
.sidebar {
  width: 310px; flex-shrink: 0; background: var(--surface); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; min-height: 0;
}
.sidebar-head { padding: 12px; border-bottom: 1px solid var(--border); }
.topic-list { flex: 1; overflow-y: auto; padding: 8px; display: flex; flex-direction: column; gap: 4px; }
.topic-item {
  width: 100%; text-align: left; background: transparent; border: 1px solid transparent;
  border-radius: 8px; padding: 9px 10px; cursor: pointer;
}
.topic-item:hover { background: #f8fafc; }
.topic-item.active { background: #eff6ff; border-color: #bfdbfe; }
.topic-item-top { display: flex; justify-content: space-between; align-items: center; gap: 6px; }
.topic-item-name { font-weight: 600; font-size: 13px; color: var(--text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topic-item-sub { font-size: 11px; color: var(--muted); margin-top: 3px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.content { flex: 1; display: flex; flex-direction: column; min-width: 0; min-height: 0; padding: 16px 22px; gap: 14px; }
.tabs { display: flex; gap: 6px; border-bottom: 1px solid var(--border); padding-bottom: 8px; flex-shrink: 0; }
.tab {
  background: transparent; border: none; padding: 6px 14px; border-radius: 8px;
  font-size: 13px; font-weight: 600; color: var(--muted); cursor: pointer;
}
.tab.active { background: #eff6ff; color: var(--primary); }

.tab-panel { flex: 1; display: flex; flex-direction: column; min-height: 0; gap: 12px; overflow-y: auto; }
.topic-header {
  display: flex; justify-content: space-between; align-items: flex-start; gap: 16px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px;
}
.topic-header h2 { margin: 0 0 4px 0; font-size: 16px; }
.action-row { display: flex; gap: 8px; flex-shrink: 0; }
.meta-row { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }

.code-box {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px 16px; margin: 0; font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12.5px; line-height: 1.6; white-space: pre-wrap; word-break: break-word; overflow-y: auto;
}
.memory-body { flex: 1; min-height: 280px; }

.dream-box {
  background: #faf5ff; border: 1px solid #ddd6fe; border-radius: var(--radius); padding: 12px 14px;
}
.dream-box-head { display: flex; justify-content: space-between; align-items: center; }
.change-list { margin: 8px 0; padding-left: 18px; font-size: 12.5px; color: #334155; }
.diff-box { max-height: 220px; background: #fff; font-size: 11.5px; }

.ask-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 18px 20px; max-width: 820px;
}
.ask-card h3 { margin: 0 0 4px 0; }
.ask-form { display: flex; gap: 10px; margin-top: 14px; }
.ask-result { margin-top: 16px; }
.capsule-box {
  margin-top: 10px; padding: 14px 16px; background: #f0fdf4; border: 1px solid #bbf7d0;
  border-radius: var(--radius); font-size: 14px; color: #064e3b; line-height: 1.6;
}

.logs-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.log-col {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px 16px; display: flex; flex-direction: column; gap: 10px;
}
.log-col h3 { margin: 0; font-size: 14px; }
.log-list { display: flex; flex-direction: column; gap: 8px; }
.log-entry {
  border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; background: #f8fafc;
}
.log-entry-q { font-weight: 600; font-size: 13px; }
.log-entry-a { font-size: 12px; color: #334155; margin-top: 4px; }

@media (max-width: 900px) {
  .workspace { flex-direction: column; }
  .sidebar { width: 100%; max-height: 240px; border-right: none; border-bottom: 1px solid var(--border); }
  .logs-grid { grid-template-columns: 1fr; }
  .stats-strip { display: none; }
}
