:root, :root[data-theme="dark"] {
  --bg: #0e1621; --panel: #17212b; --panel2: #1f2d3b; --line: #0c141c;
  --text: #e8eef4; --muted: #8596a6; --accent: #3a9ae8; --accent-text: #fff;
  --mine: #2b5278; --theirs: #1f2d3b; --active: #2b5278; --active-muted: #c7d6e4;
  --danger: #ff7b7b; --toast-bg: #3a1d1d; --toast-text: #ffd0d0;
  --scroll: rgba(133, 150, 166, .35); --scroll-hover: rgba(133, 150, 166, .6);
  --shadow: 0 1px 2px rgba(0, 0, 0, .25); color-scheme: dark;
}
:root[data-theme="light"] {
  --bg: #e6ebf0; --panel: #ffffff; --panel2: #f0f3f6; --line: #dde3ea;
  --text: #17212b; --muted: #6a7b8b; --accent: #2481cc; --accent-text: #fff;
  --mine: #d4ebff; --theirs: #ffffff; --active: #dcecfb; --active-muted: #5a6f82;
  --danger: #d93c3c; --toast-bg: #fde7e7; --toast-text: #8a1f1f;
  --scroll: rgba(90, 110, 130, .35); --scroll-hover: rgba(90, 110, 130, .6);
  --shadow: 0 1px 2px rgba(20, 40, 60, .12); color-scheme: light;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; overflow: hidden; }
body { background: var(--bg); color: var(--text); font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; -webkit-text-size-adjust: 100%; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.visually-hidden { position: absolute; left: -9999px; width: 1px; height: 1px; opacity: 0; }

/* аккуратная прокрутка */
* { scrollbar-width: thin; scrollbar-color: var(--scroll) transparent; }
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--scroll); border-radius: 10px; border: 3px solid transparent; background-clip: content-box; }
::-webkit-scrollbar-thumb:hover { background: var(--scroll-hover); background-clip: content-box; }
#messages, #dialogs, #emoji-grid { overscroll-behavior: contain; }

input, textarea, button { font: inherit; color: inherit; }
input, textarea { background: var(--panel2); border: 1px solid transparent; border-radius: 12px; padding: 11px 13px; width: 100%; outline: none; transition: border-color .15s; }
input:focus, textarea:focus { border-color: var(--accent); }
button { cursor: pointer; border: 0; border-radius: 12px; padding: 10px 16px; background: var(--panel2); transition: background .15s, opacity .15s; }
button.primary { background: var(--accent); color: var(--accent-text); font-weight: 600; }
button.primary:hover { filter: brightness(1.08); }
button.ghost { background: transparent; color: var(--accent); }
button:disabled { opacity: .6; cursor: default; }
button:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* иконки */
.ic { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; display: block; }
button.icon { background: transparent; color: var(--muted); width: 40px; height: 40px; padding: 0; display: inline-flex; align-items: center; justify-content: center; border-radius: 50%; flex-shrink: 0; }
button.icon:hover { background: var(--panel2); color: var(--text); }
#btn-push.on { color: var(--accent); }
#btn-push .slash { display: none; }
#btn-push:not(.on) .slash { display: block; }
.js-theme .ic-moon { display: none; }
:root[data-theme="light"] .js-theme .ic-sun { display: none; }
:root[data-theme="light"] .js-theme .ic-moon { display: block; }
.floating { position: fixed; top: calc(10px + env(safe-area-inset-top)); right: 10px; z-index: 5; background: var(--panel); box-shadow: var(--shadow); }
body.in-app .floating { display: none; }

/* экраны входа */
.center { min-height: 100%; height: 100%; overflow-y: auto; display: flex; align-items: center; justify-content: center; padding: 16px; }
.card { background: var(--panel); border-radius: 18px; padding: 28px; width: 100%; max-width: 380px; display: flex; flex-direction: column; gap: 14px; box-shadow: var(--shadow); }
.card h1 { margin: 0 0 4px; font-size: 22px; }
.card label { display: flex; flex-direction: column; gap: 6px; color: var(--muted); font-size: 13px; }
.card p { margin: 0; }
.row { display: flex; gap: 8px; }
.error { color: var(--danger); min-height: 1.2em; font-size: 13px; }

/* приложение */
#view-app { height: var(--app-h, 100dvh); display: flex; position: relative; }
#sidebar { width: 340px; flex-shrink: 0; background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.side-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 10px 8px 10px 14px; border-bottom: 1px solid var(--line); }
.me { display: flex; align-items: center; gap: 10px; min-width: 0; }
.me-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.actions { display: flex; flex-shrink: 0; }
#dialogs { overflow-y: auto; flex: 1; padding: 6px; }
.avatar { width: 46px; height: 46px; border-radius: 50%; flex-shrink: 0; display: flex; align-items: center; justify-content: center; font-weight: 700; color: #fff; position: relative; }
.side-head .avatar { width: 36px; height: 36px; font-size: 14px; }
.avatar.online::after { content: ""; position: absolute; right: -1px; bottom: -1px; width: 12px; height: 12px; background: #4cd964; border: 2px solid var(--panel); border-radius: 50%; }
.dialog { display: flex; gap: 12px; padding: 9px 10px; cursor: pointer; border-radius: 12px; }
.dialog:hover { background: var(--panel2); }
.dialog.active { background: var(--active); }
.dlg-body { flex: 1; min-width: 0; display: flex; flex-direction: column; justify-content: center; gap: 2px; }
.dlg-top, .dlg-bottom { display: flex; justify-content: space-between; gap: 8px; align-items: center; }
.dlg-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dlg-preview { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; }
.dialog.active .muted { color: var(--active-muted); }
.badge { background: var(--accent); color: var(--accent-text); border-radius: 12px; padding: 0 7px; font-size: 12px; font-weight: 600; line-height: 20px; min-width: 20px; text-align: center; flex-shrink: 0; }

#chat { flex: 1; display: flex; flex-direction: column; min-width: 0; min-height: 0; position: relative; }
.chat-head { display: flex; align-items: center; gap: 12px; padding: 8px 14px; background: var(--panel); border-bottom: 1px solid var(--line); min-height: 56px; }
.chat-head .avatar { width: 38px; height: 38px; font-size: 15px; }
.chat-meta { min-width: 0; }
.chat-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.back { display: none !important; margin-left: -6px; }
#messages { flex: 1; overflow-y: auto; padding: 14px 16px; display: flex; flex-direction: column; gap: 3px; }
.empty { flex: 1; display: flex; align-items: center; justify-content: center; }
.center-text { text-align: center; margin: auto; }
.day { align-self: center; background: var(--panel); color: var(--muted); padding: 3px 12px; border-radius: 12px; font-size: 12px; margin: 12px 0 6px; box-shadow: var(--shadow); }
.more { align-self: center; }
.msg { display: flex; }
.msg.mine { justify-content: flex-end; }
.bubble { max-width: min(560px, 80%); padding: 7px 11px 5px; border-radius: 16px; background: var(--theirs); overflow-wrap: anywhere; box-shadow: var(--shadow); }
.mine .bubble { background: var(--mine); border-bottom-right-radius: 5px; }
.theirs .bubble { border-bottom-left-radius: 5px; }
.author { color: var(--accent); font-size: 13px; font-weight: 600; }
.text { white-space: pre-wrap; }
.time { color: var(--muted); font-size: 11px; text-align: right; margin-top: 1px; }

.composer { display: flex; align-items: flex-end; gap: 6px; padding: 8px 10px calc(8px + env(safe-area-inset-bottom)); background: var(--panel); border-top: 1px solid var(--line); }
.composer textarea { resize: none; max-height: 160px; border-radius: 20px; padding: 11px 15px; line-height: 1.35; }
.send { width: 44px; height: 44px; padding: 0; border-radius: 50%; flex-shrink: 0; display: flex; align-items: center; justify-content: center; }
.send .ic { width: 20px; height: 20px; margin-left: -2px; }
.composer .icon { width: 44px; height: 44px; }

.toast.info { background: var(--panel); color: var(--text); box-shadow: 0 4px 16px rgba(0, 0, 0, .35); }
.down { position: absolute; right: 16px; bottom: 84px; width: 42px; height: 42px; padding: 0; border-radius: 50%; background: var(--panel); color: var(--text); box-shadow: 0 2px 8px rgba(0, 0, 0, .3); display: flex; align-items: center; justify-content: center; }
.down .badge { position: absolute; top: -8px; right: -4px; }
.toast { position: fixed; left: 50%; bottom: calc(84px + env(safe-area-inset-bottom)); transform: translateX(-50%); background: var(--toast-bg); color: var(--toast-text); padding: 8px 14px; border-radius: 12px; font-size: 14px; max-width: 90%; text-align: center; z-index: 20; }

/* смайлики */
.emoji-panel { position: absolute; left: 10px; right: 10px; bottom: calc(66px + env(safe-area-inset-bottom)); max-width: 380px; background: var(--panel); border: 1px solid var(--line); border-radius: 16px; box-shadow: 0 6px 24px rgba(0, 0, 0, .35); z-index: 6; display: flex; flex-direction: column; overflow: hidden; }
.emoji-tabs { display: flex; border-bottom: 1px solid var(--line); }
.emoji-tabs button { flex: 1; border-radius: 0; background: transparent; padding: 8px 0; font-size: 20px; line-height: 1; opacity: .55; }
.emoji-tabs button.active { opacity: 1; box-shadow: inset 0 -2px 0 var(--accent); }
.emoji-grid { display: grid; grid-template-columns: repeat(8, 1fr); gap: 2px; padding: 8px; height: 230px; overflow-y: auto; align-content: start; }
.emoji-grid button { background: transparent; padding: 0; height: 38px; font-size: 24px; line-height: 1; border-radius: 8px; }
.emoji-grid button:hover { background: var(--panel2); }

/* телефон */
/* служебные сообщения и группы */
.sys-msg { align-self: center; max-width: 90%; text-align: center; color: var(--muted); background: var(--panel); border-radius: 12px; padding: 3px 12px; font-size: 12.5px; margin: 6px 0; box-shadow: var(--shadow); }
.chat-meta.clickable { cursor: pointer; border-radius: 8px; padding: 2px 8px; margin-left: -8px; }
.chat-meta.clickable:hover { background: var(--panel2); }
.modal { position: fixed; inset: 0; background: rgba(0, 0, 0, .55); display: flex; align-items: center; justify-content: center; padding: 16px; z-index: 30; }
.modal-card { background: var(--panel); border-radius: 18px; padding: 20px; width: 100%; max-width: 420px; max-height: min(88dvh, 640px); overflow-y: auto; display: flex; flex-direction: column; gap: 12px; box-shadow: 0 12px 40px rgba(0, 0, 0, .45); }
.modal-card h2 { margin: 0; font-size: 19px; }
.modal-card h3 { margin: 6px 0 0; font-size: 13px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.modal-card .row { justify-content: flex-end; flex-wrap: wrap; }
button.danger { background: transparent; color: var(--danger); }
button.danger:hover { background: var(--panel2); }
.picker, .members { display: flex; flex-direction: column; gap: 2px; }
.pick, .member { display: flex; align-items: center; gap: 10px; padding: 6px 8px; border-radius: 10px; }
.pick { cursor: pointer; }
.pick:hover { background: var(--panel2); }
.pick input { width: 20px; height: 20px; flex-shrink: 0; accent-color: var(--accent); }
.pick .avatar, .member .avatar { width: 36px; height: 36px; font-size: 14px; }
.member-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tag { font-size: 11px; color: var(--accent); border: 1px solid var(--accent); border-radius: 8px; padding: 0 6px; }
button.kick { width: 32px; height: 32px; padding: 0; border-radius: 50%; background: transparent; color: var(--muted); font-size: 18px; line-height: 1; }
button.kick:hover { background: var(--panel2); color: var(--danger); }
.inline { display: flex; gap: 8px; }

@media (max-width: 720px) {
  body { font-size: 16px; }
  input, textarea { font-size: 16px; } /* iOS не приближает страницу при фокусе */
  #sidebar { width: 100%; border-right: 0; }
  #chat, #empty { display: none; }
  body.chat-open #sidebar { display: none; }
  body.chat-open #chat { display: flex; }
  #empty { display: none !important; }
  .back { display: inline-flex !important; }
  .bubble { max-width: 88%; }
  #messages { padding: 10px 10px; }
  .dialog { padding: 10px; }
  .card { padding: 22px; }
  .emoji-panel { left: 6px; right: 6px; max-width: none; }
  .emoji-grid { grid-template-columns: repeat(7, 1fr); height: 210px; }
  .down { bottom: 76px; right: 12px; }
}
@media (max-height: 480px) and (orientation: landscape) {
  .chat-head { min-height: 46px; padding-top: 4px; padding-bottom: 4px; }
  .emoji-grid { height: 120px; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
