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

:root {
    --primary-color: #6366f1;
    --primary-hover: #4f46e5;
    --secondary-color: #64748b;
    --secondary-hover: #475569;
    --accent-color: #10b981;
    --accent-hover: #059669;
    --danger-color: #ef4444;
    --danger-hover: #dc2626;
    --bg-color: #0f172a;
    --bg-secondary: #1e293b;
    --bg-tertiary: #334155;
    --text-primary: #f1f5f9;
    --text-secondary: #94a3b8;
    --text-muted: #64748b;
    --border-color: #475569;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 4px 6px rgba(0, 0, 0, 0.4);
    --shadow-lg: 0 10px 15px rgba(0, 0, 0, 0.5);
    --radius-sm: 4px;
    --radius-md: 8px;
    --radius-lg: 12px;
    --transition: 0.2s ease;
}

html, body {
    height: 100%;
}

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

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

/* Header */
.app-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem 2rem;
    background-color: var(--bg-secondary);
    border-bottom: 1px solid var(--border-color);
    flex-wrap: wrap;
    gap: 1rem;
}

.app-header h1 {
    font-size: 1.5rem;
    font-weight: 600;
    color: var(--text-primary);
}

.main-nav {
    display: flex;
    gap: 0.5rem;
}

.nav-btn {
    padding: 0.5rem 1.5rem;
    border: none;
    border-radius: var(--radius-md);
    background-color: transparent;
    color: var(--text-secondary);
    font-size: 1rem;
    font-weight: 500;
    cursor: pointer;
    transition: all var(--transition);
}

.nav-btn:hover {
    background-color: var(--bg-tertiary);
    color: var(--text-primary);
}

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

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

.mode-panel {
    display: none;
    width: 100%;
    height: 100%;
}

.mode-panel.active {
    display: flex;
}

/* Editor Layout */
.editor-layout {
    display: flex;
    width: 100%;
    height: 100%;
}

/* Sidebar */
.sidebar {
    width: 280px;
    background-color: var(--bg-secondary);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
}

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

.sidebar-header h2 {
    font-size: 1rem;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

/* Story List */
.story-list {
    flex: 1;
    overflow-y: auto;
    padding: 0.5rem;
}

.story-item {
    padding: 0.75rem 1rem;
    margin-bottom: 0.25rem;
    border-radius: var(--radius-md);
    cursor: pointer;
    transition: all var(--transition);
    border: 1px solid transparent;
}

.story-item:hover {
    background-color: var(--bg-tertiary);
}

.story-item.active {
    background-color: var(--primary-color);
    border-color: var(--primary-hover);
}

.story-item-title {
    font-weight: 500;
    color: var(--text-primary);
    margin-bottom: 0.25rem;
}

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

/* Editor Main */
.editor-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    position: relative;
}

.editor-toolbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem;
    background-color: var(--bg-secondary);
    border-bottom: 1px solid var(--border-color);
    flex-wrap: wrap;
    gap: 0.5rem;
}

.toolbar-left, .toolbar-right {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    flex-wrap: wrap;
}

.story-title-input {
    font-size: 1.25rem;
    font-weight: 600;
    padding: 0.5rem;
    background-color: transparent;
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    min-width: 200px;
}

.story-title-input:not(:disabled):hover,
.story-title-input:focus {
    border-color: var(--border-color);
    background-color: var(--bg-tertiary);
    outline: none;
}

.story-title-input:disabled {
    cursor: not-allowed;
}

/* Node Map */
.node-map {
    flex: 1;
    overflow: auto;
    padding: 2rem;
    background-color: var(--bg-color);
    background-image:
        radial-gradient(circle, var(--bg-tertiary) 1px, transparent 1px);
    background-size: 20px 20px;
    position: relative;
}

.node-map .empty-state {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    text-align: center;
    color: var(--text-muted);
}

.node-card {
    position: absolute;
    width: 200px;
    padding: 1rem;
    background-color: var(--bg-secondary);
    border: 2px solid var(--border-color);
    border-radius: var(--radius-md);
    cursor: move;
    transition: box-shadow var(--transition), border-color var(--transition);
    user-select: none;
}

.node-card:hover {
    box-shadow: var(--shadow-lg);
    border-color: var(--primary-color);
}

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

.node-card.start-node {
    border-color: var(--accent-color);
}

.node-card.end-node {
    border-color: var(--danger-color);
}

.node-card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 0.5rem;
}

.node-card-title {
    font-weight: 600;
    color: var(--text-primary);
    font-size: 0.9rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    flex: 1;
}

.node-badges {
    display: flex;
    gap: 0.25rem;
}

.node-badge {
    font-size: 0.65rem;
    padding: 0.1rem 0.3rem;
    border-radius: var(--radius-sm);
    font-weight: 600;
    text-transform: uppercase;
}

.node-badge.start {
    background-color: var(--accent-color);
    color: white;
}

.node-badge.end {
    background-color: var(--danger-color);
    color: white;
}

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

.node-card-choices {
    margin-top: 0.5rem;
    font-size: 0.75rem;
    color: var(--text-secondary);
}

/* Node Editor Panel */
.node-editor-panel {
    position: absolute;
    top: 0;
    right: 0;
    width: 400px;
    height: 100%;
    background-color: var(--bg-secondary);
    border-left: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    box-shadow: var(--shadow-lg);
    z-index: 100;
    overflow: hidden;
}

.node-editor-panel.hidden {
    display: none;
}

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

.panel-header h3 {
    font-size: 1.1rem;
    font-weight: 600;
}

.panel-body {
    flex: 1;
    overflow-y: auto;
    padding: 1rem;
}

.panel-actions {
    display: flex;
    gap: 0.5rem;
    margin-top: 1rem;
    padding-top: 1rem;
    border-top: 1px solid var(--border-color);
}

/* Forms */
.form-group {
    margin-bottom: 1rem;
}

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

.form-input, .form-textarea, .form-select {
    width: 100%;
    padding: 0.625rem;
    background-color: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    font-size: 0.9rem;
    transition: border-color var(--transition);
}

.form-input:focus, .form-textarea:focus, .form-select:focus {
    outline: none;
    border-color: var(--primary-color);
}

.form-textarea {
    resize: vertical;
    min-height: 100px;
    font-family: inherit;
}

.checkbox-group {
    display: flex;
    gap: 1.5rem;
}

.checkbox-group label {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    cursor: pointer;
    margin-bottom: 0;
}

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

/* Choices Container */
.choices-container, .actions-container {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    margin-bottom: 0.5rem;
}

.choice-item, .action-item {
    display: flex;
    gap: 0.5rem;
    align-items: flex-start;
    padding: 0.75rem;
    background-color: var(--bg-tertiary);
    border-radius: var(--radius-sm);
}

.choice-item input, .action-item input, .action-item select {
    flex: 1;
    min-width: 0;
}

.choice-condition {
    width: 100%;
    margin-top: 0.5rem;
}

.choice-fields {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    flex: 1;
}

.choice-main-row {
    display: flex;
    gap: 0.5rem;
    align-items: center;
}

.choice-main-row input {
    flex: 1;
}

.choice-main-row select {
    width: 120px;
}

/* Buttons */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0.625rem 1.25rem;
    border: none;
    border-radius: var(--radius-sm);
    font-size: 0.875rem;
    font-weight: 500;
    cursor: pointer;
    transition: all var(--transition);
    white-space: nowrap;
}

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

.btn-sm {
    padding: 0.375rem 0.75rem;
    font-size: 0.8rem;
}

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

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

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

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

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

.btn-accent:hover:not(:disabled) {
    background-color: var(--accent-hover);
}

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

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

.btn-close {
    background: none;
    border: none;
    font-size: 1.5rem;
    color: var(--text-muted);
    cursor: pointer;
    line-height: 1;
    padding: 0.25rem;
}

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

.btn-icon {
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    padding: 0.25rem;
    font-size: 1rem;
}

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

/* Play Mode */
.play-layout {
    display: flex;
    width: 100%;
    height: 100%;
}

.play-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.play-container {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
}

.play-content {
    flex: 1;
    max-width: 800px;
    margin: 0 auto;
    padding: 2rem;
    overflow-y: auto;
}

.play-content.hidden {
    display: none;
}

.play-title {
    font-size: 2rem;
    margin-bottom: 1.5rem;
    color: var(--text-primary);
    text-align: center;
}

.play-text {
    font-size: 1.1rem;
    line-height: 1.8;
    color: var(--text-primary);
    margin-bottom: 2rem;
    white-space: pre-wrap;
}

.play-choices {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    margin-bottom: 2rem;
}

.play-choice-btn {
    padding: 1rem 1.5rem;
    background-color: var(--bg-secondary);
    border: 2px solid var(--border-color);
    border-radius: var(--radius-md);
    color: var(--text-primary);
    font-size: 1rem;
    text-align: left;
    cursor: pointer;
    transition: all var(--transition);
}

.play-choice-btn:hover {
    background-color: var(--bg-tertiary);
    border-color: var(--primary-color);
    transform: translateX(5px);
}

.play-variables {
    padding: 1rem;
    background-color: var(--bg-secondary);
    border-radius: var(--radius-md);
    margin-bottom: 1rem;
}

.play-variables h4 {
    font-size: 0.8rem;
    color: var(--text-muted);
    text-transform: uppercase;
    margin-bottom: 0.5rem;
}

.play-var-item {
    display: flex;
    justify-content: space-between;
    padding: 0.25rem 0;
    font-size: 0.9rem;
}

.play-var-name {
    color: var(--text-secondary);
}

.play-var-value {
    color: var(--primary-color);
    font-weight: 500;
}

.play-ending {
    text-align: center;
    padding: 2rem;
    background-color: var(--bg-secondary);
    border-radius: var(--radius-lg);
    margin-top: 2rem;
}

.play-ending h3 {
    font-size: 1.5rem;
    color: var(--accent-color);
    margin-bottom: 0.5rem;
}

/* Modal */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: center;
}

.modal.hidden {
    display: none;
}

.modal-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.7);
}

.modal-content {
    position: relative;
    width: 90%;
    max-width: 500px;
    max-height: 80vh;
    background-color: var(--bg-secondary);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    display: flex;
    flex-direction: column;
}

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

.modal-header h3 {
    font-size: 1.25rem;
    font-weight: 600;
}

.modal-body {
    padding: 1.5rem;
    overflow-y: auto;
}

.help-text {
    font-size: 0.875rem;
    color: var(--text-muted);
    margin-bottom: 1rem;
}

.variables-list {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    margin-bottom: 1rem;
}

.variable-item {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.75rem;
    background-color: var(--bg-tertiary);
    border-radius: var(--radius-sm);
}

.variable-item span {
    flex: 1;
}

.variable-item .var-name {
    font-weight: 500;
    color: var(--primary-color);
}

.variable-item .var-value {
    color: var(--text-secondary);
}

.variable-item .var-type {
    font-size: 0.75rem;
    color: var(--text-muted);
    text-transform: uppercase;
}

.add-variable-form {
    display: flex;
    gap: 0.5rem;
    flex-wrap: wrap;
}

.add-variable-form input {
    flex: 1;
    min-width: 100px;
}

.add-variable-form select {
    width: 100px;
}

/* Footer */
.app-footer {
    padding: 1rem 2rem;
    background-color: var(--bg-secondary);
    border-top: 1px solid var(--border-color);
    text-align: center;
}

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

.app-footer a {
    color: var(--primary-color);
    text-decoration: none;
    font-weight: 500;
}

.app-footer a:hover {
    text-decoration: underline;
}

/* Utility */
.hidden {
    display: none !important;
}

/* Responsive Design */
@media (max-width: 1024px) {
    .node-editor-panel {
        width: 350px;
    }
}

@media (max-width: 768px) {
    .app-header {
        padding: 1rem;
    }

    .app-header h1 {
        font-size: 1.2rem;
    }

    .editor-layout, .play-layout {
        flex-direction: column;
    }

    .sidebar {
        width: 100%;
        height: auto;
        max-height: 200px;
        border-right: none;
        border-bottom: 1px solid var(--border-color);
    }

    .editor-toolbar {
        flex-direction: column;
        align-items: stretch;
    }

    .toolbar-left, .toolbar-right {
        justify-content: center;
    }

    .story-title-input {
        width: 100%;
        text-align: center;
    }

    .node-editor-panel {
        width: 100%;
        height: 60%;
        top: auto;
        bottom: 0;
        border-left: none;
        border-top: 1px solid var(--border-color);
    }

    .play-content {
        padding: 1rem;
    }

    .play-title {
        font-size: 1.5rem;
    }

    .modal-content {
        width: 95%;
        max-height: 90vh;
    }
}

@media (max-width: 480px) {
    .nav-btn {
        padding: 0.4rem 1rem;
        font-size: 0.9rem;
    }

    .btn {
        padding: 0.5rem 1rem;
        font-size: 0.8rem;
    }

    .toolbar-right {
        flex-wrap: wrap;
    }

    .choice-main-row {
        flex-direction: column;
    }

    .choice-main-row select {
        width: 100%;
    }
}

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

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

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

::-webkit-scrollbar-thumb:hover {
    background: var(--border-color);
}

/* Connection Lines (SVG overlay for node connections) */
.connections-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 0;
}

.connection-line {
    stroke: var(--border-color);
    stroke-width: 2;
    fill: none;
}

.connection-line:hover {
    stroke: var(--primary-color);
}
