/* ── Variables ───────────────────────────────────────────────────────────── */
:root {
  --bg:           #f5f4ef;
  --surface:      #ffffff;
  --border:       #e2e0d8;
  --text:         #1a1a1a;
  --text-muted:   #6b6a65;
  --primary:      #1e293b;
  --primary-h:    #334155;
  --accent:       #2563eb;
  --accent-h:     #1d4ed8;
  --danger:       #dc2626;
  --success:      #16a34a;
  --radius:       8px;
  --radius-sm:    5px;
  --shadow:       0 1px 3px rgba(0,0,0,.07), 0 1px 2px rgba(0,0,0,.05);
  --shadow-md:    0 4px 16px rgba(0,0,0,.10);
  --font:         -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
}

/* ── Reset & Base ────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html { font-size: 15px; }

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  min-height: 100vh;
}

h1, h2, h3, h4 { font-weight: 600; line-height: 1.3; }
h2 { font-size: 1rem; text-transform: uppercase; letter-spacing: .05em; color: var(--text-muted); margin-bottom: 1rem; }

a { color: var(--accent); text-decoration: none; }

/* ── Utilities ───────────────────────────────────────────────────────────── */
.hidden         { display: none !important; }
.text-muted     { color: var(--text-muted); font-size: .875rem; }
.text-right     { text-align: right; }
.no-print       { /* used in print media query */ }

/* ── Buttons ─────────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: .4rem;
  padding: .5rem 1rem;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  font-size: .875rem;
  font-family: var(--font);
  font-weight: 500;
  cursor: pointer;
  transition: background .15s, border-color .15s, color .15s;
  white-space: nowrap;
}
.btn:disabled { opacity: .5; cursor: not-allowed; }

.btn-primary {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}
.btn-primary:hover { background: var(--primary-h); border-color: var(--primary-h); }

.btn-accent {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}
.btn-accent:hover { background: var(--accent-h); border-color: var(--accent-h); }

.btn-secondary {
  background: var(--surface);
  color: var(--text);
  border-color: var(--border);
}
.btn-secondary:hover { background: var(--bg); }

.btn-ghost {
  background: transparent;
  color: var(--text-muted);
  border-color: transparent;
}
.btn-ghost:hover { background: var(--bg); color: var(--text); }

.btn-danger { background: #fef2f2; color: var(--danger); border-color: #fecaca; }
.btn-danger:hover { background: #fee2e2; }

.btn-sm   { padding: .3rem .65rem; font-size: .8rem; }
.btn-full { width: 100%; justify-content: center; padding: .65rem 1rem; }
.btn-lg   { padding: .7rem 1.5rem; font-size: 1rem; }

/* ── Alerts ──────────────────────────────────────────────────────────────── */
.alert {
  padding: .65rem 1rem;
  border-radius: var(--radius-sm);
  font-size: .875rem;
  margin-bottom: 1rem;
}
.alert-error { background: #fef2f2; color: var(--danger); border: 1px solid #fecaca; }

/* ── Form elements ───────────────────────────────────────────────────────── */
.form-group { display: flex; flex-direction: column; gap: .3rem; }

label {
  font-size: .8rem;
  font-weight: 500;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: .04em;
}

input[type="text"],
input[type="email"],
input[type="tel"],
input[type="number"],
input[type="date"],
input[type="password"],
select,
textarea {
  width: 100%;
  padding: .5rem .7rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: .9rem;
  font-family: var(--font);
  color: var(--text);
  background: var(--surface);
  transition: border-color .15s, box-shadow .15s;
  appearance: none;
}

input:focus,
select:focus,
textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(37,99,235,.12);
}

textarea { resize: vertical; min-height: 70px; }

select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%236b6a65' d='M6 8L1 3h10z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right .6rem center;
  padding-right: 2rem;
}

.form-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: .75rem;
}

/* ── LOGIN PAGE ──────────────────────────────────────────────────────────── */
.login-page {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 1.5rem;
}

.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  padding: 2.5rem 2rem;
  width: 100%;
  max-width: 380px;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

.login-header { text-align: center; }
.login-icon   { font-size: 2rem; margin-bottom: .5rem; }
.login-header h1 { font-size: 1.35rem; }

/* ── APP LAYOUT ──────────────────────────────────────────────────────────── */
#app-ui { display: flex; flex-direction: column; min-height: 100vh; }

.app-header {
  background: var(--primary);
  color: #fff;
  padding: .75rem 1.5rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: var(--shadow-md);
}

.app-header h1 {
  font-size: 1rem;
  font-weight: 600;
  color: #fff;
  text-transform: none;
  letter-spacing: 0;
}

.header-actions { display: flex; align-items: center; gap: .5rem; }

.header-actions .btn-ghost {
  color: rgba(255,255,255,.7);
}
.header-actions .btn-ghost:hover {
  background: rgba(255,255,255,.1);
  color: #fff;
}

/* ── FORM CONTAINER ──────────────────────────────────────────────────────── */
.form-container {
  max-width: 860px;
  margin: 0 auto;
  padding: 1.5rem;
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

.form-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.5rem;
  display: flex;
  flex-direction: column;
  gap: .85rem;
}

/* ── LOGO UPLOAD ─────────────────────────────────────────────────────────── */
.logo-upload-area {
  display: flex;
  align-items: center;
  gap: 1rem;
  flex-wrap: wrap;
}

.logo-preview-wrap {
  display: flex;
  align-items: center;
  gap: .75rem;
}

.logo-thumb {
  max-height: 56px;
  max-width: 160px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  object-fit: contain;
  padding: 4px;
  background: var(--bg);
}

/* ── DOC TYPE SWITCHER ───────────────────────────────────────────────────── */
.section-header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
  /* cancel the form-card gap so h2 margin-bottom still applies */
  margin-bottom: -.15rem;
}

.section-header-row h2 { margin-bottom: 0; }

.doc-type-switcher {
  display: inline-flex;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 3px;
  gap: 2px;
}

.doc-type-btn {
  padding: .28rem .85rem;
  border-radius: 3px;
  border: none;
  background: transparent;
  font-size: .8rem;
  font-family: var(--font);
  font-weight: 500;
  color: var(--text-muted);
  cursor: pointer;
  transition: background .15s, color .15s, box-shadow .15s;
}

.doc-type-btn.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}

/* ── LIGNES (items) ──────────────────────────────────────────────────────── */
.lignes-header {
  display: grid;
  grid-template-columns: 1fr 60px 100px 68px 100px 32px;
  gap: .4rem;
  padding: 0 .25rem .3rem;
  font-size: .75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--text-muted);
  border-bottom: 1px solid var(--border);
}

.ligne-row {
  display: grid;
  grid-template-columns: 1fr 60px 100px 68px 100px 32px;
  gap: .4rem;
  align-items: center;
  padding: .35rem .25rem;
  border-bottom: 1px solid var(--border);
}

.ligne-row:last-child { border-bottom: none; }

.ligne-total {
  font-size: .875rem;
  font-weight: 500;
  text-align: right;
  color: var(--text);
  padding-right: .2rem;
}

.btn-remove {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-muted);
  font-size: 1.1rem;
  cursor: pointer;
  transition: background .15s, color .15s;
  flex-shrink: 0;
}
.btn-remove:hover { background: #fef2f2; color: var(--danger); }

#lignes-container { min-height: 40px; }

/* ── TOTAUX (form) ───────────────────────────────────────────────────────── */
.totaux-form {
  display: flex;
  justify-content: flex-end;
  padding-top: .5rem;
}

.totaux-inner {
  width: 280px;
  display: flex;
  flex-direction: column;
  gap: .3rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: .75rem 1rem;
  background: var(--bg);
}

.tva-line {
  display: flex;
  justify-content: space-between;
  font-size: .8rem;
  color: var(--text-muted);
}

.total-line {
  display: flex;
  justify-content: space-between;
  font-size: .9rem;
  padding-top: .3rem;
  border-top: 1px solid var(--border);
}

.total-line-ttc {
  font-size: 1rem;
  border-top: 2px solid var(--primary);
  padding-top: .4rem;
  margin-top: .2rem;
}

/* ── FORM ACTIONS ────────────────────────────────────────────────────────── */
.form-actions {
  display: flex;
  justify-content: flex-end;
  padding-bottom: 1.5rem;
}

/* ── PREVIEW TOOLBAR ─────────────────────────────────────────────────────── */
#preview-toolbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 200;
  background: var(--primary);
  padding: .6rem 1.5rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-shadow: var(--shadow-md);
}

#preview-toolbar .btn-ghost {
  color: rgba(255,255,255,.75);
}
#preview-toolbar .btn-ghost:hover {
  background: rgba(255,255,255,.1);
  color: #fff;
}

#preview-toolbar-title {
  font-size: .875rem;
  color: rgba(255,255,255,.6);
}

/* ── DEVIS DOCUMENT (aperçu écran) ───────────────────────────────────────── */
#devis-document {
  padding: 3rem 1.5rem 4rem;
  /* top padding to clear the preview toolbar */
  padding-top: 80px;
}

.devis-page {
  background: #fff;
  max-width: 210mm;
  margin: 0 auto;
  padding: 20mm 18mm;
  box-shadow: var(--shadow-md);
  border-radius: var(--radius);
  font-size: 10pt;
  font-family: var(--font);
  color: #1a1a1a;
  line-height: 1.5;
}

/* Header (logo + emetteur) */
.devis-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 1rem;
  margin-bottom: 1.5rem;
  padding-bottom: 1.5rem;
  border-bottom: 2px solid #1e293b;
}

.devis-logo-section { flex: 0 0 auto; }

.devis-logo {
  max-height: 70px;
  max-width: 180px;
  object-fit: contain;
}

.devis-emetteur {
  text-align: right;
  font-size: 9pt;
  line-height: 1.6;
}

.devis-emetteur strong { font-size: 11pt; display: block; margin-bottom: .2rem; }

/* Titre DEVIS */
.devis-title-section {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  margin-bottom: 1.5rem;
}

.devis-title-section h1 {
  font-size: 28pt;
  font-weight: 700;
  color: #1e293b;
  letter-spacing: .05em;
}

.devis-meta {
  text-align: right;
  font-size: 9pt;
  color: #555;
  line-height: 1.8;
}

/* Parties */
.devis-parties { margin-bottom: 1.5rem; }

.devis-client {
  display: inline-block;
  font-size: 9.5pt;
  line-height: 1.7;
  border: 1px solid #d1d5db;
  border-radius: 5px;
  padding: .6rem 1rem;
  min-width: 200px;
}

.partie-label {
  font-size: 7.5pt;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: #888;
  margin-bottom: .25rem;
}

/* Objet */
.devis-objet {
  background: #f8f9fa;
  border-left: 3px solid #1e293b;
  padding: .5rem .85rem;
  margin-bottom: 1.25rem;
  font-size: 9.5pt;
}

/* Table des prestations */
.devis-table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 1rem;
  font-size: 9pt;
}

.devis-table th {
  background: #1e293b;
  color: #fff;
  padding: .5rem .6rem;
  text-align: left;
  font-weight: 600;
  font-size: 8pt;
  letter-spacing: .04em;
}

.devis-table th.text-right { text-align: right; }

.devis-table td {
  padding: .45rem .6rem;
  border-bottom: 1px solid #e5e7eb;
  vertical-align: top;
}

.devis-table tbody tr:nth-child(even) td { background: #f9fafb; }

/* Totaux */
.devis-totaux {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  margin-bottom: 1.5rem;
}

.totaux-table {
  width: 240px;
  border-collapse: collapse;
  font-size: 9.5pt;
}

.totaux-table td {
  padding: .3rem .5rem;
  border-bottom: 1px solid #e5e7eb;
}

.totaux-table tr:last-child td { border-bottom: none; }

.total-ttc-row td {
  background: #1e293b;
  color: #fff;
  font-size: 10.5pt;
  padding: .5rem .6rem;
  border-radius: 3px;
}

.acompte-info {
  margin-top: .5rem;
  font-size: 8.5pt;
  color: #555;
  font-style: italic;
}

/* Mention TVA 0% */
.mention-tva {
  font-size: 8pt;
  color: #777;
  font-style: italic;
  margin-bottom: 1rem;
}

/* Conditions & Notes */
.devis-section {
  margin-bottom: 1rem;
}

.devis-section h4 {
  font-size: 8.5pt;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: #555;
  margin-bottom: .3rem;
  padding-bottom: .2rem;
  border-bottom: 1px solid #e5e7eb;
}

.devis-section p {
  font-size: 8.5pt;
  white-space: pre-wrap;
  color: #444;
}

/* Signatures */
.devis-signatures {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 2rem;
  margin-top: 2rem;
  padding-top: 1.5rem;
  border-top: 1px solid #e5e7eb;
}

.devis-signatures--single { grid-template-columns: 1fr; max-width: 280px; }

.signature-bloc { font-size: 8.5pt; color: #555; }
.signature-bloc p { margin-bottom: .3rem; font-weight: 600; }
.signature-mention { font-weight: 400 !important; font-style: italic; font-size: 8pt; color: #888; }
.signature-space { margin-top: 3rem; border-bottom: 1px dashed #ccc; }

/* ── RESPONSIVE (small screens) ──────────────────────────────────────────── */
@media (max-width: 680px) {
  .lignes-header { display: none; }

  .ligne-row {
    grid-template-columns: 1fr 1fr;
    gap: .5rem;
    padding: .75rem .25rem;
  }

  .ligne-row input:first-child,
  .ligne-row input[data-field="description"] {
    grid-column: 1 / -1;
  }

  .devis-header { flex-direction: column; }
  .devis-emetteur { text-align: left; }
  .devis-title-section { flex-direction: column; gap: .5rem; }
  .devis-meta { text-align: left; }
  .devis-signatures { grid-template-columns: 1fr; }
}

/* ── PRINT ───────────────────────────────────────────────────────────────── */
@media print {
  #app-ui,
  #preview-toolbar { display: none !important; }

  #devis-document {
    display: block !important;
    padding: 0 !important;
  }

  body { background: #fff !important; }

  .devis-page {
    max-width: none;
    box-shadow: none;
    border-radius: 0;
    padding: 10mm 12mm;
    margin: 0;
  }

  .devis-table thead { display: table-header-group; }
  .devis-table tbody tr { page-break-inside: avoid; }

  .devis-totaux { page-break-inside: avoid; }
  .devis-signatures { page-break-inside: avoid; }

  @page {
    size: A4;
    margin: 0;
  }
}
