.story-popup-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.75);
    backdrop-filter: blur(4px);
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    box-sizing: border-box;
    /* Prevent overscroll chaining to the background on mobile */
    overscroll-behavior: contain;
    touch-action: none;
}

.story-popup-content {
    background-color: #1a1a1a;
    background-image: 
        linear-gradient(rgba(0, 0, 0, 0.75), rgba(0, 0, 0, 0.75)),
        url('../../assets/images/popupbackground.png');
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
    color: #fff;
    padding: 20px;
    width: 80vw;
    max-width: 900px;
    /* Match combat popup sizing (combat reuses story popup visuals) */
    height: 82vh;
    height: 82dvh;
    height: calc(var(--app-vh, 1vh) * 82);
    max-height: calc(100vh - 40px);
    max-height: calc(100dvh - 40px);
    max-height: calc((var(--app-vh, 1vh) * 100) - 40px);
    border-radius: 8px;
    position: relative;
    text-align: left;
    animation: pulse-glow 5s ease-in-out infinite;
    display: flex;
    flex-direction: column;
    box-sizing: border-box;
    overflow: hidden;
}

/* Narrow screens: use more viewport width/height while keeping content readable */
@media (max-width: 720px) {
    .story-popup-content:not(.confirm-popup-content) {
        width: 94vw;
        height: 86vh;
        height: 86dvh;
        height: calc(var(--app-vh, 1vh) * 86);
        max-height: calc(100vh - 40px);
        max-height: calc(100dvh - 40px);
        max-height: calc((var(--app-vh, 1vh) * 100) - 40px);
        padding: 16px;
    }

    .story-popup-header {
        padding: 10px 12px;
        margin-bottom: 14px;
    }

    .story-popup-header h2 {
        font-size: 20px;
    }
}

/* Short screens: increase popup height and tighten internal spacing to reduce scrolling */
@media (max-height: 760px) {
    .story-popup-content:not(.confirm-popup-content) {
        height: 90vh;
        height: 90dvh;
        height: calc(var(--app-vh, 1vh) * 90);
        max-height: calc(100vh - 40px);
        max-height: calc(100dvh - 40px);
        max-height: calc((var(--app-vh, 1vh) * 100) - 40px);
        padding: 16px;
    }

    .story-popup-header {
        padding: 10px 12px;
        margin-bottom: 14px;
    }

    .story-popup-body {
        padding: 6px 0 10px 0;
    }

    .story-popup-nav {
        margin-top: 10px;
        padding-top: 8px;
    }
}

.story-popup-header {
    margin-bottom: 20px;
    padding-bottom: 16px;
    flex-shrink: 0;
    border-bottom: 2px solid rgba(var(--glow-r), var(--glow-g), var(--glow-b), 0.4);
    background: linear-gradient(90deg, 
        rgba(var(--glow-r), var(--glow-g), var(--glow-b), 0.08) 0%, 
        rgba(var(--glow-r), var(--glow-g), var(--glow-b), 0.12) 50%, 
        rgba(var(--glow-r), var(--glow-g), var(--glow-b), 0.08) 100%);
    box-shadow: 0 2px 12px rgba(var(--glow-r), var(--glow-g), var(--glow-b), 0.15);
    border-radius: 4px;
    padding: 12px 16px;
    margin-bottom: 20px;
}

.story-popup-header h2 {
    margin: 0;
    color: #90EE90;
    font-size: 24px;
    text-shadow: 0 0 8px rgba(var(--glow-r), var(--glow-g), var(--glow-b), 0.6),
                 0 0 16px rgba(var(--glow-r), var(--glow-g), var(--glow-b), 0.3);
    letter-spacing: 0.5px;
}

.story-popup-body {
    display: flex;
    flex-direction: column;
    gap: 0;
    margin: 0;
    padding: 8px 0 12px 0;
    overflow-y: auto;
    overflow-x: hidden;
    flex: 1 1 0;
    min-height: 0;
    /* iOS: allow smooth scrolling inside the popup */
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    touch-action: pan-y;
}

/* Custom scrollbar matching ingame log */
.story-popup-body::-webkit-scrollbar {
    width: 12px;
}
.story-popup-body::-webkit-scrollbar-track {
    background: #111;
}
.story-popup-body::-webkit-scrollbar-thumb {
    background-color: rgba(var(--glow-r), var(--glow-g), var(--glow-b), 0.5);
    border-radius: 6px;
    border: 3px solid #111;
}
.story-popup-body::-webkit-scrollbar-thumb:hover {
    background-color: rgba(var(--glow-r), var(--glow-g), var(--glow-b), 0.8);
}

.story-popup-text {
    text-align: left;
    padding: 0 8px 16px 8px;
    max-height: 100%;
    min-height: 0;
}

.story-popup-close {
    display: none;
}

.story-popup-nav {
    display: flex;
    align-items: center;
    justify-content: center;
    border-top: 2px solid rgba(var(--glow-r), var(--glow-g), var(--glow-b), 0.4);
    padding-top: 10px;
    margin-top: 12px;
    flex-shrink: 0;
    background: transparent;
}

.story-popup-nav button {
    padding: 10px 32px;
    background-color: #333;
    color: white;
    border: 1px solid #555;
    border-radius: 4px;
    cursor: pointer;
    transition: background-color 0.2s ease;
    font-size: 14px;
}

.story-popup-nav button:hover {
    background-color: #555;
}

/* ensure popup-nav uses the shared button look but slightly smaller / tighter */
.story-popup-nav .menu-button,
.story-nav-button {
    /* make each nav button ~20% of the popup width */
    flex: 0 0 20%;
    max-width: 20%;
    width: 20%;
    min-width: 90px; /* prevent buttons from becoming unusably small on narrow viewports */
    height: 36px;
    margin: 0;
    padding: 7px 14px;
    font-size: 14px;
    line-height: 1;
    border-radius: 4px;
    box-sizing: border-box;
    transform: none;
}

.story-popup-nav .menu-button:hover,
.story-nav-button:hover {
    transform: none;
}

/* keep paging centered and allow it to grow */
.story-popup-nav #popupPaging {
    margin: 0 12px;
    color: #bbb;
    font-size: 12px;
    flex: 1 1 auto;
    text-align: center;
}

/* responsive: relax percentage on narrow screens */
@media (max-width: 700px) {
    .story-popup-nav .menu-button,
    .story-nav-button {
        flex: 0 0 28%;
        max-width: 28%;
        width: 28%;
        min-width: 72px;
    }
}

#popupMessage {
    color: #EAEAEA;
    line-height: 1.6;
    white-space: pre-wrap;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.8),
                 0 0 4px rgba(255, 255, 255, 0.03);
}

/* Outcome section shown on the last page (unlocks, rewards) - no scrollbar needed here, parent scrolls */
.popup-outcome {
    margin-top: 24px;
    padding: 0;
    color: #cfd9cf;
    font-size: 13px;
}

.popup-outcome.hidden { 
    display: none; 
}

/* Add consistent spacing between all top-level sections */
.popup-outcome > * {
    margin-bottom: 20px;
}

.popup-outcome > *:last-child {
    margin-bottom: 0;
}

.popup-outcome h4 {
    margin: 0 0 10px 0;
    padding: 8px 12px;
    font-size: 12px;
    color: #90EE90;
    letter-spacing: 0.6px;
    text-transform: uppercase;
    font-weight: 600;
    background: linear-gradient(90deg, rgba(var(--glow-r), var(--glow-g), var(--glow-b), 0.15), rgba(var(--glow-r), var(--glow-g), var(--glow-b), 0.08) 50%, rgba(var(--glow-r), var(--glow-g), var(--glow-b), 0.15));
    border-left: 3px solid rgba(var(--glow-r), var(--glow-g), var(--glow-b), 0.6);
    border-radius: 2px;
    box-shadow: 0 0 8px rgba(var(--glow-r), var(--glow-g), var(--glow-b), 0.15);
}

.popup-outcome .outcome-grid {
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.popup-outcome ul {
    margin: 0;
    padding-left: 18px;
    list-style: none;
}

.popup-outcome li::before {
    content: '• ';
    color: #90EE90;
    margin-right: 6px;
}

/* Objectives section has nested structure - ensure consistent spacing */
.popup-outcome .outcome-objectives-new,
.popup-outcome .outcome-objectives-completed {
    margin-bottom: 20px;
}

.popup-outcome .outcome-objectives-new:last-child,
.popup-outcome .outcome-objectives-completed:last-child {
    margin-bottom: 0;
}

/* Objectives section styling */
.popup-outcome .objective-rewards { 
    color: #cfd9cf; 
    opacity: 0.9; 
    margin-left: 4px; 
    font-style: italic; 
}

/* Gold background for Objective Completed section */
.popup-outcome .outcome-objectives-completed h4 {
    background: linear-gradient(90deg, rgba(255, 217, 138, 0.15), rgba(255, 217, 138, 0.08) 50%, rgba(255, 217, 138, 0.15));
    border-left: 3px solid rgba(255, 217, 138, 0.6);
    color: #ffd98a;
    box-shadow: 0 0 8px rgba(255, 217, 138, 0.15);
}

/* Responsive: adjust sizing on narrow screens */
@media (max-width: 900px) {
    .story-popup-content {
        width: 90vw;
    }
    
    .popup-outcome {
        font-size: 12px;
    }
}

/* Small confirm popup variant (reuses story popup look) */
.story-popup-content.confirm-popup-content {
    width: min(820px, 96vw);
    max-width: 820px;
    height: auto;
    max-height: min(440px, 80vh);
    max-height: min(440px, 80dvh);
    max-height: min(440px, calc(var(--app-vh, 1vh) * 80));
    padding: 22px;
}

.story-popup-content.confirm-popup-content .story-popup-body {
    padding: 6px 0 8px 0;
    /* Confirm dialogs are short; let the body size to content (avoid tiny scroll viewport). */
    flex: 0 0 auto;
    overflow: visible;
}

.story-popup-content.confirm-popup-content .story-popup-text {
    padding: 0 6px 0 6px;
}

.story-popup-content.confirm-popup-content #confirmPopupMessage {
    display: block;
    max-width: 100%;
    white-space: normal;
    overflow-wrap: anywhere;
    word-break: break-word;
    line-height: 1.4;
    font-size: 16px;
}

.story-popup-content.confirm-popup-content .story-popup-nav.confirm-popup-nav {
    justify-content: flex-end;
    gap: 10px;
}

.story-popup-content.confirm-popup-content .story-popup-nav.confirm-popup-nav .menu-button,
.story-popup-content.confirm-popup-content .story-popup-nav.confirm-popup-nav .story-nav-button {
    flex: 0 0 auto;
    width: auto;
    max-width: none;
    min-width: 110px;
}

/* Phone-landscape (e.g., iPhone 12 Pro 844x390): tighten typography and spacing */
@media (max-width: 600px),
       (max-width: 900px) and (max-height: 450px),
       (hover: none) and (pointer: coarse) and (max-width: 900px) and (max-height: 600px) {
    .story-popup-overlay {
        padding: 10px;
    }

    .story-popup-content:not(.confirm-popup-content) {
        width: 94vw;
        height: 88vh;
        height: 88dvh;
        height: calc(var(--app-vh, 1vh) * 88);
        max-height: calc(100vh - 30px);
        max-height: calc(100dvh - 30px);
        max-height: calc((var(--app-vh, 1vh) * 100) - 30px);
        padding: 12px;
    }

    .story-popup-header {
        padding: 8px 10px;
        margin-bottom: 10px;
        padding-bottom: 10px;
    }

    .story-popup-header h2 {
        font-size: 18px;
    }

    .story-popup-body {
        padding: 4px 0 8px 0;
    }

    .story-popup-text {
        padding: 0 4px 10px 4px;
    }

    #popupMessage {
        font-size: 12px;
        line-height: 1.45;
    }

    .story-popup-nav {
        margin-top: 8px;
        padding-top: 6px;
    }

    .story-popup-nav .menu-button,
    .story-nav-button {
        height: 30px;
        padding: 6px 10px;
        font-size: 12px;
        min-width: 66px;
    }

    .story-popup-nav #popupPaging {
        font-size: 11px;
    }
}

/* Forced compact mode: mirror the phone-landscape Story Popup tuning so wide phones
   that still enter compact mode match iPhone behavior. */
html.is-compact .story-popup-overlay {
    padding: 10px;
}

html.is-compact .story-popup-content:not(.confirm-popup-content) {
    width: 94vw;
    height: 88vh;
    height: 88dvh;
    height: calc(var(--app-vh, 1vh) * 88);
    max-height: calc(100vh - 30px);
    max-height: calc(100dvh - 30px);
    max-height: calc((var(--app-vh, 1vh) * 100) - 30px);
    padding: 12px;
}

html.is-compact .story-popup-header {
    padding: 8px 10px;
    margin-bottom: 10px;
    padding-bottom: 10px;
}

html.is-compact .story-popup-header h2 {
    font-size: 18px;
}

html.is-compact .story-popup-body {
    padding: 4px 0 8px 0;
}

html.is-compact .story-popup-text {
    padding: 0 4px 10px 4px;
}

html.is-compact #popupMessage {
    font-size: 12px;
    line-height: 1.45;
}

html.is-compact .story-popup-nav {
    margin-top: 8px;
    padding-top: 6px;
}

html.is-compact .story-popup-nav .menu-button,
html.is-compact .story-nav-button {
    height: 30px;
    padding: 6px 10px;
    font-size: 12px;
    min-width: 66px;
}

html.is-compact .story-popup-nav #popupPaging {
    font-size: 11px;
}