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

:root {
    --text:        #d4c5a0;
    --text-input:  #8bc49a;
    --term-bg:     rgba(4, 3, 2, 0.82);
    --btn-bg:      rgba(25, 16, 6, 0.88);
    --btn-border:  #5a4020;
    --btn-hover:   rgba(70, 48, 18, 0.92);
    --btn-text:    #c4a06a;
    --input-bg:    rgba(8, 5, 2, 0.92);
    --font:        'Courier New', Courier, monospace;
}

html, body {
    height: 100%;
    overflow: hidden;
    background: #080503;
}

/* ── background cross-fade ──────────────────────────────────────── */

.bg-container {
    position: fixed;
    inset: 0;
    z-index: 0;
}

.bg {
    position: absolute;
    inset: 0;
    background-size: cover;
    background-position: center;
    opacity: 0;
    transition: opacity 1.6s ease;
}

.bg.active { opacity: 1; }

/* Vignette + atmospheric darkening over the background image */
.bg-overlay {
    position: fixed;
    inset: 0;
    background:
        radial-gradient(ellipse at center, transparent 20%, rgba(0,0,0,0.25) 100%),
        linear-gradient(to top, rgba(0,0,0,0.35) 0%, rgba(0,0,0,0.05) 60%, rgba(0,0,0,0.15) 100%);
    z-index: 1;
    pointer-events: none;
}

/* ── loading screen ─────────────────────────────────────────────── */

#loading {
    position: fixed;
    inset: 0;
    z-index: 200;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, 0.95);
    gap: 16px;
}

#loading p {
    color: var(--text);
    font: 1rem/1.5 var(--font);
    letter-spacing: 0.15em;
    opacity: 0.6;
    animation: blink-loading 1.4s ease-in-out infinite;
}

.lantern-icon {
    width: 48px;
    height: 48px;
    opacity: 0.7;
    animation: glow 1.8s ease-in-out infinite alternate;
}

@keyframes blink-loading {
    0%, 100% { opacity: 0.3; }
    50%       { opacity: 0.8; }
}

@keyframes glow {
    from { filter: drop-shadow(0 0 4px #c87020); }
    to   { filter: drop-shadow(0 0 14px #f0a030); }
}

/* ── main layout ────────────────────────────────────────────────── */

.game {
    position: relative;
    z-index: 2;
    height: 100vh;            /* fallback for older browsers */
    height: 100svh;           /* svh = small viewport height — always fits even with browser chrome visible */
    display: flex;
    flex-direction: column;
    padding: 8px;
    gap: 6px;
    max-width: 860px;
    margin: 0 auto;
    width: 100%;
}

/* ── terminal ───────────────────────────────────────────────────── */

.terminal {
    flex: 1;
    background: var(--term-bg);
    color: var(--text);
    font: 1rem / 1.65 var(--font);
    padding: 14px 16px 8px;
    overflow-y: auto;
    border: 1px solid rgba(90, 64, 32, 0.45);
    border-radius: 3px;
    backdrop-filter: blur(2px);
    -webkit-backdrop-filter: blur(2px);
}

.terminal::-webkit-scrollbar        { width: 5px; }
.terminal::-webkit-scrollbar-track  { background: transparent; }
.terminal::-webkit-scrollbar-thumb  { background: rgba(90, 64, 32, 0.5); border-radius: 3px; }

.line-output  { white-space: pre-wrap; word-break: break-word; }
.line-input   { color: var(--text-input); margin-top: 2px; white-space: pre-wrap; }
.line-blank   { display: block; height: 0.55em; }
.line-pending { color: var(--text-input); margin-top: 2px; white-space: pre-wrap; }
.line-pending::after {
    content: '▌';
    animation: blink 1s step-end infinite;
}
@keyframes blink {
    0%, 100% { opacity: 1; }
    50%       { opacity: 0; }
}

/* ── controls ───────────────────────────────────────────────────── */

.controls {
    display: flex;
    flex-direction: column;
    gap: 5px;
    flex-shrink: 0;
}

.action-grid {
    display: flex;
    gap: 6px;
    align-items: stretch;
}

/* 3×3 compass rose */
.dir-buttons {
    display: grid;
    grid-template-columns: repeat(3, 38px);
    grid-template-rows: repeat(3, 32px);
    gap: 3px;
    flex-shrink: 0;
}

/* 2-row grid of miscellaneous buttons (auto-rows handles the Credits overflow row) */
.other-buttons {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    grid-template-rows: repeat(2, 32px);
    grid-auto-rows: 32px;
    gap: 3px;
    flex: 1;
}

/* ── buttons ────────────────────────────────────────────────────── */

button {
    background: #e8e8e8;
    color: #111111;
    border: 2px solid;
    border-color: #f0f0f0 #707070 #707070 #f0f0f0;
    border-radius: 0;
    font: bold 0.70rem / 1 system-ui, sans-serif;
    letter-spacing: 0.05em;
    cursor: pointer;
    transition: none;
    white-space: nowrap;
    padding: 1px 4px;
    box-shadow: 1px 1px 0 #000000;
}

button:hover:not(:disabled) { background: #f0f0f0; }
button:active:not(:disabled) {
    border-color: #707070 #f0f0f0 #f0f0f0 #707070;
    padding: 2px 3px 0 5px;
    box-shadow: none;
}

button:disabled {
    opacity: 0.40;
    cursor: default;
}

.btn-look { font-size: 0.60rem; }

button.btn-unavailable:not(:disabled) {
    opacity: 0.35;
    color: #666666;
}


/* Mobile-specific: make button labels heavier and taller without widening */
@media (max-width: 600px) {
    button {
        font-size: 0.82rem;
        letter-spacing: 0.02em;
        padding: 4px 4px;
    }

    /* Keep compass button widths unchanged but make them taller for readability */
    .dir-buttons {
        grid-template-columns: repeat(3, 38px);
        grid-template-rows: repeat(3, 44px);
        gap: 4px;
    }

    /* Other buttons: keep same horizontal distribution but increase height */
    .other-buttons {
        grid-template-columns: repeat(4, 1fr);
        grid-template-rows: repeat(2, 44px);
        grid-auto-rows: 44px;
        gap: 4px;
    }

    /* Make the LOOK button label fit by slightly reducing its font-size only on very narrow screens */
    @media (max-width: 380px) {
        .btn-look {
            font-size: 0.72rem;
        }
    }
}

/* ── restart overlay ────────────────────────────────────────────── */

#restart-overlay {
    position: fixed;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
    pointer-events: none;
}

#restart-overlay button {
    pointer-events: auto;
    font-size: 1rem;
    padding: 10px 40px;
    letter-spacing: 0.12em;
}

/* ── dynamic item panel ─────────────────────────────────────────── */

#items-panel {
    display: none;
    flex-direction: column;
    gap: 3px;
    padding: 3px 0;
    border-top: 1px solid rgba(90, 64, 32, 0.3);
    border-bottom: 1px solid rgba(90, 64, 32, 0.3);
    max-height: 120px;
    overflow-y: auto;
}

.items-row {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 4px;
    min-height: 0;
}

.items-section-label {
    font: 0.60rem var(--font);
    color: var(--btn-border);
    text-transform: uppercase;
    letter-spacing: 0.08em;
    min-width: 54px;
    flex-shrink: 0;
}

.item-group {
    display: flex;
    align-items: center;
    gap: 2px;
    background: rgba(10, 6, 2, 0.7);
    border: 1px solid rgba(90, 64, 32, 0.35);
    border-radius: 2px;
    padding: 1px 4px 1px 5px;
}

.item-label {
    font: 0.62rem var(--font);
    color: var(--text);
    margin-right: 3px;
    white-space: nowrap;
}

.btn-item {
    font: bold 0.58rem system-ui, sans-serif;
    letter-spacing: 0.04em;
    padding: 1px 5px;
    min-height: 18px;
    background: #e8e8e8;
    border: 2px solid;
    border-color: #f0f0f0 #707070 #707070 #f0f0f0;
    border-radius: 0;
    color: #111111;
    cursor: pointer;
    transition: none;
    box-shadow: 1px 1px 0 #000000;
}

.btn-item:hover:not(:disabled) { background: #f0f0f0; }
.btn-item:active:not(:disabled) {
    border-color: #707070 #f0f0f0 #f0f0f0 #707070;
    box-shadow: none;
}
.btn-item:disabled { opacity: 0.40; cursor: default; }

/* ── responsive (mobile) ────────────────────────────────────────── */

@media (max-width: 500px) {
    .game      { padding: 4px; gap: 4px; }
    .terminal  { font-size: 1.05rem; padding: 10px 10px 6px; }
    button     { font-size: 0.65rem; }

    .dir-buttons {
        grid-template-columns: repeat(3, 34px);
        grid-template-rows: repeat(3, 30px);
    }

    .other-buttons {
        grid-template-rows: repeat(2, 30px);
        grid-auto-rows: 30px;
    }
}

/* Row 3 of other-buttons: gap (col 1), SAVE (col 2), LOAD (col 3), CREDITS (col 4) */
#btn-save    { grid-column: 2; }
#btn-load    { grid-column: 3; }
.btn-credits { grid-column: 4; }

.btn-load:disabled {
    opacity: 0.40;
    cursor: default;
}

/* ── credits overlay ────────────────────────────────────────────── */

#credits-overlay {
    position: fixed;
    inset: 0;
    z-index: 150;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, 0.88);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s ease;
    padding: 16px;
}

#credits-overlay.visible {
    opacity: 1;
    pointer-events: auto;
}

.credits-box {
    background: var(--term-bg);
    border: 1px solid rgba(90, 64, 32, 0.6);
    border-radius: 3px;
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    padding: 28px 32px;
    max-width: 540px;
    width: 100%;
    max-height: 90vh;
    overflow-y: auto;
    color: var(--text);
    font: 0.875rem / 1.65 var(--font);
}

.credits-title {
    font: bold 1rem / 1.2 var(--font);
    color: var(--btn-text);
    letter-spacing: 0.14em;
    text-align: center;
    text-transform: uppercase;
    margin-bottom: 24px;
}

.credits-entry {
    margin-bottom: 20px;
}

.credits-name {
    font: bold 0.875rem / 1.4 var(--font);
    color: var(--btn-text);
    margin-bottom: 5px;
}

.credits-year {
    color: var(--btn-border);
    font-weight: normal;
}

.credits-entry p {
    font-size: 0.82rem;
    line-height: 1.65;
}

.btn-credits-close {
    display: block;
    margin: 20px auto 0;
    padding: 6px 36px;
    font-size: 0.78rem;
    letter-spacing: 0.08em;
}
