/* ==========================================================================
   Integration Dashboard — Custom Styles
   Supplements MRStyleSheet CDN (mr-all.bundle.css).
   ========================================================================== */

/* --------------------------------------------------------------------------
   1. CSS Variables / Tokens
   -------------------------------------------------------------------------- */
:root {
  --sidebar-width: 260px;
  --sidebar-bg: var(--mr-color-dark-blue, #15273B);
  --sidebar-nav-hover: rgba(255, 255, 255, 0.08);
  --sidebar-nav-active-bg: rgba(255, 255, 255, 0.12);
  --sidebar-border-accent: var(--mr-color-primary, #0079ae);
  --content-padding: 28px;
  --kpi-gap: 18px;
  --radius-card: var(--mr-radius-md, 8px);
  --shadow-card: 0 1px 4px rgba(0, 0, 0, 0.08);
  --transition-fast: 0.18s ease;
}

/* --------------------------------------------------------------------------
   2. App Shell Layout
   -------------------------------------------------------------------------- */
*, *::before, *::after {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  min-height: 100vh;
  overflow-x: hidden;
  font-family: var(--mr-font-family, "Segoe UI", system-ui, -apple-system, sans-serif);
  background: var(--mr-surface-body, #f4f6f8);
  color: var(--mr-color-text, #1e293b);
}

.app-shell {
  display: flex;
  min-height: 100vh;
}

/* --------------------------------------------------------------------------
   3. Sidebar
   -------------------------------------------------------------------------- */
.sidebar {
  position: fixed;
  top: 0;
  left: 0;
  width: var(--sidebar-width);
  height: 100vh;
  background: var(--sidebar-bg);
  display: flex;
  flex-direction: column;
  z-index: 100;
  overflow-y: auto;
  transition: transform var(--transition-fast);
}

.sidebar-logo {
  display: block;
  max-width: 180px;
  padding: 24px 20px 8px;
  margin: 0 auto;
}

.sidebar-title {
  color: #ffffff;
  font-weight: 700;
  font-size: 1.05rem;
  text-align: center;
  padding: 4px 20px 20px;
  margin: 0;
  letter-spacing: 0.02em;
}

.sidebar-nav {
  flex: 1;
  padding: 0;
  margin: 0;
  list-style: none;
}

.nav-link {
  display: block;
  padding: 12px 24px;
  color: rgba(255, 255, 255, 0.82);
  text-decoration: none;
  font-size: 0.94rem;
  border-left: 3px solid transparent;
  transition: background var(--transition-fast), border-color var(--transition-fast), color var(--transition-fast);
}

.nav-link:hover {
  background: var(--sidebar-nav-hover);
  color: #ffffff;
  border-left-color: rgba(255, 255, 255, 0.3);
}

.nav-link.active {
  background: var(--sidebar-nav-active-bg);
  color: #ffffff;
  border-left-color: var(--sidebar-border-accent);
  font-weight: 600;
}

.nav-link .nav-icon {
  margin-right: 10px;
  width: 1.2em;
  text-align: center;
  display: inline-block;
}

.nav-link i[class^="ri-"] {
  margin-right: 8px;
  width: 1.2em;
  display: inline-block;
  text-align: center;
}

.sidebar-footer {
  margin-top: auto;
  padding: 16px 20px;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
}

.sidebar-user-card {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 14px;
}

.sidebar-user-avatar {
  flex-shrink: 0;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.15);
  color: #ffffff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 0.85rem;
  text-transform: uppercase;
  border: 1px solid rgba(255, 255, 255, 0.25);
}

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

.sidebar-user-name {
  color: #ffffff;
  font-size: 0.82rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sidebar-user-email {
  color: rgba(255, 255, 255, 0.55);
  font-size: 0.72rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sidebar-footer-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.sidebar-signout-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  border: 1px solid rgba(255, 255, 255, 0.25);
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.08);
  color: rgba(255, 255, 255, 0.85);
  font-size: 0.78rem;
  font-weight: 500;
  cursor: pointer;
  transition: background var(--transition-fast), border-color var(--transition-fast);
  white-space: nowrap;
}

.sidebar-signout-btn:hover {
  background: rgba(255, 255, 255, 0.16);
  border-color: rgba(255, 255, 255, 0.45);
  color: #ffffff;
}

/* --------------------------------------------------------------------------
   4. Main Content
   -------------------------------------------------------------------------- */
.main-content {
  margin-left: var(--sidebar-width);
  padding: var(--content-padding);
  overflow-y: auto;
  height: 100vh;
  flex: 1;
  width: calc(100% - var(--sidebar-width));
}

.page-header {
  margin-bottom: 24px;
}

.page-title {
  font-size: 1.5rem;
  font-weight: 700;
  margin: 0 0 4px;
}

/* --------------------------------------------------------------------------
   5. KPI Stat Cards
   -------------------------------------------------------------------------- */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: var(--kpi-gap);
  margin-bottom: 28px;
}

.kpi-card {
  background: var(--mr-surface-card, #ffffff);
  border-radius: var(--radius-card);
  box-shadow: var(--shadow-card);
  padding: 20px 24px;
  display: flex;
  flex-direction: column;
  transition: box-shadow var(--transition-fast);
}

.kpi-card:hover {
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.1);
}

.kpi-icon {
  font-size: 1.5rem;
  margin-bottom: 8px;
  color: var(--mr-color-primary, #0079ae);
}

.kpi-value {
  font-size: 2.2rem;
  font-weight: 700;
  color: var(--mr-color-primary, #0079ae);
  line-height: 1.15;
}

.kpi-label {
  font-size: 0.85rem;
  color: var(--mr-color-text-secondary, #64748b);
  margin-top: 4px;
  font-weight: 500;
}

/* --------------------------------------------------------------------------
   6. Status Badge Colors
   -------------------------------------------------------------------------- */
.badge-pending {
  background: #fef3c7;
  color: #92400e;
}

.badge-processing,
.badge-loadingconfig,
.badge-authenticating,
.badge-fetchingsource {
  background: #dbeafe;
  color: #1e40af;
}

.badge-completed {
  background: #d1fae5;
  color: #065f46;
}

.badge-error {
  background: #fee2e2;
  color: #991b1b;
}

.badge-retry {
  background: #fef3c7;
  color: #92400e;
}

.badge-skipped {
  background: #f1f5f9;
  color: #64748b;
}

/* --------------------------------------------------------------------------
   7. Flow Arrows (Sync Pairs)
   -------------------------------------------------------------------------- */
.flow-arrow {
  display: flex;
  align-items: center;
  gap: 10px;
}

.flow-source,
.flow-target {
  font-weight: 600;
  white-space: nowrap;
}

.flow-arrow-icon {
  font-size: 1.3rem;
  color: var(--mr-color-primary, #0079ae);
  line-height: 1;
  flex-shrink: 0;
}

/* --------------------------------------------------------------------------
   7b. Sync Pairs Cards
   -------------------------------------------------------------------------- */
.pairs-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 18px;
}

.pair-card {
  cursor: pointer;
  padding: 20px 24px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  transition: box-shadow var(--transition-fast), opacity var(--transition-fast);
}

.pair-card:hover {
  box-shadow: 0 3px 12px rgba(0, 0, 0, 0.12);
}

.pair-card:focus-visible {
  outline: 2px solid var(--mr-color-primary, #0079ae);
  outline-offset: 2px;
}

.pair-card.pair-inactive {
  opacity: 0.55;
}

tr.row-inactive td {
  opacity: 0.45;
}

tr.row-inactive .ep-active-toggle {
  opacity: 1;
}

.pair-card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}

.pair-card-name {
  font-weight: 600;
  font-size: 0.92rem;
  color: var(--mr-color-text-secondary, #64748b);
}

.pair-flow-servers .flow-source,
.pair-flow-servers .flow-target {
  font-size: 1.1rem;
}

.pair-flow-servers .flow-source {
  color: var(--mr-color-primary, #0079ae);
}

.pair-flow-servers .flow-target {
  color: var(--mr-color-gold, #d4a017);
}

.pair-flow-endpoints {
  font-size: 0.85rem;
}

.pair-flow-endpoints .flow-source,
.pair-flow-endpoints .flow-target {
  font-weight: 500;
  color: var(--mr-color-text-secondary, #64748b);
}

.pair-flow-endpoints .flow-arrow-icon {
  font-size: 1rem;
}

.pair-card-footer {
  font-size: 0.82rem;
  margin-top: auto;
  padding-top: 4px;
  border-top: 1px solid rgba(0, 0, 0, 0.06);
}

/* --------------------------------------------------------------------------
   8. Loading Overlay
   -------------------------------------------------------------------------- */
.loading-overlay {
  position: fixed;
  inset: 0;
  background: rgba(255, 255, 255, 0.65);
  z-index: 9000;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: opacity var(--transition-fast);
}

.loading-overlay .spinner-area {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

/* --------------------------------------------------------------------------
   9. Table Enhancements
   -------------------------------------------------------------------------- */
.clickable-row {
  cursor: pointer;
}

.clickable-row:hover td {
  background: var(--mr-surface-raised, rgba(0, 121, 174, 0.04));
}

.expandable-row td {
  padding: 0;
  border-top: none;
}

.expandable-row .expand-content {
  padding: 12px 20px;
  background: var(--mr-surface-body, #f8fafc);
}

/* --------------------------------------------------------------------------
   10. Modal Enhancements
   -------------------------------------------------------------------------- */
.json-display {
  background: var(--mr-surface-body, #f1f5f9);
  padding: 16px;
  border-radius: var(--radius-card);
  overflow: auto;
  max-height: 400px;
  font-family: "Cascadia Code", "Fira Code", "Consolas", monospace;
  font-size: 0.85rem;
  line-height: 1.55;
  white-space: pre-wrap;
  word-break: break-word;
}

.error-highlight {
  color: var(--mr-color-danger, #dc2626);
  background: #fef2f2;
  padding: 8px 12px;
  border-radius: 4px;
  border-left: 3px solid var(--mr-color-danger, #dc2626);
  word-break: break-word;
  overflow-wrap: break-word;
}

.mr-modal .mr-card-body {
  word-break: break-word;
  overflow-wrap: break-word;
}

/* --------------------------------------------------------------------------
   11. Filter Bar
   -------------------------------------------------------------------------- */
.filter-bar {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 20px;
}

.filter-bar .mr-select,
.filter-bar .mr-input,
.filter-bar select,
.filter-bar input {
  min-width: 160px;
}

/* --------------------------------------------------------------------------
   12. Dark Theme
   -------------------------------------------------------------------------- */
[data-theme="dark"] {
  --sidebar-bg: #0f1e2e;
  --sidebar-nav-hover: rgba(255, 255, 255, 0.1);
  --sidebar-nav-active-bg: rgba(255, 255, 255, 0.15);
  --shadow-card: 0 1px 4px rgba(0, 0, 0, 0.3);
}

[data-theme="dark"] body,
[data-theme="dark"] .main-content {
  background: var(--mr-surface-body, #0f172a);
  color: var(--mr-color-text, #e2e8f0);
}

[data-theme="dark"] .kpi-card {
  background: var(--mr-surface-card, #1e293b);
  border: 1px solid rgba(255, 255, 255, 0.08);
}

[data-theme="dark"] .kpi-label {
  color: var(--mr-color-text-secondary, #94a3b8);
}

[data-theme="dark"] .loading-overlay {
  background: rgba(15, 23, 42, 0.7);
}

[data-theme="dark"] .json-display {
  background: var(--mr-surface-body, #0f172a);
  color: #e2e8f0;
}

[data-theme="dark"] .error-highlight {
  background: rgba(220, 38, 38, 0.15);
}

[data-theme="dark"] .expandable-row .expand-content {
  background: var(--mr-surface-body, #0f172a);
}

[data-theme="dark"] .badge-pending {
  background: rgba(254, 243, 199, 0.15);
  color: #fbbf24;
}

[data-theme="dark"] .badge-processing,
[data-theme="dark"] .badge-loadingconfig,
[data-theme="dark"] .badge-authenticating,
[data-theme="dark"] .badge-fetchingsource {
  background: rgba(59, 130, 246, 0.15);
  color: #60a5fa;
}

[data-theme="dark"] .badge-completed {
  background: rgba(16, 185, 129, 0.15);
  color: #34d399;
}

[data-theme="dark"] .badge-error {
  background: rgba(239, 68, 68, 0.15);
  color: #f87171;
}

[data-theme="dark"] .badge-retry {
  background: rgba(254, 243, 199, 0.15);
  color: #fbbf24;
}

[data-theme="dark"] .badge-skipped {
  background: rgba(148, 163, 184, 0.12);
  color: #94a3b8;
}

[data-theme="dark"] .clickable-row:hover td {
  background: rgba(255, 255, 255, 0.04);
}

[data-theme="dark"] .pair-card-footer {
  border-top-color: rgba(255, 255, 255, 0.08);
}

[data-theme="dark"] .pair-card-name {
  color: var(--mr-color-text-secondary, #94a3b8);
}

/* --------------------------------------------------------------------------
   13. Responsive Breakpoints
   -------------------------------------------------------------------------- */

/* Mobile: sidebar collapses, single-column layout */
@media (max-width: 768px) {
  .sidebar {
    transform: translateX(-100%);
  }

  .sidebar.sidebar-open {
    transform: translateX(0);
  }

  .main-content {
    margin-left: 0;
    width: 100%;
    padding: 16px;
  }

  .kpi-grid {
    grid-template-columns: 1fr;
  }

  .pairs-grid {
    grid-template-columns: 1fr;
  }

  .filter-bar {
    flex-direction: column;
    align-items: stretch;
  }

  .filter-bar .mr-select,
  .filter-bar .mr-input,
  .filter-bar select,
  .filter-bar input {
    min-width: unset;
    width: 100%;
  }

  .mobile-menu-toggle {
    display: flex;
    align-items: center;
    justify-content: center;
    position: fixed;
    top: 12px;
    left: 12px;
    z-index: 150;
    width: 40px;
    height: 40px;
    border-radius: 8px;
    background: var(--sidebar-bg);
    color: #ffffff;
    border: none;
    cursor: pointer;
    font-size: 1.25rem;
  }

  .sidebar-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.4);
    z-index: 99;
  }
}

@media (min-width: 769px) {
  .mobile-menu-toggle {
    display: none;
  }

  .sidebar-backdrop {
    display: none;
  }
}

/* Tablet: 2-column grids */
@media (min-width: 769px) and (max-width: 1024px) {
  .kpi-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

/* Desktop: full layout — auto-fit handles 5+ cards gracefully */
@media (min-width: 1025px) {
  .kpi-grid {
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  }
}

/* --------------------------------------------------------------------------
   14. Utility Classes
   -------------------------------------------------------------------------- */
.text-muted {
  color: var(--mr-color-text-secondary, #64748b);
}

.text-danger {
  color: var(--mr-color-danger, #dc2626);
}

.text-success {
  color: var(--mr-color-success, #16a34a);
}

.text-warning {
  color: #d97706;
}

.mb-1 { margin-bottom: 8px; }
.mb-2 { margin-bottom: 16px; }
.mt-1 { margin-top: 8px; }
.mt-2 { margin-top: 16px; }

.hidden {
  display: none;
}

/* --------------------------------------------------------------------------
   15. Collapsible Cards (Endpoints view)
   -------------------------------------------------------------------------- */
.endpoint-server-card {
  margin-bottom: 16px;
}

.endpoint-card-toggle {
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  align-items: center;
  user-select: none;
}

.endpoint-card-toggle:hover {
  background: var(--mr-surface-raised, rgba(0, 121, 174, 0.04));
}

.endpoint-card-toggle:focus-visible {
  outline: 2px solid var(--mr-color-primary, #0079ae);
  outline-offset: -2px;
}

.toggle-icon {
  font-size: 1rem;
  color: var(--mr-color-text-secondary, #64748b);
  transition: transform var(--transition-fast);
}

.mr-card-body.collapsed {
  display: none;
}

/* --------------------------------------------------------------------------
   16. Endpoint Active Toggle Switch
   -------------------------------------------------------------------------- */
.ep-active-toggle {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  user-select: none;
}

.ep-active-toggle input[type="checkbox"] {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}

.ep-active-slider {
  position: relative;
  display: inline-block;
  width: 36px;
  height: 20px;
  background: var(--mr-color-danger, #dc2626);
  border-radius: 20px;
  transition: background 0.2s ease;
  flex-shrink: 0;
}

.ep-active-slider::after {
  content: '';
  position: absolute;
  top: 3px;
  left: 3px;
  width: 14px;
  height: 14px;
  background: #fff;
  border-radius: 50%;
  transition: transform 0.2s ease;
}

.ep-active-toggle input:checked + .ep-active-slider {
  background: var(--mr-color-success, #16a34a);
}

.ep-active-toggle input:checked + .ep-active-slider::after {
  transform: translateX(16px);
}

.ep-active-toggle input:disabled + .ep-active-slider {
  opacity: 0.5;
  cursor: not-allowed;
}

.ep-active-label {
  font-size: 0.8125rem;
  color: var(--mr-color-text-secondary, #64748b);
  min-width: 44px;
}

/* --------------------------------------------------------------------------
   17. Toast Notifications
   -------------------------------------------------------------------------- */
#toast-container {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 9999;
  display: flex;
  flex-direction: column;
  gap: 10px;
  pointer-events: none;
}

.mr-toast {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 18px;
  border-radius: 8px;
  font-size: 0.875rem;
  font-weight: 500;
  color: #fff;
  box-shadow: 0 4px 16px rgba(0,0,0,0.18);
  opacity: 0;
  transform: translateY(12px);
  transition: opacity 0.25s ease, transform 0.25s ease;
  pointer-events: auto;
}

.mr-toast-visible {
  opacity: 1;
  transform: translateY(0);
}

.mr-toast-success {
  background: var(--mr-color-success, #16a34a);
}

.mr-toast-error {
  background: var(--mr-color-danger, #dc2626);
}

.mr-toast i {
  font-size: 1rem;
  flex-shrink: 0;
}
