/* ---- Page header ---- */
.page-header {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: var(--space-4);
  margin-bottom: var(--space-5);
  flex-wrap: wrap;
}

.page-header .page-header-text { min-width: 0; }

.page-header h2 {
  margin: 0;
  font-size: var(--text-xl);
  font-weight: 600;
  letter-spacing: -0.01em;
  color: var(--foreground-primary);
}

.page-header .page-subtitle {
  margin: var(--space-1) 0 0;
  font-size: var(--text-sm);
  color: var(--foreground-tertiary);
}

.page-header .page-actions {
  display: flex;
  gap: var(--space-2);
  align-items: center;
  flex-wrap: wrap;
}

/* ---- Card / grid / metric ---- */
.card {
  background: var(--surface-elevated);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-lg);
  padding: var(--space-4);
  margin-bottom: var(--space-4);
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: var(--space-4);
}

.metric {
  padding: var(--space-3);
  border-radius: var(--radius-md);
  background: var(--surface-secondary);
}

.metric .value {
  font-size: var(--text-2xl);
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--foreground-primary);
  word-break: break-all;
  line-height: 1.2;
}

.metric .label {
  font-size: var(--text-sm);
  color: var(--foreground-tertiary);
  margin-top: var(--space-1);
}

/* ---- Buttons ---- */
button {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  cursor: pointer;
  border: 1px solid transparent;
  border-radius: var(--radius-md);
  padding: 0 var(--space-3);
  height: 32px;
  font-size: var(--text-sm);
  font-weight: 500;
  font-family: inherit;
  transition: background 0.12s, border-color 0.12s, box-shadow 0.12s;
  background: var(--surface-tertiary);
  color: var(--foreground-primary);
  white-space: nowrap;
}

button:focus-visible {
  outline: none;
  box-shadow: 0 0 0 2px var(--surface-primary), 0 0 0 4px var(--accent-base);
}

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

.btn-primary {
  background: var(--foreground-primary);
  color: var(--surface-primary);
  border-color: var(--foreground-primary);
}

.btn-primary:hover { opacity: 0.85; }

.btn-danger {
  background: var(--semantic-error);
  color: #fff;
  border-color: var(--semantic-error);
}

.btn-danger:hover { opacity: 0.85; }

.btn-success {
  background: var(--semantic-success);
  color: #fff;
  border-color: var(--semantic-success);
}

.btn-success:hover { opacity: 0.85; }

.btn-secondary {
  background: var(--surface-elevated);
  color: var(--foreground-primary);
  border-color: var(--border-default);
}

.btn-secondary:hover {
  background: var(--surface-hover);
  border-color: var(--border-hover);
}

/* ---- Tables ---- */
table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--text-sm);
}

th, td {
  border-bottom: 1px solid var(--border-default);
  padding: 0 var(--space-3);
  height: 36px;
  text-align: left;
  vertical-align: middle;
}

thead th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--surface-secondary);
  color: var(--foreground-tertiary);
  font-weight: 600;
  font-size: var(--text-xs);
  letter-spacing: 0.03em;
  height: 34px;
}

tbody tr { transition: background 0.1s; }
tbody tr:hover { background: var(--surface-secondary); }

th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { background: var(--surface-hover); }
th .sort-indicator { color: var(--foreground-muted); margin-left: 0.3rem; }
th.sort-asc .sort-indicator::after { content: '▲'; color: var(--accent-base); }
th.sort-desc .sort-indicator::after { content: '▼'; color: var(--accent-base); }
th .sort-indicator::after { content: '⇅'; }

/* ---- Inputs ---- */
input, select, textarea {
  padding: 0 var(--space-3);
  height: 32px;
  border: 1px solid var(--border-default);
  border-radius: var(--radius-md);
  background: var(--surface-primary);
  color: var(--foreground-primary);
  font-size: var(--text-sm);
  font-family: inherit;
  transition: border-color 0.12s, box-shadow 0.12s;
}

textarea { height: auto; padding: var(--space-2) var(--space-3); }

input:hover, select:hover, textarea:hover { border-color: var(--border-hover); }

input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent-base);
  box-shadow: 0 0 0 3px var(--accent-bg);
}

/* ---- Badges ---- */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 2px var(--space-2);
  border-radius: 999px;
  font-size: var(--text-xs);
  font-weight: 500;
  line-height: 1.4;
}

.badge-enabled { background: rgba(22, 163, 74, 0.12); color: #15803d; }
.badge-disabled { background: rgba(220, 38, 38, 0.1); color: #b91c1c; }
.badge-tier { background: var(--accent-bg); color: var(--accent-base); }
.badge-neutral { background: var(--surface-tertiary); color: var(--foreground-secondary); }

[data-theme="dark"] .badge-enabled { color: #4ade80; }
[data-theme="dark"] .badge-disabled { color: #f87171; }

/* ---- Skeleton loaders ---- */
.skeleton {
  background: linear-gradient(90deg,
    var(--surface-tertiary) 25%,
    var(--surface-secondary) 50%,
    var(--surface-tertiary) 75%);
  background-size: 200% 100%;
  animation: skeleton-shimmer 1.4s ease-in-out infinite;
  border-radius: var(--radius-md);
}

.skeleton-block { width: 100%; height: 100%; min-height: 60px; }
.skeleton-text { height: 14px; margin: 4px 0; }

@keyframes skeleton-shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

/* ---- Compact user cell ---- */
.user-cell { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.user-cell .user-cell-id { font-family: monospace; font-size: var(--text-xs); color: var(--foreground-tertiary); }
.user-cell .user-cell-primary { color: var(--foreground-primary); }
.user-cell .user-cell-secondary { font-size: var(--text-xs); color: var(--foreground-muted); }

/* ---- Modal detail grid ---- */
.detail-grid { display: grid; grid-template-columns: 120px 1fr; gap: var(--space-2) var(--space-3); font-size: var(--text-sm); }
.detail-grid dt { color: var(--foreground-tertiary); margin: 0; }
.detail-grid dd { margin: 0; color: var(--foreground-primary); word-break: break-all; }

/* ---- Toast ---- */
#toastContainer {
  position: fixed;
  top: 1rem;
  right: 1rem;
  z-index: 1000;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.toast {
  padding: 0.75rem 1rem;
  border-radius: var(--radius-lg);
  background: var(--surface-elevated);
  border: 1px solid var(--border-default);
  box-shadow: var(--shadow-lg);
  font-size: var(--text-sm);
  max-width: 320px;
  animation: toastIn 0.2s ease-out;
  transition: opacity 0.2s, transform 0.2s;
}

.toast.success { border-left: 3px solid var(--semantic-success); }
.toast.error { border-left: 3px solid var(--semantic-error); }
.toast.info { border-left: 3px solid var(--semantic-info); }

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

/* ---- Loading ---- */
.loading-overlay {
  position: absolute;
  inset: 0;
  background: var(--surface-primary);
  opacity: 0.7;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
}

.spinner {
  width: 28px;
  height: 28px;
  border: 2px solid var(--border-default);
  border-top-color: var(--accent-base);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ---- Data table controls ---- */
.dt-controls {
  display: flex;
  gap: var(--space-3);
  align-items: center;
  flex-wrap: wrap;
  margin-bottom: var(--space-3);
}

.dt-controls .dt-search { flex: 1; min-width: 200px; }
.dt-controls .dt-page-size { width: 84px; }

.dt-bulk-bar {
  display: flex;
  gap: var(--space-2);
  align-items: center;
  margin-bottom: var(--space-2);
}

.dt-pagination {
  display: flex;
  gap: var(--space-2);
  align-items: center;
  margin-top: var(--space-3);
}

.dt-checkbox { width: 16px; height: 16px; cursor: pointer; }

/* ---- Empty state ---- */
.empty-state {
  text-align: center;
  padding: var(--space-6) var(--space-4);
  color: var(--foreground-tertiary);
  font-size: var(--text-sm);
}

.empty-state .empty-state-icon {
  width: 32px;
  height: 32px;
  margin-bottom: var(--space-2);
  opacity: 0.5;
}

/* ---- Layout helpers ---- */
.row {
  display: flex;
  gap: var(--space-3);
  align-items: center;
  flex-wrap: wrap;
  margin-bottom: var(--space-3);
}

.row:last-child { margin-bottom: 0; }

.muted { color: var(--foreground-muted); font-size: var(--text-sm); }
.hidden { display: none !important; }

/* ---- Modal ---- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 500;
  opacity: 0;
  transition: opacity 0.15s ease;
  padding: var(--space-4);
}

.modal-overlay.modal-visible { opacity: 1; }

.modal-panel {
  background: var(--surface-elevated);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-lg);
  width: 100%;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  transform: translateY(12px);
  transition: transform 0.15s ease;
}

.modal-visible .modal-panel { transform: translateY(0); }

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-4) var(--space-5);
  border-bottom: 1px solid var(--border-default);
  flex-shrink: 0;
}

.modal-title {
  margin: 0;
  font-size: var(--text-lg);
  font-weight: 600;
  color: var(--foreground-primary);
}

.modal-close-btn {
  background: none;
  border: none;
  font-size: 1.5rem;
  line-height: 1;
  color: var(--foreground-tertiary);
  cursor: pointer;
  padding: var(--space-1);
  height: auto;
}

.modal-close-btn:hover { color: var(--foreground-primary); }

.modal-body {
  padding: var(--space-5);
  overflow-y: auto;
  flex: 1;
}

.modal-footer {
  padding: var(--space-3) var(--space-5);
  border-top: 1px solid var(--border-default);
  display: flex;
  justify-content: flex-end;
  gap: var(--space-2);
  flex-shrink: 0;
}

/* ---- Confirm dialog ---- */
.confirm-message {
  margin: 0;
  font-size: var(--text-sm);
  color: var(--foreground-secondary);
  line-height: 1.6;
}

.confirm-actions {
  display: flex;
  justify-content: flex-end;
  gap: var(--space-2);
}

/* ---- FormBuilder ---- */
.fb-form { display: flex; flex-direction: column; gap: var(--space-4); }

.fb-field { display: flex; flex-direction: column; gap: var(--space-1); }

.fb-label {
  font-size: var(--text-sm);
  font-weight: 500;
  color: var(--foreground-secondary);
}

.fb-required { color: var(--semantic-error); }

.fb-field input,
.fb-field select,
.fb-field textarea {
  width: 100%;
}

.fb-field textarea { resize: vertical; min-height: 60px; }

.fb-help {
  margin: 0;
  font-size: var(--text-xs);
  color: var(--foreground-muted);
}

.fb-error {
  margin: 0;
  font-size: var(--text-xs);
  color: var(--semantic-error);
  min-height: 0;
}

.fb-checkbox-label {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  font-size: var(--text-sm);
  color: var(--foreground-primary);
  cursor: pointer;
}

.fb-checkbox-label input[type="checkbox"] {
  width: 16px;
  height: 16px;
  cursor: pointer;
}

.fb-tier-select {
  display: flex;
  gap: var(--space-3);
  flex-wrap: wrap;
}

.fb-tier-label {
  display: flex;
  align-items: center;
  gap: var(--space-1);
  font-size: var(--text-sm);
  color: var(--foreground-primary);
  cursor: pointer;
  white-space: nowrap;
}

.fb-tier-label input[type="checkbox"] {
  width: 16px;
  height: 16px;
  cursor: pointer;
}

.fb-status {
  margin: 0;
  font-size: var(--text-sm);
  min-height: 0;
}

.fb-status-error { color: var(--semantic-error); }

.fb-submit { align-self: flex-start; }

/* ---- Responsive modal ---- */
@media (max-width: 600px) {
  .modal-panel {
    max-width: 100% !important;
    max-height: 95vh;
    border-radius: var(--radius-lg);
  }
  .modal-body { padding: var(--space-4); }
  .modal-header, .modal-footer { padding: var(--space-3) var(--space-4); }
}

h2 { margin-top: 0; font-size: var(--text-xl); font-weight: 600; }
h3 { margin-top: 0; font-size: var(--text-lg); font-weight: 600; }

a { color: var(--accent-base); }
a:hover { color: var(--accent-hover); }

@media (max-width: 480px) {
  .dt-controls .dt-search { flex: none; width: 100%; }
}

@media (max-width: 600px) {
  .grid { grid-template-columns: repeat(2, 1fr) !important; gap: var(--space-3); }
  .row { flex-wrap: wrap; }
  table { font-size: var(--text-xs); }
  .card { padding: var(--space-3); }
}
