/* --- Global Styles --- */
body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    /* The background is served from the R2 bucket ai-we */
    background-image: url('https://ai-story-weaver-api.sclin2713.workers.dev/background.png');
    background-size: cover;
    background-position: center center;
    background-repeat: no-repeat;
    background-attachment: fixed;
    color: #e0e0e0;
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh; /* Use min-height to be flexible */
    margin: 0;
    overflow-x: hidden; /* Allow vertical scroll if needed, but prevent horizontal */
    /* Fallback background color */
    background-color: #1a1a2e;
}

.hidden {
    display: none !important;
}

/* --- New Background Selector Styles (Desktop First) --- */
#background-selection-container {
    width: 100%;
    height: 100vh;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    /* Combine background image with dark overlay for text readability */
    background-image: 
        linear-gradient(135deg, rgba(10, 10, 10, 0.7) 0%, rgba(26, 26, 46, 0.7) 50%, rgba(22, 33, 62, 0.7) 100%),
        url('https://ai-story-weaver-api.sclin2713.workers.dev/background.png');
    background-size: cover;
    background-position: center center;
    background-repeat: no-repeat;
    background-attachment: fixed;
    padding: 2vh 0;
    box-sizing: border-box;
}

.background-title {
    color: #ffffff;
    font-size: clamp(1.8rem, 5vh, 3rem);
    font-weight: bold;
    text-align: center;
    text-shadow: 0 0 8px rgba(200, 220, 255, 0.8), 0 0 16px rgba(173, 216, 230, 0.7), 0 0 24px rgba(173, 216, 230, 0.5);
    margin-bottom: 3vh;
}

.options-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1.5vh;
    width: 90%;
    max-width: 450px;
    max-height: 80vh;
}

.option-card {
    background: linear-gradient(135deg, rgba(30, 30, 60, 0.9), rgba(50, 50, 80, 0.9));
    border: 2px solid rgba(100, 149, 237, 0.3);
    border-radius: 15px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    backdrop-filter: blur(10px);
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
    padding: 1.5vh 2vw;
    text-align: center;
    width: 100%;
}

.option-card:hover {
    transform: scale(1.05);
    border-color: rgba(100, 149, 237, 0.8);
    box-shadow: 0 0 40px rgba(100, 149, 237, 0.4), 0 0 80px rgba(100, 149, 237, 0.2);
    background: linear-gradient(135deg, rgba(100, 149, 237, 0.2), rgba(50, 50, 80, 0.9));
}

.option-text {
    color: #ffffff;
    font-size: clamp(1rem, 3vh, 1.4rem);
    font-weight: 600;
    text-shadow: 0 2px 10px rgba(0, 0, 0, 0.5);
}

.center-card {
    position: static;
    transform: none;
    width: 100%;
    background: linear-gradient(135deg, rgba(40, 40, 70, 0.95), rgba(60, 60, 90, 0.95));
    border: 3px solid rgba(255, 215, 0, 0.4);
    border-radius: 20px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    transition: all 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    backdrop-filter: blur(15px);
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.4);
    z-index: 1;
    padding: 2vh 2vw;
    margin-top: 1vh;
}

.center-text { 
    color: #ffffff; 
    font-size: clamp(0.9rem, 2.5vh, 1.2rem);
    font-weight: 600; 
    text-align: center; 
    margin-bottom: 1.5vh;
}
.custom-input { 
    background: rgba(20, 20, 40, 0.8); 
    border: 2px solid rgba(255, 215, 0, 0.3); 
    border-radius: 12px; 
    padding: 1.2vh 15px;
    width: 80%; 
    font-size: clamp(0.8rem, 2.2vh, 1rem);
    color: #ffffff; 
    text-align: center; 
    margin-bottom: 1.5vh;
    transition: all 0.3s ease; 
}
.custom-input:focus { 
    outline: none; 
    border-color: rgba(255, 215, 0, 0.8); 
    box-shadow: 0 0 20px rgba(255, 215, 0, 0.2); 
}
.custom-submit-btn { 
    background: linear-gradient(45deg, #ffac00, #ffda75); 
    border: none; 
    border-radius: 10px; 
    color: #202030; 
    font-weight: bold; 
    font-size: clamp(0.8rem, 2.2vh, 1rem);
    padding: 1.2vh 20px;
    cursor: pointer; 
    transition: all 0.3s ease; 
}
.custom-submit-btn:hover { 
    transform: scale(1.05); 
    box-shadow: 0 0 25px rgba(255, 215, 0, 0.4); 
}

/* --- Main Game Screen Styles --- */
#game-container { 
    width: 90%; 
    max-width: 800px; 
    height: 95vh; 
    display: flex; 
    flex-direction: column; 
    border: 1px solid rgba(100, 149, 237, 0.3);
    border-radius: 15px; 
    background-color: rgba(20, 20, 30, 0.85); /* Semi-transparent background */
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.37);
    backdrop-filter: blur(10px);
    padding: 15px; 
    /* Ensure background image is visible behind the semi-transparent container */
    background-image: 
        linear-gradient(rgba(20, 20, 30, 0.85), rgba(20, 20, 30, 0.85)),
        url('https://ai-story-weaver-api.sclin2713.workers.dev/background.png');
    background-size: cover;
    background-position: center center;
    background-repeat: no-repeat;
    background-attachment: fixed;
}
.game-header { display: flex; justify-content: space-between; align-items: center; border-bottom: 1px solid #333; padding-bottom: 15px; gap: 15px; }
.game-header h1 { border-bottom: none; padding-bottom: 0; margin: 0; font-size: 1.5em; flex-shrink: 0; }
#reset-game-btn { background-color: #555; color: white; border: none; padding: 8px 12px; border-radius: 4px; cursor: pointer; font-size: 0.9em; transition: background-color 0.3s; margin-left: auto; }
#reset-game-btn:hover { background-color: #c13838; }
#step-counter { font-size: 1.2em; font-weight: bold; color: #a0c4ff; flex-shrink: 0; }
#story-display { flex-grow: 1; padding: 15px 5px; overflow-y: auto; line-height: 1.6; }
#story-display p { margin-bottom: 15px; }

/* New style for the generated ending image */
.ending-image {
    max-width: 100%;
    max-height: 45vh; /* Limit height to 45% of the viewport height */
    object-fit: contain; /* Ensure the whole image is visible, scaled down */
    border-radius: 8px;
    margin: 1rem auto;
    display: block;
}

.system-message { color: #ffeb3b; font-style: italic; text-align: center; padding: 10px; background-color: rgba(255, 235, 59, 0.1); border-radius: 4px; }
#input-area { display: flex; align-items: center; padding: 15px 0 0 0; border-top: 1px solid #333; gap: 15px; }
#player-input { flex-grow: 1; min-width: 50px; background-color: #2c2c2c; border: 1px solid #444; border-radius: 4px; color: #e0e0e0; padding: 10px; font-size: 1em; }
#player-input:focus { outline: none; border-color: #007acc; }
#submit-action { background-color: #007acc; color: white; border: none; padding: 10px 15px; border-radius: 4px; cursor: pointer; font-size: 1em; transition: background-color 0.3s; }
#submit-action:hover:not(:disabled) { background-color: #005f99; }
#submit-action:disabled { background-color: #444; cursor: not-allowed; }
#dice-container { display: flex; flex-direction: column; align-items: center; gap: 5px; }
#dice-face { width: 40px; height: 40px; background-color: #fff; border: 2px solid #888; border-radius: 6px; color: #121212; display: flex; justify-content: center; align-items: center; font-size: 1.5em; font-weight: bold; cursor: pointer; padding: 0; transition: all 0.3s ease; }
#dice-face:disabled { background-color: #555; border-color: #777; cursor: not-allowed; color: #999; }
#dice-face.waiting-to-roll { border-color: #ffeb3b; box-shadow: 0 0 15px #ffeb3b; animation: pulse 1.5s infinite; }
#dice-roll-info { font-size: 0.8em; color: #aaa; white-space: nowrap; }
.rolling { animation: roll 0.5s ease-in-out; }
@keyframes roll { 0% { transform: rotate(0deg) scale(1); } 25% { transform: rotate(180deg) scale(1.2); } 50% { transform: rotate(-180deg) scale(1.2); } 100% { transform: rotate(0deg) scale(1); } }
@keyframes pulse { 0% { box-shadow: 0 0 5px #ffeb3b; } 50% { box-shadow: 0 0 20px #ffeb3b; } 100% { box-shadow: 0 0 5px #ffeb3b; } }

/* Post-game and Novel Generation Styles */
#post-game-options { display: flex; flex-wrap: wrap; justify-content: center; gap: 10px; padding: 15px; border-top: 1px solid #333; }
#new-game-btn, #continue-ending-btn, #generate-novel-btn { background-color: #007acc; color: white; border: none; padding: 10px 15px; border-radius: 4px; cursor: pointer; font-size: 0.9em; transition: background-color 0.3s; }
#new-game-btn:hover, #continue-ending-btn:hover, #generate-novel-btn:hover:not(:disabled) { background-color: #005f99; }
#generate-novel-btn:disabled { background-color: #444; cursor: not-allowed; }
#novel-status { width: 100%; text-align: center; margin-top: 10px; font-size: 0.9em; color: #ffeb3b; }
#novel-status a { color: #82cfff; text-decoration: none; }
#novel-status a:hover { text-decoration: underline; }

/* --- Responsive Design --- */
@media (max-width: 768px) {
    body { overflow-y: auto; } /* Allow scroll on small screens if content overflows */
    #game-container { width: 100%; height: 100%; min-height: 100vh; border-radius: 0; border: none; padding: 10px; box-sizing: border-box; }
    .game-header h1 { font-size: 1.2em; }
    #submit-action { padding: 10px; }
    #input-area { flex-wrap: wrap; }
    #player-input { width: 100%; margin-top: 10px; }
    #dice-face { width: 35px; height: 35px; font-size: 1.2em; }
    #dice-roll-info { font-size: 0.7em; }
}