:root {
  color-scheme: dark;
  --night: #1c1828;
  --panel: #29243a;
  --ink: #f1eaf5;
  --mist: #b9adc8;
  --dim: #9387a2;
  --violet: #b99bd3;
  --sakura: #e7bfd6;
  --line: rgba(233, 220, 245, .15);
  --sans: system-ui, -apple-system, "Noto Sans TC", sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  min-height: 100vh;
  margin: 0;
  background: radial-gradient(ellipse at 50% 0%, #302640 0, var(--night) 64%);
  color: var(--ink);
  font-family: var(--sans);
  line-height: 1.6;
  padding: 24px 18px 48px;
}

a {
  color: inherit;
  text-decoration: none;
}

[hidden] {
  display: none !important;
}

button,
select {
  font: inherit;
  color: inherit;
}

.shell {
  width: min(100%, 620px);
  margin: 0 auto;
}

.game2048 .shell {
  width: min(100%, 490px);
}

.game-mines.expert .shell {
  width: min(100%, 900px);
}

.top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 0 0 24px;
  color: var(--mist);
  font-size: .9rem;
}

.back {
  color: var(--sakura);
}

.back:hover {
  text-decoration: underline;
}

.brand {
  letter-spacing: .08em;
}

.intro {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 18px;
  margin-bottom: 20px;
}

.eyebrow {
  color: var(--violet);
  font-size: .75rem;
  letter-spacing: .22em;
}

.intro h1 {
  margin: 2px 0 0;
  font-size: clamp(2rem, 8vw, 3rem);
  line-height: 1.15;
  letter-spacing: .04em;
}

.intro p {
  margin: 8px 0 0;
  color: var(--mist);
  font-size: .92rem;
}

.panel {
  background: rgba(37, 32, 51, .82);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: clamp(16px, 5vw, 26px);
  box-shadow: 0 18px 55px rgba(0, 0, 0, .2);
}

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

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

.stat {
  min-width: 76px;
  padding: 5px 12px;
  background: rgba(18, 15, 27, .42);
  border: 1px solid var(--line);
  border-radius: 10px;
  text-align: center;
}

.stat small {
  display: block;
  color: var(--dim);
  font-size: .68rem;
  letter-spacing: .1em;
}

.stat strong {
  font-size: 1.1rem;
  font-variant-numeric: tabular-nums;
}

.button {
  border: 1px solid rgba(185, 155, 211, .48);
  border-radius: 9px;
  background: rgba(185, 155, 211, .14);
  padding: 9px 13px;
  cursor: pointer;
  transition: background .18s, border-color .18s;
}

.button:hover {
  background: rgba(185, 155, 211, .25);
  border-color: var(--violet);
}

.button:focus-visible,
select:focus-visible,
a:focus-visible {
  outline: 2px solid var(--sakura);
  outline-offset: 3px;
}

.hint {
  color: var(--dim);
  font-size: .82rem;
  text-align: center;
  margin: 14px 0 0;
}

.toast {
  min-height: 1.5em;
  color: var(--sakura);
  font-size: .9rem;
  text-align: center;
  margin: 10px 0 0;
}

.board {
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.footer {
  margin-top: 20px;
  text-align: center;
  color: var(--dim);
  font-size: .78rem;
}

.footer a {
  color: var(--mist);
}

/* ---------- 2048 ---------- */
.game2048 .board {
  --board-pad: clamp(8px, 2.4vw, 12px);
  --board-gap: clamp(8px, 2.2vw, 12px);
  position: relative;
  width: 100%;
  aspect-ratio: 1 / 1;
  padding: var(--board-pad);
  background: #171421;
  border-radius: 13px;
  overflow: hidden;
}

.game2048 .board-slots {
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  grid-template-rows: repeat(4, minmax(0, 1fr));
  gap: var(--board-gap);
}

.game2048 .slot {
  min-width: 0;
  min-height: 0;
  border-radius: 9px;
  background: #393247;
}

.game2048 .tile-layer {
  position: absolute;
  inset: var(--board-pad);
  pointer-events: none;
}

.game2048 .tile {
  position: absolute;
  top: 0;
  left: 0;
  width: calc((100% - 3 * var(--board-gap)) / 4);
  height: calc((100% - 3 * var(--board-gap)) / 4);
  transform: translate(
    calc(var(--c, 0) * (100% + var(--board-gap))),
    calc(var(--r, 0) * (100% + var(--board-gap)))
  );
  transition: transform 110ms ease-out;
  z-index: 2;
}

.game2048 .tile.ghost {
  z-index: 1;
}

.game2048 .tile-inner {
  width: 100%;
  height: 100%;
  display: grid;
  place-items: center;
  border-radius: 9px;
  background: #393247;
  color: #bcb3c7;
  font-weight: 700;
  line-height: 1;
  font-size: clamp(1.35rem, 6.4vw, 2.25rem);
  font-variant-numeric: tabular-nums;
  overflow: hidden;
}

.game2048 .tile-inner[data-v="2"] {
  background: #eee4da;
  color: #554b62;
}

.game2048 .tile-inner[data-v="4"] {
  background: #e8d8c8;
  color: #554b62;
}

.game2048 .tile-inner[data-v="8"] {
  background: #e8ad78;
  color: #fff;
}

.game2048 .tile-inner[data-v="16"] {
  background: #e58b61;
  color: #fff;
}

.game2048 .tile-inner[data-v="32"] {
  background: #dc705c;
  color: #fff;
}

.game2048 .tile-inner[data-v="64"] {
  background: #cf554a;
  color: #fff;
}

.game2048 .tile-inner[data-v="128"],
.game2048 .tile-inner[data-v="256"],
.game2048 .tile-inner[data-v="512"] {
  background: #c9a152;
  color: #fff;
  font-size: clamp(1.1rem, 5.2vw, 1.85rem);
}

.game2048 .tile-inner[data-v="1024"],
.game2048 .tile-inner[data-v="2048"] {
  background: #b994d1;
  color: #fff;
  font-size: clamp(.9rem, 4.1vw, 1.45rem);
  box-shadow: 0 0 20px rgba(185, 148, 209, .28);
}

.game2048 .tile-inner[data-v="4096"],
.game2048 .tile-inner[data-v="8192"] {
  background: #9f75c2;
  color: #fff;
  font-size: clamp(.9rem, 4.1vw, 1.45rem);
  box-shadow: 0 0 22px rgba(159, 117, 194, .34);
}

.game2048 .tile-inner[data-super="1"] {
  background: #7f58a6;
  color: #fff;
  font-size: clamp(.75rem, 3.3vw, 1.15rem);
  box-shadow: 0 0 24px rgba(127, 88, 166, .38);
}

.game2048 .tile.new .tile-inner {
  animation: pop .15s ease-out both;
}

.game2048 .tile.merged .tile-inner {
  animation: merge-pop .16s ease-out;
}

@keyframes pop {
  from {
    transform: scale(.6);
    opacity: 0;
  }
  to {
    transform: scale(1);
    opacity: 1;
  }
}

@keyframes merge-pop {
  0% {
    transform: scale(.86);
  }
  55% {
    transform: scale(1.1);
  }
  100% {
    transform: scale(1);
  }
}

.pad {
  display: grid;
  grid-template-columns: repeat(3, 54px);
  grid-template-rows: repeat(2, 46px);
  gap: 5px;
  justify-content: center;
  margin: 16px auto 0;
}

.pad-button {
  border: 1px solid rgba(185, 155, 211, .42);
  border-radius: 10px;
  background: rgba(185, 155, 211, .13);
  color: var(--ink);
  font-size: 1.25rem;
  cursor: pointer;
  touch-action: manipulation;
}

.pad-button:hover,
.pad-button:active {
  background: rgba(185, 155, 211, .3);
  border-color: var(--violet);
}

.pad-button.up {
  grid-column: 2;
  grid-row: 1;
}

.pad-button.left {
  grid-column: 1;
  grid-row: 2;
}

.pad-button.down {
  grid-column: 2;
  grid-row: 2;
}

.pad-button.right {
  grid-column: 3;
  grid-row: 2;
}

.pad-button:focus-visible {
  outline: 2px solid var(--sakura);
  outline-offset: 2px;
}

/* ---------- Minesweeper ---------- */
.mine-controls {
  flex-wrap: wrap;
}

.mine-select {
  background: #211d2c;
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 8px 10px;
  min-width: 126px;
}

.mine-wrap {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.mine-board {
  display: grid;
  gap: 3px;
  width: 100%;
  margin: 0 auto;
  touch-action: manipulation;
}

.mine-board[data-level="beginner"] {
  max-width: 396px;
}

.mine-board[data-level="intermediate"] {
  min-width: 352px;
  max-width: 520px;
}

.mine-board[data-level="expert"] {
  min-width: 720px;
  max-width: 848px;
}

.cell {
  display: grid;
  place-items: center;
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
  padding: 0;
  aspect-ratio: 1;
  border: 1px solid rgba(233, 220, 245, .13);
  border-radius: 4px;
  background: #50475f;
  color: #fff;
  font-size: clamp(.72rem, 2.6vw, .98rem);
  font-weight: 700;
  line-height: 1;
  overflow: hidden;
  cursor: pointer;
  user-select: none;
}

.cell:hover:not(.open) {
  background: #635873;
}

.cell.open {
  background: #292536;
  border-color: rgba(233, 220, 245, .06);
  cursor: default;
}

.cell.flag {
  color: #f1c9a0;
}

.cell.mine {
  background: #a94e61;
}

.cell.exploded {
  background: #e06c74;
}

.n1 { color: #9dc8ff; }
.n2 { color: #9fe1b1; }
.n3 { color: #ff9b97; }
.n4 { color: #c4a8fa; }
.n5 { color: #f4c779; }
.n6 { color: #8bd8d0; }
.n7 { color: #f5a9d0; }
.n8 { color: #c9bed8; }

.mine-face {
  font-size: 1.25rem;
  line-height: 1;
  padding: 7px 12px;
}

.mine-retry {
  display: flex;
  justify-content: center;
  margin-top: 12px;
}

.mine-retry .button {
  min-width: 140px;
}

.toggle.active {
  background: rgba(231, 191, 214, .24);
  border-color: var(--sakura);
}

@media (max-width: 440px) {
  body {
    padding: 16px 12px 36px;
  }
  .intro {
    align-items: flex-start;
  }
  .controls {
    gap: 7px;
  }
  .stat {
    min-width: 64px;
    padding: 4px 8px;
  }
  .button {
    padding: 8px 10px;
  }
  .mine-board {
    gap: 2px;
  }
  .cell {
    border-radius: 3px;
  }
  .top {
    margin-bottom: 18px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: .01ms !important;
    transition-duration: .01ms !important;
  }
}

/* ---------- Lights Out ---------- */
.lights-board {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  grid-template-rows: repeat(5, minmax(0, 1fr));
  gap: clamp(7px, 2vw, 11px);
  width: min(100%, 440px);
  aspect-ratio: 1 / 1;
  margin: 4px auto 0;
}

.light-button {
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
  padding: 0;
  line-height: 1;
  border: 1px solid rgba(233, 220, 245, .14);
  border-radius: 12px;
  background: #363142;
  color: transparent;
  cursor: pointer;
  transition: background .16s, box-shadow .16s, transform .12s;
  touch-action: manipulation;
}

.light-button:hover:not(:disabled) {
  transform: translateY(-2px);
}

.light-button.on {
  background: #f1c9a0;
  box-shadow: 0 0 24px rgba(241, 201, 160, .3);
}

.light-button:disabled {
  cursor: default;
}

.light-button:focus-visible,
.memory-card:focus-visible,
.reaction-target:focus-visible {
  outline: 2px solid var(--sakura);
  outline-offset: 3px;
}

/* ---------- Memory ---------- */
.memory-board {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  grid-template-rows: repeat(4, minmax(0, 1fr));
  gap: clamp(8px, 2vw, 12px);
  width: min(100%, 440px);
  aspect-ratio: 1 / 1;
  margin: 4px auto 0;
}

.memory-controls {
  flex-wrap: wrap;
}

.memory-card {
  display: grid;
  place-items: center;
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
  padding: 0;
  line-height: 1;
  overflow: hidden;
  border: 1px solid rgba(233, 220, 245, .16);
  border-radius: 12px;
  background: linear-gradient(145deg, #554761, #332d40);
  color: var(--sakura);
  font-size: clamp(1.35rem, 6.4vw, 2.25rem);
  cursor: pointer;
  user-select: none;
  transition: transform .16s, background .16s, border-color .16s;
  touch-action: manipulation;
}

.memory-card:hover:not(:disabled) {
  transform: translateY(-2px);
}

.memory-card.revealed,
.memory-card.matched {
  background: #eee6f2;
  border-color: rgba(231, 191, 214, .52);
  color: #382e43;
}

.memory-card.matched {
  background: #d8c8e4;
  cursor: default;
}

/* ---------- Reaction test ---------- */
.reaction-target {
  display: grid;
  place-items: center;
  width: 100%;
  height: 230px;
  padding: 24px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: #373142;
  color: var(--ink);
  font-size: clamp(1.1rem, 4vw, 1.45rem);
  text-align: center;
  cursor: pointer;
  user-select: none;
  touch-action: manipulation;
  transition: border-color .15s, transform .12s;
}

.reaction-target:hover {
  border-color: rgba(185, 155, 211, .5);
}

.reaction-target.waiting {
  background: #784c63;
  border-color: #a86f8b;
}

.reaction-target.ready {
  background: #9c78bd;
  border-color: #c6a8e2;
  box-shadow: 0 0 34px rgba(185, 148, 209, .28);
}

.game-reaction .hint {
  min-height: 2.8em;
}
