/* ============================================================================
   Глибока аналітика програми лояльності: когорти, RFM, поведінка, рівні, LTV, страви, зобов'язання, персонал, години, симулятор (префікс la-).
   Окремий файл, щоб не змішувати з основною дизайн-системою; токени — з app.css.
   ========================================================================= */

@layer components {

  /* ─────────────── Каркас: перемикач розділів, заголовок розділу, висновок ─────────────── */

  .la { display: grid; gap: 16px; min-width: 0; }
  .la-view { display: grid; gap: 14px; min-width: 0; }
  /* display: grid перебиває атрибут hidden — симулятор живе прихованим, поки відкрито інший розділ. */
  .la-view[hidden] { display: none; }
  .la-body { display: grid; gap: 14px; min-width: 0; }
  .la-loading { display: grid; gap: 12px; }

  .la-switch { display: flex; align-items: center; gap: 10px; min-width: 0; }
  .la-nav {
    display: flex; gap: 2px; padding: 3px;
    /* Без min-width: 0 елемент flex не стискається й вилазить за край замість прокрутки. */
    flex: 0 1 auto; min-width: 0; max-width: 100%;
    overflow-x: auto; scrollbar-width: none;
    border-radius: var(--r-full);
    background: var(--bg-sunken);
    border: 1px solid var(--line);
  }
  .la-nav::-webkit-scrollbar { display: none; }
  .la-nav__item {
    display: inline-flex; align-items: center; gap: 6px; flex-shrink: 0;
    padding: 6px 12px;
    border-radius: var(--r-full);
    font-size: var(--t-sm); font-weight: 560;
    color: var(--ink-3);
    white-space: nowrap;
    transition: background var(--d-fast) var(--e-out), color var(--d-fast) var(--e-out), box-shadow var(--d-fast) var(--e-out);
  }
  .la-nav__item svg { opacity: .75; }
  .la-nav__item:hover { color: var(--ink); }
  .la-nav__item.is-active { background: var(--surface-solid); color: var(--accent); box-shadow: var(--sh-xs); }
  .la-nav__item.is-active svg { opacity: 1; }
  .la-nav__item:focus-visible { outline-offset: -3px; }
  /* На телефоні десять пігулок не влазять — там звичайний список. */
  @media (min-width: 721px) { .la-nav-select { display: none; } }
  @media (max-width: 720px) { .la-nav { display: none; } }

  .la-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 10px 16px; flex-wrap: wrap; }
  .la-head__title { display: grid; gap: 4px; min-width: 0; }
  .la-head__title h3 { font-size: var(--t-lg); }
  .la-scope { display: inline-flex; align-items: center; gap: 6px; font-size: var(--t-xs); color: var(--ink-3); line-height: 1.4; }
  .la-scope svg { flex-shrink: 0; }
  .la-head__select { width: auto; min-width: 200px; max-width: 280px; }
  @media (max-width: 560px) {
    .la-head__select { width: 100%; max-width: none; }
    .la-head .segmented { max-width: 100%; overflow-x: auto; scrollbar-width: none; }
  }

  /* Висновок «ну й що?» — перше, що бачить власник у кожному розділі. */
  .la-take {
    display: flex; align-items: flex-start; gap: 12px;
    padding: 14px 16px;
    border-radius: var(--r-lg);
    background: color-mix(in oklab, var(--la-c, var(--info)) 7%, var(--surface-solid));
    border: 1px solid color-mix(in oklab, var(--la-c, var(--info)) 24%, var(--line));
  }
  .la-take--ok { --la-c: var(--ok); }
  .la-take--info { --la-c: var(--info); }
  .la-take--warn { --la-c: var(--warn); }
  .la-take--danger { --la-c: var(--danger); }
  .la-take__icon {
    display: grid; place-items: center; flex-shrink: 0;
    width: 34px; height: 34px;
    border-radius: var(--r-md);
    background: color-mix(in oklab, var(--la-c, var(--info)) 15%, transparent);
    color: var(--la-c, var(--info));
  }
  .la-take__body { flex: 1; min-width: 0; display: grid; gap: 3px; }
  .la-take__body b { font-weight: 620; line-height: 1.35; color: var(--ink); }
  .la-take__body p { font-size: var(--t-md); line-height: 1.5; color: var(--ink-2); }
  .la-take__action { flex-shrink: 0; align-self: center; }
  @media (max-width: 640px) {
    .la-take { flex-wrap: wrap; }
    .la-take__body { flex-basis: calc(100% - 46px); }
    .la-take__action { margin-left: 46px; }
  }

  .la-kpis .kpi__foot { max-width: none; }

  /* Кольори рядів: смужки, теплові карти, клітинки RFM беруть --la-c. */
  .la-tone-ok { --la-c: var(--ok); }
  .la-tone-info { --la-c: var(--info); }
  .la-tone-warn { --la-c: var(--warn); }
  .la-tone-danger { --la-c: var(--danger); }
  .la-tone-muted { --la-c: var(--n-400); }

  .la-sr {
    position: absolute;
    width: 1px; height: 1px;
    padding: 0; margin: -1px;
    overflow: hidden; clip: rect(0 0 0 0);
    white-space: nowrap; border: 0;
  }

  .la-split { display: grid; grid-template-columns: minmax(0, 1.6fr) minmax(0, 1fr); gap: 14px; align-items: start; }
  .la-rfm-layout { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.3fr); gap: 14px; align-items: start; }
  @media (max-width: 1180px) {
    .la-split, .la-rfm-layout { grid-template-columns: minmax(0, 1fr); }
  }

  /* ─────────────── Таблиці розділів ─────────────── */

  .la .table td.num { white-space: nowrap; }
  .la-segments { min-width: 760px; }
  .la-segments td { vertical-align: top; }
  .la-tiers { min-width: 760px; }
  .la-ltv { min-width: 640px; }
  .la-products { min-width: 660px; }
  .la-staff { min-width: 560px; }
  .la-liability { min-width: 540px; }

  .la-share { display: flex; align-items: center; gap: 8px; min-width: 128px; }
  .la-share .progress { flex: 1; min-width: 48px; }
  .la-share > span, .la-share > b { min-width: 46px; text-align: right; font-size: var(--t-sm); white-space: nowrap; }
  .la-share--wide { min-width: 180px; }
  .la-share--wide > b { min-width: 86px; }
  /* Риска середнього на смужці частки. */
  .la-avgmark { position: relative; overflow: visible; }
  .la-avgmark::after {
    content: "";
    position: absolute; top: -3px; bottom: -3px; left: var(--la-avg, 50%);
    width: 2px; border-radius: 1px;
    background: var(--ink-3);
  }

  .la-tier { display: inline-flex; align-items: center; gap: 7px; white-space: nowrap; font-weight: 540; }
  .la-move { display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; }
  .la-move svg { color: var(--ink-4); }
  .la-link {
    color: var(--ink); font-weight: 540;
    text-decoration: underline; text-decoration-color: var(--line-strong); text-underline-offset: 3px;
    transition: color var(--d-fast) var(--e-out);
  }
  .la-link:hover { color: var(--accent); text-decoration-color: currentColor; }
  .la-person { gap: 0; min-width: 0; }
  .la-person__name { display: inline-flex; align-items: center; gap: 6px; font-weight: 540; }
  .la-product { gap: 0; min-width: 170px; }
  .la-advice { min-width: 230px; max-width: 380px; font-size: var(--t-sm); line-height: 1.45; color: var(--ink-2); }
  .la-tiers__bar { padding-bottom: 8px; }

  /* ─────────────── Горизонтальні смужки ─────────────── */

  .la-hbars { display: grid; gap: 9px; }
  .la-hbar {
    display: grid;
    grid-template-columns: minmax(84px, 38%) minmax(40px, 1fr) auto;
    align-items: center; gap: 10px;
    font-size: var(--t-sm);
  }
  .la-hbar__label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--ink-2); }
  .la-hbar__track { height: 10px; border-radius: var(--r-full); background: var(--bg-sunken); overflow: hidden; }
  .la-hbar__fill {
    display: block; height: 100%; min-width: 3px;
    border-radius: inherit;
    background: var(--la-c, var(--accent));
    transition: width var(--d-slow) var(--e-out);
  }
  .la-hbar__value {
    display: inline-flex; align-items: baseline; justify-content: flex-end; gap: 6px;
    min-width: 72px; white-space: nowrap;
    font-variant-numeric: tabular-nums;
  }
  .la-hbar__value .muted-2 { min-width: 38px; text-align: right; font-size: var(--t-xs); }

  /* ─────────────── Згруповані стовпчики по місяцях ─────────────── */

  .la-colchart { display: grid; gap: 10px; min-width: 0; }
  .la-colchart__legend { display: flex; align-items: center; justify-content: space-between; gap: 8px 16px; flex-wrap: wrap; }
  .la-legend { display: flex; flex-wrap: wrap; gap: 6px 14px; }
  .la-cols {
    display: flex; align-items: flex-end; gap: 6px;
    height: var(--la-h, 180px);
    padding: 0 2px;
    border-bottom: 1px solid var(--line-strong);
    /* Чотири лінії сітки — щоб висоту стовпців було з чим порівняти. */
    background: repeating-linear-gradient(to top, transparent 0 calc(25% - 1px), var(--line) calc(25% - 1px) 25%);
  }
  .la-cols__group {
    flex: 1 1 0; min-width: 0; height: 100%;
    display: flex; align-items: flex-end; justify-content: center; gap: 2px;
    border-radius: var(--r-xs) var(--r-xs) 0 0;
    transition: background var(--d-fast) var(--e-out);
  }
  .la-cols__group:hover { background: var(--accent-soft); }
  .la-cols__bar {
    display: block; flex: 1 1 0; max-width: 22px; min-height: 2px;
    border-radius: 4px 4px 1px 1px;
    transform-origin: bottom;
    animation: bar-rise var(--d-slow) var(--e-out) backwards;
  }
  .la-cols__bar.is-zero { min-height: 0; }
  .la-cols__x { display: flex; gap: 6px; padding: 0 2px; }
  .la-cols__x span {
    flex: 1 1 0; min-width: 0;
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
    text-align: center; font-size: var(--t-2xs); color: var(--ink-4);
  }
  .la-cols__x span.is-hidden { visibility: hidden; }

  /* ─────────────── Теплова карта «день × година» ─────────────── */

  .la-heat-wrap { display: grid; gap: 10px; min-width: 0; overflow-x: auto; }
  .la-heat { width: 100%; min-width: 420px; table-layout: fixed; border-collapse: separate; border-spacing: 2px; }
  .la-heat th { font-weight: 500; }
  .la-heat thead th {
    padding: 0 0 2px;
    text-align: center; font-size: var(--t-2xs); color: var(--ink-4);
    font-variant-numeric: tabular-nums;
  }
  .la-heat thead th:first-child, .la-heat__day { width: 30px; }
  .la-heat__day { padding-right: 4px; text-align: left; white-space: nowrap; font-size: var(--t-xs); color: var(--ink-3); }
  .la-heat__td { padding: 0; }
  .la-heat__cell {
    display: block; height: 22px;
    border-radius: 4px;
    background: color-mix(in oklab, var(--la-c, var(--accent)) var(--la-a, 0%), var(--bg-sunken));
    transition: transform var(--d-fast) var(--e-out), box-shadow var(--d-fast) var(--e-out);
  }
  .la-heat__td:hover .la-heat__cell { transform: scale(1.15); box-shadow: var(--sh-sm); }
  .la-heat__cell.is-zero { background: var(--bg-sunken); opacity: .55; }
  .la-heat__cell.is-mark { outline: 2px dashed var(--info); outline-offset: -2px; }
  .la-heat__legend { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; font-size: var(--t-xs); color: var(--ink-3); }
  .la-heat__scale {
    display: block; width: 96px; height: 8px;
    border-radius: var(--r-full);
    background: linear-gradient(90deg, color-mix(in oklab, var(--la-c, var(--accent)) 12%, var(--bg-sunken)), var(--la-c, var(--accent)));
  }
  .la-heat__marklegend { display: inline-flex; align-items: center; gap: 6px; margin-left: 10px; }
  .la-heat__marklegend i { display: block; width: 14px; height: 10px; border-radius: 3px; outline: 2px dashed var(--info); outline-offset: -2px; }

  /* ─────────────── Когорти ─────────────── */

  .la-cohorts { min-width: 640px; }
  .la-cohorts th, .la-cohorts td { padding: 6px 8px; }
  .la-cohorts .la-rowhead {
    position: sticky; left: 0; z-index: 2;
    text-align: left; white-space: nowrap;
    background: var(--surface-solid);
    box-shadow: 1px 0 0 var(--line);
  }
  .la-cohorts thead .la-rowhead { z-index: 3; background: var(--bg-sunken); }
  .la-cohorts tbody .la-rowhead,
  .la-cohorts tfoot .la-rowhead {
    top: auto;
    text-transform: none; letter-spacing: 0;
    font-size: var(--t-sm); font-weight: 560; color: var(--ink);
  }
  .la-hc {
    min-width: 54px;
    font-size: var(--t-sm);
    background: color-mix(in oklab, var(--accent) var(--la-a, 0%), transparent);
    /* Біла рамка між клітинками — плитка, а не суцільна заливка. */
    box-shadow: inset 0 0 0 1.5px var(--surface-solid);
    transition: filter var(--d-fast) var(--e-out);
  }
  .la-hc.is-hot { font-weight: 620; }
  .la-hc.is-base { background: var(--bg-sunken); color: var(--ink-3); }
  .la-hc.is-none { background: transparent; box-shadow: none; }
  .la-hc.is-partial { outline: 1.5px dashed color-mix(in oklab, var(--ink) 40%, transparent); outline-offset: -4px; }
  .la-cohorts tbody tr:hover .la-hc:not(.is-none) { filter: saturate(1.2); }
  .la-cohorts tfoot th, .la-cohorts tfoot td {
    background: var(--bg-sunken);
    border-top: 1px solid var(--line-strong);
    border-bottom: none;
    font-weight: 600;
  }
  .la-cohorts tfoot .la-rowhead { background: var(--bg-sunken); }

  /* ─────────────── RFM-матриця ─────────────── */

  /* Картка обрізає вміст (overflow: hidden), тож на вузькому екрані матриця прокручується сама. */
  .la-rfm { display: grid; grid-template-columns: auto repeat(5, minmax(48px, 1fr)); gap: 4px; overflow-x: auto; padding: 3px 1px; }
  .la-rfm__row { display: contents; }
  .la-rfm__ylab { align-self: center; padding-right: 6px; text-align: right; white-space: nowrap; font-size: var(--t-2xs); color: var(--ink-3); }
  .la-rfm__xlab { padding-top: 4px; text-align: center; line-height: 1.25; font-size: var(--t-2xs); color: var(--ink-3); }
  .la-rfm__cell {
    display: flex; flex-direction: column; justify-content: space-between; gap: 2px;
    min-height: 64px; padding: 7px 8px;
    border-radius: var(--r-sm);
    background: color-mix(in oklab, var(--la-c, var(--n-400)) var(--la-a, 8%), var(--surface-solid));
    border: 1px solid color-mix(in oklab, var(--la-c, var(--n-400)) 28%, transparent);
    color: var(--ink);
    transition: transform var(--d-fast) var(--e-out), box-shadow var(--d-fast) var(--e-out);
  }
  .la-rfm__cell:hover { transform: translateY(-1px); box-shadow: var(--sh-sm); }
  .la-rfm__cell b { font-size: var(--t-md); font-weight: 650; }
  .la-rfm__cell span {
    display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
    font-size: var(--t-2xs); line-height: 1.2; color: inherit; opacity: .8;
  }
  .la-rfm__cell.is-empty { opacity: .5; }
  @media (max-width: 520px) {
    .la-rfm { grid-template-columns: auto repeat(5, minmax(40px, 1fr)); gap: 3px; }
    .la-rfm__cell { min-height: 46px; padding: 6px; }
    .la-rfm__cell span { display: none; }
  }

  /* ─────────────── Щасливі години ─────────────── */

  .la-slots { display: grid; gap: 10px; align-content: start; }
  .la-slot {
    display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap;
    padding: 10px 12px;
    border: 1px solid var(--line);
    border-radius: var(--r-md);
    background: var(--bg-sunken);
  }
  .la-slot__main { display: grid; gap: 2px; min-width: 0; }
  .la-slot__main b { font-weight: 600; }

  /* ─────────────── Симулятор ─────────────── */

  .la-sandbox { align-items: flex-start; }
  .la-sim { display: grid; grid-template-columns: minmax(340px, 520px) minmax(0, 1fr); gap: 14px; align-items: start; }
  @media (max-width: 1180px) { .la-sim { grid-template-columns: minmax(0, 1fr); } }
  .la-sim__fields { display: grid; gap: 14px; }
  .la-sim__pair { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
  @media (max-width: 480px) { .la-sim__pair { grid-template-columns: minmax(0, 1fr); } }
  .la-sim__months { width: 76px; }
  .la-sim .field.is-changed .field__label { color: var(--accent); }
  .la-sim .field.is-changed .input,
  .la-sim .field.is-changed .select { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
  .la-sim__tiers { display: grid; gap: 8px; }
  .la-sim__tiertable { min-width: 440px; }
  .la-sim__tiertable th { padding: 7px 6px; }
  .la-sim__tiertable td { padding: 5px 6px; }
  .la-sim__tiertable tr.is-muted td { opacity: .6; }
  .la-sim__name { min-width: 96px; }
  .la-sim__num { width: 80px; padding-left: 8px; padding-right: 6px; text-align: right; }
  .la-sim__errors { align-items: flex-start; }
  .la-sim__result { display: grid; gap: 14px; min-width: 0; }
  .la-cmp { min-width: 560px; }
  .la-cmp td { vertical-align: top; }
  .la-cmp__key td { background: var(--accent-soft); }
  .la-cmp__key td:first-child { font-weight: 600; }
  .la-delta--good { color: var(--ok); font-weight: 600; }
  .la-delta--bad { color: var(--danger); font-weight: 600; }
  .la-delta--flat { color: var(--ink-4); }
  .la-notes { display: grid; gap: 6px; padding-left: 18px; font-size: var(--t-sm); color: var(--ink-2); }
}
