/* ==========================================================================
   BEDTIME QUEST - PREMIUM COZY DARK NIGHT THEME (REFACTORED)
   ========================================================================== */

:root {
    --bg-gradient: linear-gradient(135deg, #04060f 0%, #080d24 50%, #10112b 100%);
    --panel-bg: rgba(14, 18, 41, 0.75);
    --panel-border: rgba(139, 92, 246, 0.22);
    --panel-glow: 0 12px 48px rgba(0, 0, 0, 0.6), inset 0 0 25px rgba(139, 92, 246, 0.12);
    
    --color-text-main: #f1f5f9;
    --color-text-muted: #94a3b8;
    --color-gold: #fbbf24;
    --color-gold-glow: 0 0 15px rgba(251, 191, 36, 0.5);
    
    --color-magic-purple: #c084fc;
    --color-magic-pink: #f472b6;
    
    --btn-primary-bg: linear-gradient(135deg, #a855f7 0%, #7c3aed 100%);
    --btn-primary-hover: linear-gradient(135deg, #c084fc 0%, #9061f9 100%);
    
    --btn-choice-bg: rgba(26, 33, 67, 0.65);
    --btn-choice-border: rgba(167, 139, 250, 0.25);
    
    --font-heading: 'Quicksand', system-ui, -apple-system, sans-serif;
    --font-body: 'Quicksand', sans-serif;
    
    --radius-lg: 28px;
    --radius-md: 20px;
    --radius-sm: 14px;
}

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

body {
    font-family: var(--font-body);
    background: #04060f;
    color: var(--color-text-main);
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow-x: hidden;
    position: relative;
}

/* ==========================================================================
   🌙 BLUE LIGHT / BEDTIME DIMMER OVERLAY
   ========================================================================== */

#bedtime-dimmer-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: #080400; /* Dark brown-red filters blue light better than black */
    opacity: 0.0; /* Controlled dynamically via JS */
    pointer-events: none; /* Let all clicks pass through */
    z-index: 9999; /* Always on top */
    transition: opacity 0.8s cubic-bezier(0.25, 0.46, 0.45, 0.94);
}

/* ==========================================================================
   STAR PARALLAX BACKGROUND
   ========================================================================== */

#stars-container {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 0;
    pointer-events: none;
    background: var(--bg-gradient);
}

.stars {
    position: absolute;
    width: 1px;
    height: 1px;
    background: transparent;
}

.stars.small {
    box-shadow: 
        10vw 20vh #fff, 30vw 15vh rgba(255,255,255,0.7), 45vw 40vh #fff, 60vw 25vh rgba(255,255,255,0.6), 
        80vw 10vh #fff, 90vw 45vh rgba(255,255,255,0.8), 15vw 70vh #fff, 35vw 85vh rgba(255,255,255,0.5), 
        55vw 60vh #fff, 70vw 80vh rgba(255,255,255,0.7), 88vw 75vh #fff, 95vw 90vh rgba(255,255,255,0.4);
    animation: starsShimmer 5s ease-in-out infinite alternate;
}

.stars.medium {
    width: 2px;
    height: 2px;
    box-shadow: 
        20vw 35vh rgba(251, 191, 36, 0.8), 40vw 50vh rgba(167, 139, 250, 0.7), 75vw 30vh rgba(244, 114, 182, 0.8), 
        5vw 60vh rgba(251, 191, 36, 0.6), 85vw 65vh rgba(167, 139, 250, 0.8), 50vw 90vh rgba(244, 114, 182, 0.7);
    animation: starsShimmer 7s ease-in-out infinite alternate 1s;
}

.stars.large {
    width: 3px;
    height: 3px;
    box-shadow: 
        15vw 10vh rgba(255, 255, 255, 0.9), 65vw 15vh rgba(255, 255, 255, 0.95), 
        25vw 80vh rgba(255, 255, 255, 0.9), 85vw 85vh rgba(255, 255, 255, 0.95);
    animation: starsFlash 4s infinite;
}

@keyframes starsShimmer {
    0% { opacity: 0.25; }
    100% { opacity: 1; }
}

@keyframes starsFlash {
    0%, 100% { opacity: 0.15; transform: scale(0.85); }
    50% { opacity: 1; transform: scale(1.15); }
}

/* ==========================================================================
   APP CONTAINER
   ========================================================================== */

.app-container {
    position: relative;
    z-index: 1;
    width: 100%;
    max-width: 1150px;
    min-height: 90vh;
    margin: 20px;
    padding: 35px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    background: var(--panel-bg);
    border: 1px solid var(--panel-border);
    border-radius: var(--radius-lg);
    box-shadow: var(--panel-glow);
    backdrop-filter: blur(18px);
}

.app-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-bottom: 25px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.05);
    margin-bottom: 25px;
}

.logo-area {
    display: flex;
    align-items: center;
    gap: 15px;
}

.logo-icon {
    font-size: 32px;
}

.logo-area h1 {
    font-family: var(--font-heading);
    font-size: 26px;
    color: #fff;
    font-weight: 700;
}

.logo-area .tagline {
    font-size: 13px;
    color: var(--color-text-muted);
}

.premium-badge {
    font-size: 11px;
    background: linear-gradient(135deg, var(--color-gold) 0%, #d97706 100%);
    color: #fff;
    padding: 3px 8px;
    border-radius: 20px;
    vertical-align: middle;
    margin-left: 8px;
    font-family: var(--font-body);
    font-weight: 700;
    box-shadow: var(--color-gold-glow);
    text-shadow: 0 1px 2px rgba(0,0,0,0.2);
}

.btn-widget {
    background: rgba(255, 255, 255, 0.06);
    border: 1px solid rgba(255, 255, 255, 0.1);
    color: #fff;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    transition: all 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.btn-widget:hover {
    background: rgba(168, 85, 247, 0.15);
    border-color: var(--color-magic-purple);
    transform: scale(1.08);
}

.header-widgets {
    display: flex;
    align-items: center;
    gap: 15px;
}

/* ==========================================================================
   SCREENS
   ========================================================================== */

.screen {
    display: none;
    opacity: 0;
    transform: translateY(20px);
    transition: opacity 0.6s ease, transform 0.6s ease;
    flex-grow: 1;
}

.screen.active {
    display: block;
    opacity: 1;
    transform: translateY(0);
}

#reader-screen.active {
    display: flex;
}

/* ==========================================================================
   SETUP SCREEN: FORMS & PERSONALIZATION
   ========================================================================== */

.welcome-box {
    background: rgba(8, 12, 30, 0.6);
    border: 1px solid rgba(139, 92, 246, 0.1);
    padding: 30px;
    border-radius: var(--radius-md);
    margin-bottom: 35px;
    box-shadow: 0 8px 32px rgba(0,0,0,0.3);
}

.welcome-box h2 {
    font-family: var(--font-heading);
    font-size: 24px;
    color: #fff;
    margin-bottom: 8px;
    text-align: center;
}

.welcome-box p {
    color: var(--color-text-muted);
    font-size: 15px;
    margin-bottom: 25px;
    text-align: center;
}

.form-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 20px;
    margin-bottom: 20px;
}

.input-group {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.input-group label {
    font-weight: 700;
    font-size: 13.5px;
    color: var(--color-magic-purple);
    letter-spacing: 0.3px;
}

.input-group input[type="text"], 
.input-group select {
    background: rgba(10, 14, 38, 0.85);
    border: 1px solid rgba(139, 92, 246, 0.22);
    padding: 14px 18px;
    border-radius: var(--radius-sm);
    color: #fff;
    font-family: var(--font-body);
    font-size: 15px;
    font-weight: 600;
    transition: all 0.3s ease;
}

.input-group input[type="text"]:focus,
.input-group select:focus {
    outline: none;
    border-color: var(--color-magic-pink);
    box-shadow: 0 0 12px rgba(244, 114, 182, 0.25);
    background: rgba(18, 22, 53, 0.95);
}

/* Sound Board parents panel */
.soundboard-group {
    grid-column: 1 / -1;
    margin-top: 15px;
}

.soundboard-group label {
    display: block;
    font-weight: 700;
    font-size: 14px;
    color: var(--color-magic-purple);
    margin-bottom: 12px;
}

.sound-toggles-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 12px;
}

.sound-toggle-btn {
    background: rgba(255, 255, 255, 0.04);
    border: 1px solid rgba(255, 255, 255, 0.08);
    border-radius: var(--radius-sm);
    color: var(--color-text-muted);
    font-family: var(--font-body);
    font-weight: 600;
    font-size: 14px;
    padding: 12px;
    cursor: pointer;
    transition: all 0.3s cubic-bezier(0.165, 0.84, 0.44, 1);
}

.sound-toggle-btn:hover {
    background: rgba(255, 255, 255, 0.08);
    color: #fff;
}

.sound-toggle-btn.active {
    background: rgba(168, 85, 247, 0.15);
    border-color: var(--color-magic-purple);
    color: #fff;
    box-shadow: 0 0 10px rgba(168, 85, 247, 0.3);
}

/* Accordion parents layout */
.tts-settings-accordion {
    background: rgba(15, 17, 43, 0.55);
    border: 1px solid rgba(139, 92, 246, 0.15);
    border-radius: var(--radius-sm);
    padding: 12px 20px;
    margin-top: 20px;
    cursor: pointer;
}

.tts-settings-accordion summary {
    font-weight: 700;
    font-size: 14.5px;
    color: var(--color-text-muted);
    outline: none;
    user-select: none;
}

.tts-settings-accordion[open] summary {
    margin-bottom: 20px;
    color: var(--color-magic-purple);
}

.tts-settings-content {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 20px;
    cursor: default;
}

.tts-settings-content input[type="range"] {
    -webkit-appearance: none;
    width: 100%;
    height: 8px;
    background: rgba(139, 92, 246, 0.18);
    border-radius: 4px;
    outline: none;
}

.tts-settings-content input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: var(--color-magic-purple);
    cursor: pointer;
    box-shadow: 0 0 8px rgba(167, 139, 250, 0.6);
}

/* ==========================================================================
   STORIES EXPLORER (CATEGORIZED TABS)
   ========================================================================== */

.stories-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 15px;
    margin-bottom: 20px;
}

.stories-section h3 {
    font-family: var(--font-heading);
    font-size: 20px;
    color: #fff;
    position: relative;
    padding-left: 15px;
}

.stories-section h3::before {
    content: '';
    position: absolute;
    left: 0;
    top: 4px;
    width: 4px;
    height: 20px;
    background: var(--color-gold);
    border-radius: 2px;
    box-shadow: var(--color-gold-glow);
}

.category-tabs {
    display: flex;
    gap: 8px;
    overflow-x: auto;
    padding-bottom: 5px;
}

.tab-btn {
    background: rgba(255, 255, 255, 0.04);
    border: 1px solid rgba(255, 255, 255, 0.08);
    padding: 8px 16px;
    border-radius: 30px;
    color: var(--color-text-muted);
    font-family: var(--font-body);
    font-weight: 700;
    font-size: 13px;
    cursor: pointer;
    white-space: nowrap;
    transition: all 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.tab-btn:hover {
    color: #fff;
    background: rgba(255, 255, 255, 0.08);
}

.tab-btn.active {
    background: var(--btn-primary-bg);
    border-color: transparent;
    color: #fff;
    box-shadow: 0 4px 15px rgba(168, 85, 247, 0.35);
}

.stories-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
    gap: 20px;
}

.story-card {
    background: rgba(18, 23, 51, 0.5);
    border: 1px solid rgba(139, 92, 246, 0.12);
    padding: 22px;
    border-radius: var(--radius-md);
    cursor: pointer;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    transition: all 0.4s cubic-bezier(0.165, 0.84, 0.44, 1);
    position: relative;
    overflow: hidden;
}

.story-card::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 4px;
    background: linear-gradient(90deg, var(--color-magic-purple), var(--color-magic-pink));
    opacity: 0;
    transition: opacity 0.3s ease;
}

.story-card:hover {
    transform: translateY(-4px);
    background: rgba(26, 31, 68, 0.65);
    border-color: rgba(167, 139, 250, 0.35);
    box-shadow: 0 12px 28px rgba(0, 0, 0, 0.45);
}

.story-card:hover::before {
    opacity: 1;
}

.story-card-header {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 12px;
}

.story-card-icon {
    font-size: 26px;
    background: rgba(139, 92, 246, 0.1);
    padding: 8px;
    border-radius: 12px;
}

.story-card-header h4 {
    font-family: var(--font-heading);
    font-size: 16px;
    color: #fff;
    line-height: 1.3;
}

.story-card p {
    font-size: 13.5px;
    color: var(--color-text-muted);
    line-height: 1.55;
    margin-bottom: 18px;
    flex-grow: 1;
}

.story-card-btn {
    align-self: flex-start;
    background: rgba(139, 92, 246, 0.12);
    border: 1px solid rgba(139, 92, 246, 0.22);
    color: var(--color-magic-purple);
    font-family: var(--font-body);
    font-weight: 700;
    font-size: 12.5px;
    padding: 8px 18px;
    border-radius: 20px;
    cursor: pointer;
    transition: all 0.3s ease;
}

.story-card:hover .story-card-btn {
    background: var(--btn-primary-bg);
    color: #fff;
    border-color: transparent;
    box-shadow: 0 4px 12px rgba(139, 92, 246, 0.3);
}

/* ==========================================================================
   READER SCREEN: DOUBLE COLUMN LAYOUT
   ========================================================================== */

.story-layout {
    display: grid;
    grid-template-columns: 1.25fr 1fr;
    gap: 35px;
    width: 100%;
    align-items: stretch;
}

/* Left Column: Image wrapper with Particle Canvas Overlay */
.illustration-panel {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    align-items: center;
    gap: 20px;
}

.image-wrapper {
    width: 100%;
    max-width: 540px;
    aspect-ratio: 3/2;
    border-radius: var(--radius-md);
    border: 5px solid rgba(255, 255, 255, 0.08);
    box-shadow: 0 15px 45px rgba(0, 0, 0, 0.6), 0 0 35px rgba(139, 92, 246, 0.18);
    overflow: hidden;
    position: relative;
    background: #010309;
}

.image-wrapper img {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
    z-index: 0;
    transition: filter 0.5s ease;
}

/* Canvas overlays directly on top of the image */
#particle-canvas {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 2; /* On top of image to capture bubble clicks */
    pointer-events: auto; /* Required for clicking bubbles */
}

.image-overlay-glow {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    box-shadow: inset 0 0 50px rgba(0, 0, 0, 0.7);
    pointer-events: none;
    z-index: 1; /* Under canvas, above image */
}

.image-loader {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: #03050d;
    display: none;
    justify-content: center;
    align-items: center;
    z-index: 3;
}

.image-loader.active {
    display: flex;
}

/* Quick Ambient Sleep Control Panel for child's bedside */
.quick-sleep-controls {
    width: 100%;
    max-width: 540px;
    background: rgba(14, 18, 41, 0.6);
    border: 1px solid rgba(139, 92, 246, 0.12);
    border-radius: var(--radius-sm);
    padding: 12px 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 15px;
}

.ambient-switches {
    display: flex;
    gap: 8px;
}

.quick-btn {
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid rgba(255, 255, 255, 0.08);
    width: 40px;
    height: 40px;
    border-radius: 50%;
    cursor: pointer;
    font-size: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.quick-btn:hover {
    transform: scale(1.08);
    background: rgba(255, 255, 255, 0.1);
}

.quick-btn.active {
    background: rgba(168, 85, 247, 0.2);
    border-color: var(--color-magic-purple);
    box-shadow: 0 0 10px rgba(168, 85, 247, 0.4);
}

.dimmer-quick-adjust {
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 700;
    font-size: 13.5px;
    color: var(--color-text-muted);
}

.adjust-btn {
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid rgba(255, 255, 255, 0.08);
    width: 32px;
    height: 32px;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    color: #fff;
    transition: all 0.2s ease;
}

.adjust-btn:hover {
    background: rgba(255, 255, 255, 0.1);
}

/* Right Column: Book Narrative & Choices */
.narrative-panel {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    background: rgba(12, 16, 38, 0.45);
    border: 1px solid rgba(139, 92, 246, 0.1);
    border-radius: var(--radius-md);
    padding: 26px;
}

.book-container {
    flex-grow: 1;
    display: flex;
    flex-direction: column;
}

.book-header-row {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 15px;
    margin-bottom: 18px;
}

.book-container h2 {
    font-family: var(--font-heading);
    font-size: 21px;
    color: var(--color-gold);
    line-height: 1.35;
}

.text-scroll-box {
    max-height: 300px;
    overflow-y: auto;
    margin-bottom: 25px;
    padding-right: 10px;
}

.text-scroll-box::-webkit-scrollbar {
    width: 6px;
}

.text-scroll-box::-webkit-scrollbar-track {
    background: rgba(255,255,255,0.02);
    border-radius: 3px;
}

.text-scroll-box::-webkit-scrollbar-thumb {
    background: rgba(139, 92, 246, 0.25);
    border-radius: 3px;
}

.story-narration {
    font-size: 16.5px;
    line-height: 1.85;
    color: var(--color-text-main);
    font-weight: 500;
    letter-spacing: 0.2px;
}

/* Interactive Breathing Balloon */
.breathing-balloon-wrapper {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 5px;
    min-width: 75px;
}

.breathing-balloon {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: radial-gradient(circle, rgba(167, 139, 250, 0.8) 0%, rgba(244, 114, 182, 0.3) 100%);
    box-shadow: 0 0 15px rgba(167, 139, 250, 0.4);
    filter: blur(1.5px);
    animation: breathingCycle 8s ease-in-out infinite;
}

.breathing-text {
    font-size: 10px;
    font-weight: 700;
    color: var(--color-magic-purple);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    text-align: center;
}

@keyframes breathingCycle {
    0%, 100% {
        transform: scale(0.8);
        opacity: 0.5;
        box-shadow: 0 0 8px rgba(167, 139, 250, 0.2);
    }
    40%, 60% {
        transform: scale(1.35);
        opacity: 1;
        box-shadow: 0 0 20px rgba(244, 114, 182, 0.6);
    }
}

/* Audio Controls */
.audio-controls {
    margin-bottom: 22px;
}

.audio-btn {
    background: rgba(139, 92, 246, 0.15);
    border: 1px solid rgba(139, 92, 246, 0.3);
    padding: 10px 22px;
    border-radius: 30px;
    color: #c084fc;
    font-family: var(--font-body);
    font-weight: 700;
    font-size: 14px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 8px;
    transition: all 0.3s ease;
}

.audio-btn:hover {
    background: rgba(139, 92, 246, 0.25);
    color: #fff;
    border-color: rgba(139, 92, 246, 0.5);
    box-shadow: 0 0 12px rgba(139, 92, 246, 0.25);
}

.audio-btn.playing {
    background: var(--btn-primary-bg);
    color: #fff;
    border-color: transparent;
    animation: pulseButton 2.5s infinite alternate;
}

@keyframes pulseButton {
    0% { transform: scale(1); box-shadow: 0 4px 15px rgba(168, 85, 247, 0.2); }
    100% { transform: scale(1.03); box-shadow: 0 6px 20px rgba(168, 85, 247, 0.45); }
}

/* Choices Styles */
.choices-container h3 {
    font-family: var(--font-heading);
    font-size: 14.5px;
    text-transform: uppercase;
    letter-spacing: 0.8px;
    color: var(--color-text-muted);
    margin-bottom: 12px;
}

.choices-grid {
    display: flex;
    flex-direction: column;
    gap: 12px;
    margin-bottom: 25px;
}

.choice-btn {
    background: var(--btn-choice-bg);
    border: 1px solid var(--btn-choice-border);
    padding: 14px 20px;
    border-radius: var(--radius-sm);
    color: #fff;
    font-family: var(--font-body);
    font-weight: 700;
    font-size: 14.5px;
    text-align: left;
    cursor: pointer;
    display: flex;
    justify-content: space-between;
    align-items: center;
    transition: all 0.3s cubic-bezier(0.165, 0.84, 0.44, 1);
}

.choice-btn:hover {
    background: rgba(139, 92, 246, 0.2);
    border-color: var(--color-magic-purple);
    transform: translateX(5px);
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.25);
}

.choice-btn::after {
    content: '➔';
    font-size: 12px;
    color: var(--color-magic-purple);
    transition: transform 0.3s ease;
}

.choice-btn:hover::after {
    transform: translateX(4px);
}

.choice-btn.final-sleep-btn {
    background: linear-gradient(135deg, rgba(251, 191, 36, 0.2) 0%, rgba(217, 119, 6, 0.1) 100%);
    border-color: rgba(251, 191, 36, 0.4);
    color: var(--color-gold);
    justify-content: center;
    font-weight: 700;
    font-size: 15px;
}

.choice-btn.final-sleep-btn:hover {
    background: linear-gradient(135deg, rgba(251, 191, 36, 0.3) 0%, rgba(217, 119, 6, 0.25) 100%);
    border-color: var(--color-gold);
}

.choice-btn.final-sleep-btn::after {
    display: none;
}

.navigation-controls {
    border-top: 1px solid rgba(255, 255, 255, 0.05);
    padding-top: 15px;
}

/* ==========================================================================
   ENDING SCREEN
   ========================================================================== */

.ending-box {
    max-width: 620px;
    margin: 0 auto;
    background: rgba(12, 16, 38, 0.65);
    border: 1px solid rgba(139, 92, 246, 0.22);
    padding: 40px;
    border-radius: var(--radius-md);
    text-align: center;
    box-shadow: var(--panel-glow);
}

.ending-header {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 15px;
    margin-bottom: 22px;
}

.ending-header h2 {
    font-family: var(--font-heading);
    font-size: 26px;
    color: var(--color-gold);
    text-shadow: var(--color-gold-glow);
}

.ending-illustration {
    width: 100%;
    max-width: 440px;
    aspect-ratio: 3/2;
    border-radius: var(--radius-sm);
    overflow: hidden;
    margin: 0 auto 22px auto;
    border: 3px solid rgba(255, 255, 255, 0.06);
    box-shadow: 0 10px 30px rgba(0,0,0,0.5);
}

.ending-illustration img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.ending-text-box {
    font-size: 16.5px;
    line-height: 1.65;
    color: var(--color-text-main);
    margin-bottom: 25px;
}

.ending-reward {
    background: rgba(251, 191, 36, 0.08);
    border: 1px dotted rgba(251, 191, 36, 0.3);
    padding: 15px;
    border-radius: var(--radius-sm);
    margin-bottom: 30px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
}

.ending-reward p {
    font-family: var(--font-heading);
    color: var(--color-gold);
    font-size: 14.5px;
}

.reward-star-animation {
    font-size: 44px;
    animation: starPulse 1.8s ease-in-out infinite alternate;
}

@keyframes starPulse {
    0% { transform: scale(0.85); filter: drop-shadow(0 0 5px rgba(251, 191, 36, 0.3)); }
    100% { transform: scale(1.15); filter: drop-shadow(0 0 18px rgba(251, 191, 36, 0.85)); }
}

/* ==========================================================================
   BUTTONS
   ========================================================================== */

.btn-primary, 
.btn-secondary {
    font-family: var(--font-body);
    font-weight: 700;
    font-size: 15px;
    padding: 13px 30px;
    border-radius: 30px;
    cursor: pointer;
    transition: all 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    border: none;
    outline: none;
}

.btn-primary {
    background: var(--btn-primary-bg);
    color: #fff;
    box-shadow: 0 6px 22px rgba(168, 85, 247, 0.35);
}

.btn-primary:hover {
    background: var(--btn-primary-hover);
    transform: translateY(-2px);
    box-shadow: 0 8px 28px rgba(168, 85, 247, 0.55);
}

.btn-secondary {
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid rgba(255, 255, 255, 0.1);
    color: var(--color-text-muted);
}

.btn-secondary:hover {
    background: rgba(255, 255, 255, 0.1);
    color: #fff;
    border-color: rgba(255, 255, 255, 0.2);
}

/* ==========================================================================
   FOOTER
   ========================================================================== */

.app-footer {
    text-align: center;
    padding-top: 25px;
    border-top: 1px solid rgba(255, 255, 255, 0.04);
    margin-top: 25px;
    font-size: 12px;
    color: var(--color-text-muted);
}

/* ==========================================================================
   MEDIA QUERIES (RESPONSIVE)
   ========================================================================== */

@media (max-width: 950px) {
    .app-container {
        padding: 25px;
        margin: 12px;
    }
    
    .story-layout {
        grid-template-columns: 1fr;
        gap: 25px;
    }
    
    .image-wrapper {
        max-width: 100%;
    }
    
    .text-scroll-box {
        max-height: 160px;
    }
    
    .sound-toggles-grid {
        grid-template-columns: repeat(2, 1fr);
    }
}

/* ==========================================================================
   V2 ADDITIONS: reading comfort, hands-free mode, dream progress, modal
   ========================================================================== */

/* --- Story text: comfy short paragraphs --- */
.story-narration p {
    margin-bottom: 14px;
}

.story-narration p:last-child {
    margin-bottom: 0;
}

/* --- TTS follow-along word highlighting --- */
.tts-word {
    border-radius: 4px;
    transition: background 0.15s ease, color 0.15s ease;
}

.tts-word.speaking {
    background: rgba(251, 191, 36, 0.28);
    color: #fff;
    box-shadow: 0 0 10px rgba(251, 191, 36, 0.25);
}

/* --- Audio controls row: auto-read + font size --- */
.audio-controls {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 10px;
}

.audio-btn.auto-read-btn.on {
    background: var(--btn-primary-bg);
    color: #fff;
    border-color: transparent;
    box-shadow: 0 0 14px rgba(168, 85, 247, 0.4);
}

.font-size-controls {
    display: flex;
    gap: 4px;
    margin-left: auto;
    background: rgba(255, 255, 255, 0.04);
    border: 1px solid rgba(255, 255, 255, 0.08);
    border-radius: 30px;
    padding: 4px;
}

.font-size-btn {
    background: transparent;
    border: none;
    color: var(--color-text-muted);
    font-family: var(--font-body);
    font-weight: 700;
    font-size: 12.5px;
    padding: 6px 12px;
    border-radius: 20px;
    cursor: pointer;
    transition: all 0.25s ease;
}

.font-size-btn:hover {
    color: #fff;
    background: rgba(255, 255, 255, 0.08);
}

.font-size-btn.active {
    background: rgba(168, 85, 247, 0.3);
    color: #fff;
    box-shadow: 0 0 8px rgba(168, 85, 247, 0.3);
}

/* --- Choice buttons with picture icons for pre-readers --- */
.choice-btn {
    justify-content: flex-start;
    gap: 12px;
}

.choice-icon {
    font-size: 22px;
    flex-shrink: 0;
    filter: drop-shadow(0 0 6px rgba(139, 92, 246, 0.35));
}

.choice-label {
    flex-grow: 1;
}

/* --- Story cards: dream endings meter --- */
.story-card-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
    flex-wrap: wrap;
}

.endings-meter {
    font-size: 12px;
    font-weight: 700;
    color: var(--color-text-muted);
    background: rgba(255, 255, 255, 0.04);
    border: 1px solid rgba(255, 255, 255, 0.07);
    padding: 5px 10px;
    border-radius: 20px;
    white-space: nowrap;
}

.endings-meter.started {
    color: var(--color-magic-purple);
    border-color: rgba(167, 139, 250, 0.3);
}

.story-card.story-complete {
    border-color: rgba(251, 191, 36, 0.4);
}

.story-card.story-complete .endings-meter {
    color: var(--color-gold);
    border-color: rgba(251, 191, 36, 0.35);
    background: rgba(251, 191, 36, 0.08);
}

/* --- Category tab badge when every story in it has been dreamed --- */
.tab-btn.category-complete::after {
    content: ' ✨';
}

/* --- Dream Sky constellation progress --- */
.dream-sky {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 12px;
    background: rgba(8, 12, 30, 0.55);
    border: 1px solid rgba(139, 92, 246, 0.12);
    border-radius: var(--radius-sm);
    padding: 12px 18px;
    margin-bottom: 22px;
}

.dream-sky-stars {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    flex-grow: 1;
}

.sky-star {
    font-size: 15px;
    color: rgba(255, 255, 255, 0.14);
    transition: color 0.4s ease, text-shadow 0.4s ease;
    cursor: default;
}

.sky-star.lit {
    color: var(--color-magic-purple);
    text-shadow: 0 0 8px rgba(167, 139, 250, 0.6);
}

.sky-star.complete {
    color: var(--color-gold);
    text-shadow: 0 0 10px rgba(251, 191, 36, 0.7);
}

.dream-sky-label {
    font-family: var(--font-heading);
    font-size: 12.5px;
    color: var(--color-text-muted);
    white-space: nowrap;
}

/* --- Cozy quit-story modal (replaces browser confirm) --- */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(2, 4, 12, 0.75);
    backdrop-filter: blur(6px);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 10000;
    opacity: 1;
    transition: opacity 0.3s ease;
}

.modal-overlay.hidden {
    opacity: 0;
    pointer-events: none;
    visibility: hidden;
}

.modal-box {
    background: var(--panel-bg);
    border: 1px solid var(--panel-border);
    border-radius: var(--radius-md);
    box-shadow: var(--panel-glow);
    padding: 32px 36px;
    max-width: 380px;
    width: calc(100% - 40px);
    text-align: center;
}

.modal-emoji {
    font-size: 40px;
    display: block;
    margin-bottom: 12px;
}

.modal-box h3 {
    font-family: var(--font-heading);
    font-size: 20px;
    color: #fff;
    margin-bottom: 10px;
}

.modal-box p {
    font-size: 14.5px;
    color: var(--color-text-muted);
    line-height: 1.6;
    margin-bottom: 24px;
}

.modal-actions {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

/* --- Deep sleep tint (dimmer above 45%) --- */
body.deep-sleep-tint .app-container {
    filter: brightness(0.78) saturate(0.75);
    transition: filter 1.2s ease;
}

body.deep-sleep-tint .stars {
    opacity: 0.35;
}

/* --- Library status messages --- */
.error-msg,
.info-msg {
    grid-column: 1 / -1;
    text-align: center;
    padding: 30px 20px;
    border-radius: var(--radius-sm);
    font-weight: 600;
    font-size: 14.5px;
}

.error-msg {
    color: #fda4af;
    background: rgba(244, 63, 94, 0.08);
    border: 1px solid rgba(244, 63, 94, 0.25);
}

.info-msg {
    color: var(--color-text-muted);
    background: rgba(255, 255, 255, 0.03);
    border: 1px dashed rgba(255, 255, 255, 0.12);
}

/* --- Keyboard focus visibility --- */
button:focus-visible,
input:focus-visible,
select:focus-visible {
    outline: 2px solid var(--color-magic-purple);
    outline-offset: 2px;
}

/* --- Respect reduced motion preferences (decorative animations only) --- */
@media (prefers-reduced-motion: reduce) {
    .stars.small,
    .stars.medium,
    .stars.large,
    .reward-star-animation,
    .audio-btn.playing {
        animation: none;
    }

    .screen,
    .story-card,
    .choice-btn {
        transition: none;
    }
}

/* --- Small screens: keep the reading controls tidy --- */
@media (max-width: 950px) {
    .font-size-controls {
        margin-left: 0;
    }

    .modal-actions {
        flex-direction: column;
    }
}
