/* ==========================================================================
   COMPONENTS — Button, Card, Source Box, Wiki Badge, TOC, Table, Modal
   ========================================================================== */

/* ---------- BUTTON ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-sm);
  padding: 10px 20px;
  font-size: var(--fs-base);
  font-weight: var(--fw-medium);
  border-radius: var(--radius-sm);
  transition: background var(--transition), color var(--transition), border-color var(--transition);
  min-height: 44px;
  cursor: pointer;
  white-space: nowrap;
}

.btn:hover {
  text-decoration: none;
}

.btn svg {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
}

.btn-primary {
  background: var(--primary);
  color: #FFFFFF;
}

.btn-primary:hover {
  background: var(--primary-hover);
  color: #FFFFFF;
}

.btn-ghost {
  border: 1px solid var(--border);
  color: var(--text);
  background: transparent;
}

.btn-ghost:hover {
  background: var(--surface);
  color: var(--text);
}

.btn-block {
  width: 100%;
}

.btn-sm {
  padding: 6px 12px;
  font-size: var(--fs-sm);
  min-height: 36px;
}

/* ---------- CARD (generic) ---------- */
.card {
  padding: var(--space-lg);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--bg);
  transition: border-color var(--transition);
}

.card:hover {
  border-color: var(--primary);
}

.card h3 {
  margin-bottom: var(--space-xs);
}

.card p {
  font-size: var(--fs-sm);
  color: var(--text-muted);
  margin-bottom: 0;
}

/* ---------- STAGE CARD ---------- */
.stage-card {
  display: block;
  padding: var(--space-lg);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  transition: border-color var(--transition);
  color: var(--text);
}

.stage-card:hover {
  border-color: var(--primary);
  text-decoration: none;
  color: var(--text);
}

.stage-card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--space-sm);
}

.stage-number {
  font-family: var(--font-mono);
  font-size: var(--fs-sm);
  color: var(--text-muted);
}

.stage-count {
  font-size: var(--fs-sm);
  color: var(--text-muted);
}

.stage-card h3 {
  font-size: var(--fs-lg);
  font-weight: var(--fw-bold);
  margin-bottom: var(--space-xs);
}

.stage-card p {
  font-size: var(--fs-sm);
  color: var(--text-muted);
  margin-bottom: 0;
}

/* ---------- LESSON ITEM ---------- */
.lesson-item {
  display: flex;
  align-items: center;
  gap: var(--space-md);
  padding: var(--space-md);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  transition: border-color var(--transition), background var(--transition);
  color: var(--text);
}

.lesson-item:hover {
  border-color: var(--primary);
  background: var(--surface);
  text-decoration: none;
  color: var(--text);
}

.lesson-number {
  flex-shrink: 0;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--font-mono);
  font-size: var(--fs-sm);
  font-weight: var(--fw-medium);
  color: var(--text-muted);
  background: var(--surface);
  border-radius: var(--radius-sm);
}

.lesson-info {
  flex: 1;
  min-width: 0;
}

.lesson-info h4 {
  font-size: var(--fs-base);
  font-weight: var(--fw-medium);
  margin-bottom: 2px;
}

.lesson-info p {
  font-size: var(--fs-sm);
  color: var(--text-muted);
  margin-bottom: 0;
}

.lesson-item > svg {
  flex-shrink: 0;
  width: 16px;
  height: 16px;
  color: var(--text-muted);
}

/* ---------- SOURCE BOX (BẮT BUỘC) ---------- */
.source-box {
  padding: var(--space-lg);
  margin-bottom: var(--space-lg);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
}

.source-box-title {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  font-size: var(--fs-base);
  font-weight: var(--fw-bold);
  color: var(--text);
  margin-bottom: var(--space-md);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.source-box-title svg {
  width: 20px;
  height: 20px;
  color: var(--primary);
  flex-shrink: 0;
}

.source-item {
  padding: var(--space-md);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  margin-bottom: var(--space-sm);
}

.source-item:last-child {
  margin-bottom: 0;
}

.source-row {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-xs);
  font-size: var(--fs-sm);
  line-height: 1.6;
  margin-bottom: var(--space-xs);
}

.source-row:last-child {
  margin-bottom: 0;
}

.source-row-label {
  font-weight: var(--fw-medium);
  color: var(--text-muted);
  flex-shrink: 0;
}

.source-row-value {
  color: var(--text);
  flex: 1;
  min-width: 0;
}

.source-row-value a {
  color: var(--primary);
  word-break: break-word;
}

.source-row-value a:hover {
  text-decoration: underline;
}

.source-verified {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: var(--fs-sm);
  color: var(--success);
  margin-top: var(--space-xs);
}

.source-verified svg {
  width: 14px;
  height: 14px;
}

/* ---------- WIKI BADGE ---------- */
.wiki-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 18px;
  height: 18px;
  margin-left: 4px;
  font-size: 11px;
  font-weight: var(--fw-bold);
  color: var(--text-muted);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  vertical-align: middle;
  transition: background var(--transition), color var(--transition), border-color var(--transition);
}

.wiki-badge:hover {
  background: var(--text);
  color: var(--bg);
  border-color: var(--text);
  text-decoration: none;
}

/* ---------- BADGE ---------- */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 8px;
  font-size: var(--fs-sm);
  font-weight: var(--fw-medium);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text-muted);
}

.badge svg {
  width: 12px;
  height: 12px;
}

.badge-success {
  background: rgba(26, 127, 55, 0.1);
  color: var(--success);
}

.badge-primary {
  background: rgba(0, 102, 204, 0.1);
  color: var(--primary);
}

/* ---------- BREADCRUMB ---------- */
.breadcrumb {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-xs);
  padding: var(--space-md) 0;
  font-size: var(--fs-sm);
  color: var(--text-muted);
}

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

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

.breadcrumb-sep {
  color: var(--border);
}

.breadcrumb-current {
  color: var(--text);
  font-weight: var(--fw-medium);
}

/* ---------- TOC ---------- */
.toc {
  padding: var(--space-lg);
  margin-bottom: var(--space-lg);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--bg);
}

.toc-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-sm);
  font-size: var(--fs-base);
  font-weight: var(--fw-bold);
  color: var(--text);
  margin-bottom: var(--space-md);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.toc-header-left {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
}

.toc-header svg {
  width: 20px;
  height: 20px;
  color: var(--primary);
  flex-shrink: 0;
}

.toc-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border-radius: var(--radius-sm);
  color: var(--text-muted);
}

.toc-toggle:hover {
  background: var(--surface);
  color: var(--text);
}

.toc-toggle svg {
  width: 18px;
  height: 18px;
  color: currentColor;
  transition: transform var(--transition);
}

.toc.collapsed .toc-toggle svg {
  transform: rotate(-90deg);
}

.toc-list {
  display: flex;
  flex-direction: column;
  gap: var(--space-xs);
  padding: 0;
  margin: 0;
}

.toc.collapsed .toc-list {
  display: none;
}

.toc-list a {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  padding: var(--space-xs) var(--space-sm);
  font-size: var(--fs-sm);
  color: var(--text-muted);
  border-radius: var(--radius-sm);
  transition: background var(--transition), color var(--transition);
}

.toc-list a:hover {
  background: var(--surface);
  color: var(--text);
  text-decoration: none;
}

.toc-list a.active {
  color: var(--primary);
  font-weight: var(--fw-medium);
}

.toc-list a::before {
  content: '';
  width: 4px;
  height: 4px;
  border-radius: 50%;
  background: currentColor;
  flex-shrink: 0;
}

.toc-list a.active::before {
  width: 6px;
  height: 6px;
}

/* ---------- IMAGE FIGURE ---------- */
.figure {
  margin: 0 0 var(--space-lg) 0;
}

.figure img {
  width: 100%;
  border-radius: var(--radius-md);
  background: var(--surface);
}

.figure-caption {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: var(--space-sm) 0;
  font-size: var(--fs-sm);
  color: var(--text-muted);
  border-bottom: 1px solid var(--border);
}

.figure-caption-text {
  color: var(--text);
}

.figure-attribution {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-xs);
  font-size: var(--fs-sm);
  color: var(--text-muted);
}

.figure-attribution svg {
  width: 12px;
  height: 12px;
}

.figure-attribution a {
  color: var(--text-muted);
  text-decoration: underline;
}

.figure-attribution a:hover {
  color: var(--primary);
}

/* ---------- TABLE ---------- */
.table-wrap {
  width: 100%;
  overflow-x: auto;
  margin-bottom: var(--space-lg);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
}

table {
  width: 100%;
  font-size: var(--fs-sm);
  border-collapse: collapse;
}

table th,
table td {
  padding: var(--space-sm) var(--space-md);
  text-align: left;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}

table th {
  font-weight: var(--fw-medium);
  color: var(--text);
  background: var(--surface);
  white-space: nowrap;
}

table td {
  color: var(--text);
}

table tr:last-child th,
table tr:last-child td {
  border-bottom: none;
}

table code {
  font-size: 0.85em;
}

/* Mobile: table → cards */
@media (max-width: 639px) {
  .table-wrap {
    border: none;
    overflow: visible;
  }

  table,
  table thead,
  table tbody,
  table tr,
  table th,
  table td {
    display: block;
  }

  table thead {
    display: none;
  }

  table tr {
    padding: var(--space-md);
    margin-bottom: var(--space-sm);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
  }

  table td {
    padding: var(--space-xs) 0;
    border: none;
    display: flex;
    gap: var(--space-sm);
  }

  table td::before {
    content: attr(data-label);
    font-weight: var(--fw-medium);
    color: var(--text-muted);
    flex-shrink: 0;
    min-width: 100px;
  }
}

/* ---------- PROGRESS BAR ---------- */
.progress {
  display: flex;
  flex-direction: column;
  gap: var(--space-xs);
}

.progress-bar {
  width: 100%;
  height: 8px;
  background: var(--surface);
  border-radius: var(--radius-sm);
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  background: var(--primary);
  border-radius: var(--radius-sm);
  transition: width 0.3s ease;
}

.progress-label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: var(--fs-sm);
  color: var(--text-muted);
}

.progress-label strong {
  color: var(--text);
  font-weight: var(--fw-medium);
}

/* ---------- MODAL ---------- */
.modal {
  position: fixed;
  inset: 0;
  z-index: 300;
  display: none;
  align-items: center;
  justify-content: center;
  padding: var(--space-md);
}

.modal.open {
  display: flex;
}

.modal-overlay {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
}

.modal-content {
  position: relative;
  width: 100%;
  max-width: 640px;
  max-height: 90vh;
  overflow-y: auto;
  background: var(--bg);
  border-radius: var(--radius-md);
  padding: var(--space-lg);
  box-shadow: var(--shadow);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-md);
  margin-bottom: var(--space-lg);
  padding-bottom: var(--space-md);
  border-bottom: 1px solid var(--border);
}

.modal-title {
  font-size: var(--fs-lg);
  font-weight: var(--fw-bold);
}

/* ---------- SEARCH OVERLAY ---------- */

/* ---------- SEARCH RESULTS ENHANCEMENT ---------- */
.search-results {
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
  margin-top: var(--space-md);
}

.search-hint {
  font-size: var(--fs-sm);
  color: var(--text-muted);
  text-align: center;
  padding: var(--space-xl) 0;
}

.search-group {
  display: flex;
  flex-direction: column;
  gap: var(--space-xs);
}

.search-result {
  display: flex;
  align-items: center;
  gap: var(--space-md);
  padding: var(--space-sm) var(--space-md);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--bg);
  color: var(--text);
  transition: border-color var(--transition), background var(--transition);
  text-decoration: none;
}

.search-result:hover {
  border-color: var(--primary);
  background: var(--surface);
  text-decoration: none;
  color: var(--text);
}

.search-result-icon {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--primary);
  flex-shrink: 0;
}

.search-result-icon svg {
  width: 18px;
  height: 18px;
}

.search-result-body {
  display: flex;
  flex-direction: column;
  gap: 2px;
  flex: 1;
  min-width: 0;
}

.search-result-title {
  font-size: var(--fs-base);
  font-weight: var(--fw-medium);
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.search-result-subtitle {
  font-size: var(--fs-sm);
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.search-overlay {
  position: fixed;
  inset: 0;
  z-index: 300;
  display: none;
  background: var(--bg);
  padding: var(--space-lg);
  overflow-y: auto;
}

.search-overlay.open {
  display: block;
}

.search-input-wrap {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  padding: var(--space-sm) var(--space-md);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  margin-bottom: var(--space-lg);
  background: var(--bg);
}

.search-input-wrap:focus-within {
  border-color: var(--primary);
}

.search-input-wrap svg {
  width: 20px;
  height: 20px;
  color: var(--text-muted);
  flex-shrink: 0;
}

.search-input {
  flex: 1;
  min-width: 0;
  font-size: var(--fs-base);
  background: none;
  border: none;
  outline: none;
  color: var(--text);
}

.search-input::placeholder {
  color: var(--text-muted);
}

/* ---------- TABS ---------- */
.tabs {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-xs);
  margin-bottom: var(--space-lg);
  padding-bottom: var(--space-sm);
  border-bottom: 1px solid var(--border);
}

.tab {
  padding: var(--space-xs) var(--space-md);
  font-size: var(--fs-sm);
  font-weight: var(--fw-medium);
  color: var(--text-muted);
  border-radius: var(--radius-sm);
  transition: background var(--transition), color var(--transition);
  min-height: 36px;
}

.tab:hover {
  background: var(--surface);
  color: var(--text);
}

.tab.active {
  background: var(--primary);
  color: #FFFFFF;
}

/* ---------- DIVIDER ---------- */
.divider {
  height: 1px;
  background: var(--border);
  margin: var(--space-lg) 0;
  border: none;
}

/* ---------- EMPTY STATE ---------- */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-md);
  padding: var(--space-2xl) var(--space-lg);
  text-align: center;
  color: var(--text-muted);
}

.empty-state svg {
  width: 48px;
  height: 48px;
  color: var(--border);
}

.empty-state h3 {
  color: var(--text);
  font-size: var(--fs-lg);
}

/* ---------- RESPONSIVE ---------- */
@media (min-width: 640px) {
  .source-row {
    flex-wrap: nowrap;
  }
}