* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html {
  min-height: 100%;
  background: #121417;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100svh;
  display: grid;
  place-items: center;
  overflow: auto;
  background:
    radial-gradient(circle at 50% 8%, rgba(255, 238, 174, 0.3), transparent 30%),
    linear-gradient(135deg, #20252b, #121417);
  font-family: "Microsoft YaHei", "PingFang SC", "Noto Sans CJK SC", sans-serif;
  user-select: none;
  -webkit-user-select: none;
}

.game-wrap {
  position: relative;
  width: min(1280px, 100vw, calc((100svh - 84px) * 16 / 9));
  height: auto;
  display: grid;
  grid-template-rows: auto auto;
  gap: 10px;
  padding: 10px;
}

.loading-screen {
  position: absolute;
  inset: 10px 10px 72px;
  z-index: 10;
  place-items: center;
  border: 5px solid #17100c;
  border-radius: 10px;
  background: linear-gradient(145deg, #f8d890, #d98230);
  color: #27150d;
  text-align: center;
}

.loading-screen {
  display: grid;
  font-size: clamp(18px, 3vw, 30px);
  font-weight: 900;
}

.loading-screen::before {
  content: "";
  width: 38px;
  height: 38px;
  margin-bottom: 14px;
  border: 6px solid rgba(39, 21, 13, 0.2);
  border-top-color: #8c2d19;
  border-radius: 50%;
  animation: loading-spin 0.8s linear infinite;
}

body.assets-ready .loading-screen {
  display: none;
}

body:not(.assets-ready) canvas,
body:not(.assets-ready) .toolbar {
  visibility: hidden;
}

@keyframes loading-spin {
  to { transform: rotate(360deg); }
}

canvas {
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 16 / 9;
  border: 5px solid #17100c;
  border-radius: 10px;
  background: #f5cf86;
  box-shadow: 0 18px 40px rgba(0, 0, 0, 0.35);
  cursor: pointer;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.toolbar {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 10px;
}

button {
  min-height: 52px;
  border: 4px solid #17100c;
  border-radius: 999px;
  background: linear-gradient(#ffe06b, #e89a24);
  box-shadow: 0 6px 0 rgba(0, 0, 0, 0.35);
  color: #22140d;
  font: inherit;
  font-size: 18px;
  font-weight: 900;
  cursor: pointer;
  touch-action: manipulation;
}

button:active {
  transform: translateY(3px);
  box-shadow: 0 3px 0 rgba(0, 0, 0, 0.35);
}

button:focus,
canvas:focus {
  outline: none;
}

#action-upgrade {
  background: linear-gradient(#9ff09a, #43a84e);
}

button:disabled {
  filter: grayscale(0.7);
  opacity: 0.58;
  cursor: not-allowed;
  transform: none;
  box-shadow: 0 3px 0 rgba(0, 0, 0, 0.3);
}

@media (max-width: 900px), (max-height: 520px) {
  html,
  body {
    position: fixed;
    inset: 0;
    width: 100%;
    height: 100%;
    min-height: 100%;
    overflow: hidden;
    overscroll-behavior: none;
    touch-action: none;
  }

  .game-wrap {
    width: 100vw;
    height: 100svh;
    padding:
      max(2px, env(safe-area-inset-top))
      max(2px, env(safe-area-inset-right))
      max(2px, env(safe-area-inset-bottom))
      max(2px, env(safe-area-inset-left));
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    gap: 4px;
    overflow: hidden;
    overscroll-behavior: none;
    touch-action: none;
  }

  .loading-screen {
    position: fixed;
    inset: 0;
    border: 0;
    border-radius: 0;
  }

  canvas {
    position: static;
    width: 0;
    height: 100%;
    max-width: none;
    max-height: none;
    flex: 1 1 auto;
    aspect-ratio: auto;
    transform: none;
    border: 0;
    border-radius: 0;
    box-shadow: none;
  }

  .toolbar {
    position: static;
    width: 68px;
    height: 100%;
    flex: 0 0 68px;
    z-index: 8;
    grid-template-columns: 1fr;
    grid-template-rows: repeat(4, minmax(0, 1fr));
    gap: 4px;
  }

  button {
    min-width: 0;
    min-height: 40px;
    padding: 4px;
    border-width: 2px;
    box-shadow: 0 3px 0 rgba(0, 0, 0, 0.35);
    font-size: clamp(12px, 2.5vw, 15px);
  }
}

@media (max-width: 900px) and (orientation: portrait) {
  .game-wrap {
    position: fixed;
    top: 50%;
    left: 50%;
    width: 100svh;
    height: 100vw;
    padding: 2px;
    transform: translate(-50%, -50%) rotate(90deg);
    transform-origin: center;
  }

  canvas {
    max-height: none;
  }

  .toolbar {
    width: 68px;
  }
}
