:root {
  --bg: #eef1f5;
  --panel: #ffffff;
  --panel-2: #f6f7f9;
  --hover: #f0f3f7;
  --active: #e4effd;
  --border: #e1e5eb;
  --text: #18212d;
  --muted: #6a7586;
  --faint: #98a2b1;
  --accent: #2a7de1;
  --accent-ink: #ffffff;
  --accent-soft: #e4effd;
  --bubble-in: #ffffff;
  --bubble-out: #dff0ff;
  --chat-bg: #e9edf2;
  --danger: #c93c3c;
  --danger-soft: #fbe9e9;
  --ok: #26915a;
  --warn: #b26d05;
  --warn-soft: #fff4e0;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --shadow-lg: 0 8px 28px rgba(16, 24, 40, .18);
  --radius: 12px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1319;
    --panel: #151b23;
    --panel-2: #1a212b;
    --hover: #1e2631;
    --active: #1d3350;
    --border: #262f3b;
    --text: #e5e9ef;
    --muted: #93a0b2;
    --faint: #66727f;
    --accent: #4a95f0;
    --accent-ink: #ffffff;
    --accent-soft: #1d3350;
    --bubble-in: #1c2430;
    --bubble-out: #1f3b5c;
    --chat-bg: #0f151c;
    --danger: #ef6b6b;
    --danger-soft: #3a1f22;
    --ok: #4cc485;
    --warn: #e0a345;
    --warn-soft: #3a2d17;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
    --shadow-lg: 0 8px 28px rgba(0, 0, 0, .5);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--accent); }
code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .92em; }

.primary, .ghost, .danger-btn {
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 8px 14px;
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
}
.primary:hover { filter: brightness(1.06); }
.ghost { background: transparent; color: var(--text); border-color: var(--border); font-weight: 500; }
.ghost:hover { background: var(--hover); }
.danger-btn { background: var(--danger); }
.small { padding: 5px 10px; font-size: 13px; }
button:disabled { opacity: .55; cursor: default; }

/* ---------- вход ---------- */
.login { min-height: 100%; display: grid; place-items: center; padding: 24px 16px; }
.login-card {
  width: min(400px, 100%);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 28px 24px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 12px;
  text-align: center;
}
.login-logo { width: 48px; height: 48px; align-self: center; }
.login-card h1 { margin: 4px 0 0; font-size: 20px; }
.login-card p { margin: 0; color: var(--muted); }
.login-card input { padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px; background: var(--panel-2); }
.login-error { color: var(--danger) !important; }
.link-btn { border: 0; background: none; color: var(--accent); padding: 0; text-decoration: underline; font-size: 13px; }
.modal-body input[type="password"], .modal-body input[type="text"] {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel-2);
  margin-bottom: 8px;
}
.login-hint { font-size: 12px; }

/* ---------- каркас ---------- */
.app { height: 100%; display: flex; flex-direction: column; }
.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 16px;
  padding-top: calc(8px + env(safe-area-inset-top, 0px));
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; white-space: nowrap; }
.brand img { width: 24px; height: 24px; }
.spacer { flex: 1; }
.account-status { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 13px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--faint); flex: none; }
.dot.ok { background: var(--ok); }
.dot.bad { background: var(--danger); }
.dot.warn { background: var(--warn); }
.operator { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 13px; }
.operator input { width: 130px; padding: 5px 8px; border: 1px solid var(--border); border-radius: 6px; background: var(--panel-2); }

.main { flex: 1; display: flex; min-height: 0; }
.sidebar {
  width: 340px;
  flex: none;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-right: 1px solid var(--border);
  min-height: 0;
}
.search { padding: 10px 12px; border-bottom: 1px solid var(--border); }
.search input { width: 100%; padding: 8px 12px; border: 1px solid var(--border); border-radius: 18px; background: var(--panel-2); }
.chat-list { flex: 1; overflow-y: auto; }
.chat-item {
  width: 100%;
  display: flex;
  gap: 10px;
  align-items: center;
  padding: 9px 12px;
  border: 0;
  background: none;
  text-align: left;
  border-bottom: 1px solid var(--panel-2);
}
.chat-item:hover { background: var(--hover); }
.chat-item.active { background: var(--active); }
.avatar {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  flex: none;
  display: grid;
  place-items: center;
  color: #fff;
  font-weight: 600;
  font-size: 16px;
  letter-spacing: .02em;
}
.avatar.sm { width: 36px; height: 36px; font-size: 14px; }
.avatar { position: relative; overflow: hidden; }
.avatar-img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; opacity: 0; transition: opacity .2s; }
.avatar.has-photo .avatar-img { opacity: 1; }
.ci-body { flex: 1; min-width: 0; }
.ci-top, .ci-bottom { display: flex; align-items: baseline; gap: 6px; }
.ci-title { flex: 1; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ci-time { font-size: 12px; color: var(--faint); flex: none; }
.ci-preview { flex: 1; color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ci-preview.typing { color: var(--accent); }
.ci-preview .you { color: var(--text); }
.badge {
  flex: none;
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  border-radius: 10px;
  background: var(--accent);
  color: var(--accent-ink);
  font-size: 12px;
  font-weight: 600;
  display: grid;
  place-items: center;
}
.list-note { padding: 16px; text-align: center; color: var(--muted); font-size: 13px; }

/* ---------- чат ---------- */
.chat { flex: 1; display: flex; flex-direction: column; min-width: 0; min-height: 0; background: var(--chat-bg); position: relative; }
.chat-empty { flex: 1; display: grid; place-items: center; text-align: center; color: var(--muted); padding: 24px 16px; }
.chat-empty .big { font-size: 17px; color: var(--text); margin-bottom: 6px; }
.chat-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  min-height: 56px;
}
.back { display: none; }
.ch-body { flex: 1; min-width: 0; }
.ch-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ch-sub { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ch-sub.typing { color: var(--accent); }
.ch-actions { display: flex; gap: 8px; align-items: center; }

.messages { flex: 1; overflow-y: auto; padding: 12px 16px 8px; display: flex; flex-direction: column; gap: 2px; }
.day { align-self: center; margin: 10px 0 6px; padding: 3px 10px; border-radius: 10px; background: rgba(0, 0, 0, .08); color: var(--muted); font-size: 12px; }
.history-note { align-self: center; color: var(--muted); font-size: 12px; padding: 8px; }
.msg { display: flex; flex-direction: column; max-width: min(560px, 82%); margin: 2px 0; position: relative; }
.msg.in { align-self: flex-start; }
.msg.out { align-self: flex-end; align-items: flex-end; }
.bubble {
  background: var(--bubble-in);
  border-radius: var(--radius);
  padding: 6px 10px 5px;
  box-shadow: var(--shadow);
  min-width: 64px;
  max-width: 100%;
  overflow-wrap: anywhere;
}
.msg.out .bubble { background: var(--bubble-out); }
.msg.deleted .bubble { background: var(--danger-soft); box-shadow: inset 3px 0 0 var(--danger); }
.msg.pending .bubble { opacity: .85; }
.msg.failed .bubble { box-shadow: inset 0 0 0 1px var(--danger); }
.sender { font-size: 13px; font-weight: 600; color: var(--accent); margin-bottom: 2px; }
.fwd { font-size: 12px; color: var(--muted); margin-bottom: 3px; }
.text { white-space: pre-wrap; }
.text blockquote { margin: 4px 0; padding-left: 8px; border-left: 3px solid var(--accent); color: var(--muted); }
.text pre { margin: 4px 0; padding: 6px 8px; background: var(--panel-2); border-radius: 6px; overflow-x: auto; white-space: pre; }
.text code { background: var(--panel-2); border-radius: 4px; padding: 0 3px; }
.text pre code { background: none; padding: 0; }
.tg-spoiler { filter: blur(5px); cursor: pointer; transition: filter .2s; }
.tg-spoiler.revealed { filter: none; }
.reply {
  border-left: 3px solid var(--accent);
  background: rgba(42, 125, 225, .08);
  border-radius: 4px;
  padding: 3px 8px;
  margin-bottom: 4px;
  font-size: 13px;
  cursor: pointer;
  max-width: 100%;
}
.reply-who { font-weight: 600; color: var(--accent); font-size: 12px; }
.reply-text { color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.meta { display: flex; gap: 6px; justify-content: flex-end; align-items: center; font-size: 11.5px; color: var(--faint); margin-top: 2px; flex-wrap: wrap; }
.meta .via { color: var(--muted); }
.meta .edited { cursor: pointer; text-decoration: underline dotted; }
.ticks { letter-spacing: -3px; color: var(--faint); }
.ticks.read { color: var(--accent); }
.deleted-note { font-size: 12px; color: var(--danger); margin-top: 3px; }
.failed-note { font-size: 12px; color: var(--danger); margin-top: 3px; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.failed-note button { border: 0; background: none; color: var(--accent); padding: 0; text-decoration: underline; font-size: 12px; }
.progress { height: 3px; background: var(--border); border-radius: 2px; overflow: hidden; margin-top: 4px; }
.progress > div { height: 100%; background: var(--accent); width: 0; transition: width .2s; }

.menu-btn {
  position: absolute;
  top: 2px;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--muted);
  line-height: 1;
  opacity: 0;
  transition: opacity .15s;
}
.msg.in .menu-btn { right: -32px; }
.msg.out .menu-btn { left: -32px; }
.msg:hover .menu-btn, .menu-btn:focus { opacity: 1; }
@media (hover: none) { .menu-btn { opacity: .7; } }
.popup-menu {
  position: fixed;
  z-index: 40;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow-lg);
  padding: 4px;
  min-width: 190px;
}
.popup-menu button { display: block; width: 100%; text-align: left; border: 0; background: none; padding: 8px 12px; border-radius: 6px; }
.popup-menu button:hover { background: var(--hover); }
.popup-menu button.danger { color: var(--danger); }

.service { align-self: center; margin: 6px 0; padding: 4px 12px; border-radius: 12px; background: rgba(0, 0, 0, .07); color: var(--muted); font-size: 12.5px; text-align: center; max-width: 90%; }
.service.call { background: var(--panel); box-shadow: var(--shadow); color: var(--text); }
.service.call.missed { color: var(--danger); }

/* вложения */
.att { margin: 2px 0 4px; max-width: 100%; }
.att img.photo, .att video.video { display: block; max-width: min(360px, 100%); max-height: 360px; border-radius: 8px; background: var(--panel-2); cursor: zoom-in; }
.att video.video { cursor: auto; }
.round-wrap { position: relative; width: 220px; height: 220px; border-radius: 50%; cursor: pointer; outline: none; }
.round-wrap:focus-visible { box-shadow: 0 0 0 3px var(--accent); }
.round-wrap .round { width: 100%; height: 100%; border-radius: 50%; object-fit: cover; display: block; background: #000; }
.round-ring {
  position: absolute;
  inset: -4px;
  border-radius: 50%;
  background: conic-gradient(var(--accent) var(--p, 0deg), transparent 0);
  -webkit-mask: radial-gradient(farthest-side, transparent calc(100% - 3px), #000 calc(100% - 3px));
  mask: radial-gradient(farthest-side, transparent calc(100% - 3px), #000 calc(100% - 3px));
  pointer-events: none;
  opacity: 0;
  transition: opacity .2s;
}
.round-wrap.playing .round-ring { opacity: 1; }
.round-play {
  position: absolute;
  inset: 0;
  margin: auto;
  width: 52px;
  height: 52px;
  border-radius: 50%;
  background: rgba(0, 0, 0, .45);
  color: #fff;
  display: grid;
  place-items: center;
  font-size: 20px;
  padding-left: 4px;
  pointer-events: none;
}
.round-wrap.playing .round-play { display: none; }
.round-time {
  position: absolute;
  left: 50%;
  bottom: 12px;
  transform: translateX(-50%);
  background: rgba(0, 0, 0, .5);
  color: #fff;
  font-size: 12px;
  padding: 1px 8px;
  border-radius: 10px;
  pointer-events: none;
}
.bubble.bare { background: none; box-shadow: none; padding: 0; }
.bubble.bare .meta { padding: 0 4px; }
.msg.deleted .bubble.bare { background: none; box-shadow: none; }
.att .sticker { max-width: 160px; max-height: 160px; display: block; }
.att audio { width: min(300px, 70vw); display: block; height: 36px; }
.file-chip {
  display: flex;
  gap: 10px;
  align-items: center;
  padding: 6px 8px;
  border-radius: 8px;
  background: rgba(0, 0, 0, .04);
  text-decoration: none;
  color: var(--text);
  min-width: 200px;
}
.file-chip:hover { background: rgba(0, 0, 0, .07); }
.file-ico { width: 36px; height: 36px; border-radius: 8px; background: var(--accent); color: #fff; display: grid; place-items: center; font-size: 11px; font-weight: 700; flex: none; text-transform: uppercase; }
.file-ico.off { background: var(--faint); }
.file-info { min-width: 0; }
.file-name { font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 280px; }
.file-sub { font-size: 12px; color: var(--muted); }
.file-sub button { border: 0; background: none; color: var(--accent); padding: 0; text-decoration: underline; font-size: 12px; }
.att-caption { font-size: 12px; color: var(--muted); }
.geo, .contact-card, .poll { padding: 4px 0; }

/* поле ввода */
.composer { background: var(--panel); border-top: 1px solid var(--border); padding: 8px 12px; padding-bottom: calc(8px + env(safe-area-inset-bottom, 0px)); }
.composer-bar { display: flex; align-items: center; gap: 8px; padding: 4px 8px 8px; font-size: 13px; }
.composer-bar .cb-body { flex: 1; min-width: 0; border-left: 3px solid var(--accent); padding-left: 8px; }
.composer-bar .cb-title { color: var(--accent); font-weight: 600; font-size: 12px; }
.composer-bar .cb-text { color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.composer-files { display: flex; flex-wrap: wrap; gap: 8px; padding: 0 4px 8px; }
.cf {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 8px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel-2);
  font-size: 13px;
  max-width: 100%;
}
.cf-name { max-width: 220px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cf-size { color: var(--muted); font-size: 12px; }
.cf label { display: flex; gap: 4px; align-items: center; color: var(--muted); font-size: 12px; white-space: nowrap; }
.x-btn { border: 0; background: none; color: var(--muted); font-size: 16px; line-height: 1; padding: 0 2px; }
.composer-row { display: flex; align-items: flex-end; gap: 8px; }
.icon-btn { width: 40px; height: 40px; border-radius: 50%; border: 0; background: none; font-size: 20px; flex: none; }
.icon-btn:hover { background: var(--hover); }
#input {
  flex: 1;
  resize: none;
  max-height: 200px;
  min-height: 40px;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 20px;
  background: var(--panel-2);
  outline: none;
}
#input:focus { border-color: var(--accent); }
.send-btn { width: 40px; height: 40px; border-radius: 50%; border: 0; background: var(--accent); color: var(--accent-ink); font-size: 17px; flex: none; }
.composer-hint { font-size: 11.5px; color: var(--faint); padding: 4px 52px 0; }
.drop {
  position: absolute;
  inset: 8px;
  border: 2px dashed var(--accent);
  border-radius: 12px;
  background: rgba(42, 125, 225, .08);
  display: grid;
  place-items: center;
  font-size: 18px;
  color: var(--accent);
  pointer-events: none;
  z-index: 20;
}
.to-bottom {
  position: absolute;
  right: 20px;
  bottom: 120px;
  border: 1px solid var(--border);
  background: var(--panel);
  border-radius: 16px;
  padding: 6px 12px;
  box-shadow: var(--shadow-lg);
  z-index: 10;
}

/* уведомления и окна */
.toasts { position: fixed; right: 16px; top: 64px; display: flex; flex-direction: column; gap: 8px; z-index: 50; max-width: min(360px, calc(100vw - 32px)); }
.toast { background: var(--panel); border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow-lg); padding: 10px 14px; display: flex; gap: 10px; align-items: flex-start; }
.toast.call { border-color: var(--ok); }
.toast.error { border-color: var(--danger); }
.toast .t-body { flex: 1; }
.toast .t-title { font-weight: 600; }
.toast .t-text { color: var(--muted); font-size: 13px; }
.toast .t-actions { margin-top: 6px; display: flex; gap: 8px; }
.ring { animation: ring 1.2s infinite; display: inline-block; }
@keyframes ring { 0%, 100% { transform: rotate(0); } 10%, 30% { transform: rotate(-14deg); } 20%, 40% { transform: rotate(14deg); } 50% { transform: rotate(0); } }

.modal { position: fixed; inset: 0; background: rgba(10, 14, 20, .55); display: grid; place-items: center; z-index: 60; padding: 16px; }
.modal-card { background: var(--panel); border-radius: 14px; box-shadow: var(--shadow-lg); width: min(520px, 100%); max-height: 85vh; display: flex; flex-direction: column; }
.modal-card header { padding: 14px 18px; border-bottom: 1px solid var(--border); font-weight: 600; display: flex; align-items: center; gap: 8px; }
.modal-card header span { flex: 1; }
.modal-body { padding: 14px 18px; overflow-y: auto; }
.modal-body p { margin: 0 0 10px; }
.modal-actions { padding: 12px 18px; border-top: 1px solid var(--border); display: flex; gap: 8px; justify-content: flex-end; }
.version { padding: 10px 0; border-bottom: 1px solid var(--border); }
.version:last-child { border-bottom: 0; }
.version-head { font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.lightbox { background: transparent; box-shadow: none; width: auto; max-width: 95vw; align-items: center; gap: 10px; }
.lightbox img { max-width: 95vw; max-height: 80vh; border-radius: 8px; display: block; }
.lightbox a { color: #fff; }

/* узкий экран: список или чат */
@media (max-width: 760px) {
  .sidebar { width: 100%; border-right: 0; }
  .chat { display: none; }
  .app.chat-open .sidebar { display: none; }
  .app.chat-open .chat { display: flex; }
  .back { display: inline-grid; }
  .brand span, .operator { display: none; }
  .msg { max-width: 88%; }
  .msg.in .menu-btn { right: -28px; }
  .msg.out .menu-btn { left: -28px; }
  .composer-hint { display: none; }
}
