:root {
  --bg: #0d1117;
  --bg-elev: #161b22;
  --bg-elev2: #1f242c;
  --border: #2a313c;
  --text: #c9d1d9;
  --text-dim: #8b949e;
  --accent: #00ff88;
  --user-bubble: #1f6feb;
  --assistant-bubble: #1f242c;
  --error: #f85149;
}

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

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", sans-serif;
  font-size: 14px;
  line-height: 1.5;
}

.app {
  display: grid;
  grid-template-columns: 280px 1fr;
  height: 100vh;
  overflow: hidden;
}

.sidebar {
  background: var(--bg-elev);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.sidebar header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}

.sidebar h1 { font-size: 14px; font-weight: 600; }
.sidebar button {
  background: transparent;
  color: var(--text-dim);
  border: 1px solid var(--border);
  border-radius: 4px;
  width: 24px;
  height: 24px;
  cursor: pointer;
  font-size: 16px;
}
.sidebar button:hover { color: var(--text); border-color: var(--text-dim); }

.status {
  padding: 8px 16px;
  font-size: 12px;
  border-bottom: 1px solid var(--border);
}
.status.online { color: var(--accent); }
.status.offline { color: var(--error); }

.thread-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px 0;
}

.thread-item {
  padding: 8px 16px;
  cursor: pointer;
  border-left: 3px solid transparent;
  font-size: 13px;
  color: var(--text-dim);
}
.thread-item:hover { background: var(--bg-elev2); color: var(--text); }
.thread-item.active {
  background: var(--bg-elev2);
  color: var(--text);
  border-left-color: var(--accent);
}
.thread-item .title {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-bottom: 2px;
}
.thread-item .meta {
  font-size: 11px;
  color: var(--text-dim);
}

.chat {
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.chat header {
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: var(--bg-elev);
}
#thread-title { font-weight: 500; }
#mode-picker {
  background: var(--bg-elev2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 4px 8px;
  font-size: 12px;
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.msg {
  max-width: 80%;
  padding: 10px 14px;
  border-radius: 10px;
  white-space: pre-wrap;
  word-wrap: break-word;
}
.msg.user {
  background: var(--user-bubble);
  color: white;
  align-self: flex-end;
}
.msg.assistant {
  background: var(--assistant-bubble);
  align-self: flex-start;
  border: 1px solid var(--border);
}
.msg.assistant.streaming::after {
  content: "▍";
  color: var(--accent);
  animation: blink 1s infinite;
}
.msg.error {
  background: rgba(248, 81, 73, 0.1);
  border: 1px solid var(--error);
  color: var(--error);
  align-self: stretch;
  max-width: 100%;
}
@keyframes blink { 50% { opacity: 0.3; } }

.msg .role-tag {
  display: block;
  font-size: 11px;
  color: var(--text-dim);
  margin-bottom: 4px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.msg.user .role-tag { color: rgba(255,255,255,0.7); }

.tool-call {
  background: var(--bg-elev2);
  border: 1px solid var(--border);
  border-radius: 6px;
  margin: 6px 0;
  font-family: "SF Mono", Menlo, monospace;
  font-size: 12px;
  overflow: hidden;
}
.tool-call summary {
  padding: 6px 10px;
  cursor: pointer;
  background: var(--bg);
  color: var(--accent);
}
.tool-call pre {
  padding: 10px;
  overflow-x: auto;
  white-space: pre-wrap;
}

.composer {
  border-top: 1px solid var(--border);
  padding: 12px 20px;
  display: flex;
  gap: 8px;
  background: var(--bg-elev);
}
.composer textarea {
  flex: 1;
  background: var(--bg-elev2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 12px;
  font-family: inherit;
  font-size: 14px;
  resize: vertical;
  min-height: 60px;
  max-height: 200px;
}
.composer textarea:focus { outline: none; border-color: var(--accent); }
.composer button {
  background: var(--accent);
  color: black;
  border: none;
  border-radius: 6px;
  padding: 0 20px;
  font-weight: 600;
  cursor: pointer;
  font-size: 14px;
}
.composer button:disabled { opacity: 0.4; cursor: not-allowed; }
.composer button:hover:not(:disabled) { background: #00cc6e; }

.settings-drawer {
  position: fixed;
  right: 0;
  top: 0;
  bottom: 0;
  width: 320px;
  background: var(--bg-elev);
  border-left: 1px solid var(--border);
  z-index: 10;
  display: flex;
  flex-direction: column;
  transform: translateX(0);
  transition: transform 200ms ease;
}
.settings-drawer.hidden { transform: translateX(110%); }
.settings-drawer header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}
.settings-drawer h2 { font-size: 14px; font-weight: 600; }
.settings-drawer .settings-body { flex: 1; overflow-y: auto; padding: 16px; }
.settings-drawer section { margin-bottom: 24px; }
.settings-drawer h3 { font-size: 11px; color: var(--text-dim); text-transform: uppercase; letter-spacing: 0.5px; margin-bottom: 8px; }
.settings-drawer select, .settings-drawer input {
  width: 100%;
  background: var(--bg-elev2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 6px 8px;
  font-size: 13px;
  margin-bottom: 8px;
}
.settings-drawer button {
  background: var(--accent);
  color: black;
  border: none;
  border-radius: 4px;
  padding: 6px 12px;
  cursor: pointer;
  font-weight: 500;
}
.stats { font-size: 12px; color: var(--text-dim); line-height: 1.6; }
.stats strong { color: var(--text); }

/* Markdown rendering inside .msg.assistant .body */
.msg.assistant .body { line-height: 1.55; }
.msg.assistant .body p { margin: 0.5em 0; }
.msg.assistant .body p:first-child { margin-top: 0; }
.msg.assistant .body p:last-child { margin-bottom: 0; }
.msg.assistant .body h1, .msg.assistant .body h2, .msg.assistant .body h3 { margin: 1em 0 0.4em; font-weight: 600; }
.msg.assistant .body h1 { font-size: 1.3em; }
.msg.assistant .body h2 { font-size: 1.15em; }
.msg.assistant .body h3 { font-size: 1.05em; }
.msg.assistant .body code { background: rgba(255,255,255,0.07); padding: 1px 5px; border-radius: 3px; font-family: "SF Mono", Menlo, monospace; font-size: 0.92em; }
.msg.assistant .body pre.code { background: var(--bg); border: 1px solid var(--border); border-radius: 6px; padding: 10px; overflow-x: auto; margin: 0.6em 0; font-family: "SF Mono", Menlo, monospace; font-size: 12px; line-height: 1.45; }
.msg.assistant .body pre.code code { background: none; padding: 0; font-size: inherit; }
.msg.assistant .body ul, .msg.assistant .body ol { margin: 0.5em 0 0.5em 1.5em; }
.msg.assistant .body li { margin: 0.2em 0; }
.msg.assistant .body blockquote { border-left: 3px solid var(--border); padding: 0.2em 0.8em; color: var(--text-dim); margin: 0.6em 0; }
.msg.assistant .body a { color: var(--accent); text-decoration: underline; }
.msg.assistant .body strong { font-weight: 600; }
.msg.assistant .body em { font-style: italic; }

/* Tool call preview */
.tool-call summary .tc-preview {
  color: var(--text-dim);
  font-weight: normal;
  margin-left: 8px;
  font-style: italic;
}
.thinking-block { background: rgba(168, 130, 255, 0.06); border-color: rgba(168, 130, 255, 0.3); }
.thinking-block summary { color: rgba(168, 130, 255, 0.8); }

/* Mobile layout */
@media (max-width: 640px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { display: none; }
  .sidebar.open { display: flex; position: fixed; inset: 0; z-index: 50; width: 100%; }
  #close-sidebar-mobile { display: inline-block !important; }
  #open-sidebar-mobile { display: inline-block !important; background: transparent; border: none; color: var(--text); font-size: 18px; cursor: pointer; padding: 0 10px 0 0; }
  .chat header { padding: 10px 12px; }
  .messages { padding: 12px; }
  .msg { max-width: 95%; }
  .composer { padding: 8px 12px; }
  .settings-drawer { width: 100vw; }
}
