:root {
    --primary-color: #3498db;
    --secondary-color: #e74c3c;
    --background-color: #2c3e50;
    --text-color: #ecf0f1;
    --button-color: #2980b9;
    --button-hover: #1abc9c;
    --safe-area-inset-top: env(safe-area-inset-top, 0px);
    --safe-area-inset-right: env(safe-area-inset-right, 0px);
    --safe-area-inset-bottom: env(safe-area-inset-bottom, 0px);
    --safe-area-inset-left: env(safe-area-inset-left, 0px);
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: 'Arial', sans-serif;
}

body {
    background-color: var(--background-color);
    color: var(--text-color);
    overflow: hidden;
    height: 100vh;
    width: 100vw;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: var(--safe-area-inset-top) var(--safe-area-inset-right) var(--safe-area-inset-bottom) var(--safe-area-inset-left);
}

#app {
    width: 100%;
    height: 100%;
    position: relative;
}

.layout {
    width: 100%;
    height: 100%;
    display: none;
}

.layout.active {
    display: flex;
}

.hidden {
    display: none !important;
}

/* Портретная ориентация */
#portrait-layout {
    flex-direction: column;
    justify-content: space-between;
    align-items: center;
    padding: 10px;
    gap: 20px;
}

#game-container {
    width: 100%;
    max-width: min(90vh, calc(100vw - 40px));
    aspect-ratio: 4/3;
    position: relative;
    flex-shrink: 0;
}

#portrait-controls {
    width: 100%;
    max-width: min(90vh, calc(100vw - 40px));
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px 0;
    gap: 20px;
}

/* Альбомная ориентация */
#landscape-layout {
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    padding: 10px;
    gap: 20px;
}

#game-container-landscape {
    width: auto;
    height: 100%;
    aspect-ratio: 4/3;
    position: relative;
    flex-shrink: 0;
}

#landscape-left-controls,
#landscape-right-controls {
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    flex: 1;
    min-width: 0;
}

#landscape-left-controls {
    justify-content: flex-end;
    padding-right: 20px;
}

#landscape-right-controls {
    justify-content: flex-start;
    padding-left: 20px;
}

/* Canvas */
canvas {
    display: block;
    width: 100%;
    height: 100%;
    background-color: #1a1a2e;
    border-radius: 12px;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
}

/* Стили для меню */
.screen {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    background-color: rgba(26, 26, 46, 0.95);
    border-radius: 8px;
    z-index: 20;
    padding: 20px;
}

h1 {
    font-size: clamp(2rem, 8vw, 3rem);
    margin-bottom: 2rem;
    color: var(--primary-color);
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
    text-align: center;
}

h2 {
    font-size: clamp(1.5rem, 6vw, 2rem);
    margin-bottom: 1.5rem;
    color: var(--primary-color);
    text-align: center;
}

.menu-buttons {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    width: 100%;
    max-width: 300px;
}

button {
    padding: 12px 24px;
    font-size: 1.2rem;
    background-color: var(--button-color);
    color: white;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.3s ease;
}

button:hover {
    background-color: var(--button-hover);
    transform: translateY(-2px);
}

/* Стили для HUD */
#hud,
#hud-landscape {
    position: absolute;
    top: 15px;
    left: 15px;
    z-index: 5;
    display: flex;
    gap: 15px;
}

.hud-item {
    display: flex;
    align-items: center;
    gap: 5px;
    background-color: rgba(0, 0, 0, 0.6);
    padding: 8px 12px;
    border-radius: 8px;
    font-size: 14px;
    backdrop-filter: blur(4px);
}

/* Элементы управления */
.controls-container {
    opacity: 0;
    transition: opacity 0.3s ease;
}

.controls-container.visible {
    opacity: 1;
}

.d-pad {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: repeat(3, 1fr);
    gap: 8px;
}

.d-pad-button {
    background-color: rgba(255, 255, 255, 0.25);
    border-radius: 12px;
    display: flex;
    justify-content: center;
    align-items: center;
    user-select: none;
    touch-action: none;
    transition: all 0.1s ease;
    aspect-ratio: 1;
    min-width: 44px;
    min-height: 44px;
}

.d-pad-button:active {
    background-color: rgba(255, 255, 255, 0.4);
    transform: scale(0.95);
}

.d-pad-up {
    grid-column: 2;
    grid-row: 1;
}

.d-pad-left {
    grid-column: 1;
    grid-row: 2;
}

.d-pad-right {
    grid-column: 3;
    grid-row: 2;
}

.d-pad-down {
    grid-column: 2;
    grid-row: 3;
}

.action-buttons {
    display: flex;
    gap: 20px;
}

.action-button {
    width: 70px;
    height: 70px;
    min-width: 60px;
    min-height: 60px;
    border-radius: 50%;
    background-color: rgba(231, 76, 60, 0.25);
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 1.5rem;
    user-select: none;
    touch-action: none;
    transition: all 0.1s ease;
}

.action-button:active {
    background-color: rgba(231, 76, 60, 0.4);
    transform: scale(0.95);
}

/* Адаптивные размеры для элементов управления */
@media (max-width: 480px) {
    .d-pad-button {
        min-width: 40px;
        min-height: 40px;
        font-size: 14px;
    }
    
    .action-button {
        width: 60px;
        height: 60px;
        min-width: 50px;
        min-height: 50px;
        font-size: 1.2rem;
    }
    
    #portrait-controls {
        gap: 15px;
    }
}

@media (max-height: 600px) {
    .d-pad-button {
        min-width: 36px;
        min-height: 36px;
        font-size: 12px;
    }
    
    .action-button {
        width: 55px;
        height: 55px;
        min-width: 45px;
        min-height: 45px;
        font-size: 1.1rem;
    }
}

/* Настройки */
.settings-group {
    margin-bottom: 1.5rem;
    width: 90%;
    max-width: 400px;
}

.settings-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
    gap: 15px;
}

.switch {
    position: relative;
    display: inline-block;
    width: 60px;
    height: 34px;
    flex-shrink: 0;
}

.switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: #ccc;
    transition: .4s;
    border-radius: 34px;
}

.slider:before {
    position: absolute;
    content: "";
    height: 26px;
    width: 26px;
    left: 4px;
    bottom: 4px;
    background-color: white;
    transition: .4s;
    border-radius: 50%;
}

input:checked + .slider {
    background-color: var(--primary-color);
}

input:checked + .slider:before {
    transform: translateX(26px);
}

/* Таблица рекордов */
#highscores-list {
    width: 90%;
    max-width: 400px;
    margin-bottom: 2rem;
}

.highscore-item {
    display: flex;
    justify-content: space-between;
    padding: 12px;
    background-color: rgba(255, 255, 255, 0.1);
    margin-bottom: 8px;
    border-radius: 8px;
    gap: 10px;
}

.highscore-rank {
    font-weight: bold;
    color: var(--primary-color);
    min-width: 30px;
}

/* Безопасные зоны для современных устройств */
@supports(padding: max(0px)) {
    #portrait-layout,
    #landscape-layout {
        padding-left: max(10px, var(--safe-area-inset-left));
        padding-right: max(10px, var(--safe-area-inset-right));
        padding-bottom: max(10px, var(--safe-area-inset-bottom));
    }
    
    #portrait-controls {
        padding-bottom: max(20px, var(--safe-area-inset-bottom));
    }
    
    #landscape-left-controls {
        padding-left: max(20px, var(--safe-area-inset-left));
    }
    
    #landscape-right-controls {
        padding-right: max(20px, var(--safe-area-inset-right));
    }
}