/* Universal mobile baseline. Loaded AFTER each template's inline <style>,
   so these rules win the cascade on narrow viewports without needing to
   refactor every Kicksa-era template. */

@media (max-width: 640px) {

  /* -------- Page shell -------- */

  body {
    padding: 12px !important;
    font-size: 15px;
    line-height: 1.5;
  }

  /* Kicksa's .container had fixed max-width + 30px padding + box-shadow.
     Collapse that on phones so content owns the full viewport. */
  .container {
    max-width: 100% !important;
    padding: 14px !important;
    border-radius: 0 !important;
    box-shadow: none !important;
    margin: 0 !important;
  }

  /* -------- Typography -------- */

  h1 { font-size: 22px !important; line-height: 1.3 !important; }
  h2 { font-size: 17px !important; }
  h3 { font-size: 15px !important; }

  /* -------- Generic flex rows: stack on mobile -------- */
  /* Covers Kicksa's common patterns: headers, action bars, filter bars,
     the invoice-form item rows, restock forms, payment action rows, etc. */

  .container > header,
  header.page-header,
  header .actions,
  .actions,
  .item-row,
  form.filters,
  .filters,
  .filter-bar,
  .filter-row,
  .search-bar,
  .restock-form,
  .action-buttons,
  .payment-actions,
  .btn-row,
  .top-bar,
  .info-cards,
  .stat-cards,
  .info-row,
  .summary-row,
  .row-flex {
    display: flex !important;
    flex-direction: column !important;
    align-items: stretch !important;
    justify-content: flex-start !important;
    gap: 10px !important;
    flex-wrap: wrap !important;
  }

  /* Children of those rows: full width, sensible min-width overrides */
  .item-row > *,
  form.filters > *,
  .filters > *,
  .filter-bar > *,
  .filter-row > *,
  .search-bar > *,
  .restock-form > *,
  .action-buttons > *,
  .payment-actions > *,
  .btn-row > *,
  .info-cards > *,
  .stat-cards > *,
  .info-row > *,
  header .actions > *,
  .actions > * {
    width: 100% !important;
    max-width: 100% !important;
    min-width: 0 !important;
    flex: 1 1 auto !important;
  }

  /* The inline .line-total readout in the invoice form needs a label-like look */
  .item-row .line-total {
    padding: 6px 0 !important;
    font-weight: bold;
    text-align: left !important;
  }

  /* Kicksa's dashboard header (business-name + user/logout) */
  header {
    flex-direction: column !important;
    align-items: flex-start !important;
    gap: 10px !important;
  }
  header .left img {
    height: 70px !important;
  }
  header .right {
    width: 100%;
    justify-content: space-between;
  }

  /* -------- Grids: always single column on mobile -------- */

  .nav-buttons,
  .form-row,
  .row-2,
  .row-3,
  .grid,
  .grid-2,
  .grid-3,
  .metrics,
  .sub-grid,
  .who,
  .features,
  .pricing,
  .tenant-card .grid {
    grid-template-columns: 1fr !important;
    gap: 10px !important;
  }

  /* Full-width grid-column-span overrides */
  .full-width { grid-column: auto !important; }

  /* -------- Forms -------- */

  /* Some Kicksa forms (invoice creation) use `display: grid` with 2 columns.
     Forcing block flow collapses labels + fields to single column naturally. */
  form {
    display: block !important;
    max-width: 100%;
  }

  /* ...unless the form is explicitly a flex row (like admin danger-zone).
     Those already have their own rules below. */

  /* 16px+ inputs suppress iOS Safari's auto-zoom on focus */
  input[type="text"], input[type="email"], input[type="password"],
  input[type="number"], input[type="tel"], input[type="date"],
  input[type="search"], input[type="url"], input[type="file"],
  select, textarea {
    font-size: 16px !important;
    width: 100% !important;
    max-width: 100% !important;
    box-sizing: border-box !important;
  }

  /* Checkbox/radio stay inline-sized */
  input[type="checkbox"], input[type="radio"] {
    width: auto !important;
  }

  /* Buttons become full-width on mobile so they're tap-friendly */
  button,
  button.submit,
  button.primary,
  a.btn,
  a.cta,
  a.action-btn,
  a.history-link,
  header .actions a,
  .actions a,
  input[type="submit"] {
    width: 100% !important;
    box-sizing: border-box !important;
    text-align: center !important;
    justify-content: center !important;
    padding: 11px 16px !important;
    font-size: 15px !important;
    display: block !important;
  }

  /* Tiny row-action buttons stay inline (table row controls) */
  .row-actions button,
  .row-actions a.btn,
  .remove-btn,
  .delete-btn,
  .pdf-btn,
  .logout-btn {
    width: auto !important;
    display: inline-block !important;
    padding: 6px 10px !important;
  }
  .row-actions {
    flex-direction: row !important;
    flex-wrap: wrap !important;
    gap: 6px !important;
  }

  /* -------- Tables -------- */

  /* Tables become horizontally scrollable blocks. Users swipe across data
     without the page itself overflowing. */
  table {
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    white-space: nowrap;
    font-size: 13px !important;
    max-width: 100%;
  }
  .table-wrap {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  /* -------- Admin nav bar -------- */

  .admin-top-nav .inner {
    flex-direction: column;
    align-items: flex-start !important;
    gap: 10px;
    padding: 0 14px !important;
  }
  .admin-top-nav .links {
    display: flex;
    flex-wrap: wrap;
    gap: 12px 14px;
    width: 100%;
  }
  .admin-top-nav .links a {
    margin-left: 0 !important;
  }
  .admin-top-nav .logout-btn {
    margin-left: 0 !important;
    width: auto !important;
    display: inline-block !important;
    background: #2d4e74 !important;
    color: white !important;
  }

  /* Tenant home-page logout button: the base style is f5f5f5 + #222 text,
     but some browsers render the inherited text color poorly on mobile. */
  header .right .logout-btn,
  header .logout-btn {
    color: #222 !important;
    font-weight: 500 !important;
  }

  /* -------- Landing / auth page tweaks -------- */

  /* Landing nav doesn't need the redundant stacking — keep logo + lang inline */
  .top {
    flex-direction: row !important;
    align-items: center !important;
  }
  .top .brand img {
    height: 90px !important;
  }

  /* Signup brand-logo gets smaller on phones */
  .brand-logo {
    height: 100px !important;
  }

  /* Language picker card */
  .card .logo {
    height: 110px !important;
  }

  /* -------- Misc -------- */

  /* Sticky elements can cause scroll jank on mobile */
  [style*="position: sticky"],
  header.nav {
    position: static !important;
  }

  /* Admin danger-zone form */
  .danger-zone form {
    flex-direction: column !important;
  }
  .danger-zone input[type="text"] {
    max-width: 100% !important;
  }

  /* Landing plan rows */
  .plan, .plan-row {
    padding: 12px 0 !important;
  }

  /* Invoice details summary section — narrow it up */
  .summary div {
    word-break: break-word;
  }

  /* -------- Utility: hide on mobile -------- */
  /* Apply to table columns (th + td) or anything low-priority on phones. */
  .mobile-hide {
    display: none !important;
  }

  /* Reset margins on header-action anchors that had margin-left/right */
  header .actions a,
  .actions a {
    margin: 0 !important;
  }

  /* -------- Card-layout tables (table.cards) --------
     Opt-in pattern: add class="cards" to a <table> and data-label="Name"
     to each <td>. On mobile the table reflows into vertical cards with
     the column label rendered as a pseudo-element. Desktop stays unchanged. */

  table.cards,
  table.cards thead,
  table.cards tbody,
  table.cards tr,
  table.cards td {
    display: block !important;
    white-space: normal !important;
    overflow: visible !important;
    max-width: 100% !important;
  }
  table.cards thead {
    display: none !important;
  }
  table.cards tr {
    background: white !important;
    border: 1px solid #e5e5e5 !important;
    border-radius: 6px !important;
    padding: 10px 12px !important;
    margin-bottom: 10px !important;
  }
  table.cards td {
    display: flex !important;
    justify-content: space-between !important;
    align-items: baseline !important;
    padding: 5px 0 !important;
    border: none !important;
    font-size: 14px !important;
    text-align: left !important;
    gap: 10px !important;
    word-break: break-word !important;
  }
  table.cards td::before {
    content: attr(data-label);
    font-weight: bold;
    color: #666;
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    flex-shrink: 0;
    min-width: 90px;
    text-align: left;
  }
  /* If a td has no data-label (actions cell, etc.), don't show the pseudo */
  table.cards td:not([data-label])::before,
  table.cards td[data-label=""]::before {
    content: none;
    min-width: 0;
  }
  /* Actions cell: buttons on the right, align to end */
  table.cards td.actions-cell,
  table.cards td.actions {
    justify-content: flex-end !important;
    gap: 10px !important;
  }
}
