form {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
  border-radius: 0.5rem 1.5rem 0.5rem 1.5rem;
  width: 100%;
  /* caps the line length on very wide screens; centered in its section */
  max-width: 42rem;
  margin: 1rem auto 0;
  /* error mark shown inside invalid fields; fill matches --form-error (theme.css, one per theme) */
  --form-error-icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Ccircle cx='8' cy='8' r='8' fill='rgb(200,45,45)'/%3E%3Cpath d='M8 4v5' stroke='%23fff' stroke-width='1.8' stroke-linecap='round'/%3E%3Ccircle cx='8' cy='11.75' r='1' fill='%23fff'/%3E%3C/svg%3E");
}

.dark form {
  --form-error-icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Ccircle cx='8' cy='8' r='8' fill='rgb(255,91,91)'/%3E%3Cpath d='M8 4v5' stroke='%23fff' stroke-width='1.8' stroke-linecap='round'/%3E%3Ccircle cx='8' cy='11.75' r='1' fill='%23fff'/%3E%3C/svg%3E");
}

/* name + email share a line while each can keep ~14rem; otherwise they stack.
   Driven by the available width, not the viewport, so it adapts to any container. */
.form-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 250px), 1fr));
  gap: 0.75rem 1rem;
}

/* Optional reason: radio chips (the radio is visually hidden, the label is the pill).
   Arrow keys still move the selection; a second click or Space unchecks
   (js/contact-form/contact-form.js); nothing checked = "general". */
.form-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  legend {
    margin-bottom: 0.35rem;
    font-size: var(--fs-8);
    font-weight: 600;
    span {
      font-weight: 400;
      color: var(--text-muted);
    }
  }
}

.chip {
  display: inline-flex;
  align-items: center;
  min-height: 1.75rem; /* 28px: above the 24px target (WCAG 2.5.8) */
  padding: 0.2rem 0.7rem;
  font-size: var(--fs-9); /* 12px */
  color: var(--accent-color);
  background-color: var(--form-surface);
  border: 1px solid rgba(180, 201, 207, 0.5);
  /* classic symmetric pill, on purpose not the project's asymmetric corners */
  border-radius: 999px;
  cursor: pointer;
  position: relative;
  /* touch: no double-tap wait, no gray flash; the press itself is the feedback */
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  /* the checked fill changes instantly, so it lands with the finger */
  transition: border-color 0.2s ease, transform 0.1s ease;
  /* invisible hit area: half of the 0.5rem gap on each side, so a tap
     between two chips still selects the nearest one */
  &::after {
    content: "";
    position: absolute;
    inset: -0.25rem;
  }
  /* real hover only: on touch it would stick after the tap */
  @media (hover: hover) {
    &:hover {
      border-color: color-mix(in srgb, var(--form-accent) 60%, transparent);
    }
  }
  @media (prefers-reduced-motion: no-preference) {
    &:active {
      transform: scale(0.96);
    }
  }
  &:has(:checked) {
    color: #fff;
    background-color: var(--form-accent);
    border-color: var(--form-accent);
  }
  &:has(:focus-visible) {
    outline: 2px solid var(--focus-ring);
    outline-offset: 2px;
  }
}

input,
textarea {
  background-color: var(--form-surface);
  padding: 0.5rem 1rem;
  font-size: 0.875rem;
  border-radius: 0.75rem 0.25rem 0.75rem 0.25rem;
  width: 100%;
  outline: none;
  border: 1px solid rgba(180, 201, 207, 0.5);
  color: var(--accent-color);
  transition: border-color 0.2s ease;

  &:hover {
    border-color: color-mix(in srgb, var(--form-accent) 60%, transparent);
  }

  /* text fields and textarea reserve room for the error icon, so text never shifts */
  &[type="text"],
  &[type="email"],
  &:is(textarea) {
    padding-right: 2.25rem;
    background-repeat: no-repeat;
    background-size: 1rem;
    background-position: right 0.75rem center;
    &:is(textarea) {
      background-position: right 0.75rem top 0.6rem;
    }
    &:not(:placeholder-shown):invalid {
      background-image: var(--form-error-icon);
      /* the focus inset shadow would paint over the icon */
      box-shadow: none !important;
    }
  }

  &[type="text"],
  &[type="email"] {
    &:focus {
      box-shadow: 0 0 0px 1000px var(--form-surface) inset !important;
      &:placeholder-shown {
        outline: 3px solid var(--focus-ring) !important;
        color: var(--accent-color) !important;
      }
    }
    &:focus:valid {
      outline: 3px solid var(--form-success);
    }
    &:focus:not(:placeholder-shown):invalid {
      outline: 3px solid var(--form-error);
      /* text keeps the normal color: red only on the outline + icon (legibility) */
    }
    &:not(:placeholder-shown):invalid {
      outline: 3px solid var(--form-error);
    }
  }
  &:-webkit-autofill {
    box-shadow: 0 0 0px 1000px var(--form-surface) inset !important;
    -webkit-text-fill-color: var(--accent-color) !important;
    transition: background-color 5000s ease-in-out 0s;
  }
}

textarea {
  border-radius: 0.25rem 0.75rem 0.25rem 0.75rem;
  resize: vertical;
  /* grows with the text (rows="3" is the fallback where unsupported) */
  field-sizing: content;
  min-height: calc(4lh + 1rem);
  max-height: calc(12lh + 1rem);
  /* same validation states as the name and email fields */
  &:focus {
    box-shadow: 0 0 0px 1000px var(--form-surface) inset !important;
    &:placeholder-shown {
      outline: 3px solid var(--focus-ring) !important;
      color: var(--accent-color) !important;
    }
  }
  &:focus:valid {
    outline: 3px solid var(--form-success);
  }
  &:focus:not(:placeholder-shown):invalid {
    outline: 3px solid var(--form-error);
    /* text keeps the normal color: red only on the outline + icon (legibility) */
  }
  &:not(:placeholder-shown):invalid {
    outline: 3px solid var(--form-error);
  }
}

button[type="submit"] {
  background-color: var(--form-accent);
  color: #fff;
  font-weight: 600;
  padding: 0.5rem 1rem;
  font-size: 0.875rem;
  cursor: pointer;
  transition: background-color 0.25s ease-out, opacity 0.25s ease-out;
  border-radius: 0.75rem 0.25rem 0.75rem 0.25rem;
  width: 100%;
  &:hover:not(:disabled) {
    opacity: 0.85;
  }
  &:disabled {
    opacity: 0.7;
    cursor: progress;
  }
  &:focus-visible {
    outline: 2px solid var(--focus-ring);
    outline-offset: 2px;
  }
}

fieldset {
  display: grid;
  gap: 0.75rem;
  min-width: 0;
  margin: 0;
  padding: 0;
  border: none;
  label span {
    display: inline-block;
    font-size: var(--fs-8);
    font-weight: 600;
    margin-bottom: 0.35rem;
  }
  /* visual required mark; empty alt text so screen readers skip it
     (the native `required` attribute already announces the state) */
  label:has(:required) > span:first-child:not(.field-head)::after,
  label:has(:required) > .field-head > span:first-child::after {
    content: "*";
    content: "*" / "";
    margin-left: 0.2em;
    color: var(--form-accent);
  }
  /* label + character counter on one line */
  label .field-head {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    align-items: baseline;
    gap: 0 0.75rem;
    span {
      margin-bottom: 0;
    }
    .field-hint {
      margin-top: 0;
      font-variant-numeric: tabular-nums;
      span {
        display: inline;
        font-size: inherit;
        font-weight: inherit;
      }
    }
  }
}

/* required-fields notice: subtle accent bar */
.form-note {
  margin: 0 0 0.25rem;
  padding-left: 0.75rem;
  font-size: var(--fs-8);
  color: var(--text-muted);
  border-left: 2px solid var(--form-accent);
}

/* direct address above the form: visible mailto + copy button */
.contact-direct {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.25rem 0.5rem;
  font-size: var(--fs-8);
  color: var(--text-muted);
}

/* small pill, same corners and tokens as the other buttons (js/ui/copy-text.js) */
.copy-btn {
  min-height: 1.5rem; /* ≥24px target (WCAG 2.5.8) */
  padding: 0.25em 0.75em;
  font-family: var(--font-mono);
  font-size: var(--fs-9);
  color: var(--accent-color);
  background: var(--surface-light-color);
  border: 1px solid var(--line-color);
  border-radius: 0.75rem 0.25rem 0.75rem 0.25rem;
  transition: background-color 0.2s ease-out;
  &:hover {
    background: var(--surface-color);
  }
  &:focus-visible {
    outline: 2px solid var(--focus-ring);
    outline-offset: 2px;
  }
}

.field-hint {
  display: block;
  margin-top: 0.35rem;
  font-size: var(--fs-9);
  color: var(--text-muted);
}

/* Text is never red: errors are signalled by the red outline on the field and
   a red "!" icon before the message; the message itself uses the normal text
   color, so it stays as legible as the rest of the form.
   (js/contact-form/contact-form.js creates .field-error on demand.)
   small.: beats ".content-wrapper small" (muted gray, typographic.css). */
small.field-error {
  display: flex;
  align-items: flex-start;
  gap: 0.4em;
  margin-top: 0.35rem;
  font-size: var(--fs-8);
  line-height: 1.4;
  color: var(--accent-color);
  /* display: flex would override the hidden attribute (cleared errors) */
  &[hidden] {
    display: none;
  }
  &::before {
    content: "";
    flex: none;
    width: 1em;
    height: 1em;
    margin-top: 0.15em;
    background: var(--form-error-icon) center / contain no-repeat;
  }
}

/* after a failed submit, empty required fields show the error too */
:is(input, textarea)[aria-invalid="true"] {
  outline: 3px solid var(--form-error);
}

/* Turnstile: no room taken while invisible; centered when it shows a checkbox */
.form-turnstile:empty {
  display: none;
}

/* Result of a send: soft card with an icon, text in the normal color.
   tabindex="-1" (focused after success): no ring around the message. */
.form-status {
  --status-color: var(--focus-ring);
  min-height: 1.5em;
  margin: 0;
  font-size: var(--fs-8);
  line-height: 1.5;
  color: var(--accent-color);
  text-wrap: pretty;
  &:focus {
    outline: none;
  }
  /* empty: no reserved gap (kept rendered so the live region still announces) */
  &:not([data-state]) {
    min-height: 0;
  }
  &[data-state] {
    display: flex;
    align-items: flex-start;
    gap: 0.6em;
    padding: 0.75rem 1rem;
    background: color-mix(in srgb, var(--status-color) 10%, var(--form-surface));
    border: 1px solid color-mix(in srgb, var(--status-color) 45%, transparent);
    border-radius: 0.75rem 0.25rem 0.75rem 0.25rem;
    &::before {
      flex: none;
      display: grid;
      place-items: center;
      width: 1.35em;
      height: 1.35em;
      margin-top: 0.05em;
      border-radius: 50%;
      font-family: var(--font-mono);
      font-size: 0.85em;
      font-weight: 700;
      color: #fff;
      background: var(--status-color);
    }
  }
  &[data-state="success"] {
    --status-color: var(--form-success);
    &::before {
      content: "✓" / "";
    }
  }
  &[data-state="error"] {
    --status-color: var(--form-error);
    &::before {
      content: "!" / "";
    }
  }
  &[data-state="info"]::before {
    content: "i" / "";
  }
}

.form-context {
  position: absolute;
  left: -9999px;
  width: 1px;
  height: 1px;
  overflow: hidden;
  opacity: 0;
  pointer-events: none;
}
