/**
 * HYDRA Admin — production hardening polish (final pass).
 * Loads last. Scoped: body.use-new-ui #app (+ shared #modal).
 * No redesign — consistency, a11y, interaction safety.
 */

/* ---- Reduced motion ---- */
@media (prefers-reduced-motion: reduce) {
  body.use-new-ui *,
  body.use-new-ui *::before,
  body.use-new-ui *::after,
  #modal *,
  #modal *::before,
  #modal *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

/* ---- Focus visible (keyboard) ---- */
body.use-new-ui :focus:not(:focus-visible),
#modal :focus:not(:focus-visible) {
  outline: none;
}

body.use-new-ui :focus-visible,
#modal :focus-visible {
  outline: 2px solid rgba(59, 130, 246, 0.88);
  outline-offset: 2px;
}

body.use-new-ui button:focus-visible,
body.use-new-ui .btn:focus-visible,
body.use-new-ui a:focus-visible,
body.use-new-ui input:focus-visible,
body.use-new-ui select:focus-visible,
body.use-new-ui textarea:focus-visible {
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.18);
}

/* ---- Shell stacking context ---- */
body.use-new-ui #app {
  isolation: isolate;
  overflow-x: clip;
  max-width: 100%;
}

body.use-new-ui #app .content-scroll {
  overflow-anchor: auto;
}

body.use-new-ui header.topbar {
  transform: translateZ(0);
}

/* ---- Typography / spacing rhythm ---- */
body.use-new-ui #app .section-block,
body.use-new-ui #app .panel.section-block {
  margin-bottom: var(--space-3, 12px);
}

body.use-new-ui #app .page-title-text {
  letter-spacing: -0.02em;
}

/* ---- Buttons: consistent height & baseline ---- */
body.use-new-ui #app .btn:not(.btn-xs):not(.btn-sm):not(.icon-btn):not(.modal-close-btn) {
  min-height: 36px;
  padding-top: 0;
  padding-bottom: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  line-height: 1.2;
  vertical-align: middle;
}

body.use-new-ui #app .btn.btn-xs {
  min-height: 28px;
  font-size: 12px;
}

body.use-new-ui #app .btn.btn-sm {
  min-height: 32px;
  font-size: 13px;
}

body.use-new-ui #app .toolbar .btn,
body.use-new-ui #app .toolbar input,
body.use-new-ui #app .toolbar select {
  flex-shrink: 0;
}

/* ---- Chips / badges ---- */
body.use-new-ui #app .badge,
body.use-new-ui #app .chip,
body.use-new-ui #app .inv-restock-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  line-height: 1.15;
  vertical-align: middle;
}

/* ---- Tables: row rhythm ---- */
body.use-new-ui #app .table-wrap table tbody td,
body.use-new-ui #app .table-wrap table thead th {
  vertical-align: middle;
}

body.use-new-ui #app .table-wrap table tbody tr {
  transition: background-color 120ms ease;
}

@media (min-width: 769px) {
  body.use-new-ui #app .table-wrap table tbody td {
    min-height: 40px;
    padding-top: 8px;
    padding-bottom: 8px;
  }
}

/* ---- Cards: avoid phantom min-height ---- */
body.use-new-ui #app .card:not(.inv-hero-metric):not(.inv-mo-kpi-card) {
  min-height: 0;
}

body.use-new-ui #app .panel:not(.inv-restock-panel) {
  min-height: 0;
}

/* ---- Empty states (enterprise tone) ---- */
body.use-new-ui #app .empty-state,
body.use-new-ui #app .finance-empty-state {
  border-radius: var(--radius-sm, 10px);
}

body.use-new-ui #app .empty-state-title,
body.use-new-ui #app .finance-empty-state-title {
  font-weight: 650;
}

/* ---- Route / section errors ---- */
body.use-new-ui #app .hydra-route-error {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 16px 18px;
  border-radius: var(--radius-sm, 10px);
  border: 1px solid rgba(252, 165, 165, 0.45);
  background: rgba(254, 242, 242, 0.55);
}

body.use-new-ui #app .hydra-route-error .route-error-icon {
  flex-shrink: 0;
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  background: rgba(254, 226, 226, 0.9);
  color: #b91c1c;
  font-size: 14px;
  font-weight: 700;
}

body.use-new-ui #app .hydra-route-error .route-error-title {
  font-size: 15px;
  font-weight: 700;
  color: #0f172a;
  margin: 0 0 4px;
}

body.use-new-ui #app .hydra-route-error .route-error-desc {
  font-size: 13px;
  line-height: 1.45;
  margin: 0 0 10px;
}

body.use-new-ui #app .hydra-route-error .route-error-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

/* ---- Modal polish ---- */
#modal .modal-close-btn {
  min-width: 36px;
  min-height: 36px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

#modal .modal-card {
  max-height: min(88vh, 860px);
}

@media (max-width: 768px) {
  #modal {
    padding: 0;
    align-items: flex-end;
  }

  #modal .modal-card {
    width: 100% !important;
    max-width: 100%;
    max-height: 92vh;
    border-radius: 16px 16px 0 0;
    margin: 0;
  }

  #modal .modal-head {
    padding: 12px 14px 10px;
  }

  #modal #modalForm {
    padding: 10px 14px 0;
    max-height: calc(92vh - 120px);
  }

  #modal .modal-actions {
    padding: 10px 14px calc(10px + env(safe-area-inset-bottom, 0px));
    flex-wrap: wrap;
    justify-content: stretch;
  }

  #modal .modal-actions .btn {
    flex: 1 1 auto;
    min-height: 44px;
  }
}

/* ---- Topbar dropdowns: z-index aligned to contract ---- */
body.use-new-ui header.topbar .topbar-dropdown,
body.use-new-ui header.topbar .user-menu-dropdown,
body.use-new-ui header.topbar .notification-dropdown,
body.use-new-ui header.topbar .quick-add-dropdown,
body.use-new-ui header.topbar .owner-quick-dropdown,
body.use-new-ui .top-search-dropdown {
  z-index: var(--z-dropdown, 3200);
}

/* ---- Loading bar ---- */
.global-loading-bar {
  z-index: var(--z-toast, 12000);
  pointer-events: none;
}

.global-loading-bar.active {
  animation: hydra-loading-pulse 1.1s ease-in-out infinite alternate;
}

@keyframes hydra-loading-pulse {
  from {
    transform: scaleX(0.35);
  }
  to {
    transform: scaleX(0.92);
  }
}

@media (prefers-reduced-motion: reduce) {
  .global-loading-bar.active {
    animation: none;
    transform: scaleX(0.75);
  }
}

/* ---- Mobile admin: touch targets ---- */
@media (max-width: 768px) {
  body.use-new-ui #app .mobile-owner-nav-item,
  body.use-new-ui #app .mobile-topbar-hamburger,
  body.use-new-ui #app .mobile-owner-fab,
  body.use-new-ui #app .sidebar-toggle {
    min-height: 44px;
    min-width: 44px;
  }

  body.use-new-ui #app .toolbar .btn:not(.btn-xs) {
    min-height: 40px;
  }

  body.use-new-ui #app .table-wrap {
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-x: contain;
  }

  body.use-new-ui.mobile-owner-drawer-open #app .content-scroll {
    overflow: hidden;
  }
}

/* ---- Hover: reduce lag on large tables ---- */
@media (hover: hover) and (pointer: fine) {
  body.use-new-ui #app .table-wrap table tbody tr:hover {
    background-color: rgba(248, 250, 252, 0.92);
  }
}

/* ---- Icon-only buttons: ensure hit area ---- */
body.use-new-ui #app button.icon-btn,
body.use-new-ui #app .modal-close-btn {
  touch-action: manipulation;
}

/* ---- Scroll jitter: sticky table headers ---- */
body.use-new-ui #app .table-wrap {
  scroll-behavior: auto;
}

/* ---- Overlay pointer safety ---- */
#modal.hidden,
.command-palette-overlay.hidden,
.command-palette-overlay:not(.is-open),
.orders-bulk-confirm-overlay.hidden,
.sf-inq-bulk-confirm-overlay.hidden,
.dropdown-backdrop:not(.is-active),
.modal-backdrop:not(.is-active) {
  pointer-events: none !important;
}

body.use-new-ui #sfInqFloatingMenu.hidden,
body.use-new-ui .topbar-dropdown.hidden,
body.use-new-ui .notification-dropdown.hidden {
  pointer-events: none !important;
}

body.use-new-ui .mobile-owner-nav-backdrop {
  pointer-events: none;
}

body.use-new-ui.mobile-owner-drawer-open .mobile-owner-nav-backdrop {
  pointer-events: auto;
}

/* ---- Z-index contract (reassert at end of cascade) ---- */
body.use-new-ui header.topbar {
  z-index: var(--z-sticky);
}

body.use-new-ui .command-palette-overlay.is-open {
  z-index: var(--z-overlay);
}

body.use-new-ui #modal:not(.hidden) {
  z-index: var(--z-modal);
}
