/*
 * Shared operational form foundation.
 *
 * Load this after form-specific stylesheets. Existing workflow components may
 * keep their own layout, while controls and future form primitives inherit one
 * stable sizing and spacing contract.
 */

.form-record-page .standard-form {
  min-width: 0;
  display: grid;
  gap: var(--form-section-gap);
}

.form-record-page .standard-form,
.form-record-page .standard-form :where(fieldset, label, section, div) {
  min-width: 0;
}

.form-record-page .standard-form :where(
  input[type="text"],
  input[type="number"],
  input[type="datetime-local"],
  input[type="email"],
  input[type="tel"],
  input[type="password"],
  input[type="search"],
  input[type="date"],
  input[type="time"],
  input[type="month"],
  select
) {
  min-height: var(--form-control-min-height);
  border-radius: var(--form-control-radius);
  font-size: var(--form-control-font-size);
}

.form-record-page .standard-form textarea {
  min-height: var(--form-textarea-min-height);
  border-radius: var(--form-control-radius);
  font-size: var(--form-control-font-size);
}

.form-record-page .standard-form input[type="file"] {
  min-height: var(--form-control-min-height);
  border-radius: var(--form-control-radius);
}

/*
 * Linear and tabbed forms share a quiet field hierarchy. Workflow-specific
 * selectors can override these low-specificity defaults without !important.
 */
.form-record-page :where(
  .standard-form-linear-layout,
  .standard-form-tabbed-layout
) :where(fieldset),
.standard-form-section {
  min-width: 0;
  display: grid;
  gap: 14px;
  margin: 0;
  padding: 18px 1px 2px;
  border: 0;
  border-top: 1px solid var(--border);
  border-radius: 0;
  background: transparent;
}

.form-record-page :where(
  .standard-form-linear-layout,
  .standard-form-tabbed-layout
) :where(legend),
.standard-form-section > legend,
.standard-section-title {
  padding: 0 10px 0 0;
  color: var(--text);
  font-size: var(--form-legend-font-size);
  font-weight: 700;
}

.form-record-page :where(
  .standard-form-linear-layout,
  .standard-form-tabbed-layout
) :where(label),
.standard-form-grid {
  min-width: 0;
}

.form-record-page :where(
  .standard-form-linear-layout,
  .standard-form-tabbed-layout
) :where(label) {
  display: grid;
  gap: 5px;
  color: var(--text-sec);
  font-size: var(--form-label-font-size);
  font-weight: 600;
  text-transform: uppercase;
}

.form-record-page :where(
  .standard-form-linear-layout,
  .standard-form-tabbed-layout
) :where(
  input[type="text"],
  input[type="number"],
  input[type="datetime-local"],
  input[type="email"],
  input[type="tel"],
  input[type="password"],
  input[type="search"],
  input[type="date"],
  input[type="time"],
  input[type="month"],
  input[type="file"],
  select,
  textarea
) {
  width: 100%;
  max-width: 100%;
  min-width: 0;
  padding: 8px var(--form-control-padding-inline);
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  font-weight: 400;
  text-transform: none;
}

.form-record-page .standard-form :where(input[readonly]) {
  background: #f6faf9;
  color: var(--text-sec);
}

.form-record-page .standard-form :where(.empty-value-control) {
  border-color: #e0bd75;
  background: #fff8e8;
}

.form-record-page .standard-form :where(.empty-value-control:focus-visible) {
  border-color: var(--accent);
}

/* Reusable primitives for new forms and gradual migration of existing forms. */
.standard-form-grid {
  min-width: 0;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(220px, 100%), 1fr));
  gap: var(--form-field-gap);
}

.standard-form-grid[data-columns="1"] {
  grid-template-columns: minmax(0, 1fr);
}

.standard-form-grid[data-columns="2"] {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.standard-form-grid[data-columns="3"] {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.standard-form-grid[data-columns="4"] {
  grid-template-columns: repeat(4, minmax(0, 1fr));
}

.standard-field {
  min-width: 0;
  display: grid;
  align-content: start;
  gap: 5px;
  color: var(--text-sec);
  font-size: var(--form-label-font-size);
  font-weight: 600;
  text-transform: uppercase;
}

.standard-field-label {
  min-height: 1.5em;
  display: flex;
  align-items: flex-end;
  color: var(--text-sec);
  font-size: var(--form-label-font-size);
  font-weight: 600;
  text-transform: uppercase;
}

.standard-field-label.standard-field-label-aligned {
  min-height: 1.8em;
}

.standard-field-hint {
  margin: 0;
  color: var(--text-muted);
  font-size: var(--form-hint-font-size);
  font-weight: 400;
  letter-spacing: 0;
  text-transform: none;
}

.standard-input-group {
  min-width: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
}

.standard-input-group-unit {
  grid-template-columns: minmax(0, 1fr) 110px;
}

.standard-input-group-compact {
  grid-template-columns: minmax(0, 1fr) 48px;
}

.standard-input-addon {
  min-height: var(--form-control-min-height);
  display: grid;
  place-items: center;
  border: 1px solid var(--border);
  border-radius: var(--form-control-radius);
  background: var(--panel);
  color: var(--text-sec);
  font-size: var(--supporting-font-size);
  font-weight: 700;
  text-transform: none;
}

.standard-choice-field {
  min-width: 0;
  display: grid;
  gap: 5px;
}

.standard-segmented-control {
  width: fit-content;
  display: grid;
  grid-template-columns: repeat(2, minmax(130px, 1fr));
  gap: 3px;
  padding: 3px;
  border: 1px solid var(--border);
  border-radius: var(--form-control-radius);
  background: var(--panel);
}

.form-record-page .standard-form .standard-segmented-control label {
  display: block;
  color: var(--text-sec);
  font-size: 0.8rem;
  font-weight: 600;
  text-transform: none;
  cursor: pointer;
}

.form-record-page .standard-form .standard-segmented-control input {
  position: absolute;
  width: 1px;
  height: 1px;
  min-width: 1px;
  min-height: 1px;
  padding: 0;
  border: 0;
  margin: 0;
  opacity: 0;
  pointer-events: none;
}

.standard-segmented-control span {
  min-height: 34px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 6px 10px;
  border-radius: 5px;
}

.standard-segmented-control input:checked + span {
  background: var(--panel-soft);
  color: var(--accent);
}

.standard-segmented-control input:focus-visible + span {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.standard-form-actions {
  min-width: 0;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  padding-top: 2px;
}

.standard-form-actions .admin-status {
  flex: 1 1 220px;
  margin: 0;
}

/* Shared workflow primitives for tabbed and repeated-entry forms. */
.standard-tabs {
  max-width: 100%;
  display: flex;
  gap: 3px;
  padding: 3px;
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel-soft);
  scrollbar-width: thin;
}

.standard-tab {
  min-height: 36px;
  padding: 7px 12px;
  flex: 0 0 auto;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--text-sec);
  font-size: 0.78rem;
  font-weight: 700;
  white-space: nowrap;
}

.standard-tab:hover,
.standard-tab:focus-visible {
  background: var(--accent-soft);
  color: var(--accent);
}

.standard-tab[aria-selected="true"] {
  background: var(--panel);
  color: var(--accent);
  box-shadow: 0 1px 3px rgba(15, 23, 42, 0.12);
}

.standard-tab-panel[hidden] {
  display: none;
}

.standard-repeat-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.standard-repeat-add {
  min-height: 36px;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 7px 11px;
}

.standard-repeat-add svg,
.standard-repeat-remove svg {
  width: 16px;
  height: 16px;
}

.standard-repeat-table-wrap {
  max-height: 580px;
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: var(--form-control-radius);
  background: var(--panel);
}

.standard-repeat-table {
  width: 100%;
  min-width: 720px;
  border-collapse: collapse;
}

.standard-repeat-table th,
.standard-repeat-table td {
  padding: 7px 8px;
  border-bottom: 1px solid var(--border-soft);
  text-align: left;
  vertical-align: middle;
  white-space: nowrap;
}

.standard-repeat-table th {
  position: sticky;
  z-index: 1;
  top: 0;
  background: var(--panel);
  color: var(--text-sec);
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
}

.standard-repeat-table tr:last-child td {
  border-bottom: 0;
}

.standard-repeat-remove {
  width: 34px;
  height: 34px;
  min-height: 34px;
  display: inline-grid;
  place-items: center;
  padding: 0;
  color: #b42318;
}

.standard-photo-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.standard-photo-action {
  min-width: 138px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
}

.standard-photo-action svg,
.standard-photo-remove svg {
  width: 18px;
  height: 18px;
}

.standard-hidden-file {
  position: fixed;
  left: -10000px;
  width: 1px !important;
  min-width: 1px !important;
  max-width: 1px !important;
  height: 1px !important;
  min-height: 1px !important;
  max-height: 1px !important;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.standard-photo-status {
  margin: 0;
  color: var(--text-sec);
  font-size: 0.78rem;
}

.standard-photo-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(136px, 1fr));
  gap: 10px;
}

.standard-photo-card {
  min-width: 0;
  position: relative;
  overflow: hidden;
  border: 1px solid var(--border);
  background: var(--panel);
}

.standard-photo-view {
  width: 100%;
  min-height: 0;
  display: grid;
  padding: 0;
  border: 0;
  background: transparent;
  color: inherit;
  text-align: left;
}

.standard-photo-view img {
  width: 100%;
  aspect-ratio: 4 / 3;
  object-fit: cover;
  background: var(--panel-soft);
}

.standard-photo-view span {
  min-width: 0;
  padding: 7px 36px 7px 8px;
  overflow: hidden;
  color: var(--text-sec);
  font-size: 0.72rem;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.standard-photo-remove {
  width: 30px;
  height: 30px;
  position: absolute;
  right: 5px;
  bottom: 4px;
  display: inline-grid;
  place-items: center;
  padding: 0;
  border-color: #e9b4af;
  background: var(--panel);
  color: #b42318;
}

/*
 * Definition-rendered forms and shared workflow states.
 *
 * These selectors are intentionally semantic. Pages set state through data
 * attributes and ARIA rather than page-specific colour classes.
 */
.standard-form-definition {
  min-width: 0;
  display: grid;
  gap: var(--form-section-gap);
}

.standard-definition-host > .standard-form-definition,
.standard-definition-host > :only-child {
  width: 100%;
  grid-column: 1 / -1;
}

.standard-form-definition :where(
  input[type="text"],
  input[type="number"],
  input[type="datetime-local"],
  input[type="email"],
  input[type="tel"],
  input[type="date"],
  input[type="time"],
  input[type="file"],
  select
),
.standard-definition-host .standard-form-definition :where(
  input[type="text"],
  input[type="number"],
  input[type="datetime-local"],
  input[type="email"],
  input[type="tel"],
  input[type="date"],
  input[type="time"],
  input[type="file"],
  select
) {
  width: 100%;
  max-width: 100%;
  min-width: 0;
  min-height: var(--form-control-min-height);
  padding: 8px var(--form-control-padding-inline);
  border: 1px solid var(--border);
  border-radius: var(--form-control-radius);
  background: var(--panel);
  color: var(--text);
  font-size: var(--form-control-font-size);
  font-weight: 400;
  text-transform: none;
}

.standard-form-definition textarea,
.standard-definition-host .standard-form-definition textarea {
  width: 100%;
  max-width: 100%;
  min-width: 0;
  min-height: var(--form-textarea-min-height);
  padding: 8px var(--form-control-padding-inline);
  border: 1px solid var(--border);
  border-radius: var(--form-control-radius);
  background: var(--panel);
  color: var(--text);
  font-size: var(--form-control-font-size);
  font-weight: 400;
  text-transform: none;
  resize: vertical;
}

.standard-form-definition input[type="checkbox"],
.standard-definition-host .standard-form-definition input[type="checkbox"] {
  width: 18px;
  height: 18px;
  min-width: 18px;
  min-height: 18px;
  margin: 0;
  accent-color: var(--accent);
}

.standard-definition-section {
  padding-top: 12px;
}

.standard-section-description {
  margin: -5px 0 0;
  color: var(--text-muted);
  font-size: var(--form-hint-font-size);
}

.standard-definition-field {
  position: relative;
}

.standard-definition-field[data-field-type="checkbox"] {
  grid-template-columns: auto minmax(0, 1fr);
  align-items: center;
}

.standard-definition-field[data-field-type="checkbox"] .standard-field-label {
  grid-column: 2;
  grid-row: 1;
  align-items: center;
}

.standard-definition-field[data-field-type="checkbox"] input[type="checkbox"] {
  grid-column: 1;
  grid-row: 1;
}

.standard-definition-field[data-field-type="checkbox"] :where(.standard-field-hint, .standard-field-error) {
  grid-column: 1 / -1;
}

.standard-form-definition .standard-field-label {
  justify-content: space-between;
  gap: 8px;
}

.standard-field-badges {
  display: inline-flex;
  align-items: center;
  justify-content: flex-end;
  flex-wrap: wrap;
  gap: 4px;
}

.standard-field-badge {
  display: inline-flex;
  align-items: center;
  min-height: 20px;
  padding: 2px 6px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--panel-soft);
  color: var(--text-sec);
  font-size: 0.64rem;
  font-weight: 700;
  letter-spacing: 0;
  line-height: 1;
  text-transform: none;
  white-space: nowrap;
}

.standard-field-badge[data-field-badge="required"] {
  border-color: #e5aca7;
  background: #fff3f2;
  color: #9f2d25;
}

.standard-field-badge[data-field-badge="recommended"] {
  border-color: #e0bd75;
  background: #fff8e8;
  color: #795414;
}

.standard-field-badge[data-field-badge="calculated"] {
  border-color: #abd6ca;
  background: var(--accent-soft);
  color: var(--accent);
}

.standard-field-badge[data-field-badge="suggested"] {
  border-color: #a9c6e8;
  background: #f2f7fd;
  color: #245d97;
}

.standard-field-badge[data-field-badge="disabled"],
.standard-field-badge[data-field-badge="read-only"] {
  color: var(--text-muted);
}

.standard-definition-field[data-requirement="required"] .standard-field-label {
  color: var(--text);
}

.standard-definition-field[data-requirement="recommended"][data-value-empty="true"] :where(input, select, textarea) {
  border-color: #e0bd75;
  background: #fffdf8;
}

.standard-definition-field[data-validation-state="invalid"] :where(input, select, textarea) {
  border-color: #b42318;
  background: #fff7f6;
  box-shadow: 0 0 0 1px rgba(180, 35, 24, 0.08);
}

.standard-definition-field[data-validation-state="invalid"] :where(input, select, textarea):focus-visible {
  outline-color: #b42318;
}

.standard-field-error {
  margin: 0;
  color: #9f2d25;
  font-size: var(--form-hint-font-size);
  font-weight: 600;
  letter-spacing: 0;
  text-transform: none;
}

.standard-definition-field[data-value-state="calculated"] :where(input, select, textarea),
.standard-definition-field[data-control-state="read-only"] :where(input, select, textarea) {
  border-color: #c8ded8;
  background: #f6faf9;
  color: var(--text-sec);
}

.standard-definition-field[data-value-state="suggested"] :where(input, select, textarea) {
  border-color: #a9c6e8;
  background: #f7faff;
}

.standard-definition-field[data-control-state="disabled"] :where(input, select, textarea),
.standard-form-definition :where(input, select, textarea):disabled {
  border-color: var(--border-soft);
  background: var(--panel-soft);
  color: var(--text-muted);
  cursor: not-allowed;
  opacity: 1;
}

.standard-definition-input-group {
  grid-template-columns: minmax(0, 1fr) auto;
}

.standard-definition-input-group .standard-input-addon {
  min-width: 48px;
  padding-inline: 8px;
}

.standard-form-status {
  width: fit-content;
  max-width: 100%;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  min-height: 32px;
  padding: 5px 9px;
  border: 1px solid var(--border);
  border-radius: var(--form-control-radius);
  background: var(--panel-soft);
  color: var(--text-sec);
  font-size: var(--supporting-font-size);
  font-weight: 600;
}

.standard-form-status-marker {
  width: 9px;
  height: 9px;
  flex: 0 0 auto;
  border-radius: 50%;
  background: currentColor;
}

.standard-form-status[data-save-state="local"] {
  border-color: #e0bd75;
  background: #fff8e8;
  color: #795414;
}

.standard-form-status[data-save-state="syncing"] {
  border-color: #a9c6e8;
  background: #f2f7fd;
  color: #245d97;
}

.standard-form-status[data-save-state="syncing"] .standard-form-status-marker {
  border: 2px solid currentColor;
  border-right-color: transparent;
  background: transparent;
  animation: standard-form-status-spin 0.8s linear infinite;
}

.standard-form-status[data-save-state="saved"] {
  border-color: #abd6ca;
  background: var(--accent-soft);
  color: var(--accent);
}

@keyframes standard-form-status-spin {
  to {
    transform: rotate(360deg);
  }
}

@media (prefers-reduced-motion: reduce) {
  .standard-form-status[data-save-state="syncing"] .standard-form-status-marker {
    animation: none;
  }
}

@media (max-width: 760px) {
  .form-record-page .standard-form :where(
    input[type="text"],
    input[type="number"],
    input[type="datetime-local"],
    input[type="email"],
    input[type="tel"],
    input[type="password"],
    input[type="search"],
    input[type="date"],
    input[type="time"],
    input[type="month"],
    select,
    textarea
  ) {
    min-height: var(--form-control-min-height-touch);
    font-size: 16px;
  }

  .form-record-page .standard-form input[type="file"] {
    min-height: var(--form-control-min-height-touch);
    font-size: 16px;
  }

  .standard-form-grid[data-columns] {
    grid-template-columns: minmax(0, 1fr);
  }

  .standard-input-group {
    grid-template-columns: minmax(0, 1fr);
  }

  .standard-input-group-unit {
    grid-template-columns: minmax(0, 1fr) 104px;
  }

  .standard-input-group-compact {
    grid-template-columns: minmax(0, 1fr) 48px;
  }

  .standard-input-addon {
    min-height: var(--form-control-min-height-touch);
  }

  .standard-segmented-control {
    width: 100%;
  }

  .standard-tabs {
    position: relative;
    z-index: 1;
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 4px;
    margin-inline: 0;
    padding: 5px;
    overflow: visible;
    border-color: var(--border);
    background: rgba(242, 248, 246, 0.98);
    box-shadow: none;
  }

  .standard-tab {
    width: 100%;
    min-width: 0;
    min-height: var(--form-control-min-height-touch);
    padding: 5px 6px;
    border: 1px solid var(--border-soft);
    background: rgba(255, 255, 255, 0.72);
    line-height: 1.2;
    white-space: normal;
  }

  .standard-tab[aria-selected="true"] {
    border-color: rgba(15, 118, 110, 0.42);
    background: var(--panel);
  }

  .standard-repeat-table-wrap {
    order: 1;
    max-height: none;
    overflow: visible;
    border: 0;
    background: transparent;
  }

  .standard-repeat-toolbar {
    order: 2;
    padding-top: 2px;
  }

  .standard-repeat-table {
    min-width: 0;
  }

  .standard-repeat-table,
  .standard-repeat-table tbody,
  .standard-repeat-table tr,
  .standard-repeat-table td {
    width: 100%;
    display: block;
  }

  .standard-repeat-table thead {
    display: none;
  }

  .standard-repeat-table tr {
    display: grid;
    gap: 8px;
    padding: 12px 0;
    border-bottom: 1px solid var(--border);
  }

  .standard-repeat-table tr:first-child {
    padding-top: 0;
  }

  .standard-repeat-table td {
    display: grid;
    grid-template-columns: 105px minmax(0, 1fr);
    align-items: center;
    gap: 8px;
    padding: 0;
    border: 0;
    white-space: normal;
  }

  .standard-repeat-table td::before {
    content: attr(data-label);
    color: var(--text-sec);
    font-size: 0.7rem;
    font-weight: 700;
    text-transform: uppercase;
  }

  .standard-repeat-table td:last-child {
    display: flex;
    justify-content: flex-end;
  }

  .standard-repeat-table td:last-child::before {
    display: none;
  }

  .standard-photo-action {
    flex: 1 1 130px;
  }

  .standard-photo-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .standard-form-definition :where(
    input[type="text"],
    input[type="number"],
    input[type="datetime-local"],
    input[type="email"],
    input[type="tel"],
    input[type="date"],
    input[type="time"],
    input[type="file"],
    select,
    textarea
  ),
  .standard-definition-host .standard-form-definition :where(
    input[type="text"],
    input[type="number"],
    input[type="datetime-local"],
    input[type="email"],
    input[type="tel"],
    input[type="date"],
    input[type="time"],
    input[type="file"],
    select,
    textarea
  ) {
    min-height: var(--form-control-min-height-touch);
    font-size: 16px;
  }
}
