/* Canonical UI-002 shared forms.css; see design/SYSTEM.md and FORMS-AND-CARDS.md. */

/* ---------- Form and layout ---------- */
.gs-form {
  display: grid;
  gap: var(--gs-space-6);
  max-width: var(--gs-measure-form);
  margin: 0;
}
/* Directory composition owns layout only; fields keep their shared anatomy. */
.gs-search-controls { display:grid; grid-template-columns:minmax(0,1fr); gap:var(--gs-space-3); margin-block:var(--gs-space-5); }
.gs-search-controls[hidden], .gs-search-controls [hidden] { display:none; }
:is(.p-filters, .gs-filter-chips) { display: flex; flex-wrap: wrap; gap: var(--gs-space-2); }
:is(.p-filters, .gs-filter-chips) button { display: inline-flex; align-items: center; gap: var(--gs-space-2); min-height: var(--gs-control-height); padding-inline: var(--gs-space-4); border: var(--gs-border-width) solid var(--gs-color-border-strong); border-radius: var(--gs-radius-control); background: var(--gs-color-surface); color: var(--gs-color-text); font: var(--gs-weight-medium) var(--gs-text-sm) var(--gs-font-body); cursor: pointer; }
:is(.p-filters, .gs-filter-chips) button span { color: var(--gs-color-text-muted); font-variant-numeric: tabular-nums; }
:is(.p-filters, .gs-filter-chips) [aria-pressed='true'] { border-color: var(--gs-color-action); background: var(--gs-color-action); color: var(--gs-color-on-action); }
:is(.p-filters, .gs-filter-chips) [aria-pressed='true'] span { color: inherit; opacity: .8; }
.gs-search-input { position:relative; }
.gs-search-input svg { position:absolute; inset-inline-start:var(--gs-space-3); top:50%; transform:translateY(-50%); pointer-events:none; }
.gs-search-input .gs-input { padding-inline-start:var(--gs-space-8); }
.gs-search-feedback { grid-column:1/-1; display:flex; align-items:center; gap:var(--gs-space-4); }
.gs-search-feedback .gs-collection-status { margin:0; }
.gs-search-feedback button { min-height:var(--gs-control-height); }
.gs-form-intro {
  margin: 0;
  font-size: var(--gs-text-md);
  line-height: var(--gs-leading-body);
  color: var(--gs-color-text-muted);
}

/* ---------- Field: label, optional help, error, control ---------- */
.gs-field,
.gs-fieldset {
  display: grid;
  gap: var(--gs-space-2);
  min-width: 0;
  margin: 0;
  padding: 0;
  border: 0;
}
.gs-field--short { max-width: var(--gs-field-width-short); }
.gs-field--medium { max-width: var(--gs-field-width-medium); }

.gs-field__label {
  display: block;
  padding: 0;
  color: var(--gs-color-text);
  font: var(--gs-weight-medium) var(--gs-text-md)/var(--gs-leading-snug) var(--gs-font-body);
  overflow-wrap: anywhere;
}
/* "(optional)" is part of the label text, so it is read with it. */
.gs-field__marker {
  font-weight: var(--gs-weight-regular);
  color: var(--gs-color-text-muted);
}
.gs-field__help {
  margin: 0;
  font-size: var(--gs-text-md);
  line-height: var(--gs-leading-body);
  color: var(--gs-color-text-muted);
  overflow-wrap: anywhere;
}
/* Always rendered and referenced by aria-describedby; empty means no error and no description. */
.gs-field__error {
  margin: 0;
  padding-left: var(--gs-space-3);
  border-left: var(--gs-border-width-strong) solid var(--gs-color-error);
  color: var(--gs-color-error);
  font: var(--gs-weight-medium) var(--gs-text-md)/var(--gs-leading-body) var(--gs-font-body);
  overflow-wrap: anywhere;
}
.gs-field__error:empty { display: none; }

/* ---------- Text-like controls: input, select, textarea ---------- */
.gs-input {
  box-sizing: border-box;
  display: block;
  width: 100%;
  min-width: 0;
  min-height: var(--gs-control-height);
  margin: 0;
  padding: var(--gs-space-2) var(--gs-space-3);
  border: var(--gs-border-width) solid var(--gs-color-border-strong);
  border-radius: var(--gs-radius-md);
  background: var(--gs-color-field-bg);
  color: var(--gs-color-text);
  font: var(--gs-weight-regular) var(--gs-text-md)/var(--gs-leading-snug) var(--gs-font-body);
  transition: border-color var(--gs-duration-fast);
}
textarea.gs-input {
  min-height: var(--gs-textarea-min-height);
  line-height: var(--gs-leading-body);
  resize: vertical;
}
/* Native select keeps its platform arrow and keyboard behaviour. */
select.gs-input { cursor: pointer; }
.gs-input::placeholder { color: var(--gs-color-text-muted); opacity: 1; }
/* Hover never applies to an invalid control, so the error border survives hover on every surface. */
.gs-input:hover:not(:disabled, [readonly], [aria-invalid='true']) { border-color: var(--gs-color-action); }
/* Text controls show focus on pointer focus too (the caret alone is easy to lose);
   choices use :focus-visible like buttons. */
.gs-input:focus,
.gs-choice__input:focus-visible {
  outline: var(--gs-focus-width) solid var(--gs-color-focus);
  outline-offset: var(--gs-border-width);
}

/* Invalid only after the shared script sets aria-invalid; :invalid alone would show errors before interaction. */
.gs-input[aria-invalid='true'] {
  border-width: var(--gs-border-width-strong);
  border-color: var(--gs-color-error);
  /* Keep the text position stable when the border thickens. */
  padding: calc(var(--gs-space-2) - var(--gs-border-width)) calc(var(--gs-space-3) - var(--gs-border-width));
}

/* Read only: selectable and focusable; surface and help text say it cannot be edited. */
.gs-input[readonly] {
  background: var(--gs-color-soft);
  cursor: text;
}

/* Native disabled: same explicit disabled roles as .gs-button, never opacity. */
.gs-input:disabled {
  border-style: dashed;
  border-color: var(--gs-color-disabled-border);
  background: var(--gs-color-disabled-bg);
  color: var(--gs-color-disabled-text);
  cursor: not-allowed;
  opacity: 1;
  -webkit-text-fill-color: var(--gs-color-disabled-text);
}

/* ---------- Choice groups: checkbox and radio ---------- */
.gs-choices {
  display: grid;
  gap: 0;
}
.gs-choices--inline {
  display: flex;
  flex-wrap: wrap;
  column-gap: var(--gs-space-6);
}
/* The label is the whole target: at least 44px high, text wraps beside the box. */
.gs-choice {
  display: grid;
  grid-template-columns: var(--gs-choice-size) minmax(0, 1fr);
  align-items: start;
  column-gap: var(--gs-space-3);
  min-height: var(--gs-control-height);
  padding-block: var(--gs-space-2);
  box-sizing: border-box;
  color: var(--gs-color-text);
  font: var(--gs-weight-regular) var(--gs-text-md)/var(--gs-leading-snug) var(--gs-font-body);
  cursor: pointer;
}
.gs-choice__input {
  width: var(--gs-choice-size);
  height: var(--gs-choice-size);
  margin: 0;
  accent-color: var(--gs-color-action);
  cursor: pointer;
}
.gs-choice__text { overflow-wrap: anywhere; }
.gs-choice__hint {
  display: block;
  margin-top: var(--gs-space-1);
  color: var(--gs-color-text-muted);
}
.gs-choice__input[aria-invalid='true'] {
  outline: var(--gs-border-width-strong) solid var(--gs-color-error);
  outline-offset: var(--gs-border-width-strong);
}
.gs-choice__input[aria-invalid='true']:focus-visible {
  outline: var(--gs-focus-width) solid var(--gs-color-focus);
}
.gs-choice:has(.gs-choice__input:disabled) {
  color: var(--gs-color-disabled-text);
  cursor: not-allowed;
}
.gs-choice__input:disabled { cursor: not-allowed; }

/* ---------- Error summary: shown on submit, links to each invalid field ---------- */
.gs-error-summary {
  display: grid;
  gap: var(--gs-space-2);
  padding: var(--gs-space-4);
  border: var(--gs-border-width-strong) solid var(--gs-color-error);
  border-radius: var(--gs-radius-md);
  background: var(--gs-color-error-bg);
  color: var(--gs-color-text);
}
.gs-error-summary[hidden] { display: none; }
.gs-error-summary:focus-visible,
.gs-error-summary:focus {
  outline: var(--gs-focus-width) solid var(--gs-color-focus);
  outline-offset: var(--gs-focus-offset);
}
.gs-error-summary__title {
  margin: 0;
  font: var(--gs-weight-bold) var(--gs-text-lg)/var(--gs-leading-snug) var(--gs-font-heading);
  letter-spacing: var(--gs-tracking-heading);
  color: var(--gs-color-text);
}
.gs-error-summary__list {
  margin: 0;
  padding-left: var(--gs-space-5);
  display: grid;
  gap: var(--gs-space-1);
}
.gs-error-summary__list a {
  color: var(--gs-color-error);
  font-weight: var(--gs-weight-medium);
  text-decoration: underline;
  text-decoration-thickness: var(--gs-border-width);
  text-underline-offset: .2em;
  overflow-wrap: anywhere;
}
.gs-error-summary__list a:hover { text-decoration-thickness: var(--gs-text-decoration-strong); }
.gs-error-summary__list a:focus-visible {
  outline: var(--gs-focus-width) solid var(--gs-color-focus);
  outline-offset: var(--gs-border-width);
}

/* ---------- Form status: one polite region for busy, done and failed ---------- */
/* Neutral notice styling for every outcome: a finished form is not a health result,
   so there is deliberately no green "success" treatment. */
.gs-form-status {
  margin: 0;
  font-size: var(--gs-text-md);
  line-height: var(--gs-leading-body);
  color: var(--gs-color-text);
}
/* Empty: keep the live region in the accessibility tree (display:none would remove it and
   can swallow the first announcement); clip it so it takes no space. */
.gs-form-status:empty {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
}
.gs-form-status[data-state] {
  padding: var(--gs-space-3) var(--gs-space-4);
  border-left: var(--gs-current-rail) solid var(--gs-color-notice-rule);
  background: var(--gs-color-notice-bg);
}
.gs-form-status[data-state='failed'] {
  border-left-color: var(--gs-color-error);
  background: var(--gs-color-error-bg);
}
.gs-form-status:focus { outline: none; }
.gs-form-status:focus-visible {
  outline: var(--gs-focus-width) solid var(--gs-color-focus);
  outline-offset: var(--gs-border-width);
}
.gs-form-status strong { font-weight: var(--gs-weight-medium); }

/* Shared visually-hidden utility (error prefixes, honeypot labels). The PDF
   request template's spam trap (.form-trap) uses the same treatment. */
.gs-visually-hidden,
.gs-form .form-trap {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

@media (prefers-reduced-motion: reduce) {
  .gs-input { transition: none; }
}

/* Shared tool answers (UI-004/R2). Markup and behavior: article_tools.py / article-tools.js. */
/* ---------- Tool frame ---------- */
/* One on-page tool. Answers stay in this region; it never contains a <form>. */
.gs-tool {
  /* UI-004-R2 narrow inset, shared by the frame and the day body below.
     Normal text: the space-5 token (16px at a 320px viewport, as before).
     Enlarged text on a narrow screen: shrinks by one text-size step (10vw minus
     space-4), never below space-1, so nested frames stop eating the width a native
     date needs. Measured: 32px "mm/dd/yyyy" + picker needs ~232px of content. */
  display: grid;
  /* One track that may shrink below its children's min-content; without this the
     implicit auto column grew to 264-294px inside a 241px frame at 320px/200%. */
  grid-template-columns: minmax(0, 1fr);
  gap: var(--gs-space-6);
  min-width: 0;
  padding: var(--gs-space-6) var(--gs-tool-inset);
  /* Long words in tool text ("recommendation." at 200%) wrap instead of setting the
     column's minimum width; same treatment canonical headings and buttons use. */
  overflow-wrap: anywhere;
  /* Query container for the narrow-day rule below; em in the query follows text size. */
  container: gs-tool / inline-size;
  font-family: var(--gs-font-body);
  font-size: var(--gs-text-md);
  border: var(--gs-border-width) solid var(--gs-color-border);
  border-radius: var(--gs-radius-panel);
  background: var(--gs-color-surface);
}
.gs-tool > * { margin: 0; }
.gs-tool__head { display: grid; grid-template-columns: minmax(0, 1fr); gap: var(--gs-space-3); }
.gs-tool__head > * { margin: 0; }
.gs-tool__intro {
  max-width: var(--gs-measure-lead);
  font-size: var(--gs-text-lg);
  line-height: var(--gs-leading-body);
  color: var(--gs-color-text-muted);
}
/* Separate-request variant: the PDF gate. A different surface and border say
   "this sends something" before the copy does; it never wraps tool answers. */
.gs-tool--request {
  background: var(--gs-color-soft);
  border-color: var(--gs-color-border-strong);
}
@media (min-width: 60em) {
  .gs-tool { padding: var(--gs-space-10); }
}

/* ---------- Fieldset legend ---------- */
/* <legend class="gs-field__label"> reuses the label style; legends are not grid
   items, so the fieldset's gap does not reach them. */
.gs-fieldset > legend {
  float: none;
  margin: 0 0 var(--gs-space-2);
  padding: 0;
}

/* ---------- Checklist rows ---------- */
/* Divided rows for a list of independent observations (quick check). The whole
   row is the label and target; selection is shown by the native tick first and
   the row tint second, never by colour alone. */
.gs-choices--rows { border-top: var(--gs-border-width) solid var(--gs-color-border); }
.gs-choices--rows > .gs-choice {
  padding: var(--gs-space-4) var(--gs-space-3);
  border-bottom: var(--gs-border-width) solid var(--gs-color-border);
}
.gs-choices--rows > .gs-choice:has(.gs-choice__input:checked) { background: var(--gs-color-secondary-hover); }
.gs-choices--rows > .gs-choice:has(.gs-choice__input:focus-visible) {
  outline: var(--gs-focus-width) solid var(--gs-color-focus);
  outline-offset: calc(-1 * var(--gs-focus-width));
}
/* The row carries the ring, so the box does not draw a second one. */
.gs-choices--rows .gs-choice__input:focus-visible { outline: none; }

/* ---------- Field grid ---------- */
/* Responsive without breakpoints: columns appear as width allows and collapse
   again under enlarged text, because the minimum is in rem. */
.gs-field-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, var(--gs-field-width-short)), 1fr));
  gap: var(--gs-space-5) var(--gs-space-4);
}
/* Keep each control directly under its label and controls aligned along a row. */
.gs-field-grid > .gs-field { align-content: end; }
.gs-field-grid > .gs-field--wide { grid-column: 1 / -1; }

/* ---------- Native date field (UI-004-R1) ---------- */
/* Shared variant for every <input type="date">: one rule, no page overrides, no
   second controller, no smaller text. At 200% text the 32px date string plus the
   generous browser-default chrome (control padding, 24px indicator margin, 1-2px
   padding around each segment) was wider than a phone column, so the year sat under
   the picker icon. Only that chrome is tightened; the font size, the value, the
   blank state, the segments and keyboard entry (arrows, digits, Backspace, Space/
   Alt+Down for the picker) stay the platform's. Padding uses the same tokens in rem
   so it still scales with text. */
.gs-input[type='date'] { padding-inline: var(--gs-space-2); }
/* Same compensation the base rule makes when the border thickens. */
.gs-input[type='date'][aria-invalid='true'] {
  padding-inline: calc(var(--gs-space-2) - var(--gs-border-width));
}
/* Chromium/WebKit: remove the default breathing room inside the control. */
.gs-input[type='date']::-webkit-datetime-edit,
.gs-input[type='date']::-webkit-datetime-edit-fields-wrapper { padding: 0; }
.gs-input[type='date']::-webkit-datetime-edit-month-field,
.gs-input[type='date']::-webkit-datetime-edit-day-field,
.gs-input[type='date']::-webkit-datetime-edit-year-field { padding-inline: 0; }
/* The picker icon keeps its native size and 44px-class hit area via the control's
   own min-height; only its leading 24px margin goes. A small gap token separates it
   from the year. */
.gs-input[type='date']::-webkit-calendar-picker-indicator {
  margin: 0;
  margin-inline-start: var(--gs-space-1);
  padding: 0;
}

/* ---------- Unanswered select ---------- */
/* The empty first option ("Choose") reads as unanswered, distinct from an answer
   such as "No". Text stays at muted-text contrast; options keep body text. */
select.gs-input:has(> option[value='']:checked) { color: var(--gs-color-text-muted); }
select.gs-input option { color: var(--gs-color-text); }

/* ---------- Disclosure (one log day) ---------- */
/* Native <details>/<summary>: keyboard, expanded state and no-script behaviour
   come from the platform. */
.gs-disclosure-list { display: grid; grid-template-columns: minmax(0, 1fr); gap: var(--gs-space-3); }
.gs-disclosure {
  min-width: 0;
  border: var(--gs-border-width) solid var(--gs-color-border);
  border-radius: var(--gs-radius-md);
  background: var(--gs-color-surface);
}
.gs-disclosure > summary {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--gs-space-1) var(--gs-space-3);
  min-height: var(--gs-control-height);
  padding: var(--gs-space-3) var(--gs-space-4);
  border-radius: var(--gs-radius-md);
  color: var(--gs-color-text);
  font: var(--gs-weight-medium) var(--gs-text-lg)/var(--gs-leading-snug) var(--gs-font-heading);
  letter-spacing: var(--gs-tracking-heading);
  list-style: none;
  cursor: pointer;
}
.gs-disclosure > summary::-webkit-details-marker { display: none; }
/* Drawn chevron in currentColor: the shape, not a hue, shows open/closed. */
.gs-disclosure > summary::before {
  content: '';
  flex: none;
  width: .5em;
  height: .5em;
  margin-inline: var(--gs-space-1);
  border: solid currentColor;
  border-width: 0 var(--gs-border-width-strong) var(--gs-border-width-strong) 0;
  transform: rotate(-45deg);
  transition: transform var(--gs-duration-fast);
}
.gs-disclosure[open] > summary::before { transform: rotate(45deg); }
.gs-disclosure > summary:hover { background: var(--gs-color-secondary-hover); }
.gs-disclosure > summary:focus-visible {
  outline: var(--gs-focus-width) solid var(--gs-color-focus);
  outline-offset: var(--gs-border-width);
}
.gs-disclosure[open] > summary {
  border-bottom: var(--gs-border-width) solid var(--gs-color-border);
  border-radius: var(--gs-radius-md) var(--gs-radius-md) 0 0;
}
/* Neutral count of filled fields, written by the tool's existing script. Empty
   without script, so the summary then reads just "Day 1". */
.gs-disclosure__meta {
  margin-inline-start: auto;
  color: var(--gs-color-text-muted);
  font: var(--gs-weight-regular) var(--gs-text-md)/var(--gs-leading-snug) var(--gs-font-body);
  letter-spacing: normal;
}
.gs-disclosure__meta:empty { display: none; }
.gs-disclosure__body { padding: var(--gs-space-5) min(var(--gs-space-4), var(--gs-tool-inset)) var(--gs-space-6); }
/* UI-004-R2 narrow day: when enlarged text leaves the tool under ~9.5em wide (only
   ~200% text on a 320px-class screen; never at normal text >= 160px), the second
   nested frame goes: days become divided rows like the checklist rows, so a native
   date gets the tool's full width. Measured: empty 32px "mm/dd/yyyy" + picker is
   fully visible from ~236px content (268px box); with both frames it got 219px. */
@container gs-tool (max-width: 9.5em) {
  .gs-disclosure,
  .gs-disclosure > summary,
  .gs-disclosure[open] > summary { border-inline: 0; border-radius: 0; }
  .gs-disclosure > summary,
  .gs-disclosure__body { padding-inline: 0; }
}

/* ---------- Notice panel ---------- */
/* .gs-notice holding a heading, text and an action (quick-check topics). The
   base .gs-notice stays a single paragraph so inline <strong> is not split. */
.gs-notice--panel { display: grid; gap: var(--gs-space-3); justify-items: start; }
.gs-notice--panel > * { margin: 0; }

/* ---------- Print ---------- */
@media print {
  .gs-tool {
    padding: 0;
    border: 0;
    background: var(--gs-print-canvas);
  }
  .gs-tool--request,
  .gs-tool .gs-button { display: none; }
  .gs-disclosure {
    break-inside: avoid;
    border-color: var(--gs-print-border);
    background: var(--gs-print-canvas);
  }
  .gs-disclosure > summary::before { content: none; }
  .gs-field-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .gs-field-grid > .gs-field:has(input[type="date"]) { grid-column: 1 / -1; }
  .gs-tool .gs-input { appearance: none; border-color: var(--gs-print-border); background: var(--gs-print-canvas); color: var(--gs-print-text); }
}

@media (prefers-reduced-motion: reduce) {
  .gs-disclosure > summary::before { transition: none; }
}

/* Tool-bearing reading columns share adaptive gutters; other page families keep theirs. */
.gs-tool-page, .wrap:has(.gs-tool) {
  box-sizing: border-box;
  width: min(var(--gs-content-width), calc(100% - 2 * var(--gs-tool-gutter)));
  margin-inline: auto;
}

.gs-tool-page { display: grid; gap: var(--gs-space-8); max-width: var(--gs-width-narrow); }
