/*
 * Record Unit System - Mobile UX Layer
 * Desktop behavior intentionally untouched. All layout overrides are scoped
 * to tablet/phone breakpoints so existing application logic and desktop UI
 * stay unchanged.
 */

.mobile-menu-toggle,
.mobile-menu-close,
.mobile-nav-overlay,
.mobile-table-hint {
  display: none;
}

@media (max-width: 900px) {
  html {
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
    scroll-behavior: smooth;
  }

  body {
    overflow-x: hidden;
    -webkit-tap-highlight-color: rgba(37, 99, 235, .12);
  }

  body.mobile-nav-open {
    overflow: hidden;
    touch-action: none;
  }

  a,
  button,
  input,
  select,
  textarea,
  label {
    -webkit-tap-highlight-color: rgba(37, 99, 235, .12);
  }

  button,
  a.btn,
  a.ga-btn,
  a.inv-btn,
  a.dm-btn,
  .btn,
  .ga-btn,
  .inv-btn,
  .dm-btn,
  .permission-btn,
  .tgr-button {
    touch-action: manipulation;
  }

  img,
  video,
  canvas,
  iframe {
    max-width: 100%;
    height: auto;
  }

  /* ---------- App shell / mobile drawer ---------- */
  .app-shell {
    display: block !important;
    min-height: 100dvh;
  }

  .main-area {
    width: 100% !important;
    min-width: 0 !important;
  }

  .sidebar {
    position: fixed !important;
    inset: 0 auto 0 0 !important;
    z-index: 1200 !important;
    width: min(88vw, 340px) !important;
    max-width: 340px !important;
    height: 100vh !important;
    height: 100dvh !important;
    padding: max(18px, env(safe-area-inset-top)) 14px max(20px, env(safe-area-inset-bottom)) !important;
    overflow-x: hidden !important;
    overflow-y: auto !important;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    transform: translate3d(-102%, 0, 0);
    transition: transform .22s ease;
    box-shadow: 18px 0 50px rgba(5, 20, 38, .28) !important;
  }

  body.mobile-nav-open .sidebar {
    transform: translate3d(0, 0, 0);
  }

  .mobile-nav-overlay {
    position: fixed;
    inset: 0;
    z-index: 1150;
    display: block;
    background: rgba(5, 20, 38, .48);
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transition: opacity .2s ease, visibility .2s ease;
    backdrop-filter: blur(2px);
    -webkit-backdrop-filter: blur(2px);
  }

  body.mobile-nav-open .mobile-nav-overlay {
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
  }

  .sidebar .company-brand {
    position: relative;
    padding-right: 48px !important;
    margin-bottom: 12px !important;
  }

  .mobile-menu-close {
    position: absolute;
    top: 2px;
    right: 0;
    width: 42px;
    height: 42px;
    border: 1px solid rgba(255,255,255,.16);
    border-radius: 12px;
    display: inline-grid;
    place-items: center;
    background: rgba(255,255,255,.08);
    color: #fff;
    font-size: 24px;
    line-height: 1;
    cursor: pointer;
  }

  .nav-menu {
    gap: 6px !important;
    padding-bottom: 18px;
  }

  .nav-link,
  .nav-group-toggle {
    min-height: 48px;
    padding: 12px 13px !important;
    border-radius: 12px !important;
    font-size: 13px;
  }

  .nav-inner-toggle {
    min-height: 46px;
    padding-left: 14px !important;
  }

  .nav-submenu,
  .nav-inner-submenu {
    margin-left: 10px !important;
    padding-left: 9px !important;
  }

  .nav-submenu a,
  .nav-inner-submenu a {
    min-height: 44px;
    display: flex !important;
    align-items: center;
    padding: 10px 12px !important;
    font-size: 12.5px !important;
    line-height: 1.35;
  }

  .nav-ico {
    flex: 0 0 22px;
  }

  /* ---------- Compact sticky mobile header ---------- */
  .topbar {
    position: sticky !important;
    top: 0 !important;
    z-index: 900 !important;
    min-height: 0 !important;
    padding: max(10px, env(safe-area-inset-top)) 12px 10px !important;
    display: grid !important;
    grid-template-columns: 44px minmax(0, 1fr) !important;
    gap: 6px 10px !important;
    align-items: center !important;
    background: rgba(255,255,255,.96) !important;
    backdrop-filter: blur(14px);
    -webkit-backdrop-filter: blur(14px);
  }

  .mobile-menu-toggle {
    grid-column: 1;
    grid-row: 1;
    width: 44px;
    height: 44px;
    border: 1px solid #dbe5f0;
    border-radius: 12px;
    display: inline-flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 4px;
    background: #fff;
    color: #16365c;
    box-shadow: 0 4px 12px rgba(15,23,42,.06);
    cursor: pointer;
  }

  .mobile-menu-toggle span {
    display: block;
    width: 18px;
    height: 2px;
    border-radius: 999px;
    background: currentColor;
  }

  .topbar-title {
    grid-column: 2;
    grid-row: 1;
    min-width: 0;
  }

  .topbar-title .breadcrumb {
    margin: 0 0 2px !important;
    font-size: 8.5px !important;
    line-height: 1.1;
    letter-spacing: .08em !important;
  }

  .topbar-title h1,
  .topbar h1 {
    margin: 0 !important;
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 18px !important;
    line-height: 1.2 !important;
  }

  .topbar-title > p,
  .topbar > div:first-of-type > p {
    display: none !important;
  }

  .topbar-right {
    grid-column: 1 / -1;
    grid-row: 2;
    width: 100% !important;
    min-width: 0;
    display: flex !important;
    flex-wrap: nowrap !important;
    justify-content: flex-start !important;
    gap: 6px !important;
    overflow-x: auto;
    padding: 1px 0 2px;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
  }

  .topbar-right::-webkit-scrollbar {
    display: none;
  }

  .topbar-right .pill {
    min-height: 32px !important;
    flex: 0 0 auto;
    padding: 6px 10px !important;
    font-size: 10px !important;
    box-shadow: none !important;
  }

  .topbar-right .user-box {
    min-height: 34px;
    flex: 0 0 auto;
    padding: 4px 9px 4px 5px !important;
    gap: 7px !important;
    box-shadow: none !important;
  }

  .topbar-right .avatar {
    width: 28px !important;
    height: 28px !important;
    font-size: 11px;
  }

  .topbar-right .user-box strong {
    font-size: 10.5px;
  }

  .topbar-right .user-box small {
    font-size: 9px !important;
  }

  /* ---------- Main page spacing ---------- */
  .content-area {
    width: 100% !important;
    min-width: 0 !important;
    padding: 14px 12px max(24px, env(safe-area-inset-bottom)) !important;
    overflow-x: clip;
  }

  .card,
  .ga-card,
  .ga-panel,
  .ga-section,
  .dm-card,
  .inv-form-card,
  .inv-toolbar-card,
  .inv-detail-card,
  .ms-card {
    max-width: 100%;
    border-radius: 15px !important;
  }

  .card,
  .ga-card,
  .ga-panel,
  .ga-section,
  .inv-form-card,
  .inv-toolbar-card,
  .inv-detail-card {
    padding: 14px !important;
  }

  .card-header,
  .ga-section-title,
  .inv-page-head,
  .inv-table-head,
  .dm-section-title {
    gap: 10px !important;
  }

  .section-title,
  .ga-title,
  .dm-section-title h3 {
    line-height: 1.25;
  }

  .section-subtitle,
  .ga-subtitle,
  .dm-muted,
  .dm-help,
  .form-note {
    line-height: 1.5;
  }

  /* ---------- Hero / summaries ---------- */
  .dm-hero,
  .ga-hero {
    padding: 15px !important;
    border-radius: 15px !important;
  }

  .dm-hero,
  .ga-hero,
  .inv-page-head,
  .inv-hero-content {
    align-items: flex-start !important;
    flex-direction: column !important;
  }

  .dm-hero h2,
  .ga-hero-title {
    font-size: 20px !important;
  }

  .dm-hero-actions,
  .inv-actions {
    width: 100% !important;
  }

  .dm-hero-actions .dm-btn,
  .inv-actions .inv-btn {
    flex: 1 1 140px;
  }

  .stats,
  .grid,
  .grid-2,
  .grid-4,
  .ga-stat-grid,
  .ga-kpi-grid,
  .inv-summary-grid,
  .inv-stat-grid,
  .inv-dashboard-grid,
  .dm-kpis,
  .dm-grid-2,
  .dm-grid-3,
  .dm-quick-actions,
  .pc-balance-kpis,
  .pc-balance-breakdown {
    grid-template-columns: 1fr !important;
    gap: 10px !important;
  }

  /* ---------- Forms: one clear vertical flow ---------- */
  .filter-bar,
  .ga-filter,
  .ga-form-grid,
  .inv-filter-form,
  .inv-import-form,
  .inv-form-grid,
  .dm-form-grid,
  .dm-form-grid.cols-3,
  .dm-filter,
  .si-form-grid,
  .rio-filter-grid,
  .tgr-out-grid {
    grid-template-columns: 1fr !important;
  }

  .dm-field.span-2,
  .dm-field.span-all,
  .inv-field-wide,
  .field-span-2 {
    grid-column: auto !important;
  }

  input:not([type="checkbox"]):not([type="radio"]),
  select,
  textarea,
  .dm-field input,
  .dm-field select,
  .dm-field textarea,
  .ga-field input,
  .ga-field select,
  .ga-field textarea,
  .inv-field input,
  .inv-field select,
  .inv-field textarea,
  .tgr-out-field input,
  .tgr-out-field select,
  .tgr-out-field textarea,
  .si-field input,
  .si-field select,
  .si-field textarea {
    max-width: 100% !important;
    min-height: 46px;
    font-size: 16px !important; /* Prevent iOS Safari input zoom. */
  }

  textarea,
  .dm-field textarea,
  .ga-field textarea,
  .inv-field textarea {
    min-height: 96px;
  }

  input[type="file"] {
    width: 100%;
    min-height: 48px;
    padding: 7px !important;
    overflow: hidden;
  }

  input[type="file"]::file-selector-button {
    min-height: 34px;
  }

  input[type="date"],
  input[type="time"],
  input[type="datetime-local"] {
    min-width: 0;
  }

  .form-actions,
  .dm-form-actions,
  .inv-form-actions,
  .inv-filter-actions,
  .ga-btn-row,
  .tgr-out-actions,
  .kb-actions,
  .pc-filter-actions {
    width: 100% !important;
    display: flex !important;
    flex-wrap: wrap !important;
    gap: 8px !important;
  }

  .form-actions > .btn,
  .dm-form-actions > .dm-btn,
  .inv-form-actions > .inv-btn,
  .inv-filter-actions > .inv-btn,
  .ga-btn-row > .ga-btn,
  .tgr-out-actions > .tgr-button,
  .kb-actions > .dm-btn,
  .pc-filter-actions > .dm-btn {
    flex: 1 1 140px;
    min-height: 46px;
  }

  .btn,
  .ga-btn,
  .inv-btn,
  .dm-btn,
  .permission-btn,
  .tgr-button {
    min-height: 44px;
  }

  /* ---------- Destination choices / operational selectors ---------- */
  .pc-destinations,
  .kbo-destinations {
    grid-template-columns: 1fr !important;
  }

  .pc-destinations span,
  .kbo-destinations span {
    min-height: 64px !important;
  }

  /* ---------- Tables: preserve every column, make touch-scrolling obvious ---------- */
  .table-wrap,
  .ga-table-wrap,
  .inv-table-wrap,
  .dm-table-wrap,
  .ms-table-wrap,
  .tgr-out-table-wrap,
  .mobile-table-scroll {
    position: relative;
    width: 100% !important;
    max-width: 100% !important;
    overflow-x: auto !important;
    overflow-y: visible;
    overscroll-behavior-x: contain;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
    border-radius: 10px;
  }

  .mobile-table-scroll {
    margin-bottom: 10px;
  }

  table {
    max-width: none;
  }

  .table-wrap table,
  .ga-table-wrap table,
  .inv-table-wrap table,
  .dm-table-wrap table,
  .mobile-table-scroll > table {
    width: max-content;
    min-width: 100% !important;
  }

  table th,
  table td,
  .dm-table th,
  .dm-table td {
    padding: 10px 9px !important;
    font-size: 12px;
    vertical-align: top;
  }

  table th {
    white-space: nowrap;
  }

  .mobile-table-hint {
    display: none;
    align-items: center;
    gap: 7px;
    margin: 0 0 8px;
    padding: 8px 10px;
    border-radius: 10px;
    background: #eef6ff;
    border: 1px solid #d5e7fb;
    color: #31587e;
    font-size: 10.5px;
    font-weight: 700;
    line-height: 1.35;
  }

  .mobile-table-hint.is-visible {
    display: flex;
  }

  .mobile-table-hint::before {
    content: "↔";
    flex: 0 0 auto;
    width: 24px;
    height: 24px;
    display: grid;
    place-items: center;
    border-radius: 7px;
    background: #fff;
    color: #1766bb;
    font-size: 14px;
    border: 1px solid #d5e7fb;
  }

  /* Keep action menus above scroll containers. */
  .aksi-dropdown,
  .aksi-menu {
    z-index: 1300 !important;
  }

  /* ---------- Modals ---------- */
  .dm-modal,
  [class*="modal"] {
    -webkit-overflow-scrolling: touch;
  }

  .dm-modal {
    padding: 0 !important;
    align-items: flex-end !important;
  }

  .dm-modal-panel {
    width: 100% !important;
    max-width: 100% !important;
    max-height: 94dvh !important;
    border-radius: 18px 18px 0 0 !important;
  }

  .dm-modal-body {
    padding-bottom: max(18px, env(safe-area-inset-bottom)) !important;
  }

  /* ---------- Pagination / tabs ---------- */
  .inv-tabs,
  .inv-pagination,
  .table-actions {
    max-width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }

  .inv-tabs::-webkit-scrollbar,
  .inv-pagination::-webkit-scrollbar,
  .table-actions::-webkit-scrollbar {
    display: none;
  }

  .inv-tabs a,
  .inv-pagination a {
    flex: 0 0 auto;
    min-height: 42px;
  }

  /* ---------- Readability / long text ---------- */
  .card,
  .dm-card,
  .ga-card,
  .ga-panel,
  .inv-detail-card,
  td,
  .field-value,
  .ga-info-value {
    overflow-wrap: anywhere;
  }
}

@media (max-width: 480px) {
  .content-area {
    padding-left: 10px !important;
    padding-right: 10px !important;
  }

  .topbar {
    padding-left: 10px !important;
    padding-right: 10px !important;
  }

  .sidebar {
    width: min(92vw, 330px) !important;
  }

  .topbar-right .muted-pill {
    display: none !important;
  }

  .user-box > div:last-child small {
    display: none !important;
  }

  .card,
  .ga-card,
  .ga-panel,
  .ga-section,
  .inv-form-card,
  .inv-toolbar-card,
  .inv-detail-card {
    padding: 12px !important;
  }

  .dm-card:not(.dm-card-table) {
    padding: 12px !important;
  }

  .form-actions > *,
  .dm-form-actions > *,
  .inv-form-actions > *,
  .ga-btn-row > *,
  .tgr-out-actions > *,
  .kb-actions > * {
    flex-basis: 100% !important;
  }
}

@media (prefers-reduced-motion: reduce) {
  .sidebar,
  .mobile-nav-overlay {
    transition: none !important;
  }
}

/* Login is outside header_app(), but should be just as usable on phones. */
@media (max-width: 640px) {
  .login-page .login-wrapper {
    min-height: 100dvh;
    padding: max(14px, env(safe-area-inset-top)) 12px max(14px, env(safe-area-inset-bottom)) !important;
  }

  .login-page .login-card {
    width: 100% !important;
    max-width: 460px !important;
    padding: 20px !important;
    border-radius: 18px !important;
  }

  .login-page .login-form input {
    width: 100%;
    min-height: 48px;
    font-size: 16px !important;
  }

  .login-page .login-button {
    width: 100%;
    min-height: 48px;
    font-size: 15px;
    touch-action: manipulation;
  }
}

@media (max-width: 900px) {
  form,
  fieldset {
    max-width: 100%;
    min-width: 0;
  }

  /* Two legacy dashboards use an inline multi-column summary grid. */
  .content-area > .card > div[style*="grid-template-columns"] {
    grid-template-columns: 1fr !important;
  }
}
