/* ============================================================
   KITA — Device adaptation layer
   Loaded LAST so it can settle cross-file conflicts without
   touching the per-role stylesheets.

   Scope: phone + tablet behaviour that the per-page media
   queries don't cover — touch input, iOS Safari quirks,
   notch/home-indicator safe areas, and landscape.
   Layout/column breakpoints stay in their own role files.
   ============================================================ */


/* ============================================================
   1. TEXT INFLATION
   iOS Safari silently up-scales text when a phone rotates to
   landscape, which desynchronises every hand-tuned font size
   in this project. Opt out and keep the authored scale.
   ============================================================ */
html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

/* ============================================================
   CONSISTENT NATIVE SELECT CHEVRONS
   Keep every standard single-select arrow comfortably inside its border,
   regardless of the page stylesheet or device rendering engine.
   ============================================================ */
select:not([multiple]):not([size]) {
  padding-right: 2.75rem;
  -webkit-appearance: none;
  appearance: none;
  background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath fill='none' stroke='%231a2e22' stroke-linecap='round' stroke-linejoin='round' stroke-width='1.7' d='M1 1.5 6 6.5l5-5'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 1rem center;
  background-size: 12px 8px;
}

/* A native select's label must never paint underneath its chevron when a
   compact grid/flex row shrinks. Apply the same protected arrow gutter to
   every account on phone-sized screens. */
@media (max-width: 576px) {
  .main-content select:not([multiple]):not([size]),
  #main-content main select:not([multiple]):not([size]) {
    display: block;
    width: 100%;
    min-width: 0;
    max-width: 100%;
    box-sizing: border-box;
    padding-right: 2.25rem !important;
    background-position: right .75rem center !important;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}


/* ============================================================
   2. iOS INPUT ZOOM
   Safari zooms the whole page in when a field with a computed
   font-size under 16px receives focus, and never zooms back
   out. Every field in this app sits at 12–14px, so tapping any
   filter or search box on an iPhone strands the user at 1.3×.
   On a coarse pointer, promote form text to 16px and pull the
   padding back in so the controls stay the same visual weight.
   ============================================================ */
@media (pointer: coarse) {

  /* !important is load-bearing here, not laziness: page-scoped rules like
     `.income-page .form-input` and the analytics chips out-specify a plain
     class selector, and a field that stays at 13px re-introduces the zoom.
     This layer exists to have the last word on device behaviour. */
  input,
  select,
  textarea,
  .form-control,
  .form-select,
  .amfe-input,
  .form-input,
  .search-input,
  .filter-select,
  .si-filter-select,
  .si-filter-input,
  .si-select,
  .catalog-search-input,
  .ana-period,
  .fa-period {
    font-size: 16px !important;
  }

  /* Compact chips keep their footprint: the text grew, so the
     vertical padding gives some of it back. */
  .filter-select,
  .si-filter-select,
  .si-select,
  .ana-period,
  .fa-period {
    padding-top: 8px;
    padding-bottom: 8px;
  }

  .si-filter-select,
  .si-filter-input {
    height: auto;
  }
}


/* ============================================================
   3. TOUCH TARGETS
   A fingertip needs ~44px; this UI was drawn for a cursor, so
   icon-only controls sit at 20–34px. Rather than inflate them
   — which would coarsen a deliberately fine visual language —
   each keeps its drawn size and gains an invisible 44px hit
   area centred on it.
   ============================================================ */
@media (pointer: coarse) {

  /* Blanket floor for controls that can simply grow. */
  .btn,
  .btn-green,
  .btn-outline,
  .btn-primary-amfe,
  .btn-outline-amfe,
  .btn-danger-amfe,
  .btn-signin,
  .nav-link,
  .logout-link,
  .analytics-tab,
  .tab-btn,
  .chart-toggle,
  .fa-chart-btn,
  .ana-chart-btn,
  .amfe-tab,
  .ana-tab,
  .navbar-toggler,
  .btn-next,
  .btn-back,
  .btn-edit-product,
  .btn-send-reply,
  select {
    min-height: 44px;
  }

  /* Anything tappable: no 300ms delay, no grey flash rectangle. */
  a,
  button,
  select,
  summary,
  [role="button"] {
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
  }

  /* The tap flash is replaced by an authored press state, so the
     control still acknowledges the touch. */
  button:active,
  .btn:active,
  .nav-link:active,
  [role="button"]:active {
    opacity: 0.72;
  }

  /* --- Invisible hit expanders -------------------------------
     The element keeps its drawn box; ::after reaches out to 44px.
     Guarded so it never lands on a control that already has a
     pseudo-element doing visual work.                          */
  .row-chevron-btn,
  .btn-icon-danger,
  .gallery-dot,
  .gallery-arrow,
  .programs-arrow,
  .btn-close,
  .notification-mark-all,
  .rep-field-trigger,
  .chat-back-btn,
  .pagination-bar button,
  .pagination-page,
  .pagination-chevron,
  .lang-switch-opt,
  .income-collapse-toggle,
  .lightbox-close,
  .lightbox-arrow,
  .forgot-link,
  .back-home-inside,
  .join-link a,
  .footer-links a,
  .biz-row a.val {
    position: relative;
  }

  .eye-btn::after,
  .row-chevron-btn::after,
  .btn-icon-danger::after,
  .gallery-dot::after,
  .gallery-arrow::after,
  .programs-arrow::after,
  .btn-close::after,
  .notification-mark-all::after,
  .rep-field-trigger::after,
  .chat-back-btn::after,
  .pagination-bar button::after,
  .pagination-page::after,
  .pagination-chevron::after,
  .lightbox-close::after,
  .lightbox-arrow::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    min-width: 44px;
    min-height: 44px;
    width: 100%;
    height: 100%;
  }

  /* Text links and full-width rows sit in horizontal runs, so they reach
     out vertically only — growing sideways would swallow the neighbour. */
  .income-collapse-toggle::after,
  .lang-switch-opt::after,
  .forgot-link::after,
  .back-home-inside::after,
  .join-link a::after,
  .footer-links a::after,
  .biz-row a.val::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 0;
    right: 0;
    transform: translateY(-50%);
    min-height: 44px;
    height: 100%;
  }

  /* Gallery dots sit 8px apart; a 44px pad on each would swallow
     its neighbour, so they get a tighter reach plus real spacing. */
  .gallery-dots { gap: 18px; }
  .gallery-dot::after {
    min-width: 36px;
    min-height: 36px;
  }

  /* Neighbouring targets need real gaps once each reaches 44px, or the
     expanded areas overlap and a tap lands on the wrong control. */
  .pagination-bar { gap: 12px; }

  .amfe-table td .d-flex,
  .amfe-table td .btn-group {
    gap: 8px;
  }
}


/* ============================================================
   4. HOVER-DEPENDENT AFFORDANCES
   A touch tap leaves :hover latched on the last element until
   the next tap elsewhere, so lift-on-hover cards stay raised
   and look stuck. Drop the movement where there's no cursor.
   ============================================================ */
@media (hover: none) {

  .card:hover,
  .amfe-card:hover,
  .stat-card:hover,
  .product-card:hover,
  .value-card:hover,
  .program-card:hover,
  .sidebar:hover,
  #sidebar:hover {
    transform: none;
    box-shadow: inherit;
  }

  .amfe-table tbody tr:hover,
  .prod-table tbody tr:hover,
  .history-table tbody tr:hover {
    background: transparent;
  }
}


/* ============================================================
   5. SAFE AREAS  (≤991px — the drawer shell)
   The fixed top bar, hamburger, bell and drawer are pinned to
   viewport edges, which on a notched phone means underneath
   the notch in portrait and underneath the rounded corners in
   landscape. Inset them by the device's own reported margins.
   Requires viewport-fit=cover, set in app.blade.php.
   ============================================================ */
@media (max-width: 991px) {

  .page-title {
    height: calc(60px + env(safe-area-inset-top));
    padding-top: env(safe-area-inset-top);
    padding-left: max(20px, env(safe-area-inset-left));
    padding-right: calc(72px + env(safe-area-inset-right));
  }

  /* Admin's bar has a third button (Messages) past the bell. */
  .main-content .page-title {
    padding-right: calc(164px + env(safe-area-inset-right));
  }

  .mobile-menu-btn {
    top: calc(8px + env(safe-area-inset-top));
    right: calc(12px + env(safe-area-inset-right));
  }

  .notification-bell-btn {
    top: calc(8px + env(safe-area-inset-top));
    right: calc(64px + env(safe-area-inset-right));
  }

  .topbar-message-btn {
    top: calc(8px + env(safe-area-inset-top));
    right: calc(110px + env(safe-area-inset-right));
  }

  .main-content,
  #main-content {
    padding-top: calc(70px + env(safe-area-inset-top)) !important;
    padding-left: max(12px, env(safe-area-inset-left)) !important;
    padding-right: max(12px, env(safe-area-inset-right)) !important;
    padding-bottom: calc(24px + env(safe-area-inset-bottom)) !important;
  }

  .main-content.has-mobile-fab,
  .main-content:has(.mobile-add-fab),
  .main-content:has(.ra-floating-add),
  .main-content:has(.mobile-add-menu),
  #main-content.has-mobile-fab,
  #main-content:has(.mobile-add-fab),
  #main-content:has(.ra-floating-add),
  #main-content:has(.mobile-add-menu) {
    padding-bottom: calc(72px + env(safe-area-inset-bottom)) !important;
  }

  /* Right-hand drawer: clear the home indicator and, in
     landscape, the rounded corner it slides out of. */
  .sidebar,
  #sidebar {
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
    padding-right: env(safe-area-inset-right);
  }
}


/* ============================================================
   6. VIEWPORT HEIGHT
   iOS Safari's 100vh is the height with the toolbars hidden,
   so a 100vh drawer runs taller than what's on screen and its
   last items — language toggle and Logout — sit under the
   browser chrome, unreachable. dvh tracks the real viewport;
   the vh line above it stays as the fallback.
   ============================================================ */
@supports (height: 100dvh) {
  .sidebar,
  #sidebar {
    height: 100dvh;
  }
  #wrapper,
  #main-content,
  .main-content {
    min-height: 100dvh;
  }
}

/* The drawer scrolls its own overflow and stops the page behind
   it from taking over the gesture at either end. */
@media (max-width: 991px) {
  .sidebar,
  #sidebar {
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
  }
}

/* Set while the drawer is open, so the page behind it can't
   scroll under the overlay. */
body.drawer-open {
  overflow: hidden;
}


/* ============================================================
   7. PHONE LANDSCAPE
   ~390px of height with a 60px bar and a 13-item drawer. Give
   the vertical axis back what the design doesn't need here.
   ============================================================ */
@media (max-width: 991px) and (orientation: landscape) and (max-height: 500px) {

  .page-title {
    height: calc(48px + env(safe-area-inset-top));
    line-height: 48px;
    font-size: 17px;
  }

  .mobile-menu-btn,
  .notification-bell-btn,
  .topbar-message-btn {
    top: calc(4px + env(safe-area-inset-top));
    width: 40px;
    height: 40px;
  }
  .mobile-menu-btn i { font-size: 27px; }
  .notification-bell-btn i,
  .topbar-message-btn i { font-size: 21px; }

  .main-content,
  #main-content {
    padding-top: calc(58px + env(safe-area-inset-top)) !important;
  }

  /* Charts sized for portrait leave no room for anything else. */
  .chart-canvas-wrap { height: 200px; }
  .pie-canvas-wrap   { height: 160px; }

  /* The notification list must not run past the bottom edge. */
  .notification-dropdown {
    max-height: calc(100dvh - 70px);
  }
  .notification-list {
    max-height: calc(100dvh - 170px);
  }
}


/* ============================================================
   8. WIDE DATA IN NARROW COLUMNS
   Tables scroll sideways inside their own wrapper rather than
   pushing the page. Two farmer tables did this by making the
   <table> itself the scroller, which drops table layout and
   lets columns collapse; move the scroll to the wrapper and
   let the table be a table again.
   ============================================================ */
.table-scroll {
  overflow-x: auto;
  overscroll-behavior-x: contain;
  -webkit-overflow-scrolling: touch;
}

@media (max-width: 768px) {
  .prod-table,
  .history-table {
    display: table;
    width: 100%;
  }
  .prod-table-wrap,
  .table-scroll,
  .table-responsive {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
}


/* ============================================================
   9. SALES & INVENTORY — EXTRA-SMALL PHONE FIT
   Keep both period choices readable without letting their native
   controls dictate the page width. Cards and their symbols scale
   with the viewport while retaining the two-card phone layout.
   ============================================================ */
@media (max-width: 576px) {
  body.admin-shell .si-filter-bar {
    /* Keep period and month joined in one row at every width. The first
       field receives slightly more room for "Monthly View" plus its icon
       and chevron; both columns may still shrink below intrinsic size. */
    grid-template-columns: minmax(0, 1.18fr) minmax(0, .92fr);
  }

  body.admin-shell .si-filter-field {
    gap: clamp(.3rem, 1.8vw, .45rem);
    min-width: 0;
    padding: .55rem clamp(.45rem, 2vw, .65rem);
  }

  body.admin-shell .si-filter-icon,
  body.admin-shell .responsive-month-trigger .bi {
    flex: 0 0 auto;
    font-size: clamp(.78rem, 3.5vw, .9rem);
  }

  body.admin-shell .si-filter-field select.si-filter-select {
    min-width: 0;
    padding-right: 2rem !important;
    background-position: right .65rem center !important;
    background-size: 10px 7px;
    font-size: clamp(.72rem, 3.5vw, .88rem) !important;
  }

  body.admin-shell .si-filter-field:not(:first-child) {
    border-top: 0;
    border-left: 1px solid var(--border);
  }

  body.admin-shell .responsive-month-trigger {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: .35rem;
    min-width: 0;
    font-size: clamp(.72rem, 3.5vw, .88rem) !important;
  }

  body.admin-shell .responsive-month-label {
    min-width: 0;
    white-space: nowrap;
  }

  body.admin-shell .si-summary-row {
    gap: clamp(.45rem, 2vw, .65rem);
  }

  body.admin-shell .si-stat {
    min-width: 0;
    min-height: clamp(8.75rem, 42vw, 10rem);
    padding: clamp(.65rem, 3vw, .85rem) clamp(.4rem, 2vw, .55rem);
  }

  body.admin-shell .si-stat-icon {
    width: clamp(2.2rem, 12vw, 2.75rem);
    height: clamp(2.2rem, 12vw, 2.75rem);
    font-size: clamp(.9rem, 4.5vw, 1.1rem);
  }

  body.admin-shell .si-stat-label {
    font-size: clamp(.7rem, 3.3vw, .78rem);
  }

  body.admin-shell .si-stat-value {
    font-size: clamp(.98rem, 5.3vw, 1.3rem);
  }
}

/* Preserve the joined horizontal control even at the narrowest supported
   width by scaling its labels instead of moving either field below. */
@media (max-width: 359.98px) {
  body.admin-shell .si-filter-field select.si-filter-select,
  body.admin-shell .responsive-month-trigger {
    font-size: .7rem !important;
  }
}


/* ============================================================
   10. REDUCED MOTION
   ============================================================ */
@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;
  }
}
