:root {
    --background: #6e5c62;
    --background-deep: #58494e;
    --tile-border: #4c4347;
    --tile-empty: transparent;
    --correct: #3aa394;
    --present: #d3ad69;
    --absent: #181416;
    --key: #5d5356;
    --text: #fafaff;
    --muted: #d6cfd2;
    --gap: 3px;
    --attempt-count: 9;
}

* {
    box-sizing: border-box;
    touch-action: manipulation;
}

html,
body {
    height: 100%;
    margin: 0;
    overflow: hidden;
}

body {
    background: var(--background);
    color: var(--text);
    font-family: ui-rounded, "Arial Rounded MT Bold", system-ui, sans-serif;
    font-weight: 650;
    -webkit-tap-highlight-color: transparent;
}

button {
    color: inherit;
    font: inherit;
}

button:focus-visible {
    outline: 3px solid #fff;
    outline-offset: 2px;
}

.app {
    display: grid;
    grid-template-rows: auto auto minmax(0, 1fr) auto;
    height: 100dvh;
    max-width: 1440px;
    margin: 0 auto;
    padding-top: env(safe-area-inset-top);
}

.topbar {
    display: grid;
    grid-template-columns: 3rem 1fr 3rem;
    align-items: center;
    min-height: 3.25rem;
    padding: .25rem .75rem;
    border-bottom: 1px solid rgba(255, 255, 255, .18);
}

.topbar h1 {
    margin: 0;
    font-size: clamp(1.3rem, 6vw, 1.8rem);
    letter-spacing: .02em;
    text-align: center;
}

.topbar h1 a {
    color: inherit;
    text-decoration: none;
}

.icon-button {
    display: grid;
    width: 2.4rem;
    height: 2.4rem;
    place-items: center;
    border: 2px solid rgba(255, 255, 255, .65);
    border-radius: 50%;
    background: transparent;
    cursor: pointer;
    font-size: 1.2rem;
}

.icon-button:disabled {
    visibility: hidden;
}

.share-button {
    justify-self: end;
}

.controls {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr) auto;
    align-items: center;
    gap: .65rem;
    min-height: 3.15rem;
    padding: .35rem .75rem;
}

.mode-switch {
    display: flex;
    padding: 3px;
    border-radius: 999px;
    background: var(--background-deep);
}

.mode-switch button {
    min-height: 2rem;
    padding: .3rem .75rem;
    border: 0;
    border-radius: 999px;
    background: transparent;
    cursor: pointer;
    font-size: .78rem;
}

.mode-switch button[aria-pressed="true"] {
    background: var(--text);
    color: var(--background-deep);
}

.status {
    overflow: hidden;
    margin: 0;
    color: var(--muted);
    font-size: .78rem;
    text-align: right;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.new-game-button {
    min-height: 2rem;
    padding: .3rem .7rem;
    border: 1px solid rgba(255, 255, 255, .65);
    border-radius: 999px;
    background: transparent;
    cursor: pointer;
    font-size: .72rem;
    white-space: nowrap;
}

.new-game-button:hover {
    background: rgba(255, 255, 255, .1);
}

.new-game-button:disabled {
    cursor: wait;
    opacity: .55;
}

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

.game-area {
    min-height: 0;
    padding: .2rem .65rem .45rem;
    overflow: auto;
}

.boards {
    display: grid;
    grid-template: repeat(2, minmax(0, 1fr)) / repeat(2, minmax(0, 1fr));
    gap: .45rem;
    width: 100%;
    height: 100%;
    min-height: 15rem;
}

.board-shell {
    display: grid;
    grid-template-rows: 1.15rem minmax(0, 1fr);
    min-width: 0;
    min-height: 0;
    justify-items: center;
}

.board-label {
    margin: 0;
    color: var(--muted);
    font-size: .66rem;
    letter-spacing: .08em;
    text-transform: uppercase;
}

.board-shell.solved .board-label {
    color: #9de5da;
}

.board-grid {
    display: grid;
    grid-template: repeat(var(--attempt-count), minmax(0, 1fr)) / repeat(5, minmax(0, 1fr));
    gap: var(--gap);
    height: min(100%, calc((100vw - 2.5rem) * .81));
    max-height: 20rem;
    aspect-ratio: 5 / 9;
}

.app[data-variant="octeto"] .boards {
    height: auto;
    min-height: 0;
    grid-template: none / repeat(2, minmax(0, 1fr));
    grid-auto-rows: auto;
    align-items: start;
}

.app[data-variant="octeto"] .board-grid {
    width: min(100%, 10.5rem);
    height: auto;
    max-height: none;
    aspect-ratio: 5 / 13;
}

.app[data-variant="octeto"] .board-shell {
    grid-template-rows: 1.15rem auto;
}

.tile {
    display: grid;
    min-width: 0;
    min-height: 0;
    place-items: center;
    border: 2px solid var(--tile-border);
    border-radius: 8%;
    background: var(--tile-empty);
    color: var(--text);
    font-size: clamp(.62rem, 3.15vw, 1.25rem);
    line-height: 1;
    text-transform: uppercase;
    backface-visibility: hidden;
    transform-style: preserve-3d;
}

.tile.flipping {
    will-change: transform;
    animation: tile-flip 320ms ease-in-out;
}

@keyframes tile-flip {
    0% {
        transform: rotateX(0);
    }

    50% {
        transform: rotateX(90deg);
    }

    50.01% {
        transform: rotateX(-90deg);
    }

    100% {
        transform: rotateX(0);
    }
}

.tile.filled {
    border-color: #8d7c82;
}

.tile.correct,
.tile.present,
.tile.absent {
    border-color: transparent;
}

.correct {
    background: var(--correct) !important;
}

.present {
    background: var(--present) !important;
}

.absent {
    background: var(--absent) !important;
}

.keyboard {
    display: grid;
    gap: .34rem;
    width: min(100%, 46rem);
    margin: 0 auto;
    padding: .45rem .35rem calc(.5rem + env(safe-area-inset-bottom));
}

.keyboard-row {
    display: flex;
    gap: .25rem;
    justify-content: center;
}

.key {
    position: relative;
    display: grid;
    flex: 1 1 0;
    width: 0;
    height: clamp(2.45rem, 6.2vh, 3.25rem);
    max-width: 4rem;
    place-items: center;
    padding: 0;
    overflow: hidden;
    border: 0;
    border-radius: .3rem;
    background: var(--key);
    cursor: pointer;
    font-size: clamp(.72rem, 3.2vw, 1rem);
}

.key.wide {
    flex-grow: 1.55;
    max-width: 6rem;
    font-size: .64rem;
}

.key-states {
    position: absolute;
    inset: 0;
    display: grid;
    grid-template: 1fr 1fr / 1fr 1fr;
}

.app[data-board-count="8"] .key-states {
    grid-template: repeat(2, 1fr) / repeat(4, 1fr);
}

.key-state {
    background: var(--key);
}

.key-label {
    position: relative;
    z-index: 1;
    text-shadow: 0 1px 2px #251f21;
}

.toast {
    position: fixed;
    z-index: 20;
    top: calc(4rem + env(safe-area-inset-top));
    left: 50%;
    max-width: calc(100vw - 2rem);
    padding: .7rem 1rem;
    transform: translate(-50%, -1rem);
    border-radius: .45rem;
    background: #fafaff;
    color: #312a2c;
    opacity: 0;
    pointer-events: none;
    transition: opacity .18s, transform .18s;
    font-size: .86rem;
    text-align: center;
}

.toast.visible {
    transform: translate(-50%, 0);
    opacity: 1;
}

.modal {
    width: min(92vw, 32rem);
    max-height: min(88dvh, 42rem);
    padding: 1.5rem;
    overflow: auto;
    border: 0;
    border-radius: 1rem;
    background: #4c3f43;
    color: var(--text);
    box-shadow: 0 1.25rem 4rem rgba(0, 0, 0, .35);
}

.modal::backdrop {
    background: rgba(32, 24, 27, .72);
}

.modal h2 {
    margin: 0 2rem 1rem 0;
}

.modal p,
.modal li {
    color: var(--muted);
    font-weight: 500;
    line-height: 1.5;
}

.modal-close {
    position: absolute;
    top: .65rem;
    right: .75rem;
    width: 2.5rem;
    height: 2.5rem;
    border: 0;
    background: transparent;
    cursor: pointer;
    font-size: 1.8rem;
}

.example {
    display: grid;
    grid-template-columns: repeat(5, 2.3rem);
    gap: .25rem;
    margin: 1.2rem 0;
}

.example .tile {
    width: 2.3rem;
    height: 2.3rem;
    font-size: 1rem;
}

.solutions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: .5rem;
    margin: 1.1rem 0 1.4rem;
}

.solution {
    padding: .65rem;
    border-radius: .4rem;
    background: var(--background-deep);
    text-align: center;
    text-transform: uppercase;
}

.result-actions {
    display: flex;
    flex-wrap: wrap;
    gap: .65rem;
}

.primary-button,
.secondary-button {
    flex: 1;
    min-height: 2.8rem;
    padding: .6rem 1rem;
    border: 0;
    border-radius: .45rem;
    cursor: pointer;
}

.primary-button {
    background: var(--correct);
}

.secondary-button {
    background: var(--present);
    color: #2b2426;
}

.csrf-token {
    display: none;
}

@media (min-width: 800px) and (min-aspect-ratio: 1/1) {
    .controls {
        padding-inline: 1.5rem;
    }

    .game-area {
        padding: .75rem 1.5rem 1rem;
    }

    .app:not([data-variant="octeto"]) .boards {
        grid-template: minmax(0, 1fr) / repeat(4, minmax(0, 1fr));
        gap: 1rem;
    }

    .app:not([data-variant="octeto"]) .board-shell {
        grid-template-rows: auto auto;
        align-content: center;
    }

    .app:not([data-variant="octeto"]) .board-grid {
        height: min(calc((100vw - 8rem) * .45), calc(100dvh - 18rem), 24rem);
    }

    .tile {
        font-size: clamp(.8rem, 1.5vw, 1.3rem);
    }
}

@media (min-width: 1200px) and (min-height: 900px) and (min-aspect-ratio: 1/1) {
    .app[data-variant="octeto"] .boards {
        height: 100%;
        grid-template: repeat(2, minmax(0, 1fr)) / repeat(4, minmax(0, 1fr));
        grid-auto-rows: unset;
        align-items: stretch;
        gap: .55rem 1rem;
    }

    .app[data-variant="octeto"] .board-shell {
        grid-template-rows: 1rem minmax(0, 1fr);
        align-content: stretch;
    }

    .app[data-variant="octeto"] .board-grid {
        width: auto;
        height: min(100%, 20rem);
        max-height: 20rem;
    }

    .app[data-variant="octeto"] .solutions {
        grid-template-columns: repeat(4, 1fr);
    }
}

@media (max-height: 600px) {
    .topbar {
        min-height: 2.7rem;
    }

    .controls {
        min-height: 2.65rem;
    }

    .key {
        height: 2.15rem;
    }

    .keyboard {
        gap: .22rem;
        padding-top: .25rem;
    }
}

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        scroll-behavior: auto !important;
        transition-duration: .01ms !important;
    }
}
