/* OBSCURA OS - Sheets: the Query Editor and the Queries & Connections pane
   (sheets-query-editor.js). A window of its own over the workbook, as a
   desktop spreadsheet's query editor is: its ribbon, the queries down the
   left, the preview, and the query's settings and steps on the right. */

dialog.pq { width: calc(100vw - 32px); max-width: 1480px; height: calc(100vh - 32px); height: calc(100dvh - 32px); max-height: none; padding: 0; border: 1px solid var(--rule-strong); border-radius: var(--radius-md); background: var(--paper); color: var(--ink); font: var(--text-xs) "Segoe UI", system-ui, sans-serif; overflow: hidden; }
dialog.pq[open] { display: flex; flex-direction: column; }
dialog.pq::backdrop { background: rgba(0, 0, 0, .35); }
.pq-sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

.pq-top { display: flex; align-items: center; gap: 12px; min-height: 40px; padding: 0 6px 0 14px; background: var(--paper-raised); border-bottom: 1px solid var(--rule); color: var(--ink); }
.pq-top h2 { margin: 0; font: 600 var(--text-sm) "Segoe UI", system-ui, sans-serif; }
.pq-file { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--ink-soft); }
.pq-x { min-width: 32px; min-height: 32px; border: 0; border-radius: var(--radius-sm); background: none; color: inherit; font-size: var(--text-lg); line-height: 1; cursor: pointer; }
.pq-x:hover { background: var(--sh-hover); }
.sh-dlg .pq-x { min-width: 28px; min-height: 28px; color: var(--ink-soft); }
.sh-dlg .pq-x:hover { background: var(--sh-hover); }

.pq-tabs { display: flex; gap: 2px; padding: 0 8px; background: var(--sh-ribbon); border-bottom: 1px solid var(--sh-ribbon-rule); }
.pq-tabs button { min-height: 32px; padding: 0 14px; border: 0; border-bottom: 2px solid transparent; background: none; color: var(--ink-soft); font: var(--text-xs) "Segoe UI", system-ui, sans-serif; cursor: pointer; }
.pq-tabs button:hover { color: var(--ink); background: var(--sh-hover); }
.pq-tabs button[aria-selected="true"] { color: var(--sheet-green-dark); border-bottom-color: var(--sheet-green); font-weight: 600; }

.pq-bar { display: flex; align-items: stretch; gap: 0; padding: 4px 4px 0; overflow-x: auto; background: var(--sh-ribbon); border-bottom: 1px solid var(--sh-ribbon-rule); scrollbar-width: thin; }
.pq-group { display: flex; flex-direction: column; flex: none; padding: 0 4px; border-right: 1px solid var(--sh-ribbon-rule); }
.pq-group:last-child { border-right: 0; }
.pq-group-body { display: grid; grid-auto-flow: column; grid-template-rows: repeat(2, auto); align-items: start; gap: 2px 2px; }
.pq-group-label { padding: 3px 0 4px; font-size: var(--text-2xs); color: var(--ink-faint); text-align: center; white-space: nowrap; }
.pq-tb { min-height: 28px; padding: 3px 7px; border: 1px solid transparent; border-radius: var(--radius-sm); background: none; color: var(--ink); font: var(--text-xs) "Segoe UI", system-ui, sans-serif; text-align: left; white-space: nowrap; cursor: pointer; }
.pq-tb:hover, .pq-tb.is-open { background: var(--sh-hover); }
.pq-tb:disabled { opacity: .45; cursor: default; background: none; }
.pq-tb.primary { background: var(--sheet-green); border-color: var(--sheet-green); color: var(--paper); }
.pq-tb.primary:hover { background: var(--sheet-green-dark); border-color: var(--sheet-green-dark); }
.pq-caret::after { content: ""; display: inline-block; width: 5px; height: 5px; margin: 0 0 3px 7px; border-right: 1.5px solid currentColor; border-bottom: 1.5px solid currentColor; transform: rotate(45deg); opacity: .7; }

.pq-main { flex: 1; min-height: 0; display: grid; grid-template-columns: 200px minmax(0, 1fr) 250px; }
.pq-queries, .pq-settings { min-height: 0; overflow: auto; padding: 8px 10px; background: var(--paper-raised); }
.pq-queries { border-right: 1px solid var(--rule); }
.pq-settings { border-left: 1px solid var(--rule); }
.pq-queries h3, .pq-settings h3 { margin: 0 0 8px; font: 600 var(--text-xs) "Segoe UI", system-ui, sans-serif; }
.pq-settings h4 { margin: 6px 0; font: 600 var(--text-2xs) "Segoe UI", system-ui, sans-serif; letter-spacing: .06em; text-transform: uppercase; color: var(--ink-soft); }
.pq-queries ul { list-style: none; margin: 0; padding: 0; }
.pq-queries li button { display: flex; flex-direction: column; align-items: flex-start; gap: 1px; width: 100%; min-height: 32px; padding: 5px 8px; border: 1px solid transparent; border-radius: var(--radius-sm); background: none; color: var(--ink); font: var(--text-xs) "Segoe UI", system-ui, sans-serif; text-align: left; cursor: pointer; }
.pq-queries li button span { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pq-queries li button small { font-size: var(--text-2xs); color: var(--ink-faint); }
.pq-queries li button:hover { background: var(--sh-hover); }
.pq-queries li button[aria-current="true"] { background: var(--sheet-green-soft); border-color: var(--sheet-green); }

.pq-steps { list-style: none; margin: 0; padding: 0; }
.pq-steps li { display: flex; align-items: center; min-height: 30px; border: 1px solid transparent; border-radius: var(--radius-sm); }
.pq-steps li:hover { background: var(--sh-hover); }
.pq-steps li[aria-current="step"] { background: var(--sheet-green-soft); border-color: var(--sheet-green); }
.pq-step { flex: 1; min-width: 0; min-height: 30px; padding: 3px 6px; border: 0; background: none; color: var(--ink); font: var(--text-xs) "Segoe UI", system-ui, sans-serif; text-align: left; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: pointer; }
.pq-steps li.bad .pq-step { color: var(--danger); }
.pq-warn { display: inline-flex; align-items: center; justify-content: center; width: 14px; height: 14px; margin-right: 5px; border-radius: var(--radius-sm); background: var(--danger); color: var(--on-danger); font: 700 var(--text-2xs) "Segoe UI", system-ui, sans-serif; vertical-align: 1px; }
.pq-ico { flex: none; display: inline-flex; align-items: center; justify-content: center; width: 28px; height: 28px; padding: 0; border: 0; border-radius: var(--radius-sm); background: none; color: var(--ink-soft); cursor: pointer; }
.pq-ico:hover { background: var(--paper-inset); color: var(--ink); }
.pq-ico svg { width: 14px; height: 14px; fill: none; stroke: currentColor; stroke-width: 1.5; stroke-linecap: round; }

.pq-centre { min-width: 0; min-height: 0; display: flex; flex-direction: column; }
.pq-say { min-height: 32px; padding: 7px 12px; border-bottom: 1px solid var(--rule); background: var(--paper-raised); font: var(--text-2xs) ui-monospace, "Cascadia Mono", Consolas, monospace; color: var(--ink-soft); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pq-wrap { flex: 1; min-height: 0; overflow: auto; background: var(--paper); outline: none; }
.pq-status { padding: 5px 12px; border-top: 1px solid var(--rule); background: var(--paper-raised); font-size: var(--text-2xs); color: var(--ink-soft); font-variant-numeric: tabular-nums; }

.pq-grid { border-collapse: separate; border-spacing: 0; font-size: var(--text-xs); font-variant-numeric: tabular-nums; }
.pq-grid col.on { background: var(--cell-selected); }
.pq-grid th, .pq-grid td { border-right: 1px solid var(--grid-border); border-bottom: 1px solid var(--grid-border); }
.pq-grid td { max-width: 320px; padding: 3px 8px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pq-grid td.num { text-align: right; }
.pq-grid td.nil { color: var(--ink-faint); font-style: italic; }
.pq-grid td.err { color: var(--danger); font-weight: 600; }
.pq-grid thead th { position: sticky; top: 0; z-index: 2; min-width: 120px; padding: 0; background: var(--grid-head); text-align: left; font-weight: 600; cursor: default; user-select: none; }
.pq-grid thead th[aria-selected="true"] { background: var(--grid-head-on); }
.pq-grid .pq-rn { position: sticky; left: 0; z-index: 1; min-width: 40px; width: 40px; padding: 3px 6px; background: var(--grid-head); color: var(--ink-soft); font-weight: 400; text-align: right; }
.pq-grid thead .pq-rn { z-index: 3; }
.pq-head { display: flex; align-items: center; gap: 4px; padding: 3px 3px 2px 4px; }
.pq-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pq-rename { flex: 1; min-width: 0; min-height: 24px; padding: 1px 4px; border: 1px solid var(--sheet-green); border-radius: var(--radius-sm); background: var(--paper-raised); color: var(--ink); font: 600 var(--text-xs) "Segoe UI", system-ui, sans-serif; }
.pq-type { flex: none; min-width: 34px; min-height: 24px; padding: 0 3px; border: 1px solid transparent; border-radius: var(--radius-sm); background: none; color: var(--sheet-green-dark); font: 600 var(--text-2xs) ui-monospace, "Cascadia Mono", Consolas, monospace; letter-spacing: .02em; cursor: pointer; }
.pq-fbtn { flex: none; width: 24px; height: 24px; padding: 0; border: 1px solid var(--rule-strong); border-radius: var(--radius-sm); background: var(--paper-raised); color: var(--ink-soft); cursor: pointer; }
.pq-type:hover, .pq-fbtn:hover, .pq-fbtn[aria-expanded="true"] { background: var(--sh-hover); border-color: var(--rule-strong); color: var(--ink); }
.pq-fbtn svg { display: block; width: 12px; height: 12px; margin: auto; fill: none; stroke: currentColor; stroke-width: 1.5; }
/* The share of each column's preview that is valid, an error or empty:
   a data scale, three flat parts. */
.pq-quality { display: flex; height: 3px; margin: 0 4px 3px; background: var(--rule); }
.pq-quality i { display: block; height: 100%; }
.pq-quality .ok { background: var(--good); }
.pq-quality .err { background: var(--danger); }
.pq-quality .nil { background: var(--ink-faint); }
.pq-norows, .pq-empty { margin: 0; padding: 16px; color: var(--ink-soft); }
.pq-empty p { margin: 0 0 6px; max-width: 60ch; }
.pq-fail { margin: 16px; padding: 12px 14px; max-width: 70ch; border: 1px solid var(--danger); border-radius: var(--radius-sm); background: var(--danger-soft); }
.pq-fail h3 { margin: 0 0 6px; font: 600 var(--text-xs) "Segoe UI", system-ui, sans-serif; }
.pq-fail p { margin: 0 0 8px; }

/* Menus and the filter, drawn in the editor's layer. */
dialog.pq .pq-menu { position: fixed; z-index: 5; }
.pq-pop { position: fixed; z-index: 5; display: flex; flex-direction: column; width: 250px; max-height: min(460px, calc(100vh - 16px)); padding: 4px 0 0; border: 1px solid var(--rule-strong); border-radius: var(--radius-sm); background: var(--paper-raised); color: var(--ink); }
.pq-pop hr { width: 100%; margin: 3px 0; border: 0; border-top: 1px solid var(--rule); }
.pq-pop-item { min-height: 28px; padding: 3px 12px; border: 0; background: none; color: var(--ink); font: var(--text-xs) "Segoe UI", system-ui, sans-serif; text-align: left; cursor: pointer; }
.pq-pop-item:hover, .pq-pop-item:focus-visible { background: var(--sh-hover); outline: none; }
.pq-pop-find { margin: 6px 10px 4px; min-height: 28px; padding: 3px 6px; border: 1px solid var(--rule-strong); border-radius: var(--radius-sm); background: var(--paper-raised); color: var(--ink); font: var(--text-xs) "Segoe UI", system-ui, sans-serif; }
.pq-pop-list { flex: 1; min-height: 80px; margin: 0 10px; padding: 2px 6px; overflow: auto; border: 1px solid var(--rule); }
.pq-pop-list label[hidden] { display: none; }
.pq-pop-list .pq-null { color: var(--ink-faint); font-style: italic; }
.pq-pop-more { margin: 4px 10px 0; font-size: var(--text-2xs); color: var(--ink-soft); }
.pq-pop-foot { display: flex; justify-content: flex-end; gap: 6px; padding: 8px 10px 10px; }

/* The step dialogs. */
.sh-dlg .pq-cond { display: flex; gap: 6px; margin: 0 0 6px; }
.sh-dlg .pq-cond select, .sh-dlg .pq-cond input { min-height: 28px; padding: 3px 6px; border: 1px solid var(--rule-strong); border-radius: var(--radius-sm); background: var(--paper-raised); color: var(--ink); font: var(--text-xs) "Segoe UI", system-ui, sans-serif; }
.sh-dlg .pq-cond select { flex: 1 1 50%; min-width: 0; }
.sh-dlg .pq-cond input { flex: 1 1 50%; min-width: 0; }
.sh-dlg .pq-join { display: flex; gap: 16px; margin: 0 0 6px; }
.sh-dlg .pq-h { display: block; margin: 8px 0 4px; font: 600 var(--text-2xs) "Segoe UI", system-ui, sans-serif; color: var(--ink-soft); }
.sh-dlg .pq-agg { display: flex; gap: 6px; align-items: center; margin: 0 0 6px; }
.sh-dlg .pq-agg input, .sh-dlg .pq-agg select { flex: 1; min-width: 0; min-height: 28px; padding: 3px 6px; border: 1px solid var(--rule-strong); border-radius: var(--radius-sm); background: var(--paper-raised); color: var(--ink); font: var(--text-xs) "Segoe UI", system-ui, sans-serif; }
.sh-dlg .pq-agg + .sh-b, .sh-dlg #pqAddBy, .sh-dlg #pqAddAgg { margin-bottom: 6px; }
.sh-dlg .pq-checks { max-height: 220px; overflow: auto; padding: 2px 6px; border: 1px solid var(--rule); border-radius: var(--radius-sm); }
.sh-dlg .pq-custom { display: grid; grid-template-columns: minmax(0, 1fr) 190px; gap: 16px; }
.sh-dlg .pq-custom textarea { min-height: 120px; font: var(--text-xs) ui-monospace, "Cascadia Mono", Consolas, monospace; }
.sh-dlg .pq-custom .sh-list { height: 190px; margin-bottom: 6px; }
.sh-dlg .pq-try { min-height: 1.4em; margin: 0; color: var(--ink-soft); }
.sh-dlg .pq-try.bad { color: var(--danger); }

/* Queries & Connections, beside the grid. */
.pq-pane .pq-card { padding: 8px 0 10px; border-bottom: 1px solid var(--rule); }
.pq-pane .pq-card p { margin: 2px 0 6px; color: var(--ink-soft); }
.pq-card-name { padding: 0; border: 0; background: none; color: var(--ink); font: 600 var(--text-xs) "Segoe UI", system-ui, sans-serif; text-align: left; cursor: pointer; }
.pq-card-name:hover { color: var(--sheet-green-dark); text-decoration: underline; }
.pq-card-acts { display: flex; flex-wrap: wrap; gap: 4px; }
.pq-card-acts .sh-b { min-width: 0; padding: 3px 10px; }
.pq-card-acts .sh-b:disabled { opacity: .45; cursor: default; }

@media (max-width: 900px) {
  dialog.pq { width: 100vw; max-width: 100vw; height: 100vh; height: 100dvh; border: 0; border-radius: 0; }
  .pq-main { grid-template-columns: minmax(0, 1fr); grid-template-rows: auto minmax(0, 1fr) auto; }
  .pq-queries { border-right: 0; border-bottom: 1px solid var(--rule); padding: 4px 8px; }
  .pq-queries h3 { display: none; }
  .pq-queries ul { display: flex; gap: 4px; overflow-x: auto; }
  .pq-queries li { flex: none; }
  .pq-queries li button { min-height: 44px; }
  .pq-settings { max-height: 34vh; border-left: 0; border-top: 1px solid var(--rule); }
  .pq-tb, .pq-tabs button { min-height: 44px; }
  .pq-step { min-height: 44px; }
  .pq-ico { width: 44px; height: 44px; }
  .sh-dlg .pq-custom { grid-template-columns: minmax(0, 1fr); }
}
