:root {
  --bg: #212121;
  --sidebar: #181818;
  --surface: #303030;
  --surface-hover: #2a2a2a;
  --border: #3a3a3a;
  --text: #ececec;
  --text-muted: #b4b4b4;
  --text-faint: #8f8f8f;
  --accent: #ffffff;
  --accent-text: #000000;
  --radius-xl: 28px;
  --maxw: 768px;
  font-family: ui-sans-serif, -apple-system, system-ui, "Segoe UI", Helvetica, Arial, sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); }
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; }

.app { display: flex; height: 100dvh; overflow: hidden; }

/* Sidebar */
.sidebar {
  width: 260px; flex-shrink: 0; background: var(--sidebar);
  display: flex; flex-direction: column; padding: 8px; gap: 2px;
  transition: margin-left .2s ease;
}
.app.sidebar-closed .sidebar { margin-left: -260px; }
.sidebar-top { display: flex; justify-content: space-between; padding: 4px 4px 8px; }
.icon-btn {
  width: 36px; height: 36px; display: grid; place-items: center;
  border-radius: 8px; color: var(--text-muted);
}
.icon-btn:hover { background: var(--surface-hover); color: var(--text); }
.new-chat {
  display: flex; align-items: center; gap: 10px; padding: 9px 10px;
  border-radius: 10px; font-size: 14px; text-align: left;
}
.new-chat:hover { background: var(--surface-hover); }
.history-label { font-size: 12px; color: var(--text-faint); padding: 18px 10px 6px; }
.history { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 1px; }
.history-item {
  display: flex; align-items: center; gap: 4px;
  padding: 8px 10px; border-radius: 10px; font-size: 14px; cursor: pointer;
}
.history-item:hover, .history-item.active { background: var(--surface-hover); }
.history-item.active { background: #2f2f2f; }
.history-item .title { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.history-item .delete { opacity: 0; color: var(--text-faint); padding: 2px; border-radius: 6px; display: grid; }
.history-item:hover .delete { opacity: 1; }
.history-item .delete:hover { color: #f87171; }
.sidebar-footer {
  display: flex; align-items: center; gap: 10px; padding: 10px;
  border-top: 1px solid #262626; font-size: 14px;
}
.avatar {
  width: 28px; height: 28px; border-radius: 50%; background: #10a37f;
  display: grid; place-items: center; font-size: 13px; font-weight: 600; color: #fff;
}
.backdrop { display: none; }

/* Main */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; position: relative; }
.topbar { display: flex; align-items: center; gap: 4px; padding: 8px 12px; height: 56px; }
.topbar #openSidebar, .topbar #newChatMobile { display: none; }
.app.sidebar-closed .topbar #openSidebar { display: grid; }
.model-picker { position: relative; }
.model-button {
  display: flex; align-items: center; gap: 6px; font-size: 18px; font-weight: 500;
  padding: 6px 10px; border-radius: 10px; max-width: 60vw;
}
.model-button span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.model-button:hover { background: var(--surface-hover); }
.model-button svg { color: var(--text-muted); flex-shrink: 0; }
.model-menu {
  position: absolute; top: calc(100% + 6px); left: 0; z-index: 30; width: min(420px, 92vw);
  background: #2f2f2f; border: 1px solid var(--border); border-radius: 16px;
  box-shadow: 0 12px 40px rgba(0,0,0,.45); overflow: hidden;
}
.model-search { display: flex; gap: 8px; align-items: center; padding: 10px; border-bottom: 1px solid var(--border); }
.model-search input {
  flex: 1; min-width: 0; background: #212121; border: 1px solid var(--border); color: var(--text);
  border-radius: 10px; padding: 8px 10px; font: inherit; font-size: 14px; outline: none;
}
.model-search input:focus { border-color: #5a5a5a; }
.toggle-small { font-size: 12px; color: var(--text-muted); display: flex; gap: 4px; align-items: center; white-space: nowrap; cursor: pointer; }
.model-list { max-height: min(420px, 60vh); overflow-y: auto; padding: 6px; }
.model-item { display: flex; flex-direction: column; gap: 2px; padding: 8px 10px; border-radius: 10px; cursor: pointer; width: 100%; text-align: left; }
.model-item:hover, .model-item.focused { background: #3a3a3a; }
.model-item.selected .model-name::after { content: " ✓"; color: #10a37f; }
.model-name { font-size: 14px; }
.model-meta { font-size: 12px; color: var(--text-faint); display: flex; gap: 8px; flex-wrap: wrap; }
.badge { font-size: 11px; padding: 0 6px; border-radius: 6px; background: #10a37f22; color: #4fd1a5; }
.model-empty { padding: 16px; text-align: center; color: var(--text-faint); font-size: 14px; }

.thread { flex: 1; overflow-y: auto; }
.messages { max-width: var(--maxw); margin: 0 auto; padding: 16px 24px 32px; display: flex; flex-direction: column; gap: 28px; }
.empty { display: none; }
.app.is-empty .thread { display: flex; flex-direction: column; justify-content: flex-end; flex: 0.9; }
.app.is-empty .empty { display: block; text-align: center; padding-bottom: 28px; }
.app.is-empty .composer-wrap { flex: 1; justify-content: flex-start; }
.app.is-empty .disclaimer { visibility: hidden; }
.empty h1 { font-size: 28px; font-weight: 500; margin: 0; }

.msg { display: flex; font-size: 16px; line-height: 1.7; }
.msg.user { justify-content: flex-end; }
.msg.user .bubble {
  background: var(--surface); padding: 10px 18px; border-radius: 22px;
  max-width: 70%; white-space: pre-wrap; word-wrap: break-word;
}
.msg.assistant .bubble { width: 100%; overflow-wrap: anywhere; }
.msg.error .bubble { color: #f87171; }

/* Markdown */
.bubble > :first-child { margin-top: 0; }
.bubble > :last-child { margin-bottom: 0; }
.bubble p { margin: 0 0 12px; }
.bubble h1, .bubble h2, .bubble h3 { line-height: 1.3; margin: 24px 0 10px; }
.bubble h1 { font-size: 24px; } .bubble h2 { font-size: 20px; } .bubble h3 { font-size: 17px; }
.bubble ul, .bubble ol { padding-left: 24px; margin: 0 0 12px; }
.bubble li { margin: 4px 0; }
.bubble a { color: #7ab7ff; }
.bubble blockquote { border-left: 3px solid var(--border); margin: 0 0 12px; padding-left: 14px; color: var(--text-muted); }
.bubble hr { border: 0; border-top: 1px solid var(--border); margin: 20px 0; }
.bubble table { border-collapse: collapse; margin: 0 0 12px; display: block; overflow-x: auto; }
.bubble th, .bubble td { border: 1px solid var(--border); padding: 6px 12px; text-align: left; }
.bubble :not(pre) > code {
  background: #424242; padding: 2px 5px; border-radius: 6px; font-size: 0.875em;
}
.code-block { background: #171717; border-radius: 12px; margin: 0 0 14px; overflow: hidden; border: 1px solid #2a2a2a; }
.code-head {
  display: flex; justify-content: space-between; align-items: center;
  background: #2f2f2f; color: var(--text-muted); font-size: 12px; padding: 6px 14px;
}
.code-head button { font-size: 12px; color: var(--text-muted); display: flex; gap: 4px; align-items: center; }
.code-head button:hover { color: var(--text); }
.code-block pre { margin: 0; padding: 14px 16px; overflow-x: auto; }
.code-block code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13.5px; line-height: 1.55; background: none !important; padding: 0 !important; }

.cursor::after {
  content: ""; display: inline-block; width: 10px; height: 10px; margin-left: 4px;
  background: var(--text); border-radius: 50%; vertical-align: middle;
  animation: pulse 1s ease-in-out infinite;
}
@keyframes pulse { 50% { transform: scale(.6); opacity: .5; } }

.msg-actions { display: flex; gap: 2px; margin-top: 6px; }
.msg-actions button { color: var(--text-faint); padding: 5px; border-radius: 6px; display: grid; }
.msg-actions button:hover { background: var(--surface-hover); color: var(--text); }

/* Composer */
.composer-wrap { display: flex; flex-direction: column; align-items: center; padding: 0 16px 8px; }
.composer {
  width: 100%; max-width: var(--maxw); display: flex; flex-direction: column; gap: 4px;
  background: var(--surface); border-radius: var(--radius-xl); padding: 12px 12px 10px 12px;
  box-shadow: 0 0 0 1px rgba(255,255,255,.04), 0 4px 16px rgba(0,0,0,.15);
}
.composer textarea {
  resize: none; border: 0; outline: none; background: transparent; color: var(--text);
  font: inherit; font-size: 16px; line-height: 24px; max-height: 200px; padding: 4px 10px;
}
.composer-bar { display: flex; align-items: center; gap: 6px; }
.composer-bar .spacer { flex: 1; }
.chip {
  display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--text-muted);
  padding: 6px 12px; border-radius: 999px; border: 1px solid var(--border);
}
.chip:hover { background: var(--surface-hover); color: var(--text); }
.chip.on { color: #7ab7ff; border-color: #2b4a6f; background: #1d2b3a; }
.chip[hidden] { display: none; }
.composer textarea::placeholder { color: var(--text-faint); }
.send {
  width: 36px; height: 36px; border-radius: 50%; flex-shrink: 0;
  background: var(--accent); color: var(--accent-text); display: grid; place-items: center;
}
.send:disabled { background: #676767; color: #2f2f2f; cursor: default; }
.send .icon-stop { display: none; }
.send.streaming .icon-send { display: none; }
.send.streaming .icon-stop { display: block; }
.disclaimer { font-size: 12px; color: var(--text-muted); margin: 8px 0 0; text-align: center; }

/* Tool calls */
.turn { display: flex; flex-direction: column; gap: 10px; }
.tool {
  border: 1px solid var(--border); border-radius: 12px; font-size: 13.5px; overflow: hidden; background: #262626;
}
.tool summary {
  list-style: none; cursor: pointer; display: flex; align-items: center; gap: 8px; padding: 8px 12px; color: var(--text-muted);
}
.tool summary::-webkit-details-marker { display: none; }
.tool summary:hover { color: var(--text); }
.tool summary .tname { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; color: var(--text); }
.tool summary .chev { margin-left: auto; transition: transform .15s; }
.tool[open] summary .chev { transform: rotate(180deg); }
.tool .status { width: 14px; height: 14px; display: grid; place-items: center; flex-shrink: 0; }
.tool.pending .status::before {
  content: ""; width: 12px; height: 12px; border-radius: 50%;
  border: 2px solid #555; border-top-color: var(--text); animation: spin .8s linear infinite;
}
.tool.ok .status::before { content: "✓"; color: #4fd1a5; }
.tool.err .status::before { content: "!"; color: #f87171; font-weight: 700; }
@keyframes spin { to { transform: rotate(360deg); } }
.tool-body { padding: 0 12px 10px; }
.tool-body .label { font-size: 11px; text-transform: uppercase; letter-spacing: .04em; color: var(--text-faint); margin: 8px 0 4px; }
.tool-body pre {
  margin: 0; background: #171717; border-radius: 8px; padding: 8px 10px; max-height: 260px; overflow: auto;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12.5px; white-space: pre-wrap; word-break: break-word;
}
.notice { font-size: 13px; color: #fbbf24; }

/* Connectors modal */
.modal {
  width: min(560px, calc(100vw - 32px)); max-height: calc(100dvh - 48px); padding: 20px;
  background: #2a2a2a; color: var(--text); border: 1px solid var(--border); border-radius: 18px;
  box-shadow: 0 20px 60px rgba(0,0,0,.5);
}
.modal::backdrop { background: rgba(0,0,0,.55); }
.modal-head { display: flex; align-items: center; justify-content: space-between; }
.modal h2 { font-size: 18px; margin: 0; font-weight: 600; }
.modal h3 { font-size: 14px; margin: 0 0 10px; font-weight: 600; }
.modal-sub { font-size: 13px; color: var(--text-muted); margin: 6px 0 16px; line-height: 1.5; }
.server-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 18px; }
.server { border: 1px solid var(--border); border-radius: 12px; padding: 10px 12px; background: #242424; }
.server-row { display: flex; align-items: center; gap: 10px; }
.server-info { flex: 1; min-width: 0; }
.server-name { font-size: 14px; font-weight: 500; display: flex; gap: 6px; align-items: center; }
.server-url { font-size: 12px; color: var(--text-faint); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.server-tools { font-size: 12px; color: var(--text-muted); margin-top: 8px; line-height: 1.6; }
.server-tools code { background: #333; border-radius: 5px; padding: 1px 5px; margin-right: 4px; }
.server-tools.err { color: #f87171; }
.server .link { font-size: 12px; color: var(--text-muted); padding: 4px 6px; border-radius: 6px; }
.server .link:hover { background: #333; color: var(--text); }
.server .link.danger:hover { color: #f87171; }
.switch { position: relative; width: 34px; height: 20px; flex-shrink: 0; }
.switch input { opacity: 0; width: 0; height: 0; position: absolute; }
.switch span { position: absolute; inset: 0; background: #555; border-radius: 999px; transition: background .15s; cursor: pointer; }
.switch span::after { content: ""; position: absolute; width: 16px; height: 16px; left: 2px; top: 2px; background: #fff; border-radius: 50%; transition: transform .15s; }
.switch input:checked + span { background: #10a37f; }
.switch input:checked + span::after { transform: translateX(14px); }
.empty-servers { font-size: 13px; color: var(--text-faint); padding: 8px 0; }
.add-server { border-top: 1px solid var(--border); padding-top: 16px; display: flex; flex-direction: column; gap: 10px; }
.add-server label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--text-muted); }
.add-server .hint { font-size: 12px; color: var(--text-faint); }
.add-server input, .add-server textarea {
  background: #1e1e1e; border: 1px solid var(--border); color: var(--text); border-radius: 10px;
  padding: 8px 10px; font: inherit; font-size: 14px; outline: none; resize: vertical;
}
.add-server input:focus, .add-server textarea:focus { border-color: #5a5a5a; }
.form-actions { display: flex; align-items: center; justify-content: flex-end; gap: 10px; }
.form-status { flex: 1; font-size: 13px; color: var(--text-muted); }
.form-status.err { color: #f87171; }
.btn { padding: 8px 14px; border-radius: 999px; font-size: 14px; font-weight: 500; }
.btn.primary { background: #fff; color: #000; }
.btn.primary:disabled { opacity: .6; }

.section { border-bottom: 1px solid var(--border); padding: 14px 0 16px; margin-bottom: 14px; display: flex; flex-direction: column; gap: 8px; }
.section .modal-sub { margin: 0; }
.section-title { margin-top: 4px !important; }
.modal a { color: #7ab7ff; }
.key-row { display: flex; gap: 8px; align-items: center; }
.key-row input {
  flex: 1; min-width: 0; background: #1e1e1e; border: 1px solid var(--border); color: var(--text); border-radius: 10px;
  padding: 8px 10px; font: inherit; font-size: 14px; outline: none; font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
.key-row input:focus { border-color: #5a5a5a; }
.link { font-size: 12px; color: var(--text-muted); padding: 4px 6px; border-radius: 6px; }
.link:hover { background: #333; color: var(--text); }
.link.danger:hover { color: #f87171; }
.form-status.ok { color: #4fd1a5; }
.error-text { color: #f87171; }
.notice button { color: #7ab7ff; text-decoration: underline; padding: 0; margin-left: 4px; font-size: 13px; }

/* Mobile */
@media (max-width: 768px) {
  .sidebar { position: fixed; inset: 0 auto 0 0; z-index: 20; margin-left: 0; box-shadow: 4px 0 24px rgba(0,0,0,.4); }
  .app.sidebar-closed .sidebar { margin-left: -280px; }
  .app:not(.sidebar-closed) .backdrop { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 10; }
  .topbar { justify-content: space-between; }
  .topbar #openSidebar, .topbar #newChatMobile { display: grid; }
  .messages { padding: 12px 16px 24px; }
  .msg.user .bubble { max-width: 85%; }
  .empty h1 { font-size: 24px; }
}
