:root {
  --bg: #0f1419;
  --bg-elev: #151b23;
  --bg-panel: #1a212b;
  --bg-hover: #222b37;
  --border: #2a3441;
  --border-strong: #3a4656;
  --text: #d8dee6;
  --text-dim: #8b96a5;
  --text-faint: #5d6877;
  --accent: #3b82f6;
  --accent-hover: #5b9bff;
  --ok: #3fb950;
  --warn: #d29922;
  --danger: #f05252;
  --null: #6e7681;
  --num: #79c0ff;
  --radius: 6px;
  --font-ui: "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-mono: "Cascadia Code", "JetBrains Mono", Consolas, "Courier New", monospace;
  --editor-height: 260px;
  color-scheme: dark;
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 13px/1.45 var(--font-ui);
  -webkit-font-smoothing: antialiased;
}

button, input, select, textarea { font: inherit; color: inherit; }
kbd {
  font: 11px var(--font-mono);
  padding: 1px 5px;
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.14);
  margin-left: 6px;
}
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.muted { color: var(--text-dim); }
.mono { font-family: var(--font-mono); font-size: 12px; }
.spacer { flex: 1; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

/* ---------- Buttons & inputs ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 30px;
  padding: 0 12px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--bg-panel);
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.12s, border-color 0.12s;
}
.btn:hover:not(:disabled) { background: var(--bg-hover); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn.primary:hover:not(:disabled) { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn.danger:not(:disabled) { border-color: rgba(240, 82, 82, 0.5); color: #ff8a8a; }
.btn.danger:hover:not(:disabled) { background: rgba(240, 82, 82, 0.12); }
.btn.ghost { background: transparent; }
.btn.small { height: 26px; padding: 0 9px; font-size: 12px; }
.btn.block { width: 100%; justify-content: center; height: 36px; }

.link {
  border: 0;
  background: none;
  color: var(--text-faint);
  cursor: pointer;
  font-size: 11px;
  padding: 0;
  text-transform: none;
  letter-spacing: 0;
}
.link:hover { color: var(--text); }

select, input:not([type="checkbox"]) {
  height: 30px;
  padding: 0 8px;
  background: var(--bg);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
}
select:disabled, input:disabled { opacity: 0.5; }

.icon-play, .icon-stop { display: inline-block; width: 10px; height: 10px; }
.icon-play { background: currentColor; clip-path: polygon(0 0, 100% 50%, 0 100%); }
.icon-stop { background: currentColor; border-radius: 1px; }

/* ---------- Brand ---------- */
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 14px; letter-spacing: 0.2px; }
.brand-mark {
  width: 20px;
  height: 20px;
  border-radius: 5px;
  background:
    linear-gradient(#fff, #fff) 5px 6px / 10px 2px no-repeat,
    linear-gradient(#fff, #fff) 5px 9px / 10px 2px no-repeat,
    linear-gradient(#fff, #fff) 5px 12px / 6px 2px no-repeat,
    var(--accent);
}
.brand-lg { font-size: 20px; }
.brand-lg .brand-mark { width: 28px; height: 28px; background-size: 14px 2.5px, 14px 2.5px, 9px 2.5px, auto; background-position: 7px 8px, 7px 13px, 7px 18px, 0 0; }

/* ---------- Login ---------- */
.login {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 16px;
  background: radial-gradient(1000px 500px at 50% -10%, rgba(59, 130, 246, 0.14), transparent 70%), var(--bg);
}
.login-card {
  width: 100%;
  max-width: 360px;
  display: flex;
  flex-direction: column;
  padding: 28px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg-elev);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.4);
}
.login-sub { margin: 6px 0 22px; color: var(--text-dim); }
.login-card label { margin: 10px 0 5px; color: var(--text-dim); font-size: 12px; }
.login-card input { height: 36px; }
.login-error { min-height: 20px; margin: 10px 0 6px; color: var(--danger); font-size: 12px; }

/* ---------- App shell ---------- */
.app { height: 100%; display: flex; flex-direction: column; }

.topbar {
  display: flex;
  align-items: center;
  gap: 14px;
  height: 46px;
  padding: 0 14px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-elev);
  flex-shrink: 0;
}
.agent-picker { display: flex; align-items: center; gap: 8px; margin-left: 12px; min-width: 0; }
.agent-picker select { max-width: 320px; }
.user-name { color: var(--text-dim); max-width: 180px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--text-faint); flex-shrink: 0; }
.dot.ok { background: var(--ok); box-shadow: 0 0 0 3px rgba(63, 185, 80, 0.18); }
.dot.busy { background: var(--warn); box-shadow: 0 0 0 3px rgba(210, 153, 34, 0.18); }
.dot.err { background: var(--danger); }

.pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 22px;
  padding: 0 9px;
  border-radius: 11px;
  font-size: 11px;
  border: 1px solid var(--border-strong);
  color: var(--text-dim);
}
.pill::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.pill.connected { color: var(--ok); border-color: rgba(63, 185, 80, 0.35); }
.pill.connecting, .pill.reconnecting { color: var(--warn); border-color: rgba(210, 153, 34, 0.35); }
.pill.disconnected { color: var(--danger); border-color: rgba(240, 82, 82, 0.35); }

.workspace { flex: 1; display: flex; min-height: 0; }

/* ---------- Sidebar ---------- */
.sidebar {
  width: 260px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--border);
  background: var(--bg-elev);
  min-height: 0;
}
.side-section { padding: 12px 10px 8px; display: flex; flex-direction: column; min-height: 0; }
.side-section + .side-section { border-top: 1px solid var(--border); }
.side-section.grow { flex: 1; }
.side-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 4px 8px;
  font-size: 11px;
  font-weight: 600;
  color: var(--text-faint);
  text-transform: uppercase;
  letter-spacing: 0.6px;
}
.count { font-variant-numeric: tabular-nums; }

.agent-list, .history-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; }
.agent-list { max-height: 220px; }
.history-list { flex: 1; }

.agent-item, .history-item {
  display: flex;
  gap: 8px;
  padding: 7px 6px;
  border-radius: var(--radius);
  cursor: pointer;
}
.agent-item:hover, .history-item:hover { background: var(--bg-hover); }
.agent-item.selected { background: rgba(59, 130, 246, 0.14); }
.agent-item .dot { margin-top: 5px; }
.item-body { min-width: 0; flex: 1; }
.item-title { display: flex; align-items: center; gap: 6px; font-weight: 600; }
.item-title span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item-sub { color: var(--text-faint); font-size: 11px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge {
  flex-shrink: 0;
  font-size: 10px;
  font-weight: 600;
  padding: 0 5px;
  border-radius: 4px;
  background: var(--bg-hover);
  color: var(--text-dim);
}
.side-empty { padding: 6px 4px; color: var(--text-faint); font-size: 12px; }

.history-item .dot { margin-top: 5px; }
.history-sql {
  font: 12px/1.4 var(--font-mono);
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ---------- Main ---------- */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; min-height: 0; }

.toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.field { display: inline-flex; align-items: center; gap: 6px; margin-left: 8px; color: var(--text-dim); }
.field select { height: 28px; }

.editor {
  position: relative;
  display: flex;
  height: var(--editor-height);
  min-height: 100px;
  flex-shrink: 0;
  background: var(--bg);
}
.gutter {
  margin: 0;
  padding: 10px 10px 10px 0;
  width: 48px;
  flex-shrink: 0;
  text-align: right;
  color: var(--text-faint);
  font: 13px/20px var(--font-mono);
  overflow: hidden;
  user-select: none;
  border-right: 1px solid var(--border);
  background: var(--bg-elev);
}
#sql {
  flex: 1;
  margin: 0;
  padding: 10px 12px;
  border: 0;
  outline: none;
  resize: none;
  background: transparent;
  color: #e6edf3;
  font: 13px/20px var(--font-mono);
  tab-size: 2;
  white-space: pre;
  overflow: auto;
}
#sql::placeholder { color: var(--text-faint); }
.editor.running::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 2px;
  background: linear-gradient(90deg, transparent, var(--accent), transparent);
  background-size: 40% 100%;
  background-repeat: no-repeat;
  animation: scan 1.1s linear infinite;
}
@keyframes scan { from { background-position: -40% 0; } to { background-position: 140% 0; } }

.splitter {
  height: 6px;
  flex-shrink: 0;
  cursor: row-resize;
  border-top: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  background: var(--bg-elev);
}
.splitter:hover, .splitter.dragging { background: rgba(59, 130, 246, 0.35); }

/* ---------- Results ---------- */
.results { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.results-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 12px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.results-title { font-weight: 600; }
.filter { height: 26px !important; width: 200px; font-size: 12px; }
.results-body { flex: 1; overflow: auto; min-height: 0; position: relative; }

.grid { border-collapse: separate; border-spacing: 0; font: 12px/1.4 var(--font-mono); min-width: 100%; }
.grid th, .grid td {
  padding: 5px 10px;
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
  max-width: 420px;
  overflow: hidden;
  text-overflow: ellipsis;
  text-align: left;
}
.grid thead th {
  position: sticky;
  top: 0;
  z-index: 2;
  background: var(--bg-panel);
  font-family: var(--font-ui);
  font-weight: 600;
  cursor: pointer;
  user-select: none;
}
.grid thead th:hover { background: var(--bg-hover); }
.grid th .col-type { display: block; font-weight: 400; font-size: 10px; color: var(--text-faint); }
.grid th.sorted-asc .col-name::after { content: " ▲"; font-size: 9px; color: var(--accent); }
.grid th.sorted-desc .col-name::after { content: " ▼"; font-size: 9px; color: var(--accent); }
.grid .rownum {
  position: sticky;
  left: 0;
  z-index: 1;
  text-align: right;
  color: var(--text-faint);
  background: var(--bg-elev);
  min-width: 48px;
}
.grid thead .rownum { z-index: 3; cursor: default; }
.grid tbody tr:hover td { background: var(--bg-hover); }
.grid tbody tr:hover .rownum { background: var(--bg-hover); }
.grid td.num { text-align: right; color: var(--num); }
.grid td.null { color: var(--null); font-style: italic; }

.empty {
  height: 100%;
  display: grid;
  place-items: center;
  text-align: center;
  color: var(--text-faint);
  padding: 24px;
}
.empty strong { display: block; color: var(--text-dim); font-size: 14px; margin-bottom: 4px; }
.empty code { font-family: var(--font-mono); color: var(--text-dim); }

.message {
  margin: 14px;
  padding: 12px 14px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  border-left-width: 3px;
  background: var(--bg-panel);
}
.message strong { display: block; margin-bottom: 4px; }
.message pre { margin: 0; white-space: pre-wrap; word-break: break-word; font: 12px/1.5 var(--font-mono); color: var(--text); }
.message.error { border-left-color: var(--danger); }
.message.error strong { color: #ff8a8a; }
.message.warn { border-left-color: var(--warn); }
.message.warn strong { color: var(--warn); }
.message.info { border-left-color: var(--accent); }

.truncated { color: var(--warn); }

.statusbar {
  display: flex;
  align-items: center;
  gap: 10px;
  height: 26px;
  padding: 0 12px;
  border-top: 1px solid var(--border);
  background: var(--bg-elev);
  font-size: 12px;
  flex-shrink: 0;
}
.statusbar .muted { font-size: 11px; }

/* ---------- Responsive ---------- */
@media (max-width: 900px) {
  .sidebar { display: none; }
  .user-name, .statusbar .muted { display: none; }
  .agent-picker { margin-left: 0; }
  .agent-picker select { max-width: 160px; }
  .filter { width: 120px; }
  kbd { display: none; }
}
