:root {
  --color-primary: #4f46e5;
  --color-primary-dark: #4338ca;
  --color-primary-light: #eef2ff;
  --color-bg: #f8fafc;
  --color-surface: #ffffff;
  --color-border: #e2e8f0;
  --color-text: #0f172a;
  --color-text-muted: #64748b;
  --color-success: #059669;
  --color-success-bg: #ecfdf5;
  --color-warning: #d97706;
  --color-warning-bg: #fffbeb;
  --color-danger: #dc2626;
  --color-danger-bg: #fef2f2;
  --color-neutral-bg: #f1f5f9;
  --radius: 10px;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.06);
  --shadow-md: 0 4px 16px rgba(15, 23, 42, 0.08);
  --sidebar-width: 232px;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--color-bg);
  color: var(--color-text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
}

svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  flex-shrink: 0;
}

/* ---------- Login ---------- */
.login-view {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(circle at top left, #eef2ff, #f8fafc 55%);
  padding: 24px;
}

.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: 16px;
  box-shadow: var(--shadow-md);
  padding: 36px 32px;
}

.login-brand {
  text-align: center;
  margin-bottom: 28px;
}

.login-brand h1 {
  font-size: 20px;
  margin: 12px 0 4px;
}

.login-brand p {
  margin: 0;
  color: var(--color-text-muted);
  font-size: 13px;
}

.brand-mark {
  width: 44px;
  height: 44px;
  border-radius: 12px;
  background: linear-gradient(135deg, var(--color-primary), #7c6ff0);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 18px;
  margin: 0 auto;
}

#login-form { display: flex; flex-direction: column; gap: 16px; }

/* ---------- Form elements ---------- */
label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 13px;
  font-weight: 600;
  color: var(--color-text);
}

input, select, textarea {
  font: inherit;
  padding: 9px 12px;
  border: 1px solid var(--color-border);
  border-radius: 8px;
  background: var(--color-surface);
  color: var(--color-text);
  font-weight: 400;
  width: 100%;
}

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

textarea { resize: vertical; min-height: 90px; font-family: inherit; }

.switch-label {
  flex-direction: row;
  align-items: center;
  gap: 8px;
}
.switch-label input { width: auto; }

.grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
}

.form-error {
  background: var(--color-danger-bg);
  color: var(--color-danger);
  padding: 8px 12px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 500;
}

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 9px 16px;
  border-radius: 8px;
  border: 1px solid transparent;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: background .15s ease, border-color .15s ease, color .15s ease, opacity .15s ease;
  white-space: nowrap;
}
.btn:disabled { opacity: .55; cursor: not-allowed; }

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

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

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

.btn-sm { padding: 6px 10px; font-size: 12px; }
.btn-block { width: 100%; }

/* ---------- App shell ---------- */
.app-view { display: flex; min-height: 100vh; }

.sidebar {
  width: var(--sidebar-width);
  flex-shrink: 0;
  background: var(--color-surface);
  border-right: 1px solid var(--color-border);
  display: flex;
  flex-direction: column;
  padding: 20px 14px;
  position: sticky;
  top: 0;
  height: 100vh;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 10px 22px;
  font-weight: 700;
  font-size: 16px;
}
.sidebar-brand .brand-mark { width: 32px; height: 32px; font-size: 14px; border-radius: 9px; }

#nav { display: flex; flex-direction: column; gap: 2px; flex: 1; }

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 8px;
  color: var(--color-text-muted);
  text-decoration: none;
  font-weight: 500;
  font-size: 13.5px;
}
.nav-item:hover { background: var(--color-neutral-bg); color: var(--color-text); }
.nav-item.active { background: var(--color-primary-light); color: var(--color-primary); font-weight: 600; }

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

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 32px;
  border-bottom: 1px solid var(--color-border);
  background: var(--color-surface);
  position: sticky;
  top: 0;
  z-index: 5;
}
.topbar h2 { margin: 0; font-size: 18px; }

.user-info { display: flex; align-items: center; gap: 10px; }
.user-name { font-weight: 600; font-size: 13.5px; }

.content { padding: 28px 32px; flex: 1; }

/* ---------- Cards / stats ---------- */
.welcome-banner { margin-bottom: 20px; }
.welcome-banner h3 { margin: 0 0 4px; font-size: 18px; }
.welcome-banner p { margin: 0; color: var(--color-text-muted); }

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 16px;
}

.stat-card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow-sm);
}
.stat-value { font-size: 28px; font-weight: 700; line-height: 1; margin-bottom: 6px; }
.stat-label { color: var(--color-text-muted); font-size: 13px; font-weight: 500; }
.stat-success .stat-value { color: var(--color-success); }
.stat-danger .stat-value { color: var(--color-danger); }

.card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  overflow: hidden;
}

.table-scroll {
  overflow-x: auto;
}

.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
}

.select-filter { width: auto; min-width: 180px; }

/* ---------- Table ---------- */
.table { width: 100%; border-collapse: collapse; }
.table th, .table td {
  text-align: left;
  padding: 13px 20px;
  font-size: 13.5px;
  border-bottom: 1px solid var(--color-border);
}
.table th {
  color: var(--color-text-muted);
  font-weight: 600;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .03em;
  background: #fafbfc;
}
.table tbody tr:last-child td { border-bottom: none; }
.table tbody tr:hover { background: #fafbfc; }
.table code {
  background: var(--color-neutral-bg);
  padding: 2px 6px;
  border-radius: 5px;
  font-size: 12px;
}

.row-actions { display: flex; gap: 6px; justify-content: flex-end; }

.empty-state, .error-state, .loading {
  padding: 48px 24px;
  text-align: center;
  color: var(--color-text-muted);
}
.error-state { color: var(--color-danger); }

.pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 14px 20px;
  border-top: 1px solid var(--color-border);
  font-size: 13px;
  color: var(--color-text-muted);
}

/* ---------- Badges ---------- */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 11.5px;
  font-weight: 600;
  background: var(--color-neutral-bg);
  color: var(--color-text-muted);
  white-space: nowrap;
}
.badge-success { background: var(--color-success-bg); color: var(--color-success); }
.badge-warning { background: var(--color-warning-bg); color: var(--color-warning); }
.badge-danger { background: var(--color-danger-bg); color: var(--color-danger); }
.badge-neutral { background: var(--color-neutral-bg); color: var(--color-text-muted); }
.badge-role-superadmin { background: #f5f3ff; color: #7c3aed; }
.badge-role-admin { background: var(--color-primary-light); color: var(--color-primary); }
.badge-role-user { background: var(--color-neutral-bg); color: var(--color-text-muted); }

/* ---------- Modal ---------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 20px;
  animation: fadeIn .15s ease;
}

.modal {
  background: var(--color-surface);
  border-radius: 14px;
  box-shadow: var(--shadow-md);
  width: 100%;
  max-width: 460px;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  animation: slideUp .18s ease;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 22px;
  border-bottom: 1px solid var(--color-border);
}
.modal-header h3 { margin: 0; font-size: 16px; }

.modal-close {
  background: none;
  border: none;
  font-size: 20px;
  line-height: 1;
  color: var(--color-text-muted);
  cursor: pointer;
  padding: 4px;
}

#modal-form { display: flex; flex-direction: column; min-height: 0; }

.modal-body {
  padding: 20px 22px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  overflow-y: auto;
}

.modal-error {
  margin: 0 22px 14px;
  background: var(--color-danger-bg);
  color: var(--color-danger);
  padding: 8px 12px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 500;
}

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

.detail-list { display: flex; flex-direction: column; gap: 10px; font-size: 13.5px; }
.detail-list div { display: flex; justify-content: space-between; gap: 12px; border-bottom: 1px dashed var(--color-border); padding-bottom: 8px; }
.detail-list dt { color: var(--color-text-muted); font-weight: 600; }
.detail-list dd { margin: 0; text-align: right; word-break: break-word; }

@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes slideUp { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }

/* ---------- Toasts ---------- */
.toast-root {
  position: fixed;
  bottom: 24px;
  right: 24px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  z-index: 100;
}

.toast {
  background: var(--color-text);
  color: #fff;
  padding: 12px 18px;
  border-radius: 8px;
  font-size: 13.5px;
  font-weight: 500;
  box-shadow: var(--shadow-md);
  opacity: 0;
  transform: translateY(8px);
  transition: opacity .2s ease, transform .2s ease;
  max-width: 320px;
}
.toast.show { opacity: 1; transform: translateY(0); }
.toast-error { background: var(--color-danger); }
.toast-success { background: #0f172a; }

/* ---------- Responsive ---------- */
@media (max-width: 860px) {
  .app-view { flex-direction: column; }
  .sidebar {
    width: 100%;
    height: auto;
    position: static;
    flex-direction: row;
    align-items: center;
    padding: 12px 16px;
    overflow-x: auto;
  }
  .sidebar-brand { padding: 0 12px 0 0; }
  #nav { flex-direction: row; flex: none; }
  .nav-item span, .nav-item { white-space: nowrap; }
  .sidebar-footer { border-top: none; padding-top: 0; margin-left: auto; }
  .content { padding: 20px; }
  .topbar { padding: 14px 20px; }
  .grid-2 { grid-template-columns: 1fr; }
}
