:root {
  --bg: #f4f6f6;
  --panel: #ffffff;
  --text: #17201f;
  --muted: #64716f;
  --line: #dde3e2;
  --accent: #1f6f6b;
  --accent-text: #ffffff;
  --mine: #d7efe9;
  --theirs: #ffffff;
  --red: #c8372d;
  --green: #238a4a;
  --warn-bg: #fff3d6;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101615;
    --panel: #18201f;
    --text: #e8eeed;
    --muted: #93a19f;
    --line: #2a3533;
    --accent: #3fa39c;
    --accent-text: #06201e;
    --mine: #1d4a45;
    --theirs: #212b2a;
    --warn-bg: #4a3a12;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  font: 18px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; border-radius: 12px; padding: 10px 16px; background: transparent; touch-action: manipulation; }
button:disabled { opacity: .5; cursor: default; }
.primary { background: var(--accent); color: var(--accent-text); font-weight: 600; }
.ghost { color: var(--accent); }
.big { padding: 14px 20px; font-size: 20px; }
.sub { font-size: 14px; color: var(--muted); font-weight: 400; }
.error { color: var(--red); min-height: 1.4em; margin: 0; }

/* вход */
.login { min-height: 100%; display: grid; place-items: center; padding: 24px; }
.login-card {
  width: min(420px, 100%); display: grid; gap: 16px; text-align: center;
  background: var(--panel); padding: 32px 28px; border-radius: 24px; border: 1px solid var(--line);
}
.login-card img { margin: 0 auto; border-radius: 20px; }
.login-card h1 { margin: 0; font-size: 30px; }
.login-card label { display: grid; gap: 6px; text-align: left; color: var(--muted); font-size: 16px; }
.login-card input {
  padding: 14px; font-size: 20px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--bg); color: var(--text);
}

/* основная раскладка */
.app { height: 100vh; height: 100dvh; display: grid; grid-template-columns: minmax(280px, 360px) 1fr; }
.side, .chat { min-width: 0; min-height: 0; display: flex; flex-direction: column; }
.side { background: var(--panel); border-right: 1px solid var(--line); padding-left: env(safe-area-inset-left); }
.bar {
  display: flex; align-items: center; gap: 8px; min-height: 64px; flex: none;
  padding: calc(8px + env(safe-area-inset-top)) 12px 8px; border-bottom: 1px solid var(--line); background: var(--panel);
}
.bar-title { flex: 1; min-width: 0; font-weight: 700; font-size: 20px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.notice { margin: 10px; padding: 12px 14px; border-radius: 14px; background: var(--bg); border: 1px solid var(--line); font-size: 16px; }
.notice p { margin: 0 0 10px; }
.notice ol { margin: 0; padding-left: 22px; }
.notice.warn { background: var(--warn-bg); }
.share-icon { display: inline-block; border: 1.5px solid currentColor; border-radius: 5px; padding: 0 5px; font-size: 14px; }

.people { list-style: none; margin: 0; padding: 0 0 env(safe-area-inset-bottom); overflow-y: auto; flex: 1; }
.person {
  display: grid; grid-template-columns: auto 1fr auto; gap: 12px; align-items: center;
  padding: 12px 14px; border-bottom: 1px solid var(--line); cursor: pointer; min-height: 76px;
}
.person.active { background: var(--bg); }
.person-name { font-weight: 600; font-size: 19px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.person-last { color: var(--muted); font-size: 16px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge {
  background: var(--red); color: #fff; border-radius: 999px; min-width: 28px; height: 28px; padding: 0 8px;
  display: grid; place-items: center; font-size: 15px; font-weight: 700;
}
.avatar {
  width: 52px; height: 52px; border-radius: 50%; display: grid; place-items: center; position: relative;
  color: #fff; font-weight: 700; font-size: 22px; flex: none;
}
.avatar.online::after {
  content: ""; position: absolute; right: 0; bottom: 0; width: 15px; height: 15px; border-radius: 50%;
  background: #35c06b; border: 3px solid var(--panel);
}
.avatar.huge { width: 140px; height: 140px; font-size: 60px; }

/* чат */
.chat-empty { margin: auto; color: var(--muted); padding: 24px; text-align: center; font-size: 20px; }
.chat-box { display: flex; flex-direction: column; flex: 1; min-height: 0; }
.back { display: none; padding-left: 4px; font-size: 19px; }
.call-btn { background: var(--bg); border: 1px solid var(--line); font-weight: 600; white-space: nowrap; }
.msgs { flex: 1; overflow-y: auto; padding: 12px 14px; display: flex; flex-direction: column; -webkit-overflow-scrolling: touch; }
#msg-list { display: flex; flex-direction: column; gap: 6px; margin-top: auto; }
.older { align-self: center; }
.day { align-self: center; color: var(--muted); font-size: 14px; padding: 8px 0 2px; }
.msg {
  max-width: min(78%, 640px); padding: 9px 13px; border-radius: 18px; background: var(--theirs);
  border: 1px solid var(--line); align-self: flex-start; white-space: pre-wrap; overflow-wrap: anywhere;
}
.msg.mine { align-self: flex-end; background: var(--mine); border-color: transparent; }
.msg a { color: var(--accent); }
.msg .meta { display: block; text-align: right; font-size: 13px; color: var(--muted); margin-top: 2px; white-space: nowrap; }
.msg.callmsg { font-weight: 600; }
.msg.callmsg.missed { color: var(--red); }
.composer {
  display: flex; gap: 8px; align-items: flex-end; flex: none; background: var(--panel); border-top: 1px solid var(--line);
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
}
.composer textarea {
  flex: 1; resize: none; max-height: 160px; padding: 12px 14px; border-radius: 18px;
  border: 1px solid var(--line); background: var(--bg); line-height: 1.35;
}
.send { min-height: 50px; }

/* узкий экран: либо список, либо чат */
@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .app .chat { display: none; }
  .app.in-chat .side { display: none; }
  .app.in-chat .chat { display: flex; }
  .back { display: block; }
  .call-btn span { display: none; }
  .call-btn { font-size: 22px; padding: 8px 12px; }
}

/* звонок */
.call { position: fixed; inset: 0; z-index: 10; background: #0d1413; color: #fff; display: flex; flex-direction: column; }
#remote-video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; background: #0d1413; }
#local-video {
  position: absolute; right: 16px; top: calc(16px + env(safe-area-inset-top)); width: min(28vw, 200px); max-height: 30vh;
  border-radius: 14px; object-fit: cover; background: #222; z-index: 2; box-shadow: 0 4px 18px rgba(0, 0, 0, .5);
}
#local-video.mirror { transform: scaleX(-1); }
.call.no-video #remote-video, .call.no-video #local-video, .call.no-local #local-video { visibility: hidden; }
.call-info {
  position: relative; z-index: 1; flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 14px; text-align: center; padding: 24px; pointer-events: none;
}
.call.live:not(.no-video) .call-info { flex: none; padding-top: calc(16px + env(safe-area-inset-top)); align-items: flex-start; gap: 2px; text-shadow: 0 1px 4px #000; }
.call.live:not(.no-video) .avatar { display: none; }
.call.live:not(.no-video) .call-btns { margin-top: auto; }
.call-name { font-size: 34px; font-weight: 700; }
.call.live:not(.no-video) .call-name { font-size: 22px; }
.call-status { font-size: 22px; opacity: .85; }
.call-btns {
  position: relative; z-index: 2; display: flex; justify-content: center; gap: clamp(14px, 5vw, 48px); flex-wrap: wrap;
  padding: 20px 16px calc(28px + env(safe-area-inset-bottom));
}
.round { display: grid; justify-items: center; gap: 8px; color: #fff; padding: 0; font-size: 16px; text-shadow: 0 1px 4px #000; }
.round b {
  width: 76px; height: 76px; border-radius: 50%; display: grid; place-items: center; font-size: 32px;
  background: rgba(255, 255, 255, .18); font-weight: 400; text-shadow: none;
}
.round.red b { background: var(--red); }
.round.green b { background: var(--green); }
.round.off b { background: #fff; filter: grayscale(1); opacity: .65; }

.toast {
  position: fixed; left: 50%; bottom: calc(96px + env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 20;
  background: #222b2a; color: #fff; padding: 12px 20px; border-radius: 999px; max-width: 90vw; text-align: center;
  box-shadow: 0 4px 18px rgba(0, 0, 0, .35);
}
.button { display: inline-block; padding: 10px 16px; border-radius: 12px; text-decoration: none; }
