/* Розміри мають збігатися з POPUP у main.js */
:root {
  --margin: 0px; /* вікно рівно по панелі (див. POPUP.margin у main.js) */
  --pad: 10px;
  --tile-w: 124px;
  --tile-h: 34px;
  --gap: 6px;
  --section-gap: 10px;

  /* строгий чорно-білий стиль, як на сайті: чисті сірі без відтінку, без рамок і світіння */
  --panel: #000000;
  --line: rgba(255, 255, 255, 0.07);
  --bar: #0e0e0e;        /* рядок керування */
  --tile: #141414;
  --tile-hi: #222222;
  --text: #f4f4f4;
  --muted: #9a9a9a;      /* ≥ 4.5:1 на фоні панелі й плиток */
  --accent: #f4f4f4;
  --sel: rgba(255, 255, 255, 0.92);  /* вибране — біле підсвічування */
  --sel-soft: rgba(255, 255, 255, 0.14);
  --ring: rgba(255, 255, 255, 0.6);  /* лінія навколо вибраної картинки */
  --shine: rgba(255, 255, 255, 0.11); /* відблиск на комірці, поки мініатюра готується */
  --img-bg: #111111;
  --thumb: rgba(255, 255, 255, 0.16);
  --warn-bg: #f4f4f4;    /* пауза — інверсія, без кольору */
  --warn-text: #000000;
  --err-bg: #2a1111;
  --err-text: #ffb4ad;
  --info-bg: #1a1a1a;
  --info-text: #e8e8e8;
}

/* світла тема (data-theme ставить popup.js) */
:root[data-theme="light"] {
  --panel: #ffffff;
  --line: rgba(0, 0, 0, 0.08);
  --bar: #f4f4f4;
  --tile: #efefef;
  --tile-hi: #e2e2e2;
  --text: #0a0a0a;
  --muted: #5c5c5c;
  --accent: #0a0a0a;
  --sel: rgba(0, 0, 0, 0.85);
  --sel-soft: rgba(0, 0, 0, 0.1);
  --ring: rgba(0, 0, 0, 0.5);
  --shine: rgba(255, 255, 255, 0.7);
  --img-bg: #ececec;
  --thumb: rgba(0, 0, 0, 0.22);
  --warn-bg: #0a0a0a;
  --warn-text: #ffffff;
  --err-bg: #ffe4e1;
  --err-text: #8a1c12;
  --info-bg: #efefef;
  --info-text: #1f1f1f;
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  height: 100%;
  background: transparent;
  overflow: hidden;
  font: 13px/1.25 "Segoe UI Variable Text", "Segoe UI", "Microsoft YaHei UI", "Yu Gothic UI", "Malgun Gothic", "Nirmala UI", system-ui, sans-serif;
  color: var(--text);
  user-select: none;
}
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

.panel {
  position: absolute;
  inset: var(--margin);
  padding: var(--pad);
  background: var(--panel);
  border-radius: 16px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  opacity: 0;
  transform: translateY(-3px) scale(0.985);
}
/* поява: дуже коротка, щоб не гальмувати роботу */
.panel.in {
  opacity: 1;
  transform: none;
  transition: opacity 110ms ease-out, transform 140ms cubic-bezier(0.2, 0.8, 0.2, 1);
}
.panel.keep { transition: none; }

/* видимий фокус з клавіатури — нейтральний, як і вибір */
:focus-visible { outline: 2px solid var(--sel); outline-offset: 2px; }
.tile:focus-visible, .mimg:focus-visible, .trow:focus-visible { outline: none; }

/* ---------- медіа: мозаїка картинок (комірки розставляє main.js / mosaic.js) ---------- */
.mosaic {
  position: relative;
  flex: none;
  height: var(--mosaic-h, 0);
  margin-bottom: var(--section-gap);
}
.mosaic[hidden], .grid[hidden], .banner[hidden], .texts[hidden], .groups[hidden] { display: none; }
.mimg {
  position: absolute;
  border-radius: 8px;
  overflow: hidden;
  cursor: grab;
  background: var(--img-bg);
  outline: none;
  transition: filter 90ms, opacity 90ms;
}
.mimg img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  pointer-events: none;
  transition: opacity 280ms ease-out, transform 420ms cubic-bezier(0.2, 0.8, 0.2, 1);
}
/* Мініатюра ще готується (важке фото): по порожній комірці пробігає м'який відблиск — хвилею по всій
   мозаїці (затримка за номером комірки, --i), а готова картинка «проявляється»: з'являється й ледь
   осідає на місце. Анімуються лише transform і opacity одного шару; щойно картинка є — анімація зникає. */
.mimg.loading::before {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(105deg, transparent 25%, var(--shine) 50%, transparent 75%);
  transform: translateX(-100%);
  animation: shine 1.5s cubic-bezier(0.45, 0, 0.25, 1) infinite;
  animation-delay: calc(var(--i, 0) * 90ms);
}
.mimg.loading img { opacity: 0; transform: scale(1.05); }
@keyframes shine { 60%, 100% { transform: translateX(100%); } }
@media (prefers-reduced-motion: reduce) {
  .mimg.loading::before { animation: none; transform: none; opacity: 0.5; }
  .mimg img { transition: opacity 150ms; }
  .mimg.loading img { transform: none; }
}
/* наведення — трохи світліше; вибір — тонка біла лінія всередині картки */
.mimg:hover { filter: brightness(1.08); }
.mimg::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  pointer-events: none;
  transition: box-shadow 90ms;
}
/* вибрана картка — тонка напівпрозора лінія всередині, без жирної рамки */
.mimg.sel::after { box-shadow: inset 0 0 0 1px var(--ring), inset 0 0 0 2px rgba(0, 0, 0, 0.25); }
.mimg:active { cursor: grabbing; }

/* значки стану: закріплено / використано в іншій програмі */
.mimg .badges {
  position: absolute;
  left: 6px;
  top: 6px;
  display: flex;
  gap: 4px;
  z-index: 1;
  pointer-events: none;
}
.mimg .badge {
  width: 20px;
  height: 20px;
  border-radius: 6px;
  display: grid;
  place-items: center;
  background: rgba(10, 10, 14, 0.72);
  color: #fff;
}
.mimg .badge svg { width: 12px; height: 12px; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }
.mimg .badge.pinned { background: rgba(255, 255, 255, 0.92); color: #111; }

/* кнопка «Інші дії» — з'являється при наведенні та на вибраній картці */
.mimg .more, .trow .more {
  width: 24px;
  height: 22px;
  border: 0;
  border-radius: 6px;
  background: rgba(10, 10, 14, 0.72);
  color: #fff;
  display: grid;
  place-items: center;
  cursor: pointer;
  opacity: 0;
  transition: opacity 90ms, background 90ms;
}
.mimg .more { position: absolute; right: 6px; top: 6px; z-index: 1; }
.mimg .more svg, .trow .more svg { width: 14px; height: 14px; fill: currentColor; }
.mimg:hover .more, .mimg.sel .more { opacity: 1; }
.mimg .more:hover { background: rgba(10, 10, 14, 0.92); }

/* назва файлу (Джерело_слова_номер) — знизу картки */
.mimg .mname {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 14px 8px 5px;
  font-size: 10.5px;
  font-weight: 600;
  color: #fff;
  background: linear-gradient(transparent, rgba(0, 0, 0, 0.75));
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  opacity: 0;
  transition: opacity 90ms;
  pointer-events: none;
}
.mimg:hover .mname, .mimg.sel .mname { opacity: 1; }


/* рядок стану: пауза, помилка, коротке повідомлення */
.banner {
  flex: none;
  height: 30px;
  margin-bottom: 8px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  padding: 0 10px;
  font-size: 12.5px;
  font-weight: 600;
  background: var(--info-bg);
  color: var(--info-text);
  overflow: hidden;
}
.banner.paused { background: var(--warn-bg); color: var(--warn-text); }
.banner.error { background: var(--err-bg); color: var(--err-text); }
.banner-text { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ---------- рядок керування: межа між медіа й текстом ---------- */
.toolbar {
  flex: none;
  height: 36px;
  margin-bottom: 8px;
  padding: 3px;
  border-radius: 11px;
  background: var(--bar);
  display: flex;
  align-items: center;
  gap: 4px;
  /* за рядок керування (поза кнопками) панель можна перетягнути в інше місце екрана */
  -webkit-app-region: drag;
}
.toolbar .tbtn { -webkit-app-region: no-drag; }
.tbtn {
  flex: none;
  width: 34px;
  height: 30px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--text);
  cursor: pointer;
  transition: background 90ms, color 90ms;
}
.tbtn:hover { background: var(--tile-hi); }
.tbtn svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.tbtn svg.dots { fill: currentColor; stroke: none; }
.tbtn .i-play { display: none; }
.tbtn.paused { background: var(--warn-bg); color: var(--warn-text); }
.tbtn.paused .i-pause { display: none; }
.tbtn.paused .i-play { display: block; fill: currentColor; }
.tbtn:disabled { opacity: 0.5; cursor: default; }

.search {
  flex: 1;
  min-width: 0;
  height: 30px;
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 0 10px;
  border-radius: 8px;
  background: var(--panel);
  color: var(--muted);
  font-size: 13px;
  overflow: hidden;
  white-space: nowrap;
  transition: background 90ms;
}
.search svg { width: 15px; height: 15px; flex: none; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }
.search .query { color: var(--text); font-weight: 600; overflow: hidden; text-overflow: ellipsis; }
.search .caret { width: 1.5px; height: 15px; background: var(--text); flex: none; animation: blink 1s steps(1) infinite; display: none; }
.search.active { color: var(--text); }
.search.active .caret { display: block; }
.search.active .ph { display: none; }
.search .ph { overflow: hidden; text-overflow: ellipsis; }
.search.off { visibility: hidden; }
@keyframes blink { 50% { opacity: 0; } }

/* ---------- текст: групи, плитки, історія тексту ---------- */
.groups {
  flex: none;
  height: 24px;
  margin-bottom: 8px;
  display: flex;
  gap: 4px;
  overflow-x: auto;
  scrollbar-width: none;
}
.groups::-webkit-scrollbar { display: none; }
.chip {
  flex: none;
  height: 24px;
  padding: 0 10px;
  border: 0;
  border-radius: 7px;
  background: transparent;
  color: var(--muted);
  font: 600 12px/1 inherit;
  font-family: inherit;
  cursor: pointer;
  white-space: nowrap;
  max-width: 160px;
  overflow: hidden;
  text-overflow: ellipsis;
}
.chip:hover { color: var(--text); background: var(--tile); }
.chip.on { color: var(--text); background: var(--tile-hi); }
.groups.dim .chip.on { background: transparent; }

.no-results {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  font-size: 13px;
}

.grid {
  flex: none;
  display: grid;
  grid-template-columns: repeat(var(--cols, 4), var(--tile-w));
  grid-auto-rows: var(--tile-h);
  gap: var(--gap);
  height: var(--text-h, auto);
  overflow-y: auto;
}
.grid::-webkit-scrollbar { width: 8px; }
.grid::-webkit-scrollbar-thumb { background: var(--thumb); border-radius: 4px; }
.panel.scroll .grid { scrollbar-gutter: stable; }

/* компактна плитка: кольорова смужка зліва, назва, номер. --c задає popup.js */
.tile {
  --c: #4f8cff;
  position: relative;
  display: flex;
  align-items: center;
  gap: 6px;
  border: 0;
  border-radius: 7px;
  background: var(--tile);
  color: var(--text);
  font: inherit;
  text-align: left;
  padding: 0 7px 0 12px;
  cursor: pointer;
  overflow: hidden;
  outline: none;
  transition: background 90ms, transform 90ms;
}
.tile::before {
  content: "";
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 3px;
  background: var(--c);
  transition: width 90ms;
}
.tile:hover { background: var(--tile-hi); }
/* вибір: світліший фон і ширша смужка кольору плитки — без рамки */
.tile.sel { background: var(--tile-hi); }
.tile.sel::before { width: 5px; }
.tile:active { transform: scale(0.97); }
.tile .title {
  flex: 1;
  min-width: 0;
  font-size: 13px;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.tile .num {
  flex: none;
  min-width: 15px;
  height: 16px;
  border-radius: 4px;
  display: grid;
  place-items: center;
  font-size: 10.5px;
  font-weight: 700;
  color: var(--muted);
  background: color-mix(in srgb, var(--text) 7%, transparent);
}
.tile.sel .num { color: var(--text); background: var(--sel-soft); }
/* у результатах пошуку — з якої групи плитка */
.tile .gtag {
  flex: none;
  max-width: 44px;
  font-size: 10px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}


/* історія скопійованого тексту — рядками під плитками */
.texts { flex: none; margin-top: var(--section-gap); }
/* плиток немає — історія тексту одразу під рядком керування */
.grid[hidden] ~ .texts { margin-top: 0; }
.texts-head {
  height: 18px;
  font-size: 11.5px;
  font-weight: 600;
  color: var(--muted);
}
.texts-list { display: flex; flex-direction: column; gap: 4px; }
.trow {
  height: 30px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 4px 0 10px;
  border-radius: 7px;
  background: var(--tile);
  cursor: grab;
  outline: none;
  transition: background 90ms;
}
.trow:hover, .trow.sel { background: var(--tile-hi); }
.trow svg.doc { width: 14px; height: 14px; flex: none; fill: none; stroke: var(--muted); stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.trow .tline { flex: 1; min-width: 0; font-size: 12.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.trow .tpin { flex: none; display: grid; color: var(--muted); }
.trow .tpin svg { width: 13px; height: 13px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.trow .more { background: transparent; color: var(--muted); }
.trow:hover .more, .trow.sel .more { opacity: 1; }
.trow .more:hover { background: var(--sel-soft); color: var(--text); }

/* посилання: значок ланки, відтінок кольору посилань і кнопка «↗ Відкрити в браузері» */
/* без кольору: посилання — повний білий/чорний і значок ланки, звичайний текст — так само, але без ланки */
:root { --link: #ffffff; --link-bg: rgba(255, 255, 255, 0.05); }
:root[data-theme="light"] { --link: #000000; --link-bg: rgba(0, 0, 0, 0.04); }
.trow.link { background: linear-gradient(90deg, var(--link-bg), transparent 70%), var(--tile); }
.trow.link:hover, .trow.link.sel { background: linear-gradient(90deg, var(--link-bg), transparent 70%), var(--tile-hi); }
.trow.link svg.doc { stroke: var(--link); }
.trow.link .tline { color: var(--link); font-weight: 600; }
.tile.link svg.doc { width: 13px; height: 13px; flex: none; fill: none; stroke: var(--link); stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; margin-right: -2px; }
.tile.link .title { color: var(--link); }
.open {
  flex: none;
  width: 24px;
  height: 22px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--link);
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: background 90ms;
}
.open svg { width: 14px; height: 14px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.open:hover { background: var(--link-bg); }
/* на плитці кнопка не займає місця: з'являється поверх номера при наведенні чи виборі */
.tile .open { position: absolute; right: 5px; top: 50%; width: 22px; height: 22px; transform: translateY(-50%); background: var(--tile-hi); opacity: 0; pointer-events: none; }
.tile:hover .open, .tile.sel .open { opacity: 1; pointer-events: auto; }
.tile.link:hover .num, .tile.link.sel .num { visibility: hidden; }

/* кнопка перегляду в рядку історії тексту */
.trow .peek {
  flex: none;
  width: 24px;
  height: 22px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  display: grid;
  place-items: center;
  cursor: pointer;
  opacity: 0;
  transition: opacity 90ms, background 90ms;
}
.trow .peek svg { width: 15px; height: 15px; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }
.trow:hover .peek, .trow.sel .peek { opacity: 1; }
.trow .peek:hover { background: var(--sel-soft); color: var(--text); }

/* перегляд повного тексту — на місці текстової частини панелі */
.panel.previewing .groups, .panel.previewing .grid, .panel.previewing .texts { display: none; }
.preview[hidden] { display: none; }
.preview {
  flex: none;
  height: var(--preview-h, 260px);
  display: flex;
  flex-direction: column;
  border-radius: 10px;
  background: var(--tile);
  overflow: hidden;
}
.preview-head {
  flex: none;
  height: 34px;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 0 4px 0 10px;
  border-bottom: 1px solid var(--line);
}
.preview-head svg.doc { width: 14px; height: 14px; flex: none; fill: none; stroke: var(--muted); stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; margin-right: 4px; }
.preview.link .preview-head svg.doc { stroke: var(--link); }
.preview .i-link, .preview.link .i-text { display: none; }
.preview.link .i-link { display: block; }
/* у перегляді пошук не працює */
.panel.previewing .search { opacity: 0.45; }
.preview-title { flex: 1; min-width: 0; font-size: 12px; font-weight: 600; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.preview.link .preview-title { color: var(--link); }
.pbtn {
  flex: none;
  height: 26px;
  min-width: 28px;
  padding: 0 7px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  border: 0;
  border-radius: 7px;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 12px;
  cursor: pointer;
  transition: background 90ms;
}
.pbtn[hidden] { display: none; }
.pbtn:hover { background: var(--tile-hi); }
.pbtn.link { color: var(--link); }
.pbtn.link:hover { background: var(--link-bg); }
.pbtn svg { width: 14px; height: 14px; flex: none; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.preview-body {
  flex: 1;
  min-height: 0;
  overflow: auto;
  padding: 9px 12px 12px;
  font-size: 12.5px;
  line-height: 1.5;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  user-select: text;
  cursor: text;
  outline: none;
}
.preview-body::selection { background: var(--sel-soft); }
.preview-body::-webkit-scrollbar { width: 8px; }
.preview-body::-webkit-scrollbar-thumb { background: var(--thumb); border-radius: 4px; }
.preview-cut { flex: none; padding: 5px 12px; font-size: 11px; color: var(--muted); border-top: 1px solid var(--line); }
.preview-cut[hidden] { display: none; }
