:root {
    --bg-color: #f5f5f7;
    --box-bg: rgba(255, 255, 255, 0.5);
    --text-color: #333333;
    --accent-color: #0066CC;
    --font-main: 'Outfit', sans-serif;
    --font-serif: 'Playfair Display', serif;
    --font-header: 'EB Garamond', serif;
    /* White outline for text readability over variable backgrounds */
    --text-outline: 1px 1px 0 #fff, -1px -1px 0 #fff, 1px -1px 0 #fff, -1px 1px 0 #fff;
}



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

body {
    background-color: var(--bg-color);
    color: var(--text-color);
    font-family: var(--font-main);
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
    user-select: none;
    /* Prevent text selection during clicking */
}

#app {
    width: 100%;
    height: 100%;
    position: relative;
    background: radial-gradient(circle at center, #ffffff 0%, #f0f0f0 100%);
}

/* --- Background Layer --- */

#background-layer {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 0;
    overflow: hidden;
    pointer-events: none;
}

#background-image {
    height: 100%;
    width: auto;
    max-width: none;
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    /* Fade edges to transparent so they look nice on wide screens */
    -webkit-mask-image: linear-gradient(to right, transparent 0%, black 15%, black 85%, transparent 100%);
    mask-image: linear-gradient(to right, transparent 0%, black 15%, black 85%, transparent 100%);
    transition: opacity 0.5s ease;
}

.temp-hidden {
    opacity: 0 !important;
}

/* --- Fullscreen Image Mode --- */
#fullscreen-layer {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 18;
    /* Above header/shortcuts (15), below buttons (20) */
    pointer-events: none;
    overflow: hidden;
    /* Prevent scrolling if image is wider than screen */
}

.layer-img.fullscreen {
    height: 100%;
    width: auto;
    max-width: none;
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    /* Mask edges same as background */
    -webkit-mask-image: linear-gradient(to right, transparent 0%, black 15%, black 85%, transparent 100%);
    mask-image: linear-gradient(to right, transparent 0%, black 15%, black 85%, transparent 100%);
    object-fit: cover;
}

/* --- Inset / Card Mode (Square Shadow) --- */
.layer-img.inset {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: auto;
    height: auto;
    max-width: 80vh;
    max-height: 80vh;
    object-fit: contain;
    /* Drop shadow directly on the image element (since it's an overlay card) */
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.5);
    border-radius: 4px;
    /* Slight rounding looks nice on cards */
}

.placeholder-card {
    background: rgba(255, 255, 255, 0.9);
    border: 2px dashed #ccc;
    display: flex;
    /* Centering text */
    justify-content: center;
    align-items: center;
    padding: 2rem;
    text-align: center;
    color: #555;
    font-family: var(--font-serif);
    font-style: italic;
    font-size: 1.2rem;
    /* Ensure it has a size if no content forces it */
    min-width: 200px;
    min-height: 120px;
    width: auto;
    max-width: 60vh;
}

/* --- Layer 1: Stage --- */

.stage {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 10;
    /* Stage creates a stacking context. Children Z-indices are relative to this. */
    display: flex;
    justify-content: center;
    align-items: flex-end;
}

#stage-shadow {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    /* Dark central shadow (tall ellipse), fixed aspect ratio relative to height */
    background: radial-gradient(ellipse 20vh 75vh at center, rgba(0, 0, 0, 0.3) 0%, transparent 90%);
    z-index: 0;
    /* Behind characters (which start at z-index 1) */
    pointer-events: none;
    transition: opacity 0.5s ease;
}

.layers {
    position: relative;
    width: 100%;
    height: 100%;
    max-width: 100%;
    /* Wider stage for VN look */
    pointer-events: none;
    /* Let clicks pass through to UI layer if needed */
    z-index: 1;
    /* Ensure layers are above vignette */
}

.layer-img {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 95%;
    object-fit: contain;
    object-position: bottom center;
    transition: opacity 0.3s ease;
}

/* --- Layer 2: UI --- */

.ui-layer {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 100;
    /* High Z-index to ensure UI sits above everything else */
    pointer-events: none;
    /* Let clicks pass through, children will re-enable */
}

/* Helper to re-enable pointer events on interactive elements */
.message-box,
.overlay {
    pointer-events: auto;
}

.hidden {
    display: none !important;
}

/* Message Box Styles */

.message-box {
    background: var(--box-bg);
    border: 1px solid rgba(255, 255, 255, 0.5);
    padding: 1.5rem 2rem;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
    backdrop-filter: blur(8px);
    position: absolute;
    animation: fadeIn 0.3s ease;
}

.continue-indicator::after {
    content: '▼';
    display: inline-block;
    margin-left: 10px;
    color: var(--accent-color);
    animation: bounce 1s infinite;
    font-size: 0.8rem;
}

/* Center Box (narration and system messages) */
.center-box {
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 600px;
    max-width: 90%;
    text-align: center;
    border-radius: 8px;
    font-style: italic;
    color: #333;
    text-shadow: var(--text-outline);
    font-size: 1.2rem;
    line-height: 1.6;
    transition: all 0.3s ease;
}

/* Postion lower when showing with image */
.center-box.with-image {
    top: 75%;
    transform: translate(-50%, -50%);
    max-width: 80%;
    background: rgba(255, 255, 255, 0.8);
    /* Slightly more opaque for readability over image bottom? */
}

/* System/Tutorial Messages - distinct from in-world narration */
.center-box.system-message {
    font-style: normal;
    color: #0055aa;
    border-color: rgba(0, 102, 204, 0.3);
    background: rgba(230, 240, 255, 0.95);
    text-shadow: none;
    font-size: 1rem;
}

/* Dialogue Box (Bottom) */
.dialogue {
    bottom: calc(80px + env(safe-area-inset-bottom));
    left: 50%;
    transform: translateX(-50%);
    width: 800px;
    max-width: 95%;
    min-height: 150px;
    /* Fixed height for consistency */
    border-radius: 12px;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    backdrop-filter: blur(1px);
}

.name-label {
    font-family: var(--font-serif);
    font-size: 1.3rem;
    color: var(--accent-color);
    margin-bottom: 0.8rem;
    font-weight: 600;
    text-shadow: var(--text-outline);
}

#dialogue-text {
    font-size: 1.2rem;
    line-height: 1.5;
    color: var(--text-color);
    text-shadow: var(--text-outline);
}

.thought-text {
    color: #555;
    font-family: var(--font-serif);
    font-style: italic;
    text-shadow: var(--text-outline);
}

.action-text {
    font-size: 1.1rem;
    font-weight: 400;
    font-style: italic;
    color: var(--text-color);
    text-shadow: var(--text-outline);
}

.thinking-label {
    font-size: 1rem;
    font-weight: 400;
    font-style: italic;
    color: #666;
    text-shadow: var(--text-outline);
}

/* Choices Overlay */

.overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    /* No background dimming - buttons are distinctive enough */
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 20;
    /* Above message boxes */
    pointer-events: none;
}

.choices-list {
    display: flex;
    flex-direction: column;
    gap: 15px;
    max-height: 80vh;
    /* Allow scrolling if too many buttons */
    overflow-y: auto;
    padding: 20px;
    width: 100%;
    align-items: center;
    pointer-events: auto;
}

.choice-btn {
    background: rgba(255, 255, 255, 0.8);
    border: 1px solid var(--accent-color);
    color: var(--text-color);
    text-shadow: var(--text-outline);
    padding: 15px 30px;
    font-family: var(--font-main);
    font-size: 1.1rem;
    cursor: pointer;
    border-radius: 8px;
    transition: all 0.2s ease;
    width: 500px;
    /* Fixed width for uniformity */
    max-width: 90%;
    text-align: center;
    backdrop-filter: blur(1px);
}

.choice-btn:hover {
    background: rgba(230, 242, 255, 0.8);
    color: #000;
    transform: scale(1.02);
    box-shadow: 0 0 15px rgba(0, 102, 204, 0.3);
}

.choice-btn.selected {
    border-color: var(--accent-color);
    box-shadow: 0 0 12px rgba(137, 207, 240, 0.5), inset 0 0 0 2px var(--accent-color);
}

.choice-btn.selected:hover {
    box-shadow: 0 0 15px rgba(187, 134, 252, 0.4), inset 0 0 0 2px var(--accent-color);
}

/* Animations */
@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translate(-50%, -40%);
    }

    /* Generic fade for centered items approx */
    to {
        opacity: 1;
    }
}

@keyframes bounce {

    0%,
    100% {
        transform: translateY(0);
    }

    50% {
        transform: translateY(3px);
    }
}

/* Custom Scrollbar for heavy button lists */
::-webkit-scrollbar {
    width: 8px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: #555;
    border-radius: 4px;
}

/* --- Location Header --- */

.location-header {
    position: absolute;
    top: calc(20px + env(safe-area-inset-top));
    left: 50%;
    transform: translateX(-50%);
    background: rgba(255, 255, 255, 0.6);
    padding: 0.5rem 1.5rem;
    border-radius: 4px;
    font-family: var(--font-header);
    font-size: 1.1rem;
    color: var(--accent-color);
    z-index: 15;
    pointer-events: none;
    backdrop-filter: blur(5px);
    border: 1px solid rgba(137, 207, 240, 0.3);
    text-align: center;
    max-width: 90%;
}

/* --- Shortcut Bar --- */

.shortcut-bar {
    position: absolute;
    bottom: calc(15px + env(safe-area-inset-bottom));
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    gap: 8px;
    z-index: 15;
    pointer-events: auto;
}

.shortcut-btn {
    width: 44px;
    height: 44px;
    background: rgba(255, 255, 255, 0.6);
    border: 1px solid var(--accent-color);
    color: var(--text-color);
    font-family: var(--font-main);
    font-size: 1.1rem;
    cursor: pointer;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
    backdrop-filter: blur(5px);
}

.shortcut-btn:hover:not(:disabled) {
    background: #e6f2ff;
    color: #000;
    transform: scale(1.05);
}

.shortcut-btn:disabled {
    opacity: 0.35;
    cursor: not-allowed;
    border-color: #555;
}

/* --- Modal (Quit Confirmation) --- */

.modal {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.7);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 50;
    backdrop-filter: blur(2px);
    pointer-events: auto;
    animation: fadeIn 0.2s ease;
}

.modal-content {
    position: relative;
    background: rgba(255, 255, 255, 0.95);
    border: 1px solid #333;
    padding: 2rem;
    border-radius: 8px;
    text-align: center;
    max-width: 500px;
    width: 90%;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.8);
}

.modal-close-btn {
    position: absolute;
    top: 10px;
    left: 10px;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    border: 1px solid transparent;
    /* only show border on hover for cleaner look */
    background: rgba(60, 60, 60, 0.6);
    /* subtler background */
    color: var(--text-color);
    font-size: 1rem;
    line-height: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s ease;
    z-index: 10;
}

.modal-close-btn:hover {
    background: var(--accent-color);
    color: #000;
    border-color: var(--accent-color);
    transform: scale(1.1);
}

/* Inherit system message styling */
.modal-content.system-message {
    font-style: normal;
    color: #0055aa;
    border-color: rgba(0, 102, 204, 0.3);
    background: rgba(230, 240, 255, 0.95);
    font-size: 1.1rem;
}

/* Error modal styling */
.modal-content.error-content {
    font-style: normal;
    color: #0055aa;
    border-color: rgba(0, 102, 204, 0.3);
    background: rgba(230, 240, 255, 0.95);
    max-width: 600px;
}

.modal-content.error-content h2 {
    margin-top: 0;
    margin-bottom: 1rem;
    color: #0055aa;
    font-family: 'Outfit', sans-serif;
}

.modal-content.error-content p {
    margin: 0.5rem 0;
    font-size: 1rem;
}

.error-report {
    width: 100%;
    min-height: 200px;
    max-height: 300px;
    margin: 1rem 0;
    padding: 0.75rem;
    font-family: 'Courier New', monospace;
    font-size: 0.85rem;
    background: rgba(255, 255, 255, 0.9);
    border: 1px solid rgba(0, 102, 204, 0.3);
    border-radius: 4px;
    resize: vertical;
    color: #333;
}

.modal-buttons {
    display: flex;
    gap: 1rem;
    justify-content: center;
    margin-top: 1.5rem;
}

.small-btn {
    padding: 10px 20px;
    width: auto;
    min-width: 120px;
    font-size: 1rem;
}

/* --- Help Button --- */

.help-button {
    position: absolute;
    top: calc(15px + env(safe-area-inset-top));
    right: calc(15px + env(safe-area-inset-right));
    width: 36px;
    height: 36px;
    border-radius: 50%;
    border: 1px solid var(--accent-color);
    background: rgba(255, 255, 255, 0.9);
    color: var(--text-color);
    font-size: 1.2rem;
    font-weight: 600;
    cursor: pointer;
    z-index: 25;
    transition: all 0.2s ease;
    backdrop-filter: blur(5px);
    pointer-events: auto;
}

.help-button:hover {
    background: var(--accent-color);
    color: #fff;
    transform: scale(1.05);
}

/* --- Help Modal --- */

.help-content {
    max-width: 600px;
    max-height: 80vh;
    text-align: left;
    font-size: 0.95rem;
    color: var(--text-color);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    /* container doesn't scroll, body does */
    position: relative;
}

#help-body {
    overflow-y: auto;
    flex: 1;
    padding-right: 14px;
    /* increased space for scrollbar and breathing room */
}

.help-content h2 {
    text-align: center;
    color: var(--accent-color);
    margin-top: 0;
    margin-bottom: 1.5rem;
    font-family: var(--font-header);
}

.help-content h3 {
    color: var(--accent-color);
    margin-top: 1.5rem;
    margin-bottom: 0.5rem;
    font-size: 1rem;
}

.help-content p {
    margin: 0.5rem 0;
    line-height: 1.5;
}

.help-shortcuts {
    width: 100%;
    border-collapse: collapse;
    margin-top: 0.5rem;
}

.help-shortcuts th,
.help-shortcuts td {
    padding: 0.4rem 0.8rem;
    text-align: left;
    border-bottom: 1px solid rgba(0, 0, 0, 0.1);
}

.help-shortcuts th {
    color: var(--accent-color);
    font-weight: 600;
}

.help-shortcuts tr:last-child td {
    border-bottom: none;
}

kbd {
    display: inline-block;
    padding: 0.15rem 0.4rem;
    font-family: var(--font-main);
    font-size: 0.85rem;
    background: rgba(0, 0, 0, 0.05);
    border: 1px solid rgba(0, 0, 0, 0.2);
    border-radius: 3px;
    color: #333;
}

/* --- Debug Panel --- */

#debug-panel {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: 300px;
    background: rgba(255, 255, 255, 0.98);
    border-right: 1px solid var(--accent-color);
    z-index: 100;
    display: flex;
    flex-direction: column;
    padding: 15px;
    box-shadow: 5px 0 20px rgba(0, 0, 0, 0.1);
    backdrop-filter: blur(10px);
    pointer-events: auto;
    /* allow interaction within panel */
    color: #333;
    font-family: 'Outfit', sans-serif;
}

#debug-close-btn {
    position: absolute;
    top: 10px;
    right: 10px;
    background: transparent;
    border: none;
    color: #888;
    font-size: 1.5rem;
    cursor: pointer;
    line-height: 1;
    z-index: 101;
}

#debug-close-btn:hover {
    color: var(--accent-color);
}

.debug-section {
    display: flex;
    flex-direction: column;
    flex: 1;
    overflow: hidden;
    margin-bottom: 15px;
    border: 1px solid #ddd;
    border-radius: 6px;
    background: rgba(0, 0, 0, 0.03);
}

.debug-section h3 {
    margin: 0;
    padding: 8px 12px;
    background: rgba(0, 0, 0, 0.05);
    border-bottom: 1px solid #ddd;
    font-size: 0.9rem;
    color: var(--accent-color);
    text-transform: uppercase;
    letter-spacing: 1px;
}

.debug-content {
    flex: 1;
    overflow: auto;
    padding: 10px;
    font-family: monospace;
    font-size: 0.8rem;
    white-space: pre-wrap;
    word-break: break-all;
    user-select: text;
    /* allow copying */
}

/* --- Mobile Responsiveness --- */
@media (max-width: 600px) {
    :root {
        --font-size-base: 14px;
    }

    .dialogue {
        width: 95%;
        padding: 1rem;
        /* Reduce bottom spacing on small screens slightly if needed, 
           but maintain safe area */
        bottom: calc(70px + env(safe-area-inset-bottom));
    }

    .name-label {
        font-size: 1.1rem;
        margin-bottom: 0.4rem;
    }

    #dialogue-text,
    .action-text,
    .thought-text {
        font-size: 1rem;
        line-height: 1.4;
    }

    .center-box {
        width: 90%;
        padding: 1rem;
        font-size: 1rem;
    }

    .choice-btn {
        width: 100%;
        padding: 12px 20px;
        font-size: 1rem;
    }

    .shortcut-btn {
        width: 40px;
        height: 40px;
    }

    .location-header {
        font-size: 1rem;
        padding: 0.4rem 1rem;
    }

    .layer-img.inset {
        max-width: 95%;
    }
}