/* ==========================================================================
   المنصة الوطنية للربط والتشغيل الصحي البيني (Saudi Interoperability Engine)
   Borderless High-Contrast Theme System with Inverted Contrast Sidebar
   - In Dark Mode: Harmonized Graphite Sidebar (#333338, secondary-container tone) on Dark Canvas (#0C0C0E)
   - In Light Mode: Deep Executive Dark Sidebar (#141519) on Light Canvas (#D9DEE7)
   - Complete borderless flat architecture with crystal-clear tonal contrast
   - Sharp 0px geometry & clean modern aesthetics
   ========================================================================== */

:root,
[data-theme="dark"] {
  /* Dark Tonal Surface Hierarchy — deepened canvas, lifted cards for stronger separation */
  --m3-surface: #0C0C0E;                   /* Base Canvas Ground */
  --m3-surface-dim: #060607;               /* Deepest Canvas */
  --m3-surface-container-low: #151517;     /* Inner Wells & Nested Data Boxes */
  --m3-surface-container: #2B2B30;         /* Official Cards / Grid Containers */
  --m3-surface-container-high: #36363D;    /* Headers / Table Headers / Active */
  --m3-surface-container-highest: #43434B; /* Popovers / Hover State / Secondary BTN */

  /* Inverted Sidebar in Dark Mode (Harmonized #333338 = secondary-container tone: distinct from cards #2B2B30, floats off canvas #0C0C0E) */
  --sidebar-bg: #333338;
  --sidebar-item-bg: #333338;
  --sidebar-item-hover: #45454E;
  --sidebar-item-active: #54545E;
  --sidebar-icon-wrap-bg: #45454E;
  --sidebar-well: #232326;
  --sidebar-on-surface: #FFFFFF;
  --sidebar-on-surface-variant: #D1D1D6;
  --sidebar-on-surface-muted: #9E9EA4;

  /* Zero Outlines in Borderless Mode */
  --m3-outline: transparent;
  --m3-outline-variant: transparent;
  --m3-outline-focus: transparent;

  /* Crisp Formal Typography Colors (muted lifted for contrast on tonal cards) */
  --m3-on-surface: #FFFFFF;
  --m3-on-surface-variant: #A3A3A3;
  --m3-on-surface-muted: #8E8E93;

  /* Executive Accents (Formal Forest Green) */
  --m3-primary: #059669;
  --m3-primary-light: #10B981;
  --m3-on-primary: #FFFFFF;
  --m3-primary-container: #183325;
  --m3-on-primary-container: #A7F3D0;

  /* Neutral Steel Gray */
  --m3-secondary: #71717A;
  --m3-on-secondary: #FFFFFF;
  --m3-secondary-container: #333338;
  --m3-on-secondary-container: #E4E4E7;

  /* Clinical Purple */
  --m3-tertiary: #8B5CF6;
  --m3-on-tertiary: #FFFFFF;
  --m3-tertiary-container: #332540;
  --m3-on-tertiary-container: #E9D5FF;

  /* Warning / Claims */
  --m3-warning: #D97706;
  --m3-on-warning: #FFFFFF;
  --m3-warning-container: #3D2E1A;
  --m3-on-warning-container: #FDE68A;

  /* Error Roles */
  --m3-error: #DC2626;
  --m3-on-error: #FFFFFF;
  --m3-error-container: #3D1C1C;
  /* Bright danger text for the always-dark sidebar (fixed both themes for contrast) */
  --m3-error-bright: #F87171;
  --m3-on-error-container: #FECACA;

  /* Sharp Geometry & Dynamic Dimensions */
  --sidebar-width: 280px;
  --sidebar-collapsed-width: 72px;
  /* Radius scale — soft hierarchy: surfaces round, controls soften, tags are pills */
  --radius-sharp: 0px;
  --radius-xs: 3px;
  --radius-sm: 6px;
  --radius-md: 8px;
  --radius-lg: 12px;
  --radius-xl: 16px;
  --radius-pill: 999px;

  /* Motion scale — quiet tempo, layout-safe properties only */
  --motion-instant: 0.12s;
  --motion-fast: 0.15s;
  --motion-base: 0.2s;
  --motion-slow: 0.24s;
  --motion-standard: cubic-bezier(0.4, 0, 0.2, 1);
}

/* =========================================
   LIGHT THEME VARIABLES (WITH DARK CONTRAST SIDEBAR)
   ========================================= */
body.light-theme,
[data-theme="light"] {
  /* Light Tonal Surface Hierarchy — deepened canvas so white cards pop harder */
  --m3-surface: #D9DEE7;                   /* Deepened Soft Gray Canvas for Maximum Pop */
  --m3-surface-dim: #C9CFD9;               /* Subtle Canvas Well */
  --m3-surface-container-low: #E7EAF0;     /* Inner Wells & Nested Data Boxes */
  --m3-surface-container: #FFFFFF;         /* Solid White Cards (Pops off #D9DEE7) */
  --m3-surface-container-high: #EEF0F4;    /* Headers / Table Headers / Active */
  --m3-surface-container-highest: #DCE1E9; /* Popovers / Hover State / Secondary BTN */

  /* Inverted Sidebar in Light Mode (Deepened #141519, lifted hover/active for clearer selection) */
  --sidebar-bg: #141519;
  --sidebar-item-bg: #141519;
  --sidebar-item-hover: #26272F;
  --sidebar-item-active: #31333C;
  --sidebar-icon-wrap-bg: #26272F;
  --sidebar-well: #1C1D23;
  --sidebar-on-surface: #FFFFFF;
  --sidebar-on-surface-variant: #C4C4C8;
  --sidebar-on-surface-muted: #888890;

  /* Zero Outlines */
  --m3-outline: transparent;
  --m3-outline-variant: transparent;
  --m3-outline-focus: transparent;

  /* High-Legibility Dark Typography */
  --m3-on-surface: #111827;
  --m3-on-surface-variant: #4B5563;
  --m3-on-surface-muted: #6B7280;

  /* Executive Primary Roles (Saudi Forest Green) */
  --m3-primary: #059669;
  --m3-primary-light: #047857;
  --m3-on-primary: #FFFFFF;
  --m3-primary-container: #DCFCE7;
  --m3-on-primary-container: #14532D;

  /* Secondary Roles (Technical Slate) */
  --m3-secondary: #475569;
  --m3-on-secondary: #FFFFFF;
  --m3-secondary-container: #E2E8F0;
  --m3-on-secondary-container: #1E293B;

  /* Tertiary Roles (Clinical Purple) */
  --m3-tertiary: #7C3AED;
  --m3-on-tertiary: #FFFFFF;
  --m3-tertiary-container: #EDE9FE;
  --m3-on-tertiary-container: #4C1D95;

  /* Warning / Claims */
  --m3-warning: #D97706;
  --m3-on-warning: #FFFFFF;
  --m3-warning-container: #FEF3C7;
  --m3-on-warning-container: #92400E;

  /* Error Roles */
  --m3-error: #DC2626;
  --m3-on-error: #FFFFFF;
  --m3-error-container: #FEE2E2;
  /* Bright danger text for the always-dark sidebar (fixed both themes for contrast) */
  --m3-error-bright: #F87171;
  --m3-on-error-container: #7F1D1D;
}

/* Native controls (date pickers, scrollbars, form widgets) follow the active theme */
html[data-theme="dark"] { color-scheme: dark; }
html[data-theme="light"] { color-scheme: light; }

/* =========================================
   BASE RESET & TYPOGRAPHY
   ========================================= */
*, *::before, *::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  border: none !important; /* Complete elimination of element borders */
}

html {
  font-size: 14.5px;
  background-color: var(--m3-surface);
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  font-family: 'IBM Plex Sans Arabic', 'Tajawal', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background-color: var(--m3-surface);
  color: var(--m3-on-surface);
  min-height: 100vh;
  min-height: -webkit-fill-available;
  line-height: 1.6;
  overflow-x: hidden;
  position: relative;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  -webkit-tap-highlight-color: transparent;
  transition: background-color var(--motion-base) ease, color var(--motion-base) ease;
}

/* Safari: prevent iOS zoom on input focus and fix fill-available */
@supports (-webkit-touch-callout: none) {
  body {
    min-height: -webkit-fill-available;
  }
}

/* Safari: reset native form controls that ignore border:none */
input, textarea, select, button {
  -webkit-appearance: none;
  appearance: none;
  border-radius: 0;
  font-family: inherit;
}
input[type="search"] {
  -webkit-appearance: none;
  appearance: none;
}
select {
  background-color: var(--m3-surface-container-low);
}
/* Keep native checkbox/radio if any */
input[type="checkbox"], input[type="radio"] {
  -webkit-appearance: auto;
  appearance: auto;
}

/* Safari SVG fix: prevent flexbox from stretching SVGs */
svg {
  overflow: hidden;
  flex-shrink: 0;
  -webkit-flex-shrink: 0;
  display: block;
  max-width: 100%;
}
svg:not([width]):not([height]) {
  /* Safari fallback when CSS width/height fails */
  width: 1em;
  height: 1em;
}

/* Scrollbar - Minimal Flat & Borderless */
::-webkit-scrollbar {
  width: 5px;
  height: 5px;
}

::-webkit-scrollbar-track {
  background: var(--m3-surface-dim);
}

::-webkit-scrollbar-thumb {
  background: var(--m3-surface-container-highest);
  border-radius: var(--radius-pill);
}

::-webkit-scrollbar-thumb:hover {
  background: var(--m3-secondary);
}

/* Code & Pre Formatting */
code, .mono-code {
  font-family: 'JetBrains Mono', monospace;
  direction: ltr;
  display: inline-block;
  font-size: 0.84rem;
}

pre {
  font-family: 'JetBrains Mono', monospace;
  direction: ltr !important;
  text-align: left !important;
  unicode-bidi: isolate;
}

.inline-svg {
  display: inline-block;
  vertical-align: middle;
}

.spin {
  animation: spin 0.9s linear infinite;
}

@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

/* =========================================
   APPLICATION LAYOUT
   ========================================= */
.app-layout {
  display: -webkit-box;
  display: -webkit-flex;
  display: flex;
  min-height: 100vh;
  min-height: -webkit-fill-available;
  min-height: 100dvh;
}
@supports (-webkit-touch-callout: none) {
  .app-layout {
    min-height: -webkit-fill-available;
  }
}

/* =========================================
   INVERTED CONTRAST SIDEBAR
   ========================================= */
.sidebar {
  width: var(--sidebar-width);
  background-color: var(--sidebar-bg);
  padding: 20px 12px;
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 12px;
  bottom: 12px;
  right: 12px;
  border-radius: var(--radius-xl);
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.30);
  z-index: 100;
  transition: width var(--motion-base) var(--motion-standard), padding var(--motion-base) ease, background-color var(--motion-base) ease;
  gap: 16px;
}

body.light-theme .sidebar,
[data-theme="light"] .sidebar {
  box-shadow: 0 12px 32px rgba(15, 23, 42, 0.14);
}

.brand {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 18px;
  padding: 2px 4px;
}

.brand-main {
  display: flex;
  align-items: center;
  gap: 10px;
  overflow: hidden;
}

.brand-logo {
  width: auto;
  height: auto;
  background: transparent !important;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--m3-error-bright);
  flex-shrink: 0;
  box-shadow: none !important;
  padding: 0;
}

.crescent-icon {
  width: 28px;
  height: 28px;
  min-width: 28px;
  min-height: 28px;
  color: var(--m3-error-bright);
  fill: currentColor;
  flex-shrink: 0;
  -webkit-flex-shrink: 0;
  display: block;
}

.brand-text {
  overflow: hidden;
  white-space: nowrap;
}

.brand-text h2 {
  font-size: 1.02rem;
  font-weight: 700;
  letter-spacing: -0.2px;
  color: var(--sidebar-on-surface);
  line-height: 1.25;
}

.brand-subtitle {
  font-size: 0.66rem;
  color: var(--sidebar-on-surface-variant);
  font-family: 'JetBrains Mono', monospace;
  font-weight: 500;
}

/* Sidebar Toggle Button */
.sidebar-toggle-btn {
  background: var(--sidebar-icon-wrap-bg);
  border-radius: var(--radius-sm);
  color: var(--sidebar-on-surface-variant);
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: background-color var(--motion-fast) ease, color var(--motion-fast) ease, transform var(--motion-fast) ease;
}

.sidebar-toggle-btn:hover {
  background: var(--sidebar-item-active);
  color: var(--sidebar-on-surface);
}

.toggle-icon-svg {
  width: 16px;
  height: 16px;
  transition: transform var(--motion-base) ease;
}

.source-status-badge {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--sidebar-well);
  color: var(--sidebar-on-surface-variant);
  padding: 6px 12px;
  border-radius: var(--radius-pill);
  font-size: 0.72rem;
  font-weight: 600;
  margin-bottom: 16px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.pulse-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background-color: var(--m3-primary-light);
  flex-shrink: 0;
}

.nav-menu {
  display: flex;
  flex-direction: column;
  gap: 2px;
  flex-grow: 1;
  overflow-y: auto;
  padding-top: 8px;
  border-top: 1px solid rgba(255,255,255,0.06);
  margin-top: 8px;
  /* Quiet scrolling: sidebar is always dark, so use sidebar tokens (theme-proof) */
  scrollbar-width: thin;
  scrollbar-color: transparent transparent;
}
.nav-menu:hover,
.nav-menu:focus-within {
  scrollbar-color: var(--sidebar-item-hover) transparent;
}
.nav-menu::-webkit-scrollbar { width: 4px; }
.nav-menu::-webkit-scrollbar-track { background: transparent; }
.nav-menu::-webkit-scrollbar-thumb { background: transparent; border-radius: var(--radius-pill); }
.nav-menu:hover::-webkit-scrollbar-thumb,
.nav-menu:focus-within::-webkit-scrollbar-thumb { background: var(--sidebar-item-hover); }

.nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--sidebar-on-surface-variant);
  font-family: inherit;
  font-size: 0.86rem;
  font-weight: 500;
  letter-spacing: 0.01em;
  cursor: pointer;
  text-align: right;
  transition: background-color var(--motion-fast) ease, color var(--motion-fast) ease;
  position: relative;
  width: 100%;
}

.nav-item .label {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.nav-icon-wrap {
  width: 28px;
  height: 28px;
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--sidebar-icon-wrap-bg);
  color: var(--sidebar-on-surface-variant);
  flex-shrink: 0;
}

.nav-icon-wrap svg {
  width: 15px;
  height: 15px;
}

.nav-item:hover {
  background: var(--sidebar-item-hover);
  color: var(--sidebar-on-surface);
}

.nav-item:hover .nav-icon-wrap {
  color: var(--sidebar-on-surface);
}

/* Active item speaks the interface accent: primary-tinted track + solid primary icon (btn-primary language) */
.nav-item.active {
  background: color-mix(in srgb, var(--m3-primary) 24%, var(--sidebar-item-active));
  color: var(--sidebar-on-surface);
  font-weight: 600;
}

.nav-item.active .nav-icon-wrap {
  background: var(--m3-primary);
  color: var(--m3-on-primary);
}

.sidebar-footer {
  padding-top: 14px;
  margin-top: 10px;
  background: var(--sidebar-bg);
}

.version-tag {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  font-size: 0.72rem;
  color: var(--sidebar-on-surface-muted);
  margin-bottom: 8px;
  white-space: nowrap;
  overflow: hidden;
}

.version-title {
  font-family: 'JetBrains Mono', monospace;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tag-status {
  background: var(--sidebar-well);
  color: var(--sidebar-on-surface-variant);
  padding: 3px 7px;
  border-radius: var(--radius-pill);
  font-size: 0.66rem;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}

.mini-dot {
  width: 4px;
  height: 4px;
  border-radius: 50%;
  background: var(--m3-primary-light);
}

.standards-badges-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.std-pill {
  font-size: 0.62rem;
  font-family: 'JetBrains Mono', monospace;
  font-weight: 600;
  background: var(--sidebar-well);
  color: var(--sidebar-on-surface-muted);
  padding: 3px 6px;
  border-radius: var(--radius-pill);
}
/* NOTE: .session-card is permanently hidden (identity lives in the header chip) — sub-rules removed. */
.header-session-chip { display:flex; align-items:center; gap:8px; background:var(--m3-surface-container); padding:6px 10px; min-width:0; max-width:320px; border-radius: var(--radius-pill); }
.header-avatar { width:28px; height:28px; border-radius:50%; background:var(--m3-surface-container-high); display:flex; align-items:center; justify-content:center; color:var(--m3-on-surface-variant); flex-shrink:0; }
.header-avatar svg { width:14px; height:14px; }
.header-session-text { display:flex; flex-direction:column; min-width:0; line-height:1.2; }
.header-session-text strong { font-size:0.82rem; color:var(--m3-on-surface); white-space:nowrap; overflow:hidden; text-overflow:ellipsis; max-width:160px; }
.header-session-text small { font-size:0.68rem; color:var(--m3-on-surface-variant); white-space:nowrap; overflow:hidden; text-overflow:ellipsis; max-width:160px; }

/* =========================================
   CALM CHROME — quieter sidebar / header / toasts
   Tokens only (var(--*)) per DESIGN_SYSTEM.
   ========================================= */
.nav-group-label {
  font-size: 0.66rem;
  font-weight: 700;
  color: var(--sidebar-on-surface-muted);
  font-family: 'JetBrains Mono', 'IBM Plex Sans Arabic', monospace;
  letter-spacing: 0.02em;
  padding: 0 14px;
  margin: 14px 0 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.nav-menu > .nav-group-label:first-child { margin-top: 2px; }
/* Identity lives in the header chip — the drawer stays purely navigational */
.session-card { display: none !important; }
/* Quieter footer: version stays, standards recede */
.standards-badges-grid { gap: 3px; opacity: 0.7; }
.std-pill { font-size: 0.58rem; padding: 2px 5px; }
/* Calmer live status */
.live-pill { padding: 3px 8px; font-size: 0.68rem; gap: 5px; }
/* Consistent, quiet keyboard focus */
:focus-visible { outline: 2px solid var(--m3-primary); outline-offset: 2px; }

/* =========================================
   COMPLETE STATES & READING MEASURE
   Every control answers hover, focus, press, and disable. Prose keeps a calm measure.
   ========================================= */
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn:not(:disabled):active { transform: translateY(1px); }
.nav-item:active { background: var(--sidebar-item-active); }
.endpoint-btn:active, .auth-tab-btn:active, .quick-chip-btn:active { transform: translateY(1px); }
.form-input:disabled, .form-select:disabled { opacity: 0.5; cursor: not-allowed; }
.banner-text p, .source-desc, .auth-header p { max-width: 75ch; }

/* =========================================
   COLLAPSED SIDEBAR STATE
   ========================================= */
body.sidebar-collapsed .sidebar {
  width: var(--sidebar-collapsed-width);
  padding: 16px 8px;
}

body.sidebar-collapsed .brand {
  flex-direction: column;
  justify-content: center;
  gap: 10px;
  margin-bottom: 16px;
  padding: 0;
}

body.sidebar-collapsed .brand-main {
  justify-content: center;
}

body.sidebar-collapsed .brand-text {
  display: none;
}

body.sidebar-collapsed .sidebar-toggle-btn .toggle-icon-svg {
  transform: rotate(180deg);
}

body.sidebar-collapsed .source-status-badge {
  display: none;
}

body.sidebar-collapsed .nav-item {
  justify-content: center;
  padding: 9px 0;
  gap: 0;
}

body.sidebar-collapsed .nav-item .label {
  display: none;
}

body.sidebar-collapsed .sidebar-footer {
  display: none;
}

body.sidebar-collapsed .main-content {
  margin-right: calc(var(--sidebar-collapsed-width) + 24px);
  max-width: calc(100vw - var(--sidebar-collapsed-width) - 24px);
}

/* =========================================
   MAIN CONTENT & HEADER
   ========================================= */
.main-content {
  margin-right: calc(var(--sidebar-width) + 24px);
  flex-grow: 1;
  padding: 26px 34px 50px;
  max-width: calc(100vw - var(--sidebar-width) - 24px);
  transition: margin-right var(--motion-base) var(--motion-standard), max-width var(--motion-base) var(--motion-standard);
}

.top-header {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 26px;
  padding-bottom: 16px;
  gap: 16px;
}

.header-title-container {
  display: flex;
  flex-direction: column;
  gap: 5px;
  min-width: 0;
  flex: 1 1 280px;
}

.header-title-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.header-crescent-badge {
  width: auto;
  height: auto;
  background: transparent !important;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--m3-error);
  flex-shrink: 0;
  box-shadow: none !important;
  padding: 0;
}

.header-crescent-svg {
  width: 22px;
  height: 22px;
  min-width: 22px;
  min-height: 22px;
  color: var(--m3-error);
  fill: currentColor;
  flex-shrink: 0;
  -webkit-flex-shrink: 0;
  display: block;
}

.header-title-wrap h1 {
  font-size: 1.75rem;
  font-weight: 800;
  letter-spacing: -0.3px;
  color: var(--m3-on-surface);
  line-height: 1.2;
}

.header-title-wrap p {
  color: var(--m3-on-surface-variant);
  font-size: 0.86rem;
}

.header-live-stats {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-top: 2px;
}

.live-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--m3-surface-container);
  padding: 4px 9px;
  border-radius: var(--radius-pill);
  font-size: 0.72rem;
}

.live-pill-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background-color: var(--m3-primary-light);
}

.live-pill-label {
  color: var(--m3-on-surface-muted);
}

.live-pill-val {
  font-weight: 700;
  color: var(--m3-on-surface);
  font-family: 'JetBrains Mono', monospace;
}

.text-emerald {
  color: var(--m3-on-primary-container) !important;
}

.header-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

/* Active Patient Context Bar */
.active-patient-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 18px;
  margin-bottom: 22px;
  background: var(--m3-surface-container-low);
  border-radius: var(--radius-lg);
  gap: 16px;
  position: relative;
}

.active-patient-bar::before {
  content: none;
}

.patient-bar-icon {
  background: var(--m3-primary-container);
  color: var(--m3-on-primary-container);
  border-radius: var(--radius-sm);
  padding: 6px;
  width: 34px;
  height: 34px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.patient-bar-info {
  display: flex;
  align-items: center;
  gap: 10px;
}

.patient-bar-icon svg {
  width: 20px;
  height: 20px;
}

.patient-bar-details {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.patient-bar-label {
  font-size: 0.78rem;
  color: var(--m3-on-surface-variant);
}

.patient-bar-details strong {
  font-size: 0.92rem;
  color: var(--m3-on-surface);
}

.patient-bar-meta {
  font-size: 0.75rem;
  color: var(--m3-on-surface-muted);
}

.patient-bar-actions {
  flex-shrink: 0;
}

.form-select-sm {
  padding: 6px 12px;
  font-size: 0.78rem;
  border-radius: var(--radius-sm);
  background: var(--m3-surface-container);
  color: var(--m3-on-surface);
}
.patient-combobox { position:relative; width:320px; max-width:36vw; }
.combobox-input-wrap { display:flex; align-items:center; gap:6px; background:var(--m3-surface-container); padding:6px 10px; border:1px solid var(--m3-outline-variant); border-radius: var(--radius-sm); }
.combobox-input-wrap:focus-within { background:var(--m3-surface-container-high); border-color:var(--m3-primary); }
.combobox-search-icon { width:14px; height:14px; color:var(--m3-on-surface-muted); flex-shrink:0; }
#global-patient-search { flex:1; background:transparent; border:none; outline:none; color:var(--m3-on-surface); font-size:0.82rem; min-width:0; }
#global-patient-search::placeholder { color:var(--m3-on-surface-muted); }
.combobox-clear { background:transparent; color:var(--m3-on-surface-muted); cursor:pointer; font-size:0.9rem; line-height:1; padding:2px 4px; }
.combobox-clear:hover { color:var(--m3-on-surface); }
.combobox-meta { font-size:0.68rem; color:var(--m3-on-surface-muted); font-family:'JetBrains Mono',monospace; margin-inline-start:8px; }
.combobox-dropdown { position:absolute; top:calc(100% + 6px); right:0; left:0; background:var(--m3-surface-container-high); border:1px solid var(--m3-outline-variant); max-height:320px; overflow:auto; z-index:50; box-shadow:0 8px 24px rgba(0,0,0,0.35); border-radius: var(--radius-sm); }
.combobox-item { display:flex; justify-content:space-between; align-items:center; padding:8px 10px; cursor:pointer; gap:8px; border-bottom:1px solid var(--m3-surface-container); }
.combobox-item:hover { background:var(--m3-surface-container-highest); }
.combobox-item.active { background:var(--m3-primary-container); color:var(--m3-on-primary-container); }
.combobox-item-main { display:flex; flex-direction:column; gap:2px; min-width:0; }
.combobox-item-name { font-size:0.86rem; font-weight:700; white-space:nowrap; overflow:hidden; text-overflow:ellipsis; }
.combobox-item-sub { font-size:0.70rem; color:var(--m3-on-surface-variant); font-family:'JetBrains Mono',monospace; white-space:nowrap; overflow:hidden; text-overflow:ellipsis; }
.combobox-item.active .combobox-item-sub { color:var(--m3-on-primary-container); opacity:0.85; }
.combobox-load-more { width:100%; padding:8px; text-align:center; background:var(--m3-surface-container); color:var(--m3-primary); cursor:pointer; font-weight:700; font-size:0.78rem; border-radius: var(--radius-sm); }
.combobox-load-more:hover { background:var(--m3-surface-container-highest); }
.combobox-empty { padding:14px; text-align:center; color:var(--m3-on-surface-variant); font-size:0.82rem; }

/* =========================================
   BUTTONS (BORDERLESS HIGH-CONTRAST)
   ========================================= */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 8px 16px;
  border-radius: var(--radius-sm);
  font-family: inherit;
  font-size: 0.86rem;
  font-weight: 700;
  cursor: pointer;
  transition: background-color var(--motion-fast) ease, color var(--motion-fast) ease;
}

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

.btn-primary {
  background: #EDEDED;
  color: #121212;
}

.btn-primary:hover {
  background: #FFFFFF;
  color: #000000;
}

body.light-theme .btn-primary,
[data-theme="light"] .btn-primary {
  background: #0F172A;
  color: #FFFFFF;
}

body.light-theme .btn-primary:hover,
[data-theme="light"] .btn-primary:hover {
  background: #1E293B;
  color: #FFFFFF;
}

.btn-secondary {
  background: var(--m3-surface-container-highest);
  color: var(--m3-on-surface);
}

.btn-secondary:hover {
  background: #444444;
  color: #FFFFFF;
}

body.light-theme .btn-secondary,
[data-theme="light"] .btn-secondary {
  background: #DCE0E6;
  color: #111827;
}

body.light-theme .btn-secondary:hover,
[data-theme="light"] .btn-secondary:hover {
  background: #CFD5DC;
  color: #000000;
}

.btn-sm {
  padding: 5px 10px;
  font-size: 0.76rem;
}

.btn-sm .btn-svg-icon {
  width: 13px;
  height: 13px;
}

/* =========================================
   TAB PANES
   ========================================= */
.tab-pane {
  display: none;
}

.tab-pane.active {
  display: block;
  animation: tabFade var(--motion-instant) ease;
}

@keyframes tabFade {
  from { opacity: 0.9; }
  to { opacity: 1; }
}

/* =========================================
   METRIC CARDS (BORDERLESS TONAL ELEVATION)
   ========================================= */
.metrics-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 12px;
  margin-bottom: 24px;
}

.metric-card {
  background: var(--m3-surface-container);
  border-radius: var(--radius-lg);
  padding: 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  transition: background-color var(--motion-fast) ease;
}

.metric-card:hover {
  background: var(--m3-surface-container-high);
}

.metric-icon {
  width: 42px;
  height: 42px;
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.metric-icon svg {
  width: 20px;
  height: 20px;
}

/* High-Contrast Tonal Colors for Metric Icons */
.bg-blue {
  background: var(--m3-secondary-container);
  color: var(--m3-on-secondary-container);
}

.bg-emerald {
  background: var(--m3-primary-container);
  color: var(--m3-on-primary-container);
}

.bg-cyan {
  background: var(--m3-secondary-container);
  color: var(--m3-on-secondary-container);
}

.bg-purple {
  background: var(--m3-tertiary-container);
  color: var(--m3-on-tertiary-container);
}

.bg-amber {
  background: var(--m3-warning-container);
  color: var(--m3-on-warning-container);
}

.metric-info {
  display: flex;
  flex-direction: column;
}

.metric-label {
  font-size: 0.78rem;
  color: var(--m3-on-surface-variant);
  font-weight: 500;
}

.metric-value {
  font-size: 1.55rem;
  font-weight: 800;
  color: var(--m3-on-surface);
  margin: 1px 0;
  font-family: 'JetBrains Mono', monospace;
  letter-spacing: -0.5px;
}

.metric-sub {
  font-size: 0.7rem;
  color: var(--m3-on-surface-muted);
}

.trend-tag {
  color: var(--m3-on-surface-variant);
  font-weight: 600;
}

.trend-tag.success {
  color: var(--m3-on-primary-container);
  font-weight: 700;
}

/* Lead indicators: prominent stacked stats (value-first hierarchy, 1-3 items) */
.metrics-lead {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 12px;
  margin-bottom: 12px;
}

.metric-lead {
  background: var(--m3-surface-container);
  border-radius: var(--radius-lg);
  padding: 20px 22px;
  display: flex;
  flex-direction: column;
  gap: 3px;
  transition: background-color var(--motion-fast) ease;
}

.metric-lead:hover {
  background: var(--m3-surface-container-high);
}

.metric-eyebrow {
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  color: var(--m3-on-surface-muted);
}

.metric-value-lg {
  font-size: 2.1rem;
  line-height: 1.1;
}

/* Compact secondary strip: dense recessed wells, inline rhythm distinct from lead */
.metrics-compact {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
  margin-bottom: 24px;
}

.metric-compact {
  background: var(--m3-surface-container-low);
  border-radius: var(--radius-md);
  padding: 10px 14px;
  display: flex;
  flex-direction: column;
  gap: 1px;
  min-width: 0;
  transition: background-color var(--motion-fast) ease;
}

.metric-compact:hover {
  background: var(--m3-surface-container-high);
}

.metric-compact .metric-label {
  font-size: 0.72rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.metric-compact .metric-value {
  font-size: 1.1rem;
  letter-spacing: -0.3px;
}

.metric-compact .metric-sub {
  font-size: 0.66rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Icon-less stat variant (generalized): value stacked over label, no icon well needed */
.metric-card:has(> .metric-info),
.metric-card:has(.metric-info) {
  align-items: flex-start;
}

@media (max-width: 1024px) {
  .metrics-compact { grid-template-columns: repeat(3, 1fr); }
}

@media (max-width: 768px) {
  .metrics-lead { grid-template-columns: 1fr; }
  .metric-value-lg { font-size: 1.8rem; }
}

@media (max-width: 480px) {
  .metrics-compact { grid-template-columns: 1fr 1fr; }
}

/* =========================================
   SECTION TITLES
   ========================================= */
.section-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
}

.section-title-wrap {
  display: flex;
  align-items: center;
  gap: 7px;
}

.section-svg {
  width: 16px;
  height: 16px;
  color: var(--m3-on-surface-variant);
}

.section-title h3 {
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--m3-on-surface);
}

/* =========================================
   HOSPITAL SOURCES GRID (BORDERLESS)
   ========================================= */
.sources-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(310px, 1fr));
  gap: 14px;
  margin-bottom: 24px;
}

.source-card {
  background: var(--m3-surface-container);
  border-radius: var(--radius-lg);
  padding: 18px;
  transition: background-color var(--motion-fast) ease;
}

.source-card:hover {
  background: var(--m3-surface-container-high);
}

.source-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 10px;
  padding-bottom: 10px;
}

.source-tag {
  font-size: 0.66rem;
  font-family: 'JetBrains Mono', monospace;
  font-weight: 700;
  padding: 3px 6px;
  border-radius: var(--radius-pill);
  display: inline-block;
  margin-bottom: 3px;
}

.source-tag.legacy {
  background: var(--m3-warning-container);
  color: var(--m3-on-warning-container);
}

.source-tag.modern {
  background: var(--m3-secondary-container);
  color: var(--m3-on-secondary-container);
}

.source-tag.fhir {
  background: var(--m3-primary-container);
  color: var(--m3-on-primary-container);
}

.source-title-wrap h4 {
  font-size: 1rem;
  font-weight: 700;
  color: var(--m3-on-surface);
}

.status-indicator {
  font-size: 0.7rem;
  font-weight: 700;
  padding: 3px 8px;
  border-radius: var(--radius-pill);
  display: inline-flex;
  align-items: center;
  gap: 5px;
}

.status-indicator.online {
  background: var(--m3-primary-container);
  color: var(--m3-on-primary-container);
}

.status-dot {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--m3-primary-light);
}

.source-desc {
  font-size: 0.82rem;
  color: var(--m3-on-surface-variant);
  margin-bottom: 12px;
  line-height: 1.5;
}

/* Inner Well Container */
.source-details {
  display: flex;
  flex-direction: column;
  gap: 8px;
  font-size: 0.78rem;
  background: var(--m3-surface-container-low);
  padding: 12px;
  border-radius: var(--radius-md);
}

.detail-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.detail-row span {
  color: var(--m3-on-surface-muted);
}

.detail-row strong {
  color: var(--m3-on-surface);
}

.detail-row code {
  font-family: 'JetBrains Mono', monospace;
  color: var(--m3-on-secondary-container);
  font-size: 0.76rem;
}

/* =========================================
   CARDS & DATA TABLES (BORDERLESS)
   ========================================= */
.card {
  background: var(--m3-surface-container);
  border-radius: var(--radius-lg);
  overflow: hidden;
}

.card-header {
  padding: 14px 18px;
  background: var(--m3-surface-container-high);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-header-title {
  display: flex;
  align-items: center;
  gap: 8px;
}

.card-header-icon {
  width: 16px;
  height: 16px;
  color: var(--m3-on-surface-variant);
}

.card-header h3 {
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--m3-on-surface);
}

.card-body {
  padding: 16px 18px;
}

.p-0 {
  padding: 0 !important;
}

.table-card {
  overflow-x: auto;
}

.data-table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  text-align: right;
  font-size: 0.85rem;
  border: 1px solid var(--m3-outline-variant);
}

.data-table th {
  padding: 12px 16px;
  background: var(--m3-surface-container-high);
  color: var(--m3-on-surface);
  font-weight: 700;
  white-space: nowrap;
  font-size: 0.76rem;
  text-transform: uppercase;
  border-bottom: 1px solid var(--m3-outline-variant);
}

.data-table td {
  padding: 11px 16px;
  color: var(--m3-on-surface);
  vertical-align: middle;
  background: var(--m3-surface-container);
  border-bottom: 1px solid var(--m3-outline-variant);
}

.data-table tbody tr:hover td {
  background: var(--m3-surface-container-high);
}

.data-table code {
  font-size: 0.78rem;
  background: var(--m3-surface-container-low);
  padding: 2px 6px;
  border-radius: var(--radius-xs);
}
/* Sticky headers + pinned first column (RTL: right edge) in every scroller */
.data-table thead th { position: sticky; top: 0; z-index: 2; }
.data-table tbody td:first-child { position: sticky; right: 0; z-index: 1; box-shadow: -4px 0 6px -4px rgba(0, 0, 0, 0.35); }
.data-table thead th:first-child { right: 0; z-index: 3; }
/* Full-row states (loading/empty) read as quiet captions, not data */
.data-table td[colspan] { color: var(--m3-on-surface-muted); }
/* Long hashes wrap instead of forcing width */
.data-table td code { overflow-wrap: break-word; }
/* Row highlights were invisible under opaque cells — paint every cell incl. pinned */
tr[style*="background:var(--m3-primary-container)"] > td { background: var(--m3-primary-container); }
tr[style*="background:var(--m3-tertiary-container)"] > td { background: var(--m3-tertiary-container); }
/* Firefox: thin thumbs on table scrollers (WebKit covered globally) */
.table-card, .table-wrap, .table-responsive, .card-body.p-0 { scrollbar-width: thin; scrollbar-color: var(--m3-surface-container-highest) transparent; }

/* =========================================
   STATUS BADGES & PILLS
   ========================================= */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 7px;
  border-radius: var(--radius-pill);
  font-size: 0.7rem;
  font-weight: 700;
  font-family: 'JetBrains Mono', monospace;
  white-space: nowrap;
}

.badge-info {
  background: var(--m3-secondary-container);
  color: var(--m3-on-secondary-container);
}

.badge-secondary {
  background: var(--m3-secondary-container);
  color: var(--m3-on-secondary-container);
}

.badge-success {
  background: var(--m3-primary-container);
  color: var(--m3-on-primary-container);
}

.badge-warning {
  background: var(--m3-warning-container);
  color: var(--m3-on-warning-container);
}

.badge-purple {
  background: var(--m3-tertiary-container);
  color: var(--m3-on-tertiary-container);
}

.badge-error {
  background: var(--m3-error-container);
  color: var(--m3-on-error-container);
}

/* =========================================
   BANNER INFO (BORDERLESS)
   ========================================= */
.banner-info {
  background: var(--m3-surface-container);
  border-radius: var(--radius-lg);
  padding: 16px 20px;
  display: flex;
  gap: 14px;
  align-items: center;
  margin-bottom: 20px;
  position: relative;
  overflow: hidden;
}

.banner-info::before {
  content: none;
}

/* Banner accent via tinted icon well (no side-stripe): calm tonal language */
.banner-info.banner-primary .banner-icon {
  background: var(--m3-primary-container);
  color: var(--m3-on-primary-container);
}

.banner-info.banner-tertiary .banner-icon {
  background: var(--m3-tertiary-container);
  color: var(--m3-on-tertiary-container);
}

.banner-icon {
  width: 40px;
  height: 40px;
  border-radius: var(--radius-sm);
  background: var(--m3-surface-container-high);
  color: var(--m3-on-surface);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.banner-icon svg {
  width: 20px;
  height: 20px;
}

.banner-text h4 {
  font-size: 1rem;
  font-weight: 700;
  color: var(--m3-on-surface);
  margin-bottom: 2px;
}

.banner-text p {
  color: var(--m3-on-surface-variant);
  font-size: 0.84rem;
  line-height: 1.5;
}

/* =========================================
   MPI (MASTER PATIENT INDEX)
   ========================================= */
.mpi-card {
  background: var(--m3-surface-container);
  border-radius: var(--radius-lg);
  padding: 18px;
  margin-bottom: 18px;
}

.mpi-card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 14px;
  padding-bottom: 12px;
}

.mpi-person-title {
  display: flex;
  align-items: center;
  gap: 10px;
}

.avatar-circle {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--m3-surface-container-high);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--m3-on-surface-variant);
  flex-shrink: 0;
}

.avatar-circle svg {
  width: 18px;
  height: 18px;
}

.mpi-person-title h4 {
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--m3-on-surface);
}

.mpi-identifiers-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
  gap: 10px;
  margin-bottom: 14px;
}

.identifier-box {
  background: var(--m3-surface-container-low);
  border-radius: var(--radius-md);
  padding: 12px 14px;
}

.id-type {
  font-size: 0.68rem;
  color: var(--m3-on-surface-muted);
  font-weight: 600;
  display: block;
  margin-bottom: 3px;
}

.id-val {
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.98rem;
  font-weight: 700;
  color: var(--m3-on-secondary-container);
}

.id-source {
  font-size: 0.68rem;
  color: var(--m3-on-surface-variant);
}

/* =========================================
   LONGITUDINAL RECORD & TIMELINE
   ========================================= */
.patient-selector-card {
  background: var(--m3-surface-container);
  border-radius: var(--radius-lg);
  padding: 16px 20px;
  margin-bottom: 20px;
}

.form-group label {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 0.86rem;
  color: var(--m3-on-surface-variant);
  margin-bottom: 7px;
  font-weight: 600;
}

.label-svg {
  width: 15px;
  height: 15px;
  color: var(--m3-on-surface-variant);
}

.form-input,
.form-select {
  width: 100%;
  padding: 9px 14px;
  background: var(--m3-surface-container-low);
  border: 1px solid var(--m3-outline-variant);
  border-radius: var(--radius-sm);
  color: var(--m3-on-surface);
  font-family: inherit;
  font-size: 0.9rem;
  font-weight: 400;
  outline: none;
  transition: background-color var(--motion-fast) ease, border-color var(--motion-fast) ease;
}
.form-input::placeholder { color: var(--m3-on-surface-muted); }
.form-select { font-weight: 600; cursor: pointer; }
.form-input:focus,
.form-select:focus {
  background: var(--m3-surface-container-high);
  border-color: var(--m3-primary);
}

.timeline {
  position: relative;
  padding-right: 26px;
  margin-top: 18px;
}

.timeline::before {
  content: '';
  position: absolute;
  top: 4px;
  bottom: 4px;
  right: 9px;
  width: 2px;
  background: var(--m3-surface-container-high);
}

.timeline-item {
  position: relative;
  margin-bottom: 18px;
}

.timeline-dot {
  position: absolute;
  top: 0;
  right: -26px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--m3-surface-container-high);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--m3-on-surface-variant);
}

.timeline-dot svg {
  width: 12px;
  height: 12px;
}

.timeline-card {
  background: var(--m3-surface-container);
  border-radius: var(--radius-lg);
  padding: 14px 18px;
}

/* =========================================
   FHIR & NPHIES EXPLORER
   ========================================= */
.fhir-explorer-layout {
  display: grid;
  grid-template-columns: 280px 1fr;
  gap: 16px;
}

.fhir-sidebar {
  background: var(--m3-surface-container);
  border-radius: var(--radius-lg);
  padding: 14px;
  height: fit-content;
}

.fhir-sidebar-header {
  display: flex;
  align-items: center;
  gap: 7px;
  margin-bottom: 10px;
  padding-bottom: 8px;
  color: var(--m3-on-surface);
}

.fhir-sidebar-header svg {
  width: 16px;
  height: 16px;
}

.fhir-sidebar-header h4 {
  font-size: 0.95rem;
  font-weight: 700;
  color: var(--m3-on-surface);
}

.endpoint-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.endpoint-btn {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 8px 10px;
  background: transparent;
  border-radius: var(--radius-sm);
  color: var(--m3-on-surface-variant);
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.74rem;
  cursor: pointer;
  text-align: right;
  transition: background-color var(--motion-fast) ease, color var(--motion-fast) ease, transform var(--motion-fast) ease;
  width: 100%;
}

.endpoint-btn:hover {
  background: var(--m3-surface-container-high);
  color: var(--m3-on-surface);
}

.endpoint-btn.active {
  background: var(--m3-surface-container-high);
  color: var(--m3-on-secondary-container);
}

.method.get {
  background: var(--m3-surface-container-highest);
  color: var(--m3-on-surface);
  padding: 2px 5px;
  border-radius: var(--radius-pill);
  font-size: 0.62rem;
  font-weight: 800;
}

.nphies-compliance-box {
  background: var(--m3-surface-container-low);
  border-radius: var(--radius-md);
  padding: 12px;
}

.compliance-box-header {
  display: flex;
  align-items: center;
  gap: 5px;
  margin-bottom: 7px;
}

.compliance-box-header svg {
  width: 15px;
  height: 15px;
}

.compliance-box-header h5 {
  font-size: 0.82rem;
  color: var(--m3-on-surface);
  font-weight: 700;
}

.compliance-checklist {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.compliance-checklist li {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 0.72rem;
  color: var(--m3-on-surface-variant);
}

.check-svg {
  width: 12px;
  height: 12px;
  color: var(--m3-primary-light);
  flex-shrink: 0;
}

.fhir-viewer {
  background: var(--m3-surface-container);
  border-radius: var(--radius-lg);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.viewer-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  background: var(--m3-surface-container-high);
}

.viewer-url-wrap {
  display: flex;
  align-items: center;
  gap: 7px;
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.82rem;
  color: var(--m3-on-secondary-container);
}

.viewer-badge {
  background: var(--m3-surface-container);
  color: var(--m3-on-surface-variant);
  padding: 2px 6px;
  border-radius: var(--radius-pill);
  font-size: 0.64rem;
  font-weight: 700;
}

.json-display {
  padding: 18px;
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.82rem;
  color: #E2E8F0;
  overflow-x: auto;
  max-height: 600px;
  line-height: 1.55;
  direction: ltr !important;
  text-align: left !important;
  unicode-bidi: isolate;
  background: #0E0E0E;
}

body.light-theme .json-display,
[data-theme="light"] .json-display {
  background: #0F172A;
  color: #F8FAFC;
}
/* Code viewer is always dark — dark-aware thumb (theme-proof) */
.json-display {
  scrollbar-width: thin;
  scrollbar-color: transparent transparent;
}
.json-display:hover,
.json-display:focus-within {
  scrollbar-color: var(--sidebar-item-hover) transparent;
}
.json-display::-webkit-scrollbar-track { background: transparent; }
.json-display::-webkit-scrollbar-thumb { background: transparent; border-radius: var(--radius-pill); }
.json-display:hover::-webkit-scrollbar-thumb,
.json-display:focus-within::-webkit-scrollbar-thumb { background: var(--sidebar-item-hover); }

/* =========================================
   TOAST NOTIFICATIONS (BORDERLESS)
   ========================================= */
.toast-container {
  position: fixed;
  top: 18px;
  left: 18px;
  z-index: 9999;
  display: flex;
  flex-direction: column;
  gap: 7px;
  max-width: 380px;
}

.toast {
  background: var(--m3-surface-container-high);
  border-radius: var(--radius-md);
  padding: 12px 16px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: flex-start;
  gap: 10px;
  animation: toastSlideIn var(--motion-base) ease;
  color: var(--m3-on-surface);
  position: relative;
}

body.light-theme .toast,
[data-theme="light"] .toast {
  background: #FFFFFF;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.12);
}

.toast.toast-success::before {
  content: '';
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  width: 4px;
  background: var(--m3-primary-light);
}

.toast.toast-error::before {
  content: '';
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  width: 4px;
  background: var(--m3-error);
}

.toast.toast-info::before {
  content: '';
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  width: 4px;
  background: var(--m3-secondary);
}

/* Round the status stripes with their toast */
.toast.toast-success::before,
.toast.toast-error::before,
.toast.toast-info::before {
  border-start-start-radius: var(--radius-md);
  border-end-start-radius: var(--radius-md);
}

.toast-icon {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
  margin-top: 2px;
}

.toast-success .toast-icon { color: var(--m3-on-primary-container); }
.toast-error .toast-icon { color: var(--m3-on-error-container); }
.toast-info .toast-icon { color: var(--m3-on-secondary-container); }

.toast-content h5 {
  font-size: 0.88rem;
  font-weight: 700;
  margin-bottom: 2px;
  color: var(--m3-on-surface);
}

.toast-content p {
  font-size: 0.78rem;
  color: var(--m3-on-surface-variant);
  line-height: 1.4;
}

@keyframes toastSlideIn {
  from { opacity: 0; transform: translateY(-6px); }
  to { opacity: 1; transform: translateY(0); }
}

/* =========================================
   CLINICAL FILE DROPZONE
   ========================================= */
.file-dropzone {
  border: 2px dashed var(--m3-outline-focus, #444);
  border-radius: var(--radius-sm);
  padding: 32px 20px;
  text-align: center;
  background: var(--m3-surface-container-low);
  cursor: pointer;
  transition: background var(--motion-base) ease, border-color var(--motion-base) ease, transform var(--motion-fast) ease;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.file-dropzone:hover,
.file-dropzone.dragover {
  background: var(--m3-surface-container-high);
  border-color: var(--m3-primary-light);
  transform: scale(1.005);
}

.dropzone-icon {
  width: 44px;
  height: 44px;
  color: var(--m3-primary-light);
  margin-bottom: 10px;
  stroke-width: 1.6;
}

.ingestion-result-box {
  background: var(--m3-surface-container-low);
  padding: 16px 18px;
  border-radius: var(--radius-lg);
  position: relative;
  overflow: hidden;
}

.ingestion-result-box.success::before {
  content: '';
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  width: 4px;
  background: var(--m3-primary-light);
}

.ingestion-result-box.error::before {
  content: '';
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  width: 4px;
  background: var(--m3-error);
}

/* =========================================
   UTILITIES
   ========================================= */
.mt-4 { margin-top: 12px; }
.mb-4 { margin-bottom: 12px; }
.mt-6 { margin-top: 18px; }
.mb-6 { margin-bottom: 18px; }
.text-center { text-align: center; }

/* Quiet counters: plain muted numerals replace pill badges for counts */
.stat-count {
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.78rem;
  font-weight: 700;
  color: var(--m3-on-surface-muted);
}

/* Demoted descriptor: formerly a colored badge, now quiet inline text */
.muted-note {
  font-size: 0.74rem;
  color: var(--m3-on-surface-muted);
}

/* Calm table empty/loading cells: muted tone with breathing room */
.data-table td.text-center {
  color: var(--m3-on-surface-variant);
  padding: 18px 12px;
}

/* Smooth anchor/scroll motion (disabled under reduced-motion below) */
html {
  scroll-behavior: smooth;
}

/* Row hover eases instead of snapping */
.data-table tbody tr {
  transition: background-color var(--motion-fast) ease;
}

/* Skeleton loaders: shimmer wells for first-paint loading states.
   Duration exception (like spin): shimmer physics needs ~1.2s, outside --motion-* scale. */
@keyframes skelShimmer {
  from { background-position: 200% 0; }
  to { background-position: -200% 0; }
}

.skel {
  display: block;
  height: 12px;
  border-radius: var(--radius-xs);
  background: linear-gradient(90deg, var(--m3-surface-container-high) 25%, var(--m3-surface-container-highest) 50%, var(--m3-surface-container-high) 75%);
  background-size: 200% 100%;
  animation: skelShimmer 1.2s linear infinite;
}

.skel + .skel {
  margin-top: 8px;
}

.skel-row td {
  padding: 14px 16px;
}

/* Patient vitals charts: calm wells, token-colored SVG, responsive stack */
.sr-charts-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 10px;
}

.sr-chart {
  background: var(--m3-surface-container-low);
  border-radius: var(--radius-md);
  padding: 12px 14px;
}

.sr-chart-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 6px;
}

.sr-chart-head strong {
  font-size: 0.85rem;
  color: var(--m3-on-surface);
}

.sr-chart-legend {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 4px;
  font-size: 0.7rem;
  color: var(--m3-on-surface-variant);
}

.sr-legend-item {
  display: inline-flex;
  align-items: center;
  gap: 5px;
}

.sr-legend-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  display: inline-block;
  flex-shrink: 0;
}

.sr-chart-note {
  display: block;
  margin-top: 4px;
  font-size: 0.68rem;
  color: var(--m3-on-surface-muted);
}

/* Keyboard trace inside dense tables and combobox lists */
.data-table tbody tr:focus-within {
  outline: 2px solid var(--m3-primary);
  outline-offset: -2px;
}

.combobox-item:focus-visible {
  background: var(--m3-surface-container-highest);
  outline: 2px solid var(--m3-primary);
  outline-offset: -2px;
}

/* =========================================
   OFFICIAL SAUDI MEDICAL REPORT & PDF STYLES
   ========================================= */
.official-medical-report {
  display: none;
}

.report-preview-modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(15, 23, 42, 0.88);
  z-index: 999999;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  padding: 24px 16px;
  overflow-y: auto;
  backdrop-filter: blur(8px);
}

.report-preview-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  max-width: 860px;
  background: var(--m3-surface-container);
  padding: 12px 18px;
  border-radius: var(--radius-sharp);
  border: 1px solid var(--m3-outline-variant);
  margin-bottom: 12px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.25);
  position: sticky;
  top: 0;
  z-index: 10;
}

.report-preview-sheet {
  background: #ffffff !important;
  color: #1a1a1a !important;
  width: 100%;
  max-width: 860px;
  min-height: 1100px;
  padding: 36px 42px;
  border-radius:var(--radius-sharp);
  box-shadow: 0 12px 48px rgba(0, 0, 0, 0.45);
  font-family: 'Cairo', 'Segoe UI', Tahoma, sans-serif;
  position: relative;
  margin-bottom: 36px;
}

/* Explicit color isolation for the preview sheet */
.report-preview-sheet * {
  color: #1e293b;
  box-sizing: border-box;
}

.report-preview-sheet code {
  background: #f1f5f9;
  color: #0f172a;
  padding: 1px 5px;
  border-radius: 3px;
  border: 1px solid #cbd5e1;
  font-size: 0.78rem;
}

/* PDF Document Header */
.pdf-doc-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 3px double #1b5e20;
  padding-bottom: 14px;
  margin-bottom: 16px;
}

.pdf-header-emblem {
  display: flex;
  align-items: center;
  gap: 12px;
}

.pdf-emblem-svg {
  width: 52px;
  height: 52px;
  color: #1b5e20;
}

.pdf-header-titles h2 {
  font-size: 1.15rem;
  font-weight: 800;
  color: #1b5e20 !important;
  margin: 0;
}

.pdf-header-titles h3 {
  font-size: 0.92rem;
  font-weight: 700;
  color: #334155 !important;
  margin: 2px 0;
}

.pdf-header-titles span {
  font-size: 0.76rem;
  color: #64748b !important;
  display: block;
}

.pdf-header-meta {
  text-align: left;
  font-size: 0.76rem;
  color: #334155 !important;
  border: 1px solid #c8e6c9;
  background: #f1f8e9;
  padding: 8px 12px;
  border-radius:var(--radius-sharp);
  line-height: 1.5;
}

.pdf-title-banner {
  text-align: center;
  background: #f0f7f2;
  border: 1px solid #c8e6c9;
  padding: 10px;
  border-radius:var(--radius-sharp);
  margin-bottom: 16px;
}

.pdf-title-banner h1 {
  font-size: 1.25rem;
  font-weight: 800;
  color: #1b5e20 !important;
  margin: 0 0 4px 0;
}

.pdf-title-banner p {
  font-size: 0.78rem;
  color: #475569 !important;
  margin: 0;
}

/* Patient Profile Box */
.pdf-patient-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
  background: #f8fafc;
  border: 1px solid #e2e8f0;
  padding: 12px 16px;
  border-radius:var(--radius-sharp);
  margin-bottom: 16px;
  font-size: 0.82rem;
}

.pdf-grid-cell {
  display: flex;
  flex-direction: column;
}

.pdf-grid-label {
  font-size: 0.72rem;
  color: #64748b !important;
  font-weight: 600;
  margin-bottom: 2px;
}

.pdf-grid-val {
  font-weight: 700;
  color: #0f172a !important;
}

/* Section Dividers in PDF */
.pdf-section {
  margin-bottom: 16px;
  page-break-inside: avoid;
}

.pdf-section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: #f1f5f9;
  border-right: 4px solid #1b5e20;
  padding: 6px 12px;
  margin-bottom: 8px;
  border-bottom: 1px solid #cbd5e1;
}

.pdf-section-header h4 {
  font-size: 0.92rem;
  font-weight: 700;
  color: #1b5e20 !important;
  margin: 0;
}

.pdf-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.8rem;
  margin-bottom: 8px;
}

.pdf-table th {
  background: #f0fdf4 !important;
  color: #166534 !important;
  font-weight: 700;
  border: 1px solid #cbd5e1;
  padding: 7px 9px;
  text-align: right;
  font-size: 0.76rem;
}

.pdf-table td {
  border: 1px solid #e2e8f0;
  padding: 7px 9px;
  color: #1e293b !important;
  vertical-align: middle;
}

.pdf-table tr:nth-child(even) td {
  background: #f8fafc;
}

/* PDF Highlights Bar */
.pdf-kpi-bar {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
  margin-bottom: 16px;
}

.pdf-kpi-box {
  background: #ffffff;
  border: 1px solid #e2e8f0;
  border-top: 3px solid #1b5e20;
  padding: 10px 12px;
  text-align: center;
  border-radius:var(--radius-sharp);
}

.pdf-kpi-box.warning {
  border-top-color: #ea580c;
  background: #fffbeb;
}

.pdf-kpi-box .kpi-num {
  font-size: 1.3rem;
  font-weight: 800;
  color: #1b5e20 !important;
  margin: 2px 0;
}

.pdf-kpi-box.warning .kpi-num {
  color: #dc2626 !important;
}

.pdf-kpi-box .kpi-title {
  font-size: 0.74rem;
  color: #64748b !important;
  font-weight: 600;
}

/* PDF Footer with Digital Verification Seal & QR */
.pdf-doc-footer {
  border-top: 2px solid #1b5e20;
  padding-top: 14px;
  margin-top: 24px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.74rem;
  color: #475569 !important;
  page-break-inside: avoid;
}

.pdf-auth-seal {
  display: flex;
  align-items: center;
  gap: 10px;
  border: 1px dashed #22c55e;
  background: #f0fdf4;
  padding: 8px 12px;
  border-radius:var(--radius-sharp);
}

.pdf-qr-box {
  width: 58px;
  height: 58px;
  background: #ffffff;
  border: 1px solid #cbd5e1;
  padding: 3px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.pdf-auth-text {
  line-height: 1.4;
}

.pdf-auth-text strong {
  color: #1b5e20 !important;
  font-size: 0.8rem;
  display: block;
}

/* =========================================
   PRINT MEDIA RULES (ZERO BLANK PAGES)
   ========================================= */
@media print {
  @page {
    size: A4 portrait;
    margin: 8mm 10mm 10mm 10mm;
  }

  html, body {
    background: #ffffff !important;
    color: #000000 !important;
    font-size: 9pt !important;
    width: 100% !important;
    height: auto !important;
    margin: 0 !important;
    padding: 0 !important;
  }

  /* Completely hide all app structure and navigation to guarantee Page 1 starts immediately */
  .app-layout,
  #active-patient-bar,
  .sidebar,
  .top-header,
  .main-content,
  .content-wrapper,
  .tab-pane,
  .patient-selector-card,
  #btn-print-patient-summary,
  #toast-container,
  .btn,
  .modal-overlay,
  .report-preview-toolbar,
  #longitudinal-content {
    display: none !important;
  }

  .report-preview-modal {
    position: static !important;
    background: transparent !important;
    padding: 0 !important;
    margin: 0 !important;
    width: 100% !important;
    height: auto !important;
    display: block !important;
    overflow: visible !important;
    backdrop-filter: none !important;
  }

  .report-preview-sheet,
  .official-medical-report {
    display: block !important;
    box-shadow: none !important;
    border: none !important;
    width: 100% !important;
    max-width: 100% !important;
    padding: 0 !important;
    margin: 0 !important;
    min-height: auto !important;
  }

  .pdf-doc-header,
  .pdf-patient-grid,
  .pdf-section,
  .pdf-kpi-bar,
  .pdf-doc-footer {
    page-break-inside: avoid !important;
  }

  .pdf-table {
    page-break-inside: auto !important;
  }

  .pdf-table tr {
    page-break-inside: avoid !important;
    page-break-after: auto !important;
  }

  .pdf-table th {
    background: #f0fdf4 !important;
    color: #166534 !important;
    -webkit-print-color-adjust: exact !important;
    print-color-adjust: exact !important;
  }

  .pdf-kpi-box {
    -webkit-print-color-adjust: exact !important;
    print-color-adjust: exact !important;
  }

  /* Sticky has no meaning on paper — avoid print artifacts */
  .data-table thead th, .data-table tbody td:first-child { position: static; box-shadow: none; }
}
.text-muted { color: var(--m3-on-surface-muted) !important; }
.py-4 { padding-top: 12px; padding-bottom: 12px; }

/* =========================================
   CROSS-BREAKPOINT CLEANUP — same behavior on every viewport
   ========================================= */
/* Section headers wrap gracefully instead of squeezing (all sizes) */
.card-header, .section-title, .mpi-card-header, .viewer-header, .source-header { flex-wrap: wrap; gap: 10px; }
/* Balanced page headings */
.header-title-wrap h1 { text-wrap: balance; }
/* JS-rendered tables inside flush card bodies scroll instead of clipping
   (parent .card keeps overflow:hidden for its header backgrounds) */
.card-body.p-0 { overflow-x: auto; -webkit-overflow-scrolling: touch; overscroll-behavior-x: contain; }
/* Opt-in scroll wrapper for wide tables inside padded card bodies */
.table-responsive, .table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; overscroll-behavior-x: contain; }
/* Long viewer URLs wrap instead of pushing layout */
.viewer-url-wrap { min-width: 0; overflow-wrap: anywhere; }
/* Icon-less stat variant (security pane): value stacked over label */
#pane-security .metric-card:has(> .metric-value) { flex-direction: column; align-items: flex-start; gap: 2px; }
/* Orphaned action rows (ex hero headers): quiet action bar */
.actions-group { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 20px; }
/* Responsive card tables (.rx-table) + toolbar (all viewports) — shared by medications & longitudinal panes */
.rx-toolbar { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; padding: 12px 18px; background: var(--m3-surface-container-high); }
.rx-search-wrap { display: flex; align-items: center; gap: 8px; flex: 1 1 260px; min-width: 0; background: var(--m3-surface-container-low); padding: 6px 12px; border-radius: var(--radius-sm); }
.rx-search-wrap svg { width: 16px; height: 16px; color: var(--m3-on-surface-muted); flex-shrink: 0; }
.rx-search-wrap .form-input { flex: 1; min-width: 0; background: transparent; padding: 6px 0; }
.rx-scroll-hint { display: none; font-size: 0.72rem; color: var(--m3-on-surface-muted); }
/* Inline row actions (appointments) — wrap on every viewport, stack inside mobile cards */
.rx-actions { display: inline-flex; gap: 6px; flex-wrap: wrap; align-items: center; }

/* =========================================
   RESPONSIVE — Mobile & Tablet (RTL-first)
   Breakpoints: ≤1280 small-laptop, ≤1200 narrow-desktop (explorer stacks),
   ≤1024 tablet-landscape/drawer, ≤768 tablet-portrait,
   ≤640 large-phone, ≤480 small-phone, ≤380 compact.
   Tokens only (var(--*)) — no hex / no hard radius per DESIGN_SYSTEM.
   ========================================= */

/* Base overflow guards — apply on all viewports */
.main-content,
.card-body,
.tab-pane,
.content-wrapper { min-width: 0; }
img, video, svg, canvas { max-width: 100%; }
pre, code, .mono-code, .json-display {
  max-width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  overflow-wrap: break-word;
}
.json-display { white-space: pre; }

/* Drawer trigger — hidden on desktop, CSS controls visibility (no inline style) */
#mobile-menu-btn {
  display: none;
  width: 44px;
  height: 44px;
  flex-shrink: 0;
  background: var(--m3-surface-container);
  color: var(--m3-on-surface);
  align-items: center;
  justify-content: center;
  cursor: pointer;
  border-radius: var(--radius-sm);
}
#mobile-menu-btn svg { width: 22px; height: 22px; }
#mobile-menu-btn:hover { background: var(--m3-surface-container-highest); }
.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(10,10,10,0.5);
  z-index: 900;
}
.sidebar-backdrop.open { display: block; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; min-width: 0; }

/* ---------- Small laptops (≤1280px): gentle density ramp ---------- */
@media (max-width: 1280px) {
  .main-content { padding: 22px 24px 50px; }
}
/* ---------- Narrow desktop / large tablet (≤1200px): stack explorer ---------- */
@media (max-width: 1200px) {
  .fhir-explorer-layout { grid-template-columns: 1fr; }
  .endpoint-list { flex-direction: row; flex-wrap: wrap; }
  .endpoint-btn { flex: 1 1 160px; }
}
/* ---------- Tablet landscape & below: drawer navigation (≤1024px) ---------- */
@media (max-width: 1024px) {
  #mobile-menu-btn { display: inline-flex; }
  .app-layout { flex-direction: column; }
  /* RTL: sidebar lives on the right → anchor right, hide with +X.
     visibility keeps the closed drawer out of tab order / touch hit-testing. */
  .sidebar {
    position: fixed;
    top: 0;
    bottom: 0;
    right: 0;
    left: auto;
    border-radius: 0;
    width: min(300px, 85vw) !important;
    max-width: 85vw;
    height: 100vh;
    height: 100dvh;
    z-index: 1000;
    transform: translateX(100%);
    visibility: hidden;
    transition: transform var(--motion-slow) ease, visibility 0s linear var(--motion-slow);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    padding-top: max(20px, env(safe-area-inset-top));
    padding-bottom: max(20px, env(safe-area-inset-bottom));
  }
  .sidebar.open {
    transform: translateX(0);
    visibility: visible;
    transition: transform var(--motion-slow) ease;
  }
  /* Drawer scrolls on .sidebar itself — same quiet treatment */
  .sidebar {
    scrollbar-width: thin;
    scrollbar-color: transparent transparent;
  }
  .sidebar:hover,
  .sidebar:focus-within {
    scrollbar-color: var(--sidebar-item-hover) transparent;
  }
  .sidebar::-webkit-scrollbar { width: 4px; }
  .sidebar::-webkit-scrollbar-track { background: transparent; }
  .sidebar::-webkit-scrollbar-thumb { background: transparent; border-radius: var(--radius-pill); }
  .sidebar:hover::-webkit-scrollbar-thumb,
  .sidebar:focus-within::-webkit-scrollbar-thumb { background: var(--sidebar-item-hover); }
  /* Collapse toggle is desktop-only — meaningless inside the drawer */
  .sidebar-toggle-btn { display: none; }
  /* Pin logout above the drawer bottom edge: always visible, never lost in the scroll */
  .logout-btn {
    position: sticky;
    bottom: 0;
    background: var(--sidebar-bg);
    border-top: 1px solid rgba(255, 255, 255, 0.06);
    padding-top: 10px;
    margin-top: 8px;
  }
  /* Collapse mode is desktop-only */
  body.sidebar-collapsed .sidebar { width: min(300px, 85vw); padding: 20px 12px; }
  body.sidebar-collapsed .brand-text,
  body.sidebar-collapsed .source-status-badge,
  body.sidebar-collapsed .sidebar-footer { display: block; }
  body.sidebar-collapsed .nav-item { justify-content: flex-start; padding: 10px 14px; gap: 12px; }
  body.sidebar-collapsed .nav-item .label { display: inline; }
  body.sidebar-collapsed .main-content { margin-right: 0; max-width: 100vw; }
  .main-content {
    margin-right: 0 !important;
    max-width: 100vw !important;
    min-width: 0;
    padding: 18px 16px 60px;
    padding-bottom: max(60px, calc(env(safe-area-inset-bottom) + 40px));
  }
  .top-header { flex-wrap: wrap; gap: 12px; align-items: flex-start; }
  .header-title-container { flex: 1 1 240px; min-width: 0; }
  .header-actions { flex-wrap: wrap; flex: 1 1 100%; }
  /* Mid-density tables between desktop and phone */
  .data-table th, .data-table td { padding: 10px 12px; }
  .sources-grid { grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); }
  .active-patient-bar { flex-wrap: wrap; }
  .patient-combobox { width: 100%; max-width: 100%; flex: 1 1 240px; }
  /* Drawer keeps navigation + version; standards recede on touch layouts */
  .standards-badges-grid { display: none; }
  /* Wide clinical tables still scroll on tablets — hint it */
  .rx-scroll-hint { display: inline; }
  .rx-toolbar { padding: 10px 14px; }
}

/* ---------- Tablet portrait (≤768px) ---------- */
@media (max-width: 768px) {
  .top-header {
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
    gap: 12px;
    margin-bottom: 18px;
  }
  /* Menu button stays on the same row as the title (RTL-first) */
  .top-header #mobile-menu-btn { align-self: center; order: 0; }
  .header-title-container { flex: 1 1 calc(100% - 60px); min-width: 0; width: auto; }
  /* Tablets portrait: status pills live in the monitoring pane */
  .header-live-stats { display: none; }
  .header-title-wrap h1 { font-size: 1.25rem; line-height: 1.35; overflow-wrap: break-word; }
  .header-title-wrap p { font-size: 0.8rem; }
  .header-live-stats { flex-wrap: wrap; gap: 6px; }
  .header-actions { width: 100%; display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
  .header-actions .btn { width: 100%; min-height: 44px; padding: 10px 12px; font-size: 0.82rem; }
  .header-session-chip { grid-column: 1 / -1; max-width: 100%; width: 100%; }
  .active-patient-bar {
    flex-direction: column;
    align-items: stretch;
    gap: 12px;
    padding: 12px 14px;
  }
  .patient-bar-info { width: 100%; }
  .patient-bar-actions { width: 100%; }
  .patient-combobox { width: 100%; max-width: 100%; }
  .combobox-dropdown { max-height: 50vh; max-height: 50dvh; }
  .metrics-grid { grid-template-columns: repeat(2, 1fr) !important; gap: 10px; margin-bottom: 18px; }
  .metric-card { padding: 12px; gap: 10px; }
  .metric-value { font-size: 1.25rem; }
  .sources-grid { grid-template-columns: 1fr; gap: 10px; }
  .endpoint-btn { min-height: 44px; }
  .card-header { padding: 12px 14px; }
  .card-body { padding: 12px 14px; }
  .mpi-card-header { flex-wrap: wrap; gap: 10px; }
  .banner-info { padding: 12px 14px; align-items: flex-start; }
  /* Stack any inline 2/3-column form grids (they use inline style=grid-template) */
  div[style*="grid-template-columns:1fr 1fr"],
  div[style*="grid-template-columns: 1fr 1fr"],
  div[style*="grid-template-columns:1fr 1fr 1fr"],
  div[style*="grid-template-columns:2fr 1fr"],
  div[style*="grid-template-columns:1fr 1fr 1.5fr"],
  form[style*="grid-template-columns"] {
    grid-template-columns: 1fr !important;
  }
  .grid-2 { grid-template-columns: 1fr !important; }
  .viewer-header { flex-wrap: wrap; gap: 8px; }
  .viewer-url-wrap { overflow-wrap: anywhere; font-size: 0.74rem; }
}

/* ---------- Large phones (≤640px) ---------- */
@media (max-width: 640px) {
  html { font-size: 14px; }
  .main-content { padding: 14px 12px 60px; }
  /* Prevent iOS auto-zoom: 16px minimum on all text entry */
  .form-input,
  .form-select,
  input[type="text"],
  input[type="search"],
  input[type="number"],
  input[type="email"],
  input[type="tel"],
  input[type="datetime-local"],
  input[type="date"],
  textarea,
  select { font-size: 16px; }
  #global-patient-search { font-size: 16px; }
  .table-card { overflow-x: auto; -webkit-overflow-scrolling: touch; overscroll-behavior-x: contain; }
  .table-card table,
  .data-table { min-width: 600px; }
  .data-table th, .data-table td { padding: 9px 10px; font-size: 0.78rem; }
  .btn, .nav-item, .endpoint-btn { min-height: 44px; }
  .top-header #mobile-menu-btn { width: 44px; height: 44px; flex: 0 0 44px; }
  .top-header { gap: 10px; margin-bottom: 14px; padding-bottom: 10px; justify-content: flex-start; }
  .header-title-wrap h1 {
    font-size: 1.05rem;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }
  #page-subtitle { font-size: 0.75rem; }
  /* Phones: status pills live in the monitoring pane — header keeps title + actions */
  .header-live-stats { display: none; }
  /* Compact identity chip: avatar + name only */
  #header-session-chip { padding: 4px 8px; gap: 6px; }
  #header-session-chip .header-session-text small,
  #header-session-role { display: none; }
  /* Dissolved actions wrapper: everything flows in the header row order,
     icon pair clusters at the row start — no stray grid gaps */
  .header-actions { display: contents; }
  .header-title-container { flex: 1 1 auto; }
  #header-session-chip { flex: 1 1 100%; }
  #btn-run-pipeline { order: 1; flex: 1 1 100%; }
  #btn-theme-toggle { order: 2; flex: 0 0 44px; }
  #btn-refresh { order: 3; flex: 0 0 44px; }
  .header-actions .btn { min-height: 42px; font-size: 0.78rem; }
  /* Icon-only compact actions: icons are universal, truncated labels are not */
  #btn-theme-toggle, #btn-refresh { width: 44px; min-height: 44px; padding: 0; }
  #btn-theme-toggle span, #btn-refresh span { display: none; }
  #btn-theme-toggle .btn-svg-icon, #btn-refresh .btn-svg-icon { width: 18px; height: 18px; }
  .live-pill { font-size: 0.68rem; }
  .metric-card { flex-direction: row; }
  .metric-icon { width: 36px; height: 36px; }
  .section-title { flex-wrap: wrap; gap: 8px; }
  .timeline { padding-right: 22px; }
  .mpi-identifiers-grid { grid-template-columns: 1fr; }
  .file-dropzone { padding: 22px 14px; }
  /* Compact drawer spacing on phones */
  .sidebar { padding: 14px 10px; gap: 12px; }
  .brand { margin-bottom: 10px; }
  .source-status-badge { margin-bottom: 10px; }
  /* Floating layers fit small screens */
  .toast-container {
    top: auto;
    bottom: max(12px, env(safe-area-inset-bottom));
    left: 12px;
    right: 12px;
    max-width: none;
  }
  .toast { width: 100%; }
  .combobox-dropdown { position: absolute; }
  /* Inline fixed modals (auditor) become sheets */
  #auditor-connector-modal > div,
  #auditor-trace-modal > div {
    width: calc(100vw - 24px) !important;
    max-width: 100% !important;
    max-height: 88vh !important;
    max-height: 88dvh !important;
    overflow: auto !important;
    margin: 12px !important;
    padding: 14px !important;
  }
  .report-preview-modal { padding: 8px; }
  .report-preview-sheet { width: 100% !important; max-width: 100% !important; }
  /* Prescriptions & immunizations become stacked cards — no horizontal scroll */
  .rx-toolbar { flex-direction: column; align-items: stretch; }
  .rx-search-wrap { flex: 1 1 100%; }
  .rx-scroll-hint { display: none; }
  .rx-card-body { overflow: visible; padding: 10px !important; background: transparent; }
  .rx-table { min-width: 0 !important; width: 100%; }
  .rx-table thead { display: none; }
  .rx-table, .rx-table tbody { display: block; width: 100%; }
  .rx-table tr { display: block; width: 100%; background: var(--m3-surface-container); border-radius: var(--radius-md); padding: 6px 14px; margin-bottom: 10px; }
  .rx-table tr[style*="background:var(--m3-primary-container)"] > td { background: transparent; }
  .rx-table tr[style*="background:var(--m3-tertiary-container)"] > td { background: transparent; }
  .rx-table td { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; width: 100%; padding: 8px 0; font-size: 0.82rem; background: transparent !important; text-align: left; border-bottom: none !important; }
  .rx-table td::before { content: attr(data-label); font-size: 0.7rem; font-weight: 700; color: var(--m3-on-surface-muted); flex-shrink: 0; max-width: 40%; text-align: right; padding-top: 2px; }
  .rx-table td > * { min-width: 0; }
  .rx-card-body .rx-table tbody td:first-child { position: static; box-shadow: none; background: var(--m3-surface-container-low) !important; margin: 2px -6px 4px; padding: 8px 10px; border-radius: var(--radius-sm); align-items: center; }
  .rx-table td code { overflow-wrap: anywhere; }
  .rx-table td[colspan] { display: block; text-align: center; justify-content: center; }
  .rx-table td[colspan]::before { content: none; }
  /* Unified longitudinal record: patient picker is single column (no 280px blowout) */
  #longitudinal-results { grid-template-columns: 1fr !important; max-height: 60vh; max-height: 60dvh; }
  /* Print action is the primary mobile action — full width touch target */
  #pane-longitudinal #btn-print-patient-summary { flex: 1 1 100%; min-height: 44px; }
  /* Appointments: booking submit is full width, row actions stack */
  #form-book-appointment button[type="submit"] { width: 100%; min-height: 44px; }
  /* Patient access log: actions stack full width above the cards */
  #pane-patient-access .card-body .btn { flex: 1 1 100%; min-height: 44px; }
  /* Patient profile: save/cancel stack full width; self-reported rows wrap */
  #form-edit-profile .btn { flex: 1 1 100%; min-height: 44px; }
  /* NPHIES: eligibility check is the primary mobile action — full width */
  #btn-check-eligibility { flex: 1 1 100%; min-height: 44px; }
  #self-reported-health-card div[style*="space-between"] { flex-wrap: wrap; }
  .rx-table .rx-actions { display: flex; flex: 1 1 auto; min-width: 0; }
  /* Certified report preview: paper grids halve, paper tables scroll */
  .pdf-patient-grid, .pdf-kpi-bar { grid-template-columns: repeat(2, 1fr); }
  .pdf-doc-header, .pdf-section-header { flex-wrap: wrap; gap: 8px; }
  .pdf-section { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .pdf-table { min-width: 560px; }
}

/* ---------- Small phones (≤480px) ---------- */
@media (max-width: 480px) {
  .metrics-grid { grid-template-columns: 1fr 1fr !important; gap: 8px; }
  .metric-card { padding: 10px; }
  .metric-value { font-size: 1.1rem; }
  .metric-label { font-size: 0.7rem; }
  .header-title-wrap h1 { font-size: 1rem; }
  /* Smallest phones: title carries the context alone */
  #page-subtitle { display: none; }
  .card-header h3 { font-size: 0.9rem; }
  .banner-text h4 { font-size: 0.9rem; }
  .banner-text p { font-size: 0.78rem; }
  .auth-modal { padding: 22px 16px 18px; }
  .quick-chips-grid { grid-template-columns: 1fr; }
  .source-details { padding: 10px; }
}

/* ---------- Compact phones (≤380px): single column metrics ---------- */
@media (max-width: 380px) {
  .metrics-grid { grid-template-columns: 1fr !important; }
  .header-live-stats { flex-direction: column; align-items: stretch; }
  .live-pill { justify-content: center; }
}

/* ---------- Coarse pointers: guaranteed touch targets ---------- */
@media (pointer: coarse) {
  .nav-item { min-height: 44px; }
  .btn { min-height: 42px; }
  .endpoint-btn, .combobox-item, .auth-tab-btn { min-height: 44px; }
  .form-input, .form-select { min-height: 44px; }
  input[type="checkbox"], input[type="radio"] { width: 20px; height: 20px; }
  /* Touch has no hover: keep a faint thumb always visible while scrolling */
  .nav-menu, .sidebar, .json-display { scrollbar-color: var(--sidebar-item-hover) transparent; }
  .nav-menu::-webkit-scrollbar-thumb,
  .sidebar::-webkit-scrollbar-thumb,
  .json-display::-webkit-scrollbar-thumb { background: var(--sidebar-item-hover); }
}

/* ---------- Landscape phones: keep content reachable ---------- */
@media (max-height: 500px) and (orientation: landscape) {
  .sidebar { width: min(260px, 70vw) !important; }
  .main-content { padding-top: 12px; }
  .auth-modal { max-height: 94vh; max-height: 94dvh; }
}

/* ---------- Reduced motion ---------- */
@media (prefers-reduced-motion: reduce) {
  html {
    scroll-behavior: auto;
  }
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* =========================================
   GOVERNANCE — unified national center
   Mobile-first comfort: breathing room, soft rows, no squeeze
   ========================================= */
#pane-admin-governance .card-body {
  line-height: 1.7;
}

#pane-admin-governance small {
  line-height: 1.8;
}

/* Layout helpers (replace inline grids — same desktop look, responsive on phones) */
.gov-grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  margin-top: 16px;
}

.gov-filters {
  display: grid;
  gap: 8px;
  margin-bottom: 10px;
}

.gov-filters-4 {
  grid-template-columns: minmax(180px, 2fr) repeat(3, minmax(110px, 1fr));
}

.gov-toolbar {
  display: flex;
  gap: 8px;
  margin-bottom: 8px;
}

.gov-toolbar .form-select {
  min-height: 42px;
}

.gov-list {
  overflow: auto;
  border-radius: var(--radius-md);
  scrollbar-width: thin;
  scrollbar-color: var(--m3-surface-container-highest) transparent;
  -webkit-overflow-scrolling: touch;
}

.gov-list-tall { max-height: 400px; }
.gov-list-medium { max-height: 360px; }
.gov-list-short { max-height: 300px; }

/* Loading state: skeleton bars breathe inside list containers */
.gov-list:has(> .skel) {
  padding: 12px 14px;
}

.gov-pagination {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  padding-top: 10px;
  font-size: 0.78rem;
  color: var(--m3-on-surface-variant);
}

.gov-hint {
  font-size: 0.8rem;
  color: var(--m3-on-surface-variant);
  margin-bottom: 8px;
  line-height: 1.7;
}

.gov-summary {
  font-size: 0.75rem;
  color: var(--m3-on-surface-variant);
  margin-bottom: 6px;
  line-height: 1.7;
}

.gov-form-stack {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

/* JS-rendered rows — desktop stays inline, phones become soft cards */
.gov-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  line-height: 1.7;
  overflow-wrap: anywhere;
}

.gov-row + .gov-row {
  border-top: 1px solid var(--m3-outline-variant);
}

.gov-row-main {
  min-width: 0;
  flex: 1 1 auto;
}

.gov-row-meta {
  display: block;
  margin-top: 2px;
  color: var(--m3-on-surface-variant);
  font-size: 0.78rem;
  line-height: 1.8;
  overflow-wrap: anywhere;
}

.gov-row-actions {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  justify-content: flex-end;
  flex-shrink: 0;
}

.gov-row-actions .btn {
  min-height: 36px;
}

.gov-card-row {
  padding: 10px 12px;
  border: 1px solid var(--m3-outline-variant);
  border-radius: var(--radius-md);
  margin-bottom: 8px;
  background: var(--m3-surface);
  line-height: 1.7;
  overflow-wrap: anywhere;
}

/* ---------- Governance on tablets & phones ---------- */
@media (max-width: 1024px) {
  .gov-grid-2 {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 768px) {
  /* Keep admin cards readable when two-column desktop layouts are inline-styled. */
  #pane-admin-governance > .card,
  #pane-admin-governance .card {
    max-width: 100%;
    min-width: 0;
  }

  #pane-admin-governance .card-header {
    align-items: flex-start;
    flex-wrap: wrap;
  }

  #pane-admin-governance .card-header > button {
    flex: 0 0 auto;
  }

  /* Eye comfort: single-column metrics breathe better than squeezed pairs */
  #pane-admin-governance .metrics-grid {
    grid-template-columns: 1fr 1fr !important;
    gap: 10px;
  }

  /* Legacy inline filter grids (4 columns) → two comfortable columns */
  #pane-admin-governance div[style*="minmax(180px"],
  #pane-admin-governance div[style*="minmax(220px"] {
    grid-template-columns: 1fr 1fr !important;
  }

  .gov-filters-4 {
    grid-template-columns: 1fr 1fr;
  }

  .gov-toolbar {
    flex-wrap: wrap;
  }

  .gov-toolbar .form-select {
    flex: 1 1 140px;
    min-width: 0;
  }

  .gov-toolbar input[type="search"] {
    flex: 1 1 100%;
    min-width: 0;
  }

  /* Comfort reading: rows become stacked soft cards, no side squeeze */
  .gov-row {
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
    padding: 12px 14px;
    background: var(--m3-surface-container-low);
    border: 1px solid transparent;
    border-radius: var(--radius-md);
    margin-bottom: 8px;
  }

  .gov-row + .gov-row {
    border-top: 1px solid transparent;
  }

  .gov-row-actions {
    width: 100%;
    justify-content: stretch;
  }

  .gov-row-actions .btn {
    flex: 1 1 calc(50% - 3px);
    min-height: 44px;
    font-size: 0.82rem;
  }

  .gov-card-row > div[style*="space-between"] {
    flex-direction: column !important;
    align-items: stretch !important;
    gap: 10px !important;
  }

  .gov-card-row div[style*="min-width:90px"],
  .gov-card-row div[style*="display:flex; gap:6px"] {
    width: 100% !important;
    min-width: 0 !important;
    flex-direction: row !important;
    flex-wrap: wrap !important;
  }

  .gov-card-row .btn {
    flex: 1 1 calc(50% - 3px);
    min-height: 44px;
  }

  /* Any remaining legacy flex rows inside the lists */
  #gov-users-list > div,
  #gov-pending-list > div,
  #gov-org-changes-list > div,
  #gov-verify-list > div,
  #gov-patients-list > div {
    min-width: 0;
    overflow-wrap: anywhere;
  }

  #gov-users-list > div {
    align-items: stretch !important;
    flex-direction: column !important;
  }

  #gov-users-list > div > div:last-child {
    width: 100%;
    justify-content: stretch !important;
  }

  #gov-users-list > div > div:last-child .btn {
    flex: 1 1 calc(50% - 3px);
    min-height: 44px;
  }

  #gov-pending-list > div > div,
  #gov-org-changes-list > div,
  #gov-verify-list > div,
  #gov-patients-list > div {
    flex-direction: column !important;
    align-items: stretch !important;
    gap: 10px !important;
  }

  #gov-pending-list .btn,
  #gov-org-changes-list .btn,
  #gov-verify-list .btn {
    flex: 1 1 calc(50% - 3px);
    min-height: 44px;
  }

  /* No nested double-scroll on touch: the page scrolls once, calmly */
  .gov-list,
  .gov-list-tall,
  .gov-list-medium,
  .gov-list-short,
  #gov-users-list,
  #gov-pending-list,
  #gov-org-changes-list,
  #gov-verify-list,
  #gov-patients-list {
    max-height: none !important;
    overflow: visible !important;
  }

  #gov-users-search,
  #gov-org-changes-search,
  #gov-verify-search,
  #gov-patients-search {
    min-width: 0;
    width: 100%;
  }

  #gov-users-pagination,
  #gov-pending-pagination,
  #gov-org-changes-pagination,
  #gov-verify-pagination,
  #gov-patients-pagination,
  .gov-pagination {
    flex-wrap: wrap;
    gap: 8px;
  }

  #gov-users-pagination .btn,
  #gov-pending-pagination .btn,
  #gov-org-changes-pagination .btn,
  #gov-verify-pagination .btn,
  #gov-patients-pagination .btn {
    flex: 1 1 110px;
    min-height: 44px;
  }

  /* Horizontal data remains available without shrinking text to unreadable sizes. */
  #pane-admin-governance .table-wrap,
  #pane-admin-governance .table-card {
    max-width: 100%;
    overflow-x: auto;
  }
}

@media (max-width: 640px) {
  /* One calm column: metrics stop shouting side-by-side on phones */
  #pane-admin-governance .metrics-grid {
    grid-template-columns: 1fr !important;
    gap: 8px;
  }

  #pane-admin-governance .metric-card {
    padding: 14px;
  }

  #pane-admin-governance .metric-value {
    font-size: 1.35rem;
  }
}

@media (max-width: 560px) {
  /* Filters become one full-width column — large, glare-free touch targets */
  #pane-admin-governance div[style*="minmax(180px"],
  #pane-admin-governance div[style*="minmax(220px"] {
    grid-template-columns: 1fr !important;
  }

  .gov-filters-4 {
    grid-template-columns: 1fr;
  }

  .gov-toolbar .form-select {
    flex: 1 1 100%;
  }

  .gov-row-actions .btn,
  #gov-users-list > div > div:last-child .btn,
  .gov-card-row .btn,
  #gov-pending-list .btn,
  #gov-org-changes-list .btn,
  #gov-verify-list .btn {
    flex: 1 1 100%;
  }

  #pane-admin-governance .banner-text p {
    line-height: 1.8;
  }
}

@media (max-width: 480px) {
  .main-content {
    padding-inline: 10px;
  }

  .card {
    border-radius: var(--radius-md);
  }

  .card-header {
    padding: 11px 12px;
  }

  .card-body {
    padding: 11px 12px;
  }

  .card-header h3,
  .card-header-title h3 {
    max-width: 100%;
    overflow-wrap: anywhere;
  }

  .card-header > .btn,
  .card-header > button {
    width: 100%;
  }

  #pane-admin-governance .card-header > .btn,
  #pane-admin-governance .card-header > button {
    min-height: 44px;
  }

  #gov-users-list > div > div:last-child .btn {
    flex: 1 1 100%;
  }

  .form-row,
  .actions-group,
  .header-actions {
    width: 100%;
  }

  .actions-group .btn,
  .form-row .btn {
    flex: 1 1 100%;
    width: 100%;
  }

  /* Avoid clipped fixed-width dialog content on narrow devices. */
  .modal-content,
  .modal-dialog,
  .report-preview-sheet {
    width: calc(100vw - 20px) !important;
    max-width: calc(100vw - 20px) !important;
  }
}
/* ---------- Short viewports: compact sidebar chrome ---------- */
@media (max-height: 750px) {
  .brand { margin-bottom: 10px; }
  .source-status-badge { margin-bottom: 10px; }
  .sidebar-footer { padding-top: 8px; }
  .standards-badges-grid { display: none; }
}

/* =========================================
   SOVEREIGN AUTHENTICATION & LOGIN OVERLAY
   (Borderless High-Contrast Tonal System)
   ========================================= */
.auth-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(10, 10, 10, 0.88);
  display: flex;
  justify-content: center;
  align-items: flex-start;
  z-index: 99999;
  padding: 24px 16px;
  overflow-y: auto;
  overflow-x: hidden;
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  animation: authFadeIn var(--motion-slow) cubic-bezier(0.16, 1, 0.3, 1);
  -webkit-overflow-scrolling: touch;
}

body.light-theme .auth-overlay,
[data-theme="light"] .auth-overlay {
  background: rgba(15, 23, 42, 0.82);
}

@keyframes authFadeIn {
  from { opacity: 0; transform: scale(0.98); }
  to { opacity: 1; transform: scale(1); }
}

.auth-modal {
  background: var(--m3-surface-container);
  border-radius: var(--radius-md);
  width: 100%;
  max-width: 480px;
  padding: 32px 36px 28px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.65);
  position: relative;
  overflow-y: auto;
  overflow-x: hidden;
  max-height: 90vh;
  max-height: 90dvh;
  flex-shrink: 0;
  margin: auto;
  text-align: right;
  scrollbar-width: thin;
  scrollbar-color: var(--m3-surface-container-highest) transparent;
}

.auth-modal::-webkit-scrollbar {
  width: 6px;
}

.auth-modal::-webkit-scrollbar-track {
  background: transparent;
}

.auth-modal::-webkit-scrollbar-thumb {
  background: var(--m3-surface-container-highest);
}

.auth-overlay::-webkit-scrollbar {
  width: 6px;
}

.auth-overlay::-webkit-scrollbar-thumb {
  background: rgba(255,255,255,0.15);
}

body.light-theme .auth-modal,
[data-theme="light"] .auth-modal {
  background: #FFFFFF;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.18);
}

.auth-modal::before {
  content: '';
  position: absolute;
  top: 0;
  right: 0;
  left: 0;
  height: 4px;
  background: var(--m3-primary-light);
  border-start-start-radius: var(--radius-md);
  border-start-end-radius: var(--radius-md);
}

.auth-header {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  margin-bottom: 22px;
}

.auth-emblem-wrap {
  width: auto;
  height: auto;
  background: transparent !important;
  color: var(--m3-error);
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 14px;
  box-shadow: none !important;
  padding: 0;
}

.auth-emblem-svg {
  width: 52px;
  height: 52px;
  min-width: 52px;
  min-height: 52px;
  color: var(--m3-error);
  fill: currentColor;
  flex-shrink: 0;
  -webkit-flex-shrink: 0;
  display: block;
}

.auth-header h2 {
  font-size: 1.35rem;
  font-weight: 800;
  color: var(--m3-on-surface);
  letter-spacing: -0.3px;
  margin-bottom: 4px;
}

.auth-header p {
  color: var(--m3-on-surface-variant);
  font-size: 0.82rem;
  line-height: 1.45;
  margin-bottom: 12px;
}

.auth-security-badge {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  background: var(--m3-surface-container-low);
  padding: 5px 12px;
  border-radius: var(--radius-pill);
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--m3-on-surface-variant);
  font-family: 'JetBrains Mono', 'IBM Plex Sans Arabic', monospace;
}

.auth-form {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.auth-form-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.auth-form-group label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--m3-on-surface-variant);
}

.auth-label-svg {
  width: 14px;
  height: 14px;
  min-width: 14px;
  min-height: 14px;
  color: var(--m3-on-surface-muted);
  flex-shrink: 0;
  -webkit-flex-shrink: 0;
  display: block;
}

.auth-input-wrap {
  position: relative;
  display: flex;
  align-items: center;
}

.auth-input {
  width: 100%;
  padding: 11px 14px;
  background: var(--m3-surface-container-low);
  border-radius: var(--radius-sm);
  color: var(--m3-on-surface);
  font-family: inherit;
  font-size: 0.9rem;
  font-weight: 600;
  outline: none;
  -webkit-appearance: none;
  appearance: none;
  transition: background-color var(--motion-fast) ease, box-shadow var(--motion-fast) ease;
}

.auth-input:focus {
  background: var(--m3-surface-container-high);
  box-shadow: inset 0 -2px 0 0 var(--m3-primary-light);
}

.auth-input::placeholder {
  color: var(--m3-on-surface-muted);
  font-weight: 400;
  font-size: 0.84rem;
}

.auth-submit-btn {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 12px 18px;
  border-radius: var(--radius-sm);
  font-family: inherit;
  font-size: 0.92rem;
  font-weight: 700;
  cursor: pointer;
  margin-top: 6px;
  transition: background-color var(--motion-fast) ease, color var(--motion-fast) ease, transform var(--motion-fast) ease;
  background: #EDEDED;
  color: #121212;
}

.auth-submit-btn:hover {
  background: #FFFFFF;
  color: #000000;
  transform: translateY(-1px);
}

body.light-theme .auth-submit-btn,
[data-theme="light"] .auth-submit-btn {
  background: #0F172A;
  color: #FFFFFF;
}

body.light-theme .auth-submit-btn:hover,
[data-theme="light"] .auth-submit-btn:hover {
  background: #1E293B;
  color: #FFFFFF;
}

.auth-submit-btn svg {
  width: 16px;
  height: 16px;
}

.auth-error-box {
  background: var(--m3-error-container);
  color: var(--m3-on-error-container);
  padding: 10px 14px;
  border-radius: var(--radius-md);
  font-size: 0.78rem;
  font-weight: 600;
  display: none;
  align-items: center;
  gap: 8px;
  margin-top: 4px;
}

.auth-quick-accounts {
  margin-top: 20px;
  padding-top: 16px;
  border-top: 1px solid var(--m3-surface-container-high);
}

.quick-accounts-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 0.74rem;
  color: var(--m3-on-surface-muted);
  font-weight: 600;
  margin-bottom: 8px;
}

.quick-chips-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.quick-chip-btn {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  padding: 8px 10px;
  background: var(--m3-surface-container-low);
  border-radius: var(--radius-sm);
  cursor: pointer;
  text-align: right;
  transition: background-color var(--motion-fast) ease, color var(--motion-fast) ease, transform var(--motion-fast) ease;
  color: var(--m3-on-surface);
}

.quick-chip-btn:hover {
  background: var(--m3-surface-container-highest);
}

.quick-chip-title {
  font-size: 0.76rem;
  font-weight: 700;
  color: var(--m3-on-surface);
}

.quick-chip-desc {
  font-size: 0.68rem;
  font-family: 'JetBrains Mono', monospace;
  color: var(--m3-on-surface-variant);
}

.auth-footer-notice {
  text-align: center;
  font-size: 0.68rem;
  color: var(--m3-on-surface-muted);
  margin-top: 16px;
  line-height: 1.4;
}

/* Auth Modal Internal Grids - Responsive Stacking */
.auth-tabs {
  display: flex;
  gap: 10px;
  margin-bottom: 20px;
}

.auth-tab-btn {
  flex: 1;
  padding: 10px;
  border-radius: var(--radius-sm);
  border: none;
  cursor: pointer;
  font-family: inherit;
  font-weight: 700;
  font-size: 0.88rem;
  transition: background-color var(--motion-fast) ease, color var(--motion-fast) ease;
}

.auth-tab-btn.active {
  background: var(--m3-primary) !important;
  color: var(--m3-on-primary) !important;
}

.auth-tab-btn:not(.active) {
  background: var(--m3-surface-container-low) !important;
  color: var(--m3-on-surface-variant) !important;
}

.auth-demo-links {
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px solid var(--m3-surface-container-high);
}

.auth-demo-links > span {
  font-size: 0.74rem;
  color: var(--m3-on-surface-muted);
  font-weight: 600;
  display: block;
  margin-bottom: 8px;
}

.auth-demo-buttons {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.auth-demo-buttons button {
  flex: 1;
  min-width: 90px;
  padding: 6px 8px;
  font-size: 0.72rem;
  font-weight: 600;
  font-family: inherit;
  background: var(--m3-surface-container-low);
  color: var(--m3-on-surface-variant);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background-color var(--motion-fast) ease;
}

.auth-demo-buttons button:hover {
  background: var(--m3-surface-container-highest);
  color: var(--m3-on-surface);
}

@media (max-width: 560px) {
  .auth-overlay {
    padding: 12px;
    align-items: flex-start;
  }

  .auth-modal {
    max-width: 100%;
    padding: 22px 18px 20px;
    max-height: 96vh;
    max-height: 96dvh;
    margin: 0 auto;
  }

  /* Stack 2-column internal grids to single column */
  #register-patient-trusted-group div[style*="grid-template-columns:1fr 1fr"],
  #register-org-group div[style*="grid-template-columns:1fr 1fr"],
  #register-patient-profile-group div[style*="grid-template-columns:1fr 1fr"],
  #register-patient-profile-group div[style*="grid-template-columns:1fr 1fr 1fr"] {
    grid-template-columns: 1fr !important;
  }

  .auth-demo-buttons {
    flex-direction: column;
  }

  .auth-demo-buttons button {
    width: 100%;
  }
}

@media (max-height: 720px) {
  .auth-modal {
    max-height: 96vh;
    max-height: 96dvh;
  }
}

/* Sidebar Logout Button */
.logout-btn {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--m3-error-bright);
  font-family: inherit;
  font-size: 0.88rem;
  font-weight: 600;
  cursor: pointer;
  text-align: right;
  transition: background-color var(--motion-fast) ease, color var(--motion-fast) ease;
  width: 100%;
  margin-top: 6px;
}

.logout-btn .nav-icon-wrap {
  width: 28px;
  height: 28px;
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  background: color-mix(in srgb, var(--m3-error-bright) 14%, transparent);
  color: var(--m3-error-bright);
  flex-shrink: 0;
}

.logout-btn:hover {
  background: color-mix(in srgb, var(--m3-error-bright) 22%, transparent);
  color: var(--m3-error-bright);
}

body.sidebar-collapsed .logout-btn {
  justify-content: center;
  padding: 9px 0;
}

body.sidebar-collapsed .logout-btn .label {
  display: none;
}

/* ==========================================================================
   SAFARI COMPATIBILITY FIXES (WebKit-specific)
   - Fixes huge crescent SVG bug (flex + SVG sizing in WebKit)
   - Fixes backdrop-filter fallback
   - Fixes gap fallback for older WebKit
   - Fixes -webkit-fill-available for iOS viewport
   ========================================================================== */

/* Safari: ensure fixed SVG sizes are respected inside flex containers */
@supports (-webkit-touch-callout: none) or (-webkit-backdrop-filter: blur(1px)) {
  .crescent-icon,
  .auth-emblem-svg,
  .header-crescent-svg,
  .auth-label-svg,
  .btn-svg-icon,
  .toggle-icon-svg,
  .section-svg,
  .nav-icon-wrap svg,
  .patient-bar-icon,
  .dropzone-icon {
    -webkit-flex-shrink: 0;
    flex-shrink: 0;
    -webkit-flex-grow: 0;
    flex-grow: 0;
    display: block;
  }
  .brand-logo,
  .header-crescent-badge,
  .auth-emblem-wrap {
    -webkit-flex-shrink: 0;
    flex-shrink: 0;
  }
}

/* Safari fallback: if backdrop-filter not supported, use solid background */
@supports not ((-webkit-backdrop-filter: blur(14px)) or (backdrop-filter: blur(14px))) {
  .auth-overlay {
    background: rgba(10, 10, 10, 0.94) !important;
    -webkit-backdrop-filter: none !important;
    backdrop-filter: none !important;
  }
  .report-preview-modal {
    background: rgba(15, 23, 42, 0.94) !important;
    -webkit-backdrop-filter: none !important;
    backdrop-filter: none !important;
  }
}

/* Safari: gap fallback for flex containers (WebKit <14.1) */
@supports not (gap: 1px) {
  .brand-main,
  .header-title-row,
  .header-live-stats,
  .header-actions,
  .nav-menu,
  .auth-tabs,
  .auth-form,
  .auth-form-group,
  .auth-demo-buttons,
  .btn,
  .live-pill,
  .patient-bar-info,
  .source-header,
  .section-title-wrap {
    gap: 0 !important;
  }
  /* Add margins as gap fallback - approximate */
  .brand-main > * + * { margin-right: 10px; }
  .header-actions > * + * { margin-right: 8px; }
  .auth-tabs > * + * { margin-right: 10px; }
}

/* Safari: fix sticky and fixed positioning quirks.
   NOTE: do NOT set transform here — it would override the mobile drawer
   translateX() rules above (equal specificity, later in cascade). */
.sidebar {
  position: fixed;
}
.report-preview-toolbar {
  position: -webkit-sticky;
  position: sticky;
}

/* Safari: fix input zoom on focus (iOS) - ensure 16px minimum to prevent zoom */
@supports (-webkit-touch-callout: none) {
  .auth-input,
  .form-select {
    font-size: 16px;
  }
  @media (min-width: 560px) {
    .auth-input,
    .form-select {
      font-size: 0.9rem;
    }
  }
}

/* Safari: ensure auth-overlay covers viewport correctly on iOS */
@supports (-webkit-touch-callout: none) {
  .auth-overlay {
    min-height: 100vh;
    min-height: -webkit-fill-available;
    height: -webkit-fill-available;
    padding-top: max(24px, env(safe-area-inset-top));
    padding-bottom: max(24px, env(safe-area-inset-bottom));
  }
  .auth-modal {
    max-height: 90vh;
    max-height: -webkit-fill-available;
  }
}

/* Crescent glow retired: flat systematized red via --m3-error (explicit sizes + flex-shrink above already guard WebKit). */
