:root{
  --bg:#f4f5f7; --panel:#fff; --ink:#15181c; --muted:#6b7280; --line:#e5e7eb;
  --accent:#0f766e; --accent-ink:#fff; --mine:#0f766e; --mine-ink:#fff;
  --theirs:#fff; --theirs-ink:#15181c; --shadow:0 1px 2px rgba(0,0,0,.05);
}
@media (prefers-color-scheme: dark){
  :root{
    --bg:#0e1013; --panel:#171a1f; --ink:#e9eaec; --muted:#9aa1ab; --line:#262b33;
    --accent:#2dd4bf; --accent-ink:#06201d; --mine:#1c6f66; --mine-ink:#eafffb;
    --theirs:#20242b; --theirs-ink:#e9eaec; --shadow:0 1px 2px rgba(0,0,0,.4);
  }
}
*{box-sizing:border-box}
/* у .app, .login и .modal задан display — без этого правила атрибут hidden не скрывает их */
[hidden]{display:none !important}
html,body{height:100%}
body{
  margin:0;background:var(--bg);color:var(--ink);
  font:15px/1.5 -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,sans-serif;
  -webkit-font-smoothing:antialiased;overflow:hidden;
}
button,input,textarea{font:inherit;color:inherit}
button{cursor:pointer;border:0;background:none}

/* ── вход ── */
.login{height:100dvh;display:grid;place-items:center;padding:20px}
.login-card{
  width:min(380px,100%);background:var(--panel);border:1px solid var(--line);
  border-radius:16px;padding:28px 24px;box-shadow:var(--shadow);display:grid;gap:16px;
}
.brand{display:grid;justify-items:center;gap:10px;margin-bottom:6px}
.brand-logo{width:56px;height:56px}
.brand h1{margin:0;font-size:24px;letter-spacing:-.02em}
.login-card label{display:grid;gap:6px;font-size:13px;color:var(--muted)}
.login-card input{
  padding:12px 14px;border:1px solid var(--line);border-radius:10px;
  background:var(--bg);outline:none;
}
.login-card input:focus{border-color:var(--accent)}
.login-card button{
  padding:13px;border-radius:10px;background:var(--accent);color:var(--accent-ink);font-weight:600;
}
.login-card button:disabled{opacity:.6}
.error{margin:0;color:#dc2626;font-size:13.5px}
@media (prefers-color-scheme: dark){.error{color:#f87171}}

/* ── каркас ── */
.app{display:grid;grid-template-columns:320px 1fr;height:100dvh}
.sidebar{background:var(--panel);border-right:1px solid var(--line);display:flex;flex-direction:column;min-width:0}
.sidebar-head{
  display:flex;align-items:center;justify-content:space-between;gap:10px;
  padding:14px 16px;border-bottom:1px solid var(--line);
}
.me{display:flex;align-items:center;gap:10px;min-width:0}
.me-name{font-weight:600;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.icon-btn{
  display:grid;place-items:center;width:36px;height:36px;border-radius:10px;color:var(--muted);
}
.icon-btn:hover{background:var(--bg);color:var(--ink)}

.avatar{
  flex:0 0 auto;width:38px;height:38px;border-radius:50%;display:grid;place-items:center;
  color:#fff;font-weight:600;font-size:14px;letter-spacing:.02em;
}
.avatar.sm{width:30px;height:30px;font-size:12px}

.chat-list{overflow-y:auto;flex:1;padding:6px}
.chat-item{
  display:flex;gap:12px;align-items:center;width:100%;padding:10px 12px;border-radius:12px;text-align:left;
}
.chat-item:hover{background:var(--bg)}
.chat-item.active{background:var(--bg)}
.chat-item-main{min-width:0;flex:1}
.chat-item-top{display:flex;justify-content:space-between;gap:8px;align-items:baseline}
.chat-item-name{font-weight:600;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.chat-item-time{font-size:12px;color:var(--muted);flex:0 0 auto}
.chat-item-last{
  font-size:13.5px;color:var(--muted);white-space:nowrap;overflow:hidden;text-overflow:ellipsis;margin-top:2px;
}
.badge{
  flex:0 0 auto;min-width:20px;height:20px;padding:0 6px;border-radius:10px;background:var(--accent);
  color:var(--accent-ink);font-size:12px;font-weight:600;display:grid;place-items:center;
}

/* ── переписка ── */
.chat{display:flex;flex-direction:column;min-width:0;background:var(--bg)}
.empty{display:grid;place-items:center;height:100%;color:var(--muted)}
.chat-inner{display:flex;flex-direction:column;height:100%;min-height:0}
.chat-head{
  display:flex;align-items:center;gap:12px;padding:12px 18px;
  background:var(--panel);border-bottom:1px solid var(--line);
}
.chat-title-wrap{min-width:0}
.chat-title{font-weight:600;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.chat-sub{font-size:12.5px;color:var(--muted)}
.back{display:none}

.messages{flex:1;overflow-y:auto;padding:18px;display:flex;flex-direction:column;gap:2px}
.day{align-self:center;margin:14px 0 8px;padding:4px 12px;border-radius:20px;
     background:var(--panel);color:var(--muted);font-size:12.5px;box-shadow:var(--shadow)}
.row{display:flex;gap:8px;align-items:flex-end;max-width:min(660px,84%)}
.row.mine{align-self:flex-end;flex-direction:row-reverse}
.row + .row{margin-top:2px}
.row.first{margin-top:10px}
.bubble{
  padding:8px 12px;border-radius:16px;background:var(--theirs);color:var(--theirs-ink);
  box-shadow:var(--shadow);word-wrap:break-word;overflow-wrap:anywhere;white-space:pre-wrap;min-width:0;
}
.row.mine .bubble{background:var(--mine);color:var(--mine-ink)}
.bubble .sender{font-size:12.5px;font-weight:600;color:var(--accent);margin-bottom:2px}
.row.mine .bubble .sender{display:none}
.bubble .meta{font-size:11px;opacity:.65;margin-left:8px;float:right;margin-top:4px}
.bubble.deleted{font-style:italic;opacity:.6}
.bubble .file{
  display:flex;align-items:center;gap:8px;padding:8px 10px;margin-top:4px;
  border-radius:10px;background:rgba(127,127,127,.12);text-decoration:none;color:inherit;
}
.bubble .file small{opacity:.7}
.bubble img.photo{max-width:100%;border-radius:12px;margin-top:4px;display:block}
.avatar-slot{width:30px;flex:0 0 30px}

.typing{padding:0 18px 6px;color:var(--muted);font-size:13px;height:20px}

.composer{
  display:flex;align-items:flex-end;gap:8px;padding:12px 14px;
  background:var(--panel);border-top:1px solid var(--line);
}
.composer textarea{
  flex:1;resize:none;max-height:140px;padding:10px 14px;border:1px solid var(--line);
  border-radius:18px;background:var(--bg);outline:none;
}
.composer textarea:focus{border-color:var(--accent)}
.send{
  width:40px;height:40px;border-radius:50%;background:var(--accent);color:var(--accent-ink);
  display:grid;place-items:center;flex:0 0 auto;
}
.send:disabled{opacity:.45}
.attach{flex:0 0 auto;cursor:pointer}
.pending-files{padding:0 16px 12px;display:flex;gap:8px;flex-wrap:wrap;background:var(--panel)}
.pending-files span{
  font-size:12.5px;background:var(--bg);border:1px solid var(--line);
  padding:4px 10px;border-radius:20px;color:var(--muted);
}

/* ── модалка ── */
.modal{position:fixed;inset:0;background:rgba(0,0,0,.45);display:grid;place-items:center;padding:16px;z-index:20}
.modal-card{
  width:min(420px,100%);max-height:86dvh;background:var(--panel);border-radius:16px;
  padding:18px;display:flex;flex-direction:column;gap:12px;
}
.modal-card header{display:flex;align-items:center;justify-content:space-between}
.modal-card h2{margin:0;font-size:18px}
.tabs{display:flex;gap:6px;background:var(--bg);padding:4px;border-radius:10px}
.tab{flex:1;padding:8px;border-radius:8px;color:var(--muted);font-weight:500}
.tab.active{background:var(--panel);color:var(--ink);box-shadow:var(--shadow)}
#group-title{padding:11px 13px;border:1px solid var(--line);border-radius:10px;background:var(--bg);outline:none}
.people{overflow-y:auto;flex:1;display:grid;gap:2px;min-height:120px}
.person{display:flex;align-items:center;gap:10px;padding:8px 10px;border-radius:10px;width:100%;text-align:left}
.person:hover{background:var(--bg)}
.person.selected{background:var(--bg)}
.person .check{margin-left:auto;color:var(--accent);font-weight:700}
.primary{padding:12px;border-radius:10px;background:var(--accent);color:var(--accent-ink);font-weight:600}
.primary:disabled{opacity:.5}

/* ── телефон ── */
@media (max-width: 760px){
  .app{grid-template-columns:1fr}
  .sidebar{border-right:0}
  .app.viewing .sidebar{display:none}
  .app:not(.viewing) .chat{display:none}
  .back{display:grid}
  .row{max-width:88%}
  .messages{padding:14px}
}
