/* ── Tokens ─────────────────────────────────────────────────────────────── */
:root {
  --accent:       #5865f2;
  --accent-hover: #4752c4;
  --bg:           #ffffff;
  --bg2:          #f4f4f5;
  --bg3:          #e4e4e7;
  --sidebar-bg:   #f9f9fa;
  --border:       #e4e4e7;
  --text:         #18181b;
  --text2:        #52525b;
  --text3:        #a1a1aa;
  --user-bubble:  #5865f2;
  --user-text:    #ffffff;
  --think-bg:     #f0f4ff;
  --think-border: #c7d2fe;
  --error:        #ef4444;
  --shadow:       0 1px 3px rgba(0,0,0,.08), 0 4px 16px rgba(0,0,0,.06);
  --radius:       12px;
  --sidebar-w:    260px;
  --transition:   .2s ease;
}
[data-theme="dark"] {
  --bg:           #18181b;
  --bg2:          #27272a;
  --bg3:          #3f3f46;
  --sidebar-bg:   #111113;
  --border:       #3f3f46;
  --text:         #fafafa;
  --text2:        #a1a1aa;
  --text3:        #71717a;
  --user-bubble:  #4752c4;
  --think-bg:     #1e1e2e;
  --think-border: #3730a3;
  --shadow:       0 1px 3px rgba(0,0,0,.3), 0 4px 16px rgba(0,0,0,.2);
}

/* ── Reset ──────────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; overflow: hidden; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
  color: var(--text);
  background: var(--bg);
  transition: background var(--transition), color var(--transition);
}

/* ── Utilities ──────────────────────────────────────────────────────────── */
.hidden { display: none !important; }

/* ── Buttons ────────────────────────────────────────────────────────────── */
button { cursor: pointer; border: none; background: none; font: inherit; color: inherit; }

.btn-primary {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 9px 18px; border-radius: 8px;
  background: var(--accent); color: #fff;
  font-weight: 500; font-size: 14px;
  transition: background var(--transition), opacity var(--transition);
}
.btn-primary:hover { background: var(--accent-hover); }
.btn-primary:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary.full { width: 100%; }

.btn-ghost {
  display: inline-flex; align-items: center; justify-content: center;
  padding: 6px; border-radius: 6px; color: var(--text2);
  transition: background var(--transition), color var(--transition);
}
.btn-ghost:hover { background: var(--bg3); color: var(--text); }

.btn-send {
  display: flex; align-items: center; justify-content: center;
  width: 36px; height: 36px; flex-shrink: 0; border-radius: 8px;
  background: var(--accent); color: #fff;
  transition: background var(--transition), opacity var(--transition);
}
.btn-send:hover { background: var(--accent-hover); }
.btn-send:disabled { opacity: .4; cursor: not-allowed; }

.btn-danger {
  color: var(--error); font-size: 12px; padding: 4px 8px;
  border-radius: 6px; border: 1px solid transparent;
  transition: background var(--transition), border-color var(--transition);
}
.btn-danger:hover { background: rgba(239,68,68,.1); border-color: var(--error); }

/* ── Login ──────────────────────────────────────────────────────────────── */
.login-screen {
  position: fixed; inset: 0; display: flex; align-items: center; justify-content: center;
  background: var(--bg2);
}
.login-card {
  width: 100%; max-width: 360px; padding: 40px 36px;
  background: var(--bg); border-radius: var(--radius); box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 8px;
}
.login-logo { display: flex; align-items: center; gap: 10px; }
.login-logo h1 { font-size: 18px; font-weight: 700; letter-spacing: -.3px; }
.login-sub { color: var(--text3); font-size: 13px; margin-bottom: 12px; }
.login-card form { display: flex; flex-direction: column; gap: 14px; }
.field { display: flex; flex-direction: column; gap: 5px; }
.field label { font-size: 12px; font-weight: 500; color: var(--text2); }
.field input, textarea, select {
  padding: 9px 12px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--bg2); color: var(--text);
  font: inherit; font-size: 14px; width: 100%;
  transition: border-color var(--transition), background var(--transition);
}
.field input:focus, textarea:focus, select:focus {
  outline: none; border-color: var(--accent);
}
.error-msg { color: var(--error); font-size: 12px; min-height: 16px; }

/* ── App Layout ─────────────────────────────────────────────────────────── */
.app {
  display: flex; height: 100vh; overflow: hidden;
}

/* ── Sidebar ────────────────────────────────────────────────────────────── */
.sidebar {
  width: var(--sidebar-w); flex-shrink: 0;
  display: flex; flex-direction: column;
  background: var(--sidebar-bg); border-right: 1px solid var(--border);
  transition: transform var(--transition);
}
.sidebar-top {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 12px 12px;
  border-bottom: 1px solid var(--border);
}
.sidebar-brand {
  display: flex; align-items: center; gap: 8px;
  font-size: 13px; font-weight: 700; letter-spacing: -.2px; color: var(--text);
}
.chat-list {
  flex: 1; overflow-y: auto; padding: 8px 6px;
  display: flex; flex-direction: column; gap: 2px;
}
.chat-item {
  display: flex; align-items: center; gap: 6px;
  padding: 8px 10px; border-radius: 8px; cursor: pointer;
  transition: background var(--transition);
  min-width: 0;
}
.chat-item:hover { background: var(--bg3); }
.chat-item.active { background: var(--bg3); }
.chat-item-title {
  flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  font-size: 13px; color: var(--text);
}
.chat-item-del {
  flex-shrink: 0; opacity: 0; color: var(--text3); padding: 2px 4px;
  border-radius: 4px; font-size: 14px; line-height: 1;
  transition: opacity var(--transition), color var(--transition);
}
.chat-item:hover .chat-item-del { opacity: 1; }
.chat-item-del:hover { color: var(--error); }

.sidebar-footer {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 12px; border-top: 1px solid var(--border);
  gap: 8px;
}
.user-info { display: flex; align-items: center; gap: 8px; min-width: 0; flex: 1; }
.user-avatar {
  width: 28px; height: 28px; border-radius: 50%; flex-shrink: 0;
  background: var(--accent); color: #fff;
  display: flex; align-items: center; justify-content: center;
  font-size: 12px; font-weight: 600;
}
.user-name { font-size: 13px; color: var(--text2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.footer-actions { display: flex; align-items: center; gap: 2px; flex-shrink: 0; }

/* ── Mobile sidebar ─────────────────────────────────────────────────────── */
.sidebar-overlay {
  display: none; position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 99;
}
.burger { display: none; }

@media (max-width: 680px) {
  .sidebar {
    position: fixed; top: 0; left: 0; bottom: 0; z-index: 100;
    transform: translateX(-100%);
  }
  .sidebar.open { transform: translateX(0); }
  .sidebar-overlay.open { display: block; }
  .burger { display: flex; }
}

/* ── Main ───────────────────────────────────────────────────────────────── */
.main {
  flex: 1; display: flex; flex-direction: column; min-width: 0; overflow: hidden;
}
.main-header {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 16px; border-bottom: 1px solid var(--border);
  background: var(--bg); flex-shrink: 0;
}
.chat-title {
  font-size: 14px; font-weight: 600; color: var(--text);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

/* ── Messages ───────────────────────────────────────────────────────────── */
.messages {
  flex: 1; overflow-y: auto; padding: 20px 16px;
  display: flex; flex-direction: column; gap: 16px;
}
.empty-state {
  flex: 1; display: flex; flex-direction: column; align-items: center;
  justify-content: center; gap: 12px; color: var(--text3); text-align: center;
}

/* Message row */
.msg-row { display: flex; flex-direction: column; gap: 4px; max-width: 760px; width: 100%; }
.msg-row.user { align-self: flex-end; align-items: flex-end; }
.msg-row.assistant { align-self: flex-start; align-items: flex-start; }

.msg-role {
  font-size: 11px; font-weight: 500; color: var(--text3);
  padding: 0 4px;
}

.msg-bubble {
  padding: 10px 14px; border-radius: 12px; max-width: 100%; word-break: break-word;
  line-height: 1.6;
}
.msg-row.user .msg-bubble {
  background: var(--user-bubble); color: var(--user-text);
  border-bottom-right-radius: 4px;
}
.msg-row.assistant .msg-bubble {
  background: var(--bg2); color: var(--text);
  border-bottom-left-radius: 4px;
}

/* Thinking block */
.thinking-block {
  width: 100%; border: 1px solid var(--think-border);
  border-radius: 10px; overflow: hidden; margin-bottom: 6px;
  background: var(--think-bg);
}
.thinking-toggle {
  display: flex; align-items: center; gap: 6px;
  padding: 8px 12px; cursor: pointer; user-select: none;
  font-size: 12px; font-weight: 500; color: var(--text2);
  transition: background var(--transition);
}
.thinking-toggle:hover { background: var(--bg3); }
.thinking-toggle svg { flex-shrink: 0; }
.thinking-body {
  padding: 0 12px; max-height: 0; overflow: hidden;
  transition: max-height .3s ease, padding .3s ease;
  font-size: 12px; color: var(--text2); line-height: 1.6;
  white-space: pre-wrap;
}
.thinking-block.open .thinking-body { max-height: 400px; padding: 10px 12px; overflow-y: auto; }
.thinking-caret {
  margin-left: auto; transition: transform .2s;
  display: flex; align-items: center;
}
.thinking-block.open .thinking-caret { transform: rotate(180deg); }

/* Markdown */
.msg-bubble p { margin-bottom: 8px; }
.msg-bubble p:last-child { margin-bottom: 0; }
.msg-bubble h1,.msg-bubble h2,.msg-bubble h3 { margin: 12px 0 6px; font-weight: 600; }
.msg-bubble ul,.msg-bubble ol { padding-left: 20px; margin-bottom: 8px; }
.msg-bubble li { margin-bottom: 2px; }
.msg-bubble code {
  font-family: "Fira Mono", "Cascadia Code", monospace; font-size: 13px;
  background: rgba(0,0,0,.07); padding: 1px 5px; border-radius: 4px;
}
[data-theme="dark"] .msg-bubble code { background: rgba(255,255,255,.1); }
.msg-bubble pre { background: rgba(0,0,0,.07); border-radius: 8px; padding: 12px; overflow-x: auto; margin-bottom: 8px; }
[data-theme="dark"] .msg-bubble pre { background: rgba(0,0,0,.4); }
.msg-bubble pre code { background: none; padding: 0; }
.msg-bubble table { border-collapse: collapse; width: 100%; margin-bottom: 8px; font-size: 13px; }
.msg-bubble th,.msg-bubble td { border: 1px solid var(--border); padding: 6px 10px; text-align: left; }
.msg-bubble th { background: rgba(0,0,0,.05); font-weight: 600; }
.msg-bubble blockquote { border-left: 3px solid var(--accent); padding-left: 12px; color: var(--text2); margin-bottom: 8px; }
.msg-row.user .msg-bubble a { color: #c7d2fe; }
.msg-row.assistant .msg-bubble a { color: var(--accent); }

/* Streaming cursor */
.streaming-cursor::after {
  content: "▍"; animation: blink 1s step-start infinite; color: var(--accent);
}
@keyframes blink { 50% { opacity: 0; } }

/* Streaming fade-in tokens */
@keyframes streamFade {
  from { opacity: 0; filter: blur(3px); transform: translateY(2px); }
  to   { opacity: 1; filter: blur(0);   transform: translateY(0); }
}
.stream-fade {
  display: inline;
  white-space: pre-wrap;
  animation: streamFade 0.45s ease-out forwards;
}

/* ── Input ──────────────────────────────────────────────────────────────── */
.input-area {
  padding: 12px 16px 14px; border-top: 1px solid var(--border);
  background: var(--bg); flex-shrink: 0;
}
.input-wrap {
  display: flex; flex-direction: column; gap: 8px;
  background: var(--bg2); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 10px 12px;
  transition: border-color var(--transition);
}
.input-wrap:focus-within { border-color: var(--accent); }
#input-box {
  border: none; background: none; padding: 0; resize: none;
  font-size: 14px; line-height: 1.5; color: var(--text);
  max-height: 160px; overflow-y: auto; width: 100%;
}
#input-box:focus { outline: none; }
#input-box:disabled { opacity: .5; cursor: not-allowed; }
#input-box::placeholder { color: var(--text3); }
.input-actions {
  display: flex; align-items: center; justify-content: space-between;
}
.think-toggle {
  display: flex; align-items: center; gap: 6px; cursor: pointer; user-select: none;
}
.think-toggle input { display: none; }
.think-label {
  display: flex; align-items: center; gap: 4px;
  font-size: 12px; color: var(--text3);
  padding: 3px 8px; border-radius: 6px; border: 1px solid transparent;
  transition: all var(--transition);
}
.think-toggle input:checked + .think-label {
  color: var(--accent); border-color: var(--accent);
  background: rgba(88,101,242,.08);
}
.think-toggle:hover .think-label { color: var(--text2); }

/* ── Modal ──────────────────────────────────────────────────────────────── */
.modal { position: fixed; inset: 0; z-index: 200; display: flex; align-items: center; justify-content: center; padding: 16px; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,.5); backdrop-filter: blur(2px); }
.modal-box {
  position: relative; width: 100%; max-width: 580px; max-height: 90vh;
  background: var(--bg); border-radius: var(--radius); box-shadow: var(--shadow);
  display: flex; flex-direction: column; overflow: hidden;
}
.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 18px 20px 14px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.modal-header h2 { font-size: 16px; font-weight: 600; }
.modal-tabs {
  display: flex; gap: 0; padding: 12px 20px 0; border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.tab-btn {
  padding: 8px 16px; font-size: 13px; font-weight: 500; color: var(--text2);
  border-bottom: 2px solid transparent; border-radius: 0;
  transition: color var(--transition), border-color var(--transition);
  margin-bottom: -1px;
}
.tab-btn.active { color: var(--accent); border-bottom-color: var(--accent); }
.tab-btn:hover { color: var(--text); }
.tab-pane { padding: 18px 20px; overflow-y: auto; flex: 1; display: flex; flex-direction: column; gap: 12px; }
.tab-hint { font-size: 12px; color: var(--text3); }
#prompt-textarea { resize: vertical; min-height: 200px; }
.tab-actions { display: flex; align-items: center; gap: 12px; }
.status-msg { font-size: 12px; color: var(--text3); }
.status-msg.ok { color: #22c55e; }
.status-msg.err { color: var(--error); }

/* Users tab */
.user-create {
  display: grid; grid-template-columns: 1fr 1fr auto auto; gap: 8px;
  align-items: end;
}
.user-create input, .user-create select {
  padding: 8px 10px; border-radius: 8px; font-size: 13px;
  border: 1px solid var(--border); background: var(--bg2); color: var(--text);
}
@media (max-width: 520px) {
  .user-create { grid-template-columns: 1fr 1fr; }
}
.users-table { display: flex; flex-direction: column; gap: 2px; }
.user-row {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 10px; border-radius: 8px;
  transition: background var(--transition);
}
.user-row:hover { background: var(--bg2); }
.user-row-name { flex: 1; font-size: 13px; font-weight: 500; }
.user-row-role {
  font-size: 11px; padding: 2px 7px; border-radius: 10px;
  background: var(--bg3); color: var(--text2);
}
.user-row-role.admin { background: rgba(88,101,242,.12); color: var(--accent); }

/* Scrollbar */
::-webkit-scrollbar { width: 4px; height: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--bg3); border-radius: 2px; }

/* Input & select focus */
input:focus, select:focus { outline: none; border-color: var(--accent); }
