:root {
  --bg: #17181c;
  --panel: #23252b;
  --panel-2: #2c2f36;
  --text: #e8e9ec;
  --muted: #9aa0aa;
  --accent: #4f8cff;
  --danger: #e05a5a;
  --active: #4fd18b;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
}

.screen { position: fixed; inset: 0; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--danger); min-height: 1.2em; }

/* -------- Join screen -------- */
#join-screen {
  display: flex;
  align-items: center;
  justify-content: center;
}
.join-card {
  background: var(--panel);
  padding: 32px;
  border-radius: 12px;
  width: min(400px, 90vw);
  box-shadow: 0 10px 30px rgba(0, 0, 0, .35);
}
.join-card h1 { margin: 0 0 8px; font-size: 22px; }
.join-card input {
  width: 100%;
  padding: 12px 14px;
  margin: 16px 0 12px;
  border-radius: 8px;
  border: 1px solid #3a3d45;
  background: var(--panel-2);
  color: var(--text);
  font-size: 16px;
}
.join-card button {
  width: 100%;
  padding: 12px;
  border-radius: 8px;
  border: 0;
  background: var(--accent);
  color: white;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
}
.join-card button:hover { filter: brightness(1.1); }
.join-card .small { font-size: 12px; margin-top: 12px; }
.join-card .small a { color: var(--muted); text-decoration: none; }
.join-card .small a:hover { color: var(--text); }
.recent-rooms { margin-top: 20px; }
.recent-rooms p { margin: 0 0 6px; }
.recent-rooms ul { list-style: none; padding: 0; margin: 0; }
.recent-rooms li { margin: 4px 0; }
.recent-rooms a {
  display: block;
  padding: 8px 10px;
  border-radius: 6px;
  background: var(--panel-2);
  color: var(--text);
  text-decoration: none;
  font-size: 14px;
}
.recent-rooms a:hover { filter: brightness(1.15); }

/* -------- Conference layout -------- */
#conference {
  display: grid;
  grid-template-rows: 48px 1fr;
  grid-template-columns: 1fr 320px;
  grid-template-areas:
    "top   top"
    "stage chat";
}
#conference.no-chat {
  grid-template-columns: 1fr 0;
}
#conference.no-chat .chat-panel { display: none; }

.topbar {
  grid-area: top;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 16px;
  background: var(--panel);
  border-bottom: 1px solid #000;
}
.topbar .room-name { font-weight: 600; }
.topbar .peer-count {
  background: var(--panel-2);
  padding: 2px 8px;
  border-radius: 10px;
  font-size: 12px;
  color: var(--muted);
  display: inline-flex;
  align-items: center;
  gap: 4px;
}
.topbar .room-name { display: inline-flex; align-items: center; gap: 6px; }
.tb-btn i { pointer-events: none; }
.topbar .spacer { flex: 1; }
.tb-btn {
  background: var(--panel-2);
  color: var(--text);
  border: 0;
  padding: 6px 12px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 16px;
}
.tb-btn:hover { filter: brightness(1.15); }
.tb-btn.danger { background: var(--danger); }
.tb-btn.off { background: #443; color: #ffb3b3; }

.stage-wrap {
  grid-area: stage;
  display: grid;
  grid-template-rows: 1fr auto;
  padding: 12px;
  gap: 12px;
  min-height: 0;
}

.stage {
  position: relative;
  background: #000;
  border-radius: 12px;
  overflow: hidden;
  min-height: 0;
}
.stage video {
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: #000;
}

.thumbs {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 4px;
  min-height: 120px;
}
.thumb {
  position: relative;
  width: 200px;
  height: 120px;
  flex: 0 0 auto;
  background: #000;
  border-radius: 8px;
  overflow: hidden;
  border: 2px solid transparent;
  transition: border-color .15s ease;
  cursor: pointer;
}
.thumb.active { border-color: var(--active); }
.thumb video {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.tile-label {
  position: absolute;
  left: 8px;
  bottom: 8px;
  background: rgba(0, 0, 0, .55);
  color: #fff;
  padding: 3px 8px;
  border-radius: 4px;
  font-size: 12px;
  pointer-events: none;
}

.mic-indicator {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: transparent;
  transition: background .1s;
}
.thumb.talking .mic-indicator { background: var(--active); }
.thumb.muted .mic-indicator { background: var(--danger); }

/* -------- Chat panel -------- */
.chat-panel {
  grid-area: chat;
  background: var(--panel);
  display: grid;
  grid-template-rows: 1fr auto;
  border-left: 1px solid #000;
  min-height: 0;
}
.chat-messages {
  padding: 12px;
  overflow-y: auto;
  font-size: 14px;
  line-height: 1.4;
}
.chat-messages .msg { margin-bottom: 10px; }
.chat-messages .msg .who { font-weight: 600; margin-right: 6px; }
.chat-messages .msg .ts { font-size: 11px; color: var(--muted); margin-left: 6px; }
.chat-messages .msg .body { white-space: pre-wrap; word-break: break-word; }
.chat-messages .msg.system { color: var(--muted); font-style: italic; }

.chat-form {
  display: flex;
  gap: 6px;
  padding: 10px;
  border-top: 1px solid #000;
}
.chat-form input {
  flex: 1;
  padding: 8px 10px;
  border-radius: 6px;
  border: 1px solid #3a3d45;
  background: var(--panel-2);
  color: var(--text);
}
.chat-form button {
  background: var(--accent);
  color: #fff;
  border: 0;
  padding: 0 14px;
  border-radius: 6px;
  cursor: pointer;
}

/* -------- Toasts -------- */
.toasts {
  position: fixed;
  bottom: 16px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
  z-index: 100;
}
.toast {
  background: rgba(30, 32, 38, .95);
  color: var(--text);
  padding: 10px 16px;
  border-radius: 8px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, .4);
  font-size: 14px;
  border-left: 3px solid var(--accent);
}

/* -------- Responsive fallback -------- */
@media (max-width: 720px) {
  #conference {
    grid-template-columns: 1fr;
    grid-template-rows: 48px 1fr 40vh;
    grid-template-areas:
      "top"
      "stage"
      "chat";
  }
  #conference.no-chat {
    grid-template-rows: 48px 1fr 0;
  }
  .thumb { width: 140px; height: 84px; }
}
