/* Page Lemon shared controls: text fields, selects (controls.js), checkboxes, radios, switches and the confirm dialog.
   Loaded after the portal's style.css and after the admin's admin.css; written only against the tokens both define
   (--primary, --focus, --ring, --control, --input, --surface, --raised, --line, --line-strong, --alert, --tint…),
   so light and dark come for free. Brand: pagelemon.com's blue for focus and checked states, lemon as a small accent
   (the selected option's mark), DM Sans, 8 px control radius, 40 px controls (44 px touch targets on phones).
   See docs/research/controls-2026-10-14.md. */

:root {
  --ctl-h: 40px;
  --ctl-lemon: var(--soft, var(--lemon-tint, #FFF6CC));
  --ctl-hover: color-mix(in srgb, var(--ink) 55%, transparent);
}

/* ---------- Text fields ---------- */
/* The box (border, radius, height, background) stays each app's own base rule; this adds the states. */

:root:has(#shell) { --ctl-h: 38px; } /* the admin's denser controls */
:is(input:is([type="text"], [type="email"], [type="tel"], [type="password"], [type="url"], [type="number"], [type="search"],
  [type="date"], [type="time"], [type="datetime-local"]), textarea):not([class*="cl-"]) {
  transition: border-color 140ms ease, box-shadow 140ms ease, background-color 140ms ease;
}
:is(input:is([type="text"], [type="email"], [type="tel"], [type="password"], [type="url"], [type="number"], [type="search"],
  [type="date"], [type="time"], [type="datetime-local"]), textarea):not([class*="cl-"]):hover:not(:disabled, [readonly], :focus) {
  border-color: var(--ctl-hover);
}
:is(input:is([type="text"], [type="email"], [type="tel"], [type="password"], [type="url"], [type="number"], [type="search"],
  [type="date"], [type="time"], [type="datetime-local"]), textarea):not([class*="cl-"]):focus-visible {
  outline: none;
  border-color: var(--focus);
  box-shadow: 0 0 0 3px var(--ring);
}
:is(input, textarea)[aria-invalid="true"]:not([class*="cl-"]) { border-color: var(--alert); }
:is(input, textarea)[aria-invalid="true"]:not([class*="cl-"]):focus-visible { border-color: var(--alert); box-shadow: 0 0 0 3px var(--alert-tint); }
:is(input, textarea):disabled:not([type="checkbox"], [type="radio"]) {
  background: var(--raised); color: var(--ink-muted); border-color: var(--line-strong); cursor: not-allowed; opacity: 1;
}
:is(input, textarea)[readonly]:not([type="checkbox"], [type="radio"]) { background: var(--raised); }
textarea:not([class*="cl-"]) { line-height: 1.5; }

/* Label above, hint and error under. */
.field > label, .field > .label { line-height: 1.35; }
.field-error, .hint.bad {
  display: flex; gap: 6px; align-items: flex-start;
  margin: 0; color: var(--alert); font-size: 0.8125rem; font-weight: 500; line-height: 1.4;
}
.field-error::before, .hint.bad::before {
  content: "!"; flex: none; display: inline-grid; place-items: center; width: 15px; height: 15px; margin-top: 1px;
  border-radius: 50%; background: var(--alert); color: var(--on-alert); font: 700 10px/1 var(--font);
}
.field-error[hidden], .hint.bad[hidden] { display: none; }

/* Prefix / suffix: one box that looks like a single input. */
.ctl-affix {
  display: flex; align-items: stretch; min-height: var(--ctl-h); width: 100%; min-width: 0;
  border: 1px solid var(--control); border-radius: var(--r-sm); background: var(--input); overflow: hidden;
  transition: border-color 140ms ease, box-shadow 140ms ease; cursor: text;
}
.ctl-affix:hover:not(:focus-within) { border-color: var(--ctl-hover); }
.ctl-affix:focus-within { border-color: var(--focus); box-shadow: 0 0 0 3px var(--ring); }
.ctl-affix:has(input[aria-invalid="true"]) { border-color: var(--alert); }
.ctl-affix:has(input[aria-invalid="true"]):focus-within { box-shadow: 0 0 0 3px var(--alert-tint); }
.ctl-affix:has(input:disabled), .ctl-affix:has(input[readonly]) { background: var(--raised); border-color: var(--line-strong); }
.ctl-affix > input, .ctl-affix > input:is(:hover, :focus-visible, [aria-invalid="true"], :disabled, [readonly]) {
  flex: 1 1 auto; min-width: 0; border: 0 !important; box-shadow: none !important; outline: none; background: transparent !important; min-height: 0; border-radius: 0;
}
.ctl-affix-text {
  flex: none; display: flex; align-items: center; padding: 0 10px; color: var(--ink-muted); background: var(--raised);
  font-size: 0.875rem; white-space: nowrap; user-select: none;
}
.ctl-affix-text:first-child { border-right: 1px solid var(--line); }
.ctl-affix-text:last-child { border-left: 1px solid var(--line); }
.ctl-affix > input + .ctl-affix-text:last-child { padding-left: 10px; }

/* ---------- Select (controls.js enhanceSelect) ---------- */

:where(.ctl-select) { position: relative; display: inline-block; width: 100%; min-width: 0; vertical-align: middle; }
/* The wrapper carries the select's own classes for width; never its box. */
.ctl-select.ctl-select { border: 0; padding: 0; background: none; box-shadow: none; outline: none; min-height: 0; }
.ctl-native { position: absolute !important; inset: 0; width: 100% !important; height: 100% !important; min-height: 0 !important; opacity: 0; pointer-events: none; }
.ctl-select-btn {
  display: flex; align-items: center; gap: 8px; width: 100%; min-width: 0; min-height: var(--ctl-h); margin: 0;
  padding: 0 10px 0 12px; border: 1px solid var(--control); border-radius: var(--r-sm); background: var(--input);
  color: var(--ink); font: 400 var(--fs-base, 0.9375rem)/1.35 var(--font); text-align: left; cursor: pointer;
  transition: border-color 140ms ease, box-shadow 140ms ease;
}
.ctl-select-btn:hover:not(:disabled) { border-color: var(--ctl-hover); }
.ctl-select-btn:focus-visible, .ctl-select.is-open .ctl-select-btn { outline: none; border-color: var(--focus); box-shadow: 0 0 0 3px var(--ring); }
.ctl-select-btn[aria-invalid="true"] { border-color: var(--alert); }
.ctl-select-btn:disabled { background: var(--raised); color: var(--ink-muted); border-color: var(--line-strong); cursor: not-allowed; }
.ctl-select-value { flex: 1 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ctl-select.is-empty .ctl-select-value { color: var(--ink-muted); }
.ctl-chevron { flex: none; width: 16px; height: 16px; color: var(--ink-muted); transition: transform 160ms ease; }
.ctl-select.is-open .ctl-chevron { transform: rotate(180deg); }
/* Compact selects where the old <select> was compact. */
.lang-picker > .ctl-select-btn { min-height: 32px; padding: 0 6px 0 10px; font-size: var(--fs-sm, 0.8125rem); border-color: var(--line-strong); }
.lang-picker.ctl-select { width: auto; max-width: 170px; }
.nc-tools .ctl-select { width: auto; flex: 0 1 220px; max-width: 240px; }
.nc-tools .ctl-select-btn { min-height: 32px; font-size: var(--fs-sm, 0.8125rem); border-color: var(--line-strong); }
.dt-filter .ctl-select { width: auto; min-width: 140px; }
.dt-filter .ctl-select-btn { min-height: 36px; font-size: 0.875rem; }

/* The list: a popover under (or over) the button; a bottom sheet on phones. */
.ctl-list {
  position: fixed; inset: auto; margin: 0; z-index: 1000; box-sizing: border-box;
  display: flex; flex-direction: column; overflow: hidden; padding: 0;
  color: var(--ink); background: var(--surface); border: 1px solid var(--line-strong); border-radius: var(--r-md);
  box-shadow: var(--shadow); font: 400 var(--fs-md, 0.875rem)/1.35 var(--font);
  animation: ctl-pop 120ms ease-out;
}
:root[data-theme="dark"] .ctl-list { background: var(--raised); }
.ctl-list::backdrop { background: transparent; }
.ctl-listbox { list-style: none; margin: 0; padding: 4px; overflow-y: auto; overscroll-behavior: contain; flex: 1 1 auto; outline: none; }
.ctl-listbox ul { list-style: none; margin: 0; padding: 0; }
.ctl-option {
  position: relative; padding: 8px 12px 8px 32px; border-radius: 7px; cursor: pointer; white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis; user-select: none;
}
.ctl-option.is-active { background: var(--tint); }
.ctl-option[aria-selected="true"] { font-weight: 600; }
.ctl-option[aria-selected="true"]::before {
  content: ""; position: absolute; left: 10px; top: 50%; width: 14px; height: 14px; margin-top: -7px; border-radius: 50%;
  background: var(--field, #FFE45C); box-shadow: inset 0 0 0 1px rgba(30, 29, 25, 0.25);
}
.ctl-option[aria-selected="true"]::after {
  content: ""; position: absolute; left: 14.5px; top: 50%; width: 5px; height: 8px; margin-top: -5.5px;
  border: solid #1E1D19; border-width: 0 1.8px 1.8px 0; transform: rotate(45deg);
}
.ctl-option[aria-disabled="true"] { color: var(--placeholder); cursor: not-allowed; }
.ctl-group-label { padding: 10px 12px 4px; font-size: 0.75rem; font-weight: 600; letter-spacing: 0.04em; text-transform: uppercase; color: var(--ink-muted); }
.ctl-sheet-head { display: none; }
.ctl-list.is-sheet {
  left: 0; right: 0; bottom: 0; top: auto; width: 100%; max-width: none; max-height: min(70vh, 520px);
  border-radius: 18px 18px 0 0; border-width: 1px 0 0; padding-bottom: env(safe-area-inset-bottom, 0px);
  font-size: var(--fs-base, 0.9375rem); animation: ctl-sheet 180ms ease-out;
}
.ctl-list.is-sheet::backdrop { background: var(--backdrop); }
.ctl-list.is-sheet .ctl-sheet-head { display: grid; justify-items: center; gap: 8px; padding: 8px 16px 6px; }
.ctl-list.is-sheet .ctl-sheet-grip { width: 36px; height: 4px; border-radius: 2px; background: var(--line-strong); }
.ctl-list.is-sheet .ctl-sheet-head strong { justify-self: start; font-size: 0.9375rem; }
.ctl-list.is-sheet .ctl-listbox { padding: 4px 8px 12px; }
.ctl-list.is-sheet .ctl-option { min-height: 46px; display: flex; align-items: center; white-space: normal; }
@keyframes ctl-pop { from { opacity: 0; transform: translateY(-4px); } }
@keyframes ctl-sheet { from { transform: translateY(24px); opacity: 0.6; } }

/* ---------- Checkbox, radio, switch ---------- */

input[type="checkbox"]:not([role="switch"], .switch input, [class*="cl-"]), input[type="radio"]:not([class*="cl-"]) {
  -webkit-appearance: none; appearance: none; position: relative; flex: none;
  width: 18px; height: 18px; margin: 2px 0 0; padding: 0;
  border: 1.5px solid var(--control); border-radius: 5px; background: var(--input);
  cursor: pointer; vertical-align: -3px; transition: background-color 120ms ease, border-color 120ms ease, box-shadow 120ms ease;
}
/* Segmented pickers (.seg) hide their inputs; keep them out of the box above. */
.seg input[type="radio"], .seg input[type="checkbox"] { position: absolute; opacity: 0; pointer-events: none; width: 1px; height: 1px; margin: 0; }
input[type="radio"]:not([class*="cl-"]) { border-radius: 50%; }
input[type="checkbox"]:not([role="switch"], .switch input, [class*="cl-"]):hover:not(:disabled, :checked),
input[type="radio"]:not([class*="cl-"]):hover:not(:disabled, :checked) { border-color: var(--ctl-hover); }
input[type="checkbox"]:not([role="switch"], .switch input, [class*="cl-"]):is(:checked, :indeterminate),
input[type="radio"]:not([class*="cl-"]):checked { background: var(--primary); border-color: var(--primary); }
input[type="checkbox"]:not([role="switch"], .switch input, [class*="cl-"]):checked::after {
  content: ""; position: absolute; left: 50%; top: 45%; width: 4.5px; height: 9px;
  border: solid var(--on-primary); border-width: 0 2px 2px 0; transform: translate(-50%, -50%) rotate(45deg);
}
input[type="checkbox"]:not([role="switch"], .switch input, [class*="cl-"]):indeterminate::after {
  content: ""; position: absolute; left: 3px; right: 3px; top: 50%; height: 2px; margin-top: -1px; background: var(--on-primary); border-radius: 1px;
}
input[type="radio"]:not([class*="cl-"]):checked { box-shadow: inset 0 0 0 3.5px var(--input); }
input[type="checkbox"]:not([role="switch"], .switch input, [class*="cl-"]):focus-visible,
input[type="radio"]:not([class*="cl-"]):focus-visible { outline: none; box-shadow: 0 0 0 3px var(--ring); }
input[type="radio"]:not([class*="cl-"]):checked:focus-visible { box-shadow: inset 0 0 0 3.5px var(--input), 0 0 0 3px var(--ring); }
input[type="checkbox"]:not([role="switch"], .switch input, [class*="cl-"]):disabled,
input[type="radio"]:not([class*="cl-"]):disabled { opacity: 0.45; cursor: not-allowed; }
input[type="checkbox"][aria-invalid="true"]:not(:checked) { border-color: var(--alert); }

/* A switch: <input type="checkbox" role="switch">, or any checkbox inside .switch. */
input[type="checkbox"]:is([role="switch"], .switch input) {
  -webkit-appearance: none; appearance: none; position: relative; flex: none;
  width: 36px; height: 20px; margin: 1px 0 0; padding: 0; border: 0; border-radius: 999px;
  background: var(--line-strong); cursor: pointer; transition: background-color 150ms ease;
}
input[type="checkbox"]:is([role="switch"], .switch input)::after {
  content: ""; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; border-radius: 50%;
  background: #fff; box-shadow: 0 1px 2px rgba(0, 0, 0, 0.3); transition: transform 150ms ease;
}
input[type="checkbox"]:is([role="switch"], .switch input):checked { background: var(--primary); }
input[type="checkbox"]:is([role="switch"], .switch input):checked::after { transform: translateX(16px); }
input[type="checkbox"]:is([role="switch"], .switch input):focus-visible { outline: none; box-shadow: 0 0 0 3px var(--ring); }
input[type="checkbox"]:is([role="switch"], .switch input):disabled { opacity: 0.45; cursor: not-allowed; }

/* ---------- Confirm dialog (controls.js confirmModal) ---------- */

dialog.ctl-dialog {
  width: min(480px, calc(100vw - 32px)); max-height: calc(100dvh - 32px); padding: 0; overflow: auto;
  border: 1px solid var(--line-strong); border-radius: 14px; background: var(--surface); color: var(--ink); box-shadow: var(--shadow);
}
:root[data-theme="dark"] dialog.ctl-dialog { background: var(--raised); }
dialog.ctl-dialog[open] { animation: ctl-dlg 140ms ease-out; }
dialog.ctl-dialog::backdrop { background: var(--backdrop); }
.ctl-dialog-form { margin: 0; }
.ctl-dialog-head { display: flex; gap: 12px; align-items: center; padding: 22px 24px 0; }
.ctl-dialog-head h2 { margin: 0; font-size: 1.125rem; line-height: 1.35; font-weight: 650; overflow-wrap: anywhere; }
.ctl-dialog-icon { flex: none; display: grid; place-items: center; width: 36px; height: 36px; border-radius: 50%; background: var(--alert-tint); color: var(--alert); }
.ctl-dialog-icon svg { width: 18px; height: 18px; }
.ctl-dialog-body { display: grid; gap: 14px; padding: 12px 24px 0; }
.ctl-dialog.is-danger .ctl-dialog-body { padding-left: 72px; }
.ctl-dialog-text { display: grid; gap: 8px; color: var(--ink-muted); font-size: 0.9375rem; line-height: 1.5; }
.ctl-dialog-text:empty { display: none; }
.ctl-dialog-text p { margin: 0; }
.ctl-dialog-text ul { margin: 0; padding-left: 18px; display: grid; gap: 4px; }
.ctl-dialog-foot { display: flex; justify-content: flex-end; gap: 8px; flex-wrap: wrap; padding: 22px 24px 22px; }
.ctl-dialog-foot .btn { min-width: 96px; }
.ctl-dialog-foot .btn[aria-busy="true"] { cursor: progress; }
.ctl-typed-text { display: flex; align-items: center; gap: 8px; padding: 6px 6px 6px 12px; border-radius: var(--r-sm); background: var(--ctl-lemon); border: 1px dashed color-mix(in srgb, var(--ink) 25%, transparent); }
.ctl-typed-text code { flex: 1 1 auto; min-width: 0; font: 500 0.875rem/1.4 var(--mono); overflow-wrap: anywhere; color: var(--ink); }
.ctl-typed-input { font-family: var(--mono); }
@keyframes ctl-dlg { from { opacity: 0; transform: translateY(6px) scale(0.98); } }
@media (max-width: 600px) {
  :root { --ctl-h: 44px; }
  .ctl-dialog-head { padding: 20px 20px 0; }
  .ctl-dialog-body, .ctl-dialog.is-danger .ctl-dialog-body { padding: 12px 20px 0; }
  .ctl-dialog-foot { flex-direction: column-reverse; padding: 20px; }
  .ctl-dialog-foot .btn { width: 100%; min-height: 44px; }
}
@media (prefers-reduced-motion: reduce) {
  .ctl-list, .ctl-list.is-sheet, dialog.ctl-dialog[open] { animation: none; }
  .ctl-chevron, input[type="checkbox"]::after { transition: none; }
}
