:root {
  color-scheme: dark;
  --tile: 64px;
  --board-pad: var(--tile);
  --rows: 7;
  --cols: 10;
}

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

html,
body {
  height: 100%;
  width: 100%;
}

body {
  margin: 0;
  overflow: hidden;
  background: #000;
  color: #fff;
  font-family: "Microsoft YaHei", Tahoma, Arial, sans-serif;
  touch-action: manipulation;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  -webkit-user-select: none;
  user-select: none;
}

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

img {
  -webkit-user-drag: none;
  user-select: none;
}

.app-shell {
  width: 100%;
  height: 100dvh;
  display: grid;
  place-items: center;
  padding: 10px;
}

.game {
  width: min(1320px, 100%);
  height: 100%;
  min-height: 0;
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr);
  overflow: hidden;
  border: 0;
  border-radius: 8px;
  background: #000;
  box-shadow: none;
}

.top-panel {
  position: relative;
  z-index: 10;
  display: grid;
  grid-template-columns: minmax(190px, auto) 1fr auto;
  align-items: center;
  gap: 14px;
  padding: clamp(5px, 1vh, 8px) 12px;
  background: linear-gradient(180deg, rgba(50, 185, 255, 0.26), rgba(236, 226, 190, 0.16));
  border-bottom: 1px solid rgba(255, 238, 150, 0.28);
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.brand img {
  width: clamp(26px, 3.8vh, 34px);
  height: clamp(26px, 3.8vh, 34px);
  image-rendering: pixelated;
}

.brand p,
.brand h1 {
  margin: 0;
}

.brand p {
  color: #ffe65a;
  font-size: clamp(0.62rem, 1.4vh, 0.78rem);
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

.brand h1 {
  font-size: clamp(1rem, 3vh, 1.75rem);
  line-height: 1.05;
}

.menu-bar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.menu {
  position: relative;
  min-width: 0;
}

.menu-toggle {
  min-width: clamp(56px, 8vw, 74px);
  min-height: clamp(28px, 4.5vh, 34px);
  display: grid;
  place-items: center;
  border: 1px solid rgba(255, 230, 90, 0.4);
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.08);
  color: #fff;
  cursor: pointer;
  font-weight: 800;
  white-space: nowrap;
}

.top-actions {
  display: flex;
  align-items: center;
  justify-content: end;
  gap: 10px;
  min-width: 0;
}

.language-toggle {
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  border: 1px solid rgba(255, 230, 90, 0.42);
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.08);
  color: #fff;
  cursor: pointer;
  padding: 0;
}

.language-toggle svg {
  width: 18px;
  height: 18px;
  display: block;
  fill: none;
  stroke: #7ad7ff;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.language-toggle.is-open,
.language-toggle:hover {
  background: rgba(255, 230, 90, 0.18);
}

.language-popover {
  left: auto;
  right: 0;
  min-width: 128px;
  transform: none;
}

.menu-toggle.is-open,
.menu-toggle:hover {
  background: rgba(255, 230, 90, 0.18);
}

.menu-popover {
  position: absolute;
  top: calc(100% + 6px);
  left: 50%;
  min-width: 150px;
  display: grid;
  gap: 4px;
  padding: 7px;
  border: 1px solid rgba(255, 230, 90, 0.45);
  border-radius: 6px;
  background: rgba(18, 18, 18, 0.96);
  box-shadow: 0 16px 36px rgba(0, 0, 0, 0.45);
  transform: translateX(-50%);
}

.menu-popover[hidden] {
  display: none;
}

.menu-popover button {
  min-height: 34px;
  border: 1px solid rgba(255, 255, 255, 0.16);
  border-radius: 5px;
  background: rgba(255, 255, 255, 0.08);
  color: #fff;
  cursor: pointer;
  font-weight: 800;
  text-align: left;
  padding: 6px 10px;
}

.menu-popover button:hover,
.menu-popover button.is-active {
  border-color: rgba(255, 230, 90, 0.72);
  background: rgba(255, 230, 90, 0.18);
}

.menu-popover button:disabled {
  cursor: not-allowed;
  opacity: 0.45;
}

.score-box {
  min-width: 92px;
  text-align: right;
  font-family: Georgia, "Times New Roman", serif;
  color: #7ad7ff;
}

.score-box strong {
  display: block;
  font-size: clamp(1.55rem, 5vh, 3.2rem);
  line-height: 0.9;
}

.score-box span {
  color: #ffe65a;
  font-size: 0.78rem;
}

.hud {
  display: grid;
  grid-template-columns: minmax(120px, auto) repeat(3, minmax(72px, auto)) minmax(420px, 1fr);
  align-items: center;
  gap: 10px;
  padding: clamp(5px, 1vh, 8px) 12px;
  background: #000;
}

.class-card {
  display: grid;
  gap: 0;
  min-width: 96px;
  font-family: Georgia, "Times New Roman", serif;
  line-height: 0.94;
}

.class-card span {
  font-size: clamp(1.25rem, 2.15vw, 1.8rem);
}

.class-card strong {
  color: #7ad7ff;
  font-size: clamp(1.75rem, 3.35vw, 2.75rem);
  font-style: italic;
}

.mini-stat {
  min-width: 66px;
  display: grid;
  justify-items: center;
  font-family: Georgia, "Times New Roman", serif;
  line-height: 0.96;
}

.mini-stat.mobile-score {
  display: none;
}

.mini-stat span {
  color: #fff22f;
  font-size: clamp(0.92rem, 1.35vw, 1.12rem);
  font-weight: 900;
}

.mini-stat strong {
  color: #7ad7ff;
  font-size: clamp(1.35rem, 2.45vw, 2.1rem);
}

.time-area {
  display: grid;
  grid-column: 5;
  grid-template-columns: 1fr auto;
  grid-template-areas:
    "label seconds"
    "bar seconds";
  align-items: center;
  gap: 7px 14px;
  width: 100%;
  max-width: 760px;
  justify-self: stretch;
  min-width: 0;
}

.time-area > strong {
  grid-area: label;
  justify-self: center;
  color: #17ff21;
  font-size: clamp(1.35rem, 2.35vw, 2.25rem);
  line-height: 1;
  font-family: "Microsoft YaHei", Tahoma, sans-serif;
}

.time-area > span {
  grid-area: seconds;
  color: #7ad7ff;
  font-family: Georgia, "Times New Roman", serif;
  font-size: clamp(1.45rem, 2.65vw, 2.45rem);
  font-weight: 900;
}

.time-shell {
  grid-area: bar;
  position: relative;
  width: 100%;
  max-width: 100%;
  min-width: 0;
  overflow: hidden;
  height: clamp(16px, 2.8vh, 22px);
  padding: 3px;
  border: 3px solid #f2d324;
  background: #101010;
}

.time-bar {
  position: absolute;
  inset: 3px;
  width: auto;
  height: auto;
  max-width: calc(100% - 6px);
  background: linear-gradient(90deg, #ff2117 0%, #ffff20 28%, #30ff17 62%, #00d40a 100%);
  transform-origin: left center;
}

.board-panel {
  position: relative;
  min-height: 0;
  display: grid;
  place-items: start center;
  overflow: hidden;
  padding: 0 8px 6px;
  background:
    radial-gradient(circle at center 12%, rgba(29, 121, 81, 0.16), transparent 28%),
    #000;
}

.board-wrap {
  position: relative;
  width: fit-content;
  height: fit-content;
  max-width: 100%;
  align-self: start;
  filter: drop-shadow(6px 0 0 #ff6b2a);
  opacity: 0;
  transform: translateY(8px) scale(0.985);
  transition:
    opacity 180ms ease,
    transform 180ms ease;
}

.board-wrap.is-layout-ready {
  opacity: 1;
  transform: translateY(0) scale(1);
}

.board {
  position: relative;
  z-index: 2;
  display: grid;
  grid-template-columns: repeat(var(--cols), var(--tile));
  grid-template-rows: repeat(var(--rows), var(--tile));
  gap: 0;
  padding: var(--board-pad);
}

#link-layer {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 3;
  pointer-events: none;
}

.fx-layer {
  position: absolute;
  inset: 0;
  z-index: 4;
  overflow: visible;
  pointer-events: none;
}

.tile {
  width: var(--tile);
  height: var(--tile);
  position: relative;
  display: block;
  overflow: hidden;
  padding: 0;
  border: 2px solid #8e5a37;
  border-radius: 0;
  background:
    linear-gradient(135deg, rgba(255, 255, 255, 0.85), rgba(255, 255, 255, 0.2) 44%, rgba(0, 0, 0, 0.1)),
    #f1d0a4;
  box-shadow:
    inset 2px 2px 0 #fff1cc,
    inset -2px -2px 0 #b36f3d;
  cursor: pointer;
}

.tile:hover,
.tile.is-selected,
.tile.is-hint {
  background:
    linear-gradient(135deg, rgba(255, 255, 255, 0.9), rgba(255, 245, 125, 0.24)),
    #ffe0a9;
}

.tile.is-selected {
  outline: 0;
  box-shadow:
    inset 2px 2px 0 #fff1cc,
    inset -2px -2px 0 #b36f3d,
    0 0 0 4px #ffd91f,
    0 0 12px rgba(255, 217, 31, 0.5);
}

.tile.is-hint {
  outline: 0;
  box-shadow:
    inset 2px 2px 0 #fff1cc,
    inset -2px -2px 0 #b36f3d,
    0 0 0 4px #46ff3f,
    0 0 12px rgba(70, 255, 63, 0.45);
}

.tile.is-gone {
  opacity: 0;
  pointer-events: none;
}

.pokemon {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 124%;
  height: 124%;
  display: block;
  image-rendering: pixelated;
  object-fit: contain;
  pointer-events: none;
  transform: translate(-50%, -50%);
}

.tile-shatter,
.tile-piece {
  position: absolute;
  left: 0;
  top: 0;
  pointer-events: none;
  transform: translate(-50%, -50%);
}

.tile-shatter {
  z-index: 5;
  width: 0;
  height: 0;
}

.tile-piece {
  width: calc(var(--tile) / 3 + 1px);
  height: calc(var(--tile) / 3 + 1px);
  overflow: hidden;
  background:
    var(--tile-image) var(--sx) var(--sy) / 300% 300% no-repeat,
    linear-gradient(135deg, rgba(255, 255, 255, 0.84), rgba(255, 255, 255, 0.16) 45%, rgba(0, 0, 0, 0.08)),
    #f1d0a4;
  border: 1px solid rgba(142, 90, 55, 0.78);
  box-shadow:
    inset 1px 1px 0 rgba(255, 241, 204, 0.65),
    inset -1px -1px 0 rgba(128, 70, 36, 0.5),
    0 2px 4px rgba(0, 0, 0, 0.35);
  animation: tile-piece-shatter 300ms cubic-bezier(0.04, 0.74, 0.16, 1) var(--d) forwards;
}

@keyframes tile-piece-shatter {
  0% {
    opacity: 1;
    transform: translate(-50%, -50%) scale(1) rotate(0deg);
  }
  64% {
    opacity: 1;
  }
  100% {
    opacity: 0;
    transform: translate(calc(-50% + var(--x)), calc(-50% + var(--y))) scale(0.72) rotate(var(--r));
  }
}

.overlay {
  position: absolute;
  inset: 0;
  z-index: 5;
  display: grid;
  place-items: center;
  background: rgba(0, 0, 0, 0.62);
  opacity: 0;
  pointer-events: none;
}

.overlay.is-visible {
  opacity: 1;
  pointer-events: auto;
}

.options-panel {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(0, 0, 0, 0.54);
}

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

.options-card {
  width: min(420px, calc(100vw - 28px));
  border: 2px solid rgba(255, 230, 90, 0.78);
  border-radius: 8px;
  background: rgba(18, 18, 18, 0.96);
  box-shadow: 0 18px 44px rgba(0, 0, 0, 0.62);
  padding: 16px;
}

.options-card h2 {
  margin: 0 0 12px;
  color: #fff22f;
  text-align: center;
  font-size: 1.45rem;
}

.options-sections {
  display: grid;
  gap: 12px;
}

.option-section {
  display: grid;
  gap: 6px;
}

.option-section h3 {
  margin: 0;
  color: #ffe65a;
  font-size: 0.82rem;
  line-height: 1;
}

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

.options-grid button,
.options-close {
  min-height: 40px;
  border: 1px solid rgba(255, 230, 90, 0.42);
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.08);
  color: #fff;
  cursor: pointer;
  font-weight: 900;
}

.options-grid button.is-active,
.options-grid button:hover,
.options-close:hover {
  border-color: rgba(255, 230, 90, 0.85);
  background: rgba(255, 230, 90, 0.18);
}

.options-close {
  width: 100%;
}

.dialog {
  width: min(380px, calc(100% - 32px));
  border: 2px solid rgba(255, 230, 90, 0.72);
  border-radius: 8px;
  background: rgba(18, 18, 18, 0.95);
  color: #fff;
  padding: 20px;
  text-align: center;
  box-shadow: 0 18px 40px rgba(0, 0, 0, 0.58);
}

.dialog p {
  margin: 0;
  color: #e9e2bd;
  font-size: 0.95rem;
}

.dialog h2 {
  margin: 8px 0;
  color: #fff22f;
  font-size: clamp(1.45rem, 3vw, 2.2rem);
}

.dialog button {
  min-width: 118px;
  min-height: 40px;
  margin-top: 16px;
  border: 1px solid rgba(255, 230, 90, 0.72);
  border-radius: 6px;
  background: rgba(255, 230, 90, 0.16);
  color: #fff;
  cursor: pointer;
  font-weight: 900;
}

@media (max-width: 880px) {
  .top-panel {
    grid-template-columns: 1fr auto;
    gap: 8px;
  }

  .menu-bar {
    grid-column: 1 / 3;
    justify-content: start;
    overflow-x: auto;
  }

  .top-actions {
    gap: 6px;
  }

  .score-box {
    min-width: 58px;
  }

  .hud {
    grid-template-columns: auto auto auto auto;
    gap: 8px;
    padding: 8px;
  }

  .time-area {
    grid-column: 1 / -1;
  }
}

@media (orientation: portrait) {
  :root {
    --board-pad: calc(var(--tile) * 0.36);
  }

  body {
    background: #000;
  }

  .app-shell {
    padding: 0;
    overflow: visible;
  }

  .game {
    width: 100%;
    max-width: 100%;
    height: 100dvh;
    border: 0;
    border-radius: 0;
    grid-template-rows: auto auto minmax(0, 1fr);
    box-shadow: none;
  }

  .top-panel {
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 4px 8px;
    padding: 6px 8px 5px;
    border-bottom-color: rgba(255, 238, 150, 0.18);
    overflow: visible;
  }

  .top-actions {
    grid-column: 2;
    grid-row: 1;
    align-self: center;
  }

  .brand {
    gap: 6px;
    min-width: 0;
  }

  .brand img {
    width: 22px;
    height: 22px;
  }

  .brand p {
    display: none;
  }

  .brand h1 {
    font-size: clamp(1.06rem, 5vw, 1.38rem);
    white-space: nowrap;
  }

  .menu-bar {
    grid-column: 1 / -1;
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 4px;
    width: 100%;
    max-width: 100%;
    overflow: visible;
  }

  .menu-toggle {
    width: 100%;
    min-width: 0;
    min-height: 26px;
    font-size: clamp(0.76rem, 3.4vw, 0.9rem);
    border-radius: 5px;
  }

  .menu-popover {
    left: 0;
    right: auto;
    min-width: min(170px, 44vw);
    max-width: 44vw;
    transform: none;
  }

  .menu:nth-child(n + 3) .menu-popover {
    left: auto;
    right: 0;
  }

  .score-box {
    display: none;
  }

  .language-toggle {
    width: 26px;
    height: 26px;
    font-size: 0.82rem;
  }

  .language-popover {
    position: fixed;
    top: max(36px, env(safe-area-inset-top));
    right: 8px;
    left: auto;
    min-width: 118px;
    max-width: 150px;
    z-index: 60;
    transform: none;
  }

  .mobile-score {
    display: grid;
  }

  .hud {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    padding: 6px 8px;
    border-bottom: 1px solid rgba(255, 238, 150, 0.12);
    overflow: hidden;
    width: 100%;
    max-width: 100vw;
  }

  .class-card {
    display: none;
  }

  .class-card span {
    font-size: clamp(0.85rem, 4vw, 1.1rem);
  }

  .class-card strong {
    font-size: clamp(1.3rem, 7vw, 2rem);
  }

  .mini-stat {
    flex: 1 1 0;
    min-width: 0;
    padding: 2px 0;
  }

  .mini-stat span {
    font-size: clamp(0.78rem, 3.6vw, 1rem);
  }

  .mini-stat strong {
    font-size: clamp(1.25rem, 6vw, 1.85rem);
  }

  .time-area {
    grid-column: auto;
    flex: 0 0 100%;
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-areas: "label seconds" "bar bar";
    gap: 2px 8px;
    min-width: 0;
    width: 100%;
    max-width: 100%;
    overflow: hidden;
  }

  .time-area > strong {
    justify-self: start;
    font-size: clamp(1rem, 5vw, 1.35rem);
  }

  .time-area > span {
    font-size: clamp(1.1rem, 5.4vw, 1.55rem);
  }

  .time-shell {
    height: 17px;
    padding: 2px;
    border-width: 2px;
    width: 100%;
    max-width: 100%;
  }

  .time-bar {
    inset: 2px;
    max-width: calc(100% - 4px);
  }

  .board-panel {
    place-items: start center;
    padding: 6px 0 6px;
    overflow: hidden;
  }

  .board-wrap {
    align-self: start;
    max-width: 100%;
    filter: drop-shadow(3px 0 0 #ff6b2a);
  }

  .board {
    max-width: 100%;
  }

  .tile {
    border-width: 1px;
    box-shadow:
      inset 1px 1px 0 #fff1cc,
      inset -1px -1px 0 #b36f3d;
  }

  .tile.is-selected {
    box-shadow:
      inset 1px 1px 0 #fff1cc,
      inset -1px -1px 0 #b36f3d,
      0 0 0 3px #ffd91f,
      0 0 8px rgba(255, 217, 31, 0.44);
  }

  .tile.is-hint {
    box-shadow:
      inset 1px 1px 0 #fff1cc,
      inset -1px -1px 0 #b36f3d,
      0 0 0 3px #46ff3f,
      0 0 8px rgba(70, 255, 63, 0.4);
  }

  .pokemon {
    width: 124%;
    height: 124%;
  }

  .dialog {
    width: calc(100vw - 28px);
    max-width: 330px;
    padding: 14px;
    overflow: hidden;
  }

  .dialog h2 {
    font-size: clamp(1.15rem, 6vw, 1.55rem);
    line-height: 1.16;
    overflow-wrap: anywhere;
  }

  .dialog p {
    font-size: 0.88rem;
    line-height: 1.45;
    overflow-wrap: anywhere;
  }

  .dialog button {
    min-height: 36px;
    margin-top: 12px;
    font-size: 1rem;
  }
}

@media (orientation: portrait) and (max-width: 430px) {
  .menu-bar {
    gap: 3px;
  }

  .menu-toggle {
    min-height: 24px;
    font-size: clamp(0.68rem, 3.15vw, 0.82rem);
    padding: 0 3px;
  }
}

@media (orientation: portrait) and (max-height: 720px) {
  .brand p {
    display: none;
  }

  .hud {
    grid-template-columns: auto auto auto auto minmax(180px, 1fr);
    gap: 8px;
  }

  .class-card {
    min-width: 78px;
  }

  .class-card span {
    font-size: 1.05rem;
  }

  .class-card strong {
    font-size: 1.55rem;
  }

  .mini-stat {
    min-width: 56px;
  }

  .mini-stat span {
    font-size: 0.8rem;
  }

  .mini-stat strong {
    font-size: 1.25rem;
  }

  .time-area > strong {
    font-size: 1.3rem;
  }

  .time-area > span {
    font-size: 1.45rem;
  }
}
