/*
 * ============================================================================
 * ShaFX v9.20d — Mobile + iPad responsive hotfix
 * ============================================================================
 * Loaded LAST so it overrides existing mobile.css rules.
 * Targets the actual broken patterns:
 *   - Admin sidebar covers content on iPad (768-1024px)
 *   - Pay-now form fields overflow on mobile
 *   - Tables don't scroll horizontally
 *   - Grid layouts don't collapse cleanly at iPad portrait (768px)
 *   - Cards stack too narrow on phones (320-400px)
 *   - Top bars and floating action buttons miss safe areas
 * ============================================================================
 */

/* ----------------------------------------------------------------------------
 * 0. UNIVERSAL — small safety nets that apply at every size
 * ---------------------------------------------------------------------------- */
* { box-sizing: border-box; }

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

table {
  table-layout: auto;
}

/* Wrap any wide table in a scroll container automatically */
.table-scroll, .responsive-table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: thin;
}

/* Inputs and textareas never overflow */
input, select, textarea {
  max-width: 100%;
  min-width: 0;
}

/* ----------------------------------------------------------------------------
 * 1. iPAD (768–1024px) — the missing breakpoint
 * ---------------------------------------------------------------------------- */
@media (min-width: 768px) and (max-width: 1024px) {

  /* Admin shell: sidebar still visible but narrower; content gets the rest */
  .app-shell {
    display: flex;
    flex-direction: row;
  }

  .app-side {
    width: 200px !important;
    min-width: 200px !important;
    flex-shrink: 0;
    transform: translateX(0) !important;
  }

  .app-side .brand-mark { font-size: 0.9rem; }
  .app-side .group-label { font-size: 0.66rem; }
  .app-side .app-nav a {
    font-size: 0.84rem;
    padding: 8px 12px;
  }
  .app-side .app-nav a svg {
    width: 16px;
    height: 16px;
  }

  .app-main {
    flex: 1;
    min-width: 0;
    width: calc(100% - 200px);
  }

  .app-content {
    padding: 18px;
  }

  /* Pay-now form: 2-column on iPad portrait */
  .grid-2-cols-tablet,
  div[style*="grid-template-columns:1fr 1fr"] {
    grid-template-columns: 1fr 1fr !important;
  }

  /* But any 3-col layout collapses to 2 */
  div[style*="grid-template-columns:1fr 1fr 1fr"],
  div[style*="repeat(3"] {
    grid-template-columns: 1fr 1fr !important;
  }

  /* Wide stat rows collapse to 3 per row */
  div[style*="repeat(auto-fit,minmax(160px"] {
    grid-template-columns: repeat(3, 1fr) !important;
  }
}

/* ----------------------------------------------------------------------------
 * 2. iPAD PORTRAIT (768px exactly and just below)
 * ---------------------------------------------------------------------------- */
@media (min-width: 600px) and (max-width: 900px) and (orientation: portrait) {
  .app-side {
    width: 180px !important;
    min-width: 180px !important;
  }
  .app-side .app-nav a {
    font-size: 0.8rem;
    padding: 7px 10px;
  }
  .app-side .group-label {
    padding: 9px 10px 4px;
  }
}

/* ----------------------------------------------------------------------------
 * 3. MOBILE (≤ 767px) — sidebar becomes drawer
 * ---------------------------------------------------------------------------- */
@media (max-width: 767px) {

  .app-shell {
    display: block;
  }

  /* Sidebar: hidden by default, slides in when .open */
  .app-side {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: 280px;
    max-width: 85vw;
    z-index: 1000;
    transform: translateX(-105%);
    transition: transform 0.22s ease;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    box-shadow: 4px 0 24px rgba(0,0,0,0.4);
  }

  .app-side.open {
    transform: translateX(0);
  }

  /* Backdrop when drawer is open */
  body.side-open::after {
    content: "";
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    z-index: 999;
  }

  body.side-open {
    overflow: hidden;
  }

  /* Main content uses full width on mobile */
  .app-main {
    width: 100%;
    margin-left: 0;
  }

  .app-content {
    padding: 14px;
  }

  /* Topbar — hamburger trigger always visible */
  .app-top {
    position: sticky;
    top: 0;
    z-index: 50;
    padding: 10px 14px;
  }

  [data-app-toggle] {
    display: inline-flex !important;
    align-items: center;
    justify-content: center;
    width: 38px;
    height: 38px;
    border-radius: 8px;
    background: rgba(255,255,255,0.05);
    border: 1px solid rgba(255,255,255,0.1);
    color: #fff;
    cursor: pointer;
  }

  /* Page heading: reduce sizes */
  h1, h2 { font-size: 1.4rem; line-height: 1.2; }
  h3     { font-size: 1.1rem; }

  /* Wide tables: force horizontal scroll wrapper */
  table {
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    white-space: nowrap;
  }
  table th, table td {
    white-space: nowrap;
  }

  /* Any grid layout collapses to single column on phones */
  div[style*="grid-template-columns:1fr 1fr"],
  div[style*="grid-template-columns:1fr 1fr 1fr"],
  div[style*="repeat(auto-fit,minmax(280px"],
  div[style*="repeat(auto-fit,minmax(260px"],
  div[style*="repeat(auto-fit,minmax(220px"],
  div[style*="repeat(auto-fit,minmax(180px"],
  div[style*="repeat(auto-fit,minmax(160px"],
  div[style*="repeat(auto-fit,minmax(140px"],
  div[style*="repeat(3"],
  div[style*="repeat(4"] {
    grid-template-columns: 1fr !important;
  }

  /* Pay-now form: stack everything */
  .grid-2-cols-tablet,
  .grid-3-cols-tablet {
    grid-template-columns: 1fr !important;
  }

  /* Form inputs and selects: full width on mobile */
  input[type="text"], input[type="number"], input[type="email"],
  input[type="password"], input[type="date"], select, textarea {
    width: 100% !important;
    font-size: 16px;  /* prevents iOS zoom on focus */
  }

  /* Buttons: full width on mobile by default */
  button[type="submit"] {
    width: 100%;
    padding: 12px 16px;
  }

  /* Cards: less internal padding on tiny screens */
  div[style*="border-radius:14px"][style*="padding:22px"],
  div[style*="border-radius:14px"][style*="padding:18px"] {
    padding: 14px !important;
  }

  /* Hero sections: smaller padding */
  .pp-hero, .hero-canvas, .fr-hero {
    padding: 48px 16px 36px !important;
  }
  .pp-hero h1, .hero-canvas h1, .fr-hero h1 {
    font-size: 1.8rem !important;
    line-height: 1.15 !important;
  }

  /* Stat KPI rows: smaller numbers so they fit */
  div[style*="font-size:2rem"][style*="font-weight:900"] {
    font-size: 1.5rem !important;
  }

  /* Toolbars with multiple buttons: wrap to multiple rows */
  .cta-row, [class*="toolbar"] {
    flex-wrap: wrap;
    gap: 8px !important;
  }
  .cta-row a {
    flex: 1 1 auto;
    text-align: center;
    min-width: 140px;
  }
}

/* ----------------------------------------------------------------------------
 * 4. SMALL PHONES (≤ 400px)
 * ---------------------------------------------------------------------------- */
@media (max-width: 400px) {
  .app-content { padding: 10px; }
  h1 { font-size: 1.2rem; }

  /* Even more compact card padding */
  div[style*="border-radius:14px"] {
    padding: 12px !important;
  }

  /* Tiny stat cards */
  div[style*="font-size:2rem"][style*="font-weight:900"],
  div[style*="font-size:1.7rem"] {
    font-size: 1.3rem !important;
  }

  /* Hide non-critical metadata text */
  .hide-on-small { display: none !important; }
}

/* ----------------------------------------------------------------------------
 * 5. SAFE AREAS for notched phones (iPhone X+)
 * ---------------------------------------------------------------------------- */
@supports (padding: max(0px)) {
  body {
    padding-top: env(safe-area-inset-top);
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
  }
  .app-side, .app-top {
    padding-top: max(10px, env(safe-area-inset-top));
  }
}

/* ----------------------------------------------------------------------------
 * 6. TRADER DASHBOARD specific
 * ---------------------------------------------------------------------------- */
@media (max-width: 767px) {
  .dashboard-grid,
  .trader-dashboard {
    grid-template-columns: 1fr !important;
    gap: 12px !important;
  }
  .dashboard-card {
    padding: 14px !important;
  }
}

/* ----------------------------------------------------------------------------
 * 7. RTL HARDENING — for Kurdish/Arabic when the translation system lands
 * ---------------------------------------------------------------------------- */
[dir="rtl"] .app-side {
  left: auto;
  right: 0;
  transform: translateX(105%);
  border-left: 1px solid rgba(255,255,255,0.06);
  border-right: 0;
  box-shadow: -4px 0 24px rgba(0,0,0,0.4);
}
[dir="rtl"] .app-side.open {
  transform: translateX(0);
}

/* ----------------------------------------------------------------------------
 * 8. PRINT
 * ---------------------------------------------------------------------------- */
@media print {
  .app-side, .app-top, [data-app-toggle], .cta-row { display: none !important; }
  .app-content { padding: 0; }
}
