/* ==========================================================================
   Админка: список диалогов + переписка
   Наследует переменные темы из styles.css
   ========================================================================== */

body.admin {
  display: block;
  padding: 0;
  background: var(--bg-secondary);
  height: 100vh;
  overflow: hidden;
}

.app {
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  max-width: 640px;
  margin: 0 auto;
  background: var(--bg);
  position: relative;
}

.screen {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.screen[hidden] { display: none; }

/* ------------------------------------------------------------ Топбар ---- */

.topbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  background: var(--bg);
  border-bottom: 1px solid var(--bg-secondary);
  padding-top: calc(12px + env(safe-area-inset-top));
}

.topbar--chat { gap: 8px; }

.topbar__main {
  flex: 1 1 auto;
  min-width: 0;
}

.topbar__title {
  margin: 0;
  font-size: 19px;
  font-weight: 700;
  letter-spacing: -.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar__sub {
  margin: 1px 0 0;
  font-size: 12.5px;
  color: var(--hint);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.iconbtn {
  flex: 0 0 auto;
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  border: 0;
  background: var(--bg-secondary);
  color: var(--text);
  cursor: pointer;
  text-decoration: none;
  transition: opacity .15s ease;
}

.iconbtn:active { opacity: .6; }
.iconbtn svg { width: 19px; height: 19px; }

/* ----------------------------------------------------------- Фильтры ---- */

.filters {
  flex: 0 0 auto;
  display: flex;
  gap: 8px;
  padding: 10px 14px;
  background: var(--bg);
  border-bottom: 1px solid var(--bg-secondary);
  overflow-x: auto;
  scrollbar-width: none;
}

.filters::-webkit-scrollbar { display: none; }

.filter {
  flex: 0 0 auto;
  padding: 7px 14px;
  font: inherit;
  font-size: 13.5px;
  font-weight: 500;
  color: var(--hint);
  background: var(--bg-secondary);
  border: 0;
  border-radius: 999px;
  cursor: pointer;
  white-space: nowrap;
  transition: background .15s ease, color .15s ease;
}

.filter.is-active {
  color: var(--button-text);
  background: var(--button);
}

.filter__badge {
  display: inline-block;
  min-width: 18px;
  padding: 0 5px;
  margin-left: 4px;
  font-size: 11.5px;
  font-weight: 700;
  line-height: 18px;
  text-align: center;
  border-radius: 999px;
  color: var(--button-text);
  background: var(--stop);
}

.filter.is-active .filter__badge {
  color: var(--button);
  background: var(--button-text);
}

/* ------------------------------------------------------------- Список --- */

.list {
  flex: 1 1 auto;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.dialog {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 13px 14px;
  font: inherit;
  color: inherit;
  text-align: left;
  background: var(--bg);
  border: 0;
  border-bottom: 1px solid var(--bg-secondary);
  cursor: pointer;
  transition: background .15s ease;
}

.dialog:active { background: var(--bg-secondary); }

.dialog__avatar {
  flex: 0 0 auto;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-size: 16px;
  font-weight: 600;
  color: #fff;
  text-transform: uppercase;
}

.dialog__body {
  flex: 1 1 auto;
  min-width: 0;
}

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

.dialog__name {
  font-weight: 600;
  font-size: 14.5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.dialog__time {
  flex: 0 0 auto;
  font-size: 11.5px;
  color: var(--hint);
}

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

.dialog--unread .dialog__preview { color: var(--text); font-weight: 500; }

.dialog__badge {
  flex: 0 0 auto;
  min-width: 21px;
  height: 21px;
  padding: 0 6px;
  display: grid;
  place-items: center;
  font-size: 11.5px;
  font-weight: 700;
  color: #fff;
  background: var(--stop);
  border-radius: 999px;
}

.empty {
  margin: 0;
  padding: 40px 20px;
  text-align: center;
  color: var(--hint);
  font-size: 14px;
}

/* ------------------------------------------------------------ Переписка - */

.thread {
  flex: 1 1 auto;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  background: var(--bg-secondary);
}

.bubble {
  max-width: 78%;
  padding: 9px 13px;
  border-radius: 16px;
  font-size: 14.5px;
  line-height: 1.45;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  box-shadow: 0 1px 1px rgba(0, 0, 0, .05);
}

.bubble--in {
  align-self: flex-start;
  background: var(--bg);
  color: var(--text);
  border-bottom-left-radius: 5px;
}

.bubble--out {
  align-self: flex-end;
  background: var(--button);
  color: var(--button-text);
  border-bottom-right-radius: 5px;
}

.bubble__meta {
  display: block;
  margin-top: 4px;
  font-size: 11px;
  opacity: .65;
}

.bubble--out .bubble__meta { text-align: right; }

.bubble--pending { opacity: .6; }

.daysep {
  align-self: center;
  margin: 8px 0;
  padding: 3px 12px;
  font-size: 11.5px;
  color: var(--hint);
  background: var(--bg);
  border-radius: 999px;
}

/* ---------------------------------------------------------- Поле ответа - */

.replybar {
  flex: 0 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 12px;
  padding-bottom: calc(10px + env(safe-area-inset-bottom));
  background: var(--bg);
  border-top: 1px solid var(--bg-secondary);
}

.replybar textarea {
  flex: 1 1 auto;
  margin: 0;
  min-height: 40px;
  max-height: 120px;
  padding: 10px 14px;
  font-size: 14.5px;
  border-radius: 20px;
  resize: none;
}

.sendbtn {
  flex: 0 0 auto;
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 50%;
  color: var(--button-text);
  background: var(--button);
  cursor: pointer;
  transition: opacity .15s ease, transform .1s ease;
}

.sendbtn svg { width: 18px; height: 18px; }
.sendbtn:disabled { opacity: .5; cursor: default; }
.sendbtn:not(:disabled):active { transform: scale(.94); }

/* ------------------------------------------------- Присутствие в чате --- */

/* Кружки-аватары админов, которые сейчас смотрят диалог */
.presence {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
}

/* hidden должен побеждать display: flex */
.presence[hidden] { display: none; }

.presence__avatars {
  display: flex;
  flex-direction: row-reverse;
}

.presence__avatar {
  width: 28px;
  height: 28px;
  margin-left: -9px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-size: 11px;
  font-weight: 700;
  color: #fff;
  border: 2px solid var(--bg);
  text-transform: uppercase;
}

.presence__avatar:last-child { margin-left: 0; }

/* Живая точка «кто-то есть в чате» */
.presence__live {
  width: 7px;
  height: 7px;
  margin-right: 7px;
  border-radius: 50%;
  background: var(--ok, #2f9e44);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--ok, #2f9e44) 22%, transparent);
  animation: pulse 1.8s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: .45; }
}

/* Плашка «занят другим админом» в списке диалогов */
.dialog__busy {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  margin-top: 3px;
  padding: 1px 8px;
  font-size: 11px;
  font-weight: 600;
  border-radius: 999px;
  color: var(--warn, #e8912d);
  background: color-mix(in srgb, var(--warn, #e8912d) 15%, transparent);
}

.dialog--busy { border-left: 3px solid var(--warn, #e8912d); }

/* --------------------------------------------------- «Взял в работу» --- */

.ownbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 9px 14px;
  font-size: 13px;
  background: var(--bg);  border-top: 1px solid var(--bg-secondary);
}

.ownbar__info {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 8px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.ownbar--mine .ownbar__info { color: var(--ok, #2f9e44); font-weight: 600; }
.ownbar--locked .ownbar__info { color: var(--warn, #e8912d); font-weight: 600; }
.ownbar--free .ownbar__info { color: var(--hint); }

/* hidden должен побеждать display: flex */
.ownbar[hidden] { display: none; }

.ownbar__actions {
  flex: 0 0 auto;
  display: flex;
  gap: 7px;
}

.ownbtn {
  padding: 7px 14px;
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  border: 0;
  border-radius: 999px;
  cursor: pointer;
  white-space: nowrap;
  transition: opacity .15s ease;
}

.ownbtn:active { opacity: .75; }
.ownbtn:disabled { opacity: .5; cursor: default; }

.ownbtn--primary { color: var(--button-text); background: var(--button); }
.ownbtn--ghost { color: var(--text); background: var(--bg-secondary); }
.ownbtn--warn { color: #fff; background: var(--warn, #e8912d); }

/* Поле ввода, когда писать нельзя */
.replybar--locked textarea {
  opacity: .55;
  cursor: not-allowed;
}

.replybar--locked .sendbtn {
  opacity: .4;
  cursor: not-allowed;
}

/* Плашка «ведёт Иван» в списке */
.dialog__owner {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  margin-top: 3px;
  padding: 1px 8px;
  font-size: 11px;
  font-weight: 600;
  border-radius: 999px;
}

.dialog__owner--mine {
  color: var(--ok, #2f9e44);
  background: color-mix(in srgb, var(--ok, #2f9e44) 15%, transparent);
}

.dialog__owner--other {
  color: #7b5cff;
  background: color-mix(in srgb, #7b5cff 15%, transparent);
}

.dialog--locked { border-left: 3px solid #7b5cff; }

/* Плашка «ждёт ответа» — последнее сообщение от пользователя */
.dialog__wait {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  margin-top: 3px;
  padding: 1px 8px;
  font-size: 11px;
  font-weight: 600;
  border-radius: 999px;
  color: var(--stop, #e5484d);
  background: color-mix(in srgb, var(--stop, #e5484d) 14%, transparent);
}

.dialog--waiting { border-left: 3px solid var(--stop, #e5484d); }

/* ------------------------------------------------- Меню действий ------- */

.sheet {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}

/* Атрибут hidden должен побеждать display: flex — иначе невидимое меню
   растянуто на весь экран и перехватывает все нажатия. */
.sheet[hidden] { display: none; }

.sheet__backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, .45);
  opacity: 0;
  transition: opacity .2s ease;
}

.sheet.is-open .sheet__backdrop { opacity: 1; }

.sheet__panel {
  position: relative;
  width: 100%;
  max-width: 640px;
  padding: 8px 12px calc(16px + env(safe-area-inset-bottom));
  background: var(--bg);
  border-radius: 20px 20px 0 0;
  transform: translateY(100%);
  transition: transform .25s cubic-bezier(.32, .72, 0, 1);
}

.sheet.is-open .sheet__panel { transform: translateY(0); }

.sheet__handle {
  width: 36px;
  height: 4px;
  margin: 4px auto 12px;
  border-radius: 999px;
  background: var(--hint);
  opacity: .35;
}

.sheet__title {
  margin: 0 4px 10px;
  font-size: 13px;
  font-weight: 600;
  color: var(--hint);
  text-transform: uppercase;
  letter-spacing: .04em;
}

.sheet__item {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px;
  font: inherit;
  color: inherit;
  text-align: left;
  background: none;
  border: 0;
  border-radius: 14px;
  cursor: pointer;
  transition: background .15s ease;
}

.sheet__item:active { background: var(--bg-secondary); }

.sheet__icon {
  flex: 0 0 auto;
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  border-radius: 12px;
}

.sheet__icon svg { width: 20px; height: 20px; }

.sheet__icon--ok {
  color: var(--ok, #2f9e44);
  background: color-mix(in srgb, var(--ok, #2f9e44) 14%, transparent);
}

.sheet__icon--stop {
  color: var(--stop, #e5484d);
  background: color-mix(in srgb, var(--stop, #e5484d) 14%, transparent);
}

.sheet__text {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.sheet__text b { font-size: 15px; font-weight: 600; }

.sheet__text small {
  margin-top: 1px;
  font-size: 12.5px;
  color: var(--hint);
  line-height: 1.35;
}

.sheet__item--cancel {
  justify-content: center;
  margin-top: 6px;
  color: var(--hint);
}

.sheet__item--cancel .sheet__text { flex: 0 0 auto; }

/* ------------------------------------------------------------- Тосты ---- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + env(safe-area-inset-bottom));
  transform: translate(-50%, 20px);
  max-width: 90vw;
  padding: 11px 18px;
  font-size: 13.5px;
  font-weight: 500;
  color: #fff;
  background: rgba(20, 22, 28, .94);
  border-radius: 999px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .22s ease, transform .22s ease;
  z-index: 50;
}

.toast.is-visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

.toast--error { background: rgba(190, 40, 45, .96); }
.toast--ok { background: rgba(38, 130, 60, .96); }

/* --------------------------------------------------------- Загрузка ----- */

.spinner {
  width: 22px;
  height: 22px;
  margin: 40px auto;
  border: 2.5px solid var(--bg-secondary);
  border-top-color: var(--button);
  border-radius: 50%;
  animation: spin .7s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

@media (prefers-reduced-motion: reduce) {
  .spinner { animation-duration: 2s; }
}
