:root {
  --bg: #ebf0ef;
  --paper: #ffffff;
  --paper-soft: #f6faf9;
  --ink: #0f172a;
  --muted: #64748b;
  --line: #d7e0dc;
  --line-strong: #b8c7c1;
  --accent: #176b63;
  --accent-strong: #0f5b55;
  --brand-navy: #1f2d44;
  --brand-navy-strong: #18263d;
  --brand-ice: #e8efff;
  --warn: #b45309;
  --danger: #b91c1c;
  --focus: #10b981;
  --radius: 14px;
  --radius-lg: 18px;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: 'Avenir Next', 'Helvetica Neue', sans-serif;
  color: var(--ink);
  background:
    radial-gradient(circle at 8% 10%, rgba(23, 107, 99, 0.2), transparent 26%),
    radial-gradient(circle at 86% 88%, rgba(31, 45, 68, 0.18), transparent 34%),
    var(--bg);
}

.app {
  max-width: 1500px;
  margin: 0 auto;
  padding: 28px 24px 32px;
}

.card {
  max-width: 420px;
  margin: 80px auto;
  padding: 26px;
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  background: var(--paper);
  box-shadow: 0 18px 42px rgba(15, 23, 42, 0.12);
}

.hidden {
  display: none !important;
}

label {
  display: block;
  margin-bottom: 10px;
  font-size: 0.92rem;
}

input,
select,
button {
  width: 100%;
  padding: 10px 12px;
  border-radius: 11px;
  border: 1px solid var(--line);
  font: inherit;
}

input,
select {
  background: #fff;
  transition: border-color 140ms ease, box-shadow 140ms ease;
}

button {
  cursor: pointer;
  border: none;
  color: #fff;
  background: linear-gradient(180deg, var(--accent), var(--accent-strong));
  font-weight: 600;
  letter-spacing: 0.1px;
  transition: transform 120ms ease, box-shadow 120ms ease, background-color 120ms ease, opacity 120ms ease;
}

button:hover {
  background: linear-gradient(180deg, var(--accent-strong), #0b4f4a);
  transform: translateY(-1px);
  box-shadow: 0 8px 20px rgba(15, 91, 85, 0.24);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
  transform: none;
}

button.ghost {
  width: auto;
  color: #273449;
  border: 1px solid #cdd8d4;
  background: #fff;
}

button.ghost:hover {
  background: #f4f7f6;
  box-shadow: none;
}

#pdfBtn {
  width: auto;
  white-space: nowrap;
}

#zonesPreviewBtn {
  width: auto;
  white-space: nowrap;
}

#documensoSendBtn {
  width: auto;
  white-space: nowrap;
}

input:focus-visible,
select:focus-visible,
button:focus-visible,
.wysiwyg:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 1px;
  box-shadow: 0 0 0 4px rgba(16, 185, 129, 0.14);
}

button.mini {
  padding: 6px 10px;
  border-radius: 8px;
  font-size: 0.86rem;
}

button.danger {
  background: #b91c1c;
}

button.danger:hover {
  background: #991b1b;
}

#editTemplateBtn {
  width: 100%;
  margin-top: 8px;
}

.error {
  color: var(--danger);
  min-height: 1.2em;
}

.topbar {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.topbar h2 {
  margin: 0;
  letter-spacing: 0.2px;
  font-size: 1.7rem;
}

.topbar p {
  margin: 4px 0 0;
  color: #4b5563;
}

.topbar-actions {
  display: inline-flex;
  align-items: center;
  gap: 10px;
}

.busy-state {
  display: inline-flex;
  align-items: center;
  min-height: 36px;
  padding: 0 12px;
  border-radius: 999px;
  font-size: 0.83rem;
  color: #0f172a;
  border: 1px solid #bae6fd;
  background: linear-gradient(90deg, #eff6ff, #dbeafe, #eff6ff);
  background-size: 200% 100%;
  animation: busyShift 1.2s linear infinite;
}

@keyframes busyShift {
  0% { background-position: 0% 0; }
  100% { background-position: 200% 0; }
}

.view-tabs {
  display: flex;
  gap: 10px;
  margin-top: 18px;
}

.tab-btn {
  width: auto;
  min-width: 170px;
  background: #f5f8f8;
  color: #22314a;
  border: 1px solid #cdd8d4;
  font-weight: 600;
}

.tab-btn:hover {
  background: #e7efed;
}

.tab-btn.active {
  color: #fff;
  border-color: transparent;
  background: linear-gradient(180deg, var(--brand-navy), var(--brand-navy-strong));
}

.kpi-strip {
  margin-top: 16px;
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
}

.kpi-card {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px 12px;
  background: #ffffffde;
  backdrop-filter: blur(4px);
  display: flex;
  flex-direction: column;
  gap: 6px;
  box-shadow: 0 8px 16px rgba(15, 23, 42, 0.06);
}

.kpi-card span {
  font-size: 0.78rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.35px;
}

.kpi-card strong {
  font-size: 1.25rem;
  line-height: 1;
}

.flash {
  position: fixed;
  right: 20px;
  bottom: 20px;
  z-index: 40;
  margin: 0;
  max-width: min(560px, calc(100vw - 32px));
  padding: 11px 14px;
  border-radius: 12px;
  border: 1px solid var(--line);
  font-size: 0.9rem;
  line-height: 1.4;
  background: #fff;
  box-shadow: 0 14px 34px rgba(15, 23, 42, 0.2);
  animation: flashIn 180ms ease-out;
}

@keyframes flashIn {
  from {
    transform: translateY(8px);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}

.flash--ok {
  color: #14532d;
  border-color: #86efac;
  background: #f0fdf4;
}

.flash--warn {
  color: #92400e;
  border-color: #fcd34d;
  background: #fffbeb;
}

.flash--error {
  color: #991b1b;
  border-color: #fca5a5;
  background: #fef2f2;
}

.flash--info {
  color: #1e3a8a;
  border-color: #93c5fd;
  background: #eff6ff;
}

.layout {
  display: grid;
  grid-template-columns: 340px 1fr;
  gap: 16px;
  margin-top: 18px;
}

.sidebar,
.editor,
.history-card {
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  background: var(--paper);
  padding: 16px;
  box-shadow: 0 14px 30px rgba(15, 23, 42, 0.08);
}

.form-row {
  margin-bottom: 10px;
}

.hint {
  margin: 0 0 10px;
  color: var(--muted);
  font-size: 0.9rem;
}

#contractsList {
  list-style: none;
  margin: 8px 0 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 440px;
  overflow: auto;
}

#contractsSearch {
  margin: 4px 0;
}

.contract-item-btn {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 6px;
  text-align: left;
  border: 1px solid #dde7e3;
  background: #fcfefd;
  color: #111827;
  padding-right: 40px;
  transition: border-color 120ms ease;
}

.contract-item-row {
  position: relative;
}

.contract-item-btn:hover {
  background: #fafaf9;
  transform: none;
}

.contract-item-btn.is-selected {
  border-color: #0ea5a0;
  box-shadow: inset 0 0 0 1px #0ea5a0, 0 8px 18px rgba(14, 165, 160, 0.18);
}

.contract-item-title {
  font-weight: 600;
}

.contract-item-meta {
  color: var(--muted);
  font-size: 0.86rem;
}

.empty-list {
  border: 1px dashed #d6d3d1;
  border-radius: 10px;
  padding: 12px;
  color: var(--muted);
  background: #fafaf9;
}

.editor-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 10px;
  align-items: center;
  position: sticky;
  top: 10px;
  z-index: 2;
  padding: 8px;
  border: 1px solid #dbe6e1;
  border-radius: var(--radius);
  background: rgba(255, 255, 255, 0.97);
  backdrop-filter: blur(4px);
}

#contractTitle {
  flex: 1 1 280px;
  min-width: 220px;
}

#contractClientName {
  flex: 0 1 210px;
  min-width: 170px;
  max-width: 300px;
}

.mode-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0 10px;
  min-height: 40px;
  border-radius: 10px;
  border: 1px solid #d5e0dc;
  background: #f2f7f5;
  color: #375067;
  font-size: 0.85rem;
  white-space: nowrap;
}

.status-pill {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: auto;
  padding: 4px 10px;
  border-radius: 999px;
  border: 1px solid transparent;
  font-size: 0.8rem;
  font-weight: 600;
  line-height: 1.2;
}

.status-pill--draft {
  color: #1d4ed8;
  border-color: #bfd5ff;
  background: #edf3ff;
}

.status-pill--validated {
  color: #065f46;
  border-color: #99e6bf;
  background: #ebfaf1;
}

.status-pill--sent {
  color: #991b1b;
  border-color: #f7adad;
  background: #fff1f1;
}

.format-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 12px 0 10px;
}

.format-toolbar button {
  width: auto;
  color: #111827;
  border: 1px solid #d3ddd8;
  background: #fff;
  padding: 8px 10px;
  font-weight: 500;
}

.format-toolbar button:hover {
  background: #f3f4f6;
}

#textColorPicker {
  width: 44px;
  min-width: 44px;
  padding: 2px;
  border-radius: 8px;
}

#highlightBtn {
  width: auto;
  color: #111827;
  border: 1px solid #e6d585;
  background: #fff7cc;
}

.wysiwyg {
  width: min(210mm, 100%);
  min-height: 780px;
  border: 1px solid #c4d3cd;
  border-radius: 16px;
  padding: 0;
  background: #fff;
  outline: none;
  line-height: 1.52;
  box-shadow: 0 20px 42px rgba(15, 23, 42, 0.14), inset 0 0 0 1px rgba(255, 255, 255, 0.35);
}

.wysiwyg.readonly {
  background: #fafaf9;
}

.wysiwyg:empty::before {
  content: 'Selectionne ou cree un contrat...';
  color: var(--muted);
}

.wysiwyg h1,
.wysiwyg h2,
.wysiwyg h3 {
  margin: 0.6em 0 0.35em;
}

.wysiwyg p {
  margin: 0 0 0.7em;
}

.wysiwyg ul,
.wysiwyg ol {
  margin: 0.3em 0 0.8em 1.3em;
}

.wysiwyg img {
  max-width: 100%;
  height: auto;
}

.history-card {
  margin-top: 16px;
}

.editor-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-top: 2px;
  color: var(--muted);
  font-size: 0.84rem;
}

.editor-meta-right {
  display: inline-flex;
  align-items: center;
  gap: 10px;
}

#pdfLink {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 28px;
  padding: 0 10px;
  border-radius: 999px;
  border: 1px solid #7edca8;
  background: #eafaf0;
  color: #125d34;
  font-size: 0.78rem;
  font-weight: 600;
  text-decoration: none;
}

.dirty-badge {
  display: inline-flex;
  align-items: center;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 0.78rem;
  color: #975a0f;
  border: 1px solid #f0cc57;
  background: #fff8df;
}

.save-attention {
  box-shadow: 0 0 0 3px rgba(16, 185, 129, 0.22);
}

.editor-canvas {
  border-radius: 16px;
  border: 1px dashed #c9d7d2;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.88), rgba(255, 255, 255, 1)),
    repeating-linear-gradient(
      -45deg,
      rgba(23, 107, 99, 0.03),
      rgba(23, 107, 99, 0.03) 14px,
      rgba(255, 255, 255, 0.03) 14px,
      rgba(255, 255, 255, 0.03) 28px
    );
  padding: 20px 16px;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  overflow: auto;
}

.history-table {
  width: 100%;
  border-collapse: collapse;
  border: 1px solid #dbe6e1;
  border-radius: 14px;
  overflow: hidden;
}

.history-table th,
.history-table td {
  padding: 10px 8px;
  border-bottom: 1px solid #e7e5e4;
  text-align: left;
  vertical-align: middle;
}

.history-table th {
  font-size: 0.88rem;
  color: #6b7280;
  background: #fafaf9;
  position: sticky;
  top: 0;
  z-index: 1;
}

.history-table td {
  font-size: 0.92rem;
}

.history-table tbody tr:nth-child(even) {
  background: #fcfcfb;
}

.history-table tbody tr:hover {
  background: #f1f8f5;
}

.history-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  align-items: center;
}

.history-toolbar {
  margin: 8px 0 12px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.history-toolbar input {
  flex: 1 1 280px;
  min-width: 240px;
}

.history-toolbar select {
  width: auto;
  min-width: 180px;
}

.contract-item-delete {
  position: absolute;
  top: 9px;
  right: 9px;
  width: 24px;
  min-width: 24px;
  height: 24px;
  padding: 0;
  border: none;
  background: transparent;
  color: #dc2626;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.95rem;
  line-height: 1;
  border-radius: 6px;
}

.contract-item-delete:hover {
  background: rgba(220, 38, 38, 0.12);
  color: #b91c1c;
  transform: none;
}

.app-busy {
  cursor: progress;
}

body.modal-open {
  overflow: hidden;
}

.editor {
  overflow: hidden;
}

.wysiwyg,
.wysiwyg * {
  box-sizing: border-box;
}

.wysiwyg * {
  max-width: 100%;
}

.wysiwyg table {
  width: 100% !important;
  table-layout: fixed;
}

.wysiwyg [style*='width'] {
  max-width: 100% !important;
}

.wysiwyg {
  overflow-wrap: anywhere;
}

.zones-modal {
  position: fixed;
  inset: 0;
  z-index: 20;
  padding: 22px;
  background: rgba(15, 23, 42, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
}

.zones-modal-card {
  width: min(1240px, 96vw);
  max-height: calc(100vh - 44px);
  border-radius: 16px;
  border: 1px solid #cbd5e1;
  background: #fff;
  box-shadow: 0 24px 48px rgba(15, 23, 42, 0.24);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.zones-modal-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
  padding: 12px 14px;
  border-bottom: 1px solid #e2e8f0;
}

.zones-modal-header h3 {
  margin: 0;
  font-size: 1rem;
}

.zones-modal-header .hint {
  margin: 4px 0 0;
  font-size: 0.83rem;
}

.zones-modal-pages {
  flex: 1 1 auto;
  overflow: auto;
  padding: 14px;
  background: #f8fafc;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.zones-page {
  border: 1px solid #e2e8f0;
  border-radius: 10px;
  background: #fff;
  padding: 10px;
}

.zones-page-title {
  margin: 0 0 8px;
  font-size: 0.85rem;
  color: #475569;
  font-weight: 600;
}

.zones-page-canvas-wrap {
  position: relative;
  width: fit-content;
  max-width: 100%;
}

.zones-page canvas {
  display: block;
  max-width: 100%;
  border: 1px solid #dbe4ee;
  background: #fff;
}

.zones-overlay-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.zone-overlay {
  position: absolute;
  border: 2px solid #2563eb;
  border-radius: 8px;
  background: rgba(37, 99, 235, 0.12);
}

.zone-overlay.zone-overlay--signature {
  border-color: #16a34a;
  background: rgba(22, 163, 74, 0.12);
}

.zone-overlay-label {
  position: absolute;
  top: -22px;
  left: -2px;
  padding: 1px 8px;
  border-radius: 999px;
  background: #2563eb;
  color: #fff;
  font-size: 0.72rem;
  line-height: 1.45;
  font-weight: 600;
  white-space: nowrap;
}

.zone-overlay.zone-overlay--signature .zone-overlay-label {
  background: #16a34a;
}

.zones-empty {
  border: 1px dashed #cbd5e1;
  border-radius: 10px;
  background: #fff;
  padding: 18px;
  color: #64748b;
  font-size: 0.9rem;
}

.documenso-modal {
  position: fixed;
  inset: 0;
  z-index: 25;
  padding: 16px;
  background: rgba(15, 23, 42, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
}

.documenso-modal-card {
  width: min(560px, 96vw);
  border: 1px solid #cbd5e1;
  border-radius: 16px;
  background: #fff;
  box-shadow: 0 24px 48px rgba(15, 23, 42, 0.24);
  overflow: hidden;
}

#saveBtn {
  background: linear-gradient(180deg, var(--brand-navy), var(--brand-navy-strong));
}

#saveBtn:hover {
  background: linear-gradient(180deg, #192640, #132033);
  box-shadow: 0 8px 20px rgba(31, 45, 68, 0.28);
}

#newContractBtn {
  background: linear-gradient(180deg, var(--accent), var(--accent-strong));
}

#pdfBtn {
  border-color: #f6d084;
  background: #fff7e6;
  color: #8b5a04;
}

#pdfBtn:hover {
  background: #ffeec9;
  box-shadow: none;
}

#pdfBtn.is-ready {
  border-color: #9fd9b9;
  background: #eafaf0;
  color: #155e3a;
}

#zonesPreviewBtn {
  border-color: #c8d8ff;
  background: #eef4ff;
  color: #1d4ed8;
}

#zonesPreviewBtn:hover {
  background: #e2ecff;
  box-shadow: none;
}

#documensoSendBtn {
  border-color: #9fd9b9;
  background: #eafaf0;
  color: #155e3a;
}

#documensoSendBtn:hover {
  background: #dcf4e7;
  box-shadow: none;
}

.documenso-modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 12px 14px;
  border-bottom: 1px solid #e2e8f0;
}

.documenso-modal-header h3 {
  margin: 0;
  font-size: 1rem;
}

.documenso-form {
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.documenso-form input[type='email'],
.documenso-form input[type='text'],
.documenso-form input[type='number'] {
  width: 100%;
}

.documenso-manual-note {
  margin: 2px 0 0;
  padding: 10px 12px;
  border: 1px solid #c8d8ff;
  border-radius: 12px;
  background: #eef4ff;
  color: #1e3a8a;
  font-size: 0.92rem;
}

.documenso-result {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 4px;
  padding: 12px;
  border: 1px solid #b7e3c8;
  border-radius: 14px;
  background: #f3fbf6;
}

.documenso-result-summary {
  margin: 0;
  color: #14532d;
  font-size: 0.92rem;
}

.documenso-result-row {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.documenso-result-row label {
  color: #334155;
  font-size: 0.9rem;
  font-weight: 600;
}

.documenso-result-field {
  display: flex;
  gap: 8px;
  align-items: center;
}

.documenso-result-field input {
  flex: 1 1 auto;
  min-width: 0;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', monospace;
  font-size: 0.82rem;
}

.documenso-result-field button {
  width: auto;
  flex: 0 0 auto;
}

.documenso-actions {
  margin-top: 8px;
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.documenso-actions button {
  width: auto;
}

@media (max-width: 1120px) {
  .kpi-strip {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .layout {
    grid-template-columns: 1fr;
  }

  .mode-badge,
  .status-pill {
    min-height: 40px;
  }

  .editor-meta {
    flex-direction: column;
    align-items: flex-start;
  }

  .editor-meta-right {
    width: 100%;
    justify-content: space-between;
  }

  .zones-modal {
    padding: 10px;
  }

  .zones-modal-card {
    width: 100%;
    max-height: calc(100vh - 20px);
  }

  .documenso-modal {
    padding: 10px;
  }

  .documenso-modal-card {
    width: 100%;
  }

  .documenso-result-field {
    flex-direction: column;
    align-items: stretch;
  }
}

@media (max-width: 780px) {
  .app {
    padding: 14px;
  }

  .topbar {
    flex-direction: column;
  }

  .view-tabs {
    flex-direction: column;
  }

  .tab-btn {
    width: 100%;
  }

  .history-table {
    display: block;
    overflow-x: auto;
    white-space: nowrap;
  }

  .kpi-strip {
    grid-template-columns: 1fr;
  }

  .editor-canvas {
    padding: 10px;
  }

  #contractTitle,
  #contractClientName {
    min-width: 100%;
  }

  .history-toolbar input,
  .history-toolbar select {
    min-width: 100%;
    width: 100%;
  }

  .wysiwyg {
    padding: 20mm 12mm;
  }
}
