* { box-sizing: border-box; }

/* Mobile Safari can pop its text-selection magnifier/callout on a long
   press of any of these, even though nothing here is meant to be selectable
   text — every clickable/tappable control in the game (buttons, plus the
   div-based hotbar/inventory/crafting slots) opts out explicitly. */
button, #hotbar, .inventory-grid, .crafting-list {
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #000;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  color: #eaeaea;
}

#game {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
}

.hidden {
  display: none !important;
}

/* Crosshair */
#crosshair {
  position: fixed;
  top: 50%;
  left: 50%;
  width: 18px;
  height: 18px;
  transform: translate(-50%, -50%);
  pointer-events: none;
  z-index: 5;
}
#crosshair::before, #crosshair::after {
  content: '';
  position: absolute;
  background: #fff;
  mix-blend-mode: difference;
}
/* cross (default, no data-style attribute) */
#crosshair::before {
  top: 50%;
  left: 0;
  width: 100%;
  height: 2px;
  transform: translateY(-50%);
}
#crosshair::after {
  left: 50%;
  top: 0;
  height: 100%;
  width: 2px;
  transform: translateX(-50%);
}
/* dot / square / circle all reuse ::before as the one shape and drop ::after */
#crosshair[data-style="dot"]::before,
#crosshair[data-style="square"]::before,
#crosshair[data-style="circle"]::before {
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
#crosshair[data-style="dot"]::after,
#crosshair[data-style="square"]::after,
#crosshair[data-style="circle"]::after {
  content: none;
}
#crosshair[data-style="dot"]::before {
  width: 5px;
  height: 5px;
  border-radius: 50%;
}
#crosshair[data-style="square"]::before {
  width: 6px;
  height: 6px;
}
#crosshair[data-style="circle"]::before {
  width: 12px;
  height: 12px;
  box-sizing: border-box;
  background: transparent;
  border: 2px solid #fff;
  border-radius: 50%;
}

/* Save indicator */
#save-indicator {
  position: fixed;
  top: 62px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 12px;
  padding: 3px 10px;
  background: rgba(0, 0, 0, 0.5);
  border: 1px solid rgba(255, 255, 255, 0.25);
  border-radius: 10px;
  z-index: 5;
  opacity: 0;
  transition: opacity 0.4s ease;
  pointer-events: none;
}
#save-indicator.visible {
  opacity: 1;
}

/* Chat (available in single-player too, mainly for the /stuck command, and
   in multiplayer for talking to other players) */
#chat-log {
  position: fixed;
  bottom: 84px;
  left: 16px;
  width: 320px;
  max-width: calc(100vw - 32px);
  display: flex;
  flex-direction: column;
  gap: 2px;
  font-size: 13px;
  z-index: 5;
  pointer-events: none;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.9);
}
#chat-log div {
  background: rgba(0, 0, 0, 0.4);
  padding: 2px 8px;
  border-radius: 4px;
  word-break: break-word;
}
#chat-input-row {
  position: fixed;
  bottom: 84px;
  left: 16px;
  width: 320px;
  max-width: calc(100vw - 32px);
  z-index: 20;
}
#chat-input {
  width: 100%;
  box-sizing: border-box;
  padding: 8px 10px;
  font-size: 14px;
  color: #eaeaea;
  background: rgba(0, 0, 0, 0.75);
  border: 1px solid rgba(255, 255, 255, 0.4);
  border-radius: 6px;
}
/* On touch devices the bottom-left corner is the joystick's territory, and
   the on-screen keyboard covers the bottom of the screen anyway while
   composing — move chat to the top-right instead. 110px clears #hud-top-row
   (top: 14px) plus the debug/FPS line even in the worst case where its text
   wraps to multiple lines on a narrow screen, whether or not that line is
   currently shown at all. */
body.touch-device #chat-log,
body.touch-device #chat-input-row {
  left: auto;
  right: 16px;
  bottom: auto;
  top: 110px;
}

/* Interplanetary launcher warp overlay */
#dimension-transition {
  position: fixed;
  inset: 0;
  z-index: 15;
  background: #000;
  pointer-events: none;
}
#dimension-transition-canvas {
  display: block;
  width: 100%;
  height: 100%;
}

/* Mining progress */
#mining-progress {
  position: fixed;
  top: calc(50% + 18px);
  left: 50%;
  width: 80px;
  height: 6px;
  transform: translateX(-50%);
  background: rgba(255, 255, 255, 0.15);
  border: 1px solid rgba(255, 255, 255, 0.4);
  border-radius: 3px;
  overflow: hidden;
  z-index: 5;
}
#mining-progress .fill {
  height: 100%;
  width: 0%;
  background: #e8dfa0;
}

/* Top HUD row: health/oxygen (left) and debug/FPS (right) share one flex
   row spanning the viewport with space-between, so the two sides shrink
   as the window narrows instead of ever overlapping — a real layout
   guarantee (flexbox can't let siblings overlap), not a tuned breakpoint. */
#hud-top-row {
  position: fixed;
  top: 14px;
  left: 14px;
  right: 14px;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
  z-index: 5;
}
#stats {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 8px;
  min-width: 0;
  flex-shrink: 1;
}
.stat-bar {
  position: relative;
  width: clamp(70px, 20vw, 150px);
  height: 18px;
  min-width: 0;
  flex-shrink: 1;
  background: rgba(0, 0, 0, 0.5);
  border: 1px solid rgba(255, 255, 255, 0.35);
  border-radius: 4px;
  overflow: hidden;
}
.stat-bar .fill {
  height: 100%;
  width: 100%;
  transition: width 0.2s ease;
}
.stat-bar.health .fill { background: #c9403a; }
.stat-bar.oxygen .fill { background: #3ab0c9; }
.stat-bar .label {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 600;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.9);
  white-space: nowrap;
}

/* Hotbar */
#hotbar {
  position: fixed;
  bottom: 16px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 6px;
  z-index: 5;
}
.slot {
  position: relative;
  width: 52px;
  height: 52px;
  background: rgba(0, 0, 0, 0.5);
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.slot.selected {
  border-color: #fff;
  box-shadow: 0 0 8px rgba(255, 255, 255, 0.6);
}
.swatch {
  width: 30px;
  height: 30px;
  border-radius: 3px;
  border: 1px solid rgba(0, 0, 0, 0.4);
}
.swatch-1 { background: #a8a396; }
.swatch-2 { background: #5f5d58; }
.swatch-3 { background: #3a3733; }
.swatch-4 { background: #dff2f5; }
.swatch-5 { background: #4a4640; box-shadow: inset 0 0 6px #3fe0c0; }
.swatch-7 { background: #7a2f3a; box-shadow: inset 0 6px 0 #d8d0c0; }
.swatch-8 { background: #2a2350; box-shadow: inset 0 0 6px #7de8ff; }
.swatch-9 { background: #54524d; box-shadow: inset 0 0 6px #fff2c0; }
.swatch-10 { background: #c9a672; }
.swatch-11 { background: #7a4030; }
.swatch-12 { background: #c9cdd6; }
.swatch-13 { background: #3d241c; box-shadow: inset 0 0 6px #ff9d4d; }
.item-swatch {
  background: rgba(255, 255, 255, 0.08);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 17px;
  line-height: 1;
}
.count {
  position: absolute;
  bottom: 2px;
  right: 5px;
  font-size: 11px;
  font-weight: 700;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.9);
}
.key {
  position: absolute;
  top: 1px;
  left: 4px;
  font-size: 10px;
  opacity: 0.7;
}

/* Debug (FPS/coordinates): a flex child of #hud-top-row, not independently
   positioned — see the comment above #hud-top-row for why that guarantees
   it can never overlap the stat bars. */
#debug {
  margin: 0;
  padding: 6px 10px;
  background: rgba(0, 0, 0, 0.5);
  border: 1px solid rgba(255, 255, 255, 0.25);
  border-radius: 4px;
  font-size: 11px;
  font-family: 'SF Mono', Consolas, monospace;
  white-space: normal;
  text-align: right;
  min-width: 0;
  flex-shrink: 1;
}

/* Overlays */
.overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.65);
  z-index: 10;
}
.panel {
  max-width: 480px;
  padding: 28px 32px;
  background: rgba(10, 10, 14, 0.9);
  border: 1px solid rgba(255, 255, 255, 0.25);
  border-radius: 10px;
  text-align: left;
}
.panel h1 {
  margin: 0 0 10px;
  font-size: 22px;
  letter-spacing: 0.04em;
}
.version-tag {
  font-size: 12px;
  font-weight: 400;
  letter-spacing: normal;
  color: rgba(234, 234, 234, 0.5);
}
.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.panel-header h1 {
  margin: 0;
}
.close-btn {
  flex-shrink: 0;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.3);
  color: #eaeaea;
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
  touch-action: manipulation;
}
.close-btn:hover {
  background: rgba(255, 255, 255, 0.2);
}
.panel p {
  margin: 8px 0;
  line-height: 1.5;
  font-size: 14px;
  color: #cfd6d8;
}
.panel .hint {
  color: #9fd4de;
  font-size: 13px;
}
.panel h2 {
  margin: 16px 0 8px;
  font-size: 14px;
  letter-spacing: 0.03em;
  color: #cfd6d8;
}
.option-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  margin: 8px 0;
  cursor: pointer;
}
.option-row input[type="checkbox"] {
  width: 18px;
  height: 18px;
  cursor: pointer;
}
.option-row-group {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-size: 14px;
  margin: 8px 0;
}
.speed-btn-group {
  display: flex;
  gap: 6px;
}
.speed-btn {
  padding: 6px 12px;
  font-size: 13px;
  font-weight: 600;
  color: #eaeaea;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 6px;
  cursor: pointer;
}
.speed-btn:hover {
  background: rgba(255, 255, 255, 0.16);
}
.speed-btn.selected {
  background: rgba(232, 223, 160, 0.3);
  border-color: rgba(232, 223, 160, 0.7);
}
.avatar-color-group {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  max-width: 220px;
  justify-content: flex-end;
}
.avatar-color-btn {
  width: 24px;
  height: 24px;
  padding: 0;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-radius: 50%;
  cursor: pointer;
}
.avatar-color-btn:hover {
  border-color: rgba(255, 255, 255, 0.6);
}
.avatar-color-btn.selected {
  border-color: #e8dfa0;
  box-shadow: 0 0 0 2px rgba(232, 223, 160, 0.5);
}
.panel ul {
  margin: 10px 0;
  padding-left: 18px;
  font-size: 13.5px;
  line-height: 1.6;
}
#respawn-btn {
  margin-top: 14px;
  padding: 10px 18px;
  font-size: 14px;
  font-weight: 600;
  color: #0a0a0a;
  background: #e8dfa0;
  border: none;
  border-radius: 6px;
  cursor: pointer;
}
#respawn-btn:hover {
  background: #f2ecb8;
}

/* Inventory & crafting */
.inventory-panel {
  max-width: 620px;
  max-height: 85vh;
  overflow-y: auto;
}
.crafting-panel {
  max-width: 560px;
  max-height: 85vh;
  overflow-y: auto;
}
.inventory-grid {
  display: grid;
  grid-template-columns: repeat(7, 52px);
  gap: 6px;
  margin: 10px 0;
}
.inv-slot {
  position: relative;
  width: 52px;
  height: 52px;
  background: rgba(255, 255, 255, 0.06);
  border: 2px solid rgba(255, 255, 255, 0.25);
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
.inv-slot:hover {
  border-color: rgba(255, 255, 255, 0.5);
}
.inv-slot.selected {
  border-color: #e8dfa0;
  box-shadow: 0 0 8px rgba(232, 223, 160, 0.7);
}
.crafting-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin: 10px 0;
}
.recipe-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px;
  background: rgba(255, 255, 255, 0.05);
  border-radius: 6px;
}
.recipe-inputs {
  display: flex;
  gap: 8px;
}
.recipe-chip {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  color: #b7e8c9;
}
.recipe-chip .swatch {
  width: 20px;
  height: 20px;
  font-size: 12px;
}
.recipe-chip.insufficient {
  color: #e08a8a;
}
.recipe-arrow {
  opacity: 0.6;
}
.recipe-output {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  flex: 1;
}
.recipe-output .swatch {
  width: 24px;
  height: 24px;
  font-size: 14px;
}
.craft-btn {
  padding: 6px 14px;
  font-size: 13px;
  font-weight: 600;
  color: #0a0a0a;
  background: #b7e8c9;
  border: none;
  border-radius: 5px;
  cursor: pointer;
}
.craft-btn:hover:not(:disabled) {
  background: #cdf5da;
}
.craft-btn:disabled {
  background: rgba(255, 255, 255, 0.15);
  color: rgba(255, 255, 255, 0.4);
  cursor: not-allowed;
}
#equip-status {
  min-height: 1.2em;
}
.selected-item-name {
  min-height: 1.2em;
  color: #eaeaea !important;
  font-weight: 600;
}
#reset-world-btn {
  margin-top: 4px;
  padding: 8px 14px;
  font-size: 13px;
  font-weight: 600;
  color: #f0d0d0;
  background: rgba(200, 60, 60, 0.18);
  border: 1px solid rgba(224, 138, 138, 0.5);
  border-radius: 6px;
  cursor: pointer;
}
#reset-world-btn:hover {
  background: rgba(200, 60, 60, 0.3);
}
.save-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 4px;
}
.util-btn-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 4px;
}
.util-btn {
  padding: 8px 14px;
  font-size: 13px;
  font-weight: 600;
  color: #cfe4ea;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(159, 212, 222, 0.4);
  border-radius: 6px;
  cursor: pointer;
}
.util-btn:hover {
  background: rgba(255, 255, 255, 0.15);
}

/* Multiplayer join row (start overlay) */
#multiplayer-join {
  margin-top: 16px;
  padding-top: 12px;
  border-top: 1px solid rgba(255, 255, 255, 0.15);
}
.multiplayer-join-row {
  display: flex;
  gap: 8px;
}
#multiplayer-host-input,
#multiplayer-name-input {
  flex: 1;
  min-width: 0;
  padding: 8px 10px;
  font-size: 13px;
  color: #eaeaea;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(159, 212, 222, 0.4);
  border-radius: 6px;
}
#multiplayer-host-input::placeholder,
#multiplayer-name-input::placeholder {
  color: rgba(234, 234, 234, 0.4);
}

/* Desktop vs touch instructions: only one variant renders, based on body class */
.touch-controls-help {
  display: none;
}
body.touch-device .desktop-controls-help {
  display: none;
}
body.touch-device .touch-controls-help {
  display: block;
}

/* Touch controls (joystick + action buttons) */
#touch-controls {
  position: fixed;
  inset: 0;
  z-index: 6;
  pointer-events: none;
  /* iOS Safari can trigger its text-selection magnifier/callout on a long
     press anywhere in here (joystick, buttons) even though none of this is
     actually selectable text — lock it down explicitly rather than relying
     on each child's own user-select. */
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}
#touch-controls > * {
  pointer-events: auto;
}
.touch-joystick-base {
  position: fixed;
  left: 28px;
  bottom: 28px;
  width: 110px;
  height: 110px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.08);
  border: 2px solid rgba(255, 255, 255, 0.25);
  transform-origin: bottom left;
  transition: transform 0.15s ease;
  touch-action: none;
}
.touch-joystick-knob {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 48px;
  height: 48px;
  margin: -24px 0 0 -24px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.35);
  border: 1px solid rgba(255, 255, 255, 0.5);
  transition: transform 0.05s linear;
}
.touch-buttons {
  position: fixed;
  right: 20px;
  bottom: 24px;
  display: grid;
  grid-template-columns: repeat(2, 64px);
  gap: 10px;
  align-items: end;
}
.touch-btn {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.12);
  border: 2px solid rgba(255, 255, 255, 0.3);
  color: #eaeaea;
  font-size: 12px;
  font-weight: 700;
  touch-action: none;
}
.touch-btn:active {
  background: rgba(255, 255, 255, 0.3);
}
.touch-btn-primary {
  background: rgba(232, 223, 160, 0.25);
  border-color: rgba(232, 223, 160, 0.6);
}
.touch-btn-primary:active {
  background: rgba(232, 223, 160, 0.45);
}
