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

:root {
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --danger: #dc2626;
  --danger-hover: #b91c1c;
  --bg: #f8fafc;
  --surface: #ffffff;
  --border: #e2e8f0;
  --text: #1e293b;
  --text-muted: #64748b;
  --radius: 6px;
  --shadow: 0 1px 3px rgba(0,0,0,0.1);
  --header-h: 61px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

.hidden { display: none !important; }

/* --- Buttons --- */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  background: var(--surface);
  color: var(--text);
  transition: all 0.15s;
  white-space: nowrap;
}
.btn:hover { background: #f1f5f9; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: white; border-color: var(--primary); }
.btn-primary:hover { background: var(--primary-hover); }
.btn-danger { background: var(--danger); color: white; border-color: var(--danger); }
.btn-danger:hover { background: var(--danger-hover); }
.btn-block { width: 100%; justify-content: center; }
.btn-small { padding: 4px 10px; font-size: 12px; }

/* --- Screens --- */
.screen { min-height: 100vh; }

/* --- Profiles Screen --- */
#profiles-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #1e3a5f 0%, #2563eb 100%);
  padding: 40px 20px;
}
.profiles-card {
  background: var(--surface);
  padding: 40px;
  border-radius: 12px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.2);
  width: 100%;
  max-width: 560px;
}
.profiles-card h1 { font-size: 22px; margin-bottom: 4px; }
.profiles-card .subtitle { color: var(--text-muted); margin-bottom: 24px; font-size: 14px; }

.profiles-list { display: flex; flex-direction: column; gap: 10px; margin-bottom: 16px; }

.profile-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #f8fafc;
  gap: 12px;
}
.profile-info { flex: 1; min-width: 0; }
.profile-name { font-weight: 600; font-size: 15px; }
.profile-meta { font-size: 13px; color: var(--text-muted); margin-top: 2px; }
.profile-actions { display: flex; gap: 6px; flex-shrink: 0; }

.profiles-empty {
  text-align: center;
  color: var(--text-muted);
  font-size: 14px;
  padding: 24px 0;
  margin-bottom: 16px;
}
.profiles-footer { margin-top: 8px; }
.profiles-shared-divider {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  text-align: center;
  padding: 8px 0 4px;
  letter-spacing: 0.5px;
  text-transform: uppercase;
}
.profile-card-shared { background: #f0f9ff; border-color: #bae6fd; }
.badge-shared {
  display: inline-block;
  padding: 1px 6px;
  border-radius: 8px;
  font-size: 10px;
  font-weight: 600;
  background: #dbeafe;
  color: var(--primary);
  vertical-align: middle;
}

/* --- Shared form styles --- */
.form-group { margin-bottom: 16px; }
.form-group label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 4px; }
.form-group input, .form-group select {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 14px;
  outline: none;
  transition: border 0.15s;
}
.form-group input:focus, .form-group select:focus { border-color: var(--primary); }
.optional { color: var(--text-muted); font-weight: 400; }
.error-msg { color: var(--danger); font-size: 13px; }

/* --- Header --- */
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow);
  height: var(--header-h);
  flex-shrink: 0;
}
.header-left { display: flex; align-items: center; gap: 16px; }
header h1 { font-size: 18px; }
.user-badge {
  background: #dbeafe;
  color: var(--primary);
  padding: 2px 10px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 600;
}

/* --- Dashboard layout --- */
#dashboard {
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

.dashboard-body {
  display: flex;
  flex: 1;
  overflow: hidden;
}

/* --- Entity Sidebar --- */
.entity-sidebar {
  width: 240px;
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.sidebar-header {
  padding: 12px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.sidebar-header .search-input { max-width: 100%; width: 100%; }

.entity-list {
  flex: 1;
  overflow-y: auto;
  padding: 6px 0;
}

.entity-item {
  padding: 7px 16px;
  font-size: 13px;
  cursor: pointer;
  color: var(--text);
  border-left: 3px solid transparent;
  transition: background 0.1s;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.entity-item:hover { background: #f1f5f9; }
.entity-item.active {
  background: #eff6ff;
  color: var(--primary);
  border-left-color: var(--primary);
  font-weight: 600;
}
.entity-count { font-size: 11px; color: var(--text-muted); }
.entity-count-loading { font-size: 11px; color: #cbd5e1; }

.sparse-filter-label {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 11px;
  color: var(--text-muted);
  cursor: pointer;
  margin-top: 8px;
  padding: 0 2px;
}
.sparse-filter-label input[type="checkbox"] { cursor: pointer; }

/* --- Main Panel --- */
.main-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  padding: 20px 24px 0;
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
  gap: 16px;
  flex-wrap: wrap;
  flex-shrink: 0;
}

.panel-title {
  display: flex;
  align-items: baseline;
  gap: 10px;
  min-width: 0;
}
.panel-title h2 { font-size: 18px; white-space: nowrap; }

.field-count {
  font-size: 12px;
  color: var(--text-muted);
  white-space: nowrap;
}

.panel-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

/* --- Search input --- */
.search-input {
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 14px;
  outline: none;
  background: var(--surface);
}
.search-input:focus { border-color: var(--primary); }
.search-input:disabled { background: #f1f5f9; color: var(--text-muted); }

/* --- Table container --- */
.table-container {
  background: var(--surface);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  overflow: auto;
  flex: 1;
  margin-bottom: 20px;
}

table { width: 100%; border-collapse: collapse; }
th {
  text-align: left;
  padding: 10px 14px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted);
  background: #f8fafc;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
  position: sticky;
  top: 0;
  z-index: 2;
}

/* Filter row — sticky just below the header row (~37px tall) */
#fm-filter-row th {
  top: 37px;
  background: #ffffff;
  padding: 5px 8px;
  border-bottom: 2px solid var(--border);
  text-transform: none;
  letter-spacing: 0;
  font-weight: 400;
}

/* Column filter inputs and selects */
.col-filter {
  width: 100%;
  padding: 3px 6px;
  border: 1px solid var(--border);
  border-radius: 4px;
  font-size: 12px;
  outline: none;
  background: white;
  color: var(--text);
  min-width: 0;
}
.col-filter:focus { border-color: var(--primary); }
select.col-filter { cursor: pointer; }

th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { color: var(--text); }
th.sort-active { color: var(--primary); }
#sort-indicator { font-size: 10px; margin-left: 3px; }
td {
  padding: 9px 14px;
  font-size: 13px;
  border-bottom: 1px solid var(--border);
}
tr:hover td { background: #f8fafc; }
tr:last-child td { border-bottom: none; }

td.field-name {
  font-family: 'Consolas', 'Monaco', monospace;
  font-size: 12px;
  color: #0f4c81;
  white-space: nowrap;
}
td.field-label {
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
td.field-type {
  white-space: nowrap;
}
td.field-number {
  text-align: right;
  font-variant-numeric: tabular-nums;
  color: var(--text-muted);
}
td.field-text-long {
  max-width: 220px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--text-muted);
  font-size: 12px;
}

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 10px;
  font-size: 11px;
  font-weight: 600;
}
.badge-yes { background: #dcfce7; color: #16a34a; }
.badge-no { background: #f1f5f9; color: #94a3b8; }

.type-tag {
  display: inline-block;
  padding: 2px 7px;
  border-radius: 4px;
  font-size: 11px;
  font-weight: 600;
  font-family: monospace;
  background: #f1f5f9;
  color: var(--text-muted);
}
.type-tag.scalar { background: #eff6ff; color: #3b82f6; }
.type-tag.to-one { background: #fef3c7; color: #d97706; }
.type-tag.to-many { background: #fce7f3; color: #db2777; }
.type-tag.id { background: #f0fdf4; color: #16a34a; }

/* --- Loading / Empty --- */
.loading, .empty-state {
  padding: 60px 40px;
  text-align: center;
  color: var(--text-muted);
  font-size: 14px;
}

/* --- Modal --- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 20px;
}
.modal {
  background: var(--surface);
  border-radius: 12px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.2);
  width: 100%;
  max-width: 800px;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
}
.modal-small { max-width: 440px; }
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 24px;
  border-bottom: 1px solid var(--border);
}
.modal-header h2 { font-size: 18px; }
.modal-close {
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
  color: var(--text-muted);
  line-height: 1;
}
.modal-body {
  padding: 24px;
  overflow-y: auto;
  flex: 1;
}
.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 16px 24px;
  border-top: 1px solid var(--border);
}

/* --- Edit Mode --- */
.btn-warning { background: #f59e0b; color: white; border-color: #f59e0b; }
.btn-warning:hover { background: #d97706; }
.btn-active { background: var(--primary); color: white; border-color: var(--primary); }
.btn-active:hover { background: var(--primary-hover); }

td.cell-edited { background: #fef9c3 !important; }

.edit-input {
  width: 100%;
  padding: 2px 5px;
  border: 1px solid #cbd5e1;
  border-radius: 3px;
  font-size: 12px;
  font-family: inherit;
  background: white;
  box-sizing: border-box;
  min-width: 60px;
}
.edit-input:focus { border-color: var(--primary); outline: none; }
td.cell-edited .edit-input { background: #fef9c3; }
.edit-input-sm { width: 70px; min-width: 0; }

td.edit-check { text-align: center; }
td.edit-check input[type="checkbox"] { width: 16px; height: 16px; cursor: pointer; }

/* --- Responsive --- */
@media (max-width: 768px) {
  .entity-sidebar { width: 180px; }
  .panel-header { flex-direction: column; align-items: flex-start; }
  td, th { padding: 8px 10px; font-size: 12px; }
}
