/**
 * src/styles/markdown.css
 *
 * Shared Markdown + KaTeX visual styles.
 * Import on every page that uses markdown-handler.js:
 *   quiz.html   → after quiz.css
 *   result.html → after result.css
 *
 * Tokens used (all defined in themes.css):
 *   --color-primary          --color-border        --color-border-light
 *   --color-text-primary     --color-text-secondary
 *   --color-background       --color-background-secondary
 *   --color-primary-light    --shadow-sm  --shadow-md
 *   --color-success          --color-error
 *
 * Glassmorphism recipe (shared across tables and copy buttons):
 *   background: rgba(…, 0.06–0.12)
 *   backdrop-filter: blur(12px)
 *   border: 1px solid rgba(…, 0.14)
 *   box-shadow: inset 0 1px 0 rgba(255,255,255,0.08), var(--shadow-sm)
 */

/* ══════════════════════════════════════════════════════════════════════════════
   DIRECTION + OVERFLOW UTILITIES
   ══════════════════════════════════════════════════════════════════════════════ */

.ltr {
  direction: ltr;
}

/* Every element that receives renderMarkdown() output should opt in to
   overflow-wrap so long tokens, URLs, and KaTeX never escape their card. */
.md-content,
.question-text,
.option-label,
.feedback,
.explanation-body,
.formal-answer,
.formal-answer-text {
  overflow-wrap: break-word;
  word-break: break-word;
  min-width: 0;
}

/* ══════════════════════════════════════════════════════════════════════════════
   HEADINGS
   ══════════════════════════════════════════════════════════════════════════════ */

.md-h1,
.md-h2,
.md-h3,
.md-h4,
.md-h5,
.md-h6 {
  margin: 0.6em 0 0.3em;
  color: var(--color-text-primary);
  line-height: 1.3;
}
.md-h1 {
  font-size: 1.6em;
  border-bottom: 2px solid var(--color-border);
  padding-bottom: 4px;
}
.md-h2 {
  font-size: 1.35em;
  border-bottom: 1px solid var(--color-border);
  padding-bottom: 3px;
}
.md-h3 {
  font-size: 1.15em;
}
.md-h4 {
  font-size: 1.05em;
}
.md-h5 {
  font-size: 0.95em;
}
.md-h6 {
  font-size: 0.9em;
  opacity: 0.85;
}

/* ══════════════════════════════════════════════════════════════════════════════
   HORIZONTAL RULE
   ══════════════════════════════════════════════════════════════════════════════ */

.md-hr {
  border: none;
  border-top: 2px solid var(--color-border);
  margin: 0.8em 0;
}

/* ══════════════════════════════════════════════════════════════════════════════
   BLOCKQUOTE
   ══════════════════════════════════════════════════════════════════════════════ */

.md-blockquote {
  border-right: 4px solid var(--color-primary);
  margin: 0.5em 0;
  padding: 8px 16px;
  background: var(--color-primary-light);
  border-radius: 0 6px 6px 0;
  color: var(--color-text-secondary);
  font-style: italic;
}

/* ══════════════════════════════════════════════════════════════════════════════
   PARAGRAPHS  (Fix 4 — replaces the old bare-line + <br> approach)
   ══════════════════════════════════════════════════════════════════════════════ */

/* Every block of consecutive text lines is now wrapped in a <p class="md-p">.
   Vertical rhythm comes from margin rather than injected <br> tags. */
.md-p {
  margin: 0.35em 0;
  line-height: 1.6;
  color: var(--color-text-primary);
}
.md-p:first-child {
  margin-top: 0;
}
.md-p:last-child {
  margin-bottom: 0;
}

/* ══════════════════════════════════════════════════════════════════════════════
   LISTS
   ══════════════════════════════════════════════════════════════════════════════ */

.md-list {
  margin: 0.4em 0 0.4em 1.4em;
  padding: 0;
  color: var(--color-text-primary);
}
.md-list li {
  margin-bottom: 4px;
  line-height: 1.6;
}

/* ── Nested list indentation & bullet-style progression (Fix 2) ─────────────
   Each level of nesting gets its own list-style-type so readers can
   distinguish depth at a glance: disc → circle → square for <ul>.
   Nested <ol> inherits decimal numbering by default.                        */

/* Extra breathing room around any nested list */
.md-list .md-list {
  margin-top: 4px;
  margin-bottom: 2px;
}

/* Level 2 unordered: open circle */
ul.md-list > li > ul.md-list {
  list-style-type: circle;
}

/* Level 3+ unordered: filled square */
ul.md-list > li > ul.md-list > li > ul.md-list {
  list-style-type: square;
}

/* ══════════════════════════════════════════════════════════════════════════════
   INLINE LINK & IMAGE
   ══════════════════════════════════════════════════════════════════════════════ */

.md-link {
  color: var(--color-primary);
  text-decoration: underline;
  text-underline-offset: 2px;
}
.md-img {
  max-width: 100%;
  height: auto;
  border-radius: 6px;
  margin: 4px 0;
  display: block;
}

/* ══════════════════════════════════════════════════════════════════════════════
   KATEX / MATH
   ══════════════════════════════════════════════════════════════════════════════ */

.math-block,
.katex-display {
  display: block;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  padding: 10px 0;
  text-align: center;
  font-size: 1.05em;
  max-width: 100%;
}
.math-inline {
  display: inline;
}
.math-raw {
  font-family: "Courier New", Courier, monospace;
  font-size: 0.92em;
  background: var(--color-background);
  border: 1px solid var(--color-border);
  border-radius: 4px;
  padding: 1px 5px;
  color: var(--color-text-primary);
}

/* ══════════════════════════════════════════════════════════════════════════════
   INLINE CODE
   ══════════════════════════════════════════════════════════════════════════════ */

.inline-code {
  font-family: "SF Mono", "Fira Code", "Cascadia Code", Consolas, monospace;
  font-size: 0.88em;
  background: var(--color-background-secondary, rgba(99, 102, 241, 0.1));
  border: 1px solid var(--color-border);
  border-radius: 5px;
  padding: 1px 6px;
  color: var(--color-primary, #6366f1);
  /* Allow wrapping — nowrap was the primary overflow culprit on narrow viewports */
  white-space: normal;
  word-break: break-all;
}

[data-theme="dark"] .inline-code,
[data-theme="dark-slate"] .inline-code {
  background: rgba(203, 166, 247, 0.15);
  color: #cba6f7;
}

/* ══════════════════════════════════════════════════════════════════════════════
   FENCED CODE BLOCKS  +  COPY BUTTON  (Glassmorphism)
   ══════════════════════════════════════════════════════════════════════════════ */

.code-block-wrapper {
  position: relative;
  margin: 14px 0;
  direction: ltr;
  border-radius: 12px;
  /* Glassmorphism card effect around the whole block */
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.08),
    var(--shadow-sm, 0 1px 3px rgba(0, 0, 0, 0.1));
  overflow: hidden; /* keep child pre's border-radius */
}

/* Language badge in the top-left corner */
.code-lang-label {
  position: absolute;
  top: 10px;
  left: 14px;
  font-family: "SF Mono", "Fira Code", Consolas, monospace;
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--color-text-secondary, #94a3b8);
  opacity: 0.7;
  pointer-events: none;
  user-select: none;
}

/* Copy button — glass pill floating in the top-right corner */
.copy-code-btn {
  position: absolute;
  top: 8px;
  right: 10px;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 5px 10px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.72rem;
  font-weight: 600;
  line-height: 1;
  letter-spacing: 0.02em;

  /* Glassmorphism */
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.18);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.14),
    0 2px 8px rgba(0, 0, 0, 0.14);

  color: var(--color-text-secondary, #94a3b8);
  transition:
    background 0.18s ease,
    color 0.18s ease,
    box-shadow 0.18s ease,
    transform 0.12s ease;

  /* Hidden by default; reveal on wrapper hover */
  opacity: 0;
  z-index: 2;
}

.copy-code-btn .copy-label {
  font-family: "Inter", "Tajawal", sans-serif;
}

.code-block-wrapper:hover .copy-code-btn {
  opacity: 1;
}

.copy-code-btn:hover {
  background: rgba(255, 255, 255, 0.18);
  color: var(--color-text-primary, #1e1e2e);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.22),
    0 4px 12px rgba(0, 0, 0, 0.18);
  transform: translateY(-1px);
}

.copy-code-btn:active {
  transform: translateY(0);
}

/* "Copied!" confirmation state */
.copy-code-btn.copied {
  opacity: 1;
  background: rgba(16, 185, 129, 0.18); /* success green tint */
  border-color: rgba(16, 185, 129, 0.4);
  color: var(--color-success, #10b981);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.12),
    0 0 0 3px rgba(16, 185, 129, 0.12);
}

/* The <pre> inside the wrapper */
.code-block {
  background: var(--color-background-secondary, #1a1a2e);
  border: none; /* wrapper owns the border */
  border-radius: 12px; /* match wrapper */
  padding: 36px 16px 14px; /* top pad makes room for button + lang label */
  margin: 0;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  max-width: 100%;
  font-family: "SF Mono", "Fira Code", "Cascadia Code", Consolas, monospace;
  font-size: 0.9rem;
  line-height: 1.6;
  white-space: pre;
  text-align: left;
}

.code-block code {
  background: none;
  padding: 0;
  border-radius: 0;
  font-size: inherit;
  color: var(--color-code, #e2e8f0);
}

/* ── Light-theme code block overrides ──────────────────────────────────────── */
[data-theme="light"] .code-block-wrapper {
  background: rgba(0, 0, 0, 0.03);
  border-color: rgba(0, 0, 0, 0.08);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.6),
    var(--shadow-sm, 0 1px 3px rgba(0, 0, 0, 0.08));
}

[data-theme="light"] .copy-code-btn {
  background: rgba(0, 0, 0, 0.06);
  border-color: rgba(0, 0, 0, 0.12);
  color: var(--color-text-secondary, #64748b);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.8),
    0 2px 6px rgba(0, 0, 0, 0.06);
}

[data-theme="light"] .copy-code-btn:hover {
  background: rgba(0, 0, 0, 0.1);
  color: var(--color-text-primary, #0f172a);
}

[data-theme="light"] .code-block {
  background: #f8f9fb;
}

[data-theme="light"] .code-block code {
  color: #334155;
}

[data-theme="dark"] .code-block code,
[data-theme="dark-slate"] .code-block code {
  color: var(--color-code, #e2e8f0);
}

/* ══════════════════════════════════════════════════════════════════════════════
   GFM TABLES  (Glassmorphism)
   ══════════════════════════════════════════════════════════════════════════════ */

/* Outer scroll container — owns the glass aesthetic */
.md-table-wrapper {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  margin: 16px 0;
  border-radius: 14px;

  /* Glassmorphism */
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.12);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.1),
    var(--shadow-md, 0 4px 6px rgba(0, 0, 0, 0.07));

  /*
   * Scroll-shadow technique: CSS-only affordance that appears at the edges
   * only when there is content to scroll.
   * background-attachment: local  — the gradient moves with scroll,
   *                                 disappearing when the edge is reached.
   * background-attachment: scroll — fixed to the viewport, always visible.
   */
  background-image:
    linear-gradient(to left, rgba(255, 255, 255, 0.06) 20%, transparent),
    linear-gradient(to right, rgba(255, 255, 255, 0.06) 20%, transparent),
    radial-gradient(
      farthest-side at 100% 50%,
      rgba(0, 0, 0, 0.12),
      transparent
    ),
    radial-gradient(farthest-side at 0% 50%, rgba(0, 0, 0, 0.12), transparent);
  background-size:
    40px 100%,
    40px 100%,
    12px 100%,
    12px 100%;
  background-position: right, left, right, left;
  background-repeat: no-repeat;
  background-attachment: local, local, scroll, scroll;
  background-color: rgba(255, 255, 255, 0.06);
}

.md-table {
  min-width: max-content; /* lets the table grow naturally; wrapper scrolls */
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
  line-height: 1.55;
  direction: ltr;
  text-align: left;
}

/* Header row — frosted accent layer */
.md-table thead tr {
  background: rgba(99, 102, 241, 0.1); /* primary tint */
  border-bottom: 2px solid rgba(99, 102, 241, 0.22);
}

.md-table th {
  padding: 11px 16px;
  font-weight: 700;
  color: var(--color-text-primary);
  white-space: nowrap; /* keep headers on one line */
  letter-spacing: 0.01em;
  /* Subtle inset highlight on each header cell top edge */
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.1);
}

/* Body cells */
.md-table td {
  padding: 10px 16px;
  vertical-align: top;
  white-space: normal;
  min-width: 90px;
  overflow-wrap: break-word;
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}

/* Alternating rows — very subtle frosted stripe */
.md-table tbody tr:nth-child(even) {
  background: var(--color-background-secondary, rgba(0, 0, 0, 0.025));
}

/* Row hover — lift the glass slightly */
.md-table tbody tr {
  transition:
    background 0.14s ease,
    box-shadow 0.14s ease;
}
.md-table tbody tr:hover {
  background: rgba(99, 102, 241, 0.07);
  box-shadow: inset 0 0 0 1px rgba(99, 102, 241, 0.12);
}

/* Last row — no bottom border so it doesn't double-up with wrapper border */
.md-table tbody tr:last-child td {
  border-bottom: none;
}

/* ── Light-theme table overrides ───────────────────────────────────────────── */
[data-theme="light"] .md-table-wrapper {
  background: rgba(0, 0, 0, 0.02);
  border-color: rgba(0, 0, 0, 0.08);
  background-image:
    linear-gradient(to left, rgba(0, 0, 0, 0.02) 20%, transparent),
    linear-gradient(to right, rgba(0, 0, 0, 0.02) 20%, transparent),
    radial-gradient(
      farthest-side at 100% 50%,
      rgba(0, 0, 0, 0.08),
      transparent
    ),
    radial-gradient(farthest-side at 0% 50%, rgba(0, 0, 0, 0.08), transparent);
  background-size:
    40px 100%,
    40px 100%,
    12px 100%,
    12px 100%;
  background-position: right, left, right, left;
  background-repeat: no-repeat;
  background-attachment: local, local, scroll, scroll;
  background-color: rgba(0, 0, 0, 0.02);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.8),
    var(--shadow-sm, 0 1px 3px rgba(0, 0, 0, 0.06));
}

[data-theme="light"] .md-table thead tr {
  background: rgba(99, 102, 241, 0.07);
  border-bottom-color: rgba(99, 102, 241, 0.18);
}

[data-theme="light"] .md-table td {
  border-bottom-color: rgba(0, 0, 0, 0.05);
}

[data-theme="light"] .md-table tbody tr:nth-child(even) {
  background: rgba(0, 0, 0, 0.025);
}

[data-theme="light"] .md-table tbody tr:hover {
  background: rgba(99, 102, 241, 0.05);
  box-shadow: inset 0 0 0 1px rgba(99, 102, 241, 0.1);
}

/* ── Dark-slate table overrides ────────────────────────────────────────────── */
[data-theme="dark-slate"] .md-table thead tr {
  background: rgba(99, 102, 241, 0.14);
  border-bottom-color: rgba(99, 102, 241, 0.28);
}

[data-theme="dark"] .md-table th,
[data-theme="dark-slate"] .md-table th {
  background: rgba(99, 102, 241, 0.14);
}

[data-theme="dark"] .md-table tbody tr:nth-child(even),
[data-theme="dark-slate"] .md-table tbody tr:nth-child(even) {
  background: rgba(255, 255, 255, 0.04);
}

[data-theme="dark"] .md-table tbody tr:hover,
[data-theme="dark-slate"] .md-table tbody tr:hover {
  background: rgba(99, 102, 241, 0.09);
}

/* ── Dark-theme table overrides ────────────────────────────────────────────── */
[data-theme="dark"] .md-table-wrapper,
[data-theme="dark-slate"] .md-table-wrapper {
  background:
    linear-gradient(to left, var(--color-background) 20%, transparent) right,
    linear-gradient(to right, var(--color-background) 20%, transparent) left,
    radial-gradient(
        farthest-side at 100% 50%,
        rgba(255, 255, 255, 0.07),
        transparent
      )
      right,
    radial-gradient(
        farthest-side at 0% 50%,
        rgba(255, 255, 255, 0.07),
        transparent
      )
      left;
  background-color: var(--color-background);
  background-repeat: no-repeat;
  background-size:
    40px 100%,
    40px 100%,
    10px 100%,
    10px 100%;
  background-attachment: local, local, scroll, scroll;
}

/* ══════════════════════════════════════════════════════════════════════════════
   MOBILE OVERRIDES
   ══════════════════════════════════════════════════════════════════════════════ */

@media (max-width: 480px) {
  .copy-code-btn {
    /* Always visible on touch devices — no hover event */
    opacity: 1;
  }

  .code-block {
    font-size: 0.8rem;
    padding: 34px 12px 12px;
  }

  .md-table th,
  .md-table td {
    padding: 8px 12px;
    font-size: 0.85rem;
  }
}
