/* === Base === */
* { box-sizing: border-box; margin: 0; padding: 0; }
:root {
  --bg: #f5f7fb;
  --panel: #ffffff;
  --border: #e4e8ef;
  --text: #1f2937;
  --muted: #6b7280;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --success: #16a34a;
  --warning: #ea580c;
  --danger: #dc2626;
  --info: #0891b2;
  --sidebar-bg: #1e293b;
  --sidebar-text: #cbd5e1;
  --sidebar-active: #2563eb;
  --shadow: 0 1px 3px rgba(0,0,0,0.05), 0 1px 2px rgba(0,0,0,0.03);
  --radius: 8px;
}
html, body { height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  min-height: 100vh;
}
#app-shell { display: flex; min-height: 100vh; }

/* === Sidebar === */
.sidebar {
  width: 240px;
  background: var(--sidebar-bg);
  color: var(--sidebar-text);
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
}
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 18px 16px;
  border-bottom: 1px solid rgba(255,255,255,0.08);
}
.logo {
  width: 38px; height: 38px;
  border-radius: 8px;
  background: var(--primary);
  color: white;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700;
  font-size: 16px;
}
.brand-title { color: white; font-weight: 600; font-size: 15px; }
.brand-sub { color: #94a3b8; font-size: 11px; }
.nav { flex: 1; overflow-y: auto; padding: 10px 0; }
.nav-section {
  padding: 14px 16px 6px;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 1px;
  text-transform: uppercase;
  color: #64748b;
}
.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 16px;
  color: var(--sidebar-text);
  text-decoration: none;
  font-size: 13.5px;
  border-left: 3px solid transparent;
  transition: background 0.1s;
}
.nav-item:hover { background: rgba(255,255,255,0.05); color: white; }
.nav-item.active {
  background: rgba(37, 99, 235, 0.15);
  color: white;
  border-left-color: var(--primary);
}
.ic { font-size: 16px; width: 22px; text-align: center; }
.sidebar-footer {
  padding: 10px 16px;
  font-size: 11px;
  color: #64748b;
  border-top: 1px solid rgba(255,255,255,0.08);
}

/* === Main === */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.topbar {
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  padding: 14px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky;
  top: 0;
  z-index: 5;
}
.topbar-title { font-size: 18px; font-weight: 600; }
.topbar-actions { display: flex; gap: 8px; }
.content { padding: 20px 24px; flex: 1; }

/* === Buttons === */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 14px;
  border: 1px solid var(--border);
  background: white;
  color: var(--text);
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  transition: all 0.1s;
  text-decoration: none;
}
.btn:hover { background: #f9fafb; }
.btn-primary { background: var(--primary); color: white; border-color: var(--primary); }
.btn-primary:hover { background: var(--primary-hover); }
.btn-success { background: var(--success); color: white; border-color: var(--success); }
.btn-success:hover { background: #15803d; }
.btn-danger { background: var(--danger); color: white; border-color: var(--danger); }
.btn-danger:hover { background: #b91c1c; }
.btn-warning { background: var(--warning); color: white; border-color: var(--warning); }
.btn-info { background: var(--info); color: white; border-color: var(--info); }
.btn-sm { padding: 4px 10px; font-size: 12px; }
.btn-icon { padding: 6px 8px; }

/* === Cards & Panels === */
.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  box-shadow: var(--shadow);
  margin-bottom: 16px;
}
.card-title {
  font-size: 14px;
  font-weight: 600;
  margin-bottom: 12px;
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

/* === Stats === */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 14px;
  margin-bottom: 18px;
}
.stat {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  display: flex;
  align-items: center;
  gap: 14px;
  box-shadow: var(--shadow);
}
.stat-ic {
  width: 44px; height: 44px;
  border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
  font-size: 22px;
  background: #eff6ff;
}
.stat-ic.green { background: #dcfce7; }
.stat-ic.orange { background: #ffedd5; }
.stat-ic.red { background: #fee2e2; }
.stat-ic.purple { background: #f3e8ff; }
.stat-value { font-size: 22px; font-weight: 700; line-height: 1.1; }
.stat-label { font-size: 12px; color: var(--muted); margin-top: 2px; }

/* === Tables === */
.table-wrap {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: auto;
  box-shadow: var(--shadow);
}
table { width: 100%; border-collapse: collapse; }
th, td {
  padding: 10px 12px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}
th {
  background: #f9fafb;
  font-weight: 600;
  color: var(--muted);
  font-size: 11.5px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  position: sticky;
  top: 0;
}
tbody tr:hover { background: #f9fafb; }
td.actions { white-space: nowrap; text-align: right; }
.empty {
  padding: 40px 20px;
  text-align: center;
  color: var(--muted);
}
.empty .ic { font-size: 36px; display: block; margin-bottom: 10px; }

/* === Forms === */
.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 14px;
}
.form-row { display: flex; flex-direction: column; gap: 4px; }
.form-row.full { grid-column: 1 / -1; }
label {
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.3px;
}
input[type="text"], input[type="password"], input[type="number"], input[type="date"], input[type="email"], input[type="tel"], select, textarea {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 13px;
  font-family: inherit;
  background: white;
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}
textarea { resize: vertical; min-height: 60px; }

.toolbar {
  display: flex;
  gap: 8px;
  margin-bottom: 14px;
  align-items: center;
  flex-wrap: wrap;
}
.toolbar input[type="text"], .toolbar select {
  width: 220px;
}
.toolbar .spacer { flex: 1; }

/* === Badges === */
.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 10px;
  font-size: 11px;
  font-weight: 600;
  background: #e5e7eb;
  color: #374151;
}
.badge.green { background: #dcfce7; color: #166534; }
.badge.orange { background: #ffedd5; color: #9a3412; }
.badge.red { background: #fee2e2; color: #991b1b; }
.badge.blue { background: #dbeafe; color: #1e40af; }
.badge.gray { background: #f3f4f6; color: #4b5563; }

/* === Modal === */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.5);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 40px 20px;
  z-index: 100;
  overflow-y: auto;
}
.modal {
  background: white;
  border-radius: 12px;
  width: 100%;
  max-width: 720px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.2);
  animation: modalIn 0.15s ease-out;
}
.modal.lg { max-width: 980px; }
@keyframes modalIn {
  from { opacity: 0; transform: translateY(-10px); }
  to { opacity: 1; transform: translateY(0); }
}
.modal-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.modal-title { font-size: 16px; font-weight: 600; }
.modal-close {
  background: none;
  border: none;
  font-size: 22px;
  cursor: pointer;
  color: var(--muted);
  line-height: 1;
}
.modal-body { padding: 20px; max-height: calc(100vh - 200px); overflow-y: auto; }
.modal-footer {
  padding: 14px 20px;
  border-top: 1px solid var(--border);
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}

/* === Toast === */
#toast-root {
  position: fixed;
  bottom: 20px;
  right: 20px;
  z-index: 1000;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.toast {
  background: #1f2937;
  color: white;
  padding: 12px 18px;
  border-radius: 8px;
  font-size: 13px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.2);
  animation: toastIn 0.2s ease-out;
  max-width: 380px;
}
.toast.success { background: var(--success); }
.toast.error { background: var(--danger); }
.toast.warning { background: var(--warning); }
@keyframes toastIn {
  from { opacity: 0; transform: translateX(20px); }
  to { opacity: 1; transform: translateX(0); }
}

/* === Notifications (cloche + fenêtres) === */
.topbar-right { display: flex; align-items: center; gap: 12px; }
.notif-wrap { position: relative; }
.notif-btn {
  position: relative;
  background: none;
  border: 1px solid var(--border);
  border-radius: 8px;
  width: 38px; height: 36px;
  font-size: 17px;
  cursor: pointer;
}
.notif-btn:hover { background: #f3f4f6; }
.notif-btn.ring { animation: notifRing 0.8s ease-in-out 2; }
.notif-count {
  position: absolute; top: -6px; right: -6px;
  min-width: 18px; height: 18px; padding: 0 5px;
  border-radius: 9px;
  background: var(--danger); color: white;
  font-size: 11px; font-weight: 700; line-height: 18px;
  display: none;
}
.notif-count.show { display: block; }
.notif-panel {
  display: none;
  position: absolute; right: 0; top: 44px;
  width: 360px; max-height: 440px; overflow-y: auto;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.15);
  z-index: 900;
}
.notif-panel.open { display: block; }
.notif-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: 12px 14px; border-bottom: 1px solid var(--border);
  font-weight: 600; font-size: 14px;
}
.notif-item {
  display: block; width: 100%; text-align: left;
  padding: 10px 14px; border: none; border-bottom: 1px solid var(--border);
  background: none; cursor: pointer; font: inherit; color: var(--text);
}
.notif-item:hover { background: #f8fafc; }
.notif-item .t { font-weight: 600; font-size: 13px; }
.notif-item .s { font-size: 12px; color: var(--muted); margin-top: 2px; }
.notif-empty { padding: 24px 14px; text-align: center; color: var(--muted); font-size: 13px; }
.notif-foot { padding: 10px 14px; font-size: 12px; }

#notif-popups {
  position: fixed; right: 20px; bottom: 20px; z-index: 1100;
  display: flex; flex-direction: column; gap: 10px;
}
.notif-popup {
  width: 330px;
  background: var(--panel);
  border-left: 4px solid var(--primary);
  border-radius: 10px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.2);
  padding: 12px 14px;
  animation: toastIn 0.25s ease-out;
  cursor: pointer;
}
.notif-popup .t { font-weight: 700; font-size: 14px; margin-bottom: 4px; }
.notif-popup .s { font-size: 13px; color: var(--muted); }
.notif-popup .x { float: right; background: none; border: none; font-size: 18px; line-height: 1; color: var(--muted); cursor: pointer; }
@keyframes notifRing {
  0%, 100% { transform: rotate(0); }
  20% { transform: rotate(-15deg); }
  40% { transform: rotate(13deg); }
  60% { transform: rotate(-9deg); }
  80% { transform: rotate(6deg); }
}

/* === Document layout (DA, BC, BR, BS) === */
.doc-table { width: 100%; border-collapse: collapse; margin-top: 10px; }
.doc-table th, .doc-table td {
  border: 1px solid var(--border);
  padding: 6px 8px;
  font-size: 13px;
}
.doc-table input, .doc-table select {
  border: 1px solid #e5e7eb;
  padding: 5px 6px;
  font-size: 12.5px;
}
.doc-table .col-num { width: 40px; text-align: center; }
.doc-table .col-qty { width: 90px; }
.doc-table .col-price { width: 110px; }
.doc-table .col-actions { width: 50px; text-align: center; }
.doc-table tfoot td { font-weight: 600; background: #f9fafb; }

.doc-header-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-bottom: 14px;
}
.row-flex { display: flex; gap: 10px; align-items: center; }
.text-right { text-align: right; }
.text-center { text-align: center; }
.muted { color: var(--muted); font-size: 12px; }
.danger-text { color: var(--danger); }
.success-text { color: var(--success); }

/* === Print — variables dynamiques selon format papier (A5 par défaut) === */
:root {
  --paper-width: 148mm;
  --paper-padding: 10mm 12mm;
  --paper-font: 10.5px;
  --paper-company-font: 13px;
  --paper-title-font: 14px;
  --paper-meta-font: 10px;
  --paper-table-font: 9.5px;
  --paper-table-padding: 3px 4px;
  --paper-sig-font: 10px;
  --paper-sig-min-h: 18mm;
}
body[data-paper-format="A4"] {
  --paper-width: 210mm;
  --paper-padding: 18mm 20mm;
  --paper-font: 12.5px;
  --paper-company-font: 18px;
  --paper-title-font: 22px;
  --paper-meta-font: 13px;
  --paper-table-font: 12.5px;
  --paper-table-padding: 6px 8px;
  --paper-sig-font: 13px;
  --paper-sig-min-h: 30mm;
}

.print-area {
  background: white;
  padding: var(--paper-padding);
  max-width: var(--paper-width);
  margin: 0 auto;
  font-size: var(--paper-font);
  line-height: 1.35;
  color: #000;
}
.print-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  border-bottom: 2px solid #000;
  padding-bottom: 6px;
  margin-bottom: 8px;
}
.print-header > div { font-size: calc(var(--paper-font) - 1px); }
.print-company-name { font-size: var(--paper-company-font); font-weight: 700; margin-bottom: 2px; }
.print-doc-title {
  text-align: center;
  font-size: var(--paper-title-font);
  font-weight: 700;
  margin: 10px 0;
  text-transform: uppercase;
  letter-spacing: 1px;
  border: 1.5px solid #000;
  padding: 5px 0;
}
.print-doc-meta {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 2px 14px;
  margin-bottom: 10px;
  font-size: var(--paper-meta-font);
}
.print-doc-meta div { padding: 1px 0; }
.print-table { width: 100%; border-collapse: collapse; margin-top: 6px; }
.print-table th, .print-table td {
  border: 1px solid #000;
  padding: var(--paper-table-padding);
  font-size: var(--paper-table-font);
}
.print-table th { background: #f0f0f0; font-weight: 700; }
.print-signatures {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 14px;
  margin-top: 28px;
  font-size: var(--paper-sig-font);
}
.print-sig {
  border-top: 1px solid #000;
  padding-top: 3px;
  text-align: center;
  min-height: var(--paper-sig-min-h);
}

@media print {
  .sidebar, .topbar, .modal-footer, .no-print { display: none !important; }
  body { background: white; }
  .main { display: block; }
  .content { padding: 0; }
  .modal-backdrop { position: static; background: white; padding: 0; display: block; }
  .modal, .modal.lg { max-width: 100%; box-shadow: none; }
  .modal-header { border: none; padding: 0; }
  .modal-title { display: none; }
  .modal-body { max-height: none; overflow: visible; padding: 0; }
  .print-area { padding: 0; max-width: 100%; }
}

/* === Login screen === */
.login-screen {
  position: fixed;
  inset: 0;
  background: linear-gradient(135deg, #1e293b 0%, #2563eb 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  padding: 20px;
}
.login-card {
  background: white;
  padding: 40px 36px;
  border-radius: 16px;
  width: 100%;
  max-width: 400px;
  box-shadow: 0 25px 80px rgba(0,0,0,0.3);
  text-align: center;
}
.login-logo {
  width: 70px; height: 70px;
  border-radius: 18px;
  background: var(--primary);
  color: white;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700;
  font-size: 28px;
  margin: 0 auto 18px;
}
.login-title { font-size: 22px; font-weight: 700; margin-bottom: 4px; }
.login-sub { color: var(--muted); font-size: 13px; margin-bottom: 22px; }
.login-error {
  color: var(--danger);
  font-size: 13px;
  min-height: 18px;
  margin-bottom: 10px;
  text-align: left;
}
.login-remember {
  display: flex; align-items: center; gap: 8px;
  margin: 2px 0 14px; font-size: 13px; color: var(--text);
  font-weight: 400; text-transform: none; letter-spacing: 0;
  cursor: pointer; user-select: none; text-align: left;
}
.login-remember input { width: 16px; height: 16px; accent-color: var(--primary); cursor: pointer; }
.login-footer { margin-top: 18px; font-size: 11px; color: var(--muted); }
.sidebar-user {
  color: white;
  padding: 8px 12px;
  background: rgba(255,255,255,0.06);
  border-radius: 6px;
  font-size: 12px;
  margin-bottom: 8px;
}
.sidebar-user b { display: block; font-size: 13px; }
.sidebar-user .role { color: #94a3b8; font-size: 11px; }

/* === Reports / Charts === */
.report-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(420px, 1fr));
  gap: 14px;
}
.chart-wrap {
  position: relative;
  height: 280px;
}

/* === Barcode print === */
.barcode-labels {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  padding: 6mm;
}
.barcode-label {
  border: 1px dashed #ccc;
  padding: 8px;
  text-align: center;
  font-size: 11px;
  page-break-inside: avoid;
}
.barcode-label svg { max-width: 100%; height: auto; }
.barcode-label .label-name { font-size: 10px; margin-bottom: 2px; }
.barcode-label .label-price { font-size: 11px; font-weight: 600; margin-top: 2px; }

/* === Import wizard === */
.import-step {
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  margin-bottom: 12px;
}
.import-preview-table {
  font-size: 12px;
  max-height: 280px;
  overflow: auto;
}
.import-preview-table table { font-size: 12px; }
.import-preview-table td.err { background: #fee2e2; color: #991b1b; }
.import-mapping {
  display: grid;
  grid-template-columns: 180px 1fr;
  gap: 6px 12px;
  align-items: center;
  margin-top: 10px;
}
.import-mapping label { text-align: right; }

/* === Scan input === */
.scan-bar {
  display: flex;
  gap: 8px;
  margin-bottom: 10px;
  padding: 10px;
  background: #fef3c7;
  border: 1px dashed #f59e0b;
  border-radius: 6px;
}
.scan-bar input {
  flex: 1;
  border: 1px solid #f59e0b;
  background: white;
}
.scan-bar .scan-info { font-size: 11px; color: #92400e; align-self: center; }
