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

:root {
  --bg: #0f0d0b;
  --bg2: #1a1613;
  --bg3: #1e1b17;
  --bg-hover: #272320;
  --border: #3a322b;
  --border-hover: #5a4d42;
  --text: #f0e6dc;
  --text2: #a89888;
  --muted: #6e6055;
  --accent: #c2703e;
  --accent-hover: #d4854a;
  --error: #ef4444;
  --success: #22c55e;
  --warn: #f59e0b;
}

body {
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

/* ── Header ── */
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.5rem;
  border-bottom: 1px solid var(--border);
  background: var(--bg2);
  flex-wrap: wrap;
  gap: 0.5rem;
}

.header-left { display: flex; align-items: center; gap: 1rem; }
.header-right { display: flex; align-items: center; gap: 0.75rem; }
h1 { font-size: 1.1rem; font-weight: 600; }

.back-link {
  color: var(--muted);
  text-decoration: none;
  font-size: 0.85rem;
  transition: color 0.2s;
}
.back-link:hover { color: var(--accent); }

.current-user {
  font-size: 0.85rem;
  color: var(--text2);
  background: rgba(194,112,62,0.12);
  padding: 3px 10px;
  border-radius: 5px;
}

/* ── Toolbar ── */
.toolbar-wrap {
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  padding: 0.75rem 1.5rem;
}

.toolbar {
  max-width: 800px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.toolbar-right {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.filter-group {
  display: flex;
  gap: 2px;
  background: var(--bg);
  border-radius: 6px;
  padding: 2px;
}

.filter-btn {
  padding: 4px 10px;
  border: none;
  border-radius: 4px;
  font-size: 0.75rem;
  font-weight: 600;
  font-family: inherit;
  color: var(--muted);
  background: transparent;
  cursor: pointer;
  transition: all 0.2s;
}
.filter-btn:hover { color: var(--text2); }
.filter-btn.active { background: var(--accent); color: #fff; }

.user-count { font-size: 0.8rem; color: var(--muted); }

main { max-width: 800px; margin: 0 auto; padding: 1rem 1.5rem 3rem; }

/* ── Buttons ── */
.btn {
  padding: 8px 16px;
  border-radius: 6px;
  font-size: 0.85rem;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  border: none;
  transition: all 0.2s;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-outline { background: transparent; border: 1px solid var(--border); color: var(--text2); }
.btn-outline:hover { border-color: var(--accent); color: var(--accent); }
.btn-sm { padding: 5px 12px; font-size: 0.8rem; }
.btn-danger { background: transparent; border: 1px solid rgba(239,68,68,0.3); color: var(--error); }
.btn-danger:hover { background: rgba(239,68,68,0.1); }
.btn-success { background: transparent; border: 1px solid rgba(34,197,94,0.3); color: var(--success); }
.btn-success:hover { background: rgba(34,197,94,0.1); }
.btn-warn { background: transparent; border: 1px solid rgba(245,158,11,0.3); color: var(--warn); }
.btn-warn:hover { background: rgba(245,158,11,0.1); }

/* ── User Cards ── */
.user-card {
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1rem 1.2rem;
  margin-bottom: 0.6rem;
  transition: border-color 0.2s;
}
.user-card:hover { border-color: var(--border-hover); }
.user-card.inactive { opacity: 0.5; }
.user-card.editing {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(194,112,62,0.1);
}

/* ── Panneau d'edition inline ── */
.card-edit-panel {
  margin-top: 1rem;
  padding-top: 1rem;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 1rem;
  animation: slideDown 0.2s ease-out;
}
@keyframes slideDown {
  from { opacity: 0; transform: translateY(-4px); }
  to { opacity: 1; transform: translateY(0); }
}
.ef-field {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}
.ef-field > label {
  font-size: 0.72rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-weight: 600;
}
.ef-field input[type="text"],
.ef-field input[type="tel"],
.ef-field select {
  width: 100%;
  padding: 9px 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-size: 0.88rem;
  font-family: inherit;
  outline: none;
  transition: border-color 0.2s;
}
.ef-field input:focus,
.ef-field select:focus { border-color: var(--accent); }
.ef-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  align-items: flex-end;
}
.ef-row > .ef-field { flex: 1; min-width: 180px; }
.ef-phone-row {
  display: flex;
  gap: 6px;
}
.ef-phone-row input[type="tel"] { flex: 1; min-width: 0; }
.ef-phone-row select { width: 130px; flex-shrink: 0; }
.ef-toggles {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
}
.ef-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  padding-top: 0.5rem;
  border-top: 1px dashed var(--border);
}
.ef-error {
  font-size: 0.8rem;
  color: var(--error);
  min-height: 1em;
}

.card-top {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 0.5rem;
}

.card-avatar {
  width: 38px;
  height: 38px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  font-weight: 700;
  text-transform: uppercase;
  flex-shrink: 0;
}
.avatar-admin { background: rgba(194,112,62,0.2); color: var(--accent); }
.avatar-user { background: rgba(167,139,250,0.2); color: #a78bfa; }
/* Compat (anciens roles si encore affiches) */
.avatar-gestionnaire { background: rgba(167,139,250,0.2); color: #a78bfa; }
.avatar-commissionnaire { background: rgba(14,165,233,0.2); color: #38bdf8; }
.avatar-employe { background: rgba(34,197,94,0.2); color: var(--success); }

.card-info { flex: 1; min-width: 0; }
.card-name {
  font-size: 0.95rem;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 6px;
}
.card-name .inactive-badge {
  font-size: 0.65rem;
  background: rgba(239,68,68,0.15);
  color: var(--error);
  padding: 1px 6px;
  border-radius: 3px;
  font-weight: 600;
}

.card-meta {
  font-size: 0.78rem;
  color: var(--muted);
  display: flex;
  gap: 0.75rem;
  flex-wrap: wrap;
  margin-top: 2px;
}

.role-badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.role-admin { background: rgba(194,112,62,0.15); color: var(--accent); }
.role-user { background: rgba(167,139,250,0.15); color: #a78bfa; }
/* Compat (anciens roles si encore affiches) */
.role-gestionnaire { background: rgba(167,139,250,0.15); color: #a78bfa; }
.role-commissionnaire { background: rgba(14,165,233,0.15); color: #38bdf8; }
.role-employe { background: rgba(34,197,94,0.15); color: var(--success); }

/* Badges d'acces par app dans la carte utilisateur */
.app-badges {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-top: 4px;
}
.app-badge {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  padding: 2px 7px;
  font-size: 0.7rem;
  font-weight: 600;
  border-radius: 4px;
  border: 1px solid var(--border);
  background: var(--bg3);
  color: var(--text2);
}
.app-badge.lvl-employe { color: var(--success); border-color: rgba(34,197,94,0.3); background: rgba(34,197,94,0.08); }
.app-badge.lvl-gestionnaire { color: #a78bfa; border-color: rgba(167,139,250,0.3); background: rgba(167,139,250,0.08); }
.app-badge.lvl-commissionnaire { color: #38bdf8; border-color: rgba(14,165,233,0.3); background: rgba(14,165,233,0.08); }
.app-badge.lvl-oui { color: var(--accent); border-color: rgba(194,112,62,0.3); background: rgba(194,112,62,0.08); }

.card-actions {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  margin-top: 0.6rem;
  padding-top: 0.6rem;
  border-top: 1px solid var(--border);
}

/* ── Archive ── */
.archive-header {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-top: 2rem;
  margin-bottom: 0.75rem;
  padding-top: 1.5rem;
  border-top: 1px solid var(--border);
}
.archive-header h2 {
  font-size: 0.95rem;
  color: var(--muted);
  font-weight: 600;
}
.archive-count { font-size: 0.75rem; color: var(--muted); }

.archive-card {
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.8rem 1.2rem;
  margin-bottom: 0.5rem;
  opacity: 0.6;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex-wrap: wrap;
}
.archive-card:hover { opacity: 0.8; }
.archive-info { flex: 1; min-width: 0; }
.archive-name { font-size: 0.9rem; font-weight: 600; }
.archive-meta { font-size: 0.75rem; color: var(--muted); }
.archive-actions { display: flex; gap: 6px; }

/* ── Toast ── */
.toast {
  position: fixed;
  bottom: 2rem;
  left: 50%;
  transform: translateX(-50%) translateY(100px);
  background: var(--bg3);
  border: 1px solid var(--border);
  padding: 10px 20px;
  border-radius: 8px;
  font-size: 0.85rem;
  font-weight: 600;
  z-index: 200;
  transition: transform 0.3s ease, opacity 0.3s;
  opacity: 0;
  pointer-events: none;
}
.toast.show {
  transform: translateX(-50%) translateY(0);
  opacity: 1;
}
.toast.toast-success { border-color: var(--success); color: var(--success); }
.toast.toast-error { border-color: var(--error); color: var(--error); }

/* ── Modal ── */
.modal-backdrop {
  display: none;
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.6);
  z-index: 100;
}
.modal {
  display: none;
  position: fixed;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1.5rem;
  width: 520px;
  max-width: 94vw;
  max-height: 90vh;
  overflow-y: auto;
  overflow-x: hidden;
  z-index: 101;
}
.modal-backdrop.open, .modal.open { display: block; }

.modal-title {
  font-size: 1.05rem;
  font-weight: 600;
  margin-bottom: 1.2rem;
}

.field { margin-bottom: 1rem; }
#appsField { margin-top: 0.5rem; }
.field > label {
  display: block;
  font-size: 0.78rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 1px;
  margin-bottom: 4px;
}
/* Les toggle-labels n'heritent PAS du style uppercase du label parent */
.field label.toggle-label {
  text-transform: none;
  letter-spacing: normal;
  color: var(--text);
  font-size: 0.88rem;
}
.phone-row {
  display: flex;
  gap: 6px;
  align-items: stretch;
}
.phone-row input[type="tel"] { flex: 1; min-width: 0; }
.phone-row select { width: 130px; flex-shrink: 0; }

.field input[type="text"],
.field input[type="password"],
.field select {
  width: 100%;
  padding: 10px 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-size: 0.9rem;
  font-family: inherit;
  outline: none;
  transition: border-color 0.2s;
}
.field input:focus, .field select:focus {
  border-color: var(--accent);
}

.pw-strength {
  height: 3px;
  border-radius: 2px;
  margin-top: 4px;
  background: var(--border);
  transition: all 0.3s;
}
.pw-strength.weak { background: var(--error); width: 33%; }
.pw-strength.medium { background: var(--warn); width: 66%; }
.pw-strength.strong { background: var(--success); width: 100%; }

/* Toggle switch */
.toggle-label {
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  font-size: 0.88rem;
  text-transform: none;
  letter-spacing: 0;
  color: var(--text);
  line-height: 1.3;
}
.toggle-label input[type="checkbox"] {
  position: absolute;
  opacity: 0;
  pointer-events: none;
  width: 0;
  height: 0;
}
.toggle-switch {
  width: 36px;
  height: 20px;
  background: var(--border);
  border-radius: 10px;
  position: relative;
  transition: background 0.2s;
  flex-shrink: 0;
  display: inline-block;
}
.toggle-switch::after {
  content: '';
  position: absolute;
  top: 2px; left: 2px;
  width: 16px; height: 16px;
  border-radius: 50%;
  background: var(--text2);
  transition: all 0.2s;
}
.toggle-label input:checked + .toggle-switch {
  background: var(--success);
}
.toggle-label input:checked + .toggle-switch::after {
  left: 18px;
  background: #fff;
}
.toggle-label .toggle-text {
  flex: 1;
  min-width: 0;
}

.checkbox-group {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem;
  margin-top: 0.4rem;
}
.checkbox-label {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  background: var(--bg3);
  border: 1px solid var(--border);
  padding: 0.55rem 0.75rem;
  border-radius: 8px;
  font-size: 0.82rem;
  cursor: pointer;
  transition: border-color 0.2s, background 0.2s;
  color: var(--text-muted);
  user-select: none;
}
.checkbox-label:hover {
  border-color: var(--border-hover);
  background: var(--bg-hover);
}
.checkbox-label input[type=checkbox] {
  accent-color: var(--accent);
  width: 16px;
  height: 16px;
  cursor: pointer;
  flex-shrink: 0;
}
.checkbox-label input[type=checkbox]:checked + span {
  color: var(--accent);
  font-weight: 600;
}
.checkbox-label span {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
/* Permissions: full-width pour la derniere checkbox si impaire */
#permCheckboxes .checkbox-label:last-child:nth-child(odd) {
  grid-column: 1 / -1;
  max-width: 50%;
}

/* Texte d'aide sous les champs */
.help-text {
  font-size: 0.72rem;
  color: var(--muted);
  margin-top: 0.4rem;
  font-style: italic;
}

/* ── Acces par application (nouveau modele) ── */
.apps-sections {
  display: flex;
  flex-direction: column;
  gap: 0.8rem;
  margin-top: 0.4rem;
}
.apps-section {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  padding: 0.6rem 0.75rem;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 10px;
}
.apps-section-title {
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--text2);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  padding-bottom: 0.35rem;
  border-bottom: 1px solid var(--border);
  margin-bottom: 0.3rem;
}
.app-row {
  display: grid;
  grid-template-columns: minmax(0, 9rem) 1fr;
  align-items: center;
  gap: 0.5rem;
  padding: 0.3rem 0;
}
.app-row-label {
  font-size: 0.84rem;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.app-row-options {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  justify-content: flex-end;
}
.radio-option {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  padding: 0.3rem 0.55rem;
  font-size: 0.75rem;
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.15s;
  user-select: none;
  color: var(--text2);
  white-space: nowrap;
  font-weight: 500;
}
.radio-option:hover {
  border-color: var(--border-hover);
}
.radio-option input[type="radio"] {
  accent-color: var(--accent);
  width: 14px;
  height: 14px;
  margin: 0;
  cursor: pointer;
}
.radio-option.checked {
  background: rgba(194,112,62,0.15);
  border-color: var(--accent);
  color: var(--accent);
  font-weight: 700;
}
/* Toggle switch dans une app-row */
.toggle-label.inline-toggle {
  gap: 0;
  cursor: pointer;
  margin: 0;
  padding: 0.3rem 0.4rem;
}
.app-row-options .toggle-label.inline-toggle .toggle-switch {
  margin: 0;
}
/* Sous-niveaux pour apps multi (s'affichent en dessous de la ligne quand toggle on) */
.app-row-sublevels {
  grid-column: 1 / -1;
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  margin-top: 0.5rem;
  padding: 0.5rem 0.6rem;
  background: var(--bg);
  border: 1px dashed var(--border);
  border-radius: 6px;
  animation: subSlide 0.18s ease-out;
}
@keyframes subSlide {
  from { opacity: 0; transform: translateY(-3px); }
  to { opacity: 1; transform: translateY(0); }
}

/* Choix de boutique a l'interieur d'une app row (Boutiques/Pertes, quand role=employe) */
.app-row-boutiques {
  grid-column: 1 / -1;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
  margin-top: 0.4rem;
  padding: 0.5rem 0.6rem;
  background: rgba(194,112,62,0.05);
  border: 1px dashed rgba(194,112,62,0.3);
  border-radius: 6px;
  animation: subSlide 0.18s ease-out;
}
.app-row-boutiques-label {
  font-size: 0.75rem;
  color: var(--muted);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.app-row-boutiques .checkbox-label {
  padding: 0.3rem 0.55rem;
  font-size: 0.78rem;
  background: var(--bg3);
}
/* Mobile: stack label et options */
@media (max-width: 520px) {
  .app-row {
    grid-template-columns: 1fr;
    gap: 0.3rem;
  }
  .app-row-options {
    justify-content: flex-start;
  }
}

/* Permissions groupees par app/section */
.perm-sections {
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
  margin-top: 0.4rem;
}
.perm-section {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem;
  padding: 0.6rem 0.7rem;
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: 10px;
}
.perm-section-title {
  grid-column: 1 / -1;
  font-size: 0.78rem;
  font-weight: 700;
  color: var(--text);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding-bottom: 0.2rem;
  border-bottom: 1px solid var(--border);
  margin-bottom: 0.2rem;
}
.perm-section .checkbox-label {
  background: var(--bg2);
}
/* Neutraliser la regle impaire pour les sections groupees */
.perm-section .checkbox-label:last-child:nth-child(odd) {
  grid-column: auto;
  max-width: none;
}

.error-msg {
  color: var(--error);
  font-size: 0.85rem;
  margin-bottom: 0.5rem;
  min-height: 1.2em;
}

.modal-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 1rem; }

/* ── Responsive ── */
@media (max-width: 600px) {
  header { padding: 0.75rem 1rem; }
  h1 { font-size: 0.95rem; }
  .toolbar-wrap { padding: 0.6rem 1rem; }
  main { padding: 0.75rem 1rem 3rem; }
  .filter-btn { padding: 4px 8px; font-size: 0.7rem; }
}
