* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { width: 100%; height: 100%; overflow: hidden; background: #000; font-family: 'Segoe UI', Arial, sans-serif; color: #eee; user-select: none; }
canvas { display: block; width: 100%; height: 100%; }

/* ===== ЭКРАНЫ МЕНЮ ===== */
.menu-screen {
    position: fixed; inset: 0; z-index: 200;
    background: radial-gradient(ellipse at center, #0d0d1a 0%, #050510 100%);
    display: flex; align-items: center; justify-content: center;
}
.menu-screen.hidden { display: none; }

.menu-panel {
    text-align: center; padding: 40px 50px;
    background: rgba(20, 20, 40, 0.85); border: 1px solid #3a3a6a;
    border-radius: 12px; min-width: 340px;
    box-shadow: 0 0 60px rgba(80, 60, 180, 0.15);
}

.game-title {
    font-size: 52px; font-weight: 900; letter-spacing: 6px;
    background: linear-gradient(135deg, #6a4c93, #c77dff, #6a4c93);
    -webkit-background-clip: text; -webkit-text-fill-color: transparent;
    margin-bottom: 4px; text-transform: uppercase;
}
.subtitle { font-size: 14px; color: #8888aa; margin-bottom: 30px; letter-spacing: 2px; }
.version { font-size: 11px; color: #555; margin-top: 24px; }
.menu-error { font-size: 13px; color: #f55; margin-top: 14px; padding: 8px; background: rgba(255,0,0,.1); border-radius: 4px; }
.menu-error.hidden { display: none; }

.menu-buttons { display: flex; flex-direction: column; gap: 10px; }
.menu-btn {
    background: linear-gradient(135deg, #4a2c73, #6a3c9a); border: none;
    color: #fff; font-size: 16px; font-weight: 600; padding: 12px 24px;
    border-radius: 6px; cursor: pointer; transition: all .2s;
    letter-spacing: 1px;
}
.menu-btn:hover { background: linear-gradient(135deg, #5a3c83, #8a4cba); transform: scale(1.02); }
.menu-btn:active { transform: scale(0.98); }
.menu-btn.secondary {
    background: transparent; border: 1px solid #5a4a7a; color: #aaa;
}
.menu-btn.secondary:hover { background: rgba(255,255,255,.05); border-color: #8a7aaa; color: #ddd; }

/* Настройки */
.settings-panel { min-width: 420px; max-height: 80vh; overflow-y: auto; }
.settings-panel h2 { font-size: 24px; margin-bottom: 24px; color: #c8a8e8; }
.settings-section { margin-top: 16px; padding-top: 12px; border-top: 1px solid rgba(255,255,255,.06); }
.settings-section h3 { font-size: 14px; color: #a8a8d8; margin-bottom: 8px; text-transform: uppercase; letter-spacing: 1px; }
.key-row {
    display: flex; align-items: center; justify-content: space-between;
    padding: 5px 8px; margin: 2px 0; background: rgba(255,255,255,.03); border-radius: 4px;
    font-size: 12px; cursor: pointer; transition: background .15s;
}
.key-row:hover { background: rgba(255,255,255,.08); }
.key-row .key-action { color: #bbb; }
.key-row .key-code {
    background: #2a2a4a; border: 1px solid #555; border-radius: 3px;
    padding: 2px 8px; font-size: 11px; color: #ff0; font-family: monospace;
    min-width: 60px; text-align: center;
}
.key-row.editing { background: rgba(120,90,200,.2); }
.key-row.editing .key-code { border-color: #8a5cba; background: #3a2a5a; color: #fff; }

/* Режим перетаскивания кнопок */
.draggable { cursor: grab !important; transition: box-shadow .2s; }
.draggable:active { cursor: grabbing !important; box-shadow: 0 0 20px rgba(120,90,200,.4); }
.setting-row {
    display: flex; align-items: center; gap: 14px; margin-bottom: 16px;
    padding: 10px 14px; background: rgba(255,255,255,.04); border-radius: 6px;
}
.setting-row label { flex: 1; text-align: left; font-size: 14px; color: #ccc; }
.setting-row input[type="range"] {
    width: 140px; height: 4px; -webkit-appearance: none; appearance: none;
    background: #3a3a5a; border-radius: 2px; outline: none;
}
.setting-row input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none; width: 16px; height: 16px; border-radius: 50%;
    background: #8a5cba; cursor: pointer;
}
.setting-row span { min-width: 40px; text-align: right; font-size: 14px; color: #aaa; }
.setting-row label:has(input[type="checkbox"]) { flex: none; }
.toggle-switch { position: relative; display: inline-block; width: 44px; height: 22px; }
.toggle-switch input { opacity: 0; width: 0; height: 0; }
.toggle-slider { position: absolute; cursor: pointer; inset: 0; background: #3a3a5a; border-radius: 22px; transition: .2s; }
.toggle-slider::before { content: ''; position: absolute; height: 16px; width: 16px; left: 3px; bottom: 3px; background: #888; border-radius: 50%; transition: .2s; }
.toggle-switch input:checked + .toggle-slider { background: #6a4c93; }
.toggle-switch input:checked + .toggle-slider::before { transform: translateX(22px); background: #fff; }

/* ===== HUD ===== */
#hud { position: fixed; inset: 0; pointer-events: none; z-index: 10; }
#hud.hidden { display: none; }
#crosshair {
    position: absolute; top: 50%; left: 50%; transform: translate(-50%,-50%);
    font-size: 28px; color: #fff; text-shadow: 0 0 6px rgba(0,0,0,.8); font-weight: bold;
    opacity: .85;
}
#health-bar {
    position: absolute; bottom: 80px; left: 50%; transform: translateX(-50%);
    width: 200px; height: 18px; background: #444; border: 2px solid #666; border-radius: 4px;
    overflow: hidden;
}
#health-text {
    position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
    font-size: 12px; font-weight: bold; color: #fff; text-shadow: 0 0 4px #000; z-index: 1;
}
@media (hover: none) and (pointer: coarse) {
    #hotbar { pointer-events: auto; }
    #hotbar .slot { cursor: default; }
}
#hotbar {
    position: absolute; bottom: 20px; left: 50%; transform: translateX(-50%);
    display: flex; gap: 2px; background: rgba(0,0,0,.5); padding: 3px; border-radius: 4px;
}
#hotbar .slot {
    width: 44px; height: 44px; background: rgba(255,255,255,.08); border: 1px solid #444;
    border-radius: 3px; display: flex; flex-direction: column; align-items: center; justify-content: center;
    font-size: 10px; position: relative; transition: border .15s;
}
#hotbar .slot.active { border-color: #ff0; background: rgba(255,255,0,.12); }
#hotbar .slot img { width: 22px; height: 22px; image-rendering: pixelated; }
#hotbar .slot .count { position: absolute; bottom: 1px; right: 2px; font-size: 9px; color: #fff; text-shadow: 0 0 3px #000; }

/* ===== UI Screen overlay ===== */
.ui-screen {
    position: fixed; inset: 0; z-index: 100;
    background: rgba(0,0,0,.7); display: flex; align-items: center; justify-content: center;
}
.ui-screen.hidden { display: none; }
.ui-panel {
    background: #1a1a2e; border: 2px solid #4a4a7a; border-radius: 8px; padding: 20px 30px;
    min-width: 400px; position: relative; pointer-events: auto; max-height: 80vh; overflow-y: auto;
}
.ui-panel h2 { text-align: center; margin-bottom: 14px; color: #b8b; }
.btn-close {
    position: absolute; top: 8px; right: 12px; background: none; border: none;
    color: #aaa; font-size: 20px; cursor: pointer; pointer-events: auto;
}
.btn-close:hover { color: #fff; }

/* Equipment */
.inv-layout { display: flex; gap: 16px; align-items: flex-start; }
.equip-panel {
    display: flex; flex-direction: column; gap: 6px; padding: 8px;
    background: rgba(255,255,255,.04); border-radius: 6px; border: 1px solid #3a3a5a;
}
.equip-slot { width: 64px; height: 64px; border-color: #5a4a8a; }
.equip-slot .equip-label {
    font-size: 9px; color: #8888bb; text-align: center; line-height: 1.2;
    pointer-events: none;
}
@media (max-width: 768px) {
    .inv-layout { flex-direction: column; align-items: center; }
    .equip-panel { flex-direction: row; }
}

/* Slots & Grids */
.slot {
    width: 56px; height: 56px; background: rgba(255,255,255,.08); border: 2px solid #444;
    border-radius: 4px; display: flex; flex-direction: column; align-items: center;
    justify-content: center; font-size: 10px; cursor: pointer; position: relative;
    transition: background .15s;
}
.slot:hover { background: rgba(255,255,255,.15); }
.slot .count { position: absolute; bottom: 2px; right: 4px; font-size: 11px; }
.slot img { width: 32px; height: 32px; image-rendering: pixelated; }
.result-slot { border-color: #ff0; background: rgba(255,255,0,.1); }
.grid-container {
    display: grid; grid-template-columns: repeat(9,56px); gap: 3px; justify-content: center;
}
.grid-3x3 { grid-template-columns: repeat(3,56px); }

/* Furnace */
.furnace-layout { display: flex; gap: 20px; justify-content: center; align-items: center; margin: 10px 0; }
.furnace-layout .slot { width: 64px; height: 64px; }
#furnace-progress { width: 100%; height: 12px; background: #333; border-radius: 6px; margin-top: 8px; overflow: hidden; }
#furnace-fill { height: 100%; width: 0%; background: linear-gradient(90deg,#f80,#ff0); transition: width .2s; }

/* Recipes */
#recipes-list { margin-top: 12px; display: flex; flex-wrap: wrap; gap: 6px; justify-content: center; }
.recipe-item {
    background: #222; border: 1px solid #555; border-radius: 4px; padding: 6px 10px;
    cursor: pointer; font-size: 12px; transition: background .15s;
}
.recipe-item:hover { background: #334; }
.recipe-item.cant-afford { opacity: .4; }

/* ===== СЕНСОРНОЕ УПРАВЛЕНИЕ ===== */
#touch-controls { position: fixed; inset: 0; z-index: 30; pointer-events: none; }
#touch-controls.hidden { display: none; }

/* Зона осмотра (весь экран, под кнопками) */
#touch-look-zone {
    position: absolute; inset: 0; z-index: 30;
    pointer-events: auto; -webkit-tap-highlight-color: transparent;
}

/* Верхняя панель */
#touch-top-bar {
    position: absolute; top: 0; left: 0; right: 0; height: 48px; z-index: 35;
    display: flex; justify-content: space-between; align-items: center;
    padding: 6px 10px; pointer-events: none;
}
#touch-top-right { display: flex; gap: 6px; pointer-events: none; }
.touch-icon-btn {
    width: 38px; height: 38px; border-radius: 8px; border: none;
    background: rgba(0,0,0,.45); color: #fff; font-size: 16px;
    display: flex; align-items: center; justify-content: center;
    pointer-events: auto; cursor: default; -webkit-tap-highlight-color: transparent;
    backdrop-filter: blur(4px);
}
.touch-icon-btn:active { background: rgba(255,255,255,.2); transform: scale(.92); }

/* Джойстик — всегда виден */
#touch-stick-zone {
    position: absolute; bottom: 30px; left: 15px; width: 140px; height: 140px;
    pointer-events: auto; z-index: 34;
}
#touch-stick {
    position: absolute; left: 50%; top: 50%; width: 120px; height: 120px;
    margin-left: -60px; margin-top: -60px;
    border-radius: 50%; background: rgba(255,255,255,.08); border: 2px solid rgba(255,255,255,.2);
    pointer-events: none; backdrop-filter: blur(2px);
}
.touch-thumb {
    position: absolute; top: 50%; left: 50%; width: 44px; height: 44px;
    margin: -22px 0 0 -22px; border-radius: 50%;
    background: radial-gradient(circle at 35% 35%, rgba(255,255,255,.45), rgba(255,255,255,.12));
    border: 1px solid rgba(255,255,255,.3); pointer-events: none;
}

/* Правые кнопки действий */
#touch-right-actions {
    position: absolute; right: 10px; top: 50%; transform: translateY(-50%);
    display: flex; flex-direction: column; gap: 8px; align-items: center;
    pointer-events: none; z-index: 33;
}
.touch-action-btn {
    width: 48px; height: 48px; border-radius: 50%; border: 2px solid rgba(255,255,255,.2);
    background: rgba(0,0,0,.4); color: #fff; font-size: 18px; display: flex;
    align-items: center; justify-content: center; pointer-events: auto; cursor: default;
    -webkit-tap-highlight-color: transparent; user-select: none;
    backdrop-filter: blur(4px);
}
.touch-action-btn:active { background: rgba(255,255,255,.2); transform: scale(.9); }
.jump-btn { font-size: 20px; background: rgba(255,255,200,.08); border-color: rgba(255,255,200,.3); }

/* Контекстное меню */
#touch-context {
    position: fixed; z-index: 40; background: rgba(10,10,24,.95);
    border: 1px solid rgba(100,80,160,.4); border-radius: 10px; padding: 4px;
    box-shadow: 0 4px 20px rgba(0,0,0,.5); min-width: 140px;
    backdrop-filter: blur(8px);
}
#touch-context.hidden { display: none; }
.ctx-item {
    padding: 8px 14px; font-size: 13px; color: #ddd; border-radius: 6px;
    cursor: pointer; pointer-events: auto; -webkit-tap-highlight-color: transparent;
}
.ctx-item:active { background: rgba(255,255,255,.1); }
#ctx-drop { color: #f66; }
#ctx-cancel { color: #888; border-top: 1px solid rgba(255,255,255,.06); margin-top: 2px; }

/* Адаптация под портрет */
@media (orientation: portrait) {
    #touch-stick-zone { bottom: 40px; left: 10px; width: 130px; height: 130px; }
    #touch-stick { width: 110px; height: 110px; margin-left: -55px; margin-top: -55px; }
    .touch-thumb { width: 38px; height: 38px; margin: -19px 0 0 -19px; }
    .touch-action-btn { width: 42px; height: 42px; font-size: 16px; }
    #touch-right-actions { gap: 6px; }
.touch-icon-btn { width: 34px; height: 34px; font-size: 14px; }
}

/* ===== АДАПТАЦИЯ ПОД МОБИЛЬНЫЕ ===== */
@media (orientation: portrait) {
    #touch-stick-zone { bottom: 40px; left: 12px; width: 130px; height: 130px; }
    #touch-stick { width: 110px; height: 110px; }
    .touch-thumb { width: 40px; height: 40px; margin: -20px 0 0 -20px; }
    #touch-actions { bottom: 50px; gap: 6px; }
    .touch-btn { width: 46px; height: 46px; font-size: 18px; }
    .fs-btn { width: 36px; height: 36px; font-size: 14px; }
}
@media (orientation: landscape) {
    #touch-stick-zone { bottom: 20px; left: 15px; width: 150px; height: 150px; }
    #touch-stick { width: 130px; height: 130px; }
    .touch-thumb { width: 48px; height: 48px; }
    #touch-actions { bottom: 30px; gap: 8px; }
    .touch-btn { width: 52px; height: 52px; font-size: 20px; }
    .fs-btn { width: 40px; height: 40px; font-size: 16px; }
}

/* ===== АДАПТАЦИЯ UI ПОД ТЕЛЕФОН ===== */
@media (max-width: 768px) {
    .menu-panel { min-width: auto; width: 92%; padding: 24px 16px; }
    .game-title { font-size: 32px; letter-spacing: 3px; }
    .settings-panel { min-width: auto; width: 92%; }
    .setting-row { flex-wrap: wrap; gap: 8px; }
    .setting-row input[type="range"] { width: 100px; }
    .ui-panel { min-width: auto; width: 94%; padding: 14px 12px; }
    .grid-container { grid-template-columns: repeat(6, 48px); }
    .grid-container .slot { width: 48px; height: 48px; }
    .grid-container .slot img { width: 26px; height: 26px; }
    #hotbar { bottom: 10px; gap: 1px; padding: 2px; }
    #hotbar .slot { width: 34px; height: 34px; }
    #hotbar .slot img { width: 18px; height: 18px; }
    #health-bar { width: 150px; height: 14px; bottom: 70px; }
    #crosshair { font-size: 22px; }
}
@media (max-width: 768px) and (orientation: portrait) {
    .inv-layout { flex-direction: column; align-items: center; }
    .equip-panel { flex-direction: row; gap: 4px; }
    .equip-slot { width: 52px; height: 52px; }
    .grid-container { grid-template-columns: repeat(4, 48px); }
    #hotbar { bottom: 80px; }
}
@media (max-width: 768px) and (orientation: landscape) {
    .inv-layout { flex-direction: row; }
    .equip-panel { flex-direction: column; }
}
@media (max-width: 480px) {
    .game-title { font-size: 26px; }
    .menu-btn { font-size: 14px; padding: 10px 18px; }
    .setting-row input[type="range"] { width: 80px; }
    #hotbar .slot { width: 34px; height: 34px; }
    #hotbar .slot img { width: 18px; height: 18px; }
    .grid-container { grid-template-columns: repeat(4, 40px); gap: 2px; }
    .grid-container .slot { width: 40px; height: 40px; }
    .grid-container .slot img { width: 22px; height: 22px; }
    .ui-panel h2 { font-size: 16px; }
}

/* ===== ТЕЛЕФОН ===== */
#screen-phone { z-index: 50; }
.phone-panel {
    position: fixed; bottom: 20px; right: 20px; width: 300px;
    background: linear-gradient(160deg, rgba(20,22,40,.94), rgba(12,14,28,.96));
    border: 1px solid rgba(100,80,160,.35); border-radius: 16px;
    box-shadow: 0 0 40px rgba(80,60,160,.25), inset 0 1px 0 rgba(255,255,255,.04);
    backdrop-filter: blur(10px); overflow: hidden;
}
.phone-tabs {
    display: flex; border-bottom: 1px solid rgba(255,255,255,.06);
    padding: 4px 4px 0;
}
.phone-tab {
    flex: 1; background: none; border: none; color: rgba(255,255,255,.35);
    font-size: 18px; padding: 8px 0 6px; cursor: pointer; pointer-events: auto;
    border-bottom: 2px solid transparent; transition: color .15s, border-color .15s;
}
.phone-tab:hover { color: rgba(255,255,255,.6); }
.phone-tab.active { color: #fff; border-bottom-color: #8a5cba; }
.phone-content { padding: 8px 10px; }
.phone-content.hidden { display: none; }
/* Чат внутри телефона */
#phone-chat #chat-messages {
    max-height: 200px; overflow-y: auto; padding: 2px 0;
    font-size: 13px; line-height: 1.4;
    scrollbar-width: thin; scrollbar-color: rgba(255,255,255,.1) transparent;
}
#phone-chat #chat-messages::-webkit-scrollbar { width: 3px; }
#phone-chat #chat-messages::-webkit-scrollbar-thumb { background: rgba(255,255,255,.12); border-radius: 2px; }
#phone-chat #chat-messages .msg {
    padding: 3px 6px; margin: 1px 0; border-radius: 4px;
    background: rgba(255,255,255,.03); word-break: break-word;
}
#chat-input {
    width: 100%; background: rgba(255,255,255,.06);
    border: 1px solid rgba(255,255,255,.08); border-radius: 10px;
    color: #eee; padding: 8px 12px; outline: none; font-size: 13px;
    box-sizing: border-box; transition: border-color .15s; margin-top: 6px;
}
#chat-input:focus { border-color: rgba(120,90,200,.5); }
#chat-input::placeholder { color: rgba(255,255,255,.25); }
/* Компас */
.compass-wrap { display: flex; align-items: center; gap: 14px; padding: 6px 0; }
.compass-ring {
    width: 100px; height: 100px; border-radius: 50%;
    border: 2px solid rgba(255,255,255,.12); position: relative; flex-shrink: 0;
    background: radial-gradient(circle, rgba(255,255,255,.04), transparent);
}
.compass-needle {
    position: absolute; top: 50%; left: 50%; font-size: 28px; line-height: 1;
    color: #f80; text-shadow: 0 0 8px rgba(255,136,0,.5);
    transform: translate(-50%, -80%) rotate(0deg); transition: transform .1s;
}
.compass-info { display: flex; flex-direction: column; gap: 4px; flex: 1; }
.coord-row { display: flex; justify-content: space-between; padding: 3px 8px; background: rgba(255,255,255,.04); border-radius: 4px; }
.coord-label { font-size: 11px; color: rgba(255,255,255,.35); font-weight: 600; }
.coord-val { font-size: 13px; color: #eee; font-family: monospace; }
/* Рация */
.radio-wrap { text-align: center; padding: 10px 0; }
.radio-icon { font-size: 40px; margin-bottom: 6px; }
.radio-status { font-size: 13px; color: rgba(255,255,255,.4); margin-bottom: 8px; }
.radio-btn {
    background: linear-gradient(135deg, #4a2c73, #6a3c9a); border: none;
    color: #fff; font-size: 14px; font-weight: 600; padding: 8px 24px;
    border-radius: 8px; cursor: pointer; pointer-events: auto;
    transition: background .2s;
}
.radio-btn:hover { background: linear-gradient(135deg, #5a3c83, #8a4cba); }
.radio-btn.active { background: linear-gradient(135deg, #c33, #a22); }
.radio-hint { font-size: 10px; color: rgba(255,255,255,.2); margin-top: 8px; }
/* FAB */
#phone-fab {
    position: fixed; bottom: 24px; right: 24px; z-index: 49;
    width: 48px; height: 48px; border-radius: 50%;
    background: linear-gradient(135deg, #5a3c8a, #7a4cba);
    border: none; color: #fff; font-size: 20px; cursor: pointer; pointer-events: auto;
    box-shadow: 0 4px 16px rgba(80,40,160,.4);
    transition: transform .15s, box-shadow .15s;
    display: flex; align-items: center; justify-content: center;
}
#phone-fab:hover { transform: scale(1.08); box-shadow: 0 6px 20px rgba(80,40,160,.5); }
#phone-fab:active { transform: scale(.95); }
#phone-fab.hidden { display: none; }
.phone-badge {
    position: absolute; top: -4px; right: -4px; min-width: 18px; height: 18px;
    border-radius: 9px; background: #f44; color: #fff; font-size: 10px; font-weight: bold;
    display: flex; align-items: center; justify-content: center; padding: 0 4px;
    box-shadow: 0 0 6px rgba(255,60,60,.5);
}
