/* ─────────────────────────────────────────────────────────────────
 * The admin on a phone.
 *
 * Measured on the device rather than guessed at: Chrome's remote
 * debugger on the Fold, whose cover screen lays out at 320 CSS px.
 * That is where the numbers in the comments below come from.
 *
 * Note for anyone editing this: `templates/admin/base_site.html`
 * overrides `{% block branding %}` with a `.dw-admin-topbar`, and
 * jazzmin's base template has no such block — so that bar is never
 * rendered in the admin and nothing here should style it. The visible
 * top menu comes from JAZZMIN_SETTINGS["topmenu_links"].
 *
 * This file is the one place that answers for phone layout. It is
 * loaded LAST (see templates/admin/base_site.html) so it can correct
 * design.css, admin-override.css and jazzmin's own sheet without any
 * of them needing a media query of their own.
 *
 * Breakpoint is 991.98px, not 768px: AdminLTE moves the sidebar
 * off-canvas below `lg`, and jazzmin's dashboard columns are
 * `col-lg-*`, so that is where the desktop layout actually ends.
 * ───────────────────────────────────────────────────────────────── */

@media (max-width: 991.98px) {

  /* ── The layout grid ────────────────────────────────────────────
   * THIS is the rule that decides whether the admin renders at all.
   *
   * `.app-wrapper` is a CSS grid, `grid-template-columns: 250px 1fr`.
   * Below `lg` the sidebar is taken out of flow (position: fixed,
   * translated off-canvas) but its COLUMN stays 250px wide. On the
   * Fold's cover screen — a 320px layout viewport — that leaves the
   * `1fr` track 0px, and measuring the live page gave exactly that:
   *
   *     grid-template-columns: 304px 0px
   *     .app-main  width 0        .card  width 2
   *
   * Every card was two pixels wide and 31 of them stacked to a 21 000
   * pixel page of single letters. That is what "the pages have
   * nothing in them" was: the content was there, one character per
   * line, in a column two pixels across.
   *
   * The sidebar is fixed here, so its track costs nothing to zero. */
  .app-wrapper { grid-template-columns: 0 minmax(0, 1fr) !important; }

  /* …and then stop using the grid here at all.
   *
   * Zeroing the column fixed the width. The HEIGHT was the same grid
   * misbehaving from the other side: rows are `min-content 1fr
   * min-content`, and a row sized by min-content asks the header how
   * tall it would be at its NARROWEST — every search box and menu item
   * stacked. On the device that came out at 396px against 217px of
   * actual content, and `align-items: stretch` then stretched the bar
   * to fill it. A third of the screen of empty indigo above the page.
   *
   * Nothing sets a height on `.app-header` (checked against the live
   * page, rule by rule), so grid stretching was the only thing that
   * could make that box taller than what is in it.
   *
   * Below `lg` the grid earns nothing anyway: AdminLTE's own
   * `@media (max-width: 991.98px)` takes the sidebar out of flow —
   * `.sidebar-expand-lg .app-sidebar { position: fixed }` — so there is
   * no second column to place and no rows to align. Plain block flow
   * gives each of header, main and footer its own height, in document
   * order, which is what a phone wants.
   *
   * The column rule above stays as the fallback: if anything ever
   * overrides this one, the layout degrades to "tall header" rather
   * than "no content column". */
  .app-wrapper { display: block !important; }

  /* ── jazzmin's navbar ───────────────────────────────────────────
   * `navbar-expand` carries no breakpoint, so this bar never
   * collapses: the three search forms and the menu stay inline at any
   * width. Give each row its own line instead. */
  .app-header > .container-fluid {
    flex-wrap: wrap;
    row-gap: 4px;
    padding: 4px 8px;
  }
  .app-header form.d-flex {
    flex: 1 1 100%;
    margin: 0 !important;
  }
  /* Bootstrap 5 wraps `.input-group` by default, so the magnifier
   * dropped onto a second line under every search box — three rows of
   * wasted height. Input and button belong together. */
  .app-header .input-group { flex-wrap: nowrap !important; }
  .app-header .input-group .form-control { min-width: 0; }

  /* jazzmin marks its top-menu links `d-none d-sm-inline-block`, so a
   * 320px screen loses Home, OpenAPI, ReDoc and every dropdown — the
   * navigation, on the device that has the least of it. Bring them
   * back on their own sideways-scrolling line. */
  .app-header > .container-fluid > ul.navbar-nav:first-of-type {
    flex: 1 1 100%;
    order: 2;
    flex-wrap: nowrap;
    overflow-x: auto;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
  }
  .app-header > .container-fluid > ul.navbar-nav:first-of-type::-webkit-scrollbar { display: none; }
  .app-header > .container-fluid > ul.navbar-nav:first-of-type > li.nav-item {
    display: list-item !important;
  }
  .app-header .nav-link { white-space: nowrap; }

  /* ── Content area ───────────────────────────────────────────────
   * No left margin: the sidebar is off-canvas here, and any margin
   * left over from the desktop rule is content pushed off-screen. */
  .app-main,
  .app-content,
  .app-content-header,
  .app-footer { margin-left: 0 !important; }

  .app-content > .container-fluid,
  .app-content-header > .container-fluid {
    padding-left: 12px;
    padding-right: 12px;
  }

  /* One column. jazzmin's dashboard already says col-12 below lg, but
   * the change-form's own two-column blocks do not. */
  .app-content .col-lg-3,
  .app-content .col-lg-9,
  .app-content .col-md-6 { width: 100%; max-width: 100%; flex: 0 0 100%; }

  /* ── Tables ─────────────────────────────────────────────────────
   * A change list is wider than any phone. Scroll it inside its card
   * rather than letting it stretch the page: a page that scrolls
   * sideways as a whole loses its own header and filters. */
  .card-body,
  .app-content .table-responsive,
  #changelist-form { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .app-content table { max-width: none; }
  .app-content table th,
  .app-content table td { white-space: nowrap; }
  /* …except the columns that carry prose, which should wrap. */
  .app-content table td.field-__str__,
  .app-content table td .readonly { white-space: normal; word-break: break-word; }

  /* ── Change list controls ───────────────────────────────────────
   * Filters are a sidebar on desktop. Put them above the results,
   * collapsed height, instead of beside them at 0 px wide. */
  #changelist-filter {
    position: static !important;
    width: 100% !important;
    max-width: 100% !important;
    margin: 0 0 12px 0 !important;
    max-height: 40vh;
    overflow-y: auto;
  }
  #toolbar form#changelist-search { display: flex; flex-wrap: wrap; gap: 6px; }
  #toolbar form#changelist-search input[type="text"] { flex: 1 1 100%; }
  .object-tools {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    float: none !important;
    margin: 0 0 12px 0 !important;
  }
  .object-tools li { margin: 0 !important; }

  /* Bulk-action bar: the select and the Go button on one line. */
  .actions {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 6px;
  }
  .actions select { flex: 1 1 60%; min-width: 0; }

  /* ── Change form ────────────────────────────────────────────────
   * Label above input, every input the full width. Django's default
   * is a side-by-side pair sized for a 1200 px window. */
  .form-row,
  .form-row .field-box { display: block !important; width: 100% !important; }
  .form-row label {
    display: block !important;
    width: auto !important;
    margin-bottom: 4px !important;
    float: none !important;
  }
  .app-content input[type="text"],
  .app-content input[type="email"],
  .app-content input[type="url"],
  .app-content input[type="number"],
  .app-content input[type="password"],
  .app-content textarea,
  .app-content select {
    width: 100% !important;
    max-width: 100% !important;
    box-sizing: border-box;
  }
  /* Save row: buttons wrap instead of overflowing, and it stays
   * reachable at the bottom of a long form. */
  .submit-row {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
  }
  .submit-row input,
  .submit-row a { float: none !important; margin: 0 !important; }

  /* Inlines are tables of forms; let them scroll like any other. */
  .inline-group { overflow-x: auto; }

  /* ── Footer ─────────────────────────────────────────────────────*/
  #footer .dw-row {
    flex-direction: column;
    align-items: flex-start;
    gap: 6px;
  }
}

/* Small phones — the same layout, one size down. */
@media (max-width: 400px) {
  .app-content > .container-fluid { padding-left: 8px; padding-right: 8px; }
  .app-header .nav-link { font-size: 13px; padding: 6px 8px; }
  .app-content .card-header { padding: 8px 12px; }
}
