/* Связь v2 — светлая тема по умолчанию (удобнее возрастным), крупные элементы. */
:root {
  --bg: #eef1f6; --panel: #ffffff; --panel2: #f5f7fb; --border: #dde3ee;
  --text: #17202e; --muted: #6b7686; --accent: #2f7cf6; --accent-dark: #1f63d6;
  --green: #2fb96d; --red: #e5484d; --bubble-my: #d8e8ff; --bubble: #ffffff;
  --radius: 16px; --fs: 17px;
}
:root[data-font="large"] { --fs: 20px; }
:root[data-font="xlarge"] { --fs: 23px; }

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; }
body {
  background: var(--bg); color: var(--text);
  font: var(--fs)/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
}
button { font: inherit; cursor: pointer; border: none; border-radius: 12px; background: none; color: inherit; }
input, textarea { font: inherit; color: inherit; }
.hidden { display: none !important; }

/* ---------- аватары ---------- */
.ava {
  width: 52px; height: 52px; border-radius: 50%; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  color: #fff; font-weight: 700; font-size: 1.2em; position: relative;
}
.ava.small { width: 40px; height: 40px; font-size: 1em; }
.ava .dot {
  position: absolute; right: 0; bottom: 0; width: 14px; height: 14px;
  border-radius: 50%; background: #b9c2d0; border: 2.5px solid var(--panel);
}
.ava .dot.on { background: var(--green); }

/* ---------- каркас ---------- */
#app { height: 100dvh; display: flex; flex-direction: column; max-width: 640px; margin: 0 auto; background: var(--bg); }
.screen { flex: 1; display: flex; flex-direction: column; min-height: 0; }

.topbar {
  display: flex; align-items: center; gap: 10px; padding: 12px 16px;
  padding-top: calc(12px + env(safe-area-inset-top));
  background: var(--panel); border-bottom: 1px solid var(--border); flex-shrink: 0;
}
.topbar h1 { font-size: 1.25em; }
.topbar .spacer { margin-left: auto; }

#connBanner {
  background: #f6c445; color: #4a3a00; text-align: center; padding: 6px 10px;
  font-size: .85em; display: none;
}
#connBanner.show { display: block; }

/* ---------- вкладки ---------- */
#tabs {
  display: flex; background: var(--panel); border-top: 1px solid var(--border); flex-shrink: 0;
  padding-bottom: env(safe-area-inset-bottom);
}
#tabs button {
  flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 8px 0 6px; color: var(--muted); border-radius: 0; position: relative; font-size: .78em;
}
#tabs button .ico { font-size: 1.85em; line-height: 1.1; }
#tabs button.active { color: var(--accent); }
#tabs .badge {
  position: absolute; top: 2px; left: calc(50% + 8px); min-width: 21px; height: 21px;
  background: var(--red); color: #fff; border-radius: 11px; font-size: 12px; font-weight: 700;
  display: flex; align-items: center; justify-content: center; padding: 0 5px;
}

/* ---------- список чатов / контактов ---------- */
.list { flex: 1; overflow-y: auto; }
.item {
  display: flex; align-items: center; gap: 14px; padding: 12px 16px;
  background: var(--panel); border-bottom: 1px solid var(--border); cursor: pointer;
  width: 100%; text-align: left; border-radius: 0;
}
.item:active { background: var(--panel2); }
.item .col { flex: 1; min-width: 0; }
.item .row1 { display: flex; align-items: baseline; gap: 8px; }
.item .name { font-weight: 600; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .when { color: var(--muted); font-size: .75em; flex-shrink: 0; }
.item .row2 { display: flex; align-items: center; gap: 8px; margin-top: 2px; }
.item .preview { color: var(--muted); font-size: .88em; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .unread {
  min-width: 24px; height: 24px; background: var(--accent); color: #fff; border-radius: 12px;
  font-size: .75em; font-weight: 700; display: flex; align-items: center; justify-content: center; padding: 0 7px;
}
.empty { text-align: center; color: var(--muted); padding: 48px 24px; }
.empty .big { font-size: 3em; margin-bottom: 10px; }

/* ---------- экран чата ---------- */
#chatScreen { position: fixed; inset: 0; z-index: 20; background: var(--bg); display: flex; flex-direction: column; max-width: 640px; margin: 0 auto; }
#chatScreen .topbar { cursor: default; }
#backBtn { font-size: 1.6em; padding: 4px 10px 4px 0; color: var(--accent); }
#chatHead { display: flex; align-items: center; gap: 10px; flex: 1; min-width: 0; }
#chatHead .col { min-width: 0; }
#chatTitle { font-weight: 700; font-size: 1.05em; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#chatSub { color: var(--muted); font-size: .78em; }
.callBtn { font-size: 1.5em; padding: 8px 10px; color: var(--accent); }

#messages { flex: 1; overflow-y: auto; padding: 14px 12px; display: flex; flex-direction: column; gap: 6px; }
.dateSep { align-self: center; color: var(--muted); font-size: .75em; background: var(--panel); padding: 3px 12px; border-radius: 12px; margin: 8px 0 4px; }
.msg {
  max-width: 82%; padding: 9px 13px; border-radius: 18px; background: var(--bubble);
  box-shadow: 0 1px 1px rgba(20,30,50,.06); word-wrap: break-word; white-space: pre-wrap;
  align-self: flex-start; border-bottom-left-radius: 6px;
}
.msg.mine { align-self: flex-end; background: var(--bubble-my); border-bottom-left-radius: 18px; border-bottom-right-radius: 6px; }
.msg .who { font-size: .75em; font-weight: 700; margin-bottom: 2px; }
.msg .time { font-size: .68em; color: var(--muted); text-align: right; margin-top: 3px; }
.msg.sys { align-self: center; background: none; box-shadow: none; color: var(--muted); font-size: .8em; padding: 2px; }
.msg.call { display: flex; align-items: center; gap: 10px; }
.msg.call .ico { font-size: 1.4em; }
.msg img.thumb { max-width: 100%; border-radius: 12px; display: block; margin-bottom: 4px; }
.fileBox { display: flex; align-items: center; gap: 12px; }
.fileBox .fico { font-size: 2em; }
.fileBox .fname { font-weight: 600; word-break: break-all; }
.fileBox .fsize { color: var(--muted); font-size: .78em; }
.fileBox a.dl, .fileBox button.dl {
  background: var(--accent); color: #fff; padding: 8px 14px; border-radius: 10px;
  text-decoration: none; font-weight: 600; font-size: .85em; flex-shrink: 0;
}
.fileBox .gone { color: var(--muted); font-size: .85em; }

#inputBar {
  display: flex; align-items: flex-end; gap: 8px; padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  background: var(--panel); border-top: 1px solid var(--border); flex-shrink: 0;
}
#attachBtn { font-size: 1.7em; color: var(--muted); padding: 8px 6px; }
#msgInput {
  flex: 1; resize: none; max-height: 120px; padding: 11px 14px; border-radius: 22px;
  border: 1px solid var(--border); background: var(--panel2); outline: none;
}
#msgInput:focus { border-color: var(--accent); }
#sendBtn {
  width: 46px; height: 46px; border-radius: 50%; background: var(--accent); color: #fff;
  font-size: 1.3em; display: flex; align-items: center; justify-content: center; flex-shrink: 0;
}
#sendBtn:disabled { opacity: .4; }
#uploadBar { padding: 6px 16px; background: var(--panel); font-size: .8em; color: var(--muted); display: none; }
#uploadBar.show { display: block; }
#uploadBar .track { height: 6px; background: var(--panel2); border-radius: 3px; margin-top: 4px; overflow: hidden; }
#uploadBar .fill { height: 100%; background: var(--accent); width: 0; transition: width .2s; }

/* баннер активного звонка в чате */
#joinCallBanner {
  display: none; margin: 8px 12px 0; padding: 12px 16px; background: var(--green); color: #fff;
  border-radius: var(--radius); text-align: center; font-weight: 700; cursor: pointer; width: calc(100% - 24px);
}
#joinCallBanner.show { display: block; }

/* ---------- звонок ---------- */
#callScreen { position: fixed; inset: 0; z-index: 40; background: #10141c; color: #fff; display: none; flex-direction: column; }
#callScreen.show { display: flex; }
#callInfo { text-align: center; padding: calc(14px + env(safe-area-inset-top)) 16px 8px; }
#callTitle { font-size: 1.2em; font-weight: 700; }
#callStatus { color: #9aa7bd; font-size: .85em; margin-top: 2px; }
#videos { flex: 1; display: grid; gap: 6px; padding: 6px; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); align-content: center; overflow-y: auto; }
.tile { position: relative; border-radius: 14px; overflow: hidden; background: #1a2230; aspect-ratio: 3/4; max-height: 70dvh; }
#videos.count1 .tile, #videos.count2 .tile { aspect-ratio: auto; height: 100%; min-height: 200px; }
.tile video { width: 100%; height: 100%; object-fit: cover; display: block; }
.tile.me video { transform: scaleX(-1); }
.tile .tag { position: absolute; left: 8px; bottom: 8px; background: rgba(0,0,0,.55); padding: 3px 10px; border-radius: 9px; font-size: .78em; }
.tile .novid {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  font-size: 2.6em; font-weight: 700; background: linear-gradient(135deg, #2b3a5e, #1a2338);
}
#callControls { display: flex; justify-content: center; gap: 16px; padding: 14px 0 calc(20px + env(safe-area-inset-bottom)); }
.ctl {
  width: 62px; height: 62px; border-radius: 50%; background: #2c3648; color: #fff; font-size: 1.55em;
  display: flex; align-items: center; justify-content: center;
}
.ctl.off { background: var(--red); }
.ctl.hang { background: var(--red); }

/* входящий звонок */
#incomingScreen {
  position: fixed; inset: 0; z-index: 50; background: linear-gradient(160deg, #1d2a44, #101625);
  color: #fff; display: none; flex-direction: column; align-items: center; justify-content: space-between;
  padding: 15dvh 20px 10dvh; text-align: center;
}
#incomingScreen.show { display: flex; }
#incomingName { font-size: 2em; font-weight: 800; margin-top: 14px; }
#incomingKind { color: #9aa7bd; margin-top: 6px; font-size: 1.05em; }
#incomingScreen .btns { display: flex; gap: 15vw; }
#incomingScreen .btns > div { display: flex; flex-direction: column; align-items: center; gap: 10px; font-size: .95em; }
.bigCtl { width: 82px; height: 82px; border-radius: 50%; font-size: 2.2em; color: #fff; display: flex; align-items: center; justify-content: center; }
.bigCtl.acc { background: var(--green); animation: pulse 1.3s infinite; }
.bigCtl.dec { background: var(--red); }
@keyframes pulse { 0%,100% { transform: scale(1); } 50% { transform: scale(1.09); } }

/* ---------- настройки ---------- */
.settingsBody { padding: 16px; overflow-y: auto; }
.sCard { background: var(--panel); border-radius: var(--radius); padding: 16px; margin-bottom: 14px; }
.sCard h3 { font-size: .85em; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; margin-bottom: 12px; }
.sCard .rowline { display: flex; align-items: center; gap: 12px; }
.sCard input[type="text"] {
  flex: 1; min-width: 0; width: 100%; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--panel2); outline: none;
}
.sCard .rowline .btn { flex-shrink: 0; }
.btn { background: var(--accent); color: #fff; padding: 11px 18px; border-radius: 12px; font-weight: 600; }
.btn.gray { background: var(--panel2); color: var(--text); border: 1px solid var(--border); }
.btn.danger { background: var(--red); }
.btn.big { width: 100%; padding: 14px; font-size: 1.05em; }
.fontBtns { display: flex; gap: 10px; }
.fontBtns button { flex: 1; min-width: 0; padding: 12px 4px; border: 2px solid var(--border); border-radius: 12px; background: var(--panel2); font-weight: 700; font-size: .8em; }
.fontBtns button:nth-child(2) { font-size: .95em; }
.fontBtns button:nth-child(3) { font-size: 1.1em; }
.fontBtns button.active { border-color: var(--accent); color: var(--accent); }
.hint { color: var(--muted); font-size: .82em; margin-top: 10px; line-height: 1.5; }
.statusline { display: flex; align-items: center; gap: 8px; margin-bottom: 12px; }
.statusline .dot2 { width: 11px; height: 11px; border-radius: 50%; background: var(--red); }
.statusline .dot2.on { background: var(--green); }

/* ---------- новая группа ---------- */
#groupScreen { position: fixed; inset: 0; z-index: 25; background: var(--bg); display: none; flex-direction: column; max-width: 640px; margin: 0 auto; }
#groupScreen.show { display: flex; }
#groupScreen .pad { padding: 14px 16px; }
#groupName { width: 100%; padding: 12px 14px; border-radius: 12px; border: 1px solid var(--border); background: var(--panel); outline: none; }
.pick { display: flex; align-items: center; gap: 14px; padding: 11px 16px; background: var(--panel); border-bottom: 1px solid var(--border); cursor: pointer; }
.pick input { width: 24px; height: 24px; accent-color: var(--accent); }
.pick .name { font-weight: 600; }

/* ---------- установка на iPhone ---------- */
#installBanner {
  display: none; margin: 10px 14px; padding: 14px 16px; background: var(--panel);
  border: 2px solid var(--accent); border-radius: var(--radius); cursor: pointer;
}
#installBanner.show { display: block; }
#installBanner b { color: var(--accent); }
#installScreen { position: fixed; inset: 0; z-index: 60; background: var(--bg); display: none; flex-direction: column; max-width: 640px; margin: 0 auto; }
#installScreen.show { display: flex; }
.steps { padding: 10px 18px 30px; overflow-y: auto; }
.step { display: flex; gap: 14px; margin-top: 20px; align-items: flex-start; }
.step .n {
  width: 38px; height: 38px; border-radius: 50%; background: var(--accent); color: #fff;
  font-weight: 800; display: flex; align-items: center; justify-content: center; flex-shrink: 0; font-size: 1.1em;
}
.step .t { padding-top: 5px; line-height: 1.5; }
.step .t .icon { display: inline-block; background: var(--panel); border: 1px solid var(--border); border-radius: 8px; padding: 1px 9px; }

/* ---------- десктоп ---------- */
@media (min-width: 900px) {
  #app, #chatScreen, #groupScreen, #installScreen { max-width: 900px; }
}

/* тёмная тема — следуем системной */
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1420; --panel: #171e2e; --panel2: #1d2638; --border: #2a3550;
    --text: #e8edf7; --muted: #8b97b0; --bubble-my: #2a4a80; --bubble: #1d2638;
  }
  .msg { box-shadow: none; }
}
