:root {
  --brand: #2f6b3a;
  --brand-2: #3f8a4c;
  --brand-soft: #e7f2e8;
  --kraft: #d8b98b;
  --kraft-dark: #a9824f;
  --kraft-light: #f3e6cf;
  --ink: #1f2a22;
  --muted: #6b756d;
  --line: #e5e8e3;
  --bg: #f6f7f3;
  --card: #ffffff;
  --up: #c0392b;
  --down: #2e8b57;
  --radius: 16px;
  --tabbar-h: 64px;
  --bs-primary: #2f6b3a;
  --bs-primary-rgb: 47, 107, 58;
  --bs-link-color: #2f6b3a;
  --bs-link-color-rgb: 47, 107, 58;
  --bs-body-font-family: 'IBM Plex Sans Thai', system-ui, -apple-system, sans-serif;
}

html, body { background: var(--bg); color: var(--ink); }
body {
  font-family: var(--bs-body-font-family);
  -webkit-tap-highlight-color: transparent;
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 16px);
}
body.has-costbar { padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 150px); }

.btn-primary { --bs-btn-bg: var(--brand); --bs-btn-border-color: var(--brand); --bs-btn-hover-bg: #275a31; --bs-btn-hover-border-color: #275a31; --bs-btn-active-bg: #214d2a; --bs-btn-active-border-color: #214d2a; --bs-btn-disabled-bg: var(--brand); --bs-btn-disabled-border-color: var(--brand); }
.btn-outline-primary { --bs-btn-color: var(--brand); --bs-btn-border-color: var(--brand); --bs-btn-hover-bg: var(--brand); --bs-btn-hover-border-color: var(--brand); --bs-btn-active-bg: var(--brand); --bs-btn-active-border-color: var(--brand); }
.text-bg-primary, .bg-primary { background-color: var(--brand) !important; }
.text-primary { color: var(--brand) !important; }
.form-control, .form-select { border-radius: 12px; min-height: 44px; }
.form-control:focus, .form-select:focus { border-color: var(--brand-2); box-shadow: 0 0 0 .2rem rgba(47,107,58,.15); }
.btn { border-radius: 12px; min-height: 44px; display: inline-flex; align-items: center; justify-content: center; gap: 6px; }
.btn-sm { min-height: 34px; border-radius: 10px; }
.form-label { font-weight: 500; font-size: .9rem; margin-bottom: .3rem; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 1020;
  background: rgba(255,255,255,.92); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--line);
  padding-top: env(safe-area-inset-top);
}
.topbar-inner { max-width: 760px; margin: 0 auto; display: flex; align-items: center; gap: 8px; height: 56px; padding: 0 12px; }
.topbar-inner.wide, .page.wide { max-width: 1180px; }
.topbar-title { font-size: 1.1rem; font-weight: 600; margin: 0; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-logo { width: 36px; height: 36px; border-radius: 10px; background: var(--brand); color: #fff; display: inline-flex; align-items: center; justify-content: center; font-size: 1.1rem; }
.topbar-btn { width: 40px; height: 40px; display: inline-flex; align-items: center; justify-content: center; border-radius: 12px; color: var(--ink); font-size: 1.3rem; text-decoration: none; }
.topbar-btn:active { background: var(--line); }
.topbar-actions { display: flex; gap: 6px; }

.page { max-width: 760px; margin: 0 auto; padding: 14px 12px; }
.flash { border-radius: 14px; }

/* ---------- bottom tab bar ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 1030;
  display: flex; justify-content: space-around;
  background: rgba(255,255,255,.96); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  border-top: 1px solid var(--line);
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
}
.tabbar a { flex: 1; max-width: 140px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; color: var(--muted); text-decoration: none; font-size: .72rem; }
.tabbar a i { font-size: 1.35rem; line-height: 1; }
.tabbar a.active { color: var(--brand); font-weight: 600; }

/* ---------- cards & lists ---------- */
.card-soft { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; margin-bottom: 12px; }
.section-title { font-size: .85rem; font-weight: 600; color: var(--muted); margin: 18px 4px 8px; display: flex; align-items: center; justify-content: space-between; }
.list-card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; margin-bottom: 12px; }
.list-row { display: flex; align-items: center; gap: 12px; padding: 12px 14px; border-bottom: 1px solid var(--line); color: inherit; text-decoration: none; }
.list-row:last-child { border-bottom: 0; }
a.list-row:active { background: #f1f3ef; }
.list-row .grow { flex: 1; min-width: 0; }
.list-row .title { font-weight: 500; }
.list-row .sub { font-size: .82rem; color: var(--muted); }
.list-row .chev { color: #b8bfb9; }

.stat-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
@media (min-width: 768px) { .stat-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); } }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; }
.stat .lbl { font-size: .8rem; color: var(--muted); }
.stat .val { font-size: 1.35rem; font-weight: 600; font-variant-numeric: tabular-nums; }
.stat.good .val { color: var(--down); }
.stat.bad .val { color: var(--up); }

.item-avatar { border-radius: 12px; object-fit: cover; flex-shrink: 0; display: inline-flex; align-items: center; justify-content: center; }
.item-avatar-txt { color: #fff; font-weight: 600; }
.num { font-variant-numeric: tabular-nums; }
.pill { display: inline-block; padding: 2px 10px; border-radius: 999px; font-size: .75rem; background: var(--brand-soft); color: var(--brand); font-weight: 500; }
.empty { text-align: center; padding: 36px 16px; color: var(--muted); }
.empty i { font-size: 2.4rem; display: block; margin-bottom: 8px; opacity: .6; }

.fab { position: fixed; right: 16px; bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 16px); width: 56px; height: 56px; border-radius: 18px; background: var(--brand); color: #fff; display: flex; align-items: center; justify-content: center; font-size: 1.6rem; box-shadow: 0 6px 18px rgba(47,107,58,.35); z-index: 1025; text-decoration: none; }
.fab:active { transform: scale(.95); color: #fff; }

.table-clean { width: 100%; font-size: .92rem; }
.table-clean th { font-weight: 500; color: var(--muted); font-size: .8rem; padding: 6px 4px; border-bottom: 1px solid var(--line); }
.table-clean td { padding: 8px 4px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.table-clean tr:last-child td { border-bottom: 0; }
.table-clean .r { text-align: right; }

.dyn-row { display: grid; gap: 8px; align-items: end; margin-bottom: 8px; }
.seg { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 4px; scrollbar-width: none; }
.seg::-webkit-scrollbar { display: none; }
.seg a, .seg button { white-space: nowrap; border: 1px solid var(--line); background: var(--card); color: var(--ink); border-radius: 999px; padding: 6px 14px; font-size: .88rem; text-decoration: none; }
.seg .active { background: var(--brand); color: #fff; border-color: var(--brand); }

/* ---------- shopping checklist ---------- */
.check-row { display: flex; align-items: center; gap: 12px; padding: 12px 14px; border-bottom: 1px solid var(--line); cursor: pointer; user-select: none; }
.check-row:last-child { border-bottom: 0; }
.check-row .tick { width: 28px; height: 28px; border-radius: 50%; border: 2px solid #c9d0ca; display: flex; align-items: center; justify-content: center; color: #fff; flex-shrink: 0; }
.check-row.done .tick { background: var(--brand); border-color: var(--brand); }
.check-row.done .title { text-decoration: line-through; color: var(--muted); }
.buy-qty { font-size: 1.15rem; font-weight: 600; color: var(--brand); white-space: nowrap; text-align: right; }

/* ======================================================================
   BOX BUILDER
   ====================================================================== */
.bb { display: grid; grid-template-columns: minmax(0, 1fr); gap: 14px; }
.bb-left, .bb-right { min-width: 0; }
@media (min-width: 900px) {
  .bb { grid-template-columns: 400px minmax(0, 1fr); align-items: start; }
  .bb-left { position: sticky; top: 70px; }
  body.has-costbar { padding-bottom: calc(var(--tabbar-h) + 24px); }
}

.bb-head { display: grid; grid-template-columns: 1fr 110px; gap: 8px; margin-bottom: 10px; }
.bb-head.order { grid-template-columns: 1fr 90px 100px; }
.bb-head .form-control { font-weight: 500; }

/* กล่องกระดาษ */
.box-wrap { position: sticky; top: 64px; z-index: 5; background: var(--bg); padding-bottom: 8px; }
@media (min-width: 900px) { .box-wrap { position: static; } }
.kraft-box {
  position: relative;
  background: var(--kraft-light);
  border: 3px solid var(--kraft);
  border-radius: 20px;
  padding: 8px 10px;
  box-shadow: inset 0 0 0 6px #fbf5ea, 0 6px 18px rgba(120, 90, 40, .12);
  transition: transform .15s;
}
.kraft-box.bump { transform: scale(1.02); }
.kraft-box::before {
  content: ''; position: absolute; left: 14%; right: 14%; top: -12px; height: 12px;
  background: var(--kraft); border-radius: 10px 10px 0 0;
}
.box-grid { display: flex; gap: 8px; overflow-x: auto; min-height: 92px; padding: 6px 2px 2px; scrollbar-width: none; }
.box-grid::-webkit-scrollbar { display: none; }
.box-grid .box-slot { flex: 0 0 68px; }
@media (min-width: 900px) {
  .box-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(78px, 1fr)); overflow: visible; min-height: 150px; }
}
.box-empty { grid-column: 1 / -1; display: flex; flex-direction: column; align-items: center; justify-content: center; color: var(--kraft-dark); font-size: .9rem; text-align: center; }
.box-empty i { font-size: 2rem; margin-bottom: 4px; }
.box-slot {
  position: relative; background: #fff; border: 1.5px dashed var(--kraft); border-radius: 14px;
  padding: 38px 4px 5px; text-align: center; cursor: pointer; animation: drop-in .35s cubic-bezier(.2,1.4,.4,1);
}
.box-slot .item-avatar { width: 40px !important; height: 40px !important; border-radius: 50%; }
@media (min-width: 900px) { .box-slot .item-avatar { width: 48px !important; height: 48px !important; } .kraft-box { padding: 14px; } }
.box-slot .nm { font-size: .72rem; line-height: 1.2; margin-top: 3px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.box-slot .qt { font-size: .7rem; color: var(--muted); }
.box-slot .badge-q {
  position: absolute; top: -5px; right: -4px; min-width: 24px; height: 24px; border-radius: 12px;
  background: var(--brand); color: #fff; font-size: .75rem; font-weight: 600; display: flex; align-items: center; justify-content: center; padding: 0 6px;
}
@keyframes drop-in { 0% { transform: translateY(-20px) scale(.4); opacity: 0; } 100% { transform: none; opacity: 1; } }
.box-slot.pulse { animation: pulse .3s; }
@keyframes pulse { 50% { transform: scale(1.12); } }

.fly { position: fixed; z-index: 2000; pointer-events: none; border-radius: 50%; }

/* แถบต้นทุน */
.costbar {
  position: fixed; left: 0; right: 0; bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom)); z-index: 1028;
  background: #fff; border-top: 1px solid var(--line); box-shadow: 0 -6px 20px rgba(0,0,0,.06);
  padding: 10px 14px;
}
.costbar-inner { max-width: 760px; margin: 0 auto; }
@media (min-width: 900px) {
  .costbar { position: static; border: 1px solid var(--line); border-radius: var(--radius); box-shadow: none; margin-top: 12px; }
}
.cost-main { display: flex; align-items: flex-end; justify-content: space-between; gap: 10px; }
.cost-total { font-size: 1.7rem; font-weight: 600; line-height: 1; font-variant-numeric: tabular-nums; }
.cost-total small { font-size: .9rem; font-weight: 500; color: var(--muted); }
.cost-delta { font-size: .9rem; font-weight: 600; margin-left: 6px; opacity: 0; transition: opacity .25s; }
.cost-delta.show { opacity: 1; }
.cost-delta.up { color: var(--up); }
.cost-delta.down { color: var(--down); }
.cost-profit { text-align: right; }
.cost-profit .p { font-size: 1.15rem; font-weight: 600; font-variant-numeric: tabular-nums; }
.cost-profit .p.neg { color: var(--up); }
.cost-profit .p.pos { color: var(--down); }
.cost-split { display: flex; height: 8px; border-radius: 4px; overflow: hidden; background: var(--line); margin: 8px 0 6px; }
.cost-split span { transition: width .3s; }
.cost-legend { display: flex; flex-wrap: wrap; gap: 4px 12px; font-size: .75rem; color: var(--muted); }
.cost-legend b { color: var(--ink); font-weight: 500; font-variant-numeric: tabular-nums; }
.cost-legend i { display: inline-block; width: 8px; height: 8px; border-radius: 2px; margin-right: 4px; }
.cost-extra { font-size: .8rem; color: var(--muted); margin-top: 4px; }
.g-fruit { background: #e67e22; } .g-food { background: #3b82f6; } .g-packaging { background: var(--kraft-dark); } .g-overhead { background: #9aa39c; }

/* การ์ดวัตถุดิบ */
.bb-filter { display: flex; gap: 8px; margin-bottom: 10px; }
.bb-filter input { flex: 1; }
.pick-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
@media (min-width: 560px) { .pick-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
@media (min-width: 1100px) { .pick-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); } }
.pick {
  background: var(--card); border: 2px solid var(--line); border-radius: 18px; padding: 10px;
  text-align: center; position: relative; transition: border-color .15s, transform .1s; user-select: none;
}
.pick.on { border-color: var(--brand); background: #fbfdfb; }
.pick .tap { cursor: pointer; }
.pick .tap:active { transform: scale(.96); }
.pick .item-avatar { width: 72px !important; height: 72px !important; border-radius: 50%; margin: 0 auto; }
.pick .nm { font-weight: 500; margin-top: 6px; line-height: 1.25; }
.pick .pc { font-size: .78rem; color: var(--muted); }
.pick .qty-badge { position: absolute; top: 8px; right: 8px; background: var(--brand); color: #fff; border-radius: 12px; min-width: 26px; height: 26px; font-size: .8rem; font-weight: 600; display: none; align-items: center; justify-content: center; padding: 0 7px; }
.pick.on .qty-badge { display: flex; }
.cut-chips { display: flex; flex-wrap: wrap; gap: 4px; justify-content: center; margin-top: 8px; }
.cut-chips button { border: 1px solid var(--line); background: #fff; border-radius: 999px; font-size: .78rem; padding: 3px 9px; color: var(--ink); min-height: 30px; }
.cut-chips button.sel { background: var(--brand-soft); border-color: var(--brand); color: var(--brand); font-weight: 600; }
.stepper { display: flex; align-items: center; justify-content: center; gap: 4px; margin-top: 8px; }
.stepper button { width: 40px; height: 40px; border-radius: 12px; border: 1px solid var(--line); background: #fff; font-size: 1.2rem; color: var(--ink); display: flex; align-items: center; justify-content: center; }
.stepper button:active { background: var(--line); }
.stepper input { width: 52px; height: 40px; text-align: center; border: 0; font-weight: 600; font-size: 1.05rem; background: transparent; }
.pick-cat { grid-column: 1 / -1; font-size: .85rem; font-weight: 600; color: var(--muted); margin: 6px 2px -2px; }

@media print {
  .topbar, .tabbar, .fab, .no-print, .costbar { display: none !important; }
  body { padding: 0 !important; background: #fff; }
  .card-soft, .list-card { border: 0; }
}

.box-slot .box-remove {
  position: absolute; top: 0; left: 0; width: 44px; height: 44px;
  border: 0; border-radius: 12px; background: transparent; color: #b42318;
  font-size: 28px; line-height: 1; cursor: pointer; padding: 0;
  display: flex; align-items: center; justify-content: center; touch-action: manipulation;
}
.box-slot .box-remove:hover { background: #fff0ee; }
.box-slot .box-remove:focus-visible { outline: 2px solid #b42318; outline-offset: -3px; }
