:root {
  --bg: #101020;
  --panel: #1c1c34;
  --panel2: #26264a;
  --ink: #f4f4ff;
  --muted: #9a9ac0;
  --accent: #f8b800;
  --accent2: #e03c28;
  --good: #58c840;
  --font: 'Press Start 2P', ui-monospace, Menlo, Consolas, monospace;
}
* {
  box-sizing: border-box;
}
html,
body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  font-size: 12px;
  line-height: 1.6;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}
[hidden] {
  display: none !important;
}
button,
input {
  font-family: inherit;
  font-size: inherit;
  color: inherit;
}
button {
  background: var(--panel2);
  border: 2px solid #000;
  box-shadow: inset -3px -3px 0 rgba(0, 0, 0, 0.35), inset 3px 3px 0 rgba(255, 255, 255, 0.15);
  padding: 10px 14px;
  cursor: pointer;
  border-radius: 0;
}
button:hover:not(:disabled) {
  filter: brightness(1.2);
}
button:active:not(:disabled) {
  transform: translateY(1px);
}
button:disabled {
  opacity: 0.45;
  cursor: default;
}
button.big {
  background: var(--accent2);
  font-size: 14px;
  padding: 14px 20px;
}
button.ghost {
  background: transparent;
  border-color: #444;
}
button.tiny {
  padding: 4px 8px;
  font-size: 12px;
}
input {
  background: #0a0a18;
  border: 2px solid #000;
  box-shadow: inset 2px 2px 0 rgba(0, 0, 0, 0.5);
  padding: 10px;
  outline: none;
  user-select: text;
}
input:focus {
  border-color: var(--accent);
}
.muted {
  color: var(--muted);
}
.small {
  font-size: 9px;
}
.red {
  color: #f85858;
}
.row {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
}

#game {
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  background: #000;
}
body:not([data-screen='game']) #game {
  filter: blur(2px) brightness(0.35);
}

.screen {
  position: fixed;
  inset: 0;
  overflow-y: auto;
  padding: 16px;
  padding-top: max(16px, env(safe-area-inset-top));
  background: radial-gradient(ellipse at top, #2a2a60, var(--bg) 70%);
}

/* ---------------- title ---------------- */
.title-card {
  max-width: 560px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
  align-items: stretch;
}
.logo {
  margin: 12px 0 0;
  text-align: center;
  font-size: clamp(30px, 9vw, 52px);
  line-height: 1.15;
  letter-spacing: 2px;
  display: flex;
  flex-direction: column;
}
.logo span:first-child {
  color: var(--accent);
  text-shadow: 4px 4px 0 #a03c00, 8px 8px 0 #000;
}
.logo span:last-child {
  color: #fff;
  text-shadow: 4px 4px 0 var(--accent2), 8px 8px 0 #000;
}
.tagline {
  text-align: center;
  color: var(--muted);
  margin: 0;
  font-size: 10px;
}
#title-anim {
  width: 100%;
  image-rendering: pixelated;
  aspect-ratio: 320 / 56;
}
.field label {
  display: block;
  margin-bottom: 6px;
  color: var(--muted);
  font-size: 10px;
}
.field input {
  width: 100%;
}
.swatches {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.swatch {
  padding: 4px;
  width: 48px;
  height: 48px;
}
.swatch canvas {
  width: 32px;
  height: 32px;
  image-rendering: pixelated;
}
.swatch.sel {
  border-color: var(--accent);
  background: #3a3a70;
}
.actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.actions .row > * {
  flex: 1;
}
.actions .row .check {
  flex: 0 0 auto;
}
#join-code {
  text-transform: uppercase;
  letter-spacing: 4px;
  text-align: center;
}
.check {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 10px;
  cursor: pointer;
}
.check input {
  width: 18px;
  height: 18px;
}
.status {
  min-height: 1.6em;
  color: var(--accent);
  text-align: center;
  font-size: 10px;
}
details {
  background: var(--panel);
  border: 2px solid #000;
  padding: 10px 12px;
  font-size: 10px;
}
details summary {
  cursor: pointer;
  color: var(--accent);
}
details ul {
  padding-left: 18px;
  margin: 10px 0 0;
  line-height: 2;
}
.room-row {
  display: flex;
  justify-content: space-between;
  width: 100%;
  margin-top: 6px;
  font-size: 10px;
}

/* ---------------- lobby ---------------- */
.lobby-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 14px;
  max-width: 1200px;
  margin: 0 auto;
}
.card {
  background: var(--panel);
  border: 3px solid #000;
  box-shadow: 6px 6px 0 rgba(0, 0, 0, 0.4);
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.card h3 {
  margin: 6px 0 0;
  font-size: 12px;
  color: var(--accent);
}
.room-head {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
}
.code {
  font-size: 34px;
  letter-spacing: 6px;
  color: #fff;
  text-shadow: 3px 3px 0 var(--accent2);
}
.invite {
  display: flex;
  flex-direction: column;
  gap: 6px;
  align-items: flex-end;
}
.player-row {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--panel2);
  padding: 6px 10px;
  min-height: 46px;
}
.player-row.empty {
  opacity: 0.5;
}
.player-row canvas {
  width: 32px;
  height: 32px;
  image-rendering: pixelated;
}
.pname {
  flex: 1;
  font-size: 11px;
}
.ptag {
  font-size: 9px;
  color: var(--accent);
}
.world-row {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 6px;
  flex-wrap: wrap;
}
.wname {
  width: 100%;
  font-size: 9px;
  color: var(--muted);
}
.wname small {
  color: #6a6a90;
}
.lvl {
  padding: 6px 8px;
  font-size: 10px;
}
.lvl.sel {
  background: var(--accent);
  color: #000;
}
#levels {
  max-height: 46vh;
  overflow-y: auto;
}
.chat-log {
  font-size: 10px;
  line-height: 1.7;
  overflow-y: auto;
  user-select: text;
  word-break: break-word;
}
.card.chat .chat-log {
  height: 260px;
  background: #0a0a18;
  padding: 8px;
}
.chat-line.sys {
  color: var(--muted);
  font-style: italic;
}

/* ---------------- in-game DOM ---------------- */
#ingame {
  position: fixed;
  inset: 0;
  pointer-events: none;
}
#ingame > * {
  pointer-events: auto;
}
#game-chat-log {
  position: absolute;
  left: 10px;
  bottom: 60px;
  width: min(420px, 70vw);
  max-height: 30vh;
  pointer-events: none;
  text-shadow: 2px 2px 0 #000;
}
#game-chat-log .chat-line {
  transition: opacity 1s;
}
#game-chat-log .chat-line.fade {
  opacity: 0;
}
#game-chat-log.open .chat-line.fade {
  opacity: 1;
}
#game-chat-log.open {
  background: rgba(0, 0, 0, 0.55);
  padding: 6px;
}
#game-chat-input {
  position: absolute;
  left: 10px;
  bottom: 14px;
  width: min(420px, 80vw);
}
#game-chat-input input {
  width: 100%;
}
.corner {
  position: absolute;
  right: 10px;
  bottom: 10px;
  display: flex;
  gap: 6px;
  opacity: 0.75;
}
body.touch .corner {
  left: 50%;
  right: auto;
  bottom: max(10px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
}
#emote-bar {
  position: absolute;
  right: 10px;
  bottom: 52px;
  display: grid;
  grid-template-columns: repeat(2, auto);
  gap: 4px;
}
body.touch #emote-bar {
  left: 50%;
  right: auto;
  transform: translateX(-50%);
  bottom: 56px;
}
#emote-bar button {
  font-size: 9px;
  padding: 6px 8px;
}
#scoreboard {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  background: rgba(10, 10, 24, 0.92);
  border: 3px solid #000;
  padding: 14px 18px;
  min-width: 300px;
}
.sb-title {
  color: var(--accent);
  margin-bottom: 8px;
}
.sb-row {
  display: grid;
  grid-template-columns: 14px 1fr auto 60px;
  gap: 10px;
  align-items: center;
  font-size: 10px;
}
.dot {
  width: 10px;
  height: 10px;
  display: inline-block;
}

/* touch controls */
#touch {
  display: none;
}
body.touch #touch {
  display: block;
  position: absolute;
  inset: 0;
  pointer-events: auto;
  background: transparent;
}
#touch button {
  position: absolute;
  background: rgba(255, 255, 255, 0.13);
  border: 2px solid rgba(255, 255, 255, 0.35);
  box-shadow: none;
  border-radius: 50%;
  color: rgba(255, 255, 255, 0.8);
  font-size: 16px;
  width: 64px;
  height: 64px;
  padding: 0;
}
#touch button.on {
  background: rgba(255, 255, 255, 0.4);
}
.dpad {
  position: absolute;
  left: max(18px, env(safe-area-inset-left));
  bottom: max(18px, env(safe-area-inset-bottom));
  width: 200px;
  height: 140px;
}
.dpad .b-left {
  left: 0;
  bottom: 30px;
}
.dpad .b-right {
  left: 130px;
  bottom: 30px;
}
.dpad .b-down {
  left: 65px;
  bottom: 0;
}
.dpad .b-up {
  left: 65px;
  bottom: 76px;
  width: 64px;
  height: 52px;
}
.dpad .b-ul,
.dpad .b-ur {
  width: 44px !important;
  height: 44px !important;
  bottom: 96px;
  opacity: 0.4;
}
.dpad .b-ul {
  left: 8px;
}
.dpad .b-ur {
  left: 148px;
}
.ab {
  position: absolute;
  right: max(18px, env(safe-area-inset-right));
  bottom: max(18px, env(safe-area-inset-bottom));
  width: 190px;
  height: 150px;
}
.ab .b-a {
  right: 0;
  bottom: 40px;
  width: 76px !important;
  height: 76px !important;
  background: rgba(224, 60, 40, 0.35) !important;
}
.ab .b-b {
  right: 96px;
  bottom: 10px;
  width: 70px !important;
  height: 70px !important;
}
.ab .b-ab {
  right: 70px;
  bottom: 100px;
  width: 50px !important;
  height: 40px !important;
  border-radius: 12px !important;
  font-size: 9px !important;
  opacity: 0.6;
}

/* ---------------- modals ---------------- */
.modal {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.45);
  padding: 16px;
  z-index: 10;
}
#overlay {
  background: rgba(0, 0, 0, 0.25);
  pointer-events: auto;
}
.panel {
  background: var(--panel);
  border: 3px solid #000;
  box-shadow: 8px 8px 0 rgba(0, 0, 0, 0.5);
  padding: 18px 22px;
  max-width: 620px;
  width: 100%;
  max-height: 90vh;
  overflow-y: auto;
}
.panel h2 {
  margin: 0 0 10px;
  color: var(--accent);
  font-size: 18px;
}
.panel label {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 10px 0;
}
.panel ul {
  padding-left: 18px;
  line-height: 2;
}
.results {
  width: 100%;
  border-collapse: collapse;
  font-size: 10px;
  margin: 10px 0;
}
.results th {
  color: var(--muted);
  text-align: left;
  font-weight: normal;
  padding: 4px;
}
.results td {
  padding: 6px 4px;
  border-top: 2px solid #000;
}
.next {
  color: var(--good);
}
.victory h2 {
  color: #fff;
  text-shadow: 3px 3px 0 var(--accent2);
}

#toast {
  position: fixed;
  left: 50%;
  top: 18px;
  transform: translateX(-50%);
  background: #000;
  border: 2px solid var(--accent);
  padding: 10px 16px;
  z-index: 20;
  font-size: 10px;
  max-width: 90vw;
}
#net-status {
  position: fixed;
  left: 50%;
  bottom: 18px;
  transform: translateX(-50%);
  background: var(--accent2);
  padding: 8px 14px;
  z-index: 20;
  font-size: 10px;
}

@media (max-width: 600px) {
  body {
    font-size: 10px;
  }
  .code {
    font-size: 26px;
  }
}
