/* ============================================================
   AgroGest — layout.css : structure applicative
   Sidebar épurée, topbar nette, grilles responsives.
   ============================================================ */

.app {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  grid-template-rows: var(--topbar-h) 1fr;
  grid-template-areas: "sidebar topbar" "sidebar main";
  min-height: 100vh;
  transition: grid-template-columns var(--t-med);
}
.app--collapsed { grid-template-columns: var(--sidebar-w-collapsed) 1fr; }

/* ---------- Barre latérale ---------- */
.sidebar {
  grid-area: sidebar;
  background: var(--surface-1);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  padding: var(--sp-3) 0 var(--sp-4);
  overflow: hidden;
}
.sidebar__brand {
  display: flex; align-items: center; gap: var(--sp-3);
  padding: var(--sp-2) var(--sp-4) var(--sp-4);
  font-weight: 700; font-size: 18px;
  color: var(--text-1);
  white-space: nowrap;
  letter-spacing: -0.01em;
}
.sidebar__brand-logo {
  width: 36px; height: 36px; flex-shrink: 0;
  display: grid; place-items: center;
  background: var(--gradient-brand);
  color: #fff; border-radius: var(--radius-sm);
  font-size: 18px;
  box-shadow: var(--shadow-sm);
}
.sidebar__brand-text { color: var(--color-primary); }

.sidebar__nav { flex: 1; display: flex; flex-direction: column; gap: 3px; padding: var(--sp-2) var(--sp-3); overflow-y: auto; }
.sidebar__section {
  font-size: 11px; font-weight: 600; text-transform: uppercase;
  letter-spacing: .06em; color: var(--text-3);
  padding: var(--sp-3) var(--sp-3) var(--sp-1);
}
.app--collapsed .sidebar__section { opacity: 0; height: 8px; padding: 0; }

.sidebar__link {
  position: relative;
  display: flex; align-items: center; gap: var(--sp-3);
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  color: var(--text-2);
  font-weight: 500; font-size: var(--fs-sm);
  white-space: nowrap;
  transition: background var(--t-fast), color var(--t-fast);
}
.sidebar__link:hover { background: var(--surface-2); color: var(--text-1); text-decoration: none; }
.sidebar__link--active { background: var(--color-primary-soft); color: var(--color-primary); font-weight: 600; }
.sidebar__link--active::before {
  content: ""; position: absolute; left: 0; top: 8px; bottom: 8px;
  width: 3px; border-radius: 0 3px 3px 0; background: var(--color-primary);
}
.sidebar__link-icon {
  width: 22px; flex-shrink: 0; text-align: center; font-size: 16px;
}
.app--collapsed .sidebar__link { justify-content: center; }
.app--collapsed .sidebar__link-text,
.app--collapsed .sidebar__brand-text { display: none; }

/* ---------- Barre supérieure ---------- */
.topbar {
  grid-area: topbar;
  display: flex; align-items: center; gap: var(--sp-3);
  background: var(--surface-1);
  border-bottom: 1px solid var(--border);
  padding: 0 var(--sp-5);
  min-width: 0; /* empêche le contenu (titre en nowrap, icônes) de forcer la piste de grille au-delà du viewport */
}
.topbar__toggle {
  background: none; border: none; cursor: pointer; color: var(--text-2);
  width: 38px; height: 38px; border-radius: var(--radius-sm); font-size: 16px;
  display: grid; place-items: center; transition: background var(--t-fast);
  flex-shrink: 0;
}
.topbar__toggle:hover { background: var(--surface-2); color: var(--text-1); }
.topbar__title  { font-weight: 600; font-size: var(--fs-h2); color: var(--text-1); }
.topbar__spacer { flex: 1; }
.topbar__farm-select { min-width: 190px; }

.topbar__user {
  display: flex; align-items: center; gap: var(--sp-2);
  cursor: pointer; padding: 7px 12px;
  border-radius: var(--radius-sm);
  color: var(--text-2); font-size: var(--fs-sm); font-weight: 500;
  transition: background var(--t-fast), color var(--t-fast);
  flex-shrink: 0;
}
.topbar__user:hover { background: var(--surface-2); color: var(--text-1); text-decoration: none; }

/* ---------- Zone principale ---------- */
.main {
  grid-area: main;
  padding: var(--sp-6);
  overflow-y: auto;
  max-width: var(--content-max);
  width: 100%;
}
.main__header {
  display: flex; align-items: center; justify-content: space-between;
  gap: var(--sp-4);
  margin-bottom: var(--sp-5);
  flex-wrap: wrap;
}
.main__header h1 { font-size: var(--fs-h1); letter-spacing: -0.02em; }

/* ---------- Grilles ---------- */
.grid-kpi {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: var(--sp-4);
  margin-bottom: var(--sp-5);
}
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: var(--sp-4); }
.grid-charts { display: grid; grid-template-columns: 1fr 1fr; gap: var(--sp-4); }

/* ---------- Pages invité (login/register) ---------- */
.guest {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: var(--sp-5);
  background:
    radial-gradient(900px 420px at 85% -15%, var(--color-primary-soft), transparent),
    radial-gradient(700px 380px at 10% 110%, var(--color-accent-soft), transparent),
    var(--bg-page);
}
.guest__card { width: min(440px, 100%); }
.guest__card--wide { width: min(560px, 100%); }
.guest__brand {
  display: flex; align-items: center; justify-content: center; gap: var(--sp-3);
  font-size: 24px; font-weight: 700; color: var(--color-primary);
  margin-bottom: var(--sp-5);
}
.guest__brand-logo {
  width: 40px; height: 40px; display: grid; place-items: center;
  background: var(--gradient-brand); color: #fff;
  border-radius: var(--radius-md); font-size: 20px; box-shadow: var(--shadow-md);
}
.guest__footer { text-align: center; margin-top: var(--sp-4); color: var(--text-2); font-size: var(--fs-sm); }

.steps { display: flex; gap: var(--sp-2); margin-bottom: var(--sp-5); }
.steps__item { flex: 1; height: 4px; border-radius: 2px; background: var(--border); transition: background var(--t-med); }
.steps__item--done { background: var(--color-primary); }

/* ---------- Responsive ---------- */
@media (max-width: 1024px) {
  .app { grid-template-columns: var(--sidebar-w-collapsed) 1fr; }
  .sidebar__link { justify-content: center; }
  .sidebar__link-text, .sidebar__brand-text, .sidebar__section { display: none; }
  .sidebar__link--active::before { display: none; }
  .grid-charts { grid-template-columns: 1fr; }
}
@media (max-width: 768px) {
  .app { grid-template-columns: 1fr; grid-template-areas: "topbar" "main"; }
  .sidebar {
    position: fixed; top: 0; left: 0; bottom: 0;
    width: var(--sidebar-w); transform: translateX(-100%);
    transition: transform var(--t-med); z-index: 90; box-shadow: var(--shadow-lg);
  }
  .sidebar--open { transform: translateX(0); }
  .sidebar--open .sidebar__link { justify-content: flex-start; }
  .sidebar--open .sidebar__link-text,
  .sidebar--open .sidebar__brand-text,
  .sidebar--open .sidebar__section { display: inline; }
  .grid-2 { grid-template-columns: 1fr; }
  .main { padding: var(--sp-4); }

  /* La barre supérieure ne doit jamais forcer un défilement horizontal :
     on resserre les espacements et on tronque le titre plutôt que de
     laisser les icônes déborder de l'écran. */
  .topbar { padding: 0 var(--sp-2); gap: 6px; }
  .topbar__title {
    flex: 1 1 auto; min-width: 0;
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  }
  .topbar__user span { display: none; }
  .topbar__farm-select { min-width: 0; max-width: 100%; }
}
@media (max-width: 480px) {
  .modal { width: 100vw; height: 100vh; max-height: none; border-radius: 0; }
}

/* ---------- Fond assombri derrière le menu mobile ---------- */
.sidebar-backdrop {
  position: fixed; inset: 0; background: rgba(16, 21, 19, .45);
  z-index: 85; opacity: 0; pointer-events: none;
  transition: opacity var(--t-med);
}
.sidebar-backdrop--visible { opacity: 1; pointer-events: auto; }
@media (min-width: 769px) { .sidebar-backdrop { display: none; } }

/* ============================================================
   Garde-fou anti-débordement horizontal (toutes pages, tout écran)
   ============================================================ */
html, body { max-width: 100%; overflow-x: hidden; }

/* La zone de contenu ne peut jamais pousser au-delà de la fenêtre.
   min-width:0 est essentiel : sans lui, un enfant en grille/flex
   (table large, texte long) force la colonne à s'élargir. */
.main { min-width: 0; max-width: 100%; overflow-x: hidden; }
.app { max-width: 100vw; }

/* Tout conteneur de table défile horizontalement en interne plutôt
   que d'élargir la page. */
.table-wrap { max-width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }

/* Les médias et le contenu pré-formaté ne débordent jamais. */
.card img, .main img, .card svg, canvas { max-width: 100%; height: auto; }
.card { min-width: 0; max-width: 100%; }
pre, code { max-width: 100%; overflow-x: auto; white-space: pre-wrap; word-break: break-word; }

/* Les mots très longs (URLs, références) se cassent au lieu de déborder.
   (Les cellules de tableau utilisent white-space:nowrap + défilement
   horizontal du conteneur .table-wrap — voir components.css.) */
.kanban__card-title, .timeline-item__title, .suggestion__msg {
  overflow-wrap: anywhere;
}

/* Les grilles passent en une colonne dès que l'espace manque. */
@media (max-width: 768px) {
  .grid-charts, .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr !important; }
}

/* Sécurité supplémentaire sur très petits écrans : les rangées de
   boutons/filtres peuvent s'enrouler. */
@media (max-width: 600px) {
  .page-header, .toolbar, .filters { flex-wrap: wrap; }
  .btn { white-space: nowrap; }
  /* Groupes d'actions dans l'en-tête de page (ex. sélecteur + bouton,
     plage de dates) : ils s'enroulent plutôt que de déborder. */
  .main__header > div { flex-wrap: wrap; row-gap: var(--sp-2); }
}
