:root {
  --bg: #0f1116;
  --panel: #171a21;
  --panel-2: #1e222b;
  --border: #2a2f3a;
  --text: #e6e8ec;
  --text-dim: #9aa1ac;
  --accent: #5b8cff;
  --accent-dim: #3a5fc4;
  --danger: #ff6b6b;
  --success: #4fd18a;
  --radius: 10px;
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif;
  font-size: 15px;
  line-height: 1.5;
  overflow-x: hidden;
}

img, svg { max-width: 100%; }

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

header.nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 24px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}

header.nav .brand { font-weight: 600; font-size: 16px; }
header.nav .brand a { color: var(--text); }

header.nav .right { display: flex; flex-wrap: wrap; align-items: center; gap: 14px; color: var(--text-dim); font-size: 13px; }

main {
  max-width: 960px;
  margin: 0 auto;
  padding: 32px 24px 64px;
}

h1 { font-size: 22px; margin: 0 0 6px; }
h2 { font-size: 18px; margin: 0 0 6px; }
p.muted, .muted { color: var(--text-dim); }

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 16px;
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 14px;
}

.server-card {
  display: block;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  transition: border-color .15s ease;
}
.server-card:hover { border-color: var(--accent-dim); text-decoration: none; }
.server-card.locked { opacity: 0.55; cursor: not-allowed; }
.server-card h3 { margin: 0 0 6px; font-size: 16px; color: var(--text); }
.server-card p { margin: 0; font-size: 13px; color: var(--text-dim); }
.badge {
  display: inline-block;
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text-dim);
  margin-top: 8px;
}
.badge.admin { color: #ffb454; border-color: #5a4222; }

.tool-list { display: flex; flex-direction: column; gap: 10px; }
.tool-item {
  display: block;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
}
.tool-item code { color: var(--accent); font-size: 13px; }
.tool-item p { margin: 6px 0 0; font-size: 13px; color: var(--text-dim); }

form.stack { display: flex; flex-direction: column; gap: 16px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field label { font-size: 13px; font-weight: 600; }
.field .hint { font-size: 12px; color: var(--text-dim); }
.field .required { color: var(--danger); margin-left: 4px; }

input[type=text], input[type=email], input[type=password], input[type=number],
textarea, select {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  color: var(--text);
  font-family: inherit;
  font-size: 14px;
}
textarea { resize: vertical; min-height: 90px; font-family: ui-monospace, Consolas, monospace; }
input:focus, textarea:focus, select:focus { outline: none; border-color: var(--accent); }

.checkbox-row { display: flex; align-items: center; gap: 8px; }
.checkbox-row input { width: 16px; height: 16px; }

button, .btn {
  background: var(--accent);
  color: #0b0d12;
  border: none;
  border-radius: 8px;
  padding: 10px 18px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}
button:hover, .btn:hover { background: #4a79ee; text-decoration: none; }
button.secondary, .btn.secondary {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
}
button.secondary:hover { border-color: var(--accent-dim); background: transparent; }

.alert {
  border-radius: var(--radius);
  padding: 12px 16px;
  font-size: 14px;
  margin-bottom: 16px;
}
.alert.error { background: #2a1418; border: 1px solid #5a2a30; color: #ffb4bb; }
.alert.success { background: #12281d; border: 1px solid #235c3d; color: #8fe3b8; }

pre.output {
  background: #0b0d12;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  overflow-x: auto;
  font-family: ui-monospace, Consolas, monospace;
  font-size: 13px;
  white-space: pre-wrap;
  word-break: break-word;
}

.breadcrumbs { font-size: 13px; color: var(--text-dim); margin-bottom: 12px; }
.breadcrumbs a { color: var(--text-dim); }

.auth-wrap {
  max-width: 380px;
  margin: 64px auto;
}
.auth-wrap .card { padding: 28px; }

/* AI chat */
main.chat-main { max-width: 1100px; }

.chat-layout {
  display: grid;
  grid-template-columns: 220px 1fr;
  gap: 16px;
  align-items: start;
}
/* Only pin the layout to viewport height once there's a conversation pane
   that needs internal scrolling; the empty state sizes to its content. */
.chat-layout.has-active {
  height: calc(100vh - 190px);
  min-height: 420px;
}

.chat-sidebar {
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-width: 0;
}
.chat-layout.has-active .chat-sidebar { min-height: 0; height: 100%; }
.chat-session-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
  overflow-y: auto;
  min-height: 0;
  min-width: 0;
}
.chat-session-item {
  display: block;
  padding: 8px 10px;
  border-radius: 8px;
  color: var(--text-dim);
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  border: 1px solid transparent;
}
.chat-session-item:hover { background: var(--panel-2); text-decoration: none; }
.chat-session-item.active { background: var(--panel-2); border-color: var(--border); color: var(--text); }

.chat-pane {
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  min-height: 0;
  min-width: 0;
}
.chat-pane-empty {
  align-items: center;
  justify-content: center;
  padding: 48px 24px;
  min-height: 320px;
}
.chat-empty-state { max-width: 420px; text-align: center; }
.chat-empty-state h1 { font-size: 24px; }
.chat-empty-state p { margin: 10px 0 20px; }

.chat-messages {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  min-height: 0;
}

.msg { display: flex; flex-direction: column; gap: 8px; min-width: 0; }
.msg.user { align-items: flex-end; }
.msg.assistant, .msg.tool { align-items: flex-start; }

.msg .bubble {
  max-width: 78%;
  padding: 11px 15px;
  border-radius: 16px;
  font-size: 14px;
  white-space: pre-wrap;
  word-break: break-word;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
}
.msg.user .bubble { background: var(--accent); color: #0b0d12; border-bottom-right-radius: 4px; }
.msg.assistant .bubble { background: var(--panel-2); border: 1px solid var(--border); border-bottom-left-radius: 4px; }

/* Tool call/result read as their own distinct "system" blobs -- smaller
   and quieter than real message bubbles, but still clearly separated. */
.msg .tool-call {
  display: inline-flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 2px 6px;
  font-size: 12.5px;
  color: var(--text-dim);
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 7px 14px;
  max-width: 92%;
}
.msg .tool-call .tool-call-name { color: var(--text); font-weight: 600; }
.msg .tool-call code {
  color: var(--accent);
  background: none;
  padding: 0;
  font-size: 12px;
  word-break: break-all;
}
.msg.tool .tool-result {
  font-size: 12.5px;
  font-family: ui-monospace, Consolas, monospace;
  color: var(--text-dim);
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent-dim);
  border-radius: 10px;
  padding: 10px 14px;
  max-width: 92%;
  white-space: pre-wrap;
  word-break: break-word;
}

.chat-input {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  padding: 14px;
  border-top: 1px solid var(--border);
}
.chat-input textarea {
  flex: 1;
  min-width: 0;
  min-height: 44px;
  max-height: 160px;
}
.chat-input button {
  flex: 0 0 auto;
  height: 44px;
  padding: 0 20px;
}

/* Tablet */
@media (max-width: 860px) {
  main { max-width: 100%; padding: 24px 20px 48px; }
}

/* Phone */
@media (max-width: 600px) {
  header.nav {
    flex-direction: column;
    align-items: flex-start;
    gap: 10px;
    padding: 12px 16px;
  }
  header.nav .right { width: 100%; justify-content: space-between; }
  main { padding: 20px 14px 40px; }
  .card { padding: 16px; }
  .grid { grid-template-columns: 1fr; }
  h1 { font-size: 20px; }
  form.stack button[type=submit] { width: 100%; text-align: center; }
  .auth-wrap { margin: 24px auto; max-width: 100%; padding: 0 16px; }
  .auth-wrap .card { padding: 20px; }

  .chat-layout, .chat-layout.has-active { grid-template-columns: 1fr; height: auto; gap: 10px; }
  /* Stay a vertical list (not a horizontal-scroll row): a row here would
     need every child to shrink below its content width to avoid pushing
     the whole page wider than the viewport, which flexbox does not do by
     default. A capped-height vertical list has no such failure mode. */
  .chat-sidebar { max-height: 30vh; }
  .chat-session-list { overflow-y: auto; }
  .chat-pane { height: 68vh; }
  .chat-pane-empty { min-height: 260px; padding: 32px 16px; }
  .msg .bubble, .msg .tool-call, .msg.tool .tool-result { max-width: 94%; }
  .chat-input { padding: 10px; }
}
