/* ============================================================
   OCR Factures — Feuille de style principale
   Design : moderne, épuré, mobile-first
   ============================================================ */

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
    --primary:       #2563eb;
    --primary-dark:  #1d4ed8;
    --primary-light: #dbeafe;
    --bg:            #f1f5f9;
    --surface:       #ffffff;
    --border:        #e2e8f0;
    --text:          #1e293b;
    --text-muted:    #64748b;
    --success:       #059669;
    --success-light: #d1fae5;
    --warning:       #d97706;
    --warning-light: #fef3c7;
    --danger:        #dc2626;
    --danger-light:  #fee2e2;
    --info-light:    #dbeafe;
    --nav-bg:        #0f172a;
    --nav-text:      #94a3b8;
    --shadow-sm:     0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.06);
    --shadow-md:     0 4px 6px rgba(0,0,0,.07), 0 2px 4px rgba(0,0,0,.05);
    --radius:        8px;
    --radius-lg:     12px;
    --transition:    0.15s ease;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', sans-serif;
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
    line-height: 1.5;
    font-size: 15px;
}

/* ─── Navigation ─────────────────────────────────────────────────────────── */

nav {
    background: var(--nav-bg);
    padding: 0 1rem;
    display: flex;
    align-items: center;
    height: 56px;
    gap: .5rem;
    position: sticky;
    top: 0;
    z-index: 200;
    box-shadow: 0 1px 0 rgba(255,255,255,.05);
}

nav .brand {
    color: #fff;
    font-weight: 700;
    font-size: 1.05rem;
    text-decoration: none;
    margin-right: auto;
    letter-spacing: -.01em;
    display: flex;
    align-items: center;
    gap: .5rem;
}
nav .brand span { color: #60a5fa; }

.nav-logo {
    height: 36px;
    width: auto;
    max-width: 120px;
    object-fit: contain;
    display: block;
}
.nav-logo-fallback { display: none; }

.company-name-nav {
    font-size: .75rem;
    color: #64748b;
    display: none;
    white-space: nowrap;
}
@media (min-width: 1024px) {
    .company-name-nav { display: inline; }
}

nav a {
    color: var(--nav-text);
    text-decoration: none;
    padding: .4rem .75rem;
    border-radius: 6px;
    transition: all var(--transition);
    font-size: .875rem;
    display: flex;
    align-items: center;
    gap: .35rem;
    white-space: nowrap;
}
nav a:hover { color: #fff; background: rgba(255,255,255,.08); }
nav a.active { color: #fff; background: rgba(255,255,255,.12); }

.user-info {
    display: flex;
    align-items: center;
    gap: .5rem;
    border-left: 1px solid rgba(255,255,255,.08);
    padding-left: .75rem;
    margin-left: .25rem;
    color: var(--nav-text);
    font-size: .825rem;
}

.role-badge {
    background: var(--primary);
    color: #fff;
    padding: 2px 8px;
    border-radius: 20px;
    font-size: .7rem;
    font-weight: 600;
    letter-spacing: .03em;
    text-transform: uppercase;
}

/* ─── Contenu principal ──────────────────────────────────────────────────── */

main {
    max-width: 1400px;
    margin: 0 auto;
    padding: 1.5rem;
}

.page-header { margin-bottom: 1.5rem; }
.page-header h1 { font-size: 1.4rem; font-weight: 700; }
.page-header p  { color: var(--text-muted); font-size: .875rem; margin-top: .2rem; }

/* ─── Cards ─────────────────────────────────────────────────────────────── */

.card {
    background: var(--surface);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-sm);
    border: 1px solid var(--border);
}
.card-body { padding: 1.5rem; }

/* ─── Boutons ────────────────────────────────────────────────────────────── */

.btn {
    display: inline-flex;
    align-items: center;
    gap: .35rem;
    padding: .45rem .9rem;
    border-radius: 6px;
    font-size: .875rem;
    font-weight: 500;
    cursor: pointer;
    border: 1px solid transparent;
    transition: all var(--transition);
    text-decoration: none;
    line-height: 1.4;
    white-space: nowrap;
}
.btn:disabled { opacity: .5; cursor: not-allowed; }

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

.btn-success { background: var(--success); color: #fff; }
.btn-success:hover:not(:disabled) { background: #047857; }

.btn-danger  { background: var(--danger);  color: #fff; }
.btn-danger:hover:not(:disabled)  { background: #b91c1c; }

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

.btn-sm  { padding: .3rem .65rem; font-size: .8rem; }
.btn-lg  { padding: .65rem 1.25rem; font-size: .95rem; }

/* ─── Formulaires ────────────────────────────────────────────────────────── */

label {
    display: block;
    font-size: .8rem;
    font-weight: 500;
    color: var(--text-muted);
    margin-bottom: .3rem;
}
.form-group { margin-bottom: .875rem; }

input[type="text"],
input[type="number"],
input[type="date"],
input[type="email"],
input[type="password"],
select,
textarea {
    width: 100%;
    padding: .45rem .7rem;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: .875rem;
    color: var(--text);
    background: var(--surface);
    transition: border-color var(--transition), box-shadow var(--transition);
    line-height: 1.4;
}
input:focus, select:focus, textarea:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(37,99,235,.12);
}

input[type="checkbox"] {
    width: 15px;
    height: 15px;
    cursor: pointer;
    accent-color: var(--primary);
    flex-shrink: 0;
}

/* ─── Zone d'upload ──────────────────────────────────────────────────────── */

.upload-zone {
    border: 2px dashed var(--border);
    border-radius: var(--radius-lg);
    padding: 3rem 2rem;
    text-align: center;
    cursor: pointer;
    background: var(--bg);
    transition: all var(--transition);
}
.upload-zone:hover,
.upload-zone.dragging {
    border-color: var(--primary);
    background: var(--primary-light);
}
.upload-zone .upload-icon { font-size: 2.5rem; display: block; margin-bottom: .75rem; }
.upload-zone h3            { font-size: 1.05rem; margin-bottom: .35rem; }
.upload-zone p             { color: var(--text-muted); font-size: .85rem; }

/* ─── Barres de progression ──────────────────────────────────────────────── */

.progress-wrap {
    height: 5px;
    background: var(--border);
    border-radius: 3px;
    overflow: hidden;
}
.progress-bar {
    height: 100%;
    background: var(--primary);
    border-radius: 3px;
    transition: width .4s ease;
}
.progress-bar.success { background: var(--success); }
.progress-bar.warning { background: var(--warning); }
.progress-bar.danger  { background: var(--danger);  }

/* ─── Liste de fichiers ──────────────────────────────────────────────────── */

.file-list { display: flex; flex-direction: column; gap: .6rem; margin-top: 1.25rem; }

.file-item {
    display: grid;
    grid-template-columns: 2rem 1fr auto;
    align-items: center;
    gap: .75rem;
    padding: .875rem 1rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow-sm);
}
.file-item .file-icon  { font-size: 1.4rem; text-align: center; }
.file-item .file-name  { font-weight: 500; font-size: .875rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.file-item .file-meta  { font-size: .775rem; color: var(--text-muted); margin-top: .15rem; }
.file-item .file-prog  { margin-top: .4rem; }
.file-item .file-actions { display: flex; gap: .4rem; align-items: center; }

/* ─── Tableau ────────────────────────────────────────────────────────────── */

.table-container { overflow-x: auto; }

table { width: 100%; border-collapse: collapse; font-size: .85rem; }

thead th {
    background: #f8fafc;
    padding: .65rem 1rem;
    text-align: left;
    font-weight: 600;
    color: var(--text-muted);
    font-size: .75rem;
    text-transform: uppercase;
    letter-spacing: .06em;
    border-bottom: 2px solid var(--border);
    white-space: nowrap;
    user-select: none;
}
thead th[data-sort] { cursor: pointer; }
thead th[data-sort]:hover { color: var(--text); }
thead th.sorted-asc .sort-icon::after  { content: ' ↑'; color: var(--primary); }
thead th.sorted-desc .sort-icon::after { content: ' ↓'; color: var(--primary); }
.sort-icon { opacity: .4; font-size: .7rem; }

tbody td {
    padding: .8rem 1rem;
    border-bottom: 1px solid var(--border);
    vertical-align: middle;
}
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover td { background: #f8fafc; }

.text-right { text-align: right !important; }
.amount { font-family: 'SF Mono', Consolas, 'Courier New', monospace; font-weight: 600; }

/* ─── Badges ─────────────────────────────────────────────────────────────── */

.badge {
    display: inline-flex;
    align-items: center;
    gap: .25rem;
    padding: .2rem .55rem;
    border-radius: 20px;
    font-size: .72rem;
    font-weight: 600;
    letter-spacing: .02em;
    white-space: nowrap;
}
.badge-success { background: var(--success-light); color: #065f46; }
.badge-warning { background: var(--warning-light); color: #92400e; }
.badge-danger  { background: var(--danger-light);  color: #991b1b; }
.badge-info    { background: var(--info-light);    color: #1e40af; }
.badge-default { background: #f1f5f9;              color: var(--text-muted); }

/* ─── Filtres ────────────────────────────────────────────────────────────── */

.filters {
    display: flex;
    flex-wrap: wrap;
    gap: .65rem;
    align-items: flex-end;
}
.filter-group { flex: 1; min-width: 130px; }
.filter-group label { margin-bottom: .25rem; }

/* ─── Barre de sélection ─────────────────────────────────────────────────── */

.selection-bar {
    background: var(--primary);
    color: #fff;
    padding: .65rem 1rem;
    border-radius: var(--radius);
    display: none;
    align-items: center;
    gap: 1rem;
    margin-bottom: 1rem;
    font-size: .875rem;
}
.selection-bar.visible { display: flex; }

/* ─── Pagination ─────────────────────────────────────────────────────────── */

.pagination {
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: .825rem;
    color: var(--text-muted);
}
.page-btns { display: flex; gap: .25rem; flex-wrap: wrap; }
.page-btn {
    padding: .3rem .6rem;
    border: 1px solid var(--border);
    border-radius: 5px;
    background: var(--surface);
    cursor: pointer;
    font-size: .8rem;
    color: var(--text);
    transition: all var(--transition);
    line-height: 1.3;
}
.page-btn:hover  { background: var(--bg); }
.page-btn.active { background: var(--primary); color: #fff; border-color: var(--primary); }
.page-btn:disabled { opacity: .4; cursor: default; }

/* ─── Alertes ────────────────────────────────────────────────────────────── */

.alert {
    padding: .75rem 1rem;
    border-radius: var(--radius);
    font-size: .875rem;
    display: flex;
    align-items: flex-start;
    gap: .5rem;
    margin-bottom: 1rem;
}
.alert-success { background: var(--success-light); color: #065f46; border: 1px solid #a7f3d0; }
.alert-error   { background: var(--danger-light);  color: #991b1b; border: 1px solid #fca5a5; }
.alert-warning { background: var(--warning-light); color: #92400e; border: 1px solid #fde68a; }
.alert-info    { background: var(--info-light);    color: #1e40af; border: 1px solid #bfdbfe; }

/* ─── Modal ──────────────────────────────────────────────────────────────── */

.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,.55);
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
    backdrop-filter: blur(2px);
}
.modal {
    background: var(--surface);
    border-radius: var(--radius-lg);
    width: 100%;
    max-width: 1050px;
    max-height: 92vh;
    display: flex;
    flex-direction: column;
    box-shadow: 0 25px 60px rgba(0,0,0,.3);
    overflow: hidden;
}
.modal-header {
    padding: 1rem 1.25rem;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    gap: .75rem;
    flex-shrink: 0;
}
.modal-header h3 { font-size: 1rem; font-weight: 600; flex: 1; }

.modal-body {
    display: flex;
    flex: 1;
    overflow: hidden;
    min-height: 0;
}
.modal-preview {
    flex: 1;
    min-width: 200px;
    background: #f8fafc;
    overflow: auto;
    display: flex;
    align-items: flex-start;
    justify-content: center;
    padding: 1rem;
}
.modal-preview img {
    max-width: 100%;
    box-shadow: var(--shadow-md);
    border-radius: 4px;
    cursor: zoom-in;
}
.modal-preview img.zoomed { cursor: zoom-out; transform: scale(1.8); transform-origin: top left; }
.modal-preview iframe {
    width: 100%;
    min-height: 480px;
    height: 100%;
    border: none;
    border-radius: 4px;
}
.modal-resize-handle {
    width: 5px;
    cursor: col-resize;
    background: var(--border);
    flex-shrink: 0;
    transition: background var(--transition);
    user-select: none;
    -webkit-user-select: none;
}
.modal-resize-handle:hover,
.modal-resize-handle.dragging {
    background: var(--primary);
}
.modal-form {
    width: 340px;
    min-width: 180px;
    flex-shrink: 0;
    padding: 1.25rem;
    overflow-y: auto;
    border-left: 1px solid var(--border);
}
.modal-footer {
    padding: .875rem 1.25rem;
    border-top: 1px solid var(--border);
    display: flex;
    gap: .5rem;
    justify-content: flex-end;
    flex-shrink: 0;
}

/* ─── TVA lines ──────────────────────────────────────────────────────────── */

.tva-line { display: flex; gap: .4rem; align-items: center; margin-bottom: .4rem; }
.tva-line input { min-width: 0; }
.tva-line .btn-remove { flex-shrink: 0; padding: .25rem .5rem; font-size: .8rem; }

/* ─── Texte OCR brut ─────────────────────────────────────────────────────── */

pre.ocr-raw {
    background: #f8fafc;
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: .75rem;
    font-size: .72rem;
    max-height: 180px;
    overflow-y: auto;
    white-space: pre-wrap;
    word-break: break-all;
    margin-top: .4rem;
    color: var(--text-muted);
}

/* ─── Page de connexion ──────────────────────────────────────────────────── */

.login-container {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--nav-bg);
    padding: 1rem;
    background-image: radial-gradient(ellipse at 20% 50%, rgba(37,99,235,.15) 0%, transparent 60%);
}
.login-card {
    background: var(--surface);
    border-radius: var(--radius-lg);
    padding: 2.5rem 2rem;
    width: 100%;
    max-width: 400px;
    box-shadow: 0 25px 60px rgba(0,0,0,.45);
}
.login-logo {
    text-align: center;
    margin-bottom: 1.25rem;
}
.login-logo img {
    max-height: 70px;
    max-width: 220px;
    object-fit: contain;
}
.login-card h1 {
    font-size: 1.6rem;
    font-weight: 800;
    text-align: center;
    margin-bottom: .2rem;
    letter-spacing: -.02em;
    color: var(--primary);
}
.login-company {
    text-align: center;
    font-size: .8rem;
    color: var(--text-muted);
    margin-bottom: .25rem !important;
    font-weight: 500;
}
.login-card > p {
    color: var(--text-muted);
    text-align: center;
    font-size: .875rem;
    margin-bottom: 2rem;
}

/* ─── Agrégats bas de tableau ────────────────────────────────────────────── */

.totals-row td {
    font-weight: 700;
    background: #f8fafc !important;
    border-top: 2px solid var(--border);
    border-bottom: none;
}

/* ─── Spinner ────────────────────────────────────────────────────────────── */

@keyframes spin { to { transform: rotate(360deg); } }
.spinner {
    display: inline-block;
    width: 16px;
    height: 16px;
    border: 2px solid var(--border);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin .7s linear infinite;
    flex-shrink: 0;
}

/* ─── Utilitaires ────────────────────────────────────────────────────────── */

.d-flex     { display: flex; }
.flex-wrap  { flex-wrap: wrap; }
.align-center   { align-items: center; }
.justify-between{ justify-content: space-between; }
.gap-1  { gap: .5rem; }
.gap-2  { gap: 1rem; }
.mt-1   { margin-top: .5rem; }
.mt-2   { margin-top: 1rem; }
.mb-1   { margin-bottom: .5rem; }
.mb-2   { margin-bottom: 1rem; }
.w-full { width: 100%; }
.text-muted   { color: var(--text-muted); }
.text-success { color: var(--success); }
.text-danger  { color: var(--danger); }
.fw-600 { font-weight: 600; }

/* ─── Responsive ─────────────────────────────────────────────────────────── */

@media (max-width: 768px) {
    main { padding: 1rem; }

    .user-info span:not(.role-badge) { display: none; }

    .modal-body { flex-direction: column; }
    .modal-resize-handle { display: none; }
    .modal-preview { flex: none; height: 280px; width: auto !important; }
    .modal-form    { width: auto !important; border-left: none; border-top: 1px solid var(--border); }

    .filters { flex-direction: column; }
    .filter-group { min-width: 100%; }

    .file-item { grid-template-columns: 2rem 1fr; }
    .file-item .file-actions { grid-column: 1 / -1; }

    .page-header.d-flex { flex-direction: column; align-items: flex-start; }

    table { font-size: .8rem; }
    thead th, tbody td { padding: .6rem .75rem; }
}

@media (max-width: 480px) {
    nav .brand span { display: none; }
    .upload-zone { padding: 2rem 1rem; }
    .login-card { padding: 2rem 1.25rem; }
}
