/* Guide, Controls, & Codex Screens (shared styles) */
#guide-screen, #controls-screen, #bestiary-screen {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    /* Keep overlay transparent so underlying main menu background remains visible */
    background-color: transparent;
    display: flex;
    justify-content: center;
    align-items: flex-start; /* ensure settings window top is flush with browser top */
    z-index: 600; /* sit above start-screen (300) but below modal-style codex (900) so background remains visible */
    color: white;
    /* ensure overlays block underlying canvas and interactions */
    pointer-events: auto;
}

#guide-container, #controls-container, #codex-container {
    background-color: #1a1a1a;
    border: 2px solid #555;
    border-radius: 12px;
    padding: 0;
    width: 100%;
    max-width: 1100px; /* wider codex for larger screens */
    /* make settings/guide/codex containers fill viewport vertically so they are flush to top & bottom */
    height: 100vh;
    max-height: 100vh;
    box-sizing: border-box;
    -webkit-overflow-scrolling: touch;
    box-shadow: 0 0 20px rgba(0, 255, 255, 0.3);
    position: relative;
    display: flex;
    flex-direction: column;
    gap: 12px;
    /* center the container within the full-screen overlay */
    margin: 0 auto;
    align-items: stretch;
}

#close-guide-button, #close-controls-button, #close-codex-button {
    position: absolute;
    top: 10px;
    right: 15px;
    background: none;
    border: none;
    color: #aaa;
    font-size: 32px;
    cursor: pointer;
    padding: 0;
    line-height: 1;
}

#close-guide-button:hover, #close-controls-button:hover, #close-codex-button:hover {
    color: white;
}

#guide-container h2, #controls-container h2, #codex-container h2 {
    text-align: center;
    margin-top: 0;
    margin-bottom: 30px;
    color: #00ffcc;
    font-size: 2.5em;
}

#codex-container h2 {
    text-align: left;
}

/* Guide Screen Specifics */
.guide-section {
    margin-bottom: 25px;
}

.guide-section h3 {
    color: #00ffcc;
    border-bottom: 1px solid #444;
    padding-bottom: 8px;
    margin-bottom: 12px;
}

.guide-section p {
    line-height: 1.6;
    color: #ccc;
}

/* Controls Screen Specifics */
#controls-content {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 16px;
}

#controls-container {
    /* changed: make container semi-transparent so start-screen background shows through */
    background-color: rgba(26,26,26,0.95);
    width: min(94vw, 980px);
    /* ensure controls container sits flush to the browser top and bottom */
    height: 100vh;
    max-height: 100vh;
    padding: 16px;
    margin: 0;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    box-sizing: border-box;
}

#controls-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: auto auto auto auto;
    grid-template-areas:
        "audio graphics controls"
        "misc misc controls"
        "divider divider divider"
        "debug debug debug";
    gap: 16px;
    width: 100%;
    flex: 1 1 auto;
    overflow-y: auto;
    overflow-x: hidden;
    box-sizing: border-box;
    padding: 8px 6px;
}

/* Ensure the divider spans the full width */
#controls-grid::before {
    content: "";
    grid-area: divider;
    width: 100%;
    height: 1px;
    margin: 12px 0;
    background: linear-gradient(90deg, rgba(255,255,255,0.04) 0%, rgba(255,255,255,0.10) 50%, rgba(255,255,255,0.04) 100%);
    border-radius: 1px;
    box-shadow: 0 1px 0 rgba(0,0,0,0.35) inset;
    pointer-events: none;
}

/* Place sections in their designated areas */
#audio-section { grid-area: audio; }
#graphics-section { grid-area: graphics; }
#controls-section { 
    grid-area: controls; 
    max-height: none;
    overflow-y: auto;
}
#misc-section { grid-area: misc; }
#admin-section { 
    grid-area: debug;
    max-height: none;
}

/* Make sure the debug panel is properly positioned */
#admin-section {
    background: rgba(255,255,255,0.02);
    border: 1px solid rgba(255,255,255,0.04);
    padding: 12px;
    border-radius: 8px;
    color: #e6fdfa;
    overflow-y: auto;
    box-sizing: border-box;
}

/* section placement */
#controls-section .subsection-title { 
    margin: 10px 0 8px; 
    font-size: 14px; 
    color: #00ffcc;
    font-weight: 700; 
}
/* tighten spacing between titles and lists */
#controls-section #gamepad-bindings { margin-top: 6px; }
#controls-section #kbm-bindings { margin-top: 6px; }

/* Hide native scrollbars but keep scroll functionality */
#controls-content::-webkit-scrollbar { display: none; }
#controls-content { -ms-overflow-style: none; scrollbar-width: none; }

/* Ensure each column is flexible but constrained so things fit nicely */
.controls-column {
    width: 100%;
    min-width: 0;
    max-width: 100%;
}

/* Special joystick column should shrink gracefully */
.controls-column.joystick-column {
    order: -1; /* ensure joystick column is displayed first (top) */
    width: 100%;
    min-width: 0;
    max-width: 100%;
}

/* On very narrow or tall (vertical) screens, stack columns */
@media (max-width: 760px), (max-aspect-ratio: 9/16) {
    /* Stack everything vertically on narrow/tall screens for predictable order */
    #controls-grid {
        grid-template-columns: 1fr;
        grid-template-areas:
            "audio"
            "graphics"
            "misc"
            "controls"
            "divider"
            "debug";
        gap: 12px;
        padding: 6px 2px;
        overflow-y: auto;
    }
    /* Hide scrollbars on stacked layout as well */
    #controls-content::-webkit-scrollbar { display: none; }
    #controls-content { -ms-overflow-style: none; scrollbar-width: none; }

    /* Ensure divider spans correctly in the new mobile grid */
    #controls-grid::before {
        grid-row: divider;
        grid-column: 1; /* single column span */
    }
    
    .controls-column {
        max-width: 100%;
    }

    /* Reassign grid areas for mobile */
    #audio-section { grid-area: audio; }
    #graphics-section { grid-area: graphics; }
    #misc-section { grid-area: misc; }
    /* controls section is now a single row, taking the full 1-column space */
    #controls-section { 
        grid-area: controls;
        max-height: none;
    } 
    #admin-section { 
        grid-area: debug;
        max-height: none;
    }
    
    /* Remove Apply button from mobile layout */
    #controls-apply-button {
        display: none;
    }
}

.controls-column h3 {
    border-bottom: 1px solid #444;
    padding-bottom: 10px;
    margin-bottom: 20px;
    color: #00ffcc;
}

.control-binding {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 0;
    border-bottom: 1px solid #2a2a2a;
}

.control-binding:last-child {
    border-bottom: none;
}

.control-key {
    background-color: #333;
    color: #eee;
    padding: 6px 12px;
    border-radius: 5px;
    border: 1px solid #555;
    min-width: 100px;
    text-align: center;
    cursor: pointer;
    font-family: 'Courier New', Courier, monospace;
    transition: background-color 0.2s, color 0.2s;
}

.control-key:hover {
    background-color: #444;
}

.control-key.binding {
    background-color: #00ffcc;
    color: #111;
    animation: pulse 1s infinite;
}

@keyframes pulse {
    0% { box-shadow: 0 0 0 0 rgba(0, 255, 204, 0.7); }
    70% { box-shadow: 0 0 0 10px rgba(0, 255, 204, 0); }
    100% { box-shadow: 0 0 0 0 rgba(0, 255, 204, 0); }
}

.control-label {
    font-weight: bold;
    color: #00ffcc;
}

#gamepad-info p {
    padding: 10px 0;
    border-bottom: 1px solid #2a2a2a;
}
#gamepad-info p:last-child {
    border-bottom: none;
}

#reset-controls-container {
    text-align: center;
    margin-top: 30px;
}

#reset-controls-button {
    padding: 10px 25px;
    font-size: 16px;
    background-color: transparent;
    color: #ff6b6b;
    border: 2px solid #ff6b6b;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.3s;
}

#reset-controls-button:hover {
    background-color: #ff6b6b;
    color: #1a1a1a;
}

/* Smaller glass-styled toggles used in settings and admin debug panel */
.glass-toggle {
    width: 36px;
    height: 20px;
    appearance: none;
    -webkit-appearance: none;
    background: rgba(255,255,255,0.04);
    border: 1px solid rgba(255,255,255,0.06);
    border-radius: 10px;
    position: relative;
    cursor: pointer;
    box-shadow: inset 0 1px 0 rgba(255,255,255,0.02), 0 2px 8px rgba(0,0,0,0.4);
    backdrop-filter: blur(6px);
}
.glass-toggle:checked {
    background: linear-gradient(180deg, rgba(255,255,255,0.06), rgba(255,255,255,0.02));
    border-color: rgba(255,255,255,0.12);
    box-shadow: 0 2px 12px rgba(0,0,0,0.5), inset 0 1px 0 rgba(255,255,255,0.02);
}

/* visual knob inside small toggle */
.glass-toggle::after {
    content: '';
    display: block;
    width: 16px;
    height: 16px;
    border-radius: 8px;
    background: rgba(255,255,255,0.9);
    transform: translateX(4px);
    transition: transform 180ms ease, background 180ms ease;
    box-shadow: 0 2px 6px rgba(0,0,0,0.4);
}
.glass-toggle:checked::after { transform: translateX(16px); background: #00ffcc; }

/* Square plus/minus buttons used in Debug Panel */
.btn-square {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: rgba(0,0,0,0.45);
    border: 1px solid rgba(255,255,255,0.05);
    color: #fff;
    width: 36px;
    height: 36px;
    border-radius: 8px;
    font-size: 18px;
    cursor: pointer;
}
.btn-square:hover { background: rgba(255,255,255,0.03); border-color: rgba(255,255,255,0.08); }

/* NEW: blue variant for plus/minus controls in Graphics section */
.btn-square.blue {
    background: #777; /* flat gray button background */
    border: 1px solid #5f5f5f;
    color: #00ffcc !important; /* plus/minus symbol color */
    box-shadow: none !important;
    position: relative;
    z-index: 1;
}
/* remove cyan circular underlay for the new flat button appearance */
.btn-square.blue::before {
    display: none;
}
/* ensure the button content sits centered above no underlay */
.btn-square.blue > * { position: relative; z-index: 2; }

/* ensure cheat-control input group spacing looks balanced */
.cheat-control { gap: 6px; }

/* Apply button area styling: pin to bottom-left of controls window with glass look */
#controls-apply-button {
    display: none;
}

/* Ensure section titles are flush to top of the controls window */
#controls-container > h2, #controls-container h3 {
    margin-top: 6px; /* small top gap but visually flush inside container */
}

/* Make sure control columns align to top so headings are at the very top of their columns */
.controls-column { align-self: start; }

/* Landscape / horizontal layout tweak: reduce vertical insets and increase codex max-height so content fits */
@media (min-aspect-ratio: 16/9) {
    #codex-container { max-height: calc(100vh - 80px); margin: 40px auto; }
    .codex-scroll { max-height: calc(100vh - 140px); }
}

#codex-screen #codex-title { display: none !important; }

/* Ensure only the codex container's title is visible: hide any stray #codex-title outside #codex-container */
#codex-title { display: none !important; }
#codex-container #codex-title { display: block !important; color: #00ffcc !important; }

/* Also hide any duplicate codex title nodes that might be direct children of body or other roots */
body > #codex-title,
:not(#codex-container) #codex-title {
    display: none !important;
}

/* Remove black framing bars above and below the codex container (visual only, placed by parent overlay)
   Previously added via pseudo-elements; disable them so no bars appear. */
#codex-screen::before,
#codex-screen::after {
    display: none;
    content: "";
    position: fixed;
    left: 0;
    right: 0;
    height: 0;
    background: transparent;
    z-index: -1;
    pointer-events: none;
}