: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 {
    width: 100%;
    height: 100%;
    overflow: hidden;
}

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 {
    position: relative;
    z-index: 0;
    width: 100%;
    height: 100dvh;
    overflow: hidden;
    display: grid;
    grid-template-rows: auto minmax(0, 1fr) auto;
}

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

.topbar {
    display: flex;
    align-items: flex-start;
    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;
}

.topbar-center {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 12px;
    padding: 12px 0 8px;
}

.start-btn {
    height: 40px;
    padding: 0 20px;
    color: var(--ink);
    background: transparent;
    border: 1px solid var(--grid-strong);
    border-radius: 12px;
    font-size: 14px;
    font-weight: 600;
    letter-spacing: 0.3px;
    cursor: pointer;
}

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

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

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

.timer-badge {
    display: flex;
    align-items: center;
    gap: 8px;
    min-height: 36px;
    padding: 6px 14px;
    color: #ffb24d;
    background: #151515;
    border: 1px solid var(--grid-strong);
    border-radius: 999px;
    font-size: 15px;
    letter-spacing: 0.4px;
}

.timer-badge .val {
    min-width: 48px;
    text-align: center;
    font-variant-numeric: tabular-nums;
    font-feature-settings: "tnum" 1;
    font-weight: 600;
}

.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;
}

.layout-pick {
    display: block;
}

.layout-pick select {
    display: block;
    width: 100%;
    min-width: 220px;
    min-height: 36px;
    padding: 8px 32px 8px 12px;
    color: var(--ink);
    background-color: #151515;
    background-image: linear-gradient(
            45deg,
            transparent 50%,
            var(--muted) 50%
        ),
        linear-gradient(135deg, var(--muted) 50%, transparent 50%);
    background-position:
        calc(100% - 18px) 50%,
        calc(100% - 12px) 50%;
    background-size: 6px 6px, 6px 6px;
    background-repeat: no-repeat;
    border: 1px solid var(--grid-strong);
    border-radius: 999px;
    font-size: 14px;
    font-family: inherit;
    cursor: pointer;
    appearance: none;
    -webkit-appearance: none;
    -moz-appearance: none;
}

.layout-pick select:hover {
    border-color: var(--brand);
}

.layout-pick select:focus-visible {
    outline: 2px solid var(--brand);
    outline-offset: 2px;
}

.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;
    justify-content: center;
    min-height: 0;
    overflow: hidden;
    width: 100%;
    padding: 0 16px;
}

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

.stage {
    position: relative;
    width: min(100%, var(--board-w));
    aspect-ratio: var(--board-w) / var(--board-h);
    margin: 0 auto;
    overflow: hidden;
    flex: 0 0 auto;
}

#game {
    position: absolute;
    top: 0;
    left: 0;
    width: var(--board-w);
    height: var(--board-h);
}

#statusText {
    margin: 0;
    padding: 0;
    text-align: left;
    color: var(--muted);
    font-size: 16px;
    min-height: 1.4em;
}

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

.bottombar {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
    align-items: center;
    gap: 16px;
    padding: 12px 24px 20px;
}

.bottombar__actions {
    grid-column: 2;
    display: flex;
    justify-content: center;
    gap: 12px;
}

#statusText {
    grid-column: 1;
    justify-self: start;
}

.settings-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.45);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 9999;
}

.settings-overlay[hidden] {
    display: none !important;
}

.settings-card {
    background: #181818;
    border: 1px solid var(--grid-strong);
    border-radius: 16px;
    padding: 16px;
    width: min(420px, calc(100% - 28px));
    box-shadow: 0 18px 48px rgba(0, 0, 0, 0.55);
}

.settings-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 8px;
}

.settings-head h2 {
    margin: 0;
    font-size: 18px;
}

.settings-body {
    display: grid;
    gap: 12px;
}

.settings-body fieldset {
    margin: 0;
    padding: 10px;
    border: 1px solid var(--grid);
    border-radius: 12px;
}

.settings-body legend {
    padding: 0 6px;
    color: var(--muted);
    font-size: 12px;
}

.settings-foot {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    margin-top: 12px;
}

.btn {
    min-height: 36px;
    padding: 8px 18px;
    color: var(--ink);
    background: #151515;
    border: 1px solid var(--grid-strong);
    border-radius: 999px;
    font-size: 14px;
    font-family: inherit;
    cursor: pointer;
}

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

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

.btn-accent {
    color: #111;
    background: var(--brand);
    border-color: var(--brand);
    font-weight: 600;
}

.btn-accent:hover {
    filter: brightness(1.1);
}

.tile {
    position: absolute;
    pointer-events: none;
}

.tileFront {
    position: absolute;
    overflow: hidden;
    cursor: default;
    pointer-events: none;
    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;
    pointer-events: auto;
}

.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;
    pointer-events: none;
    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;
}

.fly-layer {
    position: fixed;
    inset: 0;
    z-index: 10000;
    pointer-events: none;
}

.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;
    }
}
