:root {
  --bg-top: #08111d;
  --bg-bottom: #2b0d31;
  --text: #f6f4ff;
  --muted: rgba(246, 244, 255, 0.72);
  --panel: rgba(10, 18, 34, 0.72);
  --panel-line: rgba(196, 233, 255, 0.14);
  --shadow: rgba(0, 0, 0, 0.4);
  --canvas-w: min(100%, 420px);
  --canvas-h: calc(var(--canvas-w) * 20 / 12);
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

html,
body {
  margin: 0;
  width: 100%;
  min-height: 100%;
  overflow: hidden;
  background:
    radial-gradient(circle at 20% 18%, rgba(87, 145, 255, 0.26), transparent 28%),
    radial-gradient(circle at 82% 38%, rgba(255, 84, 163, 0.16), transparent 32%),
    linear-gradient(180deg, var(--bg-top), var(--bg-bottom));
  color: var(--text);
  font-family: "Afacad", "Noto Sans TC", sans-serif;
  touch-action: manipulation;
}

body {
  position: relative;
  min-height: 100dvh;
  height: 100dvh;
  display: grid;
  place-items: center;
  padding:
    max(10px, env(safe-area-inset-top))
    12px
    max(14px, env(safe-area-inset-bottom))
    12px;
}

.backdrop,
.board-overlay {
  pointer-events: none;
}

.backdrop {
  position: fixed;
  inset: 0;
  overflow: hidden;
}

.glow,
.mist {
  position: absolute;
  border-radius: 999px;
  filter: blur(42px);
}

.glow-a {
  width: 52vw;
  height: 52vw;
  left: -14vw;
  top: 8vh;
  background: rgba(95, 162, 255, 0.28);
  animation: floatGlow 9s ease-in-out infinite;
}

.glow-b {
  width: 58vw;
  height: 58vw;
  right: -16vw;
  bottom: 5vh;
  background: rgba(255, 62, 139, 0.18);
  animation: floatGlow 11s ease-in-out infinite reverse;
}

.mist {
  left: 10vw;
  right: 10vw;
  bottom: -8vh;
  height: 26vh;
  background:
    radial-gradient(circle at 24% 50%, rgba(255, 255, 255, 0.12), transparent 20%),
    radial-gradient(circle at 70% 20%, rgba(141, 247, 255, 0.08), transparent 24%);
  animation: pulseMist 8s ease-in-out infinite;
}

.app-shell {
  position: relative;
  z-index: 1;
  width: min(100%, 520px);
  height: calc(100dvh - max(10px, env(safe-area-inset-top)) - max(14px, env(safe-area-inset-bottom)) - 24px);
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: 10px;
}

.hud {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr)) auto;
  gap: 8px;
  align-items: center;
}

.hud-pill,
.status-pill,
.icon-button {
  border: 1px solid var(--panel-line);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.08),
    0 10px 28px var(--shadow);
  backdrop-filter: blur(14px);
}

.hud-pill {
  min-height: 50px;
  padding: 8px 10px;
  border-radius: 16px;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.08), rgba(255, 255, 255, 0.02)),
    var(--panel);
}

.hud-label {
  display: block;
  font-size: 0.72rem;
  line-height: 1;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--muted);
}

.hud-value {
  display: block;
  margin-top: 4px;
  font-size: 1.24rem;
  font-weight: 800;
  line-height: 1;
}

.icon-button,
.play-again {
  border: 0;
  cursor: pointer;
}

.icon-button {
  width: 50px;
  height: 50px;
  border-radius: 16px;
  font-size: 1.45rem;
  color: var(--text);
  background:
    radial-gradient(circle at 28% 22%, rgba(255, 255, 255, 0.3), transparent 26%),
    linear-gradient(180deg, rgba(146, 244, 255, 0.34), rgba(92, 109, 255, 0.2)),
    rgba(11, 18, 36, 0.9);
}

.board-shell {
  min-height: 0;
  display: grid;
  place-items: center;
  position: relative;
  border-radius: 30px;
  padding: 10px;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.05), rgba(255, 255, 255, 0.01)),
    rgba(7, 12, 25, 0.74);
  border: 1px solid rgba(202, 237, 255, 0.1);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.04),
    0 20px 52px rgba(0, 0, 0, 0.38);
}

#gameCanvas {
  display: block;
  width: var(--canvas-w);
  height: var(--canvas-h);
  max-width: 100%;
  max-height: 100%;
  border-radius: 22px;
  background:
    linear-gradient(180deg, rgba(8, 16, 30, 0.92), rgba(36, 16, 40, 0.96)),
    linear-gradient(90deg, rgba(255, 255, 255, 0.02), transparent 32%, rgba(255, 255, 255, 0.02));
}

.board-overlay {
  position: absolute;
  inset: 10px;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  padding-top: 6px;
}

.status-pill {
  min-height: 30px;
  max-width: calc(100% - 24px);
  padding: 7px 12px;
  border-radius: 999px;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.11), rgba(255, 255, 255, 0.02)),
    rgba(10, 17, 33, 0.74);
  color: rgba(255, 248, 230, 0.96);
  font-size: 0.84rem;
  font-weight: 700;
  line-height: 1.1;
  letter-spacing: 0.02em;
  text-align: center;
}

.controls {
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  gap: 6px;
}

.control-button {
  --cooldown-progress: 0;
  --cooldown-fill: 1turn;
  --cooldown-angle: 1turn;
  --ready-breath-duration: 1.55s;
  --ready-breath-delay: 0s;
  position: relative;
  aspect-ratio: 1 / 1;
  border: 2px solid rgba(255, 255, 255, 0.18);
  border-radius: 999px;
  overflow: visible;
  cursor: pointer;
  background:
    radial-gradient(circle at 24% 18%, rgba(255, 255, 255, 0.62), transparent 16%),
    radial-gradient(circle at 50% 18%, rgba(255, 255, 255, 0.22), transparent 38%),
    linear-gradient(180deg, rgba(255, 255, 255, 0.3), rgba(255, 255, 255, 0.06) 40%, rgba(0, 0, 0, 0.12)),
    linear-gradient(180deg, rgba(18, 24, 52, 0.18), rgba(18, 24, 52, 0.42)),
    var(--button-color);
  box-shadow:
    inset 0 3px 6px rgba(255, 255, 255, 0.24),
    inset 0 -10px 14px rgba(12, 14, 30, 0.34),
    0 10px 24px rgba(0, 0, 0, 0.28);
  transition:
    filter 180ms ease,
    border-color 180ms ease,
    box-shadow 180ms ease,
    transform 180ms ease,
    opacity 180ms ease;
  transform: translateY(0) scale(1);
  transform-origin: center center;
}

.control-button::before,
.control-button::after {
  content: "";
  position: absolute;
  border-radius: 999px;
  pointer-events: none;
}

.control-button::before {
  inset: 8%;
  border: 1px solid rgba(255, 255, 255, 0.18);
}

.control-button::after {
  left: 18%;
  right: 18%;
  top: 8%;
  height: 24%;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.38), rgba(255, 255, 255, 0));
  filter: blur(2px);
}

.control-button.is-ready {
  border-color: rgba(255, 251, 224, 0.72);
  box-shadow:
    inset 0 3px 7px rgba(255, 255, 255, 0.34),
    inset 0 -10px 14px rgba(12, 14, 30, 0.26),
    0 0 0 1px rgba(255, 247, 206, 0.22),
    0 0 18px rgba(255, 226, 132, 0.34),
    0 12px 26px rgba(0, 0, 0, 0.28);
  animation:
    crystalReadyGlow 1.55s ease-in-out infinite,
    crystalBreathe var(--ready-breath-duration) ease-in-out infinite;
  animation-delay: 0s, var(--ready-breath-delay);
}

.control-button.is-cooling {
  border-color: rgba(214, 220, 244, 0.42);
  filter: saturate(0.9) brightness(0.94);
  box-shadow:
    inset 0 2px 5px rgba(255, 255, 255, 0.18),
    inset 0 -10px 18px rgba(6, 10, 24, 0.24),
    0 8px 18px rgba(0, 0, 0, 0.34);
}

.control-button.is-banked {
  box-shadow:
    inset 0 3px 6px rgba(255, 255, 255, 0.24),
    inset 0 -10px 14px rgba(12, 14, 30, 0.3),
    0 0 0 2px rgba(255, 245, 204, 0.18),
    0 12px 26px rgba(0, 0, 0, 0.3);
}

.cooldown-ring {
  position: absolute;
  inset: 0;
  z-index: 2;
  border-radius: 999px;
  pointer-events: none;
  opacity: 0;
  transition: opacity 160ms ease;
}

.control-button.is-cooling .cooldown-ring {
  opacity: 1;
}

.cooldown-ring::before,
.cooldown-ring::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: 999px;
}

.cooldown-ring::before {
  inset: 4%;
  background:
    radial-gradient(circle at 24% 18%, rgba(255, 255, 255, 0.24), transparent 18%),
    radial-gradient(circle at center, rgba(255, 246, 205, 0.12) 0 34%, rgba(255, 246, 205, 0.05) 46%, rgba(10, 14, 28, 0.08) 74%, rgba(10, 14, 28, 0.18) 100%),
    conic-gradient(
      from -90deg,
      rgba(255, 248, 214, 0.2) 0turn,
      rgba(255, 248, 214, 0.26) var(--cooldown-fill),
      rgba(7, 10, 22, 0.54) var(--cooldown-fill),
      rgba(7, 10, 22, 0.58) 1turn
    );
  box-shadow:
    inset 0 0 0 1px rgba(255, 249, 218, 0.26),
    inset 0 0 18px rgba(255, 245, 196, 0.12),
    inset 0 0 24px rgba(0, 0, 0, 0.14);
}

.cooldown-ring::after {
  inset: -8%;
  background: conic-gradient(
    from calc(var(--cooldown-angle) - 0.04turn),
    rgba(255, 252, 230, 0) 0turn,
    rgba(255, 252, 230, 0.96) 0.024turn,
    rgba(255, 220, 118, 0.74) 0.045turn,
    rgba(255, 220, 118, 0.2) 0.07turn,
    rgba(255, 252, 230, 0) 0.12turn,
    rgba(255, 252, 230, 0) 1turn
  );
  mask: radial-gradient(circle, transparent 59%, #000 64%, #000 72%, transparent 76%);
  -webkit-mask: radial-gradient(circle, transparent 59%, #000 64%, #000 72%, transparent 76%);
  filter:
    drop-shadow(0 0 6px rgba(255, 248, 198, 0.85))
    drop-shadow(0 0 12px rgba(255, 211, 101, 0.5));
}

.mini-shape {
  position: absolute;
  z-index: 1;
  left: 50%;
  top: 50%;
  width: 58%;
  height: 58%;
  transform: translate(-50%, -49%) rotate(-2deg);
  transition:
    opacity 160ms ease,
    filter 160ms ease,
    transform 160ms ease;
}

.control-button.is-cooling .mini-shape {
  opacity: 0.72;
  transform: translate(-50%, -49%) rotate(-2deg) scale(0.96);
  filter: brightness(0.96);
}

.control-button.is-ready .mini-shape {
  filter:
    drop-shadow(0 2px 0 rgba(28, 18, 12, 0.34))
    drop-shadow(0 0 4px rgba(255, 248, 206, 0.4))
    drop-shadow(0 0 9px rgba(255, 226, 130, 0.22));
}

.mini-cell {
  position: absolute;
  width: calc(25% - 2px);
  height: calc(25% - 2px);
  border-radius: 5px 4px 6px 3px;
  border: 2px solid rgba(35, 22, 14, 0.82);
  background:
    radial-gradient(circle at 30% 28%, rgba(255, 255, 255, 0.98), rgba(255, 255, 255, 0) 38%),
    linear-gradient(180deg, rgba(255, 255, 255, 0.98), rgba(248, 242, 232, 0.96) 58%, rgba(226, 216, 202, 0.94));
  box-shadow:
    0 1px 0 rgba(255, 255, 255, 0.3),
    0 2px 0 rgba(22, 14, 8, 0.18),
    inset 0 1px 0 rgba(255, 255, 255, 0.72),
    inset -1px -2px 0 rgba(70, 48, 26, 0.16);
}

.mini-cell:nth-child(odd) {
  transform: rotate(-2deg);
}

.mini-cell:nth-child(even) {
  transform: rotate(1.5deg);
}

.mini-cell:nth-child(3n) {
  border-radius: 4px 6px 4px 7px;
}

.mini-cell:nth-child(4n) {
  border-radius: 6px 3px 5px 5px;
}

.badge {
  position: absolute;
  z-index: 3;
  right: -7px;
  bottom: -6px;
  min-width: 22px;
  height: 22px;
  padding: 0 7px;
  display: grid;
  place-items: center;
  border-radius: 999px;
  color: #ffffff;
  font-size: 0.76rem;
  font-weight: 800;
  line-height: 1;
  background: linear-gradient(180deg, #ff6b6b, #d91f34);
  border: 2px solid #ffffff;
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.18),
    0 6px 14px rgba(0, 0, 0, 0.28);
}

.hidden {
  display: none !important;
}

.game-over {
  position: absolute;
  inset: 10px;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(4, 6, 12, 0.4);
}

.game-over-card {
  width: min(100%, 300px);
  padding: 22px 18px 18px;
  border-radius: 24px;
  text-align: center;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.08), rgba(255, 255, 255, 0.02)),
    rgba(8, 12, 26, 0.92);
  border: 1px solid rgba(255, 255, 255, 0.12);
  box-shadow: 0 18px 40px rgba(0, 0, 0, 0.38);
  pointer-events: auto;
}

.game-over-title {
  font-size: 1.54rem;
  font-weight: 800;
  letter-spacing: 0.04em;
}

.game-over-sub {
  margin-top: 8px;
  font-size: 0.96rem;
  color: var(--muted);
}

.play-again {
  margin-top: 16px;
  padding: 11px 18px;
  border-radius: 999px;
  font: inherit;
  font-weight: 800;
  color: #09121f;
  background: linear-gradient(180deg, #b7f6ff, #74a2ff);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.42),
    0 12px 24px rgba(0, 0, 0, 0.28);
}

@keyframes floatGlow {
  0%,
  100% {
    transform: translate3d(0, 0, 0) scale(1);
  }
  50% {
    transform: translate3d(1vw, -2vh, 0) scale(1.06);
  }
}

@keyframes pulseMist {
  0%,
  100% {
    opacity: 0.48;
    transform: translateY(0);
  }
  50% {
    opacity: 0.72;
    transform: translateY(-8px);
  }
}

@keyframes crystalBreathe {
  0%,
  100% {
    transform: translateY(0) scale(1);
  }
  50% {
    transform: translateY(-2px) scale(1.04);
  }
}

@keyframes crystalReadyGlow {
  0%,
  100% {
    filter: saturate(1.02) brightness(1);
  }
  50% {
    filter: saturate(1.18) brightness(1.12);
  }
}

@media (max-width: 430px) {
  body {
    padding-inline: 8px;
  }

  .app-shell {
    gap: 8px;
  }

  .hud {
    gap: 6px;
  }

  .hud-pill {
    min-height: 46px;
    padding-inline: 8px;
  }

  .hud-value {
    font-size: 1.12rem;
  }
}
