/**
 * Civet Worksphere - Comprehensive Enterprise Responsive Stylesheet
 * Optimized across 5 distinct viewport tiers:
 *   1. Large Desktop (1440px+)
 *   2. Desktop (1024px - 1439px)
 *   3. Tablet (768px - 1023px)
 *   4. Mobile (480px - 767px)
 *   5. Small Mobile (< 480px)
 */

/* ==========================================================================
   1. ROOT VARIABLES & SAFE AREA INSETS (iOS & Android)
   ========================================================================== */
: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;
}

/* ==========================================================================
   2. GLOBAL RESETS & VIEWPORT OVERFLOW SAFEGUARDS
   ========================================================================== */
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%;
  max-width: 100vw;
}

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

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

/* Avoid unwanted tap callouts on iOS/Android */
button, select, input, a, label {
  -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"],
  select,
  textarea {
    font-size: 16px !important;
  }
}

/* ==========================================================================
   3. RESPONSIVE SIDEBAR & OFF-CANVAS DRAWER NAVIGATION
   ========================================================================== */
.sidebar-overlay {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(3, 7, 18, 0.78);
  backdrop-filter: blur(5px);
  -webkit-backdrop-filter: blur(5px);
  z-index: 9998;
  opacity: 0;
  transition: opacity 0.25s ease;
  pointer-events: none;
}

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

.mobile-topbar {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: var(--mobile-header-height);
  background: rgba(9, 19, 36, 0.97);
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  z-index: 9990;
  padding: 0 14px;
  align-items: center;
  justify-content: space-between;
}

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

.sidebar-mobile-close {
  display: none;
  position: absolute;
  top: 10px;
  right: 10px;
  background: rgba(255, 255, 255, 0.08);
  border: 0;
  color: #f8fafc;
  font-size: 16px;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  z-index: 10;
}

@media (max-width: 1023px) {
  .sidebar-mobile-close {
    display: flex !important;
  }
}

/* ==========================================================================
   4. BREAKPOINT TIER RULES
   ========================================================================== */

/* --------------------------------------------------------------------------
   TIER 1: LARGE DESKTOP (1440px+)
   -------------------------------------------------------------------------- */
@media (min-width: 1440px) {
  #am_add_asset_form > div[style*="grid-template-columns"],
  .pnm-form-grid-4 {
    grid-template-columns: repeat(4, minmax(0, 1fr)) !important;
    gap: 24px !important;
  }
}

/* --------------------------------------------------------------------------
   TIER 2: STANDARD DESKTOP (1024px - 1439px)
   -------------------------------------------------------------------------- */
@media (min-width: 1024px) and (max-width: 1439px) {
  #am_add_asset_form > div[style*="grid-template-columns"],
  .pnm-form-grid-3 {
    grid-template-columns: repeat(3, minmax(0, 1fr)) !important;
    gap: 20px !important;
  }
}

/* --------------------------------------------------------------------------
   TIER 3: TABLET (768px - 1023px)
   -------------------------------------------------------------------------- */
@media (min-width: 768px) and (max-width: 1023px) {
  .mobile-topbar {
    display: flex !important;
  }

  /* Main workspace padding */
  .main-content,
  .app-main,
  #contentArea,
  .dashboard-content,
  .pnm-workspace {
    margin-left: 0 !important;
    padding-top: calc(var(--mobile-header-height) + 16px) !important;
    padding-left: 16px !important;
    padding-right: 16px !important;
    padding-bottom: 24px !important;
    width: 100% !important;
    max-width: 100vw !important;
  }

  /* 2-Column form grid on Tablet */
  #am_add_asset_form > div[style*="grid-template-columns"],
  .pnm-form-grid,
  .form-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    gap: 16px !important;
  }
}

/* --------------------------------------------------------------------------
   TIER 4 & 5: ALL MOBILE DEVICES (< 768px)
   -------------------------------------------------------------------------- */
@media (max-width: 767px) {
  .mobile-topbar {
    display: flex !important;
  }

  /* Full screen content area */
  .main-content,
  .app-main,
  #contentArea,
  .dashboard-content,
  .pnm-workspace {
    margin-left: 0 !important;
    padding-top: calc(var(--mobile-header-height) + 12px) !important;
    padding-left: 12px !important;
    padding-right: 12px !important;
    padding-bottom: 24px !important;
    width: 100% !important;
    max-width: 100vw !important;
  }

  /* Single Column form grid on Mobile */
  #am_add_asset_form > div[style*="grid-template-columns"],
  .pnm-form-grid,
  .form-grid,
  .grid-cols-2,
  .grid-cols-3,
  .grid-cols-4 {
    grid-template-columns: 1fr !important;
    gap: 14px !important;
  }

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

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

  /* Header action bar stacking */
  .pnm-workspace > div > div[style*="justify-content:space-between"],
  div[style*="justify-content: space-between"] {
    flex-direction: column !important;
    align-items: flex-start !important;
    gap: 12px !important;
  }

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

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

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

/* --------------------------------------------------------------------------
   TIER 5: SMALL MOBILE SCREENS (< 480px, iPhone SE, compact Android)
   -------------------------------------------------------------------------- */
@media (max-width: 479px) {
  .pnm-workspace {
    padding-left: 8px !important;
    padding-right: 8px !important;
  }

  .pnm-workspace > div {
    padding: 16px 12px !important;
    border-radius: 12px !important;
  }

  h2 {
    font-size: 18px !important;
  }

  label {
    font-size: 11px !important;
  }
}

/* ==========================================================================
   5. RESPONSIVE TABLES & HORIZONTAL SCROLL CONTAINERS
   ========================================================================== */
.table-responsive,
div[style*="overflow-x:auto"],
.overflow-x-auto,
.pnm-table-container,
.frame-box {
  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;
}

.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;
}

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

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

/* ==========================================================================
   6. RESPONSIVE MODALS & DIALOGS
   ========================================================================== */
#am_delete_modal,
#am_edit_modal,
.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: 16px !important;
  margin: 0 !important;
}

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

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

#am_delete_modal > div,
#am_edit_modal > div,
.modal-box,
.pnm-modal-content,
div[style*="max-width:900px"],
div[style*="max-width:800px"],
div[style*="max-width:700px"] {
  width: 100% !important;
  max-width: 480px !important;
  margin: auto !important;
  box-sizing: border-box !important;
  border-radius: 16px !important;
  max-height: 90vh !important;
  max-height: 90dvh !important;
  overflow-y: auto !important;
  -webkit-overflow-scrolling: touch !important;
}

@media (min-width: 769px) {
  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; }
  #am_edit_modal > div { max-width: 900px !important; }
}

/* ==========================================================================
   7. P&M NESTED HIERARCHY INDENTATION & TOUCH COMFORT
   ========================================================================== */
.erp-pnm-group-items,
.erp-settings-group-items {
  padding-left: 8px !important;
}

.erp-subitem {
  min-height: 38px !important;
  display: flex !important;
  align-items: center !important;
  gap: 8px !important;
  padding: 6px 12px !important;
  border-radius: 8px !important;
  margin: 2px 0 !important;
  font-size: 13px !important;
}

.erp-subitem.active {
  background: rgba(56, 189, 248, 0.16) !important;
  color: #38bdf8 !important;
  font-weight: 700 !important;
}
