/* Custom form controls (docs/phases/p6.md, CONTROLS): the look of what controls.js draws, and the CSS-only
   dress for the controls it leaves native (checkboxes, radios, switches, numbers, files).
   Only the tokens of styles.css, so light and dark follow the theme. Loaded after styles.css; a page's own
   <style> comes later still and wins a tie, which is how the page rules that size `.select`/`.input`
   reach the visible controls (they carry the native element's classes). */

:root {
  --ctl-chevron: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23645e73' stroke-width='1.8' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  --ctl-control-line: color-mix(in srgb, var(--muted) 72%, var(--surface));
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --ctl-chevron: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23a39db3' stroke-width='1.8' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  }
}
:root[data-theme="dark"] {
  --ctl-chevron: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23a39db3' stroke-width='1.8' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
}

/* The native element behind an enhanced control: still in the DOM, still submitted, still what
   select_option, fill and the browser's validation see, but 1x1 and out of the way. !important because an
   inline style on the native (max-width:140px) or a page rule on its classes would re-inflate the box. */
.ctl-native[aria-hidden="true"] {
  position: absolute !important;
  width: 1px !important;
  height: 1px !important;
  min-width: 0 !important;
  max-width: none !important;
  min-height: 0 !important;
  margin: -1px !important;
  padding: 0 !important;
  border: 0 !important;
  opacity: 0 !important;
  pointer-events: none !important;
  overflow: hidden !important;
  clip-path: inset(50%) !important;
  white-space: nowrap !important;
}

/* The closed look of a select: the product's chevron rather than the platform's arrow. For the native
   element (server pages before the module runs, and opted-out selects); the trigger draws its own. */
.select:not([multiple]):not([size]) {
  appearance: none;
  -webkit-appearance: none;
  padding-right: 34px;
  background-image: var(--ctl-chevron);
  background-repeat: no-repeat;
  background-position: right 11px center;
  background-size: 16px 16px;
  cursor: pointer;
}

/* -- triggers: the buttons a select or a date becomes ---------------------------------------------- */

:where(.ctl-trigger) {
  width: 100%;
  min-height: 40px;
  padding: 9px 11px;
  border: 1px solid var(--line-strong);
  border-radius: 9px;
  background: var(--surface);
  color: var(--ink);
  font: 13px var(--font);
}
.ctl-trigger {
  display: flex;
  align-items: center;
  gap: 8px;
  text-align: left;
  line-height: 1.35;
  cursor: pointer;
}
/* The trigger keeps `.select` for the page's sizing rules, but draws the chevron itself, inline. */
.ctl-trigger.ctl-select:not([size]) { background-image: none; padding-right: 10px; }
.ctl-value { flex: 1 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ctl-icon { width: 16px; height: 16px; flex: 0 0 auto; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.ctl-chevron { color: var(--muted); transition: transform .15s ease; }
.ctl-trigger[aria-expanded="true"] .ctl-chevron { transform: rotate(180deg); }
.ctl-date-icon { color: var(--muted); }
/* Muted, not the placeholder grey: --muted clears 4.5:1 on the surface in both themes. */
.ctl-date.is-empty .ctl-value { color: var(--muted); }
.ctl-trigger:hover:not(:disabled) { border-color: color-mix(in srgb, var(--accent) 45%, var(--line-strong)); }
.ctl-trigger:focus { outline: none; }
.ctl-trigger:focus-visible,
.ctl-trigger[aria-expanded="true"] { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.ctl-trigger:disabled { opacity: .55; cursor: not-allowed; }
.ctl-trigger[aria-invalid="true"], .ctl-chips:has([aria-invalid="true"]), .ctl-time[aria-invalid="true"] { border-color: var(--red); }

/* -- popups: in the top layer (Popover API), fixed to their trigger --------------------------------- */

/* Never `display` without :popover-open: an author `display` beats the UA rule that hides a closed popover. */
.ctl-pop {
  position: fixed;
  inset: auto;
  margin: 0;
  box-sizing: border-box;
  min-width: 180px;
  max-width: min(420px, calc(100vw - 16px));
  max-height: min(340px, calc(100vh - 16px));
  padding: 5px;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 11px;
  background: var(--surface);
  color: var(--ink);
  box-shadow: var(--shadow-md);
  font: 13px/1.4 var(--font);
  text-align: left;
}
.ctl-pop:popover-open { display: flex; flex-direction: column; }
/* Browsers without the Popover API: a plain fixed layer (not above a modal dialog's top layer). */
.ctl-pop.ctl-pop-fallback { display: flex; flex-direction: column; z-index: 1000; }
.ctl-pop::backdrop { background: transparent; }

.ctl-search {
  flex: 0 0 auto;
  width: 100%;
  min-height: 34px;
  margin: 0 0 5px;
  padding: 6px 9px;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  background: var(--surface);
  color: var(--ink);
  font: 13px var(--font);
}
.ctl-search:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.ctl-listbox { flex: 1 1 auto; min-height: 0; overflow-y: auto; overscroll-behavior: contain; scrollbar-width: thin; scrollbar-color: var(--line-strong) transparent; }
.ctl-option {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  min-height: 32px;
  padding: 6px 9px;
  border-radius: 7px;
  color: var(--ink);
  cursor: pointer;
  user-select: none;
}
/* The row is the hit target: elementFromPoint over an option is the option. */
.ctl-option > * { pointer-events: none; }
.ctl-option-text { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ctl-option-note { margin-left: auto; color: var(--muted); font-size: 11.5px; white-space: nowrap; }
.ctl-option.is-active { background: var(--grey-soft); }
.ctl-option[aria-selected="true"] { color: var(--accent-dark); font-weight: 650; }
.ctl-option[aria-selected="true"].is-active { background: var(--accent-soft); }
.ctl-option[aria-disabled="true"] { opacity: .5; cursor: not-allowed; }
.ctl-tick { width: 14px; height: 14px; color: var(--accent); visibility: hidden; }
.ctl-option[aria-selected="true"] .ctl-tick { visibility: visible; }
/* A datalist's suggestions are proposals, not a value: no tick, and the cursor row is plain. */
.ctl-option-note + .ctl-tick { display: none; }
.ctl-group-label { padding: 8px 9px 3px; color: var(--muted); font-size: 10.5px; font-weight: 750; letter-spacing: .06em; text-transform: uppercase; }
.ctl-empty { margin: 0; padding: 8px 9px; color: var(--muted); font-size: 12px; }

/* -- the calendar --------------------------------------------------------------------------------- */

.ctl-pop.ctl-cal { width: min(300px, calc(100vw - 16px)); max-height: none; min-width: 0; padding: 10px; overflow: visible; }
.ctl-cal-body { display: grid; gap: 8px; }
.ctl-cal-head { display: grid; grid-template-columns: 28px 28px 1fr 28px 28px; align-items: center; gap: 2px; }
.ctl-cal-nav {
  height: 28px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  color: var(--muted);
  cursor: pointer;
}
.ctl-cal-nav:hover { color: var(--ink); background: var(--grey-soft); }
.ctl-nav-icon { width: 15px; height: 15px; }
.ctl-cal-caption { color: var(--ink); font-size: 12.5px; font-weight: 700; text-align: center; }
.ctl-cal-weekdays { display: grid; grid-template-columns: repeat(7, 1fr); color: var(--muted); font-size: 9.5px; font-weight: 750; letter-spacing: .06em; text-align: center; text-transform: uppercase; }
.ctl-cal-grid { display: grid; gap: 2px; }
.ctl-week { display: grid; grid-template-columns: repeat(7, 1fr); gap: 2px; }
.ctl-day {
  min-height: 34px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  color: var(--ink);
  font-size: 12.5px;
  font-variant-numeric: tabular-nums;
  cursor: pointer;
  user-select: none;
}
/* Neighbouring months' days are dimmed, not blanked, so the weeks still read as weeks. */
.ctl-day.is-out { color: var(--muted); }
/* A ring, not a fill: filled reads as chosen. */
.ctl-day.is-today { box-shadow: inset 0 0 0 1px var(--line-strong); font-weight: 700; }
.ctl-day:hover, .ctl-day.is-active { background: var(--grey-soft); }
.ctl-day.is-active { box-shadow: inset 0 0 0 2px var(--accent); }
.ctl-day[aria-selected="true"] { color: var(--on-accent); background: var(--accent); font-weight: 700; }
.ctl-day[aria-selected="true"].is-active { background: var(--accent-dark); box-shadow: inset 0 0 0 2px var(--accent-dark); }
.ctl-day[aria-disabled="true"] { opacity: .35; cursor: not-allowed; }
.ctl-day[aria-disabled="true"]:hover { background: transparent; }
.ctl-cal-time { padding-top: 8px; border-top: 1px solid var(--line); }
.ctl-cal-time-row { display: flex; align-items: center; gap: 4px; }
.ctl-cal-caption-time { margin-right: auto; color: var(--muted); font-size: 11px; font-weight: 700; }
.ctl-cal-seg {
  min-width: 40px;
  min-height: 32px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  font-size: 13px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  cursor: pointer;
}
.ctl-cal-seg:hover { background: var(--grey-soft); }
.ctl-cal-seg.is-active { border-color: var(--accent); background: var(--accent-soft); color: var(--accent-dark); }
.ctl-cal-sep { color: var(--muted); font-weight: 700; }
.ctl-cal-foot { padding-top: 8px; display: flex; flex-wrap: wrap; align-items: center; gap: 6px; border-top: 1px solid var(--line); }
.ctl-cal-action {
  min-height: 28px;
  padding: 0 10px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  color: var(--accent-dark);
  font-size: 11.5px;
  font-weight: 700;
  cursor: pointer;
}
.ctl-cal-action:hover { background: var(--accent-soft); }
.ctl-cal-hint { flex: 1 0 100%; margin: 0; color: var(--muted); font-size: 10px; text-align: center; }
.ctl-live { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
@media (max-width: 680px) { .ctl-day { min-height: 32px; } }

/* -- chips ------------------------------------------------------------------------------------------ */

.ctl-chips {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  width: 100%;
  min-height: 40px;
  padding: 5px 8px;
  border: 1px solid var(--line-strong);
  border-radius: 9px;
  background: var(--surface);
  color: var(--ink);
  font: 13px var(--font);
  cursor: text;
}
.ctl-chips:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.ctl-chips.is-disabled { opacity: .55; cursor: not-allowed; }
.ctl-chip-list { display: contents; }
.ctl-chip {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  max-width: 100%;
  min-height: 26px;
  padding: 1px 1px 1px 10px;
  border-radius: 99px;
  background: var(--accent-soft);
  color: var(--accent-dark);
  font-size: 12px;
  font-weight: 650;
}
.ctl-chip-text { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ctl-chip-x {
  width: 24px;
  height: 24px;
  display: grid;
  place-items: center;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: inherit;
  cursor: pointer;
}
.ctl-chip-x:hover { background: color-mix(in srgb, currentColor 16%, transparent); }
.ctl-chip-x:focus-visible { outline-offset: 0; border-radius: 50%; }
.ctl-chip-icon { width: 13px; height: 13px; }
.ctl-chips-input {
  flex: 1 1 120px;
  min-width: 80px;
  min-height: 28px;
  padding: 2px 3px;
  border: 0;
  outline: none;
  background: transparent;
  color: var(--ink);
  font: 13px var(--font);
}

/* -- time: two typed segments ------------------------------------------------------------------------ */

:where(.ctl-time) {
  width: 100%;
  min-height: 40px;
  padding: 9px 11px;
  border: 1px solid var(--line-strong);
  border-radius: 9px;
  background: var(--surface);
  color: var(--ink);
  font: 13px var(--font);
}
.ctl-time { display: inline-flex; align-items: center; gap: 1px; cursor: text; font-variant-numeric: tabular-nums; }
.ctl-time:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.ctl-time.is-disabled { opacity: .55; cursor: not-allowed; }
.ctl-seg { min-width: 2.4ch; padding: 1px 3px; border-radius: 5px; text-align: center; outline: none; caret-color: transparent; }
.ctl-seg.is-empty { color: var(--muted); }
.ctl-seg:focus { color: var(--on-accent); background: var(--accent); }
.ctl-seg-sep { color: var(--muted); }

/* -- CSS only: checkbox, radio, switch, number, file ------------------------------------------------- */

/* Drawn rather than accent-coloured, so the box matches the product in both themes. The border is mixed
   from --muted for 3:1 against the surface (a control's edge has to be seen). Equal specificity with a
   page's `.foo input` rule, which comes later and wins: pages that hide a radio behind a tile keep it hidden. */
input[type="checkbox"], input[type="radio"] {
  appearance: none;
  -webkit-appearance: none;
  width: 16px;
  height: 16px;
  margin: 0;
  flex: 0 0 auto;
  display: inline-grid;
  place-content: center;
  vertical-align: -3px;
  border: 1.5px solid var(--ctl-control-line);
  border-radius: 4px;
  background: var(--surface);
  color: var(--on-accent);
  cursor: pointer;
  transition: background-color .12s ease, border-color .12s ease;
}
input[type="radio"], input[type="radio"]:focus-visible { border-radius: 50%; }
input[type="checkbox"]::before {
  content: "";
  width: 10px;
  height: 10px;
  background: currentColor;
  -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 12'%3E%3Cpath d='M2.2 6.3l2.4 2.4 5.2-5.4' fill='none' stroke='%23000' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") center / contain no-repeat;
  mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 12'%3E%3Cpath d='M2.2 6.3l2.4 2.4 5.2-5.4' fill='none' stroke='%23000' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") center / contain no-repeat;
  transform: scale(0);
  transition: transform .12s ease;
}
input[type="checkbox"]:indeterminate::before {
  -webkit-mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 12'%3E%3Cpath d='M2.5 6h7' fill='none' stroke='%23000' stroke-width='2' stroke-linecap='round'/%3E%3C/svg%3E");
  mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 12'%3E%3Cpath d='M2.5 6h7' fill='none' stroke='%23000' stroke-width='2' stroke-linecap='round'/%3E%3C/svg%3E");
}
input[type="checkbox"]:checked, input[type="checkbox"]:indeterminate, input[type="radio"]:checked { border-color: var(--accent); background-color: var(--accent); }
input[type="checkbox"]:checked::before, input[type="checkbox"]:indeterminate::before { transform: scale(1); }
input[type="radio"]:checked { box-shadow: inset 0 0 0 3px var(--surface); }
input[type="checkbox"]:hover:not(:disabled), input[type="radio"]:hover:not(:disabled) { border-color: var(--accent); }
input[type="checkbox"]:disabled, input[type="radio"]:disabled { opacity: .5; cursor: not-allowed; }

/* The switch: `<input type="checkbox" role="switch">`, 34x20, the knob a pseudo-element. */
input[type="checkbox"][role="switch"] {
  width: 34px;
  height: 20px;
  place-content: center;
  border: 0;
  border-radius: 99px;
  background-color: var(--ctl-control-line);
}
input[type="checkbox"][role="switch"]:focus-visible { border-radius: 99px; }
input[type="checkbox"][role="switch"]::before {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: #fff;
  -webkit-mask: none;
  mask: none;
  box-shadow: 0 1px 2px rgba(0, 0, 0, .25);
  transform: translateX(-7px);
}
input[type="checkbox"][role="switch"]:checked { background-color: var(--accent); }
input[type="checkbox"][role="switch"]:checked::before { transform: translateX(7px); }
input[type="checkbox"][role="switch"]:hover:not(:disabled) { background-color: color-mix(in srgb, var(--ctl-control-line) 70%, var(--accent)); }
input[type="checkbox"][role="switch"]:checked:hover:not(:disabled) { background-color: var(--accent-dark); }

/* Numbers stay typeable; only the stepper the platform drew inside the field goes. */
input[type="number"] { appearance: textfield; -moz-appearance: textfield; }
input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }

/* The file picker's button, as .btn-secondary: a pseudo-element can't take a class, so it's restated. */
.input[type="file"] { padding: 4px 11px 4px 4px; line-height: 30px; cursor: pointer; }
input[type="file"]::file-selector-button {
  height: 30px;
  margin: 0 10px 0 0;
  padding: 0 11px;
  vertical-align: top;
  border: 1px solid var(--line-strong);
  border-radius: 7px;
  background: var(--surface);
  color: var(--ink);
  font: 600 12px var(--font);
  cursor: pointer;
  transition: background-color .12s ease;
}
input[type="file"]:not(:disabled):hover::file-selector-button { background: var(--surface-soft); }
input[type="file"]:disabled { cursor: not-allowed; opacity: .55; }
input[type="file"]:disabled::file-selector-button { cursor: not-allowed; }

/* styles.css turns transitions off for `*`, which doesn't reach pseudo-elements. */
@media (prefers-reduced-motion: reduce) {
  input[type="checkbox"]::before, input[type="file"]::file-selector-button { transition: none; }
}

/* A visible control hidden with its native element: author `display` (flex, grid) would beat the UA's
   [hidden] rule, and server pages don't all load styles.css, which restates it. */
.ctl-trigger[hidden], .ctl-chips[hidden], .ctl-time[hidden] { display: none !important; }

/* -- forced colors (Windows High Contrast) ------------------------------------------------------------
   The browser replaces every colour with the system palette and drops box-shadows, so each state drawn
   above with a fill or a shadow (a ticked box, the cursor row, the selected day, a focus ring) would read
   as nothing. Here each one gets an outline or a system colour pair. The ones that fill are exempted from
   the forcing (forced-color-adjust: none), so Highlight/HighlightText survive as written. */
@media (forced-colors: active) {
  /* The platform's own boxes: they draw checked, mixed and disabled in the system's colours. */
  input[type="checkbox"], input[type="radio"] { appearance: auto; -webkit-appearance: auto; border: 0; background: none; box-shadow: none; }
  input[type="checkbox"]::before { content: none; }

  .ctl-trigger:focus-visible, .ctl-trigger[aria-expanded="true"],
  .ctl-chips:focus-within, .ctl-time:focus-within, .ctl-search:focus { outline: 2px solid Highlight; outline-offset: 1px; }
  .ctl-trigger[aria-invalid="true"], .ctl-chips:has([aria-invalid="true"]), .ctl-time[aria-invalid="true"] { border-style: dashed; border-width: 2px; }

  /* Before the cursor's rule, which wins on today itself. */
  .ctl-day.is-today { outline: 1px dashed CanvasText; outline-offset: -3px; }
  .ctl-option.is-active, .ctl-day.is-active, .ctl-cal-seg.is-active { outline: 2px solid Highlight; outline-offset: -2px; }
  .ctl-option[aria-selected="true"], .ctl-day[aria-selected="true"], .ctl-seg:focus {
    forced-color-adjust: none;
    background: Highlight;
    color: HighlightText;
  }
  .ctl-option[aria-selected="true"].is-active, .ctl-day[aria-selected="true"].is-active { outline-color: CanvasText; }
  .ctl-option[aria-disabled="true"], .ctl-day[aria-disabled="true"] { color: GrayText; }
  .ctl-day.is-out { color: GrayText; }
  .ctl-tick { color: inherit; }
  .ctl-chip { border: 1px solid CanvasText; }
}
