:root {
  --hud-text: #f4f7ff;
  --hud-shadow: rgba(0, 0, 0, 0.6);
  --panel-bg: rgba(9, 14, 24, 0.5);
  --panel-border: rgba(255, 255, 255, 0.14);
  --slot-size: 52px;
  --slot-bg: #8f8f8f;
  --slot-border: #383838;
  --slot-inner: #b3b3b3;
  --tg-safe-top: env(safe-area-inset-top, 0px);
  --tg-safe-bottom: env(safe-area-inset-bottom, 0px);
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: radial-gradient(circle at 50% 18%, #a6d8ff 0%, #7cb8f0 40%, #4f90c8 100%);
  font-family: "Trebuchet MS", "Segoe UI", sans-serif;
  /* Prevent the browser from intercepting touches for native gestures. */
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: none;
  overscroll-behavior: none;
}

body.telegram-webapp {
  /* Keep the HUD clear of the Telegram status bar and bottom safe area. */
  padding-top: var(--tg-safe-top);
  padding-bottom: var(--tg-safe-bottom);
  box-sizing: border-box;
}

#game-canvas {
  display: block;
  width: 100vw;
  height: 100vh;
}

#hud {
  position: fixed;
  inset: 0;
  pointer-events: none;
  color: var(--hud-text);
}

#underwater-overlay {
  position: absolute;
  inset: 0;
  pointer-events: none;
  opacity: 0;
  background: linear-gradient(
    180deg,
    rgba(50, 96, 150, 0.56) 0%,
    rgba(34, 72, 125, 0.72) 58%,
    rgba(22, 50, 98, 0.82) 100%
  );
  transition: opacity 120ms linear;
}

#damage-overlay {
  position: absolute;
  inset: 0;
  pointer-events: none;
  opacity: 0;
  background: radial-gradient(
    ellipse at center,
    rgba(180, 0, 0, 0.0) 30%,
    rgba(180, 0, 0, 0.6) 100%
  );
  transition: opacity 150ms ease-out;
}

#crosshair {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 18px;
  height: 18px;
  transform: translate(-50%, -50%);
}

#crosshair::before,
#crosshair::after {
  content: "";
  position: absolute;
  background: #ffffff;
  box-shadow: 0 0 6px var(--hud-shadow);
}

#crosshair::before {
  left: 8px;
  top: 0;
  width: 2px;
  height: 18px;
}

#crosshair::after {
  left: 0;
  top: 8px;
  width: 18px;
  height: 2px;
}

#hint {
  position: absolute;
  left: 50%;
  bottom: 130px;
  transform: translateX(-50%);
  max-width: calc(100vw - 40px);
  padding: 10px 14px;
  border-radius: 10px;
  border: 1px solid var(--panel-border);
  background: var(--panel-bg);
  color: #f3f7ff;
  text-align: center;
  text-wrap: balance;
  text-shadow: 0 1px 2px var(--hud-shadow);
  font-size: 13px;
  line-height: 1.35;
}

body.locked #hint {
  opacity: 0;
}

body.inventory-open #crosshair,
body.inventory-open #hotbar,
body.inventory-open #held-item-canvas,
body.inventory-open #hint,
body.inventory-open #hearts-bar {
  opacity: 0;
}

#hotbar {
  position: absolute;
  left: 50%;
  bottom: 18px;
  transform: translateX(-50%);
  display: grid;
  grid-template-columns: repeat(9, var(--slot-size));
  gap: 6px;
  padding: 8px;
  border-radius: 12px;
  border: 1px solid var(--panel-border);
  background: var(--panel-bg);
}

.slot {
  position: relative;
  width: var(--slot-size);
  height: var(--slot-size);
  box-sizing: border-box;
  border: 2px solid var(--slot-border);
  border-radius: 2px;
  background: var(--slot-bg);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  cursor: pointer;
  pointer-events: auto;
  box-shadow: inset -2px -2px 0 rgba(0, 0, 0, 0.28), inset 2px 2px 0 var(--slot-inner);
}

.slot.active {
  border-color: #f7f7b2;
  box-shadow: 0 0 0 2px rgba(246, 242, 165, 0.72), inset -2px -2px 0 rgba(0, 0, 0, 0.28),
    inset 2px 2px 0 var(--slot-inner);
  background: #9f9f9f;
}

.slot.empty {
  opacity: 0.55;
}

.slot-preview {
  width: 100%;
  height: 100%;
  background-position: center;
  background-size: 90% 90%;
  background-repeat: no-repeat;
  image-rendering: pixelated;
  filter: drop-shadow(0 2px 2px rgba(0, 0, 0, 0.45));
}

.slot-count {
  position: absolute;
  right: 2px;
  bottom: 1px;
  font-size: 11px;
  font-weight: 900;
  color: #fff;
  text-shadow: 1px 1px 0 #222, -1px -1px 0 #222, 1px -1px 0 #222, -1px 1px 0 #222;
  pointer-events: none;
  line-height: 1;
}

.slot-hud {
  pointer-events: none;
}

#held-item-canvas {
  position: absolute;
  width: 390px;
  height: 390px;
  right: -16px;
  bottom: -26px;
  pointer-events: none;
  opacity: 1;
  z-index: 3;
}

/* --- Hearts UI --- */

#hearts-bar {
  position: absolute;
  left: 50%;
  bottom: 90px;
  transform: translateX(-50%);
  display: none;
  gap: 2px;
  pointer-events: none;
  z-index: 2;
}

.heart {
  font-size: 16px;
  line-height: 1;
  filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.6));
  transition: transform 0.1s ease;
}

.heart-full {
  color: #e22;
}

.heart-empty {
  color: #444;
  opacity: 0.5;
}

.hearts-flash .heart-full {
  animation: heart-pulse 0.3s ease;
}

@keyframes heart-pulse {
  0% { transform: scale(1); }
  40% { transform: scale(1.35); }
  100% { transform: scale(1); }
}

/* --- Main Menu --- */

#main-menu {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(ellipse at 50% 30%, #1a3a5c 0%, #0d1b2a 70%, #050a12 100%);
  z-index: 1000;
}

.menu-panel {
  text-align: center;
  color: #e8edf5;
}

.menu-title {
  font-size: 52px;
  font-weight: 900;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  margin: 0 0 6px;
  text-shadow: 0 3px 12px rgba(0, 0, 0, 0.6), 0 0 40px rgba(100, 180, 255, 0.15);
  background: linear-gradient(180deg, #f0f4ff 0%, #a0c4e8 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.menu-subtitle {
  font-size: 14px;
  color: #7a9cb8;
  margin-bottom: 40px;
  letter-spacing: 0.08em;
}

.menu-buttons {
  display: flex;
  gap: 18px;
  justify-content: center;
  flex-wrap: wrap;
}

.menu-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  width: 180px;
  padding: 22px 16px;
  border: 2px solid rgba(255, 255, 255, 0.12);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.06);
  color: #dce4f0;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.1s;
  font-family: inherit;
}

.menu-btn:hover {
  background: rgba(255, 255, 255, 0.12);
  border-color: rgba(255, 255, 255, 0.28);
  transform: translateY(-2px);
}

.menu-btn:active {
  transform: translateY(0);
}

.menu-btn-icon {
  font-size: 32px;
  line-height: 1;
}

.menu-btn-survival .menu-btn-icon {
  color: #e8a44c;
}

.menu-btn-creative .menu-btn-icon {
  color: #6ccaff;
}

.menu-btn-label {
  font-size: 18px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.menu-btn-desc {
  font-size: 11px;
  color: #8a9bb0;
  line-height: 1.3;
}

/* --- Death Screen --- */

#death-screen {
  position: fixed;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  background: rgba(120, 0, 0, 0.55);
  z-index: 900;
  pointer-events: auto;
}

.death-panel {
  text-align: center;
  color: #f5e0e0;
}

.death-title {
  font-size: 48px;
  font-weight: 900;
  margin: 0 0 10px;
  text-shadow: 0 3px 10px rgba(0, 0, 0, 0.7);
  color: #ff4444;
}

.death-subtitle {
  font-size: 16px;
  color: #d4a8a8;
  margin: 0 0 30px;
}

.death-btn {
  padding: 14px 36px;
  border: 2px solid rgba(255, 255, 255, 0.2);
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.08);
  color: #f0e0e0;
  font-size: 18px;
  font-weight: 700;
  font-family: inherit;
  cursor: pointer;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  transition: background 0.15s, border-color 0.15s;
}

.death-btn:hover {
  background: rgba(255, 255, 255, 0.18);
  border-color: rgba(255, 255, 255, 0.35);
}

/* --- Inventory / existing --- */

#inventory-overlay {
  position: absolute;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  pointer-events: auto;
  background: rgba(6, 10, 18, 0.66);
}

#inventory-overlay.visible {
  display: flex;
}

#inventory-panel {
  width: fit-content;
  max-width: calc(100vw - 28px);
  max-height: calc(100vh - 36px);
  overflow: auto;
  border-radius: 4px;
  border: 3px solid #202020;
  box-shadow: inset 0 0 0 3px #c4c4c4, inset 0 0 0 6px #9c9c9c;
  background: #bcbcbc;
  padding: 12px;
  box-sizing: border-box;
}

#inventory-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #222;
  text-shadow: none;
  margin-bottom: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.inventory-hint {
  font-size: 12px;
  opacity: 0.85;
}

#inventory-main {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-bottom: 8px;
}

.inventory-section-title {
  margin: 4px 0 6px;
  color: #2c2c2c;
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

#inventory-left-decor {
  display: grid;
  grid-template-columns: 48px 1fr;
  gap: 8px;
  align-items: start;
}

#inventory-armor-column {
  display: grid;
  grid-template-rows: repeat(4, 44px);
  gap: 6px;
}

.armor-slot {
  border: 2px solid #3b3b3b;
  background: #9d9d9d;
  box-shadow: inset -2px -2px 0 rgba(0, 0, 0, 0.25), inset 2px 2px 0 #c7c7c7;
}

#inventory-player-preview {
  min-height: 196px;
  border: 2px solid #353535;
  background: #111;
}

#inventory-crafting-wrap {
  display: grid;
  align-content: start;
}

#inventory-creative-grid {
  display: grid;
  grid-template-columns: repeat(9, var(--slot-size));
  gap: 4px;
  align-content: start;
}

#inventory-creative {
  grid-column: 1 / -1;
  display: flex;
  justify-content: center;
}

#inventory-creative-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

#inventory-creative-toggle {
  pointer-events: auto;
  border: 2px solid #3c3c3c;
  background: #a7a7a7;
  color: #252525;
  font-weight: 700;
  font-size: 12px;
  text-transform: uppercase;
  padding: 6px 8px;
  cursor: pointer;
  box-shadow: inset -2px -2px 0 rgba(0, 0, 0, 0.25), inset 2px 2px 0 #cfcfcf;
}

#inventory-creative-toggle.active {
  border-color: #9a8d54;
  background: #c5b873;
}

#inventory-creative-panel {
  padding: 4px;
  overflow-y: auto;
  flex: 1;
}

#creative-modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.72);
  align-items: center;
  justify-content: center;
  z-index: 200;
  pointer-events: auto;
}

#creative-modal-overlay.visible {
  display: flex;
}

#creative-modal-panel {
  width: min(600px, calc(100vw - 32px));
  max-height: calc(100vh - 60px);
  display: flex;
  flex-direction: column;
  border-radius: 4px;
  border: 3px solid #202020;
  box-shadow: inset 0 0 0 3px #c4c4c4, inset 0 0 0 6px #9c9c9c;
  background: #bcbcbc;
  padding: 12px;
  box-sizing: border-box;
}

#creative-modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: #222;
  flex-shrink: 0;
}

#creative-modal-close {
  pointer-events: auto;
  border: 2px solid #3c3c3c;
  background: #a7a7a7;
  color: #252525;
  font-weight: 700;
  font-size: 14px;
  width: 32px;
  height: 32px;
  cursor: pointer;
  box-shadow: inset -2px -2px 0 rgba(0, 0, 0, 0.25), inset 2px 2px 0 #cfcfcf;
  display: flex;
  align-items: center;
  justify-content: center;
}

#creative-modal-close:hover {
  background: #bf6060;
  border-color: #7a2020;
  color: #fff;
}

#inventory-storage-grid {
  display: grid;
  grid-template-columns: repeat(9, var(--slot-size));
  gap: 4px;
}

#inventory-hotbar-grid {
  display: grid;
  grid-template-columns: repeat(9, var(--slot-size));
  gap: 4px;
}

#inventory-hotbar-wrap {
  margin-top: 4px;
}

#inventory-trash-slot {
  width: 52px;
  height: 52px;
  pointer-events: auto;
  border: 2px solid #4a1f1f;
  background: linear-gradient(180deg, #d7b0b0 0%, #bf8888 100%);
  color: #2a0f0f;
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  box-shadow: inset -2px -2px 0 rgba(0, 0, 0, 0.25), inset 2px 2px 0 rgba(255, 255, 255, 0.4);
}

#inventory-trash-slot.ready {
  border-color: #9d2020;
  background: linear-gradient(180deg, #e3b0b0 0%, #d56e6e 100%);
}

#inventory-crafting-placeholder {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 12px;
}

#craft-grid-2x2 {
  display: grid;
  grid-template-columns: repeat(2, 42px);
  grid-template-rows: repeat(2, 42px);
  gap: 6px;
}

.craft-slot {
  border: 2px solid #383838;
  background: #9c9c9c;
  box-shadow: inset -2px -2px 0 rgba(0, 0, 0, 0.25), inset 2px 2px 0 #c7c7c7;
}

.craft-arrow {
  font-size: 28px;
  color: #505050;
  user-select: none;
}

.craft-result-slot {
  width: 42px;
  height: 42px;
  border: 2px solid #383838;
  background: #9c9c9c;
  box-shadow: inset -2px -2px 0 rgba(0, 0, 0, 0.25), inset 2px 2px 0 #c7c7c7;
}

#inventory-cursor-item {
  position: fixed;
  width: 44px;
  height: 44px;
  pointer-events: none;
  background-position: center;
  background-size: contain;
  background-repeat: no-repeat;
  image-rendering: pixelated;
  opacity: 0;
  z-index: 100;
}

#debug {
  position: absolute;
  left: 12px;
  top: 12px;
  width: max-content;
  max-width: calc(100vw - 24px);
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--panel-border);
  background: var(--panel-bg);
  color: #ecf2ff;
  text-shadow: 0 1px 2px var(--hud-shadow);
  font-size: 12px;
  line-height: 1.4;
  white-space: pre-line;
}

@media (max-width: 640px) {
  :root {
    --slot-size: 40px;
  }

  #inventory-main {
    grid-template-columns: 1fr;
  }

  #inventory-panel {
    width: calc(100vw - 28px);
  }

  #creative-modal-panel {
    width: calc(100vw - 20px);
  }

  #inventory-creative-grid {
    grid-template-columns: repeat(5, var(--slot-size));
  }

  #held-item-canvas {
    width: 230px;
    height: 230px;
    right: -18px;
    bottom: 18px;
  }

  #hint {
    bottom: 84px;
    font-size: 12px;
  }

  .menu-title {
    font-size: 36px;
  }

  .menu-btn {
    width: 150px;
    padding: 16px 12px;
  }

  .death-title {
    font-size: 36px;
  }
}

/* ============================================================
   Mobile / Telegram touch controls
   ============================================================ */

#mobile-controls {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 50;
  user-select: none;
  -webkit-user-select: none;
}

#mobile-controls .mc-region {
  position: absolute;
  top: 0;
  bottom: 0;
  pointer-events: auto;
  touch-action: none;
}

#mc-move-area {
  left: 0;
  width: 50%;
}

#mc-look-area {
  right: 0;
  width: 50%;
}

/* Joystick base + knob — anchored to bottom-left by default, but the JS
   repositions it to the touch point on press for a more natural feel. */
#mc-joystick-base {
  position: absolute;
  left: calc(20px + var(--tg-safe-bottom, 0px) * 0);
  bottom: calc(20px + var(--tg-safe-bottom, 0px));
  width: 130px;
  height: 130px;
  margin-left: 0;
  margin-top: 0;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.08);
  border: 2px solid rgba(255, 255, 255, 0.18);
  box-shadow: inset 0 0 12px rgba(0, 0, 0, 0.35);
  transition: opacity 120ms;
  opacity: 0.85;
  pointer-events: none;
}

#mc-joystick-base.active {
  position: absolute;
  width: 130px;
  height: 130px;
  /* When active, JS sets left/top in px relative to the move area. */
  transform: translate(-50%, -50%);
  bottom: auto;
  background: rgba(255, 255, 255, 0.14);
  border-color: rgba(255, 255, 255, 0.34);
  opacity: 1;
}

#mc-joystick-knob {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: radial-gradient(circle at 30% 30%, #ffffff 0%, #c8d0e0 60%, #8a93a8 100%);
  border: 2px solid rgba(255, 255, 255, 0.4);
  box-shadow: 0 3px 8px rgba(0, 0, 0, 0.45);
  transform: translate(-50%, -50%);
  pointer-events: none;
}

#mc-buttons-left,
#mc-buttons-right {
  position: absolute;
  display: flex;
  flex-direction: column;
  gap: 10px;
  pointer-events: none;
}

#mc-buttons-left {
  left: calc(14px + env(safe-area-inset-left, 0px));
  top: calc(14px + var(--tg-safe-top, 0px));
}

#mc-buttons-right {
  right: calc(14px + env(safe-area-inset-right, 0px));
  bottom: calc(20px + var(--tg-safe-bottom, 0px));
  align-items: flex-end;
}

.mc-btn {
  pointer-events: auto;
  width: 54px;
  height: 54px;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.28);
  background: rgba(20, 28, 44, 0.55);
  color: #f4f7ff;
  font-size: 22px;
  font-weight: 700;
  font-family: inherit;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 3px 8px rgba(0, 0, 0, 0.45);
  transition: transform 80ms ease-out, background 120ms;
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

.mc-btn:active,
.mc-btn.active {
  background: rgba(120, 170, 240, 0.45);
  transform: scale(0.92);
}

.mc-btn-action {
  width: 64px;
  height: 64px;
  font-size: 24px;
}

.mc-btn-jump {
  width: 72px;
  height: 72px;
  font-size: 28px;
  background: rgba(60, 90, 140, 0.6);
}

.mc-btn-break {
  background: rgba(140, 80, 40, 0.55);
}

.mc-btn-place {
  background: rgba(40, 130, 90, 0.55);
}

/* Default: mobile-controls hidden — only shown when the JS sets the
   .touch-controls class on body. */
#mobile-controls {
  display: none;
}

body.touch-controls #mobile-controls {
  display: block;
}

/* On touch devices, hide the keyboard-focused hint and shrink HUD. */
body.touch-controls #hint {
  display: none;
}

body.touch-controls #crosshair {
  opacity: 0.7;
}

body.touch-controls #debug {
  font-size: 10px;
  padding: 6px 8px;
  max-width: 50%;
  left: 78px; /* keep clear of left-side buttons */
  top: calc(14px + var(--tg-safe-top, 0px));
}

body.touch-controls #hotbar {
  bottom: calc(96px + var(--tg-safe-bottom, 0px));
  transform: translateX(-50%) scale(0.92);
  transform-origin: bottom center;
}

body.touch-controls #held-item-canvas {
  width: 200px;
  height: 200px;
  right: 88px;
  bottom: 80px;
}

/* When the inventory is open, hide the on-screen controls so they don't
   sit on top of the panel. */
#mobile-controls.inventory-mode #mc-move-area,
#mobile-controls.inventory-mode #mc-look-area,
#mobile-controls.inventory-mode #mc-buttons-left,
#mobile-controls.inventory-mode #mc-buttons-right {
  display: none;
}

/* Smaller phones: tighten button sizes. */
@media (max-width: 420px) {
  .mc-btn {
    width: 46px;
    height: 46px;
    font-size: 18px;
  }
  .mc-btn-action {
    width: 56px;
    height: 56px;
    font-size: 20px;
  }
  .mc-btn-jump {
    width: 64px;
    height: 64px;
    font-size: 24px;
  }
  #mc-joystick-base {
    width: 110px;
    height: 110px;
  }
  #mc-joystick-knob {
    width: 48px;
    height: 48px;
  }
  body.touch-controls #hotbar {
    bottom: calc(86px + var(--tg-safe-bottom, 0px));
    transform: translateX(-50%) scale(0.82);
  }
  body.touch-controls #held-item-canvas {
    width: 160px;
    height: 160px;
    right: 76px;
    bottom: 72px;
  }
}
