:root {
  --bg: #f7efe4;
  --ink: #3f3a35;
  --muted: #8d8378;
  --accent: #e98a63;
  --accent-soft: #f6c5ab;
  --card: #fffdf9;
  --line: #e9dcc9;
  --shadow: 0 10px 30px rgba(112, 87, 58, 0.10);
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  color: var(--ink);
  background:
    radial-gradient(circle at 10% 4%, #ffe8d4 0, rgba(255, 232, 212, 0) 40%),
    radial-gradient(circle at 92% 0%, #e6edf1 0, rgba(230, 237, 241, 0) 36%),
    var(--bg);
  overflow: hidden;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.app {
  display: flex;
  flex-direction: column;
  gap: 12px;
  width: 100%;
  max-width: 1440px;
  height: 100dvh;
  margin: 0 auto;
  padding: 16px clamp(14px, 2.6vw, 34px) 18px;
}

/* ---------- 顶部 ---------- */
.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
}

.brand h1 {
  margin: 0;
  font-size: clamp(20px, 2.2vw, 28px);
  font-weight: 700;
  letter-spacing: 0.07em;
}

.brand p {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 13px;
  letter-spacing: 0.02em;
}

.top-actions {
  display: flex;
  flex: 0 0 auto;
  align-items: center;
  gap: 10px;
  margin-left: auto;
}

.level-nav {
  display: flex;
  flex: 0 0 auto;
  align-items: center;
  gap: 2px;
  padding: 4px;
  background: rgba(255, 250, 242, 0.86);
  border: 1px solid var(--line);
  border-radius: 999px;
  box-shadow: 0 6px 18px rgba(112, 87, 58, 0.06);
}

.music-btn {
  position: relative;
  display: grid;
  flex: 0 0 auto;
  place-items: center;
  width: 42px;
  height: 42px;
  padding: 0 0 1px;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: rgba(255, 250, 242, 0.86);
  color: var(--ink);
  font: inherit;
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
  box-shadow: 0 6px 18px rgba(112, 87, 58, 0.06);
  transition: background 0.18s, color 0.18s, transform 0.18s;
}

.music-btn:hover {
  background: #fff6e9;
}

.music-btn:active {
  transform: scale(0.94);
}

.music-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.music-btn.off {
  color: var(--muted);
}

.music-btn.off::after {
  content: "";
  position: absolute;
  width: 22px;
  height: 2px;
  border-radius: 2px;
  background: currentColor;
  transform: rotate(-45deg);
}

.level-arrow {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  padding: 0 0 2px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
  transition: background 0.18s, color 0.18s;
}

.level-arrow:hover:not(:disabled) {
  background: #fff6e9;
  color: var(--ink);
}

.level-arrow:disabled {
  opacity: 0.28;
  cursor: default;
}

.level-current {
  min-width: 94px;
  padding: 7px 12px;
  border: 0;
  border-radius: 999px;
  background: var(--ink);
  color: #fffdf9;
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.04em;
  font-variant-numeric: tabular-nums;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(63, 58, 53, 0.22);
  transition: transform 0.15s, box-shadow 0.15s;
}

.level-current:hover {
  transform: translateY(-1px);
  box-shadow: 0 6px 16px rgba(63, 58, 53, 0.26);
}

/* ---------- 状态栏 ---------- */
.hud {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 14px;
}

.stat {
  display: flex;
  align-items: baseline;
  gap: 5px;
  color: var(--muted);
  font-size: 13px;
  white-space: nowrap;
}

.stat b {
  color: var(--ink);
  font-size: 17px;
  font-variant-numeric: tabular-nums;
}

.progress {
  flex: 1 1 90px;
  min-width: 80px;
  height: 8px;
  overflow: hidden;
  border-radius: 999px;
  background: #e8dcc9;
  box-shadow: inset 0 1px 2px rgba(112, 87, 58, 0.12);
}

#progressFill {
  width: 100%;
  height: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, #f3b28f, var(--accent));
  transition: width 0.25s linear, background 0.3s;
}

.progress.low #progressFill {
  background: linear-gradient(90deg, #ef9a83, #d9543f);
}

.time-stat.low b {
  color: #c94f3d;
}

.actions {
  display: flex;
  gap: 8px;
}

.tool-btn {
  padding: 8px 14px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: rgba(255, 253, 249, 0.9);
  color: var(--ink);
  font: inherit;
  font-size: 13px;
  cursor: pointer;
  transition: transform 0.15s, box-shadow 0.15s, opacity 0.15s;
}

.tool-btn span {
  display: inline-block;
  min-width: 1em;
  margin-left: 2px;
  color: var(--accent);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.tool-btn:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: 0 5px 14px rgba(112, 87, 58, 0.12);
}

.tool-btn:disabled {
  opacity: 0.42;
  cursor: default;
}

.tool-btn.ghost {
  background: transparent;
}

/* ---------- 棋盘 ---------- */
.board-area {
  --tile: 64px;
  --gap: 6px;
  --pad: 28px;
  position: relative;
  display: flex;
  flex: 1;
  align-items: center;
  justify-content: center;
  min-width: 0;
  min-height: 0;
  padding: var(--pad);
  overflow: auto;
}

.board {
  display: grid;
  grid-template-columns: repeat(var(--cols, 8), var(--tile));
  grid-template-rows: repeat(var(--rows, 6), var(--tile));
  gap: var(--gap);
  align-content: center;
  justify-content: center;
  margin: auto;
}

.tile {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  width: var(--tile);
  height: var(--tile);
  padding: 5%;
  border: 1px solid #ecdfcc;
  border-radius: 22%;
  background: var(--tile-bg, linear-gradient(155deg, #fffefb 0%, #fdf5e8 100%));
  box-shadow:
    0 3px 0 rgba(120, 95, 65, 0.10),
    0 6px 14px rgba(120, 95, 65, 0.08);
  cursor: pointer;
  transition: transform 0.16s ease, box-shadow 0.16s ease, border-color 0.16s ease, opacity 0.25s ease;
  touch-action: manipulation;
}

.tile img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  pointer-events: none;
  filter: drop-shadow(0 2px 2px rgba(90, 70, 50, 0.10));
}

.tile:hover {
  transform: translateY(-2px);
  border-color: #e2cfb4;
  box-shadow:
    0 5px 0 rgba(120, 95, 65, 0.09),
    0 10px 18px rgba(120, 95, 65, 0.12);
}

.tile:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.tile.selected {
  z-index: 3;
  border-color: var(--accent);
  transform: translateY(-3px) scale(1.04);
  box-shadow:
    0 0 0 3px rgba(233, 138, 99, 0.22),
    0 10px 20px rgba(233, 138, 99, 0.20);
}

.tile.hint {
  animation: hintPulse 0.55s ease 2;
}

.tile.wrong {
  animation: shake 0.3s ease;
}

.tile.matched {
  pointer-events: none;
  animation: pop 0.28s ease forwards;
}

.tile.empty {
  visibility: hidden;
  pointer-events: none;
}

@keyframes pop {
  0% { transform: scale(1); opacity: 1; }
  45% { transform: scale(1.12); opacity: 1; }
  100% { transform: scale(0.78); opacity: 0; }
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-4px); }
  40% { transform: translateX(4px); }
  60% { transform: translateX(-3px); }
  80% { transform: translateX(3px); }
}

@keyframes hintPulse {
  0%, 100% { box-shadow: 0 3px 0 rgba(120, 95, 65, 0.10), 0 0 0 0 rgba(233, 138, 99, 0); }
  50% { box-shadow: 0 3px 0 rgba(120, 95, 65, 0.10), 0 0 0 5px rgba(233, 138, 99, 0.35); }
}

/* ---------- 连线 ---------- */
.path-layer {
  position: absolute;
  inset: 0;
  z-index: 4;
  width: 100%;
  height: 100%;
  pointer-events: none;
  overflow: visible;
}

.path-halo {
  fill: none;
  stroke: rgba(255, 253, 248, 0.88);
  stroke-width: 10;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.path-main {
  fill: none;
  stroke: var(--accent);
  stroke-width: 5;
  stroke-linecap: round;
  stroke-linejoin: round;
  filter: drop-shadow(0 2px 3px rgba(233, 138, 99, 0.35));
}

/* ---------- 加载 / 提示 ---------- */
.loading {
  position: absolute;
  inset: 0;
  z-index: 6;
  display: grid;
  place-items: center;
  background: rgba(247, 239, 228, 0.9);
  color: var(--muted);
  font-size: 15px;
  letter-spacing: 0.08em;
  transition: opacity 0.2s;
}

.loading.hidden {
  display: none;
}

/* ---------- 无解自动重排 ---------- */
.shuffle-notice {
  position: absolute;
  inset: 0;
  z-index: 7;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(247, 239, 228, 0.62);
  -webkit-backdrop-filter: blur(3px);
  backdrop-filter: blur(3px);
  animation: noticeFade 0.2s ease;
}

.shuffle-notice[hidden] {
  display: none;
}

.shuffle-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
  padding: 22px 32px 20px;
  border: 1px solid rgba(255, 255, 255, 0.78);
  border-radius: 22px;
  background: var(--card);
  box-shadow: 0 18px 50px rgba(63, 48, 33, 0.22);
  text-align: center;
  animation: rise 0.25s ease;
}

.shuffle-mark {
  display: grid;
  place-items: center;
  width: 46px;
  height: 46px;
  margin-bottom: 5px;
  border-radius: 50%;
  background: #fff3e3;
  color: var(--accent);
  font-size: 26px;
  line-height: 1;
  animation: shuffleSpin 0.85s linear infinite;
}

.shuffle-card b {
  font-size: 18px;
  letter-spacing: 0.04em;
}

.shuffle-card span {
  color: var(--muted);
  font-size: 14px;
  letter-spacing: 0.03em;
}

.board.shuffling {
  pointer-events: none;
}

.board.shuffling .tile {
  animation: tileShuffle 0.9s ease-in-out;
}

@keyframes tileShuffle {
  0% { transform: scale(1) rotate(0); opacity: 1; }
  35% { transform: scale(0.82) rotate(-5deg); opacity: 0.12; }
  55% { transform: scale(0.82) rotate(5deg); opacity: 0.12; }
  100% { transform: scale(1) rotate(0); opacity: 1; }
}

@keyframes shuffleSpin {
  to { transform: rotate(360deg); }
}

@keyframes noticeFade {
  from { opacity: 0; }
  to { opacity: 1; }
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 30px;
  z-index: 60;
  max-width: calc(100vw - 32px);
  padding: 10px 18px;
  border-radius: 999px;
  background: rgba(63, 58, 53, 0.94);
  color: #fffdf9;
  font-size: 14px;
  white-space: nowrap;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 14px);
  transition: opacity 0.22s, transform 0.22s;
}

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

/* ---------- 通关 ---------- */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(63, 58, 53, 0.32);
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
}

.overlay[hidden] {
  display: none;
}

.win-card {
  width: min(380px, 100%);
  padding: 26px 30px 28px;
  border: 1px solid rgba(255, 255, 255, 0.7);
  border-radius: 28px;
  background: var(--card);
  box-shadow: 0 24px 70px rgba(63, 48, 33, 0.28);
  text-align: center;
  animation: rise 0.3s ease;
}

.win-card img {
  width: 112px;
  height: 112px;
  border-radius: 26px;
  background: #fff;
  object-fit: contain;
  filter: drop-shadow(0 8px 14px rgba(90, 70, 50, 0.18));
}

.win-card h2 {
  margin: 6px 0 8px;
  font-size: 23px;
  letter-spacing: 0.06em;
}

.win-text {
  margin: 0;
  color: var(--muted);
  font-size: 15px;
}

.win-best {
  min-height: 20px;
  margin: 8px 0 0;
  color: var(--accent);
  font-size: 13px;
  font-weight: 600;
}

.win-actions {
  display: flex;
  gap: 10px;
  margin-top: 22px;
}

.win-actions button {
  flex: 1;
  padding: 11px 16px;
  border-radius: 999px;
  border: 1px solid transparent;
  font: inherit;
  font-size: 14px;
  cursor: pointer;
  transition: transform 0.15s, box-shadow 0.15s;
}

.win-actions button:hover {
  transform: translateY(-1px);
}

.win-actions .primary {
  background: var(--ink);
  color: #fffdf9;
  box-shadow: 0 6px 16px rgba(63, 58, 53, 0.24);
}

.win-actions .secondary {
  border-color: var(--line);
  background: transparent;
  color: var(--ink);
}

.win-card.fail h2 {
  color: #c0553f;
}

@keyframes rise {
  from { opacity: 0; transform: translateY(14px) scale(0.98); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

/* ---------- 关卡地图 ---------- */
.map-card {
  display: flex;
  flex-direction: column;
  width: min(560px, 100%);
  max-height: min(700px, calc(100dvh - 36px));
  padding: 18px 20px 16px;
  border: 1px solid rgba(255, 255, 255, 0.7);
  border-radius: 26px;
  background: var(--card);
  box-shadow: 0 24px 70px rgba(63, 48, 33, 0.28);
  animation: rise 0.3s ease;
}

.map-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.map-head h2 {
  flex: 1;
  margin: 0;
  font-size: 18px;
  font-weight: 700;
  letter-spacing: 0.05em;
  text-align: center;
  font-variant-numeric: tabular-nums;
}

.map-grid {
  display: grid;
  grid-template-columns: repeat(10, minmax(0, 1fr));
  gap: 6px;
  margin: 14px 0;
  padding: 3px;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.map-btn {
  position: relative;
  aspect-ratio: 1;
  min-width: 0;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fffaf2;
  color: var(--ink);
  font: inherit;
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  cursor: pointer;
  transition: background 0.15s, transform 0.15s, border-color 0.15s;
}

.map-btn:hover:not(:disabled) {
  transform: translateY(-1px);
  border-color: #e2cfb4;
  background: #fff3e3;
}

.map-btn.active {
  border-color: var(--ink);
  background: var(--ink);
  color: #fffdf9;
  box-shadow: 0 4px 12px rgba(63, 58, 53, 0.22);
}

.map-btn.cleared::after {
  content: '';
  position: absolute;
  bottom: 4px;
  left: 50%;
  width: 4px;
  height: 4px;
  margin-left: -2px;
  border-radius: 50%;
  background: var(--accent);
}

.map-btn.active.cleared::after {
  background: var(--accent-soft);
}

.map-btn:disabled {
  opacity: 0.3;
  cursor: default;
}

.map-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  color: var(--muted);
  font-size: 13px;
}

.map-foot b {
  color: var(--ink);
  font-size: 15px;
  font-variant-numeric: tabular-nums;
}

.map-foot .primary {
  padding: 9px 24px;
  border: 0;
  border-radius: 999px;
  background: var(--ink);
  color: #fffdf9;
  font: inherit;
  font-size: 13px;
  cursor: pointer;
  box-shadow: 0 6px 16px rgba(63, 58, 53, 0.2);
}

/* ---------- 小屏 ---------- */
@media (max-width: 680px) {
  .app {
    gap: 9px;
    padding:
      max(12px, env(safe-area-inset-top))
      max(10px, env(safe-area-inset-right))
      max(14px, env(safe-area-inset-bottom))
      max(10px, env(safe-area-inset-left));
  }

  .brand p {
    display: none;
  }

  .top-actions {
    gap: 8px;
    margin-left: auto;
  }

  .level-nav {
    padding: 3px;
  }

  .music-btn {
    width: 38px;
    height: 38px;
    font-size: 18px;
  }

  .level-arrow {
    width: 30px;
    height: 30px;
    font-size: 18px;
  }

  .level-current {
    min-width: 86px;
    padding: 6px 10px;
    font-size: 12px;
  }

  .map-card {
    padding: 16px 14px 14px;
  }

  .map-head h2 {
    font-size: 16px;
  }

  .map-grid {
    gap: 4px;
    margin: 12px 0;
  }

  .map-btn {
    border-radius: 8px;
    font-size: 11px;
  }

  .map-btn.cleared::after {
    bottom: 3px;
    width: 3px;
    height: 3px;
    margin-left: -1.5px;
  }

  .hud {
    gap: 10px;
  }

  .stat b {
    font-size: 15px;
  }

  .actions {
    width: 100%;
  }

  .tool-btn {
    flex: 1;
    text-align: center;
  }

  .win-card {
    padding: 22px 22px 24px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
