:root {
  --bg: #0a0a0c;
  --panel: #14141a;
  --border: #2e2e3a;
  --accent: #ff9d00;
  --cyan: #00e5ff;
  --danger: #ff3b3b;
  --success: #39ff14;
  --text: #f0f0f5;
}

* {
  box-sizing: border-box;
  user-select: none;
  -webkit-user-select: none;
}

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font-family: 'Courier New', Courier, monospace;
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
}

#stage-area {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100dvh;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

#game-wrapper {
  position: relative;
  width: 100%;
  height: 100%;
  background: #000;
  display: flex;
  align-items: center;
  justify-content: center;
}

canvas#view {
  width: 100%;
  height: 100%;
  display: block;
  image-rendering: pixelated;
}

/* Top HUD */
#hud {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  padding: calc(10px + env(safe-area-inset-top, 0px)) 16px 10px;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  flex-wrap: wrap;
  row-gap: 4px;
  column-gap: 10px;
  background: linear-gradient(to bottom, rgba(0,0,0,0.85), rgba(0,0,0,0));
  font-weight: bold;
  font-size: clamp(11px, 1.5vw, 15px);
  pointer-events: none;
  text-shadow: 1px 1px 0 #000;
}

.hud-group {
  display: flex;
  row-gap: 4px;
  column-gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}

.badge {
  padding: 2px 7px;
  border: 1px solid var(--border);
  background: rgba(20, 20, 26, 0.8);
  border-radius: 3px;
  white-space: nowrap;
}

.key-missing { color: #ffd700; border-color: #ffd700; }
.key-found { color: var(--cyan); border-color: var(--cyan); }

/* Radar Overlay */
#radar-overlay {
  position: absolute;
  inset: 0;
  background: rgba(5, 8, 12, 0.88);
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 15;
}

#radar-overlay.active {
  display: flex;
}

canvas#radar {
  width: min(340px, 65vw, 65vh);
  height: min(340px, 65vw, 65vh);
  border: 2px solid var(--cyan);
  background: #04080c;
  box-shadow: 0 0 20px rgba(0, 229, 255, 0.25);
}

.radar-legend {
  margin-top: 10px;
  font-size: 13px;
  display: flex;
  gap: 16px;
  color: #ccc;
  flex-wrap: wrap;
  justify-content: center;
}

/* Menus / Screens */
.screen-overlay {
  position: absolute;
  inset: 0;
  background: rgba(8, 8, 12, 0.92);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  text-align: center;
  padding: 20px;
  overflow-y: auto;
  z-index: 30;
}

.screen-overlay > :first-child {
  margin-top: auto;
}

.screen-overlay > :last-child {
  margin-bottom: auto;
}

.screen-overlay.hidden {
  display: none;
}

h1 {
  margin: 0 0 6px 0;
  font-size: clamp(26px, 4.5vw, 48px);
  color: var(--accent);
  letter-spacing: 3px;
  text-transform: uppercase;
  text-shadow: 3px 3px 0 #5a2800;
}

.subtitle {
  color: var(--cyan);
  font-size: clamp(12px, 1.5vw, 15px);
  margin-bottom: 16px;
  letter-spacing: 1px;
}

.game-instructions {
  color: var(--accent);
  font-size: clamp(12px, 1.4vw, 14px);
  line-height: 1.5;
  max-width: 640px;
  margin: 0 0 18px 0;
}

.game-instructions p {
  margin: 0 0 10px 0;
}

.game-instructions p:last-child {
  margin-bottom: 0;
}

.mode-selector {
  display: flex;
  gap: 12px 26px;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  margin-bottom: 18px;
}

.mode-group {
  display: inline-grid;
  grid-template-columns: 1fr 1fr;
  gap: 0;
  max-width: 100%;
}

.mode-group button,
button#soft-barrier-btn {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0 13px;
  min-height: 27px;
  height: 27px;
  font-size: clamp(11px, 1.5vw, 12.5px);
  line-height: 1;
  white-space: nowrap;
}

.mode-group button:first-child {
  border-radius: 9999px 0 0 9999px;
}

.mode-group button:last-child {
  border-radius: 0 9999px 9999px 0;
  margin-left: -2px;
}

.mode-group button.selected {
  z-index: 2;
}

.mode-group button:hover:not(:disabled) {
  z-index: 3;
}

.mode-group button:focus-visible,
button#soft-barrier-btn:focus-visible,
button.lobby-toggle-btn:focus-visible {
  outline: 2px solid var(--cyan);
  outline-offset: 2px;
  z-index: 4;
}

button#soft-barrier-btn {
  border-radius: 9999px;
  padding: 0 15px;
}

button {
  font-family: inherit;
  font-weight: bold;
  font-size: 14px;
  padding: 10px 16px;
  background: var(--panel);
  color: var(--text);
  border: 2px solid var(--border);
  cursor: pointer;
  text-transform: uppercase;
  transition: all 0.15s;
}

@media (hover: hover) and (pointer: fine) {
  button:hover:not(:disabled):not(.selected) {
    border-color: var(--accent);
    color: var(--accent);
  }
}

button.selected {
  background: var(--accent);
  color: #000;
  border-color: var(--accent);
}

button.primary-btn {
  font-size: 18px;
  padding: 12px 32px;
  background: var(--success);
  color: #000;
  border-color: var(--success);
}

button.primary-btn:hover:not(:disabled) {
  background: #6aff4d;
  color: #000;
  border-color: #6aff4d;
}

button.primary-btn:disabled {
  opacity: 0.35;
  filter: grayscale(40%);
  cursor: not-allowed;
}

.end-actions {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 18px;
}

.end-actions button {
  font-size: 18px;
  padding: 12px 32px;
  min-width: 200px;
  background: #000;
  color: #fff;
  border: 2px solid var(--border);
}

.end-actions button:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.end-actions button.selected,
.end-actions button.selected:hover {
  background: var(--accent);
  color: #000;
  border-color: var(--accent);
}

.controls-help {
  margin-top: 20px;
  font-size: 12px;
  color: #aaa;
  line-height: 1.6;
  max-width: 560px;
}

.room-actions {
  display: flex;
  gap: 14px;
  justify-content: center;
  align-items: center;
  margin-top: 18px;
  flex-wrap: wrap;
}

button.room-btn {
  background: #000;
  color: #fff;
  border: 2px solid var(--border);
  font-size: 14px;
  padding: 10px 20px;
  min-width: 140px;
}

.pwa-install-wrap {
  margin-top: 14px;
  display: flex;
  justify-content: center;
}

.pwa-install-btn {
  background: rgba(0, 229, 255, 0.08);
  border: 1px solid var(--cyan);
  color: var(--cyan);
  padding: 8px 18px;
  font-family: inherit;
  font-size: 12px;
  letter-spacing: 1.5px;
  text-transform: uppercase;
  cursor: pointer;
  border-radius: 4px;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  transition: all 0.2s ease;
}

.pwa-install-btn:hover {
  background: rgba(0, 229, 255, 0.22);
  box-shadow: 0 0 14px rgba(0, 229, 255, 0.5);
  transform: translateY(-1px);
}

.pwa-install-icon {
  font-size: 15px;
  font-weight: bold;
}

/* Room & Multiplayer Modals */
.modal-overlay {
  position: absolute;
  inset: 0;
  background: rgba(6, 6, 10, 0.88);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  padding: 20px;
  overflow-y: auto;
  z-index: 40;
}

.modal-overlay > :first-child {
  margin-top: auto;
}

.modal-overlay > :last-child {
  margin-bottom: auto;
}

.modal-overlay.hidden {
  display: none;
}

.modal-card {
  position: relative;
  width: min(440px, 100%);
  background: var(--panel);
  border: 2px solid var(--border);
  padding: 26px 24px 22px;
  box-shadow: 0 14px 40px rgba(0, 0, 0, 0.85);
  text-align: center;
}

.modal-card-wide {
  width: min(540px, 100%);
}

.modal-card-small {
  width: min(340px, 100%);
  padding: 36px 24px;
}

button.modal-close {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 32px;
  height: 32px;
  padding: 0;
  font-size: 20px;
  line-height: 1;
  background: #000;
  color: #fff;
  border: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal-title {
  margin: 0 0 14px 0;
  font-size: clamp(20px, 3vw, 26px);
  color: var(--accent);
  letter-spacing: 2px;
  text-transform: uppercase;
  text-shadow: 2px 2px 0 #5a2800;
}

.modal-subtitle {
  color: var(--cyan);
  font-size: 13px;
  margin-bottom: 14px;
  letter-spacing: 1px;
  text-transform: uppercase;
}

.modal-form {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  text-align: left;
}

.modal-label {
  font-size: 12px;
  font-weight: bold;
  color: #ccc;
  letter-spacing: 1px;
  margin: 10px 0 6px;
  text-transform: uppercase;
}

.modal-form > .modal-label:first-child {
  margin-top: 0;
}

.modal-input {
  font-family: inherit;
  font-size: 15px;
  padding: 10px 12px;
  background: #08080c;
  color: var(--text);
  border: 2px solid var(--border);
  outline: none;
  width: 100%;
  user-select: text;
  -webkit-user-select: text;
}

.modal-input:focus {
  border-color: var(--cyan);
}

#my-room-name,
#join-room-name,
#join-player-name {
  text-transform: uppercase;
}

.color-palette {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin-top: 4px;
}

button.color-swatch {
  position: relative;
  height: 44px;
  padding: 0;
  border: 2px solid rgba(255, 255, 255, 0.25);
  background: transparent;
  transition: transform 0.1s, opacity 0.15s, box-shadow 0.12s;
}

@media (hover: hover) and (pointer: fine) {
  button.color-swatch:hover:not(:disabled) {
    border-color: rgba(255, 255, 255, 0.85);
    color: inherit;
  }
}

button.color-swatch.selected,
button.color-swatch.selected:hover {
  border: 2px solid #ffffff;
  box-shadow: 0 0 0 3px #ffffff;
  opacity: 1;
}

button.color-swatch.dimmed,
button.color-swatch:disabled {
  opacity: 0.18;
  filter: grayscale(35%);
  cursor: not-allowed;
  border-color: rgba(255, 255, 255, 0.15);
  box-shadow: none;
}

.modal-error {
  min-height: 20px;
  margin-top: 12px;
  font-size: 12px;
  font-weight: bold;
  color: var(--danger);
  text-align: center;
  line-height: 1.4;
}

.modal-actions {
  display: flex;
  justify-content: center;
  margin-top: 14px;
}

.modal-ok-btn {
  min-width: 150px;
}

.lobby-table-wrap {
  width: 100%;
  border: 2px solid var(--border);
  background: #08080c;
  max-height: 36vh;
  overflow-y: auto;
}

.lobby-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
  text-align: left;
}

.lobby-table th {
  padding: 10px 12px;
  background: #1a1a24;
  color: var(--cyan);
  border-bottom: 2px solid var(--border);
  font-size: 12px;
  letter-spacing: 1px;
  text-transform: uppercase;
}

.lobby-table td {
  padding: 9px 12px;
  border-bottom: 1px solid var(--border);
  color: var(--text);
  vertical-align: middle;
  text-transform: uppercase;
}

.lobby-table tbody tr:last-child td {
  border-bottom: none;
}

.lobby-owner-tag {
  display: inline-block;
  margin-left: 8px;
  font-size: 11px;
  color: var(--accent);
}

.lobby-color-cell {
  display: flex;
  align-items: center;
  gap: 8px;
}

.lobby-color-chip {
  display: inline-block;
  width: 24px;
  height: 16px;
  border: 1px solid rgba(255, 255, 255, 0.65);
  flex-shrink: 0;
}

.lobby-toggles {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 8px;
  width: max-content;
  max-width: 100%;
  margin: 14px auto 0;
}

.lobby-toggle-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0;
}

button.lobby-toggle-btn {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0 13px;
  min-height: 27px;
  height: 27px;
  font-size: clamp(10.5px, 1.5vw, 12.5px);
  line-height: 1;
  white-space: nowrap;
  background: #08080c;
  color: var(--text);
  border: 2px solid var(--border);
}

button.lobby-toggle-btn:first-child {
  border-radius: 9999px 0 0 9999px;
}

button.lobby-toggle-btn:last-child {
  border-radius: 0 9999px 9999px 0;
  margin-left: -2px;
}

button.lobby-toggle-btn.selected,
button.lobby-toggle-btn.selected:hover {
  background: var(--accent);
  color: #000;
  border-color: var(--accent);
  z-index: 2;
}

button.lobby-toggle-btn:hover:not(:disabled) {
  z-index: 3;
}

@media (max-width: 480px) {
  .mode-group button,
  button#soft-barrier-btn,
  button.lobby-toggle-btn {
    padding: 0 10px;
    font-size: clamp(10px, 2.7vw, 12px);
  }
}

.get-ready-text {
  font-size: clamp(22px, 3.5vw, 30px);
  font-weight: bold;
  color: var(--success);
  letter-spacing: 2px;
  text-transform: uppercase;
}

.hidden {
  display: none !important;
}

.mp-banner {
  position: absolute;
  inset: 0;
  background: rgba(6, 6, 10, 0.78);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 20px;
  z-index: 35;
  pointer-events: none;
}

.mp-banner-interactive {
  pointer-events: auto;
}

.mp-banner-title {
  font-size: clamp(28px, 5vw, 48px);
  font-weight: bold;
  color: var(--danger);
  letter-spacing: 3px;
  text-transform: uppercase;
  text-shadow: 3px 3px 0 #3a0000;
}

.mp-banner-title-ok {
  color: var(--success);
  text-shadow: 3px 3px 0 #0a3a00;
}

.mp-banner-sub {
  margin-top: 10px;
  font-size: clamp(12px, 1.8vw, 16px);
  color: var(--cyan);
  letter-spacing: 1px;
  text-transform: uppercase;
}

.mp-transition-actions {
  margin-top: 14px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.mp-transition-table-wrap {
  width: 94%;
  max-width: 520px;
  max-height: min(220px, 36vh);
  overflow-y: auto;
  margin: 10px 0 12px 0;
  border: 2px solid var(--border);
  background: #08080c;
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.65);
}

.mp-transition-table-wrap .lobby-table th {
  padding: 8px 10px;
  font-size: 11px;
}

.mp-transition-table-wrap .lobby-table td {
  padding: 7px 10px;
  font-size: 13px;
}

#mp-transition-banner .get-ready-text {
  margin-top: 2px;
}

.mp-transition-ready {
  font-size: clamp(14px, 2.2vw, 18px);
  font-weight: bold;
  color: var(--accent);
  letter-spacing: 1.5px;
  text-transform: uppercase;
}

.spectator-canvas-wrap {
  display: flex;
  justify-content: center;
  margin-top: 4px;
}

canvas#spectator-radar {
  width: min(340px, 72vw, 52vh);
  height: min(340px, 72vw, 52vh);
  border: 2px solid var(--cyan);
  background: #04080c;
  box-shadow: 0 0 18px rgba(0, 229, 255, 0.2);
}

.spectator-legend {
  margin-top: 8px;
  font-size: 12px;
  gap: 12px;
}

.spectator-note {
  margin-top: 10px;
  font-size: 12px;
  color: #ccc;
  letter-spacing: 0.5px;
  text-transform: uppercase;
}

.results-table-wrap {
  max-height: 44vh;
}

.lobby-table tr.winner-row td {
  background: rgba(255, 157, 0, 0.16);
  color: var(--accent);
  font-weight: bold;
}

.lobby-winner-tag {
  display: inline-block;
  margin-left: 8px;
  font-size: 11px;
  color: var(--success);
}

.mp-results-footer {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 16px;
}

/* Touch Controls pinned cleanly to bottom of viewport */
#touch-controls {
  display: none;
  flex: 0 0 auto;
  width: 100%;
  padding: 8px 12px calc(10px + env(safe-area-inset-bottom, 0px));
  justify-content: space-between;
  align-items: flex-end;
  gap: 10px;
  background: var(--panel);
  border-top: 2px solid var(--border);
  z-index: 20;
}

.touch-joystick-zone {
  position: relative;
  width: clamp(122px, 40vw, 220px);
  height: clamp(132px, 30vh, 190px);
  border: 2px dashed rgba(140, 150, 175, 0.35);
  border-radius: 14px;
  background: rgba(20, 22, 30, 0.28);
  touch-action: none;
  flex-shrink: 0;
}

.touch-joystick-zone::after {
  content: 'MOVE / TURN';
  position: absolute;
  left: 0;
  right: 0;
  bottom: 6px;
  text-align: center;
  font-size: 9px;
  letter-spacing: 1px;
  color: rgba(200, 210, 230, 0.45);
  pointer-events: none;
}

.touch-joystick-zone.active::after {
  opacity: 0;
}

.joy-base {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 84px;
  height: 84px;
  margin-left: -42px;
  margin-top: -42px;
  border-radius: 50%;
  border: 2px solid rgba(140, 150, 175, 0.45);
  background: rgba(20, 22, 30, 0.45);
  pointer-events: none;
  opacity: 0.65;
  transition: opacity 0.12s, border-color 0.12s;
}

.touch-joystick-zone.active .joy-base {
  opacity: 1;
  border-color: rgba(255, 157, 0, 0.75);
  background: rgba(20, 22, 30, 0.65);
}

.joy-knob {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 36px;
  height: 36px;
  margin-left: -18px;
  margin-top: -18px;
  border-radius: 50%;
  background: rgba(220, 225, 240, 0.55);
  border: 2px solid rgba(255, 255, 255, 0.65);
  pointer-events: none;
}

.touch-joystick-zone.active .joy-knob {
  background: rgba(255, 157, 0, 0.92);
  border-color: #fff;
}

.touch-right {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 8px;
  flex-shrink: 0;
}

.touch-strafe-row,
.touch-actions {
  display: flex;
  gap: 8px;
}

.t-btn {
  font-family: inherit;
  font-weight: bold;
  text-transform: uppercase;
  cursor: pointer;
  background: #22222c;
  border: 2px solid #444455;
  color: #fff;
  font-size: 15px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 0;
  touch-action: none;
  outline: none;
  -webkit-tap-highlight-color: transparent;
  transition: none;
}

.touch-strafe-row .t-btn {
  width: 74px;
  height: 40px;
  font-size: 14px;
}

.touch-actions .t-btn {
  width: 74px;
  height: 54px;
  font-size: 11px;
  line-height: 1.15;
}

.t-btn.pressed {
  background: var(--accent);
  border-color: var(--accent);
  color: #000;
}

@media (max-width: 1280px), (pointer: coarse), (any-pointer: coarse) {
  #hud {
    padding: calc(10px + env(safe-area-inset-top, 0px)) 14px 10px;
  }
  #touch-controls {
    display: none;
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    padding: 12px 14px calc(12px + env(safe-area-inset-bottom, 0px));
    background: linear-gradient(to top, rgba(10, 10, 14, 0.65), rgba(10, 10, 14, 0.2) 72%, rgba(10, 10, 14, 0));
    border-top: none;
    pointer-events: none;
    z-index: 20;
  }
  body.playing #touch-controls {
    display: flex;
  }
  .touch-joystick-zone,
  .touch-right {
    pointer-events: auto;
  }
  .t-btn {
    background: rgba(20, 22, 30, 0.58);
    border-color: rgba(140, 150, 175, 0.55);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
  }
  .t-btn.pressed {
    background: rgba(255, 157, 0, 0.9);
    border-color: rgba(255, 157, 0, 0.95);
    color: #000;
  }
}

@media (max-width: 1280px) and (max-height: 500px), (pointer: coarse) and (max-height: 500px) {
  #touch-controls {
    padding: 6px 12px calc(6px + env(safe-area-inset-bottom, 0px));
  }
  .touch-joystick-zone {
    width: clamp(130px, 34vw, 190px);
    height: clamp(106px, 38vh, 148px);
  }
  .joy-base {
    width: 72px;
    height: 72px;
    margin-left: -36px;
    margin-top: -36px;
  }
  .joy-knob {
    width: 30px;
    height: 30px;
    margin-left: -15px;
    margin-top: -15px;
  }
  .touch-right,
  .touch-strafe-row,
  .touch-actions {
    gap: 6px;
  }
  .touch-strafe-row .t-btn {
    width: 68px;
    height: 34px;
    font-size: 13px;
  }
  .touch-actions .t-btn {
    width: 68px;
    height: 44px;
    font-size: 10px;
  }
}

@media (max-width: 560px) {
  #hud {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr) auto;
    grid-template-rows: auto auto;
    row-gap: 4px;
    column-gap: 6px;
    align-items: center;
    padding: calc(8px + env(safe-area-inset-top, 0px)) 10px 8px;
    font-size: clamp(9.5px, 2.6vw, 12px);
  }
  #hud .hud-group {
    display: contents;
  }
  #hud .badge {
    padding: 2px 5px;
  }
  #hud-level-badge {
    grid-column: 1;
    grid-row: 1;
    justify-self: start;
  }
  #hud-key {
    grid-column: 2;
    grid-row: 1;
    justify-self: start;
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  #hud-time-badge {
    grid-column: 3;
    grid-row: 1;
    justify-self: end;
  }
  #hud-barriers-badge {
    grid-column: 1 / 3;
    grid-row: 2;
    justify-self: start;
  }
  #hud-alert {
    grid-column: 3;
    grid-row: 2;
    justify-self: end;
  }
}
