:root {
  --chat-sidebar-width: 340px;
}

.chat-root {
  width: 100%;
  overflow-x: clip;
}

.chat-layout {
  display: grid;
  grid-template-columns: var(--chat-sidebar-width) 1fr;
  gap: 14px;
  align-items: start;
  min-width: 0;
  overflow-x: hidden;
}

.chat-sidebar,
.chat-pane {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: rgba(255, 255, 255, 0.02);
  box-shadow: 0 10px 30px rgba(0,0,0,0.18);
}

.chat-sidebar {
  padding: 12px;
  min-height: 560px;
  position: sticky;
  top: 88px;
}

.chat-sidebar-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
}

.chat-search {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  margin-bottom: 10px;
}

.chat-search-error {
  margin: -4px 0 10px;
  min-height: 0;
}
.chat-search-error__box {
  border: 1px solid rgba(251, 113, 133, 0.55);
  background: rgba(251, 113, 133, 0.12);
  color: rgba(251, 113, 133, 0.95);
  border-radius: 12px;
  padding: 8px 10px;
  font-weight: 700;
  font-size: 13px;
}

.chat-list {
  display: grid;
  gap: 6px;
  max-height: calc(100vh - 240px);
  overflow: auto;
  padding-right: 4px;
}

.chat-item {
  display: grid;
  gap: 2px;
  text-decoration: none;
  color: var(--text);
  border: 1px solid var(--line-soft);
  border-radius: 12px;
  padding: 10px 10px;
  background: rgba(16, 17, 20, 0.55);
  transition: border-color 0.2s ease, transform 0.12s ease, background 0.2s ease;
}

.chat-item:hover {
  border-color: rgba(245, 158, 11, 0.55);
  transform: translateY(-1px);
  background: rgba(16, 17, 20, 0.75);
}
@media (hover: none) {
  .chat-item:hover {
    transform: none;
    border-color: var(--line-soft);
    background: rgba(16, 17, 20, 0.55);
  }
}

.chat-item--active {
  border-color: var(--accent);
  background: rgba(42, 25, 6, 0.55);
}

.chat-item:focus {
  outline: none;
}
.chat-item:focus-visible {
  outline: none;
  border-color: rgba(245, 158, 11, 0.85);
  box-shadow: 0 0 0 2px rgba(245, 158, 11, 0.22);
}

.chat-item-title {
  font-weight: 800;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

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

.chat-pane {
  padding: 0;
  min-height: 560px;
  overflow: hidden;
  overflow-x: hidden;
  min-width: 0;
  display: flex;
  flex-direction: column;
  max-height: clamp(520px, 72vh, 760px);
}

.chat-pane-head {
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
  background: rgba(8, 9, 11, 0.55);
}

.chat-peer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.chat-peer-title {
  font-weight: 900;
}

.chat-peer-meta {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.chat-messages {
  flex: 1 1 auto;
  min-height: 0;
  overflow: auto;
  overflow-x: hidden;
  overflow-x: clip;
  padding: 14px;
  /* Grid на мобильных иногда "растягивает" элементы по высоте.
     Flex-колонка стабильнее: пузырь = высота контента. */
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.chat-msg {
  max-width: min(560px, 92%);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 10px 12px;
  background: rgba(16, 17, 20, 0.7);
  display: flex;
  flex-direction: column;
  gap: 6px;
  position: relative;
}

.chat-msg--me {
  margin-left: auto;
  border-color: rgba(245, 158, 11, 0.6);
  background: rgba(42, 25, 6, 0.55);
}

.chat-msg--them {
  margin-right: auto;
}

.chat-msg-text {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  word-break: break-word;
}

.chat-msg-meta {
  font-size: 12px;
  color: var(--muted);
  text-align: right;
}

.chat-msg-edited { opacity: 0.8; }

/* Меню действий как "стрелочка слева" */
.chat-msg-actions {
  position: absolute;
  top: 8px;
  left: -32px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.15s ease;
}
.chat-msg:hover .chat-msg-actions,
.chat-msg-actions:hover,
.chat-msg--menu-open .chat-msg-actions {
  opacity: 1;
  pointer-events: auto;
}
.chat-msg-menu-btn {
  width: 26px;
  height: 26px;
  border-radius: 9px;
  border: 1px solid var(--line);
  background: rgba(16, 17, 20, 0.85);
  color: var(--text);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
}
.chat-msg-menu-btn:hover {
  border-color: rgba(245, 158, 11, 0.7);
  color: var(--accent);
}

.chat-msg-menu {
  position: absolute;
  top: 30px;
  left: 34px;
  min-width: 160px;
  max-width: 240px;
  max-height: 220px;
  overflow: auto;
  background: rgba(16, 17, 20, 0.98);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: 0 16px 40px rgba(0,0,0,0.35);
  padding: 6px;
  display: grid;
  gap: 6px;
  z-index: 10;
}

/* Если сообщение справа (моё) — открываем меню влево, чтобы не вылезало за экран */
.chat-msg--me .chat-msg-menu {
  left: auto;
  right: 0;
}
.chat-msg-menu-item {
  width: 100%;
  text-align: left;
  border: 1px solid var(--line-soft);
  border-radius: 10px;
  background: transparent;
  color: var(--text);
  padding: 6px 8px;
  cursor: pointer;
  font-size: 13px;
}
.chat-msg-menu-item:hover {
  border-color: rgba(245, 158, 11, 0.7);
  color: var(--accent);
  background: rgba(245, 158, 11, 0.06);
}
.chat-msg-menu-item--danger:hover {
  border-color: rgba(251, 113, 133, 0.9);
  color: rgba(251, 113, 133, 0.95);
  background: rgba(251, 113, 133, 0.06);
}

/* Модалка */
.chat-modal {
  position: fixed;
  inset: 0;
  z-index: 999;
  display: grid;
  place-items: center;
}
.chat-modal[hidden] {
  display: none !important;
}
.chat-modal__backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.55);
  backdrop-filter: blur(6px);
}
.chat-modal__panel {
  position: relative;
  width: min(560px, 92vw);
  border: 1px solid var(--line);
  border-radius: 16px;
  background: rgba(16, 17, 20, 0.98);
  box-shadow: 0 24px 70px rgba(0,0,0,0.55);
  padding: 14px;
}
.chat-modal__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 10px;
}
.chat-modal__title { font-weight: 900; }
.chat-modal__close {
  width: 34px;
  height: 34px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: transparent;
  color: var(--text);
  cursor: pointer;
}
.chat-modal__close:hover { border-color: rgba(245, 158, 11, 0.7); color: var(--accent); }
.chat-modal__body { display: grid; gap: 10px; }
.chat-modal__foot {
  margin-top: 12px;
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
.chat-modal__textarea {
  width: 100%;
  min-height: 120px;
  resize: vertical;
}

.chat-form {
  padding: 12px 14px;
  border-top: 1px solid var(--line);
  display: flex;
  gap: 10px;
  align-items: center;
  background: rgba(8, 9, 11, 0.55);
}

.chat-input {
  flex: 1 1 auto;
  resize: none;
  min-height: 44px;
  max-height: 140px;
}

.chat-form .btn {
  height: 44px;
  align-self: center;
}

/* Dark scroll */
.chat-list::-webkit-scrollbar,
.chat-messages::-webkit-scrollbar {
  width: 10px;
}
.chat-list::-webkit-scrollbar-track,
.chat-messages::-webkit-scrollbar-track {
  background: rgba(16, 17, 20, 0.35);
  border-radius: 999px;
}
.chat-list::-webkit-scrollbar-thumb,
.chat-messages::-webkit-scrollbar-thumb {
  background: rgba(245, 158, 11, 0.35);
  border-radius: 999px;
  border: 2px solid rgba(16, 17, 20, 0.35);
}
.chat-list::-webkit-scrollbar-thumb:hover,
.chat-messages::-webkit-scrollbar-thumb:hover {
  background: rgba(245, 158, 11, 0.55);
}

@media (max-width: 980px) {
  .chat-layout {
    grid-template-columns: 1fr;
  }
  .chat-sidebar {
    position: static;
    min-height: unset;
  }
  .chat-messages {
    height: auto;
    max-height: 60vh;
  }
}

/* Mobile: hover нет, поэтому меню действий должно быть доступно */
@media (max-width: 640px) {
  .chat-pane-head {
    padding: 10px 12px;
  }

  .chat-messages {
    padding: 10px;
    gap: 8px;
    max-height: 58vh;
  }

  .chat-msg {
    max-width: 84%;
    padding: 8px 10px;
    border-radius: 12px;
    gap: 6px;
  }

  .chat-msg-text {
    font-size: 14px;
    line-height: 1.45;
  }

  .chat-msg-meta { font-size: 11px; }

  .chat-form {
    padding: 10px 12px calc(10px + env(safe-area-inset-bottom)) 12px;
  }

  .chat-input {
    font-size: 16px; /* iOS zoom fix */
    min-height: 42px;
  }

  .chat-form .btn {
    height: 42px;
    min-width: 112px;
    padding: 0 12px;
  }

  .chat-msg-actions {
    left: auto;
    right: 8px;
    top: 8px;
    opacity: 0;
    pointer-events: none;
  }
  .chat-msg--active .chat-msg-actions {
    opacity: 1;
    pointer-events: auto;
  }
  .chat-msg-menu {
    left: auto;
    right: 0;
    min-width: 170px;
  }
  .chat-msg {
    max-width: min(640px, 96%);
    padding-right: 44px; /* место под кнопку меню */
  }
}
