:root {
  --mobile-safe-top: env(safe-area-inset-top, 0px);
  --mobile-safe-right: env(safe-area-inset-right, 0px);
  --mobile-safe-bottom: env(safe-area-inset-bottom, 0px);
  --mobile-safe-left: env(safe-area-inset-left, 0px);
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

button,
input,
select,
textarea {
  touch-action: manipulation;
}

/* Desktop/laptop: nine user controls arranged as two stable rows. */
.wideAdmin,
.wideAdmin > *,
.userAdminList,
.userAdminRow {
  min-width: 0 !important;
}

.userAdminRow {
  grid-template-columns: minmax(160px, 1.15fr) repeat(3, minmax(140px, 1fr)) minmax(105px, .65fr) !important;
  align-items: center !important;
}

.userAdminRow > * {
  min-width: 0 !important;
  max-width: 100% !important;
}

.userAdminRow > :nth-child(6) { grid-column: 1; }
.userAdminRow > :nth-child(7) { grid-column: 2 / span 2; }
.userAdminRow > :nth-child(8) { grid-column: 4; }
.userAdminRow > :nth-child(9) { grid-column: 5; }

/* Keep document actions visible while the data columns scroll. */
.kgshTable th:last-child,
.kgshTable td:last-child {
  position: sticky;
  right: 0;
  z-index: 3;
  min-width: 132px;
  background: var(--card, #fff) !important;
  box-shadow: -10px 0 16px rgba(15, 23, 42, .08);
}

.kgshTable th:last-child {
  z-index: 4;
  background: var(--input, #f8fbfd) !important;
}

.kgshTable td:last-child .kgshBtn {
  width: 100%;
  white-space: normal;
}

@media (max-width: 1600px) and (min-width: 901px) {
  .wideAdmin {
    grid-template-columns: 290px minmax(0, 1fr) !important;
  }

  .userAdminRow {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
  }

  .userAdminRow > * {
    grid-column: auto !important;
  }

  .userAdminRow > :first-child,
  .userAdminRow > :nth-child(7) {
    grid-column: 1 / -1 !important;
  }

  .kgshTable {
    width: 100% !important;
    min-width: 0 !important;
    table-layout: fixed !important;
  }

  .kgshTable th,
  .kgshTable td {
    padding: 8px 7px !important;
    overflow-wrap: anywhere;
  }

  .kgshTable th:nth-child(1), .kgshTable td:nth-child(1) { width: 15%; }
  .kgshTable th:nth-child(2), .kgshTable td:nth-child(2) { width: 20%; }
  .kgshTable th:nth-child(3), .kgshTable td:nth-child(3) { width: 14%; }
  .kgshTable th:nth-child(4), .kgshTable td:nth-child(4) { width: 10%; }
  .kgshTable th:nth-child(5), .kgshTable td:nth-child(5) { width: 10%; }
  .kgshTable th:nth-child(6), .kgshTable td:nth-child(6) { display: none; }
  .kgshTable th:nth-child(7), .kgshTable td:nth-child(7) { width: 14%; }
  .kgshTable th:nth-child(8), .kgshTable td:nth-child(8) { width: 7%; }
  .kgshTable th:nth-child(9), .kgshTable td:nth-child(9) { width: 10%; }

  .kgshTable th:last-child,
  .kgshTable td:last-child {
    position: static !important;
    min-width: 0 !important;
    box-shadow: none !important;
  }
}

@media (max-width: 900px) {
  body {
    min-height: 100dvh;
    padding-bottom: var(--mobile-safe-bottom);
    overscroll-behavior-y: contain;
  }

  .mobileTopbar {
    height: calc(56px + var(--mobile-safe-top)) !important;
    padding: calc(7px + var(--mobile-safe-top)) calc(10px + var(--mobile-safe-right)) 7px calc(10px + var(--mobile-safe-left)) !important;
  }

  .topbar {
    padding-top: calc(18px + var(--mobile-safe-top)) !important;
    padding-bottom: calc(14px + var(--mobile-safe-bottom)) !important;
  }

  .page,
  .kgshNativePage,
  .kgshDocPage {
    padding-left: calc(9px + var(--mobile-safe-left)) !important;
    padding-right: calc(9px + var(--mobile-safe-right)) !important;
    padding-bottom: calc(20px + var(--mobile-safe-bottom)) !important;
    max-width: 100vw !important;
    overflow-x: hidden !important;
  }

  .page > *,
  .kgshNativePage > *,
  .kgshDocPage > *,
  .panel,
  .kgshCard,
  .adminGrid,
  .wideAdmin {
    min-width: 0 !important;
    max-width: 100% !important;
  }

  .userAdminRow > * {
    grid-column: auto !important;
  }

  .heroHeader,
  .detailHeader,
  .pageHeader,
  .kgshNativeTop,
  .kgshDocTop {
    padding: 12px !important;
    border-radius: 12px !important;
    gap: 8px !important;
  }

  .heroHeader p,
  .detailHeader p,
  .kgshNativeTop p,
  .kgshDocTop p {
    display: none !important;
  }

  .panel,
  .schemePanel,
  .summaryCard,
  .equipmentRow,
  .kgshCard,
  .kgshDocCard {
    border-radius: 12px !important;
    padding: 12px !important;
    box-shadow: none !important;
  }

  .pageHeaderActions,
  .detailHeaderActions,
  .kgshDocActions,
  .rowActions {
    display: grid !important;
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    width: 100% !important;
    gap: 7px !important;
  }

  .kgshNativeActions {
    display: grid !important;
    grid-template-columns: repeat(3, minmax(0, 1fr)) !important;
    width: 100% !important;
    gap: 7px !important;
  }

  .pageHeaderActions > *,
  .detailHeaderActions > *,
  .kgshNativeActions > *,
  .kgshDocActions > *,
  .rowActions > * {
    width: 100% !important;
    min-width: 0 !important;
    min-height: 44px !important;
  }

  input,
  select,
  textarea,
  .kgshForm input,
  .kgshForm select,
  .kgshForm textarea,
  .kgshDocCard input,
  .kgshDocCard textarea {
    min-height: 44px !important;
    font-size: 16px !important;
  }

  .advancedFilters,
  .filters,
  .compactSearchFilters,
  .photoUpload,
  .adminGrid,
  .wideAdmin,
  .kgshNativeGrid,
  .kgshFilters,
  .kgshDocGrid {
    grid-template-columns: 1fr !important;
  }

  .tabs,
  .kgshTabs {
    display: grid !important;
    grid-template-columns: repeat(3, minmax(0, 1fr)) !important;
    overflow: visible !important;
    gap: 7px !important;
    width: 100% !important;
  }

  #kgshStatusTabs {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
  }

  .tabs > *,
  .kgshTabs > * {
    width: 100% !important;
    min-width: 0 !important;
    min-height: 42px !important;
    padding: 8px 7px !important;
    white-space: normal !important;
    overflow-wrap: anywhere;
  }

  .modalBackdrop,
  .reportOverlay {
    padding: calc(8px + var(--mobile-safe-top)) calc(8px + var(--mobile-safe-right)) calc(8px + var(--mobile-safe-bottom)) calc(8px + var(--mobile-safe-left)) !important;
  }

  .scannerModal,
  .reportModal,
  .kgshEditModal {
    width: 100% !important;
    max-height: 94dvh !important;
    border-radius: 12px !important;
  }
}

@media (max-width: 520px) {
  h1,
  .heroHeader h1,
  .detailHeader h1,
  .kgshNativeTop h1,
  .kgshDocTop h1 {
    font-size: 22px !important;
  }

  .summaryCards,
  .detailSummary {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
  }

  .userAdminRow,
  .editGrid,
  .reportGrid {
    grid-template-columns: 1fr !important;
  }
}

@media (max-width: 400px) {
  .kgshNativeActions,
  .tabs,
  .kgshTabs {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
  }

  .kgshNativeActions > :last-child,
  .tabs > :last-child,
  .kgshTabs > :last-child {
    grid-column: 1 / -1;
  }
}
