/* ============================================================================
   Чати: записи CRM у повідомленнях (картка, знімок, вибір), правила чатів компанії,
   «надіслати в чат» і миттєвий пошук. Кольори — токени з app.css (обидві теми).
   ========================================================================= */

@layer components {

  /* ─────────────────────────── Значок виду запису ─────────────────────────── */

  .citem__icon--blue   { --kind: #3b82f6; }
  .citem__icon--orange { --kind: #f97316; }
  .citem__icon--green  { --kind: #10b981; }
  .citem__icon--violet { --kind: #8b5cf6; }
  .citem__icon--teal   { --kind: #14b8a6; }
  .citem__icon--red    { --kind: #ef4444; }
  .citem__icon--amber  { --kind: #d97706; }
  .citem__icon--pink   { --kind: #ec4899; }
  .citem__icon--gray   { --kind: #64748b; }

  /* ─────────────────────────── Рядок запису ─────────────────────────── */

  .citem {
    display: flex; align-items: center; gap: 11px;
    min-width: 0; width: 100%;
    text-align: left;
  }
  .citem__icon {
    --kind: var(--accent);
    position: relative;
    display: grid; place-items: center; flex-shrink: 0;
    width: 38px; height: 38px;
    border-radius: 11px;
    color: var(--kind);
    background: color-mix(in oklab, var(--kind) 13%, var(--bg-elevated));
    box-shadow: inset 0 0 0 1px color-mix(in oklab, var(--kind) 22%, transparent);
    overflow: hidden;
  }
  .citem__icon img { width: 100%; height: 100%; object-fit: cover; }
  .citem--compact .citem__icon { width: 30px; height: 30px; border-radius: 9px; }

  .citem__text { display: flex; flex-direction: column; gap: 2px; min-width: 0; flex: 1; }
  .citem__title {
    font-weight: 620; color: var(--ink); line-height: 1.3;
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  }
  .citem--compact .citem__title { font-size: var(--t-sm); }
  .citem__title mark, .cquick__snippet mark, .csearch__label mark {
    background: color-mix(in oklab, var(--warn) 28%, transparent);
    color: inherit; border-radius: 3px; padding: 0 1px;
  }
  .citem__sub {
    display: flex; align-items: center; gap: 5px; min-width: 0;
    font-size: var(--t-xs); color: var(--ink-3);
  }
  .citem__kind { font-weight: 600; color: var(--ink-2); flex-shrink: 0; }
  .citem__dot { color: var(--ink-4); }
  .citem__date { flex-shrink: 0; font-size: var(--t-2xs); color: var(--ink-3); font-variant-numeric: tabular-nums; }

  /* ─────────────────────────── Картка в повідомленні ─────────────────────────── */

  .cshare-list { display: flex; flex-direction: column; gap: 8px; margin-top: 6px; max-width: min(560px, 100%); }

  .cshare {
    --tone: var(--ink-4);
    position: relative;
    display: flex; gap: 12px;
    padding: 11px 12px 10px 15px;
    border: 1px solid var(--line);
    border-radius: 14px;
    background:
      linear-gradient(180deg, color-mix(in oklab, var(--tone) 5%, transparent), transparent 60%),
      var(--bg-elevated);
    box-shadow: 0 1px 2px color-mix(in oklab, var(--ink) 6%, transparent);
    cursor: pointer;
    overflow: hidden;
    transition: border-color var(--d-fast) var(--e-out), box-shadow var(--d-fast) var(--e-out), transform var(--d-fast) var(--e-out);
  }
  .cshare::before {
    content: ""; position: absolute; inset: 0 auto 0 0; width: 4px;
    background: var(--tone);
  }
  .cshare:hover { border-color: var(--line-strong); box-shadow: var(--sh-sm); transform: translateY(-1px); }
  .cshare:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
  .cshare--ok { --tone: var(--ok); }
  .cshare--warn { --tone: var(--warn); }
  .cshare--danger { --tone: var(--danger); }
  .cshare--info { --tone: var(--info); }
  .cshare--accent { --tone: var(--accent); }

  .cshare__main { display: flex; flex-direction: column; gap: 4px; min-width: 0; flex: 1; }
  .cshare__head { display: flex; align-items: center; gap: 7px; min-width: 0; }
  .cshare__kind {
    display: grid; place-items: center; width: 20px; height: 20px; border-radius: 6px; flex-shrink: 0;
    color: var(--kind); background: color-mix(in oklab, var(--kind) 14%, transparent);
  }
  .cshare__label {
    font-size: var(--t-2xs); font-weight: 700; letter-spacing: .04em; text-transform: uppercase; color: var(--ink-3);
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  }
  .cshare__head .badge { margin-left: auto; }
  .cshare__title {
    font-size: var(--t-base); font-weight: 680; color: var(--ink); line-height: 1.3;
    display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
  }
  .cshare__sub { font-size: var(--t-xs); color: var(--ink-3); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

  .cshare__fields { display: flex; flex-wrap: wrap; gap: 4px 6px; margin-top: 2px; }
  .cshare__field {
    display: inline-flex; gap: 4px; align-items: baseline; max-width: 100%;
    padding: 2px 8px; border-radius: var(--r-full);
    font-size: var(--t-2xs); color: var(--ink-2); background: var(--bg-sunken);
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  }
  .cshare__field > span { color: var(--ink-3); }

  .cshare__lines {
    display: flex; flex-direction: column; gap: 3px; margin-top: 4px; padding: 7px 9px;
    border-radius: 10px; background: var(--bg-sunken);
  }
  .cshare__line { display: flex; align-items: center; gap: 7px; min-width: 0; font-size: var(--t-xs); color: var(--ink-2); }
  .cshare__line.is-done > .truncate { color: var(--ink-3); text-decoration: line-through; }
  .cshare__line-amount { margin-left: auto; flex-shrink: 0; font-variant-numeric: tabular-nums; color: var(--ink-3); }
  .cshare__tick {
    display: grid; place-items: center; flex-shrink: 0;
    width: 14px; height: 14px; border-radius: 4px;
    border: 1.5px solid var(--line-strong); color: #fff;
  }
  .is-done > .cshare__tick { background: var(--ok); border-color: var(--ok); }
  .cshare__more { font-size: var(--t-2xs); color: var(--ink-3); font-weight: 600; }

  .cshare__foot { display: flex; align-items: center; gap: 10px; margin-top: 3px; min-height: 22px; }
  .cshare__amount { font-size: var(--t-md); font-weight: 720; color: var(--ink); letter-spacing: -.01em; }
  .cshare__date { font-size: var(--t-2xs); color: var(--ink-3); font-variant-numeric: tabular-nums; }
  .cshare__open {
    display: inline-flex; align-items: center; gap: 4px;
    padding: 3px 9px; border-radius: var(--r-full);
    font-size: var(--t-2xs); font-weight: 650; color: var(--accent);
    background: var(--accent-soft); text-decoration: none;
    transition: background var(--d-fast) var(--e-out);
  }
  .cshare__open:hover { background: color-mix(in oklab, var(--accent) 20%, transparent); }

  .cshare__cover {
    position: relative; flex-shrink: 0; align-self: flex-start;
    width: 92px; height: 92px; border-radius: 11px; overflow: hidden;
    background: var(--bg-sunken);
  }
  .cshare__cover img { width: 100%; height: 100%; object-fit: cover; transition: transform var(--d-slow) var(--e-out); }
  .cshare:hover .cshare__cover img { transform: scale(1.04); }
  .cshare__cover--file { display: grid; place-items: center; align-content: center; gap: 2px; color: var(--ink-3); font-size: var(--t-2xs); font-weight: 650; }
  .cshare__count {
    position: absolute; right: 5px; bottom: 5px; padding: 1px 6px; border-radius: var(--r-full);
    font-size: var(--t-2xs); font-weight: 700; color: #fff; background: rgb(0 0 0 / .55); backdrop-filter: blur(4px);
  }

  /* ─────────────────────────── Знімок запису ─────────────────────────── */

  .cview { display: flex; flex-direction: column; gap: 16px; }
  .cview__hero {
    --tone: var(--ink-4);
    display: flex; align-items: center; gap: 14px;
    padding: 14px 16px; border-radius: 14px;
    background: linear-gradient(135deg, color-mix(in oklab, var(--tone) 12%, var(--bg-elevated)), var(--bg-sunken));
    border: 1px solid color-mix(in oklab, var(--tone) 18%, var(--line));
  }
  .cview__hero--ok { --tone: var(--ok); }
  .cview__hero--warn { --tone: var(--warn); }
  .cview__hero--danger { --tone: var(--danger); }
  .cview__hero--info { --tone: var(--info); }
  .cview__icon {
    display: grid; place-items: center; flex-shrink: 0;
    width: 46px; height: 46px; border-radius: 13px;
    color: var(--kind); background: color-mix(in oklab, var(--kind) 15%, var(--bg-elevated));
    box-shadow: inset 0 0 0 1px color-mix(in oklab, var(--kind) 24%, transparent);
  }
  .cview__amount { font-size: 22px; font-weight: 760; letter-spacing: -.02em; color: var(--ink); }
  .cview__media .cm-gallery { max-width: 100%; }
  .cview__desc { white-space: pre-wrap; line-height: 1.55; color: var(--ink); font-size: var(--t-sm); }

  .cview__fields {
    display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 1px;
    margin: 0; border-radius: 12px; overflow: hidden;
    background: var(--line); border: 1px solid var(--line);
  }
  .cview__field { display: flex; flex-direction: column; gap: 2px; padding: 9px 12px; background: var(--bg-elevated); min-width: 0; }
  .cview__field dt { font-size: var(--t-2xs); font-weight: 650; letter-spacing: .03em; text-transform: uppercase; color: var(--ink-3); }
  .cview__field dd { margin: 0; font-size: var(--t-sm); color: var(--ink); overflow-wrap: anywhere; }

  .cview__lines { display: flex; flex-direction: column; border: 1px solid var(--line); border-radius: 12px; overflow: hidden; }
  .cview__lines-title {
    padding: 8px 12px; font-size: var(--t-xs); font-weight: 700; color: var(--ink-2);
    background: var(--bg-sunken); border-bottom: 1px solid var(--line);
  }
  .cview__line { display: flex; align-items: center; gap: 10px; padding: 8px 12px; border-top: 1px solid var(--line); }
  .cview__lines-title + .cview__line { border-top: 0; }
  .cview__line:nth-child(even) { background: color-mix(in oklab, var(--bg-sunken) 45%, transparent); }
  .cview__line.is-done .cview__line-text > span:first-child { text-decoration: line-through; color: var(--ink-3); }
  .cview__line-text { display: flex; flex-direction: column; gap: 1px; min-width: 0; flex: 1; font-size: var(--t-sm); }
  .cview__line-amount { flex-shrink: 0; font-size: var(--t-sm); font-weight: 620; }
  .cview__note {
    display: flex; gap: 8px; align-items: flex-start;
    padding: 10px 12px; border-radius: 10px;
    font-size: var(--t-xs); color: var(--ink-3); background: var(--bg-sunken);
  }
  .cview__note .icon { flex-shrink: 0; margin-top: 1px; }

  /* ─────────────────────────── Вибір записів ─────────────────────────── */

  .cpick { display: flex; flex-direction: column; gap: 12px; min-height: 0; }
  .cpick__search {
    display: flex; align-items: center; gap: 9px;
    padding: 0 12px; height: 44px; border-radius: 12px;
    border: 1px solid var(--line-strong); background: var(--bg-elevated); color: var(--ink-3);
    transition: border-color var(--d-fast) var(--e-out), box-shadow var(--d-fast) var(--e-out);
  }
  .cpick__search:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
  .cpick__search input { flex: 1; min-width: 0; border: 0; outline: 0; background: transparent; font: inherit; font-size: var(--t-base); color: var(--ink); }

  .cpick__kinds { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 2px; scrollbar-width: thin; }
  .cpick__kind {
    display: inline-flex; align-items: center; gap: 6px; flex-shrink: 0;
    height: 30px; padding: 0 11px; border-radius: var(--r-full);
    border: 1px solid var(--line); background: var(--bg-elevated);
    font: inherit; font-size: var(--t-xs); font-weight: 600; color: var(--ink-2); cursor: pointer;
    transition: all var(--d-fast) var(--e-out);
  }
  .cpick__kind:hover { border-color: var(--line-strong); color: var(--ink); }
  .cpick__kind.is-active { background: var(--ink); border-color: var(--ink); color: var(--bg-elevated); }

  .cpick__list {
    display: flex; flex-direction: column; gap: 2px;
    max-height: min(52vh, 460px); min-height: 220px; overflow-y: auto; margin: 0 -6px; padding: 0 6px;
  }
  .cpick__row {
    display: flex; align-items: center; gap: 10px;
    width: 100%; padding: 8px 10px; border: 1px solid transparent; border-radius: 12px;
    background: transparent; font: inherit; cursor: pointer; text-align: left;
    transition: background var(--d-fast) var(--e-out), border-color var(--d-fast) var(--e-out);
  }
  .cpick__row:hover, .cpick__row.is-active { background: var(--surface-hover); }
  .cpick__row.is-picked { background: var(--accent-soft); border-color: var(--accent-line, var(--accent)); }
  .cpick__check {
    display: grid; place-items: center; flex-shrink: 0;
    width: 20px; height: 20px; border-radius: 6px;
    border: 1.5px solid var(--line-strong); color: #fff;
    transition: all var(--d-fast) var(--e-spring);
  }
  .cpick__row.is-picked .cpick__check { background: var(--accent); border-color: var(--accent); transform: scale(1.05); }
  .cpick__empty {
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px;
    min-height: 200px; text-align: center; color: var(--ink-2);
  }
  .cpick__empty > .icon { color: var(--ink-4); }

  /* ─────────────────────────── Записи в полі повідомлення ─────────────────────────── */

  .composer__items { display: flex; flex-wrap: wrap; gap: 6px; padding: 8px 10px 0; }
  .composer__item {
    position: relative;
    display: flex; align-items: center;
    max-width: 280px; padding: 6px 28px 6px 7px;
    border: 1px solid var(--line); border-radius: 12px; background: var(--bg-elevated);
    animation: cshare-in var(--d-base) var(--e-spring);
  }
  .composer__item .cfile__remove { position: absolute; top: 50%; right: 6px; transform: translateY(-50%); }
  @keyframes cshare-in { from { opacity: 0; transform: translateY(4px) scale(.98); } }

  /* ─────────────────────────── Правила чатів ─────────────────────────── */

  .cpolicy { display: flex; flex-direction: column; gap: 10px; }
  .cpolicy__block {
    display: flex; flex-direction: column; gap: 10px;
    padding: 12px 14px; border: 1px solid var(--line); border-radius: 12px;
  }
  .cpolicy__switch { display: flex; gap: 12px; align-items: flex-start; cursor: pointer; }
  .cpolicy__switch input {
    appearance: none; flex-shrink: 0; position: relative;
    width: 36px; height: 21px; margin-top: 1px; border-radius: var(--r-full);
    background: var(--line-strong); cursor: pointer;
    transition: background var(--d-fast) var(--e-out);
  }
  .cpolicy__switch input::after {
    content: ""; position: absolute; top: 2px; left: 2px;
    width: 17px; height: 17px; border-radius: 50%; background: #fff;
    box-shadow: 0 1px 3px rgb(0 0 0 / .25);
    transition: transform var(--d-fast) var(--e-spring);
  }
  .cpolicy__switch input:checked { background: var(--accent); }
  .cpolicy__switch input:checked::after { transform: translateX(15px); }
  .cpolicy__switch input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
  .cpolicy__text { display: flex; flex-direction: column; gap: 2px; }
  .cpolicy__window { display: flex; align-items: center; gap: 10px; padding-left: 48px; }
  .cpolicy__window select { width: auto; min-width: 190px; }
  .cpolicy__window.is-off { opacity: .45; }

  /* ─────────────────────────── «Надіслати в чат» ─────────────────────────── */

  .csend { display: flex; flex-direction: column; gap: 10px; }
  .csend__item { padding: 10px 12px; border: 1px solid var(--line); border-radius: 12px; background: var(--bg-sunken); }
  .csend__filter { margin: 0; }
  .csend__targets { display: flex; flex-direction: column; gap: 1px; max-height: 280px; overflow-y: auto; }
  .csend__target {
    display: flex; align-items: center; gap: 9px; width: 100%;
    padding: 7px 10px; border: 0; border-radius: 10px; background: transparent;
    font: inherit; font-size: var(--t-sm); color: var(--ink); text-align: left; cursor: pointer;
  }
  .csend__target:hover { background: var(--surface-hover); }
  .csend__target.is-active { background: var(--accent-soft); font-weight: 620; }
  .csend__tick { margin-left: auto; color: var(--accent); flex-shrink: 0; }
  .csend__comment { resize: vertical; min-height: 56px; }

  /* ─────────────────────────── Миттєвий пошук ─────────────────────────── */

  .cquick__count {
    margin-left: 4px; padding: 0 6px; border-radius: var(--r-full);
    font-size: var(--t-2xs); font-weight: 650; color: var(--ink-3); background: var(--bg-sunken);
    text-transform: none; letter-spacing: 0;
  }
  .cquick__msg, .cquick__file, .cquick__item {
    display: flex; align-items: flex-start; gap: 9px; width: 100%;
    padding: 7px 10px; border: 0; border-radius: 10px; background: transparent;
    font: inherit; text-align: left; cursor: pointer; color: var(--ink);
    transition: background var(--d-fast) var(--e-out);
  }
  .cquick__item { align-items: center; }
  .cquick__msg:hover, .cquick__file:hover, .cquick__item:hover { background: var(--surface-hover); }
  .cquick__body { display: flex; flex-direction: column; gap: 2px; min-width: 0; flex: 1; }
  .cquick__meta { display: flex; align-items: baseline; gap: 6px; min-width: 0; font-size: var(--t-2xs); color: var(--ink-3); }
  .cquick__meta b { color: var(--ink-2); font-weight: 650; }
  .cquick__time { margin-left: auto; flex-shrink: 0; font-variant-numeric: tabular-nums; }
  .cquick__snippet {
    font-size: var(--t-xs); color: var(--ink-2); line-height: 1.4;
    display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; overflow-wrap: anywhere;
  }
  .cquick__thumb { width: 34px; height: 34px; border-radius: 8px; object-fit: cover; flex-shrink: 0; background: var(--bg-sunken); }
  .cquick__thumb--file { display: grid; place-items: center; color: var(--ink-3); }

  .cnav__filter .spinner, .csearch__field .spinner { width: 13px; height: 13px; color: var(--ink-4); flex-shrink: 0; }
  .spinner--sm { width: 13px; height: 13px; border-width: 2px; }

  /* Підказки пошуку: другий рядок, мініатюра, дія праворуч. */
  .csearch__label { display: flex; flex-direction: column; gap: 1px; }
  .csearch__main { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .csearch__sub {
    font-size: var(--t-2xs); color: var(--ink-3); font-weight: 400;
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  }
  .csearch__sub b { font-weight: 650; color: var(--ink-2); }
  .csearch__icon.citem__icon--blue, .csearch__icon.citem__icon--orange, .csearch__icon.citem__icon--green,
  .csearch__icon.citem__icon--violet, .csearch__icon.citem__icon--teal, .csearch__icon.citem__icon--red,
  .csearch__icon.citem__icon--amber, .csearch__icon.citem__icon--pink, .csearch__icon.citem__icon--gray {
    color: var(--kind); background: color-mix(in oklab, var(--kind) 13%, var(--bg-elevated));
  }
  .csearch__icon { overflow: hidden; }
  .csearch__thumb { width: 100%; height: 100%; object-fit: cover; }
  .csearch__alt {
    display: inline-flex; align-items: center; gap: 4px; flex-shrink: 0;
    padding: 3px 9px; border-radius: var(--r-full);
    font-size: var(--t-2xs); font-weight: 650; color: var(--accent); background: var(--accent-soft);
    opacity: 0; transition: opacity var(--d-fast) var(--e-out);
  }
  .csearch__item:hover .csearch__alt, .csearch__item.is-active .csearch__alt { opacity: 1; }
  .csearch__loading { display: flex; align-items: center; gap: 8px; padding: 8px 14px; font-size: var(--t-xs); color: var(--ink-3); }

  /* ─────────────────────────── Вкладка «Записи CRM» ─────────────────────────── */

  .csr-kinds { margin-bottom: 12px; }
  .csr-items { display: grid; grid-template-columns: repeat(auto-fill, minmax(340px, 1fr)); gap: 10px; }
  .csr-item {
    display: flex; flex-direction: column; gap: 10px;
    padding: 12px; border: 1px solid var(--line); border-radius: 14px; background: var(--bg-elevated);
    transition: border-color var(--d-fast) var(--e-out), box-shadow var(--d-fast) var(--e-out);
  }
  .csr-item:hover { border-color: var(--line-strong); box-shadow: var(--sh-sm); }
  .csr-item__main { color: inherit; text-decoration: none; min-width: 0; }
  .csr-item__main .citem { flex-wrap: wrap; }
  .csr-item__actions { display: flex; gap: 6px; justify-content: flex-end; }
  .csr-items__note { display: flex; gap: 6px; align-items: center; margin-top: 14px; }

  @media (max-width: 900px) {
    .cshare__cover { width: 72px; height: 72px; }
    .cview__fields { grid-template-columns: 1fr 1fr; }
    .csr-items { grid-template-columns: 1fr; }
    .cpolicy__window { padding-left: 0; flex-wrap: wrap; }
  }

  @media (prefers-reduced-motion: reduce) {
    .cshare, .cshare__cover img, .composer__item { transition: none; animation: none; }
    .cshare:hover { transform: none; }
  }
}
