/* ==========================================================================
   Service technique - styles communs
   Toutes les règles responsive sont ici : les pages utilisent les composants
   Blade (resources/views/components) et n'ont pas besoin de CSS propre.
   ========================================================================== */

:root {
    --brand: #06a044;
    --brand-dark: #05863a;
    --brand-soft: #e7f6ed;
    --sidebar-width: 240px;
    --navbar-height: 60px;
    --radius: 10px;
    --border: #e3e6ea;
    --muted: #6c757d;
    --surface: #ffffff;
    --page-bg: #f5f7f9;
    --shadow: 0 1px 3px rgba(16, 24, 40, .08), 0 1px 2px rgba(16, 24, 40, .04);
}

body {
    font-family: 'Cairo', sans-serif;
    background: var(--page-bg);
    color: #212529;
}

/* Bootstrap "success" follows the brand colour */
.btn-success { --bs-btn-bg: var(--brand); --bs-btn-border-color: var(--brand); --bs-btn-hover-bg: var(--brand-dark); --bs-btn-hover-border-color: var(--brand-dark); --bs-btn-active-bg: var(--brand-dark); }
.btn-outline-success { --bs-btn-color: var(--brand); --bs-btn-border-color: var(--brand); --bs-btn-hover-bg: var(--brand); --bs-btn-hover-border-color: var(--brand); }
.bg-success { background-color: var(--brand) !important; }
.text-success { color: var(--brand) !important; }

/* ---------- Sidebar ---------- */
.sidebar {
    position: fixed;
    top: 0;
    bottom: 0;
    inset-inline-start: 0;
    width: var(--sidebar-width);
    background: var(--surface);
    border-inline-end: 1px solid var(--border);
    padding: 12px;
    overflow-y: auto;
    z-index: 1040;
    transition: transform .25s ease;
}

.sidebar-brand {
    text-align: center;
    margin-bottom: 12px;
}

.sidebar-brand img {
    /* Logo large (CMM) : affiché entier, sans le recadrer */
    width: 100%;
    max-width: 170px;
    height: auto;
}

.sidebar-brand p {
    font-weight: 700;
    margin: 6px 0 0;
    letter-spacing: .5px;
}

.sidebar-nav a {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 14px;
    margin-bottom: 2px;
    color: #333;
    text-decoration: none;
    border-radius: 8px;
}

.sidebar-nav a i {
    width: 20px;
    text-align: center;
    color: var(--brand);
}

.sidebar-nav a:hover {
    background: var(--brand-soft);
}

.sidebar-nav a.active {
    background: var(--brand);
    color: #fff;
}

.sidebar-nav a.active i {
    color: #fff;
}

.sidebar .sidebar-close {
    display: none;
}

.sidebar-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, .4);
    z-index: 1035;
}

/* ---------- Navbar & content ---------- */
.app-navbar {
    position: sticky;
    top: 0;
    z-index: 1020;
    height: var(--navbar-height);
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 0 16px;
    margin-inline-start: var(--sidebar-width);
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    transition: margin .25s ease;
}

.app-navbar-title {
    flex: 1;
    min-width: 0;
    margin: 0;
    font-size: 1.1rem;
    font-weight: 700;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.content {
    margin-inline-start: var(--sidebar-width);
    padding: 20px;
    transition: margin .25s ease;
}

/* Desktop: the sidebar can be collapsed with the ☰ button */
@media (min-width: 992px) {
    body.sidebar-collapsed .sidebar {
        transform: translateX(-100%);
    }

    [dir="rtl"] body.sidebar-collapsed .sidebar {
        transform: translateX(100%);
    }

    body.sidebar-collapsed .app-navbar,
    body.sidebar-collapsed .content {
        margin-inline-start: 0;
    }
}

/* ---------- Page header (x-page-header) ---------- */
.page-header {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    margin-bottom: 16px;
}

.page-header h2 {
    margin: 0;
    font-size: 1.5rem;
    font-weight: 700;
}

.page-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

/* ---------- Toolbar / search (x-search-form) ---------- */
.search-form {
    max-width: 420px;
    width: 100%;
}

/* ---------- Multiple choice (x-form.checkbox-list) ---------- */
.checkbox-list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
    gap: 6px 16px;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--surface);
}

.checkbox-list.is-invalid {
    border-color: var(--bs-danger);
}

.checkbox-list .form-check {
    margin: 0;
}

/* ---------- Cards ---------- */
.card {
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}

.card-header {
    border-top-left-radius: var(--radius) !important;
    border-top-right-radius: var(--radius) !important;
}

.card-header h5 {
    font-size: 1rem;
    font-weight: 700;
}

.card.text-center-legacy {
    text-align: center;
}

/* Label / value rows (x-info-row) */
.info-row {
    display: flex;
    flex-wrap: wrap;
    gap: 4px 12px;
    padding: 8px 0;
    border-bottom: 1px dashed var(--border);
}

.info-row:last-child {
    border-bottom: 0;
}

.info-row dt {
    flex: 0 0 38%;
    font-weight: 600;
    color: var(--muted);
}

.info-row dd {
    flex: 1;
    margin: 0;
    min-width: 0;
    word-break: break-word;
}

/* Stat tiles (x-stat-card) */
.stat-card {
    display: flex;
    align-items: center;
    gap: 14px;
    padding: 16px;
    height: 100%;
    color: inherit;
    text-decoration: none;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    transition: transform .15s ease, box-shadow .15s ease;
}

a.stat-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 16px rgba(16, 24, 40, .1);
    color: inherit;
}

.stat-card-icon {
    flex: 0 0 48px;
    height: 48px;
    display: grid;
    place-items: center;
    border-radius: 12px;
    font-size: 1.3rem;
}

/* bg-success is forced to the brand colour above: keep the icon tile light */
.stat-card-icon.bg-success {
    background-color: var(--brand-soft) !important;
}

.stat-card-value {
    font-size: 1.5rem;
    font-weight: 700;
    line-height: 1.1;
}

.stat-card-label {
    color: var(--muted);
    font-size: .9rem;
}

/* ---------- Tables (x-data-table) ---------- */
.table {
    margin-bottom: 0;
    background: var(--surface);
}

.table thead th {
    background: var(--brand);
    color: #fff;
    font-weight: 600;
    white-space: nowrap;
    vertical-align: middle;
}

.table-wrapper {
    background: var(--surface);
}

.table-actions {
    white-space: nowrap;
    text-align: center;
}

.table-actions > * {
    margin-inline: 3px;
}

/* Plain coloured icons in the "Actions" column (x-icon-link / x-form-button without label) */
.table-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 4px;
    border: 0;
    font-size: 1.05rem;
    line-height: 1;
    text-decoration: none;
    vertical-align: middle;
}

.table-icon:hover {
    opacity: .75;
}

/* Square icon buttons (navbar, sidebar) */
.btn-icon {
    width: 34px;
    height: 34px;
    padding: 0;
    display: inline-grid;
    place-items: center;
    border-radius: 8px;
}

/* Form buttons: side by side, also on phones (x-form.actions, x-modal footer) */
.form-buttons {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
}

/* Groups of fields inside a .row (x-stock.piece-source-fields): their columns belong to the parent row */
.stock-group {
    display: contents;
}

/* Same base as Bootstrap gives to direct children of .row; :where() keeps it weaker than col-* classes */
:where(.row > .stock-group > *) {
    flex-shrink: 0;
    width: 100%;
    max-width: 100%;
    padding-right: calc(var(--bs-gutter-x) * .5);
    padding-left: calc(var(--bs-gutter-x) * .5);
    margin-top: var(--bs-gutter-y);
}

/* Small select placed before a search field (source filter of the pieces list) */
.search-form .search-filter {
    flex: 0 0 auto;
    width: auto;
    max-width: 45%;
}

/* Row toggle of stacked tables: only used on phones */
.stack-toggle {
    display: none;
}

/* Piece thumbnails (x-piece-thumb): fixed size, cropped, so rows keep the same height */
.piece-thumb {
    object-fit: cover;
    border-radius: 6px;
    border: 1px solid var(--border);
    background: #fff;
}

.piece-thumb-sm {
    width: 44px;
    height: 44px;
}

.piece-thumb-lg {
    width: 160px;
    height: 160px;
}

.pagination {
    flex-wrap: wrap;
    justify-content: center;
    margin: 16px 0 0;
}

/* ---------- Print ---------- */
.print-header,
.print-footer {
    display: none;
}

.footer {
    font-size: 10px;
    margin-top: 40px;
    text-align: center;
    line-height: 1.5;
}

/* ==========================================================================
   Responsive
   ========================================================================== */

/* Tablets & phones: sidebar becomes a sliding panel */
@media (max-width: 991.98px) {
    .sidebar {
        transform: translateX(-100%);
    }

    [dir="rtl"] .sidebar {
        transform: translateX(100%);
    }

    body.sidebar-open .sidebar {
        transform: none;
        box-shadow: 0 0 24px rgba(0, 0, 0, .2);
    }

    body.sidebar-open .sidebar-backdrop {
        display: block;
    }

    body.sidebar-open {
        overflow: hidden;
    }

    .sidebar .sidebar-close {
        display: inline-grid;
    }

    .app-navbar,
    .content {
        margin-inline-start: 0;
    }
}

/* Phones */
@media (max-width: 767.98px) {
    .content {
        padding: 12px;
    }

    .page-header h2 {
        font-size: 1.25rem;
    }

    .page-header,
    .page-actions {
        width: 100%;
    }

    .page-actions > .btn,
    .page-actions > form {
        flex: 1 1 auto;
    }

    .page-actions > form .btn {
        width: 100%;
    }

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

    .form-buttons > .btn {
        flex: 1 1 0;
        min-width: 0;
    }

    /* Legacy width helpers on pages not yet converted */
    .content .w-25,
    .content .w-50,
    .content .w-75 {
        width: 100% !important;
    }

    .info-row dt {
        flex-basis: 100%;
    }

    .modal-dialog {
        margin: .5rem;
    }

    /* Stacked tables: every row becomes a card, every cell shows its column name */
    .table-stack thead {
        display: none;
    }

    .table-wrapper:has(.table-stack) {
        border: 0;
        box-shadow: none;
        background: transparent;
        overflow: visible;
    }

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

    .table-stack {
        background: transparent;
    }

    .table-stack tr {
        margin-bottom: 12px;
        padding: 6px 14px;
        background: var(--surface);
        border: 1px solid var(--border);
        border-radius: var(--radius);
        box-shadow: var(--shadow);
    }

    .table-stack > :not(caption) > * > * {
        border: 0;
        box-shadow: none;
    }

    .table-stack td {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 12px;
        padding: 8px 0;
        text-align: end;
        border-bottom: 1px dashed var(--border) !important;
    }

    /* Cells with several elements (badges, buttons...) keep them together on the value side */
    .table-stack td {
        flex-wrap: wrap;
    }

    /* Highlighted rows (table-danger, table-warning...) colour the whole card */
    .table-stack tr[class*="table-"] {
        background: var(--bs-table-bg);
        border-color: var(--bs-table-border-color);
    }

    .table-stack td:last-child {
        border-bottom: 0 !important;
    }

    .table-stack td::before {
        content: attr(data-label);
        flex: 0 0 auto;
        max-width: 45%;
        font-weight: 600;
        color: var(--muted);
        text-align: start;
    }

    .table-stack td[data-label=""]::before,
    .table-stack td.table-empty::before {
        display: none;
    }

    .table-stack td.table-empty {
        justify-content: center;
    }

    /* Collapsible rows: the extra fields open with the chevron */
    .table-stack tr:not(.is-open) td.stack-extra {
        display: none;
    }

    .table-stack td.stack-toggle {
        display: flex;
        justify-content: center;
        padding: 0;
    }

    /* Closed row: the chevron is the last visible line */
    .table-stack tr:not(.is-open) td.stack-toggle {
        border-bottom: 0 !important;
    }

    .table-stack td.stack-toggle::before {
        display: none;
    }

    .table-stack td.stack-toggle .btn {
        width: 100%;
        padding: 6px 0;
        color: var(--muted);
    }

    .table-stack td.stack-toggle i {
        transition: transform .2s ease;
    }

    .table-stack tr.is-open td.stack-toggle i {
        transform: rotate(180deg);
    }

    .table-stack td.table-actions {
        justify-content: flex-end;
        flex-wrap: wrap;
        padding-top: 10px;
    }

    .table-stack td.table-actions::before {
        display: none;
    }

    /* Bigger tap targets on phones */
    .table-stack .table-icon {
        min-width: 40px;
        min-height: 40px;
        font-size: 1.2rem;
    }
}

@media print {
    .sidebar,
    .sidebar-backdrop,
    .app-navbar,
    .no-print,
    .aap {
        display: none !important;
    }

    body {
        margin: 0;
        background: #fff;
    }

    .content {
        margin: 0;
        padding: 0;
        max-width: 100%;
    }

    .print-header,
    .print-footer {
        display: block;
        position: fixed;
        width: 100%;
    }

    .print-footer {
        bottom: 0;
        margin-top: 10px;
    }
}
