/* ============================================================
   DESIGN TOKENS
   ============================================================ */
:root {
    --mx-bg:             #000;
    --mx-panel:          #111;
    --mx-neon-pink:      #ff00ff;
    --mx-neon-blue:      #00ffff;
    --mx-magbei-fuchsia: #B700B7;
    --mx-modal-bg:       #212233;
    --mx-text:           #eee;
    --mx-dim:            #777;
    --mx-font-ui:        -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    --mx-font-data:      'Courier New', Courier, monospace;
    --mx-neon-active:    #ffaa00;
}

/* ============================================================
   CLOAKING — hides wrapper until JS removes class
   ============================================================ */
.magbei-arcade-wrapper.mx-pre-cloak {
    visibility: hidden !important;
    opacity: 0 !important;
}

/* ============================================================
   WRAPPER
   flex column: screen-container grows, toolbar is fixed-height.
   No position hack — layout is self-contained.
   ============================================================ */
.magbei-arcade-wrapper {
    display: flex;
    flex-direction: column;
    position: relative;
    width: 100%;
    margin: 20px 0;
    background: #000;
    border: 1px solid #333;
    border-radius: 12px;
    overflow: hidden;
    box-shadow: 0 10px 30px rgba(0,0,0,.5);
    box-sizing: border-box;
    transition: opacity .4s ease-out;
    opacity: 1;
    visibility: visible;
    /*
     * min-height fallback — defensive guard.
     * aspect-ratio: 16/9 is the primary height source on desktop, but it
     * is silently overridden by some themes that set height:auto !important
     * on all elements. Without a min-height the wrapper collapses to 0px,
     * the screen-container (flex:1 1 0) collapses with it, and the iframe
     * becomes invisible below the always-opaque curtain (::after, z-index 15).
     * 350px is the minimum playable height — tall enough to see the game
     * and the toolbar, short enough to never create unwanted whitespace.
     * On all standard breakpoints the aspect-ratio or explicit height rules
     * override this value, so visual output is unchanged in normal cases.
     */
    min-height: 350px;
}

/* ============================================================
   SCREEN CONTAINER
   flex: 1 1 0  — grows to fill all space above the toolbar.
   min-height: 0 — required so flex child doesn't overflow
                   when iframe is absolutely positioned inside.
   ============================================================ */
.mx-screen-container {
    position: relative;
    flex: 1 1 0;
    min-height: 0;
    background: #000;
}

/* Curtain — black overlay that fades out when game loads */
.mx-screen-container::after {
    content: "";
    position: absolute;
    inset: 0;
    background: #000;
    z-index: 15;
    transition: opacity .5s ease-out;
    opacity: 1;
    pointer-events: none;
}
.mx-screen-container.mx-loaded::after { opacity: 0; }

/* iframe / embed fill the container absolutely */
.mx-screen-container iframe,
.mx-screen-container embed {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    border: none;
    display: block;
    background: #000;
    overflow: hidden; /* clips the outer CSS box only — does NOT suppress scrollbars
                         inside the embedded document (separate browsing context).
                         scrolling="no" on the <iframe> element handles that — see renderer.php. */
    z-index: 10;
}

/* ============================================================
   DESKTOP — 16:9 aspect ratio.
   Guarded by pointer:fine AND absence of data-mx-touch so
   iPads with Magic Keyboard get touch layout instead.
   ============================================================ */
@media (min-width: 769px) and (hover: hover) and (pointer: fine) {
    html:not([data-mx-touch]) .magbei-arcade-wrapper {
        aspect-ratio: 16 / 9;
    }
}

/* ============================================================
   TOOLBAR
   ============================================================ */
.mx-toolbar {
    height: 60px;
    flex-shrink: 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 15px;
    background: var(--mx-panel);
    box-sizing: border-box;
    position: relative;
    z-index: 20;
}
.mx-toolbar::before {
    content: "";
    position: absolute;
    top: 0; left: 0; right: 0;
    height: 1px;
    background: linear-gradient(90deg, var(--mx-neon-pink), var(--mx-neon-blue));
    box-shadow: 0 0 10px var(--mx-neon-pink);
    z-index: 21;
}

.mx-section               { display: flex; align-items: center; gap: 12px; }
.mx-section.section-vote  { gap: 10px; }

/* ============================================================
   BUTTONS
   ============================================================ */
.mx-btn {
    background: #222 !important;
    border: none !important;
    color: #fff !important;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0 15px;
    height: 40px;
    border-radius: 8px;
    transition: all .2s ease;
    font-family: var(--mx-font-ui);
    font-size: 16px;
    line-height: 1;
    gap: 6px;
    outline: none !important;
}
.mx-btn:hover { background: #333 !important; }
.mx-btn.active span { filter: drop-shadow(0 0 8px var(--mx-neon-active)) !important; }
.mx-btn.active      { color: var(--mx-neon-blue) !important; }
.mx-btn svg {
    width: 22px; height: 22px;
    stroke: currentColor !important;
    display: block;
    stroke-width: 1.8px;
    fill: none;
}
.mx-data-text {
    font-family: var(--mx-font-ui);
    font-size: 13px !important;
    font-weight: 700 !important;
    color: #fff;
    margin-left: 4px;
}

/* ============================================================
   STATS BAR
   ============================================================ */
.mx-bar-wrapper  { display: flex; align-items: center; gap: 8px; }
.mx-visual-bar   { width: 70px; height: 4px; background: #333; border-radius: 2px; overflow: hidden; position: relative; }
.mx-bar-fill     { height: 100%; background: linear-gradient(90deg, var(--mx-neon-blue), var(--mx-neon-pink)); transition: width .4s ease; }
.mx-percent-text { font-family: var(--mx-font-data); font-size: 14px; font-weight: bold; color: #ccc; min-width: 35px; }
.mx-separator    { width: 1px; height: 20px; background: #333; margin: 0 5px; }

/* ============================================================
   MODALS
   ============================================================ */
.mx-modal-overlay {
    position: absolute;
    inset: 0;
    background: rgba(0,0,0,.92);
    z-index: 50;
    display: none;
    align-items: center;
    justify-content: center;
    padding: 20px;
    backdrop-filter: blur(4px);
    /* Fade: JS sets display:flex then adds .mx-modal-open */
    opacity: 0;
    transition: opacity .2s ease;
}
.mx-modal-overlay.mx-modal-open { opacity: 1; }

.mx-modal-content {
    background: var(--mx-modal-bg);
    width: 100%;
    max-width: 480px;
    /*
     * height: clamp(min, preferred, max)
     *   min  280px  — never shorter than this even on tiny containers
     *   72%  of the overlay = ~2.2× the old collapsed content height;
     *        fills the screen container generously on every device
     *   max  calc(100% - 40px) — overlay padding (20px × 2); all 4
     *        corners always visible, never bleeds outside the iframe
     */
    height: clamp(280px, 72%, calc(100% - 40px));
    max-height: calc(100% - 40px);
    border-radius: 12px;
    position: relative;
    box-shadow: 0 0 30px rgba(0,0,0,.6);
    /* flex column: close button row + scrollable body */
    display: flex;
    flex-direction: column;
    overflow: hidden;
}
.mx-modal-close {
    position: absolute;
    top: 10px; right: 18px;
    width: 44px; height: 44px;
    background: transparent !important;
    border: none;
    color: #fff;
    font-size: 22px;
    cursor: pointer;
    z-index: 100;
    transition: color .2s ease, transform .2s ease;
}
.mx-modal-close:hover { color: var(--mx-neon-pink); transform: scale(1.1); }
.mx-modal-body {
    padding: 45px 30px 30px;
    /* flex: 1 1 0 + min-height: 0 — allows the body to shrink and scroll
       within the flex-column .mx-modal-content instead of a fixed px cap. */
    flex: 1 1 0;
    min-height: 0;
    overflow-y: auto;
    /* Firefox scrollbar */
    scrollbar-width: thin;
    scrollbar-color: var(--mx-magbei-fuchsia) #0d0d14;
}

/* Webkit scrollbar — dark track in harmony with modal background */
.mx-modal-body::-webkit-scrollbar         { width: 6px; }
.mx-modal-body::-webkit-scrollbar-track   { background: #0d0d14; border-radius: 3px; }
.mx-modal-body::-webkit-scrollbar-thumb   { background: var(--mx-magbei-fuchsia); border-radius: 3px; transition: background .2s; }
.mx-modal-body::-webkit-scrollbar-thumb:hover  { background: var(--mx-neon-pink); }
/* Neon glow activates only while dragging — subtle, not invasive */
.mx-modal-body::-webkit-scrollbar-thumb:active {
    background: var(--mx-neon-pink);
    box-shadow: 0 0 6px 1px var(--mx-neon-pink);
}
.mx-ctrl-group h3 { color: #fff; text-transform: uppercase; font-size: 13px; margin: 0 0 15px 0; letter-spacing: 2px; }

/*
 * .mx-ctrl-text — container for the controls description inside the modal.
 *
 * color: #fff — mandatory: modal background is #212233 (dark) but inherits
 * the theme's body color (dark). Without explicit white, plain descriptive
 * text is invisible.
 *
 * line-height: 1.5 — UX standard (GitHub, MDN, Steam). 1.9 was too tall:
 * at 13px font-size it produced ~24.7px between baselines, making each
 * keyboard row look like a paragraph break rather than a list item.
 * 1.5 = 19.5px baseline-to-baseline — comfortable for small text + kbd keys.
 *
 * white-space: normal — nl2br() already converts \n → <br> server-side.
 * pre-line was doubling blank separator lines from the ACF field:
 * a blank line in the ACF textarea became \n → <br>, then pre-line rendered
 * the raw \n again, producing two full line-height gaps per blank line.
 * normal suppresses that double-rendering.
 */
.mx-ctrl-text {
    color: #fff;
    font-size: 13px;
    line-height: 1.5;
    white-space: normal;
}

kbd {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background-color: #1a1a1a;
    border: 1px solid #333;
    border-radius: 4px;
    border-bottom: 3px solid var(--mx-magbei-fuchsia);
    color: #fff;
    font-family: var(--mx-font-data);
    font-size: 11px;
    font-weight: bold;
    padding: 2px 8px;
    margin: 2px;
    text-transform: uppercase;
}
.mx-group-pc kbd     { border-bottom-color: var(--mx-magbei-fuchsia) !important; }
.mx-group-mobile kbd { border-bottom-color: var(--mx-neon-blue) !important; }

/* ============================================================
   FULLSCREEN
   ============================================================ */
.magbei-arcade-wrapper.is-fullscreen-mode .mx-toolbar,
.magbei-arcade-wrapper:fullscreen .mx-toolbar,
.magbei-arcade-wrapper:-webkit-full-screen .mx-toolbar {
    display: none !important;
    height: 0 !important;
    padding: 0 !important;
}
.magbei-arcade-wrapper:fullscreen,
.magbei-arcade-wrapper:-webkit-full-screen,
.magbei-arcade-wrapper.is-fullscreen-mode {
    width: 100vw !important;
    height: 100vh !important;
    border: none !important;
    border-radius: 0 !important;
    margin: 0 !important;
    background: #000 !important;
}
.magbei-arcade-wrapper:fullscreen .mx-screen-container,
.magbei-arcade-wrapper:-webkit-full-screen .mx-screen-container,
.magbei-arcade-wrapper.is-fullscreen-mode .mx-screen-container {
    flex: 1 1 0;
    min-height: 0;
}

/* ============================================================
   CONTEXT A — PHONE PORTRAIT (≤768px)
   Pure width breakpoint. svh keeps height stable while Safari
   shows/hides the chrome bar. Unchanged from v8.6.7.
   ============================================================ */
@media (max-width: 768px) {
    .magbei-arcade-wrapper {
        /* Full-width breakout: CSS best-effort.
           JS (applyMobileBreakout) overrides with pixel-exact
           getBoundingClientRect() measurement. */
        width: 100vw;
        max-width: 100vw;
        margin-left: calc(50% - 50vw);
        margin-right: 0;
        margin-top: 10px;
        margin-bottom: 30px;
        /* svh = smallest viewport height — constant, immune to
           Safari chrome bar show/hide during scroll. */
        height: 70vh;        /* fallback for browsers without svh */
        height: 70svh;
        min-height: 480px;
        border-radius: 0;
        border: none;
        border-top: 1px solid #333;
        border-bottom: 1px solid #333;
        overscroll-behavior: contain;
    }
    .mx-toolbar {
        height: 65px;
        justify-content: space-evenly;
        padding: 0 8px;
        gap: 0;
    }
    .mx-toolbar .mx-section,
    .mx-toolbar .mx-section.section-vote { display: contents; }
    .mx-toolbar .mx-btn { font-size: 18px; height: 44px; padding: 0 12px; }
    .mx-bar-wrapper { gap: 6px; }
    .mx-visual-bar  { width: 50px; }
}

/* ============================================================
   CONTEXT B — TOUCH TABLETS + PHONES, PORTRAIT (≥769px)
   aspect-ratio: 4/3 gives a generous game area on portrait
   tablets (iPad mini → iPad Pro). Phone landscape width is
   ≥769px so it falls here only in portrait — but phones in
   portrait are ≤768px (Context A), so this only catches
   tablets in portrait orientation in practice.
   Example: iPad mini portrait (768×1024 viewport) →
     wrapper width = 768px → height = 768 * 3/4 = 576px total
     game area = 576 − 65px toolbar = 511px ✓
   ============================================================ */
@media (hover: none) and (pointer: coarse) and (min-width: 769px) and (orientation: portrait) {
    .magbei-arcade-wrapper {
        aspect-ratio: 4 / 3;
        width: 100vw;
        max-width: 100vw;
        margin-left: calc(50% - 50vw);
        margin-right: 0;
        margin-top: 10px;
        margin-bottom: 30px;
        border-radius: 12px;
        border: none;
        border-top: 1px solid #333;
        border-bottom: 1px solid #333;
        overscroll-behavior: contain;
    }
    .mx-toolbar {
        height: 65px;
        justify-content: space-evenly;
        padding: 0 8px;
        gap: 0;
    }
    .mx-toolbar .mx-section,
    .mx-toolbar .mx-section.section-vote { display: contents; }
    .mx-toolbar .mx-btn { font-size: 18px; height: 44px; padding: 0 12px; }
    .mx-bar-wrapper { gap: 6px; }
    .mx-visual-bar  { width: 50px; }
}

/* ============================================================
   CONTEXT B — TOUCH TABLETS + PHONES, LANDSCAPE (≥769px)
   aspect-ratio: 16/9 matches the natural game ratio and fills
   landscape screens perfectly without any fixed height.
   Because height derives from width, Safari chrome bar
   show/hide (height-only change) never affects the layout —
   zoom-on-scroll is structurally impossible.
   Example: iPhone 14 Pro landscape (852px wide) →
     wrapper width = 852px → height = 852 * 9/16 = 479px total
     game area = 479 − 65px toolbar = 414px ✓
   Example: iPad mini landscape (1024px wide) →
     wrapper width = 1024px → height = 576px total
     game area = 576 − 65px toolbar = 511px ✓
   ============================================================ */
@media (hover: none) and (pointer: coarse) and (min-width: 769px) and (orientation: landscape) {
    .magbei-arcade-wrapper {
        aspect-ratio: 16 / 9;
        width: 100vw;
        max-width: 100vw;
        margin-left: calc(50% - 50vw);
        margin-right: 0;
        margin-top: 0;
        margin-bottom: 20px;
        border-radius: 12px;
        border: none;
        border-top: 1px solid #333;
        border-bottom: 1px solid #333;
        overscroll-behavior: contain;
    }
    .mx-toolbar {
        height: 65px;
        justify-content: space-evenly;
        padding: 0 8px;
        gap: 0;
    }
    .mx-toolbar .mx-section,
    .mx-toolbar .mx-section.section-vote { display: contents; }
    .mx-toolbar .mx-btn { font-size: 18px; height: 44px; padding: 0 12px; }
    .mx-bar-wrapper { gap: 6px; }
    .mx-visual-bar  { width: 50px; }
}

/* ============================================================
   CONTEXT C — iPAD + MAGIC KEYBOARD / EXTERNAL TRACKPAD
   These report hover:hover + pointer:fine (desktop-like) but
   are touch-capable. JS sets [data-mx-touch] on <html>.
   CSS orientation media queries work here normally.
   ============================================================ */

/* Context C base — full-width breakout shared by both orientations */
html[data-mx-touch] .magbei-arcade-wrapper {
    width: 100vw;
    max-width: 100vw;
    margin-left: calc(50% - 50vw);
    margin-right: 0;
    border-radius: 12px;
    border: none;
    border-top: 1px solid #333;
    border-bottom: 1px solid #333;
    overscroll-behavior: contain;
}
html[data-mx-touch] .mx-toolbar {
    height: 65px;
    justify-content: space-evenly;
    padding: 0 8px;
    gap: 0;
}
html[data-mx-touch] .mx-toolbar .mx-section,
html[data-mx-touch] .mx-toolbar .mx-section.section-vote { display: contents; }
html[data-mx-touch] .mx-toolbar .mx-btn { font-size: 18px; height: 44px; padding: 0 12px; }
html[data-mx-touch] .mx-bar-wrapper { gap: 6px; }
html[data-mx-touch] .mx-visual-bar  { width: 50px; }

/* Context C portrait — 4/3 same as tablet portrait */
@media (orientation: portrait) {
    html[data-mx-touch] .magbei-arcade-wrapper {
        aspect-ratio: 4 / 3;
        margin-top: 10px;
        margin-bottom: 30px;
    }
}

/* Context C landscape — 16/9 same as tablet landscape */
@media (orientation: landscape) {
    html[data-mx-touch] .magbei-arcade-wrapper {
        aspect-ratio: 16 / 9;
        margin-top: 0;
        margin-bottom: 20px;
    }
}

/* ============================================================
   UTILITY — visibility classes (set per-device from admin)
   ============================================================ */
@media (min-width: 1025px)                        { .mx-hide-pc     { display: none !important; } }
@media (min-width: 768px) and (max-width: 1024px) { .mx-hide-tablet { display: none !important; } }
@media (max-width: 768px)                         { .mx-hide-mobile { display: none !important; } }
