/*
 * Sorivelo form controls (handoff sections 4, 7 and 11; design/Foundations,
 * AddDeal, Settings and States).
 *
 * Field: a label, the control, then a hint or an error, stacked 8px apart.
 *   <div class="sv-field">
 *     <label class="sv-label" for="deal-name">Deal name
 *       <span class="sv-required" aria-hidden="true">*</span></label>
 *     <input class="sv-input" id="deal-name" aria-required="true">
 *     <p class="sv-field-hint" id="deal-name-hint">Shown on the pipeline</p>
 *   </div>
 *   The asterisk is decoration; aria-required (or required) on the control
 *   is what screen readers hear. Errors: svSetFieldError(input, "Give the
 *   deal a name so your team can find it.") from forms.js adds a
 *   <p class="sv-field-error"> linked by aria-describedby and marks the
 *   control aria-invalid; a template can write the same markup itself.
 *
 * Input: sv-input on an <input>, <select> or <textarea>, or on a
 *   <div class="sv-input"> around one when it needs a prefix, an icon or a
 *   second control (amount plus currency). 40px; sv-input-sm 38,
 *   sv-input-lg 44 (sign in), sv-input-xl 48 (first run). Focus: violet
 *   border plus a 3px --focus-halo. sv-input-multiline for a textarea.
 *   sv-input-affix is a "$" or an icon inside the box.
 *   A <select> looks closest to the designs inside a div.sv-input (its
 *   arrow sits 12px in, as in New deal); sv-input on the select itself works.
 *   AI-filled: sv-input-ai on the box and <span class="sv-ai-pill">AI</span>
 *   in the label, until a person edits it (svMarkAIFilled in forms.js does
 *   both and takes them off on the first edit).
 *   Invalid: aria-invalid="true" on the control (or sv-input-invalid on
 *   the box) turns the border red. Disabled controls get the canvas fill.
 *
 * Error summary (States "Form mistakes"): the banner at the top of a form
 *   after Save, linking to each problem (svErrorSummary in forms.js).
 *   <div class="sv-form-errors" role="alert">{{icon "alert-circle"}}
 *     <p class="sv-form-errors-text">2 things need fixing:
 *       <a href="#deal-name">Deal name</a> and <a href="#close">Expected close</a>.</p></div>
 *
 * Checkbox: a real checkbox, invisible, over a drawn box.
 *   <label class="sv-check"><input type="checkbox" class="sv-check-input">
 *     <span class="sv-check-box" aria-hidden="true">{{icon "check" "sv-check-mark"}}{{icon "minus" "sv-check-dash"}}</span>
 *     Create another</label>
 *   18px, 5px radius; sv-check-lg 20px, 6px radius (table rows). Without
 *   visible text, give the input an aria-label ("Select Alder Studio").
 *   Indeterminate (input.indeterminate = true) shows the dash. Disabled is
 *   faded, and dashed when not checked.
 *
 * Switch: a real checkbox with role="switch", over a drawn 40x24 track.
 *   <label class="sv-switch"><input type="checkbox" role="switch"
 *     class="sv-switch-input"><span class="sv-switch-track"
 *     aria-hidden="true"></span>Daily brief</label>
 *   sv-switch-row lays out a settings row: text and hint left, switch right.
 *
 * Segmented control: a --surface-muted track with a white raised segment.
 *   Choosing a value: <div class="sv-seg" role="radiogroup" aria-label=...>
 *     <label class="sv-seg-option"><input type="radio" class="sv-seg-input"
 *     name="vis" value="everyone" checked><span class="sv-seg-item">Everyone
 *     </span></label>...</div> (arrow keys come with the radios).
 *   Switching views: links or buttons with class sv-seg-item straight in
 *   the track, the current one aria-current="page" or aria-pressed="true".
 *   30px segments; sv-seg-sm 28px (page header List / Pipeline).
 *
 * Every rule sets its own font, margins and colors so the old Bootstrap
 * pages (which style label, input, select, textarea and a) cannot change
 * how these look.
 */

/* Field */
.sv-field {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  min-width: 0;
  margin: 0;
  padding: 0;
  border: 0;
}

.sv-label {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  margin: 0;
  padding: 0;
  color: var(--ink-2);
  font-family: var(--font-sans);
  font-size: 13px;
  font-weight: 500;
  line-height: 1.35;
}

.sv-required {
  color: var(--risk);
  font-weight: 500;
}

.sv-ai-pill {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  box-sizing: border-box;
  height: 18px;
  padding: 0 6px;
  border-radius: var(--radius-pill);
  background: var(--violet-tint);
  color: var(--violet-text);
  font-family: var(--font-sans);
  font-size: 11px;
  font-weight: 500;
  line-height: 1;
  letter-spacing: normal;
  text-transform: none;
  white-space: nowrap;
}

.sv-field-hint {
  margin: 0;
  color: var(--ink-caption);
  font-family: var(--font-sans);
  font-size: 12px;
  font-weight: 400;
  line-height: 1.4;
}

.sv-field-error {
  margin: 0;
  color: var(--risk);
  font-family: var(--font-sans);
  font-size: 12.5px;
  font-weight: 400;
  line-height: 1.4;
}

.sv-field-error[hidden] {
  display: none;
}

/* Error summary: risk tint, 8px radius, 13px deep red text and links */
.sv-form-errors {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 10px var(--space-3);
  border-radius: var(--radius-control);
  background: var(--risk-tint);
  color: var(--risk-deep);
  font-family: var(--font-sans);
  font-size: 13px;
  line-height: 1.45;
  /* brought into view on Save: keep a little room above it */
  scroll-margin-block: var(--space-3);
}

.sv-form-errors[hidden] {
  display: none;
}

.sv-form-errors > .sv-icon {
  flex: none;
  margin-top: 2px;
}

.sv-form-errors-text {
  margin: 0;
  min-width: 0;
  text-wrap: pretty;
}

.sv-form-errors-text a {
  color: var(--risk-deep);
  text-decoration: underline;
}

.sv-form-errors-text a:hover {
  color: var(--risk-deep);
  text-decoration-thickness: 2px;
}

/* Input */
.sv-input {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  box-sizing: border-box;
  width: 100%;
  min-width: 0;
  min-height: var(--input-md);
  margin: 0;
  padding: 0 var(--space-3);
  border: 1px solid var(--border);
  border-radius: var(--radius-control);
  background: var(--surface);
  box-shadow: none;
  color: var(--ink);
  font-family: var(--font-sans);
  font-size: 14px;
  font-weight: 400;
  line-height: 1.45;
  text-align: left;
  -webkit-font-smoothing: antialiased;
  transition: border-color var(--motion-fast), box-shadow var(--motion-fast);
}

/* A control inside an input box takes the box's look */
.sv-input > input,
.sv-input > select,
.sv-input > textarea {
  flex: 1 1 auto;
  width: 100%;
  min-width: 0;
  height: auto;
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: 0;
  outline: 0;
  background: transparent;
  box-shadow: none;
  color: inherit;
  font: inherit;
}

/* A select after the input, such as a currency, sizes to its content */
.sv-input > input ~ select {
  flex: none;
  width: auto;
  color: var(--ink-3);
  font-size: 12.5px;
}

.sv-input::placeholder,
.sv-input > input::placeholder,
.sv-input > textarea::placeholder {
  color: var(--ink-caption);
  opacity: 1;
}

.sv-input-affix {
  display: inline-flex;
  flex: none;
  align-items: center;
  color: var(--ink-caption);
}

.sv-input-sm {
  min-height: var(--input-sm);
  padding: 0 10px;
}

.sv-input-lg {
  min-height: var(--input-lg);
  padding: 0 14px;
  border-radius: var(--radius-panel);
  font-size: 15px;
}

.sv-input-xl {
  min-height: var(--input-xl);
  padding: 0 14px;
  border-radius: var(--radius-panel);
  font-size: 15px;
}

.sv-input-multiline {
  align-items: flex-start;
  padding-top: 10px;
  padding-bottom: 10px;
  line-height: 1.5;
  resize: vertical;
}

.sv-input-multiline > textarea {
  resize: vertical;
}

/* AI-filled, until a person edits it */
.sv-input-ai {
  border-color: var(--violet-border);
  background: var(--ai-field-bg);
}

/* Invalid */
.sv-input-invalid,
.sv-input[aria-invalid="true"],
.sv-input:has(> [aria-invalid="true"]) {
  border-color: var(--risk-solid);
}

/* Disabled */
.sv-input:disabled,
.sv-input-disabled,
.sv-input:has(> :disabled) {
  background: var(--canvas);
  color: var(--ink-caption);
  cursor: not-allowed;
}

/* Focus: the border and halo are the focus ring, so no outline */
.sv-input:focus-visible {
  outline: none;
}

.sv-input:focus-within {
  border-color: var(--violet);
  box-shadow: 0 0 0 3px var(--focus-halo);
}

.sv-input-invalid:focus-within,
.sv-input[aria-invalid="true"]:focus-within,
.sv-input:has(> [aria-invalid="true"]):focus-within {
  border-color: var(--risk-solid);
  box-shadow: 0 0 0 3px var(--risk-tint);
}

/* Checkbox */
.sv-check {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  margin: 0;
  padding: 0;
  color: var(--ink-2);
  font-family: var(--font-sans);
  font-size: 13px;
  font-weight: 400;
  line-height: 1.35;
  vertical-align: middle;
  cursor: pointer;
}

.sv-check-input {
  position: absolute;
  top: 50%;
  left: 0;
  z-index: 1;
  width: 18px;
  height: 18px;
  margin: 0;
  padding: 0;
  opacity: 0;
  transform: translateY(-50%);
  cursor: inherit;
}

.sv-check-box {
  display: inline-flex;
  flex: none;
  align-items: center;
  justify-content: center;
  box-sizing: border-box;
  width: 18px;
  height: 18px;
  border: 1.5px solid var(--border-strong);
  border-radius: var(--radius-kbd);
  background: var(--surface);
  color: var(--surface);
  transition: background-color var(--motion-fast), border-color var(--motion-fast);
}

.sv-check-box .sv-icon {
  display: none;
  width: 12px;
  height: 12px;
  stroke-width: 3;
}

.sv-check-lg .sv-check-input,
.sv-check-lg .sv-check-box {
  width: 20px;
  height: 20px;
}

.sv-check-lg .sv-check-box {
  border-radius: var(--radius-tile-sm);
}

.sv-check:hover > .sv-check-input:enabled:not(:checked):not(:indeterminate) + .sv-check-box {
  border-color: var(--ink-faint);
}

.sv-check-input:checked + .sv-check-box,
.sv-check-input:indeterminate + .sv-check-box {
  border-color: var(--violet);
  background: var(--violet);
}

.sv-check-input:checked:not(:indeterminate) + .sv-check-box .sv-check-mark,
.sv-check-input:indeterminate + .sv-check-box .sv-check-dash {
  display: inline-block;
}

.sv-check-input:focus-visible + .sv-check-box {
  outline: var(--focus-width) solid var(--focus);
  outline-offset: var(--focus-offset);
}

.sv-check-input:disabled + .sv-check-box {
  opacity: .5;
}

.sv-check-input:disabled:not(:checked):not(:indeterminate) + .sv-check-box {
  border-style: dashed;
  background: var(--check-disabled-bg);
}

.sv-check:has(> .sv-check-input:disabled) {
  color: var(--ink-caption);
  cursor: not-allowed;
}

/* Switch */
.sv-switch {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 10px;
  margin: 0;
  padding: 0;
  color: var(--ink-2);
  font-family: var(--font-sans);
  font-size: 13px;
  font-weight: 400;
  line-height: 1.35;
  vertical-align: middle;
  cursor: pointer;
}

.sv-switch-input {
  position: absolute;
  top: 50%;
  left: 0;
  z-index: 1;
  width: 40px;
  height: 24px;
  margin: 0;
  padding: 0;
  opacity: 0;
  transform: translateY(-50%);
  cursor: inherit;
}

.sv-switch-track {
  position: relative;
  display: inline-block;
  flex: none;
  box-sizing: border-box;
  width: 40px;
  height: 24px;
  border-radius: var(--radius-pill);
  background: var(--switch-off);
  transition: background-color var(--motion-fast);
}

.sv-switch-track::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 18px;
  height: 18px;
  border-radius: var(--radius-pill);
  background: var(--surface);
  box-shadow: var(--shadow-knob);
  transition: transform var(--motion-fast);
}

.sv-switch-input:checked + .sv-switch-track {
  background: var(--violet);
}

.sv-switch-input:checked + .sv-switch-track::after {
  transform: translateX(16px);
}

.sv-switch-input:focus-visible + .sv-switch-track {
  outline: var(--focus-width) solid var(--focus);
  outline-offset: var(--focus-offset);
}

.sv-switch-input:disabled + .sv-switch-track {
  opacity: .55;
}

.sv-switch:has(> .sv-switch-input:disabled) {
  cursor: not-allowed;
}

/* A settings row: what it does on the left, the switch on the right */
.sv-switch-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: var(--space-4);
  margin: 0;
}

.sv-switch-row-text {
  display: flex;
  flex-direction: column;
  min-width: 0;
  line-height: 1.35;
}

.sv-switch-row-label {
  margin: 0;
  color: var(--ink);
  font-family: var(--font-sans);
  font-size: 14px;
  font-weight: 500;
  line-height: 1.35;
}

.sv-switch-row-text > .sv-field-hint {
  font-size: 12.5px;
  line-height: 1.35;
}

/* Segmented control */
.sv-seg {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  box-sizing: border-box;
  max-width: 100%;
  margin: 0;
  padding: 3px;
  border: 0;
  border-radius: var(--radius-seg);
  background: var(--surface-muted);
  vertical-align: middle;
}

.sv-seg-option {
  position: relative;
  display: inline-flex;
  margin: 0;
  padding: 0;
}

.sv-seg-input {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 1;
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  opacity: 0;
  cursor: pointer;
}

.sv-seg-item {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  box-sizing: border-box;
  height: var(--control-sm);
  margin: 0;
  padding: 0 10px;
  border: 0;
  border-radius: var(--radius-tile);
  background: transparent;
  box-shadow: none;
  color: var(--ink-3);
  font-family: var(--font-sans);
  font-size: 13px;
  font-weight: 500;
  line-height: 1;
  text-decoration: none;
  white-space: nowrap;
  cursor: pointer;
  transition: background-color var(--motion-fast), color var(--motion-fast), box-shadow var(--motion-fast);
}

.sv-seg-item:hover {
  color: var(--ink);
  text-decoration: none;
}

.sv-seg-input:checked + .sv-seg-item,
.sv-seg-item[aria-current="page"],
.sv-seg-item[aria-pressed="true"] {
  background: var(--surface);
  box-shadow: var(--shadow-raised);
  color: var(--ink);
}

.sv-seg-input:focus-visible + .sv-seg-item {
  outline: var(--focus-width) solid var(--focus);
  outline-offset: var(--focus-offset);
}

.sv-seg-input:disabled + .sv-seg-item {
  opacity: .5;
  cursor: not-allowed;
}

.sv-seg-option:has(> .sv-seg-input:disabled) .sv-seg-input {
  cursor: not-allowed;
}

.sv-seg-sm .sv-seg-item {
  height: 28px;
}

/* Windows high contrast drops shadows and backgrounds: the halo, the
   switch track and knob, and the raised segment would all vanish, so give
   them system colors (kept in forced colors) and outlines */
@media (forced-colors: active) {
  .sv-form-errors {
    border: 1px solid CanvasText;
  }

  .sv-input:focus-within {
    outline: 2px solid Highlight;
    outline-offset: 0;
  }

  .sv-check-input:checked + .sv-check-box,
  .sv-check-input:indeterminate + .sv-check-box {
    border-color: Highlight;
    background: Highlight;
    color: HighlightText;
  }

  .sv-switch-track {
    border: 1px solid CanvasText;
  }

  .sv-switch-track::after {
    background: CanvasText;
  }

  .sv-switch-input:checked + .sv-switch-track {
    border-color: Highlight;
    background: Highlight;
  }

  .sv-switch-input:checked + .sv-switch-track::after {
    background: HighlightText;
  }

  .sv-seg-input:checked + .sv-seg-item,
  .sv-seg-item[aria-current="page"],
  .sv-seg-item[aria-pressed="true"] {
    outline: 2px solid Highlight;
    outline-offset: -2px;
  }

  /* the focused segment is the selected one: keep its ring outside */
  .sv-seg-input:focus-visible + .sv-seg-item {
    outline: var(--focus-width) solid CanvasText;
    outline-offset: var(--focus-offset);
  }
}

/* Touch screens: 44px to hit, the look stays the same */
@media (pointer: coarse) {
  .sv-check-input {
    width: 44px;
    height: 44px;
    left: -13px;
  }

  .sv-check-lg .sv-check-input {
    width: 44px;
    height: 44px;
    left: -12px;
  }

  .sv-switch-input {
    height: 44px;
  }

  .sv-seg-input {
    top: min(0px, calc((100% - var(--control-lg)) / 2));
    height: max(100%, var(--control-lg));
  }
}
