:root {
  color-scheme: light;
  --bg: #f6f5f2;
  --surface: #fdfcfa;
  --surface-strong: rgba(255, 255, 255, 0.95);
  --surface-soft: rgba(255, 255, 255, 0.85);
  --surface-softer: rgba(255, 255, 255, 0.72);
  --surface-faint: rgba(255, 255, 255, 0.55);
  --ink-rgb: 38, 37, 31;
  --text: #26251f;
  --text-secondary: #33322f;
  --text-muted: #6b6a63;
  --text-faint: #8a8880;
  --border: rgba(38, 37, 31, 0.22);
  --border-soft: rgba(30, 30, 28, 0.1);
  --border-strong: #26251f;
  --shadow-soft: rgba(30, 30, 28, 0.06);
  --shadow-mid: rgba(38, 37, 31, 0.1);
  --shadow-strong: rgba(38, 37, 31, 0.16);
  --accent-ok: #4f7c6c;
  --accent-ok-border: rgba(79, 124, 108, 0.35);
  --accent-bad: #b1584f;
  --accent-bad-border: rgba(177, 88, 79, 0.35);
  --overlay-scrim: rgba(246, 245, 242, 0.55);
}

/* Dark mode is opt-in only, via the toggle button — light is always the
   default regardless of system preference, and nothing here reacts to
   prefers-color-scheme. */
:root[data-theme='dark'] {
  color-scheme: dark;
  --bg: #181715;
  --surface: #242320;
  --surface-strong: rgba(36, 35, 32, 0.95);
  --surface-soft: rgba(36, 35, 32, 0.85);
  --surface-softer: rgba(36, 35, 32, 0.72);
  --surface-faint: rgba(36, 35, 32, 0.55);
  --ink-rgb: 236, 233, 226;
  --text: #ece9e2;
  --text-secondary: #ded9cf;
  --text-muted: #a19d92;
  --text-faint: #837f76;
  --border: rgba(236, 233, 226, 0.2);
  --border-soft: rgba(236, 233, 226, 0.14);
  --border-strong: #ece9e2;
  --shadow-soft: rgba(0, 0, 0, 0.3);
  --shadow-mid: rgba(0, 0, 0, 0.4);
  --shadow-strong: rgba(0, 0, 0, 0.5);
  --accent-ok: #86bfa9;
  --accent-ok-border: rgba(134, 191, 169, 0.4);
  --accent-bad: #e0968c;
  --accent-bad-border: rgba(224, 150, 140, 0.4);
  --overlay-scrim: rgba(24, 23, 21, 0.6);
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--bg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  /* No native scroll or zoom gesture is ever wanted anywhere on this page —
     applied globally, not just on the canvas, since a pinch can start on
     any UI element and the viewport meta tag alone isn't reliably honoured
     (iOS Safari in particular still allows pinch-zoom regardless of it). */
  touch-action: none;
}

/* Text people actually type into should still behave like text inputs. */
input, textarea {
  -webkit-user-select: text;
  user-select: text;
  -webkit-touch-callout: default;
}

#world {
  display: block;
  width: 100vw;
  height: 100vh;
  touch-action: none;
}

.theme-toggle {
  position: fixed;
  left: calc(20px + env(safe-area-inset-left));
  top: calc(20px + env(safe-area-inset-top));
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--surface-soft);
  color: var(--text);
  font-size: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  z-index: 6;
  box-shadow: 0 2px 10px var(--shadow-soft);
}

.hint {
  position: fixed;
  left: 50%;
  bottom: calc(28px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  padding: 8px 16px;
  border-radius: 999px;
  background: var(--surface-softer);
  color: var(--text-secondary);
  font-size: 13px;
  letter-spacing: 0.02em;
  border: 1px solid var(--border-soft);
  box-shadow: 0 2px 10px var(--shadow-soft);
  pointer-events: none;
  transition: opacity 1.2s ease;
  max-width: calc(100vw - 32px);
  text-align: center;
}

.hint.hidden {
  opacity: 0;
}

.hint-touch {
  display: none;
}

.chat-bar {
  position: fixed;
  left: 50%;
  bottom: calc(28px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  transition: opacity 0.25s ease, transform 0.25s ease;
  z-index: 5;
}

.chat-bar.hidden {
  opacity: 0;
  pointer-events: none;
  transform: translateX(-50%) translateY(6px);
}

.chat-bar input {
  width: min(360px, calc(100vw - 48px));
  padding: 10px 16px;
  border-radius: 999px;
  border: 1.5px solid var(--border);
  background: var(--surface-strong);
  font-size: 14px;
  font-family: inherit;
  color: var(--text);
  outline: none;
  box-shadow: 0 2px 10px var(--shadow-mid);
  box-sizing: border-box;
}

.chat-bar input:focus {
  border-color: var(--border-strong);
}

.room-toast {
  position: fixed;
  top: calc(24px + env(safe-area-inset-top));
  left: 50%;
  transform: translateX(-50%);
  padding: 7px 16px;
  border-radius: 999px;
  background: var(--surface-soft);
  color: var(--text-secondary);
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.02em;
  border: 1px solid var(--border-soft);
  box-shadow: 0 2px 10px var(--shadow-soft);
  pointer-events: none;
  transition: opacity 0.4s ease;
  z-index: 5;
}

.room-toast.hidden {
  opacity: 0;
}

.room-hover-tooltip {
  position: fixed;
  padding: 6px 12px;
  border-radius: 10px;
  background: var(--surface-soft);
  color: var(--text);
  font-size: 12px;
  font-weight: 600;
  border: 1px solid var(--border-soft);
  box-shadow: 0 2px 10px var(--shadow-soft);
  pointer-events: none;
  z-index: 4;
  max-width: 220px;
  transition: opacity 0.15s ease;
}

.room-hover-tooltip.hidden {
  opacity: 0;
}

.room-hover-tooltip .names {
  display: block;
  font-weight: 400;
  color: var(--text-muted);
  margin-top: 2px;
}

.mic-status {
  position: fixed;
  right: calc(20px + env(safe-area-inset-right));
  bottom: calc(28px + env(safe-area-inset-bottom));
  padding: 7px 14px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface-soft);
  font-size: 12px;
  font-family: inherit;
  color: var(--text-muted);
  cursor: pointer;
  transition: opacity 0.3s ease;
  z-index: 5;
}

.mic-status.hidden {
  opacity: 0;
  pointer-events: none;
}

.mic-status[data-state='active'] {
  color: var(--accent-ok);
  border-color: var(--accent-ok-border);
}

.mic-status[data-state='denied'] {
  color: var(--accent-bad);
  border-color: var(--accent-bad-border);
}

/* Touch (joystick + jump/chat/shoot buttons) is built for every device but
   only shown where touch is the PRIMARY input — (hover:none)+(pointer:coarse)
   is the standard, reliable way to detect that, unlike sniffing touch
   support alone (which false-positives on touch-enabled laptops with a mouse). */
.touch-controls {
  display: none;
}

@media (hover: none) and (pointer: coarse) {
  .touch-controls {
    display: block;
  }

  .hint-desktop {
    display: none;
  }

  .hint-touch {
    display: inline;
  }

  /* Move off the bottom-right corner entirely rather than try to stack above
     the jump/chat/shoot buttons there — that's what was colliding with them. */
  .mic-status {
    top: calc(24px + env(safe-area-inset-top));
    right: calc(20px + env(safe-area-inset-right));
    bottom: auto;
  }

  .call-self {
    bottom: calc(150px + env(safe-area-inset-bottom));
  }

  .swatch {
    width: 34px;
    height: 34px;
  }

  /* getDisplayMedia (screen share) isn't supported on mobile Safari/Chrome —
     hiding it here beats offering a button that just fails silently. */
  #call-screenshare-btn {
    display: none;
  }

  .call-files-panel {
    bottom: calc(210px + env(safe-area-inset-bottom));
  }
}

/* Typing and moving at once doesn't make sense — free up the space instead
   of letting the chat bar sit on top of the joystick. */
.touch-controls.chat-open {
  display: none;
}

.joystick-base {
  position: fixed;
  left: calc(24px + env(safe-area-inset-left));
  bottom: calc(28px + env(safe-area-inset-bottom));
  width: 104px;
  height: 104px;
  border-radius: 50%;
  background: var(--surface-faint);
  border: 1.5px solid var(--border);
  touch-action: none;
  z-index: 8;
}

.joystick-nub {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 46px;
  height: 46px;
  margin-left: -23px;
  margin-top: -23px;
  border-radius: 50%;
  background: rgba(var(--ink-rgb), 0.75);
  transition: transform 0.05s linear;
  pointer-events: none;
}

.touch-action-buttons {
  position: fixed;
  right: calc(20px + env(safe-area-inset-right));
  bottom: calc(28px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column-reverse;
  align-items: center;
  gap: 12px;
  z-index: 8;
}

.touch-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  border: 1.5px solid var(--border);
  background: var(--surface-softer);
  color: var(--text);
  font-family: inherit;
  font-weight: 700;
  touch-action: none;
  box-shadow: 0 2px 10px var(--shadow-soft);
}

.touch-btn.touch-jump {
  width: 68px;
  height: 68px;
  font-size: 12px;
  letter-spacing: 0.03em;
}

.touch-btn.touch-chat {
  width: 50px;
  height: 50px;
  font-size: 20px;
}

.touch-btn.touch-shoot {
  width: 58px;
  height: 58px;
  font-size: 24px;
}

.call-view {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  transition: opacity 0.3s ease;
}

.call-view.hidden {
  opacity: 0;
  pointer-events: none;
}

.call-header {
  flex: none;
  padding: calc(16px + env(safe-area-inset-top)) 24px 8px;
  text-align: center;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.03em;
  color: var(--text-muted);
  text-transform: uppercase;
}

.call-grid {
  flex: 1;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(320px, 100%), 1fr));
  grid-auto-rows: 1fr;
  align-items: stretch;
  justify-items: stretch;
  gap: 16px;
  padding: 8px calc(16px + env(safe-area-inset-right)) 8px calc(16px + env(safe-area-inset-left));
  overflow: auto;
  position: relative;
  min-height: 0; /* let the grid actually shrink/fill within the flex column instead of overflowing it */
}

.call-empty {
  color: var(--text-faint);
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  grid-column: 1 / -1;
}

.call-empty.hidden {
  display: none;
}

/* Video tile backgrounds stay dark regardless of site theme — that's the
   universal video-call letterbox convention, not something to theme. */
.call-tile {
  position: relative;
  width: 100%;
  height: 100%;
  min-height: 160px;
  border-radius: 16px;
  overflow: hidden;
  background: #26251f;
  border: 1.5px solid #26251f;
}

.call-tile video {
  width: 100%;
  height: 100%;
  /* contain, not cover — the tile box can be any shape the grid gives it,
     but the actual camera frame should never be cropped to fill it. */
  object-fit: contain;
}

.call-tile.waiting video {
  opacity: 0;
}

.call-tile.waiting::after {
  content: '';
  position: absolute;
  inset: 0;
  background: repeating-linear-gradient(
    135deg,
    rgba(255, 255, 255, 0.04),
    rgba(255, 255, 255, 0.04) 10px,
    transparent 10px,
    transparent 20px
  );
}

.call-tile-label {
  position: absolute;
  left: 10px;
  bottom: 8px;
  padding: 3px 9px;
  border-radius: 999px;
  background: rgba(38, 37, 31, 0.55);
  color: #fdfcfa;
  font-size: 12px;
  font-weight: 600;
}

.call-self {
  position: fixed;
  right: calc(16px + env(safe-area-inset-right));
  bottom: calc(96px + env(safe-area-inset-bottom));
  width: min(130px, 26vw);
  aspect-ratio: 4 / 3;
  border-radius: 12px;
  overflow: hidden;
  border: 1.5px solid #26251f;
  background: #26251f;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.3);
  z-index: 1;
}

.call-self video {
  /* Absolutely positioned to fill the box, not width/height:100% in normal
     flow — percentage heights inside an aspect-ratio-only box are unreliable
     on some mobile WebKit/Blink versions (the box's height doesn't always
     resolve as "definite" for percentage purposes before layout), which
     showed up as the self-preview video rendering at its own intrinsic size
     and getting clipped by `overflow: hidden`, cutting off the bottom of
     the picture instead of filling the bubble. `inset: 0` on an absolutely
     positioned element resolves against the final computed box regardless,
     sidestepping that ambiguity entirely. */
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  transform: scaleX(-1); /* mirror — this is the only tile that should feel like a mirror */
}

.call-controls {
  flex: none;
  display: flex;
  justify-content: center;
  gap: 10px;
  padding: 16px calc(24px + env(safe-area-inset-right)) calc(20px + env(safe-area-inset-bottom)) calc(24px + env(safe-area-inset-left));
  flex-wrap: wrap;
}

.call-controls button {
  padding: 12px 20px;
  border-radius: 999px;
  border: 1.5px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 14px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
}

.call-controls button[data-state='muted'] {
  color: var(--accent-bad);
  border-color: var(--accent-bad-border);
}

.call-controls .call-leave {
  background: var(--text);
  color: var(--bg);
  border-color: var(--text);
}

.call-controls button[data-state='active'] {
  color: var(--accent-ok);
  border-color: var(--accent-ok-border);
}

.call-files-panel {
  position: fixed;
  right: calc(16px + env(safe-area-inset-right));
  left: calc(16px + env(safe-area-inset-left));
  bottom: calc(84px + env(safe-area-inset-bottom));
  max-width: 380px;
  margin-left: auto;
  max-height: min(60vh, 480px);
  display: flex;
  flex-direction: column;
  background: var(--surface-strong);
  border: 1.5px solid var(--border);
  border-radius: 16px;
  box-shadow: 0 12px 32px var(--shadow-strong);
  z-index: 2;
  overflow: hidden;
}

.call-files-panel.hidden {
  display: none;
}

.call-files-header {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px;
  font-size: 13px;
  font-weight: 700;
  color: var(--text);
  border-bottom: 1px solid var(--border-soft);
}

.call-files-close {
  border: none;
  background: none;
  color: var(--text-muted);
  font-size: 14px;
  cursor: pointer;
  padding: 2px 6px;
}

.call-files-dropzone {
  flex: none;
  margin: 12px;
  padding: 16px;
  border: 1.5px dashed var(--border);
  border-radius: 12px;
  text-align: center;
  font-size: 12px;
  color: var(--text-muted);
  cursor: pointer;
}

.call-files-dropzone:hover,
.call-files-dropzone.dragover {
  border-color: var(--border-strong);
  color: var(--text);
}

.call-files-dropzone p {
  margin: 0;
}

.call-files-queue {
  flex: 1;
  overflow-y: auto;
  padding: 0 12px 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.file-item {
  border: 1px solid var(--border-soft);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 12px;
}

.file-item-top {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  color: var(--text);
  font-weight: 600;
}

.file-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.file-size {
  color: var(--text-muted);
  flex-shrink: 0;
}

.file-peer {
  color: var(--text-faint);
  margin-top: 2px;
}

.file-progress {
  height: 5px;
  border-radius: 3px;
  background: var(--border-soft);
  overflow: hidden;
  margin-top: 6px;
}

.file-progress-bar {
  height: 100%;
  width: 0%;
  background: var(--accent-ok);
  transition: width 0.15s;
}

.file-item-meta {
  display: flex;
  justify-content: space-between;
  color: var(--text-muted);
  font-size: 11px;
  margin-top: 4px;
}

.file-item-actions {
  display: flex;
  gap: 6px;
  margin-top: 8px;
}

.file-item-actions button {
  padding: 5px 10px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 11px;
  font-family: inherit;
  cursor: pointer;
}

.overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--overlay-scrim);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  transition: opacity 0.35s ease;
  z-index: 10;
}

.overlay.hidden {
  opacity: 0;
  pointer-events: none;
}

.card {
  width: min(360px, calc(100vw - 48px));
  background: var(--surface);
  border: 1.5px solid var(--border-strong);
  border-radius: 18px;
  padding: 28px 26px 24px;
  box-shadow: 0 12px 32px var(--shadow-strong);
  text-align: center;
}

.card h1 {
  margin: 0 0 8px;
  font-size: 19px;
  font-weight: 700;
  color: var(--text);
}

.card .sub {
  margin: 0 0 18px;
  font-size: 13px;
  line-height: 1.5;
  color: var(--text-muted);
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.card input[type='text'],
.card input[type='password'],
.card input:not([type]),
#name-input,
#restore-username,
.card textarea {
  width: 100%;
  padding: 11px 13px;
  border-radius: 10px;
  border: 1.5px solid var(--border);
  font-size: 14px;
  font-family: inherit;
  color: var(--text);
  background: var(--surface);
  outline: none;
  box-sizing: border-box;
}

.card input:focus,
.card textarea:focus {
  border-color: var(--border-strong);
}

.card textarea {
  resize: none;
  height: 68px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
  margin-top: 10px;
}

.status {
  min-height: 18px;
  font-size: 12px;
  margin-top: 8px;
  color: var(--text-faint);
}

.status.ok {
  color: var(--accent-ok);
}

.status.bad {
  color: var(--accent-bad);
}

.colors {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin: 16px 0;
}

.swatch {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: 1.5px solid var(--border);
  cursor: pointer;
  padding: 0;
  transition: transform 0.15s ease;
}

.swatch.selected {
  border-color: var(--border-strong);
  transform: scale(1.15);
}

.card button {
  width: 100%;
  padding: 11px;
  border-radius: 10px;
  border: 1.5px solid var(--border-strong);
  background: var(--text);
  color: var(--bg);
  font-size: 14px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  margin-top: 6px;
}

.card button:disabled {
  opacity: 0.4;
  cursor: default;
}

.card button#copy-recovery {
  background: var(--surface);
  color: var(--text);
}

.link {
  display: inline-block;
  margin-top: 14px;
  font-size: 12px;
  color: var(--text-faint);
  text-decoration: underline;
}

.restore-box {
  margin-top: 14px;
  padding-top: 14px;
  border-top: 1px solid var(--border-soft);
}

.restore-box.hidden {
  display: none;
}

.restore-box input {
  margin-bottom: 0;
}
