/* ============================================================
   Small dashboard elements: badge, avatar, stat tile, tabs, slider,
   menu, tooltip, item list, detail list, meter, empty state, skeleton.
   Load after variables.css.

   AI/code-generation contract:
   - .badge takes one tone: --neutral, --primary, --success,
     --warning, --error or --info; add --dot for a leading dot.
     Badge text says the state ("Won", "Overdue"); colour never
     carries the meaning alone.
   - .avatar shows initials (or an <img>); --sm / --lg for size.
   - Tabs that switch pages are links with aria-current="page";
     tabs that switch panels in the page use role="tab" with
     aria-selected="true" on the active one. The show_tab action
     (actions.js) switches them; panels are .tabs__panel with
     role="tabpanel", and every panel but the first starts hidden.
   - .slider is a sideways card strip: every child is a card, it
     scrolls and snaps natively (swipe, trackpad), and the slide
     action (actions.js) moves it. Set card widths with
     --slider-card (default 14rem).
   - .menu is a native <details>: <summary class="menu__trigger">
     plus .menu__panel with .menu__item buttons or links.
   - data-tooltip="Text" on any focusable element shows a tooltip on
     hover and keyboard focus. It is a hint, never the only label:
     an icon button still needs aria-label.
   - .skeleton marks a loading placeholder; it stops animating
     under prefers-reduced-motion.
   - .icon styles an inline stroke SVG (24x24 viewBox) at 1em;
     decorative icons get aria-hidden="true".
   ============================================================ */

@layer components {

  /* ============ Badge ============ */
  .badge {
    display: inline-flex;
    align-items: center;
    gap: 0.375em;
    padding: 0.125rem 0.5rem;
    border: var(--border-width) solid transparent;
    border-radius: var(--radius-full);
    font-size: var(--font-size-xs);
    font-weight: var(--font-weight-medium);
    line-height: var(--line-height-normal);
    white-space: nowrap;
    vertical-align: middle;
  }

  .badge--neutral { background: var(--color-bg-subtle); border-color: var(--color-border); color: var(--color-text-muted); }
  .badge--primary { background: var(--color-primary-subtle); border-color: var(--color-primary-border); color: var(--color-primary-text); }
  .badge--success { background: var(--color-success-subtle); border-color: var(--color-success-border); color: var(--color-success-text); }
  .badge--warning { background: var(--color-warning-subtle); border-color: var(--color-warning-border); color: var(--color-warning-text); }
  .badge--error   { background: var(--color-error-subtle); border-color: var(--color-error-border); color: var(--color-error-text); }
  .badge--info    { background: var(--color-info-subtle); border-color: var(--color-info-border); color: var(--color-info-text); }

  .badge--dot::before {
    content: "";
    width: 0.375rem;
    height: 0.375rem;
    border-radius: var(--radius-full);
    background: currentColor;
  }

  /* ============ Status dot (a status's tone beside its name) ============ */
  .status-dot {
    display: inline-block;
    flex-shrink: 0;
    margin-inline-end: var(--space-2);
    vertical-align: middle;
    width: 0.5rem;
    height: 0.5rem;
    border-radius: var(--radius-full);
    background: var(--color-text-subtle);
  }

  .status-dot--primary { background: var(--color-primary); }
  .status-dot--success { background: var(--color-success); }
  .status-dot--warning { background: var(--color-warning); }
  .status-dot--error   { background: var(--color-error); }
  .status-dot--info    { background: var(--color-info); }

  /* ============ Status flow: "from -> to" while a status is being changed ============ */
  .status-flow {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--space-2) var(--space-3);
    padding: var(--space-3) var(--space-4);
    border: var(--border-width) solid var(--color-border);
    border-radius: var(--radius-lg);
    background: var(--color-bg-subtle);
  }

  .status-flow__side {
    display: inline-flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--space-1) var(--space-2);
    min-width: 0;
  }

  .status-flow__label {
    color: var(--color-text-muted);
    font-size: var(--font-size-xs);
    font-weight: var(--font-weight-semibold);
    letter-spacing: var(--letter-spacing-wide);
    text-transform: uppercase;
  }

  .status-flow__arrow {
    flex-shrink: 0;
    width: 1.125rem;
    height: 1.125rem;
    color: var(--color-text-subtle);
  }

  .status-flow__arrow:dir(rtl) {
    transform: scaleX(-1);
  }

  /* Where the new status will show, until one is picked */
  .status-flow__placeholder {
    padding: 0.0625rem 0.5rem;
    border: var(--border-width) dashed var(--color-border-strong);
    border-radius: var(--radius-full);
    color: var(--color-text-subtle);
    font-size: var(--font-size-xs);
    line-height: var(--line-height-normal);
  }

  .status-flow__side .badge {
    animation: status-flow-in var(--transition-base);
  }

  @keyframes status-flow-in {
    from { opacity: 0; transform: scale(0.85); }
  }

  /* ============ Avatar ============ */
  .avatar {
    display: inline-grid;
    place-items: center;
    flex-shrink: 0;
    width: 2rem;
    height: 2rem;
    overflow: hidden;
    border-radius: var(--radius-full);
    /* tint laid over the surface, so overlapping avatars stay opaque in dark mode too */
    background: linear-gradient(var(--color-primary-subtle), var(--color-primary-subtle)), var(--color-surface);
    color: var(--color-primary);
    font-size: var(--font-size-xs);
    font-weight: var(--font-weight-semibold);
    text-transform: uppercase;
  }

  .avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }

  .avatar--sm { width: 1.5rem; height: 1.5rem; font-size: 0.625rem; }
  .avatar--lg { width: 2.75rem; height: 2.75rem; font-size: var(--font-size-sm); }
  .avatar--xl { width: 6rem; height: 6rem; font-size: var(--font-size-xl, 1.25rem); }
  .profile-photo { display: flex; flex-wrap: wrap; align-items: flex-start; gap: var(--space-4); }
  .profile-photo > .stack { flex: 1 1 16rem; min-width: 0; }
  .profile-photo__actions { display: flex; flex-wrap: wrap; gap: var(--space-2); }

  /* No person: a muted circle, e.g. "Nobody" in the assign popup */
  .avatar--muted {
    background: var(--color-bg-subtle);
    color: var(--color-text-subtle);
  }

  .avatar--muted svg {
    width: 55%;
    height: 55%;
  }

  /* ============ Person: avatar, name and a detail line (components/person.twig) ============ */
  .person {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    min-width: 0;
  }

  .person__text {
    display: grid;
    min-width: 0;
    line-height: var(--line-height-tight);
  }

  .person__name,
  .person__detail {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .person__name {
    color: var(--color-text);
  }

  .person__detail {
    margin-top: 0.125rem;
    color: var(--color-text-muted);
    font-size: var(--font-size-xs);
    font-weight: var(--font-weight-normal);
  }

  /* A small rounded name with its avatar, e.g. "now -> picked" in the assign popup */
  .person-chip {
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
    max-width: 14rem;
    padding: 0.125rem var(--space-3) 0.125rem 0.125rem;
    border: var(--border-width) solid var(--color-border);
    border-radius: var(--radius-full);
    background: var(--color-surface);
    color: var(--color-text);
    font-size: var(--font-size-sm);
    line-height: var(--line-height-tight);
  }

  .person-chip:dir(rtl) {
    padding: 0.125rem 0.125rem 0.125rem var(--space-3);
  }

  .person-chip__name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .person-chip--unknown,
  .person-chip--empty {
    padding: 0.125rem var(--space-3);
    border-style: dashed;
    color: var(--color-text-muted);
  }

  .status-flow__side .person-chip {
    animation: status-flow-in var(--transition-base);
  }

  .avatar-group {
    display: inline-flex;
  }

  .avatar-group .avatar {
    box-shadow: 0 0 0 2px var(--color-surface);
  }

  .avatar-group .avatar + .avatar {
    margin-inline-start: -0.5rem;
  }

  .avatar-group .avatar--sm + .avatar--sm {
    margin-inline-start: -0.3125rem;
  }

  /* A value that opens its own popup when clicked: a status badge, a person chip, a team's avatars
     (components/status.twig change(), person.twig assigned()). Disabled, it looks like the plain value. */
  .inline-edit {
    display: inline-flex;
    align-items: center;
    gap: var(--space-1);
    max-width: 100%;
    margin: -0.1875rem;
    padding: 0.1875rem;
    border: 0;
    border-radius: var(--radius-full);
    background: transparent;
    color: var(--color-text-muted);
    font: inherit;
    text-align: start;
    cursor: pointer;
    transition: background-color var(--transition-fast);
  }

  .inline-edit:hover:not(:disabled) {
    background: var(--color-bg-muted);
  }

  .inline-edit:focus-visible {
    outline: none;
    box-shadow: 0 0 0 2px color-mix(in srgb, var(--focus-ring-color) 80%, transparent);
  }

  .inline-edit:disabled {
    cursor: default;
  }

  .inline-edit__icon {
    flex: none;
    width: 0.875rem;
    height: 0.875rem;
    opacity: 0.65;
    transition: opacity var(--transition-fast);
  }

  .inline-edit:hover .inline-edit__icon,
  .inline-edit:focus-visible .inline-edit__icon {
    opacity: 1;
  }

  .inline-edit:disabled .inline-edit__icon {
    display: none;
  }

  @media (forced-colors: active) {
    .inline-edit:focus-visible {
      outline: var(--focus-ring-width) solid Highlight;
    }
  }

  /* ============ Stat tiles ============ */
  .stat-grid {
    display: grid;
    gap: var(--space-4);
    grid-template-columns: repeat(auto-fit, minmax(min(100%, 13rem), 1fr));
  }

  .stat {
    display: grid;
    gap: var(--space-1);
    padding: var(--space-4) var(--space-5);
    min-width: 0;
    background: var(--color-surface);
    border: var(--border-width) solid var(--color-border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-xs);
  }

  .stat__label {
    min-width: 0;
    overflow-wrap: anywhere;
    font-size: var(--font-size-sm);
    color: var(--color-text-muted);
  }

  .stat__value {
    font-size: var(--font-size-3xl);
    font-weight: var(--font-weight-semibold);
    line-height: var(--line-height-tight);
    letter-spacing: var(--letter-spacing-tight);
    font-variant-numeric: tabular-nums;
  }

  /* two tiles per row on phones */
  @media (max-width: 40rem) {
    .stat-grid {
      grid-template-columns: repeat(2, minmax(0, 1fr));
      gap: var(--space-3);
    }

    .stat {
      padding: var(--space-3) var(--space-4);
    }

    .stat__value {
      font-size: clamp(var(--font-size-lg), 4.5vw, var(--font-size-2xl));
      overflow-wrap: anywhere;
    }
  }

  .stat__footer {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--space-2);
    font-size: var(--font-size-xs);
    color: var(--color-text-subtle);
  }

  .stat__delta {
    display: inline-flex;
    align-items: center;
    gap: 0.125rem;
    font-weight: var(--font-weight-semibold);
    font-variant-numeric: tabular-nums;
  }

  .stat__delta--up { color: var(--color-success-text); }
  .stat__delta--down { color: var(--color-error-text); }
  .stat__delta--up::before { content: "▲"; font-size: 0.7em; }
  .stat__delta--down::before { content: "▼"; font-size: 0.7em; }

  /* Sparkline: an inline <svg> with a .stat__line polyline and an optional .stat__area */
  .stat__chart {
    display: block;
    width: 100%;
    height: 2.5rem;
    margin-top: var(--space-2);
    color: var(--color-primary);
    overflow: visible;
  }

  .stat__line {
    fill: none;
    stroke: currentColor;
    stroke-width: 2;
    stroke-linejoin: round;
    stroke-linecap: round;
    vector-effect: non-scaling-stroke;
  }

  .stat__area {
    fill: color-mix(in srgb, currentColor 14%, transparent);
    stroke: none;
  }

  /* ============ Tabs ============ */
  .tabs {
    display: flex;
    gap: var(--space-1);
    overflow-x: auto;
    border-bottom: var(--border-width) solid var(--color-border);
    scrollbar-width: none;
  }

  .tabs__tab {
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
    margin: 0 0 calc(var(--border-width) * -1);
    padding: var(--space-2) var(--space-3);
    border: 0;
    border-bottom: var(--border-width-thick) solid transparent;
    background: none;
    font: inherit;
    font-size: var(--font-size-sm);
    font-weight: var(--font-weight-medium);
    color: var(--color-text-muted);
    text-decoration: none;
    white-space: nowrap;
    cursor: pointer;
    transition: color var(--transition-fast), border-color var(--transition-fast);
  }

  .tabs__tab:hover {
    color: var(--color-text);
    border-bottom-color: var(--color-border-strong);
  }

  .tabs__tab:focus-visible {
    outline: var(--focus-ring-width) solid var(--focus-ring-color);
    outline-offset: calc(var(--focus-ring-offset) * -1);
    border-radius: var(--radius-sm) var(--radius-sm) 0 0;
  }

  .tabs__tab[aria-selected="true"],
  .tabs__tab[aria-current="page"] {
    color: var(--color-primary);
    border-bottom-color: var(--color-primary);
  }

  .tabs__count {
    padding: 0 0.4rem;
    border-radius: var(--radius-full);
    background: var(--color-bg-muted);
    font-size: var(--font-size-xs);
    font-variant-numeric: tabular-nums;
    color: var(--color-text-muted);
  }

  .tabs__tab[aria-selected="true"] .tabs__count,
  .tabs__tab[aria-current="page"] .tabs__count {
    background: var(--color-primary-subtle);
    color: var(--color-primary);
  }

  .tabs__panel {
    padding-block: var(--space-4);
  }

  .tabs__panel:not([hidden]) {
    animation: tabs-panel-in var(--transition-base);
  }

  @keyframes tabs-panel-in {
    from { opacity: 0; transform: translateY(0.25rem); }
  }

  /* ============ Slider (a card strip moved by the slide action) ============ */
  .slider {
    display: flex;
    gap: var(--space-3);
    overflow-x: auto;
    overscroll-behavior-x: contain;
    scroll-snap-type: x mandatory;
    scroll-behavior: smooth;
    scrollbar-width: none;
  }

  .slider::-webkit-scrollbar {
    display: none;
  }

  .slider > * {
    box-sizing: border-box;
    flex: 0 0 var(--slider-card, 14rem);
    scroll-snap-align: start;
  }

  .slider:focus-visible {
    outline: var(--focus-ring-width) solid var(--focus-ring-color);
    outline-offset: var(--focus-ring-offset);
  }

  @media (prefers-reduced-motion: reduce) {
    .slider {
      scroll-behavior: auto;
    }
  }

  /* Segmented control: a compact pill-shaped tab set */
  .segmented {
    display: inline-flex;
    padding: 0.1875rem;
    gap: 0.125rem;
    border-radius: var(--radius-md);
    background: var(--color-bg-muted);
  }

  .segmented__option {
    padding: 0.25rem 0.75rem;
    text-decoration: none;
    border: 0;
    border-radius: calc(var(--radius-md) - 0.125rem);
    background: transparent;
    font: inherit;
    font-size: var(--font-size-sm);
    font-weight: var(--font-weight-medium);
    color: var(--color-text-muted);
    cursor: pointer;
  }

  .segmented__option:focus-visible {
    outline: var(--focus-ring-width) solid var(--focus-ring-color);
    outline-offset: 0;
  }

  .segmented__option[aria-pressed="true"],
  .segmented__option[aria-selected="true"],
  .segmented__option[aria-current="page"] {
    background: var(--color-surface);
    color: var(--color-text);
    box-shadow: var(--shadow-sm);
  }

  /* ============ Menu (native <details>) ============ */
  .menu {
    position: relative;
    display: inline-block;
  }

  .menu > summary {
    list-style: none;
  }

  .menu > summary::-webkit-details-marker {
    display: none;
  }

  .menu__panel {
    position: absolute;
    top: calc(100% + var(--space-1));
    inset-inline-start: 0;
    z-index: var(--z-dropdown);
    display: flex;
    flex-direction: column;
    min-width: 11rem;
    padding: var(--space-1);
    background: var(--color-surface-raised);
    border: var(--border-width) solid var(--color-border);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-lg);
  }

  .menu--end .menu__panel {
    inset-inline: auto 0;
  }

  .menu__item {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    width: 100%;
    padding: var(--space-2) var(--space-3);
    border: 0;
    border-radius: var(--radius-sm);
    background: none;
    font: inherit;
    font-size: var(--font-size-sm);
    color: var(--color-text);
    text-align: start;
    text-decoration: none;
    white-space: nowrap;
    cursor: pointer;
  }

  .menu__item:hover,
  .menu__item:focus-visible {
    background: var(--color-bg-subtle);
    outline: none;
  }

  .menu__item svg {
    width: 1rem;
    height: 1rem;
    color: var(--color-text-subtle);
  }

  .menu__item--danger,
  .menu__item--danger svg {
    color: var(--color-error-text);
  }

  .menu__item--danger:hover {
    background: var(--color-error-subtle);
  }

  .menu__separator {
    height: var(--border-width);
    margin: var(--space-1) 0;
    background: var(--color-border);
  }

  /* ============ Tooltip ============ */
  [data-tooltip] {
    position: relative;
  }

  [data-tooltip]::after {
    content: attr(data-tooltip);
    position: absolute;
    bottom: calc(100% + var(--space-2));
    left: 50%;
    z-index: var(--z-tooltip);
    width: max-content;
    max-width: 16rem;
    padding: var(--space-1) var(--space-2);
    border-radius: var(--radius-sm);
    background: var(--color-text);
    color: var(--color-bg);
    font-size: var(--font-size-xs);
    font-weight: var(--font-weight-medium);
    line-height: var(--line-height-snug);
    letter-spacing: normal;
    text-transform: none;
    white-space: normal;
    pointer-events: none;
    opacity: 0;
    transform: translate(-50%, 0.25rem);
    transition: opacity var(--transition-fast), transform var(--transition-fast);
  }

  [data-tooltip]:hover::after,
  [data-tooltip]:focus-visible::after {
    opacity: 1;
    transform: translate(-50%, 0);
  }

  /* ============ Item list (rows inside a card) ============ */
  .item-list {
    margin: 0;
    padding: 0;
    list-style: none;
  }

  .item-list__item {
    display: flex;
    flex-wrap: wrap;   /* in a narrow card (or at 200% text) the end value moves under the text */
    align-items: center;
    gap: var(--space-3);
    padding: var(--space-3) var(--space-5);
    border-bottom: var(--border-width) solid var(--color-border-subtle);
    min-width: 0;
  }

  .item-list__item:last-child {
    border-bottom: 0;
  }

  .item-list__body {
    flex: 1 1 8rem;
    min-width: 0;
    font-size: var(--font-size-sm);
  }

  .item-list__title {
    display: block;
    font-weight: var(--font-weight-medium);
    color: var(--color-text);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .item-list__meta {
    display: block;
    font-size: var(--font-size-xs);
    color: var(--color-text-subtle);
  }

  .item-list__end {
    flex-shrink: 0;
    font-size: var(--font-size-sm);
    font-variant-numeric: tabular-nums;
    color: var(--color-text-muted);
  }

  @media (max-width: 40rem) {
    .item-list__item {
      padding-inline: var(--space-4);
    }
  }

  /* ============ Detail list (label / value pairs on a record page) ============ */
  .detail-list {
    display: grid;
    grid-template-columns: minmax(7rem, max-content) minmax(0, 1fr);
    gap: var(--space-3) var(--space-5);
    margin: 0;
    font-size: var(--font-size-sm);
  }

  .detail-list dt {
    color: var(--color-text-muted);
  }

  .detail-list dd {
    margin: 0;
    min-width: 0;
    overflow-wrap: anywhere;
    color: var(--color-text);
  }

  /* ============ Meter (progress towards a target) ============ */
  .meter {
    display: block;
    height: 0.5rem;
    overflow: hidden;
    border-radius: var(--radius-full);
    background: var(--color-bg-muted);
  }

  .meter__fill {
    display: block;
    height: 100%;
    border-radius: inherit;
    background: var(--color-primary);
  }

  .meter--success .meter__fill { background: var(--color-success); }
  .meter--warning .meter__fill { background: var(--color-warning); }
  .meter--error .meter__fill { background: var(--color-error); }

  /* ============ Empty state ============ */
  .empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-7) var(--space-5);
    text-align: center;
  }

  /* Inside a popup or card: less space around */
  .empty-state--compact {
    padding: var(--space-5) var(--space-4);
    border: var(--border-width) dashed var(--color-border);
    border-radius: var(--radius-lg);
  }

  .empty-state__icon {
    display: grid;
    place-items: center;
    width: 3rem;
    height: 3rem;
    margin-bottom: var(--space-2);
    border-radius: var(--radius-full);
    background: var(--color-bg-subtle);
    color: var(--color-text-subtle);
  }

  .empty-state__icon svg {
    width: 1.5rem;
    height: 1.5rem;
  }

  .empty-state__title {
    margin: 0;
    font-size: var(--font-size-md);
    font-weight: var(--font-weight-semibold);
  }

  .empty-state__text {
    max-width: 26rem;
    margin: 0;
    font-size: var(--font-size-sm);
    color: var(--color-text-muted);
  }

  .empty-state__actions {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: var(--space-2);
    margin-top: var(--space-3);
  }

  /* ============ Skeleton (loading placeholder) ============ */
  .skeleton {
    display: block;
    height: 0.875rem;
    border-radius: var(--radius-sm);
    background: linear-gradient(90deg, var(--color-bg-muted) 25%, var(--color-bg-subtle) 50%, var(--color-bg-muted) 75%);
    background-size: 200% 100%;
    animation: skeleton-shimmer 1.4s ease-in-out infinite;
  }

  .skeleton--circle {
    width: 2rem;
    height: 2rem;
    border-radius: var(--radius-full);
  }

  .skeleton--title {
    height: 1.25rem;
    width: 40%;
  }

  @keyframes skeleton-shimmer {
    from { background-position: 100% 0; }
    to { background-position: -100% 0; }
  }

  @media (prefers-reduced-motion: reduce) {
    .skeleton {
      animation: none;
    }
  }

  /* ============ Icon (inline SVG, e.g. <svg class="icon"><use href="#i-users"/></svg>) ============ */
  .icon {
    display: inline-block;
    width: 1em;
    height: 1em;
    flex-shrink: 0;
    fill: none;
    stroke: currentColor;
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
    vertical-align: -0.125em;
  }

  /* ============ Visually hidden (labels for screen readers only) ============ */
  .visually-hidden {
    position: absolute !important;
    width: 1px;
    height: 1px;
    margin: -1px;
    padding: 0;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
    border: 0;
  }
}
