:root {
  --bg: #fffdf6;
  --panel: #f3f1e7;
  --surface: #ffffff;
  --text: #1c2420;
  --muted: #66706a;
  --line: #e2dfd2;
  --brand: #174e3b;
  --brand-ink: #ffffff;
  --accent: #d9eca9;
  --user: #e8f2d3;
  --code: #f1efe6;
  --danger: #b3261e;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #111613;
    --panel: #171d19;
    --surface: #1c231f;
    --text: #e7ece8;
    --muted: #9aa59e;
    --line: #2a332e;
    --brand: #b9dd78;
    --brand-ink: #10261c;
    --accent: #2b3a22;
    --user: #22301f;
    --code: #0c100e;
    --danger: #f2877f;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
[hidden] { display: none !important; }
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
.muted { color: var(--muted); }
.error { color: var(--danger); min-height: 1.2em; margin: 0; font-size: 14px; }

.primary {
  background: var(--brand); color: var(--brand-ink);
  border: 0; border-radius: 10px; padding: 10px 16px; font-weight: 600;
}
.primary:disabled { opacity: .5; cursor: default; }
.danger { background: none; border: 1px solid var(--danger); color: var(--danger); border-radius: 10px; padding: 9px 14px; }
.link { background: none; border: 0; color: var(--muted); padding: 6px 4px; }
.link:hover { color: var(--text); }
.icon { background: none; border: 0; font-size: 20px; padding: 4px 8px; border-radius: 8px; }
.icon:hover { background: var(--line); }

/* Sign-in */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: min(360px, 100%); background: var(--surface); border: 1px solid var(--line);
  border-radius: 18px; padding: 28px; display: flex; flex-direction: column; gap: 10px;
}
.login-card img { align-self: center; }
.login-card h1 { margin: 4px 0 0; text-align: center; font-size: 22px; }
.login-card p.muted { margin: 0 0 10px; text-align: center; font-size: 13px; }
.login-card label { font-size: 13px; font-weight: 600; }
input[type=password], textarea {
  width: 100%; border: 1px solid var(--line); background: var(--bg); border-radius: 10px; padding: 10px 12px;
}
input:focus-visible, textarea:focus-visible, button:focus-visible { outline: 2px solid var(--brand); outline-offset: 1px; }

/* Layout */
.app { display: flex; height: 100%; height: 100dvh; }
.sidebar {
  width: 260px; flex: none; background: var(--panel); border-right: 1px solid var(--line);
  display: flex; flex-direction: column; padding: 12px; gap: 10px;
}
.side-head { display: flex; align-items: center; justify-content: space-between; }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; }
.new-chat {
  border: 1px solid var(--line); background: var(--surface); border-radius: 10px; padding: 9px 12px; text-align: left; font-weight: 600;
}
.new-chat:hover { border-color: var(--brand); }
.history { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; }
.history .item { display: flex; align-items: center; border-radius: 8px; }
.history .item:hover, .history .item.active { background: var(--line); }
.history .item button.open {
  flex: 1; min-width: 0; background: none; border: 0; text-align: left; padding: 7px 8px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.history .item button.del { background: none; border: 0; color: var(--muted); padding: 4px 8px; visibility: hidden; }
.history .item:hover button.del, .history .item.active button.del { visibility: visible; }
.side-foot { display: flex; justify-content: space-between; border-top: 1px solid var(--line); padding-top: 8px; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar { display: flex; align-items: center; gap: 10px; padding: 10px 16px; border-bottom: 1px solid var(--line); min-height: 52px; }
.title { font-weight: 600; flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.model { font-size: 12px; color: var(--muted); background: var(--panel); border: 1px solid var(--line); border-radius: 999px; padding: 2px 10px; white-space: nowrap; }

.messages { flex: 1; overflow-y: auto; padding: 20px 16px 8px; }
.messages > * { max-width: 760px; margin-left: auto; margin-right: auto; }
.empty { text-align: center; margin-top: 18vh; }
.empty h2 { margin: 0 0 6px; }

.msg { margin-bottom: 18px; overflow-wrap: anywhere; }
.msg.user { display: flex; justify-content: flex-end; }
.msg.user .bubble { background: var(--user); border-radius: 16px 16px 4px 16px; padding: 9px 14px; max-width: 85%; white-space: pre-wrap; }
.msg.assistant .body > :first-child { margin-top: 0; }
.msg.assistant .body > :last-child { margin-bottom: 0; }
.msg .err { color: var(--danger); }
.msg .tools { display: flex; gap: 4px; margin-top: 4px; }
.msg .tools button { background: none; border: 0; color: var(--muted); font-size: 12px; padding: 2px 6px; border-radius: 6px; }
.msg .tools button:hover { background: var(--line); color: var(--text); }
.reasoning { border-left: 3px solid var(--line); padding: 2px 0 2px 12px; margin-bottom: 10px; color: var(--muted); font-size: 14px; }
.reasoning summary { cursor: pointer; }
.reasoning div { white-space: pre-wrap; margin-top: 6px; }
.cursor::after { content: "▍"; animation: blink 1s steps(2) infinite; color: var(--brand); }
@keyframes blink { 50% { opacity: 0; } }

.body pre { background: var(--code); border: 1px solid var(--line); border-radius: 10px; padding: 12px; overflow-x: auto; position: relative; }
.body pre button.copy { position: absolute; top: 6px; right: 6px; font-size: 12px; background: var(--surface); border: 1px solid var(--line); border-radius: 6px; padding: 2px 8px; }
.body code { font: 13px/1.5 ui-monospace, "Cascadia Code", Consolas, monospace; }
.body :not(pre) > code { background: var(--code); padding: 1px 5px; border-radius: 5px; }
.body a { color: var(--brand); }
.body table { border-collapse: collapse; display: block; overflow-x: auto; }
.body th, .body td { border: 1px solid var(--line); padding: 5px 10px; }
.body blockquote { margin: 0; padding-left: 12px; border-left: 3px solid var(--line); color: var(--muted); }

.composer {
  display: flex; gap: 8px; align-items: flex-end; margin: 0 auto; width: min(792px, 100%); padding: 8px 16px 0;
}
.composer textarea { resize: none; overflow-y: hidden; max-height: 40vh; border-radius: 14px; padding: 12px 14px; background: var(--surface); }
.send { width: 46px; height: 46px; flex: none; border-radius: 12px; padding: 0; font-size: 17px; }
.stop { background: var(--text); color: var(--bg); border: 0; }
.hint { text-align: center; font-size: 12px; margin: 6px 16px calc(10px + env(safe-area-inset-bottom)); }

.settings { border: 1px solid var(--line); border-radius: 16px; background: var(--surface); color: var(--text); width: min(520px, calc(100% - 32px)); padding: 22px; }
.settings::backdrop { background: rgb(0 0 0 / .35); }
.settings h2 { margin-top: 0; }
.settings form { display: flex; flex-direction: column; gap: 10px; }
.settings label { font-size: 14px; font-weight: 600; }
.settings .check { display: flex; gap: 8px; align-items: center; font-weight: 400; }
.settings .row { display: flex; justify-content: space-between; margin-top: 8px; }

.only-mobile { display: none; }
.scrim { position: fixed; inset: 0; background: rgb(0 0 0 / .35); z-index: 5; }
@media (max-width: 760px) {
  .only-mobile { display: inline-block; }
  .sidebar { position: fixed; inset: 0 auto 0 0; z-index: 10; transform: translateX(-100%); transition: transform .2s; box-shadow: 0 0 30px rgb(0 0 0 / .2); }
  .sidebar.open { transform: none; }
  .model { display: none; }
}
