/* ───────────── colours (light / dark) ───────────── */
:root {
  --bg: #f7f5f1;
  --panel: #efece6;
  --surface: #ffffff;
  --text: #1f1d1a;
  --muted: #6d675f;
  --line: #e2ddd4;
  --accent: #0f7b6c;
  --accent-ink: #ffffff;
  --accent-soft: #dff1ec;
  --user-bubble: #e9e5dd;
  --danger: #b4321f;
  --danger-soft: #fbe9e5;
  --ok: #17784a;
  --shadow: 0 1px 2px rgb(31 29 26 / 6%), 0 8px 24px rgb(31 29 26 / 6%);
  --radius: 14px;
  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "Cascadia Code", "SF Mono", Consolas, "Liberation Mono", monospace;
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #161513;
  --panel: #1c1b18;
  --surface: #23211e;
  --text: #ece9e3;
  --muted: #a39d93;
  --line: #34312c;
  --accent: #3cc2ab;
  --accent-ink: #0d1f1b;
  --accent-soft: #173a33;
  --user-bubble: #2d2a26;
  --danger: #ff8a75;
  --danger-soft: #3a1f1a;
  --ok: #5fd39a;
  --shadow: 0 1px 2px rgb(0 0 0 / 30%), 0 8px 24px rgb(0 0 0 / 25%);
  color-scheme: dark;
}

/* ───────────── base ───────────── */
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.6 var(--font);
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

.app { display: grid; grid-template-columns: 272px 1fr; height: 100dvh; }

/* ───────────── buttons ───────────── */
.icon-btn {
  display: inline-grid; place-items: center;
  width: 34px; height: 34px; border-radius: 9px;
  border: 0; background: transparent; color: var(--muted);
}
.icon-btn:hover { background: var(--panel); color: var(--text); }
.icon-btn.done { color: var(--ok); }
.ghost {
  display: inline-flex; align-items: center; gap: 8px;
  border: 1px solid var(--line); background: var(--surface);
  padding: 7px 12px; border-radius: 10px;
}
.ghost:hover { border-color: var(--muted); }
.ghost.small { padding: 4px 10px; font-size: 13px; }
.primary {
  border: 0; border-radius: 10px; padding: 8px 16px;
  background: var(--accent); color: var(--accent-ink); font-weight: 600;
}
.primary:hover { filter: brightness(1.06); }
.primary.danger { background: var(--danger); color: #fff; }

/* ───────────── avatar ───────────── */
.avatar {
  display: inline-grid; place-items: center; flex: none;
  width: 30px; height: 30px; border-radius: 50%;
  background: conic-gradient(from 200deg, var(--accent), #5aa9e6, #b9a5ff, var(--accent));
  color: #fff; font-weight: 700; font-size: 14px;
  box-shadow: inset 0 0 0 2px rgb(255 255 255 / 25%);
}
.avatar.big { width: 64px; height: 64px; font-size: 28px; }

/* ───────────── sidebar ───────────── */
.sidebar {
  display: flex; flex-direction: column; gap: 10px;
  padding: 14px 12px; min-height: 0;
  background: var(--panel); border-right: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 6px 6px; }
.brand-name { font-weight: 700; font-size: 18px; letter-spacing: -0.01em; }
.new-chat {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  padding: 9px 12px; border-radius: 11px;
  border: 1px solid var(--line); background: var(--surface);
  font-weight: 600; box-shadow: var(--shadow);
}
.new-chat:hover { border-color: var(--accent); }
.threads { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; margin: 0 -4px; padding: 0 4px; }
.sidebar-note { color: var(--muted); font-size: 13px; padding: 8px; margin: 0; }
.error-text { color: var(--danger); }
.thread { display: flex; align-items: center; border-radius: 10px; }
.thread:hover, .thread.active { background: var(--surface); }
.thread.active { box-shadow: inset 3px 0 0 var(--accent); }
.thread-open {
  flex: 1; min-width: 0; text-align: left;
  display: flex; flex-direction: column; gap: 1px;
  border: 0; background: transparent; padding: 8px 6px 8px 10px;
}
.thread-title { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 14px; }
.thread-time { font-size: 12px; color: var(--muted); }
.thread-action { width: 28px; height: 28px; opacity: 0; }
.thread:hover .thread-action, .thread:focus-within .thread-action, .thread.active .thread-action { opacity: 1; }
.thread-action.delete:hover { color: var(--danger); }
.sidebar-foot { display: flex; align-items: center; justify-content: space-between; padding-top: 8px; border-top: 1px solid var(--line); }
.sidebar-foot .ghost { border-color: transparent; background: transparent; }
.sidebar-foot .ghost:hover { background: var(--surface); }

/* ───────────── main ───────────── */
.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.topbar {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 18px; border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--bg) 88%, transparent);
}
.menu-btn { display: none; }
.chat-title { flex: 1; min-width: 0; margin: 0; font-size: 15px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.controls { display: flex; align-items: center; gap: 10px; }
select {
  border: 1px solid var(--line); background: var(--surface);
  border-radius: 9px; padding: 6px 10px; max-width: 150px;
}
.switch { display: inline-flex; align-items: center; gap: 7px; font-size: 13px; color: var(--muted); cursor: pointer; user-select: none; }
.switch input { position: absolute; opacity: 0; pointer-events: none; }
.track { width: 32px; height: 18px; border-radius: 99px; background: var(--line); position: relative; transition: background .15s; }
.track::after { content: ""; position: absolute; top: 2px; left: 2px; width: 14px; height: 14px; border-radius: 50%; background: var(--surface); box-shadow: 0 1px 2px rgb(0 0 0 / 25%); transition: transform .15s; }
.switch input:checked + .track { background: var(--accent); }
.switch input:checked + .track::after { transform: translateX(14px); }
.switch input:focus-visible + .track { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ───────────── messages ───────────── */
.messages { flex: 1; overflow-y: auto; padding: 28px 18px 12px; scroll-behavior: smooth; }
.messages > * { max-width: 760px; margin-left: auto; margin-right: auto; }
.loading { color: var(--muted); text-align: center; }
.msg { display: flex; gap: 12px; margin-bottom: 22px; animation: rise .18s ease-out; }
@keyframes rise { from { opacity: 0; transform: translateY(4px); } }
.msg.user { justify-content: flex-end; }
.msg.user .bubble {
  max-width: 85%; padding: 10px 14px; border-radius: 18px 18px 4px 18px;
  background: var(--user-bubble); white-space: pre-wrap; overflow-wrap: anywhere;
}
.msg-body { flex: 1; min-width: 0; }
.msg-name { font-weight: 600; font-size: 13px; margin-bottom: 2px; }
.content { overflow-wrap: anywhere; }
.content > :first-child { margin-top: 0; }
.content > :last-child { margin-bottom: 0; }
.content p, .content ul, .content ol { margin: 0 0 .8em; }
.content li + li { margin-top: .25em; }
.content h1, .content h2, .content h3 { line-height: 1.3; margin: 1.1em 0 .5em; }
.content h1 { font-size: 1.35em; } .content h2 { font-size: 1.2em; } .content h3 { font-size: 1.05em; }
.content a { color: var(--accent); }
.content blockquote { margin: 0 0 .8em; padding-left: 12px; border-left: 3px solid var(--line); color: var(--muted); }
.content table { border-collapse: collapse; margin: 0 0 .8em; display: block; overflow-x: auto; }
.content th, .content td { border: 1px solid var(--line); padding: 6px 10px; text-align: left; }
.content th { background: var(--panel); }
.content :not(pre) > code { font-family: var(--mono); font-size: .9em; background: var(--panel); padding: 1px 5px; border-radius: 5px; }
.code-block { margin: 0 0 .9em; border: 1px solid var(--line); border-radius: 12px; overflow: hidden; background: var(--surface); }
.code-head { display: flex; justify-content: space-between; align-items: center; padding: 5px 8px 5px 12px; font-size: 12px; color: var(--muted); background: var(--panel); border-bottom: 1px solid var(--line); }
.copy-code { border: 0; background: transparent; color: var(--muted); font-size: 12px; padding: 2px 8px; border-radius: 6px; }
.copy-code:hover { background: var(--surface); color: var(--text); }
.code-block pre { margin: 0; padding: 12px 14px; overflow-x: auto; }
.code-block code { font-family: var(--mono); font-size: 13px; line-height: 1.55; background: transparent !important; padding: 0 !important; }
.msg-actions { display: flex; align-items: center; gap: 2px; margin-top: 6px; opacity: .75; }
.msg:hover .msg-actions { opacity: 1; }
.msg-actions .icon-btn { width: 30px; height: 30px; }
.msg-actions svg { width: 16px; height: 16px; }
.badge { margin-left: 6px; font-size: 11px; padding: 2px 8px; border-radius: 99px; background: var(--accent-soft); color: var(--accent); font-weight: 600; letter-spacing: .02em; }
.note { margin: 6px 0 0; font-size: 13px; color: var(--muted); font-style: italic; }
.streaming .msg-actions { display: none; }
.reasoning { margin: 2px 0 10px; border-left: 2px solid var(--line); padding-left: 12px; }
.reasoning summary { cursor: pointer; color: var(--muted); font-size: 13px; }
.reasoning-text { white-space: pre-wrap; color: var(--muted); font-size: 13.5px; margin-top: 6px; max-height: 320px; overflow-y: auto; }
.typing { display: inline-flex; gap: 4px; padding: 8px 0; }
.typing i { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite ease-in-out; }
.typing i:nth-child(2) { animation-delay: .15s; } .typing i:nth-child(3) { animation-delay: .3s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; transform: translateY(0); } 40% { opacity: 1; transform: translateY(-2px); } }

.error-card { border: 1px solid var(--danger); background: var(--danger-soft); color: var(--text); border-radius: 12px; padding: 10px 14px; margin-bottom: 18px; }
.error-card strong { color: var(--danger); }
.error-card p { margin: 4px 0 8px; }

/* welcome */
.welcome { display: flex; flex-direction: column; align-items: center; text-align: center; padding: 8vh 8px 0; gap: 6px; }
.welcome h2 { margin: 14px 0 0; font-size: 28px; letter-spacing: -0.02em; }
.welcome p { margin: 0 0 18px; }
.suggestions { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; width: 100%; max-width: 640px; margin-bottom: 18px; }
.suggestion {
  text-align: left; padding: 12px 14px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--surface); font-size: 14px; line-height: 1.4;
  transition: border-color .15s, transform .15s;
}
.suggestion:hover { border-color: var(--accent); transform: translateY(-1px); }

/* ───────────── composer ───────────── */
.composer-wrap { padding: 8px 18px 14px; }
.composer {
  max-width: 760px; margin: 0 auto;
  display: flex; align-items: flex-end; gap: 8px;
  padding: 8px 8px 8px 14px; border-radius: 18px;
  border: 1px solid var(--line); background: var(--surface); box-shadow: var(--shadow);
}
.composer:focus-within { border-color: var(--accent); }
.composer textarea { flex: 1; border: 0; outline: 0; resize: none; background: transparent; padding: 7px 0; max-height: 220px; line-height: 1.5; }
.send, .stop { width: 38px; height: 38px; border-radius: 12px; border: 0; display: grid; place-items: center; flex: none; }
.send { background: var(--accent); color: var(--accent-ink); }
.send:hover { filter: brightness(1.08); }
.stop { background: var(--text); color: var(--bg); }
.stop svg { fill: currentColor; stroke: none; }
.footnote { max-width: 760px; margin: 6px auto 0; text-align: center; font-size: 12px; color: var(--muted); }

/* ───────────── dialogs ───────────── */
.dialog {
  border: 1px solid var(--line); border-radius: 16px; padding: 0;
  background: var(--surface); color: var(--text); box-shadow: var(--shadow);
  width: min(480px, calc(100vw - 32px));
}
.dialog::backdrop { background: rgb(0 0 0 / 35%); backdrop-filter: blur(2px); }
.dialog form { display: flex; flex-direction: column; gap: 12px; padding: 20px; }
.dialog h2 { margin: 0; font-size: 18px; }
.dialog label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; font-weight: 600; }
.dialog input { border: 1px solid var(--line); background: var(--bg); border-radius: 10px; padding: 9px 11px; font-weight: 400; font-size: 14px; }
.dialog input:focus { outline: none; border-color: var(--accent); }
.dialog p { margin: 0; }
.key-row { display: flex; gap: 8px; }
.key-row input { flex: 1; min-width: 0; }
.status { font-size: 13px; min-height: 1.2em; overflow-wrap: anywhere; }
.status.ok { color: var(--ok); }
.status.bad { color: var(--danger); }
.dialog-actions { display: flex; gap: 8px; align-items: center; margin-top: 4px; }
.spacer { flex: 1; }

/* ───────────── phones and small windows ───────────── */
.scrim { display: none; }
@media (max-width: 820px) {
  .app { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; inset: 0 auto 0 0; z-index: 20; width: min(300px, 86vw);
    transform: translateX(-102%); transition: transform .2s ease;
    box-shadow: var(--shadow);
  }
  .sidebar-open .sidebar { transform: none; }
  .scrim { display: block; position: fixed; inset: 0; z-index: 10; background: rgb(0 0 0 / 35%); }
  .menu-btn { display: inline-grid; }
  .topbar { padding: 8px 10px; }
  .controls { gap: 6px; }
  .switch > span:last-child { display: none; }
  select { max-width: 104px; }
  .messages { padding: 18px 12px 8px; }
  .composer-wrap { padding: 6px 10px calc(10px + env(safe-area-inset-bottom)); }
  .suggestions { grid-template-columns: 1fr; }
  .thread-action { opacity: 1; }
  .welcome { padding-top: 4vh; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; scroll-behavior: auto !important; }
}
