:root {
    --glass-bg: rgba(20, 20, 20, 0.6);
    --glass-border: rgba(255, 255, 255, 0.1);
    --glass-shine: rgba(255, 255, 255, 0.05);

    /* Tint Colors */
    --tint-blue: rgba(0, 153, 255, 0.25);
    --tint-red: rgba(255, 50, 50, 0.25);
    --tint-green: rgba(50, 255, 100, 0.2);
    --tint-yellow: rgba(255, 200, 0, 0.25);
    --tint-purple: rgba(180, 50, 255, 0.25);

    --safe-right: env(safe-area-inset-right, 10px);
    --safe-bottom: env(safe-area-inset-bottom, 20px);
}

body,
html {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
    background: #111;
    font-family: "Press Start 2P", cursive;
    font-size: 12px;
    line-height: 1.5;
    user-select: none;
    -webkit-user-select: none;
    touch-action: none;
}

#canvas {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 0;
    image-rendering: pixelated;
}

/* UI Layer */
#ui-layer {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 10;
    pointer-events: none;
    overflow: hidden;
}

/* --- SIDE TOOLS (Right Side - Default Portrait) --- */
#side-tools {
    position: absolute;
    right: var(--safe-right);
    top: 40%;
    transform: translateY(-50%);
    display: flex;
    flex-direction: column;
    gap: 12px;
    pointer-events: auto;
    align-items: flex-end;
    padding-right: 10px;
}

.tool-btn {
    width: 44px;
    height: 44px;
    border-radius: 12px;
    border: 1px solid var(--glass-border);
    color: white;
    font-size: 20px;
    background: var(--glass-bg);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
    transition: transform 0.1s;
}

.tool-btn:active {
    transform: scale(0.9);
}

.tool-btn.mini {
    width: 36px;
    height: 36px;
    font-size: 16px;
    border-radius: 8px;
}

.zoom-group {
    display: flex;
    flex-direction: column;
    gap: 4px;
    align-items: center;
    margin: 5px 0;
}

/* Tinted Buttons */
.btn-glass-blue {
    background: linear-gradient(135deg, var(--glass-bg), var(--tint-blue));
    border-color: rgba(100, 200, 255, 0.3);
}

.btn-glass-red {
    background: linear-gradient(135deg, var(--glass-bg), var(--tint-red));
    border-color: rgba(255, 100, 100, 0.3);
}

.btn-glass-yellow {
    background: linear-gradient(135deg, var(--glass-bg), var(--tint-yellow));
    border-color: rgba(255, 200, 100, 0.3);
}

.btn-glass-green {
    background: linear-gradient(135deg, var(--glass-bg), var(--tint-green));
    border-color: rgba(100, 255, 150, 0.3);
}

.btn-glass-purple {
    background: linear-gradient(135deg, var(--glass-bg), var(--tint-purple));
    border-color: rgba(200, 100, 255, 0.3);
}

/* --- BOTTOM HUD UPDATED --- */
#hud-bottom {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    padding: 10px calc(var(--safe-right) + 10px) calc(var(--safe-bottom) + 40px) 10px;
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    pointer-events: none;
    box-sizing: border-box;
}

/* Группа кнопок по центру (Инвентарь + TNT) */
.center-tools {
    display: flex;
    align-items: center;
    gap: 12px;
}

/* Стили для кнопки TNT на HUD */
#tnt-trigger {
    pointer-events: auto;
    width: 50px;
    height: 50px;
    background: rgba(127, 29, 29, 0.6);
    border: 1px solid rgba(255, 100, 100, 0.4);
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    font-size: 20px;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.4);
    transition: transform 0.1s, background 0.2s;
}

#tnt-trigger:active {
    transform: scale(0.9);
}

#tnt-trigger.active {
    background: #dc2626;
    box-shadow: 0 0 15px #ef4444;
    border-color: #fff;
    animation: pulse-red 1s infinite;
}

@keyframes pulse-red {
    0% {
        box-shadow: 0 0 0 0 rgba(220, 38, 38, 0.7);
    }

    70% {
        box-shadow: 0 0 0 10px rgba(220, 38, 38, 0);
    }

    100% {
        box-shadow: 0 0 0 0 rgba(220, 38, 38, 0);
    }
}

.hud-zone {
    pointer-events: none;
}

/* Левая зона (джойстик) */
.hud-zone.left {
    min-width: 120px;
    display: flex;
    justify-content: flex-start;
}

/* Правая зона (кнопки действий) */
.hud-zone.right {
    min-width: 130px;
    display: flex;
    justify-content: flex-end;
}

/* Центральная зона */
.hud-zone.center {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-end;
    padding-bottom: 5px;
    gap: 8px;
}

/* D-Pad (Liquid Glass) */
.d-pad {
    width: 120px;
    height: 120px;
    position: relative;
    pointer-events: auto;
}

.d-btn {
    position: absolute;
    width: 38px;
    height: 38px;
    border-radius: 10px;
    background: linear-gradient(135deg, rgba(30, 30, 30, 0.7), var(--tint-blue));
    border: 1px solid rgba(255, 255, 255, 0.15);
    backdrop-filter: blur(5px);
    color: #fff;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 14px;
    box-shadow: 0 4px 0 rgba(0, 0, 0, 0.3);
}

.d-btn:active {
    transform: translateY(2px);
    box-shadow: 0 2px 0 rgba(0, 0, 0, 0.3);
    background: var(--tint-blue);
}

.d-up {
    top: 0;
    left: 41px;
}

.d-down {
    bottom: 0;
    left: 41px;
}

.d-left {
    top: 41px;
    left: 0;
}

.d-right {
    top: 41px;
    right: 0;
}

/* Center Info Area */
#status-bar {
    font-size: 10px;
    color: rgba(255, 255, 255, 0.7);
    background: rgba(0, 0, 0, 0.4);
    padding: 2px 8px;
    border-radius: 10px;
    margin-bottom: 2px;
}

#energy-container {
    width: 120px;
    height: 14px;
    background: rgba(0, 0, 0, 0.6);
    border: 1px solid #444;
    border-radius: 8px;
    position: relative;
    overflow: hidden;
}

#energy-fill {
    height: 100%;
    width: 100%;
    background: linear-gradient(90deg, #f1c40f, #e67e22);
    transition: width 0.3s;
}

#energy-text {
    position: absolute;
    width: 100%;
    text-align: center;
    top: 0;
    font-size: 9px;
    line-height: 14px;
    color: #fff;
    font-weight: bold;
}

/* Current Item Trigger */
#inventory-trigger {
    pointer-events: auto;
    width: 50px;
    height: 50px;
    background: rgba(20, 20, 20, 0.85);
    border: 2px solid rgba(255, 255, 255, 0.4);
    border-radius: 12px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    position: relative;
    box-shadow: 0 0 15px rgba(0, 0, 0, 0.5);
}

#current-item-icon {
    width: 32px;
    height: 32px;
    display: block;
}

#current-item-icon canvas {
    width: 100%;
    height: 100%;
    display: block;
    image-rendering: pixelated;
    border-radius: 6px;
}

#current-item-name {
    font-size: 9px;
    color: #aaa;
    margin-top: -2px;
    max-width: 100%;
    overflow: hidden;
    white-space: nowrap;
}

/* Action Buttons */
.actions {
    display: flex;
    gap: 12px;
    pointer-events: auto;
    padding-bottom: 10px;
}

.action-btn {
    width: 55px;
    height: 55px;
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 24px;
    color: #fff;
    border: 1px solid rgba(255, 255, 255, 0.3);
    backdrop-filter: blur(5px);
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.4);
}

.action-btn:active {
    transform: scale(0.95);
    opacity: 0.8;
}

#btnA {
    width: 65px;
    height: 65px;
    font-size: 28px;
}

/* --- OVERLAYS (Bottom Sheet Style) --- */
.overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(2px);
    z-index: 100;
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    transition: opacity 0.2s, visibility 0.2s;
    visibility: visible;
    pointer-events: auto;
}

.overlay.hidden {
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
}

.sheet-card {
    background: rgba(20, 20, 20, 0.95);
    backdrop-filter: blur(15px);
    -webkit-backdrop-filter: blur(15px);
    width: 100%;
    max-height: 85%;
    height: 85%;
    border-radius: 20px 20px 0 0;
    border-top: 1px solid rgba(255, 255, 255, 0.2);
    display: flex;
    flex-direction: column;
    padding-bottom: var(--safe-bottom);
    box-shadow: 0 -5px 20px rgba(0, 0, 0, 0.5);
    transform: translateY(0);
    transition: transform 0.3s cubic-bezier(0.2, 0.8, 0.2, 1);
}

.overlay.hidden .sheet-card {
    transform: translateY(100%);
}

.overlay-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 15px 20px;
    background: rgba(255, 255, 255, 0.03);
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    flex-shrink: 0;
}

.overlay-header h2 {
    margin: 0;
    color: #fff;
    font-size: 18px;
    letter-spacing: 1px;
}

.close-overlay {
    background: none;
    border: none;
    color: #aaa;
    font-size: 24px;
    cursor: pointer;
    padding: 5px;
}

.overlay-content {
    flex: 1;
    padding: 20px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 15px;
}

/* Menu Tabs */
.menu-tabs {
    display: flex;
    gap: 10px;
    margin-bottom: 10px;
    margin-top: 50px;
}

.m-tab {
    flex: 1;
    background: #222;
    border: 1px solid #444;
    color: #888;
    padding: 10px;
    border-radius: 8px;
    cursor: pointer;
    font-family: inherit;
}

.m-tab.active {
    background: #444;
    color: #fff;
    border-color: #666;
}

.menu-section {
    display: none;
    flex-direction: column;
    height: 100%;
}

.menu-section.active {
    display: flex;
}

/* Global Chat in Menu */
#global-chat-log {
    flex: 1;
    background: rgba(0, 0, 0, 0.3);
    border: 1px solid #333;
    border-radius: 8px;
    padding: 10px;
    overflow-y: auto;
    font-size: 14px;
    color: #ccc;
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin-bottom: 10px;
}

.chat-log-msg.system {
    color: #f1c40f;
    font-style: italic;
}

.chat-log-sender {
    font-weight: bold;
    color: #5dade2;
}

.menu-chat-input {
    display: flex;
    gap: 10px;
    height: 40px;
}

#menu-input {
    flex: 1;
    background: #222;
    border: 1px solid #444;
    color: white;
    padding: 0 10px;
    border-radius: 6px;
    font-family: inherit;
}

#menu-send {
    width: 50px;
    background: #2ecc71;
    border: none;
    border-radius: 6px;
    color: black;
    font-weight: bold;
}

/* Inventory Styles */
#palette-tabs {
    display: flex;
    gap: 5px;
}

.tab-btn {
    flex: 1;
    padding: 10px;
    background: #222;
    border: 1px solid #444;
    color: #888;
    border-radius: 6px;
    font-family: inherit;
}

.tab-btn.active {
    background: var(--tint-blue);
    color: #fff;
    border-color: #fff;
}

#palette-items {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    justify-content: flex-start;
}

.pal-item {
    width: 48px;
    height: 48px;
    border-radius: 8px;
    background: #333;
    position: relative;
}

.pal-item canvas {
    width: 100%;
    height: 100%;
    display: block;
    image-rendering: pixelated;
    border-radius: 6px;
}

.pal-item.active {
    border: 2px solid #fff;
    box-shadow: 0 0 10px white;
}

/* Chat Input Overlay */
#chat-input-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    z-index: 50;
    pointer-events: auto;
    display: flex;
    align-items: center;
    justify-content: center;
}

.chat-input-box {
    width: 90%;
    max-width: 400px;
    background: #222;
    padding: 10px;
    border-radius: 12px;
    border: 1px solid #555;
    display: flex;
    gap: 8px;
}

#chat-input {
    flex: 1;
    background: #111;
    color: white;
    border: 1px solid #444;
    padding: 8px;
    border-radius: 6px;
    font-family: inherit;
}

#chat-send {
    background: var(--tint-blue);
    color: white;
    border: none;
    padding: 0 15px;
    border-radius: 6px;
}

#chat-close {
    background: #333;
    color: #aaa;
    border: none;
    padding: 0 10px;
    border-radius: 6px;
}

/* Admin Menu */
#admin-menu {
    position: fixed;
    display: none;
    flex-direction: column;
    background: #222;
    border: 1px solid #555;
    border-radius: 8px;
    z-index: 1000;
    width: 160px;
}

#am-header {
    background: #333;
    padding: 5px 10px;
    color: #ccc;
    display: flex;
    justify-content: space-between;
}

.admin-btn {
    background: transparent;
    padding: 10px;
    color: white;
    border: none;
    border-bottom: 1px solid #333;
    text-align: left;
    font-family: inherit;
}

.admin-btn:hover {
    background: #444;
}

/* LOADING SCREEN */
#loading-screen {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: #111;
    z-index: 9999;
    display: flex;
    justify-content: center;
    align-items: center;
    transition: opacity 0.5s ease-out;
}

.loader-content {
    text-align: center;
    color: white;
}

.loader-title {
    font-family: "Press Start 2P", cursive;
    font-size: 18px;
    line-height: 1.5;
    margin-bottom: 20px;
    letter-spacing: 2px;
    color: #5acf2c;
    text-shadow: 0 0 10px rgba(90, 207, 44, 0.5);
    text-align: center;
}

.loader-spinner {
    width: 50px;
    height: 50px;
    border: 5px solid #333;
    border-top: 5px solid #5acf2c;
    border-radius: 50%;
    margin: 0 auto 20px auto;
    animation: spin 1s linear infinite;
}

#loading-text {
    font-family: inherit;
    color: #888;
    font-size: 10px;
    margin-top: 15px;
    text-transform: uppercase;
}

@keyframes spin {
    0% {
        transform: rotate(0deg);
    }

    100% {
        transform: rotate(360deg);
    }
}

#loading-screen.fade-out {
    opacity: 0;
    pointer-events: none;
}

/* === TUTORIAL SYSTEM (FINAL) === */
#tutorial-mask {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    /* Убрали размытие, сделали фон чуть темнее для контраста */
    background: rgba(0, 0, 0, 0.7);
    z-index: 2000;
    display: none;
    transition: opacity 0.3s;
    pointer-events: none;
    /* Клики проходят сквозь маску */
}

/* Класс для мощной подсветки активного элемента */
.tutorial-highlight {
    position: relative;
    z-index: 2005 !important;
    border-radius: 12px;
    /* Ускорили анимацию до 1s для большей динамики */
    animation: tut-pulse-super 1.0s infinite ease-in-out;
}

/* Максимально заметная пульсация */
@keyframes tut-pulse-super {
    0% {
        box-shadow: 0 0 0 0 rgba(90, 207, 44, 0.7);
        border: 1px solid rgba(90, 207, 44, 1);
        transform: scale(1);
    }

    50% {
        /* Двойная тень: зеленое облако + белая вспышка */
        box-shadow:
            0 0 30px 5px rgba(90, 207, 44, 0.9),
            0 0 50px 20px rgba(255, 255, 255, 0.2);
        border: 2px solid #ffffff;
        transform: scale(1.15);
        /* Сильное увеличение */
        background-color: rgba(90, 207, 44, 0.2);
        /* Подсветка фона самой кнопки */
    }

    100% {
        box-shadow: 0 0 0 0 rgba(90, 207, 44, 0);
        border: 1px solid rgba(90, 207, 44, 1);
        transform: scale(1);
    }
}

#tutorial-box {
    position: fixed;
    /* ПОДНЯЛИ ВЫШЕ (было 15%) */
    top: 12%;
    left: 50%;
    transform: translate(-50%, 0);

    width: 85%;
    max-width: 350px;
    background: rgba(26, 26, 26, 0.98);
    /* Почти непрозрачный фон */
    border: 2px solid #5acf2c;
    border-radius: 12px;
    padding: 20px;
    z-index: 2010;
    display: none;
    text-align: center;
    box-shadow: 0 15px 40px rgba(0, 0, 0, 0.8);
    /* Глубокая тень */
    pointer-events: auto;
    /* Окно ловит клики */
}

#tut-text {
    color: #fff;
    font-size: 13px;
    margin-bottom: 20px;
    line-height: 1.6;
    text-shadow: 0 1px 2px black;
}

#tut-btn {
    background: #5acf2c;
    border: none;
    padding: 12px 30px;
    color: #000;
    font-weight: bold;
    font-family: inherit;
    border-radius: 8px;
    cursor: pointer;
    font-size: 12px;
    text-transform: uppercase;
    box-shadow: 0 4px 0 #3e8e41;
    transition: transform 0.1s;
}

#tut-btn:active {
    transform: translateY(2px);
    box-shadow: 0 2px 0 #3e8e41;
}

#tut-close {
    position: absolute;
    top: 8px;
    right: 8px;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 50%;
    width: 24px;
    height: 24px;
    border: none;
    color: #aaa;
    font-size: 14px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
}

#tut-close:active {
    background: rgba(255, 255, 255, 0.3);
    color: #fff;
}

/* --- DEVICE SPECIFIC LOGIC via JS Classes --- */
body.is-desktop .d-pad,
body.is-desktop .actions {
    display: none !important;
}

body.is-desktop #hud-bottom {
    justify-content: center;
}

body.is-mobile .d-pad {
    transform: scale(0.9);
    transform-origin: bottom left;
}

body.is-mobile .actions {
    transform: scale(0.9);
    transform-origin: bottom right;
    margin-right: 10px;
    margin-bottom: 5px;
}

/* === PROFILE STYLES === */
.profile-card {
    display: flex;
    gap: 15px;
    background: rgba(0, 0, 0, 0.3);
    padding: 15px;
    border-radius: 12px;
    margin-bottom: 15px;
    align-items: center;
}

.profile-avatar-container {
    position: relative;
    width: 64px;
    height: 64px;
    background: #222;
    border: 2px solid #444;
    border-radius: 8px;
    image-rendering: pixelated;
}

#profile-skin-canvas {
    width: 100%;
    height: 100%;
}

.profile-role-badge {
    position: absolute;
    bottom: -8px;
    left: 50%;
    transform: translateX(-50%);
    background: #f1c40f;
    color: #000;
    font-size: 8px;
    padding: 2px 4px;
    border-radius: 4px;
    font-weight: bold;
    white-space: nowrap;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
}

.profile-info {
    flex: 1;
}

#profile-name {
    margin: 0 0 8px 0;
    font-size: 14px;
    color: #fff;
}

.level-row {
    display: flex;
    justify-content: space-between;
    font-size: 10px;
    margin-bottom: 4px;
    color: #ccc;
}

.lvl-badge {
    color: #ffd700;
}

.xp-bar-bg {
    width: 100%;
    height: 8px;
    background: #333;
    border-radius: 4px;
    overflow: hidden;
}

.xp-bar-fill {
    height: 100%;
    background: linear-gradient(90deg, #3498db, #2980b9);
    transition: width 0.3s ease;
}

.stats-grid {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    gap: 10px;
    margin-bottom: 20px;
}

.stat-item {
    background: rgba(255, 255, 255, 0.05);
    padding: 10px;
    border-radius: 8px;
    text-align: center;
}

.stat-val {
    display: block;
    font-size: 16px;
    color: #fff;
    margin-bottom: 4px;
}

.stat-lbl {
    font-size: 8px;
    color: #888;
    text-transform: uppercase;
}

.quests-list h4 {
    font-size: 12px;
    color: #aaa;
    margin-bottom: 10px;
}

.quest-item {
    background: rgba(0, 0, 0, 0.2);
    padding: 10px;
    border-radius: 8px;
    margin-bottom: 8px;
}

.q-header {
    display: flex;
    justify-content: space-between;
    font-size: 10px;
    margin-bottom: 5px;
    color: #ddd;
}

.q-desc {
    font-size: 9px;
    color: #888;
    margin-bottom: 5px;
}

.q-progress-bg {
    width: 100%;
    height: 4px;
    background: #222;
    border-radius: 2px;
}

.q-progress-fill {
    height: 100%;
    background: #27ae60;
    width: 0%;
}

/* --- АДАПТАЦИЯ ПОД ГОРИЗОНТАЛЬ (LANDSCAPE MOBILE) --- */
@media screen and (orientation: landscape) and (max-height: 600px) {

    /* 1. ПРАВАЯ СТОРОНА (Меню, Чат, Скин) */
    #side-tools {
        /* Убираем transform, чтобы не ломать position: fixed у дочерних элементов */
        transform: none;
        /* Центрируем через calc (высота контента примерно 160px) */
        top: calc(50% - 80px);
        right: calc(var(--safe-right) + 10px);

        display: flex;
        flex-direction: column;
        gap: 15px;
        align-items: center;
    }

    /* 2. ЛЕВАЯ СТОРОНА (Зум) - вырываем из потока */
    #side-tools .zoom-group {
        position: fixed;
        /* Теперь это сработает относительно окна */
        left: max(20px, env(safe-area-inset-left));
        top: 50%;
        transform: translateY(-50%);
        /* Тут трансформ безопасен */

        display: flex;
        flex-direction: column;
        gap: 15px;
        z-index: 100;

        /* Сбрасываем отступы, если были */
        margin: 0;
    }

    /* Кнопки зума побольше */
    .zoom-group .tool-btn {
        width: 44px;
        height: 44px;
        font-size: 20px;
    }

    /* 3. НИЖНИЙ HUD */
    #hud-bottom {
        padding-right: calc(var(--safe-right) + 70px);
        padding-left: calc(max(20px, env(safe-area-inset-left)) + 60px);
    }
}