@scope (.kanban-board) {
  :scope {
    display: flex;
    flex-direction: column;
    height: 100%;
    min-height: 0;
  }

  .toolbar {
    display: flex;
    align-items: center;
    gap: var(--space-md);
    flex-wrap: wrap;
    padding-bottom: var(--space-lg);
    flex-shrink: 0;
  }

  /* Hosts the scroll controller: prev/next nav on top, the board below. */
  .board-shell {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-width: 0;
    min-height: 0;
  }

  /* Stage scrollbar: a full-width track standing in for the native scrollbar.
     Each slot is a stage (icon + name, matching the column headers); the raised
     thumb covers the stages on screen and carries a grip so it reads as
     draggable. Drag it, press a stage or use the end arrows and the board moves
     one stage at a time. It sticks to the top while the page scrolls. */
  .board-nav {
    position: sticky;
    top: 0;
    z-index: 2;
    display: flex;
    align-items: stretch;
    gap: var(--space-2xs);
    margin-block-end: var(--space-sm);
    padding: var(--space-2xs);
    border-radius: var(--radius-lg);
    background: var(--color-surface-muted);
    /* A soft drop shadow lifts the bar off the board it scrolls over. */
    box-shadow: inset 0 0 0 1px var(--color-border-muted), var(--shadow-md);
    flex-shrink: 0;

    &[hidden] {
      display: none;
    }
  }

  .board-nav__button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    inline-size: 2.75rem;
    padding: 0;
    border: 0;
    border-radius: var(--radius-md);
    background: transparent;
    color: var(--color-text-heading);
    cursor: pointer;
    transition: background-color 0.15s ease;

    .icon {
      inline-size: 1.25rem;
      block-size: 1.25rem;
      fill: currentColor;
    }

    &:hover:not(:disabled) {
      background: var(--color-surface-raised);
    }

    &:focus-visible {
      outline: var(--focus-ring);
      outline-offset: 0;
    }

    &:disabled {
      opacity: 0.25;
      cursor: default;
    }
  }

  .board-rail {
    --rail-start: 0;
    --rail-span: 1;

    container-type: inline-size;
    position: relative;
    flex: 1;
    min-width: 0;
    display: grid;
    grid-template-columns: repeat(var(--rail-count), minmax(0, 1fr));
    block-size: 3.25rem;
    cursor: grab;
    touch-action: none;
    user-select: none;
  }

  .board-nav[data-dragging] .board-rail {
    cursor: grabbing;
  }

  .board-rail__thumb {
    position: absolute;
    inset-block: 0;
    inset-inline-start: calc(var(--rail-start) * 100% / var(--rail-count));
    inline-size: calc(var(--rail-span) * 100% / var(--rail-count));
    border-radius: var(--radius-md);
    background: var(--color-surface-raised);
    box-shadow: var(--shadow-sm), 0 0 0 1px var(--color-border);
    pointer-events: none;
    transition: inset-inline-start 0.24s cubic-bezier(0.2, 0.8, 0.2, 1), box-shadow 0.15s ease;

    /* Grip: three dots on the bottom edge, the "pull me" cue of the thumb. */
    &::after {
      content: "";
      position: absolute;
      inset-block-end: 5px;
      inset-inline-start: 50%;
      inline-size: 1.5rem;
      block-size: 4px;
      translate: -50% 0;
      background: radial-gradient(circle, var(--color-text-muted) 1.5px, transparent 2px) 0 50% / 0.5rem 4px repeat-x;
      opacity: 0.7;
    }

    &:focus-visible {
      outline: var(--focus-ring);
      outline-offset: 2px;
    }
  }

  .board-rail:hover .board-rail__thumb {
    box-shadow: var(--shadow-md), 0 0 0 1px var(--color-border);
  }

  .board-nav[data-dragging] .board-rail__thumb {
    box-shadow: var(--shadow-md), 0 0 0 2px var(--color-primary);

    &::after {
      opacity: 1;
    }
  }

  .board-rail__stop {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-xs);
    min-width: 0;
    padding-inline: var(--space-xs);
    padding-block-end: 4px;
    pointer-events: none;
    color: var(--color-text-muted);
    transition: color 0.15s ease;

    .icon {
      inline-size: 1.25rem;
      block-size: 1.25rem;
      flex-shrink: 0;
      fill: currentColor;
      opacity: 0.6;
    }

    /* Hairline between stages outside the thumb, like ruler ticks. */
    & + &::before {
      content: "";
      position: absolute;
      inset-inline-start: 0;
      inset-block: 30%;
      border-inline-start: 1px solid var(--color-border);
    }

    &[data-in-view] {
      color: var(--color-text-heading);

      .icon {
        fill: var(--column-accent);
        opacity: 1;
      }
    }

    &[data-in-view] + &[data-in-view]::before {
      border-color: var(--color-border-muted);
    }
  }

  .board-rail__name {
    font-size: var(--text-body-sm);
    font-weight: var(--font-semibold);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  /* Narrow rail (phones): icons only, the thumb still shows the window. */
  @container (width < 44rem) {
    .board-rail__name {
      display: none;
    }
  }

  .board-rail__stop--lead,
  .column--lead { --column-accent: var(--color-info-text); }
  .board-rail__stop--prospect,
  .column--prospect { --column-accent: var(--color-primary, var(--color-info-text)); }
  .board-rail__stop--scheduled,
  .column--scheduled { --column-accent: var(--color-warning-text); }
  .board-rail__stop--not_visited,
  .column--not_visited { --column-accent: var(--color-error-text); }
  .board-rail__stop--visited,
  .column--visited { --column-accent: var(--color-info-text); }
  .board-rail__stop--closed,
  .column--closed { --column-accent: var(--color-success-text); }
  .board-rail__stop--discarded,
  .column--discarded { --column-accent: var(--color-text-muted); }

  /* Column-by-column snap. The controller sets --kanban-column-width so the
     last visible column always peeks in, signalling more stages to the side. */
  .board {
    --kanban-column-width: 18rem;

    position: relative;
    display: flex;
    align-items: stretch;
    gap: var(--space-md);
    overflow-x: auto;
    overflow-y: hidden;
    scrollbar-width: none;
    scroll-snap-type: x mandatory;
    scroll-padding-inline: var(--space-2xs);
    padding: var(--space-2xs) var(--space-2xs) var(--space-sm);
    flex: 1;
    min-height: 0;
    overscroll-behavior-x: contain;

    /* Snapping fights SortableJS auto-scroll while a card is being dragged. */
    &:has(.sortable-ghost) {
      scroll-snap-type: none;
    }

    &::-webkit-scrollbar {
      display: none;
    }
  }

  .column {
    position: relative;
    display: flex;
    flex-direction: column;
    gap: var(--space-sm);
    flex: 0 0 var(--kanban-column-width);
    inline-size: var(--kanban-column-width);
    min-height: 0;
    padding: var(--space-sm);
    scroll-snap-align: start;
    background: var(--color-surface-muted);
    border: 1px solid var(--color-border-muted);
    border-radius: var(--radius-xl, var(--radius-lg));
  }

  .column-header {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
    min-block-size: 2rem;
    padding: 0 var(--space-xs);
    flex-shrink: 0;
  }

  .column-icon {
    inline-size: 1.125rem;
    block-size: 1.125rem;
    flex-shrink: 0;
    fill: var(--column-accent);
  }

  .column-title {
    margin: 0;
    font-size: var(--text-base);
    font-weight: var(--font-semibold);
    color: var(--color-text-heading);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .column-count {
    font-size: var(--text-caption);
    font-weight: var(--font-semibold);
    color: var(--color-text-secondary);
    background: var(--color-surface-raised);
    border: 1px solid var(--color-border-muted);
    border-radius: var(--radius-full);
    min-width: 1.5rem;
    height: 1.375rem;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0 var(--space-xs);
  }

  .column-body {
    display: flex;
    flex-direction: column;
    gap: var(--space-sm);
    min-height: 6rem;
    flex: 1;
    overflow-y: auto;
    margin-inline: calc(-1 * var(--space-2xs));
    padding: var(--space-2xs);
    scrollbar-width: thin;
    scrollbar-color: transparent transparent;

    &:hover,
    &:focus-within {
      scrollbar-color: var(--color-border) transparent;
    }
  }

  .column-empty {
    font-size: var(--text-caption);
    color: var(--color-text-muted);
    text-align: center;
    padding: var(--space-lg) var(--space-sm);
    border: 1px dashed var(--color-border);
    border-radius: var(--radius-lg);
  }

  /* Card, read top to bottom as "who do I contact now": a status pill
     (temperature or sale status), who and where from, owners, what is next,
     then a meta footer with the stake and how long the lead has sat here. */
  .card {
    display: flex;
    flex-direction: column;
    gap: var(--space-sm);
    padding: var(--space-md);
    background: var(--color-surface-raised);
    border: 1px solid var(--color-border-muted);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-xs);
    text-decoration: none;
    color: var(--color-text);
    cursor: grab;
    transition: box-shadow 0.15s ease, border-color 0.15s ease;

    &.sortable-ghost {
      opacity: 0.4;
    }

    &.sortable-drag {
      cursor: grabbing;
      box-shadow: var(--shadow-lg);
    }

    &:hover {
      border-color: var(--color-border);
      box-shadow: var(--shadow-sm);
      text-decoration: none;
    }

    &:focus-visible {
      outline: var(--focus-ring);
      outline-offset: var(--focus-ring-offset);
    }
  }

  .card--highlighted {
    background: var(--color-primary-muted);
    border-color: light-dark(var(--blue-200), var(--blue-800));

    &:hover {
      border-color: light-dark(var(--blue-300), var(--blue-700));
    }
  }

  /* 1. Status pill + alerts */
  .card-top {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: var(--space-xs);
    min-block-size: 1.375rem;

    &:not(:has(.card-status, .card-negotiation, .card-alerts > *)) {
      display: none;
    }
  }

  .card-status {
    --status-ink: var(--color-text-secondary);
    --status-tint: var(--color-surface-muted);

    display: inline-flex;
    align-items: center;
    gap: 0.375rem;
    padding: 0.1875rem var(--space-sm);
    border-radius: var(--radius-sm);
    background: var(--status-tint);
    color: var(--status-ink);
    font-size: var(--text-caption);
    font-weight: var(--font-medium);
    line-height: var(--leading-none);
    white-space: nowrap;

    &::before {
      content: "";
      inline-size: 0.375rem;
      block-size: 0.375rem;
      border-radius: var(--radius-full);
      background: currentColor;
    }
  }

  .card-status--hot { --status-ink: var(--color-error-text); --status-tint: var(--color-error-muted); }
  .card-status--warm { --status-ink: var(--color-warning-text); --status-tint: var(--color-warning-muted); }
  .card-status--cold { --status-ink: var(--color-info-text); --status-tint: var(--color-info-muted); }
  .card-status--closed,
  .card-status--success { --status-ink: var(--color-success-text); --status-tint: var(--color-success-muted); }
  .card-status--warning { --status-ink: var(--color-warning-text); --status-tint: var(--color-warning-muted); }

  .card-alerts {
    display: inline-flex;
    align-items: center;
    gap: var(--space-2xs);
    margin-inline-start: auto;
  }

  /* 2. Who and where from */
  .card-body {
    display: flex;
    flex-direction: column;
    gap: var(--space-2xs);
    min-width: 0;
  }

  .card-name {
    font-size: var(--text-base);
    font-weight: var(--font-semibold);
    color: var(--color-text-heading);
    line-height: var(--leading-snug);
    overflow-wrap: anywhere;
  }

  .card-origin {
    margin: 0;
    font-size: var(--text-body-sm);
    color: var(--color-text-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  /* 3–4. Label rows: owners, then appointment / negotiation */
  .card-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-sm);
    min-block-size: 1.5rem;
    font-size: var(--text-body-sm);
    color: var(--color-text-secondary);
  }

  .card-label {
    color: var(--color-text-secondary);
  }

  .card-people {
    display: flex;
    align-items: center;
  }

  .card-avatar {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    inline-size: 1.75rem;
    block-size: 1.75rem;
    border-radius: var(--radius-full);
    font-size: 0.6875rem;
    font-weight: var(--font-bold);
    letter-spacing: 0.02em;
    box-shadow: 0 0 0 2px var(--color-surface-raised);

    & + & {
      margin-inline-start: -0.25rem;
    }
  }

  .card-avatar--sdr { background: var(--color-success-muted); color: var(--color-success-text); }
  .card-avatar--closer { background: var(--color-info-muted); color: var(--color-info-text); }
  .card-avatar--sales_manager { background: var(--color-primary-muted); color: var(--color-primary, var(--color-info-text)); }

  .card-avatar--empty {
    background: var(--color-surface-raised);
    color: var(--color-text-muted);
    border: 1px dashed var(--color-border);
  }

  .card-appointment {
    display: inline-flex;
    align-items: center;
    gap: var(--space-xs);
    color: var(--color-text-secondary);
    white-space: nowrap;

    .icon {
      inline-size: 1rem;
      block-size: 1rem;
      fill: currentColor;
    }
  }

  .card-appointment--overdue {
    color: var(--color-error-text);
    font-weight: var(--font-semibold);
  }

  .card-negotiation {
    padding: 0.1875rem var(--space-sm);
    border-radius: var(--radius-sm);
    font-size: var(--text-caption);
    font-weight: var(--font-medium);
    line-height: var(--leading-none);
    white-space: nowrap;
  }

  .card-negotiation--negotiating { background: var(--color-warning-muted); color: var(--color-warning-text); }
  .card-negotiation--negotiated { background: var(--color-info-muted); color: var(--color-info-text); }

  /* 5. Meta footer */
  .card-footer {
    display: flex;
    align-items: center;
    gap: var(--space-md);
    margin-block-start: var(--space-2xs);
    padding-block-start: var(--space-sm);
    border-block-start: 1px solid var(--color-border-muted);
  }

  .card-meta {
    display: inline-flex;
    align-items: center;
    gap: var(--space-xs);
    font-size: var(--text-caption);
    color: var(--color-text-muted);
    font-variant-numeric: tabular-nums;
    white-space: nowrap;

    .icon {
      inline-size: 0.9375rem;
      block-size: 0.9375rem;
      fill: currentColor;
    }
  }

  .card-value {
    color: var(--color-text-secondary);
    font-weight: var(--font-medium);
  }

  .card-age {
    margin-inline-start: auto;
  }

  .card-age--idle {
    color: var(--color-warning-text);
    font-weight: var(--font-semibold);
  }

  .card-age--stalled {
    color: var(--color-error-text);
    font-weight: var(--font-semibold);
  }

  /* Alerts on the card stay quiet: a small info glyph on a pale amber disc,
     explained on hover. The lead page keeps the louder triangle. */
  .card-alert {
    display: inline-grid;
    place-items: center;
    inline-size: 1.25rem;
    block-size: 1.25rem;
    border-radius: var(--radius-full);
    background: color-mix(in srgb, var(--color-warning-muted) 70%, transparent);
    color: var(--color-warning-text);
    cursor: help;
    opacity: 0.75;
    transition: opacity 0.15s ease;

    .icon {
      inline-size: 0.875rem;
      block-size: 0.875rem;
      fill: currentColor;
    }
  }

  .card:hover .card-alert {
    opacity: 1;
  }

  /* Owners popover: shown beside a card after resting the pointer on it
     (lead-owners controller). Top layer, positioned with fixed coordinates.
     Brand primary (the button colour) so it never reads as another card. */
  .owners-popover {
    --pop-bg: var(--color-primary);
    --pop-ink: var(--white);
    --pop-muted: var(--indigo-100);
    --pop-soft: color-mix(in srgb, var(--white) 14%, transparent);

    position: fixed;
    inset: auto;
    margin: 0;
    inline-size: 15rem;
    padding: var(--space-sm) var(--space-md) var(--space-md);
    border: 0;
    border-radius: var(--radius-lg);
    background: var(--pop-bg);
    color: var(--pop-ink);
    box-shadow: var(--shadow-lg), 0 0 0 1px color-mix(in srgb, var(--indigo-950) 30%, transparent);
    pointer-events: none;

    &:popover-open {
      display: flex;
      flex-direction: column;
      gap: var(--space-sm);
      animation: owners-popover-in 0.14s ease-out;
    }

    .card-avatar {
      background: var(--pop-soft);
      color: var(--pop-ink);
      box-shadow: none;
    }

    .card-avatar--empty {
      background: transparent;
      color: var(--pop-muted);
      border-color: color-mix(in srgb, var(--white) 40%, transparent);
    }
  }

  /* Lead alerts, spelled out above the owners. */
  .owners-popover__alerts {
    display: flex;
    flex-direction: column;
    gap: var(--space-xs);
    margin: 0 0 var(--space-2xs);
    padding: var(--space-sm);
    list-style: none;
    border-radius: var(--radius-md);
    background: var(--pop-soft);
  }

  .owners-popover__alert {
    display: flex;
    align-items: flex-start;
    gap: var(--space-xs);
    font-size: var(--text-caption);
    font-weight: var(--font-medium);
    line-height: var(--leading-snug);
    color: var(--amber-100);

    .icon {
      flex-shrink: 0;
      inline-size: 0.875rem;
      block-size: 0.875rem;
      margin-block-start: 1px;
      fill: currentColor;
    }
  }

  .owners-popover__title {
    margin: 0;
    font-size: var(--text-caption);
    font-weight: var(--font-semibold);
    color: var(--pop-muted);
    text-transform: uppercase;
    letter-spacing: var(--tracking-wide, 0.04em);
  }

  .owners-popover__row {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
    min-width: 0;
  }

  .owners-popover__person {
    display: flex;
    flex-direction: column;
    min-width: 0;
    line-height: var(--leading-snug);
  }

  .owners-popover__role {
    font-size: var(--text-caption);
    color: var(--pop-muted);
  }

  .owners-popover__name {
    font-size: var(--text-body-sm);
    font-weight: var(--font-medium);
    color: var(--pop-ink);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .owners-popover__name--empty {
    color: var(--pop-muted);
    font-weight: var(--font-regular, 400);
    font-style: italic;
  }

  @keyframes owners-popover-in {
    from { opacity: 0; translate: 0 4px; }
  }

  @media (prefers-reduced-motion: reduce) {
    .card,
    .board-nav__button,
    .board-rail__thumb,
    .board-rail__stop,
    .card-alert {
      transition: none;
    }

    .owners-popover:popover-open {
      animation: none;
    }
  }
}
