:root {
  --bg: #faf9f5; --panel: #f0eee6; --card: #ffffff; --text: #1f1e1d; --muted: #6b6a66;
  --line: #e3e0d6; --accent: #c96442; --accent-text: #ffffff; --code: #f3f1ea;
  --tool: #eef2f7; --tool-line: #c9d4e3; --err: #b3261e; --user: #efe9dc;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1f1e1d; --panel: #262624; --card: #2b2a27; --text: #ecebe6; --muted: #a3a29c;
    --line: #3a3935; --accent: #d97757; --accent-text: #1f1e1d; --code: #33322e;
    --tool: #23292f; --tool-line: #3b4652; --err: #f2b8b5; --user: #34322d;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 15px/1.55 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.btn {
  border: 1px solid var(--line); background: var(--card); color: var(--text);
  border-radius: 10px; padding: 8px 14px; font: inherit; cursor: pointer;
}
.btn:hover { filter: brightness(0.97); }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.btn.danger { background: var(--err); color: #fff; border-color: var(--err); }
.btn.ghost { background: transparent; }
.btn.wide { width: 100%; }
.btn.icon { padding: 6px 10px; }
.logo { width: 40px; height: 40px; border-radius: 50%; background: var(--accent); margin: 0 auto 8px; }
.logo.big { width: 56px; height: 56px; }

/* вход */
.login { min-height: 100%; display: flex; align-items: center; justify-content: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 380px; background: var(--card); border: 1px solid var(--line);
  border-radius: 18px; padding: 28px 24px; text-align: center;
}
.login-card h1 { margin: 4px 0 8px; font-size: 24px; }
.qr { display: block; width: min(300px, 80vw); aspect-ratio: 1; background: #fff; border-radius: 16px; padding: 10px; }
.qr svg { display: block; width: 100%; height: 100%; }
.login-card .btn { display: inline-block; text-decoration: none; margin-top: 4px; }

/* приложение */
.app { display: flex; height: 100%; }
.side {
  width: 270px; flex: none; background: var(--panel); border-right: 1px solid var(--line);
  display: flex; flex-direction: column;
}
.side-head, .side-foot { padding: 12px; }
.side-foot { border-top: 1px solid var(--line); display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.chats { flex: 1; overflow-y: auto; padding: 0 8px; }
.chat-item {
  display: flex; align-items: center; gap: 6px; padding: 8px 10px; border-radius: 8px;
  cursor: pointer; color: var(--text); text-decoration: none; font-size: 14px;
}
.chat-item:hover { background: var(--line); }
.chat-item.active { background: var(--card); border: 1px solid var(--line); }
.chat-item .t { flex: 1; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.chat-item .del { opacity: 0; border: 0; background: none; color: var(--muted); cursor: pointer; font-size: 14px; }
.chat-item:hover .del { opacity: 1; }
.dot { width: 7px; height: 7px; border-radius: 50%; background: var(--accent); flex: none; }

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

.thread { flex: 1; overflow-y: auto; padding: 24px 16px; }
.empty { text-align: center; margin-top: 12vh; }
.msg { max-width: 780px; margin: 0 auto 18px; }
.msg.user .bubble {
  background: var(--user); border-radius: 14px; padding: 10px 14px; margin-left: auto;
  width: fit-content; max-width: 85%; white-space: pre-wrap; word-wrap: break-word;
}
.msg.assistant .bubble { padding: 2px 2px; word-wrap: break-word; }
.bubble p { margin: 0 0 10px; }
.bubble h1, .bubble h2, .bubble h3 { margin: 14px 0 8px; line-height: 1.3; }
.bubble h1 { font-size: 20px; } .bubble h2 { font-size: 18px; } .bubble h3 { font-size: 16px; }
.bubble ul, .bubble ol { margin: 0 0 10px; padding-left: 22px; }
.bubble code { background: var(--code); padding: 1px 5px; border-radius: 5px; font: 13px/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.bubble pre {
  background: var(--code); padding: 12px; border-radius: 10px; overflow-x: auto; margin: 0 0 10px;
  border: 1px solid var(--line);
}
.bubble pre code { background: none; padding: 0; white-space: pre; }
.bubble table { border-collapse: collapse; margin: 0 0 10px; display: block; overflow-x: auto; }
.bubble th, .bubble td { border: 1px solid var(--line); padding: 4px 8px; text-align: left; }
.bubble a { color: var(--accent); }
.tool {
  background: var(--tool); border: 1px solid var(--tool-line); border-radius: 10px;
  margin: 6px 0 10px; font-size: 13px; overflow: hidden;
}
.tool summary { padding: 6px 10px; cursor: pointer; list-style: none; display: flex; gap: 8px; }
.tool summary::-webkit-details-marker { display: none; }
.tool .name { font-weight: 600; flex: none; }
.tool .arg { font-family: ui-monospace, Menlo, Consolas, monospace; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; color: var(--muted); }
.tool pre { margin: 0; padding: 8px 10px; border-top: 1px solid var(--tool-line); max-height: 300px; overflow: auto; font: 12px/1.45 ui-monospace, Menlo, Consolas, monospace; white-space: pre-wrap; }
.tool.err .name { color: var(--err); }
.error { color: var(--err); font-size: 14px; margin: 6px 0; }
.typing::after { content: "▍"; animation: blink 1s steps(2) infinite; color: var(--accent); }
@keyframes blink { 50% { opacity: 0; } }
.cost { font-size: 12px; color: var(--muted); }

.composer {
  display: flex; gap: 8px; align-items: flex-end; max-width: 812px; width: 100%;
  margin: 0 auto; padding: 10px 16px calc(14px + env(safe-area-inset-bottom));
}
.composer textarea {
  flex: 1; resize: none; max-height: 220px; border: 1px solid var(--line); border-radius: 14px;
  padding: 11px 14px; font: inherit; background: var(--card); color: var(--text); outline: none;
}
.composer textarea:focus { border-color: var(--accent); }
.composer .btn { height: 44px; min-width: 44px; border-radius: 12px; }

@media (max-width: 760px) {
  .side { position: fixed; z-index: 10; top: 0; bottom: 0; left: 0; transform: translateX(-100%); transition: transform .2s; }
  .side.open { transform: none; box-shadow: 0 0 40px rgba(0,0,0,.3); }
  #menu { display: inline-block; }
  .thread { padding: 16px 12px; }
  .composer { padding-left: 12px; padding-right: 12px; }
}
