/* Noha Auto : conçu d'abord pour le téléphone, puis élargi pour l'ordinateur (à partir de 768 px). */

:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --border: #e0e2e7;
  --text: #1c1f26;
  --text-muted: #6b7280;
  --primary: #c8102e;
  --primary-dark: #9c0c22;
  --danger: #d92d20;
  --danger-bg: #fef2f1;
  --warn-bg: #fff7e6;
  --warn-text: #92400e;
  --info-bg: #eef4fb;
  --info-text: #1e4a7a;
  --chip: #eef0f3;
  --radius: 10px;
  --tap: 46px;
  color-scheme: light;
}

* { box-sizing: border-box; }

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

body {
  margin: 0;
  font-family: -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  -webkit-tap-highlight-color: transparent;
}

.hidden { display: none !important; }
.muted { color: var(--text-muted); font-size: 13px; }
body.role-employe .admin-only { display: none !important; }
.desktop-only { display: none !important; }

:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }

/* ---------- Boutons et champs ---------- */

.btn {
  min-height: var(--tap);
  padding: 10px 16px;
  border-radius: var(--radius);
  border: 1px solid transparent;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  text-decoration: none;
  color: var(--text);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}
.btn:disabled { opacity: 0.5; cursor: default; }
.btn-primary { background: var(--primary); color: white; }
.btn-primary:active:not(:disabled) { background: var(--primary-dark); }
.btn-secondary { background: var(--surface); border-color: var(--border); }
.btn-danger { background: var(--danger-bg); color: var(--danger); border-color: #f7c9c4; }
.btn-small { min-height: 38px; padding: 6px 12px; font-size: 13px; }

.btn-alert { background: var(--surface); border-color: var(--border); color: var(--text-muted); white-space: nowrap; }
.btn-alert.has-alerts { background: var(--warn-bg); border-color: #f0c988; color: var(--warn-text); font-weight: 700; }
.btn-alert.active { background: var(--warn-text); border-color: var(--warn-text); color: #fff; }

/* 16 px minimum : en dessous, l'iPhone zoome tout seul sur le champ. */
input:not([type="checkbox"]):not([type="file"]):not([type="hidden"]), select {
  min-height: var(--tap);
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 16px;
  font-family: inherit;
  color: var(--text);
  font-weight: 400;
  background: var(--surface);
}

.logo {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: none;
  width: 38px;
  height: 38px;
  border-radius: 10px;
  background: var(--primary);
  color: white;
  font-weight: 700;
  letter-spacing: 0.5px;
}

/* ---------- En-tête ---------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 16px;
  padding-top: max(10px, env(safe-area-inset-top));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.topbar-title { display: flex; align-items: center; gap: 10px; min-width: 0; }
.topbar-title h1 { margin: 0; font-size: 18px; }
.topbar-title p { display: none; margin: 0; color: var(--text-muted); font-size: 12px; }
.topbar-actions { display: flex; gap: 10px; align-items: center; }

/* ---------- Menu compte ---------- */

.menu { position: relative; }
.menu summary { list-style: none; cursor: pointer; user-select: none; max-width: 52vw; }
.menu summary::-webkit-details-marker { display: none; }
.menu summary #menu-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.menu-panel {
  position: absolute;
  right: 0;
  top: calc(100% + 6px);
  width: min(300px, calc(100vw - 32px));
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(20, 20, 30, 0.16);
  padding: 6px;
  z-index: 40;
  display: flex;
  flex-direction: column;
}

.menu-who { margin: 0; padding: 8px 12px 10px; border-bottom: 1px solid var(--border); font-weight: 600; }

.menu-item {
  display: flex;
  align-items: center;
  width: 100%;
  min-height: var(--tap);
  text-align: left;
  padding: 10px 12px;
  border: none;
  background: transparent;
  border-radius: 8px;
  font-family: inherit;
  font-size: 15px;
  color: var(--text);
  cursor: pointer;
  text-decoration: none;
}
.menu-item:active { background: #eceef1; }

/* ---------- Liste ---------- */

main { padding: 0 16px 120px; max-width: 1300px; margin: 0 auto; }

.toolbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin: 0 -16px 12px;
  padding: 10px 16px;
  background: var(--bg);
}

#search { flex: 1 1 100%; }
#filter-categorie { flex: 1 1 0; min-width: 0; }
.result-count { flex: 1 1 100%; color: var(--text-muted); font-size: 13px; }

.table-wrap { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th { font-size: 12px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--text-muted); background: #fafafa; text-align: left; }
th, td { padding: 10px 12px; border-bottom: 1px solid var(--border); white-space: nowrap; }
td.num, th.num { text-align: right; }
td.wrap { white-space: normal; }

.badge-low {
  display: inline-block;
  margin-left: 6px;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--warn-bg);
  color: var(--warn-text);
  font-size: 11px;
  font-weight: 600;
}

.empty { text-align: center; color: var(--text-muted); padding: 30px 16px; white-space: normal; }
.row-hint { display: none; }

.pager { display: flex; align-items: center; justify-content: center; gap: 12px; margin-top: 16px; }

/* Bouton flottant « Ajouter » */
.fab {
  position: fixed;
  right: 16px;
  bottom: max(16px, env(safe-area-inset-bottom));
  z-index: 30;
  min-height: 54px;
  padding: 0 22px;
  border-radius: 999px;
  border: none;
  background: var(--primary);
  color: #fff;
  font-family: inherit;
  font-size: 16px;
  font-weight: 700;
  box-shadow: 0 6px 18px rgba(200, 16, 46, 0.4);
  cursor: pointer;
}
.fab:active { background: var(--primary-dark); }

/* ---------- Modales : feuille plein écran sur téléphone ---------- */

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(20, 20, 25, 0.5);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 50;
}

.modal {
  background: var(--surface);
  width: 100%;
  max-height: 94vh;
  max-height: 94dvh;
  overflow-y: auto;
  overscroll-behavior: contain;
  border-radius: 18px 18px 0 0;
  padding: 16px 16px 0;
}

.modal-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.modal-header h2 { margin: 0; font-size: 18px; }
.modal h3 { font-size: 14px; margin: 18px 0 8px; }

.modal-close {
  width: var(--tap);
  height: var(--tap);
  margin-right: -10px;
  border: none;
  background: transparent;
  font-size: 28px;
  line-height: 1;
  cursor: pointer;
  color: var(--text-muted);
}

.modal-footer {
  position: sticky;
  bottom: 0;
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 16px -16px 0;
  padding: 12px 16px;
  padding-bottom: max(12px, env(safe-area-inset-bottom));
  background: var(--surface);
  border-top: 1px solid var(--border);
}
.modal-footer .btn-primary { flex: 1; }
.spacer { flex: 1; }
.modal-footer .spacer { flex: 0; }

.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 8px; }
.form-grid > label { grid-column: 1 / -1; }
.form-grid > label.half { grid-column: auto; }

.form-grid label, .stack label, .mapping-grid label {
  display: flex;
  flex-direction: column;
  gap: 5px;
  font-size: 13px;
  color: var(--text-muted);
  font-weight: 600;
}

.stack { display: flex; flex-direction: column; gap: 14px; }

.form-error { margin-top: 12px; padding: 10px 12px; background: var(--danger-bg); color: var(--danger); border-radius: 8px; font-size: 14px; }
.notice { padding: 10px 12px; background: var(--info-bg); color: var(--info-text); border-radius: 8px; font-size: 14px; margin-bottom: 12px; }
code { background: var(--chip); padding: 1px 5px; border-radius: 4px; font-size: 0.92em; overflow-wrap: anywhere; }

.mapping-grid { display: grid; grid-template-columns: 1fr; gap: 12px; margin: 14px 0; }
#import-file { margin-top: 10px; max-width: 100%; }
.preview-wrap { max-height: 220px; overflow: auto; }
#preview-table { min-width: 560px; }
.import-errors { margin-top: 10px; max-height: 200px; overflow-y: auto; font-size: 13px; color: var(--danger); }

/* ---------- Téléphone : tableaux transformés en fiches ---------- */

@media (max-width: 767px) {
  #table-articles, #table-articles tbody,
  #users-table, #users-table tbody,
  #activity-table, #activity-table tbody { display: block; }

  #table-articles thead, #users-table thead, #activity-table thead { display: none; }

  #table-articles, #users-table, #activity-table { min-width: 0; }
  .table-wrap:has(#table-articles), .table-wrap:has(#users-table), .table-wrap:has(#activity-table) {
    background: transparent;
    border: none;
    overflow: visible;
  }

  #table-articles td, #users-table td, #activity-table td { display: block; padding: 0; border: 0; white-space: normal; }
  #table-articles td:empty, #activity-table td:empty { display: none; }
  #table-articles .row-hint { display: none; }

  /* Fiche article */
  #table-articles tr[data-id] {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 6px 8px;
    margin-bottom: 10px;
    padding: 12px 14px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 14px;
    cursor: pointer;
  }
  #table-articles tr[data-id]:active { background: #f1f2f5; }
  #table-articles tr.low-stock { border-color: #f0c988; background: #fffbf2; }

  #table-articles .c-ref { order: 1; flex: 1 1 0; min-width: 0; font-weight: 700; font-size: 16px; overflow-wrap: anywhere; }
  #table-articles .c-stock { order: 2; flex: none; font-weight: 700; text-align: right; }
  #table-articles .c-stock::before { content: "Stock "; font-weight: 500; color: var(--text-muted); font-size: 13px; }
  #table-articles .c-stock .badge-low { margin: 4px 0 0 0; display: block; width: max-content; margin-left: auto; }
  #table-articles tr.low-stock .c-stock { color: var(--warn-text); }
  #table-articles .c-des { order: 3; flex: 1 1 100%; font-size: 15px; }
  #table-articles .c-cat, #table-articles .c-marque, #table-articles .c-empl {
    order: 4; flex: none; padding: 2px 10px; border-radius: 999px; background: var(--chip); color: var(--text-muted); font-size: 12px;
  }
  #table-articles .c-empl::before { content: "Empl. "; }
  #table-articles .c-vente, #table-articles .c-achat { order: 5; flex: none; font-size: 14px; font-weight: 600; font-variant-numeric: tabular-nums; }
  #table-articles .c-vente { margin-left: auto; }
  #table-articles .c-vente::before { content: "Vente "; font-weight: 500; color: var(--text-muted); font-size: 12px; }
  #table-articles .c-achat::before { content: "Achat "; font-weight: 500; color: var(--text-muted); font-size: 12px; }
  #table-articles .c-achat { color: var(--text-muted); font-weight: 500; }
  #table-articles .empty-row, #table-articles tr:not([data-id]) { display: block; }
  #table-articles tr:not([data-id]) td { padding: 30px 16px; text-align: center; }

  /* Fiche utilisateur */
  #users-table tr {
    display: block;
    margin-bottom: 10px;
    padding: 12px 14px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 14px;
  }
  #users-table td { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 6px 0; }
  #users-table td::before { content: attr(data-label); color: var(--text-muted); font-size: 13px; flex: none; }
  #users-table td:first-child { font-weight: 700; font-size: 16px; }
  #users-table td:first-child::before { display: none; }
  #users-table td select { width: auto; min-width: 0; flex: 1; max-width: 200px; }
  #users-table .row-actions { justify-content: flex-start; padding-top: 10px; }
  #users-table .row-actions::before { display: none; }
  #users-table .row-actions .btn { flex: 1; }

  /* Ligne du journal */
  #activity-table tr {
    display: flex;
    flex-wrap: wrap;
    gap: 2px 8px;
    padding: 10px 2px;
    border-bottom: 1px solid var(--border);
  }
  #activity-table .c-date { order: 1; font-size: 12px; color: var(--text-muted); flex: 1 1 100%; }
  #activity-table .c-user { order: 2; font-weight: 700; }
  #activity-table .c-action { order: 3; color: var(--primary); font-weight: 600; }
  #activity-table .c-detail { order: 4; flex: 1 1 100%; font-size: 13px; color: var(--text-muted); overflow-wrap: anywhere; }
}

/* ---------- Connexion ---------- */

.login-body { display: flex; align-items: center; justify-content: center; min-height: 100vh; min-height: 100dvh; padding: 16px; }

.login-card {
  width: 100%;
  max-width: 400px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 26px 20px 22px;
  box-shadow: 0 8px 30px rgba(20, 20, 30, 0.08);
}
.login-card .logo { margin-bottom: 14px; width: 44px; height: 44px; }
.login-card h1 { margin: 0 0 4px; font-size: 22px; }
.login-card .muted { margin: 0 0 20px; }
.login-card .btn { width: 100%; }

/* ---------- Ordinateur (768 px et plus) ---------- */

@media (min-width: 768px) {
  .desktop-only { display: inline-flex !important; }
  .mobile-only { display: none !important; }
  .fab { display: none; }

  body { font-size: 14px; }

  .topbar { padding: 14px 24px; }
  .topbar-title p { display: block; }
  .topbar-actions { gap: 10px; }
  .btn { min-height: 40px; padding: 8px 16px; font-size: 14px; }
  .btn-small { min-height: 32px; padding: 5px 10px; font-size: 12px; }
  input:not([type="checkbox"]):not([type="file"]):not([type="hidden"]), select { min-height: 40px; font-size: 14px; padding: 9px 10px; border-radius: 6px; }
  .menu summary { max-width: none; }
  .menu-panel { width: 260px; }
  .menu-item { min-height: 38px; font-size: 14px; }
  .menu-item:hover { background: #f2f3f5; }

  main { padding: 20px 24px 60px; }

  .toolbar { position: static; flex-wrap: nowrap; gap: 12px; margin: 0 0 14px; padding: 0; background: transparent; }
  #search { flex: 1 1 280px; }
  #filter-categorie { flex: 0 0 auto; width: auto; }
  .result-count { flex: 0 0 auto; white-space: nowrap; }

  table { min-width: 720px; }
  #users-table { min-width: 0; }
  #users-table td { white-space: normal; }
  #activity-table { min-width: 0; }
  th { position: sticky; top: 0; }
  td.wrap { min-width: 260px; }
  #table-articles td.c-des { white-space: normal; min-width: 200px; max-width: 380px; }
  #table-articles td.c-ref { max-width: 220px; overflow-wrap: anywhere; white-space: normal; }
  tbody tr[data-id] { cursor: pointer; }
  tbody tr[data-id]:hover { background: #fafbfc; }
  tbody tr.low-stock td.stock-cell { color: var(--warn-text); font-weight: 600; }
  .row-hint { display: table-cell; color: var(--text-muted); font-size: 12px; }
  .row-actions { display: flex; gap: 6px; justify-content: flex-end; flex-wrap: wrap; }

  .modal-overlay { align-items: center; padding: 20px; }
  .modal { max-width: 520px; max-height: 90vh; border-radius: 12px; padding: 20px 24px 24px; }
  .modal-wide { max-width: 820px; }
  .modal-close { width: 32px; height: 32px; margin-right: 0; font-size: 22px; }
  .modal-footer { position: static; margin: 18px 0 0; padding: 0; border-top: none; }
  .modal-footer .btn-primary { flex: none; }
  .modal-footer .spacer { flex: 1; }

  .form-grid { gap: 12px 16px; }
  .form-grid > label, .form-grid > label.half { grid-column: auto; }
  .form-grid label, .stack label, .mapping-grid label { font-size: 12px; }
  .mapping-grid { grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 10px 16px; }
  .activity-wrap { max-height: 60vh; overflow-y: auto; }

  .login-card { padding: 32px 28px 28px; }
}
