/*
 * Stoei Core — controls: buttons, icon buttons, the light/dark switch, form
 * fields and the messages around them. Shared by websites and apps.
 *
 * Load after tokens.css and base.css, before web.css (websites) or stoei.css
 * (apps). One set of rules for both: the app look comes from settings in the
 * app's theme file (themes/app-starter.css sets the compact app sizes).
 *
 * Every look value comes from tokens.css; this file only reads settings.
 * Behaviour (live field checks, loading and "Saved" buttons, messages beside
 * buttons, the switch) is in js/stoei.js. Examples: test/index.html.
 * Check: php tools/check-css.php
 */

/* ---------- Buttons ----------
   <button class="st-btn st-btn--primary">Save</button>
   Kinds: --primary, --secondary, --ghost, --danger, --pill.
   Sizes: --sm, (normal), --lg.
   States: hover, keyboard focus (base.css ring), disabled, loading
   (aria-busy="true") and done (data-st-state="done"), set by stoei.js. */

.st-btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--st-space-2);
  min-height: var(--st-button-height); padding: 0 var(--st-button-pad);
  border: var(--st-line-width) solid transparent; border-radius: var(--st-radius-control);
  font-family: inherit; font-size: var(--st-button-text); font-weight: var(--st-button-weight);
  line-height: var(--st-leading-tight); white-space: nowrap; text-decoration: none; cursor: pointer;
}
.st-btn:hover { text-decoration: none; }

.st-btn--sm { --st-button-height: var(--st-control-height-sm); --st-button-pad: var(--st-space-4); --st-button-text: var(--st-text-3); }
.st-btn--lg { --st-button-height: var(--st-control-height-lg); --st-button-pad: var(--st-space-7); --st-button-text: var(--st-text-7); }

/* Hover only changes a button that can be used. */
.st-btn--primary { background: var(--st-color-accent-fill); color: var(--st-color-on-accent); }
.st-btn--primary:hover:not(:disabled, [aria-disabled="true"]) { background: var(--st-color-accent-fill-hover); color: var(--st-color-on-accent); }

.st-btn--secondary {
  background: var(--st-button-secondary-bg); color: var(--st-button-secondary-text);
  border-color: var(--st-button-secondary-line);
}
.st-btn--secondary:hover:not(:disabled, [aria-disabled="true"]) {
  background: var(--st-button-secondary-hover-bg); color: var(--st-button-secondary-text);
  border-color: var(--st-color-accent);
}

.st-btn--ghost { background: transparent; color: var(--st-color-text-soft); border-color: var(--st-color-line-strong); }
.st-btn--ghost:hover:not(:disabled, [aria-disabled="true"]) { background: var(--st-color-sunk); color: var(--st-color-text); }

.st-btn--danger { background: transparent; color: var(--st-color-bad); border-color: var(--st-color-bad); }
.st-btn--danger:hover:not(:disabled, [aria-disabled="true"]) { background: var(--st-color-bad-tint); color: var(--st-color-bad-hover); }

.st-btn--pill {
  --st-button-height: var(--st-touch); --st-button-pad: var(--st-space-5);
  border-radius: var(--st-radius-pill); background: var(--st-color-surface); color: var(--st-color-text);
}

/* Buttons and fields that can't be used. */
.st-btn:disabled, .st-btn[aria-disabled="true"],
:is(.st-input, .st-select, .st-textarea, .st-field :where(input, select, textarea)):disabled { opacity: var(--st-opacity-disabled); cursor: not-allowed; }

/* Loading: a turning ring before the words; the button can't be pressed twice.
   Stays at full strength (it is busy, not unavailable). */
.st-btn[aria-busy="true"] { opacity: 1; cursor: progress; }
.st-btn[aria-busy="true"]::before {
  content: ""; inline-size: 1em; block-size: 1em; flex: none;
  border: var(--st-icon-stroke) solid currentColor; border-inline-end-color: transparent;
  border-radius: var(--st-radius-pill);
}
@media (prefers-reduced-motion: no-preference) {
  .st-btn[aria-busy="true"]::before { animation: st-spin var(--st-motion-spin) linear infinite; }
}
@keyframes st-spin { to { transform: rotate(1turn); } }

/* Done: a tick and a short word ("Saved") for a moment, then back to normal. */
.st-btn[data-st-state="done"]::before {
  content: ""; inline-size: 1em; block-size: 1em; flex: none;
  background: currentColor; mask: var(--st-icon-check) center / contain no-repeat;
}

/* ---------- Round icon buttons and the light/dark switch ----------
   Take the text color of where they sit (page, header band, top bar).
   <button type="button" class="st-theme-switch" data-stoei-theme-toggle></button>
   stoei.js adds the moon and sun icons and the spoken label. */

.st-icon-btn, .st-theme-switch {
  display: inline-flex; align-items: center; justify-content: center; flex: none;
  inline-size: var(--st-touch); block-size: var(--st-touch); padding: 0;
  border: var(--st-line-width) solid var(--st-color-icon-line); border-radius: var(--st-radius-pill);
  background: transparent; color: inherit; cursor: pointer;
}
.st-icon-btn:hover, .st-theme-switch:hover { background: var(--st-color-icon-hover); text-decoration: none; }

/* Moon in light mode (press for dark), sun in dark mode. Decided by CSS, so the
   right icon shows before any script runs. */
.st-theme-switch__sun { display: none; }
:root[data-theme="dark"] .st-theme-switch__sun { display: block; }
:root[data-theme="dark"] .st-theme-switch__moon { display: none; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .st-theme-switch__sun { display: block; }
  :root:not([data-theme="light"]) .st-theme-switch__moon { display: none; }
}

/* ---------- Form fields ----------
   <div class="st-field">
     <label for="email">Email</label>
     <input id="email" type="email" required aria-describedby="email-hint email-msg">
     <p class="st-field__hint" id="email-hint">For receipts only.</p>
     <p class="st-field__msg" id="email-msg"></p>
   </div>
   Fields work inside .st-field, or on their own with .st-input, .st-select
   or .st-textarea. A wrong field gets aria-invalid="true"; a right one, when
   it has something to say, gets .is-valid on its .st-field (stoei.js). */

.st-field { display: flex; flex-direction: column; gap: var(--st-space-2); min-width: 0; }
.st-field :where(label), .st-field__label { font-weight: var(--st-weight-strong); font-size: var(--st-text-small); }
.st-field__optional, .st-hours th small { font-weight: 400; color: var(--st-color-text-muted); }
.st-field-row { display: flex; flex-wrap: wrap; gap: var(--st-space-5); }
.st-field-row > .st-field { flex: 1 1 220px; }

.st-input, .st-select, .st-textarea,
.st-field :where(input:not([type="checkbox"], [type="radio"], [type="file"]), select, textarea) {
  inline-size: 100%; min-height: var(--st-field-height); padding: 0 var(--st-field-pad);
  font-family: inherit; font-size: var(--st-text-body); color: var(--st-color-text);
  background: var(--st-color-raised); border: var(--st-line-width) solid var(--st-color-line-strong);
  border-radius: var(--st-radius-control);
}
.st-textarea, .st-field :where(textarea) { padding-block: var(--st-space-3); resize: vertical; min-height: var(--st-textarea-height); }
:is(.st-input, .st-textarea, .st-field :where(input, textarea))::placeholder { color: var(--st-color-text-faint); }

.st-input:hover, .st-select:hover, .st-textarea:hover,
.st-field :where(input, select, textarea):hover { border-color: var(--st-color-line-hover); }
.st-input:focus, .st-select:focus, .st-textarea:focus,
.st-field :where(input, select, textarea):focus { border-color: var(--st-color-accent); }
/* Wrong and right win over hover and focus. */
:is(.st-input, .st-select, .st-textarea, .st-field :where(input, select, textarea))[aria-invalid="true"] { border-color: var(--st-color-bad); }
.st-field.is-valid :where(input, select, textarea) { border-color: var(--st-color-ok); }

/* Text under a field: a hint, then what is wrong (or right). */
.st-field__hint, .st-card__date, .st-form-msg { font-size: var(--st-text-caption); color: var(--st-color-text-muted); }
.st-field__msg { font-size: var(--st-text-caption); font-weight: var(--st-weight-strong); color: var(--st-color-bad); }
.st-field__ok, .st-field.is-valid .st-field__msg { font-size: var(--st-text-caption); color: var(--st-color-ok); }
.st-field__msg:empty { display: none; }

.st-choice { display: flex; align-items: center; gap: var(--st-space-3); min-height: var(--st-control-height-sm); }
.st-choice input { inline-size: 20px; block-size: 20px; accent-color: var(--st-color-accent); }

/* ---------- Buttons with a message beside them ----------
   <div class="st-actions">
     <button class="st-btn st-btn--primary">Save</button>
     <p class="st-form-msg" role="status"></p>
   </div>
   The message sits where the eye already is, instead of a pop-up. */

.st-actions { display: flex; flex-wrap: wrap; align-items: center; gap: var(--st-space-3); }
/* .st-form-msg shares its text style with .st-field__hint (above).
   Only beside buttons does the message share a row and need a width to wrap at.
   Under a field (a column) a flex size would become its height (0.5.0 bug). */
.st-actions > .st-form-msg { flex: 1 1 200px; }
.st-form-msg--bad { color: var(--st-color-bad); }
.st-form-msg--ok { color: var(--st-color-ok); }

/* The list at the top of a form after a send was turned down: what to fix, each a link to its field. */
.st-errors { border: 2px solid var(--st-color-bad); border-radius: var(--st-radius-card); background: var(--st-color-surface); padding: var(--st-space-4) var(--st-space-6); display: flex; flex-direction: column; gap: var(--st-space-2); }
.st-errors:focus { outline: none; }
.st-errors:focus-visible { outline: var(--st-focus-width) solid var(--st-color-bad); outline-offset: var(--st-focus-offset); }
.st-errors__title { font-weight: 700; color: var(--st-color-bad); margin: 0; }
.st-errors ul { margin: 0; padding-left: 1.2em; display: flex; flex-direction: column; gap: var(--st-space-1); }
.st-errors a { color: var(--st-color-bad); font-weight: 500; }

/* ---------- Motion ---------- */

@media (prefers-reduced-motion: no-preference) {
  .st-btn, .st-icon-btn, .st-theme-switch, .st-input, .st-select, .st-textarea,
  .st-field :where(input, select, textarea) {
    transition: color var(--st-motion), background-color var(--st-motion), border-color var(--st-motion);
  }
}
