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

:root {
  --bg: #0f1117;
  --surface: #1a1d27;
  --surface2: #242736;
  --accent: #6c63ff;
  --accent2: #a29bfe;
  --text: #e8e8f0;
  --muted: #8888aa;
  --danger: #e74c3c;
  --radius: 12px;
  --border: #2d2f45;
  --border-input: #3a3c55;
  --canvas-bg: #0d0f1a;
  --ctrl-bg: #3a3c55;
  --ctrl-hover: #4a4c6a;
  --join-glow: #1e1b4b;
  --shadow-card: rgba(0,0,0,0.6);
}

[data-theme="light"] {
  --bg: #f0f2f8;
  --surface: #ffffff;
  --surface2: #e8eaef;
  --accent: #6c63ff;
  --accent2: #7c74ff;
  --text: #1a1a2e;
  --muted: #5c5c7a;
  --danger: #dc3545;
  --border: #d0d2e0;
  --border-input: #c0c2d0;
  --canvas-bg: #dde0ec;
  --ctrl-bg: #dcdee8;
  --ctrl-hover: #cccfe0;
  --join-glow: #ebe8ff;
  --shadow-card: rgba(100,100,150,0.15);
}

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

/* ── Join screen ────────────────────────────────── */
#join-screen {
  position: fixed; inset: 0;
  display: flex; align-items: center; justify-content: center;
  background: radial-gradient(ellipse at 50% 40%, var(--join-glow) 0%, var(--bg) 70%);
  z-index: 100;
}

.join-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 48px 40px;
  width: 360px;
  text-align: center;
  box-shadow: 0 24px 64px var(--shadow-card);
}

.join-card h1 {
  font-size: 2.4rem;
  font-weight: 800;
  letter-spacing: -1px;
  margin-bottom: 12px;
}

.join-card h1 span { color: var(--accent); }

.join-card p { color: var(--muted); margin-bottom: 28px; font-size: 0.95rem; line-height: 1.5; }

.join-card input {
  width: 100%;
  padding: 12px 16px;
  background: var(--surface2);
  border: 1px solid var(--border-input);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 1rem;
  outline: none;
  margin-bottom: 16px;
  transition: border-color 0.2s;
}

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

.join-card button {
  width: 100%;
  padding: 13px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s, transform 0.1s;
}

.join-card button:hover { background: var(--accent2); }
.join-card button:active { transform: scale(0.98); }
.join-card .hint { font-size: 0.78rem; margin-top: 14px; color: var(--muted); }

/* ── Room layout ───────────────────────────────── */
#room {
  display: flex;
  height: 100vh;
  width: 100vw;
}

#canvas-wrap {
  flex: 1;
  overflow: hidden;
  cursor: crosshair;
  position: relative;
  background: var(--canvas-bg);
}

#world-canvas {
  display: block;
  image-rendering: pixelated;
}

/* ── Sidebar ───────────────────────────────────── */
#sidebar {
  width: 220px;
  min-width: 220px;
  background: var(--surface);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 0;
  overflow: hidden;
}

/* Self video */
#self-video-wrap {
  position: relative;
  background: #000;
  aspect-ratio: 16/9;
}

#self-video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  transform: scaleX(-1);
}

#self-label {
  position: absolute;
  bottom: 6px; left: 8px;
  font-size: 0.72rem;
  background: rgba(0,0,0,0.6);
  padding: 2px 6px;
  border-radius: 4px;
  pointer-events: none;
}

/* Controls */
#controls {
  display: flex;
  gap: 8px;
  padding: 10px 12px;
  background: var(--surface2);
  border-bottom: 1px solid var(--border);
}

.ctrl-btn {
  flex: 1;
  padding: 8px;
  background: var(--ctrl-bg);
  border: none;
  border-radius: 8px;
  color: var(--text);
  cursor: pointer;
  transition: background 0.2s;
  display: flex; align-items: center; justify-content: center;
}

.ctrl-btn svg { width: 18px; height: 18px; }

.ctrl-btn:hover { background: var(--ctrl-hover); }
.ctrl-btn.off { background: var(--danger); color: #fff; }

/* Peer videos */
#peer-videos {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.peer-tile {
  position: relative;
  background: #000;
  border-radius: 8px;
  overflow: hidden;
  aspect-ratio: 16/9;
  border: 1px solid var(--border);
  transition: border-color 0.3s;
}

.peer-tile.active { border-color: var(--accent); }

.peer-tile video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.peer-tile .peer-name {
  position: absolute;
  bottom: 5px; left: 7px;
  font-size: 0.7rem;
  background: rgba(0,0,0,0.6);
  padding: 2px 6px;
  border-radius: 4px;
}

/* Proximity hint */
#proximity-hint {
  padding: 12px;
  text-align: center;
  font-size: 0.78rem;
  color: var(--muted);
  border-top: 1px solid var(--border);
}

/* Settings panel */
#settings-panel {
  overflow-y: auto;
  flex-shrink: 0;
  max-height: 55vh;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.settings-section {
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.settings-section + .settings-section {
  border-top: 1px solid var(--border);
}

.settings-section-title {
  font-size: 0.65rem;
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.device-row {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.device-row label {
  font-size: 0.68rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.device-row select {
  background: var(--surface2);
  border: 1px solid var(--border-input);
  border-radius: 6px;
  color: var(--text);
  font-size: 0.78rem;
  padding: 5px 8px;
  outline: none;
  cursor: pointer;
  width: 100%;
  transition: border-color 0.2s;
  appearance: auto;
}

.device-row select:focus { border-color: var(--accent); }

/* Name row */
.name-row {
  display: flex;
  gap: 6px;
  align-items: center;
}

.name-row input {
  flex: 1;
  padding: 6px 10px;
  background: var(--surface2);
  border: 1px solid var(--border-input);
  border-radius: 6px;
  color: var(--text);
  font-size: 0.82rem;
  outline: none;
  min-width: 0;
  transition: border-color 0.2s;
}

.name-row input:focus { border-color: var(--accent); }

.save-btn {
  flex-shrink: 0;
  width: 30px;
  height: 30px;
  border: none;
  border-radius: 6px;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s, transform 0.1s;
}

.save-btn:hover { background: var(--accent2); }
.save-btn:active { transform: scale(0.92); }
.save-btn svg { width: 14px; height: 14px; }

/* Color swatches */
#color-swatches {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.swatch {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 2px solid transparent;
  cursor: pointer;
  transition: transform 0.15s, border-color 0.15s;
  outline: none;
}

.swatch:hover { transform: scale(1.2); }

.swatch.selected {
  border-color: var(--text);
  transform: scale(1.1);
  box-shadow: 0 0 0 1px var(--bg);
}

#btn-settings.active { background: var(--accent); color: #fff; }

/* Direct chat panel */
#chat-panel {
  position: fixed;
  left: 16px;
  bottom: 16px;
  width: 284px;
  height: 370px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.3);
  display: flex;
  flex-direction: column;
  z-index: 150;
  overflow: hidden;
  animation: chat-slide-in 0.2s ease;
}

@keyframes chat-slide-in {
  from { opacity: 0; transform: translateY(12px); }
  to   { opacity: 1; transform: translateY(0); }
}

#chat-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 11px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--surface2);
  flex-shrink: 0;
}

#chat-peer-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}

#chat-peer-name {
  flex: 1;
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#btn-chat-close {
  width: 26px;
  height: 26px;
  border: none;
  background: none;
  color: var(--muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 5px;
  flex-shrink: 0;
  transition: background 0.15s, color 0.15s;
}

#btn-chat-close:hover { background: var(--ctrl-bg); color: var(--text); }
#btn-chat-close svg { width: 14px; height: 14px; }

#chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 10px 10px 6px;
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.chat-empty {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.78rem;
  color: var(--muted);
  font-style: italic;
}

.chat-msg {
  max-width: 82%;
  padding: 7px 11px;
  border-radius: 14px;
  font-size: 0.82rem;
  line-height: 1.45;
  word-break: break-word;
}

.chat-msg-own {
  align-self: flex-end;
  background: var(--accent);
  color: #fff;
  border-bottom-right-radius: 4px;
}

.chat-msg-peer {
  align-self: flex-start;
  background: var(--surface2);
  color: var(--text);
  border-bottom-left-radius: 4px;
}

#chat-input-row {
  display: flex;
  gap: 6px;
  padding: 8px 10px;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

#chat-input {
  flex: 1;
  padding: 8px 12px;
  background: var(--surface2);
  border: 1px solid var(--border-input);
  border-radius: 20px;
  color: var(--text);
  font-size: 0.82rem;
  outline: none;
  min-width: 0;
  transition: border-color 0.2s;
}

#chat-input:focus { border-color: var(--accent); }

#btn-chat-send {
  width: 34px;
  height: 34px;
  flex-shrink: 0;
  border: none;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s, transform 0.1s;
}

#btn-chat-send:hover { background: var(--accent2); }
#btn-chat-send:active { transform: scale(0.9); }
#btn-chat-send svg { width: 14px; height: 14px; }

/* Participants panel */
#participants-panel {
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.participants-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px 5px;
  font-size: 0.65rem;
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.count-badge {
  background: var(--surface2);
  border-radius: 10px;
  padding: 1px 7px;
  font-size: 0.68rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  color: var(--muted);
}

#participants-list {
  list-style: none;
  max-height: 148px;
  overflow-y: auto;
  padding: 0 8px 8px;
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.participant-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 6px;
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.15s;
  user-select: none;
}

.participant-row:hover { background: var(--surface2); }

.participant-row:hover .participant-icons { opacity: 1; }

.participant-dot {
  width: 11px;
  height: 11px;
  border-radius: 50%;
  flex-shrink: 0;
}

.participant-name {
  flex: 1;
  font-size: 0.82rem;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.unread-badge {
  background: #e74c3c;
  color: #fff;
  border-radius: 10px;
  font-size: 0.6rem;
  font-weight: 700;
  padding: 1px 5px;
  min-width: 16px;
  text-align: center;
  flex-shrink: 0;
  line-height: 1.5;
}

@keyframes badge-pulse {
  0%   { transform: scale(1); }
  40%  { transform: scale(1.5); }
  70%  { transform: scale(0.9); }
  100% { transform: scale(1); }
}

.badge-pulse { animation: badge-pulse 0.4s ease; }

/* Incoming message toast */
#msg-toast {
  position: fixed;
  bottom: 402px;
  left: 16px;
  max-width: 284px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  border-radius: 10px;
  padding: 10px 14px;
  font-size: 0.82rem;
  color: var(--text);
  box-shadow: 0 4px 16px rgba(0,0,0,0.25);
  z-index: 160;
  pointer-events: none;
  transition: opacity 0.25s, transform 0.25s;
}

#msg-toast.toast-visible { opacity: 1; transform: translateY(0); }
#msg-toast.toast-hidden  { opacity: 0; transform: translateY(8px); }

.participant-icons {
  display: flex;
  align-items: center;
  gap: 1px;
  opacity: 0;
  transition: opacity 0.15s;
  flex-shrink: 0;
}

.participant-icon-btn {
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2px 3px;
  border-radius: 4px;
  transition: color 0.15s;
}

.participant-icon-btn:hover { color: var(--accent); }
.participant-icon-btn svg { width: 13px; height: 13px; display: block; }

.participant-walk-icon {
  color: var(--muted);
  display: flex;
  align-items: center;
  padding: 2px 3px;
}

.participant-walk-icon svg { width: 13px; height: 13px; display: block; }

.participant-row.walking {
  background: var(--accent);
}

.participant-row.walking .participant-name,
.participant-row.walking .participant-icons { color: #fff; opacity: 1; }

.participants-empty {
  font-size: 0.78rem;
  color: var(--muted);
  text-align: center;
  padding: 10px 6px;
  font-style: italic;
}

/* Theme toggle */
#btn-theme {
  position: fixed;
  top: 14px;
  right: 14px;
  z-index: 200;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s, border-color 0.2s, transform 0.15s, box-shadow 0.2s;
  box-shadow: 0 2px 8px rgba(0,0,0,0.25);
}

#btn-theme:hover {
  background: var(--surface2);
  transform: scale(1.1);
}

#btn-theme svg { width: 18px; height: 18px; }

/* Audio mode radio group */
.radio-group {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.radio-option {
  display: flex;
  align-items: flex-start;
  gap: 7px;
  padding: 4px 6px;
  border-radius: 7px;
  cursor: pointer;
  transition: background 0.15s;
  border: 1px solid transparent;
}

.radio-option:hover {
  background: var(--surface2);
}

.radio-option input[type="radio"] {
  appearance: none;
  -webkit-appearance: none;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 2px solid var(--border-input);
  background: transparent;
  cursor: pointer;
  flex-shrink: 0;
  margin-top: 2px;
  transition: border-color 0.15s, background 0.15s;
  position: relative;
}

.radio-option input[type="radio"]:checked {
  border-color: var(--accent);
  background: var(--accent);
  box-shadow: inset 0 0 0 3px var(--surface);
}

.radio-option.selected {
  background: color-mix(in srgb, var(--accent) 10%, transparent);
  border-color: color-mix(in srgb, var(--accent) 30%, transparent);
}

.radio-label {
  display: flex;
  flex-direction: column;
  gap: 1px;
  min-width: 0;
}

.radio-label-title {
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--text);
  line-height: 1.3;
}

.radio-label-desc {
  font-size: 0.62rem;
  color: var(--muted);
  line-height: 1.3;
}

.settings-hint {
  font-size: 0.68rem;
  color: var(--accent2);
  line-height: 1.4;
  padding: 2px 0;
  min-height: 1em;
}

/* Utilities */
.hidden { display: none !important; }
