:root {
  --green: #2f7a4f;
  --green-dark: #205a38;
  --green-light: #e8f4ec;
  --red: #c0392b;
  --red-light: #fdecea;
  --yellow: #b7860b;
  --yellow-light: #fff8e1;
  --gray: #6b7280;
  --border: #e2e5e9;
  --bg: #f6f7f9;
  --text: #1f2430;
  --radius: 10px;
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
/* ปิดการขยายตัวอักษรอัตโนมัติของ mobile Safari (font boosting) — ถ้าผู้ใช้ตั้งค่าตัวอักษรใหญ่ในระบบ iOS
   Safari จะขยายบางกล่องข้อความเกินจริงแบบคาดเดาไม่ได้ ทำให้ layout ที่คำนวณพอดีจอ (เช่น topbar) ล้นจอได้
   ทั้งที่ทดสอบบน Chrome/desktop แล้วไม่มีปัญหา */
html, body { margin: 0; padding: 0; background: var(--bg); color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Sarabun", Tahoma, sans-serif;
  overflow-x: hidden; /* กันไม่ให้ทั้งหน้าเลื่อนแนวนอนได้ (ตารางแต่ละอันมี scroll ของตัวเองใน .table-wrap อยู่แล้ว) */
  width: 100%; max-width: 100vw; }
#app { min-height: 100vh; display: flex; flex-direction: column; width: 100%; max-width: 100vw; overflow-x: hidden; }

/* ---------- top bar ---------- */
/* หมายเหตุ: เดิมเคยใช้ flex-shrink + min-width:0 ให้ชื่อระบบ/ชื่อผู้ใช้ "ย่อตัวเอง" อัตโนมัติ แต่พบว่าบน
   Safari มือถือจริงบางเครื่อง ตัวอักษรหายไปทั้งหมด (กล่อง flex หดจนเหลือ 0 พิกเซล) ทั้งที่ทดสอบบน Chrome
   แล้วดูปกติ — เปลี่ยนมาใช้ max-width ตายตัว (หน่วย vw) แทนการให้ flexbox คำนวณ shrink เอง เพื่อให้ขนาด
   กล่องแน่นอนคงที่ทุก browser ไม่พึ่งพา flex-shrink algorithm ที่พฤติกรรมต่างกันไปตาม engine */
.topbar { background: var(--green); color: #fff; padding: 10px 12px; display: flex;
  align-items: center; justify-content: space-between; gap: 8px; position: sticky; top: 0; z-index: 10;
  max-width: 100%; overflow: hidden; }
.topbar h1 { font-size: 15px; margin: 0; font-weight: 600; white-space: nowrap; flex: 0 0 auto;
  max-width: 50vw; overflow: hidden; text-overflow: ellipsis; }
.topbar .right { display: flex; align-items: center; gap: 6px; flex: 0 0 auto; justify-content: flex-end; }
.topbar .user { font-size: 12px; opacity: 0.9; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  flex: 0 0 auto; max-width: 26vw; }
.topbar button { background: rgba(255,255,255,0.15); border: none; color: #fff; padding: 6px 8px;
  border-radius: 8px; font-size: 12px; flex: 0 0 auto; white-space: nowrap; }
@media (max-width: 400px) {
  .topbar .user { display: none; } /* จอแคบ: ซ่อนชื่อผู้ใช้ เหลือแค่ชื่อระบบ+ปุ่มออกจากระบบ กันล้นจอ */
}
@media (max-width: 340px) {
  .topbar h1 { font-size: 13px; max-width: 42vw; }
  .topbar button { padding: 5px 7px; font-size: 11px; }
}

/* ---------- content ---------- */
.content { flex: 1; padding: 12px; padding-bottom: 80px; max-width: 900px; width: 100%; margin: 0 auto; }
.card { background: #fff; border-radius: var(--radius); padding: 14px; margin-bottom: 12px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.06); border: 1px solid var(--border); }
h2.section-title { font-size: 15px; color: var(--green-dark); margin: 4px 0 10px; }

/* ---------- bottom nav ---------- */
.bottomnav { position: fixed; bottom: 0; left: 0; right: 0; background: #fff; border-top: 1px solid var(--border);
  display: flex; justify-content: space-around; padding: 6px 0 max(6px, env(safe-area-inset-bottom)); z-index: 10; }
.bottomnav a { flex: 1; text-align: center; font-size: 11px; color: var(--gray); text-decoration: none; padding: 6px 2px; }
.bottomnav a.active { color: var(--green); font-weight: 600; }
.bottomnav .icon { font-size: 18px; display: block; }

/* ---------- forms ---------- */
label { display: block; font-size: 13px; color: var(--gray); margin: 10px 0 4px; }
input, select, textarea { width: 100%; padding: 10px; border: 1px solid var(--border); border-radius: 8px;
  font-size: 15px; background: #fff; color: var(--text); }
textarea { resize: vertical; min-height: 60px; }
button.primary { background: var(--green); color: #fff; border: none; padding: 12px; border-radius: 8px;
  font-size: 15px; font-weight: 600; width: 100%; margin-top: 14px; }
button.secondary { background: #fff; border: 1px solid var(--border); color: var(--text); padding: 10px 14px;
  border-radius: 8px; font-size: 14px; }
button.danger { background: var(--red); color: #fff; border: none; padding: 8px 12px; border-radius: 8px; font-size: 13px; }
a.secondary { background: #fff; border: 1px solid var(--border); color: var(--text); padding: 10px 14px;
  border-radius: 8px; font-size: 14px; text-decoration: none; display: inline-block; white-space: nowrap; }
.msg { padding: 10px; border-radius: 8px; margin-top: 10px; font-size: 14px; }
.msg.error { background: var(--red-light); color: var(--red); }
.msg.success { background: var(--green-light); color: var(--green-dark); }
.msg.warning { background: var(--yellow-light); color: var(--yellow); }

/* ---------- type grid (entry menu) ---------- */
.type-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.type-btn { background: #fff; border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 10px;
  text-align: center; text-decoration: none; color: var(--text); }
.type-btn .emoji { font-size: 26px; display: block; margin-bottom: 6px; }
.type-btn .lbl { font-size: 13px; font-weight: 600; }

/* ---------- kpi cards ---------- */
.kpi-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.kpi { background: #fff; border-radius: var(--radius); padding: 14px; border: 1px solid var(--border); }
.kpi .num { font-size: 24px; font-weight: 700; color: var(--green-dark); }
.kpi .lbl { font-size: 12px; color: var(--gray); margin-top: 2px; }
.kpi.warn .num { color: var(--yellow); }
.kpi.danger .num { color: var(--red); }
a.kpi { display: block; text-decoration: none; color: inherit; }
a.kpi:active { opacity: 0.7; }

/* ---------- tables ---------- */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { border-collapse: collapse; width: 100%; font-size: 13px; }
th, td { padding: 8px 10px; text-align: right; border-bottom: 1px solid var(--border); white-space: nowrap; }
th:first-child, td:first-child { text-align: left; }
thead th { background: #fafbfc; color: var(--gray); font-weight: 600; position: sticky; top: 0; z-index: 1; }
tr.level-danger td.stockcell { background: var(--red-light); color: var(--red); font-weight: 700; }
tr.level-warning td.stockcell { background: var(--yellow-light); color: var(--yellow); font-weight: 700; }
tr.level-ok td.stockcell { color: var(--green-dark); font-weight: 600; }

/* ล็อคคอลัมน์แรก (ชื่อสินค้า) ไว้ไม่ให้เลื่อนหายตอน scroll แนวนอนดูข้อมูลรายเดือน/รายลูกค้า
   (คล้าย freeze pane ใน Excel) — ใช้กับตารางรายงานที่มีหลายคอลัมน์ */
td.freeze, th.freeze {
  position: sticky; left: 0; background: #fff; z-index: 2;
  box-shadow: 2px 0 4px -2px rgba(0,0,0,0.15);
  white-space: normal; max-width: 140px;
}
thead th.freeze { background: #fafbfc; z-index: 3; }

/* รายงานสรุปรายเดือนแบบ "ดูทั้งปี": เส้นกริดบางคั่นระหว่างคอลัมน์ย่อยในเดือนเดียวกัน
   (ยกมา/รับ/จ่าย/คงเหลือ) และเส้นหนาคั่นระหว่างเดือนให้เห็นชัดว่าคอลัมน์ไหนอยู่เดือนไหน */
.year-table th, .year-table td { border-right: 1px solid var(--border); }
.year-table thead tr:first-child th:not(.freeze) { text-align: center; } /* ชื่อเดือน (ม.ค./ก.พ./...) ให้อยู่กึ่งกลางเหนือ 4 คอลัมน์ย่อยของตัวเอง ไม่ใช่ชิดขวาตามค่า default ของตาราง */
.year-table th.month-end, .year-table td.month-end { border-right: 2px solid var(--gray); }
.year-table th:last-child, .year-table td:last-child { border-right: none; }

.filters { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 10px; }
.filters > * { flex: 1 1 120px; min-width: 0; }

.toolbar { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; gap: 8px; flex-wrap: wrap; }

.badge { display: inline-block; padding: 2px 8px; border-radius: 20px; font-size: 11px; font-weight: 600; }
.badge.in { background: var(--green-light); color: var(--green-dark); }
.badge.out { background: var(--red-light); color: var(--red); }

.center { text-align: center; }
.muted { color: var(--gray); font-size: 13px; }
.login-wrap { display: flex; align-items: center; justify-content: center; min-height: 100vh; padding: 20px; }
.login-box { background: #fff; border-radius: 14px; padding: 28px 24px; width: 100%; max-width: 360px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.08); }
.login-box h1 { text-align: center; color: var(--green-dark); font-size: 18px; margin-bottom: 4px; }
.login-box .sub { text-align: center; color: var(--gray); font-size: 13px; margin-bottom: 18px; }

.checkbox-list { display: flex; flex-direction: column; gap: 8px; margin: 8px 0; }
.checkbox-list label { display: flex; align-items: center; gap: 8px; font-size: 14px; color: var(--text); margin: 0; }
.checkbox-list input { width: auto; }

.list-row { display: flex; justify-content: space-between; align-items: center; padding: 10px 0; border-bottom: 1px solid var(--border); gap: 8px; }
.list-row:last-child { border-bottom: none; }
.list-row .main { font-size: 14px; }
.list-row .sub { font-size: 12px; color: var(--gray); }

/* ---------- จอแคบ (มือถือ) ---------- */
@media (max-width: 420px) {
  .content { padding: 8px; padding-bottom: 80px; }
  .card { padding: 10px; }
  table { font-size: 12px; }
  th, td { padding: 6px 7px; }
  td.freeze, th.freeze { max-width: 110px; }
}
