@use 'styles/globals.scss';
@use 'styles/inputs.scss';
@use 'styles/labels.scss';
@use 'styles/colspan.scss';
@use 'styles/lowFlow.scss';

.semanticForms {
  fieldset {
    border-radius: var(--semanticFormsBorderRadius);
    background: var(--semanticFormsFormSubBgColor);
    border: 1px var(--semanticFormsSubBorderColor) solid;
    display: flex;
    flex-direction: column;
    gap: 10px;
    padding: 10px 10px 20px 10px;
    margin-bottom: 15px;

    // nested fieldsets and details are darkened
    fieldset, details {
      background-color: var(--semanticFormsNestedFieldsetBgColor);
      border: var(--semanticFormsNestedFieldsetBorder);
      margin: 0;

      // add opacity to styled inputs to prevent blending with background
      input:not([type=submit], [type=reset], [type=button], [type=image], [type=checkbox], [type=radio]),
      select,
      textarea {
        background-color: var(--semanticFormsNestedInputBgColor);
      }
    }
  }

  details {
    box-sizing: border-box; // ensure sizings match across browsers
    position: relative;
    background-color: var(--semanticFormsFormSubBgColor);
    border: 1px var(--semanticFormsSubBorderColor) solid;
    border-radius: var(--semanticFormsBorderRadius);
    display: flex;
    flex-direction: column;
    gap: 5px;
    padding: 10px;
    margin: 0;
    min-height: calc(var(--semanticFormsFontSize) + 20px);

    &::details-content {
      visibility: hidden;
      opacity: 0;
      transition: opacity 250ms ease-in, visibility 0ms ease-in 250ms;
    }

    @supports (interpolate-size: allow-keywords) { // https://caniuse.com/wf-interpolate-size
      transition: opacity 250ms ease-in, visibility 0ms ease-in 250ms, height 250ms;

      &::details-content {
        height: 0;
        overflow: hidden;
        transition: height 250ms, content-visibility 250ms;
        transition-behavior: allow-discrete;
      }
    }

    summary {
      cursor: pointer;
      margin: 0;
      margin-bottom: 10px;
      line-height: 1;
      position: absolute;
      top: 10px;
      left: 22px;
      transition: top 250ms;

      &::-webkit-details-marker,
      &::marker {
        content: ''; // hide default arrow, as it cannot be animated
      }

      &::before {
        // custom arrow
        content: '';
        border-width: .4rem;
        border-style: solid;
        border-color: transparent transparent transparent var(--semanticFormsTextColor);
        position: absolute;
        top: 0;
        left: -10px;
        transform: rotate(0);
        transform-origin: .2rem 50%;
        transition: 250ms transform ease;
      }
    }

    &[open] {
      padding-top: 30px;

      &::details-content {
        visibility: visible;
        opacity: 1;
        transition: opacity 250ms ease-in, visibility 0ms ease-in 0ms;
      }

      @supports (interpolate-size: allow-keywords) {
        &::details-content {
          height: auto;
        }
      }

      summary {
        &::before {
          transform: rotate(90deg);
        }
      }

      & > dl:first-of-type {
        margin-top: 15px;
        margin-bottom: 15px;
      }
    }
  }

  details details,
  details fieldset {
    margin-top: 10px;
    background-color: var(--semanticFormsNestedFieldsetBgColor);
    border: var(--semanticFormsNestedFieldsetBorder);
  }

  details details[open] summary::before {
    transform: rotate(90deg);
  }

  details details:not([open]) summary::before {
    transform: rotate(0deg);
  }

  dl {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(var(--semanticFormsInputMinWidth), 1fr));
    gap: 10px;
    margin: 0;

    div {
      position: relative;
      width: 100%;
      max-width: var(--semanticFormsInputMaxWidth);

      &:has([data-max-content]) {
        min-width: 150px;
        max-width: max-content;

        dd {
          width: max-content;

          input, select {
            // this is not currently supported in FireFox or Safari, and is progressively enhanced with JS
            field-sizing: content;
            min-width: 150px;
            max-width: max-content;
          }
        }
      }

      // hide labels for singular buttons
      &:has(dd > button:only-child, dd > input[type=button]:only-child, dd > input[type=submit]:only-child, dd > input[type=reset]:only-child) {
        dt label {
          display: none;
        }
      }

      dd {
        position: relative;
        display: flex;
        flex-direction: column;
        gap: 10px;
        max-width: 100%;

        // checkbox/radio
        &.singleCheckbox,
        &.singleRadio {
          display: grid;
          grid-template-columns: 15px 1fr;
          align-items: center;
          padding-left: 11px;
          gap: 5px 10px;

          // anything that is not the radio/checkbox and its label
          & > :not(input[type='checkbox'], input[type='radio'], label) {
            grid-column: 1 / -1;
          }

          // additional label takes up entire width
          label:nth-of-type(2) {
            grid-column: 1 / -1;
            padding: 0 10px;
            font-size: small;
            color: var(--semanticFormsTextColor);
          }
        }

        &.singleRadio > input {
          margin: 0;
        }

        // checkbox/radio groups
        &.checkboxes, &.radios {
          ul {
            display: flex;
            flex-direction: column;
            margin-top: var(--semanticFormsInputTopMargin);
            padding-left: 10px;
            gap: 5px;

            li {
              display: flex;
              align-items: center;
              gap: 5px;
            }
          }

          // checkbox subtext
          & > label {
            padding-left: 20px;
            font-size: small;
            color: var(--semanticFormsTextColor);
          }
        }

        // singular button
        &:has(button:only-child, input[type=button]:only-child, input[type=submit]:only-child, input[type=reset]:only-child) {
          margin-left: 0;

          button, input[type=button], input[type=submit], input[type=reset] {
            height: var(--semanticFormsInputHeight);
          }
        }

        // place buttons next to input
        &:has(input ~ button:not(.clear, .show), input ~ input[type=submit], input ~ input[type=reset], input ~ input[type=button], select ~ button:not(.clear, .show),select ~ input[type=submit],select ~ input[type=reset],select ~ input[type=button]) {
          display: grid;
          grid-template-columns: 2fr 1fr;

          button.clear, button.show {
            right: 36%; // adjust clear/show button to grid
          }

          // inputs take up first section of columns
          input:not([type="submit"]), select, textarea {
            grid-row: 1 / 2;
            grid-column: 1 / 2;
          }

          // buttons take up second section of columns
          button:not(.clear, .show),
          input[type="submit"]:not(.clear, .show),
          input[type="button"]:not(.clear, .show) {
            grid-row: 1 / 2;
            grid-column: 2 / 3;
            width: 100%;
            max-width: 100%;
            padding: 10px; // reduce default padding to allow more button text to show
            height: var(--semanticFormsInputHeight);
            text-overflow: ellipsis;
            white-space: nowrap;
            overflow: hidden;
            display: inline-block;
          }

          label:nth-of-type(2),
          label:not(.floatLabelFormAnimatedLabel) {
            grid-row: 2 / 3;
            grid-column: 1 / -1;
          }
        }
      }
    }
  }

  p {
    // input/label combo
    &:has(input:not([type='checkbox'], [type='radio']), textarea, label) {
      display: flex;
      flex-direction: column;
      gap: 5px;
    }

    // checkbox or radio/label combo
    &:has(input[type='checkbox'], input[type='radio']) {
      display: flex;
      flex-direction: row;
      align-items: center;
      gap: 5px;
    }
  }

  menu:has(li > input, li > label, li > button, li > select, li > textarea),
  ul:has(li > input, li > label, li > button, li > select, li > textarea) {
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 10px;
    list-style-type: none;

    li {
      display: flex;
      align-items: center;
    }
  }

  div:has(.align-start) {
    display: flex;
    justify-content: start;
  }

  div:has(.align-center) {
    display: flex;
    justify-content: center;
  }

  div:has(.align-end) {
    display: flex;
    justify-content: end;
  }
}

.semanticForms table,
table.semanticForms {
  table-layout: fixed;
  width: 100%;
  border-spacing: 0;
  border: var(--semanticFormsBorder);
  border-radius: 10px;
  margin-bottom: 15px;
  overflow: hidden;

  tr.title th {
    background-color: var(--semanticFormsTableTitleColor);
  }

  th {
    background-color: var(--semanticFormsTableHeaderColor);
    font-weight: bold;
  }

  tr:nth-child(odd) td {
    background-color: var(--semanticFormsTableOddColor);
  }

  tr:nth-child(even) td {
    background-color: var(--semanticFormsTableEvenColor);
  }

  td, th {
    border-right: var(--semanticFormsTableBorder);
    border-bottom: var(--semanticFormsTableBorder);
    padding: 7px 10px;
  }

  th:last-of-type, td:last-of-type {
    border-right: none;
  }

  tbody {
    tr:last-of-type {
      th, td {
        border-bottom: none;
      }
    }
  }

  // adjust styling of button menus in tables
  menu:has(li > input, li > label, li > button, li > select, li > textarea, li > a) {
    width: max-content;
  }
}
