* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: Tahoma, 'TH Sarabun New', 'Segoe UI', sans-serif;
    background: #eef1f5;
    color: #1f2933;
    font-size: 15px;
    -webkit-text-size-adjust: 100%;
}

a { color: #2563eb; }

/* กันหน้าจอล้นแนวนอน */
html, body { overflow-x: hidden; max-width: 100%; }
img { max-width: 100%; }

.topbar {
    background: #0f3d2e;
    color: #fff;
    padding: 12px 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 10px;
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; flex: 1; }
.brand > div { min-width: 0; }
.topbar strong, .topbar small { overflow-wrap: anywhere; }
.logo-img {
    height: 52px;
    width: auto;
    background: #fff;
    border-radius: 8px;
    padding: 5px 8px;
}
.logo-fallback {
    background: #0d9488;
    color: #fff;
    font-weight: bold;
    font-size: 20px;
    padding: 8px 12px;
    border-radius: 8px;
    letter-spacing: 1px;
}
.topbar strong { display: block; font-size: 17px; }
.topbar small { color: #b9d4c8; }
.topnav { display: flex; gap: 8px; }

.container { max-width: 1100px; margin: 18px auto; padding: 0 16px; }

.card {
    background: #fff;
    border-radius: 12px;
    padding: 20px;
    box-shadow: 0 1px 4px rgba(0,0,0,.08);
    margin-bottom: 18px;
}
.card h2 { margin: 0 0 14px; font-size: 18px; }
.card-head { display: flex; justify-content: space-between; align-items: baseline; }

.muted { color: #6b7280; }
.alert {
    background: #fef2f2; color: #b91c1c;
    border: 1px solid #fecaca; border-radius: 8px;
    padding: 8px 12px; margin: 0 0 12px;
}
.hidden { display: none !important; }
.r { text-align: right; }
.nowrap { white-space: nowrap; }

/* ---- ฟอร์มสร้างใบงาน ---- */
.grid-form {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 12px;
}
.grid-form label {
    display: flex; flex-direction: column;
    font-size: 13px; color: #374151; gap: 4px;
}
.grid-form .col-2 { grid-column: span 2; }
.grid-form input {
    font: inherit; padding: 8px 10px;
    border: 1px solid #cbd5e1; border-radius: 8px;
}
.actions { display: flex; align-items: flex-end; }

/* ---- ปุ่ม ---- */
.btn {
    font: inherit; cursor: pointer;
    background: #e2e8f0; color: #1f2933;
    border: 0; border-radius: 8px;
    padding: 9px 16px; text-decoration: none;
    display: inline-block;
}
.btn:hover { filter: brightness(.96); }
.btn-primary { background: #2563eb; color: #fff; }
.btn-danger  { background: #ef4444; color: #fff; }
.btn-sm { padding: 6px 10px; font-size: 13px; }

/* ---- ตารางรายการล่าสุด ---- */
.list-table, .weigh-table { width: 100%; border-collapse: collapse; }
.list-table th, .list-table td,
.weigh-table th, .weigh-table td {
    padding: 8px 10px; border-bottom: 1px solid #e5e7eb;
    text-align: left;
}
.list-table th { background: #f3f4f6; font-size: 13px; }
.row-actions { display: flex; gap: 6px; justify-content: flex-end; flex-wrap: wrap; }
.inline-form { display: inline; margin: 0; }

/* ---- หน้าชั่ง ---- */
.weigh-layout { display: grid; grid-template-columns: 380px 1fr; gap: 18px; align-items: start; }
.weigh-layout .card { margin-bottom: 0; }
/* grid item ต้องหดตัวได้ ไม่งั้นตาราง min-width จะดันจอกว้างเกิน */
.weigh-layout > .card { min-width: 0; }

.next-qr {
    background: #f0fdf4; border: 1px solid #bbf7d0;
    border-radius: 8px; padding: 8px 12px; font-size: 13px; margin-bottom: 14px;
}

.mic-btn {
    width: 100%; font: inherit; cursor: pointer;
    background: #16a34a; color: #fff;
    border: 0; border-radius: 12px;
    padding: 22px; font-size: 18px; font-weight: bold;
    display: flex; flex-direction: column; align-items: center; gap: 6px;
}
.mic-btn .mic-icon { font-size: 34px; }
.mic-btn.listening { background: #dc2626; animation: pulse 1.2s infinite; }
@keyframes pulse { 50% { filter: brightness(1.18); } }

.heard {
    margin: 10px 0; min-height: 22px; font-size: 14px;
    text-align: center; font-style: italic;
}

.pending {
    background: #eff6ff; border: 2px solid #2563eb;
    border-radius: 12px; padding: 14px; margin: 10px 0; text-align: center;
}
.pending-label { font-size: 13px; color: #6b7280; }
.pending-num { font-size: 42px; font-weight: bold; color: #1d4ed8; line-height: 1.1; }
.pending-num small { font-size: 16px; color: #6b7280; }
.pending-hint { font-size: 14px; font-weight: bold; color: #047857; margin: 2px 0 6px; }
.pending-hint.warn { color: #b91c1c; }
.pending-bar { height: 8px; background: #dbeafe; border-radius: 4px; margin: 10px 0; overflow: hidden; }
.pending-bar div { height: 100%; background: #2563eb; width: 100%; transition: width .1s linear; }
.pending-actions { display: flex; gap: 8px; justify-content: center; }

.manual { margin-top: 14px; border-top: 1px solid #e5e7eb; padding-top: 12px; }
.toggle { display: flex; align-items: center; gap: 6px; font-size: 14px; margin-bottom: 10px; }
.manual-add { display: flex; gap: 8px; }
.manual-add input {
    flex: 1; font: inherit; padding: 9px 10px;
    border: 1px solid #cbd5e1; border-radius: 8px;
}

.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.weigh-table { min-width: 460px; }
.list-table { min-width: 520px; }
.weigh-table th { background: #f3f4f6; font-size: 13px; }
.weigh-table td.r, .weigh-table th.r { text-align: right; }
.weigh-table .cell-input {
    width: 78px; font: inherit; text-align: right;
    padding: 4px 6px; border: 1px solid #cbd5e1; border-radius: 6px;
}
.totals-row td { font-weight: bold; background: #f9fafb; border-top: 2px solid #cbd5e1; }
.row-new { animation: flash 1s ease; }
@keyframes flash { from { background: #fef9c3; } to { background: transparent; } }

.summary-box {
    margin-top: 16px; display: grid; gap: 6px;
}
.summary-box div {
    display: flex; justify-content: space-between;
    padding: 8px 12px; background: #f3f4f6; border-radius: 8px;
}
.summary-box .grand {
    background: #0f3d2e; color: #fff; font-size: 18px;
}
.summary-box strong { font-size: 17px; }

/* ============================================================
   Responsive — Desktop / Laptop / iPad / Tablet / Mobile
   • ≥1101px  Desktop      : 2 คอลัมน์เต็ม, ฟอร์ม 4 ช่อง/แถว
   • ≤1100px  Laptop/iPad-L: คอลัมน์ซ้ายแคบลง
   • ≤900px   iPad-P/Tablet: เรียงคอลัมน์เดียว, ฟอร์ม 2 ช่อง/แถว
   • ≤600px   Mobile       : คอลัมน์เดียวทั้งหมด, ฟอร์ม 1 ช่อง/แถว
   ============================================================ */

/* แล็ปท็อป / iPad แนวนอน */
@media (max-width: 1100px) {
    .weigh-layout { grid-template-columns: 320px 1fr; gap: 14px; }
}

/* iPad แนวตั้ง / แท็บเล็ต */
@media (max-width: 900px) {
    .weigh-layout { grid-template-columns: 1fr; }
    .grid-form { grid-template-columns: repeat(2, 1fr); }
    .grid-form .col-2 { grid-column: span 2; }
    .container { padding: 0 14px; }
    .topbar strong { font-size: 16px; }
}

/* มือถือ */
@media (max-width: 600px) {
    body { font-size: 14px; }
    .container { padding: 0 10px; margin: 12px auto; }
    .card { padding: 14px; border-radius: 10px; margin-bottom: 14px; }
    .card h2 { font-size: 16px; }

    .topbar { padding: 10px 12px; gap: 8px; }
    .topbar strong { font-size: 15px; }
    .topbar small { font-size: 11.5px; }
    .logo-img { height: 38px; }
    .topnav { width: 100%; }
    .topnav .btn { flex: 1; text-align: center; }

    /* ฟอร์ม: 1 ช่อง/แถว, ฟอนต์ 16px กัน iOS ซูมจอ */
    .grid-form { grid-template-columns: 1fr; gap: 10px; }
    .grid-form .col-2 { grid-column: span 1; }
    .grid-form input { padding: 10px; font-size: 16px; }
    .actions .btn { width: 100%; }

    /* แผงชั่งด้วยเสียง */
    .mic-btn { padding: 18px; font-size: 17px; }
    .mic-btn .mic-icon { font-size: 30px; }
    .pending-num { font-size: 36px; }
    .manual-add { flex-direction: column; }
    .manual-add input { font-size: 16px; padding: 11px; }
    .btn { padding: 11px 16px; }
    .pending-actions { flex-wrap: wrap; }
    .pending-actions .btn { flex: 1; }

    /* ตารางย่อให้พอดีจอมือถือ ไม่ต้องเลื่อนแนวนอน */
    .weigh-table, .list-table { min-width: 0; }
    .weigh-table { font-size: 12px; }
    .weigh-table th { font-size: 10.5px; }
    .weigh-table th, .weigh-table td { padding: 5px 3px; }
    .list-table th, .list-table td { padding: 7px 5px; font-size: 13px; }
    .cell-input { width: 52px; font-size: 13px; padding: 4px 3px; }
    .weigh-table .btn-sm, .list-table .btn-sm { padding: 6px 7px; font-size: 12px; }
    .summary-box .grand { font-size: 16px; }
    .summary-box strong { font-size: 16px; }
}

/* มือถือจอแคบมาก */
@media (max-width: 380px) {
    .cell-input { width: 46px; }
    .weigh-table th, .weigh-table td { padding: 5px 2px; }
    .topbar small { display: none; }
    .logo-img { height: 34px; }
}
