/* ปรับหน้าจอให้ใช้งานบนมือถือ/แท็บเล็ตได้จริง
   โหลดท้ายสุดต่อจาก components.css เพื่อทับค่าเดิม (index.html / login.html)
   layout.css มี breakpoint 900px คุมแถบข้าง+เมนูแฮมเบอร์เกอร์อยู่แล้ว ไฟล์นี้ทำส่วนที่เหลือ

   สองระดับ:
     <= 900px  แท็บเล็ต/มือถือ — ขนาดนิ้วสัมผัส กันซูมของ iOS
     <= 600px  มือถือโดยเฉพาะ — ยุบทุกอย่างเป็นคอลัมน์เดียว modal เต็มจอ */

@media (max-width: 900px) {
  /* *** ช่องกรอกต้อง >= 16px ***
     Safari บน iPhone จะซูมทั้งหน้าจอเองทุกครั้งที่โฟกัสช่องที่ตัวอักษรเล็กกว่า 16px
     แล้วไม่ซูมกลับ ผู้ใช้ต้องถ่างนิ้วย่อเองทุกครั้ง — เป็นสาเหตุอันดับหนึ่งที่ฟอร์มบนมือถือใช้ยาก */
  input,
  select,
  textarea {
    padding: 10px 12px;
    font-size: 16px;
  }

  /* เป้าสัมผัสขั้นต่ำ 44px ตามแนวทางของ iOS/Android — ปุ่มเดิมสูง ~34px กดพลาดบ่อย */
  .btn {
    min-height: 44px;
    padding: 10px 16px;
  }

  .btn-sm {
    min-height: 38px;
    padding: 8px 12px;
  }

  .nav a {
    min-height: 44px;
    padding: 11px 18px;
  }

  .menu-toggle {
    width: 44px;
    height: 44px;
  }

  .modal-close {
    min-width: 44px;
    min-height: 44px;
  }

  /* แถบบน: หัวข้อกับปุ่มคนละบรรทัด ไม่งั้นปุ่มเบียดจนอ่านหัวข้อไม่ออก
     เว้นซ้าย 56px ให้ปุ่มแฮมเบอร์เกอร์ที่ลอยทับอยู่ */
  .topbar {
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
    padding: 12px 14px;
  }

  .topbar h1 {
    padding-left: 46px;
    font-size: 20px;
    line-height: 44px;
  }

  .page-actions .btn {
    flex: 1 1 auto;
  }

  /* ตารางยังเลื่อนแนวนอน แต่ต้องไม่ให้ปุ่มในแถวถูกบีบจนตกบรรทัด */
  table.data td.actions {
    white-space: nowrap;
  }

  /* หน้าพิมพ์ (ใบเสร็จ/ใบปะหน้า) เปิดดูบนมือถือ — .page กว้างเป็นมิลลิเมตรจึงล้นจอ
     ย่อให้พอดีความกว้างจอเฉพาะตอนดูบนจอ ไม่กระทบตอนสั่งพิมพ์จริง */
  .toolbar {
    flex-wrap: wrap;
  }

  /* แท็บ (หน้า Report / Import / Settings) — เลื่อนแนวนอนแทนการตัดคำ "ยอด/ขาย" เป็น 2 บรรทัด */
  .tabs {
    overflow-x: auto;
    scrollbar-width: none;
  }

  .tab {
    flex: 0 0 auto;
    min-height: 44px;
    white-space: nowrap;
  }

  /* checkbox เลือกแถวขนาดเดิม ~13px นิ้วแตะไม่โดน */
  input[type='checkbox'],
  input[type='radio'] {
    width: 22px;
    height: 22px;
  }

  /* grid-2 ขั้นต่ำ 320px — จอ 360px หัก padding แล้วไม่พอ ล้นขอบ */
  .grid-2 {
    grid-template-columns: repeat(auto-fit, minmax(min(320px, 100%), 1fr));
  }
}

@media (max-width: 600px) {
  /* ตัวกรองเรียงเป็นคอลัมน์เดียว — 2 ช่องต่อแถวบนจอ 375px แคบจนอ่านป้ายไม่ออก */
  .filters {
    flex-direction: column;
    align-items: stretch;
    padding: 12px;
  }

  .filters .field,
  .filters .btn {
    width: 100%;
  }

  .field-row {
    flex-direction: column;
    gap: 0;
  }

  .field-row > .field {
    min-width: 0;
  }

  /* modal เต็มจอ — กล่องลอยกลางจอบนมือถือเหลือพื้นที่ใช้งานน้อยและเลื่อนยาก */
  /* grid-template-columns ต้องเป็น minmax(0, 1fr) — ค่าเริ่มต้น (auto) ยืดคอลัมน์ตามเนื้อหาที่กว้างสุด
     เคยเป็นบั๊ก: modal บิลซื้อกว้าง 713px บนจอ 375px ปุ่มปิด × กับช่องกรอกตกขอบขวา */
  .modal-backdrop {
    grid-template-columns: minmax(0, 1fr);
    padding: 0;
    place-items: stretch;
  }

  /* *** ความสูงต้องเป็นหน่วยจอ (dvh) ไม่ใช่ 100% ***
     100% อิงแถวของ grid ใน .modal-backdrop ซึ่งสูงตามเนื้อหา → modal ยืดยาวเกินจอ
     แล้ว backdrop (position: fixed) เลื่อนไม่ได้ ฟอร์มยาวๆ เช่นใบเสนอราคากดปุ่มบันทึกไม่ถึง
     vh ไว้เป็น fallback ให้เบราว์เซอร์เก่าที่ไม่รู้จัก dvh */
  .modal,
  .modal.modal-lg {
    width: 100%;
    max-width: 100%;
    height: 100vh;
    height: 100dvh;
    max-height: 100vh;
    max-height: 100dvh;
    border-radius: 0;
  }

  .modal-body {
    flex: 1 1 auto;
    min-height: 0;
    padding: 14px;
  }

  /* ปุ่มท้าย modal เต็มความกว้างและเรียงจากล่างขึ้น — ปุ่มยืนยันอยู่ล่างสุดใกล้นิ้วโป้ง */
  .modal-foot {
    flex-direction: column-reverse;
    padding: 12px 14px;
  }

  .modal-foot .btn {
    width: 100%;
  }

  /* แถบล่างตาราง: สรุปจำนวนกับปุ่มเปลี่ยนหน้าคนละบรรทัด */
  .table-foot {
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
  }

  .table-foot .row-flex {
    justify-content: space-between;
  }

  .view {
    padding: 12px 10px 72px;
  }

  /* แถบบนไม่ติดหัวจอบนมือถือ — หัวข้อ + ปุ่ม 2-3 แถวสูง ~200px กินจอ 1 ใน 4 ตลอดเวลาที่เลื่อน
     ปุ่มเมนู ☰ เป็น position: fixed แยกอยู่แล้ว ยังกดได้ทุกตำแหน่ง */
  .topbar {
    position: static;
  }

  /* ตารางกรอกสินค้าในฟอร์มออเดอร์ → การ์ดทีละรายการ
     แบบตารางบนจอ 375px ช่องชื่อสินค้าถูกบีบจนกดไม่ได้ ต้องเลื่อนแนวนอนหาช่องราคา/จำนวน
     การ์ด: SKU + ปุ่มลบ / ชื่อสินค้า / ราคา-จำนวน-ส่วนลด 3 ช่องเรียงกัน */
  .table-wrap:has(> table.data.items) {
    overflow: visible;
    background: none;
    border: 0;
  }

  table.data.items,
  table.data.items tbody {
    display: block;
  }

  table.data.items thead {
    display: none;
  }

  table.data.items tr {
    display: grid;
    grid-template-columns: 1fr 0.7fr 1fr auto;
    grid-template-areas:
      'sku sku sku del'
      'name name name name'
      'price qty disc disc';
    gap: 8px;
    margin-bottom: 10px;
    padding: 10px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
  }

  table.data.items tbody tr:hover {
    background: var(--surface);
  }

  table.data.items td {
    display: block;
    width: auto !important;
    min-width: 0;
    padding: 0;
    border: 0;
  }

  table.data.items td[data-label]::before {
    content: attr(data-label);
    display: block;
    margin-bottom: 3px;
    color: var(--text-muted);
    font-size: 12px;
    font-weight: 600;
  }

  table.data.items td input {
    width: 100%;
    min-width: 0;
  }

  table.data.items td.c-sku { grid-area: sku; }
  table.data.items td.c-name { grid-area: name; }
  table.data.items td.c-price { grid-area: price; }
  table.data.items td.c-qty { grid-area: qty; }
  table.data.items td.c-disc { grid-area: disc; }

  table.data.items td.c-del {
    grid-area: del;
    align-self: end;
  }

  /* ตารางที่มีคอลัมน์ "รวม" ต่อบรรทัด (ใบเสนอราคา) → แถวสุดท้ายของการ์ด ชิดขวา */
  table.data.items.has-total tr {
    grid-template-areas:
      'sku sku sku del'
      'name name name name'
      'price qty disc disc'
      'total total total total';
  }

  table.data.items td.c-total {
    grid-area: total;
    padding: 6px 0 0;
    border-top: 1px dashed var(--border);
    font-weight: 600;
  }

  table.data.items td.c-total::before {
    content: 'รวม ';
    color: var(--text-muted);
    font-weight: 400;
  }

  /* บิลซื้อ: สินค้า + ปุ่มลบ / จำนวน-ราคาทุน / รวม / ต้นทุนเฉลี่ยใหม่ */
  table.data.items.has-total.items-purchase tr {
    grid-template-columns: 0.8fr 1.2fr auto;
    grid-template-areas:
      'prod prod del'
      'qty cost cost'
      'total total total'
      'avg avg avg';
  }

  table.data.items td.c-prod { grid-area: prod; }
  table.data.items td.c-cost { grid-area: cost; }
  table.data.items-purchase td.c-avg { grid-area: avg; padding: 0; }

  table.data.items td.c-avg::before {
    content: 'ต้นทุนเฉลี่ยใหม่';
    display: block;
    color: var(--text-muted);
    font-size: 12px;
    font-weight: 600;
  }

  /* แก้ไขใบเสร็จ: รายการ + ปุ่มลบ / ราคา-จำนวน-ส่วนลด / รวม */
  table.data.items.has-total.items-receipt tr {
    grid-template-areas:
      'name name name del'
      'price qty disc disc'
      'total total total total';
  }

  /* modal ที่มีตารางรายการเป็นการ์ด — การ์ดซ้อนการ์ดเปลืองที่ ตัด padding ของการ์ดชั้นนอก */
  .modal-body .card:has(table.data.items) {
    padding: 12px 10px;
  }

  table.data.items td.actions .btn {
    min-width: 44px;
    min-height: 44px;
    margin: 0;
  }

  .items-total {
    text-align: left;
    line-height: 1.8;
  }

  /* ---------- ตารางรายการ (DataTable ทุกหน้า) → การ์ดทีละแถว ----------
     แบบตารางบนจอ 375px คอลัมน์วันที่ตัดเป็น 3 บรรทัด ยอดเงินกับปุ่มแก้ไข/ลบอยู่นอกจอ ต้องเลื่อนแนวนอนหา
     การ์ด: ป้าย (จาก data-label ที่ table.js ใส่ให้) ซ้าย / ค่า ขวา, ปุ่มรวมไว้ท้ายการ์ด
     หัวตารางไม่ทิ้ง — เหลือเฉพาะช่องเลือกทั้งหมดกับคอลัมน์ที่ sort ได้ แสดงเป็นแถบปุ่มเล็กเหนือการ์ด */
  .table-wrap:has(> table.data.dt) {
    overflow: visible;
    background: none;
    border: 0;
  }

  table.data.dt,
  table.data.dt tbody {
    display: block;
  }

  table.data.dt thead {
    display: block;
    margin-bottom: 8px;
  }

  table.data.dt thead tr {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
  }

  table.data.dt thead th {
    display: none;
  }

  table.data.dt thead th:has(.dt-all),
  table.data.dt thead th.sortable {
    display: flex;
    align-items: center;
    width: auto !important;
    min-height: 36px;
    padding: 6px 12px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 999px;
    font-size: 13px;
  }

  table.data.dt tbody tr {
    position: relative;
    display: block;
    margin-bottom: 10px;
    padding: 8px 12px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
  }

  table.data.dt tbody tr:hover { background: var(--surface); }
  table.data.dt tbody tr.row-danger { background: var(--danger-soft); }
  table.data.dt tbody tr.row-warning { background: var(--warning-soft); }

  table.data.dt tbody tr:has(> td.dt-check) {
    padding-left: 48px;
  }

  table.data.dt tbody td {
    display: flex;
    gap: 12px;
    align-items: baseline;
    justify-content: space-between;
    min-width: 0;
    padding: 5px 0;
    border: 0;
    text-align: right;
    white-space: normal;
    overflow-wrap: anywhere;
  }

  table.data.dt tbody td[data-label]::before {
    content: attr(data-label);
    flex: 0 0 auto;
    max-width: 45%;
    color: var(--text-muted);
    font-size: 12.5px;
    text-align: left;
  }

  /* แถว "ไม่พบข้อมูล" / กำลังโหลด (td colspan ไม่มี data-label) ให้อยู่กลางการ์ด */
  table.data.dt tbody td[colspan] {
    display: block;
    text-align: center;
  }

  table.data.dt tbody td.dt-check {
    position: absolute;
    top: 8px;
    left: 0;
    width: 48px;
    justify-content: center;
    padding: 6px 0;
  }

  table.data.dt tbody td.actions {
    flex-wrap: wrap;
    justify-content: flex-end;
    gap: 6px;
    margin-top: 6px;
    padding-top: 10px;
    border-top: 1px solid var(--border);
  }

  table.data.dt tbody td.actions:empty {
    display: none;
  }

  table.data.dt td.actions .btn {
    margin: 0;
  }

  /* ---------- Stock Adjust → การ์ดทีละสินค้า ----------
     ช่อง "ยอดใหม่" เป็นช่องหลักของหน้า แต่แบบตารางตกขอบขวาจนกดไม่ถึง */
  #table:has(> table.data.stock-adjust) {
    overflow: visible;
    background: none;
    border: 0;
  }

  table.data.stock-adjust,
  table.data.stock-adjust tbody {
    display: block;
  }

  table.data.stock-adjust thead {
    display: none;
  }

  table.data.stock-adjust tr {
    display: grid;
    grid-template-columns: 1fr 1fr auto;
    grid-template-areas:
      'sku sku del'
      'name name name'
      'stock diff diff'
      'qty qty qty';
    gap: 4px 10px;
    margin-bottom: 8px;
    padding: 10px 12px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
  }

  table.data.stock-adjust tbody tr:hover { background: var(--surface); }
  table.data.stock-adjust tr.row-edited,
  table.data.stock-adjust tbody tr.row-edited:hover { background: var(--info-soft); }
  table.data.stock-adjust tr.row-warning { background: var(--warning-soft); }

  table.data.stock-adjust td {
    display: block;
    width: auto !important;
    min-width: 0;
    padding: 0;
    border: 0;
    text-align: left;
  }

  table.data.stock-adjust td.c-sku {
    grid-area: sku;
    color: var(--text-muted);
    font-size: 12.5px;
  }

  table.data.stock-adjust td.c-name { grid-area: name; font-weight: 600; }
  table.data.stock-adjust td.c-stock { grid-area: stock; }
  table.data.stock-adjust td.c-diff { grid-area: diff; text-align: right; }
  table.data.stock-adjust td.c-qty { grid-area: qty; padding-top: 4px; }
  table.data.stock-adjust td.c-del { grid-area: del; }

  table.data.stock-adjust td[data-label]::before {
    content: attr(data-label) ' ';
    color: var(--text-muted);
    font-size: 12.5px;
  }

  table.data.stock-adjust .qty-stepper input {
    flex: 1;
    width: auto;
    min-width: 0;
  }

  table.data.stock-adjust .qty-stepper .btn {
    min-width: 48px;
    min-height: 44px;
    font-size: 18px;
  }

  /* แถบบันทึกท้ายหน้า Stock Adjust: ข้อความสรุปเต็มบรรทัด ปุ่ม 2 ปุ่มแบ่งครึ่งบรรทัดล่าง
     (เดิมข้อความถูกบีบเหลือคอลัมน์แคบ ตัดเป็น 5 บรรทัด) */
  .adjust-bar {
    gap: 8px;
    padding: 10px;
  }

  .adjust-bar #bar-summary {
    flex: 1 0 100%;
    font-size: 13px;
  }

  .adjust-bar .btn {
    flex: 1 1 0;
  }

  /* ปุ่มบนแถบเครื่องมือแถวเดียวกันหมด ไม่ต้องยืดเต็มความกว้าง */
  .page-actions {
    gap: 6px;
  }
}
