:root {
  --bg: #0c0d10;
  --panel: #14161b;
  --panel2: #1a1d24;
  --line: #23262e;
  --fg: #e8eaf0;
  --muted: #8d93a3;
  --accent: #6b5cf6;
  --accent2: #8b7bff;
  --user: #262a35;
  --radius: 16px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}
button, input, textarea { font: inherit; color: inherit; }
button { background: none; border: 0; cursor: pointer; }
.muted { color: var(--muted); }
.err { color: #ff6b6b; font-size: 14px; margin: 10px 0 0; min-height: 18px; }

/* ---------- вход ---------- */
.login {
  position: fixed; inset: 0; display: flex; align-items: center; justify-content: center;
  padding: 24px; background: radial-gradient(120% 80% at 50% 0%, #1a1630 0%, var(--bg) 60%);
}
.login-card {
  width: 100%; max-width: 380px; background: var(--panel); border: 1px solid var(--line);
  border-radius: 22px; padding: 26px 22px;
}
.login-card .logo {
  width: 52px; height: 52px; border-radius: 15px; display: grid; place-items: center;
  background: linear-gradient(145deg, var(--accent), #4b3ee0); font-size: 24px; margin-bottom: 14px;
}
.login-card h1 { margin: 0 0 4px; font-size: 24px; letter-spacing: -.3px; }
.login-card label { display: block; margin: 14px 0 0; font-size: 13px; color: var(--muted); }
.login-card input {
  width: 100%; margin-top: 6px; padding: 13px 14px; border-radius: 12px;
  background: var(--panel2); border: 1px solid var(--line); outline: none; font-size: 16px;
}
.login-card input:focus { border-color: var(--accent); }
.primary {
  width: 100%; margin-top: 18px; padding: 14px; border-radius: 13px; font-weight: 600;
  background: linear-gradient(145deg, var(--accent), #5446e8); color: #fff;
}
.primary:disabled { opacity: .55; }

/* ---------- каркас ---------- */
#shell { display: flex; flex-direction: column; height: 100dvh; min-height: 100%; }

#top {
  display: flex; align-items: center; gap: 10px; padding: calc(var(--safe-t) + 10px) 12px 10px;
  background: rgba(12, 13, 16, .86); backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 20;
}
#top .ttl { flex: 1; min-width: 0; }
#top .ttl b { display: block; font-size: 16px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#top .ttl span { display: block; font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon {
  width: 38px; height: 38px; border-radius: 12px; display: grid; place-items: center;
  font-size: 20px; color: var(--fg); background: var(--panel2); flex: none;
}
.icon:active { background: #232734; }
#backBtn { display: none; }
body.inchat #backBtn { display: grid; }
body.inchat #newBtn { display: none; }

#scroll { flex: 1; overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; }

/* ---------- список чатов ---------- */
.list { padding: 8px 0 24px; }
.row {
  display: flex; gap: 12px; align-items: center; padding: 13px 16px; border-bottom: 1px solid var(--line);
}
.row:active { background: var(--panel); }
.row.svc { opacity: .75; margin-top: 14px; border-top: 1px solid #1c1d22; }
.row.svc .txt b { font-size: 14px; color: var(--muted); }
.row .av {
  width: 38px; height: 38px; border-radius: 12px; flex: none; display: grid; place-items: center;
  background: var(--panel2); color: var(--accent2); font-size: 16px;
}
.row .txt { min-width: 0; flex: 1; }
.row .txt b { display: block; font-weight: 500; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row .txt span { display: block; font-size: 12.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sect { padding: 18px 16px 8px; font-size: 12px; letter-spacing: .04em; text-transform: uppercase; color: var(--muted); }

/* ---------- сообщения ---------- */
.chat { padding: 14px 12px calc(16px + var(--safe-b)); display: flex; flex-direction: column; gap: 12px; }
.msg { display: flex; flex-direction: column; gap: 6px; max-width: 100%; }
.msg.user { align-items: flex-end; }
.bubble {
  padding: 10px 13px; border-radius: var(--radius); font-size: 15.5px; line-height: 1.55;
  max-width: 88%; word-wrap: break-word; overflow-wrap: anywhere;
}
.msg.user .bubble { background: var(--user); border-bottom-right-radius: 6px; white-space: pre-wrap; }
.msg.assistant .bubble, .msg.error .bubble { background: var(--panel); border: 1px solid var(--line); border-bottom-left-radius: 6px; }
.msg.user .bubble.md-block p { margin: 0; }
.msg.error .bubble { border-color: #4a2226; background: #1f1416; color: #ffb4b4; }

.md-block { max-width: 100%; overflow-x: auto; }
.md-block p { margin: 0 0 8px; }
.md-block p:last-child { margin-bottom: 0; }
.md-block h1, .md-block h2, .md-block h3 { font-size: 17px; margin: 12px 0 6px; line-height: 1.3; }
.md-block ul, .md-block ol { margin: 6px 0; padding-left: 20px; }
.md-block li { margin: 3px 0; }
.md-block code { background: #0f1116; border: 1px solid var(--line); border-radius: 5px; padding: 1px 5px; font-size: 13.5px; }
.md-block pre { background: #0b0d11; border: 1px solid var(--line); border-radius: 10px; padding: 11px; overflow-x: auto; font-size: 13px; }
.md-block pre code { background: none; border: 0; padding: 0; }
.md-block blockquote { margin: 8px 0; padding-left: 10px; border-left: 3px solid var(--line); color: var(--muted); }
.md-block a { color: var(--accent2); }
.md-block table { border-collapse: collapse; font-size: 13.5px; display: block; overflow-x: auto; }
.md-block th, .md-block td { border: 1px solid var(--line); padding: 6px 9px; }

/* размышления */
.think { border: 1px solid var(--line); background: #101217; border-radius: 12px; overflow: hidden; max-width: 92%; }
.think > button {
  width: 100%; text-align: left; padding: 9px 12px; font-size: 13px; color: var(--muted);
  display: flex; align-items: center; gap: 7px;
}
.think > button .dot { width: 6px; height: 6px; border-radius: 50%; background: var(--accent2); opacity: .8; }
.think .body { padding: 0 12px 11px; font-size: 13px; color: #a8aebd; white-space: pre-wrap; display: none; }
.think.open .body { display: block; }

/* инструменты */
.tool { border: 1px solid var(--line); background: var(--panel); border-radius: 12px; max-width: 92%; overflow: hidden; }
.tool > button { width: 100%; text-align: left; padding: 9px 12px; display: flex; gap: 8px; align-items: center; font-size: 13px; }
.tool .nm { font-weight: 600; color: #c9cfe0; }
.tool .st { margin-left: auto; color: var(--muted); font-size: 12px; }
.tool .det { display: none; padding: 0 12px 10px; font-size: 12px; color: var(--muted); white-space: pre-wrap; overflow-x: auto; max-height: 220px; }
.tool.open .det { display: block; }
.tool.run .st { color: var(--accent2); }

/* файлы в сообщении */
.files { display: flex; flex-direction: column; gap: 8px; max-width: 92%; }
.search { padding: 10px 14px 4px; position: sticky; top: 0; z-index: 6; background: var(--bg); }
.search input { width: 100%; box-sizing: border-box; padding: 10px 12px; border-radius: 11px; border: 1px solid var(--line); background: var(--panel); color: var(--fg); font: inherit; font-size: 16px; -webkit-appearance: none; outline: none; }
.search input:focus { border-color: var(--accent); }
.search input::placeholder { color: var(--muted); }
.fgroup { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; padding: 14px 2px 7px; }
.fcard { display: flex; gap: 10px; align-items: center; padding: 10px 12px; border: 1px solid var(--line); background: var(--panel); border-radius: 12px; }
.fcard .ic { width: 34px; height: 34px; border-radius: 9px; display: grid; place-items: center; background: var(--panel2); flex: none; }
.fcard .nm { font-size: 13.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.fcard .sz { font-size: 11.5px; color: var(--muted); }
.fcard .acts { margin-left: auto; display: flex; gap: 6px; }
.fcard .acts a, .fcard .acts button {
  padding: 7px 10px; border-radius: 9px; background: var(--panel2); font-size: 12.5px; color: var(--fg); text-decoration: none;
}

/* статус работы */
.busy { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 13.5px; padding: 2px 4px; }
.busy .sp { width: 12px; height: 12px; border: 2px solid var(--line); border-top-color: var(--accent2); border-radius: 50%; animation: sp 0.9s linear infinite; }
@keyframes sp { to { transform: rotate(360deg); } }

/* ---------- композер ---------- */
#composer {
  border-top: 1px solid var(--line); background: rgba(12, 13, 16, .94);
  backdrop-filter: blur(14px); padding: 8px 10px calc(8px + var(--safe-b));
}
#chips { display: flex; gap: 7px; overflow-x: auto; padding: 2px 2px 8px; scrollbar-width: none; }
#chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; padding: 7px 12px; border-radius: 999px; background: var(--panel2);
  border: 1px solid var(--line); font-size: 13px; color: #cfd4e2;
}
.chip:active { background: #262b38; }
#attachRow { display: flex; gap: 7px; flex-wrap: wrap; padding: 0 0 8px; }
#attachRow:empty { display: none; }
.att { display: flex; align-items: center; gap: 6px; background: var(--panel2); border: 1px solid var(--line); border-radius: 9px; padding: 5px 8px; font-size: 12.5px; }
.att button { color: var(--muted); font-size: 15px; line-height: 1; }
.inp-row { display: flex; align-items: flex-end; gap: 8px; }
#input {
  flex: 1; resize: none; max-height: 132px; padding: 11px 13px; border-radius: 16px;
  background: var(--panel2); border: 1px solid var(--line); outline: none; font-size: 16px; line-height: 1.4;
}
#input:focus { border-color: #343a49; }
.send {
  width: 42px; height: 42px; border-radius: 50%; flex: none; display: grid; place-items: center;
  background: linear-gradient(145deg, var(--accent), #5446e8); color: #fff; font-size: 19px; font-weight: 600;
}
.send.stop { background: #3a2026; color: #ff9b9b; }

.drop {
  position: fixed; inset: 12px; border: 2px dashed var(--accent); border-radius: 20px;
  background: rgba(107, 92, 246, .14); display: grid; place-items: center; font-size: 17px; z-index: 60;
}
[hidden] { display: none !important; }

/* ---------- файлы ---------- */
.empty { padding: 40px 20px; text-align: center; color: var(--muted); font-size: 14px; }

.prof { display: inline-flex; align-items: center; gap: 4px; height: 30px; padding: 0 10px; margin-right: 8px;
  border-radius: 15px; border: 1px solid #2a2c33; background: #14161b; color: var(--muted);
  font-size: 12.5px; font-weight: 600; max-width: 118px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.prof.on { color: #cfc7ff; border-color: #4741a8; background: #1b1830; }
.sheet { position: fixed; inset: 0; background: rgba(0,0,0,.55); z-index: 40; display: flex; align-items: flex-end; }
.sheet[hidden] { display: none; }
.sheet-body { width: 100%; background: #121318; border-radius: 16px 16px 0 0; padding: 10px 8px calc(16px + env(safe-area-inset-bottom)); max-height: 78vh; overflow: auto; }
.sheet-title { padding: 10px 12px 8px; font-size: 12px; letter-spacing: .05em; text-transform: uppercase; color: var(--muted); }
.prow { display: flex; align-items: center; gap: 10px; padding: 12px; border-radius: 12px; }
.prow:active { background: #1a1c22; }
.prow.cur { background: #171426; }
.prow .txt { flex: 1; min-width: 0; }
.prow .txt b { display: block; font-size: 15px; font-weight: 500; }
.prow .txt span { display: block; font-size: 12.5px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.prow .mark { color: #a89dff; font-weight: 700; }
