:root {
  --pink: #ffb6d9;
  --purple: #c8a8e9;
  --sky: #a0e1ff;
  --yellow: #ffe5a0;
  --mint: #b8e9c9;
  --bg-start: #fff0f8;
  --bg-end: #e8f4ff;
  --text: #3d3556;
  --text-soft: #7d7398;
  --shadow: 0 6px 0 rgba(0, 0, 0, 0.12);
  --shadow-hover: 0 4px 0 rgba(0, 0, 0, 0.12);
  --board-bg: #fffaf2;
  --tray-bg: #fffefb;
}

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

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", "Helvetica Neue", Arial, sans-serif;
  color: var(--text);
  background: linear-gradient(135deg, var(--bg-start), var(--bg-end));
  overflow: hidden;
  touch-action: none;
}

/* ===== 屏幕切换 ===== */
.screen {
  position: fixed;
  inset: 0;
  display: none;
  flex-direction: column;
  overflow: auto;
}
.screen.active {
  display: flex;
}

/* ===== 关卡选择 ===== */
#level-select {
  align-items: center;
  padding: 24px 16px 40px;
  overflow-y: auto;
}
.header {
  text-align: center;
  margin-bottom: 24px;
}
.header h1 {
  margin: 0 0 8px;
  font-size: 44px;
  letter-spacing: 6px;
  color: #d35ea0;
  text-shadow: 3px 3px 0 #fff, 6px 6px 0 rgba(211, 94, 160, 0.15);
}
.subtitle {
  margin: 0;
  font-size: 18px;
  color: var(--text-soft);
}

.level-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 18px;
  width: 100%;
  max-width: 880px;
  padding: 8px;
}

.level-card {
  background: white;
  border-radius: 24px;
  padding: 14px;
  text-align: center;
  cursor: pointer;
  box-shadow: var(--shadow);
  transition: transform 0.15s ease, box-shadow 0.15s ease;
  position: relative;
  border: 4px solid transparent;
}
.level-card:hover:not(.locked) {
  transform: translateY(2px);
  box-shadow: var(--shadow-hover);
}
.level-card:active:not(.locked) {
  transform: translateY(6px);
  box-shadow: 0 0 0 rgba(0, 0, 0, 0);
}
.level-card.locked {
  cursor: not-allowed;
  filter: grayscale(0.7) brightness(0.95);
  opacity: 0.7;
}
.level-card.completed {
  border-color: #ffd24d;
}
.level-thumb {
  width: 100%;
  aspect-ratio: 1 / 1;
  border-radius: 16px;
  background: #f5edff;
  background-size: cover;
  background-position: center;
  margin-bottom: 8px;
}
.level-name {
  font-size: 16px;
  font-weight: 700;
}
.level-meta {
  font-size: 13px;
  color: var(--text-soft);
  margin-top: 2px;
}
.level-stars {
  position: absolute;
  top: 8px;
  right: 12px;
  font-size: 22px;
  filter: drop-shadow(0 2px 0 rgba(0, 0, 0, 0.15));
}
.level-lock {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 56px;
  color: rgba(0, 0, 0, 0.35);
  pointer-events: none;
}

.text-btn {
  margin-top: 20px;
  background: transparent;
  border: none;
  color: var(--text-soft);
  font-size: 14px;
  cursor: pointer;
  padding: 8px 16px;
}
.text-btn:hover {
  color: var(--text);
}

/* ===== 游戏页 ===== */
#game {
  padding: 12px;
}

.game-topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 8px 12px;
}
.level-title {
  flex: 1;
  text-align: center;
  font-size: 22px;
  font-weight: 700;
  color: #b04a85;
}
.icon-btn {
  width: 48px;
  height: 48px;
  border: none;
  border-radius: 50%;
  background: white;
  font-size: 22px;
  cursor: pointer;
  box-shadow: var(--shadow);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.1s, box-shadow 0.1s;
}
.icon-btn:active {
  transform: translateY(4px);
  box-shadow: 0 2px 0 rgba(0, 0, 0, 0.12);
}

.game-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 14px;
  align-items: center;
  min-height: 0;
}

.board-wrap {
  position: relative;
  background: var(--board-bg);
  border-radius: 24px;
  padding: 12px;
  box-shadow: inset 0 0 0 4px rgba(255, 182, 217, 0.4), 0 6px 0 rgba(0, 0, 0, 0.08);
}
.board {
  position: relative;
  background-image:
    linear-gradient(rgba(0, 0, 0, 0.06) 1px, transparent 1px),
    linear-gradient(90deg, rgba(0, 0, 0, 0.06) 1px, transparent 1px);
  border-radius: 12px;
  background-color: rgba(255, 255, 255, 0.4);
}

.peek-image {
  position: absolute;
  inset: 12px;
  width: calc(100% - 24px);
  height: calc(100% - 24px);
  object-fit: contain;
  border-radius: 12px;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.2s;
}
.peek-image.show {
  opacity: 0.9;
}

.tray {
  background: var(--tray-bg);
  border-radius: 20px;
  padding: 10px;
  box-shadow: inset 0 0 0 3px rgba(200, 168, 233, 0.35);
  width: 100%;
  max-width: 720px;
  min-height: 120px;
  display: flex;
  flex-wrap: wrap;
  align-content: flex-start;
  gap: 6px;
  position: relative;
}

/* ===== 拼图碎片 ===== */
.piece {
  position: absolute;
  background-repeat: no-repeat;
  background-size: var(--bg-w) var(--bg-h);
  border-radius: 6px;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.18), inset 0 0 0 2px rgba(255, 255, 255, 0.5);
  cursor: grab;
  touch-action: none;
  transition: box-shadow 0.1s;
  will-change: transform, left, top;
}
.piece.dragging {
  cursor: grabbing;
  z-index: 1000;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.25);
}
.piece.placed {
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12), inset 0 0 0 1px rgba(255, 255, 255, 0.4);
}

/* ===== 完成弹层 ===== */
.celebrate {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
  background: rgba(0, 0, 0, 0.35);
  backdrop-filter: blur(4px);
}
.celebrate.hidden {
  display: none;
}
#confetti {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}
.celebrate-card {
  position: relative;
  background: white;
  border-radius: 28px;
  padding: 36px 40px;
  text-align: center;
  box-shadow: 0 10px 0 rgba(0, 0, 0, 0.15);
  animation: card-in 0.5s cubic-bezier(0.34, 1.56, 0.64, 1);
}
@keyframes card-in {
  0% { transform: scale(0.3); opacity: 0; }
  100% { transform: scale(1); opacity: 1; }
}
.celebrate-stars {
  font-size: 48px;
  margin-bottom: 12px;
  filter: drop-shadow(0 4px 0 rgba(255, 180, 0, 0.4));
}
.celebrate-text {
  font-size: 36px;
  font-weight: 800;
  color: #d35ea0;
  letter-spacing: 8px;
  margin-bottom: 24px;
}
.celebrate-buttons {
  display: flex;
  gap: 12px;
  justify-content: center;
}

.big-btn {
  border: none;
  border-radius: 999px;
  padding: 14px 26px;
  font-size: 18px;
  font-weight: 700;
  color: white;
  background: linear-gradient(180deg, #ff9bcc, #d35ea0);
  cursor: pointer;
  box-shadow: 0 5px 0 #a04077;
  transition: transform 0.08s, box-shadow 0.08s;
}
.big-btn:active {
  transform: translateY(4px);
  box-shadow: 0 1px 0 #a04077;
}
.big-btn.secondary {
  background: linear-gradient(180deg, #c8a8e9, #8868c0);
  box-shadow: 0 5px 0 #5e4790;
}
.big-btn.secondary:active {
  box-shadow: 0 1px 0 #5e4790;
}

/* ===== 响应式 ===== */
@media (max-width: 480px) {
  .header h1 { font-size: 32px; letter-spacing: 4px; }
  .level-grid { grid-template-columns: repeat(2, 1fr); gap: 12px; }
  .icon-btn { width: 42px; height: 42px; font-size: 18px; }
  .level-title { font-size: 18px; }
  .celebrate-card { padding: 28px 24px; }
  .celebrate-text { font-size: 28px; }
}
