/* SQL Workspace 3-pane layout, editor chrome, result grid, and the two
   signature visualizations: the byte waterfall and the row-group heatmap.

   The waterfall/heatmap rules also serve the Query Detail page, so this file
   is loaded globally rather than per-page. */

/* --- 3-pane frame -------------------------------------------------------- */

.ws {
  display: grid;
  grid-template-columns: 268px minmax(0, 1fr) minmax(300px, 340px);
  height: calc(100vh - var(--h-topbar));
  min-height: 0;
  overflow: hidden;
}

.ws__pane {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}

.ws__pane--left {
  background: var(--c-surface);
  border-right: 1px solid var(--c-border);
}

.ws__pane--center {
  background: var(--c-canvas);
}

.ws__pane--right {
  background: var(--c-surface);
  border-left: 1px solid var(--c-border);
}

.ws__head {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--sp-2);
  padding: var(--sp-3) var(--sp-4);
  border-bottom: 1px solid var(--c-border);
  background: var(--c-surface);
}

.ws__head h2,
.ws__title {
  margin: 0;
  font-size: var(--fs-xs);
  font-weight: var(--fw-semibold);
  text-transform: uppercase;
  letter-spacing: var(--track-caps);
  color: var(--c-text-mute);
}

.ws__scroll {
  flex: 1 1 auto;
  overflow-y: auto;
  overflow-x: hidden;
  min-height: 0;
}

/* --- Left pane: dataset explorer ---------------------------------------- */

.explorer {
  padding: var(--sp-3);
}

.explorer__section {
  margin-bottom: var(--sp-5);
}

.explorer__label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 var(--sp-2) var(--sp-2);
  font-size: 10px;
  font-weight: var(--fw-semibold);
  text-transform: uppercase;
  letter-spacing: var(--track-caps);
  color: var(--c-text-faint);
}

.ds-pick {
  display: block;
  padding: var(--sp-2) var(--sp-3);
  margin-bottom: 2px;
  border-radius: var(--r-sm);
  border: 1px solid transparent;
  color: var(--c-text-dim);
  text-decoration: none;
  font-size: var(--fs-md);
  transition: background var(--t-fast) var(--ease);
}

.ds-pick:hover {
  background: var(--c-panel-2);
  color: var(--c-text);
  text-decoration: none;
}

.ds-pick.is-active {
  background: var(--c-accent-wash);
  border-color: var(--c-accent-edge);
  color: var(--c-accent-hi);
}

.ds-pick__name {
  font-weight: var(--fw-medium);
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.ds-pick__meta {
  display: block;
  margin-top: 2px;
  font-family: var(--font-mono);
  font-size: 10px;
  color: var(--c-text-faint);
}

/* Schema column list. Clicking a column inserts its name in the editor. */
.cols {
  list-style: none;
  margin: 0;
  padding: 0;
}

.col-item {
  margin: 0;
}

.col-btn {
  width: 100%;
  display: flex;
  align-items: baseline;
  gap: var(--sp-2);
  padding: var(--sp-1) var(--sp-3);
  background: none;
  border: 0;
  border-radius: var(--r-xs);
  color: var(--c-text-dim);
  font-family: var(--font-mono);
  font-size: var(--fs-sm);
  text-align: left;
  cursor: pointer;
  transition: background var(--t-fast) var(--ease);
}

.col-btn:hover {
  background: var(--c-panel-2);
  border-color: transparent;
  color: var(--c-text);
}

.col-btn__name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.col-btn__type {
  margin-left: auto;
  font-size: 10px;
  color: var(--c-text-faint);
  flex: 0 0 auto;
}

.col-btn__dot {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--c-text-faint);
  flex: 0 0 auto;
  align-self: center;
}

.col-btn__dot--partition { background: var(--c-violet); }
.col-btn__dot--num { background: var(--c-info); }

/* Example query buttons. */
.example {
  width: 100%;
  display: block;
  text-align: left;
  padding: var(--sp-2) var(--sp-3);
  margin-bottom: var(--sp-2);
  background: var(--c-panel);
  border: 1px solid var(--c-border);
  border-radius: var(--r-sm);
  cursor: pointer;
  transition: border-color var(--t-fast) var(--ease),
              background var(--t-fast) var(--ease);
}

.example:hover {
  background: var(--c-panel-2);
  border-color: var(--c-accent-edge);
}

.example__title {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  font-size: var(--fs-sm);
  font-weight: var(--fw-medium);
  color: var(--c-text);
  white-space: normal;
}

.example__teaches {
  margin-top: var(--sp-1);
  font-size: var(--fs-xs);
  color: var(--c-text-faint);
  line-height: var(--lh-snug);
  white-space: normal;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.example__step {
  flex: 0 0 auto;
  display: inline-grid;
  place-items: center;
  width: 16px;
  height: 16px;
  border-radius: var(--r-xs);
  background: var(--c-accent-wash);
  color: var(--c-accent-hi);
  font-family: var(--font-mono);
  font-size: 10px;
  font-weight: var(--fw-bold);
}

/* --- Center pane: editor ------------------------------------------------- */

.editor {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  border-bottom: 1px solid var(--c-border);
  background: var(--c-surface);
}

.editor__bar {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  padding: var(--sp-2) var(--sp-3);
  border-bottom: 1px solid var(--c-border);
  flex-wrap: wrap;
}

.editor__bar .spacer { flex: 1 1 auto; }

.editor__dataset {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  font-size: var(--fs-sm);
  color: var(--c-text-mute);
}

.editor__dataset strong {
  font-family: var(--font-mono);
  color: var(--c-text);
  font-weight: var(--fw-medium);
}

.editor__body {
  position: relative;
  min-height: 190px;
  max-height: 44vh;
  overflow: hidden;
  display: flex;
}

.editor__body textarea {
  flex: 1 1 auto;
  border: 0;
  border-radius: 0;
  background: var(--c-surface);
  min-height: 190px;
  padding: var(--sp-3) var(--sp-4);
  resize: none;
}

.editor__body textarea:focus {
  box-shadow: none;
  border: 0;
}

.editor__hint {
  display: flex;
  align-items: center;
  gap: var(--sp-4);
  padding: var(--sp-2) var(--sp-4);
  font-size: var(--fs-xs);
  color: var(--c-text-faint);
  border-top: 1px solid var(--c-border-soft);
  flex-wrap: wrap;
}

/* CodeMirror 5 theming. Scoped so it cannot leak into other <pre> blocks. */
.editor .CodeMirror {
  flex: 1 1 auto;
  height: auto;
  min-height: 190px;
  max-height: 44vh;
  background: var(--c-surface);
  color: var(--c-text);
  font-family: var(--font-mono);
  font-size: var(--fs-md);
  line-height: 1.65;
}

.editor .CodeMirror-scroll {
  min-height: 190px;
  padding: var(--sp-2) 0;
}

.editor .CodeMirror-gutters {
  background: var(--c-surface);
  border-right: 1px solid var(--c-border-soft);
}

.editor .CodeMirror-linenumber {
  color: var(--c-text-faint);
  font-size: var(--fs-xs);
  padding: 0 var(--sp-3) 0 var(--sp-2);
}

.editor .CodeMirror-cursor {
  border-left: 2px solid var(--c-accent);
}

.editor .CodeMirror-selected { background: #27314a; }
.editor .CodeMirror-focused .CodeMirror-selected { background: #2d3a57; }
.editor .CodeMirror-activeline-background { background: rgba(255, 255, 255, 0.025); }
.editor .CodeMirror-matchingbracket {
  color: var(--c-accent-hi) !important;
  border-bottom: 1px solid var(--c-accent);
}

.editor .cm-keyword  { color: #ff7bb0; font-weight: var(--fw-medium); }
.editor .cm-operator { color: var(--c-text-dim); }
.editor .cm-variable,
.editor .cm-variable-2 { color: var(--c-text); }
.editor .cm-builtin  { color: var(--c-info); }
.editor .cm-atom     { color: var(--c-violet); }
.editor .cm-number   { color: #ffb86b; }
.editor .cm-string,
.editor .cm-string-2 { color: var(--c-good); }
.editor .cm-comment  { color: var(--c-text-faint); font-style: italic; }
.editor .cm-def      { color: var(--c-accent-hi); }
.editor .cm-punctuation { color: var(--c-text-mute); }

/* --- Center pane: results ----------------------------------------------- */

.results {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

.results__tabs {
  flex: 0 0 auto;
  padding: 0 var(--sp-3);
  background: var(--c-surface);
}

.results__scroll {
  flex: 1 1 auto;
  overflow-y: auto;
  min-height: 0;
  padding: var(--sp-5);
}

.results__status {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  padding: var(--sp-3) var(--sp-5);
  border-bottom: 1px solid var(--c-border);
  background: var(--c-surface);
  font-size: var(--fs-md);
  flex-wrap: wrap;
}

.results__status .spacer { flex: 1 1 auto; }

.run-meta {
  display: flex;
  gap: var(--sp-4);
  align-items: center;
  font-family: var(--font-mono);
  font-size: var(--fs-sm);
  color: var(--c-text-mute);
  flex-wrap: wrap;
}

.run-meta b {
  color: var(--c-text);
  font-weight: var(--fw-medium);
}

/* --- Result grid --------------------------------------------------------- */

.result-grid {
  border: 1px solid var(--c-border);
  border-radius: var(--r-md);
  overflow: hidden;
  background: var(--c-panel);
}

.result-grid__bar {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  padding: var(--sp-2) var(--sp-3);
  border-bottom: 1px solid var(--c-border);
  background: var(--c-panel-2);
  font-size: var(--fs-sm);
  color: var(--c-text-mute);
  flex-wrap: wrap;
}

.result-grid__bar .spacer { flex: 1 1 auto; }

.result-grid__scroll {
  overflow: auto;
  max-height: 460px;
}

.result-grid table {
  font-family: var(--font-mono);
  font-size: var(--fs-sm);
  border-collapse: separate;
  border-spacing: 0;
}

.result-grid th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--c-panel-2);
  border-bottom: 1px solid var(--c-border);
  font-family: var(--font-ui);
  padding: var(--sp-2) var(--sp-3);
}

.result-grid td {
  padding: var(--sp-2) var(--sp-3);
  white-space: nowrap;
  border-bottom: 1px solid var(--c-border-soft);
}

.result-grid .rownum {
  color: var(--c-text-faint);
  text-align: right;
  background: var(--c-panel);
  position: sticky;
  left: 0;
  user-select: none;
  font-size: var(--fs-xs);
  border-right: 1px solid var(--c-border-soft);
}

.result-grid th.rownum {
  z-index: 2;
  background: var(--c-panel-2);
}

.result-grid td.is-num { text-align: right; }

/* --- Right pane: execution insights ------------------------------------- */

.insights {
  padding: var(--sp-4);
}

.insights__block {
  margin-bottom: var(--sp-5);
}

.insights__block:last-child { margin-bottom: 0; }

.insights__label {
  font-size: 10px;
  font-weight: var(--fw-semibold);
  text-transform: uppercase;
  letter-spacing: var(--track-caps);
  color: var(--c-text-faint);
  margin-bottom: var(--sp-3);
}

.insights .callout {
  padding: var(--sp-4);
}

.insights .callout .big {
  font-size: var(--fs-3xl);
}

.insights .metrics {
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--sp-2);
}

.insights .metric {
  padding: var(--sp-3);
}

.insights .metric .v,
.insights .metric__value {
  font-size: var(--fs-lg);
}

.insights .notes li {
  font-size: var(--fs-sm);
  padding: var(--sp-2) 0 var(--sp-2) var(--sp-5);
  line-height: var(--lh-base);
}

.insights .notes li::before {
  width: 5px;
  height: 5px;
  left: var(--sp-2);
  top: calc(var(--sp-2) + 0.55em);
}

.insights .notes li:first-child::before { top: 0.55em; }

/* --- Byte waterfall ------------------------------------------------------ */
/* A full scan decomposed into what was skipped and why. The three middle
   bars plus the read bar sum to the total exactly. */

.fall {
  display: grid;
  grid-template-columns: minmax(130px, auto) minmax(60px, 1fr) minmax(76px, auto);
  gap: var(--sp-1) var(--sp-3);
  align-items: center;
  font-size: var(--fs-sm);
}

.fall .lbl {
  color: var(--c-text-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.fall .amt {
  text-align: right;
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  color: var(--c-text-dim);
}

.fall .track {
  background: var(--c-canvas);
  border: 1px solid var(--c-border);
  border-radius: var(--r-xs);
  height: 16px;
  overflow: hidden;
}

.fall .track > span {
  display: block;
  height: 100%;
  min-width: 2px;
  border-radius: 1px;
  transition: width var(--t-slow) var(--ease-out);
}

.fall .total > span {
  background: var(--viz-neutral);
}

.fall .saved > span {
  background: var(--viz-saved);
  background-image: repeating-linear-gradient(
    -45deg,
    transparent,
    transparent 4px,
    rgba(0, 0, 0, 0.18) 4px,
    rgba(0, 0, 0, 0.18) 8px
  );
}

.fall .read > span {
  background: var(--viz-read);
}

.fall .why {
  grid-column: 1 / -1;
  color: var(--c-text-faint);
  font-size: var(--fs-xs);
  line-height: var(--lh-snug);
  margin: -2px 0 var(--sp-3);
}

.fall .row-read {
  font-weight: var(--fw-semibold);
  color: var(--c-good);
}

.fall-note {
  margin-top: var(--sp-3);
  font-size: var(--fs-xs);
  color: var(--c-text-faint);
  line-height: var(--lh-base);
  max-width: 76ch;
}

/* The insights rail is narrow, so the waterfall stacks there. */
.insights .fall {
  grid-template-columns: minmax(0, 1fr) minmax(64px, auto);
  gap: var(--sp-1) var(--sp-2);
  font-size: var(--fs-xs);
}

.insights .fall .track {
  grid-column: 1 / -1;
  height: 10px;
  order: 1;
}

/* --- Row-group heatmap --------------------------------------------------- */
/* One tile per row-group: read vs proven irrelevant. */

.heat {
  display: flex;
  flex-wrap: wrap;
  gap: var(--sp-3) var(--sp-5);
}

.heat .file {
  min-width: 0;
}

.heat .fname {
  color: var(--c-text-faint);
  font-family: var(--font-mono);
  font-size: 10px;
  margin-bottom: var(--sp-1);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 180px;
}

.heat .tiles {
  display: flex;
  gap: 2px;
}

.heat i {
  width: 11px;
  height: 11px;
  border-radius: 2px;
  display: block;
  flex: 0 0 auto;
}

.heat i.read { background: var(--viz-read); }
.heat i.skip { background: var(--viz-skip); border: 1px solid var(--c-border); }

.heat .closed {
  padding: 1px var(--sp-2);
  border: 1px dashed var(--c-text-faint);
  border-radius: var(--r-xs);
  color: var(--c-text-faint);
  font-size: 10px;
  display: inline-block;
}

.insights .heat {
  gap: var(--sp-2) var(--sp-3);
}

.insights .heat .fname { max-width: 120px; }
.insights .heat i { width: 8px; height: 8px; }

/* --- Plan diagram -------------------------------------------------------- */

.plan {
  background: var(--c-surface);
  border: 1px solid var(--c-border);
  border-radius: var(--r-md);
  padding: var(--sp-5);
  overflow: auto;
}

.plan .mermaid {
  display: flex;
  justify-content: center;
  min-height: 60px;
}

.plan .mermaid svg {
  max-width: 100%;
  height: auto;
}

/* Pre-render the source is hidden so the raw graph text never flashes. */
.plan .mermaid:not([data-rendered]) {
  color: transparent;
  user-select: none;
}

.plan__fallback {
  margin-top: var(--sp-3);
}

.plan__fallback summary {
  cursor: pointer;
  color: var(--c-text-mute);
  font-size: var(--fs-sm);
  list-style: none;
  display: inline-flex;
  align-items: center;
  gap: var(--sp-2);
}

.plan__fallback summary::-webkit-details-marker { display: none; }

.plan__fallback summary::before {
  content: "▸";
  color: var(--c-text-faint);
  transition: transform var(--t-fast) var(--ease);
}

.plan__fallback[open] summary::before { content: "▾"; }

.plan__fallback summary:hover { color: var(--c-text); }

.plan__fallback pre {
  margin-top: var(--sp-3);
  font-size: var(--fs-sm);
}

/* --- Responsive ---------------------------------------------------------- */

@media (max-width: 1440px) {
  .ws { grid-template-columns: 240px minmax(0, 1fr) minmax(280px, 310px); }
}

@media (max-width: 1240px) {
  /* Insights move below the results rather than squeezing to nothing. */
  .ws {
    grid-template-columns: 230px minmax(0, 1fr);
    height: auto;
    overflow: visible;
  }

  .ws__pane--left {
    height: calc(100vh - var(--h-topbar));
    position: sticky;
    top: var(--h-topbar);
  }

  .ws__pane--right {
    grid-column: 1 / -1;
    border-left: 0;
    border-top: 1px solid var(--c-border);
  }

  .ws__pane--center { min-height: calc(100vh - var(--h-topbar)); }
  .ws__scroll { overflow: visible; }
  .insights .fall { grid-template-columns: minmax(130px, auto) minmax(60px, 1fr) minmax(76px, auto); }
  .insights .fall .track { grid-column: auto; height: 16px; order: 0; }
  .insights .metrics { grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); }
}

@media (max-width: 900px) {
  .ws {
    grid-template-columns: minmax(0, 1fr);
  }

  .ws__pane--left {
    height: auto;
    position: static;
    border-right: 0;
    border-bottom: 1px solid var(--c-border);
    order: 2;
  }

  .ws__pane--center { order: 1; min-height: 0; }
  .ws__pane--right { order: 3; }

  .editor__body,
  .editor .CodeMirror,
  .editor .CodeMirror-scroll { max-height: none; }

  .results__scroll { padding: var(--sp-4); }
}
