/* =========================================================================
   markdown-viewer.css
   @module MarkdownViewerStyles
   @description Dedicated stylesheet for the dynamic document viewer page
   (governance/policy-viewer.html). Defines the two-column grid layout,
   sticky Table of Contents sidebar, rendered Markdown typography, and
   all responsive adaptations.

   Scope: Styles in this file apply exclusively within the context of
   policy-viewer.html. They must not be loaded on other pages.

   Component inventory:
     - .viewer-layout   : Two-column CSS Grid (TOC sidebar + content area).
     - .viewer-toc      : Sticky sidebar with generated anchor links.
     - .viewer-content  : Main document reading column.
     - .viewer-loading  : Pre-fetch loading state with CSS spinner.
     - .viewer-spinner  : Pure-CSS rotating ring animation.
     - .viewer-error    : Error state block (fetch failure / missing file).
     - #md-content      : Scoped typography rules for GFM-rendered Markdown.

   @dependencies
     variables.css (all var() references)
     assets/js/md-loader.js (populates TOC and content at runtime)

   @breakpoint
     max-width: 992px — collapses to single-column; TOC becomes a scrollable
                        box above the content column.
     max-width: 600px — further reduces content padding; tables become
                        horizontally scrollable.
   ========================================================================= */

/* -------------------------------------------------------------------------
   @component ViewerLayout
   @description Two-column CSS Grid container for the document viewer page.
   Column order in RTL: right column = .viewer-toc, left column = .viewer-content.

   @breakpoint max-width: 992px — single column (minmax(0, 1fr)).
   ------------------------------------------------------------------------- */
.viewer-layout {
  display: grid;
  grid-template-columns: 280px 1fr;
  gap: var(--space-6);
  align-items: start;
  padding-top: var(--space-6);
  padding-bottom: var(--space-7);
}

/* -------------------------------------------------------------------------
   @component ViewerToc
   @description Sticky sidebar containing the auto-generated Table of Contents.
   Populated entirely at runtime by md-loader.js → buildTableOfContents().

   Child elements:
     h2             — section heading "فهرس المحتويات".
     ul             — list of anchor links targeting h2/h3 heading slugs.
     li.toc-level-2 — top-level TOC entry (H2 headings).
     li.toc-level-3 — second-level TOC entry (H3 headings), indented via padding.
     a[data-toc-target] — anchor link; read by activateScrollSpy() to match
                          IntersectionObserver entries to TOC links.
     .toc-empty     — message rendered when the document has no H2/H3 headings.

   @states
     a.active — applied/removed by md-loader.js → activateScrollSpy()
                via IntersectionObserver when the linked heading enters viewport.

   @breakpoint max-width: 992px — position: static; max-height: none.
   JS DEPENDENCY: assets/js/md-loader.js → buildTableOfContents(), activateScrollSpy()
   ------------------------------------------------------------------------- */
.viewer-toc {
  position: sticky;
  top: calc(var(--header-height) + var(--space-4));
  max-height: calc(100vh - var(--header-height) - var(--space-6));
  overflow-y: auto;
  background: var(--color-bg-alt);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: var(--space-4);
}

.viewer-toc h2 {
  font-size: 15px;
  color: var(--color-primary-dark);
  margin-bottom: var(--space-3);
  padding-bottom: var(--space-2);
  border-bottom: 2px solid var(--color-accent);
}

.viewer-toc ul {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.viewer-toc li a {
  display: block;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  font-size: 13.5px;
  color: var(--color-text-muted);
  border-right: 3px solid transparent;
  transition: all var(--transition-fast);
}

.viewer-toc li.toc-level-3 a {
  padding-right: var(--space-4);
  font-size: 13px;
}

.viewer-toc li a:hover {
  background: var(--color-primary-light);
  color: var(--color-primary);
}

.viewer-toc li a.active {
  background: var(--color-primary-light);
  color: var(--color-primary-dark);
  border-right-color: var(--color-accent);
  font-weight: 700;
}

.toc-empty {
  font-size: 13px;
  color: var(--color-text-muted);
}

/* -------------------------------------------------------------------------
   @component ViewerContent
   @description Primary reading column. Receives the full sanitized HTML
   output from md-loader.js. Font family uses --font-official (Amiri serif)
   for formal document presentation.

   @sideeffects (applied by md-loader.js)
     innerHTML is fully replaced with <h1.doc-title> + <div#md-body> structure.
   JS DEPENDENCY: assets/js/md-loader.js → initPolicyViewer()
   ------------------------------------------------------------------------- */
.viewer-content {
  background: #fff;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
  padding: var(--space-6);
  min-height: 400px;
  font-family: var(--font-official);
  line-height: 1.9;
  min-width: 0;
  max-width: 100%;
  overflow-wrap: break-word;
  word-wrap: break-word;
}

/* -------------------------------------------------------------------------
   @component ViewerLoading
   @description Pre-fetch loading state displayed inside #md-content before
   the document is fetched and rendered. Removed from the DOM by md-loader.js
   when innerHTML is replaced.

   Child elements:
     .viewer-spinner — animated CSS ring (role="status" aria-label set in HTML).
   JS DEPENDENCY: assets/js/md-loader.js → initPolicyViewer()
   ------------------------------------------------------------------------- */
.viewer-loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--space-3);
  padding: var(--space-8) var(--space-4);
  color: var(--color-text-muted);
}

/* -------------------------------------------------------------------------
   @component ViewerSpinner
   @description Pure-CSS rotating ring animation used as a loading indicator.
   Driven by the @keyframes viewer-spin animation (360° rotation, 0.8s linear).
   ------------------------------------------------------------------------- */
.viewer-spinner {
  width: 42px;
  height: 42px;
  border: 4px solid var(--color-border);
  border-top-color: var(--color-primary);
  border-radius: 50%;
  animation: viewer-spin 0.8s linear infinite;
}

@keyframes viewer-spin {
  to { transform: rotate(360deg); }
}

/* -------------------------------------------------------------------------
   @component ViewerError
   @description Error state block injected into #md-content by md-loader.js
   when the fetch request fails (HTTP error or missing file).
   JS DEPENDENCY: assets/js/md-loader.js → renderError()
   ------------------------------------------------------------------------- */
.viewer-error {
  text-align: center;
  padding: var(--space-6);
  color: var(--color-danger);
  background: #FCEAEA;
  border-radius: var(--radius-md);
}

.viewer-error h3 {
  color: var(--color-danger);
  margin-bottom: var(--space-2);
}

/* -------------------------------------------------------------------------
   @component DocTitle
   @description Primary H1 heading injected at the top of the document
   by md-loader.js. Uses --font-heading (Cairo) and an accent underline
   to distinguish it from the body serif font used in #md-body.
   JS DEPENDENCY: assets/js/md-loader.js → initPolicyViewer()
   ------------------------------------------------------------------------- */
.viewer-content > h1.doc-title {
  font-family: var(--font-heading);
  border-bottom: 3px solid var(--color-accent);
  padding-bottom: var(--space-3);
  margin-bottom: var(--space-5);
}

/* =========================================================================
   Scoped GFM / Markdown Content Typography (#md-content)
   Applies exclusively within the rendered Markdown body.
   All rules are prefixed with #md-content to prevent leaking into other
   components on the page.
   ========================================================================= */

/* -------------------------------------------------------------------------
   Headings (H2 / H3 / H4+)
   scroll-margin-top compensates for the sticky header height so that
   in-page anchor navigation via TOC links lands with the heading fully visible.
   ------------------------------------------------------------------------- */
#md-content h2 {
  margin-top: var(--space-6);
  margin-bottom: var(--space-3);
  padding-top: var(--space-4);
  border-top: 1px solid var(--color-border);
  scroll-margin-top: calc(var(--header-height) + var(--space-3));
}

#md-content h3 {
  margin-top: var(--space-5);
  margin-bottom: var(--space-2);
  color: var(--color-primary);
  scroll-margin-top: calc(var(--header-height) + var(--space-3));
}

#md-content h4,
#md-content h5,
#md-content h6 {
  margin-top: var(--space-4);
  margin-bottom: var(--space-2);
  scroll-margin-top: calc(var(--header-height) + var(--space-3));
}

#md-content p {
  margin-bottom: var(--space-3);
  text-align: justify;
}

#md-content a {
  color: var(--color-primary);
  text-decoration: underline;
  text-underline-offset: 3px;
}

#md-content a:hover {
  color: var(--color-accent-dark);
}

/* Lists: re-enable list markers (base.css resets them globally) */
#md-content ul,
#md-content ol {
  margin: 0 0 var(--space-3) 0;
  padding-right: var(--space-5);
}

#md-content ul {
  list-style: disc;
}

#md-content ol {
  list-style: decimal;
}

#md-content li {
  margin-bottom: var(--space-2);
}

#md-content li > ul,
#md-content li > ol {
  margin-top: var(--space-2);
}

/* Blockquote: accent gold right-border on an accent-light background */
#md-content blockquote {
  margin: var(--space-4) 0;
  padding: var(--space-3) var(--space-4);
  background: var(--color-accent-light);
  border-right: 4px solid var(--color-accent);
  border-radius: var(--radius-sm);
  color: var(--color-text-muted);
  font-style: italic;
}

#md-content blockquote p:last-child {
  margin-bottom: 0;
}

/* Inline code: monospaced with alternate background; LTR direction for Latin code */
#md-content code {
  font-family: 'Consolas', 'Courier New', monospace;
  background: var(--color-bg-alt);
  color: var(--color-primary-dark);
  padding: 2px 6px;
  border-radius: var(--radius-sm);
  font-size: 0.9em;
  direction: ltr;
  unicode-bidi: embed;
}

/* Code blocks: dark background, LTR, horizontally scrollable */
#md-content pre {
  background: var(--color-bg-dark);
  color: #E8F1F5;
  padding: var(--space-4);
  border-radius: var(--radius-md);
  overflow-x: auto;
  margin: var(--space-4) 0;
  direction: ltr;
  text-align: left;
}

#md-content pre code {
  background: transparent;
  color: inherit;
  padding: 0;
}

/* GFM Tables: consistent with .doc-table component */
#md-content table {
  width: 100%;
  border-collapse: collapse;
  margin: var(--space-4) 0;
  background: #fff;
  border-radius: var(--radius-md);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
  font-family: var(--font-body);
}

#md-content th,
#md-content td {
  padding: 12px 16px;
  text-align: right;
  border-bottom: 1px solid var(--color-border);
  border-left: 1px solid var(--color-border);
  font-size: 14.5px;
}

#md-content th:last-child,
#md-content td:last-child {
  border-left: none;
}

#md-content th {
  background: var(--color-primary-light);
  color: var(--color-primary-dark);
  font-weight: 800;
}

#md-content tr:last-child td {
  border-bottom: none;
}

#md-content tr:nth-child(even) {
  background: var(--color-bg-alt);
}

/* Images: responsive, centered, with shadow frame */
#md-content img {
  max-width: 100%;
  height: auto;
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
  margin: var(--space-4) auto;
  display: block;
}

/* Horizontal Rule */
#md-content hr {
  border: none;
  border-top: 2px dashed var(--color-border-dark);
  margin: var(--space-6) 0;
}

/* Emphasis elements */
#md-content strong {
  color: var(--color-primary-dark);
  font-weight: 800;
}

#md-content em {
  color: var(--color-text-muted);
}

/* GFM Strikethrough */
#md-content del {
  color: var(--color-text-muted);
  opacity: 0.75;
}

/* GFM Task List checkboxes */
#md-content input[type="checkbox"] {
  margin-left: var(--space-2);
  accent-color: var(--color-primary);
}

/* =========================================================================
   Responsive Overrides
   ========================================================================= */
@media (max-width: 992px) {
  .viewer-layout {
    grid-template-columns: minmax(0, 1fr);
    gap: var(--space-4);
  }

  .viewer-toc {
    position: static;
    max-height: none;
  }

  .viewer-toc ul {
    max-height: 260px;
    overflow-y: auto;
  }

  .viewer-content {
    padding: var(--space-4);
  }
}

@media (max-width: 600px) {
  .viewer-content {
    padding: var(--space-3);
  }

  #md-content table {
    font-size: 13px;
    
  }
}


/* CSR Compatibility Hack */
#md-content table {
  display: block;
  overflow-x: auto;
}
