* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; font-family: system-ui, -apple-system, Segoe UI, Roboto, sans-serif; }
body { background: #e9eef3; }

.screen { height: 100vh; }
.hidden { display: none !important; }

/* Логин */
#login { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px; }
#login h1 { margin: 0 0 10px; }
#login input, #login button { width: 260px; padding: 12px; border-radius: 10px; border: 1px solid #c8d0da; font-size: 16px; }
#login button { background: #3b82f6; color: #fff; border: none; cursor: pointer; }
.error { color: #d33; min-height: 20px; font-size: 14px; }

/* Основной экран */
#app { display: flex; }
#sidebar { width: 300px; min-width: 300px; background: #fff; border-right: 1px solid #dbe2ea; display: flex; flex-direction: column; }

.me {
  padding: 12px; border-bottom: 1px solid #eee; display: flex; justify-content: space-between; align-items: center;
  font-weight: 600; gap: 8px;
}
.me button { background: none; border: none; font-size: 18px; cursor: pointer; color: #888; }
.me-avatar { cursor: pointer; position: relative; }
.me-avatar:hover::after {
  content: "📷"; position: absolute; right: -4px; bottom: -4px; font-size: 12px;
  background: #fff; border-radius: 50%; padding: 1px 3px; box-shadow: 0 1px 3px rgba(0,0,0,.15);
}

.chats { overflow-y: auto; flex: 1; }
.chat-item {
  padding: 10px 14px; cursor: pointer; border-bottom: 1px solid #f2f5f8;
  display: flex; align-items: center; gap: 10px;
}
.chat-item.active { background: #e6f0ff; }
.chat-item .name { flex: 1; }
.online-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: #22c55e; margin-right: 6px; }

#main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
#chatHeader {
  padding: 14px; background: #fff; border-bottom: 1px solid #dbe2ea; font-weight: 600;
  display: flex; align-items: center; gap: 6px;
}
.back-btn {
  display: none; background: none; border: none; font-size: 22px; cursor: pointer;
  color: #3b82f6; padding: 0 10px 0 0; line-height: 1;
}

#messages { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 8px; }

/* Сообщение: контейнер с аватаркой + пузырём */
.msg {
  position: relative;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  max-width: 85%;
  align-self: flex-start;
}
.msg.mine { align-self: flex-end; flex-direction: row-reverse; }

.msg-avatar { flex-shrink: 0; }
.msg-bubble {
  background: #fff;
  padding: 8px 12px;
  border-radius: 14px;
  box-shadow: 0 1px 2px rgba(0,0,0,.05);
  word-wrap: break-word;
  position: relative;
  max-width: 100%;
}
.msg.mine .msg-bubble { background: #d1e7ff; }

.msg .author { font-size: 12px; color: #3b82f6; margin-bottom: 2px; }
.msg .time { font-size: 11px; color: #888; text-align: right; margin-top: 4px; }
.msg img { max-width: 100%; border-radius: 10px; display: block; margin-top: 4px; }

/* Кнопки удаления/редактирования */
.msg .del, .msg .edit {
  position: absolute;
  top: 4px;
  background: none;
  border: none;
  cursor: pointer;
  font-size: 13px;
  opacity: 0;
  transition: opacity 0.15s;
  padding: 2px 4px;
  line-height: 1;
}
.msg .del { right: 6px; }
.msg .edit { right: 28px; }
.msg:hover .del, .msg:hover .edit { opacity: 0.5; }
.msg .del:hover, .msg .edit:hover { opacity: 1; }

/* Поле редактирования */
.msg .edit-input {
  width: 100%; min-width: 200px; min-height: 40px; padding: 6px 8px;
  font-family: inherit; font-size: 14px; border: 1px solid #c8d0da; border-radius: 8px;
  resize: vertical; box-sizing: border-box;
}
.msg .edit-actions { display: flex; gap: 6px; margin-top: 6px; }
.msg .edit-actions button {
  padding: 4px 10px; font-size: 12px; border-radius: 6px; border: 1px solid #c8d0da;
  background: #fff; cursor: pointer;
}
.msg .edit-actions .save { background: #3b82f6; color: #fff; border-color: #3b82f6; }

/* Composer */
#composer { display: flex; gap: 8px; padding: 10px; background: #fff; border-top: 1px solid #dbe2ea; }
#textInput { flex: 1; padding: 12px; border-radius: 20px; border: 1px solid #c8d0da; font-size: 16px; }
#composer button {
  border: none; background: #3b82f6; color: #fff; width: 46px; border-radius: 50%; font-size: 18px; cursor: pointer;
}
.attach { display: flex; align-items: center; font-size: 22px; cursor: pointer; padding: 0 6px; }

/* Бейдж непрочитанных */
.badge {
  display: inline-block; background: #ef4444; color: #fff; font-size: 11px; font-weight: 600;
  padding: 2px 7px; border-radius: 10px; margin-left: 6px; vertical-align: middle;
}

/* Галочки статусов */
.msg .ticks { font-size: 11px; margin-left: 3px; letter-spacing: -1px; }
.msg .ticks.sent { color: #9ca3af; }
.msg .ticks.delivered { color: #9ca3af; }
.msg .ticks.read { color: #3b82f6; }

/* ======== АВАТАРКИ ======== */
.avatar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  overflow: hidden;
  flex-shrink: 0;
  user-select: none;
}
.avatar img { width: 100%; height: 100%; object-fit: cover; display: block; }
.avatar-fallback { color: #fff; font-weight: 600; }

.avatar-sm { width: 32px; height: 32px; font-size: 14px; }
.avatar-md { width: 36px; height: 36px; font-size: 15px; }
.avatar-lg { width: 64px; height: 64px; font-size: 26px; }

/* ======== МОБИЛЬНАЯ ВЕРСИЯ ======== */
@media (max-width: 768px) {
  html, body {
    height: 100%;
    overflow: hidden;              /* запрет прокрутки всей страницы */
    overscroll-behavior: none;     /* отключаем «резиновый» скролл */
  }

  #login {
    padding: 20px;
    height: 100vh;
  }
  #login input, #login button {
    width: 100%;
    max-width: 320px;
    box-sizing: border-box;
  }

  /* Основной экран */
  #app {
    position: fixed;               /* важно: фиксируем к экрану */
    inset: 0;
    width: 100%;
    height: 100%;
    display: block;                /* отключаем flex на мобильном */
  }

  /* Список чатов — на весь экран */
  #sidebar {
    position: absolute;
    inset: 0;
    width: 100%;
    min-width: 0;
    z-index: 2;
    border-right: none;
  }

  /* Чат — поверх, выезжает справа */
  #main {
    position: absolute;
    inset: 0;
    z-index: 3;
    background: #e9eef3;
    transform: translateX(100%);
    transition: transform 0.25s ease;
    display: flex;
    flex-direction: column;
    min-width: 0;
  }
  #app.chat-open #main { transform: translateX(0); }

  .back-btn { display: inline-block; }

  /* Шапка чата */
  #chatHeader {
    padding: 12px 14px;
    font-size: 16px;
    flex-shrink: 0;
    padding-top: calc(12px + env(safe-area-inset-top));
  }

  /* Лента сообщений — занимает всё, что осталось */
  #messages {
    flex: 1;
    padding: 12px;
    gap: 6px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;   /* плавный скролл на iOS */
  }

  /* Composer — прилипает к низу */
  #composer {
    padding: 8px 10px;
    padding-bottom: calc(8px + env(safe-area-inset-bottom));
    flex-shrink: 0;
    gap: 6px;
  }
  #textInput {
    padding: 10px 14px;
    font-size: 16px;               /* минимум 16px — иначе iOS зумит при фокусе */
    min-width: 0;
  }
  #composer button {
    width: 44px;
    height: 44px;
    font-size: 18px;
    flex-shrink: 0;
  }
  .attach { font-size: 20px; padding: 0 4px; }

  /* Список чатов */
  .chat-item {
    padding: 12px 14px;
    font-size: 16px;
  }
  .me {
    padding: 12px 14px;
    padding-top: calc(12px + env(safe-area-inset-top));
  }
  .chats {
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  /* Сообщения */
  .msg {
    max-width: 92%;
    gap: 6px;
  }
  .msg-bubble {
    font-size: 15px;
    padding: 8px 12px;
  }
  .avatar-md { width: 32px; height: 32px; font-size: 14px; }
  .avatar-sm { width: 30px; height: 30px; font-size: 13px; }

  /* Кнопки удаления/редактирования — всегда видны (для пальца) */
  .msg .del, .msg .edit { opacity: 0.5; font-size: 14px; }

  /* Поле редактирования — на всю ширину */
  .msg .edit-input {
    min-width: 0;
    width: 100%;
    font-size: 15px;
  }

  /* Модалки админки — на всю ширину */
  .modal { max-width: 92%; padding: 16px; }

  /* Админка */
  .admin-wrap { padding: 12px; }
  .admin-header h1 { font-size: 18px; }
  .admin-table { font-size: 13px; }
  .admin-table th, .admin-table td { padding: 8px 10px; }
  .admin-table .actions { flex-direction: column; }
}
