:root {
  --navy: #140e36;
  --purple: #402158;
  --mauve: #7d5683;
  --beige: #c9bfb5;
  --lavender: #e2e9ff;
  --accent: var(--purple);
  --accent-2: var(--lavender);
  --bg: var(--lavender);
  --panel: #ffffff;
  --line: #ddd8e6;
  --text: #1c1630;
  --muted: #7b7390;
  --mine: var(--purple);
  --theirs: #ffffff;
  --badge: var(--mauve);
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--text); background: var(--bg);
  -webkit-tap-highlight-color: transparent;
}
.hidden { display: none !important; }
button { font: inherit; cursor: pointer; }

/* --- вход --- */
.login { height: 100%; display: flex; align-items: center; justify-content: center; padding: 20px; }
.login-box {
  width: 100%; max-width: 320px; background: var(--panel); border-radius: 16px;
  padding: 28px 24px; box-shadow: 0 4px 24px rgba(0,0,0,.08);
  display: flex; flex-direction: column; gap: 12px; align-items: stretch; text-align: center;
}
.login-box img { align-self: center; border-radius: 18px; }
.login-box h1 { margin: 0 0 4px; font-size: 22px; }
.login-box input { padding: 12px; border: 1px solid var(--line); border-radius: 10px; font: inherit; }
.login-box button { padding: 12px; border: 0; border-radius: 10px; background: var(--accent); color: #fff; font-weight: 600; }
.err { color: #c62828; min-height: 1.2em; margin: 0; font-size: 14px; }

/* --- раскладка --- */
.app { display: flex; height: 100%; }
.sidebar { width: 300px; flex-shrink: 0; background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; }
.side-head, .chat-head {
  height: 56px; flex-shrink: 0; display: flex; align-items: center; gap: 8px; padding: 0 12px;
  border-bottom: 1px solid var(--line); background: var(--panel);
}
.side-head { justify-content: space-between; }
.me { font-weight: 600; }
.icon-btn { border: 0; background: none; font-size: 20px; width: 40px; height: 40px; border-radius: 50%; color: var(--muted); }
.icon-btn:hover { background: var(--bg); }

.chat-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.chat-item { display: flex; align-items: center; gap: 12px; padding: 10px 12px; cursor: pointer; border-bottom: 1px solid #f1f3f5; }
.chat-item:hover { background: #f7f9fa; }
.chat-item.active { background: var(--accent-2); }
.avatar {
  width: 44px; height: 44px; border-radius: 50%; flex-shrink: 0; display: flex; align-items: center; justify-content: center;
  color: #fff; font-weight: 700; font-size: 18px;
}
.ci-body { flex: 1; min-width: 0; }
.ci-top { display: flex; justify-content: space-between; gap: 6px; }
.ci-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ci-time { color: var(--muted); font-size: 12px; flex-shrink: 0; }
.ci-bottom { display: flex; justify-content: space-between; gap: 6px; margin-top: 2px; }
.ci-last { color: var(--muted); font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.badge {
  background: var(--badge); color: #fff; border-radius: 11px; min-width: 22px; height: 22px; padding: 0 6px;
  font-size: 12px; font-weight: 700; display: inline-flex; align-items: center; justify-content: center; flex-shrink: 0;
}

/* --- чат --- */
.chat { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.chat-title { font-weight: 600; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.back { display: none; }
.net { width: 10px; height: 10px; border-radius: 50%; background: transparent; }
.net.off { background: #e53935; }

.messages { flex: 1; overflow-y: auto; padding: 12px 14px; display: flex; flex-direction: column; gap: 4px; }
.empty { margin: auto; color: var(--muted); }
.more { align-self: center; border: 1px solid var(--line); background: var(--panel); border-radius: 14px; padding: 4px 12px; color: var(--muted); margin-bottom: 8px; }
.day { align-self: center; background: rgba(0,0,0,.06); color: #555; font-size: 12px; border-radius: 10px; padding: 2px 10px; margin: 8px 0; }

.msg { max-width: min(75%, 560px); padding: 6px 10px 4px; border-radius: 14px; background: var(--theirs); align-self: flex-start;
  box-shadow: 0 1px 1px rgba(0,0,0,.06); white-space: pre-wrap; word-wrap: break-word; overflow-wrap: anywhere; }
.msg.mine { align-self: flex-end; background: var(--mine); }
.msg .who { font-size: 13px; font-weight: 600; margin-bottom: 1px; }
.msg .meta { font-size: 11px; color: var(--muted); text-align: right; margin-top: 2px; }
.msg a { color: var(--accent); }
.msg.pending { opacity: .6; }

.composer { display: flex; gap: 8px; padding: 8px 10px calc(8px + env(safe-area-inset-bottom)); background: var(--panel); border-top: 1px solid var(--line); }
.composer textarea {
  flex: 1; resize: none; border: 1px solid var(--line); border-radius: 20px; padding: 9px 14px; font: inherit;
  max-height: 140px; outline: none;
}
.composer textarea:focus { border-color: var(--accent); }
.composer button { width: 42px; height: 42px; border-radius: 50%; border: 0; background: var(--accent); color: #fff; font-size: 18px; align-self: flex-end; }
.composer button:disabled { opacity: .5; }

/* --- телефон: либо список, либо чат --- */
@media (max-width: 700px) {
  .sidebar { width: 100%; border-right: 0; }
  .chat { display: none; }
  body.in-chat .sidebar { display: none; }
  body.in-chat .chat { display: flex; }
  .back { display: inline-block; }
  .msg { max-width: 85%; }
}
.text-btn { border: 0; background: none; color: var(--muted); font-size: 13px; padding: 6px 8px; border-radius: 8px; }
.text-btn:hover { background: var(--bg); color: var(--text); }

/* --- палитра: тёмные шапки, фиолетовые свои сообщения --- */
.login { background: linear-gradient(160deg, var(--navy) 0%, var(--purple) 60%, var(--mauve) 100%); }
.login-box button { background: var(--purple); }
.login-box button:hover { background: var(--navy); }
.side-head, .chat-head { background: var(--navy); color: #fff; border-bottom-color: var(--navy); }
.side-head .text-btn, .chat-head .icon-btn { color: var(--beige); }
.side-head .text-btn:hover, .chat-head .icon-btn:hover { background: rgba(255,255,255,.1); color: #fff; }
.chat-item.active { background: var(--lavender); box-shadow: inset 3px 0 0 var(--mauve); }
.chat-item:hover { background: #f1f2fb; }
.messages { background: var(--lavender); }
.day { background: var(--beige); color: var(--navy); }
.msg.mine { color: #fff; }
.msg.mine .meta { color: rgba(255,255,255,.7); }
.msg.mine a { color: var(--beige); }
.more { color: var(--purple); border-color: var(--beige); }
.composer button { background: var(--purple); }
.composer button:hover { background: var(--navy); }
.composer textarea:focus { border-color: var(--mauve); }
.net.off { background: #ff6b81; }
