:root {
  --bg: oklch(0.98 0.006 75);
  --surface: oklch(1 0.003 75);
  --surface-sunken: oklch(0.965 0.008 75);
  --border: oklch(0.90 0.012 70);
  --border-soft: oklch(0.94 0.008 70);
  --ink: oklch(0.22 0.015 55);
  --ink-soft: oklch(0.46 0.02 55);
  --ink-faint: oklch(0.62 0.015 55);
  --accent: oklch(0.70 0.17 45);
  --accent-strong: oklch(0.55 0.18 38);
  --accent-soft: oklch(0.94 0.045 55);
  --success: oklch(0.62 0.13 150);
  --success-soft: oklch(0.94 0.05 150);
  --pending: oklch(0.55 0.13 75);
  --pending-soft: oklch(0.94 0.06 80);
  --danger: oklch(0.55 0.19 25);
  --danger-soft: oklch(0.94 0.06 25);
  --brand-ink: oklch(0.19 0.014 55);
  --brand-ink-2: oklch(0.24 0.02 50);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: "Manrope", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3, .brand { font-family: "Space Grotesk", system-ui, sans-serif; }

a { color: var(--accent-strong); text-decoration: none; }
a:hover { color: var(--ink); }

table { border-collapse: collapse; width: 100%; }
input::placeholder { color: var(--ink-faint); }

input[type="text"], input[type="email"], input[type="password"], input[type="search"], input[type="url"], input[type="datetime-local"], input[type="number"], select, textarea {
  font-family: inherit;
  font-size: 14px;
  color: var(--ink);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 11px 13px;
  background: var(--surface);
  width: 100%;
}
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.editor-tabs {
  display: flex;
  gap: 2px;
  margin-bottom: 6px;
}
.editor-tab {
  font-family: inherit;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--ink-faint);
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  padding: 6px 4px;
  cursor: pointer;
}
.editor-tab:hover { color: var(--ink); }
.editor-tab.active { color: var(--accent-strong); border-bottom-color: var(--accent); }

.rich-editor.html-source {
  width: 100%;
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: 12.5px;
  white-space: pre;
  resize: vertical;
}

.editor-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  padding: 8px;
  border: 1px solid var(--border);
  border-bottom: none;
  border-radius: 9px 9px 0 0;
  background: var(--surface);
}
.editor-toolbar .sep { width: 1px; background: var(--border); margin: 4px 4px; }
.editor-toolbar button {
  font-family: inherit;
  font-size: 13px;
  font-weight: 600;
  color: var(--ink-soft);
  background: transparent;
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 6px 10px;
  cursor: pointer;
}
.editor-toolbar button:hover { background: var(--accent-soft); border-color: var(--border); color: var(--ink); }

.rich-editor {
  border: 1px solid var(--border);
  border-radius: 0 0 9px 9px;
  padding: 12px 14px;
  min-height: 220px;
  font-size: 14px;
  line-height: 1.6;
  color: var(--ink);
  background: var(--surface);
}
.rich-editor:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.rich-editor:empty:before { content: attr(data-placeholder); color: var(--ink-faint); white-space: pre-wrap; }
.rich-editor img { max-width: 100%; }
.rich-editor h2 { font-size: 19px; margin: 14px 0 8px 0; }
.rich-editor h3 { font-size: 16px; margin: 12px 0 6px 0; }
.rich-editor p { margin: 0 0 10px 0; }

.image-toolbar {
  display: none;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  margin-top: 8px;
  padding: 8px 10px;
  border: 1px solid var(--accent);
  border-radius: 8px;
  background: var(--accent-soft);
  font-size: 12.5px;
}
.image-toolbar span { font-weight: 600; color: var(--ink-soft); margin-right: 2px; }
.image-toolbar button {
  font-family: inherit;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 5px 9px;
  cursor: pointer;
}
.image-toolbar button:hover { border-color: var(--accent); }

.app-modal {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(20, 16, 12, 0.5);
  z-index: 100;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.app-modal.open { display: flex; }
.app-modal-inner {
  background: var(--surface);
  border-radius: 12px;
  width: 100%;
  max-width: 560px;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.app-modal-inner.app-modal-narrow { max-width: 400px; }
.app-modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border-soft);
  font-size: 14px;
  font-weight: 600;
}
.app-modal-header button {
  border: none;
  background: transparent;
  font-size: 15px;
  cursor: pointer;
  color: var(--ink-faint);
  line-height: 1;
  padding: 4px;
}
.app-modal-header button:hover { color: var(--ink); }
.app-modal-body { padding: 18px; font-size: 13.5px; color: var(--ink); line-height: 1.5; }
.app-modal-actions { display: flex; gap: 10px; padding: 0 18px 18px 18px; }
.gallery-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
  gap: 10px;
  padding: 16px;
  overflow-y: auto;
}
.gallery-item { position: relative; aspect-ratio: 1 / 1; }
.gallery-thumb {
  width: 100%;
  height: 100%;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0;
  overflow: hidden;
  cursor: pointer;
  background: var(--bg);
  display: block;
}
.gallery-thumb:hover { border-color: var(--ink-faint); }
.gallery-thumb:focus { outline: none; }
.gallery-thumb:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.gallery-item img { width: 100%; height: 100%; object-fit: cover; display: block; }
.gallery-delete {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  border: 1.5px solid #fff;
  background: rgba(20, 16, 12, 0.72);
  color: #fff;
  font-size: 11px;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25);
}
.gallery-delete:hover { background: var(--ink); }
.gallery-delete:focus { outline: none; }
.gallery-delete:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.schedule-box {
  border: 1px solid var(--accent);
  border-radius: 10px;
  background: var(--accent-soft);
  padding: 14px 16px;
}
.schedule-box label { color: var(--accent-strong) !important; }
.schedule-box input[type="datetime-local"] { background: var(--surface); }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-weight: 600;
  font-size: 14.5px;
  padding: 12px;
  border-radius: 9px;
  border: none;
  cursor: pointer;
  font-family: inherit;
}
.btn-primary { background: var(--accent); color: oklch(0.99 0.005 75); }
.btn-primary:hover { background: var(--accent-strong); }
.btn-secondary { background: var(--surface); color: var(--ink); border: 1px solid var(--border); }
.btn-secondary:hover { border-color: var(--ink-faint); }
.btn-sm { padding: 7px 12px; font-size: 13px; }

.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 10px;
  border-radius: 100px;
  font-size: 12px;
  font-weight: 600;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border-soft);
  border-radius: 12px;
}

.flash {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  border-radius: 9px;
  font-size: 13.5px;
  font-weight: 500;
  margin-bottom: 16px;
}
.flash-success { background: var(--success-soft); color: var(--success); }
.flash-danger { background: var(--danger-soft); color: var(--danger); }
.flash-warning { background: var(--pending-soft); color: var(--pending); }

/* --- App shell (dashboard / users) --- */

.app-shell { display: flex; min-height: 100vh; }

.app-sidebar {
  width: 232px;
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border-soft);
  display: flex;
  flex-direction: column;
  padding: 28px 20px;
}

.app-nav { display: flex; flex-direction: column; gap: 2px; }
.app-nav-item {
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 9px 12px;
  border-radius: 8px;
  color: var(--ink-soft);
  font-weight: 500;
  font-size: 14px;
}
.app-nav-item.active { background: var(--accent-soft); color: var(--accent-strong); font-weight: 600; }

.app-main { flex: 1; padding: 36px 44px; min-width: 0; }

/* --- Auth pages --- */

.auth-shell { display: flex; min-height: 100vh; }

.auth-brand-pane {
  position: relative;
  width: 560px;
  flex-shrink: 0;
  background: linear-gradient(160deg, var(--brand-ink) 0%, var(--brand-ink-2) 100%);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: 48px;
}

.auth-form-pane {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 48px;
}

.auth-form-pane form { width: 100%; max-width: 360px; }

.mobile-nav-toggle {
  display: none;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--ink-soft);
  cursor: pointer;
  flex-shrink: 0;
}

/* --- Responsive mobile : uniquement sous 768px, aucune règle desktop
   ci-dessus n'est modifiée - le rendu au-dessus du breakpoint est inchangé. --- */
@media (max-width: 768px) {
  .app-shell { flex-direction: column; min-height: 0; }

  .app-sidebar {
    position: relative;
    width: 100%;
    flex-shrink: 0;
    flex-direction: row;
    align-items: center;
    padding: 12px 16px;
    border-right: none;
    border-bottom: 1px solid var(--border-soft);
    gap: 12px;
  }
  .app-sidebar > div:first-child { padding: 0 !important; flex-shrink: 0; }
  .mobile-nav-toggle { display: flex; }

  /* Le menu est replié par défaut (juste le bouton hamburger) et s'ouvre en
     liste déroulante pleine largeur au tap - plus fiable pour la découverte
     qu'un défilement horizontal sans indice visuel. */
  .app-nav {
    display: none;
    flex-direction: column;
    gap: 2px;
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    background: var(--surface);
    border-bottom: 1px solid var(--border-soft);
    box-shadow: 0 8px 16px rgba(0,0,0,0.08);
    padding: 8px 12px 14px;
    z-index: 20;
  }
  .app-sidebar.mobile-nav-open .app-nav { display: flex; }
  .app-nav-item { padding: 12px 10px; white-space: normal; }

  /* Bloc profil + déconnexion : passe à droite de la barre, en version
     compacte (juste l'avatar + le bouton) - l'e-mail/rôle en toutes lettres
     n'a pas la place et n'est pas utile pour se retrouver sur son propre compte. */
  .app-sidebar > div[style*="margin-top:auto"] {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-top: 0 !important;
    margin-left: auto !important;
    padding-top: 0 !important;
    border-top: none !important;
    flex-shrink: 0;
  }
  .app-sidebar > div[style*="margin-top:auto"] > div:first-child { margin-bottom: 0 !important; }
  .app-sidebar > div[style*="margin-top:auto"] div[style*="min-width:0"] { display: none; }
  .app-sidebar form button { width: auto; padding: 8px 10px; white-space: nowrap; }

  .app-main { padding: 20px 16px; }

  .auth-shell { flex-direction: column; }
  .auth-brand-pane { display: none; }
  .auth-form-pane { width: 100%; padding: 32px 20px; }

  /* Les tableaux (Utilisateurs, Contacts, événements...) débordent sur mobile :
     on les rend défilables horizontalement plutôt que de casser la mise en page. */
  .card { overflow-x: auto !important; }

  /* Grille de statistiques du tableau de bord : 4 colonnes ne rentrent pas
     sur mobile (le contenu de chaque carte a une largeur minimale non
     compressible), on passe à 2x2. */
  .stat-grid { grid-template-columns: repeat(2, minmax(0, 1fr)) !important; }

  /* Barre de filtres (recherche + selects + bouton) : passe à la ligne au
     lieu de forcer toute la page à défiler horizontalement. */
  .filter-row { flex-wrap: wrap !important; }
  .filter-row input[type="search"] { max-width: none !important; flex: 1 1 100%; }
  .filter-row select { flex: 1 1 auto; }

  /* Ligne titre + bouton d'action (ex: "Utilisateurs" + "Inviter un
     utilisateur") : empilement forcé en colonne, plus fiable qu'un simple
     flex-wrap dont le déclenchement dépend de la taille du contenu. */
  .page-header-row {
    flex-direction: column !important;
    align-items: flex-start !important;
    gap: 12px;
  }

  /* Composeur de campagne : les deux colonnes (édition / aperçu) ne rentrent
     pas côte à côte sur mobile, ce qui forçait chaque .card à défiler
     horizontalement (cf. .card{overflow-x:auto} ci-dessus, qui masquait le
     vrai souci en le rendant discrètement scrollable au lieu de casser la
     mise en page) - on empile les deux colonnes à la place. */
  .composer-grid { grid-template-columns: 1fr !important; }
  .composer-grid > div:last-child { position: static !important; }

  /* Cartes de choix de modèle et ligne de boutons finale : côte à côte, leur
     largeur cumulée dépasse légèrement la largeur disponible sur mobile
     (même constat que .page-header-row plus haut) - empilement en colonne. */
  .template-choice-row,
  .form-actions-row,
  .button-fields-row {
    flex-direction: column !important;
  }

  /* Aperçu du contenu d'une campagne (page détail) : le HTML d'un email est
     volontairement en largeur fixe (~600px, table-based layout, exigence des
     clients mail comme Outlook qui ignorent les media queries) - impossible
     à rendre "responsive" sans casser le rendu réel envoyé aux destinataires.
     On le réduit donc visuellement à l'échelle plutôt que de le laisser
     déborder ou dépendre d'un défilement horizontal peu visible. */
  .email-preview-scale { zoom: 0.5; }
}
