:root {
  --bg: #f6f3ee; --card: #fff; --ink: #2a2118; --muted: #7a6e60; --line: #e6dfd3;
  --brand: #9a5b2e; --brand-d: #7a4520; --brand-l: #f3e4d4; --ok: #2f7d4f; --warn: #b7791f; --bad: #c0392b;
  --nav-h: 62px; --radius: 14px;
  font-family: 'Noto Sans Thai', 'Sarabun', 'Leelawadee UI', Tahoma, system-ui, sans-serif;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--bg); color: var(--ink); font-size: 16px; line-height: 1.45; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
h1, h2, h3 { margin: 0; line-height: 1.25; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.right { text-align: right; }
.num { font-variant-numeric: tabular-nums; }

/* ---------- shell ---------- */
#app { min-height: 100vh; display: flex; flex-direction: column; }
.topbar { position: sticky; top: 0; z-index: 20; background: var(--brand); color: #fff; display: flex; align-items: center; gap: 10px; padding: 10px 14px; }
.topbar h1 { font-size: 1.1rem; flex: 1; }
.topbar .who { font-size: .8rem; opacity: .9; }
main { flex: 1; padding: 12px 12px calc(var(--nav-h) + 90px); max-width: 980px; width: 100%; margin: 0 auto; }
.bottomnav { position: fixed; bottom: 0; left: 0; right: 0; z-index: 30; height: var(--nav-h); background: #fff; border-top: 1px solid var(--line); display: flex; padding-bottom: env(safe-area-inset-bottom); }
.bottomnav a { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; text-decoration: none; color: var(--muted); font-size: .8rem; }
.bottomnav a .ic { font-size: 1.35rem; line-height: 1; }
.bottomnav a.on { color: var(--brand); font-weight: 600; background: var(--brand-l); }
@media (min-width: 900px) {
  #app { flex-direction: row; flex-wrap: wrap; }
  .topbar { width: 100%; }
  .bottomnav { position: sticky; top: 52px; align-self: flex-start; flex-direction: column; width: 180px; height: calc(100vh - 52px); border-top: 0; border-right: 1px solid var(--line); padding-top: 8px; justify-content: flex-start; }
  .bottomnav a { flex: 0 0 auto; flex-direction: row; justify-content: flex-start; gap: 12px; padding: 14px 18px; font-size: 1rem; }
  main { flex: 1; min-width: 0; padding-bottom: 110px; max-width: none; margin: 0; padding-left: 28px; padding-right: 28px; }
  .content { max-width: 980px; }
}

/* ---------- parts ---------- */
.seg { display: flex; gap: 4px; background: #e9e2d6; padding: 4px; border-radius: 12px; margin-bottom: 12px; }
.seg button { flex: 1; border: 0; background: transparent; padding: 9px 6px; border-radius: 9px; color: var(--muted); font-weight: 500; }
.seg button.on { background: #fff; color: var(--brand-d); box-shadow: 0 1px 3px #0002; font-weight: 600; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; margin-bottom: 12px; }
.card h3 { font-size: 1rem; margin-bottom: 8px; }
.row { display: flex; gap: 8px; align-items: center; }
.row.wrap { flex-wrap: wrap; }
.grow { flex: 1; min-width: 0; }
.btn { border: 1px solid var(--brand); background: var(--brand); color: #fff; border-radius: 12px; padding: 12px 16px; font-weight: 600; min-height: 46px; }
.btn:active { background: var(--brand-d); }
.btn.ghost { background: #fff; color: var(--brand-d); }
.btn.danger { background: var(--bad); border-color: var(--bad); }
.btn.sm { min-height: 36px; padding: 6px 12px; font-size: .9rem; border-radius: 10px; }
.btn[disabled] { opacity: .5; }
input[type=text], input[type=search], input[type=password], input[type=date], input[type=number], select, textarea {
  width: 100%; border: 1px solid #d3c8b8; border-radius: 10px; padding: 10px 12px; background: #fff; min-height: 44px;
}
textarea { min-height: 70px; resize: vertical; }
label.f { display: block; font-size: .85rem; color: var(--muted); margin: 8px 0 3px; }
.chips { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 6px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip { flex: 0 0 auto; border: 1px solid var(--line); background: #fff; border-radius: 999px; padding: 6px 13px; font-size: .9rem; white-space: nowrap; }
.chip.on { background: var(--brand); color: #fff; border-color: var(--brand); }
.badge { display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: .75rem; background: var(--brand-l); color: var(--brand-d); }
.badge.bad { background: #fde8e5; color: var(--bad); }
.badge.ok { background: #e3f3ea; color: var(--ok); }
.badge.warn { background: #fdf0d8; color: var(--warn); }
.empty { text-align: center; color: var(--muted); padding: 30px 10px; }
.err { background: #fde8e5; color: var(--bad); border-radius: 10px; padding: 8px 12px; margin: 8px 0; }

/* item list + stepper */
.items { display: flex; flex-direction: column; gap: 6px; }
.item { display: flex; align-items: center; gap: 8px; background: #fff; border: 1px solid var(--line); border-radius: 12px; padding: 8px 10px; }
.item.sel { border-color: var(--brand); background: #fff8f0; }
.item .nm { font-weight: 600; word-break: break-word; }
.item .sub { font-size: .8rem; color: var(--muted); }
.stepper { display: flex; align-items: center; gap: 4px; }
.stepper button { width: 42px; height: 42px; border-radius: 10px; border: 1px solid #d3c8b8; background: #faf6f0; font-size: 1.4rem; line-height: 1; }
.stepper button:active { background: var(--brand-l); }
.stepper input { width: 54px; text-align: center; padding: 8px 2px; font-weight: 700; font-size: 1.1rem; min-height: 42px; }
.stepper input.z { color: #b9ae9f; font-weight: 400; }
.groupH { font-size: .8rem; font-weight: 600; color: var(--brand-d); margin: 10px 2px 2px; text-transform: none; }

.dock { position: fixed; left: 0; right: 0; bottom: var(--nav-h); z-index: 25; background: #fff; border-top: 1px solid var(--line); padding: 8px 12px; display: flex; gap: 10px; align-items: center; box-shadow: 0 -4px 12px #0001; }
.dock .sum { flex: 1; font-size: .9rem; line-height: 1.25; }
.dock .sum b { font-size: 1.05rem; }
@media (min-width: 900px) { .dock { left: 180px; bottom: 0; } }

/* modal */
.modal { position: fixed; inset: 0; z-index: 60; background: #0007; display: flex; align-items: flex-end; justify-content: center; }
.modal > .sheet { background: var(--bg); width: 100%; max-width: 560px; max-height: 92vh; overflow: auto; border-radius: 18px 18px 0 0; padding: 14px; }
.modal.full > .sheet { max-width: 720px; }
@media (min-width: 700px) { .modal { align-items: center; } .modal > .sheet { border-radius: 18px; } }
.sheet h2 { font-size: 1.15rem; margin-bottom: 8px; }

table.t { width: 100%; border-collapse: collapse; font-size: .9rem; }
table.t th, table.t td { padding: 6px 6px; border-bottom: 1px solid var(--line); text-align: right; white-space: nowrap; }
table.t th:first-child, table.t td:first-child { text-align: left; }
table.t th { color: var(--muted); font-weight: 500; font-size: .8rem; }
table.t tr.tot td { font-weight: 700; border-top: 2px solid var(--line); }
.scroll { overflow-x: auto; }

.kpis { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 12px; }
.kpi { background: #fff; border: 1px solid var(--line); border-radius: 12px; padding: 10px; }
.kpi .v { font-size: 1.3rem; font-weight: 700; }
.kpi .l { font-size: .75rem; color: var(--muted); }
.kpi .d { font-size: .75rem; }
.up { color: var(--bad); } .dn { color: var(--ok); } .flat { color: var(--muted); }
.hbar { display: flex; align-items: center; gap: 8px; margin: 5px 0; font-size: .9rem; }
.hbar .lab { flex: 0 0 42%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hbar .bar { flex: 1; background: #efe8dc; border-radius: 6px; height: 14px; overflow: hidden; }
.hbar .bar i { display: block; height: 100%; background: var(--brand); border-radius: 6px; }
.hbar .val { flex: 0 0 auto; min-width: 60px; text-align: right; font-variant-numeric: tabular-nums; }
.cols { display: flex; align-items: flex-end; gap: 3px; height: 130px; padding-top: 6px; }
.cols .c { flex: 1; display: flex; flex-direction: column; justify-content: flex-end; align-items: center; height: 100%; min-width: 0; }
.cols .c i { width: 100%; background: var(--brand); border-radius: 4px 4px 0 0; min-height: 1px; }
.cols .c span { font-size: .6rem; color: var(--muted); margin-top: 2px; }
.cols .c em { font-size: .6rem; font-style: normal; }
.cols .c.zero i { background: #e6dfd3; }

.login { max-width: 360px; margin: 12vh auto 0; padding: 0 16px; }
.login h1 { text-align: center; color: var(--brand-d); margin-bottom: 18px; }

/* heat cell */
.heat td.h, .heat th:not(:first-child) { text-align: center; }
.sugg { font-weight: 700; color: var(--brand-d); }

/* ---------- slip (print) ---------- */
#print-area { display: none; }
.slip { font-family: 'Noto Sans Thai', 'Leelawadee UI', Tahoma, sans-serif; color: #000; background: #fff; width: var(--slip-w, 80mm); padding: 3mm 3mm 6mm; font-size: 12px; line-height: 1.3; margin: 0 auto; }
.slip h2 { text-align: center; font-size: 15px; margin: 0; }
.slip .c { text-align: center; }
.slip .doc { text-align: center; font-size: 17px; font-weight: 700; letter-spacing: .5px; margin: 4px 0; }
.slip hr { border: 0; border-top: 1px dashed #000; margin: 5px 0; }
.slip table { width: 100%; border-collapse: collapse; }
.slip td, .slip th { padding: 1.5px 0; vertical-align: top; }
.slip th { text-align: left; font-size: 11px; border-bottom: 1px solid #000; }
.slip .r { text-align: right; white-space: nowrap; padding-left: 4px; }
.slip .tot td { font-weight: 700; border-top: 1px solid #000; padding-top: 3px; }
.slip .sig { margin-top: 14px; }
.slip .reprint { text-align: center; font-weight: 700; border: 1px solid #000; margin: 4px 0; }
.slip.a4 { font-size: 14px; padding: 12mm; }
@media print {
  body { background: #fff; }
  #app, .modal, .dock { display: none !important; }
  #print-area { display: block !important; }
  .slip { margin: 0; }
}

/* compact steppers inside tables (bun entry on small phones) */
table.t .stepper { gap: 2px; justify-content: center; }
table.t .stepper button { width: 30px; height: 40px; font-size: 1.2rem; }
table.t .stepper input { width: 40px; padding: 4px 0; }
table.t td, table.t th { padding-left: 3px; padding-right: 3px; }
@media print { #toast { display: none !important; } }
.row > input[type=date] { flex: 1; min-width: 0; width: auto; }
