:root {
  --bg: #f3f4f6;
  --surface: #ffffff;
  --surface-2: #f8f9fb;
  --surface-3: #eef0f4;
  --border: #e2e5ea;
  --border-strong: #cfd4dc;
  --text: #171a1f;
  --text-dim: #5f6b7a;
  --text-faint: #8b95a3;
  --accent: #2f6fed;
  --accent-hover: #245cd0;
  --accent-weak: #e9f0fe;
  --ok: #17853d;
  --ok-weak: #e7f6ec;
  --warn: #b7791f;
  --warn-weak: #fdf3e2;
  --bad: #c0392b;
  --bad-weak: #fdeceb;
  --radius: 8px;
  --radius-sm: 6px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 4px 12px rgba(16, 24, 40, 0.05);
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: "PingFang SC", "Microsoft YaHei", "Hiragino Sans GB", -apple-system,
    BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 14px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}

button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }

/* ------------------------------------------------------------ login page */

.login-page {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    radial-gradient(1100px 500px at 50% -10%, #e7ecf5 0%, rgba(231, 236, 245, 0) 60%),
    var(--bg);
}

.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 28px 26px 24px;
}

.login-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 22px;
}

.login-brand .mark { width: 32px; height: 32px; }

.login-brand h1 {
  margin: 0;
  font-size: 17px;
  font-weight: 650;
  letter-spacing: 0;
}

.login-brand p {
  margin: 1px 0 0;
  font-size: 12px;
  color: var(--text-faint);
}

.field { margin-bottom: 14px; }

.field label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-dim);
  margin-bottom: 6px;
}

.input {
  width: 100%;
  height: 40px;
  padding: 0 12px;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-weak);
}

.login-error {
  min-height: 18px;
  font-size: 12.5px;
  color: var(--bad);
  margin: 2px 0 10px;
}

/* ------------------------------------------------------------ app shell */

.app {
  height: 100%;
  display: grid;
  grid-template-rows: 56px 1fr;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  gap: 9px;
  flex: 0 0 auto;
}

.brand .mark { width: 26px; height: 26px; }

.brand .name {
  font-size: 14.5px;
  font-weight: 650;
  white-space: nowrap;
}

.tabs {
  display: flex;
  gap: 4px;
  background: var(--surface-3);
  padding: 3px;
  border-radius: var(--radius-sm);
}

.tab {
  display: flex;
  align-items: center;
  gap: 7px;
  border: 0;
  background: transparent;
  color: var(--text-dim);
  padding: 6px 14px;
  border-radius: 5px;
  font-size: 13.5px;
  font-weight: 550;
  white-space: nowrap;
  transition: background 0.15s, color 0.15s;
}

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

.tab.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 1px 2px rgba(16, 24, 40, 0.08);
}

.tab svg { width: 15px; height: 15px; }

.topbar-right {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 12px;
}

.pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  font-weight: 550;
  color: var(--text-dim);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 4px 10px;
  white-space: nowrap;
}

.dot { width: 7px; height: 7px; border-radius: 50%; background: var(--text-faint); flex: 0 0 auto; }
.dot.ok { background: var(--ok); }
.dot.warn { background: var(--warn); }
.dot.bad { background: var(--bad); }

.body { min-height: 0; display: flex; }

.sidebar {
  width: 292px;
  flex: 0 0 292px;
  border-right: 1px solid var(--border);
  background: var(--surface);
  overflow-y: auto;
  padding: 16px;
}

.main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg);
}

.section + .section { margin-top: 18px; }

.section-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 11.5px;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text-faint);
  margin-bottom: 8px;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
}

.card + .card { margin-top: 8px; }

/* buttons */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  height: 36px;
  padding: 0 14px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font-size: 13.5px;
  font-weight: 550;
  transition: background 0.15s, border-color 0.15s;
  white-space: nowrap;
}

.btn:hover { background: var(--surface-2); }
.btn svg { width: 15px; height: 15px; }

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn-primary:disabled { opacity: 0.55; cursor: default; }

.btn-block { width: 100%; }

.btn-ghost {
  border-color: transparent;
  background: transparent;
  color: var(--text-dim);
}
.btn-ghost:hover { background: var(--surface-3); color: var(--text); }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-dim);
}
.icon-btn:hover { background: var(--surface-3); color: var(--text); }
.icon-btn svg { width: 16px; height: 16px; }

/* model list */

.search {
  width: 100%;
  height: 32px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  outline: none;
  font-size: 13px;
}
.search:focus { border-color: var(--accent); background: var(--surface); }

.model-list {
  margin-top: 8px;
  max-height: 240px;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}

.model-item {
  display: block;
  width: 100%;
  text-align: left;
  border: 0;
  background: transparent;
  padding: 7px 10px;
  font-size: 12.5px;
  font-family: var(--mono);
  color: var(--text-dim);
  border-bottom: 1px solid var(--border);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.model-item:last-child { border-bottom: 0; }
.model-item:hover { background: var(--surface-2); color: var(--text); }
.model-item.active { background: var(--accent-weak); color: var(--accent); font-weight: 600; }

/* key-value rows */

.kv { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; font-size: 13px; }
.kv + .kv { margin-top: 6px; }
.kv .k { color: var(--text-dim); }
.kv .v { font-weight: 600; font-variant-numeric: tabular-nums; }

.meter {
  height: 6px;
  border-radius: 999px;
  background: var(--surface-3);
  overflow: hidden;
  margin-top: 9px;
}
.meter > span { display: block; height: 100%; background: var(--ok); border-radius: 999px; }
.meter.warn > span { background: var(--warn); }
.meter.bad > span { background: var(--bad); }

.hint { font-size: 12px; color: var(--text-faint); }

/* chat */

.chat-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 18px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.chat-head .title { font-size: 14px; font-weight: 650; }
.chat-head .sub { font-size: 12px; color: var(--text-faint); font-family: var(--mono); }

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

.msg {
  max-width: 780px;
  margin: 0 auto 16px;
  display: flex;
  gap: 10px;
}

.msg .avatar {
  flex: 0 0 26px;
  width: 26px;
  height: 26px;
  border-radius: 6px;
  display: grid;
  place-items: center;
  font-size: 11px;
  font-weight: 700;
  color: #fff;
  background: var(--text-faint);
}
.msg.user .avatar { background: var(--accent); }
.msg.assistant .avatar { background: #4b5563; }

.msg .bubble {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 13px;
  font-size: 13.5px;
  min-width: 0;
  overflow-wrap: anywhere;
}

.msg.user .bubble { background: var(--accent-weak); border-color: #d3e0fd; }
.msg .bubble > :first-child { margin-top: 0; }
.msg .bubble > :last-child { margin-bottom: 0; }
.msg .bubble p { margin: 0 0 8px; }
.msg .bubble pre {
  margin: 8px 0;
  padding: 10px 12px;
  background: #0f172a;
  color: #e2e8f0;
  border-radius: var(--radius-sm);
  overflow-x: auto;
  font-family: var(--mono);
  font-size: 12.5px;
}
.msg .bubble code {
  font-family: var(--mono);
  font-size: 12.5px;
  background: var(--surface-3);
  padding: 1px 5px;
  border-radius: 4px;
}
.msg .bubble pre code { background: none; padding: 0; color: inherit; }

.empty {
  height: 100%;
  display: grid;
  place-items: center;
  text-align: center;
  color: var(--text-faint);
  font-size: 13px;
  padding: 40px 20px;
}

.composer {
  border-top: 1px solid var(--border);
  background: var(--surface);
  padding: 12px 18px 14px;
}

.composer-inner {
  max-width: 780px;
  margin: 0 auto;
  display: flex;
  gap: 10px;
  align-items: flex-end;
}

.composer textarea {
  flex: 1;
  min-height: 44px;
  max-height: 180px;
  resize: none;
  padding: 11px 12px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  outline: none;
  line-height: 1.5;
}
.composer textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-weak); }

.composer .send {
  width: 44px;
  height: 44px;
  flex: 0 0 44px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--accent);
  background: var(--accent);
  color: #fff;
  display: grid;
  place-items: center;
}
.composer .send:hover { background: var(--accent-hover); }
.composer .send:disabled { opacity: 0.5; cursor: default; }
.composer .send svg { width: 18px; height: 18px; }

.composer-meta {
  max-width: 780px;
  margin: 8px auto 0;
  display: flex;
  justify-content: space-between;
  gap: 12px;
  font-size: 11.5px;
  color: var(--text-faint);
}

/* misc */

.spinner {
  width: 13px;
  height: 13px;
  border: 2px solid var(--border-strong);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  display: inline-block;
}
@keyframes spin { to { transform: rotate(360deg); } }

.toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: #111827;
  color: #fff;
  font-size: 13px;
  padding: 9px 16px;
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
  z-index: 50;
}
.toast.show { opacity: 1; }

.hidden { display: none !important; }

.account-row {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  font-size: 12.5px;
  padding: 7px 0;
  border-bottom: 1px dashed var(--border);
}
.account-row:last-child { border-bottom: 0; }
.account-row .dot { margin-top: 5px; }
.acct-main { min-width: 0; flex: 1; }
.acct-top {
  display: flex;
  align-items: center;
  gap: 6px;
}
.acct-name {
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.acct-sub {
  color: var(--text-faint);
  font-size: 11.5px;
  font-variant-numeric: tabular-nums;
}
.tag {
  flex: 0 0 auto;
  font-size: 10.5px;
  font-weight: 600;
  text-transform: uppercase;
  color: var(--text-dim);
  background: var(--surface-3);
  border-radius: 4px;
  padding: 0 5px;
  line-height: 16px;
}

.model-item {
  display: flex;
  flex-direction: column;
  gap: 1px;
}
.mi-name { font-weight: 600; }
.mi-id { font-size: 11px; color: var(--text-faint); }
.model-item.active .mi-id { color: var(--accent); opacity: 0.75; }

.reasoning {
  margin: 0 0 8px;
  border-left: 2px solid var(--border-strong);
  padding-left: 10px;
}
.reasoning > summary {
  cursor: pointer;
  font-size: 12px;
  color: var(--text-faint);
  list-style: none;
}
.reasoning > summary::-webkit-details-marker { display: none; }
.reasoning > summary::before { content: '▸ '; }
.reasoning[open] > summary::before { content: '▾ '; }
.reasoning > div {
  font-size: 12.5px;
  color: var(--text-dim);
  margin-top: 6px;
}

/* ------------------------------------------------------------ launcher */

.launch {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 34px 20px 48px;
}

.launch-inner { max-width: 820px; margin: 0 auto; }

.launch-title {
  margin: 0 0 6px;
  font-size: 20px;
  font-weight: 650;
  letter-spacing: 0;
}

.launch-sub {
  margin: 0 0 22px;
  font-size: 13px;
  color: var(--text-dim);
}

.crumb {
  display: flex;
  align-items: center;
  gap: 7px;
  margin-bottom: 16px;
  font-size: 12.5px;
  color: var(--text-faint);
}
.crumb a {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  color: var(--accent);
  text-decoration: none;
}
.crumb a:hover { text-decoration: underline; }
.crumb svg { width: 14px; height: 14px; }

.tiles {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
}

.tile {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 18px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: inherit;
  text-decoration: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.tile:hover {
  border-color: var(--accent);
  box-shadow: var(--shadow);
}

.tile-icon {
  flex: 0 0 42px;
  width: 42px;
  height: 42px;
  border-radius: var(--radius);
  display: grid;
  place-items: center;
}
.tile-icon svg { width: 20px; height: 20px; }
.tint-blue { background: var(--accent-weak); color: var(--accent); }
.tint-teal { background: #e3f5f1; color: #0f766e; }
.tint-amber { background: var(--warn-weak); color: var(--warn); }

.tile-body {
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-width: 0;
}
.tile-name { font-size: 15px; font-weight: 650; }
.tile-desc { font-size: 12.5px; color: var(--text-dim); }
.tile-host {
  font-size: 11.5px;
  color: var(--text-faint);
  font-family: var(--mono);
}

.tile-go {
  margin-left: auto;
  flex: 0 0 auto;
  color: var(--text-faint);
}
.tile-go svg { width: 18px; height: 18px; }
.tile:hover .tile-go { color: var(--accent); }

/* responsive */

@media (max-width: 900px) {
  .body { flex-direction: column; }
  .sidebar {
    width: auto;
    flex: 0 0 auto;
    max-height: 44vh;
    border-right: 0;
    border-bottom: 1px solid var(--border);
  }
  .brand .name { display: none; }
  .topbar { gap: 10px; padding: 0 12px; }
  .msg, .composer-inner, .composer-meta { max-width: 100%; }
}

/* keep the top bar on one line on narrow phones */
@media (max-width: 560px) {
  .topbar { gap: 8px; padding: 0 10px; }
  .tabs { padding: 2px; gap: 3px; }
  .tab { padding: 6px 10px; gap: 5px; font-size: 13px; }
  .tab svg { width: 14px; height: 14px; }
  .pill { padding: 4px 8px; }
  .topbar-right { gap: 8px; }
}

@media (max-width: 430px) {
  .tab { padding: 6px 9px; }
  .pill { padding: 5px 7px; gap: 0; }
  #conn-text { display: none; }
}

@media (max-width: 700px) {
  .tiles { grid-template-columns: 1fr; }
  .launch { padding: 22px 14px 36px; }
}
