/* The cookie consent (app/views/layouts/_cookie_consent.html.erb): the banner of a visitor who has not decided yet and
   the dialog of the settings. Linked EXPLICITLY in layouts/_head, so it reaches every layout, like toast.css. Both lie
   outside any .kl wrapper: the tokens are declared on .kl__consent too, and the buttons and links are the design
   system's (ms-button, ms-link). Every element sets its own margins: above a React application the application's
   resets reach this markup too. Only the z-index is this sheet's own — a token of the mechanism, not of the style. */

@layer components {
  :root {
    /* above the public navbar over the SPAs (1001) and the applications' layers; below the toasts (1200) */
    --consent-z: 1100;
  }

  .kl__consent {
    font-family: var(--font-sans);
    font-weight: var(--weight-regular);
    line-height: 1.55;
    color: var(--ink);
  }

  .kl__consent *,
  .kl__consent *::before,
  .kl__consent *::after {
    box-sizing: border-box;
  }

  .kl__consent :focus-visible {
    outline: 2px solid var(--tone);
    outline-offset: 3px;
  }

  /* ——— Banner ——— */

  /* Shown only while the engine marks the document „pending” (app/javascript/consent.js): never for a visitor who has
     decided, never without JavaScript. Fixed, so it moves nothing on the page. */
  .kl__consent-banner {
    display: none;
  }

  html[data-consent="pending"] .kl__consent-banner {
    display: block;
    position: fixed;
    right: 16px;
    bottom: max(16px, env(safe-area-inset-bottom));
    left: 16px;
    z-index: var(--consent-z);
  }

  .kl__consent-banner-in {
    max-width: 1080px;
    margin: 0 auto;
    display: flex;
    align-items: center;
    gap: 28px;
    padding: 20px 24px;
    background: color-mix(in srgb, var(--paper) 94%, transparent);
    backdrop-filter: var(--glass-blur);
    -webkit-backdrop-filter: var(--glass-blur);
    border: 1px solid var(--line);
    border-radius: var(--radius-card);
    box-shadow: var(--shadow-xl);
  }

  .kl__consent-title {
    margin: 0 0 4px;
    font-family: var(--font-display);
    font-size: var(--text-xl);
    font-weight: var(--weight-medium);
    line-height: 1.3;
    color: var(--ink);
  }

  .kl__consent-lead {
    margin: 0;
    font-size: var(--text-md);
    color: var(--ink-soft);
  }

  .kl__consent-actions {
    flex: none;
    display: flex;
    gap: 10px;
  }

  /* ——— Settings dialog ——— */

  /* A modal <dialog> in the top layer: centred explicitly (a reset of an application may zero the UA's margin). */
  .kl__consent-dialog {
    position: fixed;
    inset: 0;
    width: min(560px, calc(100vw - 32px));
    height: fit-content;
    max-height: calc(100dvh - 32px);
    margin: auto;
    padding: 28px;
    overflow-y: auto;
    border: 0;
    border-radius: var(--radius-card);
    background: var(--paper);
    color: var(--ink);
    box-shadow: var(--shadow-xl);
  }

  .kl__consent-dialog::backdrop {
    background: color-mix(in srgb, var(--ink) 45%, transparent);
  }

  html:has(.kl__consent-dialog[open]) {
    overflow: hidden;
  }

  .kl__consent-dialog-head {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 16px;
  }

  .kl__consent-dialog-title {
    margin: 0;
    font-family: var(--font-display);
    font-size: var(--heading-md);
    font-weight: var(--weight-regular);
    line-height: 1.25;
    color: var(--ink);
  }

  .kl__consent-close {
    flex: none;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    margin: -4px -6px 0 0;
    padding: 0;
    border: 1px solid color-mix(in srgb, var(--tone) 18%, transparent);
    border-radius: 50%;
    background: var(--paper);
    font: inherit;
    font-size: var(--heading-md);
    line-height: 1;
    color: var(--ink-soft);
    cursor: pointer;
  }

  .kl__consent-close:hover {
    color: var(--tone);
    border-color: color-mix(in srgb, var(--tone) 45%, transparent);
  }

  .kl__consent-intro {
    margin: 10px 0 18px;
    font-size: var(--text-md);
    color: var(--ink-soft);
  }

  .kl__consent-categories {
    margin: 0;
    padding: 0;
    list-style: none;
    border-top: 1px solid var(--border);
  }

  .kl__consent-category {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 18px;
    margin: 0;
    padding: 16px 0;
    border-bottom: 1px solid var(--border);
  }

  .kl__consent-category-name {
    display: block;
    margin: 0;
    font-weight: var(--weight-medium);
    color: var(--ink);
  }

  label.kl__consent-category-name {
    cursor: pointer;
  }

  .kl__consent-category-description {
    margin: 4px 0 0;
    font-size: var(--text-sm);
    color: var(--muted);
  }

  .kl__consent-always {
    flex: none;
    font-size: var(--text-sm);
    font-weight: var(--weight-medium);
    color: var(--success);
    white-space: nowrap;
  }

  /* A switch (role="switch" on a native checkbox, so the keyboard and screen readers get it for free): a track of the
     accent while on. */
  .kl__consent-switch {
    appearance: none;
    flex: none;
    position: relative;
    width: 44px;
    height: 26px;
    margin: 2px 0 0;
    border: 1px solid transparent;
    border-radius: var(--radius-pill);
    background: color-mix(in srgb, var(--muted) 45%, var(--white));
    cursor: pointer;
    transition: background var(--duration-fast) var(--ease-standard);
  }

  .kl__consent-switch::before {
    content: "";
    position: absolute;
    top: 2px;
    left: 2px;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: var(--white);
    box-shadow: var(--shadow-xs);
    transition: transform var(--duration-fast) var(--ease-standard);
  }

  .kl__consent-switch:checked {
    background: var(--accent);
  }

  .kl__consent-switch:checked::before {
    transform: translateX(18px);
  }

  .kl__consent-dialog-actions {
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-end;
    gap: 10px;
    margin-top: 22px;
  }

  /* The footer's „Ustawienia cookies” opens the dialog through the engine: without it (no JavaScript) it has nothing to
     open. */
  html:not([data-consent]) [data-consent-settings] {
    display: none;
  }

  /* Phone: the banner spans the width above the bottom edge, its text above two equal buttons; the dialog's buttons
     stack. */
  @media (max-width: 768px) {
    html[data-consent="pending"] .kl__consent-banner {
      right: 8px;
      bottom: max(8px, env(safe-area-inset-bottom));
      left: 8px;
    }

    .kl__consent-banner-in {
      flex-direction: column;
      align-items: stretch;
      gap: 14px;
      padding: 16px 18px;
    }

    .kl__consent-lead {
      font-size: var(--text-sm);
    }

    .kl__consent-actions .ms-button {
      flex: 1 1 0;
    }

    .kl__consent-dialog {
      padding: 22px 18px;
    }

    .kl__consent-dialog-actions {
      flex-direction: column;
      align-items: stretch;
    }
  }

  @media (prefers-reduced-motion: reduce) {
    .kl__consent-switch,
    .kl__consent-switch::before {
      transition: none;
    }
  }
}
