:root {
    --primary: #6366f1;
    --primary-hover: #4f46e5;
    --bg-dark: #0f172a;
    --card-bg: rgba(30, 41, 59, 0.7);
    --text-main: #f8fafc;
    --text-muted: #94a3b8;
    --border: rgba(255, 255, 255, 0.1);
    --glass: rgba(255, 255, 255, 0.05);
    --success: #22c55e;
    --error: #ef4444;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: 'Inter', system-ui, -apple-system, sans-serif;
}

body {
    background:
        radial-gradient(circle at top left, rgba(99, 102, 241, 0.18), transparent 28%),
        radial-gradient(circle at bottom right, rgba(14, 165, 233, 0.12), transparent 24%),
        var(--bg-dark);
    color: var(--text-main);
    overflow-x: hidden;
    height: 100vh;
    display: flex;
    flex-direction: column;
}

.glass {
    background: var(--glass);
    backdrop-filter: blur(12px);
    border: 1px solid var(--border);
    border-radius: 12px;
}

/* Scrollbar */
::-webkit-scrollbar {
    width: 6px;
}
::-webkit-scrollbar-track {
    background: transparent;
}
::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 10px;
}
::-webkit-scrollbar-thumb:hover {
    background: var(--text-muted);
}

/* Animations */
@keyframes fadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

.animate-fade {
    animation: fadeIn 0.4s ease-out forwards;
}

/* Layout */
.app-container {
    display: flex;
    min-height: 100vh;
    padding: 20px;
    gap: 20px;
}

.page-shell {
    min-height: 100vh;
    padding: 24px;
}

.page-card {
    max-width: 1200px;
    margin: 0 auto;
    padding: 24px;
}

/* Panels */
.panel {
    display: flex;
    flex-direction: column;
    height: 100%;
}

.left-panel {
    flex: 1;
    min-width: 250px;
    max-width: 320px;
}

.middle-panel {
    flex: 3;
    display: flex;
    flex-direction: column;
}

.right-panel {
    flex: 1;
    min-width: 280px;
    max-width: 350px;
}

/* UI Elements */
.header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 15px 20px;
    gap: 12px;
}

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

.page-header {
    padding: 0 0 20px;
    border-bottom: 1px solid var(--border);
    margin-bottom: 24px;
}

.btn {
    padding: 10px 20px;
    border-radius: 8px;
    border: none;
    cursor: pointer;
    font-weight: 500;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    gap: 8px;
    color: white;
    text-decoration: none;
    justify-content: center;
}

.btn-primary {
    background: var(--primary);
}
.btn-primary:hover {
    background: var(--primary-hover);
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(99, 102, 241, 0.3);
}

.btn-ghost {
    background: transparent;
    border: 1px solid var(--border);
}
.btn-ghost:hover {
    background: var(--glass);
}

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

.input-label {
    display: block;
    margin-bottom: 8px;
    font-size: 0.9rem;
    color: var(--text-muted);
}

input, textarea, select {
    width: 100%;
    background: var(--glass);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 12px;
    color: white;
    outline: none;
    transition: border 0.3s;
}

input:focus, textarea:focus {
    border-color: var(--primary);
}

/* Story Tree */
.story-tree {
    overflow-y: auto;
    padding: 10px;
}

.tree-item {
    padding: 8px 12px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 0.95rem;
    margin-bottom: 2px;
    color: var(--text-muted);
    width: 100%;
    border: none;
    background: transparent;
    text-align: left;
}

.tree-item:hover {
    background: var(--glass);
    color: white;
}

.tree-item.active {
    background: var(--glass);
    color: var(--primary);
    border-left: 2px solid var(--primary);
}

.tree-dir {
    font-weight: 600;
    margin-top: 10px;
    color: var(--text-main);
}

.tree-toggle {
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.tree-chevron {
    transition: transform 0.2s ease;
}

.tree-group.collapsed .tree-chevron {
    transform: rotate(0deg);
}

.tree-group:not(.collapsed) .tree-chevron {
    transform: rotate(90deg);
}

.tree-group.collapsed .tree-children {
    display: none;
}

.tree-story {
    padding-left: 28px;
}

.tree-count {
    font-size: 0.75rem;
    color: var(--text-muted);
}

/* Editor */
.editor-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    padding: 20px;
    overflow: hidden;
}

#story-editor {
    flex: 1;
    background: transparent;
    border: none;
    resize: none;
    font-size: 1.1rem;
    line-height: 1.6;
    color: var(--text-main);
    margin-top: 20px;
}

#saved-story-reader {
    flex: 1;
    resize: none;
    background: transparent;
    border: none;
    line-height: 1.7;
    font-size: 1rem;
    color: var(--text-main);
}

/* Overlays / Modals */
.loading-overlay {
    position: absolute;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(15, 23, 42, 0.8);
    display: none;
    justify-content: center;
    align-items: center;
    z-index: 100;
    border-radius: 12px;
}

.loader {
    width: 48px;
    height: 48px;
    border: 5px solid #FFF;
    border-bottom-color: var(--primary);
    border-radius: 50%;
    display: inline-block;
    box-sizing: border-box;
    animation: rotation 1s linear infinite;
}

@keyframes rotation {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

/* Album Section */
.album-list {
    margin-top: 20px;
    overflow-y: auto;
    padding: 0 12px 12px;
}

.album-card {
    padding: 12px;
    background: var(--glass);
    border-radius: 8px;
    margin-bottom: 10px;
    cursor: pointer;
    border: 1px solid transparent;
    width: 100%;
    text-align: left;
    color: var(--text-main);
}

.album-card:hover {
    border-color: var(--primary);
}

.album-card.selected {
    border-color: var(--primary);
    background: rgba(99, 102, 241, 0.1);
}

.saved-story-list,
.reader-container {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
}

.saved-story-item {
    width: 100%;
    border: 1px solid transparent;
    background: var(--glass);
    border-radius: 10px;
    padding: 14px;
    margin-bottom: 12px;
    text-align: left;
    color: var(--text-main);
    cursor: pointer;
}

.saved-story-item:hover,
.saved-story-item.active {
    border-color: var(--primary);
    background: rgba(99, 102, 241, 0.1);
}

.settings-grid {
    display: grid;
    grid-template-columns: minmax(0, 2fr) minmax(280px, 1fr);
    gap: 24px;
}

.prompt-editor {
    min-height: 360px;
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
    line-height: 1.6;
}

.preview-card {
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 20px;
    background: rgba(15, 23, 42, 0.45);
}

.prompt-preview {
    padding: 16px;
    border-radius: 10px;
    background: rgba(15, 23, 42, 0.9);
    border: 1px solid var(--border);
    color: var(--text-main);
    margin-bottom: 16px;
    white-space: pre-wrap;
}

.inline-note,
.status-text {
    color: var(--text-muted);
    font-size: 0.9rem;
}

.settings-actions {
    margin-top: 18px;
    display: flex;
    gap: 12px;
}

.status-text {
    min-height: 24px;
    margin-top: 14px;
}

.status-text.error {
    color: var(--error);
}

@media (max-width: 1080px) {
    .app-container,
    .saved-layout,
    .settings-grid {
        grid-template-columns: 1fr;
        display: grid;
    }

    .left-panel,
    .right-panel {
        max-width: none;
    }

    .app-container {
        height: auto;
    }

    .panel {
        min-height: 240px;
    }
}

@media (max-width: 720px) {
    .app-container,
    .page-shell {
        padding: 12px;
    }

    .header,
    .page-header {
        flex-direction: column;
        align-items: flex-start;
    }

    .header-actions,
    .settings-actions {
        width: 100%;
        flex-wrap: wrap;
    }

    .btn {
        width: 100%;
    }
}
