:root {
  color-scheme: dark;
  --bg: #111827;
  --panel: #172033;
  --cell: #263247;
  --text: #eef6ff;
  --muted: #9fb0c6;
  --accent: #54d6a1;
  --line: rgba(255, 255, 255, .1);
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  background:
    linear-gradient(160deg, rgba(84, 214, 161, .12), transparent 34rem),
    #111827;
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
}

button {
  font: inherit;
}

.shell {
  width: min(100%, 760px);
  min-height: 100dvh;
  margin: 0 auto;
  padding: max(.75rem, env(safe-area-inset-top)) .8rem max(.8rem, env(safe-area-inset-bottom));
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: .75rem;
}

.topbar {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) 2.75rem;
  gap: .5rem;
}

.score-box,
.icon-button,
#play-again {
  border-radius: 8px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, .06);
  box-shadow: 0 10px 26px rgba(0, 0, 0, .22);
}

.score-box {
  min-width: 0;
  padding: .45rem .65rem;
  text-align: center;
}

.score-box span {
  display: block;
  color: var(--muted);
  font-size: .68rem;
  font-weight: 900;
  letter-spacing: .08em;
  text-transform: uppercase;
}

.score-box strong {
  display: block;
  margin-top: .06rem;
  font-size: clamp(1.12rem, 4vw, 1.75rem);
  line-height: 1;
}

.icon-button {
  min-width: 2.75rem;
  min-height: 2.75rem;
  color: var(--text);
  cursor: pointer;
  font-size: 1.35rem;
}

.board-wrap {
  position: relative;
  min-height: 0;
  display: grid;
  place-items: center;
  perspective: 900px;
}

.board {
  --cols: 10;
  --rows: 12;
  --gap: clamp(.14rem, .58vw, .28rem);
  --pad: clamp(.28rem, 1vw, .45rem);
  --cell-size: min(calc((100vw - 3.6rem) / var(--cols)), calc((100dvh - 8rem) / var(--rows)), 3.35rem);
  width: max-content;
  max-width: 100%;
  padding: clamp(.28rem, 1vw, .45rem);
  border-radius: 8px;
  display: grid;
  grid-template-columns: repeat(var(--cols), var(--cell-size));
  grid-template-rows: repeat(var(--rows), var(--cell-size));
  gap: var(--gap);
  background: linear-gradient(180deg, #1a2438, #131c2d);
  border: 1px solid rgba(255, 255, 255, .12);
  box-shadow: 0 18px 48px rgba(0, 0, 0, .32);
  transform-origin: 50% 50%;
  transform-style: preserve-3d;
  backface-visibility: hidden;
  will-change: transform;
}

.board.board-flipping {
  animation: boardFlip3d .42s cubic-bezier(.34, 0, .16, 1) both;
  pointer-events: none;
}

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

.board.board-falling .tile.falling {
  animation-name: sandFall;
  animation-duration: var(--fall-duration);
  animation-delay: var(--fall-delay);
  animation-fill-mode: both;
  animation-direction: normal;
  animation-iteration-count: 1;
}

.tile {
  position: relative;
  min-width: 0;
  min-height: 0;
  border: 2px solid var(--tile-border);
  border-radius: 6px;
  background: var(--tile-color);
  color: var(--tile-border);
  box-shadow: none;
  cursor: pointer;
  display: grid;
  place-items: center;
  font-size: clamp(.72rem, calc(var(--cell-size) * .42), 1.2rem);
  font-weight: 1000;
  line-height: 1;
  touch-action: manipulation;
  transition: transform .12s ease, filter .12s ease, opacity .16s ease;
}

.tile.bomb,
.tile.magnet,
.tile.switch {
  border-style: dashed;
}

.tile.bomb {
  color: #111827;
}

.tile.magnet {
  color: #dc2626;
}

.special-svg {
  width: 90%;
  height: 90%;
  display: block;
  overflow: visible;
  transform-origin: 50% 50%;
}

.magnet-svg {
  transform: scale(1.18);
}

.bomb-svg {
  width: 86%;
  height: 86%;
  transform: scale(1.28);
}

.tile.bomb::before,
.tile.bomb::after,
.tile.magnet::before,
.tile.magnet::after,
.tile.switch::before,
.tile.switch::after,
.tile.metal::before,
.tile.metal::after {
  content: "";
  position: absolute;
  pointer-events: none;
}

.tile.bomb::before,
.tile.bomb::after,
.tile.magnet::before,
.tile.magnet::after {
  content: none;
}

.tile.bomb::before {
  width: 50%;
  height: 50%;
  left: 23%;
  bottom: 10%;
  border-radius: 50%;
  background:
    radial-gradient(circle at 34% 28%, rgba(255, 255, 255, .36), transparent 0 18%, transparent 28%),
    radial-gradient(circle at 55% 64%, #374151 0, #111827 58%, #030712 100%);
  border: .08em solid #030712;
}

.tile.bomb::after {
  width: 28%;
  height: 25%;
  top: 12%;
  left: 47%;
  background:
    radial-gradient(circle at 60% 30%, #fef08a 0 14%, #fb923c 15% 24%, #ef4444 25% 32%, transparent 33%),
    linear-gradient(125deg, transparent 0 52%, #1f2937 53% 63%, transparent 64% 100%);
  transform: none;
}

.tile.magnet::before {
  width: 46%;
  height: 50%;
  left: 27%;
  border: .34em solid #dc2626;
  border-top: 0;
  border-radius: 0 0 999px 999px;
  bottom: 17%;
}

.tile.magnet::after {
  width: 54%;
  height: 20%;
  top: 25%;
  left: 23%;
  background:
    linear-gradient(90deg, #e5e7eb 0 20%, #38bdf8 20% 34%, transparent 34% 66%, #38bdf8 66% 80%, #e5e7eb 80% 100%);
  border-radius: 3px;
}

.tile.switch::before,
.tile.switch::after {
  top: 20%;
  width: 34%;
  height: 60%;
  border: 0;
  clip-path: polygon(50% 0, 100% 42%, 72% 42%, 72% 100%, 28% 100%, 28% 42%, 0 42%);
}

.tile.switch::before {
  left: 18%;
  background: #f8fafc;
}

.tile.switch::after {
  right: 18%;
  background: #f8fafc;
  transform: rotate(180deg);
}

.tile.metal {
  background:
    linear-gradient(135deg, rgba(255, 255, 255, .38), transparent 34%),
    linear-gradient(180deg, #d7dde6, #9aa7b8);
  border: 0;
  color: rgba(71, 85, 105, .92);
  cursor: default;
  box-shadow:
    inset 0 0 0 2px #475569,
    inset 0 0 0 4px rgba(241, 245, 249, .34);
}

.tile.metal::before {
  inset: 0;
  border: 0;
  background:
    linear-gradient(45deg, transparent 47%, rgba(51, 65, 85, .95) 47% 53%, transparent 53%),
    linear-gradient(-45deg, transparent 47%, rgba(51, 65, 85, .95) 47% 53%, transparent 53%),
    linear-gradient(90deg, transparent 0 8%, rgba(255, 255, 255, .28) 8% 10%, transparent 10% 90%, rgba(15, 23, 42, .22) 90% 92%, transparent 92%);
}

.tile.metal::after {
  inset: 10%;
  background:
    radial-gradient(circle at 0 0, rgba(241, 245, 249, .95) 0 .13em, rgba(51, 65, 85, .8) .14em .2em, transparent .21em),
    radial-gradient(circle at 100% 0, rgba(241, 245, 249, .95) 0 .13em, rgba(51, 65, 85, .8) .14em .2em, transparent .21em),
    radial-gradient(circle at 0 100%, rgba(241, 245, 249, .95) 0 .13em, rgba(51, 65, 85, .8) .14em .2em, transparent .21em),
    radial-gradient(circle at 100% 100%, rgba(241, 245, 249, .95) 0 .13em, rgba(51, 65, 85, .8) .14em .2em, transparent .21em);
}

.tile.empty {
  pointer-events: none;
  opacity: 0;
  box-shadow: none;
}

.tile.grouped {
  filter: brightness(1.22);
  transform: scale(.9);
  outline: 2px solid rgba(255, 255, 255, .4);
  outline-offset: -2px;
}

.tile.clearing {
  animation: clearTile .22s ease-in forwards;
}

.message {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 1rem;
  background: rgba(8, 12, 20, .58);
  backdrop-filter: blur(6px);
}

.message[hidden] {
  display: none;
}

.message-panel {
  width: min(100%, 320px);
  padding: 1.2rem;
  border-radius: 8px;
  background: #172033;
  border: 1px solid rgba(255, 255, 255, .13);
  text-align: center;
  box-shadow: 0 20px 60px rgba(0, 0, 0, .38);
}

.message-panel h1 {
  margin: 0;
  font-size: 1.8rem;
}

.message-panel p {
  margin: .45rem 0 1rem;
  color: var(--muted);
  font-weight: 800;
}

#play-again {
  width: 100%;
  min-height: 2.8rem;
  background: var(--accent);
  color: #07110d;
  border-color: transparent;
  font-weight: 1000;
  cursor: pointer;
}

@keyframes clearTile {
  to {
    transform: scale(.2) rotate(10deg);
    opacity: 0;
  }
}

@keyframes boardFlip3d {
  0% {
    transform: translateZ(0) rotateX(0deg) scale(1);
    filter: brightness(1);
  }

  45% {
    transform: translateZ(0) rotateX(58deg) scale(.98);
    filter: brightness(.9);
  }

  55% {
    transform: translateZ(0) rotateX(-28deg) scale(.98);
    filter: brightness(.94);
  }

  100% {
    transform: translateZ(0) rotateX(0deg) scale(1);
    filter: brightness(1);
  }
}

@keyframes sandFall {
  0% {
    transform: translateY(calc(var(--drop-offset) * -1));
    opacity: 1;
  }

  100% {
    transform: translateY(0);
    opacity: 1;
  }
}

@media (max-width: 430px) {
  .shell {
    padding-left: .55rem;
    padding-right: .55rem;
  }

  .topbar {
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) 2.45rem;
    gap: .42rem;
  }

  .icon-button {
    min-width: 2.45rem;
    min-height: 2.45rem;
  }
}

@media (orientation: landscape) and (max-height: 520px) {
  .shell {
    width: min(100%, 960px);
    grid-template-columns: minmax(8.5rem, 12rem) minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr);
    align-items: center;
    gap: .75rem;
  }

  .topbar {
    grid-template-columns: 1fr;
    align-content: center;
  }

  .board {
    --gap: clamp(.12rem, .42vw, .24rem);
    --cell-size: min(calc((100vw - 16rem) / var(--cols)), calc((100dvh - 5rem) / var(--rows)), 3.35rem);
  }
}
