:root {
  --page: #1b1c21;
  --surface: #24252c;
  --surface-raised: #2c2d35;
  --ink: #f1f1f4;
  --muted: rgba(241, 241, 244, 0.6);
  --blue: #4da3ff;
  --blue-soft: #8cc4ff;
  --amber: #f2b45c;
  --error: #ff6b6b;
  --thin-line: rgba(255, 255, 255, 0.07);
  --box-line: rgba(255, 255, 255, 0.3);
  --header-height: 58px;
  --footer-height: 24px;
  --viewport-height: 100vh;
}

@supports (height: 100svh) {
  :root {
    --viewport-height: 100svh;
  }
}

* {
  box-sizing: border-box;
}

html {
  overflow-x: hidden;
}

body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  margin: 0;
  background: radial-gradient(ellipse at 50% 20%, #23252e, var(--page) 70%) no-repeat, var(--page);
  background-size: 100% var(--viewport-height), auto;
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  min-height: var(--header-height);
  padding: 10px 20px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
  background: rgba(0, 0, 0, 0.18);
  user-select: none;
}

.header-start {
  display: flex;
  align-items: center;
  gap: 14px;
  min-width: 0;
}

.home-link {
  position: relative;
  display: flex;
  align-items: center;
  padding-right: 14px;
  border-right: 1px solid rgba(255, 255, 255, 0.15);
  transition: opacity 0.15s;
}

.home-link:hover {
  opacity: 0.8;
}

.home-link-mark {
  display: block;
  width: 30px;
  height: 30px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border-radius: 9px;
  background: linear-gradient(135deg, #2fb58f, #1d6f73 60%, #c0463c);
  color: #fff;
}

h1 {
  margin: 0;
  font-size: 19px;
  font-weight: 750;
  white-space: nowrap;
}

.stats {
  display: flex;
  gap: 6px;
  margin: 0;
}

.stat {
  min-width: 70px;
  padding: 5px 10px;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.05);
  text-align: center;
}

.stat-label {
  font-size: 9px;
  font-weight: 700;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  opacity: 0.6;
}

.stat-value {
  margin: 0;
  font-size: 16px;
  font-weight: 750;
  font-variant-numeric: tabular-nums;
}

.controls {
  display: flex;
  align-items: center;
  gap: 8px;
}

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
}

.mode-picker {
  display: flex;
  gap: 2px;
  padding: 3px;
  border-radius: 11px;
  background: rgba(255, 255, 255, 0.05);
}

.mode-option {
  position: relative;
  cursor: pointer;
}

.mode-option input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.mode-option span {
  display: block;
  padding: 6px 11px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 650;
  opacity: 0.7;
  transition: background 0.15s, color 0.15s, opacity 0.15s;
}

.mode-option:hover span {
  opacity: 1;
}

.mode-option input:checked + span {
  background: var(--ink);
  color: #1d1d27;
  opacity: 1;
}

.mode-option input:focus-visible + span {
  outline: 2px solid var(--blue);
  outline-offset: 1px;
}

.new-game-button {
  display: flex;
  align-items: center;
  gap: 7px;
  height: 38px;
  padding: 0 14px;
  border: 0;
  border-radius: 10px;
  background: var(--blue);
  color: #0c1726;
  font-size: 14px;
  font-weight: 700;
  transition: background 0.15s, transform 0.1s;
}

.new-game-button:hover {
  background: #6cb4ff;
}

.new-game-button:active {
  transform: scale(0.96);
}

kbd {
  padding: 1px 5px;
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.12);
  font-family: inherit;
  font-size: 10px;
  font-weight: 600;
}

.ghost-button {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  opacity: 0.85;
}

.ghost-button:hover {
  background: rgba(255, 255, 255, 0.1);
  opacity: 1;
}

.result-overlay {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(10, 11, 14, 0.6);
  backdrop-filter: blur(3px);
  animation: fade-in 0.25s ease-out;
}

.result-overlay[hidden] {
  display: none;
}

.result-card {
  width: min(360px, 100%);
  padding: 26px 24px 22px;
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 18px;
  background: var(--surface-raised);
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.5);
  text-align: center;
  animation: card-rise 0.35s cubic-bezier(0.2, 0.8, 0.25, 1);
}

.result-card h2 {
  margin: 0 0 16px;
  font-size: 22px;
}

.result-stats {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  margin: 0 0 14px;
}

.result-stat {
  min-width: 70px;
  padding: 7px 10px;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.05);
}

.result-stat dd {
  margin: 2px 0 0;
  font-size: 17px;
  font-weight: 750;
  font-variant-numeric: tabular-nums;
}

.result-note {
  margin: 0 0 14px;
  color: var(--amber);
  font-size: 14px;
  font-weight: 600;
}

.result-actions {
  display: grid;
  gap: 8px;
}

.action {
  height: 42px;
  border: 0;
  border-radius: 11px;
  background: rgba(255, 255, 255, 0.08);
  font-size: 15px;
  font-weight: 700;
}

.action:hover {
  background: rgba(255, 255, 255, 0.13);
}

.action.primary {
  background: var(--blue);
  color: #0c1726;
}

.action.primary:hover {
  background: #6cb4ff;
}

@keyframes fade-in {
  from {
    opacity: 0;
  }
}

.dialog {
  width: min(400px, calc(100vw - 32px));
  padding: 24px 22px 20px;
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 18px;
  background: var(--surface-raised);
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.5);
  color: var(--ink);
  text-align: center;
}

.dialog[open] {
  animation: card-rise 0.3s cubic-bezier(0.2, 0.8, 0.25, 1);
}

.dialog::backdrop {
  background: rgba(10, 11, 14, 0.6);
  backdrop-filter: blur(3px);
}

.dialog h2 {
  margin: 0 0 8px;
  font-size: 20px;
}

.dialog h3 {
  margin: 18px 0 6px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 650;
}

.dialog p {
  margin: 0 0 18px;
  color: var(--muted);
  font-size: 14px;
}

.dialog-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
}

.dialog-actions .action {
  padding: 0 18px;
}

.stats-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
  font-variant-numeric: tabular-nums;
}

.stats-table th,
.stats-table td {
  padding: 7px 8px;
}

.stats-table thead th {
  color: var(--muted);
  font-size: 12px;
  font-weight: 650;
}

.stats-table tbody th {
  color: var(--muted);
  font-weight: 500;
  text-align: left;
}

.stats-table td {
  font-weight: 700;
}

.stats-table tbody tr + tr {
  border-top: 1px solid var(--thin-line);
}

.dialog .daily-streak {
  color: var(--ink);
  font-weight: 600;
}

.reset-confirm {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-size: 14px;
}

.reset-confirm[hidden] {
  display: none;
}

.action.danger {
  background: var(--error);
  color: #2a0b0b;
}

@keyframes card-rise {
  from {
    opacity: 0;
    transform: translateY(14px) scale(0.97);
  }
}

.site-footer {
  display: flex;
  justify-content: center;
  gap: 8px;
  padding: 5px;
  background: rgba(0, 0, 0, 0.18);
  font-size: 10px;
  opacity: 0.7;
}

.site-footer a {
  color: inherit;
}


.guide {
  max-width: 760px;
  margin: 0 auto;
  padding: 48px 20px 64px;
  color: rgba(241, 241, 244, 0.84);
  font-size: 16px;
  line-height: 1.65;
  user-select: text;
}

.guide h2 {
  margin: 0 0 10px;
  color: var(--ink);
  font-size: 24px;
}

.guide h3 {
  margin: 22px 0 6px;
  color: var(--ink);
  font-size: 17px;
}

.guide p,
.guide li {
  margin: 0 0 10px;
}

.guide ul,
.guide ol {
  padding-left: 22px;
}

.guide kbd {
  padding: 1px 6px;
  border: 1px solid rgba(255, 255, 255, 0.25);
  border-radius: 5px;
  background: transparent;
  font: inherit;
  font-size: 13px;
}

.guide h2:not(:first-child) {
  margin-top: 40px;
}

.guide a,
.more-games a {
  color: var(--blue-soft);
}

.more-games {
  max-width: 760px;
  margin: 0 auto;
  padding: 0 20px 64px;
  user-select: text;
}

.more-games h2 {
  margin: 0 0 12px;
  font-size: 20px;
}

.more-games ul {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 20px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.noscript-note {
  margin: 0;
  padding: 12px 20px;
  background: var(--blue);
  color: #0c1726;
  text-align: center;
}

@media (max-width: 640px) {
  .site-footer {
    flex-wrap: wrap;
    row-gap: 2px;
    column-gap: 12px;
  }

  .site-footer > [aria-hidden="true"] {
    display: none;
  }
}

#table {
  position: relative;
  display: flex;
  flex: 1;
  flex-direction: column;
  min-height: calc(var(--viewport-height) - var(--header-height) - var(--footer-height));
  padding: 14px 16px 12px;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.board-frame {
  position: relative;
  flex: 1 1 0;
  min-height: 0;
}

.board {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.board.is-shuffling .tile {
  animation: tile-flip 0.4s ease-out;
}

.tile {
  --tile-ring: inset 0 0 0 0 transparent;
  position: absolute;
  top: calc(var(--y) * var(--th) / 2 + (var(--top-layer) - var(--z)) * var(--edge));
  left: calc(var(--x) * var(--tw) / 2 + (var(--z) + 1) * var(--edge));
  width: var(--tw);
  height: var(--th);
  border-radius: calc(var(--tw) * 0.13);
  background: linear-gradient(155deg, #f4eedf, #e4dac2);
  box-shadow:
    var(--tile-ring),
    calc(var(--edge) * -0.2) calc(var(--edge) * 0.2) 0 #ddd0ae,
    calc(var(--edge) * -0.4) calc(var(--edge) * 0.4) 0 #cfbf96,
    calc(var(--edge) * -0.6) calc(var(--edge) * 0.6) 0 #2b7d72,
    calc(var(--edge) * -0.8) calc(var(--edge) * 0.8) 0 #236a61,
    calc(var(--edge) * -1) calc(var(--edge) * 1) 0 #1b5650,
    calc(var(--edge) * -1.4) calc(var(--edge) * 1.6) calc(var(--edge) * 1.4) rgba(0, 0, 0, 0.38);
  cursor: not-allowed;
  -webkit-tap-highlight-color: transparent;
  transition: filter 0.15s, transform 0.12s;
}

.tile-art {
  display: block;
  width: 100%;
  height: 100%;
  opacity: 0.8;
  pointer-events: none;
}

.tile.is-free {
  --tile-ring: inset 0 0 0 calc(var(--tw) * 0.03 + 0.5px) rgba(226, 168, 62, 0.55);
  background: linear-gradient(155deg, #fffdf7, #f2e9d3);
  cursor: pointer;
}

.tile.is-free .tile-art {
  opacity: 1;
}

.tile::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  pointer-events: none;
}

.tile.is-selected {
  transform: translate(calc(var(--edge) * 0.25), calc(var(--edge) * -0.35));
}

.tile.is-selected::after {
  background: rgba(77, 163, 255, 0.2);
  box-shadow: inset 0 0 0 calc(var(--tw) * 0.05 + 1px) var(--blue), 0 0 calc(var(--tw) * 0.3) rgba(77, 163, 255, 0.55);
}

.tile.is-hint::after {
  background: rgba(242, 180, 92, 0.22);
  box-shadow: inset 0 0 0 calc(var(--tw) * 0.05 + 1px) var(--amber), 0 0 calc(var(--tw) * 0.35) rgba(242, 180, 92, 0.8);
  animation: hint-pulse 0.8s ease-in-out infinite alternate;
}

.tile.is-shaking {
  filter: brightness(0.86);
  animation: tile-shake 0.32s ease-in-out;
}

.tile.is-leaving {
  pointer-events: none;
  animation: tile-leave 0.26s ease-in forwards;
}

.tile.is-gone {
  display: none;
}

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

.tile.is-matching {
  pointer-events: none;
  animation: tile-meet 0.15s ease-in forwards;
}

.tile.is-leaving {
  translate: var(--meet-x, 0) var(--meet-y, 0);
}

@media (hover: hover) {
  .tile.is-free:hover {
    filter: brightness(1.04);
  }

  .tile:not(.is-free):hover {
    filter: brightness(0.9);
  }
}

@keyframes tile-shake {
  0%,
  100% {
    translate: 0;
  }
  25% {
    translate: -3px 0;
  }
  50% {
    translate: 3px 0;
  }
  75% {
    translate: -2px 0;
  }
}

@keyframes tile-leave {
  to {
    opacity: 0;
    transform: translate(calc(var(--edge) * 1.5), calc(var(--edge) * -2.5)) scale(0.86);
  }
}

@keyframes tile-meet {
  to {
    translate: var(--meet-x) var(--meet-y);
  }
}

@keyframes tile-flip {
  40% {
    transform: scaleX(0.1);
  }
}

@keyframes hint-pulse {
  from {
    opacity: 0.55;
  }
}

.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  width: min(100%, 760px);
  margin: 10px auto 0;
}

.status {
  min-width: 0;
  margin: 0;
  overflow: hidden;
  color: var(--muted);
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.tool-buttons {
  display: flex;
  flex-shrink: 0;
  gap: 6px;
}

.tool-button {
  display: flex;
  align-items: center;
  gap: 6px;
  height: 36px;
  padding: 0 12px;
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 10px;
  background: var(--surface);
  font-size: 14px;
  font-weight: 650;
  transition: background 0.15s, opacity 0.15s;
}

.tool-button:hover:not(:disabled) {
  background: var(--surface-raised);
}

.tool-button:disabled {
  cursor: default;
  opacity: 0.45;
}

.result-card .stat-label {
  display: block;
}

@media (max-width: 1100px) {
  header {
    flex-wrap: wrap;
    row-gap: 8px;
  }

  .button-label {
    display: none;
  }

  .new-game-button {
    width: 38px;
    justify-content: center;
    padding: 0;
  }
}

@media (max-width: 860px) {
  :root {
    --header-height: 104px;
  }
}

@media (max-width: 640px) {
  header {
    padding: 8px 12px;
    gap: 8px;
  }

  .header-start {
    gap: 10px;
  }

  .home-link {
    padding-right: 10px;
  }

  .brand-mark {
    display: none;
  }

  h1 {
    font-size: 17px;
  }

  .stats {
    gap: 4px;
  }

  .stat {
    min-width: 48px;
    padding: 4px 6px;
  }

  .stat-value {
    font-size: 15px;
  }

  .controls {
    width: 100%;
    justify-content: space-between;
    gap: 4px;
  }

  .mode-option span {
    padding: 6px 10px;
  }

  #table {
    padding: 8px 4px 10px;
  }

  .toolbar {
    flex-direction: column-reverse;
    gap: 8px;
  }

  .tool-buttons {
    width: 100%;
  }

  .tool-button {
    flex: 1;
    justify-content: center;
    height: 42px;
  }
}

@media (max-width: 400px) {
  h1 {
    font-size: 15px;
  }

  .stat {
    min-width: 42px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .tile,
  .tile::after,
  .result-overlay,
  .result-card,
  .dialog {
    animation: none !important;
    transition: none !important;
  }
}
