:root {
  --orange: #FF8A3D;
  --orange-deep: #E86314;
  --orange-soft: #FFB36E;
  --cream: #FFF8EC;
  --cream-2: #FFEFD8;
  --ink: #4A2A16;
  --muted: #9A7454;
  --line: #F0D2A9;
  --wood: #E8A45F;
  --mint: #9BD3AE;
  --safe-top: max(14px, env(safe-area-inset-top));
  --safe-bottom: max(14px, env(safe-area-inset-bottom));
}

* { box-sizing: border-box; }

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

body {
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  background:
    radial-gradient(95% 55% at 50% -6%, #FFFDF8 0%, rgba(255, 253, 248, 0) 58%),
    radial-gradient(70% 42% at 102% 0%, rgba(155, 211, 174, .34) 0%, rgba(155, 211, 174, 0) 62%),
    linear-gradient(180deg, #FFE9CD 0%, #FFDDB8 46%, #F3BE85 100%);
  background-attachment: fixed;
  min-height: 100vh;
  min-height: 100dvh;
  overflow-x: hidden;
  overscroll-behavior-y: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  touch-action: manipulation;
}

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

body::before {
  content: "";
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 0;
  pointer-events: none;
  background-image:
    linear-gradient(rgba(190, 120, 62, .075) 1.5px, transparent 1.5px),
    linear-gradient(90deg, rgba(190, 120, 62, .075) 1.5px, transparent 1.5px);
  background-size: 46px 46px;
  -webkit-mask-image: linear-gradient(180deg, #000 0%, rgba(0, 0, 0, .45) 58%, transparent 92%);
  mask-image: linear-gradient(180deg, #000 0%, rgba(0, 0, 0, .45) 58%, transparent 92%);
}

body::after {
  content: "";
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 0;
  pointer-events: none;
  background:
    radial-gradient(60% 22% at 50% 103%, rgba(156, 79, 22, .22) 0%, rgba(156, 79, 22, 0) 70%);
}

body.modal-open { overflow: hidden; }

.bg-deco {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 0;
  overflow: hidden;
  pointer-events: none;
}

.halo {
  position: absolute;
  border-radius: 50%;
  filter: blur(2px);
}

.halo-1 {
  width: 190px;
  height: 190px;
  left: -70px;
  top: 16%;
  background: radial-gradient(circle, rgba(255, 171, 98, .40) 0%, rgba(255, 171, 98, 0) 70%);
}

.halo-2 {
  width: 230px;
  height: 230px;
  right: -110px;
  top: 44%;
  background: radial-gradient(circle, rgba(155, 211, 174, .36) 0%, rgba(155, 211, 174, 0) 70%);
}

.halo-3 {
  width: 300px;
  height: 160px;
  left: 50%;
  bottom: -90px;
  transform: translateX(-50%);
  background: radial-gradient(circle, rgba(255, 255, 255, .45) 0%, rgba(255, 255, 255, 0) 70%);
}

.app {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 462px;
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0 auto;
}

/* ---------- 顶部状态 ---------- */

.topbar {
  flex: 0 0 auto;
  padding: var(--safe-top) 16px 0;
}

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

.chip {
  display: flex;
  align-items: center;
  height: 46px;
  gap: 6px;
  padding: 0 13px;
  border: 1.5px solid rgba(238, 196, 143, .82);
  border-radius: 16px;
  background: rgba(255, 252, 245, .82);
  box-shadow: 0 4px 14px rgba(160, 94, 38, .10), inset 0 1px 0 rgba(255, 255, 255, .8);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

.chip .mp-icon {
  width: 20px;
  height: 20px;
  color: var(--orange);
  flex: 0 0 auto;
}

.chip-time {
  min-width: 106px;
  color: #5D3A1F;
  font-size: 18px;
  letter-spacing: .02em;
}

.chip-score {
  flex: 1;
  justify-content: center;
  min-width: 0;
  color: var(--ink);
  font-size: 19px;
}

.chip-score .mp-icon { color: #F4A11E; }

.chip-unit {
  margin-left: -1px;
  font-size: 11px;
  font-weight: 700;
  color: var(--muted);
}

.chip-score.bump { animation: chipPop .34s ease; }

@keyframes chipPop {
  0% { transform: scale(1); }
  38% { transform: scale(1.06); }
  100% { transform: scale(1); }
}

.chip-time.urgent {
  border-color: #FF6A4F;
  background: linear-gradient(180deg, #FF7B5F, #F0563B);
  color: #FFF8F1;
  box-shadow: 0 4px 16px rgba(216, 71, 41, .35);
  animation: timePulse 1s ease-in-out infinite;
}

.chip-time.urgent .mp-icon { color: #FFF4E8; }

@keyframes timePulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.035); }
}

.restart-btn {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 46px;
  height: 46px;
  padding: 0;
  border: 1.5px solid rgba(238, 196, 143, .82);
  border-radius: 16px;
  background: rgba(255, 252, 245, .82);
  box-shadow: 0 4px 14px rgba(160, 94, 38, .10), inset 0 1px 0 rgba(255, 255, 255, .8);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
  color: #C87832;
  cursor: pointer;
  transition: transform .13s ease, background .13s ease;
}

.restart-btn .mp-icon {
  width: 22px;
  height: 22px;
  transition: transform .3s ease;
}

.restart-btn:active {
  transform: scale(.92);
  background: #FFF0DA;
}

.restart-btn:focus { outline: none; }

.restart-btn:focus-visible {
  outline: 3px solid rgba(255, 138, 61, .55);
  outline-offset: 2px;
}

.restart-btn:active .mp-icon { transform: rotate(150deg); }

.progress-card {
  margin-top: 9px;
  padding: 8px 12px 10px;
  border: 1.5px solid rgba(238, 196, 143, .62);
  border-radius: 16px;
  background: rgba(255, 250, 240, .72);
  box-shadow: 0 4px 14px rgba(160, 94, 38, .07);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
}

.progress-top {
  display: flex;
  align-items: center;
  gap: 8px;
}

.pair-text {
  color: #8B6749;
  font-size: 12px;
  font-weight: 700;
  white-space: nowrap;
}

.pair-text b {
  color: var(--orange-deep);
  font-size: 14px;
  font-weight: 900;
}

.remain-text {
  margin-left: auto;
  color: #A6815E;
  font-size: 11px;
  font-weight: 600;
  white-space: nowrap;
}

.progress-track {
  height: 7px;
  margin-top: 7px;
  overflow: hidden;
  border-radius: 99px;
  background: #F2DDC2;
  box-shadow: inset 0 1px 2px rgba(154, 99, 45, .15);
}

.progress-fill {
  width: 0%;
  height: 100%;
  border-radius: 99px;
  background: linear-gradient(90deg, #FFC06F, #FF7A2A);
  box-shadow: 0 0 8px rgba(255, 122, 42, .55);
  transition: width .38s cubic-bezier(.2, .8, .2, 1);
}

/* ---------- 棋盘 ---------- */

.stage {
  position: relative;
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 18px 16px 4px;
}

.board-shell {
  position: relative;
  width: min(calc(100vw - 32px), 430px, max(238px, calc((100vh - 206px) * .62)));
  width: min(calc(100vw - 32px), 430px, max(238px, calc((100dvh - 206px) * .62)));
  padding: 7px;
  border-radius: 32px;
  background: linear-gradient(155deg, #F7C387 0%, #E29B52 48%, #F3B774 100%);
  box-shadow:
    0 18px 30px rgba(156, 79, 22, .26),
    0 3px 0 rgba(255, 255, 255, .38) inset,
    0 -3px 0 rgba(135, 64, 13, .25) inset;
}

.board-shell::before {
  content: "";
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 0;
  border-radius: 32px;
  background: repeating-linear-gradient(104deg, rgba(255, 255, 255, .16) 0 2px, rgba(255, 255, 255, 0) 2px 11px);
  mix-blend-mode: soft-light;
  pointer-events: none;
}

.board {
  position: relative;
  z-index: 1;
  padding: 14px;
  border-radius: 26px;
  background:
    radial-gradient(125% 85% at 50% 0%, #FFFDF8 0%, #FFF7E9 58%, #FBE9CD 100%);
  box-shadow:
    0 4px 10px rgba(151, 87, 33, .16) inset,
    0 -3px 0 rgba(255, 255, 255, .82) inset;
}

.level-ribbon {
  position: absolute;
  top: -12px;
  left: 20px;
  z-index: 6;
  padding: 5px 12px 4px;
  border: 2px solid #FFF3DD;
  border-radius: 11px;
  background: linear-gradient(180deg, #FFA258, #F5761D);
  box-shadow: 0 5px 12px rgba(191, 92, 17, .28);
  color: #FFFDF8;
  font-size: 12px;
  font-weight: 800;
  letter-spacing: .06em;
  transform: rotate(-2deg);
}

.grid {
  position: relative;
  z-index: 1;
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
}

.tile {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  aspect-ratio: 1 / 1;
  min-width: 0;
  padding: 0;
  overflow: hidden;
  border: 2px solid #EFD3AC;
  border-radius: 14px;
  background: linear-gradient(158deg, #FFFDF8 0%, #FFF7EA 45%, var(--tint, #FFEBD4) 100%);
  box-shadow:
    0 3px 0 rgba(181, 120, 61, .15),
    0 4px 9px rgba(141, 84, 35, .10);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: transform .13s ease, box-shadow .13s ease, border-color .13s ease, opacity .18s ease;
}

.tile::after {
  content: "";
  position: absolute;
  left: 7%;
  right: 7%;
  top: 4%;
  height: 38%;
  border-radius: 50%;
  background: linear-gradient(180deg, rgba(255, 255, 255, .62), rgba(255, 255, 255, 0));
  pointer-events: none;
}

.tile img {
  position: relative;
  z-index: 1;
  display: block;
  width: 84%;
  height: 84%;
  object-fit: contain;
  pointer-events: none;
  filter: drop-shadow(0 1px 1px rgba(93, 48, 16, .08));
  transition: transform .15s ease;
}

.tile:active { transform: scale(.94); }

.tile:focus { outline: none; }

.tile:focus-visible {
  outline: 3px solid rgba(255, 138, 61, .55);
  outline-offset: 2px;
}

.tile.selected {
  z-index: 4;
  border-color: var(--orange);
  box-shadow:
    0 0 0 3px rgba(255, 138, 61, .28),
    0 5px 12px rgba(211, 96, 21, .26);
  transform: translateY(-2px) scale(1.04);
}

.tile.selected::before {
  content: "";
  position: absolute;
  top: 4px;
  right: 4px;
  z-index: 2;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--orange);
  box-shadow: 0 0 0 3px rgba(255, 138, 61, .22);
}

.tile.is-matched {
  z-index: 5;
  border-color: #FFC16F;
  animation: matchPop .32s ease forwards;
}

@keyframes matchPop {
  0% { transform: scale(1); }
  32% { transform: scale(1.13) rotate(-2deg); }
  100% { transform: scale(.2) rotate(8deg); opacity: 0; }
}

.tile.is-empty {
  border: 2px dashed rgba(221, 183, 137, .26);
  background: rgba(255, 250, 241, .30);
  box-shadow: none;
  pointer-events: none;
}

.tile.is-empty img,
.tile.is-empty::after { display: none; }

.tile.shake { animation: tileShake .3s ease; }

@keyframes tileShake {
  0%, 100% { transform: translateX(0) rotate(0); }
  25% { transform: translateX(-4px) rotate(-1.5deg); }
  72% { transform: translateX(4px) rotate(1.5deg); }
}

.tile.is-flipping img { animation: tileFlip .42s ease; }

@keyframes tileFlip {
  0% { transform: rotateY(88deg) scale(.85); opacity: 0; }
  100% { transform: rotateY(0) scale(1); opacity: 1; }
}

@media (hover: hover) {
  .tile:hover { border-color: #FFC589; }
  .tile:hover img { transform: scale(1.06); }
}

/* ---------- 连线 ---------- */

.path-layer {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 3;
  width: 100%;
  height: 100%;
  overflow: visible;
  opacity: 0;
  pointer-events: none;
  transition: opacity .18s ease;
}

.path-layer.show { opacity: 1; }

.path-under {
  fill: none;
  stroke: #FFF8EA;
  stroke-width: 10;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.path-main {
  fill: none;
  stroke: #FF7A24;
  stroke-width: 5;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-dasharray: 100;
  stroke-dashoffset: 100;
  animation: drawPath .22s ease-out forwards;
}

.path-dot {
  fill: #FF7A24;
  stroke: #FFF7E8;
  stroke-width: 3;
}

@keyframes drawPath {
  to { stroke-dashoffset: 0; }
}

/* ---------- 飘分 ---------- */

.score-layer {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 20;
  overflow: visible;
  pointer-events: none;
}

.float-score {
  position: absolute;
  transform: translate(-50%, -50%);
  color: #FFFDF8;
  font-size: 21px;
  font-weight: 900;
  letter-spacing: .01em;
  text-shadow: 0 2px 0 #D45B0D, 0 5px 12px rgba(118, 50, 7, .38);
  white-space: nowrap;
  animation: floatScore .86s cubic-bezier(.2, .7, .3, 1) forwards;
}

.float-score .combo {
  display: block;
  margin-bottom: 2px;
  color: #FFE7A6;
  font-size: 11px;
  font-weight: 800;
  letter-spacing: .12em;
  text-align: center;
  text-shadow: 0 1px 0 #B4520D;
}

@keyframes floatScore {
  0% { opacity: 0; transform: translate(-50%, -38%) scale(.72); }
  20% { opacity: 1; transform: translate(-50%, -58%) scale(1.08); }
  100% { opacity: 0; transform: translate(-50%, -165%) scale(.96); }
}

/* ---------- 底部提示 ---------- */

.tipbar {
  flex: 0 0 auto;
  display: flex;
  justify-content: center;
  padding: 10px 16px var(--safe-bottom);
}

.tip-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  max-width: 100%;
  padding: 8px 14px;
  border: 1px solid rgba(233, 190, 140, .68);
  border-radius: 99px;
  background: rgba(255, 251, 242, .74);
  box-shadow: 0 4px 12px rgba(160, 94, 38, .07);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
  color: #8A6547;
  font-size: 12px;
  font-weight: 600;
  line-height: 1.3;
  text-align: center;
}

.tip-pill .mp-icon {
  width: 16px;
  height: 16px;
  color: #F09A3E;
  flex: 0 0 auto;
}

/* ---------- Toast ---------- */

.toast {
  position: fixed;
  top: calc(var(--safe-top) + 108px);
  left: 50%;
  z-index: 60;
  max-width: calc(100vw - 40px);
  padding: 10px 16px;
  border: 1px solid rgba(255, 255, 255, .14);
  border-radius: 99px;
  background: rgba(74, 42, 22, .93);
  box-shadow: 0 10px 24px rgba(74, 42, 22, .26);
  color: #FFF6E8;
  font-size: 13px;
  font-weight: 600;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, -8px);
  transition: opacity .2s ease, transform .2s ease;
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
}

.toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

/* ---------- 结算 ---------- */

.overlay {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 80;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: rgba(67, 38, 20, .50);
  -webkit-backdrop-filter: blur(7px);
  backdrop-filter: blur(7px);
  opacity: 0;
  transition: opacity .25s ease;
}

.overlay[hidden] { display: none; }
.overlay.show { opacity: 1; }

.overlay-card {
  position: relative;
  width: min(100%, 350px);
  padding: 26px 22px 22px;
  overflow: hidden;
  border: 2px solid #FFE2BC;
  border-radius: 30px;
  background: linear-gradient(180deg, #FFFDF7 0%, #FFF4E1 100%);
  box-shadow: 0 26px 60px rgba(67, 33, 9, .35), 0 2px 0 #FFF inset;
  text-align: center;
  transform: translateY(16px) scale(.94);
  transition: transform .32s cubic-bezier(.2, .9, .3, 1.25);
}

.overlay.show .overlay-card { transform: none; }

.overlay-card::before {
  content: "";
  position: absolute;
  top: -46px;
  right: -40px;
  width: 150px;
  height: 150px;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(255, 189, 80, .30) 0%, rgba(255, 189, 80, 0) 70%);
  pointer-events: none;
}

.overlay-card::after {
  content: "";
  position: absolute;
  bottom: -54px;
  left: -40px;
  width: 150px;
  height: 150px;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(155, 211, 174, .26) 0%, rgba(155, 211, 174, 0) 70%);
  pointer-events: none;
}

.result-badge {
  position: relative;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 80px;
  height: 80px;
  margin: 0 auto 14px;
  border-radius: 26px;
  background: linear-gradient(160deg, #FFD36E, #FF9F2E);
  box-shadow: 0 10px 22px rgba(238, 144, 35, .35), 0 -5px 0 rgba(180, 94, 16, .18) inset;
  transform: rotate(-4deg);
}

.result-badge.win { animation: badgePop .5s cubic-bezier(.2, .9, .3, 1.4) both; }

.result-badge.lose {
  background: linear-gradient(160deg, #FFC4B6, #F07C68);
  box-shadow: 0 10px 22px rgba(208, 93, 68, .32), 0 -5px 0 rgba(163, 61, 39, .16) inset;
  transform: rotate(4deg);
}

@keyframes badgePop {
  0% { transform: rotate(-16deg) scale(.6); }
  60% { transform: rotate(3deg) scale(1.08); }
  100% { transform: rotate(-4deg) scale(1); }
}

.result-badge .mp-icon {
  width: 42px;
  height: 42px;
  color: #FFF;
}

.result-title {
  position: relative;
  z-index: 1;
  margin: 0;
  color: #4A2A16;
  font-size: 24px;
  font-weight: 900;
  letter-spacing: .01em;
}

.result-desc {
  position: relative;
  z-index: 1;
  margin: 7px 0 18px;
  color: #9A7454;
  font-size: 13px;
  line-height: 1.6;
}

.result-stats {
  position: relative;
  z-index: 1;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 20px;
}

.result-stat {
  padding: 9px 4px;
  border: 1px solid #F4DDBE;
  border-radius: 14px;
  background: #FFF8EC;
}

.result-stat span {
  display: block;
  color: #A9835F;
  font-size: 11px;
  font-weight: 600;
}

.result-stat b {
  display: block;
  margin-top: 2px;
  color: #4A2A16;
  font-size: 17px;
  font-weight: 900;
  font-variant-numeric: tabular-nums;
}

.primary-btn {
  position: relative;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  height: 52px;
  padding: 0 18px;
  border: 0;
  border-radius: 16px;
  background: linear-gradient(180deg, #FFA055, #F5761E);
  box-shadow: 0 6px 0 #D15E12, 0 10px 20px rgba(224, 103, 20, .30);
  color: #FFFDF8;
  font-family: inherit;
  font-size: 17px;
  font-weight: 800;
  cursor: pointer;
  transition: transform .12s ease, box-shadow .12s ease;
}

.primary-btn .mp-icon {
  width: 20px;
  height: 20px;
}

.primary-btn:active {
  transform: translateY(4px);
  box-shadow: 0 2px 0 #D15E12, 0 5px 10px rgba(224, 103, 20, .24);
}

.primary-btn:focus { outline: none; }

.primary-btn:focus-visible {
  outline: 3px solid rgba(255, 138, 61, .55);
  outline-offset: 3px;
}

/* ---------- 小屏适配 ---------- */

@media (max-height: 700px) {
  .stage { padding-top: 16px; }
  .progress-card { margin-top: 7px; }
  .tipbar { padding-top: 6px; padding-bottom: max(8px, env(safe-area-inset-bottom)); }
  .tip-pill { padding: 6px 11px; font-size: 11px; }
}

@media (max-width: 350px) {
  .topbar { padding-left: 10px; padding-right: 10px; }
  .stage { padding-left: 10px; padding-right: 10px; }
  .chip-time { min-width: 94px; font-size: 16px; }
  .chip-score { font-size: 17px; }
  .restart-btn { width: 42px; height: 42px; }
  .board { padding: 10px; }
  .grid { gap: 5px; }
  .tile { border-radius: 11px; }
  .pair-text, .remain-text { font-size: 10px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .001ms !important;
  }
}
