:root {
  color-scheme: light dark;
  --bg: #ffffff;
  --surface: #f4f5f7;
  --surface-2: #e9ebef;
  --text: #15181d;
  --muted: #68707c;
  --line: #e3e5ea;
  --accent: #1f6feb;
  --accent-soft: #e4eefe;
  --on-accent: #ffffff;
  --bubble-in: #f0f1f4;
  --bubble-out: #d8e7fe;
  --feed: #ffffff;
  --danger: #cf3a3a;
  --ok: #2d9b5a;
  --backdrop: rgba(10, 14, 20, .45);
  --radius: 16px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 16px;
  line-height: 1.4;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171c;
    --surface: #1c2027;
    --surface-2: #272c35;
    --text: #eceef2;
    --muted: #949caa;
    --line: #2a2f38;
    --accent: #5a9bff;
    --accent-soft: #1f2d45;
    --on-accent: #0d1420;
    --bubble-in: #21252d;
    --bubble-out: #24406e;
    --feed: #14171c;
    --danger: #ef6b6b;
    --ok: #52c07f;
    --backdrop: rgba(0, 0, 0, .6);
  }
}

/* Щипок и двойное касание не масштабируют интерфейс. touch-action задан всем:
   внутри прокручиваемых областей браузер смотрит на них, а не на корень страницы */
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; touch-action: pan-x pan-y; }
html, body { height: 100%; margin: 0; background: var(--bg); color: var(--text); overscroll-behavior: none; -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
#app { height: 100dvh; }
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
button:focus-visible, a:focus-visible, input:focus-visible, textarea:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
h1, h2, h3, p { margin: 0; }
a { color: var(--accent); }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.center { text-align: center; }
.ico { display: inline-flex; width: 24px; height: 24px; flex: none; }
.ico svg { width: 100%; height: 100%; }

.input {
  width: 100%; padding: 12px 14px; font: inherit; color: var(--text);
  background: var(--surface); border: 1px solid transparent; border-radius: 12px;
}
.input:focus { outline: none; border-color: var(--accent); }
.btn {
  display: block; width: 100%; padding: 13px 16px; border-radius: 12px; font-weight: 600;
  text-align: center; text-decoration: none; min-height: 48px;
}
.btn.primary { background: var(--accent); color: var(--on-accent); }
.btn.ghost { background: var(--surface); color: var(--text); }
.btn.danger { background: var(--danger); color: #fff; }
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 44px; height: 44px; border-radius: 50%; flex: none; color: var(--muted);
}
@media (hover: hover) { .icon-btn:hover { background: var(--surface); } }
.stack { display: flex; flex-direction: column; gap: 10px; }

/* ---------- экраны входа ---------- */
.gate {
  min-height: 100dvh; display: flex; justify-content: center; align-items: center;
  padding: calc(24px + var(--safe-top)) 20px calc(24px + var(--safe-bottom));
}
.gate-inner { width: 100%; max-width: 380px; display: flex; flex-direction: column; gap: 14px; }
.gate h1 { font-size: 1.5rem; line-height: 1.25; text-align: center; }
.gate-inner > .muted { text-align: center; }
.gate-mark {
  align-self: center; width: 72px; height: 72px; border-radius: 50%; color: var(--accent);
  background: var(--accent-soft); display: flex; align-items: center; justify-content: center;
}
.gate-mark.ok { color: var(--ok); }
.notice { background: var(--accent-soft); padding: 12px 14px; border-radius: 12px; }

.pin { display: flex; flex-direction: column; align-items: center; gap: 12px; }
.pin-sub { text-align: center; min-height: 1.4em; }
.pin-dots { display: flex; gap: 14px; margin: 14px 0 4px; }
.pin-dot { width: 14px; height: 14px; border-radius: 50%; background: var(--surface-2); transition: background .1s; }
.pin-dot.on { background: var(--accent); }
.pin-error { color: var(--danger); text-align: center; min-height: 1.4em; font-size: .95rem; }
.pin-keys { display: grid; grid-template-columns: repeat(3, 76px); gap: 12px 20px; justify-content: center; margin-top: 6px; }
.pin-key {
  height: 76px; border-radius: 50%; background: var(--surface); font-size: 1.6rem;
  display: flex; align-items: center; justify-content: center; user-select: none;
}
.pin-key:active { background: var(--surface-2); }
.pin-key.plain { background: none; color: var(--muted); }
.shake { animation: shake .3s; }
@keyframes shake { 25% { transform: translateX(-7px); } 50% { transform: translateX(7px); } 75% { transform: translateX(-4px); } }

.link-body { display: flex; flex-direction: column; gap: 14px; }
.qr-box {
  align-self: center; width: min(280px, 78vw); aspect-ratio: 1; padding: 10px; background: #fff; border-radius: var(--radius);
  display: flex; align-items: center; justify-content: center; border: 1px solid var(--line);
}
.qr-box img { width: 100%; height: 100%; display: block; }
.steps { margin: 0; padding-left: 22px; display: flex; flex-direction: column; gap: 4px; }
.spinner {
  width: 30px; height: 30px; margin: 24px auto; border-radius: 50%;
  border: 3px solid var(--surface-2); border-top-color: var(--accent); animation: spin .9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 3s; } .shake { animation: none; } }

/* ---------- каркас ---------- */
.shell { height: 100dvh; display: grid; grid-template-columns: 1fr; overflow: hidden; }
.pane { min-width: 0; min-height: 0; display: flex; flex-direction: column; background: var(--bg); height: 100dvh; }
.pane-thread { display: none; }
.shell.show-thread .pane-list { display: none; }
.shell.show-thread .pane-thread { display: flex; }
@media (min-width: 820px) {
  .shell { grid-template-columns: 340px 1fr; }
  .pane-list { display: flex !important; border-right: 1px solid var(--line); }
  .pane-thread { display: flex; }
  .icon-btn.back { display: none; }
}
.bar {
  display: flex; align-items: center; gap: 6px; flex: none; min-height: 56px;
  padding: calc(6px + var(--safe-top)) 8px 6px 16px; background: var(--bg); border-bottom: 1px solid var(--line);
}
.bar-title { flex: 1; font-size: 1.25rem; font-weight: 700; min-width: 0; }
.conn-banner { background: var(--accent-soft); padding: 7px 16px; font-size: .9rem; text-align: center; }
.search-wrap { padding: 8px 12px; flex: none; }
.search { padding: 9px 14px; }
.empty { margin: auto; padding: 32px 20px; color: var(--muted); text-align: center; display: flex; flex-direction: column; gap: 12px; }

/* ---------- список чатов ---------- */
.chat-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; -webkit-overflow-scrolling: touch; padding-bottom: var(--safe-bottom); }
.chat-row { display: flex; align-items: center; gap: 12px; padding: 9px 14px; width: 100%; text-align: left; }
.chat-row:active { background: var(--surface); }
@media (hover: hover) { .chat-row:hover { background: var(--surface); } }
.chat-row.active { background: var(--accent-soft); }
.avatar {
  position: relative; display: inline-flex; flex: none; align-items: center; justify-content: center;
  width: 52px; height: 52px; border-radius: 50%; overflow: hidden; color: #fff; font-weight: 600; font-size: 1.05rem;
}
.avatar.small { width: 40px; height: 40px; font-size: .9rem; }
.avatar img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.chat-main { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.chat-top, .chat-bottom { display: flex; align-items: baseline; gap: 8px; min-width: 0; }
.chat-bottom { align-items: center; }
.chat-title { flex: 1; min-width: 0; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-time { flex: none; font-size: .78rem; color: var(--muted); }
.chat-preview { flex: 1; min-width: 0; color: var(--muted); font-size: .93rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-prefix { color: var(--text); }
.badge {
  flex: none; min-width: 22px; height: 22px; padding: 0 7px; border-radius: 11px; background: var(--accent);
  color: var(--on-accent); font-size: .78rem; font-weight: 700; display: flex; align-items: center; justify-content: center;
}

/* ---------- переписка ---------- */
.thread-bar { padding-left: 4px; gap: 8px; }
.thread-head { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.thread-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.thread-sub { font-size: .8rem; color: var(--muted); }
.thread-sub:empty { display: none; }
.thread-sub.typing { color: var(--accent); }
.thread-body { position: relative; flex: 1; min-height: 0; display: flex; }
.scroller { flex: 1; overflow-y: auto; background: var(--feed); -webkit-overflow-scrolling: touch; overscroll-behavior: contain; }
.feed { display: flex; flex-direction: column; width: 100%; min-height: 100%; justify-content: flex-end; padding: 10px 10px 8px; max-width: 760px; margin: 0 auto; }
.to-bottom {
  position: absolute; right: 12px; bottom: 12px; width: 42px; height: 42px; border-radius: 50%;
  background: var(--bg); color: var(--muted); border: 1px solid var(--line);
  display: flex; align-items: center; justify-content: center; box-shadow: 0 2px 8px rgba(0, 0, 0, .12);
}
.day { align-self: center; margin: 12px 0 8px; font-size: .78rem; color: var(--muted); }
.day span { background: var(--surface); padding: 4px 10px; border-radius: 10px; display: inline-block; }
.day.system { text-align: center; }
.msg { display: flex; margin-top: 8px; }
.msg.cont { margin-top: 2px; }
.msg.out { justify-content: flex-end; }
.msg.flash .bubble { outline: 2px solid var(--accent); }
.bubble {
  position: relative; max-width: min(82%, 520px); min-width: 64px; padding: 7px 11px 6px; border-radius: 16px;
  background: var(--bubble-in); overflow-wrap: anywhere; display: flex; flex-direction: column; gap: 5px;
  user-select: text; -webkit-touch-callout: none;
}
.msg.out .bubble { background: var(--bubble-out); }
.bubble.media { padding: 3px 3px 5px; }
.bubble.media .msg-meta { padding-right: 8px; }
.bubble.removed { color: var(--muted); font-style: italic; }
.msg-name { font-size: .83rem; font-weight: 600; color: var(--accent); }
.msg-text { white-space: pre-wrap; }
.msg-text code { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: .92em; background: var(--surface-2); padding: 0 4px; border-radius: 4px; }
.msg-meta { align-self: flex-end; display: inline-flex; align-items: center; gap: 3px; font-size: .72rem; color: var(--muted); white-space: pre; }
.tick .ico { width: 15px; height: 15px; }
.tick.read { color: var(--accent); }
.quote {
  display: flex; flex-direction: column; text-align: left; padding: 3px 8px; border-left: 3px solid var(--accent);
  background: rgba(127, 127, 127, .1); border-radius: 4px 8px 8px 4px; font-size: .88rem; min-width: 0;
}
.quote-name { font-weight: 600; color: var(--accent); font-size: .82rem; }
.quote-text { color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 260px; }
.fwd { border-left: 3px solid var(--muted); padding-left: 8px; display: flex; flex-direction: column; gap: 4px; }
.fwd-from { font-size: .82rem; color: var(--muted); }
.reactions { display: flex; flex-wrap: wrap; gap: 4px; }
.reaction { padding: 2px 8px; border-radius: 12px; background: var(--surface-2); font-size: .85rem; }
.reaction.mine { background: var(--accent); color: var(--on-accent); }

.att-photo { display: block; max-width: 100%; height: auto; border-radius: 13px; background: var(--surface-2); cursor: zoom-in; }
.att-sticker { width: 150px; height: auto; display: block; }
.att-video { position: relative; display: block; max-width: 100%; min-height: 120px; min-width: 160px; border-radius: 13px; overflow: hidden; background: #111; }
.att-video img { width: 100%; height: 100%; object-fit: cover; display: block; }
.att-video-el { width: 100%; max-height: 60vh; border-radius: 13px; background: #000; display: block; }
.play {
  position: absolute; inset: 0; margin: auto; width: 54px; height: 54px; border-radius: 50%; color: #fff;
  background: rgba(0, 0, 0, .55); display: flex; align-items: center; justify-content: center;
}
.att-dur { position: absolute; left: 8px; bottom: 8px; color: #fff; background: rgba(0, 0, 0, .55); padding: 1px 7px; border-radius: 9px; font-size: .75rem; }
.att-file { display: flex; align-items: center; gap: 10px; text-decoration: none; color: inherit; min-width: 200px; }
.att-file-ico { width: 42px; height: 42px; flex: none; border-radius: 50%; background: var(--accent); color: var(--on-accent); display: flex; align-items: center; justify-content: center; }
.att-file-main { min-width: 0; display: flex; flex-direction: column; }
.att-file-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.att-file-size { font-size: .8rem; color: var(--muted); }
.att-audio audio { width: min(260px, 62vw); height: 40px; display: block; }
.att-card { display: flex; align-items: center; gap: 10px; }
.att-card.missed { color: var(--danger); }
.att-share { display: flex; flex-direction: column; gap: 3px; text-decoration: none; color: inherit; border-left: 3px solid var(--accent); padding-left: 8px; }
.att-share img { max-width: 100%; border-radius: 8px; }
.att-share-title { font-weight: 600; }
.att-share-desc { font-size: .88rem; color: var(--muted); }
.att-poll ul { margin: 4px 0 0; padding-left: 18px; }
.att-keyboard { display: flex; flex-wrap: wrap; gap: 5px; }
.kbd-btn { flex: 1 1 auto; text-align: center; padding: 8px 10px; border-radius: 10px; background: var(--accent-soft); color: var(--accent); text-decoration: none; font-weight: 600; font-size: .9rem; }
.kbd-btn.off { color: var(--muted); background: var(--surface-2); font-weight: 400; }

.composer-wrap { flex: none; background: var(--bg); border-top: 1px solid var(--line); padding-bottom: var(--safe-bottom); }
.composer { display: flex; align-items: flex-end; gap: 2px; padding: 6px 6px; max-width: 760px; margin: 0 auto; }
.composer-input {
  flex: 1; min-width: 0; resize: none; border: 0; border-radius: 20px; padding: 10px 14px; font: inherit;
  color: var(--text); background: var(--surface); max-height: 140px; line-height: 1.35; margin: 2px 0;
}
.composer-input:focus { outline: none; }
.icon-btn.send { color: var(--on-accent); background: var(--accent); margin: 2px 0 2px 4px; }
.context-bar { display: flex; align-items: center; gap: 8px; padding: 6px 6px 0 14px; max-width: 760px; margin: 0 auto; }
.context-main { flex: 1; min-width: 0; display: flex; flex-direction: column; border-left: 3px solid var(--accent); padding-left: 8px; font-size: .88rem; }
.context-main b { color: var(--accent); font-size: .82rem; }
.context-main span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: var(--muted); }
.readonly { padding: 14px; text-align: center; color: var(--muted); font-size: .92rem; }
.upload-pending { padding: 8px 14px 4px; font-size: .88rem; color: var(--muted); max-width: 760px; margin: 0 auto; display: flex; flex-direction: column; gap: 5px; }
.progress { height: 4px; background: var(--surface-2); border-radius: 2px; overflow: hidden; }
.progress-fill { height: 100%; width: 3%; background: var(--accent); transition: width .15s; }
.recording { align-items: center; }
.rec-dot { width: 11px; height: 11px; border-radius: 50%; background: var(--danger); animation: pulse 1.2s infinite; margin: 0 8px 0 4px; flex: none; }
@keyframes pulse { 50% { opacity: .3; } }
.rec-time { font-variant-numeric: tabular-nums; font-weight: 600; }
.rec-label { flex: 1; color: var(--muted); padding-left: 10px; font-size: .92rem; }

/* ---------- шторки, просмотр, уведомления ---------- */
.backdrop { position: fixed; inset: 0; z-index: 50; background: var(--backdrop); display: flex; align-items: flex-end; justify-content: center; }
.sheet {
  width: 100%; max-width: 480px; max-height: 86dvh; overflow-y: auto; background: var(--bg);
  border-radius: 18px 18px 0 0; padding: 8px 16px calc(16px + var(--safe-bottom)); display: flex; flex-direction: column; gap: 10px;
}
@media (min-width: 820px) { .backdrop { align-items: center; } .sheet { border-radius: 18px; padding-bottom: 16px; } }
.sheet-grip { align-self: center; width: 38px; height: 4px; border-radius: 2px; background: var(--surface-2); margin-bottom: 4px; }
.sheet-title { font-size: 1.1rem; text-align: center; }
.sheet-text { text-align: center; font-size: .95rem; }
.sheet-actions { display: flex; flex-direction: column; gap: 8px; }
.link-box { display: flex; flex-direction: column; gap: 10px; }
/* Не меньше 16px: иначе iPhone сам приближает страницу при фокусе на поле */
.link-field { font-size: 16px; }
.menu-row { display: flex; align-items: center; gap: 14px; padding: 12px 6px; width: 100%; text-align: left; border-radius: 10px; min-height: 48px; }
.menu-row .ico { color: var(--muted); }
.menu-row.danger, .menu-row.danger .ico { color: var(--danger); }
.menu-row.chosen { justify-content: space-between; font-weight: 600; }
.menu-row.chosen .ico { color: var(--accent); }
@media (hover: hover) { .menu-row:hover { background: var(--surface); } }
.react-row { display: flex; justify-content: space-between; gap: 4px; padding-bottom: 6px; border-bottom: 1px solid var(--line); }
.react-pick { width: 46px; height: 46px; font-size: 1.45rem; border-radius: 50%; }
.react-pick.mine { background: var(--accent-soft); }
.pick-list { display: flex; flex-direction: column; gap: 2px; min-height: 120px; }
.pick-row { display: flex; align-items: center; gap: 12px; padding: 8px 4px; text-align: left; width: 100%; border-radius: 10px; }
@media (hover: hover) { .pick-row:hover { background: var(--surface); } }
.viewer { position: fixed; inset: 0; z-index: 70; background: #000; display: flex; align-items: center; justify-content: center; }
.viewer, .viewer * { touch-action: none; }
.viewer img { max-width: 100%; max-height: 100%; object-fit: contain; transform-origin: center; user-select: none; -webkit-user-drag: none; }
.viewer-close { position: absolute; top: calc(8px + var(--safe-top)); right: 8px; color: #fff; }
.toast {
  position: fixed; z-index: 90; left: 50%; bottom: calc(84px + var(--safe-bottom)); transform: translateX(-50%);
  max-width: min(90vw, 420px); width: max-content; background: #23272f; color: #fff; padding: 10px 16px; border-radius: 12px; font-size: .93rem; text-align: center;
}
.toast.error { background: #b23434; }

/* ---------- настройки ---------- */
.settings { position: fixed; inset: 0; z-index: 40; background: var(--surface); display: flex; flex-direction: column; }
.settings .bar { padding-left: 4px; }
.set-body { flex: 1; overflow-y: auto; padding: 14px 14px calc(24px + var(--safe-bottom)); display: flex; flex-direction: column; gap: 18px; width: 100%; max-width: 560px; margin: 0 auto; }
.set-title { font-size: .82rem; font-weight: 600; color: var(--muted); margin: 0 0 6px 4px; }
.set-card { background: var(--bg); border-radius: 14px; overflow: hidden; }
.set-row {
  display: flex; align-items: center; justify-content: space-between; gap: 14px; width: 100%; min-height: 50px;
  padding: 11px 14px; text-align: left; border-top: 1px solid var(--line);
}
.set-card > .set-row:first-child, .set-card > div:first-child > .set-row:first-child { border-top: 0; }
.set-right { color: var(--muted); font-size: .92rem; text-align: right; flex: none; max-width: 55%; }
.set-row.accent .set-label { color: var(--accent); font-weight: 600; }
.set-row.danger .set-label { color: var(--danger); }
.set-note { padding: 12px 14px; color: var(--muted); font-size: .93rem; }
.set-account { display: flex; align-items: center; gap: 14px; padding: 14px; }
.set-name { font-weight: 600; font-size: 1.05rem; }

@media (min-width: 820px) { .thread-bar { padding-left: 14px; } }

.hint { text-align: center; font-size: .88rem; margin-top: 8px; }
.gate-inner form { margin-top: 10px; }
.set-note .steps { margin-top: 8px; }
