/*
 * RedemptionArc — mobile corrections.
 *
 * Loaded LAST in both shells (includes/header.php and admin/includes/header.php)
 * so it can win a specificity tie without !important, and so removing one
 * <link> is enough to roll the whole thing back.
 *
 * Every rule here was written against a measured number from a real page in a
 * real 375px viewport, not from reading the markup. The measurement is quoted in
 * the comment above each block, because a mobile fix with no number attached is
 * indistinguishable from a guess.
 *
 * NOTHING in this file edits an existing declaration in style.css, theme.css,
 * platform.css, content.css, admin.css or motion.css. It only adds rules.
 *
 * The recurring cause behind most of what follows is one CSS rule most people
 * meet as a mystery: a flex or grid item defaults to `min-width: auto`, which
 * means "never get narrower than your own min-content". A <select> whose widest
 * <option> is a long Persian sentence, or a <pre> holding an 80-column command,
 * therefore does not shrink on a phone — it pushes its container wider than the
 * screen, and every ancestor grows with it. `min-width: 0` on the item, plus a
 * scroll container on the genuinely unshrinkable thing, is the fix in each case.
 */

/* =========================================================
   1. Article body: a code block must not resize the page
   =========================================================
   Measured on /article/sql-injection-defence at 375px:
     - <pre> min-content width ....... 515px
     - grid column it forced ......... 599px
     - page horizontal scroll ........ 244px
     - elements reported overflowing .. 92
   One code line with an nmap command widened the whole article column, so all
   92 descendants inherited a width larger than the phone.

   The block builder's own code blocks (.ra-pre in content.css) already set
   overflow-x:auto and were never affected — which is exactly why articles
   written in the builder look fine and an article holding raw <pre> HTML does
   not. This extends the same treatment to plain <pre> from the legacy `content`
   column.

   Deliberately NOT inside a media query: a code block that scrolls instead of
   stretching its container is correct at every width. Verified harmless on
   desktop — at 1280px the same <pre> reports scrollWidth == clientWidth, so the
   rule is inert and the page renders byte-identically to before.

   After the fix, measured: 375px -> page scroll 0, the <pre> scrolls itself by
   279px; 320px -> page scroll 0, <pre> scrolls by 334px. */
.article-body pre,
.ra-content pre {
  max-width: 100%;
  overflow-x: auto;
}

/* Long unbreakable tokens — a DNS record, a hash, an API key, a URL — inside
   INLINE code. Excludes code inside <pre>, which must keep its line breaks and
   scroll horizontally instead of reflowing.
   Measured on /admin/email.php at 375px: an inline <code> holding a DMARC
   record (v=DMARC1; p=none; rua=mailto:...) overhung its container by 28px and
   was the last thing keeping that page 407px wide after the panel was fixed.

   Confined to phone widths on purpose. `anywhere` lowers an element's
   min-content contribution, so on desktop it could in principle narrow a column
   that is sized by min-content or fit-content and holds inline code — a change
   with no measured defect behind it. The overflow it repairs was only ever
   measured on a phone, so that is where the rule applies. */
@media (max-width: 900px) {
  :not(pre) > code {
    overflow-wrap: anywhere;
  }
}

/* The safety valve for the article grid. With min-width:0 the column can never
   again be widened past the viewport by a single stubborn descendant; anything
   that genuinely cannot shrink (pre above, .ra-table-wrap in content.css) is
   already its own scroll container, so nothing is clipped by this. */
@media (max-width: 900px) {
  .article-layout > * {
    min-width: 0;
  }
}

/* =========================================================
   2. Public search + filter bar
   =========================================================
   Measured on /articles at 375px:
     - .search-bar available width ... 320px
     - it holds 5 flex items, each .search-bar .form-control { flex: 1 }
       (= flex: 1 1 0%) with flex-wrap: nowrap
     - each <select> therefore rendered 40px wide
     - internal overflow per select .. 47px / 34px / 22px
   A 40px dropdown shows no option text at all, so the category, type and level
   filters were unusable on a phone while still being present and tappable.

   Letting the bar wrap is enough. Measured after the fix at 375px: the bar is
   320px wide, every child reports zero internal overflow, and the three filters
   come out 140 / 140 / 150px — readable. It settles into three rows:

       [ icon  search field ]
       [ category | type    ]
       [ level    | جستجو   ]

   The submit button is paired with the last filter rather than given a row of
   its own, which is why it gets the same flex basis as a select; alone it made
   the bar 192px tall on a 812px screen, which is a quarter of the viewport spent
   on a filter form before any article is visible.

   The inline style="max-width:170px" on the selects in articles.php is left
   alone — at this width each column computes to ~140px, comfortably under that
   cap, so the inline value never binds and no !important is needed. */
@media (max-width: 700px) {
  .search-bar {
    flex-wrap: wrap;
    row-gap: 0.6rem;
  }
  .search-bar input.form-control {
    flex: 1 1 12rem;
  }
  .search-bar select.form-control,
  .search-bar .btn {
    flex: 1 1 calc(50% - 0.35rem);
    min-width: 0;
  }
  .search-bar .btn {
    justify-content: center;
  }
}

/* =========================================================
   3. Admin panel: stop one wide child widening the whole page
   =========================================================
   The admin mobile layer in admin.css already does the hard parts — the sidebar
   becomes a drawer at 900px and wide tables become stacked cards at 700px, both
   verified working here. What it does not do is give .admin-main a min-width, so
   the single grid column of .admin-shell is sized to .admin-main's min-content
   instead of to the screen.

   Measured at 375px, page horizontal scroll before -> after this one line:
     - /admin/settings.php ... 166px -> 0   (.admin-main 541px -> 360px)
     - /admin/email.php ......  89px -> 0   (.admin-main 464px -> 360px)
   On settings.php the culprit was .set-tabs, a nav that already had
   overflow-x:auto and could always have scrolled by itself; it was being asked
   for its full 477px because the grid item above it was allowed to grow. After
   this it scrolls internally by 181px, as it was designed to. */
@media (max-width: 900px) {
  .admin-main {
    min-width: 0;
  }

  /* A <select> is sized by its widest <option>, and options never wrap.
     Measured on /admin/email.php at 375px: the send-method select, whose
     longest option reads «تابع mail() پی اچ پی (ساده ولی اغلب اسپم می شود)»,
     had a min-content width of 338px and rendered 350px wide inside a 360px
     screen. Pinning it to the column drops it to 246px; the closed control
     clips the label, which is the normal behaviour for a narrow select and far
     better than a page that scrolls sideways. */
  .admin-main select.form-control {
    width: 100%;
    min-width: 0;
    max-width: 100%;
  }
}

/* =========================================================
   4. Admin: the two spellings of a stacked table
   =========================================================
   admin.css writes the stacking rules as descendants of .table-stack
   (.table-stack tr, .table-stack td, …), which means the class works either on
   the <table> itself or on a wrapper <div> around it. Both spellings are in use:
   nine tables carry the class directly, and admin/tracking.php:993 wraps a
   <div class="table-stack"> around a bare <table>.

   The wrapper spelling is half-treated, because two rules in admin.css name the
   table element specifically and so miss it:

     .panel:has(table) table { min-width: 560px }   (<=900px, forces the scroll)
     .panel table.table-stack { min-width: 0 }      (<=700px, undoes it)

   For a wrapped table the first matches and the second does not, so it keeps a
   560px floor and side-scrolls inside its panel instead of stacking — and its
   <table> box never gets display:block, so the stacked rows end up inside an
   anonymous table cell rather than laid out as plain blocks.

   .admin-main .table-stack table is (0,2,1), which outranks
   .panel:has(table) table at (0,1,2) — :has() takes the specificity of its
   most specific argument, so the bare `table` inside it adds nothing. Relying on
   load order alone would not have been enough here. */
@media (max-width: 700px) {
  .admin-main .table-stack table {
    display: block;
    width: 100%;
    min-width: 0;
  }

  /* A stacked cell is display:flex with the label as a ::before on the same line,
     so a long unbreakable value and its label fight over one ~140px line and the
     value wins by overflowing. admin.css solves this per-field for the four
     labels it knew were long (ایمیل, متن دیدگاه, پیام, عنوان), which means every
     new long field reintroduces the bug.
     Measured on /admin/tracking.php at 375px before this rule: the cell labelled
     «دستگاه اخیر» holds "Chrome 148.0.7778.280 Windows 10/11 …" and overflowed
     itself by 29px, giving the whole stacked table 12px of internal scroll.
     Allowing the value to break makes it structurally impossible for any stacked
     cell — including ones added later — to overflow its card. `anywhere` rather
     than `break-word` on purpose: only `anywhere` lowers the min-content
     contribution, which is what actually lets the flex item shrink. It is also
     the value admin.css already chose for its four fields. */
  .table-stack td {
    min-width: 0;
    overflow-wrap: anywhere;
  }

  /* admin.css already gives this column treatment to the labels whose values are
     long (ایمیل, متن دیدگاه, پیام, عنوان): the label sits above the value instead
     of sharing one line with it. These five are the same shape and were not on
     that list — «جزئیات» holds a whole sentence plus a <code> DNS record,
     «موضوع» an email subject, «فایل» an archive path, «مسیر» a request URI,
     «دستگاه اخیر» a full browser and OS string. The rule above stops them
     overflowing; this one keeps them readable rather than wrapped into a 140px
     ribbon beside their label. */
  .table-stack td[data-label="جزئیات"],
  .table-stack td[data-label="موضوع"],
  .table-stack td[data-label="فایل"],
  .table-stack td[data-label="مسیر"],
  .table-stack td[data-label="دستگاه اخیر"] {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.2rem;
    overflow-wrap: anywhere;
  }

  /* The colspan="4" error row under a failed send in admin/email.php. It has no
     data-label by design, so it needs the wrap treatment on its own. */
  .table-stack tr.mail-err-row td {
    overflow-wrap: anywhere;
  }
}
