/* ============================================================
   ЧАТЫ
   ============================================================ */

#chats { flex: 1; overflow-y: auto; }

.chat-item {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 12px;
  cursor: pointer;
}
.chat-item:active { background: var(--bg-tertiary); }

.avatar-wrap {
  position: relative;
  width: 46px;
  height: 46px;
  flex-shrink: 0;
}
.avatar {
  width: 46px;
  height: 46px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  font-size: 18px;
  flex-shrink: 0;
  overflow: hidden;
  background-size: cover;
  background-position: center;
}
.avatar-wrap .avatar { width: 100%; height: 100%; }

.status-dot {
  position: absolute;
  right: 0;
  bottom: 0;
  width: 13px;
  height: 13px;
  border-radius: 50%;
  border: 2px solid var(--bg-secondary);
  background: #888;
}
.status-dot.online { background: #3a9d5d; }

.chat-item .info { flex: 1; min-width: 0; }
.chat-item .row1 {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  min-width: 0;
}
.chat-item .name-line {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
  flex: 1;
}
.chat-item .name {
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex-shrink: 0;
  max-width: 60%;
}
.chat-item .time {
  font-size: 12px;
  color: var(--text-muted);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 4px;
}

/* Плашки должностей */
.role-badge {
  display: inline-block;
  padding: 2px 7px;
  border-radius: 8px;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.2px;
  text-transform: uppercase;
  white-space: nowrap;
  flex-shrink: 0;
  line-height: 1.4;
}
.role-badge.role-npto {
  background: linear-gradient(135deg, #f5c542, #e6a800);
  color: #3a2a00;
  box-shadow: 0 1px 3px rgba(245,197,66,0.4);
}
.role-badge.role-storv {
  background: linear-gradient(135deg, #a67cff, #7c4dff);
  color: #fff;
  box-shadow: 0 1px 3px rgba(124,77,255,0.4);
}
.role-badge.role-orv {
  background: linear-gradient(135deg, #4a7cff, #2a5cdd);
  color: #fff;
  box-shadow: 0 1px 3px rgba(74,124,255,0.4);
}

.chat-item .check {
  font-size: 13px;
  color: var(--text-muted);
}
.chat-item .check.read {
  color: #4fc3f7;
}

.chat-item .row2 {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 3px;
}
.chat-item .preview {
  font-size: 13px;
  color: var(--text-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
}
.chat-item .unread {
  background: var(--accent);
  color: #fff;
  border-radius: 10px;
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  font-size: 12px;
  font-weight: bold;
  display: flex;
  align-items: center;
  justify-content: center;
}

.chats-section-title {
  padding: 10px 16px 6px;
  font-size: 11px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
}

/* ============================================================
   ЭКРАН ЧАТА
   ============================================================ */

#chatView {
  display: none;
  flex-direction: column;
  height: 100%;
  position: absolute;
  inset: 0;
  background: var(--bg);
  z-index: 5;
  touch-action: pan-y;
  overscroll-behavior-x: none;
  will-change: transform;
  transition: transform 0.2s ease-out, opacity 0.2s ease-out;
}
#chatView.active { display: flex; }

#chatView.swipe-return {
  transition: transform 0.25s ease-out, opacity 0.25s ease-out;
}

#chatView.swipe-closing {
  transition: transform 0.22s ease-out, opacity 0.22s ease-out;
  transform: translateX(-100%) !important;
  opacity: 0.3;
}

#pinnedBar {
  display: none;
  padding: 8px 16px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  font-size: 13px;
  border-left: 3px solid var(--accent);
}
#pinnedBar.show { display: block; }
#pinnedBar .pin-title {
  font-size: 11px;
  color: var(--accent);
  font-weight: bold;
  text-transform: uppercase;
  margin-bottom: 2px;
  display: flex;
  justify-content: space-between;
}
#pinnedBar .pin-text {
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
#pinnedBar .pin-count { color: var(--text-muted); font-size: 11px; }

#messages {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  -webkit-overflow-scrolling: touch;
  scroll-padding-bottom: 8px;
  overscroll-behavior-x: none;
}

.msg {
  padding: 7px 12px;
  border-radius: 16px;
  max-width: 80%;
  word-wrap: break-word;
  font-size: 15px;
  line-height: 1.35;
  position: relative;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}
#messages * {
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

/* Сообщение с медиа: обычный пузырь, фото вставляется напрямую */
.msg.has-media {
  padding: 4px;
  max-width: 80%;
}

/* Картинки и видео — просто ограничение, без обрезки и обёрток */
.msg img,
.msg video {
  display: block;
  max-width: 100%;
  max-height: 400px;
  width: auto;
  height: auto;
  border-radius: 12px;
  cursor: pointer;
  margin: 0;
}

/* Время и галочки поверх фото в углу */
.msg.has-media .msg-meta {
  position: absolute;
  right: 10px;
  bottom: 10px;
  margin-top: 0;
  padding: 2px 7px;
  border-radius: 12px;
  background: rgba(0, 0, 0, 0.5);
  color: #fff;
  font-size: 10px;
  opacity: 1;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 4px;
}
.msg.has-media .msg-meta .msg-check {
  color: #fff;
  opacity: 1;
}
.msg.has-media .msg-pin-mark {
  top: 8px;
  left: 8px;
}

/* Подпись под фото */
.msg.has-media .media-text {
  padding: 6px 4px 2px 4px;
  font-size: 15px;
  line-height: 1.35;
}

/* Индикатор воспроизведения видео */
.msg .media-play {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 52px;
  height: 52px;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.55);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 22px;
  pointer-events: none;
}

.own {
  align-self: flex-end;
  background: var(--own-msg);
  color: #fff;
  border-bottom-right-radius: 4px;
}
.other {
  align-self: flex-start;
  background: var(--other-msg);
  color: var(--text);
  border-bottom-left-radius: 4px;
}
.system {
  align-self: center;
  font-size: 12px;
  color: var(--text-muted);
  font-style: italic;
  padding: 4px;
}
.msg .file-link {
  display: inline-block;
  margin-top: 4px;
  padding: 6px 10px;
  background: rgba(0,0,0,0.2);
  border-radius: 6px;
  color: #fff;
  text-decoration: none;
  font-size: 13px;
}
.msg .sender-name {
  font-size: 12px;
  opacity: 0.85;
  margin-bottom: 2px;
  font-weight: 600;
}
.msg .msg-meta {
  font-size: 10px;
  opacity: 0.7;
  text-align: right;
  margin-top: 2px;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 4px;
}
.msg .msg-check { font-size: 12px; opacity: 0.85; }
.msg .msg-edited { font-style: italic; opacity: 0.7; font-size: 10px; }
.msg .msg-pin-mark {
  position: absolute;
  top: -6px;
  left: -6px;
  background: var(--accent);
  color: #fff;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.4);
}
.msg.deleted-text { font-style: italic; opacity: 0.6; }

/* Голосовые */
.voice-msg {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 180px;
}
.voice-play {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: rgba(255,255,255,0.25);
  border: none;
  color: inherit;
  font-size: 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.voice-play:active { background: rgba(255,255,255,0.4); }
.voice-wave {
  flex: 1;
  height: 30px;
  display: flex;
  align-items: center;
  gap: 2px;
}
.voice-wave span {
  display: inline-block;
  width: 3px;
  background: currentColor;
  border-radius: 2px;
  opacity: 0.5;
}
.voice-duration { font-size: 12px; opacity: 0.85; flex-shrink: 0; }

#typingIndicator {
  font-size: 12px;
  color: var(--accent);
  padding: 4px 16px;
  min-height: 20px;
  font-style: italic;
}

form.input-row {
  display: flex;
  padding: 8px 10px;
  gap: 8px;
  background: var(--bg-secondary);
  align-items: flex-end;
  padding-bottom: calc(8px + env(safe-area-inset-bottom));
}

.input-wrap {
  flex: 1;
  display: flex;
  align-items: flex-end;
  gap: 2px;
  background: var(--bg);
  border-radius: 22px;
  padding: 4px 4px 4px 8px;
  min-height: 44px;
  max-height: 140px;
}

form.input-row textarea {
  flex: 1;
  padding: 10px 4px;
  font-size: 16px;
  border: none;
  background: transparent;
  color: var(--text);
  outline: none;
  min-width: 0;
  font-family: inherit;
  resize: none;
  max-height: 130px;
  line-height: 1.4;
  overflow-y: auto;
  scrollbar-width: thin;
}

.input-icon-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 20px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: color 0.15s, background 0.15s;
  padding: 0;
  margin-bottom: 4px;
}
.input-icon-btn:active {
  background: var(--bg-tertiary);
  color: var(--text);
}

.send-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: none;
  background: var(--accent);
  color: #fff;
  font-size: 20px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s, transform 0.1s;
  padding: 0;
  box-shadow: 0 2px 8px rgba(74,124,255,0.4);
}
.send-btn:active { transform: scale(0.92); }
.send-btn.recording {
  background: var(--danger);
  animation: pulseRec 1s infinite;
}
@keyframes pulseRec {
  0%, 100% { box-shadow: 0 0 0 0 rgba(224,74,74,0.7); }
  50% { box-shadow: 0 0 0 10px rgba(224,74,74,0); }
}

#uploadProgress {
  display: none;
  padding: 8px 16px;
  background: var(--bg-secondary);
  border-top: 1px solid var(--border);
  font-size: 13px;
}
#uploadProgress.show { display: block; }
#uploadProgress .progress-info {
  display: flex;
  justify-content: space-between;
  margin-bottom: 4px;
  color: var(--text-muted);
}
#uploadProgress .progress-bar {
  height: 6px;
  background: var(--bg-tertiary);
  border-radius: 3px;
  overflow: hidden;
}
#uploadProgress .progress-fill {
  height: 100%;
  background: var(--accent);
  width: 0%;
  transition: width 0.15s;
  border-radius: 3px;
}

#recordingBar {
  display: none;
  padding: 10px 16px;
  background: var(--bg-secondary);
  border-top: 1px solid var(--border);
  align-items: center;
  gap: 12px;
}
#recordingBar.show { display: flex; }
#recordingBar .rec-dot {
  width: 12px;
  height: 12px;
  background: var(--danger);
  border-radius: 50%;
  animation: blink 1s infinite;
}
@keyframes blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.3; }
}
#recordingBar .rec-time {
  font-size: 14px;
  color: var(--text);
  font-variant-numeric: tabular-nums;
  flex: 1;
}
#recordingBar .rec-cancel {
  padding: 8px 12px;
  background: transparent;
  border: 1px solid var(--danger);
  color: var(--danger);
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
}
#recordingBar .rec-send {
  padding: 8px 16px;
  background: var(--ok);
  color: #fff;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
  font-weight: bold;
}

/* ============================================================
   ПОЛНОЭКРАННЫЙ ПРОСМОТРЩИК ФОТО/ВИДЕО
   ============================================================ */

#imageViewer {
  display: none;
  position: fixed;
  inset: 0;
  background: #000;
  z-index: 50;
  flex-direction: column;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}
#imageViewer.show { display: flex; }

#imageViewer .iv-header {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  padding-top: calc(12px + env(safe-area-inset-top));
  background: linear-gradient(to bottom, rgba(0,0,0,0.65), rgba(0,0,0,0));
  z-index: 2;
  transition: opacity 0.2s;
  color: #fff;
}
#imageViewer.iv-hidden-ui .iv-header { opacity: 0; pointer-events: none; }

#imageViewer .iv-close {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: rgba(255,255,255,0.12);
  border: none;
  color: #fff;
  font-size: 22px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
#imageViewer .iv-close:active { background: rgba(255,255,255,0.25); }

#imageViewer .iv-info {
  flex: 1;
  min-width: 0;
}
#imageViewer .iv-sender {
  font-size: 15px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
#imageViewer .iv-date {
  font-size: 12px;
  opacity: 0.7;
}

#imageViewer .iv-menu-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: rgba(255,255,255,0.12);
  border: none;
  color: #fff;
  font-size: 20px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
#imageViewer .iv-menu-btn:active { background: rgba(255,255,255,0.25); }

#imageViewer .iv-content {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  position: relative;
}
#imageViewer .iv-media {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  transition: transform 0.2s ease-out;
  will-change: transform;
  transform-origin: center center;
}

#imageViewer .iv-hint {
  position: absolute;
  bottom: calc(20px + env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  color: #fff;
  font-size: 12px;
  opacity: 0.6;
  pointer-events: none;
  transition: opacity 0.3s;
}
#imageViewer.iv-hidden-ui .iv-hint { opacity: 0; }

/* Меню «⋮» */
#imageViewer .iv-menu {
  position: absolute;
  top: 60px;
  right: 14px;
  background: #2a2a3c;
  border-radius: 12px;
  box-shadow: 0 6px 20px rgba(0,0,0,0.6);
  overflow: hidden;
  display: none;
  z-index: 3;
  min-width: 200px;
}
#imageViewer .iv-menu.show { display: block; }
#imageViewer .iv-menu-item {
  padding: 13px 18px;
  font-size: 14px;
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 10px;
  border-bottom: 1px solid rgba(255,255,255,0.06);
}
#imageViewer .iv-menu-item:last-child { border-bottom: none; }
#imageViewer .iv-menu-item:active { background: rgba(255,255,255,0.08); }