:root {
  --cols: 4;
  --panel: #ffffff;
  --accent: #ff7a59;
  --accent-2: #ff9173;
  --text: #4a3a2a;
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
}

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

html, body {
  margin: 0; min-height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  color: var(--text);
  background: linear-gradient(160deg, #fff3c4 0%, #ffd9e8 50%, #d8f0ff 100%);
  background-attachment: fixed;
  -webkit-user-select: none; user-select: none;
}

.wrap {
  min-height: 100vh; display: flex; flex-direction: column; align-items: center;
  padding: calc(env(safe-area-inset-top) + 12px) 16px calc(env(safe-area-inset-bottom) + 16px);
  gap: 12px;
}

.topbar { width: 100%; max-width: 620px; display: flex; align-items: center; justify-content: space-between; }
.brand { font-size: 22px; font-weight: 800; margin: 0; color: #ff7a59; text-shadow: 0 2px 0 #fff; }
.back-btn, .sound-toggle {
  flex: none; width: 40px; height: 40px; border-radius: 50%; border: none;
  background: #fff; color: var(--accent); box-shadow: var(--shadow);
  font-size: 20px; font-weight: 800; text-decoration: none;
  display: flex; align-items: center; justify-content: center; cursor: pointer;
}
.back-btn:active, .sound-toggle:active { transform: scale(0.92); }
.sound-toggle.off { opacity: 0.5; }
.topbar-right { display: flex; gap: 8px; }

.hud {
  width: 100%; max-width: 620px; display: flex; align-items: center; gap: 10px;
  flex-wrap: wrap;
}
.hud-item {
  background: #fff; border-radius: 999px; padding: 8px 16px; font-size: 15px; font-weight: 700;
  box-shadow: var(--shadow); color: #8a7a66;
}
.hud-item b { color: var(--accent); font-size: 17px; }
.mini-btn {
  margin-left: auto; border: none; background: var(--accent); color: #fff; font-weight: 800;
  border-radius: 999px; padding: 9px 18px; font-size: 14px; cursor: pointer; box-shadow: var(--shadow);
}
.mini-btn + .mini-btn { margin-left: 0; }
.mini-btn:active { transform: scale(0.95); }

.board-area { flex: 1; display: flex; align-items: center; justify-content: center; width: 100%; }
.board {
  position: relative;
  display: grid;
  grid-template-columns: repeat(var(--cols), 1fr);
  gap: 10px;
  width: min(94vw, 90vh, 560px);
}

.card {
  aspect-ratio: 1 / 1;
  perspective: 700px;
  cursor: pointer;
}
.card-inner {
  position: relative; width: 100%; height: 100%;
  transform-style: preserve-3d;
  transition: transform 0.4s cubic-bezier(.4,.2,.2,1);
}
.card.flipped .card-inner, .card.matched .card-inner { transform: rotateY(180deg); }

.face {
  position: absolute; inset: 0;
  -webkit-backface-visibility: hidden; backface-visibility: hidden;
  border-radius: 16px;
  display: flex; align-items: center; justify-content: center;
  box-shadow: var(--shadow);
}
.face.back {
  background: linear-gradient(150deg, #ff9a76, #ff7a59);
  color: #fff; font-size: clamp(22px, 7vw, 40px);
}
.face.back::after { content: "?"; font-weight: 900; opacity: 0.92; }
.face.front {
  background: #fff; transform: rotateY(180deg);
  border: 3px solid #ffe3c9;
}
.face.front img { width: 72%; height: 72%; pointer-events: none; }
.face.front .glyph {
  font-size: clamp(28px, 9vw, 56px); font-weight: 800; line-height: 1;
  color: #4a3a2a; pointer-events: none;
  font-family: "PingFang SC", "Microsoft YaHei", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

.card.matched .face.front { border-color: #8fd66a; box-shadow: 0 0 0 3px rgba(143,214,106,.6), var(--shadow); }
/* 配对成功：卡片本身消失特效 —— 先欢快一跳，再缩小旋转淡出（空位保留，不打乱布局） */
.card.matched { pointer-events: none; animation: matchVanish 0.7s ease forwards; }
@keyframes matchVanish {
  0%   { transform: scale(1) rotate(0); opacity: 1; }
  22%  { transform: scale(1.16) rotate(0); opacity: 1; }
  40%  { transform: scale(1) rotate(0); opacity: 1; }
  100% { transform: scale(0.05) rotate(180deg); opacity: 0; }
}

/* 浮层 */
.overlay {
  position: fixed; inset: 0; background: rgba(40, 30, 20, 0.45); backdrop-filter: blur(5px);
  display: flex; align-items: center; justify-content: center; z-index: 50; padding: 20px;
}
.overlay[hidden] { display: none; }
.card-panel {
  background: var(--panel); border-radius: 26px; padding: 26px 22px; width: 100%; max-width: 380px;
  box-shadow: var(--shadow); text-align: center;
}
.card-panel h2 { margin: 0 0 18px; font-size: 24px; color: var(--accent); }
.card-panel p { margin: 4px 0 18px; color: #8a7a66; font-weight: 600; }
.win-emoji { font-size: 64px; animation: bob 1.6s ease-in-out infinite; }
@keyframes bob { 0%,100% { transform: translateY(0) rotate(-4deg); } 50% { transform: translateY(-8px) rotate(4deg); } }

.field { text-align: left; margin-bottom: 18px; }
.field > label { display: block; font-size: 14px; color: #8a7a66; font-weight: 700; margin-bottom: 8px; }
.seg { display: flex; gap: 8px; flex-wrap: wrap; }
.seg-btn {
  flex: 1; min-width: 64px; border: 2px solid #ffe3c9; background: #fff8f0; color: var(--text);
  padding: 12px 8px; border-radius: 14px; font-size: 15px; font-weight: 700; cursor: pointer;
}
.seg-btn.active { background: var(--accent); border-color: var(--accent); color: #fff; }

.big-btn {
  width: 100%; border: none; border-radius: 16px; padding: 16px; margin-top: 6px;
  background: var(--accent); color: #fff; font-size: 18px; font-weight: 800; cursor: pointer;
  box-shadow: var(--shadow);
}
.big-btn:active { transform: scale(0.97); background: var(--accent-2); }
.big-btn.ghost { background: #fff; color: var(--accent); border: 2px solid #ffd9c9; box-shadow: none; margin-top: 10px; }
