/* Forms. Labels sit above their inputs throughout - a left-label layout wastes the horizontal room
   that document editors need, and reads worse at narrow widths. */

.form-group {
  margin-bottom: 12px;

  &:last-child {
    margin-bottom: 0;
  }
}

.form-label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--color-fg-default);
  margin-bottom: 4px;

  & .form-required {
    color: var(--color-danger-fg);
    margin-left: 2px;
  }

  & .form-optional {
    font-weight: 400;
    color: var(--color-fg-subtle);
    margin-left: 4px;
  }
}

.form-input,
.form-select,
.form-textarea {
  display: block;
  width: 100%;
  min-height: 30px;
  padding: 5px 9px;
  border: 1px solid var(--color-border-default);
  border-radius: var(--radius-md);
  background-color: var(--color-canvas-default);
  color: var(--color-fg-default);
  font-size: 13px;
  line-height: 1.4;

  &:hover:not(:disabled):not(:read-only) {
    border-color: var(--color-border-strong);
  }

  &:focus {
    outline: none;
    border-color: var(--color-accent-emphasis);
    box-shadow: 0 0 0 3px var(--color-accent-muted);
  }

  &:disabled,
  &:read-only {
    background-color: var(--color-canvas-inset);
    color: var(--color-fg-muted);
    cursor: not-allowed;
  }

  &::placeholder {
    color: var(--color-fg-subtle);
  }

  /* Blazor's EditForm marks fields itself; both classes are styled so validation reads the same
     whether it came from the client or from the API's ProblemDetails errors dictionary. */
  &.invalid,
  &[aria-invalid="true"] {
    border-color: var(--color-danger-emphasis);

    &:focus {
      box-shadow: 0 0 0 3px var(--color-danger-subtle);
    }
  }
}

.form-textarea {
  min-height: 72px;
  resize: vertical;
}

/* Anything holding a figure: mono, right-aligned, so a column of inputs in a document editor lines
   up the same way the resulting table will. */
.form-input[data-numeric="true"] {
  font-family: var(--font-mono);
  text-align: right;
}

.form-hint {
  display: block;
  font-size: 11.5px;
  color: var(--color-fg-muted);
  margin-top: 3px;
}

.form-error,
.validation-message {
  display: block;
  font-size: 11.5px;
  color: var(--color-danger-fg);
  margin-top: 3px;
}

/* The API's ProblemDetails summary, shown above a form when the failure is not attributable to one
   field. */
.form-summary {
  border: 1px solid var(--color-danger-emphasis);
  background-color: var(--color-danger-subtle);
  color: var(--color-danger-fg);
  border-radius: var(--radius-md);
  padding: 9px 11px;
  font-size: 12.5px;
  margin-bottom: 14px;

  & ul {
    margin: 6px 0 0;
    padding-left: 18px;
  }
}

/* Same shape as .form-summary, for a positive outcome (e.g. Agent Setup's "Test connection")
   rather than a failure - danger is the default because most uses of .form-summary are errors. */
.form-summary[data-variant="success"] {
  border-color: var(--color-success-emphasis);
  background-color: var(--color-success-subtle);
  color: var(--color-success-fg);
}

.validation-errors {
  margin: 0 0 12px;
  padding-left: 18px;
  color: var(--color-danger-fg);
  font-size: 12.5px;
}

.form-checkbox-row {
  display: flex;
  align-items: center;
  gap: 7px;
  margin-bottom: 12px;
  font-size: 13px;

  & input[type="checkbox"],
  & input[type="radio"] {
    width: 15px;
    height: 15px;
    accent-color: var(--color-accent-emphasis);
    margin: 0;
  }

  & label {
    cursor: pointer;
  }
}

/* Side-by-side fields. Auto-fit rather than fixed columns so a two-field row becomes one column on
   a narrow screen without a media query per form. */
.form-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
  gap: 0 12px;
}

.form-row[data-columns="2"] { grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); }
.form-row[data-columns="3"] { grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); }

.form-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 18px;
  padding-top: 14px;
  border-top: 1px solid var(--color-border-muted);
}

/* A destructive action parked at the far left of the action row, away from Save. */
.form-actions[data-has-destructive="true"] {
  justify-content: space-between;
}

/* ---- Fieldsets ---------------------------------------------------------------------------------- */

.form-section {
  border: 0;
  padding: 0;
  margin: 0 0 20px;

  & > legend {
    padding: 0;
    font-family: var(--font-display);
    font-size: 12px;
    font-weight: 600;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--color-fg-muted);
    margin-bottom: 8px;
  }
}

/* ---- Lookup (typeahead) ---------------------------------------------------------------------------- */

.lookup {
  position: relative;
  display: flex;
  gap: 6px;
  align-items: flex-start;
}

.lookup-input-wrap {
  position: relative;
  flex: 1;
  min-width: 0;

  & > input {
    width: 100%;
  }
}

/* Shown only once something is picked - clearing an empty box is what backspacing is for. Sized
   to sit inside the input's own border rather than beside it, so it reads as part of the field. */
.lookup-clear {
  position: absolute;
  top: 50%;
  right: 4px;
  transform: translateY(-50%);
  display: flex;
  align-items: center;
  justify-content: center;
  width: 18px;
  height: 18px;
  padding: 0;
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--color-fg-muted);
  font-size: 15px;
  line-height: 1;
  cursor: pointer;

  &:hover {
    background-color: var(--color-canvas-subtle);
    color: var(--color-fg-default);
  }
}

.lookup-input-wrap:has(.lookup-clear) > input {
  padding-right: 24px;
}

.lookup-menu {
  /* Placed position:fixed from the input's own rectangle by izmaLookup - these values are the
     fallback for a menu opened before the browser could be measured (a prerender). z-index sits
     above the dialog at 150, because the pickers that matter most are inside one, and below the
     toasts at 200, because an error must never end up behind a menu. */
  position: absolute;
  z-index: 160;
  top: calc(100% + 2px);
  left: 0;
  right: 0;
  max-height: 260px;
  overflow-y: auto;
  border: 1px solid var(--color-border-default);
  border-radius: var(--radius-md);
  background-color: var(--color-canvas-overlay);
  box-shadow: var(--shadow-overlay);
  padding: 3px;
}

.lookup-option {
  display: flex;
  align-items: baseline;
  gap: 8px;
  width: 100%;
  padding: 5px 8px;
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  text-align: left;
  font-size: 13px;
  cursor: pointer;

  &:hover,
  &[data-highlighted="true"] {
    background-color: var(--color-accent-subtle);
    color: var(--color-accent-fg);
  }

  & .lookup-option-code {
    font-family: var(--font-mono);
    font-size: 11.5px;
    color: var(--color-fg-muted);
  }

  & .lookup-option-note {
    margin-left: auto;
    font-size: 11.5px;
    color: var(--color-fg-subtle);
  }
}

.lookup-empty {
  padding: 10px;
  font-size: 12.5px;
  color: var(--color-fg-muted);
  text-align: center;
}

/* The menu stays open until it is dismissed, so it carries the way out with it. Sticky, because
   the list above scrolls and a Close button that scrolls away with it is not a way out. */
.lookup-foot {
  position: sticky;
  bottom: -3px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin: 3px -3px -3px;
  padding: 5px 8px;
  border-top: 1px solid var(--color-border-muted);
  background-color: var(--color-canvas-subtle);
  font-size: 11px;
  color: var(--color-fg-subtle);

  & .lookup-foot-actions {
    display: flex;
    align-items: center;
    gap: 6px;
  }
}

/* ---- Filter bar --------------------------------------------------------------------------------- */

.filter-panel {
  display: flex;
  align-items: flex-end;
  flex-wrap: wrap;
  gap: 10px;
  padding: 10px 12px;
  margin-bottom: 12px;
  border: 1px solid var(--color-border-default);
  border-radius: var(--radius-md);
  background-color: var(--color-canvas-subtle);

  & .filter-field {
    display: flex;
    flex-direction: column;
    gap: 3px;
    min-width: 150px;
  }

  & .filter-actions {
    display: flex;
    gap: 8px;
    margin-left: auto;
  }

  /* A search field with its own explicit Search button, wrapped in a <form> so Enter submits the
     same as clicking it - never fires on typing alone, since the list behind it can run into the
     thousands. */
  & .filter-search {
    display: flex;
    gap: 6px;

    & .form-input {
      flex: 1;
      min-width: 0;
    }
  }

  & .form-input,
  & .form-select {
    height: 28px;
    min-height: 28px;
    font-size: 12.5px;
  }
}
