:root {
  --bg: #f6f4f1;
  --surface: #ffffff;
  --surface-2: #ece8e3;
  --text: #1c1a18;
  --muted: #736d66;
  --line: rgba(40, 30, 20, 0.10);
  --accent: #e4522c;
  --accent-soft: rgba(228, 82, 44, 0.12);
  --on-accent: #ffffff;
  --bubble: #ffffff;
  --bubble-me: #e4522c;
  --on-bubble-me: #ffffff;
  --danger: #cf3426;
  --ok: #2b9356;
  --shadow: 0 1px 1px rgba(40, 30, 20, 0.08);
  --backdrop: rgba(20, 15, 10, 0.42);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #111111;
    --surface: #1c1c1d;
    --surface-2: #2a2a2b;
    --text: #f3f0eb;
    --muted: #9c968e;
    --line: rgba(255, 255, 255, 0.09);
    --accent: #ff6a42;
    --accent-soft: rgba(255, 106, 66, 0.16);
    --bubble: #232324;
    --bubble-me: #e4522c;
    --shadow: none;
    --backdrop: rgba(0, 0, 0, 0.6);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #111111; --surface: #1c1c1d; --surface-2: #2a2a2b; --text: #f3f0eb; --muted: #9c968e;
  --line: rgba(255, 255, 255, 0.09); --accent: #ff6a42; --accent-soft: rgba(255, 106, 66, 0.16);
  --bubble: #232324; --bubble-me: #e4522c; --shadow: none; --backdrop: rgba(0, 0, 0, 0.6); color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--bg); color: var(--text); }
body {
  font: 16px/1.35 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
input, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }
svg { display: block; }

#app {
  position: fixed; left: 0; right: 0; top: 0;
  height: var(--app-h, 100dvh);
  display: flex; flex-direction: column;
  max-width: 720px; margin: 0 auto;
  background: var(--bg);
}
@media (min-width: 721px) { #app { border-left: 1px solid var(--line); border-right: 1px solid var(--line); } }
.splash { flex: 1; display: grid; place-items: center; }
.splash img { border-radius: 18px; opacity: .9; }

.screen { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.scroll { flex: 1; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; }

/* ---------- верхняя панель ---------- */
.top {
  flex: none;
  padding: calc(env(safe-area-inset-top) + 6px) 8px 6px;
  display: flex; align-items: center; gap: 4px; min-height: 52px;
  background: var(--bg);
  border-bottom: 1px solid transparent;
}
.top.line { border-bottom-color: var(--line); }
.top .title { flex: 1; min-width: 0; font-size: 17px; font-weight: 600; text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.top .spacer { width: 44px; flex: none; }
.big-title { font-size: 30px; font-weight: 700; letter-spacing: -0.02em; padding: 0 8px; flex: 1; }
.icon-btn {
  width: 44px; height: 44px; flex: none; display: grid; place-items: center; border-radius: 50%; color: var(--accent);
}
.icon-btn:active { background: var(--accent-soft); }
.icon-btn svg { width: 24px; height: 24px; }
.back { display: flex; align-items: center; color: var(--accent); height: 44px; padding: 0 8px 0 2px; font-size: 17px; flex: none; }
.back svg { width: 26px; height: 26px; }

/* ---------- нижние вкладки ---------- */
.tabs {
  flex: none; display: flex;
  border-top: 1px solid var(--line);
  background: var(--bg);
  padding-bottom: env(safe-area-inset-bottom);
}
.tab {
  flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 7px 0 5px; font-size: 11px; color: var(--muted); position: relative;
}
.tab svg { width: 26px; height: 26px; }
.tab.on { color: var(--accent); }
.tab .count { position: absolute; top: 2px; left: calc(50% + 6px); }
.count {
  min-width: 20px; height: 20px; padding: 0 6px; border-radius: 10px;
  background: var(--accent); color: var(--on-accent);
  font-size: 12px; font-weight: 600; line-height: 20px; text-align: center;
}
.count.muted { background: var(--muted); }

/* ---------- списки ---------- */
.row {
  display: flex; align-items: center; gap: 12px; width: 100%;
  padding: 0 0 0 16px; text-align: left; min-height: 64px;
}
.row:active { background: var(--surface-2); }
.row .main {
  flex: 1; min-width: 0; align-self: stretch;
  display: flex; flex-direction: column; justify-content: center; gap: 2px;
  padding: 10px 16px 10px 0; border-bottom: 1px solid var(--line);
}
.row:last-child .main { border-bottom: 0; }
.row .line1, .row .line2 { display: flex; align-items: center; gap: 8px; min-width: 0; }
.row .name { flex: 1; min-width: 0; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row .time { font-size: 13px; color: var(--muted); flex: none; }
.row .sub { flex: 1; min-width: 0; color: var(--muted); font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row .sub b { color: var(--text); font-weight: 500; }
.row .end { flex: none; display: flex; align-items: center; gap: 6px; color: var(--muted); }
.row .end svg { width: 16px; height: 16px; }
.row.compact { min-height: 56px; }
.row .check {
  width: 24px; height: 24px; border-radius: 50%; border: 2px solid var(--line); flex: none; display: grid; place-items: center;
}
.row .check.on { background: var(--accent); border-color: var(--accent); color: #fff; }
.row .check svg { width: 14px; height: 14px; }
.online-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--ok); flex: none; }

.section-title { font-size: 13px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; padding: 18px 16px 6px; }
.card { background: var(--surface); border-radius: 14px; margin: 0 12px; overflow: hidden; box-shadow: var(--shadow); }
.card .row { min-height: 50px; }
.card .row .main { padding-top: 12px; padding-bottom: 12px; }
.empty { text-align: center; color: var(--muted); padding: 48px 32px; }
.empty .em { font-size: 44px; margin-bottom: 8px; }

/* ---------- аватары ---------- */
.ava {
  --s: 48px;
  width: var(--s); height: var(--s); border-radius: 50%; flex: none;
  object-fit: cover; display: grid; place-items: center;
  color: #fff; font-weight: 600; font-size: calc(var(--s) * 0.38); user-select: none;
  background: var(--surface-2);
}
.ava-wrap { position: relative; flex: none; }
.ava-wrap .online-dot { position: absolute; right: 1px; bottom: 1px; width: 13px; height: 13px; border: 2px solid var(--bg); }

/* ---------- поиск, поля ---------- */
.search { padding: 4px 16px 8px; flex: none; }
.search input, .field input, .field textarea {
  width: 100%; border: 1px solid var(--line); background: var(--surface); border-radius: 12px;
  padding: 11px 14px; font-size: 16px; outline: none;
}
.search input { background: var(--surface-2); border-color: transparent; padding: 9px 14px; }
.field input:focus, .field textarea:focus { border-color: var(--accent); }
.field { display: block; margin: 0 0 14px; }
.field span { display: block; font-size: 13px; color: var(--muted); margin: 0 0 6px 4px; }
.field textarea { resize: none; min-height: 84px; }
.form { padding: 8px 16px 24px; }
.hint { font-size: 13px; color: var(--muted); margin: -6px 4px 14px; }

.btn {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  width: 100%; min-height: 50px; border-radius: 14px; font-weight: 600; font-size: 17px;
  background: var(--accent); color: var(--on-accent);
}
.btn:active { filter: brightness(.92); }
.btn:disabled { opacity: .5; }
.btn.secondary { background: var(--surface-2); color: var(--text); }
.btn.small { width: auto; min-height: 36px; padding: 0 14px; font-size: 15px; border-radius: 18px; }
.link-btn { color: var(--accent); font-size: 17px; padding: 0 10px; height: 44px; font-weight: 500; flex: none; }
.link-btn:disabled { opacity: .4; }
.danger { color: var(--danger) !important; }
.cell-btn { width: 100%; text-align: left; padding: 14px 16px; font-size: 17px; border-bottom: 1px solid var(--line); display: flex; align-items: center; gap: 12px; }
.cell-btn:last-child { border-bottom: 0; }
.cell-btn:active { background: var(--surface-2); }
.cell-btn .grow { flex: 1; }
.cell-btn svg { width: 22px; height: 22px; flex: none; }
.cell-btn .share-ico { width: 17px; height: 17px; display: inline-block; }
.cell-btn .val { color: var(--muted); }

.switch { width: 51px; height: 31px; border-radius: 16px; background: var(--surface-2); position: relative; flex: none; transition: background .2s; }
.switch::after { content: ""; position: absolute; top: 2px; left: 2px; width: 27px; height: 27px; border-radius: 50%; background: #fff; box-shadow: 0 2px 4px rgba(0,0,0,.2); transition: transform .2s; }
.switch.on { background: var(--ok); }
.switch.on::after { transform: translateX(20px); }

/* ---------- вход ---------- */
.auth { flex: 1; overflow-y: auto; display: flex; flex-direction: column; justify-content: center; padding: calc(env(safe-area-inset-top) + 24px) 24px calc(env(safe-area-inset-bottom) + 24px); }
.auth .logo { width: 84px; height: 84px; border-radius: 22px; margin: 0 auto 16px; display: block; }
.auth h1 { text-align: center; font-size: 30px; margin: 0 0 4px; letter-spacing: -0.02em; }
.auth p.lead { text-align: center; color: var(--muted); margin: 0 0 28px; }
.seg { display: flex; background: var(--surface-2); border-radius: 12px; padding: 3px; margin-bottom: 20px; }
.seg button { flex: 1; height: 36px; border-radius: 9px; font-size: 15px; font-weight: 500; color: var(--muted); }
.seg button.on { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.12); }
.auth .form { padding: 0; }

/* ---------- баннеры ---------- */
.banner {
  margin: 4px 12px 10px; padding: 12px 14px; border-radius: 14px;
  background: var(--accent-soft); display: flex; gap: 12px; align-items: center;
}
.banner .txt { flex: 1; font-size: 14px; }
.banner .txt b { display: block; font-size: 15px; margin-bottom: 2px; }
.banner .x { color: var(--muted); width: 28px; height: 28px; display: grid; place-items: center; flex: none; }
.banner .x svg { width: 18px; height: 18px; }
.share-ico { display: inline-block; vertical-align: -3px; width: 17px; height: 17px; color: var(--accent); }

/* ---------- чат ---------- */
.chat-head { display: flex; align-items: center; gap: 10px; flex: 1; min-width: 0; text-align: left; }
.chat-head .ava { --s: 38px; }
.chat-head .t { min-width: 0; flex: 1; }
.chat-head .t .n { font-weight: 600; font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-head .t .s { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-head .t .s.on { color: var(--ok); }

.msgs { flex: 1; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; padding: 8px 10px 10px; display: flex; flex-direction: column; }
.msgs .load-more { align-self: center; color: var(--muted); font-size: 13px; padding: 8px; }
.day { align-self: center; font-size: 13px; color: var(--muted); background: var(--surface-2); padding: 3px 10px; border-radius: 10px; margin: 10px 0 6px; }
.sys { align-self: center; text-align: center; font-size: 13px; color: var(--muted); padding: 4px 12px; margin: 4px 24px; }
.msg { display: flex; align-items: flex-end; gap: 6px; max-width: 82%; align-self: flex-start; margin-top: 2px; }
.msg.first { margin-top: 8px; }
.msg.me { align-self: flex-end; flex-direction: row-reverse; }
.msg .ava { --s: 30px; }
.msg .ava-slot { width: 30px; flex: none; }
.bubble {
  position: relative; padding: 7px 11px 7px; border-radius: 18px;
  background: var(--bubble); color: var(--text); box-shadow: var(--shadow);
  white-space: pre-wrap; overflow-wrap: anywhere; word-break: break-word; min-width: 0;
}
.msg:not(.me) .bubble.tail { border-bottom-left-radius: 6px; }
.msg.me .bubble.tail { border-bottom-right-radius: 6px; }
.msg.me .bubble { background: var(--bubble-me); color: var(--on-bubble-me); }
.msg.me .bubble a { color: inherit; text-decoration: underline; }
.bubble .who { font-size: 13px; font-weight: 600; margin-bottom: 1px; }
.bubble .meta { float: right; margin: 7px -3px -5px 10px; font-size: 11px; line-height: 1; opacity: .62; white-space: nowrap; display: inline-flex; gap: 3px; align-items: center; }
.bubble .meta svg { width: 15px; height: 11px; }
.bubble.deleted { font-style: italic; opacity: .6; }
.bubble.pending { opacity: .7; }
.bubble.failed { outline: 2px solid var(--danger); }
.bubble.photo { padding: 3px; overflow: hidden; }
.bubble.photo img { display: block; border-radius: 15px; max-width: min(260px, 64vw); max-height: 340px; width: auto; height: auto; object-fit: cover; background: var(--surface-2); }
.bubble.photo .cap { padding: 5px 8px 4px; }
.bubble.photo .meta.on-img { position: absolute; right: 10px; bottom: 9px; margin: 0; padding: 3px 6px; border-radius: 8px; background: rgba(0,0,0,.45); color: #fff; opacity: 1; }
.bubble.flash { animation: flash .9s; }
@keyframes flash { 30% { filter: brightness(.8); } }

.to-bottom {
  position: absolute; right: 14px; bottom: calc(100% + 10px);
  width: 42px; height: 42px; border-radius: 50%; background: var(--surface); box-shadow: 0 2px 8px rgba(0,0,0,.18);
  display: grid; place-items: center; color: var(--muted);
}
.to-bottom svg { width: 22px; height: 22px; }
.to-bottom .count { position: absolute; top: -6px; right: -4px; }

.composer {
  flex: none; position: relative;
  display: flex; align-items: flex-end; gap: 6px;
  padding: 6px 8px calc(env(safe-area-inset-bottom) + 6px);
  border-top: 1px solid var(--line); background: var(--bg);
}
body.kb .composer { padding-bottom: 6px; }
body.kb .tabs { padding-bottom: 0; }
.composer textarea {
  flex: 1; min-width: 0; resize: none; border: 1px solid var(--line); background: var(--surface);
  border-radius: 20px; padding: 9px 14px; font-size: 16px; line-height: 20px; max-height: 140px; outline: none;
  height: 40px;
}
.composer .icon-btn { width: 40px; height: 40px; color: var(--muted); }
.composer .send { background: var(--accent); color: #fff; }
.composer .send:disabled { opacity: .35; }
.composer .send svg { width: 20px; height: 20px; }

/* ---------- профиль / инфо ---------- */
.hero { display: flex; flex-direction: column; align-items: center; text-align: center; padding: 8px 24px 16px; gap: 6px; }
.hero .ava { --s: 96px; }
.hero h2 { margin: 6px 0 0; font-size: 24px; letter-spacing: -0.01em; }
.hero .s { color: var(--muted); font-size: 15px; }
.hero .about { white-space: pre-wrap; margin-top: 6px; max-width: 420px; }
.hero .change-photo { color: var(--accent); font-size: 15px; padding: 6px; }
.actions { display: flex; gap: 10px; justify-content: center; padding: 4px 16px 8px; }
.actions .act { min-width: 92px; padding: 10px 12px; background: var(--surface); border-radius: 14px; box-shadow: var(--shadow); color: var(--accent); font-size: 13px; display: flex; flex-direction: column; align-items: center; gap: 4px; }
.actions .act svg { width: 24px; height: 24px; }
.role-tag { font-size: 12px; color: var(--accent); flex: none; }

/* ---------- админка ---------- */
.stats { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; padding: 4px 12px; }
.stat { background: var(--surface); border-radius: 14px; padding: 14px; box-shadow: var(--shadow); }
.stat .v { font-size: 28px; font-weight: 700; letter-spacing: -0.02em; font-variant-numeric: tabular-nums; }
.stat .l { font-size: 13px; color: var(--muted); }
.chips { display: flex; gap: 8px; padding: 2px 16px 10px; overflow-x: auto; flex: none; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip { flex: none; height: 34px; padding: 0 14px; border-radius: 17px; background: var(--surface-2); font-size: 15px; font-weight: 500; }
.chip.on { background: var(--accent); color: var(--on-accent); }
.tag { font-size: 11px; font-weight: 600; padding: 2px 7px; border-radius: 6px; background: var(--surface-2); color: var(--muted); flex: none; }
.tag.red { background: rgba(207, 52, 38, .14); color: var(--danger); }
.tag.acc { background: var(--accent-soft); color: var(--accent); }
.report { background: var(--surface); border-radius: 14px; margin: 0 12px 10px; padding: 12px 14px; box-shadow: var(--shadow); }
.report .q { background: var(--surface-2); border-radius: 10px; padding: 8px 10px; margin: 8px 0; white-space: pre-wrap; overflow-wrap: anywhere; }
.report .q img { max-width: 100%; max-height: 220px; border-radius: 8px; display: block; }
.report .m { font-size: 13px; color: var(--muted); }
.report .btns { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 8px; }

/* ---------- слои: шторка, тост, просмотр фото ---------- */
#layer { position: fixed; inset: 0; pointer-events: none; z-index: 10; }
#layer > * { pointer-events: auto; }
.backdrop { position: absolute; inset: 0; background: var(--backdrop); animation: fade .18s; }
.sheet {
  position: absolute; left: 0; right: 0; bottom: 0; max-width: 720px; margin: 0 auto;
  padding: 8px 10px calc(env(safe-area-inset-bottom) + 10px);
  animation: up .22s cubic-bezier(.2,.9,.3,1);
}
.sheet .group { background: var(--surface); border-radius: 14px; overflow: hidden; margin-bottom: 8px; }
.sheet .group .head { padding: 12px 16px; text-align: center; font-size: 13px; color: var(--muted); border-bottom: 1px solid var(--line); white-space: pre-wrap; max-height: 30vh; overflow: hidden; }
.sheet .group button { display: block; width: 100%; padding: 16px; font-size: 18px; color: var(--accent); text-align: center; border-bottom: 1px solid var(--line); }
.sheet .group button:last-child { border-bottom: 0; }
.sheet .group button:active { background: var(--surface-2); }
.sheet .cancel { font-weight: 600; }
.modal { position: absolute; left: 50%; top: 40%; transform: translate(-50%, -50%); width: min(340px, calc(100% - 40px)); background: var(--surface); border-radius: 16px; padding: 18px 16px 14px; animation: fade .18s; }
.modal h3 { margin: 0 0 12px; font-size: 17px; text-align: center; }
.modal .row2 { display: flex; gap: 8px; }
.toast {
  position: absolute; left: 50%; top: calc(env(safe-area-inset-top) + 12px); transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 15px;
  max-width: calc(100% - 32px); box-shadow: 0 6px 20px rgba(0,0,0,.2); animation: drop .25s; pointer-events: none !important;
}
.viewer { position: absolute; inset: 0; background: #000; display: grid; place-items: center; animation: fade .18s; }
.viewer img { max-width: 100%; max-height: 100%; object-fit: contain; }
.viewer .icon-btn { position: absolute; top: calc(env(safe-area-inset-top) + 8px); right: 8px; color: #fff; }
@keyframes fade { from { opacity: 0; } }
@keyframes up { from { transform: translateY(100%); } }
@keyframes drop { from { opacity: 0; transform: translate(-50%, -12px); } }
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }

.spin { width: 22px; height: 22px; border: 2.5px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: rot .8s linear infinite; margin: 24px auto; }
@keyframes rot { to { transform: rotate(360deg); } }
