/* ============================================================================
   SHARED CONTENT BLOCKS — Design System
   ============================================================================
   Single source of truth for .module-body typography and .chapter-block cards.
   Used by: textbook reader, sim case narratives, and any rich-text display.

   Load alongside main.css. Requires directive-parser.js + marked.js at runtime.
   ============================================================================ */

/* --- Module Body / Content Area --- */
.module-body {
  line-height: 1.75;
  font-size: 1.05rem;
  color: var(--app-text);
}

.module-body h1 {
  font-size: 1.8rem;
  color: #1e40af;
  margin: 2.5rem 0 1rem;
  padding-top: 1rem;
  border-top: 1px solid var(--app-border);
}

.module-body h2 {
  font-size: 1.4rem;
  color: #1e40af;
  margin: 2rem 0 0.75rem;
}

.module-body h3 {
  font-size: 1.15rem;
  color: var(--app-text);
  margin: 1.5rem 0 0.5rem;
}

.module-body p {
  margin: 0.75rem 0;
}

.module-body ul, .module-body ol {
  padding-left: 1.5rem;
  margin: 0.75rem 0;
}

.module-body li {
  margin-bottom: 0.4rem;
}

.module-body img {
  max-width: 100%;
  border-radius: 8px;
  margin: 1rem 0;
}

/* MathJax SVG containment */
.module-body mjx-container {
  overflow-x: auto;
  overflow-y: hidden;
  max-width: 100%;
}

.module-body mjx-container[display="true"] {
  display: block !important;
  text-align: center;
  margin: 1.5rem 0;
  overflow-x: auto;
}

.module-body mjx-container svg {
  max-width: 100%;
}

.module-body .MathJax {
  overflow-x: auto;
  overflow-y: hidden;
}

.module-body code {
  background: #f1f5f9;
  padding: 0.15em 0.4em;
  border-radius: 4px;
  font-size: 0.9em;
  color: #d64747;
}

.module-body blockquote {
  margin: 1rem 0;
  padding: 0.75rem 1.25rem;
  border-left: 4px solid var(--app-accent);
  background: #f8fafc;
  border-radius: 0 8px 8px 0;
  color: var(--app-muted);
}

/* --- Table House Style --- */
.module-body .table-wrapper {
  overflow-x: auto;
  margin: 1.5rem 0;
  border-radius: 12px;
  border: 1px solid var(--app-border);
  box-shadow: 0 2px 8px rgba(15, 23, 42, 0.04);
}

.module-body table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.92rem;
  line-height: 1.55;
}

.module-body thead {
  background: linear-gradient(135deg, #1e3a5f 0%, #1e40af 100%);
}

.module-body thead th {
  padding: 0.7rem 1rem;
  text-align: left;
  font-weight: 600;
  font-size: 0.82rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: #ffffff;
  border-bottom: 2px solid #1e40af;
  white-space: nowrap;
}

.module-body thead th:first-child { border-radius: 11px 0 0 0; }
.module-body thead th:last-child  { border-radius: 0 11px 0 0; }

.module-body tbody td {
  padding: 0.65rem 1rem;
  border-bottom: 1px solid #e2e8f0;
  color: var(--app-text);
  vertical-align: top;
}

.module-body tbody tr:nth-child(even) { background: #f8fafc; }
.module-body tbody tr:nth-child(odd)  { background: #ffffff; }

.module-body tbody tr:hover {
  background: #e0f2fe;
  transition: background 0.15s ease;
}

.module-body tbody tr:last-child td             { border-bottom: none; }
.module-body tbody tr:last-child td:first-child { border-radius: 0 0 0 11px; }
.module-body tbody tr:last-child td:last-child  { border-radius: 0 0 11px 0; }

.module-body tbody td strong {
  color: #1e40af;
  font-weight: 600;
}

.module-body .table-wrapper.compact table          { font-size: 0.85rem; }
.module-body .table-wrapper.compact th,
.module-body .table-wrapper.compact td             { padding: 0.45rem 0.75rem; }

/* --- Section wrapper --- */
.module-section {
  scroll-margin-top: 2rem;
  padding-bottom: 1rem;
}

/* --- Loading spinner --- */
.module-loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 4rem;
  color: var(--app-muted);
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 3px solid var(--app-border);
  border-top-color: var(--app-accent);
  border-radius: 50%;
  animation: cb-spin 0.8s linear infinite;
  margin-bottom: 1rem;
}

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


/* ============================================================================
   CONTENT BLOCKS — Repeatable Design Components
   ============================================================================ */

/* --- Block Header (shared layout for blocks with titles) --- */
.block-header {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
  margin-bottom: 0.75rem;
}

/* --- Collapse / Expand button (used by code-snippet) --- */
.block-collapse-btn {
  background: none;
  border: none;
  font-size: 1rem;
  line-height: 1;
  padding: 0.15rem 0.35rem;
  cursor: pointer;
  color: #64748b;
  border-radius: 4px;
  transition: color 0.15s ease, background 0.15s ease;
  flex-shrink: 0;
}

.block-collapse-btn:hover {
  color: #e2e8f0;
  background: rgba(255, 255, 255, 0.08);
}

.block-body {
  transition: opacity 0.2s ease;
}

.block-collapsed .block-body {
  opacity: 0;
}

.chapter-block.code-snippet.block-collapsed .code-snippet-header {
  border-bottom: none;
}

.chapter-block.comparison .block-title::before {
  content: '⚖️';
  font-size: 1.1rem;
}

/* --- Concept Block --- */
.chapter-block.concept {
  background: var(--app-card-bg);
  border: 1px solid var(--app-border);
  border-left: 5px solid var(--app-accent);
  border-radius: 12px;
  padding: 1.5rem;
  margin: 1.5rem 0;
  box-shadow: 0 4px 12px rgba(15, 23, 42, 0.05);
}

.chapter-block.concept .block-title {
  font-size: 1.1rem;
  font-weight: 700;
  color: #1e40af;
  margin: 0 0 0.75rem 0;
}

.chapter-block.concept .block-title::before {
  content: '💡';
  font-size: 1.2rem;
}

.chapter-block.concept .block-content {
  line-height: 1.7;
}

/* --- Guided Task Block --- */
.chapter-block.guided-task {
  background: #f0fdf4;
  border: 1px solid #bbf7d0;
  border-left: 5px solid var(--app-success);
  border-radius: 12px;
  padding: 1.5rem;
  margin: 1.5rem 0;
  box-shadow: 0 4px 12px rgba(15, 23, 42, 0.05);
  position: relative;
}

.reader-focus-overlay {
  position: absolute;
  top: 0; left: 0;
  width: 100%; height: 100%;
  background: rgba(255, 255, 255, 0.65);
  z-index: 50;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.35s ease;
  border-radius: 0;
}

.reader-focus-overlay.active { opacity: 1; }

.reader-task-focused {
  position: relative !important;
  z-index: 51 !important;
  box-shadow:
    0 0 0 4px var(--app-success, #22c55e),
    0 0 30px rgba(34, 197, 94, 0.35),
    0 8px 24px rgba(0, 0, 0, 0.1) !important;
  transition: box-shadow 0.3s ease, transform 0.3s ease !important;
  animation: guidedTaskPulse 2.5s ease-in-out infinite;
}

@keyframes guidedTaskPulse {
  0%, 100% {
    box-shadow:
      0 0 0 4px var(--app-success, #22c55e),
      0 0 30px rgba(34, 197, 94, 0.35),
      0 8px 24px rgba(0, 0, 0, 0.1);
  }
  50% {
    box-shadow:
      0 0 0 6px var(--app-success, #22c55e),
      0 0 40px rgba(34, 197, 94, 0.5),
      0 8px 24px rgba(0, 0, 0, 0.1);
  }
}

.chapter-block.guided-task .block-title {
  font-size: 1.1rem;
  font-weight: 700;
  color: #166534;
  margin: 0 0 0.75rem 0;
}

.chapter-block.guided-task .block-title::before {
  content: '🔬';
  font-size: 1.2rem;
}

.guided-task-steps { padding-left: 1.25rem; margin: 0.75rem 0; }
.guided-task-steps li { margin-bottom: 0.5rem; line-height: 1.6; }

.open-tool-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  margin-top: 1rem;
  padding: 0.6rem 1.2rem;
  background: var(--app-success);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s ease, transform 0.1s ease;
}

.open-tool-btn:hover { background: #22863a; transform: translateY(-1px); }
.open-tool-btn::after { content: '→'; font-size: 1.1rem; }

/* --- Video Block --- */
.chapter-block.video { margin: 1.5rem 0; }

.video-wrapper {
  position: relative;
  width: 100%;
  padding-bottom: 56.25%;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(15, 23, 42, 0.1);
}

.video-wrapper iframe {
  position: absolute;
  top: 0; left: 0;
  width: 100%; height: 100%;
  border: none;
}

.video-caption {
  text-align: center;
  font-size: 0.85rem;
  color: var(--app-muted);
  margin-top: 0.5rem;
  font-style: italic;
}

/* --- Assessment Block --- */
.chapter-block.assessment {
  background: #fffbeb;
  border: 1px solid #fde68a;
  border-left: 5px solid var(--app-warning);
  border-radius: 12px;
  padding: 1.5rem;
  margin: 1.5rem 0;
  box-shadow: 0 4px 12px rgba(15, 23, 42, 0.05);
}

.chapter-block.assessment .block-title {
  font-size: 1rem;
  font-weight: 700;
  color: #92400e;
  margin: 0 0 0.75rem 0;
}

.chapter-block.assessment .block-title::before {
  content: '✏️';
  font-size: 1.1rem;
}

.assessment-question { font-weight: 600; margin-bottom: 0.75rem; }
.assessment-options   { list-style: none; padding: 0; }

.assessment-options li {
  padding: 0.5rem 0.75rem;
  margin-bottom: 0.4rem;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.15s ease;
  border: 1px solid transparent;
}

.assessment-options li:hover    { background: #fef3c7; border-color: #fde68a; }
.assessment-options li.selected { background: #fde68a; border-color: var(--app-warning); }
.assessment-options li.correct  { background: #d1fae5; border-color: var(--app-success); }
.assessment-options li.incorrect { background: #fee2e2; border-color: var(--app-danger); }

.assessment-feedback {
  margin-top: 0.75rem;
  padding: 0.75rem;
  border-radius: 8px;
  font-size: 0.9rem;
  display: none;
}

.assessment-feedback.correct   { display: block; background: #d1fae5; color: #166534; }
.assessment-feedback.incorrect { display: block; background: #fee2e2; color: #991b1b; }

.assessment-submit-btn {
  margin-top: 0.75rem;
  padding: 0.5rem 1rem;
  background: var(--app-warning);
  color: #1f2a37;
  border: none;
  border-radius: 8px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s ease;
}

.assessment-submit-btn:hover { background: #f59e0b; }

/* --- Deep-Dive Block --- */
.chapter-block.deep-dive {
  margin: 1.5rem 0;
  border: 1px solid var(--app-border);
  border-radius: 12px;
  overflow: hidden;
}

.deep-dive-summary {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 1rem 1.25rem;
  background: #f8fafc;
  cursor: pointer;
  font-weight: 600;
  color: var(--app-accent);
  user-select: none;
  transition: background 0.15s ease;
}

.deep-dive-summary:hover { background: #e0f2fe; }

.deep-dive-summary::before {
  content: '▶';
  font-size: 0.75rem;
  transition: transform 0.2s ease;
}

.chapter-block.deep-dive[open] .deep-dive-summary::before {
  transform: rotate(90deg);
}

.deep-dive-content {
  padding: 1.25rem;
  background: var(--app-card-bg);
  border-top: 1px solid var(--app-border);
  line-height: 1.7;
}

/* --- Callout Block --- */
.chapter-block.callout {
  padding: 1.25rem 1.5rem;
  border-radius: 12px;
  margin: 1.5rem 0;
}

.callout-header {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.15rem;
  margin-bottom: 0.5rem;
}

.callout-header .block-icon { font-size: 1.4rem; line-height: 1; }
.callout-label { font-weight: 700; font-size: 0.9rem; }
.callout-body  { line-height: 1.6; }

.chapter-block.callout.key-takeaway {
  background: #f0fdfa;
  border: 1px solid #99f6e4;
  border-left: 5px solid #14b8a6;
}

.chapter-block.callout.warning {
  background: #fff7ed;
  border: 1px solid #fed7aa;
  border-left: 5px solid #ea580c;
}

.chapter-block.callout.definition {
  background: #e0f2fe;
  border: 1px solid #7dd3fc;
  border-left: 5px solid var(--app-accent);
}

.chapter-block.callout.formula {
  background: #f5f3ff;
  border: 1px solid #c4b5fd;
  border-left: 5px solid #7c3aed;
}

/* --- Mini-Interactive Block --- */
.chapter-block.mini-interactive {
  background: var(--app-card-bg);
  border: 1px solid var(--app-border);
  border-radius: 12px;
  padding: 1.5rem;
  margin: 1.5rem 0;
  box-shadow: 0 4px 12px rgba(15, 23, 42, 0.05);
}

.chapter-block.mini-interactive .block-title {
  font-size: 1rem;
  font-weight: 700;
  color: #7c3aed;
  margin: 0 0 0.75rem 0;
}

.chapter-block.mini-interactive .block-title::before { content: '🎮'; font-size: 1.1rem; }

.mini-tool-container    { min-height: 300px; position: relative; }
.mini-tool-instructions { font-size: 0.9rem; color: var(--app-muted); margin-top: 0.5rem; font-style: italic; }

/* --- Code Snippet Block --- */
.chapter-block.code-snippet {
  background: #1e293b;
  border: 1px solid #334155;
  border-radius: 12px;
  margin: 1.5rem 0;
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(15, 23, 42, 0.12);
}

.code-snippet-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.5rem 1rem;
  background: #0f172a;
  border-bottom: 1px solid #334155;
}

.code-snippet-label {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.8rem;
  color: #94a3b8;
  flex: 1;
}

.code-snippet-icon  { font-size: 1rem; }
.code-snippet-title { font-weight: 600; color: #e2e8f0; }

.code-snippet-lang {
  background: #334155;
  padding: 0.15rem 0.5rem;
  border-radius: 4px;
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: #94a3b8;
}

.code-copy-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  background: transparent;
  border: 1px solid #475569;
  color: #94a3b8;
  padding: 0.25rem 0.6rem;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.78rem;
  font-weight: 500;
  transition: all 0.15s ease;
}

.code-copy-btn:hover  { background: #334155; color: #e2e8f0; border-color: #64748b; }
.code-copy-btn.copied { border-color: var(--app-success); color: var(--app-success); }

.code-snippet-body {
  margin: 0;
  padding: 1.25rem 1.5rem;
  overflow-x: auto;
  font-size: 0.88rem;
  line-height: 1.65;
}

.code-snippet-body code {
  background: transparent !important;
  color: #e2e8f0;
  padding: 0 !important;
  font-family: 'Fira Code', 'Cascadia Code', 'JetBrains Mono', 'SF Mono', 'Consolas', monospace;
  font-size: inherit;
  border-radius: 0;
}

/* --- Example Block --- */
.chapter-block.example {
  background: #fefce8;
  border: 1px solid #fde68a;
  border-left: 5px solid #f59e0b;
  border-radius: 12px;
  padding: 1.5rem;
  margin: 1.5rem 0;
  box-shadow: 0 4px 12px rgba(15, 23, 42, 0.05);
}

.chapter-block.example .block-header { margin-bottom: 0.75rem; }

.chapter-block.example .block-title {
  font-size: 1.1rem;
  font-weight: 700;
  color: #92400e;
  margin: 0;
}

.example-context {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  background: #f59e0b;
  color: white;
  padding: 0.15rem 0.5rem;
  border-radius: 4px;
  margin-left: 0.25rem;
}

.chapter-block.example .example-content { line-height: 1.7; }

/* --- Comparison Block --- */
.chapter-block.comparison {
  background: var(--app-card-bg);
  border: 1px solid var(--app-border);
  border-radius: 12px;
  padding: 1.5rem;
  margin: 1.5rem 0;
  box-shadow: 0 4px 12px rgba(15, 23, 42, 0.05);
}

.chapter-block.comparison .block-header { margin-bottom: 1rem; }

.chapter-block.comparison .block-title {
  font-size: 1.1rem;
  font-weight: 700;
  color: #1e40af;
  margin: 0;
}

.comparison-grid { display: flex; gap: 0; align-items: stretch; }

.comparison-panel { flex: 1; padding: 1rem; line-height: 1.65; }

.comparison-panel h3 {
  font-size: 1rem;
  font-weight: 700;
  color: var(--app-accent);
  margin: 0 0 0.5rem 0;
  padding-bottom: 0.4rem;
  border-bottom: 2px solid var(--app-accent);
}

.comparison-divider { width: 1px; background: var(--app-border); flex-shrink: 0; margin: 0 0.25rem; }

/* --- Figure Block --- */
.chapter-block.figure { margin: 1.5rem 0; text-align: center; }

.figure-container { display: inline-block; margin: 0 auto; text-align: center; }

.figure-img {
  width: 100%;
  border-radius: 10px;
  box-shadow: 0 4px 16px rgba(15, 23, 42, 0.1);
}

.figure-annotation {
  text-align: left;
  font-size: 0.9rem;
  line-height: 1.6;
  margin-top: 0.75rem;
  color: var(--app-text);
}

.figure-caption {
  font-size: 0.85rem;
  color: var(--app-muted);
  font-style: italic;
  margin-top: 0.5rem;
  padding-top: 0.4rem;
  border-top: 1px solid var(--app-border);
}

/* --- Video Placeholder (when no URL provided) --- */
.video-placeholder {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 3rem;
  background: #f1f5f9;
  border-radius: 12px;
  color: var(--app-muted);
}

.video-placeholder span { font-size: 2.5rem; margin-bottom: 0.5rem; }
.video-placeholder p    { font-size: 0.9rem; }

/* --- Responsive --- */
@media (max-width: 768px) {
  .chapter-block {
    padding: 1rem !important;
  }
}
