/* ======================================
   Storybook - Responsive Layout
   ====================================== */

#storybook-container {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    width: 100vw;
    height: 100vh;
    background: linear-gradient(135deg, #667EEA 0%, #764BA2 100%);
    overflow: hidden;
    position: relative;
    font-family: "Inter", sans-serif;
    color: rgba(255, 255, 255, 0.95);
}

/* Desktop: Side-by-side layout */
@media (min-width: 768px) {
    /* Interactive pane becomes a flex row container on desktop ONLY when storybook is open */
    body.storybook-open .interactive-pane {
        display: flex;
        flex-direction: row;
        gap: 20px;
        align-items: flex-start;
        justify-content: flex-start;
        max-height: calc(100vh - 150px); /* Limit height to prevent overlap with input */
        overflow: visible; /* Ensure content isn't clipped */
    }

    /* Vertical image pane takes up available space when storybook is open */
    body.storybook-open #vertical-image-pane {
        flex: 1;
        display: block !important;
    }

    /* Storybook container has fixed width when visible */
    body.storybook-open #storybook-container {
        width: 400px;  /* Mobile-like width */
        height: calc(100vh - var(--header-height, 80px) - var(--footer-height, 70px) - 20px); /* Account for menu header and narrative input footer */
        position: relative;
        border-radius: 12px;
        box-shadow: 0 4px 20px rgba(0, 0, 0, 0.8);
        flex-shrink: 0;
        display: flex !important;
        flex-direction: column;
        margin-bottom: 10px; /* Breathing room above footer */
        max-height: calc(100vh - 160px); /* Prevent overflow */
    }

    /* No margin adjustment needed since it's inside interactive pane */
    body.storybook-open #cool-content-container {
        /* Remove the margin adjustment */
    }

    /* Keep other panes visible on desktop */
    body.storybook-open #vertical-image-pane,
    body.storybook-open #image-pane {
        display: block !important;
    }
    
    /* Ensure menu-container doesn't interfere with storybook when open */
    body.storybook-open .menu-container {
        z-index: 1900; /* Below storybook controls (2100) */
    }
}

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

/* Ensure reader takes up available space in flex container */
#storybook-container #reader {
    flex: 1;
    min-height: 0; /* Important for flex children */
    width: 100%;
    height: 100%;
    max-width: 100%;
    max-height: 100%;
    border-radius: 12px;
    overflow: hidden;
    position: relative;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.6);
    background: linear-gradient(to bottom,#ffffff 0%,#ffffff 100%) padding-box,
    linear-gradient(135deg,#f7e6b5 0%,#d9b25c 30%,#f7e6b5 55%,#b88a2d 80%,#d9b25c 100%) border-box;
}

/* Desktop adjustments for reader */
@media (min-width: 768px) {
    #storybook-container #reader {
        height: calc(100% - 60px); /* Account for controls */
    }
}

#storybook-container .page-img {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
    filter: brightness(0.85);
    z-index: 0;
}

#storybook-container .page-overlay {
    position: relative;
    z-index: 1;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    overflow-y: auto;
    padding: 1.2rem;
    font-size: 1.1rem;
    line-height: 1.7;
    color: #fffbe9;
    background: linear-gradient(to top, rgba(0, 0, 0, 0.6), transparent 50%);
    text-shadow: 0 1px 3px rgba(0, 0, 0, 0.5);
}

#storybook-container .page-overlay h1,
#storybook-container .page-overlay h2,
#storybook-container .page-overlay h3 {
    font-family: "Inter", sans-serif;
    font-weight: 700;
    text-align: center;
    margin-bottom: 1rem;
    font-size: 1.4rem;
    color: rgba(255, 255, 255, 0.95);
}

#storybook-container .page-overlay #page > p:first-of-type::first-letter {
    font-family: "Inter", sans-serif;
    font-weight: 700;
    font-size: 3rem;
    float: left;
    padding-right: 0.25rem;
    line-height: 1;
    color: #F093FB;
}

#storybook-container .gloss {
    border-bottom: 1px dotted #ffd;
    cursor: help;
    position: relative;
    outline: none;
}

#storybook-container .gloss:hover::after {
    content: attr(title);
    position: absolute;
    bottom: 125%;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(0,0,0,.8);
    color: white;
    padding: 0.4rem 0.6rem;
    border-radius: 4px;
    white-space: pre-wrap;
    max-width: 80vw;
    font-size: 0.85rem;
    text-align: center;
    z-index: 10;
}

#storybook-container #controls {
    display: flex;
    justify-content: space-around;
    align-items: center;
    padding: 0.75rem 1rem;
    background: linear-gradient(135deg,
        rgba(102, 126, 234, 0.4) 0%,
        rgba(118, 75, 162, 0.4) 100%);
    backdrop-filter: blur(10px);
    border-top: 1px solid rgba(255, 255, 255, 0.2);
    z-index: 2100; /* Above narrative input footer */
    flex-shrink: 0;
    margin-top: auto;
}

#storybook-container button {
    flex: 1;
    margin: 0 0.5rem;
    padding: 0.6rem 1rem;
    font-size: 1rem;
    font-family: "Inter", sans-serif;
    font-weight: 500;
    background: rgba(255, 255, 255, 0.15);
    backdrop-filter: blur(5px);
    color: rgba(255, 255, 255, 0.95);
    border: 1px solid rgba(255, 255, 255, 0.25);
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.3s ease;
}

#storybook-container button:hover {
    background: rgba(255, 255, 255, 0.25);
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
}

#storybook-container button:active {
    transform: translateY(0);
}

#storybook-container button:disabled {
    opacity: 0.4;
    cursor: not-allowed;
    transform: none;
}

#storybook-container .voice {
    background: linear-gradient(135deg, rgba(240, 147, 251, 0.3), rgba(118, 75, 162, 0.3));
    border-color: rgba(240, 147, 251, 0.4);
}

#storybook-container .voice:hover {
    background: linear-gradient(135deg, rgba(240, 147, 251, 0.4), rgba(118, 75, 162, 0.4));
}

#storybook-container #toggleTextBtn {
    background: rgba(255, 255, 255, 0.1);
    min-width: auto;
    padding: 0.6rem 0.8rem;
}

#storybook-container #progress {
    min-width: 3rem;
    text-align: center;
    font-size: 0.95rem;
    font-weight: 600;
}

#storybook-container #exit-storybook {
    position: absolute;
    top: 10px;
    right: 10px;
    background: rgba(102, 126, 234, 0.3);
    backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.3);
    color: white;
    font-size: 0.95rem;
    font-family: "Inter", sans-serif;
    font-weight: 500;
    padding: 0.5rem 1rem;
    border-radius: 8px;
    z-index: 9999;
    cursor: pointer;
    transition: all 0.3s ease;
}

#storybook-container #exit-storybook:hover {
    background: rgba(102, 126, 234, 0.5);
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
}

/* ======================================
   PWA Storybook Overlay (iframe)
   ====================================== */

#pwa-storybook-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: linear-gradient(135deg,
        rgba(102, 126, 234, 0.85) 0%,
        rgba(118, 75, 162, 0.85) 100%);
    backdrop-filter: blur(10px);
    z-index: 3000;
    display: flex;
    align-items: center;
    justify-content: center;
    animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

.pwa-storybook-container {
    position: relative;
    width: min(95vw, 1200px);
    height: min(95vh, 900px);
    background: rgba(255, 255, 255, 0.98);
    border: 2px solid rgba(255, 255, 255, 0.3);
    border-radius: 20px;
    box-shadow: 0 8px 32px rgba(102, 126, 234, 0.4),
                0 16px 64px rgba(118, 75, 162, 0.2);
    overflow: hidden;
    animation: slideUpFadeIn 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    display: flex;
    flex-direction: column;
}

/* Responsive sizing based on viewport */
@media (min-width: 1440px) {
    .pwa-storybook-container {
        width: min(85vw, 1400px);
        height: min(90vh, 1000px);
    }
}

@media (min-width: 1920px) {
    .pwa-storybook-container {
        width: min(75vw, 1600px);
        height: min(85vh, 1200px);
    }
}

@keyframes slideUpFadeIn {
    from {
        opacity: 0;
        transform: translateY(50px) scale(0.95);
    }
    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

.pwa-close-btn {
    position: absolute;
    top: 10px;
    right: 10px;
    width: 40px;
    height: 40px;
    border: 2px solid rgba(255, 255, 255, 0.4);
    background: linear-gradient(135deg,
        rgba(102, 126, 234, 0.4) 0%,
        rgba(118, 75, 162, 0.4) 100%);
    backdrop-filter: blur(10px);
    color: white;
    font-size: 20px;
    font-weight: 600;
    border-radius: 50%;
    cursor: pointer;
    z-index: 3001;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.3s ease;
}

.pwa-close-btn:hover {
    background: linear-gradient(135deg,
        rgba(102, 126, 234, 0.6) 0%,
        rgba(118, 75, 162, 0.6) 100%);
    transform: scale(1.1) rotate(90deg);
    box-shadow: 0 4px 16px rgba(102, 126, 234, 0.5);
}

.pwa-close-btn:focus {
    outline: 3px solid #F093FB;
    outline-offset: 3px;
}

.pwa-storybook-iframe {
    width: 100%;
    height: 100%;
    border: none;
}

/* Loading state */
.pwa-loading-overlay {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background: rgba(255, 255, 255, 0.95);
    z-index: 3002;
    transition: opacity 0.3s ease;
}

.pwa-loading-overlay.fade-out {
    opacity: 0;
    pointer-events: none;
}

.loading-spinner {
    width: 48px;
    height: 48px;
    border: 4px solid rgba(102, 126, 234, 0.2);
    border-top-color: #667EEA;
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

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

.pwa-loading-overlay p {
    margin-top: 16px;
    font-family: "Inter", sans-serif;
    color: #333;
    font-size: 16px;
}

.loading-subtitle {
    font-size: 14px;
    color: #666;
    margin-top: 8px;
}

/* Screen reader only */
.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* ======================================
   Mobile Optimizations
   ====================================== */

@media (max-width: 767px) {
    /* Larger touch targets for mobile */
    .pwa-close-btn {
        width: 44px;
        height: 44px;
        top: 12px;
        right: 12px;
        font-size: 24px;
    }

    #storybook-container button {
        padding: 0.8rem 1.2rem;
        font-size: 1.1rem;
        min-height: 44px;
    }

    #storybook-container #controls {
        padding: 1rem;
    }

    /* Full screen on mobile for maximum immersion */
    .pwa-storybook-container {
        width: 100vw;
        height: 100vh;
        max-width: none;
        max-height: none;
        border-radius: 0;
    }

    /* Loading spinner sizing for mobile */
    .loading-spinner {
        width: 56px;
        height: 56px;
        border-width: 5px;
    }

    .pwa-loading-overlay p {
        font-size: 18px;
    }
}

/* Tablet optimizations */
@media (min-width: 768px) and (max-width: 1024px) {
    body.storybook-open #storybook-container {
        width: 350px;
    }

    .pwa-storybook-container {
        width: min(90vw, 900px);
        height: min(90vh, 700px);
    }
}

/* Medium laptops */
@media (min-width: 1025px) and (max-width: 1439px) {
    .pwa-storybook-container {
        width: min(90vw, 1000px);
        height: min(90vh, 800px);
    }
}

/* Ultra-wide screens */
@media (min-width: 2560px) {
    .pwa-storybook-container {
        width: min(60vw, 1800px);
        height: min(80vh, 1400px);
    }
}

/* Portrait orientation (phone/tablet) */
@media (orientation: portrait) and (max-width: 1024px) {
    .pwa-storybook-container {
        width: 95vw;
        height: 85vh;
        max-width: none;
    }
}

/* Landscape orientation on mobile */
@media (orientation: landscape) and (max-height: 600px) {
    .pwa-storybook-container {
        height: 98vh;
        width: min(85vw, 1000px);
    }

    .pwa-close-btn {
        width: 36px;
        height: 36px;
        font-size: 18px;
    }
}

/* High DPI displays */
@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
    .pwa-close-btn,
    #storybook-container button {
        /* Sharper borders on retina */
        border-width: 1.5px;
    }
}

/* Ensure iframe is always responsive */
.pwa-storybook-iframe {
    min-width: 100%;
    min-height: 100%;
    max-width: 100%;
    max-height: 100%;
}

/* ======================================
   Chapter Celebration Animations
   ====================================== */

.chapter-celebration-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    z-index: 4000;
    pointer-events: none;
    overflow: hidden;
}

.celebration-confetti {
    position: absolute;
    width: 10px;
    height: 10px;
    background: #F093FB;
    animation: confettiFall 3s linear forwards;
}

@keyframes confettiFall {
    0% {
        transform: translateY(0) rotate(0deg);
        opacity: 1;
    }
    100% {
        transform: translateY(100vh) rotate(720deg);
        opacity: 0;
    }
}

.celebration-message {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 4001;
    text-align: center;
    pointer-events: none;
    animation: celebrationPulse 2s ease-out forwards;
}

.celebration-message h2 {
    font-family: "Inter", sans-serif;
    font-weight: 700;
    font-size: 3rem;
    color: white;
    text-shadow: 0 0 20px rgba(102, 126, 234, 0.8),
                 0 0 40px rgba(118, 75, 162, 0.6);
    margin: 0;
}

.celebration-message .xp-badge {
    display: inline-block;
    margin-top: 1rem;
    padding: 1rem 2rem;
    background: linear-gradient(135deg,
        rgba(102, 126, 234, 0.9) 0%,
        rgba(118, 75, 162, 0.9) 100%);
    backdrop-filter: blur(10px);
    border: 2px solid rgba(255, 255, 255, 0.5);
    border-radius: 20px;
    font-size: 2rem;
    font-weight: 700;
    color: white;
    box-shadow: 0 8px 32px rgba(102, 126, 234, 0.6);
}

@keyframes celebrationPulse {
    0% {
        transform: translate(-50%, -50%) scale(0.5);
        opacity: 0;
    }
    50% {
        transform: translate(-50%, -50%) scale(1.1);
        opacity: 1;
    }
    100% {
        transform: translate(-50%, -50%) scale(1);
        opacity: 0;
    }
}

.fade-out {
    animation: fadeOut 0.5s ease-out forwards;
}

@keyframes fadeOut {
    from { opacity: 1; }
    to { opacity: 0; }
}

/* ======================================
   Achievement Unlock Animation
   ====================================== */

.achievement-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: linear-gradient(135deg,
        rgba(102, 126, 234, 0.95) 0%,
        rgba(118, 75, 162, 0.95) 100%);
    backdrop-filter: blur(10px);
    z-index: 5000;
    display: flex;
    align-items: center;
    justify-content: center;
    animation: fadeIn 0.3s ease;
}

.achievement-card {
    background: rgba(255, 255, 255, 0.98);
    border: 3px solid rgba(255, 255, 255, 0.5);
    border-radius: 24px;
    padding: 3rem;
    text-align: center;
    max-width: 500px;
    box-shadow: 0 16px 48px rgba(0, 0, 0, 0.4),
                0 0 100px rgba(240, 147, 251, 0.6);
    animation: achievementReveal 0.6s cubic-bezier(0.68, -0.55, 0.265, 1.55);
}

@keyframes achievementReveal {
    0% {
        transform: scale(0) rotate(-180deg);
        opacity: 0;
    }
    100% {
        transform: scale(1) rotate(0deg);
        opacity: 1;
    }
}

.achievement-card .achievement-icon {
    font-size: 5rem;
    margin-bottom: 1rem;
    animation: iconBounce 0.8s ease infinite;
}

@keyframes iconBounce {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-10px); }
}

.achievement-card h2 {
    font-family: "Inter", sans-serif;
    font-weight: 700;
    font-size: 2rem;
    color: #667EEA;
    margin: 1rem 0 0.5rem 0;
}

.achievement-card .description {
    font-family: "Inter", sans-serif;
    font-size: 1.1rem;
    color: #666;
    margin-bottom: 1.5rem;
}

.achievement-card .rarity {
    display: inline-block;
    padding: 0.5rem 1.5rem;
    border-radius: 12px;
    font-weight: 600;
    font-size: 0.9rem;
    text-transform: uppercase;
    letter-spacing: 1px;
    margin-bottom: 1rem;
}

.achievement-card .rarity.common {
    background: linear-gradient(135deg, #95a5a6, #7f8c8d);
    color: white;
}

.achievement-card .rarity.uncommon {
    background: linear-gradient(135deg, #2ecc71, #27ae60);
    color: white;
}

.achievement-card .rarity.rare {
    background: linear-gradient(135deg, #3498db, #2980b9);
    color: white;
}

.achievement-card .rarity.epic {
    background: linear-gradient(135deg, #9b59b6, #8e44ad);
    color: white;
}

.achievement-card .rarity.legendary {
    background: linear-gradient(135deg, #f39c12, #e67e22);
    color: white;
    box-shadow: 0 0 20px rgba(243, 156, 18, 0.6);
}

.achievement-card .xp-reward {
    font-size: 2.5rem;
    font-weight: 700;
    color: #F093FB;
    text-shadow: 0 2px 8px rgba(240, 147, 251, 0.4);
}
