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

@font-face {
    font-family: 'LXGW WenKai';
    src: url('../assets/font/LXGWWenKai-Regular.woff2') format('woff2');
    font-display: swap;
}

body {
    font-family: 'LXGW WenKai', "KaiTi", "STKaiti", serif;
    background: #000;
    color: #e0d8b0;
    height: 100vh;
    height: 100dvh; /* dynamic viewport height */
    display: flex;
    flex-direction: column;
    overflow: hidden;
    position: relative;
    /* safe-area support */
    padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
    margin: 0;
}

body::before {
    content: '';
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: url('../assets/images/game-bg.webp') center/cover no-repeat fixed;
    filter: blur(8px);
    z-index: -1;
    transform: scale(1.05);
}

#game-container {
    position: absolute;
    top: env(safe-area-inset-top, 0px);
    left: 50%;
    transform: translateX(-50%);
    width: 100%;
    /* max-width: 375px; */
    height: calc(100vh - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px));
    height: calc(100dvh - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px));
    overflow: hidden;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    z-index: 1;
    aspect-ratio: 5/9;
    background-color: rgba(0,0,0,0.7);
    box-shadow: 0 0 30px rgba(0,0,0,0.8);
    border: 2px solid #3a2a16;
    border-radius: 8px;
    /* safe-area support */
    padding-left: env(safe-area-inset-left, 0px);
    padding-right: env(safe-area-inset-right, 0px);
    /* performance hints */
    contain: strict;
    will-change: transform;
    backface-visibility: hidden;
    /* interaction hints */
    touch-action: manipulation;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    /* prevent content overflow */
    overflow-x: hidden;
    overflow-y: auto;
}

#background {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
    z-index: 0;
    filter: none;
}

#loading-container {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background-color: #000;
    z-index: 10;
}

#loading-container div {
    color: #e0d8b0;
    font-family: "KaiTi", "STKaiti", serif;
    font-size: 18px;
}

#loading-bar {
    width: 80%;
    height: 20px;
    background-color: #333;
    margin-top: 20px;
    border-radius: 10px;
    overflow: hidden;
}

#loading-progress {
    height: 100%;
    width: 0;
    background-color: #8b5a2b;
    transition: width 0.3s;
}

#opening-text, #story-text {
    width: calc(100% - 40px);
    font-size: 20px;
    line-height: 1.8;
    margin: 10px auto;
    white-space: pre-line;
    text-align: center;
    max-height: 70vh;

    overflow-y: auto;
    padding: 15px;
    font-family: 'LXGW WenKai', "KaiTi", "STKaiti", serif;
    background: linear-gradient(145deg, rgba(139, 90, 43, 0.95), rgba(107, 69, 31, 0.95));
    border: 3px solid #a67c52;
    border-radius: 12px;
    /*
     * ⚠️ Both frames must be drawn with box-shadow, **not ::before/::after**.
     *
     * This element is a scroll container (see overflow-y: auto below). An
     * absolutely positioned pseudo-element is contained by the scroll container
     * itself, so it lives in the scrollable overflow area — **its edges scroll
     * with the text**. The visible symptom: as you scroll down, the bottom edge
     * of the frame drifts up and cuts a line across the prose (reported by a
     * player).
     *
     * box-shadow is painted on the element's own border box and **does not
     * scroll**, so it always hugs the box.
     *
     * The cost: the old inner frame was inset 5px with an 8px radius, which
     * box-shadow cannot express; it now sits flush inside the border (second
     * shadow below). The result is the same double-line look as before.
     */
    box-shadow:
        /* outer frame (was ::before: 2px, 14px radius, .3 alpha) */
        0 0 0 2px rgba(224, 216, 176, 0.3),
        /* inner frame (was ::after: 1px, .2 alpha) */
        inset 0 0 0 1px rgba(230, 216, 176, 0.2),
        /* original drop shadow and inner glow, unchanged */
        0 10px 30px rgba(0, 0, 0, 0.5),
        inset 0 0 20px rgba(230, 216, 176, 0.1);
    position: relative;
    backdrop-filter: none;
    display: none;
}

#opening-text {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    max-width: 600px;
    margin: 0;
    min-height: 20px;
    max-height: 70vh;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding: 30px;
    box-sizing: border-box;
}

#story-text {
    font-size: 18px;
    text-align: left;
    max-height: 100vh;
}

/*
 * ⚠️ Two rules used to live here: `#opening-text::before, #story-text::before`
 * (2px outer frame) and `#opening-text::after, #story-text::after` (1px inner
 * frame, inset 5px).
 *
 * They were **absolutely positioned** with the element itself as containing
 * block — and `#story-text` is an `overflow-y: auto` scroll container, so those
 * decorative lines **scrolled with the prose**: on scroll the bottom edge drifted
 * into the middle of the text and looked like a rule cutting through it.
 *
 * They are now drawn with the **box-shadow** in the rule above (the element's
 * own decoration, which does not scroll).
 * **Do not turn them back into pseudo-elements** unless the decoration is also
 * moved out of the scroll container (e.g. a non-scrolling wrapper — but
 * `game.js` rewrites the body via `innerHTML`, which would clear it, so that way
 * is closed).
 */

.btn {
    padding: 12px 24px;
    margin: 10px;
    background: linear-gradient(180deg, #8b5a2b 0%, #6d451f 100%);
    color: #e0d8b0;
    border: 1px solid #a67c52;
    border-radius: 8px;
    font-size: 16px;
    cursor: pointer;
    transition: all 0.3s;
    box-shadow: 0 2px 5px rgba(0,0,0,0.2);
    font-family: 'LXGW WenKai', "KaiTi", "STKaiti", serif;
}

.btn:hover {
    background: linear-gradient(180deg, #a67c52 0%, #8b5a2b 100%);
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(0,0,0,0.3);
}

#options-container {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 60px;
    position: absolute;
    bottom: 10px;
    left: 0;
    flex-wrap: nowrap;
    overflow-x: auto;
    padding: 0 10px;
}

.btn-option {
    flex: 0 0 auto;
    width: 80px;
    height: 50px;
    margin: 0 8px;
    padding: 5px 10px;
    white-space: nowrap;
}

.api-loading-container {
    display: none;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    /* Translucent on purpose: the previous scene must stay readable. */
    background: rgba(0, 0, 0, 0.35);
    z-index: 20;
    align-items: center;
    justify-content: center;
}

.api-loading-spinner {
    width: 44px;
    height: 44px;
    border: 3px solid rgba(224, 216, 176, 0.25);
    border-top-color: #e0d8b0;
    border-radius: 50%;
    animation: api-spin 0.9s linear infinite;
}

@keyframes api-spin {
    to { transform: rotate(360deg); }
}

.btn-disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

/* Settings icon */
.settings-icon {
    position: absolute;
    top: 10px;
    left: 10px;
    width: 32px;
    height: 32px;
    cursor: pointer;
    z-index: 50;
    transition: transform 0.3s;
    display: none;
}

.settings-icon:hover {
    transform: rotate(30deg);
}

/* Settings modal */
.settings-modal-content {
    width: 80%;
    max-width: 300px;
}

.setting-item {
    margin: 20px 0;
    display: flex;
    flex-direction: column;
    align-items: center;
}

.setting-item label {
    margin-bottom: 10px;
    font-size: 16px;
    display: block;
}

.radio-group {
    display: flex;
    gap: 15px;
    margin-top: 10px;
}

.radio-group label {
    display: flex;
    align-items: center;
    gap: 5px;
    cursor: pointer;
}

.radio-group input[type="radio"] {
    accent-color: #8b5a2b;
}

.volume-slider {
    width: 100%;
    margin: 10px 0;
    -webkit-appearance: none;
    height: 8px;
    background: #333;
    outline: none;
    border-radius: 4px;
}

.volume-slider::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: #e0d8b0;
    cursor: pointer;
}

#volume-value {
    font-size: 14px;
    color: #e0d8b0;
    margin-left: 10px;
}


.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0,0,0,0.7);
    z-index: 100;
    justify-content: center;
    align-items: center;
}

.modal-content {
    background-color: #222;
    padding: 20px;
    border-radius: 8px;
    max-width: 300px;
    text-align: center;
}

.source-link {
    display: inline-block;
    margin-top: 4px;
    font-size: 12px;
    letter-spacing: 0.5px;
    color: rgba(224, 216, 176, 0.62);
    text-decoration: none;
}

.source-link:hover {
    color: #f3dfa6;
    text-decoration: underline;
}

.modal-options {
    display: flex;
    justify-content: center;
    gap: 15px;
    margin-top: 20px;
}

/* Responsive: large screens */
@media (min-width: 768px) {
    #game-container {
        max-width: 540px;
        width: 90%;
    }
    
    #story-text {
        font-size: 20px;
        max-height: 80vh;
    }
    
    .btn {
        font-size: 18px;
    }
}

/* Responsive: medium phones (iPhone XR 414×896, iPhone 12 390×844) */
@media (max-width: 767px) and (min-width: 500px) {
    #game-container {
        max-width: 380px;
        width: 92%;
    }
    
    #story-text {
        font-size: 17px;
        max-height: 75vh;
        padding: 12px;
    }
    
    .btn {
        font-size: 15px;
        padding: 10px 20px;
    }
    
    .btn-option {
        width: 75px;
        height: 45px;
        margin: 0 6px;
        font-size: 13px;
    }
    
    #options-container {
        height: 55px;
        bottom: calc(10px + env(safe-area-inset-bottom, 0px));
    }
}

/* Responsive: small phones (iPhone SE 375×667) */
@media (max-width: 500px) {
    #game-container {
        max-width: 100%;
        width: 100%;
    }
    
    #story-text {
        font-size: 16px;
        max-height: 75vh;
        padding: 10px;
        line-height: 1.6;
    }
    
    .btn {
        font-size: 14px;
        padding: 8px 16px;
    }
    
    .btn-option {
        width: 70px;
        height: 40px;
        margin: 0 5px;
        font-size: 12px;
    }
    
    #options-container {
        height: 50px;
        bottom: calc(10px + env(safe-area-inset-bottom, 0px));
    }
}

/* Petal effect */
.petal {
    position: fixed;
    pointer-events: none;
    z-index: 5;
    background: linear-gradient(45deg, #ff9a9e, #fad0c4);
    border-radius: 50% 5px 50% 5px;
    box-shadow: 0 2px 10px rgba(255, 154, 158, 0.3);
    transition: transform 0.3s ease-out;
}

/* Petal fall animation */
@keyframes fall {
    0% {
        transform: translateY(0) rotate(0deg);
        opacity: 1;
    }
    25% {
        transform: translateY(25vh) rotate(90deg);
    }
    50% {
        transform: translateY(50vh) rotate(180deg);
        opacity: 0.8;
    }
    75% {
        transform: translateY(75vh) rotate(270deg);
    }
    100% {
        transform: translateY(100vh) rotate(360deg);
        opacity: 0;
    }
}

/* Petal size variants */
.petal.small {
    width: 12px;
    height: 12px;
}

.petal.medium {
    width: 18px;
    height: 18px;
}

.petal.large {
    width: 25px;
    height: 25px;
}

/* Petal colour variants */
.petal.pink {
    background: linear-gradient(45deg, #ff9a9e, #fad0c4);
}

.petal.white {
    background: linear-gradient(45deg, #ffffff, #f0f0f0);
}

.petal.purple {
    background: linear-gradient(45deg, #a18cd1, #fbc2eb);
}

/* Responsive: very small screens */
@media (max-width: 320px) {
    #game-container {
        max-width: 100%;
        width: 100%;
    }
    
    #story-text {
        font-size: 14px;
        max-height: 70vh;
        padding: 8px;
        line-height: 1.5;
    }
    
    .btn {
        font-size: 12px;
        padding: 6px 12px;
    }
    
    .btn-option {
        width: 65px;
        height: 35px;
        margin: 0 4px;
        font-size: 11px;
    }
    
    #options-container {
        height: 45px;
        bottom: 5px;
    }
}