:root {
  --bg: #f6f7f4;
  --panel: #ffffff;
  --panel-2: #eef1ec;
  --line: #dfe4dc;
  --text: #1d2420;
  --muted: #6a756e;
  --accent: #2f7d5b;
  --accent-text: #ffffff;
  --accent-soft: #e2f0e8;
  --user-bubble: #2f7d5b;
  --user-text: #ffffff;
  --ai-bubble: #ffffff;
  --danger: #b4412f;
  --code-bg: #f0f2ee;
  --shadow: 0 1px 2px rgba(20, 30, 25, .06), 0 4px 16px rgba(20, 30, 25, .05);
  --left-w: 248px;
  --right-w: 288px;
  --radius: 12px;
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #121614;
  --panel: #1a1f1c;
  --panel-2: #222924;
  --line: #2e3631;
  --text: #e4ebe6;
  --muted: #8f9a93;
  --accent: #5fb98d;
  --accent-text: #0e1a14;
  --accent-soft: #1f3329;
  --user-bubble: #2c5a45;
  --user-text: #eef7f2;
  --ai-bubble: #1a1f1c;
  --danger: #e07a68;
  --code-bg: #232a26;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
button, input, textarea, select { font: inherit; color: inherit; }
a { color: var(--accent); }
code { font-family: ui-monospace, SFMono-Regular, Consolas, "Liberation Mono", monospace; font-size: .9em; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.pad { padding: 8px 12px; }
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

/* ── Кнопки и поля ─────────────────────────────────────────────── */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 9px 16px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--panel); cursor: pointer; text-decoration: none; color: var(--text);
}
.btn:hover { background: var(--panel-2); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
.btn-primary:hover { background: var(--accent); filter: brightness(1.07); }
.btn-ghost { border-color: transparent; background: transparent; padding: 7px 10px; color: var(--muted); }
.btn-ghost:hover { color: var(--text); }
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center; flex: none;
  width: 36px; height: 36px; border-radius: 9px; border: 0; background: transparent;
  color: var(--muted); cursor: pointer;
}
.icon-btn:hover { background: var(--panel-2); color: var(--text); }
.icon-btn.small { width: 30px; height: 30px; }
.icon-btn.small svg { width: 17px; height: 17px; }
.icon-btn.danger:hover { color: var(--danger); }
.link-btn { border: 0; background: none; padding: 0; color: var(--accent); cursor: pointer; font-size: 13px; white-space: nowrap; }
.link-btn:hover { text-decoration: underline; }
input, textarea, select {
  width: 100%; padding: 9px 11px; border: 1px solid var(--line); border-radius: 10px;
  background: var(--panel); outline: none;
}
input:focus, textarea:focus, select:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
textarea { resize: vertical; }
label { display: block; font-size: 13px; font-weight: 600; color: var(--muted); margin: 14px 0 6px; }

/* ── Вход и страница ошибки ────────────────────────────────────── */
.page-center { display: grid; place-items: center; padding: 16px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 16px; box-shadow: var(--shadow); }
.login { width: 100%; max-width: 360px; padding: 28px; display: flex; flex-direction: column; }
.login .btn { margin-top: 18px; }
.form-error { color: var(--danger); margin: 10px 0 0; font-size: 14px; }
.form-note { color: var(--muted); margin: 10px 0 0; font-size: 13px; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 17px; letter-spacing: .2px; }
.brand-mark { width: 22px; height: 22px; border-radius: 50%; background: radial-gradient(circle at 30% 30%, #7fd1a6, var(--accent)); flex: none; }

/* ── Каркас: три колонки ───────────────────────────────────────── */
.app {
  display: grid;
  grid-template-columns: var(--left-w) minmax(0, 1fr) var(--right-w);
  height: 100vh; height: 100dvh;
}
.col-left, .col-right {
  background: var(--panel); display: flex; flex-direction: column; min-height: 0; overflow: hidden;
}
.col-left { border-right: 1px solid var(--line); }
.col-right { border-left: 1px solid var(--line); }
.left-head, .right-head {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 14px 14px 10px; min-height: 60px;
}
.right-head h2 { font-size: 15px; margin: 0; }
.head-actions { display: flex; align-items: center; gap: 2px; }
.passport-name { font-weight: 600; font-size: 16px; margin-bottom: 4px; overflow-wrap: anywhere; }
.new-project { display: flex; gap: 6px; padding: 0 12px 10px; }
.new-project input { padding: 7px 10px; }
.new-project.big { padding: 0; max-width: 420px; margin: 18px auto 0; }
.project-list { flex: 1; overflow-y: auto; padding: 4px 8px; }
.project-link {
  display: flex; align-items: center; gap: 6px; padding: 8px 8px; border-radius: 9px;
  color: var(--text); text-decoration: none;
}
.project-link span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.project-link .chev { width: 15px; height: 15px; flex: none; color: var(--muted); transition: transform .15s; }
.project.open > .project-link .chev { transform: rotate(90deg); }
.project-link:hover { background: var(--panel-2); }
.project-link.active { font-weight: 600; }
.chat-list { margin: 2px 0 8px 20px; padding-left: 8px; border-left: 1px solid var(--line); }
.chat-link {
  display: block; padding: 6px 10px; border-radius: 8px; color: var(--text); text-decoration: none;
  font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.chat-link:hover { background: var(--panel-2); }
.chat-link.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.chat-new {
  display: flex; align-items: center; gap: 6px; width: 100%; padding: 6px 10px; border: 0; border-radius: 8px;
  background: none; color: var(--accent); font-size: 14px; cursor: pointer; text-align: left;
}
.chat-new svg { width: 15px; height: 15px; }
.chat-new:hover { background: var(--panel-2); }
.left-foot { border-top: 1px solid var(--line); padding: 8px; display: flex; justify-content: space-between; align-items: center; }

.passport { padding: 0 16px 16px; overflow-y: auto; }
.passport .btn { margin-top: 12px; }
.saved { color: var(--accent); margin-left: 10px; }

/* ── Чат ───────────────────────────────────────────────────────── */
.col-chat { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.chat-head {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 10px 16px; min-height: 60px; border-bottom: 1px solid var(--line); background: var(--bg);
}
.title-wrap { display: flex; align-items: center; gap: 2px; min-width: 0; flex: 1 1 160px; }
.chat-title { font-size: 17px; margin: 0 6px 0 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.model-picker { display: flex; align-items: center; gap: 10px; flex: 0 1 auto; min-width: 0; }
.model-picker select { width: 260px; max-width: 100%; padding: 7px 10px; }
.model-status { color: var(--muted); white-space: nowrap; }
.model-status.err { color: var(--danger); white-space: normal; }

.feed { flex: 1; overflow-y: auto; min-height: 0; }
.feed-inner { max-width: 820px; margin: 0 auto; padding: 22px 18px 8px; display: flex; flex-direction: column; gap: 14px; }
.msg { display: flex; flex-direction: column; }
.msg-user { align-items: flex-end; }
.msg-ai { align-items: flex-start; }
.bubble { max-width: min(680px, 92%); padding: 10px 14px; border-radius: 14px; overflow-wrap: anywhere; }
.msg-user .bubble { background: var(--user-bubble); color: var(--user-text); white-space: pre-wrap; border-bottom-right-radius: 4px; }
.msg-ai .bubble { background: var(--ai-bubble); border: 1px solid var(--line); border-bottom-left-radius: 4px; box-shadow: var(--shadow); }
.msg-ai .bubble.streaming { white-space: pre-wrap; }
.msg-ai .bubble.error { border-color: var(--danger); color: var(--danger); }
.msg-meta { font-size: 12px; color: var(--muted); margin: 4px 6px 0; }
.typing::after { content: "▍"; animation: blink 1s steps(1) infinite; color: var(--muted); }
@keyframes blink { 50% { opacity: 0; } }

.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p, .md ul, .md ol, .md pre, .md table, .md blockquote { margin: 0 0 .7em; }
.md h1, .md h2, .md h3, .md h4 { margin: .9em 0 .4em; line-height: 1.3; }
.md h1 { font-size: 1.3em; } .md h2 { font-size: 1.18em; } .md h3 { font-size: 1.06em; }
.md ul, .md ol { padding-left: 1.4em; }
.md code { background: var(--code-bg); padding: 1px 5px; border-radius: 5px; }
.md pre { background: var(--code-bg); padding: 10px 12px; border-radius: 9px; overflow-x: auto; }
.md pre code { background: none; padding: 0; }
.md blockquote { border-left: 3px solid var(--line); padding-left: 10px; color: var(--muted); }
.md table { border-collapse: collapse; display: block; overflow-x: auto; max-width: 100%; }
.md th, .md td { border: 1px solid var(--line); padding: 5px 9px; text-align: left; }
.md th { background: var(--panel-2); }

.empty-chat, .welcome { text-align: center; color: var(--text); margin: auto; padding: 48px 16px; }
.welcome h2 { margin: 0 0 8px; }

.composer {
  display: flex; gap: 8px; align-items: flex-end;
  max-width: 820px; width: 100%; margin: 0 auto; padding: 10px 18px 16px;
  padding-bottom: max(16px, env(safe-area-inset-bottom));
}
.composer textarea {
  resize: none; max-height: 40vh; min-height: 46px; padding: 12px 14px; border-radius: 14px;
  background: var(--panel); box-shadow: var(--shadow); line-height: 1.45;
}
.send-btn {
  flex: none; width: 46px; height: 46px; border-radius: 14px; border: 0; cursor: pointer;
  background: var(--accent); color: var(--accent-text); display: grid; place-items: center;
}
.send-btn:disabled { opacity: .5; cursor: default; }

/* ── Адаптив: боковые колонки превращаются в выезжающие панели ─── */
.overlay { display: none; }
.only-mobile { display: none; }
.hide-wide-left, .hide-wide-right { display: none; }

@media (max-width: 1100px) {
  .app { grid-template-columns: var(--left-w) minmax(0, 1fr); }
  .hide-wide-right { display: inline-flex; }
  .col-right {
    position: fixed; top: 0; right: 0; bottom: 0; width: min(var(--right-w), 88vw); z-index: 30;
    transform: translateX(100%); transition: transform .2s ease; box-shadow: var(--shadow);
  }
  body.show-right .col-right { transform: none; }
  body.show-right .overlay, body.show-left .overlay {
    display: block; position: fixed; inset: 0; background: rgba(0, 0, 0, .35); z-index: 20;
  }
}
@media (max-width: 760px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .hide-wide-left, .only-mobile { display: inline-flex; }
  .col-left {
    position: fixed; top: 0; left: 0; bottom: 0; width: min(var(--left-w), 84vw); z-index: 30;
    transform: translateX(-100%); transition: transform .2s ease; box-shadow: var(--shadow);
  }
  body.show-left .col-left { transform: none; }
  .chat-head { padding: 8px 10px; gap: 6px; }
  .title-wrap { flex: 1 1 0; }
  .model-picker { order: 3; flex: 1 1 100%; flex-wrap: wrap; gap: 4px 10px; }
  .model-picker select { width: 100%; }
  .feed-inner { padding: 14px 12px 6px; }
  .bubble { max-width: 96%; }
  .composer { padding: 8px 10px 12px; }
  .composer textarea { font-size: 16px; } /* iOS не зумит поле при фокусе */
}
