:root {
  --bg: #f4f1ea;
  --panel: #ffffff;
  --panel-2: #faf8f3;
  --ink: #1d2421;
  --muted: #6b746f;
  --line: #e4dfd3;
  --accent: #1f6f5c;
  --accent-ink: #ffffff;
  --accent-soft: #dcefe8;
  --mine: #d6ecdf;
  --theirs: #ffffff;
  --danger: #c0392b;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(0, 0, 0, .06), 0 4px 16px rgba(0, 0, 0, .06);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111513;
    --panel: #1a201d;
    --panel-2: #151a18;
    --ink: #e8ece9;
    --muted: #8f9a94;
    --line: #28312d;
    --accent: #3fae8f;
    --accent-ink: #0b1411;
    --accent-soft: #1b3a31;
    --mine: #1f4a3c;
    --theirs: #232b27;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}
[hidden] { display: none !important; }
button { font: inherit; color: inherit; cursor: pointer; }
input, textarea { font: inherit; color: inherit; }

/* ---------- вход ---------- */
.auth { min-height: 100%; display: grid; place-items: center; padding: 24px 16px; }
.card {
  width: min(420px, 100%); background: var(--panel); border-radius: 20px;
  padding: 28px 24px; box-shadow: var(--shadow); display: grid; gap: 14px;
}
.card .logo { justify-self: center; border-radius: 18px; }
.card h1 { margin: 0; text-align: center; font-size: 24px; }
.card label { display: grid; gap: 6px; font-size: 14px; color: var(--muted); }
.card input {
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px;
  padding: 12px 14px; font-size: 16px; color: var(--ink);
}
.card input:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 0; }
.tabs { display: grid; grid-template-columns: 1fr 1fr; background: var(--panel-2); border-radius: 10px; padding: 3px; }
.tab { border: 0; background: transparent; padding: 9px; border-radius: 8px; color: var(--muted); }
.tab.active { background: var(--panel); color: var(--ink); box-shadow: var(--shadow); font-weight: 600; }
.primary {
  background: var(--accent); color: var(--accent-ink); border: 0; border-radius: 10px;
  padding: 12px 16px; font-weight: 600;
}
.primary:disabled { opacity: .6; }
.error { color: var(--danger); margin: 0; font-size: 14px; }
.hint { margin: 0; font-size: 13px; color: var(--muted); text-align: center; }

/* ---------- каркас ---------- */
.app { height: 100dvh; display: grid; grid-template-columns: 330px 1fr; background: var(--bg); }
.sidebar { background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.side-head {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: calc(12px + env(safe-area-inset-top)) 14px 12px; border-bottom: 1px solid var(--line);
}
.me { display: flex; align-items: center; gap: 10px; min-width: 0; }
.me-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.side-actions { display: flex; gap: 2px; }
.avatar {
  width: 40px; height: 40px; border-radius: 50%; flex: none; display: grid; place-items: center;
  font-weight: 700; color: #fff; background: var(--accent); position: relative; user-select: none;
}
.avatar.online::after {
  content: ""; position: absolute; right: -1px; bottom: -1px; width: 12px; height: 12px;
  border-radius: 50%; background: #34c759; border: 2px solid var(--panel);
}
.icon-btn {
  width: 40px; height: 40px; border-radius: 50%; border: 0; background: transparent;
  display: grid; place-items: center; color: var(--muted); flex: none;
}
.icon-btn:hover { background: var(--panel-2); color: var(--ink); }
.icon-btn svg, .round svg, .send svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

.chat-list { overflow-y: auto; flex: 1; min-height: 0; }
.chat-item {
  display: flex; gap: 12px; align-items: center; padding: 10px 14px; width: 100%;
  background: transparent; border: 0; text-align: left; border-bottom: 1px solid var(--line);
}
.chat-item:hover { background: var(--panel-2); }
.chat-item.active { background: var(--accent-soft); }
.chat-item .body { min-width: 0; flex: 1; }
.chat-item .row { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
.chat-item .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-item .time { font-size: 12px; color: var(--muted); flex: none; }
.chat-item .preview { font-size: 14px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge {
  min-width: 20px; height: 20px; padding: 0 6px; border-radius: 10px; background: var(--accent);
  color: var(--accent-ink); font-size: 12px; font-weight: 700; display: inline-grid; place-items: center; flex: none;
}
.chat-item .live { color: var(--accent); font-weight: 600; }
.people { border-top: 1px solid var(--line); padding: 8px 0 calc(8px + env(safe-area-inset-bottom)); }
.people h2 { margin: 6px 14px; font-size: 13px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }

.main { min-width: 0; min-height: 0; display: flex; }
.empty { margin: auto; color: var(--muted); }
.chat-view { display: flex; flex-direction: column; width: 100%; min-height: 0; }
.chat-head {
  display: flex; align-items: center; gap: 6px; background: var(--panel); border-bottom: 1px solid var(--line);
  padding: calc(8px + env(safe-area-inset-top)) 10px 8px;
}
.chat-title { flex: 1; min-width: 0; display: grid; }
.chat-title span { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-title small { color: var(--muted); }
.back { display: none; }

.call-banner {
  display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap;
  background: var(--accent-soft); padding: 8px 14px; border-bottom: 1px solid var(--line); font-size: 14px;
}
.small { border: 1px solid var(--line); background: var(--panel); border-radius: 8px; padding: 6px 10px; font-size: 14px; }
.small.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }

/* ---------- сообщения ---------- */
.messages {
  flex: 1; min-height: 0; overflow-y: auto; padding: 12px 12px 6px; display: flex; flex-direction: column; gap: 4px;
  overscroll-behavior: contain;
}
.day { align-self: center; font-size: 12px; color: var(--muted); background: var(--panel); border-radius: 10px; padding: 3px 10px; margin: 8px 0; }
.more { align-self: center; margin: 4px 0 8px; }
.msg { max-width: min(78%, 560px); display: flex; flex-direction: column; }
.msg.mine { align-self: flex-end; }
.bubble {
  background: var(--theirs); border-radius: var(--radius); border-top-left-radius: 4px; padding: 7px 11px 5px;
  box-shadow: 0 1px 1px rgba(0, 0, 0, .06); overflow-wrap: anywhere; white-space: pre-wrap;
}
.msg.mine .bubble { background: var(--mine); border-radius: var(--radius); border-top-right-radius: 4px; }
.msg .author { font-size: 13px; font-weight: 600; color: var(--accent); margin-bottom: 1px; }
.msg .meta { font-size: 11px; color: var(--muted); text-align: right; margin-top: 2px; }
.bubble a { color: var(--accent); }
.bubble img.media, .bubble video.media { display: block; max-width: 100%; max-height: 360px; border-radius: 10px; cursor: zoom-in; background: #000; }
.bubble audio { display: block; width: 100%; min-width: 220px; margin-top: 2px; }
.filecard {
  display: flex; gap: 10px; align-items: center; text-decoration: none; color: inherit !important;
  background: rgba(0, 0, 0, .05); padding: 8px 10px; border-radius: 10px; min-width: 200px;
}
.filecard .ico { width: 36px; height: 36px; border-radius: 8px; background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; font-size: 11px; font-weight: 700; flex: none; }
.filecard .fname { font-weight: 600; overflow-wrap: anywhere; line-height: 1.2; }
.filecard .fsize { font-size: 12px; color: var(--muted); }
.typing { height: 20px; padding: 0 16px; font-size: 13px; color: var(--muted); }
.uploads { padding: 0 12px; display: grid; gap: 4px; }
.upload { font-size: 13px; background: var(--panel); border-radius: 8px; padding: 6px 10px; position: relative; overflow: hidden; }
.upload i { position: absolute; inset: 0 auto 0 0; background: var(--accent-soft); z-index: 0; }
.upload span { position: relative; z-index: 1; }

.composer {
  display: flex; align-items: flex-end; gap: 6px; background: var(--panel); border-top: 1px solid var(--line);
  padding: 8px 10px calc(8px + env(safe-area-inset-bottom));
}
.composer textarea {
  flex: 1; resize: none; max-height: 140px; border: 1px solid var(--line); border-radius: 20px; padding: 10px 14px;
  background: var(--panel-2); line-height: 1.3;
}
.send {
  width: 42px; height: 42px; border-radius: 50%; border: 0; background: var(--accent); color: var(--accent-ink);
  display: grid; place-items: center; flex: none;
}

/* ---------- звонок ---------- */
.call { position: fixed; inset: 0; z-index: 50; background: #0b0f0d; color: #fff; display: flex; flex-direction: column; }
.call-top {
  display: flex; justify-content: space-between; gap: 8px; padding: calc(12px + env(safe-area-inset-top)) 16px 8px;
  font-weight: 600;
}
.call-status { font-weight: 400; opacity: .7; font-size: 14px; }
.call-grid {
  flex: 1; min-height: 0; padding: 8px; display: grid; gap: 8px; align-content: center;
  grid-template-columns: 1fr;
}
.call-grid[data-n="2"], .call-grid[data-n="3"], .call-grid[data-n="4"] { grid-template-columns: repeat(2, 1fr); }
.call-grid[data-n="5"], .call-grid[data-n="6"], .call-grid[data-n="7"], .call-grid[data-n="8"], .call-grid[data-n="9"] { grid-template-columns: repeat(3, 1fr); }
@media (max-width: 700px) and (orientation: portrait) {
  .call-grid[data-n="2"] { grid-template-columns: 1fr; }
}
.tile {
  position: relative; background: #1b2420; border-radius: 16px; overflow: hidden; min-height: 120px;
  display: grid; place-items: center; aspect-ratio: 4 / 3; max-height: 100%;
}
.tile video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; background: #000; }
.tile.local video { transform: scaleX(-1); }
.tile.screen video { object-fit: contain; transform: none; }
.tile .avatar { width: 84px; height: 84px; font-size: 32px; }
.tile.hasvideo .avatar { display: none; }
.tile .tname {
  position: absolute; left: 8px; bottom: 8px; background: rgba(0, 0, 0, .55); padding: 3px 9px; border-radius: 10px; font-size: 13px;
}
.tile.speaking { outline: 3px solid #34c759; outline-offset: -3px; }
.tile.muted .tname::after { content: " · без звука"; opacity: .8; }
.call-controls {
  display: flex; justify-content: center; gap: 16px; padding: 12px 16px calc(20px + env(safe-area-inset-bottom));
}
.round {
  width: 56px; height: 56px; border-radius: 50%; border: 0; background: #2a3430; color: #fff;
  display: grid; place-items: center;
}
.round.off { background: #fff; color: #111; }
.round.danger { background: var(--danger); }

/* ---------- уведомления ---------- */
.toasts { position: fixed; left: 0; right: 0; bottom: calc(80px + env(safe-area-inset-bottom)); z-index: 90; display: grid; justify-items: center; gap: 6px; pointer-events: none; padding: 0 12px; }
.toast { background: #222; color: #fff; padding: 10px 14px; border-radius: 12px; max-width: 460px; font-size: 14px; box-shadow: var(--shadow); }
.lightbox { position: fixed; inset: 0; z-index: 80; background: rgba(0, 0, 0, .9); display: grid; place-items: center; }
.lightbox img { max-width: 96vw; max-height: 92vh; object-fit: contain; }

/* ---------- телефон ---------- */
@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .main { display: none; }
  .app.chat-open .sidebar { display: none; }
  .app.chat-open .main { display: flex; }
  .back { display: grid; }
  .msg { max-width: 88%; }
}

/* ---------- входящий звонок ---------- */
.incoming {
  position: fixed; left: 12px; right: 12px; top: calc(12px + env(safe-area-inset-top)); z-index: 70;
  max-width: 520px; margin: 0 auto; background: var(--panel); border: 1px solid var(--accent); border-radius: 16px;
  box-shadow: var(--shadow); padding: 12px 14px; display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap;
  animation: pulse 1.6s ease-in-out infinite;
}
.incoming-actions { display: flex; gap: 8px; }
@keyframes pulse { 50% { box-shadow: 0 0 0 4px var(--accent-soft); } }
@media (prefers-reduced-motion: reduce) { .incoming { animation: none; } }
