/* =============================================================================
   Survey UI - Global Styles
   
   This file contains:
   1. Tailwind CSS setup and configuration
   2. Base design tokens (colors, radius)
   3. Survey-specific theming tokens (--fb-* variables)
   
   All tokens are designed to be overridden by consumers for custom theming.
   Components use Tailwind utilities backed by these CSS variables.
   ============================================================================= */

/*
  Cascade layer order (ENG-3449 / ENG-3552). The first statement in this stylesheet, so wherever the
  host page has not named these layers yet, it fixes their order before anything below registers
  them. Customer CSS lives in the four fb-* layers and is all `!important`, and for important
  declarations the earlier layer wins: survey dark > survey > workspace dark > workspace > the
  theme editor and every built-in style. Keep in sync with CUSTOM_CSS_LAYER_ORDER in
  packages/types/custom-css.ts; the renderer also puts the fb-* names first in <head>.
*/
@layer fb-survey-dark, fb-survey, fb-workspace-dark, fb-workspace, theme, base, components, utilities;
@import "tailwindcss/theme.css" layer(theme);
@import "tailwindcss/utilities.css" layer(utilities) important;

/* NOSONAR: Tailwind CSS v4 directives - @config and @source are valid at-rules */
@config "../../tailwind.config.ts";
/* NOSONAR */
@source "../../src/**/*.{ts,tsx,js,jsx}";
/* NOSONAR */

/*
  Unitless line heights (ENG-3427). Without these, `leading-<n>` is `calc(var(--spacing) * n)`, a
  length: in the surveys bundle rem→em makes it an em length, which iPhone WebKit scales twice under
  the iOS SDK's `-webkit-text-size-adjust` (Dynamic Type). A ratio scales once, and n / 4 is what the
  length gave on the element itself. Keep in sync with packages/surveys/src/styles/global.css.
*/
@theme inline {
  --leading-4: 1;
  --leading-5: 1.25;
  --leading-6: 1.5;
}

/* =============================================================================
   Design Tokens
   ============================================================================= */

#fbjs {
  /* ---------------------------------------------------------------------------
     Base Primitives
     These are the foundation tokens used throughout the design system.
     --------------------------------------------------------------------------- */
  --radius: 0.625rem;
  --background: oklch(1 0 0);
  --foreground: oklch(0.145 0 0);
  --card: oklch(1 0 0);
  --card-foreground: oklch(0.145 0 0);
  --popover: oklch(1 0 0);
  --popover-foreground: oklch(0.145 0 0);
  --primary: oklch(0.205 0 0);
  --primary-foreground: oklch(0.985 0 0);
  --secondary: oklch(0.97 0 0);
  --secondary-foreground: oklch(0.205 0 0);
  --muted: oklch(0.97 0 0);
  --muted-foreground: oklch(0.556 0 0);
  --accent: oklch(0.97 0 0);
  --accent-foreground: oklch(0.205 0 0);
  --destructive: oklch(0.577 0.245 27.325);
  --destructive-muted: oklch(0.97 0.02 27.325);
  --border: oklch(0.922 0 0);
  --input: black;
  --slate-50: rgb(248, 250, 252);
  --slate-100: rgb(241 245 249);
  --slate-200: rgb(226 232 240);
  --slate-700: rgb(51 65 85);
  --slate-900: rgb(15 23 42);

  /* ---------------------------------------------------------------------------
     Theming Tokens (--fb-*)

     Color values MUST match the output of getSuggestedColors("#1e40af") in
     apps/web/lib/styling/constants.ts.  Non-color values MUST match
     STYLE_DEFAULTS in the same file.

     At runtime styles.ts always overrides these from the JS object, but the
     CSS values below act as the initial / fallback state so the survey looks
     correct before JS executes.

     Derived colour palette for brand #1e40af:
       questionColor  = #142a72   (brand mixed with black @ 0.35)
       inputBg        = #edf0f9   (brand mixed with white @ 0.92)
       inputBorder    = #a5b3df   (brand mixed with white @ 0.60)
       cardBg         = #f8f9fd   (brand mixed with white @ 0.97)
       cardBorder     = #d2d9ef   (brand mixed with white @ 0.80)
       progressTrack  = #d2d9ef   (brand mixed with white @ 0.80)
     --------------------------------------------------------------------------- */

  /* ── Brand ─────────────────────────────────────────────────────────── */
  --fb-survey-brand-color: #1e40af;
  --fb-accent-background-color: #d2d9ef;
  --fb-accent-background-color-selected: #bcc6e7;

  /* ── Headlines ─────────────────────────────────────────────────────── */
  --fb-element-headline-font-family: inherit;
  --fb-element-headline-font-weight: 500;
  --fb-element-headline-font-size: 16px;
  --fb-element-headline-color: #142a72;
  --fb-element-headline-opacity: 1;

  /* ── Descriptions ──────────────────────────────────────────────────── */
  --fb-element-description-font-family: inherit;
  --fb-element-description-font-weight: 400;
  --fb-element-description-font-size: 14px;
  --fb-element-description-color: #142a72;
  --fb-element-description-opacity: 1;

  /* ── Upper Labels ("Required" + rating/matrix scale labels) ────────────
     Brand-tinted text at full opacity. The opacity is the WCAG 1.4.3 fix: the
     previous 0.6 opacity diluted the color into the card and failed the 4.5:1
     contrast for this small text. The hue stays configurable and brand-derived
     (see getSuggestedColors().elementUpperLabelColor in
     apps/web/lib/styling/constants.ts). */
  --fb-element-upper-label-font-family: inherit;
  --fb-element-upper-label-font-weight: 400;
  --fb-element-upper-label-font-size: 12px;
  --fb-element-upper-label-color: #142a72;
  --fb-element-upper-label-opacity: 1;

  /* ── Labels ────────────────────────────────────────────────────────── */
  --fb-label-font-family: inherit;
  --fb-label-font-weight: 400;
  --fb-label-font-size: 14px;
  --fb-label-color: #142a72;
  --fb-label-opacity: 1;

  /* ── Buttons ───────────────────────────────────────────────────────── */
  --fb-button-height: auto;
  --fb-button-width: auto;
  --fb-button-font-family: inherit;
  --fb-button-font-size: 16px;
  --fb-button-font-weight: 500;
  --fb-button-border-radius: 8px;
  --fb-button-bg-color: #1e40af;
  --fb-button-text-color: #ffffff;
  --fb-button-padding-x: 12px;
  --fb-button-padding-y: 12px;

  /* ── Inputs ────────────────────────────────────────────────────────── */
  --fb-input-bg-color: #edf0f9;
  --fb-input-border-color: #a5b3df;
  --fb-input-border-radius: 8px;
  --fb-input-font-family: inherit;
  --fb-input-font-size: 14px;
  --fb-input-font-weight: 400;
  --fb-input-color: #142a72;
  --fb-input-placeholder-color: var(--fb-input-text-color, var(--fb-input-color));
  --fb-input-placeholder-opacity: 0.5;
  --fb-input-width: 100%;
  --fb-input-height: 20px;
  --fb-input-padding-x: 8px;
  --fb-input-padding-y: 8px;
  --fb-input-shadow: 0 1px 2px 0 rgb(0 0 0 / 0.05);

  /* ── Progress Bar ──────────────────────────────────────────────────── */
  --fb-progress-track-height: 8px;
  --fb-progress-track-bg-color: #d2d9ef;
  --fb-progress-track-border-radius: 8px;
  --fb-progress-indicator-bg-color: #1e40af;
  --fb-progress-indicator-border-radius: 8px;

  /* ── Options (radio / checkbox / multi-select) ─────────────────────── */
  --fb-option-bg-color: #edf0f9;
  --fb-option-label-color: #142a72;
  --fb-option-border-radius: 8px;
  --fb-option-padding-x: 16px;
  --fb-option-padding-y: 16px;
  --fb-option-font-family: inherit;
  --fb-option-font-size: 14px;
  --fb-option-font-weight: 400;

  /* ── Page backdrop ─────────────────────────────────────────────────── */
  /* The gray the link-survey shell paints behind the widget when no survey
     background is configured (MediaBackground's default, slate-200). The
     cardless layout blends its scroll fades into it; the shell can override
     this token if its default ever changes. */
  --fb-page-bg-color: #e2e8f0;
}

/* =============================================================================
   Dark appearance

   The renderer sets data-appearance on every #fbjs root (ENG-2940). These are
   the fallback values for a survey without styling, so a dark survey paints
   dark from its first frame. styles.ts overrides them from the styling object
   exactly as it does for light.

   Color values MUST match getDerivedDarkColors("#1e40af") in
   packages/types/dark-palette.ts. Brand, button and progress indicator keep
   their light values (D12), so they are not repeated here.

   `color-scheme` sits on the survey root, never on :root — on :root it can
   paint a transparent mobile WebView dark over the host app (ENG-3447).
   ============================================================================= */

#fbjs[data-appearance="dark"] {
  color-scheme: dark;

  --background: #0d1426;
  --foreground: #dde2f3;
  --card: #0d1426;
  --card-foreground: #dde2f3;
  --popover: #0e1832;
  --popover-foreground: #dde2f3;
  --secondary: #0e1832;
  --secondary-foreground: #dde2f3;
  --muted: #0e1832;
  --muted-foreground: #9fa5b9;
  --accent: #101d42;
  --accent-foreground: #dde2f3;
  --border: #5b6a97;
  --input: #dde2f3;

  /* Hover / selected states lighten a dark surface instead of darkening it. */
  --fb-shade-color: white;
  --fb-tint-color: #0d1426;

  --fb-accent-background-color: #101d42;
  --fb-accent-background-color-selected: #122251;
  --fb-element-headline-color: #dde2f3;
  --fb-element-description-color: #dde2f3;
  --fb-element-upper-label-color: #dde2f3;
  --fb-label-color: #dde2f3;
  --fb-input-bg-color: #0e1832;
  --fb-input-border-color: #5b6a97;
  --fb-input-color: #dde2f3;
  --fb-input-shadow: none;
  --fb-progress-track-bg-color: #5a6587;
  --fb-option-bg-color: #0e1832;
  --fb-option-label-color: #dde2f3;
  --fb-page-bg-color: #0b0f17;

  /* Readable variants of the brand and the error red on the dark card
     (getDarkReadableColors in dark-palette.ts). */
  --destructive: #ee4d54;
  --fb-brand-readable-color: #788ccf;
}

#fbjs .button-custom {
  width: var(--fb-button-width);
  height: var(--fb-button-height);
  background-color: var(--fb-button-bg-color);
  border-radius: var(--fb-button-border-radius);
  font-family: var(--fb-button-font-family);
  font-weight: var(--fb-button-font-weight);
  padding-left: var(--fb-button-padding-x);
  padding-right: var(--fb-button-padding-x);
  padding-top: var(--fb-button-padding-y);
  padding-bottom: var(--fb-button-padding-y);
  color: var(--fb-button-text-color);
  font-size: var(--fb-button-font-size);
}

/*
 * Element prompts are wrapped in a real heading for WCAG 2.4.6 (see element-header.tsx). The
 * heading is a semantic wrapper only — the visible text is still the <Label> inside it — so the
 * UA's own heading typography and margins have to be neutralised or the prompt gains size and
 * spacing it never had. `packages/surveys/preflight.css` already does this for the bundled
 * survey, but that preflight ships with the surveys package and this stylesheet is consumed on
 * its own too (Storybook, and any standalone React consumer), so survey-ui declares it itself.
 */
#fbjs h1,
#fbjs h2,
#fbjs h3,
#fbjs h4,
#fbjs h5,
#fbjs h6 {
  font-size: inherit;
  font-weight: inherit;
  margin: 0;
}

#fbjs .label-headline,
#fbjs .label-headline * {
  font-family: var(--fb-element-headline-font-family);
  font-weight: var(--fb-element-headline-font-weight);
  font-size: var(--fb-element-headline-font-size);
  color: var(--fb-element-headline-color);
  opacity: var(--fb-element-headline-opacity);
}

#fbjs .label-description,
#fbjs .label-description *,
#fbjs .text-subheading,
#fbjs .text-subheading * {
  font-family: var(--fb-element-description-font-family);
  font-weight: var(--fb-element-description-font-weight);
  font-size: var(--fb-element-description-font-size);
  color: var(--fb-element-description-color);
  opacity: var(--fb-element-description-opacity);
}

/*
 * Formatted text inside the headline and description hooks takes its typography from the hook element
 * rather than from the theme variables directly, so a rule on the hook (Custom CSS, ENG-3554) reaches
 * every word of it, not only plain-text headlines. The hook element holds the same theme values, so
 * nothing changes without Custom CSS. Opacity is not inherited and stays as it is.
 */
#fbjs [data-fb-part="headline"] *,
#fbjs [data-fb-part="description"] * {
  font-family: inherit;
  font-weight: inherit;
  font-size: inherit;
  color: inherit;
}

/*
 * Keep bold (<strong>/<b>) inside rich text actually bold. The headline /
 * description weight rules above target every descendant (`* `), which would
 * otherwise flatten bold spans to the base weight and erase the contrast
 * between bold and non-bold text. Higher specificity + !important wins over
 * those rules (and the equivalent runtime rules injected by styles.ts).
 */
#fbjs .label-headline strong,
#fbjs .label-headline b,
#fbjs .label-description strong,
#fbjs .label-description b,
#fbjs .text-subheading strong,
#fbjs .text-subheading b,
#fbjs .htmlbody strong,
#fbjs .htmlbody b {
  font-weight: 700 !important;
}

#fbjs .label-default,
#fbjs .label-default * {
  font-family: var(--fb-label-font-family);
  font-weight: var(--fb-label-font-weight);
  font-size: var(--fb-label-font-size);
  color: var(--fb-label-color);
  opacity: var(--fb-label-opacity);
}

#fbjs .label-card,
#fbjs .label-card * {
  font-family: var(--fb-element-upper-label-font-family);
  font-weight: var(--fb-element-upper-label-font-weight);
  font-size: var(--fb-element-upper-label-font-size);
  color: var(--fb-element-upper-label-color);
  opacity: var(--fb-element-upper-label-opacity);
}

#fbjs .progress-track {
  height: var(--fb-progress-track-height);
  background-color: var(--fb-progress-track-bg-color);
  border-radius: var(--fb-progress-track-border-radius);
}

#fbjs .progress-indicator {
  --fb-progress-translate-x: -100%;
  background-color: var(--fb-progress-indicator-bg-color);
  border-radius: var(--fb-progress-indicator-border-radius);
}

/* =============================================================================
   Focus Visibility (WCAG 2.4.7)

   One uniform focus indicator for every focusable element in the survey: a
   dashed outline in the focus-ring outer color, drawn over a halo in the inner
   color. It is dashed so focus never reads as selection: a selected choice is
   a solid fill/border, a focused one gets a dashed outline (ENG-4062). The
   halo sits behind the whole indicator — the gap between control and outline
   and the gaps between the dashes — so the dashes always meet the halo color
   and stay visible on dark or saturated backgrounds (WCAG 1.4.11).

   This lives in the `components` layer with !important on purpose: the
   per-component Tailwind `focus-visible:ring-*` / `outline-none` utilities are
   emitted in the later `utilities` layer as !important, and for important
   declarations an earlier layer wins the cascade. That lets this single rule
   replace every ad-hoc focus style uniformly.

   Colors, widths and the outline style are exposed as tokens so a consumer can
   re-theme the indicator if needed.
   ============================================================================= */
@layer components {
  #fbjs {
    --fb-focus-ring-inner-color: #ffffff;
    --fb-focus-ring-outer-color: var(--fb-survey-brand-color, #1e40af);
    --fb-focus-ring-inner-width: 2px;
    --fb-focus-ring-outer-width: 2px;
    --fb-focus-ring-style: dashed;
  }

  /* The halo separates the outline from the control; on a dark card it takes
     the card color instead of white. */
  #fbjs[data-appearance="dark"] {
    --fb-focus-ring-inner-color: var(--fb-survey-background-color, #0d1426);
    /* The brand can sit below 3 : 1 on the dark card; styles.ts emits the readable variant. */
    --fb-focus-ring-outer-color: #4b66bf;
  }

  /* Tailwind v4's transition-colors (and transition-all) include the outline
     properties. The resting values below match what the focus rule sets, so
     gaining focus only flips outline-style (a discrete, non-animated change)
     instead of animating the outline color in from the browser default — a
     dark ring flashing outside the control. While outline-style is none
     nothing is painted, so these are invisible at rest. */
  #fbjs a,
  #fbjs button,
  #fbjs input,
  #fbjs select,
  #fbjs textarea,
  #fbjs summary,
  #fbjs [tabindex],
  #fbjs label:has(input[type="radio"]),
  #fbjs label:has(input[type="checkbox"]),
  #fbjs [data-fb-focus-ring],
  #fbjs [role="menuitem"],
  #fbjs [role="menuitemradio"],
  #fbjs [role="menuitemcheckbox"] {
    outline-color: var(--fb-focus-ring-outer-color);
    outline-offset: var(--fb-focus-ring-inner-width);
  }

  /* Inset variants (below) rest at their inset offset for the same reason. */
  #fbjs label[data-fb-scale-cell]:has(input[type="radio"]),
  #fbjs [role="menuitem"],
  #fbjs [role="menuitemradio"],
  #fbjs [role="menuitemcheckbox"] {
    outline-offset: calc(-1 * var(--fb-focus-ring-outer-width));
  }

  #fbjs a:focus-visible,
  #fbjs button:focus-visible,
  #fbjs input:focus-visible,
  #fbjs select:focus-visible,
  #fbjs textarea:focus-visible,
  #fbjs summary:focus-visible,
  /* tabindex="-1" targets are script-focus-only (e.g. the ending card container focused
     for screen-reader announcement) — not operable controls, so no indicator. :where()
     keeps the exclusion at zero specificity so later same-specificity overrides (the
     inset menu-item outline below) still win the cascade. */
  #fbjs [tabindex]:where(:not([tabindex="-1"])):focus-visible,
  /* A visually-hidden radio/checkbox drives a visible label: paint the indicator on the label. */
  #fbjs label:has(input[type="radio"]:focus-visible),
  #fbjs label:has(input[type="checkbox"]:focus-visible),
  #fbjs [data-fb-focus-ring]:has(:focus-visible),
  /* Indicator carrier as the hidden input's direct sibling (e.g. the matrix's round
     indicator), for controls where the wrapping label is not the right shape to ring. */
  #fbjs input:focus-visible + [data-fb-focus-ring] {
    /* A real outline (not a box-shadow) carries the indicator, so it survives forced-colors,
       where box-shadows drop; the halo is the shadow underneath it. */
    outline: var(--fb-focus-ring-outer-width) var(--fb-focus-ring-style) var(--fb-focus-ring-outer-color) !important;
    outline-offset: var(--fb-focus-ring-inner-width) !important;
    box-shadow: 0 0 0 calc(var(--fb-focus-ring-inner-width) + var(--fb-focus-ring-outer-width))
      var(--fb-focus-ring-inner-color) !important;
  }

  /* Matrix cells: the label spans the whole cell, so a label-level indicator renders as a
     wide sharp rectangle around a 16px circle. Suppress it; the round indicator (the
     input's direct sibling, tagged data-fb-focus-ring) carries the outline and matches the
     control's shape (WCAG 2.4.7 stays satisfied by the indicator). */
  #fbjs table label:has(input[type="radio"]:focus-visible) {
    box-shadow: none !important;
    outline: none !important;
  }

  /* The hidden input is 1px and clipped; its visible label paints the indicator instead.
     outline:none also prevents a stray Highlight outline on it under forced-colors. */
  #fbjs input.sr-only:focus-visible {
    box-shadow: none !important;
    outline: none !important;
  }

  /* Scale cells (NPS / rating numbers) sit flush together and against the card's
     scroll edge, so an outset indicator is clipped (WCAG 2.4.11). Draw the dashed
     outline inset instead (over an inset halo band) and raise the focused cell. The
     negative outline-offset keeps it inset under forced-colors too, where the halo
     drops but the outline stays. */
  #fbjs label[data-fb-scale-cell]:has(input[type="radio"]:focus-visible) {
    z-index: 20;
    outline-offset: calc(-1 * var(--fb-focus-ring-outer-width)) !important;
    box-shadow: inset 0 0 0 var(--fb-focus-ring-outer-width) var(--fb-focus-ring-inner-color) !important;
  }

  /* Dropdown menu options: the menu content clips overflowing shadows and outlines
     (overflow-hidden for its rounded corners), which cut the sides off an outset
     indicator. Draw it inset for menu items as well (same rationale as above). */
  #fbjs [role="menuitem"]:focus-visible,
  #fbjs [role="menuitemradio"]:focus-visible,
  #fbjs [role="menuitemcheckbox"]:focus-visible {
    outline-offset: calc(-1 * var(--fb-focus-ring-outer-width)) !important;
    box-shadow: inset 0 0 0 var(--fb-focus-ring-outer-width) var(--fb-focus-ring-inner-color) !important;
  }

  /* Forced colors replace author colors on outlines; pin the system focus color so the
     dashed outline reads as focus in high-contrast themes. */
  @media (forced-colors: active) {
    #fbjs a:focus-visible,
    #fbjs button:focus-visible,
    #fbjs input:focus-visible,
    #fbjs select:focus-visible,
    #fbjs textarea:focus-visible,
    #fbjs summary:focus-visible,
    #fbjs [tabindex]:where(:not([tabindex="-1"])):focus-visible,
    #fbjs label:has(input[type="radio"]:focus-visible),
    #fbjs label:has(input[type="checkbox"]:focus-visible),
    #fbjs [data-fb-focus-ring]:has(:focus-visible),
    #fbjs input:focus-visible + [data-fb-focus-ring] {
      outline-color: Highlight !important;
    }
  }
}

/* ---------------------------------------------------------------------------
   Textarea Scrollbar Styling
   Smaller, more subtle scrollbars for textarea elements.
   --------------------------------------------------------------------------- */
#fbjs textarea {
  /* Firefox */
  scrollbar-width: thin;
  scrollbar-color: hsl(215.4 16.3% 46.9% / 0.3) transparent;
}

/* Chrome, Edge, and Safari */
#fbjs textarea::-webkit-scrollbar {
  width: 4px;
  height: 4px;
}

#fbjs textarea::-webkit-scrollbar-track {
  background: transparent;
}

#fbjs textarea::-webkit-scrollbar-thumb {
  background-color: hsl(215.4 16.3% 46.9% / 0.3);
  border-radius: 2px;
}

#fbjs textarea::-webkit-scrollbar-thumb:hover {
  background-color: hsl(215.4 16.3% 46.9% / 0.5);
}
