:root {
  --bg: #f4f6f9;
  --panel: #ffffff;
  --text: #1c2430;
  --muted: #6b7686;
  --border: #e1e6ed;
  --primary: #0b5cad;
  --primary-soft: #e6f0fa;
  --success: #1a8f4c;
  --warning: #b7791f;
  --danger: #c53030;
  --sidebar: #0f1e33;
  --radius: 8px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 14px;
  color: var(--text);
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); display: flex; min-height: 100vh; }
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

.sidebar { width: 210px; background: var(--sidebar); color: #c9d4e3; flex-shrink: 0; position: sticky; top: 0; height: 100vh; }
.brand { display: flex; align-items: center; gap: 10px; padding: 20px 18px; color: #fff; font-weight: 700; font-size: 17px; }
.brand svg { color: #4fa3ff; }
#nav a { display: block; padding: 10px 20px; color: #c9d4e3; border-left: 3px solid transparent; }
#nav a:hover { background: rgba(255,255,255,.06); text-decoration: none; }
#nav a.active { background: rgba(79,163,255,.12); color: #fff; border-left-color: #4fa3ff; }

main { flex: 1; padding: 24px 32px; min-width: 0; }
h1 { font-size: 22px; margin: 0; }
h2 { font-size: 16px; margin: 0 0 12px; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 20px; flex-wrap: wrap; }
.page-head .actions { display: flex; gap: 8px; flex-wrap: wrap; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.right { text-align: right; }
.nowrap { white-space: nowrap; }

.card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; margin-bottom: 18px; }
.grid { display: grid; gap: 16px; }
.grid-4 { grid-template-columns: repeat(auto-fit, minmax(min(190px, 100%), 1fr)); }
.grid-2 { grid-template-columns: repeat(auto-fit, minmax(min(340px, 100%), 1fr)); }
.kpi .label { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .04em; }
.kpi .value { font-size: 24px; font-weight: 700; margin-top: 6px; }
.kpi .sub { color: var(--muted); font-size: 12px; margin-top: 4px; }
.kpi.danger .value { color: var(--danger); }

table { width: 100%; border-collapse: collapse; }
th, td { padding: 9px 10px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: middle; }
th { font-size: 12px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .03em; background: #fafbfd; }
tbody tr.clickable { cursor: pointer; }
tbody tr.clickable:hover { background: #f7f9fc; }
.table-wrap { overflow-x: auto; }
.empty { text-align: center; color: var(--muted); padding: 30px; }

.btn { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--border); background: #fff; color: var(--text); padding: 8px 14px; border-radius: 6px; cursor: pointer; font: inherit; font-weight: 500; white-space: nowrap; }
.btn:hover { background: #f3f5f8; text-decoration: none; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: #094c8f; }
.btn.success { background: var(--success); border-color: var(--success); color: #fff; }
.btn.danger { color: var(--danger); border-color: #f0c4c4; }
.btn.danger:hover { background: #fdf1f1; }
.btn.sm { padding: 4px 9px; font-size: 12px; }
.btn.icon { padding: 4px 8px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(220px, 100%), 1fr)); gap: 14px 16px; }
.field { display: flex; flex-direction: column; gap: 5px; }
.field.full { grid-column: 1 / -1; }
label { font-size: 12px; font-weight: 600; color: var(--muted); }
input, select, textarea { font: inherit; padding: 8px 10px; border: 1px solid #cdd5df; border-radius: 6px; background: #fff; color: var(--text); width: 100%; }
input:focus, select:focus, textarea:focus { outline: 2px solid #b8d5f3; border-color: var(--primary); }
textarea { min-height: 70px; resize: vertical; }
.filters { display: flex; gap: 10px; margin-bottom: 14px; flex-wrap: wrap; }
.filters input, .filters select { width: auto; min-width: 180px; }

.badge { display: inline-block; padding: 3px 9px; border-radius: 999px; font-size: 12px; font-weight: 600; background: #eef1f5; color: #4a5568; white-space: nowrap; }
.badge.planifie { background: #eef1f5; color: #4a5568; }
.badge.en_cours, .badge.emise { background: var(--primary-soft); color: var(--primary); }
.badge.suspendu { background: #fff4e0; color: var(--warning); }
.badge.termine, .badge.payee, .badge.accepte, .badge.rembourse { background: #e3f6eb; color: var(--success); }
.badge.envoyee { background: #ebe7fb; color: #5a3fc0; }
.badge.facture { background: #e0f4f4; color: #137a7a; }
.badge.annulee, .badge.refuse { background: #fdeaea; color: var(--danger); }
.badge.retard { background: var(--danger); color: #fff; }
.badge.brouillon { background: #f2f2f2; color: #777; border: 1px dashed #bbb; }

.progress { height: 8px; background: #e7ebf0; border-radius: 99px; overflow: hidden; min-width: 80px; }
.progress > span { display: block; height: 100%; background: var(--primary); }

.lines td { padding: 5px 6px; }
.lines input, .lines select { padding: 6px 8px; }
.lines .c-qte { width: 80px; } .lines .c-u { width: 70px; } .lines .c-pu { width: 110px; } .lines .c-tva { width: 80px; } .lines .c-tot { width: 110px; } .lines .c-del { width: 40px; }
.totals { margin-left: auto; width: 300px; margin-top: 14px; }
.totals div { display: flex; justify-content: space-between; padding: 5px 0; }
.totals .ttc { font-size: 17px; font-weight: 700; border-top: 2px solid var(--text); margin-top: 4px; padding-top: 8px; }

.detail-list { display: grid; grid-template-columns: 150px 1fr; gap: 8px 12px; margin: 0; }
.detail-list dt { color: var(--muted); font-size: 13px; }
.detail-list dd { margin: 0; }

.chart { display: flex; align-items: flex-end; gap: 6px; height: 160px; padding-top: 10px; }
.chart .bar { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: flex-end; height: 100%; gap: 4px; }
.chart .bar span { width: 100%; max-width: 38px; background: var(--primary); border-radius: 4px 4px 0 0; min-height: 2px; }
.chart .bar small { font-size: 10px; color: var(--muted); }

.modal-back { position: fixed; inset: 0; background: rgba(15,30,51,.45); display: flex; align-items: flex-start; justify-content: center; padding: 60px 16px; z-index: 50; overflow-y: auto; }
.modal { background: #fff; border-radius: 10px; width: 100%; max-width: 640px; box-shadow: 0 20px 50px rgba(0,0,0,.25); }
.modal header { padding: 16px 20px; border-bottom: 1px solid var(--border); display: flex; justify-content: space-between; align-items: center; }
.modal header h2 { margin: 0; }
.modal .body { padding: 20px; }
.modal footer { padding: 14px 20px; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: 8px; }
.close-x { border: 0; background: none; font-size: 22px; cursor: pointer; color: var(--muted); }

#toast { position: fixed; bottom: 24px; right: 24px; z-index: 100; display: flex; flex-direction: column; gap: 8px; }
.toast { background: var(--text); color: #fff; padding: 11px 16px; border-radius: 6px; box-shadow: 0 6px 20px rgba(0,0,0,.2); max-width: 420px; }
.toast.error { background: var(--danger); }
.toast.success { background: var(--success); }
.alert { padding: 10px 14px; border-radius: 6px; background: #fff8e6; border: 1px solid #f3dfa8; margin-bottom: 14px; }


.neg { color: var(--danger); }
.badge.avoir { background: #fdeaea; color: var(--danger); }

/* Photos de chantier */
.dropzone { border: 2px dashed #c3cedb; border-radius: var(--radius); padding: 16px; text-align: center; background: #fafbfd; margin-bottom: 16px; display: flex; flex-direction: column; gap: 10px; }
.dropzone.over { border-color: var(--primary); background: var(--primary-soft); }
.dz-opts { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; }
.dz-opts select, .dz-opts input { width: auto; min-width: 240px; }
.link-like { color: var(--primary); cursor: pointer; text-decoration: underline; font-size: inherit; font-weight: 600; }
.photo-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 12px; }
.photo { margin: 0; position: relative; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; cursor: pointer; background: #fff; }
.photo:hover { box-shadow: 0 4px 14px rgba(0,0,0,.12); }
.photo img { width: 100%; aspect-ratio: 4 / 3; object-fit: cover; display: block; background: #eef1f5; }
.photo .badge { position: absolute; top: 8px; left: 8px; }
.photo figcaption { padding: 7px 9px; font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.photo.refuse { border-color: #f0b4b4; } .photo.valide { border-color: #a9dcbd; }
.badge.ph-a_verifier { background: #fff4e0; color: var(--warning); }
.badge.ph-valide { background: #e3f6eb; color: var(--success); }
.badge.ph-refuse { background: #fdeaea; color: var(--danger); }
.pv-img { position: relative; background: #111; border-radius: 6px; text-align: center; }
.pv-img img { max-width: 100%; max-height: 62vh; display: block; margin: 0 auto; }
.pv-nav { position: absolute; top: 50%; transform: translateY(-50%); border: 0; background: rgba(0,0,0,.5); color: #fff; font-size: 30px; width: 44px; height: 60px; cursor: pointer; border-radius: 6px; }
.pv-nav.prev { left: 8px; } .pv-nav.next { right: 8px; }

/* Connexion & utilisateur */
.sidebar { display: flex; flex-direction: column; }
#nav { flex: 1; }
#userbox { padding: 14px 18px; border-top: 1px solid rgba(255,255,255,.1); font-size: 13px; }
#userbox .who { color: #fff; font-weight: 600; }
#userbox .role { color: #8fa3bd; font-size: 12px; margin-bottom: 8px; }
#userbox button { background: none; border: 0; color: #9fc6ff; cursor: pointer; padding: 2px 0; font: inherit; display: block; }
#userbox button:hover { text-decoration: underline; }
body.logged-out .sidebar { display: none; }
.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 16px; width: 100%; }
.login-card { background: #fff; border: 1px solid var(--border); border-radius: 12px; padding: 32px 28px; width: 100%; max-width: 380px; box-shadow: 0 10px 30px rgba(15,30,51,.08); }
.login-card h1 { display: flex; align-items: center; gap: 10px; margin-bottom: 6px; color: var(--primary); }
.login-card form { display: flex; flex-direction: column; gap: 14px; margin-top: 20px; }
.login-card .err { color: var(--danger); font-size: 13px; min-height: 18px; }

/* ====================================================================== Mobile & tablette */
.topbar, .nav-backdrop { display: none; }
.cam-btn { display: none; }

/* Tablette (iPad portrait et paysage) et téléphone : menu en tiroir */
@media (max-width: 1024px) {
  body { display: block; }
  .topbar {
    display: flex; align-items: center; gap: 10px; position: sticky; top: 0; z-index: 30;
    background: var(--sidebar); color: #fff; min-height: 56px;
    padding: env(safe-area-inset-top) 12px 0 max(8px, env(safe-area-inset-left));
  }
  .menu-btn { background: none; border: 0; color: #fff; width: 44px; height: 44px; display: grid; place-items: center; border-radius: 8px; cursor: pointer; }
  .menu-btn:active { background: rgba(255,255,255,.12); }
  .topbar-title { font-weight: 700; font-size: 17px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  body.logged-out .topbar { display: none; }

  .sidebar {
    position: fixed; top: 0; left: 0; bottom: 0; z-index: 50; width: min(290px, 85vw); height: auto;
    transform: translateX(-100%); transition: transform .22s ease; overflow-y: auto;
    padding-top: env(safe-area-inset-top); padding-bottom: env(safe-area-inset-bottom);
  }
  body.nav-open .sidebar { transform: none; box-shadow: 0 0 40px rgba(0,0,0,.35); }
  .nav-backdrop { position: fixed; inset: 0; z-index: 40; background: rgba(15,30,51,.5); }
  body.nav-open .nav-backdrop { display: block; }
  body.nav-open { overflow: hidden; }
  #nav a { padding: 14px 22px; font-size: 16px; }

  main { padding: 20px max(20px, env(safe-area-inset-right)) calc(24px + env(safe-area-inset-bottom)) max(20px, env(safe-area-inset-left)); }

  /* Pas de zoom automatique d'iOS dans les champs (police ≥ 16 px) */
  input, select, textarea { font-size: 16px; }
  .btn { min-height: 42px; }
  .btn.sm { min-height: 34px; }
  .modal-back { padding: 30px 16px; }
}

/* Écran tactile : appareil photo, pas d'effets de survol */
@media (pointer: coarse) {
  .cam-btn { display: inline-flex; }
  .dz-hint { display: none; }
  .photo:hover { box-shadow: none; }
}

/* Téléphone */
@media (max-width: 700px) {
  main { padding-left: max(14px, env(safe-area-inset-left)); padding-right: max(14px, env(safe-area-inset-right)); padding-top: 16px; }
  h1 { font-size: 20px; }
  .card { padding: 14px; border-radius: 10px; }
  .page-head { flex-direction: column; align-items: stretch; }
  .page-head .actions .btn { flex: 1 1 auto; justify-content: center; }
  .filters input, .filters select { width: 100%; min-width: 0; }
  .grid-4 { grid-template-columns: 1fr 1fr; gap: 10px; }
  .grid-4 .card { margin-bottom: 0; }
  .grid-4 + .grid-2, .grid-4 + .card { margin-top: 12px; }
  .kpi .value { font-size: 20px; }
  .detail-list { grid-template-columns: 105px 1fr; }
  .totals { width: 100%; }
  div:has(> #catalog) { width: 100%; }
  .form-grid > .field[style*="span"] { grid-column: 1 / -1 !important; }
  #itf { grid-template-columns: 1fr 1fr !important; }
  #itf > .field:nth-child(4), #itf > button { grid-column: 1 / -1; justify-content: center; }
  #catalog { flex: 1 1 100%; width: 100% !important; max-width: none !important; }
  .dz-opts select, .dz-opts input { width: 100%; min-width: 0; }
  .photo-grid { grid-template-columns: repeat(2, 1fr); gap: 8px; }

  /* Tableaux à en-tête -> cartes empilées (étiquettes ajoutées par app.js) */
  table.cards thead { display: none; }
  table.cards, table.cards tbody, table.cards tfoot, table.cards tr, table.cards td { display: block; width: 100%; }
  table.cards tr { border: 1px solid var(--border); border-radius: 10px; padding: 6px 12px; margin-bottom: 10px; background: #fff; }
  table.cards tbody tr.clickable:active { background: #f3f6fa; }
  table.cards td { border: 0; padding: 6px 0; display: flex; justify-content: space-between; align-items: center; gap: 12px; text-align: right; min-height: 0; }
  table.cards td::before { content: attr(data-label); color: var(--muted); font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .03em; text-align: left; flex-shrink: 0; }
  table.cards td:first-child { font-size: 15px; justify-content: flex-start; text-align: left; border-bottom: 1px solid var(--border); padding-bottom: 8px; margin-bottom: 2px; }
  table.cards td:first-child::before { display: none; }
  table.cards td:empty, table.cards td.empty-cell { display: none; }
  table.cards td.empty { justify-content: center; }
  table.cards td.empty::before { display: none; }
  table.cards td .cv { display: flex; flex-direction: column; align-items: flex-end; gap: 3px; min-width: 0; }
  table.cards td:first-child .cv { align-items: flex-start; }
  table.cards td .progress { width: 140px; max-width: 100%; }
  table.cards tfoot tr { background: var(--primary-soft); }
  .table-wrap { overflow: visible; }
  /* Éditeur de lignes de facture */
  table.cards.lines td { text-align: left; }
  table.cards.lines td input { max-width: 60%; text-align: right; }
  table.cards.lines td .cv { align-items: stretch; flex: 1; max-width: 60%; }
  table.cards.lines td .cv input { max-width: none; }
  table.cards.lines td:first-child .cv { max-width: none; }
  table.cards.lines td:first-child input { max-width: none; text-align: left; }
  table.cards.lines td:last-child { justify-content: flex-end; }

  /* Fenêtres en plein écran */
  .modal-back { padding: 0; align-items: stretch; }
  .modal { max-width: none !important; border-radius: 0; min-height: 100%; display: flex; flex-direction: column; }
  .modal header { position: sticky; top: 0; background: #fff; z-index: 1; padding-top: calc(14px + env(safe-area-inset-top)); }
  .modal .body { flex: 1; padding: 16px; }
  .modal footer { position: sticky; bottom: 0; background: #fff; padding-bottom: calc(12px + env(safe-area-inset-bottom)); }
  .modal footer .btn { flex: 1; justify-content: center; }
  .close-x { width: 44px; height: 44px; font-size: 28px; }
  .pv-img img { max-height: 55vh; }
  #pv .btn { flex: 1 1 40%; justify-content: center; }
  .pv-nav { width: 40px; height: 54px; }

  #toast { left: 12px; right: 12px; bottom: calc(16px + env(safe-area-inset-bottom)); }
  .toast { max-width: none; }
}
