.commodity-form {
  margin: 1.25em 0;
  padding: 1em;
  border: 1px solid #ddd;
  border-radius: 0.3em;
  background-color: var(--bg-color, #fff);
}

.form-group {
  margin-bottom: 1em;
}

.hidden-field {
  display: none !important;
}

.form-group label {
  display: block;
  margin-bottom: 0.3em;
  font-weight: bold;
}

.form-group input {
  width: 100%;
  padding: 0.5em;
  border: 1px solid #ccc;
  border-radius: 0.25em;
  box-sizing: border-box;
}

.form-group small {
  display: block;
  margin-top: 0.3em;
  color: #666;
  font-size: 0.8em;
}

button.button {
  background-color: var(--tg-theme-button-color, #4caf50);
  color: var(--tg-theme-button-text-color, white);
  padding: 0.6em 1em;
  border: none;
  border-radius: 0.25em;
  cursor: pointer;
  font-size: 1em;
  min-width: 10em;
}

button.button:hover {
  opacity: 0.9;
}

button.button.compact,
a.button.compact {
  min-width: auto;
  padding: 0.4em 0.6em;
  font-size: 1.2em;
}

.htmx-indicator {
  display: none;
  margin-left: 0.6em;
  color: #666;
}

.htmx-request .htmx-indicator {
  display: inline;
}

.htmx-request.button {
  opacity: 0.7;
  cursor: wait;
}

.form-banner {
  margin-bottom: 1em;
  padding: 0.6em 0.8em;
  border: 1px solid #d9534f;
  border-radius: 0.25em;
  background-color: #fdf0ef;
  color: #a94442;
}

.form-banner p {
  margin: 0;
}

.field-error,
.row-error {
  display: block;
  margin-top: 0.25em;
  color: #d9534f;
  font-size: 0.9em;
}

.invalid,
.invalid input,
.invalid select {
  border-color: #d9534f;
}

.row-group {
  margin-bottom: 0.8em;
}

.nested-rows {
  margin: 0.4em 0 0 1.5em;
  padding-left: 0.8em;
  border-left: 2px solid #ddd;
}

.orphan-rows {
  margin-top: 1em;
  padding: 0.6em;
  border: 1px dashed #d9534f;
  border-radius: 0.25em;
}

/* The shared forms (form_render): forms/form.html and its parts. */

.nomi-form {
  margin: 1.25em 0;
  padding: 1em;
  border: 1px solid #ddd;
  border-radius: 0.3em;
  background-color: var(--bg-color, #fff);
}

.nomi-form .field {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.25em 0.5em;
  margin-bottom: 0.4em;
}

.nomi-form .field > span:first-child {
  width: 7em;
  flex-shrink: 0;
  font-weight: bold;
}

.nomi-form .field input:not([type="hidden"]),
.nomi-form .field textarea,
.nomi-form .field select {
  flex: 1;
  min-width: 0;
  padding: 0.4em;
  border: 1px solid #ccc;
  border-radius: 0.25em;
  box-sizing: border-box;
  font: inherit;
  background-color: #fff;
  color: inherit;
}

.nomi-form .field textarea {
  resize: vertical;
}

.nomi-form textarea[name="source"] {
  min-height: 26em;
  font-family: ui-monospace, monospace;
  line-height: 1.4;
  white-space: pre;
  overflow: auto;
}

.template-test-invalid .field-error {
  display: block;
  font-family: ui-monospace, monospace;
  white-space: pre-wrap;
}

.nomi-form .field .field-errors {
  flex-basis: 100%;
}

.nomi-form .field .field-error {
  margin-top: 0;
}

.nomi-form .form-rows {
  margin: 1em 0;
  padding: 0.6em 0.8em;
  border: 1px solid #e0e0e0;
  border-radius: 0.25em;
}

.nomi-form .form-rows > legend {
  padding: 0 0.3em;
  font-weight: bold;
}

.nomi-form .row-group {
  margin-bottom: 1em;
  padding: 0.6em;
  border: 1px solid #e0e0e0;
  border-radius: 0.25em;
  background-color: #f9f9f9;
}

/* Every row of a kind is one grid line with fixed columns, so the cells,
   their labels and the remove button line up across rows; a cell is placed
   by its data-cell key and keeps its column even when a neighbour is hidden.
   Each control spans the three subgrid rows: its title, the input, its
   errors. */

.nomi-form .form-row {
  display: grid;
  grid-template-rows: auto auto auto;
  column-gap: 0.8em;
  align-items: start;
  margin-bottom: 0.4em;
}

/* Splits, split tags and transaction tags share one set of column tracks,
   so a tag's name, value and description sit under a split's from, to and
   commodities, and every remove button shares the last column. */
.nomi-form [data-row] {
  grid-template-columns:
    minmax(0, 2fr) minmax(0, 2fr) minmax(0, 1fr) minmax(0, 1fr)
    minmax(0, 1fr) minmax(0, 1fr) 8.5em;
}

.nomi-form .nested-rows {
  margin: 0.4em 0 0;
  padding: 0.4em 0 0;
  border-left: none;
  border-top: 1px dashed #ddd;
}

.nomi-form .form-row > .field {
  display: grid;
  grid-row: 1 / span 3;
  grid-template-rows: subgrid;
  margin-bottom: 0;
}

.nomi-form [data-cell="from"],
.nomi-form [data-cell="name"] {
  grid-column: 1;
}

.nomi-form [data-cell="to"],
.nomi-form .form-row > [data-cell="value"] {
  grid-column: 2;
}

.nomi-form [data-cell="from-commodity"] {
  grid-column: 3;
}

.nomi-form [data-cell="description"] {
  grid-column: 3 / span 2;
}

.nomi-form [data-row="tags"] > [data-cell="description"] {
  grid-column: 3 / -2;
}

.nomi-form [data-cell="side"] {
  grid-column: 5 / span 2;
}

.nomi-form [data-row="splits"] > [data-cell="value"] {
  grid-column: 4;
}

.nomi-form [data-row="splits"] > [data-cell="to-commodity"] {
  grid-column: 5;
}

.nomi-form
  [data-row="splits"]:has(> [data-cell="to-amount"].hidden-field)
  > [data-cell="to-commodity"] {
  grid-column: 5 / span 2;
}

.nomi-form [data-cell="to-amount"] {
  grid-column: 6;
}

.nomi-form .form-row .field > span:first-child {
  width: auto;
  font-size: 0.9em;
}

.nomi-form .form-row .field input:not([type="hidden"]),
.nomi-form .form-row .field textarea,
.nomi-form .form-row .field select {
  width: 100%;
  height: 2.2em;
}

.nomi-form .form-row .field textarea {
  min-height: 2.2em;
  padding-block: 0.3em;
  line-height: 1.4;
}

.nomi-form .form-row > button[data-action="remove-row"] {
  grid-column: -2 / -1;
  grid-row: 2;
  align-self: stretch;
  justify-self: stretch;
}

.nomi-form .form-row > .row-error {
  grid-column: 1 / -1;
  grid-row: 4;
}

.nomi-form .nested-rows .form-row .field > span:first-child {
  font-weight: normal;
}

.nomi-form button[data-action] {
  justify-self: start;
  padding: 0.3em 0.6em;
  border: none;
  border-radius: 0.25em;
  color: white;
  font-size: 0.85em;
  cursor: pointer;
}

.nomi-form button[data-action="add-row"] {
  background-color: #28a745;
}

.nomi-form button[data-action="remove-row"] {
  background-color: #dc3545;
}

.nomi-form button[type="submit"] {
  margin-top: 0.6em;
  padding: 0.6em 1em;
  min-width: 10em;
  border: none;
  border-radius: 0.25em;
  background-color: var(--tg-theme-button-color, #4caf50);
  color: var(--tg-theme-button-text-color, white);
  font-size: 1em;
  cursor: pointer;
}

.nomi-form button:hover {
  opacity: 0.9;
}

.nomi-form .result-box:empty {
  display: none;
}

.nomi-form .result-box {
  margin-top: 0.6em;
  padding: 0.5em 0.8em;
  border-radius: 0.25em;
}

.nomi-form .result-box.valid-message {
  border: 1px solid #5cb85c;
  background-color: #eff8ef;
}

.nomi-form .result-box.error-message {
  border: 1px solid #d9534f;
  background-color: #fdf0ef;
}

.nomi-form .form-reload {
  display: inline-block;
  margin-top: 0.4em;
  font-weight: bold;
}

/* A narrow screen stacks a row's cells, each still title over input. */
@media (max-width: 720px) {
  .nomi-form .field > span:first-child {
    width: 100%;
  }

  .nomi-form .form-row,
  .nomi-form [data-row] {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: none;
    row-gap: 0.4em;
  }

  .nomi-form .form-row > .field,
  .nomi-form .form-row > [data-cell],
  .nomi-form .form-row > button[data-action="remove-row"],
  .nomi-form .form-row > .row-error {
    grid-column: 1;
    grid-row: auto;
  }

  .nomi-form
    [data-row="splits"]:has(> [data-cell="to-amount"].hidden-field)
    > [data-cell="to-commodity"] {
    grid-column: 1;
  }

  .nomi-form .form-row > .field {
    display: flex;
    flex-direction: column;
    align-items: stretch;
  }

  /* On a phone every box costs its border and padding on both sides of a
     narrow screen: the form, its sections and its row groups draw none, and
     a row group is set apart by a divider line only. */
  .nomi-form,
  .nomi-form .form-rows,
  .nomi-form .row-group {
    margin: 0;
    padding: 0;
    border: none;
    border-radius: 0;
    background: none;
  }

  .nomi-form .form-rows {
    margin-top: 1em;
  }

  .nomi-form .form-rows > legend {
    padding: 0;
    margin-bottom: 0.3em;
  }

  .nomi-form .row-group {
    padding: 0.6em 0;
    border-bottom: 1px solid #ddd;
  }

  .nomi-form .nested-rows {
    border-top: none;
  }

  .nomi-form .field input:not([type="hidden"]),
  .nomi-form .field select {
    min-height: 48px;
  }

  /* A form's own button takes the full width and a 48px row, so it is one
     thumb press away wherever the thumb lands. */
  .nomi-form button[data-action],
  .nomi-form button[type="submit"] {
    display: block;
    width: 100%;
    min-height: 48px;
    margin: 0.6em 0;
    padding: 0.6em 1em;
    font-size: inherit;
  }
}

@media (prefers-color-scheme: dark) {
  .nomi-form,
  .nomi-form .form-rows {
    border-color: #404040;
    background-color: #1f1f1f;
  }

  .nomi-form .row-group {
    border-color: #404040;
    background-color: #262626;
  }

  .nomi-form .nested-rows {
    border-top-color: #404040;
  }

  .nomi-form .field input:not([type="hidden"]),
  .nomi-form .field textarea,
  .nomi-form .field select {
    border-color: #555;
    background-color: #2a2a2a;
    color: #e0e0e0;
    color-scheme: dark;
  }

  .nomi-form .form-banner,
  .nomi-form .result-box.error-message {
    border-color: #a94442;
    background-color: #3a2020;
    color: #f2b8b5;
  }

  .nomi-form .result-box.valid-message {
    background-color: #1f3320;
  }

  .nomi-form .field-error,
  .nomi-form .row-error {
    color: #ff7b72;
  }
}

.nomi-form .result-box .error-alert {
  margin: 0;
  padding: 0;
  border: none;
  background: none;
  color: inherit;
}

@media (max-width: 720px) and (prefers-color-scheme: dark) {
  .nomi-form,
  .nomi-form .form-rows,
  .nomi-form .row-group {
    background: none;
  }
}
