@use '../abstracts' as *;

// Блокировка скролла
html.modal-scroll-lock {
  overflow: hidden;
  overscroll-behavior: none;
  height: 100%;
}

body.modal-scroll-lock {
  overflow: hidden;
  overscroll-behavior: none;
}

// Оверлей модалки
.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 2000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: max(20px, env(safe-area-inset-top, 0px)) max(20px, env(safe-area-inset-right, 0px))
    max(20px, env(safe-area-inset-bottom, 0px)) max(20px, env(safe-area-inset-left, 0px));
  background: rgba($color-blue, 0.2);
  will-change: opacity;

  // Старый класс — оставлен на случай JS без Alpine
  &.is-open {
    display: flex;
  }

  .modal {
    will-change: transform, opacity;
  }
}

.modal {
  background: $color-white;
  border-radius: 20px;
  padding: 40px;
  width: 100%;
  max-width: 480px;
  position: relative;
  box-shadow: 0 24px 48px rgba(21, 35, 70, 0.18);

  @include xl {
    max-width: 540px;
    min-height: 370px;
    padding: 30px;
    border-radius: 16px;
    border: 1px solid $color-dark-blue;
    background: $color-white;
  }

  &__close {
    // Figma 129:19594: зона 24×24, крестик — два штриха с round cap
    position: absolute;
    top: 16px;
    right: 16px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 24px;
    height: 24px;
    min-width: 24px;
    min-height: 24px;
    box-sizing: border-box;
    cursor: pointer;
    background: none;
    border: none;
    padding: 0;
    line-height: 0;
    color: $color-text-secondary;
    transition: opacity 0.2s;

    // Сброс `svg { max-width: 100%; height: auto; }` из reset — иначе иконка не занимает 24×24
    &-icon {
      display: block;
      width: 100%;
      height: 100%;
      max-width: none;
      flex-shrink: 0;
    }

    &:hover {
      opacity: 0.75;
    }

    &:focus-visible {
      outline: 2px solid $color-blue;
      outline-offset: 2px;
      border-radius: 6px;
    }

    @include xl {
      top: 30px;
      right: 30px;
      transform: translate(50%, -50%);
    }
  }

  &__title {
    font-size: 24px;
    font-weight: 700;
    color: $color-text;
    margin: 0;
    padding-right: 0;
    line-height: 1.25;

    @include xl {
      font-family: Raleway;
      font-size: 32px;
      font-style: normal;
      font-weight: 700;
      line-height: 1;
      margin-bottom: 30px;
    }
  }

  &__form {
    display: flex;
    flex-direction: column;
    gap: 16px;
  }

  &__group {
    display: flex;
    flex-direction: column;
    gap: 6px;

    label {
      font-size: 13px;
      font-weight: 500;
      color: $color-text-secondary;
    }

    input,
    select,
    textarea {
      padding: 12px 14px;
      border: 1px solid rgba($color-text, 0.15);
      border-radius: $radius-sm;
      font-family: $font-main;
      font-size: 14px;
      color: $color-text;
      transition: border-color 0.2s;

      &:focus {
        outline: none;
        border-color: $color-blue;
      }

      &.is-invalid {
        border-color: $color-red;

        &:focus {
          border-color: $color-red;
        }
      }
    }
  }

  // Блок капчи: картинка + кнопка обновления + поле ввода
  &__group--captcha {
    gap: 10px;
  }

  &__captcha-row {
    display: flex;
    align-items: flex-end;
    gap: 10px;
  }

  &__captcha-img {
    display: block;
    border-radius: $radius-sm;
    border: 1px solid rgba($color-text, 0.1);
    flex-shrink: 0;
  }

  &__captcha-refresh {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    border-radius: $radius-sm;
    background: transparent;
    border: 1px solid rgba($color-text, 0.15);
    color: $color-text-secondary;
    cursor: pointer;
    transition: background 0.2s, border-color 0.2s, color 0.2s;
    flex-shrink: 0;
    margin-bottom: 2px;

    &:hover {
      background: rgba($color-blue, 0.06);
      border-color: $color-blue;
      color: $color-blue;
    }

    &:active {
      opacity: 0.7;
    }

    &:focus-visible {
      outline: 2px solid $color-blue;
      outline-offset: 2px;
    }

    svg {
      width: 16px;
      height: 16px;
    }
  }

  &__captcha-input {
    width: 100%;
    height: 40px;
    padding: 10px 15px;
    border: 1px solid $color-text-secondary;
    border-radius: $radius-sm;
    font-family: $font-main;
    font-size: 14px;
    color: $color-text;
    transition: border-color 0.2s;

    &::placeholder {
      color: $color-text-secondary;
    }

    &:focus {
      outline: none;
      border-color: $color-blue;
    }

    &:hover:not(:focus) {
      border-color: rgba($color-text, 0.35);
    }

    &.is-invalid {
      border-color: $color-red;

      &:focus {
        border-color: $color-red;
      }
    }
  }

  &__checkbox.is-invalid span {
    color: $color-red;
  }

  &__checkbox {
    display: flex;
    gap: 10px;
    align-items: flex-start;

    input {
      margin-top: 2px;
      flex-shrink: 0;
    }

    span {
      font-size: 12px;
      color: $color-text-secondary;
      line-height: 1.4;
    }

    a {
      color: $color-blue;
      text-decoration: underline;

      &:focus-visible {
        outline: 2px solid $color-blue;
        outline-offset: 2px;
      }
    }
  }

  &__note {
    font-size: 12px;
    color: $color-text-secondary;
    text-align: center;
  }

  @include sm {
    padding: 24px 20px;
    border-radius: $radius-md;

    &__title {
      font-size: 20px;
    }
  }
}

// «Заказать звонок»: ≤360 — Figma 129:19618/129:19619 (320px, title 24); 361–576 — 129:19650 (title 32); 768–991 — 129:19681 (688px); 992 — 129:19712 (676px); 993–1919 — 129:19557 (540px); 1920+ — 129:19588 (594px).
.modal--callback {
  max-width: 540px;
  width: 100%;
  padding: 30px;
  border-radius: 16px;
  border: 1px solid $color-dark-blue;
  background: $color-white;
  box-shadow: 0 24px 64px rgba(21, 35, 70, 0.2);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 30px;
  position: relative;

  @include up($bp-1920) {
    max-width: min(594px, calc(100vw - 40px));
  }

  .modal__head--callback {
    display: flex;
    align-items: flex-start;
    justify-content: center;
    gap: 30px;
    width: 100%;
    margin: 0;
  }

  .modal__title {
    flex: 1 1 auto;
    min-width: 0;
    margin: 0;
    padding: 0;
    font-family: $font-heading;
    font-size: 36px;
    font-weight: 700;
    line-height: normal;
    color: $color-text;
  }

  .modal__close {
    position: static;
    flex-shrink: 0;
    margin: 0;
    transform: none;

    @include xl {
      top: auto;
      right: auto;
      transform: none;
    }
  }

  .modal__form--callback {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 30px;
    width: 100%;
  }

  .modal__fields {
    display: flex;
    flex-direction: column;
    gap: 20px;
    width: 100%;
  }

  .modal__group {
    gap: 10px;

    label {
      font-family: $font-main;
      font-size: 14px;
      font-weight: 500;
      line-height: $line-height-tight;
      color: $color-black;
    }

    input {
      height: 40px;
      padding: 10px 15px;
      border: 1px solid $color-text-secondary;
      border-radius: 8px;
      font-family: $font-main;
      font-size: 14px;
      color: $color-text;
      transition: border-color 0.2s;

      &::placeholder {
        color: $color-text-secondary;
      }

      &:focus {
        outline: none;
        border-color: $color-blue;
      }

      &:hover:not(:focus) {
        border-color: rgba($color-text, 0.35);
      }

      &.is-invalid {
        border-color: $color-red;

        &:focus {
          border-color: $color-red;
        }
      }
    }
  }

  // Капча в callback модалке
  .modal__group--captcha {
    gap: 10px;
  }

  .modal__captcha-row {
    display: flex;
    align-items: flex-end;
    gap: 10px;
  }

  .modal__captcha-img {
    display: block;
    border-radius: $radius-sm;
    border: 1px solid rgba($color-text, 0.1);
    flex-shrink: 0;
  }

  .modal__captcha-refresh {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    border-radius: $radius-sm;
    background: transparent;
    border: 1px solid rgba($color-text, 0.15);
    color: $color-text-secondary;
    cursor: pointer;
    transition: background 0.2s, border-color 0.2s, color 0.2s;
    flex-shrink: 0;
    margin-bottom: 2px;

    &:hover {
      background: rgba($color-blue, 0.06);
      border-color: $color-blue;
      color: $color-blue;
    }

    &:active {
      opacity: 0.7;
    }

    &:focus-visible {
      outline: 2px solid $color-blue;
      outline-offset: 2px;
    }

    svg {
      width: 16px;
      height: 16px;
    }
  }

  .modal__captcha-input {
    width: 100%;
    height: 40px;
    padding: 10px 15px;
    border: 1px solid $color-text-secondary;
    border-radius: $radius-sm;
    font-family: $font-main;
    font-size: 14px;
    color: $color-text;
    transition: border-color 0.2s;

    &::placeholder {
      color: $color-text-secondary;
    }

    &:focus {
      outline: none;
      border-color: $color-blue;
    }

    &:hover:not(:focus) {
      border-color: rgba($color-text, 0.35);
    }

    &.is-invalid {
      border-color: $color-red;

      &:focus {
        border-color: $color-red;
      }
    }
  }

  .modal__checkbox--callback {
    gap: 8px;
    align-items: flex-start;
    cursor: pointer;

    // Figma 129:19614 CheckBox_Icon_UIA: 16×16, border 1.2px #4F6ECB, radius 4px, fill rgba(blue,0.1) при checked
    input[type='checkbox'] {
      appearance: none;
      -webkit-appearance: none;
      position: relative;
      box-sizing: border-box;
      width: 16px;
      height: 16px;
      margin: 2px 0 0;
      flex-shrink: 0;
      align-self: flex-start;
      border: 1.2px solid $color-blue;
      border-radius: 4px;
      background: $color-white;
      cursor: pointer;
      transition: background 0.15s ease, border-color 0.15s ease;

      &:checked {
        background: rgba($color-blue, 0.1);
      }

      &:checked::after {
        content: '';
        position: absolute;
        left: 4px;
        top: 1px;
        width: 4px;
        height: 7px;
        border: solid $color-blue;
        border-width: 0 1.5px 1.5px 0;
        transform: rotate(45deg);
        box-sizing: border-box;
      }

      &:focus-visible {
        outline: 2px solid $color-blue;
        outline-offset: 2px;
      }
    }

    &.is-invalid input[type='checkbox'] {
      border-color: $color-red;

      &:checked {
        background: rgba($color-red, 0.06);
      }

      &:checked::after {
        border-color: $color-red;
      }
    }

    span {
      font-size: 14px;
      font-weight: 400;
      line-height: normal;
      color: $color-black;
    }

    a {
      color: $color-blue;
      text-decoration: none;

      &:hover {
        text-decoration: underline;
      }

      &:focus-visible {
        outline: 2px solid $color-blue;
        outline-offset: 2px;
      }
    }
  }

  .modal__form--callback > button.btn[type='submit'] {
    display: flex;
    align-items: center;
    justify-content: center;
    box-sizing: border-box;
    width: 190px;
    height: 40px;
    margin-inline: auto;
    padding: 8px 16px;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 600;
    line-height: normal;
  }

  // ≤1919: ширина как Figma 129:19557 (540), с учётом паддингов оверлея
  @include xl {
    min-height: 0;
    max-width: min(540px, calc(100vw - 40px));
  }

  // 768–991: Figma 129:19681 — карточка 688px (макет «768 Заказать звонок»)
  @include between($bp-768, $until-991) {
    max-width: min(688px, calc(100vw - 40px));
  }

  // Ровно 992px: Figma 129:19712 — карточка 676px
  @include between($bp-md2, $bp-md2) {
    max-width: min(676px, calc(100vw - 40px));
  }

  @include sm {
    padding: 20px;
    border-radius: $radius-md; // 16px — как в Figma 129:19650
    gap: 20px;

    .modal__form--callback {
      gap: 20px;
    }

    .modal__fields {
      gap: 15px;
    }

    .modal__title {
      font-size: 32px;
    }
  }

  // ≤360px: xs в проекте = max 359 — на 360 срабатывает только sm; Figma 129:19619 отдельно
  @include down-to($bp-360) {
    max-width: min(320px, calc(100vw - 40px));

    .modal__title {
      font-size: 24px;
    }

    .modal__form--callback > button.btn[type='submit'] {
      width: 100%;
    }
  }
}

// «Записаться на приём»: база 540 / 1441+ 594; ≤1439 — 676 (Figma 992); max-width 768 — min(688px, calc(100vw - 40px)) + шапка Figma 129:18279. Без between(min–max).
.modal--appointment {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 30px;
  max-width: min(540px, calc(100vw - 40px));
  width: 100%;
  padding: 30px;
  border-radius: $radius-md;
  border: 1px solid $color-dark-blue;
  box-shadow: 0 24px 64px rgba(21, 35, 70, 0.2);

  .modal__head {
    display: flex;
    align-items: flex-start;
    justify-content: center;
    gap: 30px;
    width: 100%;
    margin: 0;
  }

  .modal__title {
    flex: 1 1 auto;
    min-width: 0;
    margin: 0;
    padding: 0;
    font-family: $font-heading;
    font-size: 36px;
    font-weight: 700;
    line-height: 1.2;
    color: $color-text;
    hyphens: manual;
    text-wrap: balance;
  }

  .modal__close {
    position: static;
    flex-shrink: 0;
    margin: 0;
    transform: none;

    @include xl {
      top: auto;
      right: auto;
      transform: none;
    }
  }

  .modal__form--appointment {
    display: flex;
    flex-direction: column;
    gap: 30px;
    width: 100%;
  }

  .modal__fields {
    display: flex;
    flex-direction: column;
    gap: 20px;
    width: 100%;
  }

  .modal__group {
    gap: 10px;

    label {
      font-family: $font-main;
      font-size: 14px;
      font-weight: 500;
      line-height: 1.3;
      color: $color-black;
    }

    input {
      height: 40px;
      padding: 10px 15px;
      border: 1px solid $color-text-secondary;
      border-radius: $radius-sm;
      font-family: $font-main;
      font-size: 14px;
      color: $color-text;
      transition: border-color 0.2s;

      &::placeholder {
        color: $color-text-secondary;
      }

      &:focus {
        outline: none;
        border-color: $color-blue;
      }

      &:hover:not(:focus) {
        border-color: rgba($color-text, 0.35);
      }

      &.is-invalid {
        border-color: $color-red;

        &:focus {
          border-color: $color-red;
        }
      }
    }
  }

  .modal__checkbox--appointment.is-invalid span {
    color: $color-red;
  }

  .modal__select-wrap {
    position: relative;

    select {
      width: 100%;
      height: 40px;
      padding: 10px 40px 10px 15px;
      border: 1px solid $color-text-secondary;
      border-radius: $radius-sm;
      font-family: $font-main;
      font-size: 14px;
      line-height: 1.3;
      color: $color-header-strong-text;
      background-color: $color-white;
      appearance: none;
      cursor: pointer;
      transition: border-color 0.2s;

      &:focus {
        outline: none;
        border-color: $color-blue;
      }

      &:hover:not(:focus) {
        border-color: rgba($color-text, 0.35);
      }
    }

    &::after {
      content: '';
      position: absolute;
      right: 15px;
      top: 50%;
      transform: translateY(-50%);
      width: 16px;
      height: 16px;
      pointer-events: none;
      background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 16 16' fill='none'%3E%3Cpath d='M4 6l4 4 4-4' stroke='%23212121' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") center / contain no-repeat;
    }

    // Специализация заблокирована (пришла с карточки врача)
    &.is-locked {
      select {
        pointer-events: none;
        background-color: rgba(0, 0, 0, 0.03);
        color: inherit;
        opacity: 1;
        cursor: default;
        border-color: rgba(0, 0, 0, 0.15);
      }

      // Стрелку-chevron заменяем на замок
      &::after {
        background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none'%3E%3Crect x='3' y='11' width='18' height='11' rx='2' stroke='%23888' stroke-width='1.5'/%3E%3Cpath d='M7 11V7a5 5 0 0 1 10 0v4' stroke='%23888' stroke-width='1.5' stroke-linecap='round'/%3E%3C/svg%3E");
        opacity: 0.6;
      }
    }
  }

  // Капча в appointment модалке
  .modal__group--captcha {
    gap: 10px;
  }

  .modal__captcha-row {
    display: flex;
    align-items: flex-end;
    gap: 10px;
  }

  .modal__captcha-img {
    display: block;
    border-radius: $radius-sm;
    border: 1px solid rgba($color-text, 0.1);
    flex-shrink: 0;
  }

  .modal__captcha-refresh {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    border-radius: $radius-sm;
    background: transparent;
    border: 1px solid rgba($color-text, 0.15);
    color: $color-text-secondary;
    cursor: pointer;
    transition: background 0.2s, border-color 0.2s, color 0.2s;
    flex-shrink: 0;
    margin-bottom: 2px;

    &:hover {
      background: rgba($color-blue, 0.06);
      border-color: $color-blue;
      color: $color-blue;
    }

    &:active {
      opacity: 0.7;
    }

    &:focus-visible {
      outline: 2px solid $color-blue;
      outline-offset: 2px;
    }

    svg {
      width: 16px;
      height: 16px;
    }
  }

  .modal__captcha-input {
    width: 100%;
    height: 40px;
    padding: 10px 15px;
    border: 1px solid $color-text-secondary;
    border-radius: $radius-sm;
    font-family: $font-main;
    font-size: 14px;
    color: $color-text;
    transition: border-color 0.2s;

    &::placeholder {
      color: $color-text-secondary;
    }

    &:focus {
      outline: none;
      border-color: $color-blue;
    }

    &:hover:not(:focus) {
      border-color: rgba($color-text, 0.35);
    }

    &.is-invalid {
      border-color: $color-red;

      &:focus {
        border-color: $color-red;
      }
    }
  }

  .modal__checkbox--appointment {
    gap: 8px;
    align-items: flex-start;
    cursor: pointer;

    // Figma 129:17308 — как CheckBox_Icon_UIA в «Заказать звонок»
    input[type='checkbox'] {
      appearance: none;
      -webkit-appearance: none;
      position: relative;
      box-sizing: border-box;
      width: 16px;
      height: 16px;
      margin: 2px 0 0;
      flex-shrink: 0;
      align-self: flex-start;
      border: 1.2px solid $color-blue;
      border-radius: 4px;
      background: $color-white;
      cursor: pointer;
      transition: background 0.15s ease, border-color 0.15s ease;

      &:checked {
        background: rgba($color-blue, 0.1);
      }

      &:checked::after {
        content: '';
        position: absolute;
        left: 4px;
        top: 1px;
        width: 4px;
        height: 7px;
        border: solid $color-blue;
        border-width: 0 1.5px 1.5px 0;
        transform: rotate(45deg);
        box-sizing: border-box;
      }

      &:focus-visible {
        outline: 2px solid $color-blue;
        outline-offset: 2px;
      }
    }

    &.is-invalid input[type='checkbox'] {
      border-color: $color-red;

      &:checked {
        background: rgba($color-red, 0.06);
      }

      &:checked::after {
        border-color: $color-red;
      }
    }

    span {
      font-size: 14px;
      font-weight: 400;
      line-height: normal;
      color: $color-black;
    }

    a {
      color: $color-blue;
      text-decoration: none;

      &:hover {
        text-decoration: underline;
      }

      &:focus-visible {
        outline: 2px solid $color-blue;
        outline-offset: 2px;
      }
    }
  }

  .modal__form--appointment > button.btn[type='submit'] {
    display: flex;
    align-items: center;
    justify-content: center;
    box-sizing: border-box;
    width: 190px;
    height: 40px;
    margin-inline: auto;
    padding: 8px 16px;
    border-radius: $radius-sm;
    font-size: 14px;
    font-weight: 600;
    line-height: normal;
  }

  // --- Только одиночные пороги max-width (и min-width 1441+), без диапазонов min–max.

  // min-width: 1441px — карточка 594 (Figma)
  @include up(1441px) {
    max-width: min(594px, calc(100vw - 40px));
  }

  // max-width: 1439px — планшет 676, заголовок по центру (Figma 992)
  @include lg {
    max-width: min(676px, calc(100vw - 40px));

    .modal__head {
      display: block;
      position: relative;
      width: 100%;
    }

    .modal__title {
      flex: none;
      width: 100%;
      box-sizing: border-box;
      padding-inline: 40px;
      text-align: center;
    }

    .modal__close {
      position: absolute;
      top: 0;
      right: 0;
    }
  }

  // max-width: 768px — ширина карточки и шапка как Figma 129:18279 / 129:19442
  @media (max-width: $bp-md) {
    max-width: min(688px, calc(100vw - 40px));
    padding: 30px;
    gap: 30px;

    .modal__head {
      display: flex;
      align-items: flex-start;
      justify-content: center;
      gap: 30px;
      position: static;
      width: 100%;
    }

    .modal__title {
      flex: 1 1 auto;
      min-width: 0;
      width: auto;
      padding-inline: 0;
      text-align: left;
      font-size: 36px;
    }

    .modal__close {
      position: static;
      top: auto;
      right: auto;
    }

    .modal__form--appointment {
      gap: 30px;
    }
  }

  // ≤576
  @include sm {
    padding: 20px 16px;

    .modal__title {
      font-size: 22px;
    }
  }

  // ≤360
  @include xs {
    .modal__title {
      font-size: 18px;
    }

    .modal__form--appointment > button.btn[type='submit'] {
      width: 100%;
    }
  }
}

// legacy: раньше использовался .no-scroll — заменён на modal-scroll-lock + fixed body в modals.ts
body.no-scroll {
  overflow: hidden;
}
