/* ============================================================
   Gestionnaire de Factures — Feuille de styles principale
   ============================================================ */

/* Variables */
:root {
    --primary:       #2563eb;
    --primary-dark:  #1d4ed8;
    --primary-light: #eff6ff;
    --success:       #16a34a;
    --success-light: #f0fdf4;
    --warning:       #d97706;
    --warning-light: #fffbeb;
    --danger:        #dc2626;
    --danger-light:  #fef2f2;
    --gray-50:       #f9fafb;
    --gray-100:      #f3f4f6;
    --gray-200:      #e5e7eb;
    --gray-300:      #d1d5db;
    --gray-400:      #9ca3af;
    --gray-500:      #6b7280;
    --gray-600:      #4b5563;
    --gray-700:      #374151;
    --gray-800:      #1f2937;
    --gray-900:      #111827;
    --white:         #ffffff;
    --sidebar-w:     240px;
    --radius:        8px;
    --radius-lg:     12px;
    --shadow-sm:     0 1px 2px rgba(0,0,0,.05);
    --shadow:        0 1px 3px rgba(0,0,0,.1), 0 1px 2px rgba(0,0,0,.06);
    --shadow-md:     0 4px 6px -1px rgba(0,0,0,.1), 0 2px 4px -1px rgba(0,0,0,.06);
    --shadow-lg:     0 10px 15px -3px rgba(0,0,0,.1), 0 4px 6px -2px rgba(0,0,0,.05);
    --font:          'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    --transition:    150ms ease;
}

/* Reset */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 16px; -webkit-text-size-adjust: 100%; }
body { font-family: var(--font); color: var(--gray-800); background: var(--gray-50); line-height: 1.5; }
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
img { max-width: 100%; display: block; }
button { cursor: pointer; font-family: inherit; }
input, select, textarea, button { font-family: inherit; font-size: inherit; }

/* ============================================================
   Page de login
   ============================================================ */
.login-page {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    background: linear-gradient(135deg, #1e3a8a 0%, #2563eb 50%, #3b82f6 100%);
}

.login-container { width: 100%; max-width: 420px; padding: 24px; }

.login-card {
    background: var(--white);
    border-radius: var(--radius-lg);
    padding: 48px 40px;
    box-shadow: var(--shadow-lg);
    text-align: center;
}

.login-logo {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 72px;
    height: 72px;
    background: var(--primary-light);
    border-radius: var(--radius-lg);
    color: var(--primary);
    margin-bottom: 24px;
}

.login-card h1 { font-size: 1.5rem; font-weight: 700; color: var(--gray-900); margin-bottom: 8px; }
.login-subtitle { color: var(--gray-500); font-size: 0.9rem; margin-bottom: 32px; }

/* ============================================================
   Layout principal (sidebar + contenu)
   ============================================================ */
.app-layout {
    display: flex;
    min-height: 100vh;
}

/* Sidebar */
.sidebar {
    width: var(--sidebar-w);
    background: var(--gray-900);
    color: var(--white);
    display: flex;
    flex-direction: column;
    position: fixed;
    top: 0; left: 0;
    height: 100vh;
    z-index: 100;
    overflow-y: auto;
}

.sidebar-brand {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 20px 20px 16px;
    border-bottom: 1px solid rgba(255,255,255,.1);
}

.sidebar-brand svg { color: var(--primary); flex-shrink: 0; }
.sidebar-brand-text { font-weight: 700; font-size: 0.95rem; line-height: 1.2; }
.sidebar-brand-text small { display: block; font-weight: 400; font-size: 0.75rem; color: var(--gray-400); }

.sidebar-nav { flex: 1; padding: 16px 0; }

.nav-section-label {
    padding: 8px 20px 4px;
    font-size: 0.7rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: .08em;
    color: var(--gray-500);
}

.nav-link {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 20px;
    color: var(--gray-400);
    transition: color var(--transition), background var(--transition);
    font-size: 0.9rem;
    font-weight: 500;
    border-radius: 0;
}
.nav-link:hover, .nav-link.active {
    background: rgba(255,255,255,.06);
    color: var(--white);
    text-decoration: none;
}
.nav-link.active { border-left: 3px solid var(--primary); padding-left: 17px; }
.nav-link svg { flex-shrink: 0; opacity: .75; }
.nav-link.active svg { opacity: 1; color: var(--primary); }

.sidebar-footer {
    padding: 16px 20px;
    border-top: 1px solid rgba(255,255,255,.1);
}

/* Contenu principal */
.main-content {
    margin-left: var(--sidebar-w);
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 100vh;
}

.page-header {
    background: var(--white);
    border-bottom: 1px solid var(--gray-200);
    padding: 20px 32px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
}

.page-header h1 { font-size: 1.4rem; font-weight: 700; color: var(--gray-900); }
.page-header p { color: var(--gray-500); font-size: 0.875rem; margin-top: 2px; }

.page-body { padding: 28px 32px; flex: 1; }

/* ============================================================
   Composants
   ============================================================ */

/* Boutons */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 16px;
    border-radius: var(--radius);
    font-size: 0.875rem;
    font-weight: 500;
    border: 1px solid transparent;
    transition: background var(--transition), border-color var(--transition), color var(--transition), box-shadow var(--transition);
    white-space: nowrap;
    line-height: 1.25;
}
.btn:hover { text-decoration: none; }

.btn-primary { background: var(--primary); color: var(--white); border-color: var(--primary); }
.btn-primary:hover { background: var(--primary-dark); border-color: var(--primary-dark); }

.btn-secondary { background: var(--white); color: var(--gray-700); border-color: var(--gray-300); box-shadow: var(--shadow-sm); }
.btn-secondary:hover { background: var(--gray-50); }

.btn-danger { background: var(--danger); color: var(--white); border-color: var(--danger); }
.btn-danger:hover { background: #b91c1c; }

.btn-success { background: var(--success); color: var(--white); border-color: var(--success); }
.btn-success:hover { background: #15803d; }

.btn-sm { padding: 5px 10px; font-size: 0.8rem; }
.btn-xs { padding: 3px 8px; font-size: 0.75rem; }
.btn-full { width: 100%; justify-content: center; padding: 10px 16px; font-size: 1rem; }

/* Formulaires */
.form-group { margin-bottom: 16px; }
.form-group label { display: block; font-size: 0.875rem; font-weight: 500; color: var(--gray-700); margin-bottom: 6px; }
.form-control {
    width: 100%;
    padding: 8px 12px;
    border: 1px solid var(--gray-300);
    border-radius: var(--radius);
    font-size: 0.9rem;
    color: var(--gray-800);
    background: var(--white);
    transition: border-color var(--transition), box-shadow var(--transition);
}
.form-control:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(37,99,235,.15);
}
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.form-row-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 16px; }

/* Alertes */
.alert {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px 16px;
    border-radius: var(--radius);
    font-size: 0.875rem;
    margin-bottom: 16px;
}
.alert-error   { background: var(--danger-light);  color: var(--danger);  border: 1px solid #fecaca; }
.alert-success { background: var(--success-light); color: var(--success); border: 1px solid #bbf7d0; }
.alert-warning { background: var(--warning-light); color: var(--warning); border: 1px solid #fde68a; }
.alert-info    { background: var(--primary-light); color: var(--primary); border: 1px solid #bfdbfe; }

/* Cards */
.card {
    background: var(--white);
    border-radius: var(--radius-lg);
    border: 1px solid var(--gray-200);
    box-shadow: var(--shadow-sm);
    overflow: hidden;
}
.card-header {
    padding: 16px 20px;
    border-bottom: 1px solid var(--gray-200);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}
.card-header h2, .card-header h3 { font-size: 1rem; font-weight: 600; color: var(--gray-900); }
.card-body { padding: 20px; }
.card + .card { margin-top: 20px; }

/* Stats cards */
.stats-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 16px; margin-bottom: 24px; }
.stat-card {
    background: var(--white);
    border: 1px solid var(--gray-200);
    border-radius: var(--radius-lg);
    padding: 20px;
    display: flex;
    align-items: flex-start;
    gap: 16px;
}
.stat-icon {
    width: 44px; height: 44px;
    border-radius: var(--radius);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}
.stat-icon.blue   { background: var(--primary-light); color: var(--primary); }
.stat-icon.green  { background: var(--success-light); color: var(--success); }
.stat-icon.orange { background: var(--warning-light); color: var(--warning); }
.stat-icon.red    { background: var(--danger-light);  color: var(--danger);  }
.stat-value { font-size: 1.75rem; font-weight: 700; color: var(--gray-900); line-height: 1; }
.stat-label { font-size: 0.8rem; color: var(--gray-500); margin-top: 4px; }

/* Tableau */
.table-wrapper { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 0.875rem; }
thead { background: var(--gray-50); }
th {
    padding: 10px 16px;
    text-align: left;
    font-weight: 600;
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: .04em;
    color: var(--gray-500);
    border-bottom: 1px solid var(--gray-200);
    white-space: nowrap;
}
td {
    padding: 12px 16px;
    border-bottom: 1px solid var(--gray-100);
    color: var(--gray-700);
    vertical-align: middle;
}
tr:last-child td { border-bottom: none; }
tr:hover td { background: var(--gray-50); }

/* Badges statut */
.badge {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 2px 10px;
    border-radius: 9999px;
    font-size: 0.75rem;
    font-weight: 500;
}
.badge-pending   { background: var(--warning-light); color: var(--warning); }
.badge-processed { background: var(--success-light); color: var(--success); }
.badge-error     { background: var(--danger-light);  color: var(--danger);  }
.badge-ignored      { background: var(--gray-100);      color: var(--gray-400); }
.badge-needs-review { background: #fef9c3;             color: #854d0e; border: 1px solid #fde68a; }

/* Badges type de document */
.badge-type { border-radius: 6px; padding: 2px 8px; font-size: 0.72rem; }
.badge-type-blue   { background: #dbeafe; color: #1d4ed8; }
.badge-type-green  { background: #dcfce7; color: #15803d; }
.badge-type-purple { background: #ede9fe; color: #6d28d9; }
.badge-type-orange { background: #ffedd5; color: #c2410c; }
.badge-type-teal   { background: #ccfbf1; color: #0f766e; }
.badge-type-indigo { background: #e0e7ff; color: #4338ca; }
.badge-type-gray   { background: var(--gray-100); color: var(--gray-500); }
.badge-type-red    { background: var(--danger-light); color: var(--danger); }

/* Barre de confiance */
.confidence-bar {
    display: flex;
    align-items: center;
    gap: 6px;
    min-width: 80px;
}
.confidence-fill {
    height: 6px;
    border-radius: 3px;
    transition: width .3s;
    flex-shrink: 0;
}
.confidence-label {
    font-size: 0.72rem;
    color: var(--gray-500);
    white-space: nowrap;
}

/* Barre de recherche */
.search-bar {
    display: flex;
    gap: 12px;
    align-items: flex-end;
    flex-wrap: wrap;
    padding: 16px 20px;
    background: var(--gray-50);
    border-bottom: 1px solid var(--gray-200);
}
.search-field { flex: 1; min-width: 200px; }
.search-field label { display: block; font-size: 0.75rem; font-weight: 500; color: var(--gray-600); margin-bottom: 4px; }
.search-field .form-control { padding: 7px 12px; font-size: 0.875rem; }

.search-input-wrap { position: relative; flex: 1; min-width: 220px; }
.search-input-wrap svg { position: absolute; left: 10px; top: 50%; transform: translateY(-50%); color: var(--gray-400); pointer-events: none; }
.search-input-wrap .form-control { padding-left: 34px; }

/* Actions dans le tableau */
.table-actions { display: flex; gap: 6px; align-items: center; }

/* Pagination */
.empty-state {
    text-align: center;
    padding: 60px 24px;
    color: var(--gray-400);
}
.empty-state svg { margin: 0 auto 16px; opacity: .4; }
.empty-state h3 { font-size: 1rem; color: var(--gray-500); margin-bottom: 8px; }
.empty-state p  { font-size: 0.875rem; }

/* ============================================================
   Page prévisualisation
   ============================================================ */
.preview-layout { display: flex; gap: 24px; align-items: flex-start; }
.preview-sidebar { width: 280px; flex-shrink: 0; }
.preview-main { flex: 1; min-width: 0; }
.preview-frame {
    width: 100%; border: 1px solid var(--gray-200); border-radius: var(--radius);
    background: var(--gray-100); min-height: 600px;
}
.preview-frame iframe { width: 100%; height: 600px; border: none; border-radius: var(--radius); }
.preview-frame img { width: 100%; border-radius: var(--radius); }
.preview-text { background: var(--white); border: 1px solid var(--gray-200); border-radius: var(--radius); padding: 20px; font-size: 0.875rem; line-height: 1.7; white-space: pre-wrap; color: var(--gray-700); max-height: 600px; overflow-y: auto; }

.info-list { list-style: none; }
.info-list li { display: flex; justify-content: space-between; gap: 8px; padding: 8px 0; border-bottom: 1px solid var(--gray-100); font-size: 0.875rem; }
.info-list li:last-child { border-bottom: none; }
.info-list .info-key { color: var(--gray-500); font-weight: 500; }
.info-list .info-val { color: var(--gray-800); font-weight: 600; text-align: right; word-break: break-all; }

/* ============================================================
   Page paramètres
   ============================================================ */
.mailbox-list { list-style: none; }
.mailbox-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 14px 20px;
    border-bottom: 1px solid var(--gray-100);
}
.mailbox-item:last-child { border-bottom: none; }
.mailbox-icon {
    width: 40px; height: 40px;
    background: var(--primary-light);
    color: var(--primary);
    border-radius: var(--radius);
    display: flex; align-items: center; justify-content: center;
    flex-shrink: 0;
}
.mailbox-info { flex: 1; min-width: 0; }
.mailbox-name { font-weight: 600; font-size: 0.9rem; color: var(--gray-900); }
.mailbox-host { font-size: 0.8rem; color: var(--gray-500); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mailbox-actions { display: flex; gap: 6px; }

/* Modal */
.modal-overlay {
    position: fixed; inset: 0;
    background: rgba(0,0,0,.5);
    display: flex; align-items: center; justify-content: center;
    z-index: 1000;
    padding: 24px;
}
.modal-overlay.hidden { display: none; }

/* ── Modal prévisualisation document ── */
.preview-modal-box {
    background: var(--white);
    border-radius: var(--radius-lg);
    box-shadow: 0 24px 64px rgba(0,0,0,.3);
    width: min(92vw, 1200px);
    height: min(90vh, 860px);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}
.preview-modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    border-bottom: 1px solid var(--gray-200);
    background: var(--gray-50);
    gap: 12px;
    flex-shrink: 0;
}
.preview-modal-title {
    font-size: .9rem;
    font-weight: 600;
    color: var(--gray-800);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    min-width: 0;
}
.preview-modal-close {
    background: none;
    border: none;
    cursor: pointer;
    padding: 4px;
    color: var(--gray-400);
    border-radius: var(--radius);
    line-height: 0;
    flex-shrink: 0;
}
.preview-modal-close:hover { background: var(--gray-100); color: var(--gray-700); }
.preview-modal-body {
    flex: 1;
    overflow: hidden;
}
.preview-modal-body iframe {
    width: 100%;
    height: 100%;
    border: none;
    display: block;
}
.modal {
    background: var(--white);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    width: 100%;
    max-width: 540px;
    max-height: 90vh;
    overflow-y: auto;
}
.modal-header {
    display: flex; align-items: center; justify-content: space-between;
    padding: 20px 24px;
    border-bottom: 1px solid var(--gray-200);
}
.modal-header h3 { font-size: 1.05rem; font-weight: 600; }
.modal-close {
    background: none; border: none; color: var(--gray-400);
    padding: 4px; border-radius: var(--radius);
    transition: color var(--transition);
}
.modal-close:hover { color: var(--gray-700); }
.modal-body { padding: 24px; }
.modal-footer { padding: 16px 24px; border-top: 1px solid var(--gray-200); display: flex; gap: 10px; justify-content: flex-end; }

/* Cron info box */
.cron-info { background: var(--gray-900); color: #e5e7eb; border-radius: var(--radius); padding: 14px 16px; font-family: 'Courier New', monospace; font-size: 0.82rem; overflow-x: auto; }
.cron-info .token { color: #fbbf24; }

/* ============================================================
   Page Boîte de réception (inbox)
   ============================================================ */

/* Item de liste */
.inbox-item {
    background: var(--white);
    border: 1px solid var(--gray-200);
    border-radius: var(--radius-lg);
    margin-bottom: 10px;
    box-shadow: var(--shadow-sm);
    overflow: hidden;
    transition: box-shadow var(--transition);
}
.inbox-item:hover { box-shadow: var(--shadow); }

/* En-tête cliquable */
.inbox-header {
    display: flex;
    align-items: center;
    gap: 14px;
    padding: 14px 18px;
    cursor: pointer;
    user-select: none;
    transition: background var(--transition);
}
.inbox-header:hover { background: var(--gray-50); }
.inbox-header:focus { outline: 2px solid var(--primary); outline-offset: -2px; }
.inbox-header--open { background: var(--gray-50); }

/* Icône extension */
.inbox-file-icon {
    width: 38px; height: 38px;
    border-radius: var(--radius);
    display: flex; align-items: center; justify-content: center;
    flex-shrink: 0;
    background: var(--gray-100);
    color: var(--gray-500);
}
.inbox-ext-pdf  { background: #fef2f2; color: #dc2626; }
.inbox-ext-png,
.inbox-ext-jpg,
.inbox-ext-jpeg { background: #eff6ff; color: #2563eb; }

/* Méta (objet + détails) */
.inbox-meta { flex: 1; min-width: 0; }
.inbox-subject {
    font-weight: 600;
    font-size: .9rem;
    color: var(--gray-900);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.inbox-details {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 4px;
    margin-top: 3px;
    font-size: .75rem;
    color: var(--gray-500);
}
.inbox-from { display: flex; align-items: center; gap: 4px; color: var(--gray-600); }
.inbox-sep  { color: var(--gray-300); }
.inbox-filename { font-family: 'Courier New', monospace; font-size: .7rem; color: var(--gray-400); }

/* Badges et confiance (à droite de l'en-tête) */
.inbox-badges {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
}

/* Chevron */
.inbox-chevron {
    flex-shrink: 0;
    color: var(--gray-400);
    transition: transform var(--transition);
}
.inbox-header--open .inbox-chevron { transform: rotate(180deg); }

/* Panneau expansible */
.inbox-panel {
    border-top: 1px solid var(--gray-100);
}
.inbox-panel-inner {
    display: grid;
    grid-template-columns: 300px 1fr;
    gap: 0;
}

/* Colonne gauche (infos + OCR) */
.inbox-panel-left {
    padding: 20px;
    border-right: 1px solid var(--gray-100);
    background: var(--gray-50);
    overflow-y: auto;
    max-height: 560px;
}

/* Colonne droite (aperçu fichier) */
.inbox-panel-right {
    padding: 20px;
    background: var(--white);
}

/* Titre de section à l'intérieur du panneau */
.inbox-section-title {
    font-size: .72rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: .07em;
    color: var(--gray-400);
    margin-bottom: 10px;
}
.inbox-section { }

/* Texte OCR brut */
.inbox-ocr-text {
    background: var(--white);
    border: 1px solid var(--gray-200);
    border-radius: var(--radius);
    padding: 12px;
    font-family: 'Courier New', monospace;
    font-size: .72rem;
    line-height: 1.6;
    white-space: pre-wrap;
    color: var(--gray-600);
    max-height: 200px;
    overflow-y: auto;
}

/* Aperçu fichier dans le panneau */
.inbox-preview-wrap {
    border: 1px solid var(--gray-200);
    border-radius: var(--radius);
    overflow: hidden;
    background: var(--gray-100);
}

/* Pagination inbox */
.inbox-pagination {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 16px;
    margin-top: 20px;
    padding: 12px 0;
}
.inbox-pagination-info {
    font-size: .875rem;
    color: var(--gray-500);
}

/* Responsive inbox */
@media (max-width: 780px) {
    .inbox-panel-inner { grid-template-columns: 1fr; }
    .inbox-panel-left  { border-right: none; border-bottom: 1px solid var(--gray-100); max-height: none; }
    .inbox-badges      { display: none; }
}

/* Responsive */
@media (max-width: 768px) {
    .sidebar { width: 200px; }
    .main-content { margin-left: 200px; }
    .page-body { padding: 16px; }
    .form-row, .form-row-3 { grid-template-columns: 1fr; }
    .preview-layout { flex-direction: column; }
    .preview-sidebar { width: 100%; }
    .stats-grid { grid-template-columns: 1fr 1fr; }
}

@media (max-width: 520px) {
    .sidebar { display: none; }
    .main-content { margin-left: 0; }
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

.hidden { display: none !important; }
