:root {
  --bg: #ffffff; --bg2: #f3f5f4; --panel: #ffffff; --text: #15191c; --muted: #6b7479; --line: #e3e7e5;
  --accent: #178f6e; --accent-h: #127a5d; --accent-soft: #e2f3ec; --on-accent: #fff;
  --chat-bg: #e9eeec; --in: #ffffff; --out: #d6f2e5; --out-meta: #4f8a72; --danger: #e0484d;
  --hover: rgba(0,0,0,.045); --sel: #e2f3ec; --shadow: 0 1px 1.5px rgba(0,0,0,.09);
  --overlay: rgba(10,14,16,.45); --r: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1315; --bg2: #171c1f; --panel: #13181a; --text: #e7eaec; --muted: #929ca2; --line: #252c30;
    --accent: #2db88e; --accent-h: #39c99d; --accent-soft: #173a30; --on-accent: #06140f;
    --chat-bg: #0a0d0f; --in: #1b2125; --out: #1b4638; --out-meta: #8cc9b2; --danger: #f06368;
    --hover: rgba(255,255,255,.05); --sel: #173a30; --shadow: 0 1px 1.5px rgba(0,0,0,.5);
    --overlay: rgba(0,0,0,.6);
    color-scheme: dark;
  }
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  font: 15px/1.38 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  background: var(--bg); color: var(--text); overflow: hidden; overscroll-behavior: none;
}
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
input, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }
svg.i { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round;
  stroke-linejoin: round; flex: none; }
.hidden { display: none !important; }
.muted { color: var(--muted); }

/* ---------- каркас ---------- */
#app { display: flex; height: 100%; height: 100dvh; }
.side { width: 360px; flex: none; display: flex; flex-direction: column; background: var(--panel);
  border-right: 1px solid var(--line); min-height: 0; }
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; background: var(--chat-bg); position: relative; }
.empty-main { margin: auto; color: var(--muted); text-align: center; padding: 24px; }
.empty-main .pill { display: inline-block; background: var(--overlay); color: #fff; border-radius: 16px; padding: 6px 14px; }
@media (max-width: 799px) {
  .side { width: 100%; border: 0; }
  .main { position: fixed; inset: 0; z-index: 5; transform: translateX(100%); transition: transform .22s ease; visibility: hidden; }
  body.in-chat .main { transform: none; visibility: visible; }
}

.bar { height: 56px; flex: none; display: flex; align-items: center; gap: 6px; padding: 0 8px;
  background: var(--panel); border-bottom: 1px solid var(--line); }
.bar .title { flex: 1; min-width: 0; font-weight: 600; font-size: 17px; padding-left: 6px; }
.bar .sub { font-size: 13px; color: var(--muted); font-weight: 400; }
.ib { width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center; color: var(--muted); flex: none; }
.ib:hover { background: var(--hover); color: var(--text); }
.ib.accent { color: var(--accent); }
.search { padding: 8px 12px; border-bottom: 1px solid var(--line); }
.search input { width: 100%; height: 38px; border-radius: 19px; border: 0; padding: 0 16px; background: var(--bg2); outline: none; }
.conn { font-size: 12.5px; color: var(--muted); padding: 4px 16px; background: var(--bg2); }

/* ---------- список чатов ---------- */
.list { flex: 1; overflow-y: auto; min-height: 0; }
.item { display: flex; align-items: center; gap: 12px; padding: 8px 12px; cursor: pointer; position: relative; }
.item:hover { background: var(--hover); }
.item.sel { background: var(--sel); }
.item .body { flex: 1; min-width: 0; }
.item .row { display: flex; align-items: baseline; gap: 6px; }
.item .name { flex: 1; min-width: 0; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item .time { font-size: 12px; color: var(--muted); flex: none; }
.item .prev { flex: 1; min-width: 0; color: var(--muted); font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item .prev.typing { color: var(--accent); }
.badge { min-width: 22px; height: 22px; border-radius: 11px; background: var(--accent); color: var(--on-accent);
  font-size: 12.5px; font-weight: 600; display: grid; place-items: center; padding: 0 6px; flex: none; }
.badge.mute { background: var(--muted); color: var(--bg); }
.section { font-size: 13px; color: var(--muted); padding: 12px 16px 4px; font-weight: 600; }
.check { width: 22px; height: 22px; border-radius: 50%; border: 2px solid var(--line); flex: none; display: grid; place-items: center; }
.item.on .check { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.item.on .check svg { width: 14px; height: 14px; stroke-width: 3; }

.av { width: 46px; height: 46px; border-radius: 50%; flex: none; display: grid; place-items: center; color: #fff;
  font-weight: 600; font-size: 17px; position: relative; user-select: none; }
.av.s { width: 38px; height: 38px; font-size: 14.5px; }
.av.l { width: 96px; height: 96px; font-size: 36px; }
.av.xl { width: 128px; height: 128px; font-size: 46px; }
.av svg { width: 55%; height: 55%; }
.av .dot { position: absolute; right: 1px; bottom: 1px; width: 12px; height: 12px; border-radius: 50%;
  background: #2fc77a; border: 2px solid var(--panel); }

/* ---------- чат ---------- */
.chat-head { cursor: pointer; }
.msgs { flex: 1; overflow-y: auto; min-height: 0; padding: 8px 0 6px; overflow-anchor: none; }
.msgs-in { max-width: 820px; margin: 0 auto; padding: 0 10px; display: flex; flex-direction: column; }
.day { align-self: center; background: var(--overlay); color: #fff; font-size: 12.5px; border-radius: 12px;
  padding: 3px 11px; margin: 10px 0 6px; position: sticky; top: 6px; z-index: 1; }
.sys { align-self: center; text-align: center; color: var(--muted); font-size: 13px; margin: 6px 24px; }
.sys.call { background: var(--panel); color: var(--text); border-radius: 16px; padding: 6px 14px; box-shadow: var(--shadow);
  cursor: pointer; display: flex; gap: 8px; align-items: center; }
.sys.call svg { width: 18px; height: 18px; }
.sys.call.missed { color: var(--danger); }
.m { display: flex; flex-direction: column; max-width: min(78%, 560px); margin-top: 2px; position: relative; }
.m.first { margin-top: 8px; }
.m.in { align-self: flex-start; }
.m.out { align-self: flex-end; }
.m .who { font-size: 13px; font-weight: 600; margin: 0 0 2px 12px; }
.b { background: var(--in); border-radius: var(--r); padding: 6px 10px 6px; box-shadow: var(--shadow);
  position: relative; word-wrap: break-word; overflow-wrap: anywhere; min-width: 64px; }
.m.out .b { background: var(--out); }
.m.in.first .b { border-top-left-radius: 5px; }
.m.out.first .b { border-top-right-radius: 5px; }
.b .t { white-space: pre-wrap; }
.b .t.big { font-size: 34px; line-height: 1.15; }
.b .t .del { font-style: italic; color: var(--muted); }
.meta { float: right; font-size: 11.5px; color: var(--muted); margin: 7px -2px -4px 10px; display: flex;
  align-items: center; gap: 3px; user-select: none; white-space: nowrap; }
.m.out .meta { color: var(--out-meta); }
.meta svg { width: 16px; height: 16px; stroke-width: 2.2; }
.meta .read { color: var(--accent); }
.b.media { padding: 3px; overflow: hidden; }
.b.media .t { padding: 4px 7px 2px; }
.b.media .meta.on-img { position: absolute; right: 8px; bottom: 7px; background: rgba(0,0,0,.45); color: #fff;
  border-radius: 9px; padding: 1px 6px; margin: 0; }
.img { display: block; border-radius: 11px; max-width: 100%; background: var(--bg2); cursor: zoom-in; object-fit: cover; }
video.vid { display: block; border-radius: 11px; max-width: 100%; max-height: 360px; background: #000; }
.quote { border-left: 3px solid var(--accent); padding: 1px 8px; margin: 2px 0 4px; border-radius: 3px;
  background: var(--accent-soft); font-size: 13.5px; cursor: pointer; max-width: 100%; overflow: hidden; }
.quote b { color: var(--accent); display: block; font-size: 13px; }
.quote span { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: var(--muted); }
.file { display: flex; align-items: center; gap: 10px; text-decoration: none; color: inherit; padding: 2px 0; min-width: 200px; }
.file .fi { width: 44px; height: 44px; border-radius: 50%; background: var(--accent); color: var(--on-accent); display: grid; place-items: center; flex: none; }
.file .fn { font-weight: 600; word-break: break-all; }
.file .fs { font-size: 13px; color: var(--muted); }
.voice { display: flex; align-items: center; gap: 10px; min-width: 220px; padding: 2px 0; }
.voice .pp { width: 40px; height: 40px; border-radius: 50%; background: var(--accent); color: var(--on-accent); display: grid; place-items: center; flex: none; }
.voice .wave { flex: 1; height: 26px; display: flex; align-items: center; gap: 2px; cursor: pointer; }
.voice .wave i { flex: 1; background: var(--muted); opacity: .45; border-radius: 2px; min-height: 3px; }
.voice .wave i.on { background: var(--accent); opacity: 1; }
.voice .vd { font-size: 12.5px; color: var(--muted); min-width: 34px; }
.m.hl .b { animation: hl 1.4s ease; }
@keyframes hl { 0%, 40% { box-shadow: 0 0 0 3px var(--accent); } 100% { box-shadow: var(--shadow); } }
.typing-row { font-size: 13px; color: var(--accent); }
.up-prog { height: 4px; border-radius: 2px; background: var(--line); margin-top: 6px; overflow: hidden; }
.up-prog i { display: block; height: 100%; background: var(--accent); width: 0; transition: width .2s; }
.down { position: absolute; right: 16px; bottom: 76px; width: 46px; height: 46px; border-radius: 50%; background: var(--panel);
  box-shadow: 0 2px 8px rgba(0,0,0,.18); display: grid; place-items: center; color: var(--muted); z-index: 2; }
.down .badge { position: absolute; top: -6px; right: -4px; }

/* ---------- ввод ---------- */
.composer { flex: none; background: var(--panel); border-top: 1px solid var(--line); padding: 6px 8px;
  padding-bottom: max(6px, env(safe-area-inset-bottom)); }
.composer .ctx { display: flex; align-items: center; gap: 8px; padding: 2px 4px 6px 8px; }
.composer .ctx .quote { flex: 1; margin: 0; }
.composer .row { display: flex; align-items: flex-end; gap: 4px; max-width: 820px; margin: 0 auto; }
.composer textarea { flex: 1; resize: none; border: 0; outline: none; background: var(--bg2); border-radius: 20px;
  padding: 9px 14px; max-height: 160px; min-height: 40px; line-height: 22px; }
.send { width: 40px; height: 40px; border-radius: 50%; background: var(--accent); color: var(--on-accent); display: grid; place-items: center; flex: none; }
.send:hover { background: var(--accent-h); }
.rec { display: flex; align-items: center; gap: 10px; flex: 1; height: 40px; padding: 0 8px; }
.rec .dot { width: 10px; height: 10px; border-radius: 50%; background: var(--danger); animation: blink 1s infinite; }
@keyframes blink { 50% { opacity: .2; } }
.drop { position: absolute; inset: 0; background: var(--overlay); color: #fff; display: grid; place-items: center;
  font-size: 20px; z-index: 6; pointer-events: none; }

/* ---------- панели, окна, меню ---------- */
.sheet-bg { position: fixed; inset: 0; background: var(--overlay); z-index: 20; display: flex; align-items: center; justify-content: center; }
.sheet { background: var(--panel); width: min(440px, 100%); max-height: min(720px, 100%); border-radius: 16px;
  display: flex; flex-direction: column; overflow: hidden; box-shadow: 0 10px 40px rgba(0,0,0,.3); }
@media (max-width: 799px) {
  .sheet-bg { align-items: stretch; }
  .sheet { width: 100%; max-height: none; border-radius: 0; }
}
.sheet .scroll { overflow-y: auto; flex: 1; min-height: 0; }
.pad { padding: 16px; }
.center { text-align: center; }
.field { display: block; margin: 0 0 12px; }
.field span { display: block; font-size: 13px; color: var(--muted); margin: 0 0 4px 2px; }
.field input { width: 100%; height: 44px; border-radius: 10px; border: 1.5px solid var(--line); background: var(--bg);
  padding: 0 12px; outline: none; }
.field input:focus { border-color: var(--accent); }
.btn { height: 44px; border-radius: 10px; background: var(--accent); color: var(--on-accent); font-weight: 600;
  padding: 0 18px; display: inline-flex; align-items: center; justify-content: center; gap: 8px; }
.btn:hover { background: var(--accent-h); }
.btn.wide { width: 100%; }
.btn.ghost { background: var(--bg2); color: var(--text); }
.btn.danger { background: transparent; color: var(--danger); }
.btn:disabled { opacity: .55; cursor: default; }
.err { color: var(--danger); font-size: 14px; min-height: 20px; margin: 4px 0 8px; }
.link { color: var(--accent); cursor: pointer; font-weight: 600; }
.rowbtn { display: flex; align-items: center; gap: 16px; padding: 12px 16px; cursor: pointer; width: 100%; text-align: left; }
.rowbtn:hover { background: var(--hover); }
.rowbtn svg { color: var(--muted); }
.rowbtn.red, .rowbtn.red svg { color: var(--danger); }
.rowbtn .r { margin-left: auto; color: var(--muted); font-size: 14px; }
.sep { height: 8px; background: var(--bg2); }
.profile { padding: 22px 16px 14px; text-align: center; }
.profile .nm { font-size: 21px; font-weight: 600; margin-top: 10px; }
.acts { display: flex; justify-content: center; gap: 12px; padding: 4px 16px 16px; }
.acts button { display: flex; flex-direction: column; align-items: center; gap: 4px; width: 84px; padding: 8px 0;
  border-radius: 12px; background: var(--bg2); color: var(--accent); font-size: 13px; }
.note { font-size: 13.5px; color: var(--muted); padding: 8px 16px; }
.code { font-family: ui-monospace, Consolas, monospace; background: var(--bg2); border-radius: 8px; padding: 10px; word-break: break-all; user-select: all; }

.menu { position: fixed; z-index: 40; background: var(--panel); border-radius: 12px; box-shadow: 0 6px 28px rgba(0,0,0,.25);
  padding: 6px 0; min-width: 200px; }
.menu button { display: flex; align-items: center; gap: 14px; width: 100%; padding: 10px 16px; text-align: left; }
.menu button:hover { background: var(--hover); }
.menu button svg { width: 20px; height: 20px; color: var(--muted); }
.menu button.red, .menu button.red svg { color: var(--danger); }
.toast { position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%); background: rgba(20,24,26,.92); color: #fff;
  padding: 10px 16px; border-radius: 12px; z-index: 60; max-width: calc(100% - 32px); font-size: 14px; }
.viewer { position: fixed; inset: 0; background: rgba(0,0,0,.92); z-index: 50; display: grid; place-items: center; }
.viewer img { max-width: 100vw; max-height: 100vh; object-fit: contain; }
.viewer .ib { position: absolute; top: 10px; color: #fff; background: rgba(0,0,0,.35); }

/* ---------- вход ---------- */
/* блок прокручивается: если форма выше свободного места (открыта клавиатура), её можно докрутить */
.auth-wrap { flex: 1; min-width: 0; min-height: 0; overflow-y: auto; display: flex; overscroll-behavior: contain; }
.auth { margin: auto; width: min(400px, 100%); padding: 24px 20px; }
.logo { width: 76px; height: 76px; border-radius: 22px; background: var(--accent); color: var(--on-accent);
  display: grid; place-items: center; margin: 0 auto 14px; }
.logo svg { width: 44px; height: 44px; }
.auth h1 { text-align: center; margin: 0 0 4px; font-size: 26px; }
.auth p.lead { text-align: center; color: var(--muted); margin: 0 0 22px; }
.apps { display: flex; gap: 8px; margin-top: 18px; }
.apps a { flex: 1; text-decoration: none; }

/* ---------- звонок ---------- */
.call { position: fixed; inset: 0; z-index: 30; background: #0d1416; color: #fff; display: flex; flex-direction: column; overflow: hidden; }
.call .rv { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; background: #000; }
@media (min-width: 800px) { .call .rv { object-fit: contain; } }
.call .lv { position: absolute; right: 14px; top: calc(14px + env(safe-area-inset-top)); width: 112px; height: 150px; border-radius: 14px;
  object-fit: cover; background: #222; z-index: 2; box-shadow: 0 2px 12px rgba(0,0,0,.4); transform: scaleX(-1); }
.call .lv.back { transform: none; }
@media (min-width: 800px) { .call .lv { width: 220px; height: 150px; } }
.call .who { position: relative; z-index: 1; text-align: center; padding: calc(56px + env(safe-area-inset-top)) 16px 0;
  text-shadow: 0 1px 4px rgba(0,0,0,.6); }
.call .who .nm { font-size: 26px; font-weight: 600; }
.call .who .st { font-size: 15px; opacity: .85; margin-top: 4px; }
.call.has-video .who { padding-top: calc(18px + env(safe-area-inset-top)); }
.call.has-video .who .nm { font-size: 18px; }
.call .big { position: relative; z-index: 1; margin: 34px auto 0; }
.call.has-video .big { display: none; }
.call .ctl { position: absolute; z-index: 2; left: 0; right: 0; bottom: 0; display: flex; justify-content: center;
  gap: 18px; padding: 24px 12px calc(28px + env(safe-area-inset-bottom)); flex-wrap: wrap;
  background: linear-gradient(transparent, rgba(0,0,0,.55)); }
.cb { width: 60px; height: 60px; border-radius: 50%; background: rgba(255,255,255,.18); color: #fff; display: grid; place-items: center; }
.cb.off { background: #fff; color: #111; }
.cb.red { background: #e5484d; }
.cb.green { background: #22b06b; }
.cb svg { width: 28px; height: 28px; }
.cb.red svg.hang { transform: rotate(135deg); }
.cl { display: flex; flex-direction: column; align-items: center; gap: 6px; font-size: 12.5px; opacity: .95; }
.call .min { position: absolute; left: 10px; top: calc(10px + env(safe-area-inset-top)); z-index: 3; color: #fff; }
.callbar { position: fixed; top: 0; left: 0; right: 0; z-index: 25; background: #22b06b; color: #fff; text-align: center;
  padding: 6px 12px; padding-top: calc(6px + env(safe-area-inset-top)); font-size: 14px; cursor: pointer; font-weight: 600; }

@media (min-width: 800px) { .back-btn { display: none !important; } }
svg.i.mini { width: 16px; height: 16px; color: var(--muted); }
.call.ending .ctl { opacity: .4; pointer-events: none; }
select.field { border: 1.5px solid var(--line); background: var(--bg); color: var(--text); margin-bottom: 12px; }
