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

body {
    background: #0d0d0d;
    color: #e0e0e0;
    font-family: 'Segoe UI', system-ui, sans-serif;
    height: 100dvh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* ── Floating HUD ────────────────────────────────────────────────────────── */

#hud {
    position: fixed;
    top: 0;
    left: 0;
    z-index: 50;
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 10px 12px;
}

#hud-details {
    min-width: 0;
    max-width: calc(100vw - 110px);
    max-height: 0;
    overflow: hidden;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.4s, max-height 0.4s, visibility 0s 0.4s;
}
#hud-details.visible {
    max-height: 4rem;
    opacity: 1;
    visibility: visible;
    transition: opacity 0.4s, max-height 0.4s, visibility 0s;
}

#controller-details {
    display: flex;
    flex-wrap: wrap;
    gap: 0 8px;
    color: #bbb;
    font-size: 0.7rem;
}

#controller-name {
    overflow-wrap: anywhere;
}

#controller-room {
    white-space: nowrap;
}

#settings-btn {
    background: none;
    border: none;
    color: #555;
    font-size: 1.2rem;
    cursor: pointer;
    padding: 4px 6px;
    border-radius: 6px;
    line-height: 1;
    transition: color 0.15s;
}
#settings-btn:hover { color: #aaa; }

#fullscreen-btn {
    background: none;
    border: none;
    color: #555;
    font-size: 1.6rem;
    cursor: pointer;
    padding: 4px 8px;
    border-radius: 6px;
    line-height: 1;
    transition: color 0.15s;
}
#fullscreen-btn:hover { color: #aaa; }

#status {
    background: none;
    border: 1px solid transparent;
    border-radius: 4px;
    padding: 3px;
    font-size: 1rem;
    line-height: 1;
    cursor: pointer;
    transition: color 0.3s, background 0.15s;
}
#status[aria-pressed="true"] { background: #303030; box-shadow: inset 0 1px 3px #000; }
#status:focus-visible { outline: 2px solid #aaa; }
#status.connected    { color: #4f8; }
#status.disconnected { color: #333; }
#status.connecting   { color: #fa4; }
#status.error        { color: #f44; }
#status.room-not-found { color: #f44; }
#status.room-orphaned { color: #fa4; }

#connect-btn {
    background: #1b1b2e;
    border: 1px solid #336;
    border-radius: 6px;
    color: #8af;
    font-size: 0.9rem;
    padding: 9px 14px;
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s;
}
#connect-btn:hover  { background: #252540; }
#connect-btn:active { background: #0e1a0e; border-color: #4a8; color: #4fa; }
#connect-btn:disabled { opacity: 0.45; cursor: not-allowed; }

#status-label {
    display: block;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 0.65rem;
    color: #555;
    font-family: monospace;
}

#conn-status {
    font-size: 0.7rem;
    color: #555;
    font-family: monospace;
    margin-bottom: 4px;
}

/* ── Settings pane ───────────────────────────────────────────────────────── */

#settings-pane {
    position: fixed;
    inset: 0;
    background: #0d0d0d;
    z-index: 100;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow-y: auto;
}
#settings-pane[hidden] { display: none; }

#settings-panel {
    display: flex;
    flex-direction: column;
    gap: 8px;
    width: min(340px, 88vw);
    background: #161616;
    border: 1px solid #2a2a2a;
    border-radius: 12px;
    padding: 24px;
    margin: 16px 0;
}
#settings-panel h2 {
    font-size: 0.75rem;
    color: #666;
    text-transform: uppercase;
    letter-spacing: 2px;
    margin-bottom: 4px;
}
#settings-panel label {
    font-size: 0.7rem;
    color: #666;
    text-transform: uppercase;
    letter-spacing: 1.5px;
    margin-top: 4px;
}
#settings-panel input {
    background: #0d0d0d;
    border: 1px solid #3a3a3a;
    border-radius: 6px;
    color: #fff;
    font-size: 0.95rem;
    padding: 7px 10px;
    outline: none;
}
#settings-panel input:focus {
    border-color: #4af;
    box-shadow: 0 0 0 2px rgba(68, 170, 255, 0.15);
}
#player-name-row, #room-id-row, #room-code-field {
    display: flex;
    gap: 8px;
}
#room-code-field { flex-direction: column; }
#room-code-field[hidden] { display: none; }
#player-name-row input, #room-id-row input {
    flex: 1;
    min-width: 0;
}
#clear-player-name-btn, #clear-room-id-btn {
    width: 36px;
    flex: none;
    background: #1b1b1b;
    border: 1px solid #3a3a3a;
    border-radius: 6px;
    color: #aaa;
    font-size: 1.2rem;
    cursor: pointer;
}
#clear-player-name-btn:hover, #clear-room-id-btn:hover { color: #fff; border-color: #4af; }
#port-https-row {
    display: flex;
    gap: 16px;
    align-items: flex-end;
}
#port-https-row > div {
    display: flex;
    flex-direction: column;
    gap: 8px;
}
#port-https-row > div:first-child {
    flex: 1;
}
#port-https-row input[type="checkbox"] {
    width: 20px;
    height: 20px;
    margin-bottom: 6px;
    cursor: pointer;
}
#ping-debug-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: 4px;
}
#ping-debug-row label {
    margin-top: 0;
}
#ping-debug-row input[type="checkbox"] {
    width: 20px;
    height: 20px;
    cursor: pointer;
}
#auto-connect-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: 4px;
}
#auto-connect-row label {
    margin-top: 0;
}
#auto-connect-row input[type="checkbox"] {
    width: 20px;
    height: 20px;
    cursor: pointer;
}
#room-id {
    text-align: center;
    letter-spacing: 4px;
    text-transform: uppercase;
    font-weight: bold;
}
#advanced-settings-btn {
    background: none;
    border: 1px solid #2a2a2a;
    border-radius: 6px;
    color: #555;
    font-size: 0.8rem;
    padding: 7px 10px;
    cursor: pointer;
    text-align: left;
    transition: color 0.15s, border-color 0.15s;
    margin-top: 4px;
}
#advanced-settings-btn:hover { color: #999; border-color: #444; }

/* ── Advanced settings pane ──────────────────────────────────────────────── */

#advanced-settings-pane {
    position: fixed;
    inset: 0;
    background: #0d0d0d;
    z-index: 100;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow-y: auto;
}
#advanced-settings-pane[hidden] { display: none; }

#advanced-settings-panel {
    display: flex;
    flex-direction: column;
    gap: 8px;
    width: min(340px, 88vw);
    background: #161616;
    border: 1px solid #2a2a2a;
    border-radius: 12px;
    padding: 24px;
    margin: 16px 0;
}
#advanced-settings-panel h2 {
    font-size: 0.75rem;
    color: #666;
    text-transform: uppercase;
    letter-spacing: 2px;
    margin-bottom: 4px;
}
#advanced-settings-panel label {
    font-size: 0.7rem;
    color: #666;
    text-transform: uppercase;
    letter-spacing: 1.5px;
    margin-top: 4px;
}
#advanced-settings-panel input {
    background: #0d0d0d;
    border: 1px solid #3a3a3a;
    border-radius: 6px;
    color: #fff;
    font-size: 0.95rem;
    padding: 7px 10px;
    outline: none;
}
#advanced-settings-panel input:focus {
    border-color: #4af;
    box-shadow: 0 0 0 2px rgba(68, 170, 255, 0.15);
}
#advanced-settings-panel select {
    background: #0d0d0d;
    border: 1px solid #3a3a3a;
    border-radius: 6px;
    color: #fff;
    font-size: 0.95rem;
    padding: 7px 10px;
    outline: none;
    cursor: pointer;
}
#advanced-settings-panel select:focus {
    border-color: #4af;
    box-shadow: 0 0 0 2px rgba(68, 170, 255, 0.15);
}
#advanced-settings-back-btn {
    background: none;
    border: 1px solid #2a2a2a;
    border-radius: 6px;
    color: #555;
    font-size: 0.8rem;
    padding: 7px 10px;
    cursor: pointer;
    text-align: left;
    transition: color 0.15s, border-color 0.15s;
    margin-top: 4px;
}
#advanced-settings-back-btn:hover { color: #999; border-color: #444; }

#settings-close-btn {
    margin-top: 8px;
    background: #1b1b2e;
    border: 1px solid #336;
    border-radius: 6px;
    color: #8af;
    font-size: 0.9rem;
    padding: 9px 14px;
    cursor: pointer;
    transition: background 0.15s;
}
#settings-close-btn:hover { background: #252540; }
#go-btn {
    margin-top: 8px;
    background: #236048;
    border: 1px solid #3a8a63;
    border-radius: 6px;
    color: #fff;
    font-size: 0.9rem;
    font-weight: bold;
    padding: 10px 14px;
    cursor: pointer;
}
#go-btn:disabled { opacity: 0.45; cursor: not-allowed; }
#go-btn:not(:disabled):hover { background: #307c5c; }

/* ── Main layout ─────────────────────────────────────────────────────────── */

main {
    --square-track: min(calc((100dvh - 32px) / 7), calc((100vw - clamp(48px, 15vmin, 64px) - 72px) / 14));
    flex: 1;
    display: flex;
    min-height: 0;
}

@media (orientation: portrait) {
    main {
        --square-track: min(calc((100vw - 24px) / 7), calc((100dvh - clamp(48px, 15vmin, 64px) - 120px) / 14));
        flex-direction: column;
        gap: 8px;
        padding: 8px 0;
    }
}

@media (orientation: portrait) and (max-height: 680px) {
    main {
        --square-track: min(calc((100vw - 24px) / 7), calc((100dvh - clamp(48px, 15vmin, 64px) - 112px) / 14));
    }
}

#joystick-zone,
#buttons-zone {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
}

#joystick-zone {
    padding: 0 12px;
}

#buttons-zone {
    padding: 16px;
}

@media (orientation: portrait) {
    #buttons-zone {
        padding: 8px 0;
    }
}

#control-zone {
    flex: 0 0 auto;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 0 8px;
}

@media (orientation: portrait) {
    #control-zone {
        flex-direction: row;
        padding: 8px 0;
    }
}

#vip-control-slot {
    display: grid;
    place-items: center;
    width: clamp(48px, 15vmin, 64px);
    height: clamp(48px, 15vmin, 64px);
}
#vip-control-slot > * {
    grid-area: 1 / 1;
}

#control-btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: clamp(48px, 15vmin, 64px);
    height: clamp(48px, 15vmin, 64px);
    padding: 4px;
    border-radius: 999px;
    border: 3px solid #d4af37;
    background: #d4af37;
    color: #1a1400;
    font-size: 0.85rem;
    font-weight: bold;
    cursor: pointer;
    transition: background 0.15s, color 0.15s;
}
#control-btn:hover {
    filter: brightness(1.1);
}
#control-btn.active {
    background: #0d0d0d;
    color: #d4af37;
}
/* Keeps the button occupying its layout space while hidden (no reflow of the
   joystick/buttons zones beside it) - see setTakeButtonVisible() in controller.js. */
#control-btn.take-hidden {
    visibility: hidden;
}

#vip-badge {
    display: flex;
    align-items: center;
    justify-content: center;
    width: clamp(48px, 15vmin, 64px);
    height: clamp(48px, 15vmin, 64px);
    border: 3px solid #d4af37;
    border-radius: 4px;
    background: #d4af37;
    color: #1a1400;
    font-weight: bold;
}
#vip-badge[hidden] {
    display: none;
}

#pause-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: clamp(48px, 15vmin, 64px);
    height: clamp(48px, 15vmin, 64px);
    padding: 0;
    border-radius: 999px;
    border: 3px solid #4a4a4a;
    background: radial-gradient(circle at 35% 30%, #3a3a3a, #1c1c1c 70%);
    box-shadow: inset 0 1px 2px rgba(255, 255, 255, 0.08), 0 2px 6px rgba(0, 0, 0, 0.5);
    cursor: pointer;
    transition: filter 0.06s, transform 0.06s, border-color 0.15s;
}
#pause-btn:hover {
    border-color: #6a6a6a;
}
#pause-btn:active,
#pause-btn.pressed {
    transform: scale(0.94);
    filter: brightness(1.4);
}
/* Keeps the button occupying its layout space while hidden (no reflow of the
   control zone beside it) - see setPauseButtonVisible() in controller.js. */
#pause-btn.pause-hidden {
    visibility: hidden;
}

#pause-icon {
    display: flex;
    gap: 6px;
}
.pause-bar {
    width: 7px;
    height: 22px;
    border-radius: 3px;
    background: #bbbbbb;
    box-shadow: inset 0 1px 1px rgba(255, 255, 255, 0.2);
}

/* ── Joystick ────────────────────────────────────────────────────────────── */

#joystick-base {
    --joystick-tracks: 5;
    width: calc(var(--joystick-tracks) * var(--square-track));
    height: calc(var(--joystick-tracks) * var(--square-track));
    border-radius: 50%;
    background: radial-gradient(circle at 50% 50%, #252525, #141414);
    border: 2px solid #383838;
    box-shadow: 0 0 30px rgba(0, 0, 0, 0.6), inset 0 1px 3px rgba(255, 255, 255, 0.04);
    position: relative;
    touch-action: none;
    cursor: grab;
}

@media (orientation: portrait) and (max-height: 680px) {
    #joystick-zone {
        align-items: flex-end;
        padding-top: 48px;
    }
}

@media (orientation: portrait) and (min-height: 681px) {
    #joystick-zone { padding-top: 56px; }
}

#joystick-base::after {
    content: '';
    position: absolute;
    inset: 12%;
    border-radius: 50%;
    border: 1px solid #2a2a2a;
    pointer-events: none;
}

#joystick-base.active {
    cursor: grabbing;
}

#joystick-thumb {
    width: 42%;
    height: 42%;
    border-radius: 50%;
    background: radial-gradient(circle at 38% 32%, #4a4a4a, #1c1c1c);
    border: 2px solid #555;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 1;
    box-shadow: 0 4px 14px rgba(0, 0, 0, 0.7);
    pointer-events: none;
    transition: background 0.12s, border-color 0.12s;
}

#joystick-base.active #joystick-thumb {
    background: radial-gradient(circle at 38% 32%, cyan, #2233aa);
    border-color: cyan;
    box-shadow: 0 4px 14px rgba(80, 100, 255, 0.4);
}

/* ── Action buttons (diamond layout) ────────────────────────────────────── */

.buttons-grid {
    --button-track: var(--square-track);
    display: grid;
    grid-template-columns: repeat(7, var(--button-track));
    grid-template-rows: repeat(7, var(--button-track));
    gap: 0;
    touch-action: none;
}
.buttons-grid[hidden] { display: none; }

#btn-n {
    grid-column: 3 / span 3;
    grid-row: 1 / span 3;
}
#btn-w {
    grid-column: 1 / span 3;
    grid-row: 3 / span 3;
}
#btn-e {
    grid-column: 5 / span 3;
    grid-row: 3 / span 3;
}
#btn-s {
    grid-column: 3 / span 3;
    grid-row: 5 / span 3;
}

.action-btn {
    background: #141414;
    font-size: clamp(32px, 20vmin, 85px);
    font-weight: bold;
    cursor: pointer;
    touch-action: none;
    transition: transform 0.06s, filter 0.06s;
    border: 0px;
    transform: rotate(45deg);
    display: flex;
    align-items: center;
    justify-content: center;
}

.action-btn span {
    display: block;
    transform: rotate(-45deg);
}

.action-btn.pressed,
.action-btn:active {
    transform: rotate(45deg);
    filter: brightness(1.8);
}

#btn-n {
    color: #ffb060;
    background: #cc7730;
}
#btn-w {
    color: #7bb8ff;
    background: #4488cc;
}
#btn-e {
    color: #ff8080;
    background: #cc4444;
}
#btn-s {
    color: #70e070;
    background: #3aaa3a;
}

#btn-single {
    grid-column: 1 / span 7;
    grid-row: 1 / span 7;
    border-radius: 24px;
    transform: none;
    color: #70e070;
    background: #3aaa3a;
}
#btn-single span {
    transform: none;
}
#btn-single.pressed,
#btn-single:active {
    transform: none;
    filter: brightness(1.8);
}

#buttons-grid-two {
    --button-track: min(var(--square-track), calc((100vw - clamp(48px, 15vmin, 64px) - 72px) / 20));
    grid-template-columns: repeat(2, calc(5 * var(--button-track)));
    grid-template-rows: calc(7 * var(--button-track));
}
#btn-a {
    grid-column: 1;
    grid-row: 1;
    border-radius: 24px 0 0 24px;
    transform: none;
    color: #7bb8ff;
    background: #4488cc;
}
#btn-b {
    grid-column: 2;
    grid-row: 1;
    border-radius: 0 24px 24px 0;
    transform: none;
    color: #ff8080;
    background: #cc4444;
}
#btn-a span,
#btn-b span {
    transform: none;
}
#btn-a.pressed,
#btn-a:active,
#btn-b.pressed,
#btn-b:active {
    transform: none;
    filter: brightness(1.8);
}
@media (orientation: portrait) {
    #buttons-grid-two {
        --button-track: min(var(--square-track), calc((100dvh - clamp(48px, 15vmin, 64px) - 120px - 7 * var(--square-track)) / 10));
        grid-template-columns: calc(7 * var(--button-track));
        grid-template-rows: repeat(2, calc(5 * var(--button-track)));
    }
    #btn-a {
        grid-column: 1;
        grid-row: 1;
        border-radius: 24px 24px 0 0;
    }
    #btn-b {
        grid-column: 1;
        grid-row: 2;
        border-radius: 0 0 24px 24px;
    }
}

@media (orientation: portrait) and (max-height: 680px) {
    #buttons-grid-two {
        --button-track: min(var(--square-track), calc((100dvh - clamp(48px, 15vmin, 64px) - 112px - 7 * var(--square-track)) / 10));
    }
}

/* ── Action buttons (arrows / D-pad layout) ─────────────────────────────── */

#buttons-grid-arrows #btn-up {
    grid-column: 3 / span 3;
    grid-row: 1 / span 3;
}
#buttons-grid-arrows #btn-left {
    grid-column: 1 / span 3;
    grid-row: 3 / span 3;
}
#buttons-grid-arrows #btn-right {
    grid-column: 5 / span 3;
    grid-row: 3 / span 3;
}
#buttons-grid-arrows #btn-down {
    grid-column: 3 / span 3;
    grid-row: 5 / span 3;
}
#btn-up {
    color: #ffb060;
    background: #cc7730;
}
#btn-left {
    color: #7bb8ff;
    background: #4488cc;
}
#btn-right {
    color: #ff8080;
    background: #cc4444;
}
#btn-down {
    color: #70e070;
    background: #3aaa3a;
}

/* CSS-drawn triangles instead of Unicode arrow glyphs: font fallback for the
   arrow characters differs across browsers/platforms (seen as mis-centered,
   undersized arrows in Chrome/Linux vs Firefox/Android), so render shapes
   that aren't subject to font-metric differences. */
#buttons-grid-arrows .action-btn span {
    width: 46px;
    height: 46px;
    background: currentColor;
}
#buttons-grid-arrows .arrow-up {
    clip-path: polygon(50% 0%, 100% 100%, 0% 100%);
}
#buttons-grid-arrows .arrow-down {
    clip-path: polygon(50% 100%, 0% 0%, 100% 0%);
}
#buttons-grid-arrows .arrow-left {
    clip-path: polygon(0% 50%, 100% 0%, 100% 100%);
}
#buttons-grid-arrows .arrow-right {
    clip-path: polygon(100% 50%, 0% 0%, 0% 100%);
}

/* ── Ping graph ──────────────────────────────────────────────────────────── */

#ping-graph {
    position: fixed;
    top: 0;
    right: 0;
    z-index: 50;
    pointer-events: none;
}
