/* 1. IMPORTS & VARIABLES */
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&family=Merriweather:ital,wght@0,300;0,400;0,700;1,300;1,400&display=swap');

:root {
    /* Color Palette - Professional Slate & Indigo */
    --bg-body: #f8fafc;
    --bg-surface: #ffffff;
    --bg-input: #f1f5f9;
    
    --text-main: #1e293b;
    --text-muted: #64748b;
    --text-inverted: #ffffff;

    --primary: #4f46e5;       /* Indigo 600 */
    --primary-hover: #4338ca; /* Indigo 700 */
    
    --border: #e2e8f0;
    --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
    --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
    --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);

    --radius-sm: 6px;
    --radius-md: 8px;
    --radius-lg: 12px;
}

/* 2. RESET & BASE */
*, *::before, *::after {
    box-sizing: border-box;
}

body {
    margin: 0;
    background-color: var(--bg-body);
    color: var(--text-main);
    font-family: 'Inter', system-ui, -apple-system, sans-serif;
    -webkit-font-smoothing: antialiased;
    line-height: 1.5;
    overflow-x: hidden; /* Prevent body scroll */
}

#app {
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    max-width: 1280px;
    margin: 0 auto;
    padding: 0;
    width: 100%; /* Ensure full width usage */
}

/* 3. HEADER */
header {
    background: var(--bg-surface);
    padding: 16px 24px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 10;
}

.logo {
    font-weight: 700;
    font-size: 1.25rem;
    color: var(--text-main);
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0; /* Prevent logo squishing */
}

.logo .icon {
    font-size: 1.5rem;
}

/* 4. BUTTONS */
button {
    font-family: inherit;
    transition: all 0.2s ease;
}

.btn-primary, .btn-secondary {
    padding: 10px 20px;
    font-size: 0.95rem;
    font-weight: 600;
    border-radius: var(--radius-sm);
    cursor: pointer;
    border: none;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    white-space: nowrap; /* Keep button text on one line */
}

.btn-primary {
    background-color: var(--primary);
    color: var(--text-inverted);
    box-shadow: 0 2px 4px rgba(79, 70, 229, 0.2);
}

.btn-primary:hover {
    background-color: var(--primary-hover);
    transform: translateY(-1px);
    box-shadow: 0 4px 6px rgba(79, 70, 229, 0.3);
}

.btn-secondary {
    background-color: var(--bg-surface);
    color: var(--text-main);
    border: 1px solid var(--border);
}

.btn-secondary:hover {
    background-color: var(--bg-input);
    border-color: #cbd5e1;
}

.btn-primary:disabled, .btn-secondary:disabled {
    opacity: 0.6;
    cursor: not-allowed;
    transform: none;
}

/* 5. HERO / LANDING */
#landing-view {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 40px 20px;
    width: 100%;
}

.hero-container {
    width: 100%;
    display: flex;
    justify-content: center;
}

.hero-content {
    width: min(700px, 100%);
    background: var(--bg-surface);
    border-radius: var(--radius-lg);
    padding: 40px;
    box-shadow: var(--shadow-lg);
    border: 1px solid var(--border);
}

.hero-content h1 {
    font-family: 'Merriweather', serif;
    font-size: 2.5rem;
    margin: 0 0 12px 0;
    color: var(--text-main);
    line-height: 1.2;
}

.subtitle {
    color: var(--text-muted);
    font-size: 1.1rem;
    margin-bottom: 32px;
}

/* Form Elements */
.field-label {
    font-size: 0.75rem;
    font-weight: 700;
    letter-spacing: 0.05em;
    text-transform: uppercase;
    color: var(--text-muted);
    margin-bottom: 8px;
    display: block;
}

.input-group textarea {
    width: 100%;
    min-height: 120px;
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: 16px;
    font-family: inherit;
    font-size: 1rem;
    background: var(--bg-input);
    resize: vertical;
    transition: border-color 0.2s, box-shadow 0.2s;
}

.input-group textarea:focus, 
.chapter-controls input:focus, 
.tone-select:focus {
    outline: none;
    border-color: var(--primary);
    background: var(--bg-surface);
    box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.1);
}

.field-row {
    margin: 24px 0;
    padding: 0;
    border: none;
}

.field-header {
    display: flex;
    justify-content: space-between;
    margin-bottom: 8px;
}

.field-value {
    font-weight: 600;
    color: var(--primary);
}

.chapter-controls {
    display: grid;
    grid-template-columns: 1fr 80px;
    gap: 12px;
    align-items: center;
}

.chapter-controls input[type="number"], 
.tone-select {
    width: 100%;
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: 10px 12px;
    font-size: 0.95rem;
    background: var(--bg-surface);
    color: var(--text-main);
}

input[type="range"] {
    width: 100%;
    height: 6px;
    background: var(--bg-input);
    border-radius: 5px;
    outline: none;
    -webkit-appearance: none;
}
input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: var(--primary);
    cursor: pointer;
    border: 2px solid white;
    box-shadow: 0 2px 4px rgba(0,0,0,0.2);
}

.field-hint {
    font-size: 0.85rem;
    color: var(--text-muted);
    margin-top: 8px;
}

#generate-btn {
    width: 100%;
    padding: 14px;
    font-size: 1.1rem;
    margin-top: 12px;
}

.error {
    background-color: #fef2f2;
    color: #ef4444;
    border: 1px solid #fee2e2;
    border-radius: var(--radius-md);
    padding: 12px;
    margin-top: 16px;
    font-size: 0.9rem;
}

.auth-panel,
.saved-panel {
    margin-top: 24px;
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: 16px;
    background: var(--bg-surface);
}

.auth-panel h3,
.saved-panel h3 {
    margin: 0 0 12px 0;
    font-size: 1rem;
    font-weight: 600;
}

.auth-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 8px;
}

.auth-grid input {
    width: 100%;
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: 10px 12px;
    font-size: 0.95rem;
    background: var(--bg-surface);
    color: var(--text-main);
}

.auth-actions {
    display: flex;
    gap: 10px;
    margin-top: 12px;
    flex-wrap: wrap;
}

.auth-row {
    font-size: 0.95rem;
    color: var(--text-muted);
}

.auth-message {
    margin-top: 12px;
    font-size: 0.9rem;
    color: var(--text-muted);
}

.auth-message.is-error {
    color: #b91c1c;
}

.saved-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
    color: var(--text-muted);
}

.saved-item {
    text-align: left;
    padding: 10px 12px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    background: var(--bg-input);
    color: var(--text-main);
}

.saved-item:hover {
    background: var(--bg-surface);
}

.saved-meta {
    font-size: 0.8rem;
    color: var(--text-muted);
    margin-top: 4px;
}

/* 6. APP VIEW / RE-READER */
#re-reader-view {
    padding: 24px;
    max-width: 1200px;
    margin: 0 auto;
    width: 100%;
}

.status-bar {
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: 16px 20px;
    box-shadow: var(--shadow-sm);
    margin-bottom: 24px;
}

.status-info {
    display: flex;
    align-items: center;
    gap: 16px;
    flex-wrap: wrap;
}

.badge {
    background: #e0e7ff;
    color: var(--primary);
    border: 1px solid #c7d2fe;
    padding: 4px 10px;
    border-radius: 20px;
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
}

.badge.is-paused {
    background: #fff1f2;
    color: #e11d48;
    border-color: #fecdd3;
}

.progress-bar-bg {
    background: var(--bg-input);
    height: 6px;
    border-radius: 3px;
    margin-top: 16px;
    overflow: hidden;
    border: none;
}

.progress-bar-fill {
    height: 100%;
    background: var(--primary);
    transition: width 0.3s ease;
}

/* LAYOUT SPLIT - KEY FIXES FOR SCROLLING
   We use min-width: 0 on children to force Grid to wrap text 
*/
.content-split {
    display: grid;
    grid-template-columns: 280px 1fr;
    gap: 32px;
    align-items: start;
    width: 100%;
}

/* Sidebar */
.chapter-list {
    background: var(--bg-surface);
    border-radius: var(--radius-md);
    border: 1px solid var(--border);
    max-height: calc(100vh - 150px);
    overflow-y: auto;
    padding: 0;
    position: sticky;
    top: 90px;
    min-width: 0; /* Prevents overflow */
}

.sidebar-header {
    padding: 16px;
    border-bottom: 1px solid var(--border);
    background: var(--bg-surface);
    position: sticky;
    top: 0;
}

.sidebar-header h3 {
    margin: 0;
    font-size: 1rem;
    font-weight: 600;
}

#chapters-ul li {
    padding: 10px 16px;
    border-bottom: 1px solid var(--bg-input);
    cursor: pointer;
    font-size: 0.9rem;
    color: var(--text-muted);
    transition: background 0.1s, color 0.1s;
    /* Ensure long chapter titles wrap */
    white-space: normal; 
    word-wrap: break-word;
}

#chapters-ul li:hover {
    background: var(--bg-input);
    color: var(--text-main);
}

#chapters-ul li.active {
    background: #eff6ff;
    color: var(--primary);
    font-weight: 600;
    border-left: 3px solid var(--primary);
    padding-left: 13px;
}

#chapters-ul li.locked {
    color: #94a3b8;
    cursor: not-allowed;
}

#chapters-ul li.locked:hover {
    background: transparent;
    color: #94a3b8;
}

#chapters-ul li.ready {
    color: var(--text-main);
}

/* Reading Pane */
.reader-pane {
    display: flex;
    flex-direction: column;
    gap: 24px;
    min-width: 0; /* CRITICAL FIX: prevents text from blowing out the width */
}

.meta-panel {
    background: var(--bg-input);
    border-radius: var(--radius-md);
    padding: 16px;
    border: 1px solid var(--border);
    /* Ensure content wraps */
    overflow-wrap: break-word; 
    word-wrap: break-word;
    max-width: 100%;
}

.meta-panel summary {
    cursor: pointer;
    font-weight: 600;
    font-size: 0.85rem;
    text-transform: uppercase;
    color: var(--text-muted);
}

.meta-panel[open] summary {
    margin-bottom: 8px;
}

.meta-panel h3 {
    margin: 0 0 8px 0;
    font-size: 0.85rem;
    text-transform: uppercase;
    color: var(--text-muted);
}

/* The Paper Sheet */
.paper-sheet {
    background: #ffffff;
    border: 1px solid var(--border);
    box-shadow: var(--shadow-sm);
    border-radius: 2px;
    padding: 60px;
    min-height: 500px;
    color: #2c2c2c;
    font-family: 'Merriweather', serif;
    font-size: 1.15rem;
    line-height: 1.8;
    
    /* CRITICAL FIXES FOR TEXT WRAPPING */
    overflow-wrap: break-word;
    word-wrap: break-word;
    word-break: break-word;
    max-width: 100%;
}

.paper-sheet img {
    max-width: 100%; /* Prevent generated images from overflowing */
    height: auto;
}

.paper-sheet h1, .paper-sheet h2 {
    font-weight: 700;
    margin-top: 1em;
    line-height: 1.3;
}

.chapter-actions {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    gap: 12px;
    margin-top: 4px;
}

.icon-button {
    width: 40px;
    height: 40px;
    padding: 0;
    border-radius: var(--radius-sm);
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.icon-button svg {
    width: 18px;
    height: 18px;
}

.icon-button[data-state="copied"] {
    border-color: var(--primary);
    color: var(--primary);
}

.placeholder-text {
    color: var(--text-muted);
    font-family: 'Inter', sans-serif;
    font-style: italic;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    gap: 12px;
}

.spinner {
    width: 24px;
    height: 24px;
    border: 3px solid var(--bg-input);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

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

/* FIX for the Story Bible pre-formatted text */
.mono {
    font-family: 'Menlo', 'Monaco', 'Courier New', monospace;
    font-size: 0.85rem;
    color: #475569;
    background: #ffffff;
    padding: 12px;
    border-radius: 4px;
    border: 1px solid var(--border);
    
    /* CRITICAL: Wraps code/pre text so it doesn't scroll horizontally */
    white-space: pre-wrap;       
    word-wrap: break-word;       
    overflow-wrap: break-word;
    max-width: 100%;
}

.hidden {
    display: none !important;
}

/* 7. RESPONSIVE DESIGN */
@media (max-width: 900px) {
    .content-split {
        grid-template-columns: 1fr;
        gap: 24px;
    }

    .chapter-list {
        position: static;
        max-height: 300px;
        width: 100%;
    }
}

@media (max-width: 600px) {
    header {
        flex-direction: column;
        align-items: stretch;
        gap: 16px;
    }
    
    #re-reader-view {
        padding: 16px;
    }

    .hero-content {
        padding: 24px 16px;
    }

    .hero-content h1 {
        font-size: 2rem;
    }

    .paper-sheet {
        padding: 24px 20px; /* Reduced padding for mobile */
        font-size: 1.05rem;
    }
    
    .chapter-controls {
        grid-template-columns: 1fr;
    }
}
