:root {
  --bg: #0f0f1a;
  --surface: #1a1a2e;
  --surface2: #252540;
  --surface3: #2e2e50;
  --accent: #c0392b;
  --accent-hover: #e74c3c;
  --gold: #f0a500;
  --text: #e8e8f0;
  --text-dim: #9090b0;
  --border: #3a3a60;
  --border-light: #4a4a70;
  --success: #27ae60;
  --danger: #c0392b;
  --radius: 4px;
}

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

html, body {
  height: 100%;
  overflow: hidden;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  font-size: 13px;
  background: var(--bg);
  color: var(--text);
  line-height: 1.4;
}

#app {
  display: grid;
  grid-template-columns: var(--sidebar-width, 180px) 4px 1fr;
  height: 100vh;
  overflow: hidden;
}


#resize-handle {
  background: var(--border);
  cursor: col-resize;
  transition: background 0.15s;
  position: relative;
  z-index: 10;
}

#resize-handle:hover,
#resize-handle.dragging {
  background: var(--accent);
}

/* Sidebar */
#sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

#sidebar-header {
  padding: 10px 12px 10px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

#sidebar-header h1 {
  font-size: 15px;
  font-weight: 600;
  color: var(--gold);
  letter-spacing: 0.3px;
}

#table-groups {
  overflow-y: auto;
  flex: 1;
  padding: 8px 0;
}


.table-group {
  margin-bottom: 4px;
}

.group-label {
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: var(--text-dim);
  padding: 8px 16px 4px;
}

.table-btn {
  display: block;
  width: 100%;
  text-align: left;
  padding: 6px 16px;
  background: none;
  border: none;
  color: var(--text);
  cursor: pointer;
  font-size: 13px;
  transition: background 0.1s;
  border-radius: 0;
}

.table-btn:hover {
  background: var(--surface2);
}

.table-btn.active {
  background: var(--surface3);
  color: var(--gold);
  font-weight: 500;
}

/* Main */
#main {
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

#toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  gap: 12px;
}

#toolbar-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

#table-title {
  font-size: 15px;
  font-weight: 600;
  color: var(--gold);
}

#row-count {
  font-size: 12px;
  color: var(--text-dim);
}

#toolbar-right {
  display: flex;
  align-items: center;
  gap: 8px;
}

#search-input {
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 5px 10px;
  border-radius: var(--radius);
  font-size: 13px;
  width: 200px;
  outline: none;
}

#search-input:focus {
  border-color: var(--accent);
}

#search-input:disabled {
  opacity: 0.4;
}

button {
  padding: 5px 12px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--text);
  cursor: pointer;
  font-size: 13px;
  transition: background 0.1s, border-color 0.1s;
}

button:hover:not(:disabled) {
  background: var(--surface3);
  border-color: var(--border-light);
}

button:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

button.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 500;
}

button.btn-primary:hover:not(:disabled) {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}

button.btn-danger {
  background: transparent;
  border-color: var(--danger);
  color: var(--danger);
}

button.btn-danger:hover:not(:disabled) {
  background: var(--danger);
  color: #fff;
}

/* Grid */
#grid-container {
  flex: 1;
  overflow: auto;
  position: relative;
}

#grid-placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: var(--text-dim);
}

.data-grid {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
}

.data-grid th {
  position: sticky;
  top: 0;
  background: var(--surface2);
  border-bottom: 2px solid var(--border);
  padding: 7px 10px;
  text-align: left;
  font-weight: 600;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.3px;
  color: var(--text-dim);
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
  z-index: 1;
}

.data-grid th:hover {
  color: var(--text);
  background: var(--surface3);
}

.data-grid th.sort-asc::after { content: ' ↑'; color: var(--gold); }
.data-grid th.sort-desc::after { content: ' ↓'; color: var(--gold); }

.data-grid td {
  padding: 5px 10px;
  border-bottom: 1px solid var(--border);
  max-width: 180px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  vertical-align: middle;
}

.data-grid tr:nth-child(even) td {
  background: rgba(255,255,255,0.02);
}

.data-grid tr:hover td {
  background: var(--surface2);
}

.data-grid td.pk-col {
  color: var(--text-dim);
  font-size: 11px;
}

.data-grid td.fk-col .fk-name {
  color: var(--text-dim);
  font-size: 11px;
}

.data-grid td .null-badge {
  color: var(--text-dim);
  font-style: italic;
  font-size: 11px;
}

.edit-btn {
  padding: 2px 6px;
  font-size: 11px;
  border-color: var(--border-light);
}

.data-grid td.editable {
  cursor: text;
}

.data-grid td.editable.fk-col {
  cursor: pointer;
}

.data-grid td.editable:hover {
  background: var(--surface3) !important;
  outline: 1px solid var(--border-light);
  outline-offset: -1px;
}

.data-grid td input.inline-input,
.data-grid td select.inline-select {
  width: 100%;
  background: var(--surface3);
  border: none;
  border-bottom: 2px solid var(--accent);
  color: var(--text);
  font-size: 12px;
  font-family: inherit;
  outline: none;
  padding: 0 2px;
}

.fk-overlay {
  position: fixed;
  z-index: 500;
  background: var(--surface2);
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  box-shadow: 0 6px 24px rgba(0,0,0,0.5);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.fk-overlay input[type="search"] {
  border: none;
  border-bottom: 1px solid var(--border);
  border-radius: 0;
  background: var(--surface3);
  color: var(--text);
  padding: 6px 10px;
  font-size: 12px;
  outline: none;
  flex-shrink: 0;
  width: 100%;
}

.fk-list {
  overflow-y: auto;
  max-height: 260px;
  flex: 1;
}

.fk-item {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  padding: 5px 10px;
  cursor: pointer;
  gap: 8px;
}

.fk-item:hover,
.fk-item.selected {
  background: var(--surface3);
}

.fk-item.selected {
  border-left: 2px solid var(--accent);
  padding-left: 8px;
}

.fk-item-name {
  flex: 1;
  font-size: 12px;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.fk-item-id {
  font-size: 11px;
  color: var(--text-dim);
  flex-shrink: 0;
}

/* Pagination */
#pagination {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 16px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

#page-info {
  font-size: 12px;
  color: var(--text-dim);
  white-space: nowrap;
}

#page-size-select {
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 4px 8px;
  border-radius: var(--radius);
  font-size: 12px;
  width: auto;
  margin-left: auto;
}

/* Modal */
#modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.75);
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
}

#modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  width: 640px;
  max-height: 85vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 20px 60px rgba(0,0,0,0.5);
}

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

#modal-title {
  font-size: 15px;
  font-weight: 600;
}

#modal-close {
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 16px;
  cursor: pointer;
  padding: 2px 6px;
}

#modal-close:hover {
  color: var(--text);
  background: var(--surface2);
}

#modal-body {
  overflow-y: auto;
  flex: 1;
  padding: 16px 18px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

#modal-footer {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 18px;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

/* Form fields */
.field-group {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.field-label {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.4px;
  color: var(--text-dim);
  display: flex;
  align-items: center;
  gap: 8px;
}

.field-badge {
  font-size: 9px;
  padding: 1px 5px;
  border-radius: 3px;
  font-weight: 600;
  letter-spacing: 0;
  text-transform: uppercase;
}

.badge-pk { background: var(--gold); color: #000; }
.badge-fk { background: #2980b9; color: #fff; }
.badge-enum { background: #8e44ad; color: #fff; }
.badge-null { background: var(--surface3); color: var(--text-dim); border: 1px solid var(--border); }

.field-value-ro {
  padding: 6px 10px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-dim);
  font-family: monospace;
  font-size: 13px;
}

input[type="text"],
input[type="number"],
input[type="search"],
textarea,
select {
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 6px 10px;
  border-radius: var(--radius);
  font-size: 13px;
  font-family: inherit;
  outline: none;
  width: 100%;
}

input[type="text"]:focus,
input[type="number"]:focus,
input[type="search"]:focus,
textarea:focus,
select:focus {
  border-color: var(--accent);
}

input:disabled,
textarea:disabled,
select:disabled {
  opacity: 0.4;
}

textarea {
  resize: vertical;
  min-height: 80px;
}

.fk-field {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.fk-filter {
  font-size: 12px;
  padding: 5px 10px;
}

.null-checkbox-row {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 4px;
}

.null-checkbox-row input[type="checkbox"] {
  width: auto;
  cursor: pointer;
}

.null-checkbox-row label {
  font-size: 12px;
  color: var(--text-dim);
  cursor: pointer;
}

.loading-overlay {
  position: absolute;
  inset: 0;
  background: rgba(15,15,26,0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-dim);
  font-size: 14px;
  z-index: 10;
}

.error-msg {
  color: var(--accent-hover);
  padding: 12px 16px;
  background: rgba(192,57,43,0.1);
  border: 1px solid rgba(192,57,43,0.3);
  border-radius: var(--radius);
  margin: 8px;
}

/* Mobile: one pane at a time */
@media (max-width: 767px) {
  #app {
    grid-template-columns: 1fr;
  }

  #resize-handle {
    display: none;
  }

  /* Default: show sidebar, hide main */
  #main {
    display: none;
  }

  /* Grid view: hide sidebar, show main */
  #app.grid-view #sidebar {
    display: none;
  }

  #app.grid-view #main {
    display: flex;
  }

  #back-btn {
    display: flex !important;
  }

  #search-input {
    width: 120px;
  }

  #export-btn {
    display: none;
  }
}
