:root {
  --bg: #f7f7f8;
  --bg-elev: #ffffff;
  --bg-sunken: #ececf1;
  --text: #202123;
  --text-muted: #6b6c7a;
  --text-faint: #8e8ea0;
  --border: #e5e5e8;
  --border-strong: #d0d0d4;
  --accent: #10a37f;
  --accent-hover: #0e8f6f;
  --user-bubble: #f4f4f4;
  --ai-bubble: transparent;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
  --sidebar-w: 260px;
  --radius: 12px;
  --code-bg: #f0f0f3;
}

[data-theme="dark"] {
  --bg: #212121;
  --bg-elev: #2a2b2c;
  --bg-sunken: #1a1a1a;
  --text: #ececec;
  --text-muted: #a1a1a3;
  --text-faint: #7d7d80;
  --border: #3a3a3a;
  --border-strong: #4a4a4a;
  --accent: #10a37f;
  --accent-hover: #13b891;
  --user-bubble: #2f2f2f;
  --ai-bubble: transparent;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
  --code-bg: #2a2a2a;
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 15px;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

button {
  font-family: inherit;
  cursor: pointer;
  border: none;
  background: none;
  color: inherit;
}

textarea {
  font-family: inherit;
  color: inherit;
}

.app {
  display: flex;
  height: 100vh;
  width: 100vw;
  overflow: hidden;
}

/* ---------- Sidebar ---------- */
.sidebar {
  width: var(--sidebar-w);
  background: var(--bg-sunken);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  transition: transform 0.25s ease, margin-left 0.25s ease;
  z-index: 20;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-mark {
  color: var(--accent);
  display: inline-flex;
}

.brand-title {
  font-weight: 600;
  font-size: 15px;
  line-height: 1.1;
}

.brand-sub {
  font-size: 11px;
  color: var(--text-faint);
  margin-top: 2px;
}

.icon-btn {
  width: 34px;
  height: 34px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  color: var(--text-muted);
  transition: background 0.15s, color 0.15s;
}

.icon-btn:hover {
  background: var(--bg-elev);
  color: var(--text);
}

.new-chat {
  margin: 4px 12px 12px;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  background: var(--bg-elev);
  border-radius: 8px;
  font-size: 14px;
  color: var(--text);
  font-weight: 500;
  transition: background 0.15s;
}

.new-chat:hover {
  background: var(--bg);
}

.sidebar-section {
  flex: 1;
  overflow-y: auto;
  padding: 0 8px;
}

.sidebar-section-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-faint);
  padding: 8px 10px 4px;
  font-weight: 600;
}

.chat-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.chat-list-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 10px;
  border-radius: 8px;
  font-size: 13.5px;
  color: var(--text-muted);
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-list-item:hover {
  background: var(--bg-elev);
  color: var(--text);
}

.chat-list-item.active {
  background: var(--bg-elev);
  color: var(--text);
  font-weight: 500;
}

.chat-list-item span {
  overflow: hidden;
  text-overflow: ellipsis;
}

.sidebar-footer {
  border-top: 1px solid var(--border);
  padding: 10px 12px 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.model-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text-muted);
  background: var(--bg-elev);
  border: 1px solid var(--border);
  padding: 5px 9px;
  border-radius: 999px;
  width: fit-content;
}

.model-pill .dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--accent);
}

.user-chip {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px;
  border-radius: 8px;
}

.avatar {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: #5436DA;
  color: #fff;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 13px;
  flex-shrink: 0;
}

.user-meta {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.user-name {
  font-size: 13.5px;
  font-weight: 500;
}

.user-plan {
  font-size: 11.5px;
  color: var(--text-faint);
}

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

.topbar {
  height: 56px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  background: var(--bg);
  position: sticky;
  top: 0;
  z-index: 10;
}

.model-selector {
  display: flex;
  align-items: center;
  gap: 8px;
}

.model-name {
  font-weight: 600;
  font-size: 15px;
}

.model-tag {
  font-size: 11.5px;
  color: var(--text-faint);
  background: var(--bg-sunken);
  padding: 2px 7px;
  border-radius: 999px;
  border: 1px solid var(--border);
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 4px;
}

[data-theme="light"] .icon-moon { display: none; }
[data-theme="dark"] .icon-sun { display: none; }

/* ---------- Chat ---------- */
.chat {
  flex: 1;
  overflow-y: auto;
  padding: 0;
  scroll-behavior: smooth;
}

.chat-inner {
  max-width: 768px;
  margin: 0 auto;
  padding: 24px 16px 40px;
  display: flex;
  flex-direction: column;
  gap: 24px;
}

/* Welcome */
.welcome {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 80px 16px 40px;
  max-width: 768px;
  margin: 0 auto;
}

.welcome-logo {
  color: var(--accent);
  margin-bottom: 18px;
}

.welcome h1 {
  font-size: 28px;
  font-weight: 600;
  margin: 0 0 8px;
  letter-spacing: -0.01em;
}

.welcome p {
  color: var(--text-muted);
  margin: 0 0 36px;
  max-width: 520px;
  line-height: 1.55;
}

.suggestions {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  width: 100%;
  max-width: 640px;
}

.suggestion {
  text-align: left;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg-elev);
  color: var(--text-muted);
  font-size: 13.5px;
  line-height: 1.4;
  transition: background 0.15s, border-color 0.15s, color 0.15s, transform 0.1s;
}

.suggestion:hover {
  background: var(--bg);
  border-color: var(--border-strong);
  color: var(--text);
}

.suggestion:active {
  transform: scale(0.99);
}

.suggestion .label {
  font-weight: 500;
  color: var(--text);
  display: block;
  margin-bottom: 3px;
  font-size: 13px;
}

/* Messages */
.message {
  display: flex;
  gap: 14px;
  align-items: flex-start;
}

.message .avatar-bubble {
  width: 30px;
  height: 30px;
  border-radius: 6px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  font-weight: 600;
  font-size: 13px;
  margin-top: 2px;
}

.message.user .avatar-bubble {
  background: #5436DA;
  color: #fff;
  border-radius: 50%;
}

.message.ai .avatar-bubble {
  background: var(--accent);
  color: #fff;
}

.message .content {
  flex: 1;
  min-width: 0;
  font-size: 15.5px;
  line-height: 1.7;
  color: var(--text);
  word-wrap: break-word;
  overflow-wrap: break-word;
}

.message .author {
  font-weight: 600;
  margin-bottom: 4px;
  font-size: 14px;
}

.message .body p {
  margin: 0 0 14px;
}

.message .body p:last-child {
  margin-bottom: 0;
}

.message .body strong {
  font-weight: 600;
}

.message .body em {
  font-style: italic;
}

.message .body ul, .message .body ol {
  margin: 0 0 14px;
  padding-left: 24px;
}

.message .body li {
  margin-bottom: 6px;
}

.message .body li::marker {
  color: var(--text-faint);
}

.message .body code {
  font-family: 'JetBrains Mono', 'SF Mono', Menlo, Consolas, monospace;
  font-size: 0.88em;
  background: var(--code-bg);
  padding: 2px 6px;
  border-radius: 4px;
}

.message .body pre {
  background: var(--code-bg);
  padding: 12px 14px;
  border-radius: 8px;
  overflow-x: auto;
  font-size: 13.5px;
  line-height: 1.55;
  margin: 0 0 14px;
}

.message .body pre code {
  background: none;
  padding: 0;
}

.message .body a {
  color: var(--accent);
  text-decoration: underline;
  text-underline-offset: 2px;
}

.message .body h1, .message .body h2, .message .body h3 {
  margin: 18px 0 10px;
  font-weight: 600;
  line-height: 1.3;
}

.message .body h1 { font-size: 1.4em; }
.message .body h2 { font-size: 1.2em; }
.message .body h3 { font-size: 1.05em; }

.message .body blockquote {
  border-left: 3px solid var(--border-strong);
  margin: 0 0 14px;
  padding: 4px 0 4px 14px;
  color: var(--text-muted);
}

/* Thinking indicator */
.thinking {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--text-muted);
  font-size: 14px;
  padding: 6px 0;
}

.thinking-dots {
  display: inline-flex;
  gap: 3px;
}

.thinking-dots span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-muted);
  animation: bounce 1.2s infinite ease-in-out;
}

.thinking-dots span:nth-child(2) { animation-delay: 0.15s; }
.thinking-dots span:nth-child(3) { animation-delay: 0.3s; }

@keyframes bounce {
  0%, 80%, 100% { transform: translateY(0); opacity: 0.4; }
  40% { transform: translateY(-4px); opacity: 1; }
}

/* Caret */
.caret {
  display: inline-block;
  width: 8px;
  height: 1em;
  background: var(--text);
  vertical-align: text-bottom;
  margin-left: 2px;
  animation: blink 1s steps(1) infinite;
}

@keyframes blink {
  50% { opacity: 0; }
}

/* Composer */
.composer-wrap {
  padding: 10px 16px 20px;
  background: var(--bg);
}

.composer {
  max-width: 768px;
  margin: 0 auto;
}

.composer-inner {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 10px 10px 10px 16px;
  box-shadow: var(--shadow);
  transition: border-color 0.15s, box-shadow 0.15s;
}

.composer-inner:focus-within {
  border-color: var(--text-faint);
  box-shadow: 0 0 0 1px var(--text-faint), var(--shadow);
}

#input {
  flex: 1;
  border: none;
  background: transparent;
  outline: none;
  resize: none;
  font-size: 15px;
  line-height: 1.5;
  max-height: 200px;
  padding: 6px 0;
}

#input::placeholder {
  color: var(--text-faint);
}

.send-btn {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s, opacity 0.15s, transform 0.1s;
}

.send-btn:hover:not(:disabled) {
  background: var(--accent-hover);
}

.send-btn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.send-btn svg {
  margin-left: 1px;
  margin-top: 1px;
}

.composer-hint {
  text-align: center;
  font-size: 11.5px;
  color: var(--text-faint);
  margin-top: 8px;
}

/* Scrollbar */
.chat::-webkit-scrollbar,
.sidebar-section::-webkit-scrollbar {
  width: 8px;
}
.chat::-webkit-scrollbar-track,
.sidebar-section::-webkit-scrollbar-track {
  background: transparent;
}
.chat::-webkit-scrollbar-thumb,
.sidebar-section::-webkit-scrollbar-thumb {
  background: var(--border-strong);
  border-radius: 4px;
}
.chat::-webkit-scrollbar-thumb:hover,
.sidebar-section::-webkit-scrollbar-thumb:hover {
  background: var(--text-faint);
}

/* Mobile */
.mobile-only { display: none; }
.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  z-index: 15;
  opacity: 0;
  transition: opacity 0.2s;
}

@media (max-width: 768px) {
  .mobile-only { display: inline-flex; }
  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    transform: translateX(-100%);
    height: 100vh;
    box-shadow: 2px 0 12px rgba(0,0,0,0.15);
  }
  .sidebar.open { transform: translateX(0); }
  .sidebar-backdrop.visible { display: block; opacity: 1; }
  .welcome h1 { font-size: 22px; }
  .welcome { padding: 40px 12px 24px; }
  .suggestions { grid-template-columns: 1fr; }
  .chat-inner { padding: 16px 12px 24px; }
  .composer-wrap { padding: 8px 12px 14px; }
  .topbar { padding: 0 8px 0 4px; }
  .sidebar-toggle { display: none; }
}
