:root {
    --color-primary: #7c3aed; --color-primary-dark: #5b21b6; --color-primary-light: #a78bfa;
    --color-accent: #14b8a6; --color-accent-dark: #0f766e;
    --color-danger: #ef4444; --color-danger-dark: #b91c1c;
    --color-background: #f8fafc; --color-text: #1e293b; --color-text-light: #64748b;
    --color-board-light: #fff; --color-board-dark: #f1f5f9;
    --border-radius-sm: 8px; --border-radius-md: 12px; --border-radius-lg: 16px;
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -2px rgba(0, 0, 0, 0.1);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -4px rgba(0, 0, 0, 0.1);
}

/* ОБЩИЕ СБРОСЫ / GENERAL RESETS */
* { 
    box-sizing: border-box; 
    user-select: none; 
    -webkit-tap-highlight-color: transparent; 
    -webkit-touch-callout: none; 
    outline: none; 
    touch-action: manipulation;
}

html, body {
    width: 100%; height: 100%;
    overflow: hidden !important;
    overscroll-behavior: none;
    position: fixed; top: 0; left: 0;
    background-color: var(--color-background);
    font-family: 'Montserrat', -apple-system, sans-serif;
    color: var(--color-text);
    -webkit-text-size-adjust: none;
}

body.loading { opacity: 0; transition: opacity 0.3s ease-in-out; }

/* ГЛАВНЫЙ ВРАППЕР / MAIN WRAPPER */
.game-wrapper {
    width: 100%; height: 100%;
    display: flex; justify-content: center;
    position: relative; z-index: 1;
}

/* ИГРОВОЙ КОНТЕЙНЕР / GAME CONTAINER */
.game-container {
    width: 90%; max-width: 650px; height: 85%;
    display: flex; flex-direction: column;
    padding: 10px;
    position: relative; overflow: hidden;
}

.hidden { display: none !important; }

/* ---- ШАПКА / HEADER ---- */
.game-header {
    display: flex; flex-direction: column; align-items: center;
    width: 100%; margin-bottom: 5px; flex-shrink: 0;
}

.game-title {
    font-size: 2rem; font-weight: 900;
    color: var(--color-primary); margin: 0 0 5px 0;
    text-transform: uppercase; letter-spacing: 1px;
    align-items: center;
    text-shadow: 2px 2px 0 rgba(0,0,0,0.1);
}

.header-controls {
    display: flex; justify-content: space-between; align-items: center;
    width: 100%; gap: 10px;
}

.control-btn {
    width: 40px; height: 40px;
    background-color: #fff;
    border: 2px solid #e2e8f0; border-radius: 12px;
    cursor: pointer; display: flex; justify-content: center; align-items: center;
    box-shadow: 0 2px 0 #cbd5e1; color: var(--color-text-light);
}
.control-btn:active { transform: translateY(2px); box-shadow: none; }

.score-box {
    background-color: #fff; padding: 5px 15px;
    border-radius: 12px; box-shadow: 0 2px 0 #cbd5e1;
    border: 2px solid #e2e8f0;
    display: flex; align-items: center; gap: 10px;
    min-width: 120px; justify-content: center;
}
.score-text { display: flex; flex-direction: column; line-height: 1; text-align: left; }
.score-text span:first-child { font-size: 0.7rem; font-weight: 700; color: var(--color-text-light); text-transform: uppercase; }
#current-score { font-size: 1.3rem; font-weight: 900; color: var(--color-text); }

/* ---- ЦЕНТРАЛЬНАЯ ОБЛАСТЬ / CENTER AREA ---- */
.board-area-wrapper {
    display: flex; justify-content: space-between; align-items: center;
    flex-grow: 1; width: 100%;
    overflow: hidden; 
    gap: 10px; padding: 5px;
}

/* === ГРАДУСНИК / THERMOMETER (LEFT) === */
.thermometer-container {
    flex: 0 0 40px; height: 100%; max-height: 80%;
    display: flex; flex-direction: column;
    align-items: center; justify-content: space-between;
}

.thermometer-label {
    font-size: 0.7rem; font-weight: 700; color: var(--color-text-light);
    writing-mode: horizontal-tb;
    text-transform: uppercase; margin-bottom: 2px;
}

.thermometer-glass {
    flex-grow: 1; width: 16px;
    background-color: #e2e8f0; border-radius: 10px;
    position: relative; overflow: hidden;
    box-shadow: inset 0 2px 4px rgba(0,0,0,0.1);
    border: 2px solid #fff; margin: 5px 0;
}

.thermometer-fill {
    position: absolute; bottom: 0; left: 0; width: 100%;
    background: linear-gradient(to top, var(--color-primary), var(--color-accent));
    transition: height 0.5s cubic-bezier(0.4, 0, 0.2, 1);
    border-radius: 8px;
}
.thermometer-value { font-size: 0.9rem; font-weight: 800; color: var(--color-primary); margin-top: 5px; }

/* === ИГРОВАЯ ДОСКА / BOARD (CENTER) === */
#board-container {
    flex: 2 2 auto;
    aspect-ratio: 1 / 1;
    
    background-color: #e2e8f0; /* Цвет линий сетки */
    border: 4px solid #fff;
    border-radius: var(--border-radius-lg);
    box-shadow: var(--shadow-lg);

    display: grid;
    grid-template-columns: repeat(9, 1fr);
    grid-template-rows: repeat(9, 1fr);
    gap: 2px; /* Сетка через gap - самое стабильное решение */
    
    align-self: center;
    
    /* Изоляция лайаута - предотвращает влияние детей на родителя */
    contain: layout size;
    min-height: 0; min-width: 0;
}

/* === ФИГУРЫ СПРАВА / PIECES (RIGHT) === */
#pieces-container {
    /* [SAFEGUARD] Увеличена ширина до 100px.
       Фиксированный размер (0 0) предотвращает сжатие/расширение */
    flex: 0 0 90px;
    height: 100%; max-height: 85%;
    display: flex; flex-direction: column;
    justify-content: space-around; align-items: center;
    
    /* Скрыть скроллбары, если вдруг вылезет, но visible для hover эффекта  */
    overflow: visible; 
}
.piece-slot {
    width: 100%; height: 30%; position: relative;
    display: flex; justify-content: center; align-items: center;
}
.piece {
    display: grid; gap: 2px; cursor: pointer;
    transition: transform 0.1s;
    transform-origin: center center;
}

/* [CRITICAL FIX] 
   Вместо transform: scale (который может оставлять занимаемое место),
   мы физически уменьшаем размер блоков в боковой панели.
   15px * 5 блоков + 8px gap = 83px. Это меньше, чем 100px контейнер.
   Больше никаких скачков! */
#pieces-container .piece-block {
    width: 16px; 
    height: 16px;
}
#pieces-container .piece:hover { transform: scale(1.1); }
#pieces-container .piece.unplaceable { opacity: 0.4; filter: grayscale(100%); }

/* ЯЧЕЙКИ И БЛОКИ */
.cell {
    position: relative;
    background-color: #fff; 
    will-change: background-color;
}
.cell-light { background-color: var(--color-board-light); }
.cell-dark { background-color: var(--color-board-dark); }

.cell.ghost {
    opacity: 0.5 !important;
    border-radius: 4px; z-index: 5;
    transform: scale(0.9);
}

.cell.filled, .piece-block.visible {
    background: linear-gradient(135deg, var(--color-primary-light), var(--color-primary));
    box-shadow: inset 0 0 0 1px rgba(0,0,0,0.05);
    border-radius: 2px;
    z-index: 2;
}

/* Lego effect */
.cell.filled::after, .piece-block.visible::after {
    content: ''; position: absolute;
    top: 50%; left: 50%; transform: translate(-50%, -50%);
    width: 60%; height: 60%; border-radius: 50%;
    background: rgba(255,255,255,0.25);
    box-shadow: 0 1px 2px rgba(0,0,0,0.1);
    pointer-events: none;
}

/* Стандартный размер блоков (например, при драге или на поле используются размеры сетки) */
.piece-block { width: 18px; height: 18px; position: relative; border-radius: 2px; }

/* АНИМАЦИИ */
.cell.just-placed { animation: pop-in 0.25s cubic-bezier(0.175, 0.885, 0.32, 1.275); }
@keyframes pop-in { 0% { transform: scale(0); } 100% { transform: scale(1); } }

.cell.clearing { 
    animation: explode 0.4s forwards ease-out; 
    z-index: 10; 
}
@keyframes explode { 
    0% { transform: scale(1); opacity: 1; filter: brightness(1.2); } 
    50% { transform: scale(1.4); opacity: 0.8; }
    100% { transform: scale(0); opacity: 0; } 
}

/* === ПОХВАЛА (Замедлено исчезновение) === */
#praise-container {
    position: fixed; top: 0; left: 0; width: 100%; height: 100%;
    z-index: 9999; pointer-events: none;
}
.praise-popup {
    position: absolute; right: 20px; top: 35%;
    width: 180px; height: 180px;
    background-size: contain; background-repeat: no-repeat; background-position: center;
    /* Увеличена длительность и изменена кривая прозрачности */
    animation: praise-entry 1.4s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
}
@keyframes praise-entry {
    0% { transform: scale(0) rotate(-10deg); opacity: 0; }
    20% { transform: scale(1.1) rotate(5deg); opacity: 1; }
    40% { transform: scale(1) rotate(0deg); opacity: 1; }
    85% { transform: scale(1) translateY(0); opacity: 1; } /* Долго висит */
    100% { transform: scale(0.8) translateY(-20px); opacity: 0; }
}

/* МОДАЛЬНЫЕ ОКНА */
.fullscreen-modal-container {
    position: fixed; top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(15, 23, 42, 0.95); backdrop-filter: blur(4px);
    display: flex; justify-content: center; align-items: center;
    z-index: 10000;
}
.modal-window {
    background: #fff; padding: 25px; border-radius: 24px;
    text-align: center; width: 85%; max-width: 320px;
    box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
    display: flex; flex-direction: column; gap: 15px;
}
.modal-window h1 { margin: 0; font-size: 2.2rem; color: var(--color-primary); }
.modal-window h2 { margin: 0; font-size: 1.8rem; color: var(--color-text); }

.btn {
    border: none; padding: 14px; border-radius: 12px;
    font-size: 1.1rem; font-weight: 800; cursor: pointer; font-family: inherit; letter-spacing: 0.5px;
}
.btn-primary {
    background: linear-gradient(to bottom right, var(--color-primary-light), var(--color-primary));
    color: #fff; box-shadow: 0 4px 6px -1px rgba(124, 58, 237, 0.3);
}
.btn-primary:active { transform: scale(0.98); }

.floating-piece {
    position: absolute; z-index: 5000; pointer-events: none; opacity: 0.95;
    filter: drop-shadow(0 10px 15px rgba(0,0,0,0.2));
}

/* === КОНФЕТТИ (Исправлен разлет) === */
#confetti-container { 
    position: fixed; top: 0; left: 0; width: 100%; height: 100%; 
    pointer-events: none; z-index: 5000; 
    display: flex; justify-content: center; align-items: center; 
}
.confetti-particle {
    position: absolute; width: 10px; height: 10px; border-radius: 2px;
    opacity: 0;
}

/* Разные направления полета */
.confetti-particle:nth-child(1n) { animation: bang-1 1s ease-out forwards; }
.confetti-particle:nth-child(2n) { animation: bang-2 1s ease-out forwards; }
.confetti-particle:nth-child(3n) { animation: bang-3 1s ease-out forwards; }
.confetti-particle:nth-child(4n) { animation: bang-4 1s ease-out forwards; }
.confetti-particle:nth-child(5n) { animation: bang-5 1s ease-out forwards; }
.confetti-particle:nth-child(6n) { animation: bang-6 1s ease-out forwards; }
.confetti-particle:nth-child(7n) { animation: bang-7 1s ease-out forwards; }

@keyframes bang-1 { 0% { transform: translate(0,0); opacity:1; } 100% { transform: translate(200px, -100px) rotate(100deg); opacity:0; } }
@keyframes bang-2 { 0% { transform: translate(0,0); opacity:1; } 100% { transform: translate(-200px, -150px) rotate(-140deg); opacity:0; } }
@keyframes bang-3 { 0% { transform: translate(0,0); opacity:1; } 100% { transform: translate(100px, 200px) rotate(400deg); opacity:0; } }
@keyframes bang-4 { 0% { transform: translate(0,0); opacity:1; } 100% { transform: translate(-150px, 150px) rotate(-200deg); opacity:0; } }
@keyframes bang-5 { 0% { transform: translate(0,0); opacity:1; } 100% { transform: translate(0px, -250px) rotate(90deg); opacity:0; } }
@keyframes bang-6 { 0% { transform: translate(0,0); opacity:1; } 100% { transform: translate(250px, 50px) rotate(180deg); opacity:0; } }
@keyframes bang-7 { 0% { transform: translate(0,0); opacity:1; } 100% { transform: translate(-50px, -200px) rotate(-90deg); opacity:0; } }
