/* Klotski (/klotski): only what is the game's own - the wooden tray, its exit gap and the blocks. Tiles, buttons,
   fields and the accent come from the app kit (css/app-kit, docs/app-kit.md), the button row from
   css/app-kit/games.css (every game). Loaded only on this page (StylePaths). */

.klotski-frame {
    --kl-rim: clamp(14px, 4.2vw, 30px);
    position: relative;
    /* The tray is 4 squares wide and 5 high, plus the frame; room below for the arrows under the gap. */
    width: min(100%, calc((100vh - 300px) * 0.8 + 2 * var(--kl-rim)), 520px);
    min-width: min(100%, 240px);
    margin: 0 auto calc(var(--kl-rim) + 22px);
    padding: var(--kl-rim);
    box-sizing: border-box;
}

/* The scene canvas reaches beyond the frame for the tray's shadow: never a sideways scroll for it. */
.klotski-panel {
    overflow-x: clip;
}

/* The tray - carved frame, floor and the gap - is drawn on this canvas (filter-engines/klotski.js, drawTray), the
   same drawing as Save image. */
.kl-scene {
    position: absolute;
    inset: -48px;
    width: calc(100% + 96px);
    height: calc(100% + 96px);
    pointer-events: none;
}

.klotski-board {
    position: relative;
    aspect-ratio: 4 / 5;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
}

/* Under the gap: two chevrons that point the way out. */
.kl-exit {
    position: absolute;
    left: 50%;
    top: calc(100% + 4px);
    width: clamp(16px, 4vw, 26px);
    height: 22px;
    transform: translateX(-50%);
    display: flex;
    flex-direction: column;
    align-items: center;
    pointer-events: none;
}

.kl-exit::before,
.kl-exit::after {
    content: "";
    width: 100%;
    aspect-ratio: 1.6 / 1;
    background: var(--ak-accent);
    clip-path: polygon(0 0, 50% 45%, 100% 0, 100% 55%, 50% 100%, 0 55%);
    animation: kl-chevron 1.4s ease-in-out infinite;
}

.kl-exit::after {
    margin-top: -18%;
    animation-delay: 0.2s;
}

/* Phones: the stage is as high as the screen is wide, so the tray is sized to fit under the tiles (the status line
   may scroll), and the arrows under the gap give way. */
@media (max-width: 640px) {
    .klotski-frame {
        width: min(100%, calc((100vw - 112px) * 0.8 + 0.4 * var(--kl-rim)));
        min-width: 0;
        margin-bottom: 6px;
    }

    .kl-exit {
        display: none;
    }

    /* Controls hidden (landing-panel-toggle.js): the stage is the phone's whole height, so the tray takes the width
       (or what the height leaves for it under the tiles). */
    .ak-app.is-panel-collapsed .klotski-frame {
        width: min(100%, calc((100svh - 230px) * 0.8));
    }
}

@keyframes kl-chevron {
    0%, 100% { opacity: 0.35; transform: translateY(0); }
    50% { opacity: 1; transform: translateY(20%); }
}

/* A block: positioned in squares (--kl-x, --kl-y, --kl-w, --kl-h); it is drawn on its canvas. */
.kl-block,
.kl-ghost {
    position: absolute;
    left: calc(var(--kl-x) * 25%);
    top: calc(var(--kl-y) * 20%);
    width: calc(var(--kl-w) * 25%);
    height: calc(var(--kl-h) * 20%);
    padding: 0;
    border: 0;
    background: none;
    box-sizing: border-box;
}

.kl-block {
    z-index: 2;
    cursor: grab;
    border-radius: 14%;
    transition: left 150ms cubic-bezier(0.3, 0.7, 0.3, 1), top 150ms cubic-bezier(0.3, 0.7, 0.3, 1), transform 900ms cubic-bezier(0.5, 0, 0.85, 0.45), opacity 900ms ease-in;
    -webkit-tap-highlight-color: transparent;
}

/* The canvas is larger than the button (set from the script), so the shadow has room. */
.kl-block-art {
    position: absolute;
    pointer-events: none;
    transition: transform 140ms ease, filter 140ms ease;
}

@media (hover: hover) {
    .kl-block:not(.is-stuck):hover .kl-block-art {
        filter: brightness(1.07);
    }
}

.kl-block:focus-visible {
    outline: 3px solid var(--ak-accent);
    outline-offset: -3px;
}

/* Picked up: the block lifts a little off the floor. */
.kl-block.is-dragging {
    z-index: 3;
    cursor: grabbing;
    transition: none;
}

.kl-block.is-dragging .kl-block-art {
    transform: scale(1.03);
    filter: brightness(1.06);
}

/* Solve: the block glides square by square along its path. */
.kl-block.is-gliding {
    transition-duration: 120ms, 120ms, 900ms, 900ms;
    transition-timing-function: linear;
}

.kl-block.is-stuck {
    cursor: default;
}

/* The Hint (and the move Solve plays next): the block glows and its target is outlined on the floor. */
.kl-block.is-hint .kl-block-art {
    animation: kl-pulse 0.8s ease-in-out infinite alternate;
}

@keyframes kl-pulse {
    from { filter: drop-shadow(0 0 2px #fde68a) drop-shadow(0 0 2px #fbbf24); }
    to { filter: drop-shadow(0 0 6px #fde68a) drop-shadow(0 0 10px #fbbf24); }
}

.kl-ghost {
    z-index: 1;
    pointer-events: none;
}

.kl-ghost::before {
    content: "";
    position: absolute;
    inset: 5%;
    border: 3px dashed rgba(255, 236, 190, 0.9);
    border-radius: 14%;
    background: rgba(251, 191, 36, 0.18);
    box-shadow: 0 0 0 2px rgba(251, 191, 36, 0.45);
}

/* Solved: the big block slides out through the gap. */
.klotski-board.is-out .kl-block.is-big {
    transform: translateY(125%);
    opacity: 0;
}

.klotski-board.is-solving,
.klotski-board.is-making {
    pointer-events: none;
}

.klotski-board.is-making .kl-block {
    opacity: 0.55;
}

.klotski-status {
    text-align: center;
}

.klotski-status.is-solved {
    color: var(--ak-success);
    font-weight: 600;
}

@media (prefers-reduced-motion: reduce) {
    .kl-block { transition: none; }
    .kl-exit::before, .kl-exit::after { animation: none; opacity: 1; }
    .kl-block.is-hint .kl-block-art { animation: none; filter: drop-shadow(0 0 4px #fbbf24) drop-shadow(0 0 4px #fbbf24); }
}
