html,
body {
    background-color: #444;
    width: 100%;
    height: 100%;
    padding: 0;
    margin: 0;
    box-sizing: border-box;
    overflow: hidden;
    touch-action: none;
}

body {
    padding: 12px;
}

div,
p,
button {
    box-sizing: border-box;
    padding: 0px;
    margin: 0px;
}

button {
    border: none;
    background-color: unset;
    -ms-touch-action: manipulation;
    touch-action: manipulation;
    -webkit-user-select: none; /* Safari/iOS */
    -moz-user-select: none; /* Firefox */
    -ms-user-select: none; /* IE/Edge */
    user-select: none;
    -webkit-touch-callout: none;
    -webkit-tap-highlight-color: transparent;
}

label {
    user-select: none;
}

/* Controller display */
#controller-body {
    width: 100%;
    height: 100%;
    box-sizing: border-box;
    /*background-color: darkred;*/
    border-radius: 24px;
    background: linear-gradient(
        145deg,
        #ff6a55,
        #ff3f31
    ); /* Base gradient for color */
    box-shadow:
        inset 8px 8px 4px rgba(255, 255, 255, 0.2),
        inset -8px -8px 4px rgba(0, 0, 0, 0.2),
        -8px -4px 0px #ff4f41,
        -8px -4px 0px #ff1f11,
        8px 8px 8px rgba(0, 0, 0, 0.2);
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

#screen-container {
    /*border: 1px solid rgba(0, 0, 0, 0.5);*/
    /*background-color: rgba(0, 0, 0, 0.2);*/
    flex: 1;
    padding: 24px;
    position: relative;
}

#game-container {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
}

#screen {
    position: relative;
    width: 100%;
    height: 100%;
    background-color: #444;
    border-radius: 4px;
    overflow: hidden;
    box-sizing: border-box;
}

#screen-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    border-radius: 4px;
    border: 8px solid black;
    border-left: 20px solid black;
    border-right: 20px solid black;
    box-sizing: border-box;
    box-shadow:
        inset 6px 6px 8px rgba(0, 0, 0, 0.3),
        inset -3px -3px 4px rgba(0, 0, 0, 0.2);
}

#screen-shine-1 {
    position: absolute;
    top: 20px;
    left: -20%;
    width: 100%;
    height: 6px;
    border-radius: 4px;
    background-color: rgba(255, 255, 255, 0.2);
    transform: rotate(-27deg);
    mix-blend-mode: screen;
}
#screen-shine-2 {
    position: absolute;
    top: 40px;
    left: -20%;
    width: 100%;
    height: 12px;
    border-radius: 4px;
    background-color: rgba(255, 255, 255, 0.2);
    transform: rotate(-27deg);
    mix-blend-mode: hard-light;
}
#power-indicator {
    position: absolute;
    top: 50px;
    left: 8px;
    width: 4px;
    height: 4px;
    border-radius: 4px;
    background: radial-gradient(
        circle at 50% 50%,
        rgba(255, 255, 255, 0.8) 40%,
        rgba(50, 255, 150, 1) 60%
    );
    border: 1px solid rgba(0, 0, 0, 0.1);
    box-shadow:
        inset 0 0 1px rgba(0, 0, 0, 0.2),
        0 0 8px rgba(255, 255, 255, 1);
}

#controls-container {
    /*border: 1px solid rgba(0, 0, 0, 0.5);*/
    flex: 1;
    display: flex;
    flex-direction: column;
}

#controls-top {
    padding: 8px;
    display: flex;
    justify-content: center;
}

#controls-top > p {
    color: rgba(0, 0, 0, 0.2);
    font-size: 24px;
    font-weight: 600;
    padding: 8px;
    font-family:
        "Gill Sans", "Gill Sans MT", Calibri, "Trebuchet MS", sans-serif;
    text-align: center;
    border: 2px solid rgba(0, 0, 0, 0.2);
    width: fit-content;
    border-radius: 24px;
    box-shadow:
        inset 1px 1px 1px rgba(255, 255, 255, 0.1),
        inset -1px -1px 1px rgba(0, 0, 0, 0.2),
        -1px -1px 1px rgba(0, 0, 0, 0.2),
        1px 1px 1px rgba(255, 255, 255, 0.1);
    text-shadow:
        /*1px 1px 1px rgba(255, 255, 255, 0.1),
        -1px -1px 1px rgba(0, 0, 0, 0.2),*/
        -1px -1px 1px rgba(0, 0, 0, 0.2),
        1px 1px 1px rgba(255, 255, 255, 0.1);
}

#controls-area {
    display: flex;
    padding: 12px;
}

#pad-container {
    flex: 1;
    display: flex;
    padding-left: 16px;
    align-items: center;
    padding-top: 12px;
    padding-bottom: 12px;
}

#pad {
    display: grid;
    grid-template-areas:
        ". u ."
        "l c r"
        ". d .";
}
#pad > button {
    width: 36px;
    height: 36px;
    background-color: #444;
    color: rgba(0, 0, 0, 0.2);
    text-shadow:
        -1px -1px 1px rgba(0, 0, 0, 0.2),
        1px 1px 1px rgba(255, 255, 255, 0.1);
}
#pad-up {
    grid-area: u;
    border-top-left-radius: 8px;
    border-top-right-radius: 8px;
    box-shadow:
        inset 2px 2px 1px rgba(255, 255, 255, 0.2),
        inset -2px 0px 1px rgba(0, 0, 0, 0.2),
        -2px -2px 1px rgba(0, 0, 0, 0.2),
        2px 2px 1px rgba(255, 255, 255, 0.2);
}
#pad-up:active {
    box-shadow:
        inset 2px 2px 2px rgba(255, 255, 255, 0.1),
        inset -2px 1px 3px rgba(0, 0, 0, 0.4),
        -2px -2px 1px rgba(0, 0, 0, 0.2),
        2px 2px 1px rgba(255, 255, 255, 0.2);
    background: linear-gradient(0deg, #444, #383838);
    text-shadow:
        -1px -1px 1px rgba(0, 0, 0, 0.3),
        1px 1px 1px rgba(255, 255, 255, 0.1);
}
#pad-left {
    grid-area: l;
    border-top-left-radius: 8px;
    border-bottom-left-radius: 8px;
    box-shadow:
        inset 2px 2px 1px rgba(255, 255, 255, 0.2),
        inset 0 -2px 1px rgba(0, 0, 0, 0.2),
        -2px -2px 1px rgba(0, 0, 0, 0.2),
        2px 2px 1px rgba(255, 255, 255, 0.2);
}
#pad-left:active {
    box-shadow:
        inset 2px 2px 2px rgba(255, 255, 255, 0.1),
        inset 2px -2px 3px rgba(0, 0, 0, 0.4),
        -2px -2px 1px rgba(0, 0, 0, 0.2),
        2px 2px 1px rgba(255, 255, 255, 0.2);
    background: linear-gradient(270deg, #444, #383838);
    text-shadow:
        -1px -1px 1px rgba(0, 0, 0, 0.3),
        1px 1px 1px rgba(255, 255, 255, 0.1);
}
#pad-right {
    grid-area: r;
    border-top-right-radius: 8px;
    border-bottom-right-radius: 8px;
    box-shadow:
        inset 0 2px 1px rgba(255, 255, 255, 0.2),
        inset 0 -2px 1px rgba(0, 0, 0, 0.2),
        -2px -2px 1px rgba(0, 0, 0, 0.2),
        2px 2px 1px rgba(255, 255, 255, 0.2);
}
#pad-right:active {
    box-shadow:
        inset 2px 2px 2px rgba(255, 255, 255, 0.1),
        inset 1px -2px 3px rgba(0, 0, 0, 0.4),
        -2px -2px 1px rgba(0, 0, 0, 0.2),
        2px 2px 1px rgba(255, 255, 255, 0.2);
    background: linear-gradient(90deg, #444, #383838);
    text-shadow:
        -1px -1px 1px rgba(0, 0, 0, 0.3),
        1px 1px 1px rgba(255, 255, 255, 0.1);
}
#pad-down {
    grid-area: d;
    border-bottom-right-radius: 8px;
    border-bottom-left-radius: 8px;
    box-shadow:
        inset 2px 0px 1px rgba(255, 255, 255, 0.1),
        inset -2px -2px 1px rgba(0, 0, 0, 0.2),
        -2px -2px 1px rgba(0, 0, 0, 0.3),
        2px 2px 1px rgba(255, 255, 255, 0.2);
}
#pad-down:active {
    box-shadow:
        inset 2px 2px 2px rgba(255, 255, 255, 0.1),
        inset -2px 1px 3px rgba(0, 0, 0, 0.4),
        -2px -2px 1px rgba(0, 0, 0, 0.2),
        2px 2px 1px rgba(255, 255, 255, 0.2);
    background: linear-gradient(180deg, #444, #383838);
    text-shadow:
        -1px -1px 1px rgba(0, 0, 0, 0.3),
        1px 1px 1px rgba(255, 255, 255, 0.1);
}

#pad-center {
    grid-area: c;
    width: 36px;
    height: 36px;
    background-color: #444;
    box-shadow:
        2px 2px 4px #444,
        -2px -2px 4px #444;
}

#button-container {
    flex: 1;
    align-items: center;
    display: flex;
}

#button-stack {
    height: 110px;
    width: 100%;
    display: flex;
    gap: 16px;
    justify-content: flex-end;
    padding-right: 24px;
}

#button-a {
    align-self: flex-end;
}
#button-b {
    align-self: flex-start;
}

button.action-button {
    background-color: #444;
    width: 60px;
    height: 60px;
    border-radius: 50%;
    font-size: 18px;
    box-shadow:
        inset 3px 3px 0px rgba(255, 255, 255, 0.2),
        inset -3px -3px 0px rgba(0, 0, 0, 0.2),
        -2px -2px 1px rgba(0, 0, 0, 0.2),
        2px 2px 1px rgba(255, 255, 255, 0.2);
    color: rgba(0, 0, 0, 0.3);
    text-shadow:
        -1px -1px 1px rgba(0, 0, 0, 0.2),
        1px 1px 1px rgba(255, 255, 255, 0.1);
}

button.action-button:active {
    box-shadow:
        inset 3px 3px 0px rgba(255, 255, 255, 0.1),
        inset -4px -4px 1px rgba(0, 0, 0, 0.3),
        -2px -2px 1px rgba(0, 0, 0, 0.2),
        2px 2px 1px rgba(255, 255, 255, 0.2);
    color: rgba(0, 0, 0, 0.3);
    text-shadow:
        -1px -1px 1px rgba(0, 0, 0, 0.3),
        1px 1px 1px rgba(255, 255, 255, 0.1);
}

#controls-meta {
    padding: 32px;
    display: flex;
    align-items: flex-end;
    justify-content: center;
    flex: 1;
}
#meta-stack {
    display: flex;
    gap: 24px;
}

.meta-button-stack {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
}

.meta-button {
    width: 60px;
    height: 24px;
    background-color: #444;
    border-radius: 40px;
    box-shadow:
        inset 3px 3px 0px rgba(255, 255, 255, 0.2),
        inset -3px -3px 0px rgba(0, 0, 0, 0.2),
        -2px -2px 1px rgba(0, 0, 0, 0.2),
        2px 2px 1px rgba(255, 255, 255, 0.2);
}

.meta-button:active {
    box-shadow:
        inset 3px 3px 0px rgba(255, 255, 255, 0.1),
        inset -4px -4px 1px rgba(0, 0, 0, 0.3),
        -2px -2px 1px rgba(0, 0, 0, 0.2),
        2px 2px 1px rgba(255, 255, 255, 0.2);
}

.meta-button-stack > label {
    color: rgba(0, 0, 0, 0.1);
    font-size: 16px;
    font-weight: 600;
    font-family:
        "Gill Sans", "Gill Sans MT", Calibri, "Trebuchet MS", sans-serif;
    text-shadow:
        -1px -1px 0px rgba(0, 0, 0, 0.2),
        1px 1px 0px rgba(255, 255, 255, 0.1);
}

#game-canvas {
    position: absolute;
}

@media (orientation: landscape) {
    #controller-body {
        display: grid;
        grid-template-columns: minmax(0, 150px) minmax(340px, 1.4fr) minmax(
                0,
                150px
            );
        grid-template-rows: auto 1fr auto;
        gap: 8px;
        padding: 8px;
    }

    /* Flatten wrappers so inner sections can be placed on the grid */
    #controls-container,
    #controls-area {
        display: contents;
    }

    /* Center column: logo on top, screen fills remaining space */
    #controls-top {
        grid-column: 3;
        grid-row: 3;
        align-self: start;
        justify-self: center;
    }

    #screen-container {
        grid-column: 2;
        grid-row: 2 / span 2;
        align-self: stretch;
        justify-self: stretch;
        height: 100%;
        padding: 0px 12px 4px 12px;
    }

    /* Left column: D-pad centered, meta buttons at bottom */
    #pad-container {
        grid-column: 1;
        grid-row: 2;
        align-self: center;
        justify-self: center;
        padding-left: 0;
    }

    #controls-meta {
        grid-column: 1;
        grid-row: 3;
        align-self: end;
        justify-self: center;
        padding: 16px 0 20px 0;
    }

    /* Right column: action buttons centered */
    #button-container {
        grid-column: 3;
        grid-row: 2;
        align-self: center;
        justify-self: center;
    }

    /* Action buttons layout refinements for landscape */
    #button-stack {
        justify-content: center;
        padding: 0;
        gap: 0;
    }
}

/* HUD (local player) overlay at top-right */
#hud-overlay {
    position: absolute;
    top: 12px;
    right: 28px;
    display: flex;
    flex-direction: column;
    pointer-events: none;
}

#hud-health,
#hud-points {
    display: grid;
    grid-template-columns: 28px 140px 28px;
    align-items: center;
    color: #eee;
    font-family: monospace;
    font-size: 12px;
    text-shadow: 0px 0px 2px #222;
}

.hud-bar {
    position: relative;
    grid-column: 2;
    width: 140px;
    height: 10px;
    background: rgba(0, 0, 0, 0.7);
    border: 2px solid #222;
}

.hud-bar > .fill {
    position: absolute;
    height: 100%;
    background: linear-gradient(to bottom, #3bd66f, #2aa84f);
    image-rendering: pixelated;
    transition: width 400ms cubic-bezier(0.175, 0.885, 0.32, 1.275);
}
.hud-val {
    width: 28px;
    text-align: right;
    font-family: monospace;
}

#hud-points .fill {
    background: linear-gradient(to bottom, #ffd54a, #f39c12);
}

/* Floating health/points bars for other players (piggy-backs on .player transform) */
.player {
    --hp: 0;
    --hp-max: 15; /* TODO: keep in sync with game max health */
    --pts: 0;
    --pts-max: 4095;
    position: absolute;
}

/* Hide floating bars for the local player if desired; toggle by adding .is-local in JS */
.player.is-local::before,
.player.is-local::after {
    display: none;
}

.player::before,
.player::after {
    content: "";
    position: absolute;
    left: 1px;
    width: 22px;
    border: 1px solid #111;
    background: rgba(0, 0, 0, 0.6);
    box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.1);
    z-index: 2;
}

/* Health bar (2px) */
.player::before {
    top: -1px;
    height: 2px;
    background:
        linear-gradient(
            to right,
            #ff5e5e calc((var(--hp, 0) / var(--hp-max, 1)) * 100%),
            transparent 0
        ),
        rgba(0, 0, 0, 0.6);
}

/* Points bar (2px) */
.player::after {
    top: 1px;
    height: 2px;
    background:
        linear-gradient(
            to right,
            #ffd54a calc((var(--pts, 0) / var(--pts-max, 1)) * 100%),
            transparent 0
        ),
        rgba(0, 0, 0, 0.6);
}
