/* ==========================================================
   AI · Главная страница — разговор с моделями прямо в браузере
   ========================================================== */

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #f2f2f7; --surface: #ffffff; --surface2: #fafafa;
  --text: #1c1c1e; --text2: #48484a; --dim: #8e8e93;
  --border: #e5e5ea;
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.08), 0 4px 16px rgba(0,0,0,0.04);
  --accent: #007aff; --green: #34c759; --red: #ff3b30; --orange: #ff9500;
  --radius: 14px; --radius-sm: 8px;
  --bar-h: 56px;
}

html, body { height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Display', 'Segoe UI', Roboto, sans-serif;
  font-size: 16px; line-height: 1.6; color: var(--text); background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

/* Страница занимает высоту окна: лента прокручивается, полосы стоят на месте */
#app, .app-page { height: 100%; }
.app-page { display: flex; flex-direction: column; }

/* ── Верхняя полоса ─────────────────────────────────────── */
.app-bar {
  flex: 0 0 auto; height: var(--bar-h);
  display: flex; align-items: center; gap: 16px; padding: 0 20px;
  background: rgba(255,255,255,0.9);
  backdrop-filter: blur(20px); -webkit-backdrop-filter: blur(20px);
  border-bottom: 1px solid var(--border);
}

.app-brand {
  display: flex; align-items: center; gap: 9px;
  text-decoration: none; color: var(--text); flex: 0 0 auto;
}
.app-brand-icon {
  width: 30px; height: 30px; border-radius: 8px;
  background: linear-gradient(135deg, #007aff, #5ac8fa);
  display: flex; align-items: center; justify-content: center;
  color: #fff; font-size: 16px;
}
.app-brand-name { font-size: 17px; font-weight: 700; letter-spacing: .2px; }
.app-brand:hover .app-brand-icon { filter: brightness(1.08); }

/* Поле ключа посередине полосы */
.token-field {
  flex: 1 1 auto; max-width: 520px; margin: 0 auto;
  display: flex; align-items: center; gap: 8px;
  padding: 5px 12px; border: 1px solid var(--border); border-radius: 999px;
  background: var(--surface2); transition: border-color .15s, background .15s;
}
.token-field:focus-within { border-color: var(--accent); background: var(--surface); }
.token-icon { color: var(--dim); font-size: 15px; flex: 0 0 auto; }
.token-input {
  flex: 1 1 auto; min-width: 80px;
  border: 0; outline: 0; background: none;
  font-family: inherit; font-size: 13px; color: var(--text);
}
.token-eye {
  border: 0; background: none; cursor: pointer; color: var(--dim);
  display: flex; align-items: center; padding: 2px; font-size: 14px;
}
.token-eye:hover { color: var(--text); }
/* Подпись состояния не растягивает полосу: длинный текст отказа обрезается,
   целиком он виден при наведении. Иначе кнопка «Админка» уезжала за экран. */
.token-status {
  font-size: 12px; color: var(--dim); flex: 0 1 auto;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 190px;
}
.token-field.is-ok   .token-status { color: var(--green); }
.token-field.is-bad  .token-status { color: var(--red); }
.token-field.is-bad  { border-color: var(--red); }
.token-field.is-wait .token-status { color: var(--orange); }

.app-admin {
  flex: 0 0 auto;
  display: flex; align-items: center; gap: 6px;
  padding: 6px 12px; border: 1px solid var(--border); border-radius: 8px;
  font-size: 13px; font-weight: 500; color: var(--text2);
  text-decoration: none; background: var(--surface);
  transition: color .15s, border-color .15s;
}
.app-admin:hover { color: var(--accent); border-color: var(--accent); }

/* ── Середина: модель, лента, ввод ──────────────────────── */
.app-main {
  flex: 1 1 auto; min-height: 0;
  width: 100%; max-width: 860px; margin: 0 auto; padding: 0 20px;
  display: flex; flex-direction: column;
}

.model-bar {
  flex: 0 0 auto; display: flex; align-items: center; gap: 10px;
  padding: 12px 0 8px;
}
.model-select {
  border: 1px solid var(--border); border-radius: 8px;
  padding: 6px 10px; font-family: inherit; font-size: 13px;
  color: var(--text); background: var(--surface); max-width: 380px;
}
.model-select:focus { outline: 0; border-color: var(--accent); }
.model-price { font-size: 12px; color: var(--dim); }

/* ── Лента разговора ────────────────────────────────────── */
.chat-area {
  flex: 1 1 auto; min-height: 0; overflow-y: auto;
  display: flex; flex-direction: column; gap: 14px;
  padding: 8px 0 16px;
  scroll-behavior: smooth;   /* лента едет плавно, а не прыгает на каждой части ответа */
}
/* Уважаем настройку системы «меньше движения» */
@media (prefers-reduced-motion: reduce) {
  .chat-area { scroll-behavior: auto; }
}
.chat-greeting {
  margin: auto; text-align: center; color: var(--dim); font-size: 15px;
  max-width: 420px; padding: 40px 0;
}

.msg { display: flex; }
.msg-user  { justify-content: flex-end; }
.msg-model { justify-content: flex-start; }

.msg-bubble {
  max-width: 82%;
  padding: 11px 15px; border-radius: var(--radius);
  box-shadow: var(--shadow-sm); font-size: 15px;
}
.msg-user  .msg-bubble { background: var(--accent); color: #fff; border-bottom-right-radius: 5px; }
.msg-model .msg-bubble { background: var(--surface); border-bottom-left-radius: 5px; }
.msg.is-error .msg-bubble { background: #fff2f1; color: var(--red); }

.msg-text { word-break: break-word; }
/* Свой вопрос показывается как написан, с переносами строк */
.msg-user .msg-text { white-space: pre-wrap; }

/* ── Разметка ответа модели ─────────────────────────────── */
.msg-text > *:first-child { margin-top: 0; }
.msg-text > *:last-child  { margin-bottom: 0; }

.msg-text p { margin: 0 0 10px; }
.msg-text h1, .msg-text h2, .msg-text h3,
.msg-text h4, .msg-text h5, .msg-text h6 {
  margin: 16px 0 8px; line-height: 1.3; font-weight: 650;
}
.msg-text h1 { font-size: 20px; }
.msg-text h2 { font-size: 18px; }
.msg-text h3 { font-size: 16px; }
.msg-text h4, .msg-text h5, .msg-text h6 { font-size: 15px; }

.msg-text ul, .msg-text ol { margin: 0 0 10px; padding-left: 22px; }
.msg-text li { margin: 3px 0; }
.msg-text li > ul, .msg-text li > ol { margin-bottom: 0; }

.msg-text strong { font-weight: 650; }
.msg-text em     { font-style: italic; }
.msg-text s      { opacity: .6; }

.msg-text code {
  font-family: 'SF Mono', ui-monospace, Menlo, Consolas, monospace;
  font-size: .88em; padding: 1px 5px; border-radius: 5px;
  background: var(--bg); color: #b8004f;
}
.msg-text pre {
  margin: 0 0 10px; padding: 11px 13px; border-radius: var(--radius-sm);
  background: #1c1c1e; overflow-x: auto;
}
.msg-text pre code {
  background: none; padding: 0; color: #f2f2f7; font-size: 13px; line-height: 1.5;
}

.msg-text blockquote {
  margin: 0 0 10px; padding: 2px 0 2px 12px;
  border-left: 3px solid var(--border); color: var(--text2);
}
.msg-text hr { margin: 14px 0; border: 0; border-top: 1px solid var(--border); }
.msg-text a  { color: var(--accent); }

/* В своём пузыре фон синий — разметка на нём читается иначе */
.msg-user .msg-text code { background: rgba(255,255,255,.2); color: #fff; }
.msg-user .msg-text a    { color: #fff; text-decoration: underline; }

.msg.is-waiting .msg-text::after {
  content: '···'; color: var(--dim); letter-spacing: 2px;
  animation: blink 1.2s steps(1) infinite;
}
@keyframes blink { 0%, 100% { opacity: .3 } 50% { opacity: 1 } }

.msg-files { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.msg-file {
  font-size: 12px; padding: 2px 8px; border-radius: 6px;
  background: rgba(255,255,255,.22);
}
.msg-model .msg-file { background: var(--bg); color: var(--text2); }

.msg-image { display: block; margin-top: 8px; }
.msg-image img { max-width: 100%; border-radius: var(--radius-sm); display: block; }

.msg-foot { margin-top: 7px; font-size: 11px; color: var(--dim); }
.msg-user .msg-foot { color: rgba(255,255,255,.75); }
/* Ответ оборвался на середине: сказанное сохраняем, но помечаем. */
.msg.is-broken .msg-bubble { border-left: 3px solid var(--orange); }
.msg.is-broken .msg-foot   { color: var(--orange); }

.msg-actions { display: flex; gap: 4px; margin-top: 8px; }
.msg-act { padding: 3px 5px; border-radius: 6px; font-size: 15px; }
.common-button.msg-act:hover { color: var(--accent); background: var(--bg); }

/* ── Поле ввода ─────────────────────────────────────────── */
.composer {
  flex: 0 0 auto; padding: 10px 0 18px;
  border-radius: var(--radius);
}
/* Файл тащат в любое место страницы — подсвечиваем всю область разговора */
.composer.is-drop::after {
  content: 'Отпустите файл здесь';
  position: fixed; inset: var(--bar-h) 0 0 0; z-index: 500;
  display: flex; align-items: center; justify-content: center;
  background: rgba(0,122,255,.06); border: 2px dashed var(--accent);
  color: var(--accent); font-size: 15px; font-weight: 500;
  pointer-events: none;
}

.attach-bar { display: flex; flex-wrap: wrap; gap: 7px; padding-bottom: 9px; }
.chip {
  display: flex; align-items: center; gap: 7px;
  padding: 5px 9px; border: 1px solid var(--border); border-radius: 999px;
  background: var(--surface); font-size: 12px; max-width: 260px;
}
.chip-icon { color: var(--accent); font-size: 14px; flex: 0 0 auto; }
.chip-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip-size { color: var(--dim); flex: 0 0 auto; }
.chip-remove {
  border: 0; background: none; cursor: pointer; color: var(--dim);
  display: flex; padding: 0; font-size: 13px; flex: 0 0 auto;
}
.chip-remove:hover { color: var(--red); }

.composer-row {
  display: flex; align-items: flex-end; gap: 6px;
  padding: 7px 8px; background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
}
.composer-row:focus-within { border-color: var(--accent); }

.composer-field { flex: 1 1 auto; min-width: 0; }
.composer-input {
  width: 100%; border: 0; outline: 0; background: none; resize: none;
  font-family: inherit; font-size: 15px; line-height: 1.5; color: var(--text);
  max-height: 220px; padding: 6px 2px;
}

/* Вид кнопки-значка задан один раз на классе самого виджета (CommonButton
   ставит `is-icon` в setIcon). Места употребления задают только размер и цвет,
   а не повторяют рамку и раскладку заново. */
.common-button.is-icon {
  flex: 0 0 auto; border: 0; cursor: pointer; background: none;
  color: var(--dim); font-family: inherit;
  display: flex; align-items: center; justify-content: center;
}
.common-button.is-loading { opacity: .55; cursor: default; }

/* Специфичность равна .common-button.is-icon: иначе общий вид кнопки-значка
   (серая, без фона) перебивал бы цвет главной кнопки, и «Отправить» теряла
   синий вид. Правило класса: наслаиваешь поверх общего — держи ту же
   специфичность или выше. */
.composer-act, .composer-send { width: 34px; height: 34px; border-radius: 9px; font-size: 17px; }
.common-button.composer-act:hover { color: var(--accent); background: var(--bg); }
.common-button.composer-act.is-recording { color: #fff; background: var(--red); }

.common-button.composer-send { background: var(--accent); color: #fff; }
.common-button.composer-send:hover { filter: brightness(1.06); }

.composer-hint { min-height: 18px; padding: 5px 4px 0; font-size: 12px; color: var(--dim); }

/* ── Всплывающее сообщение ──────────────────────────────── */
.toast {
  position: fixed; bottom: 28px; left: 50%; transform: translateX(-50%) translateY(20px);
  background: #1c1c1e; color: #fff; padding: 12px 20px; border-radius: 12px;
  font-size: 14px; font-weight: 500; z-index: 2000;
  opacity: 0; transition: all .3s; pointer-events: none;
  display: flex; align-items: center; gap: 8px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.3);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.success { background: #1a7431; }
.toast.error   { background: #c0392b; }

/* ── Мелкие экраны ──────────────────────────────────────── */
@media (max-width: 640px) {
  .app-bar { gap: 10px; padding: 0 12px; }
  .app-brand-name { display: none; }
  .app-admin span { display: none; }
  .app-main { padding: 0 12px; }
  .msg-bubble { max-width: 92%; }
  /* Цена в две строки поднимала всю полосу выбора: на узком экране прячем,
     а после выбора модели она всё равно видна на широком и в админке. */
  .model-price { display: none; }
  .model-select { max-width: none; flex: 1 1 auto; }
}

/* ── Выбор модели: карточки вместо строки ───────────────── */
.model-menu { position: relative; flex: 0 0 auto; }

.model-menu-button {
  display: flex; align-items: center; gap: 6px;
  padding: 6px 10px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); cursor: pointer;
  font-family: inherit; font-size: 13px; color: var(--text);
}
.model-menu-button:hover { border-color: var(--accent); }
.model-menu.is-open .model-menu-button { border-color: var(--accent); }
.model-menu-arrow { color: var(--dim); font-size: 14px; }

.model-menu-drop {
  position: absolute; top: calc(100% + 6px); left: 0; z-index: 400;
  min-width: 340px; max-width: 420px; max-height: 60vh; overflow-y: auto;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: 0 8px 28px rgba(0,0,0,.12);
  padding: 6px;
}

.model-menu-group {
  padding: 8px 10px 4px; font-size: 11px; font-weight: 600;
  color: var(--dim); text-transform: uppercase; letter-spacing: .4px;
}

.model-option {
  padding: 8px 10px; border-radius: var(--radius-sm); cursor: pointer;
}
.model-option:hover { background: var(--bg); }
.model-option.is-current { background: rgba(0,122,255,.08); }

.model-option-main { display: flex; align-items: center; gap: 7px; }
.model-option-name { font-size: 14px; font-weight: 550; }
.model-option-tag {
  font-size: 10px; font-weight: 600; padding: 1px 6px; border-radius: 20px;
  background: #e8f8ee; color: #1a7431; text-transform: uppercase; letter-spacing: .3px;
}
.model-option-sub {
  display: flex; justify-content: space-between; gap: 10px; margin-top: 2px;
  font-size: 12px; color: var(--dim);
}
.model-option-price { white-space: nowrap; }

@media (max-width: 640px) {
  .model-menu-drop { min-width: 0; width: calc(100vw - 24px); max-width: none; }
}

/* Режим рисования включён */
.common-button.composer-act.is-on { color: #fff; background: var(--accent); }
