/* ──────────────────────────────────────────────────────────────
   Полотно чата: шапка, сообщения, пузыри, свайпы, действия.
   ────────────────────────────────────────────────────────────── */

/* ── шапка чата ──────────────────────────────────────────────── */
.chat-head {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 58px;
  padding: 8px 10px 8px 12px;
  padding-top: calc(8px + var(--safe-t));
  background: var(--glass);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--line);
}

.chat-head__back { display: none; }

.chat-head__center {
  display: flex;
  align-items: center;
  gap: 11px;
  flex: 0 1 auto;
  max-width: 100%;
  min-width: 0;
  margin-right: auto;
  padding: 4px 6px;
  margin: -4px -6px;
  border-radius: var(--r-md);
  text-align: left;
  transition: background var(--dur) var(--ease);
}
.chat-head__center:hover { background: var(--panel-2); }

.chat-head__avatar {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--panel-3);
  border: 1px solid var(--line);
  color: var(--text);
  flex: none;
}
.chat-head__avatar svg { width: 18px; height: auto; }

.chat-head__text { min-width: 0; display: flex; flex-direction: column; }
.chat-head__title {
  font-size: 1.02em;
  font-weight: 700;
  letter-spacing: -.01em;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.chat-head__sub {
  font-size: .79em;
  color: var(--text-3);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.chat-head__sub[data-state="typing"] { color: var(--accent); }

.chat-head__actions { display: flex; align-items: center; gap: 2px; flex: none; }

/* ── лента сообщений ─────────────────────────────────────────── */
.messages-wrap {
  position: relative;
  display: flex;
  min-height: 0;
  overflow: hidden;
}

.messages {
  flex: 1;
  min-width: 0;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  overscroll-behavior-y: contain;
  -webkit-overflow-scrolling: touch;
  padding: 18px 0 8px;
  scroll-behavior: smooth;
}
.messages__inner {
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  gap: 3px;
  width: 100%;
  min-height: 100%;
  max-width: var(--chat-max);
  margin: 0 auto;
  padding: 8px clamp(12px, 3vw, 28px);
}

.messages--empty { display: flex; align-items: center; justify-content: center; }

/* разделитель дня */
.day {
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 16px 0 12px;
}
.day span {
  padding: 3px 12px;
  border-radius: var(--r-pill);
  background: var(--panel-2);
  border: 1px solid var(--line);
  color: var(--text-3);
  font-size: .76em;
  font-weight: 600;
  backdrop-filter: blur(6px);
}

/* ── сообщение ───────────────────────────────────────────────── */
.msg {
  position: relative;
  display: flex;
  flex-direction: column;
  max-width: min(78%, 560px);
  align-self: flex-start;
  margin-top: 10px;
}
.msg--grouped { margin-top: 3px; }
.msg--grouped .msg__name { display: none; }
.msg--out { align-self: flex-end; align-items: flex-end; }

/* появление — только у нового сообщения и только один раз */
.msg--new { animation: msg-in 200ms var(--ease-out) both; }
@keyframes msg-in {
  from { opacity: 0; transform: translateY(7px); }
  to { opacity: 1; transform: none; }
}
@media (prefers-reduced-motion: reduce) {
  .msg--new { animation: none; }
}

.msg__name {
  padding: 0 4px 3px 11px;
  font-size: .78em;
  font-weight: 700;
  letter-spacing: -.005em;
  color: var(--name-bot);
  -webkit-user-select: none;
  user-select: none;
}
.msg--out .msg__name {
  padding: 0 11px 3px 4px;
  color: var(--name-user);
}

/* обёртка, которая едет при свайпе: пузырь + время под ним */
.msg__swipe {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  max-width: 100%;
  will-change: transform;
  touch-action: pan-y;
}
.msg--out .msg__swipe { align-items: flex-end; }
.msg__swipe--settle { transition: transform var(--dur-med) var(--ease-out); }

/* значок действия: лежит под пузырём у того края, который открывается */
.msg__hint {
  position: absolute;
  top: 50%;
  right: 10px;
  z-index: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  margin-top: -15px;
  border-radius: 50%;
  color: var(--accent);
  background: var(--accent-dim);
  opacity: 0;
  transform: scale(.5);
  transition: opacity 120ms var(--ease), transform 120ms var(--ease);
  pointer-events: none;
}
.msg__hint--right { right: auto; left: 10px; }
.msg__hint svg { width: 16px; height: 16px; }
.msg--armed .msg__hint { opacity: 1; transform: scale(1); }

/* сам пузырь */
.bubble {
  position: relative;
  max-width: 100%;
  padding: 8px 12px;
  border-radius: 17px;
  background: var(--bubble-in);
  border: 1px solid var(--bubble-in-line);
  color: var(--bubble-in-text);
  box-shadow: var(--shadow-1);
  word-break: break-word;
  overflow-wrap: anywhere;
  -webkit-user-select: text;
  user-select: text;
}

.msg--in .bubble { border-top-left-radius: 6px; }
.msg--out .bubble {
  border-top-right-radius: 6px;
  background: var(--bubble-out);
  border-color: transparent;
  color: var(--bubble-out-text);
}

.bubble--deleted { opacity: .62; font-style: italic; }
.bubble--failed { border-color: var(--danger); }

.bubble__text {
  font-size: 1em;
  line-height: 1.45;
  letter-spacing: -.003em;
  white-space: pre-wrap;
}
.bubble__text > *:first-child { margin-top: 0; }
.bubble__text > *:last-child { margin-bottom: 0; }

/* время под пузырём: у Кая слева, у вас справа */
.bubble__meta {
  display: flex;
  align-items: center;
  gap: 4px;
  margin-top: 4px;
  padding: 0 10px 0 5px;
  font-size: .72em;
  line-height: 1.2;
  color: var(--text-3);
  -webkit-user-select: none;
  user-select: none;
  font-variant-numeric: tabular-nums;
}
.msg--in .bubble__meta { padding-left: 6px; padding-right: 10px; }
.msg--out .bubble__meta { padding-left: 10px; padding-right: 6px; }
.bubble__ticks { display: inline-flex; align-items: center; }
.bubble__ticks svg { width: 15px; height: 15px; }
.bubble__ticks--read { color: var(--accent); }

/* ошибка генерации */
.msg__error {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 5px;
  padding: 6px 11px;
  border-radius: var(--r-pill);
  background: var(--danger-dim);
  color: var(--danger);
  font-size: .82em;
}
.msg__error[hidden] { display: none; }

/* ── жесты ───────────────────────────────────────────────────── */
@media (hover: none) {
  .msg--swiping .bubble { filter: brightness(1.05); }
}


/* ── «печатает…» ─────────────────────────────────────────────── */
.typing {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 12px 14px;
  border-radius: 17px;
  border-top-left-radius: 6px;
  background: var(--bubble-in);
  border: 1px solid var(--bubble-in-line);
  box-shadow: var(--shadow-1);
  width: fit-content;
}
.typing i {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-3);
  animation: typing-dot 1.25s var(--ease) infinite;
}
.typing i:nth-child(2) { animation-delay: .16s; }
.typing i:nth-child(3) { animation-delay: .32s; }

@keyframes typing-dot {
  0%, 60%, 100% { opacity: .35; transform: translateY(0); }
  30% { opacity: 1; transform: translateY(-3px); }
}

/* ── кнопка «вниз» ───────────────────────────────────────────── */
.scroll-down {
  position: absolute;
  right: 18px;
  bottom: 18px;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--elev);
  border: 1px solid var(--line-2);
  color: var(--text-2);
  box-shadow: var(--shadow-2);
  opacity: 0;
  transform: translateY(8px) scale(.9);
  pointer-events: none;
  transition: opacity var(--dur) var(--ease), transform var(--dur) var(--ease);
}
.scroll-down[data-visible="true"] { opacity: 1; transform: none; pointer-events: auto; }
.scroll-down svg { width: 18px; height: 18px; }

/* ── markdown внутри пузырей ─────────────────────────────────── */
.bubble__text p { margin: 0 0 .55em; }
.bubble__text p:last-child { margin-bottom: 0; }
.bubble__text strong { font-weight: 700; }
.bubble__text em { font-style: italic; }
.bubble__text code {
  font-family: var(--font-mono);
  font-size: .87em;
  padding: 1px 5px;
  border-radius: 5px;
  background: rgba(255, 255, 255, .08);
}
.msg--out .bubble__text code { background: rgba(0, 0, 0, .18); }
.bubble__text pre {
  margin: .5em 0;
  padding: 10px 12px;
  border-radius: 10px;
  background: var(--bg-deep);
  border: 1px solid var(--line);
  overflow-x: auto;
  max-width: 100%;
}
.bubble__text pre code {
  background: none;
  padding: 0;
  font-size: .85em;
  line-height: 1.5;
  white-space: pre;
}
.msg--out .bubble__text pre { background: rgba(0, 0, 0, .22); border-color: rgba(255, 255, 255, .1); }
.msg--out .bubble__text pre code { color: var(--bubble-out-text); }
.bubble__text ul,
.bubble__text ol { margin: .3em 0 .5em; padding-left: 1.35em; }
.bubble__text li { margin: .12em 0; }
.bubble__text blockquote {
  margin: .4em 0;
  padding: 2px 0 2px 10px;
  border-left: 2px solid var(--accent);
  color: var(--text-2);
}
.msg--out .bubble__text blockquote { color: rgba(255, 255, 255, .82); border-left-color: rgba(255, 255, 255, .5); }
.bubble__text h1,
.bubble__text h2,
.bubble__text h3 {
  margin: .5em 0 .3em;
  font-size: 1.04em;
  font-weight: 700;
  line-height: 1.3;
}
.bubble__text hr { margin: .6em 0; border: 0; border-top: 1px solid var(--line-2); }
.bubble__text a { text-decoration: underline; text-underline-offset: 2px; }
.msg--out .bubble__text a { color: #ffe6d4; }
.bubble__text table { border-collapse: collapse; margin: .4em 0; font-size: .9em; }
.bubble__text th,
.bubble__text td { border: 1px solid var(--line-2); padding: 4px 8px; }

/* ── мобильные уточнения ─────────────────────────────────────── */
@media (max-width: 860px) {
  .chat-head__back { display: inline-flex; margin-left: -4px; }
  .chat-head { min-height: 54px; padding-left: 6px; }
  .chat-head__avatar { width: 34px; height: 34px; }
  .messages { padding-top: 10px; }
  .messages__inner { padding: 0 10px; gap: 2px; }
  .msg { max-width: 86%; }
  .msg__name { font-size: .76em; }
  .bubble { padding: 8px 11px 6px; }
  .scroll-down { right: 12px; bottom: 12px; }
}

/* ── «показать более ранние» ─────────────────────────────────── */
.load-earlier {
  align-self: center;
  margin: 4px auto 10px;
  padding: 7px 14px;
  border-radius: var(--r-pill);
  background: var(--panel-2);
  border: 1px solid var(--line);
  color: var(--text-2);
  font-size: .82em;
  font-weight: 600;
  transition: background var(--dur) var(--ease), color var(--dur) var(--ease);
}
.load-earlier:hover { background: var(--panel-3); color: var(--text); }
