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

:root {
  --bg: #0d0d0d;
  --surface: #161616;
  --surface2: #1e1e1e;
  --sidebar-bg: #111;
  --border: #2a2a2a;
  --text: #e8e8e8;
  --text-muted: #888;
  --accent: #3b82f6;
  --accent-hover: #2563eb;
  --tool: #854d0e;
  --tool-bg: #1c1108;
  --success: #16a34a;
  --danger: #dc2626;
  --radius: 12px;
  --radius-sm: 8px;
  --sidebar-w: 260px;
}

html, body {
  height: 100%;
  font-family: 'Inter', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.6;
  overflow: hidden;
}

/* ── App shell ── */
#app {
  display: flex;
  height: 100vh;
  height: 100dvh;
}

/* ── Sidebar ── */
#sidebar {
  width: var(--sidebar-w);
  background: var(--sidebar-bg);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  overflow: hidden;
}

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

.sidebar-title {
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.new-conv-btn {
  background: var(--accent);
  border: none;
  color: #fff;
  width: 26px;
  height: 26px;
  border-radius: 6px;
  font-size: 1.2rem;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding-bottom: 1px;
  transition: background 0.15s;
}
.new-conv-btn:hover { background: var(--accent-hover); }

.conv-list {
  flex: 1;
  overflow-y: auto;
  padding: 6px 0;
}
.conv-list::-webkit-scrollbar { width: 3px; }
.conv-list::-webkit-scrollbar-thumb { background: var(--border); }

.conv-list-loading {
  font-size: 0.8rem;
  color: var(--text-muted);
  padding: 12px 14px;
}

.conv-item {
  padding: 9px 14px;
  cursor: pointer;
  border-radius: 0;
  transition: background 0.1s;
  border-left: 2px solid transparent;
}
.conv-item:hover { background: var(--surface2); }
.conv-item.active {
  background: var(--surface2);
  border-left-color: var(--accent);
}
.conv-item-preview {
  font-size: 0.82rem;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.4;
}
.conv-item-meta {
  font-size: 0.7rem;
  color: var(--text-muted);
  margin-top: 2px;
}

/* ── Main panel ── */
#main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow: hidden;
}

/* ── Header ── */
header {
  padding: 12px 16px 10px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}

.sidebar-toggle {
  display: none;
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 1.2rem;
  cursor: pointer;
  padding: 4px 6px;
  border-radius: 6px;
  line-height: 1;
}
.sidebar-toggle:hover { color: var(--text); }

.header-title .logo { font-weight: 700; font-size: 0.95rem; }
.header-title .logo span { color: var(--accent); }
.header-title .subtitle { font-size: 0.72rem; color: var(--text-muted); }

.model-badge {
  margin-left: auto;
  font-size: 0.68rem;
  padding: 3px 8px;
  border-radius: 20px;
  border: 1px solid var(--border);
  color: var(--text-muted);
  white-space: nowrap;
}

/* ── Chat window ── */
#chat-window {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}
#chat-window::-webkit-scrollbar { width: 4px; }
#chat-window::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

/* ── Empty state ── */
#empty-state {
  margin: auto;
  padding: 32px 20px;
  text-align: center;
  color: var(--text-muted);
  max-width: 520px;
  width: 100%;
}
#empty-state h2 { font-size: 1.15rem; color: var(--text); margin-bottom: 6px; }
#empty-state p { font-size: 0.85rem; margin-bottom: 20px; }
.suggestion-chips { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.chip {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 20px;
  padding: 6px 14px;
  font-size: 0.8rem;
  cursor: pointer;
  transition: border-color 0.15s;
  text-align: left;
}
.chip:hover { border-color: var(--accent); }

/* ── Messages ── */
.msg { display: flex; gap: 10px; animation: fadeIn 0.2s ease; }
.msg.user { flex-direction: row-reverse; }

.avatar {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.78rem;
  flex-shrink: 0;
  font-weight: 600;
}
.msg.user .avatar { background: var(--accent); color: #fff; }
.msg.assistant .avatar { background: var(--surface2); color: var(--text-muted); }

.msg-right { display: flex; flex-direction: column; max-width: min(78%, 640px); min-width: 0; }

.bubble {
  padding: 10px 14px;
  border-radius: var(--radius);
  line-height: 1.65;
  word-break: break-word;
}
.msg.user .bubble {
  background: var(--accent);
  color: #fff;
  border-bottom-right-radius: 4px;
}
.msg.assistant .bubble {
  background: var(--surface);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}

/* ── Markdown ── */
.bubble h1, .bubble h2, .bubble h3 { margin: 12px 0 6px; }
.bubble h1 { font-size: 1.05em; border-bottom: 1px solid var(--border); padding-bottom: 4px; }
.bubble h2 { font-size: 1em; }
.bubble h3 { font-size: 0.95em; }
.bubble p { margin: 6px 0; }
.bubble ul, .bubble ol { margin: 6px 0 6px 20px; }
.bubble li { margin: 2px 0; }
.bubble code { background: var(--surface2); padding: 1px 5px; border-radius: 4px; font-family: monospace; font-size: 0.88em; }
.bubble pre { background: var(--surface2); padding: 10px; border-radius: var(--radius-sm); overflow-x: auto; margin: 8px 0; }
.bubble pre code { background: none; padding: 0; }
.bubble a { color: var(--accent); }
.bubble strong { color: #fff; }
.bubble hr { border: none; border-top: 1px solid var(--border); margin: 10px 0; }
.bubble table { border-collapse: collapse; width: 100%; font-size: 0.85em; margin: 8px 0; }
.bubble th, .bubble td { border: 1px solid var(--border); padding: 5px 8px; text-align: left; }
.bubble th { background: var(--surface2); }

/* ── Tool indicators ── */
.tools-area { display: flex; flex-direction: column; gap: 4px; margin-bottom: 4px; }
.tool-indicator {
  display: flex; align-items: center; gap: 6px;
  font-size: 0.75rem; color: #a16207;
  background: var(--tool-bg); border: 1px solid #451a03;
  border-radius: var(--radius-sm); padding: 4px 10px;
  width: fit-content;
}
.tool-indicator .spinner {
  width: 9px; height: 9px;
  border: 2px solid #a16207; border-top-color: transparent;
  border-radius: 50%; animation: spin 0.8s linear infinite; flex-shrink: 0;
}
.tool-indicator.done { color: var(--success); background: #052e16; border-color: #14532d; }
.tool-indicator.done .spinner { display: none; }

/* ── Router badge + cost ── */
.router-badge {
  font-size: 0.67rem; color: var(--text-muted);
  padding: 2px 7px; border-radius: 20px;
  border: 1px solid var(--border); display: inline-block; margin-bottom: 4px; width: fit-content;
}
.cost-line { font-size: 0.67rem; color: var(--text-muted); margin-top: 5px; }

/* ── Typing cursor ── */
.cursor {
  display: inline-block; width: 2px; height: 1em;
  background: var(--text); margin-left: 2px;
  animation: blink 1s step-end infinite; vertical-align: text-bottom;
}

/* ── Input area ── */
#input-area {
  padding: 10px 16px 14px;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
  background: var(--bg);
}
#input-form { display: flex; gap: 8px; align-items: flex-end; }
#user-input {
  flex: 1; background: var(--surface); border: 1px solid var(--border);
  color: var(--text); border-radius: var(--radius);
  padding: 10px 14px; font-size: 0.9rem; font-family: inherit;
  resize: none; min-height: 44px; max-height: 140px; outline: none;
  line-height: 1.5; transition: border-color 0.15s;
}
#user-input:focus { border-color: var(--accent); }
#user-input::placeholder { color: var(--text-muted); }

#send-btn {
  background: var(--accent); color: #fff; border: none;
  border-radius: var(--radius); width: 44px; height: 44px;
  cursor: pointer; display: flex; align-items: center; justify-content: center;
  transition: background 0.15s; flex-shrink: 0;
}
#send-btn:hover:not(:disabled) { background: var(--accent-hover); }
#send-btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* ── Mobile sidebar overlay ── */
.sidebar-overlay {
  display: none;
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.6);
  z-index: 40;
}

/* ── Animations ── */
@keyframes fadeIn { from { opacity: 0; transform: translateY(5px); } to { opacity: 1; transform: translateY(0); } }
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes blink { 0%, 100% { opacity: 1; } 50% { opacity: 0; } }

/* ── Mobile (≤ 700px) ── */
@media (max-width: 700px) {
  #sidebar {
    position: fixed;
    left: 0; top: 0; bottom: 0;
    z-index: 50;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    width: 280px;
  }
  #sidebar.open { transform: translateX(0); }
  .sidebar-overlay.open { display: block; }
  .sidebar-toggle { display: flex; }

  #chat-window { padding: 12px; gap: 12px; }
  .msg-right { max-width: 86%; }
  .bubble { font-size: 0.88rem; padding: 9px 12px; }
  .header-title .subtitle { display: none; }
  #input-area { padding: 8px 12px 12px; }
  .chip { font-size: 0.78rem; padding: 5px 12px; }
}

/* ── Very small screens (≤ 380px) ── */
@media (max-width: 380px) {
  .msg-right { max-width: 92%; }
  .bubble { font-size: 0.85rem; }
}
