:root {
  --bg: #ffffff;
  --bg-2: #f7f7f8;
  --bg-3: #ececf1;
  --text: #1f1f24;
  --text-2: #6b6b76;
  --border: #e3e3e8;
  --accent: #6c5ce7;
  --accent-hover: #5b4bd6;
  --accent-soft: #ece9fd;
  --user-bubble: #f0eefc;
  --danger: #d9534f;
  --code-bg: #f4f4f7;
  --shadow: 0 8px 30px rgba(0,0,0,.08);
  --sidebar-w: 280px;
  --radius: 14px;
}
[data-theme="dark"] {
  --bg: #16161a;
  --bg-2: #1e1e24;
  --bg-3: #2a2a33;
  --text: #ececf1;
  --text-2: #9a9aa6;
  --border: #2e2e38;
  --accent: #8b7cf6;
  --accent-hover: #9d90fa;
  --accent-soft: #2b2749;
  --user-bubble: #2a2740;
  --code-bg: #111116;
  --shadow: 0 8px 30px rgba(0,0,0,.4);
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, "Helvetica Neue", Arial, sans-serif;
  font-size: 15px;
  line-height: 1.55;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}
button { font: inherit; color: inherit; cursor: pointer; border: 0; background: none; }
input, textarea, select { font: inherit; color: inherit; }
::selection { background: var(--accent-soft); }

.app { display: flex; height: 100dvh; overflow: hidden; }

/* ---------- Сайдбар ---------- */
.sidebar {
  width: var(--sidebar-w);
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  background: var(--bg-2);
  border-right: 1px solid var(--border);
  transition: margin-left .25s ease;
}
.app.sidebar-hidden .sidebar { margin-left: calc(-1 * var(--sidebar-w)); }
.sidebar-header { display: flex; gap: 8px; padding: 12px; align-items: center; }
.sidebar-header .btn-primary { flex: 1; }
.search {
  margin: 0 12px 8px;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  outline: none;
}
.search:focus { border-color: var(--accent); }
.chat-list { flex: 1; overflow-y: auto; padding: 4px 8px; }
.chat-group-label {
  font-size: 11px; text-transform: uppercase; letter-spacing: .06em;
  color: var(--text-2); padding: 12px 8px 4px;
}
.chat-item {
  display: flex; align-items: center; gap: 6px;
  padding: 8px 10px; border-radius: 10px; cursor: pointer;
  color: var(--text);
}
.chat-item:hover { background: var(--bg-3); }
.chat-item.active { background: var(--accent-soft); }
.chat-item .title {
  flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 14px;
}
.chat-item .actions { display: none; gap: 2px; }
.chat-item:hover .actions, .chat-item.active .actions { display: flex; }
.chat-item .actions button { padding: 3px; border-radius: 6px; color: var(--text-2); display: flex; }
.chat-item .actions button:hover { background: var(--bg); color: var(--text); }
.chat-item input.rename {
  flex: 1; border: 1px solid var(--accent); border-radius: 6px; padding: 2px 6px; background: var(--bg); outline: none; font-size: 14px;
}
.sidebar-footer { padding: 8px; border-top: 1px solid var(--border); display: flex; flex-direction: column; gap: 2px; }
.sidebar-backdrop { display: none; }

/* ---------- Кнопки ---------- */
.btn-primary {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 9px 14px; border-radius: 10px;
  background: var(--accent); color: #fff; font-weight: 500;
  transition: background .15s;
}
.btn-primary:hover { background: var(--accent-hover); }
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 36px; height: 36px; border-radius: 10px; color: var(--text-2);
}
.icon-btn:hover { background: var(--bg-3); color: var(--text); }
.ghost-btn {
  display: flex; align-items: center; gap: 10px; width: 100%;
  padding: 9px 12px; border-radius: 10px; color: var(--text); text-align: left;
}
.ghost-btn:hover { background: var(--bg-3); }
.ghost-btn.danger { color: var(--danger); width: auto; }
.ghost-btn.danger:hover { background: rgba(217,83,79,.1); }
.icon-moon { display: none; }
[data-theme="dark"] .icon-moon { display: block; }
[data-theme="dark"] .icon-sun { display: none; }

/* ---------- Основная область ---------- */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; position: relative; }
.topbar {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 14px; border-bottom: 1px solid var(--border);
  background: var(--bg);
}
.app:not(.sidebar-hidden) #open-sidebar { display: none; }
.model-picker select {
  appearance: none; -webkit-appearance: none;
  padding: 7px 32px 7px 12px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--bg-2);
  font-weight: 500; cursor: pointer; outline: none; max-width: 320px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%23888' stroke-width='2.5'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 10px center;
}
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 14px; }
.think-toggle { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--text-2); cursor: pointer; user-select: none; }
.think-toggle input { accent-color: var(--accent); width: 16px; height: 16px; cursor: pointer; }
.status-dot { width: 10px; height: 10px; border-radius: 50%; background: #bbb; }
.status-dot.ok { background: #2ecc71; }
.status-dot.err { background: var(--danger); }

.messages-wrap { flex: 1; overflow-y: auto; scroll-behavior: smooth; }
.messages { max-width: 820px; margin: 0 auto; padding: 24px 20px 20px; display: flex; flex-direction: column; gap: 22px; }

.empty-state {
  height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center;
  text-align: center; padding: 20px; color: var(--text-2);
}
.empty-state.hidden { display: none; }
.empty-state .logo {
  width: 64px; height: 64px; border-radius: 18px; background: var(--accent); color: #fff;
  display: flex; align-items: center; justify-content: center; font-size: 34px; font-weight: 700;
  margin-bottom: 18px; box-shadow: var(--shadow);
}
.empty-state h1 { margin: 0 0 6px; font-size: 26px; color: var(--text); }
.empty-state p { margin: 0 0 26px; }
.suggestions { display: grid; grid-template-columns: repeat(2, minmax(0, 260px)); gap: 10px; }
.suggestions button {
  padding: 12px 14px; border: 1px solid var(--border); border-radius: 12px;
  background: var(--bg-2); text-align: left; font-size: 13.5px; color: var(--text);
}
.suggestions button:hover { background: var(--bg-3); }

/* ---------- Сообщения ---------- */
.msg { display: flex; gap: 12px; }
.msg.user { flex-direction: row-reverse; }
.avatar {
  flex: 0 0 30px; width: 30px; height: 30px; border-radius: 9px;
  display: flex; align-items: center; justify-content: center; font-size: 14px; font-weight: 700;
}
.msg.assistant .avatar { background: var(--accent); color: #fff; }
.msg.user .avatar { background: var(--bg-3); color: var(--text-2); }
.bubble { min-width: 0; max-width: 100%; }
.msg.user .bubble {
  background: var(--user-bubble); padding: 10px 16px; border-radius: 18px 18px 4px 18px; max-width: 80%;
  white-space: pre-wrap; word-wrap: break-word;
}
.msg.assistant .bubble { flex: 1; padding-top: 3px; }
.msg.error .bubble { color: var(--danger); background: rgba(217,83,79,.08); padding: 10px 14px; border-radius: 12px; }

.msg-actions { display: flex; gap: 4px; margin-top: 6px; opacity: 0; transition: opacity .15s; }
.msg:hover .msg-actions { opacity: 1; }
.msg.user .msg-actions { justify-content: flex-end; }
.msg-actions button {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 4px 8px; border-radius: 7px; font-size: 12px; color: var(--text-2);
}
.msg-actions button:hover { background: var(--bg-3); color: var(--text); }
.msg-meta { font-size: 11.5px; color: var(--text-2); align-self: center; margin-left: 6px; }

.edit-box textarea {
  width: 100%; min-height: 80px; padding: 10px 12px; border-radius: 12px;
  border: 1px solid var(--accent); background: var(--bg); outline: none; resize: vertical;
}
.edit-box .row { display: flex; gap: 8px; justify-content: flex-end; margin-top: 8px; }
.edit-box .row button { padding: 6px 14px; border-radius: 8px; }
.edit-box .row .cancel { background: var(--bg-3); }

/* Размышления */
.thinking {
  border: 1px solid var(--border); border-radius: 12px; margin-bottom: 12px;
  background: var(--bg-2); overflow: hidden;
}
.thinking summary {
  cursor: pointer; padding: 8px 12px; font-size: 13px; color: var(--text-2);
  display: flex; align-items: center; gap: 8px; list-style: none; user-select: none;
}
.thinking summary::-webkit-details-marker { display: none; }
.thinking summary::before {
  content: ""; width: 6px; height: 6px; border-right: 1.5px solid currentColor; border-bottom: 1.5px solid currentColor;
  transform: rotate(-45deg); transition: transform .15s; margin-right: 2px;
}
.thinking[open] summary::before { transform: rotate(45deg); }
.thinking.active summary { color: var(--accent); }
.thinking.active summary::after {
  content: ""; width: 12px; height: 12px; border: 2px solid var(--accent); border-top-color: transparent;
  border-radius: 50%; animation: spin .8s linear infinite; margin-left: auto;
}
@keyframes spin { to { transform: rotate(360deg); } }
.thinking .think-body {
  padding: 4px 14px 12px; font-size: 13.5px; color: var(--text-2);
  white-space: pre-wrap; word-wrap: break-word; max-height: 320px; overflow-y: auto;
  border-top: 1px solid var(--border);
}

/* Markdown */
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p { margin: 0 0 12px; }
.md h1, .md h2, .md h3, .md h4 { margin: 20px 0 10px; line-height: 1.3; }
.md h1 { font-size: 1.5em; } .md h2 { font-size: 1.3em; } .md h3 { font-size: 1.15em; }
.md ul, .md ol { padding-left: 24px; margin: 0 0 12px; }
.md li { margin: 3px 0; }
.md blockquote { margin: 0 0 12px; padding: 4px 14px; border-left: 3px solid var(--accent); color: var(--text-2); }
.md a { color: var(--accent); }
.md hr { border: 0; border-top: 1px solid var(--border); margin: 16px 0; }
.md table { border-collapse: collapse; margin: 0 0 12px; display: block; overflow-x: auto; max-width: 100%; }
.md th, .md td { border: 1px solid var(--border); padding: 6px 12px; text-align: left; }
.md th { background: var(--bg-2); }
.md img { max-width: 100%; border-radius: 8px; }
.md code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .9em;
  background: var(--code-bg); padding: 2px 5px; border-radius: 5px;
}
.md pre { margin: 0 0 12px; }
.code-block { border: 1px solid var(--border); border-radius: 10px; overflow: hidden; background: var(--code-bg); margin: 0 0 12px; }
.code-block .code-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 5px 12px; font-size: 12px; color: var(--text-2); background: var(--bg-3);
}
.code-block .code-head button { font-size: 12px; padding: 2px 8px; border-radius: 6px; color: var(--text-2); display: inline-flex; gap: 4px; align-items: center; }
.code-block .code-head button:hover { background: var(--bg); color: var(--text); }
.code-block pre { margin: 0; padding: 12px 14px; overflow-x: auto; }
.code-block pre code { background: none; padding: 0; font-size: 13px; line-height: 1.5; }
.hljs { background: transparent !important; }

.cursor::after {
  content: "▍"; color: var(--accent); animation: blink 1s steps(2) infinite; margin-left: 1px;
}
@keyframes blink { 50% { opacity: 0; } }

/* ---------- Ввод ---------- */
.composer-wrap { padding: 8px 20px 12px; background: var(--bg); }
.composer {
  max-width: 820px; margin: 0 auto; display: flex; align-items: flex-end; gap: 8px;
  border: 1px solid var(--border); border-radius: 18px; padding: 8px 8px 8px 16px;
  background: var(--bg-2); box-shadow: 0 2px 12px rgba(0,0,0,.04);
  transition: border-color .15s, box-shadow .15s;
}
.composer:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.composer textarea {
  flex: 1; resize: none; border: 0; outline: none; background: transparent;
  max-height: 240px; padding: 6px 0; line-height: 1.5;
}
.send-btn {
  width: 36px; height: 36px; border-radius: 12px; flex: 0 0 auto;
  background: var(--accent); color: #fff; display: flex; align-items: center; justify-content: center;
  transition: background .15s, opacity .15s;
}
.send-btn:hover { background: var(--accent-hover); }
.send-btn:disabled { opacity: .35; cursor: default; }
.send-btn .icon-stop { display: none; }
.send-btn.stop { background: var(--text); color: var(--bg); }
.send-btn.stop .icon-send { display: none; }
.send-btn.stop .icon-stop { display: block; }
.disclaimer { text-align: center; font-size: 11.5px; color: var(--text-2); margin: 8px 0 0; }

.scroll-bottom {
  position: absolute; bottom: 96px; left: 50%; transform: translateX(-50%);
  width: 36px; height: 36px; border-radius: 50%; background: var(--bg); border: 1px solid var(--border);
  box-shadow: var(--shadow); display: none; align-items: center; justify-content: center; color: var(--text-2); z-index: 5;
}
.scroll-bottom.show { display: flex; }

/* ---------- Модалка ---------- */
.modal { position: fixed; inset: 0; z-index: 50; display: flex; align-items: center; justify-content: center; }
.modal[hidden] { display: none; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,.45); }
.modal-card {
  position: relative; width: min(560px, calc(100% - 32px)); max-height: calc(100% - 32px);
  background: var(--bg); border-radius: 18px; box-shadow: var(--shadow); display: flex; flex-direction: column;
}
.modal-header { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px 8px; }
.modal-header h2 { margin: 0; font-size: 18px; }
.modal-body { padding: 8px 20px; overflow-y: auto; display: flex; flex-direction: column; gap: 14px; }
.modal-footer { display: flex; align-items: center; gap: 8px; padding: 12px 20px 16px; border-top: 1px solid var(--border); }
.modal-footer .spacer { flex: 1; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 13.5px; flex: 1; }
.field > span { color: var(--text-2); }
.field textarea, .field input[type="number"] {
  padding: 9px 12px; border: 1px solid var(--border); border-radius: 10px; background: var(--bg-2); outline: none; resize: vertical;
}
.field textarea:focus, .field input[type="number"]:focus { border-color: var(--accent); }
.field input[type="range"] { accent-color: var(--accent); }
.field.checkbox { flex-direction: row; align-items: center; gap: 10px; cursor: pointer; }
.field.checkbox input { accent-color: var(--accent); width: 16px; height: 16px; }
.field-row { display: flex; gap: 16px; }
.hint { font-size: 12.5px; color: var(--text-2); margin: 0; }
.hint code { background: var(--code-bg); padding: 1px 5px; border-radius: 4px; font-size: 12px; }

.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px;
  font-size: 13.5px; box-shadow: var(--shadow); z-index: 100;
}

/* ---------- Мобильная версия ---------- */
@media (max-width: 800px) {
  .sidebar { position: fixed; inset: 0 auto 0 0; z-index: 30; margin-left: 0 !important; transform: translateX(0); transition: transform .25s ease; }
  .app.sidebar-hidden .sidebar { transform: translateX(-100%); }
  .app:not(.sidebar-hidden) .sidebar-backdrop { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 25; }
  .app:not(.sidebar-hidden) #open-sidebar { display: inline-flex; }
  .suggestions { grid-template-columns: 1fr; }
  .field-row { flex-direction: column; gap: 14px; }
  .think-toggle span { display: none; }
  .msg.user .bubble { max-width: 90%; }
  .messages { padding: 16px 12px; }
  .composer-wrap { padding: 6px 10px 10px; }
}
