:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #dde1e7;
  --text: #1c2330;
  --muted: #5d6879;
  --primary: #1d3a5f;
  --primary-2: #2c5282;
  --accent: #c8912e;
  --danger: #b42318;
  --danger-bg: #fdecea;
  --warn-bg: #fff6e0;
  --warn: #8a5a00;
  --ok: #1e7a46;
  --ok-bg: #e7f6ed;
  --radius: 8px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.08);
  font-size: 15px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  line-height: 1.45;
}

a { color: var(--primary-2); }
h1 { font-size: 1.5rem; margin: 0 0 0.25rem; }
h2 { font-size: 1.15rem; margin: 0 0 1rem; }
h3 { font-size: 1rem; margin: 1rem 0 0.5rem; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.right { text-align: right; }
.nowrap { white-space: nowrap; }
.hidden { display: none !important; }

/* ---------- En-tête ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 1.5rem;
  padding: 0 1.25rem;
  height: 56px;
  background: var(--primary);
  color: #fff;
  position: sticky;
  top: 0;
  z-index: 20;
}
.topbar .brand { font-weight: 700; letter-spacing: 0.02em; white-space: nowrap; }
.topbar .brand span { color: var(--accent); }
.topbar nav { display: flex; gap: 0.25rem; flex: 1; overflow-x: auto; }
.topbar nav a {
  color: #d7e0ec;
  text-decoration: none;
  padding: 0.4rem 0.7rem;
  border-radius: 6px;
  white-space: nowrap;
}
.topbar nav a:hover, .topbar nav a.active { background: rgba(255, 255, 255, 0.12); color: #fff; }
.topbar .user { display: flex; align-items: center; gap: 0.75rem; font-size: 0.9rem; white-space: nowrap; }
.topbar .user a { color: #d7e0ec; }

main { max-width: 1180px; margin: 0 auto; padding: 1.5rem 1.25rem 6rem; }
.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 1rem; margin-bottom: 1.25rem; flex-wrap: wrap; }

/* ---------- Cartes ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.25rem;
  margin-bottom: 1.25rem;
}
.card > h2:first-child { display: flex; align-items: center; gap: 0.5rem; }

/* ---------- Formulaires ---------- */
.grid { display: grid; gap: 0.9rem 1rem; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); }
.grid.two { grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); }
.full { grid-column: 1 / -1; }
label.field { display: flex; flex-direction: column; gap: 0.3rem; font-size: 0.85rem; font-weight: 600; color: var(--muted); }
input[type='text'], input[type='email'], input[type='tel'], input[type='url'], input[type='number'],
input[type='password'], input[type='search'], input[type='date'], select, textarea {
  font: inherit;
  color: var(--text);
  padding: 0.5rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #fff;
  width: 100%;
  min-width: 0;
}
textarea { min-height: 80px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary-2); outline-offset: -1px; border-color: transparent; }
input.invalid { border-color: var(--danger); background: var(--danger-bg); }
.check { display: inline-flex; align-items: center; gap: 0.4rem; font-size: 0.92rem; cursor: pointer; }
.checks { display: flex; flex-wrap: wrap; gap: 0.5rem 1.25rem; }

/* ---------- Boutons ---------- */
.btn {
  font: inherit;
  font-weight: 600;
  font-size: 0.9rem;
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.5rem 0.9rem;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: #fff;
  color: var(--text);
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-2); }
.btn.accent { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.danger { color: var(--danger); border-color: #f1b8b2; }
.btn.danger:hover { background: var(--danger-bg); }
.btn.small { padding: 0.25rem 0.55rem; font-size: 0.8rem; }
.btn.icon { padding: 0.3rem 0.5rem; }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.actions { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; }

/* ---------- Tableaux ---------- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 0.55rem 0.6rem; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
th { font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); background: var(--surface-2); }
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: #f7f9fc; }
tr.inactive td { opacity: 0.55; }
td.num, th.num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }

/* ---------- Badges, alertes ---------- */
.badge { display: inline-block; padding: 0.1rem 0.5rem; border-radius: 999px; font-size: 0.75rem; font-weight: 700; background: var(--surface-2); color: var(--muted); }
.badge.BROUILLON { background: #eef1f5; }
.badge.EMIS { background: #e3edfa; color: var(--primary-2); }
.badge.SIGNE { background: var(--ok-bg); color: var(--ok); }
.badge.REFUSE { background: var(--danger-bg); color: var(--danger); }
.badge.option { background: var(--warn-bg); color: var(--warn); }
.alert { padding: 0.75rem 1rem; border-radius: 6px; margin: 0.75rem 0; font-size: 0.92rem; }
.alert.warn { background: var(--warn-bg); color: var(--warn); border: 1px solid #f3d58a; }
.alert.danger { background: var(--danger-bg); color: var(--danger); border: 1px solid #f1b8b2; }
.alert.ok { background: var(--ok-bg); color: var(--ok); }

/* ---------- Lignes répétables ---------- */
.rows { display: flex; flex-direction: column; gap: 0.6rem; }
.row-item {
  display: grid;
  gap: 0.6rem;
  align-items: end;
  padding: 0.75rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface-2);
}
.row-item.offering { grid-template-columns: 1.2fr 2fr 0.7fr 0.7fr auto; }
.row-item.commune { grid-template-columns: 1fr auto auto; align-items: center; }
.row-item.concurrent { grid-template-columns: 1fr 1.5fr auto; }
@media (max-width: 760px) {
  .row-item.offering, .row-item.concurrent { grid-template-columns: 1fr; }
}

/* ---------- Modules ---------- */
.module-group { margin-bottom: 1.1rem; }
.module-group h3 { margin-top: 0; font-size: 0.9rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--primary); }
.module {
  display: grid;
  grid-template-columns: minmax(220px, 1.2fr) 2fr auto;
  gap: 0.6rem;
  align-items: center;
  padding: 0.35rem 0.5rem;
  border-radius: 6px;
}
.module.on { background: #eef4fb; }
.module .price { font-size: 0.8rem; color: var(--muted); }
.module input[type='text'] { padding: 0.3rem 0.5rem; font-size: 0.88rem; }
@media (max-width: 760px) { .module { grid-template-columns: 1fr; } }

/* ---------- Fiche : navigation ---------- */
.fiche-layout { display: grid; grid-template-columns: 200px 1fr; gap: 1.25rem; align-items: start; }
.fiche-nav { position: sticky; top: 72px; display: flex; flex-direction: column; gap: 0.1rem; font-size: 0.9rem; }
.fiche-nav a { padding: 0.35rem 0.6rem; border-radius: 6px; text-decoration: none; color: var(--muted); }
.fiche-nav a:hover { background: var(--surface); color: var(--text); }
@media (max-width: 900px) {
  .fiche-layout { grid-template-columns: 1fr; }
  .fiche-nav { display: none; }
}

.savebar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 0.7rem 1.25rem;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 0.75rem;
  z-index: 15;
  box-shadow: 0 -2px 8px rgba(16, 24, 40, 0.06);
}
.savebar .status { margin-right: auto; font-size: 0.88rem; color: var(--muted); }
.savebar .status.dirty { color: var(--warn); font-weight: 600; }

/* ---------- Références visuelles ---------- */
.reference { border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem; background: var(--surface-2); }
.reference-head { display: flex; justify-content: space-between; align-items: center; gap: 0.5rem; margin-bottom: 0.75rem; }
.level { display: inline-flex; gap: 0.25rem; }
.level button {
  width: 30px; height: 30px; border-radius: 50%;
  border: 1px solid var(--border); background: #fff; cursor: pointer; font-weight: 700; color: var(--muted);
}
.level button.on { background: var(--accent); color: #fff; border-color: var(--accent); }
.shots { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 0.75rem; margin-top: 0.75rem; }
.shot { background: #fff; border: 1px solid var(--border); border-radius: 6px; overflow: hidden; display: flex; flex-direction: column; }
.shot img { width: 100%; height: 110px; object-fit: cover; object-position: top; background: var(--surface-2); display: block; }
.shot .shot-body { padding: 0.4rem; display: flex; gap: 0.3rem; }
.shot input { font-size: 0.8rem; padding: 0.25rem 0.4rem; }
.dropzone {
  border: 2px dashed var(--border); border-radius: 6px; padding: 0.9rem; text-align: center;
  color: var(--muted); font-size: 0.88rem; cursor: pointer; background: #fff;
}
.dropzone.over { border-color: var(--primary-2); background: #eef4fb; }
.swatches { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; }
.swatch { display: inline-flex; align-items: center; gap: 0.35rem; background: #fff; border: 1px solid var(--border); border-radius: 999px; padding: 0.15rem 0.5rem 0.15rem 0.2rem; font-family: monospace; font-size: 0.85rem; }
.swatch i { width: 20px; height: 20px; border-radius: 50%; display: inline-block; border: 1px solid rgba(0, 0, 0, 0.15); }

/* ---------- Devis ---------- */
.quote-lines td { padding: 0.35rem 0.35rem; }
.quote-lines input, .quote-lines select { padding: 0.35rem 0.4rem; font-size: 0.88rem; }
.quote-lines .qty { width: 70px; }
.quote-lines .money { width: 105px; text-align: right; }
.quote-lines .disc { display: flex; gap: 0.25rem; min-width: 150px; }
.quote-lines .disc select { width: 64px; }
.quote-lines tr.section td { background: var(--surface-2); font-weight: 700; font-size: 0.82rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--primary); }
.totals { display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 1rem; }
.total-box { border: 1px solid var(--border); border-radius: var(--radius); padding: 0.9rem 1rem; background: var(--surface-2); }
.total-box h3 { margin: 0 0 0.5rem; font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); }
.total-box .line { display: flex; justify-content: space-between; gap: 1rem; font-variant-numeric: tabular-nums; }
.total-box .grand { font-size: 1.2rem; font-weight: 800; color: var(--primary); border-top: 1px solid var(--border); margin-top: 0.4rem; padding-top: 0.4rem; }
.pay-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 1rem; }
.pay-option { border: 2px solid var(--border); border-radius: var(--radius); padding: 0.9rem 1rem; background: #fff; }
.pay-option.selected { border-color: var(--accent); }
.pay-option h3 { margin-top: 0; display: flex; justify-content: space-between; align-items: center; }

/* ---------- Divers ---------- */
.toast {
  position: fixed; right: 1rem; bottom: 4.5rem; z-index: 50;
  background: var(--text); color: #fff; padding: 0.7rem 1rem; border-radius: 6px;
  box-shadow: var(--shadow); max-width: 420px; font-size: 0.92rem;
}
.toast.error { background: var(--danger); }
.empty { text-align: center; color: var(--muted); padding: 2rem 1rem; }
.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 1rem; background: var(--primary); }
.login-card { width: 100%; max-width: 380px; }
.login-card .brand { font-weight: 800; font-size: 1.2rem; margin-bottom: 1rem; color: var(--primary); }
.login-card .brand span { color: var(--accent); }
dialog { border: none; border-radius: var(--radius); padding: 1.25rem; max-width: 520px; width: calc(100% - 2rem); box-shadow: 0 10px 40px rgba(0, 0, 0, 0.25); }
dialog::backdrop { background: rgba(16, 24, 40, 0.45); }
.stat { font-size: 1.6rem; font-weight: 800; color: var(--primary); }

/* ---------- Mobile ---------- */
.fiche-layout > *, .card, .grid > * { min-width: 0; }
@media (max-width: 900px) {
  .fiche-layout { grid-template-columns: minmax(0, 1fr); }
}
@media (max-width: 700px) {
  .topbar { flex-wrap: wrap; height: auto; padding: 0.5rem 1rem; gap: 0.25rem 1rem; }
  .topbar nav { order: 3; flex-basis: 100%; }
  .topbar .user { margin-left: auto; }
  main { padding: 1rem 1rem 6rem; }
  .card { padding: 1rem; }
  .grid, .grid.two { grid-template-columns: minmax(0, 1fr); }
  .savebar { flex-wrap: wrap; padding: 0.6rem 1rem; }
  .savebar .status { flex-basis: 100%; }
}
@media (max-width: 760px) {
  .module:not(.on) > input, .module:not(.on) > label.check.small { display: none; }
}
