:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f9fafb;
  --border: #e5e7eb;
  --border-strong: #d1d5db;
  --text: #111827;
  --text-2: #374151;
  --muted: #6b7280;
  --accent: #1557c0;
  --accent-2: #0f469e;
  --accent-soft: #eaf1fd;
  --green: #047857;
  --green-soft: #ecfdf5;
  --amber: #b45309;
  --amber-soft: #fffbeb;
  --red: #b91c1c;
  --red-soft: #fef2f2;
  --blue: #1d4ed8;
  --blue-soft: #eff6ff;
  --gray-soft: #f3f4f6;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05);
  --shadow-lg: 0 20px 40px -12px rgba(16, 24, 40, .25);
  font-family: -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 14px;
  color: var(--text);
}

* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); }
body { -webkit-font-smoothing: antialiased; }
a { color: var(--accent-2); text-decoration: none; }
a:hover { text-decoration: underline; }
svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex-shrink: 0; }

/* ---------- Layout ---------- */
.layout { display: flex; min-height: 100vh; }
.sidebar {
  width: 240px; flex-shrink: 0; background: var(--surface); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh; padding: 18px 12px;
}
.brand { display: block; padding: 0 6px 20px; text-decoration: none; }
.brand:hover { text-decoration: none; }
.brand-logo { display: block; width: 100%; height: auto; }
.brand-sub { color: var(--muted); font-size: 12px; margin-top: 6px; padding-left: 4px; }
.nav { display: flex; flex-direction: column; gap: 2px; }
.nav a {
  display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: 8px;
  color: var(--text-2); font-weight: 500; text-decoration: none;
}
.nav a:hover { background: var(--surface-2); color: var(--text); }
.nav a.active { background: var(--accent-soft); color: var(--accent); }
.nav .nav-sep { height: 1px; background: var(--border); margin: 10px 8px; }
.sidebar-foot { margin-top: auto; padding: 10px; color: var(--muted); font-size: 12px; line-height: 1.5; }

.content { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; gap: 14px; padding: 22px 32px 6px;
}
.topbar h1 { font-size: 22px; margin: 0; letter-spacing: -.02em; font-weight: 650; }
.page-sub { margin: 3px 0 0; color: var(--muted); }
.page-sub:empty { display: none; }
.topbar-actions { margin-left: auto; display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.menu-btn { display: none; }
.main { padding: 18px 32px 48px; max-width: 1400px; width: 100%; }

/* ---------- Composants ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 7px; height: 36px; padding: 0 14px; border-radius: 8px;
  border: 1px solid var(--border-strong); background: var(--surface); color: var(--text); font: inherit;
  font-weight: 550; cursor: pointer; white-space: nowrap; transition: background .12s, border-color .12s;
}
.btn:hover { background: var(--surface-2); }
.btn svg { width: 16px; height: 16px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-2); }
.btn.danger { color: var(--red); }
.btn.danger:hover { background: var(--red-soft); border-color: #fecaca; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--gray-soft); }
.btn.sm { height: 30px; padding: 0 10px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.icon-btn {
  display: inline-grid; place-items: center; width: 30px; height: 30px; border-radius: 7px; border: 0;
  background: transparent; color: var(--muted); cursor: pointer;
}
.icon-btn:hover { background: var(--gray-soft); color: var(--text); }
.icon-btn.danger:hover { background: var(--red-soft); color: var(--red); }
.icon-btn svg { width: 16px; height: 16px; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 18px; border-bottom: 1px solid var(--border); }
.card-head h3 { margin: 0; font-size: 14px; font-weight: 650; }
.card-head .muted { font-size: 13px; }
.card-body { padding: 18px; }
.muted { color: var(--muted); }
.grid { display: grid; gap: 16px; }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: 2fr 1fr; }
.stack { display: flex; flex-direction: column; gap: 16px; }
.row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.spacer { flex: 1; }

.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 12px; margin-bottom: 16px; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 18px; box-shadow: var(--shadow); }
.kpi-label { color: var(--muted); font-size: 12.5px; font-weight: 500; }
.kpi-value { font-size: 22px; font-weight: 650; letter-spacing: -.02em; margin-top: 6px; font-variant-numeric: tabular-nums; }
.kpi-foot { color: var(--muted); font-size: 12px; margin-top: 4px; }
.kpi.positive .kpi-value { color: var(--green); }
.kpi.negative .kpi-value { color: var(--red); }

.toolbar { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 14px; }
.toolbar .spacer { flex: 1; }
.input, .toolbar select, .toolbar input {
  height: 36px; border: 1px solid var(--border-strong); border-radius: 8px; padding: 0 11px; font: inherit;
  background: var(--surface); color: var(--text); min-width: 0;
}
.search { position: relative; }
.search input { padding-left: 34px; width: 260px; }
.search svg { position: absolute; left: 10px; top: 9px; color: var(--muted); width: 17px; height: 17px; }

.segmented { display: inline-flex; background: var(--gray-soft); border-radius: 8px; padding: 3px; }
.segmented button {
  border: 0; background: transparent; font: inherit; font-weight: 550; padding: 0 12px; height: 30px; border-radius: 6px;
  color: var(--muted); cursor: pointer;
}
.segmented button.on { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.period { display: inline-flex; align-items: center; gap: 4px; }
.period-label { display: inline-block; font-weight: 600; min-width: 190px; text-align: center; }
.period-label::first-letter { text-transform: uppercase; }

/* Tableaux */
.table-wrap { overflow-x: auto; }
table.table { width: 100%; border-collapse: collapse; }
.table th {
  text-align: left; font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .03em;
  padding: 10px 14px; border-bottom: 1px solid var(--border); background: var(--surface-2); white-space: nowrap;
}
.table td { padding: 11px 14px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table tbody tr:last-child td { border-bottom: 0; }
.table tbody tr:hover td { background: #fafbfc; }
.table tfoot td { padding: 11px 14px; border-top: 1px solid var(--border); background: var(--surface-2); font-weight: 650; }
.table .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.table .actions { text-align: right; white-space: nowrap; width: 1%; }
.table .strong { font-weight: 600; }
.table .sub { color: var(--muted); font-size: 12.5px; margin-top: 2px; }
.table tr.clickable { cursor: pointer; }
.empty { padding: 44px 20px; text-align: center; color: var(--muted); }
.empty strong { display: block; color: var(--text); font-size: 15px; margin-bottom: 4px; }

.badge {
  display: inline-flex; align-items: center; gap: 5px; height: 22px; padding: 0 9px; border-radius: 999px;
  font-size: 12px; font-weight: 600; white-space: nowrap;
}
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; opacity: .8; }
.badge.gray { background: var(--gray-soft); color: #4b5563; }
.badge.blue { background: var(--blue-soft); color: var(--blue); }
.badge.green { background: var(--green-soft); color: var(--green); }
.badge.amber { background: var(--amber-soft); color: var(--amber); }
.badge.red { background: var(--red-soft); color: var(--red); }
.badge.violet { background: #f5f3ff; color: #6d28d9; }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 16px; }
.tabs a { padding: 9px 14px; color: var(--muted); font-weight: 550; border-bottom: 2px solid transparent; margin-bottom: -1px; text-decoration: none; }
.tabs a.active { color: var(--accent); border-color: var(--accent); }

.progress { height: 6px; background: var(--gray-soft); border-radius: 99px; overflow: hidden; min-width: 70px; }
.progress > span { display: block; height: 100%; background: var(--accent); border-radius: 99px; }

.list { display: flex; flex-direction: column; }
.list-item { display: flex; align-items: center; gap: 12px; padding: 12px 18px; border-bottom: 1px solid var(--border); }
.list-item:last-child { border-bottom: 0; }
.list-item .date-pill {
  width: 46px; text-align: center; border: 1px solid var(--border); border-radius: 8px; padding: 4px 0; line-height: 1.15;
}
.date-pill b { display: block; font-size: 16px; }
.date-pill span { font-size: 11px; color: var(--muted); text-transform: uppercase; }

.info-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 16px 24px; }
.info-grid dt { color: var(--muted); font-size: 12px; margin-bottom: 3px; }
.info-grid dd { margin: 0; font-weight: 500; word-break: break-word; }

/* ---------- Agenda ---------- */
.week-grid { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; background: var(--surface); }
.week-col { border-right: 1px solid var(--border); min-height: 420px; display: flex; flex-direction: column; }
.week-col:last-child { border-right: 0; }
.week-col-head { padding: 10px 12px; border-bottom: 1px solid var(--border); background: var(--surface-2); }
.week-col-head .d { font-size: 12px; color: var(--muted); text-transform: capitalize; }
.week-col-head .n { font-size: 18px; font-weight: 650; }
.week-col.today .week-col-head { background: var(--accent-soft); }
.week-col.today .n { color: var(--accent); }
.week-col-body { padding: 8px; display: flex; flex-direction: column; gap: 6px; }
.week-col-foot { margin-top: auto; padding: 8px 12px; border-top: 1px dashed var(--border); font-size: 12px; color: var(--muted); }

.event {
  border-radius: 7px; padding: 7px 9px; font-size: 12.5px; cursor: pointer; border-left: 3px solid; line-height: 1.35;
  background: var(--blue-soft); border-color: var(--blue);
}
.event:hover { filter: brightness(.97); }
.event .t { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.event .s { color: var(--text-2); }
.event.livraison.livree { background: var(--green-soft); border-color: var(--green); }
.event.livraison.annulee { background: var(--gray-soft); border-color: #9ca3af; text-decoration: line-through; }
.event.production { background: var(--surface); border: 1px dashed var(--border-strong); border-left: 3px solid #8b5cf6; }

.month-grid { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.month-dow { padding: 8px 10px; font-size: 12px; color: var(--muted); font-weight: 600; background: var(--surface-2); border-bottom: 1px solid var(--border); text-transform: uppercase; }
.month-cell { min-height: 112px; border-right: 1px solid var(--border); border-bottom: 1px solid var(--border); padding: 6px; display: flex; flex-direction: column; gap: 4px; }
.month-cell:nth-child(7n) { border-right: 0; }
.month-cell.out { background: var(--surface-2); }
.month-cell.out .day-n { color: #c0c4cc; }
.month-cell .day-n { font-size: 12.5px; font-weight: 600; padding: 2px 4px; }
.month-cell.today .day-n { background: var(--accent); color: #fff; border-radius: 6px; display: inline-block; width: fit-content; }
.month-cell .event { padding: 3px 6px; font-size: 11.5px; }
.month-cell .more { font-size: 11.5px; color: var(--muted); padding: 0 4px; }
.legend { display: flex; gap: 16px; color: var(--muted); font-size: 12.5px; align-items: center; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }

/* ---------- Formulaires / Modale ---------- */
.overlay {
  position: fixed; inset: 0; background: rgba(17, 24, 39, .45); display: grid; place-items: center; z-index: 50;
  padding: 20px; animation: fade .12s ease-out;
}
.modal {
  background: var(--surface); border-radius: 14px; width: 100%; max-width: 560px; max-height: calc(100vh - 40px);
  display: flex; flex-direction: column; box-shadow: var(--shadow-lg); animation: pop .14s ease-out;
}
.modal.wide { max-width: 860px; }
.modal > header { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px; border-bottom: 1px solid var(--border); }
.modal > header h3 { margin: 0; font-size: 16px; font-weight: 650; }
.modal-body { padding: 20px; overflow-y: auto; }
.modal > footer { display: flex; justify-content: flex-end; gap: 8px; padding: 14px 20px; border-top: 1px solid var(--border); background: var(--surface-2); border-radius: 0 0 14px 14px; }
@keyframes fade { from { opacity: 0; } }
@keyframes pop { from { transform: translateY(6px) scale(.99); opacity: 0; } }

.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px 16px; }
.form-grid .full { grid-column: 1 / -1; }
.field label { display: block; font-size: 12.5px; font-weight: 600; color: var(--text-2); margin-bottom: 5px; }
.field label .req { color: var(--red); }
.field input, .field select, .field textarea {
  width: 100%; height: 38px; border: 1px solid var(--border-strong); border-radius: 8px; padding: 0 11px; font: inherit;
  background: var(--surface); color: var(--text);
}
.field textarea { height: auto; min-height: 76px; padding: 9px 11px; resize: vertical; }
.field input:focus, .field select:focus, .field textarea:focus, .toolbar input:focus, .toolbar select:focus {
  outline: none; border-color: var(--accent-2); box-shadow: 0 0 0 3px rgba(21, 87, 192, .15);
}
.field .hint { font-size: 12px; color: var(--muted); margin-top: 4px; }
.field.check { display: flex; align-items: center; gap: 9px; }
.field.check input { width: 16px; height: 16px; }
.field.check label { margin: 0; }
.form-section { font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); font-weight: 650; grid-column: 1 / -1; padding-top: 6px; border-top: 1px solid var(--border); margin-top: 4px; }
.form-section:first-child { border-top: 0; padding-top: 0; margin-top: 0; }
.callout { background: var(--amber-soft); color: #78350f; border: 1px solid #fde68a; border-radius: 8px; padding: 10px 12px; font-size: 13px; }
.callout.info { background: var(--blue-soft); color: #1e3a8a; border-color: #bfdbfe; }

.pick-table { border: 1px solid var(--border); border-radius: 8px; overflow: hidden; margin-top: 6px; }
.pick-table .table td, .pick-table .table th { padding: 8px 12px; }
.total-line { display: flex; justify-content: space-between; padding: 12px 14px; background: var(--surface-2); border-top: 1px solid var(--border); font-weight: 650; }

/* ---------- Toasts ---------- */
.toasts { position: fixed; right: 20px; bottom: 20px; display: flex; flex-direction: column; gap: 8px; z-index: 80; }
.toast {
  background: #111827; color: #fff; padding: 11px 16px; border-radius: 9px; box-shadow: var(--shadow-lg); font-weight: 500;
  animation: pop .16s ease-out; max-width: 380px;
}
.toast.error { background: var(--red); }

/* ---------- Facture ---------- */
.invoice-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 16px; align-items: center; }
.invoice {
  background: #fff; max-width: 820px; margin: 0 auto; padding: 48px 52px; border: 1px solid var(--border);
  border-radius: 4px; box-shadow: var(--shadow); color: #111827; font-size: 13px;
}
.inv-head { display: flex; justify-content: space-between; gap: 24px; padding-bottom: 26px; border-bottom: 2px solid var(--accent); }
.inv-logo { display: block; width: 280px; max-width: 100%; height: auto; margin-bottom: 10px; }
.inv-company .name { font-size: 22px; font-weight: 750; color: var(--accent); letter-spacing: -.02em; }
.inv-company .slogan { color: var(--muted); margin-bottom: 8px; }
.inv-company div, .inv-client div { line-height: 1.55; }
.inv-title { text-align: right; }
.inv-title h2 { margin: 0; font-size: 28px; letter-spacing: .08em; font-weight: 300; color: var(--text-2); }
.inv-title .no { font-weight: 700; font-size: 15px; margin-top: 4px; }
.inv-meta { margin-top: 12px; display: grid; grid-template-columns: auto auto; gap: 3px 14px; justify-content: end; }
.inv-meta span:nth-child(odd) { color: var(--muted); }
.inv-parties { display: flex; justify-content: space-between; gap: 24px; margin: 26px 0; }
.inv-client { flex: 1; background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; padding: 14px 18px; min-width: 0; }
.inv-label { font-size: 11px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); font-weight: 650; margin-bottom: 6px; }
.inv-client .cname { font-weight: 700; font-size: 15px; }
.invoice table { width: 100%; border-collapse: collapse; }
.invoice th { background: var(--accent); color: #fff; font-size: 11.5px; text-transform: uppercase; letter-spacing: .04em; padding: 9px 12px; text-align: left; }
.invoice td { padding: 10px 12px; border-bottom: 1px solid var(--border); }
.invoice .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.inv-totals { margin-left: auto; width: 320px; margin-top: 16px; }
.inv-totals div { display: flex; justify-content: space-between; padding: 7px 12px; }
.inv-totals .grand { background: var(--accent); color: #fff; font-weight: 700; font-size: 15px; border-radius: 6px; margin-top: 4px; }
.inv-words { margin-top: 22px; padding: 12px 16px; border: 1px dashed var(--border-strong); border-radius: 8px; }
.inv-notes { margin-top: 18px; display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
.inv-notes p { margin: 0; white-space: pre-line; line-height: 1.5; }
.inv-sign { margin-top: 36px; display: flex; justify-content: flex-end; }
.inv-sign div { width: 240px; text-align: center; border-top: 1px solid var(--border-strong); padding-top: 6px; color: var(--muted); }
.inv-foot { margin-top: 36px; padding-top: 12px; border-top: 1px solid var(--border); text-align: center; font-size: 11.5px; color: var(--muted); }
.paid-stamp {
  display: inline-block; border: 3px solid var(--green); color: var(--green); padding: 4px 14px; font-weight: 800; letter-spacing: .15em;
  transform: rotate(-6deg); border-radius: 6px; margin-top: 10px;
}

/* ---------- Connexion ---------- */
#auth-screen { position: fixed; inset: 0; z-index: 90; background: var(--bg); display: grid; place-items: center; padding: 16px; }
.auth-card { width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow-lg); padding: 30px 28px; }
.auth-card h2 { margin: 18px 0 2px; font-size: 20px; letter-spacing: -.02em; }
.auth-card > p { margin: 0 0 20px; }
.auth-logo { display: block; width: 240px; max-width: 100%; height: auto; margin: 0 auto; }
.auth-error { background: var(--red-soft); color: var(--red); border: 1px solid #fecaca; border-radius: 8px; padding: 9px 12px; font-size: 13px; }
.user-box { display: flex; align-items: center; gap: 10px; padding: 10px 0 12px; margin-bottom: 8px; border-bottom: 1px solid var(--border); }
.avatar { width: 32px; height: 32px; border-radius: 50%; background: var(--accent-soft); color: var(--accent); display: grid; place-items: center; font-weight: 700; flex-shrink: 0; }

/* Zone hors écran utilisée pour générer les PDF */
.pdf-host { position: fixed; left: 0; top: 0; z-index: -1; opacity: 0; pointer-events: none; }
.invoice.pdf { width: 696px; max-width: none; margin: 0; border: 0; box-shadow: none; border-radius: 0; padding: 0; }
.invoice.pdf .inv-head, .invoice.pdf .inv-parties { flex-direction: row; }
.invoice.pdf .inv-title { text-align: right; }
.invoice.pdf .inv-meta { justify-content: end; text-align: left; }
.invoice.pdf .inv-totals { width: 320px; }

/* ---------- Responsive ---------- */
@media (max-width: 1100px) {
  .grid-3, .grid-2 { grid-template-columns: 1fr; }
}
@media (max-width: 860px) {
  .sidebar { position: fixed; left: 0; z-index: 40; transform: translateX(-100%); transition: transform .2s; box-shadow: var(--shadow-lg); }
  .sidebar.open { transform: none; }
  .menu-btn { display: inline-grid; }
  .topbar { padding: 16px; flex-wrap: wrap; }
  .main { padding: 12px 16px 40px; }
  .form-grid { grid-template-columns: 1fr; }
  .search input { width: 100%; }
  .week-grid { grid-template-columns: 1fr; }
  .week-col { border-right: 0; border-bottom: 1px solid var(--border); min-height: 0; }
  .month-cell { min-height: 70px; }
  .invoice { padding: 24px 18px; }
  .inv-head, .inv-parties { flex-direction: column; }
  .inv-title, .inv-meta { text-align: left; justify-content: start; }
  .inv-totals { width: 100%; }
  .inv-client { min-width: 0; }
}

/* ---------- Impression ---------- */
@media print {
  @page { size: A4; margin: 12mm; }
  html, body { background: #fff; }
  .sidebar, .topbar, .invoice-actions, .toasts, #modal-root { display: none !important; }
  .main { padding: 0; max-width: none; }
  .invoice { border: 0; box-shadow: none; padding: 0; max-width: none; }
  /* La largeur A4 déclencherait la mise en page mobile : on garde celle du bureau. */
  .inv-head, .inv-parties { flex-direction: row; }
  .inv-title { text-align: right; }
  .inv-meta { justify-content: end; text-align: left; }
  .inv-totals { width: 320px; }
  .invoice th, .inv-totals .grand { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}
