/*
  Site-level overrides for the Monochrome theme.
  Goal: make AsciiDoc tables (and other structured reference elements) feel intentional and readable.
  Keep it minimal and in the spirit of the theme.
*/

/* Asciidoctor tables: Hugo/Asciidoctor emits `.tableblock` (and sometimes `mc-table`). */

/*
  Key gotcha:
  Asciidoctor output currently contains duplicate `class` attributes on <table>.
  HTML parsers keep the *first* `class`, so `mc-table` gets dropped.
  That means we must style Asciidoctor's native `.tableblock` reliably.
*/

/* Improve table container behavior and overall rhythm */
article table.tableblock {
  width: 100%;
  max-width: 100%;
  border-collapse: collapse;
  margin: 1rem 0;
  background: var(--color-background);
}

/* Match Monochrome's scroll behavior, but for AsciiDoc tables */
article table.tableblock {
  display: block;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

/* Ensure the internal table layout is preserved when the table is `display:block` */
article table.tableblock > thead,
article table.tableblock > tbody,
article table.tableblock > tfoot {
  display: table;
  width: 100%;
  table-layout: fixed;
}

/* Make header rows visually distinct without heavy styling */
article table.tableblock thead th,
article table.mc-table thead th {
  font-weight: 600;
  background: var(--color-background-hover);
  /* Avoid the "boxed text all over" look */
}

/* Cell typography and spacing */
article table.tableblock td,
article table.tableblock th,
article table.mc-table td,
article table.mc-table th {
  vertical-align: top;
  line-height: 1.35;
}

/* Borders + padding (Monochrome-like) */
article table.tableblock td,
article table.tableblock th {
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--color-border);
}

/* Thead bottom edge reads as a "header bar" */
article table.tableblock thead th {
  border-bottom-width: 2px;
}

/* Reduce paragraph spacing inside cells (AsciiDoc wraps in <p class="tableblock">) */
article table.tableblock p.tableblock {
  margin: 0;
}

article table.tableblock p.tableblock + p.tableblock {
  margin-top: 0.5rem;
}

/* Lists inside table cells should align and not explode spacing */
article table.tableblock td ul,
article table.tableblock td ol {
  margin: 0.25rem 0 0.25rem 1.1rem;
}

article table.tableblock td li {
  margin: 0.15rem 0;
}

/* Zebra striping for scan-ability (subtle, theme-consistent) */
article table.tableblock tbody tr:nth-child(even),
article table.mc-table tbody tr:nth-child(even) {
  background: color-mix(in srgb, var(--color-background) 92%, var(--color-background-hover));
}

/* Allow long phrases to wrap instead of forcing overflow */
article table.tableblock td,
article table.tableblock th {
  overflow-wrap: anywhere;
  word-break: normal;
}

/* Optional: slightly rounded corners for the table as a single object */
article table.tableblock {
  border-radius: 0.375rem;
  overflow: hidden;
}

/* If the theme keeps display:block on mc-table, make sure border radius still works */
article table.mc-table {
  border-radius: 0.375rem;
}

/* Table captions (AsciiDoc emits <caption class="title">) */
article table.tableblock > caption.title {
  caption-side: top;
  text-align: left;
  padding: 0.25rem 0.1rem;
  margin-bottom: 0.25rem;
  font-weight: 600;
  color: var(--color-text);
}

/* Make code and inline code inside cells less shouty */
article table.tableblock code {
  font-size: 0.95em;
}

/* Richer section/article index cards */
.section-list-layout {
  margin: 2rem 0;
}

.section-list-card {
  padding: 1rem;
  margin: 1.25rem 0;
  background-color: var(--color-background-card, var(--color-background));
  border-radius: 0.375rem;
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
}

.section-list-layout > .section-list-card:first-child {
  margin-top: 0;
}

.section-list-card__media {
  display: block;
  width: 100%;
  margin-bottom: 0.85rem;
}

.section-list-card__media img {
  display: block;
  width: 100%;
  max-width: 100%;
  max-height: 18rem;
  object-fit: cover;
  border-radius: 0.375rem;
  background: var(--color-background-card, var(--color-background-hover));
}

.section-list-card__body {
  min-width: 0;
}

.section-list-card__header {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.5rem 1rem;
  margin-bottom: 0.65rem;
}

.section-list-card__title {
  margin: 0;
  font-size: 1.25rem;
  line-height: 1.2;
}

.section-list-card__title a {
  text-decoration: none;
}

.section-list-card__title a:hover,
.section-list-card__title a:focus {
  text-decoration: underline;
}

.section-list-card__date {
  font-size: 0.9rem;
  color: var(--color-text-secondary, var(--color-text));
  white-space: nowrap;
}

.section-list-card__summary {
  color: var(--color-text-secondary, var(--color-text));
  font-size: 0.875rem;
  line-height: 1.6;
}

.section-list-card__meta {
  margin-top: 0.65rem;
  font-size: 0.75rem;
  color: var(--color-text-secondary, var(--color-text));
}

.section-list-card__summary > :first-child {
  margin-top: 0;
}

.section-list-card__summary > :last-child {
  margin-bottom: 0;
}

@media (max-width: 700px) {
  .section-list-card__media img {
    max-height: 14rem;
  }
}

/* ===================================================================
   Long-article navigability: chapter breaks, numbering, progress bar
   =================================================================== */

/* --- 0. Markdown horizontal rules (<hr> from ---) ---------------- */
/* Make <hr> visually distinct from the thin structural h2 border:
   thicker, but lower contrast — a soft thematic pause, not a heading. */

article hr {
  border: none;
  height: 8px;
  background: var(--color-border);
  opacity: 0.45;
  border-radius: 1px;
  margin-top: 3rem;
  margin-bottom: 1.5rem;
}

/* When an <hr> sits right before an h2 chapter heading (the hr is the
   last child of one section, the h2 opens the next sibling section),
   collapse the combined whitespace to ~1rem so they don't float apart. */
article > section:has(> hr:last-child) + section > h2 {
  margin-top: 0;
  padding-top: 0.5rem;
  border-top: none;          /* the <hr> already provides the divider */
}

/* --- 1. Visual chapter breaks ------------------------------------ */
/* Give each top-level section (which contains an h2) generous vertical
   breathing room and a subtle top rule so readers perceive discrete
   chapters, not a wall of text.
   DOM: <article> > <section id="..."> > <h2 class="header-anchor-wrapper">
   h3 sub-sections are nested *inside* the h2 section. */

article > section > h2 {
  margin-top: 3rem;
  padding-top: 2rem;
  border-top: 1px solid var(--color-border);
}

/* ===================================================================
   PDF / print export
   =================================================================== */

.article-pdf-download {
  margin: 0.5rem 0 1.25rem;
  font-size: 0.95rem;
}

.article-pdf-download a {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.35rem 0.65rem;
  border: 1px solid var(--color-border);
  border-radius: 999px;
  text-decoration: none;
}

.article-pdf-download a:hover,
.article-pdf-download a:focus {
  background: var(--color-background-hover);
}

@media print {
  @page {
    size: A4;
    margin: 18mm 22mm 22mm;
  }

  html,
  body {
    background: #fff !important;
    color: #000 !important;
    font-size: 11.5pt !important;
    line-height: 1.55 !important;
  }

  header,
  body > header,
  #header_content,
  #header_left,
  #header_right,
  #sidebar_btn,
  #theme_tool,
  #search_tool,
  .brand,
  footer,
  .reading-progress-bar,
  .no-print,
  nav,
  aside,
  .toc,
  #TableOfContents,
  .collapsible-menu,
  .breadcrumbs,
  .edit-page,
  .navbar,
  .search,
  .theme-toggle {
    display: none !important;
  }

  #content > .content-margin:first-child {
    display: none !important;
  }

  .pdf-frontmatter {
    display: block;
    margin: 0 auto 2rem;
    padding: 0;
    width: 100%;
    max-width: 34rem;
  }

  main,
  #content,
  .content-margin,
  article {
    width: 100% !important;
    max-width: 34rem !important;
    margin: 0 auto !important;
    padding: 0 !important;
  }

  article,
  article p,
  article li,
  article blockquote {
    font-size: 11.5pt !important;
    line-height: 1.55 !important;
  }

  article p,
  article ul,
  article ol,
  article blockquote,
  article pre,
  article table {
    margin-top: 0;
    margin-bottom: 0.95rem;
  }

  article h1 {
    font-size: 22pt !important;
    line-height: 1.2 !important;
  }

  article h2 {
    font-size: 15pt !important;
    line-height: 1.3 !important;
  }

  article h3 {
    font-size: 12.5pt !important;
    line-height: 1.35 !important;
  }

  a,
  a:visited {
    color: inherit !important;
    text-decoration: none;
  }

  html[data-pdf-link-mode="public"] article a[href^="http"]::after {
    content: " (" attr(href) ")";
    font-size: 0.85em;
    word-break: break-all;
  }

  html[data-pdf-link-mode="local"] article a[href^="http"]::after {
    content: "";
  }

  .pdf-title-page {
    min-height: 70vh;
    display: flex;
    flex-direction: column;
    justify-content: center;
    page-break-after: always;
    break-after: page;
    margin: 0;
  }

  .pdf-title-page__title {
    font-size: 2.4rem;
    font-weight: 700;
    line-height: 1.15;
    margin: 0 0 1rem;
  }

  .pdf-title-page__description {
    font-size: 12pt;
    line-height: 1.6;
    max-width: 34rem;
    color: #333;
    margin: 0 0 1.5rem;
  }

  .pdf-title-page__source {
    font-size: 0.85rem;
    color: #666;
    word-break: break-all;
    margin: 0;
  }

  .pdf-toc {
    page-break-after: always;
    break-after: page;
    margin: 0;
    max-width: 34rem;
  }

  .pdf-toc__title {
    font-size: 1.5rem;
    font-weight: 700;
    margin: 0 0 1.25rem;
  }

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

  .pdf-toc__item {
    margin: 0 0 0.65rem;
    line-height: 1.45;
    font-size: 11.5pt;
  }

  .pdf-toc__link {
    display: inline-grid;
    grid-template-columns: auto 1fr;
    column-gap: 0.3rem;
    align-items: baseline;
    color: inherit !important;
    text-decoration: none !important;
  }

  .pdf-toc__number {
    min-width: 2.1rem;
    font-variant-numeric: tabular-nums;
    color: #666;
  }

  .pdf-toc__text {
    flex: 1;
  }

  .pdf-toc__item--h3 {
    padding-left: 1.5rem;
    color: #333;
    font-size: 0.95rem;
  }

  img,
  table,
  pre,
  blockquote {
    break-inside: avoid;
    page-break-inside: avoid;
  }

  article table,
  article table.tableblock,
  article table.mc-table {
    width: min(100%, 44rem) !important;
    max-width: 44rem !important;
    margin-left: 50% !important;
    margin-right: 0 !important;
    transform: translateX(-50%);
    font-size: 10pt !important;
  }

  article table.tableblock,
  article table.mc-table {
    display: table !important;
    overflow: visible !important;
  }

  article table thead,
  article table tbody,
  article table tfoot {
    display: table-header-group;
  }

  article table tbody {
    display: table-row-group;
  }

  article table tfoot {
    display: table-footer-group;
  }

  article table th,
  article table td {
    font-size: 10pt !important;
    line-height: 1.35 !important;
    word-break: normal;
    overflow-wrap: anywhere;
  }

  h1,
  h2,
  h3,
  h4 {
    break-after: avoid-page;
    page-break-after: avoid;
  }

  article > section > h2 {
    border-top: none;
    padding-top: 0;
    margin-top: 2rem;
  }

  article hr {
    opacity: 0.2;
    height: 2px;
    margin: 2rem 0 1rem;
  }

  article table.tableblock,
  article table.tableblock > thead,
  article table.tableblock > tbody,
  article table.tableblock > tfoot {
    display: table;
    width: 100%;
    table-layout: auto;
  }

  pre,
  code {
    white-space: pre-wrap;
    word-break: break-word;
  }
}

/* The intro section (no h2) and the first h2 section need no top divider */
article > section:first-child > h2,
article > section:nth-child(2) > h2 {
  border-top: none;
  margin-top: 0;
  padding-top: 0;
}

/* --- 2. Section numbering (CSS counters) ------------------------- */
/* Auto-number h2 headings so readers can orient themselves
   ("I'm in section 7 of 16") — pairs with the ToC. */

article {
  counter-reset: chapter-counter section-counter;
}

article > section > h2::before {
  counter-increment: chapter-counter;
  content: counter(chapter-counter) ". ";
  color: var(--color-text-secondary);
  font-weight: 400;
}

article > section > h3::before {
  counter-increment: section-counter;
  content: counter(chapter-counter) "." counter(section-counter) ". ";
  color: var(--color-text-secondary);
  font-weight: 400;
}

article > section:has(> h2) {
  counter-reset: section-counter;
}

/* --- 2b. ToC numbering ------------------------------------------ */
/* Mirror article numbering in the generated ToC, but keep it subtle
   so scanning stays easy. Hugo emits nested <nav id="TableOfContents">
   lists for h2/h3 in the monochrome ToC wrappers. */

.collapsible-menu nav#TableOfContents > ul {
  counter-reset: toc-h2;
}

.collapsible-menu nav#TableOfContents > ul > li {
  counter-increment: toc-h2;
  counter-reset: toc-h3;
}

.collapsible-menu nav#TableOfContents > ul > li > a::before {
  content: counter(toc-h2) ". ";
  color: var(--color-text-secondary);
  opacity: 0.7;
  font-variant-numeric: tabular-nums;
}

.collapsible-menu nav#TableOfContents > ul > li > ul > li {
  counter-increment: toc-h3;
}

.collapsible-menu nav#TableOfContents > ul > li > ul > li > a::before {
  content: counter(toc-h2) "." counter(toc-h3) ". ";
  color: var(--color-text-secondary);
  opacity: 0.6;
  font-variant-numeric: tabular-nums;
}

/* --- 3. Reading progress bar ------------------------------------- */
/* A thin fixed bar at the very top of the viewport showing how far
   the reader has scrolled through the page. */

.reading-progress-bar {
  position: fixed;
  top: 0;
  left: 0;
  width: 0%;
  height: 3px;
  background: var(--color-text-secondary);
  z-index: 9999;
  transition: width 50ms linear;
  pointer-events: none;
}
