/* ============================================================
   DOORSTEP — Forms v1
   Drop-in stylesheet for form controls, field layouts, and dialogs.
   Place at: app/static/css/doorstep-forms.css
   Load AFTER doorstep.css.

   Rules:
   - Every class prefixed .ds-
   - Built only from --ds-* tokens from doorstep.css
   - No JS required
   - Does not touch Bootstrap selectors (.form-control / .modal etc)
   - Components work as standalone class additions
   ============================================================ */

/* ---------- 1. Field layout ---------- */
.ds-field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 0;
}
.ds-field + .ds-field { margin-top: 18px; }

.ds-field-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 18px 20px;
}
.ds-field-row .ds-field + .ds-field { margin-top: 0; }

.ds-fieldset {
  border: 0;
  padding: 0;
  margin: 0 0 28px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.ds-legend {
  font-family: var(--ds-font-display);
  font-size: 16px;
  font-weight: 600;
  letter-spacing: -0.01em;
  color: var(--ds-ink);
  padding: 0 0 6px;
  display: flex;
  align-items: center;
  gap: 10px;
}
.ds-legend-hint {
  font-family: var(--ds-font-body);
  font-size: 12px;
  font-weight: 500;
  color: var(--ds-ink-4);
  margin-left: 4px;
}

/* ---------- 2. Label + help + error ---------- */
.ds-label {
  font-size: 10.5px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--ds-ink-3);
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  gap: 8px;
}
.ds-label .req {
  color: var(--ds-bad);
  font-weight: 700;
}
.ds-label .opt {
  color: var(--ds-ink-5);
  font-weight: 500;
  text-transform: none;
  letter-spacing: 0.02em;
  font-size: 11px;
}

.ds-help {
  font-size: 12px;
  color: var(--ds-ink-4);
  line-height: 1.45;
}
.ds-error {
  font-size: 12px;
  color: var(--ds-bad);
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-weight: 500;
}
.ds-error::before {
  content: "";
  width: 4px;
  height: 4px;
  border-radius: 50%;
  background: var(--ds-bad);
  display: inline-block;
}

/* ---------- 3. Text inputs / selects / textareas ---------- */
.ds-input,
.ds-select,
.ds-textarea {
  width: 100%;
  font: inherit;
  font-size: 14px;
  font-family: var(--ds-font-body);
  color: var(--ds-ink);
  background: var(--ds-surf);
  border: 0;
  box-shadow: inset 0 0 0 1px var(--ds-line);
  border-radius: var(--ds-radius-sm);
  padding: 11px 14px;
  line-height: 1.4;
  transition: box-shadow .15s, background .15s;
  -webkit-appearance: none;
  appearance: none;
}
.ds-input::placeholder,
.ds-textarea::placeholder {
  color: var(--ds-ink-5);
}
.ds-input:hover,
.ds-select:hover,
.ds-textarea:hover {
  box-shadow: inset 0 0 0 1px var(--ds-ink-5);
}
.ds-input:focus,
.ds-select:focus,
.ds-textarea:focus {
  outline: 0;
  box-shadow: inset 0 0 0 2px var(--ds-ink);
  background: var(--ds-surf);
}
.ds-input[readonly],
.ds-textarea[readonly] {
  background: var(--ds-surf-2);
  color: var(--ds-ink-3);
  cursor: default;
  box-shadow: inset 0 0 0 1px var(--ds-line-2);
}
.ds-input:disabled,
.ds-select:disabled,
.ds-textarea:disabled {
  background: var(--ds-surf-3);
  color: var(--ds-ink-5);
  cursor: not-allowed;
  box-shadow: inset 0 0 0 1px var(--ds-line-2);
}

.ds-textarea {
  min-height: 96px;
  resize: vertical;
  line-height: 1.55;
}

/* Select chevron */
.ds-select {
  padding-right: 38px;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='20' height='20' viewBox='0 0 24 24' fill='none' stroke='%237E818A' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round'><path d='M6 9l6 6 6-6'/></svg>");
  background-repeat: no-repeat;
  background-position: right 12px center;
  background-size: 18px 18px;
}

/* Monospace variant — for prices, IDs, PINs */
.ds-input.mono,
.ds-textarea.mono {
  font-family: var(--ds-font-mono);
  letter-spacing: 0.02em;
}

/* Compact density */
.ds-input.sm,
.ds-select.sm { padding: 8px 12px; font-size: 13px; }
.ds-select.sm { padding-right: 34px; background-position: right 10px center; background-size: 16px 16px; }

/* Invalid states */
.ds-input.is-invalid,
.ds-select.is-invalid,
.ds-textarea.is-invalid,
.ds-field.has-error .ds-input,
.ds-field.has-error .ds-select,
.ds-field.has-error .ds-textarea {
  box-shadow: inset 0 0 0 1px var(--ds-bad);
  background: var(--ds-bad-tint);
}
.ds-input.is-invalid:focus,
.ds-select.is-invalid:focus,
.ds-textarea.is-invalid:focus,
.ds-field.has-error .ds-input:focus,
.ds-field.has-error .ds-select:focus,
.ds-field.has-error .ds-textarea:focus {
  box-shadow: inset 0 0 0 2px var(--ds-bad);
  background: var(--ds-surf);
}

/* ---------- 4. Input with leading/trailing affix ---------- */
.ds-input-wrap {
  position: relative;
  display: flex;
  align-items: center;
}
.ds-input-wrap .ds-input { padding-left: 40px; }
.ds-input-wrap .ds-input.has-suffix { padding-right: 52px; padding-left: 14px; }
.ds-input-wrap .ds-input.has-prefix.has-suffix { padding-left: 40px; padding-right: 52px; }

.ds-input-wrap .prefix,
.ds-input-wrap .suffix {
  position: absolute;
  color: var(--ds-ink-4);
  font-size: 12px;
  font-weight: 600;
  pointer-events: none;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}
.ds-input-wrap .prefix { left: 14px; }
.ds-input-wrap .suffix { right: 14px; font-family: var(--ds-font-mono); letter-spacing: 0.04em; text-transform: uppercase; font-size: 10.5px; }
.ds-input-wrap .prefix .ds-icon,
.ds-input-wrap .suffix .ds-icon { width: 16px; height: 16px; }

/* Input with trailing inline button (e.g. Generate PIN, Copy) */
.ds-input-wrap .inline-btn {
  position: absolute;
  right: 5px;
  top: 50%;
  transform: translateY(-50%);
  padding: 6px 12px;
  border-radius: var(--ds-radius-xs);
  font-size: 11px;
  font-weight: 600;
  color: var(--ds-ink-2);
  background: var(--ds-surf-3);
  pointer-events: auto;
  cursor: pointer;
}
.ds-input-wrap .inline-btn:hover { background: var(--ds-line-2); color: var(--ds-ink); }

/* ---------- 5. Checkbox + Radio ---------- */
.ds-check,
.ds-radio {
  display: inline-flex;
  align-items: flex-start;
  gap: 10px;
  cursor: pointer;
  font-size: 13.5px;
  color: var(--ds-ink-2);
  line-height: 1.5;
  padding: 4px 0;
  user-select: none;
}
.ds-check input,
.ds-radio input {
  appearance: none;
  -webkit-appearance: none;
  flex-shrink: 0;
  width: 18px;
  height: 18px;
  margin: 1px 0 0;
  background: var(--ds-surf);
  box-shadow: inset 0 0 0 1.5px var(--ds-line);
  border-radius: 5px;
  cursor: pointer;
  position: relative;
  transition: all .15s;
}
.ds-radio input {
  border-radius: 50%;
}
.ds-check input:hover,
.ds-radio input:hover {
  box-shadow: inset 0 0 0 1.5px var(--ds-ink-4);
}
.ds-check input:focus-visible,
.ds-radio input:focus-visible {
  outline: 2px solid var(--ds-ink);
  outline-offset: 2px;
}
.ds-check input:checked {
  background: var(--ds-ink);
  box-shadow: inset 0 0 0 1.5px var(--ds-ink);
}
.ds-check input:checked::after {
  content: "";
  position: absolute;
  inset: 0;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='white' stroke-width='3' stroke-linecap='round' stroke-linejoin='round'><path d='M5 12l5 5 9-10'/></svg>");
  background-position: center;
  background-repeat: no-repeat;
  background-size: 14px 14px;
}
.ds-radio input:checked {
  box-shadow: inset 0 0 0 5px var(--ds-ink);
  background: var(--ds-surf);
}
.ds-check input:disabled,
.ds-radio input:disabled {
  background: var(--ds-surf-3);
  box-shadow: inset 0 0 0 1.5px var(--ds-line-2);
  cursor: not-allowed;
}
.ds-check input:disabled + span,
.ds-radio input:disabled + span {
  color: var(--ds-ink-5);
  cursor: not-allowed;
}
.ds-check .sub,
.ds-radio .sub {
  display: block;
  font-size: 12px;
  color: var(--ds-ink-4);
  margin-top: 2px;
  font-weight: 400;
}

/* Card-style radio group (for big choices like role, plan) */
.ds-radio-card {
  display: flex;
  gap: 12px;
  padding: 14px 16px;
  border-radius: var(--ds-radius-sm);
  background: var(--ds-surf);
  box-shadow: inset 0 0 0 1px var(--ds-line);
  cursor: pointer;
  transition: box-shadow .15s, background .15s;
  align-items: flex-start;
}
.ds-radio-card:hover { box-shadow: inset 0 0 0 1px var(--ds-ink-4); }
.ds-radio-card input { margin-top: 2px; }
.ds-radio-card:has(input:checked) {
  box-shadow: inset 0 0 0 2px var(--ds-ink);
  background: var(--ds-surf);
}
.ds-radio-card .title { font-weight: 600; color: var(--ds-ink); font-size: 14px; }
.ds-radio-card .desc { font-size: 12.5px; color: var(--ds-ink-4); margin-top: 2px; line-height: 1.45; }

/* ---------- 6. Switch ---------- */
.ds-switch {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  cursor: pointer;
  user-select: none;
  font-size: 13.5px;
  color: var(--ds-ink-2);
}
.ds-switch input {
  appearance: none;
  -webkit-appearance: none;
  width: 36px;
  height: 20px;
  background: var(--ds-line);
  border-radius: 99px;
  position: relative;
  cursor: pointer;
  transition: background .18s;
  flex-shrink: 0;
}
.ds-switch input::after {
  content: "";
  position: absolute;
  left: 2px;
  top: 2px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 2px rgba(10,12,20,.2);
  transition: left .18s;
}
.ds-switch input:checked {
  background: var(--ds-ink);
}
.ds-switch input:checked::after {
  left: 18px;
}
.ds-switch input:focus-visible {
  outline: 2px solid var(--ds-ink);
  outline-offset: 3px;
}
.ds-switch input:disabled {
  background: var(--ds-surf-3);
  cursor: not-allowed;
}
.ds-switch input:disabled::after {
  background: var(--ds-line-2);
  box-shadow: none;
}
.ds-switch .sub {
  display: block;
  font-size: 12px;
  color: var(--ds-ink-4);
  margin-top: 2px;
  font-weight: 400;
}

/* ---------- 7. File upload ---------- */
.ds-file {
  display: block;
  border-radius: var(--ds-radius-sm);
  background: var(--ds-surf);
  box-shadow: inset 0 0 0 1px var(--ds-line);
  padding: 22px 18px;
  text-align: center;
  cursor: pointer;
  transition: box-shadow .15s, background .15s;
  position: relative;
}
.ds-file:hover {
  box-shadow: inset 0 0 0 1px var(--ds-ink-4);
  background: var(--ds-surf-2);
}
.ds-file.is-dragover {
  box-shadow: inset 0 0 0 2px var(--ds-ink);
  background: var(--ds-surf-2);
}
.ds-file input[type="file"] {
  position: absolute;
  opacity: 0;
  inset: 0;
  width: 100%;
  height: 100%;
  cursor: pointer;
}
.ds-file-title {
  font-family: var(--ds-font-display);
  font-size: 14px;
  font-weight: 600;
  color: var(--ds-ink);
  margin-bottom: 4px;
}
.ds-file-hint {
  font-size: 12px;
  color: var(--ds-ink-4);
  font-family: var(--ds-font-mono);
  letter-spacing: 0.02em;
}

/* File chip — when a file is selected */
.ds-file-chip {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px 8px 8px;
  background: var(--ds-surf-3);
  border-radius: var(--ds-radius-sm);
  font-size: 12.5px;
  color: var(--ds-ink-2);
  font-weight: 500;
}
.ds-file-chip .thumb {
  width: 28px;
  height: 28px;
  border-radius: var(--ds-radius-xs);
  background: var(--ds-surf);
  display: grid;
  place-items: center;
  color: var(--ds-ink-4);
  flex-shrink: 0;
}
.ds-file-chip .thumb .ds-icon { width: 16px; height: 16px; }
.ds-file-chip .meta { line-height: 1.25; min-width: 0; }
.ds-file-chip .meta strong { font-weight: 600; color: var(--ds-ink); display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 200px; }
.ds-file-chip .meta small { font-size: 10.5px; color: var(--ds-ink-4); font-family: var(--ds-font-mono); letter-spacing: 0.02em; text-transform: uppercase; }
.ds-file-chip button {
  margin-left: auto;
  width: 22px;
  height: 22px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  color: var(--ds-ink-4);
  cursor: pointer;
}
.ds-file-chip button:hover { background: var(--ds-line-2); color: var(--ds-ink); }

/* ---------- 8. Input groups (segmented, button row) ---------- */
.ds-seg {
  display: inline-flex;
  background: var(--ds-surf);
  padding: 3px;
  border-radius: 99px;
  box-shadow: inset 0 0 0 1px var(--ds-line);
  gap: 0;
}
.ds-seg label {
  position: relative;
  padding: 7px 14px;
  font-size: 12px;
  font-weight: 600;
  color: var(--ds-ink-4);
  border-radius: 99px;
  cursor: pointer;
  transition: color .15s, background .15s;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.ds-seg label input { position: absolute; opacity: 0; inset: 0; cursor: pointer; }
.ds-seg label:has(input:checked) { background: var(--ds-ink); color: #fff; }
.ds-seg label:hover { color: var(--ds-ink); }
.ds-seg label:has(input:checked):hover { color: #fff; }

/* ---------- 9. PIN / code input ---------- */
.ds-pin {
  display: inline-flex;
  gap: 8px;
}
.ds-pin input {
  width: 44px;
  height: 52px;
  text-align: center;
  font-family: var(--ds-font-mono);
  font-size: 20px;
  font-weight: 500;
  color: var(--ds-ink);
  background: var(--ds-surf);
  box-shadow: inset 0 0 0 1px var(--ds-line);
  border: 0;
  border-radius: var(--ds-radius-sm);
  -moz-appearance: textfield;
}
.ds-pin input::-webkit-outer-spin-button,
.ds-pin input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.ds-pin input:focus {
  outline: 0;
  box-shadow: inset 0 0 0 2px var(--ds-ink);
}

/* ---------- 10. Form actions strip ---------- */
.ds-form-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 20px 0 4px;
  margin-top: 28px;
  border-top: 1px solid var(--ds-line-2);
}
.ds-form-actions .spacer { flex: 1; }
.ds-form-actions .dirty {
  font-family: var(--ds-font-mono);
  font-size: 10.5px;
  color: var(--ds-warn);
  letter-spacing: 0.1em;
  text-transform: uppercase;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.ds-form-actions .dirty::before {
  content: "";
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--ds-warn);
}

/* Sticky variant — bottom of long forms */
.ds-form-actions.sticky {
  position: sticky;
  bottom: 0;
  background: var(--ds-surf-2);
  z-index: 5;
  padding: 16px 0;
  margin-left: -4px;
  margin-right: -4px;
  padding-left: 4px;
  padding-right: 4px;
}

/* ---------- 11. Button variants (forms extend doorstep buttons) ---------- */
.ds-btn.danger {
  background: var(--ds-bad-tint);
  color: var(--ds-bad);
}
.ds-btn.danger:hover {
  background: var(--ds-bad);
  color: #fff;
}
.ds-btn.outline {
  background: transparent;
  box-shadow: inset 0 0 0 1px var(--ds-line);
}
.ds-btn.outline:hover {
  background: var(--ds-surf);
  box-shadow: inset 0 0 0 1px var(--ds-ink-4);
}
.ds-btn.sm {
  padding: 7px 12px;
  font-size: 12px;
}
.ds-btn.block {
  display: flex;
  justify-content: center;
  width: 100%;
}

/* ---------- 12. Dialog (native <dialog>) ---------- */
/* NOT for the legacy Bootstrap PIN modal — use only on new dialogs with <dialog class="ds-dialog"> */
.ds-dialog {
  padding: 0;
  border: 0;
  border-radius: var(--ds-radius);
  box-shadow: var(--ds-shadow-float);
  background: var(--ds-surf);
  color: var(--ds-ink);
  max-width: min(520px, calc(100vw - 40px));
  width: 100%;
  max-height: calc(100vh - 60px);
  overflow: hidden;
  font-family: var(--ds-font-body);
}
.ds-dialog::backdrop {
  background: rgba(10,12,20,.42);
  backdrop-filter: blur(4px);
}
.ds-dialog-head {
  padding: 20px 22px 16px;
  display: flex;
  align-items: start;
  gap: 16px;
}
.ds-dialog-head h2 {
  font-family: var(--ds-font-display);
  font-size: 20px;
  font-weight: 600;
  letter-spacing: -0.015em;
  color: var(--ds-ink);
  margin: 0;
  flex: 1;
  line-height: 1.2;
}
.ds-dialog-head p {
  font-size: 13px;
  color: var(--ds-ink-4);
  margin: 4px 0 0;
  line-height: 1.5;
}
.ds-dialog-close {
  width: 32px;
  height: 32px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  color: var(--ds-ink-4);
  flex-shrink: 0;
  cursor: pointer;
}
.ds-dialog-close:hover { background: var(--ds-surf-3); color: var(--ds-ink); }
.ds-dialog-body {
  padding: 6px 22px 22px;
  overflow-y: auto;
  max-height: calc(100vh - 240px);
}
.ds-dialog-foot {
  padding: 16px 22px;
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  background: var(--ds-surf-2);
}

/* ---------- 13. Inline message strips (in-form notices) ---------- */
.ds-notice {
  display: flex;
  gap: 12px;
  padding: 14px 16px;
  border-radius: var(--ds-radius-sm);
  font-size: 13px;
  line-height: 1.5;
  align-items: flex-start;
}
.ds-notice .ds-icon { flex-shrink: 0; margin-top: 1px; }
.ds-notice strong { font-weight: 600; display: block; margin-bottom: 2px; }
.ds-notice.info  { background: var(--ds-blue-tint);     color: var(--ds-notice-info-text, #0B1680); }
.ds-notice.info strong { color: var(--ds-blue); }
.ds-notice.warn  { background: var(--ds-warn-tint);     color: var(--ds-notice-warn-text, #5A3A00); }
.ds-notice.warn strong { color: var(--ds-warn); }
.ds-notice.good  { background: var(--ds-good-tint);     color: var(--ds-notice-good-text, #15592B); }
.ds-notice.good strong { color: var(--ds-good); }
.ds-notice.bad   { background: var(--ds-bad-tint);      color: var(--ds-notice-bad-text, #7A1414); }
.ds-notice.bad strong { color: var(--ds-bad); }

:root[data-theme="dark"] {
  --ds-notice-info-text: var(--ds-ink-2);
  --ds-notice-warn-text: var(--ds-ink-2);
  --ds-notice-good-text: var(--ds-ink-2);
  --ds-notice-bad-text:  var(--ds-ink-2);
}

/* ---------- 14. Utility ---------- */
.ds-stack { display: flex; flex-direction: column; gap: 14px; }
.ds-stack-sm { display: flex; flex-direction: column; gap: 8px; }
.ds-row { display: flex; gap: 10px; align-items: center; }
.ds-row-between { display: flex; gap: 10px; align-items: center; justify-content: space-between; }
.ds-grow { flex: 1; min-width: 0; }
