:root {
  --accent: #0fa47a;
  --accent-text: #fff;
  --bg: #ffffff;
  --bg-2: #f3f7f5;
  --hover: #eef5f1;
  --active: #0fa47a;
  --text: #000;
  --muted: #707579;
  --border: #dde8e3;
  --chat-bg: #d5f2e4;
  --chat-bg-2: #edf8d3;
  --in: #ffffff;
  --out: #d9f7e8;
  --out-meta: #0b9a71;
  --in-meta: #8c9195;
  --link: #0b8f69;
  --badge: #0fa47a;
  --badge-muted: #c4c9cc;
  --menu: rgba(255, 255, 255, .96);
  --shadow: 0 1px 2px rgba(16, 35, 47, .15);
  --danger: #e53935;
  --rec: #e53935;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --accent: #1fbf8f;
    --bg: #212121;
    --bg-2: #181818;
    --hover: #2c2c2c;
    --active: #179e77;
    --text: #fff;
    --muted: #aaaaaa;
    --border: #0f0f0f;
    --chat-bg: #0c120f;
    --chat-bg-2: #102019;
    --in: #212121;
    --out: #1b6550;
    --out-meta: #b9f3dc;
    --in-meta: #9e9e9e;
    --link: #5fe0b4;
    --badge: #1fbf8f;
    --badge-muted: #5b5b5b;
    --menu: rgba(33, 33, 33, .96);
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.35 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  overflow: hidden;
  overscroll-behavior: none;
}
[hidden] { display: none !important; }
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; padding: 0; }
svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- Вход ---------- */
.login { position: fixed; inset: 0; display: grid; place-items: center; padding: 24px; background: var(--bg-2); }
.login-card { max-width: 380px; text-align: center; background: var(--bg); padding: 32px 24px; border-radius: 16px; box-shadow: var(--shadow); }
.login-card img { border-radius: 24px; }
.login-card h1 { font-size: 24px; margin: 16px 0 8px; }
.login-card p { color: var(--muted); margin: 0; }
.login-form { display: flex; flex-direction: column; gap: 10px; margin-top: 16px; }
.login-form input, .sheet .field {
  font: inherit; font-size: 16px; color: var(--text); background: var(--bg-2);
  border: 1px solid var(--border); border-radius: 12px; padding: 12px 14px; outline: none; width: 100%;
}
.login-form input:focus, .sheet .field:focus { border-color: var(--accent); }
.login-btn {
  background: var(--accent); color: #fff; font-weight: 600; border-radius: 12px; padding: 12px; margin-top: 4px;
}
.login-btn:disabled { opacity: .6; }
.login-text { margin-top: 12px !important; min-height: 1.3em; }
.login-text.err { color: var(--danger) !important; }
.login-links { display: flex; justify-content: space-between; gap: 8px; margin-top: 10px; flex-wrap: wrap; }
.login-links button { color: var(--accent); font-size: 15px; padding: 6px 2px; }
.login-sub { font-size: 15px; color: var(--muted); margin: 4px 0 0; }
.call-box { display: flex; flex-direction: column; gap: 12px; margin-top: 16px; text-align: center; }
.call-box .call-btn {
  display: block; background: #4fae4e; color: #fff; text-decoration: none; font-weight: 600; font-size: 18px;
  border-radius: 14px; padding: 14px;
}
.call-box .call-num { font-size: 22px; font-weight: 700; letter-spacing: .5px; }
.call-wait { display: flex; align-items: center; justify-content: center; gap: 8px; color: var(--muted); font-size: 15px; }
.spinner { width: 16px; height: 16px; border-radius: 50%; border: 2px solid var(--border); border-top-color: var(--accent); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.family-code { font-size: 32px; font-weight: 700; letter-spacing: 4px; text-align: center; padding: 8px 20px; user-select: all; }
.login-card { width: 100%; }
.sheet .fields { display: flex; flex-direction: column; gap: 10px; padding: 4px 20px 0; }

/* ---------- Каркас ---------- */
#app { display: flex; height: 100dvh; }
#sidebar {
  width: 380px; min-width: 300px; max-width: 40%;
  display: flex; flex-direction: column;
  border-right: 1px solid var(--border); background: var(--bg);
}
#chatView {
  flex: 1; min-width: 0; position: relative;
  display: flex; flex-direction: column;
  background: var(--chat-bg);
  background-image:
    radial-gradient(circle at 20% 20%, rgba(255,255,255,.18) 0 2px, transparent 3px),
    radial-gradient(circle at 70% 60%, rgba(255,255,255,.12) 0 2px, transparent 3px),
    linear-gradient(160deg, var(--chat-bg), var(--chat-bg-2));
  background-size: 48px 48px, 64px 64px, 100% 100%;
}
#chatView > .chat-empty { flex: 1; display: grid; place-items: center; }
.chat-empty span { background: rgba(0,0,0,.25); color: #fff; padding: 4px 12px; border-radius: 16px; font-size: 14px; }
#chatView:not(.open) > :not(.chat-empty) { display: none !important; }
#chatView.open > .chat-empty { display: none; }

.bar {
  display: flex; align-items: center; gap: 8px;
  min-height: 56px; padding: 0 8px;
  padding-top: env(safe-area-inset-top);
  background: var(--bg);
  flex-shrink: 0;
}
.side-bar { border-bottom: 1px solid var(--border); }
.bar-title { flex: 1; font-weight: 600; font-size: 18px; padding-left: 4px; }
.conn { font-size: 13px; color: var(--muted); }
.icon-btn {
  width: 40px; height: 40px; border-radius: 50%;
  display: grid; place-items: center; color: var(--muted); flex-shrink: 0;
}
.icon-btn:hover { background: var(--hover); }
.text-btn { color: var(--accent); font-weight: 600; padding: 6px 10px; border-radius: 8px; }
.text-btn:hover { background: var(--hover); }

/* ---------- Список чатов ---------- */
.chat-list { flex: 1; overflow-y: auto; padding: 6px; }
.chat-item {
  display: flex; align-items: center; gap: 12px; width: 100%;
  padding: 8px; border-radius: 12px; text-align: left;
}
.chat-item:hover { background: var(--hover); }
.chat-item.active { background: var(--active); color: var(--accent-text); }
.chat-item .ci-body { flex: 1; min-width: 0; }
.ci-row { display: flex; align-items: center; gap: 6px; }
.ci-name { flex: 1; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ci-time { font-size: 13px; color: var(--muted); display: flex; align-items: center; gap: 2px; }
.ci-text { flex: 1; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 15px; }
.ci-text b { color: var(--text); font-weight: 400; }
.ci-text .typing { color: var(--accent); }
.chat-item.active .ci-time, .chat-item.active .ci-text, .chat-item.active .ci-text b,
.chat-item.active .ci-text .typing { color: rgba(255,255,255,.9); }
.badge {
  min-width: 22px; height: 22px; padding: 0 7px; border-radius: 11px;
  background: var(--badge); color: #fff; font-size: 13px; font-weight: 600;
  display: grid; place-items: center;
}
.chat-item.active .badge { background: #fff; color: var(--active); }
.ticks { color: var(--out-meta); font-size: 13px; letter-spacing: -4px; padding-right: 4px; }
.chat-item .ticks { color: var(--badge); }
.chat-item.active .ticks { color: #fff; }

.push-banner {
  margin: 8px; padding: 10px 12px; border-radius: 12px;
  background: var(--bg-2); font-size: 14px; display: flex; gap: 10px; align-items: center;
}
.push-banner span { flex: 1; }

/* ---------- Аватары ---------- */
.avatar {
  border-radius: 50%; flex-shrink: 0;
  display: grid; place-items: center;
  color: #fff; font-weight: 600; overflow: hidden;
  background: linear-gradient(#ffffff33, #00000011), var(--c, #65aadd);
}
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.avatar .online-dot { display: none; }
.av-wrap { position: relative; flex-shrink: 0; }
.av-wrap.online::after {
  content: ""; position: absolute; right: 1px; bottom: 1px;
  width: 12px; height: 12px; border-radius: 50%;
  background: #4fae4e; border: 2px solid var(--bg);
}

/* ---------- Шапка чата ---------- */
.chat-bar { border-left: 1px solid var(--border); box-shadow: 0 1px 0 var(--border); z-index: 2; }
#backBtn { display: none; }
.chat-info { flex: 1; min-width: 0; padding-left: 4px; }
.chat-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-status { font-size: 14px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-status.on { color: var(--accent); }

/* ---------- Сообщения ---------- */
.messages {
  flex: 1; overflow-y: auto; overflow-x: hidden;
  padding: 8px 12px 4px;
  display: flex; flex-direction: column;
  overscroll-behavior: contain;
}
.messages > :first-child { margin-top: auto; }
.day {
  align-self: center; margin: 10px 0 6px;
  background: rgba(0,0,0,.22); color: #fff; font-size: 13px; font-weight: 500;
  padding: 3px 10px; border-radius: 12px; position: sticky; top: 4px; z-index: 1;
}
.msg { display: flex; align-items: flex-end; gap: 6px; margin-top: 2px; max-width: min(560px, 88%); }
.msg.first { margin-top: 8px; }
.msg.out { align-self: flex-end; flex-direction: row-reverse; }
.msg.in { align-self: flex-start; }
.msg .av-slot { width: 34px; flex-shrink: 0; }
.msg.out .av-slot, .chat-direct .msg .av-slot { display: none; }
.bubble {
  position: relative; min-width: 0;
  background: var(--in); border-radius: 15px; box-shadow: var(--shadow);
  padding: 6px 9px 6px; overflow-wrap: anywhere;
  -webkit-touch-callout: none;
}
.msg.out .bubble { background: var(--out); }
.msg.in.last .bubble { border-bottom-left-radius: 4px; }
.msg.out.last .bubble { border-bottom-right-radius: 4px; }
.msg.pending .bubble { opacity: .75; }
.msg.flash .bubble { animation: flash 1.2s; }
@keyframes flash { 0%, 60% { filter: brightness(.82); } }
.sender { font-weight: 600; font-size: 14px; margin-bottom: 2px; }
.text { white-space: pre-wrap; }
.text a { color: var(--link); }
.meta-space { display: inline-block; width: var(--mw, 52px); height: 1px; }
.meta {
  position: absolute; right: 9px; bottom: 4px;
  font-size: 12px; color: var(--in-meta); white-space: nowrap;
  display: flex; align-items: center; gap: 3px; user-select: none;
}
.msg.out .meta { color: var(--out-meta); }
.meta .ticks { padding-right: 3px; color: inherit; }
.meta.over {
  background: rgba(0,0,0,.45); color: #fff !important; padding: 1px 6px; border-radius: 10px;
  right: 8px; bottom: 8px;
}
.reply-quote {
  display: block; width: 100%; text-align: left;
  border-left: 3px solid var(--accent); background: color-mix(in srgb, var(--accent) 10%, transparent);
  border-radius: 4px 6px 6px 4px; padding: 3px 8px; margin: 2px 0 4px; font-size: 14px;
  max-width: 100%; overflow: hidden;
}
.msg.out .reply-quote { border-color: var(--out-meta); background: color-mix(in srgb, var(--out-meta) 14%, transparent); }
.reply-quote b { display: block; color: var(--accent); font-weight: 600; }
.msg.out .reply-quote b { color: var(--out-meta); }
.reply-quote span { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.media { display: block; margin: -3px -6px 4px; border-radius: 12px; overflow: hidden; background: rgba(0,0,0,.08); position: relative; }
.media img, .media video { display: block; width: 100%; height: 100%; object-fit: cover; max-height: 420px; }
.media img { cursor: zoom-in; }
.bubble.only-media { padding: 4px; }
.bubble.only-media .media { margin: 0; }
.progress {
  position: absolute; inset: 0; display: grid; place-items: center;
  background: rgba(0,0,0,.35); color: #fff; font-weight: 600;
}

.voice { display: flex; align-items: center; gap: 10px; min-width: 220px; padding: 2px 0 14px; }
.voice .play {
  width: 42px; height: 42px; border-radius: 50%; flex-shrink: 0;
  background: var(--accent); color: #fff; display: grid; place-items: center;
}
.msg.out .voice .play { background: var(--out-meta); color: var(--out); }
.voice .play svg { width: 20px; height: 20px; fill: currentColor; stroke: none; }
.voice .track { flex: 1; min-width: 0; }
.voice .wave { height: 22px; display: flex; align-items: center; gap: 2px; cursor: pointer; }
.voice .wave i { flex: 1; border-radius: 2px; background: color-mix(in srgb, var(--in-meta) 55%, transparent); }
.msg.out .voice .wave i { background: color-mix(in srgb, var(--out-meta) 45%, transparent); }
.voice .wave i.on { background: var(--accent); }
.msg.out .voice .wave i.on { background: var(--out-meta); }
.voice .dur { font-size: 13px; color: var(--in-meta); margin-top: 2px; }
.msg.out .voice .dur { color: var(--out-meta); }

.file { display: flex; align-items: center; gap: 10px; padding: 2px 0 14px; color: inherit; text-decoration: none; min-width: 200px; }
.file .ficon { width: 42px; height: 42px; border-radius: 50%; background: var(--accent); color: #fff; display: grid; place-items: center; flex-shrink: 0; }
.file .fname { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file .fsize { font-size: 13px; color: var(--in-meta); }

.down-btn {
  position: absolute; right: 16px; bottom: 76px; z-index: 3;
  width: 48px; height: 48px; border-radius: 50%; background: var(--bg); color: var(--muted);
  box-shadow: 0 1px 4px rgba(0,0,0,.25); display: grid; place-items: center;
}
.down-btn span:not(:empty) {
  position: absolute; top: -6px; right: -2px; min-width: 22px; height: 22px; border-radius: 11px;
  background: var(--badge); color: #fff; font-size: 13px; display: grid; place-items: center; padding: 0 6px;
}

/* ---------- Поле ввода ---------- */
.composer-wrap {
  padding: 6px 12px calc(8px + env(safe-area-inset-bottom));
  max-width: 760px; width: 100%; align-self: center;
}
.composer { display: flex; align-items: flex-end; gap: 8px; }
.composer textarea {
  flex: 1; resize: none; border: 0; outline: none;
  font: inherit; color: var(--text); background: var(--bg);
  border-radius: 22px; padding: 11px 16px; max-height: 160px; min-height: 46px;
  box-shadow: var(--shadow);
}
#attachBtn { width: 46px; height: 46px; background: var(--bg); box-shadow: var(--shadow); }
.round-btn {
  width: 46px; height: 46px; border-radius: 50%; flex-shrink: 0;
  background: var(--accent); color: #fff; display: grid; place-items: center;
  box-shadow: var(--shadow);
}
.round-btn svg { grid-area: 1 / 1; }
.round-btn .i-send { fill: currentColor; stroke: none; }
.round-btn .i-send, .round-btn .i-ok { display: none; }
.round-btn.send .i-mic, .round-btn.edit .i-mic, .round-btn.rec .i-mic { display: none; }
.round-btn.send .i-send, .round-btn.rec .i-send { display: block; }
.round-btn.edit .i-ok { display: block; }
.round-btn.rec { background: var(--rec); }

.reply-bar, .rec-bar {
  display: flex; align-items: center; gap: 8px;
  background: var(--bg); border-radius: 14px 14px 0 0; padding: 4px 4px 4px 12px;
  margin: 0 54px -6px; padding-bottom: 10px; box-shadow: var(--shadow);
}
.reply-icon { color: var(--accent); flex-shrink: 0; }
.reply-body { flex: 1; min-width: 0; border-left: 2px solid var(--accent); padding-left: 8px; font-size: 14px; }
.reply-body b { display: block; color: var(--accent); }
.reply-body span { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rec-bar { padding: 8px 8px 14px 14px; }
.rec-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--rec); animation: blink 1s infinite; }
@keyframes blink { 50% { opacity: .2; } }
.rec-hint { flex: 1; color: var(--muted); font-size: 14px; }

/* ---------- Контекстное меню, шторка, просмотр ---------- */
.menu {
  position: fixed; z-index: 50; min-width: 190px; padding: 6px;
  background: var(--menu); backdrop-filter: blur(10px);
  border-radius: 12px; box-shadow: 0 4px 24px rgba(0,0,0,.25);
}
.menu button { display: flex; gap: 14px; align-items: center; width: 100%; padding: 8px 12px; border-radius: 8px; font-size: 15px; }
.menu button:hover { background: var(--hover); }
.menu button svg { width: 20px; height: 20px; color: var(--muted); }
.menu button.danger, .menu button.danger svg { color: var(--danger); }

.sheet-wrap { position: fixed; inset: 0; z-index: 40; background: rgba(0,0,0,.4); display: grid; place-items: center; padding: 16px; }
.sheet {
  width: 100%; max-width: 420px; max-height: 90dvh; overflow-y: auto;
  background: var(--bg); border-radius: 16px; padding: 8px 0 12px;
}
.sheet h2 { font-size: 18px; margin: 8px 20px 8px; }
.sheet .row { display: flex; align-items: center; gap: 14px; width: 100%; padding: 10px 20px; text-align: left; }
.sheet button.row:hover { background: var(--hover); }
.sheet .row .grow { flex: 1; min-width: 0; }
.sheet .sub { font-size: 14px; color: var(--muted); }
.sheet .profile { display: flex; flex-direction: column; align-items: center; gap: 10px; padding: 12px 20px 8px; }
.sheet .profile input {
  font: inherit; color: var(--text); background: var(--bg-2); border: 1px solid var(--border);
  border-radius: 10px; padding: 8px 12px; width: 100%; text-align: center;
}
.sheet .note { font-size: 14px; color: var(--muted); padding: 4px 20px 8px; }
.sheet .actions { display: flex; justify-content: flex-end; gap: 8px; padding: 8px 16px 0; }
.sheet .danger { color: var(--danger); }

.viewer { position: fixed; inset: 0; z-index: 60; background: rgba(0,0,0,.92); display: grid; place-items: center; }
.viewer img { max-width: 100vw; max-height: 100dvh; object-fit: contain; }
.viewer .icon-btn { position: absolute; top: calc(8px + env(safe-area-inset-top)); right: 8px; color: #fff; }
.viewer .icon-btn:hover { background: rgba(255,255,255,.12); }

.toast {
  position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%); z-index: 70;
  background: rgba(0,0,0,.8); color: #fff; padding: 8px 16px; border-radius: 10px; font-size: 14px;
  max-width: 90vw; text-align: center;
}

/* ---------- Телефон: одна колонка ---------- */
@media (max-width: 760px) {
  #sidebar { width: 100%; max-width: none; border: 0; }
  #chatView { position: fixed; inset: 0; z-index: 10; transform: translateX(100%); transition: transform .22s ease; }
  #chatView.open { transform: none; }
  #backBtn { display: grid; }
  .chat-bar { border-left: 0; }
  .msg { max-width: 92%; }
  .messages { padding: 6px 8px 4px; }
  .composer-wrap { padding-left: 8px; padding-right: 8px; }
  .reply-bar, .rec-bar { margin: 0 0 -6px; }
  .bubble { user-select: none; -webkit-user-select: none; }
}

/* Ссылка на Android-приложение */
.apk-link { display: block; margin-top: 14px; color: var(--accent); font-weight: 600; text-decoration: none; }

