:root {
  --bg: #0b0f14;
  --bg-soft: #111823;
  --panel: #151d29;
  --panel-2: #1b2534;
  --border: #253044;
  --text: #e6edf5;
  --muted: #8fa3bd;
  --accent: #4f9cf9;
  --accent-2: #7c5cff;
  --ok: #34d399;
  --warn: #fbbf24;
  --err: #f87171;
  --gpu0: #4f9cf9;
  --gpu1: #a78bfa;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

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

body {
  font-family: var(--sans);
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

html { height: 100%; overflow: hidden; }

.app {
  display: grid; grid-template-columns: 288px 1fr 340px;
  height: 100vh; max-height: 100vh; overflow: hidden;
}
/* every grid child must be allowed to shrink below content height,
   otherwise a tall image blows the row past the viewport */
.app > * { min-height: 0; max-height: 100vh; }

/* ---------------- sidebar ---------------- */
.sidebar {
  background: var(--bg-soft);
  border-right: 1px solid var(--border);
  padding: 18px 16px;
  overflow-y: auto;
}

.brand { display: flex; align-items: center; margin-bottom: 6px; }
.brand-logo {
  width: 100%;
  max-width: 220px;
  height: auto;
  display: block;
}
.brand-sub { font-size: 11px; color: var(--muted); margin-bottom: 20px; }

.section-label {
  font-size: 10px; text-transform: uppercase; letter-spacing: 0.09em;
  color: var(--muted); font-weight: 700; margin: 18px 0 9px;
}

.model-card {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: 9px; padding: 11px 12px; margin-bottom: 9px;
  border-left: 3px solid var(--border);
}
.model-card.gpu0 { border-left-color: var(--gpu0); }
.model-card.gpu1 { border-left-color: var(--gpu1); }
.model-card.svc  { border-left-color: #2dd4a7; }
.model-card.down { opacity: 0.55; border-left-color: var(--err); }

.model-row { display: flex; justify-content: space-between; align-items: center; }
.model-name { font-size: 13px; font-weight: 600; }
.model-meta { font-size: 11px; color: var(--muted); margin-top: 3px; font-family: var(--mono); }

.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--err); }
.dot.up { background: var(--ok); box-shadow: 0 0 8px rgba(52,211,153,.55); }

.tool-chip {
  display: inline-flex; align-items: center; gap: 5px;
  font-size: 11px; font-family: var(--mono);
  background: var(--panel); border: 1px solid var(--border);
  padding: 4px 8px; border-radius: 6px; margin: 0 4px 5px 0;
  cursor: pointer; user-select: none; transition: all .15s;
}
.tool-chip:hover { border-color: var(--accent); }
.tool-chip.off { opacity: .38; text-decoration: line-through; }
.tool-chip .tdot { width: 5px; height: 5px; border-radius: 50%; background: var(--ok); }
.tool-chip.off .tdot { background: var(--muted); }

.field { margin-bottom: 12px; }
.field label { display: block; font-size: 11px; color: var(--muted); margin-bottom: 5px; }
.field input, .field select {
  width: 100%; background: var(--panel); color: var(--text);
  border: 1px solid var(--border); border-radius: 7px;
  padding: 8px 10px; font-size: 12px; font-family: var(--mono);
}
.field input:focus, .field select:focus { outline: none; border-color: var(--accent); }

.switch { display: flex; align-items: center; gap: 8px; font-size: 12px; margin-bottom: 9px; }
.switch input { accent-color: var(--accent); width: 15px; height: 15px; }

/* ---------------- chat ---------------- */
/* min-height:0 is critical: without it grid/flex children refuse to shrink
   below their content height, so tall content (images) pushes the composer
   off-screen and the messages pane never scrolls. */
.chat {
  display: flex; flex-direction: column; min-width: 0; min-height: 0;
  height: 100vh; overflow: hidden; background: var(--bg);
}

.chat-head {
  padding: 13px 22px; border-bottom: 1px solid var(--border);
  display: flex; justify-content: space-between; align-items: center;
  background: var(--bg-soft);
}
.chat-title { font-size: 13px; font-weight: 600; }
.chat-sub { font-size: 11px; color: var(--muted); margin-top: 2px; font-family: var(--mono); }

.btn {
  background: var(--panel-2); color: var(--text); border: 1px solid var(--border);
  padding: 7px 13px; border-radius: 7px; font-size: 12px; cursor: pointer;
  transition: all .15s; font-family: var(--sans);
}
.btn:hover { border-color: var(--accent); }
.btn.primary {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  border: none; font-weight: 600;
}
.btn:disabled { opacity: .5; cursor: not-allowed; }

.messages { flex: 1 1 0; min-height: 0; overflow-y: auto; padding: 22px; }

.msg { margin-bottom: 20px; max-width: 860px; }
.msg-head {
  display: flex; align-items: center; gap: 8px;
  font-size: 11px; color: var(--muted); margin-bottom: 7px;
  text-transform: uppercase; letter-spacing: .06em; font-weight: 600;
}
.avatar {
  width: 21px; height: 21px; border-radius: 5px; display: grid; place-items: center;
  font-size: 10px; font-weight: 700; text-transform: none;
}
.avatar.user { background: var(--panel-2); }
.avatar.bot { background: linear-gradient(135deg, var(--accent), var(--accent-2)); }

.msg-body {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: 10px; padding: 13px 15px; font-size: 14px; line-height: 1.62;
  white-space: pre-wrap; word-wrap: break-word;
}
.msg.user .msg-body { background: var(--panel-2); }

.badge {
  display: inline-block; font-size: 10px; font-family: var(--mono);
  padding: 2px 7px; border-radius: 5px; background: var(--panel-2);
  border: 1px solid var(--border); color: var(--muted); text-transform: none;
}
.badge.gpu0 { color: var(--gpu0); border-color: rgba(79,156,249,.4); }
.badge.gpu1 { color: var(--gpu1); border-color: rgba(167,139,250,.4); }

.trace { margin: 9px 0; border-left: 2px solid var(--border); padding-left: 12px; }
.trace-item {
  font-family: var(--mono); font-size: 11.5px; padding: 7px 0;
  color: var(--muted); border-bottom: 1px dashed var(--border);
}
.trace-item:last-child { border-bottom: none; }
.trace-name { color: var(--accent); font-weight: 600; }
.trace-ok { color: var(--ok); }
.trace-err { color: var(--err); }
.trace-args {
  display: block; margin-top: 4px; color: #6b7f99;
  white-space: pre-wrap; word-break: break-all; font-size: 10.5px;
}

.composer { border-top: 1px solid var(--border); padding: 14px 22px; background: var(--bg-soft); }
.composer-inner { display: flex; gap: 10px; align-items: flex-end; }
textarea#input {
  flex: 1; background: var(--panel); color: var(--text);
  border: 1px solid var(--border); border-radius: 10px;
  padding: 11px 13px; font-size: 14px; font-family: var(--sans);
  resize: none; min-height: 46px; max-height: 190px; line-height: 1.5;
}
textarea#input:focus { outline: none; border-color: var(--accent); }
.composer-hint { font-size: 10.5px; color: var(--muted); margin-top: 7px; }
.attach-row { display: flex; gap: 8px; align-items: center; margin-bottom: 9px; flex-wrap: wrap; }
.thumb { height: 40px; border-radius: 6px; border: 1px solid var(--border); }

/* ---------------- inspector ---------------- */
.inspector {
  background: var(--bg-soft); border-left: 1px solid var(--border);
  padding: 18px 16px; overflow-y: auto;
}
.event {
  font-family: var(--mono); font-size: 11px; padding: 8px 10px;
  border-radius: 7px; background: var(--panel); border: 1px solid var(--border);
  margin-bottom: 7px; border-left: 3px solid var(--muted);
}
.event.route { border-left-color: var(--accent); }
.event.tool_call { border-left-color: var(--warn); }
.event.tool_result { border-left-color: var(--ok); }
.event.vision_start, .event.vision_done { border-left-color: var(--gpu1); }
.event.error, .event.vision_error { border-left-color: var(--err); }
.event-type { font-weight: 700; text-transform: uppercase; font-size: 9.5px; letter-spacing: .07em; }
.event-body { color: var(--muted); margin-top: 4px; word-break: break-word; }

.stat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 14px; }
.stat {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: 8px; padding: 9px 11px;
}
.stat-val { font-size: 17px; font-weight: 700; font-family: var(--mono); }
.stat-lbl { font-size: 10px; color: var(--muted); margin-top: 2px; }

.typing { display: inline-flex; gap: 4px; }
.typing span {
  width: 6px; height: 6px; border-radius: 50%; background: var(--muted);
  animation: blink 1.3s infinite;
}
.typing span:nth-child(2) { animation-delay: .2s; }
.typing span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%,60%,100% { opacity:.25 } 30% { opacity:1 } }

::-webkit-scrollbar { width: 9px; height: 9px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 5px; }
::-webkit-scrollbar-thumb:hover { background: #32405a; }

/* markdown rendering inside assistant messages */
.md-img {
  display: block;
  max-width: min(480px, 100%);
  max-height: 55vh;
  object-fit: contain;
  border-radius: 10px;
  margin: 8px 0 4px;
  border: 1px solid rgba(255,255,255,0.08);
  cursor: zoom-in;
}
.img-wrap { display: inline-block; }
.img-bar { display: flex; gap: 8px; margin: 0 0 8px; }
.img-btn {
  display: inline-block;
  background: var(--panel-2, #1b2333);
  border: 1px solid rgba(255,255,255,0.12);
  color: #cfe3ff;
  border-radius: 6px;
  padding: 4px 10px;
  font-size: 11.5px;
  text-decoration: none;
  cursor: pointer;
  font-family: var(--sans, sans-serif);
}
.img-btn:hover { border-color: #6cb8ff; }

/* past chats */
.sess-item {
  background: var(--panel, #131a29);
  border: 1px solid var(--border, #24304a);
  border-radius: 8px;
  padding: 8px 10px;
  margin-bottom: 6px;
  cursor: pointer;
  transition: border-color .15s;
}
.sess-item:hover { border-color: var(--accent, #4f8cff); }
.sess-item.active { border-color: var(--accent, #4f8cff); background: var(--panel-2, #1b2333); }
.sess-row { display: flex; justify-content: space-between; align-items: center; }
.sess-name {
  font-size: 12px; font-weight: 600;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 190px;
}
.sess-x {
  color: var(--muted, #7d8aa5); font-size: 11px; padding: 2px 5px;
  border-radius: 4px; flex: 0 0 auto;
}
.sess-x:hover { color: #ff6b6b; background: rgba(255,107,107,0.12); }
.history-note {
  text-align: center; color: var(--muted, #7d8aa5);
  font-size: 11px; font-family: var(--mono, monospace);
  margin: 4px 0 14px;
}

/* full-screen lightbox */
.lightbox {
  position: fixed; inset: 0; z-index: 1000;
  background: rgba(4,8,16,0.92);
  display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 14px;
  cursor: zoom-out;
}
.lightbox img {
  max-width: 92vw; max-height: 84vh;
  border-radius: 10px;
  box-shadow: 0 12px 60px rgba(0,0,0,0.7);
  cursor: default;
}
.lb-bar { display: flex; gap: 10px; }
.lightbox .img-btn { font-size: 13px; padding: 7px 14px; }
.md-code {
  background: rgba(0,0,0,0.35);
  border: 1px solid rgba(255,255,255,0.07);
  border-radius: 8px;
  padding: 10px 12px;
  margin: 8px 0;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 12px;
  overflow-x: auto;
  white-space: pre;
}
.msg-body code {
  background: rgba(255,255,255,0.08);
  border-radius: 4px;
  padding: 1px 5px;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 12px;
}
.msg-body a { color: #6cb8ff; }

/* ---------- copy buttons ---------- */
.copy-btn {
  background: rgba(255,255,255,0.06);
  border: 1px solid rgba(255,255,255,0.12);
  color: #9aa7b8;
  border-radius: 6px;
  font-size: 11px;
  padding: 2px 8px;
  cursor: pointer;
  transition: all .15s;
  font-family: inherit;
}
.copy-btn:hover { color: #e8eef6; border-color: rgba(255,255,255,0.3); }
.copy-btn.copied { color: #2dd4a7; border-color: #2dd4a7; }
.msg-copy {
  margin-left: auto;
  opacity: 0;
}
.msg:hover .msg-copy { opacity: 1; }
.msg-head { display: flex; align-items: center; gap: 8px; }

/* ---------- code blocks with header bar ---------- */
.code-wrap { display: block; margin: 8px 0; }
.code-bar {
  display: flex; align-items: center; justify-content: space-between;
  background: rgba(0,0,0,0.5);
  border: 1px solid rgba(255,255,255,0.07);
  border-bottom: none;
  border-radius: 8px 8px 0 0;
  padding: 4px 10px;
}
.code-lang {
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 11px; color: #7c8ba1; text-transform: lowercase;
}
.code-wrap .md-code { margin: 0; border-radius: 0 0 8px 8px; }

/* ---------- file chips (downloadable outputs in chat) ---------- */
.file-chip {
  display: inline-flex; align-items: center; gap: 8px;
  background: rgba(45,212,167,0.07);
  border: 1px solid rgba(45,212,167,0.25);
  border-radius: 8px;
  padding: 6px 10px;
  margin: 4px 0;
  font-size: 12px;
}
.fc-name {
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  color: #d5e2f0;
  max-width: 260px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

/* ---------- sidebar: new chat + rename ---------- */
.newchat-btn { width: 100%; margin-bottom: 8px; text-align: center; }
.sess-acts { display: flex; gap: 6px; flex-shrink: 0; }
.sess-ren {
  color: #5f6f83; cursor: pointer; font-size: 12px;
  padding: 0 2px; transition: color .15s;
}
.sess-ren:hover { color: #6cb8ff; }

/* ---------- sidebar: output files ---------- */
.file-item {
  display: flex; align-items: center; gap: 7px;
  background: rgba(255,255,255,0.03);
  border: 1px solid rgba(255,255,255,0.06);
  border-radius: 8px;
  padding: 6px 9px;
  margin-bottom: 6px;
  font-size: 12px;
}
.file-ico { flex-shrink: 0; }
.file-name {
  flex: 1 1 auto; min-width: 0;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  color: #c9d6e4;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 11px;
}
.file-size { color: #5f6f83; font-size: 10px; flex-shrink: 0; }
.file-act {
  color: #7c8ba1; text-decoration: none; flex-shrink: 0;
  padding: 0 2px; transition: color .15s;
}
.file-act:hover { color: #6cb8ff; }

/* ---------- user badge ---------- */
.user-badge {
  display: flex; align-items: center; gap: 9px;
  background: rgba(255,255,255,0.04);
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: 10px; padding: 8px 10px;
}
.user-avatar {
  width: 28px; height: 28px; border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  display: grid; place-items: center; font-weight: 700; font-size: 13px;
  flex-shrink: 0;
}
.user-info { display: flex; flex-direction: column; min-width: 0; flex: 1 1 auto; }
.user-name {
  font-size: 12px; font-weight: 600; color: #e8eef6;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.user-role { font-size: 10px; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; }
.user-role.admin { color: #2dd4a7; }
.user-act {
  color: #7c8ba1; cursor: pointer; text-decoration: none;
  font-size: 14px; padding: 0 3px; transition: color .15s; flex-shrink: 0;
}
.user-act:hover { color: #6cb8ff; }

/* ---------- file delete ---------- */
.file-del:hover { color: #ff6b6b; }

/* ---------- login overlay ---------- */
.login-overlay {
  position: fixed; inset: 0; z-index: 2000;
  background: rgba(6,10,16,0.94);
  backdrop-filter: blur(6px);
  display: none; align-items: center; justify-content: center;
}
.login-card {
  width: 340px; padding: 32px 28px;
  background: var(--panel, #121821);
  border: 1px solid rgba(255,255,255,0.09);
  border-radius: 16px;
  box-shadow: 0 24px 80px rgba(0,0,0,0.6);
  display: flex; flex-direction: column; gap: 12px;
}
.login-logo { width: 100%; max-width: 240px; margin: 0 auto 4px; display: block; }
.login-sub {
  text-align: center; font-size: 12px; color: var(--muted); margin-bottom: 6px;
}
.login-card input {
  width: 100%; padding: 10px 12px;
  background: rgba(0,0,0,0.3);
  border: 1px solid rgba(255,255,255,0.12);
  border-radius: 8px; color: #e8eef6; font-size: 13px; font-family: inherit;
}
.login-card input:focus { outline: none; border-color: var(--accent); }
.login-err { color: #ff6b6b; font-size: 11px; min-height: 14px; text-align: center; }
.login-card .btn { width: 100%; text-align: center; padding: 10px; font-size: 13px; }

@media (max-width: 1200px) {
  .app { grid-template-columns: 250px 1fr; }
  .inspector { display: none; }
}
