:root {
  color-scheme: light;
  --ink: #382921;
  --muted: #795f4e;
  --paper: #fff8eb;
  --line: #382921;
  --yellow: #ffd66e;
  --ceiling: #f4c777;
  --hud-bg: rgba(255, 250, 236, 0.78);
  --hud-shadow: 0 3px 12px rgba(82, 56, 34, 0.14);
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  min-height: 100%;
  overflow: hidden;
}

body {
  margin: 0;
  background: #ffe1a3;
  color: var(--ink);
  font-family:
    -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB",
    "Microsoft YaHei", sans-serif;
}

button {
  color: inherit;
  font: inherit;
  cursor: pointer;
  touch-action: manipulation;
}

.game-shell {
  position: fixed;
  inset: 0;
  overflow: hidden;
  background: #f7d794;
}

.game-shell::before {
  content: "";
  position: absolute;
  inset: -28px;
  background:
    linear-gradient(rgba(255, 236, 188, 0.24), rgba(255, 232, 174, 0.24)),
    url("assets/background.png") center / cover no-repeat;
  filter: blur(16px) saturate(0.92);
  transform: scale(1.06);
}

.game-shell::after {
  content: "";
  position: absolute;
  inset: 0;
  background: rgba(255, 226, 164, 0.34);
}

.stage-wrap {
  position: relative;
  z-index: 1;
  width: min(100vw, calc(100vh * 430 / 760));
  height: 100%;
  margin: 0 auto;
}

.stage {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background-color: var(--ceiling);
  background-image: url("assets/background.png");
  background-position: center;
  background-size: cover;
  background-repeat: no-repeat;
  user-select: none;
  touch-action: manipulation;
  --scene-shift: 0px;
}

.stage.on-home .game-hud,
.stage.on-home .message,
.stage.on-home .dropper,
.stage.on-home .mibao-watch,
.stage.on-home .mibao-success,
.stage.on-home .mibao-jump,
.stage.on-home #gameCanvas {
  visibility: hidden;
}

.stage.level-mosquito .dropper,
.stage.level-mosquito .mibao-success,
.stage.level-mosquito .mibao-jump {
  visibility: hidden;
}

.stage.level-mosquito {
  background-image: url("assets/kitchen-background-green-v1.png");
}

.stage.level-text .dropper,
.stage.level-text .mibao-success,
.stage.level-text .mibao-jump,
.stage.level-text #gameCanvas {
  visibility: hidden;
}

.stage.level-text {
  background-color: #fff4df;
  background-image: none;
  background-position: center;
}

.stage.level-mahjong .dropper,
.stage.level-mahjong .mibao-watch,
.stage.level-mahjong .mibao-success,
.stage.level-mahjong .mibao-jump,
.stage.level-mahjong #gameCanvas {
  visibility: hidden;
}

.stage.level-mahjong {
  background-image: url("assets/living-room-purple-v1.png");
  background-position: center;
  background-size: cover;
}

.start-screen {
  position: absolute;
  inset: 0;
  z-index: 9;
  display: block;
  width: 100%;
  height: 100%;
  padding: 0;
  border: 0;
  background: transparent;
}

.start-screen.hidden {
  display: none;
}

.start-screen img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.game-hud {
  position: absolute;
  z-index: 7;
  left: max(16px, env(safe-area-inset-left));
  right: max(16px, env(safe-area-inset-right));
  top: max(14px, env(safe-area-inset-top));
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto auto auto;
  gap: 8px;
  align-items: center;
  pointer-events: none;
}

h1 {
  margin: 0;
  min-width: 0;
  font-size: clamp(26px, 6.2vw, 40px);
  line-height: 1;
  letter-spacing: 0;
  text-shadow:
    0 2px 0 rgba(255, 250, 236, 0.92),
    0 4px 10px rgba(82, 56, 34, 0.16);
}

.progress {
  min-width: 70px;
  padding: 8px 10px;
  border-radius: 999px;
  background: var(--hud-bg);
  box-shadow: var(--hud-shadow);
  font-size: clamp(15px, 3.7vw, 22px);
  font-weight: 950;
  line-height: 1;
  text-align: center;
}

.game-hud button {
  min-width: 56px;
  padding: 9px 12px;
  border: 0;
  border-radius: 999px;
  background: rgba(255, 214, 110, 0.9);
  box-shadow: var(--hud-shadow);
  font-size: clamp(14px, 3.5vw, 20px);
  font-weight: 950;
  line-height: 1;
  pointer-events: auto;
}

.game-hud button[aria-pressed="false"] {
  background: rgba(255, 250, 236, 0.72);
  color: var(--muted);
}

.message {
  position: absolute;
  z-index: 6;
  left: 50%;
  top: calc(max(14px, env(safe-area-inset-top)) + 54px);
  width: min(72vw, 340px);
  min-height: 32px;
  display: grid;
  place-items: center;
  translate: -50% 0;
  padding: 6px 12px;
  border-radius: 999px;
  background: rgba(255, 250, 236, 0.62);
  box-shadow: var(--hud-shadow);
  font-size: clamp(13px, 3.2vw, 18px);
  font-weight: 850;
  line-height: 1.2;
  text-align: center;
}

.current-item {
  display: none;
}

#gameCanvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

.text-game {
  position: absolute;
  z-index: 4;
  inset: 94px 8px max(10px, env(safe-area-inset-bottom));
  display: block;
  pointer-events: auto;
}

.text-game.hidden {
  display: none;
}

.spot-board {
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-rows: minmax(0, 1fr) minmax(0, 1fr);
  gap: 7px;
}

.spot-panel {
  position: relative;
  min-height: 0;
  overflow: hidden;
  border: 3px solid rgba(56, 41, 33, 0.72);
  border-radius: 8px;
  background: #fff4df;
  box-shadow: var(--hud-shadow);
}

.spot-panel img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.spot-hotspot {
  position: absolute;
  border: 0;
  border-radius: 999px;
  background: transparent;
  translate: -50% -50%;
  touch-action: manipulation;
}

.spot-hotspot::after {
  content: "";
  position: absolute;
  inset: 8%;
  border: 4px solid rgba(255, 214, 110, 0);
  border-radius: inherit;
  box-shadow: 0 0 0 0 rgba(56, 41, 33, 0);
  transition:
    border-color 160ms ease,
    box-shadow 160ms ease,
    background 160ms ease;
}

.spot-hotspot.found::after {
  background: rgba(255, 214, 110, 0.18);
  border-color: #ffd66e;
  box-shadow:
    0 0 0 3px rgba(56, 41, 33, 0.86),
    0 4px 14px rgba(56, 41, 33, 0.18);
}

.spot-board.wrong {
  animation: wrong-tile 220ms ease-in-out;
}

.mahjong-game {
  position: absolute;
  z-index: 4;
  inset: 104px 4px max(10px, env(safe-area-inset-bottom));
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
  gap: 8px;
  place-items: center;
  pointer-events: auto;
}

.mahjong-game.hidden {
  display: none;
}

.mahjong-board {
  position: relative;
  width: min(100%, 350px);
  height: min(100%, 640px);
  border-radius: 8px;
  overflow: visible;
}

.mahjong-tile {
  position: absolute;
  width: 62px;
  height: 72px;
  border: 3px solid var(--line);
  border-radius: 8px;
  background:
    linear-gradient(145deg, rgba(255, 255, 255, 0.96), rgba(255, 239, 198, 0.96));
  box-shadow:
    4px 5px 0 rgba(56, 41, 33, 0.32),
    inset -4px -5px 0 rgba(235, 196, 126, 0.42);
  color: var(--ink);
  font-size: 34px;
  font-weight: 950;
  line-height: 1;
  touch-action: manipulation;
  transform: translate(-50%, -50%);
  transition:
    opacity 160ms ease,
    scale 140ms ease,
    filter 160ms ease,
    box-shadow 160ms ease;
}

.mahjong-tile span {
  display: grid;
  place-items: center;
  width: 100%;
  height: 100%;
  translate: 0 -1px;
}

.mahjong-tile.locked {
  opacity: 0.62;
  filter: saturate(0.72);
}

.mahjong-tile.available {
  opacity: 1;
}

.mahjong-tile.selected {
  scale: 1.08;
  background:
    linear-gradient(145deg, #fff9e8, #ffd66e);
  box-shadow:
    0 0 0 4px rgba(56, 41, 33, 0.85),
    5px 7px 0 rgba(56, 41, 33, 0.28),
    inset -4px -5px 0 rgba(235, 196, 126, 0.4);
}

.mahjong-tile.matched {
  opacity: 0;
  scale: 0.52;
  pointer-events: none;
}

.mahjong-tile.wrong {
  animation: wrong-tile 220ms ease-in-out;
}

.mahjong-shuffle {
  min-width: 104px;
  padding: 10px 18px;
  border: 0;
  border-radius: 999px;
  background: rgba(255, 214, 110, 0.94);
  box-shadow: var(--hud-shadow);
  color: var(--ink);
  font-size: clamp(15px, 3.7vw, 20px);
  font-weight: 950;
}

.mahjong-shuffle:disabled {
  opacity: 0.54;
  filter: saturate(0.75);
}

.mahjong-link {
  position: absolute;
  inset: 0;
  z-index: 2000;
  width: 100%;
  height: 100%;
  overflow: visible;
  pointer-events: none;
}

.mahjong-link polyline {
  fill: none;
  stroke: #ffd66e;
  stroke-width: 9;
  stroke-linecap: round;
  stroke-linejoin: round;
  filter: drop-shadow(0 2px 0 rgba(56, 41, 33, 0.86));
}

.dropper {
  position: absolute;
  left: 50%;
  top: 58px;
  z-index: 4;
  width: 46px;
  height: 42px;
  translate: -50% 0;
  pointer-events: none;
}

.dropper::before {
  content: "";
  position: absolute;
  left: 50%;
  top: -52px;
  width: 4px;
  height: 58px;
  translate: -50% 0;
  border-radius: 99px;
  background: var(--line);
}

.dropper span {
  position: absolute;
  inset: 0;
  border: 3px solid var(--line);
  border-radius: 50% 50% 45% 45%;
  background: var(--yellow);
  box-shadow: 0 5px 0 rgba(56, 41, 33, 0.18);
}

.dropper span::after {
  content: "";
  position: absolute;
  left: 50%;
  bottom: -15px;
  width: 24px;
  height: 20px;
  translate: -50% 0;
  border: 4px solid var(--line);
  border-top: 0;
  border-radius: 0 0 18px 18px;
}

.mibao-watch,
.mibao-success,
.mibao-jump {
  position: absolute;
  z-index: 3;
  pointer-events: none;
  filter: drop-shadow(0 7px 0 rgba(56, 41, 33, 0.16));
}

.mibao-watch {
  left: clamp(4px, 1.8vw, 12px);
  bottom: max(10px, env(safe-area-inset-bottom));
  width: clamp(108px, 29%, 178px);
  transform: translateY(var(--scene-shift));
  transform-origin: 22% 92%;
  will-change: transform;
}

.mibao-watch.tail-wiggle {
  animation: tail-wiggle 820ms ease-in-out;
}

.mibao-success {
  left: 50%;
  top: 50%;
  width: clamp(250px, 58vw, 430px);
  translate: -50% -50%;
  opacity: 0;
  scale: 0.7;
  transition:
    opacity 240ms ease,
    scale 720ms cubic-bezier(0.2, 1.4, 0.38, 1),
    top 980ms cubic-bezier(0.2, 1.4, 0.38, 1),
    left 980ms cubic-bezier(0.2, 1.4, 0.38, 1);
}

.mibao-jump {
  left: 72%;
  top: 48%;
  width: clamp(176px, 42vw, 320px);
  translate: -50% -50%;
  opacity: 0;
  scale: 0.72;
  transition:
    opacity 160ms ease,
    scale 360ms cubic-bezier(0.2, 1.45, 0.38, 1);
}

.stage.won .mibao-watch {
  opacity: 0;
}

.stage.success-jump .mibao-jump {
  opacity: 1;
  scale: 1;
}

.stage.success-wash .mibao-jump {
  opacity: 0;
  scale: 0.86;
}

.stage.success-wash .mibao-success {
  opacity: 1;
  scale: 1;
}

@keyframes tail-wiggle {
  0%,
  100% {
    transform: translateY(var(--scene-shift)) rotate(0deg);
  }

  22% {
    transform: translateY(var(--scene-shift)) rotate(-2.4deg);
  }

  48% {
    transform: translateY(var(--scene-shift)) rotate(2deg);
  }

  72% {
    transform: translateY(var(--scene-shift)) rotate(-1.2deg);
  }
}

@keyframes wrong-tile {
  0%,
  100% {
    translate: 0 0;
  }

  35% {
    translate: -6px 0;
  }

  70% {
    translate: 6px 0;
  }
}

.overlay {
  position: absolute;
  inset: 0;
  z-index: 8;
  display: grid;
  place-items: center;
  padding: 22px;
  background: rgba(255, 248, 235, 0.58);
  backdrop-filter: blur(2px);
}

.overlay.hidden {
  display: none;
}

.panel {
  position: relative;
  width: min(78vw, 340px);
  padding: 18px;
  border-radius: 22px;
  background: rgba(255, 250, 236, 0.82);
  box-shadow: 0 10px 34px rgba(82, 56, 34, 0.18);
  text-align: center;
}

.panel-close {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: rgba(255, 250, 236, 0.9);
  box-shadow: var(--hud-shadow);
  color: var(--ink);
  font-size: 24px;
  font-weight: 950;
  line-height: 1;
}

.panel-close[hidden] {
  display: none;
}

.panel p {
  margin: 0 0 6px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 900;
}

.panel .panel-body {
  margin: 10px auto 0;
  max-width: 15em;
  color: var(--ink);
  font-size: clamp(15px, 3.7vw, 18px);
  font-weight: 850;
  line-height: 1.45;
}

.panel h2 {
  margin: 0;
  font-size: clamp(24px, 6.2vw, 34px);
  line-height: 1.12;
  letter-spacing: 0;
}

.overlay.result .panel p:first-child {
  margin: 0;
  color: var(--ink);
  font-size: clamp(24px, 6.2vw, 34px);
  line-height: 1.12;
}

.overlay.result .panel h2 {
  margin-top: 8px;
  color: var(--muted);
  font-size: clamp(15px, 3.7vw, 18px);
  font-weight: 900;
  line-height: 1.35;
}

.panel-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px;
  margin-top: 16px;
}

.panel button {
  margin-top: 16px;
  padding: 11px 18px;
  border: 0;
  border-radius: 999px;
  background: var(--yellow);
  box-shadow: 0 5px 15px rgba(82, 56, 34, 0.16);
  font-weight: 950;
}

.panel-actions button {
  margin-top: 0;
}

.panel button.secondary {
  background: rgba(255, 250, 236, 0.92);
}

@media (min-aspect-ratio: 3 / 4) {
  .stage {
    background-size: auto 100%;
  }
}
