:root {
    --bg-color: #3b2314;
    --text-color: #f7e8ce;
    --tile-width: 44px;
    /* base half-width */
    --tile-height: 56px;
    /* base half-height */
    --z-offset-x: -4px;
    /* small shift to give 3d stacking feel */
    --z-offset-y: -4px;
}

body,
html {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
    background-color: var(--bg-color);
    font-family: 'Arial', sans-serif;
    color: var(--text-color);
    overflow: hidden;
    /* no scrolling */
    user-select: none;
    -webkit-user-select: none;
    -ms-user-select: none;
}

#game-ui {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    /* Let clicks pass to board usually */
    display: flex;
    flex-direction: column;
}

#top-bar {
    display: flex;
    justify-content: space-between;
    padding: 10px 20px;
    font-size: 1.2rem;
    font-weight: bold;
    text-shadow: 1px 1px 2px #000;
    pointer-events: auto;
    z-index: 1000;
}

#right-buttons {
    position: absolute;
    right: 20px;
    top: 50%;
    transform: translateY(-50%);
    display: flex;
    flex-direction: column;
    gap: 15px;
    pointer-events: auto;
    z-index: 1000;
}

.icon-btn {
    width: 60px;
    height: 60px;
    background: #6c4623;
    border: 3px solid #3c1e08;
    border-radius: 12px;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.5), inset 0 2px 5px rgba(255, 255, 255, 0.2);
    display: flex;
    justify-content: center;
    align-items: center;
    color: white;
    font-size: 24px;
    cursor: pointer;
    background-image: linear-gradient(180deg, #875c32, #5a3514);
}

.icon-btn:active {
    transform: translateY(2px);
    box-shadow: 0 2px 3px rgba(0, 0, 0, 0.5), inset 0 2px 5px rgba(255, 255, 255, 0.2);
}

#board-container {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    pointer-events: auto;
    overflow: hidden;
}

#board {
    position: absolute;
    left: 50%;
    top: 50%;
    /* Dimensions derived from max x/y in layouts */
    width: calc(32 * calc(var(--tile-width) / 2));
    height: calc(18 * calc(var(--tile-height) / 2));
    transform-origin: center center;
    transition: transform 0.25s ease-out;
}

.tile {
    position: absolute;
    width: var(--tile-width);
    height: var(--tile-height);
    /* background: #fffdf4;
    border-bottom: 6px solid #8b7552;
    border-right: 4px solid #9c845e;
    border-left: 2px solid #e3ccaa;
    border-top: 1px solid #ffebcd;
    box-shadow: 3px 4px 8px rgba(0,0,0,0.5); */
    box-sizing: border-box;
    cursor: pointer;
    background-position: center;
    background-size: 85%;
    background-repeat: no-repeat;
    transition: transform 0.1s;
}



.tile.selected {
    transform: translateY(-5px);
    box-shadow: 0 0 10px #ffeb3b, 4px 6px 8px rgba(0, 0, 0, 0.4);
    filter: brightness(1.2);
}

.tile.blocked {
    filter: brightness(0.7) contrast(0.9);
    cursor: default;
}

.tile.hinted {
    animation: hintBlink 1s infinite alternate;
}

.tile.matched {
    animation: matchFade 0.3s forwards;
    pointer-events: none;
}

@keyframes hintBlink {
    0% {
        filter: brightness(1);
        box-shadow: 0 0 5px #ff5722;
    }

    100% {
        filter: brightness(1.5);
        box-shadow: 0 0 15px #ff5722;
    }
}

@keyframes matchFade {
    0% {
        transform: scale(1);
        opacity: 1;
    }

    100% {
        transform: scale(1.5);
        opacity: 0;
    }
}

#modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.8);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 2000;
}

.modal {
    background: #4a2f1d;
    padding: 30px 40px;
    border-radius: 12px;
    border: 4px solid #9e6f43;
    text-align: center;
    color: white;
    box-shadow: 0 10px 20px rgba(0, 0, 0, 0.6);
}

.modal h2 {
    margin-top: 0;
    font-size: 2em;
    color: #ffd700;
}

.modal button {
    padding: 10px 20px;
    font-size: 1.2em;
    background: #2e8b57;
    color: white;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    margin-top: 20px;
}

.modal button:hover {
    background: #3cb371;
}

.hidden {
    display: none !important;
}

#mode-selection {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.4);
    backdrop-filter: blur(5px);
    z-index: 2000;
    overflow-y: auto;
    display: flex;
    padding: 20px;
    box-sizing: border-box;
}

.mode-grid {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    gap: 20px;
    padding: 20px;
    background: rgba(43, 22, 10, 0.9);
    border: 4px solid #6b432a;
    border-radius: 12px;
    margin: auto;
    width: 100%;
    max-width: 680px;
    box-sizing: border-box;
}

@media (max-width: 600px) {
    .mode-grid {
        grid-template-columns: 1fr 1fr;
        gap: 10px;
        padding: 10px;
    }
}

.mode-btn {
    background: #8b5b33;
    border: 3px solid #523118;
    border-radius: 8px;
    color: white;
    width: 100%;
    min-width: 0;
    max-width: 200px;
    height: 240px;
    cursor: pointer;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    align-items: center;
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.5);
    transition: transform 0.1s, background 0.2s;
    overflow: hidden;
    box-sizing: border-box;
    margin: 0 auto;
}

.mode-btn:hover {
    background: #9d683a;
    transform: translateY(-2px);
}

.mode-btn:active {
    transform: translateY(2px);
}

.mode-diff {
    width: 100%;
    padding: 8px 0;
    background: rgba(0, 0, 0, 0.2);
    font-size: 16px;
    font-weight: bold;
    border-bottom: 2px solid rgba(0, 0, 0, 0.2);
}

.mode-preview {
    flex-grow: 1;
    width: 100%;
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
}

.mini-board {
    position: relative;
    transform-origin: center center;
}

.mini-tile {
    position: absolute;
    background: #ffffff;
    border: 1px solid #cccccc;
    box-shadow: -1px 1px 1px rgba(0, 0, 0, 0.4);
    box-sizing: border-box;
    border-radius: 2px;
}

.mode-name {
    width: 100%;
    padding: 12px 0;
    background: rgba(0, 0, 0, 0.2);
    font-size: 18px;
    font-weight: bold;
    border-top: 2px solid rgba(0, 0, 0, 0.2);
}

/* For mobile portrait scaling logic */
/* Handled mainly in JS to calculate exact transform: scale(X) */

@media (max-width: 600px) {
    #right-buttons {
        top: auto;
        bottom: 20px;
        right: auto;
        left: 50%;
        transform: translateX(-50%);
        flex-direction: row;
        gap: 15px;
        z-index: 2000;
        /* Ensure on top of tiles sitting near bottom */
    }

    #board-container {
        /* Leave padding at bottom for controls */
        bottom: 80px;
    }
}

@media (min-width: 769px) {
    .mode-grid {
        gap: 30px;
        padding: 30px;
        max-width: 960px;
    }

    .mode-btn {
        max-width: 280px;
    }
}