/**
 * @prop --mds-input-date-icon-color: The color of the date input icon
 * @prop --mds-input-date-background: Public. The background of the date input
 * @prop --mds-input-date-field-background-empty: The background when the date field is empty
 * @prop --mds-input-date-field-color-empty: The text/icon color when the date field is empty
 * @prop --mds-input-date-icon-color-rgb: Deprecated, read by nothing: the icon takes `--mds-input-date-icon-color`.
 * @prop --mds-input-date-ring: Public. The focus ring of the date input
 * @prop --mds-input-date-shadow: Public. The shadow applied to the date input
 * @prop --mds-input-date-variant-color-rgb: Public. The base RGB value for the date input variant
 * @prop --mds-input-tip-background: Public. The background of the input tip
 */

/* ❌ Not supported
@property --mds-input-date-ring {
  inherits: false;
  initial-value: 0 0 0 1px rgb(var(--mds-input-date-variant-color-rgb) / 0.1);
  syntax: "<box-shadow>";
}

@property --mds-input-date-shadow {
  inherits: false;
  initial-value: 0 1px 3px 0 rgb(var(--mds-input-date-variant-color-rgb) / 0.1), 0 1px 2px 0 rgb(var(--mds-input-date-variant-color-rgb) / 0.06);
  syntax: "<box-shadow>";
}
*/

:host {

  /* the size of the field, lg by default: its height and its padding; the calendar button sits
     in a square as tall as the field */
  --private-input-date-height: --spacing(1200);
  --private-input-date-padding-inline: --spacing(400);
  --private-input-date-calendar-area: --spacing(1200);

  /* capped at a part of the height of the field like mds-button's (#913); the top tip keeps clear
     of the corner by it (#1368) */
  --private-input-date-radius: min(
    var(--magma-radius-2xl, var(--radius-squircle-2xl, 40px)),
    var(--magma-corner-cap, 0.5) * var(--private-input-date-height)
  );
  --mds-input-date-icon-color: rgb(var(--magma-accent-fg, var(--variant-primary-03, 0 73 211)));

  /* a field stands a step off its surface: overlay on a page, the step below inside an overlay
     surface, which passes it as --magma-field-fill (#1193) */
  --mds-input-date-background: rgb(var(--magma-field-fill, var(--magma-surface-overlay, var(--surface-neutral-overlay, 255 255 255))));
  --mds-input-date-field-background-empty: rgb(var(--magma-danger-wash-soft, var(--status-error-10, 255 232 230)));
  --mds-input-date-field-color-empty: rgb(var(--magma-danger-text-default, var(--text-error-default, 89 0 0)));
  --mds-input-date-ring: 0 0 0 1px rgb(var(--mds-input-date-variant-color-rgb, 0 0 0) / 0.1);
  --mds-input-date-shadow:
    0 1px --spacing(75) 0 rgb(var(--mds-input-date-variant-color-rgb, 0 0 0) / 0.1),
    0 1px --spacing(50) 0 rgb(var(--mds-input-date-variant-color-rgb, 0 0 0) / 0.06);
  --mds-input-date-variant-color-rgb: 0 0 0;
  --mds-input-tip-background: rgb(var(--magma-surface-inverse-muted, var(--tone-neutral-03, 73 73 73)));
  --mds-input-date-transition-duration: var(--duration-300, 300ms);

  /* private */
  --private-input-date-transition-duration: var(--mds-input-date-transition-duration);

  @apply text-info-detail;

  border-radius: var(--private-input-date-radius);
  color: rgb(var(--magma-text-default, var(--text-neutral-default, 5 5 5)));
  container-type: inline-size;
  corner-shape: var(--magma-corner-shape, squircle);
  display: flex;
  gap: --spacing(200);
  overflow: visible;
  position: relative;
}

.input::-webkit-calendar-picker-indicator {
  appearance: none;

  /* stylelint-disable-next-line order/properties-alphabetical-order */
  appearance: textfield; /* hide it Firefox */
  display: none;
}

.input {
  @apply text-code-hack;

  background-color: transparent;
  border: 0;
  border-radius: inherit;
  box-sizing: border-box;
  color: inherit;
  corner-shape: inherit;
  margin: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  transition-duration: var(--private-input-date-transition-duration);
  transition-property:
    background-color, border-color, box-shadow, color, fill, margin, opacity, padding, transform;
  transition-timing-function: var(--ease-out-expo, cubic-bezier(0.19, 1, 0.22, 1));
  width: min-content;
}

:host([empty]) .input {
  background-color: var(--mds-input-date-field-background-empty, rgb(var(--magma-danger-wash-soft, var(--status-error-10, 255 232 230))));
  color: var(--mds-input-date-field-color-empty, rgb(var(--magma-danger-text-default, var(--text-error-default, 89 0 0))));
}

:host([slot]) {
  color: inherit;
  display: contents;
}

:host([slot]) .input {
  cursor: text;
  text-align: center;
}

:host(:not([slot])) .input {
  background-color: var(--mds-input-date-background, rgb(var(--magma-surface-overlay, var(--surface-neutral-overlay, 255 255 255))));

  /* border-radius: var(--magma-radius-xs); */
  box-shadow: var(--mds-input-date-ring), var(--mds-input-date-shadow);
  min-height: var(--private-input-date-height);
  padding-left: var(--private-input-date-padding-inline);
  padding-right: var(--private-input-date-padding-inline);
  width: 100%;
}

/* the calendar button takes the end of the field only where it is shown, on a field the user can
   change (hasCalendarButton): a read-only or disabled date has the padding of any field (#1368) */

:host(:not([slot])) .input.has-calendar-button {
  padding-right: var(--private-input-date-calendar-area);
}

:host(:focus-within) .input {
  @apply outline-hidden transition-cosmetic;

  --mds-input-date-ring: 0 0 0 --spacing(75) rgb(var(--mds-input-date-variant-color-rgb, 0 0 0) / 1);
  --mds-input-date-shadow:
    0 --spacing(100) --spacing(150) --spacing(75) rgb(var(--mds-input-date-variant-color-rgb, 0 0 0) / 0.1),
    0 --spacing(50) --spacing(100) -1px rgb(var(--mds-input-date-variant-color-rgb, 0 0 0) / 0.06);

  transition-duration: var(--duration-300, 300ms);
  transition-timing-function: var(--ease-out-expo, cubic-bezier(0.19, 1, 0.22, 1));
}

.input::placeholder {
  color: rgb(var(--magma-text-subtle, var(--text-neutral-subtle, 99 99 99)));
}

.input::-webkit-outer-spin-button,
.input::-webkit-inner-spin-button {
  appearance: none;
  display: none;
  margin: 0;
}

.input:disabled {
  background-color: rgb(var(--magma-wash-soft, var(--tone-neutral-10, 237 237 237)));
}

.input:disabled,
.input:disabled::placeholder {
  color: rgb(var(--magma-text-disabled, var(--text-neutral-disabled, 119 119 119)));
}

.action-open-calendar-wrapper {
  align-items: center;
  aspect-ratio: 1;
  bottom: 0;
  display: flex;
  justify-content: center;
  position: absolute;
  right: 0;
  top: 0;
}

.action-open-calendar {
  --mds-button-color: var(--mds-input-date-icon-color, rgb(var(--magma-accent-emphasis, var(--variant-primary-04, 11 96 255))));

  /* mds-button aligns itself flex-start on its :host: re-center it inside the
     48px square wrapper so the icon sits on the input's vertical axis. */
  align-self: center;
}

mds-calendar {
  min-width: min-content;
  width: 100cqw;
}

mds-dropdown {
  padding: 0;
}

mds-input-tip {
  --mds-input-tip-field-radius: var(--private-input-date-radius);
  --mds-input-tip-background: inherit;
}

:host(:not(:is([hydrated], .hydrated))) {
  @apply not-hydrated;
}


mds-button,
mds-calendar,
mds-dropdown,
mds-input-tip,
mds-input-tip-item {
  &:not(:is([hydrated], .hydrated)) {
    @apply not-hydrated;
  }
}

/* The size of the field, with the heights of mds-button: md 36px and sm 28px here, lg 48px in
 * the base sheet. The calendar button sits in a square as tall as the field: the default md
 * button fills the one of an md field, an sm field gets an sm button (see the render). */

:host([size='md']) {
  --private-input-date-height: --spacing(900);
  --private-input-date-padding-inline: --spacing(300);
  --private-input-date-calendar-area: --spacing(1000);
}

:host([size='sm']) {
  --private-input-date-height: --spacing(700);
  --private-input-date-padding-inline: --spacing(200);
  --private-input-date-calendar-area: --spacing(800);
}

/* Motion, held back.
 *
 * Every component zeroes the private durations its own base CSS reads, which composes
 * with the public knobs - but that only reaches the declarations that read them. An audit
 * of the 52 sheets found 21 transitions in 15 components that did not: the host's own
 * transition in mds-button and mds-table, the focus ring of the input family, the tab
 * slider, the close button of mds-note. This reset reaches the whole shadow tree, which is
 * what the preference promises, and keeps reaching it when a declaration is added.
 *
 * Transitions only, on purpose. A keyframe animation is usually a loader, and a loader has
 * to keep saying "working": mds-spinner slows to 8s under reduced motion instead of
 * freezing, and a duration of nearly zero on an infinite animation would spin it out of
 * control instead of stopping it.
 *
 * 0.01ms rather than 0s, so a component that waits for its own transitionend still gets
 * one: mds-modal, mds-push-notification and mds-tree-item all listen for it, and a
 * transition of zero never fires it. */

/* stylelint-disable time-min-milliseconds -- the floor is there to keep a transition
   perceivable; the point of these two is that it is not. */

:host([pref-animation='reduce']),
:host([pref-animation='reduce']) *,
:host([pref-animation='reduce']) *::before,
:host([pref-animation='reduce']) *::after {
  transition-duration: 0.01ms !important;
}

@media (prefers-reduced-motion: reduce) {
  :host([pref-animation='system']),
  :host([pref-animation='system']) *,
  :host([pref-animation='system']) *::before,
  :host([pref-animation='system']) *::after,
  :host(:not([pref-animation])),
  :host(:not([pref-animation])) *,
  :host(:not([pref-animation])) *::before,
  :host(:not([pref-animation])) *::after {
    transition-duration: 0.01ms !important;
  }
}

/* stylelint-enable time-min-milliseconds */


/* Animation preference. The component mirrors the resolved mode from <html>
 * onto its host as pref-animation (see subscribePreference in @common/preference);
 * these plain shadow selectors then zero the private duration(s) the base CSS
 * reads. `reduce` always stops; `system` and no controller follow the OS via
 * @media; `no-preference` keeps the base value. Customization composes (the base
 * sets --private from the public token, this only overrides it when reduced). */

:host([pref-animation='reduce']) {
  --private-input-date-transition-duration: 0s;
}

@media (prefers-reduced-motion: reduce) {
  :host([pref-animation='system']),
  :host(:not([pref-animation])) {
    --private-input-date-transition-duration: 0s;
  }
}

/* Theme preference. The component reflects the resolved theme mode and scheme
 * from <html> onto its host as pref-mode + pref-theme-scheme (see
 * subscribePreference in @common/preference); these plain shadow selectors refine
 * for dark. `dark` is the explicit choice; `system` and no controller follow the
 * OS via @media; the scheme axis gates it. Cross-browser, no :host-context. */

:host([pref-mode='dark']:not([pref-theme-scheme='light'])),
:host([pref-theme-scheme='dark']) {
  --mds-input-date-ring: var(--shadow-ring-2, 0 0 0 2px rgb(var(--tone-neutral-01, 5 5 5) / 0.3));

  .counter-button:hover {
    --mds-button-background: rgb(var(--magma-wash-strong, var(--tone-neutral-08, 208 208 208)));
  }
}

@media (prefers-color-scheme: dark) {
  :host([pref-mode='system']:not([pref-theme-scheme='light'])),
  :host(:not([pref-mode]):not([pref-theme-scheme='light'])) {
    --mds-input-date-ring: var(--shadow-ring-2, 0 0 0 2px rgb(var(--tone-neutral-01, 5 5 5) / 0.3));

    .counter-button:hover {
      --mds-button-background: rgb(var(--magma-wash-strong, var(--tone-neutral-08, 208 208 208)));
    }
  }
}

/* Contrast preference. The component reflects the resolved mode from <html> onto
 * its host as pref-contrast (see subscribePreference in @common/preference); these
 * plain shadow selectors refine for high contrast. `more` is the explicit choice;
 * `system` and no controller follow the OS via @media. Cross-browser, no :host-context.
 *
 * The placeholder names --magma-text-subtle on purpose: under pref-contrast-more the
 * layer promotes it to the muted step, darker than at rest but still below the
 * value, which text-default carries (text-muted would be promoted to it). */

:host([pref-contrast='more']) {
  --mds-input-date-ring: var(--shadow-ring-strong-2, 0 0 0 2px rgb(var(--tone-neutral-01, 5 5 5) / 0.6));
  --mds-input-date-background: rgb(var(--magma-wash-base, var(--tone-neutral-09, 225 225 225)));

  .input::placeholder {
    color: rgb(var(--magma-text-subtle, var(--text-neutral-subtle, 99 99 99)));
  }
}

@media (prefers-contrast: more) {
  :host([pref-contrast='system']),
  :host(:not([pref-contrast])) {
    --mds-input-date-ring: var(--shadow-ring-strong-2, 0 0 0 2px rgb(var(--tone-neutral-01, 5 5 5) / 0.6));
    --mds-input-date-background: rgb(var(--magma-wash-base, var(--tone-neutral-09, 225 225 225)));

    .input::placeholder {
      color: rgb(var(--magma-text-subtle, var(--text-neutral-subtle, 99 99 99)));
    }
  }
}

/* Firefox paints its own calendar button inside <input type="date"> and offers no pseudo-element
   to hide it (Chromium's ::-webkit-calendar-picker-indicator has no counterpart). The custom
   calendar button stays visible, as in Chromium, and the native one is hidden (#1235). */

@supports (-moz-appearance: none) {

  /* in a range the field is slotted and paints nothing: clip the native button away, and give
     back the room it takes so the fields sit as in Chromium */

  :host([slot]) .input {
    clip-path: inset(0 --spacing(500) 0 0);
    margin-right: calc(-1 * --spacing(500));
  }

  /* standalone: the field paints the background and the ring, so it cannot be clipped. The
     native button sits at its right edge, under the square of the custom one, which is made
     opaque with the background of the field to cover it */

  :host(:not([slot])) .input {
    padding-right: 0;
  }

  :host(:not([slot])) .action-open-calendar-wrapper {
    background-color: var(--mds-input-date-background, rgb(var(--magma-surface-overlay, var(--surface-neutral-overlay, 255 255 255))));
    border-radius: inherit;
    corner-shape: inherit;
  }

  :host([empty]:not([slot])) .action-open-calendar-wrapper {
    background-color: var(--mds-input-date-field-background-empty, rgb(var(--magma-danger-wash-soft, var(--status-error-10, 255 232 230))));
  }

  :host(:not([slot])) .input:disabled ~ .action-open-calendar-wrapper {
    background-color: rgb(var(--magma-wash-soft, var(--tone-neutral-10, 237 237 237)));
  }
}

:host {
  --mds-input-date-icon-color: rgb(var(--magma-accent-emphasis, var(--variant-primary-04, 11 96 255)));
  --mds-input-date-variant-color-rgb: 0 0 0;
}

:host(:focus) {
  --mds-input-date-icon-color: rgb(var(--magma-accent-fg, var(--variant-primary-03, 0 73 211)));
  --mds-input-date-variant-color-rgb: var(--magma-accent-emphasis, var(--variant-primary-04, 11 96 255));
}

:host([variant='ai']) {
  --mds-input-date-icon-color: rgb(var(--magma-accent-ai-emphasis, var(--variant-ai-04, 128 78 232)));
  --mds-input-tip-background: rgb(var(--magma-accent-ai-fg, var(--variant-ai-03, 105 49 204)));
  --mds-input-date-variant-color-rgb: 0 0 0;
}

:host([variant='ai']:focus) {
  --mds-input-date-icon-color: rgb(var(--magma-accent-ai-fg, var(--variant-ai-03, 105 49 204)));
  --mds-input-date-variant-color-rgb: var(--magma-accent-ai-emphasis, var(--variant-ai-04, 128 78 232));
}

:host([variant='info']) {
  --mds-input-date-icon-color: rgb(var(--magma-info-emphasis, var(--status-info-04, 0 119 164)));
  --mds-input-tip-background: rgb(var(--magma-info-emphasis, var(--status-info-04, 0 119 164)));
  --mds-input-date-variant-color-rgb: 0 0 0;
}

:host([variant='info']:focus) {
  --mds-input-date-icon-color: rgb(var(--magma-info-emphasis, var(--status-info-04, 0 119 164)));
  --mds-input-date-variant-color-rgb: var(--magma-info-emphasis, var(--status-info-04, 0 119 164));
}

:host([variant='success']) {
  --mds-input-date-icon-color: rgb(var(--magma-success-emphasis, var(--status-success-04, 0 128 57)));
  --mds-input-tip-background: rgb(var(--magma-success-emphasis, var(--status-success-04, 0 128 57)));
  --mds-input-date-variant-color-rgb: 0 0 0;
}

:host([variant='success']:focus) {
  --mds-input-date-icon-color: rgb(var(--magma-success-emphasis, var(--status-success-04, 0 128 57)));
  --mds-input-date-variant-color-rgb: var(--magma-success-emphasis, var(--status-success-04, 0 128 57));
}

:host([variant='warning']) {
  --mds-input-date-icon-color: rgb(var(--magma-warning-emphasis, var(--status-warning-04, 142 106 0)));
  --mds-input-tip-background: rgb(var(--magma-warning-emphasis, var(--status-warning-04, 142 106 0)));
  --mds-input-date-variant-color-rgb: 0 0 0;
}

:host([variant='warning']:focus) {
  --mds-input-date-icon-color: rgb(var(--magma-warning-emphasis, var(--status-warning-04, 142 106 0)));
  --mds-input-date-variant-color-rgb: var(--magma-warning-emphasis, var(--status-warning-04, 142 106 0));
}

:host([variant='error']) {
  --mds-input-date-icon-color: rgb(var(--magma-danger-emphasis, var(--status-error-04, 196 64 65)));
  --mds-input-tip-background: rgb(var(--magma-danger-emphasis, var(--status-error-04, 196 64 65)));
  --mds-input-date-variant-color-rgb: 0 0 0;
}

:host([variant='error']:focus) {
  --mds-input-date-icon-color: rgb(var(--magma-danger-emphasis, var(--status-error-04, 196 64 65)));
  --mds-input-date-variant-color-rgb: var(--magma-danger-emphasis, var(--status-error-04, 196 64 65));
}

