:root {
  color-scheme: light;
  --bg: #f4f4f2;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --hover: #ebeae6;
  --active: #dceafb;
  --border: #e2e1dc;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #8a8984;
  --accent: #2a78d6;
  --accent-ink: #fff;
  --mine: #dceafb;
  --mine-ink: #0d366b;
  --theirs: #f0efec;
  --danger: #e34948;
  --ok: #2eaa4f;
  --chat-bg: #f7f7f5;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121211;
    --surface: #1a1a19;
    --surface-2: #242422;
    --hover: #262624;
    --active: #183a63;
    --border: #2f2f2c;
    --text: #fff;
    --text-2: #c3c2b7;
    --muted: #8e8d86;
    --accent: #3987e5;
    --mine: #184f95;
    --mine-ink: #eaf2fd;
    --theirs: #2a2a28;
    --chat-bg: #141413;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
}
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--danger); min-height: 1.2em; }
button { font: inherit; color: inherit; }
input, textarea {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; width: 100%;
}
input:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.btn { border: 0; border-radius: 10px; padding: 11px 16px; cursor: pointer; background: var(--surface-2); }
.btn.primary { background: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.primary:disabled { opacity: .6; cursor: default; }

.icon-btn {
  width: 40px; height: 40px; border: 0; border-radius: 50%; background: transparent; cursor: pointer;
  display: inline-grid; place-items: center; flex: none; color: var(--text-2);
}
.icon-btn:hover { background: var(--hover); }
.icon-btn svg { width: 22px; height: 22px; fill: currentColor; }
.icon-btn:disabled { opacity: .35; cursor: default; background: transparent; }

/* ---------- вход ---------- */
.login-wrap { height: 100%; display: grid; place-items: center; padding: 16px; overflow: auto; }
.login-card { width: 100%; max-width: 380px; display: grid; gap: 14px; }
.login-card h1 { margin: 0; font-size: 26px; }
.login-card p { margin: -6px 0 6px; }
.login-card label { display: grid; gap: 5px; color: var(--text-2); }
.logo img { width: 56px; height: 56px; }

/* ---------- раскладка ---------- */
.layout { display: grid; grid-template-columns: minmax(280px, 340px) 1fr; height: 100%; }
.sidebar { display: flex; flex-direction: column; background: var(--surface); border-right: 1px solid var(--border); min-height: 0; }
.side-head { display: flex; align-items: center; gap: 8px; padding: 10px 10px 6px 14px; position: relative; }
.me { flex: 1; display: flex; align-items: center; gap: 10px; min-width: 0; font-weight: 600; }
.me span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.menu {
  position: absolute; right: 10px; top: 52px; z-index: 10; background: var(--surface);
  border: 1px solid var(--border); border-radius: 10px; box-shadow: 0 8px 24px rgba(0,0,0,.15); padding: 4px; min-width: 210px;
}
.menu button { display: block; width: 100%; text-align: left; border: 0; background: none; padding: 9px 12px; border-radius: 7px; cursor: pointer; }
.menu button:hover { background: var(--hover); }
.conn { margin: 0 12px 6px; padding: 6px 10px; border-radius: 8px; background: var(--surface-2); color: var(--text-2); font-size: 12px; }
.search-box { padding: 4px 12px 8px; }
.search-box input { background: var(--surface-2); border-color: transparent; padding: 8px 12px; }
.contacts { overflow-y: auto; flex: 1; }

.contact { display: flex; gap: 12px; align-items: center; padding: 9px 14px; cursor: pointer; }
.contact:hover { background: var(--hover); }
.contact.active { background: var(--active); }
.contact .body { flex: 1; min-width: 0; }
.contact .row { display: flex; align-items: baseline; gap: 8px; }
.contact .name { flex: 1; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.contact .time { font-size: 12px; color: var(--muted); flex: none; }
.contact .preview { flex: 1; color: var(--text-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; }
.contact.inactive .name { color: var(--muted); }
.badge { flex: none; min-width: 20px; height: 20px; padding: 0 6px; border-radius: 10px; background: var(--accent); color: #fff; font-size: 12px; font-weight: 600; display: grid; place-items: center; }

.avatar {
  position: relative; flex: none; border-radius: 50%; display: grid; place-items: center;
  color: #fff; font-weight: 600; user-select: none;
}
.avatar.dim { background: #9e9d98 !important; }
.avatar img { position: absolute; inset: 0; width: 100%; height: 100%; border-radius: 50%; object-fit: cover; }
.avatar.has-img img[src] + .ini { visibility: hidden; }
.avatar.dim img { filter: grayscale(1) opacity(.6); }
.avatar .online { position: absolute; right: -1px; bottom: -1px; width: 28%; height: 28%; border-radius: 50%; background: var(--ok); border: 2px solid var(--surface); }

/* ---------- чат ---------- */
.chat { display: flex; flex-direction: column; min-width: 0; min-height: 0; background: var(--chat-bg); position: relative; }
.empty-chat { margin: auto; text-align: center; color: var(--muted); }
.empty-chat img { width: 72px; opacity: .5; }
.chat-inner { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.chat-head { display: flex; align-items: center; gap: 10px; padding: 8px 12px; background: var(--surface); border-bottom: 1px solid var(--border); }
.chat-head .back { display: none; }
.peer-info { flex: 1; min-width: 0; }
.peer-name { font-weight: 600; font-size: 15px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.peer-status { font-size: 12px; color: var(--muted); }
.peer-status.online { color: var(--ok); }

.messages { flex: 1; overflow-y: auto; padding: 12px 16px; display: flex; flex-direction: column; gap: 3px; }
.messages .spacer { flex: 1; }
.day { align-self: center; margin: 10px 0 6px; padding: 3px 12px; border-radius: 12px; background: var(--surface-2); color: var(--text-2); font-size: 12px; }
.msg { display: flex; }
.msg.mine { justify-content: flex-end; }
.bubble {
  max-width: min(560px, 75%); padding: 7px 11px 5px; border-radius: 16px 16px 16px 4px;
  background: var(--theirs); white-space: pre-wrap; word-wrap: break-word; overflow-wrap: anywhere;
}
.msg.mine .bubble { background: var(--mine); color: var(--mine-ink); border-radius: 16px 16px 4px 16px; }
.meta { display: flex; justify-content: flex-end; align-items: center; gap: 3px; font-size: 11px; color: var(--muted); margin-top: 2px; }
.meta svg { width: 15px; height: 15px; fill: currentColor; }
.meta .read { color: var(--accent); }
.bubble a { color: inherit; }
.file { display: flex; align-items: center; gap: 10px; cursor: pointer; padding: 3px 0; }
.file .ic { width: 40px; height: 40px; border-radius: 50%; background: var(--accent); display: grid; place-items: center; flex: none; }
.file .ic svg { width: 20px; height: 20px; fill: #fff; }
.file .fname { font-weight: 600; word-break: break-all; }
.file .fsize { font-size: 12px; color: var(--muted); }
.img-prev { display: block; max-width: 320px; max-height: 320px; border-radius: 10px; cursor: zoom-in; margin: 2px 0; background: var(--surface-2); min-width: 120px; min-height: 80px; object-fit: cover; }
.callrec { display: flex; align-items: center; gap: 8px; }
.callrec svg { width: 20px; height: 20px; fill: var(--accent); }
.callrec.missed svg { fill: var(--danger); }
.callrec b { font-weight: 600; }
.pending .bubble { opacity: .75; }
.pending.failed .bubble { opacity: 1; outline: 1px solid var(--danger); }
.pending .err { color: var(--danger); font-size: 12px; margin-top: 3px; }
.pending .err button { border: 0; background: none; color: var(--accent); cursor: pointer; padding: 0 0 0 8px; }

.composer { display: flex; align-items: flex-end; gap: 6px; padding: 8px 10px; background: var(--surface); border-top: 1px solid var(--border); }
.composer textarea { resize: none; max-height: 180px; border-radius: 20px; background: var(--surface-2); border-color: transparent; padding: 10px 14px; line-height: 1.4; }
.composer .send { color: var(--accent); }
.blocked { padding: 14px; text-align: center; color: var(--muted); background: var(--surface); border-top: 1px solid var(--border); }
.drop-hint {
  position: absolute; inset: 60px 12px 12px; border: 2px dashed var(--accent); border-radius: 16px;
  display: grid; place-items: center; background: color-mix(in srgb, var(--chat-bg) 85%, transparent);
  color: var(--accent); font-size: 18px; font-weight: 600; pointer-events: none; z-index: 5;
}

/* ---------- звонок ---------- */
.call { position: fixed; inset: 0; z-index: 50; background: #101418; color: #fff; display: flex; flex-direction: column; align-items: center; }
#remote-video { display: none; }
.call.has-video #remote-video { display: block; position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; background: #000; }
#local-video { position: absolute; right: 20px; top: 20px; width: 220px; max-width: 30vw; aspect-ratio: 4/3; object-fit: cover; border-radius: 12px; background: #222; transform: scaleX(-1); z-index: 2; }
.call-info { position: relative; z-index: 1; margin-top: 12vh; text-align: center; text-shadow: 0 1px 4px rgba(0,0,0,.6); }
.call.has-video .call-info { margin-top: 20px; }
.call.has-video #call-avatar { display: none; }
.call-name { font-size: 28px; font-weight: 600; margin-top: 18px; }
.call-status { opacity: .8; font-size: 16px; margin-top: 4px; }
.call-controls { position: relative; z-index: 1; margin-top: auto; margin-bottom: 48px; display: flex; gap: 22px; align-items: flex-end; }
.cbtn { display: grid; justify-items: center; gap: 8px; font-size: 13px; color: rgba(255,255,255,.85); }
.cbtn button { width: 60px; height: 60px; border-radius: 50%; border: 0; cursor: pointer; display: grid; place-items: center; background: rgba(255,255,255,.16); color: #fff; }
.cbtn button:hover { filter: brightness(1.15); }
.cbtn button.on { background: #fff; color: #101418; }
.cbtn button.red { background: #e34948; width: 68px; height: 68px; }
.cbtn button.green { background: #2eaa4f; width: 68px; height: 68px; }
.cbtn svg { width: 28px; height: 28px; fill: currentColor; }

.viewer { position: fixed; inset: 0; z-index: 60; background: rgba(0,0,0,.85); display: grid; place-items: center; cursor: zoom-out; }
.viewer img { max-width: 95vw; max-height: 95vh; }

/* ---------- узкое окно ---------- */
@media (max-width: 720px) {
  .layout { grid-template-columns: 1fr; }
  .layout.chat-open .sidebar { display: none; }
  .layout:not(.chat-open) .chat { display: none; }
  .chat-head .back { display: inline-grid; }
  .bubble { max-width: 85%; }
}
