/* ===== Мессенджер (чат) — стилистика Viaduk ===== */

/* Основной layout */
.chat-page {
  display: flex;
  height: calc(100vh - var(--header-h, 64px) - 60px);
  max-width: var(--container, 1040px);
  margin: 0 auto;
  background: var(--surface, #fff);
  border-radius: var(--radius, 16px);
  overflow: hidden;
  box-shadow: var(--shadow, 0 10px 24px rgba(2,6,23,.08));
  margin-top: 16px;
  margin-bottom: 16px;
}

/* ------ Панель диалогов (левая) ------ */
.chat-sidebar {
  width: 340px;
  min-width: 280px;
  border-right: 1px solid var(--border, rgba(15,23,42,.1));
  display: flex;
  flex-direction: column;
  background: var(--surface, #fff);
}

.chat-sidebar__header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.chat-sidebar__title {
  font-size: 18px;
  font-weight: 700;
  margin: 0;
  color: var(--text);
}

.chat-sidebar__search {
  width: 100%;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm, 12px);
  font-size: 14px;
  outline: none;
  background: var(--surface-2, #f0f4f8);
  color: var(--text);
  transition: border-color .2s;
  margin: 0 20px 12px;
}

.chat-sidebar__search:focus {
  border-color: var(--accent, #3ba4c7);
}

.chat-sidebar__list {
  flex: 1;
  overflow-y: auto;
  padding: 0;
  margin: 0;
  list-style: none;
}

/* Элемент диалога */
.chat-conv {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 20px;
  cursor: pointer;
  transition: background .15s;
  border-bottom: 1px solid var(--border);
  position: relative;
}

.chat-conv:hover {
  background: var(--surface-2, #f0f4f8);
}

.chat-conv--active {
  background: var(--surface-2, #f0f4f8);
  border-left: 3px solid var(--accent, #3ba4c7);
}

.chat-conv__avatar {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: var(--accent, #3ba4c7);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 18px;
  flex-shrink: 0;
  text-transform: uppercase;
}

.chat-conv__body {
  flex: 1;
  min-width: 0;
}

.chat-conv__top {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
}

.chat-conv__name {
  font-weight: 600;
  font-size: 14px;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-conv__time {
  font-size: 12px;
  color: var(--muted, #64748b);
  flex-shrink: 0;
}

.chat-conv__preview {
  font-size: 13px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 2px;
}

.chat-conv__listing {
  font-size: 12px;
  color: var(--accent, #3ba4c7);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 1px;
  font-weight: 500;
  display: flex;
  align-items: center;
  gap: 4px;
}

.chat-conv__listing::before {
  content: '\f02b';
  font-family: 'Font Awesome 6 Free', 'Font Awesome 5 Free';
  font-weight: 900;
  font-size: 9px;
  opacity: .7;
  flex-shrink: 0;
}

.chat-conv__badge {
  position: absolute;
  top: 14px;
  right: 16px;
  background: var(--accent, #3ba4c7);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  min-width: 20px;
  height: 20px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 6px;
}

.chat-sidebar__empty {
  padding: 40px 20px;
  text-align: center;
  color: var(--muted);
  font-size: 14px;
}

.chat-sidebar__empty i {
  font-size: 48px;
  margin-bottom: 12px;
  display: block;
  opacity: .4;
}

/* ------ Окно сообщений (правая часть) ------ */
.chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--bg, #f6f8fb);
}

.chat-main__placeholder {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  gap: 12px;
}

.chat-main__placeholder i {
  font-size: 64px;
  opacity: .3;
}

/* Заголовок чата */
.chat-header {
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--surface, #fff);
  min-height: 64px;
}

.chat-header__back {
  display: none;
  background: none;
  border: none;
  font-size: 20px;
  color: var(--text);
  cursor: pointer;
  padding: 4px;
}

.chat-header__avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 16px;
  flex-shrink: 0;
  text-transform: uppercase;
}

.chat-header__info {
  flex: 1;
  min-width: 0;
}

.chat-header__name {
  font-weight: 600;
  font-size: 15px;
  color: var(--text);
}

.chat-header__listing {
  font-size: 12px;
  color: var(--accent, #3ba4c7);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 2px;
  font-weight: 500;
  text-decoration: none;
  display: flex;
  align-items: center;
  gap: 4px;
  cursor: pointer;
}

.chat-header__listing:hover {
  text-decoration: underline;
}

.chat-header__listing i {
  font-size: 10px;
  opacity: .7;
  flex-shrink: 0;
}

.chat-header__status {
  font-size: 12px;
  color: var(--muted);
}

.chat-header__listing-link {
  font-size: 12px;
  color: var(--accent);
  text-decoration: none;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 200px;
  display: block;
}

.chat-header__listing-link:hover {
  text-decoration: underline;
}

/* Список сообщений */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.chat-messages__date-divider {
  text-align: center;
  font-size: 12px;
  color: var(--muted);
  padding: 8px 0;
  position: relative;
}

.chat-messages__date-divider::before,
.chat-messages__date-divider::after {
  content: '';
  position: absolute;
  top: 50%;
  width: calc(50% - 60px);
  height: 1px;
  background: var(--border);
}

.chat-messages__date-divider::before { left: 0; }
.chat-messages__date-divider::after { right: 0; }

/* Пузырь сообщения */
.chat-msg {
  max-width: 70%;
  padding: 10px 14px;
  border-radius: 16px;
  font-size: 14px;
  line-height: 1.45;
  word-break: break-word;
  position: relative;
}

.chat-msg--out {
  align-self: flex-end;
  background: var(--accent, #3ba4c7);
  color: #fff;
  border-bottom-right-radius: 4px;
}

.chat-msg--in {
  align-self: flex-start;
  background: var(--surface, #fff);
  color: var(--text);
  border-bottom-left-radius: 4px;
  box-shadow: 0 1px 3px rgba(0,0,0,.06);
}

/* Системное сообщение «Вам написали по этому объявлению» (только для получателя) */
.chat-msg--system {
  align-self: center;
  max-width: 90%;
  background: var(--surface-2, #f0f4f8);
  color: var(--muted, #64748b);
  border-radius: 12px;
  padding: 10px 14px;
  font-size: 13px;
  text-align: center;
}

.chat-msg__system-text a {
  color: var(--accent, #3ba4c7);
  font-weight: 600;
  text-decoration: none;
}

.chat-msg__system-text a:hover {
  text-decoration: underline;
}

.chat-msg__time {
  font-size: 11px;
  margin-top: 4px;
  display: flex;
  align-items: center;
  gap: 4px;
}

.chat-msg--out .chat-msg__time {
  color: rgba(255,255,255,.7);
  justify-content: flex-end;
}

.chat-msg--in .chat-msg__time {
  color: var(--muted);
}

.chat-msg__read-icon {
  font-size: 12px;
}

.chat-msg__read-icon--read {
  color: rgba(255,255,255,.9);
}

/* Typing indicator */
.chat-typing {
  font-size: 13px;
  color: var(--muted);
  padding: 4px 14px;
  font-style: italic;
  height: 24px;
}

/* Поле ввода */
.chat-input {
  padding: 12px 20px;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: flex-end;
  gap: 10px;
  background: var(--surface, #fff);
}

.chat-input__field {
  flex: 1;
  resize: none;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm, 12px);
  padding: 10px 14px;
  font-size: 14px;
  font-family: inherit;
  outline: none;
  max-height: 120px;
  min-height: 42px;
  background: var(--surface-2, #f0f4f8);
  color: var(--text);
  transition: border-color .2s;
  line-height: 1.4;
}

.chat-input__field:focus {
  border-color: var(--accent);
}

.chat-input__send {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  border: none;
  background: var(--accent, #3ba4c7);
  color: #fff;
  font-size: 18px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background .2s, transform .1s;
  flex-shrink: 0;
}

.chat-input__send:hover {
  background: var(--accent-2, #5fc9e8);
}

.chat-input__send:active {
  transform: scale(.95);
}

.chat-input__send:disabled {
  opacity: .5;
  cursor: not-allowed;
}

/* Кнопка "Написать продавцу" на странице объявления */
.btn-message-seller {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 10px 20px;
  background: var(--accent, #3ba4c7);
  color: #fff;
  border: none;
  border-radius: var(--radius-sm, 12px);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: background .2s;
}

.btn-message-seller:hover {
  background: var(--accent-2, #5fc9e8);
}

/* Счётчик непрочитанных в навигации */
.nav-chat-badge {
  background: #e74c3c;
  color: #fff;
  font-size: 10px;
  font-weight: 700;
  min-width: 18px;
  height: 18px;
  border-radius: 9px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0 5px;
  margin-left: 4px;
  vertical-align: middle;
}

/* Loading */
.chat-loading {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
  color: var(--muted);
  gap: 8px;
}

/* ===== Адаптивность (мобильные) ===== */
@media (max-width: 768px) {
  .chat-page {
    flex-direction: column;
    height: calc(100vh - 56px);
    margin: 0;
    border-radius: 0;
  }

  /* На странице Сообщения: убираем футер, чуть уменьшаем высоту чата по вертикали,
     фиксируем скролл внутри чата — убираем дёргание внизу при прокрутке */
  body.page-messages footer {
    display: none !important;
  }

  body.page-messages #content {
    min-height: 0;
    display: flex;
    flex-direction: column;
    height: calc(100dvh - 56px);
    overflow: hidden;
    padding-bottom: 20px;
    box-sizing: border-box;
  }

  /* Чат чуть короче по высоте на мобильных: снизу остаётся отступ 20px */
  body.page-messages #content .chat-page {
    flex: 0 0 auto;
    height: calc(100dvh - 56px - 20px);
    min-height: 0;
    max-height: calc(100dvh - 56px - 20px);
    contain: layout;
  }

  /* Один скролл только внутри списка сообщений — без дёргания внизу страницы */
  body.page-messages .chat-main {
    overflow: hidden;
    min-height: 0;
  }

  body.page-messages .chat-messages {
    -webkit-overflow-scrolling: touch;
    overflow-anchor: none;
    transform: translateZ(0);
  }

  .chat-sidebar {
    width: 100%;
    min-width: 0;
    border-right: none;
  }

  /* Когда открыт диалог — прячем sidebar, показываем main */
  .chat-page--conv-open .chat-sidebar {
    display: none;
  }

  .chat-page--conv-open .chat-main {
    display: flex;
  }

  /* Когда список — показываем sidebar, прячем main */
  .chat-page:not(.chat-page--conv-open) .chat-main {
    display: none;
  }

  .chat-header__back {
    display: block;
  }

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

  .chat-sidebar__search {
    margin: 0 12px 8px;
  }

  .chat-sidebar__header {
    padding: 12px 16px;
  }

  .chat-conv {
    padding: 12px 16px;
  }

  /* В открытом диалоге: панель ввода прижата к низу — при фокусе остаётся над клавиатурой */
  body.page-messages .chat-page--conv-open .chat-main .chat-input {
    position: sticky;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 10;
    background: var(--surface, #fff);
    box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.06);
  }

  body.page-messages .chat-page--conv-open .chat-main .chat-messages {
    padding-bottom: 8px;
  }
}

@media (max-width: 480px) {
  .chat-input {
    padding: 8px 12px;
  }

  .chat-messages {
    padding: 12px;
  }
}

