:root {
  --bg: #0f0f1a;
  --bg2: #1a1a2e;
  --bg3: #16213e;
  --accent: #e94560;
  --accent2: #0f3460;
  --text: #e0e0e0;
  --text2: #888;
  --border: #2a2a4a;
  --radius: 10px;
  --sidebar-w: 240px;
}

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

html, body, #app { height: 100%; overflow: hidden; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
}

.screen { height: 100%; }
.hidden { display: none !important; }

/* ── Login ─────────────────────────────────────── */
#login-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  background: var(--bg);
}

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

.app-name {
  font-size: 2.2em;
  font-weight: 700;
  letter-spacing: 0.05em;
  color: var(--accent);
  margin-bottom: 8px;
}

.login-sub { color: var(--text2); font-size: 0.9em; margin-bottom: 24px; }

#login-form { display: flex; flex-direction: column; gap: 12px; }

input[type="email"], input[type="text"] {
  width: 100%;
  padding: 12px 14px;
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 1em;
  outline: none;
}
input:focus { border-color: var(--accent); }

button {
  padding: 12px 20px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: 1em;
  cursor: pointer;
  font-weight: 600;
  transition: opacity 0.15s;
}
button:hover { opacity: 0.85; }
button.ghost { background: transparent; color: var(--text2); border: 1px solid var(--border); }
button.ghost:hover { color: var(--text); }
button.danger { background: #c0392b; }

.msg { margin-top: 12px; font-size: 0.9em; padding: 8px 12px; border-radius: 6px; }
.msg.ok { background: #1a3a1a; color: #6fcf6f; }
.msg.err { background: #3a1a1a; color: #cf6f6f; }

/* ── Main layout ────────────────────────────────── */
#main-screen { display: flex; height: 100%; }

/* ── Sidebar ────────────────────────────────────── */
#sidebar {
  width: var(--sidebar-w);
  min-width: var(--sidebar-w);
  background: var(--bg2);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  height: 100%;
  transition: transform 0.2s;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 16px 14px;
  border-bottom: 1px solid var(--border);
}

.app-name-small {
  font-size: 1.1em;
  font-weight: 700;
  color: var(--accent);
  letter-spacing: 0.05em;
}

.icon-btn {
  background: transparent;
  border: none;
  color: var(--text2);
  font-size: 1.2em;
  padding: 4px 8px;
  cursor: pointer;
  border-radius: 6px;
}
.icon-btn:hover { background: var(--border); color: var(--text); opacity: 1; }

#room-list { flex: 1; overflow-y: auto; padding: 8px 0; }

.room-item {
  display: block;
  padding: 10px 16px;
  color: var(--text2);
  text-decoration: none;
  border-radius: 6px;
  margin: 2px 8px;
  font-size: 0.95em;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.room-item:hover { background: var(--border); color: var(--text); }
.room-item.active { background: var(--accent2); color: var(--text); font-weight: 600; }
.room-item::before { content: '# '; opacity: 0.5; }

.sidebar-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-top: 1px solid var(--border);
}

.user-name { font-size: 0.88em; color: var(--text2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ── Chat area ──────────────────────────────────── */
#chat-area { flex: 1; display: flex; flex-direction: column; min-width: 0; height: 100%; }

#welcome-view {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text2);
}

#room-view { display: flex; flex-direction: column; height: 100%; }

.room-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--bg2);
}
.room-header h2 { font-size: 1em; font-weight: 600; color: var(--text); }
.room-header h2::before { content: '# '; color: var(--text2); }

#messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 20px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.msg-group { margin-bottom: 12px; }

.msg-row {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  padding: 2px 0;
}

.avatar {
  width: 34px;
  height: 34px;
  min-width: 34px;
  border-radius: 50%;
  background: var(--accent2);
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.85em;
  font-weight: 700;
  text-transform: uppercase;
}

.msg-meta {
  display: flex;
  align-items: baseline;
  gap: 8px;
  margin-bottom: 2px;
}
.msg-sender { font-weight: 600; font-size: 0.9em; }
.msg-time { color: var(--text2); font-size: 0.75em; }

.msg-content { font-size: 0.95em; line-height: 1.5; word-break: break-word; }
.msg-content a { color: #6aabff; }

.msg-body { flex: 1; min-width: 0; }

/* Continued messages (same sender) */
.msg-row.continued .avatar { visibility: hidden; }
.msg-row.continued .msg-meta { display: none; }

/* ── Link preview card ──────────────────────────── */
.link-preview {
  display: flex;
  gap: 12px;
  margin-top: 8px;
  background: var(--bg3);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  border-radius: 8px;
  overflow: hidden;
  max-width: 440px;
  text-decoration: none;
  color: var(--text);
}
.preview-img { width: 80px; min-width: 80px; height: 80px; object-fit: cover; }
.preview-text { padding: 10px 12px; min-width: 0; }
.preview-title { font-weight: 600; font-size: 0.9em; margin-bottom: 4px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.preview-desc { font-size: 0.8em; color: var(--text2); display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }

/* ── YouTube embed ──────────────────────────────── */
.yt-embed {
  margin-top: 8px;
  max-width: 440px;
  aspect-ratio: 16/9;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid var(--border);
}
.yt-embed iframe { width: 100%; height: 100%; border: none; }

/* ── Message input ──────────────────────────────── */
#message-form-wrap {
  padding: 12px 16px;
  padding-bottom: max(12px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  background: var(--bg2);
}
#message-form { display: flex; gap: 10px; }
#message-input {
  flex: 1;
  padding: 12px 14px;
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: 24px;
  color: var(--text);
  font-size: 0.95em;
  outline: none;
}
#message-input:focus { border-color: var(--accent); }
#message-form button {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  padding: 0;
  font-size: 1.1em;
  flex-shrink: 0;
}

/* ── Modals ─────────────────────────────────────── */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 20px;
}

.modal-box {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px 24px;
  width: 100%;
  max-width: 400px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.modal-box h3 { font-size: 1.1em; color: var(--accent); }
.modal-box h4 { font-size: 0.95em; color: var(--text2); }
.modal-box label { display: flex; flex-direction: column; gap: 6px; font-size: 0.88em; color: var(--text2); }
.modal-box hr { border: none; border-top: 1px solid var(--border); }

#users-list { display: flex; flex-direction: column; gap: 8px; margin-top: 4px; }
.user-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--bg3);
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 0.88em;
}
.user-row-info { display: flex; flex-direction: column; gap: 2px; }
.user-row-email { color: var(--text2); font-size: 0.85em; }
.user-row-badges { display: flex; gap: 6px; }
.badge { background: var(--accent2); color: var(--text); font-size: 0.75em; padding: 2px 8px; border-radius: 12px; }
.badge.admin { background: var(--accent); }

/* ── Mobile ─────────────────────────────────────── */
.mobile-only { display: none; }

@media (max-width: 640px) {
  .mobile-only { display: flex; }

  #sidebar {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    z-index: 50;
    transform: translateX(-100%);
  }
  #sidebar.open { transform: translateX(0); box-shadow: 4px 0 20px rgba(0,0,0,0.5); }

  #sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.4);
    z-index: 49;
  }
  #sidebar-overlay.visible { display: block; }
}

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