/* Чат: страница «Сообщения» и панель живого урока */
.chat [hidden],
.chat-page [hidden] {
  display: none !important;
}

.chat-layout {
  display: grid;
  grid-template-columns: 300px 1fr;
  gap: 16px;
  height: calc(100vh - 170px);
  min-height: 420px;
}

.chat-side {
  overflow-y: auto;
}

.chat-main {
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow: hidden;
}

.chat-back {
  display: none;
  padding: 10px 14px 0;
  text-decoration: none;
  color: var(--accent);
}

.chat-contact {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  color: var(--text);
  text-decoration: none;
  border-bottom: 1px solid var(--border);
}

.chat-contact:hover {
  background: var(--raised);
}

.chat-contact.active {
  background: var(--accent-soft);
}

.chat-avatar {
  position: relative;
  flex: none;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent);
  display: grid;
  place-items: center;
  font-weight: 600;
}

.chat-avatar.online::after {
  content: '';
  position: absolute;
  right: -1px;
  bottom: -1px;
  width: 11px;
  height: 11px;
  border-radius: 50%;
  background: var(--ok);
  border: 2px solid var(--surface);
}

.chat-contact-text {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.chat-contact-top,
.chat-contact-bottom {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}

.chat-preview {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-badge {
  flex: none;
  min-width: 20px;
  padding: 1px 6px;
  border-radius: var(--r-md);
  background: var(--accent);
  color: var(--on-accent);
  font-size: var(--fs-xs);
  font-weight: 600;
  text-align: center;
}

/* Сама переписка */
.chat {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  height: 100%;
}

.chat-panel {
  background: var(--surface);
}

.chat-head {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
}

.chat-head h2 {
  margin: 0;
  font-size: var(--fs-lg);
}

.chat-head-tools {
  position: relative;
}

/* «Салют» и «Новые сообщения» — главные кнопки сайта в малом размере: углы --r-md и внутренняя
   двойная линия оклада, как у .btn.primary (таблеток на сайте нет). */
.chat-burst-btn,
.chat-new {
  display: inline-flex;
  align-items: center;
  gap: var(--sp-2);
  min-height: var(--h-sm);
  padding: 0 var(--sp-3);
  border: 0;
  border-radius: var(--r-md);
  background: var(--accent);
  box-shadow: inset 0 0 0 2px var(--accent), inset 0 0 0 3px color-mix(in srgb, var(--on-accent) 35%, transparent);
  color: var(--on-accent);
  font: 600 var(--fs-sm) / 1.2 var(--font-text);
  letter-spacing: 0.02em;
  cursor: pointer;
  transition: background-color var(--dur-fast) var(--ease-out);
}

.chat-burst-btn:hover,
.chat-new:hover {
  background: var(--accent-hover);
  box-shadow: inset 0 0 0 2px var(--accent-hover), inset 0 0 0 3px color-mix(in srgb, var(--on-accent) 35%, transparent);
}

.chat-palette {
  position: absolute;
  right: 0;
  top: calc(100% + 8px);
  z-index: 20;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 4px;
  padding: 8px;
  background: var(--raised);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow-pop);
}

.chat-palette button {
  border: 0;
  background: none;
  font-size: var(--fs-glyph);
  line-height: 1;
  padding: 8px;
  border-radius: var(--r-md);
  cursor: pointer;
}

.chat-palette button:hover {
  background: var(--accent-soft);
}

.chat-body {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
}

.chat-list {
  flex: 1;
  overflow-y: auto;
  /* Сверху место под панель действий первого сообщения (.chat-tools поднята на 30px). */
  padding: var(--sp-8) 14px 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  background: var(--bg);
  outline: none;
}

.chat-empty {
  margin: auto;
  text-align: center;
}

.chat-new {
  position: absolute;
  left: 50%;
  bottom: var(--sp-3);
  transform: translateX(-50%);
}

/* Плавает над перепиской — с тенью. */
.chat-new {
  box-shadow: inset 0 0 0 2px var(--accent), inset 0 0 0 3px color-mix(in srgb, var(--on-accent) 35%, transparent), var(--shadow);
}

.chat-new:hover {
  box-shadow: inset 0 0 0 2px var(--accent-hover), inset 0 0 0 3px color-mix(in srgb, var(--on-accent) 35%, transparent), var(--shadow);
}

/* День в переписке — подпись между двумя волосяными линиями, гаснущими наружу (как .rule-title). */
.chat-day {
  align-self: stretch;
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  margin: var(--sp-2) 0 var(--sp-1);
}

.chat-day::before,
.chat-day::after {
  content: '';
  flex: 1;
  height: 1px;
}

.chat-day::before {
  background: linear-gradient(90deg, transparent, var(--frame));
}

.chat-day::after {
  background: linear-gradient(90deg, var(--frame), transparent);
}

.chat-day span {
  color: var(--muted);
  font: 700 var(--fs-2xs) / 1.3 var(--font-text);
  letter-spacing: 0.14em;
  text-transform: uppercase;
}

.chat-msg {
  position: relative;
  display: flex;
  flex-direction: column;
  max-width: 78%;
}

.chat-msg.mine {
  align-self: flex-end;
  align-items: flex-end;
}

.chat-msg.theirs {
  align-self: flex-start;
  align-items: flex-start;
}

.chat-bubble {
  padding: 7px 10px 4px;
  border-radius: var(--r-lg);
  background: var(--surface);
  border: 1px solid var(--border);
  overflow-wrap: anywhere;
  min-width: 0;
  max-width: 100%;
}

.chat-msg.mine .chat-bubble {
  background: color-mix(in srgb, var(--accent) 16%, transparent);
  border-color: color-mix(in srgb, var(--accent) 35%, transparent);
  border-bottom-right-radius: var(--r-sm);
}

/* На светлом фоне подложка своих сообщений бледнее, чтобы время и ссылки читались */
:root[data-theme='light'] .chat-msg.mine .chat-bubble {
  background: color-mix(in srgb, var(--accent) 9%, var(--surface));
}

.chat-msg.theirs .chat-bubble {
  border-bottom-left-radius: var(--r-sm);
}

.chat-text {
  white-space: pre-wrap;
}

.chat-text a {
  color: var(--accent);
}

.chat-meta {
  display: flex;
  justify-content: flex-end;
  gap: 4px;
  margin-top: 2px;
  font-size: var(--fs-xs);
  color: var(--muted);
}

.chat-tick.read {
  color: var(--accent);
}

.chat-image img {
  display: block;
  max-width: 240px;
  max-height: 240px;
  border-radius: var(--r-md);
  margin-bottom: 4px;
}

.chat-media {
  display: block;
  max-width: 100%;
  width: 260px;
  margin-bottom: 4px;
  border-radius: var(--r-md);
}

.chat-file {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  margin-bottom: 4px;
  border-radius: var(--r-md);
  background: color-mix(in srgb, var(--text) 6%, transparent);
  color: var(--text);
  text-decoration: none;
}

.chat-file-name {
  font-weight: 500;
  word-break: break-all;
}

.chat-file-size {
  color: var(--muted);
  font-size: var(--fs-xs);
  white-space: nowrap;
}

.chat-reactions {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-top: 2px;
}

.chat-reactions:empty {
  display: none;
}

.chat-react {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  border: 1px solid var(--frame-soft);
  background: var(--surface);
  border-radius: var(--r-sm);
  padding: 1px var(--sp-2);
  font-size: var(--fs-sm);
  cursor: pointer;
}

/* Свои смайлики (public/emoji): размер — от размера шрифта, как у обычных эмодзи. */
img.emoji {
  display: inline-block;
  width: 1.3em;
  height: 1.3em;
  object-fit: contain;
  vertical-align: -0.3em;
}

.chat-tools button,
.chat-palette button {
  display: inline-grid;
  place-items: center;
}

.chat-burst-item img.emoji {
  width: 1em;
  height: 1em;
}

/* Окошко «Смайлики / Стикеры» у поля ввода — вкладки-закладки сайта (.tabs), компактнее. */
.pop-tabs {
  margin-bottom: var(--sp-2);
}

.pop-tabs .tab {
  min-height: var(--h-md);
  font-size: var(--fs-ui);
}

/* 36 смайликов — ровно шесть рядов по шесть, все видны сразу (без прокрутки). */
.emoji-grid {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 2px;
  width: min(300px, 80vw);
}

.emoji-grid[hidden],
.stk-pane[hidden] {
  display: none;
}

.emoji-grid button {
  display: grid;
  place-items: center;
  aspect-ratio: 1;
  padding: 4px;
  border: 0;
  border-radius: var(--r-md);
  background: none;
  font-size: var(--fs-heading);
  cursor: pointer;
  transition: background-color var(--dur-fast) var(--ease-out), transform var(--dur-release) var(--ease-spring);
}

.emoji-grid button:hover {
  background: var(--accent-soft);
}

.emoji-grid button:active {
  transform: scale(0.9);
  transition-duration: var(--dur-press);
}

.chat-react.mine {
  background: var(--accent-soft);
  border-color: color-mix(in srgb, var(--accent) 45%, transparent);
}

.chat-tools {
  position: absolute;
  top: -30px;
  display: none;
  gap: 2px;
  padding: 3px var(--sp-1);
  background: var(--surface);
  border: 1px solid var(--frame-soft);
  border-radius: var(--r-md);
  box-shadow: var(--shadow);
  z-index: 5;
  white-space: nowrap;
}

.chat-msg.mine .chat-tools {
  right: 0;
}

.chat-msg.theirs .chat-tools {
  left: 0;
}

.chat-msg:hover .chat-tools,
.chat-msg.open .chat-tools,
.chat-msg:focus-within .chat-tools {
  display: flex;
}

.chat-tools button {
  border: 0;
  background: none;
  cursor: pointer;
  font-size: var(--fs-base);
  padding: 2px 4px;
  border-radius: var(--r-sm);
}

.chat-tools button:hover {
  background: var(--accent-soft);
}

.chat-tools .chat-del {
  font-size: var(--fs-xs);
  color: var(--bad);
  padding: 2px 8px;
}

.chat-progress {
  padding: 6px 14px;
  font-size: var(--fs-sm);
  color: var(--muted);
  background: var(--warn-soft);
}

.chat-composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 12px;
  border-top: 1px solid var(--border);
  background: var(--surface);
}

/* Не ниже кнопок рядом (одна строка): общее правило полей снимает min-height у textarea. */
.chat-input {
  flex: 1;
  resize: none;
  min-height: var(--h-md);
  max-height: 140px;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  background: var(--field);
  color: var(--text);
  font: inherit;
  line-height: 1.35;
}

/* Кнопки у поля ввода — одинаковые круги со значком ровно по центру. */
.chat-attach,
.chat-send,
.chat-composer .stk-btn {
  flex: none;
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  cursor: pointer;
}

.chat-attach,
.chat-composer .stk-btn {
  background: none;
  color: var(--muted);
}

.chat-attach:hover,
.chat-composer .stk-btn:hover {
  background: var(--accent-soft);
  color: var(--accent);
}

.chat-send {
  background: var(--accent);
  color: var(--on-accent);
}

.chat-send:disabled {
  opacity: 0.5;
}

/* Летящие реакции */
.chat-burst-layer {
  position: fixed;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
  z-index: 1000;
}

.chat-burst-item {
  position: absolute;
  left: var(--x);
  bottom: -60px;
  font-size: var(--size, 36px);
  opacity: 0;
  animation: chat-fly 2.4s ease-out forwards;
}

.chat-burst-item.still {
  bottom: 45%;
  animation: chat-fade 2s ease-out forwards;
}

@keyframes chat-fly {
  0% {
    transform: translate(0, 0);
    opacity: 0;
  }
  10% {
    opacity: 1;
  }
  50% {
    transform: translate(var(--sway), -50vh);
  }
  100% {
    transform: translate(calc(var(--sway) * -1), -105vh);
    opacity: 0;
  }
}

@keyframes chat-fade {
  0% {
    opacity: 0;
  }
  20% {
    opacity: 1;
  }
  100% {
    opacity: 0;
  }
}

@media (prefers-reduced-motion: reduce) {
  /* Реакция не летит снизу, а проявляется на месте — иначе она остаётся за краем экрана. */
  .chat-burst-item {
    bottom: 45%;
    animation: chat-fade 2s ease-out forwards;
  }
}

@media (max-width: 700px) {
  /* Шапка на телефоне в две строки и заголовок страницы — около 190px; dvh не считает панель браузера,
     поэтому поле ввода остаётся на экране. */
  .chat-layout {
    display: block;
    height: calc(100vh - 190px);
    height: calc(100dvh - 190px);
  }

  .chat-layout > * {
    height: 100%;
  }

  .chat-page.has-open .chat-side,
  .chat-page:not(.has-open) .chat-main {
    display: none;
  }

  .chat-back {
    display: block;
  }

  .chat-msg {
    max-width: 90%;
  }
}

.chat-tools {
  background: var(--raised);
}

.chat-send,
.chat-attach,
.chat-burst-btn,
.chat-react {
  transition: transform var(--dur-release) var(--ease-spring), background-color var(--dur-fast) var(--ease-out);
}

.chat-send:active:not(:disabled),
.chat-attach:active,
.chat-burst-btn:active,
.chat-react:active {
  transform: scale(0.94);
  transition-duration: var(--dur-press);
}

/* История переписки появляется сразу; новое сообщение въезжает со стороны автора на 12px. */
.chat-msg.arrived {
  animation: msg-in-theirs var(--dur-base) var(--ease-out);
}

.chat-msg.mine.arrived {
  animation-name: msg-in-mine;
}

@keyframes msg-in-theirs {
  from {
    opacity: 0;
    transform: translateX(-12px);
  }
}

@keyframes msg-in-mine {
  from {
    opacity: 0;
    transform: translateX(12px);
  }
}

/* «Прочитано»: галочка загорается латунью, когда собеседник открыл переписку. */
.chat-tick {
  transition: color var(--dur-base) var(--ease-out);
}