* {
  box-sizing: border-box;
}

:root {
  --bg: #0f0f12;
  --surface: #1a1a20;
  --border: #2a2a32;
  --text: #e8e8ec;
  --text-muted: #888;
  --accent: #6c5ce7;
  --accent-hover: #7d6ef7;
  --danger: #e74c3c;
  --radius: 10px;
  --font: "DM Sans", "Segoe UI", system-ui, sans-serif;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.5;
}

/* Room page: no document scroll so input stays fixed at bottom */
body:has(.page.room) {
  overflow: hidden;
  height: 100vh;
  height: 100dvh;
}

.page {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.page.room {
  height: 100vh;
  min-height: 100vh;
  height: 100dvh;
  min-height: 100dvh;
  overflow: hidden;
  flex-direction: row;
  align-items: stretch;
  justify-content: stretch;
  padding: 0;
}

/* ----- Home ----- */
.home header {
  text-align: center;
  margin-bottom: 40px;
}

.home h1 {
  font-size: 1.75rem;
  font-weight: 700;
  margin: 0 0 8px;
  letter-spacing: -0.02em;
}

.home header p {
  margin: 0;
  color: var(--text-muted);
  font-size: 0.95rem;
}

.btn-destroy {
  margin-top: 12px;
  padding: 8px 16px;
  background: transparent;
  border: 1px solid var(--danger);
  border-radius: 8px;
  color: var(--danger);
  font: inherit;
  font-size: 0.9rem;
  cursor: pointer;
}

.btn-destroy:hover {
  background: rgba(231, 76, 60, 0.15);
}

.room .sidebar-actions {
  margin-top: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.room .sidebar-actions .btn-destroy {
  margin-top: 0;
  width: 100%;
}

.cards {
  display: flex;
  gap: 24px;
  flex-wrap: wrap;
  justify-content: center;
  max-width: 720px;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px;
  min-width: 280px;
  max-width: 340px;
}

.card h2 {
  font-size: 1.1rem;
  font-weight: 600;
  margin: 0 0 20px;
}

.card .hint {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin: -8px 0 16px;
}

.card label {
  display: block;
  margin-bottom: 16px;
  font-size: 0.9rem;
  color: var(--text-muted);
}

.card input {
  display: block;
  width: 100%;
  margin-top: 6px;
  padding: 12px 14px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font: inherit;
}

.card input:focus {
  outline: none;
  border-color: var(--accent);
}

.card button {
  width: 100%;
  padding: 12px 16px;
  background: var(--accent);
  border: none;
  border-radius: 8px;
  color: #fff;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

.card button:hover {
  background: var(--accent-hover);
}

/* ----- Room ----- */
.room .sidebar {
  width: 260px;
  min-width: 260px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.sidebar-toggle {
  display: none;
  position: fixed;
  top: calc(8px + env(safe-area-inset-top));
  right: 12px;
  z-index: 20;
  padding: 8px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--text);
  font: inherit;
  font-size: 0.85rem;
  cursor: pointer;
}

.sidebar-toggle:hover {
  border-color: var(--accent);
}

.page.room.sidebar-collapsed .sidebar {
  display: none;
}

.room .back {
  color: var(--text-muted);
  text-decoration: none;
  font-size: 0.9rem;
}

.room .back:hover {
  color: var(--accent);
}

.room-info {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}

.room-info .label,
.users-wrap .label {
  width: 100%;
  font-size: 0.8rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.room-info code {
  font-size: 0.9rem;
  background: var(--bg);
  padding: 6px 10px;
  border-radius: 6px;
  flex: 1;
  min-width: 0;
  word-break: break-all;
}

.room-info #copyLink {
  padding: 6px 12px;
  background: var(--accent);
  border: none;
  border-radius: 6px;
  color: #fff;
  font-size: 0.85rem;
  cursor: pointer;
}

.room-info #copyLink:hover {
  background: var(--accent-hover);
}

.users-wrap ul {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 6px 10px;
}

.users-wrap li {
  padding: 4px 10px;
  font-size: 0.9rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--bg);
}

.users-wrap li:last-child {
  border-bottom: 1px solid var(--border);
}

.chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  max-width: 720px;
  margin: 0 auto;
  width: 100%;
}

#messageList {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-y: overlay; /* overlay scrollbar so it does not take layout space (WebKit) */
  list-style: none;
  margin: 0;
  padding: 20px;
  padding-bottom: calc(20px + var(--keyboard-offset, 0px));
  display: flex;
  flex-direction: column;
  gap: 12px;
  scrollbar-gutter: stable; /* reserve scrollbar space so layout does not shift (e.g. Firefox) */
  scrollbar-color: transparent transparent;
  scrollbar-width: thin;
}

#messageList::-webkit-scrollbar {
  width: 6px;
}

#messageList::-webkit-scrollbar-track {
  background: transparent;
}

#messageList::-webkit-scrollbar-thumb {
  background: transparent;
  border-radius: 4px;
}


#messageList li {
  max-width: 85%;
  align-self: flex-start;
}

#messageList li.own {
  align-self: flex-end;
}

#messageList .msg-head {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-bottom: 4px;
}

#messageList .msg-body {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 14px;
  word-break: break-word;
}

#messageList li.own .msg-body {
  background: var(--accent);
  border-color: var(--accent);
}

#messageList .msg-time {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: 4px;
}

.chat-form {
  display: flex;
  gap: 10px;
  padding: 16px 20px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: calc(16px + env(safe-area-inset-bottom));
  transform: translateY(calc(-1 * var(--keyboard-offset, 0px)));
}

.chat-form input {
  flex: 1;
  padding: 12px 16px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font: inherit;
}

.chat-form input:focus {
  outline: none;
  border-color: var(--accent);
}

.chat-form button {
  padding: 12px 20px;
  background: var(--accent);
  border: none;
  border-radius: 8px;
  color: #fff;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

.chat-form button:hover {
  background: var(--accent-hover);
}

/* Toast */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(100px);
  padding: 12px 20px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.4);
  opacity: 0;
  transition: transform 0.25s, opacity 0.25s;
  z-index: 100;
}

.toast.show {
  transform: translateX(-50%) translateY(0);
  opacity: 1;
}

.toast.error {
  border-color: var(--danger);
  color: #ff6b6b;
}

@media (max-width: 960px) {
  .room .sidebar {
    width: 220px;
    min-width: 220px;
  }

  .chat-main {
    max-width: 100%;
  }
}

@media (max-width: 720px) {
  .page {
    padding: 16px;
  }

  .cards {
    width: 100%;
    gap: 16px;
  }

  .card {
    width: 100%;
    max-width: 100%;
    min-width: 0;
    padding: 20px;
  }

  .home header {
    margin-bottom: 28px;
  }

  .home h1 {
    font-size: 1.5rem;
  }

  .page.room {
    flex-direction: column;
  }

  .room .sidebar {
    width: 100%;
    min-width: 0;
    border-right: none;
    border-bottom: 1px solid var(--border);
    padding: 12px;
    gap: 12px;
    max-height: 40vh;
    overflow-y: auto;
  }

  .sidebar-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  .room .sidebar-actions {
    flex-direction: row;
    flex-wrap: wrap;
  }

  .room .sidebar-actions .btn-destroy {
    width: auto;
    flex: 1 1 160px;
  }

  .room .back {
    font-size: 0.8rem;
  }

  .room-info .label,
  .users-wrap .label {
    font-size: 0.7rem;
  }

  .room-info code {
    font-size: 0.8rem;
    padding: 4px 8px;
  }

  .room-info #copyLink {
    padding: 6px 10px;
    font-size: 0.8rem;
  }

  .users-wrap li {
    padding: 6px 0;
    font-size: 0.85rem;
  }

  .chat-main {
    width: 100%;
  }

  #messageList {
    padding: 12px 16px;
    gap: 10px;
  }

  #messageList li {
    max-width: 92%;
  }

  .chat-form {
    padding: 12px 16px;
    padding-bottom: env(safe-area-inset-bottom);
  }

  .chat-form button {
    padding: 12px 16px;
  }
}

@media (max-width: 420px) {
  .room-info {
    flex-wrap: wrap;
    align-items: center;
  }

  .room-info #copyLink {
    margin-left: 8px;
    white-space: nowrap;
  }

  .room-info code {
    flex: 1 1 auto;
  }
}
