:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --surface-3: #e8ebf0;
  --border: #e1e4ea;
  --border-strong: #cdd2da;
  --text: #161a21;
  --text-2: #586170;
  --text-3: #8b93a1;
  --accent: #0d9488;
  --accent-hover: #0b7d73;
  --accent-soft: #e6f6f4;
  --danger: #d6336c;
  --danger-soft: #fdeef3;
  --ok: #2f9e44;
  --warn: #e8590c;
  --code-kw: #7c3aed;
  --code-fn: #0369a1;
  --code-str: #b45309;
  --code-num: #be185d;
  --code-com: #8b93a1;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 2px 8px rgba(16, 24, 40, .04);
  --mono: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --ui: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1116;
    --surface: #161b22;
    --surface-2: #1c222b;
    --surface-3: #252c37;
    --border: #2a313c;
    --border-strong: #3a4250;
    --text: #e6e9ee;
    --text-2: #a3abb8;
    --text-3: #6f7885;
    --accent: #2dd4bf;
    --accent-hover: #5eead4;
    --accent-soft: #0f2e2b;
    --danger: #f472b6;
    --danger-soft: #3a1626;
    --code-kw: #c4b5fd;
    --code-fn: #7dd3fc;
    --code-str: #fcd34d;
    --code-num: #f9a8d4;
    --code-com: #6f7885;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--text); font: 14px/1.5 var(--ui); -webkit-font-smoothing: antialiased; }
button, input, textarea { font: inherit; color: inherit; }
code, kbd { font-family: var(--mono); font-size: .88em; }
code { background: var(--surface-2); border-radius: 4px; padding: 1px 5px; }
kbd { border: 1px solid var(--border-strong); border-bottom-width: 2px; border-radius: 4px; padding: 0 5px; background: var(--surface); }
[hidden] { display: none !important; }
.muted { color: var(--text-2); }
.small { font-size: 12px; }
.spacer { flex: 1; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; gap: 7px; height: 34px; padding: 0 14px; border-radius: 8px;
  border: 1px solid var(--border-strong); background: var(--surface); cursor: pointer; font-weight: 500;
  text-decoration: none; white-space: nowrap; transition: background .12s, border-color .12s, opacity .12s;
}
a.btn { color: var(--text); }
.btn:hover:not(:disabled) { background: var(--surface-2); }
.btn:focus-visible, textarea:focus-visible, .dropzone:focus-within { outline: 2px solid var(--accent); outline-offset: 2px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn svg { width: 15px; height: 15px; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--accent-hover); border-color: var(--accent-hover); }
@media (prefers-color-scheme: dark) { .btn-primary { color: #04201d; } }
.btn-ghost { border-color: transparent; background: transparent; }
.btn-sm { height: 28px; padding: 0 10px; font-size: 13px; }

/* Top bar */
.topbar { height: 60px; display: flex; align-items: center; gap: 14px; padding: 0 20px; background: var(--surface); border-bottom: 1px solid var(--border); }
.brand { display: flex; align-items: center; gap: 11px; }
.brand b { display: block; font-size: 16px; letter-spacing: -.01em; }
.brand small { display: block; color: var(--text-3); font-size: 12px; margin-top: -2px; }
.logo { width: 34px; height: 34px; border-radius: 9px; display: grid; place-items: center; color: #fff;
  background: linear-gradient(135deg, #14b8a6, #0d9488 60%, #0f766e); box-shadow: 0 2px 6px rgba(13, 148, 136, .35); }
.logo svg { width: 20px; height: 20px; }
.engine-pill { display: inline-flex; align-items: center; gap: 7px; height: 28px; padding: 0 11px; border-radius: 999px;
  background: var(--surface-2); color: var(--text-2); font-size: 12px; font-weight: 500; }
.engine-pill .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--text-3); }
.engine-pill[data-state="up"] .dot { background: var(--ok); box-shadow: 0 0 0 3px rgba(47, 158, 68, .18); }
.engine-pill[data-state="down"] { color: var(--danger); }
.engine-pill[data-state="down"] .dot { background: var(--danger); }

/* Layout */
.layout { display: grid; grid-template-columns: 290px minmax(0, 1fr); height: calc(100vh - 60px); }
.sidebar { border-right: 1px solid var(--border); background: var(--surface); overflow-y: auto; padding: 18px 14px 24px; }
.sidebar section + section { margin-top: 22px; }
.sidebar h2 { font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .07em; color: var(--text-3); margin: 0 6px 8px; }
.main { display: flex; flex-direction: column; gap: 16px; padding: 18px; min-width: 0; overflow: hidden; }

/* Tables list */
.table-item { border-radius: 8px; }
.table-row { display: flex; align-items: center; gap: 8px; width: 100%; padding: 7px 8px; border: 0; background: none; border-radius: 8px; cursor: pointer; text-align: left; }
.table-row:hover { background: var(--surface-2); }
.table-row .chev { width: 14px; height: 14px; color: var(--text-3); transition: transform .15s; flex: none; }
.table-item.open .chev { transform: rotate(90deg); }
.table-row .tname { font-family: var(--mono); font-size: 13px; font-weight: 500; }
.table-row .meta { margin-left: auto; color: var(--text-3); font-size: 11.5px; white-space: nowrap; }
.badge { font-size: 10px; font-weight: 600; letter-spacing: .04em; text-transform: uppercase; padding: 1px 6px; border-radius: 4px; background: var(--surface-3); color: var(--text-2); }
.columns { list-style: none; margin: 2px 0 6px 22px; padding: 0 0 0 10px; border-left: 1px solid var(--border); }
.columns li button { display: flex; justify-content: space-between; gap: 10px; width: 100%; padding: 3px 8px; border: 0; background: none; border-radius: 6px; cursor: pointer; text-align: left; font-family: var(--mono); font-size: 12px; color: var(--text-2); }
.columns li button:hover { background: var(--surface-2); color: var(--text); }
.columns .ctype { color: var(--text-3); font-family: var(--ui); font-size: 11px; }
.table-actions { margin: 0 0 8px 32px; }

/* Upload */
.dropzone { display: flex; align-items: center; gap: 12px; padding: 14px; border: 1.5px dashed var(--border-strong); border-radius: 10px; cursor: pointer; color: var(--text-2); transition: border-color .12s, background .12s; }
.dropzone:hover, .dropzone.drag { border-color: var(--accent); background: var(--accent-soft); }
.dropzone svg { width: 26px; height: 26px; flex: none; color: var(--accent); }
.dropzone b { color: var(--text); font-weight: 500; }
.upload { border: 1px solid var(--border); border-radius: 10px; padding: 12px; background: var(--surface-2); }
.upload-info { display: flex; gap: 10px; align-items: center; min-width: 0; }
.upload-info b { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 500; max-width: 190px; }
.upload-info small { color: var(--text-2); }
.file-icon { flex: none; width: 38px; height: 38px; border-radius: 8px; display: grid; place-items: center; background: var(--accent-soft); color: var(--accent); font-size: 10px; font-weight: 700; letter-spacing: .04em; }
.upload-actions { display: flex; gap: 6px; margin-top: 10px; }
.help p { margin: 0 6px 8px; color: var(--text-2); font-size: 12.5px; }
.help code { font-size: 11px; line-height: 1.8; }

/* Cards */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); min-width: 0; }
.card-head { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.card-head h2 { font-size: 14px; margin: 0 4px; }
.kbd-hint { color: var(--text-3); font-size: 12px; }

.examples { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { height: 28px; padding: 0 11px; border-radius: 999px; border: 1px solid var(--border); background: var(--surface-2); color: var(--text-2); cursor: pointer; font-size: 12.5px; font-weight: 500; }
.chip:hover { color: var(--text); border-color: var(--border-strong); }
.chip.active { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }

/* Editor with syntax highlighting overlay */
.editor { position: relative; height: 210px; }
.editor textarea, .editor .highlight {
  position: absolute; inset: 0; margin: 0; padding: 14px 16px; border: 0; width: 100%; height: 100%;
  font: 13.5px/1.65 var(--mono); white-space: pre-wrap; overflow-wrap: anywhere; tab-size: 2; overflow: auto;
}
.editor textarea { resize: none; background: transparent; color: transparent; caret-color: var(--text); outline: none; z-index: 1; }
.editor textarea::selection { background: rgba(13, 148, 136, .25); color: transparent; }
.editor .highlight { color: var(--text); pointer-events: none; border-radius: 0 0 var(--radius) var(--radius); }
.tok-kw { color: var(--code-kw); font-weight: 500; }
.tok-fn { color: var(--code-fn); }
.tok-str { color: var(--code-str); }
.tok-num { color: var(--code-num); }
.tok-com { color: var(--code-com); font-style: italic; }
.tok-qid { color: var(--code-fn); }

/* Results */
.results-card { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.stats { color: var(--text-2); font-size: 12.5px; }
.stats b { color: var(--text); font-weight: 600; }
.results { flex: 1; overflow: auto; min-height: 0; border-radius: 0 0 var(--radius) var(--radius); }
.empty { height: 100%; min-height: 220px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px; color: var(--text-2); text-align: center; padding: 24px; }
.empty svg { width: 40px; height: 40px; color: var(--text-3); margin-bottom: 6px; }
.empty b { color: var(--text); font-weight: 600; }
table { border-collapse: separate; border-spacing: 0; width: 100%; font-size: 13px; }
thead th { position: sticky; top: 0; z-index: 1; background: var(--surface-2); text-align: left; font-weight: 600; padding: 8px 14px; border-bottom: 1px solid var(--border); white-space: nowrap; }
thead th small { display: block; font-weight: 400; color: var(--text-3); font-size: 11px; }
tbody td { padding: 6px 14px; border-bottom: 1px solid var(--border); white-space: nowrap; max-width: 380px; overflow: hidden; text-overflow: ellipsis; font-variant-numeric: tabular-nums; }
tbody tr:hover td { background: var(--surface-2); }
td.num, th.num { text-align: right; }
td.rownum, th.rownum { color: var(--text-3); text-align: right; width: 1%; padding-right: 8px; font-size: 12px; }
td .null { color: var(--text-3); font-style: italic; }
td .bool { font-family: var(--mono); font-size: 12px; }
.truncated { padding: 10px 14px; font-size: 12.5px; color: var(--text-2); background: var(--surface-2); border-top: 1px solid var(--border); }
.error { margin: 18px; padding: 14px 16px; border-radius: 10px; background: var(--danger-soft); color: var(--text); border: 1px solid color-mix(in srgb, var(--danger) 35%, transparent); }
.error b { color: var(--danger); display: block; margin-bottom: 4px; }
.error pre { margin: 0; white-space: pre-wrap; font: 13px/1.5 var(--mono); }
.loading { display: flex; align-items: center; justify-content: center; gap: 10px; height: 100%; min-height: 200px; color: var(--text-2); }
.spinner { width: 18px; height: 18px; border-radius: 50%; border: 2px solid var(--border-strong); border-top-color: var(--accent); animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

@media (max-width: 860px) {
  .layout { grid-template-columns: 1fr; height: auto; }
  /* query box first on phones; tables and upload follow */
  .sidebar { order: 1; border-right: 0; border-top: 1px solid var(--border); max-height: none; }
  .main { overflow: visible; padding: 14px; }
  .results { max-height: 70vh; }
  .brand small, .kbd-hint { display: none; }
  .topbar { padding: 0 14px; }
}

/* Touch screens: comfortable tap targets */
@media (pointer: coarse) {
  .btn-sm, .chip { height: 40px; padding: 0 14px; }
  .table-row { padding: 11px 8px; }
  .columns li button { padding: 9px 8px; }
  .examples { gap: 8px; }
  /* 16px stops iOS Safari zooming in on focus; both layers must stay the same size */
  .editor textarea, .editor .highlight { font-size: 16px; }
}
