:root {
  --bg: #313338; --side: #2b2d31; --dark: #1e1f22; --user: #232428; --input: #383a40; --text: #dbdee1; --muted: #949ba4;
  --white: #f2f3f5; --sel: #404249; --hover: #35373c; --acc: #5865f2; --green: #23a559; --red: #da373c; --line: #3f4147; --link: #00a8fc;
  --sat: env(safe-area-inset-top, 0px); --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px); --sar: env(safe-area-inset-right, 0px);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--dark); color: var(--text); overflow: hidden; overscroll-behavior: none;
  font: 16px/1.35 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; -webkit-text-size-adjust: 100%; }
#app { position: fixed; left: 0; top: 0; width: 100%; height: 100%; }
.hidden { display: none !important; }
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
input, textarea { font: inherit; color: var(--text); }
.screen { position: absolute; inset: 0; }

/* вход */
#login { display: flex; align-items: center; justify-content: center; background: var(--bg); padding: var(--sat) 20px var(--sab); overflow: auto; }
#loginForm { width: 100%; max-width: 340px; display: flex; flex-direction: column; gap: 10px; }
.logo { font-size: 30px; font-weight: 800; color: var(--white); text-align: center; letter-spacing: .5px; }
.sub { text-align: center; color: var(--muted); margin-bottom: 8px; }
#loginForm input { background: var(--input); border: 1px solid transparent; border-radius: 8px; padding: 12px 14px; outline: none; width: 100%; }
#loginForm input:focus { border-color: var(--acc); }
#loginForm .reg { display: none; }
#loginForm.regmode .reg { display: block; }
.err { color: #fa777c; min-height: 18px; font-size: 14px; text-align: center; }
.btn { border-radius: 8px; padding: 12px; font-weight: 600; }
.btn.primary { background: var(--acc); color: #fff; }
.btn.primary:active { filter: brightness(.9); }
.btn.link { color: var(--link); font-weight: 400; }

/* основной каркас: на телефоне два экрана, на широких — рядом */
#main { display: flex; background: var(--bg); }
#side { width: 100%; background: var(--side); display: flex; flex-direction: column; min-width: 0; padding-left: var(--sal); }
#chat { position: absolute; inset: 0; background: var(--bg); display: flex; flex-direction: column; transform: translateX(100%);
  transition: transform .22s ease; min-width: 0; padding-right: var(--sar); }
#main.inchat #chat { transform: none; }
#empty { display: none; }
.bar { height: calc(52px + var(--sat)); padding: var(--sat) 8px 0 8px; display: flex; align-items: center; gap: 4px; flex: none;
  background: var(--bg); border-bottom: 1px solid var(--dark); }
#side .bar { background: var(--side); padding-left: 14px; }
.title { font-weight: 800; font-size: 19px; color: var(--white); flex: 1; }
.conn { width: 10px; height: 10px; border-radius: 50%; background: var(--green); margin-right: 6px; }
.conn.off { background: #f0b232; }
.ib { width: 40px; height: 40px; border-radius: 50%; font-size: 20px; color: var(--muted); display: flex; align-items: center; justify-content: center; flex: none; text-decoration: none; }
.ib:active { background: var(--hover); }
.ib.back { font-size: 32px; line-height: 1; }
.ib.send { color: var(--acc); }
.ctitle { flex: 1; min-width: 0; margin-left: 4px; }
#chatName { font-weight: 700; color: var(--white); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#chatSub { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
@media (min-width: 820px) {
  #side { width: 300px; flex: none; border-right: 1px solid var(--dark); }
  #chat { position: relative; inset: auto; flex: 1; transform: none; }
  .ib.back { display: none; }
  #empty { display: flex; position: absolute; inset: 0; align-items: center; justify-content: center; color: var(--muted); background: var(--bg); z-index: 2; }
  #main.inchat #empty { display: none; }
}

/* список чатов */
#list { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding-bottom: var(--sab); }
.sect { padding: 14px 16px 6px; font-size: 12px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .5px; }
.item { display: flex; align-items: center; gap: 12px; padding: 8px 14px; min-height: 56px; }
.item:active, .item.sel { background: var(--sel); }
.item .nm { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: var(--white); }
.item .nm small { display: block; color: var(--muted); font-size: 12px; }
.item.off .nm { color: var(--muted); }
.badge { background: var(--red); color: #fff; border-radius: 10px; min-width: 20px; height: 20px; padding: 0 6px; font-size: 12px; font-weight: 700; display: flex; align-items: center; justify-content: center; }
.badge.men { background: var(--acc); }
.av { width: 40px; height: 40px; border-radius: 50%; flex: none; position: relative; display: flex; align-items: center; justify-content: center;
  font-weight: 700; color: #fff; font-size: 17px; background-size: cover; background-position: center; }
.av.sm { width: 32px; height: 32px; font-size: 14px; }
.av.xl { width: 120px; height: 120px; font-size: 48px; }
.av .dot { position: absolute; right: -2px; bottom: -2px; width: 13px; height: 13px; border-radius: 50%; border: 3px solid var(--side); background: var(--muted); box-sizing: content-box; margin: 0; }
.av .dot.on { background: var(--green); }
.av.sm .dot { width: 9px; height: 9px; border-width: 2px; border-color: var(--bg); }

/* сообщения */
#msgs { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 8px 0 6px; overscroll-behavior: contain; }
.dsep { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 12px; font-weight: 600; padding: 12px 14px 4px; }
.dsep::before, .dsep::after { content: ""; flex: 1; height: 1px; background: var(--line); }
.msg { display: flex; gap: 12px; padding: 2px 14px 2px 14px; position: relative; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
.msg.first { margin-top: 10px; padding-top: 3px; }
.msg.hl { background: rgba(250, 190, 40, .14); }
.msg.pressed { background: var(--hover); }
.msg .gut { width: 40px; flex: none; font-size: 10px; color: var(--muted); text-align: center; padding-top: 5px; visibility: hidden; }
.msg .body { flex: 1; min-width: 0; }
.msg .head { display: flex; align-items: baseline; gap: 8px; }
.msg .who { font-weight: 600; color: var(--white); }
.msg .adm { color: var(--acc); font-size: 13px; }
.msg .ts { font-size: 11px; color: var(--muted); }
.msg .tx { white-space: pre-wrap; overflow-wrap: anywhere; word-break: break-word; }
.msg .ed { font-size: 11px; color: var(--muted); }
.msg .tick { font-size: 11px; color: var(--muted); margin-left: 4px; }
.msg .tick.rd { color: var(--link); }
.msg.mention { background: rgba(88, 101, 242, .15); box-shadow: inset 3px 0 0 var(--acc); }
.msg .pin { font-size: 11px; color: var(--muted); }
a.lnk { color: var(--link); text-decoration: none; word-break: break-all; }
.men { background: rgba(88, 101, 242, .3); color: #c9cdfb; border-radius: 4px; padding: 0 2px; }
.men.me { background: rgba(250, 166, 26, .3); color: #ffd48a; }
mark { background: #f0b41e; color: #181818; border-radius: 2px; }
mark.cur { background: #ff8c1e; }
.reply { display: flex; gap: 6px; align-items: center; font-size: 13px; color: var(--muted); margin-bottom: 2px; padding-left: 8px; border-left: 2px solid var(--line); overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.reply b { color: var(--text); font-weight: 600; }
.rxs { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; }
.rx { background: var(--side); border: 1px solid transparent; border-radius: 8px; padding: 1px 7px; font-size: 14px; display: flex; gap: 4px; align-items: center; }
.rx.mine { background: rgba(88, 101, 242, .25); border-color: var(--acc); }
.rx span { font-size: 12px; color: var(--text); }
.img { display: block; max-width: min(300px, 100%); max-height: 320px; border-radius: 8px; margin-top: 3px; background: var(--user); min-width: 80px; min-height: 60px; object-fit: contain; }
.img.stk { background: none; max-width: 150px; max-height: 150px; min-width: 0; min-height: 0; }
.file { display: inline-flex; align-items: center; gap: 10px; background: var(--side); border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; margin-top: 3px; max-width: 100%; }
.file .fi { font-size: 24px; }
.file .fn { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--link); }
.file .fs { font-size: 12px; color: var(--muted); }
.pv { margin-top: 4px; max-width: min(380px, 100%); background: var(--side); border-left: 4px solid var(--acc); border-radius: 6px; padding: 8px 10px; }
.pv .ps { font-size: 12px; color: var(--muted); }
.pv .pt { color: var(--link); font-weight: 600; margin: 2px 0; }
.pv .pd { font-size: 13px; color: var(--text); display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.pv img { display: block; max-width: 100%; max-height: 180px; border-radius: 6px; margin-top: 6px; }
#typing { height: 18px; font-size: 12px; color: var(--muted); padding: 0 16px; flex: none; }
.more { text-align: center; color: var(--muted); padding: 20px; }

/* ввод */
#compose { flex: none; padding: 4px 8px calc(6px + var(--sab)) 8px; background: var(--bg); }
#compose .row { display: flex; align-items: flex-end; gap: 4px; background: var(--input); border-radius: 22px; padding: 2px 4px; }
#input { flex: 1; min-width: 0; background: none; border: 0; outline: none; resize: none; padding: 9px 4px; max-height: 120px; line-height: 1.3; }
#compose .ib { height: 40px; }
.ctx { display: flex; align-items: center; background: var(--side); border-radius: 10px 10px 0 0; padding: 4px 4px 4px 12px; font-size: 13px; color: var(--muted); margin-bottom: 4px; }
.ctx > div { flex: 1; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.ctx b { color: var(--text); }
#emoji { margin-top: 6px; background: var(--side); border-radius: 12px; overflow: hidden; }
#emoji .tabs { display: flex; border-bottom: 1px solid var(--dark); }
#emoji .tabs button { flex: 1; padding: 9px; color: var(--muted); font-weight: 600; border-bottom: 2px solid transparent; }
#emoji .tabs button.on { color: var(--white); border-color: var(--acc); }
#emGrid { height: 210px; overflow-y: auto; display: grid; padding: 6px; gap: 2px; align-content: start; -webkit-overflow-scrolling: touch; }
#emGrid.e { grid-template-columns: repeat(8, 1fr); }
#emGrid.s { grid-template-columns: repeat(4, 1fr); }
#emGrid button { font-size: 26px; border-radius: 8px; padding: 4px 0; }
#emGrid button:active { background: var(--sel); }
#emGrid img { width: 100%; max-width: 80px; display: block; margin: 0 auto; }

/* поиск */
.searchbar { display: flex; align-items: center; gap: 2px; padding: 4px 8px; background: var(--side); flex: none; }
.searchbar input { flex: 1; min-width: 0; background: var(--input); border: 0; outline: none; border-radius: 8px; padding: 8px 10px; }
#searchCnt { font-size: 12px; color: var(--muted); min-width: 36px; text-align: center; }

/* панель звонка в чате */
.callbar { display: flex; align-items: center; gap: 10px; background: var(--green); color: #fff; padding: 6px 14px; font-weight: 600; flex: none; }
.callbar span { flex: 1; }

/* оверлеи */
.overlay { position: fixed; inset: 0; z-index: 50; }
#call { background: linear-gradient(#1e1f22, #111214); display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px; padding: var(--sat) 0 var(--sab); z-index: 60; }
#callName { font-size: 26px; font-weight: 700; color: var(--white); }
#callState { color: var(--muted); }
.callbtns { display: flex; gap: 28px; margin-top: 36px; }
.cb { width: 68px; height: 68px; border-radius: 50%; font-size: 28px; display: flex; align-items: center; justify-content: center; color: #fff; }
.cb.green { background: var(--green); }
.cb.red { background: var(--red); }
.cb.red, .cb.red * { }
#cHang { transform: rotate(135deg); }
.cb.gray { background: #4e5058; }
.cb.gray.on { background: #fff; color: #111; }
#sheet { background: rgba(0, 0, 0, .55); display: flex; align-items: flex-end; z-index: 70; }
#sheetBody { width: 100%; max-width: 560px; margin: 0 auto; background: var(--side); border-radius: 16px 16px 0 0; padding: 8px 0 calc(8px + var(--sab)); max-height: 80%; overflow-y: auto; }
@media (min-width: 820px) { #sheet { align-items: center; } #sheetBody { border-radius: 16px; padding-bottom: 8px; } }
.srx { display: flex; justify-content: space-around; padding: 8px 10px 10px; border-bottom: 1px solid var(--dark); }
.srx button { font-size: 28px; width: 46px; height: 46px; border-radius: 50%; }
.srx button:active { background: var(--sel); }
.sact { display: flex; align-items: center; gap: 14px; width: 100%; padding: 14px 20px; text-align: left; }
.sact:active { background: var(--sel); }
.sact.red { color: #fa777c; }
.sact .si { width: 24px; text-align: center; }
.sform { padding: 10px 20px; display: flex; flex-direction: column; gap: 10px; }
.sform input[type=text] { background: var(--input); border: 0; outline: none; border-radius: 8px; padding: 11px 12px; width: 100%; }
.sform label { font-size: 12px; color: var(--muted); text-transform: uppercase; font-weight: 700; }
.pinitem { padding: 10px 20px; border-bottom: 1px solid var(--dark); }
.pinitem small { color: var(--muted); display: block; }
.pinitem div { overflow: hidden; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; }
#viewer { background: rgba(0, 0, 0, .94); display: flex; align-items: center; justify-content: center; z-index: 80; }
#viewer img { max-width: 100%; max-height: 100%; object-fit: contain; }
#viewer .ib { position: absolute; top: calc(10px + var(--sat)); background: rgba(40, 40, 44, .8); color: #fff; }
#viewerClose { right: 10px; }
#viewerSave { right: 60px; }
#toast { position: fixed; left: 50%; bottom: calc(90px + var(--sab)); transform: translateX(-50%); background: #111214; color: var(--white); padding: 10px 16px; border-radius: 20px;
  font-size: 14px; opacity: 0; transition: opacity .2s; pointer-events: none; z-index: 100; max-width: 85%; text-align: center; }
#toast.on { opacity: 1; }
::-webkit-scrollbar { width: 8px; }
::-webkit-scrollbar-thumb { background: #1a1b1e; border-radius: 4px; }
