* {
    box-sizing: border-box;
    user-select: none;
    -webkit-user-select: none;
    margin: 0;
    padding: 0;
    -webkit-tap-highlight-color: transparent;
}

body,
html {
    width: 100%;
    height: 100%;
    height: 100dvh;
    min-height: -webkit-fill-available;
    overflow: hidden;
    background: #090a0f;
    font-family: 'Courier New', Courier, monospace;
    color: #eee;
}

#game-container {
    position: relative;
    width: 100vw;
    height: 100%;
    height: 100dvh;
    min-height: -webkit-fill-available;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-between;
    background: radial-gradient(circle at center, #181a24 0%, #060608 100%);
    overflow: hidden;
    padding-left: env(safe-area-inset-left, 0px);
    padding-right: env(safe-area-inset-right, 0px);
    padding-top: env(safe-area-inset-top, 0px);
    padding-bottom: env(safe-area-inset-bottom, 0px);
}

/* ======================================================= */
/* ГЛАВНОЕ МЕНЮ                                            */
/* ======================================================= */

#main-menu-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100%;
    height: 100dvh;
    min-height: -webkit-fill-available;
    background: radial-gradient(circle at 50% 30%, #151a2d 0%, #0b0e17 55%, #05060a 100%);
    z-index: 200;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    align-items: center;
    padding: max(12px, env(safe-area-inset-top, 0px)) max(14px, env(safe-area-inset-right, 0px)) max(14px, env(safe-area-inset-bottom, 0px)) max(14px, env(safe-area-inset-left, 0px));
    box-sizing: border-box;
    overflow-y: auto;
}

.menu-top-bar {
    width: 100%;
    max-width: 460px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
}

.menu-profile-badge {
    background: rgba(26, 32, 53, 0.85);
    border: 1.5px solid #2563eb;
    border-radius: 20px;
    padding: 5px 12px;
    color: #f3f4f6;
    font-size: 11px;
    font-family: inherit;
    font-weight: bold;
    cursor: pointer;
    box-shadow: 0 0 10px rgba(37, 99, 235, 0.25);
    display: flex;
    align-items: center;
    gap: 5px;
    max-width: 48%;
}

.menu-profile-badge span#menu-player-name {
    display: inline-block;
    max-width: 85px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.menu-profile-badge .badge-link {
    color: #38bdf8;
    text-decoration: underline;
}

.menu-top1-badge {
    background: rgba(30, 27, 18, 0.9);
    border: 1.5px solid #eab308;
    border-radius: 20px;
    padding: 5px 12px;
    color: #facc15;
    font-size: 11px;
    font-family: inherit;
    font-weight: bold;
    cursor: pointer;
    box-shadow: 0 0 12px rgba(234, 179, 8, 0.35);
    display: flex;
    align-items: center;
    gap: 5px;
    max-width: 48%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.menu-center-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
    max-width: 330px;
    margin: auto 0;
}

.menu-title-block {
    text-align: center;
    margin-bottom: 14px;
}

.menu-main-title {
    font-size: 24px;
    font-weight: 900;
    color: #fbbf24;
    text-shadow: 0 0 16px rgba(251, 191, 36, 0.6), 0 2px 4px rgba(0, 0, 0, 0.8);
    letter-spacing: 2px;
    margin-bottom: 3px;
}

.menu-sub-title {
    font-size: 11px;
    font-weight: bold;
    color: #38bdf8;
    letter-spacing: 3px;
    text-shadow: 0 0 8px rgba(56, 189, 248, 0.5);
}

.menu-mode-row {
    width: 100%;
    display: flex;
    align-items: center;
    gap: 6px;
    margin-bottom: 10px;
}

.menu-select-btn {
    background: #182035;
    border: 1.5px solid #2a3656;
    color: #f3f4f6;
    padding: 8px 6px;
    border-radius: 18px;
    font-family: inherit;
    font-size: 11px;
    font-weight: bold;
    outline: none;
    cursor: pointer;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);
    text-align: center;
    text-align-last: center;
    min-width: 0;
    width: 100%;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.menu-btn-gear {
    background: #1e293b;
    border: 1.5px solid #eab308;
    color: #facc15;
    border-radius: 50%;
    width: 34px;
    height: 34px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 15px;
    cursor: pointer;
    flex-shrink: 0;
    box-shadow: 0 0 10px rgba(234, 179, 8, 0.3);
    transition: transform 0.15s ease, background 0.15s ease;
}

.menu-btn-gear:active {
    transform: scale(0.92) rotate(45deg);
    background: #334155;
}

.menu-buttons-list {
    width: 100%;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.menu-btn-play {
    width: 100%;
    background: linear-gradient(135deg, #ff5722 0%, #ea580c 50%, #c2410c 100%);
    border: 2px solid #fdba74;
    border-radius: 24px;
    padding: 12px 18px;
    color: #ffffff;
    font-size: 15px;
    font-weight: 900;
    font-family: inherit;
    letter-spacing: 2px;
    cursor: pointer;
    box-shadow: 0 0 18px rgba(234, 88, 12, 0.6), 0 5px 12px rgba(0, 0, 0, 0.5);
    transition: transform 0.1s ease;
}

.menu-btn-play:active {
    transform: scale(0.96);
}

.menu-btn-secondary {
    width: 100%;
    background: #161c2d;
    border: 1.5px solid #28334e;
    border-radius: 20px;
    padding: 9px 14px;
    color: #e2e8f0;
    font-size: 11.5px;
    font-weight: bold;
    font-family: inherit;
    cursor: pointer;
    box-shadow: 0 3px 8px rgba(0, 0, 0, 0.3);
    transition: background 0.15s ease, border-color 0.15s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
}

.menu-btn-secondary:active,
.menu-btn-secondary:hover {
    background: #232c45;
    border-color: #3b82f6;
}

.menu-footer {
    font-size: 9.5px;
    color: #64748b;
    letter-spacing: 1px;
    margin-top: 10px;
}

/* ======================================================= */
/* ИГРОВОЙ ЭКРАН И ВЕРХНИЙ БАР                             */
/* ======================================================= */

#top-bar {
    position: relative;
    width: 100%;
    height: 42px;
    background: rgba(11, 13, 19, 0.96);
    border-bottom: 1px solid #2a2f42;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 max(10px, env(safe-area-inset-right, 0px)) 0 max(10px, env(safe-area-inset-left, 0px));
    z-index: 25;
    flex-shrink: 0;
}

#btn-top-profile {
    max-width: 130px;
}

#top-bar-player-name {
    display: inline-block;
    max-width: 85px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    vertical-align: middle;
}

.top-bar-center {
    display: flex;
    align-items: center;
    gap: 8px;
}

#timer-badge {
    display: none;
    background: #ef4444;
    color: #fff;
    padding: 3px 10px;
    border-radius: 12px;
    font-weight: bold;
    font-size: 13px;
    letter-spacing: 1px;
    box-shadow: 0 0 10px rgba(239, 68, 68, 0.6);
}

.btn {
    background: #202434;
    color: #f3f4f6;
    border: 1px solid #414966;
    padding: 6px 10px;
    font-size: 11px;
    font-family: inherit;
    font-weight: bold;
    cursor: pointer;
    border-radius: 4px;
    transition: 0.15s ease;
    touch-action: manipulation;
    display: flex;
    align-items: center;
    gap: 5px;
}

.btn:hover,
.btn:active {
    background: #353d5a;
    border-color: #60a5fa;
}

.btn-highlight {
    background: #2563eb;
    border-color: #60a5fa;
}

.btn-locked {
    background: #334155 !important;
    border-color: #475569 !important;
    color: #94a3b8 !important;
    opacity: 0.65;
    cursor: not-allowed;
    box-shadow: none !important;
}

.btn-locked:active,
.btn-locked:hover {
    background: #334155 !important;
    border-color: #475569 !important;
    transform: none !important;
}

#btn-claim-win-disconnect:not(:disabled) {
    background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%) !important;
    border-color: #f87171 !important;
    color: #ffffff !important;
    box-shadow: 0 0 16px rgba(239, 68, 68, 0.6) !important;
    font-weight: 900;
}

.status-led {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #ef4444;
    box-shadow: 0 0 6px #ef4444;
    transition: background 0.3s, box-shadow 0.3s;
}

.status-led.online {
    background: #22c55e;
    box-shadow: 0 0 8px #22c55e;
}

/* ======================================================= */
/* ПАНЕЛЬ ОЖИДАНИЯ В ОНЛАЙНЕ                               */
/* ======================================================= */

#online-wait-bar {
    position: absolute;
    top: 48px;
    left: 50%;
    transform: translateX(-50%);
    background: linear-gradient(135deg, rgba(24, 28, 44, 0.96) 0%, rgba(15, 18, 30, 0.98) 100%);
    border: 1.5px solid #f59e0b;
    border-radius: 24px;
    padding: 4px 12px;
    display: none;
    align-items: center;
    gap: 10px;
    z-index: 50;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.8), 0 0 12px rgba(245, 158, 11, 0.25);
}

.wait-bar-info {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 11px;
    font-weight: bold;
    color: #f3f4f6;
    white-space: nowrap;
}

#wait-player-name {
    color: #facc15;
    display: inline-block;
    max-width: 100px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    vertical-align: bottom;
}

#wait-timer-val {
    color: #38bdf8;
    background: rgba(0, 0, 0, 0.4);
    padding: 2px 6px;
    border-radius: 4px;
    font-family: monospace;
}

.wait-bar-btn {
    padding: 4px 10px;
    font-size: 11px;
    font-weight: bold;
    border-radius: 14px;
    border: 1px solid transparent;
    cursor: pointer;
    font-family: inherit;
    color: #fff;
    transition: 0.15s ease;
    display: flex;
    align-items: center;
    gap: 4px;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.4);
}

.wait-bar-btn.btn-claim {
    background: linear-gradient(135deg, #ef4444 0%, #b91c1c 100%);
    border-color: #f87171;
}

.wait-bar-btn.btn-save {
    background: linear-gradient(135deg, #10b981 0%, #047857 100%);
    border-color: #34d399;
}

/* ======================================================= */
/* ИГРОВОЕ ПОЛЕ И ЗОНА СТОЛА                               */
/* ======================================================= */

#canvas-wrapper {
    position: relative;
    width: 100%;
    flex: 1;
    height: calc(100% - 42px);
    height: calc(100dvh - 42px);
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    touch-action: none;
}

@media (orientation: landscape) {
    #canvas-wrapper {
        padding-left: calc(112px + env(safe-area-inset-left, 0px));
        padding-right: calc(96px + env(safe-area-inset-right, 0px));
        padding-top: 6px;
        padding-bottom: 6px;
    }
}

@media (orientation: portrait) {
    #canvas-wrapper {
        padding-top: 72px;
        padding-bottom: 124px;
        padding-left: 8px;
        padding-right: 8px;
    }
}

canvas {
    display: block;
    box-shadow: 0 15px 50px rgba(0, 0, 0, 0.9);
    border-radius: 6px;
    touch-action: none;
}

/* ======================================================= */
/* ЛЕВЫЕ БЛОКИ: ПОЛКА ШАРОВ И ВИНТ (ЕДИНЫЙ ЛЕВЫЙ КРАЙ)     */
/* ======================================================= */

#ball-rack-container {
    position: absolute;
    top: 48px;
    left: max(10px, env(safe-area-inset-left, 0px));
    width: 96px;
    z-index: 15;
    background: linear-gradient(145deg, #3c1e08 0%, #241104 50%, #160a02 100%);
    border: 1.5px solid #5a3010;
    border-radius: 8px;
    padding: 5px 6px;
    box-shadow: 0 6px 18px rgba(0, 0, 0, 0.85);
    display: flex;
    flex-direction: column;
    gap: 4px;
    margin: 0;
}

.rack-title {
    font-size: 8px;
    font-weight: bold;
    color: #c49a6c;
    text-align: center;
    letter-spacing: 0.5px;
}

.rack-section {
    display: flex;
    flex-direction: column;
    gap: 3px;
    padding: 3px 4px;
    border-radius: 4px;
    background: rgba(0, 0, 0, 0.25);
}

.rack-section.active-turn {
    background: rgba(250, 204, 21, 0.12);
    border: 1px solid rgba(250, 204, 21, 0.4);
}

.rack-player-label {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 8px;
    font-weight: bold;
    color: #d1d5db;
}

.rack-player-label span:first-child {
    max-width: 60px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.rack-section.active-turn .rack-player-label span:first-child {
    color: #facc15;
}

.rack-score {
    color: #4ade80;
    font-size: 9px;
}

.rack-divider {
    height: 1px;
    background: #854d0e;
    margin: 1px 0;
}

.rack-shelf-row {
    display: flex;
    gap: 3px;
    align-items: center;
    justify-content: flex-start;
    height: 18px;
    background: #140801;
    border-radius: 9px;
    padding: 1px 3px;
    border-bottom: 1px solid #441f05;
}

.shelf-ball {
    width: 14px;
    height: 14px;
    border-radius: 50%;
    background: radial-gradient(circle at 35% 35%, #ffffff 0%, #fbf8eb 60%, #a89f89 100%);
    border: 1px solid rgba(0, 0, 0, 0.6);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 7.5px;
    font-weight: bold;
    color: #111;
    flex-shrink: 0;
}

.shelf-ball.cue-ball {
    background: radial-gradient(circle at 35% 35%, #fffbe6 0%, #ffd700 60%, #997a00 100%);
    color: #cc0000;
}

#spin-control-wrapper {
    position: absolute;
    left: max(10px, env(safe-area-inset-left, 0px));
    bottom: max(10px, env(safe-area-inset-bottom, 0px));
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 3px;
    z-index: 15;
    background: rgba(13, 15, 22, 0.92);
    border: 1.5px solid #2f364d;
    padding: 5px 6px;
    border-radius: 8px;
    width: 96px;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.7);
    margin: 0;
}

#spin-title {
    font-size: 8px;
    font-weight: bold;
    color: #94a3b8;
}

#spin-pad {
    width: 64px;
    height: 64px;
    border-radius: 50%;
    background: radial-gradient(circle at 35% 35%, #ffffff 0%, #cbd5e1 50%, #64748b 100%);
    border: 2px solid #e2e8f0;
    position: relative;
    cursor: crosshair;
    box-shadow: inset 0 0 8px rgba(0, 0, 0, 0.5);
    touch-action: none;
}

#spin-marker {
    position: absolute;
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: #ef4444;
    border: 2px solid #ffffff;
    box-shadow: 0 0 4px #ef4444;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    pointer-events: none;
}

/* ======================================================= */
/* ПРАВЫЕ БЛОКИ: СИЛА И УДАР                               */
/* ======================================================= */

#right-controls {
    position: absolute;
    right: max(10px, env(safe-area-inset-right, 0px));
    bottom: max(10px, env(safe-area-inset-bottom, 0px));
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    z-index: 15;
}

#power-control {
    position: relative;
    width: 36px;
    height: 150px;
    background: rgba(13, 15, 22, 0.92);
    border: 2px solid #333a52;
    border-radius: 20px;
    display: flex;
    flex-direction: column-reverse;
    align-items: center;
    padding: 3px;
    touch-action: none;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.7);
}

#power-fill {
    width: 100%;
    height: 50%;
    background: linear-gradient(to top, #22c55e 0%, #eab308 55%, #ef4444 100%);
    border-radius: 16px;
    transition: height 0.04s linear;
}

#power-label {
    position: absolute;
    top: -18px;
    font-size: 9px;
    font-weight: bold;
    color: #94a3b8;
    letter-spacing: 1px;
}

#hit-btn {
    width: 62px;
    height: 62px;
    border-radius: 50%;
    background: radial-gradient(circle at 35% 35%, #ef4444 0%, #b91c1c 70%, #7f1d1d 100%);
    border: 3px solid #fca5a5;
    color: #fff;
    font-weight: bold;
    font-size: 13px;
    font-family: inherit;
    box-shadow: 0 0 16px rgba(239, 68, 68, 0.55);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    touch-action: manipulation;
}

#hit-btn:active {
    transform: scale(0.92);
}

/* ======================================================= */
/* АДАПТАЦИЯ ДЛЯ ПОРТРЕТНОГО РЕЖИМА                        */
/* ======================================================= */

@media (orientation: portrait) {
    #ball-rack-container {
        top: 48px;
        left: 50%;
        transform: translateX(-50%);
        width: 94%;
        max-width: 440px;
        flex-direction: row;
        justify-content: space-between;
    }

    .rack-title {
        display: none;
    }

    .rack-section {
        flex: 1;
    }

    .rack-divider {
        width: 1px;
        height: 100%;
        margin: 0 4px;
    }

    #power-control {
        height: 100px;
        width: 30px;
    }

    #hit-btn {
        width: 52px;
        height: 52px;
        font-size: 11px;
    }

    #spin-control-wrapper {
        width: 80px;
        padding: 4px 6px;
    }

    #spin-pad {
        width: 50px;
        height: 50px;
    }
}

/* ======================================================= */
/* МОДАЛЬНЫЕ ОКНА                                          */
/* ======================================================= */

.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.85);
    backdrop-filter: blur(5px);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 300;
}

.modal {
    background: #151824;
    border: 2px solid #333d59;
    border-radius: 10px;
    width: 94%;
    max-width: 620px;
    max-height: 92vh;
    overflow-y: auto;
    padding: 18px;
    color: #f3f4f6;
    box-shadow: 0 25px 60px rgba(0, 0, 0, 0.95);
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid #283047;
    padding-bottom: 8px;
    margin-bottom: 12px;
}

.modal-header h2 {
    font-size: 15px;
    color: #60a5fa;
    letter-spacing: 0.5px;
}

.tabs-nav {
    display: flex;
    flex-wrap: wrap;
    gap: 5px;
    margin-bottom: 14px;
    border-bottom: 1px solid #2b3248;
    padding-bottom: 8px;
}

.tab-btn {
    background: #1f2538;
    border: 1px solid #3d4766;
    color: #94a3b8;
    padding: 6px 10px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 11px;
    font-family: inherit;
    font-weight: bold;
}

.tab-btn.active {
    background: #2563eb;
    color: #ffffff;
    border-color: #60a5fa;
}

.tab-content {
    display: none;
}

.tab-content.active {
    display: block;
}

.form-group {
    margin-bottom: 12px;
}

.form-group label {
    display: block;
    font-size: 11px;
    margin-bottom: 4px;
    color: #94a3b8;
    font-weight: bold;
}

.form-group input[type="text"],
.form-group select,
.form-group input[type="range"] {
    width: 100%;
    background: #20263a;
    border: 1px solid #3a4566;
    color: #fff;
    padding: 7px 10px;
    border-radius: 5px;
    font-family: inherit;
    font-size: 12px;
}

.color-row {
    display: flex;
    gap: 10px;
    align-items: center;
    font-size: 12px;
}

.color-row input[type="color"] {
    width: 44px;
    height: 32px;
    padding: 1px;
    cursor: pointer;
    background: transparent;
    border: none;
}

.color-presets-row {
    display: flex;
    gap: 8px;
    align-items: center;
    margin-top: 6px;
    padding-left: 2px;
}

.color-preset-btn {
    width: 22px;
    height: 22px;
    border-radius: 50%;
    border: 1.5px solid #475569;
    cursor: pointer;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.4);
    transition: transform 0.15s ease, border-color 0.15s ease;
}

.color-preset-btn:hover,
.color-preset-btn:active {
    transform: scale(1.2);
    border-color: #facc15;
}

.toggle-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 12px;
    padding: 3px 0;
}

.profile-card,
.leaderboard-card {
    background: #1b2030;
    border: 1px solid #333d5c;
    border-radius: 8px;
    padding: 12px;
    margin-bottom: 14px;
}

.profile-field,
.leaderboard-row {
    display: flex;
    justify-content: space-between;
    padding: 6px 0;
    font-size: 12px;
    border-bottom: 1px dashed #283047;
}

.profile-field:last-child,
.leaderboard-row:last-child {
    border-bottom: none;
}

.profile-field span.val,
.leaderboard-row span.time {
    color: #38bdf8;
    font-weight: bold;
    letter-spacing: 1px;
}

.devices-list,
.rooms-list {
    margin-top: 10px;
    max-height: 150px;
    overflow-y: auto;
    font-size: 11px;
}

.device-item,
.room-item {
    background: #232a3f;
    padding: 7px 10px;
    border-radius: 4px;
    margin-bottom: 6px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.btn-unlink-device {
    background: linear-gradient(135deg, #7f1d1d 0%, #450a0a 100%);
    border: 1.5px solid #f59e0b;
    color: #facc15;
    border-radius: 50%;
    width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    font-weight: bold;
    cursor: pointer;
    box-shadow: 0 0 6px rgba(245, 158, 11, 0.4), inset 0 0 4px rgba(127, 29, 29, 0.8);
    transition: transform 0.15s ease, background 0.15s ease, border-color 0.15s ease;
    flex-shrink: 0;
}

.btn-unlink-device:hover,
.btn-unlink-device:active {
    transform: scale(1.18);
    background: linear-gradient(135deg, #991b1b 0%, #581c1c 100%);
    border-color: #fde047;
    color: #ffffff;
    box-shadow: 0 0 10px rgba(250, 204, 21, 0.7);
}

.status-toast {
    position: absolute;
    top: 50px;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(15, 23, 42, 0.96);
    border: 1px solid #38bdf8;
    padding: 6px 16px;
    border-radius: 16px;
    font-size: 12px;
    color: #38bdf8;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.3s;
    z-index: 50;
    font-weight: bold;
    text-align: center;
    max-width: 90%;
}