/* ================================================================
   CODE STUDIO - Google AI Studio Side Panel Layout
   Created by: Dakota Stewart
   Delphi Labs - AGI MODEL 7

   Side panel layout that splits the chat view: chat LEFT, preview RIGHT.
   Activated by adding .cs-split-active to .chat-think-wrapper.
   Dark theme matching Google AI Studio (#1b1b1f / #8ab4f8).
   ================================================================ */

/* ================================================================
   Z-INDEX HIERARCHY (Issue #7)
   --z-base:       1-10      — Base content, inline overlays
   --z-panel:      100       — Panels, sidebars
   --z-dropdown:   500       — Dropdowns, tooltips
   --z-modal:      1000      — Overlays, modals
   --z-fullscreen: 5000      — Fullscreen panels
   --z-toast:      10000     — Toast notifications
   --z-critical:   50000     — Critical overlays (mobile takeover)
   ================================================================ */
:root {
    --z-base: 5;
    --z-panel: 100;
    --z-dropdown: 500;
    --z-modal: 1000;
    --z-fullscreen: 5000;
    --z-toast: 10000;
    --z-critical: 50000;
}


/* ================================================================
   1. SPLIT LAYOUT - Chat left, Code Studio panel right
   ================================================================ */

/* FIX #54: Use CSS custom property for chat width so JS drag handler can update it */
.chat-think-wrapper.cs-split-active {
    --chat-width: 42%;
    display: flex !important;
    flex-direction: row !important;
}

/* Chat narrows to left side */
.chat-think-wrapper.cs-split-active .chat-container {
    flex: 0 0 var(--chat-width, 42%);
    max-width: var(--chat-width, 42%);
    border-right: 1px solid #303036;
    overflow-y: auto;
}

/* Welcome overlay constrained in split mode */
.chat-think-wrapper.cs-split-active .welcome-overlay {
    max-width: var(--chat-width, 42%);
}

/* Don't animate the flex change on the wrapper */
.chat-think-wrapper {
    transition: none;
}


/* ================================================================
   2. SIDE PANEL - Right-hand preview/code panel
   ================================================================ */

.cs-side-panel {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-width: 0;
    background: #1b1b1f;
    height: 100%;
    overflow: hidden;
    transition: opacity 0.2s ease;
    opacity: 0;
    pointer-events: none;
}

.chat-think-wrapper.cs-split-active .cs-side-panel {
    opacity: 1;
    pointer-events: auto;
}

.cs-side-panel.cs-visible {
    opacity: 1;
    pointer-events: auto;
}


/* ================================================================
   3. PANEL HEADER - Preview / Code tabs + action buttons
   ================================================================ */

.cs-panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 16px;
    border-bottom: 1px solid #303036;
    background: #1b1b1f;
    min-height: 44px;
    flex-shrink: 0;
}

.cs-panel-tabs {
    display: flex;
    gap: 4px;
}

.cs-panel-tab {
    padding: 6px 16px;
    border: none;
    background: transparent;
    color: #b0b5bb;
    font-size: 14px;
    cursor: pointer;
    border-radius: 20px;
    transition: all 0.15s ease;
    font-family: 'Google Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI',
                 Roboto, 'Helvetica Neue', Arial, sans-serif;
    line-height: 1.4;
}

.cs-panel-tab:hover {
    background: #28282e;
    color: #e3e3e3;
}

.cs-panel-tab.active {
    background: #303036;
    color: #e3e3e3;
}

.cs-tab-dot {
    color: #8ab4f8;
    margin-right: 4px;
}

.cs-panel-actions {
    display: flex;
    gap: 4px;
}

.cs-panel-action-btn {
    width: 32px;
    height: 32px;
    border: none;
    background: transparent;
    color: #9aa0a6;
    cursor: pointer;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s ease;
    font-size: 16px;
    line-height: 1;
}

.cs-panel-action-btn:hover {
    background: #303036;
    color: #e3e3e3;
}

.cs-panel-action-btn:active {
    background: #3a3a40;
}


/* ================================================================
   4. PANEL BODY - Preview iframe & Code editor views
   ================================================================ */

.cs-panel-body {
    flex: 1;
    min-height: 0;
    overflow: hidden;
    position: relative;
    background: #131316;
}

/* --- Preview View --- */

.cs-preview-view {
    width: 100%;
    height: 100%;
    position: relative;
}

.cs-preview-iframe {
    width: 100%;
    height: 100%;
    border: none;
    background: #fff;
    display: block;
}

.cs-preview-loading {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 12px;
    background: rgba(19, 19, 22, 0.9);
    color: #9aa0a6;
    font-size: 14px;
    font-family: 'Google Sans', -apple-system, sans-serif;
    z-index: var(--z-base);
}

.cs-preview-spinner {
    width: 24px;
    height: 24px;
    border: 2px solid #303036;
    border-top-color: #8ab4f8;
    border-radius: 50%;
    animation: cs-spin 0.6s linear infinite;
    will-change: transform;
}

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

/* --- Code Loading Overlay (Issue #64) --- */

.cs-code-loading {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 12px;
    background: rgba(30, 30, 30, 0.9);
    color: #9aa0a6;
    font-size: 14px;
    font-family: 'Google Sans', -apple-system, sans-serif;
    z-index: var(--z-base);
    pointer-events: none;
}

.cs-code-loading .cs-preview-spinner {
    width: 24px;
    height: 24px;
}

/* --- Code View --- */

.cs-code-view {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    background: #1e1e1e;
}

.cs-code-tabs {
    display: flex;
    background: #252526;
    border-bottom: 1px solid #303036;
    overflow-x: auto;
    flex-shrink: 0;
}

.cs-code-tab {
    padding: 8px 16px;
    border: none;
    background: transparent;
    color: #b0b5bb;
    font-size: 13px;
    font-family: 'JetBrains Mono', 'Fira Code', 'SF Mono', 'Cascadia Code', 'Consolas', monospace;
    cursor: pointer;
    border-bottom: 2px solid transparent;
    white-space: nowrap;
    transition: all 0.15s ease;
}

.cs-code-tab:hover {
    background: #2a2a2d;
}

.cs-code-tab.active {
    color: #e3e3e3;
    border-bottom-color: #8ab4f8;
    background: #1e1e1e;
}

.cs-code-editor-wrap {
    flex: 1;
    display: flex;
    overflow: hidden;
    min-height: 0;
    position: relative;
}

/* Monaco Editor Container */
.cs-monaco-container {
    flex: 1;
    min-height: 0;
    min-width: 0;
    position: relative;
}

/* Hide line numbers div - Monaco has its own */
.cs-code-line-numbers {
    display: none;
}

/* Hidden textarea fallback (only shown if Monaco fails to load) */
.cs-code-textarea {
    flex: 1;
    padding: 8px 16px;
    background: #1e1e1e;
    color: #d4d4d4;
    border: none;
    outline: none;
    resize: none;
    font-family: 'JetBrains Mono', 'Fira Code', 'SF Mono', 'Cascadia Code', 'Consolas', monospace;
    font-size: 13px;
    line-height: 1.6;
    tab-size: 4;
    -moz-tab-size: 4;
    min-width: 0;
    white-space: pre;
    display: none; /* Hidden when Monaco active */
}

.cs-code-textarea.cs-fallback-active {
    display: block;
}

.cs-code-textarea::placeholder {
    color: #5a5a5a;
}

/* Monaco minimap styling override */
.cs-monaco-container .minimap {
    opacity: 0.8;
}

/* Monaco suggestions widget dark override */
.cs-monaco-container .suggest-widget {
    border-color: #303036 !important;
}

/* --- Code Status Bar (below editor) --- */

.cs-code-statusbar {
    display: flex;
    align-items: center;
    padding: 4px 16px;
    background: #252526;
    border-top: 1px solid #303036;
    font-family: 'JetBrains Mono', 'Fira Code', 'SF Mono', 'Consolas', monospace;
    font-size: 12px;
    color: #9aa0a6;
    flex-shrink: 0;
    min-height: 24px;
}

.cs-status-file {
    color: #e3e3e3;
    margin-right: 4px;
}

.cs-status-sep {
    color: #5a5a5a;
    margin: 0 8px;
}

.cs-status-lang {
    color: #8ab4f8;
}

.cs-status-cursor {
    color: #9aa0a6;
}

.cs-status-saved {
    color: #81c995;
}

.cs-status-saved.unsaved {
    color: #f9ab00;
}

/* --- Line Numbers --- */

.cs-line-num {
    line-height: 1.6;
    font-size: 13px;
}


/* ================================================================
   5. CONSOLE PANEL - Bottom of side panel
   ================================================================ */

.cs-panel-console {
    flex-shrink: 0;
    border-top: 1px solid #303036;
    background: #1b1b1f;
    max-height: 200px;
    display: flex;
    flex-direction: column;
}

.cs-console-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 4px 12px;
    border-bottom: 1px solid #252528;
    min-height: 32px;
    flex-shrink: 0;
}

.cs-console-tabs {
    display: flex;
}

.cs-console-tab {
    padding: 4px 12px;
    border: none;
    background: transparent;
    color: #b0b5bb;
    font-size: 12px;
    cursor: pointer;
    border-bottom: 2px solid transparent;
    transition: all 0.15s ease;
    font-family: 'Google Sans', -apple-system, sans-serif;
}

.cs-console-tab:hover {
    color: #e3e3e3;
}

.cs-console-tab.active {
    color: #8ab4f8;
    border-bottom-color: #8ab4f8;
}

.cs-console-actions {
    display: flex;
    gap: 4px;
}

.cs-console-action-btn {
    width: 24px;
    height: 24px;
    border: none;
    background: transparent;
    color: #9aa0a6;
    cursor: pointer;
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    transition: background 0.15s ease;
}

.cs-console-action-btn:hover {
    background: #303036;
    color: #e3e3e3;
}

.cs-console-body {
    flex: 1;
    overflow-y: auto;
    padding: 8px 12px;
    font-family: 'JetBrains Mono', 'Fira Code', 'SF Mono', 'Cascadia Code', 'Consolas', monospace;
    font-size: 12px;
    line-height: 1.5;
    min-height: 60px;
    max-height: 240px;
}

.cs-console-empty {
    color: #5a5a5a;
    font-style: italic;
    padding: 12px 0;
    text-align: center;
}

.cs-panel-console.cs-console-hidden {
    display: none !important;
}

.cs-console-entry {
    padding: 2px 0;
    display: flex;
    gap: 8px;
    align-items: flex-start;
}

.cs-console-entry-type {
    flex-shrink: 0;
    font-weight: 600;
    text-transform: uppercase;
    font-size: 10px;
    padding: 1px 4px;
    border-radius: 2px;
    line-height: 1.6;
}

.cs-console-entry[data-type="log"] .cs-console-entry-type {
    color: #9aa0a6;
    background: #252528;
}

.cs-console-entry[data-type="warn"] .cs-console-entry-type {
    color: #f9ab00;
    background: #3d2e00;
}

.cs-console-entry[data-type="error"] .cs-console-entry-type {
    color: #f28b82;
    background: #3d1c1c;
}

.cs-console-entry[data-type="info"] .cs-console-entry-type {
    color: #8ab4f8;
    background: #1c2a3d;
}

.cs-console-entry-message {
    color: #d4d4d4;
    word-break: break-word;
    flex: 1;
    min-width: 0;
}

.cs-console-entry-time {
    color: #5a5a5a;
    margin-left: auto;
    flex-shrink: 0;
    font-size: 11px;
    white-space: nowrap;
}

.cs-console-input-wrap {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 12px;
    border-top: 1px solid #252528;
    background: #1b1b1f;
    flex-shrink: 0;
}

.cs-console-prompt {
    color: #8ab4f8;
    font-family: 'JetBrains Mono', 'Fira Code', 'SF Mono', 'Cascadia Code', 'Consolas', monospace;
    font-weight: 600;
    font-size: 12px;
    flex-shrink: 0;
}

.cs-console-input {
    flex: 1;
    background: transparent;
    border: none;
    outline: none;
    color: #d4d4d4;
    font-family: 'JetBrains Mono', 'Fira Code', 'SF Mono', 'Cascadia Code', 'Consolas', monospace;
    font-size: 12px;
    min-width: 0;
}

.cs-console-input::placeholder {
    color: #5a5a5a;
}

.cs-console-run-btn {
    padding: 2px 10px;
    background: transparent;
    border: 1px solid #303036;
    color: #9aa0a6;
    font-size: 12px;
    cursor: pointer;
    border-radius: 4px;
    transition: all 0.15s ease;
    flex-shrink: 0;
    font-family: 'Google Sans', -apple-system, sans-serif;
}

.cs-console-run-btn:hover {
    background: #303036;
    color: #e3e3e3;
}


/* ================================================================
   6. STATUS BAR - Bottom edge of the panel
   ================================================================ */

.cs-panel-statusbar {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    padding: 4px 12px;
    background: #1b1b1f;
    border-top: 1px solid #252528;
    min-height: 24px;
    flex-shrink: 0;
    gap: 8px;
    font-size: 11px;
    color: #9aa0a6;
    font-family: 'Google Sans', -apple-system, sans-serif;
}

.cs-badge {
    font-size: 11px;
    padding: 2px 8px;
    border-radius: 8px;
    margin-left: 8px;
    font-weight: 500;
}

.cs-badge-error {
    color: #f28b82;
    background: #3d1c1c;
}

.cs-badge-warn {
    color: #f9ab00;
    background: #3d2e00;
}

.cs-badge-success {
    color: #81c995;
    background: #1c3d22;
}

.cs-badge-info {
    color: #8ab4f8;
    background: #1c2a3d;
}


/* ================================================================
   7. FULLSCREEN MODE - Panel takes over entire viewport
   ================================================================ */

.cs-side-panel.cs-fullscreen {
    position: fixed !important;
    top: 0 !important;
    right: 0 !important;
    bottom: 0 !important;
    left: 0 !important;
    inset: 0 !important;
    z-index: var(--z-fullscreen) !important;
    flex: none !important;
    max-width: 100% !important;
    width: 100% !important;
}


/* ================================================================
   8. RESPONSIVE - Mobile / small screens (iOS optimized)
   ================================================================ */

@media (max-width: 768px) {

    /* --- HIDE chat + welcome when studio is active --- */
    .chat-think-wrapper.cs-split-active .chat-container {
        display: none !important;
    }

    .chat-think-wrapper.cs-split-active .welcome-overlay {
        display: none !important;
    }

    /* --- STUDIO TAKES OVER: fixed fullscreen overlay on mobile ---
       This positions the panel above the nav bar, input area, and
       everything else so the studio gets the full viewport.
       Uses --z-critical to sit above all other UI layers. */
    .chat-think-wrapper.cs-split-active .cs-side-panel {
        position: fixed !important;
        top: 0 !important;
        left: 0 !important;
        right: 0 !important;
        bottom: 0 !important;
        z-index: var(--z-critical) !important;
        max-width: 100% !important;
        width: 100% !important;
        height: 100% !important;
        /* Bottom safe area handled by status bar; top by header */
        background: #1b1b1f !important;
    }

    /* --- PANEL HEADER: iOS touch-friendly --- */
    .cs-panel-header {
        padding: 8px 12px;
        padding-top: max(8px, env(safe-area-inset-top, 0px));
        min-height: calc(48px + max(0px, env(safe-area-inset-top, 0px) - 8px));
        gap: 8px;
        /* Prevent iOS rubber-band from pulling header offscreen */
        -webkit-user-select: none;
        user-select: none;
    }

    .cs-panel-tab {
        padding: 8px 14px;
        font-size: 14px;
        /* iOS minimum touch target */
        min-height: 36px;
        display: inline-flex;
        align-items: center;
    }

    /* Action buttons: meet iOS 44px touch target minimum */
    .cs-panel-action-btn {
        width: 44px;
        height: 44px;
        font-size: 18px;
        border-radius: 10px;
        -webkit-tap-highlight-color: transparent;
        touch-action: manipulation;
    }

    .cs-panel-action-btn:active {
        background: #3a3a40;
        transform: scale(0.95);
    }

    /* --- PANEL BODY: fill remaining space --- */
    .cs-panel-body {
        flex: 1;
        min-height: 0;
    }

    /* --- PREVIEW IFRAME: full area, allow touch scroll --- */
    .cs-preview-iframe {
        -webkit-overflow-scrolling: touch;
    }

    /* --- CODE VIEW: optimize for mobile editing --- */
    .cs-code-tabs {
        -webkit-overflow-scrolling: touch;
    }

    .cs-code-tab {
        padding: 10px 14px;
        font-size: 13px;
        min-height: 40px;
    }

    .cs-code-statusbar {
        padding: 6px 12px;
        font-size: 11px;
        min-height: 28px;
    }

    /* Monaco: slightly larger font on mobile for readability */
    .cs-code-textarea {
        font-size: 14px;
    }

    /* --- CONSOLE: compact for mobile, respect bottom safe area --- */
    .cs-panel-console {
        max-height: 160px;
    }

    .cs-console-body {
        max-height: 150px;
        font-size: 12px;
    }

    .cs-console-action-btn {
        width: 36px;
        height: 36px;
        min-width: 36px;
        min-height: 36px;
        -webkit-tap-highlight-color: transparent;
        touch-action: manipulation;
    }

    .cs-console-input-wrap {
        padding: 8px 12px;
    }

    .cs-console-input {
        font-size: 14px;
        min-height: 44px;
    }

    .cs-console-run-btn {
        padding: 6px 14px;
        min-height: 36px;
        font-size: 13px;
        -webkit-tap-highlight-color: transparent;
        touch-action: manipulation;
    }

    /* --- STATUS BAR: account for bottom safe area --- */
    .cs-panel-statusbar {
        padding: 6px 12px;
        padding-bottom: calc(6px + env(safe-area-inset-bottom, 0px));
        min-height: calc(28px + env(safe-area-inset-bottom, 0px));
    }

    /* --- FULLSCREEN MODE: same as above but explicit safe areas --- */
    .cs-side-panel.cs-fullscreen {
        padding-top: env(safe-area-inset-top, 0px) !important;
        padding-bottom: env(safe-area-inset-bottom, 0px) !important;
    }
}

/* --- iPhone SE / extra-small devices --- */
@media (max-width: 375px) {
    .cs-panel-header {
        padding-left: 8px;
        padding-right: 8px;
    }

    .cs-panel-tab {
        padding: 6px 10px;
        font-size: 13px;
    }

    .cs-panel-action-btn {
        width: 40px;
        height: 40px;
    }

    .cs-console-body {
        max-height: 70px;
    }
}

/* --- Landscape on mobile: more horizontal space, less vertical --- */
@media (max-width: 768px) and (orientation: landscape) {
    .chat-think-wrapper.cs-split-active .cs-side-panel {
        padding-top: 0;
        padding-left: env(safe-area-inset-left, 0px);
        padding-right: env(safe-area-inset-right, 0px);
    }

    .cs-panel-header {
        padding-top: 4px;
        min-height: 40px;
    }

    .cs-panel-console {
        max-height: 100px;
    }

    .cs-console-body {
        max-height: 50px;
    }
}


/* ================================================================
   10. SCROLLBAR STYLING
   ================================================================ */

/* Firefox scrollbar styling */
.cs-console-body {
    scrollbar-width: thin;
    scrollbar-color: #404046 transparent;
}

.cs-code-tabs {
    scrollbar-width: thin;
    scrollbar-color: #404046 transparent;
}

/* Webkit scrollbar styling */
.cs-console-body::-webkit-scrollbar {
    width: 6px;
}

.cs-console-body::-webkit-scrollbar-thumb {
    background: #404046;
    border-radius: 3px;
}

.cs-console-body::-webkit-scrollbar-track {
    background: transparent;
}

/* Scrollbar for code tabs overflow */
.cs-code-tabs::-webkit-scrollbar {
    height: 4px;
}

.cs-code-tabs::-webkit-scrollbar-thumb {
    background: #404046;
    border-radius: 2px;
}

.cs-code-tabs::-webkit-scrollbar-track {
    background: transparent;
}


/* ================================================================
   11. EMPTY STATE - When no content is loaded yet
   ================================================================ */

.cs-empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    gap: 16px;
    color: #9aa0a6;
    font-family: 'Google Sans', -apple-system, sans-serif;
    padding: 32px;
    text-align: center;
}

.cs-empty-state-icon {
    font-size: 48px;
    opacity: 0.4;
    color: #8ab4f8;
}

.cs-empty-state-title {
    font-size: 16px;
    color: #e3e3e3;
    font-weight: 500;
}

.cs-empty-state-description {
    font-size: 13px;
    color: #9aa0a6;
    max-width: 280px;
    line-height: 1.5;
}


/* ================================================================
   12. RESIZE HANDLE - Drag to resize chat/panel split
   ================================================================ */

.cs-resize-handle {
    width: 8px;
    margin-left: -2px;
    cursor: col-resize;
    background: transparent;
    flex-shrink: 0;
    position: relative;
    z-index: var(--z-base);
    transition: background 0.15s ease;
}

.cs-resize-handle:hover,
.cs-resize-handle.cs-dragging {
    background: #8ab4f8;
}

.cs-resize-handle::after {
    content: '';
    position: absolute;
    top: 0;
    bottom: 0;
    left: -4px;
    right: -4px;
}


/* ================================================================
   13. AI AUTOCOMPLETE STATUS INDICATORS (Issue #70)
   ================================================================ */

.saic-loading {
    animation: saic-pulse 1s ease-in-out infinite;
}

.saic-loading::after {
    content: '';
    display: inline-block;
    width: 8px;
    height: 8px;
    margin-left: 4px;
    border: 1.5px solid #8ab4f8;
    border-top-color: transparent;
    border-radius: 50%;
    animation: cs-spin 0.6s linear infinite;
    vertical-align: middle;
    will-change: transform;
}

@keyframes saic-pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

.saic-error {
    color: #f28b82 !important;
}

.saic-disabled {
    opacity: 0.4;
}


