* {
  box-sizing: border-box;
}

:root {
  --bg: #1b1f24;
  --panel-bg: #2a2f38;
  --accent: #4a9c4e;
  --accent-dark: #3a7c3e;
  --text: #f2f2f2;
  --muted: #a9b0ba;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: 'Courier New', Courier, monospace;
  image-rendering: pixelated;
  overflow: hidden;
  /* This isn't a text-content site, and rapid taps on the mobile touch
     controls were triggering the browser's native text-selection/magnifier
     gesture. Disable selection and callout menus globally... */
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

/* ...except inside the name input, which still needs normal text editing/selection. */
input,
textarea {
  -webkit-user-select: text;
  user-select: text;
  -webkit-touch-callout: default;
}

.hidden {
  display: none !important;
}

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.75);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
}

.panel {
  background: var(--panel-bg);
  border: 4px solid #000;
  box-shadow: 0 0 0 4px var(--accent);
  border-radius: 8px;
  padding: 24px 28px;
  width: min(360px, 90vw);
  text-align: center;
}

.panel h1 {
  margin-top: 0;
  color: var(--accent);
  letter-spacing: 1px;
}

.panel p {
  color: var(--muted);
  font-size: 13px;
  line-height: 1.4;
}

.panel label {
  display: block;
  text-align: left;
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 4px;
}

#name-input {
  width: 100%;
  padding: 8px;
  font-family: inherit;
  font-size: 14px;
  background: #12151a;
  color: var(--text);
  border: 2px solid #000;
  border-radius: 4px;
  margin-bottom: 14px;
}

#avatar-picker {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-bottom: 14px;
}

.avatar-option {
  background: #12151a;
  border: 2px solid #000;
  border-radius: 6px;
  padding: 6px 4px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

.avatar-option canvas {
  image-rendering: pixelated;
  width: 40px;
  height: 45px;
}

.avatar-option span {
  font-size: 10px;
  color: var(--muted);
}

.avatar-option.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent);
  background: #1a2a1c;
}

.avatar-option.selected span {
  color: var(--accent);
}

.avatar-option.add-custom {
  justify-content: center;
}

.avatar-option.add-custom .plus-icon {
  font-size: 20px;
  color: var(--muted);
  line-height: 45px;
}

.custom-avatar-panel {
  width: min(300px, 90vw);
}

#custom-avatar-preview {
  display: flex;
  justify-content: center;
  margin-bottom: 10px;
}

#custom-avatar-canvas {
  image-rendering: pixelated;
  width: 64px;
  height: 72px;
  background: #12151a;
  border: 2px solid #000;
  border-radius: 6px;
}

.color-field {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}

.color-field label {
  margin-bottom: 0;
}

.color-field input[type="color"] {
  width: 48px;
  height: 30px;
  padding: 2px;
  background: #12151a;
  border: 2px solid #000;
  border-radius: 4px;
  cursor: pointer;
}

button {
  font-family: inherit;
  font-size: 14px;
  padding: 10px 16px;
  background: var(--accent);
  color: #06210a;
  border: 2px solid #000;
  border-radius: 4px;
  cursor: pointer;
  font-weight: bold;
  width: 100%;
  margin-top: 6px;
}

button:hover {
  background: var(--accent-dark);
}

button.secondary {
  background: #555;
  color: var(--text);
}

button.secondary:hover {
  background: #444;
}

.status {
  min-height: 18px;
  color: #f1c40f;
  font-size: 13px;
}

.hint {
  font-size: 11px;
  color: var(--muted);
}

#game-wrap {
  position: relative;
  width: 100vw;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}

#game-canvas {
  background: #2f6b32;
  border: 4px solid #000;
  box-shadow: 0 0 0 4px var(--accent);
  image-rendering: pixelated;
  width: auto;
  height: auto;
  max-width: 96vw;
  max-height: 96vh;
}

#hud {
  position: absolute;
  top: 12px;
  left: 12px;
  right: 12px;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  z-index: 10;
  pointer-events: none;
}

#hud > * {
  pointer-events: auto;
}

.hud-left {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

#health-display {
  background: rgba(20, 22, 26, 0.85);
  border: 2px solid #000;
  border-radius: 6px;
  padding: 6px 10px;
  display: flex;
  gap: 3px;
}

.heart {
  position: relative;
  width: 18px;
  height: 18px;
  font-size: 18px;
  line-height: 18px;
}

.heart .heart-bg,
.heart .heart-fill {
  position: absolute;
  top: 0;
  left: 0;
  white-space: nowrap;
}

.heart .heart-bg {
  color: #3a3f47;
}

.heart .heart-fill {
  width: 0%;
  overflow: hidden;
  color: #e74c3c;
}

#armor-display {
  background: rgba(20, 22, 26, 0.85);
  border: 2px solid #000;
  border-radius: 6px;
  padding: 4px 6px;
  display: flex;
  align-items: center;
  width: fit-content;
}

#armor-display canvas {
  image-rendering: pixelated;
  display: block;
}

#credits-display {
  background: rgba(20, 22, 26, 0.85);
  border: 2px solid #000;
  border-radius: 6px;
  padding: 8px 14px;
  font-size: 18px;
  font-weight: bold;
  color: #f1c40f;
}

#punch-display {
  background: rgba(20, 22, 26, 0.85);
  border: 2px solid #000;
  border-radius: 6px;
  padding: 4px 12px;
  font-size: 13px;
  font-weight: bold;
  color: #ff8a5c;
}

#players-panel {
  background: rgba(20, 22, 26, 0.85);
  border: 2px solid #000;
  border-radius: 6px;
  padding: 8px 12px;
  font-size: 12px;
  min-width: 130px;
}

#players-panel .row {
  display: flex;
  align-items: center;
  gap: 6px;
  margin: 3px 0;
}

#players-panel .dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  border: 1px solid #000;
  flex-shrink: 0;
}

.hud-right {
  display: flex;
  gap: 8px;
}

#mute-btn,
#waypoints-btn,
#unstick-btn,
#minimap-menu-btn,
#dev-btn {
  width: 42px;
  height: 42px;
  padding: 0;
  font-size: 18px;
  border-radius: 50%;
  margin-top: 0;
}

#mute-btn.muted {
  background: #c0392b;
}

#waypoints-btn {
  background: #7a8cff;
}

#waypoints-btn:hover {
  background: #5f6fe0;
}

#unstick-btn {
  background: #e67e22;
}

#unstick-btn:hover {
  background: #cf6d1a;
}

/* Low-key styling (muted grey, smaller/dimmer than the other HUD buttons) -
   this is a developer tool, not something to draw a regular player's eye. */
#dev-btn {
  background: rgba(90, 94, 102, 0.6);
  opacity: 0.6;
}

#dev-btn:hover {
  background: rgba(110, 114, 122, 0.8);
  opacity: 1;
}

#minimap-menu-btn {
  background: #3fa7a3;
}

#minimap-menu-btn:hover {
  background: #2f8f8b;
}

#minimap-wrap {
  position: absolute;
  top: 64px;
  right: 12px;
  /* Draggable, so it can end up overlapping other same-layer HUD chrome
     (hotbar, chat log, etc.) - stay above all of it so those elements
     never silently swallow taps/drags meant for the minimap. */
  z-index: 12;
  background: rgba(20, 22, 26, 0.85);
  border: 2px solid #000;
  border-radius: 6px;
  padding: 6px;
  cursor: pointer;
  pointer-events: auto;
  touch-action: none;
  transition: transform 0.15s ease;
}

/* Hidden via the minimap menu's toggle. Only applies while collapsed -
   if the player hits N to expand while it's hidden, the expanded overlay
   (a different visual state, not just a moved/shown minimap-wrap) should
   still work, and collapsing re-hides it. */
#minimap-wrap.minimap-hidden:not(.minimap-expanded) {
  display: none;
}

#minimap-title {
  font-size: 10px;
  font-weight: bold;
  color: var(--muted);
  text-align: center;
  margin-bottom: 3px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#minimap-canvas {
  display: block;
  image-rendering: pixelated;
}

#minimap-wrap.minimap-expanded {
  position: fixed;
  top: 50%;
  right: auto;
  left: 50%;
  transform: translate(-50%, -50%);
  padding: 14px;
  box-shadow: 0 0 0 4px var(--accent), 0 0 40px rgba(0, 0, 0, 0.6);
  z-index: 30;
}

#minimap-wrap.minimap-expanded #minimap-title {
  font-size: 15px;
  margin-bottom: 8px;
}

#interact-prompt {
  position: absolute;
  bottom: 112px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(20, 22, 26, 0.9);
  border: 2px solid #000;
  border-radius: 6px;
  padding: 8px 14px;
  font-size: 13px;
  z-index: 10;
}

#interact-prompt kbd {
  background: var(--accent);
  color: #06210a;
  border-radius: 3px;
  padding: 1px 6px;
  font-weight: bold;
}

#death-message {
  position: absolute;
  top: 40%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: rgba(120, 20, 20, 0.92);
  border: 3px solid #000;
  box-shadow: 0 0 0 3px #e74c3c;
  border-radius: 8px;
  padding: 14px 28px;
  font-size: 26px;
  font-weight: bold;
  letter-spacing: 1px;
  color: #fff3b0;
  z-index: 20;
  pointer-events: none;
  transition: opacity 0.4s ease;
}

#death-message.fade-out {
  opacity: 0;
}

#hotbar {
  position: absolute;
  bottom: 56px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 6px;
  z-index: 10;
}

.hotbar-slot {
  position: relative;
  width: 42px;
  height: 42px;
  background: rgba(20, 22, 26, 0.85);
  border: 2px solid #000;
  border-radius: 6px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.hotbar-slot.selected {
  border-color: #fff3b0;
  box-shadow: 0 0 0 2px var(--accent);
}

.hotbar-key {
  position: absolute;
  top: 2px;
  left: 4px;
  font-size: 10px;
  color: var(--muted);
}

.hotbar-icon {
  width: 24px;
  height: 24px;
  border-radius: 4px;
  background: transparent;
}

.hotbar-icon.filled {
  border: 1px solid rgba(255, 255, 255, 0.4);
}

/* Weapon (sword/wand/gun) cooldown indicator - opacity is set every frame
   in JS, proportional to time remaining, so it fades out as the weapon
   comes off cooldown rather than just toggling on/off. */
.hotbar-cooldown {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  border-radius: 6px;
  background: rgba(52, 152, 219, 0.65);
  pointer-events: none;
  opacity: 0;
}

#chat-log {
  position: absolute;
  left: 12px;
  bottom: 100px;
  width: min(300px, 40vw);
  max-height: 150px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  overflow: hidden;
  pointer-events: none;
  z-index: 10;
}

.chat-row {
  background: rgba(20, 22, 26, 0.75);
  border-radius: 4px;
  padding: 3px 8px;
  font-size: 12px;
  line-height: 1.4;
  word-break: break-word;
}

.chat-row .chat-name {
  font-weight: bold;
}

#chat-input-wrap {
  position: absolute;
  left: 12px;
  bottom: 56px;
  width: min(300px, 40vw);
  z-index: 15;
}

#chat-input {
  width: 100%;
  padding: 8px 10px;
  font-family: inherit;
  font-size: 13px;
  background: rgba(12, 14, 18, 0.95);
  color: var(--text);
  border: 2px solid var(--accent);
  border-radius: 6px;
}

#controls-hint {
  position: absolute;
  bottom: 14px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 11px;
  color: var(--muted);
  background: rgba(20, 22, 26, 0.7);
  padding: 4px 10px;
  border-radius: 4px;
  z-index: 10;
}

/* Touch control pad: hidden by default, only shown for coarse (touch) pointers. */
#touch-controls {
  display: none;
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 10px 16px calc(10px + env(safe-area-inset-bottom, 0px));
  justify-content: space-between;
  align-items: flex-end;
  z-index: 15;
  pointer-events: none;
}

@media (pointer: coarse) {
  #touch-controls {
    display: flex;
  }
}

#touch-dpad,
#touch-right {
  pointer-events: auto;
}

#touch-dpad {
  display: grid;
  grid-template-columns: repeat(3, 54px);
  grid-template-rows: repeat(3, 54px);
  gap: 8px;
}

.dpad-btn {
  width: 54px;
  height: 54px;
  margin: 0;
  padding: 0;
  border-radius: 10px;
  background: rgba(20, 22, 26, 0.7);
  border: 2px solid rgba(255, 255, 255, 0.25);
  color: #fff;
  font-size: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

.dpad-btn:active {
  background: rgba(255, 255, 255, 0.25);
}

.dpad-up { grid-column: 2; grid-row: 1; }
.dpad-left { grid-column: 1; grid-row: 2; }
.dpad-right { grid-column: 3; grid-row: 2; }
.dpad-down { grid-column: 2; grid-row: 3; }

/* Right-hand side column: RUN sits above the action buttons, out of the
   way of the D-pad and the inventory row above it. */
#touch-right {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 10px;
}

.run-btn {
  width: 54px;
  height: 38px;
  margin: 0;
  padding: 0;
  border-radius: 10px;
  background: rgba(230, 126, 34, 0.7);
  border: 2px solid rgba(255, 255, 255, 0.25);
  color: #fff;
  font-size: 11px;
  font-weight: bold;
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

.run-btn:active {
  background: rgba(255, 255, 255, 0.25);
}

/* Toggled on (tap to enable/disable running, rather than press-and-hold). */
.run-btn.dpad-run-active {
  background: rgba(255, 165, 40, 0.95);
  box-shadow: 0 0 0 2px rgba(255, 220, 150, 0.9), inset 0 0 6px rgba(255, 255, 255, 0.5);
}

#touch-actions {
  display: flex;
  align-items: flex-end;
  gap: 14px;
}

.action-btn {
  width: 58px;
  height: 58px;
  margin: 0;
  padding: 0;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.3);
  color: #06210a;
  font-size: 22px;
  font-weight: bold;
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

.action-btn:active {
  transform: scale(0.94);
}

.action-punch { background: rgba(231, 76, 60, 0.85); }
.action-interact { background: rgba(46, 204, 113, 0.85); }
.action-chat { background: rgba(241, 196, 15, 0.85); font-size: 20px; }
.action-use { background: rgba(74, 156, 78, 0.85); font-size: 20px; }
.action-waypoints { background: rgba(122, 140, 255, 0.85); }

/* Compact layout for small / touch screens. */
@media (max-width: 900px), (pointer: coarse) {
  #game-wrap {
    align-items: flex-start;
    padding-top: 84px;
    box-sizing: border-box;
  }

  #game-canvas {
    max-height: 58vh;
  }

  #hud {
    flex-wrap: wrap;
    gap: 6px;
  }

  /* Hearts/armor/coins/punch counter as a compact wrapping row instead of
     a tall stacked column - a column here used to eat a third of the
     screen height and shove everything else down with it. */
  .hud-left {
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
  }

  #credits-display,
  #punch-display {
    font-size: 13px;
    padding: 5px 9px;
  }

  #players-panel {
    font-size: 10px;
    min-width: 80px;
    padding: 6px 8px;
  }

  #mute-btn,
  #waypoints-btn,
  #unstick-btn,
  #minimap-menu-btn,
  #dev-btn {
    width: 36px;
    height: 36px;
    font-size: 15px;
  }

  #controls-hint {
    display: none;
  }

  #minimap-wrap {
    top: 96px;
    padding: 4px;
  }

  #minimap-wrap.minimap-expanded {
    top: 50%;
  }

  #interact-prompt {
    bottom: 258px;
  }

  /* Positioned by JS (positionMobileHotbar) to sit just under the
     rendered canvas, which varies in height by area aspect ratio - this
     is only a pre-JS fallback so it doesn't flash at 0 for a frame. */
  #hotbar {
    bottom: auto;
    top: 150px;
  }

  .hotbar-slot {
    width: 34px;
    height: 34px;
  }

  .hotbar-icon {
    width: 18px;
    height: 18px;
  }

  #chat-log {
    bottom: 300px;
    width: min(220px, 55vw);
    max-height: 110px;
  }

  #chat-input-wrap {
    bottom: 258px;
    width: min(220px, 55vw);
  }

  /* 4 action buttons now (E/punch/chat/waypoints) plus the D-pad need to
     fit side by side even on narrow phones - shrink both, and tighten the
     row's own padding/gap, rather than letting the row overflow off-screen. */
  #touch-controls {
    padding-left: 8px;
    padding-right: 8px;
  }

  #touch-dpad {
    grid-template-columns: repeat(3, 40px);
    grid-template-rows: repeat(3, 40px);
    gap: 6px;
  }

  .dpad-btn {
    width: 40px;
    height: 40px;
    font-size: 14px;
  }

  #touch-right {
    gap: 6px;
  }

  .run-btn {
    width: 40px;
    height: 30px;
    font-size: 9px;
  }

  /* 5 action buttons now (E/punch/use/chat/waypoints) - shrink them a touch
     smaller than the old cramped layout so there's real breathing room
     between buttons instead of them all but touching. */
  #touch-actions {
    gap: 8px;
  }

  .action-btn {
    width: 36px;
    height: 36px;
    font-size: 13px;
  }

  .panel {
    padding: 18px 16px;
  }
}

.slot-panel {
  width: min(320px, 90vw);
}

#reels {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin: 16px 0;
}

.reel {
  width: 64px;
  height: 64px;
  background: #12151a;
  border: 2px solid #000;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 32px;
}

.reel.spinning {
  animation: spin-blur 0.1s linear infinite;
}

@keyframes spin-blur {
  0% { transform: translateY(-4px); }
  50% { transform: translateY(4px); }
  100% { transform: translateY(-4px); }
}

.bet-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  margin: 12px 0;
  font-size: 14px;
}

.bet-row button {
  width: auto;
  margin: 0;
  padding: 6px 12px;
}

#bet-value {
  min-width: 40px;
  font-weight: bold;
  color: #f1c40f;
}

.waypoints-panel {
  width: min(360px, 90vw);
}

#waypoints-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin: 14px 0;
}

.waypoint-row {
  display: flex;
  align-items: center;
  gap: 8px;
  background: #12151a;
  border: 2px solid #000;
  border-radius: 6px;
  padding: 8px 10px;
  text-align: left;
}

.waypoint-info {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  font-size: 12px;
}

.waypoint-info strong {
  color: #7a8cff;
}

.waypoint-info span {
  color: var(--muted);
}

.dev-pin-panel {
  width: min(300px, 90vw);
}

#dev-pin-input {
  text-align: center;
  letter-spacing: 4px;
  font-size: 18px;
}

.dev-menu-panel {
  width: min(340px, 90vw);
}

.minimap-menu-panel {
  width: min(280px, 90vw);
}

#dev-item-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin: 14px 0;
}

.dev-item-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  width: auto;
  margin: 0;
  padding: 10px 6px;
  background: #12151a;
  border: 2px solid #000;
  border-radius: 6px;
  font-size: 10px;
  color: var(--text);
}

.dev-item-btn:hover {
  border-color: var(--accent);
  background: #1b1f26;
}

.dev-item-btn canvas {
  image-rendering: pixelated;
}

.waypoint-row button {
  width: auto;
  margin: 0;
  padding: 6px 10px;
  font-size: 12px;
}
