:root {
  --bg: #ffffff;
  --bg-2: #f6f6f8;
  --bg-3: #ececf1;
  --ink: #1d1d22;
  --ink-2: #5d5d68;
  --ink-3: #8a8a96;
  --line: #e3e3ea;
  --accent: #4f46e5;
  --accent-ink: #ffffff;
  --user: #eef0ff;
  --danger: #c62828;
  --good: #2e7d32;
  --code-bg: #f6f8fa;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "Cascadia Code", "SF Mono", Consolas, monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17171b;
    --bg-2: #1f1f25;
    --bg-3: #2a2a32;
    --ink: #ececf1;
    --ink-2: #b4b4c0;
    --ink-3: #868694;
    --line: #2f2f38;
    --accent: #7c74ff;
    --user: #26264a;
    --danger: #ef6b6b;
    --good: #6fcf73;
    --code-bg: #0f0f13;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { font-family: var(--font); background: var(--bg); color: var(--ink); font-size: 15px; }
[hidden] { display: none !important; }
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; padding: 0; }
input, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }
.muted { color: var(--ink-2); }

/* ── Screens ─────────────────────────────────────────── */
.center-screen { min-height: 100dvh; display: grid; place-items: center; padding: 16px; background: var(--bg-2); }
.card { width: 100%; max-width: 380px; background: var(--bg); border: 1px solid var(--line); border-radius: 16px; padding: 28px; display: flex; flex-direction: column; gap: 12px; text-align: center; }
.card h1 { margin: 4px 0 0; font-size: 22px; }
.card p { margin: 0; line-height: 1.5; }
.card input { padding: 11px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); }
.card input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.primary { background: var(--accent); color: var(--accent-ink); padding: 11px 14px; border-radius: 10px; font-weight: 600; }
.primary:disabled { opacity: .6; cursor: default; }
.msg { min-height: 1.2em; font-size: 14px; }
.msg.error { color: var(--danger); }
.logo { display: block; width: 52px; height: 52px; margin: 0 auto; }
.logo.big { width: 64px; height: 64px; filter: drop-shadow(0 8px 18px rgba(79, 70, 229, .25)); }
.brand { display: flex; align-items: center; gap: 10px; padding: 14px 14px 2px; font-weight: 700; font-size: 15px; letter-spacing: -.01em; }
.brand img { width: 26px; height: 26px; }
.spinner { width: 28px; height: 28px; border: 3px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Layout ──────────────────────────────────────────── */
#app { display: flex; height: 100dvh; overflow: hidden; }
.sidebar { width: 280px; flex: none; background: var(--bg-2); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.sidebar-top { display: flex; gap: 8px; padding: 12px; align-items: center; }
.new-chat { flex: 1; text-align: left; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); font-weight: 600; }
.new-chat:hover { border-color: var(--accent); }
.icon-btn { width: 36px; height: 36px; border-radius: 8px; display: grid; place-items: center; font-size: 18px; color: var(--ink-2); flex: none; }
.icon-btn:hover { background: var(--bg-3); }
.chat-list { flex: 1; overflow-y: auto; padding: 4px 8px 12px; display: flex; flex-direction: column; gap: 2px; }
.chat-item { display: flex; align-items: center; gap: 4px; border-radius: 8px; }
.chat-item:hover, .chat-item.active { background: var(--bg-3); }
.chat-item .open { flex: 1; min-width: 0; text-align: left; padding: 9px 10px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item .del { opacity: 0; padding: 6px 8px; color: var(--ink-3); border-radius: 6px; }
.chat-item:hover .del, .chat-item.active .del { opacity: 1; }
.chat-item .del:hover { color: var(--danger); }
.list-label { font-size: 12px; color: var(--ink-3); padding: 12px 10px 4px; }
.sidebar-bottom { border-top: 1px solid var(--line); padding: 10px 14px; display: flex; justify-content: space-between; align-items: center; gap: 8px; font-size: 13px; }
.who { color: var(--ink-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.link-btn { color: var(--ink-2); text-decoration: underline; flex: none; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar { height: 52px; flex: none; display: flex; align-items: center; gap: 8px; padding: 0 12px; border-bottom: 1px solid var(--line); }
.chat-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.scroll { flex: 1; overflow-y: auto; position: relative; }
.messages { max-width: 800px; margin: 0 auto; padding: 24px 16px 8px; display: flex; flex-direction: column; gap: 22px; }

.empty { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; padding: 16px; gap: 8px; }
.empty h2 { margin: 8px 0 0; font-size: 24px; }
.empty p { margin: 0; max-width: 420px; line-height: 1.5; }

/* Wide screens: the chats column can be hidden; the menu button shows only while it is. */
@media (min-width: 900px) {
  .backdrop { display: none; }
  #app:not(.sidebar-hidden) #menu { display: none; }
  #app.sidebar-hidden .sidebar { display: none; }
}

/* Phones: the chats column is a drawer. */
@media (max-width: 899px) {
  .sidebar { position: fixed; z-index: 20; top: 0; bottom: 0; left: 0; width: min(300px, 85vw); transform: translateX(-100%); transition: transform .2s ease; }
  #app.drawer-open .sidebar { transform: none; }
  .backdrop { position: fixed; inset: 0; z-index: 10; background: rgba(0,0,0,.35); display: none; }
  #app.drawer-open .backdrop { display: block; }
  .messages { padding-top: 16px; }
}

/* ── Messages ────────────────────────────────────────── */
.row-user { display: flex; justify-content: flex-end; }
.bubble { max-width: 80%; background: var(--user); padding: 10px 14px; border-radius: 16px 16px 4px 16px; white-space: pre-wrap; word-wrap: break-word; line-height: 1.55; }
.row-bot { display: flex; flex-direction: column; gap: 8px; }
.answer { line-height: 1.7; font-size: 15.5px; word-wrap: break-word; }
.answer > :first-child { margin-top: 0; }
.answer > :last-child { margin-bottom: 0; }
.answer p { margin: 0 0 12px; }
.answer h1, .answer h2, .answer h3, .answer h4 { margin: 18px 0 8px; line-height: 1.3; }
.answer h1 { font-size: 20px; } .answer h2 { font-size: 18px; } .answer h3, .answer h4 { font-size: 16px; }
.answer ul, .answer ol { margin: 0 0 12px; padding-left: 24px; }
.answer li { margin-bottom: 4px; }
.answer blockquote { margin: 0 0 12px; padding-left: 12px; border-left: 3px solid var(--line); color: var(--ink-2); }
.answer table { border-collapse: collapse; margin: 0 0 12px; display: block; overflow-x: auto; max-width: 100%; }
.answer th, .answer td { border: 1px solid var(--line); padding: 6px 10px; text-align: left; vertical-align: top; }
.answer th { background: var(--bg-2); }
.answer code { font-family: var(--mono); font-size: .9em; background: var(--bg-3); padding: 1px 5px; border-radius: 5px; }
.answer pre { position: relative; margin: 0 0 12px; background: var(--code-bg); border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.answer pre code { display: block; padding: 14px; overflow-x: auto; background: none; font-size: 13.5px; line-height: 1.5; }
.answer pre code.hljs { background: none; }
.code-copy { position: absolute; top: 6px; right: 6px; font-size: 12px; padding: 3px 8px; border-radius: 6px; background: var(--bg); border: 1px solid var(--line); color: var(--ink-2); opacity: 0; }
.answer pre:hover .code-copy { opacity: 1; }
.typing::after { content: "▍"; animation: blink 1s steps(2) infinite; color: var(--ink-3); }
@keyframes blink { 50% { opacity: 0; } }
.error-box { color: var(--danger); background: color-mix(in srgb, var(--danger) 8%, transparent); border-radius: 10px; padding: 10px 12px; font-size: 14px; display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.error-box button { text-decoration: underline; color: inherit; }
.stopped { color: var(--ink-3); font-size: 13px; font-style: italic; }

/* The line under each answer: model, cost, time, feedback. */
.meta { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 10px; font-size: 12.5px; color: var(--ink-3); }
.meta .tag { background: var(--bg-2); border: 1px solid var(--line); border-radius: 999px; padding: 2px 9px; color: var(--ink-2); }
.meta .act { padding: 3px 7px; border-radius: 6px; font-size: 14px; line-height: 1; }
.meta .act:hover { background: var(--bg-3); }
.meta .act.on-good { color: var(--good); background: color-mix(in srgb, var(--good) 12%, transparent); }
.meta .act.on-bad { color: var(--danger); background: color-mix(in srgb, var(--danger) 12%, transparent); }
.meta .spacer { flex: 1; }
.fb-form { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; font-size: 13px; background: var(--bg-2); border: 1px solid var(--line); border-radius: 10px; padding: 10px; }
.fb-form select, .fb-form input { padding: 6px 8px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); }
.fb-form input { flex: 1; min-width: 160px; }
.fb-form button { padding: 6px 12px; border-radius: 8px; background: var(--accent); color: var(--accent-ink); font-weight: 600; }
.fb-form button.ghost { background: none; color: var(--ink-2); font-weight: 400; }
.fb-note { font-size: 13px; color: var(--ink-2); font-style: italic; }

/* ── Composer ────────────────────────────────────────── */
.composer-wrap { flex: none; padding: 8px 16px calc(12px + env(safe-area-inset-bottom)); }
.composer-wrap > * { max-width: 800px; margin-left: auto; margin-right: auto; }
.status { font-size: 13px; color: var(--ink-3); min-height: 0; padding: 0 4px 6px; }
.status:empty { display: none; }
.composer { display: flex; align-items: flex-end; gap: 8px; border: 1px solid var(--line); border-radius: 16px; padding: 8px 8px 8px 14px; background: var(--bg); box-shadow: 0 2px 10px rgba(0,0,0,.04); }
.composer:focus-within { border-color: var(--accent); }
.composer textarea { flex: 1; border: 0; outline: 0; resize: none; background: none; line-height: 1.5; max-height: 220px; padding: 6px 0; font-size: 15px; }
.send { width: 36px; height: 36px; border-radius: 10px; background: var(--accent); color: var(--accent-ink); font-size: 18px; font-weight: 700; flex: none; }
.send:disabled { opacity: .4; cursor: default; }
.send.stop { background: var(--ink); color: var(--bg); font-size: 14px; }
.hint { font-size: 12px; color: var(--ink-3); text-align: center; padding-top: 6px; }
@media (max-width: 899px) { .hint { display: none; } }
