:root {
  --font: 'Figtree', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  --bg: #f3f5f7;
  --panel: #ffffff;
  --ink: #151a21;
  --muted: #566170;
  --line: #e0e5ea;
  --accent: #0b7a6b;
  --on-accent: #ffffff;
  --out: #0a6b5e;
  --sel: #e4f2ef;
  --danger: #c0392b;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1217;
    --panel: #161b22;
    --ink: #e8ecf1;
    --muted: #9aa5b3;
    --line: #262d37;
    --accent: #35b8a4;
    --on-accent: #04201b;
    --out: #115c52;
    --sel: #1a2927;
    --danger: #ef6a5b;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; margin: 0; }
html, body { height: 100%; }
body {
  font: 16px/1.45 var(--font);
  background: var(--bg);
  color: var(--ink);
  -webkit-text-size-adjust: 100%;
  overflow: hidden;
}
[hidden] { display: none !important; }
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; -webkit-tap-highlight-color: transparent; }
button:disabled { cursor: not-allowed; opacity: .55; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.sprite { position: absolute; width: 0; height: 0; overflow: hidden; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.i { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.muted { color: var(--muted); }
.center { text-align: center; }

.screen {
  height: var(--vh, 100dvh);
  display: grid;
  place-items: center;
  padding: 24px;
  overflow: auto;
  color: var(--muted);
}

.welcome-card { width: min(100%, 400px); display: flex; flex-direction: column; gap: 12px; color: var(--ink); }
.welcome-card h1 { font-size: clamp(36px, 10vw, 52px); line-height: 1.02; letter-spacing: -.025em; font-weight: 700; }
.welcome-card .btn { margin-top: 8px; }

label { font-size: 14px; font-weight: 600; }
input {
  width: 100%;
  min-height: 48px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--bg);
  font-size: 16px;
}
input::placeholder, textarea::placeholder { color: var(--muted); opacity: .8; }

.btn {
  min-height: 44px;
  padding: 0 18px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--panel);
  font-weight: 600;
  transition: filter .15s, transform .08s;
}
.btn:hover:not(:disabled) { filter: brightness(.96); }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn.primary { background: var(--accent); color: var(--on-accent); border-color: transparent; }
.btn.ghost { border-color: transparent; color: var(--muted); }
.btn.small { min-height: 36px; padding: 0 12px; font-size: 14px; }

.icon-btn {
  width: 44px;
  height: 44px;
  border-radius: 12px;
  display: grid;
  place-items: center;
  color: var(--muted);
  flex: none;
}
.icon-btn:hover { background: var(--bg); color: var(--ink); }
.icon-btn:active { background: var(--line); }

.app {
  display: grid;
  grid-template-columns: minmax(320px, 380px) minmax(0, 1fr);
  height: var(--vh, 100dvh);
}

.side {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  background: var(--panel);
  border-right: 1px solid var(--line);
}
.side-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: calc(10px + var(--safe-t)) calc(8px + var(--safe-r)) 10px calc(16px + var(--safe-l));
  border-bottom: 1px solid var(--line);
}
.me { display: flex; align-items: center; gap: 12px; min-width: 0; }
.me strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.actions { display: flex; gap: 4px; }

.avatar {
  width: 44px;
  height: 44px;
  border-radius: 14px;
  display: grid;
  place-items: center;
  color: #fff;
  font-weight: 700;
  font-size: 15px;
  flex: none;
}

.list { list-style: none; padding: 0; overflow-y: auto; flex: 1; min-height: 0; overscroll-behavior: contain; padding-bottom: var(--safe-b); }
.item {
  display: grid;
  grid-template-columns: 44px minmax(0, 1fr) auto;
  gap: 12px;
  align-items: center;
  width: 100%;
  min-height: 68px;
  padding: 10px calc(16px + var(--safe-r)) 10px calc(16px + var(--safe-l));
  text-align: left;
}
.item:hover { background: var(--bg); }
.item.active { background: var(--sel); }
.meta { min-width: 0; }
.meta strong, .meta small { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.meta small { color: var(--muted); font-size: 14px; }
.item time { color: var(--muted); font-size: 12px; align-self: start; padding-top: 4px; }

.empty { padding: 32px 24px; text-align: center; color: var(--muted); display: flex; flex-direction: column; align-items: center; gap: 16px; }
.empty.big { flex: 1; justify-content: center; }

.chat { display: flex; flex-direction: column; min-width: 0; min-height: 0; background: var(--bg); }
.thread { display: flex; flex-direction: column; flex: 1; min-height: 0; }
.chat-head {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: calc(10px + var(--safe-t)) calc(12px + var(--safe-r)) 10px calc(12px + var(--safe-l));
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.back { display: none; margin-right: -4px; }
.peer { min-width: 0; display: flex; flex-direction: column; }
.peer strong, .peer small { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.peer small { color: var(--muted); font-size: 13px; letter-spacing: .06em; }

.messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 16px calc(16px + var(--safe-r)) 12px calc(16px + var(--safe-l));
}
.hint { margin: auto; color: var(--muted); text-align: center; }
.day {
  align-self: center;
  margin: 10px 0 6px;
  padding: 2px 12px;
  font-size: 12px;
  color: var(--muted);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
}

.msg {
  max-width: min(78%, 520px);
  padding: 8px 10px 6px;
  border-radius: 14px;
  background: var(--panel);
  border: 1px solid var(--line);
  overflow-wrap: anywhere;
}
.msg.in { align-self: flex-start; border-bottom-left-radius: 4px; }
.msg.out { align-self: flex-end; border-bottom-right-radius: 4px; background: var(--out); border-color: transparent; color: #fff; }
.msg .body { white-space: pre-wrap; }
.msg time { display: block; text-align: right; margin-top: 2px; font-size: 11px; color: var(--muted); }
.msg.out time { color: rgba(255, 255, 255, .88); }
.media { display: block; width: min(280px, 62vw); border-radius: 10px; overflow: hidden; background: #000; }
.media img { display: block; width: 100%; aspect-ratio: 4 / 3; object-fit: cover; }
video.media { aspect-ratio: 16 / 9; }
.msg audio { display: block; width: min(260px, 62vw); height: 40px; }
.file { display: flex; align-items: center; gap: 10px; width: min(260px, 62vw); color: inherit; text-decoration: none; min-height: 44px; }
.file span { min-width: 0; }
.file b { display: block; font-weight: 600; overflow-wrap: anywhere; }
.file small { opacity: .85; font-size: 12px; }

.composer {
  position: relative;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px calc(12px + var(--safe-r)) calc(10px + var(--safe-b)) calc(12px + var(--safe-l));
  background: var(--panel);
  border-top: 1px solid var(--line);
}
.field {
  flex: 1;
  min-width: 0;
  min-height: 44px;
  display: flex;
  align-items: center;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 14px;
}
.field:focus-within { outline: 2px solid var(--accent); outline-offset: 1px; }
.field textarea {
  flex: 1;
  min-width: 0;
  max-height: 140px;
  padding: 10px 14px;
  border: 0;
  outline: 0;
  resize: none;
  background: none;
  font-size: 16px;
  line-height: 1.4;
}
.send {
  width: 44px;
  height: 44px;
  border-radius: 14px;
  display: grid;
  place-items: center;
  background: var(--accent);
  color: var(--on-accent);
  flex: none;
  transition: filter .15s, transform .08s;
}
.send:hover { filter: brightness(.94); }
.send:active { transform: scale(.96); }
.send.live { background: var(--danger); color: #fff; }

.recbar { flex: 1; display: flex; align-items: center; gap: 10px; padding: 4px 6px 4px 14px; font-variant-numeric: tabular-nums; }
.recbar .btn { margin-left: auto; }
.dot { width: 10px; height: 10px; border-radius: 50%; background: var(--danger); animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .3; } }

.menu {
  position: absolute;
  left: calc(12px + var(--safe-l));
  bottom: calc(100% + 6px);
  display: flex;
  flex-direction: column;
  min-width: 190px;
  padding: 6px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .14);
  z-index: 5;
}
.menu button { display: flex; align-items: center; gap: 12px; min-height: 44px; padding: 0 12px; border-radius: 10px; text-align: left; }
.menu button:hover { background: var(--bg); }

dialog {
  width: min(92vw, 380px);
  max-height: 92dvh;
  margin: auto;
  padding: 0;
  border: 0;
  border-radius: 18px;
  background: var(--panel);
  color: var(--ink);
  overflow: auto;
}
dialog::backdrop { background: rgba(8, 12, 16, .6); }
.dlg { display: flex; flex-direction: column; gap: 14px; padding: 22px; }
.dlg h2 { font-size: 20px; letter-spacing: -.01em; }
.stack { display: flex; flex-direction: column; gap: 10px; }
.row { display: flex; gap: 10px; }
.row .btn { flex: 1; }
.qr { align-self: center; padding: 14px; background: #fff; border-radius: 14px; border: 1px solid var(--line); line-height: 0; }
.qr img, .qr canvas { width: 200px; height: 200px; max-width: 100%; }
.big-id { text-align: center; font-size: 30px; font-weight: 700; letter-spacing: .12em; font-variant-numeric: tabular-nums; }
.reader { width: 100%; min-height: 240px; border-radius: 12px; overflow: hidden; background: #000; }
.reader video { width: 100% !important; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + var(--safe-b));
  max-width: calc(100vw - 32px);
  padding: 10px 16px;
  border-radius: 12px;
  background: var(--ink);
  color: var(--panel);
  font-size: 14px;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 16px);
  transition: opacity .2s, transform .2s;
  z-index: 100;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 799px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .app.in-chat .side { display: none; }
  .app:not(.in-chat) .chat { display: none; }
  .back { display: grid; }
  .side { border-right: 0; }
  .msg { max-width: 86%; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}