/* ============================================================================
   Внутрішня документація та вбудований переглядач документів.
   Окремий файл, щоб не змішувати з основною дизайн-системою; токени — з app.css.
   ========================================================================= */

@layer components {

  /* ─────────────────────────── Переглядач ─────────────────────────── */

  .dv {
    --dv-stage: #e3e6ec;
    --dv-info: 360px;
    position: fixed; inset: 0; z-index: 80;
    display: flex; flex-direction: column;
    background: var(--surface-solid);
    color: var(--ink);
    animation: dv-in .18s var(--e-out);
  }
  :root[data-theme="dark"] .dv { --dv-stage: #0a0d12; }
  /* Відкритий із бічної панелі (картка завдання) — над нею, але під тостами. */
  .dv.dv--top { z-index: 120; }
  @keyframes dv-in { from { opacity: 0; transform: scale(.985); } }

  .dv__bar {
    flex: none;
    display: flex; align-items: center; gap: 8px;
    height: 56px; padding: 0 10px 0 8px;
    border-bottom: 1px solid var(--line);
    background: var(--surface-solid);
  }
  .dv__title { display: flex; flex-direction: column; min-width: 0; flex: 0 1 auto; line-height: 1.25; }
  .dv__title b { font-size: var(--t-sm); font-weight: 620; }
  .dv__badge { flex: none; }

  .dv__kind {
    flex: none; display: grid; place-items: center;
    width: 30px; height: 30px; border-radius: 9px;
    color: var(--kc, var(--ink-3));
    background: color-mix(in oklab, var(--kc, var(--ink-3)) 13%, transparent);
  }

  .dv__group {
    flex: none; display: flex; align-items: center; gap: 2px;
    padding-left: 8px; margin-left: 2px;
    border-left: 1px solid var(--line);
  }
  .dv__group:first-of-type { border-left: 0; }
  .dv__group .icon-btn:disabled { opacity: .35; pointer-events: none; }
  .dv__group .icon-btn.is-active { background: var(--accent-soft); color: var(--accent); }
  .dv__percent {
    min-width: 52px; height: 30px; padding: 0 6px;
    border-radius: var(--r-sm);
    font-size: var(--t-sm); font-variant-numeric: tabular-nums; color: var(--ink-2);
  }
  .dv__percent:hover { background: var(--surface-hover); color: var(--ink); }
  .dv__pages { flex: none; font-variant-numeric: tabular-nums; padding: 0 4px; }

  .dv__body { flex: 1; min-height: 0; display: flex; position: relative; overflow: hidden; }

  .dv__viewport { flex: 1; min-width: 0; position: relative; background: var(--dv-stage); }
  .dv__stage { position: absolute; inset: 0; overflow: auto; overscroll-behavior: contain; }
  .dv__stage:has(.dv-sheets) { overflow: hidden; }

  .dv__state {
    position: absolute; inset: 0;
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px;
    padding: 24px; text-align: center;
  }
  .dv__state .t-sm { max-width: 420px; }
  .dv__state-icon {
    display: grid; place-items: center; width: 64px; height: 64px; border-radius: 18px;
    background: var(--surface-solid); color: var(--ink-3); box-shadow: var(--sh-sm);
  }

  .dv__video { position: absolute; inset: 0; display: grid; place-items: center; background: #000; }
  .dv__video video { width: 100%; height: 100%; max-width: 100%; object-fit: contain; background: #000; }

  /* Панель відомостей: праворуч на широкому екрані, знизу на телефоні. */
  .dv__info {
    flex: none; width: var(--dv-info);
    margin-right: calc(var(--dv-info) * -1);
    display: flex; flex-direction: column;
    border-left: 1px solid var(--line);
    background: var(--surface-solid);
    transition: margin-right .24s var(--e-out);
  }
  .dv--info .dv__info { margin-right: 0; }
  .dv__info-head {
    flex: none; display: flex; align-items: center; justify-content: space-between;
    height: 48px; padding: 0 8px 0 16px; border-bottom: 1px solid var(--line);
  }
  .dv__info-body { flex: 1; min-height: 0; overflow: auto; padding: 16px; }
  .dv__scrim { display: none; }

  @media (max-width: 900px) {
    .dv__hide-sm, .dv__percent, .dv__badge { display: none; }
    .dv__bar { gap: 4px; padding-right: 4px; }
    .dv__group { padding-left: 4px; margin-left: 0; }
    .dv__title { flex: 1; }
    .dv__info {
      position: absolute; left: 0; right: 0; bottom: 0; z-index: 3;
      width: auto; max-height: 76%; margin: 0;
      border-left: 0; border-radius: var(--r-xl) var(--r-xl) 0 0;
      box-shadow: var(--sh-lg);
      transform: translateY(102%);
      transition: transform .26s var(--e-out);
    }
    .dv--info .dv__info { transform: none; }
    .dv__info-head .icon-btn svg { transform: rotate(90deg); }
    .dv__title b, .dv__title span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
    .dv--info .dv__scrim {
      display: block; position: absolute; inset: 0; z-index: 2;
      background: rgba(10, 13, 18, .38);
    }
  }

  @media (max-width: 600px) {
    .dv__kind, .dv__nav, .dv__pages { display: none; }
    .dv__group:first-of-type, .dv__zoom { border-left: 0; }
  }

  /* PDF */
  .dv-pdf {
    display: flex; flex-direction: column; align-items: center; gap: 30px;
    width: max-content; min-width: 100%;
    padding: 24px 16px 56px;
  }
  .dv-pdf__page { position: relative; flex: none; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.12), 0 6px 24px rgba(0,0,0,.10); }
  .dv-pdf__page canvas { display: block; width: 100%; height: 100%; max-width: none; }
  .dv-pdf__num {
    position: absolute; left: 0; right: 0; bottom: -21px;
    text-align: center; font-size: 11px; color: var(--ink-4); font-variant-numeric: tabular-nums;
  }

  /* Word */
  .dv-docx { min-width: 100%; width: max-content; }
  .dv-docx .docx-wrapper { background: transparent !important; padding: 24px 16px 48px !important; }
  .dv-docx .docx-wrapper > section.docx { box-shadow: 0 1px 3px rgba(0,0,0,.12), 0 6px 24px rgba(0,0,0,.10) !important; margin-bottom: 24px !important; }

  /* PowerPoint */
  .dv-pptx {
    display: flex; flex-direction: column; align-items: center;
    width: max-content; min-width: 100%;
    padding: 24px 16px 56px;
  }
  .dv-pptx__deck { flex: none; }
  .dv-pptx__deck img { max-width: none; }
  /* Бібліотека робить власне вікно з прокруткою й чорним тлом — слайди мають іти стрічкою, як сторінки PDF. */
  .dv-pptx .pptx-preview-wrapper { height: auto !important; overflow: visible !important; background: transparent !important; }
  .dv-pptx .pptx-preview-slide-wrapper {
    margin: 0 auto 24px !important;
    box-shadow: 0 1px 3px rgba(0,0,0,.12), 0 6px 24px rgba(0,0,0,.10);
  }

  /* Excel і CSV */
  .dv-sheets { position: absolute; inset: 0; display: flex; flex-direction: column; background: var(--surface-solid); }
  .dv-sheets__grid { flex: 1; min-height: 0; overflow: auto; }
  .dv-sheets__tabs {
    flex: none; display: flex; gap: 4px; overflow-x: auto;
    padding: 6px 8px; border-top: 1px solid var(--line); background: var(--bg-sunken);
  }
  .dv-sheets__tab {
    flex: none; height: 28px; padding: 0 12px; border-radius: var(--r-sm);
    font-size: var(--t-sm); color: var(--ink-2); white-space: nowrap;
  }
  .dv-sheets__tab:hover { background: var(--surface-hover); color: var(--ink); }
  .dv-sheets__tab.active { background: var(--surface-solid); color: var(--ok); font-weight: 600; box-shadow: var(--sh-xs); }

  .dv-grid { border-collapse: separate; border-spacing: 0; font-size: 12.5px; font-variant-numeric: tabular-nums; }
  .dv-grid th, .dv-grid td {
    height: 24px; padding: 3px 8px;
    border-right: 1px solid var(--line); border-bottom: 1px solid var(--line);
    white-space: nowrap; max-width: 480px; overflow: hidden; text-overflow: ellipsis;
    text-align: left; vertical-align: top;
  }
  .dv-grid td.num { text-align: right; }
  .dv-grid thead th {
    position: sticky; top: 0; z-index: 2;
    background: var(--bg-sunken); color: var(--ink-3); font-weight: 560; text-align: center;
  }
  .dv-grid tbody th {
    position: sticky; left: 0; z-index: 1;
    min-width: 44px; background: var(--bg-sunken); color: var(--ink-3); font-weight: 500; text-align: center;
  }
  .dv-grid .dv-grid__corner { left: 0; z-index: 3; }
  .dv-note, .dv-empty { padding: 12px 16px; font-size: var(--t-sm); color: var(--ink-3); }

  /* Текст */
  .dv-text {
    margin: 24px auto 48px; width: min(920px, calc(100% - 32px));
    padding: 28px 32px; border-radius: var(--r-md);
    background: var(--surface-solid); color: var(--ink); box-shadow: var(--sh-sm);
    font: 13px/1.65 ui-monospace, "Cascadia Mono", "SF Mono", Consolas, monospace;
    white-space: pre-wrap; overflow-wrap: anywhere;
  }

  /* Фото */
  .dv-image { position: absolute; inset: 0; display: flex; overflow: auto; padding: 16px; }
  .dv-image img { margin: auto; max-width: 100%; max-height: 100%; object-fit: contain; cursor: zoom-in; user-select: none; }
  .dv-image.is-zoomed img { max-width: none; max-height: none; cursor: zoom-out; }

  .dv-print-frame { position: fixed; right: 0; bottom: 0; width: 0; height: 0; border: 0; }

  /* Кольори типів файлів — спільні для переглядача й карток. */
  .dv__kind--pdf, .kb-kind--pdf { --kc: #e5484d; }
  .dv__kind--word, .kb-kind--word { --kc: #2f6fed; }
  .dv__kind--excel, .kb-kind--excel { --kc: #1f9d55; }
  .dv__kind--text, .kb-kind--text { --kc: #6b7384; }
  .dv__kind--image, .kb-kind--image { --kc: #8e4ec6; }
  .dv__kind--video, .kb-kind--video { --kc: #d6409f; }
  .dv__kind--presentation, .kb-kind--presentation { --kc: #e8590c; }
  .dv__kind--other, .kb-kind--other { --kc: #6b7384; }

  /* ─────────────────────────── Сторінка розділу ─────────────────────────── */

  .kb-side .card__body { display: flex; flex-direction: column; gap: 2px; }
  .kb-side__label {
    margin: 12px 12px 4px;
    font-size: var(--t-xs); font-weight: 600; letter-spacing: .04em; text-transform: uppercase; color: var(--ink-4);
  }
  .kb-side__row { position: relative; display: flex; align-items: center; }
  .kb-side__row .folder { flex: 1; min-width: 0; }
  .kb-side__edit {
    position: absolute; right: 34px; width: 26px; height: 26px;
    opacity: 0; transition: opacity var(--d-fast) var(--e-out);
  }
  .kb-side__row:hover .kb-side__edit, .kb-side__edit:focus-visible { opacity: 1; }

  /* Підрозділи: відступ за рівнем і дії розділу при наведенні. */
  .kb-side__row { padding-left: calc(var(--kb-depth, 0) * 16px); }
  .kb-side__tools {
    position: absolute; right: 30px; display: flex; gap: 2px;
    border-radius: var(--r-sm); background: var(--surface-solid);
    opacity: 0; transition: opacity var(--d-fast) var(--e-out);
  }
  .kb-side__tools .icon-btn { width: 26px; height: 26px; }
  .kb-side__row:hover .kb-side__tools, .kb-side__tools:focus-within { opacity: 1; }
  @media (hover: none) { .kb-side__tools { opacity: 1; } }

  .kb-chips { display: none; }
  .kb-search { max-width: 300px; flex: 1 1 220px; }

  @media (max-width: 1000px) {
    .kb-side { display: none; }
    .kb-chips {
      display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none;
      margin: 0 -2px; padding: 2px;
    }
    .kb-chips .chip { flex: none; }
    .kb-chips--sub { margin-top: -6px; }
    .kb-chips--sub .chip { font-size: var(--t-xs); }
  }

  .kb-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(268px, 1fr)); gap: 12px; }

  .kb-card {
    display: flex; gap: 12px; min-width: 0;
    padding: 14px; text-align: left;
    border: 1px solid var(--line); border-radius: var(--r-lg);
    background: var(--surface-solid); box-shadow: var(--sh-xs);
    transition: border-color var(--d-fast) var(--e-out), box-shadow .18s var(--e-out), transform .18s var(--e-out);
  }
  .kb-card:hover { border-color: var(--line-strong); box-shadow: var(--sh-md); transform: translateY(-1px); }
  .kb-card:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

  .kb-card__thumb {
    position: relative; flex: none;
    display: grid; place-items: center; align-content: start; padding-top: 12px;
    width: 52px; height: 66px; border-radius: 10px;
    color: var(--kc); background: color-mix(in oklab, var(--kc) 12%, transparent);
  }
  .kb-card__ext {
    position: absolute; left: 0; right: 0; bottom: 7px;
    text-align: center; font-size: 9.5px; font-weight: 700; letter-spacing: .05em;
  }
  .kb-card__body { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 5px; }
  .kb-card__title {
    font-size: .875rem; font-weight: 620; line-height: 1.3; color: var(--ink);
    display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
  }
  .kb-card__chips { display: flex; flex-wrap: wrap; gap: 4px; }
  .kb-card__meta { display: flex; align-items: center; gap: 6px; margin-top: auto; padding-top: 2px; min-width: 0; }
  .kb-card__date { margin-left: auto; flex: none; font-variant-numeric: tabular-nums; }

  .kb-tag {
    display: inline-flex; align-items: center; height: 20px; padding: 0 7px;
    border-radius: var(--r-full); background: var(--muted-soft);
    font-size: 11px; font-weight: 520; color: var(--ink-2); white-space: nowrap;
  }
  .kb-tag--all { background: var(--accent-soft); color: var(--accent); }
  .kb-dot { flex: none; width: 7px; height: 7px; border-radius: 50%; }

  .kb-positions { display: flex; flex-wrap: wrap; gap: 6px; max-height: 196px; overflow: auto; padding: 2px; }

  .kb-icons { display: grid; grid-template-columns: repeat(8, 1fr); gap: 6px; }
  .kb-icon {
    display: grid; place-items: center; aspect-ratio: 1;
    border: 1px solid var(--line); border-radius: var(--r-sm); color: var(--ink-2);
  }
  .kb-icon:hover { border-color: var(--line-strong); color: var(--ink); }
  .kb-icon.active { border-color: var(--accent-line); background: var(--accent-soft); color: var(--accent); }

  /* Панель відомостей документа */
  .kb-info { display: flex; flex-direction: column; }
  .kb-info__desc { margin-bottom: 14px; color: var(--ink-2); white-space: pre-line; }
  .kb-info__list { display: grid; grid-template-columns: 1fr; gap: 3px; }
  .kb-info__list dt {
    margin-top: 9px;
    font-size: var(--t-xs); font-weight: 560; color: var(--ink-4);
  }
  .kb-info__list dt:first-child { margin-top: 0; }
  .kb-info__list dd { margin: 0; font-size: var(--t-sm); display: flex; flex-direction: column; gap: 2px; }
  .kb-info__file { overflow-wrap: anywhere; }
  .kb-info__actions { display: flex; flex-wrap: wrap; gap: 6px; margin: 18px 0 6px; }
  .kb-info__label {
    margin: 18px 0 8px;
    font-size: var(--t-xs); font-weight: 600; letter-spacing: .04em; text-transform: uppercase; color: var(--ink-4);
  }

  .kb-versions { list-style: none; padding: 0; display: flex; flex-direction: column; gap: 8px; }
  .kb-version {
    display: flex; flex-direction: column; gap: 2px; min-width: 0;
    padding: 10px 12px; border: 1px solid var(--line); border-radius: var(--r-md);
  }
  .kb-version.is-viewing { border-color: var(--accent-line); background: var(--accent-soft); }
  .kb-version__head { display: flex; align-items: center; gap: 6px; font-size: var(--t-sm); }
  .kb-version__actions { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; }
  .kb-version__actions:empty { display: none; }
  .btn--xs { height: 26px; padding: 0 8px; gap: 4px; font-size: var(--t-xs); }

  /* ─────────────────────────── Матеріали завдань ─────────────────────────── */

  /* Пункт чек-листа: скріпка праворуч від тексту, матеріали — під ним із відступом чекбокса. */
  .checklist__entry { position: relative; }
  .checklist__entry > .checklist__item { padding-right: 42px; }
  .tm-item__add { position: absolute; top: 4px; right: 4px; width: 30px; height: 30px; color: var(--ink-4); }
  .tm-item__add:hover { color: var(--accent); }
  .tm-item { display: flex; flex-direction: column; gap: 8px; padding: 0 10px 8px 38px; }

  .tm-chips { display: flex; flex-wrap: wrap; gap: 6px; }
  .tm-chip {
    display: inline-flex; align-items: center; max-width: 100%; min-width: 0;
    border: 1px solid var(--line); border-radius: var(--r-full);
    background: var(--surface-solid);
  }
  .tm-chip__open {
    display: inline-flex; align-items: center; gap: 6px; min-width: 0; max-width: 280px;
    padding: 3px 10px; font-size: var(--t-xs); color: var(--ink-2); text-decoration: none;
    border-radius: var(--r-full);
  }
  .tm-chip__open > svg { flex: none; color: var(--kc, var(--ink-3)); }
  .tm-chip__open:hover { color: var(--accent); }
  .tm-chip__x {
    display: grid; place-items: center; width: 22px; height: 22px; margin-right: 2px;
    border-radius: var(--r-full); color: var(--ink-4);
  }
  .tm-chip__x:hover { color: var(--danger, #e5484d); background: var(--surface-hover); }

  .tm-list { display: flex; flex-direction: column; gap: 2px; }
  .tm-row { display: flex; align-items: center; gap: 6px; }
  .tm-row__open {
    display: flex; align-items: center; gap: 10px; flex: 1; min-width: 0;
    padding: 6px 8px; border-radius: var(--r-sm); text-align: left; color: inherit; text-decoration: none;
  }
  .tm-row__open:hover { background: var(--surface-hover); }
  .tm-row__icon {
    display: grid; place-items: center; flex: none; width: 32px; height: 32px; border-radius: var(--r-sm);
    color: var(--kc); background: color-mix(in oklab, var(--kc) 12%, transparent);
  }

  .tm-picker {
    display: flex; flex-direction: column; gap: 10px;
    padding: 10px 12px; border: 1px solid var(--line); border-radius: var(--r-md);
    background: var(--bg-sunken);
  }
  .tm-tabs { display: flex; flex-wrap: wrap; gap: 6px; }
  .tm-tabs .chip { display: inline-flex; align-items: center; gap: 5px; }
  .tm-results { display: flex; flex-direction: column; gap: 4px; max-height: 280px; overflow: auto; }
  .tm-result { display: flex; align-items: center; gap: 10px; padding: 4px 2px; }
  .tm-result .tm-row__icon { width: 28px; height: 28px; }
}
