/* The SQL playground (sql.html); colors come from style.css's palette. */
body.sql-page { overflow: auto; height: auto; min-height: 100%; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

.sql-head { padding: 14px 20px 10px; border-bottom: 1px solid var(--border); background: var(--surface-1); }
.sql-head h1 { margin: 2px 0 4px; font-size: 20px; }
.sql-head p { margin: 0; max-width: 72ch; }
.sql-head a { color: inherit; }
.sql-head .back { font-size: 13px; color: var(--ink-2); text-decoration: none; }
.sql-head .back:hover { color: var(--accent); }

.sql-main { display: grid; grid-template-columns: 260px minmax(0, 1fr); gap: 20px; padding: 16px 20px 32px; }
.sql-side h2 {
  font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.8px;
  color: var(--ink-2); margin: 0 0 8px;
}
.sql-side h2:not(:first-child) { margin-top: 18px; }
.sql-table { margin-bottom: 10px; }
.sql-table summary { cursor: pointer; font-weight: 600; font-size: 13px; }
.sql-table summary .muted { font-weight: 400; }
.sql-table ul { list-style: none; margin: 4px 0 0; padding: 0 0 0 14px; font-size: 12px; }
.sql-table li { display: flex; justify-content: space-between; gap: 8px; padding: 1px 0; }
.sql-table li code { font-size: 12px; }
.sql-table li .type { color: var(--muted); font-size: 11px; }
#examples { list-style: none; margin: 0; padding: 0; }
#examples button {
  font: inherit; font-size: 13px; color: var(--ink-2); background: none; border: 0;
  padding: 3px 0; cursor: pointer; text-align: left;
}
#examples button:hover { color: var(--accent); }

#editor { min-width: 0; border: 1px solid var(--border); background: var(--surface-1); }
#editor:focus-within { border-color: var(--accent); }
#editor .cm-editor { color: var(--ink); background: var(--surface-1); font-size: 14px; }
#editor .cm-editor.cm-focused { outline: none; }
#editor .cm-scroller { min-height: 240px; max-height: 55vh; overflow: auto;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; line-height: 1.65; }
#editor .cm-content { padding: 12px 0; caret-color: var(--ink); }
#editor .cm-line { padding: 0 14px 0 8px; }
#editor .cm-gutters { background: var(--surface-1); color: var(--muted); border-color: var(--border); }
#editor .cm-activeLine, #editor .cm-activeLineGutter { background: color-mix(in srgb, var(--accent) 7%, transparent); }
#editor .cm-cursor { border-left-color: var(--ink); }
#editor .cm-selectionBackground { background: color-mix(in srgb, var(--accent) 25%, transparent); }
#editor .cm-tooltip, #editor .cm-panels { color: var(--ink); background: var(--surface-1); border-color: var(--border); }
#editor .cm-tooltip-autocomplete { font: 12px/1.5 ui-monospace, Menlo, monospace; max-width: min(600px, 90vw); }
#editor .cm-tooltip-autocomplete ul li[aria-selected] { background: var(--accent); color: white; }
#editor .cm-completionDetail { margin-left: 16px; color: inherit; opacity: .8; }
#editor .cm-diagnostic { font: 12px/1.5 system-ui, sans-serif; white-space: pre-wrap; max-width: min(600px, 85vw); }
#editor .cm-search { font-size: 12px; display: flex; gap: 6px; flex-wrap: wrap; padding: 8px; }
#editor .cm-search input, #editor .cm-search button { color: var(--ink); background: var(--page); border: 1px solid var(--border); }
.sql-editor-head, .sql-editor-foot { display: flex; align-items: center; justify-content: space-between;
  gap: 12px; color: var(--ink-2); font-size: 12px; padding: 8px 12px; border: 1px solid var(--border); }
.sql-editor-head { border-bottom: 0; border-radius: 8px 8px 0 0; background: var(--surface-1); }
.sql-editor-foot { border-top: 0; border-radius: 0 0 8px 8px; font-size: 11px; }
#sql-cursor { white-space: nowrap; font-variant-numeric: tabular-nums; }
#sql-validation[data-state="valid"] { color: var(--s14); }
#sql-validation[data-state="error"] { color: var(--s8); }
.sql-work { min-width: 0; }
.sql-run-hint { color: var(--muted); font-size: 11px; }
.sql-help { font-size: 12px; color: var(--ink-2); margin: -4px 0 12px; }
.sql-help summary { cursor: pointer; width: fit-content; }
.sql-help p { max-width: 85ch; margin: 6px 0; }
.sql-bar { display: flex; align-items: center; gap: 10px; margin: 8px 0 12px; flex-wrap: wrap; }
.sql-bar button {
  font: inherit; font-size: 13px; cursor: pointer; padding: 5px 16px; border-radius: 8px;
  border: 1px solid var(--accent); background: var(--accent); color: #fff;
}
.sql-bar button:disabled { opacity: 0.5; cursor: default; }
.sql-bar #check, .sql-bar #format { background: none; color: var(--ink); border-color: var(--border); }
.sql-bar #stop { background: none; color: var(--ink); border-color: var(--border); }
#sql-status { font-size: 12px; color: var(--ink-2); }
#sql-error {
  white-space: pre-wrap; font: 12px/1.5 ui-monospace, Menlo, monospace; color: var(--s8);
  border: 1px solid var(--border); border-radius: 8px; padding: 8px 12px; margin-bottom: 12px;
}
.sql-results { overflow: auto; max-height: 65vh; border: 1px solid var(--border); border-radius: 8px; }
.sql-results:empty { display: none; }
.sql-results table { border-collapse: collapse; font-size: 12px; min-width: 100%; }
.sql-results th, .sql-results td {
  padding: 4px 10px; border-bottom: 1px solid var(--grid); text-align: left;
  white-space: nowrap; max-width: 420px; overflow: hidden; text-overflow: ellipsis;
}
.sql-results th { position: sticky; top: 0; background: var(--surface-1); font-weight: 600; }
.sql-results td.num { text-align: right; font-variant-numeric: tabular-nums; }
.sql-results td.null { color: var(--muted); font-style: italic; }

.query-results { border: 1px solid var(--border); border-radius: 10px; background: var(--surface-1); overflow: hidden; }
.query-results [hidden] { display: none !important; }
.result-heading { display: flex; align-items: center; gap: 16px; justify-content: space-between;
  padding: 0 16px; border-bottom: 1px solid var(--border); }
.result-tabs { display: flex; gap: 20px; }
.result-tabs button { font: inherit; font-size: 13px; padding: 12px 0 10px; cursor: pointer;
  color: var(--ink-2); background: none; border: 0; border-bottom: 2px solid transparent; }
.result-tabs button[aria-selected="true"] { color: var(--ink); border-bottom-color: var(--accent); font-weight: 600; }
.result-tabs button:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }
.result-source { color: var(--muted); font-size: 11px; text-align: right; }
.query-results .sql-results { border: 0; border-radius: 0; }
.chart-loading { margin: 0; color: var(--ink-2); font-size: 13px; }
.chart-loading:not(:empty) { padding: 32px 20px; }
.chart-heading { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 18px 20px 10px; }
.chart-title-label { flex: 1; min-width: 0; }
.chart-title-label input { display: block; width: 100%; font: 600 17px/1.5 system-ui, sans-serif;
  color: var(--ink); background: none; border: 1px solid transparent; border-radius: 4px; padding: 3px 4px; }
.chart-title-label input:hover { border-color: var(--border); }
.chart-title-label input:focus { outline: 0; border-color: var(--accent); }
.chart-heading button { font: inherit; font-size: 12px; color: white; background: var(--accent);
  border: 1px solid var(--accent); border-radius: 7px; padding: 7px 12px; cursor: pointer; white-space: nowrap; }
.chart-heading button:disabled { opacity: .45; cursor: default; }
.chart-layout { display: grid; grid-template-columns: minmax(0, 1fr) 205px; padding: 8px 20px 4px; gap: 20px; }
.chart-preview, .chart-plot { min-width: 0; width: 100%; }
.chart-empty { display: grid; align-content: center; text-align: center; min-height: 340px;
  margin: 0; padding: 20px 32px; color: var(--ink-2); font-size: 14px; line-height: 1.6; }
.chart-controls { display: grid; gap: 14px; align-content: start; border-left: 1px solid var(--border); padding-left: 18px; }
.chart-controls label { display: flex; flex-direction: column; gap: 5px; font-size: 11px; color: var(--ink-2); min-width: 0; }
.chart-controls select { width: 100%; min-width: 0; font: inherit; font-size: 12px; color: var(--ink);
  border: 1px solid var(--border); border-radius: 6px; background: var(--surface-1); padding: 7px 6px; }
.chart-controls select:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.chart-controls select:disabled { opacity: .45; }
.chart-reset { justify-self: start; cursor: pointer; padding: 0; font: inherit; font-size: 11px;
  color: var(--ink-2); background: none; border: 0; text-decoration: underline; text-underline-offset: 3px; }
.chart-settings-help { font-size: 11px; color: var(--muted); line-height: 1.5; margin: 0; }
.chart-note { margin: 12px 20px 16px; padding-top: 12px; border-top: 1px solid var(--border);
  font-size: 11px; color: var(--ink-2); line-height: 1.6; }
.chart-status { color: var(--ink-2); font-size: 12px; margin: 0 20px; }
.chart-status:not(:empty) { margin-bottom: 16px; }

@media (max-width: 760px) {
  .sql-main { grid-template-columns: 1fr; padding: 12px; }
  .sql-head { padding: 12px; }
  .sql-side { order: 2; }
  .sql-editor-foot { align-items: flex-start; }
  #editor .cm-scroller { min-height: 210px; max-height: 50vh; }
  .result-heading { gap: 10px; padding: 0 12px; }
  .chart-heading { padding: 12px 10px 6px; gap: 8px; }
  .chart-title-label input { font-size: 15px; }
  .chart-heading button { padding: 7px 9px; font-size: 11px; }
  .chart-layout { grid-template-columns: 1fr; padding: 6px 10px; gap: 12px; }
  .chart-controls { grid-template-columns: repeat(2, minmax(0, 1fr)); border-left: 0;
    border-top: 1px solid var(--border); padding: 14px 2px 4px; gap: 12px; }
  .chart-settings-help { grid-column: 1 / -1; }
  .chart-note { margin: 10px 12px 14px; }
}

/* ?embed (sql.html): the editor, one row of examples, and results */
.embed-only { display: none; }
.embed .embed-only { display: inline; margin-left: 10px; color: var(--ink-2); font-size: 11px; }
.embed .sql-head, .embed #schema, .embed .sql-side h2, .embed .sql-help,
.embed .sql-editor-foot, .embed #check, .embed .sql-run-hint { display: none; }
/* Fill the frame exactly: the editor keeps its height and the results take
   the rest, scrolling inside, so the framing page can give it a fixed box */
.embed body.sql-page { height: 100dvh; overflow: hidden; }
.embed .sql-main { display: flex; flex-direction: column; gap: 10px; padding: 10px; height: 100%; box-sizing: border-box; }
.embed .sql-work { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.embed .sql-work > * { flex: none; }
.embed #query-results:not([hidden]), .embed #table-panel:not([hidden]) { display: flex; flex-direction: column; flex: 1; min-height: 0; }
.embed #chart-panel:not([hidden]) { flex: 1; min-height: 0; overflow: auto; }
.embed .result-tabs button { padding: 8px 0 6px; }
.embed .sql-side { order: 0; }
.embed #examples { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; }
.embed #examples button {
  white-space: nowrap; font-size: 12px; padding: 4px 10px;
  border: 1px solid var(--border); border-radius: 999px; background: var(--surface-1);
}
.embed #examples button:hover { border-color: var(--accent); }
.embed .sql-editor-head { padding: 6px 10px; }
.embed #editor { border-radius: 0 0 8px 8px; }
.embed #editor .cm-scroller { min-height: 120px; max-height: 180px; font-size: 13px; }
.embed .sql-bar { margin: 8px 0; }
.embed .sql-results { flex: 1; min-height: 0; max-height: none; }
/* Phone-sized frames: a shorter editor and one row of buttons, so results
   still get most of the space */
@media (max-width: 480px) {
  .embed .sql-main { gap: 8px; padding: 8px; }
  .embed #editor .cm-scroller { min-height: 92px; max-height: 92px; font-size: 12px; line-height: 1.5; }
  .embed #editor .cm-content { padding: 6px 0; }
  .embed #format, .embed #sql-validation { display: none; }
  .embed .sql-bar { margin: 6px 0; flex-wrap: nowrap; }
  .embed .result-heading { padding: 0 10px; }
}
