.klotski-play {
  --game-play-width: 560px;
}

.klotski-toolbar, .klotski-stats, .klotski-pad, .klotski-actions, .klotski-victory-actions {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  gap: 8px;
}

.klotski-toolbar label, .klotski-stats, .klotski-status {
  color: var(--game-secondary);
  font-size: 14px;
  line-height: 1.6;
}
.klotski-toolbar select { min-width: 0; max-width: 100%; }
.klotski-stats { gap: 24px; margin: 10px 0; font-variant-numeric: tabular-nums; }
.klotski-stats strong { margin-inline-start: 3px; color: var(--game-ink); }

.klotski-frame {
  --klotski-rim: #273e41;
  --klotski-rim-edge: #203336;
  --klotski-well: #d6c5a4;
  --klotski-grid: #87714a42;
  --klotski-gap: clamp(4px, 1.4vw, 7px);
  box-sizing: border-box;
  width: min(100%, 316px);
  margin-inline: auto;
  padding: 9px 9px 0;
  border: 1px solid var(--klotski-rim-edge);
  border-radius: 14px;
  background: var(--klotski-rim);
  box-shadow: inset 0 1px #e5f1f52e, 0 4px 0 #20333626;
}

html[data-theme=dark] .klotski-frame {
  --klotski-rim: #314c4f;
  --klotski-rim-edge: #101d22;
  --klotski-well: #a08d6c;
  --klotski-grid: #3b342945;
}

.klotski-board {
  position: relative;
  aspect-ratio: 4 / 5;
  inline-size: 100%;
  border-radius: 6px 6px 3px 3px;
  background:
    linear-gradient(90deg, transparent calc(100% - 1px), var(--klotski-grid) 0) 0 0 / 25% 100%,
    linear-gradient(0deg, var(--klotski-grid) 1px, transparent 0) 0 0 / 100% 20%,
    var(--klotski-well);
  box-shadow: inset 0 1px 7px #2f1e1133;
  user-select: none;
}

.klotski-exit {
  box-sizing: border-box;
  display: grid;
  place-items: center;
  width: 50%;
  min-height: 25px;
  margin-inline: auto;
  border-radius: 0 0 7px 7px;
  background: var(--klotski-well);
  color: #473b28;
  font-size: 13px;
  font-weight: 650;
  line-height: 1;
  transition: background-color 180ms ease-out, color 180ms ease-out;
}

html[data-theme=dark] .klotski-exit { color: #19160f; }
.klotski-board[data-won] + .klotski-exit { background: #bcd9be; color: #294830; }

.klotski-piece {
  --klotski-color: #e5d7b8;
  --klotski-edge: #8b7956;
  --klotski-label: #342b1c;
  position: absolute;
  inset: calc(var(--y) * 20% + var(--klotski-gap) / 2) auto auto calc(var(--x) * 25% + var(--klotski-gap) / 2);
  inline-size: calc(var(--w) * 25% - var(--klotski-gap));
  block-size: calc(var(--h) * 20% - var(--klotski-gap));
  transform: translate(var(--drag-x, 0px), var(--drag-y, 0px));
  box-sizing: border-box;
  display: grid;
  place-items: center;
  padding: 2px;
  overflow: hidden;
  border: 1px solid var(--klotski-edge);
  border-radius: clamp(7px, 1.5vw, 10px);
  background: linear-gradient(155deg, #ffffff11, transparent 68%), var(--klotski-color);
  color: var(--klotski-label);
  box-shadow: inset 0 1px #ffffff41, inset 0 -2px #20180e26, 0 2px 2px #3929134d;
  font: 650 clamp(14px, 3.9vw, 20px)/1.15 var(--font-family, system-ui, sans-serif);
  cursor: grab;
  touch-action: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  transition: top 180ms cubic-bezier(.2,.72,.36,1), left 180ms cubic-bezier(.2,.72,.36,1), transform 160ms ease-out;
}

.klotski-piece-label { pointer-events: none; }
.klotski-piece[data-kind=goal] {
  --klotski-color: #a44034;
  --klotski-edge: #762b25;
  --klotski-label: #fff6e6;
  font-size: clamp(21px, 5.6vw, 31px);
  letter-spacing: .09em;
  text-indent: .09em;
}
.klotski-piece[data-kind=horizontal] {
  --klotski-color: #b78d61;
  --klotski-edge: #785739;
}
.klotski-piece[data-kind=vertical] {
  --klotski-color: #4b6a62;
  --klotski-edge: #314941;
  --klotski-label: #fff8e9;
}
.klotski-piece[data-kind=soldier] { font-size: clamp(11px, 3.5vw, 16px); white-space: nowrap; }

@media (hover: hover) {
  .klotski-piece:hover:not(:disabled) { filter: brightness(1.07); }
}
.klotski-piece[data-selected], .klotski-piece:focus-visible {
  outline: 2px solid #142d38;
  outline-offset: -2px;
  z-index: 1;
}
.klotski-piece[data-selected] { box-shadow: inset 0 0 0 3px #fff5d8, 0 2px 2px #3929134d; }
.klotski-piece[data-hinted] {
  outline: 3px dashed #724111;
  outline-offset: -4px;
  box-shadow: inset 0 0 0 4px #ffe79e, 0 2px 2px #3929134d;
  z-index: 1;
}
.klotski-piece[data-dragging] {
  transition: none;
  z-index: 2;
  cursor: grabbing;
}
.klotski-piece:disabled, .klotski-board[data-won] .klotski-piece { cursor: default; }

.klotski-pad { margin-top: 13px; }
body[data-fourhao-game="klotski"] .klotski-pad button {
  width: 44px;
  min-height: 44px;
  padding: 0;
  font-size: 21px;
  line-height: 1;
}
body[data-fourhao-game="klotski"] .klotski-pad [data-available] {
  color: var(--game-accent);
  background: var(--game-accent-soft);
}
body[data-fourhao-game="klotski"] .klotski-pad [data-hinted] {
  border-color: #ac792c;
  background: #fff0cc;
  color: #724111;
}
.klotski-actions { margin-top: 9px; }
#show-victory[hidden], #next-level[hidden] { display: none; }
.klotski-status { min-height: 1.6em; margin: 9px 0 0; text-align: center; }
.klotski-status[data-error] { color: #9a4137; }
html[data-theme=dark] .klotski-status[data-error] { color: #ffb8ad; }

.klotski-victory {
  box-sizing: border-box;
  width: min(420px, calc(100% - 32px));
  max-height: calc(100% - 32px);
  margin: auto;
  padding: 0;
  border: 1px solid var(--game-line);
  border-radius: 14px;
  background: var(--game-surface);
  color: var(--game-secondary);
  box-shadow: 0 18px 54px #14243840;
  overscroll-behavior: contain;
}
.klotski-victory::backdrop { background: #1724388c; }
.klotski-victory-body { padding: 24px clamp(18px, 6vw, 32px); text-align: center; }
.klotski-victory h2 { margin: 0; color: var(--game-accent); font-size: 22px; line-height: 1.45; }
.klotski-victory p { margin: 9px 0 16px; line-height: 1.65; }

@media (max-width: 440px) {
  .klotski-frame { width: min(100%, 278px); }
  body[data-fourhao-game="klotski"] .klotski-actions .game-page-button { padding-inline: 10px; }
}

@media (prefers-reduced-motion: reduce) {
  .klotski-piece, .klotski-exit { transition: none; }
}
.klotski-victory[open] {
  animation: klotski-victory-enter 220ms ease-out;
}
@keyframes klotski-victory-enter {
  from { opacity: .3; }
  55% { box-shadow: 0 0 0 5px #bfa68547, 0 18px 45px #13263442; }
}
@media (prefers-reduced-motion: reduce) {
  .klotski-victory[open] { animation: none; }
}
