* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    -webkit-touch-callout: none;
    -webkit-tap-highlight-color: transparent;
    
    /* Адаптивный шрифт для читаемости на любых экранах */
    font-size: clamp(10px, 1.3vw, 24px);
}

html {
    font-size: 16px;
}

html, body {
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
    overflow: hidden;
    overscroll-behavior: none; /* блокирует pull-to-refresh и эффекты оверскролла */
}

body {
    font-family: 'Courier New', monospace;
    font-size: 1rem;
    background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%);
    background-attachment: fixed;
    color: #fff;
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100%;
    position: fixed; /* фиксируем тело, чтобы оно не смещалось на iOS при pull-to-refresh */
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    touch-action: none; /* блокируем жесты прокрутки/обновления */
}

#game-wrapper {
    /* КЛЮЧЕВОЕ РЕШЕНИЕ: Фиксированные размеры для игровой логики */
    /* Высота увеличена до 950px для размещения всего контента с запасом */
    width: 1400px;
    height: 950px;
    
    /* КЛЮЧЕВОЕ РЕШЕНИЕ: Центрирование через transform */
    position: fixed;
    top: 50%;
    left: 50%;
    transform-origin: center center;
    
    /* Будет установлен из JavaScript: transform: translate(-50%, -50%) scale(X); */
    
    /* Оптимизация для плавного масштабирования */
    will-change: transform;
    
    /* Внутренняя структура */
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 1.25rem;
    box-sizing: border-box;
    overflow: hidden;
    touch-action: none; /* дополнительно блокируем жесты внутри игры */
}

.container {
    /* Фиксированные размеры в логических пикселях */
    width: 1350px; /* 1400px - padding (1.25rem * 2) */
    max-width: 1350px;
    /* Высота 100% от родителя, но с учетом padding game-wrapper */
    height: calc(100% - 2.5rem); /* 100% минус padding game-wrapper (1.25rem * 2) */
    background: rgba(0, 0, 0, 0.7);
    border-radius: 0.9375rem;
    padding: 1.25rem;
    box-shadow: 0 0.625rem 2.5rem rgba(0, 0, 0, 0.5);
    display: flex;
    flex-direction: column;
    align-items: center;
    overflow: hidden;
    box-sizing: border-box;
}

h1 {
    text-align: center;
    /* Адаптивный шрифт через clamp */
    font-size: clamp(1.2rem, 2.5vw, 2.5em);
    margin-bottom: 1.25rem;
    text-shadow: 0.1875rem 0.1875rem 0px #000;
    color: #4fc3f7;
    pointer-events: none;
}

.game-info {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1.25rem;
    flex-wrap: wrap;
    gap: 0.625rem;
    width: 100%;
}

.info-item {
    background: rgba(255, 255, 255, 0.1);
    padding: 0.625rem 1.25rem;
    border-radius: 0.5rem;
    font-size: 1.2em;
    font-weight: bold;
    pointer-events: none;
}

.timer {
    color: #ffeb3b;
}

.score {
    color: #4caf50;
}

.high-score {
    color: #ff9800;
}

.controls {
    display: flex;
    gap: 0.625rem;
    justify-content: center;
    margin-bottom: 1.25rem;
    flex-wrap: wrap;
    width: 100%;
}

button {
    padding: 0.75rem 1.5rem;
    font-size: 1.1em;
    font-family: 'Courier New', monospace;
    font-weight: bold;
    border: 0.1875rem solid #fff;
    border-radius: 0.5rem;
    background: #2196f3;
    color: #fff;
    cursor: pointer;
    transition: all 0.3s;
    text-transform: uppercase;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

button:hover {
    background: #1976d2;
    transform: scale(1.05);
}

button:active {
    transform: scale(0.95);
}

button:disabled {
    background: #666;
    cursor: not-allowed;
    opacity: 0.6;
}

.game-board {
    display: grid;
    grid-template-columns: repeat(10, 1fr);
    gap: 0.125rem;
    background: #000;
    padding: 0.125rem;
    border: 0.25rem solid #333;
    border-radius: 0.5rem;
    margin: 0 auto;
    /* Фиксированные размеры в логических пикселях */
    width: 600px;
    height: 600px;
    max-width: 600px;
    max-height: 600px;
    touch-action: none;
}

.cell {
    aspect-ratio: 1;
    background: #2a2a2a;
    position: relative;
    cursor: pointer;
    border: 1px solid #444;
    transition: transform 0.2s;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

.cell:hover {
    transform: scale(1.1);
    z-index: 10;
    box-shadow: 0 0 10px rgba(79, 195, 247, 0.5);
}

.cell.start {
    background: #4caf50;
}

.cell.end {
    background: #f44336;
}

.cell.water {
    background: #03a9f4 !important;
    animation: waterFlow 0.5s ease-in-out;
}

@keyframes waterFlow {
    0% {
        background: #2a2a2a;
    }
    100% {
        background: #03a9f4;
    }
}

.pipe {
    width: 100%;
    height: 100%;
    position: relative;
    pointer-events: none;
}

/* Прямая горизонтальная */
.pipe-horizontal::before {
    content: '';
    position: absolute;
    top: 50%;
    left: 0;
    right: 0;
    height: 30%;
    background: #8b4513;
    transform: translateY(-50%);
    border: 2px solid #654321;
}

/* Прямая вертикальная */
.pipe-vertical::before {
    content: '';
    position: absolute;
    left: 50%;
    top: 0;
    bottom: 0;
    width: 30%;
    background: #8b4513;
    transform: translateX(-50%);
    border: 2px solid #654321;
}

/* Угловая (верх-право) */
.pipe-corner-tr::before {
    content: '';
    position: absolute;
    top: 0;
    left: 50%;
    width: 30%;
    height: 50%;
    background: #8b4513;
    border: 2px solid #654321;
    border-bottom: none;
}
.pipe-corner-tr::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 50%;
    height: 30%;
    background: #8b4513;
    border: 2px solid #654321;
    border-left: none;
}

/* Угловая (верх-лево) */
.pipe-corner-tl::before {
    content: '';
    position: absolute;
    top: 0;
    left: 20%;
    width: 30%;
    height: 50%;
    background: #8b4513;
    border: 2px solid #654321;
    border-bottom: none;
}
.pipe-corner-tl::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 0;
    width: 50%;
    height: 30%;
    background: #8b4513;
    border: 2px solid #654321;
    border-right: none;
}

/* Угловая (низ-право) */
.pipe-corner-br::before {
    content: '';
    position: absolute;
    bottom: 0;
    left: 50%;
    width: 30%;
    height: 50%;
    background: #8b4513;
    border: 2px solid #654321;
    border-top: none;
}
.pipe-corner-br::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 50%;
    height: 30%;
    background: #8b4513;
    border: 2px solid #654321;
    border-left: none;
}

/* Угловая (низ-лево) */
.pipe-corner-bl::before {
    content: '';
    position: absolute;
    bottom: 0;
    left: 20%;
    width: 30%;
    height: 50%;
    background: #8b4513;
    border: 2px solid #654321;
    border-top: none;
}
.pipe-corner-bl::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 0;
    width: 50%;
    height: 30%;
    background: #8b4513;
    border: 2px solid #654321;
    border-right: none;
}

/* Тройник (верх-право-низ) */
.pipe-t-vertical::before {
    content: '';
    position: absolute;
    left: 50%;
    top: 0;
    bottom: 0;
    width: 30%;
    background: #8b4513;
    transform: translateX(-50%);
    border: 2px solid #654321;
}
.pipe-t-vertical::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 50%;
    height: 30%;
    background: #8b4513;
    transform: translateY(-50%);
    border: 2px solid #654321;
    border-left: none;
}

/* Тройник (лево-верх-право) */
.pipe-t-horizontal::before {
    content: '';
    position: absolute;
    top: 50%;
    left: 0;
    right: 0;
    height: 30%;
    background: #8b4513;
    transform: translateY(-50%);
    border: 2px solid #654321;
}
.pipe-t-horizontal::after {
    content: '';
    position: absolute;
    top: 0;
    left: 50%;
    width: 30%;
    height: 50%;
    background: #8b4513;
    transform: translateX(-50%);
    border: 2px solid #654321;
    border-bottom: none;
}

/* Крестовина */
.pipe-cross::before {
    content: '';
    position: absolute;
    left: 50%;
    top: 0;
    bottom: 0;
    width: 30%;
    background: #8b4513;
    transform: translateX(-50%);
    border: 2px solid #654321;
}
.pipe-cross::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 0;
    right: 0;
    height: 30%;
    background: #8b4513;
    transform: translateY(-50%);
    border: 2px solid #654321;
}

.message {
    text-align: center;
    font-size: 2em;
    font-weight: bold;
    margin-top: 1.25rem;
    padding: 1.25rem;
    border-radius: 0.5rem;
    display: none;
    pointer-events: none;
}

.message.success {
    background: rgba(76, 175, 80, 0.3);
    color: #4caf50;
    display: block;
}

.message.failure {
    background: rgba(244, 67, 54, 0.3);
    color: #f44336;
    display: block;
}

/* Реклама с обратным отсчетом */
.ad-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.9);
    display: none;
    justify-content: center;
    align-items: center;
    z-index: 10000;
    flex-direction: column;
}

.ad-overlay.active {
    display: flex;
}

.ad-countdown {
    font-size: 4em;
    font-weight: bold;
    color: #fff;
    text-shadow: 0 0 20px rgba(79, 195, 247, 0.8);
    margin-bottom: 20px;
    animation: pulse 1s infinite;
}

@keyframes pulse {
    0%, 100% {
        transform: scale(1);
    }
    50% {
        transform: scale(1.1);
    }
}

/* Пауза игры */
.game-paused {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.7);
    display: none;
    justify-content: center;
    align-items: center;
    z-index: 9999;
    flex-direction: column;
}

.game-paused.active {
    display: flex;
}

.pause-message {
    font-size: 3em;
    font-weight: bold;
    color: #fff;
    text-shadow: 3px 3px 0px #000;
}

/* Оверлей следующего уровня с большой кнопкой */
.next-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.8);
    display: none;
    justify-content: center;
    align-items: center;
    z-index: 10001;
    flex-direction: column;
    padding: 1.25rem;
}

.next-overlay.active {
    display: flex;
}

.next-overlay #newLevelBig {
    padding: 1.25rem 2.5rem;
    font-size: 2em;
    border-width: 0.25rem;
    background: #4caf50;
    color: #fff;
    text-transform: uppercase;
    box-shadow: 0 0.75rem 2rem rgba(0, 0, 0, 0.4);
}

.next-overlay #newLevelBig:hover {
    background: #43a047;
    transform: scale(1.06);
}

/* Оверлей поражения с большой кнопкой */
.defeat-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.8);
    display: none;
    justify-content: center;
    align-items: center;
    z-index: 10001;
    flex-direction: column;
    padding: 1.25rem;
}

.defeat-overlay.active {
    display: flex;
}

.defeat-overlay .defeat-message {
    font-size: 2.5em;
    font-weight: bold;
    color: #f44336;
    text-shadow: 3px 3px 0px #000;
    margin-bottom: 2rem;
    text-align: center;
}

.defeat-overlay #newLevelDefeat {
    padding: 1.25rem 2.5rem;
    font-size: 2em;
    border-width: 0.25rem;
    background: #f44336;
    color: #fff;
    text-transform: uppercase;
    box-shadow: 0 0.75rem 2rem rgba(0, 0, 0, 0.4);
}

.defeat-overlay #newLevelDefeat:hover {
    background: #d32f2f;
    transform: scale(1.06);
}

@media (max-width: 768px) {
    /* Медиа-запросы используются только для небольших корректировок,
       основные размеры фиксированы и масштабируются через transform: scale() */
    .container {
        padding: 0.9375rem;
    }

    .game-info {
        gap: 0.5rem;
        flex-direction: column;
        width: 100%;
    }

    .info-item {
        padding: 0.5rem 1rem;
        width: 100%;
        text-align: center;
    }

    .controls {
        gap: 0.5rem;
        width: 100%;
    }

    button {
        padding: 0.625rem 1.25rem;
        flex: 1;
        min-width: 0;
    }

    /* Размеры игрового поля фиксированы, не изменяем их в медиа-запросах */

    .message {
        font-size: 1.5em;
        padding: 0.9375rem;
        margin-top: 0.9375rem;
    }
}

@media (max-width: 480px) {
    /* Медиа-запросы используются только для небольших корректировок,
       основные размеры фиксированы и масштабируются через transform: scale() */

    .game-info {
        margin-bottom: 0.9375rem;
    }

    .controls {
        margin-bottom: 0.9375rem;
    }
}

@media (orientation: landscape) and (max-height: 600px) {
    /* Медиа-запросы используются только для небольших корректировок,
       основные размеры фиксированы и масштабируются через transform: scale() */
    .container {
        padding: 0.625rem;
    }

    .game-info {
        margin-bottom: 0.5rem;
    }

    .info-item {
        padding: 0.375rem 0.75rem;
        font-size: 0.9em;
    }

    .controls {
        margin-bottom: 0.5rem;
    }

    button {
        padding: 0.5rem 1rem;
        font-size: 0.9em;
    }
}

