:root {
  --bg: #f4f2ee;
  --surface: #ffffff;
  --surface-2: #ebe8e2;
  --line: #d9d4cb;
  --text: #1c1f23;
  --muted: #5f646b;
  --accent: #b9770e;
  --accent-ink: #ffffff;
  --user: #e6ecf2;
  --danger: #b3261e;
  --ok: #2e7d32;
  --code-bg: #f0ede7;
  --radius: 12px;
  --fs: 18px;
  --side-w: 300px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121418;
    --surface: #1b1e23;
    --surface-2: #23272d;
    --line: #333840;
    --text: #e8e6e1;
    --muted: #9ba1a8;
    --accent: #f2b33d;
    --accent-ink: #1a1405;
    --user: #22303d;
    --danger: #ff8a80;
    --ok: #81c784;
    --code-bg: #15181c;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-size: var(--fs);
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
}
[hidden] { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }
svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.muted { color: var(--muted); }
.error { color: var(--danger); min-height: 1.5em; }

.btn {
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: var(--radius);
  padding: 12px 16px;
  min-height: 48px;
  cursor: pointer;
}
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; font-weight: 600; }
.btn.ghost { background: transparent; }
.btn.wide { width: 100%; }
.btn:focus-visible, .icon-btn:focus-visible, .send:focus-visible, .quick button:focus-visible,
input:focus-visible, textarea:focus-visible, select:focus-visible {
  outline: 3px solid var(--accent); outline-offset: 2px;
}
.icon-btn {
  display: inline-grid; place-items: center;
  width: 48px; height: 48px; flex: none;
  border: 0; background: transparent; border-radius: var(--radius);
  color: var(--muted); cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }

/* вход */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: min(380px, 100%); display: grid; gap: 12px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 16px; padding: 28px 24px;
}
.login-card h1 { margin: 0; font-size: 1.6em; }
.login-card p { margin: 0; }
.login-card input { padding: 12px 14px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--bg); min-height: 48px; }

/* каркас */
.app { display: flex; height: 100dvh; }
.sidebar {
  width: var(--side-w); flex: none; display: flex; flex-direction: column;
  background: var(--surface); border-right: 1px solid var(--line);
}
.side-top { padding: 12px; display: grid; gap: 10px; }
.side-top input {
  padding: 10px 12px; border: 1px solid var(--line); border-radius: var(--radius);
  background: var(--bg); min-height: 44px;
}
.chat-list { list-style: none; margin: 0; padding: 4px 8px; overflow-y: auto; flex: 1; }
.chat-list button {
  width: 100%; text-align: left; border: 0; background: transparent; cursor: pointer;
  padding: 12px; border-radius: 10px; display: grid; gap: 2px; min-height: 48px;
}
.chat-list button:hover { background: var(--surface-2); }
.chat-list button[aria-current="true"] { background: var(--surface-2); box-shadow: inset 3px 0 0 var(--accent); }
.chat-list .t { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: .95em; }
.chat-list .d { font-size: .78em; color: var(--muted); }
.chat-list .busy::after { content: " · отвечает"; color: var(--accent); }
.side-bottom { padding: 12px; border-top: 1px solid var(--line); display: grid; grid-template-columns: 1fr 1fr; gap: 8px; align-items: end; }
.side-bottom #logout { grid-column: 1 / -1; }
.model { flex: 1; display: grid; gap: 4px; font-size: .8em; color: var(--muted); }
.model select { padding: 10px; border-radius: var(--radius); border: 1px solid var(--line); background: var(--bg); min-height: 44px; font-size: 1.1em; color: var(--text); }
.scrim { position: fixed; inset: 0; background: rgb(0 0 0 / .45); z-index: 9; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.top {
  display: flex; align-items: center; gap: 4px;
  padding: 4px 8px; padding-top: max(4px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--line); background: var(--surface);
}
.chat-title { flex: 1; min-width: 0; margin: 0 4px; font-size: 1em; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#menu { display: none; }

/* сообщения */
.messages { flex: 1; overflow-y: auto; padding: 16px; overscroll-behavior: contain; }
.messages > * { max-width: 860px; margin-inline: auto; }
.empty { padding: 12vh 4px 0; }
.empty-lead { font-size: 1.25em; font-weight: 600; margin: 0 0 8px; }
.who { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin: 16px 0; padding: 14px; border: 1px solid var(--accent); border-radius: var(--radius); }
.who p { margin: 0; width: 100%; font-weight: 600; }
.who .btn { flex: 1; }
.msg .who-tag { font-size: .75em; color: var(--muted); }
.msg { margin-bottom: 18px; }
.msg.user .bubble {
  background: var(--user); border-radius: 16px 16px 4px 16px;
  padding: 10px 14px; margin-left: auto; width: fit-content; max-width: 92%;
  white-space: pre-wrap; overflow-wrap: anywhere;
}
.msg.user { display: flex; flex-direction: column; align-items: flex-end; gap: 6px; }
.msg .files { display: flex; flex-wrap: wrap; gap: 6px; justify-content: flex-end; }
.msg .files img { width: 96px; height: 96px; object-fit: cover; border-radius: 10px; border: 1px solid var(--line); }
.msg .files a.file { font-size: .8em; padding: 6px 10px; border: 1px solid var(--line); border-radius: 8px; color: var(--text); text-decoration: none; background: var(--surface); }
.msg.bot .md { overflow-wrap: anywhere; }
.msg.bot .body:empty { display: none; }
.msg .err {
  margin-top: 8px; padding: 10px 14px; border-radius: 10px;
  border: 1px solid var(--danger); color: var(--danger);
}
.msg .err pre { color: var(--text); }
.status { font-size: .85em; color: var(--muted); display: flex; flex-direction: column; gap: 2px; margin-bottom: 6px; }
.status .now { color: var(--accent); }
.status .now::before {
  content: ""; display: inline-block; width: 8px; height: 8px; border-radius: 50%;
  background: var(--accent); margin-right: 8px; vertical-align: middle;
  animation: pulse 1.2s ease-in-out infinite;
}
@keyframes pulse { 50% { opacity: .25; } }
@media (prefers-reduced-motion: reduce) { .status .now::before { animation: none; } }
.commit { font-size: .8em; color: var(--ok); margin-top: 4px; }

/* markdown */
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md h1, .md h2, .md h3, .md h4 { line-height: 1.25; margin: 1.1em 0 .4em; }
.md h1 { font-size: 1.3em; } .md h2 { font-size: 1.18em; } .md h3, .md h4 { font-size: 1.05em; }
.md p, .md ul, .md ol { margin: .5em 0; }
.md li { margin: .15em 0; }
.md a { color: var(--accent); }
.md code { font-family: ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace; font-size: .88em; background: var(--code-bg); padding: .1em .35em; border-radius: 5px; }
.md blockquote { margin: .6em 0; padding: .2em 0 .2em 14px; border-left: 3px solid var(--accent); color: var(--muted); }
.md hr { border: 0; border-top: 1px solid var(--line); margin: 1em 0; }
.md .table-wrap { overflow-x: auto; margin: .6em 0; border: 1px solid var(--line); border-radius: 10px; }
.md table { border-collapse: collapse; width: 100%; font-size: .9em; }
.md th, .md td { padding: 8px 10px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
.md tr:last-child td { border-bottom: 0; }
.md th { background: var(--surface-2); }
.code { position: relative; margin: .6em 0; }
.code pre { margin: 0; background: var(--code-bg); border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; padding-right: 92px; overflow-x: auto; }
.code pre code { background: none; padding: 0; font-size: .85em; }
.copy {
  position: absolute; top: 6px; right: 6px;
  border: 1px solid var(--line); background: var(--surface); border-radius: 8px;
  padding: 6px 10px; min-height: 36px; font-size: .78em; cursor: pointer;
}
.copy.done { color: var(--ok); border-color: var(--ok); }

/* поле ввода */
.composer {
  border-top: 1px solid var(--line); background: var(--surface);
  padding: 8px 8px max(8px, env(safe-area-inset-bottom));
}
.composer > * { max-width: 860px; margin-inline: auto; }
.quick { display: flex; gap: 8px; overflow-x: auto; padding: 2px 2px 8px; scrollbar-width: none; }
.quick button {
  flex: none; border: 1px solid var(--line); background: var(--bg); border-radius: 999px;
  padding: 8px 14px; min-height: 40px; font-size: .85em; cursor: pointer; white-space: nowrap;
}
.quick button:hover { border-color: var(--accent); }
.attachments { display: flex; flex-wrap: wrap; gap: 8px; }
.attachments:not(:empty) { padding: 0 2px 8px; }
.chip {
  display: flex; align-items: center; gap: 6px; max-width: 100%;
  border: 1px solid var(--line); border-radius: 10px; padding: 4px 4px 4px 6px; font-size: .8em; background: var(--bg);
}
.chip img { width: 40px; height: 40px; object-fit: cover; border-radius: 6px; }
.chip .n { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 180px; }
.chip.loading { opacity: .6; }
.chip.failed { border-color: var(--danger); color: var(--danger); }
.chip button { border: 0; background: transparent; width: 32px; height: 32px; cursor: pointer; color: var(--muted); font-size: 1.1em; }
.send-row { display: flex; align-items: flex-end; gap: 4px; }
.send-row textarea {
  flex: 1; min-width: 0; resize: none; max-height: 40vh;
  border: 1px solid var(--line); border-radius: 22px; background: var(--bg);
  padding: 11px 16px; min-height: 48px; line-height: 1.4;
}
.send {
  width: 48px; height: 48px; flex: none; border-radius: 50%; border: 0; cursor: pointer;
  background: var(--accent); color: var(--accent-ink); display: grid; place-items: center;
}
.send:disabled { opacity: .45; cursor: default; }
.send.stop { background: var(--danger); color: #fff; }
.camera { display: none; }

/* телефон: список чатов выезжает слева, камера под рукой */
@media (max-width: 899px) {
  :root { --fs: 17px; }
  #menu { display: inline-grid; }
  .sidebar {
    position: fixed; inset: 0 auto 0 0; z-index: 10; width: min(86vw, 340px);
    transform: translateX(-100%); transition: transform .22s ease-out;
    padding-top: env(safe-area-inset-top);
  }
  .sidebar.open { transform: none; box-shadow: 0 0 40px rgb(0 0 0 / .35); }
  .camera { display: inline-grid; }
  .messages { padding: 12px; }
}
@media (prefers-reduced-motion: reduce) { .sidebar { transition: none; } }
