:root {
  --brand: #1e3a8a;
  --brand-dark: #d97706;
  --brand-light: #fef3c7;
  --accent: #1e3a8a;
  --sidebar-bg: #2b2b2b;
  --sidebar-text: #cbd5e1;
  --sidebar-active: #1e3a8a;
  --bg: #f1f5f9;
  --surface: #ffffff;
  --border: #e2e8f0;
  --text: #1e293b;
  --text-muted: #64748b;
  --success: #16a34a;
  --success-bg: #dcfce7;
  --warning: #d97706;
  --warning-bg: #fef3c7;
  --danger: #dc2626;
  --danger-bg: #fee2e2;
  --info: #2563eb;
  --info-bg: #dbeafe;
  --muted: #6b7280;
  --muted-bg: #f3f4f6;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(15, 23, 42, .08), 0 1px 2px rgba(15, 23, 42, .06);
  --shadow-lg: 0 10px 24px rgba(15, 23, 42, .12);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 14px;
  color: var(--text);
  background: var(--bg);
  line-height: 1.5;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

h1, h2, h3, h4 { color: var(--text); margin: 0 0 .5rem; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.25rem; }
h3 { font-size: 1.05rem; }

/* ─── App shell ─────────────────────────────────────────── */
.app-shell { display: flex; min-height: 100vh; }

.sidebar {
  width: 250px;
  background: var(--sidebar-bg);
  color: var(--sidebar-text);
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  z-index: 50;
  transition: transform .25s ease;
  overflow-y: auto;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: .6rem;
  padding: 1.1rem 1.2rem;
  color: #fff;
  font-weight: 700;
  font-size: 1rem;
  border-bottom: 1px solid rgba(255,255,255,.08);
  text-decoration: none !important;
}

.sidebar-brand:hover { color: #fff; }

.brand-badge {
  width: 34px;
  height: 34px;
  border-radius: 8px;
  background: linear-gradient(135deg, var(--accent), var(--brand));
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: .85rem;
  color: #fff;
  flex-shrink: 0;
}

.sidebar .nav { padding: .8rem .65rem; flex: 1; }

.sidebar .nav-section-label {
  font-size: .66rem;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: #94a3b8;
  padding: .55rem .75rem .35rem;
}

.sidebar .nav-link {
  display: flex;
  align-items: center;
  gap: .65rem;
  padding: .62rem .75rem;
  color: var(--sidebar-text);
  border-radius: 8px;
  margin-bottom: 2px;
  font-size: .92rem;
  transition: background .15s, color .15s;
}

.sidebar .nav-link:hover { background: var(--sidebar-active); color: #fff; text-decoration: none; }
.sidebar .nav-link.active {
  background: var(--sidebar-active);
  color: #fff;
  box-shadow: inset 3px 0 0 #60a5fa;
}
.sidebar .nav-link svg { width: 18px; height: 18px; flex-shrink: 0; opacity: .85; }

.sidebar-footer { padding: 1rem 1.2rem; border-top: 1px solid rgba(255,255,255,.08); font-size: .8rem; color: #64748b; }

.main {
  flex: 1;
  margin-left: 250px;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

/* ─── Topbar ────────────────────────────────────────────── */
.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: .9rem 1.5rem;
  display: flex;
  align-items: center;
  gap: 1rem;
  position: sticky;
  top: 0;
  z-index: 40;
}

.hamburger {
  display: none;
  background: none;
  border: 1px solid var(--border);
  border-radius: 8px;
  width: 38px;
  height: 38px;
  cursor: pointer;
  align-items: center;
  justify-content: center;
  color: var(--text);
}

.topbar-title { font-size: 1.15rem; font-weight: 600; }

.topbar-spacer { flex: 1; }

.topbar-controls {
  display: flex;
  align-items: center;
  gap: .75rem;
}

.topbar-logout { margin: 0; }

.topbar-control {
  display: flex;
  align-items: center;
  gap: .4rem;
}

.topbar-control label {
  color: var(--text-muted);
  font-size: .82rem;
  white-space: nowrap;
}

.topbar-control select {
  max-width: 16rem;
  padding: .35rem .5rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  font-size: .82rem;
}

.user-chip {
  display: flex;
  align-items: center;
  gap: .6rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: .3rem .5rem .3rem .3rem;
  color: inherit;
  text-decoration: none;
  transition: border-color .15s ease;
}
.user-chip:hover { border-color: var(--primary); }

.user-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), var(--brand));
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: .8rem;
}
.user-avatar-image { object-fit: cover; flex-shrink: 0; }
.profile-image-preview {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  object-fit: cover;
}

.user-chip .user-meta { line-height: 1.15; }
.user-chip .user-meta .name { font-weight: 600; font-size: .85rem; }
.user-chip .user-meta .role { font-size: .72rem; color: var(--text-muted); }

/* Role switcher (Super Administrator) */
.role-switcher { display: flex; align-items: center; gap: .45rem; }
.role-switcher-label {
  font-size: .68rem;
  font-weight: 600;
  letter-spacing: .05em;
  text-transform: uppercase;
  color: var(--text-muted);
  white-space: nowrap;
}
.role-switcher .form-select {
  max-width: 230px;
  padding: .35rem .6rem;
  font-size: .78rem;
}

@media (max-width: 1100px) {
  .role-switcher-label { display: none; }
  .role-switcher .form-select { max-width: 170px; }
}

@media (max-width: 760px) {
  .user-chip .user-meta { display: none; }
  .role-switcher .form-select { max-width: 132px; }
}

.content { padding: 1.5rem; flex: 1; }

/* ─── Cards ─────────────────────────────────────────────── */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-bottom: 1.25rem;
}

.card-header {
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .75rem;
  flex-wrap: wrap;
}

.card-header h3 { margin: 0; font-size: 1rem; }

.card-body { padding: 1.25rem; }

.card-title { font-size: .95rem; font-weight: 600; margin-bottom: .75rem; }

/* ─── Stat cards ────────────────────────────────────────── */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 1rem;
  margin-bottom: 1.5rem;
}

.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.1rem 1.25rem;
  box-shadow: var(--shadow);
  display: flex;
  align-items: center;
  gap: 1rem;
}

.stat-icon {
  width: 46px;
  height: 46px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.stat-icon svg { width: 22px; height: 22px; }

.stat-icon.brand { background: var(--brand-light); color: var(--brand); }
.stat-icon.success { background: var(--success-bg); color: var(--success); }
.stat-icon.warning { background: var(--warning-bg); color: var(--warning); }
.stat-icon.danger { background: var(--danger-bg); color: var(--danger); }
.stat-icon.info { background: var(--info-bg); color: var(--info); }
.stat-icon.muted { background: var(--muted-bg); color: var(--muted); }

.stat-value { font-size: 1.5rem; font-weight: 700; line-height: 1.1; }
.stat-label { font-size: .8rem; color: var(--text-muted); }

/* ─── Buttons ───────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: .45rem;
  padding: .5rem .95rem;
  border-radius: 8px;
  border: 1px solid transparent;
  font-size: .88rem;
  font-weight: 550;
  cursor: pointer;
  text-decoration: none !important;
  transition: background .15s, border-color .15s, color .15s;
  line-height: 1.2;
}

.btn svg { width: 15px; height: 15px; }
.btn:disabled { opacity: .55; cursor: not-allowed; }

.btn-primary { background: var(--brand); color: #fff; }
.btn-primary:hover { background: var(--brand-dark); }

.btn-accent { background: var(--accent); color: #fff; }
.btn-accent:hover { background: var(--brand-dark); }

.btn-success { background: var(--success); color: #fff; }
.btn-success:hover { background: #15803d; }

.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { background: #b91c1c; }

.btn-outline { background: transparent; border-color: var(--border); color: var(--text); }
.btn-outline:hover { background: var(--bg); }

.btn-outline-primary { background: transparent; border-color: var(--brand); color: var(--brand); }
.btn-outline-primary:hover { background: var(--brand-light); }

.btn-outline-danger { background: transparent; border-color: var(--danger); color: var(--danger); }
.btn-outline-danger:hover { background: var(--danger-bg); }

.btn-ghost { background: transparent; color: var(--text-muted); border-color: transparent; }
.btn-ghost:hover { background: var(--bg); color: var(--text); }

.btn-sm { padding: .3rem .65rem; font-size: .8rem; }
.btn-block { width: 100%; }

/* ─── Badges ────────────────────────────────────────────── */
.badge {
  display: inline-flex;
  align-items: center;
  gap: .35rem;
  padding: .22rem .6rem;
  border-radius: 999px;
  font-size: .74rem;
  font-weight: 600;
  letter-spacing: .01em;
  white-space: nowrap;
}

.badge-success { background: var(--success-bg); color: var(--success); }
.badge-warning { background: var(--warning-bg); color: var(--warning); }
.badge-danger { background: var(--danger-bg); color: var(--danger); }
.badge-info { background: var(--info-bg); color: var(--info); }
.badge-muted { background: var(--muted-bg); color: var(--muted); }
.badge-brand { background: var(--brand-light); color: var(--brand); }

/* ─── Alerts / flash ────────────────────────────────────── */
.alert {
  display: flex;
  align-items: flex-start;
  gap: .6rem;
  padding: .85rem 1rem;
  border-radius: 8px;
  margin-bottom: 1.1rem;
  font-size: .9rem;
  border: 1px solid transparent;
}

.alert-success { background: var(--success-bg); color: #14532d; border-color: #bbf7d0; }
.alert-error { background: var(--danger-bg); color: #7f1d1d; border-color: #fecaca; }
.alert-info { background: var(--info-bg); color: #2563eb; border-color: #bfdbfe; }
.alert-warning { background: var(--warning-bg); color: #92400e; border-color: #fde68a; }

/* ─── Tables ────────────────────────────────────────────── */
.table-responsive { overflow-x: auto; }

.table {
  width: 100%;
  border-collapse: collapse;
  font-size: .88rem;
}

.table th, .table td {
  padding: .4rem .9rem;
  text-align: left;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

.table th {
  background: #16a34a;
  font-size: .74rem;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: #fff;
  font-weight: 600;
  white-space: nowrap;
}

.table tbody tr:hover { background: #f8fafc; }
.table tbody tr:last-child td { border-bottom: none; }
.table tbody tr.row-selected { background: #eff6ff; }
.table tbody tr.row-disabled { opacity: .5; }

.table .actions { display: flex; gap: .35rem; flex-wrap: wrap; }

/* ─── Forms ─────────────────────────────────────────────── */
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 1rem; }
.form-group { margin-bottom: 1rem; }
.form-label { display: block; margin-bottom: .35rem; font-size: .84rem; font-weight: 600; color: var(--text); }
.form-control, .form-select {
  width: 100%;
  padding: .55rem .8rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: .9rem;
  background: #fff;
  color: var(--text);
  transition: border-color .15s, box-shadow .15s;
}
.form-control:focus, .form-select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(67, 56, 202, .12);
}
.form-control.is-invalid, .form-select.is-invalid { border-color: var(--danger); }
.form-error { color: var(--danger); font-size: .78rem; margin-top: .3rem; }
.form-help { color: var(--text-muted); font-size: .78rem; margin-top: .3rem; }
.invalid-feedback { color: var(--danger); font-size: .78rem; }
textarea.form-control { min-height: 90px; resize: vertical; }

/* ─── Progress bars ─────────────────────────────────────── */
.progress {
  height: 8px;
  background: var(--border);
  border-radius: 999px;
  overflow: hidden;
  min-width: 90px;
}

.progress-bar { height: 100%; border-radius: 999px; }
.progress-bar.low { background: var(--success); }
.progress-bar.mid { background: var(--warning); }
.progress-bar.full { background: var(--danger); }

/* ─── Pagination ────────────────────────────────────────── */
.pagination { display: flex; justify-content: flex-end; gap: .35rem; margin-top: 1.1rem; flex-wrap: wrap; }
.pagination a, .pagination span {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 34px;
  height: 34px;
  padding: 0 .6rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
  color: var(--text);
  font-size: .85rem;
  text-decoration: none !important;
}
.pagination .active-page { background: var(--brand); color: #fff; border-color: var(--brand); }
.pagination .disabled { opacity: .5; pointer-events: none; }
.pagination a:hover:not(.active-page) { background: var(--bg); }

/* ─── Filters bar ───────────────────────────────────────── */
.filters-bar {
  display: flex;
  gap: .75rem;
  flex-wrap: wrap;
  align-items: flex-end;
}
.filters-bar .form-group { margin-bottom: 0; min-width: 160px; flex: 1; }
.filters-bar .form-group.narrow { flex: 0 0 auto; min-width: 130px; }
.search-wrap { position: relative; flex: 2; }
.search-wrap svg { position: absolute; left: .7rem; top: 50%; transform: translateY(-50%); width: 15px; height: 15px; color: var(--text-muted); }
.search-wrap input { padding-left: 2.1rem; }

/* ─── Modal ─────────────────────────────────────────────── */
.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(15, 23, 42, .5);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 1rem;
}
.modal-backdrop.show { display: flex; }
.modal {
  background: #fff;
  border-radius: 12px;
  max-width: 460px;
  width: 100%;
  box-shadow: var(--shadow-lg);
  overflow: hidden;
}
.modal-header { padding: 1.1rem 1.3rem; border-bottom: 1px solid var(--border); display: flex; align-items: center; justify-content: space-between; }
.modal-header h3 { margin: 0; font-size: 1.05rem; }
.modal-body { padding: 1.3rem; color: var(--text-muted); }
.modal-footer { padding: 1rem 1.3rem; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: .6rem; }
.modal-close { background: none; border: none; font-size: 1.4rem; line-height: 1; cursor: pointer; color: var(--text-muted); }

/* ─── Selection list ────────────────────────────────────── */
.selection-item {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 1rem;
  align-items: center;
  padding: .9rem 1.1rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  margin-bottom: .7rem;
  background: #fff;
}

.selection-index {
  width: 30px; height: 30px;
  border-radius: 50%;
  background: var(--brand-light);
  color: var(--brand);
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: .85rem;
}

/* ─── Meta panel ────────────────────────────────────────── */
.meta-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.2rem;
  box-shadow: var(--shadow);
}

.meta-row { display: flex; justify-content: space-between; padding: .45rem 0; border-bottom: 1px solid var(--border); font-size: .88rem; }
.meta-row:last-child { border-bottom: none; }
.meta-row .label { color: var(--text-muted); }
.meta-row .value { font-weight: 600; }

/* ─── Empty state ───────────────────────────────────────── */
.empty-state {
  text-align: center;
  padding: 2.5rem 1rem;
  color: var(--text-muted);
}
.empty-state svg { width: 42px; height: 42px; opacity: .4; margin-bottom: .6rem; }
.empty-state p { margin: 0 0 .5rem; }

/* ─── Auth page ─────────────────────────────────────────── */
.auth-body {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #1e3a8a 0%, #f59e0b 50%, #d97706 100%);
  padding: 1.5rem;
}
.auth-card {
  background: #fff;
  border-radius: 16px;
  box-shadow: var(--shadow-lg);
  width: 100%;
  max-width: 400px;
  padding: 2.2rem;
}
.auth-brand {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: .7rem;
  margin-bottom: .4rem;
}
.auth-brand .brand-badge { width: 46px; height: 46px; border-radius: 12px; font-size: 1.05rem; }
.auth-title { text-align: center; font-weight: 700; color: var(--brand); font-size: 1.15rem; margin-bottom: .15rem; }
.auth-subtitle { text-align: center; color: var(--text-muted); font-size: .85rem; margin-bottom: 1.6rem; }
.auth-foot { text-align: center; color: #94a3b8; font-size: .78rem; margin-top: 1.4rem; }

/* ─── Utilities ─────────────────────────────────────────── */
.flex { display: flex; gap: .75rem; align-items: center; flex-wrap: wrap; }
.flex-between { display: flex; justify-content: space-between; align-items: center; gap: .75rem; flex-wrap: wrap; }
.mt-0 { margin-top: 0; } .mt-1 { margin-top: .5rem; } .mt-2 { margin-top: 1rem; } .mt-3 { margin-top: 1.5rem; }
.mb-0 { margin-bottom: 0; } .mb-1 { margin-bottom: .5rem; } .mb-2 { margin-bottom: 1rem; }
.text-muted { color: var(--text-muted); }
.text-success { color: var(--success); }
.text-danger { color: var(--danger); }
.text-warning { color: var(--warning); }
.text-small { font-size: .8rem; }
.font-semibold { font-weight: 600; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .84rem; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 1.25rem; }
@media (max-width: 900px) { .grid-2 { grid-template-columns: 1fr; } }

/* ─── Print ─────────────────────────────────────────────── */
@media print {
  .sidebar, .topbar, .no-print, .topbar-title, .topbar-spacer { display: none !important; }
  .main { margin-left: 0; }
  .content { padding: 0; }
  .card { border: none; box-shadow: none; }
}

/* ─── Responsive ────────────────────────────────────────── */
@media (max-width: 860px) {
  .sidebar { transform: translateX(-100%); }
  .sidebar.open { transform: translateX(0); box-shadow: var(--shadow-lg); }
  .main { margin-left: 0; }
  .hamburger { display: inline-flex; }
  .topbar { flex-wrap: wrap; }
  .topbar-title { flex: 1; }
  .topbar-spacer { display: none; }
  .topbar-logout { order: 3; }
  .topbar-controls {
    order: 4;
    flex: 1 0 100%;
    flex-wrap: wrap;
    padding-left: 38px;
  }
  .topbar-control { flex: 1 1 12rem; }
  .topbar-control select { min-width: 0; max-width: 100%; }
}

/* ---- Sortable table headers ------------------------------------------ */
.table th.th-sort .th-sort-link {
  display: inline-flex;
  align-items: center;
  gap: .3rem;
  color: inherit;
  text-decoration: none;
}
.table th.th-sort .th-sort-link:hover { text-decoration: underline; }
.table th.th-sort .th-arrow { font-size: .65rem; line-height: 1; opacity: .55; }
.table th.th-sort .th-sort-link:hover .th-arrow { opacity: .85; }
.table th.th-sort .th-sort-link.is-active { font-weight: 700; }
.table th.th-sort .th-sort-link.is-active .th-arrow { opacity: 1; }

/* --- Sidebar dropdown groups ----------------------------- */
.nav-group { margin-bottom: .2rem; }
.nav-group-toggle {
  display: flex;
  align-items: center;
  gap: .5rem;
  padding: .6rem .75rem;
  color: #aebccd;
  border-radius: 8px;
  cursor: pointer;
  list-style: none;
  font-size: .72rem;
  font-weight: 700;
  letter-spacing: .055em;
  text-transform: uppercase;
  user-select: none;
}
.nav-group-toggle::-webkit-details-marker { display: none; }
.nav-group-toggle::after {
  content: '';
  width: .4rem;
  height: .4rem;
  border-right: 1.5px solid currentColor;
  border-bottom: 1.5px solid currentColor;
  transform: rotate(-45deg);
  margin-left: auto;
  transition: transform .15s ease;
  opacity: .7;
}
.nav-group[open] > .nav-group-toggle::after { transform: rotate(45deg); }
.nav-group-toggle:hover,
.nav-group[open] > .nav-group-toggle { background: rgba(255,255,255,.06); color: #fff; }
.nav-group-items {
  margin: .15rem 0 .45rem .7rem;
  padding-left: .5rem;
  border-left: 1px solid rgba(255,255,255,.12);
}
.nav-group-items .nav-link {
  margin-left: .15rem;
  padding: .55rem .65rem;
  font-size: .87rem;
}
.nav-group-toggle:focus-visible,
.sidebar .nav-link:focus-visible {
  outline: 2px solid #93c5fd;
  outline-offset: 2px;
}
