:root {
  --ground: #eceef4;
  --well: #d9dce7;
  --well-edge: #c6cad8;
  --ink: #1b1e2b;
  --muted: #5d6278;
  --mark: #1b1e2b;
  --scrim: rgba(236, 238, 244, 0.9);

  --red: #e5484d;
  --yellow: #f0b90b;
  --green: #2d9d63;
  --blue: #3b78f0;
  --purple: #8e4ec6;
  --gray: #6b7289;
  --arrow-light: #ffffff;
  --arrow-dark: #3b2a00;

  --display: "Rubik", ui-rounded, "Segoe UI", system-ui, sans-serif;
  --body: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;

  --cell: 48px;
  --pop-ms: 140ms;
  --shift-ms: 220ms;
  --clear-ms: 220ms;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --ground: #13151d;
    --well: #1e212d;
    --well-edge: #2b2f3f;
    --ink: #e8e9f1;
    --muted: #979cb0;
    --mark: #ffffff;
    --scrim: rgba(19, 21, 29, 0.9);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --ground: #13151d;
  --well: #1e212d;
  --well-edge: #2b2f3f;
  --ink: #e8e9f1;
  --muted: #979cb0;
  --mark: #ffffff;
  --scrim: rgba(19, 21, 29, 0.9);
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--ground);
  color: var(--ink);
  font: 14px/1.4 var(--body);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

[hidden] { display: none !important; }

.app {
  height: 100%;
  max-width: 560px;
  margin: 0 auto;
  padding-inline: 16px;
  padding-block: max(12px, env(safe-area-inset-top, 0px)) max(12px, env(safe-area-inset-bottom, 0px));
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* Header */

.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.wordmark {
  margin: 0;
  font: 800 22px/1 var(--display);
  letter-spacing: -0.02em;
}

.wordmark span {
  margin-left: 0.18em;
  color: var(--muted);
  font-weight: 500;
}

button {
  font: 700 14px/1 var(--display);
  color: var(--ground);
  background: var(--ink);
  border: 0;
  border-radius: 999px;
  padding: 10px 18px;
  cursor: pointer;
  touch-action: manipulation;
}

button.ghost {
  color: var(--ink);
  background: transparent;
  box-shadow: inset 0 0 0 1.5px var(--well-edge);
}

button:disabled {
  opacity: 0.4;
  cursor: default;
}

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

button:focus-visible {
  outline: 3px solid var(--blue);
  outline-offset: 2px;
}

/* Stats */

.stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.stat {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.stat .label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
}

.stat .value {
  font: 700 26px/1.1 var(--display);
  font-variant-numeric: tabular-nums;
}

/* Board */

.board-wrap {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
}

.frame {
  position: relative;
  padding: 6px;
  border-radius: 18px;
  background: var(--well);
  box-shadow: inset 0 0 0 1px var(--well-edge);
}

.board {
  position: relative;
  overflow: hidden;
  border-radius: 12px;
  cursor: pointer;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}

.tile {
  position: absolute;
  left: 0;
  top: 0;
  width: var(--cell);
  height: var(--cell);
  padding: calc(var(--cell) * 0.05);
  transition: transform var(--shift-ms) cubic-bezier(0.3, 0.8, 0.4, 1);
  will-change: transform;
}

.face {
  width: 100%;
  height: 100%;
  display: grid;
  place-items: center;
  border-radius: 24%;
  background: var(--tile);
  color: var(--arrow);
  --face-shadow: inset 0 calc(var(--cell) * -0.06) 0 rgba(0, 0, 0, 0.18);
  box-shadow: var(--face-shadow);
  transition: transform var(--clear-ms) ease-in, opacity var(--clear-ms) ease-in;
}

.face svg {
  width: 56%;
  height: 56%;
  display: block;
}

.c-red { --tile: var(--red); --arrow: var(--arrow-light); }
.c-yellow { --tile: var(--yellow); --arrow: var(--arrow-dark); }
.c-green { --tile: var(--green); --arrow: var(--arrow-light); }
.c-blue { --tile: var(--blue); --arrow: var(--arrow-light); }
.c-purple { --tile: var(--purple); --arrow: var(--arrow-light); }
.c-gray { --tile: var(--gray); --arrow: var(--arrow-light); }

.d-down svg { transform: rotate(180deg); }
.d-left svg { transform: rotate(-90deg); }
.d-right svg { transform: rotate(90deg); }

.tile.marked .face {
  box-shadow: var(--face-shadow), 0 0 0 3px var(--mark);
  animation: throb 520ms ease-in-out infinite alternate;
}

@keyframes throb {
  from { transform: scale(1); }
  to { transform: scale(0.86); }
}

.tile.popping .face,
.tile.clearing .face {
  animation: none;
  transform: scale(0.2) rotate(-20deg);
  opacity: 0;
}

.tile.popping .face { transition-duration: var(--pop-ms); }

.tile.appearing .face {
  transform: scale(0.2);
  opacity: 0;
  transition: none;
}

.tile.turning svg {
  transform: rotate(90deg);
  transition: transform var(--shift-ms) ease-out;
}

.tile.refused .face { animation: refuse 260ms ease-in-out; }

.tile.hinted .face {
  box-shadow: var(--face-shadow), inset 0 0 0 3px var(--mark);
}

.tile.hinted svg { animation: hint 700ms ease-in-out infinite alternate; }

@keyframes hint {
  to { scale: 1.22; }
}

@keyframes refuse {
  20%, 60% { transform: translateX(-9%); }
  40%, 80% { transform: translateX(9%); }
}

.toast {
  position: absolute;
  left: 50%;
  top: 50%;
  translate: -50% -50%;
  padding: 10px 16px;
  border-radius: 999px;
  background: var(--ink);
  color: var(--ground);
  font: 800 20px/1 var(--display);
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  scale: 0.8;
  transition: opacity 160ms ease, scale 160ms ease;
}

.toast.show {
  opacity: 1;
  scale: 1;
}

.over {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 16px;
  border-radius: 18px;
  background: var(--scrim);
  text-align: center;
}

.over p { margin: 0; }
.over-title { font: 700 16px/1.2 var(--display); color: var(--muted); }
.over-score { font: 800 40px/1.1 var(--display); font-variant-numeric: tabular-nums; }
.over-note { min-height: 1.4em; color: var(--muted); margin-bottom: 8px !important; }

/* Clear-order track */

.track {
  flex: none;
  position: relative;
  padding: 6px;
  border-radius: 14px;
  background: var(--well);
  box-shadow: inset 0 0 0 1px var(--well-edge);
}

.order {
  margin: 0;
  padding: 0;
  list-style: none;
  display: flex;
  gap: 10px;
}

.order li {
  --cell: 34px;
  width: 34px;
  height: 34px;
}

.order .face {
  border-radius: 9px;
  --face-shadow: none;
}

.order li { transition: opacity 400ms ease, scale 400ms ease; }

.order li.gone {
  opacity: 0.15;
  scale: 0.7;
}

.cursor {
  position: absolute;
  top: 6px;
  left: 0;
  width: 34px;
  height: 34px;
  border-radius: 9px;
  box-shadow: 0 0 0 3px var(--mark);
  opacity: 0;
  transition-property: transform, opacity;
  transition-timing-function: ease-in-out, ease;
  pointer-events: none;
}

.cursor.on { opacity: 1; }

/* Particles thrown out by cleared tiles */

.fx {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: visible;
}

.p {
  position: absolute;
  width: 6px;
  height: 6px;
  margin: -3px 0 0 -3px;
  border-radius: 50%;
  background: var(--tile);
  animation: burst 600ms cubic-bezier(0.2, 0.7, 0.3, 1) forwards;
}

@keyframes burst {
  to {
    transform: translate(var(--dx), var(--dy)) rotate(var(--rot)) scale(0.3);
    opacity: 0;
  }
}

/* Sound and theme controls */

.settings {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

button.icon {
  width: 36px;
  height: 36px;
  padding: 0;
  display: grid;
  place-items: center;
}

button.icon svg { width: 20px; height: 20px; }

.skins {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 88px));
  gap: 6px;
}

.skin-chip {
  padding: 8px 6px;
  white-space: nowrap;
  border-radius: 10px;
  font-size: 12px;
  font-weight: 700;
  color: var(--chip-ink);
  background: var(--chip-bg);
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.12);
}

.skin-chip[aria-checked="true"] {
  box-shadow: 0 0 0 2px var(--ground), 0 0 0 4px var(--ink);
}

@media (prefers-reduced-motion: reduce) {
  .tile.marked .face { animation: none; }
  .tile.hinted svg { animation: none; }
}
