/*
 * Оформление "Пятнашек".
 *
 * Вся геометрия поля выводится из одной переменной --board-size, поэтому
 * интерфейс одинаково работает в окне любого размера, на телефоне и в
 * полноэкранном режиме. Модуль WebAssembly задаёт фишкам только номер строки
 * и столбца через --row и --col.
 */

:root {
    color-scheme: dark;

    /* Тёмное окружение: страница, карточка игры и коробка поля. */
    --page-top: #141d1f;
    --page-bottom: #0b1112;
    --card: #172224;
    --tray: #111a1c;
    --socket: rgba(255, 255, 255, 0.05);
    --line: #2c3d40;
    --line-strong: #3d5458;
    --ink: #e9f1f2;
    --ink-soft: #8ea5a8;
    --accent: #2bb3e8;
    --accent-bright: #5fc9f1;
    --accent-ink: #0b1618;
    --accent-soft: rgba(43, 179, 232, 0.14);

    --tile-cyan: #2bb3e8;
    --tile-cyan-shade: #1c8ab5;
    --tile-yellow: #ffc42e;
    --tile-yellow-shade: #d99d0f;
    --tile-orange: #f4551e;
    --tile-orange-shade: #c53c0c;
    --tile-blue: #0f5fb0;
    --tile-blue-shade: #0a4682;

    /* Отступы страницы и карточки: их же вычитает расчёт стороны поля. */
    --outer-pad: clamp(8px, 2vmin, 24px);
    --card-pad: clamp(14px, 2.4vmin, 24px);

    /* Сторона поля: вписывается в ширину карточки, в высоту экрана и в
       разумный максимум. Запас в 6px оставлен на полосу прокрутки. */
    --board-fit: calc(100vw - 2 * (var(--outer-pad) + var(--card-pad)) - 6px);
    --board-size: min(var(--board-fit), 58vh, 520px);
    --radius-card: 12px;
    --radius-tray: 12px;
}

@supports (height: 100dvh) {
    :root {
        --board-size: min(var(--board-fit), 58dvh, 520px);
    }
}

* {
    box-sizing: border-box;
}

html {
    height: 100%;
}

body {
    display: grid;
    place-items: center;
    min-height: 100vh;
    margin: 0;
    padding: var(--outer-pad);
    padding-top: max(var(--outer-pad), env(safe-area-inset-top));
    padding-bottom: max(var(--outer-pad), env(safe-area-inset-bottom));
    background: none;
    color: var(--ink);
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    line-height: 1.4;
    overscroll-behavior: none;
    -webkit-text-size-adjust: 100%;
    -webkit-tap-highlight-color: transparent;
}

@supports (height: 100dvh) {
    body {
        min-height: 100dvh;
    }
}

:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

/* Карточка игры */

.app {
    display: grid;
    gap: clamp(10px, 1.8vmin, 18px);
    width: 100%;
    max-width: calc(var(--board-size) + 2 * var(--card-pad));
    padding: var(--card-pad);
    background: var(--card);
    border-radius: var(--radius-card);
    box-shadow: none;
    border: 1px solid #2c3d40;
}

.head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}

.head__title {
    margin: 0;
    font-size: clamp(19px, 3.4vmin, 26px);
    font-weight: 700;
    letter-spacing: 0.01em;
}

.icon-button {
    display: grid;
    flex: none;
    place-items: center;
    width: 38px;
    height: 38px;
    padding: 0;
    background: var(--tray);
    border: 1px solid var(--line);
    border-radius: 12px;
    color: var(--ink-soft);
    cursor: pointer;
    touch-action: manipulation;
    transition: background 150ms ease, border-color 150ms ease, color 150ms ease;
}

.icon-button:hover {
    color: var(--ink);
    border-color: var(--line-strong);
}

.icon-button--active {
    background: var(--accent-soft);
    border-color: rgba(43, 179, 232, 0.45);
    color: var(--accent);
}

.icon-button[hidden] {
    display: none;
}

.icon-button__glyph {
    width: 20px;
    height: 20px;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.9;
    stroke-linecap: round;
    stroke-linejoin: round;
}

/* Показатели партии */

.stats {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: clamp(6px, 1.2vmin, 10px);
}

.stat {
    display: grid;
    align-content: center;
    gap: 2px;
    padding: clamp(6px, 1.2vmin, 10px) clamp(4px, 1vmin, 8px);
    background: var(--tray);
    border-radius: 14px;
    box-shadow: inset 0 0 0 1px var(--line);
    text-align: center;
}

.stat__label {
    color: var(--ink-soft);
    font-size: clamp(9px, 1.5vmin, 11px);
    letter-spacing: 0.05em;
    text-transform: uppercase;
}

.stat__value {
    font-size: clamp(15px, 2.6vmin, 20px);
    font-weight: 700;
    font-variant-numeric: tabular-nums;
}

@media (max-width: 400px) {
    .stats {
        grid-template-columns: repeat(2, 1fr);
    }
}

/* Поле */

.field {
    position: relative;
    justify-self: center;
    width: var(--board-size);
    height: var(--board-size);
}

.board {
    --pad: calc(var(--board-size) * 0.028);
    --gap: calc(var(--board-size) * 0.019);
    --cell: calc((var(--board-size) - 2 * var(--pad) - 3 * var(--gap)) / 4);

    position: relative;
    width: 100%;
    height: 100%;
    padding: var(--pad);
    background: var(--tray);
    border-radius: var(--radius-tray);
    box-shadow: inset 0 2px 7px rgba(0, 0, 0, 0.45), inset 0 0 0 1px var(--line);
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
}

/* Место в коробке задаёт свойство translate, а не transform: тогда scale
   отсчитывается от центра самой фишки и подъём при наведении не тянет её
   по полю. */
.cell,
.tile {
    position: absolute;
    top: 0;
    left: 0;
    width: var(--cell);
    height: var(--cell);
    translate:
            calc(var(--pad) + var(--col) * (var(--cell) + var(--gap)))
            calc(var(--pad) + var(--row) * (var(--cell) + var(--gap)));
}

.cell {
    background: var(--socket);
    border-radius: calc(var(--cell) * 0.16);
    box-shadow: inset 0 0 0 1px var(--line);
}

.tile {
    display: grid;
    place-items: center;
    background: var(--tile-color);
    border-radius: calc(var(--cell) * 0.17);
    box-shadow: 0 calc(var(--cell) * 0.035) 0 var(--tile-shade),
    0 calc(var(--cell) * 0.055) calc(var(--cell) * 0.11) rgba(0, 0, 0, 0.4);
    color: #ffffff;
    font-size: calc(var(--cell) * 0.44);
    font-weight: 700;
    line-height: 1;
    text-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
    cursor: default;
    will-change: translate;
    -webkit-touch-callout: none;
    transition: translate 150ms cubic-bezier(0.22, 0.61, 0.36, 1), scale 140ms ease,
    box-shadow 140ms ease;
}

.tile--cyan {
    --tile-color: var(--tile-cyan);
    --tile-shade: var(--tile-cyan-shade);
}

.tile--yellow {
    --tile-color: var(--tile-yellow);
    --tile-shade: var(--tile-yellow-shade);
}

.tile--orange {
    --tile-color: var(--tile-orange);
    --tile-shade: var(--tile-orange-shade);
}

.tile--blue {
    --tile-color: var(--tile-blue);
    --tile-shade: var(--tile-blue-shade);
}

.tile--movable {
    cursor: pointer;
}

/* Единственный отклик на указатель: фишка чуть приподнимается на месте.
   Нажатие ничего не двигает - ответом служит сам ход. */
@media (hover: hover) {
    .tile--movable:hover {
        z-index: 1;
        scale: 1.03;
        box-shadow: 0 calc(var(--cell) * 0.035) 0 var(--tile-shade),
        0 calc(var(--cell) * 0.09) calc(var(--cell) * 0.17) rgba(0, 0, 0, 0.5);
    }
}

/* Браузеры без отдельного свойства translate (Chrome до 104, Safari до 14.1):
   место задаёт transform, чтобы поле не сложилось в угол. Подъёма при
   наведении там не будет - такие браузеры не понимают и scale. */
@supports not (translate: 1px 1px) {
    .cell,
    .tile {
        transform: translate(
                calc(var(--pad) + var(--col) * (var(--cell) + var(--gap))),
                calc(var(--pad) + var(--row) * (var(--cell) + var(--gap)))
        );
    }

    .tile {
        will-change: transform;
        transition: transform 150ms cubic-bezier(0.22, 0.61, 0.36, 1);
    }
}

/* Раздача: фишки разъезжаются по новым местам волной. */
.board--shuffling .tile {
    transition-duration: 320ms;
    transition-delay: calc(var(--i) * 8ms);
}

/* Победа: волна по собранным фишкам. */
.board--solved .tile {
    animation: tile-pop 560ms cubic-bezier(0.34, 1.56, 0.64, 1) both;
    animation-delay: calc(var(--i) * 24ms);
}

@keyframes tile-pop {
    0%,
    100% {
        scale: 1;
    }
    45% {
        scale: 1.07;
    }
}

/* Итоги партии */

.win {
    position: absolute;
    inset: 0;
    z-index: 2;
    display: grid;
    place-items: center;
    padding: 6%;
    background: rgba(11, 18, 19, 0.72);
    backdrop-filter: blur(7px);
    -webkit-backdrop-filter: blur(7px);
    border-radius: var(--radius-tray);
    animation: fade-in 240ms ease both;
}

.win[hidden] {
    display: none;
}

@keyframes fade-in {
    from {
        opacity: 0;
        scale: 0.98;
    }
    to {
        opacity: 1;
        scale: 1;
    }
}

.win__card {
    display: grid;
    gap: clamp(8px, 2%, 14px);
    justify-items: center;
    padding: clamp(16px, 5%, 28px) clamp(18px, 6%, 34px);
    background: var(--card);
    border-radius: 20px;
    box-shadow: inset 0 0 0 1px var(--line), 0 18px 40px rgba(0, 0, 0, 0.55);
    text-align: center;
}

.win__title {
    margin: 0;
    font-size: clamp(20px, 4.4vmin, 28px);
    font-weight: 700;
}

.win__summary {
    display: flex;
    gap: clamp(14px, 5%, 28px);
    justify-content: center;
}

.win__item {
    display: grid;
    gap: 2px;
}

.win__label {
    color: var(--ink-soft);
    font-size: clamp(9px, 1.6vmin, 11px);
    letter-spacing: 0.05em;
    text-transform: uppercase;
}

.win__value {
    font-size: clamp(16px, 3vmin, 22px);
    font-weight: 700;
    font-variant-numeric: tabular-nums;
}

.win__record {
    margin: 0;
    padding: 5px 12px;
    background: var(--accent-soft);
    border-radius: 999px;
    color: var(--accent);
    font-size: clamp(12px, 2.2vmin, 14px);
    font-weight: 600;
}

.win__record[hidden] {
    display: none;
}

/* Управление */

.foot {
    display: grid;
    gap: 8px;
    justify-items: center;
}

.button {
    padding: 0.72em 1.5em;
    background: var(--accent);
    border: 0;
    border-radius: 14px;
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.35);
    color: var(--accent-ink);
    font: inherit;
    font-weight: 600;
    cursor: pointer;
    touch-action: manipulation;
    appearance: none;
    transition: background 150ms ease, box-shadow 150ms ease, translate 120ms ease;
}

.button:hover {
    background: var(--accent-bright);
}

.button:active {
    box-shadow: 0 3px 10px rgba(0, 0, 0, 0.3);
    translate: 0 1px;
}

.button--ghost {
    background: var(--accent-soft);
    box-shadow: inset 0 0 0 1px rgba(43, 179, 232, 0.3);
    color: var(--accent);
}

.button--ghost:hover {
    background: rgba(43, 179, 232, 0.22);
}

.boot-error {
    margin: 0;
    padding: 12px 14px;
    background: rgba(224, 76, 58, 0.14);
    border-radius: 14px;
    box-shadow: inset 0 0 0 1px rgba(224, 76, 58, 0.34);
    color: #ff9d8c;
    font-size: 13px;
}

.boot-error[hidden] {
    display: none;
}

/* Полный экран: та же карточка, но во весь экран устройства. */

.app:fullscreen {
    --board-size: min(86vw, 72vh, 660px);

    width: 100%;
    height: 100%;
    max-width: none;
    grid-template-columns: min(100%, calc(var(--board-size) + 2 * var(--card-pad)));
    align-content: center;
    justify-content: center;
    padding: clamp(16px, 3vmin, 40px);
    background: radial-gradient(125% 100% at 50% 0%, var(--page-top), var(--page-bottom));
    border-radius: 0;
    box-shadow: none;
}

.app:-webkit-full-screen {
    --board-size: min(86vw, 72vh, 660px);

    width: 100%;
    height: 100%;
    max-width: none;
    grid-template-columns: min(100%, calc(var(--board-size) + 2 * var(--card-pad)));
    align-content: center;
    justify-content: center;
    padding: clamp(16px, 3vmin, 40px);
    background: radial-gradient(125% 100% at 50% 0%, var(--page-top), var(--page-bottom));
    border-radius: 0;
    box-shadow: none;
}

@supports (height: 100dvh) {
    .app:fullscreen {
        --board-size: min(86vw, 72dvh, 660px);
    }
}

/* Телефон лёжа: поле слева, показатели и кнопки справа. */

@media (orientation: landscape) and (max-height: 620px) {
    :root {
        --board-size: min(84vh, 52vw, 520px);
    }

    .app {
        grid-template-areas:
            "field head"
            "field stats"
            "field foot";
        grid-template-columns: auto minmax(180px, 300px);
        gap: clamp(8px, 2vmin, 18px) clamp(14px, 2.5vmin, 24px);
        align-items: start;
        width: auto;
        max-width: none;
    }

    .head {
        grid-area: head;
    }

    .stats {
        grid-area: stats;
        grid-template-columns: repeat(2, 1fr);
    }

    .field {
        grid-area: field;
        align-self: center;
    }

    .foot {
        grid-area: foot;
    }

    .app:fullscreen,
    .app:-webkit-full-screen {
        --board-size: min(88vh, 52vw, 620px);

        grid-template-columns: auto minmax(180px, 300px);
        align-content: center;
    }

    @supports (height: 100dvh) {
        :root {
            --board-size: min(84dvh, 52vw, 520px);
        }

        .app:fullscreen {
            --board-size: min(88dvh, 52vw, 620px);
        }
    }
}

@media (prefers-reduced-motion: reduce) {
    .tile,
    .button,
    .icon-button {
        transition-duration: 1ms;
    }

    .board--shuffling .tile {
        transition-delay: 0ms;
    }

    .board--solved .tile,
    .win {
        animation: none;
    }
}
