:root {
  --bg: #0d0d0f;
  --bg-elev: #17171b;
  --bg-elev-2: #1f1f25;
  --border: #2a2a32;
  --text: #ececf1;
  --text-dim: #9a9aa5;
  --accent: #c96442;
  --accent-hover: #d8754f;
  --user-bubble: #2a2a32;
  --code-bg: #0a0a0c;
  --danger: #e5534b;
  --radius: 12px;
  --sidebar-w: 280px;
}

[data-theme="light"] {
  --bg: #ffffff;
  --bg-elev: #f7f7f8;
  --bg-elev-2: #ececf1;
  --border: #e0e0e6;
  --text: #1a1a1e;
  --text-dim: #6b6b76;
  --accent: #c96442;
  --accent-hover: #b5563a;
  --user-bubble: #ececf1;
  --code-bg: #f4f4f6;
}

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

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

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

/* ---------- Sidebar ---------- */
#sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: var(--bg-elev);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transition: transform 0.25s ease;
}

.sidebar-header {
  display: flex;
  gap: 8px;
  padding: 12px;
  border-bottom: 1px solid var(--border);
}

.primary-btn {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 14px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
}
.primary-btn:hover { background: var(--accent-hover); }

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

.chat-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-radius: var(--radius);
  cursor: pointer;
  color: var(--text-dim);
  font-size: 14px;
  margin-bottom: 2px;
  transition: background 0.15s;
}
.chat-item:hover { background: var(--bg-elev-2); }
.chat-item.active { background: var(--bg-elev-2); color: var(--text); }
.chat-item .title {
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.chat-item .title-input {
  flex: 1;
  background: var(--bg);
  border: 1px solid var(--accent);
  color: var(--text);
  border-radius: 6px;
  padding: 2px 6px;
  font-size: 14px;
}
.chat-item-actions {
  display: none;
  gap: 4px;
}
.chat-item:hover .chat-item-actions,
.chat-item.active .chat-item-actions { display: flex; }

.mini-btn {
  background: transparent;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 13px;
  padding: 2px 4px;
  border-radius: 4px;
}
.mini-btn:hover { color: var(--text); background: var(--bg); }

.sidebar-footer {
  padding: 12px;
  border-top: 1px solid var(--border);
}

.ghost-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-dim);
  padding: 8px 12px;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 13px;
  transition: all 0.15s;
}
.ghost-btn:hover { color: var(--text); border-color: var(--text-dim); }

.icon-btn {
  background: transparent;
  border: none;
  color: var(--text);
  font-size: 20px;
  cursor: pointer;
  padding: 4px 8px;
}

/* ---------- Main ---------- */
#main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  position: relative;
}

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

.chat-title {
  flex: 1;
  font-size: 16px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar-actions {
  display: flex;
  gap: 8px;
}

/* ---------- Messages ---------- */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px 0;
  scroll-behavior: smooth;
}

.empty-state {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--text-dim);
  text-align: center;
  gap: 8px;
}
.empty-logo {
  font-size: 48px;
  color: var(--accent);
  margin-bottom: 8px;
}
.empty-state h2 { font-size: 22px; color: var(--text); }

.msg {
  max-width: 760px;
  margin: 0 auto 20px;
  padding: 0 20px;
  display: flex;
  gap: 14px;
  animation: fadeIn 0.2s ease;
}
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

.msg .avatar {
  width: 30px;
  height: 30px;
  border-radius: 6px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  font-weight: 700;
}
.msg.user .avatar { background: var(--user-bubble); color: var(--text); }
.msg.assistant .avatar { background: var(--accent); color: #fff; }

.msg .body {
  flex: 1;
  min-width: 0;
  padding-top: 3px;
  line-height: 1.65;
  font-size: 15px;
  word-wrap: break-word;
  overflow-wrap: break-word;
}

/* Markdown styles */
.body p { margin: 0 0 12px; }
.body p:last-child { margin-bottom: 0; }
.body ul, .body ol { margin: 0 0 12px; padding-left: 24px; }
.body li { margin-bottom: 4px; }
.body h1, .body h2, .body h3, .body h4 { margin: 18px 0 10px; line-height: 1.3; }
.body h1 { font-size: 1.5em; }
.body h2 { font-size: 1.3em; }
.body h3 { font-size: 1.15em; }
.body a { color: var(--accent); }
.body blockquote {
  border-left: 3px solid var(--border);
  padding-left: 14px;
  color: var(--text-dim);
  margin: 0 0 12px;
}
.body table {
  border-collapse: collapse;
  width: 100%;
  margin: 0 0 12px;
  font-size: 14px;
}
.body th, .body td {
  border: 1px solid var(--border);
  padding: 8px 10px;
  text-align: left;
}
.body th { background: var(--bg-elev); }

/* Inline code */
.body :not(pre) > code {
  background: var(--bg-elev-2);
  padding: 2px 6px;
  border-radius: 5px;
  font-size: 0.88em;
  font-family: "SF Mono", "Cascadia Code", Consolas, Monaco, monospace;
}

/* Code blocks */
.code-wrapper {
  position: relative;
  margin: 0 0 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--code-bg);
}
.code-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 12px;
  background: var(--bg-elev);
  border-bottom: 1px solid var(--border);
  font-size: 12px;
  color: var(--text-dim);
}
.code-wrapper pre {
  margin: 0;
  padding: 14px;
  overflow-x: auto;
}
.code-wrapper pre code {
  font-family: "SF Mono", "Cascadia Code", Consolas, Monaco, monospace;
  font-size: 13px;
  line-height: 1.5;
  background: transparent !important;
  padding: 0 !important;
}
.copy-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-dim);
  font-size: 11px;
  padding: 3px 8px;
  border-radius: 5px;
  cursor: pointer;
  transition: all 0.15s;
}
.copy-btn:hover { color: var(--text); border-color: var(--text-dim); }
.copy-btn.copied { color: #4caf50; border-color: #4caf50; }

/* Thinking indicator */
.thinking {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--text-dim);
  font-style: italic;
}
.thinking .dots span {
  display: inline-block;
  width: 6px; height: 6px;
  margin: 0 1px;
  background: var(--text-dim);
  border-radius: 50%;
  animation: blink 1.4s infinite both;
}
.thinking .dots span:nth-child(2) { animation-delay: 0.2s; }
.thinking .dots span:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink {
  0%, 80%, 100% { opacity: 0.2; }
  40% { opacity: 1; }
}

/* ---------- Composer ---------- */
.composer {
  flex-shrink: 0;
  padding: 12px 16px 16px;
  border-top: 1px solid var(--border);
  background: var(--bg);
}
.composer-inner {
  max-width: 760px;
  margin: 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px 8px 8px 14px;
  transition: border-color 0.15s;
}
.composer-inner:focus-within { border-color: var(--accent); }

#input {
  flex: 1;
  resize: none;
  background: transparent;
  border: none;
  outline: none;
  color: var(--text);
  font-size: 15px;
  line-height: 1.5;
  font-family: inherit;
  max-height: 200px;
  overflow-y: auto;
  padding: 6px 0;
}
#input::placeholder { color: var(--text-dim); }

.send-btn {
  flex-shrink: 0;
  width: 36px;
  height: 36px;
  border-radius: 8px;
  border: none;
  background: var(--accent);
  color: #fff;
  font-size: 16px;
  cursor: pointer;
  transition: background 0.15s, opacity 0.15s;
}
.send-btn:hover { background: var(--accent-hover); }
.send-btn:disabled { opacity: 0.4; cursor: not-allowed; }

.composer-hint {
  max-width: 760px;
  margin: 8px auto 0;
  text-align: center;
  font-size: 11px;
  color: var(--text-dim);
}

/* Error message */
.error-banner {
  max-width: 760px;
  margin: 0 auto 16px;
  padding: 12px 16px;
  background: rgba(229, 83, 75, 0.12);
  border: 1px solid var(--danger);
  border-radius: var(--radius);
  color: var(--danger);
  font-size: 14px;
}

/* ---------- Mobile ---------- */
.mobile-only { display: none; }
.backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 19;
}

@media (max-width: 768px) {
  .mobile-only { display: inline-flex; }
  #sidebar {
    position: fixed;
    top: 0; left: 0; bottom: 0;
    z-index: 20;
    transform: translateX(-100%);
    box-shadow: 2px 0 16px rgba(0,0,0,0.3);
  }
  #sidebar.open { transform: translateX(0); }
  .backdrop.show { display: block; }
  .msg { padding: 0 14px; }
  .topbar-actions .ghost-btn { padding: 6px 8px; font-size: 12px; }
}

/* Scrollbars */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-dim); }

.continuing-hint {
  display: inline-block;
  margin-top: 6px;
  padding: 2px 8px;
  font-size: 11px;
  color: var(--text-dim);
  background: var(--bg-elev-2);
  border-radius: 10px;
  font-style: italic;
}