/* ==========================================================================
   ProdyPanda Factures — Trilingual Multi-Level HTML Wiki Stylesheet
   Warm Ledger Light Mode (#F5F4F0) & Carbon Ledger Dark Mode (#141413)
   Supports LTR (English, Français) and RTL (العربية)
   ========================================================================== */

:root {
  --bg-page: #F5F4F0;
  --bg-surface: #FFFFFF;
  --bg-elevated: #FAF9F6;
  --bg-subtle: #EAE8E1;
  --bg-code: #181817;
  --text-primary: #141413;
  --text-secondary: #4A4945;
  --text-muted: #5C5A54;
  --border-color: #DDDAD2;
  --border-strong: #C5C1B7;
  --brand-crimson: #D80010;
  --brand-crimson-hover: #A8000C;
  --brand-crimson-soft: rgba(216, 0, 16, 0.1);
  --shadow-sm: 0 1px 3px rgba(20, 20, 19, 0.06);
  --shadow-md: 0 6px 20px rgba(20, 20, 19, 0.09);
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 14px;
  --topbar-height: 62px;
  --sidebar-width: 280px;
  --toc-width: 250px;
  --font-sans: 'IBM Plex Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --font-ar: 'Segoe UI', Tahoma, 'IBM Plex Sans Arabic', 'Noto Sans Arabic', sans-serif;
  --font-mono: 'JetBrains Mono', 'Cascadia Mono', Consolas, monospace;
}

html[data-theme="dark"] {
  color-scheme: dark;
  --bg-page: #141413;
  --bg-surface: #1D1D1B;
  --bg-elevated: #252522;
  --bg-subtle: #2D2D2A;
  --bg-code: #0E0E0D;
  --text-primary: #F2F0E8;
  --text-secondary: #B8B5AC;
  --text-muted: #B8B4A9;
  --border-color: #32312E;
  --border-strong: #46443F;
  --brand-crimson: #F02838;
  --brand-crimson-hover: #FF4757;
  --brand-crimson-soft: rgba(240, 40, 56, 0.16);
  --shadow-sm: 0 1px 4px rgba(0, 0, 0, 0.35);
  --shadow-md: 0 10px 30px rgba(0, 0, 0, 0.55);
}

*, *::before, *::after {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
  scroll-padding-top: calc(var(--topbar-height) + 20px);
}

body {
  margin: 0;
  background-color: var(--bg-page);
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: 15.5px;
  line-height: 1.68;
  -webkit-font-smoothing: antialiased;
}

html[dir="rtl"] body {
  font-family: var(--font-ar);
  font-size: 16px;
  line-height: 1.8;
}

a {
  color: var(--brand-crimson);
  text-decoration: none;
  transition: color 0.15s ease;
}

a:hover {
  color: var(--brand-crimson-hover);
  text-decoration: underline;
}

/* Top Navigation Bar */
.wiki-topbar {
  position: sticky;
  top: 0;
  z-index: 100;
  height: var(--topbar-height);
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 18px;
  gap: 14px;
  box-shadow: var(--shadow-sm);
}

.topbar-start, .topbar-end {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-link {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--text-primary);
  text-decoration: none !important;
}

.brand-logo {
  height: 34px;
  width: auto;
  border-radius: 6px;
}

.logo-dark { display: none; }
html[data-theme="dark"] .logo-light { display: none; }
html[data-theme="dark"] .logo-dark { display: inline-block; }

.brand-text {
  display: flex;
  flex-direction: column;
  line-height: 1.15;
}

.brand-title {
  font-weight: 700;
  font-size: 15px;
  letter-spacing: -0.01em;
}

.brand-accent {
  color: var(--brand-crimson);
}

.brand-subtitle {
  font-size: 11px;
  color: var(--text-muted);
}

/* Search Trigger */
.topbar-center {
  flex: 1;
  max-width: 440px;
}

.search-trigger-btn {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 7px 12px;
  background: var(--bg-elevated);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  color: var(--text-muted);
  cursor: pointer;
  font-family: inherit;
  font-size: 13.5px;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.search-trigger-btn:hover {
  border-color: var(--brand-crimson);
  color: var(--text-primary);
}

.search-trigger-text {
  flex: 1;
  text-align: start;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.search-kbd {
  font-family: var(--font-mono);
  font-size: 11px;
  padding: 2px 6px;
  border-radius: 4px;
  background: var(--bg-subtle);
  color: var(--text-secondary);
  border: 1px solid var(--border-color);
}

/* Language Switcher & Icon Buttons */
.lang-switcher {
  display: flex;
  align-items: center;
  background: var(--bg-elevated);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 2px;
  gap: 2px;
}

.lang-pill {
  padding: 4px 9px;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  text-decoration: none !important;
}

.lang-pill:hover {
  color: var(--text-primary);
  background: var(--bg-subtle);
}

.lang-pill.active {
  background: var(--brand-crimson);
  color: #FFFFFF;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-color);
  background: var(--bg-elevated);
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.15s ease;
}

.icon-btn:hover {
  color: var(--brand-crimson);
  border-color: var(--brand-crimson);
}

.icon-moon { display: none; }
html[data-theme="dark"] .icon-sun { display: none; }
html[data-theme="dark"] .icon-moon { display: block; }

/* 3-Column Layout */
.wiki-layout {
  display: grid;
  grid-template-columns: var(--sidebar-width) minmax(0, 1fr) var(--toc-width);
  max-width: 1560px;
  margin: 0 auto;
  min-height: calc(100vh - var(--topbar-height));
}

body.sidebar-collapsed .wiki-layout {
  grid-template-columns: 0px minmax(0, 1fr) var(--toc-width);
}

body.sidebar-collapsed .wiki-sidebar {
  overflow: hidden;
  padding: 0;
  border: none;
}

/* Left Sidebar */
.wiki-sidebar {
  position: sticky;
  top: var(--topbar-height);
  height: calc(100vh - var(--topbar-height));
  overflow-y: auto;
  background: var(--bg-surface);
  border-inline-end: 1px solid var(--border-color);
  padding: 20px 14px;
}

.sidebar-section {
  margin-bottom: 24px;
}

.sidebar-heading {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  padding: 0 10px 8px;
}

.sidebar-nav-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.sidebar-link {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  font-size: 13.5px;
  font-weight: 500;
  text-decoration: none !important;
  transition: background 0.15s, color 0.15s;
}

.sidebar-link:hover {
  background: var(--bg-elevated);
  color: var(--text-primary);
}

.sidebar-link.active {
  background: var(--brand-crimson-soft);
  color: var(--brand-crimson);
  font-weight: 600;
}

/* Level Badges */
.level-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 700;
  padding: 2px 6px;
  border-radius: 4px;
  white-space: nowrap;
  flex-shrink: 0;
}

.badge-neutral { background: #6E6D68; color: #FFFFFF; }
.badge-green   { background: #15803D; color: #FFFFFF; }
.badge-blue    { background: #1D4ED8; color: #FFFFFF; }
.badge-amber   { background: #B45309; color: #FFFFFF; }
.badge-purple  { background: #6D28D9; color: #FFFFFF; }
.badge-crimson { background: #D80010; color: #FFFFFF; }

.sidebar-lang-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 6px;
  padding: 0 6px;
}

.sidebar-lang-btn {
  display: block;
  padding: 7px 10px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-color);
  background: var(--bg-elevated);
  color: var(--text-secondary);
  font-size: 13px;
  font-weight: 600;
  text-decoration: none !important;
}

.sidebar-lang-btn.active,
.sidebar-lang-btn:hover {
  border-color: var(--brand-crimson);
  color: var(--brand-crimson);
}

/* Main Article Container */
.wiki-main {
  padding: 32px 42px 64px;
  min-width: 0;
}

.wiki-article {
  background: var(--bg-surface);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  padding: 36px 44px;
  box-shadow: var(--shadow-sm);
  overflow-wrap: anywhere;
}

/* Hub Quick Cards */
.hub-hero-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 14px;
  margin-bottom: 30px;
}

@media (max-width: 960px) {
  .hub-hero-grid {
    grid-template-columns: 1fr;
  }
}

.hub-lang-card {
  background: var(--bg-elevated);
  border: 1px solid var(--border-color);
  border-top: 3px solid var(--brand-crimson);
  border-radius: var(--radius-md);
  padding: 16px 18px;
}

.hub-lang-header {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  margin-bottom: 12px;
}

.hub-lang-flag {
  font-size: 26px;
  line-height: 1;
}

.hub-lang-header h3 {
  margin: 0 0 4px;
  font-size: 16px;
}

.hub-lang-header p {
  margin: 0;
  font-size: 12.5px;
  color: var(--text-muted);
}

.hub-level-pills {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.hub-pill {
  font-size: 12px;
  font-weight: 600;
  padding: 4px 9px;
  border-radius: 5px;
  text-decoration: none !important;
  border: 1px solid var(--border-color);
  background: var(--bg-surface);
  color: var(--text-primary);
}

.hub-pill:hover {
  border-color: var(--brand-crimson);
  color: var(--brand-crimson);
}

/* Typography & Headings */
.wiki-article h1,
.wiki-article h2,
.wiki-article h3,
.wiki-article h4 {
  color: var(--text-primary);
  font-weight: 700;
  line-height: 1.3;
  position: relative;
}

.wiki-h1 {
  font-size: 28px;
  margin: 0 0 20px;
  padding-bottom: 14px;
  border-bottom: 2px solid var(--brand-crimson);
}

.wiki-h2 {
  font-size: 21px;
  margin: 36px 0 14px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border-color);
}

.wiki-h3 {
  font-size: 17px;
  margin: 26px 0 10px;
}

.wiki-h4 {
  font-size: 15px;
  margin: 20px 0 8px;
}

.heading-anchor {
  opacity: 0;
  margin-inline-start: 8px;
  color: var(--text-muted);
  font-weight: 400;
  text-decoration: none !important;
  transition: opacity 0.15s;
}

.wiki-article h1:hover .heading-anchor,
.wiki-article h2:hover .heading-anchor,
.wiki-article h3:hover .heading-anchor,
.wiki-article h4:hover .heading-anchor {
  opacity: 1;
}

.wiki-article p {
  margin: 0 0 14px;
}

.wiki-list {
  margin: 0 0 16px;
  padding-inline-start: 24px;
}

.wiki-list li {
  margin-bottom: 6px;
}

.wiki-sublist {
  margin: 6px 0 8px;
  padding-inline-start: 22px;
}

.list-continuation {
  margin: 6px 0 8px !important;
}

.wiki-divider {
  border: 0;
  border-top: 1px solid var(--border-color);
  margin: 28px 0;
}

/* Inline Code & Fenced Code Blocks */
code {
  font-family: var(--font-mono);
  font-size: 0.88em;
  background: var(--bg-subtle);
  color: var(--text-primary);
  padding: 2px 6px;
  border-radius: 4px;
}

.code-block-wrapper,
.mermaid-wrapper {
  margin: 18px 0;
  border-radius: var(--radius-md);
  overflow: hidden;
  border: 1px solid var(--border-strong);
  background: var(--bg-code);
}

.mermaid-wrapper {
  background: var(--bg-elevated);
}

.code-block-header,
.mermaid-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 7px 14px;
  background: rgba(255, 255, 255, 0.06);
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}

.mermaid-header {
  background: var(--bg-subtle);
  border-bottom: 1px solid var(--border-color);
}

.code-lang-badge {
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 700;
  color: #D4D2C8;
  letter-spacing: 0.05em;
}

.mermaid-header .code-lang-badge {
  color: var(--text-secondary);
}

.copy-code-btn {
  background: rgba(255, 255, 255, 0.1);
  color: #F2F0E8;
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 4px;
  padding: 3px 9px;
  font-size: 11.5px;
  font-family: var(--font-sans);
  cursor: pointer;
}

.copy-code-btn:hover {
  background: var(--brand-crimson);
  border-color: var(--brand-crimson);
}

.code-block {
  margin: 0;
  padding: 16px;
  overflow-x: auto;
  color: #F5F4F0;
  font-family: var(--font-mono);
  font-size: 13px;
  line-height: 1.55;
}

.code-block code {
  background: transparent;
  color: inherit;
  padding: 0;
  font-size: inherit;
}

pre.mermaid {
  margin: 0;
  padding: 18px;
  overflow-x: auto;
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--text-primary);
  text-align: center;
}

/* Responsive Tables */
.table-responsive {
  width: 100%;
  overflow-x: auto;
  margin: 18px 0 22px;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 8px;
}

.wiki-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

.wiki-table th,
.wiki-table td {
  padding: 10px 14px;
  border-bottom: 1px solid var(--border-color);
  text-align: start;
  vertical-align: top;
}

.wiki-table th {
  background: var(--bg-elevated);
  font-weight: 700;
  color: var(--text-primary);
  border-bottom: 2px solid var(--border-strong);
}

.wiki-table tr:last-child td {
  border-bottom: none;
}

.wiki-table tbody tr:hover {
  background: var(--bg-elevated);
}

/* Callout Alerts & Blockquotes */
.wiki-alert {
  margin: 18px 0;
  padding: 14px 18px;
  border-radius: var(--radius-md);
  border-inline-start: 4px solid var(--brand-crimson);
  background: var(--bg-elevated);
}

.wiki-alert-title {
  font-weight: 700;
  font-size: 14px;
  margin-bottom: 6px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.wiki-alert-body p:last-child {
  margin-bottom: 0;
}

.wiki-alert-note      { border-inline-start-color: #2563EB; }
.wiki-alert-tip       { border-inline-start-color: #16A34A; }
.wiki-alert-important { border-inline-start-color: #7C3AED; }
.wiki-alert-warning   { border-inline-start-color: #D97706; }
.wiki-alert-caution   { border-inline-start-color: #DC2626; }

.wiki-blockquote {
  margin: 16px 0;
  padding: 10px 18px;
  border-inline-start: 3px solid var(--border-strong);
  background: var(--bg-elevated);
  color: var(--text-secondary);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
}

.wiki-blockquote p:last-child {
  margin-bottom: 0;
}

/* Prev / Next Pager */
.wiki-pager {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  margin-top: 40px;
  padding-top: 24px;
  border-top: 1px solid var(--border-color);
}

.wiki-pager-card {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 14px 18px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border-color);
  background: var(--bg-elevated);
  text-decoration: none !important;
  transition: border-color 0.15s, transform 0.15s;
}

.wiki-pager-card:hover {
  border-color: var(--brand-crimson);
  transform: translateY(-1px);
}

.wiki-pager-next {
  text-align: end;
  align-items: flex-end;
}

.pager-direction {
  font-size: 12px;
  color: var(--text-muted);
  font-weight: 600;
}

.pager-title {
  font-size: 14px;
  font-weight: 700;
  color: var(--text-primary);
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.wiki-footer {
  margin-top: 28px;
  text-align: center;
  font-size: 13px;
  color: var(--text-muted);
}

/* Right Table of Contents */
.wiki-toc {
  padding: 28px 16px;
}

.toc-sticky {
  position: sticky;
  top: calc(var(--topbar-height) + 24px);
  max-height: calc(100vh - var(--topbar-height) - 48px);
  overflow-y: auto;
}

.toc-heading {
  font-size: 11.5px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  margin-bottom: 10px;
}

.toc-list {
  list-style: none;
  margin: 0;
  padding: 0;
  border-inline-start: 2px solid var(--border-color);
}

.toc-item {
  margin: 0;
}

.toc-link {
  display: block;
  padding: 5px 12px;
  font-size: 12.5px;
  color: var(--text-secondary);
  text-decoration: none !important;
  border-inline-start: 2px solid transparent;
  margin-inline-start: -2px;
  line-height: 1.4;
}

.toc-h3 .toc-link {
  padding-inline-start: 24px;
  font-size: 12px;
}

.toc-link:hover,
.toc-link.active {
  color: var(--brand-crimson);
  border-inline-start-color: var(--brand-crimson);
  font-weight: 600;
}

/* Instant Search Modal */
.search-modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: rgba(15, 15, 14, 0.65);
  backdrop-filter: blur(3px);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 8vh 16px 24px;
}

.search-modal-backdrop[hidden] {
  display: none !important;
}

.search-modal {
  width: 100%;
  max-width: 680px;
  background: var(--bg-surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
  overflow: hidden;
}

.search-modal-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border-color);
}

.search-modal-input {
  flex: 1;
  min-width: 0;
  border: none;
  background: transparent;
  color: var(--text-primary);
  font-size: 16px;
  font-family: inherit;
  outline: none;
}

.search-close-btn {
  font-family: var(--font-mono);
  font-size: 11px;
  padding: 4px 8px;
  border-radius: 4px;
  border: 1px solid var(--border-color);
  background: var(--bg-elevated);
  color: var(--text-secondary);
  cursor: pointer;
}

.search-filter-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 8px 18px;
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border-color);
}

.search-lang-filter {
  border: 1px solid var(--border-color);
  background: var(--bg-surface);
  color: var(--text-secondary);
  border-radius: 999px;
  padding: 3px 11px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}

.search-lang-filter.active {
  background: var(--brand-crimson);
  color: #FFFFFF;
  border-color: var(--brand-crimson);
}

.search-results-list {
  max-height: 60vh;
  overflow-y: auto;
  padding: 10px;
}

.search-result-item {
  display: block;
  padding: 12px 14px;
  border-radius: var(--radius-sm);
  text-decoration: none !important;
  color: var(--text-primary);
  border: 1px solid transparent;
  margin-bottom: 4px;
}

.search-result-item:hover {
  background: var(--bg-elevated);
  border-color: var(--brand-crimson);
}

.search-result-top {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-bottom: 4px;
}

.search-result-title {
  font-weight: 700;
  font-size: 14.5px;
}

.search-result-snippet {
  font-size: 12.5px;
  color: var(--text-secondary);
  line-height: 1.45;
}

/* Responsive Breakpoints */
@media (max-width: 1180px) {
  .wiki-layout {
    grid-template-columns: var(--sidebar-width) minmax(0, 1fr);
  }
  .wiki-toc {
    display: none;
  }
}

@media (max-width: 860px) {
  :root {
    --topbar-height: 108px;
  }
  .wiki-topbar {
    height: auto;
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 8px;
    padding: 8px 12px;
  }
  .topbar-start {
    grid-column: 1 / -1;
  }
  .topbar-center {
    min-width: 0;
    max-width: none;
  }
  .search-trigger-btn {
    min-height: 36px;
    justify-content: center;
  }
  .search-trigger-text,
  .search-kbd {
    display: none;
  }
  .topbar-end {
    gap: 6px;
  }
  .wiki-layout {
    grid-template-columns: minmax(0, 1fr);
  }
  .wiki-sidebar {
    position: fixed;
    top: var(--topbar-height);
    inset-inline-start: 0;
    z-index: 150;
    width: 280px;
    box-shadow: var(--shadow-md);
    visibility: hidden;
  }
  body.sidebar-mobile-open .wiki-sidebar {
    visibility: visible;
  }
  .wiki-main {
    padding: 18px 14px 40px;
  }
  .wiki-article {
    padding: 22px 18px;
  }
  .wiki-pager {
    grid-template-columns: 1fr;
  }
}

/* Print Stylesheet */
@media print {
  .wiki-topbar,
  .wiki-sidebar,
  .wiki-toc,
  .wiki-pager,
  .copy-code-btn,
  .search-modal-backdrop {
    display: none !important;
  }
  body {
    background: #FFFFFF;
    color: #000000;
  }
  .wiki-layout {
    display: block;
  }
  .wiki-main {
    padding: 0;
  }
  .wiki-article {
    border: none;
    box-shadow: none;
    padding: 0;
  }
}
