:root {
  --bg: #f4f6f8;
  --panel: #ffffff;
  --panel-2: #f8fafb;
  --line: #e3e8ec;
  --text: #1d2a33;
  --muted: #6b7a86;
  --accent: #0e8f86;
  --accent-2: #0b746d;
  --accent-soft: #e2f4f2;
  --own: #d9f2ee;
  --other: #ffffff;
  --danger: #c6383b;
  --badge: #e0464a;
  --online: #22b35e;
  --shadow: 0 8px 30px rgba(20, 40, 50, .14);
  --radius: 12px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12181c;
    --panel: #1a2227;
    --panel-2: #1f282e;
    --line: #2b363d;
    --text: #e3eaee;
    --muted: #8d9ba5;
    --accent: #2bb3a8;
    --accent-2: #3cc7bb;
    --accent-soft: #1d3a38;
    --own: #1f4643;
    --other: #222c32;
    --shadow: 0 8px 30px rgba(0, 0, 0, .5);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 "Segoe UI", system-ui, sans-serif;
  overflow: hidden;
}

button, input, textarea, select { font: inherit; color: inherit; }

button {
  cursor: pointer;
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: 8px;
  padding: 7px 14px;
}
button:hover { background: var(--panel-2); }
button:disabled { opacity: .5; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
button.primary:hover { background: var(--accent-2); }
button.danger { color: var(--danger); }
button.wide { width: 100%; padding: 10px; }
button.link { border: none; background: none; color: var(--accent); padding: 4px; }
button.link:hover { text-decoration: underline; }
button.icon {
  border: none; background: none; padding: 7px; border-radius: 8px; color: var(--muted);
  display: inline-flex; align-items: center; justify-content: center;
}
button.icon:hover { background: var(--panel-2); color: var(--text); }
button.icon svg { width: 20px; height: 20px; }

input, textarea, select {
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: 8px;
  padding: 8px 10px;
  outline: none;
}
input:focus, textarea:focus, select:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
label input, label select { color: var(--text); font-size: 14px; }
label.check { flex-direction: row; align-items: center; gap: 8px; color: var(--text); font-size: 14px; }

.muted { color: var(--muted); }
.small { font-size: 12px; }
.error { color: var(--danger); min-height: 1.2em; margin: 0; font-size: 13px; }

/* ---------- экраны входа ---------- */

.screen { position: fixed; inset: 0; }
.screen.center { display: flex; align-items: center; justify-content: center; padding: 16px; }
.card {
  background: var(--panel);
  border-radius: 16px;
  box-shadow: var(--shadow);
  padding: 28px;
  display: flex; flex-direction: column; gap: 14px;
}
.card.narrow { width: 360px; max-width: 100%; text-align: center; }
.card.narrow label { text-align: left; }
.card h1 { font-size: 20px; margin: 0; }
.logo img { width: 64px; height: 64px; border-radius: 14px; }
.logo.big img { width: 96px; height: 96px; border-radius: 20px; opacity: .85; }

.spinner {
  width: 26px; height: 26px; margin: 0 auto;
  border: 3px solid var(--accent-soft); border-top-color: var(--accent);
  border-radius: 50%; animation: spin 1s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- основной экран ---------- */

.screen.main { display: flex; }

.sidebar {
  width: 310px; min-width: 260px;
  display: flex; flex-direction: column;
  background: var(--panel);
  border-right: 1px solid var(--line);
}

.me { display: flex; align-items: center; gap: 10px; padding: 12px; border-bottom: 1px solid var(--line); }
.me-text { flex: 1; min-width: 0; }
.me-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.me-sub { font-size: 12px; color: var(--muted); }

.search { padding: 10px 12px; }
.search input { width: 100%; background: var(--panel-2); }

.list { flex: 1; overflow-y: auto; padding: 0 6px 10px; }
.section { font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); padding: 12px 8px 4px; }

.item {
  display: flex; align-items: center; gap: 10px;
  padding: 8px; border-radius: 10px; cursor: pointer;
}
.item:hover { background: var(--panel-2); }
.item.active { background: var(--accent-soft); }
.item-main { flex: 1; min-width: 0; }
.item-top { display: flex; align-items: baseline; gap: 6px; }
.item-name { flex: 1; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item-time { font-size: 11px; color: var(--muted); flex: none; }
.item-bottom { display: flex; align-items: center; gap: 6px; }
.item-preview { flex: 1; font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.badge {
  flex: none; min-width: 20px; height: 20px; padding: 0 6px; border-radius: 10px;
  background: var(--badge); color: #fff; font-size: 12px; font-weight: 600;
  display: inline-flex; align-items: center; justify-content: center;
}

.avatar {
  position: relative; flex: none;
  width: 40px; height: 40px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  color: #fff; font-weight: 600; font-size: 15px; user-select: none;
}
.avatar.small { width: 32px; height: 32px; font-size: 12px; }
.avatar.group svg { width: 20px; height: 20px; }
.avatar.online::after {
  content: ""; position: absolute; right: 0; bottom: 0;
  width: 11px; height: 11px; border-radius: 50%;
  background: var(--online); border: 2px solid var(--panel);
}

.netstate { padding: 8px 12px; font-size: 12px; color: var(--muted); border-top: 1px solid var(--line); }
.netstate.bad { background: #fdecea; color: var(--danger); }
@media (prefers-color-scheme: dark) { .netstate.bad { background: #3a1f20; } }

/* ---------- чат ---------- */

.chat { flex: 1; min-width: 0; display: flex; flex-direction: column; position: relative; }
.placeholder { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; color: var(--muted); }

.chat-head {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 18px; background: var(--panel); border-bottom: 1px solid var(--line);
}
.chat-title { font-weight: 600; font-size: 15px; }
.chat-sub { font-size: 12px; color: var(--muted); }
.chat-sub.typing { color: var(--accent); }

.messages { flex: 1; overflow-y: auto; padding: 14px 18px 8px; }
.more { text-align: center; padding: 6px; color: var(--muted); font-size: 12px; }
.day { text-align: center; margin: 14px 0 8px; }
.day span { background: var(--panel); border: 1px solid var(--line); color: var(--muted); font-size: 12px; padding: 2px 10px; border-radius: 10px; }

.msg { display: flex; align-items: flex-end; gap: 8px; margin-top: 8px; }
.msg.cont { margin-top: 2px; }
.msg.own { justify-content: flex-end; }
.msg .avatar-slot { width: 32px; flex: none; }
.bubble {
  max-width: min(640px, 75%);
  background: var(--other);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 6px 10px 4px;
  box-shadow: 0 1px 1px rgba(0, 0, 0, .03);
  user-select: text;
}
.msg.own .bubble { background: var(--own); border-color: transparent; }
.sender { font-size: 12px; font-weight: 600; margin-bottom: 1px; }
.text { white-space: pre-wrap; word-wrap: break-word; overflow-wrap: anywhere; }
.text a { color: var(--accent); }
.deleted { font-style: italic; color: var(--muted); }
.meta { float: right; margin: 4px 0 0 10px; font-size: 11px; color: var(--muted); user-select: none; }
.meta .ticks { margin-left: 3px; }
.meta .ticks.read { color: var(--accent); }

.file { display: flex; align-items: center; gap: 10px; padding: 4px 0; min-width: 220px; }
.file-icon {
  width: 38px; height: 38px; border-radius: 8px; background: var(--accent-soft); color: var(--accent);
  display: flex; align-items: center; justify-content: center; flex: none;
}
.file-icon svg { width: 20px; height: 20px; }
.file-info { flex: 1; min-width: 0; }
.file-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.file-size { font-size: 12px; color: var(--muted); }
.file button { padding: 3px 8px; font-size: 12px; }
.image { display: block; max-width: 100%; max-height: 320px; border-radius: 8px; margin: 2px 0 4px; cursor: zoom-in; background: var(--panel-2); min-width: 120px; min-height: 80px; }

.composer { background: var(--panel); border-top: 1px solid var(--line); padding: 8px 12px; }
.composer-row { display: flex; align-items: flex-end; gap: 6px; }
.composer textarea {
  flex: 1; resize: none; max-height: 180px; min-height: 40px;
  background: var(--panel-2); border-radius: 12px; line-height: 1.4; overflow-y: hidden;
}
button.send { color: var(--accent); }
.attachments { display: flex; flex-wrap: wrap; gap: 6px; }
.attachments:not(:empty) { padding-bottom: 8px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  background: var(--accent-soft); border-radius: 8px; padding: 4px 6px 4px 10px; font-size: 13px; max-width: 280px;
}
.chip span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chip button { border: none; background: none; padding: 0 4px; color: var(--muted); }

.drop {
  position: absolute; inset: 10px; border: 3px dashed var(--accent); border-radius: 16px;
  background: color-mix(in srgb, var(--accent-soft) 85%, transparent);
  display: flex; align-items: center; justify-content: center;
  font-size: 18px; font-weight: 600; color: var(--accent); pointer-events: none;
}

/* ---------- окна, меню, подсказки ---------- */

.modal-back {
  position: fixed; inset: 0; background: rgba(10, 20, 25, .45);
  display: flex; align-items: center; justify-content: center; padding: 20px; z-index: 10;
}
.modal {
  background: var(--panel); border-radius: 16px; box-shadow: var(--shadow);
  width: 560px; max-width: 100%; max-height: 100%; overflow: auto;
  padding: 22px; display: flex; flex-direction: column; gap: 12px;
}
.modal.wide { width: 820px; }
.modal h2 { margin: 0; font-size: 18px; }
.modal h3 { margin: 6px 0 0; font-size: 14px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }
.row { display: flex; gap: 10px; }
.row > * { flex: 1; }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); }
.tabs button { border: none; border-radius: 8px 8px 0 0; background: none; color: var(--muted); }
.tabs button.on { color: var(--accent); box-shadow: inset 0 -2px 0 var(--accent); font-weight: 600; }

table { width: 100%; border-collapse: collapse; }
th { text-align: left; font-size: 12px; color: var(--muted); font-weight: 600; padding: 6px; border-bottom: 1px solid var(--line); }
td { padding: 6px; border-bottom: 1px solid var(--line); vertical-align: middle; }
td.actions { text-align: right; white-space: nowrap; }
td.actions button { padding: 3px 8px; font-size: 12px; }
tr.off td { color: var(--muted); }
.pill { font-size: 11px; padding: 1px 7px; border-radius: 8px; background: var(--panel-2); border: 1px solid var(--line); }
.pill.on { color: var(--online); border-color: currentColor; }

.checklist { max-height: 240px; overflow: auto; border: 1px solid var(--line); border-radius: 8px; padding: 6px 10px; display: grid; grid-template-columns: 1fr 1fr; gap: 4px 12px; }
.code { font-family: Consolas, monospace; font-size: 12px; word-break: break-all; }
textarea.code { width: 100%; }

.menu {
  position: fixed; z-index: 20; background: var(--panel); border: 1px solid var(--line);
  border-radius: 10px; box-shadow: var(--shadow); padding: 4px; min-width: 170px;
}
.menu button { display: block; width: 100%; text-align: left; border: none; background: none; padding: 7px 12px; }
.menu button:hover { background: var(--panel-2); }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 30;
  background: #263238; color: #fff; padding: 10px 18px; border-radius: 10px; box-shadow: var(--shadow); max-width: 80%;
}

/* ---------- веб-версия и телефоны ---------- */

button.back { display: none; margin-left: -8px; }
button.back svg { width: 24px; height: 24px; }
.push-box { border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; display: flex; flex-direction: column; gap: 8px; font-size: 13px; }
.push-box .row { align-items: center; }

@media (max-width: 720px) {
  html, body { font-size: 15px; }
  .screen.main { display: block; }
  .sidebar { width: 100%; min-width: 0; height: 100%; border-right: none; }
  .chat { position: absolute; inset: 0; display: none; background: var(--bg); }
  body.in-chat .sidebar { display: none; }
  body.in-chat .chat { display: flex; }
  button.back { display: inline-flex; }
  .chat-head { padding: 8px 10px; padding-top: max(8px, env(safe-area-inset-top)); gap: 8px; }
  .me { padding-top: max(12px, env(safe-area-inset-top)); }
  .messages { padding: 10px 10px 6px; }
  .bubble { max-width: 85%; }
  .composer { padding: 6px 8px; padding-bottom: max(6px, env(safe-area-inset-bottom)); }
  .composer textarea { font-size: 16px; } /* иначе iPhone увеличивает страницу при вводе */
  input, select, textarea { font-size: 16px; }
  .netstate { padding-bottom: max(8px, env(safe-area-inset-bottom)); }
  .modal-back { padding: 0; align-items: stretch; }
  .modal, .modal.wide { width: 100%; max-height: 100%; border-radius: 0; padding: 18px 16px; padding-top: max(18px, env(safe-area-inset-top)); }
  .row { flex-direction: column; }
  .checklist { grid-template-columns: 1fr; }
  table, tbody, tr, td { display: block; }
  table tr:first-child { display: none; }
  table tr { border-bottom: 1px solid var(--line); padding: 6px 0; }
  table td { border: none; padding: 2px 4px; }
  td.actions { text-align: left; }
  .placeholder { display: none; }
}

/* Телефоны: iPhone увеличивает страницу при нажатии на поле со шрифтом меньше 16px
   и не возвращает масштаб обратно — поэтому все поля на сенсорных экранах 16px. */
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
body { touch-action: manipulation; }
@media (pointer: coarse), (max-width: 720px) {
  input, select, textarea,
  label input, label select, label textarea,
  .search input, .composer textarea { font-size: 16px; }
}
.chat-head > div:not(.avatar) { min-width: 0; }
.chat-title, .chat-sub { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ---------- смайлики Twemoji и реакции ---------- */

img.emoji { width: 1.25em; height: 1.25em; vertical-align: -0.25em; margin: 0 0.05em; }
.bubble.jumbo .text { font-size: 40px; line-height: 1.15; }
.bubble.jumbo { background: transparent; border-color: transparent; box-shadow: none; padding-left: 2px; padding-right: 2px; }
.msg.own .bubble.jumbo { background: transparent; }

.reactions { display: flex; flex-wrap: wrap; gap: 4px; margin: 6px 0 2px; clear: both; }
.reaction {
  display: inline-flex; align-items: center; gap: 4px; padding: 2px 8px; border-radius: 12px;
  background: var(--panel-2); border: 1px solid var(--line); font-size: 12px; line-height: 18px;
}
.reaction img.emoji { width: 16px; height: 16px; vertical-align: 0; margin: 0; }
.reaction.mine { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); font-weight: 600; }

.menu-reactions { display: flex; gap: 2px; padding: 2px 2px 6px; border-bottom: 1px solid var(--line); margin-bottom: 4px; }
.menu .menu-reactions button { width: 36px; height: 36px; padding: 0; display: flex; align-items: center; justify-content: center; border-radius: 8px; }
.menu-reactions img.emoji { width: 24px; height: 24px; margin: 0; vertical-align: 0; }
.menu .menu-reactions button.more { font-size: 20px; color: var(--muted); }

.emoji-picker {
  position: fixed; z-index: 25; width: 360px; height: 400px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px; box-shadow: var(--shadow);
  display: flex; flex-direction: column; overflow: hidden;
}
.emoji-picker.sheet { left: 0 !important; right: 0; bottom: 0; top: auto !important; width: auto; height: 46vh; border-radius: 16px 16px 0 0; padding-bottom: env(safe-area-inset-bottom); }
.ep-search { margin: 10px 10px 6px; }
.ep-tabs { display: flex; padding: 0 6px; border-bottom: 1px solid var(--line); overflow-x: auto; flex: none; }
.ep-tab { border: none; background: none; padding: 6px; border-radius: 8px; flex: none; }
.ep-tab:hover { background: var(--panel-2); }
.ep-tab .ep-img { width: 20px; height: 20px; }
.ep-body { flex: 1; overflow-y: auto; padding: 4px 8px 10px; position: relative; }
.ep-title { font-size: 12px; color: var(--muted); padding: 8px 4px 4px; }
.ep-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(38px, 1fr)); }
.ep-item { border: none; background: none; padding: 5px; border-radius: 8px; aspect-ratio: 1; display: flex; align-items: center; justify-content: center; }
.ep-item:hover { background: var(--panel-2); }
.ep-item .ep-img { width: 26px; height: 26px; }
.ep-empty { padding: 30px; text-align: center; color: var(--muted); }

/* На телефоне долгое нажатие открывает наше меню, а не системное «Скопировать/Выделить». */
@media (pointer: coarse) {
  .bubble { -webkit-touch-callout: none; -webkit-user-select: none; user-select: none; }
}

/* Кнопка смайликов внутри поля ввода, справа — как в Telegram. */
.input-wrap { flex: 1; position: relative; display: flex; min-width: 0; }
.input-wrap textarea { flex: 1; padding-right: 42px; }
.input-wrap #btn-emoji { position: absolute; right: 4px; bottom: 4px; padding: 6px; }
