:root {
  color-scheme: light;
  --ink: #776e65;
  --muted: #8f7f72;
  --paper: #faf8ef;
  --board: #bbada0;
  --line: #a99a8f;
  --accent: #8f7a66;
  --danger: #9f6b52;
  --cell: rgba(238, 228, 218, 0.38);
}

* {
  box-sizing: border-box;
}

body {
  min-height: 100svh;
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: var(--ink);
  background:
    linear-gradient(120deg, rgba(237, 194, 46, 0.16), transparent 42%),
    linear-gradient(230deg, rgba(242, 177, 121, 0.12), transparent 48%),
    var(--paper);
  display: grid;
  place-items: center;
  padding: max(18px, env(safe-area-inset-top)) 14px max(18px, env(safe-area-inset-bottom));
  touch-action: manipulation;
}

.app {
  width: min(94vw, 520px);
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  gap: 14px;
  margin-bottom: 14px;
}

h1 {
  margin: 0;
  font-size: 34px;
  line-height: 1;
  letter-spacing: 0;
}

p {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 15px;
}

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

.scoreBox {
  min-width: 70px;
  padding: 9px 10px;
  border-radius: 8px;
  background: #bbada0;
  color: #fff;
  border: 1px solid rgba(143, 122, 102, 0.45);
  text-align: center;
  box-shadow: 0 5px 14px rgba(65, 42, 19, 0.1);
}

.scoreBox span {
  display: block;
  color: rgba(255, 255, 255, 0.72);
  font-size: 12px;
  font-weight: 800;
  text-transform: uppercase;
}

.scoreBox strong {
  display: block;
  margin-top: 2px;
  font-size: 22px;
}

.boardShell {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  border-radius: 8px;
  padding: 10px;
  background: var(--board);
  border: 2px solid var(--line);
  box-shadow: 0 18px 34px rgba(75, 55, 31, 0.18);
}

.board {
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: 9px;
}

.tile {
  display: grid;
  place-items: center;
  border-radius: 8px;
  background: var(--cell);
  user-select: none;
  overflow: hidden;
}

.piece {
  width: 100%;
  height: 100%;
  display: grid;
  place-items: center;
  border-radius: 8px;
  border: 1px solid rgba(38, 50, 56, 0.08);
  box-shadow: inset 0 -4px 0 rgba(38, 50, 56, 0.08);
  color: #776e65;
  font-size: clamp(26px, 8vw, 42px);
  font-weight: 900;
  line-height: 1;
  transform: scale(1);
  animation: pop 120ms ease-out;
}

.piece span {
  display: block;
}

.level1 { background: #eee4da; }
.level2 { background: #ede0c8; }
.level3 { background: #f2b179; color: #f9f6f2; }
.level4 { background: #f59563; color: #f9f6f2; }
.level5 { background: #f67c5f; color: #f9f6f2; }
.level6 { background: #f65e3b; color: #f9f6f2; }
.level7 { background: #edcf72; color: #f9f6f2; font-size: clamp(22px, 7vw, 38px); }
.level8 { background: #edcc61; color: #f9f6f2; font-size: clamp(22px, 7vw, 38px); }
.level9 { background: #edc850; color: #f9f6f2; font-size: clamp(22px, 7vw, 38px); }
.level10 { background: #edc53f; color: #f9f6f2; font-size: clamp(18px, 6vw, 32px); }
.level11,
.level12,
.level13,
.level14,
.level15 {
  background: #3c3a32;
  color: #f9f6f2;
  font-size: clamp(17px, 5.2vw, 30px);
}

.overlay {
  position: absolute;
  inset: 10px;
  border-radius: 8px;
  background: rgba(250, 248, 239, 0.9);
  display: grid;
  place-content: center;
  gap: 14px;
  text-align: center;
  backdrop-filter: blur(5px);
}

.hidden {
  display: none;
}

.overlay strong {
  font-size: 30px;
}

.controls {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-top: 14px;
}

button {
  border: 0;
  border-radius: 8px;
  padding: 13px 12px;
  background: var(--accent);
  color: white;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}

button:active {
  transform: translateY(1px);
}

#undo {
  background: var(--danger);
}

.beian-footer {
  width: min(500px, calc(100vw - 32px));
  margin: 18px auto 0;
  display: flex;
  flex-wrap: wrap;
  gap: 8px 14px;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  font-size: 12px;
}

.beian-footer a {
  display: inline-flex;
  min-height: 28px;
  gap: 5px;
  align-items: center;
}

.beian-footer img {
  display: block;
  flex: none;
}

@keyframes pop {
  from { transform: scale(0.74); }
  to { transform: scale(1); }
}

@media (max-width: 420px) {
  .topbar {
    align-items: stretch;
    flex-direction: column;
  }

  .scores {
    width: 100%;
  }

  .scoreBox {
    flex: 1;
  }

  .piece {
    font-size: 28px;
  }
}
