/* ============================================================
   Base Reset & Variables
   ============================================================ */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #0f1117;
  --surface: #1a1d27;
  --surface-2: #1f2233;
  --border: #2a2f45;
  --border-strong: #3a4060;
  --text: #e2e8f0;
  --text-muted: #8b97b0;
  --text-light: #5a6480;
  --primary: #5b8df6;
  --primary-dark: #4070e8;
  --primary-light: #1a2240;
  --success: #34d399;
  --success-light: #0d2b20;
  --danger: #f87171;
  --danger-light: #2d1515;
  --warning: #fbbf24;
  --warning-light: #2d2008;
  --sidebar-width: 240px;
  --radius: 8px;
  --radius-sm: 4px;
  --shadow: 0 1px 3px rgba(0,0,0,.4), 0 1px 2px rgba(0,0,0,.3);
  --shadow-md: 0 4px 6px -1px rgba(0,0,0,.5), 0 2px 4px -1px rgba(0,0,0,.3);
  --shadow-lg: 0 10px 15px -3px rgba(0,0,0,.6), 0 4px 6px -2px rgba(0,0,0,.4);
}

html, body { height: 100%; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; font-size: 14px; color: var(--text); background: var(--bg); }

/* ============================================================
   Layout
   ============================================================ */
#app {
  display: flex;
  min-height: 100vh;
}

.sidebar {
  width: var(--sidebar-width);
  min-width: var(--sidebar-width);
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0; left: 0; bottom: 0;
  z-index: 100;
  overflow-y: auto;
}

.sidebar.hidden { display: none; }

.main-content {
  flex: 1;
  min-height: 100vh;
  padding: 32px;
  overflow-y: auto;
}

.main-content.with-sidebar {
  margin-left: var(--sidebar-width);
}

/* ============================================================
   Sidebar
   ============================================================ */
.sidebar-header {
  padding: 20px 16px;
  border-bottom: 1px solid var(--border);
}

.sidebar-logo {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 700;
  font-size: 16px;
  color: var(--primary);
}

.sidebar-logo svg { flex-shrink: 0; }

.sidebar-nav {
  list-style: none;
  padding: 12px 8px;
  flex: 1;
}

.sidebar-nav li a {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 12px;
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  text-decoration: none;
  font-weight: 500;
  transition: background .15s, color .15s;
}

.sidebar-nav li a:hover {
  background: var(--bg);
  color: var(--text);
}

.sidebar-nav li a.active {
  background: var(--primary-light);
  color: var(--primary);
}

.sidebar-footer {
  padding: 12px 16px;
  border-top: 1px solid var(--border);
}

/* ============================================================
   Page Header
   ============================================================ */
.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 24px;
}

.page-title {
  font-size: 22px;
  font-weight: 700;
  color: var(--text);
}

.page-subtitle {
  color: var(--text-muted);
  font-size: 14px;
  margin-top: 2px;
}

.breadcrumb {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--text-muted);
  margin-bottom: 6px;
}

.breadcrumb a { color: var(--primary); text-decoration: none; }
.breadcrumb a:hover { text-decoration: underline; }
.breadcrumb span { color: var(--text-light); }

/* ============================================================
   Cards
   ============================================================ */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow);
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

.card-title {
  font-size: 15px;
  font-weight: 600;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}

.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow);
}

.stat-value {
  font-size: 28px;
  font-weight: 700;
  color: var(--primary);
  line-height: 1;
  margin-bottom: 6px;
}

.stat-label {
  color: var(--text-muted);
  font-size: 13px;
}

/* ============================================================
   Buttons
   ============================================================ */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all .15s;
  text-decoration: none;
  white-space: nowrap;
}

.btn:disabled { opacity: .5; cursor: not-allowed; }

.btn-primary { background: var(--primary); color: #fff; border-color: var(--primary); }
.btn-primary:hover:not(:disabled) { background: var(--primary-dark); border-color: var(--primary-dark); }

.btn-secondary { background: var(--surface); color: var(--text); border-color: var(--border-strong); }
.btn-secondary:hover:not(:disabled) { background: var(--bg); }

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

.btn-ghost { background: transparent; color: var(--text-muted); border-color: transparent; }
.btn-ghost:hover:not(:disabled) { background: var(--bg); color: var(--text); }

.btn-sm { padding: 5px 10px; font-size: 12px; }
.btn-icon { padding: 6px; }

/* ============================================================
   Tables
   ============================================================ */
.table-wrapper { overflow-x: auto; }

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

thead th {
  text-align: left;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  color: var(--text-muted);
  font-weight: 600;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .04em;
  background: var(--surface-2);
}

tbody tr {
  border-bottom: 1px solid var(--border);
  transition: background .1s;
}

tbody tr:last-child { border-bottom: none; }
tbody tr:hover { background: var(--surface-2); }

tbody td {
  padding: 12px 14px;
  vertical-align: middle;
}

.table-actions { display: flex; gap: 6px; align-items: center; }

/* ============================================================
   Forms
   ============================================================ */
.form-group { margin-bottom: 16px; }
.form-group label { display: block; font-weight: 500; margin-bottom: 6px; font-size: 13px; }
.form-group .hint { font-size: 12px; color: var(--text-muted); margin-top: 4px; }

input[type=text], input[type=email], input[type=password], textarea, select {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  font-size: 13px;
  color: var(--text);
  background: var(--surface);
  transition: border-color .15s;
  outline: none;
  font-family: inherit;
}

input[type=text]:focus, input[type=email]:focus, input[type=password]:focus, textarea:focus, select:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(59,130,246,.1);
}

input.invalid, textarea.invalid {
  border-color: var(--danger);
  box-shadow: 0 0 0 3px rgba(239,68,68,.1);
}

textarea { min-height: 120px; resize: vertical; }
textarea.json-editor { font-family: 'Cascadia Code', 'Fira Code', 'Consolas', monospace; font-size: 12px; min-height: 200px; }

.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.form-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 20px; }

/* ============================================================
   Modal
   ============================================================ */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 20px;
}

.modal {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  width: 100%;
  max-width: 520px;
  max-height: 90vh;
  overflow-y: auto;
  animation: modalIn .15s ease-out;
}

.modal-lg { max-width: 720px; }

@keyframes modalIn {
  from { opacity: 0; transform: scale(.96); }
  to   { opacity: 1; transform: scale(1); }
}

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

.modal-title { font-size: 16px; font-weight: 600; }

.modal-body { padding: 20px 24px; }
.modal-footer {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  padding: 16px 24px;
  border-top: 1px solid var(--border);
}

.modal-close {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-muted);
  padding: 4px;
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
}
.modal-close:hover { background: var(--bg); color: var(--text); }

/* ============================================================
   Toast
   ============================================================ */
#toast-container {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 2000;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.toast {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  font-size: 13px;
  font-weight: 500;
  max-width: 360px;
  animation: toastIn .2s ease-out;
}

@keyframes toastIn {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}

.toast-success { background: var(--success-light); color: #15803d; border: 1px solid #86efac; }
.toast-error   { background: var(--danger-light);  color: #b91c1c; border: 1px solid #fca5a5; }
.toast-info    { background: var(--primary-light); color: var(--primary-dark); border: 1px solid #93c5fd; }

/* ============================================================
   Badges & Tags
   ============================================================ */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 2px 8px;
  border-radius: 99px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .03em;
}

.badge-success { background: var(--success-light); color: #34d399; }
.badge-danger  { background: var(--danger-light);  color: #f87171; }
.badge-primary { background: var(--primary-light); color: var(--primary); }
.badge-neutral { background: var(--surface-2); color: var(--text-muted); border: 1px solid var(--border); }

.perm-tags { display: flex; flex-wrap: wrap; gap: 4px; }
.perm-tag {
  padding: 2px 7px;
  border-radius: 99px;
  font-size: 11px;
  font-weight: 500;
  background: var(--primary-light);
  color: var(--primary);
}
.perm-tag.read   { background: #0d2b20; color: #34d399; }
.perm-tag.write  { background: #1a2240; color: #5b8df6; }
.perm-tag.delete { background: #2d1515; color: #f87171; }
.perm-tag.admin  { background: #1e1030; color: #a78bfa; }

/* ============================================================
   Auth Pages (Login / Setup)
   ============================================================ */
.auth-page {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background: var(--bg);
}

.auth-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 40px;
  width: 100%;
  max-width: 400px;
  box-shadow: var(--shadow-md);
}

.auth-logo {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 700;
  font-size: 20px;
  color: var(--primary);
  margin-bottom: 28px;
  justify-content: center;
}

.auth-title { font-size: 18px; font-weight: 600; margin-bottom: 4px; text-align: center; }
.auth-subtitle { color: var(--text-muted); font-size: 13px; margin-bottom: 24px; text-align: center; }

/* ============================================================
   Copy Field
   ============================================================ */
.copy-field {
  display: flex;
  gap: 8px;
  align-items: center;
}

.copy-field input {
  flex: 1;
  font-family: monospace;
  font-size: 12px;
  background: var(--surface-2);
}

/* ============================================================
   Alert / Info Box
   ============================================================ */
.alert {
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  margin-bottom: 16px;
  display: flex;
  gap: 10px;
  align-items: flex-start;
}

.alert-warning { background: var(--warning-light); color: #92400e; border: 1px solid #fcd34d; }
.alert-danger  { background: var(--danger-light);  color: #b91c1c; border: 1px solid #fca5a5; }
.alert-info    { background: var(--primary-light); color: var(--primary-dark); border: 1px solid #93c5fd; }
.alert-success { background: var(--success-light); color: #15803d; border: 1px solid #86efac; }

/* ============================================================
   Search / Filter Bar
   ============================================================ */
.filter-bar {
  display: flex;
  gap: 10px;
  margin-bottom: 16px;
  align-items: center;
  flex-wrap: wrap;
}

.filter-bar input, .filter-bar select { width: auto; min-width: 180px; }
.filter-bar .spacer { flex: 1; }

/* ============================================================
   Empty State
   ============================================================ */
.empty-state {
  text-align: center;
  padding: 48px 24px;
  color: var(--text-muted);
}

.empty-state svg { margin-bottom: 16px; opacity: .4; }
.empty-state h3 { font-size: 16px; margin-bottom: 8px; color: var(--text); }
.empty-state p  { font-size: 13px; margin-bottom: 20px; }

/* ============================================================
   Permissions Checkboxes
   ============================================================ */
.permissions-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}

.permission-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: border-color .15s, background .15s;
}

.permission-item:hover { border-color: var(--primary); background: var(--primary-light); }
.permission-item input[type=checkbox] { width: auto; }

/* ============================================================
   Misc Utilities
   ============================================================ */
.text-muted  { color: var(--text-muted); }
.text-danger { color: var(--danger); }
.text-success{ color: var(--success); }
.text-mono   { font-family: monospace; font-size: 12px; }

.d-flex   { display: flex; }
.gap-2    { gap: 8px; }
.mt-4     { margin-top: 16px; }
.mt-2     { margin-top: 8px; }
.mb-4     { margin-bottom: 16px; }
.w-full   { width: 100%; }
.truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 220px; }

.divider { border: none; border-top: 1px solid var(--border); margin: 20px 0; }

.loading {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 48px;
  color: var(--text-muted);
  font-size: 13px;
}

@media (max-width: 768px) {
  .sidebar { transform: translateX(-100%); transition: transform .2s; }
  .sidebar.open { transform: translateX(0); }
  .main-content.with-sidebar { margin-left: 0; }
  .form-row { grid-template-columns: 1fr; }
  .stats-grid { grid-template-columns: repeat(2, 1fr); }
}

/* ============================================================
   JSON Viewer
   ============================================================ */
.json-viewer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 8px;
}

.json-viewer-header label { margin-bottom: 0; }

.json-viewer-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.json-viewer {
  max-height: 420px;
  overflow: auto;
  padding: 12px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--bg);
  font-family: 'Cascadia Code', 'Fira Code', 'Consolas', monospace;
  font-size: 12px;
  line-height: 1.65;
}

.json-viewer-row,
.json-viewer-summary {
  padding-left: calc(var(--json-depth) * 18px);
  min-height: 22px;
}

.json-viewer-summary {
  cursor: pointer;
  list-style: none;
  user-select: none;
}

.json-viewer-summary::-webkit-details-marker { display: none; }

.json-viewer-summary::before {
  content: '>';
  display: inline-block;
  width: 14px;
  margin-left: -14px;
  color: var(--text-muted);
  transition: transform .12s;
}

.json-viewer-branch[open] > .json-viewer-summary::before {
  transform: rotate(90deg);
}

.json-viewer-summary:hover,
.json-viewer-row:hover {
  background: var(--surface);
}

.json-viewer-key { color: #93c5fd; }
.json-viewer-string { color: #86efac; }
.json-viewer-number { color: #fbbf24; }
.json-viewer-boolean { color: #fca5a5; }
.json-viewer-null { color: var(--text-light); font-style: italic; }
.json-viewer-bracket { color: var(--text); }
.json-viewer-meta { color: var(--text-muted); margin-left: 8px; }

.stat-card-link {
  display: block;
  color: inherit;
  text-decoration: none;
}

.stat-card-link:hover {
  border-color: var(--primary);
  background: var(--surface-2);
}

.stat-card-muted { opacity: .65; }

.stat-value-text {
  font-size: 16px;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.stat-meta {
  margin-top: 8px;
  color: var(--text-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
