/* =============================================
   Visual SQL Query Builder — Styles
   Theme: IDE-like split view
   ============================================= */

:root {
    --bg-primary: #0f1117;
    --bg-secondary: #161822;
    --bg-tertiary: #1c1f2e;
    --bg-card: #1e2133;
    --bg-hover: #252940;
    --border: #2a2e42;
    --border-light: #343856;
    --text-primary: #e4e6f0;
    --text-secondary: #9ca3bf;
    --text-muted: #6b7294;
    --blue-400: #60a5fa;
    --blue-500: #3b82f6;
    --blue-600: #2563eb;
    --green-400: #4ade80;
    --green-500: #22c55e;
    --green-600: #16a34a;
    --orange-400: #fb923c;
    --orange-500: #f97316;
    --purple-400: #c084fc;
    --purple-500: #a855f7;
    --red-400: #f87171;
    --red-500: #ef4444;
    --yellow-400: #facc15;
    --cyan-400: #22d3ee;

    --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    --font-mono: 'JetBrains Mono', 'Fira Code', monospace;

    --radius-sm: 6px;
    --radius-md: 10px;
    --radius-lg: 14px;

    --shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    --transition: 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

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

html {
    font-size: 16px;
}

body {
    font-family: var(--font-sans);
    background: var(--bg-primary);
    color: var(--text-primary);
    line-height: 1.5;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

/* Header */
.app-header {
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 50;
}

.header-inner {
    max-width: 1600px;
    margin: 0 auto;
    padding: 0.625rem 1.25rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
}

.logo {
    display: flex;
    align-items: center;
    gap: 0.625rem;
}

.logo h1 {
    font-size: 1rem;
    font-weight: 700;
    color: var(--text-primary);
}

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

/* Common Controls */
.select-sm,
.input-sm {
    background: var(--bg-tertiary);
    color: var(--text-primary);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 0.35rem 0.625rem;
    font-size: 0.8125rem;
    font-family: var(--font-sans);
    cursor: pointer;
    transition: var(--transition);
}

.select-sm:focus,
.input-sm:focus {
    outline: none;
    border-color: var(--blue-500);
    box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.2);
}

.select-sm option {
    background: var(--bg-secondary);
}

.select-multi {
    background: var(--bg-tertiary);
    color: var(--text-primary);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 0.25rem;
    font-size: 0.8125rem;
    font-family: var(--font-sans);
    min-height: 60px;
    width: 100%;
}

.select-multi:focus {
    outline: none;
    border-color: var(--blue-500);
}

/* Buttons */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 0.375rem;
    padding: 0.4rem 0.875rem;
    border: none;
    border-radius: var(--radius-sm);
    font-size: 0.8125rem;
    font-weight: 600;
    font-family: var(--font-sans);
    cursor: pointer;
    transition: var(--transition);
    white-space: nowrap;
}

.btn-ghost {
    background: transparent;
    color: var(--text-secondary);
}

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

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

.btn-secondary:hover {
    background: var(--bg-hover);
    border-color: var(--border-light);
}

.btn-sm {
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
    padding: 0.2rem 0.5rem;
    border: none;
    border-radius: var(--radius-sm);
    font-size: 0.75rem;
    font-weight: 600;
    font-family: var(--font-sans);
    cursor: pointer;
    transition: var(--transition);
}

.btn-accent {
    background: rgba(59, 130, 246, 0.15);
    color: var(--blue-400);
}

.btn-accent:hover {
    background: rgba(59, 130, 246, 0.25);
}

.btn-danger {
    background: rgba(239, 68, 68, 0.15);
    color: var(--red-400);
}

.btn-danger:hover {
    background: rgba(239, 68, 68, 0.25);
}

/* Main */
.app-main {
    flex: 1;
    overflow: hidden;
}

/* Split Layout */
.split-layout {
    display: grid;
    grid-template-columns: 1fr 1fr;
    height: calc(100vh - 52px);
}

/* Builder Panel */
.builder-panel {
    overflow-y: auto;
    padding: 1rem;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    border-right: 1px solid var(--border);
}

.builder-section {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: 1rem;
}

.section-title {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.8125rem;
    font-weight: 700;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.04em;
    margin-bottom: 0.75rem;
}

.section-title .btn-sm {
    margin-left: auto;
}

/* Schema Browser */
.schema-browser {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.table-card {
    background: var(--bg-tertiary);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    overflow: hidden;
    transition: var(--transition);
}

.table-card.selected {
    border-color: var(--blue-500);
    box-shadow: 0 0 0 1px rgba(59, 130, 246, 0.3);
}

.table-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.5rem 0.75rem;
    cursor: pointer;
    transition: var(--transition);
    user-select: none;
}

.table-header:hover {
    background: var(--bg-hover);
}

.table-name {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-weight: 600;
    font-size: 0.875rem;
}

.table-icon {
    width: 20px;
    height: 20px;
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.6875rem;
    font-weight: 700;
    color: var(--bg-primary);
}

.table-icon.blue {
    background: var(--blue-400);
}

.table-icon.green {
    background: var(--green-400);
}

.table-icon.orange {
    background: var(--orange-400);
}

.table-icon.purple {
    background: var(--purple-400);
}

.table-toggle {
    font-size: 0.75rem;
    color: var(--text-muted);
    transition: transform var(--transition);
}

.table-card.open .table-toggle {
    transform: rotate(180deg);
}

.table-columns {
    display: none;
    padding: 0 0.5rem 0.5rem;
}

.table-card.open .table-columns {
    display: block;
}

.column-item {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.3rem 0.5rem;
    border-radius: 4px;
    cursor: pointer;
    font-size: 0.8125rem;
    transition: var(--transition);
    color: var(--text-secondary);
}

.column-item:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

.column-item.selected {
    background: rgba(34, 197, 94, 0.1);
    color: var(--green-400);
}

.column-item .col-type {
    font-size: 0.6875rem;
    color: var(--text-muted);
    font-family: var(--font-mono);
    margin-left: auto;
}

.column-item .col-key {
    font-size: 0.625rem;
    padding: 0.1rem 0.3rem;
    border-radius: 3px;
    font-weight: 700;
}

.col-key.pk {
    background: rgba(250, 204, 21, 0.15);
    color: var(--yellow-400);
}

.col-key.fk {
    background: rgba(168, 85, 247, 0.15);
    color: var(--purple-400);
}

/* Joins */
.joins-list,
.conditions-list {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.join-row,
.condition-row {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem;
    background: var(--bg-tertiary);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    flex-wrap: wrap;
}

.join-row select,
.condition-row select,
.condition-row input {
    background: var(--bg-primary);
    color: var(--text-primary);
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 0.25rem 0.5rem;
    font-size: 0.8125rem;
    font-family: var(--font-sans);
    flex: 1;
    min-width: 80px;
}

.join-row select:focus,
.condition-row select:focus,
.condition-row input:focus {
    outline: none;
    border-color: var(--blue-500);
}

.join-type-badge {
    font-size: 0.6875rem;
    font-weight: 700;
    padding: 0.15rem 0.4rem;
    border-radius: 3px;
    background: rgba(59, 130, 246, 0.15);
    color: var(--blue-400);
    white-space: nowrap;
}

.condition-logic {
    font-size: 0.75rem;
    font-weight: 700;
    color: var(--orange-400);
    padding: 0.15rem 0.4rem;
    border-radius: 3px;
    background: rgba(249, 115, 22, 0.12);
    cursor: pointer;
    border: none;
    font-family: var(--font-sans);
}

.empty-state {
    font-size: 0.8125rem;
    color: var(--text-muted);
    font-style: italic;
    padding: 0.5rem;
    text-align: center;
}

/* Options Grid */
.options-grid {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

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

.option-row label {
    font-size: 0.75rem;
    font-weight: 700;
    color: var(--purple-400);
    font-family: var(--font-mono);
    min-width: 80px;
}

/* Output Panel */
.output-panel {
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.output-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.75rem 1.25rem;
    border-bottom: 1px solid var(--border);
    background: var(--bg-secondary);
}

.output-header h2 {
    font-size: 0.875rem;
    font-weight: 700;
    color: var(--text-primary);
}

.output-actions {
    display: flex;
    gap: 0.375rem;
}

.sql-output {
    flex: 1;
    overflow-y: auto;
    padding: 1.25rem;
    background: var(--bg-primary);
}

.sql-output pre {
    margin: 0;
    white-space: pre-wrap;
    word-break: break-word;
}

.sql-output code {
    font-family: var(--font-mono);
    font-size: 0.9rem;
    line-height: 1.7;
    color: var(--text-primary);
}

/* Syntax Highlighting */
.kw {
    color: var(--blue-400);
    font-weight: 600;
}

.fn {
    color: var(--purple-400);
}

.str {
    color: var(--green-400);
}

.num {
    color: var(--orange-400);
}

.op {
    color: var(--red-400);
}

.tbl {
    color: var(--cyan-400);
}

.col-hl {
    color: var(--text-primary);
}

.comment {
    color: var(--text-muted);
    font-style: italic;
}

/* Explanation */
.explanation-panel {
    padding: 1rem 1.25rem;
    border-top: 1px solid var(--border);
    background: var(--bg-secondary);
}

.explanation-panel h3 {
    display: flex;
    align-items: center;
    gap: 0.375rem;
    font-size: 0.75rem;
    font-weight: 700;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.04em;
    margin-bottom: 0.5rem;
}

.explanation-panel p {
    font-size: 0.8125rem;
    color: var(--text-secondary);
    line-height: 1.6;
}

/* Modal */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(4px);
    z-index: 200;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s;
}

.modal-overlay.visible {
    opacity: 1;
    pointer-events: auto;
}

.modal {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    width: 90%;
    max-width: 500px;
    box-shadow: var(--shadow);
}

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

.modal-header h3 {
    font-size: 1rem;
    font-weight: 700;
}

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

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

.modal-body {
    padding: 1rem 1.25rem;
    max-height: 400px;
    overflow-y: auto;
}

.saved-query-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.75rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    margin-bottom: 0.5rem;
    cursor: pointer;
    transition: var(--transition);
}

.saved-query-item:hover {
    background: var(--bg-hover);
    border-color: var(--blue-500);
}

.saved-query-name {
    font-weight: 600;
    font-size: 0.875rem;
}

.saved-query-date {
    font-size: 0.75rem;
    color: var(--text-muted);
}

.modal-empty {
    text-align: center;
    color: var(--text-muted);
    padding: 2rem;
}

/* Toast */
.toast {
    position: fixed;
    bottom: 1.5rem;
    left: 50%;
    transform: translateX(-50%) translateY(80px);
    background: var(--bg-card);
    color: var(--text-primary);
    border: 1px solid var(--border);
    padding: 0.625rem 1.25rem;
    border-radius: var(--radius-md);
    font-size: 0.8125rem;
    font-weight: 500;
    box-shadow: var(--shadow);
    z-index: 300;
    opacity: 0;
    transition: all 0.3s;
    pointer-events: none;
}

.toast.visible {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

/* Responsive */
@media (max-width: 900px) {
    .split-layout {
        grid-template-columns: 1fr;
        height: auto;
    }

    .builder-panel {
        border-right: none;
        border-bottom: 1px solid var(--border);
        max-height: 50vh;
    }

    .output-panel {
        min-height: 50vh;
    }
}

@media (max-width: 640px) {
    .header-inner {
        padding: 0.5rem 0.75rem;
    }

    .logo h1 {
        font-size: 0.875rem;
    }

    .btn span {
        display: none;
    }

    .builder-section {
        padding: 0.75rem;
    }

    .join-row,
    .condition-row {
        flex-direction: column;
        align-items: stretch;
    }
}