:root {
    --accent: #d32f2f;
    --accent-dark: #b71c1c;
    --sidebar-bg: #1c1e26;
    --sidebar-text: #b7bac5;
    --bg: #f4f5f7;
    --text: #1d2327;
    --text-muted: #6b7280;
    --border: #eef0f2;
    --sidebar-width: 248px;
    --sidebar-collapsed-width: 72px;
}

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

body {
    font-family: -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
}

a { text-decoration: none; color: inherit; }

.app-shell {
    display: flex;
    min-height: 100vh;
}

/* ---------- Sidebar ---------- */
.sidebar {
    width: var(--sidebar-width);
    flex-shrink: 0;
    background: var(--sidebar-bg);
    display: flex;
    flex-direction: column;
    padding: 20px 0;
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    overflow-y: auto;
    z-index: 1000;
    transition: transform 0.25s ease;
}

.sidebar-brand {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 0 20px 22px 20px;
    margin-bottom: 8px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}

.sidebar-brand span {
    color: #ffffff;
    font-size: 19px;
    font-weight: 700;
}

.sidebar-link {
    display: flex;
    align-items: center;
    gap: 12px;
    margin: 2px 12px;
    padding: 10px 12px;
    border-radius: 8px;
    color: var(--sidebar-text);
    font-size: 14px;
    font-weight: 600;
}

.sidebar-link:hover {
    background: rgba(255, 255, 255, 0.06);
    color: #ffffff;
}

.sidebar-link.active {
    background: var(--accent);
    color: #ffffff;
}

.sidebar-spacer { flex-grow: 1; }

.sidebar-divider {
    height: 1px;
    background: rgba(255, 255, 255, 0.08);
    margin: 8px 20px;
}

.sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.4);
    z-index: 999;
}

.sidebar-btn-reset {
    width: calc(100% - 24px);
    background: none;
    border: none;
    text-align: left;
    cursor: pointer;
    font-family: inherit;
}

.sidebar-group-toggle .chevron {
    margin-left: auto;
    flex-shrink: 0;
    transition: transform 0.2s ease;
}

.sidebar-group-toggle.open .chevron {
    transform: rotate(180deg);
}

.sidebar-submenu {
    display: none;
    flex-direction: column;
    margin: 2px 12px 4px 34px;
    gap: 2px;
}

.sidebar-submenu.open { display: flex; }

.sidebar-sublink {
    padding: 8px 10px;
    border-radius: 6px;
    color: var(--sidebar-text);
    font-size: 13px;
    font-weight: 600;
}

.sidebar-sublink:hover {
    background: rgba(255, 255, 255, 0.06);
    color: #ffffff;
}

.sidebar-sublink.active {
    background: var(--accent);
    color: #ffffff;
}

/* Mode réduit (icônes seules) : desktop uniquement, le menu mobile reste en overlay plein écran */
@media (min-width: 783px) {
    body.sidebar-collapsed .sidebar { width: var(--sidebar-collapsed-width); }
    body.sidebar-collapsed .main-wrapper { margin-left: var(--sidebar-collapsed-width); }

    body.sidebar-collapsed .sidebar-brand,
    body.sidebar-collapsed .sidebar-link {
        justify-content: center;
        gap: 0;
        padding-left: 0;
        padding-right: 0;
    }

    body.sidebar-collapsed .sidebar-brand span,
    body.sidebar-collapsed .sidebar-link span {
        display: none;
    }

    body.sidebar-collapsed .sidebar-collapse-btn svg {
        transform: rotate(180deg);
    }

    body.sidebar-collapsed .sidebar-group-toggle .chevron,
    body.sidebar-collapsed .sidebar-submenu {
        display: none;
    }
}

/* ---------- Main wrapper ---------- */
.main-wrapper {
    flex-grow: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    margin-left: var(--sidebar-width);
}

.topbar {
    height: 68px;
    flex-shrink: 0;
    background: linear-gradient(135deg, var(--accent), var(--accent-dark));
    display: flex;
    align-items: center;
    padding: 0 28px;
    gap: 16px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.12);
    position: sticky;
    top: 0;
    z-index: 500;
}

.burger-btn {
    display: none;
    background: none;
    border: none;
    cursor: pointer;
    padding: 4px;
}

.search-box {
    flex-grow: 1;
    display: flex;
    align-items: center;
    background: rgba(255, 255, 255, 0.16);
    border-radius: 8px;
    padding: 8px 14px;
    max-width: 360px;
    gap: 8px;
}

.search-box span {
    color: rgba(255, 255, 255, 0.85);
    font-size: 13px;
}

.topbar-spacer { flex-grow: 1; }

.topbar-username {
    color: #ffffff;
    font-size: 14px;
    font-weight: 600;
    white-space: nowrap;
}

.topbar-avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.9);
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--accent-dark);
    font-weight: 700;
    font-size: 14px;
    flex-shrink: 0;
}

/* ---------- Content ---------- */
.content {
    flex-grow: 1;
    padding: 16px 32px;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.page-heading {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    flex-wrap: wrap;
}

.page-heading h1 {
    font-size: 23px;
    font-weight: 700;
    color: var(--text);
    margin-bottom: 4px;
}

.page-heading p {
    font-size: 13px;
    color: var(--text-muted);
}

.alert {
    position: fixed;
    top: 20px;
    right: 20px;
    z-index: 2000;
    padding: 12px 16px;
    border-radius: 8px;
    font-size: 13.5px;
    font-weight: 600;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
    opacity: 1;
    transform: translateY(0);
    transition: opacity 0.4s ease, transform 0.4s ease;
}

.alert.alert-hide {
    opacity: 0;
    transform: translateY(-8px);
}

.alert-success { background: #e6f4ea; color: #1a7f4b; }
.alert-error { background: #fdecea; color: #b71c1c; }

.stats-grid {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 16px;
}

.two-col-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
    align-items: start;
}

.inline-form {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    padding: 12px 16px;
    border-bottom: 1px solid var(--border);
}

.inline-form input[type="text"],
.inline-form select {
    padding: 7px 10px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 13px;
    font-family: inherit;
    color: var(--text);
}

.inline-form input[type="text"] { flex-grow: 1; min-width: 140px; }

.form-lock { border: 0; padding: 0; margin: 0; min-width: 0; }

.stat-card {
    background: #ffffff;
    border-radius: 10px;
    padding: 18px 20px;
    text-align: center;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}

.stat-card .label {
    font-size: 12px;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: .03em;
}

.stat-card .value {
    font-size: 26px;
    font-weight: 700;
    color: var(--text);
    margin-top: 6px;
}

/* ---------- Card / Table ---------- */
.card {
    background: #ffffff;
    border-radius: 10px;
    overflow: hidden;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}

.card-toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 12px 16px;
    flex-wrap: wrap;
}

.card-toolbar .search-input {
    flex-grow: 1;
    max-width: 320px;
    padding: 9px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 13.5px;
}

.filters-form {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 10px;
    flex-grow: 1;
}

.filters-form select {
    padding: 9px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 13.5px;
    color: var(--text);
    background: #ffffff;
}

.filters-right {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 10px;
    margin-left: auto;
    justify-content: flex-end;
}

.filters-right input[type="date"] {
    padding: 8px 10px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 13.5px;
    font-family: inherit;
}

.filters-check {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 13.5px;
    color: var(--text-muted);
    white-space: nowrap;
}

.btn {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 9px 16px;
    border-radius: 8px;
    border: none;
    font-size: 13.5px;
    font-weight: 600;
    cursor: pointer;
}

.btn-primary { background: var(--accent); color: #ffffff; }
.btn-primary:hover { background: var(--accent-dark); }

.btn-secondary { background: #eef0f2; color: var(--text); }
.btn-secondary:hover { background: #e2e5e9; }

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

table.data-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 13.5px;
}

table.data-table thead tr { background: var(--sidebar-bg); }

table.data-table th {
    text-align: left;
    padding: 8px 16px;
    color: #ffffff;
    font-weight: 600;
    white-space: nowrap;
}

table.data-table td {
    padding: 7px 16px;
    color: #374151;
    border-bottom: 1px solid var(--border);
}

table.data-table td.strong { color: var(--text); font-weight: 600; white-space: nowrap; min-width: 84px; }

table.data-table tbody tr:hover { background: #fafafa; }

.actions-cell { display: flex; gap: 6px; }

/* ---------- Bon de livraison ---------- */
.badge {
    display: inline-block;
    padding: 2px 10px;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 600;
}
.badge-brouillon { background: #fff4e0; color: #b26a00; }
.badge-valide { background: #e6f4ea; color: #1a7f4b; }
.badge-facture { background: #e8f0fe; color: #1a4fa3; margin-left: 4px; }
.badge-envoye { background: #e8f0fe; color: #1a4fa3; }
.badge-accepte { background: #e6f4ea; color: #1a7f4b; }
.badge-refuse { background: #fdecea; color: #b71c1c; }
.badge-impayee { background: #fdecea; color: #b71c1c; }
.badge-partielle { background: #fff4e0; color: #b26a00; }
.badge-payee { background: #e6f4ea; color: #1a7f4b; }

.autocomplete { position: relative; width: 100%; }
.autocomplete input[type="text"] { width: 100%; }

.autocomplete-list {
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    z-index: 300;
    background: #ffffff;
    border: 1px solid var(--border);
    border-radius: 8px;
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.12);
    max-height: 260px;
    overflow-y: auto;
    min-width: 260px;
}

.autocomplete-item {
    padding: 8px 12px;
    font-size: 13px;
    cursor: pointer;
    border-bottom: 1px solid var(--border);
    text-align: left;
}
.autocomplete-item:hover { background: #f4f5f7; }
.autocomplete-item small { display: block; color: var(--text-muted); font-size: 12px; }

.lines-table input[type="text"],
.lines-table input[type="number"] {
    padding: 6px 8px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 13px;
    font-family: inherit;
    width: 100%;
    min-width: 64px;
}
.lines-table td { vertical-align: middle; }
.lines-table.no-stock th:nth-child(2),
.lines-table.no-stock td:nth-child(2) { display: none; }
.lines-table tr.line-warn { background: #fdecea; }
.lines-table tr.line-warn .l-stock { color: var(--accent-dark); font-weight: 700; }
.lines-table th:nth-child(7) { text-align: right; }
.lines-table td.l-total {
    text-align: right;
    white-space: nowrap;
    font-weight: 700;
    color: var(--text);
    background: #f4f5f7;
    min-width: 110px;
}
.table-wrap:has(.lines-table) { overflow: visible; }

.bl-totals {
    width: 200px;
    max-width: 100%;
    margin: 16px 0 0 auto;
    border: 2px solid var(--sidebar-bg);
    border-radius: 8px;
    overflow: hidden;
    background: #ffffff;
}
.bl-totals > div {
    display: flex;
    justify-content: space-between;
    gap: 16px;
    padding: 8px 12px;
    font-size: 13px;
    color: var(--text-muted);
    border-bottom: 1px solid var(--border);
}
.bl-totals strong { color: var(--text); white-space: nowrap; text-align: right; }
.bl-totals > .bl-total-ttc {
    background: #dfe2e6;
    border-top: 2px solid var(--sidebar-bg);
    border-bottom: none;
    padding: 9px 12px;
    font-size: 14px;
    color: var(--text);
    font-weight: 700;
}

.rg-summary {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 14px;
    margin-top: 14px;
}
.rg-actions { display: flex; gap: 8px; flex-wrap: wrap; }
#rgTable input.rg-aff {
    padding: 6px 8px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 13px;
    width: 130px;
    text-align: right;
}
#rgTable input.rg-aff.rg-over { border-color: var(--accent); background: #fdecea; }

.bulk-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 10px;
    padding: 8px 16px;
    background: #f9fafb;
    border-top: 1px solid var(--border);
    border-bottom: 1px solid var(--border);
}
.bulk-info { font-size: 13px; color: var(--text-muted); }
.bulk-info.bulk-error { color: var(--accent-dark); font-weight: 600; }
.bulk-bar .btn[disabled] { opacity: .45; cursor: not-allowed; }
.cell-select input { width: 16px; height: 16px; cursor: pointer; }

.bl-group-row td {
    background: #eef1f5;
    font-weight: 700;
    color: var(--text);
    border-top: 2px solid var(--sidebar-bg);
}

.pagination {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 10px;
    padding: 12px 16px;
    border-top: 1px solid var(--border);
}

.pagination-info { font-size: 13px; color: var(--text-muted); }
.pagination-links { display: flex; gap: 6px; flex-wrap: wrap; }

.icon-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    border-radius: 6px;
    border: none;
    cursor: pointer;
}

.icon-btn.print { background: #4b5563; }
.icon-btn.transfer { background: #1a7f4b; }
.icon-btn.credit { background: #b26a00; }
.icon-btn.regl { background: #0f766e; }
.icon-btn.edit { background: #2271b1; }
.icon-btn.delete { background: #d63638; }

.empty-state {
    padding: 40px 20px;
    text-align: center;
    color: var(--text-muted);
    font-size: 14px;
}

/* ---------- Form ---------- */
.form-card {
    background: #ffffff;
    border-radius: 10px;
    padding: 16px 20px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}

.tabs {
    display: flex;
    gap: 4px;
    border-bottom: 1px solid var(--border);
    margin-bottom: 16px;
}

.tab-btn {
    padding: 10px 18px;
    border: none;
    background: none;
    font-size: 13.5px;
    font-weight: 600;
    color: var(--text-muted);
    cursor: pointer;
    border-bottom: 2px solid transparent;
    margin-bottom: -1px;
}

.tab-btn:hover { color: var(--text); }

.tab-btn.active {
    color: var(--accent);
    border-bottom-color: var(--accent);
}

.tab-panel { display: none; }
.tab-panel.active { display: block; }

.form-section-title {
    font-size: 13px;
    font-weight: 700;
    color: var(--text);
    margin: 14px 0 10px 0;
    padding-bottom: 6px;
    border-bottom: 1px solid var(--border);
}

.form-section-title:first-child { margin-top: 0; }

.form-grid {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 10px 14px;
}

.form-field { display: flex; flex-direction: column; gap: 4px; }
.form-field.span-2 { grid-column: span 2; }
.form-field.span-3 { grid-column: span 3; }
.form-field.span-4 { grid-column: span 4; }

.form-field label {
    font-size: 12px;
    font-weight: 600;
    color: var(--text-muted);
}

.form-field input[type="text"],
.form-field input[type="number"],
.form-field input[type="date"],
.form-field select,
.form-field textarea {
    padding: 7px 10px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 13px;
    font-family: inherit;
    color: var(--text);
}

.form-field input:focus,
.form-field select:focus,
.form-field textarea:focus {
    outline: none;
    border-color: var(--accent);
}

.form-field textarea { resize: vertical; min-height: 48px; }

.form-check { display: flex; align-items: center; gap: 8px; }

.form-checks-row {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 8px 20px;
}

.photo-field {
    display: flex;
    align-items: center;
    gap: 12px;
}

.photo-preview {
    width: 64px;
    height: 64px;
    border-radius: 8px;
    object-fit: cover;
    border: 1px solid var(--border);
    flex-shrink: 0;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.form-actions {
    display: flex;
    gap: 10px;
    margin-top: 16px;
    padding-top: 12px;
    border-top: 1px solid var(--border);
}

/* ---------- Responsive ---------- */
@media (max-width: 960px) {
    .form-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .form-field.span-3, .form-field.span-4 { grid-column: span 2; }
    .stats-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .two-col-grid { grid-template-columns: 1fr; }
}

@media (max-width: 782px) {
    .sidebar {
        transform: translateX(-100%);
    }
    .sidebar.open { transform: translateX(0); }
    .sidebar-overlay.open { display: block; }

    .main-wrapper { margin-left: 0; }

    .burger-btn { display: inline-flex; }

    .content { padding: 18px 16px; }

    .form-grid { grid-template-columns: 1fr; }
    .form-field.span-2, .form-field.span-3, .form-field.span-4 { grid-column: span 1; }

    .stats-grid { grid-template-columns: 1fr; }

    .topbar-username { display: none; }

    .search-box { max-width: none; }
}

/* Tableaux -> cartes empilées sur mobile (les boutons d'action restent visibles sans défilement horizontal) */
@media (max-width: 680px) {
    .table-wrap { overflow-x: visible; }

    table.data-table thead { display: none; }

    table.data-table, table.data-table tbody, table.data-table tr, table.data-table td {
        display: block;
        width: 100%;
    }

    table.data-table tr {
        margin-bottom: 12px;
        border: 1px solid var(--border);
        border-radius: 10px;
        overflow: hidden;
    }

    table.data-table tr:last-child { margin-bottom: 0; }

    table.data-table td {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 12px;
        text-align: right;
        padding: 10px 14px;
        border-bottom: 1px solid var(--border);
        white-space: normal;
    }

    table.data-table td:last-child { border-bottom: none; }

    table.data-table td::before {
        content: attr(data-label);
        font-weight: 600;
        color: var(--text-muted);
        text-align: left;
        flex-shrink: 0;
    }

    .actions-cell { justify-content: flex-end; width: 100%; }

    .cell-select:not(:has(input)) { display: none; }
    .lines-table td .autocomplete { max-width: 68%; }
    .lines-table td input[type="number"] { max-width: 60%; text-align: right; }
    .autocomplete-list { min-width: 0; }
    .bl-totals { width: 100%; }
}

/* Liste Stock Produit : tout centré et aligné en hauteur sauf référence et désignation ; saisie sans contour */
.stock-table th, .stock-table td { vertical-align: middle; text-align: center; }
.stock-table th:nth-child(1), .stock-table td:nth-child(1),
.stock-table th:nth-child(2), .stock-table td:nth-child(2) { text-align: left; }
.stock-table input[type="number"] {
    width: 90px; border: 0; background: transparent; text-align: center; font: inherit; color: inherit;
    padding: 4px 0; box-shadow: none; outline: none; -moz-appearance: textfield;
}
.stock-table input[type="number"]::-webkit-outer-spin-button,
.stock-table input[type="number"]::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.stock-table input[type="number"]:focus { border-bottom: 2px solid var(--accent); }
@media (max-width: 680px) {
    .stock-table td, .stock-table td:nth-child(1), .stock-table td:nth-child(2) { text-align: right; }
}
/* Interventions : vue technicien (smartphone) */
.int-week { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 8px; }
.int-week-title { flex: 1; text-align: center; font-weight: 700; }
.int-week-title small { display: block; font-weight: 400; color: var(--text-muted); font-size: 12px; }
.int-day { margin: 16px 2px 6px; font-weight: 700; text-transform: capitalize; }
.int-day-late { color: #b71c1c; }
.int-card { background: #fff; border-radius: 12px; padding: 14px; margin-bottom: 10px; box-shadow: 0 1px 2px rgba(0,0,0,.06); }
.int-card.int-done { opacity: .6; }
.int-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.int-addr { color: var(--text-muted); font-size: 13px; margin-top: 2px; }
.int-lines { margin: 8px 0; padding-left: 18px; font-size: 13.5px; }
.int-late { color: #b71c1c; font-size: 12.5px; margin-bottom: 6px; }
.int-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.int-actions form { margin: 0; }
.int-actions .btn { min-height: 42px; padding: 0 14px; }
.int-actions .int-fait { min-width: 96px; font-size: 15px; }
.int-info div { margin-bottom: 6px; }
.int-k { display: inline-block; min-width: 100px; color: var(--text-muted); font-size: 12px; text-transform: uppercase; letter-spacing: .03em; }
.int-link { color: var(--accent); font-weight: 600; text-decoration: none; }
/* Zone de signature du client */
.sig-pad { display: block; width: 100%; max-width: 400px; height: 150px; border: 2px dashed #9aa0a6; border-radius: 8px; background: #fff; touch-action: none; cursor: crosshair; }