* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  min-height: 100%;
  margin: 0;
}

body {
  font-family:
    Inter, "Microsoft YaHei", "PingFang SC", system-ui, -apple-system,
    sans-serif;
  overflow-x: hidden;
  color: #fffdf4;
  background:
    radial-gradient(circle at 18% 8%, rgba(255, 255, 255, 0.65), transparent 18%),
    linear-gradient(135deg, rgba(98, 205, 64, 0.38), transparent 30%),
    linear-gradient(180deg, #8edbff 0%, #76d757 42%, #55c33d 100%);
  background-size: 100% 100%;
  background-position: center;
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  background-image:
    linear-gradient(rgba(255, 255, 255, 0.16) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255, 255, 255, 0.12) 1px, transparent 1px);
  background-size: 54px 54px;
  mask-image: radial-gradient(circle at center, black, transparent 86%);
}

.app {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.game-shell {
  width: min(100%, 960px);
  position: relative;
}

.topbar {
  position: relative;
  display: flex;
  justify-content: flex-start;
  gap: 16px;
  align-items: center;
  min-height: 220px;
  margin-bottom: 14px;
}

.title-block {
  position: fixed;
  top: 34px;
  left: 48px;
  width: min(330px, calc(100vw - 80px));
  min-width: 0;
  z-index: 10;
  pointer-events: none;
}

.tool-hud {
  position: absolute;
  top: 138px;
  left: 0;
  z-index: 10;
  flex: 0 0 330px;
  margin-left: 0;
  min-height: 74px;
  padding: 10px 12px 8px;
  display: flex;
  align-items: center;
  gap: 10px;
  border: 3px solid rgba(255, 92, 92, 0);
  border-radius: 8px;
  background: rgba(11, 24, 34, 0.2);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.16),
    0 8px 0 rgba(56, 118, 54, 0.22);
}

.tool-hud-title {
  flex: 0 0 auto;
  margin: 0 2px 12px 0;
  font-size: 17px;
  font-weight: 900;
  color: #fff8d4;
  text-shadow: 0 2px 0 rgba(83, 63, 23, 0.38);
}

.tool-hud-row {
  display: flex;
  align-items: center;
}

.tool-hud-slots {
  display: flex;
  flex: 0 0 auto;
  align-items: flex-start;
  gap: 8px;
}

.tool-hud-item {
  width: 58px;
  display: grid;
  justify-items: center;
  gap: 3px;
}

.tool-hud-slot {
  width: 52px;
  height: 44px;
  display: grid;
  place-items: center;
  padding: 4px;
  border: 2px solid color-mix(in srgb, var(--tool-color), white 18%);
  border-radius: 6px;
  background: rgba(13, 24, 34, 0.7);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.12);
}

.tool-hud-slot img {
  width: 30px;
  height: 30px;
  object-fit: contain;
  opacity: 0.36;
  filter: saturate(0.75);
}

.tool-hud-caption {
  width: 100%;
  color: #fff8d8;
  font-size: 10px;
  font-weight: 900;
  line-height: 1;
  text-align: center;
  white-space: nowrap;
  text-shadow: 0 1px 0 rgba(45, 78, 28, 0.44);
}

.tool-hud-slot.is-unlocked {
  background: color-mix(in srgb, var(--tool-color), #152634 76%);
  border-color: var(--tool-color);
}

.tool-hud-slot.is-unlocked img {
  opacity: 1;
  filter: none;
}

.tool-hud-slot.is-unlocked .tool-hud-caption {
  color: #ffffff;
}

.tool-hud-slot.is-active {
  transform: translateY(-1px);
  box-shadow:
    0 0 0 2px rgba(255, 255, 255, 0.18),
    0 0 18px color-mix(in srgb, var(--tool-color), transparent 20%);
}

h1 {
  margin: 0 0 4px;
  font-size: clamp(28px, 3.1vw, 42px);
  font-weight: 800;
  color: #fffaf0;
  text-shadow:
    0 3px 0 rgba(126, 88, 32, 0.42),
    0 7px 18px rgba(43, 112, 22, 0.24);
}

p {
  margin: 0;
  max-width: 320px;
  font-size: clamp(14px, 1.35vw, 17px);
  color: #fff6d8;
  font-weight: 700;
  line-height: 1.25;
  overflow-wrap: break-word;
  text-shadow: 0 2px 0 rgba(45, 105, 27, 0.28);
}

.status-pill {
  position: absolute;
  top: 138px;
  right: 0;
  z-index: 10;
  min-width: 112px;
  padding: 10px 14px;
  border: 2px solid rgba(255, 245, 191, 0.9);
  border-radius: 8px;
  background: linear-gradient(180deg, #f7b440, #d97824);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.18),
    0 9px 0 rgba(139, 80, 25, 0.45),
    0 14px 22px rgba(70, 111, 29, 0.22);
  text-align: center;
  font-weight: 700;
  user-select: none;
}

.sound-control {
  display: flex;
  position: absolute;
  top: 138px;
  right: 130px;
  z-index: 10;
  flex: 0 0 auto;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  border-radius: 8px;
  background: rgba(11, 24, 34, 0.12);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.14),
    0 8px 0 rgba(56, 118, 54, 0.18);
  user-select: none;
}

.sound-toggle {
  position: relative;
  width: 76px;
  height: 38px;
  border: 2px solid rgba(255, 245, 191, 0.9);
  border-radius: 999px;
  overflow: hidden;
  color: rgba(255, 250, 240, 0.66);
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.26), transparent 42%),
    linear-gradient(180deg, #f7b440, #d97824);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.18),
    inset 0 -5px 9px rgba(129, 65, 20, 0.22),
    0 6px 0 rgba(139, 80, 25, 0.45),
    0 10px 18px rgba(70, 111, 29, 0.2);
  font: inherit;
  font-size: 17px;
  font-weight: 900;
  letter-spacing: 0;
  cursor: pointer;
  touch-action: none;
  transition:
    background 160ms ease,
    box-shadow 160ms ease,
    transform 120ms ease;
}

.sound-toggle-text {
  position: absolute;
  left: 13px;
  top: 50%;
  transform: translateY(-50%);
  z-index: 1;
  line-height: 1;
  text-shadow: 0 2px 0 rgba(127, 58, 18, 0.42);
}

.sound-toggle-knob {
  position: absolute;
  top: 3px;
  right: 3px;
  z-index: 2;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background:
    radial-gradient(circle at 32% 28%, rgba(255, 255, 255, 1), rgba(255, 255, 255, 0.92) 48%, rgba(238, 238, 238, 0.96) 100%);
  box-shadow:
    inset 0 2px 2px rgba(255, 255, 255, 0.95),
    inset 0 -2px 4px rgba(179, 179, 179, 0.2),
    0 3px 8px rgba(0, 0, 0, 0.24);
  transition:
    left 180ms ease,
    right 180ms ease;
}

.sound-toggle-label {
  width: 64px;
  color: #fffaf0;
  font-size: 14px;
  font-weight: 900;
  line-height: 1.1;
  text-shadow: 0 2px 0 rgba(45, 105, 27, 0.28);
}

.sound-toggle.is-muted {
  color: rgba(255, 250, 240, 0.62);
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.18), transparent 42%),
    linear-gradient(180deg, #ca8b45, #986337);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.14),
    inset 0 -4px 8px rgba(80, 45, 23, 0.18),
    0 6px 0 rgba(97, 57, 27, 0.42),
    0 10px 18px rgba(70, 111, 29, 0.16);
}

.sound-toggle.is-muted .sound-toggle-text {
  left: 36px;
  text-shadow: 0 2px 0 rgba(86, 48, 24, 0.42);
}

.sound-toggle.is-muted .sound-toggle-knob {
  right: 41px;
}

.sound-toggle:active {
  transform: translateY(1px);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.14),
    inset 0 -4px 7px rgba(129, 65, 20, 0.18),
    0 4px 0 rgba(139, 80, 25, 0.38);
}

#game {
  width: 100%;
  overflow: hidden;
  border: 3px solid rgba(255, 242, 184, 0.92);
  border-radius: 8px;
  background: #83d850;
  box-shadow:
    0 18px 0 rgba(101, 139, 41, 0.45),
    0 24px 48px rgba(66, 112, 31, 0.28),
    0 0 0 8px rgba(255, 249, 202, 0.24);
}

#game canvas {
  display: block;
  width: 100%;
  height: auto;
  max-width: 100%;
}

.mobile-controls {
  display: none;
  justify-content: space-between;
  align-items: center;
  gap: 18px;
  margin-top: 16px;
}

.dpad {
  width: 150px;
  display: grid;
  grid-template-columns: repeat(3, 46px);
  grid-template-rows: repeat(3, 46px);
  gap: 6px;
}

.dpad button,
.action-button,
.tool-button {
  border: 0;
  border-radius: 8px;
  color: #fffaf0;
  background: linear-gradient(180deg, #6fb6ff, #3d88df);
  font-size: 20px;
  font-weight: 800;
  touch-action: manipulation;
  box-shadow:
    inset 0 2px 0 rgba(255, 255, 255, 0.18),
    0 8px 0 rgba(39, 91, 139, 0.48);
}

.dpad button:active,
.action-button:active,
.tool-button:active {
  transform: translateY(1px);
  box-shadow:
    inset 0 2px 0 rgba(255, 255, 255, 0.14),
    0 5px 0 rgba(39, 91, 139, 0.48);
}

.dpad [data-dir="up"] {
  grid-column: 2;
  grid-row: 1;
}

.dpad [data-dir="left"] {
  grid-column: 1;
  grid-row: 2;
}

.dpad [data-dir="down"] {
  grid-column: 2;
  grid-row: 3;
}

.dpad [data-dir="right"] {
  grid-column: 3;
  grid-row: 2;
}

.action-stack {
  display: grid;
  gap: 10px;
}

.action-button {
  width: 118px;
  height: 62px;
  background: linear-gradient(180deg, #ffb24c, #e37822);
}

.tool-button {
  width: 118px;
  height: 46px;
  background: linear-gradient(180deg, #7cc7ff, #3f91df);
}

.wand-button {
  background: linear-gradient(180deg, #b589ff, #7a51d0);
}

.grenade-button {
  background: linear-gradient(180deg, #9ed66d, #4b9a35);
  font-size: 17px;
}

.help {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  margin-top: 12px;
  color: #fffdf2;
  font-size: 14px;
  font-weight: 700;
  text-shadow: 0 2px 0 rgba(52, 111, 26, 0.34);
}

@media (max-width: 1099px) {
  .topbar {
    flex-wrap: wrap;
    align-items: flex-start;
  }

  .title-block,
  .tool-hud,
  .sound-control,
  .status-pill {
    position: static;
    pointer-events: auto;
  }

  .title-block {
    order: 1;
    width: 100%;
    flex: 1 1 100%;
  }

  .tool-hud {
    order: 2;
    flex: 1 1 100%;
    margin-left: 0;
  }

  .sound-control {
    order: 3;
  }

  .status-pill {
    order: 4;
  }
}

@media (max-width: 760px) {
  html,
  body {
    min-height: 100%;
  }

  body {
    overflow-x: hidden;
    -webkit-text-size-adjust: 100%;
  }

  .app {
    min-height: 100svh;
    padding: max(8px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right))
      max(10px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
    place-items: start center;
  }

  .game-shell {
    width: min(100%, 960px);
  }

  .topbar {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 8px;
    align-items: start;
    min-height: 0;
    margin-bottom: 8px;
  }

  .title-block {
    position: static;
    order: 1;
    grid-column: 1 / -1;
    width: 100%;
    flex: 1 1 100%;
    pointer-events: auto;
  }

  h1 {
    margin-bottom: 2px;
    font-size: 25px;
    line-height: 1.02;
  }

  p {
    max-width: 100%;
    font-size: 13px;
    line-height: 1.18;
  }

  .tool-hud {
    order: 2;
    grid-column: 1 / -1;
    width: 100%;
    min-height: 60px;
    flex: 1 1 100%;
    gap: 7px;
    padding: 7px 8px 6px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  .tool-hud-title {
    margin: 0 0 10px;
    font-size: 15px;
  }

  .tool-hud-slots {
    gap: 6px;
  }

  .tool-hud-item {
    width: 49px;
  }

  .tool-hud-slot {
    width: 45px;
    height: 39px;
    padding: 3px;
    border-radius: 6px;
  }

  .tool-hud-slot img {
    width: 27px;
    height: 27px;
  }

  .tool-hud-caption {
    font-size: 9px;
  }

  .sound-control {
    order: 3;
    grid-column: 1;
    justify-self: start;
    gap: 6px;
    padding: 5px 7px;
  }

  .status-pill {
    order: 4;
    grid-column: 2;
    justify-self: end;
    min-width: 86px;
    padding: 8px 10px;
    font-size: 13px;
  }

  .topbar p {
    display: block;
  }

  .sound-toggle {
    width: 66px;
    height: 34px;
    font-size: 14px;
  }

  .sound-toggle-text {
    left: 10px;
  }

  .sound-toggle-knob {
    width: 28px;
    height: 28px;
  }

  .sound-toggle.is-muted .sound-toggle-text {
    left: 31px;
  }

  .sound-toggle.is-muted .sound-toggle-knob {
    right: 35px;
  }

  .sound-toggle-label {
    width: 54px;
    font-size: 12px;
  }

  #game {
    width: 100%;
    max-width: 100%;
    border-width: 2px;
    border-radius: 6px;
    box-shadow:
      0 10px 0 rgba(101, 139, 41, 0.4),
      0 16px 28px rgba(66, 112, 31, 0.24),
      0 0 0 5px rgba(255, 249, 202, 0.22);
  }

  #game canvas {
    width: 100% !important;
    max-width: 100% !important;
    height: auto !important;
  }

  .mobile-controls {
    display: grid;
    grid-template-columns: 116px minmax(0, calc(100vw - 152px));
    gap: 8px;
    align-items: center;
    margin-top: 12px;
    width: 100%;
    overflow: hidden;
  }

  .dpad {
    width: 116px;
    grid-template-columns: repeat(3, 36px);
    grid-template-rows: repeat(3, 36px);
    gap: 4px;
  }

  .dpad button {
    min-width: 36px;
    min-height: 36px;
    font-size: 18px;
  }

  .action-stack {
    grid-template-columns: minmax(0, 1fr);
    gap: 6px;
    width: 100%;
    max-width: calc(100vw - 152px);
    min-width: 0;
  }

  .action-button,
  .tool-button {
    min-width: 0;
    width: 100%;
    height: 38px;
    border-radius: 8px;
    font-size: 15px;
    line-height: 1;
  }

  .action-button {
    height: 42px;
  }

  .help {
    flex-direction: column;
    gap: 4px;
    margin-top: 10px;
    font-size: 12px;
    line-height: 1.25;
  }
}

@media (max-width: 760px) and (orientation: landscape) {
  .app {
    padding: max(6px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right))
      max(6px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
  }

  .game-shell {
    width: min(100%, calc((100svh - 122px) * 1.607));
  }

  .topbar {
    grid-template-columns: minmax(184px, 1fr) auto auto;
    gap: 6px;
    margin-bottom: 6px;
  }

  .title-block {
    grid-column: 1;
  }

  h1 {
    font-size: 20px;
  }

  p {
    font-size: 11px;
    max-width: 260px;
  }

  .tool-hud {
    grid-column: 2;
    width: auto;
    min-height: 52px;
    padding: 6px 7px 5px;
  }

  .tool-hud-title {
    font-size: 13px;
  }

  .tool-hud-item {
    width: 43px;
  }

  .tool-hud-slot {
    width: 39px;
    height: 34px;
  }

  .tool-hud-slot img {
    width: 23px;
    height: 23px;
  }

  .tool-hud-caption {
    font-size: 8px;
  }

  .sound-control {
    grid-column: 3;
    grid-row: 1;
  }

  .status-pill {
    grid-column: 3;
    grid-row: 2;
  }

  .mobile-controls {
    margin-top: 7px;
  }

  .dpad {
    width: 108px;
    grid-template-columns: repeat(3, 34px);
    grid-template-rows: repeat(3, 34px);
  }

  .dpad button {
    min-width: 34px;
    min-height: 34px;
    font-size: 16px;
  }

  .action-stack {
    grid-template-columns: repeat(4, minmax(66px, 1fr));
  }

  .action-button,
  .tool-button {
    height: 38px;
    font-size: 13px;
  }

  .help {
    display: none;
  }
}

@media (max-width: 980px) and (max-height: 520px) {
  .app {
    min-height: 100svh;
    padding: max(6px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right))
      max(6px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
    place-items: start center;
  }

  .game-shell {
    width: min(100%, calc((100svh - 116px) * 1.607));
  }

  .topbar {
    width: min(calc(100vw - 16px), 820px);
    margin-left: 50%;
    transform: translateX(-50%);
    display: grid;
    grid-template-columns: minmax(160px, 1fr) auto auto;
    gap: 6px;
    align-items: start;
    min-height: 0;
    margin-bottom: 6px;
  }

  .title-block,
  .tool-hud,
  .sound-control,
  .status-pill {
    position: static;
    pointer-events: auto;
  }

  .title-block {
    grid-column: 1;
    width: 100%;
  }

  h1 {
    margin-bottom: 1px;
    font-size: 20px;
    line-height: 1.02;
  }

  p {
    max-width: 250px;
    font-size: 11px;
    line-height: 1.15;
  }

  .tool-hud {
    grid-column: 2;
    width: 250px;
    min-height: 52px;
    padding: 6px 7px 5px;
    gap: 6px;
    overflow: visible;
  }

  .tool-hud-title {
    margin: 0 0 9px;
    font-size: 13px;
  }

  .tool-hud-slots {
    gap: 5px;
  }

  .tool-hud-item {
    width: 43px;
  }

  .tool-hud-slot {
    width: 39px;
    height: 34px;
    padding: 3px;
  }

  .tool-hud-slot img {
    width: 23px;
    height: 23px;
  }

  .tool-hud-caption {
    font-size: 8px;
  }

  .sound-control {
    grid-column: 3;
    grid-row: 1;
    gap: 5px;
    padding: 5px 6px;
  }

  .sound-toggle {
    width: 60px;
    height: 32px;
    font-size: 13px;
  }

  .sound-toggle-knob {
    width: 26px;
    height: 26px;
  }

  .sound-toggle.is-muted .sound-toggle-text {
    left: 28px;
  }

  .sound-toggle.is-muted .sound-toggle-knob {
    right: 31px;
  }

  .sound-toggle-label {
    width: 48px;
    font-size: 11px;
  }

  .status-pill {
    grid-column: 3;
    grid-row: 2;
    justify-self: end;
    min-width: 80px;
    padding: 7px 9px;
    font-size: 12px;
  }

  #game {
    width: 100%;
    max-width: 100%;
    border-width: 2px;
    border-radius: 6px;
    box-shadow:
      0 8px 0 rgba(101, 139, 41, 0.4),
      0 12px 20px rgba(66, 112, 31, 0.22),
      0 0 0 5px rgba(255, 249, 202, 0.22);
  }

  #game canvas {
    width: 100% !important;
    max-width: 100% !important;
    height: auto !important;
  }

  .mobile-controls {
    display: grid;
    grid-template-columns: 108px minmax(0, 1fr);
    gap: 8px;
    align-items: center;
    width: 100%;
    margin-top: 6px;
    overflow: hidden;
  }

  .dpad {
    width: 108px;
    grid-template-columns: repeat(3, 34px);
    grid-template-rows: repeat(3, 34px);
    gap: 3px;
  }

  .dpad button {
    min-width: 34px;
    min-height: 34px;
    font-size: 16px;
  }

  .action-stack {
    width: 100%;
    min-width: 0;
    grid-template-columns: repeat(4, minmax(58px, 1fr));
    gap: 6px;
  }

  .action-button,
  .tool-button {
    min-width: 0;
    width: 100%;
    height: 36px;
    font-size: 12px;
  }

  .help {
    display: none;
  }
}
