/**
 * KABANEK Admin — design system (SPEC FR8)
 * Loaded by index.php, audit.php, invite.php
 */
:root {
  --brand-primary: #0a3d1f;
  --brand-dark: #052413;
  --brand-gold: #d4b87a;
  --brand-gold-dark: #b8985a;
  --brand-pale: #f4f2ec;
  --bg-card: #1b2c14;
  --text-primary: #e8f0e2;
  --text-muted: #7a9a6e;
  --border: #2d5016;
  --error: #ef5350;
  --success: #4caf50;
  --warning: #ff9800;
  --topbar-h: 56px;
  --font: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  /* Legacy aliases (inline templates / older markup) */
  --bg: var(--brand-dark);
  --s1: var(--bg-card);
  --s2: #142218;
  --s3: #1a3018;
  --bd: var(--border);
  --tx: var(--text-primary);
  --tx2: #8aab7a;
  --tx3: var(--text-muted);
  --gr: var(--success);
  --grd: #3d9e46;
  --grb: rgba(76, 175, 80, 0.12);
  --red: var(--error);
  --gold: var(--brand-gold);
  --blue: #5ba3d9;
  --purple: #9b7fd4;
  --orng: #e8a565;
}

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

html {
  -webkit-text-size-adjust: 100%;
}

body.admin-app {
  font-family: var(--font);
  background: var(--brand-dark);
  color: var(--text-primary);
  min-height: 100vh;
  line-height: 1.45;
}

/* C3 — focus klawiatury (WCAG) */
.admin-app a:focus-visible,
.admin-app button:focus-visible,
.admin-app input:focus-visible,
.admin-app select:focus-visible,
.admin-app textarea:focus-visible,
.admin-app summary:focus-visible,
.admin-app [role="button"]:focus-visible {
  outline: 2px solid var(--brand-gold);
  outline-offset: 2px;
}

.admin-app .drawer-toggle:focus-visible,
.admin-app .modal-close:focus-visible {
  outline-offset: 3px;
}

/* ---------- Login (FR1) ---------- */
body.admin-login {
  font-family: var(--font);
  min-height: 100vh;
  margin: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  color: var(--text-primary);
  background: var(--brand-dark);
  background-image: radial-gradient(ellipse 120% 80% at 0% 0%, #0a3d1f 0%, transparent 55%),
    linear-gradient(180deg, var(--brand-dark), #031208);
}

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

.admin-login .login-brand-logo {
  font-size: 28px;
  font-weight: 800;
  color: var(--brand-gold);
  letter-spacing: 0.02em;
}

.admin-login .login-brand-sub {
  font-size: 13px;
  color: var(--text-muted);
  margin-top: 4px;
}

.admin-login .login-card {
  width: 100%;
  max-width: 420px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 32px;
  box-shadow: 0 24px 64px rgba(0, 0, 0, 0.45);
}

.admin-login .login-card h1 {
  font-size: 18px;
  font-weight: 700;
  margin-bottom: 6px;
  color: var(--text-primary);
}

.admin-login .login-card .sub {
  font-size: 13px;
  color: var(--text-muted);
  margin-bottom: 24px;
}

.admin-login .f {
  margin-bottom: 16px;
}

.admin-login .f label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 6px;
}

.admin-login .f input {
  width: 100%;
  background: var(--brand-dark);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
  color: var(--text-primary);
  font-size: 16px;
  font-family: inherit;
  outline: none;
  min-height: 44px;
}

.admin-login .f input:focus {
  border-color: var(--brand-gold);
  box-shadow: 0 0 0 2px rgba(212, 184, 122, 0.25);
}

.admin-login .login-pw-row {
  display: flex;
  gap: 8px;
  align-items: stretch;
}

.admin-login .login-pw-row input {
  flex: 1;
}

.admin-login .btn-login-pw-toggle {
  flex-shrink: 0;
  min-width: 48px;
  padding: 0 10px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--s2);
  color: var(--brand-gold);
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  font-family: inherit;
}

.admin-login .login-pw-hint {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 6px;
  line-height: 1.4;
}

.admin-login .btn-login {
  width: 100%;
  margin-top: 8px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  background: var(--brand-gold);
  color: var(--brand-dark);
  border: none;
  border-radius: 12px;
  padding: 14px;
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
  font-family: inherit;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.25);
  min-height: 48px;
  touch-action: manipulation;
}

.admin-login .btn-login:hover {
  background: var(--brand-gold-dark);
}

.admin-login .err-toast {
  background: rgba(239, 83, 80, 0.15);
  color: var(--error);
  padding: 12px 14px;
  border-radius: 10px;
  font-size: 13px;
  margin-bottom: 16px;
  font-weight: 600;
  border: 1px solid rgba(239, 83, 80, 0.35);
  animation: admin-shake 0.42s ease-in-out;
}

@keyframes admin-shake {
  0%,
  100% {
    transform: translateX(0);
  }
  20% {
    transform: translateX(-6px);
  }
  40% {
    transform: translateX(6px);
  }
  60% {
    transform: translateX(-4px);
  }
  80% {
    transform: translateX(4px);
  }
}

.admin-login .login-footer {
  display: block;
  text-align: center;
  margin-top: 22px;
  color: var(--text-muted);
  font-size: 12px;
}

.admin-login .login-footer a {
  color: var(--brand-gold);
  text-decoration: none;
}

.admin-login .login-footer a:hover {
  text-decoration: underline;
}

/* ---------- Typography / buttons ---------- */
.btn-primary,
.btn.btn-primary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  background: var(--brand-gold);
  color: var(--brand-dark);
  border: none;
  border-radius: 12px;
  padding: 12px 18px;
  font-weight: 700;
  font-size: 14px;
  font-family: inherit;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
  text-decoration: none;
  touch-action: manipulation;
}

.btn-primary:hover {
  background: var(--brand-gold-dark);
}

.btn-secondary,
.btn.btn-secondary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  background: var(--s2);
  border: 1px solid var(--border);
  color: var(--tx2);
  border-radius: 10px;
  padding: 10px 16px;
  font-weight: 700;
  font-size: 13px;
  font-family: inherit;
  cursor: pointer;
  text-decoration: none;
  touch-action: manipulation;
}

.btn-secondary:hover {
  background: var(--s3);
  color: var(--tx);
}

.btn-danger,
.btn.btn-danger {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: rgba(239, 83, 80, 0.12);
  color: var(--error);
  border: 1px solid rgba(239, 83, 80, 0.45);
  border-radius: 10px;
  padding: 10px 16px;
  font-weight: 700;
  font-size: 13px;
  font-family: inherit;
  cursor: pointer;
  touch-action: manipulation;
}

.btn-danger:hover {
  background: rgba(239, 83, 80, 0.2);
}

/* ---------- Topbar (FR2) ---------- */
.admin-topbar {
  position: sticky;
  top: 0;
  z-index: 80;
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  min-height: var(--topbar-h);
  padding: 10px 18px;
  background: linear-gradient(90deg, var(--brand-primary), var(--brand-dark));
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.35);
  border-bottom: 1px solid rgba(45, 80, 22, 0.5);
}

.admin-topbar-brand {
  font-size: 15px;
  font-weight: 800;
  color: var(--brand-gold);
  letter-spacing: 0.03em;
  white-space: nowrap;
}

.admin-topbar-nav {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  flex: 1;
  justify-content: center;
}

.admin-topbar-nav a {
  padding: 8px 14px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 700;
  text-decoration: none;
  color: rgba(244, 242, 236, 0.85);
  border: 1px solid transparent;
  background: rgba(0, 0, 0, 0.15);
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}

.admin-topbar-nav a:hover {
  background: rgba(255, 255, 255, 0.08);
  color: var(--brand-pale);
}

.admin-topbar-nav a.is-active {
  background: var(--brand-gold);
  color: var(--brand-dark);
  border-color: var(--brand-gold-dark);
}

.admin-topbar-user {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-left: auto;
}

.admin-topbar-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 800;
  color: var(--brand-pale);
  background: linear-gradient(135deg, var(--brand-primary), var(--brand-dark));
  border: 2px solid var(--brand-gold);
  flex-shrink: 0;
}

.admin-topbar-meta {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.admin-topbar-meta .email {
  font-size: 11px;
  color: rgba(244, 242, 236, 0.75);
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.admin-topbar-meta .role-pill {
  font-size: 9px;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--brand-dark);
  background: var(--brand-gold);
  padding: 2px 8px;
  border-radius: 999px;
  width: fit-content;
}

.admin-topbar-user .btn-logout {
  padding: 8px 12px;
  font-size: 12px;
}

.drawer-toggle {
  display: none;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  background: rgba(0, 0, 0, 0.2);
  color: var(--brand-gold);
  font-size: 20px;
  cursor: pointer;
  flex-shrink: 0;
}

.drawer-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  z-index: 55;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
}

/* ---------- Layout: sidebar + main ---------- */
.admin-layout {
  display: grid;
  grid-template-columns: 320px 1fr;
  min-height: calc(100vh - var(--topbar-h));
}

.admin-sidebar {
  background: var(--bg-card);
  border-right: 1px solid var(--border);
  padding: 16px;
  overflow-y: auto;
  max-height: calc(100vh - var(--topbar-h));
  min-height: 0;
}

.admin-sidebar h2.section-title {
  font-size: 11px;
  color: var(--text-muted);
  letter-spacing: 1.5px;
  font-weight: 800;
  margin-bottom: 10px;
}

.admin-main {
  padding: 24px 24px 100px;
  overflow-y: auto;
  max-height: calc(100vh - var(--topbar-h));
  /* Grid item: default min-height:auto blocks max-height scroll when content is tall (perm tree). */
  min-height: 0;
}

/* ---------- Cards ---------- */
.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 20px;
  margin-bottom: 20px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.2);
}

.card h3,
.card .card-title {
  font-size: 13px;
  color: var(--brand-gold);
  letter-spacing: 1.2px;
  font-weight: 800;
  margin-bottom: 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
}

/* ---------- User list cards (FR3) ---------- */
.user-search {
  width: 100%;
  background: var(--s2);
  border: 1px solid var(--border);
  color: var(--text-primary);
  padding: 10px 12px;
  border-radius: 10px;
  font-size: 14px;
  font-family: inherit;
  outline: none;
  margin-bottom: 12px;
}

.user-search:focus {
  border-color: var(--brand-gold);
  box-shadow: 0 0 0 2px rgba(212, 184, 122, 0.2);
}

.btn-add-user {
  width: 100%;
  margin-bottom: 12px;
}

.user-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.user-item {
  padding: 12px 14px;
  background: var(--s2);
  border: 1px solid var(--border);
  border-radius: 12px;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, box-shadow 0.15s;
  display: flex;
  align-items: center;
  gap: 12px;
}

.user-item:hover {
  background: var(--s3);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}

.user-item.selected {
  border-color: var(--brand-primary);
  border-left: 4px solid var(--brand-primary);
  background: rgba(10, 61, 31, 0.35);
  box-shadow: inset 0 0 0 1px rgba(212, 184, 122, 0.12);
}

.user-item .ava {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 13px;
  flex-shrink: 0;
  color: var(--brand-pale);
  background: linear-gradient(135deg, var(--brand-primary), var(--brand-dark));
  border: 2px solid rgba(212, 184, 122, 0.35);
}

.user-item.selected .ava {
  border-color: var(--brand-gold);
}

.user-item .info {
  flex: 1;
  min-width: 0;
}

.user-item .nm {
  font-size: 14px;
  font-weight: 700;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.user-item .em {
  font-size: 11px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.user-item .meta-line {
  font-size: 10px;
  color: var(--text-muted);
  margin-top: 3px;
}

.user-item .chev {
  color: var(--brand-gold);
  font-size: 18px;
  font-weight: 700;
  opacity: 0.7;
}

.badge-role {
  font-size: 9px;
  padding: 2px 8px;
  border-radius: 999px;
  font-weight: 800;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  flex-shrink: 0;
}

.badge-role.admin {
  background: rgba(212, 184, 122, 0.25);
  color: var(--brand-gold);
  border: 1px solid rgba(212, 184, 122, 0.4);
}

.badge-role.user {
  background: rgba(255, 255, 255, 0.06);
  color: var(--tx2);
  border: 1px solid var(--border);
}

.badge-role.zoo {
  background: rgba(155, 127, 212, 0.2);
  color: #c9b3e8;
  border: 1px solid rgba(155, 127, 212, 0.45);
}

.badge-role.inactive {
  background: rgba(239, 83, 80, 0.12);
  color: var(--error);
  border: 1px solid rgba(239, 83, 80, 0.35);
}

/* ---------- Detail header (FR4) ---------- */
.detail-header {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  align-items: center;
  margin-bottom: 16px;
}

.detail-header .big-ava {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 22px;
  font-weight: 800;
  color: var(--brand-pale);
  background: linear-gradient(135deg, var(--brand-primary), var(--brand-dark));
  border: 3px solid var(--brand-gold);
}

.detail-header .titles {
  flex: 1;
  min-width: 0;
}

.detail-header .titles h2 {
  font-size: 20px;
  font-weight: 800;
  color: var(--text-primary);
}

.detail-header .titles .email {
  font-size: 13px;
  color: var(--text-muted);
}

.detail-header .badge-role.large {
  font-size: 11px;
  padding: 6px 12px;
}

.detail-stat {
  font-size: 13px;
  color: var(--tx2);
  margin-bottom: 14px;
  padding: 10px 14px;
  background: var(--s2);
  border: 1px solid var(--border);
  border-radius: 10px;
}

/* ---------- Permissions tree (existing class names) ---------- */
.perm-tree {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.dept {
  background: var(--s2);
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
}

.dept-head {
  padding: 14px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  cursor: pointer;
  background: linear-gradient(135deg, var(--s2), var(--s1));
  transition: background 0.15s;
}

.dept-head:hover {
  background: var(--s3);
}

.dept-head .title {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1;
}

.dept-head .icon {
  font-size: 22px;
}

.dept-head .name {
  font-size: 14px;
  font-weight: 800;
  color: var(--tx);
}

.dept-head .count {
  font-size: 10px;
  color: var(--tx3);
  background: var(--s3);
  padding: 3px 8px;
  border-radius: 10px;
  font-weight: 700;
}

.dept-head .arrow {
  color: var(--tx3);
  transition: transform 0.2s;
}

.dept.open .dept-head .arrow {
  transform: rotate(90deg);
  color: var(--brand-gold);
}

.dept-body {
  display: none;
  padding: 0 12px 12px;
  border-top: 1px solid var(--border);
}

.dept.open .dept-body {
  display: block;
}

.dept-actions,
.mod-actions-row {
  display: flex;
  gap: 6px;
  padding: 10px 0;
  flex-wrap: wrap;
}

.mini-btn {
  background: var(--s3);
  border: 1px solid var(--border);
  color: var(--tx2);
  padding: 6px 10px;
  border-radius: 8px;
  font-size: 10px;
  font-family: inherit;
  font-weight: 700;
  cursor: pointer;
  letter-spacing: 0.03em;
  touch-action: manipulation;
}

.mini-btn:hover {
  background: var(--border);
  color: var(--tx);
}

.mini-btn.r {
  color: var(--blue);
  border-color: rgba(91, 163, 217, 0.45);
}

.mini-btn.e {
  color: var(--gold);
  border-color: rgba(212, 184, 122, 0.45);
}

.mini-btn.d {
  color: var(--red);
  border-color: rgba(239, 83, 80, 0.45);
}

.mini-btn.clear {
  color: var(--tx3);
}

.mod {
  background: var(--s1);
  border: 1px solid var(--border);
  border-radius: 10px;
  margin-bottom: 8px;
  overflow: hidden;
}

.mod-head {
  padding: 10px 14px;
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  transition: background 0.15s;
}

.mod-head:hover {
  background: var(--s2);
}

.mod-head .ic {
  font-size: 16px;
  flex-shrink: 0;
}

.mod-head .nm {
  flex: 1;
  font-size: 13px;
  font-weight: 700;
  color: var(--tx);
}

.mod-head .perm-chips {
  display: flex;
  gap: 4px;
}

.pchip {
  font-size: 9px;
  padding: 2px 6px;
  border-radius: 4px;
  font-weight: 800;
  letter-spacing: 0.03em;
}

.pchip.R {
  background: rgba(91, 163, 217, 0.18);
  color: var(--blue);
}

.pchip.E {
  background: rgba(212, 184, 122, 0.18);
  color: var(--gold);
}

.pchip.D {
  background: rgba(239, 83, 80, 0.18);
  color: var(--red);
}

.pchip.off {
  background: var(--s3);
  color: var(--tx3);
}

.mod.open .mod-head {
  background: var(--s2);
  border-bottom: 1px solid var(--border);
}

.mod-body {
  display: none;
  padding: 10px 14px;
}

.mod.open .mod-body {
  display: block;
}

.tab-table {
  width: 100%;
  border-collapse: collapse;
}

.tab-table th {
  text-align: left;
  padding: 6px 8px;
  font-size: 9px;
  color: var(--tx3);
  letter-spacing: 1px;
  font-weight: 800;
  border-bottom: 1px solid var(--border);
}

.tab-table th.c {
  text-align: center;
  width: 52px;
}

.tab-table td {
  padding: 8px;
  border-bottom: 1px solid rgba(45, 80, 22, 0.25);
  font-size: 12px;
}

.tab-table td.c {
  text-align: center;
}

/* Colored square checkboxes (FR4) */
.perm-sq {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border-radius: 6px;
  cursor: pointer;
  border: 2px solid transparent;
  vertical-align: middle;
}

.perm-sq input {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}

.perm-sq span {
  display: block;
  width: 18px;
  height: 18px;
  border-radius: 4px;
  border: 2px solid rgba(255, 255, 255, 0.25);
  background: var(--s3);
  transition: transform 0.12s, box-shadow 0.12s;
}

.perm-sq-r span {
  border-color: var(--blue);
  background: rgba(91, 163, 217, 0.25);
}

.perm-sq-e span {
  border-color: var(--gold);
  background: rgba(212, 184, 122, 0.25);
}

.perm-sq-d span {
  border-color: var(--red);
  background: rgba(239, 83, 80, 0.22);
}

.perm-sq input:focus-visible + span {
  box-shadow: 0 0 0 2px var(--brand-gold);
}

.perm-sq input:checked + span {
  transform: scale(1.08);
  box-shadow: 0 0 0 2px rgba(212, 184, 122, 0.35);
}

.tab-row.module-row td {
  background: rgba(10, 61, 31, 0.25);
  font-weight: 700;
}

.tab-row.module-row td:first-child {
  color: var(--brand-gold);
}

.btn-save {
  background: var(--brand-gold);
  color: var(--brand-dark);
  border: none;
  padding: 14px 22px;
  border-radius: 12px;
  font-weight: 800;
  cursor: pointer;
  font-family: inherit;
  font-size: 15px;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.25);
}

.btn-save:hover {
  background: var(--brand-gold-dark);
}

.btn-sec {
  background: var(--s2);
  border: 1px solid var(--border);
  color: var(--tx2);
  padding: 10px 16px;
  border-radius: 10px;
  font-weight: 700;
  cursor: pointer;
  font-family: inherit;
  font-size: 12px;
}

.btn-sec:hover {
  background: var(--s3);
  color: var(--tx);
}

.actions-bar {
  display: flex;
  gap: 8px;
  margin-top: 14px;
  flex-wrap: wrap;
  position: sticky;
  bottom: 0;
  z-index: 20;
  padding: 14px 0 8px;
  background: linear-gradient(0deg, var(--brand-dark), rgba(5, 36, 19, 0.92) 70%, transparent);
}

.detail-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 8px;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 40vh;
  color: var(--text-muted);
}

.empty-state .ico {
  font-size: 72px;
  opacity: 0.35;
  margin-bottom: 12px;
}

.back-btn {
  display: none;
  margin-bottom: 12px;
}

/* ---------- Modals ---------- */
.modal-overlay,
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: none;
  align-items: flex-end;
  justify-content: center;
  z-index: 200;
  padding: 16px;
}

@media (min-width: 640px) {
  .modal-overlay,
  .modal {
    align-items: center;
  }
}

.modal.open,
.modal-overlay.open {
  display: flex;
  opacity: 1;
  pointer-events: auto;
}

.modal-panel {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 16px;
  max-width: 480px;
  width: 100%;
  max-height: 90vh;
  overflow: auto;
  padding: 22px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.45);
}

.modal-panel.wide {
  max-width: 1600px;
}

.modal-panel h2 {
  font-size: 17px;
  color: var(--brand-gold);
  margin-bottom: 14px;
}

.modal-close {
  float: right;
  background: var(--s2);
  border: 1px solid var(--border);
  color: var(--tx);
  padding: 6px 12px;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 700;
  font-size: 12px;
}

.modal.open[role="dialog"],
.modal-overlay.open[role="dialog"] {
  outline: none;
}

#m[role="dialog"] .modal-box {
  outline: none;
}

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

.f {
  display: flex;
  flex-direction: column;
  gap: 5px;
  margin-bottom: 10px;
}

.f label {
  font-size: 10px;
  color: var(--tx3);
  font-weight: 700;
  letter-spacing: 1px;
}

.f input,
.f select {
  background: var(--s2);
  border: 1px solid var(--border);
  color: var(--tx);
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 14px;
  font-family: inherit;
  outline: none;
  min-height: 44px;
}

.f input:focus,
.f select:focus {
  border-color: var(--brand-gold);
  box-shadow: 0 0 0 2px rgba(212, 184, 122, 0.2);
}

.checkrow {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 0;
}

.checkrow input {
  width: 18px;
  height: 18px;
  accent-color: var(--success);
}

.checkrow label {
  font-size: 13px;
  color: var(--tx);
}

.toast {
  position: fixed;
  top: 20px;
  right: 20px;
  bottom: auto;
  background: var(--success);
  color: #fff;
  padding: 12px 18px;
  border-radius: 10px;
  font-weight: 800;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
  z-index: 9999;
  font-size: 13px;
}

.toast.err {
  background: var(--error);
}

.user-sum-wrap {
  margin-bottom: 12px;
  max-height: 220px;
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--s2);
}

.user-sum-wrap summary {
  cursor: pointer;
  padding: 8px 10px;
  font-size: 11px;
  font-weight: 800;
  color: var(--tx3);
  letter-spacing: 0.06em;
}

.user-sum-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 10px;
}

.user-sum-table th,
.user-sum-table td {
  padding: 6px 8px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

.user-sum-table th {
  color: var(--tx3);
  font-weight: 800;
}

/* ---------- Audit page (FR6) ---------- */
.audit-filters {
  background: var(--bg-card);
  border-bottom: 1px solid var(--border);
  padding: 14px 18px;
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: flex-end;
}

.audit-filters label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 10px;
  color: var(--tx3);
  font-weight: 800;
  letter-spacing: 0.05em;
}

.audit-filters select,
.audit-filters input[type="date"] {
  background: var(--s2);
  border: 1px solid var(--border);
  color: var(--tx);
  padding: 8px 10px;
  border-radius: 8px;
  font-size: 13px;
  min-width: 130px;
  min-height: 44px;
}

.audit-filters button,
.audit-filters .btn-csv {
  background: var(--brand-gold);
  color: var(--brand-dark);
  border: none;
  padding: 10px 16px;
  border-radius: 10px;
  font-weight: 800;
  cursor: pointer;
  font-size: 13px;
  text-decoration: none;
  display: inline-block;
  min-height: 44px;
}

.audit-filters .btn-csv {
  background: var(--s2);
  color: var(--brand-gold);
  border: 1px solid var(--brand-gold);
}

.audit-list {
  padding: 16px 18px 32px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.audit-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px 16px;
  cursor: pointer;
  transition: transform 0.12s, box-shadow 0.12s, border-color 0.12s;
}

.audit-card:hover {
  border-color: rgba(212, 184, 122, 0.35);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.25);
  transform: translateY(-1px);
}

.audit-card-top {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: flex-start;
  justify-content: space-between;
}

.audit-card-title {
  font-size: 14px;
  font-weight: 700;
  color: var(--text-primary);
  flex: 1;
  min-width: 0;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  line-height: 1.35;
}

.audit-card-meta {
  font-size: 11px;
  color: var(--text-muted);
  text-align: right;
}

.area-badge {
  font-size: 9px;
  padding: 3px 8px;
  border-radius: 999px;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.area-badge.auth {
  background: rgba(91, 163, 217, 0.2);
  color: #9fd0f0;
}

.area-badge.admin {
  background: rgba(212, 184, 122, 0.22);
  color: var(--brand-gold);
}

.area-badge.pasza,
.area-badge.feed {
  background: rgba(232, 165, 101, 0.2);
  color: #f5c49a;
}

.area-badge.default {
  background: rgba(255, 255, 255, 0.06);
  color: var(--tx2);
}

.audit-footnote {
  padding: 0 18px 20px;
  font-size: 12px;
  color: var(--text-muted);
}

/* Diff */
.diff-add {
  color: #7bed9f;
}

.diff-del {
  color: #ff7675;
}

.diff-chg {
  color: var(--brand-gold);
}

.modal-box pre,
pre.audit-pre {
  background: #080c0a;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  font-size: 11px;
  overflow: auto;
  white-space: pre-wrap;
  margin-top: 8px;
}

/* ---------- Invite page ---------- */
.invite-wrap {
  max-width: 1600px;
  margin: 0 auto;
  padding: 20px 18px 40px;
}

.invite-hero h1 {
  color: var(--brand-gold);
  font-size: 22px;
  margin-bottom: 6px;
}

.invite-hero .sub {
  color: var(--text-muted);
  font-size: 13px;
  margin-bottom: 18px;
}

.invite-list-cards {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.invite-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px 16px;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  align-items: center;
}

/* ---------- Mobile drawer (≤768px) ---------- */
@media (max-width: 768px) {
  .drawer-toggle {
    display: inline-flex;
  }

  /* Drawer zamknięty: warstwa jest display:block (nad całą treścią) — musi być
     całkowicie „przezroczysta” dla hit-testu (SPEC 20260425 mobile drawer dept).
     Jawne pointer-events + visibility obok opacity — unika regresji w stacku z-index. */
  .drawer-backdrop {
    display: block;
    pointer-events: none;
    visibility: hidden;
  }

  body.drawer-open .drawer-backdrop {
    opacity: 1;
    pointer-events: auto;
    visibility: visible;
  }

  .admin-layout {
    grid-template-columns: 1fr;
    min-height: calc(100vh - var(--topbar-h));
  }

  .admin-sidebar {
    position: fixed;
    top: var(--topbar-h);
    left: 0;
    bottom: 0;
    width: min(320px, 90vw);
    max-height: none;
    z-index: 60;
    transform: translateX(-100%);
    transition: transform 0.22s ease;
    box-shadow: 8px 0 32px rgba(0, 0, 0, 0.35);
    border-right: 1px solid var(--border);
  }

  body.drawer-open .admin-sidebar {
    transform: translateX(0);
  }

  .admin-main {
    max-height: none;
    padding: 16px 16px 100px;
    min-height: 0;
  }

  .admin-topbar-nav {
    order: 3;
    width: 100%;
    justify-content: flex-start;
  }

  .admin-topbar-user {
    margin-left: 0;
  }

  .admin-topbar-user .admin-topbar-meta {
    display: none;
  }

  body.mobile-detail .admin-sidebar {
    transform: translateX(-100%);
  }

  body.mobile-detail.drawer-open .admin-sidebar {
    transform: translateX(0);
  }

  .grid-2 {
    grid-template-columns: 1fr;
  }

  .back-btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
  }

  body.mobile-detail .back-btn {
    display: inline-flex;
  }
}

@media (max-width: 1600px) and (min-width: 769px) {
  .admin-layout {
    grid-template-columns: 280px 1fr;
  }
}

/* Legacy mobile-detail without drawer */
/* Tablet: single column detail hides sidebar (769–900px); phone uses drawer instead */
@media (max-width: 1600px) and (min-width: 769px) {
  body.mobile-detail .admin-sidebar {
    display: none;
  }

  body.mobile-detail .back-btn {
    display: inline-flex;
  }
}

input[type="email"],
input[type="text"],
input[type="password"],
input[type="search"],
select,
textarea {
  font-size: 16px;
}

input[type="checkbox"] {
  min-height: auto;
}

button,
.btn-save,
.btn-sec,
.btn-danger,
.btn-add-user {
  min-height: 40px;
  touch-action: manipulation;
}

.mini-btn {
  min-height: 32px;
}

/* ---------- Permissions dept tree + gospodarstwo sub-depts (SPEC 20260425) ---------- */
.dept-card .dept-head .name {
  font-weight: 800;
}

.sub-dept {
  background: var(--s3);
  border: 1px solid var(--border);
  border-radius: 10px;
  margin-top: 12px;
  overflow: hidden;
}

.sub-dept-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  cursor: pointer;
  background: linear-gradient(135deg, var(--s3), var(--s2));
  transition: background 0.15s;
}

.sub-dept-head:hover {
  background: var(--s2);
}

.sub-dept-title {
  flex: 1;
  font-size: 13px;
  font-weight: 700;
  color: var(--tx);
}

.sub-dept-head .arrow {
  color: var(--tx3);
  transition: transform 0.2s;
}

.sub-dept.open .sub-dept-head .arrow {
  transform: rotate(90deg);
  color: var(--brand-gold);
}

.sub-dept-body {
  display: none;
  padding: 8px 12px 12px;
  border-top: 1px solid var(--border);
}

.sub-dept.open .sub-dept-body {
  display: block;
}

.gosp-sub-actions {
  padding-top: 6px;
}

.gosp-dept-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 8px;
  font-size: 12px;
  color: var(--tx2);
  cursor: pointer;
  user-select: none;
}

.gosp-tabs-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 8px;
  margin-top: 10px;
}

.gosp-tab-cell {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 8px 10px;
  background: var(--s2);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  font-size: 12px;
  color: var(--tx);
}

.gosp-tab-cell input {
  margin-top: 2px;
  flex-shrink: 0;
}

.gosp-tab-lbl {
  line-height: 1.35;
}

.tab-table-gosp {
  margin-bottom: 4px;
}

@media (max-width: 640px) {
  .tab-table {
    display: block;
    width: 100%;
  }

  .tab-table thead {
    display: none;
  }

  .tab-table tbody,
  .tab-table tr,
  .tab-table td {
    display: block;
    width: 100%;
  }

  .tab-table tr.tab-row {
    border: 1px solid var(--border);
    border-radius: 8px;
    margin-bottom: 8px;
    padding: 6px 0;
    background: var(--s2);
  }

  .tab-table tr.tab-row td {
    border: none;
    text-align: left;
    padding: 4px 10px;
  }

  .tab-table tr.tab-row td.c {
    display: inline-block;
    width: auto;
    padding: 4px 8px;
  }

  .tab-table tr.module-row td:first-child {
    font-weight: 700;
    padding-bottom: 6px;
    border-bottom: 1px solid rgba(45, 80, 22, 0.35);
    margin-bottom: 4px;
  }

  .gosp-tabs-grid {
    grid-template-columns: 1fr;
  }
}
