/* Общий движок настольных игр-бродилок (roll-and-move).
   Фон страницы и картинку доски каждая игра задаёт у себя в <style>. */

body, html {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
    font-family: sans-serif;
    overscroll-behavior: none;
    -webkit-text-size-adjust: 100%;

    background-color: #222;
    background-repeat: no-repeat;
    background-position: center center;
    background-attachment: fixed;
    background-size: cover;

    display: flex;
    align-items: center;
    justify-content: center;
}

#game-viewport {
    width: 100vw;
    height: 100dvh;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
}

/* Игровое поле */
#game-board {
    --token-size: clamp(42px, 5.4vmin, 50px);
    --dice-size: 60px;
    --dice-depth: 30px;
    --dice-back: -30px;

    width: calc(100vw - 4px);
    height: calc(100dvh - 4px);
    min-height: 320px;

    border: 2px solid rgba(255, 255, 255, 0.3);
    border-radius: 15px;
    box-sizing: border-box;

    background-color: transparent;
    background-size: contain;
    background-position: center;
    background-repeat: no-repeat;

    position: relative;
    box-shadow: 0 0 20px rgba(0,0,0,0.8);
    touch-action: none;
    flex: 0 0 auto;
}

/* --- КНОПКА ОТКРЫТИЯ --- */
#open-btn {
    position: absolute;
    top: max(15px, env(safe-area-inset-top));
    left: max(15px, env(safe-area-inset-left));
    width: 48px;
    height: 48px;
    background: rgba(255, 255, 255, 0.9);
    border-radius: 8px;
    cursor: pointer;
    z-index: 900;
    display: none;
    align-items: center;
    justify-content: center;
    font-size: 24px;
    box-shadow: 0 4px 6px rgba(0,0,0,0.3);
    user-select: none;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}
#open-btn:hover {
    background: #fff;
    transform: scale(1.05);
}

/* --- ПАНЕЛЬ УПРАВЛЕНИЯ --- */
#controls {
    position: absolute;
    top: max(15px, env(safe-area-inset-top));
    left: max(15px, env(safe-area-inset-left));
    width: 280px;
    max-width: calc(100vw - 30px);
    background: rgba(255, 255, 255, 0.95);
    padding: 20px;
    padding-top: 35px;
    border-radius: 8px;
    z-index: 1000;
    box-shadow: 0 4px 15px rgba(0,0,0,0.5);
    transition: opacity 0.3s, visibility 0.3s, transform 0.3s;
    box-sizing: border-box;
    touch-action: pan-y;
    overscroll-behavior: contain;
}

.close-btn {
    position: absolute;
    top: 4px;
    right: 6px;
    width: 44px;
    height: 44px;
    font-size: 24px;
    font-weight: bold;
    color: #555;
    cursor: pointer;
    line-height: 44px;
    text-align: center;
    user-select: none;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}
.close-btn:hover { color: red; }

/* --- СТИЛИ ФИШЕК --- */
.token {
    width: var(--token-size);
    height: var(--token-size);
    border-radius: 50%;
    position: absolute;
    cursor: grab;
    box-shadow: 2px 5px 10px rgba(0,0,0,0.5);
    border: 2px solid white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: bold;
    color: white;
    text-shadow: 1px 1px 2px black;
    user-select: none;
    touch-action: none;
    -webkit-tap-highlight-color: transparent;
    transition: transform 0.1s;
    top: var(--token-y);
    left: var(--token-x);
}

.token:active {
    cursor: grabbing;
    transform: scale(1.1);
    z-index: 2000 !important;
}

/* Позиции фишек */
.red    { --token-x: 350px; --token-y: 30px; background: radial-gradient(circle at 30% 30%, #ff6b6b, #c0392b); }
.blue   { --token-x: 410px; --token-y: 30px; background: radial-gradient(circle at 30% 30%, #4facfe, #00f2fe); }
.green  { --token-x: 470px; --token-y: 30px; background: radial-gradient(circle at 30% 30%, #43e97b, #38f9d7); }

.yellow { --token-x: 350px; --token-y: 90px; background: radial-gradient(circle at 30% 30%, #f1c40f, #f39c12); }
.purple { --token-x: 410px; --token-y: 90px; background: radial-gradient(circle at 30% 30%, #9b59b6, #8e44ad); }
.orange { --token-x: 470px; --token-y: 90px; background: radial-gradient(circle at 30% 30%, #e67e22, #d35400); }

/* --- 3D КУБИК --- */
.scene {
    width: var(--dice-size);
    height: var(--dice-size);
    perspective: 600px;
    position: absolute;
    top: var(--dice-y, 40px);
    left: var(--dice-x, 560px);
    cursor: grab;
    z-index: 500;
    touch-action: none;
    -webkit-tap-highlight-color: transparent;
}

.cube {
    width: 100%;
    height: 100%;
    position: relative;
    transform-style: preserve-3d;
    transform: translateZ(var(--dice-back));
    transition: transform 1.5s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.cube__face {
    position: absolute;
    width: var(--dice-size);
    height: var(--dice-size);
    background: white;
    border: 2px solid #333;
    border-radius: 8px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: clamp(23px, 3.1vmin, 28px);
    font-weight: bold;
    color: #333;
    box-shadow: inset 0 0 15px rgba(0,0,0,0.1);
    user-select: none;
    background: linear-gradient(135deg, #fff 0%, #f0f0f0 100%);
}

.cube__face--1  { transform: rotateY(  0deg) translateZ(var(--dice-depth)); }
.cube__face--2  { transform: rotateY( 90deg) translateZ(var(--dice-depth)); }
.cube__face--3  { transform: rotateY(180deg) translateZ(var(--dice-depth)); }
.cube__face--4  { transform: rotateY(-90deg) translateZ(var(--dice-depth)); }
.cube__face--5  { transform: rotateX( 90deg) translateZ(var(--dice-depth)); }
.cube__face--6  { transform: rotateX(-90deg) translateZ(var(--dice-depth)); }

.hint {
    font-size: 13px;
    color: #444;
    margin-top: 10px;
    line-height: 1.4;
    border-top: 1px solid #ccc;
    padding-top: 10px;
}

.hint a {
    color: #007bff;
    text-decoration: none;
    font-weight: bold;
}
.hint a:hover {
    text-decoration: underline;
}

button.action-btn {
    width: 100%;
    min-height: 44px;
    padding: 10px 12px;
    background-color: #f0f0f0;
    border: 1px solid #ccc;
    border-radius: 4px;
    cursor: pointer;
    margin-top: 10px;
    touch-action: manipulation;
}
button.action-btn:hover { background-color: #e0e0e0; }

h3 { margin-top: 0; margin-bottom: 10px; font-size: 18px; }

@media (max-width: 767px), (pointer: coarse) and (max-width: 900px) {
    body, html {
        background-attachment: scroll;
    }

    #game-viewport {
        align-items: flex-start;
        justify-content: flex-start;
        overflow-x: auto;
        overflow-y: hidden;
        -webkit-overflow-scrolling: touch;
    }

    #game-board {
        width: 146.525vh;
        height: calc(100dvh - 4px);
        border-radius: 10px;
        background-size: 100% 100%;
        min-height: 0;
        touch-action: pan-x;
        --token-size: clamp(38px, 11vw, 46px);
        --dice-size: 56px;
        --dice-depth: 28px;
        --dice-back: -28px;
    }

    #controls {
        position: fixed;
        top: auto;
        right: max(12px, env(safe-area-inset-right));
        bottom: max(12px, env(safe-area-inset-bottom));
        left: max(12px, env(safe-area-inset-left));
        width: auto;
        max-width: none;
        max-height: min(70dvh, 420px);
        overflow: auto;
        padding: 16px;
        padding-top: 42px;
        border-radius: 12px;
    }

    #open-btn {
        position: fixed;
        top: max(12px, env(safe-area-inset-top));
        left: max(12px, env(safe-area-inset-left));
    }

    .red    { --token-x: 14px; --token-y: 74px; }
    .blue   { --token-x: calc(20px + var(--token-size)); --token-y: 74px; }
    .green  { --token-x: calc(26px + var(--token-size) + var(--token-size)); --token-y: 74px; }
    .yellow { --token-x: 14px; --token-y: calc(82px + var(--token-size)); }
    .purple { --token-x: calc(20px + var(--token-size)); --token-y: calc(82px + var(--token-size)); }
    .orange { --token-x: calc(26px + var(--token-size) + var(--token-size)); --token-y: calc(82px + var(--token-size)); }

    .scene {
        --dice-x: calc(32px + var(--token-size) + var(--token-size) + var(--token-size));
        --dice-y: 78px;
    }

    .hint {
        font-size: 14px;
    }
}

@supports not (height: 100dvh) {
    #game-board {
        height: calc(100vh - 4px);
    }
}
