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

:root {
  --bg: #0f1117;
  --surface: #1a1d27;
  --surface2: #222535;
  --border: #2e3148;
  --accent: #6c63ff;
  --accent-hover: #7c74ff;
  --text: #e8eaf0;
  --text-muted: #8b8fa8;
  --danger: #e05252;
  --success: #4caf7d;
  --warning: #f0a930;
  --own-bubble: #2d2b4e;
  --other-bubble: #1e2030;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 4px 24px rgba(0,0,0,0.4);
  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
}

html, body {
  height: 100%;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.5;
}

/* ── Screens ── */
.screen {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
}

.screen[hidden] { display: none !important; }

/* ── Card ── */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 2.5rem 2rem;
  width: 100%;
  max-width: 420px;
  box-shadow: var(--shadow);
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.25rem;
}

.logo {
  font-size: 1.6rem;
  font-weight: 700;
  letter-spacing: -0.03em;
  color: var(--accent);
}

.tagline {
  color: var(--text-muted);
  font-size: 0.875rem;
  margin-bottom: 1.75rem;
}

.welcome {
  color: var(--text-muted);
  font-size: 0.875rem;
  margin-bottom: 1.5rem;
}

.welcome span { color: var(--text); font-weight: 500; }

/* ── Forms ── */
form { display: flex; flex-direction: column; gap: 0.5rem; }

label {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-top: 0.75rem;
}

label:first-child { margin-top: 0; }

input[type="text"],
input[type="password"] {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 0.95rem;
  padding: 0.65rem 0.875rem;
  width: 100%;
  outline: none;
  transition: border-color 0.15s;
}

input:focus {
  border-color: var(--accent);
}

button[type="submit"],
.btn-primary {
  margin-top: 1rem;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 0.95rem;
  font-weight: 600;
  padding: 0.7rem 1.25rem;
  cursor: pointer;
  transition: background 0.15s, opacity 0.15s;
}

button[type="submit"]:hover { background: var(--accent-hover); }
button[type="submit"]:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-ghost {
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  font-size: 0.8rem;
  padding: 0.35rem 0.75rem;
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s;
}

.btn-ghost:hover { color: var(--text); border-color: var(--text-muted); }

.hint {
  font-size: 0.78rem;
  color: var(--text-muted);
  margin-top: 0.25rem;
}

.error-msg {
  font-size: 0.82rem;
  color: var(--danger);
  margin-top: 0.5rem;
}

/* ── Chat layout ── */
.chat-layout {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  width: 100%;
  max-width: 860px;
  margin: 0 auto;
}

#screen-chat {
  align-items: stretch;
  padding: 0;
}

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.875rem 1.25rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  gap: 0.75rem;
}

.chat-header-left {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  min-width: 0;
}

.chat-header-right {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-shrink: 0;
}

.room-name {
  font-weight: 700;
  font-size: 1rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.badge-encrypted {
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--success);
  background: rgba(76,175,125,0.12);
  border: 1px solid rgba(76,175,125,0.3);
  border-radius: 100px;
  padding: 0.15rem 0.55rem;
  white-space: nowrap;
  flex-shrink: 0;
}

/* ── Connection status ── */
.conn-status {
  font-size: 0.75rem;
  font-weight: 600;
  border-radius: 100px;
  padding: 0.2rem 0.6rem;
}

.conn-connected { background: rgba(76,175,125,0.15); color: var(--success); }
.conn-connecting { background: rgba(240,169,48,0.15); color: var(--warning); }
.conn-disconnected { background: rgba(224,82,82,0.12); color: var(--danger); }

/* ── Message list ── */
.message-list {
  flex: 1;
  overflow-y: auto;
  padding: 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  scroll-behavior: smooth;
}

.messages-empty {
  text-align: center;
  color: var(--text-muted);
  font-size: 0.875rem;
  margin: auto;
}

/* ── Message bubbles ── */
.message-row {
  display: flex;
  flex-direction: column;
  max-width: 72%;
}

.message-row.own {
  align-self: flex-end;
  align-items: flex-end;
}

.message-row.other {
  align-self: flex-start;
  align-items: flex-start;
}

.message-meta {
  font-size: 0.72rem;
  color: var(--text-muted);
  margin-bottom: 0.2rem;
  padding: 0 0.25rem;
}

.message-bubble {
  padding: 0.55rem 0.875rem;
  border-radius: var(--radius);
  font-size: 0.9rem;
  line-height: 1.5;
  word-break: break-word;
  white-space: pre-wrap;
}

.message-row.own .message-bubble {
  background: var(--own-bubble);
  border-bottom-right-radius: 4px;
}

.message-row.other .message-bubble {
  background: var(--other-bubble);
  border-bottom-left-radius: 4px;
  border: 1px solid var(--border);
}

.message-bubble.decrypt-error {
  color: var(--danger);
  font-style: italic;
  font-size: 0.82rem;
}

.message-time {
  font-size: 0.68rem;
  color: var(--text-muted);
  margin-top: 0.2rem;
  padding: 0 0.25rem;
}

/* ── System messages ── */
.message-row.system {
  align-self: center;
  align-items: center;
  max-width: 100%;
}

.message-row.system .message-bubble {
  background: none;
  border: 1px solid var(--border);
  color: var(--text-muted);
  font-size: 0.78rem;
  padding: 0.25rem 0.75rem;
  border-radius: 100px;
}

/* ── Reconnect banner ── */
.reconnect-banner {
  background: rgba(240,169,48,0.1);
  border-bottom: 1px solid rgba(240,169,48,0.3);
  color: var(--warning);
  font-size: 0.8rem;
  text-align: center;
  padding: 0.4rem;
  flex-shrink: 0;
}

/* ── Chat footer ── */
.chat-footer {
  display: flex;
  gap: 0.75rem;
  padding: 0.875rem 1.25rem;
  background: var(--surface);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
  align-items: flex-end;
}

.message-input {
  flex: 1;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-family: var(--font);
  font-size: 0.925rem;
  line-height: 1.5;
  padding: 0.6rem 0.875rem;
  resize: none;
  outline: none;
  max-height: 8rem;
  overflow-y: auto;
  transition: border-color 0.15s;
}

.message-input:focus { border-color: var(--accent); }

.btn-send {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  font-weight: 600;
  padding: 0.6rem 1.1rem;
  cursor: pointer;
  transition: background 0.15s, opacity 0.15s;
  flex-shrink: 0;
  align-self: flex-end;
}

.btn-send:hover { background: var(--accent-hover); }
.btn-send:disabled { opacity: 0.4; cursor: not-allowed; }

/* ── Scrollbar ── */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

/* ── Mobile ── */
@media (max-width: 600px) {
  .card { padding: 2rem 1.25rem; }
  .message-row { max-width: 88%; }
  .chat-header { padding: 0.75rem 1rem; }
  .message-list { padding: 1rem; }
  .chat-footer { padding: 0.75rem 1rem; }
}
