/* ==========================================================================
   Base
   ========================================================================== */

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: 'Segoe UI', system-ui, -apple-system, Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  transition: background 0.15s ease, color 0.15s ease;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

h1 { font-size: 1.5rem; margin: 0; }
h2 { font-size: 1.15rem; margin: 1.5rem 0 0.75rem; }
h3 { font-size: 0.95rem; margin: 1rem 0 0.5rem; color: var(--text-muted); }

.muted { color: var(--text-muted); font-size: 0.85rem; }
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0;
}

/* ==========================================================================
   Layout
   ========================================================================== */

.navbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.6rem 1.25rem;
  background: var(--navbar-bg);
  color: var(--navbar-text);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 20;
}

.navbar-left, .navbar-right { display: flex; align-items: center; gap: 0.75rem; }
.brand { font-weight: 700; font-size: 1.05rem; color: var(--navbar-text); }
.brand:hover { text-decoration: none; }

.breadcrumb { display: flex; align-items: center; gap: 0.35rem; font-size: 0.9rem; }
.crumb-sep { color: var(--text-muted); }
.crumb-current { color: var(--text-muted); }

.navbar-user { font-size: 0.85rem; color: var(--text-muted); }

.page-container {
  max-width: 1280px;
  margin: 0 auto;
  padding: 1.5rem 1.25rem 3rem;
}

.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1.25rem;
  flex-wrap: wrap;
  gap: 0.75rem;
}
.page-header-actions { display: flex; gap: 0.5rem; }

/* ==========================================================================
   Buttons
   ========================================================================== */

.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 0.5rem 0.9rem;
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  background: var(--bg-elevated);
  color: var(--text);
  transition: background 0.15s ease, border-color 0.15s ease, transform 0.05s ease;
}
.btn:hover { background: var(--bg-hover); text-decoration: none; }
.btn:active { transform: translateY(1px); }
.btn-sm { padding: 0.32rem 0.6rem; font-size: 0.8rem; }
.btn-block { width: 100%; justify-content: center; }

.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--primary-contrast); }
.btn-primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); }

.btn-danger { background: transparent; border-color: var(--danger); color: var(--danger); }
.btn-danger:hover { background: var(--danger); color: #fff; }

.btn-secondary { background: var(--bg-hover); border-color: var(--border); color: var(--text); }

.btn-ghost { background: transparent; border-color: var(--border); color: var(--text); }

.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.inline-form { display: inline; }

/* ==========================================================================
   Cards
   ========================================================================== */

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 1rem;
}

.card {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1rem 1.1rem;
  box-shadow: var(--shadow);
}

.card-header { display: flex; align-items: center; justify-content: space-between; gap: 0.5rem; }
.card-header h3 { margin: 0; font-size: 1.05rem; }
.card-actions { display: flex; flex-wrap: wrap; gap: 0.4rem; margin-top: 0.85rem; }

.badge {
  font-size: 0.7rem;
  padding: 0.15rem 0.5rem;
  border-radius: 100px;
  background: var(--bg-hover);
  color: var(--text-muted);
  border: 1px solid var(--border);
  white-space: nowrap;
}
.badge-muted { opacity: 0.7; }

/* ==========================================================================
   Forms
   ========================================================================== */

label {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-bottom: 0.85rem;
}

.checkbox-label { flex-direction: row; align-items: center; }
.checkbox-label input { width: auto; }

input[type="text"], input[type="password"], input[type="number"],
input[type="search"], input[type="email"], select, textarea {
  font-family: inherit;
  font-size: 0.9rem;
  padding: 0.5rem 0.65rem;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--input-bg);
  color: var(--text);
}
input:focus, select:focus, textarea:focus {
  outline: 2px solid var(--primary);
  outline-offset: 1px;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 0 1rem;
}

/* ==========================================================================
   Login page
   ========================================================================== */

.login-body {
  margin: 0;
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  color: var(--text);
  font-family: 'Segoe UI', system-ui, sans-serif;
}
.login-card {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 2rem;
  width: 100%;
  max-width: 360px;
  box-shadow: var(--shadow);
}
.login-title { margin: 0 0 0.25rem; }
.login-subtitle { color: var(--text-muted); font-size: 0.85rem; margin: 0 0 1.25rem; }
.login-form { display: flex; flex-direction: column; }

/* ==========================================================================
   Alerts / toasts
   ========================================================================== */

.alert {
  padding: 0.65rem 0.9rem;
  border-radius: 8px;
  font-size: 0.85rem;
  margin-bottom: 1rem;
}
.alert-error { background: rgba(220, 38, 38, 0.12); color: var(--danger); border: 1px solid rgba(220, 38, 38, 0.3); }
.alert-success { background: rgba(22, 163, 74, 0.12); color: var(--success); border: 1px solid rgba(22, 163, 74, 0.3); }

.toast-root {
  position: fixed;
  top: 1rem;
  right: 1rem;
  z-index: 100;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}
.toast {
  padding: 0.65rem 1rem;
  border-radius: 8px;
  font-size: 0.85rem;
  box-shadow: var(--shadow);
  min-width: 220px;
  animation: toast-in 0.15s ease;
}
.toast-success { background: var(--success); color: #fff; }
.toast-error { background: var(--danger); color: #fff; }
@keyframes toast-in { from { opacity: 0; transform: translateY(-8px); } to { opacity: 1; transform: none; } }

/* ==========================================================================
   Tables
   ========================================================================== */

.table-wrapper {
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg-elevated);
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
  white-space: nowrap;
}

.data-table th, .data-table td {
  padding: 0.55rem 0.75rem;
  border-bottom: 1px solid var(--border);
  text-align: left;
}

.data-table thead th {
  background: var(--bg-hover);
  position: sticky;
  top: 0;
  cursor: pointer;
  user-select: none;
}
.data-table thead th.sortable:hover { color: var(--primary); }
.data-table thead th .sort-arrow { font-size: 0.7rem; margin-left: 0.25rem; opacity: 0.7; }

.data-table tbody tr:hover { background: var(--bg-hover); }
.data-table tbody tr.row-selected { background: rgba(37, 99, 235, 0.1); }

.actions-cell { display: flex; gap: 0.35rem; white-space: nowrap; }

.cell-editable { cursor: text; }
.cell-editing input {
  width: 100%;
  min-width: 80px;
}
.cell-null { color: var(--text-muted); font-style: italic; }

/* ==========================================================================
   Toolbar / pagination
   ========================================================================== */

.toolbar {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  margin-bottom: 1rem;
  flex-wrap: wrap;
}
.toolbar #search-input { flex: 1; min-width: 200px; }
.toolbar-info { margin-left: auto; font-size: 0.8rem; color: var(--text-muted); }

.pagination {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  margin-top: 1rem;
  flex-wrap: wrap;
}
.pagination button.active { background: var(--primary); color: var(--primary-contrast); border-color: var(--primary); }

/* ==========================================================================
   Modal
   ========================================================================== */

.modal { position: fixed; inset: 0; z-index: 50; display: flex; align-items: center; justify-content: center; }
.modal[hidden] { display: none; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,0.45); }
.modal-dialog {
  position: relative;
  background: var(--bg-elevated);
  border-radius: 12px;
  border: 1px solid var(--border);
  width: 100%;
  max-width: 480px;
  max-height: 88vh;
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow);
}
.modal-lg .modal-dialog { max-width: 720px; }
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
}
.modal-header h2 { margin: 0; }
.modal-close { background: none; border: none; font-size: 1.4rem; cursor: pointer; color: var(--text-muted); line-height: 1; }
.modal-body { padding: 1.1rem 1.25rem; overflow-y: auto; }
.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  padding: 0.85rem 1.25rem;
  border-top: 1px solid var(--border);
}

/* ==========================================================================
   Column builder (create table / add column)
   ========================================================================== */

.column-row {
  display: grid;
  grid-template-columns: 1.3fr 1fr 0.6fr 0.6fr 0.6fr 1fr auto;
  gap: 0.4rem;
  align-items: center;
  margin-bottom: 0.5rem;
}
.column-row input[type="text"], .column-row select { width: 100%; }
.column-row .col-flags { display: flex; gap: 0.3rem; align-items: center; font-size: 0.75rem; color: var(--text-muted); }

/* ==========================================================================
   Empty state
   ========================================================================== */

.empty-state {
  text-align: center;
  padding: 3rem 1rem;
  color: var(--text-muted);
}
.empty-state h1 { font-size: 3rem; margin-bottom: 0.5rem; }

/* ==========================================================================
   Theme switcher
   ========================================================================== */

.theme-switcher select {
  padding: 0.35rem 0.5rem;
  font-size: 0.8rem;
}

/* ==========================================================================
   SQL editor
   ========================================================================== */

#sql-textarea {
  width: 100%;
  font-family: 'Cascadia Code', 'Consolas', monospace;
  font-size: 0.9rem;
  background: var(--code-bg);
  resize: vertical;
}
.sql-toolbar {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin: 0.6rem 0 1rem;
}

.history-list { list-style: none; padding: 0; margin: 0; }
.history-item {
  padding: 0.5rem 0.75rem;
  border-radius: 6px;
  background: var(--code-bg);
  margin-bottom: 0.4rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  font-size: 0.8rem;
}
.history-item code { white-space: pre-wrap; word-break: break-all; }
.history-item:hover { background: var(--bg-hover); }
.history-error { border-left: 3px solid var(--danger); }
.history-date { color: var(--text-muted); white-space: nowrap; font-size: 0.72rem; }

.test-result { margin-top: 0.75rem; font-size: 0.85rem; }
.test-result.ok { color: var(--success); }
.test-result.fail { color: var(--danger); }
