:root {
  --bg: #0f1115;
  --panel: #171a21;
  --panel-2: #1e2230;
  --line: #2a2f3c;
  --txt: #e7e9ee;
  --muted: #8b91a1;
  --accent: #5b8cff;
  --accent-2: #3a6df0;
  --ok: #2ecc71;
  --warn: #f1c40f;
  --err: #e74c3c;
  --radius: 12px;
}
* { box-sizing: border-box; }
body {
  margin: 0; background: var(--bg); color: var(--txt);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

.nav {
  border-bottom: 1px solid var(--line); padding: 14px 24px; background: var(--panel);
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
}
.nav-right { display: flex; align-items: center; gap: 14px; }
.nav-user { color: var(--muted); font-size: 13px; }
.nav-logout, .nav-link { font-size: 13px; }

/* Réglages */
.inline-form { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.inline-form input[type=text], .inline-form select {
  background: var(--panel-2); border: 1px solid var(--line); color: var(--txt);
  padding: 10px 12px; border-radius: 9px; font-size: 14px;
}
.inline-form input[type=text]:focus, .inline-form select:focus { outline: none; border-color: var(--accent); }
.f-inline { display: inline-flex; align-items: center; gap: 7px; font-size: 14px; color: var(--txt); }
.f-inline input[type=checkbox] { width: 15px; height: 15px; accent-color: var(--accent); }
.cat-list { display: flex; flex-direction: column; gap: 8px; }
.cat-row { display: flex; gap: 10px; align-items: center; }
.cat-row .inline-form { flex: 1; }
.cat-row .inline-form input[type=text] { flex: 1; max-width: 420px; }
.brand { font-weight: 700; font-size: 17px; color: var(--txt); letter-spacing: .3px; }
.brand:hover { text-decoration: none; color: var(--accent); }

.wrap { max-width: none; margin: 0; padding: 28px 32px 60px; }

.head { margin-bottom: 22px; }
.head h1 { margin: 6px 0 4px; font-size: 26px; }
.sub { color: var(--muted); margin: 0; }
.back { font-size: 14px; }

.card {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 22px; margin-bottom: 22px;
}
.card h2 { margin: 0 0 14px; font-size: 16px; font-weight: 600; }

.upload .drop {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 6px; min-height: 140px; border: 2px dashed var(--line); border-radius: var(--radius);
  cursor: pointer; text-align: center; padding: 18px; transition: .15s;
  background: var(--panel-2);
}
.upload .drop:hover, .upload .drop.over { border-color: var(--accent); background: #1b2030; }
.upload .drop input { display: none; }
.drop-txt { color: var(--muted); }
.drop-txt small { color: #6b7180; }
.drop-count { color: var(--accent); font-size: 13px; font-weight: 600; }
.row { display: flex; gap: 10px; margin-top: 14px; }

/* Capture / file d'attente mobile */
.cap-zone { border: 2px dashed var(--line); border-radius: var(--radius); padding: 22px; text-align: center; background: var(--panel-2); transition: .15s; }
.cap-zone.over { border-color: var(--accent); background: #1b2030; }
.cap-actions { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
.cap-btn { font-size: 15px; padding: 12px 20px; }
.cap-hint { color: var(--muted); font-size: 13px; margin: 12px 0 0; }
.queue { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 14px; }
.q-item { position: relative; width: 88px; height: 88px; border: 1px solid var(--line); border-radius: 9px; overflow: hidden; background: var(--panel-2); }
.q-item img { width: 100%; height: 100%; object-fit: cover; display: block; }
.q-pdf { width: 100%; height: 100%; display: flex; align-items: center; justify-content: center; color: var(--muted); font-weight: 700; font-size: 13px; }
.q-rm { position: absolute; top: 3px; right: 3px; width: 22px; height: 22px; border-radius: 50%; border: 0; background: rgba(0,0,0,.6); color: #fff; cursor: pointer; font-size: 15px; line-height: 1; padding: 0; }
.q-rm:hover { background: var(--err); }
.cap-foot { margin-top: 14px; }
.cap-foot button { width: 100%; padding: 13px; font-size: 15px; }
@media (max-width: 720px) { .cap-actions { flex-direction: column; } .cap-btn { width: 100%; } }

input[type=text] {
  flex: 1; background: var(--panel-2); border: 1px solid var(--line); color: var(--txt);
  padding: 10px 12px; border-radius: 9px; font-size: 14px;
}
input[type=text]:focus { outline: none; border-color: var(--accent); }

button, .btn {
  background: var(--accent-2); color: #fff; border: 0; padding: 10px 16px; border-radius: 9px;
  font-size: 14px; font-weight: 600; cursor: pointer; transition: .15s; white-space: nowrap;
}
button:hover, .btn:hover { background: var(--accent); text-decoration: none; }
.btn { display: inline-block; }
.row-link { color: var(--txt); }
.row-link:hover { color: var(--accent); text-decoration: none; }

table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--line); }
th { color: var(--muted); font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: .4px; }
tbody tr:hover { background: var(--panel-2); }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
.file { color: var(--muted); max-width: 220px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.muted { color: var(--muted); }
.next { margin-top: 16px; font-size: 13px; }

.badge {
  display: inline-block; padding: 3px 9px; border-radius: 20px; font-size: 12px; font-weight: 600;
  background: var(--panel-2); color: var(--muted); border: 1px solid var(--line);
}
.b-uploaded, .b-pending { color: var(--warn); border-color: #4a421a; }
.b-extracting { color: var(--accent); border-color: #243a6b; }
.b-review, .b-extracted { color: var(--ok); border-color: #1d4730; }
.b-done { color: var(--ok); background: #14301f; border-color: #1d4730; }
.b-error { color: var(--err); border-color: #4a2420; }

/* Formulaire dossier */
.form-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 14px; margin-bottom: 16px; }
.f { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); }
.f-wide { grid-column: 1 / -1; }
.f input, .f select {
  background: var(--panel-2); border: 1px solid var(--line); color: var(--txt);
  padding: 10px 12px; border-radius: 9px; font-size: 14px;
}
.f input:focus, .f select:focus { outline: none; border-color: var(--accent); }
.upload button { margin-top: 14px; }
.card-head { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 14px; }
.card-head h2 { margin: 0; }
.total { color: var(--muted); font-size: 14px; }
.total strong { color: var(--txt); }
@media (max-width: 640px) { .form-grid { grid-template-columns: 1fr; } }

/* En-tête avec action + popup */
.head-row { display: flex; justify-content: space-between; align-items: flex-start; gap: 20px; }
.btn-lg { font-size: 14px; padding: 11px 18px; flex-shrink: 0; }
.btn-ghost { background: transparent; border: 1px solid var(--line); color: var(--txt); }
.btn-ghost:hover { background: var(--panel-2); }

.modal-overlay {
  position: fixed; inset: 0; background: rgba(8, 10, 14, .66); backdrop-filter: blur(2px);
  display: none; align-items: flex-start; justify-content: center; padding: 64px 20px; z-index: 100;
}
.modal-overlay.open { display: flex; animation: fade .12s ease; }
.modal {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 24px; width: 100%; max-width: 560px; box-shadow: 0 24px 70px rgba(0,0,0,.55);
  animation: pop .14s ease;
}
.modal-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 18px; }
.modal-head h2 { margin: 0; }
.modal-close { background: none; border: 0; color: var(--muted); font-size: 24px; line-height: 1; cursor: pointer; padding: 0 4px; }
.modal-close:hover { color: var(--txt); }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 8px; align-items: center; }
@keyframes fade { from { opacity: 0 } to { opacity: 1 } }

/* Import Drive : carte + navigateur de dossiers */
.drive-row { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.drive-paste { display: flex; gap: 8px; flex: 1; min-width: 240px; }
.drive-paste input[type=text] {
  flex: 1; background: var(--panel-2); border: 1px solid var(--line); color: var(--txt);
  padding: 10px 12px; border-radius: 9px; font-size: 14px;
}
.drive-paste input[type=text]:focus { outline: none; border-color: var(--accent); }
.drive-browser { max-width: 600px; }
.drive-bc { font-size: 13px; color: var(--muted); margin-bottom: 12px; word-break: break-word; }
.drive-bc .crumb { color: var(--accent); }
.drive-bc .crumb.cur { color: var(--txt); font-weight: 600; }
.drive-list { border: 1px solid var(--line); border-radius: 10px; overflow: hidden auto; max-height: 46vh; min-height: 120px; margin-bottom: 16px; }
.drive-item { display: flex; align-items: stretch; border-bottom: 1px solid var(--line); }
.drive-item:last-child { border-bottom: 0; }
.drive-nav {
  flex: 1; display: flex; align-items: center; gap: 10px; text-align: left;
  background: transparent; border: 0; border-radius: 0; color: var(--txt);
  padding: 12px 14px; font-size: 14px; cursor: pointer;
}
.drive-nav:hover { background: var(--panel-2); }
.drive-ico { font-size: 15px; }
.drive-chev { margin-left: auto; color: var(--muted); font-size: 18px; }
.drive-pick {
  background: transparent; border: 0; border-left: 1px solid var(--line); color: var(--accent);
  padding: 0 16px; font-size: 13px; font-weight: 600; border-radius: 0; cursor: pointer;
}
.drive-pick:hover { background: var(--accent); color: #fff; }
.drive-loading, .drive-empty { padding: 18px 14px; color: var(--muted); font-size: 13px; display: flex; align-items: center; gap: 8px; }
.drive-status { font-size: 13px; }
.drive-opt {
  display: flex; align-items: center; gap: 9px; margin: 16px 0 0; padding: 11px 13px;
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 9px;
  font-size: 14px; color: var(--txt); cursor: pointer; user-select: none;
}
.drive-opt input { width: 16px; height: 16px; accent-color: var(--accent); cursor: pointer; flex-shrink: 0; }

/* Capture + import Drive côte à côte (desktop) */
.io-row { display: flex; gap: 22px; align-items: stretch; margin-bottom: 22px; }
.io-row > * { flex: 1 1 0; min-width: 0; margin-bottom: 0; }
.import-card { display: flex; flex-direction: column; justify-content: center; }
@media (max-width: 860px) { .io-row { flex-direction: column; gap: 22px; } }

/* Barre de progression d'import */
.import-prog { margin-top: 16px; }
.ip-bar { height: 8px; background: var(--panel-2); border: 1px solid var(--line); border-radius: 6px; overflow: hidden; }
.ip-fill { height: 100%; width: 0; background: var(--accent); transition: width .25s ease; }
.ip-label { margin-top: 8px; font-size: 13px; color: var(--muted); word-break: break-word; }
.ip-label.err { color: var(--err); }

/* Aperçu justificatif (modale) */
.modal-overlay.viewer { align-items: center; padding: 40px 20px; }
.viewer-box {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  width: 100%; max-width: 960px; max-height: 88vh; display: flex; flex-direction: column;
  box-shadow: 0 24px 70px rgba(0,0,0,.55); animation: pop .14s ease; overflow: hidden;
}
.viewer-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 16px; border-bottom: 1px solid var(--line); }
.viewer-title { font-weight: 600; font-size: 14px; }
.viewer-actions { display: flex; align-items: center; gap: 12px; }
.viewer-body { flex: 1; min-height: 220px; overflow: auto; display: flex; align-items: center; justify-content: center; background: #0c0e12; padding: 10px; }
.viewer-img { max-width: 100%; max-height: 76vh; object-fit: contain; border-radius: 4px; }
.viewer-pdf { width: 100%; height: 76vh; border: 0; background: #fff; border-radius: 4px; }
.viewer-load { color: var(--muted); font-size: 13px; display: flex; align-items: center; gap: 8px; }
.work-prep { width: 100%; max-width: 460px; padding: 20px; }
.btn-ghost.arch-ok { color: var(--ok); border-color: #1d4730; }
@media (max-width: 720px) { .viewer-pdf { height: 70vh; } .modal-overlay.viewer { padding: 16px 10px; } }
@keyframes pop { from { opacity: 0; transform: translateY(-8px) scale(.98) } to { opacity: 1; transform: none } }

/* Extraction en cours */
.head-tools { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; }
.btn-sm { padding: 6px 12px; font-size: 13px; }
.extracting-tag { display: inline-flex; align-items: center; gap: 8px; color: var(--accent); font-size: 13px; font-weight: 600; }
.spinner {
  width: 13px; height: 13px; border: 2px solid var(--line); border-top-color: var(--accent);
  border-radius: 50%; display: inline-block; animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Conversion de devise */
.fx { font-size: 12px; font-weight: 600; color: var(--accent); cursor: help; white-space: nowrap; }
.fx-warn { color: var(--warn); }
.orig { font-size: 11px; color: var(--muted); margin-top: 2px; }
.fx-edit { display: flex; align-items: center; gap: 5px; margin-top: 5px; }
.fx-lbl { font-size: 11px; color: var(--muted); white-space: nowrap; }
.ci-taux { width: 86px; min-width: 0; text-align: right; padding: 5px 7px; font-size: 12px; font-variant-numeric: tabular-nums; }
.fx-src { font-size: 11px; color: var(--muted); margin-top: 3px; }
.fx-src.fx-warn { color: var(--warn); }

/* Tableau éditable (correction) */
.grid.edit td { vertical-align: middle; }
.ci {
  background: var(--panel-2); border: 1px solid var(--line); color: var(--txt);
  border-radius: 6px; padding: 6px 8px; font-size: 13px; width: 100%; min-width: 100px;
}
.ci:focus { outline: none; border-color: var(--accent); }
.ci-four { min-width: 300px; }
.ci-cat { min-width: 170px; }
.ci-num { text-align: right; min-width: 56px; font-variant-numeric: tabular-nums; }
.ci-sm { min-width: 96px; }
.ci-date { min-width: 140px; }
.ci-dev { min-width: 68px; width: auto; }
.conv-cell { font-variant-numeric: tabular-nums; white-space: nowrap; }
.save-bar { display: flex; align-items: center; justify-content: space-between; gap: 16px; margin-top: 16px; padding-top: 14px; border-top: 1px solid var(--line); flex-wrap: wrap; }
.save-bar .muted { font-size: 13px; }
.btn-pdf { background: var(--accent-2); color: #fff; border: 0; text-decoration: none; display: inline-block; }
.btn-pdf:hover { background: var(--accent); text-decoration: none; }

/* Voyant de validation (par ligne) */
.voyant-th { width: 22px; }
.voyant-cell { text-align: center; padding: 0 2px !important; }
.voyant { display: inline-block; width: 11px; height: 11px; border-radius: 50%; background: var(--muted); vertical-align: middle; }
.voyant.v-green  { background: var(--ok); }
.voyant.v-orange { background: var(--warn); }
.voyant.v-red    { background: var(--err); }
.voyant.v-wait   { background: var(--muted); animation: vpulse 1s ease-in-out infinite; }
@keyframes vpulse { 0%,100% { opacity: .35 } 50% { opacity: 1 } }

/* Barre filtre + légende */
.filter-bar { display: flex; flex-direction: column; align-items: stretch; gap: 10px; margin-bottom: 14px; }
.legend { display: flex; gap: 14px; flex-wrap: wrap; }
.legend .lg { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.four-filter { display: flex; align-items: center; gap: 10px; }
.four-filter .ci { flex: 1; width: auto; min-width: 0; }
.filter-count { font-size: 12px; white-space: nowrap; }
th.sortable { cursor: pointer; user-select: none; white-space: nowrap; }
th.sortable:hover { color: var(--accent); }
.sort-arr { color: var(--accent); font-weight: 700; }

.notice { padding: 12px 16px; border-radius: 9px; margin-bottom: 18px; font-size: 14px; }
.notice.ok { background: #14301f; border: 1px solid #1d4730; color: #2ecc71; }
.notice.err { background: #2a1a18; border: 1px solid #4a2420; color: #e98a80; }

/* Actions dossier / pièces */
.actions { display: flex; gap: 8px; flex-shrink: 0; align-items: flex-start; }
.actions form { display: inline; }
.btn-danger { background: transparent; border: 1px solid #4a2420; color: #e98a80; }
.btn-danger:hover { background: #2a1a18; }
.row-actions { display: flex; gap: 6px; align-items: center; white-space: nowrap; }
.row-actions form { display: inline; }
.ico {
  display: inline-flex; align-items: center; justify-content: center; width: 28px; height: 28px;
  border-radius: 7px; border: 1px solid var(--line); background: var(--panel-2); color: var(--muted);
  font-size: 13px; cursor: pointer; text-decoration: none; padding: 0;
}
.ico:hover { color: var(--txt); border-color: var(--accent); text-decoration: none; }
.ico-del:hover { color: #e98a80; border-color: #4a2420; }
.b-archive { color: var(--muted); }

/* Onglets actifs / archivés */
.filter-row { display: flex; gap: 18px; margin-bottom: 14px; border-bottom: 1px solid var(--line); }
.filter-row a { color: var(--muted); padding: 0 0 10px; font-size: 14px; border-bottom: 2px solid transparent; margin-bottom: -1px; }
.filter-row a:hover { color: var(--txt); text-decoration: none; }
.filter-row a.on { color: var(--txt); border-bottom-color: var(--accent); }

/* Tableaux scrollables sur mobile */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.table-wrap table { min-width: 680px; }

/* Page de connexion */
.login-body { display: flex; align-items: center; justify-content: center; min-height: 100vh; padding: 24px; }
.login-card {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 32px; width: 100%; max-width: 380px; display: flex; flex-direction: column; gap: 12px;
  box-shadow: 0 24px 70px rgba(0,0,0,.5);
}
.login-brand { font-size: 22px; font-weight: 700; }
.login-sub { color: var(--muted); margin: 0 0 6px; font-size: 14px; }
.login-card button { margin-top: 8px; }
.login-err { background: #2a1a18; border: 1px solid #4a2420; color: #e98a80; padding: 9px 12px; border-radius: 9px; font-size: 13px; }

/* Responsive */
@media (max-width: 720px) {
  .wrap { padding: 18px 14px 48px; }
  .nav { padding: 12px 16px; }
  .nav-user { display: none; }
  .head-row { flex-direction: column; align-items: stretch; }
  .head-row .btn-lg { width: 100%; }
  .actions { width: 100%; }
  .actions form, .actions .btn-ghost { flex: 1; }
  .actions .btn-danger { width: 100%; }
  .card { padding: 16px; }
  .card-head { flex-direction: column; align-items: flex-start; gap: 6px; }
  th, td { padding: 9px 9px; }
  .modal { padding: 20px; }
  .modal-overlay { padding: 28px 14px; }
}

/* Cellule résumé de carte : masquée sur desktop, sert d'en-tête accordéon sur mobile */
.td-sum { display: none; }

/* Liste des pièces sur mobile : cartes accordéon (résumé cliquable + détails dépliables) */
@media (max-width: 720px) {
  .grid.edit { min-width: 0 !important; }
  .grid.edit thead { display: none; }
  .grid.edit, .grid.edit tbody { display: block; }
  .grid.edit tr {
    display: block; background: var(--panel-2); border: 1px solid var(--line);
    border-radius: 12px; margin-bottom: 10px; overflow: hidden;
  }
  /* résumé toujours visible (ID · date · montant + chevron), cliquable */
  .td-sum {
    display: flex !important; align-items: center; justify-content: space-between;
    gap: 10px; padding: 14px; cursor: pointer; border: 0 !important;
  }
  .sum-main { display: flex; align-items: center; gap: 10px; min-width: 0; flex: 1; }
  .sum-main .voyant { flex-shrink: 0; }
  .sum-txt { display: flex; flex-direction: column; min-width: 0; }
  .sum-four { font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .sum-meta { font-size: 12px; color: var(--muted); }
  .sum-right { display: flex; align-items: center; gap: 12px; flex-shrink: 0; }
  .sum-amount { font-weight: 600; font-variant-numeric: tabular-nums; }
  .sum-chev { color: var(--muted); font-size: 20px; line-height: 1; transition: transform .15s ease; }
  tr.open { background: var(--panel); }
  tr.open .sum-chev { transform: rotate(90deg); }

  /* détails : cachés tant que la carte n'est pas dépliée */
  .grid.edit td:not(.td-sum) { display: none; }
  .grid.edit tr.open td:not(.td-sum) {
    display: block; border: 0 !important; padding: 0 14px 12px !important;
  }
  /* redondants avec le résumé */
  .grid.edit tr.open .td-voyant, .grid.edit tr.open .td-id, .grid.edit tr.open .td-conv { display: none !important; }
  .grid.edit tr.open td[data-label]::before {
    content: attr(data-label); display: block; margin: 0 0 4px;
    font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .4px;
  }
  .grid.edit tr.open .ci { width: 100% !important; min-width: 0 !important; font-size: 15px; padding: 10px; }
  .grid.edit tr.open .ci-taux { width: auto !important; flex: 1; font-size: 14px; padding: 8px; }
  .grid.edit tr.open .conv-cell { white-space: normal; }
  .grid.edit tr.open .row-actions { justify-content: flex-start; padding-top: 2px; }
  .grid.edit .ico { width: 34px; height: 34px; font-size: 15px; }
  .grid.edit .fx { white-space: normal; }
  .save-bar button { width: 100%; padding: 12px; }
}

/* Réglages : table des associations fournisseur→catégorie en cartes sur mobile */
@media (max-width: 720px) {
  .maps { min-width: 0 !important; }
  .maps thead { display: none; }
  .maps, .maps tbody { display: block; }
  .maps tr {
    display: grid; grid-template-columns: 1fr auto; align-items: center; gap: 2px 10px;
    padding: 12px 0; border-bottom: 1px solid var(--line);
  }
  .maps tr:last-child { border-bottom: 0; }
  .maps td { display: block; border: 0 !important; padding: 0 !important; }
  .maps td:nth-child(1) { grid-column: 1; font-weight: 600; word-break: break-word; }
  .maps td:nth-child(2) { grid-column: 1; color: var(--muted); font-size: 13px; }
  .maps td.row-actions { grid-column: 2; grid-row: 1 / 3; }

  /* Catégories : input pleine largeur, ✕ aligné en haut à droite */
  .cat-row { align-items: flex-start; }
  .cat-row .inline-form input[type=text] { flex: 1 1 100%; max-width: none; }
}
