:root {
  --bg: #0f1115;
  --bg-raised: #171a21;
  --bg-input: #1d212b;
  --border: #2a2f3a;
  --text: #e8eaf0;
  --text-muted: #8b93a5;
  --accent: #d97e4a;
  --accent-dim: #b5643a;
  --user-bubble: #2b3242;
  --error: #e06c6c;
  --radius: 14px;
}

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

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

body { overflow: hidden; }

.muted { color: var(--text-muted); }
.error { color: var(--error); font-size: 0.9em; margin-top: 10px; }

/* ── buttons ── */
.btn {
  border: 1px solid var(--border);
  background: var(--bg-input);
  color: var(--text);
  border-radius: 10px;
  padding: 8px 14px;
  font-size: 0.95em;
  cursor: pointer;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: #16100b; font-weight: 600; }
.btn.primary:active { background: var(--accent-dim); }
.btn.ghost { background: transparent; }
.btn.small { padding: 5px 11px; font-size: 0.85em; }
.btn.wide { width: 100%; padding: 12px; margin-top: 18px; }
.btn:disabled { opacity: 0.5; cursor: default; }
.icon-btn {
  background: none; border: none; color: var(--text);
  font-size: 1.3em; cursor: pointer; padding: 4px 10px;
}

/* ── login ── */
.login-body { display: flex; align-items: center; justify-content: center; }
.login-card {
  width: min(360px, 90vw);
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 32px 28px;
}
.login-card h1 { font-size: 1.4em; margin-bottom: 2px; }
.login-card .muted { font-size: 0.85em; margin-bottom: 24px; }
.login-card label { display: block; font-size: 0.85em; color: var(--text-muted); margin: 14px 0 6px; }
.login-card input {
  width: 100%;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text);
  padding: 11px 12px;
  font-size: 1em;
}
.login-card input:focus { outline: none; border-color: var(--accent); }
.login-card .step-hint { font-size: 0.85em; margin-top: 18px; }
.login-card .btn.ghost.wide { margin-top: 8px; }

/* ── layout ── */
.chat {
  height: 100dvh;
  display: flex;
  flex-direction: column;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  padding-top: calc(10px + env(safe-area-inset-top));
  border-bottom: 1px solid var(--border);
  background: var(--bg-raised);
}
.conv-title {
  flex: 1;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
#modelSelect {
  background: var(--bg-input);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 8px;
  font-size: 0.85em;
}

/* ── sidebar ── */
.sidebar {
  position: fixed;
  inset: 0 auto 0 0;
  width: min(300px, 82vw);
  background: var(--bg-raised);
  border-right: 1px solid var(--border);
  transform: translateX(-100%);
  transition: transform 0.22s ease;
  z-index: 30;
  display: flex;
  flex-direction: column;
  padding: 14px;
  padding-top: calc(14px + env(safe-area-inset-top));
}
.sidebar.open { transform: translateX(0); }
.sidebar-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 14px;
}
.sidebar-title { font-weight: 700; }
.conv-list { flex: 1; overflow-y: auto; list-style: none; }
.conv-list li {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 10px 10px;
  border-radius: 10px;
  cursor: pointer;
  font-size: 0.92em;
}
.conv-list li.active { background: var(--bg-input); }
.conv-list li span {
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.conv-list li button {
  background: none; border: none; color: var(--text-muted);
  cursor: pointer; font-size: 1em; padding: 2px 6px;
}
.overlay {
  position: fixed; inset: 0;
  background: rgba(0, 0, 0, 0.5);
  opacity: 0; pointer-events: none;
  transition: opacity 0.22s ease;
  z-index: 20;
}
.overlay.show { opacity: 1; pointer-events: auto; }

/* ── messages ── */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 14px 8px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.empty-state { margin: auto; text-align: center; }
.empty-state h2 { font-size: 1.15em; margin-bottom: 6px; }
.empty-state p { font-size: 0.9em; }

.msg {
  max-width: 92%;
  line-height: 1.5;
  word-wrap: break-word;
  overflow-wrap: break-word;
}
.msg.user {
  align-self: flex-end;
  background: var(--user-bubble);
  border-radius: var(--radius) var(--radius) 4px var(--radius);
  padding: 10px 14px;
  white-space: pre-wrap;
}
.msg.assistant { align-self: flex-start; width: 100%; max-width: 100%; }
.msg.assistant p { margin: 0 0 10px; }
.msg.assistant p:last-child { margin-bottom: 0; }
.msg.assistant h1, .msg.assistant h2, .msg.assistant h3 { margin: 14px 0 8px; font-size: 1.05em; }
.msg.assistant ul, .msg.assistant ol { margin: 0 0 10px 22px; }
.msg.assistant li { margin-bottom: 4px; }
.msg.assistant a { color: var(--accent); }
.msg.assistant blockquote {
  border-left: 3px solid var(--border);
  padding-left: 12px;
  color: var(--text-muted);
  margin-bottom: 10px;
}
.msg.assistant code {
  background: var(--bg-input);
  border-radius: 5px;
  padding: 2px 5px;
  font-size: 0.86em;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
.msg.assistant pre {
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px;
  overflow-x: auto;
  margin-bottom: 10px;
}
.msg.assistant pre code { background: none; padding: 0; }
.msg.assistant table { border-collapse: collapse; margin-bottom: 10px; max-width: 100%; display: block; overflow-x: auto; }
.msg.assistant th, .msg.assistant td { border: 1px solid var(--border); padding: 6px 10px; font-size: 0.9em; }

.msg.status {
  align-self: flex-start;
  color: var(--text-muted);
  font-size: 0.82em;
  font-style: italic;
}
.msg.error-msg { align-self: flex-start; color: var(--error); font-size: 0.9em; }

.typing::after {
  content: "▍";
  animation: blink 1s step-start infinite;
  color: var(--accent);
}
@keyframes blink { 50% { opacity: 0; } }

/* ── composer ── */
.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 12px;
  padding-bottom: calc(10px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  background: var(--bg-raised);
}
.composer textarea {
  flex: 1;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 12px;
  color: var(--text);
  padding: 11px 13px;
  font-size: 1em;
  font-family: inherit;
  resize: none;
  max-height: 140px;
  line-height: 1.4;
}
.composer textarea:focus { outline: none; border-color: var(--accent); }
.btn.send {
  background: var(--accent);
  border-color: var(--accent);
  color: #16100b;
  font-weight: 700;
  font-size: 1.1em;
  border-radius: 12px;
  padding: 10px 16px;
}

/* ── desktop ── */
@media (min-width: 900px) {
  .sidebar { transform: translateX(0); position: static; }
  .overlay { display: none; }
  body { display: flex; }
  .chat { flex: 1; }
  #menuBtn { display: none; }
  .messages { padding: 24px 10vw 12px; }
  .composer { padding-left: 10vw; padding-right: 10vw; }
}
