:root {
  --bg: #f4f1ea;
  --surface: #fffdf8;
  --ink: #1d2320;
  --muted: #6d736e;
  --line: #e3ddd1;
  --accent: #2f6f5e;
  --accent-ink: #ffffff;
  --accent-soft: #e2efe8;
  --mine: #d6ebdf;
  --theirs: #fffdf8;
  --badge: #c0573e;
  --danger: #b4432e;
  --shadow: 0 1px 1px rgba(40, 35, 25, .08);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141716;
    --surface: #1d2120;
    --ink: #ecefe9;
    --muted: #98a09a;
    --line: #2b3130;
    --accent: #62b398;
    --accent-ink: #0e1a15;
    --accent-soft: #1f3a30;
    --mine: #23443a;
    --theirs: #222826;
    --badge: #e2795e;
    --danger: #e4775f;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%;
  background: var(--bg); color: var(--ink);
  font: 17px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}
body { position: fixed; inset: 0; overflow: hidden; overscroll-behavior: none; }
#app {
  position: fixed; top: 0; left: 0; right: 0;
  height: var(--app-h, 100dvh);
  max-width: 760px; margin: 0 auto;
  background: var(--bg);
}
@media (min-width: 761px) { #app { border-left: 1px solid var(--line); border-right: 1px solid var(--line); } }

button { font: inherit; color: inherit; }
input, textarea { font: inherit; font-size: 17px; color: var(--ink); }
a { color: var(--accent); }
.muted { color: var(--muted); }
.small { font-size: 14px; }
.center { text-align: center; padding: 24px 16px; }

/* ---------- Каркас экрана ---------- */
.screen { display: flex; flex-direction: column; height: 100%; }
.bar {
  display: flex; align-items: center; gap: 10px;
  padding: calc(env(safe-area-inset-top) + 8px) 12px 10px;
  min-height: 60px;
  border-bottom: 1px solid var(--line);
  background: var(--bg);
  flex: none;
}
.bar h1 { flex: 1; margin: 0 0 0 6px; font-size: 26px; letter-spacing: -.02em; }
.bar-title { flex: 1; min-width: 0; }
.bar-name { font-weight: 650; font-size: 19px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bar-sub { color: var(--muted); font-size: 14px; }
.conn { font-size: 13px; color: var(--muted); background: var(--surface); border: 1px solid var(--line); padding: 2px 8px; border-radius: 99px; }
.scroll { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; }

.icon-btn {
  border: 0; background: none; padding: 8px; margin: -4px;
  border-radius: 12px; display: grid; place-items: center; color: var(--accent);
  min-width: 44px; min-height: 44px; cursor: pointer;
}
.icon-btn:active { background: var(--accent-soft); }
.back { margin-left: -10px; }

/* ---------- Аватары ---------- */
.av {
  width: 52px; height: 52px; border-radius: 50%; flex: none;
  display: grid; place-items: center;
  color: #fff; font-weight: 650; font-size: 22px;
}
.bar .av { width: 40px; height: 40px; font-size: 18px; }
.av.group { background: var(--accent); }
.av.big { width: 88px; height: 88px; font-size: 36px; }
.av.big svg { width: 44px; height: 44px; }
.bar .av.group svg { width: 22px; height: 22px; }

/* ---------- Список чатов ---------- */
.list { padding: 4px 0; }
.row {
  display: flex; align-items: center; gap: 14px; width: 100%;
  padding: 12px 16px; border: 0; background: none; text-align: left; cursor: pointer;
}
.row:active { background: var(--accent-soft); }
.row-main { flex: 1; min-width: 0; border-bottom: 1px solid var(--line); padding-bottom: 12px; margin-bottom: -12px; }
.row:last-child .row-main { border-bottom: 0; }
.row-top, .row-bottom { display: flex; align-items: center; gap: 8px; }
.row-name { flex: 1; font-weight: 650; font-size: 19px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row-time { color: var(--muted); font-size: 14px; }
.row-preview { flex: 1; color: var(--muted); font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 2px; }
.has-unread .row-preview { color: var(--ink); }
.has-unread .row-time { color: var(--accent); font-weight: 600; }
.badge {
  background: var(--accent); color: var(--accent-ink); font-size: 14px; font-weight: 700;
  min-width: 24px; height: 24px; padding: 0 7px; border-radius: 12px; display: grid; place-items: center;
}
.banner {
  margin: 12px 16px 4px; padding: 14px 16px; border-radius: 16px;
  background: var(--accent-soft); display: grid; gap: 12px;
}
.banner.warn { background: var(--surface); border: 1px solid var(--line); color: var(--muted); font-size: 15px; }
.hint { margin: 16px; padding: 16px; border-radius: 16px; border: 1px dashed var(--line); color: var(--muted); }

/* ---------- Кнопки и поля ---------- */
.btn {
  border: 0; border-radius: 14px; padding: 12px 18px;
  background: var(--accent); color: var(--accent-ink); font-weight: 650; cursor: pointer;
  min-height: 48px;
}
.btn:active { filter: brightness(.92); }
.btn:disabled { opacity: .5; }
.btn.secondary { background: var(--accent-soft); color: var(--accent); }
.btn.big { width: 100%; font-size: 19px; min-height: 56px; }
.link { border: 0; background: none; color: var(--accent); padding: 8px 4px; cursor: pointer; font-size: 15px; }
.link.danger { color: var(--danger); }
input:not([type]), input[type=text] {
  border: 1px solid var(--line); background: var(--surface); border-radius: 12px; padding: 12px 14px; outline: none; min-width: 0;
}
input:focus, textarea:focus { border-color: var(--accent); }
.big-input { width: 100%; font-size: 19px !important; padding: 14px 16px !important; }
.inline { display: flex; gap: 8px; flex-wrap: wrap; }
.inline input { flex: 1; }

/* ---------- Сообщения ---------- */
.msgs { padding: 8px 10px 12px; }
.day { text-align: center; margin: 14px 0 8px; }
.day span {
  display: inline-block; font-size: 14px; color: var(--muted);
  background: var(--bg); border: 1px solid var(--line); padding: 3px 12px; border-radius: 99px;
}
.msg { display: flex; margin-top: 3px; }
.msg.first { margin-top: 10px; }
.msg.mine { justify-content: flex-end; }
.bubble {
  max-width: min(82%, 520px);
  background: var(--theirs); border-radius: 18px; padding: 8px 12px 6px;
  box-shadow: var(--shadow); border: 1px solid var(--line);
  overflow-wrap: anywhere;
}
.msg.first:not(.mine) .bubble { border-top-left-radius: 6px; }
.msg.mine .bubble { background: var(--mine); border-color: transparent; }
.msg.mine.first .bubble { border-top-right-radius: 6px; }
.from { font-weight: 650; font-size: 15px; margin-bottom: 2px; }
.text { white-space: pre-wrap; font-size: 17px; }
.meta { font-size: 12px; color: var(--muted); text-align: right; margin-top: 2px; display: flex; gap: 4px; justify-content: flex-end; align-items: center; }
.st { font-size: 12px; letter-spacing: -2px; padding-right: 2px; }
.st.read { color: var(--accent); }
.bubble.has-photo { padding: 4px 4px 6px; }
.bubble.has-photo .from, .bubble.has-photo .text, .bubble.has-photo .meta { padding: 0 8px; }
.photo { display: block; max-width: 100%; max-height: 360px; min-height: 80px; border-radius: 14px; cursor: zoom-in; background: var(--line); }
.empty { text-align: center; color: var(--muted); margin-top: 30vh; font-size: 18px; }

.composer {
  flex: none; display: flex; align-items: flex-end; gap: 8px;
  padding: 8px 10px calc(8px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line); background: var(--bg);
}
html.kb .composer { padding-bottom: 8px; }
.composer .icon-btn { margin: 0; }
.composer textarea {
  flex: 1; resize: none; min-height: 44px; max-height: 140px;
  border: 1px solid var(--line); border-radius: 22px; padding: 10px 16px;
  background: var(--surface); outline: none; line-height: 1.35;
}
.send {
  width: 44px; height: 44px; flex: none; border: 0; border-radius: 50%;
  background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; cursor: pointer;
}
.send:active { filter: brightness(.9); }

/* ---------- Настройки ---------- */
.settings { padding: 8px 16px calc(24px + env(safe-area-inset-bottom)); }
.settings section { padding: 16px 0; border-bottom: 1px solid var(--line); }
.settings section:last-child { border-bottom: 0; }
.settings h2 { font-size: 15px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 0 0 10px; }
.settings p { margin: 0 0 12px; }
.people { display: grid; gap: 4px; }
.person { display: flex; align-items: center; gap: 10px; padding: 6px 0; }
.person .link { font-size: 14px; padding: 8px 2px; white-space: nowrap; }
.person .av { width: 40px; height: 40px; font-size: 17px; }
.person-name { flex: 1; font-weight: 600; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.linkbox { margin-top: 12px; padding: 14px; border-radius: 14px; background: var(--accent-soft); display: grid; gap: 10px; }
.linkbox p { margin: 0; font-size: 15px; }
.linkbox input { width: 100%; font-size: 14px !important; }

/* ---------- Экраны входа/установки ---------- */
.center-screen {
  height: 100%; overflow-y: auto; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 14px; text-align: center;
  padding: calc(env(safe-area-inset-top) + 24px) 24px calc(env(safe-area-inset-bottom) + 24px);
  max-width: 460px; margin: 0 auto;
}
.center-screen h1 { margin: 6px 0 0; font-size: 28px; letter-spacing: -.02em; }
.center-screen p { margin: 0; font-size: 18px; }
.error { color: var(--danger); min-height: 1.4em; }
.steps { text-align: left; font-size: 18px; padding-left: 26px; margin: 4px 0; display: grid; gap: 12px; }
.steps svg { color: var(--accent); }
.install { justify-content: flex-start; }

/* ---------- Всплывашки ---------- */
#toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  top: calc(env(safe-area-inset-top) + 12px); z-index: 50;
  max-width: min(92vw, 520px); padding: 12px 18px; border-radius: 16px;
  background: var(--ink); color: var(--bg); font-size: 16px; box-shadow: 0 6px 24px rgba(0,0,0,.25);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; cursor: pointer;
}
.viewer {
  position: fixed; inset: 0; z-index: 60; background: rgba(0,0,0,.92);
  display: grid; place-items: center; padding: env(safe-area-inset-top) 0 env(safe-area-inset-bottom);
}
.viewer img { max-width: 100vw; max-height: 100%; object-fit: contain; }
.viewer-close {
  position: absolute; top: calc(env(safe-area-inset-top) + 10px); right: 14px;
  width: 44px; height: 44px; border-radius: 50%; border: 0; background: rgba(255,255,255,.18); color: #fff; font-size: 20px;
}
