:root {
    --color-primary: #FFD700;
    /* Yellow */
    --color-secondary: #0000FF;
    /* Blue */
    --color-accent: #FF0000;
    /* Red */
    --color-success: #008000;
    /* Green */
    --color-bg: #1a1a1a;
    --color-text: #FFFFFF;
    --border-width: 4px;
    --font-header: 'Black Ops One', cursive;
    --font-text: 'VT323', monospace;

    --app-scale: 1;
    --app-margin-top: 0px;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    user-select: none;
    -webkit-user-select: none;
}

body {
    background-color: var(--color-bg);
    background-image: url('background.png');
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
    color: var(--color-text);
    font-family: var(--font-text);
    overflow: hidden;
    height: 100vh;
    width: 100vw;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}

/* --- Layers --- */
.layer {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-start;
    z-index: 10;
    transition: opacity 0.5s ease;
    overflow-y: auto;
    /* Changed from hidden to auto to prevent vertical overlap on small screens */
    overflow-x: hidden;
}

/* Content Scaling Wrapper */
canvas {
    display: block;
}

.menu-content,
.game-header,
.game-container,
#intro-layer button {
    transform: scale(var(--app-scale));
    transform-origin: top center;
    margin-top: var(--app-margin-top);
}

/* Adjust Intro Video scaling might be different */
#intro-video {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    min-width: 100%;
    min-height: 100%;
    z-index: -1;
}

.hidden {
    display: none !important;
    opacity: 0;
    pointer-events: none;
}

/* --- Intro Layer --- */
#intro-layer {
    background: #000;
    z-index: 100;
}

#intro-video {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

#skip-intro {
    position: absolute;
    bottom: 10%;
    z-index: 101;
    animation: blink 1s infinite;
}

/* --- Menu Layer --- */
#menu-layer {
    background: rgba(0, 0, 0, 0.85);
}

.menu-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2rem;
    width: 95%;
    max-width: 1200px;
    /* Increased to allow 3-column side-by-side */
}

.main-logo {
    width: 80%;
    max-width: 400px;
    image-rendering: pixelated;
    animation: bounce 2s infinite ease-in-out;
}

#profile-card {
    position: absolute;
    top: 20px;
    right: 20px;
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 15px;
    background: rgba(0, 0, 0, 0.9);
    cursor: pointer;
    transition: transform 0.2s, border-color 0.2s;
    z-index: 1000;
}

#profile-card:hover {
    transform: scale(1.05);
    border-color: var(--color-accent);
}

#menu-avatar {
    width: 40px;
    height: 40px;
    border: 2px solid white;
}

#menu-username {
    font-size: 1.5rem;
    color: var(--color-primary);
    font-family: var(--font-header);
}

/* --- Menu Layout --- */
.menu-container {
    display: flex;
    flex-wrap: wrap;
    /* Allows panels to stack dynamically */
    flex-direction: row;
    align-items: flex-start;
    justify-content: center;
    gap: 20px;
    width: 100%;
    max-width: 1200px;
}

.menu-left,
.menu-right {
    background: #000;
    padding: 1.5rem;
    display: flex;
    flex-direction: column;
    align-items: center;
    border: var(--border-width) solid var(--color-primary);
    /* Ensure explicit border here if class used elsewhere without retro-border */
}

.menu-left {
    flex: 1;
    min-width: 250px;
    height: 400px;
    /* Match right height */
}

.menu-right {
    flex: 1;
    min-width: 280px;
    /* height: auto; Allow button container to fit */
    display: flex;
    justify-content: center;
}

/* Sub-menus */
#lobby-menu {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
    gap: 10px;
    animation: fadeIn 0.3s;
}

#main-menu-actions {
    display: flex;
    flex-direction: column;
    gap: 15px;
    width: 100%;
    animation: fadeIn 0.3s;
}

.divider {
    margin: 10px 0;
    border-top: 1px solid #444;
    width: 100%;
}

.private-room-section {
    display: flex;
    flex-direction: column;
    gap: 10px;
    align-items: center;
    width: 100%;
    background: #111;
    padding: 10px;
    border: 1px solid #333;
}

#lobby-status {
    font-size: 1.5rem;
    color: var(--color-primary);
    text-align: center;
    margin-bottom: 1rem;
    min-height: 30px;
}

.retro-input {
    background: #000;
    border: 2px solid var(--color-primary);
    color: white;
    font-family: var(--font-header);
    font-size: 1.5rem;
    padding: 0.5rem;
    text-align: center;
    width: 80%;
    text-transform: uppercase;
    letter-spacing: 5px;
}

.row {
    display: flex;
    gap: 10px;
    width: 100%;
    justify-content: center;
}

.leaderboard-section {
    width: 100%;
    text-align: center;
    height: 100%;
    display: flex;
    flex-direction: column;
}

.leaderboard-section h3 {
    color: var(--color-accent);
    font-family: var(--font-header);
    border-bottom: 2px solid #333;
    padding-bottom: 5px;
    margin-bottom: 10px;
}

#leaderboard-list {
    list-style: none;
    font-family: var(--font-text);
    color: #ccc;
    font-size: 1.2rem;
    overflow-y: auto;
    flex-grow: 1;
    text-align: left;
}

#leaderboard-list li {
    display: flex;
    justify-content: space-between;
    padding: 5px 10px;
    border-bottom: 1px solid #222;
}

/* Golden */
#leaderboard-list li:nth-child(1) {
    color: #FFD700;
}

/* Silver */
#leaderboard-list li:nth-child(2) {
    color: #C0C0C0;
}

/* Bronze */
#leaderboard-list li:nth-child(3) {
    color: #CD7F32;
}

/* In-Lobby Roster Styles */
#lobby-roster li {
    display: flex;
    justify-content: space-between;
    padding: 5px 10px;
    border-bottom: 1px solid #222;
    align-items: center;
}

.ready-status {
    font-size: 1rem;
    padding: 3px 8px;
    border-radius: 4px;
    text-transform: uppercase;
    font-weight: bold;
}

.status-ready {
    background-color: var(--color-success);
    color: white;
}

.status-waiting {
    background-color: #555;
    color: #ccc;
}

.host-icon {
    color: var(--color-primary);
    margin-right: 5px;
}

/* Responsive Adjustments */
@media (max-width: 600px) {
    .menu-container {
        flex-direction: column-reverse;
        /* Leaderboard at bottom on mobile? Or standard column */
        align-items: center;
    }

    /* Adjust order if needed, but default column is Top->Bottom. 
       HTML order is Leaderboard -> Menu. 
       On mobile maybe Menu should be first? 
       Use column-reverse to put Menu (Right in HTML) on Top.
    */
    .menu-container {
        flex-direction: column-reverse;
    }

    .menu-right {
        height: auto;
        width: 100%;
        margin-bottom: 20px;
    }
}


.controls-info {
    text-align: center;
    font-size: 1.2rem;
    color: #aaa;
    margin-top: 1rem;
    border-top: 2px solid #444;
    padding-top: 1rem;
    width: 100%;
}

/* --- Game Layer --- */
#game-layer {
    justify-content: flex-start;
    padding-top: 1rem;
    flex-direction: column;
}

.game-header {
    width: 100%;
    max-width: 600px;
    display: flex;
    justify-content: flex-start;
    padding: 0 1rem;
    margin-bottom: 0.5rem;
    z-index: 2000;
    pointer-events: auto;
}

.small-btn {
    font-size: 1rem;
    padding: 0.5rem 1rem;
    background-color: #cc0000 !important;
    /* Force Red */
    border-bottom-color: #880000;
}

.small-btn:hover {
    background-color: #ff0000;
}

.game-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
    height: 100%;
    max-width: 600px;
    gap: 1rem;
    position: relative;
}

#fx-canvas {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 50;
}

/* --- Boards --- */
.retro-border {
    border: var(--border-width) solid var(--color-primary);
    box-shadow:
        4px 4px 0px var(--color-secondary),
        -4px -4px 0px var(--color-accent);
    background: rgba(0, 0, 0, 0.9);
    position: relative;
    /* Eliminate stray text/descender gaps inside wrappers */
    display: flex;
    flex-direction: column;
}

.player-info {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    padding: 0.5rem;
    background: var(--color-secondary);
    color: white;
    font-family: var(--font-header);
    border-bottom: 2px solid var(--color-primary);
}

.avatar {
    width: 32px;
    height: 32px;
    border: 2px solid white;
    margin-right: 0.5rem;
}

/* Opponent Area */
.opponent-area {
    width: 200px;
    /* Scaled down */
    display: flex;
    flex-direction: column;
    align-items: center;
    transform: scale(0.8);
    transform-origin: top center;
}

#opponent-canvas {
    background: #111;
    image-rendering: pixelated;
    width: 150px;
    height: 300px;
}

/* Player Area */
.player-area {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-top: -30px;
    /* Overlap slightly due to scale */
}

.hud-row {
    display: flex;
    align-items: flex-start;
    gap: 1rem;
}

.hud-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    background: rgba(0, 0, 0, 0.8);
    border: 2px solid #444;
    padding: 0.5rem;
    width: 80px;
}

.hud-label {
    color: #888;
    font-size: 1.2rem;
    margin-bottom: 0.2rem;
}

#player-canvas {
    background: #111;
    image-rendering: pixelated;
}

/* --- Mobile Controls --- */
.mobile-controls {
    position: absolute;
    bottom: 20px;
    width: 100%;
    display: flex;
    justify-content: space-between;
    padding: 0 20px;
    pointer-events: none;
    /* Let clicks pass through empty space */
}

.d-pad,
.action-pad {
    display: flex;
    gap: 10px;
    pointer-events: auto;
}

.d-pad {
    align-items: flex-end;
}

.control-btn {
    width: 60px;
    height: 60px;
    background: rgba(255, 255, 255, 0.15);
    border: 2px solid var(--color-primary);
    border-radius: 50%;
    color: white;
    font-size: 32px;
    /* Bigger Icon */
    display: flex;
    align-items: center;
    justify-content: center;
    touch-action: manipulation;
    /* Prevent selection */
    user-select: none;
    -webkit-user-select: none;
}

.control-btn:active {
    background: var(--color-primary);
    color: black;
    transform: scale(0.95);
}

/* Desktop Hide Controls */
@media (min-width: 768px) {
    .mobile-controls {
        display: none;
    }

    .opponent-area {
        transform: scale(1);
        margin-bottom: 20px;
    }

    .game-container {
        flex-direction: row;
        justify-content: center;
        align-items: flex-start;
        padding-top: 50px;
    }

    .player-area {
        margin-top: 0;
        margin-left: 20px;
    }
}

/* --- Components --- */
.retro-btn {
    background: var(--color-success);
    color: white;
    font-family: var(--font-header);
    font-size: 1.5rem;
    padding: 1rem 2rem;
    border: none;
    border-bottom: 4px solid #005000;
    cursor: pointer;
    transition: transform 0.1s;
    text-transform: uppercase;
    letter-spacing: 2px;
}

.retro-btn:active {
    transform: translateY(4px);
    border-bottom: 0;
    margin-top: 4px;
}

.retro-btn:hover {
    background: #00a000;
}

/* --- Modal --- */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.7);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 3000;
    animation: fadeIn 0.3s;
}

.modal-content {
    background: var(--color-bg);
    padding: 2rem;
    text-align: center;
    min-width: 300px;
    animation: popUp 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

/* --- Component: Private Room Section --- */
.private-room-section {
    display: flex;
    flex-direction: column;
    width: 100%;
    gap: 10px;
    margin-top: 15px;
}

.private-room-section .retro-input {
    width: 100%;
    font-size: 1.5rem;
    padding: 0.5rem;
    text-align: center;
    background: #222;
    border: 2px solid #555;
    color: white;
    font-family: var(--font-header);
    text-transform: uppercase;
}

.private-room-section .row {
    display: flex;
    justify-content: space-between;
    gap: 10px;
    width: 100%;
}

.private-room-section .row button {
    flex: 1;
    /* Split width 50/50 */
    font-size: 1rem;
    padding: 0.8rem;
    white-space: nowrap;
}

.modal-content h2 {
    font-family: var(--font-header);
    font-size: 3rem;
    color: var(--color-primary);
    margin-bottom: 1rem;
    text-shadow: 2px 2px var(--color-accent);
}

.modal-content p {
    font-size: 1.5rem;
    margin-bottom: 2rem;
}

.modal-content button {
    /* Base styles handled by .retro-btn class in HTML */
    background: var(--color-accent);
    border-bottom-color: #aa0000;
    color: white;
    font-family: var(--font-header);
    font-size: 1.5rem;
    padding: 1rem 2rem;
    border: none;
    border-bottom: 4px solid #aa0000;
    cursor: pointer;
    transition: transform 0.1s;
    text-transform: uppercase;
}

.modal-content button:hover {
    background: #ff3333;
}

.modal-content button:active {
    transform: translateY(4px);
    border-bottom: 0;
    margin-top: 4px;
}


/* --- Animations --- */
@keyframes blink {

    0%,
    100% {
        opacity: 1;
    }

    50% {
        opacity: 0;
    }
}

@keyframes bounce {

    0%,
    100% {
        transform: translateY(0);
    }

    50% {
        transform: translateY(-10px);
    }
}

@keyframes popUp {
    from {
        transform: scale(0.5);
        opacity: 0;
    }

    to {
        transform: scale(1);
        opacity: 1;
    }
}

@keyframes fadeIn {
    from {
        opacity: 0;
    }

    to {
        opacity: 1;
    }
}

@keyframes shake {
    0% {
        transform: translate(1px, 1px) rotate(0deg);
    }

    10% {
        transform: translate(-1px, -2px) rotate(-1deg);
    }

    20% {
        transform: translate(-3px, 0px) rotate(1deg);
    }

    30% {
        transform: translate(3px, 2px) rotate(0deg);
    }

    40% {
        transform: translate(1px, -1px) rotate(1deg);
    }

    50% {
        transform: translate(-1px, 2px) rotate(-1deg);
    }

    60% {
        transform: translate(-3px, 1px) rotate(0deg);
    }

    70% {
        transform: translate(3px, 1px) rotate(-1deg);
    }

    80% {
        transform: translate(-1px, -1px) rotate(1deg);
    }

    90% {
        transform: translate(1px, 2px) rotate(0deg);
    }

    100% {
        transform: translate(1px, -2px) rotate(-1deg);
    }
}

.shake {
    animation: shake 0.5s;
    animation-iteration-count: 1;
}

/* --- Utilities --- */
.text-center {
    text-align: center;
}

.mt-1 {
    margin-top: 1rem;
}

.mb-1 {
    margin-bottom: 1rem;
}

/* --- CRT Effect Overlay (Optional Polish) --- */
body::after {
    content: " ";
    display: block;
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    background: linear-gradient(rgba(18, 16, 16, 0) 50%, rgba(0, 0, 0, 0.25) 50%), linear-gradient(90deg, rgba(255, 0, 0, 0.06), rgba(0, 255, 0, 0.02), rgba(0, 0, 255, 0.06));
    z-index: 200;
    background-size: 100% 2px, 3px 100%;
    pointer-events: none;
}

/* --- Responsive Adjustments --- */
/* --- Custom Components (Chat, Lobbies, Stats) --- */
.chat-section {
    display: flex;
    flex-direction: column;
    width: 100%;
    height: 100%;
    margin-top: 10px;
    flex-grow: 1;
}

.chat-section h3 {
    color: var(--color-accent);
    font-family: var(--font-header);
    border-bottom: 2px solid #333;
    padding-bottom: 5px;
    margin-bottom: 5px;
    text-align: center;
}

.chat-messages {
    flex-grow: 1;
    overflow-y: auto;
    background: #111;
    border: 1px solid #333;
    padding: 10px;
    margin-bottom: 10px;
    font-family: var(--font-text);
    font-size: 1.2rem;
    color: #eee;
    text-align: left;
    height: 150px;
    /* Fixed height to prevent breaking layout */
}

.chat-messages .chat-line {
    margin-bottom: 5px;
    word-wrap: break-word;
}

.chat-messages .chat-time {
    color: #666;
    font-size: 0.8em;
    margin-right: 5px;
}

.chat-messages .chat-name {
    color: var(--color-primary);
    font-weight: bold;
    margin-right: 5px;
}

.chat-messages .chat-text {
    color: white;
}

.chat-input-row {
    display: flex;
    gap: 5px;
    width: 100%;
}

.chat-input-row .retro-input {
    flex-grow: 1;
    width: auto;
    font-size: 1rem;
    text-transform: none;
    /* Allow lower case for chat */
    letter-spacing: normal;
}

.lobby-browser-section {
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
}

.lobby-list {
    list-style: none;
    width: 100%;
    max-height: 150px;
    overflow-y: auto;
    background: #111;
    border: 1px solid #333;
    padding: 5px;
    margin: 0;
}

.lobby-list li {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 5px 10px;
    border-bottom: 1px solid #222;
    color: #ddd;
    font-family: var(--font-text);
    font-size: 1.2rem;
}

@media (max-width: 380px) {
    .opponent-area {
        transform: scale(0.6);
        margin-bottom: -50px;
    }

    #player-canvas {
        width: 200px;
        height: 400px;
    }

    .control-btn {
        width: 50px;
        height: 50px;
        font-size: 20px;
    }
}

@media (max-height: 700px) and (orientation: portrait) {
    .main-logo {
        width: 60%;
    }

    .game-container {
        gap: 0;
    }

    .opponent-area {
        transform: scale(0.6);
    }

    .player-area {
        transform: scale(0.9);
        margin-top: -40px;
    }
}