:root {
  --bg: #0f1117;
  --bg-alt: #171a23;
  --panel: #1c202b;
  --border: #2a2f3d;
  --text: #e6e8ee;
  --text-dim: #9aa1b2;
  --accent: #d97848;
  --accent-soft: rgba(217, 120, 72, 0.15);
  --danger: #e5534b;
  --radius: 10px;
  --sidebar-w: 300px;
  font-size: 15px;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: ui-sans-serif, system-ui, "Segoe UI", Helvetica, Arial, sans-serif;
  line-height: 1.55;
}
.hidden { display: none !important; }
button { font: inherit; cursor: pointer; }

/* ---------- Login ---------- */
.login-screen {
  position: fixed; inset: 0;
  display: flex; align-items: center; justify-content: center;
  background: radial-gradient(ellipse at 30% 20%, #1b2030 0%, var(--bg) 65%);
}
.login-card {
  width: min(360px, 90vw);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 2rem 2rem 1.6rem;
  display: flex; flex-direction: column; gap: 0.9rem;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}
.login-logo { font-size: 2.4rem; text-align: center; }
.login-card h1 { margin: 0; text-align: center; font-size: 1.4rem; }
.login-sub { margin: -0.6rem 0 0.4rem; text-align: center; color: var(--text-dim); font-size: 0.85rem; }
.login-card label { display: flex; flex-direction: column; gap: 0.3rem; font-size: 0.85rem; color: var(--text-dim); }
.login-card input {
  background: var(--bg-alt); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px;
  padding: 0.6rem 0.7rem; font-size: 1rem; outline: none;
}
.login-card input:focus { border-color: var(--accent); }
.login-card button[type="submit"] {
  margin-top: 0.4rem;
  background: var(--accent); color: #fff; border: none;
  border-radius: 8px; padding: 0.65rem; font-weight: 600;
}
.login-card button[type="submit"]:disabled { opacity: 0.6; cursor: wait; }
.login-error { color: var(--danger); font-size: 0.85rem; text-align: center; margin: 0; }

/* ---------- Layout ---------- */
.app { display: flex; height: 100vh; height: 100dvh; }
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }

/* ---------- Sidebar ---------- */
.sidebar {
  width: var(--sidebar-w);
  background: var(--bg-alt);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  padding: 0.8rem;
  gap: 0.8rem;
  transition: margin-left 0.2s ease;
}
.sidebar.collapsed { margin-left: calc(-1 * var(--sidebar-w)); }
.sidebar-head { display: flex; align-items: center; justify-content: space-between; }
.sidebar-title { font-weight: 700; }
.sidebar-backdrop { display: none; }

.icon-btn {
  background: transparent; border: 1px solid transparent; color: var(--text-dim);
  border-radius: 8px; padding: 0.35rem 0.55rem;
}
.icon-btn:hover { background: var(--panel); color: var(--text); }

.usage-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.9rem;
}
.usage-today { display: flex; align-items: center; gap: 0.7rem; margin-bottom: 0.7rem; }
.usage-flame { font-size: 1.7rem; }
.usage-tokens { font-size: 1.25rem; font-weight: 700; letter-spacing: 0.02em; }
.usage-label { color: var(--text-dim); font-size: 0.75rem; }
.usage-table { width: 100%; border-collapse: collapse; font-size: 0.83rem; }
.usage-table td { padding: 0.22rem 0; color: var(--text-dim); }
.usage-table td:last-child { text-align: right; color: var(--text); font-variant-numeric: tabular-nums; }

.usage-limits { margin-top: 0.6rem; font-size: 0.75rem; color: var(--text-dim); }
.usage-limits .bar {
  height: 4px; border-radius: 2px; background: var(--border);
  margin: 0.25rem 0 0.5rem; overflow: hidden;
}
.usage-limits .bar > div { height: 100%; background: var(--accent); }
.usage-limits .bar > div.over { background: var(--danger); }
.usage-limits .paused { color: var(--danger); font-weight: 600; }

.new-chat-btn {
  background: var(--accent-soft); color: var(--accent);
  border: 1px solid var(--accent); border-radius: var(--radius);
  padding: 0.55rem; font-weight: 600;
}
.new-chat-btn:hover { background: var(--accent); color: #fff; }

.conversation-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; }
.conversation-item {
  display: flex; align-items: center; gap: 0.4rem;
  padding: 0.45rem 0.55rem; border-radius: 8px;
  color: var(--text-dim); font-size: 0.87rem;
  cursor: pointer; border: none; background: none; text-align: left; width: 100%;
}
.conversation-item:hover { background: var(--panel); color: var(--text); }
.conversation-item.active { background: var(--panel); color: var(--text); }
.conversation-item .title { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conversation-item .del { visibility: hidden; color: var(--text-dim); }
.conversation-item:hover .del { visibility: visible; }
.conversation-item .del:hover { color: var(--danger); }

.sidebar-foot {
  display: flex; align-items: center; justify-content: space-between;
  border-top: 1px solid var(--border); padding-top: 0.6rem;
  color: var(--text-dim); font-size: 0.85rem;
}

/* ---------- Top bar ---------- */
.topbar {
  display: flex; align-items: center; gap: 0.6rem;
  padding: 0.55rem 0.9rem;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}
.model-select {
  background: var(--panel); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px;
  padding: 0.4rem 0.6rem; font-size: 0.9rem; max-width: 60vw;
}
.conn-warning { color: #e5b84b; font-size: 0.8rem; margin-left: auto; }

/* ---------- Messages ---------- */
.messages { flex: 1; overflow-y: auto; padding: 1.2rem 0 2rem; }
.empty-state { text-align: center; color: var(--text-dim); margin-top: 18vh; }
.empty-logo { font-size: 3rem; }
.empty-state h2 { color: var(--text); margin: 0.4rem 0; }

.msg { max-width: 820px; margin: 0 auto 1.1rem; padding: 0 1.2rem; }
.msg-user .bubble {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px 14px 4px 14px;
  padding: 0.7rem 1rem;
  margin-left: auto;
  width: fit-content; max-width: 85%;
  white-space: pre-wrap; word-break: break-word;
}
.msg-attachments { display: flex; gap: 0.4rem; flex-wrap: wrap; justify-content: flex-end; margin-bottom: 0.3rem; }
.msg-attachments .chip, .attachment-bar .chip {
  background: var(--accent-soft); color: var(--accent);
  border-radius: 999px; padding: 0.15rem 0.6rem; font-size: 0.78rem;
  display: inline-flex; align-items: center; gap: 0.35rem;
}
.attachment-bar { max-width: 820px; margin: 0 auto; padding: 0 1.2rem; display: flex; gap: 0.4rem; flex-wrap: wrap; }
.attachment-bar .chip button { background: none; border: none; color: inherit; padding: 0; }

.msg-assistant .meta {
  display: flex; align-items: center; gap: 0.5rem;
  color: var(--text-dim); font-size: 0.78rem; margin-bottom: 0.25rem;
}
.msg-assistant .meta .model-tag { color: var(--accent); font-weight: 600; }
.msg-assistant .content { word-break: break-word; }
.msg-assistant .content > *:first-child { margin-top: 0; }

.thinking-box {
  border: 1px dashed var(--border); border-radius: 8px;
  margin-bottom: 0.6rem; font-size: 0.85rem; color: var(--text-dim);
}
.thinking-box summary { cursor: pointer; padding: 0.4rem 0.7rem; user-select: none; }
.thinking-box .thinking-text { padding: 0 0.9rem 0.6rem; white-space: pre-wrap; }

.notice { color: #e5b84b; font-size: 0.85rem; margin: 0.4rem 0; }
.notice.error { color: var(--danger); }

/* math */
.content .katex-display { overflow-x: auto; overflow-y: hidden; padding: 0.2rem 0; }

/* markdown content */
.content pre {
  position: relative;
  background: #11141c; border: 1px solid var(--border);
  border-radius: var(--radius); padding: 0.9rem;
  overflow-x: auto; font-size: 0.86rem;
}
.content code { font-family: ui-monospace, "Cascadia Code", Menlo, Consolas, monospace; }
.content :not(pre) > code {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: 5px; padding: 0.08rem 0.35rem; font-size: 0.86em;
}
.content table { border-collapse: collapse; margin: 0.6rem 0; display: block; overflow-x: auto; }
.content th, .content td { border: 1px solid var(--border); padding: 0.35rem 0.7rem; }
.content blockquote { border-left: 3px solid var(--accent); margin: 0.6rem 0; padding-left: 0.9rem; color: var(--text-dim); }
.content img { max-width: 100%; border-radius: 8px; }
.content a { color: var(--accent); }

.codebar {
  display: flex; align-items: center; justify-content: space-between;
  background: #171b26; border: 1px solid var(--border); border-bottom: none;
  border-radius: var(--radius) var(--radius) 0 0;
  padding: 0.25rem 0.6rem; font-size: 0.75rem; color: var(--text-dim);
  margin-top: 0.7rem;
}
.codebar + pre { border-radius: 0 0 var(--radius) var(--radius); margin-top: 0; }
.codebar .actions { display: flex; gap: 0.5rem; }
.codebar button {
  background: none; border: none; color: var(--text-dim);
  font-size: 0.75rem; padding: 0.1rem 0.3rem; border-radius: 4px;
}
.codebar button:hover { color: var(--text); background: var(--panel); }

.msg-actions { display: flex; gap: 0.6rem; margin-top: 0.3rem; }
.msg-actions button {
  background: none; border: none; color: var(--text-dim); font-size: 0.75rem; padding: 0;
}
.msg-actions button:hover { color: var(--text); }

.cursor { display: inline-block; width: 7px; height: 1em; background: var(--accent); vertical-align: text-bottom; animation: blink 1s steps(1) infinite; }
@keyframes blink { 50% { opacity: 0; } }

/* ---------- Composer ---------- */
.composer-wrap { padding: 0.4rem 0 0.9rem; background: linear-gradient(transparent, var(--bg) 30%); }
.composer {
  max-width: 820px; margin: 0.4rem auto 0; padding: 0.45rem 0.55rem;
  display: flex; align-items: flex-end; gap: 0.4rem;
  background: var(--panel); border: 1px solid var(--border);
  border-radius: 16px;
  margin-left: auto; margin-right: auto;
}
@media (min-width: 900px) { .composer { margin-left: auto; margin-right: auto; } }
.composer textarea {
  flex: 1; resize: none; border: none; outline: none;
  background: transparent; color: var(--text);
  font: inherit; max-height: 40vh; padding: 0.35rem 0.2rem;
}
.send-btn {
  background: var(--accent); color: #fff; border: none;
  border-radius: 10px; width: 2.2rem; height: 2.2rem; font-size: 1rem;
}
.send-btn:disabled { opacity: 0.45; cursor: default; }
.composer-hint { text-align: center; color: var(--text-dim); font-size: 0.72rem; margin-top: 0.35rem; }

/* ---------- Mobile ---------- */
@media (max-width: 760px) {
  .sidebar {
    position: fixed; z-index: 30; height: 100dvh; left: 0; top: 0;
    margin-left: 0; transform: translateX(-100%);
    transition: transform 0.2s ease;
    width: min(var(--sidebar-w), 85vw);
  }
  .sidebar.open-mobile { transform: translateX(0); }
  .sidebar.collapsed { margin-left: 0; }
  .sidebar-backdrop.show {
    display: block; position: fixed; inset: 0; z-index: 20;
    background: rgba(0, 0, 0, 0.5);
  }

  /* iOS zooms the page when a focused input's font-size is < 16px */
  .composer textarea, .login-card input { font-size: 16px; }

  .topbar { padding: 0.45rem 0.6rem; }
  .model-select { flex: 1; min-width: 0; max-width: none; }
  .msg { padding: 0 0.8rem; margin-bottom: 0.9rem; }
  .msg-user .bubble { max-width: 95%; }
  .composer { margin: 0.4rem 0.6rem 0; }
  .composer-wrap { padding-bottom: max(0.9rem, env(safe-area-inset-bottom)); }
  .messages { padding-top: 0.8rem; }
}
