/* Sectioned entity forms (templates/contracts/sectioned_form.html): sections,
   choice chips, switches, money input, summary aside and the popup (.modal). */

/* Section card: numbered head plus the shared .form-grid. */
.form-section { display: grid; gap: 18px; margin-bottom: 18px; scroll-margin-top: 130px; }
.form-section-head { display: flex; gap: 14px; align-items: flex-start; }
.form-section-head h2 { margin: 0; font-size: 20px; }
.form-section-head p { margin: 4px 0 0; font-size: 14px; }
.form-section-num {
  flex: 0 0 34px;
  width: 34px;
  height: 34px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  background: #dfecf2;
  color: var(--brand-deep);
  font-weight: 800;
}
.form-section.is-complete .form-section-num { background: #dbeee6; color: var(--ok); }
details.form-section > summary { list-style: none; cursor: pointer; }
details.form-section > summary::-webkit-details-marker { display: none; }
details.form-section > summary .form-section-head { position: relative; padding-right: 40px; }
details.form-section > summary .form-section-head::after {
  content: "▾";
  position: absolute;
  top: 4px;
  right: 6px;
  color: var(--muted);
  font-size: 18px;
  transition: transform .15s ease;
}
details.form-section[open] > summary .form-section-head::after { transform: rotate(180deg); }
details.form-section:not([open]) { gap: 0; }
.optional-tag { margin-left: 8px; vertical-align: middle; }

/* Rows, hints and validation. */
.form-row { display: grid; gap: 7px; align-content: start; min-width: 0; }
.form-row[hidden] { display: none; }
fieldset.form-row { min-width: 0; margin: 0; padding: 0; border: 0; }
fieldset.form-row > legend { margin-bottom: 7px; padding: 0; }
.req { margin-left: 3px; color: var(--danger); }
.field-hint { margin: 0; color: var(--muted); font-size: 13px; line-height: 1.4; }
.form-row .errorlist { margin: 0; padding: 0; list-style: none; font-size: 13px; font-weight: 600; }
.form-row .errorlist[hidden] { display: none; }
.form-row--invalid input[aria-invalid="true"],
.form-row--invalid textarea[aria-invalid="true"] {
  border-color: var(--danger);
  box-shadow: 0 0 0 4px rgba(155, 60, 53, .12);
}
.form-row--invalid .choice-group { border-radius: 10px; box-shadow: 0 0 0 2px rgba(155, 60, 53, .5); }

/* Select with an attached action button ("+ Новый"). */
.field-with-action { display: flex; gap: 8px; min-width: 0; }
.field-with-action > select,
.field-with-action > input { flex: 1 1 auto; min-width: 0; }
.field-with-action > .btn { flex: 0 0 auto; min-height: 46px; padding: 0 14px; font-size: 14px; white-space: nowrap; }
.field-hint[hidden] { display: none; }
.field-hint--warn { color: #875313; font-weight: 600; }

/* Search combobox over a hidden native select (combobox.js). */
.combobox { position: relative; flex: 1 1 auto; min-width: 0; }
.combobox-input { padding-right: 38px; text-overflow: ellipsis; }
.combobox::after {
  content: "";
  position: absolute;
  top: 50%;
  right: 16px;
  width: 8px;
  height: 8px;
  border-right: 2px solid var(--muted);
  border-bottom: 2px solid var(--muted);
  pointer-events: none;
  transform: translateY(-70%) rotate(45deg);
}
.combobox-list {
  position: absolute;
  top: calc(100% + 4px);
  right: 0;
  left: 0;
  z-index: 30;
  max-height: min(320px, 50vh);
  margin: 0;
  padding: 6px;
  overflow-y: auto;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  box-shadow: 0 18px 44px rgba(52, 69, 78, .18);
  list-style: none;
}
.combobox-list[hidden] { display: none; }
.combobox-option { display: grid; gap: 2px; padding: 9px 10px; border-radius: 8px; cursor: pointer; overflow-wrap: anywhere; }
.combobox-option small { color: var(--muted); font-size: 12px; }
.combobox-option:hover, .combobox-option.is-active { background: #edf3f5; }
.combobox-option mark { padding: 0; border-radius: 3px; background: var(--accent-soft); color: inherit; }
.combobox-note { padding: 9px 10px; color: var(--muted); font-size: 13px; }

/* Approval route builder (partials/approval_route_builder.html, approval-route.js).
   Approvers are .sort-row items, the same as the stage plan. */
.route-builder { display: grid; gap: 14px; min-width: 0; }
.route-builder [hidden] { display: none; }
.route-empty {
  display: grid;
  justify-items: center;
  gap: 6px;
  padding: 22px 16px;
  border: 1px dashed var(--line);
  border-radius: 10px;
  background: rgba(255, 255, 255, .6);
  color: var(--muted);
  text-align: center;
}
.route-empty strong { color: var(--ink); font-size: 16px; }
.route-toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.route-picker { display: grid; gap: 8px; }
.picker-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.link-btn { min-height: 36px; padding: 6px 10px; border: 0; border-radius: 8px; background: transparent; color: var(--brand); font: inherit; font-weight: 700; cursor: pointer; }
.link-btn:hover, .link-btn:focus-visible { background: #edf3f5; }
.suggest-block { display: grid; gap: 8px; }
.suggest-chips { display: flex; flex-wrap: wrap; gap: 8px; }
.suggest-chip {
  display: inline-grid;
  gap: 1px;
  padding: 7px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  color: var(--ink);
  font: inherit;
  font-size: 14px;
  font-weight: 700;
  text-align: left;
  cursor: pointer;
}
.suggest-chip small { color: var(--muted); font-size: 12px; font-weight: 500; }
.suggest-chip:hover { border-color: var(--brand); }
.suggest-chip:disabled { border-style: dashed; color: var(--muted); cursor: default; }
.template-list { display: grid; grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); gap: 10px; }
.template-card {
  display: grid;
  gap: 6px;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  color: var(--ink);
  font: inherit;
  text-align: left;
  cursor: pointer;
}
.template-card:hover, .template-card:focus-visible { border-color: var(--brand); box-shadow: 0 0 0 4px rgba(49, 95, 112, .12); }
.template-card strong { font-size: 15px; }
.template-card span { color: var(--muted); font-size: 13px; }
.template-card small { font-size: 12px; }

/* Choice chips: radio or checkbox options shown as buttons. */
.choice-group { display: flex; flex-wrap: wrap; gap: 8px; min-width: 0; }
.choice { position: relative; display: inline-flex; }
.choice[hidden] { display: none; }
.choice input { position: absolute; width: 1px; height: 1px; min-width: 0; margin: 0; opacity: 0; pointer-events: none; }
.choice span {
  display: inline-flex;
  align-items: center;
  min-height: 42px;
  padding: 8px 14px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: rgba(255, 255, 255, .88);
  font-weight: 600;
  cursor: pointer;
  user-select: none;
}
.choice span:hover { border-color: #9fb2bc; }
.choice input:checked + span { background: var(--brand-deep); border-color: var(--brand-deep); color: #fff; }
.choice input:focus-visible + span { outline: 3px solid rgba(192, 131, 50, .58); outline-offset: 2px; }
.choice-group--segmented {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: minmax(0, 1fr);
  gap: 0;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: rgba(255, 255, 255, .88);
  overflow: hidden;
}
.choice-group--segmented .choice span {
  width: 100%;
  min-height: 46px;
  justify-content: center;
  border: 0;
  border-radius: 0;
  text-align: center;
}
.choice-group--segmented .choice + .choice span { border-left: 1px solid var(--line-soft); }

/* Switch built on a native checkbox. */
.switch { display: flex; align-items: center; gap: 12px; min-height: 46px; cursor: pointer; font-weight: 600; }
.switch input {
  position: relative;
  flex: 0 0 44px;
  width: 44px;
  min-width: 44px;
  height: 26px;
  margin: 0;
  border: 0;
  border-radius: 999px;
  background: #cfd9de;
  appearance: none;
  cursor: pointer;
  transition: background-color .15s ease;
}
.switch input::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(23, 33, 38, .3);
  transition: transform .15s ease;
}
.switch input:checked { background: var(--brand-deep); }
.switch input:checked::after { transform: translateX(18px); }
.switch input:focus-visible { outline: 3px solid rgba(192, 131, 50, .58); outline-offset: 2px; }

/* Money input with the currency sign inside. */
.money-field { position: relative; }
.money-field input { padding-right: 34px; text-align: right; font-variant-numeric: tabular-nums; }
.money-field::after {
  content: "₽";
  position: absolute;
  top: 50%;
  right: 13px;
  color: var(--muted);
  pointer-events: none;
  transform: translateY(-50%);
}

/* Summary aside. */
.create-summary { display: grid; gap: 16px; }
.create-summary h2 { margin: 0; font-size: 18px; }
.summary-list { display: grid; gap: 12px; margin: 0; }
.summary-list div { display: grid; gap: 3px; min-width: 0; }
.summary-list dd { margin: 0; font-weight: 600; overflow-wrap: anywhere; }
.summary-list dd.is-empty { color: #97a3a9; font-weight: 400; }
.section-progress { display: grid; gap: 2px; margin: 0; padding: 12px 0 0; border-top: 1px solid var(--line-soft); list-style: none; }
.section-progress a { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 8px; color: var(--ink); font-weight: 600; }
.section-progress a:hover { background: #edf3f5; text-decoration: none; }
.progress-dot {
  flex: 0 0 20px;
  width: 20px;
  height: 20px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 2px solid var(--line);
  border-radius: 50%;
  color: #fff;
  font-size: 12px;
  font-weight: 800;
}
.is-complete > a .progress-dot { border-color: var(--ok); background: var(--ok); }
.is-complete > a .progress-dot::after { content: "✓"; }
.summary-actions { display: grid; gap: 8px; }
.summary-actions .btn { justify-content: center; }

/* Section chips and the sticky phone action bar are shown by the breakpoints below. */
.section-nav, .action-bar { display: none; }

/* File input with a button that removes the chosen file (ui-shell.js). */
.file-field { display: flex; gap: 8px; align-items: center; min-width: 0; }
.file-field > input { flex: 1 1 auto; min-width: 0; }
.file-field > [data-file-clear][hidden] { display: none; }

/* Password with a show/hide eye (ui-shell.js). */
.password-field { position: relative; }
.password-field input { padding-right: 52px; }
.password-toggle { position: absolute; top: 50%; right: 3px; transform: translateY(-50%); }
.password-toggle[aria-pressed="false"] .password-toggle-slash { display: none; }

/* Sortable list: stage plan and approval route builder (sortable-list.js).
   Each list sets only its grid columns. */
.sort-list { display: grid; gap: 8px; margin: 0; padding: 0; list-style: none; }
.sort-list:empty { display: none; }
.sort-row {
  display: grid;
  gap: 10px;
  align-items: center;
  min-height: 52px;
  padding: 6px 8px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
}
.sort-row.is-dragging { position: relative; z-index: 1; border-color: var(--brand); box-shadow: var(--shadow); }
.sort-grip { cursor: grab; touch-action: none; }
.sort-row.is-dragging .sort-grip { cursor: grabbing; }
.sort-num { color: var(--muted); font-weight: 700; }
.sort-name { display: grid; gap: 2px; min-width: 0; }
.sort-name strong { overflow-wrap: anywhere; }
.sort-name .muted { font-size: 13px; }
.sort-name .muted:empty { display: none; }
.route-builder-row { grid-template-columns: 36px 28px minmax(0, 1fr) 36px; }

/* Advance payments of a contract (widgets/advance_payments.html, sectioned-form.js). */
.advance-payments { display: grid; gap: 10px; min-width: 0; }
.advance-payments > .btn { justify-self: start; }
.advance-payment-list { display: grid; gap: 8px; margin: 0; padding: 0; list-style: none; }
.advance-payment-list:empty { display: none; }
.advance-payment-row { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) 36px; gap: 10px; align-items: center; }

/* Contract stage plan (stage-plan.js). */
.stage-plan { display: grid; gap: 12px; min-width: 0; }
.stage-plan-row { grid-template-columns: 36px 28px minmax(0, 1fr) auto auto 36px; }
.stage-plan-days, .stage-plan-end { white-space: nowrap; }
.stage-plan-end { font-weight: 700; }
.stage-plan-actions { display: flex; flex-wrap: wrap; gap: 8px 14px; align-items: center; }
.stage-plan-actions .field-hint:empty { display: none; }
.stage-plan .empty[hidden] { display: none; }
@media (max-width: 620px) {
  .sort-num { display: none; }
  .advance-payment-row { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) 44px; }
  .route-builder-row { grid-template-columns: 44px minmax(0, 1fr) 44px; }
  .stage-plan-row { grid-template-columns: 44px minmax(0, 1fr) 44px; row-gap: 2px; }
  .stage-plan-row .sort-grip { grid-column: 1; grid-row: 1 / span 2; }
  .stage-plan-row .sort-name { grid-column: 2; grid-row: 1; }
  .stage-plan-days { grid-column: 2; grid-row: 2; justify-self: start; }
  .stage-plan-end { grid-column: 2; grid-row: 2; justify-self: end; }
  .stage-plan-delete { grid-column: 3; grid-row: 1 / span 2; }
}

/* Popup on a native <dialog>: the one component for confirmations and small
   forms (quick create, new stage, invoice document and payment).
   Opened by ui-shell.js, sectioned-form.js and stage-plan.js. */
.modal {
  width: min(520px, calc(100vw - 32px));
  max-width: 100vw;
  max-height: calc(100dvh - 32px);
  padding: 0;
  border: 1px solid var(--line);
  border-radius: var(--radius-xl);
  background: #f6f9fa;
  color: var(--ink);
  box-shadow: var(--shadow);
}
.modal[open] { display: flex; flex-direction: column; }
.modal::backdrop { background: rgba(23, 33, 38, .38); }
/* Optional wrappers (<form>, .modal-inner) must not break the head/body/foot column. */
.modal > form, .modal-inner { display: contents; }
.modal-head { display: flex; flex: none; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 16px 14px 20px; border-bottom: 1px solid var(--line-soft); }
.modal-head h2 { margin: 0; font-size: 20px; }
.modal-body { display: grid; flex: 1 1 auto; align-content: start; gap: 16px; min-height: 0; padding: 20px; overflow-y: auto; }
.modal-body > p { margin: 0; }
.modal-body > .attention { margin: 0; }
.modal-body > .attention[hidden] { display: none; }
.modal-foot { display: flex; flex: none; justify-content: flex-end; gap: 10px; padding: 14px 20px calc(14px + env(safe-area-inset-bottom)); border-top: 1px solid var(--line-soft); }

@media (max-width: 1050px) {
  .section-nav {
    display: flex;
    gap: 8px;
    margin: 0 -22px 16px;
    padding: 2px 22px 6px;
    overflow-x: auto;
    scrollbar-width: none;
  }
  .section-nav a {
    flex: 0 0 auto;
    padding: 8px 14px;
    border: 1px solid var(--line);
    border-radius: 999px;
    background: rgba(255, 255, 255, .8);
    color: var(--ink);
    font-size: 14px;
    font-weight: 700;
  }
  .section-nav a:hover { text-decoration: none; }
}

@media (max-width: 620px) {
  .form-section { scroll-margin-top: 90px; }
  .form-section-head h2 { font-size: 18px; }
  .section-nav { margin: 0 -12px 14px; padding: 2px 12px 6px; }
  .choice-group--segmented { display: flex; flex-wrap: wrap; gap: 8px; border: 0; background: transparent; overflow: visible; }
  .choice-group--segmented .choice { flex: 1 1 calc(50% - 4px); }
  .choice-group--segmented .choice span,
  .choice-group--segmented .choice + .choice span { border: 1px solid var(--line); border-radius: 10px; }
  .field-with-action > .btn { padding: 0 12px; }
  .combobox-option { min-height: 44px; align-content: center; }
  .route-toolbar .btn { flex: 1 1 100%; justify-content: center; }
  .link-btn { min-height: 44px; }
  .field-with-action--wrap { flex-wrap: wrap; }
  .field-with-action--wrap > .btn { flex: 1 1 100%; justify-content: center; }
  .summary-actions { display: none; }
  .action-bar {
    position: sticky;
    bottom: 0;
    z-index: 20;
    display: flex;
    gap: 8px;
    margin: 16px -12px -18px;
    padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
    border-top: 1px solid var(--line);
    background: rgba(236, 242, 245, .94);
    backdrop-filter: blur(14px);
  }
  .action-bar .btn { flex: 1 1 0; justify-content: center; }
  .action-bar .btn.secondary { flex: 0 0 auto; }
  .modal { width: calc(100vw - 16px); max-height: calc(100dvh - 16px); }
  .modal-foot .btn { flex: 1 1 0; justify-content: center; }
}

/* .main uses 10px side padding here; keep the bars flush with the screen. */
@media (max-width: 390px) {
  .action-bar, .section-nav { margin-right: -10px; margin-left: -10px; padding-right: 10px; padding-left: 10px; }
}
