/* ============================================
   StorySynth - Interactive Storytelling App
   Brand Colors:
   - Storybook Blue: #4A90E2
   - Imagination Yellow: #FFD93D
   - Magic Purple: #9B51E0
   - Friendly Teal: #27AE60
   - Soft Chalk: #F9F9F9
   ============================================ */

/* CSS Custom Properties - Night Mode */
:root {
    --storybook-blue: #5D9CEC;
    /* Brighter blue for dark bg */
    --imagination-yellow: #FFD93D;
    --magic-purple: #AC6DDE;
    /* Lighter purple for dark bg */
    --friendly-teal: #48C774;
    /* Brighter teal */
    --soft-chalk: #2D2F55;
    /* Dark Card Background */
    --text-dark: #F0F3F5;
    /* Light Text */
    --text-light: #B8B9C8;
    /* Muted Text */
    --text-placeholder: #8A8D9F;
    --white: #1A1B41;
    /* Page Background (Dark Twilight) */
    --input-bg: #232544;
    --border-color: #4A4D6E;

    --shadow-light: rgba(0, 0, 0, 0.3);
    --shadow-medium: rgba(0, 0, 0, 0.5);
    --border-radius: 12px;
    --border-radius-large: 20px;
    --transition-speed: 0.3s;
}

/* Reset & Base Styles */
*,
*::before,
*::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html {
    font-size: 16px;
}

body {
    font-family: 'Quicksand', sans-serif;
    background: linear-gradient(135deg, #1A1B41 0%, #242551 100%);
    color: var(--text-dark);
    line-height: 1.5;
    min-height: 100vh;
}

/* App Container */
.app-container {
    max-width: 800px;
    margin: 0 auto;
    padding: 20px;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

/* Header */
.header {
    text-align: center;
    padding: 30px 20px;
    animation: fadeInDown 0.6s ease-out;
}

.logo {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
    margin-bottom: 10px;
}

.logo-icon {
    font-size: 2.5rem;
    animation: sparkle 2s ease-in-out infinite;
}

.logo h1 {
    font-size: 2.5rem;
    font-weight: 700;
    color: var(--storybook-blue);
    text-shadow: 2px 2px 0 var(--imagination-yellow);
}

.tagline {
    font-size: 1.25rem;
    color: var(--magic-purple);
    font-weight: 500;
}

/* Step Indicator */
.step-indicator {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    padding: 20px;
    margin-bottom: 30px;
}

.step {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
    opacity: 0.5;
    transition: opacity var(--transition-speed), transform var(--transition-speed);
}

.step.active {
    opacity: 1;
    transform: scale(1.1);
}

.step.completed {
    opacity: 1;
}

.step-number {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: var(--storybook-blue);
    color: var(--white);
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 1.1rem;
    box-shadow: 0 4px 8px var(--shadow-medium);
}

.step.active .step-number {
    background: var(--magic-purple);
    animation: pulse 1.5s ease-in-out infinite;
}

.step.completed .step-number {
    background: var(--friendly-teal);
}

.step-label {
    font-size: 0.875rem;
    font-weight: 600;
    color: var(--text-dark);
}

.step-line {
    width: 40px;
    height: 4px;
    background: var(--storybook-blue);
    border-radius: 2px;
    opacity: 0.3;
}

/* Main Content */
.main-content {
    flex: 1;
    display: flex;
    flex-direction: column;
}

/* Form Sections */
.form-section {
    background: var(--white);
    border-radius: var(--border-radius-large);
    padding: 30px;
    box-shadow: 0 8px 30px var(--shadow-light);
    display: none;
    animation: fadeIn 0.4s ease-out;
}

.form-section.active {
    display: block;
}

.form-section h2 {
    font-size: 1.75rem;
    color: var(--storybook-blue);
    text-align: center;
    margin-bottom: 10px;
    font-weight: 700;
}

.section-description {
    text-align: center;
    color: var(--text-placeholder);
    font-size: 1.1rem;
    margin-bottom: 25px;
}

/* Character Count Grid (Step 1) */
.character-count-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 20px;
    max-width: 400px;
    margin: 0 auto;
}

.character-tile {
    background: var(--soft-chalk);
    border: 3px solid transparent;
    border-radius: var(--border-radius);
    padding: 25px 15px;
    cursor: pointer;
    transition: all var(--transition-speed);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    box-shadow: 0 4px 12px var(--shadow-light);
    font-family: inherit;
}

.character-tile:hover {
    transform: translateY(-4px);
    box-shadow: 0 8px 20px var(--shadow-medium);
    border-color: var(--magic-purple);
}

.character-tile.selected {
    background: linear-gradient(135deg, var(--magic-purple), #B476E5);
    border-color: var(--magic-purple);
    color: var(--white);
}

.tile-number {
    font-size: 2.5rem;
    font-weight: 700;
}

.tile-label {
    font-size: 1rem;
    font-weight: 600;
}

/* Character Forms (Step 2) */
.character-forms {
    display: flex;
    flex-direction: column;
    gap: 20px;
    margin-bottom: 25px;
}

.character-form-item {
    background: var(--soft-chalk);
    border-radius: var(--border-radius);
    padding: 20px;
    animation: slideIn 0.3s ease-out;
    display: flex;
    flex-direction: column;
    gap: 15px;
    border: 2px solid var(--border-color);
}

.character-form-header {
    display: flex;
    align-items: center;
    gap: 10px;
    color: var(--storybook-blue);
    font-weight: 700;
    font-size: 1.1rem;
    margin-bottom: 5px;
}

.character-form-group {
    display: flex;
    flex-direction: column;
    gap: 15px;
}

.character-field {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.character-label {
    font-size: 0.95rem;
    font-weight: 600;
    color: var(--imagination-yellow);
    /* Pop color on dark */
}

.character-input,
.character-textarea {
    width: 100%;
    padding: 12px 15px;
    font-size: 1rem;
    font-family: inherit;
    border: 2px solid var(--border-color);
    border-radius: var(--border-radius);
    background: var(--input-bg);
    color: var(--text-dark);
    transition: all var(--transition-speed);
}

.character-textarea {
    min-height: 80px;
    resize: vertical;
}

.character-input:focus,
.character-textarea:focus {
    outline: none;
    border-color: var(--magic-purple);
    box-shadow: 0 0 0 4px rgba(155, 81, 224, 0.1);
    border-width: 4px;
}

.character-input::placeholder {
    color: var(--text-placeholder);
}

.character-input:focus {
    outline: none;
    border-width: 4px;
    box-shadow: 0 0 0 4px rgba(74, 144, 226, 0.2);
}

/* Location Grid (Step 3) */
.location-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 15px;
    margin-bottom: 25px;
}

.location-tile {
    background: var(--soft-chalk);
    border: 3px solid transparent;
    border-radius: var(--border-radius);
    padding: 20px 15px;
    cursor: pointer;
    transition: all var(--transition-speed);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
    box-shadow: 0 4px 12px var(--shadow-light);
    font-family: inherit;
}

.location-tile:hover {
    transform: translateY(-3px);
    box-shadow: 0 8px 20px var(--shadow-medium);
    border-color: var(--storybook-blue);
}

.location-tile.selected {
    border-color: var(--magical-purple);
    background: linear-gradient(135deg, #2D2F55, #3A3D6E);
    box-shadow: 0 0 0 3px rgba(172, 109, 222, 0.4);
}

.location-emoji {
    font-size: 2.5rem;
}

.location-name {
    font-size: 0.95rem;
    font-weight: 600;
    text-align: center;
    color: var(--text-dark);
}

/* Navigation Buttons */
.navigation-buttons {
    display: flex;
    justify-content: space-between;
    gap: 15px;
    margin-top: 20px;
}

.nav-btn {
    padding: 15px 30px;
    font-size: 1.1rem;
    font-weight: 600;
    font-family: inherit;
    border: none;
    border-radius: var(--border-radius);
    cursor: pointer;
    transition: all var(--transition-speed);
    display: flex;
    align-items: center;
    gap: 8px;
    box-shadow: 0 4px 12px var(--shadow-light);
}

.nav-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 16px var(--shadow-medium);
}

.nav-btn:active {
    transform: translateY(0);
}

.back-btn {
    background: transparent;
    color: var(--text-dark);
    border: 2px solid var(--border-color);
}

.back-btn:hover {
    background: rgba(255, 255, 255, 0.05);
    border-color: var(--storybook-blue);
}

.next-btn {
    background: var(--friendly-teal);
    color: var(--white);
    margin-left: auto;
}

.next-btn:hover {
    background: #219653;
}

.start-btn {
    background: linear-gradient(135deg, var(--storybook-blue), #357ABD);
    color: var(--white);
    margin-left: auto;
    padding: 18px 35px;
    font-size: 1.2rem;
}

.start-btn:hover {
    background: linear-gradient(135deg, #357ABD, var(--storybook-blue));
}

.start-again-btn {
    background: var(--soft-chalk);
    color: var(--text-dark);
    border: 2px solid var(--storybook-blue);
    margin: 0 auto;
}

/* Home Section */
.home-container {
    text-align: center;
    padding: 50px 20px;
}

.home-illustration {
    position: relative;
    margin-bottom: 30px;
}

.home-book {
    font-size: 6rem;
    display: block;
    animation: bookFloat 3s ease-in-out infinite;
}

.home-sparkles {
    position: absolute;
    top: -10px;
    left: 50%;
    transform: translateX(-50%);
    width: 200px;
    display: flex;
    justify-content: space-around;
}

.home-sparkles span {
    font-size: 1.5rem;
    animation: sparkleFloat 1.5s ease-in-out infinite;
}

.home-sparkles span:nth-child(1) {
    animation-delay: 0s;
}

.home-sparkles span:nth-child(2) {
    animation-delay: 0.3s;
}

.home-sparkles span:nth-child(3) {
    animation-delay: 0.6s;
}

.home-container h2 {
    color: var(--storybook-blue);
    font-size: 2rem;
    margin-bottom: 15px;
}

.home-description {
    color: var(--text-placeholder);
    font-size: 1.2rem;
    margin-bottom: 35px;
    max-width: 400px;
    margin-left: auto;
    margin-right: auto;
}

.home-start-btn {
    display: block;
    margin: 0 auto;
    background: linear-gradient(135deg, var(--storybook-blue), #357ABD);
    color: var(--white);
    padding: 20px 50px;
    font-size: 1.4rem;
    border-radius: 50px;
    box-shadow: 0 8px 25px rgba(74, 144, 226, 0.4);
}

.home-start-btn:hover {
    background: linear-gradient(135deg, #357ABD, var(--storybook-blue));
    transform: translateY(-4px);
    box-shadow: 0 12px 30px rgba(74, 144, 226, 0.5);
}

/* Custom Location Input */
.custom-location-container {
    margin-top: 25px;
    padding-top: 20px;
    border-top: 2px dashed var(--imagination-yellow);
}

.custom-location-label {
    color: var(--magic-purple);
    font-size: 1rem;
    font-weight: 600;
    margin-bottom: 12px;
    text-align: center;
}

.custom-location-input {
    width: 100%;
    padding: 15px 18px;
    font-size: 1rem;
    font-family: inherit;
    border: 2px solid var(--border-color);
    border-radius: var(--border-radius);
    background: var(--input-bg);
    color: var(--text-dark);
    transition: all var(--transition-speed);
}

.custom-location-input::placeholder {
    color: var(--text-placeholder);
}

.custom-location-input:focus {
    outline: none;
    border-width: 4px;
    box-shadow: 0 0 0 4px rgba(74, 144, 226, 0.2);
}

.custom-location-input.active {
    border-color: var(--imagination-yellow);
    background: var(--input-bg);
}

/* Fresh Start Banner */
.fresh-start-banner {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
    padding: 15px 20px;
    background: linear-gradient(135deg, var(--imagination-yellow), #FFE066);
    border-radius: var(--border-radius);
    margin-bottom: 20px;
    box-shadow: 0 4px 15px rgba(255, 217, 61, 0.3);
    animation: slideDown 0.4s ease-out;
    transition: opacity 0.3s ease;
}

.fresh-start-banner .fresh-icon {
    font-size: 1.5rem;
}

.fresh-start-banner .fresh-text {
    font-size: 1.05rem;
    font-weight: 600;
    color: var(--text-dark);
}

@keyframes slideDown {
    from {
        transform: translateY(-20px);
        opacity: 0;
    }

    to {
        transform: translateY(0);
        opacity: 1;
    }
}

/* Loading State - Enhanced Animation */
.loading-container {
    text-align: center;
    padding: 40px 20px;
}

.story-loading-animation {
    margin-bottom: 30px;
}

.magic-book {
    position: relative;
    width: 100px;
    height: 80px;
    margin: 0 auto;
}

.book-cover {
    width: 100px;
    height: 80px;
    background: linear-gradient(135deg, var(--magic-purple), #B476E5);
    border-radius: 8px 15px 15px 8px;
    box-shadow: 0 8px 20px rgba(155, 81, 224, 0.3);
    animation: bookFloat 2s ease-in-out infinite;
}

.book-cover::before {
    content: '📖';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 2.5rem;
}

.sparkles {
    position: absolute;
    top: -10px;
    left: 50%;
    transform: translateX(-50%);
    width: 140px;
    display: flex;
    justify-content: space-around;
}

.sparkles span {
    font-size: 1.2rem;
    animation: sparkleFloat 1.5s ease-in-out infinite;
}

.sparkles span:nth-child(1) {
    animation-delay: 0s;
}

.sparkles span:nth-child(2) {
    animation-delay: 0.2s;
}

.sparkles span:nth-child(3) {
    animation-delay: 0.4s;
}

.sparkles span:nth-child(4) {
    animation-delay: 0.6s;
}

.sparkles span:nth-child(5) {
    animation-delay: 0.8s;
}

.loading-title {
    color: var(--storybook-blue);
    font-size: 1.5rem;
    margin-bottom: 10px;
}

.loading-text {
    color: var(--text-placeholder);
    font-size: 1.1rem;
    margin-bottom: 30px;
}

.loading-steps {
    display: flex;
    flex-direction: column;
    gap: 12px;
    max-width: 280px;
    margin: 0 auto;
}

.loading-step {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 16px;
    background: var(--soft-chalk);
    border-radius: var(--border-radius);
    opacity: 0.4;
    transition: all 0.5s ease;
}

.loading-step.active {
    opacity: 1;
    background: linear-gradient(135deg, #E8F4FD, var(--white));
    box-shadow: 0 4px 12px var(--shadow-light);
}

.loading-step.completed {
    opacity: 1;
    background: linear-gradient(135deg, #E8F9EE, var(--white));
}

.loading-step.completed .step-icon::after {
    content: '✓';
    position: absolute;
    right: -5px;
    bottom: -2px;
    font-size: 0.7rem;
    color: var(--friendly-teal);
}

.loading-step .step-icon {
    font-size: 1.5rem;
    position: relative;
}

.loading-step span:last-child {
    font-size: 0.95rem;
    font-weight: 500;
    color: var(--text-dark);
}

/* Active Story Exists Section */
.active-story-container {
    text-align: center;
    padding: 40px 20px;
}

.active-story-icon {
    font-size: 5rem;
    margin-bottom: 20px;
    animation: bounce 0.6s ease-out;
}

.active-story-container h2 {
    color: var(--magic-purple);
    margin-bottom: 15px;
}

.active-story-message {
    color: var(--text-placeholder);
    font-size: 1.1rem;
    margin-bottom: 30px;
}

.active-story-buttons {
    display: flex;
    flex-direction: column;
    gap: 15px;
    max-width: 300px;
    margin: 0 auto;
}

.resume-btn {
    background: linear-gradient(135deg, var(--friendly-teal), #219653);
    color: var(--white);
}

.resume-btn:hover {
    background: linear-gradient(135deg, #219653, var(--friendly-teal));
}

.restart-btn {
    background: var(--soft-chalk);
    color: var(--text-dark);
    border: 2px solid var(--magic-purple);
}

.restart-btn:hover {
    background: #F3E8FC;
}

/* Story Display Section */
.story-display {
    padding: 10px 0;
}

.story-header {
    text-align: center;
    margin-bottom: 25px;
    padding-bottom: 20px;
    border-bottom: 2px dashed var(--imagination-yellow);
}

.story-title {
    color: var(--storybook-blue);
    font-size: 1.8rem;
    margin-bottom: 8px;
}

.episode-title {
    color: var(--magic-purple);
    font-size: 1.1rem;
    font-weight: 600;
}

/* Story Image */
.story-image-container {
    position: relative;
    width: 100%;
    max-width: 500px;
    margin: 0 auto 25px;
    border-radius: var(--border-radius-large);
    overflow: hidden;
    box-shadow: 0 8px 30px var(--shadow-medium);
}

.story-image {
    width: 100%;
    height: auto;
    display: block;
    border-radius: var(--border-radius-large);
}

.image-loading-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(135deg, #E8F4FD, var(--soft-chalk));
    display: flex;
    align-items: center;
    justify-content: center;
}

.image-loading-overlay.hidden {
    display: none;
}

.image-spinner {
    width: 50px;
    height: 50px;
    border: 4px solid var(--soft-chalk);
    border-top-color: var(--magic-purple);
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

/* Story Text */
.story-text-container {
    background: var(--soft-chalk);
    border-radius: var(--border-radius);
    padding: 25px;
    margin-bottom: 25px;
    border-left: 4px solid var(--storybook-blue);
}

.story-text {
    font-size: 1.15rem;
    line-height: 1.8;
    color: var(--text-dark);
    white-space: pre-wrap;
}

/* Audio Progress Bar - Top of Story Section */
.audio-progress-bar {
    position: sticky;
    top: 0;
    left: 0;
    right: 0;
    height: 6px;
    background: var(--soft-chalk);
    border-radius: 3px;
    margin-bottom: 20px;
    overflow: hidden;
    z-index: 100;
}

.audio-progress-fill {
    height: 100%;
    width: 0%;
    background: linear-gradient(90deg, var(--magic-purple), var(--storybook-blue));
    border-radius: 3px;
    transition: width 0.1s linear;
}

/* Floating Audio Controls - Right Side */
.floating-audio-controls {
    position: fixed;
    right: 20px;
    top: 50%;
    transform: translateY(-50%);
    display: flex;
    flex-direction: column;
    gap: 12px;
    z-index: 1000;
}

.floating-play-btn {
    width: 65px;
    height: 65px;
    border-radius: 50%;
    border: none;
    background: linear-gradient(135deg, var(--imagination-yellow), #FFE066);
    box-shadow: 0 6px 20px rgba(255, 217, 61, 0.5);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all var(--transition-speed);
}

.floating-play-btn:hover {
    transform: scale(1.1);
    box-shadow: 0 8px 25px rgba(255, 217, 61, 0.6);
}

.floating-play-btn.playing {
    background: linear-gradient(135deg, var(--friendly-teal), #219653);
    box-shadow: 0 6px 20px rgba(39, 174, 96, 0.5);
}

.floating-play-btn.playing:hover {
    box-shadow: 0 8px 25px rgba(39, 174, 96, 0.6);
}

.floating-play-icon {
    font-size: 1.8rem;
}

.floating-restart-btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    padding: 12px 16px;
    border-radius: var(--border-radius);
    border: 2px solid var(--storybook-blue);
    background: var(--white);
    color: var(--storybook-blue);
    cursor: pointer;
    font-family: inherit;
    transition: all var(--transition-speed);
}

.floating-restart-btn:hover {
    background: var(--storybook-blue);
    color: var(--white);
}

.floating-restart-btn .restart-icon {
    font-size: 1.2rem;
}

.floating-restart-btn .restart-label {
    font-size: 0.75rem;
    font-weight: 600;
}

/* Hide floating controls when audio not available */
.floating-audio-controls.hidden {
    display: none;
}

/* Responsive - move controls below on mobile */
@media (max-width: 768px) {
    .floating-audio-controls {
        position: fixed;
        right: 15px;
        bottom: 20px;
        top: auto;
        transform: none;
        flex-direction: row;
    }

    .floating-play-btn {
        width: 55px;
        height: 55px;
    }

    .floating-play-icon {
        font-size: 1.5rem;
    }

    .floating-restart-btn {
        padding: 10px 12px;
    }
}

/* Story Choices */
.story-choices {
    margin-top: 30px;
    padding-top: 25px;
    border-top: 2px dashed var(--imagination-yellow);
}

.choices-title {
    text-align: center;
    color: var(--magic-purple);
    font-size: 1.3rem;
    margin-bottom: 20px;
}

.choices-container {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.choice-btn {
    width: 100%;
    padding: 18px 25px;
    background: linear-gradient(135deg, var(--friendly-teal), #219653);
    color: var(--white);
    border: none;
    border-radius: var(--border-radius);
    font-size: 1.05rem;
    font-weight: 600;
    font-family: inherit;
    cursor: pointer;
    text-align: left;
    display: flex;
    align-items: center;
    gap: 15px;
    box-shadow: 0 4px 15px rgba(39, 174, 96, 0.3);
    transition: all var(--transition-speed);
}

.choice-btn:hover {
    transform: translateX(8px);
    box-shadow: 0 6px 20px rgba(39, 174, 96, 0.4);
}

.choice-btn:active {
    transform: translateX(4px);
}

.choice-number {
    width: 35px;
    height: 35px;
    background: rgba(255, 255, 255, 0.2);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    flex-shrink: 0;
}

.choice-text {
    flex: 1;
}

/* Error State */
.error-container {
    text-align: center;
    padding: 40px;
}

.error-icon {
    font-size: 5rem;
    margin-bottom: 20px;
}

.error-text {
    color: #E74C3C;
    font-size: 1.1rem;
    margin-bottom: 30px;
}

/* Additional Animations */
@keyframes bookFloat {

    0%,
    100% {
        transform: translateY(0) rotate(-2deg);
    }

    50% {
        transform: translateY(-10px) rotate(2deg);
    }
}

@keyframes sparkleFloat {

    0%,
    100% {
        transform: translateY(0) scale(1);
        opacity: 1;
    }

    50% {
        transform: translateY(-15px) scale(1.2);
        opacity: 0.6;
    }
}

/* Footer */
.footer {
    text-align: center;
    padding: 20px;
    color: var(--text-placeholder);
    font-size: 0.9rem;
}

/* Animations */
@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes fadeInDown {
    from {
        opacity: 0;
        transform: translateY(-20px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes slideIn {
    from {
        opacity: 0;
        transform: translateX(-20px);
    }

    to {
        opacity: 1;
        transform: translateX(0);
    }
}

@keyframes pulse {

    0%,
    100% {
        box-shadow: 0 0 0 0 rgba(155, 81, 224, 0.4);
    }

    50% {
        box-shadow: 0 0 0 10px rgba(155, 81, 224, 0);
    }
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

@keyframes bounce {

    0%,
    100% {
        transform: translateY(0);
    }

    50% {
        transform: translateY(-15px);
    }
}

@keyframes sparkle {

    0%,
    100% {
        transform: scale(1) rotate(0deg);
    }

    50% {
        transform: scale(1.1) rotate(10deg);
    }
}

/* Finale Section (End of Story) */
.finale-container {
    text-align: center;
    padding: 20px 0;
    max-width: 600px;
    margin: 0 auto;
}

.finale-header {
    margin-bottom: 30px;
}

.finale-sparkles {
    font-size: 2rem;
    margin-bottom: 10px;
    animation: sparkle 2s ease-in-out infinite;
}

.finale-title {
    display: none;
    /* Removed from top, moved to signature below text */
}

.finale-the-end {
    font-size: 2.2rem;
    color: var(--magic-purple);
    font-family: 'Quicksand', sans-serif;
    font-style: italic;
    font-weight: 700;
    margin: 20px 0 10px;
    animation: fadeIn 1s ease-out;
}

.finale-subtitle {
    font-size: 1.25rem;
    color: var(--magic-purple);
    font-weight: 600;
}

.finale-image-container {
    position: relative;
    width: 100%;
    max-width: 500px;
    margin: 0 auto 25px;
    border-radius: var(--border-radius-large);
    overflow: hidden;
    box-shadow: 0 8px 30px var(--shadow-medium);
}

.finale-image {
    width: 100%;
    height: auto;
    display: block;
    border-radius: var(--border-radius-large);
}

.finale-overlay {
    position: absolute;
    top: 20px;
    right: 20px;
}

.finale-crown {
    font-size: 3rem;
    filter: drop-shadow(0 4px 8px rgba(0, 0, 0, 0.3));
    animation: bounce 2s ease-in-out infinite;
}

.finale-content {
    background: var(--soft-chalk);
    padding: 30px;
    border-radius: var(--border-radius);
    border: 2px solid var(--border-color);
}

.finale-text-container {
    background: var(--soft-chalk);
    border-radius: var(--border-radius);
    padding: 25px;
    margin-bottom: 25px;
    border-left: 4px solid var(--magic-purple);
    text-align: left;
}

.finale-story-text {
    font-size: 1.15rem;
    line-height: 1.8;
    color: var(--text-dark);
    white-space: pre-wrap;
}

.finale-congrats {
    font-size: 1.2rem;
    color: var(--friendly-teal);
    font-weight: 600;
    margin-bottom: 25px;
    line-height: 1.6;
    animation: fadeIn 1s ease-out;
}

.finale-buttons {
    display: flex;
    justify-content: center;
    gap: 15px;
    flex-wrap: wrap;
}

.download-btn {
    padding: 18px 30px;
    font-size: 1.1rem;
    background: linear-gradient(135deg, var(--storybook-blue), #3E7BCE);
    color: white;
    border: none;
    box-shadow: 0 8px 20px rgba(74, 144, 226, 0.3);
}

.download-btn:hover {
    background: linear-gradient(135deg, #3E7BCE, var(--storybook-blue));
    transform: translateY(-4px);
    box-shadow: 0 12px 25px rgba(74, 144, 226, 0.4);
}

.finale-buttons .restart-btn {
    padding: 18px 30px;
    font-size: 1.1rem;
    background: linear-gradient(135deg, var(--magic-purple), #8E44AD);
    color: white;
    border: none;
    box-shadow: 0 8px 20px rgba(155, 81, 224, 0.3);
}

.finale-buttons .restart-btn:hover {
    background: linear-gradient(135deg, #8E44AD, var(--magic-purple));
    transform: translateY(-4px);
    box-shadow: 0 12px 25px rgba(155, 81, 224, 0.5);
}

/* PDF Export Styling (Storybook Template) */
.pdf-template {
    padding: 40px;
    background: white;
    color: #333;
    font-family: 'Quicksand', sans-serif;
}

.pdf-title-page {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 257mm;
    text-align: center;
}

.pdf-story-title {
    font-size: 40px;
    color: #4A90E2;
    margin-bottom: 20px;
    border-bottom: 3px solid #FFD93D;
    padding-bottom: 20px;
}

.pdf-subtitle {
    font-size: 18px;
    color: #9B51E0;
    font-style: italic;
    margin: 0;
}

.pdf-episode {
    margin-bottom: 50px;
}

.pdf-episode-title {
    font-size: 24px;
    color: #9B51E0;
    margin-bottom: 20px;
    text-align: center;
}

.pdf-image-container {
    width: 100%;
    margin-bottom: 25px;
    text-align: center;
}

.pdf-image {
    max-width: 100%;
    max-height: 400px;
    border-radius: 15px;
    border: 3px solid #4A90E2;
}

.pdf-text {
    font-size: 18px;
    line-height: 1.8;
    color: #444;
    white-space: pre-wrap;
    background: #F9F9F9;
    padding: 20px;
    border-radius: 10px;
}

.pdf-footer {
    text-align: center;
    font-style: italic;
    color: #9B51E0;
    font-size: 28px;
    margin-top: 40px;
}

/* Responsive Design */
@media (max-width: 600px) {
    .app-container {
        padding: 15px;
    }

    .header {
        padding: 20px 15px;
    }

    .logo h1 {
        font-size: 2rem;
    }

    .tagline {
        font-size: 1rem;
    }

    .step-indicator {
        gap: 5px;
    }

    .step-number {
        width: 35px;
        height: 35px;
        font-size: 1rem;
    }

    .step-label {
        font-size: 0.75rem;
    }

    .step-line {
        width: 25px;
    }

    .form-section {
        padding: 20px;
    }

    .form-section h2 {
        font-size: 1.4rem;
    }

    .character-count-grid,
    .location-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 12px;
    }

    .character-tile,
    .location-tile {
        padding: 18px 12px;
    }

    .tile-number {
        font-size: 2rem;
    }

    .location-emoji {
        font-size: 2rem;
    }

    .navigation-buttons {
        flex-direction: column;
    }

    .nav-btn {
        width: 100%;
        justify-content: center;
    }

    .back-btn {
        order: 2;
    }

    .next-btn,
    .start-btn {
        order: 1;
        margin-left: 0;
    }
}

/* Accessibility - Focus States */
button:focus-visible,
input:focus-visible {
    outline: 3px solid var(--imagination-yellow);
    outline-offset: 2px;
}

/* High contrast mode support */
@media (prefers-contrast: high) {

    .character-tile,
    .location-tile {
        border-color: var(--text-dark);
    }
}

/* ============================================================
   AUTH OVERLAY
   ============================================================ */
.auth-overlay {
    position: fixed;
    inset: 0;
    background: linear-gradient(135deg, #0f1030 0%, #1a1b41 60%, #2a1060 100%);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    padding: 20px;
    animation: fadeIn 0.4s ease-out;
}

.auth-overlay.hidden {
    display: none;
}

.auth-card {
    background: rgba(45, 47, 85, 0.85);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border: 1px solid rgba(93, 156, 236, 0.25);
    border-radius: 24px;
    padding: 40px 36px 32px;
    width: 100%;
    max-width: 420px;
    box-shadow: 0 20px 60px rgba(0,0,0,0.5), 0 0 0 1px rgba(255,255,255,0.04);
    animation: slideUp 0.5s cubic-bezier(0.22, 1, 0.36, 1);
}

@keyframes slideUp {
    from { transform: translateY(30px); opacity: 0; }
    to   { transform: translateY(0);    opacity: 1; }
}

.auth-logo {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    margin-bottom: 4px;
}

.auth-logo-icon {
    font-size: 2rem;
    animation: sparkle 2s ease-in-out infinite;
}

.auth-logo h1 {
    font-size: 2rem;
    font-weight: 700;
    color: var(--storybook-blue);
    text-shadow: 2px 2px 0 var(--imagination-yellow);
}

.auth-tagline {
    text-align: center;
    color: var(--text-light);
    font-size: 1rem;
    margin-bottom: 24px;
}

/* ── Auth Tabs ─────────────────────────────────────────────── */
.auth-tabs {
    display: flex;
    gap: 4px;
    background: rgba(0,0,0,0.2);
    border-radius: 12px;
    padding: 4px;
    margin-bottom: 24px;
}

.auth-tab {
    flex: 1;
    padding: 10px;
    border: none;
    border-radius: 9px;
    font-family: inherit;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    background: transparent;
    color: var(--text-light);
    transition: all 0.25s;
}

.auth-tab.active {
    background: var(--storybook-blue);
    color: #fff;
    box-shadow: 0 3px 10px rgba(93, 156, 236, 0.4);
}

/* ── Auth Form Panels ──────────────────────────────────────── */
.auth-form-panel {
    display: none;
    flex-direction: column;
    gap: 16px;
    animation: fadeIn 0.3s ease-out;
}

.auth-form-panel.active {
    display: flex;
}

.auth-field {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.auth-field label {
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--imagination-yellow);
}

.auth-input {
    width: 100%;
    padding: 13px 16px;
    font-size: 1rem;
    font-family: inherit;
    border: 2px solid var(--border-color);
    border-radius: 12px;
    background: var(--input-bg);
    color: var(--text-dark);
    transition: all 0.25s;
}

.auth-input:focus {
    outline: none;
    border-color: var(--storybook-blue);
    box-shadow: 0 0 0 4px rgba(93, 156, 236, 0.15);
}

.auth-input::placeholder {
    color: var(--text-placeholder);
}

.auth-error {
    font-size: 0.9rem;
    color: #ff6b8a;
    min-height: 1.2em;
    text-align: center;
}

.auth-success {
    font-size: 0.9rem;
    color: #5dc89a;
    min-height: 1.2em;
    text-align: center;
}

.auth-forgot-link {
    text-align: center;
    margin: 6px 0 0;
    font-size: 0.88rem;
}

.auth-text-btn {
    background: none;
    border: none;
    color: var(--storybook-blue);
    cursor: pointer;
    font-size: inherit;
    font-family: inherit;
    padding: 0;
    text-decoration: underline;
    text-underline-offset: 2px;
}

.auth-text-btn:hover {
    color: var(--magic-purple);
}

.auth-forgot-desc {
    font-size: 0.9rem;
    color: var(--text-secondary);
    text-align: center;
    margin: 0 0 12px;
    line-height: 1.45;
}

.auth-submit-btn {
    width: 100%;
    padding: 14px;
    background: linear-gradient(135deg, var(--storybook-blue), #357ABD);
    color: #fff;
    border: none;
    border-radius: 12px;
    font-size: 1.1rem;
    font-weight: 700;
    font-family: inherit;
    cursor: pointer;
    box-shadow: 0 4px 16px rgba(93, 156, 236, 0.4);
    transition: all 0.25s;
}

.auth-submit-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 24px rgba(93, 156, 236, 0.5);
}

.auth-submit-btn:active {
    transform: translateY(0);
}

.auth-submit-btn:disabled {
    opacity: 0.6;
    cursor: not-allowed;
    transform: none;
}

.auth-tier-note {
    font-size: 0.85rem;
    text-align: center;
    color: var(--friendly-teal);
    margin-top: -8px;
}

/* ── Auth Divider & Demo Button ────────────────────────────── */
.auth-divider {
    display: flex;
    align-items: center;
    gap: 12px;
    margin: 20px 0 14px;
    color: var(--text-placeholder);
    font-size: 0.9rem;
}

.auth-divider::before,
.auth-divider::after {
    content: '';
    flex: 1;
    height: 1px;
    background: var(--border-color);
}

.auth-demo-btn {
    width: 100%;
    padding: 13px;
    background: transparent;
    border: 2px solid var(--border-color);
    border-radius: 12px;
    font-size: 1rem;
    font-weight: 600;
    font-family: inherit;
    color: var(--text-dark);
    cursor: pointer;
    transition: all 0.25s;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
}

.auth-demo-btn:hover {
    border-color: var(--magic-purple);
    background: rgba(172, 109, 222, 0.08);
    transform: translateY(-1px);
}

.auth-demo-note {
    font-size: 0.85rem;
    color: var(--text-placeholder);
    font-weight: 400;
}

.auth-demo-info {
    text-align: center;
    font-size: 0.82rem;
    color: var(--text-placeholder);
    margin-top: 10px;
    line-height: 1.5;
}

/* ============================================================
   DEMO BANNER (shown in header when in demo mode)
   ============================================================ */
.demo-banner {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
    flex-wrap: wrap;
    background: linear-gradient(135deg, rgba(255, 217, 61, 0.15), rgba(255, 217, 61, 0.05));
    border: 1px solid rgba(255, 217, 61, 0.3);
    border-radius: 10px;
    padding: 8px 16px;
    margin-top: 12px;
    font-size: 0.9rem;
    color: var(--imagination-yellow);
    font-weight: 600;
}

.demo-banner.hidden { display: none; }

.demo-banner-msg {
    font-weight: 400;
    color: var(--text-light);
}

.demo-upgrade-link {
    background: none;
    border: none;
    color: var(--storybook-blue);
    font-family: inherit;
    font-size: inherit;
    font-weight: 600;
    cursor: pointer;
    text-decoration: underline;
    padding: 0;
}

.demo-upgrade-link:hover {
    color: var(--magic-purple);
}

/* ============================================================
   USER BADGE (shown in header when logged in)
   ============================================================ */
.user-badge {
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: center;
    width: 100%; /* Take full width of header */
    max-width: 600px; /* Optional max width */
    margin: 14px auto 0; /* Center it if there's max width */
    padding: 8px 16px;
    background: rgba(45, 47, 85, 0.6);
    border: 1px solid var(--border-color);
    border-radius: 50px;
    animation: fadeIn 0.4s ease-out;
}

.user-badge.hidden { display: none; }

/* Group Name and Tier together for center layout */
.user-badge-info, #userBadgeInfo {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
}

.user-badge-name {
    font-size: 0.95rem;
    font-weight: 700;
    color: var(--text-dark);
}

.user-badge-tier {
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--magic-purple);
    background: rgba(172, 109, 222, 0.12);
    padding: 3px 10px;
    border-radius: 20px;
    border: 1px solid rgba(172, 109, 222, 0.25);
    line-height: 1; /* Tighter line height to look good above/below name */
}

.user-badge-stories {
    justify-self: start;
    padding: 6px 14px;
    background: rgba(93, 156, 236, 0.12);
    border: 1px solid rgba(93, 156, 236, 0.25);
    border-radius: 20px;
    color: var(--storybook-blue);
    font-size: 0.85rem;
    font-weight: 600;
    font-family: inherit;
    cursor: pointer;
    transition: all 0.2s;
}

.user-badge-stories:hover {
    background: rgba(93, 156, 236, 0.22);
    transform: translateY(-1px);
}

.user-badge-logout {
    justify-self: end;
    padding: 6px 14px;
    background: transparent;
    border: 1px solid var(--border-color);
    border-radius: 20px;
    color: var(--text-placeholder);
    font-size: 0.82rem;
    font-weight: 500;
    font-family: inherit;
    cursor: pointer;
    transition: all 0.2s;
}

.user-badge-logout:hover {
    border-color: #ff6b8a;
    color: #ff6b8a;
}

/* ============================================================
   MODALS (Paywall + Library Full)
   ============================================================ */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(10, 11, 30, 0.85);
    backdrop-filter: blur(6px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 900;
    padding: 24px;
    animation: fadeIn 0.3s ease-out;
}

.modal-overlay.hidden { display: none; }

.modal-card {
    background: var(--soft-chalk);
    border: 1px solid var(--border-color);
    border-radius: 24px;
    padding: 40px 32px 32px;
    max-width: 420px;
    width: 100%;
    text-align: center;
    position: relative;
    box-shadow: 0 20px 60px rgba(0,0,0,0.5);
    animation: slideUp 0.4s cubic-bezier(0.22, 1, 0.36, 1);
}

.modal-card h2 {
    color: var(--storybook-blue);
    font-size: 1.6rem;
    margin: 12px 0 10px;
}

.paywall-icon {
    font-size: 3.5rem;
    margin-bottom: 4px;
    animation: bounce 0.6s ease-out;
}

.paywall-text {
    color: var(--text-light);
    font-size: 1rem;
    line-height: 1.6;
    margin-bottom: 24px;
}

.paywall-buttons {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.paywall-login-link {
    background: none;
    border: none;
    color: var(--storybook-blue);
    font-family: inherit;
    font-size: 0.95rem;
    font-weight: 500;
    cursor: pointer;
    text-decoration: underline;
    padding: 4px;
}

.paywall-login-link:hover {
    color: var(--magic-purple);
}

.modal-close-btn {
    position: absolute;
    top: 14px;
    right: 14px;
    background: rgba(255,255,255,0.06);
    border: 1px solid var(--border-color);
    border-radius: 50%;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1rem;
    color: var(--text-placeholder);
    cursor: pointer;
    font-family: inherit;
    transition: all 0.2s;
}

.modal-close-btn:hover {
    color: var(--text-dark);
    border-color: var(--storybook-blue);
}

/* Upgrade tiers list inside library-full modal */
.upgrade-tiers {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-bottom: 20px;
    text-align: left;
}

.upgrade-tier-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 14px;
    background: rgba(93, 156, 236, 0.06);
    border: 1px solid rgba(93, 156, 236, 0.15);
    border-radius: 10px;
    font-size: 0.9rem;
}

.upgrade-tier-name {
    font-weight: 700;
    color: var(--storybook-blue);
}

.upgrade-tier-limit {
    color: var(--text-light);
    font-size: 0.85rem;
}

.upgrade-tier-item.current-tier {
    border-color: var(--friendly-teal);
    background: rgba(72, 199, 116, 0.08);
}

.upgrade-tier-item.current-tier .upgrade-tier-name {
    color: var(--friendly-teal);
}

.upgrade-cta-btn {
    width: 100%;
    padding: 14px;
    background: linear-gradient(135deg, var(--magic-purple), #8B3DC8);
    color: #fff;
    border: none;
    border-radius: 12px;
    font-size: 1.05rem;
    font-weight: 700;
    font-family: inherit;
    cursor: pointer;
    box-shadow: 0 4px 16px rgba(172, 109, 222, 0.4);
    transition: all 0.25s;
}

.upgrade-cta-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 24px rgba(172, 109, 222, 0.5);
}

/* ============================================================
   STORY LIBRARY PANEL (slide-in from right)
   ============================================================ */
.library-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(10, 11, 30, 0.7);
    backdrop-filter: blur(4px);
    z-index: 800;
    animation: fadeIn 0.3s;
}

.library-backdrop.hidden { display: none; }

.library-panel {
    position: fixed;
    top: 0;
    left: 0; /* Slide in from left */
    width: min(420px, 100vw);
    height: 100dvh;
    background: var(--soft-chalk);
    border-right: 1px solid var(--border-color); /* Border on right now */
    z-index: 810;
    box-shadow: 8px 0 40px rgba(0,0,0,0.4); /* Shadow on right */
    display: flex;
    flex-direction: column;
    animation: slideInLeft 0.35s cubic-bezier(0.22, 1, 0.36, 1);
    overflow: hidden;
}

.library-panel.hidden { display: none; }

@keyframes slideInLeft {
    from { transform: translateX(-100%); }
    to   { transform: translateX(0); }
}

.library-panel-inner {
    display: flex;
    flex-direction: column;
    height: 100%;
    overflow: hidden;
}

.library-panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 24px 24px 16px;
    border-bottom: 1px solid var(--border-color);
    flex-shrink: 0;
}

.library-panel-header h2 {
    font-size: 1.4rem;
    color: var(--storybook-blue);
    text-align: left;
    margin: 0;
}

.library-close-btn {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    border: 1px solid var(--border-color);
    background: transparent;
    color: var(--text-placeholder);
    font-size: 1.1rem;
    cursor: pointer;
    font-family: inherit;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    flex-shrink: 0;
}

.library-close-btn:hover {
    color: var(--text-dark);
    border-color: var(--storybook-blue);
}

.library-tier-badge {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--magic-purple);
    padding: 8px 24px;
    border-bottom: 1px solid var(--border-color);
    flex-shrink: 0;
}

.library-stories-list {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.library-empty {
    text-align: center;
    color: var(--text-placeholder);
    padding: 40px 20px;
    font-size: 1rem;
    line-height: 1.7;
}

.library-empty-icon {
    font-size: 3rem;
    display: block;
    margin-bottom: 12px;
}

/* Story Card in Library */
.story-card {
    background: var(--input-bg);
    border: 1px solid var(--border-color);
    border-radius: 14px;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    transition: border-color 0.2s;
}

.story-card:hover {
    border-color: var(--storybook-blue);
}

.story-card-title {
    font-size: 1rem;
    font-weight: 700;
    color: var(--text-dark);
    line-height: 1.3;
}

.story-card-meta {
    display: flex;
    gap: 10px;
    align-items: center;
    font-size: 0.82rem;
    color: var(--text-placeholder);
    flex-wrap: wrap;
}

.story-card-part {
    background: rgba(93, 156, 236, 0.1);
    color: var(--storybook-blue);
    padding: 2px 8px;
    border-radius: 6px;
    font-weight: 600;
}

.story-card-actions {
    display: flex;
    gap: 8px;
    margin-top: 4px;
}

.story-card-resume-btn {
    flex: 1;
    padding: 9px 12px;
    background: linear-gradient(135deg, var(--friendly-teal), #219653);
    color: #fff;
    border: none;
    border-radius: 9px;
    font-size: 0.9rem;
    font-weight: 600;
    font-family: inherit;
    cursor: pointer;
    transition: all 0.2s;
}

.story-card-resume-btn:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(72, 199, 116, 0.35);
}

.story-card-delete-btn {
    padding: 9px 13px;
    background: transparent;
    border: 1px solid var(--border-color);
    border-radius: 9px;
    color: var(--text-placeholder);
    font-size: 0.9rem;
    font-family: inherit;
    cursor: pointer;
    transition: all 0.2s;
}

.story-card-delete-btn:hover {
    background: rgba(255, 107, 138, 0.1);
    border-color: #ff6b8a;
    color: #ff6b8a;
}

.library-new-btn {
    margin: 16px;
    padding: 14px;
    background: linear-gradient(135deg, var(--storybook-blue), #357ABD);
    color: #fff;
    border: none;
    border-radius: 12px;
    font-size: 1rem;
    font-weight: 700;
    font-family: inherit;
    cursor: pointer;
    box-shadow: 0 4px 14px rgba(93, 156, 236, 0.35);
    transition: all 0.25s;
    flex-shrink: 0;
}

.library-new-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 20px rgba(93, 156, 236, 0.45);
}