:root {
  --bg: #070708;
  --panel: #101114;
  --empty: #2a2a32;
  --pit: #000000;
  --line: rgba(255, 255, 255, 0.22);
  --text: #f4f4f5;
  --muted: #9a9aa3;
  color-scheme: dark;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: #1c1c1f;
  color: var(--text);
  font-family: ui-rounded, "Nunito", "Segoe UI", system-ui, sans-serif;
}

body {
  min-height: 100dvh;
  height: 100dvh;
  display: grid;
  place-items: center;
}

.phone {
  position: relative;
  width: min(100%, 420px);
  height: min(100dvh, 860px);
  max-height: 100%;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 28px;
  padding:
    max(12px, env(safe-area-inset-top, 0px))
    max(12px, env(safe-area-inset-right, 0px))
    max(12px, env(safe-area-inset-bottom, 0px))
    max(12px, env(safe-area-inset-left, 0px));
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow: hidden;
  box-shadow: 0 30px 80px rgba(0, 0, 0, 0.45);
  user-select: none;
}

.welcome {
  position: absolute;
  inset: 0;
  z-index: 6;
  display: flex;
  flex-direction: column;
  padding:
    max(28px, env(safe-area-inset-top, 0px))
    max(20px, env(safe-area-inset-right, 0px))
    max(20px, env(safe-area-inset-bottom, 0px))
    max(20px, env(safe-area-inset-left, 0px));
  background: var(--bg);
}

.welcome.hidden {
  display: none;
}

.welcome-main {
  flex: 1;
  min-height: 0;
  overflow: auto;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.welcome-logo {
  margin: 0;
  font-size: clamp(2rem, 9vw, 2.6rem);
  font-weight: 700;
  letter-spacing: 0.01em;
}

.welcome-tag {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 0.95rem;
  line-height: 1.4;
}

.welcome-spectrum {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 4px;
  margin: 22px 0 8px;
}

.welcome-spectrum span {
  height: 12px;
  border-radius: 3px;
  background: var(--hue);
}

.welcome-list {
  margin: 18px 0 0;
}

.play-btn {
  appearance: none;
  flex: 0 0 auto;
  width: 100%;
  margin-top: 18px;
  border: 0;
  border-radius: 999px;
  padding: 14px 18px;
  font: inherit;
  font-size: 1.15rem;
  font-weight: 800;
  cursor: pointer;
  background: linear-gradient(160deg, #ff7a59 0%, #ffd45e 100%);
  color: #1a1208;
}

.play-btn:active {
  filter: brightness(1.06);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  flex: 0 0 auto;
  min-width: 0;
}

.topbar > .logo {
  min-width: 0;
  flex: 1;
}

.logo {
  margin: 0;
  font-size: clamp(1.05rem, 4.6vw, 1.28rem);
  font-weight: 700;
  letter-spacing: 0.01em;
}

.rainbow-word span:nth-child(1) { color: hsl(0 82% 56%); }
.rainbow-word span:nth-child(2) { color: hsl(35 82% 56%); }
.rainbow-word span:nth-child(3) { color: hsl(70 82% 56%); }
.rainbow-word span:nth-child(4) { color: hsl(105 82% 56%); }
.rainbow-word span:nth-child(5) { color: hsl(140 82% 56%); }
.rainbow-word span:nth-child(6) { color: hsl(175 82% 56%); }
.rainbow-word span:nth-child(7) { color: hsl(210 82% 56%); }
.rainbow-word span:nth-child(8) { color: hsl(245 82% 56%); }
.rainbow-word span:nth-child(9) { color: hsl(280 82% 56%); }
.rainbow-word span:nth-child(10) { color: hsl(315 82% 56%); }

.bottombar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex: 0 0 auto;
}

.help-btn {
  appearance: none;
  flex: 0 0 auto;
  width: 32px;
  height: 32px;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 1.05rem;
  font-weight: 700;
  line-height: 1;
  cursor: pointer;
}

.help-btn:active {
  color: var(--text);
  background: var(--panel);
}

.retry {
  appearance: none;
  flex: 0 0 auto;
  margin: 0;
  border: 1px solid var(--line);
  background: transparent;
  color: var(--muted);
  border-radius: 999px;
  padding: 8px 16px;
  font: inherit;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
}

.retry:active {
  color: var(--text);
  background: var(--panel);
}

.retry.is-armed {
  color: var(--text);
  border-color: color-mix(in srgb, white 55%, transparent);
  background: var(--panel);
}

.meters {
  display: flex;
  align-items: flex-end;
  gap: 12px;
  flex: 0 0 auto;
  min-width: 0;
}

.score-wrap {
  min-width: 0;
  padding: 0;
  text-align: right;
}

.score-label {
  display: block;
  color: var(--muted);
  font-size: 0.65rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

#level,
#lines,
#score {
  font-size: clamp(1.05rem, 4.4vw, 1.4rem);
  font-weight: 700;
  display: inline-block;
}

#score.bump {
  animation: score-bump 280ms ease-out;
}

.well-wrap {
  border: 1px solid var(--line);
  border-top: 0;
  border-radius: 0 0 8px 8px;
  background: var(--pit);
  flex: 0 0 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.board-wrap {
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel);
  flex: 0 0 auto;
  align-self: center;
  width: 100%;
  min-height: 0;
}

.well {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 2px;
  padding: 2px 6px 6px;
  height: 160px;
  min-height: 0;
  overflow: hidden;
}

.spectrum {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 2px;
  margin: 0 6px 6px;
  padding-top: 8px;
  border-top: 1px solid var(--line);
}

.spectrum span {
  position: relative;
  height: 24px;
  border-radius: 2px;
  background: var(--hue);
  overflow: hidden;
}

.spectrum .mark {
  position: absolute;
  top: 50%;
  left: 50%;
  right: auto;
  bottom: auto;
  width: 14px;
  height: 14px;
  margin: 0;
  transform: translate(-50%, -50%);
}

.well-col {
  display: grid;
  grid-template-rows: repeat(12, 1fr);
  gap: 2px;
}

.well-cell.is-drop-in {
  z-index: 2;
  animation: well-drop-in 0.78s cubic-bezier(0.2, 0.85, 0.2, 1) both;
}

.well-cell {
  border-radius: 1px;
  background: var(--pit);
}

.well-cell.filled {
  background: var(--hue);
  box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--hue) 40%, black);
}

.well-cell.just-added {
  animation: pop-in 180ms ease-out;
}

.well-cell.flashing {
  z-index: 1;
  animation: well-flash 0.72s ease-in-out forwards;
}

.well-cell.dropping {
  animation: well-drop 0.42s cubic-bezier(0.2, 0.8, 0.2, 1);
}

.well-cell.is-explode {
  z-index: 3;
  animation: well-explode 0.32s ease-out forwards;
}

@keyframes well-flash {
  0%,
  35% {
    filter: brightness(1);
    transform: scale(1);
  }
  12%,
  48% {
    filter: brightness(2.5);
    transform: scale(1.12);
    box-shadow: 0 0 10px var(--hue);
  }
  70% {
    filter: brightness(1.2);
    transform: scale(1);
  }
  100% {
    opacity: 0;
    transform: scale(0.35);
  }
}

@keyframes well-drop {
  from {
    transform: translateY(calc(-100% - 2px));
  }
  to {
    transform: translateY(0);
  }
}

@keyframes well-explode {
  0% {
    opacity: 1;
    transform: scale(1) rotate(0deg);
    filter: brightness(1);
  }
  22% {
    opacity: 1;
    transform: scale(1.55) rotate(12deg);
    filter: brightness(2.6);
    box-shadow: 0 0 14px var(--hue);
  }
  100% {
    opacity: 0;
    transform: scale(0.2) translateY(-22px) rotate(-18deg);
    filter: brightness(3);
  }
}

@keyframes well-drop-in {
  from {
    transform: translateY(calc(-11 * (100% + 2px)));
  }
  to {
    transform: translateY(0);
  }
}

@keyframes score-bump {
  0% {
    transform: scale(1);
  }
  40% {
    transform: scale(1.28);
  }
  100% {
    transform: scale(1);
  }
}

.board {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  grid-template-rows: repeat(5, 1fr);
  gap: 4px;
  padding: 6px;
  width: 100%;
  aspect-ratio: 1;
  touch-action: none;
}

.cell {
  position: relative;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: var(--empty);
}

.tile {
  position: absolute;
  inset: 0;
  border-radius: 3px;
  background: var(--hue);
  box-shadow: inset 0 0 0 1px color-mix(in srgb, white 18%, transparent);
}

.tile.spawn {
  animation: pop-in 180ms ease-out;
}

.tile.merge {
  animation: merge-pop 200ms ease-out;
}

.tile.will-merge {
  z-index: 1;
  box-shadow:
    inset 0 0 0 3px #fff,
    0 0 10px rgba(255, 255, 255, 0.8);
}

.mark {
  display: block;
  pointer-events: none;
  overflow: visible;
  fill: none;
  stroke: rgb(0 0 0 / 0.38);
  stroke-width: 2px;
  stroke-linejoin: round;
  stroke-linecap: round;
}

.mark * {
  vector-effect: non-scaling-stroke;
}

.tile .mark {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 56%;
  height: 56%;
  transform: translate(-50%, -50%);
}

.overlay {
  position: absolute;
  inset: 0;
  z-index: 4;
  display: grid;
  place-items: center;
  background: rgba(0, 0, 0, 0.62);
  padding: 24px;
}

.overlay.hidden {
  display: none;
}

.overlay-card {
  width: min(100%, 280px);
  padding: 22px 20px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: #141418;
  text-align: center;
}

.help-card {
  width: min(100%, 320px);
}

#over-title {
  margin: 0;
  font-size: 1.6rem;
  font-weight: 700;
}

#over-reason {
  margin: 8px 0 0;
  color: var(--muted);
}

.over-score {
  margin: 16px 0 18px;
  font-size: 1.05rem;
}

.level-up-card {
  background: linear-gradient(160deg, #ff7a59 0%, #ffd45e 100%);
  color: #1a1208;
  border: none;
  box-shadow: 0 10px 36px rgba(255, 150, 70, 0.45);
}

.level-up-kicker {
  margin: 0;
  color: rgba(26, 18, 8, 0.62);
  font-size: 0.75rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

#level-up-title {
  margin: 8px 0 0;
  font-size: 1.55rem;
  font-weight: 700;
}

#restart,
#help-close {
  appearance: none;
  border: 1px solid var(--line);
  background: #f4f4f5;
  color: #111;
  border-radius: 999px;
  padding: 10px 18px;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}

.help-title {
  margin: 0;
  font-size: 1.35rem;
  font-weight: 700;
}

.help-list {
  margin: 14px 0 18px;
  padding: 0 0 0 1.1em;
  color: var(--muted);
  text-align: left;
  font-size: 0.88rem;
  line-height: 1.45;
}

.help-list li + li {
  margin-top: 8px;
}

@keyframes pop-in {
  from {
    transform: scale(0.55);
    opacity: 0.2;
  }
  to {
    transform: scale(1);
    opacity: 1;
  }
}

@keyframes merge-pop {
  0% {
    transform: scale(0.86);
  }
  60% {
    transform: scale(1.08);
  }
  100% {
    transform: scale(1);
  }
}

@media (max-width: 430px) {
  .phone {
    width: 100%;
    height: 100dvh;
    max-height: 100dvh;
    border: 0;
    border-radius: 0;
    box-shadow: none;
  }
}
