:root {
    --primary: #163f80;
    --primary-light: #1f5da3;
    --primary-dark: #20abe2;
    --accent: #2f619e;
    --bg: #eae8ee;
    --card-bg: #ffffff;
    --text: #100303;
    --text-muted: #5c6871;
    --danger: #d64545;
    --success: #163b80;
    --warning: #e6a417;
    --border: #dcefe1;
    --radius: 14px;
    --shadow: 0 4px 20px rgba(22, 128, 60, 0.08);
    --shadow-lg: 0 12px 40px rgba(22, 128, 60, 0.14);
}

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

body {
    font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
    background: #e7e8e9;
    color: var(--text);
    line-height: 1.5;
}

a { text-decoration: none; color: inherit; }
ul { list-style: none; }

.container { max-width: 1200px; margin: 0 auto; padding: 0 24px; }

/* ---------- Boutons ---------- */
.btn {
    display: inline-flex; align-items: center; justify-content: center;
    gap: 8px;
    padding: 12px 24px;
    border-radius: 10px;
    border: none;
    font-weight: 600;
    font-size: 15px;
    cursor: pointer;
    transition: all .2s ease;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); transform: translateY(-1px); }
.btn-accent { background: var(--accent); color: #2b2005; }
.btn-accent:hover { filter: brightness(1.05); }
.btn-outline { background: transparent; border: 2px solid var(--primary); color: var(--primary); }
.btn-outline:hover { background: var(--primary); color: #fff; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-block { width: 100%; }
.btn-sm { padding: 8px 14px; font-size: 13px; }

/* ---------- Cartes ---------- */
.card {
    background: var(--card-bg);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 28px;
    border: 1px solid var(--border);
}

/* ---------- Formulaires ---------- */
.form-group { margin-bottom: 18px; }
label { display: block; font-weight: 600; margin-bottom: 6px; font-size: 14px; color: var(--text); }
input, select, textarea {
    width: 100%;
    padding: 12px 14px;
    border: 1.5px solid var(--border);
    border-radius: 10px;
    font-size: 15px;
    background: #fbfdfc;
    transition: border-color .15s;
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--primary-light); background: #fff; }
.form-hint { font-size: 12px; color: var(--text-muted); margin-top: 4px; }

/* ---------- Landing / Accueil ---------- */
.hero {
    position: relative;
    min-height: 100vh;
    background: linear-gradient(135deg, var(--primary-dark), var(--primary) 60%, var(--primary-light));
    color: #fff;
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    text-align: center;
    padding: 40px 20px;
}
.hero h1 { font-size: 42px; margin-bottom: 12px; }
.hero p { font-size: 18px; opacity: .9; max-width: 600px; margin-bottom: 40px; }
.access-choices {
    display: flex; gap: 24px; flex-wrap: wrap; justify-content: center;
}
.access-card {
    background: rgba(255,255,255,0.08);
    backdrop-filter: blur(6px);
    border: 1.5px solid rgba(255,255,255,0.25);
    border-radius: var(--radius);
    padding: 32px 40px;
    width: 260px;
    transition: transform .2s, background .2s;
    cursor: pointer;
}
.access-card:hover { transform: translateY(-6px); background: rgba(255,255,255,0.16); }
.access-card .icon { font-size: 40px; margin-bottom: 14px; }
.access-card h3 { font-size: 20px; margin-bottom: 8px; }
.access-card p { font-size: 13px; opacity: .85; margin-bottom: 0; }
.super-admin-link { margin-top: 50px; font-size: 13px; opacity: .6; }
.super-admin-link:hover { opacity: 1; }

/* ---------- Sélecteur de langue ---------- */
.lang-switcher { display: inline-block; }
.lang-switcher select {
    width: auto;
    padding: 8px 12px;
    border-radius: 8px;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
}
.hero .lang-switcher { position: absolute; top: 20px; right: 24px; }
.hero .lang-switcher select {
    background: rgba(255,255,255,0.12);
    border: 1.5px solid rgba(255,255,255,0.35);
    color: #fff;
}
.hero .lang-switcher select option { color: #100303; }
.topbar .lang-switcher select {
    background: #fff;
    border: 1.5px solid var(--border);
    color: var(--text);
}

/* ---------- Dashboard ---------- */
.topbar {
    background: #f3f4f3; border-bottom: 1px solid var(--border);
    padding: 16px 24px; display: flex; align-items: center; justify-content: space-between;
    position: sticky; top: 0; z-index: 10;
    flex-wrap: wrap; row-gap: 10px;
}
.topbar .brand { font-weight: 800; font-size: 20px; color: var(--primary); }
/* Le bloc d'actions (liens + sélecteur de langue) est toujours le 2e enfant direct
   du topbar dans toutes les pages — on le rend pliable sans avoir à toucher
   chaque fichier PHP individuellement. */
.topbar > div:last-child { flex-wrap: wrap; row-gap: 8px; }
.layout { display: flex; min-height: calc(100vh - 66px); align-items: flex-start; }
.sidebar {
    width: 240px; background: #fff; border-right: 1px solid var(--border); padding: 20px 0;
    position: sticky; top: 66px; height: calc(100vh - 66px); overflow-y: auto; flex-shrink: 0;
}
.sidebar a { display: block; padding: 12px 24px; font-size: 14px; color: var(--text-muted); font-weight: 600; }
.sidebar a:hover, .sidebar a.active { background: #eef7f4; color: var(--primary); border-right: 3px solid var(--primary); }
.main { flex: 1; padding: 28px; }

.health-banner {
    background: linear-gradient(120deg, var(--primary), var(--primary-light));
    color: #dee3e5; border-radius: var(--radius); padding: 24px 28px;
    display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 16px;
    margin-bottom: 28px;
}
.health-banner .metric { text-align: center; }
.health-banner .metric .value { font-size: 26px; font-weight: 800; }
.health-banner .metric .label { font-size: 12px; opacity: .85; }

.modules-grid {
    display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 18px;
}
.module-card {
    background: #63a7eb; border: 1px solid var(--border); border-radius: var(--radius);
    padding: 22px 18px; text-align: center; transition: all .2s;
}
.module-card:hover { box-shadow: var(--shadow-lg); transform: translateY(-4px); border-color: var(--primary-light); }
.module-card .icon { font-size: 32px; margin-bottom: 10px; display: block; }
.module-card .name { font-weight: 700; font-size: 14px; }

/* ---------- Tableaux ---------- */
table { width: 100%; border-collapse: collapse; background: #fff; border-radius: var(--radius); overflow: hidden; }
th, td { padding: 12px 16px; text-align: left; font-size: 14px; border-bottom: 1px solid var(--border); }
th { background: #f0f5f3; font-weight: 700; color: var(--text-muted); text-transform: uppercase; font-size: 11px; letter-spacing: .5px; }
tr:hover td { background: #fafcfb; }

.badge { padding: 4px 10px; border-radius: 20px; font-size: 12px; font-weight: 700; }
.badge-success { background: #e3f7ec; color: var(--success); }
.badge-warning { background: #fdf1dc; color: var(--warning); }
.badge-danger { background: #fbe6e6; color: var(--danger); }
.badge-muted { background: #eef1f0; color: var(--text-muted); }

.alert { padding: 14px 18px; border-radius: 10px; margin-bottom: 20px; font-size: 14px; font-weight: 600; }
.alert-success { background: #e3f7ec; color: var(--success); border: 1px solid #b9e8cd; }
.alert-danger { background: #fbe6e6; color: var(--danger); border: 1px solid #f2bcbc; }
.alert-warning { background: #fdf1dc; color: #a4740c; border: 1px solid #f4dba3; }

.auth-wrapper {
    min-height: 100vh; display: flex; align-items: center; justify-content: center;
    background: linear-gradient(135deg, var(--primary-dark), var(--primary));
    padding: 20px;
}
.auth-box { background: #fff; border-radius: var(--radius); padding: 40px; width: 100%; max-width: 420px; box-shadow: var(--shadow-lg); }
.auth-box h2 { margin-bottom: 6px; }
.auth-box .subtitle { color: var(--text-muted); font-size: 14px; margin-bottom: 26px; }

/* ---------- Paiement ---------- */
.payment-methods { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px,1fr)); gap: 16px; margin: 24px 0; }
.payment-method {
    border: 2px solid var(--border); border-radius: var(--radius); padding: 20px;
    text-align: center; cursor: pointer; transition: all .15s; background: #fff;
}
.payment-method.selected, .payment-method:hover { border-color: var(--primary); background: #f2f9f6; }
.payment-method .logo { font-size: 30px; margin-bottom: 8px; }
.payment-method .name { font-weight: 700; font-size: 14px; }

/* ---------- Reçu imprimable ---------- */
.receipt { max-width: 520px; margin: 30px auto; background: #fff; border: 1px dashed #999; padding: 30px; }
.receipt h2 { text-align: center; margin-bottom: 4px; }
.receipt .sub { text-align: center; color: var(--text-muted); font-size: 13px; margin-bottom: 20px; }
.receipt table td { border: none; padding: 6px 0; }
.receipt .total-row { font-weight: 800; font-size: 18px; border-top: 2px solid #000; padding-top: 10px; }

.receipt-formatted { max-width: 380px; padding: 32px 28px; border: 1px solid var(--border); box-shadow: var(--shadow-lg); }
.receipt-letterhead { text-align: center; margin-bottom: 18px; padding-bottom: 16px; border-bottom: 2px solid var(--primary); }
.receipt-logo { max-height: 70px; max-width: 220px; margin-bottom: 10px; object-fit: contain; }
.receipt-entreprise-nom { font-size: 19px; font-weight: 800; color: var(--primary-dark); }
.receipt-entreprise-detail { font-size: 12px; color: var(--text-muted); margin-top: 2px; }
.receipt-titre { text-align: center; font-size: 13px; font-weight: 700; letter-spacing: 1.5px; color: var(--text-muted); margin-bottom: 16px; }
.receipt-footer { margin-top: 24px; font-size: 11px; text-align: center; color: var(--text-muted); line-height: 1.5; }

@media print {
    .no-print { display: none !important; }
    body { background: #fff !important; }
    @page { size: auto; margin: 12mm; }
    .receipt-formatted { box-shadow: none; border: none; max-width: 100%; margin: 0; padding: 0; }
}


/* ---------- Plan Comptable — arbre pliable ---------- */
.account-tree { background: #fff; border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.account-node, .account-leaf { border-bottom: 1px solid var(--border); }
.account-node:last-child, .account-leaf:last-child { border-bottom: none; }
.account-node > summary {
    cursor: pointer; list-style: none; display: flex; align-items: center; gap: 14px;
    padding: 12px 16px; background: #fff; transition: background .15s;
}
.account-node > summary:hover { background: #f2f9f4; }
.account-node > summary::-webkit-details-marker { display: none; }
.account-node > summary::before { content: '▸'; color: var(--primary); font-size: 13px; transition: transform .15s; flex-shrink: 0; }
.account-node[open] > summary::before { transform: rotate(90deg); }
.account-children { padding-left: 28px; border-left: 2px solid var(--border); margin-left: 22px; }
.account-leaf { display: flex; align-items: center; gap: 14px; padding: 12px 16px 12px 34px; }
.account-row-code { width: 70px; flex-shrink: 0; }
.account-row-nom { flex: 1; font-weight: 600; }
.account-row-classe { width: 90px; flex-shrink: 0; }
.account-row-solde { width: 110px; text-align: right; flex-shrink: 0; }
.account-row-actions { flex-shrink: 0; }
.account-edit-box { padding: 14px 16px; background: #f7faf9; border-top: 1px dashed var(--border); }

/* ---------- Vente — barre de recherche de produits ---------- */
.produit-search-wrap { position: relative; margin-bottom: 14px; }
.produit-search-results {
    display: none;
    position: absolute;
    top: calc(100% + 4px);
    left: 0; right: 0;
    background: #fff;
    border: 1.5px solid var(--border);
    border-radius: 10px;
    box-shadow: var(--shadow-lg);
    max-height: 320px;
    overflow-y: auto;
    z-index: 20;
}
.produit-search-results.open { display: block; }
.produit-search-item {
    display: flex; align-items: center; justify-content: space-between; gap: 12px;
    padding: 10px 14px; cursor: pointer; font-size: 14px; border-bottom: 1px solid var(--border);
}
.produit-search-item:last-child { border-bottom: none; }
.produit-search-item:hover { background: #f2f9f4; }
.produit-search-item.disabled { opacity: .5; cursor: not-allowed; }
.produit-search-nom { font-weight: 600; }
.produit-search-meta { font-size: 12px; color: var(--text-muted); white-space: nowrap; }
.produit-search-empty { padding: 14px; text-align: center; color: var(--text-muted); font-size: 13px; }

/* ---------- Vente — panier multi-produits ---------- */
.cart-table th, .cart-table td { padding: 8px 10px; }
.cart-remove { color: var(--danger); cursor: pointer; font-weight: 700; }
.paiement-toggle { display: flex; gap: 12px; }
.paiement-toggle label {
    flex: 1; text-align: center; border: 2px solid var(--border); border-radius: 10px;
    padding: 14px; font-weight: 700; cursor: pointer; transition: all .15s;
}
.paiement-toggle input { display: none; }
.paiement-toggle input:checked + span { color: var(--primary); }
.paiement-toggle label:has(input:checked) { border-color: var(--primary); background: #f2f9f4; }

/* ---------- Vente — panneau de reçu latéral (sans redirection) ---------- */
.vente-layout { display: flex; gap: 24px; align-items: flex-start; }
.vente-layout .vente-principal { flex: 2; min-width: 0; }
.vente-layout .vente-recu-panel { flex: 1; min-width: 300px; position: sticky; top: 90px; }
.receipt-panel-inner { background: #fff; border: 2px solid var(--primary); border-radius: var(--radius); padding: 24px; }
.receipt-panel-inner h3 { text-align: center; margin-bottom: 2px; }
.receipt-panel-inner .sub { text-align: center; color: var(--text-muted); font-size: 13px; margin-bottom: 16px; }
.receipt-panel-inner table td { border: none; padding: 5px 0; font-size: 14px; }
.receipt-panel-inner .total-row { font-weight: 800; font-size: 17px; border-top: 2px solid #000; padding-top: 8px; }

@media print {
    body.print-receipt-mode * { visibility: hidden; }
    body.print-receipt-mode #receiptPrintArea, body.print-receipt-mode #receiptPrintArea * { visibility: visible; }
    body.print-receipt-mode #receiptPrintArea { position: absolute; left: 0; top: 0; width: 100%; padding: 0; }
}

@media (max-width: 900px) {
    .vente-layout { flex-direction: column; }
    .vente-layout .vente-recu-panel { position: static; width: 100%; }

    /* ---------- Tableaux : défilement horizontal plutôt que débordement ----------
       Cible uniquement les cartes qui contiennent un vrai tableau de données ;
       la carte "Nouvelle vente" contient aussi la recherche de produits dont le
       menu déroulant est positionné en absolu — on l'exclut explicitement pour
       ne jamais le couper. */
    .card:has(table) { overflow-x: auto; -webkit-overflow-scrolling: touch; }
    .card table { min-width: 560px; }
    .card:has(.produit-search-wrap) { overflow-x: visible; }
    .card:has(.produit-search-wrap) table { min-width: 0; }
}

@media (max-width: 768px) {
    .layout { flex-direction: column; }
    .sidebar { width: 100%; position: static; height: auto; }
    .hero h1 { font-size: 30px; }

    .container { padding: 0 14px; }
    .main { padding: 18px 0; }
    .topbar { padding: 12px 16px; }
    .topbar .brand { font-size: 16px; }
    .card { padding: 18px; }
    h2 { font-size: 20px; }
    h3 { font-size: 17px; }

    /* Formulaires de création/édition : un champ par ligne, plus lisible qu'une
       rangée compressée à plusieurs colonnes de largeur minimale. */
    .form-group { min-width: 0 !important; flex-basis: 100% !important; }
}

@media (max-width: 600px) {
    /* ---------- Plan comptable : l'arbre de comptes s'empile au lieu de se
       comprimer (code, nom, classe, solde(s), actions sur plusieurs lignes) ---------- */
    .account-node > summary, .account-leaf {
        flex-wrap: wrap;
        row-gap: 4px;
        padding: 10px 12px;
    }
    .account-leaf { padding-left: 20px; }
    .account-row-nom { flex: 1 1 55%; }
    .account-row-classe { width: auto; }
    .account-row-solde { width: auto; text-align: left; font-size: 13px; }
    .account-children { padding-left: 12px; margin-left: 10px; }
}

@media (max-width: 480px) {
    .hero h1 { font-size: 24px; }
    .hero p { font-size: 15px; }
    .access-choices { gap: 16px; }
    .access-card { width: 100%; max-width: 280px; padding: 26px 30px; }
    .auth-box { padding: 26px 20px; }
    .btn { padding: 10px 18px; font-size: 14px; }
    .modules-grid { grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 12px; }

    /* Badge flottant de session employé : plus discret sur petit écran pour ne
       jamais chevaucher un bouton ou un champ de formulaire. */
    #emp-session-widget { bottom: 8px; right: 8px; min-width: 90px; padding: 8px 10px; }
    #emp-session-widget #emp-session-time { font-size: 14px; }
}
