/* ===========================================================================
   mobile.css — phone layer for the TPS BA Unit portal
   Injected into every page by _build/inject_mobile.py (idempotent).

   RULES OF THE ROAD
   1. Everything lives inside @media (max-width:760px). Desktop cannot regress.
   2. Selectors carry one extra element (nav.qnav, header.hdr, a.brand) so they
      beat the inline <style> that inject_quicknav.py writes AFTER this file.
   3. Logical properties only — the portal is Arabic-first (dir="rtl").
   ======================================================================== */

@media (max-width: 760px) {

  /* ---------------------------------------------------------------- 1. HEADER
     The 709px overflow on all 15 Payments Academy pages came from here:
     .brand is white-space:nowrap and carries a title AND a subtitle, while
     .tools holds two toggle groups that cannot wrap, inside a 60px-tall bar.
     The page body already stacks correctly on phones (.side is display:none,
     .wrap is display:block), so only the bar itself needs to give. */

  header.hdr {
    height: auto;
    min-height: 56px;
    flex-wrap: wrap;
    row-gap: 6px;
    padding-block: 8px;
    align-items: center;
  }
  header.hdr a.brand {
    white-space: normal;
    min-width: 0;
    flex: 1 1 auto;
    font-size: .95rem;
    line-height: 1.25;
    gap: 8px;
  }
  /* the strapline is the single widest offender — desktop only */
  header.hdr a.brand small { display: none; }
  header.hdr a.brand .logo { width: 26px; height: 26px; flex: none; }
  header.hdr a.brand .logo svg { width: 26px; height: 26px; }

  /* breadcrumb duplicates the chapter title shown below it */
  header.hdr .crumb { display: none; }

  header.hdr .tools {
    margin-inline-start: 0;
    flex: 1 0 100%;
    flex-wrap: wrap;
    gap: 6px;
    justify-content: flex-start;
  }
  header.hdr .lang button { padding: 8px 11px; min-height: 40px; }
  header.hdr .lang.trk button { padding: 8px 9px; }

  /* ------------------------------------------------------- 2. QUICK-NAV → BURGER
     Was a sideways-scrolling chip strip. Becomes a disclosure menu so that
     nothing on the page scrolls horizontally. */

  .qnav-toggle {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    min-height: 46px;
    padding: 10px 16px;
    background: #132c4f;
    border: 0;
    border-bottom: 1px solid rgba(255, 255, 255, .09);
    color: #fff;
    font: inherit;
    font-size: .85rem;
    font-weight: 700;
    cursor: pointer;
    text-align: start;
  }
  .qnav-toggle .qnav-bars {
    position: relative;
    width: 18px;
    height: 2px;
    background: currentColor;
    border-radius: 2px;
    flex: none;
    transition: background .15s;
  }
  .qnav-toggle .qnav-bars::before,
  .qnav-toggle .qnav-bars::after {
    content: '';
    position: absolute;
    inset-inline-start: 0;
    width: 18px;
    height: 2px;
    background: currentColor;
    border-radius: 2px;
    transition: transform .18s;
  }
  .qnav-toggle .qnav-bars::before { top: -6px; }
  .qnav-toggle .qnav-bars::after { top: 6px; }
  .qnav-toggle[aria-expanded="true"] .qnav-bars { background: transparent; }
  .qnav-toggle[aria-expanded="true"] .qnav-bars::before { transform: translateY(6px) rotate(45deg); }
  .qnav-toggle[aria-expanded="true"] .qnav-bars::after  { transform: translateY(-6px) rotate(-45deg); }
  .qnav-toggle .qnav-label { flex: none; }
  .qnav-toggle .qnav-current {
    opacity: .72;
    font-weight: 600;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  nav.qnav {
    display: none;
    flex-direction: column;
    overflow-x: visible;
    padding: 0 0 6px;
    gap: 0;
  }
  nav.qnav.qnav-open { display: flex; }
  nav.qnav a {
    flex: none;
    display: flex;
    align-items: center;
    min-height: 46px;
    white-space: normal;
    padding: 10px 18px;
    font-size: .88rem;
    border-bottom: 0;
    border-inline-start: 3px solid transparent;
  }
  nav.qnav a.on {
    border-bottom-color: transparent;
    border-inline-start-color: #e0a526;
    background: rgba(255, 255, 255, .06);
  }

  /* ------------------------------------------------------------- 3. TABLES
     266 tables on the portal; 24 are wider than a phone. mobile.js tags them:
       .m-stack  — 3+ columns, becomes one card per row with its header as a label
       .m-wrap   — 2 columns, simply allowed to wrap instead of scrolling
     Either way the scroll wrapper stops scrolling. */

  .m-scrollfree { overflow-x: visible !important; }

  table.m-wrap { width: 100% !important; min-width: 0 !important; table-layout: fixed; }
  table.m-wrap th,
  table.m-wrap td {
    white-space: normal !important;
    word-break: break-word;
    overflow-wrap: anywhere;
  }

  table.m-stack,
  table.m-stack thead,
  table.m-stack tbody,
  table.m-stack tr,
  table.m-stack td {
    display: block;
    width: 100% !important;
    min-width: 0 !important;
  }
  table.m-stack thead { display: none; }
  table.m-stack tr {
    border: 1px solid var(--line, #e2e7ee);
    border-radius: 12px;
    margin-bottom: 10px;
    padding: 4px 0;
    background: var(--card, #fff);
    overflow: hidden;
  }
  table.m-stack td {
    display: grid;
    grid-template-columns: minmax(88px, 38%) 1fr;
    gap: 12px;
    align-items: start;
    padding: 9px 14px;
    border: 0;
    border-bottom: 1px solid rgba(0, 0, 0, .05);
    white-space: normal !important;
    word-break: break-word;
  }
  table.m-stack tr td:last-child { border-bottom: 0; }
  table.m-stack td::before {
    content: attr(data-label);
    font-weight: 700;
    font-size: .78rem;
    line-height: 1.45;
    color: var(--muted, #6b7480);
  }
  table.m-stack td[data-label=""]::before { display: none; }


  /* ------------------------------------------------- 5. CHAPTER FOOTER BAR
     .chapnav (prev / home / next) plus .prog and .count sit in one unwrapping
     flex row inside the fixed footer. min-width:90px and min-width:80px keep
     it from ever shrinking, which is the residual 494px overflow. */

  .ftr, footer.ftr {
    height: auto;
    min-height: var(--footer-h, 64px);
    flex-wrap: wrap;
    row-gap: 8px;
    column-gap: 6px;
    padding-block: 8px;
    padding-inline: 10px;
  }
  .ftr .btn, .ftr button.btn { padding-inline: 10px; }
  .ftr .nav { gap: 6px; }
  .ftr .prog, .prog { min-width: 0; flex: 1 1 100%; order: -1; }
  .ftr .count, .count { min-width: 0; flex: none; }
  .ftr .keys, .keys { display: none; }          /* keyboard-shortcut hint */

  /* Two rows of controls is the floor; three was 231px — 27% of the screen
     permanently covered. Drop the two labels that the icon already says. */
  .ftr .nav button.btn span, .ftr .nav .btn span { display: none; }
  .chapnav a.btn .ic + span:not(.en):not(.ar) { display: none; }

  .chapnav {
    margin-inline-start: 0;
    flex: 1 1 auto;
    flex-wrap: nowrap;
    gap: 6px;
    justify-content: space-between;
  }
  .chapnav a.btn, .chapnav button.btn {
    font-size: .76rem;
    padding-inline: 10px;
    min-height: 44px;
    flex: 0 1 auto;
    min-width: 0;
  }
  .chapnav a.btn span, .chapnav button.btn span {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  /* the middle "Home" button carries an unambiguous icon */
  .chapnav a.btn.m-iconly span { display: none; }


  /* ------------------------------------------------ 6. CHAPTER TOC DRAWER
     Pre-existing bug, invisible to an overflow test: at <=980px .side becomes
     position:fixed; z-index:45 — but it is only ever display:none inside
     @media print. So on a phone the 290px contents panel sits permanently on
     top of the page, covering the left third and swallowing taps. Turn it
     into a real drawer: closed by default, opened by a button in the header. */

  aside.side, .side {
    display: none;
    inset-block-start: 0;
    height: 100dvh;
    width: min(86vw, 320px);
    z-index: 60;
    box-shadow: 0 0 40px rgba(11, 31, 58, .34);
    padding-block-start: 8px;
  }
  aside.side.m-open, .side.m-open { display: block; }

  .m-scrim {
    position: fixed;
    inset: 0;
    z-index: 55;
    background: rgba(11, 31, 58, .52);
    border: 0;
    padding: 0;
    cursor: pointer;
  }
  .m-scrim[hidden] { display: none !important; }

  .m-toc-btn {
    display: inline-flex;
    align-items: center;
    gap: 7px;
    min-height: 40px;
    padding: 8px 13px;
    border: 1px solid rgba(255, 255, 255, .28);
    border-radius: 9px;
    background: transparent;
    color: #fff;
    font: inherit;
    font-size: .8rem;
    font-weight: 700;
    cursor: pointer;
  }
  .m-toc-btn svg { width: 15px; height: 15px; fill: none; stroke: currentColor; stroke-width: 2; }
  .side .m-toc-close {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    width: calc(100% - 24px);
    margin: 4px 12px 10px;
    min-height: 44px;
    border: 1px solid var(--line, #e2e7ee);
    border-radius: 10px;
    background: var(--card-2, #f8fafc);
    font: inherit;
    font-weight: 700;
    cursor: pointer;
  }

  /* --------------------------------------------------------- 7. STRAGGLERS
     Found by the 65-page sweep, not by reasoning: the template pages use
     .crumbs (plural) rather than .crumb, and long slash-joined strings such
     as "Responsible/Accountable/Consulted/Informed" have no break point. */

  header.hdr .crumbs, .crumbs { display: none; }
  .sec-exp, .sec-exp * { overflow-wrap: anywhere; word-break: break-word; }
  select.jump, .jump { min-height: 44px; max-width: 100%; }


  /* ----------------------------------------------------------- 4. DIAGRAMS
     23 diagrams carry 30–70 labels each. Scaled to a phone their text renders
     at ~2.4px — they fit, but cannot be read. So: a legible preview plus a
     full-screen viewer that can be pinched and panned. */

  /* The button sits UNDER the figure, not on top of it: an overlay covered
     the very artwork it exists to reveal. */
  .m-dgm-open {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    width: 100%;
    min-height: 44px;
    margin-block-start: 10px;
    padding: 10px 14px;
    border: 1px solid rgba(11, 31, 58, .18);
    border-radius: 12px;
    background: #f3f6fa;
    color: #0b1f3a;
    font: inherit;
    font-size: .82rem;
    font-weight: 700;
    cursor: pointer;
  }
  .m-dgm-open svg { width: 14px; height: 14px; fill: none; stroke: currentColor; stroke-width: 2; }
}

/* The viewer itself is not inside the media query — it is only ever opened
   by mobile.js, which only binds below 760px. */
.m-viewer {
  position: fixed;
  inset: 0;
  z-index: 9999;
  background: #0b1f3a;
  display: flex;
  flex-direction: column;
}
.m-viewer[hidden] { display: none !important; }
.m-viewer-bar {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 14px;
  background: #0b1f3a;
  border-bottom: 1px solid rgba(255, 255, 255, .14);
  color: #fff;
  font-size: .8rem;
}
.m-viewer-hint { opacity: .7; }
.m-viewer-close {
  min-height: 44px;
  min-width: 44px;
  padding: 8px 16px;
  border: 1px solid rgba(255, 255, 255, .3);
  border-radius: 10px;
  background: transparent;
  color: #fff;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}
.m-viewer-stage {
  flex: 1;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  touch-action: pinch-zoom pan-x pan-y;
  background: #fff;
  padding: 12px;
  display: flex;
  align-items: safe center;
  justify-content: safe center;
}
/* mobile.js sets the width so the labels land near 13px; this is the floor */
.m-viewer-stage svg {
  width: 920px;
  max-width: none;
  height: auto;
  display: block;
  flex: none;
  margin: auto;
}
body.m-viewer-lock { overflow: hidden; }

@media (prefers-reduced-motion: reduce) {
  .qnav-toggle .qnav-bars,
  .qnav-toggle .qnav-bars::before,
  .qnav-toggle .qnav-bars::after { transition: none; }
}
