:root {
  --bg: #0e1621;
  --sidebar: #17212b;
  --panel: #1e2c3a;
  --bubble-in: #182533;
  --bubble-out: #2b5278;
  --accent: #5288c1;
  --accent2: #6ab3f3;
  --text: #e6ebf1;
  --muted: #7d8b99;
  --green: #25d366;
  --danger: #e55656;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}

body { overflow: hidden; }

.hidden { display: none !important; }

#app {
  display: grid;
  grid-template-columns: 320px 1fr;
  height: 100vh;
}

/* ---------- sidebar ---------- */
.sidebar {
  display: flex;
  flex-direction: column;
  background: var(--sidebar);
  border-right: 1px solid #0b0f14;
}

.profile {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px;
  border-bottom: 1px solid #0b0f14;
}

.avatar {
  width: 48px; height: 48px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--accent2), var(--accent));
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 18px; color: #fff;
  flex-shrink: 0;
}

.profile-info { min-width: 0; }

.profile-label { font-size: 12px; color: var(--muted); }

.conn {
  margin-left: auto;
  display: flex; align-items: center; gap: 6px;
  font-size: 11px; color: var(--muted);
}

.conn-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.conn-dot.on { background: var(--green); box-shadow: 0 0 6px var(--green); }
.conn-dot.poll { background: #f0b429; }
.conn-dot.off { background: var(--danger); }

.profile-id {
  display: flex; align-items: center; gap: 8px;
  font-size: 22px; font-weight: 700; letter-spacing: 2px;
  color: var(--accent2);
}

#copyId {
  background: none; border: none; color: var(--muted);
  font-size: 16px; cursor: pointer; padding: 2px 6px; border-radius: 6px;
}
#copyId:hover { color: var(--text); background: var(--panel); }

.find {
  display: flex;
  gap: 8px;
  padding: 12px 16px;
}

.find input {
  flex: 1;
  background: var(--panel);
  border: 1px solid #22303f;
  border-radius: 10px;
  color: var(--text);
  padding: 10px 12px;
  font-size: 14px;
  outline: none;
  letter-spacing: 2px;
}
.find input:focus { border-color: var(--accent); }

.find button {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 10px;
  padding: 0 16px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}
.find button:hover { background: var(--accent2); }
.find button:disabled { opacity: .5; cursor: default; }

.find-error {
  padding: 0 16px 8px;
  font-size: 13px;
  color: var(--danger);
}

.chats-title {
  padding: 14px 16px 6px;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--muted);
}

.chat-list { flex: 1; overflow-y: auto; }

.chat-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  cursor: pointer;
  border-bottom: 1px solid transparent;
}
.chat-item:hover, .chat-item.active { background: var(--panel); }

.chat-item .avatar { width: 42px; height: 42px; font-size: 15px; }

.chat-item .ci-body { min-width: 0; flex: 1; }

.chat-item .ci-name { font-weight: 600; font-size: 15px; }

.chat-item .ci-id { font-size: 12px; color: var(--muted); letter-spacing: 1px; }

.empty-chats { padding: 20px 16px; color: var(--muted); font-size: 13px; text-align: center; }

.sidebar-hint {
  padding: 12px 16px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.5;
  border-top: 1px solid #0b0f14;
}
.sidebar-hint b { color: var(--text); }

/* ---------- main ---------- */
.main {
  display: flex;
  flex-direction: column;
  background: var(--bg);
  min-width: 0;
}

.empty-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 20px;
  text-align: center;
  color: var(--muted);
}

.empty-icon { font-size: 46px; }

.empty-title { font-size: 18px; font-weight: 700; color: var(--text); }

.empty-text { max-width: 360px; font-size: 13px; line-height: 1.6; }

.chat-header {
  padding: 14px 20px;
  border-bottom: 1px solid #0b0f14;
  background: var(--panel);
  display: flex; flex-direction: column; gap: 2px;
}

.chat-name-wrap {
  display: flex; align-items: center; gap: 10px;
}

.e2e-badge {
  background: #1a3a2c;
  color: var(--green);
  border: 1px solid #2c5a44;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: .5px;
  padding: 2px 8px;
  border-radius: 20px;
}

.chat-name { font-size: 16px; font-weight: 700; }

.chat-id { font-size: 13px; color: var(--muted); letter-spacing: 1px; }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.msg {
  max-width: 70%;
  padding: 8px 12px;
  border-radius: 14px;
  font-size: 14px;
  line-height: 1.4;
  word-wrap: break-word;
  white-space: pre-wrap;
  position: relative;
}

.msg.mine {
  align-self: flex-end;
  background: var(--bubble-out);
  border-bottom-right-radius: 4px;
}

.msg.theirs {
  align-self: flex-start;
  background: var(--bubble-in);
  border-bottom-left-radius: 4px;
}

.msg .msg-time {
  display: block;
  font-size: 10px;
  color: rgba(255,255,255,.45);
  margin-top: 4px;
  text-align: right;
}

.msg.sent .msg-time::after { content: " ✓"; color: var(--green); }

.media img, .media video {
  max-width: 300px;
  max-height: 300px;
  border-radius: 8px;
  display: block;
  cursor: pointer;
  background: #000;
}

.media .media-name {
  font-size: 12px;
  color: var(--muted);
  margin-top: 4px;
}

.msg.delivering { opacity: .6; }

/* ---------- composer ---------- */
.composer {
  border-top: 1px solid #0b0f14;
  padding: 12px 16px;
  background: var(--panel);
}

.composer-row {
  display: flex;
  align-items: flex-end;
  gap: 10px;
}

.composer button {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 50%;
  width: 42px; height: 42px;
  font-size: 18px;
  cursor: pointer;
  flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
}
.composer button:hover { background: var(--accent2); }

.composer textarea {
  flex: 1;
  background: var(--bg);
  border: 1px solid #22303f;
  border-radius: 12px;
  color: var(--text);
  padding: 10px 14px;
  font-size: 15px;
  font-family: inherit;
  resize: none;
  outline: none;
  max-height: 120px;
}
.composer textarea:focus { border-color: var(--accent); }

.attach-preview {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-top: 10px;
  background: var(--bg);
  border-radius: 10px;
  padding: 8px 12px;
  font-size: 13px;
}

.attach-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

#attachRemove {
  width: 26px; height: 26px;
  font-size: 12px;
  background: none;
  color: var(--danger);
}
#attachRemove:hover { background: var(--bg); }

/* ---------- loading ---------- */
.loading {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  background: var(--bg);
  color: var(--muted);
  font-size: 14px;
}

.loading-hint {
  max-width: 300px;
  text-align: center;
  font-size: 12px;
  line-height: 1.5;
  color: var(--muted);
}

/* ---------- send progress overlay ---------- */
.progress-overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(6, 12, 20, 0.78);
  backdrop-filter: blur(3px);
  display: flex;
  align-items: center;
  justify-content: center;
}

.progress-card {
  background: var(--panel);
  border: 1px solid #2a3a4e;
  border-radius: 16px;
  padding: 24px 28px;
  width: 280px;
  text-align: center;
  box-shadow: 0 20px 60px rgba(0,0,0,.5);
}

.ring-wrap { position: relative; width: 132px; height: 132px; margin: 0 auto 14px; }

.ring { transform: rotate(-90deg); width: 132px; height: 132px; }

.ring-bg {
  fill: none;
  stroke: #22303f;
  stroke-width: 9;
}

.ring-fg {
  fill: none;
  stroke: var(--accent2);
  stroke-width: 9;
  stroke-linecap: round;
  stroke-dasharray: 326.7;
  stroke-dashoffset: 326.7;
  transition: stroke-dashoffset .18s linear;
}

.ring-center {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
}

.pct { font-size: 24px; font-weight: 700; color: var(--text); }

.pstate { font-size: 11px; color: var(--muted); }

.pfile {
  font-size: 13px;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin-bottom: 6px;
}

.peta { font-size: 12px; color: var(--muted); margin-bottom: 14px; }

.pbtn {
  background: #22303f;
  color: var(--text);
  border: none;
  border-radius: 10px;
  padding: 8px 18px;
  font-size: 13px;
  cursor: pointer;
}
.pbtn:hover { background: #2b3a4d; }

.spinner {
  width: 44px; height: 44px;
  border: 4px solid var(--panel);
  border-top-color: var(--accent2);
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- scrollbar ---------- */
::-webkit-scrollbar { width: 8px; }
::-webkit-scrollbar-thumb { background: #2b3a4d; border-radius: 4px; }

/* ---------- AUTH ---------- */
.auth-screen {
  position: fixed; inset: 0;
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
  background: radial-gradient(1200px 800px at 20% -10%, #1e2c3a 0%, var(--bg) 55%);
  overflow-y: auto;
}
.auth-card {
  width: 100%; max-width: 360px;
  background: var(--panel);
  border: 1px solid #2a3a4e;
  border-radius: 18px;
  padding: 26px 24px;
  box-shadow: 0 24px 70px rgba(0,0,0,.5);
}
.auth-logo { font-size: 40px; text-align: center; }
.auth-title { font-size: 22px; font-weight: 800; text-align: center; color: var(--text); }
.auth-sub { font-size: 12px; color: var(--muted); text-align: center; margin: 4px 0 18px; }
.auth-tabs {
  display: flex;
  background: var(--bg);
  border-radius: 10px;
  padding: 4px;
  margin-bottom: 16px;
}
.auth-tab {
  flex: 1; border: none; background: none; color: var(--muted);
  padding: 8px 0; font-size: 14px; font-weight: 600; border-radius: 8px; cursor: pointer;
}
.auth-tab.active { background: var(--accent); color: #fff; }
.auth-error {
  background: rgba(229,86,86,.12);
  border: 1px solid rgba(229,86,86,.4);
  color: #ff9a9a;
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 13px;
  margin-bottom: 14px;
}
#loginForm, #regForm { display: flex; flex-direction: column; gap: 10px; }
#loginForm.hidden, #regForm.hidden { display: none; }
#loginForm input, #regForm input {
  background: var(--bg);
  border: 1px solid #22303f;
  border-radius: 10px;
  color: var(--text);
  padding: 12px 14px;
  font-size: 15px;
  outline: none;
}
#loginForm input:focus, #regForm input:focus { border-color: var(--accent); }
.reg-user-wrap { position: relative; }
.reg-at {
  position: absolute; left: 14px; top: 50%; transform: translateY(-50%);
  color: var(--muted); font-weight: 700;
}
.reg-user-wrap input { width: 100%; padding-left: 32px; }
.reg-check { position: absolute; right: 12px; top: 50%; transform: translateY(-50%); font-size: 12px; color: var(--danger); }
.reg-check.ok { color: var(--green); }
.reg-avatar { display: flex; align-items: center; gap: 10px; }
.reg-avatar-btn {
  background: var(--bg); border: 1px dashed #2a3a4e; color: var(--muted);
  border-radius: 10px; padding: 10px 14px; font-size: 13px; cursor: pointer; flex: 1;
}
.reg-avatar-btn:hover { color: var(--text); border-color: var(--accent); }
.reg-avatar-preview { width: 52px; height: 52px; border-radius: 50%; object-fit: cover; border: 2px solid var(--accent); }
.auth-btn {
  background: var(--accent); color: #fff; border: none;
  border-radius: 10px; padding: 12px; font-size: 15px; font-weight: 700; cursor: pointer;
}
.auth-btn:hover { background: var(--accent2); }
.auth-btn:disabled { opacity: .45; cursor: default; }
.auth-hint { margin-top: 14px; font-size: 11px; color: var(--muted); text-align: center; line-height: 1.5; }

/* ---------- profile ---------- */
.avatar-btn {
  border: none; cursor: pointer; overflow: hidden; padding: 0;
  font-size: 18px; color: #fff;
}
.avatar img, .avatar-btn img, .avatar-sm img, .msg-avatar img { width: 100%; height: 100%; object-fit: cover; border-radius: 50%; }
.profile-user { font-size: 12px; color: var(--accent2); }
.avatar-sm { width: 40px; height: 40px; font-size: 15px; overflow: hidden; }
.icon-btn {
  background: none; border: none; color: var(--muted);
  font-size: 18px; cursor: pointer; padding: 6px 8px; border-radius: 8px;
}
.icon-btn:hover { color: var(--text); background: var(--panel); }
.profile-id { display: flex; align-items: center; justify-content: center; gap: 8px; padding: 6px 0 2px; font-size: 20px; font-weight: 700; letter-spacing: 2px; color: var(--accent2); }

/* ---------- chat header ---------- */
.chat-header-row { display: flex; align-items: center; gap: 10px; }
.back-btn, .menu-btn { display: none; }

/* ---------- messages ---------- */
.msg-row { display: flex; align-items: flex-end; gap: 8px; }
.msg-avatar {
  width: 30px; height: 30px; border-radius: 50%;
  background: linear-gradient(135deg, var(--accent2), var(--accent));
  display: flex; align-items: center; justify-content: center;
  font-size: 11px; font-weight: 700; color: #fff; flex-shrink: 0; overflow: hidden;
}
.msg-body { margin-top: 2px; }
.msg .attachment { margin-bottom: 4px; }
.attach-media {
  max-width: 280px; max-height: 280px;
  border-radius: 10px; display: block; cursor: pointer; background: #000;
}
.attach-audio { max-width: 240px; width: 100%; }
.file-card {
  display: flex; align-items: center; gap: 10px;
  background: rgba(255,255,255,.05);
  border: 1px solid rgba(255,255,255,.08);
  border-radius: 10px;
  padding: 8px 10px;
  min-width: 180px; max-width: 260px;
}
.file-icon { font-size: 26px; }
.file-info { flex: 1; min-width: 0; }
.file-info .media-name { font-size: 12px; color: var(--text); word-break: break-all; }
.file-size { font-size: 11px; color: var(--muted); }
.file-dl {
  color: var(--accent2); text-decoration: none; font-size: 18px;
  background: rgba(255,255,255,.08); border-radius: 8px; padding: 4px 8px;
}
.file-dl:hover { background: rgba(255,255,255,.16); }
.file-loading { display: inline-block; font-size: 12px; color: var(--muted); padding: 8px 0; }

/* ---------- composer ---------- */
.rec-time { font-size: 13px; color: var(--danger); font-weight: 700; letter-spacing: .5px; padding: 0 4px; }
#micBtn.recording { background: var(--danger); animation: pulse 1s infinite; }
@keyframes pulse { 50% { transform: scale(1.12); } }

/* ---------- scrollbar ---------- */
::-webkit-scrollbar { width: 8px; }
::-webkit-scrollbar-thumb { background: #2b3a4d; border-radius: 4px; }

/* ---------- mobile ---------- */
@media (max-width: 720px) {
  #app { grid-template-columns: 1fr; display: block; }
  .sidebar {
    display: none;
    position: fixed; top: 0; left: 0; bottom: 0;
    z-index: 20; width: 85%; max-width: 340px;
  }
  .sidebar.open { display: flex; box-shadow: 20px 0 60px rgba(0,0,0,.5); }
  .main { height: 100vh; display: flex; flex-direction: column; }
  .back-btn { display: inline-flex; }
  .menu-btn { display: inline-flex; }
  body.chat-open .menu-btn { display: none; }
  .messages { padding: 12px 10px; }
  .msg { max-width: 82%; }
  .msg-row .msg { max-width: calc(68% + 8px); }
  .composer { padding: 8px 8px calc(8px + env(safe-area-inset-bottom)); }
  .attach-media { max-width: 210px; max-height: 210px; }
}