/* ============ TOKENS (cohérents avec befast.ma) ============ */
:root {
  --navy: #1e3a8a;
  --navy-dark: #172d6b;
  --navy-tint: rgba(30,58,138,0.08);
  --bg: #f8fafc;
  --bg-alt: #eef2f7;
  --white: #ffffff;
  --text: #0f172a;
  --text-dim: #475569;
  --text-faint: #64748b;
  --border: #e2e8f0;
  --border-strong: #cbd5e1;
  --green: #10b981;
  --green-tint: rgba(16,185,129,0.1);
  --orange: #ea580c;
  --orange-tint: rgba(234,88,12,0.1);
  --red: #dc2626;
  --red-tint: rgba(220,38,38,0.1);
  --gray: #64748b;
  --gray-tint: rgba(100,116,139,0.1);
  --radius: 12px;
  --radius-sm: 8px;
}

* , *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.5;
}
a { color: inherit; text-decoration: none; }
ul { list-style: none; }
button { font-family: inherit; cursor: pointer; }
input, select, textarea { font-family: inherit; font-size: 0.92rem; }
h1, h2, h3 { font-weight: 700; letter-spacing: -0.01em; }

/* ============ LAYOUT ============ */
.app-shell { display: flex; min-height: 100vh; }

.sidebar {
  width: 250px;
  flex-shrink: 0;
  background: var(--white);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0; bottom: 0; left: 0;
  z-index: 100;
  transition: transform .25s ease;
}
.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 22px 24px;
  border-bottom: 1px solid var(--border);
  font-family: 'Inter', sans-serif;
  font-weight: 800;
  font-size: 1.15rem;
}
.sidebar-brand .logo-be { color: var(--text); }
.sidebar-brand .logo-fast { color: var(--navy); }
.sidebar-brand small { display: block; font-size: 0.68rem; font-weight: 600; color: var(--text-faint); letter-spacing: 0.04em; text-transform: uppercase; }

.sidebar-nav { flex: 1; padding: 16px 12px; overflow-y: auto; }
.sidebar-nav a {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  color: var(--text-dim);
  font-size: 0.9rem;
  font-weight: 500;
  margin-bottom: 2px;
  transition: background .15s, color .15s;
}
.sidebar-nav a svg { width: 18px; height: 18px; flex-shrink: 0; }
.sidebar-nav a:hover { background: var(--bg-alt); color: var(--text); }
.sidebar-nav a.active { background: var(--navy-tint); color: var(--navy); font-weight: 600; }
.sidebar-nav .nav-section-label {
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-faint);
  padding: 16px 14px 6px;
}

.sidebar-footer { padding: 16px; border-top: 1px solid var(--border); }
.sidebar-user { display: flex; align-items: center; gap: 10px; padding: 8px; border-radius: var(--radius-sm); }
.sidebar-user-avatar {
  width: 34px; height: 34px;
  border-radius: 50%;
  background: var(--navy);
  color: #fff;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 0.85rem;
  flex-shrink: 0;
}
.sidebar-user-info { flex: 1; min-width: 0; }
.sidebar-user-name { font-size: 0.85rem; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sidebar-user-role { font-size: 0.72rem; color: var(--text-faint); text-transform: capitalize; }
.sidebar-logout-btn {
  width: 100%;
  margin-top: 10px;
  padding: 8px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--white);
  color: var(--text-dim);
  font-size: 0.82rem;
  font-weight: 600;
  display: flex; align-items: center; justify-content: center; gap: 6px;
}
.sidebar-logout-btn:hover { border-color: var(--red); color: var(--red); }

.main-content { flex: 1; margin-left: 250px; min-width: 0; }
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 32px;
  background: var(--white);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 50;
}
.topbar h1 { font-size: 1.3rem; }
.topbar-actions { display: flex; gap: 10px; align-items: center; }
.mobile-menu-toggle { display: none; background: none; border: none; padding: 8px; }
.mobile-menu-toggle svg { width: 22px; height: 22px; }

.page-body { padding: 32px; max-width: 1280px; }

/* ============ BUTTONS ============ */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 10px 18px;
  border-radius: var(--radius-sm);
  font-weight: 600;
  font-size: 0.88rem;
  border: 1px solid transparent;
  transition: all .15s;
  white-space: nowrap;
  background: none;
}
.btn svg { width: 15px; height: 15px; }
.btn-primary { background: var(--navy); color: #fff; }
.btn-primary:hover { background: var(--navy-dark); }
.btn-outline { background: var(--white); color: var(--text-dim); border-color: var(--border-strong); }
.btn-outline:hover { border-color: var(--navy); color: var(--navy); }
.btn-danger { background: var(--white); color: var(--red); border-color: var(--red-tint); }
.btn-danger:hover { background: var(--red-tint); }
.btn-sm { padding: 6px 12px; font-size: 0.8rem; }
.btn-block { width: 100%; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* ============ CARDS / TABLES ============ */
.card {
  background: var(--white);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  animation: card-in .35s ease both;
}
.card + .card { margin-top: 20px; }
.stat-card { animation: card-in .35s ease both; }
.stats-grid .stat-card:nth-child(2) { animation-delay: .04s; }
.stats-grid .stat-card:nth-child(3) { animation-delay: .08s; }
.stats-grid .stat-card:nth-child(4) { animation-delay: .12s; }
@keyframes card-in {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}
@media (prefers-reduced-motion: reduce) {
  .card, .stat-card { animation: none; }
}

.btn.is-loading { position: relative; color: transparent !important; pointer-events: none; }
.btn.is-loading::after {
  content: '';
  position: absolute;
  width: 15px; height: 15px;
  border-radius: 50%;
  border: 2px solid rgba(255,255,255,0.4);
  border-top-color: #fff;
  animation: btn-spin .6s linear infinite;
}
.btn-outline.is-loading::after, .btn-danger.is-loading::after {
  border-color: rgba(30,58,138,0.25); border-top-color: var(--navy);
}
@keyframes btn-spin { to { transform: rotate(360deg); } }

.stats-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; margin-bottom: 24px; }
.stat-card { background: var(--white); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; }
.stat-card .stat-label { font-size: 0.8rem; color: var(--text-faint); margin-bottom: 8px; }
.stat-card .stat-value { font-size: 1.7rem; font-weight: 800; color: var(--text); }
.stat-card .stat-value.navy { color: var(--navy); }
.stat-card .stat-value.green { color: var(--green); }
.stat-card .stat-value.orange { color: var(--orange); }
.stat-card-featured {
  background: linear-gradient(135deg, var(--navy) 0%, var(--navy-dark) 100%);
  border-color: var(--navy-dark);
}
.stat-card-featured .stat-label { color: rgba(255,255,255,0.65); }
.stat-card-featured .stat-value { color: #fff; font-size: 2rem; }
.stat-card-featured .stat-value.orange { color: #fda98a; }

/* overflow-x:auto seul ne suffit pas : sans min-width, le tableau se contente
   de tasser ses colonnes sur petit écran (texte tronqué illisible) au lieu de
   déborder proprement. Le min-width force le vrai scroll horizontal, et le
   double dégradé applique une ombre de bord qui indique visuellement qu'il y
   a plus de contenu à faire défiler (disparaît naturellement une fois arrivé
   au bout grâce à background-attachment:local sur le fond blanc). */
.table-wrap {
  overflow-x: auto;
  background:
    linear-gradient(to right, var(--white) 50%, rgba(255,255,255,0)),
    linear-gradient(to right, rgba(255,255,255,0), var(--white) 50%) 100% 0,
    linear-gradient(to right, rgba(15,23,42,0.12), rgba(15,23,42,0)),
    linear-gradient(to left, rgba(15,23,42,0.12), rgba(15,23,42,0)) 100% 0;
  background-repeat: no-repeat;
  background-color: var(--white);
  background-size: 24px 100%, 24px 100%, 10px 100%, 10px 100%;
  background-attachment: local, local, scroll, scroll;
}
table { width: 100%; min-width: 640px; border-collapse: collapse; }
thead th {
  text-align: left;
  font-size: 0.76rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-faint);
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
tbody td { padding: 14px; border-bottom: 1px solid var(--border); font-size: 0.9rem; vertical-align: middle; }
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: var(--bg-alt); }
.table-empty { text-align: center; padding: 48px 20px; color: var(--text-faint); }
.empty-state { text-align: center; padding: 56px 24px; }
.empty-state-icon {
  width: 52px; height: 52px; margin: 0 auto 16px;
  border-radius: 50%; background: var(--bg-alt);
  display: flex; align-items: center; justify-content: center;
}
.empty-state-icon svg { width: 24px; height: 24px; color: var(--text-faint); }
.empty-state-title { font-size: 0.95rem; font-weight: 600; color: var(--text-dim); margin-bottom: 6px; }
.empty-state-hint { font-size: 0.82rem; color: var(--text-faint); margin-bottom: 20px; }

/* ============ PROGRESSION (objectifs) ============ */
.progress-track {
  width: 100%;
  height: 8px;
  background: var(--bg-alt);
  border-radius: 100px;
  overflow: hidden;
}
.progress-fill {
  height: 100%;
  background: var(--navy);
  border-radius: 100px;
  transition: width .2s;
}
.progress-fill.complete { background: var(--green); }
.progress-label { font-size: 0.78rem; color: var(--text-faint); margin-top: 4px; }

.category-row td {
  background: var(--bg-alt);
  font-size: 0.74rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-dim);
  padding: 8px 14px;
}
.category-row:hover td { background: var(--bg-alt); }

/* status badges */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 0.76rem;
  font-weight: 700;
  padding: 4px 10px;
  border-radius: 100px;
  text-transform: capitalize;
}
.badge-gray { background: var(--bg-alt); color: var(--text-dim); border: 1px solid var(--border-strong); }
.badge-navy { background: var(--navy-tint); color: var(--navy); }
.badge-green { background: var(--green-tint); color: var(--green); }
.badge-orange { background: var(--orange-tint); color: var(--orange); }
.badge-red { background: var(--red-tint); color: var(--red); }

/* ============ FORMS ============ */
.form-group { margin-bottom: 18px; }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
label { display: block; font-size: 0.85rem; font-weight: 600; color: var(--text-dim); margin-bottom: 6px; }
input[type="text"], input[type="email"], input[type="tel"], input[type="password"],
input[type="number"], input[type="date"], select, textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--white);
  color: var(--text);
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--navy);
  box-shadow: 0 0 0 3px rgba(30,58,138,0.12);
}
textarea { resize: vertical; }
.form-actions { display: flex; gap: 10px; margin-top: 24px; }
.form-error {
  background: var(--red-tint);
  color: var(--red);
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
  margin-bottom: 18px;
  font-weight: 600;
}
.form-hint { font-size: 0.78rem; color: var(--text-faint); margin-top: 4px; }

/* ============ LOGIN PAGE ============ */
.login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background:
    radial-gradient(600px 400px at 15% 15%, rgba(30,58,138,0.10), rgba(30,58,138,0) 60%),
    radial-gradient(500px 500px at 85% 85%, rgba(234,88,12,0.07), rgba(234,88,12,0) 60%),
    var(--bg-alt);
  padding: 20px;
}
.login-card {
  background: var(--white);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  width: 100%;
  max-width: 400px;
  box-shadow: 0 24px 48px -20px rgba(15,23,42,0.2);
  overflow: hidden;
}
.login-card-header {
  background: linear-gradient(135deg, var(--navy) 0%, var(--navy-dark) 100%);
  padding: 32px 44px 26px;
  border-bottom: 3px solid #60a5fa;
}
.login-logo { text-align: center; font-family: 'Inter', sans-serif; font-weight: 800; font-size: 1.5rem; margin-bottom: 6px; color: #fff; }
.login-logo .logo-fast { color: #60a5fa; }
.login-subtitle { text-align: center; color: rgba(255,255,255,0.7); font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.05em; font-weight: 600; }
.login-card-body { padding: 36px 44px 44px; }

/* ============ ITEMS BUILDER (devis/factures) ============ */
.items-table { width: 100%; border-collapse: collapse; margin-bottom: 12px; }
.items-table th { text-align: left; font-size: 0.75rem; text-transform: uppercase; color: var(--text-faint); padding: 6px 8px; }
.items-table td { padding: 6px 8px; vertical-align: top; }
.items-table input, .items-table select { padding: 8px 10px; font-size: 0.85rem; }
.item-remove-btn {
  background: var(--red-tint); color: var(--red); border: none; border-radius: var(--radius-sm);
  width: 32px; height: 32px; display: flex; align-items: center; justify-content: center;
}
.item-remove-btn svg { width: 14px; height: 14px; }
.totals-box { max-width: 320px; margin-left: auto; }
.totals-row { display: flex; justify-content: space-between; padding: 8px 0; font-size: 0.9rem; color: var(--text-dim); }
.totals-row.total { border-top: 1px solid var(--border); margin-top: 4px; padding-top: 12px; font-size: 1.1rem; font-weight: 800; color: var(--text); }
.totals-row input { width: 80px; text-align: right; }

.page-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 20px; flex-wrap: wrap; gap: 12px; }
.filters-row { display: flex; gap: 10px; margin-bottom: 20px; flex-wrap: wrap; }
.filters-row select, .filters-row input { width: auto; }

.doc-preview { background: var(--white); border: 1px solid var(--border); border-radius: var(--radius); padding: 48px; max-width: 800px; }
.doc-preview-header { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 40px; padding-bottom: 24px; border-bottom: 2px solid var(--navy); }
.doc-preview-header .logo { font-size: 1.4rem; font-weight: 800; }
.doc-preview-header .logo .logo-fast { color: var(--navy); }
.doc-preview-meta { text-align: right; font-size: 0.85rem; color: var(--text-dim); }
.doc-preview-meta strong { display: block; font-size: 1.1rem; color: var(--navy); margin-bottom: 4px; }
.doc-parties { display: grid; grid-template-columns: 1fr 1fr; gap: 24px; margin-bottom: 32px; }
.doc-parties h4 { font-size: 0.75rem; text-transform: uppercase; color: var(--text-faint); margin-bottom: 8px; }
.doc-parties p { font-size: 0.9rem; color: var(--text); line-height: 1.6; }

@media print {
  .no-print { display: none !important; }
  .doc-preview { border: none; box-shadow: none; max-width: 100%; padding: 0; }
  body { background: #fff; }
}

/* ============ RESPONSIVE ============ */
@media (max-width: 900px) {
  .sidebar { transform: translateX(-100%); box-shadow: 0 0 0 9999px rgba(0,0,0,0); }
  .sidebar.open { transform: translateX(0); box-shadow: 20px 0 40px rgba(0,0,0,0.15); }
  .main-content { margin-left: 0; }
  .mobile-menu-toggle { display: flex; align-items: center; }
  .stats-grid { grid-template-columns: 1fr 1fr; }
  .form-row { grid-template-columns: 1fr; }
  .page-body { padding: 20px; }
  .topbar { padding: 16px 20px; }
  .doc-parties { grid-template-columns: 1fr; }
}

@media (max-width: 700px) {
  .items-table thead { display: none; }
  .items-table, .items-table tbody, .items-table tr, .items-table td {
    display: block;
    width: 100% !important;
  }
  .items-table tr.item-row {
    position: relative;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 14px 44px 14px 14px;
    margin-bottom: 12px;
  }
  .items-table td { padding: 6px 0; }
  .items-table td[data-label]::before {
    content: attr(data-label);
    display: block;
    font-size: 0.72rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--text-faint);
    margin-bottom: 4px;
  }
  .items-table td:last-child {
    position: absolute;
    top: 10px;
    right: 10px;
    padding: 0;
    width: auto !important;
  }
  .items-table .line-total { font-weight: 700; font-size: 1rem; }
}
