:root {
  --bg: #191715;
  --bg-2: #211d1c;
  --bg-3: #2a2523;
  --line: #3a3532;
  --text: #F1ECEC;
  --muted: #a49c98;
  --faint: #6f6763;
  --accent: #F5B84E;
  --font-ui: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-mono: ui-monospace, "Cascadia Code", "JetBrains Mono", "SF Mono", Menlo, Consolas, monospace;
  --cols: 4;
  --cell: clamp(36px, min(22vw, calc((100dvh - 170px) / 4)), 130px);
  --gap: 4px;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html,
body {
  height: 100%;
}

body {
  font-family: var(--font-ui);
  background:
    radial-gradient(900px 560px at 50% -10%, #2b2622, transparent 70%),
    var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
  align-items: center;
  height: auto;
  min-height: 100dvh;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

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

button {
  font: inherit;
  border: none;
  cursor: pointer;
  -webkit-user-select: none;
  user-select: none;
}

/* ---------- topbar ---------- */

.topbar {
  width: 100%;
  max-width: 860px;
  padding: 14px 16px 4px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.back {
  font-family: var(--font-mono);
  font-size: .82rem;
  color: var(--muted);
  padding: 8px 12px;
  border-radius: 9px;
  border: 1px solid transparent;
  transition: color .15s, border-color .15s, background .15s;
}

.back:hover {
  color: var(--text);
  background: var(--bg-2);
  border-color: var(--line);
}

.topbar h1 {
  font-size: 1.1rem;
  font-weight: 600;
  letter-spacing: .02em;
}

.topbar h1 .size {
  color: var(--faint);
  font-weight: 500;
}

.stats {
  display: flex;
  gap: 14px;
  font-family: var(--font-mono);
  font-size: .82rem;
  color: var(--muted);
}

.stats b {
  color: var(--text);
  font-variant-numeric: tabular-nums;
  font-weight: 600;
}

.select-wrap {
  position: relative;
  display: inline-flex;
  align-items: center;
}

.nav-select {
  appearance: none;
  -webkit-appearance: none;
  font-family: var(--font-mono);
  font-size: .8rem;
  letter-spacing: .02em;
  color: var(--text);
  background:
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M1 1l4 4 4-4' stroke='%23a49c98' stroke-width='1.6' fill='none' stroke-linecap='round'/%3E%3C/svg%3E") no-repeat right 12px center,
    var(--bg-2);
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 8px 32px 8px 12px;
  cursor: pointer;
  max-width: 190px;
  transition: border-color .15s, filter .15s;
}

.nav-select:hover {
  border-color: var(--faint);
  filter: brightness(1.08);
}

.nav-select:focus-visible {
  outline: 2px dashed var(--accent);
  outline-offset: 3px;
}

.nav-select option {
  background: var(--bg-2);
  color: var(--text);
}

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

/* ---------- action buttons ---------- */

#playAgain {
  background: var(--accent);
  color: #1a1613;
  font-weight: 700;
  font-size: .92rem;
  padding: 10px 20px;
  border-radius: 12px;
  box-shadow: 0 6px 22px rgba(245, 184, 78, .35);
  transition: transform .1s, filter .15s;
}

#playAgain {
  font-size: .98rem;
  padding: 11px 26px;
}

#playAgain:hover {
  filter: brightness(1.08);
}

#playAgain:active {
  transform: scale(.96);
}

/* ---------- board ---------- */

.hint {
  font-family: var(--font-mono);
  font-size: .76rem;
  letter-spacing: .02em;
  color: var(--faint);
  margin: 10px 0 14px;
  text-align: center;
  padding: 0 12px;
}

#board {
  position: relative;
  width: calc(var(--cell) * 4);
  height: calc(var(--cell) * 4);
  touch-action: manipulation;
  background: linear-gradient(145deg, #161719, #09090a);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: var(--gap);
  box-shadow: 0 18px 50px rgba(0, 0, 0, .5), inset 0 0 0 1px rgba(255, 255, 255, .03);
}

.tile {
  position: absolute;
  left: var(--gap);
  top: var(--gap);
  width: calc(var(--cell) - var(--gap) * 2);
  height: calc(var(--cell) - var(--gap) * 2);
  transform: translate(calc(var(--tx) * var(--cell)), calc(var(--ty) * var(--cell)));
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--cell) * .44);
  font-weight: 700;
  color: #111214;
  text-shadow: 0 1px 0 #f5f5f58f;
  background: linear-gradient(155deg, #f4f5f5, #9a9da1 42%, #71747a 68%, #b9bcc0);
  border: 1px solid #d4d6d8;
  border-radius: calc(var(--cell) * .12);
  box-shadow: inset 0 0 0 2px #2d2f3366, inset 0 2px 5px #fff8, 0 2px 4px #0009;
  cursor: pointer;
  will-change: transform;
  transition: transform .15s ease, filter .1s;
}

.tile:hover {
  filter: brightness(1.12);
  border-color: var(--metal-accent);
}

.tile:active {
  filter: brightness(.9);
}

.tile.empty {
  background: transparent;
  border-color: transparent;
  box-shadow: none;
  cursor: default;
  pointer-events: none;
}

/* ---------- win overlay ---------- */

.win-overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(10, 8, 7, .82);
  backdrop-filter: blur(4px);
  z-index: 50;
  padding: 16px;
}

.win-overlay.hidden {
  display: none;
}

.win-box {
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 28px 32px;
  text-align: center;
  box-shadow: 0 20px 60px rgba(0, 0, 0, .5);
  max-width: 340px;
  animation: pop .25s ease;
}

@keyframes pop {
  from {
    transform: scale(.92);
    opacity: 0;
  }
}

@media (prefers-reduced-motion: reduce) {
  .win-box {
    animation: none;
  }
}

.win-box h2 {
  font-size: 1.6rem;
  margin-bottom: 8px;
  color: var(--accent);
}

.win-box p {
  color: var(--muted);
  margin-bottom: 18px;
}

/* ---------- mobile ---------- */

@media (max-width: 760px) {
  .topbar {
    padding: 10px 12px 4px;
  }

  .nav-select {
    max-width: 150px;
    font-size: .74rem;
    padding: 7px 28px 7px 10px;
  }

  .stats {
    gap: 10px;
    font-size: .76rem;
  }
}
