:root {
  --bg-top: #fff4cb;
  --bg-bottom: #ffc4c9;
  --panel: #fff9f6d4;
  --text: #2b1d30;
  --accent: #ef476f;
  --board-bg: #ffffffb3;
  --board-cell: #f7ecff;
  --hint: #fff176;
  --focus: #3d5afe;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: "Trebuchet MS", "Segoe UI", sans-serif;
  color: var(--text);
  background: radial-gradient(circle at 20% 20%, #fff7d4, transparent 45%),
    radial-gradient(circle at 90% 90%, #ffd0df, transparent 35%),
    linear-gradient(145deg, var(--bg-top), var(--bg-bottom));
  display: grid;
  place-items: center;
  padding: 16px;
  padding-top: max(16px, env(safe-area-inset-top));
  padding-right: max(16px, env(safe-area-inset-right));
  padding-bottom: max(16px, env(safe-area-inset-bottom));
  padding-left: max(16px, env(safe-area-inset-left));
}

.app {
  width: min(880px, 100%);
  background: var(--panel);
  border-radius: 20px;
  border: 2px solid #ffffffd9;
  box-shadow: 0 16px 34px #0000001f;
  padding: 16px;
}

.hud h1 {
  margin: 0 0 10px;
  font-size: clamp(1.4rem, 3vw, 2rem);
}

.stats {
  display: grid;
  grid-template-columns: repeat(4, minmax(110px, 1fr));
  gap: 10px;
}

.stat {
  background: #ffffffc2;
  border: 1px solid #ffffff;
  border-radius: 12px;
  padding: 8px 12px;
}

.label {
  display: block;
  font-size: 0.78rem;
  opacity: 0.85;
}

.stat span:last-child {
  font-size: 1.2rem;
  font-weight: 700;
}

.goals {
  margin-top: 10px;
  background: #ffffff8f;
  border: 1px solid #ffffff;
  border-radius: 12px;
  padding: 8px 10px;
}

.goals p {
  margin: 2px 0;
  font-weight: 600;
}

.game-wrap {
  margin: 14px 0;
  display: grid;
  place-items: center;
}

.board {
  width: min(92vw, 560px);
  height: min(92vw, 560px);
  min-width: 288px;
  min-height: 288px;
  max-width: 560px;
  max-height: 560px;
  padding: 8px;
  border-radius: 16px;
  background: var(--board-bg);
  border: 2px solid #ffffffd4;
  box-shadow: inset 0 3px 8px #00000018;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  gap: 6px;
  touch-action: none;
  position: relative;
  overflow: hidden;
}

.cell {
  background: var(--board-cell);
  border-radius: 12px;
  border: 1px solid #ffffffde;
  display: grid;
  place-items: center;
  position: relative;
  overflow: hidden;
  cursor: pointer;
  transition: transform 120ms ease;
  user-select: none;
  padding: 0;
  min-height: 0;
}

.cell.selected {
  transform: scale(1.05);
  outline: 3px solid #ffe066;
}

.cell.preview {
  outline: 3px dashed #ff9800;
}

.cell.hint {
  outline: 3px solid var(--hint);
  animation: hintPulse 900ms ease-in-out infinite;
}

.cell.keyboard-focus {
  outline: 3px solid var(--focus);
}

.candy {
  width: 72%;
  height: 72%;
  border-radius: 50%;
  position: relative;
  background:
    radial-gradient(circle at 30% 28%, var(--ball-highlight) 0 20%, transparent 21%),
    radial-gradient(circle at 33% 30%, var(--ball-mid) 0 42%, var(--ball-base) 58%, var(--ball-shadow) 100%);
  border: 1px solid var(--ball-rim);
  box-shadow: inset -5px -8px 10px #00000035, 0 3px 5px #0000002c;
  overflow: hidden;
}

.candy::after {
  content: "";
  position: absolute;
  inset: auto 15% 12% 15%;
  height: 18%;
  border-radius: 50%;
  background: #ffffff26;
}

.type-0 {
  --ball-base: #ff2d6f;
  --ball-mid: #ff5d8f;
  --ball-shadow: #c51d4f;
  --ball-highlight: #ffe5ee;
  --ball-rim: #b10f43;
}

.type-1 {
  --ball-base: #ffca28;
  --ball-mid: #ffd95d;
  --ball-shadow: #d19f0d;
  --ball-highlight: #fff7dc;
  --ball-rim: #be8d00;
}

.type-2 {
  --ball-base: #00bcd4;
  --ball-mid: #35d2e5;
  --ball-shadow: #078da0;
  --ball-highlight: #ddfaff;
  --ball-rim: #04788a;
}

.type-3 {
  --ball-base: #7cb342;
  --ball-mid: #9acb63;
  --ball-shadow: #5f8e2d;
  --ball-highlight: #effadb;
  --ball-rim: #4f7d20;
}

.type-4 {
  --ball-base: #8e24aa;
  --ball-mid: #af49ca;
  --ball-shadow: #67167e;
  --ball-highlight: #f3e0fb;
  --ball-rim: #560d6b;
}

.type-5 {
  --ball-base: #ff7043;
  --ball-mid: #ff9470;
  --ball-shadow: #cd4a22;
  --ball-highlight: #ffe9de;
  --ball-rim: #b23a17;
}

.special {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  pointer-events: none;
}

.special.striped-h::before,
.special.striped-v::before {
  content: "";
  position: absolute;
  inset: 22%;
  border-radius: 999px;
  background: #ffffffd1;
}

.special.striped-v::before {
  transform: rotate(90deg);
}

.special.wrapped {
  border: 3px dashed #ffffffcc;
}

.special.color-bomb {
  border: 2px solid #ffffffd1;
  background: conic-gradient(
    #ff2d6f,
    #ffca28,
    #00bcd4,
    #7cb342,
    #8e24aa,
    #ff7043,
    #ff2d6f
  );
  box-shadow: inset 0 0 0 5px #1f1f1fcc;
}

.controls {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
}

.btn-row {
  display: flex;
  gap: 8px;
}

button {
  border: 0;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  padding: 10px 16px;
  font-weight: 700;
  cursor: pointer;
}

button:hover {
  filter: brightness(1.08);
}

#pause-btn {
  background: #3457d5;
}

#hint-btn {
  background: #6d4c41;
}

#message {
  margin: 0;
  font-weight: 600;
}

.candy.falling {
  animation: rollDown 280ms cubic-bezier(0.2, 0.75, 0.3, 1) both;
}

.candy.matched {
  animation: popMatch 220ms cubic-bezier(0.2, 0.82, 0.3, 1) forwards;
}

.candy.swapping {
  animation: swapPulse 140ms ease;
}

.floating-text {
  place-self: center;
  z-index: 10;
  color: #fff;
  font-size: clamp(0.75rem, 1.8vw, 1rem);
  font-weight: 800;
  text-shadow: 0 1px 2px #00000060;
  pointer-events: none;
  animation: floatUp 650ms ease-out forwards;
}

.floating-text.combo {
  color: #fff8c2;
}

.impact-spark {
  position: absolute;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  transform: translate(-50%, -50%);
  pointer-events: none;
  z-index: 15;
  box-shadow: 0 0 0 0 color-mix(in srgb, var(--spark-color) 70%, #fff 30%);
  animation: sparkBurst 260ms ease-out forwards;
}

.impact-spark::before,
.impact-spark::after {
  content: "";
  position: absolute;
  inset: -8px;
  border-radius: 50%;
  border: 2px solid color-mix(in srgb, var(--spark-color) 72%, #fff 28%);
  opacity: 0.85;
}

.impact-spark::after {
  inset: -14px;
  border-width: 1px;
  opacity: 0.65;
}

.impact-spark.type-0 { --spark-color: #ff2d6f; }
.impact-spark.type-1 { --spark-color: #ffca28; }
.impact-spark.type-2 { --spark-color: #00bcd4; }
.impact-spark.type-3 { --spark-color: #7cb342; }
.impact-spark.type-4 { --spark-color: #8e24aa; }
.impact-spark.type-5 { --spark-color: #ff7043; }

.board.combo-impact {
  animation: boardPunch 180ms ease-out;
}

body.colorblind .type-0 {
  --ball-base: #ff6f00;
  --ball-mid: #ffa33a;
  --ball-shadow: #bf4f00;
  --ball-highlight: #ffe8cb;
  --ball-rim: #9e4300;
}

body.colorblind .type-1 {
  --ball-base: #1e88e5;
  --ball-mid: #54a8ef;
  --ball-shadow: #0f5da8;
  --ball-highlight: #e0f0ff;
  --ball-rim: #0a4c8b;
}

body.colorblind .type-2 {
  --ball-base: #8e24aa;
  --ball-mid: #ac4bc6;
  --ball-shadow: #66187f;
  --ball-highlight: #f3e2fb;
  --ball-rim: #531168;
}

body.colorblind .type-3 {
  --ball-base: #00897b;
  --ball-mid: #35a79b;
  --ball-shadow: #066157;
  --ball-highlight: #dcf8f4;
  --ball-rim: #03574f;
}

body.colorblind .type-4 {
  --ball-base: #c62828;
  --ball-mid: #db5757;
  --ball-shadow: #8c1b1b;
  --ball-highlight: #ffe2e2;
  --ball-rim: #771313;
}

body.colorblind .type-5 {
  --ball-base: #546e7a;
  --ball-mid: #78909b;
  --ball-shadow: #34434b;
  --ball-highlight: #e3ecf0;
  --ball-rim: #2b3940;
}

.overlay {
  position: fixed;
  inset: 0;
  background: #140d17a8;
  display: grid;
  place-items: center;
  z-index: 25;
  padding: 14px;
}

.overlay.hidden {
  display: none;
}

.modal {
  width: min(460px, 100%);
  background: #fff8f2;
  border-radius: 16px;
  border: 2px solid #fff;
  box-shadow: 0 14px 30px #00000040;
  padding: 16px;
}

.modal h2 {
  margin: 0 0 8px;
}

.modal p {
  margin: 0 0 10px;
  font-weight: 600;
}

.modal-stats {
  background: #fff;
  border-radius: 10px;
  border: 1px solid #efefef;
  padding: 10px;
  margin-bottom: 10px;
  font-weight: 600;
  line-height: 1.5;
}

@keyframes rollDown {
  from {
    transform: translateY(calc(-1 * var(--fall-distance, 0px))) rotate(calc(-1 * var(--fall-turns, 0turn)));
  }
  to {
    transform: translateY(0) rotate(0turn);
  }
}

@keyframes popMatch {
  0% {
    transform: scale(1);
    opacity: 1;
  }
  40% {
    transform: scale(1.16) rotate(0.08turn);
    opacity: 1;
    filter: brightness(1.25);
  }
  100% {
    transform: scale(0.2) rotate(0.22turn);
    opacity: 0;
    filter: brightness(1.6);
  }
}

@keyframes floatUp {
  from {
    transform: translateY(0);
    opacity: 1;
  }
  to {
    transform: translateY(-28px);
    opacity: 0;
  }
}

@keyframes swapPulse {
  from { transform: scale(1); }
  50% { transform: scale(1.13); }
  to { transform: scale(1); }
}

@keyframes sparkBurst {
  0% {
    opacity: 1;
    transform: translate(-50%, -50%) scale(0.4);
  }
  100% {
    opacity: 0;
    transform: translate(-50%, -50%) scale(2.3);
  }
}

@keyframes boardPunch {
  0% { transform: scale(1); }
  35% { transform: scale(1.012); }
  100% { transform: scale(1); }
}

@keyframes hintPulse {
  0% { transform: scale(1); }
  50% { transform: scale(1.04); }
  100% { transform: scale(1); }
}

body.reduced-motion * {
  animation-duration: 0.01ms !important;
  animation-iteration-count: 1 !important;
  transition-duration: 0.01ms !important;
}

@media (max-width: 760px) {
  .stats {
    grid-template-columns: repeat(2, minmax(110px, 1fr));
  }

  .app {
    padding: 14px;
  }

  .board {
    gap: 4px;
    padding: 6px;
  }

  .cell {
    border-radius: 10px;
  }
}

@media (max-width: 520px) {
  body {
    padding: 12px;
    padding-top: max(12px, env(safe-area-inset-top));
    padding-right: max(12px, env(safe-area-inset-right));
    padding-bottom: max(12px, env(safe-area-inset-bottom));
    padding-left: max(12px, env(safe-area-inset-left));
  }

  .app {
    padding: 14px;
    border-radius: 18px;
  }

  .controls {
    align-items: stretch;
  }

  .btn-row {
    width: 100%;
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .btn-row button:last-child:nth-child(odd) {
    grid-column: 1 / -1;
  }

  #message {
    width: 100%;
    text-align: center;
  }

  .board {
    width: min(96vw, 440px);
    height: min(96vw, 440px);
    min-width: 0;
    min-height: 0;
    gap: 4px;
    padding: 6px;
  }

  .cell {
    border-radius: 10px;
  }

  .modal {
    padding: 14px;
  }
}

@media (max-width: 390px) {
  button {
    padding: 10px 12px;
  }

  .stat {
    padding: 8px 10px;
  }

  .goals {
    padding: 8px;
  }
}
