/*
 * Segmented toggle strips (btn-primary vs btn-outline-*).
 *
 * Outline buttons use a 1.5px border (see components/buttons.css); solid primary
 * had no border — toggling classes changes the box and the row shifts.
 *
 * Patterns:
 *   segmented-toggle segmented-toggle--capsule — connected capsule (use segmented_capsule_tag)
 *     Modifiers: --capsule--full, --capsule--comfortable, --capsule--cols-2
 *     Mobile: default 3 cols/row; 2 or 4 segments use 2 cols/row (2×2)
 *     Entity sub-nav: segmented-capsule controller balances wrap rows (4→2+2, 7→4+3)
 *   btn-group segmented-toggle — locale/theme only (2 segments)
 */

/* Match solid CTA green (--btn-primary-*) so outlines aren’t the dustier --accent-primary ring. */
.segmented-toggle > .btn.btn-primary {
  border: 1.5px solid var(--btn-primary-bg);
}

.segmented-toggle > .btn.btn-primary:hover,
.segmented-toggle > .btn.btn-primary:focus,
.segmented-toggle > .btn.btn-primary:focus-visible,
.segmented-toggle > .btn.btn-primary:active,
.segmented-toggle > .btn.btn-primary.active {
  border-color: var(--btn-primary-hover);
}

/* Same weight in both states — avoids text reflow when switching. */
.segmented-toggle > .btn.btn-primary,
.segmented-toggle > .btn.btn-outline-primary,
.segmented-toggle > .btn.btn-outline-secondary {
  font-weight: 500;
}

/*
 * Connected capsule — 1px grid gutters; desktop single row (or full-width equal columns).
 * Narrow viewports: --segmented-capsule-cols (default 3) per row; 2 or 4 segments → 2 cols.
 * Balanced wraps (data-controller=segmented-capsule): JS sets --segmented-capsule-cols so rows stay even.
 */
.segmented-toggle--capsule {
  --segmented-capsule-cols: 3;
  /* ~6.5rem so phone widths (~320–390px) still fit 3/row; JS balancer reads this. */
  --segmented-capsule-min-col: 6.5rem;
  display: grid;
  gap: 1px;
  width: 100%;
  max-width: 100%;
  padding: 0;
  border: 1px solid var(--border-primary);
  border-radius: var(--radius-lg);
  overflow: hidden;
  background: var(--border-primary);
}

.segmented-toggle--capsule--cols-2 {
  --segmented-capsule-cols: 2;
}

.segmented-toggle--capsule--full,
.segmented-toggle--capsule.w-100 {
  width: 100%;
}

.segmented-toggle--capsule > .btn {
  margin: 0;
  border: none;
  border-radius: 0;
  text-align: center;
  white-space: nowrap;
  padding: var(--space-2) var(--space-4);
}

.segmented-toggle--capsule > .btn.btn-outline-primary {
  border-color: transparent;
  background-color: var(--bg-elevated);
  color: var(--accent-primary);
}

.segmented-toggle--capsule > .btn.btn-outline-primary:hover {
  background: var(--accent-primary);
  color: #fff;
}

.segmented-toggle--capsule > .btn.btn-outline-secondary {
  border-color: transparent;
  background-color: var(--bg-elevated);
  color: var(--text-secondary);
}

.segmented-toggle--capsule > .btn.btn-outline-secondary:hover {
  background: var(--btn-outline-secondary-hover-bg);
  border-color: transparent;
  color: var(--btn-outline-secondary-hover-color);
}

/*
 * After Turbo navigation the pointer often rests over an inactive capsule segment (e.g. the
 * second column) and :hover reads as a false “selection”. Suppress until the user moves the
 * pointer or uses the keyboard (see application.js lockPointerNavHoverUntilPointerMove).
 */
body.pointer-nav-hover-locked .segmented-toggle--capsule > .btn.btn-outline-secondary:hover,
body.pointer-nav-hover-locked .segmented-toggle--capsule > .btn.btn-outline-primary:hover {
  background-color: var(--bg-elevated);
  border-color: transparent;
  color: var(--text-secondary);
}

body.pointer-nav-hover-locked .segmented-toggle--capsule > .btn.btn-primary:hover {
  background: var(--btn-primary-bg);
  color: #fff;
}

/* Mouse-click :focus on inactive segments — match resting state (bootstrap .btn:focus ring is cleared below). */
.segmented-toggle--capsule > .btn.btn-outline-secondary:focus:not(:focus-visible),
.segmented-toggle--capsule > .btn.btn-outline-primary:focus:not(:focus-visible) {
  background-color: var(--bg-elevated);
  border-color: transparent;
  color: var(--text-secondary);
  box-shadow: none !important;
}

.segmented-toggle--capsule > .btn.btn-outline-primary:focus:not(:focus-visible) {
  color: var(--accent-primary);
}

.segmented-toggle--capsule > .btn.btn-primary:focus:not(:focus-visible) {
  background: var(--btn-primary-bg);
  color: #fff;
  box-shadow: none !important;
}

.segmented-toggle--capsule > .btn:disabled,
.segmented-toggle--capsule > .btn.disabled {
  opacity: 0.55;
  cursor: not-allowed;
  pointer-events: none;
}

.segmented-toggle--capsule > .btn.btn-outline-secondary:disabled,
.segmented-toggle--capsule > .btn.btn-outline-secondary.disabled {
  background-color: var(--bg-elevated);
  color: var(--text-muted);
}

.segmented-toggle--capsule > .btn.btn-primary {
  border: none;
  background: var(--btn-primary-bg);
  color: #fff;
}

.segmented-toggle--capsule > .btn.btn-primary:hover {
  background: var(--btn-primary-hover);
  color: #fff;
}

/*
 * Capsule segments: no outer focus ring (clashes with grid gutters / looks like a left edge).
 * Keyboard users get an inset ring; mouse click does not leave a persistent halo.
 */
.segmented-toggle--capsule > .btn:focus {
  outline: none !important;
  outline-offset: 0 !important;
}

.segmented-toggle--capsule > .btn:focus:not(:focus-visible) {
  box-shadow: none !important;
}

.segmented-toggle--capsule > .btn:focus-visible {
  outline: none !important;
  outline-offset: 0 !important;
  box-shadow: inset 0 0 0 2px rgba(var(--btn-primary-rgb), 0.55) !important;
  position: relative;
  z-index: 1;
}

.segmented-toggle--capsule > .btn.btn-outline-primary:focus-visible,
.segmented-toggle--capsule > .btn.btn-outline-secondary:focus-visible {
  background-color: var(--bg-elevated);
  color: inherit;
}

.segmented-toggle--capsule > .btn.btn-outline-primary:focus-visible {
  color: var(--accent-primary);
}

.segmented-toggle--capsule > .btn.btn-outline-secondary:focus-visible {
  color: var(--text-secondary);
}

.segmented-toggle--capsule > .btn.btn-primary:focus-visible {
  background: var(--btn-primary-bg);
  color: #fff;
  box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.75) !important;
}

.segmented-toggle--capsule--comfortable > .btn {
  min-height: 3.25rem;
  padding-top: var(--space-3);
  padding-bottom: var(--space-3);
  white-space: normal;
  line-height: 1.25;
}

@media (min-width: 768px) {
  .segmented-toggle--capsule:not(.segmented-toggle--capsule--full):not(.w-100) {
    display: inline-grid;
    width: auto;
    max-width: 100%;
    grid-auto-flow: column;
    grid-auto-columns: auto;
    grid-template-columns: none;
  }

  .segmented-toggle--capsule--full,
  .segmented-toggle--capsule.w-100 {
    grid-auto-flow: column;
    grid-auto-columns: 1fr;
    grid-template-columns: none;
  }

  /*
   * Entity workspace sub-nav can grow to many tabs. Wrap by default; JS
   * (segmented-capsule) then picks a balanced column count for the viewport.
   */
  .segmented-toggle--capsule.entity-sub-nav {
    grid-auto-flow: row;
    grid-auto-columns: unset;
    grid-template-columns: repeat(var(--segmented-capsule-cols), minmax(0, 1fr));
  }

  /* CSS fallback before JS: 4→2+2; 5–6 keep 3/row. */
  .segmented-toggle--capsule.entity-sub-nav:has(> :first-child:nth-last-child(4)) {
    --segmented-capsule-cols: 2;
  }

  .segmented-toggle--capsule.entity-sub-nav:has(> :first-child:nth-last-child(5)),
  .segmented-toggle--capsule.entity-sub-nav:has(> :first-child:nth-last-child(6)) {
    --segmented-capsule-cols: 3;
  }

  .segmented-toggle--capsule.entity-sub-nav:has(> :first-child:nth-last-child(7)),
  .segmented-toggle--capsule.entity-sub-nav:has(> :first-child:nth-last-child(8)) {
    --segmented-capsule-cols: 4;
  }

  .segmented-toggle--capsule.entity-sub-nav:has(> :first-child:nth-last-child(9)),
  .segmented-toggle--capsule.entity-sub-nav:has(> :first-child:nth-last-child(n + 10)) {
    --segmented-capsule-cols: 5;
  }

  .segmented-toggle--capsule.entity-sub-nav > .btn {
    white-space: normal;
    line-height: 1.25;
  }
}

/* JS-balanced capsules (entity sub-nav, etc.): wrap with even row lengths. */
.segmented-toggle--capsule[data-segmented-capsule-balanced="true"] {
  grid-auto-flow: row;
  grid-auto-columns: unset;
  grid-template-columns: repeat(var(--segmented-capsule-cols), minmax(0, 1fr));
}

.segmented-toggle--capsule[data-segmented-capsule-balanced="true"] > .btn {
  white-space: normal;
  line-height: 1.25;
}

@media (max-width: 767.98px) {
  .segmented-toggle--capsule {
    grid-template-columns: repeat(var(--segmented-capsule-cols), minmax(0, 1fr));
    grid-auto-flow: row;
  }

  /* Fallback when JS isn't balancing: 2 or 4 segments → 2 cols (2×2). */
  .segmented-toggle--capsule:not([data-segmented-capsule-balanced="true"]):has(> :first-child:nth-last-child(2)),
  .segmented-toggle--capsule:not([data-segmented-capsule-balanced="true"]):has(> :first-child:nth-last-child(4)) {
    --segmented-capsule-cols: 2;
  }

  .segmented-toggle--capsule:not(.segmented-toggle--capsule--comfortable) > .btn {
    white-space: normal;
    line-height: 1.25;
    padding: var(--space-2) var(--space-3);
  }

  .segmented-toggle--capsule--span-last > .btn:last-child:nth-child(3) {
    grid-column: 1 / -1;
  }
}

/* Admin org units index: stack filter bars vertically. */
.admin-org-units-index-filters {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  width: 100%;
}

.admin-org-units-index-filters > .segmented-toggle--capsule {
  margin-bottom: 0;
}

.admin-org-units-index-filters > .segmented-toggle--capsule:last-child {
  margin-bottom: var(--space-3);
}
