/*
 * Reusable UI pieces (SPEC 78-82).
 *
 * SPEC 81: selected state uses the gradient sparingly — a tinted border and a
 * gradient check badge, never a fully gradient-filled card. SPEC 74 wants the
 * surface to stay mostly white with light borders and discreet shadows.
 */

/* ------------------------------ option cards --------------------------- */

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(148px, 1fr));
  gap: var(--space-3);
}

.card-list {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

.option-card {
  position: relative;
  display: flex;
  text-align: left;
  background: var(--surface);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-lg);
  transition:
    border-color var(--duration-fast) var(--ease-out),
    background-color var(--duration-fast) var(--ease-out),
    box-shadow var(--duration-fast) var(--ease-out);
}

.option-card--grid {
  flex-direction: column;
  align-items: flex-start;
  gap: var(--space-2);
  padding: var(--space-4);
  min-height: 116px;
}

.option-card--row {
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  width: 100%;
}

.option-card:hover {
  border-color: var(--border-strong);
  background: var(--surface-secondary);
}

.option-card[aria-checked='true'] {
  border-color: transparent;
  background: var(--accent-soft);
  box-shadow: 0 0 0 1.5px var(--accent);
}

.option-card[data-blocked='true'] {
  opacity: 0.5;
}

.option-card[data-blocked='true']:hover {
  border-color: var(--border-default);
  background: var(--surface);
}

.option-card__icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  flex: none;
  border-radius: var(--radius-md);
  color: var(--text-secondary);
  background: var(--surface-muted);
}

.option-card[aria-checked='true'] .option-card__icon {
  color: var(--text-on-accent);
  background: var(--gradient-primary);
}

.option-card__text {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.option-card__label {
  font-size: var(--text-body);
  font-weight: var(--weight-label);
  color: var(--text-primary);
}

.option-card__description {
  font-size: var(--text-helper);
  color: var(--text-muted);
  line-height: var(--leading-normal);
}

.option-card__check {
  position: absolute;
  top: 10px;
  right: 10px;
  display: none;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 20px;
  border-radius: var(--radius-pill);
  color: var(--text-on-accent);
  background: var(--gradient-primary);
}

.option-card--row .option-card__check {
  position: static;
  margin-left: auto;
  flex: none;
}

.option-card[aria-checked='true'] .option-card__check {
  display: inline-flex;
}

/* --------------------------------- chips ------------------------------- */

.chip-row {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 13px;
  border-radius: var(--radius-pill);
  border: 1px solid var(--border-default);
  background: var(--surface);
  font-size: var(--text-label);
  font-weight: var(--weight-medium);
  color: var(--text-secondary);
  transition:
    border-color var(--duration-fast) var(--ease-out),
    color var(--duration-fast) var(--ease-out),
    background-color var(--duration-fast) var(--ease-out);
}

.chip:hover {
  border-color: var(--border-strong);
  color: var(--text-primary);
}

.chip[aria-checked='true'] {
  color: var(--accent-deep);
  background: var(--accent-soft);
  border-color: transparent;
  box-shadow: 0 0 0 1.5px var(--accent);
  font-weight: var(--weight-label);
}

.chip[data-blocked='true'] {
  opacity: 0.45;
}

.chip__check {
  display: inline-flex;
  color: var(--accent);
}

.count-badge {
  font-size: var(--text-helper);
  font-weight: var(--weight-label);
  color: var(--text-muted);
  padding: 3px 9px;
  border-radius: var(--radius-pill);
  background: var(--surface-muted);
  white-space: nowrap;
}

.count-badge[data-full='true'] {
  color: var(--accent-deep);
  background: var(--accent-soft);
}

/* ----------------------------- suggestions ----------------------------- */

.suggestion-row {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
}

.suggestion {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 12px;
  border-radius: var(--radius-md);
  border: 1px dashed var(--border-strong);
  background: var(--surface);
  font-size: var(--text-label);
  color: var(--text-secondary);
  text-align: left;
  transition:
    border-color var(--duration-fast) var(--ease-out),
    color var(--duration-fast) var(--ease-out);
}

.suggestion:hover {
  border-style: solid;
  border-color: var(--accent);
  color: var(--accent-deep);
}

.link-button {
  color: var(--accent);
  font-weight: var(--weight-label);
  text-decoration: underline;
  font-size: inherit;
}

/* -------------------------------- fields ------------------------------- */

.field-stack {
  display: flex;
  flex-direction: column;
  gap: var(--space-5);
}

.field__header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--space-3);
  margin-bottom: 6px;
}

.field__count {
  font-size: var(--text-helper);
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.field__count[data-near='true'] {
  color: var(--warning);
}

.field__footer {
  margin-top: 5px;
  min-height: 16px;
}

.label-with-tooltip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
}

/* -------------------------------- tooltip ------------------------------ */

.tooltip {
  position: relative;
  display: inline-flex;
}

.tooltip__trigger {
  display: inline-flex;
  align-items: center;
  color: var(--text-disabled);
  border-radius: var(--radius-pill);
}

.tooltip__trigger:hover {
  color: var(--accent);
}

.tooltip__bubble {
  position: absolute;
  bottom: calc(100% + 8px);
  left: 50%;
  transform: translateX(-50%);
  z-index: var(--z-drawer);
  width: max-content;
  max-width: 260px;
  padding: var(--space-3);
  font-size: var(--text-helper);
  font-weight: var(--weight-regular);
  line-height: var(--leading-normal);
  color: var(--text-primary);
  background: var(--surface);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
}

.tooltip__bubble[hidden] {
  display: none;
}

/* -------------------------------- slider ------------------------------- */

/*
 * Nine sliders is a lot to set one by one, so the presets come first: one click
 * moves all of them, and the summary underneath reads the result back as a
 * sentence, which is the only form in which nine numbers mean anything.
 *
 * The pill row is shared with the Soul step's base souls, which is the same
 * gesture applied to a different set of fields — one row of "start from this and
 * adjust". Keeping one class keeps the two reading as the same affordance.
 */
.behavior-presets {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: var(--space-5);
}

.behavior-preset {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px var(--space-3);
  font-size: var(--text-label);
  color: var(--text-secondary);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-pill);
  background: var(--surface);
  transition:
    color var(--duration-fast) var(--ease-out),
    border-color var(--duration-fast) var(--ease-out);
}

.behavior-preset:hover {
  color: var(--accent-deep);
  border-color: var(--accent);
  background: var(--accent-soft);
}

.behavior-summary {
  margin-top: var(--space-5);
  padding: var(--space-4);
  font-size: var(--text-label);
  line-height: var(--leading-relaxed);
  color: var(--text-primary);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  background: var(--surface-secondary);
}

.behavior-summary__tag {
  display: inline-block;
  margin-right: 8px;
  padding: 2px 8px;
  font-size: 11px;
  font-weight: var(--weight-label);
  color: var(--accent-deep);
  background: var(--accent-soft-strong);
  border-radius: var(--radius-pill);
}

.slider-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: var(--space-5);
}

.slider {
  padding: var(--space-4);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  background: var(--surface);
}

.slider__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
}

.slider__label {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--text-primary);
}

.slider__readout {
  font-size: var(--text-helper);
  font-weight: var(--weight-label);
  color: var(--accent-deep);
  background: var(--accent-soft);
  padding: 3px 9px;
  border-radius: var(--radius-pill);
  white-space: nowrap;
}

.slider__question {
  margin: 5px 0 var(--space-3);
}

.slider__track-row {
  display: flex;
  align-items: center;
  gap: var(--space-3);
}

.slider__end {
  flex: none;
  font-size: 11px;
}

.slider__input {
  flex: 1;
  min-width: 0;
  appearance: none;
  height: 6px;
  border-radius: var(--radius-pill);
  background: linear-gradient(
    to right,
    var(--accent) 0%,
    var(--accent) var(--slider-fill, 50%),
    var(--surface-muted) var(--slider-fill, 50%),
    var(--surface-muted) 100%
  );
}

.slider__input::-webkit-slider-thumb {
  appearance: none;
  width: 18px;
  height: 18px;
  border-radius: var(--radius-pill);
  background: var(--surface);
  border: 2px solid var(--accent);
  box-shadow: var(--shadow-sm);
  cursor: grab;
}

.slider__input::-moz-range-thumb {
  width: 16px;
  height: 16px;
  border-radius: var(--radius-pill);
  background: var(--surface);
  border: 2px solid var(--accent);
  box-shadow: var(--shadow-sm);
  cursor: grab;
}

/* --------------------------------- toasts ------------------------------ */

.toast-region {
  position: fixed;
  right: var(--space-5);
  bottom: var(--space-5);
  z-index: var(--z-toast);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  pointer-events: none;
}

.toast {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  min-width: 280px;
  max-width: 420px;
  padding: var(--space-3) var(--space-4);
  background: var(--surface);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  font-size: var(--text-label);
  pointer-events: auto;
  animation: toast-in 160ms var(--ease-out);
}

.toast[data-leaving='true'] {
  opacity: 0;
  transform: translateY(6px);
  transition: opacity 150ms var(--ease-out), transform 150ms var(--ease-out);
}

@keyframes toast-in {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
}

.toast__icon {
  display: inline-flex;
  color: var(--text-secondary);
}

.toast--success .toast__icon {
  color: var(--success);
}

.toast--error .toast__icon {
  color: var(--danger);
}

.toast__message {
  flex: 1;
}

.toast__action {
  font-weight: var(--weight-label);
  color: var(--accent);
  white-space: nowrap;
}

.toast__close {
  display: inline-flex;
  color: var(--text-disabled);
  padding: 2px;
  border-radius: var(--radius-sm);
}

.toast__close:hover {
  color: var(--text-primary);
}

/* --------------------------------- dialog ------------------------------ */

/*
 * The `* { margin: 0 }` reset wipes out the UA stylesheet's `margin: auto` on
 * <dialog>, which is what centres a modal in the viewport. It has to be put
 * back explicitly.
 */
.dialog,
.palette,
.gallery {
  margin: auto;
  padding: 0;
  border: 1px solid var(--border-default);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-lg);
  background: var(--surface);
  color: var(--text-primary);
}

.dialog::backdrop,
.palette::backdrop,
.gallery::backdrop {
  background: rgba(16, 16, 16, 0.32);
  backdrop-filter: blur(2px);
}

.dialog {
  width: min(440px, calc(100vw - 32px));
}

.dialog__body {
  padding: var(--space-6) var(--space-6) var(--space-4);
}

.dialog__description {
  margin-top: var(--space-2);
  color: var(--text-secondary);
  line-height: var(--leading-relaxed);
}

.dialog__actions {
  display: flex;
  justify-content: flex-end;
  gap: var(--space-2);
  padding: var(--space-4) var(--space-6) var(--space-6);
}

/* ------------------------------ choice cards ---------------------------- */

/* Three ways to start an agent: wider than a confirmation, still a dialog. */
.dialog--choices {
  width: min(520px, calc(100vw - 32px));
}

/*
 * An editor rather than a question: the knowledge dialog puts a Markdown source
 * column beside its preview, and neither is readable at confirmation width.
 *
 * It is also the only dialog tall enough to outgrow a short screen, so the body
 * scrolls and the actions stay pinned where they can be reached. The [open]
 * qualifier matters: a bare `display: flex` would override the UA rule that keeps
 * a closed dialog hidden.
 */
.dialog--wide {
  width: min(920px, calc(100vw - 32px));
  max-height: calc(100dvh - 32px);
}

.dialog--wide[open] {
  display: flex;
  flex-direction: column;
}

.dialog--wide .dialog__body {
  min-height: 0;
  overflow-y: auto;
}

.choice-list {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  margin-top: var(--space-5);
}

.choice-card {
  display: flex;
  align-items: flex-start;
  gap: var(--space-3);
  width: 100%;
  padding: var(--space-4);
  text-align: left;
  border: 1px solid var(--border-default);
  border-radius: var(--radius-lg);
  background: var(--surface);
  transition:
    border-color var(--duration-fast) var(--ease-out),
    box-shadow var(--duration-base) var(--ease-out),
    transform var(--duration-fast) var(--ease-out);
}

.choice-card:hover {
  border-color: transparent;
  box-shadow:
    0 0 0 1.5px var(--accent),
    var(--shadow-md);
}

.choice-card:active {
  transform: translateY(1px);
}

.choice-card__icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  flex: none;
  border-radius: var(--radius-md);
  color: var(--accent-deep);
  background: var(--surface-muted);
  transition: background-color var(--duration-fast) var(--ease-out);
}

.choice-card:hover .choice-card__icon {
  background: var(--accent-soft);
}

.choice-card__text {
  display: flex;
  flex-direction: column;
  gap: 3px;
  flex: 1;
  min-width: 0;
}

.choice-card__label {
  font-size: var(--text-body);
  font-weight: var(--weight-label);
  color: var(--text-primary);
}

.choice-card__description {
  font-size: var(--text-helper);
  line-height: var(--leading-normal);
  color: var(--text-secondary);
}

.choice-card__go {
  display: inline-flex;
  flex: none;
  margin-top: 9px;
  color: var(--text-disabled);
  transition:
    color var(--duration-fast) var(--ease-out),
    transform var(--duration-fast) var(--ease-out);
}

.choice-card:hover .choice-card__go {
  color: var(--accent);
  transform: translateX(2px);
}

/* ----------------------------- template gallery ------------------------- */

/*
 * A carousel rather than a long list: a page of six can be compared at a
 * glance, and paging keeps the modal a fixed size instead of growing with the
 * catalogue.
 */
.gallery {
  display: flex;
  flex-direction: column;
  width: min(1040px, calc(100vw - 32px));
  /*
   * Fixed, like a slide: every page holds six cards, so letting the modal size
   * itself would make it twitch by a line whenever a tagline wrapped.
   */
  height: min(516px, calc(100vh - 48px));
  overflow: hidden;
}

.gallery__header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-4);
  flex: none;
  padding: var(--space-5) var(--space-6);
  border-bottom: 1px solid var(--border-subtle);
}

.gallery__title {
  margin-bottom: 2px;
}

.gallery__close {
  display: inline-flex;
  flex: none;
  padding: 8px;
  border-radius: var(--radius-md);
  color: var(--text-muted);
}

.gallery__close:hover {
  color: var(--text-primary);
  background: var(--surface-muted);
}

.gallery__stage {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  flex: 1;
  min-height: 0;
  padding: var(--space-5) var(--space-4);
}

.gallery__arrow {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 38px;
  flex: none;
  border: 1px solid var(--border-default);
  border-radius: var(--radius-pill);
  background: var(--surface);
  color: var(--text-secondary);
  box-shadow: var(--shadow-sm);
  transition:
    color var(--duration-fast) var(--ease-out),
    border-color var(--duration-fast) var(--ease-out);
}

.gallery__arrow:hover:not(:disabled) {
  color: var(--accent);
  border-color: var(--accent);
}

.gallery__arrow:disabled {
  opacity: 0.35;
  box-shadow: none;
  cursor: default;
}

.gallery__viewport {
  flex: 1;
  min-width: 0;
  height: 100%;
  overflow: hidden;
}

.gallery__track {
  display: flex;
  height: 100%;
  transition: transform var(--duration-panel) var(--ease-out);
}

.gallery__page {
  flex: 0 0 100%;
  min-width: 0;
  align-content: start;
  overflow-y: auto;
  padding: 2px;
}

.gallery__nav {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-3);
  flex: none;
  padding: var(--space-3) var(--space-6) var(--space-4);
  border-top: 1px solid var(--border-subtle);
}

.gallery__dots {
  display: flex;
  gap: 2px;
}

/*
 * The visible dot is 7px, but the button around it is a full 24px target, so
 * paging is usable with a thumb.
 */
.gallery__dot {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  border-radius: var(--radius-pill);
  background: none;
}

.gallery__dot::after {
  content: '';
  width: 7px;
  height: 7px;
  border-radius: var(--radius-pill);
  background: var(--border-strong);
  transition:
    background-color var(--duration-base) var(--ease-out),
    transform var(--duration-base) var(--ease-out);
}

.gallery__dot[data-active='true']::after {
  background: var(--gradient-primary);
  transform: scale(1.5);
}

.gallery__counter {
  font-size: var(--text-helper);
  font-variant-numeric: tabular-nums;
  color: var(--text-muted);
}

@media (max-width: 767px) {
  .gallery {
    width: calc(100vw - 24px);
    height: calc(100vh - 24px);
  }

  .gallery__stage {
    padding: var(--space-4) var(--space-4) 0;
  }

  /* Swipe and the dots carry navigation where there is no room for arrows. */
  .gallery__arrow {
    display: none;
  }
}

/* ---------------------------- command palette -------------------------- */

/* The palette sits high rather than dead centre, the way search overlays do. */
.palette {
  width: min(560px, calc(100vw - 32px));
  margin: 12vh auto auto;
  overflow: hidden;
}

.palette__header {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-4) var(--space-5);
  border-bottom: 1px solid var(--border-subtle);
  color: var(--text-muted);
}

.palette__input {
  flex: 1;
  border: none;
  outline: none;
  background: transparent;
  font-size: 15px;
  color: var(--text-primary);
}

.palette__results {
  max-height: 46vh;
  overflow-y: auto;
  padding: var(--space-2);
}

.palette__group {
  padding: var(--space-3) var(--space-3) var(--space-1);
  font-size: 11px;
  font-weight: var(--weight-label);
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.palette__item {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-md);
  cursor: pointer;
}

.palette__item[aria-selected='true'] {
  background: var(--accent-soft);
}

.palette__item-icon {
  display: inline-flex;
  color: var(--text-secondary);
}

.palette__item[aria-selected='true'] .palette__item-icon {
  color: var(--accent-deep);
}

.palette__item-text {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.palette__item-label {
  font-size: var(--text-body);
  font-weight: var(--weight-medium);
}

.palette__item-hint {
  font-size: var(--text-helper);
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.palette__empty {
  padding: var(--space-6);
  text-align: center;
}

/* ------------------------------ empty state ---------------------------- */

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-10) var(--space-6);
  text-align: center;
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius-lg);
  background: var(--surface-secondary);
}

.empty-state__icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  margin-bottom: var(--space-1);
  border-radius: var(--radius-pill);
  color: var(--accent-deep);
  background: var(--accent-soft);
}

.empty-state__title {
  font-size: var(--text-body);
  font-weight: var(--weight-label);
}

.empty-state__description {
  max-width: 46ch;
}

.empty-state .btn {
  margin-top: var(--space-3);
}
