/* Final Polish CSS - Phase 3 Part 3 */
/* Enhanced UX, notifications, help system, and production-ready styling */


/* Help System Styles */
.help-button {
  background: none;
  border: 1px solid hsl(var(--border));
  border-radius: 50%;
  width: 2rem;
  height: 2rem;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s ease;
  font-size: 0.875rem;
  margin-left: 1rem;
}

.help-button:hover {
  background: hsl(var(--background-hover));
  border-color: hsl(var(--primary));
  transform: scale(1.05);
}

.help-float {
  position: fixed;
  bottom: 2rem;
  right: 2rem;
  width: 3rem;
  height: 3rem;
  background: hsl(var(--primary));
  color: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  z-index: 1000;
  transition: all 0.3s ease;
  font-size: 1.25rem;
}

.help-float:hover {
  transform: scale(1.1);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.2);
}

.help-modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(4px);
  z-index: 2000;
  display: flex;
  align-items: center;
  justify-content: center;
  animation: fadeIn 0.3s ease;
}

.help-modal-content {
  background: hsl(var(--background));
  border-radius: 0.75rem;
  width: 90%;
  max-width: 600px;
  max-height: 80vh;
  overflow: hidden;
  box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1);
  border: 1px solid hsl(var(--border));
  animation: slideIn 0.3s ease;
}

.help-modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1.5rem;
  border-bottom: 1px solid hsl(var(--border));
  background: hsl(var(--background-subtle));
}

.help-modal-header h2 {
  margin: 0;
  color: hsl(var(--foreground));
  font-size: 1.5rem;
  font-weight: 700;
}

.help-close {
  background: none;
  border: none;
  font-size: 1.5rem;
  color: hsl(var(--foreground-muted));
  cursor: pointer;
  padding: 0.25rem;
  border-radius: 0.25rem;
  transition: all 0.2s ease;
}

.help-close:hover {
  background: hsl(var(--background-hover));
  color: hsl(var(--foreground));
}

.help-modal-body {
  padding: 1.5rem;
  overflow-y: auto;
  max-height: 60vh;
}

.help-content h3 {
  color: hsl(var(--primary));
  margin-top: 0;
  margin-bottom: 1rem;
}

.help-content h4 {
  color: hsl(var(--foreground));
  margin-top: 1.5rem;
  margin-bottom: 0.75rem;
  font-weight: 600;
}

.help-content ul {
  margin-left: 1.5rem;
  line-height: 1.6;
}

.help-content li {
  margin-bottom: 0.5rem;
}

.help-shortcuts {
  background: hsl(var(--background-subtle));
  padding: 1rem;
  border-radius: 0.5rem;
  margin-top: 1.5rem;
}

.help-shortcuts h4 {
  margin-top: 0;
}

.help-related {
  margin-top: 1.5rem;
  border-top: 1px solid hsl(var(--border));
  padding-top: 1rem;
}

.help-links {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.help-link {
  background: hsl(var(--primary));
  color: white;
  border: none;
  padding: 0.5rem 1rem;
  border-radius: 0.375rem;
  cursor: pointer;
  transition: all 0.2s ease;
  font-size: 0.875rem;
}

.help-link:hover {
  background: hsl(var(--primary-hover));
  transform: translateY(-1px);
}

/* Notification System */
/* Global variable for topbar height (desktop default 4rem) */
:root {
  --topbar-height: 4rem;
}

/* Hide compact topbar search on desktop */
.topbar-search-mobile {
  display: none;
}

/* Hide compact topbar search by default (desktop) */
.topbar-search-mobile {
  display: none;
}

@media (max-width: 768px) {
  :root {
    --topbar-height: 3.75rem;
  }

  /* Hide brand text on mobile to reclaim topbar space (LTR and RTL) */
  .brand-text {
    display: none !important;
  }

  /* Mobile topbar search (LTR) */
  html[dir="ltr"] .topbar-search-mobile {
    display: flex;
    align-items: center;
    flex: 1 1 auto;
    padding: 0 0.5rem;
    order: 2;
  }

  /* Mobile topbar search (RTL mirror) */
  html[dir="rtl"] .topbar-search-mobile {
    display: flex;
    align-items: center;
    flex: 1 1 auto;
    padding: 0 0.5rem;
    order: 2;
    /* Center in RTL */
  }

  .topbar-search-mobile .topbar-search-input-wrapper {
    position: relative;
    width: 100%;
  }

  .topbar-search-mobile .search-icon {
    position: absolute;
    left: 0.75rem;
    top: 50%;
    transform: translateY(-50%);
    width: 1rem;
    height: 1rem;
    color: var(--foreground-muted);
    pointer-events: none;
  }

  html[dir="rtl"] .topbar-search-mobile .search-icon {
    left: auto;
    right: 0.75rem;
  }

  .topbar-search-mobile .search-input {
    width: 100%;
    padding: 0.5rem 0.75rem 0.5rem 2rem;
    background-color: var(--input);
    border: 1px solid var(--input-border);
    border-radius: var(--radius-lg);
    color: var(--foreground-secondary);
    font-size: var(--font-size-sm);
  }

  html[dir="rtl"] .topbar-search-mobile .search-input {
    padding: 0.5rem 2rem 0.5rem 0.75rem;
    text-align: right;
  }

  .topbar-search-mobile .search-input:focus {
    outline: none;
    border-color: var(--ring);
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
  }

  /* Hide desktop search on mobile; we use topbar one */
  .search-container {
    display: none;
  }

  /* Step 1: In RTL, put the hamburger (three lines) all the way to the right */
  html[dir="rtl"] .topbar-left {
    flex-direction: row !important;
    justify-content: flex-end;
  }

  html[dir="rtl"] .topbar-left .brand {
    order: 1;
  }

  html[dir="rtl"] .topbar-left .sidebar-toggle {
    order: 2;
  }

  /* Enforce perfect ordering of topbar sections on mobile */
  html[dir="ltr"] .topbar-left {
    order: 1;
  }

  html[dir="ltr"] .topbar-search-mobile {
    order: 2;
  }

  html[dir="ltr"] .topbar-right {
    order: 3;
  }

  /* DISABLED - Conflicts with index.html RTL mobile rules */
  /*
  html[dir="rtl"] .topbar-right {
    display: flex !important;
    order: 1 !important;
    html[dir="rtl"] .topbar-left .sidebar-toggle,
    body.rtl .topbar-left .sidebar-toggle,
    html.rtl .topbar-left .sidebar-toggle,
  }

  html[dir="rtl"] .topbar-search-mobile {
    order: 2 !important;
  }

  html[dir="rtl"] .topbar-left {
    order: 3 !important;
  }
  */


  /* Tablet: show compact search and keep ordering, hide desktop search */
  @media (max-width: 1024px) {

    html[dir="ltr"] .topbar-search-mobile,

    /* Make common widget shells fluid and enable container queries (safe no-op if unsupported) */
    :where(.widget-card, .dashboard-widget, .finance-card, .smart-widget, .template-card) {
      max-width: 100%;
      min-width: 0;
      container-type: inline-size;
    }

    /* Ensure media elements never overflow on narrow screens */
    :where(.widget-card, .dashboard-widget, .finance-card, .smart-widget, .template-card) img,
    :where(.widget-card, .dashboard-widget, .finance-card, .smart-widget, .template-card) video,
    :where(.widget-card, .dashboard-widget, .finance-card, .smart-widget, .template-card) canvas,
    :where(.widget-card, .dashboard-widget, .finance-card, .smart-widget, .template-card) svg {
      max-width: 100%;
      height: auto;
    }

    /* Let long tables scroll horizontally without breaking layout */
    :where(.widget-card, .dashboard-widget, .finance-card, .smart-widget) {
      overflow-x: auto;
    }

    :where(.widget-card, .dashboard-widget, .finance-card, .smart-widget) table {
      width: 100%;
      min-width: 640px;
      /* prevent unreadable squish on small screens */
    }

    /* Widget headers wrap gracefully on small widths */
    :where(.widget-card, .dashboard-widget, .finance-card, .smart-widget) .widget-header {
      display: flex;
      flex-wrap: wrap;
      gap: .5rem .65rem;
      align-items: flex-start;
    }

    :where(.widget-card, .dashboard-widget, .finance-card, .smart-widget) .widget-actions {
      display: flex;
      flex-wrap: wrap;
      gap: .35rem .45rem;
    }

    /* Unified desktop content gutter next to fixed sidebar (LTR/RTL logical) */
    @media (min-width: 1025px) {
      .sidebar+.main-content {
        /* Keep a small breathing space so cards never touch or slide under the sidebar */
        padding-inline-start: max(var(--sidebar-gap, 0.75rem), 0px);
      }

      /* In RTL, logical padding maps to right automatically; keep symmetry explicit for robustness */
      html[dir="rtl"] .sidebar+.main-content {
        padding-inline-end: max(var(--sidebar-gap, 0.75rem), 0px);
      }
    }

    /* Mobile & tablet: sidebar overlays content -> no extra offset; make grids single-column and generous spacing */
    @media (max-width: 1024px) {
      .sidebar+.main-content {
        margin: 0 !important;
        /* do not push content; overlay takes precedence */
        padding-inline: clamp(.5rem, 4vw, 1rem);
      }

      /* Common responsive grids: auto-fit cards to available width */
      :where(.page-grid, .finance-grid, .sales-sidebar, .finance-page-grid) {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
        gap: clamp(.6rem, 2.5vw, 1rem);
      }
    }

    /* Extra-small devices: tighten paddings and font sizes slightly to fit comfortably */
    @media (max-width: 420px) {
      :where(.widget-card, .dashboard-widget, .finance-card, .smart-widget) {
        padding: clamp(.6rem, 3.5vw, .9rem);
      }

      :where(.widget-card, .dashboard-widget, .finance-card, .smart-widget) .widget-title {
        font-size: .95em;
      }
    }

    html[dir="rtl"] .topbar-search-mobile {
      display: flex;
    }

    .search-container {
      display: none;
    }

    html[dir="ltr"] .topbar-left {
      order: 1;
    }

    html[dir="ltr"] .topbar-search-mobile {
      order: 2;
    }

    html[dir="ltr"] .topbar-right {
      order: 3;
    }

    /* DISABLED - Conflicts with index.html RTL mobile rules */
    /*
    html[dir="rtl"] .topbar-right {
      display: contents !important;
    }

    html[dir="rtl"] .topbar-left {
      order: 6 !important;
      flex-direction: row-reverse;
    }

    html[dir="rtl"] .topbar-search-mobile {
      order: 5 !important;
    }

    html[dir="rtl"] #global-period-btn {
      order: 4 !important;
    }

    html[dir="rtl"] #cloud-status-indicator,
    html[dir="rtl"] .cloud-status-indicator {
      order: 3 !important;
    }

    html[dir="rtl"] .notification-btn {
      order: 2 !important;
    }

    html[dir="rtl"] .user-btn {
      order: 1 !important;
    }
    */
  }

  /* Enforce perfect ordering of topbar sections on mobile */
  html[dir="ltr"] .topbar-left {
    order: 1;
  }

  html[dir="ltr"] .topbar-right {
    order: 3;
  }

  html[dir="rtl"] .topbar-left {
    order: 3;
  }

  html[dir="rtl"] .topbar-right {
    order: 1;
  }
}

/* Tablet breakpoint: also show compact search and hide desktop search */
@media (max-width: 1024px) {

  html[dir="ltr"] .topbar-search-mobile,
  html[dir="rtl"] .topbar-search-mobile {
    display: flex;
    align-items: center;
    flex: 1 1 auto;
    padding: 0 0.5rem;
  }

  /* Hide desktop search on tablet as well */
  .search-container {
    display: none;
  }

  /* Keep ordering on tablet */
  html[dir="ltr"] .topbar-left {
    order: 1;
  }

  html[dir="ltr"] .topbar-search-mobile {
    order: 2;
  }

  html[dir="ltr"] .topbar-right {
    order: 3;
  }

  html[dir="rtl"] .topbar-left {
    order: 3;
  }

  html[dir="rtl"] .topbar-search-mobile {
    order: 2;
  }

  html[dir="rtl"] .topbar-right {
    order: 1;
  }
}

.notification-container {
  position: fixed;
  top: calc(var(--topbar-height, 4rem) + 12px);
  right: max(12px, env(safe-area-inset-right));
  z-index: 3000;
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-width: min(380px, 92vw);
  pointer-events: none;
}

/* Logical placement via ID to reduce specificity issues */
#notification-container {
  position: fixed;
  top: calc(var(--topbar-height, 4rem) + 12px);
  /* Default: LTR to the right */
  inset-inline-end: max(12px, env(safe-area-inset-right));
  display: flex;
  flex-direction: column;
  gap: 12px;
  pointer-events: none;
  z-index: 3000;
}

/* RTL: appear on the left side */
html[dir="rtl"] #notification-container {
  inset-inline-start: max(12px, env(safe-area-inset-left));
  inset-inline-end: auto;
}

/* Force-left for all directions if needed */
.notification-container.force-left,
html[dir="ltr"] .notification-container.force-left {
  left: max(12px, env(safe-area-inset-left));
  right: auto;
}

html[dir="rtl"] .notification-container,
html[lang="ar"] .notification-container,
html.rtl .notification-container,
body.rtl .notification-container,
.rtl .notification-container {
  right: auto !important;
  left: 12px !important;
}

/* Ensure logical alignment of content when mirrored */
.rtl .notification-container,
html[dir="rtl"] .notification-container {
  align-items: flex-start;
}

.notification {
  --accent: #3b82f6;
  /* default info */
  background:
    linear-gradient(180deg, rgba(255, 255, 255, .06), rgba(255, 255, 255, .02)) hsl(var(--background));
  border: 1px solid color-mix(in oklab, var(--accent), hsl(var(--border)) 70%);
  border-radius: 12px;
  box-shadow:
    0 10px 30px rgba(0, 0, 0, .20),
    0 1px 0 rgba(255, 255, 255, .06) inset,
    0 0 0 1px rgba(255, 255, 255, .04) inset;
  overflow: hidden;
  transform: translateY(-10px) scale(.98);
  transition: transform .36s cubic-bezier(.21, .98, .34, 1), opacity .28s ease, filter .28s ease;
  position: relative;
  opacity: 0;
  pointer-events: auto;
  /* clickable even when container is pointer-events none */
  backdrop-filter: saturate(160%) blur(12px);
  -webkit-backdrop-filter: saturate(160%) blur(12px);
}

.notification::before {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  pointer-events: none;
  background: radial-gradient(120% 60% at var(--glow-x, 10%) 0%, rgba(59, 130, 246, .15) 0%, transparent 60%);
  opacity: .25;
  filter: blur(14px);
}

/* RTL reading order and alignment */
html[dir="rtl"] .notification,
html.rtl .notification,
body.rtl .notification,
.rtl .notification {
  direction: rtl;
  text-align: right;
}

html[dir="rtl"] .notification .notification-body,
html.rtl .notification .notification-body,
body.rtl .notification .notification-body,
.rtl .notification .notification-body {
  text-align: right;
}

html[dir="rtl"] .notification .notification-message,
html.rtl .notification .notification-message,
body.rtl .notification .notification-message,
.rtl .notification .notification-message {
  text-align: right;
}

html[dir="rtl"] .notification .notification-close,
html.rtl .notification .notification-close,
body.rtl .notification .notification-close,
.rtl .notification .notification-close {
  margin-right: auto;
  margin-left: 0;
}



/* Show state */
.notification.notification-show {
  transform: translateY(0) scale(1);
  opacity: 1;
}

.notification.notification-hide {
  transform: translateY(-10px) scale(.96);
  opacity: 0;
  filter: blur(1px);
}

.notification:hover {
  transform: translateY(-2px) scale(1.01);
}

.notification.dragging {
  transition: none !important;
  cursor: grabbing;
}



.notification-success {
  --accent: #22c55e;
}

.notification-error {
  --accent: #ef4444;
}

.notification-warning {
  --accent: #f59e0b;
}

.notification-info {
  --accent: #3b82f6;
}

/* Subtle per-type background tint */
.notification-success {
  background-color: color-mix(in oklab, #22c55e, transparent 95%);
}

.notification-error {
  background-color: color-mix(in oklab, #ef4444, transparent 95%);
}

.notification-warning {
  background-color: color-mix(in oklab, #f59e0b, transparent 95%);
}

.notification-info {
  background-color: color-mix(in oklab, #3b82f6, transparent 95%);
}

.notification-success::after,
.notification-error::after,
.notification-warning::after,
.notification-info::after {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: linear-gradient(90deg, rgba(255, 255, 255, .25), var(--accent) 80%);
  opacity: .9;
}

/* Thin left accent bar for quick scanning */
.notification::marker {
  display: none;
}

.notification::selection {
  background: transparent;
}

.notification::before {
  content: "";
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 3px;
  background: linear-gradient(180deg, var(--accent), color-mix(in oklab, var(--accent), transparent 50%));
  opacity: .75;
  pointer-events: none;
}

.notification-content {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 14px 16px;
}

html[dir="rtl"] .notification-content,
html[lang="ar"] .notification-content,
html.rtl .notification-content,
body.rtl .notification-content,
.rtl .notification-content {
  flex-direction: row-reverse;
}

/* Mirror actions group in RTL */
html[dir="rtl"] .notification-actions,
html[lang="ar"] .notification-actions,
html.rtl .notification-actions,
body.rtl .notification-actions,
.rtl .notification-actions {
  flex-direction: row-reverse;
}

.notification-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  flex: 0 0 28px;
  border-radius: 999px;
  background: color-mix(in oklab, var(--accent), #000 75%);
  box-shadow: 0 1px 0 rgba(255, 255, 255, .06) inset, 0 0 10px color-mix(in oklab, var(--accent), transparent 80%);
  color: white;
}

.notification-icon svg {
  display: block;
  stroke: currentColor;
}

.notification-body {
  flex: 1;
  min-width: 0;
}

.notification-message {
  font-weight: 600;
  color: hsl(var(--foreground));
  margin-bottom: 0.25rem;
  line-height: 1.4;
  letter-spacing: .2px;
}

.notification-details {
  font-size: 0.875rem;
  color: hsl(var(--foreground-muted));
  line-height: 1.4;
}

.notification-actions {
  display: flex;
  gap: 0.5rem;
  margin-top: 0.5rem;
}

.notification-action {
  background: hsl(var(--primary));
  color: white;
  border: none;
  padding: 0.25rem 0.75rem;
  border-radius: 0.25rem;
  cursor: pointer;
  font-size: 0.75rem;
  transition: all 0.2s ease;
}

.notification-action:hover {
  background: hsl(var(--primary-hover));
}

.notification-close {
  background: none;
  border: none;
  color: hsl(var(--foreground-muted));
  cursor: pointer;
  padding: 0.25rem;
  border-radius: 0.25rem;
  transition: all 0.2s ease;
  align-self: flex-start;
}

.notification-close:hover {
  background: hsl(var(--background-hover));
  color: hsl(var(--foreground));
}

.notification-timer {
  position: absolute;
  bottom: 0;
  left: 0;
  height: 3px;
  background: linear-gradient(90deg, #60a5fa, #22d3ee);
  animation: notificationTimer linear;
  transform-origin: left;
}

.notification:hover .notification-timer,
.notification.paused .notification-timer {
  animation-play-state: paused;
}

html[dir="rtl"] .notification-timer,
html[lang="ar"] .notification-timer,
html.rtl .notification-timer,
body.rtl .notification-timer,
.rtl .notification-timer {
  left: auto;
  right: 0;
  transform-origin: right;
}

@keyframes notificationTimer {
  from {
    width: 100%;
  }

  to {
    width: 0%;
  }
}

/* Hotfix: Ensure RTL mobile hamburger is at the far right */
@media (max-width: 768px) {
  html[dir="rtl"] .topbar {
    display: flex;
  }

  /* Place topbar-left (brand + hamburger) as the last section and align to the right */
  html[dir="rtl"] .topbar-left {
    order: 99 !important;
    display: flex !important;
    flex-direction: row !important;
    justify-content: flex-end !important;
    margin-left: 0 !important;
    margin-right: 0 !important;
  }

  /* Inside topbar-left: brand then hamburger, so hamburger sits at the extreme right */
  html[dir="rtl"] .topbar-left .brand {
    order: 1 !important;
  }

  html[dir="rtl"] .topbar-left .sidebar-toggle {
    order: 2 !important;
  }
}

/* Extend the same positioning to tablet widths */
@media (max-width: 1024px) {
  html[dir="rtl"] .topbar {
    display: flex;
  }

  html[dir="rtl"] .topbar-left {
    order: 99 !important;
    display: flex !important;
    flex-direction: row !important;
    justify-content: flex-end !important;
  }

  html[dir="rtl"] .topbar-left .brand {
    order: 1 !important;
  }

  html[dir="rtl"] .topbar-left .sidebar-toggle {
    order: 2 !important;
  }
}

/* Tablet breakpoint: show compact topbar search and enforce section order; hide desktop search */
@media (max-width: 1024px) {

  /* Show compact search for both directions */
  html[dir="ltr"] .topbar-search-mobile,
  html[dir="rtl"] .topbar-search-mobile {
    display: flex;
    align-items: center;
    flex: 1 1 auto;
    padding: 0 0.5rem;
  }

  /* Hide desktop search at tablet sizes */
  .search-container {
    display: none;
  }

  /* Keep ordering consistent with mobile */
  html[dir="ltr"] .topbar-left {
    order: 1;
  }

  html[dir="ltr"] .topbar-search-mobile {
    order: 2;
  }

  html[dir="ltr"] .topbar-right {
    order: 3;
  }

  /* RTL exact order (leftmost → rightmost order values low→high):
     1: .user-btn (left edge)
     2: .notification-btn
     3: #cloud-status-indicator /.cloud-status-indicator
     4: #global-period-btn
     5: .topbar-search-mobile
     6: .topbar-left (brand D + hamburger at the far right)
  */
  html[dir="rtl"] .topbar-right {
    display: contents !important;
  }

  html[dir="rtl"] .user-btn {
    order: 1 !important;
  }

  html[dir="rtl"] .notification-btn {
    order: 2 !important;
  }

  html[dir="rtl"] #cloud-status-indicator,
  html[dir="rtl"] .cloud-status-indicator {
    order: 3 !important;
  }

  html[dir="rtl"] #global-period-btn {
    order: 4 !important;
  }

  html[dir="rtl"] .topbar-search-mobile {
    order: 5 !important;
  }

  html[dir="rtl"] .topbar-left {
    order: 6 !important;
    flex-direction: row-reverse;
  }
}

/* Keyboard Shortcuts */
.shortcuts-modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(4px);
  z-index: 2000;
  display: flex;
  align-items: center;
  justify-content: center;
  animation: fadeIn 0.3s ease;
}

.shortcuts-modal-content {
  background: hsl(var(--background));
  border-radius: 0.75rem;
  width: 90%;
  max-width: 500px;
  max-height: 80vh;
  overflow: hidden;
  box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1);
  border: 1px solid hsl(var(--border));
}

.shortcuts-modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1.5rem;
  border-bottom: 1px solid hsl(var(--border));
  background: hsl(var(--background-subtle));
}

.shortcuts-modal-body {
  padding: 1.5rem;
  overflow-y: auto;
  max-height: 60vh;
}

.shortcuts-table {
  width: 100%;
  border-collapse: collapse;
}

.shortcuts-table th,
.shortcuts-table td {
  padding: 0.75rem;
  text-align: left;
  border-bottom: 1px solid hsl(var(--border));
}

.shortcuts-table th {
  background: hsl(var(--background-subtle));
  font-weight: 600;
  color: hsl(var(--foreground));
}

.shortcuts-table kbd {
  background: hsl(var(--background-subtle));
  border: 1px solid hsl(var(--border));
  border-radius: 0.25rem;
  padding: 0.25rem 0.5rem;
  font-family: monospace;
  font-size: 0.875rem;
  color: hsl(var(--foreground));
}

/* User Preferences */
.preferences-button,
.search-button {
  background: none;
  border: 1px solid hsl(var(--border));
  border-radius: 0.375rem;
  padding: 0.5rem;
  cursor: pointer;
  transition: all 0.2s ease;
  font-size: 1rem;
  margin-left: 0.5rem;
}

.preferences-button:hover,
.search-button:hover {
  background: hsl(var(--background-hover));
  border-color: hsl(var(--primary));
}

.preferences-modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(4px);
  z-index: 2000;
  display: flex;
  align-items: center;
  justify-content: center;
  animation: fadeIn 0.3s ease;
}

.preferences-modal-content {
  background: hsl(var(--background));
  border-radius: 0.75rem;
  width: 90%;
  max-width: 400px;
  box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1);
  border: 1px solid hsl(var(--border));
}

.preferences-form {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.preference-group {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.preference-group label {
  font-weight: 500;
  color: hsl(var(--foreground));
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.preference-group select {
  padding: 0.75rem;
  border: 1px solid hsl(var(--border));
  border-radius: 0.375rem;
  background: hsl(var(--background));
  color: hsl(var(--foreground));
}

.preference-group input[type="checkbox"] {
  width: 1rem;
  height: 1rem;
  accent-color: hsl(var(--primary));
}

/* Global Search */
.search-modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(4px);
  z-index: 2000;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding-top: 10vh;
}

.search-modal-content {
  background: hsl(var(--background));
  border-radius: 0.75rem;
  width: 90%;
  max-width: 600px;
  box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1);
  border: 1px solid hsl(var(--border));
  overflow: hidden;
}

.search-input-container {
  display: flex;
  align-items: center;
  padding: 1rem;
  border-bottom: 1px solid hsl(var(--border));
}

.search-input {
  flex: 1;
  border: none;
  outline: none;
  font-size: 1.125rem;
  background: transparent;
  color: hsl(var(--foreground));
}

.search-results {
  max-height: 400px;
  overflow-y: auto;
}

.search-result {
  padding: 1rem;
  border-bottom: 1px solid hsl(var(--border));
  cursor: pointer;
  transition: all 0.2s ease;
}

.search-result:hover {
  background: hsl(var(--background-hover));
}

.search-result-title {
  font-weight: 600;
  color: hsl(var(--foreground));
  margin-bottom: 0.25rem;
}

.search-result-subtitle {
  color: hsl(var(--foreground-muted));
  font-size: 0.875rem;
  margin-bottom: 0.25rem;
}

.search-result-type {
  background: hsl(var(--background-subtle));
  color: hsl(var(--foreground-muted));
  padding: 0.125rem 0.5rem;
  border-radius: 0.25rem;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  display: inline-block;
}

.search-hint,
.search-no-results {
  padding: 2rem;
  text-align: center;
  color: hsl(var(--foreground-muted));
  font-style: italic;
}

/* Guided Tour */
.tour-offer-modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(4px);
  z-index: 2000;
  display: flex;
  align-items: center;
  justify-content: center;
  animation: fadeIn 0.3s ease;
}

.tour-offer-content {
  background: hsl(var(--background));
  border-radius: 0.75rem;
  padding: 2rem;
  text-align: center;
  box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1);
  border: 1px solid hsl(var(--border));
  max-width: 400px;
}

.tour-offer-content h2 {
  color: hsl(var(--primary));
  margin-bottom: 1rem;
}

.tour-offer-actions {
  display: flex;
  gap: 1rem;
  justify-content: center;
  margin-top: 1.5rem;
}

.tour-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.3);
  z-index: 2000;
  pointer-events: none;
}

.tour-step {
  position: absolute;
  background: hsl(var(--background));
  border: 1px solid hsl(var(--border));
  border-radius: 0.5rem;
  padding: 1rem;
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);
  max-width: 300px;
  pointer-events: all;
  z-index: 2001;
}

.tour-step h3 {
  margin-top: 0;
  color: hsl(var(--primary));
}

.tour-actions {
  display: flex;
  gap: 0.5rem;
  margin-top: 1rem;
}

.tour-actions button {
  background: hsl(var(--primary));
  color: white;
  border: none;
  padding: 0.5rem 1rem;
  border-radius: 0.25rem;
  cursor: pointer;
  font-size: 0.875rem;
}

.tour-actions button:last-child {
  background: hsl(var(--background-subtle));
  color: hsl(var(--foreground-muted));
}

.tour-progress {
  font-size: 0.75rem;
  color: hsl(var(--foreground-muted));
  text-align: center;
  margin-top: 0.5rem;
}

.tour-highlight {
  position: relative;
  z-index: 2001;
  box-shadow: 0 0 0 4px hsl(var(--primary)) !important;
  border-radius: 0.375rem !important;
}

/* =========================
   Mobile-First Modal Optimization
   - Neutralize large inline widths
   - Sticky header/footer
   - Responsive forms & tables
   - LTR/RTL logical paddings
   ========================= */

:root {
  --modal-mobile-pad: 16px;
}

@media (max-width: 1024px) {

  /* Overlay spacing respects small screens */
  .modal-overlay {
    padding-top: max(6vh, var(--modal-mobile-pad));
    padding-bottom: var(--modal-mobile-pad);
    padding-inline: 12px;
    overflow: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    touch-action: pan-y;
  }

  /* Standard modal shells */
  .modal-overlay .modal-content {
    width: min(720px, 96vw);
    max-width: 96vw !important;
    min-width: 0 !important;
    height: auto;
    max-height: calc(100dvh - 2 * var(--modal-mobile-pad));
    border-radius: 14px;
    display: flex;
    flex-direction: column;
  }

  /* Custom shells with inline styles (no .modal-content) */
  .modal-overlay>div:not(.modal-content) {
    width: min(720px, 96vw) !important;
    max-width: 96vw !important;
    min-width: 0 !important;
    max-height: calc(100dvh - 2 * var(--modal-mobile-pad)) !important;
    border-radius: 14px !important;
    overflow: auto;
    box-sizing: border-box;
  }

  /* Outstanding Vendor Bills modal specific */
  #outstanding-vendor-modal {
    padding-top: max(6vh, var(--modal-mobile-pad));
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  #outstanding-vendor-modal>div {
    width: min(720px, 96vw) !important;
    max-width: 96vw !important;
    min-width: 0 !important;
    max-height: calc(100dvh - 2 * var(--modal-mobile-pad)) !important;
    border-radius: 14px !important;
    overflow: auto;
  }

  /* Ensure buttons stay within card bounds */
  .modal-entry-card {
    position: relative;
    overflow: hidden;
  }

  .modal-entry-card-menu {
    flex-shrink: 0;
    width: 44px;
    height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    line-height: 1;
  }

  /* Keep actions accessible */
  .modal-overlay .modal-header,
  .modal-overlay .modal-footer {
    position: sticky;
    z-index: 2;
    background: hsl(var(--card, var(--background)));
  }

  .modal-overlay .modal-header {
    top: 0;
  }

  .modal-overlay .modal-footer {
    bottom: 0;
  }

  .modal-overlay .modal-body {
    flex: 1 1 auto;
    overflow: auto;
  }

  .modal-overlay .modal-body {
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    touch-action: pan-y;
  }

  /* Compact action buttons */
  .modal-overlay .modal-header .btn,
  .modal-overlay .modal-footer .btn {
    padding: .5rem .7rem;
    font-size: .8rem;
  }

  /* Form grids inside modals: stack on small screens */
  #customerForm,
  #new-debt-form,
  #manual-ar-form,
  #ovb-form {
    display: grid;
    grid-template-columns: 1fr !important;
    gap: 12px !important;
  }

  #customerForm [style*="grid-column:span"],
  #new-debt-form [style*="grid-column:span"],
  #manual-ar-form [style*="grid-column:span"],
  #ovb-form [style*="grid-column:span"] {
    grid-column: auto / auto !important;
  }

  /* Tables/data-grids: horizontal scroll instead of squish */
  .modal-overlay .data-grid-wrapper,
  .modal-overlay .journal-table-wrap {
    overflow: auto;
    max-height: 56vh;
    -webkit-overflow-scrolling: touch;
  }

  .modal-overlay table {
    width: 100%;
    table-layout: fixed;
  }

  .modal-overlay table th,
  .modal-overlay table td {
    padding: 6px 8px;
    word-break: break-word;
  }
}

/* Respect RTL for modal internals when page is RTL */
html[dir="rtl"] .modal-overlay .modal-content {
  direction: rtl;
}

html[dir="rtl"] .modal-overlay .modal-header,
html[dir="rtl"] .modal-overlay .modal-footer {
  text-align: right;
}

/* Mobile stacked table pattern (no horizontal scrolling) */
@media (max-width: 768px) {
  @media (max-width: 768px) {
    table.stack-table {
      border-collapse: collapse;
    }

    table.stack-table thead {
      display: none;
    }

    table.stack-table tbody tr {
      display: grid;
      grid-template-columns: 1fr;
      gap: 6px;
      border: 1px solid hsl(var(--border));
      border-radius: 10px;
      padding: 8px;
      margin-bottom: 10px;
      background: hsl(var(--surface, var(--background)));
    }

    table.stack-table tbody tr td {
      display: grid;
      grid-template-columns: minmax(120px, 40%) 1fr;
      gap: 8px;
      align-items: start;
      padding: 6px 4px;
      border: 0;
    }

    table.stack-table tbody tr td::before {
      content: attr(data-label);
      font-weight: 600;
      color: hsl(var(--foreground));
      opacity: .8;
    }

    table.stack-table tfoot {
      display: none;
    }
  }

  /* ========================= 
     Universal Card Layout for Mobile Modals
     ========================= */

  /* Hide desktop tables on mobile */
  .data-grid-wrapper table.data-grid,
  #debt-table,
  #customers-table,
  #ovb-bills-table {
    display: none;
  }

  /* Show card layouts on mobile */
  .modal-cards-container {
    display: block;
    padding: .75rem;
    animation: fadeIn 0.3s ease-out;
  }

  @keyframes fadeIn {
    from {
      opacity: 0;
      transform: translateY(10px);
    }

    to {
      opacity: 1;
      transform: translateY(0);
    }
  }

  .modal-entry-card {
    background: linear-gradient(135deg, #1a2332 0%, #141a20 100%);
    border: 1px solid #2d3b46;
    border-radius: 16px;
    margin-bottom: 1rem;
    padding: 1.25rem;
    box-shadow: 0 4px 16px rgba(0, 0, 0, .4);
    position: relative;
    cursor: pointer;
    transition: all .3s cubic-bezier(0.4, 0, 0.2, 1);
    -webkit-tap-highlight-color: rgba(14, 165, 233, 0.1);
    overflow: hidden;
  }

  .modal-entry-card::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 3px;
    background: linear-gradient(90deg, #3b82f6, #10b981, #f59e0b);
    opacity: 0;
    transition: opacity 0.3s ease;
  }

  .modal-entry-card:hover::before,
  .modal-entry-card:active::before {
    opacity: 1;
  }

  .modal-entry-card:hover,
  .modal-entry-card:active {
    transform: translateY(-2px);
    box-shadow: 0 8px 24px rgba(0, 0, 0, .5);
    border-color: #3b82f6;
  }

  .modal-entry-card-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 1rem;
    gap: 0.75rem;
    /* Ensure consistent spacing */
  }

  .modal-entry-card-title {
    display: flex;
    flex-direction: column;
    gap: .25rem;
    flex: 1 1 0;
    /* Allow shrinking to prevent overflow */
    min-width: 0;
    /* Allow text to wrap/ellipsis */
  }

  .modal-entry-card-primary {
    font-size: .95rem;
    font-weight: 700;
    color: #e2e8f0;
    letter-spacing: .5px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .modal-entry-card-secondary {
    font-size: .75rem;
    color: #94a3b8;
    font-weight: 500;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .modal-entry-card-menu {
    width: 44px;
    height: 44px;
    background: rgba(59, 130, 246, 0.1);
    border: 1px solid rgba(59, 130, 246, 0.3);
    border-radius: 12px;
    color: #3b82f6;
    font-size: 1.2rem;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all .2s ease;
    touch-action: manipulation;
    flex-shrink: 0;
  }

  .modal-entry-card-menu:hover,
  .modal-entry-card-menu:active {
    background: rgba(59, 130, 246, 0.2);
    transform: scale(1.05);
  }

  .modal-entry-card-content {
    display: grid;
    gap: .75rem;
    margin-bottom: 1rem;
  }

  .modal-entry-card-row {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    padding: .5rem 0;
    border-bottom: 1px solid rgba(45, 59, 70, 0.5);
    gap: 1rem;
  }

  .modal-entry-card-row:last-child {
    border-bottom: none;
  }

  .modal-entry-card-label {
    font-size: .7rem;
    text-transform: uppercase;
    letter-spacing: .5px;
    color: #7dd3fc;
    font-weight: 600;
    min-width: 80px;
    flex-shrink: 0;
  }

  .modal-entry-card-value {
    font-size: .8rem;
    color: #e2e8f0;
    text-align: right;
    flex: 1;
    word-break: break-word;
    line-height: 1.4;
  }

  .modal-entry-card-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-top: .75rem;
    border-top: 1px solid #2d3b46;
    gap: 1rem;
  }

  .modal-entry-card-status {
    display: inline-flex;
    align-items: center;
    gap: .75rem;
    flex-wrap: wrap;
  }

  .modal-entry-card-amount {
    font-size: 1.1rem;
    font-weight: 700;
    color: #10b981;
    flex-shrink: 0;
  }

  /* Status badges optimized for cards */
  .modal-entry-card .status-badge {
    padding: .4rem .8rem;
    font-size: .7rem;
    border-radius: 20px;
    font-weight: 600;
    letter-spacing: .3px;
    display: inline-flex;
    align-items: center;
    gap: .35rem;
  }

  /* Specific status colors */
  .status-active {
    background: #064e3b;
    color: #34d399;
  }

  .status-paid_off,
  .status-paid {
    background: #064e3b;
    color: #34d399;
  }

  .status-unpaid {
    background: #7c2d12;
    color: #fbbf24;
  }

  .status-overdue {
    background: #7c2d12;
    color: #f87171;
  }

  .status-pending {
    background: #1e3a8a;
    color: #60a5fa;
  }

  .status-partially_paid {
    background: #7c2d12;
    color: #fbbf24;
  }

  .status-draft {
    background: #374151;
    color: #cbd5e1;
  }

  /* Special styling for paid items */
  .modal-entry-card.card-paid {
    opacity: 0.75;
    filter: grayscale(0.2);
  }

  .modal-entry-card.card-paid .modal-entry-card-primary {
    text-decoration: line-through;
    opacity: 0.8;
  }

  /* Empty state for cards */
  .modal-cards-empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 3rem 1.5rem;
    text-align: center;
    color: #64748b;
    min-height: 300px;
  }

  .modal-cards-empty svg {
    width: 64px;
    height: 64px;
    margin-bottom: 1.5rem;
    opacity: 0.4;
    color: #475569;
  }

  .modal-cards-empty h3 {
    font-size: 1.2rem;
    margin-bottom: .75rem;
    color: #94a3b8;
    font-weight: 600;
  }

  .modal-cards-empty p {
    font-size: .9rem;
    line-height: 1.6;
    max-width: 320px;
    margin-bottom: 1rem;
  }
}

/* Desktop: Hide cards, show tables */
@media (min-width: 769px) {
  .modal-cards-container {
    display: none;
  }
}

/* Loading States */
.lazy-loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 2rem;
  color: hsl(var(--foreground-muted));
}

.loading-spinner {
  width: 2rem;
  height: 2rem;
  border: 2px solid hsl(var(--border));
  border-top: 2px solid hsl(var(--primary));
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 1rem;
}

.loading-text {
  font-size: 0.875rem;
  margin: 0;
}

.lazy-error {
  text-align: center;
  padding: 2rem;
  color: hsl(var(--foreground-muted));
}

.lazy-error button {
  background: hsl(var(--primary));
  color: white;
  border: none;
  padding: 0.5rem 1rem;
  border-radius: 0.25rem;
  cursor: pointer;
  margin-top: 1rem;
}

/* App Version */
.app-version {
  position: fixed;
  bottom: 10px;
  right: 10px;
  font-size: 0.75rem;
  color: hsl(var(--foreground-muted));
  z-index: 1000;
  pointer-events: none;
  opacity: 0.7;
}

/* Accessibility Enhancements */
.focus-visible {
  outline: 2px solid hsl(var(--primary)) !important;
  outline-offset: 2px !important;
}

.sr-live-region {
  position: absolute;
  left: -10000px;
  top: auto;
  width: 1px;
  height: 1px;
  overflow: hidden;
}

.accessibility-mode {
  --primary: hsl(220, 100%, 40%);
  --background: hsl(0, 0%, 100%);
  --foreground: hsl(0, 0%, 0%);
  --border: hsl(0, 0%, 20%);
}

.accessibility-mode * {
  transition: none !important;
  animation: none !important;
}

.no-animations * {
  transition: none !important;
  animation: none !important;
}

.compact-mode {
  --spacing: 0.5rem;
  --padding: 0.75rem;
}

.compact-mode .kpi-card,
.compact-mode .dashboard-widget,
.compact-mode .template-card {
  padding: var(--padding);
}

/* Animations */
@keyframes fadeIn {
  from {
    opacity: 0;
  }

  to {
    opacity: 1;
  }
}

@keyframes slideIn {
  from {
    transform: translateY(-20px);
    opacity: 0;
  }

  to {
    transform: translateY(0);
    opacity: 1;
  }
}

@keyframes spin {
  from {
    transform: rotate(0deg);
  }

  to {
    transform: rotate(360deg);
  }
}

/* Responsive Design */
@media (max-width: 768px) {
  .notification-container {
    left: max(12px, env(safe-area-inset-left));
    right: max(12px, env(safe-area-inset-right));
    max-width: none;
  }

  .help-modal-content,
  .shortcuts-modal-content,
  .preferences-modal-content,
  .search-modal-content {
    width: 95%;
    margin: 1rem;
  }

  .help-float {
    bottom: 1rem;
    right: 1rem;
    width: 2.5rem;
    height: 2.5rem;
  }

  .tour-step {
    max-width: 250px;
    padding: 0.75rem;
  }

  .app-version {
    display: none;
  }
}

/* Highest-specificity RTL mobile override: ensure hamburger at far right */
@media (max-width: 768px) {
  html.rtl[dir="rtl"] body.rtl .topbar {
    display: flex;
  }

  html.rtl[dir="rtl"] body.rtl .topbar-left {
    order: 999 !important;
    display: flex !important;
    flex-direction: row !important;
    justify-content: flex-end !important;
    align-items: center !important;
    margin: 0 !important;
  }

  html.rtl[dir="rtl"] body.rtl .topbar-left .brand {
    order: 1 !important;
  }

  html.rtl[dir="rtl"] body.rtl .topbar-left .sidebar-toggle {
    order: 2 !important;
  }
}

/* Definitive RTL small-screen layout override: mirror topbar cleanly */
@media (max-width: 1024px) {
  html[dir="rtl"] .topbar {
    display: flex !important;
    flex-direction: row-reverse !important;
    /* put left cluster to the right edge */
  }

  /* Ensure three sections are ordered: left cluster (right edge) = 3, search = 2, right cluster (left edge) = 1 */
  html[dir="rtl"] .topbar-left {
    order: 3 !important;
    display: flex !important;
    flex-direction: row !important;
    justify-content: flex-end !important;
  }

  html[dir="rtl"] .topbar-search-mobile {
    order: 2 !important;
  }

  html[dir="rtl"] .topbar-right {
    order: 1 !important;
    display: flex !important;
    flex-direction: row !important;
    justify-content: flex-start !important;
  }

  /* Inside the left cluster, keep brand then hamburger so the hamburger sits at the extreme right */
  html[dir="rtl"] .topbar-left .brand {
    order: 1 !important;
  }

  html[dir="rtl"] .topbar-left .sidebar-toggle {
    order: 2 !important;
  }
}

/* Ultimate fallback: cover all RTL class variants with strong !important rules */
@media (max-width: 1024px) {

  body.rtl .topbar,
  html.rtl .topbar,
  .rtl .topbar {
    display: flex !important;
    flex-direction: row-reverse !important;
  }

  body.rtl .topbar-left,
  html.rtl .topbar-left,
  .rtl .topbar-left {
    order: 3 !important;
    display: flex !important;
    flex-direction: row !important;
    justify-content: flex-end !important;
    margin: 0 !important;
  }

  body.rtl .topbar-search-mobile,
  html.rtl .topbar-search-mobile,
  .rtl .topbar-search-mobile {
    order: 2 !important;
    display: flex !important;
  }

  body.rtl .topbar-right,
  html.rtl .topbar-right,
  .rtl .topbar-right {
    order: 1 !important;
    display: flex !important;
    flex-direction: row !important;
    justify-content: flex-start !important;
  }

  body.rtl .topbar-left .brand,
  html.rtl .topbar-left .brand,
  .rtl .topbar-left .brand {
    order: 1 !important;
  }

  body.rtl .topbar-left .sidebar-toggle,
  html.rtl .topbar-left .sidebar-toggle,
  .rtl .topbar-left .sidebar-toggle {
    order: 2 !important;
  }
}

/* Ensure desktop never shows compact search */
@media (min-width: 1025px) {
  .topbar-search-mobile {
    display: none !important;
  }
}

/* Final RTL small-screen mirror with high specificity to beat earlier rules */
@media (max-width: 1024px) {

  /* Use both dir=rtl and .rtl/body.rtl/html.rtl to cover all class patterns */
  html[dir="rtl"] .topbar,
  body.rtl .topbar,
  html.rtl .topbar,
  .rtl .topbar {
    display: flex !important;
    flex-direction: row !important;
    /* do not reverse container, rely on order */
    justify-content: space-between !important;
    align-items: center !important;
  }

  /* Cluster order: left cluster (becomes right edge) = 3, search center = 2, right cluster (becomes left edge) = 1 */
  html[dir="rtl"] .topbar-right,
  body.rtl .topbar-right,
  html.rtl .topbar-right,
  .rtl .topbar-right {
    order: 1 !important;
    display: flex !important;
  }

  html[dir="rtl"] .topbar-search-mobile,
  body.rtl .topbar-search-mobile,
  html.rtl .topbar-search-mobile,
  .rtl .topbar-search-mobile {
    order: 2 !important;
    display: flex !important;
    flex: 1 1 auto !important;
  }

  html[dir="rtl"] .search-container,
  body.rtl .search-container,
  html.rtl .search-container,
  .rtl .search-container {
    display: none !important;
  }

  html[dir="rtl"] .topbar-left,
  body.rtl .topbar-left,
  html.rtl .topbar-left,
  .rtl .topbar-left {
    order: 3 !important;
    display: flex !important;
    /* Force LTR direction within this cluster so flex alignment maps to visual right */
    direction: ltr !important;
    flex-direction: row !important;
    justify-content: flex-end !important;
    /* with direction:ltr this pins items to the right edge */
    flex: 0 0 auto !important;
  }

  /* Inside the right-edge cluster: D then hamburger so hamburger is at the extreme right */
  html[dir="rtl"] .topbar-left .brand,
  body.rtl .topbar-left .brand,
  html.rtl .topbar-left .brand,
  .rtl .topbar-left .brand {
    order: 1 !important;
  }

  html[dir="rtl"] .topbar-left .sidebar-toggle,
  body.rtl .topbar-left .sidebar-toggle,
  html.rtl .topbar-left .sidebar-toggle,
  .rtl .topbar-left .sidebar-toggle {
    order: 2 !important;
  }
}