/* Дизайн-система интерфейса: Zulip-подобная тема.
   Структура: тёмный левый сайдбар, белая центральная колонка,
   правый сайдбар пользователей; recipient bars над группами сообщений. */

:root {
  --sidebar-bg: #22272e;
  --sidebar-bg-hover: #2e343d;
  --sidebar-active: #39414c;
  --sidebar-text: #ccd2da;
  --sidebar-muted: #8b949e;
  --accent: #5b6ee8;
  --accent-hover: #4a5bd4;
  --bg: #ffffff;
  --bar-bg: #e9edf3;
  --bar-border: #c6cedb;
  --text: #1f2328;
  --text-muted: #6b7280;
  --border: #d9dee5;
  --hover: #f5f7fa;
  --danger: #dc2626;
  --radius: 6px;
  --sidebar-w: 260px;
  --users-w: 220px;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  line-height: 1.45;
  color: var(--text);
  background: var(--bg);
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

.visually-hidden {
  position: absolute;
  width: 1px; height: 1px;
  margin: -1px; padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

/* ---- Каркас: 3 колонки ---- */

#shell {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

#main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.chat-header {
  flex-shrink: 0;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}

.chat-header h2 {
  margin: 0;
  font-size: 17px;
  font-weight: 600;
}

.crumb-sep { color: var(--text-muted); font-weight: 400; }

/* ---- Левый сайдбар ---- */

#sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  background: var(--sidebar-bg);
  color: var(--sidebar-text);
  padding: 0 0 12px;
}

.sb-org {
  padding: 14px 14px 10px;
  border-bottom: 1px solid #31373f;
  margin-bottom: 6px;
}

.sb-org-name { font-weight: 700; font-size: 15px; color: #fff; }

.sb-section {
  padding: 12px 14px 4px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--sidebar-muted);
}

#sidebar ul { list-style: none; margin: 0; padding: 0; }

#sidebar li a {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 5px 14px;
  color: var(--sidebar-text);
  border-radius: 0 var(--radius) var(--radius) 0;
}

#sidebar li a:hover {
  background: var(--sidebar-bg-hover);
  text-decoration: none;
  color: #fff;
}

.sb-mentions { padding: 8px 14px; }
.sb-mentions a { color: var(--sidebar-muted); }

.sb-bottom {
  margin-top: auto;
  padding: 10px 14px 0;
  border-top: 1px solid #31373f;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.sb-bottom a, .sb-bottom .linklike {
  color: var(--sidebar-muted);
  padding: 4px 0;
  text-align: left;
  /* Кнопка Выход - type=submit: сбрасываем общий синий стиль submit */
  background: none;
  border: 0;
  width: 100%;
}

.sb-bottom a:hover, .sb-bottom .linklike:hover {
  color: #fff;
  text-decoration: none;
}

.sb-bottom form { margin: 0; }

.linklike {
  background: none;
  border: 0;
  font: inherit;
  cursor: pointer;
}

/* ---- Бейджи ---- */

.badge {
  display: inline-block;
  min-width: 20px;
  padding: 1px 6px;
  border-radius: 10px;
  background: var(--accent);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  text-align: center;
}

/* ---- Лента ---- */

#messages {
  flex: 1;
  overflow-y: auto;
  padding: 0 0 8px;
}

.empty-state {
  margin: auto;
  color: var(--text-muted);
  text-align: center;
}

.topic-group { margin: 0; }

/* Recipient bar: строка "канал > тема" над группой, залипает сверху */
.recipient-bar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 5px 16px;
  background: var(--bar-bg);
  border-top: 1px solid var(--bar-border);
  border-bottom: 1px solid var(--bar-border);
  border-left: 4px solid var(--accent);
  font-weight: 600;
}

.recipient-bar a { color: var(--text); }
.recipient-bar a:hover { color: var(--accent); }
.recipient-bar .rb-sep { color: var(--text-muted); font-weight: 400; }

/* Сообщение: аватар + контент */
.message {
  display: flex;
  gap: 10px;
  padding: 6px 16px;
}

.message:hover { background: var(--hover); }

.message header {
  font-size: 14px;
  margin-bottom: 1px;
}

.message header time {
  color: var(--text-muted);
  font-size: 12px;
  margin-left: 6px;
}

.message .edited { color: var(--text-muted); font-size: 12px; }

.avatar {
  flex-shrink: 0;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 15px;
  user-select: none;
}

.avatar-s { width: 24px; height: 24px; font-size: 12px; }

.msg-main { min-width: 0; }

/* Тело сообщения: markdown-рендер */
.body { overflow-wrap: break-word; }
.body p { margin: 0 0 6px; }
.body p:last-child { margin-bottom: 0; }
.body pre {
  background: #f0f2f5;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px 10px;
  overflow-x: auto;
}
.body code {
  background: #f0f2f5;
  border-radius: 3px;
  padding: 1px 4px;
  font-size: 13px;
}
.body pre code { background: none; padding: 0; }
.body blockquote {
  margin: 4px 0;
  padding: 2px 12px;
  border-left: 3px solid var(--bar-border);
  color: var(--text-muted);
}
.body table { border-collapse: collapse; margin: 6px 0; }
.body th, .body td { border: 1px solid var(--border); padding: 3px 8px; }

/* ---- Композер ---- */

#composer {
  flex-shrink: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 10px 16px;
  border-top: 1px solid var(--border);
  background: var(--bg);
}

#composer textarea {
  flex: 1 1 100%;
  resize: vertical;
  min-height: 44px;
  max-height: 160px;
}

#composer-topic { flex: 1 1 240px; }

input, textarea, button {
  font: inherit;
  color: inherit;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 7px 10px;
  background: var(--bg);
}

input:focus, textarea:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}

button[type="submit"] {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
  cursor: pointer;
  align-self: flex-end;
}

button[type="submit"]:hover { background: var(--accent-hover); }

.composer-error {
  flex-basis: 100%;
  color: var(--danger);
  font-size: 13px;
}

.error { color: var(--danger); }

/* ---- Правый сайдбар ---- */

#users {
  width: var(--users-w);
  flex-shrink: 0;
  overflow-y: auto;
  border-left: 1px solid var(--border);
  padding: 10px 0;
  background: #fafbfc;
}

#users h3 {
  margin: 0;
  padding: 6px 12px 8px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-muted);
}

#users ul { list-style: none; margin: 0; padding: 0; }

#users li {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 3px 12px;
}

#users li:hover { background: var(--hover); }
.user-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user.is-self .user-name { font-weight: 600; }

/* ---- Формы настроек/админки ---- */

#main > section, .settings-block { padding: 0 16px; }

#main form { max-width: 480px; }
#main form input { display: block; width: 100%; margin-bottom: 8px; }
#main form button { margin-bottom: 8px; }

table { border-collapse: collapse; }
th, td { border: 1px solid var(--border); padding: 4px 10px; text-align: left; }

/* ---- Непрочитанное ---- */

.unread-divider {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 10px 16px 6px;
  color: var(--accent);
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.unread-divider::before,
.unread-divider::after {
  content: "";
  flex: 1;
  height: 1px;
  background: var(--accent);
  opacity: 0.4;
}

.msg-unread { background: #f7f9ff; }
.msg-unread:hover { background: #eef2ff; }

.new-marker {
  margin-left: 8px;
  padding: 0 5px;
  border-radius: 3px;
  background: var(--accent);
  color: #fff;
  font-size: 10px;
  font-weight: 700;
  vertical-align: 1px;
}

/* ---- Действия над сообщением ---- */

.msg-actions {
  margin-left: 8px;
  opacity: 0;
  transition: opacity 0.1s;
  display: inline-flex;
  gap: 4px;
  align-items: center;
}

.message:hover .msg-actions,
.message:focus-within .msg-actions { opacity: 1; }

.msg-link {
  color: var(--text-muted);
  font-size: 12px;
  padding: 0 3px;
}

.msg-act {
  padding: 0 4px;
  border: 0;
  background: none;
  color: var(--text-muted);
  font-size: 11px;
  cursor: pointer;
  border-radius: 3px;
}

.msg-act:hover { background: var(--bar-bg); color: var(--text); }

.edit-area {
  width: 100%;
  min-height: 70px;
  resize: vertical;
  font: inherit;
}

.edit-actions {
  display: flex;
  gap: 8px;
  margin-top: 6px;
}

.edit-actions button[type="button"]:not(.linklike) {
  padding: 4px 12px;
  border: 1px solid var(--accent);
  background: var(--accent);
  color: #fff;
  border-radius: var(--radius);
  cursor: pointer;
}

.msg-error { font-size: 12px; margin-top: 4px; }

/* ---- Заголовок темы: действия ---- */

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.header-form { margin: 0; }

.header-act {
  font-size: 12px;
  color: var(--text-muted);
  padding: 2px 6px;
  border-radius: 3px;
}

.header-act:hover { background: var(--hover); color: var(--text); }

/* ---- Поиск ---- */

.sb-search { padding: 8px 12px 4px; }

.sb-search input {
  width: 100%;
  padding: 5px 9px;
  border: 1px solid #3a414b;
  border-radius: var(--radius);
  background: #1b1f25;
  color: var(--sidebar-text);
  font-size: 13px;
}

.sb-search input::placeholder { color: var(--sidebar-muted); }

.search-form {
  display: flex;
  gap: 8px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}

.search-form input { flex: 1; }

.search-results { flex: 1; overflow-y: auto; }

.search-hit { border-bottom: 1px solid var(--border); }

.search-context {
  margin-left: 8px;
  font-size: 12px;
  color: var(--text-muted);
}

/* ---- Подсказки упоминаний ---- */

.mention-hints {
  position: absolute;
  z-index: 20;
  display: flex;
  flex-direction: column;
  min-width: 220px;
  margin-top: -4px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.12);
  overflow: hidden;
}

.mention-hints button {
  padding: 6px 10px;
  border: 0;
  background: none;
  text-align: left;
  font-size: 13px;
  cursor: pointer;
}

.mention-hints button:hover,
.mention-hints button:focus { background: var(--hover); }

/* ---- Мобильная версия: сайдбары в off-canvas ---- */

#menu-btn {
  display: none;
  position: fixed;
  top: 10px;
  left: 10px;
  z-index: 40;
  flex-direction: column;
  gap: 4px;
  padding: 8px;
  background: var(--sidebar-bg);
  border: 0;
  border-radius: var(--radius);
  cursor: pointer;
}

#menu-btn span {
  display: block;
  width: 18px;
  height: 2px;
  background: #fff;
}

@media (max-width: 900px) {
  #menu-btn { display: flex; }

  /* Кнопка-гамбургер фиксирована в углу: заголовок уходит вправо */
  .chat-header { padding-left: 56px; }

  #sidebar {
    position: fixed;
    top: 0; left: 0; bottom: 0;
    z-index: 30;
    transform: translateX(-100%);
    transition: transform 0.15s ease;
  }

  body.sidebar-open #sidebar { transform: translateX(0); }

  #users { display: none; }
}
