/* ==========================================================================
   Forms
   ========================================================================== */

/* min-width: 0 is load-bearing, not defensive styling. A flex/grid item's
   automatic minimum size is its content's min-content size, and form
   controls (notably input[type=date] and input[type=number]) carry a large
   intrinsic minimum that `width: 100%` does not override in WebKit. Without
   this the field refuses to shrink, pushes past the dialog, and - because
   .modal__body scrolls - the whole panel becomes pannable sideways. */
.field {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  min-width: 0;
}

.field__label {
  font-size: var(--fs-caption);
  font-weight: var(--fw-medium);
  color: var(--color-text-secondary);
}

.field__hint {
  font-size: var(--fs-caption);
  color: var(--color-text-tertiary);
}

.input,
.select {
  background: var(--color-surface-raised);
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius-md);
  padding: var(--space-3) var(--space-4);
  font-size: var(--fs-body);
  color: var(--color-text-primary);
  transition: border-color var(--dur-fast) var(--ease-standard),
    box-shadow var(--dur-fast) var(--ease-standard);
  width: 100%;
  /* Controls must be allowed to shrink below their intrinsic minimum;
     see the note on .field above. box-sizing is already border-box from
     the reset, so max-width: 100% cannot be defeated by padding. */
  min-width: 0;
  max-width: 100%;
}

/* WebKit gives date/time/number controls an intrinsic width that ignores
   `width: 100%`; these declarations are what actually let them fit a
   narrow dialog on iOS. */
input[type="date"],
input[type="time"],
input[type="datetime-local"],
input[type="number"] {
  min-width: 0;
  max-width: 100%;
}

/* ---- Date/time controls on WebKit ----
   `min-width: 0` alone is not enough. While the control keeps its native
   appearance, iOS Safari sizes it from the *shadow* content - the
   ::-webkit-date-and-time-value box - and that box has its own intrinsic
   width plus a centring margin, neither of which the host element's width
   can override. The result is a field that renders wider than its
   container and pushes past the dialog edge.

   Dropping the native appearance is what hands sizing back to CSS; the
   shadow-part rules then stop the inner value box from re-inflating it.
   Everything else about the control (the native picker, the value format,
   the locale) is unaffected - only its box sizing changes. */
input[type="date"],
input[type="time"],
input[type="datetime-local"] {
  -webkit-appearance: none;
  appearance: none;
  display: block;
  width: 100%;
}

input[type="date"]::-webkit-date-and-time-value,
input[type="time"]::-webkit-date-and-time-value,
input[type="datetime-local"]::-webkit-date-and-time-value {
  /* iOS centres this box and lets it set the control's width. Left-align
     it, let it shrink, and it stops driving the outer size. */
  text-align: left;
  min-width: 0;
  margin: 0;
}

input[type="date"]::-webkit-datetime-edit,
input[type="time"]::-webkit-datetime-edit,
input[type="datetime-local"]::-webkit-datetime-edit {
  min-width: 0;
  overflow: hidden;
}

/* Removing the native appearance also removes the calendar affordance on
   WebKit, so restore a visible one that stays inside the field. */
input[type="date"]::-webkit-calendar-picker-indicator {
  margin-left: auto;
  flex-shrink: 0;
  opacity: 0.5;
  cursor: pointer;
}

/* The focus ring is the accent at 30%, not --color-accent-soft (13%): a
   ring meant to say "you are here" has to be visible at a glance. */
.input:focus,
.select:focus {
  outline: none;
  border-color: var(--color-accent);
  box-shadow: 0 0 0 3px rgba(var(--color-accent-rgb), 0.3);
}

/* iOS Safari auto-zooms the page on focusing any text input/textarea/select
   under 16px, which is what made New Trade / Add Challenge / Growth
   Simulator's number fields feel like a "moving", mis-scaled panel on
   phones - this is the shared root cause, fixed once here rather than
   per-view. Scoped to mobile widths only so desktop's 15px stays as-is. */
@media (max-width: 880px) {
  .input,
  .select,
  textarea.input {
    font-size: 16px;
  }
}

.input.is-invalid {
  border-color: var(--color-loss);
}

.input.is-invalid:focus {
  border-color: var(--color-loss);
  box-shadow: 0 0 0 3px var(--color-loss-soft);
}

.input::placeholder {
  color: var(--color-text-tertiary);
}

.input:hover:not(:focus) {
  border-color: var(--color-border-strong);
}

.select {
  appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, currentColor 50%),
    linear-gradient(135deg, currentColor 50%, transparent 50%);
  background-position: calc(100% - 20px) center, calc(100% - 15px) center;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
  color: var(--color-text-primary);
  cursor: pointer;
}

/* ---- Custom select (enhances native .select; see js/components/select.js) ---- */

.cselect {
  position: relative;
  width: 100%;
}

/* Native <select> kept as the source of truth, visually removed but still
   focusable-by-name / present for FormData. */
.cselect__native {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  border: 0;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  overflow: hidden;
  pointer-events: none;
}

.cselect__trigger {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2);
  background: var(--color-surface-raised);
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius-md);
  padding: var(--space-3) var(--space-4);
  font-size: var(--fs-body);
  font-weight: var(--fw-medium);
  color: var(--color-text-primary);
  cursor: pointer;
  text-align: left;
  transition: border-color var(--dur-fast) var(--ease-standard),
    background-color var(--dur-fast) var(--ease-standard),
    box-shadow var(--dur-fast) var(--ease-standard);
}

.cselect__trigger:hover {
  border-color: var(--color-border-strong);
  background: var(--color-surface-hover);
}

.cselect.is-open .cselect__trigger,
.cselect__trigger:focus-visible {
  outline: none;
  border-color: var(--color-accent);
  box-shadow: 0 0 0 3px rgba(var(--color-accent-rgb), 0.3);
}

.cselect__trigger[disabled] {
  opacity: 0.5;
  pointer-events: none;
}

.cselect__value {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.cselect__chevron {
  flex-shrink: 0;
  color: var(--color-text-tertiary);
  transition: transform var(--dur-fast) var(--ease-standard);
}

.cselect.is-open .cselect__chevron {
  transform: rotate(180deg);
  color: var(--color-text-secondary);
}

.cselect__panel {
  /* Portaled to <body> and positioned via inline left/top/width when open
     (see select.js) - this deliberately escapes any ancestor's stacking
     context or overflow clipping, which `position: absolute` nested inside
     .cselect could not (e.g. .stagger-item's persistent transform from its
     entrance animation creates a new stacking context that trapped a
     nested z-index). */
  position: fixed;
  z-index: var(--z-dropdown);
  background: var(--color-surface-raised);
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  padding: var(--space-2);
  display: none;
  flex-direction: column;
  gap: 2px;
  max-height: 264px;
  overflow-y: auto;
  animation: cselect-pop var(--dur-fast) var(--ease-decel);
}

.cselect__panel.is-open {
  display: flex;
}

@keyframes cselect-pop {
  from { opacity: 0; transform: translateY(-4px); }
  to { opacity: 1; transform: translateY(0); }
}

.cselect__option {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-sm);
  font-size: var(--fs-footnote);
  color: var(--color-text-secondary);
  text-align: left;
  cursor: pointer;
  white-space: nowrap;
  transition: background-color var(--dur-fast) var(--ease-standard),
    color var(--dur-fast) var(--ease-standard);
}

/* The highlighted option - keyboard (aria-activedescendant) and pointer
   both move it (select.js), so there is one highlight, never a hover and a
   keyboard focus pointing at different rows. */
.cselect__option.is-focused {
  background: var(--color-surface-hover);
  color: var(--color-text-primary);
}

.cselect__option.is-active {
  color: var(--color-accent);
  font-weight: var(--fw-medium);
  background: var(--color-accent-soft);
}

/* minmax(0, 1fr), never a bare 1fr: a bare `1fr` track floors at its
   content's min-content size, which for a form control is large enough to
   blow the dialog out sideways. Same fix, same reason as .grid-cols-N in
   layout.css. */
.form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--space-4);
  min-width: 0;
}

@media (max-width: 700px) {
  .form-grid {
    grid-template-columns: minmax(0, 1fr);
  }
}

/* ---- Segmented control (tabs) ---- */

.segmented {
  display: inline-flex;
  padding: 3px;
  background: var(--color-surface-raised);
  /* The strong hairline, not the default one: in light mode the track is
     #ffffff on a #f4f4f6 page, so the border is the ONLY thing separating
     the control from what it sits on. */
  border: 1px solid var(--color-border-strong);
  /* Recessed, like .pill-toggle - the thumb rides in a groove. */
  box-shadow: inset 0 1px 2px var(--surface-lowlight);
  border-radius: var(--radius-pill);
  position: relative;
  gap: 2px;
}

/* --fw-semibold on every option, active and inactive alike. Weighting only
   the active one would be the obvious way to make it stand out, but the
   option widths - and therefore the thumb measured from them - would change
   on every switch, so the other labels would visibly shift. The active state
   is carried by colour and by the thumb behind it; the weight is here to
   make the labels themselves legible, which is also what .pill-toggle__opt
   already uses, so the two control families in the Journal toolbar finally
   share a type weight. */
.segmented__option {
  position: relative;
  z-index: 1;
  padding: var(--space-2) var(--space-5);
  font-size: var(--fs-caption);
  font-weight: var(--fw-semibold);
  color: var(--color-text-secondary);
  border-radius: var(--radius-pill);
  cursor: pointer;
  transition: color var(--dur-fast) var(--ease-standard);
}

.segmented__option:hover {
  color: var(--color-text-primary);
}

.segmented__option.is-active {
  color: var(--color-text-primary);
}

.segmented__thumb {
  position: absolute;
  top: 3px;
  bottom: 3px;
  /* left: 0 is load-bearing, not a reset.
     Every positioner in the app writes translateX(active.offsetLeft), and
     offsetLeft is measured from the offsetParent's PADDING edge. Without an
     explicit `left`, an absolutely positioned box falls back to its static
     position - the start of the CONTENT box - so the track's own 3px padding
     was counted twice and the thumb sat ~3.5px right of the label it is
     meant to sit behind. Pinning left: 0 puts its origin on the padding edge,
     the same origin offsetLeft uses. Measured on the Journal tab bar: 3.4px
     off before, 0.0px after. Worst on the narrowest option ("List", 35px),
     where 3.5px is 10% of the pill - which is what read as an uneven,
     half-missed highlight. Fixes every .segmented in the app at once, with
     no change to any of the four JS positioners. */
  left: 0;
  /* The thumb's geometry is written inline in JS from a measurement taken
     at render time. It is a decoration that by definition can never be
     wider than the track it slides in, so this invariant is stated here
     rather than trusted to every call site re-measuring on resize. Without
     it a stale width (dialog measured wide, then narrowed) overflows its
     container and makes the whole panel pannable. */
  max-width: 100%;
  border-radius: var(--radius-pill);
  /* --color-surface-hover, not --color-surface: it is the one neutral that
     contrasts with the track (--color-surface-raised) in BOTH themes, so one
     declaration covers them without a per-theme override. It was broken both
     ways before - dark: #131316 thumb on a #1c1c1f track, i.e. the active
     pill was DARKER than the track and read as a hole; light: #ffffff on
     #ffffff, identical, so the pill was invisible and the weakened
     --shadow-sm was carrying the whole active state on its own.
     Now dark #232326 sits above its track, light #f0f0f2 below its white
     one - visible either way. */
  background: var(--color-surface-hover);
  /* Definition for the light theme, where a near-white pill on white has
     almost no luminance step to rely on. box-sizing is border-box globally,
     so this does not change the width JS writes. */
  border: 1px solid var(--color-border-strong);
  /* Lit top edge on top of the drop shadow: the thumb sits PROUD of the
     groove it slides in, which is the whole reason the recess above exists. */
  box-shadow: var(--shadow-sm), inset 0 1px 0 var(--surface-highlight);
  transition: transform var(--dur-base) var(--ease-spring),
    width var(--dur-base) var(--ease-spring);
}

/* ---- Touch target sizing ----
   Bumps tap targets toward the ~44px recommendation on phones/tablets
   and genuine touch input, without changing the tighter desktop-mouse
   sizing. */
@media (max-width: 880px), (pointer: coarse) {
  .btn--icon {
    width: 44px;
    height: 44px;
  }

  .segmented__option {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 38px;
    padding: var(--space-3) var(--space-5);
  }
}

/* ---- Toggle switch ---- */

.switch {
  width: 44px;
  height: 26px;
  border-radius: var(--radius-pill);
  background: var(--color-border-strong);
  position: relative;
  transition: background-color var(--dur-base) var(--ease-standard);
  flex-shrink: 0;
}

.switch.is-on {
  background: var(--color-accent);
}

.switch__thumb {
  position: absolute;
  top: 3px;
  left: 3px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
  transition: transform var(--dur-base) var(--ease-spring-snappy);
}

.switch.is-on .switch__thumb {
  transform: translateX(18px);
}

/* ---- Slider ---- */

.slider {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 4px;
  border-radius: var(--radius-pill);
  background: var(--color-border-strong);
  outline: none;
}

.slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #fff;
  border: 2px solid var(--color-accent);
  box-shadow: var(--shadow-sm);
  cursor: pointer;
  transition: transform var(--dur-fast) var(--ease-spring-snappy);
}

.slider::-webkit-slider-thumb:hover {
  transform: scale(1.15);
}

.slider::-moz-range-thumb {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #fff;
  border: 2px solid var(--color-accent);
  cursor: pointer;
}

/* `outline: none` above hid keyboard focus on the Growth Simulator sliders
   entirely; the ring moves to the thumb, where the value is. */
.slider:focus-visible::-webkit-slider-thumb {
  box-shadow: 0 0 0 4px rgba(var(--color-accent-rgb), 0.35);
}
.slider:focus-visible::-moz-range-thumb {
  box-shadow: 0 0 0 4px rgba(var(--color-accent-rgb), 0.35);
}


/* Account colour picker (js/components/account-form.js, audit Etap 6.4). */
.acct-swatches {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-3);
}

.acct-swatch {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border-radius: var(--radius-sm);
  background: var(--sw);
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.12);
  color: #fff;
  transition: transform var(--dur-fast) var(--ease-standard),
    opacity var(--dur-fast) var(--ease-standard);
}

.acct-swatch svg {
  opacity: 0;
}

.acct-swatch.is-active {
  box-shadow: 0 0 0 2px var(--color-surface-raised), 0 0 0 4px var(--sw);
}

.acct-swatch.is-active svg {
  opacity: 1;
  filter: drop-shadow(0 0 1px rgba(0, 0, 0, 0.6));
}

/* Used by another account: dimmed, still selectable. */
.acct-swatch.is-used {
  opacity: 0.4;
}

/* Focus must read on every swatch: undimmed, and the outline clear of the
   selection ring (which on the accent swatch is the outline's own colour). */
.acct-swatch:focus-visible {
  opacity: 1;
  outline-offset: 5px;
}

.acct-swatch:hover {
  transform: scale(1.08);
  opacity: 1;
}

/* Phone: two even rows of four at a full 44px touch target, rather than
   a ragged wrap. */
@media (max-width: 700px) {
  .acct-swatches {
    display: grid;
    grid-template-columns: repeat(4, 44px);
  }

  .acct-swatch {
    width: 44px;
    height: 44px;
  }

  [data-type-picker] .chip {
    min-height: 44px;
  }
}
