:root {
  --bg: #f6f5f2;
  --surface: #ffffff;
  --surface-2: #efede8;
  --line: #e3e0d9;
  --text: #17161a;
  --muted: #6d6a73;
  --accent: #5a3ff0;
  --accent-ink: #ffffff;
  --accent-soft: #ebe7ff;
  --bubble-out: #5a3ff0;
  --bubble-out-ink: #ffffff;
  --bubble-in: #ffffff;
  --danger: #c4322c;
  --ok: #1f8f5f;
  --radius: 18px;
  --ease: cubic-bezier(0.2, 0.8, 0.2, 1);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Noto Sans", "Helvetica Neue", Arial, sans-serif;
  --top: env(safe-area-inset-top, 0px);
  --bottom: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121214;
    --surface: #1b1b1f;
    --surface-2: #242429;
    --line: #2d2d33;
    --text: #f1f0f4;
    --muted: #9b99a4;
    --accent: #8b78ff;
    --accent-ink: #120d2b;
    --accent-soft: #2a2550;
    --bubble-out: #6a52ff;
    --bubble-out-ink: #ffffff;
    --bubble-in: #232328;
    --danger: #ff6b63;
    --ok: #4cc28d;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overscroll-behavior: none; }
html { -webkit-tap-highlight-color: transparent; -webkit-text-size-adjust: 100%; }
body {
  font: 16px/1.4 var(--font);
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}
button, input, textarea { font: inherit; color: inherit; }
button {
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
  cursor: pointer;
}
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
svg.i { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

.app {
  height: 100dvh;
  max-width: 720px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  position: relative;
  overflow: hidden;
}
@media (min-width: 760px) {
  .app { border-inline: 1px solid var(--line); }
}

/* ---------- верхняя панель ---------- */
.bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(10px + var(--top)) 12px 10px;
  min-height: calc(60px + var(--top));
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(1.4) blur(14px);
  -webkit-backdrop-filter: saturate(1.4) blur(14px);
  border-bottom: 1px solid var(--line);
  position: relative;
  z-index: 2;
}
.bar h1 { font-size: 26px; font-weight: 700; letter-spacing: -0.02em; margin: 0 0 0 6px; flex: 1; }
.bar .title { flex: 1; min-width: 0; display: flex; align-items: center; gap: 10px; background: none; border: 0; padding: 4px; border-radius: 12px; text-align: left; }
.bar .title b { display: block; font-size: 17px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bar .title small { display: flex; align-items: center; gap: 4px; color: var(--muted); font-size: 13px; }
.bar .title small svg.i { width: 13px; height: 13px; }
.net { font-size: 13px; color: var(--muted); padding: 0 6px; }

.icon-btn {
  width: 44px; height: 44px;
  display: grid; place-items: center;
  border: 0; border-radius: 50%;
  background: transparent;
  color: var(--text);
  transition: background 120ms var(--ease), transform 120ms var(--ease);
}
.icon-btn:active { transform: scale(0.94); background: var(--surface-2); }
.icon-btn.accent { color: var(--accent); }
@media (hover: hover) and (pointer: fine) {
  .icon-btn:hover { background: var(--surface-2); }
}

/* ---------- кнопки ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 50px; padding: 0 22px;
  border: 0; border-radius: 14px;
  background: var(--accent); color: var(--accent-ink);
  font-weight: 600;
  transition: transform 120ms var(--ease), filter 120ms var(--ease);
}
.btn:active { transform: scale(0.98); filter: brightness(0.95); }
.btn:disabled { opacity: 0.5; }
.btn.ghost { background: var(--surface-2); color: var(--text); }
.btn.danger { background: transparent; color: var(--danger); }
.btn svg.i { width: 20px; height: 20px; }

/* ---------- онбординг ---------- */
.welcome {
  flex: 1;
  display: flex; flex-direction: column; justify-content: center;
  padding: calc(32px + var(--top)) 28px calc(32px + var(--bottom));
  gap: 28px;
}
.mark { width: 64px; height: 64px; border-radius: 20px; }
.welcome h2 { font-size: 34px; line-height: 1.1; letter-spacing: -0.025em; margin: 0; }
.welcome p { margin: 0; color: var(--muted); font-size: 17px; max-width: 34ch; }
.welcome .invite-note { color: var(--text); background: var(--accent-soft); padding: 14px 16px; border-radius: 14px; }
.field { display: flex; flex-direction: column; gap: 8px; }
.field label { font-size: 14px; color: var(--muted); }
.field input {
  min-height: 52px; padding: 0 16px;
  font-size: 17px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 14px;
}
.field input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.points { list-style: none; margin: 0; padding: 0; display: grid; gap: 12px; }
.points li { display: flex; gap: 12px; align-items: flex-start; color: var(--muted); font-size: 15px; }
.points svg.i { color: var(--accent); width: 20px; height: 20px; margin-top: 1px; }

/* ---------- список чатов ---------- */
.scroll { flex: 1; overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; }
.chats { list-style: none; margin: 0; padding: 4px 0; }
.chat-row {
  width: 100%;
  display: flex; align-items: center; gap: 12px;
  padding: 10px 16px;
  border: 0; background: transparent; text-align: left;
  transition: background 120ms var(--ease);
}
.chat-row:active { background: var(--surface-2); }
@media (hover: hover) and (pointer: fine) {
  .chat-row:hover { background: var(--surface-2); }
}
.chat-row .body { flex: 1; min-width: 0; border-bottom: 1px solid var(--line); padding: 4px 0 12px; }
.chat-row:last-child .body { border-bottom-color: transparent; }
.chat-row .top, .chat-row .bottom { display: flex; align-items: center; gap: 8px; }
.chat-row .name { flex: 1; min-width: 0; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-row time { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.chat-row .last { flex: 1; min-width: 0; color: var(--muted); font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: flex; gap: 4px; align-items: center; }
.chat-row .last svg.i { width: 16px; height: 16px; }
.badge { min-width: 22px; height: 22px; padding: 0 7px; border-radius: 11px; background: var(--accent); color: var(--accent-ink); font-size: 13px; font-weight: 600; display: grid; place-items: center; font-variant-numeric: tabular-nums; }
.unread .last { color: var(--text); }
.unread time { color: var(--accent); }

.avatar {
  --h: 260;
  width: 52px; height: 52px; border-radius: 50%; flex: none;
  display: grid; place-items: center;
  font-weight: 650; font-size: 19px; letter-spacing: -0.01em;
  color: hsl(var(--h) 55% 28%);
  background: linear-gradient(140deg, hsl(var(--h) 85% 88%), hsl(calc(var(--h) + 40) 80% 80%));
}
.avatar.sm { width: 38px; height: 38px; font-size: 15px; }
@media (prefers-color-scheme: dark) {
  .avatar { color: hsl(var(--h) 80% 90%); background: linear-gradient(140deg, hsl(var(--h) 45% 34%), hsl(calc(var(--h) + 40) 45% 26%)); }
}

.empty {
  flex: 1;
  display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center;
  gap: 14px; padding: 32px 28px;
  color: var(--muted);
}
.empty h3 { margin: 0; color: var(--text); font-size: 20px; }
.empty p { margin: 0; max-width: 32ch; }
.empty .glyph { width: 72px; height: 72px; border-radius: 24px; display: grid; place-items: center; background: var(--accent-soft); color: var(--accent); }
.empty .glyph svg.i { width: 32px; height: 32px; }

/* ---------- нижние вкладки ---------- */
.tabs {
  display: grid; grid-template-columns: repeat(4, 1fr);
  padding: 6px 4px calc(6px + var(--bottom));
  background: color-mix(in srgb, var(--bg) 90%, transparent);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border-top: 1px solid var(--line);
}
.tabs button {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 6px 0; border: 0; background: none;
  color: var(--muted); font-size: 12px; font-weight: 500;
  transition: color 120ms var(--ease), transform 120ms var(--ease);
}
.tabs button:active { transform: scale(0.95); }
.tabs button[aria-current="page"] { color: var(--accent); }
.tabs button[aria-current="page"] svg.i { stroke-width: 2.2; }

/* ---------- чат ---------- */
.thread { padding: 12px 12px 8px; display: flex; flex-direction: column; gap: 2px; }
.day { align-self: center; margin: 14px 0 8px; padding: 4px 12px; border-radius: 12px; font-size: 13px; color: var(--muted); background: var(--surface-2); }
.e2e-note { align-self: center; max-width: 34ch; text-align: center; margin: 8px 0 12px; padding: 10px 14px; border-radius: 14px; font-size: 13px; color: var(--muted); background: var(--surface); border: 1px solid var(--line); display: flex; gap: 8px; align-items: flex-start; }
.e2e-note svg.i { width: 16px; height: 16px; color: var(--accent); margin-top: 1px; }
.msg {
  max-width: min(78%, 480px);
  padding: 8px 12px 6px;
  border-radius: var(--radius);
  background: var(--bubble-in);
  box-shadow: 0 1px 0 color-mix(in srgb, var(--text) 6%, transparent);
  white-space: pre-wrap; overflow-wrap: anywhere;
  align-self: flex-start;
}
.msg.fresh { animation: rise 200ms var(--ease); }
.msg.out { align-self: flex-end; background: var(--bubble-out); color: var(--bubble-out-ink); }
.msg + .msg:not(.out), .msg.out + .msg.out { margin-top: 0; }
.msg:not(.out) + .msg.out, .msg.out + .msg:not(.out) { margin-top: 8px; }
.msg .meta { float: right; display: inline-flex; align-items: center; gap: 3px; margin: 6px 0 -2px 10px; font-size: 11.5px; opacity: 0.72; font-variant-numeric: tabular-nums; }
.msg .meta svg.i { width: 15px; height: 15px; stroke-width: 2; }
.msg .meta .read { color: #b9f0d6; opacity: 1; }
.msg:not(.out) .meta { color: var(--muted); opacity: 1; }
.msg .meta .failed { color: #ffd1cf; }
@keyframes rise { from { opacity: 0; transform: translateY(6px); } }
@media (prefers-reduced-motion: reduce) { .msg.fresh { animation: none; } }

.composer {
  display: flex; align-items: flex-end; gap: 8px;
  padding: 8px 10px calc(8px + var(--bottom));
  border-top: 1px solid var(--line);
  background: var(--bg);
}
.composer textarea {
  flex: 1; resize: none;
  min-height: 44px; max-height: 160px;
  padding: 11px 16px;
  font-size: 16px; line-height: 1.35;
  border: 1px solid var(--line); border-radius: 22px;
  background: var(--surface);
}
.composer textarea:focus { outline: none; border-color: var(--accent); }
.send {
  width: 44px; height: 44px; border-radius: 50%; border: 0;
  display: grid; place-items: center;
  background: var(--accent); color: var(--accent-ink);
  transition: transform 120ms var(--ease), opacity 120ms var(--ease);
}
.send:disabled { opacity: 0.35; }
.send:active:not(:disabled) { transform: scale(0.92); }
.send svg.i { stroke-width: 2.4; }

/* ---------- шторка ---------- */
.scrim { position: absolute; inset: 0; background: rgb(0 0 0 / 0.36); z-index: 10; animation: fade 180ms var(--ease); }
.sheet {
  position: absolute; left: 0; right: 0; bottom: 0; z-index: 11;
  max-height: 88%; overflow-y: auto; overscroll-behavior: contain;
  padding: 10px 20px calc(20px + var(--bottom));
  background: var(--surface);
  border-radius: 24px 24px 0 0;
  display: flex; flex-direction: column; gap: 16px;
  animation: up 260ms var(--ease);
}
.sheet::before { content: ""; align-self: center; width: 40px; height: 5px; border-radius: 3px; background: var(--line); }
.sheet h3 { margin: 0; font-size: 21px; }
.sheet p { margin: 0; color: var(--muted); }
.sheet .row { display: flex; gap: 10px; }
.sheet .row .btn { flex: 1; }
.link-box { padding: 14px 16px; border-radius: 14px; background: var(--surface-2); font-size: 14px; word-break: break-all; user-select: all; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.code { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px 8px; padding: 16px; border-radius: 14px; background: var(--surface-2); font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 17px; text-align: center; font-variant-numeric: tabular-nums; }
.kv { display: grid; gap: 4px; font-size: 14px; color: var(--muted); }
.kv code { color: var(--text); font-size: 13px; word-break: break-all; }
@keyframes up { from { transform: translateY(100%); } }
@keyframes fade { from { opacity: 0; } }
@media (prefers-reduced-motion: reduce) { .sheet, .scrim { animation: none; } }

.toast {
  position: absolute; left: 50%; bottom: calc(90px + var(--bottom)); z-index: 20;
  transform: translateX(-50%);
  max-width: calc(100% - 32px);
  padding: 12px 18px; border-radius: 14px;
  background: var(--text); color: var(--bg);
  font-size: 15px;
  animation: fade 160ms var(--ease);
}
.spinner { width: 28px; height: 28px; border-radius: 50%; border: 3px solid var(--accent-soft); border-top-color: var(--accent); animation: spin 0.8s linear infinite; margin: auto; }
@keyframes spin { to { transform: rotate(1turn); } }
.sprite { position: absolute; }
