:root {
  --bg: #0e0f14; --panel: rgba(18, 20, 28, 0.88); --border: rgba(255,255,255,0.08);
  --text: #e8e8f0; --muted: #9aa0b4; --accent: #ffb347; --accent-2: #e94560; --ok: #2ecc71;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font-family: Inter, system-ui, sans-serif; overflow: hidden; }
body { touch-action: manipulation; -webkit-tap-highlight-color: transparent; }
canvas#game { display: block; width: 100vw; height: 100vh; height: 100dvh; image-rendering: pixelated; image-rendering: crisp-edges; touch-action: none; }
.pixel { font-family: 'Press Start 2P', monospace; }

.overlay { position: fixed; inset: 0; display: grid; place-items: center; background: radial-gradient(ellipse at center, rgba(40,30,50,0.85), rgba(10,10,16,0.97)); z-index: 20; }
.card { width: min(440px, 92vw); background: var(--panel); border: 1px solid var(--border); border-radius: 14px; padding: 28px; backdrop-filter: blur(8px); box-shadow: 0 20px 60px rgba(0,0,0,0.6); }
/* The page itself never scrolls, so a dialog taller than the viewport (e.g. the avatar editor with
   its swatch rows wrapped on a short window) scrolls internally instead of pushing Join/Save off-screen. */
.card { max-height: calc(100vh - 24px); overflow-y: auto; }
.card h1 { font-size: 20px; margin: 0 0 8px; color: var(--accent); letter-spacing: 1px; }
.card .sub { color: var(--muted); margin: 0 0 16px; font-size: 14px; }
.card .meta { font-size: 13px; color: var(--muted); margin-bottom: 14px; min-height: 18px; }
.card label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 14px; }
.card input[type=text], .card input[type=password], .card input:not([type]) { width: 100%; margin-top: 6px; padding: 12px 14px; border-radius: 8px; border: 1px solid var(--border); background: #0b0c11; color: var(--text); font-size: 15px; outline: none; }
.card input:focus { border-color: var(--accent); }
.card label.check { display: flex; align-items: center; gap: 8px; }
button.primary { width: 100%; padding: 13px; border: 0; border-radius: 8px; background: var(--accent); color: #1a1208; font-weight: 600; font-size: 15px; cursor: pointer; }
button.primary:hover { filter: brightness(1.08); }
button.primary:disabled { opacity: 0.6; cursor: wait; }
.error { color: var(--accent-2); font-size: 13px; margin: 10px 0 0; }
.hint { color: var(--muted); font-size: 12px; margin: 16px 0 0; text-align: center; }
kbd { background: #23252f; border: 1px solid #3a3d4a; border-bottom-width: 2px; border-radius: 4px; padding: 1px 5px; font-size: 11px; font-family: inherit; }

#hud { position: fixed; inset: 0; pointer-events: none; z-index: 10; }
#hud > * { pointer-events: auto; }
#topbar { position: absolute; top: 12px; left: 12px; display: flex; gap: 8px; align-items: center; }
.brand { font-size: 11px; color: var(--accent); background: var(--panel); border: 1px solid var(--border); padding: 10px 12px; border-radius: 8px; }
.chip { font-size: 13px; background: var(--panel); border: 1px solid var(--border); padding: 8px 12px; border-radius: 8px; color: var(--text); }
.chip.btn { cursor: pointer; }
.chip.btn:hover { border-color: var(--accent); }
.chip.ok { color: var(--ok); }

#side { position: absolute; top: 12px; right: 12px; bottom: 12px; width: 280px; display: flex; flex-direction: column; gap: 10px; }
#side section { background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 12px; display: flex; flex-direction: column; min-height: 0; }
#side h3 { margin: 0 0 8px; font-size: 12px; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); }
#people { list-style: none; margin: 0; padding: 0; max-height: 34vh; overflow: auto; font-size: 13px; }
#people li { display: flex; align-items: center; gap: 8px; padding: 5px 4px; border-radius: 6px; }
#people li.near { background: rgba(255,179,71,0.12); }
#people li .dot { width: 10px; height: 10px; border-radius: 50%; flex: none; }
#people li .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#people li .tag { font-size: 11px; color: var(--muted); }
#chat { flex: 1; }
#messages { flex: 1; overflow: auto; font-size: 13px; line-height: 1.45; min-height: 80px; }
#messages .msg b { color: var(--accent); font-weight: 600; }
#messages .msg.sys { color: var(--muted); font-style: italic; }
#chat-form { margin-top: 8px; }
#chat-input { width: 100%; padding: 9px 10px; border-radius: 8px; border: 1px solid var(--border); background: #0b0c11; color: var(--text); font-size: 13px; outline: none; }
#chat-input:focus { border-color: var(--accent); }

#bottombar { position: absolute; left: 12px; bottom: 12px; display: flex; gap: 8px; align-items: center; }
.ctl { background: var(--panel); border: 1px solid var(--border); color: var(--text); padding: 10px 14px; border-radius: 8px; font-size: 13px; cursor: pointer; }
.ctl.info { cursor: default; color: var(--muted); }
#mic-btn.live { border-color: var(--ok); color: var(--ok); }
#mic-btn.muted { border-color: var(--accent-2); color: #ff8fa3; }

#toast { position: fixed; left: 50%; top: 70px; transform: translateX(-50%); background: var(--panel); border: 1px solid var(--border); padding: 10px 16px; border-radius: 8px; font-size: 13px; z-index: 30; }

@media (max-width: 800px) {
  #side { width: 200px; }
  #bottombar .info { display: none; }
}

/* ---- Touch devices and small screens --------------------------------------------------------
   The side panel becomes a drawer behind the 💬 button, the bottom bar moves into that drawer, and
   the on-screen controls (#touch) take the bottom of the screen. */
.drawer-btn { display: none; position: relative; }
.badge { position: absolute; top: -6px; right: -6px; min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px; background: var(--accent-2); color: #fff; font-size: 11px; font-weight: 600; display: grid; place-items: center; }
#touch { position: fixed; inset: 0; pointer-events: none; z-index: 12; user-select: none; -webkit-user-select: none; }
#joy-zone { position: absolute; left: 0; bottom: 0; width: 55%; height: 70%; pointer-events: auto; touch-action: none; padding-left: env(safe-area-inset-left); }
#joy-base { position: absolute; width: 112px; height: 112px; margin: -56px 0 0 -56px; border-radius: 50%; background: rgba(255,255,255,0.10); border: 2px solid rgba(255,255,255,0.35); backdrop-filter: blur(2px); }
#joy-knob { position: absolute; left: 50%; top: 50%; width: 52px; height: 52px; margin: -26px 0 0 -26px; border-radius: 50%; background: rgba(255,179,71,0.85); box-shadow: 0 4px 12px rgba(0,0,0,0.4); }
#tc-buttons { position: absolute; right: max(14px, env(safe-area-inset-right)); bottom: max(18px, env(safe-area-inset-bottom)); display: flex; flex-direction: column; align-items: flex-end; gap: 10px; pointer-events: auto; }
.tc-row { display: flex; gap: 10px; }
.tc-small { width: 48px; height: 48px; border-radius: 50%; border: 1px solid rgba(255,255,255,0.25); background: rgba(18,20,28,0.8); color: var(--text); font-size: 20px; }
.tc-small.live { border-color: var(--ok); box-shadow: 0 0 0 2px rgba(46,204,113,0.35); }
.tc-small.off { opacity: 0.5; }
.tc-big { width: 84px; height: 84px; border-radius: 50%; border: 2px solid rgba(255,255,255,0.3); background: rgba(18,20,28,0.85); color: var(--text); display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; box-shadow: 0 6px 18px rgba(0,0,0,0.45); }
.tc-big .tc-icon { font-family: 'Press Start 2P', monospace; font-size: 22px; line-height: 1; }
.tc-big .tc-label { font-size: 9px; color: var(--muted); max-width: 72px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tc-big.ready { border-color: var(--accent); background: rgba(255,179,71,0.25); }
.tc-big.ready .tc-icon { color: var(--accent); }
.tc-big.busy { border-color: var(--accent); animation: pulse 0.8s ease-in-out infinite; }
@keyframes pulse { 0%,100% { transform: scale(1); } 50% { transform: scale(0.94); } }
.tc-big:active { transform: scale(0.94); }
#tc-emotes { display: grid; grid-template-columns: repeat(5, 40px); gap: 6px; padding: 8px; border-radius: 16px; background: rgba(18,20,28,0.9); border: 1px solid var(--border); }
#tc-emotes button { width: 40px; height: 40px; border-radius: 50%; border: 0; background: rgba(255,255,255,0.08); font-size: 20px; }

/* Emote menu (desktop bottom bar) */
#emote-wrap { position: relative; }
.emote-menu { position: absolute; bottom: 48px; left: 0; display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 4px; padding: 8px; background: var(--panel); border: 1px solid var(--border); border-radius: 10px; box-shadow: 0 10px 30px rgba(0,0,0,0.4); width: 340px; }
.emote-menu button { display: flex; align-items: center; gap: 6px; padding: 6px 8px; border: 0; border-radius: 6px; background: transparent; color: var(--text); font-size: 12px; cursor: pointer; text-align: left; white-space: nowrap; min-width: 0; }
.emote-menu button:hover { background: rgba(255,255,255,0.08); }
.emote-menu button .ic { font-size: 18px; }
.emote-menu button kbd { margin-left: auto; }
body.touch #emote-wrap { display: none; }

/* viewport-fit=cover lets the page run under notches and rounded corners; keep the HUD inside the safe area. */
body.touch #topbar { top: max(12px, env(safe-area-inset-top)); left: max(12px, env(safe-area-inset-left)); }
body.touch #topbar .brand { display: none; }
body.touch .drawer-btn { display: inline-block; }
body.touch #bottombar { position: static; flex-wrap: wrap; }
body.touch #bottombar .info { display: none; }
body.touch #side { transform: translateX(110%); transition: transform 0.2s ease; top: calc(48px + max(12px, env(safe-area-inset-top))); bottom: max(12px, env(safe-area-inset-bottom)); right: max(12px, env(safe-area-inset-right)); width: min(320px, 88vw); z-index: 15; }
body.touch.drawer-open #side { transform: none; }
body.touch #people { max-height: 26vh; }
body.touch #vitals { right: auto; left: max(12px, env(safe-area-inset-left)); bottom: auto; top: calc(48px + max(12px, env(safe-area-inset-top))); width: 200px; padding: 6px 10px; gap: 2px; }
body.touch .vital { grid-template-columns: 70px 1fr 26px; font-size: 10px; }
body.touch #vitals-hint { display: none; }
body.touch #action { left: 50%; right: auto; top: calc(46px + max(12px, env(safe-area-inset-top))); bottom: auto; transform: translateX(-50%); min-width: 0; width: max-content; max-width: calc(100vw - 24px); padding: 8px 12px; }
body.touch #action-icon { font-size: 22px; }
body.touch #action-title { font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
body.touch #action-sub { display: none; }
body.touch .card { padding: 20px; }
body.touch .hint.keys { display: none; }
body.touch .hint.touch-hint { display: block !important; }
@media (max-width: 600px) { body.touch #topbar .chip { font-size: 12px; padding: 6px 8px; } body.touch #copy-link { display: none; } body.touch #vitals { display: none; } }
@media (max-height: 700px) { body.touch #vitals { display: none; } }

/* Avatar editor */
.av-label { font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.av-editor { display: grid; grid-template-columns: 76px 1fr; gap: 10px 14px; align-items: start; margin-bottom: 14px; }
.av-preview { width: 76px; height: 92px; display: grid; place-items: center; background: #0b0c11; border: 1px solid var(--border); border-radius: 8px; }
.av-preview canvas { image-rendering: pixelated; }
.av-rows { display: flex; flex-direction: column; gap: 6px; }
.av-row { display: grid; grid-template-columns: 40px 1fr; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.av-row > div { display: flex; flex-wrap: wrap; gap: 4px; max-width: 100%; }
.av-row[data-k=skin] .sw, .av-row .sw.skin { border-radius: 50%; }
.sw { width: 20px; height: 20px; border-radius: 5px; border: 2px solid transparent; cursor: pointer; padding: 0; }
.sw.txt { width: auto; padding: 0 7px; font-size: 11px; background: #23252f; color: var(--text); }
.sw.active { border-color: var(--accent); box-shadow: 0 0 0 1px #000 inset; }
.av-random { grid-column: 1 / -1; justify-self: start; background: #23252f; color: var(--text); border: 1px solid var(--border); border-radius: 6px; padding: 5px 10px; font-size: 12px; cursor: pointer; }
.card.small { width: min(420px, 92vw); }
.card h2 { font-size: 14px; color: var(--accent); margin: 0 0 14px; }

/* Action panel (bottom right) */
#action { position: absolute; right: 12px; bottom: 12px; display: flex; gap: 12px; align-items: center; background: var(--panel); border: 1px solid var(--border); border-radius: 12px; padding: 12px 16px; min-width: 260px; max-width: 320px; box-shadow: 0 10px 30px rgba(0,0,0,0.4); }
#action.busy { border-color: var(--accent); }
#action-icon { font-size: 30px; line-height: 1; filter: drop-shadow(0 2px 2px rgba(0,0,0,0.4)); }
#action.busy #action-icon { animation: wobble 0.6s ease-in-out infinite; }
@keyframes wobble { 0%,100% { transform: rotate(-6deg); } 50% { transform: rotate(6deg) translateY(-2px); } }
#action-body { flex: 1; min-width: 0; }
#action-title { font-size: 13px; font-weight: 600; }
#action-sub { font-size: 12px; color: var(--muted); margin-top: 2px; }
#action-bar { height: 6px; background: #23252f; border-radius: 3px; margin-top: 8px; overflow: hidden; display: none; }
#action.busy #action-bar { display: block; }
#action-fill { height: 100%; width: 0%; background: var(--accent); transition: width 0.1s linear; }
#side { bottom: 84px; }

/* Whiteboard */
.wb-card { background: var(--panel); border: 1px solid var(--border); border-radius: 12px; padding: 10px; box-shadow: 0 20px 60px rgba(0,0,0,0.6); }
.wb-tools { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.wb-title { font-size: 11px; color: var(--accent); margin-right: 8px; }
.wb-sw { width: 24px; height: 24px; border-radius: 50%; border: 2px solid transparent; cursor: pointer; padding: 0; }
.wb-sw.active { border-color: var(--accent); }
.wb-sw.eraser { color: #333; font-size: 12px; }
.wb-tools .spacer { flex: 1; }
.wb-simple canvas { display: block; background: #fbfbf7; border-radius: 6px; cursor: crosshair; touch-action: none; } /* only the pen board: Excalidraw stacks a transparent canvas over its drawing canvas */
.wb-card { width: min(1200px, 96vw); }
.wb-note { font-size: 12px; color: var(--muted); }
.wb-excalidraw { height: min(78vh, 800px); border-radius: 8px; overflow: hidden; background: #fbfbf7; }
.wb-excalidraw .excalidraw { --color-primary: #e08a2e; }
.wb-loading { height: min(78vh, 800px); display: grid; place-items: center; color: var(--muted); background: #fbfbf7; border-radius: 8px; font-size: 14px; }
.wb-simple .wb-tools { margin-bottom: 6px; }
body.touch .wb-card { width: 100vw; max-height: 100vh; border-radius: 0; padding: 6px; }
body.touch .wb-excalidraw, body.touch .wb-loading { height: calc(100vh - 60px); height: calc(100dvh - 60px); }
body.touch .wb-note { display: none; }

/* Vitals (bottom right, above the action panel) */
#vitals { position: absolute; right: 12px; bottom: 96px; width: 260px; background: var(--panel); border: 1px solid var(--border); border-radius: 12px; padding: 10px 14px; display: flex; flex-direction: column; gap: 6px; }
#vitals.exhausted { border-color: var(--accent-2); }
#vitals.sleepy { border-color: #8a7cff; }
.vital { display: grid; grid-template-columns: 92px 1fr 32px; align-items: center; gap: 8px; font-size: 12px; }
.vlabel { color: var(--muted); }
.vbar { height: 8px; background: #23252f; border-radius: 4px; overflow: hidden; }
.vfill { height: 100%; width: 50%; transition: width 0.4s ease; }
.vfill.energy { background: linear-gradient(90deg, #f39c12, #2ecc71); }
.vfill.sleep { background: linear-gradient(90deg, #8a7cff, #e94560); }
.vval { text-align: right; color: var(--muted); font-variant-numeric: tabular-nums; }
#vitals-hint { font-size: 11px; color: var(--muted); min-height: 14px; }
#side { bottom: 200px; }
#action { bottom: 12px; }
@media (max-height: 700px) { #vitals { display: none; } #side { bottom: 84px; } }

/* Jukebox */
.jb-card { width: min(560px, 94vw); padding: 18px; }
.jb-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.jb-head h2 { margin: 0; }
#jb-player-wrap { aspect-ratio: 16 / 9; background: #000; border-radius: 8px; overflow: hidden; margin-bottom: 10px; }
#jb-player-wrap iframe, #jb-player { width: 100%; height: 100%; }
.jb-now { margin-bottom: 10px; font-size: 14px; font-weight: 600; }
#jukebox.playing .jb-now { color: var(--accent); }
.jb-now .muted { font-weight: 400; font-size: 12px; color: var(--muted); }
.jb-actions { display: flex; gap: 8px; margin-bottom: 10px; }
.jb-actions button { flex: 1; }
button.primary.small { width: auto; padding: 8px 14px; font-size: 13px; }

/* Mini-games */
.mg-card { width: min(680px, 96vw); padding: 14px 16px; }
.mg-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.mg-head h2 { margin: 0; }
.mg-screen { background: #000; border-radius: 8px; padding: 6px; box-shadow: inset 0 0 0 2px #1a1a26, 0 0 30px rgba(63,225,255,0.08); }
.mg-screen canvas { display: block; width: 100%; aspect-ratio: 320 / 200; image-rendering: pixelated; image-rendering: crisp-edges; touch-action: none; cursor: crosshair; border-radius: 4px; }
.mg-status { display: flex; justify-content: space-between; align-items: center; margin: 8px 2px 0; font-size: 10px; }
.mg-status .muted { font-family: Inter, sans-serif; font-size: 12px; color: var(--muted); }
.mg-over { display: none; margin-top: 10px; }
#minigame.over .mg-over { display: block; }
.mg-card .hint { margin-top: 8px; }
.mg-boardwrap { margin-top: 10px; border-top: 1px solid var(--border); padding-top: 8px; }
.mg-board-title { font-size: 11px; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); margin-bottom: 4px; }
#mg-board { margin: 0; padding: 0 0 0 18px; font-size: 13px; columns: 2; }
#mg-board li { display: flex; justify-content: space-between; gap: 8px; padding: 2px 6px 2px 0; break-inside: avoid; }
#mg-board li b { color: var(--accent); font-weight: 600; }
body.touch .mg-card { padding: 10px; }

/* Background music control */
.ctl.bgm { display: flex; align-items: center; gap: 8px; padding: 4px 10px 4px 4px; }
.ctl.bgm button { background: transparent; border: 0; color: var(--muted); font-size: 13px; padding: 6px 6px; cursor: pointer; border-radius: 6px; }
.ctl.bgm.on button { color: var(--accent); }
.ctl.bgm input[type=range] { width: 70px; accent-color: var(--accent); }

#people li.npc { opacity: 0.75; }
#people li.npc .name i { font-style: normal; font-size: 10px; color: var(--muted); margin-left: 4px; }

/* Room entry */
body.home { overflow: auto; height: auto; min-height: 100vh; background: radial-gradient(ellipse at top left, #30273a, #0e0f14 65%); }
.lobby { max-width: 1020px; margin: auto; padding: 48px 24px; }
.lobby header { margin-bottom: 36px; }
.lobby a { color: var(--accent); }
.lobby .brand { text-decoration: none; display: inline-block; }
.eyebrow { font-size: 11px; letter-spacing: 2px; margin-top: 40px; color: var(--accent); }
.lobby h1 { font-size: clamp(36px, 6vw, 64px); letter-spacing: -2px; line-height: 1.08; margin: 18px 0; }
.lobby header > p:last-child { color: var(--muted); line-height: 1.6; }
.room-options { display: grid; grid-template-columns: 1fr 1fr; gap: 24px; align-items: start; }
.lobby .card { width: 100%; max-height: none; overflow: visible; }
.lobby h2 { margin: 0 0 12px; font-size: 22px; }
.lobby .sub { line-height: 1.5; }
.card select, .card input[type=number] { width: 100%; padding: 12px; margin-top: 6px; color: var(--text); background: #0b0c11; border: 1px solid var(--border); border-radius: 8px; font: inherit; }
.room-created { margin-top: 24px; }
.primary-link { display: inline-block; padding: 14px; font-weight: 600; }
@media (max-width: 640px) { .room-options { grid-template-columns: 1fr; } .lobby { padding: 24px 16px; } .lobby .card { padding: 22px; } }
