/* Темы */
:root, [data-theme="dark"] {
  --bg: #14151a; --bg2: #1c1e26; --bg3: #242734;
  --fg: #e4e6ee; --muted: #8b90a3; --border: #2c3040;
  --accent: #6c8cff; --accent-fg: #fff; --danger: #e5604c;
  --user-bubble: #2b3350; --assistant-bubble: #1e2028;
  --code-bg: #101218;
  /* Типографская шкала: xs — служебное, sm — вторичное, md — тело,
     lg — заголовки, display — код входа */
  --fs-xs: 12px; --fs-sm: 13px; --fs-md: 15px; --fs-lg: 20px; --fs-display: 40px;
  /* Шкала отступов, шаг 4px (индекс = кратность) */
  --sp-1: 4px; --sp-2: 8px; --sp-3: 12px; --sp-4: 16px; --sp-5: 20px; --sp-6: 24px;
  --sp-8: 32px; --sp-10: 40px;
}
[data-theme="light"] {
  --bg: #f4f5f8; --bg2: #ffffff; --bg3: #e9ebf2;
  --fg: #212430; --muted: #6a7080; --border: #d5d9e4;
  --accent: #3f63d8; --accent-fg: #fff; --danger: #c0392b;
  --user-bubble: #dbe4ff; --assistant-bubble: #ffffff;
  --code-bg: #2b2e3a;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--fg);
  font: var(--fs-md)/1.55 -apple-system, "Segoe UI", Roboto, sans-serif;
}
button { font: inherit; cursor: pointer; }
.hidden { display: none !important; }
.muted { color: var(--muted); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--sp-2);
  border: 1px solid var(--border); background: var(--bg3); color: var(--fg);
  border-radius: 8px; padding: var(--sp-2) var(--sp-3);
}
.btn:hover { filter: brightness(1.15); }
.btn.primary { background: var(--accent); color: var(--accent-fg); border-color: transparent; }
.btn.secondary { background: transparent; }
.btn.small { padding: var(--sp-1) var(--sp-2); font-size: var(--fs-sm); border-radius: 6px; }
.btn.block { width: 100%; }
.btn.danger { background: var(--danger); color: #fff; border-color: transparent; }
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  background: none; border: none; color: var(--fg);
  border-radius: 6px; padding: var(--sp-1);
}
.icon-btn:hover { background: var(--bg3); }
/* Векторные иконки: stroke наследует currentColor от кнопки */
.ic {
  width: 18px; height: 18px; flex-shrink: 0;
  fill: none; stroke: currentColor; stroke-width: 2;
  stroke-linecap: round; stroke-linejoin: round;
}
/* Иконка в текстовых контекстах (чипы, мета, заголовки чатов) — мельче */
.chat-item .title .ic, .att-chip .ic, .meta .ic, .think-toggle .ic {
  width: 14px; height: 14px;
}
.meta .ic, .att-chip .ic { vertical-align: -2px; }
/* Активное состояние иконок-переключателей (закреплено) */
.icon-btn.on, .chat-item .pinbtn.on { color: var(--accent); }
.chat-item .pinbtn { background: none; border: none; color: var(--muted); border-radius: 6px; padding: 2px; }
.chat-item .pinbtn:hover { color: var(--fg); }

/* Состояния интерактивных элементов: плавные переходы, явный фокус с клавиатуры */
.btn, .icon-btn, .chat-item, .hit, .chat-item .pinbtn, .chat-item .del,
input, textarea, select {
  transition: background-color .12s, border-color .12s, color .12s, filter .12s;
}
.btn:active, .icon-btn:active { filter: brightness(.85); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Экран входа */
.screen { display: flex; align-items: center; justify-content: center; min-height: 100vh; }
.auth-card {
  background: var(--bg2); border: 1px solid var(--border); border-radius: 14px;
  padding: var(--sp-8) var(--sp-10); text-align: center; max-width: 380px; width: 90%;
}
.auth-card h1 { margin: 0 0 var(--sp-1); font-size: var(--fs-lg); }
.auth-code {
  font-family: ui-monospace, Consolas, monospace; font-size: var(--fs-display); letter-spacing: 8px;
  margin: var(--sp-5) 0 var(--sp-2); color: var(--accent); user-select: all;
}
.auth-status { font-weight: 600; margin: var(--sp-2) 0; }
#auth-timer { margin-bottom: var(--sp-3); }

/* Каркас */
#app { display: grid; grid-template-columns: 290px 1fr; height: 100vh; }

/* Сайдбар */
#sidebar {
  background: var(--bg2); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; padding: var(--sp-3); gap: var(--sp-2); overflow: hidden;
}
.side-head { display: flex; align-items: center; justify-content: space-between; gap: var(--sp-2); }
#user-info { font-size: var(--fs-sm); color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.side-actions { display: flex; gap: 2px; flex-shrink: 0; }
#search {
  background: var(--bg); color: var(--fg); border: 1px solid var(--border);
  border-radius: 8px; padding: var(--sp-2) var(--sp-2); width: 100%;
}
#chat-list { overflow-y: auto; flex: 1; }
.chat-item {
  padding: var(--sp-2) var(--sp-3); border-radius: 8px; cursor: pointer; margin-bottom: 2px;
  display: flex; justify-content: space-between; gap: var(--sp-2); align-items: center;
}
.chat-item:hover { background: var(--bg3); }
.chat-item.active { background: var(--bg3); border-left: 3px solid var(--accent); }
.chat-item .title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-item .del { visibility: hidden; color: var(--muted); }
.chat-item:hover .del { visibility: visible; }
.chat-item .del:hover { color: var(--danger); }
/* бейдж «общий» у расшаренных чатов и имя владельца в строке */
.badge-shared {
  font-size: var(--fs-xs); background: var(--bg3); color: var(--fg);
  border: 1px solid var(--border); border-radius: 4px; padding: 0 var(--sp-1);
}

#search-results { overflow-y: auto; flex: 1; font-size: var(--fs-sm); }
#search-results .hit { padding: var(--sp-2) var(--sp-2); border-radius: 8px; cursor: pointer; border-bottom: 1px solid var(--border); }
#search-results .hit:hover { background: var(--bg3); }
#search-results .hit .src { color: var(--muted); font-size: var(--fs-xs); }

/* Чат */
#main { display: flex; flex-direction: column; min-width: 0; height: 100vh; }
#chat-head {
  display: flex; align-items: center; gap: var(--sp-2); padding: var(--sp-2) var(--sp-4);
  border-bottom: 1px solid var(--border); background: var(--bg2);
}
#burger { display: none; }
#chat-title { flex: 1; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.head-actions { display: flex; gap: var(--sp-2); flex-shrink: 0; }

#messages { flex: 1; overflow-y: auto; padding: var(--sp-5) var(--sp-4) var(--sp-2); scroll-behavior: smooth; }
.msg { max-width: 800px; margin: 0 auto var(--sp-4); display: flex; flex-direction: column; }
.msg.user { align-items: flex-end; }
.msg .bubble {
  max-width: 85%; padding: var(--sp-2) var(--sp-3); border-radius: 12px; position: relative;
  overflow-wrap: break-word;
}
.msg.user .bubble { background: var(--user-bubble); border-bottom-right-radius: 4px; }
.msg.assistant .bubble { background: var(--assistant-bubble); border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.msg .meta { font-size: var(--fs-xs); color: var(--muted); margin: var(--sp-1) var(--sp-2) 0; display: flex; gap: var(--sp-2); }
/* автор сообщения в расшаренном чате выделяется цветом акцента */
.msg .meta .author { color: var(--accent); }

.msg .pending { color: var(--muted); font-size: var(--fs-sm); padding: 2px 0; }
.msg .acts { display: flex; gap: 2px; opacity: 0; transition: opacity .15s; }
.msg:hover .acts { opacity: 1; }
.msg .acts .icon-btn { font-size: var(--fs-sm); padding: 2px var(--sp-1); }
.msg .cursor {
  display: inline-block; width: 8px; height: 16px; background: var(--accent);
  vertical-align: text-bottom; animation: blink 1s steps(1) infinite; border-radius: 2px;
}
@keyframes blink { 50% { opacity: 0; } }

/* Markdown внутри сообщений */
.bubble p { margin: 0 0 var(--sp-2); } .bubble p:last-child { margin-bottom: 0; }
.bubble pre {
  background: var(--code-bg); color: #e8eaf0; padding: var(--sp-3); border-radius: 8px;
  overflow-x: auto; font-size: var(--fs-sm); margin: var(--sp-2) 0;
}
.bubble code { font-family: ui-monospace, Consolas, monospace; font-size: var(--fs-sm); }
.bubble :not(pre) > code { background: var(--code-bg); padding: 1px var(--sp-1); border-radius: 4px; }
.bubble table { border-collapse: collapse; margin: var(--sp-2) 0; }
.bubble th, .bubble td { border: 1px solid var(--border); padding: var(--sp-1) var(--sp-2); }
.bubble blockquote { border-left: 3px solid var(--accent); margin: var(--sp-2) 0; padding: 2px var(--sp-3); color: var(--muted); }
.bubble img { max-width: 100%; border-radius: 8px; }
.bubble a { color: var(--accent); }
.att-chip {
  display: inline-flex; align-items: center; gap: var(--sp-2); background: var(--bg3);
  border: 1px solid var(--border); border-radius: 8px; padding: var(--sp-1) var(--sp-2);
  font-size: var(--fs-sm); margin: var(--sp-1) var(--sp-1) 0 0; max-width: 260px;
}
.att-chip .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.att-chip a { color: inherit; text-decoration: none; }
.msg .stats { font-size: var(--fs-xs); color: var(--muted); margin-top: var(--sp-2); }

/* блок размышлений reasoning-моделей: сворачиваемая серая вставка над ответом */
.think-toggle { background: none; border: none; color: var(--muted); font-size: var(--fs-xs); cursor: pointer; padding: 2px 0 var(--sp-1); }
.think-toggle:hover { color: var(--fg); }
.think-body { color: var(--muted); font-size: var(--fs-sm); white-space: pre-wrap; word-break: break-word;
  border-left: 2px solid var(--border); padding-left: var(--sp-2); margin: 2px 0 var(--sp-2); max-height: 220px; overflow-y: auto; }

/* Композер */
#composer { border-top: 1px solid var(--border); background: var(--bg2); padding: var(--sp-2) var(--sp-4) var(--sp-3); }
#ctx-bar {
  height: 4px; background: var(--bg3); border-radius: 2px; position: relative;
  margin: 0 auto var(--sp-2); max-width: 800px; overflow: hidden;
}
#ctx-fill { height: 100%; background: var(--accent); width: 0; transition: width .3s; }
#ctx-fill.warn { background: #e0a03c; } #ctx-fill.over { background: var(--danger); }
#ctx-label {
  position: absolute; right: 0; top: calc(-1 * var(--sp-4)); font-size: var(--fs-xs); color: var(--muted);
}
#attachments { max-width: 800px; margin: 0 auto; }
#attachments:empty { display: none; }
#attachments .att-chip .x { cursor: pointer; color: var(--muted); }
#attachments .att-chip .x:hover { color: var(--danger); }
.input-row { display: flex; gap: var(--sp-2); max-width: 800px; margin: 0 auto; align-items: flex-end; }
#input {
  flex: 1; resize: none; background: var(--bg); color: var(--fg);
  border: 1px solid var(--border); border-radius: 10px; padding: var(--sp-2) var(--sp-3);
  max-height: 200px; font: inherit;
}
#input:focus { outline: none; border-color: var(--accent); }
#btn-send { height: 42px; min-width: 52px; }
#btn-send.stop { background: var(--danger); }

/* Модалки */
#modal-root {
  position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 50;
  display: flex; align-items: center; justify-content: center; padding: var(--sp-5);
}
.modal {
  background: var(--bg2); border: 1px solid var(--border); border-radius: 14px;
  width: 100%; max-width: 560px; max-height: 85vh; display: flex; flex-direction: column;
}
.modal.wide { max-width: 720px; }
.modal-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: var(--sp-3) var(--sp-4); border-bottom: 1px solid var(--border); font-weight: 600;
}
.modal-body { padding: var(--sp-3) var(--sp-4); overflow-y: auto; }
.modal textarea, .modal input[type="text"], .modal input[type="number"] {
  width: 100%; background: var(--bg); color: var(--fg);
  border: 1px solid var(--border); border-radius: 8px; padding: var(--sp-2) var(--sp-2); font: inherit;
}
.modal .field { margin-bottom: var(--sp-3); }
.modal .field label { display: block; font-size: var(--fs-sm); color: var(--muted); margin-bottom: var(--sp-1); }
.modal .row { display: flex; gap: var(--sp-2); }
.modal .row .field { flex: 1; }
.modal-foot { padding: var(--sp-3) var(--sp-4); border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: var(--sp-2); }

/* Слои */
.layer { border: 1px solid var(--border); border-radius: 10px; margin-bottom: var(--sp-3); }
.layer-head { display: flex; align-items: center; gap: var(--sp-2); padding: var(--sp-2) var(--sp-3); }
.layer-head .name { flex: 1; font-weight: 600; }
.layer-entries { padding: 0 var(--sp-3) var(--sp-2); }
.layer-entry {
  background: var(--bg); border: 1px solid var(--border); border-radius: 8px;
  padding: var(--sp-2) var(--sp-2); margin-bottom: var(--sp-2); font-size: var(--fs-md); white-space: pre-wrap;
  display: flex; justify-content: space-between; gap: var(--sp-2);
}
.layer-entry .entry-text { flex: 1; }
.layer-entry .icon-btn { font-size: var(--fs-xs); align-self: flex-start; }
.layer-add { display: flex; gap: var(--sp-2); padding: 0 var(--sp-3) var(--sp-3); }
.layer-add textarea { flex: 1; min-height: 40px; }
.layer-new { display: flex; gap: var(--sp-2); margin-top: var(--sp-2); }

/* Таблицы (сессии, админ) */
table.list { width: 100%; border-collapse: collapse; font-size: var(--fs-sm); }
table.list th, table.list td { text-align: left; padding: var(--sp-2) var(--sp-2); border-bottom: 1px solid var(--border); vertical-align: top; }
table.list .badge { font-size: var(--fs-xs); background: var(--bg3); border-radius: 4px; padding: 1px var(--sp-2); }
table.list .badge.blocked { background: var(--danger); color: #fff; }
table.list .ua { color: var(--muted); max-width: 220px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
/* заголовок раздела внутри модалки (блоки «Приглашения»/«Пользователи» админки) */
.modal-h { font-size: var(--fs-sm); font-weight: 600; margin: var(--sp-3) 0 var(--sp-1); }
/* код приглашения в админке: клик копирует в буфер */
.inv-code { font-family: ui-monospace, Consolas, monospace; letter-spacing: 1px; cursor: pointer; }

/* Участники расшаренного чата */
.share-row {
  display: flex; justify-content: space-between; align-items: center; gap: var(--sp-3);
  padding: var(--sp-2) 0; border-bottom: 1px solid var(--border); font-size: var(--fs-sm);
}
.share-add { display: flex; gap: var(--sp-2); }
.share-add select { flex: 1; }

/* Тосты */
#toasts { position: fixed; bottom: var(--sp-5); right: var(--sp-5); z-index: 100; display: flex; flex-direction: column; gap: var(--sp-2); }
.toast {
  background: var(--bg3); border: 1px solid var(--border);
  border-radius: 8px; padding: var(--sp-2) var(--sp-4); max-width: 360px; box-shadow: 0 4px 16px rgba(0,0,0,.3);
  animation: slidein .2s ease;
}
.toast.err { border-left: 3px solid var(--danger); }
.toast.info { border-left-color: var(--accent); }
@keyframes slidein { from { transform: translateX(30px); opacity: 0; } }

/* Мобильная версия */
@media (max-width: 800px) {
  #app { grid-template-columns: 1fr; }
  #sidebar {
    position: fixed; left: 0; top: 0; bottom: 0; width: 290px; z-index: 40;
    transform: translateX(-100%); transition: transform .2s ease;
  }
  #sidebar.open { transform: none; box-shadow: 0 0 40px rgba(0,0,0,.5); }
  #burger { display: block; }
  .head-actions .btn span { display: none; }
  .msg .bubble { max-width: 100%; }
}

/* запись голоса: пульсирующая кнопка-микрофон */
#btn-voice.recording {
  color: #e74c3c;
  animation: voice-pulse 1.2s ease-in-out infinite;
}

@keyframes voice-pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.45; }
}
