:root {
  --primary: #4f46e5;
  --primary-hover: #4338ca;
  --success: #16a34a;
  --warning: #f59e0b;
  --danger: #dc2626;
  --gray-50: #f9fafb;
  --gray-100: #f3f4f6;
  --gray-200: #e5e7eb;
  --gray-300: #d1d5db;
  --gray-500: #6b7280;
  --gray-700: #374151;
  --gray-900: #111827;
  --bg: #f9fafb;
  --card-bg: #ffffff;
  --border: #e5e7eb;
  --radius: 8px;
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.05);
  --shadow: 0 1px 3px rgba(0,0,0,0.1), 0 1px 2px rgba(0,0,0,0.06);
}

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

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  font-size: 14px;
  color: var(--gray-900);
  background: var(--bg);
  line-height: 1.5;
}

.app {
  display: flex;
  height: 100vh;
}

/* SIDEBAR */
.sidebar {
  width: 240px;
  background: white;
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 20px 0;
  flex-shrink: 0;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 20px 20px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 16px;
}
.logo { font-size: 24px; }
.brand-name { font-size: 18px; font-weight: 700; color: var(--primary); }

.nav { display: flex; flex-direction: column; padding: 0 12px; gap: 4px; flex: 1; }
.nav-link {
  display: block;
  padding: 10px 12px;
  color: var(--gray-700);
  text-decoration: none;
  border-radius: var(--radius);
  font-weight: 500;
  font-size: 14px;
  transition: all 0.15s;
}
.nav-link:hover { background: var(--gray-100); }
.nav-link.active { background: var(--primary); color: white; }

.sidebar-footer { padding: 16px 20px; color: var(--gray-500); border-top: 1px solid var(--border); }

/* MAIN */
.main {
  flex: 1;
  overflow-y: auto;
  padding: 32px 40px;
}

.view { display: none; }
.view.active { display: block; }

.view-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
  flex-wrap: wrap;
  gap: 12px;
}
.view-header h1 { font-size: 24px; font-weight: 700; }

.muted { color: var(--gray-500); font-size: 13px; }

/* BUTTONS */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 14px;
  background: white;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  color: var(--gray-700);
  transition: all 0.15s;
  text-decoration: none;
}
.btn:hover { background: var(--gray-50); border-color: var(--gray-300); }
.btn.primary { background: var(--primary); color: white; border-color: var(--primary); }
.btn.primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); }
.btn.success { background: var(--success); color: white; border-color: var(--success); }
.btn.danger { background: var(--danger); color: white; border-color: var(--danger); }
.btn.small { padding: 5px 10px; font-size: 12px; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* CARDS */
.card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 20px;
  box-shadow: var(--shadow-sm);
}
.card h2 { font-size: 16px; font-weight: 600; margin-bottom: 16px; color: var(--gray-900); }
.card-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px; }
.card-header h2 { margin-bottom: 0; }

/* STATS */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}
.stat-card {
  background: white;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow-sm);
}
.stat-label { font-size: 12px; color: var(--gray-500); text-transform: uppercase; letter-spacing: 0.5px; }
.stat-value { font-size: 24px; font-weight: 700; color: var(--gray-900); margin-top: 4px; }
.stat-card.highlight { background: linear-gradient(135deg, var(--primary), var(--primary-hover)); color: white; border: none; }
.stat-card.highlight .stat-label, .stat-card.highlight .stat-value { color: white; }

/* FIELDS */
.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; }
.field label { font-size: 12px; font-weight: 500; color: var(--gray-700); }
.field input, .field textarea, .field select {
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 14px;
  font-family: inherit;
  background: white;
  color: var(--gray-900);
  transition: border-color 0.15s;
}
.field input:focus, .field textarea:focus, .field select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(79,70,229,0.1);
}

.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
.grid-2 { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; }
.span-1 { grid-column: span 1; }
.span-2 { grid-column: span 2; }
.span-3 { grid-column: span 3; }

@media (max-width: 768px) {
  .grid-3 { grid-template-columns: 1fr; }
  .span-2, .span-3 { grid-column: span 1; }
  .app { flex-direction: column; height: auto; }
  .sidebar { width: 100%; flex-direction: row; padding: 12px; }
  .nav { flex-direction: row; flex-wrap: wrap; padding: 0; }
  .main { padding: 20px; }
}

/* TABLES */
.table { width: 100%; border-collapse: collapse; }
.table th, .table td {
  padding: 10px 12px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}
.table th { background: var(--gray-50); font-weight: 600; color: var(--gray-700); font-size: 12px; text-transform: uppercase; letter-spacing: 0.3px; }
.table .right { text-align: right; }
.table tbody tr:hover { background: var(--gray-50); }

/* LIGNES TABLE */
.lignes-table input, .lignes-table select, .lignes-table textarea {
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 4px;
  font-size: 13px;
  width: 100%;
  font-family: inherit;
  resize: vertical;
  vertical-align: top;
}
.lignes-table input:focus, .lignes-table select:focus, .lignes-table textarea:focus { outline: none; border-color: var(--primary); }
.lignes-table td { padding: 8px; vertical-align: top; }
.lignes-table .col-designation { min-width: 160px; max-width: 280px; }
.lignes-table .col-details { min-width: 240px; }
.lignes-table .lg-details {
  min-height: 36px;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 4px;
  font-size: 13px;
  line-height: 1.45;
  background: white;
  outline: none;
  font-family: inherit;
  word-break: break-word;
  overflow-wrap: anywhere;
}
.lignes-table .lg-details:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(79,70,229,0.1); }
.lignes-table .lg-details--empty::before {
  content: attr(data-placeholder);
  color: var(--gray-300);
  font-style: italic;
  pointer-events: none;
}
.lignes-table .col-qty, .lignes-table .col-pu, .lignes-table .col-tva { width: 80px; }
.lignes-table .col-total { font-weight: 600; text-align: right; white-space: nowrap; width: 110px; min-width: 110px; }
.lignes-table .col-del { width: 40px; vertical-align: middle; }
.lignes-table tr { vertical-align: top; }
.lignes-table thead th { vertical-align: top; }

/* Force les inputs à rester en haut de leur cellule */
.lignes-table input[type="number"],
.lignes-table input[type="text"] {
  vertical-align: top;
  height: 32px;
}

/* TOTAUX */
.totaux {
  margin-top: 16px;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 4px;
}
.totaux-line {
  display: flex;
  justify-content: space-between;
  width: 280px;
  padding: 6px 0;
  font-size: 14px;
}
.totaux-line.grand {
  font-size: 18px;
  font-weight: 700;
  border-top: 2px solid var(--gray-900);
  padding-top: 10px;
  margin-top: 4px;
}

/* FILTERS */
.filters {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}
.filters input, .filters select {
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 13px;
  background: white;
}
.filters input { flex: 1; min-width: 200px; }

/* STATUS BADGES */
.badge {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 12px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.3px;
}
.badge-draft    { background: #e5e7eb; color: #374151; }
.badge-sent     { background: #dbeafe; color: #1e40af; }
.badge-accepted { background: #d1fae5; color: #065f46; }
.badge-refused  { background: #fee2e2; color: #991b1b; }
.badge-paid     { background: #d1fae5; color: #065f46; }

/* EMPTY */
.empty {
  text-align: center;
  padding: 60px 20px;
  color: var(--gray-500);
}
.hidden { display: none !important; }

/* MODAL */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 20px;
}
.modal-content {
  background: white;
  border-radius: var(--radius);
  max-width: 900px;
  width: 100%;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
}
.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 24px;
  border-bottom: 1px solid var(--border);
}
.preview-content {
  overflow-y: auto;
  padding: 24px;
  background: var(--gray-100);
  flex: 1;
}

/* PREVIEW / PDF — looks like real paper */
.devis-preview {
  background: white;
  padding: 40px;
  max-width: 800px;
  margin: 0 auto;
  box-shadow: var(--shadow);
  font-size: 12px;
  line-height: 1.4;
  color: var(--gray-900);
}
.preview-header { display: flex; justify-content: space-between; align-items: flex-start; border-bottom: 3px solid var(--primary); padding-bottom: 20px; margin-bottom: 20px; }
.preview-emetteur h2 { font-size: 18px; color: var(--primary); margin-bottom: 6px; }
.preview-emetteur p { margin: 1px 0; font-size: 11px; }
.preview-logo img { max-height: 70px; max-width: 180px; }
.preview-title { text-align: right; }
.preview-title h1 { font-size: 26px; color: var(--primary); }
.preview-title .numero { font-size: 16px; font-weight: 700; margin-top: 4px; color: var(--primary); }
.preview-title .dates { font-size: 11px; color: var(--gray-500); margin-top: 4px; }
.preview-client { margin: 20px 0; padding: 12px; background: var(--gray-50); border-left: 4px solid var(--primary); }
.preview-client .label { font-size: 10px; text-transform: uppercase; color: var(--gray-500); margin-bottom: 4px; }
.preview-client .nom { font-weight: 600; font-size: 13px; }
.preview-objet { margin: 16px 0; font-size: 12px; }
.preview-table { width: 100%; border-collapse: collapse; margin: 16px 0; font-size: 11px; table-layout: fixed; }
.preview-table th { background: var(--primary); color: white; padding: 8px 6px; text-align: left; vertical-align: top; }
.preview-table th.right { text-align: right; }
.preview-table td { padding: 6px; border-bottom: 1px solid var(--border); vertical-align: top; word-wrap: break-word; overflow-wrap: break-word; }
.preview-table td.right { text-align: right; }
.preview-table tbody tr { vertical-align: top; }
.preview-totaux { display: flex; justify-content: flex-end; margin-top: 16px; }
.preview-totaux-box { width: 280px; }
.preview-totaux-box .line { display: flex; justify-content: space-between; padding: 4px 0; }
.preview-totaux-box .line.grand { border-top: 2px solid var(--gray-900); font-weight: 700; font-size: 14px; padding-top: 8px; margin-top: 4px; }
.preview-conditions { margin-top: 24px; padding-top: 16px; border-top: 1px solid var(--border); font-size: 11px; }
.preview-conditions h3 { font-size: 12px; margin: 8px 0 4px; }
.preview-conditions p, .preview-conditions div { white-space: pre-wrap; }

/* Détails d'une ligne dans le PDF (HTML riche rendu) */
.ligne-details-pdf {
  font-size: 10px;
  color: #6b7280;
  margin-top: 3px;
  line-height: 1.35;
}
.ligne-details-pdf ul, .ligne-details-pdf ol { margin: 2px 0 2px 16px; padding: 0; }

/* EMETTEUR DISPLAY */
.emetteur-display {
  background: var(--gray-50);
  padding: 12px;
  border-radius: var(--radius);
  font-size: 13px;
}
.emetteur-display .empty { padding: 12px; }

/* REQUIRED asterisk */
.required { color: var(--danger); font-weight: 700; margin-left: 2px; }

/* Hardcoded block (RIB figé) */
.hardcoded-block {
  padding: 10px 12px;
  background: #fef9c3;
  border: 1px solid #facc15;
  border-radius: var(--radius);
  font-size: 13px;
  color: #713f12;
  white-space: pre-wrap;
  line-height: 1.55;
}
.hardcoded-block code {
  background: rgba(0,0,0,0.08);
  padding: 1px 5px;
  border-radius: 3px;
  font-size: 12px;
}

/* Field error highlight (rouge pulsant) */
.field-error {
  animation: fieldShake 0.45s ease-out;
  border-color: var(--danger) !important;
  background: #fef2f2 !important;
}
.field-error label { color: var(--danger) !important; }
@keyframes fieldShake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-6px); }
  40% { transform: translateX(6px); }
  60% { transform: translateX(-4px); }
  80% { transform: translateX(4px); }
}

/* Rich text toolbar */
.rt-toolbar {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 8px;
  background: #f8fafc;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 10px;
  flex-wrap: wrap;
}
.rt-label { font-size: 12px; color: var(--gray-500); margin-right: 6px; font-weight: 500; }
.rt-btn {
  background: white;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 4px 8px;
  font-size: 13px;
  cursor: pointer;
  color: var(--gray-700);
  min-width: 30px;
  height: 28px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.rt-btn:hover { background: var(--gray-50); border-color: var(--gray-300); }
.rt-btn:active { background: var(--gray-100); }
.rt-btn-danger { color: var(--danger); }
.rt-btn-danger:hover { background: #fee2e2; }
.rt-sep { width: 1px; height: 20px; background: var(--border); margin: 0 4px; }
.rt-color {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  color: transparent;
  width: 30px;
  height: 28px;
  border: 1px solid var(--border);
  border-radius: 4px;
  cursor: pointer;
  background: white;
  overflow: hidden;
  font-size: 14px;
}
.rt-color input[type="color"] {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  border: none;
  padding: 0;
  cursor: pointer;
  opacity: 0;
}
.rt-color:hover { background: var(--gray-50); }

/* TOAST */
.toast {
  position: fixed;
  bottom: 20px;
  right: 20px;
  background: var(--gray-900);
  color: white;
  padding: 12px 20px;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  z-index: 2000;
  font-size: 13px;
  font-weight: 500;
  animation: slideIn 0.3s ease-out;
}
.toast.success { background: var(--success); }
.toast.error   { background: var(--danger); }
@keyframes slideIn { from { transform: translateX(100%); opacity: 0; } to { transform: translateX(0); opacity: 1; } }