/* ============================================
   РАТК Components
   ============================================ */

@layer components {

/* --- Page hero --- */
.hero {
  margin-bottom: var(--space-8);
}

.hero__label {
  font-family: var(--font-ui);
  font-size: var(--text-xs);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: var(--tracking-caps);
  color: var(--color-accent);
  margin-bottom: var(--space-3);
}

.hero__title {
  margin-bottom: var(--space-4);
}

.hero__subtitle {
  font-size: var(--text-lg);
  color: var(--color-text-secondary);
  line-height: var(--leading-normal);
  max-width: 600px;
}

/* --- Article / Long-form content --- */
.article {
  font-family: var(--font-body);
  font-size: var(--text-base);
  line-height: var(--leading-relaxed);
}

.article h2 {
  font-size: var(--text-2xl);
  margin-top: var(--space-16);
  margin-bottom: var(--space-6);
  padding-top: var(--space-8);
  border-top: 1px solid var(--color-border-light);
}

.article h2:first-child {
  margin-top: 0;
  padding-top: 0;
  border-top: none;
}

.article h3 {
  font-size: var(--text-xl);
  margin-top: var(--space-10);
  margin-bottom: var(--space-4);
}

.article h4 {
  font-size: var(--text-lg);
  margin-top: var(--space-8);
  margin-bottom: var(--space-3);
}

.article h5 {
  font-size: var(--text-base);
  margin-top: var(--space-6);
  margin-bottom: var(--space-3);
  font-family: var(--font-ui);
  font-weight: 600;
  color: var(--color-text-secondary);
}

.article p {
  margin-bottom: var(--space-4);
}

.article ul,
.article ol {
  margin-bottom: var(--space-4);
  padding-left: 1.75em;
}

.article li {
  margin-bottom: var(--space-2);
}

.article img {
  border-radius: var(--radius-md);
  margin: var(--space-8) 0;
}

.article figure {
  margin: var(--space-10) 0;
}

.article figcaption {
  font-family: var(--font-ui);
  font-size: var(--text-sm);
  color: var(--color-text-muted);
  margin-top: var(--space-2);
  text-align: center;
}

.article table {
  margin: var(--space-8) 0;
}

/* --- Source reference (footnote-like) --- */
@scope (.article) {
  .source-ref {
    display: inline;
    font-family: var(--font-ui);
    font-size: 0.72em;
    font-weight: 600;
    vertical-align: super;
    margin-inline-start: 0.1em;
    line-height: 1;
  }

  .source-ref a {
    color: var(--color-text);
    text-decoration: none;

    &:hover {
      color: var(--color-accent);
      text-decoration: underline;
      text-underline-offset: 2px;
    }

    &:focus-visible {
      outline: 2px solid var(--color-accent);
      outline-offset: 2px;
      border-radius: 2px;
    }
  }

  .article-footnotes {
    margin: var(--space-6) 0 var(--space-10);
  }

  .article-footnotes__rule {
    width: min(420px, 100%);
    border-top: 1px solid var(--color-text);
    opacity: 0.6;
    margin-bottom: var(--space-3);
  }

  .article-footnotes__list {
    list-style: none;
    padding: 0;
    margin: 0;
  }

  .article-footnotes__item {
    margin: 0;
    line-height: var(--leading-snug);
    border-radius: var(--radius-sm);
    transition: background var(--duration-fast) var(--ease-default);

    &:has(.article-footnotes__link:hover),
    &:has(.article-footnotes__link:focus-visible) {
      background: var(--color-accent-bg);
    }
  }

  .article-footnotes__link {
    color: var(--color-text);
    text-decoration: none;

    &:hover {
      color: var(--color-accent);
      text-decoration: underline;
      text-underline-offset: 2px;
    }
  }

  .article-footnotes__number {
    font-family: var(--font-ui);
    font-size: 0.78em;
    font-weight: 600;
    vertical-align: super;
    margin-inline-end: 0.28em;
  }
}

/* --- Table of contents sidebar --- */
.toc {
  position: sticky;
  inset-block-start: 5rem;
  max-block-size: calc(100vh - 6rem);
  max-block-size: calc(100svh - 6rem);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  container-type: inline-size;
  container-name: toc-panel;
}

.toc__title {
  font-family: var(--font-ui);
  font-size: var(--text-xs);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: var(--tracking-caps);
  color: var(--color-text-muted);
  margin-bottom: var(--space-4);
}

/* --- Scrollable area with fade masks --- */
.toc__scroll {
  position: relative;
  flex: 1 1 auto;
  min-block-size: 0;          /* critical: allows flex child to shrink below content */
  overflow-y: auto;
  overflow-x: hidden;
  padding-inline-end: var(--space-4);

  /* Custom scrollbar — Firefox */
  scrollbar-width: thin;
  scrollbar-color: transparent transparent;
}

.toc__scroll:hover {
  scrollbar-color: var(--color-border) transparent;
}

/* Custom scrollbar — WebKit (Chrome, Safari, Edge) */
.toc__scroll::-webkit-scrollbar {
  width: 4px;
}

.toc__scroll::-webkit-scrollbar-track {
  background: transparent;
}

.toc__scroll::-webkit-scrollbar-thumb {
  background: transparent;
  border-radius: 4px;
}

.toc__scroll:hover::-webkit-scrollbar-thumb {
  background: var(--color-border);
}

.toc__scroll::-webkit-scrollbar-thumb:hover {
  background: var(--color-text-muted);
}

/* --- Fade-out gradient masks (driven by JS classes on .toc) --- */
.toc__fade-top,
.toc__fade-bottom {
  position: absolute;
  inset-inline: 0;
  block-size: 2rem;
  z-index: 2;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.3s var(--ease-default);
}

.toc__fade-top {
  inset-block-start: 0;
  background: linear-gradient(to bottom, var(--color-bg) 0%, transparent 100%);
}

.toc__fade-bottom {
  inset-block-end: 0;
  background: linear-gradient(to top, var(--color-bg) 0%, transparent 100%);
}

/* JS toggles these classes on the element itself */
.toc__fade-top--visible {
  opacity: 1;
}

.toc__fade-bottom--visible {
  opacity: 1;
}

/* --- Scroll position track line (left accent) --- */
.toc__track {
  position: absolute;
  inset-block-start: 0;
  inset-inline-start: 0;
  inline-size: 2px;
  block-size: 100%;
  z-index: 1;
  pointer-events: none;
}

.toc__track-bg {
  position: absolute;
  inset-block-start: 0;
  inset-inline-start: 0;
  inline-size: 100%;
  block-size: 100%;
  background: var(--color-border-light);
  border-radius: 1px;
}

.toc__track-fill {
  position: absolute;
  inset-block-start: 0;
  inset-inline-start: 0;
  inline-size: 100%;
  block-size: 0%;
  background: var(--color-accent);
  border-radius: 1px;
  transition: block-size 120ms linear;
  will-change: block-size;
}

.toc__list {
  list-style: none;
  padding: 0;
}

.toc__list li {
  margin: 0;
}

.toc__link {
  display: block;
  font-family: var(--font-ui);
  font-size: var(--text-sm);
  color: var(--color-text-muted);
  text-decoration: none;
  padding: var(--space-2) 0;
  padding-inline-start: var(--space-4);
  border-inline-start: 2px solid transparent;
  transition: color var(--duration-fast) var(--ease-default),
              border-color var(--duration-fast) var(--ease-default),
              background var(--duration-fast) var(--ease-default);
  line-height: var(--leading-snug);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;

  &:hover {
    color: var(--color-text);
    background: var(--color-accent-bg);
  }

  &.toc__link--active {
    color: var(--color-accent);
    border-inline-start-color: var(--color-accent);
    font-weight: 500;
    background: var(--color-accent-bg);
  }

  &.toc__link--sub {
    padding-inline-start: calc(var(--space-4) + var(--space-4));
    font-size: var(--text-xs);
  }

  &.toc__link--sub2 {
    padding-inline-start: calc(var(--space-4) + var(--space-4) + var(--space-3));
    font-size: var(--text-xs);
    color: var(--color-text-muted);
  }
}

/* --- Research layout (article + toc) --- */
.research-layout {
  display: grid;
  grid-template-columns: 1fr var(--sidebar-width);
  gap: var(--space-16);
  align-items: start;
  container-type: inline-size;
  container-name: research-layout;
}

@container research-layout (max-width: 54rem) {
  .research-layout > .article,
  .research-layout > .toc {
    grid-column: 1 / -1;
  }

  .toc {
    position: static;
    max-block-size: none;
    overflow: visible;
    padding: var(--space-6);
    background: var(--color-surface-alt);
    border-radius: var(--radius-md);
    margin-block-end: var(--space-8);
    order: -1;
  }

  .toc__scroll {
    overflow: visible;
    max-block-size: none;
  }

  .toc__fade-top,
  .toc__fade-bottom,
  .toc__track {
    display: none;
  }
}

@media (max-width: 960px) {
  .research-layout {
    grid-template-columns: 1fr;
  }

  .toc {
    position: static;
    max-block-size: none;
    overflow: visible;
    padding: var(--space-6);
    background: var(--color-surface-alt);
    border-radius: var(--radius-md);
    margin-block-end: var(--space-8);
    order: -1;
  }

  .toc__scroll {
    overflow: visible;
    max-block-size: none;
  }

  .toc__fade-top,
  .toc__fade-bottom {
    display: none;
  }

  .toc__track {
    display: none;
  }
}

/* --- Source card --- */
.source-card {
  display: grid;
  grid-template-rows: auto auto 1fr auto;
  gap: var(--space-1);
  background: var(--color-surface);
  border: 1px solid var(--color-border-light);
  border-radius: var(--radius-md);
  padding: var(--space-6);
  text-decoration: none;
  color: var(--color-text);
  transition: border-color var(--duration-normal) var(--ease-default),
              box-shadow var(--duration-normal) var(--ease-default);
  container-type: inline-size;
  container-name: source-card;

  &:hover {
    border-color: var(--color-accent);
    box-shadow: var(--shadow-md);
    color: var(--color-text);
  }

  &:focus-visible {
    outline: 2px solid var(--color-accent);
    outline-offset: 2px;
  }

  &:hover .source-card__title {
    color: var(--color-accent);
  }
}

.source-card__number {
  font-family: var(--font-ui);
  font-size: var(--text-xs);
  font-weight: 600;
  color: var(--color-text-muted);
  margin-bottom: var(--space-2);
}

.source-card__title {
  font-family: var(--font-ui);
  font-size: var(--text-lg);
  font-weight: 600;
  line-height: var(--leading-snug);
  margin-bottom: var(--space-2);
  transition: color var(--duration-fast) var(--ease-default);
}

.source-card__annotation {
  font-size: var(--text-sm);
  color: var(--color-text-secondary);
  line-height: var(--leading-normal);
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.source-card__meta {
  display: flex;
  gap: var(--space-4);
  margin-block-start: var(--space-4);
  padding-block-start: var(--space-3);
  border-block-start: 1px solid var(--color-border-light);
}

.source-card__tag {
  font-family: var(--font-ui);
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  display: flex;
  align-items: center;
  gap: var(--space-1);
}

.source-card__tag svg {
  width: 14px;
  height: 14px;
  opacity: 0.6;
}

@container source-card (max-width: 28rem) {
  .source-card__title {
    font-size: var(--text-base);
  }

  .source-card__meta {
    flex-direction: column;
    align-items: flex-start;
    gap: var(--space-2);
  }
}

/* --- Sources grid --- */
.sources-grid {
  display: block;
  container-type: inline-size;
  container-name: sources-list;
}

.sources-list {
  list-style: none;
  margin: 0;
  padding: 0;

  li {
    margin: 0;
    padding-block: var(--space-3);
    border-bottom: 1px solid var(--color-border-light);
    scroll-margin-block-start: 6rem;
    content-visibility: auto;
    contain-intrinsic-size: 1px 88px;

    &:first-child {
      border-top: 1px solid var(--color-border-light);
    }
  }
}

.sources-list__link {
  display: grid;
  grid-template-columns: max-content 1fr;
  column-gap: var(--space-2);
  text-decoration: none;
  color: var(--color-text);
  line-height: var(--leading-relaxed);

  &:hover {
    color: var(--color-accent);
  }

  &:focus-visible {
    outline: 2px solid var(--color-accent);
    outline-offset: 3px;
    border-radius: var(--radius-sm);
  }
}

.sources-list__index {
  font-family: var(--font-ui);
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  margin-inline-end: 0;
  white-space: nowrap;
}

.sources-note {
  margin-block-start: var(--space-10);
}

@container sources-list (max-width: 44rem) {
  .sources-list li {
    padding-block: var(--space-2);
  }

  .sources-list__link {
    font-size: var(--text-sm);
  }
}

/* --- Source detail --- */
.source-detail {
  container-type: inline-size;
  container-name: source-detail;
}

.source-detail__header {
  margin-bottom: var(--space-8);
}

.source-detail__back {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  font-family: var(--font-ui);
  font-size: var(--text-sm);
  color: var(--color-text-muted);
  text-decoration: none;
  margin-bottom: var(--space-6);
  transition: color var(--duration-fast) var(--ease-default);
}

.source-detail__back:hover {
  color: var(--color-accent);
}

.source-detail__back svg {
  width: 16px;
  height: 16px;
}

.source-detail__title {
  margin-bottom: var(--space-4);
}

.source-detail__annotation {
  font-size: var(--text-lg);
  color: var(--color-text-secondary);
  line-height: var(--leading-relaxed);
  margin-bottom: var(--space-8);
}

.source-detail__meta {
  font-family: var(--font-ui);
  font-size: var(--text-xs);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: var(--tracking-caps);
  color: var(--color-text-muted);
  margin-bottom: var(--space-2);
}

.source-detail__image {
  border-radius: var(--radius-md);
  margin-bottom: var(--space-8);
  border: 1px solid var(--color-border-light);
}

.source-detail__image--fixed {
  aspect-ratio: 16 / 10;
}

.source-detail__image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: var(--radius-md);
}

.source-detail__image--empty {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-6);
  text-align: center;
}

.source-detail__image--empty img {
  display: none;
}

.source-detail__image-note {
  max-inline-size: 24ch;
  line-height: var(--leading-relaxed);
  color: var(--color-text-muted);
}

.source-detail__actions {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-3);
  margin-bottom: var(--space-8);
}

.source-detail__mentions {
  margin-top: var(--space-8);
}

@container source-detail (max-width: 40rem) {
  .source-detail__actions {
    flex-direction: column;
    align-items: stretch;
  }

  .source-detail__actions .btn {
    inline-size: 100%;
    justify-content: center;
  }
}

/* --- Buttons --- */
.btn {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  font-family: var(--font-ui);
  font-size: var(--text-sm);
  font-weight: 500;
  padding: var(--space-3) var(--space-5);
  border-radius: var(--radius-sm);
  text-decoration: none;
  border: 1px solid transparent;
  cursor: pointer;
  transition: all var(--duration-fast) var(--ease-default);
  white-space: nowrap;
}

.btn:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: 2px;
}

.btn svg {
  width: 16px;
  height: 16px;
}

.btn--primary {
  background: var(--color-accent);
  color: #fff;
  border-color: var(--color-accent);
}

.btn--primary:hover {
  background: var(--color-accent-hover);
  border-color: var(--color-accent-hover);
  color: #fff;
}

.btn--secondary {
  background: var(--color-surface);
  color: var(--color-text);
  border-color: var(--color-border);
}

.btn--secondary:hover {
  border-color: var(--color-accent);
  color: var(--color-accent);
  background: var(--color-accent-bg);
}

/* --- About page --- */
.about-content {
  max-width: var(--content-width);
}

.about-content p {
  margin-bottom: var(--space-4);
}

/* --- Breadcrumb --- */
.breadcrumb {
  font-family: var(--font-ui);
  font-size: var(--text-sm);
  color: var(--color-text-muted);
  margin-bottom: var(--space-6);
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.breadcrumb a {
  color: var(--color-text-muted);
  text-decoration: none;
}

.breadcrumb a:hover {
  color: var(--color-accent);
}

.breadcrumb__sep {
  opacity: 0.4;
}

/* --- Placeholder images --- */
.placeholder-img {
  background: var(--color-surface-alt);
  border: 1px dashed var(--color-border);
  border-radius: var(--radius-md);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--color-text-muted);
  font-family: var(--font-ui);
  font-size: var(--text-sm);
  min-height: 200px;
}

/* --- Reading progress bar (top) --- */
.reading-progress {
  position: fixed;
  inset-block-start: 0;
  inset-inline: 0;
  height: 3px;
  z-index: 1000;
  background: transparent;
  pointer-events: none;
}

.reading-progress__fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(
    90deg,
    var(--color-accent) 0%,
    var(--color-accent) 92%,
    color-mix(in srgb, var(--color-accent) 60%, white) 100%
  );
  box-shadow: 0 0 8px rgba(26, 79, 138, 0.35);
  border-radius: 0 2px 2px 0;
  transition: width 80ms linear;
  will-change: width;
}

/* --- TOC progress ring --- */
.toc__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--space-4);
}

.toc__header .toc__title {
  margin-bottom: 0;
}

.toc__progress {
  position: relative;
  width: 36px;
  height: 36px;
  flex-shrink: 0;
}

.toc__progress-ring {
  width: 36px;
  height: 36px;
  transform: rotate(-90deg);
}

.toc__progress-ring-bg {
  fill: none;
  stroke: var(--color-border-light);
  stroke-width: 2.5;
}

.toc__progress-ring-fill {
  fill: none;
  stroke: var(--color-accent);
  stroke-width: 2.5;
  stroke-linecap: round;
  stroke-dasharray: 97.389;    /* 2 * π * 15.5 */
  stroke-dashoffset: 97.389;
  transition: stroke-dashoffset 80ms linear;
  will-change: stroke-dashoffset;
}

.toc__progress-pct {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--font-ui);
  font-size: 9px;
  font-weight: 600;
  color: var(--color-accent);
  letter-spacing: -0.02em;
  pointer-events: none;
}

/* Hide ring on mobile where TOC is collapsed */
@container toc-panel (max-width: 14rem) {
  .toc__progress {
    display: none;
  }
}

/* Fallback for engines without container queries */
@media (max-width: 960px) {
  .toc__progress {
    display: none;
  }
}

/* --- Scroll to top --- */
.scroll-top {
  position: fixed;
  inset-block-end: var(--space-6);
  inset-inline-end: var(--space-6);
  width: 40px;
  height: 40px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-full);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--color-text-secondary);
  cursor: pointer;
  box-shadow: var(--shadow-md);
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--duration-normal) var(--ease-default),
              color var(--duration-fast) var(--ease-default),
              border-color var(--duration-fast) var(--ease-default);
  z-index: 50;

  &.scroll-top--visible {
    opacity: 1;
    pointer-events: auto;
  }

  &:hover {
    color: var(--color-accent);
    border-color: var(--color-accent);
  }

  &:focus-visible {
    outline: 2px solid var(--color-accent);
    outline-offset: 2px;
  }

  svg {
    width: 18px;
    height: 18px;
  }
}

}
