/* ═══════════════ Темы оформления ═══════════════
   Базовые (нейтральные) цвета + акцентная палитра темы.
   Тема — атрибут data-theme, тёмный режим — атрибут data-dark (ставит app.js). */
:root {
  --panel: #ffffff;
  --text: #1f2320;
  --muted: #7a7d78;
  --bubble-in: #ffffff;
  --danger: #c4452f;
  --shadow: 0 1px 2px rgba(40, 30, 10, .08);
  --radius: 16px;
  --badge: var(--accent);
  color-scheme: light;
}
[data-dark] {
  --panel: #1c201f;
  --text: #e8ebe9;
  --muted: #8f9794;
  --bubble-in: #262c2a;
  --danger: #ef7a62;
  --shadow: 0 1px 2px rgba(0, 0, 0, .35);
  color-scheme: dark;
}

/* Мята — исходная тема */
:root, [data-theme="mint"] {
  --accent: #1f8a70; --accent-strong: #176c58; --accent-soft: #e3f3ee;
  --bg: #f4f1ec; --panel-2: #faf8f5; --border: #e6e1d8;
  --bubble-out: #d9f2e9; --bubble-out-text: #13332a; --chat-bg: #ece6dc;
}
[data-dark], [data-theme="mint"][data-dark] {
  --accent: #3cbf9b; --accent-strong: #5ad3b1; --accent-soft: #1d3a33;
  --bg: #141716; --panel: #1c201f; --panel-2: #222726; --border: #2d3331;
  --bubble-in: #262c2a; --bubble-out: #1f4a3e; --bubble-out-text: #e4f6ef; --chat-bg: #101312;
}

/* Лаванда */
[data-theme="lavender"] {
  --accent: #7c5cc4; --accent-strong: #63459f; --accent-soft: #efe9fa;
  --bg: #f4f2f7; --panel-2: #faf9fc; --border: #e4dfec;
  --bubble-out: #e7ddf8; --bubble-out-text: #2e2148; --chat-bg: #e8e3ef;
}
[data-theme="lavender"][data-dark] {
  --accent: #a98ef0; --accent-strong: #c0aaf5; --accent-soft: #2c2540;
  --bg: #16141b; --panel: #1e1b24; --panel-2: #25212c; --border: #322d3b;
  --bubble-in: #2a2632; --bubble-out: #3e3260; --bubble-out-text: #efe9fb; --chat-bg: #121016;
}

/* Терракота */
[data-theme="terracotta"] {
  --accent: #c25f37; --accent-strong: #a04c29; --accent-soft: #f8e9e1;
  --bg: #f6f0ea; --panel-2: #fbf8f5; --border: #eaded3;
  --bubble-out: #f6dccd; --bubble-out-text: #4a2414; --chat-bg: #efe4d8;
}
[data-theme="terracotta"][data-dark] {
  --accent: #e48a62; --accent-strong: #f0a682; --accent-soft: #3a261d;
  --bg: #181412; --panel: #201a17; --panel-2: #28211d; --border: #362c26;
  --bubble-in: #2c2420; --bubble-out: #5a3423; --bubble-out-text: #fbeae1; --chat-bg: #120f0d;
}

/* Море */
[data-theme="ocean"] {
  --accent: #2f74c0; --accent-strong: #235c9c; --accent-soft: #e3eefa;
  --bg: #eef2f6; --panel-2: #f8fafc; --border: #dde4ec;
  --bubble-out: #d6e7fa; --bubble-out-text: #12304f; --chat-bg: #e1e8f0;
}
[data-theme="ocean"][data-dark] {
  --accent: #5aa2ec; --accent-strong: #7db7f2; --accent-soft: #1b2b3e;
  --bg: #12161b; --panel: #191f26; --panel-2: #1f262f; --border: #2a333e;
  --bubble-in: #232b35; --bubble-out: #1f4268; --bubble-out-text: #e3f0fd; --chat-bg: #0e1216;
}

/* Пион */
[data-theme="rose"] {
  --accent: #c2456e; --accent-strong: #9f3558; --accent-soft: #fae6ed;
  --bg: #f7f0f2; --panel-2: #fcf8f9; --border: #ecdde2;
  --bubble-out: #f8dbe5; --bubble-out-text: #4b1529; --chat-bg: #f0e3e7;
}
[data-theme="rose"][data-dark] {
  --accent: #ec7a9f; --accent-strong: #f39ab7; --accent-soft: #3b1f2a;
  --bg: #181316; --panel: #21191d; --panel-2: #291f24; --border: #382a31;
  --bubble-in: #2c2227; --bubble-out: #5e2a3e; --bubble-out-text: #fde6ee; --chat-bg: #120e10;
}

/* Олива */
[data-theme="olive"] {
  --accent: #66842c; --accent-strong: #516a21; --accent-soft: #eef3e1;
  --bg: #f3f2ea; --panel-2: #faf9f4; --border: #e3e1d3;
  --bubble-out: #e2ecc9; --bubble-out-text: #2b3812; --chat-bg: #e9e7da;
}
[data-theme="olive"][data-dark] {
  --accent: #a3c25e; --accent-strong: #bcd67f; --accent-soft: #2a3320;
  --bg: #151612; --panel: #1d1f19; --panel-2: #24271f; --border: #30342a;
  --bubble-in: #272a22; --bubble-out: #3e4c22; --bubble-out-text: #eef5dc; --chat-bg: #10110d;
}

/* Цвет текста на кнопках с акцентом: в тёмных темах акцент светлый */
:root { --on-accent: #fff; }
[data-dark] { --on-accent: #0f1412; }

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

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }

svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 40px; height: 40px;
  flex: none;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--muted);
}
.icon-btn:hover { background: var(--panel-2); color: var(--text); }

.btn {
  border: 1px solid var(--border);
  background: var(--panel);
  border-radius: 12px;
  padding: 10px 16px;
  font-weight: 500;
}
.btn:hover { background: var(--panel-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn.primary:hover { background: var(--accent-strong); }
.btn.danger { color: var(--danger); }
.btn.wide { width: 100%; }
.btn:disabled { opacity: .6; cursor: default; }

/* ───── Вход ───── */
.auth {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 24px 16px;
}
.auth-card {
  width: 100%;
  max-width: 380px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 24px;
  padding: 32px 28px 28px;
  box-shadow: 0 10px 40px rgba(40, 30, 10, .08);
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.auth-logo { align-self: center; }
.auth-card h1 { margin: 0; text-align: center; font-size: 24px; }
.auth-sub { margin: -10px 0 4px; text-align: center; color: var(--muted); }
.tabs { display: grid; grid-template-columns: 1fr 1fr; background: var(--panel-2); border-radius: 12px; padding: 4px; }
.tab { border: 0; background: transparent; padding: 8px; border-radius: 9px; color: var(--muted); font-weight: 500; }
.tab.active { background: var(--panel); color: var(--text); box-shadow: var(--shadow); }
.auth-card:not(.register) .reg-only { display: none; }

.field { display: flex; flex-direction: column; gap: 6px; }
.field > span { font-size: 13px; color: var(--muted); }
.field input, .field textarea, .search, .modal input[type="text"], .modal input[type="password"] {
  width: 100%;
  border: 1px solid var(--border);
  background: var(--panel-2);
  border-radius: 12px;
  padding: 11px 13px;
  outline: none;
}
.field input:focus, .search:focus, .modal input:focus { border-color: var(--accent); background: var(--panel); }
.form-error { color: var(--danger); font-size: 14px; min-height: 0; }
.form-error:empty { display: none; }

/* ───── Раскладка ───── */
.app {
  display: grid;
  grid-template-columns: 340px 1fr;
  height: 100vh;
  height: 100dvh;
}
.sidebar {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--panel);
  border-right: 1px solid var(--border);
}
.sidebar-head {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 10px 10px 8px;
  padding-top: max(10px, env(safe-area-inset-top));
}
.search { padding: 9px 13px; border-radius: 20px; }
.conn { text-align: center; font-size: 13px; color: var(--muted); padding: 4px; background: var(--panel-2); }

.chat-list { list-style: none; margin: 0; padding: 4px 6px 12px; overflow-y: auto; flex: 1; }
.chat-item {
  display: flex;
  gap: 12px;
  align-items: center;
  padding: 9px 10px;
  border-radius: 14px;
  cursor: pointer;
}
.chat-item:hover { background: var(--panel-2); }
.chat-item.active { background: var(--accent-soft); }
.ci-body { flex: 1; min-width: 0; }
.ci-top, .ci-bottom { display: flex; align-items: baseline; gap: 8px; }
.ci-title { flex: 1; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ci-time { font-size: 12px; color: var(--muted); flex: none; }
.ci-preview { flex: 1; color: var(--muted); font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ci-preview.typing { color: var(--accent); }
.badge {
  flex: none;
  min-width: 20px; height: 20px;
  padding: 0 6px;
  border-radius: 10px;
  background: var(--badge);
  color: var(--on-accent);
  font-size: 12px;
  font-weight: 600;
  display: grid;
  place-items: center;
}
.list-empty { color: var(--muted); text-align: center; padding: 32px 16px; font-size: 14px; white-space: pre-line; }

.avatar {
  position: relative;
  flex: none;
  width: 46px; height: 46px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  color: #fff;
  font-weight: 600;
  font-size: 16px;
  letter-spacing: .5px;
  user-select: none;
}
.avatar.sm { width: 38px; height: 38px; font-size: 14px; }
.avatar.online::after {
  content: "";
  position: absolute;
  right: 0; bottom: 0;
  width: 12px; height: 12px;
  border-radius: 50%;
  background: #34c759;
  border: 2px solid var(--panel);
}

/* ───── Чат ───── */
.main { position: relative; min-width: 0; min-height: 0; background: var(--chat-bg); }
.empty {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  text-align: center;
  gap: 8px;
}
.empty img { opacity: .5; }
.chat { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.chat-head {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 8px 10px;
  padding-top: max(8px, env(safe-area-inset-top));
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.chat-head .back { display: none; }
.chat-head-main {
  display: flex;
  align-items: center;
  gap: 12px;
  flex: 1;
  min-width: 0;
  border: 0;
  background: none;
  text-align: left;
  padding: 2px 6px;
  border-radius: 12px;
}
.chat-head-text { display: flex; flex-direction: column; min-width: 0; }
.chat-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-sub { font-size: 13px; color: var(--muted); }
.chat-sub.typing, .chat-sub.online { color: var(--accent); }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 12px 16px 8px;
  display: flex;
  flex-direction: column;
  outline: none;
  overscroll-behavior: contain;
}
.messages > :first-child { margin-top: auto; }
.loading { text-align: center; color: var(--muted); padding: 24px; }

.day-sep { align-self: center; margin: 12px 0 8px; }
.day-sep span {
  display: inline-block;
  padding: 3px 12px;
  border-radius: 12px;
  background: color-mix(in srgb, var(--panel) 80%, transparent);
  color: var(--muted);
  font-size: 13px;
  box-shadow: var(--shadow);
}

.msg { display: flex; margin-top: 8px; max-width: min(78%, 560px); }
.msg.cont { margin-top: 2px; }
.msg.mine { align-self: flex-end; }
.bubble {
  position: relative;
  background: var(--bubble-in);
  border-radius: var(--radius);
  border-bottom-left-radius: 6px;
  padding: 7px 11px 6px;
  box-shadow: var(--shadow);
  min-width: 64px;
  overflow-wrap: anywhere;
}
.msg.cont .bubble { border-top-left-radius: 6px; }
.msg.mine .bubble {
  background: var(--bubble-out);
  color: var(--bubble-out-text);
  border-radius: var(--radius);
  border-bottom-right-radius: 6px;
}
.msg.mine.cont .bubble { border-top-right-radius: 6px; }
.author { font-size: 13px; font-weight: 600; margin-bottom: 2px; }
.text { white-space: pre-wrap; }
.text a, .bubble a.link { color: var(--accent); text-decoration: underline; text-underline-offset: 2px; }
.msg.mine .text a { color: inherit; }
.meta {
  float: right;
  margin: 6px 0 -4px 10px;
  font-size: 11.5px;
  color: var(--muted);
  display: inline-flex;
  gap: 3px;
  align-items: center;
  user-select: none;
}
.msg.mine .meta { color: color-mix(in srgb, var(--bubble-out-text) 55%, transparent); }
.tick { font-size: 12px; letter-spacing: -3px; padding-right: 3px; }
.tick.read { color: var(--accent); }
.msg.mine .tick.read { color: var(--accent-strong); }

.att-img {
  display: block;
  max-width: 100%;
  max-height: 340px;
  border-radius: 10px;
  margin: 2px -5px 4px;
  max-inline-size: calc(100% + 10px);
  cursor: zoom-in;
  background: var(--panel-2);
}
.att-video { display: block; max-width: 100%; max-height: 360px; border-radius: 10px; margin: 2px 0 4px; }
.bubble audio { display: block; max-width: 260px; margin: 2px 0 4px; }
.att-file {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 2px 6px;
  color: inherit;
  text-decoration: none;
}
.att-icon {
  width: 40px; height: 40px;
  border-radius: 10px;
  display: grid;
  place-items: center;
  background: var(--accent);
  color: var(--on-accent);
  flex: none;
}
.att-icon svg { width: 20px; height: 20px; }
.att-info { display: flex; flex-direction: column; min-width: 0; }
.att-name { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 260px; }
.att-size { font-size: 12px; color: var(--muted); }

.to-bottom {
  position: absolute;
  right: 18px;
  bottom: 84px;
  width: 42px; height: 42px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--muted);
  box-shadow: var(--shadow);
  display: grid;
  place-items: center;
}

.upload-bar { padding: 6px 16px; font-size: 13px; color: var(--muted); background: var(--panel); border-top: 1px solid var(--border); }

.composer {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  padding: 8px 10px;
  padding-bottom: max(8px, env(safe-area-inset-bottom));
  background: var(--panel);
  border-top: 1px solid var(--border);
}
.composer textarea {
  flex: 1;
  resize: none;
  border: 1px solid var(--border);
  background: var(--panel-2);
  border-radius: 20px;
  padding: 9px 14px;
  max-height: 160px;
  line-height: 1.4;
  outline: none;
}
.composer textarea:focus { border-color: var(--accent); }
.send-btn {
  width: 40px; height: 40px;
  border: 0;
  border-radius: 50%;
  background: var(--accent);
  color: var(--on-accent);
  display: grid;
  place-items: center;
  flex: none;
}
.send-btn:hover { background: var(--accent-strong); }

/* ───── Окна ───── */
.modal {
  border: 0;
  padding: 0;
  border-radius: 20px;
  background: var(--panel);
  color: var(--text);
  width: min(440px, calc(100vw - 32px));
  max-height: min(640px, calc(100dvh - 48px));
  box-shadow: 0 20px 60px rgba(0, 0, 0, .25);
}
.modal::backdrop { background: rgba(20, 15, 5, .45); }
#modal-body { display: flex; flex-direction: column; max-height: inherit; }
.modal-head { display: flex; align-items: center; gap: 8px; padding: 14px 12px 8px 20px; }
.modal-head h2 { flex: 1; margin: 0; font-size: 18px; }
.modal-content { padding: 4px 20px 20px; overflow-y: auto; display: flex; flex-direction: column; gap: 14px; }
.modal-section { display: flex; flex-direction: column; gap: 8px; }
.modal-section h3 { margin: 4px 0 0; font-size: 13px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.row { display: flex; gap: 8px; align-items: center; }
.row > input { flex: 1; }
.hint { font-size: 13px; color: var(--muted); margin: 0; }

.user-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; }
.user-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px;
  border-radius: 12px;
  cursor: pointer;
}
.user-row:hover { background: var(--panel-2); }
.user-row .u-text { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.user-row .u-name { font-weight: 500; }
.user-row .u-sub { font-size: 13px; color: var(--muted); }
.user-row input[type="checkbox"] { width: 20px; height: 20px; accent-color: var(--accent); }
.user-row.static { cursor: default; }
.user-row.static:hover { background: none; }

.profile { display: flex; flex-direction: column; align-items: center; gap: 6px; text-align: center; padding: 8px 0; }
.profile .avatar { width: 84px; height: 84px; font-size: 28px; }
.profile .p-name { font-size: 20px; font-weight: 600; }
.profile .p-sub { color: var(--muted); }
.profile-edit .profile { padding: 0 0 4px; }
.profile-edit .profile .avatar { transition: background-color .2s ease; }
.modal .field input { background: var(--panel-2); }

.lightbox {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(0, 0, 0, .9);
  display: grid;
  place-items: center;
  cursor: zoom-out;
}
.lightbox img { max-width: 96vw; max-height: 94dvh; object-fit: contain; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(90px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: #222;
  color: #fff;
  padding: 10px 16px;
  border-radius: 12px;
  z-index: 60;
  max-width: calc(100vw - 32px);
  font-size: 14px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, .25);
}

/* ───── Телефон ───── */
@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .main { display: none; }
  .app.chat-open .sidebar { display: none; }
  .app.chat-open .main { display: block; }
  .chat-head .back { display: inline-grid; }
  .msg { max-width: 86%; }
  .messages { padding: 10px 10px 6px; }
  .sidebar { border-right: 0; }
}

/* ───── Выбор темы ───── */
.seg { display: grid; grid-template-columns: repeat(3, 1fr); background: var(--panel-2); border-radius: 12px; padding: 4px; }
.seg button { border: 0; background: transparent; padding: 7px 4px; border-radius: 9px; color: var(--muted); font-weight: 500; font-size: 14px; }
.seg button.active { background: var(--panel); color: var(--text); box-shadow: var(--shadow); }

.theme-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.theme-card {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 0;
  border: 2px solid transparent;
  border-radius: 14px;
  background: none;
  text-align: center;
  color: var(--text);
}
.theme-card .tc-preview {
  display: flex;
  flex-direction: column;
  gap: 5px;
  padding: 10px 8px;
  border-radius: 12px;
  background: var(--chat-bg);
  border: 1px solid var(--border);
}
.tc-bubble { height: 12px; border-radius: 6px; box-shadow: var(--shadow); }
.tc-bubble.in { width: 62%; background: var(--bubble-in); }
.tc-bubble.out { width: 70%; align-self: flex-end; background: var(--bubble-out); }
.tc-bar { display: flex; align-items: center; gap: 5px; margin-top: 2px; }
.tc-bar span:first-child { flex: 1; height: 10px; border-radius: 5px; background: var(--panel); }
.tc-bar span:last-child { width: 14px; height: 14px; border-radius: 50%; background: var(--accent); }
.theme-card .tc-name { font-size: 13px; padding-bottom: 4px; }
.theme-card.active { border-color: var(--accent); }
.theme-card.active .tc-name { font-weight: 600; }

/* ═══════════════ Анимации ═══════════════ */
:root {
  --ease-out: cubic-bezier(.2, .8, .2, 1);
  --ease-spring: cubic-bezier(.34, 1.4, .64, 1);
}

@keyframes fade-in { from { opacity: 0; } }
@keyframes fade-out { to { opacity: 0; } }
@keyframes rise-in { from { opacity: 0; transform: translateY(12px); } }
@keyframes pop-in { from { opacity: 0; transform: scale(.92); } }
@keyframes pop { 0% { transform: scale(1); } 40% { transform: scale(1.35); } 100% { transform: scale(1); } }

/* Плавная смена темы */
html.theme-anim,
html.theme-anim *,
html.theme-anim *::before,
html.theme-anim *::after {
  transition: background-color .35s ease, border-color .35s ease, color .35s ease, fill .35s ease, box-shadow .35s ease !important;
}

/* Вход и появление приложения */
.auth:not([hidden]) .auth-card { animation: rise-in .5s var(--ease-out) both; }
.auth-logo { animation: pop-in .6s var(--ease-spring) .1s both; }
.auth-card .reg-only { animation: rise-in .3s var(--ease-out) both; }
.form-error:not(:empty) { animation: shake .4s ease; }
@keyframes shake {
  20%, 60% { transform: translateX(-5px); }
  40%, 80% { transform: translateX(5px); }
}
.tab { transition: background-color .2s ease, color .2s ease, box-shadow .2s ease; }
.app:not([hidden]) { animation: fade-in .35s ease both; }

/* Кнопки — мягкий отклик на нажатие */
.icon-btn, .btn, .send-btn, .chat-head-main, .to-bottom, .theme-card, .seg button {
  transition: background-color .18s ease, color .18s ease, transform .15s var(--ease-out), box-shadow .18s ease, border-color .18s ease;
}
.icon-btn:active, .send-btn:active, .to-bottom:active { transform: scale(.88); }
.btn:active, .theme-card:active { transform: scale(.97); }
.send-btn:hover { transform: scale(1.06); }
.send-btn:hover:active { transform: scale(.9); }
.chat-head-main:hover { background: var(--panel-2); }
.field input, .search, .composer textarea, .modal input {
  transition: border-color .2s ease, background-color .2s ease, box-shadow .2s ease;
}
.field input:focus, .search:focus, .composer textarea:focus, .modal input:focus {
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent);
}

/* Список чатов */
.chat-item { transition: background-color .18s ease, transform .15s var(--ease-out); }
.chat-item:active { transform: scale(.985); }
.badge.pop { animation: pop .4s var(--ease-spring); }
.conn:not([hidden]) { animation: rise-in .3s var(--ease-out) both; }

/* «печатает» с бегущими точками */
.dots { display: inline-flex; gap: 2px; margin-left: 3px; vertical-align: middle; }
.dots i {
  width: 4px; height: 4px;
  border-radius: 50%;
  background: currentColor;
  animation: dot 1.2s ease-in-out infinite;
}
.dots i:nth-child(2) { animation-delay: .15s; }
.dots i:nth-child(3) { animation-delay: .3s; }
@keyframes dot {
  0%, 60%, 100% { opacity: .3; transform: translateY(0); }
  30% { opacity: 1; transform: translateY(-3px); }
}
.chat-sub { transition: color .2s ease; }

/* Открытие чата */
.messages.enter { animation: fade-in .3s ease both; }
.messages.enter .msg { animation: rise-in .35s var(--ease-out) both; }
.empty:not([hidden]).fade-in { animation: fade-in .35s ease both; }
.chat:not([hidden]) .chat-head, .chat:not([hidden]) .composer { animation: fade-in .25s ease both; }

/* Новое сообщение */
.msg.appear { animation: msg-in .38s var(--ease-spring) both; transform-origin: bottom left; }
.msg.mine.appear { transform-origin: bottom right; }
@keyframes msg-in {
  from { opacity: 0; transform: translateY(14px) scale(.9); }
}
.day-sep.appear { animation: fade-in .3s ease both; }

/* Удаление сообщения */
.msg.removing {
  overflow: hidden;
  animation: msg-out .35s var(--ease-out) forwards;
}
@keyframes msg-out {
  40% { opacity: 0; transform: scale(.85); }
  100% { opacity: 0; transform: scale(.85); height: 0; margin-top: 0; }
}

/* Галочки и вложения */
.tick.pop { display: inline-block; animation: pop .45s var(--ease-spring); }
.att-img { opacity: 0; transition: opacity .35s ease; }
.att-img.loaded { opacity: 1; }
.att-file:hover .att-icon { transform: translateY(-1px); }
.att-icon { transition: transform .2s var(--ease-out); }
.upload-bar:not([hidden]) { animation: rise-in .25s var(--ease-out) both; }
.upload-bar::before {
  content: "";
  display: inline-block;
  width: 10px; height: 10px;
  margin-right: 8px;
  border: 2px solid var(--accent);
  border-right-color: transparent;
  border-radius: 50%;
  vertical-align: -1px;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.to-bottom:not([hidden]) { animation: pop-in .25s var(--ease-spring) both; }

/* Окна */
.modal[open] { animation: modal-in .32s var(--ease-spring) both; }
.modal[open]::backdrop { animation: fade-in .25s ease both; }
.modal.closing { animation: modal-out .22s ease forwards; }
.modal.closing::backdrop { animation: fade-out .22s ease forwards; }
@keyframes modal-in { from { opacity: 0; transform: translateY(16px) scale(.96); } }
@keyframes modal-out { to { opacity: 0; transform: translateY(10px) scale(.97); } }
#modal-body.swap .modal-content { animation: fade-in .25s ease both; }
.user-row { transition: background-color .15s ease; }
.theme-card:hover .tc-preview { transform: translateY(-2px); box-shadow: 0 4px 14px rgba(0, 0, 0, .08); }
.tc-preview { transition: transform .2s var(--ease-out), box-shadow .2s ease; }
.theme-card.active .tc-bar span:last-child { animation: pop .45s var(--ease-spring); }

/* Просмотр фото */
.lightbox.in { animation: fade-in .25s ease both; }
.lightbox.in img { animation: zoom-in .35s var(--ease-out) both; }
.lightbox.out { animation: fade-out .2s ease forwards; }
.lightbox.out img { animation: zoom-out .2s ease forwards; }
@keyframes zoom-in {
  from { opacity: 0; transform: translate(var(--from-x, 0), var(--from-y, 0)) scale(var(--from-s, .8)); }
}
@keyframes zoom-out { to { opacity: 0; transform: scale(.92); } }

/* Всплывающие подсказки */
.toast.in { animation: toast-in .35s var(--ease-spring) both; }
.toast.out { animation: toast-out .25s ease forwards; }
@keyframes toast-in { from { opacity: 0; transform: translate(-50%, 16px) scale(.95); } }
@keyframes toast-out { to { opacity: 0; transform: translate(-50%, 10px); } }

/* Телефон: чат выезжает справа, список возвращается слева */
@media (max-width: 760px) {
  .main.slide-in { animation: slide-in .32s var(--ease-out) both; }
  .main.slide-out { animation: slide-out .26s ease-in forwards; }
  .sidebar.slide-back { animation: slide-back .3s var(--ease-out) both; }
  @keyframes slide-in { from { transform: translateX(100%); } }
  @keyframes slide-out { to { transform: translateX(100%); } }
  @keyframes slide-back { from { transform: translateX(-24%); opacity: .4; } }
  .app { overflow: hidden; }
}

/* Кто попросил систему уменьшить движение — без анимаций */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .01ms !important;
  }
  .att-img { opacity: 1; }
}

/* ───── Аватары с фото ───── */
.avatar { overflow: visible; }
.avatar img {
  width: 100%; height: 100%;
  border-radius: 50%;
  object-fit: cover;
  display: block;
  user-select: none;
}
.avatar-edit { display: flex; flex-direction: column; align-items: center; gap: 6px; padding-bottom: 4px; }
.avatar-btn { position: relative; border: 0; padding: 0; background: none; border-radius: 50%; }
.avatar-btn .avatar { width: 96px; height: 96px; font-size: 32px; }
.avatar-cam {
  position: absolute;
  right: -2px; bottom: -2px;
  width: 34px; height: 34px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--accent);
  color: var(--on-accent);
  border: 3px solid var(--panel);
  transition: transform .2s var(--ease-spring);
}
.avatar-cam svg { width: 16px; height: 16px; }
.avatar-btn:hover .avatar-cam { transform: scale(1.1); }
.avatar-btn:active { transform: scale(.96); }
.avatar-actions { display: flex; gap: 14px; }
.link-btn { border: 0; background: none; padding: 2px 0; color: var(--accent); font-weight: 500; font-size: 14px; }
.link-btn.danger { color: var(--danger); }
.link-btn:hover { text-decoration: underline; text-underline-offset: 3px; }

.cropper { width: min(340px, calc(100vw - 32px)); }
.crop-view {
  position: relative;
  align-self: center;
  overflow: hidden;
  border-radius: 50%;
  background: var(--panel-2);
  touch-action: none;
  cursor: grab;
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.crop-view:active { cursor: grabbing; }
.crop-view img {
  position: absolute;
  left: 0; top: 0;
  max-width: none;
  transform-origin: 0 0;
  user-select: none;
  pointer-events: none;
}
.crop-zoom { display: flex; align-items: center; gap: 10px; color: var(--muted); }
.crop-zoom svg { width: 18px; height: 18px; flex: none; }
.crop-zoom input { flex: 1; accent-color: var(--accent); }

/* ═══════════════ Звонки ═══════════════ */
.call {
  position: fixed;
  inset: 0;
  z-index: 80;
  display: flex;
  flex-direction: column;
  align-items: center;
  color: #fff;
  background:
    radial-gradient(120% 80% at 50% 20%, color-mix(in srgb, var(--accent) 55%, #000) 0%, #0d100f 70%);
  overflow: hidden;
  padding: env(safe-area-inset-top) 16px env(safe-area-inset-bottom);
}
.call.in { animation: fade-in .3s ease both; }
.call.in .call-info { animation: rise-in .45s var(--ease-out) .05s both; }
.call.in .call-controls { animation: rise-in .45s var(--ease-out) .12s both; }
.call.out { animation: fade-out .3s ease forwards; }

.call-remote {
  position: absolute;
  inset: 0;
  width: 100%; height: 100%;
  object-fit: cover;
  background: #000;
  opacity: 0;
  transition: opacity .4s ease;
}
.call.has-video .call-remote { opacity: 1; }
.call audio { display: none; }

.call-info {
  position: relative;
  margin-top: 14vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  text-align: center;
  transition: margin .4s var(--ease-out), transform .4s var(--ease-out);
}
.call-avatar .avatar {
  width: 128px; height: 128px;
  font-size: 44px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, .35);
}
.call-name { font-size: 26px; font-weight: 600; margin-top: 10px; text-shadow: 0 1px 8px rgba(0, 0, 0, .4); }
.call-status { font-size: 16px; opacity: .8; font-variant-numeric: tabular-nums; text-shadow: 0 1px 6px rgba(0, 0, 0, .4); }
.call-peer-muted { display: flex; align-items: center; gap: 6px; font-size: 14px; opacity: .85; padding: 4px 12px; border-radius: 14px; background: rgba(0, 0, 0, .35); }
.call-peer-muted svg { width: 16px; height: 16px; }

/* «волны» вокруг аватара, пока идёт вызов */
.call:not(.has-video) .call-avatar { position: relative; }
.call.incoming .call-avatar::before,
.call.incoming .call-avatar::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, .5);
  animation: ripple 2s ease-out infinite;
}
.call.incoming .call-avatar::after { animation-delay: 1s; }
@keyframes ripple { from { transform: scale(1); opacity: .8; } to { transform: scale(1.7); opacity: 0; } }

/* с видео: имя и время — компактно сверху */
.call.has-video .call-info { margin-top: 18px; }
.call.has-video .call-avatar { display: none; }
.call.has-video .call-name { font-size: 18px; margin-top: 0; }
.call.has-video .call-status { font-size: 14px; }

.call-local {
  position: absolute;
  right: 16px;
  bottom: calc(132px + env(safe-area-inset-bottom));
  width: 112px;
  aspect-ratio: 3 / 4;
  object-fit: cover;
  border-radius: 14px;
  background: #222;
  box-shadow: 0 6px 24px rgba(0, 0, 0, .45);
  border: 2px solid rgba(255, 255, 255, .25);
  animation: pop-in .35s var(--ease-spring) both;
}
.call-local.mirror { transform: scaleX(-1); }

.call-controls {
  position: relative;
  margin-top: auto;
  margin-bottom: 28px;
  display: flex;
  gap: 18px;
  justify-content: center;
  flex-wrap: wrap;
}
.call.incoming .call-controls { gap: 72px; }
.call-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  border: 0;
  background: none;
  color: #fff;
  padding: 0;
  font-size: 12px;
}
.call-btn-ico {
  width: 60px; height: 60px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: rgba(255, 255, 255, .16);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  transition: background-color .2s ease, transform .15s var(--ease-out);
}
.call-btn-ico svg { width: 26px; height: 26px; }
.call-btn:hover .call-btn-ico { background: rgba(255, 255, 255, .26); }
.call-btn:active .call-btn-ico { transform: scale(.9); }
.call-btn.off .call-btn-ico { background: #fff; color: #1b1f1d; }
.call-btn.red .call-btn-ico { background: #e5484d; }
.call-btn.red:hover .call-btn-ico { background: #d13a40; }
.call-btn.green .call-btn-ico { background: #30a46c; }
.call-btn.green:hover .call-btn-ico { background: #298e5d; }
.call-btn.red .call-btn-ico svg { transform: rotate(0deg); }
.call-btn.pulse .call-btn-ico { animation: call-pulse 1.4s ease-in-out infinite; }
@keyframes call-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(48, 164, 108, .6); transform: scale(1); }
  50% { box-shadow: 0 0 0 16px rgba(48, 164, 108, 0); transform: scale(1.06); }
}
.call-btn-label { opacity: .85; text-shadow: 0 1px 4px rgba(0, 0, 0, .5); }

/* Запись о звонке в чате */
.call-bubble { min-width: 0; }
.call-row {
  display: flex;
  align-items: center;
  gap: 10px;
  border: 0;
  background: none;
  padding: 2px 0 0;
  text-align: left;
  color: inherit;
}
.call-ico {
  width: 36px; height: 36px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--accent-soft);
  color: var(--accent);
  flex: none;
  transition: transform .2s var(--ease-out);
}
.call-ico svg { width: 18px; height: 18px; }
.call-row:hover .call-ico { transform: scale(1.08); }
.call-row.missed .call-ico { background: color-mix(in srgb, var(--danger) 15%, transparent); color: var(--danger); }
.call-row.missed .call-text > span:first-child { color: var(--danger); }
.call-text { display: flex; flex-direction: column; font-weight: 500; }
.call-dur { font-size: 13px; font-weight: 400; color: var(--muted); }
.msg.mine .call-dur { color: color-mix(in srgb, var(--bubble-out-text) 60%, transparent); }

@media (max-width: 760px) {
  .call-info { margin-top: 12vh; }
  .call-btn-ico { width: 56px; height: 56px; }
  .call-controls { gap: 14px; }
}
