:root {
    --bg: #f4f6f9;
    --surface: #ffffff;
    --text: #1a2332;
    --muted: #5c6b7a;
    --primary: #0f4c81;
    --primary-hover: #0d3f6b;
    --accent: #e67e22;
    --border: #dde3ea;
    --success: #1e7e4a;
    --error: #b42318;
    --radius: 10px;
    --shadow: 0 8px 24px rgba(15, 76, 129, 0.08);
    font-family: "Segoe UI", system-ui, sans-serif;
}

* { box-sizing: border-box; }
html {
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
}
body { margin: 0; background: var(--bg); color: var(--text); line-height: 1.5; }
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

.topbar {
    display: flex; align-items: center; gap: 1.5rem;
    padding: 0.85rem 1.5rem; background: var(--primary); color: #fff;
    box-shadow: var(--shadow);
}
.brand { color: #fff; font-weight: 700; font-size: 1.1rem; text-decoration: none; }
.main-nav { display: flex; gap: 1rem; flex: 1; }
.main-nav a { color: rgba(255,255,255,.9); }
.user-menu { display: flex; align-items: center; gap: .75rem; color: #fff; }

.container { max-width: 1280px; margin: 0 auto; padding: 1.5rem; }
.container-wide { max-width: 100%; width: 100%; min-width: 0; padding: 1rem 1.25rem 2rem; }
.page-library main.container-wide {
    width: 100%;
    max-width: 100%;
    min-width: 0;
    box-sizing: border-box;
}
.library-bulk-form-meta { display: none; }
.page-library .page-header { flex-wrap: wrap; }
.page-header { display: flex; justify-content: space-between; align-items: flex-start; gap: 1rem; margin-bottom: 1.25rem; }
.page-header h1 { margin: .2rem 0 0; font-size: 1.6rem; }
.actions { display: flex; gap: .5rem; flex-wrap: wrap; }
.muted { color: var(--muted); margin: 0; }

.panel {
    background: var(--surface); border: 1px solid var(--border);
    border-radius: var(--radius); padding: 1.25rem; margin-bottom: 1rem;
    box-shadow: var(--shadow);
}
.panel h2 { margin-top: 0; font-size: 1.1rem; }

.stats-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 1rem; margin-bottom: 1rem; }
.stat-card {
    background: var(--surface); border: 1px solid var(--border);
    border-radius: var(--radius); padding: 1rem;
}
.stat-card.highlight { border-color: var(--accent); background: #fff8f0; }
.stat-label { display: block; color: var(--muted); font-size: .85rem; }
.stat-card strong { font-size: 1.35rem; }

.meta-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 1rem; }
.grid-2 { display: grid; grid-template-columns: 1fr 1.4fr; gap: 1rem; }
@media (max-width: 900px) { .grid-2 { grid-template-columns: 1fr; } }

.data-table { width: 100%; border-collapse: collapse; font-size: .92rem; }
.data-table th, .data-table td { padding: .55rem .6rem; border-bottom: 1px solid var(--border); text-align: left; }
.data-table th { background: #f8fafc; font-weight: 600; }
.data-table .num { text-align: right; white-space: nowrap; }
.line-lot td { background: #eef4fb; font-weight: 600; }
.line-poste td { background: #f8fafc; }
.best-price { background: #e8f5ee; font-weight: 600; }
.table-scroll {
    display: block;
    width: 100%;
    max-width: 100%;
    overflow-x: auto;
    overflow-y: visible;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-x: contain;
}

.btn {
    display: inline-flex; align-items: center; justify-content: center;
    padding: .5rem 1rem; border-radius: 8px; border: none; cursor: pointer;
    font-weight: 600; text-decoration: none;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); color: #fff; text-decoration: none; }
.btn-ghost { background: transparent; border: 1px solid var(--border); color: var(--text); }
.btn-danger { background: var(--error); color: #fff; }
.btn-sm { padding: .25rem .55rem; font-size: .85rem; }
.btn-block { width: 100%; }

.badge {
    display: inline-block; padding: .15rem .45rem; border-radius: 999px;
    background: #e8eef5; font-size: .75rem; text-transform: uppercase;
}

.alert { padding: .75rem 1rem; border-radius: 8px; margin-bottom: 1rem; }
.alert-success { background: #e8f5ee; color: var(--success); }
.alert-error { background: #fdecec; color: var(--error); }

.auth-shell { min-height: 100vh; display: flex; align-items: center; justify-content: center; }
.auth-container { width: 100%; max-width: 520px; padding: 1rem; }
.auth-card {
    background: var(--surface); padding: 2rem; border-radius: var(--radius);
    box-shadow: var(--shadow); border: 1px solid var(--border);
}
.auth-card-wide { max-width: 640px; margin: 0 auto; }

label { display: block; font-size: .9rem; margin-bottom: .75rem; }
input, select, textarea {
    width: 100%; margin-top: .25rem; padding: .5rem .65rem;
    border: 1px solid var(--border); border-radius: 8px; font: inherit;
}
.form-stack label + label { margin-top: 0; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
.form-grid .full { grid-column: 1 / -1; }
fieldset { border: 1px solid var(--border); border-radius: 8px; padding: 1rem; }
legend { font-weight: 600; padding: 0 .35rem; }

.line-form-row { display: flex; flex-wrap: wrap; gap: .4rem; align-items: center; }
.line-form-row .grow { flex: 2; min-width: 180px; }
.num-input { width: 90px; }
.library-pick { margin-top: .75rem; font-size: .9rem; }
.inline-form { display: inline; }
.actions-cell { white-space: nowrap; }
.actions-cell .btn, .actions-cell form { margin-left: .25rem; }
.form-grid .inline-form { display: inline-block; margin-left: .5rem; }
.search-bar { display: flex; gap: .5rem; }
.center { text-align: center; }
.small { font-size: .85rem; }
.work-type-title { color: var(--primary); border-bottom: 2px solid var(--accent); padding-bottom: .35rem; }
.descriptif-grid { grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); }
.pricing-summary .calc-table { max-width: 480px; margin-top: 1rem; }
.row-pv td { background: #fff3cd; font-size: 1.05rem; }
.row-marge td { background: #fce4ec; }
.col-pv { background: #fff8e6; }
.library-table input.grow { width: 100%; min-width: 140px; }
.col-name { min-width: 280px; max-width: 420px; }
.inline-rename-form { display: flex; gap: .35rem; align-items: center; }
.library-grid .inline-rename-form .article-name-field { flex: 1; min-width: 160px; max-width: 260px; font: inherit; font-size: .82rem; }
.inline-rename-form .article-name-field { flex: 1; min-width: 200px; font: inherit; line-height: 1.35; }
.article-name-field { width: 100%; font: inherit; line-height: 1.4; }
.work-type-rename { display: inline-flex; gap: .5rem; align-items: center; margin-left: .5rem; flex-wrap: wrap; }
.work-type-rename .work-type-input { min-width: 8rem; max-width: 20rem; font-weight: 600; }
#materials-table input, #labor-table input { width: 100%; max-width: 100%; }
.form-actions { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; margin-top: 1rem; }

/* Bibliothèque — grille */
.library-grid-panel {
    padding: 0;
    width: 100%;
    max-width: 100%;
    min-width: 0;
}
.library-grid-panel .table-scroll {
    border-top: 1px solid var(--border);
}
.library-grid {
    width: 100%;
    min-width: 72rem;
    font-size: .82rem;
    border-collapse: collapse;
}
.library-grid thead th {
    position: sticky;
    top: 0;
    z-index: 2;
    white-space: nowrap;
    background: #e8eef5;
    border-bottom: 2px solid var(--primary);
}
.library-grid td, .library-grid th { padding: .4rem .55rem; vertical-align: middle; }
.library-grid .col-type { white-space: nowrap; font-weight: 600; color: var(--primary); }
.library-grid .col-name { min-width: 200px; max-width: 280px; white-space: normal; }
.library-grid .col-name-h { min-width: 180px; }
.library-grid .cell-wrap { white-space: normal; max-width: 140px; display: inline-block; }
.library-grid .col-pv { background: #fff3cd; font-weight: 700; }
.library-grid .col-actions { white-space: nowrap; }
.library-lot-row td {
    background: var(--primary);
    color: #fff;
    padding: .65rem 1rem;
    border-top: 3px solid var(--accent);
}
.library-lot-row .lot-label {
    font-size: .7rem;
    letter-spacing: .08em;
    opacity: .85;
    margin-right: .5rem;
}
.library-lot-row .lot-title { font-size: 1.1rem; margin-right: 1rem; }
.library-lot-row .btn { color: #fff; border-color: rgba(255,255,255,.4); }
.library-empty-lot td { font-style: italic; padding: .75rem 1rem; background: #fafafa; }
.library-lot-add .quick-add-row { margin-bottom: 1rem; padding-bottom: 1rem; border-bottom: 1px solid var(--border); }
.library-lot-add .quick-add-lot { align-items: flex-end; }
.library-sub-row td {
    background: #f5f0e6;
    font-weight: 600;
    border-top: 1px solid #e0d4c0;
    padding: .5rem .75rem;
}
.library-sub-row .sub-label {
    font-size: .7rem;
    text-transform: uppercase;
    color: #8a6d3b;
    margin-right: .5rem;
}
.library-sub-row .sub-title {
    color: #6b4e16;
    margin-right: .75rem;
}
.library-sub-row .btn { border-color: rgba(107, 78, 22, .35); }
.library-empty-sub td { font-style: italic; padding: .5rem 1rem; background: #faf8f5; }
.library-type-row td {
    background: #eef4fb;
    font-weight: 600;
    border-top: 1px solid var(--border);
    padding: .45rem .75rem;
}
.library-lot-add .quick-add-row {
    display: flex;
    flex-wrap: wrap;
    gap: 1rem;
    align-items: flex-end;
}
.library-type-row .type-label { font-size: .7rem; text-transform: uppercase; color: var(--muted); margin-right: .5rem; }
.library-type-row .type-title { color: var(--primary); }
.library-type-row .type-label { margin-right: .75rem; color: var(--primary); text-transform: uppercase; font-size: .75rem; }
.library-ouvrage-row--open { cursor: pointer; }
.library-ouvrage-row--open:hover td { background: #eef4ff; }
.library-ouvrage-row--open:focus-visible td { outline: 2px solid var(--primary, #1a5fb4); outline-offset: -2px; }
.library-ouvrage-row--open .col-check,
.library-ouvrage-row--open .col-actions { cursor: default; }
.library-ouvrage-row:hover td { background: #fafcff; }
.library-search { padding: 1rem 1.25rem .5rem; }
.library-bulk-wrapper { display: block; }
.library-bulk-bar { padding: .75rem 1.25rem; margin-bottom: 0; display: flex; flex-wrap: wrap; align-items: center; gap: 1rem; }
.hidden-form { display: none; }
.library-empty-state p { margin: .5rem 0; }
.library-empty-state .btn { margin-right: .5rem; margin-top: .5rem; }
#library-actions { scroll-margin-top: 1rem; }
.library-quick-add h2 { margin: 0 0 .75rem; font-size: 1.05rem; color: var(--primary); }
.quick-add-form {
    display: flex;
    flex-wrap: wrap;
    gap: .75rem;
    align-items: flex-end;
}
.quick-add-form label { margin-bottom: 0; font-size: .85rem; }
.quick-field-grow { flex: 2 1 220px; min-width: 200px; }
.name-edit-cell { display: flex; gap: .3rem; align-items: center; min-width: 180px; }
.name-edit-cell .name-input { flex: 1; min-width: 120px; font-size: .82rem; }
.library-bulk-form {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 1rem;
}
.bulk-select-all { margin: 0; font-weight: 600; display: inline-flex; align-items: center; gap: .4rem; }
.bulk-select-all input { width: auto; margin: 0; }
.library-grid .col-check { width: 2.5rem; text-align: center; padding: .35rem; }
.library-grid .col-check input { width: 1rem; height: 1rem; margin: 0; cursor: pointer; }
.library-ouvrage-row.is-selected td { background: #fff8e6; }
.panel-flush { padding-bottom: 0; }

/* Fiche ouvrage — bandeau horizontal */
.library-sheet { overflow-x: auto; }
.library-sheet .sheet-title { font-size: 1rem; margin-bottom: .75rem; color: var(--primary); }
.excel-row {
    display: flex;
    flex-wrap: nowrap;
    gap: .5rem;
    margin-bottom: .75rem;
    min-width: min(100%, 1200px);
}
.excel-row-descriptif { min-width: 1100px; }
.excel-cell {
    flex: 1 1 0;
    min-width: 95px;
    margin-bottom: 0;
    font-size: .8rem;
}
.excel-cell-wide { flex: 2 1 220px; min-width: 200px; }
.excel-cell input { margin-top: .2rem; padding: .35rem .5rem; font-size: .85rem; }
.excel-row-details {
    display: grid;
    grid-template-columns: 1fr 1fr minmax(220px, 280px);
    gap: 1rem;
    align-items: start;
    min-width: 900px;
}
.excel-block h3 { margin: 0 0 .5rem; font-size: .9rem; color: var(--muted); }
.excel-block-pv {
    background: #fff8e6;
    border: 1px solid #f0d78c;
    border-radius: 8px;
    padding: .75rem;
}
.excel-block-pv label { font-size: .8rem; margin-bottom: .5rem; }
.excel-block-pv .calc-table-compact { margin-top: .5rem; font-size: .85rem; }
.excel-notes { display: block; margin-top: .75rem; font-size: .85rem; }
.excel-notes input { margin-top: .25rem; }
.materials-block-header {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: .75rem;
    margin-bottom: .75rem;
}
.materials-block-header h3 { margin: 0; }
.supplier-pick-label { margin: 0; font-size: .9rem; }
.supplier-pick-label select { min-width: 220px; margin-left: .35rem; }
#materials-table .col-pick { width: 5.5rem; white-space: nowrap; vertical-align: middle; }
.supplier-price-picker {
    border: none;
    border-radius: var(--radius);
    padding: 0;
    max-width: min(720px, 96vw);
    width: 100%;
    box-shadow: 0 16px 48px rgba(0, 0, 0, .2);
}
.supplier-price-picker::backdrop { background: rgba(15, 23, 42, .45); }
.supplier-price-picker .picker-inner { padding: 1rem 1.25rem 1.25rem; margin: 0; }
.supplier-price-picker .picker-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    margin-bottom: .5rem;
}
.supplier-price-picker .picker-header h3 { margin: 0; }
.supplier-price-picker .picker-search {
    width: 100%;
    margin: .5rem 0 .75rem;
    padding: .5rem .65rem;
    border: 1px solid var(--border);
    border-radius: 6px;
}
.supplier-price-picker .picker-list-wrap { max-height: 50vh; }
.supplier-import-panel { border-left: 4px solid var(--primary); }
.supplier-import-form {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-end;
    gap: 1rem;
    margin-top: .75rem;
}
.supplier-import-form .import-file-label { flex: 1 1 260px; margin: 0; }
.supplier-import-form .import-replace-check {
    display: inline-flex;
    align-items: center;
    gap: .4rem;
    margin: 0;
    font-size: .9rem;
}
.supplier-import-form .import-replace-check input { width: auto; margin: 0; }
#supplier-prices-table input { width: 100%; min-width: 0; }
#materials-table input, #labor-table input { width: 100%; min-width: 0; }
#materials-table .input-unit { width: 3rem; }
#materials-table .num-input, #labor-table .num-input { width: 4.5rem; }

.import-mode-box {
    background: #f8fafc;
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 1rem;
    margin: .75rem 0;
}
.import-mode-box p { margin: 0 0 .75rem; }
.import-mode-danger { color: var(--error); font-weight: 600; }
.import-lot-fieldset {
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 1rem 1.25rem;
    margin: 0 0 1rem;
}
.import-lot-fieldset legend { font-weight: 600; padding: 0 .35rem; }
.lot-mode-option { display: flex; align-items: center; gap: .5rem; font-weight: 600; margin-top: .75rem; }
.lot-mode-option input { width: auto; margin: 0; }
.import-lot-existing, .import-lot-new { margin-left: 1.5rem; }
.import-lot-existing select, .import-lot-new input { margin-top: .25rem; }

@media (max-width: 1100px) {
    .excel-row { flex-wrap: wrap; min-width: 0; }
    .excel-row-descriptif { min-width: 0; }
    .excel-row-details { grid-template-columns: 1fr; min-width: 0; }
}

@media (max-width: 900px) {
    .topbar {
        flex-wrap: wrap;
        gap: .75rem;
        padding: .75rem 1rem;
    }
    .main-nav {
        flex-wrap: wrap;
        order: 3;
        width: 100%;
    }
    .page-header {
        flex-direction: column;
        align-items: stretch;
    }
    .page-header .actions {
        justify-content: flex-start;
    }
    .library-grid {
        min-width: 52rem;
        font-size: .78rem;
    }
    .library-grid .col-detail {
        display: none;
    }
}
