/* =====================================================================
   On a phone
   =====================================================================
   Loaded after app.css on every layout. Everything here is inside a media
   query, so a desktop is not touched by any of it.

   What an audit of every screen at 390px found, and what answers it:

   1. DATA TABLES CUT OFF. Eight columns do not fit, so each table scrolled
      sideways inside its box and the Status and Actions columns were out of
      sight. The page was not "wider than the screen", so nothing complained.
      Each row is now a card of "Heading  Value" lines
      (public/assets/js/responsive-tables.js labels the cells).

   2. HOLES IN FILTER PANELS. `.filterbar .field` carries `flex: 1 1 10rem`,
      which is a *width* in a row and a *height* in a column. Stacked on a
      phone, every filter field was held open 160px tall (256px for the wide
      one), with the box floating at the top of an empty rectangle.

   3. CONTROLS WIDER THAN THEIR CARD. A date input has an intrinsic width in
      Safari that ignores `width: 100%`, so "Submitted from" ran off the
      right edge of its panel.

   4. TOO SMALL TO HIT, TOO SMALL TO READ. Buttons of 24-32px, flags of 28px,
      writing at 10.9px.

   5. TWO MENU ICONS. The drawer button and the desktop "collapse the
      sidebar to a rail" toggle sat side by side. The second means nothing
      when the sidebar is already off screen.
   ===================================================================== */

@media (max-width: 900px) {

  /* 5 -------------------------------------------------------------- */
  .railtoggle, html.js .railtoggle { display: none; }

  /* 4: a thumb, not a pointer ------------------------------------- */
  .btn-sm, .rowactions .btn { min-height: 40px; padding-inline: .9rem; }
  .flagbtn { min-height: 38px; min-width: 46px; }
  .iconbtn { min-width: 40px; min-height: 40px; }
  .filterchips .chip, .chiprow .chip { min-height: 40px; display: inline-flex; align-items: center; }
  .input, .select, input[type=search], input[type=date], input[type=text],
  input[type=email], input[type=tel], input[type=number], input[type=password],
  select { min-height: 44px; }

  /* Nothing on a phone reads at 11px. */
  .tag, .brand .tag, .sidebar .brand .tag, .more, .stat.is-openable .more,
  .none, .imagefield .preview .none, .badge, .fieldtable .badge, .tiny, .small,
  .table th, th.flagcol, .flagcol, .gtree-head, .badge-mute { font-size: max(.78rem, 12px); }
  .table th { letter-spacing: .04em; }

  input[type=file] { min-height: 44px; width: 100%; font-size: .9rem; padding: .35rem 0; }
  input[type=file]::file-selector-button {
    min-height: 40px; padding: .5rem .9rem; margin-right: .75rem; cursor: pointer;
    border: 1px solid var(--line); border-radius: var(--radius-sm); background: #fff;
    font: inherit; font-weight: 600; color: var(--ink);
  }

  /* The column chooser opens from the right edge of the toolbar. At phone
     width that put its left half off the screen, with the Apply button
     beyond reach; it is a sheet from the bottom instead. */
  .colmenu {
    position: fixed; left: .75rem; right: .75rem; top: auto; bottom: .75rem; width: auto;
    max-height: 72vh; overflow-y: auto; z-index: 80;
    box-shadow: 0 -8px 40px rgba(15,23,42,.25);
  }
  .colmenu-list { max-height: 44vh; }

  /* A field marked wide spans two columns of a grid that is now one. */
  .form-grid { grid-template-columns: minmax(0, 1fr); }
  .form-grid > .wide, .form-grid > .span-2 { grid-column: 1 / -1; }

  /* 3 -------------------------------------------------------------- */
  input[type=date], input[type=time], input[type=datetime-local],
  input[type=month], input[type=week] {
    display: block; width: 100%; max-width: 100%; min-width: 0; box-sizing: border-box;
    -webkit-appearance: none; appearance: none;
    padding-block: .55rem; text-align: left; line-height: 1.3;
  }
  input[type=date]::-webkit-date-and-time-value { text-align: left; min-height: 1.3em; }
  input[type=date]::-webkit-datetime-edit { padding: 0; }
  .input, .select, .textarea, input, select, textarea { max-width: 100%; }

  /* 2 -------------------------------------------------------------- */
  .filterbar { flex-direction: column; align-items: stretch; gap: 0; padding: 1rem; }
  .filterbar .field,
  .filterbar .field.grow {
    flex: 0 0 auto;          /* the fix: in a column, flex-basis is a height */
    width: 100%; min-width: 0; margin-bottom: .85rem;
  }
  .filterbar .field.filteractions {
    flex-direction: row; margin-bottom: 0; gap: .6rem;
  }
  .filterbar .field.filteractions .btn { flex: 1 1 0; justify-content: center; }
  .filterbar .field.filteractions .btn-primary { flex: 2 1 0; }

  /* Two short fields share a line: a from/to pair should read as a pair. */
  .filterbar .field.pair { width: calc(50% - .4rem); }

  /* Page-level buttons: wrap, and fill the line when alone. */
  .page-actions, .toolbar { flex-wrap: wrap; gap: .6rem; }
  .page-actions > .btn:only-child, .page-actions > .toolbar:only-child { width: 100%; }
  .page-actions > .btn:only-child { justify-content: center; }
  .panel > header { flex-wrap: wrap; gap: .5rem .75rem; }
  .panel > header .btn { flex: none; }
  .panel > .body { padding: 1rem; }

  /* Tiles: two across, and a lone last one takes the whole row. */
  .statrow { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .65rem; }
  .statrow .stat-card:last-child:nth-child(odd) { grid-column: 1 / -1; }
  .statrow .stat-card .num { font-size: 1.35rem; overflow-wrap: anywhere; }

  /* The status line above the enrolment list. */
  .statusline, .page-actions .badge { align-self: center; }

  /* Pagination reads as one bar. */
  .pager { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: .5rem; }
  .pager .btn:last-child { justify-self: end; }
}

/* 1 ------------------------------------------------------------------ */
@media (max-width: 760px) {

  .gridscroll, .tablescroll { overflow: visible; max-height: none; }
  .tablescroll { margin: 0; padding: 0; }
  .tablescroll > .table.rt, .tablescroll > .reporttable.rt, .table.rt { min-width: 0; width: 100%; }
  .panel > .body, .panel .body { overflow-x: visible; }

  .rt, .rt tbody, .rt tr, .rt td { display: block; width: 100%; box-sizing: border-box; }
  .rt { border: 0; font-size: .95rem; }

  .rt thead { display: none; }

  .rt tbody { display: grid; grid-template-columns: minmax(0, 1fr); gap: .7rem; padding: .7rem; background: var(--bg-soft); }
  .rt tbody tr {
    display: flex; flex-direction: column;
    background: #fff; border: 1px solid var(--line); border-radius: var(--radius);
    padding: .3rem .95rem .45rem; box-shadow: var(--shadow-sm, 0 1px 2px rgba(15,23,42,.04));
  }
  .rt tbody tr:hover { background: #fff; }
  .rt tbody tr.is-selected { border-color: var(--brand); }
  .rt tbody tr.is-void { opacity: .65; }

  .rt td {
    display: grid; grid-template-columns: minmax(5.5rem, 36%) minmax(0, 1fr);
    column-gap: .75rem; align-items: baseline;
    padding: .55rem 0; border: 0; border-bottom: 1px dashed var(--line-soft);
    text-align: left; white-space: normal; overflow-wrap: anywhere; word-break: normal;
  }
  .rt td:last-child { border-bottom: 0; }
  .rt td::before {
    content: attr(data-label);
    font-size: .72rem; line-height: 1.4; text-transform: uppercase; letter-spacing: .05em;
    color: var(--ink-faint); font-weight: 700;
  }
  .rt td[data-label=""] { display: block; }
  .rt td[data-label=""]::before { display: none; }
  .rt td.rt-empty { display: none; }
  .rt td.num { text-align: left; }
  .rt td > * { min-width: 0; max-width: 100%; }
  .rt tbody tr { min-width: 0; }
  .rt td a:not(.btn) { overflow-wrap: anywhere; }
  .rt td .badge, .rt td .chip { justify-self: start; }

  /* What the row is — the number, the name — heads the card. */
  .rt td.rt-title {
    display: block; padding: .7rem 0 .6rem; font-size: 1.02rem; font-weight: 600;
    border-bottom: 1px solid var(--line);
  }
  .rt td.rt-title::before { display: none; }

  /* The tick box rides above the card's content, out of the way. */
  .rt td.tickcol { order: -1; display: block; padding: .5rem 0 0; border: 0; }
  .rt td.tickcol::before { display: none; }

  /* Actions: a row of buttons at the foot, not a column of labels. */
  .rt td.stickyend, .rt td.rowactions {
    position: static; display: flex; flex-wrap: wrap; gap: .5rem;
    padding: .7rem 0 .15rem; background: transparent; border-bottom: 0;
  }
  .rt td.rowactions::before, .rt td.stickyend::before { display: none; }
  .rt td.rowactions .btn, .rt td.stickyend .btn { flex: 1 1 auto; justify-content: center; }
  .rt td.rowactions form { display: contents; }

  /* A table already inside a padded panel does not pad its cards a second time. */
  .panel > .body .rt tbody, .panel > .body > .tablescroll .rt tbody { padding: 0; background: transparent; }
  .panel > .body .rt-tools { padding: 0 0 .7rem; }

  /* The form builder: six on/off switches per field belong side by side,
     not in six labelled rows. Dragging by a handle does not work under a
     finger, so the handle goes. */
  .fieldtable.rt tbody tr { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); column-gap: .6rem; }
  .fieldtable.rt td { grid-column: 1 / -1; }
  .fieldtable.rt td.drag-handle { display: none; }
  .fieldtable.rt td.flagcol {
    grid-column: auto; display: flex; flex-direction: column; align-items: flex-start; gap: .3rem;
    padding: .6rem 0 .3rem; border-bottom: 0;
  }
  .fieldtable.rt td.flagcol::before { order: -1; }
  .fieldtable.rt td.flagcol form { width: 100%; }
  .fieldtable.rt td.flagcol .flagbtn { width: 100%; }

  /* A control inside a cell fills it instead of pushing the card wide. */
  .rt td select, .rt td input:not([type=checkbox]):not([type=radio]), .rt td textarea {
    width: 100%; min-width: 0;
  }
  .rt td code { white-space: normal; overflow-wrap: anywhere; }

  /* The member grid: headings are sort links and filter boxes. They are
     folded behind "Sort & filter" rather than lost. */
  .rt-tools { padding: .7rem .7rem 0; }
  .rt-tools .rt-toggle { width: 100%; justify-content: center; }
  .rt.rt-open thead { display: block; padding: .7rem; background: #fff; border-bottom: 1px solid var(--line); }
  .rt.rt-open thead tr { display: block; }
  .rt.rt-open thead tr:first-child { display: flex; flex-wrap: wrap; gap: .4rem; margin-bottom: .8rem; }
  .rt.rt-open thead th { display: block; width: auto; padding: 0; border: 0; position: static; background: transparent; }
  .rt.rt-open thead tr:first-child th.sortable a {
    padding: .45rem .8rem; border: 1px solid var(--line); border-radius: 999px; background: #fff;
  }
  .rt.rt-open thead tr:first-child th.sorted a { border-color: var(--brand); background: var(--brand-tint, #f2f6ff); }
  .rt.rt-open thead tr:first-child th:not(.sortable):not(.tickcol) { display: none; }
  .rt.rt-open thead tr.filterrow th { width: 100%; margin-bottom: .65rem; }
  .rt.rt-open thead tr.filterrow th::before {
    content: attr(data-label); display: block; margin-bottom: .25rem;
    font-size: .72rem; letter-spacing: .05em; text-transform: uppercase;
    color: var(--ink-faint); font-weight: 700;
  }
  .rt.rt-open thead tr.filterrow th.tickcol,
  .rt.rt-open thead tr.filterrow th[data-label=""]::before { display: none; }
  .rt.rt-open thead tr.filterrow th.tickcol { display: none; }
  .rt.rt-open thead tr.filterrow th .muted { display: none; }
  .rt.rt-open thead tr.filterrow th.stickyend { margin: .3rem 0 0; }
  .rt.rt-open thead tr.filterrow th.stickyend .btn { width: 100%; justify-content: center; min-height: 44px; }
  .rt.rt-open thead .filterinput { width: 100%; min-width: 0; font-size: 16px; min-height: 44px; }
}

@media (min-width: 761px) {
  .rt-tools { display: none; }
}

/* ---------------------------------------------------------------------
   Dashboard on a phone
   --------------------------------------------------------------------- */
@media (max-width: 760px) {
  .dash-hero { padding: 1rem; }
  .dash-actions, .dash-years { width: 100%; }
  .dash-years a { min-height: 40px; padding-inline: .85rem; }
  .dash-years small { font-size: max(.7rem, 12px); }
  .dash-actions .btn { flex: 1 1 auto; justify-content: center; }
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .7rem; }
  .kpi { padding: .85rem .9rem; }
  .kpi:last-child:nth-child(odd) { grid-column: 1 / -1; }
  .kpi-lbl { font-size: max(.72rem, 12px); }
  .dash-row { gap: 1rem; }
  .ch-columns, .ch-area { --ch-h: 8.5rem; padding-left: 2.2rem; }
  .ch-area { --ch-h: 9.5rem; }
  .ch-xl, .ch-xlabels span { font-size: max(.7rem, 12px); }
  .ch-tick i { font-size: max(.7rem, 12px); }
  .ch-barval { font-size: max(.7rem, 12px); }
  .ch-cols { gap: 2px; }
  .ch-legrow { min-height: 44px; }
  .queue li { padding: .7rem 0; }
  .qchip { font-size: max(.8rem, 12px); }
}
@media (max-width: 420px) {
  /* Twelve month names do not fit twelve narrow slots; every second one. */
  .ch-xlabels span:nth-child(even), .ch-cols[style*="--n:12"] .ch-xl { visibility: hidden; }
  .ch-cols[style*="--n:12"] .ch-col:nth-child(odd) .ch-xl { visibility: visible; }
}

/* Phones only: the role editor's permission codes and the backup page's
   long cron lines may wrap instead of pushing the page sideways. */
@media (max-width: 760px) {
  .permtable td.nowrap { white-space: normal; }
  .permtable code, .admin-body code { overflow-wrap: anywhere; word-break: break-word; white-space: normal; }
}
