/* Admin operations reliability overrides.
 * Loaded before the application bundle so it protects every admin route,
 * including lazy-loaded people and operations modules.
 */
@media (min-width: 1024px) {
  .admin-shell.admin-shell-v2 > .admin-sidebar {
    position: fixed !important;
    inset: 0 auto 0 0 !important;
    z-index: 40 !important;
    width: 286px !important;
    min-width: 286px !important;
    max-width: 286px !important;
  }

  .admin-shell.admin-shell-v2 > main {
    width: calc(100% - 286px) !important;
    margin-left: 286px !important;
  }

  .admin-shell-v2 .admin-sidebar-nav {
    overscroll-behavior: contain;
    scrollbar-width: thin;
  }
}

/*
 * Radix renders Dialog, Select, Popover and Dropdown content in document.body.
 * The protected admin theme normally lives on .admin-shell-v2, so portalled
 * controls used to fall back to public-site variables and could be visually
 * collapsed by public/global rules. Mirror the admin design tokens onto body
 * only while an admin shell is mounted so every portal receives the same
 * theme without changing public pages.
 */
body:has(.admin-shell-v2) {
  --admin-bg: #050505;
  --admin-panel: rgba(12, 12, 12, 0.86);
  --admin-panel-solid: #0b0b0b;
  --admin-panel-raised: rgba(18, 18, 18, 0.92);
  --admin-line: rgba(255, 255, 255, 0.105);
  --admin-line-strong: rgba(255, 255, 255, 0.19);
  --admin-text: #f7f7f7;
  --admin-muted: rgba(255, 255, 255, 0.55);
  --admin-faint: rgba(255, 255, 255, 0.34);
  --admin-hover: rgba(255, 255, 255, 0.055);
  --admin-active: rgba(255, 255, 255, 0.09);
  --background: 0 0% 1.8%;
  --foreground: 0 0% 97%;
  --card: 0 0% 4.5%;
  --card-foreground: 0 0% 97%;
  --popover: 0 0% 5%;
  --popover-foreground: 0 0% 97%;
  --primary: 0 0% 96%;
  --primary-foreground: 0 0% 3%;
  --secondary: 0 0% 7%;
  --secondary-foreground: 0 0% 92%;
  --muted: 0 0% 7%;
  --muted-foreground: 0 0% 57%;
  --accent: 0 0% 9%;
  --accent-foreground: 0 0% 98%;
  --border: 0 0% 14%;
  --input: 0 0% 18%;
  --ring: 0 0% 82%;
  color-scheme: dark;
}

.light body:has(.admin-shell-v2) {
  --admin-bg: #f4f4f2;
  --admin-panel: rgba(255, 255, 255, 0.82);
  --admin-panel-solid: #ffffff;
  --admin-panel-raised: rgba(255, 255, 255, 0.96);
  --admin-line: rgba(0, 0, 0, 0.10);
  --admin-line-strong: rgba(0, 0, 0, 0.18);
  --admin-text: #111111;
  --admin-muted: rgba(0, 0, 0, 0.57);
  --admin-faint: rgba(0, 0, 0, 0.35);
  --admin-hover: rgba(0, 0, 0, 0.045);
  --admin-active: rgba(0, 0, 0, 0.075);
  --background: 60 9% 96%;
  --foreground: 0 0% 7%;
  --card: 0 0% 100%;
  --card-foreground: 0 0% 7%;
  --popover: 0 0% 100%;
  --popover-foreground: 0 0% 7%;
  --primary: 0 0% 8%;
  --primary-foreground: 0 0% 98%;
  --secondary: 0 0% 94%;
  --secondary-foreground: 0 0% 12%;
  --muted: 0 0% 94%;
  --muted-foreground: 0 0% 43%;
  --accent: 0 0% 91%;
  --accent-foreground: 0 0% 8%;
  --border: 0 0% 84%;
  --input: 0 0% 80%;
  --ring: 0 0% 20%;
  color-scheme: light;
}

/* Radix dialogs must remain visible, scrollable and above the admin shell. */
[data-radix-dialog-overlay] {
  position: fixed !important;
  inset: 0 !important;
  z-index: 9998 !important;
}

[data-radix-dialog-content],
[role="dialog"][data-state="open"] {
  position: fixed !important;
  z-index: 9999 !important;
  max-height: min(90dvh, 920px) !important;
  overflow-x: hidden !important;
  overflow-y: auto !important;
  overscroll-behavior: contain;
}

body:has(.admin-shell-v2) [role="dialog"][data-state="open"] {
  color: hsl(var(--foreground)) !important;
  background: hsl(var(--card)) !important;
  border-color: hsl(var(--border)) !important;
  opacity: 1 !important;
  visibility: visible !important;
  isolation: isolate;
}

/*
 * Form-layout guard: a global/public rule must never be able to collapse an
 * admin dialog's Tailwind grid or labels. This directly protects Employee,
 * Customer, Order, Project, Pre-order, Finance and Settings dialogs.
 */
body:has(.admin-shell-v2) [role="dialog"][data-state="open"] .grid {
  display: grid !important;
  height: auto !important;
  min-height: 0 !important;
  opacity: 1 !important;
  visibility: visible !important;
}

body:has(.admin-shell-v2) [role="dialog"][data-state="open"] label {
  position: static !important;
  width: auto !important;
  height: auto !important;
  margin: initial;
  clip: auto !important;
  clip-path: none !important;
  transform: none !important;
  opacity: 1 !important;
  visibility: visible !important;
  overflow: visible !important;
  pointer-events: auto !important;
  display: block !important;
}

body:has(.admin-shell-v2) [role="dialog"][data-state="open"] label.flex {
  display: flex !important;
}

body:has(.admin-shell-v2) [role="dialog"][data-state="open"] label.grid {
  display: grid !important;
}

body:has(.admin-shell-v2) [role="dialog"][data-state="open"] input,
body:has(.admin-shell-v2) [role="dialog"][data-state="open"] textarea,
body:has(.admin-shell-v2) [role="dialog"][data-state="open"] select,
body:has(.admin-shell-v2) [role="dialog"][data-state="open"] [role="combobox"] {
  position: relative !important;
  min-width: 0 !important;
  max-width: 100%;
  opacity: 1 !important;
  visibility: visible !important;
  pointer-events: auto !important;
  color: hsl(var(--foreground)) !important;
  background-color: hsl(var(--background)) !important;
  border-color: hsl(var(--border)) !important;
}

@media (min-width: 768px) {
  [data-radix-dialog-content],
  [role="dialog"][data-state="open"] {
    width: min(760px, calc(100vw - 48px)) !important;
  }
}

@media (max-width: 767px) {
  body:has(.admin-shell-v2) [role="dialog"][data-state="open"] {
    width: calc(100vw - 24px) !important;
    max-width: calc(100vw - 24px) !important;
    max-height: calc(100dvh - 24px) !important;
  }
}

/* Select/Popover/Dropdown portals are body-level siblings of the admin shell. */
body:has(.admin-shell-v2) [data-radix-popper-content-wrapper] {
  z-index: 10020 !important;
  color-scheme: inherit;
}

body:has(.admin-shell-v2) [data-radix-popper-content-wrapper] > * {
  color: hsl(var(--popover-foreground)) !important;
  border-color: hsl(var(--border)) !important;
}

.admin-shell-v2 input,
.admin-shell-v2 textarea,
.admin-shell-v2 button,
.admin-shell-v2 [role="combobox"],
body:has(.admin-shell-v2) [role="dialog"] input,
body:has(.admin-shell-v2) [role="dialog"] textarea,
body:has(.admin-shell-v2) [role="dialog"] button,
body:has(.admin-shell-v2) [role="dialog"] [role="combobox"] {
  min-width: 0;
}
