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

:root {
  --bg:        #1a1a2e;
  --surface:   #16213e;
  --panel:     #0f3460;
  --accent:    #e94560;
  --accent2:   #c73652;
  --text:      #e0e0e0;
  --muted:     #888;
  --border:    #1a1a2e;
  --green:     #4caf50;
  --green-bg:  #1a6b3a;
  --red:       #e94560;
  --red-dim:   #7a1f30;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  overflow: hidden;
}

.hidden { display: none !important; }

/* ── Screens ── */
.screen { display: none; width: 100vw; height: 100vh; }
.screen.active { display: flex; }

/* ── Shared card ── */
.card {
  background: var(--surface);
  border: 1px solid var(--panel);
  border-radius: 12px;
  padding: 36px 44px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  width: min(400px, calc(100% - 32px));
  box-shadow: 0 8px 32px rgba(0,0,0,0.4);
  margin: auto;
}

.card h1 { font-size: 1.5rem; color: var(--accent); margin-bottom: 4px; }

.muted  { color: var(--muted); font-size: 0.88rem; text-align: center; }
.small  { font-size: 0.76rem; }
.error-msg { color: #ff6b6b; font-size: 0.82rem; min-height: 1.1em; text-align: center; width: 100%; }

/* ── Shared inputs ── */
input[type=text], input[type=password] {
  width: 100%;
  padding: 10px 14px;
  border-radius: 8px;
  border: 1px solid var(--panel);
  background: var(--panel);
  color: var(--text);
  font-size: 0.95rem;
  outline: none;
  transition: border-color 0.2s;
}
input:focus { border-color: var(--accent); }

/* ── Shared buttons ── */
button {
  width: 100%;
  padding: 10px;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 0.95rem;
  cursor: pointer;
  transition: background 0.2s;
}
button:hover:not(:disabled) { background: var(--accent2); }
button:disabled { opacity: 0.5; cursor: default; }

.btn-secondary {
  background: var(--panel);
  color: var(--text);
}
.btn-secondary:hover:not(:disabled) { background: #1a3a70; }

/* ── Screen 1: Auth ── */
#auth-screen { align-items: center; justify-content: center; }

/* Tab bar */
.tab-bar {
  display: flex;
  width: 100%;
  gap: 0;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid var(--panel);
}
.tab-btn {
  flex: 1;
  border-radius: 0;
  background: var(--panel);
  color: var(--muted);
  font-size: 0.9rem;
  padding: 8px;
  border: none;
}
.tab-btn.active  { background: var(--accent); color: white; }
.tab-btn:hover:not(.active):not(:disabled) { background: #1a3a70; color: var(--text); }

/* Tab panels */
.tab-panel { display: none; flex-direction: column; gap: 12px; width: 100%; }
.tab-panel.active { display: flex; }

/* ── Screen 2: Lobby ── */
#lobby-screen { flex-direction: column; }

#lobby-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 28px;
  background: var(--panel);
  font-size: 1.05rem;
  font-weight: 600;
  color: var(--accent);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

#lobby-header-right {
  display: flex;
  align-items: center;
  gap: 12px;
}

#lobby-user-badge {
  font-size: 0.85rem;
  color: var(--muted);
  font-weight: 400;
}

.logout-btn {
  width: auto;
  padding: 5px 14px;
  font-size: 0.8rem;
  border-radius: 6px;
  background: transparent;
  border: 1px solid var(--muted);
  color: var(--muted);
}
.logout-btn:hover { border-color: var(--accent); color: var(--accent); background: transparent; }

#lobby-body {
  display: flex;
  flex: 1;
  overflow: hidden;
}

/* Room list */
#room-list-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 28px;
  overflow-y: auto;
  gap: 16px;
}

#room-list-panel h2,
#create-room-panel h2 {
  font-size: 0.78rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
}

#room-list { list-style: none; display: flex; flex-direction: column; gap: 10px; }

.room-empty { color: var(--muted); font-size: 0.9rem; padding: 8px 0; }

.room-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--surface);
  border: 1px solid var(--panel);
  border-radius: 10px;
  padding: 14px 18px;
  gap: 12px;
  transition: border-color 0.2s;
}
.room-item:hover { border-color: var(--accent); }

.room-info { display: flex; flex-direction: column; gap: 3px; flex: 1; }
.room-name  { font-size: 1rem; font-weight: 600; }
.room-count { font-size: 0.76rem; color: var(--muted); }

.room-actions { display: flex; gap: 8px; align-items: center; flex-shrink: 0; }

.join-room-btn {
  width: auto;
  padding: 7px 16px;
  border-radius: 6px;
  font-size: 0.85rem;
}

/* Banned users panel */
#banned-panel {
  width: 300px;
  flex-shrink: 0;
  background: var(--surface);
  border-left: 1px solid var(--panel);
  padding: 28px 22px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  overflow-y: auto;
}
#banned-panel h2 {
  font-size: 0.78rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
}
#banned-list { list-style: none; display: flex; flex-direction: column; gap: 8px; }

.banned-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--panel);
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 0.88rem;
  gap: 8px;
}

.unban-btn {
  width: auto;
  padding: 4px 10px;
  font-size: 0.75rem;
  border-radius: 5px;
  background: var(--green-bg);
  color: var(--green);
  border: 1px solid var(--green);
  flex-shrink: 0;
}
.unban-btn:hover:not(:disabled) { background: var(--green); color: white; }

.delete-room-btn {
  width: auto;
  padding: 6px 12px;
  border-radius: 6px;
  font-size: 0.78rem;
  background: var(--red-dim);
  color: #ff9999;
  border: 1px solid var(--red);
}
.delete-room-btn:hover:not(:disabled) { background: var(--red); color: white; }

/* Create room panel */
#create-room-panel {
  width: 300px;
  flex-shrink: 0;
  background: var(--surface);
  border-left: 1px solid var(--panel);
  padding: 28px 22px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  overflow-y: auto;
}

/* ── Join password modal ── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.65);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.modal-overlay.hidden { display: none; }

.modal-card {
  background: var(--surface);
  border: 1px solid var(--panel);
  border-radius: 12px;
  padding: 30px 34px;
  width: 360px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  box-shadow: 0 12px 40px rgba(0,0,0,0.5);
}
.modal-card h3 { font-size: 1.1rem; color: var(--accent); }

.modal-actions { display: flex; gap: 10px; margin-top: 4px; }
.modal-actions button { flex: 1; }

/* ── Screen 3: Chat ── */
#chat-screen { flex-direction: row; }

#chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  overflow: hidden;
}

#chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  gap: 12px;
}

#chat-room-name { font-size: 1rem; font-weight: 600; color: var(--accent); }

#chat-header-right { display: flex; align-items: center; gap: 10px; flex-shrink: 0; }

#e2ee-badge {
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  background: var(--green-bg);
  color: var(--green);
  padding: 3px 8px;
  border-radius: 4px;
}

#leave-btn {
  width: auto;
  padding: 6px 14px;
  font-size: 0.82rem;
  border-radius: 6px;
  background: transparent;
  border: 1px solid var(--muted);
  color: var(--muted);
}
#leave-btn:hover { border-color: var(--accent); color: var(--accent); background: transparent; }

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

.msg { display: flex; flex-direction: column; max-width: 65%; }
.msg.own   { align-self: flex-end;   align-items: flex-end; }
.msg.other { align-self: flex-start; align-items: flex-start; }

.msg-meta { font-size: 0.72rem; color: var(--muted); margin-bottom: 3px; }
.msg.own .msg-meta { color: #8888aa; }

.msg-bubble {
  padding: 8px 14px;
  border-radius: 16px;
  font-size: 0.95rem;
  line-height: 1.4;
  word-break: break-word;
}
.msg.own   .msg-bubble { background: var(--accent); color: white; border-bottom-right-radius: 4px; }
.msg.other .msg-bubble { background: var(--panel);  color: var(--text); border-bottom-left-radius: 4px; }

.system-notice { align-self: center; font-size: 0.78rem; color: #555; font-style: italic; padding: 4px 0; }

#input-bar {
  display: flex;
  gap: 10px;
  padding: 14px 20px;
  background: var(--panel);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}
#msg-input { flex: 1; border-radius: 24px; width: auto; }
#send-btn  { width: auto; padding: 10px 22px; border-radius: 24px; flex-shrink: 0; }

/* Sidebar */
#sidebar {
  width: 210px;
  background: var(--panel);
  display: flex;
  flex-direction: column;
  border-left: 1px solid var(--border);
  flex-shrink: 0;
}

#sidebar-header {
  padding: 14px 16px;
  font-size: 0.78rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  border-bottom: 1px solid var(--border);
}

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

#user-list li {
  padding: 6px 12px;
  font-size: 0.88rem;
  display: flex;
  align-items: center;
  gap: 6px;
  justify-content: space-between;
}

#user-list li::before {
  content: '';
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--green);
  flex-shrink: 0;
}

.user-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#user-list li.me .user-name { color: var(--accent); font-weight: 600; }

.ban-btn {
  width: auto;
  padding: 3px 8px;
  font-size: 0.7rem;
  border-radius: 4px;
  background: var(--red-dim);
  color: #ff9999;
  border: 1px solid var(--red);
  flex-shrink: 0;
}
.ban-btn:hover:not(:disabled) { background: var(--red); color: white; }

/* Scrollbar */
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--panel); border-radius: 2px; }

/* ── Sidebar toggle (mobile only, hidden on desktop) ── */
#sidebar-toggle {
  display: none;
  align-items: center;
  gap: 6px;
  width: auto;
  padding: 6px 12px;
  font-size: 0.82rem;
  border-radius: 6px;
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--muted);
  flex-shrink: 0;
}

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

/* ── iOS Safari viewport fix ── */
@supports (height: 100dvh) {
  body { height: 100dvh; }
  .screen { height: 100dvh; }
}

/* ── Tablet / mobile (≤ 768px) ── */
@media (max-width: 768px) {
  /* Auth card */
  .card { padding: 28px 24px; }

  /* Lobby header */
  #lobby-header { padding: 12px 16px; }
  #lobby-user-badge { display: none; }

  /* Lobby body: stack panels vertically */
  #lobby-body { flex-direction: column; overflow-y: auto; overflow-x: hidden; }

  #room-list-panel {
    padding: 20px 16px;
    overflow-y: visible;
  }

  #create-room-panel {
    width: 100%;
    border-left: none;
    border-top: 1px solid var(--panel);
    padding: 20px 16px;
    overflow-y: visible;
  }

  #banned-panel {
    width: 100%;
    border-left: none;
    border-top: 1px solid var(--panel);
    padding: 20px 16px;
    overflow-y: visible;
  }

  /* Modal */
  .modal-card {
    width: calc(100% - 32px);
    padding: 24px 20px;
  }

  /* Chat: sidebar becomes a slide-in drawer */
  #sidebar {
    position: fixed;
    top: 0;
    right: 0;
    height: 100%;
    z-index: 50;
    width: 240px;
    transform: translateX(100%);
    transition: transform 0.25s ease;
    border-left: none;
    box-shadow: -4px 0 20px rgba(0,0,0,0.4);
  }
  #sidebar.open { transform: translateX(0); }

  /* Show sidebar toggle button */
  #sidebar-toggle { display: flex; }

  /* Chat header */
  #chat-header { padding: 10px 14px; }
  #e2ee-badge { display: none; }

  /* Messages */
  #messages { padding: 12px 14px; }
  .msg { max-width: 82%; }

  /* Input bar */
  #input-bar { padding: 10px 14px; }
}

/* ── Small mobile (≤ 480px) ── */
@media (max-width: 480px) {
  .card { padding: 24px 16px; }

  #lobby-header { padding: 10px 14px; font-size: 0.95rem; }

  #chat-header-right { gap: 6px; }
  #leave-btn, .logout-btn { padding: 5px 10px; font-size: 0.76rem; }

  .room-item { padding: 12px 14px; }
  .join-room-btn { padding: 6px 12px; }
}
