:root {
    --bg-primary: #0f172a;
    --bg-secondary: #1e293b;
    --accent: #6366f1;
    --accent-hover: #4f46e5;
    --text-primary: #f8fafc;
    --text-secondary: #94a3b8;
    --border: rgba(255, 255, 255, 0.1);
    --glass: rgba(30, 41, 59, 0.7);
    --glass-border: rgba(255, 255, 255, 0.05);
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: 'Inter', system-ui, -apple-system, sans-serif;
    background-color: var(--bg-primary);
    color: var(--text-primary);
    height: 100vh;
    overflow: hidden;
}

.app-container {
    display: flex;
    height: 100vh;
    width: 100vw;
}

/* Sidebar / Tree View */
.sidebar {
    width: 250px;
    min-width: 150px;
    max-width: 600px;
    background: var(--bg-secondary);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    position: relative;
    user-select: none;
    flex-shrink: 0;
}

.sidebar-header {
    padding: 1.5rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.brand {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.logo-img {
    width: 28px;
    height: 28px;
    object-fit: contain;
}

.sidebar-header h2 {
    font-size: 1rem;
    font-weight: 600;
}

.btn-add {
    background: var(--accent);
    border: none;
    color: white;
    width: 28px;
    height: 28px;
    border-radius: 6px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
}

.btn-add:hover {
    background: var(--accent-hover);
}

.tree-container {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 1rem;
    padding-top: 0;
    scrollbar-width: thin;
    scrollbar-color: var(--border) transparent;
}

/* Hidden scrollbar that shows on hover */
.tree-container::-webkit-scrollbar {
    width: 6px;
}

.tree-container::-webkit-scrollbar-track {
    background: transparent;
}

.tree-container::-webkit-scrollbar-thumb {
    background: transparent;
    border-radius: 3px;
}

.tree-container:hover::-webkit-scrollbar-thumb {
    background: var(--border);
}

.sidebar-footer {
    padding: 1rem;
    border-top: 1px solid var(--border);
    background: rgba(0, 0, 0, 0.2);
}

.btn-docs {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.75rem 1rem;
    background: var(--accent);
    color: white !important;
    text-decoration: none;
    border-radius: 8px;
    font-size: 0.9rem;
    font-weight: 600;
    transition: all 0.2s;
    justify-content: center;
}

.btn-docs:hover {
    filter: brightness(1.2);
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(99, 102, 241, 0.3);
}

.btn-docs i {
    width: 18px;
    height: 18px;
}

.tree-item {
    padding: 0.5rem;
    cursor: pointer;
    border-radius: 4px;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.9rem;
    transition: background 0.2s;
}

.tree-item:hover {
    background: rgba(255, 255, 255, 0.05);
}

.tree-item i {
    color: var(--accent);
}

/* Main Content Area */
.main-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.tab-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* Tab Bar */
.tab-bar {
    display: flex;
    align-items: center;
    background: var(--bg-secondary);
    border-top: 1px solid var(--border);
    height: 36px;
    min-height: 36px;
    padding: 0 4px;
    gap: 2px;
    user-select: none;
}

.tab-bar-list {
    display: flex;
    flex: 1;
    overflow-x: auto;
    gap: 2px;
    scrollbar-width: none;
}

.tab-bar-list::-webkit-scrollbar {
    display: none;
}

.tab-item {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 4px 12px;
    background: transparent;
    border: none;
    color: var(--text-secondary);
    font-size: 0.75rem;
    font-family: inherit;
    cursor: pointer;
    white-space: nowrap;
    border-radius: 4px 4px 0 0;
    transition: all 0.15s;
    max-width: 180px;
    position: relative;
}

.tab-item:hover {
    background: rgba(255, 255, 255, 0.05);
    color: var(--text-primary);
}

.tab-item.active {
    background: rgba(99, 102, 241, 0.12);
    color: var(--text-primary);
    border-bottom: 2px solid var(--accent);
}

.tab-item-title {
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 120px;
}

.tab-item .tab-close {
    width: 16px;
    height: 16px;
    border-radius: 3px;
    border: none;
    background: transparent;
    color: var(--text-secondary);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    line-height: 1;
    opacity: 0;
    transition: all 0.15s;
    padding: 0;
}

.tab-item:hover .tab-close {
    opacity: 0.7;
}

.tab-item .tab-close:hover {
    opacity: 1;
    background: rgba(239, 68, 68, 0.2);
    color: #ef4444;
}

.tab-add {
    width: 28px;
    height: 28px;
    border-radius: 4px;
    border: none;
    background: transparent;
    color: var(--text-secondary);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: all 0.15s;
}

.tab-add:hover {
    background: rgba(255, 255, 255, 0.08);
    color: var(--text-primary);
}

/* Vertical Splitter for Sidebar */
.resizer-v {
    width: 8px;
    cursor: col-resize;
    position: absolute;
    right: -4px;
    top: 0;
    bottom: 0;
    z-index: 100;
    transition: background 0.2s;
    background: transparent;
}

.resizer-v:hover {
    background: var(--accent);
    opacity: 0.5;
}

.resizer-v.active {
    background: var(--accent);
    opacity: 1;
}

/* Row Action Buttons */
.row-action-btn {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    border: 1px solid var(--border);
    background: rgba(255, 255, 255, 0.05);
    color: var(--text-dim);
    font-size: 11px;
    font-weight: 600;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s;
}

.row-action-btn:hover {
    background: var(--accent);
    border-color: var(--accent);
    color: white;
    transform: scale(1.1);
}

.row-action-btn.active {
    background: var(--accent);
    color: white;
}

/* Query Section */
.query-section {
    height: 350px;
    min-height: 100px;
    display: flex;
    flex-direction: column;
    border-bottom: 1px solid var(--border);
    position: relative;
    background: #1e1e1e;
}

.toolbar {
    padding: 0.75rem 1.5rem;
    background: var(--bg-secondary);
    display: flex;
    justify-content: flex-end;
    gap: 1rem;
}

.btn-execute {
    background: var(--accent);
    border: none;
    color: white;
    padding: 0.5rem 1.5rem;
    border-radius: 6px;
    font-weight: 600;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.editor-container {
    flex: 1;
    background: #1e1e1e;
}

/* Horizontal Splitter for Query/Results */
.resizer-h {
    height: 6px;
    cursor: row-resize;
    position: absolute;
    bottom: -3px;
    left: 0;
    right: 0;
    z-index: 100;
    transition: background 0.2s;
}

.resizer-h:hover,
.resizer-h.active {
    background: var(--accent);
}

/* Results Section */
.results-section {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.results-toolbar {
    padding: 0.75rem 1.5rem;
    background: rgba(255, 255, 255, 0.02);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.results-title {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-dim);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.results-actions {
    display: flex;
    gap: 0.5rem;
}

.btn-icon {
    background: none;
    border: 1px solid transparent;
    color: var(--text-dim);
    width: 32px;
    height: 32px;
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s;
}

.btn-icon:hover {
    background: rgba(255, 255, 255, 0.05);
    color: var(--text-secondary);
    border-color: var(--border);
}

.grid-container {
    flex: 1;
    overflow: auto;
    /* Handles both X and Y scrolling */
    position: relative;
    padding: 1rem;
    display: block;
    /* Ensure it behaves as a block for scrolling */
}

/* iframe for HTML/non-table results */
.grid-container iframe {
    width: 100%;
    height: calc(100% - 2rem);
    min-height: 300px;
    border: none;
    background: white;
    border-radius: 8px;
}

.results-table {
    width: auto;
    /* Allow table to grow based on content/resizing */
    min-width: 100%;
    /* Ensure it fills at least the container */
    border-spacing: 0;
    border-collapse: separate;
    font-size: 0.875rem;
    border-radius: 8px;
    /* overflow: hidden; Removed to allow sticky headers to work better with scroll */
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.2);
    table-layout: fixed;
    /* Important for reliable column resizing */
}

.results-table th {
    position: sticky;
    top: 0;
    background: #1e293b;
    color: var(--text-secondary);
    font-weight: 500;
    text-transform: uppercase;
    font-size: 0.75rem;
    letter-spacing: 0.05em;
    padding: 1rem 0.75rem;
    border-bottom: 2px solid var(--border);
    z-index: 20;
    text-align: left;
}

.sort-header {
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    white-space: nowrap;
    /* Prevent header text wrapping */
    overflow: hidden;
    text-overflow: ellipsis;
}

.results-table td {
    padding: 1rem 0.75rem;
    border-bottom: 1px solid var(--border);
    color: var(--text-primary);
    white-space: nowrap;
    max-width: 300px;
    overflow: hidden;
    text-overflow: ellipsis;
}

.results-table tr:last-child td {
    border-bottom: none;
}

.results-table tr:hover td {
    background: rgba(99, 102, 241, 0.05);
}

/* Badge styles for different types */
.badge {
    padding: 0.25rem 0.625rem;
    border-radius: 9999px;
    font-size: 0.75rem;
    font-weight: 500;
}

.badge-id {
    background: rgba(59, 130, 246, 0.1);
    color: #60a5fa;
    border: 1px solid rgba(59, 130, 246, 0.2);
}

.badge-string {
    color: var(--text-primary);
}

.badge-number {
    color: #fbbf24;
}

.badge-bool {
    background: rgba(16, 185, 129, 0.1);
    color: #34d399;
}

/* Filter Inputs */
.filter-input {
    display: block;
    width: 100%;
    margin-top: 0.5rem;
    background: rgba(15, 23, 42, 0.6);
    border: 1px solid var(--border);
    border-radius: 6px;
    color: white;
    padding: 0.375rem 0.5rem;
    font-size: 0.75rem;
    outline: none;
    transition: all 0.2s;
}

.filter-input:focus {
    border-color: var(--accent);
    background: rgba(15, 23, 42, 0.8);
    box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.2);
}

/* Checkbox Modernization */
input[type="checkbox"] {
    appearance: none;
    -webkit-appearance: none;
    width: 1.1rem;
    height: 1.1rem;
    border: 2px solid var(--border);
    border-radius: 4px;
    background: transparent;
    cursor: pointer;
    position: relative;
    transition: all 0.2s;
}

input[type="checkbox"]:checked {
    background: var(--accent);
    border-color: var(--accent);
}

input[type="checkbox"]:checked::after {
    content: '✓';
    position: absolute;
    color: white;
    font-size: 0.8rem;
    left: 2px;
    top: -1px;
}

/* Context Menu */
.context-menu {
    position: fixed;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.5);
    border-radius: 6px;
    padding: 0.5rem 0;
    z-index: 1000;
    min-width: 160px;
}

.context-menu-item {
    padding: 0.5rem 1rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 0.75rem;
    font-size: 0.9rem;
}

.context-menu-item:hover {
    background: var(--accent);
}

/* Custom Scrollbar */
::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--text-secondary);
}

/* Login Modal */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.8);
    backdrop-filter: blur(5px);
    display: none;
    justify-content: center;
    align-items: center;
    z-index: 9999;
}

.login-box {
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    padding: 2rem;
    border-radius: 12px;
    width: 100%;
    max-width: 400px;
    box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.5);
}

.login-header {
    text-align: center;
    margin-bottom: 2rem;
}

.login-logo {
    width: 48px;
    height: 48px;
    margin-bottom: 1rem;
}

.form-group {
    margin-bottom: 1rem;
}

.form-label {
    display: block;
    margin-bottom: 0.5rem;
    font-size: 0.875rem;
    color: var(--text-secondary);
}

.form-input {
    width: 100%;
    padding: 0.75rem;
    background: rgba(0, 0, 0, 0.2);
    border: 1px solid var(--border);
    border-radius: 6px;
    color: white;
    outline: none;
    transition: all 0.2s;
}

.form-input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.2);
}

.btn-primary {
    width: 100%;
    padding: 0.75rem;
    background: var(--accent);
    color: white;
    border: none;
    border-radius: 6px;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.2s;
}

.btn-primary:hover {
    background: var(--accent-hover);
}

.error-msg {
    color: #ef4444;
    font-size: 0.875rem;
    text-align: center;
    margin-top: 1rem;
    display: none;
}

/* Column Resizing */
.col-resizer {
    position: absolute;
    right: 0;
    top: 0;
    bottom: 0;
    width: 4px;
    cursor: col-resize;
    background: transparent;
    z-index: 30;
    transition: background 0.2s;
}

.col-resizer:hover,
.col-resizer.active {
    background: var(--accent);
}

/* Ensure TH has relative positioning for resizer */
.results-table th {
    position: relative;
}

/* History Button */
.btn-history {
    background: transparent;
    border: 1px solid var(--border);
    color: var(--text-secondary);
    padding: 0.5rem 1.25rem;
    border-radius: 6px;
    font-weight: 600;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    transition: all 0.2s;
    font-size: 0.875rem;
}

.btn-history:hover {
    background: rgba(255, 255, 255, 0.05);
    border-color: var(--accent);
    color: var(--text-primary);
}

/* History Modal */
.history-modal {
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    padding: 0;
    border-radius: 16px;
    width: 95%;
    max-width: 600px;
    max-height: 80vh;
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.7);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    animation: modal-enter 0.3s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes modal-enter {
    from { opacity: 0; transform: scale(0.95) translateY(10px); }
    to { opacity: 1; transform: scale(1) translateY(0); }
}

.history-modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 1.5rem;
    background: rgba(0, 0, 0, 0.2);
    border-bottom: 1px solid var(--border);
}

.history-modal-header h3 {
    font-size: 1.1rem;
    font-weight: 600;
    color: var(--text-primary);
    display: flex;
    align-items: center;
    gap: 10px;
}

.history-filter-bar {
    padding: 1rem 1.5rem;
    background: rgba(0, 0, 0, 0.1);
    border-bottom: 1px solid var(--border);
}

.history-list {
    flex: 1;
    overflow-y: auto;
    padding: 0.5rem 0;
}

.history-item {
    display: flex;
    align-items: center;
    gap: 1.25rem;
    padding: 1rem 1.5rem;
    margin: 4px 12px;
    border-radius: 10px;
    cursor: pointer;
    transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
    position: relative;
    border: 1px solid transparent;
}

.history-item:hover {
    background: rgba(255, 255, 255, 0.05);
    transform: translateX(4px);
    border-color: var(--border);
}

.history-item.active {
    background: linear-gradient(90deg, rgba(99, 102, 241, 0.15), rgba(99, 102, 241, 0.05));
    border-color: rgba(99, 102, 241, 0.3);
}

.history-item-icon {
    width: 40px;
    height: 40px;
    background: rgba(255, 255, 255, 0.03);
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--accent);
    flex-shrink: 0;
    transition: all 0.2s;
}

.history-item:hover .history-item-icon {
    background: var(--accent);
    color: white;
}

.history-item-content {
    flex: 1;
    min-width: 0;
}

.history-item .preview {
    font-family: 'JetBrains Mono', 'Cascadia Code', monospace;
    font-size: 0.85rem;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin-bottom: 0.35rem;
    letter-spacing: -0.01em;
}

.history-item .timestamp {
    font-size: 0.75rem;
    color: var(--text-secondary);
    display: flex;
    align-items: center;
    gap: 4px;
}

.history-item .btn-delete {
    background: rgba(239, 68, 68, 0.05);
    border: 1px solid rgba(239, 68, 68, 0.1);
    color: var(--text-secondary);
    width: 32px;
    height: 32px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s;
    opacity: 0;
    transform: scale(0.8);
}

.history-item:hover .btn-delete {
    opacity: 1;
    transform: scale(1);
}

.history-item .btn-delete:hover {
    background: #ef4444;
    color: white;
    border-color: #ef4444;
}

/* Empty State Styling */
.history-list .empty-state {
    padding: 3rem 1rem;
    text-align: center;
    color: var(--text-secondary);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1rem;
}

.history-list .empty-state i {
    opacity: 0.3;
}