:root {
  --bg: #1f2120;
  --panel: #181c29;
  --text: #fbf3e4;
  --muted: #d9cfbd;
  --violet: #7c47f5;
  --cyan: #27d6c1;
  --yellow: #facc15;
  --pink: #fb7185;
  --lime: #a3e635;
  --orange: #fb923c;
  --face: #1c2135;
  --cols: 4;
  --size: 80px;
  --cw: 80px;
  --ch: 80px;
  --gap: 8px;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  color: var(--text);
  font-family: ui-rounded, "SF Pro Rounded", "Segoe UI", system-ui, sans-serif;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

body {
  background:
    radial-gradient(120% 80% at 10% 0%, rgb(124 58 237 / 0.28), transparent 60%),
    radial-gradient(90% 70% at 100% 100%, rgb(34 211 238 / 0.16), transparent 60%),
    var(--bg);
}

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

.hidden {
  display: none !important;
}

/* ---------- Layout ---------- */

.app {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.hud {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 10px 12px 8px;
}

.stat {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 74px;
  padding: 4px 12px 5px;
  border-radius: 14px;
  background: rgb(255 255 255 / 0.07);
  box-shadow: inset 0 0 0 1px rgb(255 255 255 / 0.06);
}

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

.stat strong {
  font-size: 20px;
  line-height: 1.15;
  font-variant-numeric: tabular-nums;
}

.icon-btn {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  margin-left: 4px;
  padding: 0;
  border: 0;
  border-radius: 14px;
  background: rgb(255 255 255 / 0.1);
  color: var(--text);
  cursor: pointer;
}

.icon-btn svg {
  width: 22px;
  height: 22px;
  fill: currentColor;
}

.icon-btn:hover {
  background: rgb(255 255 255 / 0.16);
}

.board-wrap {
  position: relative;
  flex: 1 1 0;
  min-width: 0;
  min-height: 0;
  margin: 4px 12px 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: none;
}

#board-canvas {
  max-width: 100%;
  max-height: 100%;
  aspect-ratio: 1 / 1;
  border-radius: 18px;
  background: radial-gradient(circle at 50% 50%, #1a2238 0%, #0c101d 100%);
  box-shadow: 0 16px 36px rgba(0, 0, 0, 0.4), inset 0 0 0 1px rgba(255, 255, 255, 0.08);
  cursor: pointer;
  outline: none;
  touch-action: none;
}

#board-canvas:focus-visible {
  box-shadow: 0 0 0 3px #38bdf8, 0 16px 36px rgba(0, 0, 0, 0.5);
}

.menu-col {
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: 100%;
  margin-bottom: 12px;
}

.primary-accent {
  background: linear-gradient(135deg, #f59e0b, #ef4444) !important;
  color: #fff !important;
  font-weight: 700 !important;
  box-shadow: 0 4px 14px rgba(245, 158, 11, 0.35);
}

.panel-wide {
  max-width: 600px !important;
  width: 95% !important;
  max-height: 90vh !important;
  overflow-y: auto !important;
}

.tier-tabs {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  padding-bottom: 8px;
  margin-bottom: 12px;
  width: 100%;
}

.tier-tab {
  flex: 1 0 auto;
  padding: 6px 12px;
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.05);
  color: var(--text);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}

.tier-tab.active {
  background: var(--cyan);
  color: #0b0d12;
  border-color: var(--cyan);
}

.stage-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
  width: 100%;
  margin-bottom: 14px;
}

.stage-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 10px 4px;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 10px;
  color: var(--text);
  font-weight: 700;
  font-size: 14px;
  cursor: pointer;
  transition: transform 0.15s ease, background 0.15s ease;
}

.stage-card:hover {
  background: rgba(255, 255, 255, 0.12);
  transform: translateY(-2px);
}

.stage-card.locked {
  opacity: 0.35;
  cursor: not-allowed;
}

.stage-card.current {
  border-color: var(--cyan);
  box-shadow: 0 0 10px rgba(39, 214, 193, 0.35);
}

.stage-stars {
  font-size: 11px;
  color: #facc15;
  letter-spacing: 1px;
}

.star {
  font-size: 28px;
  color: #facc15;
  margin: 0 3px;
  transition: transform 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.board {
  display: grid;
  grid-template-columns: repeat(var(--cols), var(--cw));
  grid-auto-rows: var(--ch);
  gap: var(--gap);
}

@media (orientation: landscape) and (max-height: 540px) {
  .app {
    flex-direction: row;
  }

  .hud {
    flex-direction: column;
    justify-content: center;
    width: 112px;
    padding: 10px 0 10px 12px;
  }

  .stat {
    width: 100%;
  }

  .icon-btn {
    margin: 4px 0 0;
  }

  .board-wrap {
    margin: 10px 12px;
  }
}

/* ---------- Cards ---------- */

.card {
  --radius: calc(var(--size) * 0.16);
  position: relative;
  width: var(--cw);
  height: var(--ch);
  padding: 0;
  border: 0;
  border-radius: var(--radius);
  background: none;
  color: inherit;
  font: inherit;
  cursor: pointer;
  perspective: 700px;
}

.board.dealing .card {
  animation: deal 0.42s cubic-bezier(0.3, 1.5, 0.5, 1) backwards;
  animation-delay: calc(var(--i, 0) * 18ms);
}

.card:focus {
  outline: none;
}

.card:focus-visible {
  outline: 3px solid var(--cyan);
  outline-offset: 3px;
}

.card-inner {
  position: absolute;
  inset: 0;
  border-radius: inherit;
  transform-style: preserve-3d;
  transition: transform 0.36s cubic-bezier(0.3, 1.3, 0.55, 1);
}

.card.is-open .card-inner,
.card.is-matched .card-inner {
  transform: rotateY(180deg);
}

/* A real card lifts off the table as it turns edge-on, then slaps down with a little
   overshoot; turning back over does the same. */
.card.is-open .card-inner {
  animation: flip-open 0.42s cubic-bezier(0.3, 0.7, 0.4, 1) both;
}

.card.is-closing .card-inner {
  animation: flip-close 0.4s cubic-bezier(0.3, 0.7, 0.4, 1) both;
}

.card-cover,
.card-face {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: inherit;
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
}

.card-cover {
  background:
    radial-gradient(circle at 50% 50%, rgb(255 255 255 / 0.9) 0 10%, transparent 11%),
    radial-gradient(circle at 50% 50%, transparent 0 21%, rgb(255 255 255 / 0.32) 22% 28%, transparent 29%),
    radial-gradient(rgb(255 255 255 / 0.13) 18%, transparent 22%) 0 0 / 16% 16%,
    linear-gradient(150deg, #8b5cf6 0%, #7c47f5 50%, #5b30cf 100%);
  box-shadow:
    inset 0 0 0 2px rgb(255 255 255 / 0.14),
    inset 0 calc(var(--size) * -0.06) 0 rgb(0 0 0 / 0.22),
    0 4px 10px rgb(0 0 0 / 0.3);
}

.card:hover .card-cover {
  filter: brightness(1.12);
}

.card-face {
  transform: rotateY(180deg);
  background: radial-gradient(circle at 50% 35%, #262c45, var(--face) 70%);
  box-shadow:
    inset 0 0 0 2px rgb(255 255 255 / 0.1),
    0 4px 10px rgb(0 0 0 / 0.3);
}

.card-face svg {
  width: 70%;
  height: 70%;
  overflow: visible;
  filter: drop-shadow(0 2px 0 rgb(0 0 0 / 0.35));
}

.card.is-wrong .card-face {
  box-shadow:
    inset 0 0 0 3px var(--pink),
    0 4px 10px rgb(0 0 0 / 0.3);
}

.board .card.is-wrong {
  animation: shake 0.4s 0.3s ease both;
}

.board .card.is-matched {
  cursor: default;
  animation: wobble 0.7s 0.28s ease both;
}

.card.is-matched .card-face {
  box-shadow:
    inset 0 0 0 3px currentColor,
    0 0 18px -4px currentColor;
}

/* A check badge marks matched cards so state never relies on color alone. */
.card.is-matched .card-face::after {
  content: "";
  position: absolute;
  top: 6%;
  right: 6%;
  width: 24%;
  height: 24%;
  min-width: 12px;
  min-height: 12px;
  border-radius: 50%;
  background:
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M6 12.5l4 4 8-9' fill='none' stroke='%230f1117' stroke-width='3.4' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") center / 80% no-repeat,
    var(--lime);
}

@keyframes flip-open {
  0% {
    transform: rotateY(0deg);
  }
  45% {
    transform: translateZ(26px) rotateY(95deg);
  }
  80% {
    transform: translateZ(5px) rotateY(186deg);
  }
  100% {
    transform: rotateY(180deg);
  }
}

@keyframes flip-close {
  0% {
    transform: rotateY(180deg);
  }
  45% {
    transform: translateZ(22px) rotateY(85deg);
  }
  80% {
    transform: translateZ(4px) rotateY(-5deg);
  }
  100% {
    transform: rotateY(0deg);
  }
}

@keyframes deal {
  from {
    opacity: 0;
    transform: translateY(14px) scale(0.6);
  }
}

@keyframes wobble {
  0% {
    transform: none;
  }
  20% {
    transform: rotate(-9deg) scale(1.09);
  }
  40% {
    transform: rotate(7deg) scale(1.06);
  }
  60% {
    transform: rotate(-4deg) scale(1.03);
  }
  80% {
    transform: rotate(2deg);
  }
  100% {
    transform: none;
  }
}

@keyframes shake {
  0%,
  100% {
    transform: none;
  }
  25% {
    transform: translateX(-5%);
  }
  50% {
    transform: translateX(4%);
  }
  75% {
    transform: translateX(-2%);
  }
}

/* ---------- Overlays ---------- */

.overlay {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: flex;
  padding: 16px;
  overflow-y: auto;
  background: rgb(31 33 32 / 0.84);
  -webkit-backdrop-filter: blur(5px);
  backdrop-filter: blur(5px);
}

.panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  width: min(460px, 100%);
  margin: auto;
  padding: clamp(18px, 4vmin, 30px);
  border-radius: 26px;
  text-align: center;
  background: linear-gradient(180deg, #1f2436, var(--panel));
  box-shadow:
    inset 0 0 0 1px rgb(255 255 255 / 0.08),
    0 24px 60px rgb(0 0 0 / 0.45);
}

.panel h1,
.panel h2 {
  margin: 0;
  font-size: clamp(28px, 7vmin, 46px);
  line-height: 1.05;
  letter-spacing: -0.01em;
}

.panel h1 {
  background: linear-gradient(90deg, var(--yellow), var(--pink) 45%, #c084fc);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.panel p {
  margin: 0;
}

.goal {
  color: #d5dae4;
  font-size: 17px;
  line-height: 1.35;
}

.hint {
  color: var(--muted);
  font-size: 14px;
}

.best-line {
  color: var(--muted);
  font-size: 15px;
}

.best-line strong {
  color: var(--text);
}

.best-line span {
  white-space: nowrap;
}

.new-best {
  display: inline-block;
  margin-left: 6px;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--yellow);
  color: #1a1408;
  font-size: 12px;
  font-weight: 800;
}

.logo {
  display: flex;
  gap: 8px;
  height: 52px;
}

.mini {
  display: grid;
  place-items: center;
  width: 42px;
  height: 52px;
  border-radius: 10px;
}

.mini:nth-child(1) {
  transform: rotate(-10deg);
}

.mini:nth-child(2) {
  transform: rotate(-3deg) translateY(-4px);
}

.mini:nth-child(3) {
  transform: rotate(4deg) translateY(-4px);
}

.mini:nth-child(4) {
  transform: rotate(11deg);
}

.mini-back {
  background:
    radial-gradient(circle, rgb(255 255 255 / 0.9) 0 4px, transparent 5px),
    linear-gradient(150deg, #8b5cf6, #5b30cf);
  box-shadow: inset 0 0 0 2px rgb(255 255 255 / 0.15);
}

.mini-face {
  background: var(--face);
  box-shadow: inset 0 0 0 2px currentColor;
}

.mini-face svg {
  width: 30px;
  height: 30px;
}

.choices {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  width: 100%;
  margin-top: 4px;
}

.row {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px;
  margin-top: 4px;
}

.btn {
  min-width: 44px;
  min-height: 52px;
  padding: 10px 24px;
  border: 0;
  border-radius: 999px;
  background: #ee5e24;
  color: #15130f;
  font: inherit;
  font-size: 19px;
  font-weight: 800;
  cursor: pointer;
  box-shadow: 0 5px 0 #b8400f;
  transition:
    transform 0.08s ease,
    box-shadow 0.08s ease,
    filter 0.15s ease;
}

.btn:hover {
  filter: brightness(1.1);
}

.btn:active {
  transform: translateY(3px);
  box-shadow: 0 2px 0 #b8400f;
}

.btn:focus {
  outline: none;
}

.btn:focus-visible,
.icon-btn:focus-visible {
  outline: 3px solid var(--cyan);
  outline-offset: 3px;
}

.btn-alt {
  background: #2b3147;
  box-shadow: 0 5px 0 #161a27;
}

.btn-alt:active {
  box-shadow: 0 2px 0 #161a27;
}

.choice {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 10px 6px;
  border-radius: 18px;
  font-size: 18px;
}

.choice small {
  font-size: 12px;
  font-weight: 700;
  opacity: 0.85;
}

.choice-cyan {
  background: #0891b2;
  box-shadow: 0 5px 0 #155e75;
}

.choice-cyan:active {
  box-shadow: 0 2px 0 #155e75;
}

.choice-pink {
  background: #e11d48;
  box-shadow: 0 5px 0 #881337;
}

.choice-pink:active {
  box-shadow: 0 2px 0 #881337;
}

.stars {
  display: flex;
  gap: 6px;
  height: 56px;
}

.stars svg {
  width: 56px;
  height: 56px;
}

.stars .on {
  fill: var(--yellow);
  stroke: var(--yellow);
  animation: star-pop 0.45s cubic-bezier(0.3, 1.6, 0.5, 1) backwards;
}

.stars .off {
  fill: #2f3650;
  stroke: #2f3650;
}

.stars svg:nth-child(2) {
  animation-delay: 0.15s;
  transform: translateY(-8px);
}

.stars svg:nth-child(3) {
  animation-delay: 0.3s;
}

@keyframes star-pop {
  from {
    opacity: 0;
    transform: scale(0.2) rotate(-40deg);
  }
}

.near-miss {
  margin: -4px 0 6px;
  font-size: 14px;
  font-weight: 800;
  color: #ff6b4a;
  letter-spacing: 0.02em;
  animation: pulse-near 1.6s ease-in-out infinite;
}

@keyframes pulse-near {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.85; transform: scale(1.02); }
}

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

.summary > div {
  display: flex;
  flex-direction: column;
  padding: 8px 4px;
  border-radius: 14px;
  background: rgb(255 255 255 / 0.06);
}

.summary strong {
  font-size: 24px;
  font-variant-numeric: tabular-nums;
}

@media (max-height: 460px) {
  .panel {
    gap: 8px;
    padding: 14px 20px;
  }

  .logo {
    display: none;
  }

  .goal {
    font-size: 15px;
  }

  .stars,
  .stars svg {
    height: 40px;
    width: auto;
  }

  .stars svg {
    width: 40px;
  }

  .summary strong {
    font-size: 20px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .board.dealing .card,
  .board .card.is-matched,
  .board .card.is-wrong,
  .stars .on {
    animation: none;
  }

  .card-inner {
    transition-duration: 0.01s;
  }

  .card.is-open .card-inner,
  .card.is-closing .card-inner {
    animation: none;
  }
}
