/* Forms: fields and their captions, inputs, the form grid, sections and
   footer, the line grid (1A) - then the combobox and the date picker (1C).

   The field part is in the `components` layer (tokens.css), so a utility on
   a field (`.field.mt`, `.note.mt`) wins. The few rules that have to beat
   an unlayered table rule in datatable.css (the line grid's cells) sit
   outside the layer, at the end of the field part. */

@layer components {

/* ── fields ───────────────────────────────────────────────────────────── */
label.field, div.field, .field { display: block; margin-bottom: var(--s-3); min-width: 0; }
/* In a page head a field is a filter beside the buttons, not one of a stack:
   its bottom margin only pushed the head taller and the input below the
   buttons' line. */
.page-head .field { margin-bottom: 0; }
/* The caption: field()'s .field-label, and `label.field > span`, the
   hand-built spelling. (div.field > label and .lbl are legacy.css.) */
label.field > span, .field-label {
  display: block; font-size: var(--fs-sm); line-height: var(--lh-sm); font-weight: 500;
  color: var(--text-muted); margin-bottom: var(--s-1);
}
.field-label { display: flex; align-items: center; gap: var(--s-1); }
/* Required: a red asterisk the eye finds and a screen reader skips (the
   control itself says aria-required). */
.req { color: var(--danger); margin-inline-start: var(--s-half); font-weight: 600; }
/* Help under a control. (The older `.note` is legacy.css.) */
.hint { font-size: var(--fs-sm); line-height: var(--lh-sm); color: var(--text-subtle); margin-top: var(--s-1); }
.field-error {
  display: flex; align-items: flex-start; gap: var(--s-1);
  color: var(--danger); font-size: var(--fs-sm); line-height: var(--lh-sm); margin-top: var(--s-1);
}
.field-error .ico { width: 13px; height: 13px; margin-top: 2px; }

/* ── inputs ───────────────────────────────────────────────────────────── */
.input, .select, textarea.input {
  height: var(--control-h); width: 100%; min-width: 0; padding: 0 var(--s-2);
  border: 1px solid var(--border-input); border-radius: var(--r-md);
  background: var(--surface); color: var(--text); font: inherit; font-size: var(--fs-md);
  transition: border-color var(--dur-fast) var(--ease), box-shadow var(--dur-fast) var(--ease);
}
textarea.input { height: auto; min-height: calc(var(--control-h) * 2); padding: var(--s-1h) var(--s-2); resize: vertical; line-height: var(--lh-md); }
.input:hover, .select:hover { border-color: var(--text-subtle); }
.input:focus, .select:focus, .input:focus-visible, .select:focus-visible {
  outline: none; border-color: var(--accent); box-shadow: var(--focus);
}
.input::placeholder { color: var(--text-subtle); opacity: 1; }
.input:disabled, .select:disabled { background: var(--surface-2); color: var(--text-disabled); border-color: var(--border); cursor: not-allowed; }
.input[readonly] { background: var(--surface-2); }
/* An error is a red edge AND the message under it (colour is never the only
   signal); focus keeps the red. */
.input[aria-invalid="true"], .select[aria-invalid="true"], .input.bad {
  border-color: var(--danger);
}
.input[aria-invalid="true"]:focus, .select[aria-invalid="true"]:focus, .input.bad:focus {
  box-shadow: 0 0 0 2px var(--surface), 0 0 0 4px var(--danger);
}
/* Figures are typed and read right-aligned and tabular, like the column
   they end up in. */
.input.num-input { text-align: end; font-variant-numeric: tabular-nums lining-nums; }
.input-wrap { position: relative; display: flex; align-items: center; min-width: 0; }
.input-wrap > .input { padding-inline-end: calc(var(--s-2) + var(--addon-w, 0px)); }
.input-addon {
  position: absolute; inset-inline-end: var(--s-2); pointer-events: none;
  font-size: var(--fs-sm); color: var(--text-subtle); white-space: nowrap;
}

/* ── checkbox and segmented ───────────────────────────────────────────── */
.checkbox { display: inline-flex; align-items: center; gap: var(--s-2); min-height: 24px; cursor: pointer; }
.checkbox input, input[type="checkbox"], input[type="radio"] { accent-color: var(--accent-solid); width: 15px; height: 15px; margin: 0; }
.segmented {
  display: inline-flex; flex-wrap: wrap; gap: var(--s-half); padding: var(--s-half); max-width: 100%;
  border: 1px solid var(--border-input); border-radius: var(--r-md); background: var(--surface-2);
}
.seg {
  height: calc(var(--control-h) - 6px); padding: 0 var(--s-3); border: 1px solid transparent;
  border-radius: var(--r-sm); background: none; color: var(--text-muted);
  font: inherit; font-size: var(--fs-md); font-weight: 500; cursor: pointer; white-space: nowrap;
}
.seg:hover { color: var(--text); background: var(--surface-3); }
.seg[aria-checked="true"] {
  background: var(--surface); color: var(--text); font-weight: 600;
  border-color: var(--border-strong); box-shadow: var(--shadow-1);
}
/* The checked segment's shadow would hide the base focus ring (this layer
   wins), and in a radiogroup the checked segment is the only one Tab reaches. */
.seg:focus-visible, .seg[aria-checked="true"]:focus-visible { box-shadow: var(--focus); }

/* ── form grid, section, footer ───────────────────────────────────────────
   At most --cols columns, each at least 220px: the grid drops a column
   whenever the next one would be narrower, so the same form works in a
   page, a modal and a phone without a breakpoint. The wrapper is a size
   container so a field that spans columns stops spanning when there are
   not enough of them. */
.form-grid-wrap { container-type: inline-size; min-width: 0; }
.form-grid {
  --cols: 2;
  display: grid; gap: var(--s-3) var(--s-4); align-items: start;
  grid-template-columns: repeat(auto-fill,
    minmax(max(var(--form-col-min), calc((100% - (var(--cols) - 1) * var(--s-4)) / var(--cols))), 1fr));
}
.form-grid > .field, .form-grid > label.field, .form-grid > div.field { margin-bottom: 0; }
.form-grid > .span-2 { grid-column: span 2; }
.form-grid > .span-3 { grid-column: span 3; }
.form-grid > .span-4 { grid-column: span 4; }
.form-grid > .span-all { grid-column: 1 / -1; }
@container (max-width: 940px) { .form-grid > .span-4 { grid-column: 1 / -1; } }
@container (max-width: 700px) { .form-grid > .span-3 { grid-column: 1 / -1; } }
@container (max-width: 460px) { .form-grid > .span-2 { grid-column: 1 / -1; } }

.form-section > .card-head { flex-wrap: wrap; }
.form-section-desc { flex-basis: 100%; margin-top: calc(-1 * var(--s-1)); font-size: var(--fs-sm); line-height: var(--lh-sm); color: var(--text-muted); }
/* Cancel · Save draft · primary, bottom right, and still there after
   scrolling past a long line grid. */
.form-footer {
  position: sticky; bottom: 0; z-index: var(--z-sticky);
  display: flex; align-items: center; justify-content: flex-end; flex-wrap: wrap; gap: var(--s-2);
  margin-top: var(--s-4); padding: var(--s-3) var(--s-4);
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-lg);
  box-shadow: var(--shadow-2);
}
.form-footer .form-footer-note { margin-inline-end: auto; font-size: var(--fs-sm); color: var(--text-muted); }

/* ── line grid (components/lines.js) ──────────────────────────────────── */
.linegrid { min-width: 0; }
.lg-foot { display: flex; align-items: center; gap: var(--s-3); flex-wrap: wrap; margin-top: var(--s-2); }
.lg-foot .hint { margin: 0; margin-inline-start: auto; font-size: var(--fs-xs); }
.lg-errors { margin-top: var(--s-2); }

} /* @layer components */

/* A phone scrolls the grid inside its frame rather than squeezing every
   input to a few characters. */
.linegrid table.tbl-entry { min-width: 640px; }
@media (max-width: 600px) { .lg-foot .hint { display: none; } }

/* The line grid's cells: tighter than a list table, whose unlayered
   `table.tbl td` rule in datatable.css would otherwise win over any layered
   rule here. */
.linegrid table.tbl td { padding: var(--s-half) var(--s-1); vertical-align: top; }
.linegrid table.tbl th { padding: var(--s-1h) var(--s-2); }
.linegrid table.tbl td.lg-no, .linegrid table.tbl th.lg-no { width: 32px; padding-inline: var(--s-2); color: var(--text-subtle); text-align: end; vertical-align: middle; font-size: var(--fs-sm); }
.linegrid table.tbl td.lg-out { padding: var(--s-1h) var(--s-2); vertical-align: middle; }
.linegrid table.tbl td.lg-del { width: 34px; vertical-align: middle; }
.linegrid table.tbl tfoot td { padding: var(--s-2); }
.linegrid table.tbl .input, .linegrid table.tbl .select { height: calc(var(--control-h) - 2px); }
.linegrid table.tbl tbody tr:hover { background: transparent; }
.linegrid table.tbl tbody tr:focus-within { background: var(--row-hover); }

@layer components {

/* ── combobox (1C) ────────────────────────────────────────────────────
   A WAI-ARIA combobox: the highlighted option is aria-activedescendant, the
   chosen one aria-selected. Both are drawn, so a mouse user sees what the
   keyboard would pick and which one is already chosen. */
.combo { position: relative; }
.combo.clearable .input { padding-inline-end: 30px; }
.combo-clear {
  position: absolute; inset-block-start: 50%; inset-inline-end: 3px; transform: translateY(-50%);
  display: grid; place-items: center; width: 24px; height: 24px; border: 0; border-radius: var(--r-sm);
  background: none; color: var(--text-muted); cursor: pointer;
}
.combo-clear:hover { background: var(--surface-2); color: var(--text); }
.combo-clear .ico { width: 13px; height: 13px; }
.combo-menu {
  position: absolute; z-index: var(--z-dropdown); inset-inline: 0; top: calc(100% + 3px);
  background: var(--surface); border: 1px solid var(--border-strong); border-radius: var(--r-lg);
  box-shadow: var(--shadow-2); max-height: 280px; overflow-y: auto; padding: 4px;
  min-width: 220px;
}
/* Label over sublabel on the left, the status badge on the right. */
.combo-opt {
  display: grid; grid-template-columns: minmax(0, 1fr) auto; column-gap: 8px; align-items: center;
  padding: 6px 9px; border-radius: var(--r-sm); cursor: pointer;
}
.combo-opt > :not(.badge) { grid-column: 1; }
.combo-opt > .badge { grid-column: 2; grid-row: 1 / span 2; }
.combo-opt-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.combo-opt small { display: block; color: var(--text-muted); font-size: 11px; }
.combo-opt.on { background: var(--accent-subtle); }
.combo-opt[aria-selected="true"] .combo-opt-label { font-weight: 600; }
.combo-opt[aria-selected="true"] .combo-opt-label::after { content: ' ✓'; color: var(--accent); }
.combo-empty { padding: 10px; color: var(--text-muted); text-align: center; font-size: 12px; }

/* ── date picker (1C) ──────────────────────────────────────────────────
   A ledger date is not a birthday: what decides whether it is valid is the
   accounting period, so the grid shows period status directly and a chip
   beside the field names the period the date falls in. */
.dp { position: relative; display: flex; align-items: center; flex-wrap: wrap; gap: 4px; }
.dp-field { display: flex; align-items: center; gap: 4px; flex: 1 1 150px; min-width: 126px; }
/* width 0 with a flex basis: an input's own width is about 20 characters,
   and as a flex item that became the least the picker would shrink to - two
   of them in a range pushed a phone's page sideways. */
.dp .input { padding-inline-end: 4px; flex: 1 1 108px; min-width: 92px; width: 0; }
.dp-btn {
  height: var(--control-h); width: var(--control-h); flex: none; display: grid; place-items: center;
  border: 1px solid var(--border-input); border-radius: var(--r-md); background: var(--surface);
  color: var(--text-muted); cursor: pointer;
}
.dp-btn:hover { background: var(--surface-2); color: var(--text); }
.dp-status { display: flex; gap: 4px; flex-wrap: wrap; margin-inline-start: 2px; }
.dp-status:empty { display: none; }
.dp-status .badge { white-space: nowrap; }
.dp-msg { flex: 1 1 100%; color: var(--danger); font-size: 11.5px; }

.dp-pop {
  position: absolute; z-index: var(--z-popover); top: calc(100% + 5px); inset-inline-start: 0;
  background: var(--surface); border: 1px solid var(--border-strong); border-radius: var(--r-lg);
  box-shadow: var(--shadow-2); overflow: hidden;
}
.dp-pop.dp-pop-end { inset-inline-start: auto; inset-inline-end: 0; }
.dp-body { display: flex; }
.dp-presets {
  display: flex; flex-direction: column; gap: 2px; padding: 8px;
  border-inline-end: 1px solid var(--border); background: var(--surface-2); min-width: 132px;
}
.dp-preset {
  text-align: start; padding: 5px 8px; border: none; background: none; border-radius: var(--r-sm);
  color: var(--text-muted); font: inherit; font-size: var(--fs-sm); cursor: pointer; white-space: nowrap;
}
.dp-preset:hover:not(:disabled) { background: var(--accent-subtle); color: var(--accent); }
.dp-preset:disabled { opacity: .45; cursor: not-allowed; }

.dp-cal { padding: 8px; }
.dp-head { display: flex; align-items: center; gap: 2px; margin-bottom: 6px; }
.dp-title { flex: 1; text-align: center; font-weight: 600; font-size: 12.5px; }
.dp-title-note { color: var(--danger); font-weight: 600; }
.dp-nav {
  border: none; background: none; color: var(--text-muted); cursor: pointer;
  min-width: 24px; min-height: 24px; padding: 3px 6px; border-radius: var(--r-sm); font: inherit; line-height: 1;
}
.dp-nav:hover { background: var(--surface-2); color: var(--text); }

.dp-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 1px; }
/* Rows are real ARIA rows for a screen reader, and nothing to the layout. */
.dp-week { display: contents; }
.dp-dow span {
  text-align: center; font-size: 10px; font-weight: 600; color: var(--text-muted);
  text-transform: uppercase; letter-spacing: .04em; padding: 3px 0;
}
.dp-day {
  height: 28px; width: 30px; border: none; background: none; border-radius: var(--r-sm);
  color: var(--text); font: inherit; font-size: 12px; cursor: pointer;
  font-variant-numeric: tabular-nums;
}
.dp-day:hover:not(:disabled):not([aria-disabled="true"]) { background: var(--surface-2); }
.dp-day:focus-visible, .dp-nav:focus-visible, .dp-preset:focus-visible, .dp-btn:focus-visible {
  outline: none; box-shadow: var(--focus);
}
/* Days of the next and previous month can be picked, so they are text, not
   disabled text: --text-subtle keeps 4.5:1 where --text-disabled is 2.6:1. */
.dp-day.out { color: var(--text-subtle); }
.dp-day.weekend:not(.sel):not(.out) { color: var(--text-muted); }
.dp-day.today { box-shadow: inset 0 0 0 1px var(--accent); font-weight: 700; }
.dp-day.closed:not(.sel) { background: var(--danger-subtle); color: var(--danger); }
.dp-day.sel { background: var(--accent-solid); color: var(--accent-fg); font-weight: 600; }
.dp-day:disabled, .dp-day[aria-disabled="true"] { opacity: .35; cursor: not-allowed; }

.dp-legend {
  display: flex; gap: 12px; margin-top: 7px; padding-top: 6px;
  border-top: 1px solid var(--border); font-size: 10.5px; color: var(--text-muted);
}
.dp-legend span { display: flex; align-items: center; gap: 4px; }
.dp-swatch { width: 9px; height: 9px; border-radius: 2px; display: inline-block; }
.dp-swatch.closed { background: var(--danger-subtle); border: 1px solid var(--danger); }
.dp-swatch.today  { box-shadow: inset 0 0 0 1px var(--accent); }

.dp-range { display: flex; flex-direction: column; gap: 7px; }
.dp-range > .row { flex-wrap: wrap; }
.dp-range > .row > .dp { flex: 0 1 200px; }
.dp-quick { display: flex; gap: 5px; flex-wrap: wrap; }

@media (max-width: 600px) {
  /* "from [date] to [date]" stays one line on a phone: two 200px pickers
     and the "to" between them do not fit 358px, and a "to" left dangling
     at a line's end reads as a typo. */
  .dp-range > .row { flex-wrap: nowrap; }
  .dp-range > .row > .dp { flex: 1 1 0; min-width: 0; }
  .dp-range > .row > .dp .dp-field { min-width: 0; }
  .dp-range > .row > .dp .input { min-width: 0; }
}
@media (max-width: 480px) {
  /* The quick dates would push the calendar off a phone's edge. */
  .dp-presets { display: none; }
  .dp-pop { max-width: calc(100vw - 32px); }
}

} /* @layer components: combobox, date picker */

/* ── contracting and the manual journal: inline controls ─────────────── */
@layer components {
  .row-inline { display: flex; gap: var(--s-1h); align-items: center; }
  .row-inline .input { flex: 1; }
  label.field.check { display: flex; align-items: center; gap: var(--s-2); margin-top: 22px; }
  label.field.check > span { margin: 0; }

  .dim-row { display: flex; flex-wrap: wrap; gap: var(--s-2); }
  .dim-row label.field.dim { margin-bottom: 0; min-width: 150px; flex: 1; }
}
