:root {
  --bg: #16181d; --panel: #1d2027; --panel2: #252932; --line: #2f3440;
  --ink: #e8eaee; --muted: #8b92a0; --accent: #d97757; --accent2: #f0a07f;
  --user: #2b3240; --ok: #5fb37f; --err: #e26d6d; --code: #12141a;
  --safe-b: env(safe-area-inset-bottom, 0px); --safe-t: env(safe-area-inset-top, 0px);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--ink);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, sans-serif; }
button { font: inherit; color: inherit; cursor: pointer; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.err { color: var(--err); min-height: 1.4em; margin-top: 8px; }

/* Вход */
.login { min-height: 100%; display: grid; place-items: center; padding: 24px; }
.login-card { width: 100%; max-width: 340px; background: var(--panel); border: 1px solid var(--line);
  border-radius: 16px; padding: 28px 22px; display: flex; flex-direction: column; gap: 12px; }
.login-card h1 { margin: 0 0 6px; font-size: 20px; text-align: center; }
.logo { font-size: 34px; color: var(--accent); text-align: center; }
.login-card label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
input, select, textarea { font: inherit; color: var(--ink); background: var(--panel2); border: 1px solid var(--line);
  border-radius: 10px; padding: 10px 12px; outline: none; }
input:focus, textarea:focus, select:focus { border-color: var(--accent); }
.login-card button, .btn-primary { background: var(--accent); border: 0; border-radius: 10px; padding: 11px; font-weight: 600; color: #fff; }

/* Каркас */
.app { display: flex; height: 100dvh; }
.side { width: 280px; flex: none; background: var(--panel); border-right: 1px solid var(--line);
  display: flex; flex-direction: column; padding-top: var(--safe-t); }
.side-head { padding: 12px; }
.side-head .btn-primary { width: 100%; }
.conv-list { flex: 1; overflow-y: auto; padding: 0 8px; }
.conv { padding: 9px 10px; border-radius: 8px; cursor: pointer; display: flex; gap: 6px; align-items: center; }
.conv:hover { background: var(--panel2); }
.conv.active { background: var(--panel2); box-shadow: inset 2px 0 var(--accent); }
.conv .t { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv .src { font-size: 11px; color: var(--muted); }
.side-foot { display: flex; justify-content: space-between; padding: 10px 14px calc(10px + var(--safe-b)); border-top: 1px solid var(--line); }
.link { background: none; border: 0; color: var(--muted); padding: 4px; }
.link:hover { color: var(--ink); }
.scrim { display: none; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.top { display: flex; align-items: center; gap: 8px; padding: calc(8px + var(--safe-t)) 10px 8px; border-bottom: 1px solid var(--line); background: var(--bg); }
.top-title { flex: 1; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; display: flex; align-items: center; gap: 8px; }
.icon { background: none; border: 0; font-size: 20px; padding: 4px 8px; color: var(--muted); border-radius: 8px; }
.icon:hover { color: var(--ink); background: var(--panel2); }
#menuBtn { display: none; }
.busy { width: 9px; height: 9px; border-radius: 50%; background: var(--accent); animation: pulse 1.2s infinite; flex: none; }
@keyframes pulse { 50% { opacity: .3; } }

.conv-settings { padding: 10px 14px; border-bottom: 1px solid var(--line); background: var(--panel); display: flex; flex-wrap: wrap; gap: 10px 16px; align-items: center; }
.conv-settings label { display: flex; gap: 8px; align-items: center; font-size: 13px; color: var(--muted); }
.conv-settings select { padding: 6px 8px; max-width: 60vw; }
.row { display: flex; gap: 12px; }

/* Сообщения */
.messages { flex: 1; overflow-y: auto; padding: 16px 14px 8px; display: flex; flex-direction: column; gap: 14px; }
.empty { margin: auto; color: var(--muted); }
.msg { max-width: 860px; width: 100%; margin: 0 auto; }
.msg.user .bubble { background: var(--user); border-radius: 14px 14px 4px 14px; padding: 10px 14px; margin-left: auto; max-width: 85%; width: fit-content; white-space: pre-wrap; word-wrap: break-word; }
.msg.assistant .bubble { padding: 2px 2px; word-wrap: break-word; }
.bubble p { margin: 0 0 .6em; }
.bubble p:last-child { margin-bottom: 0; }
.bubble h1, .bubble h2, .bubble h3 { font-size: 1.05em; margin: .8em 0 .4em; }
.bubble ul, .bubble ol { margin: .2em 0 .6em; padding-left: 1.3em; }
.bubble code { font-family: ui-monospace, "Cascadia Code", Consolas, monospace; font-size: .88em; background: var(--code); padding: 1px 5px; border-radius: 5px; }
.bubble pre { background: var(--code); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; overflow-x: auto; margin: .4em 0 .8em; }
.bubble pre code { background: none; padding: 0; font-size: 13px; line-height: 1.45; }
.bubble a { color: var(--accent2); }
.tools { margin: 2px 0 8px; display: flex; flex-direction: column; gap: 3px; }
.tool { font-size: 12.5px; color: var(--muted); font-family: ui-monospace, Consolas, monospace; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tool b { color: var(--accent2); font-weight: 600; }
.tool.err b { color: var(--err); }
.tools details summary { cursor: pointer; font-size: 12.5px; color: var(--muted); }
.status { font-size: 12.5px; color: var(--muted); font-style: italic; }
.error { color: var(--err); font-size: 13.5px; white-space: pre-wrap; }
.meta { font-size: 11.5px; color: var(--muted); margin-top: 4px; }
.denials { margin-top: 8px; background: #3a2a22; border: 1px solid #6b4a3a; border-radius: 10px; padding: 10px 12px; font-size: 13.5px; }
.denials code { background: #2a1d17; }
.denials button { margin-top: 8px; background: var(--accent); border: 0; color: #fff; border-radius: 8px; padding: 7px 12px; font-weight: 600; }
.cursor::after { content: "▍"; color: var(--accent); animation: pulse 1s infinite; }

/* Ввод */
.composer { border-top: 1px solid var(--line); padding: 8px 10px calc(8px + var(--safe-b)); background: var(--bg); }
.composer-row { display: flex; gap: 6px; align-items: flex-end; max-width: 860px; margin: 0 auto; }
.composer textarea { flex: 1; resize: none; max-height: 40vh; min-height: 42px; line-height: 1.4; }
.send, .stop { width: 42px; height: 42px; border-radius: 10px; border: 0; color: #fff; font-size: 17px; flex: none; }
.send { background: var(--accent); }
.send:disabled { opacity: .4; }
.stop { background: var(--err); }
.attach { height: 42px; display: grid; place-items: center; cursor: pointer; }
.attach-list { max-width: 860px; margin: 0 auto 6px; display: flex; flex-wrap: wrap; gap: 6px; }
.attach-list:empty { display: none; }
.chip { background: var(--panel2); border: 1px solid var(--line); border-radius: 8px; padding: 3px 8px; font-size: 12.5px; }

/* Диалог файлов */
.dlg { background: var(--panel); color: var(--ink); border: 1px solid var(--line); border-radius: 14px; padding: 0; width: min(560px, 94vw); max-height: 80vh; }
.dlg::backdrop { background: rgba(0,0,0,.55); }
.dlg-head { display: flex; justify-content: space-between; align-items: center; padding: 12px 14px; border-bottom: 1px solid var(--line); }
.files-list { overflow-y: auto; max-height: 65vh; padding: 6px 8px 12px; }
.file { display: flex; justify-content: space-between; gap: 10px; padding: 9px 8px; border-radius: 8px; color: var(--ink); text-decoration: none; }
.file:hover { background: var(--panel2); }
.file span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Телефон */
@media (max-width: 760px) {
  #menuBtn { display: inline-block; }
  .side { position: fixed; inset: 0 auto 0 0; z-index: 20; transform: translateX(-100%); transition: transform .2s; width: 82vw; max-width: 320px; }
  .side.open { transform: none; }
  .scrim.open { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 10; }
  .messages { padding: 12px 10px 6px; }
  textarea, input, select { font-size: 16px; } /* без авто-зума в iOS */
}
