/* ========================================================================== 
   ICYLAB OS - Contrato visual CRM compartido
   Fuente: Cotizaciones, Opportunity y Embudo.
   Estas clases son la única base de layout para CRM y Finanzas.
   ========================================================================== */

/* Page shell -------------------------------------------------------------- */
.crm-ui-main {
    height: 100vh;
    height: 100dvh;
    min-height: 0;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

.crm-ui-page-content {
    flex: 1 1 0%;
    min-height: 0;
    width: 100%;
    max-width: var(--container-max);
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

.crm-ui-page {
    flex: 1 1 0%;
    min-height: 0;
    width: 100%;
    max-width: 100%;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    padding-top: var(--space-5);
    padding-bottom: var(--space-5);
}

.crm-ui-page-header {
    flex: 0 0 auto;
    min-width: 0;
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: var(--space-4);
    margin: 0 0 var(--space-4);
}

.crm-ui-page-header-copy {
    min-width: 0;
    flex: 1 1 auto;
}

.crm-ui-page-header-copy .label {
    display: block;
    margin: 0 0 6px;
}

.crm-ui-page-header-copy h1,
.crm-ui-page-header-copy h2 {
    margin: 0 0 4px;
    line-height: 1.1;
}

.crm-ui-page-header-copy p {
    margin: 0;
    max-width: 72ch;
}

.crm-ui-page-actions {
    margin-left: auto;
    flex: 0 0 auto;
    min-width: 0;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 10px;
    flex-wrap: nowrap;
}

.crm-ui-page-actions .btn {
    white-space: nowrap;
}

.crm-ui-env-select {
    min-width: 132px;
    height: 42px;
    min-height: 42px;
    border: 1px solid var(--border-default);
    border-radius: var(--radius-lg);
    padding: 0 36px 0 13px;
    background: var(--surface-1);
    color: var(--text-primary);
    font: inherit;
}

/* Filters ---------------------------------------------------------------- */
.crm-ui-filter {
    --crm-filter-columns: minmax(280px, 1fr) 180px minmax(360px, 430px);
    flex: 0 0 auto;
    position: relative;
    z-index: 20;
    min-width: 0;
    width: 100%;
    max-width: 100%;
    display: grid;
    grid-template-columns: var(--crm-filter-columns);
    align-items: center;
    gap: var(--space-3);
    padding: var(--space-4);
    margin: 0 0 var(--space-4);
    overflow: hidden;
    background: var(--surface-1);
    border: 1px solid var(--border-default);
    border-radius: var(--radius-2xl);
}

.crm-ui-filter > * {
    min-width: 0;
    max-width: 100%;
}

.crm-ui-filter input,
.crm-ui-filter select,
.crm-ui-input {
    width: 100%;
    min-width: 0;
    height: 44px;
    min-height: 44px;
    border: 1px solid var(--border-default);
    border-radius: var(--radius-lg);
    padding: 0 13px;
    background: var(--surface-1);
    color: var(--text-primary);
    font: inherit;
}

.crm-ui-date-group {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: var(--space-2);
    min-width: 0;
    width: 100%;
}

.crm-ui-date-field {
    position: relative;
    display: block;
    min-width: 0;
    width: 100%;
}

.crm-ui-date-field > span,
.crm-ui-date-field > em {
    display: none;
}

.crm-ui-filter-actions {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 8px;
    min-width: 0;
}

/* KPI cards --------------------------------------------------------------- */
.crm-ui-kpis {
    --crm-kpi-columns: 4;
    flex: 0 0 auto;
    display: grid;
    grid-template-columns: repeat(var(--crm-kpi-columns), minmax(0, 1fr));
    gap: var(--space-3);
    margin: 0 0 var(--space-4);
}

.crm-ui-kpis .kpi-card {
    min-width: 0;
    min-height: 0;
}

.crm-ui-kpis .kpi-main {
    margin-bottom: 0;
}

.crm-ui-kpis .kpi-value,
.crm-ui-kpis strong.kpi-value {
    display: block;
    min-width: 0;
    color: var(--text-primary);
    font-size: clamp(1.05rem, 1.55vw, 1.45rem);
    line-height: 1.1;
    overflow-wrap: anywhere;
}

/* Table card -------------------------------------------------------------- */
.crm-ui-table-card {
    flex: 1 1 0%;
    min-height: 0;
    width: 100%;
    max-width: 100%;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    background: var(--surface-1);
    border: 1px solid var(--border-default);
    border-radius: var(--radius-xl);
    box-shadow: var(--shadow-md);
}

.crm-ui-table-toolbar,
.crm-ui-bulkbar {
    flex: 0 0 auto;
    min-width: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 12px 16px;
    border-bottom: 1px solid var(--border-subtle);
    background: var(--surface-1);
}

.crm-ui-table-toolbar-copy {
    min-width: 0;
}

.crm-ui-table-toolbar-copy h2,
.crm-ui-table-toolbar-copy h3 {
    margin: 0 0 4px;
    color: var(--text-primary);
    font-size: 1rem;
    line-height: 1.2;
}

.crm-ui-table-toolbar-copy p {
    margin: 0;
    color: var(--text-muted);
    font-size: .86rem;
}

.crm-ui-bulkbar-summary {
    min-width: 0;
    display: flex;
    align-items: center;
    gap: 14px;
    flex-wrap: wrap;
}

.crm-ui-bulkbar-actions {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 8px;
    flex-wrap: nowrap;
}

.crm-ui-table-scroll {
    flex: 1 1 auto;
    min-height: 0;
    width: 100%;
    max-width: 100%;
    overflow-y: auto;
    overflow-x: hidden;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    border: 0;
    border-radius: 0;
    background: var(--surface-1);
}

.crm-ui-table {
    width: 100%;
    min-width: 0;
    max-width: 100%;
    table-layout: fixed;
    border-collapse: collapse;
}

.crm-ui-table th,
.crm-ui-table td {
    min-width: 0;
    padding: 12px;
    vertical-align: middle;
    box-sizing: border-box;
    border-bottom: 1px solid var(--border-subtle);
}

.crm-ui-table th {
    position: sticky;
    top: 0;
    z-index: 12;
    background: var(--surface-2);
    color: var(--text-muted);
    font-size: .75rem;
    font-weight: 700;
    line-height: 1.1;
    text-transform: uppercase;
    letter-spacing: .06em;
    white-space: nowrap;
}

.crm-ui-table td {
    color: var(--text-primary);
    overflow: hidden;
    text-overflow: ellipsis;
}

.crm-ui-table tbody tr:hover td {
    background: var(--surface-2);
}

.crm-ui-table td small,
.crm-ui-cell-subtitle {
    display: block;
    width: 100%;
    min-width: 0;
    margin-top: 3px;
    color: var(--text-muted);
    font-size: .75rem;
    line-height: 1.2;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.crm-ui-cell-stack {
    display: flex;
    min-width: 0;
    width: 100%;
    flex-direction: column;
    justify-content: center;
    gap: 2px;
    overflow: hidden;
}

.crm-ui-cell-stack > * {
    min-width: 0;
    max-width: 100%;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.crm-ui-action-cell {
    text-align: center !important;
}

.crm-ui-action-btn {
    width: 36px !important;
    min-width: 36px !important;
    max-width: 36px !important;
    height: 36px !important;
    min-height: 36px !important;
    padding: 0 !important;
    flex: 0 0 36px !important;
    border: 1px solid var(--border-default) !important;
    border-radius: var(--radius-md) !important;
    background: var(--surface-2) !important;
    color: var(--text-primary) !important;
}

.crm-ui-action-btn:hover {
    background: var(--surface-3) !important;
    border-color: var(--border-strong) !important;
}

.crm-ui-action-btn svg,
.crm-ui-action-btn i {
    width: 18px;
    height: 18px;
}

body.is-modal-open,
body.crm-ui-modal-open {
    overflow: hidden !important;
    overscroll-behavior: none;
}

/* Modal contract: exact Opportunity geometry ----------------------------- */
.crm-ui-modal-backdrop {
    position: fixed !important;
    inset: 0 !important;
    z-index: 1050 !important;
    padding: 24px !important;
    align-items: center !important;
    justify-content: center !important;
    overflow: hidden !important;
    background: rgba(0, 0, 0, .68) !important;
    backdrop-filter: blur(5px) !important;
}

.crm-ui-modal-backdrop.show {
    display: flex !important;
    opacity: 1 !important;
}

.crm-ui-modal-box {
    width: min(1120px, calc(100vw - 48px)) !important;
    max-width: calc(100vw - 48px) !important;
    height: min(700px, calc(100dvh - 100px)) !important;
    min-height: min(700px, calc(100dvh - 100px)) !important;
    max-height: calc(100dvh - 100px) !important;
    display: flex !important;
    flex-direction: column !important;
    overflow: hidden !important;
    border: 1px solid var(--border-default) !important;
    border-radius: var(--radius-xl) !important;
    background: var(--surface-1) !important;
    box-shadow: var(--shadow-lg) !important;
}

.crm-ui-modal-layer {
    flex: 1 1 auto;
    width: 100%;
    min-height: 0;
    height: 100%;
    overflow: hidden;
    display: none;
    flex-direction: column;
}

.crm-ui-modal-layer.active {
    display: flex;
}

.crm-ui-modal-header {
    flex: 0 0 auto !important;
    min-height: 78px !important;
    padding: 20px 24px !important;
    display: grid !important;
    grid-template-columns: 46px minmax(0, 1fr) 46px !important;
    align-items: flex-start !important;
    gap: 14px !important;
    border-bottom: 1px solid var(--border-subtle) !important;
    background: var(--surface-1) !important;
}

.crm-ui-modal-header--no-back {
    grid-template-columns: minmax(0, 1fr) 46px !important;
}

.crm-ui-modal-heading {
    min-width: 0;
    max-width: 100%;
}

.crm-ui-modal-heading h2,
.crm-ui-modal-heading h3 {
    margin: 0 !important;
    color: var(--text-primary) !important;
    font-size: clamp(1.125rem, 2vw, 1.5rem) !important;
    font-weight: 600 !important;
    line-height: 1.14 !important;
    letter-spacing: -.02em !important;
    white-space: normal !important;
    word-break: normal !important;
    overflow-wrap: break-word !important;
}

.crm-ui-modal-heading p {
    margin: 3px 0 0 !important;
    color: var(--text-muted) !important;
    font-size: .875rem !important;
    line-height: 1.2 !important;
}

.crm-ui-modal-icon-btn {
    width: 42px !important;
    min-width: 42px !important;
    max-width: 42px !important;
    height: 42px !important;
    min-height: 42px !important;
    padding: 0 !important;
    border: 1px solid var(--border-default) !important;
    border-radius: var(--radius-lg) !important;
    background: transparent !important;
    color: var(--text-primary) !important;
    box-shadow: none !important;
}

.crm-ui-modal-icon-btn:hover {
    background: var(--surface-2) !important;
    border-color: var(--border-strong) !important;
}

.crm-ui-modal-body {
    flex: 1 1 auto !important;
    min-height: 0 !important;
    max-height: none !important;
    padding: 20px 24px !important;
    overflow-y: auto !important;
    overflow-x: hidden !important;
    overscroll-behavior: contain !important;
    -webkit-overflow-scrolling: touch !important;
    background: var(--surface-0) !important;
}

.crm-ui-modal-footer {
    flex: 0 0 auto !important;
    width: 100% !important;
    min-height: 76px !important;
    height: auto !important;
    max-height: none !important;
    padding: 14px 20px !important;
    display: flex !important;
    flex-direction: row !important;
    align-items: center !important;
    justify-content: flex-end !important;
    gap: 12px !important;
    flex-wrap: nowrap !important;
    overflow: visible !important;
    background: var(--surface-0) !important;
    border-top: 1px solid var(--border-subtle) !important;
    border-radius: 0 0 var(--radius-xl) var(--radius-xl) !important;
}

.crm-ui-modal-footer:empty {
    display: none !important;
}

.crm-ui-modal-footer:has(.modal-footer-actions-left) {
    justify-content: space-between !important;
}

.crm-ui-modal-footer .modal-footer-actions-left,
.crm-ui-modal-footer .modal-footer-actions-right {
    min-width: 0;
    display: flex;
    align-items: center;
    gap: 10px;
    flex-wrap: nowrap;
    overflow: visible;
}

.crm-ui-modal-footer .modal-footer-actions-left {
    margin-right: auto;
    justify-content: flex-start;
    flex: 0 0 auto;
}

.crm-ui-modal-footer .modal-footer-actions-right {
    margin-left: auto;
    justify-content: flex-end;
    flex: 1 1 auto;
}

.crm-ui-modal-footer .btn {
    width: auto !important;
    min-width: 118px !important;
    max-width: none !important;
    height: 48px !important;
    min-height: 48px !important;
    padding: 0 18px !important;
    display: inline-flex !important;
    align-items: center !important;
    justify-content: center !important;
    gap: 8px !important;
    flex: 0 0 auto !important;
    white-space: nowrap !important;
    overflow: visible !important;
    text-overflow: clip !important;
    border-radius: var(--radius-lg) !important;
}

.crm-ui-modal-footer .btn-primary,
.crm-ui-modal-footer .btn-success,
.crm-ui-modal-footer .btn-danger {
    min-width: 178px !important;
}

/* Shared inner cards/forms ------------------------------------------------ */
.crm-ui-section {
    min-width: 0;
    padding: 18px 20px;
    border: 1px solid var(--border-default);
    border-radius: var(--radius-xl);
    background: var(--surface-1);
    box-shadow: var(--shadow-sm);
}

.crm-ui-section h3 {
    margin: 0 0 14px;
    color: var(--text-primary);
    font-size: 1.05rem;
    line-height: 1.2;
}

.crm-ui-summary-grid {
    display: grid;
    grid-template-columns: repeat(var(--crm-summary-columns, 4), minmax(0, 1fr));
    gap: 12px;
    margin-bottom: 14px;
}

.crm-ui-summary-card {
    min-width: 0;
    padding: 16px;
    border: 1px solid var(--border-default);
    border-radius: var(--radius-xl);
    background: var(--surface-1);
    box-shadow: var(--shadow-sm);
}

.crm-ui-summary-card span {
    display: block;
    color: var(--text-muted);
    font-size: .75rem;
    font-weight: 800;
    letter-spacing: .04em;
    text-transform: uppercase;
}

.crm-ui-summary-card strong {
    display: block;
    margin-top: 6px;
    color: var(--text-primary);
    font-size: 1.12rem;
    overflow-wrap: anywhere;
}

.crm-ui-form-grid {
    display: grid;
    grid-template-columns: repeat(var(--crm-form-columns, 2), minmax(0, 1fr));
    gap: 14px;
}

.crm-ui-field {
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin-bottom: 12px;
}

.crm-ui-field label,
.crm-ui-field > span:first-child {
    color: var(--text-secondary);
    font-size: .78rem;
    font-weight: 800;
    letter-spacing: .04em;
    text-transform: uppercase;
}

.crm-ui-field input,
.crm-ui-field select,
.crm-ui-field textarea {
    width: 100%;
    min-width: 0;
    min-height: 44px;
    padding: 10px 12px;
    border: 1px solid var(--border-default);
    border-radius: var(--radius-lg);
    background: var(--surface-1);
    color: var(--text-primary);
    font: inherit;
}

.crm-ui-inner-table-wrap {
    width: 100%;
    max-width: 100%;
    overflow: auto;
    border: 1px solid var(--border-default);
    border-radius: var(--radius-xl);
    background: var(--surface-1);
}

.crm-ui-inner-table {
    width: 100%;
    border-collapse: collapse;
    background: var(--surface-1);
}

.crm-ui-inner-table th {
    padding: 12px 14px;
    border-bottom: 1px solid var(--border-default);
    background: var(--surface-2);
    color: var(--text-secondary);
    font-size: .75rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: .04em;
    white-space: nowrap;
}

.crm-ui-inner-table td {
    padding: 12px 14px;
    border-bottom: 1px solid var(--border-subtle);
    color: var(--text-primary);
    vertical-align: middle;
}

/* Responsive: exact Cotizaciones table/page composition ------------------ */
@media (max-width: 1280px) {
    .crm-ui-kpis[style*="--crm-kpi-columns: 6"],
    .crm-ui-kpis[style*="--crm-kpi-columns:6"] {
        grid-template-columns: repeat(3, minmax(0, 1fr));
    }
}

@media (max-width: 980px), (hover: none) and (pointer: coarse) {
    .crm-ui-main {
        max-width: 100vw;
        overflow: hidden;
    }

    .crm-ui-main * {
        box-sizing: border-box;
    }

    .crm-ui-page-content {
        overflow: hidden;
    }

    .crm-ui-page {
        height: calc(100dvh - var(--topbar-height, 72px));
        max-height: calc(100dvh - var(--topbar-height, 72px));
        min-height: 0;
        padding: var(--space-3);
        overflow: hidden;
    }

    .crm-ui-page-header {
        display: grid;
        grid-template-columns: minmax(0, 1fr) auto;
        align-items: start;
        gap: 12px;
        width: 100%;
        margin-bottom: var(--space-4);
    }

    .crm-ui-page-header-copy {
        grid-column: 1 / 2;
        min-width: 0;
        max-width: 100%;
    }

    .crm-ui-page-header-copy .label {
        margin-bottom: 6px;
        font-size: .8rem;
        line-height: 1.1;
    }

    .crm-ui-page-header-copy h1,
    .crm-ui-page-header-copy h2 {
        margin: 0 0 8px;
        font-size: 1.55rem;
        line-height: 1.15;
    }

    .crm-ui-page-header-copy p {
        margin: 0;
        font-size: .95rem;
        line-height: 1.35;
        color: var(--text-secondary);
    }

    .crm-ui-page-actions {
        grid-column: 2 / 3;
        align-items: flex-start;
        justify-content: flex-end;
        gap: 8px;
        width: auto;
        min-width: 42px;
        max-width: max-content;
        padding-top: 34px;
        margin: 0;
    }

    .crm-ui-page-actions .btn {
        width: 42px;
        min-width: 42px;
        max-width: 42px;
        height: 42px;
        min-height: 42px;
        padding: 0;
        flex: 0 0 42px;
        border-radius: var(--radius-md);
        font-size: 0;
    }

    .crm-ui-page-actions .btn span {
        display: none;
    }

    .crm-ui-page-actions .btn svg,
    .crm-ui-page-actions .btn i {
        width: 18px;
        height: 18px;
        margin: 0;
    }

    .crm-ui-mobile-hidden-control {
        display: none !important;
    }

    .crm-ui-env-select {
        width: 104px;
        min-width: 104px;
        max-width: 104px;
        height: 42px;
        min-height: 42px;
        padding: 0 30px 0 10px;
        font-size: .78rem;
    }

    .crm-ui-filter {
        display: grid;
        grid-template-columns: minmax(0, 1fr);
        gap: 8px;
        width: 100%;
        max-width: 100%;
        margin: 0 0 var(--space-3);
        padding: 0;
        overflow: visible;
        background: transparent;
        border: 0;
        border-radius: 0;
        box-shadow: none;
    }

    .crm-ui-filter input,
    .crm-ui-filter select,
    .crm-ui-input {
        width: 100%;
        max-width: 100%;
        height: 44px;
        min-height: 44px;
    }

    .crm-ui-date-group {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 8px;
    }

    .crm-ui-date-field > span {
        display: block;
        margin-bottom: 4px;
        color: var(--text-muted);
        font-size: .68rem;
        font-weight: 800;
        line-height: 1;
        letter-spacing: .04em;
        text-transform: uppercase;
    }

    .crm-ui-date-field > input {
        padding-left: 12px;
        padding-right: 12px;
        color: var(--text-primary);
        background: var(--surface-1);
    }

    .crm-ui-date-field > em {
        position: absolute;
        left: 12px;
        bottom: 15px;
        display: block;
        color: var(--text-muted);
        font-size: .88rem;
        font-style: normal;
        line-height: 1;
        opacity: .68;
        pointer-events: none;
    }

    .crm-ui-date-field.has-value > em {
        display: none;
    }

    .crm-ui-filter-actions {
        width: 100%;
        justify-content: stretch;
    }

    .crm-ui-filter-actions .btn {
        flex: 1 1 auto;
    }

    .crm-ui-kpis {
        display: none !important;
        visibility: hidden !important;
        height: 0 !important;
        min-height: 0 !important;
        max-height: 0 !important;
        margin: 0 !important;
        padding: 0 !important;
        overflow: hidden !important;
    }

    .crm-ui-table-card {
        flex: 1 1 auto;
        min-height: 0;
        width: 100%;
        max-width: 100%;
        overflow: hidden;
        border-radius: var(--radius-xl);
    }

    .crm-ui-table-toolbar,
    .crm-ui-bulkbar {
        padding: 10px 12px;
        gap: 8px;
    }

    .crm-ui-table-toolbar-copy p {
        display: none;
    }

    .crm-ui-table-scroll {
        flex: 1 1 auto;
        min-height: 0;
        width: 100%;
        max-width: 100%;
        overflow-x: hidden;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }

    .crm-ui-mobile-table,
    .crm-ui-mobile-table thead,
    .crm-ui-mobile-table tbody {
        display: block;
        width: 100%;
        min-width: 0;
        max-width: 100%;
        border-collapse: collapse;
        table-layout: fixed;
    }

    .crm-ui-mobile-table colgroup,
    .crm-ui-mobile-table .crm-mobile-hidden {
        display: none !important;
        width: 0 !important;
        max-width: 0 !important;
        padding: 0 !important;
        border: 0 !important;
        overflow: hidden !important;
    }

    .crm-ui-mobile-table thead tr,
    .crm-ui-mobile-table tbody tr {
        display: grid;
        grid-template-columns: var(--crm-mobile-columns, 46px minmax(0, 1fr) 94px 64px);
        width: 100%;
        min-width: 0;
        max-width: 100%;
        align-items: stretch;
        gap: 0;
        box-sizing: border-box;
    }

    .crm-ui-mobile-table thead tr {
        min-height: 42px;
        border-bottom: 1px solid var(--border-subtle);
    }

    .crm-ui-mobile-table tbody tr {
        min-height: 72px;
        border-bottom: 1px solid var(--border-subtle);
    }

    .crm-ui-mobile-table th,
    .crm-ui-mobile-table td {
        display: flex;
        align-items: center;
        align-self: stretch;
        width: auto !important;
        min-width: 0 !important;
        max-width: 100% !important;
        margin: 0 !important;
        padding: 10px 8px !important;
        border: 0 !important;
        overflow: hidden !important;
        white-space: nowrap !important;
    }

    .crm-ui-mobile-table thead th {
        position: static;
        min-height: 42px;
        font-size: .69rem;
        line-height: 1;
        letter-spacing: .04em;
        color: var(--text-muted);
        background: var(--surface-2);
    }

    .crm-ui-mobile-table tbody td {
        min-height: 72px;
    }

    .crm-ui-mobile-table td[colspan],
    .crm-ui-mobile-table th[colspan] {
        grid-column: 1 / -1;
        width: 100% !important;
        justify-content: center;
        white-space: normal !important;
    }

    .crm-ui-mobile-table .crm-ui-action-cell {
        justify-content: center;
        text-align: center;
        padding-left: 4px !important;
        padding-right: 4px !important;
        overflow: visible !important;
    }

    .crm-ui-mobile-table .crm-ui-action-btn {
        width: 40px !important;
        min-width: 40px !important;
        max-width: 40px !important;
        height: 40px !important;
        min-height: 40px !important;
        flex: 0 0 40px !important;
    }

    .crm-ui-mobile-table .crm-mobile-primary,
    .crm-ui-mobile-table .crm-mobile-metric {
        min-width: 0;
    }

    .crm-ui-mobile-table .crm-mobile-metric {
        justify-content: flex-end;
        text-align: right;
    }

    .crm-ui-modal-backdrop {
        padding: 12px !important;
        align-items: center !important;
        justify-content: center !important;
    }

    .crm-ui-modal-box {
        width: calc(100vw - 24px) !important;
        max-width: calc(100vw - 24px) !important;
        height: calc(100dvh - 200px) !important;
        min-height: 0 !important;
        max-height: calc(100dvh - 200px) !important;
        border-radius: 18px !important;
    }

    .crm-ui-modal-header {
        min-height: 64px !important;
        padding: 14px !important;
        grid-template-columns: 42px minmax(0, 1fr) 42px !important;
        gap: 10px !important;
    }

    .crm-ui-modal-header--no-back {
        grid-template-columns: minmax(0, 1fr) 42px !important;
    }

    .crm-ui-modal-heading h2,
    .crm-ui-modal-heading h3 {
        font-size: 1.05rem !important;
        line-height: 1.16 !important;
    }

    .crm-ui-modal-heading p {
        margin-top: 2px !important;
        font-size: .78rem !important;
        line-height: 1.15 !important;
        white-space: nowrap !important;
        overflow: hidden !important;
        text-overflow: ellipsis !important;
    }

    .crm-ui-modal-body {
        padding: 14px !important;
    }

    .crm-ui-modal-footer {
        min-height: 68px !important;
        padding: 10px 12px calc(10px + env(safe-area-inset-bottom)) !important;
        gap: 10px !important;
    }

    .crm-ui-modal-footer .btn {
        height: 44px !important;
        min-height: 44px !important;
        min-width: 106px !important;
        padding: 0 14px !important;
        font-size: .94rem !important;
        flex: 0 1 auto !important;
    }

    .crm-ui-modal-footer .btn-primary,
    .crm-ui-modal-footer .btn-success,
    .crm-ui-modal-footer .btn-danger {
        min-width: 124px !important;
    }

    .crm-ui-summary-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .crm-ui-form-grid {
        grid-template-columns: minmax(0, 1fr);
    }

    .crm-ui-section,
    .crm-ui-summary-card {
        padding: 14px;
        border-radius: 16px;
    }
}

@media (max-width: 760px) and (max-height: 700px) {
    .crm-ui-modal-box {
        height: calc(100dvh - 96px) !important;
        max-height: calc(100dvh - 96px) !important;
    }
}

@media (max-width: 340px) {
    .crm-ui-summary-grid {
        grid-template-columns: minmax(0, 1fr);
    }
}

/* ========================================================================== 
   CRM visual authority layer
   Deliberately uses higher specificity to make the shared CRM contract the
   effective source of truth over legacy module selectors and inline patches.
   This is not a second component definition: it is the compatibility bridge
   while the remaining CRM modules are progressively migrated to these classes.
   ========================================================================== */
body .app-main.crm-ui-main {
    height: 100vh !important;
    height: 100dvh !important;
    min-height: 0 !important;
    overflow: hidden !important;
    display: flex !important;
    flex-direction: column !important;
}

body .app-main.crm-ui-main > .crm-ui-page-content.crm-ui-page {
    flex: 1 1 0% !important;
    min-height: 0 !important;
    height: auto !important;
    max-height: none !important;
    width: 100% !important;
    max-width: var(--container-max) !important;
    display: flex !important;
    flex-direction: column !important;
    overflow: hidden !important;
    padding-top: var(--space-5) !important;
    padding-bottom: var(--space-5) !important;
}

body .app-main.crm-ui-main .crm-ui-page-header,
body .app-main.crm-ui-main .crm-ui-filter,
body .app-main.crm-ui-main .crm-ui-kpis {
    flex: 0 0 auto !important;
}

body .app-main.crm-ui-main .crm-ui-table-card {
    flex: 1 1 0% !important;
    min-height: 0 !important;
    height: auto !important;
    max-height: none !important;
    display: flex !important;
    flex-direction: column !important;
    overflow: hidden !important;
}

body .app-main.crm-ui-main .crm-ui-table-scroll,
body .app-main.crm-ui-main .table-wrap.crm-ui-table-scroll {
    flex: 1 1 auto !important;
    min-height: 0 !important;
    height: auto !important;
    max-height: none !important;
    overflow-y: auto !important;
    overflow-x: hidden !important;
    overscroll-behavior: contain !important;
    -webkit-overflow-scrolling: touch !important;
}

body .app-main.crm-ui-main .crm-ui-table {
    width: 100% !important;
    min-width: 0 !important;
    max-width: 100% !important;
    margin-bottom: 0 !important;
    table-layout: fixed !important;
}

body .crm-ui-modal-backdrop {
    position: fixed !important;
    inset: 0 !important;
    display: none;
    align-items: center !important;
    justify-content: center !important;
    padding: 24px !important;
    overflow: hidden !important;
    background: rgba(0, 0, 0, .68) !important;
    backdrop-filter: blur(5px) !important;
    z-index: 1050 !important;
}

body .crm-ui-modal-backdrop.show {
    display: flex !important;
    opacity: 1 !important;
}

body .crm-ui-modal-backdrop > .crm-ui-modal-box {
    width: min(1120px, calc(100vw - 48px)) !important;
    max-width: calc(100vw - 48px) !important;
    height: min(700px, calc(100dvh - 100px)) !important;
    min-height: min(700px, calc(100dvh - 100px)) !important;
    max-height: calc(100dvh - 100px) !important;
    display: flex !important;
    flex-direction: column !important;
    overflow: hidden !important;
}

body .crm-ui-modal-backdrop .crm-ui-modal-body {
    flex: 1 1 auto !important;
    min-height: 0 !important;
    height: auto !important;
    max-height: none !important;
    padding: 20px 24px !important;
    overflow-y: auto !important;
    overflow-x: hidden !important;
    overscroll-behavior: contain !important;
    -webkit-overflow-scrolling: touch !important;
}

body .crm-ui-modal-backdrop .crm-ui-modal-footer {
    flex: 0 0 auto !important;
    width: 100% !important;
    min-height: 76px !important;
    height: auto !important;
    max-height: none !important;
    padding: 14px 20px !important;
    display: flex !important;
    grid-template-columns: none !important;
    flex-direction: row !important;
    align-items: center !important;
    justify-content: flex-end !important;
    gap: 12px !important;
    flex-wrap: nowrap !important;
    overflow: visible !important;
}

body .crm-ui-modal-backdrop .crm-ui-modal-footer .modal-footer-actions-left {
    flex: 0 0 auto !important;
    width: auto !important;
    min-width: 0 !important;
    margin-right: auto !important;
}

body .crm-ui-modal-backdrop .crm-ui-modal-footer .modal-footer-actions-right {
    flex: 0 1 auto !important;
    width: auto !important;
    min-width: 0 !important;
    margin-left: auto !important;
    justify-content: flex-end !important;
}

body .crm-ui-modal-backdrop .crm-ui-modal-footer .btn {
    width: auto !important;
    min-width: 118px !important;
    max-width: none !important;
    height: 48px !important;
    min-height: 48px !important;
    padding: 0 18px !important;
    flex: 0 0 auto !important;
}

body .crm-ui-modal-backdrop .crm-ui-modal-footer .btn-primary,
body .crm-ui-modal-backdrop .crm-ui-modal-footer .btn-success,
body .crm-ui-modal-backdrop .crm-ui-modal-footer .btn-danger {
    min-width: 178px !important;
}

@media (max-width: 980px), (hover: none) and (pointer: coarse) {
    body .app-main.crm-ui-main > .crm-ui-page-content.crm-ui-page {
        height: calc(100dvh - var(--topbar-height, 72px)) !important;
        max-height: calc(100dvh - var(--topbar-height, 72px)) !important;
        min-height: 0 !important;
        max-width: 100% !important;
        padding: var(--space-3) !important;
        overflow: hidden !important;
    }

    body .app-main.crm-ui-main .crm-ui-page-header {
        display: grid !important;
        grid-template-columns: minmax(0, 1fr) auto !important;
        align-items: start !important;
        gap: 12px !important;
        width: 100% !important;
        margin-bottom: var(--space-4) !important;
    }

    body .app-main.crm-ui-main .crm-ui-page-header-copy {
        display: block !important;
        grid-column: 1 / 2 !important;
        min-width: 0 !important;
        max-width: 100% !important;
    }

    body .app-main.crm-ui-main .crm-ui-page-header-copy .label {
        display: block !important;
        margin: 0 0 6px !important;
        font-size: .8rem !important;
        line-height: 1.1 !important;
    }

    body .app-main.crm-ui-main .crm-ui-page-header-copy h1,
    body .app-main.crm-ui-main .crm-ui-page-header-copy h2 {
        display: block !important;
        margin: 0 0 8px !important;
        font-size: 1.55rem !important;
        line-height: 1.15 !important;
    }

    body .app-main.crm-ui-main .crm-ui-page-header-copy p {
        display: block !important;
        margin: 0 !important;
        font-size: .95rem !important;
        line-height: 1.35 !important;
        color: var(--text-secondary) !important;
    }

    body .app-main.crm-ui-main .crm-ui-page-actions,
    body .app-main.crm-ui-main .top-actions-cluster.crm-ui-page-actions {
        display: flex !important;
        grid-column: 2 / 3 !important;
        align-items: flex-start !important;
        justify-content: flex-end !important;
        gap: 8px !important;
        width: auto !important;
        min-width: 42px !important;
        max-width: max-content !important;
        padding-top: 34px !important;
        margin: 0 !important;
        flex: 0 0 auto !important;
    }

    body .app-main.crm-ui-main .crm-ui-filter,
    body .app-main.crm-ui-main .filter-bar-container.crm-ui-filter {
        display: grid !important;
        grid-template-columns: minmax(0, 1fr) !important;
        gap: 8px !important;
        width: 100% !important;
        max-width: 100% !important;
        margin: 0 0 var(--space-3) !important;
        padding: 0 !important;
        overflow: visible !important;
        background: transparent !important;
        border: 0 !important;
        border-radius: 0 !important;
        box-shadow: none !important;
    }

    body .app-main.crm-ui-main .crm-ui-date-group {
        display: grid !important;
        grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
        gap: 8px !important;
    }

    body .app-main.crm-ui-main .crm-ui-kpis {
        display: none !important;
        visibility: hidden !important;
        height: 0 !important;
        min-height: 0 !important;
        max-height: 0 !important;
        margin: 0 !important;
        padding: 0 !important;
        overflow: hidden !important;
    }

    body .app-main.crm-ui-main .crm-ui-table-card {
        flex: 1 1 0 !important;
        min-height: 0 !important;
        height: auto !important;
        max-height: none !important;
        overflow: hidden !important;
    }

    body .app-main.crm-ui-main .crm-ui-table-scroll,
    body .app-main.crm-ui-main .table-wrap.crm-ui-table-scroll {
        flex: 1 1 auto !important;
        min-height: 0 !important;
        height: auto !important;
        max-height: none !important;
        overflow-y: auto !important;
        overflow-x: hidden !important;
    }

    body .crm-ui-modal-backdrop {
        align-items: center !important;
        justify-content: center !important;
        padding: 12px !important;
    }

    body .crm-ui-modal-backdrop > .crm-ui-modal-box {
        width: calc(100vw - 24px) !important;
        max-width: calc(100vw - 24px) !important;
        height: calc(100dvh - 200px) !important;
        min-height: 0 !important;
        max-height: calc(100dvh - 200px) !important;
        border-radius: 18px !important;
    }

    body .crm-ui-modal-backdrop .crm-ui-modal-header {
        min-height: 64px !important;
        padding: 14px !important;
        gap: 10px !important;
    }

    body .crm-ui-modal-backdrop .crm-ui-modal-body {
        min-height: 0 !important;
        height: auto !important;
        max-height: none !important;
        padding: 14px !important;
        overflow-y: auto !important;
        overflow-x: hidden !important;
    }

    body .crm-ui-modal-backdrop .crm-ui-modal-footer {
        min-height: 68px !important;
        height: auto !important;
        max-height: none !important;
        padding: 10px 12px calc(10px + env(safe-area-inset-bottom)) !important;
        display: flex !important;
        grid-template-columns: none !important;
        gap: 10px !important;
        overflow: visible !important;
    }

    body .crm-ui-modal-backdrop .crm-ui-modal-footer .modal-footer-actions-right {
        width: auto !important;
        flex: 0 1 auto !important;
        gap: 10px !important;
    }

    body .crm-ui-modal-backdrop .crm-ui-modal-footer .btn {
        width: auto !important;
        min-width: 106px !important;
        max-width: none !important;
        height: 44px !important;
        min-height: 44px !important;
        padding: 0 14px !important;
        font-size: .94rem !important;
        flex: 0 0 auto !important;
    }

    body .crm-ui-modal-backdrop .crm-ui-modal-footer .btn-primary,
    body .crm-ui-modal-backdrop .crm-ui-modal-footer .btn-success,
    body .crm-ui-modal-backdrop .crm-ui-modal-footer .btn-danger {
        min-width: 124px !important;
    }
}

/* Compact CRM viewport contract -----------------------------------------
   Same component system as Cotizaciones, condensed only when the usable
   viewport is too short to preserve an internal table viewport. */
@media (max-width: 760px) and (max-height: 700px) {
    body .app-main.crm-ui-main > .crm-ui-page-content.crm-ui-page {
        padding-top: 8px !important;
        padding-bottom: 8px !important;
    }

    body .app-main.crm-ui-main .crm-ui-page-header {
        align-items: center !important;
        margin-bottom: 8px !important;
        gap: 8px !important;
    }

    body .app-main.crm-ui-main .crm-ui-page-header-copy .label,
    body .app-main.crm-ui-main .crm-ui-page-header-copy p {
        display: none !important;
    }

    body .app-main.crm-ui-main .crm-ui-page-header-copy h1,
    body .app-main.crm-ui-main .crm-ui-page-header-copy h2 {
        margin: 0 !important;
        font-size: 1.32rem !important;
        line-height: 1.08 !important;
    }

    body .app-main.crm-ui-main .crm-ui-page-actions,
    body .app-main.crm-ui-main .top-actions-cluster.crm-ui-page-actions {
        align-items: center !important;
        padding-top: 0 !important;
    }

    body .app-main.crm-ui-main .crm-ui-filter,
    body .app-main.crm-ui-main .filter-bar-container.crm-ui-filter {
        gap: 6px !important;
        margin-bottom: 8px !important;
    }

    body .app-main.crm-ui-main .crm-ui-filter input,
    body .app-main.crm-ui-main .crm-ui-filter select,
    body .app-main.crm-ui-main .crm-ui-input {
        height: 40px !important;
        min-height: 40px !important;
    }

    body .app-main.crm-ui-main .crm-ui-date-field > span {
        margin-bottom: 2px !important;
        font-size: .62rem !important;
    }

    body .app-main.crm-ui-main .crm-ui-date-field > em {
        bottom: 13px !important;
        font-size: .78rem !important;
    }

    body .app-main.crm-ui-main .crm-ui-bulkbar,
    body .app-main.crm-ui-main .crm-ui-table-toolbar {
        min-height: 52px !important;
        padding: 6px 8px !important;
        gap: 6px !important;
    }

    body .app-main.crm-ui-main .crm-ui-bulkbar-summary {
        gap: 2px 8px !important;
        font-size: .8rem !important;
        line-height: 1.15 !important;
    }

    body .app-main.crm-ui-main .crm-ui-bulkbar-actions {
        gap: 6px !important;
    }

    body .app-main.crm-ui-main .crm-ui-bulkbar-actions .btn {
        width: 40px !important;
        min-width: 40px !important;
        max-width: 40px !important;
        height: 40px !important;
        min-height: 40px !important;
        padding: 0 !important;
    }
}

/* Short mobile viewports keep the same CRM modal shell, with more usable body height. */
@media (max-width: 760px) and (max-height: 700px) {
    body .crm-ui-modal-backdrop > .crm-ui-modal-box {
        height: calc(100dvh - 96px) !important;
        max-height: calc(100dvh - 96px) !important;
    }
}

/* CRM Clientes v7.2.6: ciclo vigente manual o recalculado desde FACTO. */
.customer-restock-control {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 48px;
    gap: 8px;
    align-items: center;
}

.customer-restock-control .customer-restock-refresh {
    width: 48px;
    min-width: 48px;
    height: 48px;
}

.customer-restock-control .customer-restock-refresh.is-loading svg {
    animation: customer-restock-spin .8s linear infinite;
}

@keyframes customer-restock-spin {
    to { transform: rotate(360deg); }
}

/* CRM Clientes v7.2.7: formularios compactos y consistentes. */
.customer-quick-grid--polished {
    column-gap: 20px;
    row-gap: 20px;
}

.customer-quick-grid--polished .form-field {
    min-width: 0;
    align-self: start;
}

.customer-quick-grid--polished .label {
    display: block;
    min-height: 18px;
    margin-bottom: 8px;
    line-height: 1.2;
}

.customer-quick-grid--polished .input,
.customer-quick-grid--polished .select {
    width: 100%;
    height: 48px;
    min-height: 48px;
    border-radius: 14px;
}

.customer-quick-grid--polished .field-help {
    display: block;
    margin-top: 6px;
    color: var(--text-muted);
    font-size: .75rem;
    line-height: 1.3;
}

#customers-main #customerModal .product-modal-body,
#customer-view-main #customerEditModal .product-modal-body {
    scrollbar-width: thin;
    scrollbar-color: var(--border-strong) transparent;
}

#customers-main #customerModal .product-modal-body::-webkit-scrollbar,
#customer-view-main #customerEditModal .product-modal-body::-webkit-scrollbar {
    width: 8px;
}

#customers-main #customerModal .product-modal-body::-webkit-scrollbar-thumb,
#customer-view-main #customerEditModal .product-modal-body::-webkit-scrollbar-thumb {
    background: var(--border-strong);
    border-radius: 999px;
}

@media (max-width: 760px) {
    .customer-quick-grid--polished {
        row-gap: 16px;
    }
}
