/* Мессенджер — веб-клиент в стиле Telegram Desktop.
   Токены заданы для двух тем, разметка от них не зависит. */

:root[data-theme="light"] {
  --accent: #3390ec;
  --accent-hover: #2b7fd3;
  --accent-weak: #e8f1fb;
  --bg: #ffffff;
  --sidebar: #ffffff;
  --pane: #e3eaf1;
  --pane-pattern: rgba(51, 144, 236, 0.055);
  --text: #0f1419;
  --muted: #707579;
  --muted-2: #9aa0a6;
  --border: #e6e9ec;
  --row-hover: #f1f3f5;
  --row-active: #3390ec;
  --row-active-text: #ffffff;
  --bubble-in: #ffffff;
  --bubble-out: #eeffde;
  --bubble-out-text: #0f1419;
  --head: #ffffff;
  --shadow: 0 1px 2px rgba(16, 35, 47, 0.12), 0 6px 20px rgba(16, 35, 47, 0.08);
  --badge: #3390ec;
  --danger: #e0483c;
}

:root[data-theme="dark"] {
  --accent: #5288c1;
  --accent-hover: #6496cd;
  --accent-weak: #1d2b3a;
  --bg: #0e1621;
  --sidebar: #17212b;
  --pane: #0e1621;
  --pane-pattern: rgba(82, 136, 193, 0.08);
  --text: #e9edf0;
  --muted: #93a6ba;
  --muted-2: #7b8fa3;
  --border: #101a24;
  --row-hover: #202b36;
  --row-active: #2b5278;
  --row-active-text: #ffffff;
  --bubble-in: #182533;
  --bubble-out: #2b5278;
  --bubble-out-text: #ffffff;
  --head: #17212b;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.5), 0 8px 24px rgba(0, 0, 0, 0.35);
  --badge: #5288c1;
  --danger: #ef5b50;
}

* { box-sizing: border-box; }

/* Классы вида .login/.sheet задают display:grid и перебивают браузерное
   правило [hidden]. Без этого атрибут hidden не прячет блоки, и поверх
   приложения висит экран входа и пустая шторка. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--pane);
  color: var(--text);
  font: 15px/1.45 "Segoe UI", Roboto, "Helvetica Neue", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
}

button, input, textarea { font: inherit; color: inherit; }
button:disabled { cursor: default; opacity: 0.55; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
a { color: var(--accent); }
button { cursor: pointer; border: 0; background: none; }
input { outline: none; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

/* ---------- вход ---------- */

.login {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 32px 16px;
  background:
    radial-gradient(1200px 600px at 15% -10%, var(--accent-weak), transparent 60%),
    radial-gradient(900px 500px at 110% 110%, var(--accent-weak), transparent 55%),
    var(--pane);
}

.login-card {
  width: min(400px, 100%);
  background: var(--sidebar);
  border-radius: 18px;
  padding: 32px 28px 24px;
  box-shadow: var(--shadow);
  text-align: center;
}

.logo { color: var(--accent); display: grid; place-items: center; margin-bottom: 14px; }
.login-card h1 { font-size: 22px; margin: 0 0 6px; letter-spacing: -0.01em; }
.login-card > p { margin: 0 0 22px; font-size: 14px; }

.stack { display: grid; gap: 12px; text-align: left; }
.field { display: grid; gap: 6px; }
.field span { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; }
.field input {
  height: 46px;
  padding: 0 14px;
  border-radius: 10px;
  border: 1.5px solid var(--border);
  background: var(--bg);
  color: var(--text);
}
.field input:focus { border-color: var(--accent); }

.btn {
  height: 46px;
  border-radius: 10px;
  font-weight: 600;
  transition: background 120ms ease, color 120ms ease;
}
.btn.primary { background: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-hover); }
.btn.ghost { color: var(--accent); }

.note { margin: 14px 0 0; font-size: 13px; color: var(--danger); min-height: 18px; }
.disclaimer {
  margin: 20px 0 0;
  font-size: 12px;
  line-height: 1.5;
  color: var(--muted-2);
  border-top: 1px solid var(--border);
  padding-top: 14px;
  text-align: left;
}

/* ---------- каркас ---------- */

.app {
  height: 100vh;
  height: 100dvh;
  display: grid;
  grid-template-columns: minmax(320px, 27%) 1fr;
  overflow: hidden;
}

.sidebar {
  background: var(--sidebar);
  border-right: 1px solid var(--border);
  display: grid;
  grid-template-rows: auto auto 1fr auto;
  min-height: 0;
}

.sidebar-head {
  display: grid;
  grid-template-columns: 1fr 36px 36px;
  align-items: center;
  gap: 8px;
  padding: 10px 10px 8px;
}

.search {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 38px;
  padding: 0 12px;
  background: var(--row-hover);
  border-radius: 10px;
  color: var(--muted);
}
.search input { flex: 1; background: none; border: 0; color: var(--text); }
.search input::placeholder { color: var(--muted-2); }

.icon-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  color: var(--muted);
  transition: background 120ms ease, color 120ms ease;
}
.icon-btn:hover { background: var(--row-hover); color: var(--text); }

.tabs { display: flex; gap: 6px; padding: 0 10px 8px; }
.tab {
  flex: 1;
  height: 32px;
  border-radius: 8px;
  font-size: 13.5px;
  font-weight: 600;
  color: var(--muted);
}
.tab:hover { background: var(--row-hover); }
.tab.active { background: var(--accent-weak); color: var(--accent); }

.chat-list { overflow-y: auto; padding: 0 6px 8px; min-height: 0; }
.chat-list::-webkit-scrollbar, .messages::-webkit-scrollbar { width: 8px; }
.chat-list::-webkit-scrollbar-thumb, .messages::-webkit-scrollbar-thumb {
  background: var(--border); border-radius: 8px;
}

.row {
  display: grid;
  grid-template-columns: 54px 1fr auto;
  gap: 10px;
  align-items: center;
  padding: 8px 10px;
  border-radius: 10px;
  cursor: pointer;
  transition: background 120ms ease;
}
.row:hover { background: var(--row-hover); }
.row.active { background: var(--row-active); color: var(--row-active-text); }
.row.active .muted, .row.active .row-preview, .row.active .row-kind { color: rgba(255, 255, 255, 0.82); }
.row.active .badge { background: #fff; color: var(--row-active); }

.avatar {
  border-radius: 50%;
  display: grid;
  place-items: center;
  color: #fff;
  font-weight: 600;
  width: 54px;
  height: 54px;
  font-size: 19px;
  letter-spacing: 0.02em;
  user-select: none;
}
.avatar.chat { width: 42px; height: 42px; font-size: 16px; }
.avatar.me { width: 40px; height: 40px; font-size: 15px; }

.row-main { min-width: 0; }
.row-top { display: flex; align-items: baseline; gap: 8px; }
.row-name {
  font-weight: 600;
  font-size: 15px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.row-kind { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.row-preview {
  font-size: 13.5px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 2px;
}
.row-side { display: grid; justify-items: end; gap: 6px; }
.row-time { font-size: 12px; color: var(--muted-2); }
.badge {
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  border-radius: 10px;
  background: var(--badge);
  color: #fff;
  font-size: 12px;
  font-weight: 600;
  display: grid;
  place-items: center;
}

.list-hint { padding: 18px 14px; color: var(--muted); font-size: 13.5px; }
.check {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-top: 8px;
  font-size: 13px;
  line-height: 1.35;
  color: var(--muted);
  overflow-wrap: anywhere;
}
.check input { flex: 0 0 16px; width: 16px; height: 16px; accent-color: var(--accent); }

.sidebar-foot {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-top: 1px solid var(--border);
}
.who { flex: 1; min-width: 0; }
.who-name { font-weight: 600; font-size: 14px; }

/* ---------- панель чата ---------- */

.pane {
  position: relative;
  min-width: 0;
  display: grid;
  background:
    radial-gradient(circle at 20% 20%, var(--pane-pattern) 0 2px, transparent 2px 26px),
    radial-gradient(circle at 70% 60%, var(--pane-pattern) 0 2px, transparent 2px 30px),
    var(--pane);
  background-size: 64px 64px, 90px 90px, auto;
}

.empty { display: grid; place-items: center; }
.empty-badge {
  padding: 8px 16px;
  border-radius: 20px;
  background: rgba(0, 0, 0, 0.18);
  color: #fff;
  font-size: 13px;
  backdrop-filter: blur(4px);
}

.chat {
  display: grid;
  grid-template-rows: auto 1fr auto auto;
  /* колонка не должна расти до min-content: иначе шапка и поле ввода
     вылезают за экран на 320 px */
  grid-template-columns: minmax(0, 1fr);
  min-height: 0;
  min-width: 0;
}

.chat-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 14px;
  background: var(--head);
  border-bottom: 1px solid var(--border);
  min-height: 58px;
}
.chat-head .back { display: none; }
.chat-title { flex: 1; min-width: 0; }
.chat-title h2 {
  margin: 0;
  font-size: 15.5px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.chat-title .small { margin-top: 1px; }
.chat-actions { display: flex; gap: 2px; }

.messages {
  overflow-y: auto;
  min-width: 0;
  padding: 14px clamp(14px, 5%, 64px) 8px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-height: 0;
}

.day-divider {
  align-self: center;
  margin: 8px 0;
  padding: 3px 10px;
  border-radius: 12px;
  background: rgba(0, 0, 0, 0.18);
  color: #fff;
  font-size: 12px;
  backdrop-filter: blur(4px);
}

.bubble {
  max-width: min(62%, 560px);
  padding: 7px 11px 8px;
  border-radius: 12px;
  background: var(--bubble-in);
  box-shadow: 0 1px 1px rgba(16, 35, 47, 0.08);
  position: relative;
}
.bubble.out {
  align-self: flex-end;
  background: var(--bubble-out);
  color: var(--bubble-out-text);
  border-bottom-right-radius: 4px;
}
.bubble.in { align-self: flex-start; border-bottom-left-radius: 4px; }
.bubble .meta {
  display: flex;
  justify-content: flex-end;
  gap: 6px;
  align-items: center;
  font-size: 11px;
  color: var(--muted);
  margin-top: 3px;
}
.bubble.out .meta { color: rgba(0, 0, 0, 0.45); }
:root[data-theme="dark"] .bubble.out .meta { color: rgba(255, 255, 255, 0.6); }
.bubble .author { font-size: 12.5px; font-weight: 600; color: var(--accent); margin-bottom: 2px; }

.locked {
  align-self: center;
  max-width: 520px;
  text-align: center;
  font-size: 12.5px;
  color: var(--muted);
  background: var(--bubble-in);
  border: 1px dashed var(--border);
  border-radius: 12px;
  padding: 10px 14px;
}

.post {
  background: var(--bubble-in);
  border-radius: 12px;
  padding: 12px 14px 8px;
  box-shadow: 0 1px 2px rgba(16, 35, 47, 0.1);
  max-width: min(74%, 640px);
  align-self: flex-start;
}
.post-head { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; }
.post-title { font-weight: 600; font-size: 14px; }
.post-text { white-space: pre-wrap; word-wrap: break-word; font-size: 15px; }
.post-foot {
  display: flex;
  gap: 14px;
  justify-content: flex-end;
  align-items: center;
  font-size: 12px;
  color: var(--muted);
  margin-top: 6px;
}
.post-foot .views { display: inline-flex; align-items: center; gap: 4px; }

.composer {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  background: var(--head);
  border-top: 1px solid var(--border);
}
.composer textarea {
  flex: 1;
  min-width: 0;
  min-height: 44px;
  max-height: 160px;
  padding: 11px 16px;
  border-radius: 22px;
  border: 0;
  background: var(--row-hover);
  color: var(--text);
  resize: none;
  outline: none;
  line-height: 1.4;
}
.composer textarea::placeholder { color: var(--muted-2); }
.composer.disabled textarea { opacity: 0.6; }
.composer { align-items: flex-end; }
.send {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--accent);
  color: #fff;
  transition: background 120ms ease, transform 80ms ease;
}
.send:hover:not(:disabled) { background: var(--accent-hover); }
.send:disabled { background: var(--row-hover); color: var(--muted-2); cursor: default; }
.composer-note {
  padding: 0 16px 12px;
  background: var(--head);
  font-size: 12.5px;
  color: var(--muted);
}

/* ---------- шторка и тост ---------- */

.sheet {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  display: grid;
  place-items: center;
  padding: 20px;
  z-index: 20;
}
.sheet-card {
  width: min(460px, 100%);
  background: var(--sidebar);
  border-radius: 16px;
  box-shadow: var(--shadow);
  max-height: 80vh;
  display: grid;
  grid-template-rows: auto 1fr;
}
.sheet-card header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}
.sheet-card h3 { margin: 0; font-size: 16px; }
.sheet-body { padding: 16px; overflow-y: auto; display: grid; gap: 12px; }
.sheet-body .kv { display: flex; justify-content: space-between; gap: 12px; font-size: 14px; }
.sheet-body .kv span:first-child { color: var(--muted); }
.sheet-body code {
  display: block;
  padding: 10px 12px;
  border-radius: 10px;
  background: var(--row-hover);
  font: 13px/1.4 ui-monospace, "Cascadia Mono", Consolas, monospace;
  word-break: break-all;
}
.sheet-actions { display: flex; gap: 8px; }
.sheet-actions .btn { flex: 1; height: 40px; }

.toast {
  position: fixed;
  left: 50%;
  bottom: 26px;
  transform: translateX(-50%);
  background: rgba(23, 33, 43, 0.95);
  color: #fff;
  padding: 10px 18px;
  border-radius: 20px;
  font-size: 13.5px;
  z-index: 30;
  box-shadow: var(--shadow);
}

/* ---------- сообщения, файлы, участники ---------- */

.bubble .text { white-space: pre-wrap; overflow-wrap: anywhere; }
.ticks { font-size: 11px; letter-spacing: -2px; }
.ticks.read { color: var(--accent); }
.bubble.out .ticks.read { color: #4fae4e; }
:root[data-theme="dark"] .bubble.out .ticks.read { color: #8fd0ff; }

.service {
  align-self: center;
  max-width: 80%;
  text-align: center;
  padding: 3px 12px;
  border-radius: 12px;
  background: rgba(0, 0, 0, 0.18);
  color: #fff;
  font-size: 12.5px;
  backdrop-filter: blur(4px);
}

.more {
  align-self: center;
  padding: 4px 14px;
  border-radius: 12px;
  background: var(--accent-weak);
  color: var(--accent);
  font-size: 13px;
}

.file {
  display: flex;
  align-items: center;
  gap: 10px;
  text-align: left;
  padding: 4px 2px;
  max-width: 100%;
}
.file-icon {
  width: 40px; height: 40px; border-radius: 50%;
  display: grid; place-items: center;
  background: var(--accent); color: #fff; font-size: 18px; flex: 0 0 40px;
}
.file-info { display: grid; min-width: 0; }
.file-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.file-size { font-size: 12.5px; color: var(--muted); }
.bubble.out .file-size { color: inherit; opacity: 0.7; }
.photo { display: block; max-width: 100%; max-height: 360px; border-radius: 8px; cursor: zoom-in; }

.members { display: grid; gap: 4px; }
.member { display: flex; align-items: center; gap: 10px; padding: 6px 0; }
.member-main { flex: 1; min-width: 0; font-size: 14px; }
.member-actions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: flex-end; }
.avatar.mini { width: 34px; height: 34px; font-size: 13px; flex: 0 0 34px; }
.link { color: var(--accent); font-size: 12.5px; padding: 2px 0; }
.danger, .btn.ghost.danger { color: var(--danger); }
code.fp { display: inline; padding: 2px 6px; font-size: 12px; }

.me-btn {
  display: flex; align-items: center; gap: 10px; width: 100%;
  text-align: left; border-radius: 10px; padding: 2px;
}
.me-btn:hover { background: var(--row-hover); }
.chat-title-btn { display: flex; align-items: center; gap: 10px; flex: 1; min-width: 0; text-align: left; }

/* ---------- адаптив ---------- */

@media (max-width: 800px) {
  .app { grid-template-columns: 1fr; }
  /* на телефоне кнопки-иконки должны попадать в палец */
  .icon-btn { width: 44px; height: 44px; }
  .sidebar-head { grid-template-columns: 1fr 44px 44px; }
  .pane { display: none; }
  .app.chat-open .pane { display: grid; }
  .app.chat-open .sidebar { display: none; }
  .chat-head .back { display: grid; }
  .bubble { max-width: 82%; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
