:root {
  --bg: #ffffff;
  --bg-2: #f3f5f7;
  --chat-bg: #eef2f0;
  --line: #e3e7ea;
  --text: #121417;
  --muted: #6b737c;
  --accent: #1d7f6a;
  --accent-2: #e2f2ee;
  --accent-text: #ffffff;
  --mine: #d9f2e9;
  --mine-text: #0f2a23;
  --theirs: #ffffff;
  --danger: #d23b3b;
  --green: #22a36b;
  --shadow: 0 1px 1px rgba(16, 24, 32, .06), 0 2px 8px rgba(16, 24, 32, .06);
  --ease-out: cubic-bezier(0.23, 1, 0.32, 1);
  --ease-drawer: cubic-bezier(0.32, 0.72, 0, 1);
  --r: 18px;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171c;
    --bg-2: #1c2026;
    --chat-bg: #0f1215;
    --line: #262b33;
    --text: #e8ebee;
    --muted: #8b949e;
    --accent: #3cbf9e;
    --accent-2: #17332c;
    --accent-text: #08130f;
    --mine: #1f4d40;
    --mine-text: #e6f6f0;
    --theirs: #1d2229;
    --danger: #f06363;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3);
  }
}

* { box-sizing: border-box; }
html {
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
  height: 100%;
}
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;
  overscroll-behavior: none;
}
#app { height: 100dvh; overflow: hidden; }
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
button, a, [role="button"], .row { touch-action: manipulation; user-select: none; -webkit-user-select: none; }
input, textarea, select { font: inherit; font-size: 16px; color: inherit; }
h1, h2, h3, p { margin: 0; }
h3 { font-size: 13px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 22px 0 8px; display: flex; align-items: center; gap: 6px; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.accent { color: var(--accent) !important; }
.mono { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; }
[hidden] { display: none !important; }

.ic { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.ic.sm { width: 17px; height: 17px; vertical-align: -3px; }
.ic.tick { width: 16px; height: 16px; stroke-width: 2; }

/* ---------- кнопки ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 0 18px; border-radius: 12px;
  background: var(--bg-2); font-weight: 600;
  transition: transform 140ms var(--ease-out), background-color 140ms;
}
.btn:active { transform: scale(0.97); }
.btn.primary { background: var(--accent); color: var(--accent-text); }
.btn.danger { color: var(--danger); }
.btn.ghost { background: none; color: var(--accent); }
.btn.wide { width: 100%; }
.btn.small { min-height: 34px; padding: 0 12px; font-size: 14px; border-radius: 10px; }
.btn:disabled { opacity: .6; }
.icon-btn {
  width: 44px; height: 44px; border-radius: 50%; display: grid; place-items: center; color: var(--accent);
  transition: transform 140ms var(--ease-out), background-color 140ms;
}
.icon-btn:active { transform: scale(0.92); background: var(--bg-2); }
.round-btn {
  width: 44px; height: 44px; border-radius: 50%; display: grid; place-items: center; color: var(--muted);
  transition: transform 140ms var(--ease-out);
}
.round-btn:active { transform: scale(0.9); }
.round-btn.send { background: var(--accent); color: var(--accent-text); }
.link { color: var(--accent); padding: 10px; font-size: 14px; }
@media (hover: hover) and (pointer: fine) {
  .btn:hover { filter: brightness(0.97); }
  .icon-btn:hover { background: var(--bg-2); }
  .row:hover { background: var(--bg-2); }
  .menu-item:hover { background: var(--bg-2); }
}

/* ---------- вход ---------- */
.center-screen {
  min-height: 100dvh; display: grid; place-items: center; gap: 16px; text-align: center;
  padding: calc(24px + var(--safe-t)) 16px calc(24px + var(--safe-b));
}
.auth-card { width: 100%; max-width: 380px; display: flex; flex-direction: column; gap: 12px; text-align: left; }
.brand { text-align: center; margin-bottom: 12px; display: grid; justify-items: center; gap: 8px; }
.brand h1 { font-size: 28px; letter-spacing: -0.02em; }
.brand p { max-width: 300px; }
.brand-mark { width: 72px; height: 72px; border-radius: 22%; box-shadow: 0 6px 20px rgba(15, 107, 90, .28); }
.field { display: grid; gap: 6px; font-size: 13px; color: var(--muted); }
.input {
  width: 100%; min-height: 48px; padding: 0 14px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--bg); outline: none; transition: border-color 140ms;
}
.input:focus { border-color: var(--accent); }
.pw { position: relative; display: block; }
.pw .input { padding-right: 52px; }
.eye { position: absolute; right: 4px; top: 50%; transform: translateY(-50%); width: 44px; height: 44px; display: grid; place-items: center; color: var(--muted); }
.eye.on { color: var(--accent); }
.center { text-align: center; }
.admin-btns { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.note { font-size: 13px; color: var(--muted); background: var(--bg-2); padding: 10px 12px; border-radius: 12px; }
.error { color: var(--danger); font-size: 14px; min-height: 0; }
.error:empty { display: none; }
.spinner { width: 28px; height: 28px; border-radius: 50%; border: 3px solid var(--line); border-top-color: var(--accent); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- раскладка ---------- */
.shell { display: grid; grid-template-columns: 360px 1fr; height: 100dvh; overflow: hidden; }
.list-pane { display: flex; flex-direction: column; border-right: 1px solid var(--line); min-width: 0; min-height: 0; background: var(--bg); }
.chat-pane { min-width: 0; min-height: 0; background: var(--chat-bg); display: flex; flex-direction: column; }
@media (max-width: 760px) {
  .shell { grid-template-columns: 50% 50%; width: 200%; transition: transform 320ms var(--ease-drawer); }
  .shell.in-chat { transform: translateX(-50%); }
  .list-pane { border-right: 0; }
}
@media (min-width: 761px) { .back { display: none !important; } }
@media (prefers-reduced-motion: reduce) { .shell { transition: none; } }

.bar {
  display: flex; align-items: center; gap: 4px; min-height: 56px;
  padding: var(--safe-t) 8px 0 16px; background: var(--bg); border-bottom: 1px solid var(--line); flex: none;
}
.bar-title { flex: 1; font-size: 22px; letter-spacing: -0.02em; display: flex; align-items: center; gap: 10px; }
.net-badge { font-size: 12px; font-weight: 500; color: var(--muted); letter-spacing: 0; }

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

/* ---------- список ---------- */
.list { flex: 1; overflow-y: auto; overscroll-behavior: contain; padding: 6px 6px calc(6px + var(--safe-b)); }
.row {
  width: 100%; display: flex; align-items: center; gap: 12px; padding: 9px 10px; border-radius: 14px; text-align: left;
  transition: background-color 120ms;
}
.row:active { background: var(--bg-2); }
.row.active { background: var(--accent-2); }
.row-main { flex: 1; min-width: 0; }
.row-top, .row-bottom { display: flex; align-items: baseline; gap: 8px; }
.row-name { flex: 1; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row-time { font-size: 12px; color: var(--muted); flex: none; }
.row-preview { flex: 1; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 14px; }
.badge { flex: none; min-width: 22px; height: 22px; padding: 0 7px; border-radius: 11px; background: var(--accent); color: var(--accent-text); font-size: 12px; font-weight: 700; display: grid; place-items: center; }
.list-empty { padding: 48px 24px; text-align: center; display: grid; gap: 12px; justify-items: center; }

.av {
  --hue: 160; width: 48px; height: 48px; border-radius: 50%; flex: none; display: grid; place-items: center;
  font-weight: 600; font-size: 17px; color: #fff;
  background: linear-gradient(160deg, hsl(var(--hue) 55% 58%), hsl(calc(var(--hue) + 25) 55% 46%));
}
.av.sm { width: 38px; height: 38px; font-size: 14px; }
.av.xl { width: 88px; height: 88px; font-size: 30px; }
.av-wrap { position: relative; flex: none; }
.dot { position: absolute; right: 1px; bottom: 1px; width: 13px; height: 13px; border-radius: 50%; background: var(--green); border: 2px solid var(--bg); }

/* ---------- чат ---------- */
.empty-chat { margin: auto; text-align: center; display: grid; gap: 8px; justify-items: center; padding: 24px; max-width: 360px; }
.empty-badge { width: 56px; height: 56px; border-radius: 50%; background: var(--bg); display: grid; place-items: center; color: var(--accent); margin-bottom: 4px; }
.chat { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.chat-bar { padding-left: 4px; }
.chat-who { flex: 1; min-width: 0; display: flex; align-items: center; gap: 10px; text-align: left; padding: 4px; border-radius: 12px; }
.who-text { display: grid; min-width: 0; }
.who-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.who-status { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.msgs { flex: 1; overflow-y: auto; overscroll-behavior: contain; padding: 8px 10px 12px; display: flex; flex-direction: column; gap: 3px; }
.e2e-note { align-self: center; max-width: 320px; text-align: center; font-size: 12.5px; color: var(--muted); background: var(--bg); padding: 8px 12px; border-radius: 12px; margin: 8px 0 4px; }
.day { align-self: center; margin: 10px 0 6px; }
.day span { font-size: 12.5px; font-weight: 600; padding: 4px 10px; border-radius: 12px; background: color-mix(in srgb, var(--bg) 85%, transparent); color: var(--muted); backdrop-filter: blur(6px); }
.msg { display: flex; margin-top: 5px; }
.msg.grouped { margin-top: 0; }
.msg.mine { justify-content: flex-end; }
.bubble {
  max-width: min(78%, 520px); padding: 7px 10px 6px 12px; border-radius: var(--r); background: var(--theirs);
  box-shadow: var(--shadow); position: relative; display: flex; flex-wrap: wrap; align-items: flex-end; gap: 2px 10px;
  overflow-wrap: anywhere; user-select: text; -webkit-user-select: text;
}
.msg.mine .bubble { background: var(--mine); color: var(--mine-text); }
.msg:not(.grouped):not(.mine) .bubble { border-bottom-left-radius: 6px; }
.msg.mine:not(.grouped) .bubble { border-bottom-right-radius: 6px; }
.text { white-space: pre-wrap; flex: 1 1 auto; }
.meta { margin-left: auto; font-size: 11.5px; color: var(--muted); display: inline-flex; align-items: center; gap: 3px; white-space: nowrap; transform: translateY(2px); }
.msg.mine .meta { color: color-mix(in srgb, var(--mine-text) 60%, transparent); }
.ticks.read { color: var(--accent); }
.msg.pending .bubble { opacity: .7; }
.retry { color: var(--danger); font-size: 13px; font-weight: 600; }
.bubble.media { padding: 3px; flex-direction: column; align-items: stretch; gap: 0; }
.bubble.media .meta { position: absolute; right: 9px; bottom: 7px; color: #fff; background: rgba(0, 0, 0, .4); padding: 1px 6px; border-radius: 9px; }
.bubble.media .text.cap { padding: 6px 8px 18px; }
.bubble.media .text.cap ~ .meta { position: static; background: none; color: var(--muted); padding: 0 6px 4px; align-self: flex-end; }
.bare { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; }
.bare .meta { background: color-mix(in srgb, var(--bg) 80%, transparent); padding: 1px 7px; border-radius: 9px; }

.photo { display: block; border-radius: 15px; overflow: hidden; background: var(--bg-2); max-width: 100%; position: relative; }
.photo img, .photo video { width: 100%; height: 100%; object-fit: cover; display: block; }
.photo.loading::after, .circle-msg.loading::after { content: ""; position: absolute; inset: 0; margin: auto; width: 26px; height: 26px; border-radius: 50%; border: 3px solid rgba(255,255,255,.5); border-top-color: var(--accent); animation: spin .8s linear infinite; }
.photo.broken::after { content: "Не загрузилось"; position: absolute; inset: 0; display: grid; place-items: center; color: var(--muted); font-size: 13px; }

.voice { display: flex; align-items: center; gap: 10px; width: min(250px, 62vw); }
.v-play { width: 42px; height: 42px; border-radius: 50%; background: var(--accent); color: var(--accent-text); display: grid; place-items: center; flex: none; transition: transform 140ms var(--ease-out); }
.v-play:active { transform: scale(0.92); }
.v-play.busy { opacity: .6; }
.v-play .ic { fill: currentColor; stroke: none; width: 20px; height: 20px; }
.v-body { display: grid; gap: 3px; flex: 1; }
.wave { display: flex; align-items: center; gap: 2px; height: 26px; cursor: pointer; }
.wave i { width: 3px; border-radius: 2px; background: color-mix(in srgb, var(--accent) 35%, transparent); flex: none; }
.wave i.on { background: var(--accent); }
.v-time { font-size: 12px; color: var(--muted); }

.circle-msg { position: relative; width: 220px; height: 220px; border-radius: 50%; overflow: hidden; background: #000; display: block; transition: transform 160ms var(--ease-out); }
.circle-msg:active { transform: scale(0.98); }
.circle-msg video { width: 100%; height: 100%; object-fit: cover; display: block; }
.c-ring { position: absolute; inset: 0; width: 100%; height: 100%; transform: rotate(-90deg); pointer-events: none; }
.c-ring circle { fill: none; stroke: rgba(255, 255, 255, .9); stroke-width: 3; stroke-linecap: round; transition: stroke-dashoffset 250ms linear; }
.c-play { position: absolute; inset: 0; margin: auto; width: 48px; height: 48px; border-radius: 50%; background: rgba(0, 0, 0, .45); color: #fff; display: grid; place-items: center; transition: opacity 160ms; }
.c-play .ic { fill: currentColor; stroke: none; }
.circle-msg.playing .c-play { opacity: 0; }
.c-dur { position: absolute; left: 50%; bottom: 12px; transform: translateX(-50%); font-size: 12px; color: #fff; background: rgba(0,0,0,.45); padding: 1px 7px; border-radius: 9px; }

.file { display: flex; align-items: center; gap: 10px; text-align: left; min-width: 0; }
.file-ic { width: 44px; height: 44px; border-radius: 50%; background: var(--accent); color: var(--accent-text); display: grid; place-items: center; flex: none; }
.file-text { display: grid; min-width: 0; }
.file-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 220px; }
.call-line { display: inline-flex; align-items: center; gap: 4px; font-weight: 500; }
.call-line.bad { color: var(--danger); }

/* ---------- ввод ---------- */
.composer { flex: none; background: var(--bg); border-top: 1px solid var(--line); padding: 6px 6px calc(6px + var(--safe-b)); position: relative; }
.compose-row { display: flex; align-items: flex-end; gap: 4px; }
.compose-input {
  flex: 1; resize: none; border: 0; outline: none; background: var(--bg-2); border-radius: 22px;
  padding: 11px 14px; min-height: 44px; max-height: 160px; line-height: 1.35;
}
.compose-actions { display: flex; gap: 2px; }
.rec-bar { display: flex; align-items: center; gap: 8px; min-height: 44px; }
.rec-info { flex: 1; display: flex; align-items: center; gap: 10px; justify-content: center; font-variant-numeric: tabular-nums; }
.rec-dot { width: 12px; height: 12px; border-radius: 50%; background: var(--danger); transition: transform 100ms linear; }
.rec-time { font-weight: 600; }
.rec-circle-wrap { position: absolute; left: 0; right: 0; bottom: calc(100% + 16px); display: grid; place-items: center; pointer-events: none; }
.rec-preview { width: min(70vw, 280px); aspect-ratio: 1; border-radius: 50%; object-fit: cover; transform: scaleX(-1); box-shadow: 0 10px 40px rgba(0,0,0,.35); border: 3px solid var(--accent); }

/* ---------- шторки, меню, тосты ---------- */
.sheet-wrap {
  position: fixed; inset: 0; z-index: 50; background: rgba(8, 12, 16, .42); display: flex; align-items: flex-end; justify-content: center;
  animation: fade 200ms var(--ease-out);
}
.sheet {
  width: 100%; max-width: 480px; max-height: 90dvh; overflow: hidden; display: flex; flex-direction: column;
  background: var(--bg); border-radius: 20px 20px 0 0; padding-bottom: var(--safe-b);
  animation: rise 300ms var(--ease-drawer);
}
@media (min-width: 761px) {
  .sheet-wrap { align-items: center; }
  .sheet { border-radius: 20px; animation: pop 200ms var(--ease-out); }
}
.sheet-wrap.closing { opacity: 0; transition: opacity 180ms; }
.sheet-head { display: flex; align-items: center; padding: 10px 8px 4px 20px; }
.sheet-head h2 { flex: 1; font-size: 18px; }
.sheet-body { overflow-y: auto; overscroll-behavior: contain; padding: 4px 20px 20px; display: grid; gap: 12px; align-content: start; }
.sheet-body h3 { margin-bottom: 0; }
.row-btns { display: flex; gap: 8px; flex-wrap: wrap; }
.row-btns .btn { flex: 1; }
.stack { display: grid; gap: 10px; }
.profile { display: flex; align-items: center; gap: 16px; }
.code { font-size: 17px; letter-spacing: .05em; line-height: 1.7; background: var(--bg-2); padding: 10px 14px; border-radius: 12px; word-spacing: .3em; user-select: text; }
.menu-item { display: flex; align-items: center; gap: 12px; width: 100%; padding: 12px; border-radius: 12px; text-align: left; font-weight: 500; }
.menu-item:active { background: var(--bg-2); }
.menu-item.danger { color: var(--danger); }
.admin-row { display: flex; align-items: center; gap: 10px; padding: 6px 0; }

.toasts { position: fixed; left: 0; right: 0; top: calc(12px + var(--safe-t)); z-index: 90; display: grid; justify-items: center; gap: 6px; pointer-events: none; padding: 0 16px; }
.toast { background: #22272e; color: #fff; padding: 10px 16px; border-radius: 14px; font-size: 14px; box-shadow: 0 6px 24px rgba(0,0,0,.25); animation: drop 220ms var(--ease-out); transition: opacity 180ms, transform 180ms; max-width: 420px; }
.toast.out { opacity: 0; transform: translateY(-6px); }

.viewer { position: fixed; inset: 0; z-index: 60; background: rgba(0,0,0,.92); display: grid; place-items: center; animation: fade 180ms var(--ease-out); }
.viewer img { max-width: 100%; max-height: 100dvh; object-fit: contain; }
.viewer-close { position: absolute; top: calc(8px + var(--safe-t)); right: 8px; color: #fff; }
.viewer.closing { opacity: 0; transition: opacity 160ms; }

/* ---------- звонок ---------- */
.call {
  position: fixed; inset: 0; z-index: 70; background: radial-gradient(120% 90% at 50% 20%, #24332f, #0b0f0e);
  color: #fff; animation: fade 220ms var(--ease-out); overflow: hidden;
}
.call.closing { opacity: 0; transition: opacity 180ms; }
.call video.remote { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; background: #000; }
.call video.remote.contain { object-fit: contain; }
.call-face { position: absolute; inset: 0; display: grid; place-items: center; padding-bottom: 80px; background: radial-gradient(120% 90% at 50% 20%, #24332f, #0b0f0e); }
.call-face .av { width: 128px; height: 128px; font-size: 44px; box-shadow: 0 0 0 10px rgba(255,255,255,.06), 0 0 0 22px rgba(255,255,255,.03); }
.call-top { position: absolute; top: 0; left: 0; right: 0; padding: calc(28px + var(--safe-t)) 16px 40px; text-align: center; background: linear-gradient(rgba(0,0,0,.45), transparent); }
.call-name { font-size: 24px; font-weight: 600; text-shadow: 0 1px 6px rgba(0,0,0,.4); }
.call-status { margin-top: 4px; opacity: .85; font-variant-numeric: tabular-nums; }
.call-e2e { margin-top: 6px; font-size: 12.5px; opacity: .75; }
.call-e2e .net.good { color: #7ee2b0; }
.call-e2e .net.mid { color: #ffd166; }
.call-e2e .net.bad { color: #ff8a8a; }
.call video.local {
  position: absolute; right: 12px; top: calc(118px + var(--safe-t)); width: 108px; aspect-ratio: 3 / 4; object-fit: cover;
  border-radius: 14px; background: #111; box-shadow: 0 6px 24px rgba(0,0,0,.45); z-index: 2;
}
.call video.local.mirror { transform: scaleX(-1); }
.call-controls {
  position: absolute; left: 0; right: 0; bottom: 0; display: flex; justify-content: center; gap: 8px; flex-wrap: wrap;
  padding: 24px 12px calc(28px + var(--safe-b)); background: linear-gradient(transparent, rgba(0,0,0,.55));
}
.call-btn { display: grid; justify-items: center; gap: 6px; width: 64px; font-size: 12px; color: rgba(255,255,255,.9); }
.call-btn .ic { width: 58px; height: 58px; padding: 16px; border-radius: 50%; background: rgba(255,255,255,.16); backdrop-filter: blur(8px); transition: transform 140ms var(--ease-out), background-color 140ms; }
.call-btn:active .ic { transform: scale(0.92); }
.call-btn.off .ic { background: #fff; color: #111; }
.call-btn.on .ic { background: var(--accent); }
.call-btn.red .ic { background: #e5484d; color: #fff; }
.call-btn.green .ic { background: #2fb36d; color: #fff; }

@keyframes fade { from { opacity: 0; } }
@keyframes rise { from { transform: translateY(100%); } }
@keyframes pop { from { opacity: 0; transform: scale(0.96); } }
@keyframes drop { from { opacity: 0; transform: translateY(-8px); } }
@media (prefers-reduced-motion: reduce) {
  .sheet, .sheet-wrap, .toast, .call, .viewer { animation-duration: 1ms; }
}
