/* --- Main Layout --- */
body { /* Assuming similar base styles */
    background-color: #100f17; /* Lighter background for the page */
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
    /* ... other base styles */
}

/* Global typography improvements */
h1, h2, h3, h4, h5, h6 {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
    font-weight: 600;
}

label, button, input, textarea, select {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
}

.chat-page-container {
    display: flex;
    height: calc(100vh - 40px); /* Full height minus padding */
    max-width: 1600px; /* Even wider container for better space utilization */
    margin: 20px auto;
    background-color: #1a2a40; /* Base dark color */
    border-radius: 8px;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
    overflow: hidden;
    min-height: 600px; /* Ensure minimum usable height */
}

.panel-header {
    padding: 15px;
    border-bottom: 1px solid #4a5568;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

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

.panel-toggle-btn {
    background: transparent;
    border: none;
    color: #a0b3c6;
    cursor: pointer;
    padding: 8px;
    border-radius: 4px;
    transition: color 0.2s ease, background-color 0.2s ease;
    display: flex;
    align-items: center;
    justify-content: center;
}

.panel-toggle-btn:hover {
    color: #f97316;
    background-color: rgba(249, 115, 22, 0.1);
}

.panel-toggle-btn i {
    transition: transform 0.3s ease;
}

.connections-panel.collapsed .panel-toggle-btn i {
    transform: rotate(180deg);
}

.results-panel.collapsed .panel-toggle-btn i {
    transform: rotate(180deg);
}
.panel-header h3 {
    margin: 0;
    color: #f97316; /* Orange titles */
    font-size: 1.1em;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
    font-weight: 600;
}

/* --- Connections Panel (Left) --- */
.connections-panel {
    flex: 0 0 260px; /* Slightly narrower to give more space to results */
    background-color: #2d3748; /* Slightly lighter */
    display: flex;
    flex-direction: column;
    border-right: 1px solid #4a5568;
    transition: flex-basis 0.3s ease, min-width 0.3s ease;
    overflow: hidden;
    min-width: 260px;
}

.connections-panel.collapsed {
    flex: 0 0 50px; /* Collapsed width - just enough for toggle button */
    min-width: 50px;
}

.connections-panel.collapsed .connections-list,
.connections-panel.collapsed .add-connection-btn {
    display: none;
}

.connections-panel.collapsed .panel-header h3 {
    display: none;
}

.connections-list {
    list-style: none;
    padding: 0;
    margin: 0;
    overflow-y: auto;
    flex-grow: 1; /* Takes remaining space */
}

.connection-item {
    display: flex;
    align-items: flex-start;
    padding: 12px 15px;
    border-bottom: 1px solid #4a5568;
    cursor: pointer;
    transition: background-color 0.2s ease;
    min-height: 44px;
     position: relative;
    -webkit-tap-highlight-color: transparent; /* Remove default mobile tap highlight */
    touch-action: manipulation; /* Optimize for touch */
}
.connection-item:hover {
    background-color: #4a5568;
}

.connection-item.tapped,
.connection-item.touching {
    background-color: #5a6678; /* Slightly lighter than hover for visual feedback */
    transition: background-color 0.2s ease;
}

/* Ensure iOS Safari properly handles touch events */
@media (pointer: coarse) {
    .connection-item {
        min-height: 48px; /* Slightly larger touch target on mobile */
    }
    
    .connection-item label,
    .settings-btn {
        /* Improve touch targets on mobile */
        min-height: 44px;
    }
}
.connection-item input[type="checkbox"] {
    margin-right: 10px;
    cursor: pointer;
    margin-top: 2px; /* Align with first line of text */
    flex-shrink: 0;
}
.connection-item label {
    flex-grow: 1;
    display: flex;
    align-items: flex-start;
    cursor: pointer;
    min-width: 0; /* Allow shrinking */
    overflow: hidden;
    width: 100%; /* Ensure full tap area on mobile */
    -webkit-tap-highlight-color: transparent; /* iOS Safari fix */
    touch-action: manipulation; /* Optimize for touch */
    user-select: none; /* Prevent text selection on mobile */
}
.status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    margin-right: 8px;
    background-color: #a0b3c6; /* Default/unknown */
    flex-shrink: 0;
    margin-top: 6px; /* Center with first line of text */
}
.status-dot.active { background-color: #28a745; } /* Green */
.status-dot.error { background-color: #ef4444; } /* Red */

.connection-name {
    color: #e2e8f0;
    font-size: 0.95em;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
    font-weight: 500;
    word-wrap: break-word;
    word-break: break-word;
    hyphens: auto;
    line-height: 1.4;
    flex: 1;
    min-width: 0;
    margin-top: -2px; /* Align with first line of text */
}
.settings-btn {
    background: none;
    border: none;
    color: #a0aec0;
    cursor: pointer;
    padding: 8px;
    margin: -8px 0 -8px 10px;
    position: relative;
    z-index: 2; /* Ensure it's above other elements */
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
    flex-shrink: 0;
    align-self: flex-start;
}
.settings-btn:hover { color: #e2e8f0; }

.add-connection-btn {
    /* Style like other buttons */
    display: block;
    width: calc(100% - 30px);
    margin: 15px;
    padding: 10px;
    background-color: #f97316;
    color: #0a192f;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    text-align: center;
}
.add-connection-btn i { margin-right: 5px; }

/* --- Chat Panel (Center) --- */
.chat-panel {
    flex-grow: 1; /* Takes remaining space */
    background-color: #1a2a40; /* Darkest */
    display: flex;
    flex-direction: column;
    transition: all 0.3s ease;
    min-width: 300px; /* Minimum width to maintain usability */
}

/* Enhanced panel collapsing behavior for better space utilization */
.connections-panel.collapsed ~ .chat-panel {
    margin-left: 0;
}

.chat-panel ~ .results-panel.collapsed {
    margin-right: 0;
}

/* Adaptive chat panel sizing when panels are collapsed */
.connections-panel.collapsed ~ .chat-panel {
    flex-grow: 1;
    max-width: none;
}

.results-panel.collapsed ~ .chat-panel {
    flex-grow: 1;
    max-width: none;
}

.connections-panel.collapsed ~ .chat-panel .chat-history {
    max-width: none;
}

.results-panel.collapsed ~ .chat-panel .chat-history {
    max-width: none;
}

/* When both panels are collapsed - maximize chat space */
.connections-panel.collapsed ~ .chat-panel ~ .results-panel.collapsed {
    margin-left: 0;
}

.connections-panel.collapsed ~ .chat-panel:has(~ .results-panel.collapsed) {
    flex-grow: 1;
    max-width: none;
    margin-left: 0;
    margin-right: 0;
}

/* Smart panel auto-collapse on smaller screens */
@media (max-width: 1300px) {
    .connections-panel:not(.user-expanded) {
        flex: 0 0 50px;
    }
    .connections-panel:not(.user-expanded) .connections-list,
    .connections-panel:not(.user-expanded) .add-connection-btn,
    .connections-panel:not(.user-expanded) .panel-header h3 {
        display: none;
    }
}

@media (max-width: 1100px) {
    .results-panel:not(.user-expanded) {
        flex: 0 0 50px;
    }
    .results-panel:not(.user-expanded) .results-content,
    .results-panel:not(.user-expanded) .performance-panel,
    .results-panel:not(.user-expanded) .results-placeholder,
    .results-panel:not(.user-expanded) .panel-header h3 {
        display: none;
    }
}

.chat-history {
    flex-grow: 1;
    padding: 20px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 15px;
    max-width: 700px;
    transition: all 0.3s ease;
    min-height: 200px;
    max-height: calc(100vh - 300px);
}

.chat-history.collapsed {
    flex-grow: 0;
    min-height: 60px;
    max-height: 60px;
    overflow: hidden;
    padding: 10px 20px;
}

.chat-history.expanded {
    flex-grow: 1;
    min-height: 400px;
    max-height: calc(100vh - 200px);
}

.chat-panel .panel-toggle-btn.collapsed i {
    transform: rotate(180deg);
}
.chat-message {
    max-width: 75%;
    padding: 10px 15px;
    border-radius: 15px;
    font-size: 1.2em;
    line-height: 2;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
    word-wrap: break-word;
    word-break: break-word;
    overflow-wrap: break-word;
}
.chat-message.user {
    background-color: #8b5cf6; /* Light purple */
    color: #fff;
    border-bottom-left-radius: 0;
    align-self: flex-start;
}
.chat-message.ai {
    background-color: #f97316; /* Orange */
    color: #0a192f; /* Dark text */
    border-bottom-right-radius: 0;
    align-self: flex-end;
}
.chat-message p { margin: 0; }
.chat-message strong { font-weight: bold; } /* For simple results */

/* Markdown styling for AI messages */
.chat-message.ai h1, .chat-message.ai h2, .chat-message.ai h3, 
.chat-message.ai h4, .chat-message.ai h5, .chat-message.ai h6 {
    margin: 0.5em 0 0.3em 0;
    font-weight: bold;
    color: inherit;
}

.chat-message.ai h1 { font-size: 1.2em; }
.chat-message.ai h2 { font-size: 1.1em; }
.chat-message.ai h3 { font-size: 1.05em; }
.chat-message.ai h4, .chat-message.ai h5, .chat-message.ai h6 { font-size: 1em; }

.chat-message.ai ul, .chat-message.ai ol {
    margin: 0.5em 0;
    padding-left: 1.5em;
}

.chat-message.ai li {
    margin: 0.2em 0;
}

.chat-message.ai code {
    background-color: rgba(0, 0, 0, 0.1);
    padding: 0.1em 0.3em;
    border-radius: 3px;
    font-family: 'SF Mono', 'Monaco', 'Inconsolata', 'Roboto Mono', 'Source Code Pro', 'Menlo', 'Consolas', monospace;
    font-size: 0.9em;
    word-break: break-all;
}

.chat-message.ai pre {
    background-color: rgba(0, 0, 0, 0.1);
    padding: 0.8em;
    border-radius: 5px;
    overflow-x: auto;
    margin: 0.5em 0;
}

.chat-message.ai pre code {
    background: none;
    padding: 0;
}

.chat-message.ai blockquote {
    border-left: 3px solid rgba(0, 0, 0, 0.2);
    margin: 0.5em 0;
    padding-left: 1em;
    font-style: italic;
}

.chat-message.ai table {
    border-collapse: collapse;
    margin: 0.5em 0;
    width: 100%;
}

.chat-message.ai th, .chat-message.ai td {
    border: 1px solid rgba(0, 0, 0, 0.2);
    padding: 0.3em 0.6em;
    text-align: left;
}

.chat-message.ai th {
    background-color: rgba(0, 0, 0, 0.1);
    font-weight: bold;
}

.query-suggestions {
    padding: 5px 15px;
    border-top: 1px solid #4a5568;
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}
.query-suggestions button {
    background-color: #2d3748;
    color: #a0b3c6;
    border: 1px solid #4a5568;
    padding: 5px 10px;
    font-size: 0.8em;
    border-radius: 15px;
    cursor: pointer;
}
.query-suggestions button:hover { background-color: #4a5568; color: #e2e8f0;}

.chat-input-area {
    display: flex;
    padding: 15px;
    border-top: 1px solid #4a5568;
    background-color: #2d3748; /* Slightly lighter input area */
}
.chat-input-area textarea {
    flex-grow: 1;
    background: none;
    border: none;
    color: #e2e8f0;
    resize: none;
    height: 40px; /* Start height */
    max-height: 100px; /* Limit growth */
    padding: 10px;
    font-size: 1em;
    outline: none;
}
.chat-input-area textarea::placeholder { color: #a0b3c6; }

#send-chat-btn {
    background: none;
    border: none;
    color: #f97316; /* Orange send */
    font-size: 1.5em;
    padding: 5px 10px;
    cursor: pointer;
    margin-left: 10px;
}
#send-chat-btn:hover { color: #ea580c; }

/* --- Results Panel (Right) --- */
.results-panel {
    flex: 0 0 450px; /* Wider for better data visualization and table display */
    background-color: #2d3748; /* Matches left panel */
    display: flex;
    flex-direction: column;
    border-left: 1px solid #4a5568;
    transition: flex-basis 0.3s ease, min-width 0.3s ease;
    overflow: hidden;
    min-width: 400px; /* Ensure minimum width for usability */
    max-width: 600px; /* Prevent from becoming too wide */
}

.results-panel.collapsed {
    flex: 0 0 50px; /* Collapsed width - just enough for toggle button */
    min-width: 50px;
}

.results-panel.collapsed .results-content,
.results-panel.collapsed .performance-panel,
.results-panel.collapsed .results-placeholder {
    display: none;
}

.results-panel.collapsed .panel-header h3 {
    display: none;
}
.close-results-btn { /* Style like settings btn */
    background: none; border: none; color: #a0b3c6; cursor: pointer; padding: 5px;
}
.close-results-btn:hover { color: #e2e8f0; }

.results-content {
    flex-grow: 1;
    padding: 15px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 20px;
    color:#d8d8d8;
}

.result-section h4 {
    color: #d8d8d8; /* Subtler heading */
    font-size: 0.9em;
    margin-bottom: 8px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}
.result-section p { font-size: 0.9em; margin: 0 0 5px 0; }
.result-section pre {
    background-color: #1a2a40;
    color:#d8dadd;
    padding: 10px;
    border-radius: 4px;
    overflow-x: auto;
    font-size: 0.85em;
    max-height: 150px; /* Limit SQL height */
}
.result-section code { color: #e2e8f0; }

.result-section.collapsible h4 { 
    cursor: pointer;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
    font-weight: 600;
}
.result-section.collapsible h4 i { margin-left: 5px; transition: transform 0.2s ease; }
.result-section.collapsible.collapsed pre { display: none; }
.result-section.collapsible.collapsed h4 i { transform: rotate(-90deg); }

.table-container {
    max-height: 350px; /* Increased height for better data viewing */
    overflow: auto;
    border: 1px solid #4a5568;
    border-radius: 6px;
    color: #e2e8f0;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
    background-color: #1a2a40;
}
/* Add basic table styling */
.table-container table { width: 100%; border-collapse: collapse; }
.table-container th, .table-container td { padding: 8px; border: 1px solid #4a5568; text-align: left; font-size: 0.85em; }
.table-container th { background-color: #4a5568; }

.chart-container { /* Enhanced chart container for better visualization */
    position: relative;
    height: 300px; /* Increased height for better chart visibility */
    min-height: 250px;
    width: 100%;
    background-color: #1a2a40;
    border-radius: 6px;
    border: 1px solid #4a5568;
    overflow: hidden;
}

.result-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    margin-top: 10px;
    padding-top: 15px;
    border-top: 1px solid #4a5568;
}
.result-actions button { /* Style action buttons */
    background-color: #4a5568;
    color: #e2e8f0;
    padding: 8px 12px;
    font-size: 0.9em;
    /* ... other button styles */
}
.result-actions button:hover { background-color: #5a6578; }
.result-actions button i { margin-right: 5px; }

.results-placeholder {
    flex-grow: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    color: #a0b3c6;
    text-align: center;
    padding: 20px;
}

/* --- Utility --- */
.hidden { display: none !important; }
/* Primary chart containers for ECharts and D3 fallback */
#d3-chart-container,
#resultsChart {
    width: 100%;
    height: 100%; /* Fill the chart container */
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
}

#resultsChart {
    min-height: 280px;
}

#d3-chart-container svg {
    max-width: 100%;
    max-height: 100%;
    width: auto;
    height: auto;
}

.d3-tooltip {
    position: absolute;
    text-align: center;
    padding: 8px;
    font-size: 12px;
    background: rgba(0, 0, 0, 0.8);
    color: white;
    border-radius: 4px;
    pointer-events: none;
    z-index: 1000;
}

.bar:hover {
    fill: orange;
}

.axis text {
    font-size: 12px;
}

.axis path,
.axis line {
    fill: none;
    stroke: #000;
    shape-rendering: crispEdges;
}

/* --- Results Panel Adjustments --- */
.results-panel {
    /* Ensure flex direction allows stacking */
    flex-direction: column;
}
.results-content {
    /* Allow content to scroll independently */
    overflow-y: auto;
    flex-grow: 1; /* Takes up space until performance panel expands */
    padding-bottom: 10px; /* Space for the toggle button */
}

/* --- Toggle Button for Performance Panel --- */
.toggle-performance-btn {
    display: flex; /* Use flex to align icon and text */
    align-items: center;
    justify-content: center;
    width: calc(100% - 30px); /* Match padding */
    margin: 15px 15px 5px 15px; /* Add some margin */
    padding: 8px 15px;
    background-color: #4a5568; /* Consistent button color */
    color: #e2e8f0;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-size: 0.9em;
    transition: background-color 0.3s ease;
}
.toggle-performance-btn:hover {
    background-color: #5a6578;
}
.toggle-performance-btn span {
    flex-grow: 1; /* Push icon to the right */
    text-align: center;
}
.toggle-performance-btn i {
    transition: transform 0.3s ease;
}
.toggle-performance-btn.expanded i {
    transform: rotate(180deg); /* Point chevron up when expanded */
}


/* --- Performance Panel --- */
.performance-panel {
    background-color: #2d3748; /* Same as results panel */
    border-top: 2px solid #4a5568; /* Stronger separator */
    overflow: hidden; /* Key for collapse/expand */
    max-height: 0; /* Initially collapsed */
    transition: max-height 0.5s ease-out, padding 0.5s ease-out; /* Smooth transition */
    padding: 0 15px; /* No padding when collapsed */
    flex-shrink: 0; /* Prevent shrinking */
}
.performance-panel.expanded {
    max-height: 500px; /* Adjust max-height as needed for content */
    padding: 15px 15px; /* Add padding back when expanded */
    /* Consider adding overflow-y: auto if content might exceed max-height */
}

/* --- Performance Header --- */
.performance-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 15px;
    padding-bottom: 10px;
    border-bottom: 1px solid #4a5568;
    gap: 15px;
}
.integration-info {
    display: flex;
    flex-direction: column;
    font-size: 0.9em;
}
#perf-integration-name {
    font-weight: bold;
    color: #e2e8f0;
    font-size: 1.1em; /* Make name slightly larger */
    margin-bottom: 3px;
}
#perf-next-runtime {
    color: #a0b3c6;
}

.status-badge {
    padding: 3px 8px;
    border-radius: 12px; /* Pill shape */
    font-size: 0.8em;
    font-weight: bold;
    text-transform: uppercase;
}
.status-badge.active { background-color: #16a34a; color: #fff; } /* Green */
.status-badge.paused { background-color: #d97706; color: #fff; } /* Amber */
.status-badge.error { background-color: #dc2626; color: #fff; } /* Red */

.integration-controls button {
    background: none;
    border: 1px solid #4a5568;
    color: #a0b3c6;
    padding: 5px 8px;
    border-radius: 4px;
    cursor: pointer;
    margin-left: 5px;
}
.integration-controls button:hover {
    background-color: #4a5568;
    color: #e2e8f0;
}
.integration-controls button i { font-size: 0.9em; }

/* --- Performance Chart Area --- */
.performance-chart-area h4 {
    color: #a0b3c6;
    font-size: 0.9em;
    margin-bottom: 8px;
    text-transform: uppercase;
}
.performance-chart-container {
    height: 200px; /* Adjust height as needed */
    position: relative;
}

#performanceChart {
    width: 100%;
    height: 100%;
}

/* --- Settings & Usage Area --- */
.settings-usage-area {
    display: flex;
    justify-content: space-between;
    gap: 20px;
    margin-top: 15px;
    padding-top: 15px;
    border-top: 1px solid #4a5568;
}
.settings-section, .usage-section {
    flex: 1;
}
.settings-section h4, .usage-section h4 {
    color: #a0b3c6;
    font-size: 0.9em;
    margin-top: 0;
    margin-bottom: 10px;
    text-transform: uppercase;
}
.settings-section button {
    display: block;
    width: 100%;
    margin-bottom: 8px;
    background-color: #4a5568;
    color: #e2e8f0;
    padding: 8px 12px;
    font-size: 0.9em;
    border: none;
    border-radius: 4px;
    text-align: left;
    cursor: pointer;
}
.settings-section button:hover { background-color: #5a6578; }

.usage-section p {
    font-size: 0.9em;
    color: #a0b3c6;
    margin: 0;
}
.usage-section p span {
    font-weight: bold;
    font-size: 1.2em; /* Make usage number stand out */
    color: #e2e8f0;
}

/* Ensure results placeholder is handled correctly */
.results-placeholder {
    flex-grow: 1; /* Takes up space when results content is empty */
    display: flex;
    justify-content: center;
    align-items: center;
    color: #a0b3c6;
    text-align: center;
    padding: 20px;
}
.results-panel:has(.results-content:not(:empty)) .results-placeholder {
    display: none; /* Hide placeholder if results content has children */
}
.results-panel:has(#connection-status:not(.hidden)) .results-placeholder {
     display: none; /* Hide placeholder if status is shown */
}
.chat-message {
    opacity: 0;
    transform: translateY(20px);
    animation: chatFadeIn 0.4s cubic-bezier(.4,0,.2,1) forwards;
}
@keyframes chatFadeIn {
    to {
        opacity: 1;
        transform: none;
    }
}
.toast {
    position: fixed;
    bottom: 30px;
    left: 50%;
    transform: translateX(-50%);
    background: #f97316;
    color: #fff;
    padding: 16px 32px;
    border-radius: 8px;
    font-size: 1.1em;
    box-shadow: 0 4px 16px rgba(0,0,0,0.2);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.4s;
    z-index: 9999;
}
.toast.show {
    opacity: 1;
    pointer-events: auto;
}
.button-group button:active,
.result-actions button:active,
.add-connection-btn:active {
    transform: scale(0.97);
    box-shadow: 0 2px 8px rgba(0,0,0,0.12);
}
body.light-mode {
    background: #7c94a4;
    color: #222;
}
body.light-mode .chat-page-container,
body.light-mode .results-panel,
body.light-mode .chat-input-area,
body.light-mode .chat-input textarea,
body.light-mode .results-section,
body.light-mode .connections-panel {
    background: #d2dfe8;
    color: #222;
}
body.light-mode .chat-message.ai {
    color: white; /* color: #fbbf24; */
    background: #222;
}
body.light-mode .chat-panel{
    background: #fff;
    color: #222;
}
body.light-mode .connection-name {
    color: #222;
}
body.light-mode .results-content,
body.light-mode .results-section,
body.light-mode .results-section h4,
body.light-mode .results-panel{
    color:#222;
}
body.light-mode .table-container{
    color:#222;
}
/* Enhanced responsive design for better UI fitting */
@media (max-width: 1400px) {
    .chat-page-container {
        max-width: 100vw;
        margin: 10px;
        height: calc(100vh - 20px);
    }
    .results-panel {
        flex: 0 0 400px; /* Slightly smaller on medium screens */
        min-width: 350px;
    }
    .connections-panel {
        flex: 0 0 240px;
        min-width: 240px;
    }
}

@media (max-width: 1200px) {
    .chat-page-container {
        margin: 5px;
        height: calc(100vh - 10px);
    }
    .results-panel {
        flex: 0 0 350px;
        min-width: 320px;
    }
    .connections-panel {
        flex: 0 0 220px;
        min-width: 220px;
    }
    .chart-container {
        height: 250px;
        min-height: 200px;
    }
    .table-container {
        max-height: 300px;
    }
}

@media (max-width: 1000px) {
    .chat-page-container {
        flex-direction: column;
        max-width: 100vw;
        height: auto;
        min-height: 100vh;
    }
    .connections-panel {
        flex: 0 0 200px;
        max-width: 100%;
        border-right: none;
        border-bottom: 1px solid #4a5568;
        min-width: 0;
    }
    .results-panel {
        flex: 0 0 400px;
        max-width: 100vw;
        border-left: none;
        border-top: 1px solid #4a5568;
        min-width: 0;
        order: 3; /* Move results panel to bottom */
    }
    .chat-panel {
        min-width: 0;
        flex: 1;
        order: 2; /* Keep chat in middle */
    }
    .connections-panel {
        order: 1; /* Keep connections at top */
    }
}

@media (max-width: 768px) {
    .chat-page-container {
        margin: 0;
        border-radius: 0;
        height: 100vh;
    }
    .connections-panel {
        flex: 0 0 150px;
    }
    .results-panel {
        flex: 0 0 350px;
    }
    .chart-container {
        height: 200px;
        min-height: 180px;
    }
    .table-container {
        max-height: 250px;
        font-size: 0.85em;
    }
    .result-section {
        padding: 10px;
    }
    .results-content {
        padding: 10px;
    }
}

@media (max-width: 600px) {
    .connections-panel {
        flex: 0 0 120px;
    }
    .results-panel {
        flex: 0 0 300px;
    }
    .panel-header {
        padding: 10px;
    }
    .panel-header h3 {
        font-size: 1em;
    }
    .chart-container {
        height: 180px;
        min-height: 150px;
    }
    .table-container {
        max-height: 200px;
        font-size: 0.8em;
    }
    .table-container th, .table-container td {
        padding: 6px;
    }
}
.button-group button:active,
.result-actions button:active,
.add-connection-btn:active {
    transform: scale(0.97);
    box-shadow: 0 2px 8px rgba(0,0,0,0.12);
}
input:focus, textarea:focus, select:focus {
    outline: 2px solid #f97316;
    outline-offset: 2px;
}
#expand-chart-btn {
    position: absolute; top: 10px; right: 10px;
    background: #2d3748; color: #f97316; border: none; border-radius: 4px; cursor: pointer;
    z-index: 2;
    padding: 8px;
    transition: background-color 0.2s ease;
}

#expand-chart-btn:hover {
    background: #4a5568;
    color: #ea580c;
}
.modal {
    position: fixed; top: 0; left: 0; width: 100vw; height: 100vh;
    background: rgba(0,0,0,0.7); display: flex; align-items: center; justify-content: center;
    z-index: 1000;
}
.modal-content {
    background: #1a2a40; padding: 24px; border-radius: 8px; position: relative;
    min-width: 600px; min-height: 500px;
    max-width: 90vw;
    max-height: 90vh;
    overflow: auto;
}

#modal-d3-chart-container {
    width: 100%;
    height: 400px;
    min-height: 300px;
}
#close-chart-modal {
    position: absolute; top: 10px; right: 10px; background: none; border: none; color: #f97316; font-size: 1.5em;
}
.result-section.collapsible h4 {
    cursor: pointer;
    user-select: none;
    transition: color 0.2s;
}
.result-section.collapsible h4:hover {
    color: #f97316;
}
.table-container th {
    cursor: pointer;
    user-select: none;
}
.table-container th.sorted-asc::after { content: " ▲"; }
.table-container th.sorted-desc::after { content: " ▼"; }
.chat-message.loading p {
    display: flex; gap: 4px;
}
.dot {
    width: 8px; height: 8px; border-radius: 50%; background: #222; animation: bounce 1s infinite alternate;
}
body.light-mode .dot {
    background:#f97316;
}
.dot:nth-child(2) { animation-delay: 0.2s; }
.dot:nth-child(3) { animation-delay: 0.4s; }
@keyframes bounce { to { transform: translateY(-8px); opacity: 0.5; } }

/* Streaming message styles */
.chat-message.ai.streaming::after {
    content: "";
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #222;
    margin-left: 4px;
    animation: blink 1s infinite;
}

body.light-mode .chat-message.ai.streaming::after {
    background: #f97316;
}

@keyframes blink {
    0%, 50% { opacity: 1; }
    51%, 100% { opacity: 0; }
}
/* Theme variables */
:root {
  --bg: #0f172a;              /* slate-900 */
  --panel: #111827;           /* gray-900 */
  --panel-2: #0b1220;         /* deep panel */
  --text: #e5e7eb;            /* gray-200 */
  --muted: #9ca3af;           /* gray-400 */
  --border: #1f2937;          /* gray-800 */
  --accent: #22d3ee;          /* cyan-400 */
  --accent-strong: #06b6d4;   /* cyan-500 */
  --danger: #ef4444;
  --success: #22c55e;
  --shadow: rgba(0, 0, 0, 0.3);
}

/* Optional light theme (will be toggled by JS) */
body[data-theme="light"] {
  --bg: #f8fafc;              /* slate-50 */
  --panel: #ffffff;
  --panel-2: #f1f5f9;
  --text: #0f172a;
  --muted: #475569;
  --border: #e2e8f0;
  --accent: #2563eb;          /* blue-600 */
  --accent-strong: #1d4ed8;   /* blue-700 */
  --shadow: rgba(0,0,0,0.08);
}

/* High-level surfaces */
.chat-page-container { background: var(--bg); color: var(--text); }
.results-panel, .chat-panel, .connections-panel {
  background: var(--panel);
  border-left: 1px solid var(--border);
}

/* Modal Base Styles */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.6);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1000;
    backdrop-filter: blur(4px);
}

.modal.hidden {
    display: none;
}

.modal-content {
    background-color: #2d3748;
    border-radius: 12px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.4);
    animation: modalSlideIn 0.3s ease-out;
    position: relative;
    margin: 20px;
}

@keyframes modalSlideIn {
    from {
        opacity: 0;
        transform: translateY(-30px) scale(0.95);
    }
    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

/* Connection Settings Modal */
.connection-settings-content {
    max-width: 600px;
    width: 90vw;
    max-height: 80vh;
    overflow-y: auto;
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px 24px 16px;
    border-bottom: 1px solid #4a5568;
}

.modal-header h3 {
    margin: 0;
    color: #e2e8f0;
    font-size: 1.25rem;
    font-weight: 600;
}

.close-modal-btn {
    background: transparent;
    border: none;
    color: #a0b3c6;
    cursor: pointer;
    padding: 8px;
    border-radius: 4px;
    font-size: 1.1rem;
    transition: all 0.2s ease;
}

.close-modal-btn:hover {
    background-color: #4a5568;
    color: #e2e8f0;
}

.modal-body {
    padding: 24px;
}

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

.form-group label {
    display: block;
    margin-bottom: 6px;
    color: #e2e8f0;
    font-weight: 500;
    font-size: 0.9rem;
}

.form-group input,
.form-group textarea,
.form-group select {
    width: 100%;
    padding: 10px 12px;
    background-color: #2d3748;
    border: 1px solid #4a5568;
    border-radius: 6px;
    color: #e2e8f0;
    font-size: 0.9rem;
    transition: all 0.2s ease;
    box-sizing: border-box;
}

.form-group input:focus,
.form-group textarea:focus,
.form-group select:focus {
    outline: none;
    border-color: #3b82f6;
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.form-group input:disabled,
.form-group select:disabled {
    background-color: #1a202c;
    color: #a0b3c6;
    cursor: not-allowed;
}

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

.form-actions {
    display: flex;
    gap: 12px;
    justify-content: flex-end;
    margin-top: 32px;
    padding-top: 20px;
    border-top: 1px solid #4a5568;
}

.btn {
    padding: 10px 16px;
    border: none;
    border-radius: 6px;
    font-size: 0.9rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
    display: inline-flex;
    align-items: center;
    gap: 8px;
}

.btn-primary {
    background-color: #f08e4b;
    color: white;
}

.btn-primary:hover {
    background-color: #2563eb;
}

.btn-secondary {
    background-color: #2f384d;
    color: #e2e8f0;
}

.btn-secondary:hover {
    background-color: #5a6578;
}

.connection-field-group {
    background-color: #1a202c;
    padding: 16px;
    border-radius: 6px;
    margin-bottom: 20px;
    border: 1px solid #4a5568;
}

.connection-field-group h4 {
    margin: 0 0 16px 0;
    color: #e2e8f0;
    font-size: 1rem;
    font-weight: 600;
}

.field-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
    margin-bottom: 16px;
}

.field-row:last-child {
    margin-bottom: 0;
}

@media (max-width: 600px) {
    .field-row {
        grid-template-columns: 1fr;
        gap: 12px;
    }
    
    .connection-settings-content {
        width: 95vw;
        margin: 20px auto;
    }
    
    .form-actions {
        flex-direction: column;
    }
    
    .btn {
        width: 100%;
        justify-content: center;
    }
}

/* Light mode support for connection settings */
body.light-mode .modal-header {
    border-bottom: 1px solid #e2e8f0;
}

body.light-mode .modal-header h3,
body.light-mode .form-group label {
    color: #1a202c;
}

body.light-mode .close-modal-btn {
    color: #4a5568;
}

body.light-mode .close-modal-btn:hover {
    background-color: #111827;
    color: #1a202c;
}

body.light-mode .form-group input,
body.light-mode .form-group textarea,
body.light-mode .form-group select {
    background-color: white;
    border: 1px solid #d1d5db;
    color: #1a202c;
}

body.light-mode .form-group input:disabled,
body.light-mode .form-group select:disabled {
    background-color: #f3f4f6;
    color: #6b7280;
}

body.light-mode .connection-field-group {
    background-color: #f9fafb;
    border: 1px solid #e5e7eb;
}

body.light-mode .connection-field-group h4 {
    color: #1a202c;
}

body.light-mode .form-actions {
    border-top: 1px solid #e2e8f0;
}

body.light-mode .modal-content {
    background-color: white;
}

/* Section cards */
.result-section, .result-actions {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 6px 18px var(--shadow);
}

/* Headings and spacing */
.result-section h4, .results-panel .panel-header h3, .chat-panel .panel-header h3 {
  color: var(--text);
  letter-spacing: 0.2px;
}
.result-section { transition: box-shadow 180ms ease, transform 180ms ease; }
.result-section:hover { box-shadow: 0 10px 24px var(--shadow); transform: translateY(-1px); }

/* Collapsible affordance */
#generated-sql h4 { 
    display: flex; 
    align-items: center; 
    justify-content: space-between; 
    gap: 8px; 
    cursor: pointer;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
    font-weight: 600;
}
#generated-sql h4 .icon-btn {
  border: none; background: transparent; color: var(--muted); cursor: pointer;
  padding: 4px 6px; border-radius: 6px; transition: background 140ms ease, color 140ms ease, transform 80ms ease;
}
#generated-sql h4 .icon-btn:hover, #generated-sql h4 .icon-btn:focus {
  color: var(--text); background: var(--border); outline: 2px solid transparent;
}
#generated-sql h4 .icon-btn:active { transform: scale(0.96); }

/* Code block styling */
#generated-sql pre {
  background: #0b1020;        /* deep navy for contrast */
  color: #e6edf3;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px 14px;
  overflow: auto;
  font-size: 0.9rem;
  line-height: 1.5;
}
body[data-theme="light"] #generated-sql pre {
  background: #f6f8fa;
  color: #111827;
}

/* Buttons & focus */
button, .icon-btn {
  transition: background 150ms ease, color 150ms ease, box-shadow 150ms ease, transform 80ms ease;
}
button:focus-visible, .icon-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* Collapsible transition (height) if you want smooth open/close) */
.result-section.collapsible pre {
  transition: max-height 200ms ease;
}
.result-section.collapsible.collapsed pre {
  max-height: 0; padding-top: 0; padding-bottom: 0; border-width: 0; overflow: hidden;
}

/* Toast improvements */
.toast {
  background: rgba(17, 24, 39, 0.98);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  box-shadow: 0 8px 18px var(--shadow);
}
body[data-theme="light"] .toast { background: rgba(255, 255, 255, 0.98); color: #111827; }

/* Subtle accent elements */
a, .accent { color: var(--accent); }
a:hover, .accent:hover { color: var(--accent-strong); }

/* Ensure charts and tables don’t overflow weirdly */
.chart-container, .table-container { border-radius: 8px; }

/* Login modal specific adjustments (reuse base .modal styles) */
#login-modal .modal-content {
  max-width: 480px;
}
:root {
  --muted-text: #a0b3c6;
}
.modal-content {
    background: #1a2a40; padding: 24px; border-radius: 8px; position: relative;
    min-width: 600px; min-height: 500px;
    max-width: 90vw;
    max-height: 90vh;
    overflow: auto;
}

#modal-d3-chart-container {
    width: 100%;
    height: 400px;
    min-height: 300px;
}
#close-chart-modal {
    position: absolute; top: 10px; right: 10px; background: none; border: none; color: #f97316; font-size: 1.5em;
}
.result-section.collapsible h4 {
    cursor: pointer;
    user-select: none;
    transition: color 0.2s;
}
.result-section.collapsible h4:hover {
    color: #f97316;
}
.table-container th {
    cursor: pointer;
    user-select: none;
}
.table-container th.sorted-asc::after { content: " ▲"; }
.table-container th.sorted-desc::after { content: " ▼"; }
.chat-message.loading p {
    display: flex; gap: 4px;
}
.dot {
    width: 8px; height: 8px; border-radius: 50%; background: #222; animation: bounce 1s infinite alternate;
}
body.light-mode .dot {
    background:#f97316;
}
.dot:nth-child(2) { animation-delay: 0.2s; }
.dot:nth-child(3) { animation-delay: 0.4s; }
@keyframes bounce { to { transform: translateY(-8px); opacity: 0.5; } }

/* Streaming message styles */
.chat-message.ai.streaming::after {
    content: "";
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #222;
    margin-left: 4px;
    animation: blink 1s infinite;
}

body.light-mode .chat-message.ai.streaming::after {
    background: #f97316;
}

@keyframes blink {
    0%, 50% { opacity: 1; }
    51%, 100% { opacity: 0; }
}
/* Theme variables */
:root {
  --bg: #0f172a;              /* slate-900 */
  --panel: #111827;           /* gray-900 */
  --panel-2: #0b1220;         /* deep panel */
  --text: #e5e7eb;            /* gray-200 */
  --muted: #9ca3af;           /* gray-400 */
  --border: #1f2937;          /* gray-800 */
  --accent: #22d3ee;          /* cyan-400 */
  --accent-strong: #06b6d4;   /* cyan-500 */
  --danger: #ef4444;
  --success: #22c55e;
  --shadow: rgba(0, 0, 0, 0.3);
}

/* Optional light theme (will be toggled by JS) */
body[data-theme="light"] {
  --bg: #f8fafc;              /* slate-50 */
  --panel: #ffffff;
  --panel-2: #f1f5f9;
  --text: #0f172a;
  --muted: #475569;
  --border: #e2e8f0;
  --accent: #2563eb;          /* blue-600 */
  --accent-strong: #1d4ed8;   /* blue-700 */
  --shadow: rgba(0,0,0,0.08);
}

/* High-level surfaces */
.chat-page-container { background: var(--bg); color: var(--text); }
.results-panel, .chat-panel, .connections-panel {
  background: var(--panel);
  border-left: 1px solid var(--border);
}

/* Modal Base Styles */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.6);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1000;
    backdrop-filter: blur(4px);
}

.modal.hidden {
    display: none;
}

.modal-content {
    background-color: #2d3748;
    border-radius: 12px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.4);
    animation: modalSlideIn 0.3s ease-out;
    position: relative;
    margin: 20px;
}

@keyframes modalSlideIn {
    from {
        opacity: 0;
        transform: translateY(-30px) scale(0.95);
    }
    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

/* Connection Settings Modal */
.connection-settings-content {
    max-width: 600px;
    width: 90vw;
    max-height: 80vh;
    overflow-y: auto;
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px 24px 16px;
    border-bottom: 1px solid #4a5568;
}

.modal-header h3 {
    margin: 0;
    color: #e2e8f0;
    font-size: 1.25rem;
    font-weight: 600;
}

.close-modal-btn {
    background: transparent;
    border: none;
    color: #a0b3c6;
    cursor: pointer;
    padding: 8px;
    border-radius: 4px;
    font-size: 1.1rem;
    transition: all 0.2s ease;
}

.close-modal-btn:hover {
    background-color: #4a5568;
    color: #e2e8f0;
}

.modal-body {
    padding: 24px;
}

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

.form-group label {
    display: block;
    margin-bottom: 6px;
    color: #e2e8f0;
    font-weight: 500;
    font-size: 0.9rem;
}

.form-group input,
.form-group textarea,
.form-group select {
    width: 100%;
    padding: 10px 12px;
    background-color: #2d3748;
    border: 1px solid #4a5568;
    border-radius: 6px;
    color: #e2e8f0;
    font-size: 0.9rem;
    transition: all 0.2s ease;
    box-sizing: border-box;
}

.form-group input:focus,
.form-group textarea:focus,
.form-group select:focus {
    outline: none;
    border-color: #3b82f6;
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.form-group input:disabled,
.form-group select:disabled {
    background-color: #1a202c;
    color: #a0b3c6;
    cursor: not-allowed;
}

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

.form-actions {
    display: flex;
    gap: 12px;
    justify-content: flex-end;
    margin-top: 32px;
    padding-top: 20px;
    border-top: 1px solid #4a5568;
}

.btn {
    padding: 10px 16px;
    border: none;
    border-radius: 6px;
    font-size: 0.9rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
    display: inline-flex;
    align-items: center;
    gap: 8px;
}

.btn-primary {
    background-color: #3b82f6;
    color: white;
}

.btn-primary:hover {
    background-color: #2563eb;
}

.btn-secondary {
    background-color: #2f384d;
    color: #e2e8f0;
}

.btn-secondary:hover {
    background-color: #5a6578;
}

.connection-field-group {
    background-color: #1a202c;
    padding: 16px;
    border-radius: 6px;
    margin-bottom: 20px;
    border: 1px solid #4a5568;
}

.connection-field-group h4 {
    margin: 0 0 16px 0;
    color: #e2e8f0;
    font-size: 1rem;
    font-weight: 600;
}

.field-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
    margin-bottom: 16px;
}

.field-row:last-child {
    margin-bottom: 0;
}

@media (max-width: 600px) {
    .field-row {
        grid-template-columns: 1fr;
        gap: 12px;
    }
    
    .connection-settings-content {
        width: 95vw;
        margin: 20px auto;
    }
    
    .form-actions {
        flex-direction: column;
    }
    
    .btn {
        width: 100%;
        justify-content: center;
    }
}

/* Light mode support for connection settings */
body.light-mode .modal-header {
    border-bottom: 1px solid #e2e8f0;
}

body.light-mode .modal-header h3,
body.light-mode .form-group label {
    color: #1a202c;
}

body.light-mode .close-modal-btn {
    color: #4a5568;
}

body.light-mode .close-modal-btn:hover {
    background-color: #111827;
    color: #1a202c;
}

body.light-mode .form-group input,
body.light-mode .form-group textarea,
body.light-mode .form-group select {
    background-color: white;
    border: 1px solid #d1d5db;
    color: #1a202c;
}

body.light-mode .form-group input:disabled,
body.light-mode .form-group select:disabled {
    background-color: #f3f4f6;
    color: #6b7280;
}

body.light-mode .connection-field-group {
    background-color: #f9fafb;
    border: 1px solid #e5e7eb;
}

body.light-mode .connection-field-group h4 {
    color: #1a202c;
}

body.light-mode .form-actions {
    border-top: 1px solid #e2e8f0;
}

body.light-mode .modal-content {
    background-color: white;
}

/* Section cards */
.result-section, .result-actions {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 6px 18px var(--shadow);
}

/* Headings and spacing */
.result-section h4, .results-panel .panel-header h3, .chat-panel .panel-header h3 {
  color: var(--text);
  letter-spacing: 0.2px;
}
.result-section { transition: box-shadow 180ms ease, transform 180ms ease; }
.result-section:hover { box-shadow: 0 10px 24px var(--shadow); transform: translateY(-1px); }

/* Collapsible affordance */
#generated-sql h4 { 
    display: flex; 
    align-items: center; 
    justify-content: space-between; 
    gap: 8px; 
    cursor: pointer;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
    font-weight: 600;
}
#generated-sql h4 .icon-btn {
  border: none; background: transparent; color: var(--muted); cursor: pointer;
  padding: 4px 6px; border-radius: 6px; transition: background 140ms ease, color 140ms ease, transform 80ms ease;
}
#generated-sql h4 .icon-btn:hover, #generated-sql h4 .icon-btn:focus {
  color: var(--text); background: var(--border); outline: 2px solid transparent;
}
#generated-sql h4 .icon-btn:active { transform: scale(0.96); }

/* Code block styling */
#generated-sql pre {
  background: #0b1020;        /* deep navy for contrast */
  color: #e6edf3;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px 14px;
  overflow: auto;
  font-size: 0.9rem;
  line-height: 1.5;
}
body[data-theme="light"] #generated-sql pre {
  background: #f6f8fa;
  color: #111827;
}

/* Buttons & focus */
button, .icon-btn {
  transition: background 150ms ease, color 150ms ease, box-shadow 150ms ease, transform 80ms ease;
}
button:focus-visible, .icon-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* Collapsible transition (height) if you want smooth open/close) */
.result-section.collapsible pre {
  transition: max-height 200ms ease;
}
.result-section.collapsible.collapsed pre {
  max-height: 0; padding-top: 0; padding-bottom: 0; border-width: 0; overflow: hidden;
}

/* Toast improvements */
.toast {
  background: rgba(17, 24, 39, 0.98);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  box-shadow: 0 8px 18px var(--shadow);
}
body[data-theme="light"] .toast { background: rgba(255, 255, 255, 0.98); color: #111827; }

/* Subtle accent elements */
a, .accent { color: var(--accent); }
a:hover, .accent:hover { color: var(--accent-strong); }

/* Ensure charts and tables don’t overflow weirdly */
.chart-container, .table-container { border-radius: 8px; }

.result-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    margin-top: 10px;
    padding-top: 15px;
    border-top: 1px solid #4a5568;
}
.result-actions button { /* Style action buttons */
    background-color: #4a5568;
    color: #e2e8f0;
    padding: 8px 12px;
    font-size: 0.9em;
    /* ... other button styles */
}
.result-actions button:hover { background-color: #5a6578; }
.result-actions button i { margin-right: 5px; }

.results-placeholder {
    flex-grow: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    color: #a0b3c6;
    text-align: center;
    padding: 20px;
}

/* --- Utility --- */
.hidden { display: none !important; }
/* Enhanced D3 Chart Styles for better responsiveness */
#d3-chart-container {
    width: 100%;
    height: 100%; /* Fill the chart container */
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
}

#d3-chart-container svg {
    max-width: 100%;
    max-height: 100%;
    width: auto;
    height: auto;
}

.d3-tooltip {
    position: absolute;
    text-align: center;
    padding: 8px;
    font-size: 12px;
    background: rgba(0, 0, 0, 0.8);
    color: white;
    border-radius: 4px;
    pointer-events: none;
    z-index: 1000;
}

.bar:hover {
    fill: orange;
}

.axis text {
    font-size: 12px;
}

.axis path,
.axis line {
    fill: none;
    stroke: #000;
    shape-rendering: crispEdges;
}

/* --- Results Panel Adjustments --- */
.results-panel {
    /* Ensure flex direction allows stacking */
    flex-direction: column;
}
.results-content {
    /* Allow content to scroll independently */
    overflow-y: auto;
    flex-grow: 1; /* Takes up space until performance panel expands */
    padding-bottom: 10px; /* Space for the toggle button */
}

/* --- Toggle Button for Performance Panel --- */
.toggle-performance-btn {
    display: flex; /* Use flex to align icon and text */
    align-items: center;
    justify-content: center;
    width: calc(100% - 30px); /* Match padding */
    margin: 15px 15px 5px 15px; /* Add some margin */
    padding: 8px 15px;
    background-color: #4a5568; /* Consistent button color */
    color: #e2e8f0;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-size: 0.9em;
    transition: background-color 0.3s ease;
}
.toggle-performance-btn:hover {
    background-color: #5a6578;
}
.toggle-performance-btn span {
    flex-grow: 1; /* Push icon to the right */
    text-align: center;
}
.toggle-performance-btn i {
    transition: transform 0.3s ease;
}
.toggle-performance-btn.expanded i {
    transform: rotate(180deg); /* Point chevron up when expanded */
}


/* --- Performance Panel --- */
.performance-panel {
    background-color: #2d3748; /* Same as results panel */
    border-top: 2px solid #4a5568; /* Stronger separator */
    overflow: hidden; /* Key for collapse/expand */
    max-height: 0; /* Initially collapsed */
    transition: max-height 0.5s ease-out, padding 0.5s ease-out; /* Smooth transition */
    padding: 0 15px; /* No padding when collapsed */
    flex-shrink: 0; /* Prevent shrinking */
}
.performance-panel.expanded {
    max-height: 500px; /* Adjust max-height as needed for content */
    padding: 15px 15px; /* Add padding back when expanded */
    /* Consider adding overflow-y: auto if content might exceed max-height */
}

/* --- Performance Header --- */
.performance-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 15px;
    padding-bottom: 10px;
    border-bottom: 1px solid #4a5568;
    gap: 15px;
}
.integration-info {
    display: flex;
    flex-direction: column;
    font-size: 0.9em;
}
#perf-integration-name {
    font-weight: bold;
    color: #e2e8f0;
    font-size: 1.1em; /* Make name slightly larger */
    margin-bottom: 3px;
}
#perf-next
-runtime {
    color: #a0b3c6;
    font-size: 0.85em;
}

/* Status badges for performance panel */
.status-badge {
    padding: 2px 8px;
    border-radius: 12px;
    font-size: 0.75em;
    font-weight: bold;
    text-transform: uppercase;
}
.status-badge.active {
    background-color: #10b981;
    color: #fff;
}
.status-badge.inactive {
    background-color: #6b7280;
    color: #fff;
}
.status-badge.error {
    background-color: #ef4444;
    color: #fff;
}

/* --- Modal Tabs Styles --- */
.modal-tabs {
    display: flex;
    border-bottom: 1px solid #4a5568;
    margin-bottom: 20px;
    gap: 2px;
}

.tab-btn {
    flex: 1;
    padding: 12px 20px;
    background: transparent;
    border: none;
    color: #a0b3c6;
    cursor: pointer;
    font-size: 0.9em;
    font-weight: 500;
    transition: all 0.3s ease;
    border-bottom: 2px solid transparent;
    position: relative;
}

.tab-btn:hover:not(:disabled) {
    background-color: #374151;
    color: #e2e8f0;
}

.tab-btn.active {
    color: #3b82f6;
    border-bottom-color: #3b82f6;
    background-color: #1f2937;
}

.tab-btn:disabled {
    color: #6b7280;
    cursor: not-allowed;
    opacity: 0.5;
}

.tab-content {
    display: none;
}

.tab-content.active {
    display: block;
}

/* --- Schema Analysis Styles --- */
.schema-loading {
    text-align: center;
    padding: 40px 20px;
}

.loading-animation {
    display: flex;
    justify-content: center;
    margin-bottom: 20px;
}

.loading-animation svg {
    animation: spin 2s linear infinite;
}

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

#schema-loading-text {
    color: #a0b3c6;
    font-size: 1.1em;
    margin: 0;
}

.schema-results {
    padding: 20px 0;
}

.schema-summary {
    margin-bottom: 25px;
    padding: 15px;
    background-color: #374151;
    border-radius: 8px;
    border-left: 4px solid #3b82f6;
}

.schema-summary h4 {
    color: #e2e8f0;
    margin: 0 0 8px 0;
    font-size: 1.2em;
}

.schema-summary p {
    color: #a0b3c6;
    margin: 0;
}

#table-count {
    color: #3b82f6;
    font-weight: bold;
}

.schema-tables {
    margin-bottom: 25px;
}

.schema-table {
    background-color: #374151;
    border-radius: 8px;
    margin-bottom: 15px;
    overflow: hidden;
    border: 1px solid #4a5568;
}

.schema-table-header {
    padding: 12px 15px;
    background-color: #4a5568;
    border-bottom: 1px solid #6b7280;
    cursor: pointer;
    display: flex;
    justify-content: space-between;
    align-items: center;
    transition: background-color 0.3s ease;
}

.schema-table-header:hover {
    background-color: #5a6578;
}

.schema-table-name {
    font-weight: bold;
    color: #e2e8f0;
}

.schema-table-count {
    color: #a0b3c6;
    font-size: 0.9em;
}

.schema-table-content {
    padding: 15px;
    display: none;
}

.schema-table-content.expanded {
    display: block;
}

.schema-columns {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 10px;
}

.schema-column {
    padding: 8px 12px;
    background-color: #2d3748;
    border-radius: 4px;
    border-left: 3px solid #3b82f6;
}

.schema-column-name {
    font-weight: bold;
    color: #e2e8f0;
    font-size: 0.9em;
}

.schema-column-type {
    color: #a0b3c6;
    font-size: 0.8em;
    margin-top: 2px;
}

.schema-actions {
    display: flex;
    justify-content: space-between;
    gap: 15px;
    padding-top: 20px;
    border-top: 1px solid #4a5568;
}

.schema-error {
    text-align: center;
    padding: 40px 20px;
    color: #ef4444;
}

.error-icon {
    font-size: 3em;
    margin-bottom: 15px;
}

.schema-error p {
    margin: 0 0 20px 0;
    font-size: 1.1em;
}

/* --- Responsive Modal Tabs --- */
@media (max-width: 768px) {
    .modal-tabs {
        flex-direction: column;
    }
    
    .tab-btn {
        border-bottom: 1px solid #4a5568;
        border-radius: 0;
    }
    
    .tab-btn.active {
        border-bottom-color: #3b82f6;
        border-left: 3px solid #3b82f6;
    }
    
    .schema-columns {
        grid-template-columns: 1fr;
    }
    
    .schema-actions {
        flex-direction: column;
    }
}

/* --- Connection Status Styles --- */
.status-dot.pending {
    background-color: #f59e0b;
    animation: pulse 2s infinite;
}

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

.connection-status {
    font-size: 0.75em;
    color: #f59e0b;
    font-style: italic;
    margin-left: 5px;
    word-wrap: break-word;
    line-height: 1.3;
}

/* --- Connection Item Disabled State --- */
.connection-item input[type="checkbox"]:disabled + label {
    cursor: not-allowed;
}

.connection-item input[type="checkbox"]:disabled + label .connection-name {
    color: #6b7280;
}

/* --- Connection Name Responsive Fixes --- */
@media (max-width: 1200px) {
    .connections-panel {
        min-width: 200px;
    }
    
    .connection-name {
        font-size: 0.9em;
    }
    
    .connection-status {
        font-size: 0.7em;
    }
}

@media (max-width: 768px) {
    .connection-item {
        padding: 10px 12px;
    }
    
    .connection-name {
        font-size: 0.85em;
        line-height: 1.3;
    }
    
    .settings-btn {
        padding: 4px;
        margin-left: 6px;
    }
}

/* Ensure connection text container uses available space */
.connection-item label {
    flex-direction: row;
    align-items: flex-start;
    gap: 0;
}

.connection-item .connection-name {
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: flex-start;
}

/* Save Query Modal Styles */
#save-query-modal .modal-content {
    max-width: 600px;
}

#save-query-modal .form-group {
    margin-bottom: 16px;
}

#save-query-modal .form-group label {
    display: block;
    margin-bottom: 6px;
    font-weight: 500;
    color: #e2e8f0;
}

#save-query-modal .form-group input,
#save-query-modal .form-group textarea,
#save-query-modal .form-group select {
    width: 100%;
    padding: 10px;
    border: 1px solid #4a5568;
    border-radius: 6px;
    background: #2d3748;
    color: #e2e8f0;
    font-size: 14px;
    box-sizing: border-box;
}

#save-query-modal .form-group input:focus,
#save-query-modal .form-group textarea:focus,
#save-query-modal .form-group select:focus {
    outline: none;
    border-color: #3b82f6;
    box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.1);
}

#save-query-modal .form-group small {
    display: block;
    margin-top: 4px;
    font-size: 12px;
    color: #a0b3c6;
}

#save-query-modal .form-actions {
    display: flex;
    gap: 12px;
    justify-content: flex-end;
    margin-top: 24px;
    padding-top: 16px;
    border-top: 1px solid #4a5568;
}

#save-query-modal .btn {
    padding: 10px 20px;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    font-size: 14px;
    font-weight: 500;
    display: flex;
    align-items: center;
    gap: 6px;
    transition: all 0.2s ease;
}

#save-query-modal .btn-primary {
    background: #3b82f6;
    color: white;
}

#save-query-modal .btn-primary:hover {
    background: #2563eb;
}

#save-query-modal .btn-secondary {
    background: #2f384d;
    color: #e2e8f0;
    border: 1px solid #4a5568;
}

#save-query-modal .btn-secondary:hover {
    background: #5a6578;
}

/* Checkbox styling in save query modal */
#save-query-modal input[type="checkbox"] {
    width: auto;
    margin-right: 8px;
}

#save-query-modal label[style*="flex"] {
    display: flex !important;
    align-items: center;
    gap: 8px;
    margin-bottom: 8px;
}

/* Light mode support for save query modal */
body.light-mode #save-query-modal .form-group label {
    color: #1a202c;
}

body.light-mode #save-query-modal .form-group input,
body.light-mode #save-query-modal .form-group textarea,
body.light-mode #save-query-modal .form-group select {
    background-color: white;
    border: 1px solid #d1d5db;
    color: #1a202c;
}

body.light-mode #save-query-modal .form-group small {
    color: #4a5568;
}

body.light-mode #save-query-modal .form-actions {
    border-top: 1px solid #e2e8f0;
}

body.light-mode #save-query-modal .btn-secondary {
    background: #f3f4f6;
    color: #1a202c;
    border: 1px solid #d1d5db;
}

body.light-mode #save-query-modal .btn-secondary:hover {
    background: #e5e7eb;
}

/* --------------------------------------------------
   Mobile UX Enhancements (2025-09-15)
   - Fluid typography, safe-area support, sticky input, touch targets
   - Polishes layout for very small screens and reduced motion users
--------------------------------------------------- */
html { font-size: clamp(15px, 1.8vw, 16px); }
body { line-height: 1.6; -webkit-text-size-adjust: 100%; }

/* Use dynamic viewport height and safe-area spacing */
.chat-page-container {
    min-height: 100dvh;
    padding-bottom: max(16px, env(safe-area-inset-bottom));
}

/* Sticky headers with subtle backdrop for readability */
.panel-header {
    position: sticky;
    top: env(safe-area-inset-top, 0px);
    z-index: 5;
    backdrop-filter: blur(6px);
    background: var(--panel-bg);
    background: color-mix(in srgb, var(--panel-bg) 88%, transparent);
}

/* Sticky chat input and comfortable tap area */
.chat-input-area {
    position: sticky;
    bottom: max(0px, env(safe-area-inset-bottom));
    padding-bottom: max(8px, env(safe-area-inset-bottom));
    z-index: 6;
}

/* Chat history scroll behavior on mobile */
.chat-history {
    overscroll-behavior-y: contain;
    scroll-behavior: smooth;
}

/* Horizontal scrolling suggestions on small widths */
.query-suggestions {
    display: flex;
    gap: 8px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scroll-snap-type: x proximity;
    padding: 8px 12px;
}
.query-suggestions button {
    scroll-snap-align: start;
    flex: 0 0 auto;
    min-height: 40px;
}

/* Touch target minimum sizes */
button, .btn, .icon-btn, .panel-toggle-btn, .settings-btn {
    min-height: 44px;
    min-width: 44px;
    touch-action: manipulation;
}

/* Mobile sticky actions respect safe-area */
.results-sticky-actions {
    position: sticky;
    bottom: max(0px, env(safe-area-inset-bottom));
    left: 0;
    right: 0;
    display: none; /* shown on mobile only */
    justify-content: center;
    gap: 8px;
    padding: 8px 12px;
    padding-bottom: max(8px, env(safe-area-inset-bottom));
    background: color-mix(in srgb, var(--panel-bg) 92%, transparent);
    border-top: 1px solid var(--border-color, #4a5568);
    box-shadow: 0 -4px 12px rgba(0,0,0,0.25);
    backdrop-filter: blur(6px);
    z-index: 7;
}
@media (max-width: 768px) { .results-sticky-actions { display: flex; } }

/* Reduced motion users */
@media (prefers-reduced-motion: reduce) {
    * { animation: none !important; transition: none !important; scroll-behavior: auto !important; }
}

/* Narrow phone layout refinements */
@media (max-width: 600px) {
    .chat-page-container { grid-template-columns: 1fr; }
    .connections-panel { order: 3; }
    .results-panel { order: 2; }
    .chat-panel { order: 1; }
    .panel-header h3 { font-size: 1.05rem; }
    .chat-input-area textarea { min-height: clamp(44px, 12svh, 120px); }
}

/* Modals safe-area handling */
.modal .modal-content {
    margin-top: max(10px, env(safe-area-inset-top));
    margin-bottom: max(10px, env(safe-area-inset-bottom));
}

/* Data tables scroll on mobile */
.table-container {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

/* Visible focus outline for accessibility */
:where(button, .btn, .icon-btn, .panel-toggle-btn):focus-visible {
    outline: 2px solid var(--accent-color, #3b82f6);
    outline-offset: 2px;
}

/* Mobile Tabs and Slides */
:root {
    --tab-index: 1; /* 0=connections, 1=chat, 2=views */
    --tabbar-h: 52px;
}

/* Hidden by default on desktop; becomes visible under 768px */
.mobile-tabs,
.mobile-slides {
    display: none;
}

@media (max-width: 768px) {
    /* Make the main container a simple block; slides will handle layout */
    .chat-page-container {
        display: block;
        margin: 0;
        border-radius: 0;
        min-height: 100dvh;
        overflow: visible;               /* ensure sticky children aren't clipped */
        height: auto;                    /* let content define height */
    }

    .chat-panel { 
        min-height: 0;                   /* important for flex + sticky on mobile */
    }

    /* Tab strip */
    .mobile-tabs {
        position: sticky;
        top: env(safe-area-inset-top, 0px);
        display: grid;
        grid-template-columns: repeat(3, 1fr);
        align-items: center;
        gap: 6px;
        padding: 8px 12px;
        background: color-mix(in srgb, var(--panel-bg, #2d3748) 92%, transparent);
        border-bottom: 1px solid var(--border-color, #4a5568);
        z-index: 10;
    }

    .mobile-tabs .mobile-tab {
        -webkit-tap-highlight-color: transparent;
        min-height: 44px;
        border: none;
        background: transparent;
        color: var(--text-color, #e2e8f0);
        border-radius: 10px;
        font-weight: 600;
    }
    .mobile-tabs .mobile-tab[aria-selected="true"] {
        background: color-mix(in srgb, var(--panel-bg, #2d3748) 70%, transparent);
    }

    /* Underline indicator */
    .mobile-tab-indicator {
        position: absolute;
        left: 0;
        bottom: 0;
        height: 3px;
        width: calc(100% / 3);
        transform: translateX(calc(var(--tab-index, 1) * 100%));
        background: var(--accent-color, #3b82f6);
        transition: transform 250ms ease;
        border-radius: 3px 3px 0 0;
        pointer-events: none;
    }

    /* Slides: 100vw panels with translateX */
    .mobile-slides {
        display: grid;
        grid-auto-flow: column;
        grid-auto-columns: 100vw;
        width: 100vw;
        height: calc(100dvh - var(--tabbar-h, 52px));
        overflow: visible;               /* don't clip sticky children */
        transform: translate3d(calc(-100vw * var(--tab-index, 1)), 0, 0);
        transition: transform 300ms ease;
        will-change: transform;
        touch-action: pan-y;
        contain: paint;
        z-index: 5;
    }
    .mobile-slide {
        overflow: visible;               /* allow sticky descendants to show */
        display: block;
    }

    /* Ensure input stays on top and visible */
    .chat-input-area {
        position: sticky;
        bottom: max(0px, env(safe-area-inset-bottom));
        padding-bottom: max(8px, env(safe-area-inset-bottom));
        z-index: 10;                     /* above most local surfaces */
    }

    /* Keep the last messages from being covered by the sticky input */
    .chat-history {
        padding-bottom: max(84px, calc(env(safe-area-inset-bottom) + 84px));
        max-height: none;                /* allow natural height with internal scrolling */
    }

    /* When using reduced motion, avoid sliding animation */
    @media (prefers-reduced-motion: reduce) {
        .mobile-tab-indicator { transition: none !important; }
        .mobile-slides { transition: none !important; }
    }
}

/* Keep sticky input and sticky actions working within slides (already added previously) */
/* existing code integrates sticky behavior */

/* Mobile sticky headers are defined earlier. Restore desktop behavior here */
@media (min-width: 1001px) {
    .panel-header {
        position: static;
        backdrop-filter: none;
        /* Inherit background from current theme/panel so it looks as before */
        background: inherit;
    }
    .chat-input-area {
        position: static;
        /* restore prior spacing */
        padding-bottom: 15px;
    }
}
