:root {
  --ink: #3b2a26;
  --ink-soft: #7a5d50;
  --paper: #fffaf0;
  --paper-deep: #f6ead8;
  --peach: #f4a261;
  --coral: #e76f51;
  --sun: #ffd166;
  --shadow: #d8c2a8;
  --tile: clamp(40px, 13vw, 56px);
  --gap: clamp(5px, 1.1vw, 9px);
  --font: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans SC",
    system-ui, -apple-system, sans-serif;
}

* {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100dvh;
  overflow-x: hidden;
  color: var(--ink);
  font-family: var(--font);
  background:
    radial-gradient(circle at 12% 8%, rgba(244, 162, 97, 0.2), transparent 26rem),
    radial-gradient(circle at 88% 92%, rgba(122, 199, 79, 0.14), transparent 28rem),
    var(--paper-deep);
}

body::before {
  position: fixed;
  inset: 0;
  z-index: -1;
  content: "";
  pointer-events: none;
  background-image: radial-gradient(rgba(59, 42, 38, 0.07) 1px, transparent 1px);
  background-size: 22px 22px;
}

button {
  font-family: inherit;
}

.page {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: min(100%, 640px);
  padding: max(16px, env(safe-area-inset-top)) 18px
    max(18px, env(safe-area-inset-bottom));
  margin: 0 auto;
}

/* ---------- 顶部 ---------- */

.hero {
  display: flex;
  align-items: center;
  gap: 14px;
  width: 100%;
  margin: 4px 0 14px;
}

.hero-mark {
  display: grid;
  flex: 0 0 auto;
  width: 66px;
  height: 66px;
  place-items: center;
  background: #fff7e8;
  border: 3px solid var(--ink);
  border-radius: 50%;
  box-shadow: 0 4px 0 var(--shadow);
  transform: rotate(-4deg);
}

.hero-mark img {
  width: 82%;
  height: 82%;
}

.hero-copy {
  min-width: 0;
}

.eyebrow {
  margin: 0 0 1px;
  color: var(--ink-soft);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.12em;
}

h1 {
  margin: 0;
  font-size: clamp(28px, 6.4vw, 42px);
  line-height: 1.05;
  letter-spacing: -0.02em;
}

.subtitle {
  margin: 5px 0 0;
  color: var(--ink-soft);
  font-size: clamp(13px, 2.8vw, 15px);
}

.game {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
}

/* ---------- 状态栏 ---------- */

.hud {
  display: grid;
  grid-template-columns: auto auto auto 1fr;
  gap: 12px;
  align-items: center;
  width: 100%;
  padding: 9px 14px;
  background: rgba(255, 250, 240, 0.82);
  border: 2.5px solid var(--ink);
  border-radius: 18px;
  box-shadow: 0 4px 0 var(--shadow);
}

.hud-level {
  display: flex;
  flex-direction: column;
  gap: 0;
  align-items: flex-start;
  min-width: 62px;
  padding: 3px 9px 4px;
  color: var(--ink);
  font: inherit;
  text-align: left;
  appearance: none;
  cursor: pointer;
  background: #fffdf7;
  border: 2px solid var(--ink);
  border-radius: 12px;
  box-shadow: 0 2px 0 var(--shadow);
  touch-action: manipulation;
}

.hud-level:active {
  box-shadow: 0 0 0 var(--shadow);
  transform: translateY(2px);
}

.hud-level:focus-visible {
  outline: 4px solid #2f80ed;
  outline-offset: 2px;
}

.hud-level-badge {
  color: var(--ink-soft);
  font-size: 11px;
  font-weight: 800;
  line-height: 1.2;
}

.hud-level-name {
  font-size: 15px;
  font-weight: 800;
  line-height: 1.3;
}

.hud-item {
  display: flex;
  gap: 4px;
  align-items: baseline;
  white-space: nowrap;
}

.hud-label {
  color: var(--ink-soft);
  font-size: 13px;
  font-weight: 700;
}

.hud-value {
  font-size: 22px;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.03em;
}

.hud-unit {
  color: var(--ink-soft);
  font-size: 12px;
  font-weight: 700;
}

.hud-progress {
  min-width: 60px;
}

.progress-track {
  height: 10px;
  overflow: hidden;
  background: #eadbc4;
  border: 2px solid var(--ink);
  border-radius: 999px;
}

.progress-bar {
  width: 0;
  height: 100%;
  background: repeating-linear-gradient(
    45deg,
    var(--peach) 0 8px,
    #f7b97f 8px 16px
  );
  transition: width 0.3s ease;
}

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

.board-card {
  display: flex;
  justify-content: center;
  width: 100%;
  padding: clamp(9px, 2vw, 16px);
  margin-top: 14px;
  background: rgba(255, 250, 240, 0.92);
  border: 3px solid var(--ink);
  border-radius: 26px;
  box-shadow:
    0 7px 0 var(--shadow),
    0 18px 30px rgba(59, 42, 38, 0.1);
}

.board-stage {
  position: relative;
  flex: 0 0 auto;
}

.board {
  display: grid;
  grid-template-columns: repeat(6, var(--tile));
  grid-auto-rows: var(--tile);
  gap: var(--gap);
}

.tile {
  position: relative;
  display: grid;
  width: var(--tile);
  height: var(--tile);
  margin: 0;
  padding: 6%;
  appearance: none;
  cursor: pointer;
  background: #fffdf7;
  border: 3px solid var(--ink);
  border-radius: 18px;
  box-shadow: 0 3px 0 var(--shadow);
  place-items: center;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  transition:
    transform 0.12s ease,
    box-shadow 0.12s ease,
    background 0.12s ease,
    opacity 0.22s ease;
}

.tile img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  pointer-events: none;
  user-select: none;
  -webkit-user-drag: none;
}

.tile:hover {
  transform: translateY(-2px);
}

.tile:focus-visible {
  outline: 4px solid #2f80ed;
  outline-offset: 3px;
}

.tile.is-selected {
  z-index: 2;
  background: #ffe9a8;
  border-color: var(--coral);
  box-shadow:
    0 0 0 4px rgba(231, 111, 81, 0.22),
    0 3px 0 #d59a72;
  transform: translateY(-3px) scale(1.04);
}

.tile.is-hint {
  animation: hintPulse 0.65s ease-in-out 0s 2;
}

.tile.is-wrong {
  animation: wrongShake 0.35s ease;
}

.tile.is-removed {
  pointer-events: none;
  opacity: 0;
  box-shadow: none;
  transform: scale(0.35) rotate(9deg);
}

.tile.is-blank {
  visibility: hidden;
  pointer-events: none;
  border: 0;
  background: transparent;
  box-shadow: none;
}

.path-layer {
  position: absolute;
  inset: 0;
  z-index: 5;
  width: 100%;
  height: 100%;
  overflow: visible;
  pointer-events: none;
}

.path-halo {
  fill: none;
  stroke: var(--paper);
  stroke-width: 12;
  stroke-linecap: round;
  stroke-linejoin: round;
  opacity: 0.95;
}

.path-line {
  fill: none;
  stroke: var(--coral);
  stroke-width: 7;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-dasharray: 1400;
  stroke-dashoffset: 1400;
  filter: drop-shadow(0 2px 2px rgba(59, 42, 38, 0.25));
  animation: drawPath 0.32s ease-out forwards;
}

/* ---------- 操作按钮 ---------- */

.controls {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: clamp(7px, 1.8vw, 12px);
  width: 100%;
  margin-top: 14px;
}

.control-btn {
  display: flex;
  flex-direction: column;
  gap: 2px;
  align-items: center;
  justify-content: center;
  min-height: 58px;
  padding: 6px 4px;
  color: var(--ink);
  font-size: 15px;
  font-weight: 700;
  appearance: none;
  cursor: pointer;
  background: var(--paper);
  border: 2.5px solid var(--ink);
  border-radius: 16px;
  box-shadow: 0 4px 0 var(--shadow);
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  transition:
    transform 0.1s ease,
    box-shadow 0.1s ease,
    opacity 0.2s ease;
}

.control-btn:active {
  box-shadow: 0 1px 0 var(--shadow);
  transform: translateY(3px);
}

.control-btn:focus-visible {
  outline: 4px solid #2f80ed;
  outline-offset: 3px;
}

.control-btn.is-off {
  opacity: 0.55;
}

.control-icon {
  font-size: 20px;
  line-height: 1;
}

.tip {
  margin: 12px 0 0;
  color: var(--ink-soft);
  font-size: 13px;
  text-align: center;
}

.footer {
  margin-top: 16px;
  color: #9b7f70;
  font-size: 11px;
  letter-spacing: 0.02em;
  text-align: center;
}

/* ---------- 提示条 ---------- */

.toast {
  position: fixed;
  bottom: max(22px, env(safe-area-inset-bottom));
  left: 50%;
  z-index: 80;
  max-width: calc(100vw - 32px);
  padding: 11px 18px;
  color: #fff8ee;
  font-size: 14px;
  font-weight: 600;
  text-align: center;
  pointer-events: none;
  background: var(--ink);
  border-radius: 999px;
  opacity: 0;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.18);
  transform: translate(-50%, 14px);
  transition:
    opacity 0.18s ease,
    transform 0.18s ease;
}

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

/* ---------- 胜利弹层 ---------- */

.win-overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: grid;
  padding: 20px;
  background: rgba(59, 42, 38, 0.5);
  backdrop-filter: blur(5px);
  place-items: center;
}

.win-overlay[hidden] {
  display: none;
}

.win-card {
  width: min(100%, 360px);
  padding: 24px 24px 26px;
  text-align: center;
  background: var(--paper);
  border: 3px solid var(--ink);
  border-radius: 28px;
  box-shadow:
    0 10px 0 rgba(59, 42, 38, 0.25),
    0 30px 60px rgba(0, 0, 0, 0.2);
  animation: popIn 0.3s cubic-bezier(0.2, 0.9, 0.3, 1.2);
}

.win-animals {
  display: flex;
  gap: 4px;
  justify-content: center;
  margin-bottom: 8px;
}

.win-animals img {
  width: 64px;
  height: 64px;
}

.win-animals img:nth-child(1) {
  transform: rotate(-8deg);
}

.win-animals img:nth-child(2) {
  transform: translateY(-5px) scale(1.06);
}

.win-animals img:nth-child(3) {
  transform: rotate(8deg);
}

.win-card h2 {
  margin: 6px 0 8px;
  font-size: 28px;
}

.win-card p {
  margin: 0 0 20px;
  color: var(--ink-soft);
  font-size: 16px;
}

.primary-btn {
  width: 100%;
  min-height: 56px;
  color: var(--ink);
  font-size: 18px;
  font-weight: 800;
  cursor: pointer;
  background: var(--peach);
  border: 3px solid var(--ink);
  border-radius: 16px;
  box-shadow: 0 4px 0 #c9834a;
  touch-action: manipulation;
}

.primary-btn:active {
  box-shadow: 0 1px 0 #c9834a;
  transform: translateY(3px);
}

.primary-btn:focus-visible {
  outline: 4px solid #2f80ed;
  outline-offset: 3px;
}

.win-actions {
  display: grid;
  gap: 8px;
}

.secondary-btn {
  width: 100%;
  min-height: 50px;
  color: var(--ink);
  font-size: 16px;
  font-weight: 800;
  cursor: pointer;
  background: #fffdf7;
  border: 2.5px solid var(--ink);
  border-radius: 16px;
  box-shadow: 0 3px 0 var(--shadow);
  touch-action: manipulation;
}

.secondary-btn:active {
  box-shadow: 0 0 0 var(--shadow);
  transform: translateY(2px);
}

.secondary-btn:focus-visible {
  outline: 4px solid #2f80ed;
  outline-offset: 3px;
}

.text-btn {
  margin-top: 12px;
  padding: 4px 8px;
  color: var(--ink-soft);
  font: inherit;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  background: transparent;
  border: 0;
  text-decoration: underline;
  text-underline-offset: 3px;
}

.text-btn:focus-visible {
  outline: 3px solid #2f80ed;
  outline-offset: 3px;
}

.level-panel {
  position: relative;
  width: min(100%, 540px);
  max-height: calc(100dvh - 32px);
  overflow: auto;
}

.level-close {
  position: absolute;
  top: 10px;
  right: 12px;
  display: grid;
  width: 34px;
  height: 34px;
  padding: 0;
  color: var(--ink-soft);
  font: inherit;
  font-size: 24px;
  line-height: 1;
  cursor: pointer;
  background: transparent;
  border: 0;
  border-radius: 50%;
  place-items: center;
}

.level-close:hover {
  background: #f3e4d2;
}

.level-close:focus-visible {
  outline: 3px solid #2f80ed;
  outline-offset: 2px;
}

.level-panel h2 {
  margin-top: 0;
}

.level-progress {
  margin: -4px 0 16px;
  color: var(--ink-soft);
  font-size: 15px;
}

.level-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin-bottom: 16px;
}

.level-item {
  display: flex;
  flex-direction: column;
  gap: 1px;
  align-items: flex-start;
  min-height: 82px;
  padding: 9px 10px;
  color: var(--ink);
  font: inherit;
  text-align: left;
  appearance: none;
  cursor: pointer;
  background: #fffdf7;
  border: 2.5px solid var(--ink);
  border-radius: 14px;
  box-shadow: 0 3px 0 var(--shadow);
  touch-action: manipulation;
}

.level-item:active {
  box-shadow: 0 0 0 var(--shadow);
  transform: translateY(2px);
}

.level-item:focus-visible {
  outline: 4px solid #2f80ed;
  outline-offset: 2px;
}

.level-item.is-current {
  background: #fff0d2;
  border-color: var(--coral);
}

.level-item.is-done {
  background: #eef8e8;
}

.level-item.is-locked {
  cursor: not-allowed;
  opacity: 0.48;
  box-shadow: none;
}

.level-number {
  color: var(--ink-soft);
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.04em;
}

.level-name {
  font-size: 16px;
  font-weight: 800;
  line-height: 1.25;
}

.level-meta {
  color: var(--ink-soft);
  font-size: 11px;
  line-height: 1.3;
}

.level-state {
  margin-top: auto;
  color: var(--ink-soft);
  font-size: 10px;
  font-weight: 700;
}

/* ---------- 动画 ---------- */

@keyframes hintPulse {
  0%,
  100% {
    box-shadow: 0 3px 0 var(--shadow);
    transform: scale(1);
  }

  50% {
    box-shadow:
      0 0 0 6px rgba(255, 209, 102, 0.55),
      0 3px 0 var(--shadow);
    transform: scale(1.08);
  }
}

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

  25% {
    transform: translateX(-3px) rotate(-2deg);
  }

  75% {
    transform: translateX(3px) rotate(2deg);
  }
}

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

@keyframes popIn {
  from {
    opacity: 0;
    transform: translateY(10px) scale(0.9);
  }

  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

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

@media (max-width: 520px) {
  .page {
    padding-right: 14px;
    padding-left: 14px;
  }

  .hero {
    gap: 10px;
    margin-bottom: 10px;
  }

  .hero-mark {
    width: 54px;
    height: 54px;
  }

  .eyebrow {
    font-size: 10px;
  }

  .subtitle {
    font-size: 12px;
  }

  .hud {
    gap: 8px;
    padding: 8px 9px;
  }

  .hud-level {
    min-width: 54px;
    padding: 2px 6px 3px;
  }

  .hud-level-badge {
    font-size: 10px;
  }

  .hud-level-name {
    font-size: 13px;
  }

  .hud-value {
    font-size: 20px;
  }

  .hud-progress {
    min-width: 40px;
  }

  .board-card {
    padding: 9px;
    margin-top: 10px;
    border-radius: 22px;
  }

  .controls {
    gap: 7px;
    margin-top: 10px;
  }

  .control-btn {
    min-height: 54px;
    font-size: 13px;
    border-radius: 14px;
  }

  .control-icon {
    font-size: 18px;
  }

  .tip {
    margin-top: 9px;
    font-size: 12px;
  }
}

@media (max-height: 720px) and (max-width: 520px) {
  .hero {
    margin: 0 0 7px;
  }

  .hero-mark {
    width: 46px;
    height: 46px;
    border-width: 2.5px;
  }

  .eyebrow,
  .subtitle,
  .tip,
  .footer {
    display: none;
  }

  h1 {
    font-size: 26px;
  }

  .hud {
    padding: 6px 10px;
  }

  .board-card {
    padding: 7px;
    margin-top: 8px;
  }

  .controls {
    margin-top: 8px;
  }

  .control-btn {
    min-height: 50px;
  }
}

@media (max-height: 860px) and (min-width: 521px) {
  .tip,
  .footer {
    display: none;
  }

  .hero {
    margin-bottom: 10px;
  }

  .board-card {
    padding: 12px;
    margin-top: 10px;
  }

  .controls {
    margin-top: 10px;
  }
}

@media (max-width: 340px) {
  :root {
    --tile: 40px;
    --gap: 4px;
  }

  .page {
    padding-left: 10px;
    padding-right: 10px;
  }

  .board-card {
    padding: 7px;
  }

  .hud {
    gap: 8px;
    padding: 6px 8px;
  }

  .hud-value {
    font-size: 18px;
  }

  .controls {
    gap: 6px;
  }

  .control-btn {
    min-height: 48px;
    font-size: 12px;
  }
}

@media (max-width: 360px) {
  .level-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  .level-panel {
    padding-right: 16px;
    padding-left: 16px;
  }
}

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