:root{
  --bg:#eceff4; --panel:#fff; --ink:#0e1621; --muted:#7b8794; --line:#e3e7ee;
  --brand:#2688eb; --brand-ink:#fff; --in-bg:#fff; --out-bg:#cfe6ff; --hover:#f3f6fa;
  --danger:#e64646; --ok:#3fb950; --shadow:0 2px 12px rgba(20,40,80,.08);
}
@media (prefers-color-scheme: dark){
  :root{ --bg:#0f1720; --panel:#161f2b; --ink:#e7edf5; --muted:#8a97a6; --line:#243040;
    --brand:#3d8bf0; --in-bg:#1d2836; --out-bg:#265079; --hover:#1c2735; --shadow:none; }
}
*{box-sizing:border-box}
html,body{height:100%;margin:0}
body{font:15px/1.4 -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Arial,sans-serif;
  background:var(--bg);color:var(--ink);-webkit-tap-highlight-color:transparent}
.hidden{display:none !important}
button{font:inherit;cursor:pointer;border:none;background:none;color:inherit}
input,textarea,select{font:inherit;color:inherit}

/* ВХОД */
.login{position:fixed;inset:0;display:flex;align-items:center;justify-content:center;padding:20px}
.login-card{background:var(--panel);padding:28px 24px;border-radius:16px;box-shadow:var(--shadow);
  width:100%;max-width:340px;display:flex;flex-direction:column;gap:12px}
.login-logo{font-size:26px;font-weight:800;color:var(--brand);text-align:center}
.login-sub{color:var(--muted);text-align:center;margin-bottom:8px;font-size:13px}
.login-card input{padding:12px 14px;border:1px solid var(--line);border-radius:10px;background:var(--bg)}
.login-card button{padding:12px;background:var(--brand);color:var(--brand-ink);border-radius:10px;font-weight:600}
.login-error{color:var(--danger);font-size:13px;text-align:center;min-height:16px}

/* КАРКАС */
.app{display:flex;height:100vh;height:100dvh;overflow:hidden}
.sidebar{width:360px;min-width:360px;background:var(--panel);border-right:1px solid var(--line);
  display:flex;flex-direction:column;height:100%}
.chat{flex:1;display:flex;flex-direction:column;height:100%;min-width:0}

/* ШАПКА СПИСКА */
.side-head{padding:12px;border-bottom:1px solid var(--line);display:flex;flex-direction:column;gap:10px}
.side-title{display:flex;align-items:center;justify-content:space-between;font-weight:700;font-size:17px}
.side-actions{display:flex;gap:4px}
.icon-btn{width:36px;height:36px;border-radius:9px;display:flex;align-items:center;justify-content:center;
  font-size:18px;color:var(--muted)}
.icon-btn:hover{background:var(--hover);color:var(--ink)}
.search{padding:9px 12px;border:1px solid var(--line);border-radius:10px;background:var(--bg);width:100%}
.filters{display:flex;gap:6px;flex-wrap:wrap}
.chip{padding:5px 11px;border-radius:20px;background:var(--bg);color:var(--muted);font-size:13px;border:1px solid transparent}
.chip.active{background:var(--brand);color:var(--brand-ink)}
.community-filter{padding:8px 10px;border:1px solid var(--line);border-radius:10px;background:var(--bg);width:100%}

/* СПИСОК ДИАЛОГОВ */
.conv-list{flex:1;overflow-y:auto}
.conv{display:flex;gap:10px;padding:11px 12px;border-bottom:1px solid var(--line);cursor:pointer;align-items:center}
.conv:hover{background:var(--hover)}
.conv.active{background:var(--hover)}
.conv .avatar{width:46px;height:46px;flex:0 0 46px}
.conv-main{flex:1;min-width:0}
.conv-top{display:flex;justify-content:space-between;gap:8px}
.conv-name{font-weight:600;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.conv-time{color:var(--muted);font-size:12px;flex:0 0 auto}
.conv-bottom{display:flex;justify-content:space-between;gap:8px;align-items:center;margin-top:2px}
.conv-preview{color:var(--muted);font-size:13px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;flex:1}
.conv-comm{font-size:11px;color:var(--brand);white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.badge{background:var(--brand);color:#fff;border-radius:20px;min-width:20px;height:20px;
  font-size:12px;display:flex;align-items:center;justify-content:center;padding:0 6px;flex:0 0 auto}
.dot-new{width:8px;height:8px;border-radius:50%;background:var(--ok);flex:0 0 auto}

.avatar{width:40px;height:40px;border-radius:50%;object-fit:cover;background:var(--line)}

/* ЧАТ */
.chat-empty{flex:1;display:flex;align-items:center;justify-content:center;color:var(--muted)}
.chat-view{flex:1;display:flex;flex-direction:column;min-height:0}
.chat-head{display:flex;align-items:center;gap:10px;padding:10px 12px;border-bottom:1px solid var(--line);background:var(--panel)}
.back-btn{display:none;font-size:26px}
.chat-peer{flex:1;min-width:0}
.chat-name{font-weight:700;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.chat-community{font-size:12px;color:var(--brand)}
.chat-head-actions{display:flex;gap:6px}
.mini-btn{padding:7px 12px;border-radius:8px;background:var(--bg);color:var(--muted);font-size:13px;border:1px solid transparent}
.mini-btn:hover{background:var(--hover);color:var(--ink)}
#btn-open.active{background:rgba(63,185,80,.15);color:var(--ok);border-color:var(--ok)}
#btn-close.active{background:rgba(230,70,70,.15);color:var(--danger);border-color:var(--danger)}

.messages{flex:1;overflow-y:auto;padding:16px;display:flex;flex-direction:column;gap:6px;
  background:var(--bg)}
.msg{max-width:72%;padding:8px 12px;border-radius:14px;box-shadow:var(--shadow);word-wrap:break-word;white-space:pre-wrap}
.msg.in{align-self:flex-start;background:var(--in-bg);border-bottom-left-radius:4px}
.msg.out{align-self:flex-end;background:var(--out-bg);border-bottom-right-radius:4px}
.msg-text{white-space:pre-wrap;overflow-wrap:anywhere;word-break:break-word}
.msg .lnk{color:var(--brand);text-decoration:none}
.msg .lnk:hover{text-decoration:underline}
.msg-time{font-size:11px;color:var(--muted);margin-top:3px;text-align:right}
.msg img.att{max-width:240px;border-radius:10px;display:block;margin:4px 0;cursor:pointer}
.msg a.att-doc{color:var(--brand);text-decoration:none}
.msg .att-audio{width:220px}

/* COMPOSER */
.composer{display:flex;align-items:flex-end;gap:6px;padding:10px 12px;border-top:1px solid var(--line);
  background:var(--panel);position:relative;padding-bottom:calc(10px + env(safe-area-inset-bottom))}
.msg-input{flex:1;resize:none;max-height:140px;padding:10px 14px;border:1px solid var(--line);
  border-radius:20px;background:var(--bg);line-height:1.35}
.send-btn{width:42px;height:42px;border-radius:50%;background:var(--brand);color:#fff;font-size:18px;flex:0 0 auto}
.send-btn:disabled{opacity:.5}
.attach-preview{position:absolute;bottom:64px;left:12px;background:var(--panel);padding:8px 12px;
  border-radius:10px;box-shadow:var(--shadow);display:flex;gap:8px;align-items:center;font-size:13px}
.attach-preview button{color:var(--danger)}

/* ПАНЕЛИ */
.panel{position:fixed;inset:0;background:rgba(0,0,0,.4);display:flex;align-items:center;justify-content:center;padding:16px;z-index:50}
.panel-box{background:var(--panel);border-radius:14px;width:100%;max-width:440px;max-height:86vh;
  overflow-y:auto;box-shadow:var(--shadow)}
.panel-head{display:flex;justify-content:space-between;align-items:center;padding:14px 16px;
  border-bottom:1px solid var(--line);font-weight:700;position:sticky;top:0;background:var(--panel)}
.templates-list,.communities-list{padding:8px 12px;display:flex;flex-direction:column;gap:6px}
.tpl{padding:10px 12px;border:1px solid var(--line);border-radius:10px;cursor:pointer}
.tpl:hover{background:var(--hover)}
.tpl-title{font-weight:600}
.tpl-body{color:var(--muted);font-size:13px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.tpl .del{float:right;color:var(--danger)}
.template-form{display:flex;flex-direction:column;gap:8px;padding:12px}
.template-form input,.template-form textarea{padding:10px 12px;border:1px solid var(--line);border-radius:10px;background:var(--bg)}
.template-form button{padding:11px;background:var(--brand);color:#fff;border-radius:10px;font-weight:600}
.comm{display:flex;gap:10px;align-items:center;padding:10px;border:1px solid var(--line);border-radius:10px}
.comm .avatar{width:38px;height:38px}
.comm-main{flex:1;min-width:0}
.comm-name{font-weight:600;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.comm-sub{font-size:12px;color:var(--muted)}
.comm-btns{display:flex;flex-direction:column;gap:4px;align-items:flex-end;flex:0 0 auto}
.comm-toggle{font-size:12px;padding:6px 10px;border-radius:8px;background:var(--bg)}
.comm-toggle.on{color:var(--ok)} .comm-toggle.off{color:var(--danger)}
.comm-backfill{font-size:12px;padding:6px 10px;border-radius:8px;background:var(--bg);color:var(--brand);white-space:nowrap}
.comm-backfill:hover{background:var(--hover)}
.comm-backfill:disabled{opacity:.6}
.comm-msg{font-size:13px;min-height:16px}
.panel-hint{padding:0 16px 16px;color:var(--muted);font-size:12px}

/* МОБИЛКА */
@media (max-width:760px){
  .sidebar{width:100%;min-width:0}
  .chat{position:absolute;inset:0;transform:translateX(100%);transition:transform .22s ease;z-index:10}
  .app.show-chat .chat{transform:translateX(0)}
  .app.show-chat .sidebar{display:none}
  .back-btn{display:flex}
  .chat-empty{display:none}
  .msg{max-width:84%}
}
