*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

/* ── CSP-safe visibility helpers ── */
.hidden { display: none !important; }
.loading-placeholder { color: var(--muted); font-style: italic; }
.load-error { color: #991b1b; }
.sql-label { margin-bottom: 0; }

:root {
  --green: #1a3a6b;
  --green-light: #e8edf7;
  --green-dark: #0f2548;
  --bg: #f4f6f8;
  --surface: #ffffff;
  --border: #e2e6ea;
  --text: #1a1f2e;
  --muted: #6b7280;
  --sidebar-w: 230px;
  --banner-h: 48px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* ── Banner ── */
.banner {
  background: var(--green);
  color: #fff;
  text-align: center;
  padding: 0 24px;
  height: var(--banner-h);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.9rem;
  font-weight: 500;
  flex-shrink: 0;
  gap: 8px;
}
.banner strong { font-weight: 700; }
.banner-link {
  background: none;
  border: none;
  padding: 0;
  font: inherit;
  color: inherit;
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 2px;
}

/* ── Page layout ── */
.page {
  display: flex;
  flex: 1;
  overflow: hidden;
  height: calc(100vh - var(--banner-h));
}

/* ── Sidebar ── */
.sidebar {
  width: var(--sidebar-w);
  min-width: 160px;
  max-width: 480px;
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
}

/* Drag handle */
.sidebar-resize {
  position: absolute;
  top: 0;
  right: 0;
  width: 4px;
  height: 100%;
  cursor: col-resize;
  z-index: 10;
}
.sidebar-resize:hover,
.sidebar-resize.dragging { background: var(--green); opacity: 0.5; }

.sidebar-header {
  height: 48px;
  padding: 0 14px;
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--muted);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  display: flex;
  align-items: center;
}

.table-list {
  overflow-y: auto;
  flex: 1;
}

/* "All tables" item */
.table-list-all {
  height: 40px;
  padding: 0 14px;
  font-size: 0.83rem;
  cursor: pointer;
  color: var(--text);
  border-left: 3px solid transparent;
  display: flex;
  align-items: center;
  transition: background 0.15s, border-color 0.15s;
}
.table-list-all:hover { background: var(--green-light); }
.table-list-all.active {
  background: var(--green-light);
  border-left-color: var(--green);
  color: var(--green-dark);
  font-weight: 600;
}

/* Schema folder header */
.schema-header {
  padding: 10px 14px 4px;
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 5px;
  user-select: none;
  cursor: pointer;
}
.schema-header:hover { color: var(--text); }
.schema-caret {
  font-size: 0.6rem;
  transition: transform 0.15s;
  display: inline-block;
}
.schema-header.collapsed .schema-caret { transform: rotate(-90deg); }

/* Table item under a schema */
.table-list-item {
  padding: 7px 14px 7px 26px;
  font-size: 0.82rem;
  cursor: pointer;
  color: var(--text);
  border-left: 3px solid transparent;
  word-break: break-all;
  transition: background 0.15s, border-color 0.15s;
}
.table-list-item:hover { background: var(--green-light); }
.table-list-item.active {
  background: var(--green-light);
  border-left-color: var(--green);
  color: var(--green-dark);
  font-weight: 600;
}
.schema-group.collapsed .table-list-item { display: none; }

/* ── Main area ── */
.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.toolbar {
  height: 48px;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.toolbar h1 {
  font-size: 1rem;
  font-weight: 700;
  color: var(--text);
  flex-shrink: 0;
}

.search-wrap {
  position: relative;
  flex: 1;
  max-width: 340px;
  margin-left: auto;
}

.search-icon {
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--muted);
  font-size: 0.85rem;
  pointer-events: none;
}

#col-filter {
  width: 100%;
  padding: 8px 12px 8px 32px;
  border: 1.5px solid var(--border);
  border-radius: 8px;
  font-size: 0.875rem;
  color: var(--text);
  outline: none;
  transition: border-color 0.2s;
  background: var(--bg);
}
#col-filter:focus { border-color: var(--green); background: #fff; }
#col-filter::placeholder { color: #adb5bd; }

.clear-filter {
  background: none;
  border: 1.5px solid var(--border);
  border-radius: 6px;
  padding: 6px 10px;
  font-size: 0.8rem;
  color: var(--muted);
  cursor: pointer;
  display: none;
  transition: border-color 0.2s;
  flex-shrink: 0;
}
.clear-filter:hover { border-color: var(--green); color: var(--green); }
.clear-filter.visible { display: block; }

.refresh-catalog {
  background: none;
  border: 1.5px solid var(--border);
  border-radius: 6px;
  padding: 6px 10px;
  font-size: 0.8rem;
  color: var(--muted);
  cursor: pointer;
  flex-shrink: 0;
  transition: border-color 0.2s, color 0.2s;
}
.refresh-catalog:hover:not(:disabled) { border-color: var(--green); color: var(--green); }
.refresh-catalog:disabled { opacity: 0.5; cursor: not-allowed; }

/* ── Catalog table wrapper ── */
.table-wrap {
  flex: 1;
  overflow: auto;
  padding: 0 20px 20px;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.83rem;
}

thead th {
  position: sticky;
  top: 0;
  height: 40px;
  background: #f8f9fb;
  border-bottom: 2px solid var(--border);
  border-top: 1px solid var(--border);
  padding: 0 12px;
  text-align: left;
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  white-space: nowrap;
  z-index: 1;
}

/* Column width control */
th:nth-child(1) { width: 140px; min-width: 120px; }  /* Table */
th:nth-child(2) { width: 240px; min-width: 180px; }  /* Table Description */
th:nth-child(3) { min-width: 140px; }                 /* Column */

tbody td {
  padding: 9px 12px;
  border-bottom: 1px solid #f0f2f4;
  vertical-align: top;
  line-height: 1.45;
}

/* Table group header row */
tr.row-table-header td {
  border-top: 2px solid var(--border);
  background: #f8f9fb;
  padding-top: 12px;
  padding-bottom: 12px;
}

td.cell-table-name {
  font-weight: 700;
  font-size: 0.82rem;
  color: var(--green-dark);
  white-space: nowrap;
}

td.cell-table-desc {
  font-size: 0.81rem;
  color: #4b5563;
  max-width: 240px;
  white-space: normal;
  word-wrap: break-word;
  vertical-align: top;
}

td.cell-col-name {
  font-family: "SF Mono", "Fira Code", monospace;
  font-size: 0.8rem;
  color: #1a1a2e;
}

td.cell-type {
  font-family: "SF Mono", "Fira Code", monospace;
  font-size: 0.75rem;
  color: #6366f1;
  white-space: nowrap;
}

td.cell-col-desc {
  color: #4b5563;
  max-width: 220px;
}

td.cell-pk {
  text-align: center;
  font-family: "SF Mono", "Fira Code", monospace;
  font-size: 0.8rem;
  color: var(--muted);
}
td.cell-pk.is-pk { color: var(--green-dark); font-weight: 700; }

td.cell-fk {
  font-family: "SF Mono", "Fira Code", monospace;
  font-size: 0.75rem;
  color: var(--muted);
}

.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: var(--muted);
}

.loading-state {
  text-align: center;
  padding: 60px 20px;
  color: var(--muted);
}

.spinner-sm {
  display: inline-block;
  width: 18px; height: 18px;
  border: 2px solid #e2e6ea;
  border-top-color: var(--green);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  vertical-align: middle;
  margin-right: 8px;
}

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

/* ── Floating chat pill ── */
.chat-fab {
  position: fixed;
  bottom: 28px;
  right: 28px;
  height: 48px;
  padding: 0 20px 0 16px;
  background: var(--green);
  border-radius: 24px;
  display: flex;
  align-items: center;
  gap: 9px;
  cursor: pointer;
  box-shadow: 0 4px 18px rgba(26,58,107,0.45);
  border: none;
  transition: transform 0.2s, box-shadow 0.2s;
  z-index: 100;
  color: #fff;
  font-size: 0.875rem;
  font-weight: 600;
  font-family: inherit;
  white-space: nowrap;
}
.chat-fab:hover {
  transform: scale(1.04);
  box-shadow: 0 6px 24px rgba(26,58,107,0.55);
}
.chat-fab svg { flex-shrink: 0; }

/* ── Chat modal ── */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.35);
  display: none;
  align-items: flex-end;
  justify-content: flex-end;
  padding: 24px 24px calc(var(--banner-h) + 80px) 24px;
  z-index: 200;
}
.modal-backdrop.open { display: flex; }

.modal {
  background: var(--surface);
  border-radius: 16px;
  box-shadow: 0 8px 40px rgba(0,0,0,0.18);
  width: 480px;
  max-width: 100%;
  display: flex;
  flex-direction: column;
  max-height: calc(100vh - 120px);
  overflow: hidden;
  animation: slide-up 0.22s ease;
}

@keyframes slide-up {
  from { transform: translateY(24px); opacity: 0; }
  to   { transform: translateY(0);    opacity: 1; }
}

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

.modal-title {
  display: flex;
  align-items: center;
  gap: 8px;
}

.modal-title .logo-dot {
  width: 10px; height: 10px;
  background: var(--green);
  border-radius: 50%;
}

.modal-title h2 {
  font-size: 1rem;
  font-weight: 700;
  color: var(--text);
}

.modal-title span {
  font-size: 0.75rem;
  color: var(--muted);
  font-weight: 400;
}

.modal-close {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--muted);
  font-size: 1.2rem;
  line-height: 1;
  padding: 4px;
  border-radius: 4px;
}
.modal-close:hover { color: var(--text); background: var(--bg); }

.modal-body {
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  overflow-y: auto;
  flex: 1;
}

.mq-label {
  font-size: 0.75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  margin-bottom: 6px;
}

#mq-input {
  width: 100%;
  height: 90px;
  border: 1.5px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 0.9rem;
  resize: none;
  color: var(--text);
  outline: none;
  transition: border-color 0.2s;
  font-family: inherit;
}
#mq-input:focus { border-color: var(--green); }

#mq-submit {
  width: 100%;
  padding: 11px;
  background: var(--green);
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.2s;
  font-family: inherit;
}
#mq-submit:hover:not(:disabled) { opacity: 0.88; }
#mq-submit:disabled { opacity: 0.5; cursor: not-allowed; }

.mq-result { display: flex; flex-direction: column; gap: 10px; }

.sql-block-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

#mq-copy {
  background: none;
  border: 1.5px solid var(--border);
  color: var(--muted);
  padding: 3px 10px;
  font-size: 0.75rem;
  border-radius: 6px;
  cursor: pointer;
  font-family: inherit;
}
#mq-copy:hover { border-color: var(--green); color: var(--green); }

pre.sql-pre {
  background: #1e1e2e;
  color: #cdd6f4;
  border-radius: 8px;
  padding: 14px;
  overflow-x: auto;
  font-size: 0.8rem;
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
  font-family: "SF Mono", "Fira Code", monospace;
  max-height: 200px;
  overflow-y: auto;
}

.redash-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px;
  background: var(--green-light);
  border: 1.5px solid #a0b4d6;
  border-radius: 8px;
  color: var(--green-dark);
  font-weight: 600;
  font-size: 0.875rem;
  text-decoration: none;
  transition: background 0.15s;
}
.redash-btn:hover { background: #d0ddf0; }

.auto-del-note {
  font-size: 0.75rem;
  color: #92400e;
  background: #fffbeb;
  border: 1px solid #fcd34d;
  border-radius: 6px;
  padding: 7px 10px;
}

.mq-error {
  padding: 10px 12px;
  background: #fef2f2;
  border: 1.5px solid #fca5a5;
  border-radius: 8px;
  color: #991b1b;
  font-size: 0.85rem;
}

.spinner-mq {
  display: inline-block;
  width: 14px; height: 14px;
  border: 2px solid rgba(255,255,255,0.4);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  vertical-align: middle;
  margin-right: 6px;
}

/* scrollbar */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #ced4da; border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: #adb5bd; }
