/* Forest Dwellers' Sliding Puzzle */

:root {
    --forest: #3D523F;
    --deep-forest: #2E3D30;
    --gold: #D4AF37;
    --cream: #F4EDD2;
    --soft-cream: #FAF7E8;
    --purple: #9370DB;
    --purple-highlight: #BA55D3;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    padding: 20px;
    background: var(--soft-cream);
    color: var(--deep-forest);
    font-family: "Comic Sans MS", "Chalkboard", cursive, sans-serif;
}

.game-page {
    width: min(760px, 100%);
    margin: 0 auto;
}

.game-header {
    text-align: center;
    margin-bottom: 14px;
}

.game-header h1 {
    margin: 0;
    color: var(--forest);
    font-size: clamp(1.8rem, 4vw, 2.8rem);
}

.game-header p { margin: 5px 0 0; font-size: 1.1rem; }

/* ---------- Controls ---------- */
.controls {
    background: var(--cream);
    border: 3px solid var(--forest);
    border-radius: 14px;
    padding: 12px;
    margin-bottom: 16px;
    display: grid;
    gap: 10px;
}

.row {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    flex-wrap: wrap;
}

.row[hidden], .toggle[hidden], .opt-btn[hidden] { display: none; }

.control-label { font-weight: bold; margin-right: 4px; }

.opt-btn,
.new-game {
    min-height: 44px;
    border: 2px solid var(--forest);
    border-radius: 10px;
    padding: 7px 14px;
    background: var(--soft-cream);
    color: var(--deep-forest);
    font: inherit;
    font-weight: bold;
    cursor: pointer;
}

.opt-btn small { font-size: .78rem; }

.opt-btn.active,
.opt-btn:hover,
.new-game:hover { background: var(--gold); color: var(--deep-forest); }

.new-game { background: var(--forest); color: var(--soft-cream); }

.toggle {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    min-height: 44px;
    background: var(--soft-cream);
    border-radius: 8px;
    padding: 6px 10px;
    cursor: pointer;
}

.toggle input { width: 20px; height: 20px; accent-color: var(--forest); }

.pic-btn {
    width: 60px;
    height: 60px;
    border: 3px solid var(--forest);
    border-radius: 10px;
    background-size: cover;
    background-position: center;
    cursor: pointer;
    padding: 0;
}

.pic-btn.active { border-color: var(--gold); box-shadow: 0 0 0 3px var(--gold); }

.stats span {
    background: var(--soft-cream);
    border-radius: 8px;
    padding: 6px 10px;
}

.opt-btn:focus-visible,
.new-game:focus-visible,
.pic-btn:focus-visible,
.tile:focus-visible {
    outline: 4px solid var(--purple-highlight);
    outline-offset: 2px;
}

/* ---------- Board ---------- */
.board-wrap {
    position: relative;
    width: min(100%, 480px);
    margin: 0 auto;
}

.board {
    --size: 3;
    --gap: 6px;
    position: relative;
    width: 100%;
    aspect-ratio: 1;
    background-color: var(--deep-forest);
    background-size: cover;
    border: 5px solid var(--gold);
    border-radius: 16px;
    overflow: hidden;
    touch-action: none;
}

.tile {
    --r: 0;
    --c: 0;
    position: absolute;
    width: calc((100% - var(--gap)) / var(--size) - var(--gap));
    height: calc((100% - var(--gap)) / var(--size) - var(--gap));
    left: calc(var(--gap) + var(--c) * (100% - var(--gap)) / var(--size));
    top: calc(var(--gap) + var(--r) * (100% - var(--gap)) / var(--size));
    border: 0;
    border-radius: 10px;
    background-color: var(--cream);
    color: var(--forest);
    font: inherit;
    font-weight: bold;
    font-size: calc(min(100vw, 480px) / var(--size) * .42);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    box-shadow: inset 0 -4px 0 rgba(0,0,0,.18), 0 2px 5px rgba(0,0,0,.3);
    transition: left .15s ease, top .15s ease, box-shadow .2s ease;
    padding: 0;
}

.tile .num { display: none; line-height: 1; }
.show-numbers .tile .num { display: block; }

.board:not(.picture-mode) .tile.home { background-color: #e9dfae; }

/* Picture tiles: numbers become a small badge in the corner */
.picture-mode .tile { font-size: calc(min(100vw, 480px) / var(--size) * .2); }

.picture-mode.show-numbers .tile .num {
    position: absolute;
    top: 4px;
    left: 4px;
    background: rgba(250,247,232,.9);
    border-radius: 6px;
    padding: 2px 6px;
}

/* Solved: tiles close their gaps and the picture comes together */
.board.solved .tile {
    box-shadow: none;
    animation: glow 1s ease 2;
}

.board.picture-mode.solved .tile {
    opacity: 0;
    transition: opacity .8s ease .2s;
}

.board.solved { box-shadow: 0 0 0 6px var(--purple-highlight); }

.tile.nudge { animation: shake .3s ease; }

.peek-image {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
    border: 5px solid var(--gold);
    border-radius: 16px;
    cursor: pointer;
    z-index: 2;
}

.peek-image[hidden] { display: none; }

.message {
    min-height: 1.6em;
    text-align: center;
    font-size: 1.2rem;
    font-weight: bold;
    color: var(--forest);
    margin: 16px 0 0;
}

@keyframes shake {
    0%, 100% { translate: 0; }
    25% { translate: -5px 0; }
    75% { translate: 5px 0; }
}

@keyframes glow {
    50% { background-color: var(--gold); }
}

@media (max-width: 500px) {
    body { padding: 10px; }
    .board { --gap: 4px; }
    .control-label { width: 100%; text-align: center; }
    .opt-btn, .new-game { padding: 6px 10px; }
    .pic-btn { width: 52px; height: 52px; }
}

@media (prefers-reduced-motion: reduce) {
    .tile { transition: none; }
    .tile.nudge, .board.solved .tile { animation: none; }
}
