/*
  Contrast audit layer.
  Goal: guarantee readable foregrounds for controls in light/dark and in interactive states
  without overwriting semantic backgrounds such as sales-funnel statuses or the theme toggle.
*/

:root,
html[data-theme="dark"],
html[data-theme="light"]{
  --audit-btn-text: var(--theme-button-text);
  --audit-input-text: var(--theme-text);
  --audit-input-placeholder: var(--theme-muted);
  --audit-menu-bg: var(--theme-menu-bg);
  --audit-menu-text: var(--theme-menu-text);
  --audit-menu-hover-bg: var(--theme-menu-hover-bg);
  --audit-menu-hover-text: var(--theme-menu-hover-text);
  --audit-menu-active-bg: var(--theme-menu-active-bg);
  --audit-menu-active-text: var(--theme-menu-active-text);
  --audit-danger: var(--theme-danger);
  --audit-danger-hover-bg: var(--theme-menu-danger-hover-bg);
  --audit-focus-ring: rgba(106,166,255,.58);
}

/* -------- Neutral buttons / button-like controls -------- */
:is(
  button,
  .btn,
  .btn-mini,
  .btn-small,
  .file-btn,
  .composer-file-btn,
  .compact-btn,
  .chatmenu-btn,
  .assign-btn,
  .save-assign,
  .crm-btn,
  .crm-btn-mini,
  .crm-icon-btn,
  .cal-btn,
  .cal-btn-small,
  .smallbtn,
  .ti-btn,
  .ti-file-btn,
  .mobile-nav-toggle,
  .mobile-nav-close,
  .wiz-btn,
  .menu-trigger,
  .tick-btn,
  .detail-btn,
  .secondary,
  .primary
)
:not(.theme-toggle)
:not(.theme-target-dark)
:not(.theme-target-light)
:not(.crm-sales-status)
:not(.status-pill)
:not(.danger)
:not(.danger-btn)
:not(.crm-btn-danger)
:not(.flow-node-complete-btn)
:not(.flow-node-delete-btn),
:is(
  button,
  .btn,
  .btn-mini,
  .btn-small,
  .file-btn,
  .composer-file-btn,
  .compact-btn,
  .chatmenu-btn,
  .assign-btn,
  .save-assign,
  .crm-btn,
  .crm-btn-mini,
  .crm-icon-btn,
  .cal-btn,
  .cal-btn-small,
  .smallbtn,
  .ti-btn,
  .ti-file-btn,
  .mobile-nav-toggle,
  .mobile-nav-close,
  .wiz-btn,
  .menu-trigger,
  .tick-btn,
  .detail-btn,
  .secondary,
  .primary
)
:not(.theme-toggle)
:not(.theme-target-dark)
:not(.theme-target-light)
:not(.crm-sales-status)
:not(.status-pill)
:not(.danger)
:not(.danger-btn)
:not(.crm-btn-danger)
:not(.flow-node-complete-btn)
:not(.flow-node-delete-btn):is(:hover,:focus,:focus-visible,:active,[aria-pressed="true"],[aria-current="page"],.is-active,.active){
  color: var(--audit-btn-text) !important;
  -webkit-text-fill-color: var(--audit-btn-text) !important;
  text-shadow: none !important;
}

:is(
  .btn,
  .btn-mini,
  .btn-small,
  .file-btn,
  .composer-file-btn,
  .compact-btn,
  .chatmenu-btn,
  .assign-btn,
  .save-assign,
  .crm-btn,
  .crm-btn-mini,
  .crm-icon-btn,
  .cal-btn,
  .cal-btn-small,
  .smallbtn,
  .ti-btn,
  .ti-file-btn,
  .mobile-nav-toggle,
  .mobile-nav-close,
  .wiz-btn,
  .menu-trigger,
  .tick-btn,
  .detail-btn,
  .secondary,
  .primary,
  button
)
:not(.theme-toggle)
:not(.crm-sales-status)
:not(.status-pill)
:not(.danger)
:not(.danger-btn)
:not(.crm-btn-danger)
:not(.flow-node-complete-btn)
:not(.flow-node-delete-btn) :is(svg,path,circle,rect,polygon,line,polyline,ellipse),
:is(
  .btn,
  .btn-mini,
  .btn-small,
  .file-btn,
  .composer-file-btn,
  .compact-btn,
  .chatmenu-btn,
  .assign-btn,
  .save-assign,
  .crm-btn,
  .crm-btn-mini,
  .crm-icon-btn,
  .cal-btn,
  .cal-btn-small,
  .smallbtn,
  .ti-btn,
  .ti-file-btn,
  .mobile-nav-toggle,
  .mobile-nav-close,
  .wiz-btn,
  .menu-trigger,
  .tick-btn,
  .detail-btn,
  .secondary,
  .primary,
  button
)
:not(.theme-toggle)
:not(.crm-sales-status)
:not(.status-pill)
:not(.danger)
:not(.danger-btn)
:not(.crm-btn-danger)
:not(.flow-node-complete-btn)
:not(.flow-node-delete-btn):is(:hover,:focus,:focus-visible,:active,[aria-pressed="true"],[aria-current="page"],.is-active,.active) :is(svg,path,circle,rect,polygon,line,polyline,ellipse){
  fill: currentColor !important;
  stroke: currentColor !important;
}

:is(
  .btn,
  .btn-mini,
  .btn-small,
  .file-btn,
  .composer-file-btn,
  .compact-btn,
  .chatmenu-btn,
  .assign-btn,
  .save-assign,
  .crm-btn,
  .crm-btn-mini,
  .crm-icon-btn,
  .cal-btn,
  .cal-btn-small,
  .smallbtn,
  .ti-btn,
  .ti-file-btn,
  .mobile-nav-toggle,
  .mobile-nav-close,
  .wiz-btn,
  .menu-trigger,
  .tick-btn,
  .detail-btn,
  .secondary,
  .primary,
  button
)
:not(.theme-toggle)
:not(.crm-sales-status)
:not(.status-pill):is(:disabled,[aria-disabled="true"],.is-disabled){
  color: var(--audit-input-placeholder) !important;
  -webkit-text-fill-color: var(--audit-input-placeholder) !important;
  text-shadow: none !important;
}

/* Keep semantic buttons readable without changing their backgrounds */
:is(.danger,.danger-btn,.crm-btn-danger,.flow-node-delete-btn,.flow-node-complete-btn){
  -webkit-text-fill-color: currentColor !important;
  text-shadow: none !important;
}
:is(.danger,.danger-btn,.crm-btn-danger,.flow-node-delete-btn,.flow-node-complete-btn) :is(svg,path,circle,rect,polygon,line,polyline,ellipse){
  fill: currentColor !important;
  stroke: currentColor !important;
}

/* Tabs / active chips: preserve existing backgrounds, only enforce readable text */
:is(.tab,.wiz-step,.process-chip,.pill)
:is(:hover,:focus,:focus-visible,:active,.active,.is-current,.is-done,[aria-current="page"],.selected){
  -webkit-text-fill-color: currentColor !important;
  text-shadow: none !important;
}

/* -------- Menus / popovers -------- */
:is(
  .chatmenu-pop,
  .ti-thread-menu-pop,
  .row-menu-pop,
  .crm-kebab-menu,
  .assign-pop,
  [role="menu"],
  [role="listbox"]
){
  color: var(--audit-menu-text) !important;
  -webkit-text-fill-color: var(--audit-menu-text) !important;
}

:is(
  .chatmenu-pop button,
  .ti-thread-menu-pop button,
  .crm-kebab-item,
  .row-menu-pop .menu-item,
  .assign-pop button,
  [role="menuitem"],
  [role="option"]
),
:is(
  .chatmenu-pop button,
  .ti-thread-menu-pop button,
  .crm-kebab-item,
  .row-menu-pop .menu-item,
  .assign-pop button,
  [role="menuitem"],
  [role="option"]
):not(.danger):not(.danger-btn):not(.crm-btn-danger):not(.flow-node-delete-btn):is(:hover,:focus,:focus-visible,:active,[aria-selected="true"],[aria-current="page"],.active,.selected){
  color: var(--audit-menu-text) !important;
  -webkit-text-fill-color: var(--audit-menu-text) !important;
  text-shadow: none !important;
}

:is(
  .chatmenu-pop button,
  .ti-thread-menu-pop button,
  .crm-kebab-item,
  .row-menu-pop .menu-item,
  .assign-pop button,
  [role="menuitem"],
  [role="option"]
):is(:hover,:focus,:focus-visible,:active,[aria-selected="true"],[aria-current="page"],.active,.selected):not(.danger):not(.danger-btn):not(.crm-btn-danger):not(.flow-node-delete-btn){
  background: var(--audit-menu-hover-bg) !important;
  background-color: var(--audit-menu-hover-bg) !important;
  background-image: none !important;
  color: var(--audit-menu-hover-text) !important;
  -webkit-text-fill-color: var(--audit-menu-hover-text) !important;
}

:is(
  .chatmenu-pop button.danger,
  .ti-thread-menu-pop button.danger,
  .crm-kebab-item.danger,
  .row-menu-pop .menu-item.danger,
  .assign-pop .danger,
  .assign-pop .danger-btn,
  [role="menuitem"].danger,
  [role="option"].danger
),
:is(
  .chatmenu-pop button.danger,
  .ti-thread-menu-pop button.danger,
  .crm-kebab-item.danger,
  .row-menu-pop .menu-item.danger,
  .assign-pop .danger,
  .assign-pop .danger-btn,
  [role="menuitem"].danger,
  [role="option"].danger
):is(:hover,:focus,:focus-visible,:active,[aria-selected="true"],.active,.selected){
  color: var(--audit-danger) !important;
  -webkit-text-fill-color: var(--audit-danger) !important;
  text-shadow: none !important;
}

:is(
  .chatmenu-pop button.danger,
  .ti-thread-menu-pop button.danger,
  .crm-kebab-item.danger,
  .row-menu-pop .menu-item.danger,
  .assign-pop .danger,
  .assign-pop .danger-btn,
  [role="menuitem"].danger,
  [role="option"].danger
):is(:hover,:focus,:focus-visible,:active,[aria-selected="true"],.active,.selected){
  background: var(--audit-danger-hover-bg) !important;
  background-color: var(--audit-danger-hover-bg) !important;
  background-image: none !important;
}

/* -------- Inputs / selects / textareas -------- */
:is(
  input,
  select,
  textarea,
  .crm-input,
  .crm-select,
  .crm-textarea,
  .wiz-field input,
  .wiz-field select,
  .wiz-field textarea,
  .lead-form input,
  .lead-form select,
  .lead-form textarea,
  .webchat-start-card input,
  .webchat-start-card select,
  .webchat-start-card textarea,
  .assign-pop select,
  .form-grid select,
  .cal-filter select
):not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="color"]),
:is(
  input,
  select,
  textarea,
  .crm-input,
  .crm-select,
  .crm-textarea,
  .wiz-field input,
  .wiz-field select,
  .wiz-field textarea,
  .lead-form input,
  .lead-form select,
  .lead-form textarea,
  .webchat-start-card input,
  .webchat-start-card select,
  .webchat-start-card textarea,
  .assign-pop select,
  .form-grid select,
  .cal-filter select
):not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="color"]):is(:hover,:focus,:focus-visible,:active){
  color: var(--audit-input-text) !important;
  -webkit-text-fill-color: var(--audit-input-text) !important;
  caret-color: var(--audit-input-text) !important;
  text-shadow: none !important;
}

:is(
  input,
  textarea,
  .crm-input,
  .crm-textarea,
  .wiz-field input,
  .wiz-field textarea,
  .lead-form input,
  .lead-form textarea,
  .webchat-start-card input,
  .webchat-start-card textarea
)::placeholder{
  color: var(--audit-input-placeholder) !important;
  -webkit-text-fill-color: var(--audit-input-placeholder) !important;
  opacity: 1 !important;
}

:is(
  select option,
  select optgroup,
  .crm-select option,
  .crm-select optgroup,
  .assign-pop select option,
  .assign-pop select optgroup,
  .form-grid select option,
  .form-grid select optgroup,
  .cal-filter select option,
  .cal-filter select optgroup
){
  background: var(--audit-menu-bg) !important;
  color: var(--audit-menu-text) !important;
}

:is(
  select option,
  .crm-select option,
  .assign-pop select option,
  .form-grid select option,
  .cal-filter select option
):is(:hover,:focus,:active,:checked,[selected]),
:is(
  select optgroup option,
  .crm-select optgroup option,
  .assign-pop select optgroup option,
  .form-grid select optgroup option,
  .cal-filter select optgroup option
):is(:hover,:focus,:active,:checked,[selected]){
  background: var(--audit-menu-active-bg) !important;
  color: var(--audit-menu-active-text) !important;
}

:is(
  select:disabled,
  textarea:disabled,
  input:disabled,
  .crm-select:disabled,
  .crm-input:disabled,
  .crm-textarea:disabled,
  .assign-pop select:disabled,
  .form-grid select:disabled,
  .cal-filter select:disabled
){
  color: var(--audit-input-placeholder) !important;
  -webkit-text-fill-color: var(--audit-input-placeholder) !important;
}

/* Focus ring without changing semantic fills */
:is(
  button,
  .btn,
  .btn-mini,
  .btn-small,
  .file-btn,
  .composer-file-btn,
  .compact-btn,
  .chatmenu-btn,
  .assign-btn,
  .save-assign,
  .crm-btn,
  .crm-btn-mini,
  .crm-icon-btn,
  .cal-btn,
  .cal-btn-small,
  .smallbtn,
  .ti-btn,
  .ti-file-btn,
  .wiz-btn,
  .menu-trigger,
  .tick-btn,
  .detail-btn,
  .secondary,
  .primary,
  input,
  select,
  textarea,
  .crm-input,
  .crm-select,
  .crm-textarea
):not(.theme-toggle):focus-visible{
  outline: 2px solid var(--audit-focus-ring) !important;
  outline-offset: 1px !important;
}

/* -------- Wizard blink fallback -------- */
:is(.wiz-blink-target,[data-wiz-blink]),
:is(.wiz-blink-target,[data-wiz-blink]) *{
  -webkit-text-fill-color: currentColor !important;
  text-shadow: none !important;
}
:is(.wiz-blink-target,[data-wiz-blink]) :is(svg,path,circle,rect,polygon,line,polyline,ellipse){
  fill: currentColor !important;
  stroke: currentColor !important;
}
