/* =============================================================================
   @elements/style — form

   Bare HTML form controls (<input>, <textarea>, <select>) look like
   proper Elements fields out of the box — no class needed. Wrap the
   element in `<div class="field">` for label / hint / error structure.

   Sizes follow the universal vocabulary:

     <input>                          — default 44px (matches button)
     <input class="is-sm">               — 32px (dashboard density)
     <input class="is-lg">               — 48px (hero)

   States use the `is-` prefix (runtime toggles):

     <input class="is-error">
     <input class="is-ok">

   Field structure — generic scoped children inside <div class="field">:

     <div class="field">
       <label>Email</label>
       <input type="email">
       <p class="hint">We'll never share it.</p>
     </div>

     <div class="field">
       <div class="head">
         <label>Password</label>
         <a href="/forgot">forgot?</a>
       </div>
       <input type="password">
       <p class="error">Wrong password.</p>
     </div>

   Two-column form layout — apply `.is-grid` to the <form>:

     <form class="is-grid">
       <div class="field">…</div>
       <div class="field">…</div>
       <div class="field is-full">…</div>     <!-- spans both columns -->
     </form>

   Toggle / checkbox / radio / stepper:

     <button class="toggle is-on">…</button>
     <input type="checkbox" class="checkbox">
     <input type="radio" class="radio">
     <div class="stepper">
       <button class="minus">−</button>
       <input class="val" type="number" value="1">
       <button class="plus">+</button>
     </div>
============================================================================= */

/* ─── Bare element defaults ─────────────────────────────────────────
   Mirror Elements field chrome onto bare text-like inputs, textareas,
   and selects. Wrapped in :where() so specificity is zero — any single
   class wins. */

:where(
  input[type="text"],
  input[type="email"],
  input[type="password"],
  input[type="search"],
  input[type="tel"],
  input[type="url"],
  input[type="number"],
  input[type="date"],
  input[type="time"],
  input[type="datetime-local"],
  input[type="month"],
  input[type="week"],
  input:not([type]),
  textarea,
  select
) {
  display: block;
  width: 100%;
  height: var(--input-height);
  padding: var(--input-padding-y) var(--input-padding-x);
  font-family: var(--font-sans);
  font-size: var(--input-font-size);
  line-height: 1.4;
  color: var(--ink);
  background: var(--input-bg);
  border: 1px solid var(--input-border);
  border-radius: var(--input-radius);
  transition:
    border-color var(--duration-fast) var(--ease-out),
    background-color var(--duration-fast) var(--ease-out),
    box-shadow var(--duration-fast) var(--ease-out);
}

:where(
  input[type="text"]::placeholder,
  input[type="email"]::placeholder,
  input[type="password"]::placeholder,
  input[type="search"]::placeholder,
  input[type="tel"]::placeholder,
  input[type="url"]::placeholder,
  input[type="number"]::placeholder,
  input:not([type])::placeholder,
  textarea::placeholder
) {
  color: var(--input-placeholder);
  opacity: 1;
}

:where(
  input[type="text"]:hover:not(:disabled):not(:focus),
  input[type="email"]:hover:not(:disabled):not(:focus),
  input[type="password"]:hover:not(:disabled):not(:focus),
  input[type="search"]:hover:not(:disabled):not(:focus),
  input[type="tel"]:hover:not(:disabled):not(:focus),
  input[type="url"]:hover:not(:disabled):not(:focus),
  input[type="number"]:hover:not(:disabled):not(:focus),
  input:not([type]):hover:not(:disabled):not(:focus),
  textarea:hover:not(:disabled):not(:focus),
  select:hover:not(:disabled):not(:focus)
) {
  border-color: var(--input-border-hover);
  box-shadow: var(--shadow-soft);
}

:where(
  input[type="text"]:focus,
  input[type="email"]:focus,
  input[type="password"]:focus,
  input[type="search"]:focus,
  input[type="tel"]:focus,
  input[type="url"]:focus,
  input[type="number"]:focus,
  input:not([type]):focus,
  textarea:focus,
  select:focus
) {
  outline: none;
  border-color: var(--input-border-focus);
  background: var(--input-bg-focus);
  box-shadow: var(--shadow-soft);
}

:where(
  input[type="text"]:disabled,
  input[type="email"]:disabled,
  input[type="password"]:disabled,
  input[type="search"]:disabled,
  input[type="tel"]:disabled,
  input[type="url"]:disabled,
  input[type="number"]:disabled,
  input:not([type]):disabled,
  textarea:disabled,
  select:disabled
) {
  opacity: 0.5;
  cursor: not-allowed;
  background: var(--bg-soft);
}

/* Textarea: padding-driven height */
:where(textarea) {
  height: auto;
  min-height: 5rem;
  resize: vertical;
  line-height: var(--leading-snug);
  padding-top: var(--space-2);
  padding-bottom: var(--space-2);
}

/* Select: appearance reset + chevron */
:where(select) {
  padding-right: 2rem;
  line-height: var(--input-height);
  font-family: var(--font-sans) !important;
  background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 20 20'%3e%3cpath stroke='%2392939a' stroke-linecap='round' stroke-linejoin='round' stroke-width='1.5' d='M6 8l4 4 4-4'/%3e%3c/svg%3e");
  background-position: right 0.625rem center;
  background-repeat: no-repeat;
  background-size: 1.125em 1.125em;
  appearance: none;
  cursor: pointer;
  padding-top: 0;
  padding-bottom: 0;
}

/* ─── Sizes (single-axis modifier) ─────────────────────────────────
   Class-level rules win over the :where() defaults. */

input.is-sm, textarea.is-sm, select.is-sm {
  height: var(--input-height-sm);
  padding: 0.25rem 0.625rem;
  font-size: var(--text-xs);
}
textarea.is-sm { height: auto; min-height: 4rem; }
select.is-sm { line-height: var(--input-height-sm); padding-top: 0; padding-bottom: 0; }

input.is-lg, textarea.is-lg, select.is-lg {
  height: var(--input-height-lg);
  padding: 0.6875rem 0.875rem;
  font-size: var(--text-md);
}
textarea.is-lg { height: auto; min-height: 6rem; }
select.is-lg { line-height: var(--input-height-lg); padding-top: 0; padding-bottom: 0; }

/* ─── States ───────────────────────────────────────────────────── */

input.is-error, textarea.is-error, select.is-error {
  border-color: var(--red);
}
input.is-error:focus, textarea.is-error:focus, select.is-error:focus {
  border-color: var(--red);
  background: var(--bg);
}

input.is-ok, textarea.is-ok, select.is-ok {
  border-color: var(--green);
}
input.is-ok:focus, textarea.is-ok:focus, select.is-ok:focus {
  border-color: var(--green);
  background: var(--bg);
}

/* ─── Checkbox / radio ────────────────────────────────────────── */

input.checkbox,
input.radio {
  width: 1rem;
  height: 1rem;
  color: var(--ink);
  background: var(--bg);
  border: 1px solid var(--ink-faint);
  cursor: pointer;
  appearance: none;
  flex-shrink: 0;
  padding: 0;
  display: inline-block;
  transition:
    background-color var(--duration-fast) var(--ease-out),
    border-color var(--duration-fast) var(--ease-out);
}
input.checkbox { border-radius: var(--radius-xs); }
input.radio    { border-radius: var(--radius-full); }

input.checkbox:hover,
input.radio:hover { border-color: var(--ink); }

input.checkbox:checked,
input.radio:checked {
  background-color: var(--ink);
  border-color: var(--ink);
}

input.checkbox:checked {
  background-image: url("data:image/svg+xml,%3csvg viewBox='0 0 16 16' fill='white' xmlns='http://www.w3.org/2000/svg'%3e%3cpath d='M12.207 4.793a1 1 0 010 1.414l-5 5a1 1 0 01-1.414 0l-2-2a1 1 0 011.414-1.414L6.5 9.086l4.293-4.293a1 1 0 011.414 0z'/%3e%3c/svg%3e");
  background-size: 100% 100%;
  background-position: center;
  background-repeat: no-repeat;
}
input.radio:checked {
  background-image: url("data:image/svg+xml,%3csvg viewBox='0 0 16 16' fill='white' xmlns='http://www.w3.org/2000/svg'%3e%3ccircle cx='8' cy='8' r='3'/%3e%3c/svg%3e");
  background-size: 100% 100%;
  background-position: center;
  background-repeat: no-repeat;
}

input.checkbox:focus-visible,
input.radio:focus-visible {
  outline: none;
  border-color: var(--ink);
  box-shadow: inset 0 0 0 1px var(--ink);
}

/* ─── Toggle / switch ─────────────────────────────────────────────
   `.toggle` is a coercion class — apply to a <button> for a toggle
   control. Add `.is-on` to flip. Use `.is-sm` for the small variant. */

.toggle {
  width: 2.375rem;
  height: 1.375rem;
  background: var(--ink-faint);
  border-radius: var(--radius-full);
  position: relative;
  cursor: pointer;
  flex-shrink: 0;
  border: none;
  padding: 0;
  transition:
    background-color 200ms var(--ease-out),
    box-shadow 150ms var(--ease-out);
}
.toggle::after {
  content: '';
  position: absolute;
  top: 2px;
  left: 2px;
  width: 1.125rem;
  height: 1.125rem;
  background: #ffffff;
  border-radius: 50%;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.15);
  transition:
    transform 200ms cubic-bezier(0.4, 0, 0.2, 1),
    width 80ms var(--ease-out);
}

.toggle.is-on                { background: var(--accent); }
.toggle.is-on::after         { transform: translateX(1rem); }

.toggle:hover:not(:disabled)       { background: var(--ink-dim); }
.toggle.is-on:hover:not(:disabled) { background: var(--accent-deep); }

.toggle:active:not(:disabled)::after        { width: 1.375rem; }
.toggle.is-on:active:not(:disabled)::after  { transform: translateX(0.75rem); width: 1.375rem; }

.toggle:focus-visible {
  outline: none;
  box-shadow:
    0 0 0 2px var(--bg),
    0 0 0 4px var(--accent-deep);
}

.toggle:disabled {
  background: var(--rule);
  cursor: not-allowed;
  opacity: 0.6;
}
.toggle:disabled::after {
  background: var(--bg-soft);
  box-shadow: none;
}

.toggle.is-sm {
  width: 1.75rem;
  height: 1.125rem;
}
.toggle.is-sm::after {
  width: 0.875rem;
  height: 0.875rem;
}
.toggle.is-sm.is-on::after { left: calc(100% - 0.875rem - 2px); }

/* ─── Stepper ──────────────────────────────────────────────────────
   Quantity increment/decrement.

     <div class="stepper">
       <button class="minus">−</button>
       <span class="val">1</span>          <!-- read-only -->
       <button class="plus">+</button>
     </div>

     <div class="stepper">
       <button class="minus">−</button>
       <input class="val" type="number" value="1">
       <button class="plus">+</button>
     </div> */

.stepper {
  display: inline-flex;
  align-items: center;
  border: 1px solid var(--rule);
  border-radius: var(--radius-md);
  background: var(--bg);
  overflow: hidden;
  font-family: var(--font-sans);
}

:where(.stepper > button) {
  background: var(--bg);
  border: none;
  height: 2rem;
  width: 2rem;
  padding: 0;
  color: var(--ink);
  font-family: var(--font-sans);
  font-size: var(--text-md);
  font-weight: var(--font-bold);
  cursor: pointer;
  border-radius: 0;
  transition: background var(--duration-fast) var(--ease-out);
}
:where(.stepper > button:hover:not(:disabled)) { background: var(--bg-panel); }
:where(.stepper > button:disabled) { opacity: 0.4; cursor: not-allowed; }
:where(.stepper > button:focus-visible) { outline: none; box-shadow: none; }

:where(.stepper > .val) {
  padding: 0 0.875rem;
  min-width: 2.5rem;
  text-align: center;
  font-size: var(--text-sm);
  font-weight: var(--font-medium);
  border-left: 1px solid var(--rule);
  border-right: 1px solid var(--rule);
  line-height: 2rem;
  font-variant-numeric: tabular-nums;
}

/* Typable .val on an <input type="number"> */
:where(.stepper > input.val) {
  height: 2rem;
  width: auto;
  line-height: 2rem;
  font-family: var(--font-sans);
  color: var(--ink);
  background: transparent;
  border-top: none;
  border-bottom: none;
  border-radius: 0;
  outline: none;
  -moz-appearance: textfield;
  appearance: none;
  display: inline-block;
}
:where(.stepper > input.val::-webkit-outer-spin-button),
:where(.stepper > input.val::-webkit-inner-spin-button) {
  -webkit-appearance: none;
  margin: 0;
}
:where(.stepper > input.val:focus) {
  background: transparent;
  box-shadow: none;
}
:where(.stepper > input.val::selection) {
  background: var(--bg-panel);
  color: var(--ink);
}

/* ─── Field structure ──────────────────────────────────────────── */

.field { margin-bottom: var(--space-4); }
.field:last-child { margin-bottom: 0; }
.field.is-full { grid-column: 1 / -1; }

/* A bare <label> inside .field auto-styles. */
:where(.field > label),
:where(.field > .head > label) {
  display: block;
  font-family: var(--font-sans);
  font-size: 0.84375rem;
  font-weight: var(--font-medium);
  color: var(--ink);
  margin-bottom: 0.375rem;
}

/* .field > .head = label + right-aligned slot (forgot?, hint, etc.) */
:where(.field > .head) {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 0.375rem;
}
:where(.field > .head > label) { margin-bottom: 0; }

/* Helper text below a field. */
:where(.field > .hint),
.hint {
  font-family: var(--font-sans);
  font-size: var(--text-2xs);
  color: var(--ink-dim);
  margin-top: 0.375rem;
  line-height: 1.5;
}

/* Error text below a field. */
:where(.field > .error) {
  font-family: var(--font-sans);
  font-size: var(--text-xs);
  color: var(--red);
  margin-top: 0.25rem;
}

/* Two-column grid layout: <form class="is-grid"> */
form.is-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.875rem 0.75rem;
  margin-bottom: 0.625rem;
}
form.is-grid > .field { margin-bottom: 0; }

/* ─── Fieldset / legend ──────────────────────────────────────── */

fieldset {
  padding: var(--space-4);
  border: 1px solid var(--rule);
  border-radius: var(--radius-md);
}
legend {
  padding: 0 var(--space-2);
  font-family: var(--font-sans);
  font-size: var(--text-2xs);
  font-weight: var(--font-bold);
  text-transform: uppercase;
  letter-spacing: var(--tracking-widest);
  color: var(--ink-dim);
}
