:root {
  --bg: #0f1117;
  --bg-elev: #171a23;
  --bg-sidebar: #13151d;
  --bg-input: #1e222d;
  --bg-hover: #252a38;
  --border: #2a3040;
  --text: #e5e7eb;
  --text-muted: #9ca3af;
  --accent: #4d6bfe;
  --accent-hover: #3b59e8;
  --danger: #ef4444;
  --success: #10b981;
  --warn: #f59e0b;
  --user-bubble: #232b42;
  --code-bg: #0d1117;
  --radius: 12px;
}

body.light {
  --bg: #f8fafc;
  --bg-elev: #ffffff;
  --bg-sidebar: #f1f5f9;
  --bg-input: #ffffff;
  --bg-hover: #e2e8f0;
  --border: #e2e8f0;
  --text: #0f172a;
  --text-muted: #64748b;
  --accent: #4d6bfe;
  --accent-hover: #3b59e8;
  --user-bubble: #e8efff;
  --code-bg: #1e293b;
}

* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0; height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "PingFang SC", "Microsoft YaHei", sans-serif;
  background: var(--bg); color: var(--text);
  font-size: 15px; line-height: 1.6;
}
.hidden { display: none !important; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* Auth View */
.auth-wrap {
  min-height: 100vh; display: flex; align-items: center; justify-content: center;
  padding: 20px; background: radial-gradient(circle at top, rgba(77,107,254,0.14), transparent 60%);
}
.auth-card {
  width: 100%; max-width: 420px; background: var(--bg-elev);
  border: 1px solid var(--border); border-radius: 16px; padding: 28px;
  box-shadow: 0 20px 50px rgba(0,0,0,0.35);
}
.brand-head { display: flex; align-items: center; gap: 12px; margin-bottom: 18px; }
.brand-logo {
  width: 40px; height: 40px; border-radius: 10px;
  background: linear-gradient(135deg, #4d6bfe, #7c3aed);
  display: flex; align-items: center; justify-content: center;
  color: #fff; font-weight: 700; font-size: 18px;
}
.brand-title { font-size: 19px; font-weight: 700; margin: 0; }
.brand-sub { font-size: 12px; color: var(--text-muted); margin: 2px 0 0; }

.tabs { display: flex; gap: 6px; background: var(--bg-input); padding: 4px; border-radius: 10px; margin-bottom: 18px; }
.tab-btn {
  flex: 1; padding: 8px; border: none; background: transparent; color: var(--text-muted);
  border-radius: 8px; cursor: pointer; font-weight: 600; font-size: 14px;
}
.tab-btn.active { background: var(--bg-elev); color: var(--text); box-shadow: 0 1px 4px rgba(0,0,0,0.2); }

.field { margin-bottom: 14px; }
.field label { display: block; font-size: 13px; color: var(--text-muted); margin-bottom: 6px; }
.input, select, textarea.input {
  width: 100%; padding: 10px 12px; border-radius: 9px;
  border: 1px solid var(--border); background: var(--bg-input);
  color: var(--text); font-size: 14px; outline: none; font-family: inherit;
}
.input:focus, select:focus { border-color: var(--accent); }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 10px 16px; border-radius: 9px; border: 1px solid transparent;
  font-size: 14px; font-weight: 600; cursor: pointer; transition: 0.15s;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-secondary { background: var(--bg-input); color: var(--text); border-color: var(--border); }
.btn-secondary:hover { background: var(--bg-hover); }
.btn-danger { background: rgba(239,68,68,0.15); color: #f87171; border-color: rgba(239,68,68,0.3); }
.btn-sm { padding: 5px 10px; font-size: 12px; border-radius: 7px; }
.w-full { width: 100%; }

.alert {
  padding: 10px 12px; border-radius: 8px; font-size: 13px; margin-bottom: 14px;
  border: 1px solid transparent;
}
.alert-error { background: rgba(239,68,68,0.12); border-color: rgba(239,68,68,0.35); color: #fca5a5; }
.alert-info { background: rgba(77,107,254,0.12); border-color: rgba(77,107,254,0.35); color: #93c5fd; }
.alert-ok { background: rgba(16,185,129,0.12); border-color: rgba(16,185,129,0.35); color: #6ee7b7; }
body.light .alert-error { color: #b91c1c; }
body.light .alert-info { color: #1d4ed8; }
body.light .alert-ok { color: #047857; }

.divider {
  display: flex; align-items: center; gap: 10px; margin: 16px 0;
  color: var(--text-muted); font-size: 12px;
}
.divider::before, .divider::after { content: ""; flex: 1; height: 1px; background: var(--border); }

/* Chat Layout */
.app-shell { display: flex; height: 100vh; overflow: hidden; }
.sidebar {
  width: 270px; flex-shrink: 0; background: var(--bg-sidebar);
  border-right: 1px solid var(--border); display: flex; flex-direction: column;
  transition: margin-left 0.2s ease;
}
.sidebar.collapsed { margin-left: -270px; }
.sidebar-top { padding: 14px; border-bottom: 1px solid var(--border); display: flex; flex-direction: column; gap: 10px; }
.conv-list { flex: 1; overflow-y: auto; padding: 8px; }
.conv-item {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 9px 10px; border-radius: 9px; cursor: pointer; margin-bottom: 4px;
  color: var(--text); font-size: 13.5px;
}
.conv-item:hover { background: var(--bg-hover); }
.conv-item.active { background: var(--bg-hover); border: 1px solid var(--border); }
.conv-title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.conv-actions { display: none; gap: 4px; }
.conv-item:hover .conv-actions, .conv-item.active .conv-actions { display: flex; }
.icon-btn {
  background: transparent; border: none; color: var(--text-muted); cursor: pointer;
  padding: 3px 5px; border-radius: 5px; font-size: 13px;
}
.icon-btn:hover { background: var(--bg-input); color: var(--text); }

.sidebar-foot {
  padding: 12px 14px; border-top: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  font-size: 13px;
}
.user-chip { display: flex; align-items: center; gap: 8px; overflow: hidden; }
.user-avatar {
  width: 28px; height: 28px; border-radius: 50%; background: var(--accent);
  color: #fff; display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 12px; flex-shrink: 0;
}

.main-pane { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.topbar {
  height: 54px; padding: 0 16px; border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  background: var(--bg-elev);
}
.topbar-left, .topbar-right { display: flex; align-items: center; gap: 10px; }
.model-select { width: auto; min-width: 185px; padding: 6px 10px; font-size: 13px; font-weight: 600; }

.toggle-pill {
  display: inline-flex; align-items: center; gap: 6px; padding: 5px 11px;
  border-radius: 999px; border: 1px solid var(--border); background: var(--bg-input);
  color: var(--text-muted); font-size: 12.5px; cursor: pointer; user-select: none;
}
.toggle-pill.active {
  border-color: var(--accent); color: #93c5fd; background: rgba(77,107,254,0.16);
}
body.light .toggle-pill.active { color: #1d4ed8; }

.chat-scroll { flex: 1; overflow-y: auto; padding: 24px 16px; }
.chat-inner { max-width: 820px; margin: 0 auto; display: flex; flex-direction: column; gap: 22px; }

.welcome-box {
  text-align: center; margin: 80px auto 20px; max-width: 560px;
}
.welcome-box h1 { font-size: 24px; margin: 0 0 8px; }
.welcome-box p { color: var(--text-muted); font-size: 14px; margin: 0 0 20px; }
.quick-prompts { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.quick-btn {
  text-align: left; padding: 12px 14px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--bg-elev);
  color: var(--text); cursor: pointer; font-size: 13px;
}
.quick-btn:hover { border-color: var(--accent); background: var(--bg-hover); }

.msg-row { display: flex; gap: 12px; align-items: flex-start; }
.msg-row.user { justify-content: flex-end; }
.msg-avatar {
  width: 30px; height: 30px; border-radius: 8px; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center; font-size: 13px; font-weight: 700;
}
.msg-avatar.ai { background: linear-gradient(135deg, #4d6bfe, #7c3aed); color: #fff; }
.msg-content-wrap { max-width: 88%; min-width: 0; }
.msg-row.user .msg-bubble {
  background: var(--user-bubble); padding: 11px 15px; border-radius: 14px 14px 4px 14px;
  white-space: pre-wrap; word-break: break-word;
}
.msg-row.assistant .msg-content-wrap { flex: 1; max-width: calc(100% - 42px); }

.thinking-box {
  margin-bottom: 10px; border-left: 3px solid var(--accent);
  background: rgba(77,107,254,0.07); border-radius: 0 8px 8px 0;
  padding: 8px 12px; font-size: 13px; color: var(--text-muted);
}
.thinking-box summary { cursor: pointer; font-weight: 600; color: var(--text); user-select: none; }
.thinking-text { margin-top: 8px; white-space: pre-wrap; word-break: break-word; line-height: 1.55; }

/* Markdown Body */
.md-body { word-break: break-word; line-height: 1.7; }
.md-body p { margin: 0 0 10px; }
.md-body p:last-child { margin-bottom: 0; }
.md-body h1, .md-body h2, .md-body h3, .md-body h4 { margin: 16px 0 8px; line-height: 1.35; }
.md-body ul, .md-body ol { padding-left: 22px; margin: 8px 0; }
.md-body blockquote {
  margin: 10px 0; padding: 6px 12px; border-left: 3px solid var(--border);
  color: var(--text-muted); background: rgba(128,128,128,0.06);
}
.md-body table { border-collapse: collapse; width: 100%; margin: 12px 0; font-size: 13.5px; }
.md-body th, .md-body td { border: 1px solid var(--border); padding: 7px 10px; text-align: left; }
.md-body th { background: var(--bg-elev); }
.md-body code:not(pre code) {
  background: rgba(128,128,128,0.18); padding: 2px 6px; border-radius: 5px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 0.88em;
}
.code-block-wrap {
  margin: 10px 0; border-radius: 10px; overflow: hidden; border: 1px solid var(--border);
  background: var(--code-bg);
}
.code-block-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: 6px 12px; background: rgba(255,255,255,0.05); color: #9ca3af; font-size: 12px;
}
.code-copy-btn {
  background: transparent; border: 1px solid rgba(255,255,255,0.18); color: #cbd5e1;
  border-radius: 5px; padding: 2px 8px; font-size: 11px; cursor: pointer;
}
.code-copy-btn:hover { background: rgba(255,255,255,0.1); }
.code-block-wrap pre { margin: 0; padding: 12px; overflow-x: auto; font-size: 13px; line-height: 1.5; }

.msg-meta {
  display: flex; align-items: center; gap: 10px; margin-top: 6px;
  font-size: 12px; color: var(--text-muted);
}

/* Composer */
.composer-wrap { padding: 10px 16px 18px; background: var(--bg); }
.composer-box {
  max-width: 820px; margin: 0 auto; background: var(--bg-input);
  border: 1px solid var(--border); border-radius: 16px; padding: 10px 14px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.15);
}
.composer-box:focus-within { border-color: var(--accent); }
.composer-textarea {
  width: 100%; border: none; background: transparent; color: var(--text);
  resize: none; outline: none; font-family: inherit; font-size: 15px;
  line-height: 1.5; max-height: 200px; min-height: 26px;
}
.composer-foot {
  display: flex; align-items: center; justify-content: space-between;
  margin-top: 8px; gap: 10px;
}
.composer-hint { font-size: 12px; color: var(--text-muted); }

/* Attachments */
.att-bar {
  display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px;
}
.att-chip {
  display: inline-flex; align-items: center; gap: 7px; padding: 5px 9px;
  border-radius: 8px; border: 1px solid var(--border); background: var(--bg-elev);
  font-size: 12px; max-width: 240px;
}
.att-chip-img { width: 28px; height: 28px; object-fit: cover; border-radius: 5px; }
.att-chip-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.att-chip-del {
  background: transparent; border: none; color: var(--text-muted); cursor: pointer;
  padding: 0 3px; font-size: 14px; line-height: 1;
}
.att-chip-del:hover { color: var(--danger); }
.msg-atts { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 6px; justify-content: flex-end; }
.msg-att-img {
  max-width: 240px; max-height: 200px; border-radius: 10px;
  border: 1px solid var(--border); object-fit: contain; background: var(--bg-elev);
}
.msg-att-doc {
  display: inline-flex; align-items: center; gap: 6px; padding: 6px 10px;
  border-radius: 9px; border: 1px solid var(--border); background: var(--bg-elev); font-size: 12.5px;
}
.composer-box.drag-over { border-color: var(--accent); background: rgba(77,107,254,0.08); }

/* Admin Page */
.admin-container { max-width: 1060px; margin: 0 auto; padding: 24px 16px 60px; }
.card {
  background: var(--bg-elev); border: 1px solid var(--border);
  border-radius: 14px; padding: 20px; margin-bottom: 18px;
}
.card h2 { margin: 0 0 14px; font-size: 17px; }
.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; }
.table-wrap { overflow-x: auto; }
table.data-table { width: 100%; border-collapse: collapse; font-size: 13px; }
table.data-table th, table.data-table td {
  padding: 9px 10px; border-bottom: 1px solid var(--border); text-align: left;
}
table.data-table th { color: var(--text-muted); font-weight: 600; }
.badge {
  display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 11.5px; font-weight: 600;
}
.badge-ok { background: rgba(16,185,129,0.15); color: #34d399; }
.badge-off { background: rgba(239,68,68,0.15); color: #f87171; }

@media (max-width: 768px) {
  .sidebar { position: fixed; top: 0; bottom: 0; left: 0; z-index: 40; }
  .sidebar.collapsed { margin-left: -270px; }
  .quick-prompts, .grid-2 { grid-template-columns: 1fr; }
}
