/* Тема: светлая/тёмная по системе. Все цвета токенами, компоненты их только читают. */
:root {
  color-scheme: light dark;
  --bg: #f6f5f1;
  --bg-2: #ecebe5;
  --surface: #ffffff;
  --text: #1f1e1b;
  --muted: #6f6c64;
  --line: #dddbd3;
  --accent: #c96442;
  --accent-ink: #ffffff;
  --accent-soft: #f4e3db;
  --user-bubble: #e9e6dd;
  --code-bg: #f1efe8;
  --code-bar: #e6e3da;
  --danger: #b3261e;
  --ok: #2e7d4f;
  --warn-bg: #fff4d6;
  --warn-text: #6b4e00;
  --tk-k: #9a3d8f;
  --tk-s: #2f7a3a;
  --tk-n: #a8521c;
  --tk-c: #8a877e;
  --tk-f: #2457a6;
  --shadow: 0 8px 30px rgba(0, 0, 0, .12);
  --radius: 14px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas, "Liberation Mono", monospace;
  --vvh: 100dvh;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1a1917;
    --bg-2: #24221f;
    --surface: #262421;
    --text: #ece9e2;
    --muted: #9c988e;
    --line: #36332e;
    --accent: #d97757;
    --accent-ink: #1a1917;
    --accent-soft: #3a2a23;
    --user-bubble: #302d29;
    --code-bg: #141311;
    --code-bar: #22201c;
    --danger: #f2948b;
    --ok: #7fc99a;
    --warn-bg: #3a3017;
    --warn-text: #f1d58a;
    --tk-k: #d49ad0;
    --tk-s: #9fd39a;
    --tk-n: #f0a46b;
    --tk-c: #7c786f;
    --tk-f: #8fb6f2;
    --shadow: 0 8px 30px rgba(0, 0, 0, .5);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.55 var(--font);
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
  overflow: hidden;
}
button { font: inherit; color: inherit; cursor: pointer; }
input, select, textarea { font: inherit; color: inherit; }
.muted { color: var(--muted); }
.mono { font-family: var(--mono); font-size: 13px; }
.err-text { color: var(--danger); min-height: 1.4em; margin: 0; font-size: 14px; }

/* ── вход ─────────────────────────────────────────────────────────────── */
.login {
  min-height: var(--vvh);
  display: grid;
  place-items: center;
  padding: 24px 16px calc(24px + env(safe-area-inset-bottom));
  overflow-y: auto;
}
.login-card {
  width: 100%;
  max-width: 360px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  text-align: center;
}
.login-card h1 { margin: 6px 0 0; font-size: 24px; letter-spacing: -.01em; }
.login-card p { margin: 0 0 8px; }
.login-card label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  text-align: left;
  font-size: 14px;
  color: var(--muted);
}
.login-card input {
  height: 48px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  font-size: 17px;
  color: var(--text);
  outline: none;
}
.login-card input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
#login-code { letter-spacing: .25em; font-family: var(--mono); }
.logo {
  width: 52px; height: 52px;
  margin: 0 auto;
  display: grid;
  place-items: center;
  border-radius: 16px;
  background: var(--accent);
  color: var(--accent-ink);
}
.logo.big { width: 64px; height: 64px; border-radius: 20px; }
.primary {
  height: 48px;
  border: 0;
  border-radius: 12px;
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
  font-size: 16px;
}
.primary:disabled { opacity: .6; cursor: default; }

/* ── каркас чата ─────────────────────────────────────────────────────── */
.app {
  height: var(--vvh);
  display: flex;
  flex-direction: column;
  position: relative;
}
.top {
  flex: none;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: calc(6px + env(safe-area-inset-top)) max(8px, env(safe-area-inset-right)) 6px max(8px, env(safe-area-inset-left));
  border-bottom: 1px solid var(--line);
  background: var(--bg);
}
.top-title { flex: 1; min-width: 0; text-align: center; padding: 0 4px; }
.t1 { font-weight: 600; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.t2 { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-family: var(--mono); }
.icon-btn {
  flex: none;
  width: 40px; height: 40px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--text);
}
.icon-btn:hover { background: var(--bg-2); }
.icon-btn:active { background: var(--line); }

.banner {
  flex: none;
  padding: 8px 16px;
  background: var(--warn-bg);
  color: var(--warn-text);
  font-size: 14px;
  border-bottom: 1px solid var(--line);
}

.feed {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 16px max(16px, env(safe-area-inset-right)) 24px max(16px, env(safe-area-inset-left));
}
#msgs, .empty { max-width: 780px; margin: 0 auto; }

.empty { padding: 8vh 0 0; text-align: center; }
.empty h2 { margin: 16px 0 4px; font-size: 22px; letter-spacing: -.01em; }
.empty p { margin: 0 0 20px; word-break: break-all; }
.chips { display: flex; flex-direction: column; gap: 8px; align-items: stretch; max-width: 460px; margin: 0 auto; }
.chip {
  padding: 11px 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  text-align: left;
  font-size: 15px;
}
.chip:hover { border-color: var(--accent); }

/* ── сообщения ───────────────────────────────────────────────────────── */
.msg { margin: 0 0 22px; }
.msg.user { display: flex; justify-content: flex-end; }
.bubble {
  max-width: 88%;
  padding: 10px 14px;
  border-radius: 18px 18px 4px 18px;
  background: var(--user-bubble);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.msg.bot { position: relative; }
.preview-note {
  font-size: 13px;
  color: var(--muted);
  margin: 0 0 8px;
}
.preview {
  border-left: 3px solid var(--line);
  padding: 4px 0 4px 12px;
  white-space: pre-wrap;
  color: var(--muted);
  font-size: 14px;
  margin-bottom: 22px;
  overflow-wrap: anywhere;
}

.meta {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 6px;
  font-size: 12px;
  color: var(--muted);
}
.meta button {
  border: 0;
  background: none;
  padding: 2px 4px;
  color: var(--muted);
  font-size: 12px;
  border-radius: 6px;
}
.meta button:hover { color: var(--text); background: var(--bg-2); }
.msg.err .md { color: var(--danger); }

.steps {
  margin: 0 0 8px;
  font-size: 13px;
  color: var(--muted);
}
.steps summary { cursor: pointer; list-style: none; user-select: none; }
.steps summary::-webkit-details-marker { display: none; }
.steps summary::before { content: "▸ "; }
.steps[open] summary::before { content: "▾ "; }
.steps ol {
  margin: 6px 0 0;
  padding: 0 0 0 4px;
  list-style: none;
  font-family: var(--mono);
  font-size: 12px;
  max-height: 260px;
  overflow-y: auto;
}
.steps li { padding: 1px 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.live-head {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 6px;
}
.pulse {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--accent);
  animation: pulse 1.2s ease-in-out infinite;
}
@keyframes pulse { 0%, 100% { opacity: .35; transform: scale(.85); } 50% { opacity: 1; transform: scale(1); } }
@media (prefers-reduced-motion: reduce) { .pulse { animation: none; } }
.live-steps {
  font-family: var(--mono);
  font-size: 12px;
  color: var(--muted);
  margin: 0 0 8px;
  padding: 0;
  list-style: none;
}
.live-steps li { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; padding: 1px 0; }
.live-steps li:last-child { color: var(--text); }

/* ── markdown ────────────────────────────────────────────────────────── */
.md { overflow-wrap: anywhere; }
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p { margin: 0 0 12px; }
.md h2, .md h3, .md h4, .md h5, .md h6 { margin: 20px 0 8px; line-height: 1.3; letter-spacing: -.01em; }
.md h2 { font-size: 20px; }
.md h3 { font-size: 17px; }
.md h4, .md h5, .md h6 { font-size: 15px; }
.md ul, .md ol { margin: 0 0 12px; padding-left: 24px; }
.md li { margin: 3px 0; }
.md li > ul, .md li > ol { margin: 4px 0 0; }
.md a { color: var(--accent); text-underline-offset: 2px; }
.md hr { border: 0; border-top: 1px solid var(--line); margin: 18px 0; }
.md blockquote { margin: 0 0 12px; padding: 2px 0 2px 14px; border-left: 3px solid var(--line); color: var(--muted); }
.md code {
  font-family: var(--mono);
  font-size: .88em;
  padding: .12em .38em;
  border-radius: 6px;
  background: var(--code-bg);
  border: 1px solid var(--line);
}
.md .task { font-size: 1.05em; }
.md .task.done { color: var(--ok); }
.code {
  margin: 0 0 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  overflow: hidden;
  background: var(--code-bg);
}
.code-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 4px 6px 4px 12px;
  background: var(--code-bar);
  font-size: 12px;
  color: var(--muted);
  font-family: var(--mono);
}
.code-bar .copy {
  border: 0;
  background: none;
  font-size: 12px;
  color: var(--muted);
  padding: 4px 8px;
  border-radius: 6px;
}
.code-bar .copy:hover { color: var(--text); background: var(--line); }
.code pre {
  margin: 0;
  padding: 12px 14px;
  overflow-x: auto;
  font-size: 13px;
  line-height: 1.5;
  -webkit-overflow-scrolling: touch;
}
.code pre code { padding: 0; border: 0; background: none; font-size: inherit; white-space: pre; }
.tk-k { color: var(--tk-k); }
.tk-s { color: var(--tk-s); }
.tk-n { color: var(--tk-n); }
.tk-c { color: var(--tk-c); font-style: italic; }
.tk-f { color: var(--tk-f); }
.table { overflow-x: auto; margin: 0 0 12px; border: 1px solid var(--line); border-radius: 10px; }
.md table { border-collapse: collapse; font-size: 14px; min-width: 100%; }
.md th, .md td { padding: 7px 11px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
.md th { background: var(--bg-2); font-weight: 600; white-space: nowrap; }
.md tr:last-child td { border-bottom: 0; }
.md .al-c { text-align: center; }
.md .al-r { text-align: right; }

/* ── поле ввода ──────────────────────────────────────────────────────── */
.composer {
  flex: none;
  padding: 8px max(12px, env(safe-area-inset-right)) calc(8px + env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  background: var(--bg);
}
.composer-box {
  max-width: 780px;
  margin: 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 6px 6px 6px 14px;
  border: 1px solid var(--line);
  border-radius: 22px;
  background: var(--surface);
  box-shadow: 0 1px 2px rgba(0, 0, 0, .04);
}
.composer-box:focus-within { border-color: var(--accent); }
#input {
  flex: 1;
  min-width: 0;
  resize: none;
  border: 0;
  outline: none;
  background: transparent;
  font-size: 16px; /* меньше 16px iOS зумит страницу при фокусе */
  line-height: 1.45;
  padding: 7px 0;
  max-height: 40vh;
}
.send {
  flex: none;
  width: 36px; height: 36px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-ink);
}
.send:disabled { background: var(--line); color: var(--muted); cursor: default; }
.send.stop { background: var(--text); color: var(--bg); }
.hint {
  max-width: 780px;
  margin: 4px auto 0;
  min-height: 0;
  font-size: 12px;
  color: var(--muted);
  text-align: center;
}
.hint:empty { display: none; }

.to-bottom {
  position: absolute;
  left: 50%;
  bottom: calc(86px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  width: 38px; height: 38px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: var(--surface);
  box-shadow: var(--shadow);
  z-index: 5;
}

/* ── шторки ──────────────────────────────────────────────────────────── */
.scrim {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .35);
  z-index: 20;
}
.drawer {
  position: fixed;
  top: 0; bottom: 0; left: 0;
  width: min(380px, 88vw);
  z-index: 30;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  border-right: 1px solid var(--line);
  box-shadow: var(--shadow);
  padding: env(safe-area-inset-top) 0 env(safe-area-inset-bottom) env(safe-area-inset-left);
  animation: slide-in .18s ease-out;
}
@keyframes slide-in { from { transform: translateX(-100%); } to { transform: none; } }
.drawer-head { display: flex; align-items: center; gap: 4px; padding: 8px 8px 8px 16px; }
.drawer-head h2 { flex: 1; margin: 0; font-size: 18px; }
.new-chat {
  margin: 0 12px 8px;
  height: 42px;
  border: 1px dashed var(--line);
  border-radius: 12px;
  background: transparent;
  font-weight: 600;
}
.new-chat:hover { border-color: var(--accent); color: var(--accent); }
.sess-list { flex: 1; overflow-y: auto; padding: 0 8px; -webkit-overflow-scrolling: touch; }
.sess {
  display: block;
  width: 100%;
  text-align: left;
  padding: 10px 10px;
  margin: 0 0 2px;
  border: 0;
  border-radius: 10px;
  background: transparent;
}
.sess:hover { background: var(--bg-2); }
.sess.current { background: var(--accent-soft); }
.sess .s1 { font-size: 15px; font-weight: 500; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.sess .s2 { font-size: 12px; color: var(--muted); font-family: var(--mono); margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sess-empty { padding: 20px 12px; color: var(--muted); font-size: 14px; text-align: center; }
.drawer-note { margin: 0; padding: 10px 16px 12px; font-size: 12px; color: var(--muted); border-top: 1px solid var(--line); }

.sheet {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 30;
  max-width: 560px;
  margin: 0 auto;
  padding: 8px 20px calc(20px + env(safe-area-inset-bottom));
  background: var(--bg);
  border-radius: 20px 20px 0 0;
  border: 1px solid var(--line);
  border-bottom: 0;
  box-shadow: var(--shadow);
  animation: sheet-up .2s ease-out;
  max-height: 85vh;
  overflow-y: auto;
}
@keyframes sheet-up { from { transform: translateY(100%); } to { transform: none; } }
.sheet-grip { width: 40px; height: 5px; border-radius: 3px; background: var(--line); margin: 0 auto 10px; }
.sheet h2 { margin: 0 0 14px; font-size: 18px; }
.field { display: flex; flex-direction: column; gap: 8px; margin: 0 0 18px; font-size: 14px; color: var(--muted); }
.field select {
  height: 44px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  font-size: 16px;
}
.seg { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.seg button {
  min-width: 0;
  height: 38px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font-size: 14px;
}
.seg button[aria-checked="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.exec { color: var(--text); font-size: 14px; }
.exec .ok { color: var(--ok); }
.exec .bad { color: var(--danger); }
.danger {
  width: 100%;
  height: 44px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: transparent;
  color: var(--danger);
  font-weight: 600;
}

.toast {
  position: fixed;
  left: 50%;
  top: calc(64px + env(safe-area-inset-top));
  transform: translateX(-50%);
  z-index: 40;
  max-width: calc(100vw - 32px);
  padding: 9px 16px;
  border-radius: 12px;
  background: var(--text);
  color: var(--bg);
  font-size: 14px;
  box-shadow: var(--shadow);
}

@media (min-width: 900px) {
  .feed { padding-top: 28px; }
  .msg { margin-bottom: 28px; }
}
