:root {
    --bg: #111;
    --ink: #f2f2f7;
    --muted: #9aa3a7;
    --brand: #ff5e00;
    --brand-rgb: 255, 94, 0;
    --grid: #2a2a2a;
    --grid-strong: #3a3a3a;
    --tile-face: #f3efe6;
    --tile-face-2: #e7e1d4;
    --tile-edge: #2a2a2a;
    --tile-back: #3a3a3a;
    --tile-w: 56px;
    --tile-h: 80px;
    --tile-d: 7px;
    --board-w: 860px;
    --board-h: 662px;
}

* {
    box-sizing: border-box;
}

html,
body {
    height: 100%;
}

html,
body,
button {
    font-family: Inter, system-ui, Segoe UI, Roboto, Helvetica, Arial, sans-serif;
}

body {
    margin: 0;
    color: var(--ink);
    background:
        radial-gradient(
            700px 360px at 50% 80px,
            rgba(var(--brand-rgb), 0.12) 0%,
            transparent 70%
        ),
        var(--bg);
}

.app {
    min-height: 100dvh;
    display: grid;
    grid-template-rows: minmax(0, 1fr) auto minmax(0, 1fr);
}

.above {
    display: flex;
    flex-direction: column;
    min-height: 0;
}

.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    flex-shrink: 0;
    padding: 20px 24px 0;
}

.brand {
    display: flex;
    align-items: center;
    gap: 10px;
}

.brand-icon {
    font-size: 28px;
    line-height: 1;
    color: var(--brand);
}

.topbar h1 {
    margin: 0;
    font-size: 24px;
    font-weight: 600;
    letter-spacing: 0.5px;
}

#controls {
    display: flex;
    justify-content: center;
    gap: 12px;
    margin-top: auto;
    margin-bottom: auto;
}

.icon-btn {
    width: 40px;
    height: 40px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    color: var(--ink);
    background: transparent;
    border: 1px solid var(--grid-strong);
    border-radius: 12px;
    cursor: pointer;
}

.icon-btn:hover {
    border-color: var(--brand);
    color: var(--brand);
}

.icon-btn:active {
    transform: translateY(1px);
}

.icon-btn:focus-visible {
    outline: 2px solid var(--brand);
    outline-offset: 2px;
}

.icon-btn:disabled {
    opacity: 0.35;
    cursor: default;
    pointer-events: none;
}

.difficulty {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.chip {
    background: #151515;
    color: var(--ink);
    border: 1px solid var(--grid);
    padding: 8px 12px;
    border-radius: 999px;
    cursor: pointer;
    font-size: 14px;
}

.chip:hover {
    border-color: var(--grid-strong);
}

.chip.active {
    border-color: var(--brand);
    box-shadow: 0 0 0 1px var(--brand) inset;
}

.play {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 0;
    width: 100%;
    padding: 0 16px;
}

.play-col {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 16px;
    width: 100%;
    max-width: var(--board-w);
}

.stage {
    container-type: inline-size;
    position: relative;
    width: 100%;
    max-width: var(--board-w);
    aspect-ratio: var(--board-w) / var(--board-h);
    margin: 0 auto;
}

#game {
    position: absolute;
    top: 0;
    left: 0;
    width: var(--board-w);
    height: var(--board-h);
    transform-origin: top left;
    transform: scale(calc(100cqi / var(--board-w)));
}

#statusText {
    grid-row: 3;
    align-self: end;
    margin: 0;
    padding: 0 24px 20px;
    text-align: center;
    color: var(--muted);
    font-size: 16px;
    min-height: 1.4em;
}

#statusText strong {
    color: var(--ink);
    font-weight: 600;
}

#infoText {
    display: none;
    position: absolute;
    inset: 0;
    z-index: 2;
    overflow: auto;
    padding: 24px 28px;
    background: rgba(0, 0, 0, 0.88);
    color: var(--ink);
    font-size: 16px;
    line-height: 1.5;
}

#infoText a {
    color: var(--brand);
    text-decoration: none;
}

#infoText a:hover {
    text-decoration: underline;
}

#infoText img {
    width: 48px;
    border-radius: 8px;
    background-image: linear-gradient(
        to left top,
        var(--tile-face-2),
        var(--tile-face)
    );
    border: 1px solid var(--grid);
    margin: 0 4px;
}

.tile {
    position: absolute;
}

.tileFront {
    position: absolute;
    overflow: hidden;
    cursor: default;
    touch-action: manipulation;
    user-select: none;
    -webkit-user-drag: none;
    background-image: linear-gradient(
        to left top,
        var(--tile-face-2),
        var(--tile-face)
    );
    border: 1px solid var(--tile-edge);
}

.tileFront img {
    pointer-events: none;
    -webkit-user-drag: none;
    user-select: none;
}

.tileFront::after {
    content: "";
    position: absolute;
    inset: 0;
    pointer-events: none;
    border-radius: inherit;
    background: rgba(0, 0, 0, 0.28);
}

.tileFront.openTile {
    cursor: pointer;
}

.tileFront.openTile::after {
    background: transparent;
}

.tileFront.openTile:hover:not(.selectedTile):not(.alertTile)::after {
    background: rgba(var(--brand-rgb), 0.34);
}

.tileFront.openTile:hover:not(.selectedTile):not(.alertTile) {
    box-shadow: 0 0 0 2px var(--brand);
}

.selectedTile {
    border-color: var(--brand);
    box-shadow: 0 0 0 2px var(--brand), 0 0 12px rgba(var(--brand-rgb), 0.45);
}

.selectedTile::after {
    background: rgba(var(--brand-rgb), 0.5);
}

.alertTile {
    box-shadow: 0 0 0 2px #ffb24d, 0 0 14px rgba(255, 178, 77, 0.55);
}

.alertTile::after {
    background: rgba(255, 178, 77, 0.45);
}

.tileBack {
    position: absolute;
    top: calc(-1 * var(--tile-d));
    left: calc(-1 * var(--tile-d));
    width: calc(var(--tile-w) + var(--tile-d));
    height: calc(var(--tile-h) + var(--tile-d));
    background-color: var(--tile-back);
    border: 1px solid var(--tile-edge);
}

.tile--flush-left .tileBack {
    left: 0;
    width: var(--tile-w);
}

.tile--flush-top .tileBack {
    top: 0;
    height: var(--tile-h);
}

.tile-fly {
    pointer-events: none;
    position: fixed;
}

.buffer {
    display: flex;
    justify-content: center;
    gap: 10px;
    flex-shrink: 0;
}

.buffer__slot {
    width: 48px;
    height: 68px;
    border: 1px dashed var(--grid-strong);
    border-radius: 10px;
    background: rgba(255, 255, 255, 0.04);
    overflow: hidden;
}

.buffer__face {
    width: 100%;
    height: 100%;
    background-image: linear-gradient(
        to left top,
        var(--tile-face-2),
        var(--tile-face)
    );
    border-radius: inherit;
}

.buffer__face img {
    display: block;
    width: 100%;
    height: 100%;
    object-fit: contain;
}

.buffer__face--pop {
    animation: buffer-pop 0.28s ease forwards;
}

@keyframes buffer-pop {
    0% {
        transform: scale(1);
        opacity: 1;
    }
    100% {
        transform: scale(0.2) rotate(12deg);
        opacity: 0;
    }
}
