:root {
  --bg: #0f1420;
  --bg-secondary: #171e2e;
  --bg-elevated: #1f2940;
  --border: #2a3550;
  --text: #e6eaf2;
  --text-muted: #8b96ad;
  --accent: #4f8cff;
  --accent-hover: #6fa0ff;
  --user-bubble: #2b4a86;
  --assistant-bubble: #232d47;
  --error: #ff6b6b;
  --success: #3ddc84;
  --radius: 10px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body { height: 100%; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.5;
}

.hidden { display: none !important; }

/* ---------- Login ---------- */
.login-view {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.login-card {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 32px;
  width: 100%;
  max-width: 380px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.login-card h1 { font-size: 24px; }
.login-sub { color: var(--text-muted); margin-bottom: 8px; }

input[type="password"], input[type="text"], textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  resize: vertical;
}

input:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.error-text { color: var(--error); font-size: 14px; }

/* ---------- Buttons ---------- */
.btn {
  padding: 10px 16px;
  border: none;
  border-radius: 8px;
  background: var(--bg-elevated);
  color: var(--text);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
}
.btn:hover { background: var(--border); }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-ghost { background: transparent; color: var(--text-muted); }
.btn-ghost:hover { background: var(--bg-elevated); color: var(--text); }
.btn-small { padding: 6px 10px; font-size: 13px; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* ---------- App layout ---------- */
.app-view {
  display: grid;
  grid-template-columns: 280px 1fr;
  height: 100vh;
}

@media (max-width: 720px) {
  .app-view { grid-template-columns: 1fr; }
  .sidebar { display: none; }
}

/* ---------- Sidebar ---------- */
.sidebar {
  background: var(--bg-secondary);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px;
  border-bottom: 1px solid var(--border);
}

.sidebar-header h2 { font-size: 16px; }

.conversation-list {
  list-style: none;
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.conversation-item {
  padding: 10px 12px;
  border-radius: 8px;
  cursor: pointer;
  margin-bottom: 2px;
  position: relative;
}
.conversation-item:hover { background: var(--bg-elevated); }
.conversation-item.active { background: var(--bg-elevated); outline: 1px solid var(--accent); }
.conversation-item-title { font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; padding-right: 46px; }
.conversation-item-preview { font-size: 12px; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 2px; }

.conversation-item-actions {
  position: absolute;
  top: 8px;
  right: 8px;
  display: flex;
  gap: 2px;
  opacity: 0;
  transition: opacity .1s;
}
.conversation-item:hover .conversation-item-actions { opacity: 1; }
.conversation-action {
  border: none;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 13px;
  padding: 2px 5px;
  border-radius: 4px;
  line-height: 1;
}
.conversation-action:hover { background: var(--bg-secondary); color: var(--text); }

.conversation-badges { display: flex; gap: 4px; margin-top: 4px; flex-wrap: wrap; }
.badge {
  font-size: 10px;
  padding: 1px 6px;
  border-radius: 6px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  color: var(--text-muted);
  white-space: nowrap;
}
.badge.batch { background: #3a2f66; color: #c9b8ff; border-color: #4a3d80; }
.badge.model { background: #123a2e; color: #7ce0b0; border-color: #1d5a45; }

.sidebar-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
}

.footer-actions {
  display: flex;
  align-items: center;
  gap: 6px;
}

.status-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--text-muted);
  display: inline-block;
}
.status-dot.ok { background: var(--success); }
.status-dot.down { background: var(--error); }

/* ---------- Chat main ---------- */
.chat-main {
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-secondary);
}

.chat-header h3 { font-size: 16px; }

/* ---------- Live / Batch mode toggle ---------- */
.chat-header-controls {
  display: flex;
  align-items: center;
  gap: 10px;
}

.mode-toggle {
  display: flex;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--bg-elevated);
}

.mode-btn {
  appearance: none;
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 13px;
  padding: 6px 12px;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s ease, color 0.15s ease;
}

.mode-btn:hover { color: var(--text); }

.mode-btn.active {
  background: var(--accent);
  color: #fff;
}

.footer-actions { position: relative; }

.footer-menu { position: relative; display: inline-block; }

.menu-popover {
  position: absolute;
  bottom: calc(100% + 6px);
  right: 0;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 4px;
  min-width: 130px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 6px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.4);
  z-index: 150;
}

.menu-popover .btn { text-align: left; white-space: nowrap; }

.batch-badge {
  display: inline-block;
  min-width: 16px;
  margin-left: 5px;
  padding: 0 5px;
  border-radius: 9px;
  background: #3a2f66;
  border: 1px solid #4a3d80;
  color: #c9b8ff;
  font-size: 10px;
  line-height: 15px;
  text-align: center;
  vertical-align: 1px;
}

@media (max-width: 720px) {
  .chat-header { flex-wrap: wrap; gap: 8px; }
}

/* ---------- Messages ---------- */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.message {
  max-width: 78%;
  padding: 10px 14px;
  border-radius: var(--radius);
  white-space: pre-wrap;
  word-wrap: break-word;
  position: relative;
}
.message.user { align-self: flex-end; background: var(--user-bubble); }
.message.assistant { align-self: flex-start; background: var(--assistant-bubble); }
.message.system { align-self: center; background: transparent; border: 1px dashed var(--border); color: var(--text-muted); font-size: 13px; }

.message-text { white-space: normal; }
.message-text p { margin: 0 0 8px; }
.message-text p:last-child { margin-bottom: 0; }
.message-text pre {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px;
  overflow-x: auto;
  font-size: 13px;
}
.message-text code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.9em;
}
.message-text :not(pre) > code {
  background: var(--bg);
  padding: 1px 5px;
  border-radius: 4px;
}
.message-text .katex-display { margin: 8px 0; overflow-x: auto; overflow-y: hidden; }

.message-copy {
  display: inline-block;
  margin-top: 6px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 11px;
  cursor: pointer;
  padding: 2px 0;
}
.message-copy:hover { color: var(--text); }

.message-delete {
  display: inline-block;
  margin-top: 6px;
  margin-left: 10px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 11px;
  cursor: pointer;
  padding: 2px 0;
}
.message-delete:hover { color: var(--error); }

.message-model {
  display: block;
  font-size: 11px;
  color: var(--accent-hover);
  margin-bottom: 4px;
  font-weight: 700;
}

.message-date {
  display: block;
  font-size: 10px;
  color: var(--text-muted);
  margin-top: 2px;
}

.message-websearch {
  display: inline-block;
  font-size: 10px;
  padding: 1px 6px;
  margin-bottom: 4px;
  border-radius: 4px;
  border: 1px solid #1d5a45;
  background: #123a2e;
  color: #7ce0b0;
  cursor: help;
}

.message-err { color: var(--error); font-size: 13px; margin-top: 4px; }

/* ---------- Chat form ---------- */
.chat-form {
  display: flex;
  gap: 10px;
  padding: 14px 20px;
  border-top: 1px solid var(--border);
  background: var(--bg-secondary);
}

.chat-form textarea { flex: 1; }

.websearch-toggle {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--text-muted);
  white-space: nowrap;
  cursor: pointer;
  user-select: none;
}
.websearch-toggle input { width: auto; margin: 0; }

/* ---------- Model picker ---------- */
.model-picker { position: relative; }

.model-dropdown {
  position: absolute;
  right: 0;
  top: 40px;
  width: 280px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.4);
  z-index: 100;
}

.dropdown-hint { font-size: 12px; color: var(--text-muted); margin-bottom: 8px; }

.model-check { display: flex; align-items: center; gap: 8px; padding: 4px 0; font-size: 14px; cursor: pointer; }
.model-check input { width: auto; }
.model-price { color: var(--text-muted); font-size: 12px; margin-left: auto; white-space: nowrap; }

.model-custom { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--text-muted); margin-top: 10px; }
.model-custom input { margin-top: 4px; }

/* ---------- Import modal ---------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(5, 8, 15, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 200;
}

.modal {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  width: 100%;
  max-width: 560px;
  max-height: 85vh;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 20px;
  overflow-y: auto;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.modal-hint { font-size: 13px; color: var(--text-muted); }
.modal-hint code {
  background: var(--bg-elevated);
  padding: 1px 5px;
  border-radius: 4px;
  font-size: 12px;
}

.modal textarea {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
  min-height: 160px;
}

.account-code {
  font-family: monospace;
  font-size: 12px;
  word-break: break-all;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px;
  user-select: all;
}

/* Client-account creation row (Settings modal). */
.account-create-row {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
  margin: 8px 0;
}

.account-create-row input {
  flex: 1 1 140px;
  min-width: 0;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font-size: 13px;
}

.import-status { font-size: 13px; min-height: 18px; }
.import-status.ok { color: var(--success); }
.import-status.err { color: var(--error); }

.modal-actions { display: flex; justify-content: flex-end; gap: 8px; }

.modal-divider { border: none; border-top: 1px solid var(--border); margin: 16px 0 12px; }

.modal-field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 12px;
  color: var(--text-muted);
}
.modal-field input { margin-top: 2px; }
.modal-field select {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
}
.settings-hint { font-weight: 400; color: var(--text-muted); font-size: 11px; }


.batch-jobs { margin-top: 8px; display: flex; flex-direction: column; gap: 6px; }
.batch-job-row {
  font-size: 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.batch-job-status { font-weight: 700; text-transform: uppercase; }
.batch-job-status.completed { color: var(--success); }
.batch-job-status.error, .batch-job-status.failed { color: var(--error); }
.batch-job-status.pending { color: var(--accent-hover); }
.batch-job-error {
  flex-basis: 100%;
  color: var(--error);
  font-size: 11px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.batch-clear {
  background: none;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 11px;
  line-height: 1;
  padding: 3px 6px;
}
.batch-clear:hover { color: var(--error); border-color: var(--error); }
.spinning { animation: sync-spin 0.9s linear infinite; }
@keyframes sync-spin { to { transform: rotate(360deg); } }
.reasoning-select {
  background: var(--bg-elevated, #1c1c22);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 12px;
  padding: 4px 6px;
}

.cache-pings-wrap { margin-top: 14px; border-top: 1px solid var(--border); padding-top: 10px; }
.cache-pings-title { margin: 0 0 4px; font-size: 13px; }
.cache-pings-hint { margin: 0 0 8px; font-size: 11px; color: var(--text-muted); }
.cache-pings { display: flex; flex-direction: column; gap: 4px; max-height: 180px; overflow-y: auto; }
.cache-ping-row {
  display: flex; align-items: baseline; gap: 8px;
  font-size: 12px; flex-wrap: wrap;
}
.cache-ping-mark { font-weight: 700; }
.cache-ping-mark.ok { color: var(--success); }
.cache-ping-mark.fail { color: var(--error); }
.cache-ping-error { flex-basis: 100%; color: var(--error); font-size: 11px; }
.cache-ping-empty { font-size: 12px; color: var(--text-muted); }
.cache-ping-enabled { font-size: 11px; color: var(--text-muted); margin-bottom: 4px; }