.af-toolbar {
    display: flex;
    justify-content: flex-end;
    margin-bottom: 12px;
}

.af-sheet {
    container: affiliation-sheet / inline-size;
}

[data-af-price-preview-result] {
    display: block;
    width: 100%;
    font-size: .75rem;
    color: var(--muted);
    margin-top: 4px;
}
[data-af-price-preview] { flex-wrap: wrap; }
[data-af-price-preview-result][hidden] { display: none; }

.af-sheet .af-panel {
    min-width: 0;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 14px;
    padding: clamp(16px, 2cqw, 24px);
    margin-bottom: 16px;
}

.af-section-heading {
    margin-bottom: 16px;
}

.af-section-heading h2 {
    margin: 0;
    color: var(--text);
    font-size: 1.2rem;
    font-weight: 700;
    line-height: 1.3;
}

.af-editor-columns {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(17rem, 0.46fr);
    grid-template-areas: "economics options";
    column-gap: 16px;
    align-items: start;
}

.af-editor-economics {
    grid-area: economics;
    min-width: 0;
}


.af-editor-side {
    grid-area: options;
    min-width: 0;
}

#content .af-options #content .af-panel .af-checks.af-options {
    display: grid;
    gap: 4px;
}

.af-check {
    justify-content: space-between;
    flex-direction: row-reverse;
    min-height: 40px;
    padding: 6px 10px;
    border-radius: 8px;
    background: var(--surface-soft);
}

.af-options input[type=checkbox] {
    appearance: none;
    flex: 0 0 32px;
    width: 32px;
    height: 18px;
    margin: 0;
    border: 1px solid var(--border);
    border-radius: 999px;
    background: var(--surface);
    cursor: pointer;
    display: flex;
    align-items: center;
    padding: 2px;
}

.af-options input[type=checkbox]::before {
    content: '';
    width: 12px;
    height: 12px;
    border-radius: 50%;
    background: var(--muted);
}

.af-options input[type=checkbox]:checked {
    justify-content: flex-end;
    background: var(--accent);
    border-color: var(--accent);
}

.af-options input[type=checkbox]:checked::before {
    background: var(--surface);
}

.af-section-content,
.af-field,
.af-link-groups > div {
    min-width: 0;
}

.af-link-groups {
    display: grid;
    gap: 12px;
}

.af-assignment-card {
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 12px;
    min-width: 0;
}

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

.af-assignment-heading h3 {
    margin: 0;
}

.af-assignment-summary {
    margin: 10px 0;
}

.af-assignment-editor {
    margin-top: 12px;
}

.af-assignment-editor[hidden] {
    display: none;
}

.af-assignment-editor > label {
    display: block;
    margin: 8px 0;
}

.af-assignment-editor input[type="search"] {
    display: block;
    max-width: 22rem;
    width: 100%;
}

.af-assignment-editor [data-af-assignment-row][hidden] {
    display: none;
}

.af-row {
    display: grid;
    align-items: end;
    justify-content: start;
    gap: 12px;
}

.af-row + .af-row {
    margin-top: 14px;
}

/* Chiusura e subentro: quattro campi uguali quando tutti sono necessari.
   Le colonne non utilizzate si richiudono senza lasciare spazi vuoti. */
.af-closure-fields {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(min(100%, 8rem), 1fr));
    align-items: end;
    gap: 12px;
    margin-top: 16px;
}

.af-closure-fields > [hidden] { display: none; }
.af-closure-cell, .af-closure-cell > .af-field { min-width: 0; width: 100%; }
#content .af-closure-cell select,
#content .af-closure-cell input { width: 100%; min-width: 0; max-width: 100%; }
#content .af-closure-notes { display: block; width: 100%; margin-top: 14px; }
#content .af-closure-notes textarea { display: block; width: 100%; box-sizing: border-box; }

.af-identity {
    grid-template-columns: minmax(0, 17rem) minmax(0, 22rem) minmax(0, 21rem);
}

.af-validity {
    grid-template-columns: minmax(0, 9rem) repeat(2, minmax(0, 10.5rem));
}

.af-limits {
    grid-template-columns: repeat(2, minmax(0, 9rem)) repeat(2, minmax(0, 9rem));
}

.af-commission {
    grid-template-columns: minmax(0, 8rem) minmax(0, 15rem) minmax(0, 17rem);
}

#content .af-panel label {
    margin: 0;
}

#content .af-panel .af-field input[readonly] {
    background: var(--surface-soft);
}

#content .af-panel .af-money input,
#content .af-panel .af-percent input,
#content .af-panel .af-price-edit input {
    font-variant-numeric: tabular-nums;
}

.af-checks,
.af-price-actions,
.af-price-edit {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 8px 14px;
}

.af-check {
    display: inline-flex;
    align-items: center;
    min-height: 32px;
    padding: 4px 0;
    font-weight: 500;
    cursor: pointer;
    overflow-wrap: anywhere;
}

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

.af-assignment {
    display: grid;
    justify-items: start;
    gap: 4px;
    max-height: 16rem;
    overflow: auto;
}

.af-panel h3 {
    margin: 0 0 8px;
}

#content .af-panel textarea {
    min-height: 78px;
}

.af-actions {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    gap: 8px;
    margin-top: 12px;
}

.af-save-bar {
    margin: 0;
    padding: 12px 20px;
    background: var(--surface-soft);
    border: 1px solid var(--border);
    border-radius: 12px;
    margin-bottom: 16px;
    position: sticky;
    bottom: 8px;
    z-index: 65;
    box-shadow: 0 6px 20px #0004;
}

.af-save-bar > .button[type="submit"], .af-save-bar > span.af-help { margin-left: auto; }
.af-unsaved-indicator { color: #e6b14b; font-size: .8rem; }
.af-unsaved-indicator[hidden] { display: none; }

.af-hint,
.af-panel .af-help,
.af-assignment > p,
.af-panel .table-wrap + p {
    margin: 8px 0 0;
    color: var(--muted);
    font-size: .9rem;
}

.af-hint {
    padding: 6px 20px 16px;
}

.af-panel .af-help:first-child {
    margin-top: 0;
}

.af-panel .table-wrap {
    margin: 0;
}

.af-panel .af-help + .table-wrap {
    margin-top: 10px;
}

.af-price-edit {
    flex-wrap: nowrap;
    gap: 6px;
}

#content .af-panel .af-price-edit input {
    width: 7rem;
    min-width: 7rem;
    min-height: 34px;
}

@container affiliation-sheet (max-width: 55rem) {
    .af-editor-columns {
        display: block;
    }

    .af-editor-side {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 16px;
    }
}

@container affiliation-sheet (max-width: 40rem) {
    .af-identity {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .af-title {
        grid-column: 1 / -1;
        max-width: 28rem;
    }

    .af-commission {
        grid-template-columns: minmax(0, 8rem) minmax(0, 16rem);
    }

    .af-policy {
        grid-column: 1 / -1;
        max-width: 17rem;
    }
}

@container affiliation-sheet (max-width: 31rem) {
    .af-sheet .af-panel {
        padding: 14px;
    }

    .af-identity,
    .af-editor-side,
    .af-pair {
        grid-template-columns: minmax(0, 1fr);
    }

    .af-validity,
    .af-limits {
        grid-template-columns: repeat(2, minmax(0, 10rem));
    }

    .af-status {
        grid-column: 1 / -1;
        max-width: 9rem;
    }

    .af-settle {
        grid-column: 1 / -1;
        max-width: 16rem;
    }

    .af-save-bar {
        padding: 10px 14px;
    }
}

.af-list-filters {
    display: flex;
    flex-wrap: wrap;
    align-items: end;
    gap: 10px 14px;
    margin-bottom: 14px;
}

.af-list-filters .af-search {
    flex: 0 1 25rem;
}

.af-list-filters .af-actions {
    margin: 0;
}

.af-panel details > summary {
    cursor: pointer;
    font-weight: 600;
    margin-bottom: 10px;
}

.af-revision-data {
    display: grid;
    grid-template-columns: max-content minmax(7rem, 1fr);
    gap: 4px 12px;
}

.af-revision-data dd {
    margin: 0;
}

.af-toolbar,
.af-row-actions {
    display: flex;
    align-items: center;
    gap: 8px;
}

.af-row-actions {
    white-space: nowrap;
}

.af-toolbar > form,
.af-row-actions > form {
    margin: 0;
}

#content .button.af-delete {
    color: var(--error-text);
    background: var(--error-bg);
}

.af-editor-economics {
    container: affiliation-economics / inline-size;
}

@container affiliation-sheet (min-width: 53rem) {
    .af-agreement-fields {
        display: grid;
        grid-template-columns: minmax(0, 1.2fr) minmax(0, 1.5fr) minmax(0, 1.45fr) minmax(0, .65fr) repeat(2, minmax(0, .8fr));
        align-items: end;
        gap: 10px;
    }

    .af-agreement-fields > .af-row {
        display: contents;
    }
}

@container affiliation-economics (min-width: 43rem) {
    .af-economic-fields {
        display: grid;
        grid-template-columns: repeat(5, minmax(0, .8fr)) minmax(0, 1.55fr) minmax(0, 1.6fr);
        align-items: end;
        gap: 8px;
    }

    .af-economic-fields > .af-row {
        display: contents;
    }
}

.af-editor-side.af-editor-side-models {
    display: block;
}

.af-revision-fields > .af-validity {
    margin-bottom: 12px;
}

.af-workspace-links {
    flex-wrap: wrap;
}

.af-sheet .af-editor-side .af-panel {
    padding: 12px 14px;
    margin-bottom: 10px;
}

.af-editor-side .af-section-heading {
    margin-bottom: 8px;
}

#content .af-editor-side .af-checks.af-options {
    display: flex;
    align-items: stretch;
    flex-wrap: nowrap;
    gap: 5px;
}

#content .af-editor-side .af-options .af-check {
    flex: 1 1 auto;
    flex-direction: column-reverse;
    justify-content: center;
    gap: 3px;
    min-width: 0;
    min-height: 0;
    padding: 4px 3px;
    font-size: .8rem;
    text-align: center;
}

.af-editor-side .af-options input[type=checkbox] {
    flex-basis: 18px;
}

.af-model-save-row {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: end;
    gap: 8px;
}

.af-contract-type {
    max-width: 16rem;
}

.af-contract-periods,
.af-contract-inputs {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
}

.af-contract-periods {
    margin-top: 12px;
}

.af-contract-period {
    min-width: 0;
    padding: 12px;
    background: var(--surface-soft);
    border: 1px solid var(--border);
    border-radius: 9px;
}

.af-contract-inputs > :is([data-affiliation-rate],.af-policy,.af-check) {
    grid-column: 1 / -1;
}

#content .af-contract-inputs input[inputmode=decimal] {
    max-width: 12ch;
}

.af-form:has(select[name=contract_type] option[value=discount]:checked) [data-affiliation-rate=commission],
.af-form:has(select[name=contract_type] option[value=commission]:checked) [data-affiliation-rate=discount] {
    display: none;
}


@container affiliation-sheet (max-width: 37rem) {
    .af-contract-periods {
        grid-template-columns: minmax(0, 1fr);
    }
}

.af-contract-summary {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
    margin: 0 0 12px;
}

.af-contract-summary dt {
    color: var(--muted);
    font-size: 12px;
}

.af-contract-summary dd {
    margin: 0;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
}

.af-contract-header {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
}

.af-contract-header > label {
    flex: 1 1 12rem;
}

.af-inline-sale { grid-column: 1 / -1; }
#content .af-contract-inputs :disabled,
#content .af-inline-sale input[readonly] { opacity: .6; }

/* Affiliazioni: la programmazione delle revisioni usa una riga compatta locale. */
#content .af-schedule .af-schedule-row {
    display: flex;
    flex-wrap: wrap;
    align-items: end;
    justify-content: flex-start;
    gap: 10px 12px;
}
#content .af-schedule .af-schedule-row > .af-field.af-date {
    flex: 0 1 12rem;
    width: 12rem;
    max-width: 100%;
    min-width: 0;
}
#content .af-schedule .af-schedule-row > .af-field.af-date input[type=date] {
    width: 100%;
    min-width: 0;
}
#content .af-schedule .af-schedule-row > .button {
    flex: 0 0 auto;
    white-space: nowrap;
    margin: 0;
}
