/* ══════════════════════════════════════════════
   PG Dashboard — Premium Dark Theme
   ══════════════════════════════════════════════ */

:root {
  /* Palette */
  --bg-base: #0d1117;
  --bg-surface: #161b22;
  --bg-elevated: #1c2333;
  --bg-hover: #21283b;
  --bg-active: #263354;

  --border: #30363d;
  --border-light: #21262d;

  --text-primary: #e6edf3;
  --text-secondary: #8b949e;
  --text-muted: #546178;

  --accent: #58a6ff;
  --accent-hover: #79c0ff;
  --accent-muted: rgba(88, 166, 255, .15);
  --accent-gradient: linear-gradient(135deg, #58a6ff 0%, #a371f7 100%);

  --success: #3fb950;
  --success-bg: rgba(63, 185, 80, .12);

  --danger: #f85149;
  --danger-bg: rgba(248, 81, 73, .12);

  --warning: #d29922;
  --warning-bg: rgba(210, 153, 34, .12);

  /* Layout */
  --sidebar-w: 280px;
  --topbar-h: 56px;
  --radius: 8px;
  --radius-lg: 12px;

  /* Transitions */
  --ease: cubic-bezier(.4,0,.2,1);
}

/* ── Reset ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  background: var(--bg-base);
  color: var(--text-primary);
  overflow: hidden;
}

body {
  display: flex;
}

/* ── Scrollbar ── */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 4px;
}
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

/* ══════════════════════════════════════════════
   SIDEBAR
   ══════════════════════════════════════════════ */
#sidebar {
  width: var(--sidebar-w);
  min-width: var(--sidebar-w);
  height: 100vh;
  display: flex;
  flex-direction: column;
  background: var(--bg-surface);
  border-right: 1px solid var(--border);
  transition: transform .3s var(--ease), opacity .3s var(--ease);
  z-index: 20;
}

#sidebar.collapsed {
  transform: translateX(-100%);
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border-light);
}

.logo {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 700;
  font-size: 16px;
  background: var(--accent-gradient);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.logo svg { color: var(--accent); }

/* Connection dot */
.status-dot {
  width: 10px; height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
  transition: background .3s;
}
.status-dot.connected { background: var(--success); box-shadow: 0 0 8px var(--success); }
.status-dot.disconnected { background: var(--danger); box-shadow: 0 0 8px var(--danger); }

/* Search */
.sidebar-search {
  padding: 12px 16px;
}
.sidebar-search input {
  width: 100%;
  padding: 8px 12px;
  background: var(--bg-base);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-size: 13px;
  outline: none;
  transition: border-color .2s;
}
.sidebar-search input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-muted);
}
.sidebar-search input::placeholder { color: var(--text-muted); }

/* Table list */
.table-list {
  flex: 1;
  overflow-y: auto;
  padding: 4px 8px;
}

.table-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 9px 12px;
  margin: 2px 0;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 13px;
  color: var(--text-secondary);
  transition: all .15s var(--ease);
  user-select: none;
}
.table-item:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}
.table-item.active {
  background: var(--accent-muted);
  color: var(--accent);
  font-weight: 500;
}
.table-item .table-name {
  display: flex;
  align-items: center;
  gap: 8px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.table-item .table-name svg {
  flex-shrink: 0;
  opacity: .5;
}
.table-item .row-count {
  font-size: 11px;
  color: var(--text-muted);
  background: var(--bg-base);
  padding: 2px 7px;
  border-radius: 10px;
  flex-shrink: 0;
}
.table-item.active .row-count {
  background: rgba(88,166,255,.12);
  color: var(--accent);
}

.schema-group {
  padding: 6px 12px 4px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: var(--text-muted);
  margin-top: 8px;
}

.loading-placeholder {
  text-align: center;
  padding: 40px 16px;
  color: var(--text-muted);
  font-size: 13px;
}

.sidebar-footer {
  padding: 12px 16px;
  border-top: 1px solid var(--border-light);
}
.sidebar-footer button {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 8px;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-secondary);
  font-size: 13px;
  cursor: pointer;
  transition: all .2s;
}
.sidebar-footer button:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
  border-color: var(--text-muted);
}

/* ══════════════════════════════════════════════
   MAIN
   ══════════════════════════════════════════════ */
#main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
}

/* ── Topbar ── */
#topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: var(--topbar-h);
  padding: 0 24px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-surface);
  flex-shrink: 0;
  gap: 16px;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}
.topbar-left h1 {
  font-size: 16px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-shrink: 0;
}

.badge {
  font-size: 11px;
  color: var(--text-muted);
  background: var(--bg-elevated);
  padding: 3px 10px;
  border-radius: 10px;
  white-space: nowrap;
}

/* Search box */
.search-box {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--bg-base);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 6px 12px;
  transition: border-color .2s;
}
.search-box:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-muted);
}
.search-box svg { color: var(--text-muted); flex-shrink: 0; }
.search-box input {
  border: none;
  background: none;
  color: var(--text-primary);
  font-size: 13px;
  outline: none;
  width: 200px;
}
.search-box input::placeholder { color: var(--text-muted); }

/* Buttons */
.icon-btn {
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 6px;
  border-radius: var(--radius);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all .15s;
  font-size: 20px;
  line-height: 1;
}
.icon-btn:hover { background: var(--bg-hover); color: var(--text-primary); }

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 16px;
  border: none;
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all .2s var(--ease);
  white-space: nowrap;
}
.btn-primary {
  background: var(--accent);
  color: #fff;
}
.btn-primary:hover { background: var(--accent-hover); transform: translateY(-1px); box-shadow: 0 4px 12px rgba(88,166,255,.3); }
.btn-ghost {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-secondary);
}
.btn-ghost:hover { background: var(--bg-hover); color: var(--text-primary); }
.btn-danger {
  background: var(--danger);
  color: #fff;
}
.btn-danger:hover { background: #e5433b; }
.btn-sm { padding: 4px 10px; font-size: 12px; }

/* ── Empty State ── */
.empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  color: var(--text-muted);
}
.empty-state h2 { font-size: 20px; font-weight: 600; color: var(--text-secondary); }
.empty-state p { font-size: 14px; }

/* ── Data Container ── */
.data-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.table-wrapper {
  flex: 1;
  overflow: auto;
}

/* ── Data Table ── */
#data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

#data-table thead {
  position: sticky;
  top: 0;
  z-index: 5;
}

#data-table th {
  background: var(--bg-elevated);
  padding: 10px 14px;
  text-align: left;
  font-weight: 600;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--text-secondary);
  border-bottom: 2px solid var(--border);
  white-space: nowrap;
  cursor: pointer;
  user-select: none;
  transition: color .15s;
  position: relative;
}
#data-table th:hover { color: var(--accent); }

#data-table th .sort-arrow {
  display: inline-block;
  margin-left: 4px;
  opacity: .3;
  font-size: 11px;
  transition: opacity .15s;
}
#data-table th.sorted-asc .sort-arrow,
#data-table th.sorted-desc .sort-arrow {
  opacity: 1;
  color: var(--accent);
}

#data-table th.col-actions {
  cursor: default;
  text-align: center;
  width: 90px;
}

#data-table td {
  padding: 8px 14px;
  border-bottom: 1px solid var(--border-light);
  max-width: 300px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  transition: background .1s;
}

#data-table tr { transition: background .1s; }
#data-table tbody tr:hover { background: var(--bg-hover); }
#data-table tbody tr.editing { background: var(--accent-muted); }

/* Inline edit */
#data-table td input.cell-edit {
  width: 100%;
  padding: 4px 8px;
  background: var(--bg-base);
  border: 1px solid var(--accent);
  border-radius: 4px;
  color: var(--text-primary);
  font-size: 13px;
  font-family: inherit;
  outline: none;
  box-shadow: 0 0 0 3px var(--accent-muted);
}

/* Cell types */
.cell-null {
  color: var(--text-muted);
  font-style: italic;
  font-size: 12px;
}
.cell-bool-true { color: var(--success); }
.cell-bool-false { color: var(--danger); }
.cell-number { font-variant-numeric: tabular-nums; }
.cell-pk {
  font-weight: 600;
  color: var(--warning);
}

/* Row actions */
.row-actions {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
}
.row-actions button {
  padding: 4px 6px;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 4px;
  color: var(--text-muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  transition: all .15s;
}
.row-actions button:hover {
  background: var(--bg-elevated);
  color: var(--text-primary);
  border-color: var(--border);
}
.row-actions button.action-delete:hover { color: var(--danger); }
.row-actions button.action-save { color: var(--success); }
.row-actions button.action-cancel { color: var(--danger); }

/* ── Pagination ── */
.pagination {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 24px;
  border-top: 1px solid var(--border);
  background: var(--bg-surface);
  flex-shrink: 0;
  gap: 16px;
}

.page-info {
  font-size: 13px;
  color: var(--text-secondary);
  white-space: nowrap;
}

.page-controls {
  display: flex;
  align-items: center;
  gap: 4px;
}

.page-btn {
  padding: 5px 10px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 14px;
  transition: all .15s;
}
.page-btn:hover:not(:disabled) { background: var(--bg-hover); color: var(--text-primary); }
.page-btn:disabled { opacity: .3; cursor: not-allowed; }
.page-btn.active { background: var(--accent-muted); color: var(--accent); border-color: var(--accent); }

.page-numbers {
  display: flex;
  gap: 4px;
}

.page-size label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--text-secondary);
}
.page-size select {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-primary);
  padding: 4px 8px;
  font-size: 13px;
  cursor: pointer;
  outline: none;
}

/* ── Loading ── */
.loading-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(13,17,23,.7);
  backdrop-filter: blur(4px);
  z-index: 10;
}

.spinner {
  width: 40px; height: 40px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Modal ── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.6);
  backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  animation: fadeIn .2s var(--ease);
}
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

.modal {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  width: 560px;
  max-width: 95vw;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 24px 48px rgba(0,0,0,.4);
  animation: slideUp .25s var(--ease);
}
@keyframes slideUp { from { transform: translateY(20px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }

.modal-sm { width: 400px; }

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 24px;
  border-bottom: 1px solid var(--border-light);
}
.modal-header h2 { font-size: 16px; font-weight: 600; }

.modal-form {
  padding: 20px 24px;
  overflow-y: auto;
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.form-group label {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: .04em;
  display: flex;
  align-items: center;
  gap: 6px;
}
.form-group label .type-badge {
  font-weight: 400;
  font-size: 10px;
  text-transform: none;
  letter-spacing: 0;
  color: var(--text-muted);
  background: var(--bg-base);
  padding: 1px 6px;
  border-radius: 4px;
}
.form-group label .pk-badge {
  font-weight: 600;
  font-size: 10px;
  text-transform: none;
  letter-spacing: 0;
  color: var(--warning);
  background: var(--warning-bg);
  padding: 1px 6px;
  border-radius: 4px;
}

.form-group input,
.form-group textarea {
  padding: 8px 12px;
  background: var(--bg-base);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-size: 14px;
  font-family: inherit;
  outline: none;
  transition: border-color .2s;
}
.form-group input:focus,
.form-group textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-muted);
}
.form-group textarea { min-height: 60px; resize: vertical; }

.modal-body-text {
  padding: 20px 24px;
  color: var(--text-secondary);
  font-size: 14px;
  line-height: 1.6;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 16px 24px;
  border-top: 1px solid var(--border-light);
}

/* ── Toasts ── */
.toast-container {
  position: fixed;
  bottom: 24px;
  right: 24px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 200;
}

.toast {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 20px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 13px;
  color: var(--text-primary);
  box-shadow: 0 8px 24px rgba(0,0,0,.3);
  animation: toastIn .3s var(--ease);
  min-width: 280px;
}
@keyframes toastIn { from { transform: translateX(100%); opacity: 0; } to { transform: translateX(0); opacity: 1; } }

.toast.toast-exit { animation: toastOut .3s var(--ease) forwards; }
@keyframes toastOut { to { transform: translateX(100%); opacity: 0; } }

.toast-success { border-left: 3px solid var(--success); }
.toast-error { border-left: 3px solid var(--danger); }
.toast-info { border-left: 3px solid var(--accent); }

/* ── Responsive ── */
@media (max-width: 768px) {
  html,
  body {
    height: auto;
    min-height: 100%;
    overflow: hidden;
  }

  body {
    display: block;
  }

  #sidebar {
    position: fixed;
    left: 0; top: 0;
    width: min(88vw, var(--sidebar-w));
    min-width: 0;
    transform: translateX(-100%);
    box-shadow: 4px 0 24px rgba(0,0,0,.4);
  }
  #sidebar:not(.collapsed) { transform: translateX(0); }

  #main {
    height: 100dvh;
    width: 100%;
  }

  #topbar {
    height: auto;
    min-height: var(--topbar-h);
    padding: 10px 12px;
    align-items: flex-start;
    gap: 10px;
    flex-wrap: wrap;
  }

  .topbar-left {
    flex: 1 1 100%;
    width: 100%;
  }

  .topbar-left h1 {
    font-size: 15px;
  }

  .topbar-right {
    flex: 1 1 100%;
    width: 100%;
    gap: 8px;
  }

  .badge {
    max-width: 44vw;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .search-box {
    flex: 1;
    min-width: 0;
  }

  .search-box input {
    width: 100%;
    min-width: 0;
  }

  #btn-add-row {
    padding-inline: 12px;
  }

  .empty-state {
    padding: 24px;
    text-align: center;
  }

  .table-wrapper {
    padding: 12px;
    background: var(--bg-base);
  }

  #data-table,
  #data-table thead,
  #data-table tbody,
  #data-table tr,
  #data-table td {
    display: block;
    width: 100%;
  }

  #data-table thead {
    display: none;
  }

  #data-table {
    border-collapse: separate;
    border-spacing: 0;
  }

  #data-table tbody {
    display: flex;
    flex-direction: column;
    gap: 12px;
  }

  #data-table tr {
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
    box-shadow: 0 8px 24px rgba(0,0,0,.16);
  }

  #data-table tbody tr:hover {
    background: var(--bg-surface);
  }

  #data-table td {
    display: grid;
    grid-template-columns: minmax(92px, 38%) minmax(0, 1fr);
    gap: 12px;
    align-items: start;
    max-width: none;
    min-height: 42px;
    padding: 10px 12px;
    white-space: normal;
    overflow-wrap: anywhere;
    border-bottom: 1px solid var(--border-light);
    text-overflow: clip;
  }

  #data-table td:last-child {
    border-bottom: none;
  }

  #data-table td[colspan] {
    display: block;
  }

  #data-table td[colspan]::before {
    content: none;
  }

  #data-table td::before {
    content: attr(data-label);
    color: var(--text-muted);
    font-size: 11px;
    font-weight: 600;
    letter-spacing: .04em;
    text-transform: uppercase;
    overflow-wrap: anywhere;
  }

  #data-table td input.cell-edit {
    min-width: 0;
  }

  .row-actions {
    justify-content: flex-start;
  }

  .pagination {
    flex-wrap: wrap;
    justify-content: center;
    padding: 10px 12px;
    gap: 10px;
  }

  .page-info,
  .page-size {
    flex: 1 1 auto;
  }

  .page-controls {
    order: 3;
    width: 100%;
    justify-content: center;
  }

  .page-numbers {
    max-width: 45vw;
    overflow-x: auto;
    padding-bottom: 2px;
  }

  .modal {
    width: calc(100vw - 24px);
    max-height: calc(100dvh - 24px);
  }

  .modal-header,
  .modal-form,
  .modal-body-text,
  .modal-footer {
    padding-left: 16px;
    padding-right: 16px;
  }

  .toast-container {
    left: 12px;
    right: 12px;
    bottom: 12px;
  }

  .toast {
    min-width: 0;
    width: 100%;
  }
}
