/* Tables visual theme adapter.
 *
 * This stylesheet is intentionally loaded after the legacy Tables styles and
 * every modal/Guide partial.  Module components consume only the semantic
 * tokens below; each token maps to the shared application palette.  Adding a
 * new application theme therefore requires no Tables-specific colour pass.
 * Database actions, drag/resize behavior and relation events are untouched.
 */
:root {
    color-scheme: light;

    --tables-canvas: var(--pp-app-bg);
    --tables-surface: var(--pp-surface);
    --tables-surface-raised: var(--pp-surface-raised);
    --tables-surface-soft: var(--pp-surface-soft);
    --tables-surface-muted: var(--pp-surface-muted);
    --tables-input: var(--pp-input-bg);
    --tables-border: var(--pp-border);
    --tables-border-strong: var(--pp-border-strong);
    --tables-text: var(--pp-text);
    --tables-text-muted: var(--pp-text-muted);
    --tables-accent: var(--pp-accent);
    --tables-accent-soft: var(--pp-accent-soft);
    --tables-on-accent: var(--bs-white);
    --tables-shadow-color: var(--pp-shadow-color);

    --tables-hover: color-mix(in srgb, var(--tables-accent-soft) 58%, var(--tables-surface));
    --tables-selected: var(--tables-accent-soft);
    --tables-focus-ring: color-mix(in srgb, var(--tables-accent) 32%, transparent);
    --tables-overlay: color-mix(in srgb, var(--pp-navbar-bg) 74%, transparent);
    --tables-modal-header: linear-gradient(135deg,
        color-mix(in srgb, var(--tables-accent-soft) 64%, var(--tables-surface-raised)),
        var(--tables-surface-raised));
    --tables-modal-header-border: color-mix(in srgb, var(--tables-accent) 42%, var(--tables-border));
    --tables-tooltip-bg: var(--tables-surface-raised);
    --tables-tooltip-text: var(--tables-text);
    --tables-tooltip-border: var(--tables-border-strong);
    --tables-relation-line: var(--tables-accent);
    --tables-pk-accent: var(--tables-accent);
    --tables-ai-accent: var(--bs-info);
    --tables-warning-accent: var(--bs-warning);
    --tables-danger-accent: var(--bs-danger);
    --tables-success-accent: var(--bs-success);
}

html[data-pp-theme="dark"] {
    color-scheme: dark;
}

/* Canvas, toolbar, sidebar and cards. */
#mainArea {
    --tables-header-row-height: 50px;
}

.left-panel > .panel-header,
.right-workspace > .toolbar {
    box-sizing: border-box;
    flex: 0 0 var(--tables-header-row-height);
    width: 100%;
    min-height: var(--tables-header-row-height);
    height: var(--tables-header-row-height);
    align-items: center;
    border-color: var(--tables-border) !important;
    background: var(--tables-surface-soft) !important;
    color: var(--tables-text) !important;
}

.left-panel > .panel-header { padding: .42rem .55rem; }
.right-workspace > .toolbar {
    flex-wrap: nowrap;
    overflow-x: auto;
    overflow-y: hidden;
    padding: .42rem .55rem;
    scrollbar-width: thin;
}
.right-workspace > .toolbar > .btn { flex: 0 0 auto; white-space: nowrap; }

.tables-module .modal-backdrop.show {
    background: var(--tables-overlay) !important;
    opacity: 1 !important;
}

#mainArea,
.right-workspace,
.workspace-content,
#selectedTablesContainer {
    background: var(--tables-canvas) !important;
    color: var(--tables-text) !important;
}

.left-panel,
.table-filter,
.table-list,
.table-group-body,
.table-card,
.table-card .card-body,
.table-card .card-header {
    border-color: var(--tables-border) !important;
    background: var(--tables-surface) !important;
    color: var(--tables-text) !important;
}

.table-filter input,
.table-card :where(.form-control, .form-select) {
    border-color: var(--tables-border-strong) !important;
    background: var(--tables-input) !important;
    color: var(--tables-text) !important;
}
.table-filter input::placeholder { color: var(--tables-text-muted) !important; }
.table-filter input:focus {
    border-color: var(--tables-accent) !important;
    background: var(--tables-input) !important;
    box-shadow: 0 0 0 3px var(--tables-focus-ring) !important;
}
.table-filter i,
.table-row-meta,
.table-card .field-list-item .fi,
#emptySelection { color: var(--tables-text-muted) !important; }
.table-list-stats,
.table-group-count {
    background: var(--tables-surface-muted) !important;
    color: var(--tables-accent) !important;
}

.table-group-header {
    border-color: var(--tables-border) !important;
    background: linear-gradient(180deg, var(--tables-surface-soft), var(--tables-surface-muted)) !important;
    color: var(--tables-text) !important;
}
.table-group[data-group="project"] > .table-group-header { border-left-color: var(--tables-accent) !important; }
.table-group[data-group="system-generated"] > .table-group-header { border-left-color: var(--tables-border-strong) !important; }
.table-group-header:hover { background: var(--tables-hover) !important; }
.table-group-title i { color: var(--tables-text-muted) !important; }

.left-panel .table-list .table-group-body > .table-list-item,
.left-panel .table-list .table-group-body > .table-list-item[data-scope="system"] {
    border-color: var(--tables-border) !important;
    background: var(--tables-surface) !important;
    color: var(--tables-text) !important;
}
.left-panel .table-list .table-group-body > .table-list-item:hover,
.left-panel .table-list .table-group-body > .table-list-item[data-scope="system"]:hover {
    background: var(--tables-hover) !important;
}
.table-list-item[data-scope="system"] .table-row-label i,
.table-list-item[data-scope="system"] .table-row-name { color: var(--tables-text-muted) !important; }

.table-card { box-shadow: 0 2px 8px var(--tables-shadow-color) !important; }
.table-card:hover { box-shadow: 0 8px 20px var(--tables-shadow-color) !important; }
.table-card.system-table-card,
.table-card.system-table-card .card-header,
.table-card .card-header { background: var(--tables-surface-soft) !important; }
.table-card .field-list-item {
    border-color: var(--tables-border) !important;
    color: var(--tables-text) !important;
}
.table-card .field-list-item.highlight-match {
    outline-color: var(--tables-accent) !important;
    background: var(--tables-selected) !important;
}
.table-card .field-list-item.link-source-field {
    outline: 3px solid var(--tables-accent) !important;
    outline-offset: -2px;
}
.relation-count-badge { color: var(--tables-on-accent) !important; }

#relationsSvg { color: var(--tables-relation-line) !important; }
#relationsSvg .relation-arrow-line { stroke: var(--tables-relation-line) !important; }
#relationsSvg .relation-arrow-group:hover .relation-arrow-line {
    stroke: var(--tables-accent) !important;
    filter: drop-shadow(0 2px 3px var(--tables-focus-ring)) !important;
}
#relationsSvg #arrowhead path { fill: var(--tables-relation-line) !important; }
.relation-hint {
    border-color: var(--tables-border) !important;
    background: color-mix(in srgb, var(--tables-surface-raised) 94%, transparent) !important;
    color: var(--tables-text-muted) !important;
    box-shadow: 0 8px 24px var(--tables-shadow-color) !important;
}

/* Buttons and focus states inherit the active palette rather than Bootstrap blue. */
#mainArea .btn-primary,
#mainArea .btn-outline-primary:hover,
#mainArea .btn-outline-primary:focus-visible {
    --bs-btn-bg: var(--tables-accent);
    --bs-btn-border-color: var(--tables-accent);
    --bs-btn-hover-bg: color-mix(in srgb, var(--tables-accent) 84%, var(--tables-text));
    --bs-btn-hover-border-color: color-mix(in srgb, var(--tables-accent) 84%, var(--tables-text));
    --bs-btn-active-bg: color-mix(in srgb, var(--tables-accent) 76%, var(--tables-text));
    --bs-btn-active-border-color: color-mix(in srgb, var(--tables-accent) 76%, var(--tables-text));
}
#mainArea .btn-outline-primary {
    --bs-btn-color: var(--tables-accent);
    --bs-btn-border-color: var(--tables-accent);
}
#mainArea .btn-info,
#mainArea .btn-outline-info:hover,
#mainArea .btn-outline-info:focus-visible {
    --bs-btn-bg: var(--tables-accent);
    --bs-btn-border-color: var(--tables-accent);
    --bs-btn-color: var(--tables-surface-raised);
    --bs-btn-hover-bg: color-mix(in srgb, var(--tables-accent) 84%, var(--tables-text));
    --bs-btn-hover-border-color: color-mix(in srgb, var(--tables-accent) 84%, var(--tables-text));
}
#mainArea .btn-outline-info {
    --bs-btn-color: var(--tables-accent);
    --bs-btn-border-color: var(--tables-accent);
}

/* Theme-aware Bootstrap tooltips. */
.tables-tooltip .tooltip-inner {
    border-color: var(--tables-tooltip-border) !important;
    background: var(--tables-tooltip-bg) !important;
    color: var(--tables-tooltip-text) !important;
    box-shadow: 0 .7rem 1.6rem var(--tables-shadow-color) !important;
}
.tables-tooltip.bs-tooltip-top .tooltip-arrow::before { border-top-color: var(--tables-tooltip-bg) !important; }
.tables-tooltip.bs-tooltip-bottom .tooltip-arrow::before { border-bottom-color: var(--tables-tooltip-bg) !important; }
.tables-tooltip.bs-tooltip-start .tooltip-arrow::before { border-left-color: var(--tables-tooltip-bg) !important; }
.tables-tooltip.bs-tooltip-end .tooltip-arrow::before { border-right-color: var(--tables-tooltip-bg) !important; }

/* Every Tables modal uses one palette-driven frame, including legacy utility headers. */
:is(#createTableModal, #editTableModal, #createCustomViewModal, #createSqlViewModal, #relationModal, #editRelationModal) .modal-content {
    border: 1px solid var(--tables-border) !important;
    background: var(--tables-surface-raised) !important;
    color: var(--tables-text) !important;
    box-shadow: 0 24px 70px var(--tables-shadow-color) !important;
}
:is(#createTableModal, #editTableModal, #createCustomViewModal, #createSqlViewModal, #relationModal, #editRelationModal) .modal-header,
:is(#createTableModal, #editTableModal, #createCustomViewModal, #createSqlViewModal, #relationModal, #editRelationModal) .modal-header:is(.bg-primary, .bg-info, .bg-secondary),
:is(#relationModal, #editRelationModal) :is(.relation-modal-header, .relation-modal-header-edit) {
    display: flex;
    align-items: center;
    min-height: 3rem;
    border-color: var(--tables-modal-header-border) !important;
    background: var(--tables-modal-header) !important;
    color: var(--tables-text) !important;
}
:is(#createTableModal, #editTableModal, #createCustomViewModal, #createSqlViewModal, #relationModal, #editRelationModal) .modal-title,
:is(#createTableModal, #editTableModal, #createCustomViewModal, #createSqlViewModal, #relationModal, #editRelationModal) .modal-header i {
    color: var(--tables-text) !important;
}
.relation-modal-subtitle { color: var(--tables-text-muted) !important; }
:is(#createTableModal, #editTableModal, #createCustomViewModal, #createSqlViewModal, #relationModal, #editRelationModal) .modal-body {
    background: var(--tables-surface-soft) !important;
    color: var(--tables-text) !important;
}
:is(#createTableModal, #editTableModal, #createCustomViewModal, #createSqlViewModal, #relationModal, #editRelationModal) .modal-footer {
    border-color: var(--tables-border) !important;
    background: var(--tables-surface) !important;
    color: var(--tables-text) !important;
}
:is(#createTableModal, #editTableModal, #createCustomViewModal, #createSqlViewModal, #relationModal, #editRelationModal) :where(.form-control, .form-select, .input-group-text, textarea) {
    border-color: var(--tables-border-strong) !important;
    background-color: var(--tables-input) !important;
    color: var(--tables-text) !important;
}
:is(#createTableModal, #editTableModal, #createCustomViewModal, #createSqlViewModal, #relationModal, #editRelationModal) :where(.form-control, textarea)::placeholder {
    color: var(--tables-text-muted) !important;
}
:is(#createTableModal, #editTableModal, #createCustomViewModal, #createSqlViewModal, #relationModal, #editRelationModal) :where(.form-control, .form-select, textarea):focus {
    border-color: var(--tables-accent) !important;
    box-shadow: 0 0 0 .18rem var(--tables-focus-ring) !important;
}
:is(#createTableModal, #editTableModal, #createCustomViewModal, #createSqlViewModal, #relationModal, #editRelationModal) :where(.form-control, .form-select):disabled {
    border-color: var(--tables-border) !important;
    background: var(--tables-surface-muted) !important;
    color: var(--tables-text-muted) !important;
    opacity: 1;
}
:is(#createTableModal, #editTableModal, #createCustomViewModal, #createSqlViewModal, #relationModal, #editRelationModal) .form-check-input {
    border-color: var(--tables-border-strong) !important;
    background-color: var(--tables-input) !important;
}
:is(#createTableModal, #editTableModal, #createCustomViewModal, #createSqlViewModal, #relationModal, #editRelationModal) .form-check-input:checked {
    border-color: var(--tables-accent) !important;
    background-color: var(--tables-accent) !important;
}

:is(#createTableModal, #editTableModal, #createCustomViewModal, #createSqlViewModal, #relationModal, #editRelationModal) .modal-header .btn-close {
    box-sizing: border-box !important;
    width: 2rem;
    height: 2rem;
    flex: 0 0 2rem;
    margin: 0 0 0 auto !important;
    padding: 0 !important;
    border: 1px solid var(--tables-border-strong) !important;
    border-radius: .58rem;
    background-color: var(--tables-surface-muted) !important;
    background-position: center;
    background-size: .78rem;
    opacity: .82;
    filter: none;
}
html[data-pp-theme="dark"] :is(#createTableModal, #editTableModal, #createCustomViewModal, #createSqlViewModal, #relationModal, #editRelationModal) .modal-header .btn-close {
    filter: invert(1) grayscale(1);
}
:is(#createTableModal, #editTableModal, #createCustomViewModal, #createSqlViewModal, #relationModal, #editRelationModal) .modal-header .btn-close:hover {
    background-color: var(--tables-hover) !important;
    opacity: 1;
}
:is(#createTableModal, #editTableModal, #createCustomViewModal, #createSqlViewModal, #relationModal, #editRelationModal) .modal-header .btn-close:focus-visible {
    outline: 3px solid var(--tables-focus-ring);
    outline-offset: 2px;
    box-shadow: none;
}

/* Create/Edit schema editor. */
.schema-resize-corner { border-color: color-mix(in srgb, var(--tables-accent) 58%, transparent) !important; }
.schema-resize-right:hover,
.schema-resize-bottom:hover { background: color-mix(in srgb, var(--tables-accent) 14%, transparent) !important; }
.schema-resize-corner:hover { border-color: var(--tables-accent) !important; }
.schema-table-meta,
.schema-fields-wrap,
.schema-field-row {
    border-color: var(--tables-border) !important;
    background: var(--tables-surface) !important;
    color: var(--tables-text) !important;
    box-shadow: none !important;
}
.schema-help {
    border-color: var(--tables-modal-header-border) !important;
    background: var(--tables-accent-soft) !important;
    color: var(--tables-text) !important;
}
.schema-help i { color: var(--tables-accent) !important; }
.schema-field-header {
    border-color: var(--tables-border) !important;
    background: linear-gradient(180deg, var(--tables-surface-soft), var(--tables-surface-muted)) !important;
    color: var(--tables-text-muted) !important;
}
.schema-field-row:nth-child(even) { background: var(--tables-surface-soft) !important; }
.schema-field-row:hover { background: var(--tables-hover) !important; }
.schema-section-title,
.schema-field-row .schema-check .form-check-label { color: var(--tables-text) !important; }
.schema-field-row .schema-check {
    border-color: var(--tables-border) !important;
    background: var(--tables-surface-soft) !important;
}
.schema-field-row .schema-check-pk {
    border-color: color-mix(in srgb, var(--tables-pk-accent) 52%, var(--tables-border)) !important;
    background: color-mix(in srgb, var(--tables-pk-accent) 14%, var(--tables-surface)) !important;
}
.schema-field-row .schema-check-ai {
    border-color: color-mix(in srgb, var(--tables-ai-accent) 52%, var(--tables-border)) !important;
    background: color-mix(in srgb, var(--tables-ai-accent) 14%, var(--tables-surface)) !important;
}
.schema-field-row .schema-check-pk .form-check-label { color: var(--tables-pk-accent) !important; }
.schema-field-row .schema-check-ai .form-check-label { color: var(--tables-ai-accent) !important; }
.schema-field-row .schema-check-pk .form-check-input:checked { border-color: var(--tables-pk-accent) !important; background-color: var(--tables-pk-accent) !important; }
.schema-field-row .schema-check-ai .form-check-input:checked { border-color: var(--tables-ai-accent) !important; background-color: var(--tables-ai-accent) !important; }
.schema-field-row .schema-check-pk:has(.form-check-input:checked) { border-color: var(--tables-pk-accent) !important; background: color-mix(in srgb, var(--tables-pk-accent) 25%, var(--tables-surface)) !important; }
.schema-field-row .schema-check-ai:has(.form-check-input:checked) { border-color: var(--tables-ai-accent) !important; background: color-mix(in srgb, var(--tables-ai-accent) 25%, var(--tables-surface)) !important; }

/* Custom and SQL view dialogs. */
:is(#createCustomViewModal, #createSqlViewModal) code { color: var(--tables-accent) !important; }
:is(#createCustomViewModal, #createSqlViewModal) .form-text { color: var(--tables-text-muted) !important; }

/* Relationship editor hierarchy. */
.relation-help,
.relation-help-compact {
    border-color: var(--tables-modal-header-border) !important;
    background: var(--tables-accent-soft) !important;
    color: var(--tables-text) !important;
}
.relation-help i,
.relation-guide summary { color: var(--tables-accent) !important; }
.relation-node,
.relation-section,
.relation-option-card,
.relation-link-mode-card,
.relation-choice,
.relation-order-card,
.relation-page-chip,
.relation-guide,
.relation-guide-grid div,
.relation-flow-icon {
    border-color: var(--tables-border) !important;
    background: var(--tables-surface) !important;
    color: var(--tables-text) !important;
    box-shadow: none !important;
}
.relation-choice,
.relation-link-mode-card,
.relation-order-card,
.relation-page-chip,
.relation-guide-grid div { background: var(--tables-surface-soft) !important; }
.relation-node-master,
.relation-node-detail { border-top-color: var(--tables-accent) !important; }
.relation-flow-line { background: var(--tables-accent) !important; }
.relation-flow-icon { color: var(--tables-accent) !important; }
.relation-node-title,
.relation-node-title span,
.relation-section-title span,
.relation-choice strong,
.relation-option-card strong,
.relation-option-heading,
.relation-mini-choice,
.relation-order-card strong,
.relation-guide-grid strong { color: var(--tables-text) !important; }
.relation-node-title small,
.relation-section-title small,
.relation-choice small,
.relation-option-card small,
.relation-order-card small,
.relation-guide-grid span,
.relation-control > span { color: var(--tables-text-muted) !important; }
.relation-choice:hover,
.relation-option-card:hover,
.relation-page-chip:hover {
    border-color: var(--tables-accent) !important;
    background: var(--tables-hover) !important;
}
.relation-choice:has(.form-check-input:checked),
.relation-page-chip:has(.form-check-input:checked) {
    border-color: var(--tables-accent) !important;
    background: var(--tables-selected) !important;
    color: var(--tables-accent) !important;
}
.relation-warning-card {
    border-color: color-mix(in srgb, var(--tables-warning-accent) 62%, var(--tables-border)) !important;
    background: color-mix(in srgb, var(--tables-warning-accent) 12%, var(--tables-surface)) !important;
}
.relation-fk-actions,
.relation-badge-color { border-color: var(--tables-border) !important; }
.relation-fk-actions span { color: color-mix(in srgb, var(--tables-warning-accent) 72%, var(--tables-text)) !important; }
.relation-future-pages { background: var(--tables-surface-soft) !important; }

/* Tables Guide overlay is not a Bootstrap modal. */
.tgh-modal {
    border: 1px solid var(--tables-border) !important;
    background: var(--tables-surface-raised) !important;
    color: var(--tables-text) !important;
    box-shadow: 0 32px 80px var(--tables-shadow-color) !important;
}
.tgh-body,
.tgh-content { background: var(--tables-surface) !important; color: var(--tables-text) !important; }
.tgh-toc { border-color: var(--tables-border) !important; background: var(--tables-surface-soft) !important; }
.tgh-toc a { color: var(--tables-text-muted) !important; }
.tgh-toc a:hover,
.tgh-toc a.active { background: var(--tables-selected) !important; color: var(--tables-accent) !important; }
.tgh-content h3,
.tgh-card h5 { color: var(--tables-text) !important; }
.tgh-content h4 { color: var(--tables-accent) !important; }
.tgh-content p,
.tgh-content li,
.tgh-lead,
.tgh-card p,
.tgh-foot { color: var(--tables-text-muted) !important; }
.tgh-card,
.tgh-table th,
.tgh-table td {
    border-color: var(--tables-border) !important;
    background: var(--tables-surface) !important;
    color: var(--tables-text) !important;
}
.tgh-table th { background: var(--tables-surface-soft) !important; color: var(--tables-text-muted) !important; }
.tgh-content code,
.tgh-kbd {
    border-color: var(--tables-border-strong) !important;
    background: var(--tables-surface-muted) !important;
    color: var(--tables-accent) !important;
}
.tgh-callout.info { background: var(--tables-accent-soft) !important; color: var(--tables-text) !important; }
.tgh-callout.tip { background: color-mix(in srgb, var(--tables-success-accent) 14%, var(--tables-surface)) !important; color: var(--tables-text) !important; }
.tgh-callout.warn { background: color-mix(in srgb, var(--tables-warning-accent) 14%, var(--tables-surface)) !important; color: var(--tables-text) !important; }

@media (max-width: 767.98px) {
    :is(#createTableModal, #editTableModal, #createCustomViewModal, #createSqlViewModal, #relationModal, #editRelationModal) .modal-header .btn-close {
        width: 1.9rem;
        height: 1.9rem;
        flex-basis: 1.9rem;
    }
}

@media (prefers-reduced-motion: reduce) {
    .table-card,
    .table-group-header,
    .relation-choice,
    .relation-option-card,
    .relation-page-chip,
    .tgh-modal { transition: none !important; }
}

/* ══════════════════════════ SQL Editor ══════════════════════════ */
.sqled-modal .modal-dialog { max-width: min(1100px, 96vw); }
.sqled { background: var(--pp-surface); color: var(--pp-text); border: 1px solid var(--pp-border); border-radius: 14px; overflow: hidden; }
.sqled-header { align-items: flex-start; background: var(--tables-modal-header, var(--pp-surface-muted)); border-bottom: 1px solid var(--pp-border); }
.sqled-title-wrap .modal-title { font-weight: 800; }
.sqled-sub { font-size: .76rem; color: var(--pp-text-muted); margin-top: 2px; display: flex; align-items: center; gap: .4rem; flex-wrap: wrap; }
.sqled-badge { display: inline-flex; align-items: center; padding: .1rem .5rem; border-radius: 999px; font-size: .68rem; font-weight: 800; letter-spacing: .02em; background: color-mix(in srgb, var(--pp-accent) 16%, transparent); color: var(--pp-accent); border: 1px solid color-mix(in srgb, var(--pp-accent) 30%, transparent); }
.sqled-body { padding: 12px 14px; }

.sqled-bar { display: flex; flex-wrap: wrap; align-items: center; gap: .4rem; margin-bottom: 10px; }
.sqled-bar .sqled-sep { width: 1px; align-self: stretch; margin: 2px 4px; background: var(--pp-border); }
.sqled-bar .sqled-check { display: inline-flex; align-items: center; gap: .35rem; font-size: .76rem; color: var(--pp-text-muted); margin: 0; cursor: pointer; user-select: none; }
.sqled-kbd { font-size: .68rem; opacity: .7; border: 1px solid currentColor; border-radius: 4px; padding: 0 .28rem; margin-left: .15rem; }

.sqled-editor { height: 260px; border: 1px solid var(--pp-border); border-radius: 10px; overflow: hidden; background: var(--pp-input-bg, var(--tables-input)); }
.sqled-editor .CodeMirror { height: 100%; font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace; font-size: 13px; line-height: 1.5; background: transparent; color: var(--pp-text); }
.sqled-editor .CodeMirror-gutters { background: color-mix(in srgb, var(--pp-text) 5%, transparent); border-right: 1px solid var(--pp-border); }
.sqled-editor .CodeMirror-linenumber { color: var(--pp-text-muted); }
.sqled-editor .CodeMirror-cursor { border-left: 1.6px solid var(--pp-accent); }
.sqled-editor .CodeMirror-selected { background: color-mix(in srgb, var(--pp-accent) 18%, transparent) !important; }
.sqled-editor .CodeMirror-activeline-background { background: color-mix(in srgb, var(--pp-accent) 7%, transparent); }
.sqled-editor .CodeMirror-matchingbracket { color: var(--pp-accent) !important; font-weight: 700; }
/* syntax tokens — readable on light + dark */
.sqled-editor .cm-keyword { color: var(--pp-accent); font-weight: 700; }
.sqled-editor .cm-string, .sqled-editor .cm-string-2 { color: var(--bs-success); }
.sqled-editor .cm-number { color: var(--bs-warning); }
.sqled-editor .cm-comment { color: var(--pp-text-muted); font-style: italic; }
.sqled-editor .cm-operator, .sqled-editor .cm-punctuation { color: var(--pp-text-muted); }
.sqled-editor .cm-variable-2, .sqled-editor .cm-variable-3, .sqled-editor .cm-type { color: var(--bs-info); }
.sqled-editor .cm-atom, .sqled-editor .cm-builtin { color: var(--bs-danger); }
.sqled-fallback { width: 100%; height: 100%; border: 0; resize: none; padding: 10px 12px; font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 13px; background: transparent; color: var(--pp-text); outline: none; }

.sqled-results { margin-top: 12px; max-height: 34vh; overflow: auto; border-radius: 10px; }
.sqled-empty, .sqled-note { padding: 14px; border: 1px dashed var(--pp-border); border-radius: 10px; color: var(--pp-text-muted); font-size: .82rem; background: var(--pp-surface-soft, transparent); }
.sqled-note.sqled-error { border-style: solid; border-color: var(--bs-danger); color: var(--bs-danger); background: color-mix(in srgb, var(--bs-danger) 8%, transparent); }
.sqled-note.sqled-warn { border-color: var(--bs-warning); color: var(--bs-warning-text-emphasis); }

.sqled-summary { display: flex; flex-wrap: wrap; gap: 12px; align-items: center; padding: 8px 12px; border-radius: 9px; font-size: .8rem; margin-bottom: 8px; border: 1px solid var(--pp-border); background: var(--pp-surface-muted); }
.sqled-summary.ok .s-ico { color: var(--bs-success); }
.sqled-summary.bad .s-ico { color: var(--bs-danger); }
.sqled-summary .s-fail { color: var(--bs-danger); font-weight: 600; }
.sqled-summary .s-tx { margin-left: auto; padding: .05rem .45rem; border-radius: 6px; background: color-mix(in srgb, var(--pp-accent) 12%, transparent); color: var(--pp-accent); font-weight: 700; }

.sqled-stmt { border: 1px solid var(--pp-border); border-radius: 9px; margin-bottom: 7px; overflow: hidden; background: var(--pp-surface); }
.sqled-stmt.err { border-color: color-mix(in srgb, var(--bs-danger) 55%, var(--pp-border)); }
.sqled-stmt-head { display: flex; align-items: center; gap: .55rem; padding: 7px 10px; }
.sqled-badge2 { width: 20px; height: 20px; display: inline-grid; place-items: center; border-radius: 6px; font-size: .72rem; flex: 0 0 auto; }
.sqled-stmt.ok .sqled-badge2 { background: color-mix(in srgb, var(--bs-success) 16%, transparent); color: var(--bs-success); }
.sqled-stmt.err .sqled-badge2 { background: color-mix(in srgb, var(--bs-danger) 16%, transparent); color: var(--bs-danger); }
.sqled-stmt.skip .sqled-badge2 { background: var(--pp-surface-muted); color: var(--pp-text-muted); }
.sqled-n { font-size: .72rem; font-weight: 800; color: var(--pp-text-muted); flex: 0 0 auto; }
.sqled-sql { flex: 1 1 auto; min-width: 0; font-size: .76rem; color: var(--pp-text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; background: transparent; }
.sqled-meta { flex: 0 0 auto; font-size: .72rem; color: var(--pp-text-muted); }
.sqled-errmsg { padding: 8px 12px; border-top: 1px solid var(--pp-border); font-size: .78rem; color: var(--bs-danger); font-family: ui-monospace, Menlo, monospace; white-space: pre-wrap; }

.sqled-grid-wrap { overflow: auto; border-top: 1px solid var(--pp-border); max-height: 240px; }
.sqled-grid { width: 100%; border-collapse: collapse; font-size: .76rem; }
.sqled-grid th, .sqled-grid td { padding: 5px 10px; border-bottom: 1px solid var(--pp-border); border-right: 1px solid var(--pp-border); text-align: left; white-space: nowrap; }
.sqled-grid th { position: sticky; top: 0; background: var(--pp-surface-muted); font-weight: 700; z-index: 1; }
.sqled-grid tbody tr:hover { background: color-mix(in srgb, var(--pp-accent) 6%, transparent); }
.sqled-null { color: var(--pp-text-muted); font-style: italic; }
.sqled-grid-note { padding: 5px 10px; font-size: .72rem; color: var(--pp-text-muted); }

.sqled-footer { justify-content: flex-end; gap: .5rem; border-top: 1px solid var(--pp-border); }
.sqled-status { margin-right: auto; font-size: .76rem; color: var(--pp-text-muted); }

.sqled-mismatch { display: flex; gap: .6rem; align-items: flex-start; padding: 10px 12px; margin-bottom: 8px; border-radius: 9px; font-size: .8rem; line-height: 1.5;
  background: color-mix(in srgb, var(--bs-warning) 12%, var(--pp-surface)); border: 1px solid color-mix(in srgb, var(--bs-warning) 45%, transparent); color: var(--pp-text); }
.sqled-mismatch > i { color: var(--bs-warning); font-size: 1rem; margin-top: 1px; flex: 0 0 auto; }
.sqled-mismatch code { background: color-mix(in srgb, var(--bs-warning) 20%, transparent); padding: 0 .3rem; border-radius: 4px; }

/* SQL Editor — dialect "Fix" / conversion */
.sqled-fixrow { display: flex; align-items: center; gap: .6rem; margin-top: .55rem; flex-wrap: wrap; }
.sqled-fixbtn { display: inline-flex; align-items: center; gap: .4rem; padding: .32rem .7rem; border-radius: 7px; border: 1px solid var(--bs-warning); background: var(--bs-warning); color: var(--bs-warning-text-emphasis); font-size: .78rem; font-weight: 700; cursor: pointer; }
.sqled-fixbtn:hover { filter: brightness(1.05); }
.sqled-fixnote { font-size: .72rem; color: var(--pp-text-muted); }

.sqled-conv { border: 1px solid var(--pp-border); border-radius: 10px; padding: 12px; background: var(--pp-surface); }
.sqled-conv-head { font-size: .82rem; font-weight: 700; margin-bottom: 10px; display: flex; align-items: center; gap: .45rem; flex-wrap: wrap; }
.sqled-conv-head > i { color: var(--pp-accent); }
.sqled-be { font-weight: 500; color: var(--pp-text-muted); }
.sqled-conv-sec { margin-bottom: 10px; }
.sqled-conv-sec.warn { background: color-mix(in srgb, var(--bs-warning) 10%, transparent); border: 1px solid color-mix(in srgb, var(--bs-warning) 40%, transparent); border-radius: 8px; padding: 8px 10px; }
.sqled-conv-t { font-size: .72rem; font-weight: 800; letter-spacing: .02em; text-transform: uppercase; color: var(--pp-text-muted); margin-bottom: 4px; }
.sqled-conv-sec.warn .sqled-conv-t { color: var(--bs-warning-text-emphasis); display: flex; align-items: center; gap: .35rem; }
.sqled-conv-sec ul { margin: 0; padding-left: 18px; font-size: .78rem; line-height: 1.55; }
.sqled-conv-sec.warn ul { color: var(--bs-warning-text-emphasis); }
.sqled-conv-pre { max-height: 220px; overflow: auto; margin: 4px 0 10px; padding: 10px 12px; border: 1px solid var(--pp-border); border-radius: 8px; background: var(--pp-input-bg, var(--tables-input)); color: var(--pp-text); font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 12.5px; line-height: 1.5; white-space: pre; }
.sqled-conv-actions { display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; }
.sqled-conv-hint { font-size: .72rem; color: var(--pp-text-muted); }

/* SQL Editor — Phase 2: error line highlighting */
.sqled-editor .CodeMirror .sqled-errline { background: color-mix(in srgb, var(--bs-danger) 14%, transparent) !important; }
.sqled-gutter { width: 14px; }
.sqled-gutter-mark { color: var(--bs-danger); font-size: 12px; line-height: 1; padding-left: 2px; cursor: help; }
.sqled-stmt[data-line] { cursor: pointer; }
.sqled-stmt.err[data-line]:hover { background: color-mix(in srgb, var(--bs-danger) 6%, var(--pp-surface)); }
.sqled-line { flex: 0 0 auto; font-size: .68rem; font-weight: 700; color: var(--pp-text-muted); background: color-mix(in srgb, var(--pp-text) 8%, transparent); padding: 0 .3rem; border-radius: 4px; }
.sqled-stmt.err .sqled-line { color: var(--bs-danger); background: color-mix(in srgb, var(--bs-danger) 12%, transparent); }

/* SQL Editor — Phase 3: Preview plan + dry run */
.sqled-plan { border: 1px solid var(--pp-border); border-radius: 10px; padding: 12px; background: var(--pp-surface); }
.sqled-plan-head { font-size: .82rem; font-weight: 800; display: flex; align-items: center; gap: .45rem; margin-bottom: 8px; }
.sqled-plan-head > i { color: var(--pp-accent); }
.sqled-plan-chips { display: flex; flex-wrap: wrap; gap: .4rem; margin-bottom: 8px; }
.sqled-chip { font-size: .72rem; font-weight: 700; padding: .18rem .5rem; border-radius: 999px; background: var(--pp-surface-muted); color: var(--pp-text-muted); border: 1px solid var(--pp-border); }
.sqled-chip.danger { background: color-mix(in srgb, var(--bs-danger) 12%, transparent); color: var(--bs-danger); border-color: color-mix(in srgb, var(--bs-danger) 35%, transparent); }
.sqled-danger { display: flex; gap: .5rem; align-items: flex-start; padding: 9px 11px; margin-bottom: 8px; border-radius: 8px; font-size: .78rem; background: color-mix(in srgb, var(--bs-danger) 9%, transparent); border: 1px solid color-mix(in srgb, var(--bs-danger) 40%, transparent); color: var(--bs-danger); }
.sqled-danger > i { margin-top: 1px; }
.sqled-plan-list { display: flex; flex-direction: column; gap: 3px; }
.sqled-plan-row { display: flex; align-items: center; gap: .5rem; padding: 6px 8px; border-radius: 7px; font-size: .8rem; cursor: pointer; }
.sqled-plan-row:hover { background: color-mix(in srgb, var(--pp-accent) 6%, transparent); }
.sqled-plan-row.destructive { background: color-mix(in srgb, var(--bs-danger) 7%, transparent); }
.sqled-plan-ico { width: 20px; height: 20px; display: inline-grid; place-items: center; border-radius: 6px; background: var(--pp-surface-muted); color: var(--pp-text-muted); flex: 0 0 auto; font-size: .74rem; }
.sqled-plan-row.destructive .sqled-plan-ico { background: color-mix(in srgb, var(--bs-danger) 16%, transparent); color: var(--bs-danger); }
.sqled-plan-label { flex: 1 1 auto; min-width: 0; }
.sqled-plan-label code { background: color-mix(in srgb, var(--pp-accent) 10%, transparent); padding: 0 .3rem; border-radius: 4px; }
.sqled-plan-rows { flex: 0 0 auto; font-size: .72rem; color: var(--pp-text-muted); }
.sqled-plan-note { flex: 0 0 auto; font-size: .72rem; font-weight: 700; color: var(--bs-danger); }
.sqled-note.sqled-dry { border-style: solid; border-color: color-mix(in srgb, var(--pp-accent) 45%, transparent); color: var(--pp-text); background: color-mix(in srgb, var(--pp-accent) 8%, transparent); }
#sqledDryRun:disabled { opacity: .5; }

/* SQL Editor — real-time mismatch banner + source picker */
.sqled-live-mismatch { display: flex; align-items: center; gap: .55rem; margin-top: 8px; padding: 8px 11px; border-radius: 9px; font-size: .8rem; line-height: 1.45;
  background: color-mix(in srgb, var(--bs-warning) 12%, var(--pp-surface)); border: 1px solid color-mix(in srgb, var(--bs-warning) 45%, transparent); color: var(--pp-text); }
.sqled-live-mismatch > i { color: var(--bs-warning); font-size: 1rem; flex: 0 0 auto; }
.sqled-live-text { flex: 1 1 auto; min-width: 0; }
.sqled-live-text .sqled-fixbtn { margin-left: .4rem; vertical-align: middle; }
.sqled-live-text code { background: color-mix(in srgb, var(--bs-warning) 20%, transparent); padding: 0 .3rem; border-radius: 4px; }
.sqled-live-x { flex: 0 0 auto; border: 0; background: transparent; color: var(--pp-text-muted); font-size: 1.1rem; line-height: 1; cursor: pointer; padding: 0 .2rem; }
.sqled-live-x:hover { color: var(--pp-text); }
.sqled-src { margin-left: auto; font-size: .72rem; font-weight: 600; color: var(--pp-text-muted); display: inline-flex; align-items: center; gap: .3rem; }
.sqled-src-sel { font-size: .72rem; padding: .12rem .3rem; border-radius: 6px; border: 1px solid var(--pp-border); background: var(--pp-input-bg, var(--tables-input)); color: var(--pp-text); }

/* SQL Editor — button clarity, snippets, history, autocomplete, CSV */
.sqled-bar-label { font-size: .68rem; font-weight: 800; text-transform: uppercase; letter-spacing: .04em; color: var(--pp-text-muted); align-self: center; margin-right: .1rem; }
.sqled-run-real { font-weight: 700; }
.sqled-run-real .sqled-runhint { font-size: .62rem; font-weight: 600; opacity: .85; border: 1px solid currentColor; border-radius: 4px; padding: 0 .28rem; margin: 0 .1rem; }
.sqled-writes-note { margin-right: auto; font-size: .72rem; color: var(--pp-text-muted); display: inline-flex; align-items: center; gap: .3rem; }
.sqled-footer { flex-wrap: wrap; }
.sqled-snippets { max-height: 60vh; overflow: auto; font-size: .82rem; }
.sqled-snippets .dropdown-header { font-size: .66rem; text-transform: uppercase; letter-spacing: .04em; font-weight: 800; }
.sqled-history-menu { min-width: 320px; max-width: 420px; max-height: 60vh; overflow: auto; }
.sqled-hist-item { display: flex; align-items: center; justify-content: space-between; gap: .6rem; font-size: .78rem; }
.sqled-hist-sql { font-family: ui-monospace, Menlo, monospace; color: var(--pp-text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sqled-hist-t { flex: 0 0 auto; font-size: .68rem; color: var(--pp-text-muted); }
.sqled-grid-foot { display: flex; align-items: center; justify-content: space-between; gap: .6rem; padding: 4px 8px; }
.sqled-export { border: 1px solid var(--pp-border); background: var(--pp-surface-muted); color: var(--pp-text); font-size: .68rem; font-weight: 700; padding: .12rem .45rem; border-radius: 6px; cursor: pointer; display: inline-flex; align-items: center; gap: .25rem; }
.sqled-export:hover { border-color: var(--pp-accent); color: var(--pp-accent); }
/* CodeMirror autocomplete popup */
.CodeMirror-hints.default, .CodeMirror-hints { z-index: 3000; font-family: ui-monospace, Menlo, monospace; font-size: 12.5px; border: 1px solid var(--pp-border); background: var(--pp-surface); box-shadow: 0 10px 30px -8px var(--tables-shadow-color); border-radius: 8px; }
.CodeMirror-hint { color: var(--pp-text); padding: 2px 10px; }
li.CodeMirror-hint-active { background: var(--pp-accent); color: var(--tables-on-accent); }
.sqled-hint-kw::after { content: 'kw'; float: right; font-size: 9px; opacity: .45; margin-left: 10px; }
.CodeMirror-hint { display: flex; align-items: center; }
.sqled-hint-type { margin-left: auto; padding-left: 14px; font-size: 9.5px; opacity: .6; font-style: normal; }
li.CodeMirror-hint-active .sqled-hint-type { opacity: .85; color: var(--tables-on-accent); }
.sqled-hint-col span:first-child { color: var(--pp-accent); }
li.CodeMirror-hint-active.sqled-hint-col span:first-child { color: var(--tables-on-accent); }

/* Custom View / SQL View modals — CodeMirror SQL editor (reuses SQL Editor engine) */
.sqled-viewcm.CodeMirror { border: 1px solid var(--pp-border); border-radius: 8px; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12.5px; line-height: 1.5; background: var(--pp-input-bg, var(--tables-input)); color: var(--pp-text); }
.sqled-viewcm .CodeMirror-gutters { background: color-mix(in srgb, var(--pp-text) 5%, transparent); border-right: 1px solid var(--pp-border); }
.sqled-viewcm .CodeMirror-linenumber { color: var(--pp-text-muted); }
.sqled-viewcm .CodeMirror-cursor { border-left: 1.6px solid var(--pp-accent); }
.sqled-viewcm .CodeMirror-selected { background: color-mix(in srgb, var(--pp-accent) 18%, transparent) !important; }
.sqled-viewcm .CodeMirror-activeline-background { background: color-mix(in srgb, var(--pp-accent) 6%, transparent); }
.sqled-viewcm .cm-keyword { color: var(--pp-accent); font-weight: 700; }
.sqled-viewcm .cm-string, .sqled-viewcm .cm-string-2 { color: var(--bs-success); }
.sqled-viewcm .cm-number { color: var(--bs-warning); }
.sqled-viewcm .cm-comment { color: var(--pp-text-muted); font-style: italic; }
.sqled-viewcm .cm-operator, .sqled-viewcm .cm-punctuation { color: var(--pp-text-muted); }
.sqled-viewcm .cm-variable-2, .sqled-viewcm .cm-type { color: var(--bs-info); }
.sqled-viewcm .cm-atom, .sqled-viewcm .cm-builtin { color: var(--bs-danger); }

/* ── Schema explorer sidebar (Custom View / SQL View modals) ─────────────── */
.sqled-view-layout { display: flex; gap: 14px; align-items: stretch; }
.sqled-view-main { flex: 1 1 auto; min-width: 0; }
.sqled-schema { flex: 0 0 248px; width: 248px; display: flex; flex-direction: column; border: 1px solid var(--pp-border); border-radius: 10px; background: var(--pp-surface-muted); overflow: hidden; max-height: 440px; }
.sqled-schema-head { display: flex; align-items: center; gap: .4rem; padding: 8px 10px; font-size: .72rem; font-weight: 800; text-transform: uppercase; letter-spacing: .04em; color: var(--pp-text-muted); border-bottom: 1px solid var(--pp-border); background: var(--pp-surface); }
.sqled-schema-head > i { color: var(--pp-accent); font-size: .95rem; }
.sqled-schema-search { flex: 1 1 auto; min-width: 0; margin-left: auto; font-size: .74rem; padding: .18rem .45rem; border-radius: 7px; border: 1px solid var(--pp-border); background: var(--pp-input-bg, var(--tables-input)); color: var(--pp-text); text-transform: none; letter-spacing: 0; font-weight: 500; }
.sqled-schema-search:focus { outline: 0; border-color: var(--pp-accent); box-shadow: 0 0 0 2px color-mix(in srgb, var(--pp-accent) 22%, transparent); }
.sqled-schema-list { flex: 1 1 auto; overflow-y: auto; padding: 4px; }
.sqled-schema-empty { padding: 10px; font-size: .75rem; color: var(--pp-text-muted); text-align: center; }
.sqled-tbl { border-radius: 7px; }
.sqled-tbl-head { display: flex; align-items: center; gap: .3rem; padding: 3px 5px; border-radius: 7px; }
.sqled-tbl-head:hover { background: color-mix(in srgb, var(--pp-accent) 8%, transparent); }
.sqled-tbl-toggle { border: 0; background: transparent; color: var(--pp-text-muted); font-size: .7rem; line-height: 1; padding: 2px; cursor: pointer; flex: 0 0 auto; }
.sqled-tbl-toggle:hover { color: var(--pp-accent); }
.sqled-tbl-ico { color: var(--pp-text-muted); font-size: .78rem; flex: 0 0 auto; }
.sqled-tbl-n { flex: 1 1 auto; min-width: 0; font-size: .8rem; font-weight: 600; color: var(--pp-text); cursor: pointer; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sqled-tbl-n:hover { color: var(--pp-accent); text-decoration: underline; }
.sqled-tbl-c { flex: 0 0 auto; font-size: .64rem; font-weight: 700; color: var(--pp-text-muted); background: color-mix(in srgb, var(--pp-text) 10%, transparent); border-radius: 20px; padding: 0 .38rem; }
.sqled-tbl-star { flex: 0 0 auto; border: 0; background: transparent; color: var(--pp-text-muted); font-size: .66rem; padding: 2px 3px; cursor: pointer; border-radius: 5px; }
.sqled-tbl-star:hover { color: var(--pp-accent); background: color-mix(in srgb, var(--pp-accent) 14%, transparent); }
.sqled-tbl-cols { padding: 1px 0 3px 20px; }
.sqled-col { display: flex; align-items: baseline; gap: .5rem; padding: 2px 6px; border-radius: 6px; cursor: pointer; }
.sqled-col:hover { background: color-mix(in srgb, var(--pp-accent) 10%, transparent); }
.sqled-col-n { flex: 0 1 auto; min-width: 0; font-family: ui-monospace, Menlo, monospace; font-size: .74rem; color: var(--pp-text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sqled-col:hover .sqled-col-n { color: var(--pp-accent); }
.sqled-col-t { flex: 1 1 auto; text-align: right; font-size: .64rem; color: var(--pp-text-muted); text-transform: lowercase; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sqled-schema-foot { flex: 0 0 auto; padding: 6px 9px; font-size: .64rem; line-height: 1.4; color: var(--pp-text-muted); border-top: 1px solid var(--pp-border); background: var(--pp-surface); }
.sqled-schema-foot i { color: var(--pp-accent); }
.sqled-schema-foot code { background: color-mix(in srgb, var(--pp-accent) 12%, transparent); padding: 0 .25rem; border-radius: 4px; }
.sqled-schema { flex: 0 0 232px; width: 232px; }
@media (max-width: 575.98px) {
  .sqled-view-layout { flex-direction: column; }
  .sqled-schema { flex-basis: auto; width: 100%; max-height: 220px; }
}
