:root {
  --viewport-width: 100vw;
  --viewport-height: 100dvh;
  --shell-pad: 14px;
  --stage-width: min(620px, calc(var(--viewport-width) - (var(--shell-pad) * 2)));
  --board-size: min(320px, calc(var(--stage-width) - 24px));
  --panel-gap: clamp(8px, 1.5vmin, 16px);
  --panel-radius: clamp(18px, calc(var(--stage-width) * 0.05), 28px);
  --panel-border-size: clamp(2px, calc(var(--stage-width) * 0.0065), 3px);
  --bg-top: #84e4ff;
  --bg-bottom: #ffe07e;
  --panel: rgba(255, 252, 238, 0.84);
  --panel-border: rgba(47, 137, 147, 0.42);
  --ink: #18465a;
  --tile-a: #fffefb;
  --tile-b: #f8f8f2;
  --tile-edge: rgba(45, 86, 52, 0.28);
  --tile-shadow: rgba(41, 81, 49, 0.18);
  --tile-text: #223324;
  --dead-a: #d3d0cb;
  --dead-b: #b7b2ae;
  --dead-edge: rgba(102, 22, 35, 0.34);
  --dead-shadow: rgba(77, 20, 33, 0.2);
  --dead-text: #55202d;
  --tile-fill-size: 76%;
  --board-fill-size: 88%;
  --mini-fill-size: 72%;
  --dead-fill-size: 74%;
  --board-frame-size: 128%;
  --crayon-green: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'%3E%3Cpath d='M16 14C26 8 39 9 51 9h18c9 0 16 2 23 10 6 8 8 16 8 25v14c0 12-1 22-9 30-8 8-18 9-29 9H40c-13 0-22-1-30-10S2 69 2 57V41c0-11 2-20 10-27 2-2 3-2 4 0' fill='none' stroke='%23294f33' stroke-width='7.2' stroke-linecap='round' stroke-linejoin='round' opacity='.95'/%3E%3Cpath d='M12 18c8-10 22-10 35-10h18c11 0 22 3 28 14M8 33c-4 13-4 26-1 39m80-53c7 8 8 18 8 29v11c0 14-3 22-13 31M18 93c9 4 21 4 32 4h13c14 0 24-2 31-9' fill='none' stroke='%23376b44' stroke-width='3.8' stroke-linecap='round' stroke-linejoin='round' opacity='.72'/%3E%3Cpath d='M18 12c11-4 22-4 33-4m-42 19c2-5 6-9 9-11m66 6c5 7 7 15 7 24m-7 37c-6 8-13 11-22 12M15 87c7 6 15 8 25 9m-29-27c0 9 1 14 4 20' fill='none' stroke='%23224129' stroke-width='2.1' stroke-linecap='round' stroke-linejoin='round' opacity='.52'/%3E%3C/svg%3E");
  --crayon-red: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'%3E%3Cpath d='M16 14C26 8 39 9 51 9h18c9 0 16 2 23 10 6 8 8 16 8 25v14c0 12-1 22-9 30-8 8-18 9-29 9H40c-13 0-22-1-30-10S2 69 2 57V41c0-11 2-20 10-27 2-2 3-2 4 0' fill='none' stroke='%23641728' stroke-width='7.2' stroke-linecap='round' stroke-linejoin='round' opacity='.95'/%3E%3Cpath d='M12 18c8-10 22-10 35-10h18c11 0 22 3 28 14M8 33c-4 13-4 26-1 39m80-53c7 8 8 18 8 29v11c0 14-3 22-13 31M18 93c9 4 21 4 32 4h13c14 0 24-2 31-9' fill='none' stroke='%23893346' stroke-width='3.8' stroke-linecap='round' stroke-linejoin='round' opacity='.72'/%3E%3Cpath d='M18 12c11-4 22-4 33-4m-42 19c2-5 6-9 9-11m66 6c5 7 7 15 7 24m-7 37c-6 8-13 11-22 12M15 87c7 6 15 8 25 9m-29-27c0 9 1 14 4 20' fill='none' stroke='%234f1622' stroke-width='2.1' stroke-linecap='round' stroke-linejoin='round' opacity='.52'/%3E%3C/svg%3E");
  --soft: cubic-bezier(0.2, 0.8, 0.2, 1);
}

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

html,
body {
  margin: 0;
  width: 100%;
  height: 100%;
  max-width: 100%;
  max-height: 100%;
  overflow: hidden;
  overflow: clip;
  overscroll-behavior: none;
  background:
    radial-gradient(circle at 14% 18%, rgba(255, 255, 255, 0.9), transparent 10%),
    radial-gradient(circle at 78% 10%, rgba(255, 255, 255, 0.72), transparent 9%),
    radial-gradient(circle at 26% 78%, rgba(255, 143, 197, 0.26), transparent 20%),
    radial-gradient(circle at 82% 82%, rgba(129, 232, 188, 0.28), transparent 18%),
    linear-gradient(180deg, var(--bg-top), #baf8ff 34%, #fff1b5 76%, var(--bg-bottom));
  color: var(--ink);
  font-family:
    "Trebuchet MS",
    "Arial Rounded MT Bold",
    "Noto Sans TC",
    "PingFang TC",
    sans-serif;
  user-select: none;
  -webkit-user-select: none;
}

body {
  touch-action: none;
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  background:
    linear-gradient(90deg, rgba(255, 255, 255, 0.09) 1px, transparent 1px),
    linear-gradient(180deg, rgba(255, 255, 255, 0.08) 1px, transparent 1px);
  background-size: 18px 18px;
  opacity: 0.45;
}

button,
input,
textarea,
select {
  font: inherit;
  -webkit-user-select: none;
  user-select: none;
}

button {
  cursor: pointer;
  touch-action: none;
}

#app {
  position: relative;
  width: 100%;
  height: 100%;
  max-width: 100%;
  max-height: 100%;
  overflow: hidden;
  overflow: clip;
  touch-action: none;
  padding: 0;
}

#fx-layer {
  position: fixed;
  inset: 0;
  z-index: 30;
  pointer-events: none;
  overflow: hidden;
}

.game-shell {
  position: relative;
  width: 100%;
  height: 100vh;
  height: 100svh;
  max-width: 100%;
  max-height: 100vh;
  max-height: 100svh;
  display: flex;
  justify-content: center;
  align-items: stretch;
  padding:
    calc(clamp(6px, calc(var(--shell-pad) * 0.65), 12px) + env(safe-area-inset-top, 0px))
    calc(clamp(8px, calc(var(--shell-pad) * 0.9), 16px) + env(safe-area-inset-right, 0px))
    calc(clamp(2px, calc(var(--shell-pad) * 0.35), 6px) + env(safe-area-inset-bottom, 0px))
    calc(clamp(8px, calc(var(--shell-pad) * 0.9), 16px) + env(safe-area-inset-left, 0px));
  overflow: hidden;
  overflow: clip;
  touch-action: none;
}

.game-stage {
  position: relative;
  width: min(var(--stage-width), 100%);
  height: 100%;
  max-width: 100%;
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: var(--panel-gap);
  --stage-pad-top: clamp(6px, calc(var(--stage-width) * 0.018), 10px);
  --stage-pad-x: clamp(8px, calc(var(--stage-width) * 0.024), 14px);
  --stage-pad-bottom: 0px;
  padding:
    var(--stage-pad-top)
    var(--stage-pad-x)
    var(--stage-pad-bottom);
}

.top-bar {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(72px, 34%);
  gap: var(--panel-gap);
  align-items: stretch;
}

.hud-stack {
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: clamp(6px, calc(var(--stage-width) * 0.016), 12px);
}

.hud-row,
.restart-row {
  min-height: clamp(34px, calc(var(--stage-width) * 0.09), 46px);
  display: flex;
  align-items: center;
}

.hud-row {
  justify-content: space-between;
  gap: clamp(8px, calc(var(--stage-width) * 0.02), 14px);
}

.panel {
  min-width: 0;
  background: var(--panel);
  border: var(--panel-border-size) solid var(--panel-border);
  border-radius: var(--panel-radius);
  box-shadow:
    0 10px 0 rgba(34, 123, 130, 0.18),
    0 18px 34px rgba(35, 95, 124, 0.14),
    inset 0 1px 0 rgba(255, 255, 255, 0.78);
  backdrop-filter: blur(16px);
}

.panel-title {
  margin: 0 0 clamp(5px, calc(var(--stage-width) * 0.014), 10px);
  font-size: clamp(10px, calc(var(--stage-width) * 0.028), 12px);
  letter-spacing: 0.16em;
  text-transform: uppercase;
  opacity: 0.78;
  color: #2d8795;
  font-weight: 900;
}

.panel-title.is-nowrap {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.dead-panel {
  min-height: 0;
  height: 100%;
  padding: clamp(8px, calc(var(--stage-width) * 0.026), 16px);
  display: flex;
  flex-direction: column;
}

.level-badge {
  font-size: clamp(16px, calc(var(--stage-width) * 0.048), 26px);
  line-height: 1;
  font-weight: 900;
  letter-spacing: 0.08em;
  color: #1c6f7e;
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.7);
}

.dead-strip {
  position: relative;
  display: flex;
  align-items: center;
  gap: clamp(6px, calc(var(--stage-width) * 0.02), 12px);
  min-height: clamp(42px, calc(var(--stage-width) * 0.16), 70px);
  flex: 1;
  overflow-x: auto;
  overflow-y: hidden;
  padding: 4px 2px 2px;
  scrollbar-width: none;
  cursor: grab;
  touch-action: none;
}

.dead-strip::-webkit-scrollbar {
  display: none;
}

.dead-strip.is-dragging {
  cursor: grabbing;
}

.empty-dead {
  min-width: 100%;
  min-height: clamp(42px, calc(var(--stage-width) * 0.15), 64px);
}

.tile,
.mini-tile,
.fx-trail {
  --tile-frame: url("./assets/tile-frame-normal-1.png");
}

.dead-tile,
.tile.is-blocked {
  --dead-frame: url("./assets/tile-frame-dead-1.png");
}

.tile[data-frame-index="1"],
.mini-tile[data-frame-index="1"],
.fx-trail[data-frame-index="1"] {
  --tile-frame: url("./assets/tile-frame-normal-2.png");
}

.tile[data-frame-index="2"],
.mini-tile[data-frame-index="2"],
.fx-trail[data-frame-index="2"] {
  --tile-frame: url("./assets/tile-frame-normal-3.png");
}

.tile[data-frame-index="3"],
.mini-tile[data-frame-index="3"],
.fx-trail[data-frame-index="3"] {
  --tile-frame: url("./assets/tile-frame-normal-3.png");
}

.dead-tile[data-frame-index="1"],
.tile.is-blocked[data-frame-index="1"] {
  --dead-frame: url("./assets/tile-frame-dead-2.png");
}

.dead-tile[data-frame-index="2"],
.tile.is-blocked[data-frame-index="2"] {
  --dead-frame: url("./assets/tile-frame-dead-3.png");
}

.dead-tile[data-frame-index="3"],
.tile.is-blocked[data-frame-index="3"] {
  --dead-frame: url("./assets/tile-frame-dead-4.png");
}

.dead-tile {
  position: relative;
  flex: 0 0 auto;
  width: clamp(38px, calc(var(--stage-width) * 0.148), 62px);
  height: clamp(38px, calc(var(--stage-width) * 0.148), 62px);
  border-radius: clamp(14px, calc(var(--stage-width) * 0.045), 20px);
  display: grid;
  place-items: center;
  font-weight: 900;
  font-size: clamp(14px, calc(var(--stage-width) * 0.058), 24px);
  color: var(--dead-text);
  background: var(--dead-frame) center / 126% 126% no-repeat;
  border: none;
  box-shadow: none;
  animation: death-breath var(--shake-speed, 1.45s) steps(2, end) infinite;
}

.dead-tile > span {
  position: relative;
  z-index: 1;
  width: 82%;
  height: 82%;
  display: grid;
  place-items: center;
  border-radius: 24%;
  background: linear-gradient(180deg, var(--tile-a), var(--tile-b));
}

.dead-tile[data-digits="3"] {
  font-size: clamp(11px, calc(var(--stage-width) * 0.046), 19px);
}

.dead-tile[data-digits="4"] {
  font-size: clamp(9px, calc(var(--stage-width) * 0.038), 15px);
}

.dead-tile::before,
.dead-tile::after {
  content: "";
  position: absolute;
  top: -16%;
  width: clamp(8px, calc(var(--stage-width) * 0.028), 13px);
  height: clamp(12px, calc(var(--stage-width) * 0.04), 18px);
  background: linear-gradient(180deg, #9a3148, #5b1628);
  clip-path: polygon(50% 0, 100% 100%, 18% 82%);
  border-radius: 40% 40% 8% 8%;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.18);
  z-index: 3;
}

.dead-tile::before {
  left: clamp(7px, calc(var(--stage-width) * 0.022), 10px);
  transform: rotate(-18deg);
}

.dead-tile::after {
  right: clamp(7px, calc(var(--stage-width) * 0.022), 10px);
  transform: scaleX(-1) rotate(-18deg);
}

.lucky-side {
  min-width: 0;
  align-content: stretch;
}

.restart-row {
  justify-content: flex-end;
}

.icon-button {
  width: clamp(34px, calc(var(--stage-width) * 0.09), 46px);
  height: clamp(34px, calc(var(--stage-width) * 0.09), 46px);
  border: clamp(2px, calc(var(--stage-width) * 0.006), 3px) solid rgba(255, 195, 93, 0.46);
  border-radius: 999px;
  display: grid;
  place-items: center;
  padding: 0;
  background:
    linear-gradient(180deg, rgba(255, 252, 210, 0.96), rgba(255, 232, 159, 0.96));
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.92),
    0 4px 0 rgba(208, 165, 74, 0.2);
  color: #986612;
}

.icon-button svg {
  width: 62%;
  height: 62%;
  fill: currentColor;
  stroke: #7b5112;
  stroke-width: 1.25;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.icon-button:active {
  transform: scale(0.98);
}

.ghost-button {
  border: clamp(2px, calc(var(--stage-width) * 0.006), 3px) solid rgba(64, 145, 151, 0.34);
  border-radius: 999px;
  padding: clamp(6px, calc(var(--stage-width) * 0.02), 10px) clamp(10px, calc(var(--stage-width) * 0.032), 16px);
  color: #226271;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.92), rgba(219, 255, 251, 0.9));
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.85),
    0 5px 0 rgba(71, 154, 152, 0.16);
  font-size: clamp(11px, calc(var(--stage-width) * 0.03), 14px);
  font-weight: 900;
}

.ghost-button:active {
  transform: scale(0.98);
}

.lucky-panel {
  padding: clamp(8px, calc(var(--stage-width) * 0.026), 16px);
  min-height: 0;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.lucky-meta {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: clamp(6px, calc(var(--stage-width) * 0.02), 12px);
  margin-bottom: clamp(6px, calc(var(--stage-width) * 0.016), 10px);
}

.lucky-pair {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: clamp(6px, calc(var(--stage-width) * 0.02), 10px);
  align-items: center;
}

.mini-tile {
  height: clamp(42px, calc(var(--stage-width) * 0.16), 74px);
  border-radius: clamp(14px, calc(var(--stage-width) * 0.045), 20px);
  display: grid;
  place-items: center;
  padding: 0 clamp(4px, calc(var(--stage-width) * 0.014), 10px);
  background: var(--tile-frame) center / 100% 100% no-repeat;
  color: var(--tile-text);
  font-size: clamp(11px, calc(var(--stage-width) * 0.056), 22px);
  line-height: 1;
  font-weight: 900;
  font-variant-numeric: tabular-nums;
  border: none;
  box-shadow: none;
  overflow: hidden;
}

.mini-tile > span {
  position: relative;
  z-index: 1;
  width: var(--mini-fill-size);
  height: var(--mini-fill-size);
  display: grid;
  place-items: center;
  border-radius: 24%;
  background: linear-gradient(180deg, var(--tile-a), var(--tile-b));
}

.mini-tile::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background: var(--tile-frame) center / 100% 100% no-repeat;
  pointer-events: none;
  z-index: 2;
}

.mini-tile[data-digits="3"] {
  font-size: clamp(8px, calc(var(--stage-width) * 0.038), 14px);
}

.mini-tile[data-digits="4"] {
  font-size: clamp(7px, calc(var(--stage-width) * 0.032), 11px);
}

.mini-tile.left {
  animation: lucky-left 2s ease-in-out infinite;
}

.mini-tile.right {
  animation: lucky-right 2s ease-in-out infinite;
}

.board-panel {
  padding: clamp(8px, calc(var(--stage-width) * 0.026), 14px);
  display: flex;
  justify-content: center;
  align-items: flex-start;
  min-height: 0;
  overflow: hidden;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.18), rgba(238, 251, 243, 0.12));
  border: clamp(2px, calc(var(--stage-width) * 0.006), 3px) solid rgba(48, 110, 62, 0.18);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.4),
    0 10px 24px rgba(67, 110, 72, 0.08);
  backdrop-filter: blur(8px);
}

.board {
  position: relative;
  width: min(100%, var(--board-size));
  height: min(100%, var(--board-size));
  max-width: 100%;
  max-height: 100%;
  aspect-ratio: 1;
  display: grid;
  gap: clamp(5px, calc(var(--stage-width) * 0.018), 14px);
  grid-template-columns: repeat(var(--size), 1fr);
  padding: clamp(6px, calc(var(--stage-width) * 0.02), 12px);
  border-radius: clamp(22px, calc(var(--stage-width) * 0.076), 34px);
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.1), rgba(234, 245, 236, 0.08));
  box-shadow: inset 0 0 0 1px rgba(54, 102, 64, 0.12);
  touch-action: none;
  border: 0;
}

.cell {
  position: relative;
  border-radius: clamp(14px, calc(var(--stage-width) * 0.048), 24px);
  background: transparent;
  box-shadow: none;
}

.cell.is-active {
  box-shadow:
    inset 0 0 0 3px rgba(255, 243, 129, 0.98),
    0 0 0 2px rgba(255, 255, 255, 0.7),
    0 0 24px rgba(255, 211, 61, 0.34);
}

.cell.is-preview-target {
  box-shadow:
    inset 0 0 0 4px rgba(255, 255, 255, 0.94),
    0 0 0 2px rgba(44, 172, 156, 0.28);
}

.tile {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  border-radius: clamp(14px, calc(var(--stage-width) * 0.048), 24px);
  color: var(--tile-text);
  background: var(--tile-frame) center / var(--board-frame-size) var(--board-frame-size) no-repeat;
  box-shadow: none;
  border: none;
  font-size: clamp(16px, calc(var(--stage-width) * 0.092), 42px);
  font-weight: 900;
  font-variant-numeric: tabular-nums;
  transition:
    transform 180ms var(--soft),
    filter 180ms var(--soft),
    opacity 180ms var(--soft);
  will-change: transform;
}

.tile::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background: var(--tile-frame) center / var(--board-frame-size) var(--board-frame-size) no-repeat;
  pointer-events: none;
  z-index: 2;
}

.tile.is-pointer-dragging {
  transition: none;
  z-index: 8;
  filter: brightness(1.05) saturate(1.08);
  box-shadow: none;
}

.tile[data-digits="3"] {
  font-size: clamp(13px, calc(var(--stage-width) * 0.068), 30px);
}

.tile[data-digits="4"] {
  font-size: clamp(11px, calc(var(--stage-width) * 0.054), 24px);
}

.tile-inner {
  position: relative;
  z-index: 1;
  width: var(--board-fill-size);
  height: var(--board-fill-size);
  display: grid;
  place-items: center;
  border-radius: 24%;
  background: linear-gradient(180deg, var(--tile-a), var(--tile-b));
}

.tile.is-spawn {
  animation: tile-pop-jelly 520ms cubic-bezier(0.2, 0.9, 0.2, 1.15);
}

.tile.is-merge {
  animation: tile-merge 320ms var(--soft);
}

.tile.is-corrupting {
  animation: tile-corrupt 760ms steps(4, end);
}

.tile.is-blocked {
  color: var(--dead-text);
  background: var(--dead-frame) center / var(--board-frame-size) var(--board-frame-size) no-repeat;
  box-shadow: none;
  filter: none;
}

.tile.is-blocked .tile-inner {
  background: linear-gradient(180deg, var(--tile-a), var(--tile-b));
  overflow: visible;
}

.tile.is-blocked::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background: var(--dead-frame) center / var(--board-frame-size) var(--board-frame-size) no-repeat;
  pointer-events: none;
  z-index: 2;
}

.tile.is-blocked .tile-inner::before,
.tile.is-blocked .tile-inner::after {
  content: "";
  position: absolute;
  top: -24%;
  width: clamp(9px, calc(var(--stage-width) * 0.03), 15px);
  height: clamp(14px, calc(var(--stage-width) * 0.042), 20px);
  background: linear-gradient(180deg, #9a3148, #5b1628);
  clip-path: polygon(50% 0, 100% 100%, 18% 82%);
  border-radius: 40% 40% 8% 8%;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.18);
  z-index: 3;
}

.tile.is-blocked .tile-inner::before {
  left: 8%;
  transform: rotate(-18deg);
}

.tile.is-blocked .tile-inner::after {
  right: 8%;
  transform: scaleX(-1) rotate(-18deg);
}

.reserve-panel {
  padding: clamp(6px, calc(var(--stage-width) * 0.018), 10px);
  background-color: rgba(255, 252, 238, 0.18);
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.07), rgba(238, 251, 243, 0.03));
  border: clamp(2px, calc(var(--stage-width) * 0.006), 3px) solid rgba(48, 110, 62, 0.08);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.14),
    0 4px 10px rgba(67, 110, 72, 0.02);
  backdrop-filter: blur(3px);
  opacity: 0.66;
}

.reserve-row {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: clamp(5px, calc(var(--stage-width) * 0.018), 14px);
  padding-top: 0;
}

.reserve-slot {
  position: relative;
  aspect-ratio: 1;
  border-radius: clamp(14px, calc(var(--stage-width) * 0.044), 22px);
  background: transparent;
  border: none;
  box-shadow: none;
}

.reserve-slot .tile {
  font-size: clamp(13px, calc(var(--stage-width) * 0.06), 34px);
}

.reserve-slot.is-entry-slot .tile {
  animation: reserve-entry-bob 2s ease-in-out infinite;
  transform-origin: 50% 78%;
}

.menu-overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: calc(var(--shell-pad) + 8px);
  z-index: 20;
  touch-action: none;
}

.menu-title {
  margin: 0 0 clamp(14px, calc(var(--stage-width) * 0.042), 22px);
  font-size: clamp(22px, calc(var(--stage-width) * 0.08), 34px);
  font-weight: 900;
  color: #2f6d78;
  letter-spacing: 0.04em;
}

.menu-card {
  width: min(100%, max(180px, var(--stage-width)));
  padding: clamp(18px, calc(var(--stage-width) * 0.06), 34px);
  border-radius: clamp(24px, calc(var(--stage-width) * 0.08), 38px);
  background:
    radial-gradient(circle at top, rgba(255, 255, 255, 0.92), transparent 28%),
    radial-gradient(circle at bottom left, rgba(255, 142, 194, 0.2), transparent 32%),
    linear-gradient(180deg, rgba(255, 251, 229, 0.98), rgba(255, 232, 167, 0.95));
  border: clamp(2px, calc(var(--stage-width) * 0.008), 4px) solid rgba(255, 186, 93, 0.54);
  box-shadow:
    0 10px 0 rgba(240, 169, 74, 0.24),
    0 30px 54px rgba(91, 129, 98, 0.14);
  text-align: center;
}

.menu-blink {
  margin: 0 0 clamp(10px, calc(var(--stage-width) * 0.03), 16px);
  font-size: clamp(28px, calc(var(--stage-width) * 0.14), 64px);
  font-weight: 900;
  letter-spacing: 0.08em;
  color: #ff9e3d;
  text-shadow:
    3px 3px 0 rgba(255, 255, 255, 0.9),
    0 0 22px rgba(255, 205, 114, 0.28);
}

.menu-copy {
  margin: 0 0 clamp(14px, calc(var(--stage-width) * 0.04), 24px);
  opacity: 0.9;
  font-size: clamp(13px, calc(var(--stage-width) * 0.04), 15px);
  color: #2a6980;
  font-weight: 700;
}

.size-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: clamp(8px, calc(var(--stage-width) * 0.025), 14px);
}

.size-button {
  border: clamp(2px, calc(var(--stage-width) * 0.007), 3px) solid rgba(255, 216, 126, 0.82);
  border-radius: clamp(18px, calc(var(--stage-width) * 0.07), 28px);
  padding: clamp(16px, calc(var(--stage-width) * 0.06), 26px) 8px;
  color: #784114;
  background:
    radial-gradient(circle at top, rgba(255, 255, 255, 0.48), transparent 38%),
    linear-gradient(180deg, rgba(255, 241, 184, 0.98), rgba(255, 183, 93, 0.98));
  box-shadow:
    inset 0 2px 0 rgba(255, 255, 255, 0.78),
    0 6px 0 rgba(235, 151, 60, 0.24),
    0 18px 28px rgba(132, 104, 46, 0.12);
}

.size-button strong {
  display: block;
  font-size: clamp(22px, calc(var(--stage-width) * 0.09), 44px);
  font-weight: 900;
}

.tutorial-overlay {
  position: fixed;
  inset: 0;
  z-index: 35;
  display: grid;
  place-items: center;
  padding:
    calc(16px + env(safe-area-inset-top, 0px))
    calc(16px + env(safe-area-inset-right, 0px))
    calc(16px + env(safe-area-inset-bottom, 0px))
    calc(16px + env(safe-area-inset-left, 0px));
  background: rgba(18, 46, 56, 0.14);
  touch-action: auto;
}

.tutorial-card {
  position: relative;
  width: min(100%, 420px);
  display: grid;
  gap: 10px;
  padding: 12px;
  border-radius: 22px;
  background: rgba(255, 250, 244, 0.94);
  border: 2px solid rgba(206, 108, 96, 0.28);
  box-shadow:
    0 20px 42px rgba(16, 48, 58, 0.24),
    inset 0 1px 0 rgba(255, 255, 255, 0.9);
  touch-action: auto;
}

.tutorial-note {
  margin: 0;
  padding: 0 6px;
  color: #c8302f;
  text-align: center;
  font-size: clamp(15px, calc(var(--stage-width) * 0.04), 21px);
  line-height: 1.32;
  font-weight: 900;
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.82);
}

.tutorial-video {
  display: block;
  width: 100%;
  max-height: min(72vh, 560px);
  border-radius: 18px;
  background: #000;
  box-shadow: 0 18px 36px rgba(16, 48, 58, 0.18);
  touch-action: auto;
}

.level-banner {
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  z-index: 40;
  font-size: clamp(44px, 11vw, 110px);
  font-weight: 900;
  letter-spacing: 0.16em;
  color: #ffb53f;
  text-shadow:
    4px 4px 0 rgba(255, 255, 255, 0.9),
    0 0 22px rgba(255, 219, 129, 0.36);
  pointer-events: none;
  animation: banner-pop 1s ease forwards;
}

.fx-trail,
.fx-impact,
.fx-equation {
  position: fixed;
  pointer-events: none;
}

.fx-trail {
  display: grid;
  place-items: center;
  border-radius: 24px;
  color: var(--tile-text);
  background: var(--tile-frame) center / 100% 100% no-repeat;
  box-shadow: none;
  font-weight: 900;
}

.fx-impact {
  --impact-scale-x: 1;
  --impact-scale-y: 1;
  --impact-duration: 1500ms;
  width: var(--impact-size, 132px);
  height: var(--impact-size, 132px);
  margin-left: calc(var(--impact-size, 132px) / -2);
  margin-top: calc(var(--impact-size, 132px) / -2);
  transform-origin: center;
  animation: impact-life var(--impact-duration) cubic-bezier(0.2, 0.84, 0.18, 1) forwards;
}

.fx-impact.is-horizontal {
  --impact-scale-x: 1.12;
  --impact-scale-y: 0.9;
}

.fx-impact.is-vertical {
  --impact-scale-x: 0.9;
  --impact-scale-y: 1.12;
}

.fx-equation {
  left: 0;
  top: 0;
  z-index: 34;
  color: #f5bf3d;
  font-weight: 900;
  line-height: 1;
  letter-spacing: 0.02em;
  white-space: nowrap;
  text-shadow:
    0 3px 0 rgba(111, 67, 6, 0.76),
    0 10px 26px rgba(255, 245, 188, 0.72),
    0 0 2px rgba(255, 252, 240, 0.95);
}

.impact-layer,
.impact-core {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.impact-layer {
  width: 100%;
  height: 100%;
}

.impact-core {
  width: 34%;
  height: 34%;
  border-radius: 50%;
}

.impact-core-black {
  background: #121212;
  transform: translate(-50%, -50%) scale(1.18);
  filter: blur(0.5px);
}

.impact-core-white {
  background: #fff;
  box-shadow:
    0 0 22px rgba(255, 255, 255, 0.92),
    0 0 42px rgba(255, 255, 255, 0.34);
}

.impact-arc,
.impact-spike,
.impact-dot {
  position: absolute;
  left: 50%;
  top: 50%;
  display: block;
}

.impact-arc {
  width: 42%;
  height: 42%;
  margin-left: -21%;
  margin-top: -21%;
  border-radius: 50%;
  transform:
    rotate(var(--angle))
    translateY(calc(var(--radius) * -1))
    rotate(var(--rotation));
  animation: impact-arc 1000ms ease-out forwards;
}

.impact-arc::before,
.impact-arc::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: 50%;
  border: 8px solid #121212;
  border-left-color: transparent;
  border-bottom-color: transparent;
}

.impact-arc::after {
  inset: 4px;
  border-width: 6px;
  border-color: #fff;
  border-left-color: transparent;
  border-bottom-color: transparent;
}

.impact-spike {
  width: 26px;
  height: 58px;
  margin-left: -13px;
  margin-top: -29px;
  transform:
    rotate(var(--angle))
    translateY(calc(var(--distance) * -1))
    rotate(var(--rotation))
    scale(var(--spike-scale, 1));
  animation: impact-spike 1000ms ease-out forwards;
}

.impact-spike::before,
.impact-spike::after {
  content: "";
  position: absolute;
  inset: 0;
  clip-path: polygon(50% 0, 100% 100%, 0 100%);
}

.impact-spike::before {
  background: #121212;
  transform: scale(1.14);
}

.impact-spike::after {
  background: #fff;
}

.impact-dot {
  width: 16px;
  height: 16px;
  margin-left: -8px;
  margin-top: -8px;
  transform:
    rotate(var(--angle))
    translateY(calc(var(--distance) * -1))
    scale(var(--dot-scale, 1));
  animation: impact-dot 1000ms ease-out forwards;
}

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

.impact-dot::before {
  background: #121212;
  transform: scale(1.15);
}

.impact-dot::after {
  background: #fff;
}

.impact-dots {
  width: 100%;
  height: 100%;
}

@keyframes lucky-left {
  0%,
  35%,
  100% {
    transform: translateX(0) translateY(0) scale(1);
  }
  45% {
    transform: translateX(8px) translateY(-4px) scale(1.05);
  }
  55% {
    transform: translateX(14px) translateY(0) scale(1.02);
  }
}

@keyframes lucky-right {
  0%,
  35%,
  100% {
    transform: translateX(0) translateY(0) scale(1);
  }
  45% {
    transform: translateX(-8px) translateY(-4px) scale(1.05);
  }
  55% {
    transform: translateX(-14px) translateY(0) scale(1.02);
  }
}

@keyframes tile-pop-jelly {
  0% {
    opacity: 0;
    transform: scale(0.18);
  }
  18% {
    opacity: 1;
    transform: scale(1.24);
  }
  34% {
    transform: scale(0.9);
  }
  50% {
    transform: scale(1.08);
  }
  68% {
    transform: scale(0.96);
  }
  82% {
    transform: scale(1.03);
  }
  100% {
    opacity: 1;
    transform: scale(1);
  }
}

@keyframes tile-merge {
  0% {
    transform: scale(0.92);
    filter: brightness(1.1);
  }
  55% {
    transform: scale(1.12);
    filter: brightness(1.2);
  }
  100% {
    transform: scale(1);
  }
}

@keyframes tile-corrupt {
  0%,
  100% {
    transform: translateX(0);
  }
  25% {
    transform: translateX(-3px) translateY(-1px);
  }
  50% {
    transform: translateX(3px) translateY(1px);
  }
  75% {
    transform: translateX(-2px) translateY(1px);
  }
}

@keyframes death-breath {
  0%,
  100% {
    transform: translate(0, 0);
  }
  20% {
    transform: translate(-2px, 1px);
  }
  50% {
    transform: translate(2px, -1px);
  }
  75% {
    transform: translate(-1px, -1px);
  }
}

@keyframes burst-ray {
  0% {
    opacity: 1;
    transform: translateY(0) scale(0.28);
  }
  60% {
    opacity: 1;
  }
  100% {
    opacity: 0;
    transform: translateY(-48px) scale(1.18);
  }
}

@keyframes impact-life {
  0% {
    opacity: 0;
    transform: scale(calc(var(--impact-scale-x) * 0.35), calc(var(--impact-scale-y) * 0.35));
  }
  16% {
    opacity: 1;
    transform: scale(calc(var(--impact-scale-x) * 1.08), calc(var(--impact-scale-y) * 1.08));
  }
  42% {
    opacity: 1;
    transform: scale(var(--impact-scale-x), var(--impact-scale-y));
  }
  100% {
    opacity: 0;
    transform: scale(calc(var(--impact-scale-x) * 1.14), calc(var(--impact-scale-y) * 1.14));
  }
}

@keyframes impact-arc {
  0% {
    opacity: 0;
    transform:
      rotate(var(--angle))
      translateY(calc(var(--radius) * -0.2))
      rotate(var(--rotation))
      scale(0.2);
  }
  18% {
    opacity: 1;
  }
  100% {
    opacity: 0;
    transform:
      rotate(var(--angle))
      translateY(calc(var(--radius) * -1.18))
      rotate(var(--rotation))
      scale(1.08);
  }
}

@keyframes impact-spike {
  0% {
    opacity: 0;
    transform:
      rotate(var(--angle))
      translateY(calc(var(--distance) * -0.18))
      rotate(var(--rotation))
      scale(0.18);
  }
  14% {
    opacity: 1;
  }
  40% {
    opacity: 1;
  }
  100% {
    opacity: 0;
    transform:
      rotate(var(--angle))
      translateY(calc(var(--distance) * -1.08))
      rotate(var(--rotation))
      scale(calc(var(--spike-scale, 1) * 1.08));
  }
}

@keyframes impact-dot {
  0% {
    opacity: 0;
    transform:
      rotate(var(--angle))
      translateY(calc(var(--distance) * -0.2))
      scale(0.2);
  }
  20% {
    opacity: 1;
  }
  100% {
    opacity: 0;
    transform:
      rotate(calc(var(--angle) + 18deg))
      translateY(calc(var(--distance) * -1.28))
      scale(calc(var(--dot-scale, 1) * 0.88));
  }
}

@keyframes banner-pop {
  0% {
    opacity: 0;
    transform: translate(-50%, -50%) scale(0.66);
  }
  25% {
    opacity: 1;
    transform: translate(-50%, -50%) scale(1.06);
  }
  75% {
    opacity: 1;
    transform: translate(-50%, -50%) scale(1);
  }
  100% {
    opacity: 0;
    transform: translate(-50%, -58%) scale(1.04);
  }
}

@keyframes reserve-entry-bob {
  0%,
  100% {
    transform: translateY(0) scale(1);
    filter: brightness(1);
  }
  18% {
    transform: translateY(-3%) scale(1.03, 0.98);
    filter: brightness(1.08);
  }
  34% {
    transform: translateY(-11%) scale(1.06, 0.92);
    filter: brightness(1.12);
  }
  56% {
    transform: translateY(0) scale(0.96, 1.05);
    filter: brightness(0.98);
  }
  74% {
    transform: translateY(-4%) scale(1.02, 0.99);
    filter: brightness(1.04);
  }
}

@media (max-width: 360px), (max-height: 620px) {
  .game-shell {
    padding:
      calc(max(6px, calc(var(--shell-pad) - 4px)) + env(safe-area-inset-top, 0px))
      calc(max(8px, calc(var(--shell-pad) - 2px)) + env(safe-area-inset-right, 0px))
      calc(max(2px, calc(var(--shell-pad) * 0.3)) + env(safe-area-inset-bottom, 0px))
      calc(max(8px, calc(var(--shell-pad) - 2px)) + env(safe-area-inset-left, 0px));
  }

  .size-grid {
    gap: 6px;
  }

  .size-button {
    padding-top: 12px;
    padding-bottom: 12px;
  }
}

@media (max-width: 320px), (max-height: 480px) {
  .top-bar {
    grid-template-columns: minmax(0, 1.55fr) minmax(0, 0.95fr);
  }

  .panel {
    box-shadow:
      0 6px 0 rgba(34, 123, 130, 0.16),
      0 12px 22px rgba(35, 95, 124, 0.12),
      inset 0 1px 0 rgba(255, 255, 255, 0.78);
  }
}

html[data-layout="compact"] .game-stage,
html[data-layout="ultra"] .game-stage {
  padding:
    clamp(4px, calc(var(--stage-width) * 0.014), 8px)
    clamp(5px, calc(var(--stage-width) * 0.018), 10px)
    0;
}

html[data-layout="compact"] .top-bar,
html[data-layout="ultra"] .top-bar {
  grid-template-columns: minmax(0, 1.46fr) minmax(68px, 0.82fr);
  gap: clamp(5px, calc(var(--stage-width) * 0.016), 8px);
}

html[data-layout="compact"] .hud-stack,
html[data-layout="ultra"] .hud-stack {
  gap: 4px;
}

html[data-layout="compact"] .dead-panel,
html[data-layout="ultra"] .dead-panel,
html[data-layout="compact"] .lucky-panel,
html[data-layout="ultra"] .lucky-panel,
html[data-layout="compact"] .reserve-panel,
html[data-layout="ultra"] .reserve-panel {
  padding: clamp(6px, calc(var(--stage-width) * 0.022), 10px);
}

html[data-layout="compact"] .reserve-panel,
html[data-layout="ultra"] .reserve-panel {
  margin-bottom: 0;
}

html[data-layout="compact"] .panel-title,
html[data-layout="ultra"] .panel-title {
  letter-spacing: 0.1em;
  font-size: clamp(9px, calc(var(--stage-width) * 0.034), 11px);
}

html[data-layout="compact"] .hud-row,
html[data-layout="ultra"] .hud-row,
html[data-layout="compact"] .restart-row,
html[data-layout="ultra"] .restart-row {
  min-height: 26px;
}

html[data-layout="compact"] .level-badge,
html[data-layout="ultra"] .level-badge {
  font-size: clamp(11px, calc(var(--stage-width) * 0.046), 15px);
}

html[data-layout="compact"] .icon-button,
html[data-layout="ultra"] .icon-button {
  width: 26px;
  height: 26px;
}

html[data-layout="compact"] .dead-strip,
html[data-layout="ultra"] .dead-strip {
  min-height: clamp(40px, calc(var(--stage-width) * 0.18), 50px);
  gap: 4px;
  padding-top: 2px;
  padding-bottom: 2px;
}

html[data-layout="compact"] .dead-tile,
html[data-layout="ultra"] .dead-tile {
  width: clamp(28px, calc(var(--stage-width) * 0.13), 36px);
  height: clamp(28px, calc(var(--stage-width) * 0.13), 36px);
  border-radius: 10px;
  font-size: clamp(9px, calc(var(--stage-width) * 0.05), 14px);
}

html[data-layout="compact"] .dead-tile::before,
html[data-layout="compact"] .dead-tile::after,
html[data-layout="ultra"] .dead-tile::before,
html[data-layout="ultra"] .dead-tile::after {
  top: -3px;
  width: 6px;
  height: 9px;
}

html[data-layout="compact"] .lucky-side,
html[data-layout="ultra"] .lucky-side {
  gap: 4px;
}

html[data-layout="compact"] .ghost-button,
html[data-layout="ultra"] .ghost-button {
  padding: 4px 6px;
  font-size: 10px;
  line-height: 1.1;
  border-width: 2px;
}

html[data-layout="compact"] .lucky-meta,
html[data-layout="ultra"] .lucky-meta {
  margin-bottom: 4px;
}

html[data-layout="compact"] .lucky-pair,
html[data-layout="ultra"] .lucky-pair {
  gap: 4px;
}

html[data-layout="compact"] .mini-tile,
html[data-layout="ultra"] .mini-tile {
  height: clamp(30px, calc(var(--stage-width) * 0.14), 40px);
  border-radius: 10px;
  font-size: clamp(8px, calc(var(--stage-width) * 0.046), 12px);
}

html[data-layout="compact"] .board-panel,
html[data-layout="ultra"] .board-panel {
  padding: 5px;
}

html[data-layout="compact"] .board,
html[data-layout="ultra"] .board {
  gap: clamp(3px, calc(var(--stage-width) * 0.014), 5px);
  padding: clamp(6px, calc(var(--stage-width) * 0.024), 8px);
  border-radius: clamp(18px, calc(var(--stage-width) * 0.07), 24px);
}

html[data-layout="compact"] .cell,
html[data-layout="compact"] .tile,
html[data-layout="ultra"] .cell,
html[data-layout="ultra"] .tile {
  border-radius: 10px;
}

html[data-layout="compact"] .tile,
html[data-layout="ultra"] .tile {
  font-size: clamp(11px, calc(var(--stage-width) * 0.072), 18px);
  border-width: 2px;
  box-shadow: none;
}

html[data-layout="compact"],
html[data-layout="ultra"] {
  --board-frame-size: 134%;
}

html[data-layout="compact"] .tile[data-digits="3"],
html[data-layout="ultra"] .tile[data-digits="3"] {
  font-size: clamp(10px, calc(var(--stage-width) * 0.058), 15px);
}

html[data-layout="compact"] .tile[data-digits="4"],
html[data-layout="ultra"] .tile[data-digits="4"] {
  font-size: clamp(8px, calc(var(--stage-width) * 0.048), 13px);
}

html[data-layout="compact"] .reserve-row,
html[data-layout="ultra"] .reserve-row {
  gap: 4px;
}

html[data-layout="compact"] .reserve-slot,
html[data-layout="ultra"] .reserve-slot {
  border-radius: 10px;
}

html[data-layout="compact"] .reserve-slot .tile,
html[data-layout="ultra"] .reserve-slot .tile {
  font-size: clamp(10px, calc(var(--stage-width) * 0.054), 16px);
}

html[data-layout="compact"] .menu-overlay,
html[data-layout="ultra"] .menu-overlay {
  padding: calc(var(--shell-pad) + 4px);
}

html[data-layout="compact"] .menu-card,
html[data-layout="ultra"] .menu-card {
  width: min(100%, max(180px, var(--stage-width)));
  padding: 14px 10px;
}

html[data-layout="compact"] .menu-copy,
html[data-layout="ultra"] .menu-copy {
  font-size: 12px;
  margin-bottom: 10px;
}

html[data-layout="compact"] .menu-title,
html[data-layout="ultra"] .menu-title {
  font-size: clamp(16px, calc(var(--stage-width) * 0.076), 22px);
  margin-bottom: 10px;
}

html[data-layout="compact"] .size-grid,
html[data-layout="ultra"] .size-grid {
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 6px;
}

html[data-layout="compact"] .size-button,
html[data-layout="ultra"] .size-button {
  padding: 10px 6px;
}

html[data-layout="compact"] .size-button strong,
html[data-layout="ultra"] .size-button strong {
  font-size: clamp(18px, calc(var(--stage-width) * 0.09), 24px);
}
