:root {
  --bg: #212121;
  --sidebar-bg: #171717;
  --panel: #2f2f2f;
  --panel-2: #3a3a3a;
  --border: #4a4a4a;
  --text: #f5f5f5;
  --text-2: #c9c9c9;
  --muted: #9d9d9d;
  --placeholder: #8f8f8f;
  --accent: #10a37f;
  --accent-hover: #0f8f70;
  --danger: #f85149;
}

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

html, body { height: 100%; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 15px;
  line-height: 1.6;
  overflow: hidden;
}

button { font-family: inherit; }

/* ───────────── Login ───────────── */

#login-screen {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  z-index: 100;
}

#login-screen[hidden] { display: none; }

.login-card {
  width: min(400px, 92vw);
  text-align: center;
  padding: 20px;
}

.login-logo {
  width: 72px;
  height: 72px;
  margin: 0 auto 24px;
  border-radius: 50%;
  background: var(--panel);
  border: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 32px;
}

.login-card h1 { font-size: 26px; font-weight: 600; margin-bottom: 6px; }
.login-sub { color: var(--muted); font-size: 14px; margin-bottom: 28px; }

.pass-wrap { display: flex; gap: 10px; }

#pass-input {
  flex: 1;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text);
  padding: 13px 16px;
  font-size: 15px;
  outline: none;
}

#pass-input::placeholder { color: var(--placeholder); }
#pass-input:focus { border-color: var(--accent); }

#login-btn {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 10px;
  padding: 0 22px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}

#login-btn:hover { background: var(--accent-hover); }
#login-btn:disabled { opacity: 0.5; cursor: not-allowed; }

.error { color: var(--danger); font-size: 13px; margin-top: 12px; }

/* ───────────── Layout ───────────── */

#app { display: flex; height: 100%; }
#app[hidden] { display: none; }

#sidebar {
  width: 260px;
  flex-shrink: 0;
  background: var(--sidebar-bg);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 10px;
}

.new-chat {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 11px 14px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  margin-bottom: 8px;
}

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

#session-list {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.session {
  display: flex;
  align-items: center;
  width: 100%;
  background: transparent;
  border: none;
  color: var(--text-2);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 13.5px;
  text-align: left;
  cursor: pointer;
  gap: 6px;
}

.session:hover { background: var(--panel); color: var(--text); }

.session .s-title {
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.session .s-del {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 13px;
  cursor: pointer;
  padding: 2px 5px;
  border-radius: 4px;
  opacity: 0;
}

.session:hover .s-del { opacity: 1; }
.session .s-del:hover { background: var(--panel-2); color: var(--danger); }

.session.active { background: var(--panel-2); color: var(--text); }

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

.sidebar-btn {
  width: 100%;
  background: transparent;
  border: none;
  color: var(--text-2);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 13.5px;
  cursor: pointer;
  text-align: left;
  display: flex;
  align-items: center;
  gap: 8px;
}

.sidebar-btn:hover { background: var(--panel); color: var(--text); }

#main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  height: 100%;
}

/* ───────────── Topbar (mobile only) ───────────── */

#topbar { display: none; }

.ghost-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-2);
  border-radius: 8px;
  padding: 8px 14px;
  font-size: 13px;
  cursor: pointer;
}

.ghost-btn:hover { color: var(--text); border-color: var(--panel-2); background: var(--panel); }

/* ───────────── Welcome ───────────── */

.welcome {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 28px;
  padding: 24px;
  overflow-y: auto;
}

.welcome[hidden] { display: none; }

.welcome h2 {
  font-size: 32px;
  font-weight: 600;
  letter-spacing: -0.02em;
  text-align: center;
}

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

.sugg {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 16px;
  color: var(--text-2);
  padding: 16px;
  font-size: 14px;
  text-align: left;
  cursor: pointer;
  line-height: 1.5;
}

.sugg:hover { background: var(--panel); border-color: var(--panel-2); color: var(--text); }

/* ───────────── Messages ───────────── */

#messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px 0 8px;
  scroll-behavior: smooth;
}

#messages[hidden] { display: none; }

.msg {
  max-width: 768px;
  margin: 0 auto 26px;
  padding: 0 20px;
}

.msg.user { display: flex; justify-content: flex-end; }

.msg.user .bubble {
  max-width: 75%;
  background: rgba(255, 255, 255, 0.06);
  border-radius: 14px;
  border-bottom-right-radius: 4px;
  padding: 10px 14px;
  color: var(--text);
}

.msg.assistant { display: flex; flex-direction: column; }
.msg.assistant .bubble { max-width: 100%; color: var(--text); }

.model-label { font-size: 12px; color: var(--muted); margin-bottom: 6px; }

.msg .bubble { font-size: 15px; word-break: break-word; }

.msg .bubble p { margin-bottom: 10px; }
.msg .bubble p:last-child { margin-bottom: 0; }
.msg .bubble h1, .msg .bubble h2, .msg .bubble h3 { margin: 14px 0 8px; line-height: 1.3; }
.msg .bubble h1 { font-size: 20px; }
.msg .bubble h2 { font-size: 18px; }
.msg .bubble h3 { font-size: 16px; }
.msg .bubble ul, .msg .bubble ol { padding-left: 22px; margin-bottom: 10px; }
.msg .bubble li { margin-bottom: 4px; }
.msg .bubble a { color: var(--accent); text-decoration: none; }
.msg .bubble a:hover { text-decoration: underline; }
.msg .bubble code {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 1px 6px;
  font-size: 13px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color: var(--text-2);
}
.msg .bubble pre {
  background: #141414;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px;
  overflow-x: auto;
  margin: 10px 0;
}
.msg .bubble pre code { background: none; border: none; padding: 0; font-size: 13px; }

.msg.streaming .bubble::after {
  content: "▍";
  color: var(--muted);
  animation: blink 1s steps(2) infinite;
}

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

.think-section {
  margin-bottom: 10px;
}

.thinking-toggle {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 12px;
  cursor: pointer;
  padding: 2px 0;
  align-self: flex-start;
  display: block;
}

.thinking-toggle:hover { color: var(--text); }

.thinking-toggle:first-child { margin-bottom: 4px; }
.thinking-toggle:last-child { margin-top: 4px; }

.thinking {
  display: none;
  border-left: 3px solid var(--accent);
  border-radius: 0 8px 8px 0;
  background: rgba(255, 255, 255, 0.03);
  padding: 10px 14px;
  font-size: 13px;
  color: var(--secondary);
  white-space: pre-wrap;
  line-height: 1.5;
}

.thinking.open { display: block; }

/* ─── syntax highlighting ─── */

.msg .bubble .tok-kw { color: #ff7b72; }
.msg .bubble .tok-str { color: #a5d6ff; }
.msg .bubble .tok-com { color: #8b949e; font-style: italic; }
.msg .bubble .tok-num { color: #79c0ff; }
.msg .bubble .tok-fn { color: #d2a8ff; }
.msg .bubble .tok-tag { color: #7ee787; }
.msg .bubble .tok-attr { color: #79c0ff; }

/* ───────────── Composer ───────────── */

#composer-wrap {
  flex-shrink: 0;
  padding: 0 24px 12px;
  background: linear-gradient(transparent, var(--bg) 30%);
}

#composer {
  position: relative;
  max-width: 768px;
  margin: 0 auto;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 10px 12px;
}

#composer:focus-within { border-color: var(--panel-2); }

#chat-input {
  width: 100%;
  background: transparent;
  border: none;
  color: var(--text);
  font-family: inherit;
  font-size: 15px;
  line-height: 1.6;
  resize: none;
  max-height: 180px;
  outline: none;
  padding: 4px 48px 4px 8px;
}

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

#send-btn {
  position: absolute;
  right: 12px;
  bottom: 12px;
  width: 34px;
  height: 34px;
  border: none;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  font-size: 14px;
  cursor: pointer;
}

#send-btn:hover:not(:disabled) { background: var(--accent-hover); }
#send-btn:disabled { opacity: 0.4; cursor: default; }

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

#scroll-bottom-btn {
  position: fixed;
  right: 26px;
  bottom: 110px;
  z-index: 40;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  font-size: 16px;
  cursor: pointer;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
}

#scroll-bottom-btn:hover { background: var(--panel-2); }
#scroll-bottom-btn[hidden] { display: none; }

/* ───────────── Settings modal ───────────── */

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  padding: 16px;
}

.modal-overlay[hidden] { display: none; }

.modal {
  width: min(440px, 100%);
  max-height: 80vh;
  background: var(--sidebar-bg);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.5);
  padding: 18px;
  display: flex;
  flex-direction: column;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
}

.modal-header h3 { font-size: 16px; font-weight: 600; }

.modal-close {
  background: transparent;
  border: none;
  color: var(--muted);
  font-size: 15px;
  cursor: pointer;
  width: 28px;
  height: 28px;
  border-radius: 6px;
}

.modal-close:hover { background: var(--panel); color: var(--text); }

#settings-search {
  width: 100%;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  padding: 10px 12px;
  font-size: 13.5px;
  outline: none;
}

#settings-search::placeholder { color: var(--placeholder); }
#settings-search:focus { border-color: var(--accent); }

#settings-models {
  list-style: none;
  margin-top: 10px;
  overflow-y: auto;
}

.smodel {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 9px 12px;
  font-size: 13.5px;
  border-radius: 8px;
  cursor: pointer;
  color: var(--text-2);
}

.smodel:hover { background: var(--panel); color: var(--text); }
.smodel.selected { background: var(--panel-2); color: var(--text); }
.smodel .check { color: var(--accent); font-weight: 700; font-size: 14px; }
.smodel.empty { color: var(--muted); cursor: default; justify-content: center; }
.smodel.empty:hover { background: transparent; }

/* ───────────── Mobile ───────────── */

@media (max-width: 768px) {
  #sidebar { display: none; }
  #topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 14px;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
  }
  #composer-wrap { padding: 0 12px 10px; }
  .welcome h2 { font-size: 24px; }
  .suggestions { grid-template-columns: 1fr; }
  .msg { padding: 0 14px; }
  .msg.user .bubble { max-width: 85%; }
  .modal { width: 100%; }
}
