:root {
  --bg: #f2f2f3;
  --text: #1d1f20;
  --accent: #5980a6;
  --n100: #f5f5f8; --n200: #e7e7ea; --n300: #d4d4d7; --n400: #b7b7ba;
  --n500: #98989b; --n600: #7a7a7d; --n700: #5d5d60; --n800: #424244; --n900: #2b2b2d;
  --a100: #eef6ff; --a200: #d6ebff; --a300: #b5d9fd; --a400: #94bce3;
  --a500: #749dc4; --a600: #597ea3; --a700: #416180; --a800: #2c455d; --a900: #1d2d3d;
  --danger: #c0392b; --ok: #2e7d5b;
  --font: 'DM Sans', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  --mono: 'Fira Code', 'JetBrains Mono', Consolas, monospace;
}

* , *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; font-family: var(--font); background: var(--bg); color: var(--text); }
body { overflow: hidden; }
textarea { resize: none; font-family: inherit; }
a { color: var(--accent); }
button { font-family: inherit; }
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--n300); border-radius: 4px; }
button:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}
[hidden] { display: none !important; }

@keyframes msgIn { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: none; } }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes slideUp { from { opacity: 0; transform: translateY(24px); } to { opacity: 1; transform: none; } }
@keyframes dot { 0%, 60%, 100% { transform: translateY(0); opacity: .3; } 30% { transform: translateY(-5px); opacity: 1; } }
@keyframes spin { to { transform: rotate(360deg); } }

/* ------------------------------- вход ------------------------------- */
#authScreen {
  position: fixed; inset: 0; display: flex; align-items: center; justify-content: center;
  background: var(--bg); z-index: 20; animation: fadeIn .3s ease;
}
#authScreen .dots {
  position: absolute; inset: 0; opacity: .7;
  background-image: radial-gradient(var(--n200) 1px, transparent 1px); background-size: 28px 28px;
}
.auth-card {
  position: relative; width: 390px; max-width: calc(100vw - 32px); background: #fff; border-radius: 24px;
  padding: 40px 36px; box-shadow: 0 8px 48px rgba(0,0,0,.11), 0 1px 4px rgba(0,0,0,.06);
  animation: slideUp .35s ease;
}
/* Знак логотипа: размер задаёт контейнер, цвет наследуется от него. */
.logo-mark { width: 100%; height: 100%; display: block; }
.auth-logo {
  display: inline-flex; width: 76px; height: 76px; align-items: center; justify-content: center;
  margin-bottom: 12px; color: var(--accent);
}
.field { margin-bottom: 16px; }
.field label { display: block; font-size: 13px; font-weight: 500; margin-bottom: 7px; color: var(--n700); }
.input {
  width: 100%; height: 46px; padding: 0 14px; border: 1.5px solid var(--n200); border-radius: 12px;
  background: #fff; font-family: inherit; font-size: 14.5px; color: var(--text); outline: none;
  transition: border-color .15s, box-shadow .15s;
}
.input:focus { border-color: var(--a400); box-shadow: 0 0 0 3px rgba(89,128,166,.12); }
textarea.input { height: auto; padding: 10px 14px; line-height: 1.5; }
select.input { cursor: pointer; }
.btn {
  height: 46px; padding: 0 20px; border: none; border-radius: 12px; background: var(--accent); color: #fff;
  font-size: 15px; font-weight: 500; cursor: pointer; transition: background .15s, opacity .15s;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
}
.btn:hover { background: var(--a700); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-block { width: 100%; }
.btn-ghost { background: none; color: var(--n700); border: 1.5px solid var(--n200); }
.btn-ghost:hover { background: var(--n100); color: var(--text); }
.btn-danger { background: var(--danger); }
.btn-danger:hover { background: #a3281c; }
.btn-sm { height: 34px; padding: 0 12px; font-size: 13px; border-radius: 9px; }
.auth-switch { margin-top: 18px; text-align: center; font-size: 13.5px; color: var(--n600); }
.auth-switch button { background: none; border: none; color: var(--accent); cursor: pointer; font-size: inherit; font-weight: 500; }
.alert { padding: 10px 14px; border-radius: 10px; font-size: 13.5px; margin-bottom: 14px; line-height: 1.45; }
.alert-error { background: #fdecea; border: 1px solid #f5c6c2; color: #8c2019; }
.alert-info { background: var(--a100); border: 1px solid var(--a200); color: var(--a800); }

/* ------------------------------- каркас ------------------------------- */
#app { display: flex; height: 100vh; height: 100dvh; overflow: hidden; animation: fadeIn .3s ease; }

.sidebar-backdrop {
  position: fixed; inset: 0; background: rgba(29,45,61,.45); z-index: 14;
  animation: fadeIn .18s ease; backdrop-filter: blur(1px);
}

aside.sidebar {
  width: 268px; background: var(--a900); display: flex; flex-direction: column; flex-shrink: 0;
  transition: margin-left .2s ease;
}
.brand { padding: 20px 16px 14px; display: flex; align-items: center; gap: 10px; }
.brand-mark {
  width: 34px; height: 34px; display: flex; align-items: center; justify-content: center;
  flex-shrink: 0; color: #fff;
}
.brand-name { font-size: 18px; font-weight: 700; color: #fff; letter-spacing: -.01em; }
.side-pad { padding: 0 12px 10px; }
.side-btn {
  width: 100%; padding: 10px 14px; background: rgba(255,255,255,.08); border: 1px solid rgba(255,255,255,.13);
  border-radius: 12px; color: rgba(255,255,255,.85); font-size: 13.5px; font-weight: 500; cursor: pointer;
  display: flex; align-items: center; gap: 8px; transition: background .15s;
}
.side-btn:hover { background: rgba(255,255,255,.15); }
.side-label {
  padding: 2px 20px 7px; font-size: 10.5px; font-weight: 600; color: rgba(255,255,255,.3);
  letter-spacing: .1em; text-transform: uppercase;
}
.chat-list { flex: 1; overflow-y: auto; padding: 0 8px; }
.chat-list::-webkit-scrollbar-thumb { background: rgba(255,255,255,.15); }
.chat-item {
  padding: 9px 10px 9px 13px; border-radius: 12px; cursor: pointer; margin-bottom: 2px;
  color: rgba(255,255,255,.72); font-size: 13.5px; line-height: 1.3; display: flex; align-items: center; gap: 6px;
  transition: background .12s, color .12s;
}
.chat-item:hover { background: rgba(255,255,255,.08); color: #fff; }
.chat-item.active { background: rgba(255,255,255,.14); color: #fff; }
.chat-item .t { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-item .act {
  opacity: 0; background: none; border: none; color: rgba(255,255,255,.5); cursor: pointer; padding: 2px;
  display: flex; border-radius: 6px; flex-shrink: 0;
}
.chat-item:hover .act { opacity: 1; }
.chat-item .act:hover { color: #fff; background: rgba(255,255,255,.14); }
.empty-hint { padding: 14px 20px; color: rgba(255,255,255,.3); font-size: 12.5px; line-height: 1.5; }

.side-foot {
  padding: 13px 12px; border-top: 1px solid rgba(255,255,255,.07); display: flex; align-items: center; gap: 10px;
}
.avatar {
  width: 36px; height: 36px; border-radius: 50%; background: var(--a600); display: flex; align-items: center;
  justify-content: center; color: #fff; font-weight: 600; font-size: 15px; flex-shrink: 0;
}
.side-foot .who { flex: 1; min-width: 0; }
.side-foot .name {
  color: rgba(255,255,255,.9); font-size: 13.5px; font-weight: 500;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.side-foot .sub { color: rgba(255,255,255,.35); font-size: 11px; }
.icon-btn {
  width: 32px; height: 32px; background: none; border: none; cursor: pointer; color: rgba(255,255,255,.4);
  display: flex; align-items: center; justify-content: center; border-radius: 8px;
  transition: color .12s, background .12s; flex-shrink: 0;
}
.icon-btn:hover { color: #fff; background: rgba(255,255,255,.1); }

main.chat { flex: 1; display: flex; flex-direction: column; overflow: hidden; background: var(--bg); min-width: 0; }
.topbar {
  padding: 13px 22px; background: #fff; border-bottom: 1px solid var(--n200); display: flex;
  align-items: center; gap: 12px; flex-shrink: 0;
}
.topbar .title { font-size: 16px; font-weight: 600; letter-spacing: -.01em; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pill {
  font-size: 12px; color: var(--a700); background: var(--a100); padding: 4px 12px; border-radius: 20px;
  font-weight: 500; white-space: nowrap;
}
.pill.quota { color: var(--n700); background: var(--n100); }
.burger { display: none; }

#messages { flex: 1; overflow-y: auto; padding: 22px 28px; display: flex; flex-direction: column; gap: 16px; }
.msg { display: flex; animation: msgIn .28s ease; }
.msg.user { justify-content: flex-end; }
.bubble {
  max-width: 76%; line-height: 1.6; font-size: 14.5px;
}
.msg.user .bubble {
  padding: 11px 17px; border-radius: 20px 20px 5px 20px; background: var(--a600); color: #fff;
  box-shadow: 0 2px 10px rgba(89,128,166,.3); white-space: pre-wrap; word-break: break-word;
}
.msg.assistant .bubble {
  padding: 14px 18px; border-radius: 5px 20px 20px 20px; background: #fff; border: 1px solid var(--n200);
  box-shadow: 0 1px 6px rgba(0,0,0,.07); word-break: break-word;
}
.msg.assistant .bubble.failed { background: #fdecea; border-color: #f5c6c2; color: #8c2019; }
.reasoning {
  font-size: 12.5px; color: var(--a700); background: var(--a100); border: 1px solid var(--a200);
  border-radius: 10px; padding: 9px 12px; margin-bottom: 10px; line-height: 1.5; white-space: pre-wrap;
}
.reasoning b { display: block; margin-bottom: 4px; font-size: 11.5px; letter-spacing: .04em; text-transform: uppercase; }
.msg-files { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.file-chip {
  padding: 5px 10px; border-radius: 8px; font-size: 12px; display: inline-flex; align-items: center; gap: 6px;
  background: var(--n100); color: var(--n700); border: 1px solid var(--n200); text-decoration: none;
}
.msg.user .file-chip { background: rgba(255,255,255,.18); color: #fff; border-color: rgba(255,255,255,.25); }
.msg-img { max-width: 100%; border-radius: 12px; margin-top: 4px; display: block; }
.msg-meta { font-size: 11px; color: var(--n500); margin-top: 7px; display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.msg-meta button { background: none; border: none; color: var(--n500); cursor: pointer; font-size: 11px; padding: 0; }
.msg-meta button:hover { color: var(--accent); }

.typing { display: flex; gap: 5px; align-items: center; padding: 4px 0; }
.typing span { width: 7px; height: 7px; border-radius: 50%; background: var(--n400); display: inline-block; }
.typing span:nth-child(1) { animation: dot 1.2s 0s ease infinite; }
.typing span:nth-child(2) { animation: dot 1.2s .2s ease infinite; }
.typing span:nth-child(3) { animation: dot 1.2s .4s ease infinite; }

.placeholder {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px;
  color: var(--n500); text-align: center; padding: 20px;
}
.placeholder .mark {
  width: 56px; height: 56px; display: flex; align-items: center; justify-content: center;
  margin-bottom: 4px; color: var(--n300);
}
.placeholder .big { font-size: 15px; font-weight: 500; color: var(--n700); }
.suggestions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-top: 12px; max-width: 560px; }
.suggestions button {
  background: #fff; border: 1px solid var(--n200); border-radius: 12px; padding: 9px 14px; font-size: 13px;
  color: var(--n700); cursor: pointer; transition: border-color .12s, color .12s;
}
.suggestions button:hover { border-color: var(--a400); color: var(--a700); }

/* ------------------------------- ввод ------------------------------- */
.composer { padding: 12px 20px 16px; background: #fff; border-top: 1px solid var(--n200); flex-shrink: 0; }
.attach-row { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
.attach-pill {
  padding: 6px 12px; background: var(--a100); border: 1px solid var(--a200); border-radius: 10px;
  display: inline-flex; align-items: center; gap: 7px; font-size: 13px; color: var(--a800);
}
.attach-pill button { background: none; border: none; cursor: pointer; color: var(--a600); display: flex; padding: 0; }
.input-box {
  background: var(--n100); border: 1.5px solid var(--n200); border-radius: 18px; overflow: hidden;
  transition: border-color .15s, box-shadow .15s;
}
.input-box:focus-within { border-color: var(--a400); box-shadow: 0 0 0 3px rgba(89,128,166,.1); }
#msgInput {
  display: block; width: 100%; background: none; border: none; outline: none; font-size: 15px; color: var(--text);
  line-height: 1.55; padding: 14px 18px 10px; max-height: 180px; overflow-y: auto;
}
.toolbar { display: flex; align-items: center; padding: 6px 8px 8px; gap: 4px; flex-wrap: wrap; }
.tool {
  height: 34px; padding: 0 11px; border-radius: 9px; border: 1.5px solid transparent; background: none;
  cursor: pointer; color: var(--n500); display: flex; align-items: center; gap: 6px; font-size: 12.5px;
  font-weight: 500; transition: background .12s, color .12s, border-color .12s;
}
.tool:hover { background: var(--n200); color: var(--n800); }
.tool.on { border-color: var(--a400); background: rgba(89,128,166,.12); color: var(--a700); }
.tool:disabled { opacity: .45; cursor: not-allowed; }
.tool-select {
  height: 34px; border: 1.5px solid var(--n200); border-radius: 9px; background: #fff; color: var(--text);
  font-family: inherit; font-size: 12.5px; font-weight: 500; padding: 0 8px; cursor: pointer; outline: none; max-width: 190px;
}
.spacer { flex: 1; }
.send {
  height: 34px; padding: 0 16px; border-radius: 10px; background: var(--accent); border: none; cursor: pointer;
  color: #fff; display: flex; align-items: center; gap: 7px; font-size: 13px; font-weight: 600;
  box-shadow: 0 2px 8px rgba(89,128,166,.35); transition: background .12s;
}
.send:hover { background: var(--a700); }
.send:disabled { opacity: .5; cursor: not-allowed; box-shadow: none; }
.send.stop { background: var(--danger); box-shadow: none; }
.hint { font-size: 11.5px; color: var(--n500); text-align: center; margin-top: 7px; }

/* ------------------------------- markdown ------------------------------- */
.md > *:first-child { margin-top: 0; }
.md > *:last-child { margin-bottom: 0; }
.md p { margin: 0 0 8px; }
.md h1 { font-size: 20px; margin: 14px 0 8px; }
.md h2 { font-size: 17px; margin: 14px 0 6px; }
.md h3 { font-size: 15px; margin: 12px 0 5px; }
.md ul, .md ol { margin: 6px 0 9px; padding-left: 22px; line-height: 1.65; }
.md li { margin-bottom: 4px; }
.md blockquote { border-left: 3px solid var(--a300); padding: 2px 0 2px 12px; margin: 8px 0; color: var(--n700); }
.md hr { border: none; border-top: 1px solid var(--n200); margin: 12px 0; }
.md a { word-break: break-all; }
.md code.inline {
  background: rgba(89,128,166,.1); border: 1px solid rgba(89,128,166,.18); border-radius: 5px;
  padding: 1px 6px; font-size: .87em; font-family: var(--mono); color: var(--a700);
}
.md table { border-collapse: collapse; margin: 10px 0; font-size: 13.5px; display: block; overflow-x: auto; }
.md th, .md td { border: 1px solid var(--n200); padding: 6px 10px; text-align: left; }
.md th { background: var(--n100); font-weight: 600; }
.code-block { background: #1c2333; border-radius: 14px; margin: 12px 0; overflow: hidden; border: 1px solid rgba(255,255,255,.05); }
.code-head {
  display: flex; align-items: center; justify-content: space-between; padding: 8px 14px;
  border-bottom: 1px solid rgba(255,255,255,.07);
}
.code-head span { font-size: 11.5px; color: #6d85a8; font-family: var(--mono); letter-spacing: .04em; }
.code-head button {
  background: none; border: 1px solid rgba(255,255,255,.14); border-radius: 7px; color: #6d85a8;
  font-size: 11px; padding: 3px 10px; cursor: pointer;
}
.code-head button:hover { color: #cdd9e8; border-color: rgba(255,255,255,.3); }
.code-block pre { padding: 14px 16px; margin: 0; overflow-x: auto; font-size: 13px; line-height: 1.7; color: #cdd9e8; }
.code-block code { font-family: var(--mono); }

/* ------------------------------- тосты ------------------------------- */
#toasts { position: fixed; bottom: 20px; right: 20px; display: flex; flex-direction: column; gap: 8px; z-index: 50; }
.toast {
  background: var(--n900); color: #fff; padding: 11px 16px; border-radius: 12px; font-size: 13.5px;
  box-shadow: 0 6px 24px rgba(0,0,0,.2); animation: slideUp .25s ease; max-width: 380px; line-height: 1.45;
}
.toast.err { background: #8c2019; }

/* ------------------------------- модалка ------------------------------- */
.modal-back {
  position: fixed; inset: 0; background: rgba(29,45,61,.42); display: flex; align-items: center;
  justify-content: center; z-index: 40; animation: fadeIn .18s ease; padding: 20px;
}
.modal {
  background: #fff; border-radius: 20px; padding: 26px; width: 480px; max-width: 100%; max-height: 90vh;
  overflow-y: auto; box-shadow: 0 12px 48px rgba(0,0,0,.18); animation: slideUp .22s ease;
}
.modal h3 { font-size: 18px; margin-bottom: 4px; }
.modal .sub { font-size: 13px; color: var(--n600); margin-bottom: 18px; }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 20px; }

@media (max-width: 820px) {
  aside.sidebar {
    position: fixed; inset: 0 auto 0 0; z-index: 15; width: 82vw; max-width: 300px;
    box-shadow: 4px 0 24px rgba(0,0,0,.25);
    padding-bottom: env(safe-area-inset-bottom);
  }
  aside.sidebar.hidden { transform: translateX(-100%); box-shadow: none; }
  aside.sidebar { transition: transform .22s ease; margin-left: 0; }

  .burger { display: flex; }
  .topbar { padding: 10px 12px; gap: 8px; }
  .topbar .title { font-size: 15px; }
  #renameBtn { display: none; }
  .pill { font-size: 11px; padding: 3px 9px; max-width: 42vw; overflow: hidden; text-overflow: ellipsis; }
  .pill.quota { display: none; }

  #messages { padding: 14px 12px; gap: 12px; }
  .bubble { max-width: 92%; font-size: 15px; }
  .msg.user .bubble { padding: 10px 14px; border-radius: 18px 18px 4px 18px; }
  .msg.assistant .bubble { padding: 12px 14px; border-radius: 4px 18px 18px 18px; }
  .md table { font-size: 12.5px; }
  .code-block pre { font-size: 12px; padding: 12px; }

  .composer { padding: 8px 10px calc(10px + env(safe-area-inset-bottom)); }
  .input-box { border-radius: 16px; }
  /* 16px не даёт iOS зумить страницу при фокусе. */
  #msgInput { font-size: 16px; padding: 12px 14px 8px; max-height: 128px; }
  .toolbar { gap: 6px; padding: 6px; }
  .tool, .tool-select, .send { height: 40px; }
  .tool { padding: 0 10px; }
  .tool-select { flex: 1 1 auto; min-width: 0; max-width: none; font-size: 13px; }
  #modelSelect { flex: 2 1 120px; }
  #effortSelect { flex: 2 1 120px; }
  .toolbar .spacer { display: none; }
  #attachBtn { flex: 0 0 auto; }
  .send { flex: 1 1 100%; justify-content: center; order: 5; font-size: 14px; }
  .hint { font-size: 11px; margin-top: 5px; }

  .suggestions button { font-size: 12.5px; padding: 8px 12px; }
  .modal { padding: 20px; border-radius: 16px; }
  .modal-actions { flex-direction: column-reverse; }
  .modal-actions .btn { width: 100%; }
  #toasts { left: 12px; right: 12px; bottom: calc(12px + env(safe-area-inset-bottom)); }
  .toast { max-width: none; }

  .auth-card { padding: 32px 22px; border-radius: 20px; }
  .input { font-size: 16px; }
}

@media (max-width: 400px) {
  .brand-name { font-size: 16px; }
  #messages { padding: 12px 10px; }
  .bubble { max-width: 96%; }
  .tool span, #attachBtn { font-size: 0; }
  #attachBtn { padding: 0 12px; }
  #attachBtn svg { width: 18px; height: 18px; }
}

/* Телефон в альбомной ориентации: экономим вертикаль. */
@media (max-height: 460px) and (max-width: 900px) {
  .topbar { padding: 6px 12px; }
  #msgInput { max-height: 72px; }
  .hint { display: none; }
}
