:root {
  --bg: #0f1115;
  --bg-soft: #171a21;
  --bg-hover: #1f232d;
  --bg-active: #262b38;
  --border: #262b36;
  --text: #e8eaf0;
  --text-dim: #9aa1b0;
  --text-faint: #6b7280;
  --accent: #6e8bff;
  --accent-soft: #3a4a8a;
  --accent-fg: #ffffff;
  --danger: #e5534b;
  --user-bubble: #2b3a63;
  --code-bg: #0b0d12;
  --radius: 12px;
  --radius-sm: 8px;
  --sidebar-w: 280px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.55;
}

.hidden { display: none !important; }

button {
  font-family: var(--font);
  cursor: pointer;
  border: none;
  background: none;
  color: inherit;
}

/* ---------------- screens (login / password) ---------------- */
.screen {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(1200px 600px at 50% -10%, #1a2030 0%, var(--bg) 60%);
}
.login-card {
  width: 360px;
  max-width: 92vw;
  padding: 36px 32px;
  background: var(--bg-soft);
  border: 1px solid var(--border);
  border-radius: 16px;
  text-align: center;
  box-shadow: 0 20px 60px rgba(0,0,0,.5);
}
.login-logo { font-size: 40px; line-height: 1; margin-bottom: 8px; }
.login-card h1 { margin: 0 0 4px; font-size: 22px; font-weight: 650; }
.login-sub { margin: 0 0 24px; color: var(--text-dim); font-size: 14px; }
.login-card form { display: flex; flex-direction: column; gap: 10px; }
.login-card input {
  width: 100%;
  padding: 11px 14px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 16px;
  outline: none;
}
.login-card input:focus { border-color: var(--accent); }
.login-card button[type="submit"] {
  padding: 11px 14px;
  background: var(--accent);
  color: var(--accent-fg);
  border-radius: var(--radius-sm);
  font-size: 15px;
  font-weight: 600;
}
.login-card button[type="submit"]:hover { filter: brightness(1.08); }
.error { color: var(--danger); font-size: 13px; margin-top: 2px; }

/* ---------------- app layout ---------------- */
.app { display: flex; height: 100vh; overflow: hidden; }

.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg-soft);
  border-right: 1px solid var(--border);
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  z-index: 20;
  transform: translateX(-100%);
  transition: transform .2s ease;
}
.sidebar.open { transform: translateX(0); box-shadow: 8px 0 32px rgba(0,0,0,.35); }
.sidebar-backdrop {
  position: fixed;
  inset: 0;
  z-index: 19;
  background: rgba(0,0,0,.45);
}
.sidebar-header { padding: 14px; border-bottom: 1px solid var(--border); }
.sidebar-top { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.brand { font-size: 16px; font-weight: 650; display: flex; align-items: center; gap: 6px; }
.brand-mark { color: var(--accent); }
.new-thread {
  width: 100%;
  padding: 10px 12px;
  background: var(--bg-hover);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 14px;
  text-align: left;
  transition: background .12s;
}
.new-thread:hover { background: var(--bg-active); }

.threads { flex: 1; overflow-y: auto; padding: 8px; }
.section-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--text-faint);
  padding: 12px 10px 4px;
  font-weight: 600;
}
.thread-item {
  position: relative;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 10px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  color: var(--text-dim);
  transition: background .12s;
}
.thread-item:hover { background: var(--bg-hover); }
.thread-item.active { background: var(--bg-active); color: var(--text); }
.thread-item .t-body { flex: 1; min-width: 0; }
.thread-item .t-title {
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.thread-item .t-time { font-size: 11px; color: var(--text-faint); }
.thread-item .pin-mark { color: var(--text-faint); font-size: 12px; flex-shrink: 0; }
.thread-item .t-actions { display: flex; gap: 2px; opacity: 0; transition: opacity .12s; flex-shrink: 0; }
.thread-item:hover .t-actions { opacity: 1; }
.t-actions button {
  width: 24px; height: 24px;
  border-radius: 6px;
  font-size: 13px;
  display: flex; align-items: center; justify-content: center;
  color: var(--text-dim);
}
.t-actions button:hover { background: var(--bg-active); color: var(--text); }
.t-actions button.del:hover { color: var(--danger); }

.sidebar-footer { padding: 10px 12px; border-top: 1px solid var(--border); }
.settings-btn {
  width: 100%;
  padding: 9px 12px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  color: var(--text-dim);
  display: flex; align-items: center; gap: 8px;
  transition: background .12s;
}
.settings-btn:hover { background: var(--bg-hover); color: var(--text); }

/* ---------------- main ---------------- */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }

.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 18px;
  border-bottom: 1px solid var(--border);
}
.icon-btn {
  width: 34px; height: 34px;
  border-radius: 8px;
  font-size: 16px;
  display: flex; align-items: center; justify-content: center;
  color: var(--text-dim);
}
.icon-btn:hover { background: var(--bg-hover); color: var(--text); }
.thread-title { font-weight: 600; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-spacer { flex: 1; }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px 0 8px;
  display: flex;
  flex-direction: column;
}
.msg-row { display: flex; margin-bottom: 18px; padding: 0 18px; }
.msg-row.user { justify-content: flex-end; }
.msg-row.assistant { justify-content: flex-start; }
.msg-row .avatar {
  width: 28px; height: 28px; flex-shrink: 0;
  border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
  font-size: 15px;
  margin: 2px 10px 0 0;
}
.msg-row.assistant .avatar { background: var(--bg-active); }
.msg-row.user .avatar { background: var(--accent-soft); margin: 2px 0 0 10px; }
.bubble { max-width: 760px; }
.msg-row.user .bubble {
  background: var(--user-bubble);
  padding: 10px 14px;
  border-radius: 16px 16px 4px 16px;
  white-space: pre-wrap;
  word-break: break-word;
}
.msg-row.assistant .bubble { width: 100%; }

/* reasoning block */
.reasoning {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  margin-bottom: 10px;
  background: var(--bg-soft);
  overflow: hidden;
}
.reasoning summary {
  cursor: pointer;
  list-style: none;
  padding: 8px 12px;
  font-size: 13px;
  color: var(--text-dim);
  display: flex; align-items: center; gap: 8px;
  user-select: none;
}
.reasoning summary::-webkit-details-marker { display: none; }
.reasoning summary .chev { transition: transform .15s; font-size: 11px; }
.reasoning[open] summary .chev { transform: rotate(90deg); }
.reasoning .reasoning-body {
  padding: 0 12px 12px;
  color: var(--text-dim);
  font-size: 13.5px;
  white-space: pre-wrap;
  border-top: 1px solid var(--border);
  padding-top: 10px;
}

/* markdown content */
.md { word-break: break-word; }
.md p { margin: 0 0 10px; }
.md p:last-child { margin-bottom: 0; }
.md h1, .md h2, .md h3, .md h4 { margin: 16px 0 8px; line-height: 1.3; }
.md h1:first-child, .md h2:first-child, .md h3:first-child { margin-top: 0; }
.md ul, .md ol { margin: 0 0 10px; padding-left: 22px; }
.md li { margin: 3px 0; }
.md a { color: var(--accent); text-decoration: none; }
.md a:hover { text-decoration: underline; }
.md code {
  font-family: var(--mono);
  font-size: 13px;
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 1px 5px;
}
.md pre {
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px;
  overflow-x: auto;
  margin: 0 0 12px;
}
.md pre code { background: none; border: none; padding: 0; font-size: 13px; }
.md blockquote {
  margin: 0 0 10px;
  padding: 2px 0 2px 12px;
  border-left: 3px solid var(--border);
  color: var(--text-dim);
}
.md table { border-collapse: collapse; margin: 0 0 12px; }
.md th, .md td { border: 1px solid var(--border); padding: 6px 10px; font-size: 14px; }
.md hr { border: none; border-top: 1px solid var(--border); margin: 16px 0; }
.md strong { font-weight: 650; }

/* empty state */
.empty-state {
  flex: 1;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  color: var(--text-faint);
  gap: 10px;
  padding: 40px;
  text-align: center;
}
.empty-state .big { font-size: 42px; }
.empty-state p { max-width: 420px; }

/* ---------------- composer ---------------- */
.composer {
  padding: 10px 18px 16px;
  background: linear-gradient(transparent, var(--bg) 30%);
}
.tool-status {
  font-size: 13px;
  color: var(--text-dim);
  padding: 4px 4px 8px;
  font-family: var(--mono);
  display: flex; align-items: center; gap: 8px;
}
.tool-status .spinner {
  width: 12px; height: 12px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.composer-row {
  display: flex; align-items: flex-end; gap: 8px;
  background: var(--bg-soft);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 10px 10px 10px 16px;
}
.composer-row:focus-within { border-color: var(--accent); }
#input {
  flex: 1;
  background: none;
  border: none;
  outline: none;
  resize: none;
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.5;
  max-height: 200px;
  padding: 4px 0;
}
.send-btn, .stop-btn {
  width: 38px; height: 38px;
  border-radius: 10px;
  font-size: 16px;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.send-btn { background: var(--accent); color: var(--accent-fg); }
.send-btn:hover { filter: brightness(1.08); }
.send-btn:disabled { opacity: .4; cursor: default; }
.stop-btn { background: var(--bg-hover); color: var(--danger); }
.composer-hint { font-size: 11px; color: var(--text-faint); text-align: center; padding-top: 6px; }

/* ---------------- modal ---------------- */
.modal { position: fixed; inset: 0; z-index: 50; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,.55); }
.modal-panel {
  position: absolute;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  width: 560px;
  max-width: 94vw;
  max-height: 86vh;
  display: flex; flex-direction: column;
  background: var(--bg-soft);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: 0 24px 80px rgba(0,0,0,.6);
}
.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}
.modal-header h2 { margin: 0; font-size: 17px; font-weight: 650; }
.modal-body { padding: 20px; overflow-y: auto; }
.modal-body h3 { margin: 0 0 6px; font-size: 14px; }
.muted { color: var(--text-dim); font-size: 13px; margin: 0 0 14px; }
.muted code { font-family: var(--mono); background: var(--code-bg); border: 1px solid var(--border); padding: 0 5px; border-radius: 4px; }
.cmd-group { margin-bottom: 16px; }
.cmd-group h4 {
  margin: 0 0 6px; font-size: 12px;
  text-transform: uppercase; letter-spacing: .05em;
  color: var(--text-faint);
}
.cmd-row {
  display: flex; align-items: baseline; gap: 10px;
  padding: 5px 8px;
  border-radius: 6px;
  font-size: 13.5px;
}
.cmd-row:hover { background: var(--bg-hover); }
.cmd-row code { font-family: var(--mono); color: var(--accent); font-size: 13px; white-space: nowrap; }
.cmd-row .cmd-desc { color: var(--text-dim); }
.modal-section { margin-top: 18px; padding-top: 16px; border-top: 1px solid var(--border); display: flex; flex-direction: column; gap: 8px; }
.ghost-btn {
  padding: 9px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  font-size: 14px;
  text-align: left;
  color: var(--text-dim);
}
.ghost-btn:hover { background: var(--bg-hover); color: var(--text); }
.ghost-btn.danger:hover { color: var(--danger); border-color: var(--danger); }
.confirm-panel { width: 360px; max-width: 92vw; }
.confirm-panel .modal-body { padding: 20px; }
.confirm-text { margin: 0; font-size: 15px; }
.confirm-actions {
  display: flex; justify-content: flex-end; gap: 8px;
  padding: 0 20px 16px;
}

/* ---------------- model settings ---------------- */
.model-row { display: flex; gap: 8px; margin: 10px 0 6px; flex-wrap: wrap; }
.model-select, .model-input {
  flex: 1 1 150px; min-width: 0;
  background: var(--bg-soft); border: 1px solid var(--border); color: var(--text);
  border-radius: var(--radius-sm); padding: 8px 10px; font-size: 16px; font-family: var(--font);
}
.model-select:focus, .model-input:focus { outline: none; border-color: var(--accent); }
#model-status { min-height: 18px; color: var(--text-dim); font-size: 13px; }

/* ---------------- responsive ---------------- */
#sidebar-toggle { display: flex; }

::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 6px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-faint); }
::-webkit-scrollbar-track { background: transparent; }

/* ---------------- attachments ---------------- */
.attach-btn {
  width: 38px; height: 38px;
  border-radius: 10px;
  font-size: 18px;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
  color: var(--text-dim);
}
.attach-btn:hover { background: var(--bg-hover); color: var(--text); }
.attachments-preview {
  display: flex; flex-wrap: wrap; gap: 8px;
  padding: 0 6px 8px;
}
.attach-item { position: relative; }
.attach-item .attach-thumb {
  width: 64px; height: 64px;
  object-fit: cover;
  border-radius: 8px;
  border: 1px solid var(--border);
}
.attach-item .attach-chip {
  max-width: 220px;
  font-size: 12.5px;
  color: var(--text-dim);
  background: var(--bg-soft);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 12px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.attach-item .attach-chip.attach-error { color: var(--danger); border-color: var(--danger); }
.attach-item .attach-remove {
  position: absolute; top: -6px; right: -6px;
  width: 20px; height: 20px;
  border-radius: 50%;
  background: var(--bg-active);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 11px;
  line-height: 1;
  display: flex; align-items: center; justify-content: center;
}
.attach-item .attach-remove:hover { background: var(--danger); color: #fff; }
.msg-attach { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.msg-attach-img {
  max-width: 240px; max-height: 200px;
  border-radius: 8px; border: 1px solid var(--border);
  display: block; object-fit: cover;
}
.msg-attach-chip {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 13px; color: var(--text);
  background: rgba(0,0,0,.25);
  border: 1px solid var(--border);
  border-radius: 8px; padding: 6px 10px;
  text-decoration: none; max-width: 240px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.msg-attach-chip:hover { background: rgba(0,0,0,.4); }
