/*
 * Engine forms: calculator form grid, fields, hints, date picker button. Loaded by layout.html.twig after engine.css,
 * before the site theme (the theme overrides tokens, not these rules).
 *
 * Field markup: .field > label (+ .unit, .optional) / widget / .field-error / .help.
 * In a .form-grid--rows grid every field is a subgrid of four rows (label, widget, error, hint), so fields of one
 * row of the grid stand on one line whatever the length of their labels and hints. Without subgrid support the label
 * keeps a two-line minimum height instead.
 */

.form-grid { display: grid; grid-template-columns: minmax(0, 1fr); gap: 0 var(--s-4); align-items: start; }
.form-section { min-width: 0; margin: 0 0 var(--s-2); padding: var(--s-4) 0 0; border: 0; border-top: 1px solid var(--c-border); }
.form-section:first-child, .calc-form > .callout + .form-section { padding-top: 0; border-top: 0; }
.form-section[hidden] { display: none; }
.form-section legend { float: left; width: 100%; margin: 0 0 var(--s-3); padding: 0; font-family: var(--font-display); font-weight: 700; font-size: var(--fs-h3); }
.form-section legend + * { clear: both; }
.form-actions { display: flex; flex-wrap: wrap; align-items: center; gap: var(--s-3); margin-top: var(--s-2); }
.form-actions button { min-width: 10rem; }

.calc-form .field { min-width: 0; }
/* min-width 0 + overflow-wrap: a long unbreakable word in a label or hint must not widen the field (and the row of the grid) beyond the screen */
.calc-form .field label { display: flex; flex-wrap: nowrap; align-items: baseline; justify-content: space-between; gap: var(--s-2); line-height: 1.3; min-width: 0; overflow-wrap: anywhere; }
.calc-form .field label .unit { font-weight: 400; color: var(--c-muted); white-space: nowrap; }
/* "optional" is a small grey note at the right end of the label, never a part of the wrapped label text. */
.calc-form .field label .optional { margin-left: auto; flex: none; font-weight: 400; font-size: var(--fs-xs); color: var(--c-muted); white-space: nowrap; }
.calc-form .field input:not([type="checkbox"], [type="radio"]), .calc-form .field select {
    display: block; width: 100%; max-width: 100%; min-height: 46px; padding: 9px 12px;
    border-radius: var(--radius); font-size: 1rem; line-height: 1.3;
}
.calc-form .field select { padding-right: 36px; }
/* 16px keeps iOS from zooming into the field. */
@media (max-width: 767px) { .calc-form .field input, .calc-form .field select { font-size: max(1rem, 16px); } }
.calc-form .field .help { margin: var(--s-1) 0 0; line-height: 1.35; overflow-wrap: anywhere; }
.field-error { margin: var(--s-1) 0 0; font-size: var(--fs-sm); font-weight: 600; color: var(--c-error); overflow-wrap: anywhere; }
.field--error input, .field--error select { border-color: var(--c-error); }
/* Consent checkbox (required, unchecked by default): the box and the text with its link in one row. */
.calc-form .field--consent { display: flex; flex-wrap: wrap; align-items: flex-start; gap: 0 var(--s-2); }
.calc-form .field--consent input[type="checkbox"] { flex: none; width: 18px; height: 18px; margin: 3px 0 0; accent-color: var(--c-accent); }
.calc-form .field--consent label { flex: 1 1 0; min-width: 0; display: block; font-weight: 400; line-height: 1.4; cursor: pointer; }
.calc-form .field--consent .field-error, .calc-form .field--consent .field__error { flex: 1 0 100%; }

/* Fallback without subgrid: labels reserve two lines, so inputs of one row still line up for the usual texts. */
:is(.form-grid--rows, .calc-form__row) > .field > label { min-height: 2.6em; }

@supports (grid-template-rows: subgrid) {
    /* the column is bounded (minmax(0, 1fr)): a long word cannot widen the implicit auto column beyond the field */
    :is(.form-grid--rows, .calc-form__row) > .field { display: grid; grid-row: span 4; grid-template-rows: subgrid; grid-template-columns: minmax(0, 1fr); min-width: 0; row-gap: 0; align-content: start; }
    :is(.form-grid--rows, .calc-form__row) > .field > label { grid-row: 1; min-height: 0; align-self: end; }
    :is(.form-grid--rows, .calc-form__row) > .field > :not(label, .help, .field-error, .field__error) { grid-row: 2; }
    :is(.form-grid--rows, .calc-form__row) > .field > .field-error, :is(.form-grid--rows, .calc-form__row) > .field > .field__error { grid-row: 3; }
    :is(.form-grid--rows, .calc-form__row) > .field > .help { grid-row: 4; }
}

@media (min-width: 560px) { .form-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (min-width: 960px) { .form-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); } }

/* ---------- Date field (date-field.js): the text input stays the main control, the button opens the native picker ---------- */
.date-field { position: relative; display: flex; align-items: stretch; min-width: 0; }
.date-field > input[type="text"] { flex: 1 1 auto; min-width: 0; border-top-right-radius: 0; border-bottom-right-radius: 0; }
.date-field__btn {
    flex: none; display: inline-flex; align-items: center; justify-content: center; width: 46px; min-width: 44px; min-height: 44px; padding: 0;
    border: 1px solid var(--c-border-strong); border-left: 0; border-radius: 0 var(--radius) var(--radius) 0;
    background: var(--c-surface); color: var(--c-muted); cursor: pointer;
}
.date-field__btn:hover { color: var(--c-link); background: var(--c-accent-soft); }
.date-field__btn:focus-visible { outline: 2px solid var(--c-focus); outline-offset: -2px; }
.date-field__btn svg { width: 20px; height: 20px; stroke: currentColor; fill: none; stroke-width: 1.75; stroke-linecap: round; stroke-linejoin: round; }
.calc-form .field .date-field__native, .date-field__native { position: absolute; right: 0; bottom: 0; width: 1px; height: 1px; min-height: 0; max-width: 1px; padding: 0; border: 0; opacity: 0; pointer-events: none; }
.field--error .date-field__btn { border-color: var(--c-error); }
@media print { .date-field__btn, .date-field__native { display: none; } }

/* ---------- Select with long options ---------- */
/* A native select shows its chosen option on ONE line and cuts it («Когда узнал(а) или должен(на) бы…»). Browsers with
   customizable selects (Chromium 135+, appearance: base-select) get a select whose closed field and open list WRAP the
   text, styled with the same tokens as the other fields; the rest keep the native control (so long option texts should
   stay short, with the explanation in the hint under the field). Tokens only; the picker follows the theme like the page. */
@supports (appearance: base-select) {
    .calc-form .field select, .calc-form .field select::picker(select) { appearance: base-select; }
    .calc-form .field select { height: auto; min-height: 46px; padding: 9px 12px; background-image: none; align-items: center; gap: var(--s-2); text-align: left; cursor: pointer; }
    .calc-form .field select::picker-icon {
        flex: none; width: 8px; height: 8px; margin-left: auto; content: "";
        border-right: 2px solid var(--c-muted); border-bottom: 2px solid var(--c-muted); transform: translateY(-2px) rotate(45deg);
    }
    .calc-form .field select:open::picker-icon { transform: translateY(2px) rotate(-135deg); }
    .calc-form .field select selectedcontent { display: block; flex: 1 1 auto; min-width: 0; white-space: normal; overflow-wrap: break-word; line-height: 1.3; }
    .calc-form .field select::picker(select) {
        box-sizing: border-box; max-height: min(60vh, 24rem); padding: var(--s-1); margin-top: 4px;
        border: 1px solid var(--c-border-strong); border-radius: var(--radius); background: var(--c-surface); color: var(--c-text); box-shadow: var(--shadow-pop);
    }
    .calc-form .field select option { display: flex; align-items: center; gap: var(--s-2); min-height: 44px; padding: 8px 12px; border-radius: var(--radius-sm); white-space: normal; overflow-wrap: break-word; line-height: 1.3; cursor: pointer; }
    .calc-form .field select option::checkmark { content: "✓"; margin-left: auto; color: var(--c-accent); }
    .calc-form .field select option:hover, .calc-form .field select option:focus-visible { background: var(--c-row-hover); }
    .calc-form .field select option:checked { background: var(--c-accent-soft); font-weight: 600; }
}
