* {
  box-sizing: border-box;
}

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

body {
  position: fixed;
  inset: 0;
  overflow: hidden;
  overscroll-behavior: none;
  color: #18323f;
  font-family: Arial, "PingFang SC", "Microsoft YaHei", sans-serif;
  background:
    linear-gradient(180deg, rgba(245, 251, 248, 0.96), rgba(250, 241, 230, 0.98)),
    #f5f8f3;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
  -webkit-touch-callout: none;
  user-select: none;
}

.level-view {
  display: none;
}

.level-one.is-active {
  display: block;
}

.level-two.is-active {
  display: grid;
}

.level-one {
  width: min(100vw, 430px);
  height: 100vh;
  height: 100dvh;
  height: var(--app-height, 100dvh);
  margin: 0 auto;
  overflow: hidden;
  background: #f5f8f3;
}

#gameCanvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
}

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

.app {
  position: relative;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 8px;
  width: min(100vw, 430px);
  height: 100vh;
  height: 100dvh;
  height: var(--app-height, 100dvh);
  margin: 0 auto;
  padding: max(8px, env(safe-area-inset-top)) 8px max(8px, env(safe-area-inset-bottom));
  overflow: hidden;
}

.app.level-view {
  display: none;
}

.app.level-view.is-active {
  display: grid;
}

.hud {
  position: relative;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 58px;
  gap: 8px;
  padding: 8px 9px;
  border: 1px solid rgba(35, 76, 83, 0.12);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.88);
  box-shadow: 0 14px 36px rgba(36, 82, 88, 0.08);
}

.hud-title {
  min-width: 0;
  flex: 1;
}

.hud h1 {
  margin: 0 0 4px;
  font-size: 16px;
  line-height: 1.16;
}

.hud p {
  min-height: 16px;
  margin: 0;
  color: #65757c;
  font-size: 12px;
  line-height: 1.35;
}

.difficulty {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  width: min(210px, 100%);
  margin-top: 6px;
  padding: 2px;
  border: 1px solid rgba(35, 76, 83, 0.1);
  border-radius: 8px;
  background: rgba(238, 247, 244, 0.72);
}

.difficulty-button {
  display: grid;
  place-items: center;
  min-width: 0;
  min-height: 26px;
  border: 0;
  border-radius: 6px;
  color: #65757c;
  background: transparent;
  font-size: 11px;
  font-weight: 700;
  cursor: default;
}

.difficulty-button.is-active {
  color: #18323f;
  background: #68d9c5;
  box-shadow: 0 4px 12px rgba(32, 169, 141, 0.18);
}

.difficulty-button.is-done {
  color: #0d7e70;
  background: rgba(104, 217, 197, 0.22);
}

.difficulty-button.is-locked {
  color: rgba(101, 117, 124, 0.52);
}

.hud-meta {
  display: flex;
  align-items: center;
  gap: 8px;
}

.timer {
  display: grid;
  place-items: center;
  width: 50px;
  height: 38px;
  border: 1px solid rgba(238, 93, 34, 0.22);
  border-radius: 8px;
  color: #18323f;
  background: rgba(255, 241, 216, 0.9);
  font-size: 15px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.timer.is-low {
  color: #b6402f;
  background: rgba(255, 232, 225, 0.94);
}

.icon-button {
  width: 38px;
  height: 38px;
  border: 1px solid rgba(32, 169, 141, 0.22);
  border-radius: 8px;
  color: #0d7e70;
  background: rgba(232, 251, 246, 0.92);
  font-size: 22px;
  font-weight: 700;
  cursor: pointer;
}

.stage {
  position: relative;
  z-index: 1;
  min-height: 0;
  overflow: hidden;
  border: 1px solid rgba(35, 76, 83, 0.1);
  border-radius: 8px;
  background:
    linear-gradient(180deg, rgba(251, 255, 253, 0.9), rgba(255, 247, 237, 0.86));
}

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

.board {
  position: absolute;
  inset: 8px;
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  grid-template-rows: repeat(8, minmax(0, 1fr));
  gap: 5px;
  transition: opacity 420ms ease, transform 420ms ease;
}

.board.is-hidden {
  opacity: 0;
  pointer-events: none;
  transform: scale(0.96);
}

.cell {
  position: relative;
  min-width: 0;
  min-height: 0;
  border: 1px solid rgba(24, 50, 63, 0.045);
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.18);
}

.card {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template-rows: auto 1fr;
  place-items: center;
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
  padding: 4px 3px;
  border: 1px solid rgba(95, 125, 130, 0.3);
  border-radius: 6px;
  color: #18323f;
  cursor: pointer;
  transition:
    opacity 220ms ease,
    transform 180ms ease,
    border-color 180ms ease,
    box-shadow 180ms ease;
}

.card[data-type="point"] {
  background: linear-gradient(180deg, rgba(237, 255, 249, 0.96), rgba(104, 217, 197, 0.84));
}

.card[data-type="meridian"] {
  background: linear-gradient(180deg, rgba(255, 248, 224, 0.96), rgba(248, 187, 82, 0.84));
}

.card::before {
  display: block;
  min-width: 26px;
  padding: 1px 4px;
  border-radius: 6px;
  color: rgba(24, 50, 63, 0.68);
  background: rgba(255, 255, 255, 0.42);
  font-size: 9px;
  line-height: 1.2;
  content: attr(data-badge);
}

.card span {
  display: block;
  max-width: 100%;
  overflow-wrap: anywhere;
  text-align: center;
  font-size: 13px;
  font-weight: 700;
  line-height: 1.05;
}

.card.is-selected {
  border-color: #e77835;
  box-shadow: 0 0 0 3px rgba(248, 187, 82, 0.24), 0 10px 24px rgba(231, 120, 53, 0.22);
  transform: translateY(-2px);
}

.card.is-wrong {
  animation: shake 260ms ease;
}

.card.is-clearing {
  pointer-events: none;
  animation: clearPop 420ms ease both;
}

.card.is-removed {
  opacity: 0;
  pointer-events: none;
  transform: scale(0.66);
}

.burst {
  position: fixed;
  z-index: 12;
  width: 1px;
  height: 1px;
  pointer-events: none;
}

.spark {
  position: absolute;
  left: 0;
  top: 0;
  width: 6px;
  height: 6px;
  border-radius: 999px;
  background: #ffe26f;
  box-shadow: 0 0 12px rgba(255, 129, 44, 0.78);
  transform: translate(-50%, -50%);
  animation: sparkFly 520ms ease-out forwards;
}

.match-line {
  position: fixed;
  z-index: 11;
  left: 0;
  top: 0;
  width: var(--line-width);
  height: 4px;
  border-radius: 999px;
  pointer-events: none;
  background: linear-gradient(90deg, rgba(104, 217, 197, 0), #ffe26f 20%, #f8bb52 56%, rgba(231, 120, 53, 0));
  box-shadow: 0 0 14px rgba(248, 187, 82, 0.7);
  opacity: 0;
  transform: translate(var(--line-x), var(--line-y)) rotate(var(--line-angle)) scaleX(0);
  transform-origin: left center;
  animation: matchLine 520ms ease-out forwards;
}

.score-float {
  position: fixed;
  z-index: 13;
  left: var(--float-x);
  top: var(--float-y);
  min-width: 42px;
  padding: 4px 8px;
  border-radius: 999px;
  color: #18323f;
  background: #ffe26f;
  box-shadow: 0 8px 22px rgba(231, 120, 53, 0.24);
  font-size: 12px;
  font-weight: 800;
  text-align: center;
  pointer-events: none;
  transform: translate(-50%, -50%) scale(0.78);
  animation: scoreFloat 760ms ease-out forwards;
}

.stage::after {
  position: absolute;
  inset: 0;
  border-radius: 8px;
  pointer-events: none;
  background: radial-gradient(circle at 50% 50%, rgba(255, 226, 111, 0.34), rgba(104, 217, 197, 0.06) 42%, transparent 70%);
  opacity: 0;
  content: "";
}

.stage.is-match-flash::after {
  animation: stageFlash 460ms ease-out;
}

.end-scene {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: none;
  background:
    linear-gradient(180deg, rgba(16, 43, 50, 0.98), rgba(235, 247, 242, 0.98) 52%, rgba(255, 244, 231, 0.98));
}

.end-scene.is-visible {
  display: block;
}

#endCanvas {
  width: 100%;
  height: 100%;
}

.win-copy {
  position: absolute;
  top: 50%;
  left: 50%;
  display: grid;
  width: min(calc(100vw - 34px), 356px);
  min-height: 144px;
  place-items: center;
  padding: 20px 18px 16px;
  border-radius: 8px;
  color: #ffffff;
  background: rgba(24, 50, 63, 0.9);
  box-shadow: 0 0 34px rgba(255, 129, 44, 0.38);
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, -50%) scale(0.92);
  transition: opacity 420ms ease, transform 420ms ease;
}

.win-copy.is-visible {
  opacity: 1;
  pointer-events: auto;
  transform: translate(-50%, -50%) scale(1);
}

.win-copy strong {
  font-size: 20px;
  line-height: 1.35;
  text-align: center;
}

.win-copy span {
  color: #ffe26f;
  font-size: 17px;
  font-weight: 700;
}

.win-copy button,
.result-box button {
  min-width: 104px;
  min-height: 34px;
  border: 0;
  border-radius: 8px;
  color: #18323f;
  background: #68d9c5;
  font-weight: 700;
  cursor: pointer;
}

.result-panel {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: none;
  place-items: center;
  padding: 18px;
  background: rgba(16, 43, 50, 0.48);
}

.result-panel.is-visible {
  display: grid;
}

.result-box {
  display: grid;
  width: min(calc(100vw - 34px), 320px);
  gap: 14px;
  place-items: center;
  padding: 22px 18px;
  border-radius: 8px;
  color: #18323f;
  background: rgba(255, 255, 255, 0.96);
  box-shadow: 0 18px 48px rgba(24, 50, 63, 0.22);
}

.result-box strong {
  font-size: 22px;
}

.result-box span {
  color: #65757c;
  font-size: 14px;
}

@keyframes shake {
  0%,
  100% {
    transform: translateX(0);
  }

  25% {
    transform: translateX(-5px);
  }

  75% {
    transform: translateX(5px);
  }
}

@keyframes clearPop {
  0% {
    filter: brightness(1);
    transform: translateX(0) scale(1);
  }

  18% {
    transform: translateX(-4px) scale(1.04);
  }

  36% {
    filter: brightness(1.22);
    transform: translateX(4px) scale(1.08);
  }

  58% {
    transform: translateX(-2px) scale(1.02);
  }

  100% {
    filter: brightness(1.08);
    transform: translateX(0) scale(0.68);
  }
}

@keyframes sparkFly {
  0% {
    opacity: 1;
    transform: translate(-50%, -50%) scale(1);
  }

  100% {
    opacity: 0;
    transform: translate(calc(-50% + var(--dx)), calc(-50% + var(--dy))) scale(0.35);
  }
}

@keyframes matchLine {
  0% {
    opacity: 0;
    transform: translate(var(--line-x), var(--line-y)) rotate(var(--line-angle)) scaleX(0);
  }

  24% {
    opacity: 1;
  }

  78% {
    opacity: 1;
    transform: translate(var(--line-x), var(--line-y)) rotate(var(--line-angle)) scaleX(1);
  }

  100% {
    opacity: 0;
    transform: translate(var(--line-x), var(--line-y)) rotate(var(--line-angle)) scaleX(1);
  }
}

@keyframes scoreFloat {
  0% {
    opacity: 0;
    transform: translate(-50%, -25%) scale(0.72);
  }

  18% {
    opacity: 1;
    transform: translate(-50%, -55%) scale(1.06);
  }

  100% {
    opacity: 0;
    transform: translate(-50%, -132%) scale(0.9);
  }
}

@keyframes stageFlash {
  0% {
    opacity: 0;
  }

  28% {
    opacity: 1;
  }

  100% {
    opacity: 0;
  }
}

@media (max-width: 420px) {
  .app {
    padding-right: 7px;
    padding-left: 7px;
  }

  .hud h1 {
    font-size: 15px;
  }

  .hud p {
    font-size: 11px;
  }

  .difficulty {
    width: min(180px, 100%);
    margin-top: 5px;
  }

  .difficulty-button {
    min-height: 24px;
    font-size: 10px;
  }

  .board {
    inset: 7px;
    gap: 4px;
  }

  .card span {
    font-size: 12px;
  }
}

@media (max-width: 360px), (max-height: 700px) {
  .app {
    gap: 6px;
    padding-right: 6px;
    padding-left: 6px;
  }

  .hud {
    min-height: 52px;
    padding: 7px;
  }

  .hud h1 {
    font-size: 14px;
  }

  .hud p {
    font-size: 10px;
  }

  .difficulty {
    width: min(160px, 100%);
    margin-top: 4px;
  }

  .difficulty-button {
    min-height: 22px;
    font-size: 9px;
  }

  .hud-meta {
    gap: 5px;
  }

  .timer,
  .icon-button {
    width: 34px;
    height: 34px;
  }

  .timer {
    font-size: 13px;
  }

  .icon-button {
    font-size: 19px;
  }

  .board {
    inset: 6px;
    gap: 3px;
  }

  .card {
    padding: 3px 2px;
  }

  .card::before {
    min-width: 24px;
    font-size: 8px;
  }

  .card span {
    font-size: 11px;
  }
}
