/* CSS Variables */
:root {
    --primary-color: #6366f1;
    --primary-hover: #4f46e5;
    --secondary-color: #64748b;
    --secondary-hover: #475569;
    --danger-color: #ef4444;
    --danger-hover: #dc2626;
    --success-color: #22c55e;
    --background: #0f172a;
    --surface: #1e293b;
    --surface-hover: #334155;
    --border-color: #334155;
    --text-primary: #f8fafc;
    --text-secondary: #94a3b8;
    --text-muted: #64748b;
    --node-story: #6366f1;
    --node-choice: #f59e0b;
    --node-ending: #22c55e;
    --shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.3), 0 2px 4px -2px rgba(0, 0, 0, 0.3);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.4), 0 4px 6px -4px rgba(0, 0, 0, 0.4);
    --radius: 8px;
    --radius-lg: 12px;
    --transition: all 0.2s ease;
}

/* Reset & Base */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html, body {
    height: 100%;
    overflow: hidden;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
    background: var(--background);
    color: var(--text-primary);
    line-height: 1.6;
}

/* App Container */
.app-container {
    display: flex;
    flex-direction: column;
    height: 100vh;
    overflow: hidden;
}

/* Header */
.header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 20px;
    background: var(--surface);
    border-bottom: 1px solid var(--border-color);
    flex-shrink: 0;
    gap: 16px;
}

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

.logo {
    width: 36px;
    height: 36px;
    object-fit: contain;
}

.header h1 {
    font-size: 1.25rem;
    font-weight: 600;
    white-space: nowrap;
}

.header-right {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    justify-content: flex-end;
}

/* Buttons */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 16px;
    border: none;
    border-radius: var(--radius);
    font-size: 0.875rem;
    font-weight: 500;
    cursor: pointer;
    transition: var(--transition);
    white-space: nowrap;
}

.btn svg {
    flex-shrink: 0;
}

.btn-primary {
    background: var(--primary-color);
    color: white;
}

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

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

.btn-secondary:hover {
    background: var(--secondary-hover);
}

.btn-danger {
    background: var(--danger-color);
    color: white;
}

.btn-danger:hover {
    background: var(--danger-hover);
}

.btn-sm {
    padding: 6px 12px;
    font-size: 0.8125rem;
}

/* Main Content */
.main-content {
    display: flex;
    flex: 1;
    overflow: hidden;
    position: relative;
}

/* Sidebar */
.sidebar {
    width: 260px;
    background: var(--surface);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    flex-shrink: 0;
}

.sidebar-section {
    padding: 16px;
    border-bottom: 1px solid var(--border-color);
}

.sidebar-section h3 {
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-secondary);
    margin-bottom: 12px;
}

.tool-buttons {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.tool-btn {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    background: transparent;
    border: none;
    border-radius: var(--radius);
    color: var(--text-secondary);
    font-size: 0.875rem;
    cursor: pointer;
    transition: var(--transition);
    text-align: left;
}

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

.tool-btn.active {
    background: var(--primary-color);
    color: white;
}

.tool-btn svg {
    flex-shrink: 0;
}

/* Node Types */
.node-types {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.node-type-btn {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 12px;
    background: transparent;
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    color: var(--text-secondary);
    font-size: 0.875rem;
    cursor: pointer;
    transition: var(--transition);
}

.node-type-btn:hover {
    border-color: var(--text-muted);
}

.node-type-btn.active {
    border-color: var(--primary-color);
    background: rgba(99, 102, 241, 0.1);
    color: var(--text-primary);
}

.node-type-icon {
    width: 12px;
    height: 12px;
    border-radius: 3px;
}

.node-type-icon.story {
    background: var(--node-story);
}

.node-type-icon.choice {
    background: var(--node-choice);
}

.node-type-icon.ending {
    background: var(--node-ending);
}

/* Node List */
.node-list-section {
    flex: 1;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

.node-list {
    flex: 1;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.node-list-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 12px;
    background: var(--surface-hover);
    border-radius: var(--radius);
    cursor: pointer;
    transition: var(--transition);
}

.node-list-item:hover {
    background: var(--border-color);
}

.node-list-item.selected {
    background: var(--primary-color);
}

.node-list-item .node-indicator {
    width: 8px;
    height: 8px;
    border-radius: 2px;
    flex-shrink: 0;
}

.node-list-item .node-name {
    flex: 1;
    font-size: 0.875rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.node-list-item .start-badge {
    font-size: 0.625rem;
    padding: 2px 6px;
    background: var(--success-color);
    border-radius: 4px;
    text-transform: uppercase;
    font-weight: 600;
}

.empty-message {
    color: var(--text-muted);
    font-size: 0.875rem;
    text-align: center;
    padding: 20px;
}

/* Canvas Container */
.canvas-container {
    flex: 1;
    position: relative;
    overflow: hidden;
    background: var(--background);
    background-image:
        radial-gradient(circle at 1px 1px, var(--border-color) 1px, transparent 0);
    background-size: 40px 40px;
}

.canvas-controls {
    position: absolute;
    top: 16px;
    right: 16px;
    display: flex;
    flex-direction: column;
    gap: 4px;
    z-index: 10;
}

.canvas-ctrl-btn {
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--surface);
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    color: var(--text-secondary);
    cursor: pointer;
    transition: var(--transition);
}

.canvas-ctrl-btn:hover {
    background: var(--surface-hover);
    color: var(--text-primary);
}

.canvas {
    width: 100%;
    height: 100%;
    position: relative;
    cursor: crosshair;
    overflow: hidden;
}

.connections-svg {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 1;
}

.nodes-container {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 2;
}

/* Story Nodes */
.story-node {
    position: absolute;
    width: 180px;
    min-height: 80px;
    background: var(--surface);
    border: 2px solid var(--border-color);
    border-radius: var(--radius-lg);
    padding: 12px;
    cursor: move;
    transition: box-shadow 0.2s ease, border-color 0.2s ease;
    user-select: none;
}

.story-node:hover {
    box-shadow: var(--shadow-lg);
}

.story-node.selected {
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.3);
}

.story-node.start-node {
    border-color: var(--success-color);
}

.story-node.start-node::before {
    content: 'START';
    position: absolute;
    top: -10px;
    left: 50%;
    transform: translateX(-50%);
    background: var(--success-color);
    color: white;
    font-size: 0.625rem;
    font-weight: 600;
    padding: 2px 8px;
    border-radius: 4px;
}

.story-node.node-type-story {
    border-left: 4px solid var(--node-story);
}

.story-node.node-type-choice {
    border-left: 4px solid var(--node-choice);
}

.story-node.node-type-ending {
    border-left: 4px solid var(--node-ending);
}

.node-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 8px;
    margin-bottom: 8px;
}

.node-title {
    font-size: 0.875rem;
    font-weight: 600;
    color: var(--text-primary);
    word-break: break-word;
}

.node-preview {
    font-size: 0.75rem;
    color: var(--text-muted);
    overflow: hidden;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
}

.node-image-preview {
    width: 100%;
    height: 60px;
    object-fit: cover;
    border-radius: 4px;
    margin-bottom: 8px;
}

.connection-point {
    position: absolute;
    width: 14px;
    height: 14px;
    background: var(--surface);
    border: 2px solid var(--primary-color);
    border-radius: 50%;
    cursor: pointer;
    z-index: 10;
}

.connection-point.input {
    top: 50%;
    left: -7px;
    transform: translateY(-50%);
}

.connection-point.output {
    top: 50%;
    right: -7px;
    transform: translateY(-50%);
}

.connection-point:hover {
    background: var(--primary-color);
}

/* Connections */
.connection-line {
    stroke: var(--primary-color);
    stroke-width: 2;
    fill: none;
}

.connection-line.temp {
    stroke-dasharray: 5, 5;
    opacity: 0.7;
}

.connection-line:hover {
    stroke-width: 4;
    cursor: pointer;
}

.connection-arrow {
    fill: var(--primary-color);
}

/* Editor Panel */
.editor-panel {
    width: 320px;
    background: var(--surface);
    border-left: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    transform: translateX(100%);
    transition: transform 0.3s ease;
    flex-shrink: 0;
    position: absolute;
    right: 0;
    top: 0;
    bottom: 0;
    z-index: 20;
}

.editor-panel.open {
    transform: translateX(0);
}

.editor-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px;
    border-bottom: 1px solid var(--border-color);
}

.editor-header h3 {
    font-size: 1rem;
    font-weight: 600;
}

.close-btn {
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    border: none;
    border-radius: var(--radius);
    color: var(--text-secondary);
    cursor: pointer;
    transition: var(--transition);
}

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

.editor-content {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
}

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

.form-group label {
    display: block;
    font-size: 0.8125rem;
    font-weight: 500;
    color: var(--text-secondary);
    margin-bottom: 6px;
}

.form-group input[type="text"],
.form-group textarea,
.form-group select {
    width: 100%;
    padding: 10px 12px;
    background: var(--background);
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    color: var(--text-primary);
    font-size: 0.875rem;
    transition: var(--transition);
}

.form-group input[type="text"]:focus,
.form-group textarea:focus,
.form-group select:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.2);
}

.form-group textarea {
    resize: vertical;
    min-height: 100px;
}

.checkbox-group label {
    display: flex;
    align-items: center;
    gap: 8px;
    cursor: pointer;
}

.checkbox-group input[type="checkbox"] {
    width: 18px;
    height: 18px;
    accent-color: var(--primary-color);
}

.image-preview {
    margin-top: 8px;
    border-radius: var(--radius);
    overflow: hidden;
}

.image-preview img {
    width: 100%;
    max-height: 150px;
    object-fit: cover;
}

/* Choices List */
.choices-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-bottom: 8px;
}

.choice-item {
    display: flex;
    gap: 8px;
    align-items: center;
}

.choice-item input {
    flex: 1;
    padding: 8px 10px;
    background: var(--background);
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    color: var(--text-primary);
    font-size: 0.8125rem;
}

.choice-item input:focus {
    outline: none;
    border-color: var(--primary-color);
}

.choice-item .remove-choice-btn {
    width: 28px;
    height: 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    border: none;
    border-radius: var(--radius);
    color: var(--text-muted);
    cursor: pointer;
    transition: var(--transition);
}

.choice-item .remove-choice-btn:hover {
    background: var(--danger-color);
    color: white;
}

.editor-actions {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-top: 24px;
}

/* Footer */
.footer {
    padding: 12px 20px;
    background: var(--surface);
    border-top: 1px solid var(--border-color);
    text-align: center;
    flex-shrink: 0;
}

.footer p {
    font-size: 0.8125rem;
    color: var(--text-muted);
}

.footer a {
    color: var(--primary-color);
    text-decoration: none;
    transition: var(--transition);
}

.footer a:hover {
    color: var(--primary-hover);
    text-decoration: underline;
}

/* Modal */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.7);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    opacity: 0;
    visibility: hidden;
    transition: var(--transition);
    padding: 20px;
}

.modal.open {
    opacity: 1;
    visibility: visible;
}

.modal-content {
    background: var(--surface);
    border-radius: var(--radius-lg);
    max-width: 600px;
    width: 100%;
    max-height: 90vh;
    display: flex;
    flex-direction: column;
    box-shadow: var(--shadow-lg);
}

.preview-modal-content {
    max-width: 700px;
}

.confirm-modal-content {
    max-width: 400px;
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    border-bottom: 1px solid var(--border-color);
}

.modal-header h2 {
    font-size: 1.125rem;
    font-weight: 600;
}

.close-modal-btn {
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    border: none;
    border-radius: var(--radius);
    color: var(--text-secondary);
    cursor: pointer;
    transition: var(--transition);
}

.close-modal-btn:hover {
    background: var(--surface-hover);
    color: var(--text-primary);
}

.modal-body {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
}

.modal-footer {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    padding: 16px 20px;
    border-top: 1px solid var(--border-color);
}

/* Preview Container */
.preview-container {
    text-align: center;
}

.preview-image {
    margin-bottom: 20px;
    border-radius: var(--radius);
    overflow: hidden;
}

.preview-image img {
    max-width: 100%;
    max-height: 300px;
    object-fit: contain;
}

.preview-title {
    font-size: 1.5rem;
    font-weight: 600;
    margin-bottom: 16px;
    color: var(--text-primary);
}

.preview-text {
    font-size: 1rem;
    color: var(--text-secondary);
    line-height: 1.8;
    margin-bottom: 24px;
    white-space: pre-wrap;
}

.preview-choices {
    display: flex;
    flex-direction: column;
    gap: 10px;
    max-width: 400px;
    margin: 0 auto;
}

.preview-choice-btn {
    padding: 14px 20px;
    background: var(--background);
    border: 2px solid var(--border-color);
    border-radius: var(--radius);
    color: var(--text-primary);
    font-size: 0.9375rem;
    cursor: pointer;
    transition: var(--transition);
    text-align: left;
}

.preview-choice-btn:hover {
    border-color: var(--primary-color);
    background: rgba(99, 102, 241, 0.1);
}

.preview-ending {
    padding: 30px;
    text-align: center;
}

.preview-ending h4 {
    font-size: 1.25rem;
    color: var(--success-color);
    margin-bottom: 10px;
}

/* Connection delete button */
.connection-delete {
    position: absolute;
    width: 24px;
    height: 24px;
    background: var(--danger-color);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    z-index: 5;
    opacity: 0;
    transition: opacity 0.2s ease;
}

.connection-delete:hover {
    opacity: 1 !important;
}

.connection-delete svg {
    width: 14px;
    height: 14px;
    stroke: white;
}

/* Scrollbar */
::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

::-webkit-scrollbar-track {
    background: var(--background);
}

::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--text-muted);
}

/* Mobile Responsive */
@media (max-width: 1024px) {
    .sidebar {
        width: 220px;
    }

    .editor-panel {
        width: 300px;
    }
}

@media (max-width: 768px) {
    .header {
        padding: 10px 16px;
        flex-wrap: wrap;
    }

    .header h1 {
        font-size: 1rem;
    }

    .header-right {
        width: 100%;
        justify-content: center;
        margin-top: 8px;
    }

    .btn span {
        display: none;
    }

    .btn {
        padding: 8px;
    }

    .sidebar {
        position: absolute;
        left: 0;
        top: 0;
        bottom: 0;
        z-index: 30;
        transform: translateX(-100%);
        transition: transform 0.3s ease;
    }

    .sidebar.open {
        transform: translateX(0);
    }

    .editor-panel {
        width: 100%;
    }

    .main-content {
        position: relative;
    }

    .canvas-container {
        width: 100%;
    }

    /* Mobile toggle buttons */
    .mobile-toggle {
        display: block;
        position: fixed;
        bottom: 80px;
        width: 50px;
        height: 50px;
        border-radius: 50%;
        background: var(--primary-color);
        color: white;
        border: none;
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        z-index: 25;
        box-shadow: var(--shadow-lg);
    }

    .mobile-toggle.sidebar-toggle {
        left: 16px;
    }

    .mobile-overlay {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.5);
        z-index: 25;
        opacity: 0;
        visibility: hidden;
        transition: var(--transition);
    }

    .mobile-overlay.open {
        opacity: 1;
        visibility: visible;
    }
}

@media (max-width: 480px) {
    .modal-content {
        max-height: 95vh;
    }

    .preview-title {
        font-size: 1.25rem;
    }

    .preview-text {
        font-size: 0.9375rem;
    }

    .story-node {
        width: 150px;
        min-height: 60px;
        padding: 10px;
    }

    .node-title {
        font-size: 0.8125rem;
    }
}

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

.story-node {
    animation: fadeIn 0.2s ease;
}

/* Toast notifications */
.toast-container {
    position: fixed;
    bottom: 80px;
    right: 20px;
    z-index: 1001;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.toast {
    padding: 12px 20px;
    background: var(--surface);
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    color: var(--text-primary);
    font-size: 0.875rem;
    box-shadow: var(--shadow-lg);
    animation: slideIn 0.3s ease;
}

.toast.success {
    border-color: var(--success-color);
}

.toast.error {
    border-color: var(--danger-color);
}

@keyframes slideIn {
    from {
        opacity: 0;
        transform: translateX(100%);
    }
    to {
        opacity: 1;
        transform: translateX(0);
    }
}

/* Connection labels */
.connection-label {
    position: absolute;
    background: var(--surface);
    padding: 2px 8px;
    border-radius: 4px;
    font-size: 0.75rem;
    color: var(--text-secondary);
    pointer-events: none;
    z-index: 3;
    white-space: nowrap;
}

/* Choice Selector */
.choice-selector-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-top: 12px;
}

.choice-selector-btn {
    padding: 12px 16px;
    background: var(--background);
    border: 2px solid var(--border-color);
    border-radius: var(--radius);
    color: var(--text-primary);
    font-size: 0.9375rem;
    text-align: left;
    cursor: pointer;
    transition: var(--transition);
}

.choice-selector-btn:hover:not(:disabled) {
    border-color: var(--primary-color);
    background: rgba(99, 102, 241, 0.1);
}

.choice-selector-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.choice-selector-btn .choice-status {
    font-size: 0.75rem;
    color: var(--text-muted);
    margin-left: 8px;
}
