/* ── Reset & base ─────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --sidebar-w: 220px;
  --tab-h: 42px;

  /* Colors */
  --sidebar-bg:   #16171d;
  --sidebar-text: #c4c6d0;
  --sidebar-muted:#5a5c6a;
  --sidebar-hover:#1e2029;
  --sidebar-active-bg: rgba(59,130,246,0.12);
  --sidebar-active-text: #93c5fd;

  --main-bg:  #ffffff;
  --panel-bg: #fafafa;
  --border:   #e5e7eb;

  --text:        #111827;
  --text-muted:  #6b7280;
  --text-xs:     #9ca3af;

  --blue:        #3b82f6;
  --blue-hover:  #2563eb;
  --blue-light:  #eff6ff;
  --red:         #ef4444;
  --green:       #10b981;

  --radius:   6px;
  --radius-sm: 4px;
  --shadow:   0 1px 3px rgba(0,0,0,0.08), 0 1px 2px rgba(0,0,0,0.06);
  --shadow-md: 0 4px 16px rgba(0,0,0,0.12), 0 2px 6px rgba(0,0,0,0.08);

  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', sans-serif;
  --mono: 'SF Mono', 'Fira Code', 'Consolas', monospace;
}

html, body { height: 100%; }

body {
  font-family: var(--font);
  font-size: 14px;
  color: var(--text);
  background: var(--main-bg);
  overflow: hidden;
}

/* ── App layout ────────────────────────────────────────────── */
.app {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  height: 100vh;
}

/* ── Sidebar ───────────────────────────────────────────────── */
.sidebar {
  background: var(--sidebar-bg);
  color: var(--sidebar-text);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  border-right: 1px solid rgba(255,255,255,0.05);
}

.sidebar-header {
  padding: 16px 14px 12px;
  border-bottom: 1px solid rgba(255,255,255,0.06);
  flex-shrink: 0;
}

.app-logo {
  display: flex;
  flex-direction: column;
  line-height: 1.2;
}

.logo-main {
  font-size: 15px;
  font-weight: 700;
  color: #fff;
  letter-spacing: 0.3px;
}

.logo-sub {
  font-size: 11px;
  font-weight: 500;
  color: var(--blue);
  letter-spacing: 0.5px;
  text-transform: uppercase;
}

.sidebar-section {
  padding: 10px 10px 12px;
  border-bottom: 1px solid rgba(255,255,255,0.05);
  flex-shrink: 0;
}

.sidebar-section--grow {
  flex: 1;
  overflow-y: auto;
  min-height: 0;
}

.section-label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--sidebar-muted);
  margin-bottom: 7px;
  padding: 0 2px;
}

/* URL display */
.url-display {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--sidebar-text);
  word-break: break-all;
  line-height: 1.4;
  padding: 2px 2px;
  cursor: pointer;
  border-radius: var(--radius-sm);
}

.url-display:hover { color: #fff; }
.url-display.not-set { color: var(--sidebar-muted); font-style: italic; font-family: var(--font); font-size: 12px; }

.url-editor { display: flex; flex-direction: column; gap: 6px; }
.url-editor input {
  width: 100%;
  padding: 6px 8px;
  font-size: 12px;
  font-family: var(--mono);
  background: rgba(255,255,255,0.07);
  border: 1px solid rgba(255,255,255,0.12);
  border-radius: var(--radius-sm);
  color: #fff;
  outline: none;
}
.url-editor input:focus { border-color: var(--blue); }
.url-editor-actions { display: flex; gap: 5px; justify-content: flex-end; }

/* Sidebar list */
.sidebar-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.list-empty {
  font-size: 12px;
  color: var(--sidebar-muted);
  font-style: italic;
  padding: 3px 6px;
}

.sidebar-item {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 6px 6px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 12.5px;
  color: var(--sidebar-text);
  transition: background 0.1s;
  position: relative;
}

.sidebar-item:hover { background: var(--sidebar-hover); }

.sidebar-item.active {
  background: var(--sidebar-active-bg);
  color: var(--sidebar-active-text);
}

.item-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--sidebar-muted);
  flex-shrink: 0;
}
.sidebar-item.active .item-dot { background: var(--blue); }

.item-name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.item-actions {
  display: none;
  gap: 1px;
  flex-shrink: 0;
}
.sidebar-item:hover .item-actions { display: flex; }

.item-action-btn {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--sidebar-muted);
  font-size: 11px;
  padding: 2px 4px;
  border-radius: 3px;
  line-height: 1;
}
.item-action-btn:hover { color: var(--sidebar-text); background: rgba(255,255,255,0.08); }
.item-action-btn.del:hover { color: #fca5a5; }

/* ── Main area ─────────────────────────────────────────────── */
.main {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--main-bg);
}

/* ── Empty state ───────────────────────────────────────────── */
.empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  color: var(--text-muted);
  padding: 40px;
  text-align: center;
}

.empty-state-icon {
  font-size: 36px;
  opacity: 0.25;
  line-height: 1;
}

.empty-state h2 { font-size: 18px; color: var(--text); font-weight: 600; }
.empty-state p  { font-size: 13px; max-width: 320px; line-height: 1.5; }

/* ── Query builder ─────────────────────────────────────────── */
.query-builder {
  display: flex;
  flex-direction: column;
  height: 100%;
  overflow: hidden;
}

/* Tab bar */
.tab-bar {
  display: flex;
  align-items: center;
  height: var(--tab-h);
  padding: 0 16px;
  border-bottom: 1px solid var(--border);
  background: var(--panel-bg);
  gap: 0;
  flex-shrink: 0;
}

.tab {
  padding: 0 14px;
  height: 100%;
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-muted);
  transition: color 0.15s, border-color 0.15s;
  white-space: nowrap;
}

.tab:hover { color: var(--text); }
.tab.active { color: var(--blue); border-bottom-color: var(--blue); }
.tab-spacer { flex: 1; }

.active-project-badge {
  font-size: 11px;
  color: var(--text-muted);
  background: var(--panel-bg);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 2px 10px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 200px;
}

/* Workspace split */
.workspace {
  display: grid;
  grid-template-columns: 380px 1fr;
  flex: 1;
  overflow: hidden;
  min-height: 0;
}

/* Form panel */
.form-panel {
  border-right: 1px solid var(--border);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.form-scroll {
  overflow-y: auto;
  flex: 1;
  padding: 16px 20px 24px;
  display: flex;
  flex-direction: column;
  gap: 0;
}

/* Field rows */
.field-row {
  display: grid;
  grid-template-columns: 100px 1fr;
  gap: 8px;
  align-items: start;
  padding: 6px 0;
  border-bottom: 1px solid transparent;
}

.field-label {
  font-size: 12.5px;
  font-weight: 500;
  color: var(--text-muted);
  padding-top: 7px;
  line-height: 1.3;
}

.required { color: var(--red); margin-left: 2px; }

.field-control {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}

.field-control--inline {
  flex-direction: row;
  align-items: center;
  gap: 6px;
}

/* Select wrap */
.select-wrap {
  position: relative;
}

.select-wrap select {
  width: 100%;
  padding: 6px 28px 6px 8px;
  font-size: 13px;
  font-family: var(--font);
  background: #fff;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  outline: none;
  cursor: pointer;
  appearance: none;
  -webkit-appearance: none;
}

.select-wrap::after {
  content: '▾';
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
  pointer-events: none;
  color: var(--text-muted);
  font-size: 11px;
}

.select-wrap select:focus {
  border-color: var(--blue);
  box-shadow: 0 0 0 2px rgba(59,130,246,0.12);
}

/* Text inputs */
.text-input, .custom-text {
  width: 100%;
  padding: 6px 8px;
  font-size: 13px;
  font-family: var(--font);
  background: #fff;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  outline: none;
}

.text-input:focus, .custom-text:focus {
  border-color: var(--blue);
  box-shadow: 0 0 0 2px rgba(59,130,246,0.12);
}

.custom-text {
  font-family: var(--mono);
  font-size: 12px;
}

/* Checkbox */
.checkbox-label {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 13px;
  color: var(--text);
  cursor: pointer;
  padding: 6px 0;
}

.checkbox-label input[type="checkbox"] {
  width: 15px;
  height: 15px;
  cursor: pointer;
  accent-color: var(--blue);
  flex-shrink: 0;
}

/* Date custom wrap (text + datetime-local) */
.custom-date-wrap {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.custom-date-input {
  width: 100%;
  padding: 6px 8px;
  font-size: 12px;
  font-family: var(--font);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  outline: none;
  background: #fff;
}

.custom-date-input:focus {
  border-color: var(--blue);
  box-shadow: 0 0 0 2px rgba(59,130,246,0.12);
}

/* Filters section */
.filters-section {
  margin-top: 10px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
}

.filters-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.filters-title {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.filters-container {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

/* Filter row */
.filter-row {
  display: grid;
  grid-template-columns: 1fr 110px 1fr 24px;
  gap: 5px;
  align-items: start;
}

.filter-row > div:first-child {
  display: flex;
  flex-direction: column;
}

.filter-row .select-wrap select {
  font-size: 12px;
  padding: 5px 24px 5px 7px;
}

.filter-row .text-input {
  font-size: 12px;
  padding: 5px 7px;
}

.filter-remove {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-xs);
  font-size: 13px;
  padding: 4px;
  border-radius: var(--radius-sm);
  line-height: 1;
  text-align: center;
}

.filter-remove:hover { color: var(--red); background: #fef2f2; }

/* Form actions */
.form-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 20px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
}

/* ── Results panel ─────────────────────────────────────────── */
.results-panel {
  overflow: hidden;
  display: flex;
  flex-direction: column;
  background: var(--panel-bg);
}

/* Result view tabs */
.results-tab-bar {
  display: flex;
  padding: 0 16px;
  border-bottom: 1px solid var(--border);
  background: var(--main-bg);
  flex-shrink: 0;
}

.result-tab {
  padding: 0 14px;
  height: 36px;
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  cursor: pointer;
  font-size: 12.5px;
  font-weight: 500;
  color: var(--text-muted);
  transition: color 0.15s, border-color 0.15s;
  margin-bottom: -1px;
}

.result-tab:hover:not(:disabled) { color: var(--text); }
.result-tab.active { color: var(--blue); border-bottom-color: var(--blue); }
.result-tab:disabled { opacity: 0.35; cursor: not-allowed; }

.results-scroll {
  overflow-y: auto;
  flex: 1;
  padding: 16px 20px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.results-meta {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.results-url {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--text-muted);
  background: #fff;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 6px 10px;
  word-break: break-all;
  line-height: 1.5;
}

.results-url:empty { display: none; }

.results-status {
  font-size: 12px;
  font-weight: 500;
}

.results-status.ok    { color: var(--green); }
.results-status.err   { color: var(--red); }
.results-status.loading { color: var(--text-muted); }

.results-container {
  flex: 1;
  min-height: 0;
}

.results-placeholder {
  font-size: 13px;
  color: var(--text-xs);
  font-style: italic;
  padding: 16px 0;
}

.results-error {
  background: #fef2f2;
  border: 1px solid #fecaca;
  border-radius: var(--radius);
  padding: 12px 14px;
  font-size: 13px;
  color: var(--red);
  line-height: 1.5;
  white-space: pre-wrap;
  font-family: var(--mono);
}

/* highlight.js wrapper */
.hljs-wrap {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  overflow: auto;
  max-height: calc(100vh - 200px);
}

.hljs-wrap pre {
  margin: 0;
}

.hljs-wrap code {
  font-family: var(--mono);
  font-size: 12px;
  line-height: 1.6;
}

/* ── Data table ────────────────────────────────────────────── */
.table-wrap {
  overflow: auto;
  max-height: calc(100vh - 230px);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #fff;
}

.data-table {
  border-collapse: collapse;
  width: 100%;
  min-width: max-content;
  font-size: 12.5px;
}

.data-table th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--panel-bg);
  padding: 8px 14px;
  text-align: left;
  font-size: 11.5px;
  font-weight: 600;
  color: var(--text-muted);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

.data-table td {
  padding: 7px 14px;
  border-bottom: 1px solid #f0f0f0;
  color: var(--text);
  white-space: nowrap;
}

.data-table tbody tr:last-child td { border-bottom: none; }
.data-table tbody tr:hover td { background: var(--blue-light); }

.data-table td.cell-time {
  font-family: var(--mono);
  font-size: 11.5px;
  color: var(--text-muted);
}

.data-table td.cell-num {
  font-family: var(--mono);
  text-align: right;
}

.table-no-data {
  padding: 16px;
  font-size: 13px;
  color: var(--text-muted);
  font-style: italic;
}

.table-count-note {
  font-size: 12.5px;
  color: var(--text-muted);
  background: var(--blue-light);
  border: 1px solid #bfdbfe;
  border-radius: var(--radius-sm);
  padding: 6px 10px;
  margin-bottom: 8px;
}

.table-count-note strong {
  color: var(--blue);
  font-weight: 600;
}

/* ── Buttons ───────────────────────────────────────────────── */
.btn {
  padding: 7px 16px;
  font-size: 13px;
  font-weight: 500;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  cursor: pointer;
  transition: background 0.12s, border-color 0.12s;
  white-space: nowrap;
}

.btn-primary {
  background: var(--blue);
  color: #fff;
  border-color: var(--blue);
}
.btn-primary:hover { background: var(--blue-hover); border-color: var(--blue-hover); }

.btn-ghost {
  background: transparent;
  color: var(--text-muted);
  border-color: var(--border);
}
.btn-ghost:hover { background: var(--panel-bg); color: var(--text); }

.btn-xs {
  padding: 4px 10px;
  font-size: 11.5px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  cursor: pointer;
  font-weight: 500;
}

.btn-xs.btn-primary { background: var(--blue); color: #fff; border-color: var(--blue); }
.btn-xs.btn-primary:hover { background: var(--blue-hover); }

.btn-xs.btn-ghost {
  background: transparent;
  color: var(--sidebar-muted);
  border-color: rgba(255,255,255,0.12);
  color: var(--sidebar-text);
}
.btn-xs.btn-ghost:hover { background: rgba(255,255,255,0.08); }

/* Icon buttons */
.icon-btn {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--sidebar-muted);
  font-size: 13px;
  padding: 2px 5px;
  border-radius: 3px;
  line-height: 1;
}
.icon-btn:hover { color: var(--sidebar-text); background: rgba(255,255,255,0.08); }

/* ── Modals ────────────────────────────────────────────────── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  backdrop-filter: blur(2px);
}

.modal {
  background: #fff;
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  width: 380px;
  max-width: calc(100vw - 32px);
  overflow: hidden;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
}

.modal-header h3 {
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
}

.modal-header .icon-btn {
  color: var(--text-muted);
}
.modal-header .icon-btn:hover { color: var(--text); background: var(--panel-bg); }

.modal-body {
  padding: 18px 18px 12px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.modal-body .field-row {
  grid-template-columns: 90px 1fr;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 12px 18px 16px;
}

/* ── Utilities ─────────────────────────────────────────────── */
.hidden { display: none !important; }

/* Loading spinner (CSS only) */
.spinner {
  display: inline-block;
  width: 14px;
  height: 14px;
  border: 2px solid var(--border);
  border-top-color: var(--blue);
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
  vertical-align: middle;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* Scrollbar */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: #d1d5db; }
