:root {
  --ink: #1C2238;
  --panel: #232A45;
  --panel-2: #2F3859;
  --line: rgba(255, 255, 255, 0.08);
  --text: #F2F4FA;
  --muted: #A9B1CC;
  --signal: #FFC83D;
  --danger: #F06464;
  font-family: Lexend, system-ui, -apple-system, "Segoe UI", sans-serif;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; background: var(--ink); color: var(--text); }
[hidden] { display: none !important; }
button, input { font: inherit; }
:focus-visible { outline: 2px solid var(--signal); outline-offset: 2px; }

/* Join screen: the office grid itself is the backdrop */
.join, .lost {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 24px;
  background-color: #262D4A;
  background-image:
    linear-gradient(rgba(255,255,255,0.04) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255,255,255,0.04) 1px, transparent 1px);
  background-size: 32px 32px;
}
.lost { position: fixed; inset: 0; background-color: rgba(28,34,56,0.92); z-index: 10; }
.join-card {
  width: min(720px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 28px;
  display: grid;
  gap: 18px;
}
.join-card h1 { margin: 0; font-size: 2rem; font-weight: 600; letter-spacing: -0.01em; }
.join-card p { margin: 0; color: var(--muted); line-height: 1.55; }
.join-card label { display: grid; gap: 6px; font-size: 0.9rem; color: var(--muted); }
#name, #chat-input {
  background: var(--ink);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  color: var(--text);
  width: 100%;
}
/* Character builder (join card: compact row; editing happens in the Edit Avatar modal) */
.builder {
  display: grid; grid-template-columns: 120px minmax(0, 1fr); gap: 18px; align-items: center;
  padding: 14px; border-radius: 16px; background: var(--ink); border: 1px solid var(--line);
}
#preview {
  width: 120px; height: 120px; image-rendering: pixelated; cursor: pointer; display: block;
  border-radius: 14px; border: 1px solid var(--line);
  background:
    radial-gradient(ellipse at 50% 85%, rgba(255,255,255,0.08), transparent 60%),
    linear-gradient(180deg, #2b3357 0%, #1f2542 100%);
}
.builder-info { display: grid; gap: 4px; min-width: 0; }
.builder-kicker { font-size: 0.74rem; letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted); font-weight: 600; }
#avatar-name { font-size: 1.25rem; font-weight: 600; }
.builder-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.ghost {
  display: inline-flex; align-items: center; gap: 7px;
  background: transparent; color: var(--text); border: 1px solid var(--line); border-radius: 999px;
  padding: 8px 14px; font-size: 0.85rem; cursor: pointer;
}
.ghost:hover { background: rgba(255,255,255,0.06); }
.ghost svg { width: 15px; height: 15px; }
@media (max-width: 520px) {
  .builder { grid-template-columns: 1fr; justify-items: center; text-align: center; }
  .builder-actions { justify-content: center; }
}
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* Edit Avatar modal */
.am {
  --am-accent: var(--signal);
  width: min(960px, calc(100vw - 32px)); max-width: none; max-height: calc(100vh - 32px);
  padding: 0; border: 1px solid var(--line); border-radius: 18px; overflow: hidden;
  background: var(--panel); color: var(--text);
  box-shadow: 0 30px 80px rgba(0,0,0,0.55);
  display: none; grid-template-rows: auto minmax(0, 1fr) auto;
}
.am[open] { display: grid; }
.am::backdrop { background: rgba(10, 13, 24, 0.7); -webkit-backdrop-filter: blur(4px); backdrop-filter: blur(4px); }
.am-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 18px 16px 22px; border-bottom: 1px solid var(--line);
}
.am-head h2 { margin: 0; font-size: 1.15rem; font-weight: 600; }
.am-x {
  display: grid; place-items: center; width: 34px; height: 34px; border: 0; border-radius: 10px;
  background: transparent; color: var(--muted); cursor: pointer;
}
.am-x:hover { background: rgba(255,255,255,0.08); color: var(--text); }
.am-x svg { width: 18px; height: 18px; }

.am-body {
  display: grid; grid-template-columns: 200px minmax(0, 1fr) 280px; min-height: 0;
  height: min(560px, calc(100vh - 32px - 70px - 74px));
}
/* left: sections */
.am-nav { border-right: 1px solid var(--line); overflow-y: auto; padding: 14px 10px; scrollbar-width: thin; }
.am-nav ul { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; }
.am-sec {
  width: 100%; display: flex; align-items: center; gap: 12px; padding: 7px 12px 7px 8px;
  border: 0; border-radius: 12px; background: transparent; color: var(--text); cursor: pointer;
  font-size: 0.95rem; text-align: left; transition: background 0.12s;
}
.am-sec:hover { background: rgba(255,255,255,0.06); }
.am-sec.selected { background: var(--am-accent); color: var(--ink); font-weight: 600; }
.am-sec canvas { width: 36px; height: 36px; image-rendering: pixelated; border-radius: 9px; background: rgba(0,0,0,0.22); flex: none; }
.am-sec.selected canvas { background: rgba(28,34,56,0.28); }

/* centre: options grid + swatches */
.am-options { display: grid; grid-template-rows: minmax(0, 1fr) auto; gap: 12px; padding: 16px; min-width: 0; min-height: 0; }
.am-grid-wrap { background: var(--ink); border-radius: 14px; overflow-y: auto; padding: 14px; scrollbar-width: thin; min-height: 0; }
.am-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(88px, 1fr)); gap: 10px; }
.am-grid.disabled { opacity: 0.45; pointer-events: none; filter: grayscale(0.4); }
.am-opt {
  position: relative; display: grid; place-items: center; gap: 4px; padding: 6px 4px;
  aspect-ratio: 1; border-radius: 12px; cursor: pointer;
  background: rgba(255,255,255,0.03); border: 2px solid transparent; color: var(--muted);
  transition: border-color 0.12s, background 0.12s, transform 0.12s;
}
.am-opt:hover { background: rgba(255,255,255,0.08); transform: translateY(-1px); }
.am-opt.selected { border-color: var(--am-accent); background: rgba(255,200,61,0.08); color: var(--text); }
.am-opt.selected::after {
  content: "✓"; position: absolute; top: -8px; right: -8px; width: 18px; height: 18px; border-radius: 50%;
  display: grid; place-items: center; font-size: 11px; font-weight: 700; line-height: 1;
  background: var(--am-accent); color: var(--ink); box-shadow: 0 0 0 2px var(--ink);
}
.am-opt canvas { image-rendering: pixelated; display: block; width: 72px; height: 72px; }
.am-opt-name { font-size: 0.74rem; line-height: 1; }
.am-note { margin: 12px 2px 0; font-size: 0.8rem; color: var(--muted); line-height: 1.45; }
.am-swatches { display: flex; flex-wrap: wrap; gap: 8px; padding: 12px 14px; background: var(--ink); border-radius: 14px; }
.am-swatches.disabled { opacity: 0.4; pointer-events: none; }
.am-sw {
  width: 28px; height: 28px; border-radius: 50%; cursor: pointer; padding: 0;
  background: var(--c); border: 2px solid transparent;
  box-shadow: inset 0 0 0 1px rgba(0,0,0,0.25), 0 0 0 2px var(--ink);
  transition: transform 0.12s, box-shadow 0.12s;
}
.am-sw:hover { transform: scale(1.08); }
.am-sw.selected { box-shadow: inset 0 0 0 1px rgba(0,0,0,0.25), 0 0 0 2px var(--ink), 0 0 0 4px var(--am-accent); }

/* right: preview on a tiny office scene (canvas is resized to its box every frame) */
.am-stage { position: relative; padding: 16px 16px 16px 0; min-height: 0; }
#am-preview {
  display: block; width: 100%; height: 100%; image-rendering: pixelated;
  border-radius: 14px; cursor: pointer; border: 1px solid var(--line); background: #2f3859;
}
.am-dice {
  position: absolute; right: 28px; bottom: 28px; width: 40px; height: 40px; border: 0; border-radius: 11px;
  display: grid; place-items: center; cursor: pointer;
  background: rgba(28,34,56,0.9); color: var(--text); box-shadow: 0 6px 18px rgba(0,0,0,0.35);
  transition: background 0.12s, transform 0.12s;
}
.am-dice:hover { background: var(--am-accent); color: var(--ink); transform: rotate(-12deg) scale(1.05); }
.am-dice svg { width: 20px; height: 20px; }

.am-foot {
  display: flex; justify-content: flex-end; gap: 10px; padding: 14px 18px; border-top: 1px solid var(--line);
  background: rgba(0,0,0,0.12);
}
.am-foot .primary { padding: 10px 22px; }
.am-foot .ghost { padding: 9px 18px; border-radius: 10px; }

@media (max-width: 900px) {
  .am-body { grid-template-columns: 168px minmax(0, 1fr) 220px; }
  .am-sec { font-size: 0.88rem; gap: 10px; }
  .am-sec canvas { width: 32px; height: 32px; }
  .am-options { padding: 14px 12px; }
  .am-grid { grid-template-columns: repeat(auto-fill, minmax(80px, 1fr)); gap: 8px; }
  .am-opt canvas { width: 64px; height: 64px; }
}
@media (max-width: 640px) {
  /* sections become a horizontal strip on top; options and preview share the row below */
  .am-body { grid-template-columns: minmax(0, 1fr) 42%; grid-template-rows: auto minmax(0, 1fr); height: min(620px, calc(100vh - 32px - 70px - 74px)); }
  .am-nav { grid-column: 1 / -1; border-right: 0; border-bottom: 1px solid var(--line); padding: 8px 10px; overflow-x: auto; overflow-y: hidden; }
  .am-nav ul { display: flex; gap: 4px; }
  .am-sec { width: auto; flex: none; flex-direction: column; gap: 4px; font-size: 0.7rem; padding: 6px 8px; }
  .am-sec canvas { width: 30px; height: 30px; }
  .am-options { padding: 12px 8px 12px 12px; gap: 8px; }
  .am-grid { grid-template-columns: repeat(auto-fill, minmax(64px, 1fr)); gap: 6px; }
  .am-opt canvas { width: 52px; height: 52px; }
  .am-opt-name { font-size: 0.66rem; }
  .am-swatches { gap: 6px; padding: 8px 10px; }
  .am-sw { width: 24px; height: 24px; }
  .am-stage { padding: 12px 12px 12px 0; }
  .am-dice { right: 20px; bottom: 20px; width: 34px; height: 34px; }
}
.primary {
  background: var(--signal); color: var(--ink); border: 0; border-radius: 10px;
  padding: 12px; font-weight: 600; cursor: pointer;
}
.primary:disabled { opacity: 0.6; cursor: progress; }
.join-card .hint { font-size: 0.82rem; }

/* App layout: the map fills the screen; everything else floats over it */
#app { height: 100%; }
/* The game screen owns all touch gestures (so the browser never zooms the whole page);
   scrollable overlays below opt back in to their own panning. */
.stage { position: relative; height: 100%; min-width: 0; min-height: 0; touch-action: none; overscroll-behavior: none; }
.dock { touch-action: pan-x; }
.chatpanel, #messages, .device-panel { touch-action: pan-y; }
#world { display: block; width: 100%; height: 100%; cursor: grab; touch-action: none; }
#world:active { cursor: grabbing; }
.zone-status {
  position: absolute; top: 158px; left: 50%; transform: translateX(-50%);
  background: rgba(28,34,56,0.9); padding: 8px 14px; border-radius: 999px; font-size: 0.85rem;
  white-space: nowrap; max-width: calc(100% - 24px); overflow: hidden; text-overflow: ellipsis;
}
/* "Eric entered the Meeting room" notice, under the status pill */
.room-toast {
  position: absolute; top: 196px; left: 50%; transform: translateX(-50%) translateY(-6px);
  background: rgba(28,34,56,0.9); padding: 6px 12px; border-radius: 999px; font-size: 0.8rem;
  white-space: nowrap; max-width: calc(100% - 24px); overflow: hidden; text-overflow: ellipsis;
  opacity: 0; transition: opacity 0.25s ease, transform 0.25s ease; pointer-events: none;
}
.room-toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
/* Modern floating control bar */
.controlbar {
  position: absolute; bottom: 18px; left: 50%; transform: translateX(-50%);
  display: flex; align-items: center; gap: 6px; z-index: 5;
  padding: 7px; border-radius: 20px;
  background: rgba(18, 20, 28, 0.72);
  border: 1px solid rgba(255, 255, 255, 0.09);
  box-shadow: 0 14px 40px rgba(0, 0, 0, 0.45), inset 0 1px 0 rgba(255, 255, 255, 0.05);
  -webkit-backdrop-filter: blur(16px); backdrop-filter: blur(16px);
}
.cb-sep { width: 1px; height: 26px; background: rgba(255, 255, 255, 0.1); margin: 0 2px; }

.cb-group { display: flex; align-items: center; gap: 1px; }
.cb-btn {
  position: relative; display: inline-grid; place-items: center;
  width: 46px; height: 42px; border: 0; border-radius: 13px; cursor: pointer;
  background: rgba(255, 255, 255, 0.06); color: #eef0f8;
  transition: background 0.14s ease, color 0.14s ease, transform 0.08s ease;
}
.cb-btn:hover { background: rgba(255, 255, 255, 0.13); }
.cb-btn:active { transform: scale(0.94); }
.cb-btn .cb-ico { width: 21px; height: 21px; }
.cb-group .cb-btn { border-radius: 13px 4px 4px 13px; }
.cb-chevron {
  display: inline-grid; place-items: center; width: 22px; height: 42px; border: 0; cursor: pointer;
  background: rgba(255, 255, 255, 0.06); color: #9aa2bd; border-radius: 4px 13px 13px 4px;
  transition: background 0.14s ease, color 0.14s ease;
}
.cb-chevron:hover { background: rgba(255, 255, 255, 0.13); color: #fff; }
.cb-chevron svg { width: 14px; height: 14px; }
.cb-chevron[aria-expanded="true"] { background: rgba(255, 255, 255, 0.16); color: #fff; }

/* muted / video-off states get a red tint + diagonal slash */
.cb-btn .cb-slash { position: absolute; width: 21px; height: 21px; color: #ff6b6b; display: none; }
.cb-btn[aria-pressed="false"] { background: rgba(240, 90, 90, 0.16); color: #ff6b6b; }
.cb-btn[aria-pressed="false"]:hover { background: rgba(240, 90, 90, 0.26); }
.cb-btn[aria-pressed="false"] .cb-slash { display: block; }
.cb-btn:disabled { opacity: 0.4; cursor: not-allowed; }
.cb-btn.leave:hover { background: rgba(240, 90, 90, 0.22); color: #ff8080; }

.reaction-bar {
  position: absolute; bottom: 74px; left: 50%; transform: translateX(-50%); z-index: 6;
  display: flex; gap: 2px; padding: 6px; border-radius: 16px;
  background: rgba(18, 20, 28, 0.9); border: 1px solid rgba(255, 255, 255, 0.09);
  box-shadow: 0 14px 36px rgba(0, 0, 0, 0.45);
  -webkit-backdrop-filter: blur(16px); backdrop-filter: blur(16px);
}
.reaction-bar button {
  width: 42px; height: 42px; border: 0; background: transparent; border-radius: 11px;
  font-size: 1.4rem; line-height: 1; cursor: pointer; transition: background 0.12s, transform 0.12s;
}
.reaction-bar button:hover { background: rgba(255, 255, 255, 0.12); transform: scale(1.18); }

.device-panel {
  position: absolute; bottom: 74px; left: 50%; transform: translateX(-50%);
  width: min(290px, calc(100% - 24px)); z-index: 6;
  background: rgba(18, 20, 28, 0.94); border: 1px solid rgba(255, 255, 255, 0.09);
  border-radius: 16px; padding: 14px; display: grid; gap: 12px;
  box-shadow: 0 14px 40px rgba(0, 0, 0, 0.5);
  -webkit-backdrop-filter: blur(16px); backdrop-filter: blur(16px);
}
.device-panel label { display: grid; gap: 5px; font-size: 0.8rem; color: var(--muted); }
.device-panel select {
  background: rgba(255, 255, 255, 0.05); color: var(--text); border: 1px solid var(--line);
  border-radius: 9px; padding: 8px 10px; width: 100%; cursor: pointer;
}
.device-panel select:disabled { opacity: 0.5; cursor: not-allowed; }
.device-note { margin: 0; font-size: 0.78rem; color: var(--muted); line-height: 1.45; }

/* Map zoom pill (bottom-right); only the canvas world scales, never this UI */
.zoomctl {
  position: absolute; right: 14px; bottom: 18px; z-index: 5;
  display: flex; flex-direction: column; align-items: stretch; gap: 2px;
  padding: 5px; border-radius: 14px;
  background: rgba(18, 20, 28, 0.72);
  border: 1px solid rgba(255, 255, 255, 0.09);
  box-shadow: 0 14px 40px rgba(0, 0, 0, 0.45), inset 0 1px 0 rgba(255, 255, 255, 0.05);
  -webkit-backdrop-filter: blur(16px); backdrop-filter: blur(16px);
}
.zoomctl button {
  display: grid; place-items: center; width: 36px; height: 34px; border: 0; border-radius: 10px;
  background: rgba(255, 255, 255, 0.06); color: #eef0f8; cursor: pointer;
  transition: background 0.14s ease, color 0.14s ease, transform 0.08s ease;
}
.zoomctl button:hover { background: rgba(255, 255, 255, 0.13); }
.zoomctl button:active { transform: scale(0.94); }
.zoomctl button:disabled { opacity: 0.35; cursor: default; transform: none; background: rgba(255, 255, 255, 0.06); }
.zoomctl svg { width: 16px; height: 16px; }
#zoom-level {
  height: 26px; background: transparent; color: var(--muted);
  font-size: 0.68rem; font-weight: 600; letter-spacing: 0.02em; font-variant-numeric: tabular-nums;
}
#zoom-level:hover { color: var(--text); }

/* Virtual joystick (mobile): appears under a single finger on the map */
.joystick {
  position: absolute; z-index: 6; width: 116px; height: 116px; margin: -58px 0 0 -58px;
  border-radius: 50%; pointer-events: none; touch-action: none;
  background: radial-gradient(circle at 50% 50%, rgba(255,255,255,0.10), rgba(18,20,28,0.30) 70%);
  border: 2px solid rgba(255,255,255,0.18);
  box-shadow: 0 8px 30px rgba(0,0,0,0.4);
}
.joy-knob {
  position: absolute; top: 50%; left: 50%; width: 50px; height: 50px; margin: -25px 0 0 -25px;
  border-radius: 50%; background: rgba(255,255,255,0.85);
  border: 2px solid rgba(18,20,28,0.25); box-shadow: 0 4px 14px rgba(0,0,0,0.35);
}

/* Floating participant video windows: a centred frame at the top that groups the call */
.dock {
  position: absolute; top: 12px; left: 50%; transform: translateX(-50%); z-index: 5;
  display: flex; gap: 8px; align-items: flex-start;
  max-width: calc(100% - 20px); overflow-x: auto; overflow-y: hidden;
  padding: 8px; border-radius: 18px; scrollbar-width: thin;
  background: rgba(18, 20, 28, 0.55);
  border: 1px solid rgba(255, 255, 255, 0.1);
  box-shadow: 0 16px 44px rgba(0, 0, 0, 0.45), inset 0 1px 0 rgba(255, 255, 255, 0.05);
  -webkit-backdrop-filter: blur(14px); backdrop-filter: blur(14px);
}
.dock.empty { display: none; } /* hidden while you're alone; shows once others join the call */
.dock::-webkit-scrollbar { height: 6px; }
.dock::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.2); border-radius: 3px; }
.tile {
  position: relative; flex: none; width: 164px; aspect-ratio: 4 / 3;
  background: rgba(13,15,22,0.92); border-radius: 12px; overflow: hidden;
  border: 1px solid rgba(255,255,255,0.08);
}
.tile video { width: 100%; height: 100%; object-fit: cover; display: block; }
.tile.self video { transform: scaleX(-1); }
.tile .ph {
  position: absolute; inset: 0; display: none; place-items: center;
  font-size: 1.6rem; font-weight: 600; color: var(--muted);
}
.tile.cam-off video, .tile.no-video video { visibility: hidden; }
.tile.cam-off .ph, .tile.no-video .ph { display: grid; }
.tile[data-state="new"], .tile[data-state="connecting"] { opacity: 0.6; }
.tile .name {
  position: absolute; left: 6px; bottom: 6px; font-size: 0.72rem;
  background: rgba(28,34,56,0.85); padding: 2px 8px; border-radius: 999px;
  max-width: calc(100% - 12px); overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.tile.mic-off .name::before {
  content: ""; display: inline-block; width: 7px; height: 7px; border-radius: 50%;
  background: var(--danger); margin-right: 5px; vertical-align: 1px;
}

/* Unread dot on the control-bar chat button */
.cb-badge {
  position: absolute; top: 3px; right: 3px; width: 9px; height: 9px;
  border-radius: 999px; background: var(--signal); box-shadow: 0 0 0 2px rgba(18,20,28,0.9);
}

/* Floating chat history panel (toggled from the control bar) */
.chatpanel {
  position: absolute; left: 12px; bottom: 74px; z-index: 7;
  width: min(340px, calc(100% - 24px)); max-height: min(60vh, 440px);
  display: grid; grid-template-rows: auto minmax(0, 1fr) auto;
  background: rgba(18,20,28,0.92); border: 1px solid rgba(255,255,255,0.1);
  border-radius: 16px; overflow: hidden;
  box-shadow: 0 18px 50px rgba(0,0,0,0.5);
  -webkit-backdrop-filter: blur(16px); backdrop-filter: blur(16px);
}
.chatpanel-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 10px 10px 14px; border-bottom: 1px solid var(--line);
  font-size: 0.85rem; font-weight: 600; color: var(--muted);
}
.chatpanel-x {
  display: grid; place-items: center; width: 28px; height: 28px; border: 0; border-radius: 8px;
  background: transparent; color: var(--muted); cursor: pointer;
}
.chatpanel-x:hover { background: rgba(255,255,255,0.08); color: var(--text); }
.chatpanel-x svg { width: 16px; height: 16px; }
#messages {
  list-style: none; margin: 0; padding: 12px; overflow-y: auto;
  display: flex; flex-direction: column; gap: 8px; font-size: 0.88rem; line-height: 1.45;
}
#messages:empty::before { content: "No messages yet."; color: var(--muted); }
#messages .who { font-weight: 600; margin-right: 6px; }
#messages .sys { color: var(--muted); }
#chat-form { padding: 10px; border-top: 1px solid var(--line); }

@media (max-width: 760px) {
  .tile { width: 128px; }
  .dock { top: 8px; max-width: calc(100% - 16px); }
  .zone-status { top: 126px; }
  .room-toast { top: 160px; }
  .chatpanel { left: 8px; right: 8px; width: auto; bottom: 80px; }
  .zoomctl { bottom: 86px; } /* clear the centred control bar on narrow stages */
}
