/**
 * TRN ERP — touch / tablet layer (tablet APK plan P1–P3).
 *
 * Loaded LAST in layouts/app.blade.php (after fund-calm.css) so it wins at equal specificity.
 * Gated by `html.trn-touch` (set in partials/pwa-head: coarse pointer OR Capacitor/TRNTabletApp UA),
 * so it works in the Android WebView, in Chrome PWA on a tablet, and in MCP touch-emulation.
 * Canon breakpoints stay: phone ≤720 · tablet 721–960 · desktop ≥961 · hub widgets ≥1280.
 * Fund-only tablet UI (icon rail, peek pane) lives in pages/fund-tablet.css.
 */

/* ── 0. Base touch behaviour ───────────────────────────── */
html.trn-touch {
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

html.trn-touch a,
html.trn-touch button,
html.trn-touch [role="button"],
html.trn-touch [role="tab"],
html.trn-touch summary,
html.trn-touch label,
html.trn-touch input,
html.trn-touch select,
html.trn-touch textarea {
  touch-action: manipulation; /* no 300ms delay / double-tap zoom */
}

/* UI chrome is not selectable by accident; content stays selectable */
html.trn-touch .sd-taskbar,
html.trn-touch .sd-fund-sidebar,
html.trn-touch .sd-window-titlebar,
html.trn-touch .btn,
html.trn-touch .sd-tray-btn {
  -webkit-user-select: none;
  user-select: none;
}

/* Native app: no rubber-band / pull-to-refresh bounce from WebView on shell */
html.trn-app,
html.trn-app body {
  overscroll-behavior-y: none;
}

/* ── 1. Typography (≥16px body, secondary ≥14px) ───────── */
html.trn-touch body.sd-app .sd-window-body {
  font-size: max(1rem, 16px);
}

html.trn-touch body.sd-app .small,
html.trn-touch body.sd-app small,
html.trn-touch body.sd-app .form-text,
html.trn-touch body.sd-app .text-muted.small {
  font-size: max(0.875rem, 14px);
}

/* ── 2. Buttons (≥44px, sm keeps 44px hit-area) ────────── */
html.trn-touch body.sd-app .btn,
html.trn-touch body.sd-app .btn-sm,
html.trn-touch body.sd-app .btn-group-sm > .btn,
html.trn-touch body.sd-app .fund-glass-btn,
html.trn-touch body.sd-app .trn-icon-btn,
html.trn-touch body.sd-app .trn-actions-trigger,
html.trn-touch body.sd-app .sd-fund-sidebar-toggle {
  min-height: var(--sd-touch-min);
  min-width: var(--sd-touch-min);
}

html.trn-touch body.sd-app .btn-sm {
  padding-block: 0.5rem;
  padding-inline: 0.85rem;
  font-size: max(0.9375rem, 15px);
}

html.trn-touch body.sd-app .btn-close {
  min-width: var(--sd-touch-min);
  min-height: var(--sd-touch-min);
  background-size: 0.9em;
}

/* Tray + window controls */
html.trn-touch body.sd-app .sd-tray-btn {
  min-width: var(--sd-touch-min);
  min-height: var(--sd-touch-min);
}

html.trn-touch body.sd-app .sd-taskbar-tray {
  gap: 0.5rem; /* inter-spacing ≥ 8px */
}

html.trn-touch body.sd-app .sd-win-btn {
  min-width: var(--sd-touch-min);
  min-height: var(--sd-touch-min);
}

html.trn-touch body.sd-app .sd-taskbar-tab,
html.trn-touch body.sd-app .sd-taskbar-more > summary {
  min-height: var(--sd-touch-min);
}

/* ── 3. Form controls (≥48px, 16px = no WebView zoom) ──── */
html.trn-touch body.sd-app .form-control,
html.trn-touch body.sd-app .form-select,
html.trn-touch body.sd-app .form-control-sm,
html.trn-touch body.sd-app .form-select-sm,
html.trn-touch body.sd-app input[type="text"],
html.trn-touch body.sd-app input[type="search"],
html.trn-touch body.sd-app input[type="email"],
html.trn-touch body.sd-app input[type="tel"],
html.trn-touch body.sd-app input[type="number"],
html.trn-touch body.sd-app input[type="password"],
html.trn-touch body.sd-app input[type="date"],
html.trn-touch body.sd-app select,
html.trn-touch body.sd-app textarea {
  min-height: var(--sd-touch-target);
  font-size: 16px;
}

html.trn-touch body.sd-app textarea.form-control {
  min-height: calc(var(--sd-touch-target) * 2);
}

/* Checkbox / radio: 24px visual + 44px label hit-area */
html.trn-touch body.sd-app .form-check {
  min-height: var(--sd-touch-min);
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding-left: 0;
}

html.trn-touch body.sd-app .form-check .form-check-input {
  float: none;
  margin: 0;
  width: 1.5rem;
  height: 1.5rem;
  flex: 0 0 auto;
}

html.trn-touch body.sd-app .form-check .form-check-label {
  flex: 1 1 auto;
  min-height: var(--sd-touch-min);
  display: inline-flex;
  align-items: center;
}

/* ── 4. Menus / dropdowns / row actions ────────────────── */
html.trn-touch body.sd-app .dropdown-item,
html.trn-touch body.sd-app .trn-actions-menu .dropdown-item,
html.trn-touch body.sd-app .sd-taskbar-more__menu a,
html.trn-touch body.sd-app .sd-space-switcher__item,
html.trn-touch body.sd-app .sd-start-tile {
  min-height: var(--sd-touch-target);
  display: flex;
  align-items: center;
}

html.trn-touch body.sd-app .trn-actions-trigger {
  width: var(--sd-touch-min);
  height: var(--sd-touch-min);
}

/* Tabs / pills that navigate */
html.trn-touch body.sd-app .nav-link,
html.trn-touch body.sd-app [role="tab"],
html.trn-touch body.sd-app .trn-profile-sections a {
  min-height: var(--sd-touch-min);
  display: inline-flex;
  align-items: center;
}

/* Pagination */
html.trn-touch body.sd-app .page-link {
  min-width: var(--sd-touch-min);
  min-height: var(--sd-touch-min);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

/* ── 5. Hover-only UI → always visible on touch ────────── */
@media (hover: none) {
  html.trn-touch .trn-hover-reveal,
  html.trn-touch [data-hover-reveal] {
    opacity: 1 !important;
    visibility: visible !important;
    pointer-events: auto !important;
  }
}

/* ── 6. Fund (shared touch parts; rail/peek in fund-tablet.css) ── */
html.trn-touch body[data-sd-module="fundraising"] .sd-fund-sidebar__link,
html.trn-touch body[data-sd-module="fundraising"] .sd-fund-sidebar__btn,
html.trn-touch body[data-sd-module="fundraising"] .sd-fund-sidebar__more > summary,
html.trn-touch body[data-sd-module="fundraising"] .sd-fund-sidebar__compose {
  min-height: var(--sd-touch-target);
}

html.trn-touch body[data-sd-module="fundraising"] .sd-fund-sidebar__search-input {
  min-height: var(--sd-touch-target);
  font-size: 16px;
}

html.trn-touch body[data-sd-module="fundraising"] .sd-fund-sidebar__density-btn {
  min-height: var(--sd-touch-min);
}

html.trn-touch body[data-sd-module="fundraising"] .fund-work-row__peek {
  width: var(--sd-touch-min);
  height: var(--sd-touch-min);
}

html.trn-touch body[data-sd-module="fundraising"] .fund-work-row {
  min-height: 56px; /* touch row ≥56px */
}

/* Density: compact is mouse-only → hide the switcher on touch */
html.trn-touch body[data-sd-module="fundraising"] .sd-fund-sidebar__density {
  display: none;
}

/* Fund chrome (hamburger bar) — shown on tablet too (≤960) and on touch landscape */
@media (max-width: 960px) {
  body[data-sd-module="fundraising"] .sd-fund-chrome {
    display: flex;
  }
}

/* Large-text accessibility (Android font scale 130–200%): rem-based, no clipping */
html.trn-touch body.sd-app .sd-taskbar {
  min-height: var(--sd-taskbar-h);
  height: auto;
}

/* ── 7. Tablet layout (721–960 portrait, ≥961 coarse landscape) ── */
@media (min-width: 721px) and (max-width: 960px) {
  html.trn-touch body.sd-app .sd-window-body .trn-page,
  html.trn-touch body.sd-app .sd-window-body {
    --trn-page-gutter: var(--sd-tablet-gutter);
  }

  html.trn-touch body.sd-app .trn-kpi-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (min-width: 961px) and (max-width: 1366px) {
  /* Landscape tablet: desktop table XOR stays, but rows get touch height */
  html.trn-touch body.sd-app .trn-list-table tbody tr > td,
  html.trn-touch body.sd-app .trn-list-table tbody tr > th {
    padding-block: 0.8rem;
    min-height: 56px;
  }

  html.trn-touch body.sd-app .trn-list-table thead th {
    padding-block: 0.7rem;
  }
}

/* Hub tiles: 2 col portrait / 3 col landscape — tile is the touch target; keep min-heights so label never collides with emblem */
html.trn-touch body.sd-app .sd-hub-stage .sd-module-tile__icon {
  flex-shrink: 0;
}

@media (min-width: 721px) and (max-width: 960px) {
  html.trn-touch body.sd-app .sd-hub-stage .sd-module-tile {
    min-height: 156px;
  }

  html.trn-touch body.sd-app .sd-hub-stage .sd-module-tile__icon {
    width: 64px;
    height: 64px;
    min-width: 64px;
    min-height: 64px;
  }
}

@media (min-width: 961px) and (max-width: 1366px) {
  html.trn-touch body.sd-app .sd-hub-stage .sd-module-tile {
    min-height: clamp(132px, 17vh, 164px);
  }

  html.trn-touch body.sd-app .sd-hub-stage .sd-module-tile__icon {
    width: clamp(56px, 7vh, 68px);
    height: clamp(56px, 7vh, 68px);
    min-width: 0;
    min-height: 0;
  }
}

html.trn-touch body.sd-app .sd-hub-stage .sd-module-tile__copy {
  flex: 0 1 auto;
  min-width: 0;
  width: 100%;
}

html.trn-touch body.sd-app .sd-hub-stage .sd-module-tile__label {
  font-size: max(1rem, 16px);
}

/* Scroll clearance: taskbar + voice dock FAB must not cover bottom tile row */
html.trn-touch body.sd-app .sd-desktop--hub {
  scroll-padding-bottom: calc(
    var(--sd-chrome-bottom, 48px) + var(--sd-voice-dock-size, 68px) + 12px
  );
}

html.trn-touch body.sd-app .sd-hub-stage {
  padding-bottom: calc(
    var(--sd-chrome-bottom, 48px) + var(--sd-voice-dock-size, 68px) + 16px
  );
}

/* Touch landscape: do not clip hub in desktop densify (overflow:hidden squishes icon + label) */
@media (min-width: 961px) {
  html.trn-touch body.sd-app .sd-desktop--hub {
    overflow-y: auto;
  }

  html.trn-touch body.sd-app .sd-hub-stage {
    height: auto;
    max-height: none;
    overflow: visible;
  }
}

@media (min-width: 721px) and (max-width: 960px) {
  html.trn-touch body.sd-app .sd-hub-stage .sd-desktop-icons {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (min-width: 961px) and (max-width: 1366px) {
  html.trn-touch body.sd-app .sd-hub-stage .sd-desktop-icons {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}

@media (min-width: 1367px) {
  html.trn-touch body.sd-app .sd-hub-stage .sd-desktop-icons {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }
}

/* KPI cards bigger on touch tablets */
@media (min-width: 721px) {
  html.trn-touch body.sd-app .trn-kpi {
    min-height: 7rem;
  }

  html.trn-touch body.sd-app .trn-kpi-icon {
    width: 48px;
    height: 48px;
  }
}

@media (min-width: 1100px) {
  html.trn-touch body.sd-app .trn-kpi-grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }
}

/* Forms: 2-col grid in landscape; sticky action bar sits above chrome-bottom */
@media (min-width: 961px) and (max-width: 1366px) {
  html.trn-touch body.sd-app .trn-stack-form .row-2col,
  html.trn-touch body.sd-app .trn-form-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0.9rem 1.25rem;
  }
}

html.trn-touch body.sd-app .trn-form-actions--sticky,
html.trn-touch body.sd-app .trn-form-sticky {
  bottom: var(--sd-chrome-bottom);
}

/* ── 8. Modals / offcanvas on tablets ──────────────────── */
html.trn-touch body.sd-app .modal-dialog {
  max-width: min(640px, calc(100vw - 2rem));
}

html.trn-touch body.sd-app .modal-header .btn-close {
  margin: -0.25rem -0.25rem -0.25rem auto;
}

/* ── 9. Charts: tap, not hover; legend ≥14px ───────────── */
html.trn-touch body.sd-app canvas,
html.trn-touch body.sd-app .echarts-for-react {
  touch-action: pan-y;
}

/* ── 10. Low-end / slow devices: drop backdrop blur + heavy motion ── */
html.trn-lowend .sd-fund-sidebar,
html.trn-lowend .sd-window,
html.trn-lowend .sd-start-menu,
html.trn-lowend .sd-notification-center,
html.trn-lowend .sd-search-overlay,
html.trn-lowend .sd-taskbar,
html.trn-lowend .trn-voice-modal__panel,
html.trn-lowend .fund-peek-panel {
  -webkit-backdrop-filter: none !important;
  backdrop-filter: none !important;
}

html.trn-lowend .trn-auth-video,
html.trn-lowend video[autoplay] {
  display: none;
}

@media (update: slow) {
  html.trn-touch * {
    -webkit-backdrop-filter: none !important;
    backdrop-filter: none !important;
  }
}

/* ── 11. Reduced motion ────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
  html.trn-touch .sd-fund-sidebar,
  html.trn-touch .sd-fund-sidebar__backdrop,
  html.trn-touch .sd-edge-peek {
    transition: none !important;
  }
}

/* ── 12. Safe-area for notch / gesture bars ────────────── */
html.trn-touch body.sd-app .sd-taskbar {
  padding-left: max(0.5rem, var(--sd-safe-left));
  padding-right: max(0.5rem, var(--sd-safe-right));
}

html.trn-touch body.sd-app .sd-window-body {
  padding-left: var(--sd-safe-left);
  padding-right: var(--sd-safe-right);
}

/* ── 13. Hamburger for non-Fund windowed modules (titlebar) ── */
.sd-win-btn--menu {
  display: none;
}

@media (max-width: 960px) {
  html.trn-touch body.sd-app .sd-win-btn--menu {
    display: inline-grid;
  }
}

@media (min-width: 961px) and (max-width: 1366px) {
  html.trn-touch body.sd-app .sd-win-btn--menu {
    display: inline-grid;
  }
}

/* ── 14. Edge-swipe peek handle (gestures) ─────────────── */
.sd-edge-peek {
  position: fixed;
  left: 0;
  top: 50%;
  width: 8px;
  height: 64px;
  margin-top: -32px;
  border-radius: 0 8px 8px 0;
  background: var(--sd-accent, #13c6d5);
  opacity: 0;
  pointer-events: none;
  z-index: 9100;
  transition: opacity 160ms ease;
}

.sd-edge-peek.is-visible {
  opacity: 0.85;
}

/* Visible, tap-able handle on the left edge (tablet; alternative to gesture) */
html.trn-touch .sd-edge-handle {
  position: fixed;
  left: 0;
  top: 50%;
  width: 14px;
  height: 56px;
  margin-top: -28px;
  border: 0;
  border-radius: 0 10px 10px 0;
  background: color-mix(in srgb, var(--sd-accent, #13c6d5) 55%, transparent);
  z-index: 9050;
  padding: 0;
  display: none;
}

@media (min-width: 721px) and (max-width: 960px) {
  html.trn-touch body[data-sd-module="fundraising"]:not(.sd-fund-sidebar-open) .sd-edge-handle {
    display: block;
  }
}

/* ── 15. Tablet/APK: hide OS search chrome (accidental tap → keyboard / Ctrl+K) ── */
html.trn-touch body.sd-app .sd-start-search,
html.trn-touch body.sd-app .sd-hub-search {
  display: none !important;
}

html.trn-touch body.sd-app #sd-search-btn {
  display: none !important;
}

/* PWA standalone: no center voice dock — less bottom scroll padding */
html.trn-touch.trn-pwa-standalone body.sd-app .sd-desktop--hub {
  scroll-padding-bottom: calc(var(--sd-chrome-bottom, 48px) + 12px);
}

html.trn-touch.trn-pwa-standalone body.sd-app .sd-hub-stage {
  padding-bottom: calc(var(--sd-chrome-bottom, 48px) + 16px);
}
