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

:root {
  --bg: #0e0e0e;
  --surface: #1a1a1a;
  --surface2: #242424;
  --border: #2e2e2e;
  --text: #e8e8e8;
  --text-muted: #888;
  --accent: #6c5ce7;
  --accent-hover: #7f70f0;
  --danger: #e74c3c;
  --success: #2ecc71;
  --radius: 10px;
}

html {
  height: 100%;
  height: 100dvh;
}

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

.screen {
  display: none;
  height: 100%;
  flex-direction: column;
}
.screen.active {
  display: flex;
}

/* ---------- LOBBY ---------- */
#lobby {
  padding: 20px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.lobby-header {
  text-align: center;
  padding: 40px 0 20px;
}
.lobby-header h1 {
  font-size: 1.8rem;
  font-weight: 700;
  letter-spacing: -0.5px;
}
.lobby-header .dot {
  color: var(--accent);
  margin-left: 2px;
}
.subtitle {
  color: var(--text-muted);
  font-size: 0.9rem;
  margin-top: 6px;
}

.lobby-form {
  max-width: 400px;
  margin: 20px auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

input[type="text"] {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  font-size: 16px;
  color: var(--text);
  outline: none;
  width: 100%;
  transition: border-color 0.15s;
}
input[type="text"]:focus {
  border-color: var(--accent);
}

.room-join-row {
  display: flex;
  gap: 8px;
}
.room-join-row input {
  flex: 1;
}

button {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  padding: 12px 20px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
  white-space: nowrap;
}
button:active {
  transform: scale(0.97);
}
button:hover {
  background: var(--accent-hover);
}

.error {
  color: var(--danger);
  font-size: 0.85rem;
  min-height: 1.2em;
}

.rooms-section {
  max-width: 400px;
  margin: 10px auto 0;
}
.rooms-section h2 {
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--text-muted);
  margin-bottom: 10px;
}

.room-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.empty-state {
  color: var(--text-muted);
  font-size: 0.9rem;
  text-align: center;
  padding: 20px 0;
}

.room-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  cursor: pointer;
  transition: border-color 0.15s;
}
.room-card:hover {
  border-color: var(--accent);
}
.room-card .room-name {
  font-weight: 600;
  font-size: 0.95rem;
}
.room-card .room-meta {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 0.8rem;
  color: var(--text-muted);
}
.room-card .locked-badge {
  color: var(--danger);
  font-weight: 600;
}

/* ---------- CHAT ROOM ---------- */
#chatroom {
  position: relative;
  overflow: hidden;
}

.chat-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  min-height: 54px;
  flex-shrink: 0;
  z-index: 5;
}
.chat-header-info {
  flex: 1;
  min-width: 0;
}
.chat-header-info h2 {
  font-size: 1rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
#user-count {
  font-size: 0.75rem;
  color: var(--text-muted);
  cursor: pointer;
}
#user-count:hover {
  text-decoration: underline;
}

.icon-btn {
  background: transparent;
  color: var(--text);
  padding: 8px;
  font-size: 1.3rem;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 40px;
  min-height: 40px;
}
.icon-btn:hover {
  background: var(--surface2);
}

/* Messages area */
.messages {
  flex: 1;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 10px 14px;
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-height: 0;
}

/* Bubbles */
.msg {
  max-width: 80%;
  padding: 7px 11px;
  border-radius: 14px;
  font-size: 0.93rem;
  line-height: 1.4;
  word-wrap: break-word;
  overflow-wrap: break-word;
}

.msg.mine {
  align-self: flex-end;
  background: var(--accent);
  color: #fff;
  border-bottom-right-radius: 4px;
}
.msg.theirs {
  align-self: flex-start;
  background: var(--surface2);
  border-bottom-left-radius: 4px;
}

/* Group spacing: add gap before a new speaker */
.msg.group-start {
  margin-top: 10px;
}

.msg .msg-header {
  display: flex;
  align-items: baseline;
  gap: 6px;
  margin-bottom: 2px;
}

.msg .msg-user {
  font-size: 0.75rem;
  font-weight: 700;
}

.msg .msg-time {
  font-size: 0.65rem;
  opacity: 0.5;
}

.msg .msg-text {
  white-space: pre-wrap;
}

/* System messages */
.msg.system {
  align-self: center;
  background: transparent;
  color: var(--text-muted);
  font-size: 0.78rem;
  font-style: italic;
  padding: 6px 0;
  max-width: 100%;
  text-align: center;
  margin-top: 6px;
}

/* Users drawer */
.users-drawer {
  position: absolute;
  top: 54px;
  right: 0;
  bottom: 0;
  width: 220px;
  background: var(--surface);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  z-index: 10;
  transition: transform 0.2s;
}
.users-drawer.hidden {
  transform: translateX(100%);
  pointer-events: none;
}
.drawer-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  font-size: 0.9rem;
}
.users-drawer ul {
  list-style: none;
  overflow-y: auto;
  padding: 8px 14px;
  flex: 1;
}
.users-drawer li {
  padding: 6px 0;
  font-size: 0.9rem;
  border-bottom: 1px solid var(--border);
}
.users-drawer li:last-child {
  border-bottom: none;
}

/* Typing indicator */
.typing-bar {
  flex-shrink: 0;
  padding: 4px 14px;
  font-size: 0.78rem;
  color: var(--text-muted);
  background: var(--surface);
  border-top: 1px solid var(--border);
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.typing-bar.hidden {
  display: none;
}
.typing-bar .typing-dots {
  display: inline-block;
  letter-spacing: 2px;
  animation: blink 1.2s infinite steps(1);
}
@keyframes blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.3; }
}

/* Input bar */
.chat-input-bar {
  display: flex;
  gap: 8px;
  padding: 8px 10px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
  z-index: 5;
}
.chat-input-bar input {
  flex: 1;
  border-radius: 20px;
  padding: 10px 16px;
}
.chat-input-bar button {
  border-radius: 20px;
  padding: 10px 18px;
}

/* Mobile */
@media (max-width: 480px) {
  .lobby-header { padding-top: 30px; }
  .lobby-header h1 { font-size: 1.5rem; }
  .users-drawer { width: 100%; }
}

/* Safe area for notch phones */
@supports (padding: env(safe-area-inset-bottom)) {
  .chat-input-bar {
    padding-bottom: calc(8px + env(safe-area-inset-bottom));
  }
  #lobby {
    padding-bottom: calc(20px + env(safe-area-inset-bottom));
  }
}
