/* 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 {
    /* Refined Color Palette */
    --bg-body: #f8fafc;
    --bg-surface: #ffffff;
    --bg-input: #f1f5f9;
    --bg-accent-light: #e0e7ff;
    
    --text-main: #0f172a;
    --text-muted: #64748b;
    --text-inverted: #ffffff;

    --primary: #4f46e5;       
    --primary-hover: #4338ca; 
    --danger: #ef4444;

    --border: #e2e8f0;
    
    /* Better Shadows */
    --shadow-sm: 0 1px 3px 0 rgb(0 0 0 / 0.1), 0 1px 2px -1px rgb(0 0 0 / 0.1);
    --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 20px 25px -5px rgb(0 0 0 / 0.1), 0 8px 10px -6px rgb(0 0 0 / 0.1);
    --shadow-sticky: 0 -4px 6px -1px rgb(0 0 0 / 0.1);

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

/* 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;
    padding-bottom: 80px; /* Space for sticky player */
}

/* 3. FULL WIDTH HEADER */
.main-header {
    background: var(--bg-surface);
    border-bottom: 1px solid var(--border);
    width: 100%;
    position: sticky;
    top: 0;
    z-index: 50; /* Above everything else */
}

.header-inner {
    max-width: 1280px; /* Constrain content width */
    margin: 0 auto;
    padding: 16px 24px;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.logo {
    font-weight: 800;
    font-size: 1.35rem;
    color: var(--text-main);
    display: flex;
    align-items: center;
    gap: 10px;
    letter-spacing: -0.02em;
}

.logo.is-clickable {
    cursor: pointer;
    user-select: none;
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 12px;
}

#header-user-area {
    display: flex;
    align-items: center;
    gap: 10px;
}

.credits-pill {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 6px 10px;
    border-radius: 999px;
    border: 1px solid var(--border);
    background: var(--bg-input);
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--text-main);
}

.credits-pill span {
    display: inline-flex;
    align-items: center;
}

.credits-pill .credits-label {
    color: var(--text-muted);
    font-size: 0.65rem;
    text-transform: uppercase;
    letter-spacing: 0.08em;
}

.credits-pill .credits-balance {
    font-size: 0.9rem;
}

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

button:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    box-shadow: none;
}

.btn-primary {
    background-color: var(--primary);
    color: var(--text-inverted);
    border: 1px solid transparent;
    padding: 10px 20px;
    border-radius: var(--radius-sm);
    font-weight: 600;
    font-size: 0.95rem;
}

.btn-primary:hover {
    background-color: var(--primary-hover);
    box-shadow: 0 4px 12px rgba(79, 70, 229, 0.25);
}

.btn-primary-outline {
    background: transparent;
    color: var(--primary);
    border: 1px solid var(--primary);
    padding: 8px 18px;
    border-radius: var(--radius-sm);
    font-weight: 600;
    font-size: 0.9rem;
}

.btn-primary-outline:hover {
    background: #eff6ff;
}

.btn-nav {
    padding: 8px 18px;
    font-size: 0.9rem;
}

.btn-secondary {
    background-color: var(--bg-surface);
    color: var(--text-main);
    border: 1px solid var(--border);
    padding: 10px 16px;
    border-radius: var(--radius-sm);
    font-weight: 500;
}

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

.btn-secondary.is-disabled {
    opacity: 0.5;
    cursor: not-allowed;
    box-shadow: none;
}

.btn-secondary.is-disabled:hover {
    border-color: var(--border);
    background: var(--bg-surface);
}

.btn-text {
    background: none;
    border: none;
    color: var(--text-muted);
    font-weight: 500;
    padding: 8px 12px;
}

.btn-text:hover {
    color: var(--primary);
}

.btn-large {
    width: 100%;
    padding: 16px;
    font-size: 1.1rem;
    border-radius: var(--radius-md);
}

.btn-secondary.compact, .btn-primary.compact {
    padding: 6px 12px;
    font-size: 0.85rem;
}

/* 5. APP CONTAINER */
#app {
    max-width: 1280px;
    margin: 0 auto;
    width: 100%;
}

/* 6. LANDING PAGE POLISH */
#landing-view {
    padding: 60px 20px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 40px;
}

.hero-section {
    text-align: center;
    max-width: 600px;
}

.hero-section h1 {
    font-size: 3rem;
    font-weight: 800;
    letter-spacing: -0.03em;
    margin: 0 0 16px 0;
    line-height: 1.1;
    background: -webkit-linear-gradient(45deg, #1e293b, #4f46e5);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

.hero-section .subtitle {
    font-size: 1.25rem;
    color: var(--text-muted);
    line-height: 1.5;
}

.generator-card {
    background: var(--bg-surface);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    border: 1px solid var(--border);
    width: 100%;
    max-width: 800px;
    padding: 32px;
}

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

.main-input textarea {
    width: 100%;
    min-height: 140px;
    border: 2px solid var(--bg-input);
    border-radius: var(--radius-md);
    padding: 16px;
    font-size: 1.1rem;
    background: var(--bg-input);
    font-family: inherit;
    resize: vertical;
    transition: all 0.2s;
}

.main-input textarea:focus {
    background: var(--bg-surface);
    border-color: var(--primary);
    outline: none;
    box-shadow: 0 0 0 4px rgba(79, 70, 229, 0.1);
}

.settings-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 24px;
    margin: 32px 0;
    padding-top: 24px;
    border-top: 1px solid var(--border);
}

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

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

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

input[type="range"] {
    width: 100%;
    accent-color: var(--primary);
    cursor: pointer;
}

/* 7. AUTH VIEW */
#auth-view {
    padding: 60px 20px;
    display: flex;
    justify-content: center;
}

#library-view {
    padding: 60px 20px;
    display: flex;
    justify-content: center;
}

.auth-container {
    width: 100%;
    max-width: 440px;
}

.auth-card {
    background: var(--bg-surface);
    padding: 40px;
    border-radius: var(--radius-lg);
    border: 1px solid var(--border);
    box-shadow: var(--shadow-md);
}

.auth-card h2 {
    margin: 0 0 8px 0;
    font-size: 1.75rem;
    text-align: center;
}

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

.form-group {
    margin-bottom: 20px;
}

.form-group input {
    width: 100%;
    padding: 12px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    font-size: 1rem;
}

.auth-buttons {
    display: flex;
    flex-direction: column;
    gap: 12px;
    margin-top: 24px;
}

.full-width {
    width: 100%;
}

.saved-panel-wrapper {
    margin-top: 32px;
    padding-top: 24px;
    border-top: 1px solid var(--border);
}

.storage-indicator {
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 12px 14px;
    display: grid;
    gap: 8px;
}

.storage-label {
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--text-muted);
    font-weight: 600;
}

.storage-bar {
    height: 6px;
    background: var(--bg-surface);
    border-radius: 999px;
    overflow: hidden;
}

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

.storage-text {
    font-size: 0.8rem;
    color: var(--text-muted);
}

.saved-panel-wrapper h3 {
    font-size: 1rem;
    margin-bottom: 12px;
}

.saved-list {
    display: grid;
    gap: 12px;
}

.saved-item {
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 12px 14px;
    display: flex;
    align-items: center;
    gap: 12px;
}

.saved-item:hover {
    background: var(--bg-surface);
    border-color: #cbd5e1;
}

.saved-open {
    background: none;
    border: none;
    padding: 0;
    text-align: left;
    flex: 1;
    color: inherit;
    font: inherit;
}

.saved-open:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: 2px;
    border-radius: 6px;
}

.saved-title {
    font-weight: 600;
}

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

.saved-delete {
    background: var(--bg-surface);
    border: 1px solid var(--border);
    color: var(--danger);
    border-radius: var(--radius-sm);
    padding: 6px 10px;
    font-size: 0.8rem;
    font-weight: 600;
    white-space: nowrap;
}

.saved-delete:hover {
    background: #fee2e2;
    border-color: #fca5a5;
}

.empty-state {
    text-align: center;
    padding: 20px;
    background: var(--bg-input);
    border-radius: var(--radius-sm);
    color: var(--text-muted);
    font-size: 0.9rem;
}

/* CREDITS VIEW */
.credits-page {
    max-width: 980px;
    margin: 0 auto;
    padding: 60px 20px;
}

.credits-hero {
    text-align: center;
    margin-bottom: 32px;
}

.credits-hero h2 {
    margin: 0 0 12px 0;
    font-size: 2rem;
}

.credits-subtitle {
    margin: 0;
    color: var(--text-muted);
}

.credits-notes {
    list-style: none;
    margin: 24px auto 24px;
    padding: 16px 20px;
    max-width: 680px;
    border-radius: var(--radius-md);
    border: 1px solid var(--border);
    background: var(--bg-input);
    color: var(--text-muted);
    display: grid;
    gap: 8px;
}

.credits-notes li {
    margin: 0;
    font-size: 0.95rem;
}

.credits-notes strong {
    color: var(--text-main);
}

.credits-grid {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 20px;
}

.credits-option {
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: 20px;
    box-shadow: var(--shadow-sm);
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.credits-option-info {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.credits-amount {
    font-size: 1.05rem;
    font-weight: 700;
}

.credits-price {
    font-size: 1.4rem;
    font-weight: 700;
    color: var(--primary);
}

.credits-footnote {
    text-align: center;
    margin-top: 20px;
    color: var(--text-muted);
    font-size: 0.85rem;
}

/* 8. READER VIEW & STATUS BAR */
#re-reader-view {
    padding: 24px 20px;
}

.status-bar {
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: 12px 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 32px;
    box-shadow: var(--shadow-sm);
}

.status-left {
    display: flex;
    flex-direction: column;
    gap: 6px;
    min-width: 300px;
}

.status-text-group {
    display: flex;
    align-items: center;
    gap: 12px;
}

.status-right {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
}

.progress-bar-bg {
    width: 100%;
    height: 4px;
    background: var(--bg-input);
    border-radius: 2px;
    overflow: hidden;
}

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

.badge {
    background: var(--bg-accent-light);
    color: var(--primary);
    font-size: 0.7rem;
    padding: 2px 8px;
    border-radius: 12px;
    font-weight: 700;
    text-transform: uppercase;
}

/* 9. LAYOUT SPLIT */
.content-split {
    display: grid;
    grid-template-columns: 260px 1fr;
    gap: 40px;
    align-items: start;
}

.chapter-list {
    position: sticky;
    top: 100px; /* Below header */
    max-height: calc(100vh - 120px);
    overflow-y: auto;
    border-right: 1px solid var(--border);
    padding-right: 12px;
}

.sidebar-header {
    margin-bottom: 16px;
    padding-bottom: 8px;
    border-bottom: 2px solid var(--bg-input);
}

#chapters-ul {
    list-style: none;
    padding: 0;
    margin: 0;
}

#chapters-ul li {
    padding: 10px 12px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: 0.9rem;
    color: var(--text-muted);
    margin-bottom: 4px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
}

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

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

#chapters-ul .chapter-title {
    flex: 1;
    min-width: 0;
}

.chapter-play {
    width: 32px;
    height: 32px;
    border-radius: 999px;
    border: 1px solid var(--border);
    background: var(--bg-surface);
    color: var(--text-muted);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.chapter-play:hover {
    border-color: #cbd5e1;
    color: var(--text-main);
}

.chapter-play:disabled,
.chapter-play.is-disabled,
.chapter-play.is-credit-locked {
    opacity: 0.4;
    cursor: not-allowed;
}

.chapter-play .icon {
    display: none;
    align-items: center;
    justify-content: center;
}

.chapter-play svg {
    width: 14px;
    height: 14px;
}

.chapter-play[data-state="idle"] .icon-play,
.chapter-play[data-state="paused"] .icon-play {
    display: inline-flex;
}

.chapter-play[data-state="playing"] .icon-pause {
    display: inline-flex;
}

.chapter-play[data-state="loading"] .icon-loading {
    display: inline-flex;
}

.chapter-play[data-state="playing"] {
    border-color: #c7d2fe;
    background: #eef2ff;
    color: var(--primary);
}

.chapter-play[data-state="loading"] {
    color: var(--primary);
}

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

#chapters-ul li.active {
    background: #eef2ff;
    color: var(--primary);
    font-weight: 600;
}

.icon-button.is-credit-locked,
.btn-secondary.is-credit-locked {
    opacity: 0.55;
    cursor: not-allowed;
}

.icon-button.is-credit-locked:hover,
.btn-secondary.is-credit-locked:hover {
    color: var(--text-muted);
    border-color: var(--border);
}

/* 10. PAPER & READING */
.reader-pane {
    min-width: 0; /* Prevent grid blowout */
}

.story-meta-group {
    margin-bottom: 24px;
    display: flex;
    gap: 12px;
    flex-wrap: wrap;
}

.meta-panel {
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--bg-surface);
    padding: 12px;
    flex: 1;
    min-width: 250px;
}

.meta-panel summary {
    font-size: 0.8rem;
    font-weight: 700;
    color: var(--text-muted);
    cursor: pointer;
    outline: none;
}

.mono {
    font-family: 'Menlo', monospace;
    font-size: 0.8rem;
    white-space: pre-wrap;
    background: var(--bg-input);
    padding: 10px;
    border-radius: 4px;
    margin-top: 10px;
}

.chapter-shell {
    position: relative;
    box-shadow: var(--shadow-md);
}

.paper-sheet {
    background: #ffffff;
    padding: 60px 80px;
    min-height: 600px;
    color: #333;
    font-family: 'Merriweather', serif;
    font-size: 1.2rem;
    line-height: 1.9;
}

.chapter-actions-footer {
    margin-top: 20px;
    display: flex;
    justify-content: flex-end;
    gap: 12px;
}

.audio-loading {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 16px 0;
    color: var(--text-muted);
}

.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.6);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 120;
}

.modal-card {
    position: relative;
    background: var(--bg-surface);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-lg);
    padding: 24px 26px;
    width: min(420px, 90vw);
    text-align: left;
}

.modal-title {
    margin: 0 0 10px 0;
    font-size: 1.1rem;
}

.modal-text {
    margin: 0;
    color: var(--text-muted);
    line-height: 1.5;
}

.modal-actions {
    margin-top: 16px;
}

.modal-close {
    position: absolute;
    top: 10px;
    right: 10px;
    width: 32px;
    height: 32px;
    border-radius: 999px;
    border: 1px solid var(--border);
    background: var(--bg-input);
    color: var(--text-muted);
    font-size: 1.1rem;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.modal-close:hover {
    color: var(--text-main);
    border-color: #cbd5e1;
}

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

.chapter-play .spinner {
    width: 14px;
    height: 14px;
    border-width: 2px;
}

/* 11. STICKY MEDIA PLAYER */
.sticky-audio-bar {
    position: fixed;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    width: 90%;
    max-width: 600px;
    background: var(--bg-surface);
    color: var(--text-main);
    padding: 12px 20px;
    border-radius: 50px;
    border: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    box-shadow: var(--shadow-lg);
    z-index: 100;
    animation: slideUp 0.4s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes slideUp {
    from { transform: translate(-50%, 100%); opacity: 0; }
    to { transform: translate(-50%, 0); opacity: 1; }
}

.audio-content {
    display: flex;
    align-items: center;
    gap: 16px;
    flex: 1;
}

.audio-track-info {
    font-size: 0.85rem;
    font-weight: 600;
    white-space: nowrap;
}

.sticky-audio-bar audio {
    height: 30px;
    filter: none;
    flex: 1;
    max-width: 300px;
}

.close-audio {
    background: none;
    border: none;
    color: var(--text-muted);
    font-size: 1.5rem;
    margin-left: 12px;
    padding: 0 8px;
    cursor: pointer;
}

.close-audio:hover {
    color: var(--text-main);
}

/* Utilities */
.hidden { display: none !important; }
.icon-button { padding: 8px; display: inline-flex; }

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

/* 12. RESPONSIVE */
@media (max-width: 900px) {
    .content-split {
        grid-template-columns: 1fr;
    }
    .chapter-list {
        position: static;
        max-height: 300px;
        border-right: none;
        border-bottom: 1px solid var(--border);
        margin-bottom: 24px;
    }
    .settings-grid {
        grid-template-columns: 1fr;
        gap: 16px;
    }
    .credits-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
}

@media (max-width: 600px) {
    .hero-section h1 { font-size: 2.2rem; }
    .paper-sheet { padding: 30px 24px; font-size: 1.1rem; }
    .status-bar { flex-direction: column; align-items: stretch; gap: 16px; }
    .status-left { min-width: auto; }
    .credits-grid {
        grid-template-columns: 1fr;
    }
}
