:root {
    --color-navy: #1E3A5F;
    --color-navy-dark: #152942;
    --color-navy-light: #2A4A6F;
    --color-gold: #E8A525;
    --color-gold-light: #F5C04A;
    --color-gold-dark: #C98F1D;
    --bg-primary: #0D1B2A;
    --bg-secondary: #1B2838;
    --text-primary: #FFFFFF;
    --text-secondary: #A0B4C8;
    --text-muted: #6B8299;
    --border-color: #2A4A6F;
    --focus-ring: var(--color-gold);
    --focus-glow: rgba(232, 165, 37, 0.4);
}

[data-theme="light"] {
    --bg-primary: #F5F7FA;
    --bg-secondary: #FFFFFF;
    --text-primary: #1E3A5F;
    --text-secondary: #4A6580;
    --text-muted: #7A8FA0;
    --border-color: #C8D4E0;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    margin: 0;
    overflow: hidden;
    background: var(--bg-primary);
    font-family: 'Outfit', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    color: var(--text-primary);
}

/* Unity Container */
#unity-container {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    overflow: hidden;
}

#unity-canvas {
    width: 100%;
    height: 100%;
    display: block;
    background: var(--color-navy-dark);
}

#unity-loading-bar {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    display: none;
    text-align: center;
}

.loading-label {
    color: var(--text-secondary);
    font-size: 0.875rem;
    margin-bottom: 12px;
}

#unity-progress-bar-empty {
    width: 240px;
    height: 8px;
    background: var(--bg-secondary);
    border-radius: 4px;
    overflow: hidden;
    border: 1px solid var(--border-color);
}

#unity-progress-bar-full {
    width: 0%;
    height: 100%;
    background: linear-gradient(90deg, var(--color-gold-dark), var(--color-gold));
    transition: width 0.15s ease-out;
}

/* ================================================================
   Game Menu Overlay
   ================================================================ */
#game-menu {
    position: fixed;
    inset: 0;
    z-index: 500;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.3s ease, visibility 0.3s ease;
}

#game-menu.visible {
    opacity: 1;
    visibility: visible;
}

.menu-backdrop {
    position: absolute;
    inset: 0;
    /* No backdrop blur: blurring a live WebGL feed saturates low-power GPUs
       (Pi-class consoles drop from 60 to ~32 fps). A near-opaque tint reads
       the same and composites for free. */
    background: rgba(13, 27, 42, 0.96);
}

.menu-card {
    position: relative;
    z-index: 1;
    background: linear-gradient(
        165deg,
        rgba(30, 58, 95, 0.65) 0%,
        rgba(27, 40, 56, 0.8) 100%
    );
    border: 1px solid rgba(232, 165, 37, 0.15);
    border-radius: 20px;
    padding: 40px 48px 32px;
    min-width: 400px;
    max-width: 520px;
    box-shadow:
        0 24px 80px rgba(0, 0, 0, 0.5),
        0 0 0 1px rgba(255, 255, 255, 0.04) inset,
        0 1px 0 rgba(255, 255, 255, 0.06) inset;
    transform: translateY(20px);
    transition: transform 0.35s cubic-bezier(0.16, 1, 0.3, 1);
}

#game-menu.visible .menu-card {
    transform: translateY(0);
}

/* Menu Logo */
.menu-logo {
    text-align: center;
    margin-bottom: 28px;
    font-size: 1.5rem;
    font-weight: 700;
    letter-spacing: 0.08em;
    user-select: none;
}

.menu-logo .logo-accent {
    color: var(--color-gold);
}

/* Menu Body — QR + Slots side by side */
.menu-body {
    display: flex;
    gap: 28px;
    align-items: center;
    justify-content: center;
    margin-bottom: 28px;
}

/* Connect prompt (shown in the waiting state when no controller is attached) */
.menu-connect-prompt {
    display: none;
    flex-direction: column;
    align-items: center;
    gap: 14px;
    flex-shrink: 0;
}

.menu-connect-glyph {
    width: 56px;
    height: 56px;
    color: var(--color-gold);
}

.menu-connect-text {
    font-size: 0.95rem;
    color: var(--text-secondary);
    font-weight: 500;
    letter-spacing: 0.02em;
    text-align: center;
}

/* Phone-controller QR — revealed only when the console passed a ?pair= URL.
   Hidden by default so off-console pages (no pairing URL) look unchanged.
   Shown beside the slots in the open menu and below the prompt while waiting. */
.menu-qr {
    display: none;
    flex-direction: column;
    align-items: center;
    gap: 12px;
    flex-shrink: 0;
}

#game-menu.has-pair-qr .menu-qr {
    display: flex;
}

.menu-qr-canvas {
    background: #FFFFFF;
    padding: 10px;
    border-radius: 12px;
    line-height: 0;
}

.menu-qr-cap {
    display: flex;
    flex-direction: column;
    gap: 3px;
    font-size: 0.8rem;
    color: var(--text-muted);
    text-align: center;
    letter-spacing: 0.02em;
}

.menu-qr-cap strong {
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--text-secondary);
}

/* Waiting state — show the connect prompt, hide menu items */
#game-menu.waiting .menu-connect-prompt {
    display: flex;
}

#game-menu.waiting .menu-body {
    flex-direction: column;
}

#game-menu.waiting .menu-slots-section {
    display: none;
}

#game-menu.waiting .menu-items {
    display: none;
}

#game-menu.waiting .menu-control-hints {
    display: none;
}

/* Slot Badges */
.menu-slots-section {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
}

.menu-slots-label {
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--text-muted);
}

.menu-slots-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 6px;
}

.menu-slot-badge {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 38px;
    height: 38px;
    border-radius: 8px;
    font-size: 0.75rem;
    font-weight: 700;
    transition: all 0.25s ease;
    border: 2px solid transparent;
}

.menu-slot-badge.empty {
    background: rgba(30, 58, 95, 0.4);
    border-color: rgba(42, 74, 111, 0.4);
    color: rgba(107, 130, 153, 0.5);
}

.menu-slot-badge.connected {
    background: var(--color-navy);
    border-color: var(--color-navy-light);
    color: var(--text-primary);
}

.menu-slot-badge.connected.p1 {
    background: var(--color-gold-dark);
    border-color: var(--color-gold);
    color: var(--bg-primary);
    box-shadow: 0 0 12px var(--focus-glow);
}

.menu-slot-badge.disconnected {
    background: var(--color-navy);
    border-color: var(--color-navy-light);
    color: var(--text-primary);
    animation: slotPulse 1.5s ease-in-out infinite;
}

@keyframes slotPulse {
    0%, 100% { opacity: 0.7; }
    50% { opacity: 0.35; }
}

/* Menu Items (Continue / Exit) */
.menu-items {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-bottom: 24px;
}

.menu-item {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 14px 24px;
    background: rgba(30, 58, 95, 0.5);
    border: 2px solid rgba(42, 74, 111, 0.6);
    border-radius: 12px;
    font-family: inherit;
    font-size: 1rem;
    font-weight: 600;
    color: var(--text-primary);
    cursor: pointer;
    transition: all 0.15s ease;
    outline: none;
    user-select: none;
}

.menu-item:hover {
    background: rgba(42, 74, 111, 0.6);
    border-color: var(--text-muted);
}

.menu-item.focused {
    border-color: var(--color-gold);
    box-shadow: 0 0 0 4px var(--focus-glow);
    background: rgba(42, 74, 111, 0.65);
    animation: menuFocusPulse 2s ease-in-out infinite;
}

@keyframes menuFocusPulse {
    0%, 100% { box-shadow: 0 0 0 4px var(--focus-glow); }
    50% { box-shadow: 0 0 0 8px var(--focus-glow); }
}

.menu-item.exit-item {
    color: var(--text-secondary);
    font-weight: 500;
}

/* Control Hints */
.menu-control-hints {
    display: flex;
    justify-content: center;
    gap: 24px;
    padding-top: 16px;
    border-top: 1px solid rgba(42, 74, 111, 0.4);
}

.menu-hint {
    display: flex;
    align-items: center;
    gap: 6px;
}

.menu-hint-key {
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 28px;
    height: 28px;
    padding: 0 6px;
    background: rgba(30, 58, 95, 0.6);
    border: 1px solid rgba(42, 74, 111, 0.6);
    border-radius: 5px;
    font-size: 0.75rem;
    font-weight: 700;
    color: var(--color-gold);
}

.menu-hint-key-dpad {
    position: relative;
    width: 28px;
}

.menu-hint-key-dpad::before {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 14px;
    height: 14px;
    background: var(--color-gold);
    clip-path: polygon(
        50% 0%, 60% 35%, 100% 35%, 100% 65%, 60% 65%, 50% 100%,
        40% 65%, 0% 65%, 0% 35%, 40% 35%
    );
}

.menu-hint-action {
    color: var(--text-muted);
    font-size: 0.75rem;
    font-weight: 500;
}

/* Face-button position diamond (brand-agnostic controller hint) */
.fbtn {
    width: 26px;
    height: 26px;
    flex-shrink: 0;
    color: var(--text-muted);
}
.fbtn circle { fill: none; stroke: currentColor; stroke-width: 1.8; opacity: 0.6; }
.fbtn-confirm .fb-d,
.fbtn-back .fb-r { fill: var(--color-gold); stroke: var(--color-gold); opacity: 1; }

/* Input Debug Panel — hidden by default */
#input-debug {
    display: none;
    position: fixed;
    bottom: 24px;
    left: 24px;
    background: var(--bg-secondary);
    border: 2px solid var(--border-color);
    padding: 12px 16px;
    border-radius: 10px;
    font-family: 'Courier New', monospace;
    font-size: 0.75rem;
    min-width: 180px;
    max-height: 140px;
    overflow-y: auto;
    z-index: 600;
}

.debug-enabled #input-debug {
    display: block;
}

#input-debug::before {
    content: 'Input Log';
    display: block;
    color: var(--text-muted);
    font-family: 'Outfit', sans-serif;
    font-weight: 600;
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-bottom: 8px;
    padding-bottom: 6px;
    border-bottom: 1px solid var(--border-color);
}

#input-log div {
    padding: 2px 0;
    color: var(--color-gold);
}
