/**
 * Civet WorkSphere — Master Enterprise Responsive Stylesheet
 * 
 * Precision-engineered for all device viewports:
 *   1. Ultra-Wide & Large Desktop (≥ 1440px / 1600px / 1920px)
 *   2. Standard Desktop & Small Laptops (1024px – 1439px)
 *   3. Tablet (iPad Pro, iPad Air, iPad Mini: 768px – 1023px)
 *   4. Mobile & Phablet (iPhone 15/16 Pro Max, Plus, Android: 480px – 767px)
 *   5. Compact Mobile (iPhone SE, Android: 320px – 479px)
 *   6. Landscape Mobile & Tablet (@media (orientation: landscape) and (max-height: 550px))
 */

/* ==========================================================================
   1. ROOT VARIABLES, SAFE AREA INSETS & TOKENS
   ========================================================================== */
:root {
  --sat: env(safe-area-inset-top, 0px);
  --sar: env(safe-area-inset-right, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
  
  --mobile-header-height: 56px;
  --touch-target-min: 44px;
  --civet-drawer-width: min(290px, 84vw);
  
  --glass-bg-mobile: rgba(9, 19, 36, 0.92);
  --glass-border-mobile: rgba(255, 255, 255, 0.12);
  --glass-blur-mobile: 14px;
  
  --solid-bg-mobile: #091324;
  --solid-border-mobile: #1e293b;
}

/* ==========================================================================
   2. GLOBAL RESETS, FLUID VIEWPORT & ZERO HORIZONTAL OVERFLOW
   ========================================================================== */
html {
  -webkit-text-size-adjust: 100%;
  -ms-text-size-adjust: 100%;
  text-size-adjust: 100%;
  box-sizing: border-box;
  overflow-x: hidden !important;
  width: 100% !important;
  max-width: 100vw !important;
  min-width: 0 !important;
  scroll-behavior: smooth;
}

*, *::before, *::after {
  box-sizing: inherit;
}

body {
  overflow-x: hidden !important;
  width: 100% !important;
  max-width: 100vw !important;
  min-width: 0 !important;
  min-height: 100vh;
  min-height: 100dvh;
  padding-top: 0;
  padding-bottom: var(--sab);
  padding-left: var(--sal);
  padding-right: var(--sar);
  margin: 0;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  background-color: #0b1526;
  color: #e2e8f0;
}

/* Prevent unwanted tap highlights on touch devices */
button, select, input, a, label, .clickable {
  -webkit-tap-highlight-color: transparent;
}

/* Base input text sizing to prevent iOS Safari auto-zoom */
@media screen and (max-width: 768px) {
  input[type="text"],
  input[type="number"],
  input[type="email"],
  input[type="password"],
  input[type="date"],
  input[type="datetime-local"],
  input[type="tel"],
  input[type="search"],
  input[type="file"],
  select,
  textarea {
    font-size: 16px !important;
  }
}

/* Responsive Image and Logo Behavior */
img, svg, video, canvas {
  max-width: 100%;
  height: auto;
}

/* ==========================================================================
   3. MOBILE TOPBAR & OFF-CANVAS DRAWER SYSTEM (<= 1023px)
   ========================================================================== */
.mobile-topbar {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  width: 100% !important;
  max-width: 100vw !important;
  height: calc(var(--mobile-header-height) + var(--sat));
  padding-top: var(--sat);
  background: var(--glass-bg-mobile);
  border-bottom: 1px solid var(--glass-border-mobile);
  z-index: 9990;
  padding-left: max(10px, var(--sal));
  padding-right: max(10px, var(--sar));
  align-items: center;
  justify-content: space-between;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.45);
  box-sizing: border-box;
}

.mobile-topbar-brand {
  display: flex !important;
  align-items: center !important;
  justify-content: center !important;
  cursor: pointer;
  user-select: none;
  height: 100%;
}

.mobile-topbar-brand img,
#adminMobileTopbar img {
  height: 38px !important;
  max-height: 42px !important;
  width: auto !important;
  max-width: 160px !important;
  object-fit: contain !important;
  filter: drop-shadow(0 2px 8px rgba(0, 0, 0, 0.45)) !important;
  transition: transform 0.2s ease;
}

.mobile-topbar-brand:active img {
  transform: scale(0.96);
}

.mobile-nav-toggle {
  background: transparent;
  border: none;
  color: #f8fafc;
  font-size: 20px;
  width: var(--touch-target-min);
  height: var(--touch-target-min);
  min-width: var(--touch-target-min);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  border-radius: 8px;
  padding: 0;
  margin: 0;
  transition: background 0.2s ease, color 0.2s ease;
}

.mobile-nav-toggle:active {
  background: rgba(255, 255, 255, 0.12);
  color: #38bdf8;
}

/* Off-Canvas Backdrop Overlay */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  background: rgba(2, 6, 18, 0.75);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  z-index: 9998;
  opacity: 0;
  transition: opacity 0.25s cubic-bezier(0.4, 0, 0.2, 1);
  pointer-events: none;
}

.sidebar-overlay.active {
  display: block !important;
  opacity: 1 !important;
  pointer-events: auto !important;
}

/* Mobile Close Button Inside Sidebar Drawer */
.sidebar-mobile-close {
  display: none;
  position: absolute;
  top: max(10px, var(--sat));
  right: 12px;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.14);
  color: #f8fafc;
  font-size: 15px;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  z-index: 10;
  transition: all 0.2s ease;
}

.sidebar-mobile-close:hover,
.sidebar-mobile-close:active {
  background: rgba(239, 68, 68, 0.25);
  color: #f87171;
  border-color: rgba(239, 68, 68, 0.5);
}

/* ==========================================================================
   4. RESPONSIVE BREAKPOINT RULES (TABLETS & MOBILE <= 1023px)
   ========================================================================== */
@media (max-width: 1023px) {
  /* Show mobile topbar, hide desktop topbar */
  .mobile-topbar {
    display: flex !important;
  }

  .topbar {
    display: none !important;
  }

  .sidebar-mobile-close {
    display: flex !important;
  }

  /* Off-Canvas Drawer Transformation */
  aside#adminSidebar,
  aside#mainSidebar,
  aside#approverSidebar,
  aside.sidebar,
  [data-erp-drawer] {
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    bottom: 0 !important;
    height: 100vh !important;
    height: 100dvh !important;
    width: var(--civet-drawer-width) !important;
    max-width: 320px !important;
    min-width: 0 !important;
    z-index: 9999 !important;
    transform: translate3d(-100%, 0, 0) !important;
    transition: transform 0.28s cubic-bezier(0.2, 0.8, 0.2, 1) !important;
    box-shadow: 6px 0 28px rgba(0, 0, 0, 0.75) !important;
    visibility: visible !important;
    display: flex !important;
    flex-direction: column !important;
    background: #091324 !important;
    padding-top: var(--sat);
    padding-bottom: var(--sab);
    margin: 0 !important;
    overflow-y: auto !important;
    overflow-x: hidden !important;
  }

  /* Active Drawer Slide-In */
  aside#adminSidebar.mobile-open,
  aside#mainSidebar.mobile-open,
  aside#approverSidebar.mobile-open,
  aside.sidebar.mobile-open,
  [data-erp-drawer].mobile-open,
  [data-erp-drawer].erp-drawer-open {
    transform: translate3d(0, 0, 0) !important;
  }

  /* Reveal all labels, text, search, and chevrons inside Mobile Drawer */
  aside#adminSidebar .erp-module,
  aside.sidebar .erp-module {
    width: 100% !important;
    display: flex !important;
    flex-direction: column !important;
    margin: 0 0 2px 0 !important;
  }

  aside#adminSidebar .erp-module-toggle,
  aside.sidebar .erp-module-toggle {
    width: 100% !important;
    max-width: 100% !important;
    min-width: 0 !important;
    height: auto !important;
    min-height: 40px !important;
    padding: 8px 12px !important;
    justify-content: flex-start !important;
    display: flex !important;
    align-items: center !important;
  }

  aside#adminSidebar .erp-module-toggle span,
  aside.sidebar .erp-module-toggle span,
  aside#adminSidebar .erp-module-toggle .module-chevron,
  aside.sidebar .erp-module-toggle .module-chevron,
  aside#adminSidebar #sidebarSearchWrapper,
  aside#adminSidebar .sidebar-logo img,
  aside#adminSidebar .sidebar-tagline {
    display: inline-block !important;
    visibility: visible !important;
    opacity: 1 !important;
    width: auto !important;
    height: auto !important;
  }

  aside#adminSidebar .erp-module.expanded > .erp-subnav,
  aside.sidebar .erp-module.expanded > .erp-subnav {
    display: flex !important;
    flex-direction: column !important;
    opacity: 1 !important;
    visibility: visible !important;
    max-height: 600px !important;
    width: 100% !important;
    height: auto !important;
    padding-left: 12px !important;
  }

  /* Main Workspace Takes 100% Full Width (Zero Margin) */
  main,
  .main,
  #mainContent,
  .main-wrapper,
  .main-content,
  .app-main,
  .content,
  #contentArea,
  #tab-dashboard,
  .tab-pane,
  .tab-content,
  .subtab-pane,
  .pnm-workspace {
    margin-left: 0 !important;
    left: 0 !important;
    width: 100% !important;
    max-width: 100vw !important;
    min-width: 0 !important;
    box-sizing: border-box !important;
    overflow-x: hidden !important;
  }

  .main,
  .main-wrapper,
  .main-content,
  .app-main {
    padding-top: calc(var(--mobile-header-height) + var(--sat)) !important;
  }

  .content {
    padding: 12px 12px 24px !important;
  }

  /* Charts and Sub-grids Single Column */
  .dashboard-grid-2col,
  div[style*="grid-template-columns:1.2fr"],
  div[style*="grid-template-columns: 1.2fr"] {
    grid-template-columns: 1fr !important;
    gap: 14px !important;
  }

  /* Body scroll lock during drawer open */
  body.drawer-open {
    overflow: hidden !important;
    touch-action: none !important;
  }
}

/* --------------------------------------------------------------------------
   TABLET TIER (768px – 1023px: iPad Pro, Air, Mini)
   -------------------------------------------------------------------------- */
@media (min-width: 768px) and (max-width: 1023px) {
  #am_add_asset_form > div[style*="grid-template-columns"],
  #pnm_usage_form,
  .pnm-assets-grid,
  .pnm-form-grid,
  .form-grid,
  .grid-cols-2,
  .grid-cols-3,
  .grid-cols-4 {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    gap: 16px !important;
  }

  div[style*="grid-template-columns:repeat(auto-fit,minmax(190px,1fr))"],
  div[style*="grid-template-columns:repeat(auto-fit, minmax(190px, 1fr))"],
  div[style*="grid-template-columns:repeat(auto-fit, minmax(200px, 1fr))"] {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    gap: 12px !important;
  }
}

/* --------------------------------------------------------------------------
   ALL MOBILE DEVICES (< 768px: iPhone 15/16 Pro Max, Plus, SE, Android)
   -------------------------------------------------------------------------- */
@media (max-width: 767px) {
  .content,
  .pnm-workspace {
    padding-left: max(10px, var(--sal)) !important;
    padding-right: max(10px, var(--sar)) !important;
    padding-bottom: max(20px, var(--sab)) !important;
    width: 100% !important;
    max-width: 100vw !important;
    box-sizing: border-box !important;
  }

  /* 1-Column Responsive Forms */
  #am_add_asset_form > div[style*="grid-template-columns"],
  #pnm_usage_form,
  .pnm-assets-grid,
  .pnm-form-grid,
  .form-grid,
  .grid-cols-2,
  .grid-cols-3,
  .grid-cols-4,
  div[style*="grid-template-columns:repeat(auto-fit, minmax(240px, 1fr))"],
  div[style*="grid-template-columns:repeat(auto-fit, minmax(250px, 1fr))"],
  div[style*="grid-template-columns:repeat(auto-fit, minmax(270px, 1fr))"],
  div[style*="grid-template-columns:repeat(auto-fit,minmax(270px,1fr))"],
  div[style*="grid-template-columns:repeat(auto-fit, minmax(280px, 1fr))"] {
    grid-template-columns: 1fr !important;
    gap: 12px !important;
  }

  /* Responsive KPI Metric Cards (Strictly 2 per row on mobile) */
  .kpi-cards-grid,
  div[style*="grid-template-columns:repeat(auto-fit,minmax(190px,1fr))"],
  div[style*="grid-template-columns:repeat(auto-fit, minmax(190px, 1fr))"],
  div[style*="grid-template-columns:repeat(auto-fit, minmax(170px, 1fr))"],
  div[style*="grid-template-columns:repeat(auto-fit, minmax(180px, 1fr))"],
  div[style*="grid-template-columns:repeat(auto-fit, minmax(200px, 1fr))"] {
    display: grid !important;
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    gap: 10px !important;
  }

  .card-stat,
  .panel,
  .pnm-card {
    width: 100% !important;
    max-width: 100% !important;
    box-sizing: border-box !important;
    padding: 14px 12px !important;
    border-radius: 12px !important;
  }

  .card-stat {
    display: flex !important;
    flex-direction: column !important;
    justify-content: space-between !important;
    overflow: hidden !important;
    box-shadow: 0 4px 14px rgba(0, 0, 0, 0.25) !important;
  }

  .card-stat > div:first-child {
    font-size: 11px !important;
    font-weight: 800 !important;
    letter-spacing: 0.03em !important;
    text-transform: uppercase !important;
    line-height: 1.25 !important;
    margin-bottom: 2px !important;
    word-break: normal !important;
    overflow-wrap: break-word !important;
  }

  .card-stat div[style*="font-size:38px"],
  .card-stat div[style*="font-size:34px"],
  .card-stat div[style*="font-size:30px"],
  .card-stat .stat-value {
    font-size: 24px !important;
    font-weight: 900 !important;
    line-height: 1.15 !important;
    margin: 4px 0 !important;
    letter-spacing: -0.02em !important;
  }

  .card-stat > div:last-child {
    font-size: 11px !important;
    line-height: 1.25 !important;
    color: #cbd5e1 !important;
    font-weight: 500 !important;
    word-break: normal !important;
    overflow-wrap: break-word !important;
  }

  /* Form Control Touch Target Sizes */
  input[type="text"],
  input[type="number"],
  input[type="email"],
  input[type="password"],
  input[type="date"],
  input[type="file"],
  select,
  textarea,
  .pnm-input,
  .pnm-select {
    width: 100% !important;
    max-width: 100% !important;
    min-height: var(--touch-target-min) !important;
    border-radius: 10px !important;
    box-sizing: border-box !important;
  }

  /* Responsive Buttons */
  .pnm-btn,
  .btn,
  button[type="submit"],
  button[type="button"] {
    min-height: var(--touch-target-min) !important;
    border-radius: 8px !important;
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  /* Action Toolbars Stack */
  .pnm-workspace > div > div[style*="justify-content:space-between"],
  div[style*="justify-content:space-between"],
  div[style*="justify-content: space-between"] {
    flex-direction: column !important;
    align-items: stretch !important;
    gap: 10px !important;
  }

  .pnm-workspace > div > div[style*="justify-content:space-between"] button,
  div[style*="justify-content: space-between"] button {
    width: 100% !important;
  }

  /* Form Action Footer Buttons Stack */
  .form-actions,
  div[style*="justify-content:flex-end"] {
    flex-direction: column-reverse !important;
    width: 100% !important;
    gap: 8px !important;
  }

  div[style*="justify-content:flex-end"] button {
    width: 100% !important;
  }

  /* Fluid Typography */
  h1, h2, .section-title {
    font-size: clamp(17px, 5vw, 22px) !important;
    line-height: 1.3 !important;
  }
  
  h3, h4 {
    font-size: clamp(14px, 4vw, 17px) !important;
  }
}

/* --------------------------------------------------------------------------
   COMPACT MOBILE SCREENS (< 480px, iPhone SE, compact Android)
   -------------------------------------------------------------------------- */
@media (max-width: 479px) {
  .content,
  .pnm-workspace {
    padding-left: max(6px, var(--sal)) !important;
    padding-right: max(6px, var(--sar)) !important;
  }

  .kpi-cards-grid,
  div[style*="grid-template-columns:repeat(auto-fit,minmax(190px,1fr))"],
  div[style*="grid-template-columns:repeat(auto-fit, minmax(190px, 1fr))"],
  div[style*="grid-template-columns:repeat(auto-fit, minmax(170px, 1fr))"],
  div[style*="grid-template-columns:repeat(auto-fit, minmax(180px, 1fr))"],
  div[style*="grid-template-columns:repeat(auto-fit, minmax(200px, 1fr))"] {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    gap: 8px !important;
  }

  .card-stat {
    padding: 12px 10px !important;
    border-radius: 10px !important;
  }

  .card-stat > div:first-child {
    font-size: 10px !important;
    line-height: 1.2 !important;
  }

  .card-stat div[style*="font-size:38px"],
  .card-stat div[style*="font-size:34px"],
  .card-stat div[style*="font-size:30px"],
  .card-stat .stat-value {
    font-size: 20px !important;
    margin: 2px 0 !important;
  }

  .card-stat > div:last-child {
    font-size: 10px !important;
    line-height: 1.2 !important;
  }
}

/* ==========================================================================
   5. RESPONSIVE TABLES & HORIZONTAL SCROLL CONTAINERS
   ========================================================================== */
.table-responsive,
div[style*="overflow-x:auto"],
.overflow-x-auto,
.pnm-table-container,
.frame-box,
.custom-table-wrapper {
  overflow-x: auto !important;
  -webkit-overflow-scrolling: touch !important;
  max-width: 100% !important;
  width: 100% !important;
  box-sizing: border-box !important;
  border-radius: 12px;
  position: relative;
  scrollbar-width: thin;
}

.table-responsive::-webkit-scrollbar,
div[style*="overflow-x:auto"]::-webkit-scrollbar,
.overflow-x-auto::-webkit-scrollbar {
  height: 6px;
}

.table-responsive::-webkit-scrollbar-track,
div[style*="overflow-x:auto"]::-webkit-scrollbar-track,
.overflow-x-auto::-webkit-scrollbar-track {
  background: rgba(15, 23, 42, 0.6);
  border-radius: 999px;
}

.table-responsive::-webkit-scrollbar-thumb,
div[style*="overflow-x:auto"]::-webkit-scrollbar-thumb,
.overflow-x-auto::-webkit-scrollbar-thumb {
  background: rgba(56, 189, 248, 0.4);
  border-radius: 999px;
}

/* Sticky Action Column Pinning */
.civet-sticky-action-col,
th[style*="position:sticky;right:0"],
td[style*="position:sticky;right:0"] {
  position: sticky !important;
  right: 0 !important;
  background: #091324 !important;
  z-index: 5 !important;
  box-shadow: -4px 0 10px rgba(0, 0, 0, 0.4) !important;
}

@media (max-width: 768px) {
  table {
    font-size: 12px !important;
  }

  th, td {
    padding: 8px 10px !important;
    white-space: nowrap !important;
  }
}

/* ==========================================================================
   6. RESPONSIVE MODALS & DIALOGS
   ========================================================================== */
.modal,
.civet-modal,
[role="dialog"]:not(.civet-date-popup):not(.flatpickr-calendar),
.modal-backdrop,
.modal-bg,
.pnm-modal-bg {
  position: fixed !important;
  inset: 0 !important;
  width: 100vw !important;
  max-width: 100vw !important;
  height: 100vh !important;
  height: 100dvh !important;
  z-index: 10000 !important;
  box-sizing: border-box !important;
  padding: max(12px, var(--sat)) max(12px, var(--sar)) max(12px, var(--sab)) max(12px, var(--sal)) !important;
  margin: 0 !important;
}

.modal-bg:not(.open),
.pnm-modal-bg:not(.open) {
  display: none !important;
}

.modal-bg.open,
.pnm-modal-bg.open {
  display: flex !important;
  align-items: center !important;
  justify-content: center !important;
}

.modal-dialog,
.civet-modal-content,
.modal-box,
.pnm-modal-content,
#am_delete_modal > div,
#am_edit_modal > div {
  width: 92% !important;
  margin: auto !important;
  box-sizing: border-box !important;
  border-radius: 16px !important;
  max-height: calc(100dvh - 32px) !important;
  overflow-y: auto !important;
  -webkit-overflow-scrolling: touch !important;
}

#am_delete_modal > div {
  max-width: 480px !important;
}

@media (max-width: 768px) {
  .modal-dialog,
  .civet-modal-content,
  .modal-box,
  .pnm-modal-content,
  #am_delete_modal > div,
  #am_edit_modal > div {
    width: min(100%, calc(100vw - 20px)) !important;
    max-width: calc(100vw - 20px) !important;
  }
}

@media (min-width: 769px) {
  #am_delete_modal > div { max-width: 480px !important; }
  #am_edit_modal > div { max-width: 900px !important; }
  div[style*="max-width:900px"] { max-width: 900px !important; }
  div[style*="max-width:800px"] { max-width: 800px !important; }
  div[style*="max-width:700px"] { max-width: 700px !important; }
}

/* ==========================================================================
   7. PERFORMANCE & ACCESSIBILITY
   ========================================================================== */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}
