:root {
  --bg: #0f172a;
  --bg-elev: #1e293b;
  --bg-elev2: #334155;
  --panel: #1e293b;
  --panel-2: #273449;
  --border: #334155;
  --text: #e2e8f0;
  --text-muted: #94a3b8;
  --accent: #6366f1;
  --accent-2: #8b5cf6;
  --accent-grad: linear-gradient(135deg, #6366f1, #8b5cf6);
  --bubble-me: linear-gradient(135deg, #6366f1, #7c3aed);
  --bubble-them: #334155;
  --online: #22c55e;
  --danger: #ef4444;
  --shadow: 0 10px 40px rgba(0, 0, 0, 0.4);
  --radius: 14px;
}

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

html, body { height: 100%; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background:
    radial-gradient(1200px 600px at 80% -10%, rgba(99, 102, 241, 0.18), transparent 60%),
    radial-gradient(900px 500px at -10% 110%, rgba(139, 92, 246, 0.15), transparent 55%),
    var(--bg);
  color: var(--text);
  height: 100vh;
  overflow: hidden;
}

.hidden { display: none !important; }

/* ---- Login ------------------------------------------------------------- */
#login-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  padding: 24px;
}

.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 40px 32px 32px;
  text-align: center;
  box-shadow: var(--shadow);
}

.logo {
  width: 64px;
  height: 64px;
  margin: 0 auto 18px;
  border-radius: 18px;
  display: grid;
  place-items: center;
  font-size: 32px;
  background: var(--accent-grad);
  box-shadow: 0 8px 24px rgba(99, 102, 241, 0.45);
}

.login-card h1 { font-size: 22px; font-weight: 700; letter-spacing: -0.01em; }
.subtitle { color: var(--text-muted); font-size: 14px; margin-top: 6px; line-height: 1.5; }

#login-form { display: flex; flex-direction: column; gap: 12px; margin-top: 24px; }
#login-form input {
  height: 46px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  padding: 0 16px;
  font-size: 15px;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}
#login-form input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.25); }

#login-form button {
  height: 46px;
  border: none;
  border-radius: 12px;
  background: var(--accent-grad);
  color: white;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.1s, box-shadow 0.15s;
}
#login-form button:hover { box-shadow: 0 6px 20px rgba(99, 102, 241, 0.5); }
#login-form button:active { transform: translateY(1px); }

.error { color: var(--danger); font-size: 13px; min-height: 18px; margin-top: 10px; }
.hint { color: var(--text-muted); font-size: 12px; margin-top: 14px; }

/* ---- Chat layout ------------------------------------------------------- */
#chat-screen {
  display: grid;
  grid-template-columns: 320px 1fr;
  height: 100vh;
}

.sidebar {
  background: var(--bg-elev);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.sidebar-header { padding: 18px 18px 14px; border-bottom: 1px solid var(--border); }
.me { display: flex; align-items: center; gap: 12px; }
.me-info { display: flex; flex-direction: column; line-height: 1.2; }
.me-name { font-weight: 600; font-size: 15px; }
.me-status { display: flex; align-items: center; gap: 6px; color: var(--text-muted); font-size: 12px; margin-top: 2px; }

.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }
.dot.online { background: var(--online); box-shadow: 0 0 0 3px rgba(34, 197, 94, 0.18); }
.dot.offline { background: var(--text-muted); }

.avatar {
  width: 40px; height: 40px; border-radius: 12px;
  display: grid; place-items: center;
  font-weight: 600; font-size: 16px; color: white;
  flex-shrink: 0;
}

.sidebar-section-title {
  padding: 14px 18px 8px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  font-weight: 600;
}

.conversation-list, .user-list {
  list-style: none;
  overflow-y: auto;
  padding-bottom: 8px;
}
.conversation-list { max-height: 35vh; }
.user-list { flex: 1; }

.list-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 18px;
  cursor: pointer;
  transition: background 0.12s;
}
.list-item:hover { background: var(--panel-2); }
.list-item.active { background: var(--bg-elev2); }

.list-item .meta { flex: 1; min-width: 0; }
.list-item .name { font-size: 14px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.list-item .preview { font-size: 12px; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 2px; }

.badge {
  background: var(--accent);
  color: white;
  font-size: 11px;
  font-weight: 700;
  min-width: 20px; height: 20px; padding: 0 6px;
  border-radius: 10px;
  display: grid; place-items: center;
}
.badge.zero { display: none; }

.presence { width: 10px; height: 10px; border-radius: 50%; margin-left: auto; }
.presence.online { background: var(--online); }
.presence.offline { background: transparent; border: 2px solid var(--border); }

/* ---- Chat panel -------------------------------------------------------- */
.chat-panel {
  display: grid;
  grid-template-rows: auto 1fr auto;
  position: relative;
  overflow: hidden;
}

.chat-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px 22px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-elev);
}
.chat-header.empty { visibility: hidden; }
.chat-partner-info { display: flex; flex-direction: column; line-height: 1.25; }
.chat-partner-name { font-weight: 600; font-size: 15px; }
.chat-partner-status { font-size: 12px; color: var(--text-muted); display: flex; align-items: center; gap: 6px; margin-top: 2px; }

.messages {
  overflow-y: auto;
  padding: 22px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  background:
    radial-gradient(700px 400px at 90% 0%, rgba(99, 102, 241, 0.07), transparent 60%),
    var(--bg);
}

.msg-row { display: flex; gap: 10px; align-items: flex-end; }
.msg-row.me { justify-content: flex-end; }

.bubble {
  max-width: 70%;
  padding: 10px 14px;
  border-radius: 16px;
  font-size: 14.5px;
  line-height: 1.4;
  word-wrap: break-word;
  white-space: pre-wrap;
}
.bubble.them { background: var(--bubble-them); border-bottom-left-radius: 4px; }
.bubble.me { background: var(--bubble-me); color: white; border-bottom-right-radius: 4px; }

.msg-meta { font-size: 11px; color: var(--text-muted); margin-top: 3px; padding: 0 6px; }
.msg-row.me .msg-meta { text-align: right; }

.composer {
  display: flex;
  gap: 10px;
  padding: 16px 22px;
  border-top: 1px solid var(--border);
  background: var(--bg-elev);
}
.composer input {
  flex: 1;
  height: 44px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  padding: 0 16px;
  font-size: 14.5px;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.composer input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.2); }
.composer input:disabled { opacity: 0.5; }
.composer button {
  height: 44px;
  padding: 0 22px;
  border: none;
  border-radius: 12px;
  background: var(--accent-grad);
  color: white;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.1s, box-shadow 0.15s;
}
.composer button:hover:not(:disabled) { box-shadow: 0 6px 18px rgba(99, 102, 241, 0.45); }
.composer button:disabled { opacity: 0.45; cursor: not-allowed; }

.empty-state {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  text-align: center;
  pointer-events: none;
  color: var(--text-muted);
}
.empty-state .empty-icon { font-size: 56px; margin-bottom: 14px; opacity: 0.8; }
.empty-state h2 { font-weight: 600; font-size: 18px; color: var(--text); margin-bottom: 6px; }
.empty-state p { font-size: 14px; }

/* ---- Scrollbar --------------------------------------------------------- */
.messages::-webkit-scrollbar,
.conversation-list::-webkit-scrollbar,
.user-list::-webkit-scrollbar { width: 8px; }
.messages::-webkit-scrollbar-thumb,
.conversation-list::-webkit-scrollbar-thumb,
.user-list::-webkit-scrollbar-thumb { background: var(--border); border-radius: 8px; }

/* ---- Date divider ------------------------------------------------------ */
.day-divider {
  align-self: center;
  font-size: 11px;
  color: var(--text-muted);
  background: var(--bg-elev2);
  padding: 4px 12px;
  border-radius: 10px;
  margin: 6px 0;
}

/* ---- Responsive -------------------------------------------------------- */
@media (max-width: 720px) {
  #chat-screen { grid-template-columns: 1fr; }
  .sidebar { display: none; }
  .sidebar.show { display: flex; position: absolute; inset: 0; z-index: 5; }
}