* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --bg-primary: #1e1e1e;
    --bg-secondary: #2d2d2d;
    --bg-tertiary: #3e3e3e;
    --text-primary: #ffffff;
    --text-secondary: #cccccc;
    --accent: #007acc;
    --error: #ff6b6b;
    --success: #51cf66;
    --border: #444444;
    --font-mono: 'Consolas', 'Monaco', 'Courier New', monospace;
    --drawer-space: 320px; /* default reserved space for right drawer (updated dynamically by JS) */
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background-color: var(--bg-primary);
    color: var(--text-primary);
    overflow: hidden;
}

.app-container {
    display: flex;
    flex-direction: column;
    height: 100vh;
}

.toolbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.75rem 1.5rem;
    background-color: var(--bg-secondary);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
    position: relative;
}

/* New: center the H1 within the usable content area (accounts for right drawer via --drawer-space) */
.toolbar h1 {
    position: absolute;
    left: calc((100% - var(--drawer-space)) / 2);
    transform: translateX(-50%);
    margin: 0;
    font-size: 1rem;
    pointer-events: none;
    z-index: 1;
}

/* toolbar-right to position settings button */
.toolbar-right {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-left: 1rem;
}

/* Settings panel (right side) */
.settings-panel {
    position: fixed;
    top: 0;
    right: -360px; /* hidden by default */
    width: 320px;
    height: 100vh;
    background-color: var(--bg-secondary);
    border-left: 1px solid var(--border);
    box-shadow: -8px 0 24px rgba(0,0,0,0.4);
    transition: right 0.25s ease;
    z-index: 1100;
    display: flex;
    flex-direction: column;
}

.settings-panel.open {
    right: 0;
}

.settings-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem;
    background-color: var(--bg-tertiary);
    border-bottom: 1px solid var(--border);
    position: relative; /* added to allow centering the h3 */
}

.settings-header h3 {
    margin: 0;
    font-size: 1rem;
    position: absolute; /* center horizontally within header */
    left: 50%;
    transform: translateX(-50%);
    text-align: center;
    pointer-events: none; /* allow close button clicks through if overlapping */
}

.settings-body {
    padding: 1rem;
    overflow-y: auto;
    color: var(--text-secondary);
}

.settings-body label {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-bottom: 0.75rem;
    cursor: pointer;
}

.controls {
    display: flex;
    gap: 0.5rem;
    align-items: center;
}

.btn {
    padding: 0.5rem 1rem;
    border: none;
    border-radius: 4px;
    font-size: 0.875rem;
    cursor: pointer;
    transition: all 0.2s ease;
    font-weight: 500;
}

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

.btn-primary:hover {
    background-color: #005a9e;
}

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

.btn-secondary:hover {
    background-color: #4a4a4a;
}

.btn-small {
    padding: 0.25rem 0.5rem;
    font-size: 0.75rem;
}

.select {
    padding: 0.5rem;
    background-color: var(--bg-tertiary);
    color: var(--text-primary);
    border: 1px solid var(--border);
    border-radius: 4px;
    cursor: pointer;
}

.main-content {
    display: flex;
    flex: 1;
    overflow: hidden;
    /* Reserve space on the right for the drawer so it doesn't overlap the panel */
    margin-right: 320px; /* matches right-drawer width when open */
    transition: margin-right 0.28s cubic-bezier(.2,.9,.2,1);
}

.editors-container {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-width: 0;
}

.editor-wrapper {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
    border-right: 1px solid var(--border);
}

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

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

.CodeMirror {
    flex: 1;
    font-family: var(--font-mono);
    font-size: 14px;
    line-height: 1.5;
}

.resize-handle {
    width: 4px;
    background-color: var(--border);
    cursor: col-resize;
    position: relative;
}

.resize-handle:hover {
    background-color: var(--accent);
}

.output-container {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-width: 0;
}

.output-header {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    padding: 0.5rem 1rem;
    background-color: var(--bg-secondary);
    border-bottom: 1px solid var(--border);
    gap: 8px;
}
/* small menu button styling */
#outputMenuBtn {
    padding: 0.25rem 0.5rem;
    font-size: 0.9rem;
}

/* Output side menu (collapsible panel) */
.output-side-menu {
    position: absolute;
    top: 56px; /* below output header */
    right: calc(50% - 10px); /* positioned near the split; will be adjusted via JS */
    width: 286px; /* increased ~30% from 220px */
    max-width: 40%;
    height: calc(100% - 56px);
    background: var(--bg-secondary);
    border-left: 1px solid var(--border);
    box-shadow: -8px 0 24px rgba(0,0,0,0.4);
    transform: translateX(320px); /* adjusted to keep menu hidden off-canvas with larger width */
    transition: transform 0.22s ease;
    z-index: 1050;
    display: flex;
    flex-direction: column;
    pointer-events: none;
    opacity: 0;
}
.output-side-menu.open {
    transform: translateX(0);
    pointer-events: auto;
    opacity: 1;
}
.output-side-header {
    display:flex;
    justify-content:space-between;
    align-items:center;
    padding:10px;
    border-bottom:1px solid var(--border);
    background:var(--bg-tertiary);
}
.output-side-body {
    padding:10px;
    overflow:auto;
    color:var(--text-secondary);
}
/* ensure iframe and output container are positioned relative for absolute side menu */
.output-container {
    position: relative;
}

#outputFrame {
    flex: 1;
    border: none;
    background-color: white;
}

.console-output {
    height: 150px;
    background-color: var(--bg-secondary);
    border-top: 1px solid var(--border);
    padding: 0.5rem;
    font-family: var(--font-mono);
    font-size: 12px;
    overflow-y: auto;
    display: none;
}

.console-output.active {
    display: block;
}

.console-line {
    margin-bottom: 0.25rem;
}

.console-error {
    color: var(--error);
}

.console-log {
    color: var(--text-secondary);
}

.error-toast {
    position: fixed;
    top: 20px;
    right: 20px;
    background-color: var(--error);
    color: white;
    padding: 1rem 1.5rem;
    border-radius: 4px;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.3);
    transform: translateX(400px);
    transition: transform 0.3s ease;
    z-index: 1000;
}

.error-toast.show {
    transform: translateX(0);
}

.mobile-controls {
    display: none;
}

/* Modal Styles */
.modal {
    display: none;
    position: fixed;
    z-index: 1000;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    align-items: center;
    justify-content: center;
}

.modal-content {
    background-color: var(--bg-secondary);
    border-radius: 8px;
    width: 90%;
    max-width: 600px;
    max-height: 80vh;
    overflow: hidden;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
}

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

.modal-header h2 {
    margin: 0;
    font-size: 1.25rem;
}

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

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

.modal-body {
    padding: 1.5rem;
}

.idea-prompt {
    background-color: var(--bg-tertiary);
    padding: 0.75rem;
    border-radius: 4px;
    margin-bottom: 1rem;
    border-left: 4px solid var(--accent);
    font-style: italic;
}

#aiPrompt {
    width: 100%;
    min-height: 100px;
    padding: 0.75rem;
    background-color: var(--bg-primary);
    color: var(--text-primary);
    border: 1px solid var(--border);
    border-radius: 4px;
    font-family: inherit;
    font-size: 0.9rem;
    resize: vertical;
}

.ai-options {
    display: flex;
    gap: 1rem;
    margin-top: 1rem;
}

.ai-options label {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    cursor: pointer;
}

.ai-options input[type="checkbox"] {
    accent-color: var(--accent);
}

.modal-footer {
    display: flex;
    gap: 1rem;
    justify-content: flex-end;
    padding: 1rem 1.5rem;
    background-color: var(--bg-tertiary);
    border-top: 1px solid var(--border);
}

/* Responsive Design */
@media (max-width: 768px) {
    .toolbar {
        padding: 0.5rem 1rem;
    }
    
    .toolbar h1 {
        font-size: 1rem;
    }
    
    .controls {
        gap: 0.25rem;
    }
    
    .btn {
        padding: 0.4rem 0.8rem;
        font-size: 0.75rem;
    }
    
    .main-content {
        flex-direction: column;
    }
    
    .editors-container {
        flex-direction: row;
        flex-wrap: wrap;
    }
    
    .editor-wrapper {
        min-width: 100%;
        border-right: none;
        border-bottom: 1px solid var(--border);
    }
    
    .resize-handle {
        width: 100%;
        height: 4px;
        cursor: row-resize;
    }
    
    .mobile-controls {
        display: block;
        position: fixed;
        bottom: 20px;
        left: 20px;
        z-index: 100;
    }
    
    .joystick-container {
        width: 120px;
        height: 120px;
    }
    
    /* Responsive modal */
    .modal-content {
        width: 95%;
        margin: 1rem;
    }
    
    .modal-body {
        padding: 1rem;
    }
    
    .ai-options {
        flex-direction: column;
        gap: 0.5rem;
    }
    
    .modal-footer {
        flex-direction: column;
        gap: 0.5rem;
    }
}

/* Collapsed editor state */
.editor-wrapper.collapsed {
    flex: 0 0 auto;
}

.editor-wrapper.collapsed .CodeMirror {
    display: none;
}

/* Auto-run indicator */
#autoRunBtn.active {
    background-color: var(--success);
    color: white;
}

/* System prompt list item */
.system-prompt-item {
    background-color: var(--bg-tertiary);
    border: 1px solid var(--border);
    padding: 8px;
    border-radius: 6px;
    display: flex;
    gap: 8px;
    align-items: flex-start;
    justify-content: space-between;
    font-size: 0.9rem;
    color: var(--text-secondary);
}

.system-prompt-meta {
    display:flex;
    flex-direction:column;
    gap:4px;
    flex:1;
    min-width:0;
}

.system-prompt-actions {
    display:flex;
    gap:6px;
    align-items:center;
}

/* small badge for inactive prompts */
.system-prompt-title {
    font-weight:600;
    color:var(--text-primary);
    overflow:hidden;
    text-overflow:ellipsis;
    white-space:nowrap;
}

/* Right-side collapsible drawer */
.right-drawer {
    position: fixed;
    top: 0;
    right: 0;
    height: 100vh;
    width: 320px;
    max-width: 80%;
    background: var(--bg-secondary);
    border-left: 1px solid var(--border);
    box-shadow: -8px 0 24px rgba(0,0,0,0.35);
    transform: translateX(100%); /* hidden by default */
    transition: transform 0.28s cubic-bezier(.2,.9,.2,1);
    z-index: 1200;
    display: flex;
    align-items: stretch;
    pointer-events: none;
    overflow: hidden;
}

/* When drawer is collapsed we only show thin tab and reduce reserved space */
.right-drawer.collapsed {
    width: 46px; /* accommodate the thin tab area */
    transform: translateX(calc(100% - 46px));
    pointer-events: auto; /* allow hovering / clicking icons when collapsed so tooltips work */
}

/* When drawer is open remove the translate so it's visible and ensure main content margin stays consistent */
.right-drawer.open {
    transform: translateX(0);
    pointer-events: auto;
}

/* Adjust main-content margin when drawer is collapsed to avoid unnecessary empty space */
.right-drawer.collapsed ~ .main-content,
.right-drawer.collapsed + .main-content {
    margin-right: 46px;
}

/* Ensure main-content uses full width again if drawer is removed (safety) */
.right-drawer:not(.collapsed) ~ .main-content,
.right-drawer.open ~ .main-content {
    margin-right: 320px;
}

/* Thin vertical tab anchored to the right bottom of the drawer */
.drawer-tab {
    position: absolute;
    left: 14px;
    bottom: 14px;
    transform: translateX(0);
    width: 36px;
    height: 36px;
    border-radius: 8px;
    border: none;
    background: var(--bg-tertiary);
    color: var(--text-primary);
    display:flex;
    align-items:center;
    justify-content:center;
    cursor: pointer;
    font-size: 1rem;
    box-shadow: 0 6px 14px rgba(0,0,0,0.35);
    transition: transform 0.2s ease, background 0.15s ease;
    z-index: 1210;
    pointer-events: auto;
}

.drawer-tab:hover { transform: translateX(0) scale(1.04); background: #4a4a4a; }

/* Arrow rotates when drawer is open */
.right-drawer.open .drawer-tab .drawer-tab-icon {
    transform: rotate(180deg);
    transition: transform 0.22s ease;
}

.drawer-tab .drawer-tab-icon {
    display:inline-block;
    transition: transform 0.22s ease;
    font-size: 1.1rem;
    line-height: 1;
}

/* Small responsive adjustments */
@media (max-width: 768px) {
    .right-drawer {
        width: 260px;
    }
    .right-drawer.collapsed {
        width: 46px;
    }
}

/* Icon-only button styling for drawer */
.icon-btn {
    width: 40px; /* reduced from 44px (~10% smaller) */
    height: 40px; /* reduced from 44px */
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: 8px;
    border: none;
    font-size: 0.83rem; /* reduced from 0.92rem (~10% smaller) */
    cursor: pointer;
    padding: 0;
    background: var(--bg-tertiary);
    color: var(--text-primary);
    box-shadow: none;
}
.icon-btn:hover { background: #4a4a4a; transform: translateY(-2px); }
.icon-btn.btn-primary { background: var(--accent); color: white; }
.icon-btn.btn-primary:hover { background: #005a9e; }

/* Ensure the drawer tab stays bottom-left both when collapsed and open */
.right-drawer .drawer-tab {
    left: 14px;
    bottom: 14px;
    transform: translateX(0);
}

.right-drawer.open .drawer-tab {
    /* keep same bottom-left anchor when open, only rotate icon */
    left: 14px;
    right: auto;
    transform: translateX(0);
}

.custom-tooltip {
    position: fixed;
    background: var(--bg-tertiary);
    color: var(--text-primary);
    padding: 6px 8px;
    border-radius: 6px;
    font-size: 0.85rem;
    box-shadow: 0 6px 16px rgba(0,0,0,0.35);
    pointer-events: none;
    z-index: 2000;
    transform-origin: left top;
    transition: opacity 0.12s ease, transform 0.12s ease;
    opacity: 0;
    transform: translateY(-6px) scale(0.98);
}
.custom-tooltip.show {
    opacity: 1;
    transform: translateY(0) scale(1);
}