/**
 * แสดงตารางเป็นการ์ดสมัยใหม่บน tablet/มือถือ (< 992px)
 * ใช้คู่กับ .table-mobile-stack ห่อ table + public/js/table-mobile-stack.js
 */
@media (max-width: 991.98px) {
  .table-mobile-stack table thead {
    display: none !important;
  }

  .table-mobile-stack table tbody tr {
    display: block;
    margin-bottom: 0.65rem;
    padding: 0.65rem 0.8rem 0.55rem;
    background: var(--bs-body-bg, #fff);
    border: 1px solid rgba(15, 23, 42, 0.08);
    border-radius: 0.75rem;
    box-shadow: 0 1px 10px rgba(15, 23, 42, 0.05);
    border-inline-start: 3px solid #7c3aed;
  }

  .table-mobile-stack table tbody tr:nth-child(even) {
    background: rgba(124, 58, 237, 0.03);
  }

  .table-mobile-stack table tbody td {
    display: block;
    width: 100% !important;
    max-width: none !important;
    text-align: start !important;
    border: none !important;
    padding: 0.35rem 0 !important;
    border-bottom: 1px solid rgba(15, 23, 42, 0.06) !important;
  }

  .table-mobile-stack table tbody td:last-child {
    border-bottom: none !important;
    padding-bottom: 0 !important;
  }

  .table-mobile-stack table tbody td::before {
    display: block;
    content: attr(data-label);
    font-size: 0.62rem;
    font-weight: 600;
    letter-spacing: 0.03em;
    text-transform: uppercase;
    color: #64748b;
    margin-bottom: 0.15rem;
  }

  .table-mobile-stack table tbody td:not([data-label])::before {
    display: none;
  }

  .table-mobile-stack .table-mobile-stack__row-ico {
    display: inline-flex !important;
    align-items: center;
    justify-content: center;
    width: 2rem;
    height: 2rem;
    border-radius: 0.5rem;
    background: rgba(124, 58, 237, 0.12);
    color: #6d28d9;
    font-size: 0.95rem;
    margin-right: 0.45rem;
    vertical-align: middle;
    flex-shrink: 0;
  }

  .table-mobile-stack table tbody td:first-child {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    column-gap: 0.45rem;
    row-gap: 0.25rem;
    padding-top: 0 !important;
    border-bottom: 1px solid rgba(124, 58, 237, 0.12) !important;
    margin-bottom: 0.25rem;
    padding-bottom: 0.45rem !important;
  }

  .table-mobile-stack table tbody td:first-child::before {
    flex: 0 0 100%;
    margin-bottom: 0;
  }

  /*
   * รายการย่อยใบแจ้งหนี้ (2 คอลัมน์): แถวเดียว — ข้อความซ้าย / ยอดขวา
   * ต้องยกเลิก width:100% ของ td มิฉะนั้น flex จะบีบคอลัมน์ข้อความให้แคบผิดปกติ
   */
  .table-mobile-stack--compact table tbody tr:not(.table-mobile-stack__fullrow) {
    display: flex;
    flex-direction: row;
    align-items: flex-start;
    gap: 0.65rem;
    margin-bottom: 0.45rem;
    padding: 0.5rem 0.7rem;
    border-radius: 0.6rem;
    border-inline-start-width: 3px;
    box-shadow: 0 1px 6px rgba(15, 23, 42, 0.04);
    min-width: 0;
  }

  .table-mobile-stack--compact table tbody tr:not(.table-mobile-stack__fullrow) td {
    display: block;
    width: auto !important;
    max-width: none !important;
    border-bottom: none !important;
    padding: 0 !important;
    min-width: 0;
  }

  .table-mobile-stack--compact table tbody tr:not(.table-mobile-stack__fullrow) td::before {
    display: none !important;
  }

  .table-mobile-stack--compact table tbody tr:not(.table-mobile-stack__fullrow) td:first-child {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr);
    column-gap: 0.45rem;
    align-items: start;
    flex: 1 1 0;
    margin-bottom: 0;
    padding-bottom: 0 !important;
    border-bottom: none !important;
    font-size: 0.9rem;
    line-height: 1.4;
    overflow-wrap: break-word;
    word-break: normal;
  }

  .table-mobile-stack--compact .table-mobile-stack__row-ico {
    width: 1.65rem;
    height: 1.65rem;
    font-size: 0.82rem;
    margin-right: 0;
    border-radius: 0.45rem;
    grid-column: 1;
  }

  .table-mobile-stack--compact table tbody tr:not(.table-mobile-stack__fullrow) td:last-child {
    flex: 0 0 auto;
    width: auto !important;
    text-align: end !important;
    font-weight: 600;
    font-variant-numeric: tabular-nums;
    font-size: 0.9rem;
    line-height: 1.4;
    white-space: nowrap;
    padding-top: 0.1rem !important;
  }

  .table-mobile-stack table tbody tr.table-mobile-stack__fullrow td {
    display: block;
    text-align: center !important;
    border: none !important;
    padding: 1rem !important;
  }

  .table-mobile-stack table tbody tr.table-mobile-stack__fullrow td::before {
    display: none !important;
  }

  .table-mobile-stack table tfoot {
    display: block;
    width: 100%;
  }

  .table-mobile-stack table tfoot tr {
    display: block;
    margin-top: 0.35rem;
    padding: 0.5rem 0.75rem;
    background: rgba(124, 58, 237, 0.08);
    border: 1px solid rgba(124, 58, 237, 0.18);
    border-radius: 0.55rem;
  }

  .table-mobile-stack table tfoot td {
    display: block;
    width: 100% !important;
    border: none !important;
    padding: 0.25rem 0 !important;
    text-align: start !important;
  }

  .table-mobile-stack table tfoot td:empty {
    display: none !important;
  }

  [data-bs-theme="dark"] .table-mobile-stack table tbody tr {
    background: rgba(30, 41, 59, 0.65);
    border-color: rgba(255, 255, 255, 0.08);
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
  }

  [data-bs-theme="dark"] .table-mobile-stack table tbody tr:nth-child(even) {
    background: rgba(30, 41, 59, 0.85);
  }

  [data-bs-theme="dark"] .table-mobile-stack table tbody td {
    border-bottom-color: rgba(255, 255, 255, 0.06) !important;
  }

  [data-bs-theme="dark"] .table-mobile-stack .table-mobile-stack__row-ico {
    background: rgba(167, 139, 250, 0.2);
    color: #c4b5fd;
  }
}
