/* =====================================================================
   KOFANAG MS — Back-office
   Palette issue du logo : noir, orange #FFA500, rouge #FF3131, bordeaux #A80000, bleu #1E5175
   ===================================================================== */
:root {
    --k-black: #141414;
    --k-ink: #1f1f1f;
    --k-orange: #FFA500;
    --k-red: #FF3131;
    --k-wine: #A80000;
    --k-wine-dark: #860000;
    --k-blue: #1E5175;
    --k-bg: #f5f3f1;
    --k-card: #ffffff;
    --k-border: #ebe6e2;
    --k-muted: #77716c;
    --bs-primary: #A80000;
    --bs-primary-rgb: 168, 0, 0;
    --bs-link-color: #A80000;
    --bs-link-hover-color: #860000;
    --bs-link-color-rgb: 168, 0, 0;
    --bs-body-font-family: 'Poppins', system-ui, -apple-system, 'Segoe UI', Roboto, Arial, sans-serif;
    --bs-body-font-size: .875rem;
    --bs-body-bg: var(--k-bg);
    --sidebar-w: 252px;
}
body.admin { background: var(--k-bg); color: var(--k-ink); }
.btn-primary { --bs-btn-bg: var(--k-wine); --bs-btn-border-color: var(--k-wine); --bs-btn-hover-bg: var(--k-wine-dark); --bs-btn-hover-border-color: var(--k-wine-dark); --bs-btn-active-bg: #6f0000; --bs-btn-active-border-color: #6f0000; --bs-btn-disabled-bg: var(--k-wine); --bs-btn-disabled-border-color: var(--k-wine); }
.btn-outline-primary { --bs-btn-color: var(--k-wine); --bs-btn-border-color: var(--k-wine); --bs-btn-hover-bg: var(--k-wine); --bs-btn-hover-border-color: var(--k-wine); --bs-btn-active-bg: var(--k-wine-dark); --bs-btn-active-border-color: var(--k-wine-dark); }
.btn-accent { background: var(--k-orange); color: #1d1d1d; border-color: var(--k-orange); font-weight: 600; }
.btn-accent:hover { background: #ec9800; color: #000; }
.text-primary { color: var(--k-wine) !important; }
.bg-primary { background-color: var(--k-wine) !important; }
.text-bg-primary { background-color: var(--k-wine) !important; color: #fff !important; }
.text-bg-orange { background: #fd7e14 !important; color: #fff !important; }
.text-bg-light { background: #eceae8 !important; color: #444 !important; }
.form-control:focus, .form-select:focus { border-color: #d98080; box-shadow: 0 0 0 .2rem rgba(168, 0, 0, .14); }
.form-check-input:checked { background-color: var(--k-wine); border-color: var(--k-wine); }
.page-link { color: var(--k-wine); }
.active > .page-link, .page-link.active { background-color: var(--k-wine); border-color: var(--k-wine); }
.nav-pills .nav-link.active { background: var(--k-wine); }
.nav-tabs .nav-link.active { color: var(--k-wine); font-weight: 600; border-bottom: 2px solid var(--k-wine); }
.nav-tabs .nav-link { color: #555; }
a { text-decoration: none; }

/* ---------- Structure ---------- */
.app { display: flex; min-height: 100vh; }
.sidebar { width: var(--sidebar-w); background: var(--k-black); color: #cfcac6; position: fixed; inset: 0 auto 0 0; display: flex; flex-direction: column; z-index: 1040; transition: transform .25s ease; }
.sidebar-brand { display: block; margin: 14px 14px 10px; background: #fff; border-radius: 14px; padding: 8px 10px; text-align: center; }
.sidebar-brand img { max-width: 100%; height: 52px; object-fit: contain; }
.sidebar-nav { flex: 1; overflow-y: auto; padding: 6px 10px 20px; }
.sidebar-nav .nav-link { display: flex; align-items: center; gap: 11px; width: 100%; color: #cfcac6; padding: 9px 12px; border-radius: 10px; font-size: .86rem; border: 0; background: none; text-align: left; margin-bottom: 2px; border-left: 3px solid transparent; }
.sidebar-nav .nav-link i { font-size: 1.05rem; width: 20px; text-align: center; }
.sidebar-nav .nav-link:hover { background: rgba(255, 255, 255, .06); color: #fff; }
.sidebar-nav .nav-link.active { background: rgba(255, 165, 0, .13); color: #fff; border-left-color: var(--k-orange); font-weight: 500; }
.nav-sub { display: none; padding-left: 30px; }
.nav-group.open .nav-sub { display: block; }
.nav-group.open .caret { transform: rotate(180deg); }
.caret { transition: transform .2s; font-size: .75rem !important; }
.nav-sub .nav-link { padding: 7px 12px; font-size: .82rem; }
.sidebar-foot { padding: 12px 18px; border-top: 1px solid rgba(255, 255, 255, .07); }
.sidebar-foot a { color: #a9a39e; }
.sidebar-foot a:hover { color: var(--k-orange); }
.main { flex: 1; margin-left: var(--sidebar-w); min-width: 0; display: flex; flex-direction: column; }
.topbar { position: sticky; top: 0; z-index: 1030; background: rgba(255, 255, 255, .94); backdrop-filter: blur(8px); border-bottom: 1px solid var(--k-border); display: flex; align-items: center; gap: 12px; padding: 10px 22px; }
.content { padding: 22px; flex: 1; }
.app-footer { padding: 12px 22px; border-top: 1px solid var(--k-border); }
.btn-icon { width: 38px; height: 38px; display: inline-flex; align-items: center; justify-content: center; border-radius: 10px; border: 1px solid var(--k-border); background: #fff; font-size: 1.1rem; }
.user-btn { display: flex; align-items: center; gap: 9px; border: 0; background: none; padding: 2px 6px; }
.avatar { width: 36px; height: 36px; border-radius: 50%; background: linear-gradient(135deg, var(--k-orange), var(--k-red)); color: #fff; display: inline-flex; align-items: center; justify-content: center; font-weight: 600; font-size: .8rem; flex-shrink: 0; }
.avatar-lg { width: 64px; height: 64px; font-size: 1.3rem; }
.notif-dot { position: absolute; top: 3px; right: 3px; min-width: 17px; height: 17px; border-radius: 9px; background: var(--k-red); color: #fff; font-size: .65rem; display: flex; align-items: center; justify-content: center; padding: 0 4px; }
.notif-menu { width: 360px; max-height: 470px; overflow-y: auto; padding: 0; }
.notif-item { display: flex; gap: 10px; padding: 10px 14px; border-bottom: 1px solid #f1eeec; color: inherit; }
.notif-item.unread { background: #fff8ec; }
.notif-item:hover { background: #f8f5f3; }
.notif-icon { width: 30px; height: 30px; border-radius: 8px; display: flex; align-items: center; justify-content: center; flex-shrink: 0; }

/* Recherche globale */
.global-search { position: relative; flex: 1; max-width: 520px; }
.global-search > i { position: absolute; left: 12px; top: 50%; transform: translateY(-50%); color: #999; }
.global-search input { width: 100%; border: 1px solid var(--k-border); background: #f8f6f4; border-radius: 10px; padding: 8px 12px 8px 36px; outline: none; }
.global-search input:focus { background: #fff; border-color: #d9a0a0; }
.search-results { display: none; position: absolute; top: 110%; left: 0; right: 0; background: #fff; border-radius: 12px; max-height: 420px; overflow-y: auto; z-index: 1050; border: 1px solid var(--k-border); }
.search-results.show { display: block; }
.search-results .sr-group { padding: 6px 14px 2px; font-size: .7rem; text-transform: uppercase; color: #999; letter-spacing: .05em; }
.search-results a { display: block; padding: 7px 14px; color: inherit; }
.search-results a:hover, .search-results a.focus { background: #fbf1f1; }

/* ---------- Composants ---------- */
.page-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 18px; }
.page-head h1 { font-size: 1.35rem; font-weight: 700; margin: 0; }
.page-head .subtitle { color: var(--k-muted); font-size: .83rem; }
.card { border: 1px solid var(--k-border); border-radius: 14px; box-shadow: 0 1px 2px rgba(0, 0, 0, .03); }
.card-header { background: #fff; border-bottom: 1px solid var(--k-border); padding: 12px 16px; font-weight: 600; border-radius: 14px 14px 0 0 !important; }
.min-w-0 { min-width: 0; }
.card.kpi { flex-direction: row; }
.kpi { padding: 16px; display: flex; gap: 14px; align-items: center; height: 100%; }
.kpi .ico { width: 46px; height: 46px; border-radius: 12px; display: flex; align-items: center; justify-content: center; font-size: 1.3rem; flex-shrink: 0; }
.kpi .val { font-size: 1.22rem; font-weight: 700; line-height: 1.15; }
.kpi .lbl { color: var(--k-muted); font-size: .78rem; }
.ico-wine { background: #fbe9e9; color: var(--k-wine); } .ico-orange { background: #fff3dc; color: #d98400; }
.ico-green { background: #e5f6ec; color: #198754; } .ico-blue { background: #e4eef6; color: var(--k-blue); }
.ico-red { background: #ffe8e8; color: #dc3545; } .ico-gray { background: #efedeb; color: #555; }
a.kpi-link { color: inherit; display: block; height: 100%; }
a.kpi-link:hover .card { border-color: #e2bcbc; }

/* Tables AJAX */
.ktable .toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; padding: 12px 14px; border-bottom: 1px solid var(--k-border); }
.ktable .toolbar .search { position: relative; min-width: 220px; flex: 1; max-width: 340px; }
.ktable .toolbar .search i { position: absolute; left: 10px; top: 50%; transform: translateY(-50%); color: #999; }
.ktable .toolbar .search input { padding-left: 32px; }
.ktable .filters { display: flex; flex-wrap: wrap; gap: 8px; }
.ktable .filters .form-select, .ktable .filters .form-control { width: auto; min-width: 140px; }
.ktable table { margin: 0; }
.ktable thead th { background: #faf8f6; font-size: .75rem; text-transform: uppercase; letter-spacing: .03em; color: #6a645f; font-weight: 600; white-space: nowrap; border-bottom: 1px solid var(--k-border); }
.ktable th[data-sort] { cursor: pointer; user-select: none; }
.ktable th[data-sort]:hover { color: var(--k-wine); }
.ktable th.sorted-asc::after { content: ' ▲'; font-size: .65rem; color: var(--k-wine); }
.ktable th.sorted-desc::after { content: ' ▼'; font-size: .65rem; color: var(--k-wine); }
.ktable td { vertical-align: middle; }
.ktable tbody tr:hover { background: #fdfaf8; }
.ktable .footer { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: 10px; padding: 10px 14px; border-top: 1px solid var(--k-border); }
.ktable .loading { opacity: .5; pointer-events: none; }
.ktable .col-hidden { display: none !important; }
.ktable .empty { padding: 40px 10px; text-align: center; color: #999; }
.actions { white-space: nowrap; text-align: right; }
.actions .btn { padding: .22rem .45rem; }
.badge-status { font-weight: 500; font-size: .72rem; padding: .38em .75em; }
.money { white-space: nowrap; font-variant-numeric: tabular-nums; }
.thumb { width: 42px; height: 42px; border-radius: 8px; object-fit: cover; background: #f1eeec; }
.thumb-lg { width: 100%; max-width: 260px; aspect-ratio: 1; object-fit: cover; border-radius: 12px; }
.stat-line { display: flex; justify-content: space-between; padding: 6px 0; border-bottom: 1px dashed #eee6e0; }
.stat-line:last-child { border-bottom: 0; }
.dl-grid { display: grid; grid-template-columns: 160px 1fr; gap: 6px 12px; }
.dl-grid dt { color: var(--k-muted); font-weight: 400; }
.dl-grid dd { margin: 0; }
.timeline { position: relative; padding-left: 22px; }
.timeline::before { content: ''; position: absolute; left: 7px; top: 4px; bottom: 4px; width: 2px; background: #eee3dc; }
.timeline .tl-item { position: relative; margin-bottom: 12px; }
.timeline .tl-item::before { content: ''; position: absolute; left: -19px; top: 5px; width: 10px; height: 10px; border-radius: 50%; background: var(--k-orange); border: 2px solid #fff; box-shadow: 0 0 0 1px #f0c37a; }

/* Modal */
.modal-content { border: 0; border-radius: 16px; }
.modal-header { border-bottom: 1px solid var(--k-border); }
.modal-header .modal-title { font-weight: 600; font-size: 1.05rem; }
.modal-footer { border-top: 1px solid var(--k-border); }
.form-label { font-weight: 500; font-size: .8rem; margin-bottom: .25rem; color: #4a4541; }
.req::after { content: ' *'; color: var(--k-red); }
.calc-box { background: #fbf7f4; border: 1px solid #efe4dc; border-radius: 12px; padding: 14px; }
.calc-total { font-size: 1.25rem; font-weight: 700; color: var(--k-wine); }
.item-row .btn-remove { opacity: .6; } .item-row .btn-remove:hover { opacity: 1; }

/* Calendrier des échéances */
.cal { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; }
.cal .dow { text-align: center; font-size: .72rem; color: #888; padding: 4px; text-transform: uppercase; }
.cal .day { min-height: 92px; background: #fff; border: 1px solid var(--k-border); border-radius: 8px; padding: 4px 5px; font-size: .72rem; overflow: hidden; }
.cal .day.out { background: #faf8f6; color: #bbb; }
.cal .day.today { border-color: var(--k-orange); box-shadow: inset 0 0 0 1px var(--k-orange); }
.cal .day .num { font-weight: 600; font-size: .78rem; }
.cal .ev { display: block; border-radius: 4px; padding: 1px 4px; margin-top: 2px; color: #fff; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ev-a_venir, .ev-reechelonnee { background: #6c757d; } .ev-aujourdhui { background: #e0a800; } .ev-payee { background: #198754; }
.ev-partielle { background: #fd7e14; } .ev-en_retard { background: #dc3545; }

/* Divers */
.toast { border: 0; border-radius: 12px; }
.ajax-loader { position: fixed; inset: 0; background: rgba(255, 255, 255, .45); display: none; align-items: center; justify-content: center; z-index: 2000; }
.ajax-loader.show { display: flex; }
.login-page { min-height: 100vh; display: flex; align-items: center; justify-content: center; background: radial-gradient(circle at 20% 10%, #ffcf7a 0, transparent 40%), radial-gradient(circle at 90% 90%, #ff8a8a 0, transparent 40%), linear-gradient(135deg, #fff3e0, #fbe3e3); padding: 20px; }
.login-card { width: 100%; max-width: 410px; border-radius: 20px; border: 0; box-shadow: 0 20px 50px rgba(120, 30, 0, .15); }
.login-card .logo { height: 64px; }
.perm-table td, .perm-table th { font-size: .8rem; }
.chart-box { position: relative; height: 280px; }
.empty-state { text-align: center; padding: 30px; color: #999; }
.empty-state i { font-size: 2rem; display: block; margin-bottom: 6px; }
@media (max-width: 991.98px) {
    .sidebar { transform: translateX(-100%); }
    .sidebar.show { transform: none; box-shadow: 0 0 40px rgba(0, 0, 0, .4); }
    .sidebar-backdrop.show { position: fixed; inset: 0; background: rgba(0, 0, 0, .35); z-index: 1035; }
    .main { margin-left: 0; }
    .content { padding: 14px; }
    .topbar { padding: 8px 12px; }
    .dl-grid { grid-template-columns: 120px 1fr; }
    .cal .day { min-height: 60px; font-size: .62rem; }
}
@media (max-width: 575.98px) {
    .notif-menu { width: 92vw; }
    .ktable .toolbar .search { max-width: none; }
    .ktable .filters .form-select, .ktable .filters .form-control { min-width: 0; flex: 1 1 45%; }
    .page-head h1 { font-size: 1.15rem; }
}
@media print {
    .sidebar, .topbar, .app-footer, .no-print, .ktable .toolbar, .ktable .footer { display: none !important; }
    .main { margin: 0; }
    .card { border: 0; box-shadow: none; }
}
/* Modals superposés (confirmation au-dessus d'un formulaire) */
#confirmModal { z-index: 1070; }
.modal-backdrop.show + .modal-backdrop.show { z-index: 1065; }
.modal-content > form { display: flex; flex-direction: column; min-height: 0; max-height: 100%; overflow: hidden; }
.modal-dialog-scrollable .modal-content > form > .modal-body { overflow-y: auto; }
