:root {
  color-scheme: light;
  --cream: #fbf4e7;
  --cream-deep: #f4e8d3;
  --ink: #3e3941;
  --muted: #8d8280;
  --line: rgba(111, 83, 72, 0.12);
  --board: #efe1cb;
  --board-shadow: rgba(114, 82, 62, 0.16);
  --focus: #7466ad;
  --gap: clamp(4px, 1.25vw, 9px);
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
  background: var(--cream);
}

body {
  min-width: 320px;
  min-height: 100vh;
  margin: 0;
  overflow-x: hidden;
  color: var(--ink);
  font-family: ui-rounded, "SF Pro Rounded", "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
  background:
    radial-gradient(circle at 8% 12%, rgba(255, 221, 179, 0.48), transparent 28rem),
    radial-gradient(circle at 92% 86%, rgba(210, 233, 218, 0.45), transparent 30rem),
    var(--cream);
}

button {
  font: inherit;
}

.game-shell {
  width: 100%;
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0 auto;
  display: grid;
  place-items: center;
  justify-content: center;
  padding: 0;
}

.topbar,
.intro {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.topbar {
  position: fixed;
  top: calc(env(safe-area-inset-top) + 12px);
  right: calc(env(safe-area-inset-right) + 12px);
  z-index: 10;
}

.top-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.skin-switch {
  display: inline-flex;
  gap: 2px;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: rgba(255, 252, 246, 0.58);
}

.skin-option {
  min-height: 32px;
  padding: 5px 10px;
  border: 0;
  border-radius: 999px;
  color: #887a79;
  background: transparent;
  cursor: pointer;
  font-size: 0.75rem;
  transition: color 180ms ease, background 180ms ease, transform 180ms ease;
}

.skin-option:hover {
  color: var(--ink);
}

.skin-option:active {
  transform: scale(0.96);
}

.skin-option.is-active {
  color: var(--ink);
  background: rgba(255, 255, 255, 0.88);
}

.eyebrow {
  margin: 0 0 5px;
  color: #ad8f78;
  font-size: 0.67rem;
  font-weight: 800;
  letter-spacing: 0.2em;
}

h1,
h2,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 0;
  font-size: clamp(1.45rem, 4vw, 1.9rem);
  font-weight: 800;
  letter-spacing: 0.04em;
}

.icon-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  min-height: 44px;
  padding: 4px;
  border: 0;
  border-radius: 0;
  color: #76686b;
  background: transparent;
  box-shadow: none;
  cursor: pointer;
  transition: color 180ms ease, background 180ms ease, transform 180ms ease;
}

.icon-button:hover {
  color: var(--ink);
  background: transparent;
  transform: translateY(-1px);
}

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

.icon-button:focus-visible,
.gem:focus-visible {
  outline: 3px solid rgba(116, 102, 173, 0.38);
  outline-offset: 3px;
}

.sound-icon {
  display: block;
  width: 21.6px;
  height: 21.6px;
  object-fit: contain;
  object-position: center;
}

.intro {
  gap: 20px;
  margin: clamp(28px, 6vw, 48px) 2px 18px;
}

.intro h2 {
  margin-bottom: 5px;
  font-size: clamp(1.08rem, 3vw, 1.28rem);
  font-weight: 750;
  letter-spacing: 0.05em;
}

.intro p {
  max-width: 28rem;
  margin-bottom: 0;
  color: var(--muted);
  font-size: 0.86rem;
  line-height: 1.65;
}

.breathing-dot {
  flex: 0 0 auto;
  width: 10px;
  height: 10px;
  margin-right: 7px;
  border-radius: 50%;
  background: #9bc6ab;
  box-shadow: 0 0 0 7px rgba(155, 198, 171, 0.15);
  animation: breathe 3.2s ease-in-out infinite;
}

.board-shell {
  --board-size: min(80vh, calc(100vw - 24px));
  --board-size: min(80dvh, calc(100vw - 24px));
  width: var(--board-size);
  height: var(--board-size);
  padding: 0;
  border: 0;
  border-radius: clamp(22px, 5vw, 34px);
  background: transparent;
  box-shadow: none;
  user-select: none;
}

.board {
  display: grid;
  grid-template-columns: repeat(var(--cols, 8), minmax(0, 1fr));
  grid-template-rows: repeat(var(--rows, 8), minmax(0, 1fr));
  gap: var(--gap);
  width: 100%;
  height: 100%;
  padding: clamp(8px, 1.8vw, 14px);
  border-radius: clamp(17px, 4vw, 27px);
  background:
    linear-gradient(135deg, rgba(255, 252, 242, 0.58), transparent 40%),
    var(--board);
  box-shadow: none;
  touch-action: none;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.gem {
  position: relative;
  display: grid;
  min-width: 0;
  min-height: 0;
  place-items: center;
  padding: 0;
  overflow: hidden;
  border: 0;
  border-radius: 26%;
  color: rgba(255, 255, 255, 0.9);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  box-shadow:
    inset 0 2px 2px rgba(255, 255, 255, 0.54),
    inset 0 -5px 8px rgba(73, 44, 67, 0.12),
    0 3px 4px rgba(92, 64, 60, 0.13);
  transform: translateZ(0);
  transition: transform 160ms ease, filter 160ms ease, box-shadow 160ms ease;
}

.gem::before {
  position: absolute;
  top: 11%;
  left: 16%;
  width: 35%;
  height: 17%;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.45);
  content: "";
  transform: rotate(-18deg);
}

.gem::after {
  position: absolute;
  right: -17%;
  bottom: -31%;
  width: 78%;
  height: 55%;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.12);
  content: "";
}

.gem:hover {
  filter: saturate(1.08) brightness(1.04);
  transform: translateY(-2px) scale(1.035);
}

.gem:active,
.gem.is-selected {
  transform: scale(0.91);
}

.gem.is-selected {
  box-shadow:
    0 0 0 3px rgba(255, 255, 255, 0.9),
    0 0 0 6px rgba(116, 102, 173, 0.45),
    inset 0 2px 2px rgba(255, 255, 255, 0.55),
    0 6px 12px rgba(82, 64, 104, 0.22);
  z-index: 2;
}

.gem[data-kind="0"] { background: linear-gradient(145deg, #8ed7d2, #55b6bc); }
.gem[data-kind="1"] { background: linear-gradient(145deg, #ffb495, #e8797c); }
.gem[data-kind="2"] { background: linear-gradient(145deg, #c6b5ef, #8e7fd0); }
.gem[data-kind="3"] { background: linear-gradient(145deg, #f5d883, #e9ad59); }
.gem[data-kind="4"] { background: linear-gradient(145deg, #a8d9ad, #6fb48a); }
.gem[data-kind="5"] { background: linear-gradient(145deg, #df9cc0, #ba6e9d); }

.gem-symbol {
  position: relative;
  z-index: 1;
  font-size: clamp(1rem, 5vw, 1.75rem);
  line-height: 1;
  text-shadow: 0 2px 2px rgba(70, 47, 65, 0.16);
  transition: transform 160ms ease;
}

.gem.is-selected .gem-symbol {
  transform: scale(1.12) rotate(-8deg);
}

.gem.is-matched {
  z-index: 4;
  overflow: visible;
  animation: jelly-pop 420ms cubic-bezier(0.25, 0.8, 0.25, 1) both;
  pointer-events: none;
}

.gem.is-falling {
  animation: fall-local 360ms cubic-bezier(0.2, 0.9, 0.2, 1) both;
}

.gem.is-new {
  animation: fall-new 360ms cubic-bezier(0.2, 0.9, 0.2, 1) both;
}

.gem.is-invalid {
  animation: nudge 260ms ease-in-out both;
}

.gem.is-swapping {
  z-index: 3;
  pointer-events: none;
  animation-duration: 180ms;
  animation-timing-function: cubic-bezier(0.22, 0.8, 0.25, 1);
  animation-fill-mode: both;
}

.gem.is-swap-right { animation-name: swap-right; }
.gem.is-swap-left { animation-name: swap-left; }
.gem.is-swap-down { animation-name: swap-down; }
.gem.is-swap-up { animation-name: swap-up; }

.match-particle {
  position: absolute;
  z-index: 3;
  top: 50%;
  left: 50%;
  width: clamp(3px, 1vw, 6px);
  height: clamp(3px, 1vw, 6px);
  border-radius: 50%;
  background: #fffdf3;
  box-shadow: 0 0 4px rgba(255, 255, 255, 0.8);
  pointer-events: none;
  animation: particle-burst 330ms cubic-bezier(0.15, 0.75, 0.25, 1) both;
}

body[data-skin="paper"] {
  color: #403b38;
  background: #f6eee1;
}

body[data-skin="paper"] .top-actions,
body[data-skin="paper"] .skin-switch,
body[data-skin="paper"] .icon-button {
  --line: rgba(64, 59, 56, 0.2);
}

body[data-skin="paper"] .skin-switch {
  background: rgba(247, 247, 237, 0.68);
}

body[data-skin="paper"] .skin-option.is-active,
body[data-skin="paper"] .icon-button {
  color: #403b38;
  background: transparent;
  box-shadow: none;
}

body[data-skin="paper"] .board-shell {
  border: 0;
  background: transparent;
  box-shadow: none;
}

body[data-skin="paper"] .board {
  border-radius: 18px;
  background: #f6eee1;
  box-shadow: none;
}

body[data-skin="paper"] .gem {
  overflow: visible;
  border-radius: 0;
  color: #403b38;
  background: transparent;
  box-shadow: none;
}

body[data-skin="paper"] .gem::before,
body[data-skin="paper"] .gem::after,
body[data-skin="paper"] .gem[data-kind="0"],
body[data-skin="paper"] .gem[data-kind="1"],
body[data-skin="paper"] .gem[data-kind="2"],
body[data-skin="paper"] .gem[data-kind="3"],
body[data-skin="paper"] .gem[data-kind="4"],
body[data-skin="paper"] .gem[data-kind="5"] {
  background: transparent;
}

body[data-skin="paper"] .gem-symbol {
  display: grid;
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  place-items: center;
  font-size: 1rem;
  text-shadow: none;
}

body[data-skin="paper"] .paper-symbol {
  display: block;
  width: 85%;
  height: 85%;
  min-width: 0;
  min-height: 0;
  margin: 0;
  overflow: hidden;
  object-fit: contain;
  object-position: center;
}

body[data-skin="paper"] .gem[data-kind="4"] .paper-symbol {
  transform: scale(0.85);
}

body[data-skin="paper"] .gem:hover {
  filter: none;
  transform: translateY(-1px) scale(1.02);
}

body[data-skin="paper"] .gem:active,
body[data-skin="paper"] .gem.is-selected {
  outline: none;
  border-radius: 0;
  box-shadow: none;
  transform: none;
}

body[data-skin="paper"] .gem:focus-visible {
  outline: none;
  box-shadow: none;
}

body[data-skin="paper"] .gem:active .gem-symbol,
body[data-skin="paper"] .gem.is-selected .gem-symbol,
body[data-skin="paper"] .gem:focus-visible .gem-symbol {
  overflow: visible;
  transform: none;
}

body[data-skin="paper"] .gem:active .paper-symbol,
body[data-skin="paper"] .gem.is-selected .paper-symbol,
body[data-skin="paper"] .gem:focus-visible .paper-symbol {
  overflow: visible;
  filter:
    drop-shadow(1.6px 0 0 #fffdf7)
    drop-shadow(-1.6px 0 0 #fffdf7)
    drop-shadow(0 1.6px 0 #fffdf7)
    drop-shadow(0 -1.6px 0 #fffdf7)
    drop-shadow(1.2px 1.2px 0 #fffdf7)
    drop-shadow(-1.2px 1.2px 0 #fffdf7)
    drop-shadow(1.2px -1.2px 0 #fffdf7)
    drop-shadow(-1.2px -1.2px 0 #fffdf7);
}

body[data-skin="paper"] .gem.is-matched {
  box-shadow: none;
}

body[data-skin="paper"] .match-particle {
  background: #403b38;
  box-shadow: none;
}

body[data-skin="paper"] .breathing-dot {
  border: 1px solid #84634f;
  background: #7c9f7a;
  box-shadow: none;
}

.hint {
  margin: 18px 5px 0;
  color: #a18e84;
  font-size: 0.78rem;
  letter-spacing: 0.04em;
  text-align: center;
}

.hint span {
  display: inline-block;
  margin-right: 4px;
  color: #d5a45a;
  animation: twinkle 2.5s ease-in-out infinite;
}

@media (min-width: 561px) and (max-height: 900px) {
  .game-shell {
    padding-top: 14px;
    padding-bottom: 14px;
  }

  .intro {
    margin-top: 16px;
    margin-bottom: 10px;
  }

  .intro h2 {
    margin-bottom: 2px;
  }

  .hint {
    margin-top: 8px;
  }
}

noscript {
  display: block;
  margin-top: 16px;
  color: #a86565;
  font-size: 0.85rem;
  text-align: center;
}

@keyframes breathe {
  0%, 100% { transform: scale(0.86); opacity: 0.7; }
  50% { transform: scale(1.15); opacity: 1; }
}

@keyframes jelly-pop {
  0% { opacity: 1; transform: scale(1) rotate(0); }
  40% { opacity: 1; transform: scale(1.18) rotate(calc(var(--pop-turn, 0deg) * .4)); }
  100% { opacity: 0; transform: scale(0) rotate(var(--pop-turn, 0deg)); }
}

@keyframes fall-local {
  0% { transform: translateY(var(--fall-start, -20px)); }
  70% { transform: translateY(4%) scale(1.04); }
  100% { transform: translateY(0) scale(1); }
}

@keyframes fall-new {
  0% { opacity: 0; transform: translateY(var(--fall-start, -20px)) scale(0.82); }
  70% { opacity: 1; transform: translateY(4%) scale(1.04); }
  100% { opacity: 1; transform: translateY(0) scale(1); }
}

@keyframes nudge {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-8%); }
  75% { transform: translateX(8%); }
}

@keyframes swap-right {
  to { transform: translateX(calc(100% + var(--gap))); }
}

@keyframes swap-left {
  to { transform: translateX(calc(-100% - var(--gap))); }
}

@keyframes swap-down {
  to { transform: translateY(calc(100% + var(--gap))); }
}

@keyframes swap-up {
  to { transform: translateY(calc(-100% - var(--gap))); }
}

@keyframes particle-burst {
  0% { opacity: 1; transform: rotate(var(--particle-angle)) translateY(0) scale(0.9); }
  100% { opacity: 0; transform: rotate(var(--particle-angle)) translateY(-28px) scale(0.1); }
}

@keyframes twinkle {
  0%, 100% { opacity: 0.65; transform: scale(0.9) rotate(0); }
  50% { opacity: 1; transform: scale(1.1) rotate(14deg); }
}

@media (max-width: 560px) {
  .game-shell {
    width: min(100% - 20px, 680px);
    padding-top: 16px;
    padding-bottom: 16px;
  }

  .mute-label {
    display: none;
  }

  .top-actions {
    gap: 4px;
  }

  .skin-switch {
    padding: 2px;
  }

  .skin-option {
    min-height: 34px;
    padding: 5px 8px;
    font-size: 0.7rem;
  }

  .icon-button {
    width: 44px;
    height: 44px;
    justify-content: center;
    padding: 4px;
  }

  .intro {
    margin-top: 25px;
  }

  .intro p {
    font-size: 0.8rem;
  }

  .breathing-dot {
    margin-right: 5px;
  }
}

@media (max-width: 560px), (pointer: coarse) and (max-width: 1100px) {
  html,
  body {
    overflow: hidden;
  }

  .game-shell {
    display: block;
    width: 100%;
    min-height: 100dvh;
    padding: calc(env(safe-area-inset-top) + 44px) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  }

  .topbar {
    top: env(safe-area-inset-top);
    right: env(safe-area-inset-right);
  }

  .board-shell {
    width: calc(100dvw - env(safe-area-inset-left) - env(safe-area-inset-right));
    height: calc(100dvh - env(safe-area-inset-top) - env(safe-area-inset-bottom) - 44px);
    margin: 0;
  }

  .board {
    width: 100%;
    height: 100%;
  }
}

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