/* ===========================================================================
   Раскладка приложения.

   Главная правка мобильной версии. Раньше композер был position:absolute,
   а место под него освобождалось фиксированным padding-bottom:180px у .chat.
   Любое изменение высоты композера (перенос кнопок на второй ряд на узком
   экране, полоса вложений, многострочный ввод) ломало это соответствие,
   и композер наезжал на последнее сообщение.

   Теперь .workspace — grid из трёх строк: шапка / лента / композер.
   Лента получает minmax(0,1fr) и скроллится внутри себя, композер занимает
   ровно свою высоту. Перекрытие невозможно by construction, а высоту
   композера больше никому не нужно измерять.
   =========================================================================== */

.app {
  height: var(--app-h);
  display: flex;
  overflow: hidden;
}

.workspace {
  flex: 1;
  min-width: 0;
  height: 100%;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  /* minmax(0,1fr), а не auto: неявная колонка grid растягивается до
     max-content, и композер со своим рядом контролов задавал ширину всей
     колонки — на экране 360px раскладка разъезжалась до 409px и кнопка
     отправки уходила за правый край. */
  grid-template-columns: minmax(0, 1fr);
}

/* ---------- шапка ---------- */
.header {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 78px;
  padding: 14px 18px;
  padding-top: calc(14px + var(--safe-t));
}

.header-title {
  min-width: 0;
  max-width: calc(100% - 140px);
  text-align: center;
  pointer-events: none;
}

.header-title > b {
  display: block;
  font-size: 15px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Часы работы под названием чата. Раньше эта строка была спрятана совсем, и
   «Thinking 12s» никто не видел — а это единственное место, где показано, что
   агент жив и сколько уже думает. Прячем только когда сказать нечего. */
.header-title > span {
  display: block;
  margin-top: 1px;
  font: 10px var(--mono);
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.header-title > span:empty { display: none; }

#activeTitle { display: inline-block; will-change: opacity, transform, filter; }
#activeTitle.title-swap { animation: pico-title-swap .38s cubic-bezier(.22, .8, .22, 1) both; }

@keyframes pico-title-swap {
  0%   { opacity: 0; transform: translateY(5px) scale(.97); filter: blur(3px); }
  65%  { opacity: 1; transform: translateY(-1px) scale(1.01); filter: blur(0); }
  100% { opacity: 1; transform: translateY(0) scale(1); filter: blur(0); }
}

.icon-button {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--text-2);
  font-size: 21px;
}

.icon-button:hover { background: var(--surface-2); }

.glass-menu {
  position: absolute;
  left: 18px;
  top: calc(14px + var(--safe-t));
  width: 48px;
  height: 48px;
  border: 0;
  border-radius: 50%;
  overflow: hidden;
  background: var(--glass-fill);
  box-shadow: 0 8px 28px rgba(80, 70, 55, .1);
  transition: transform .16s ease, opacity .16s ease, box-shadow .16s ease;
}

.glass-menu .liquidGlass-effect,
.glass-menu .liquidGlass-tint,
.glass-menu .liquidGlass-shine { border-radius: 50%; }

.glass-menu.is-pressed {
  transform: scale(.88);
  box-shadow: 0 0 0 5px var(--accent-glow), 0 8px 28px rgba(80, 70, 55, .1);
}

.glass-menu.menu-hidden { opacity: 0; pointer-events: none; transform: scale(.76); }

.hamb { position: relative; z-index: 3; display: grid; gap: 4px; width: 18px; }
.hamb i { height: 1.8px; border-radius: 4px; background: var(--text-2); }

.new-session-top {
  position: absolute;
  right: 18px;
  top: calc(14px + var(--safe-t));
  display: grid;
  place-items: center;
  width: 48px;
  height: 48px;
  border: 1px solid var(--glass-edge);
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  box-shadow: 0 8px 24px var(--accent-glow);
  transition: transform .16s ease, background .16s ease;
}

.new-session-top svg { width: 20px; height: 20px; fill: none; stroke: #fff; stroke-width: 2.2; stroke-linecap: round; }
.new-session-top:hover { background: var(--accent-hover); transform: scale(1.04); }
.new-session-top:active { transform: scale(.92); }

/* ---------- лента сообщений ---------- */
.chat {
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  display: flex;
  flex-direction: column;
  padding: 0 max(16px, calc((100% - 760px) / 2));
}

.chat.has-messages { padding-top: 20px; }
.chat:not(.has-messages) { overflow: hidden; }

.empty-state {
  flex: 1 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 28px 0;
}

.agent-orb { display: grid; place-items: center; width: 112px; height: 112px; }

.agent-orb img {
  width: 104px;
  height: 104px;
  object-fit: contain;
  filter: drop-shadow(0 16px 24px rgba(56, 104, 216, .12));
}

.empty-state h1 {
  margin: 17px 0 8px;
  font: 700 16px/1.4 var(--mono);
  letter-spacing: -.035em;
}

.messages {
  display: grid;
  align-content: start;
  grid-auto-rows: max-content;
  gap: 14px;
  padding-bottom: 8px;
}

/* ---------- композер ----------
   В потоке: третья строка grid у .workspace. */
.composer-zone {
  min-width: 0;
  padding: 10px max(12px, calc((100% - 760px) / 2)) var(--dock-gap);
  background: linear-gradient(transparent 0, var(--bg) 32%);
}

.composer {
  min-width: 0;
  border-radius: var(--radius-md);
  box-shadow: 0 18px 55px rgba(70, 62, 50, .14);
}

/* ---------- планка подсказки ----------
   Узкая строка прямо над доком, видна только пока агент работает. Нарочно
   ниже ростом и тише по цвету, чем композер: это не второй ввод, а реплика
   в сторону — «обрати внимание». */
.steer {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 6px;
  margin-bottom: 6px;
  padding: 4px 4px 4px 12px;
  border: 1px solid var(--accent-soft);
  border-radius: 999px;
  background: var(--surface-2);
}

.steer input {
  min-width: 0;
  height: 34px;
  border: 0;
  outline: 0;
  background: transparent;
  color: var(--text);
  /* 16px — иначе iOS зумит страницу при фокусе. */
  font-size: 16px;
}

.steer input::placeholder { color: var(--muted); }

.steer-send {
  flex: 0 0 auto;
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border: 0;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
}

.steer-send svg { width: 17px; height: 17px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.steer-send:disabled { opacity: .5; }

.composer-content {
  position: relative;
  z-index: 3;
  min-width: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 6px;
  padding: 9px 10px 8px;
}

.composer textarea {
  width: 100%;
  min-height: 42px;
  /* 16px обязателен: при меньшем размере iOS Safari зумит страницу при
     фокусе на поле и вёрстка «съезжает» — это была одна из жалоб. */
  font-size: 16px;
  line-height: 1.35;
  padding: 9px 6px 2px;
  border: 0;
  outline: 0;
  resize: none;
  background: transparent;
  color: var(--text);
  field-sizing: content;
  max-height: 40vh;
}

.composer textarea::placeholder { color: var(--muted); }

.composer-dollar { display: none; }

/* Один ряд без горизонтальной прокрутки: скрепка и кнопка отправки фиксированы,
   чип модели забирает остаток и усекается многоточием. На 320px тоже помещается. */
.composer-actions {
  display: flex;
  align-items: center;
  gap: 7px;
  min-height: 36px;
  min-width: 0;
}

.chip {
  display: flex;
  align-items: center;
  gap: 5px;
  height: 34px;
  min-width: 0;
  padding: 0 11px;
  border: 0;
  border-radius: 10px;
  background: var(--surface-2);
  color: var(--text-2);
  font: 11px var(--mono);
  transition: background .16s ease;
}

.chip:hover { background: var(--surface-3); }
.chip:focus-visible { outline: 0; box-shadow: 0 0 0 3px var(--accent-glow); }

.chip-label { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Ширина по содержимому, но сжимается, если места мало. Было flex:1 1 auto —
   на широком экране чип растягивался на всю свободную ширину и выглядел
   огромной кнопкой. */
.chip-model { flex: 0 1 auto; }

.chip-sub {
  flex: 0 0 auto;
  padding: 2px 5px;
  border-radius: 5px;
  background: var(--accent-soft);
  color: var(--accent-strong);
  font: 9px var(--mono);
}

/* Распорка: всё слева прижато к скрепке, кнопка отправки — к правому краю. */
.chip-mode { flex: 0 0 auto; margin-right: auto; }

.chip-dot {
  width: 6px;
  height: 6px;
  flex: 0 0 auto;
  border-radius: 50%;
  background: var(--muted);
}

/* Агент работает в песочнице — это состояние стоит показывать. */
.composer.agent-mode .chip-mode { background: var(--accent-soft); color: var(--accent-strong); }
.composer.agent-mode .chip-dot { background: var(--accent); }

.composer-button {
  width: 34px;
  height: 34px;
  flex: 0 0 auto;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--text-2);
  font-size: 21px;
}

.composer-button:hover { background: var(--surface-2); }

.send-button,
.stop-button {
  display: grid;
  place-items: center;
  width: 68px;
  min-width: 68px;
  height: 34px;
  border: 0;
  border-radius: 10px;
  background: var(--accent);
  color: #fff;
  box-shadow: 0 7px 18px var(--accent-glow);
  transition: transform .18s ease, background .18s ease, box-shadow .18s ease;
}

.send-button:hover,
.stop-button:hover { background: var(--accent-hover); transform: scale(1.04); }
.send-button:active { transform: scale(.92); }
.send-button svg { width: 18px; height: 18px; fill: none; stroke: #fff; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

.send-button.is-running svg { display: none; }

.send-button.is-running::before {
  content: "";
  display: block;
  width: 10px;
  height: 10px;
  border-radius: 2px;
  background: #fff;
  animation: stop-dot-pulse 1s ease-in-out infinite;
}

@keyframes stop-dot-pulse {
  0%, 100% { transform: scale(.68); opacity: .72; }
  50%      { transform: scale(1); opacity: 1; }
}

.composer-meta { display: none; }

/* ---------- боковая панель ---------- */
.drawer {
  position: fixed;
  z-index: 10;
  inset: 12px auto calc(var(--dock-gap) + 12px) 12px;
  width: min(304px, calc(100vw - 24px));
  border: 0;
  border-radius: var(--radius-lg);
  box-shadow: 0 18px 55px rgba(70, 62, 50, .14);
  transform: translateX(calc(-100% - 24px));
  transition: transform .22s ease-out;
  will-change: transform;
}

.drawer.open { transform: none; }

.drawer .liquidGlass-effect,
.drawer .liquidGlass-tint,
.drawer .liquidGlass-shine { border-radius: var(--radius-lg); }

.drawer-content {
  position: relative;
  z-index: 3;
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: 18px;
  overflow-y: auto;
  touch-action: pan-y;
}

.drawer-top { display: flex; align-items: center; justify-content: space-between; min-height: 40px; }
.brand { display: flex; align-items: center; gap: 10px; }
.brand img { width: 34px; height: 34px; }
.brand b { font: 10px var(--mono); letter-spacing: .03em; }
.drawer-close { font-size: 23px; touch-action: manipulation; }

.new-chat {
  display: flex;
  align-items: center;
  gap: 9px;
  height: 42px;
  margin-top: 20px;
  padding: 0 11px;
  border: 1px solid var(--glass-edge);
  border-radius: var(--radius-sm);
  background: var(--glass-fill);
  font-size: 12px;
}

.new-chat span { font-size: 18px; }
.new-chat kbd {
  margin-left: auto;
  padding: 2px 5px;
  border: 1px solid var(--line);
  border-radius: 5px;
  font: 9px var(--sans);
  color: var(--muted);
}

.drawer-section { margin-top: 22px; min-height: 0; }

.section-caption {
  padding: 0 8px 7px;
  font-size: 9px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: .11em;
}

.chat-list { display: grid; gap: 3px; }

.chat-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 27px;
  align-items: center;
  gap: 3px;
}

.chat-item {
  min-width: 0;
  width: 100%;
  padding: 9px;
  border: 0;
  border-radius: 13px;
  background: transparent;
  color: var(--muted);
  text-align: left;
  font-size: 11px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-item:hover,
.chat-item.active { background: var(--glass-fill-strong); color: var(--text); }

/* Отметка «здесь идёт работа»: агент продолжает работать и в закрытом чате,
   и это должно быть видно, не открывая его. */
.chat-busy {
  display: inline-block;
  width: 6px;
  height: 6px;
  margin-right: 6px;
  border-radius: 50%;
  background: var(--accent);
  animation: pico-pulse 1.6s ease-in-out infinite;
}

.chat-item.is-busy { color: var(--text); }

.chat-delete {
  width: 27px;
  height: 27px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  color: var(--muted);
  font: 16px/1 var(--sans);
  opacity: .35;
  transition: opacity .16s ease, background .16s ease, color .16s ease;
}

.chat-delete:hover,
.chat-delete:focus-visible { opacity: 1; background: var(--glass-fill-strong); color: var(--red); outline: 0; }

.drawer-bottom { margin-top: auto; display: grid; gap: 4px; padding-top: 16px; }

.drawer-action {
  display: grid;
  grid-template-columns: 30px 1fr auto;
  align-items: center;
  gap: 9px;
  width: 100%;
  padding: 9px 7px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  text-align: left;
}

.drawer-action:hover { background: var(--glass-fill-strong); }
.drawer-action b, .drawer-action small, .account b, .account small { display: block; }
.drawer-action b { font-size: 11px; }
.drawer-action small { font-size: 9px; color: var(--muted); }
.drawer-action i { color: var(--muted); font-style: normal; }

.action-icon {
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border-radius: 8px;
  background: var(--surface-3);
  color: var(--accent);
}

.key-icon svg { width: 17px; height: 17px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

.account {
  display: flex;
  align-items: center;
  gap: 9px;
  margin-top: 7px;
  padding: 12px 7px 3px;
  border-top: 1px solid var(--line-soft);
}

.account-avatar {
  display: grid;
  place-items: center;
  width: 29px;
  height: 29px;
  border-radius: 50%;
  background: var(--surface-3);
  color: var(--text-2);
  font-size: 11px;
}

.account b { font-size: 10px; }
.account small { font-size: 9px; color: var(--muted); }

.scrim {
  position: fixed;
  z-index: 9;
  inset: 0;
  background: transparent;
  opacity: 0;
  pointer-events: none;
  touch-action: none;
  transition: opacity .2s;
}

.scrim.show { opacity: 1; pointer-events: auto; }

/* ---------- десктоп ----------
   Панель становится постоянной колонкой, а не наложением. Раньше отступ под
   неё задавался жёстким left:310px у absolute-композера при ширине панели
   304px + 12px отступа — числа не сходились. Теперь это обычный flex-поток. */
@media (min-width: 900px) {
  .drawer {
    position: relative;
    inset: auto;
    flex: 0 0 304px;
    margin: 12px 0 12px 12px;
    transform: none;
  }

  .drawer-close,
  .glass-menu,
  .scrim { display: none !important; }
}

/* ---------- узкие экраны ---------- */
@media (max-width: 620px) {
  .header { height: 70px; }
  .glass-menu, .new-session-top { top: calc(11px + var(--safe-t)); }
  .glass-menu { left: 14px; }
  .new-session-top { right: 14px; }
  .header-title { max-width: calc(100% - 130px); }
  .composer-zone { padding-left: 12px; padding-right: 12px; }
}

@media (max-width: 380px) {
  .composer-actions { gap: 6px; }
  .chip { padding: 0 9px; }
  .send-button, .stop-button { width: 52px; min-width: 52px; }
}
