/* ==========================================================================
   responsive.css  -  Step 14.7: the ONE shared responsive stylesheet.

   Linked by every full-page view (layout.php, layouts/base.php, the sign-in and share pages and the stand-alone statement pages) AFTER their own
   styles, so it wins. Nothing here changes a screen wider than 1024 px, except the rules marked "all widths". Printing is untouched: every rule
   that moves things around is inside @media screen.

   Widths it is built and checked for:  360 px (small phone), 768 px (tablet), 1280 px (laptop).   Breakpoints: 1024, 768, 560.
   tests/run_layout.php checks the files; tests/e2e/layout.js opens every screen in Chromium at the three widths and fails on sideways page scroll.
   ========================================================================== */

/* ---- all widths: nothing may be wider than its box ---------------------- */
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
img, video, canvas, iframe { max-width: 100%; height: auto; }
svg { max-width: 100%; }
pre, code { overflow-wrap: anywhere; }
pre { max-width: 100%; overflow-x: auto; }
.content, .main, .page, .card { min-width: 0; }
.content { grid-template-columns: minmax(0, 1fr); }

/* A table that is wider than the screen scrolls INSIDE its own box (the box is added around every table by Responsive::wrapTables(); tables on
   pages that do not go through the layout get the same behaviour from the screen rule at the bottom). */
.table-scroll { max-width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; overscroll-behavior-x: contain; }
.table-scroll:focus-visible { outline: 2px solid var(--accent, #2563eb); outline-offset: 2px; }
.table-scroll > table { margin: 0; }

/* a page's own ".actions" / ".chip" rules (a form's button bar) must never restyle the top bar, which uses the same class names */
.topbar .actions { position: static; padding: 0; border: 0; border-radius: 0; background: none; box-shadow: none; backdrop-filter: none; display: flex; flex-direction: row; }
.topbar .actions::before, .topbar .actions::after { content: none; }

/* the drawer's close button and the tap-to-close layer exist only where the menu is a drawer (see the 1024 rule) */
.sidebar-close, .nav-backdrop { display: none; }

@media screen {

  /* ---- 1024 and below: the side menu is a drawer (layout.php already does this); the page gets the full width ---- */
  @media (max-width: 1024px) {
    .content { padding: 14px; }
    .page { padding: 14px; }
    .sidebar { z-index: 40; width: min(300px, 86vw); inset: 0 auto 0 0; overflow-y: auto; }
    .grid { grid-template-columns: repeat(6, minmax(0, 1fr)); }
    .grid > * { min-width: 0; }
    .sidebar-close { display: inline-flex; align-items: center; justify-content: center; margin-left: 6px; width: 40px; height: 40px; border: 1px solid var(--divider, #273244); border-radius: 10px; background: transparent; color: inherit; font-size: 16px; cursor: pointer; }
    .brand .version { margin-left: auto; }
    .nav-backdrop.show { display: block; position: fixed; inset: 0; z-index: 39; background: rgba(0, 0, 0, .35); }
    .sidebar.show { box-shadow: none; }            /* the backdrop dims the page now (it can be tapped; a box-shadow cannot) */
    .nav a { min-height: 44px; }
  }

  /* ---- 768 and below: tablet portrait and phones ---- */
  @media (max-width: 768px) {
    body { font-size: 15px; }
    .content { padding: 10px; gap: 12px; }
    .page { padding: 12px; border-radius: 12px; }
    .page .h { flex-wrap: wrap; gap: 8px; }
    .title { font-size: 17px; overflow-wrap: anywhere; }

    /* the top bar wraps: menu button + buttons on the first row, the search box on its own row below */
    .topbar { flex-wrap: wrap; gap: 8px; padding: 8px 10px; }
    .topbar .search { order: 3; flex: 1 1 100%; min-width: 0; }
    .topbar .actions { flex-wrap: wrap; gap: 6px; margin-left: auto; min-width: 0; justify-content: flex-end; }
    .topbar .actions form { margin: 0; }
    .topbar .btn, .topbar .chip { white-space: nowrap; }
    .menu { right: 0; max-width: calc(100vw - 20px); }

    /* the 12-column grid and every multi-column form fall to one column */
    .grid { grid-template-columns: minmax(0, 1fr); }
    .grid > * { grid-column: 1 / -1 !important; }
    .form-grid, .two-col, .cols-2, .cols-3, .cols-4 { grid-template-columns: minmax(0, 1fr) !important; }
    .kpi-row, .kpis, .stats { grid-template-columns: repeat(2, minmax(0, 1fr)) !important; }
    .form-row { flex-wrap: wrap; align-items: stretch; }
    .form-row label { flex: 1 1 100%; }
    .form-row input[type="text"], .form-row input[type="number"], .form-row select { width: 100%; }

    /* controls: big enough to touch, never smaller than 16 px (a smaller field makes iOS zoom the page when it is tapped) */
    input:not([type="checkbox"]):not([type="radio"]):not([type="hidden"]):not([type="range"]), select, textarea { font-size: 16px !important; min-height: 40px !important; max-width: 100%; }
    button, .btn, .button, a.btn { min-height: 40px; }
    input[type="checkbox"], input[type="radio"] { width: 20px; height: 20px; }
    .page form input:not([type="checkbox"]):not([type="radio"]):not([type="hidden"]):not([type="submit"]):not([type="button"]), .page form select, .page form textarea { max-width: 100%; }

    /* long words and addresses wrap instead of pushing the page wide */
    .page, .card, td, th, li, p, h1, h2, h3, a { overflow-wrap: anywhere; }
    td, th { overflow-wrap: normal; }          /* a table cell keeps its words whole: the table scrolls instead */

    /* a row that the page makes with display:flex (a class or an inline style) may wrap, and its children may shrink: a field with a long label or a
       long list of options must not make the row wider than the screen */
    [style*="display:flex"], [style*="display: flex"], .row { flex-wrap: wrap; }
    [style*="display:flex"] > *, [style*="display: flex"] > *, .row > * { min-width: 0; max-width: 100%; }
    [style*="display:flex"] > input, [style*="display: flex"] > input { flex: 1 1 120px; width: auto !important; }
    select { text-overflow: ellipsis; }
    :where(span, div, label, p, td, th):has(> select, > textarea) { min-width: 0; max-width: 100%; }   /* whatever wraps a long list of options may shrink to the screen */

    /* button rows wrap */
    .actions-row, .toolbar, .btn-row, .pg-actions, .lock-actions { display: flex; flex-wrap: wrap; gap: 8px; }

    /* things that sit on top of the page */
    .toasts { left: 10px; right: 10px; top: 10px; }
    .toast { max-width: none; }
    .modal { min-width: 0; width: calc(100vw - 20px); max-width: calc(100vw - 20px); top: 6vh; max-height: 88vh; overflow: auto; }

    /* a table on a page that did not go through the layout (stand-alone pages): scroll inside its own box */
    table:not(.no-scroll) { display: block; max-width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }
    .table-scroll > table:not(.no-scroll) { display: table; max-width: none; overflow: visible; }
  }

  /* ---- 560 and below: phones ---- */
  @media (max-width: 560px) {
    .kpi-row, .kpis, .stats { grid-template-columns: minmax(0, 1fr) !important; }
    .topbar .actions .btn { padding: 6px 9px; font-size: 13px; }
    .brand h1 { font-size: 14px; }
    .crumbs { overflow-wrap: anywhere; }
    .footer { padding: 12px 8px; }
    /* a form's sticky button bar: one slim row, not a tall column that covers a third of the screen */
    .content .page .actions { flex-direction: row !important; flex-wrap: wrap; padding: 10px 12px !important; gap: 8px !important; }
    .content .page .actions > .button, .content .page .actions > .btn, .content .page .actions > button { flex: 1 1 120px; width: auto !important; padding: 10px 14px; }
  }
}
