/**
 * @file
 * Layout of the Klaro! notice, "Cookie settings" dialog and embed
 * placeholders, for every theme, following the ICALEO Design System consent
 * board (ConsentBanner, the Cookie settings dialog, ConsentPlaceholder).
 *
 * Only structure: positions, sizes, order and spacing. Colours, type and
 * button looks belong to each theme (icaleo_ds and ilsc_ds map them to their
 * design-system tokens; the Bootstrap themes keep Klaro!'s own).
 *
 * - Banner: text on the left, "Customize", "Necessary only" and "Accept all"
 *   on the right, one baseline; on phones the two choices side by side and
 *   "Customize" below. It never blocks the page.
 * - Dialog: header and footer stay in view and the list scrolls. Each purpose
 *   is a row with its switch at the right and an On / Off / Some label, so
 *   the state does not rely on colour; Necessary shows "Always on" instead of
 *   a switch. Its services open beneath it (consent-ui.js labels the toggle).
 *   Footer: "Save choices" on the left, the two choices on the right.
 * - Klaro!'s Drupal stylesheet gives every span in the dialog "width: auto"
 *   and positions the switch absolutely; Web Awesome (the design-system
 *   themes) makes every <label> "position: relative". Both are undone here.
 *
 * Scoped under #klaro, the module's fixed container, because Klaro!'s
 * selectors go up to six classes deep; placeholders render inline, outside
 * #klaro, and are scoped under body.
 *
 * @see js/consent-ui.js
 * @see web/modules/contrib/klaro/css/klaro-override.css
 * @see \Drupal\lia_consent\Hook\LiaConsentHooks::pageAttachmentsAlter()
 */

/* ---------- Banner ---------- */

#klaro .klaro .cookie-notice:not(.cookie-modal-notice) {
  right: 1rem;
  bottom: 1rem;
  left: 1rem;
  width: auto;
  max-width: 80rem;
  margin: 0 auto;
}

#klaro .klaro .cookie-notice:not(.cookie-modal-notice) .cn-body {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 0.25rem 2rem;
  padding: 1.5rem 2rem;
}

#klaro .klaro .cookie-notice:not(.cookie-modal-notice) .cn-body > h2,
#klaro .klaro .cookie-notice:not(.cookie-modal-notice) .cn-body > p {
  grid-column: 1;
  max-width: 72ch;
  margin: 0;
}

#klaro .klaro .cookie-notice:not(.cookie-modal-notice) .cn-ok {
  grid-column: 2;
  grid-row: 1 / span 3;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: flex-end;
  gap: 0.75rem;
  margin: 0;
}

#klaro .klaro .cookie-notice:not(.cookie-modal-notice) .cn-buttons {
  display: flex;
  gap: 0.75rem;
  margin: 0;
}

#klaro .klaro .cookie-notice .cm-btn,
#klaro .klaro.learn-more-as-button .cookie-notice:not(.cookie-modal-notice) a.cm-link.cn-learn-more,
#klaro .klaro .cookie-modal .cm-footer .cm-btn,
body .klaro.cm-as-context-notice .context-notice .cm-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  box-sizing: border-box;
  min-height: 2.75rem;
  margin: 0;
  padding: 0.625rem 1.0625rem;
  font-size: inherit;
  line-height: 1.2;
  white-space: nowrap;
  text-decoration: none;
}

/* ---------- Dialog ---------- */

#klaro .klaro .cookie-modal .cm-modal.cm-klaro {
  display: flex;
  flex-direction: column;
  /* 600px wide and at most 44rem tall: a settings panel, not a page. */
  width: min(37.5rem, 100%);
  max-width: min(37.5rem, 100%);
  max-height: min(calc(100dvh - 4rem), 44rem);
  overflow: hidden;
}

#klaro .klaro .cookie-modal .cm-header,
#klaro .klaro .cookie-modal .cm-footer {
  flex: 0 0 auto;
}

#klaro .klaro .cookie-modal .cm-header {
  padding: 1.25rem 1.5rem 1rem;
}

#klaro .klaro .cookie-modal .cm-header h1.title {
  margin: 0 3rem 0.375rem 0;
}

#klaro .klaro .cookie-modal .cm-header button.hide {
  top: 0.75rem;
  right: 0.75rem;
  display: grid;
  place-items: center;
  width: 2.75rem;
  height: 2.75rem;
  padding: 0;
}

#klaro .klaro .cookie-modal .cm-body {
  flex: 1 1 auto;
  min-height: 0;
  padding: 0 1.5rem;
  overflow-y: auto;
}

#klaro .klaro .cookie-modal ul.cm-purposes,
#klaro .klaro .cookie-modal ul.cm-content {
  margin: 0;
  padding: 0;
  list-style: none;
}

/* One purpose per row. "Toggle all services": "Accept all" and "Necessary
   only" do its job. lia_consent sets Klaro!'s hideToggleAll, which covers
   the per-purpose service lists; in grouped mode Klaro! still renders the
   overall row, so it is hidden here (display: none also takes it out of the
   tab order). */
#klaro .klaro .cookie-modal li.cm-purpose {
  position: relative;
  margin: 0;
  padding: 1rem 0;
}

#klaro .klaro .cookie-modal li.cm-toggle-all {
  display: none;
}

/* The checkbox stays for keyboard and screen readers, out of sight. */
#klaro .klaro .cookie-modal .cm-list-input {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: 0;
  opacity: 0;
}

/* Title on the left, state label and switch on the right. */
#klaro .klaro .cookie-modal .cm-list-label {
  position: relative;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  /* The whole row is the target (label for the checkbox), so 36px is enough
     height for the 24px switch. */
  min-height: 2.25rem;
  margin: 0;
  padding: 0;
  cursor: pointer;
}

#klaro .klaro .cookie-modal .cm-list-label .cm-list-title {
  flex: 0 1 auto;
  width: auto;
}

#klaro .klaro .cookie-modal .cm-list-label .cm-required {
  display: none;
}

#klaro .klaro .cookie-modal .cm-list-label::after {
  order: 2;
  margin-left: auto;
  min-width: 2.5em;
  text-align: right;
  font-size: 0.8125rem;
  font-weight: 600;
  content: "Off";
}

#klaro .klaro .cookie-modal .cm-list-input:checked + .cm-list-label::after {
  content: "On";
}

#klaro .klaro .cookie-modal .cm-list-input.half-checked:checked + .cm-list-label::after {
  content: "Some";
}

#klaro .klaro .cookie-modal .cm-list-label .cm-switch {
  position: relative;
  top: auto;
  left: auto;
  order: 3;
  flex: 0 0 auto;
  width: 44px;
  height: 24px;
}

/* Klaro!'s knob is sized for its 50 x 30px switch. */
#klaro .klaro .cookie-modal .cm-list-label .slider {
  border-radius: 999px;
}

#klaro .klaro .cookie-modal .cm-list-label .slider::before {
  left: 3px;
  bottom: 3px;
  width: 18px;
  height: 18px;
}

#klaro .klaro .cookie-modal .cm-list-input:checked + .cm-list-label .slider::before {
  transform: translateX(20px);
}

#klaro .klaro .cookie-modal .cm-list-input.half-checked:checked + .cm-list-label .slider::before {
  transform: translateX(10px);
}

/* Rows are divided by a hairline; themes give it their border colour. The
   last row before the hidden "Toggle all services" has none. */
#klaro .klaro .cookie-modal li.cm-purpose:not(:last-child):not(:has(+ li.cm-toggle-all)) {
  border-bottom: 1px solid rgba(127, 127, 127, 0.25);
}

/* Always-on (Necessary): a label instead of a switch that cannot change. */
#klaro .klaro .cookie-modal .cm-list-input.required + .cm-list-label .cm-switch {
  display: none;
}

#klaro .klaro .cookie-modal .cm-list-input.required + .cm-list-label::after {
  content: "\2713\00a0 Always on";
  min-width: 0;
}

#klaro .klaro .cookie-modal li.cm-purpose > [id$="-description"] .cm-list-description {
  margin: 0.125rem 0 0;
  max-width: 64ch;
}

/* The services under each purpose. */
#klaro .klaro .cookie-modal .cm-services {
  margin-top: 0.25rem;
}

/* Klaro!'s "↓ 2 Services" link: a chevron after "2 services" instead. */
#klaro .klaro .cookie-modal .cm-caret a {
  display: inline-flex;
  align-items: center;
  gap: 0.375rem;
  min-height: 2.25rem;
  font-weight: 600;
  text-decoration: none;
  text-transform: lowercase;
}

#klaro .klaro .cookie-modal .cm-caret a > span:first-child {
  display: none;
}

#klaro .klaro .cookie-modal .cm-caret a::after {
  content: "";
  width: 0.5em;
  height: 0.5em;
  margin-left: 0.125rem;
  border-right: 2px solid currentColor;
  border-bottom: 2px solid currentColor;
  transform: translateY(-0.15em) rotate(45deg);
}

#klaro .klaro .cookie-modal .cm-caret:has(+ ul.cm-content.expanded) a::after {
  transform: translateY(0.1em) rotate(-135deg);
}

#klaro .klaro .cookie-modal ul.cm-content {
  display: none;
  margin-top: 0.5rem;
  padding-left: 1rem;
  border-left: 2px solid currentColor;
  border-left-color: rgba(127, 127, 127, 0.3);
}

#klaro .klaro .cookie-modal ul.cm-content.expanded {
  display: grid;
  gap: 0.75rem;
}

#klaro .klaro .cookie-modal li.cm-service {
  position: relative;
  margin: 0;
  padding: 0;
}

#klaro .klaro .cookie-modal li.cm-service .cm-list-label {
  min-height: 2.25rem;
}

/* Necessary services have no switch and no label in the list. */
#klaro .klaro .cookie-modal li.cm-service .cm-list-input.required + .cm-list-label::after {
  content: none;
}

#klaro .klaro .cookie-modal li.cm-service .cm-list-description {
  margin: 0;
}

#klaro .klaro .cookie-modal li.cm-service p.purposes {
  display: none;
}

/* Footer: "Save choices" left, "Necessary only" and "Accept all" right. */
#klaro .klaro .cookie-modal .cm-footer {
  padding: 0.875rem 1.5rem;
}

#klaro .klaro .cookie-modal .cm-footer-buttons {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.75rem;
}

#klaro .klaro .cookie-modal .cm-footer-buttons .cm-btn-accept {
  order: -1;
  margin-right: auto;
}

#klaro .klaro .cookie-modal .cm-powered-by {
  display: none;
}

/* ---------- Embed placeholder ---------- */

body .klaro.cm-as-context-notice .context-notice.lia-consent-placeholder {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 1rem;
  box-sizing: border-box;
  padding: 2rem;
  text-align: left;
}

body .klaro.cm-as-context-notice .context-notice.lia-consent-placeholder p {
  max-width: 62ch;
  margin: 0;
  text-align: left;
}

/* Klaro!'s generic question; the service's own text says it better. */
body .klaro.cm-as-context-notice .context-notice.lia-consent-placeholder > p:not(.cm-buttons):not(.cm-dialog-link):nth-of-type(2) {
  display: none;
}

/* The heading ("Registration form not loaded"), written as <strong> because
   Klaro! filters the placeholder text to inline HTML. */
body .klaro.cm-as-context-notice .context-notice.lia-consent-placeholder strong {
  display: block;
  margin-bottom: 0.25rem;
  color: inherit;
  font-size: 1.125em;
}

body .klaro.cm-as-context-notice .context-notice .lia-consent-placeholder__eyebrow {
  font-size: 0.75rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

body .klaro.cm-as-context-notice .lia-consent-placeholder__actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
}

/* ---------- Phones ---------- */

@media (max-width: 767.98px) {
  #klaro .klaro .cookie-notice:not(.cookie-modal-notice) {
    right: 0;
    bottom: 0;
    left: 0;
  }

  #klaro .klaro .cookie-notice:not(.cookie-modal-notice) .cn-body {
    grid-template-columns: 1fr;
    gap: 0.5rem;
    padding: 1rem 1rem calc(1rem + env(safe-area-inset-bottom, 0px));
  }

  #klaro .klaro .cookie-notice:not(.cookie-modal-notice) .cn-ok {
    grid-column: 1;
    grid-row: auto;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.5rem;
    margin-top: 0.5rem;
  }

  #klaro .klaro .cookie-notice:not(.cookie-modal-notice) .cn-buttons {
    display: contents;
  }

  #klaro .klaro .cookie-notice:not(.cookie-modal-notice) .cm-btn,
  #klaro .klaro.learn-more-as-button .cookie-notice:not(.cookie-modal-notice) a.cm-link.cn-learn-more {
    width: 100%;
    white-space: normal;
  }

  #klaro .klaro.learn-more-as-button .cookie-notice:not(.cookie-modal-notice) a.cm-link.cn-learn-more {
    grid-column: 1 / -1;
    order: 3;
  }

  #klaro .klaro .cookie-modal .cm-modal.cm-klaro {
    width: 100%;
    max-width: 100%;
    height: 100dvh;
    max-height: 100dvh;
  }

  #klaro .klaro .cookie-modal .cm-header {
    padding: 1rem;
  }

  #klaro .klaro .cookie-modal .cm-body {
    padding: 0 1rem;
  }

  #klaro .klaro .cookie-modal .cm-footer {
    padding: 0.75rem 1rem calc(0.75rem + env(safe-area-inset-bottom, 0px));
  }

  #klaro .klaro .cookie-modal .cm-footer-buttons {
    display: grid;
    grid-template-columns: 1fr;
    gap: 0.5rem;
  }

  #klaro .klaro .cookie-modal .cm-footer-buttons .cm-btn {
    width: 100%;
  }

  #klaro .klaro .cookie-modal .cm-footer-buttons .cm-btn-accept {
    order: 3;
    margin-right: 0;
  }

  body .klaro.cm-as-context-notice .context-notice.lia-consent-placeholder {
    padding: 1.5rem 1rem;
  }

  body .klaro.cm-as-context-notice .lia-consent-placeholder__actions {
    display: grid;
    grid-template-columns: 1fr;
    width: 100%;
  }

  body .klaro.cm-as-context-notice .lia-consent-placeholder__actions .cm-btn {
    width: 100%;
    white-space: normal;
  }
}

@media (prefers-reduced-motion: reduce) {
  #klaro .klaro .cookie-modal .slider,
  #klaro .klaro .cookie-modal .slider::before {
    transition: none;
  }
}
