/* ==========================================================================
   Mobile & tablet layer. Loaded last; only changes layout below 900px / 640px.
   - off-canvas menu with a backdrop (base.html toggles body.nav-open)
   - tables scroll inside their card; list tables (.stack) become one card per row on phones
   - forms in one column, 16px inputs (no iOS zoom), save buttons kept in view
   ========================================================================== */

/* account links inside the drawer: phones only */
.side-user { display: none; }
.nav-backdrop { display: none; }
.side .side-close { display: none; }

.g-2-1 { grid-template-columns: minmax(0, 2fr) minmax(0, 1fr); }

/* every table scrolls inside its own box, never the page */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; max-width: 100%; }
.content { min-width: 0; }
.content img, .content video { max-width: 100%; height: auto; }
pre, code { overflow-wrap: anywhere; }

@media (max-width: 900px) {
  /* scroll hint: soft shadow on the side that has more columns */
  .table-wrap {
  background:
    linear-gradient(90deg, #fff 30%, rgba(255, 255, 255, 0)) left / 24px 100% no-repeat local,
    linear-gradient(270deg, #fff 30%, rgba(255, 255, 255, 0)) right / 24px 100% no-repeat local,
    radial-gradient(farthest-side at 0 50%, rgba(15, 23, 42, .14), transparent) left / 10px 100% no-repeat scroll,
    radial-gradient(farthest-side at 100% 50%, rgba(15, 23, 42, .14), transparent) right / 10px 100% no-repeat scroll; }
  /* ---------- off-canvas menu ---------- */
  .side { position: fixed; top: 0; bottom: 0; left: 0; z-index: 40; width: min(300px, 86vw); height: 100%;
    transform: translateX(-105%); transition: transform .22s ease; padding-top: calc(14px + env(safe-area-inset-top));
    padding-bottom: calc(30px + env(safe-area-inset-bottom)); overscroll-behavior: contain;
    box-shadow: 0 0 40px rgba(0, 0, 0, .35); }
  .side.open { left: 0; transform: none; }
  .nav-open .nav-backdrop { display: block; position: fixed; inset: 0; z-index: 35; background: rgba(8, 15, 30, .45);
    backdrop-filter: blur(2px); -webkit-backdrop-filter: blur(2px); }
  .nav-open { overflow: hidden; }
  .side .side-close { display: grid; place-items: center; position: absolute; top: calc(10px + env(safe-area-inset-top));
    right: 10px; width: 40px; height: 40px; border: 0; border-radius: 10px; background: rgba(255, 255, 255, .08);
    color: #fff; font-size: 22px; line-height: 1; cursor: pointer; }
  .side .brand { padding-right: 52px; }
  .side a, .side .sec > button { min-height: 44px; display: flex; align-items: center; }
  .side .items a { padding: 10px 12px; font-size: 14px; }
  .side .sec.on > button { min-height: 44px; }
  .crumbs { font-size: 12px; margin-bottom: 10px; }
  .crumbs .c-cur { max-width: 60vw; }
  .side-user { display: flex; flex-wrap: wrap; align-items: center; gap: 2px 4px; margin: 6px 4px 10px; padding: 10px 0 12px;
    border-bottom: 1px solid rgba(255, 255, 255, .1); }
  .side-user .who { flex: 1 0 100%; }
  .side-user a { min-height: 40px !important; padding: 6px 10px !important; font-size: 13px; }
  .side-user form { margin-left: auto; }
  .side-user .who { color: #fff; font-weight: 600; padding: 0 10px 6px; }
  .side-user form { margin: 6px 10px 0; }
  .side-user .btn { width: 100%; justify-content: center; min-height: 44px; }

  /* ---------- top bar ---------- */
  .top { padding: 8px 12px; padding-top: calc(8px + env(safe-area-inset-top)); gap: 8px;
    padding-left: calc(12px + env(safe-area-inset-left)); padding-right: calc(12px + env(safe-area-inset-right)); }
  .top .menu-toggle { display: inline-flex; align-items: center; justify-content: center; min-width: 44px; min-height: 44px;
    padding: 0 12px; font-size: 15px; }
  .top form.search { display: block; flex: 1; min-width: 0; max-width: none; }
  .search-trigger { min-height: 44px; width: 100%; }
  .search-trigger span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .top .user { gap: 10px; }
  .top .bell { min-width: 44px; min-height: 44px; justify-content: center; }

  /* ---------- page ---------- */
  .content { padding: 16px 16px 60px; padding-left: calc(16px + env(safe-area-inset-left));
    padding-right: calc(16px + env(safe-area-inset-right)); padding-bottom: calc(60px + env(safe-area-inset-bottom)); }
  .page-head { flex-direction: column; align-items: stretch; gap: 10px; }
  .page-head .actions { justify-content: flex-start; }
  .g2, .g3, .g4, .g-2-1, .form-grid { grid-template-columns: minmax(0, 1fr); }
  .span2 { grid-column: auto; }
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .filters .field { min-width: 0; flex: 1 1 140px; }
  .card { padding: 16px; }
  .sums { max-width: none; }
}

@media (max-width: 640px) {
  body { font-size: 15px; }
  /* name, password and sign-out move into the menu; the bar keeps menu, search and bell */
  .top .user > :not(.bell) { display: none !important; }
  .search-trigger kbd { display: none; }

  .page-head h1 { font-size: 21px; }
  .actions { gap: 8px; }
  .actions > .btn, .actions > a.btn, .actions > form > .btn { min-height: 42px; }
  .page-head .actions > .btn, .page-head .actions > a.btn { flex: 1 1 auto; justify-content: center; }
  .btn { min-height: 40px; }
  .btn.sm { min-height: 34px; }
  .kpi .value { font-size: 21px; }
  .card { padding: 14px; border-radius: 14px; }

  /* forms: big enough to tap, 16px so iOS does not zoom in */
  .input, select.input, textarea.input, .form-grid input:not([type=checkbox]):not([type=radio]), .form-grid select,
  .filters input, .filters select { font-size: 16px; min-height: 44px; }
  textarea.input { min-height: 88px; height: 120px; resize: vertical; }
  input.check, input[type=checkbox], input[type=radio] { width: 20px; height: 20px; }
  .filters { flex-direction: column; align-items: stretch; gap: 10px; }
  .filters .field, .filters > .input, .filters > input, .filters > select { flex: none; width: 100%; max-width: none !important; }
  .filters > div:last-child { display: flex; gap: 8px; }
  .filters > div:last-child .btn { flex: 1; justify-content: center; }
  /* keep Save in reach at the bottom of long forms */
  form.card > .actions:last-child, form > .actions.sticky {
    position: sticky; bottom: 0; z-index: 4; margin: 16px -14px -14px !important; padding: 12px 14px;
    padding-bottom: calc(12px + env(safe-area-inset-bottom));
    background: rgba(255, 255, 255, .94); backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
    border-top: 1px solid rgba(15, 23, 42, .08); border-radius: 0 0 14px 14px; }
  form.card > .actions:last-child .btn { flex: 1; justify-content: center; }

  /* detail facts: label above value */
  dl.facts { grid-template-columns: minmax(0, 1fr); gap: 0; }
  dl.facts dt { border-bottom: 0 !important; padding-bottom: 0; font-size: 12px; }
  dl.facts dd { padding-top: 2px; }

  /* ---------- list tables become one card per row ---------- */
  table.t.stack, table.t.stack tbody, table.t.stack tr, table.t.stack td, table.t.stack tfoot { display: block; width: 100%; }
  table.t.stack thead, table.t.stack tr:has(> th) { display: none; }
  table.t.stack tr { padding: 10px 14px; border-bottom: 1px solid rgba(15, 23, 42, .08); }
  table.t.stack tr:last-child { border-bottom: 0; }
  table.t.stack td { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; padding: 3px 0;
    border: 0 !important; text-align: right; white-space: normal; background: transparent !important; }
  table.t.stack td::before { content: attr(data-label); flex: none; max-width: 45%; text-align: left; color: var(--muted);
    font-size: 12px; font-weight: 600; }
  table.t.stack td:not([data-label])::before, table.t.stack td[data-label=""]::before { content: none; }
  table.t.stack td:first-child { display: block; text-align: left; font-size: 15px; font-weight: 600; padding-bottom: 4px; }
  table.t.stack td:first-child::before { content: none; }
  table.t.stack td.ph-hide { display: none; }
  /* statement on a phone: one card per line, empty debit/credit cells left out, balance in bold */
  table.t.stack.stmt td:empty { display: none; }
  table.t.stack.stmt td:last-child { font-weight: 700; }
  table.t.stack.stmt tr.ob td:nth-child(3) { display: block; text-align: left; font-weight: 700; padding-bottom: 4px; }
  table.t.stack.stmt tr.ob td:nth-child(3)::before { content: none; }  /* a phone card keeps only the key columns */
  table.t.stack td.empty { display: block; text-align: center; }
  table.t.stack td.empty::before { content: none; }
  table.t.stack tr.clickable:hover td:first-child { box-shadow: none; }
  table.t.stack tfoot tr { background: color-mix(in srgb, var(--brand) 5%, #fff); }
  .card:has(> .table-wrap > table.t.stack), .card:has(> table.t.stack) { background: var(--glass, #fff); }
  .table-wrap:has(> table.t.stack) { background: none; overflow: visible; }

  /* payments: tabs scroll sideways instead of wrapping into a wall */
  .tabs { flex-wrap: nowrap; overflow-x: auto; -webkit-overflow-scrolling: touch; scrollbar-width: none; }
  .tabs a { white-space: nowrap; min-height: 44px; display: inline-flex; align-items: center; }

  .pager { flex-wrap: wrap; }
  .pager .btn { min-height: 44px; }
  .msg { padding-right: 12px; }
}

/* list filters fold away on phones (crud/list.html) */
.filters-box > summary { display: none; }
@media (max-width: 640px) {
  .filters-box { margin-bottom: 12px; }
  .filters-box > summary { display: flex; align-items: center; justify-content: space-between; min-height: 44px;
    padding: 0 14px; border-radius: 12px; background: rgba(255, 255, 255, .7); border: 1px solid rgba(15, 23, 42, .08);
    font-weight: 600; list-style: none; }
  .filters-box > summary::-webkit-details-marker { display: none; }
  .filters-box > summary::after { content: "▾"; color: var(--muted); }
  .filters-box[open] > summary { margin-bottom: 8px; }
  .filters-box[open] > summary::after { content: "▴"; }
  .filters-box > summary .on { color: var(--brand); font-size: 12px; margin-left: auto; margin-right: 8px; }
}

/* tenant / landlord portal */
@media (max-width: 640px) {
  .portal-top { padding: 10px 14px; padding-top: calc(10px + env(safe-area-inset-top)); gap: 10px; }
  .portal-top .brand b { font-size: 14px; }
  .portal-top .brand span { display: none; }
  .portal-top .actions { flex-wrap: nowrap; }
}

@media print {
  .side-user, .nav-backdrop, .side-close { display: none !important; }
}
