/**
 * Form field component (L3). Single mock-matching field treatment shared by
 * native BS5 controls (.form-control / .form-select / native inputs / textarea)
 * and the drawer field classes (.inp / .sel). Field layout helpers
 * (.fld / .fld-row) live here too so all field styling has one home.
 * Mirrors docs/mocks/app/assets/css/billgrid.css; canonical Warm-Ledger tokens;
 * scoped body.bg-app; the .fld/.inp contract also serves the client document pages (body.client-document).
 */

/* Shared border / radius / type for every field control. */
body.bg-app input[type="text"],
body.bg-app input[type="email"],
body.bg-app input[type="password"],
body.bg-app input[type="search"],
body.bg-app input[type="number"],
body.bg-app input[type="date"],
body.bg-app input[type="time"],
body.bg-app textarea,
body.bg-app select:not([data-bg-select-bound]):not(.tomselected),
body.bg-app .inp,
body.client-document .inp,
body.bg-app select.sel:not([data-bg-select-bound]):not(.tomselected) {
  border-radius: var(--r-sm);
  border: 1px solid var(--border);
  font-size: var(--fs-body);
  background-color: var(--bg-panel);
  color: var(--text);
}

body.bg-app input::placeholder,
body.bg-app textarea::placeholder {
  color: var(--text-muted);
}

/* Shared focus ring (gold border + gold-soft halo). */
body.bg-app input:focus,
body.bg-app textarea:focus,
body.bg-app select:not([data-bg-select-bound]):not(.tomselected):focus,
body.bg-app .inp:focus,
body.client-document .inp:focus,
body.bg-app select.sel:not([data-bg-select-bound]):not(.tomselected):focus {
  outline: 0;
  border-color: var(--gold);
  box-shadow: var(--focus-ring);
}

body.bg-app .col-form-label,
body.bg-app .control-label {
  font-size: var(--font-size-body);
  color: var(--text);
  font-weight: var(--font-weight-medium);
}

body.bg-app .row.mb-3 {
  margin-bottom: var(--space-3);
}

/* ---- drawer field classes (.fld / .inp / .sel) ---- */
:is(body.bg-app, body.client-document) .fld {
  margin-bottom: 15px;
}

:is(body.bg-app, body.client-document) .fld > label {
  display: block;
  font-size: var(--fs-sm);
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 6px;
}

:is(body.bg-app, body.client-document) .fld > label .req {
  color: var(--overdue-fg);
  margin-left: 2px;
}

:is(body.bg-app, body.client-document) .inp {
  width: 100%;
  height: var(--field-h);
  padding: 0 11px;
  background: var(--bg-panel);
  color: var(--text);
  font: inherit;
  transition: border-color var(--duration-fast) var(--ease-standard),
    box-shadow var(--duration-fast) var(--ease-standard);
}

:is(body.bg-app, body.client-document) .inp::placeholder {
  color: var(--text-muted);
}

body.bg-app textarea.inp {
  height: auto;
  padding: 9px 11px;
  resize: vertical;
  min-height: 76px;
  line-height: 1.5;
}

:is(body.bg-app, body.client-document) .fld.invalid .inp {
  border-color: var(--overdue-fg);
}

:is(body.bg-app, body.client-document) .fld.invalid .inp:focus {
  box-shadow: 0 0 0 3px var(--overdue-bg);
}

/* An amount with its currency symbol in front (ported from the mocks' .amt-group). */
body.bg-app .amt-group {
  display: flex;
  align-items: stretch;
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  background: var(--bg-panel);
  overflow: hidden;
  transition: border-color var(--duration-fast) var(--ease-standard),
    box-shadow var(--duration-fast) var(--ease-standard);
}

body.bg-app .amt-group:focus-within {
  border-color: var(--gold);
  box-shadow: 0 0 0 3px var(--gold-soft);
}

body.bg-app .amt-group .cur-sym {
  display: inline-grid;
  place-items: center;
  min-width: 32px;
  padding: 0 9px;
  border-right: 1px solid var(--border);
  background: var(--sand-50);
  color: var(--text-muted);
  font-weight: 600;
}

body.bg-app .amt-group .amt-inp {
  flex: 1;
  min-width: 0;
  height: var(--field-h);
  padding: 0 11px;
  border: 0;
  background: transparent;
  color: var(--text);
  font: inherit;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

body.bg-app .amt-group .amt-inp:focus {
  outline: 0;
}

body.bg-app .fld-row {
  display: flex;
  gap: 12px;
}

body.bg-app .fld-row > .fld {
  flex: 1;
}

/* A field's one-line help under its input. */
:is(body.bg-app, body.client-document) .fld .fld-help {
  margin: 5px 0 0;
  font-size: var(--fs-sm);
  color: var(--text-muted);
}

/* A field's reason it can't be saved, shown while the field is .invalid. */
:is(body.bg-app, body.client-document) .fld-err {
  display: none;
  margin-top: 5px;
  font-size: var(--fs-sm);
  color: var(--overdue-fg);
}

:is(body.bg-app, body.client-document) .fld.invalid .fld-err {
  display: block;
}

/* A checkbox field with its label inline ("Billable", "Rebill to the client"); in a .fld-row it lines up with the
   inputs beside it. */
body.bg-app .fld-check {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  margin-bottom: 15px;
  font-size: var(--fs-ui);
  color: var(--ink);
  cursor: pointer;
}

body.bg-app .fld-row > .fld-check {
  flex: 0 0 auto;
  align-self: flex-end;
  height: var(--field-h);
}

/* Legacy Bootstrap-2 .input-prepend/.add-on markup (e.g. the time import's amount
   fields) has no container styling here, so the add-on and the
   full-width .inp just float unboxed and wrap onto separate lines. Give it
   the same seamless-control treatment as timesheets-import.css /
   settings-shell.css. */
body.bg-app .fld .input-prepend {
  display: inline-flex;
  align-items: stretch;
  height: var(--field-h);
  width: 100%;
  margin: 0;
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  background: var(--bg-panel);
  overflow: hidden;
}

body.bg-app .fld .input-prepend .add-on {
  display: flex;
  align-items: center;
  float: none;
  padding: 0 11px;
  border-right: 1px solid var(--border);
  background: var(--color-bg-main);
  color: var(--text-muted);
}

body.bg-app .fld .input-prepend .inp {
  flex: 1;
  width: auto;
  height: auto;
  border: 0;
  border-radius: 0;
}

body.bg-app .fld .input-prepend .inp:focus {
  box-shadow: none;
}

body.bg-app .fld .input-prepend:focus-within {
  border-color: var(--gold);
  box-shadow: var(--focus-ring);
}

/* Native select with caret (background-image set after the bg shorthand). */
body.bg-app select.sel:not([data-bg-select-bound]):not(.tomselected) {
  width: 100%;
  height: var(--field-h);
  padding: 0 32px 0 11px;
  background: var(--bg-panel);
  color: var(--text);
  font: inherit;
  appearance: none;
  cursor: pointer;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 16 16' fill='none' stroke='%239b9279' stroke-width='1.25'%3E%3Cpath d='M4 6l4 4 4-4'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
}

/* BgSelect (Tom Select): one border on .ts-control, not native .sel + control */
body.bg-app .fld .ts-wrapper,
body.bg-app .field .ts-wrapper {
  display: block;
  width: 100%;
}

/*
 * Hide the native select once Tom Select has bound it. Deliberately not
 * scoped to a .fld/.field ancestor: Tom Select inserts .ts-wrapper as a
 * sibling (afterend), never a descendant, so an ancestor-scoped selector
 * can't reach it, and several bound selects (invoice/estimate client &
 * currency pickers) aren't wrapped in .fld/.field at all. Without this,
 * those selects depend solely on the vendor's ts-hidden-accessible class,
 * so a slow/failed bind would leave both controls' borders visible.
 */
body.bg-app select.sel[data-bg-select-bound],
body.bg-app select[data-bg-select-bound].sel,
body.bg-app select.sel.tomselected {
  position: absolute !important;
  width: 1px !important;
  height: 1px !important;
  padding: 0 !important;
  margin: -1px !important;
  overflow: hidden !important;
  clip: rect(0, 0, 0, 0) !important;
  white-space: nowrap !important;
  border: 0 !important;
  background: none !important;
  box-shadow: none !important;
  appearance: none !important;
}

/* ---- settings / staff card fields (bare input in .field, not only .inp) ---- */
body.bg-app.app-sidebar-shell .inner-content .settings-card .field input:not([type="checkbox"]):not([type="radio"]):not([type="file"]),
body.bg-app.app-sidebar-shell .inner-content .settings-card .field select:not([data-bg-select-bound]):not(.tomselected),
body.bg-app.app-sidebar-shell .inner-content .settings-card .field textarea {
  width: 100%;
  max-width: 100%;
  height: auto;
  margin: 0;
  padding: 8px 10px;
  box-sizing: border-box;
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  background: var(--bg-panel);
  color: var(--text);
  font-family: inherit;
  font-size: var(--fs-body);
  line-height: 1.4;
  box-shadow: none;
  transition: border-color var(--duration-fast) var(--ease-standard),
    box-shadow var(--duration-fast) var(--ease-standard);
}

body.bg-app.app-sidebar-shell .inner-content .settings-card .field textarea {
  min-height: 84px;
  resize: vertical;
}

body.bg-app.app-sidebar-shell .inner-content .settings-card .field input::placeholder,
body.bg-app.app-sidebar-shell .inner-content .settings-card .field textarea::placeholder {
  color: var(--text-muted);
  opacity: 1;
}

body.bg-app.app-sidebar-shell .inner-content .settings-card .field input:not([type="checkbox"]):not([type="radio"]):not([type="file"]):focus,
body.bg-app.app-sidebar-shell .inner-content .settings-card .field select:not([data-bg-select-bound]):not(.tomselected):focus,
body.bg-app.app-sidebar-shell .inner-content .settings-card .field textarea:focus {
  outline: none;
  border-color: var(--gold);
  box-shadow: var(--focus-ring);
}

body.bg-app.app-sidebar-shell .inner-content .settings-card .field select:not([data-bg-select-bound]):not(.tomselected) {
  appearance: none;
  -webkit-appearance: none;
  padding-right: 32px;
  background-color: var(--bg-panel);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 16 16' fill='none' stroke='%239b9279' stroke-width='1.25'%3E%3Cpath d='M4 6l4 4 4-4'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  cursor: pointer;
}

body.bg-app.app-sidebar-shell .inner-content .settings-card .field .ts-wrapper {
  width: 100%;
  max-width: 100%;
}

/* On/off switch: a checkbox with role="switch" (Settings > Invoice Defaults late fees, the editor's Late fee card). */
body.bg-app input.bg-switch {
  appearance: none;
  -webkit-appearance: none;
  position: relative;
  flex: none;
  width: 34px;
  height: 20px;
  margin: 0;
  border: 0;
  border-radius: var(--r-pill);
  background: var(--sand-300);
  cursor: pointer;
  transition: background .15s ease;
}

body.bg-app input.bg-switch::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--bg-panel);
  box-shadow: var(--shadow-raise);
  transition: transform .15s ease;
}

body.bg-app input.bg-switch:checked {
  background: var(--gold);
}

body.bg-app input.bg-switch:checked::after {
  transform: translateX(14px);
}

body.bg-app input.bg-switch:focus-visible {
  outline: none;
  box-shadow: 0 0 0 3px var(--gold-soft);
}

body.bg-app input.bg-switch:disabled {
  cursor: not-allowed;
  opacity: .55;
}

/* Explicit On / Off setting. Native radio pairs retain keyboard and form semantics. */
body.bg-app .bg-onoff {
  display: inline-flex;
  align-items: stretch;
  flex: none;
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  background: var(--sand-50);
  font: inherit;
  font-size: var(--fs-sm);
  font-weight: 600;
  line-height: 1;
}

body.bg-app .bg-onoff > label {
  position: relative;
  display: grid;
  min-width: calc(var(--space-8) + var(--space-4));
  min-height: var(--tap-min);
  margin: 0;
  padding: var(--space-1);
  cursor: pointer;
}

body.bg-app .bg-onoff > label > input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
}

body.bg-app .bg-onoff > label > span {
  display: grid;
  place-items: center;
  border-radius: var(--r-sm);
  color: var(--text-muted);
  pointer-events: none;
  transition: background .12s ease, color .12s ease, box-shadow .12s ease;
}

body.bg-app .bg-onoff > label:has(> input:checked) > span {
  background: var(--bg-panel);
  color: var(--ink);
  box-shadow: var(--shadow-raise);
}

body.bg-app .bg-onoff > label:has(> input[value="true"]:checked) > span {
  color: var(--gold-deep);
}

body.bg-app .bg-onoff > label:has(> input:focus-visible) {
  outline: 2px solid var(--ring);
  outline-offset: 2px;
  border-radius: var(--r-md);
}

body.bg-app .bg-onoff > label:has(> input:hover:not(:disabled):not(:checked)) > span {
  color: var(--ink);
}

body.bg-app .bg-onoff > label:has(> input:disabled) {
  opacity: .55;
  cursor: not-allowed;
}

body.bg-app .bg-onoff > label > input:disabled {
  cursor: not-allowed;
}

@media (prefers-reduced-motion: reduce) {
  body.bg-app .bg-onoff > label > span { transition: none; }
}
