:root {
  color-scheme: light;
  --bg: #eef8f5;
  --page-background:
    radial-gradient(circle at top, rgba(108, 197, 204, 0.22), transparent 36%),
    linear-gradient(160deg, #f8fbf6 0%, #e7f5f7 48%, #e9f7ef 100%);
  --grid-line: rgba(21, 86, 101, 0.08);
  --grid-opacity: 0.55;
  --panel: rgba(250, 255, 252, 0.94);
  --panel-soft: #e8f5f3;
  --ink: #15343d;
  --muted: #42646a;
  --line: #9bc7c4;
  --card-back: #2f8798;
  --card-back-shadow: #1e6270;
  --card-face: #fffdf6;
  --card-icon: #15343d;
  --accent: #147c69;
  --accent-strong: #0e5e51;
  --secondary: #2f79b8;
  --secondary-strong: #215984;
  --button-ink: #ffffff;
  --match-bg: #def7e9;
  --match: #0c6b4f;
  --focus: #2fbf83;
  --shadow: rgba(31, 78, 89, 0.16);
}

body.dark {
  color-scheme: dark;
  --bg: #08141d;
  --page-background:
    radial-gradient(circle at top, rgba(125, 228, 255, 0.16), transparent 35%),
    linear-gradient(160deg, #061018 0%, #0d2230 48%, #09141d 100%);
  --grid-line: rgba(255, 255, 255, 0.025);
  --grid-opacity: 0.35;
  --panel: rgba(9, 26, 38, 0.92);
  --panel-soft: rgba(15, 37, 52, 0.92);
  --ink: #edf7ff;
  --muted: #9db8c7;
  --line: rgba(131, 220, 255, 0.36);
  --card-back: #123146;
  --card-back-shadow: #061018;
  --card-face: #0f2a38;
  --card-icon: #d9f7ff;
  --accent: #7de4ff;
  --accent-strong: #2fb9d8;
  --secondary: #7de4ff;
  --secondary-strong: #2b8ea8;
  --button-ink: #061018;
  --match-bg: #063827;
  --match: #86efac;
  --focus: #ffd166;
  --shadow: rgba(0, 0, 0, 0.34);
}

* {
  box-sizing: border-box;
}

body {
  min-height: 100vh;
  margin: 0;
  display: block;
  background: var(--page-background);
  color: var(--ink);
  font-family: Arial, Helvetica, sans-serif;
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  background:
    linear-gradient(var(--grid-line) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid-line) 1px, transparent 1px);
  background-size: 28px 28px;
  opacity: var(--grid-opacity);
}

button {
  font: inherit;
}

.game-shell {
  display: grid;
  width: 100%;
  min-height: 100vh;
  margin: 0;
  padding: clamp(12px, 2vw, 24px);
  grid-template-columns: clamp(260px, 24vw, 320px) minmax(0, 1fr);
  gap: clamp(18px, 3vw, 34px);
  align-items: start;
}

.side-panel {
  position: sticky;
  top: 24px;
  display: grid;
  gap: 14px;
}

.topbar,
.stats {
  display: grid;
  gap: 16px;
}

.topbar,
.panel-section,
.stats,
.game-result {
  padding: 16px;
  border: 2px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: 0 8px 20px var(--shadow);
}

.eyebrow {
  margin: 0 0 6px;
  color: var(--accent);
  font-size: 0.82rem;
  font-weight: 700;
  letter-spacing: 0;
  text-transform: uppercase;
}

.site-back {
  display: inline-flex;
  margin-bottom: 14px;
  color: var(--accent);
  font-weight: 700;
  text-decoration: none;
}

.site-back:hover,
.site-back:focus-visible {
  color: var(--focus);
  outline: none;
}

h1 {
  margin: 0;
  font-size: clamp(1.65rem, 3vw, 2.45rem);
  line-height: 1.05;
  letter-spacing: 0;
}

.restart-button {
  width: 100%;
  border: 0;
  border-radius: 8px;
  padding: 12px 16px;
  background: var(--secondary);
  color: var(--button-ink);
  cursor: pointer;
  font-weight: 700;
  box-shadow: 0 5px 0 var(--secondary-strong);
  transition: transform 140ms ease, box-shadow 140ms ease;
}

.top-actions {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 48px 48px;
  gap: 10px;
}

.restart-button:hover {
  transform: translateY(-1px);
}

.restart-button:active {
  transform: translateY(4px);
  box-shadow: 0 1px 0 var(--secondary-strong);
}

.panel-section {
  display: grid;
  gap: 14px;
}

.panel-section.is-muted {
  opacity: 0.72;
}

.panel-section.is-hidden {
  display: none;
}

.mode-buttons {
  display: grid;
  gap: 8px;
}

.pairs-mode-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 48px;
  gap: 8px;
}

.icon-mode-button {
  display: grid;
  padding: 0;
  place-items: center;
  font-size: 1.35rem;
  text-align: center;
}

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

.size-buttons .mode-button {
  text-align: center;
}

.grid-controls {
  display: grid;
  gap: 8px;
}

.stepper {
  display: grid;
  grid-template-columns: 38px minmax(0, 1fr) 38px;
  align-items: center;
  gap: 8px;
}

.stepper strong {
  text-align: center;
}

.stepper-button {
  display: grid;
  min-height: 36px;
  padding: 0;
  place-items: center;
  font-size: 1.35rem;
  line-height: 1;
}

.stepper-button span {
  display: block;
  transform: translateY(-1px);
}

.mode-button,
.small-button,
.icon-button {
  border: 2px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  background: var(--panel-soft);
  color: var(--ink);
  cursor: pointer;
  font-weight: 700;
  text-align: left;
}

.mode-button.is-active {
  border-color: var(--accent);
  background: var(--accent);
  color: var(--button-ink);
}

.small-button {
  width: 100%;
  padding: 8px 10px;
  text-align: center;
}

.icon-button {
  display: grid;
  min-width: 48px;
  padding: 0;
  place-items: center;
  background: var(--secondary);
  color: var(--button-ink);
  font-size: 1.35rem;
  text-align: center;
}

.icon-button[aria-pressed="true"] {
  border-color: var(--secondary);
  background: var(--secondary);
  color: var(--button-ink);
}

#minimizeUiButton[aria-pressed="true"],
#infinitePairsButton[aria-pressed="true"] {
  border-color: var(--accent);
  background: var(--accent);
  color: var(--button-ink);
}

.stats {
  grid-template-columns: 1fr 1fr;
}

.game-result {
  display: grid;
  gap: 12px;
  width: min(280px, 100%);
  border-color: var(--accent);
}

.game-result.is-hidden {
  display: none;
}

.game-result p {
  margin: 0;
  font-weight: 800;
}

.stats div {
  min-width: 74px;
}

.stat-action {
  grid-column: 1 / -1;
}

.stat-label {
  display: block;
  margin-bottom: 3px;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0;
  text-transform: uppercase;
}

.stats strong {
  font-size: 1.4rem;
}

.play-area {
  position: relative;
  display: grid;
  justify-items: center;
  padding-top: 4px;
}

.play-area .game-result {
  position: fixed;
  top: 24px;
  right: 24px;
  z-index: 3;
}

.board {
  display: grid;
  width: min(100%, calc(var(--grid-columns, 6) * var(--card-width, 56px) + (var(--grid-columns, 6) - 1) * 10px));
  margin: 0 auto;
  grid-template-columns: repeat(var(--grid-columns, 6), 1fr);
  gap: 10px;
}

.board[data-columns="6"] {
  grid-template-columns: repeat(var(--grid-columns, 6), 1fr);
}

.board[data-card-size="small"] {
  --card-width: 56px;
}

.board[data-card-size="medium"] {
  --card-width: 82px;
}

.board[data-card-size="large"] {
  --card-width: 112px;
}

.card {
  aspect-ratio: 5 / 7;
  width: 100%;
  border: 0;
  border-radius: 8px;
  padding: 0;
  background: transparent;
  cursor: pointer;
  perspective: 900px;
}

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

.card-inner {
  position: relative;
  display: block;
  width: 100%;
  height: 100%;
  border-radius: 8px;
  transform-style: preserve-3d;
  transition: transform 260ms ease;
}

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

.card-face {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  border-radius: 8px;
  backface-visibility: hidden;
  border: 2px solid var(--ink);
  box-shadow: 0 8px 0 var(--shadow);
}

.card-back {
  background: var(--card-back);
  box-shadow: inset 0 -8px 0 var(--card-back-shadow), 0 8px 0 var(--shadow);
}

.card-back::before {
  content: "?";
  color: #ffffff;
  font-size: clamp(1.4rem, 5vw, 2.5rem);
  font-weight: 800;
}

.card-front {
  background: var(--card-face);
  color: var(--card-icon);
  font-size: clamp(1.55rem, 4vw, 3rem);
  transform: rotateY(180deg);
}

.card-image {
  display: block;
  width: 76%;
  height: 76%;
  object-fit: contain;
}

.card.is-matched .card-front {
  background: var(--match-bg);
  color: var(--match);
}

.card.is-selected .card-face {
  border-color: var(--focus);
  box-shadow: 0 0 0 4px rgba(34, 197, 94, 0.35), 0 8px 0 var(--shadow);
}

.card.is-just-matched .card-front {
  animation: correct-pop 520ms ease;
}

.card.is-matched {
  cursor: default;
}

.status {
  min-height: 24px;
  margin: 18px 0 0;
  color: var(--ink);
  font-weight: 700;
  text-align: center;
}

.status.is-success {
  color: var(--match);
}

.status.is-error {
  color: #b45309;
}

.status.is-win {
  color: var(--accent);
}

body.ui-minimized .game-shell {
  grid-template-columns: 1fr;
  padding-top: 82px;
  padding-bottom: 94px;
}

body.ui-minimized .side-panel {
  position: static;
  display: block;
}

body.ui-minimized .topbar {
  position: fixed;
  top: 12px;
  right: 12px;
  z-index: 5;
  width: min(360px, calc(100vw - 24px));
  padding: 10px;
  box-shadow: 0 8px 20px var(--shadow);
}

body.ui-minimized .topbar > div:first-child {
  display: none;
}

body.ui-minimized .top-actions {
  grid-template-columns: minmax(0, 1fr) 44px 44px;
}

body.ui-minimized .restart-button {
  padding: 10px 12px;
}

body.ui-minimized .icon-button {
  min-width: 0;
}

body.ui-minimized .stats {
  position: fixed;
  right: 12px;
  bottom: 12px;
  left: 12px;
  z-index: 5;
  display: grid;
  grid-template-columns: repeat(5, minmax(74px, 1fr));
  gap: 10px;
  align-items: center;
  padding: 10px;
  box-shadow: 0 8px 20px var(--shadow);
}

body.ui-minimized .panel-section {
  display: none;
}

body.ui-minimized .stat-action {
  display: none;
}

body.ui-minimized .stats strong {
  font-size: 1.05rem;
}

body.ui-minimized .play-area {
  min-height: calc(100vh - 176px);
}

body.ui-minimized .play-area .game-result {
  top: 88px;
  right: 12px;
}

@keyframes correct-pop {
  0%,
  100% {
    transform: rotateY(180deg) scale(1);
  }

  45% {
    transform: rotateY(180deg) scale(1.08);
  }
}

@media (max-width: 860px) {
  .game-shell {
    grid-template-columns: 1fr;
    min-height: auto;
  }

  .side-panel {
    position: static;
  }

  .play-area {
    width: 100%;
  }

  body.ui-minimized .side-panel {
    position: static;
  }

  body.ui-minimized .stats {
    grid-template-columns: repeat(3, minmax(74px, 1fr));
  }

  body.ui-minimized .stat-action {
    display: none;
  }
}

@media (max-width: 560px) {
  .game-shell {
    padding: 12px;
  }

  h1 {
    font-size: 1.8rem;
  }

  .mode-buttons {
    grid-template-columns: 1fr;
  }

  .stats {
    gap: 10px;
    padding: 12px;
  }

  .stats div {
    min-width: calc(50% - 10px);
  }

  .stats strong {
    font-size: 1.16rem;
  }

  .play-area .game-result {
    position: static;
    width: 100%;
    margin-bottom: 16px;
  }

  body.ui-minimized .game-shell {
    padding-top: 78px;
    padding-bottom: 172px;
  }

  body.ui-minimized .top-actions {
    grid-template-columns: minmax(0, 1fr) 42px 42px;
  }

  body.ui-minimized .stats {
    grid-template-columns: repeat(2, minmax(72px, 1fr));
    gap: 8px;
  }
}
