/* ============================================================
   Onboarding — welcome-popup gating, tour chrome, replay controls
   ------------------------------------------------------------
   Loaded only on pages that can start a tour. Everything is scoped to
   driver.js' own classes plus .otk-* hooks, so no existing rule is
   overridden except the tour layer's z-index (which must sit under the
   popup overlay at 2147483600 and above all page UI).
   ============================================================ */

/* While the onboarding state call is in flight, a welcome popup that the
   popup loader may already have built is hidden for that moment. The
   attribute is set and cleared by onboarding-welcome.js, and the popup
   cannot appear before window load + 400 ms, so it is never hidden for a
   real viewer — this exists purely to stop a returning user on a second
   device from seeing a frame of the welcome dialog. */
html[data-otk-welcome-check] .otk-popup-overlay {
  visibility: hidden !important;
}

/* --- tour layering: below the popup maker, above the app --------------- */
.driver-overlay {
  z-index: 2147483500 !important;
}
.driver-popover {
  z-index: 2147483501 !important;
}

/* --- blur veil: driver's SVG dims the page but cannot blur it.
   Four pointer-transparent panels sit just outside the stage cut-out and
   apply a backdrop blur, so the highlighted element stays sharp while the
   rest of the page is softened and darkened. ------------------------------ */
.otk-tour-veil {
  position: fixed;
  inset: 0;
  z-index: 2147483499;
  pointer-events: none;
  overflow: hidden;
}

.otk-tour-veil__panel {
  position: absolute;
  display: block;
  background: rgba(9, 13, 20, 0.15);
  -webkit-backdrop-filter: blur(6px) saturate(0.9);
  backdrop-filter: blur(6px) saturate(0.9);
}

/* --- focus ring on the element the step is talking about --------------- */
.driver-active-element {
  outline: 3px solid var(--otk-tour-ring, #ff8a1f) !important;
  outline-offset: 4px;
  border-radius: 12px;
  box-shadow: 0 0 0 6px rgba(255, 138, 31, 0.22), 0 10px 30px rgba(9, 13, 20, 0.28);
}

.driver-active-element::after {
  content: "";
  position: absolute;
  inset: -4px;
  border-radius: inherit;
  pointer-events: none;
  border: 3px solid var(--otk-tour-ring, #ff8a1f);
  opacity: 0.9;
}

@media (prefers-reduced-motion: no-preference) {
  .driver-active-element {
    animation: otk-tour-ring 2.4s ease-in-out infinite;
  }
  @keyframes otk-tour-ring {
    0%, 100% { outline-color: var(--otk-tour-ring, #ff8a1f); }
    50%      { outline-color: rgba(255, 138, 31, 0.55); }
  }
}

/* --- tour card -------------------------------------------------------- */
.driver-popover {
  --otk-tour-bg: var(--surface, #ffffff);
  --otk-tour-ink: var(--ink, #0f172a);
  --otk-tour-muted: var(--muted, #52606d);
  --otk-tour-accent: var(--brand-navy-700, #0b3c5d);
  --otk-tour-on-accent: #ffffff;
  --otk-tour-line: var(--line, #e4e6ea);
  --otk-tour-ring: var(--brand-orange-400, #ff8a1f);
  --otk-tour-font: "Poppins", var(--font-sans, "Segoe UI"), Tahoma, Geneva, sans-serif;

  color-scheme: light dark;
  background: var(--otk-tour-bg);
  color: var(--otk-tour-ink);
  border-radius: calc(var(--radius-lg, 14px));
  border: 1px solid var(--otk-tour-line);
  box-shadow: 0 18px 44px rgba(9, 13, 20, 0.24);
  max-width: min(480px, calc(100vw - 24px));
  padding: 20px 22px 18px;
  font-family: var(--otk-tour-font);
  font-size: 16px;
}

/* driver.css sets max-width: 300px on the same class and is loaded after this
   sheet on some pages; the doubled class keeps the wider card and the Poppins
   face (driver.css declares its own font-family on .driver-popover). */
.driver-popover.driver-popover {
  --driver-popover-font-family: var(--otk-tour-font);
  max-width: min(480px, calc(100vw - 24px));
  min-width: 0;
  font-family: var(--otk-tour-font);
  /* driver.css is loaded after this sheet and paints #fff / #2d2d2d on the
     single-class selector. Repeating the class outranks it regardless of
     stylesheet order, so the card follows the active theme. */
  background: var(--otk-tour-bg);
  color: var(--otk-tour-ink);
  border: 1px solid var(--otk-tour-line);
  box-shadow: 0 18px 44px rgba(9, 13, 20, 0.24);
}

.driver-popover.driver-popover .driver-popover-arrow-side-left.driver-popover-arrow {
  border-left-color: var(--otk-tour-bg);
}
.driver-popover.driver-popover .driver-popover-arrow-side-right.driver-popover-arrow {
  border-right-color: var(--otk-tour-bg);
}
.driver-popover.driver-popover .driver-popover-arrow-side-top.driver-popover-arrow {
  border-top-color: var(--otk-tour-bg);
}
.driver-popover.driver-popover .driver-popover-arrow-side-bottom.driver-popover-arrow {
  border-bottom-color: var(--otk-tour-bg);
}

.driver-popover .driver-popover-title,
.driver-popover .driver-popover-description {
  color: inherit;
  font-family: inherit;
}

.driver-popover .driver-popover-title {
  font-size: 20px;
  font-weight: 700;
  line-height: 1.28;
  margin: 0 0 10px;
  /* Leave room for the absolute close button instead of letting it sit on
     top of the first word. */
  padding-right: 28px;
  overflow-wrap: anywhere;
}

.driver-popover .driver-popover-description {
  font-size: 16px;
  line-height: 1.65;
  color: var(--otk-tour-muted);
  margin: 0 0 16px;
  overflow-wrap: anywhere;
}

@media (min-width: 600px) {
  .driver-popover .driver-popover-description {
    font-size: 16.5px;
  }
}

/* Step media: a generated, step-aware SVG (or a video) at the top of the
   card. The aspect ratio is fixed so late layout never jumps. */
.otk-tour-media {
  margin: 0 0 14px;
  padding: 0;
  overflow: hidden;
  border-radius: 10px;
  border: 1px solid var(--otk-tour-line);
  background: #0a1a27;
  aspect-ratio: 12 / 5;
  line-height: 0;
}

.otk-tour-media--art svg {
  display: block;
  width: 100%;
  height: 100%;
}

.otk-tour-image,
.otk-tour-video {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 0;
}

/* Progress: a thin bar plus "3 of 6". */
.driver-popover .driver-popover-progress-text {
  font-size: 12.5px;
  font-weight: 600;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  color: var(--otk-tour-muted);
  margin: 0 0 8px;
}

.driver-popover .driver-popover-progress-text::before {
  content: "";
  display: block;
  height: 3px;
  width: 100%;
  margin-bottom: 6px;
  border-radius: 999px;
  background: linear-gradient(
    90deg,
    var(--otk-tour-accent) 0%,
    var(--otk-tour-accent) var(--otk-tour-progress, 0%),
    var(--otk-tour-line) var(--otk-tour-progress, 0%),
    var(--otk-tour-line) 100%
  );
}

/* Buttons
   The button row wraps instead of shrinking: labels never truncate and no
   control is pushed off the card on a narrow phone. */
.driver-popover .driver-popover-footer {
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
}

.driver-popover .driver-popover-navigation-btns {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  align-items: center;
  gap: 8px;
  margin-left: auto;
}

.driver-popover .driver-popover-navigation-btns button {
  flex: 0 1 auto;
  min-width: 0;
  white-space: nowrap;
  font-family: inherit;
  font-size: 15px;
  font-weight: 600;
  border-radius: calc(var(--radius-md, 10px));
  padding: 11px 18px;
  min-height: 44px;
  border: 1px solid var(--otk-tour-line);
  background: transparent;
  color: var(--otk-tour-ink);
  text-shadow: none;
  cursor: pointer;
}

.driver-popover .driver-popover-navigation-btns button + button {
  margin-left: 0;
}

.driver-popover .driver-popover-navigation-btns button:focus-visible {
  outline: 2px solid var(--otk-tour-ring);
  outline-offset: 2px;
}

.driver-popover .driver-popover-next-btn {
  background: var(--otk-tour-accent);
  border-color: var(--otk-tour-accent);
  color: var(--otk-tour-on-accent);
}

.driver-popover .driver-popover-next-btn:hover {
  filter: brightness(1.08);
}

.driver-popover .driver-popover-close-btn {
  color: var(--otk-tour-muted);
  font-size: 18px;
  line-height: 1;
}

.driver-popover .driver-popover-close-btn:hover,
.driver-popover .driver-popover-close-btn:focus-visible {
  color: var(--otk-tour-ink);
}

/* "Skip tour" is a real, full-width secondary button: easy to see and hit,
   especially on a phone where the tour card is a bottom sheet. */
.otk-tour-skip {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  margin-top: 12px;
  padding: 12px 18px;
  min-height: 46px;
  border: 1.5px solid var(--otk-tour-line);
  border-radius: calc(var(--radius-md, 10px));
  background: var(--otk-tour-bg);
  font-family: inherit;
  font-size: 15px;
  font-weight: 700;
  letter-spacing: 0.01em;
  color: var(--otk-tour-ink);
  text-decoration: none;
  cursor: pointer;
}

.otk-tour-skip:hover,
.otk-tour-skip:focus-visible {
  border-color: var(--otk-tour-ring);
  color: var(--otk-tour-ink);
  background: rgba(255, 138, 31, 0.08);
}

.otk-tour-skip:focus-visible {
  outline: 2px solid var(--otk-tour-ring);
  outline-offset: 2px;
}

.otk-tour-hint {
  margin: 10px 0 0;
  font-size: 13px;
  line-height: 1.5;
  color: var(--otk-tour-muted);
}

/* Dark theme: same structure, deeper ground (matches theme.css). */
[data-theme="dark"] .driver-popover {
  --otk-tour-bg: var(--surface, #0c1d2e);
  --otk-tour-ink: var(--ink, #e7eef7);
  --otk-tour-muted: var(--muted, #9fb3c8);
  --otk-tour-line: var(--line, #1e3a52);
  --otk-tour-accent: var(--brand-orange-400, #ff7f11);
  --otk-tour-on-accent: #0b1724;
  --otk-tour-ring: var(--brand-orange-400, #ff7f11);

  box-shadow: 0 18px 44px rgba(0, 0, 0, 0.55);
}

@media (prefers-color-scheme: dark) {
  html:not([data-theme="light"]) .driver-popover {
    --otk-tour-bg: #0c1d2e;
    --otk-tour-ink: #e7eef7;
    --otk-tour-muted: #9fb3c8;
    --otk-tour-line: #1e3a52;
    --otk-tour-accent: var(--brand-orange-400, #ff7f11);
    --otk-tour-on-accent: #0b1724;
    --otk-tour-ring: var(--brand-orange-400, #ff7f11);
  }
}

/* --- phones: the tooltip becomes a bottom sheet ------------------------ */
@media (max-width: 600px) {
  .driver-popover.driver-popover {
    position: fixed !important;
    left: 8px !important;
    right: 8px !important;
    bottom: calc(8px + env(safe-area-inset-bottom, 0px)) !important;
    top: auto !important;
    max-width: none;
    /* Leave the top third of the phone free for the highlighted element;
       the card itself scrolls if a step has more copy than fits. */
    max-height: min(66vh, 560px);
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    margin: 0 !important;
    padding: 14px 14px 12px;
    transform: none !important;
  }

  .otk-tour-media {
    /* Keep the same 12:5 frame as desktop so the generated artwork is never
       cropped; the extra height saved on a phone goes to the copy. */
    aspect-ratio: 12 / 5;
  }

  .driver-popover .driver-popover-description {
    margin-bottom: 12px;
    font-size: 15.5px;
    line-height: 1.6;
  }

  .driver-popover .driver-popover-title {
    font-size: 18px;
  }

  .otk-tour-skip {
    min-height: 48px;
    font-size: 15.5px;
  }

  .driver-popover .driver-popover-navigation-btns {
    width: 100%;
    justify-content: stretch;
  }

  .driver-popover .driver-popover-navigation-btns button {
    flex: 1 1 0;
    padding-inline: 10px;
  }

}

/* --- replay controls injected into the existing menus ----------------- */
.otk-tour-replay,
.otk-tour-replay-li > a {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  font-family: inherit;
  font-size: 14px;
  font-weight: 500;
  color: inherit;
  background: none;
  border: 0;
  text-align: left;
  padding: 10px 12px;
  border-radius: calc(var(--radius-md, 10px));
  cursor: pointer;
  text-decoration: none;
}

.otk-tour-replay:hover,
.otk-tour-replay:focus-visible,
.otk-tour-replay-li > a:hover,
.otk-tour-replay-li > a:focus-visible {
  background: rgba(11, 60, 93, 0.08);
}

/* --- screen-reader step announcement --------------------------------- */
.otk-sr-only {
  position: absolute !important;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

/* --- reduced motion --------------------------------------------------- */
@media (prefers-reduced-motion: reduce) {
  .driver-popover,
  .driver-overlay,
  .driver-active-element,
  .driver-highlighted-element {
    transition: none !important;
    animation: none !important;
  }
}

/* Theme-aware tour card. theme.js mirrors the theme onto <body> as well as
   <html>, so both roots have to be covered; the appended fallback used to
   paint a white card over a body-only dark theme. */
:root[data-theme="dark"] .driver-popover,
body[data-theme="dark"] .driver-popover {
  --otk-tour-bg: #111a24;
  --otk-tour-ink: #eaf1f7;
  --otk-tour-muted: #a9b8c6;
  --otk-tour-line: #2a3947;
  --otk-tour-accent: #8ec5ff;
  --otk-tour-on-accent: #0b1724;
  box-shadow: 0 18px 44px rgba(0, 0, 0, 0.55);
}
:root[data-theme="light"] .driver-popover,
body[data-theme="light"] .driver-popover {
  --otk-tour-bg: #ffffff;
  --otk-tour-ink: #0f172a;
  --otk-tour-muted: #52606d;
  --otk-tour-line: #e4e6ea;
  --otk-tour-on-accent: #ffffff;
}

/* --- page tour launcher ------------------------------------------------
   One small control, present only while tours are enabled. It sits above the
   mobile bottom nav and never covers page actions on a wide screen. */
.otk-tour-launch {
  position: fixed;
  right: 18px;
  bottom: 18px;
  z-index: 9000;
  font-family: var(--otk-tour-font, "Poppins", "Segoe UI", sans-serif);
  touch-action: none; /* the button is dragged, not scrolled */
}

/* While a drag is in progress the whole control follows the pointer. */
.otk-tour-launch__btn { cursor: grab; touch-action: none; }
.otk-tour-launch__btn.is-dragging { cursor: grabbing; }
.otk-tour-launch__btn.is-dragging:hover { filter: none; }

.otk-tour-launch__btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 10px 15px;
  border-radius: 999px;
  border: 1px solid var(--otk-tour-line, rgba(15, 23, 42, 0.14));
  background: var(--otk-tour-accent, #0b3c5d);
  color: var(--otk-tour-on-accent, #ffffff);
  font: inherit;
  font-size: 0.86rem;
  font-weight: 600;
  line-height: 1;
  cursor: pointer;
  box-shadow: 0 10px 26px rgba(9, 17, 28, 0.28);
}

.otk-tour-launch__btn:hover,
.otk-tour-launch__btn:focus-visible {
  filter: brightness(1.08);
  outline: 3px solid var(--otk-tour-ring, #ff8a1f);
  outline-offset: 2px;
}

.otk-tour-launch__menu {
  position: absolute;
  right: 0;
  bottom: calc(100% + 10px);
  width: min(320px, calc(100vw - 32px));
  max-height: min(70vh, 520px);
  overflow-y: auto;
  padding: 14px;
  border-radius: 14px;
  border: 1px solid var(--otk-tour-line, #e4e6ea);
  background: var(--otk-tour-bg, #ffffff);
  color: var(--otk-tour-ink, #0f172a);
  box-shadow: 0 20px 44px rgba(9, 17, 28, 0.24);
  text-align: left;
}

.otk-tour-launch__menu[hidden] { display: none; }

.otk-tour-launch__title {
  margin: 0 0 10px;
  font-size: 0.82rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  color: var(--otk-tour-muted, #52606d);
}

.otk-tour-launch__all,
.otk-tour-launch__item {
  display: block;
  width: 100%;
  margin: 0 0 6px;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--otk-tour-line, #e4e6ea);
  background: transparent;
  color: inherit;
  font: inherit;
  font-size: 0.9rem;
  text-align: left;
  cursor: pointer;
}

.otk-tour-launch__all {
  font-weight: 700;
  border-color: transparent;
  background: var(--otk-tour-accent, #0b3c5d);
  color: var(--otk-tour-on-accent, #ffffff);
}

.otk-tour-launch__item:hover,
.otk-tour-launch__all:hover,
.otk-tour-launch__item:focus-visible,
.otk-tour-launch__all:focus-visible {
  filter: brightness(1.08);
  outline: 2px solid var(--otk-tour-ring, #ff8a1f);
  outline-offset: 1px;
}

.otk-tour-launch__note {
  margin: 10px 2px 0;
  font-size: 0.76rem;
  line-height: 1.5;
  color: var(--otk-tour-muted, #52606d);
}

/* Sequence position, shown above the first step of every tour in a run. */
.otk-tour-seq {
  margin: 0 0 8px;
  font-size: 0.74rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--otk-tour-muted, #52606d);
}

@media (max-width: 767px) {
  .otk-tour-launch {
    right: 12px;
    bottom: calc(76px + env(safe-area-inset-bottom, 0px));
  }
  .otk-tour-launch__btn { padding: 9px 13px; font-size: 0.82rem; }
  .otk-tour-launch__menu { max-height: 60vh; }
}

@media (prefers-reduced-motion: reduce) {
  .otk-tour-launch__btn,
  .otk-tour-launch__menu { transition: none; }
}


/* --- phone layout -------------------------------------------------------
   On a phone the pill would sit on top of the create (+) button, so it
   becomes a compact icon-only control placed just above the + button and
   BELOW it in the stack: the + always wins the tap, the tour control never
   hides it. It stays draggable; a moved position is remembered. */
@media (max-width: 700px) {
  .otk-tour-launch {
    right: 16px;
    bottom: calc(var(--mbn-total-height, 68px) + 14px + 54px + 12px);
    z-index: 12499; /* one below .mbn-fab (12501) */
  }

  .otk-tour-launch__btn {
    width: 46px;
    height: 46px;
    padding: 0;
    border-radius: 50%;
    justify-content: center;
    gap: 0;
  }

  /* SVG only — the label lives in aria-label for screen readers. */
  .otk-tour-launch__btn span {
    position: absolute;
    width: 1px;
    height: 1px;
    margin: -1px;
    padding: 0;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
    border: 0;
  }

  .otk-tour-launch__btn svg { width: 22px; height: 22px; }

  .otk-tour-launch__menu {
    right: 0;
    bottom: calc(100% + 10px);
    width: min(320px, calc(100vw - 32px));
  }
}

/* Admin pages have no create button; keep the icon clear of the nav rail. */
@media (max-width: 700px) {
  html.mbn-context-admin .otk-tour-launch {
    bottom: calc(var(--mbn-total-height, 68px) + 16px);
  }
}
