:root {
    --bg: #f4f7fb;
    --card: #ffffff;
    --text: #172033;
    --muted: #687084;
    --primary: #0f766e;
    --primary-dark: #115e59;
    --border: #dfe5ef;
    --paid-bg: #dcfce7;
    --paid-text: #166534;
    --unpaid-bg: #fee2e2;
    --unpaid-text: #991b1b;
}

* { box-sizing: border-box; }
body {
    margin: 0;
    font-family: Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
}

.topbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 16px;
    padding: 14px 24px;
    background: #0f172a;
    color: white;
}

.brand {
    color: white;
    font-weight: 800;
    text-decoration: none;
}

.topbar nav {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
}

.topbar nav a {
    color: #e5e7eb;
    text-decoration: none;
    font-size: 14px;
}

.container {
    width: min(1180px, 94%);
    margin: 28px auto;
}

.card {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 24px;
    box-shadow: 0 10px 30px rgba(15, 23, 42, 0.06);
}

.center-card {
    max-width: 520px;
    margin: 60px auto;
    text-align: center;
}

h1 { margin-top: 0; }
.muted { color: var(--muted); }

input, select, button {
    min-height: 42px;
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 9px 12px;
    font-size: 15px;
}

button, .button-link {
    background: var(--primary);
    color: white;
    border: 0;
    cursor: pointer;
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
}

button:hover, .button-link:hover { background: var(--primary-dark); }
button.small { min-height: 34px; font-size: 13px; padding: 7px 10px; }

.form-inline, .form-grid, .filters {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    align-items: center;
    margin-bottom: 18px;
}

.form-inline input { flex: 1; }
.form-grid input, .form-grid select { flex: 1; min-width: 180px; }
.form-stack { display: grid; gap: 10px; }

.item-row {
    display: grid;
    grid-template-columns: 1fr 110px 160px;
    gap: 10px;
}

.summary {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 12px;
    margin: 18px 0;
}

.summary div {
    border: 1px solid var(--border);
    border-radius: 14px;
    padding: 14px;
    background: #f8fafc;
}

.summary span {
    display: block;
    color: var(--muted);
    font-size: 13px;
    margin-bottom: 5px;
}

.summary strong { font-size: 20px; }

.table-wrap { overflow-x: auto; }
table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 14px;
}

th, td {
    border-bottom: 1px solid var(--border);
    padding: 12px;
    text-align: left;
    vertical-align: top;
}

th {
    background: #eef3f8;
    font-size: 13px;
    text-transform: uppercase;
    color: #526681;
    font-weight: 800;
    border-bottom: 1px solid #d8e0ea;
}

tbody tr {
    transition: background-color 0.15s ease;
}

tbody tr:hover {
    background: #fafcff;
}

.amount-col {
    text-align: center;
    vertical-align: middle;
    white-space: nowrap;
}

.badge {
    display: inline-block;
    padding: 6px 10px;
    border-radius: 999px;
    font-weight: 800;
    font-size: 13px;
}

.badge.paid { background: var(--paid-bg); color: var(--paid-text); }
.badge.unpaid { background: var(--unpaid-bg); color: var(--unpaid-text); }

.empty {
    padding: 20px;
    background: #f8fafc;
    border: 1px dashed var(--border);
    border-radius: 12px;
    color: var(--muted);
}

@media (max-width: 760px) {
    .topbar { align-items: flex-start; flex-direction: column; }
    .summary { grid-template-columns: 1fr; }
    .item-row { grid-template-columns: 1fr; }
}

.actions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    align-items: center;
}

.small-link {
    min-height: 34px;
    padding: 7px 10px;
    border-radius: 10px;
    background: #334155;
    color: #ffffff;
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 13px;
    font-weight: 700;
}

.small-link:hover {
    background: #1e293b;
}

.form-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    align-items: center;
    margin-top: 10px;
}

.button-link.secondary {
    background: #64748b;
}

.button-link.secondary:hover {
    background: #475569;
}

.alert-error {
    border: 1px solid #fecaca;
    background: #fef2f2;
    color: #991b1b;
    padding: 12px 14px;
    border-radius: 12px;
    margin-bottom: 16px;
    font-weight: 700;
}

button.danger {
    background: #dc2626;
}

button.danger:hover {
    background: #b91c1c;
}


/* V5 — UX actions admin */
.action-col {
    width: 170px;
    min-width: 170px;
    text-align: center;
    vertical-align: middle;
}

.actions {
    display: flex;
    flex-direction: column;
    gap: 8px;
    align-items: stretch;
    justify-content: center;
}

.actions form {
    margin: 0;
    width: 100%;
}

.actions .small-link,
.actions button.small {
    width: 100%;
    min-height: 38px;
    padding: 8px 12px;
    border-radius: 10px;
    font-size: 13px;
    line-height: 1.15;
    white-space: nowrap;
}

.badge {
    white-space: nowrap;
}

@media (max-width: 900px) {
    .action-col {
        width: 150px;
        min-width: 150px;
    }

    .actions .small-link,
    .actions button.small {
        font-size: 12px;
        padding-left: 8px;
        padding-right: 8px;
    }
}

/* V8 — formulaire livraison dynamique */
.items-header {
    display: flex;
    justify-content: space-between;
    align-items: stretch;
    gap: 16px;
    margin-top: 8px;
}

.items-header h3 {
    margin-bottom: 4px;
}

.live-total {
    min-width: 220px;
    border: 1px solid var(--border);
    border-radius: 14px;
    background: #f8fafc;
    padding: 12px 14px;
    display: flex;
    flex-direction: column;
    justify-content: center;
}

.live-total span {
    color: var(--muted);
    font-size: 13px;
    margin-bottom: 4px;
}

.live-total strong {
    font-size: 20px;
}

.item-row {
    display: grid;
    grid-template-columns: minmax(240px, 1fr) 110px 160px 140px;
    gap: 10px;
    align-items: center;
}

.item-row-head {
    color: var(--muted);
    font-size: 13px;
    text-transform: uppercase;
    margin-top: 4px;
}

.line-total {
    min-height: 42px;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: #f8fafc;
    padding: 10px 12px;
    font-weight: 800;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    white-space: nowrap;
}

.hidden-row {
    display: none;
}

.add-line-btn {
    justify-self: start;
    min-height: 40px;
    padding: 9px 14px;
}

@media (max-width: 760px) {
    .items-header {
        flex-direction: column;
    }

    .live-total {
        min-width: 0;
    }

    .item-row,
    .item-row-head {
        grid-template-columns: 1fr;
    }

    .item-row-head {
        display: none;
    }

    .line-total {
        justify-content: flex-start;
    }
}


/* V9 — finition formulaire + responsive mobile */
html {
    -webkit-text-size-adjust: 100%;
}

.card {
    overflow: hidden;
}

.filters select,
.filters input,
.filters button,
.form-inline input,
.form-inline button,
.form-grid input,
.form-grid select,
.form-grid button,
.form-stack input,
.form-stack select,
.form-stack button {
    max-width: 100%;
}

.form-actions .button-link.secondary {
    min-height: 42px;
    padding: 9px 14px;
    border-radius: 10px;
}

@media (max-width: 900px) {
    .container {
        width: min(100% - 24px, 1180px);
        margin: 16px auto;
    }

    .card {
        padding: 18px;
        border-radius: 14px;
    }

    h1 {
        font-size: 28px;
        line-height: 1.1;
    }

    .filters,
    .form-inline,
    .form-grid {
        display: grid;
        grid-template-columns: 1fr;
    }

    .filters select,
    .filters input,
    .filters button,
    .form-inline input,
    .form-inline button,
    .form-grid input,
    .form-grid select,
    .form-grid button {
        width: 100%;
    }

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

@media (max-width: 760px) {
    body {
        background: #f8fafc;
    }

    .topbar {
        position: sticky;
        top: 0;
        z-index: 10;
        padding: 12px 14px;
    }

    .topbar nav {
        width: 100%;
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 8px;
    }

    .topbar nav a {
        background: rgba(255,255,255,0.08);
        border-radius: 8px;
        padding: 8px 10px;
        text-align: center;
    }

    .center-card {
        margin: 28px auto;
    }

    .card {
        padding: 16px;
        box-shadow: 0 6px 18px rgba(15, 23, 42, 0.05);
    }

    h1 {
        font-size: 26px;
    }

    .items-header {
        gap: 10px;
    }

    .live-total {
        width: 100%;
    }

    .delivery-item-row {
        border: 1px solid var(--border);
        border-radius: 14px;
        padding: 12px;
        background: #fbfdff;
        margin-bottom: 10px;
    }

    .delivery-item-row select,
    .delivery-item-row input,
    .delivery-item-row .line-total {
        width: 100%;
    }

    .line-total {
        justify-content: space-between;
    }

    .line-total::before {
        content: 'Total ligne';
        color: var(--muted);
        font-weight: 700;
        margin-right: 12px;
    }

    .add-line-btn,
    .form-actions button,
    .form-actions .button-link {
        width: 100%;
    }

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

    table,
    thead,
    tbody,
    th,
    td,
    tr {
        display: block;
        width: 100%;
    }

    thead {
        display: none;
    }

    tbody tr {
        border: 1px solid var(--border);
        border-top: 4px solid #eef3f8;
        border-radius: 14px;
        background: #ffffff;
        margin-bottom: 14px;
        padding: 10px 12px;
        box-shadow: 0 4px 14px rgba(15, 23, 42, 0.04);
    }

    td {
        border-bottom: 1px solid #edf2f7;
        display: grid;
        grid-template-columns: 120px 1fr;
        gap: 12px;
        padding: 10px 0;
        align-items: start;
        text-align: right;
    }

    td:last-child {
        border-bottom: 0;
    }

    td::before {
        content: attr(data-label);
        font-weight: 800;
        color: var(--muted);
        text-transform: uppercase;
        font-size: 12px;
        text-align: left;
    }

    .amount-col {
        text-align: right;
        white-space: normal;
    }

    .action-col {
        width: 100%;
        min-width: 0;
        text-align: right;
    }

    .actions {
        width: 100%;
        gap: 8px;
    }

    .actions .small-link,
    .actions button.small {
        min-height: 42px;
        font-size: 14px;
    }
}

@media (max-width: 420px) {
    .container {
        width: calc(100% - 16px);
        margin: 10px auto;
    }

    .card {
        padding: 14px;
        border-radius: 12px;
    }

    h1 {
        font-size: 24px;
    }

    td {
        grid-template-columns: 1fr;
        text-align: left;
        gap: 4px;
    }

    .amount-col {
        text-align: left;
    }
}

/* V12 — centrage vertical de la date dans les lignes de livraison */
.date-col {
    text-align: center;
    vertical-align: middle;
    white-space: nowrap;
}

@media (max-width: 760px) {
    .date-col {
        text-align: right;
        white-space: normal;
    }
}

@media (max-width: 420px) {
    .date-col {
        text-align: left;
    }
}

/* V13 — centrage forcé de la première colonne Date, même si index.php n'a pas encore la classe date-col */
@media (min-width: 761px) {
    table thead th:first-child,
    table tbody td:first-child,
    th.date-col,
    td.date-col {
        text-align: center !important;
        vertical-align: middle !important;
        white-space: nowrap;
    }

    table tbody td:first-child,
    td.date-col {
        padding-top: 0 !important;
        padding-bottom: 0 !important;
    }
}


/* V15 — alignement horizontal des contenus sous les titres (desktop) */
@media (min-width: 761px) {
    table thead th,
    table tbody td {
        vertical-align: middle !important;
    }

    /* Aligner les entêtes et les contenus de chaque colonne de façon cohérente */
    table thead th,
    table tbody td {
        text-align: center;
    }

    /* Conserver un rendu propre sur plusieurs lignes */
    table tbody td {
        line-height: 1.3;
    }

    /* Colonnes spécifiques */
    table thead th:nth-child(2),
    table tbody td:nth-child(2),
    table thead th:nth-child(3),
    table tbody td:nth-child(3),
    table thead th:nth-child(4),
    table tbody td:nth-child(4),
    table thead th:nth-child(5),
    table tbody td:nth-child(5) {
        text-align: center !important;
    }

    .date-col,
    .amount-col,
    .action-col {
        text-align: center !important;
        vertical-align: middle !important;
    }

    .actions {
        align-items: center;
        justify-content: center;
    }

    .actions .small-link,
    .actions button.small {
        margin-left: auto;
        margin-right: auto;
    }
}
