:root {
  --s: 1;            /* touch controller size multiplier */
  --o: 0.55;         /* touch controller opacity */
  --pad-h: 44dvh;    /* portrait controller area */
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
html, body {
  margin: 0; height: 100%; background: #0d0d10; color: #e8e8ee;
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overflow: hidden; overscroll-behavior: none;
}
button, select, input { font: inherit; }
button {
  background: #2a2a35; color: #eee; border: 1px solid #444; border-radius: 8px;
  padding: .45em .8em; cursor: pointer;
}
button:hover { background: #363645; }
button.big { font-size: 1.2em; padding: .7em 1.4em; background: #5b3fd1; border-color: #7b62ea; }
.err { color: #ff7b7b; min-height: 1.2em; }
.hint { opacity: .6; font-size: .85em; }

/* ---------- join ---------- */
#join { position: fixed; inset: 0; display: grid; place-items: center; padding: 1em; }
#joinForm { display: grid; gap: .9em; width: min(420px, 100%); }
#joinForm h1 { margin: 0 0 .3em; font-size: 2em; letter-spacing: .02em; }
#joinForm label { display: grid; gap: .3em; }
#joinForm input { padding: .6em; border-radius: 8px; border: 1px solid #444; background: #1b1b22; color: #eee; }

/* ---------- stage / video ---------- */
#app { position: fixed; inset: 0; touch-action: none; user-select: none; -webkit-user-select: none; }
#stage { position: absolute; inset: 0; display: grid; place-items: center; }
#screen { position: relative; }
#screen canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
#video { image-rendering: pixelated; image-rendering: crisp-edges; background: #000; }
#overlay { pointer-events: none; }
#status {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%);
  background: #000a; padding: .6em 1em; border-radius: 8px; pointer-events: none;
}
#status:empty { display: none; }

#topbar {
  position: absolute; top: calc(6px + var(--safe-t)); left: calc(6px + var(--safe-l));
  right: calc(6px + var(--safe-r)); display: flex; gap: 6px; align-items: center;
  pointer-events: none; z-index: 5;
}
#topbar button { pointer-events: auto; background: #0008; border-color: #fff3; padding: .3em .6em; }
#badge { flex: 1; font-size: .85em; opacity: .75; text-shadow: 0 1px 2px #000; }
body.playing:not(.menu-open) #topbar { opacity: .35; transition: opacity .3s; }
body.playing #topbar:hover { opacity: 1; }

/* portrait + touch controller: video on top, controller below */
body.pad-on.portrait #stage { bottom: var(--pad-h); top: calc(40px + var(--safe-t)); }

/* ---------- touch controller ---------- */
#pad { position: absolute; inset: 0; pointer-events: none; z-index: 4; }
#pad > * { pointer-events: auto; opacity: var(--o); touch-action: none; }
body.portrait #pad { top: auto; height: var(--pad-h); }
body.portrait #pad > * { opacity: max(var(--o), .8); }

.cluster { position: absolute; width: calc(150px * var(--s)); height: calc(150px * var(--s)); }
#dpad { left: calc(22px + var(--safe-l)); bottom: calc(26px + var(--safe-b)); }
#face { right: calc(22px + var(--safe-r)); bottom: calc(26px + var(--safe-b)); width: calc(160px * var(--s)); height: calc(160px * var(--s)); }
body.portrait #dpad, body.portrait #face { bottom: calc(50% - 75px * var(--s) + 10px); }

.dp { position: absolute; background: #3a3a46; border: 2px solid #fff4; }
.dp.up, .dp.down { left: 33.3%; width: 33.4%; height: 36%; }
.dp.up { top: 0; border-radius: 8px 8px 0 0; border-bottom: 0; }
.dp.down { bottom: 0; border-radius: 0 0 8px 8px; border-top: 0; }
.dp.left, .dp.right { top: 33.3%; height: 33.4%; width: 36%; }
.dp.left { left: 0; border-radius: 8px 0 0 8px; border-right: 0; }
.dp.right { right: 0; border-radius: 0 8px 8px 0; border-left: 0; }
.dp.hub { left: 33.3%; top: 33.3%; width: 33.4%; height: 33.4%; border: 0; }
.dp.on { background: #8a7cf0; }

.fb {
  position: absolute; width: 36%; height: 36%; border-radius: 50%;
  display: grid; place-items: center; font-weight: 700; font-size: calc(18px * var(--s));
  border: 2px solid #fff5; color: #fff; text-shadow: 0 1px 2px #000;
}
.fb.x { left: 32%; top: 0; background: #2f5fd0; }
.fb.y { left: 0; top: 32%; background: #2b9b4b; }
.fb.a { right: 0; top: 32%; background: #c8302f; }
.fb.b { left: 32%; bottom: 0; background: #d6b21f; }
.fb.on, .pill.on, .shoulder.on { filter: brightness(1.6); transform: scale(.94); }

.shoulder {
  position: absolute; top: calc(48px + var(--safe-t)); width: calc(110px * var(--s)); height: calc(42px * var(--s));
  background: #3a3a46; border: 2px solid #fff4; display: grid; place-items: center; font-weight: 700;
}
.shoulder.left { left: calc(14px + var(--safe-l)); border-radius: 18px 6px 6px 6px; }
.shoulder.right { right: calc(14px + var(--safe-r)); border-radius: 6px 18px 6px 6px; }
body.portrait .shoulder { top: 8px; }

#mid {
  position: absolute; left: 50%; bottom: calc(14px + var(--safe-b)); transform: translateX(-50%);
  display: flex; gap: calc(14px * var(--s));
}
.pill {
  padding: calc(6px * var(--s)) calc(14px * var(--s)); border-radius: 99px; background: #3a3a46;
  border: 2px solid #fff4; font-size: calc(11px * var(--s)); font-weight: 700; letter-spacing: .05em;
}

/* ---------- menu ---------- */
#menu {
  position: absolute; top: 0; left: 0; bottom: 0; width: min(420px, 100%); z-index: 10;
  background: #15151cf2; border-right: 1px solid #333; overflow-y: auto;
  padding: calc(10px + var(--safe-t)) 14px calc(20px + var(--safe-b)) calc(14px + var(--safe-l));
  touch-action: pan-y; user-select: text;
}
#menu header { display: flex; justify-content: space-between; align-items: center; }
#menu section { border-top: 1px solid #333; margin-top: 10px; padding-top: 6px; }
#menu h3 { margin: .3em 0 .5em; font-size: 1em; opacity: .8; }
#menu label { display: block; margin: .4em 0; }
#menu select { background: #22222c; color: #eee; border: 1px solid #444; border-radius: 6px; padding: .25em; }
#menu .row { display: flex; flex-wrap: wrap; gap: 6px; margin: 8px 0; align-items: center; }
#menu .row select { flex: 1; min-width: 0; }
#players { width: 100%; border-collapse: collapse; font-size: .92em; }
#players td, #players th { padding: 3px 4px; text-align: left; }
#players tr.turn td:first-child::before { content: "▶ "; color: #8a7cf0; }
label.file { display: inline-block !important; }
label.file input { display: none; }
label.file { background: #2a2a35; border: 1px solid #444; border-radius: 8px; padding: .45em .8em; cursor: pointer; }

/* big screens / TVs: larger UI text */
@media (min-width: 1600px) { html, body { font-size: 18px; } }

/* ---------- links / misc ---------- */
a { color: #a99bff; }
a.btn {
  display: inline-block; background: #2a2a35; color: #eee; border: 1px solid #444; border-radius: 8px;
  padding: .45em .8em; text-decoration: none;
}
.crumb { margin: 0; }
#inviteLink {
  flex: 1; min-width: 0; background: #1b1b22; color: #ccc; border: 1px solid #444; border-radius: 6px;
  padding: .35em; font-size: .85em;
}

/* ---------- keyboard mapping dialog ---------- */
#keysModal {
  position: fixed; inset: 0; z-index: 20; background: #000a; display: grid; place-items: center;
  padding: 12px; overflow-y: auto; touch-action: pan-y;
}
.kbox {
  background: #17171f; border: 1px solid #333; border-radius: 14px; padding: 14px 18px;
  width: min(720px, 100%); max-height: 100%; overflow-y: auto;
}
.kbox header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 26px; }
.snes {   /* the controller body */
  position: relative; width: 100%; aspect-ratio: 2.35; margin: 10px 0 6px;
  background: linear-gradient(#d9d9e0, #b9b9c4); border-radius: 999px; color: #222;
  box-shadow: inset 0 -6px 0 #0002;
}
.kb {
  position: absolute; transform: translate(-50%, -50%); cursor: pointer; user-select: none;
  display: grid; place-items: center; font-weight: 800; text-align: center;
  transition: transform .08s, box-shadow .08s;
}
.kb:hover { box-shadow: 0 0 0 3px #8a7cf0; }
.kb .chip {
  position: absolute; left: 50%; top: 100%; transform: translate(-50%, 3px); white-space: nowrap;
  background: #111; color: #ffe36e; font: 700 12px/1 ui-monospace, monospace;
  padding: 3px 5px; border-radius: 5px; pointer-events: none;
}
.kb.dk { width: 11%; aspect-ratio: 1; background: #34343c; color: #ccc; border-radius: 6px; font-size: 12px; }
.kb.sh { width: 22%; height: 18%; background: #9c9caa; border-radius: 14px 14px 4px 4px; color: #222; }
.kb.sh { grid-auto-flow: column; gap: 8px; }
.kb.sh .chip { position: static; transform: none; }
.kb.pl { width: 12%; height: 9%; background: #55555f; color: #eee; border-radius: 99px; font-size: 9px; transform: translate(-50%, -50%) rotate(-25deg); }
.kb.pl .chip { transform: translate(-50%, 6px) rotate(25deg); }
.kb.fc { width: 12%; aspect-ratio: 1; border-radius: 50%; color: #fff; font-size: 18px; text-shadow: 0 1px 2px #0008; }
.kb.fc.x { background: #2f5fd0; } .kb.fc.y { background: #2b9b4b; }
.kb.fc.a { background: #c8302f; } .kb.fc.b { background: #d6b21f; }
.kb.listening, .keycap.listening { box-shadow: 0 0 0 4px #ffe36e; animation: blink .8s infinite; }
@keyframes blink { 50% { box-shadow: 0 0 0 4px #ffe36e44; } }
#keysTable { width: 100%; border-collapse: collapse; margin-top: 30px; }
#keysTable td, #keysTable th { padding: 3px 6px; text-align: left; }
.keycap {
  min-width: 6.5em; font: 700 13px ui-monospace, monospace; background: #24242e; border-bottom-width: 3px;
}
.kbox .row { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin-top: 10px; }

/* key hints on the on-screen controller (desktop) */
body.keyhints #pad [data-key]:not([data-key=""])::after,
body.keyhints #dpad .dp[data-key]:not([data-key=""])::after {
  content: attr(data-key); position: absolute; font: 700 11px/1 ui-monospace, monospace;
  background: #000c; color: #ffe36e; padding: 2px 4px; border-radius: 4px; pointer-events: none;
  right: -4px; bottom: -4px;
}
#pad [data-btn], #dpad .dp { position: absolute; }
#mid .pill { position: relative; }
body.keyhints #dpad .dp[data-key]:not([data-key=""])::after {
  right: auto; bottom: auto; left: 50%; top: 50%; transform: translate(-50%, -50%); z-index: 1;
}
button.danger { background: #5a1f24; border-color: #a33; }
button.danger:hover { background: #7a252c; }
#joinForm label.check { display: flex; gap: 8px; align-items: flex-start; }
#joinForm label.check input { width: auto; margin-top: 4px; }
