:root {
  --green: #2f6f4f; --green-dark: #24573e; --bg: #faf7f2; --card: #fff; --ink: #222; --muted: #6b6b6b;
  --line: #e4ddd2; --accent: #f2b134; --danger: #b3261e; --radius: 16px;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--bg); color: var(--ink);
  font: 18px/1.35 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
[hidden] { display: none !important; }
button { font: inherit; cursor: pointer; }
.view { min-height: 100vh; padding-bottom: calc(24px + env(safe-area-inset-bottom)); }
.has-tabs .view { padding-bottom: calc(96px + env(safe-area-inset-bottom)); }

/* Top bar + search */
.topbar { position: sticky; top: 0; z-index: 5; background: var(--green); color: #fff;
  padding: calc(10px + env(safe-area-inset-top)) 14px 12px; box-shadow: 0 2px 8px rgba(0,0,0,.15); }
.title-row { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }
.title-row h1 { font-size: 22px; margin: 0; flex: 1; }
.badge { background: var(--accent); color: #3a2a00; font-size: 13px; font-weight: 800; padding: 3px 8px; border-radius: 8px; }
.icon-btn { background: rgba(255,255,255,.15); color: #fff; border: 0; width: 48px; height: 48px; border-radius: 12px; font-size: 24px; }
#search { width: 100%; height: 52px; border-radius: 14px; border: 0; padding: 0 16px; font-size: 18px; }
.status { text-align: center; color: var(--muted); margin: 16px; }

/* Grid */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 12px; padding: 12px; }
.card { background: var(--card); border: 0; border-radius: var(--radius); padding: 0; text-align: left; overflow: hidden;
  box-shadow: 0 1px 3px rgba(0,0,0,.08); display: flex; flex-direction: column; }
.card:active { transform: scale(.97); }
.card .img { aspect-ratio: 1; background: #eee center/cover no-repeat; width: 100%; }
.card .name { padding: 8px 10px 2px; font-size: 16px; font-weight: 600; line-height: 1.2;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; min-height: 2.4em; }
.card .meta { padding: 0 10px 10px; font-size: 14px; color: var(--muted); }

/* Add screen */
.add-top { padding-bottom: 10px; }
.back-btn { background: rgba(255,255,255,.15); color: #fff; border: 0; height: 48px; padding: 0 18px; border-radius: 12px; font-size: 20px; font-weight: 600; }
.add-body { padding: 14px; max-width: 560px; margin: 0 auto; }
.add-head { display: flex; gap: 14px; align-items: center; margin-bottom: 8px; }
.add-head img { width: 96px; height: 96px; object-fit: cover; border-radius: 14px; background: #eee; flex: none; }
.add-head h2 { font-size: 22px; margin: 0; }
.opt-group { margin: 14px 0; }
.opt-group h3 { font-size: 15px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 0 0 8px; }
.opt-values { display: flex; flex-wrap: wrap; gap: 10px; }
.opt { min-height: 56px; padding: 0 18px; border-radius: 14px; border: 2px solid var(--line); background: #fff; font-size: 18px; font-weight: 600; color: var(--ink); }
.opt[aria-pressed="true"] { border-color: var(--green); background: var(--green); color: #fff; }
.opt:disabled { opacity: .35; text-decoration: line-through; }
.stock-line { font-size: 18px; margin: 16px 0 10px; text-align: center; }
.stock-line strong { font-size: 22px; }
.qty-row { display: flex; align-items: center; justify-content: center; gap: 14px; margin: 8px 0 18px; }
.qty-btn { width: 84px; height: 84px; border-radius: 50%; border: 0; background: #e9e2d6; font-size: 44px; font-weight: 700; color: var(--ink); line-height: 1; }
.qty-btn:active { background: #d9cfbf; }
#qtyInput { width: 110px; height: 84px; text-align: center; font-size: 44px; font-weight: 800; border: 2px solid var(--line); border-radius: 18px; background: #fff; -moz-appearance: textfield; }
#qtyInput::-webkit-outer-spin-button, #qtyInput::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.btn-add, .btn-primary { width: 100%; min-height: 76px; border: 0; border-radius: 20px; background: var(--green); color: #fff; font-size: 26px; font-weight: 800; box-shadow: 0 3px 0 var(--green-dark); }
.btn-add:active { transform: translateY(2px); box-shadow: 0 1px 0 var(--green-dark); }
.btn-add:disabled { background: #a9b8ae; box-shadow: none; }
.hint { text-align: center; color: var(--muted); font-size: 15px; min-height: 1.4em; }

/* PIN */
.pin-view { display: flex; flex-direction: column; align-items: center; padding: 60px 24px; text-align: center; }
.pin-logo { width: 96px; height: 96px; border-radius: 22px; }
.pin-view h1 { margin: 14px 0 4px; }
#pinForm { width: 100%; max-width: 320px; display: grid; gap: 14px; }
#pinInput { height: 68px; font-size: 32px; text-align: center; letter-spacing: .3em; border-radius: 16px; border: 2px solid var(--line); }
.error { color: var(--danger); font-weight: 600; min-height: 1.4em; }

/* Toast */
.toast { position: fixed; left: 12px; right: 12px; bottom: calc(16px + env(safe-area-inset-bottom)); z-index: 20; background: #1f1f1f; color: #fff;
  border-radius: 16px; padding: 14px 16px; display: flex; align-items: center; gap: 12px; font-size: 17px; box-shadow: 0 6px 20px rgba(0,0,0,.3); }
.toast.error-toast { background: var(--danger); }
.toast span { flex: 1; }
.undo-btn { background: transparent; color: var(--accent); border: 2px solid var(--accent); border-radius: 12px; height: 48px; padding: 0 16px; font-weight: 800; }

.has-tabs .toast { bottom: calc(92px + env(safe-area-inset-bottom)); }

/* Tab bar */
.tabbar { position: fixed; left: 0; right: 0; bottom: 0; z-index: 10; display: flex; background: #fff;
  border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom); box-shadow: 0 -2px 10px rgba(0,0,0,.06); }
.tab { flex: 1; min-height: 76px; border: 0; background: none; font-size: 18px; font-weight: 700; color: var(--muted);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; }
.tab-ico { font-size: 24px; line-height: 1; }
.tab[aria-current="page"] { color: var(--green); box-shadow: inset 0 4px 0 var(--green); }
.count { display: inline-block; min-width: 26px; padding: 1px 7px; border-radius: 13px; background: var(--danger); color: #fff; font-size: 15px; }

/* To print */
.summary { margin: 0; font-size: 18px; font-weight: 600; }
.print-list { padding: 10px 12px; display: grid; gap: 10px; }
.prow { display: flex; align-items: center; gap: 12px; width: 100%; text-align: left; background: var(--card); border: 0;
  border-radius: var(--radius); padding: 10px; box-shadow: 0 1px 3px rgba(0,0,0,.08); min-height: 96px; color: var(--ink); }
.prow:active { transform: scale(.98); }
.prow img { width: 76px; height: 76px; border-radius: 12px; object-fit: cover; background: #eee; flex: none; }
.prow .info { flex: 1; min-width: 0; }
.prow .ptitle { font-size: 16px; font-weight: 600; line-height: 1.2; }
.prow .pvar { display: flex; flex-wrap: wrap; gap: 4px; margin: 5px 0 4px; }
.chip { background: #eef5f0; color: var(--green-dark); border: 1px solid #cfe3d6; border-radius: 8px; padding: 1px 7px; font-size: 15px; font-weight: 700; }
.prow .porders { font-size: 13px; color: var(--muted); }
.need { flex: none; text-align: center; min-width: 70px; }
.need small { display: block; font-size: 13px; color: var(--muted); font-weight: 700; text-transform: uppercase; }
.need b { display: block; font-size: 36px; line-height: 1; color: var(--danger); }
.all-good { text-align: center; padding: 40px 20px; font-size: 20px; }
.all-good div { font-size: 54px; }

/* Add screen extras */
.for-orders { background: #fff4dc; border: 1px solid #f2d79a; color: #5a4200; border-radius: 12px; padding: 8px 12px; font-size: 16px; margin: 8px 0 0; }
.fix-row { display: flex; gap: 12px; align-items: flex-start; background: #fff; border: 2px solid var(--line); border-radius: 14px;
  padding: 12px; margin: 0 0 14px; font-size: 16px; }
.fix-row input { width: 28px; height: 28px; flex: none; accent-color: var(--green); margin: 0; }

/* Keep quantity + Add button always on screen */
.add-actions { position: sticky; bottom: 0; z-index: 4; background: var(--bg); width: 100%;
  padding: 8px max(14px, calc((100% - 532px) / 2)) calc(10px + env(safe-area-inset-bottom)); border-top: 1px solid var(--line);
  box-shadow: 0 -4px 12px rgba(0,0,0,.06); }
.add-actions .fix-row { margin: 2px 0 6px; padding: 8px 10px; font-size: 14px; line-height: 1.3; }
.add-actions .stock-line { margin: 2px 0 6px; font-size: 17px; }
.add-actions .qty-row { margin: 4px 0 10px; }
.add-actions .qty-btn { width: 76px; height: 76px; }
.add-actions #qtyInput { height: 76px; }
#addView { padding-bottom: 0; display: flex; flex-direction: column; }
#addView .add-body { flex: 1; width: 100%; }
.tab .count { margin-left: 4px; vertical-align: 2px; }
.tab { flex-direction: column; }

/* Add printed / Set count switch */
.mode-seg { display: flex; background: #e9e2d6; border-radius: 16px; padding: 4px; margin: 4px 0 10px; }
.seg { flex: 1; min-height: 56px; border: 0; border-radius: 12px; background: transparent; font-size: 18px; font-weight: 800; color: var(--muted); }
.seg[aria-selected="true"] { background: #fff; color: var(--green); box-shadow: 0 1px 4px rgba(0,0,0,.15); }
#countPane .count-help { font-size: 14px; color: var(--muted); margin: 0 0 8px; }

/* Count rows */
.count-list { display: grid; gap: 8px; }
.crow { background: #fff; border: 2px solid var(--line); border-radius: 14px; padding: 8px 10px; display: grid;
  grid-template-columns: 1fr auto; align-items: center; gap: 4px 10px; }
.crow.changed { border-color: var(--accent); background: #fffaf0; }
.crow.stale { border-color: var(--danger); }
.crow .cname { font-weight: 700; font-size: 16px; line-height: 1.2; }
.crow .cmeta { grid-column: 1 / 2; font-size: 13px; color: var(--muted); }
.crow .cdelta { font-weight: 800; color: var(--green-dark); }
.crow .cdelta.neg { color: var(--danger); }
.cstep { grid-row: 1 / span 2; grid-column: 2; display: flex; align-items: center; gap: 6px; }
.cstep button { width: 56px; height: 56px; border-radius: 14px; border: 0; background: #e9e2d6; font-size: 30px; font-weight: 800; color: var(--ink); }
.cstep input { width: 64px; height: 56px; text-align: center; font-size: 26px; font-weight: 800; border: 2px solid var(--line); border-radius: 12px; -moz-appearance: textfield; }
.cstep input::-webkit-outer-spin-button, .cstep input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.crow.single { padding: 14px; }

/* Counted ticks + stocktake */
.card { position: relative; }
.tick { position: absolute; top: 8px; right: 8px; width: 40px; height: 40px; border-radius: 50%; background: var(--green); color: #fff;
  display: flex; align-items: center; justify-content: center; font-size: 24px; font-weight: 900; box-shadow: 0 2px 6px rgba(0,0,0,.3); border: 3px solid #fff; }
.card.counted .img { opacity: .55; }
.stocktake-bar { margin: 10px 12px 0; background: #eef5f0; border: 1px solid #cfe3d6; color: var(--green-dark); border-radius: 12px; padding: 8px 12px; font-weight: 700; font-size: 16px; }
.sheet { position: fixed; inset: 0; z-index: 30; background: rgba(0,0,0,.4); display: flex; align-items: flex-end; }
.sheet-card { background: var(--bg); width: 100%; border-radius: 22px 22px 0 0; padding: 18px 16px calc(18px + env(safe-area-inset-bottom)); display: grid; gap: 12px; }
.sheet-card h2 { margin: 0; }
.sheet-card p { margin: 0; font-size: 17px; }
.btn-secondary { width: 100%; min-height: 64px; border-radius: 18px; border: 2px solid var(--danger); background: #fff; color: var(--danger); font-size: 20px; font-weight: 800; }
.btn-primary { min-height: 64px; }
.tick { z-index: 2; }

/* Menu extras */
.menu-row { display: flex; gap: 10px; }
.btn-outline { flex: 1; min-height: 60px; border-radius: 16px; border: 2px solid var(--green); background: #fff; color: var(--green); font-size: 18px; font-weight: 800; }
.btn-outline.small { flex: 0 0 auto; padding: 0 14px; border-color: var(--line); color: var(--muted); font-size: 16px; }
.sheet-card .mode-seg { margin: 0; }

/* Arrange mode */
.arrange-bar { display: flex; align-items: center; gap: 10px; font-size: 15px; font-weight: 600; }
.arrange-bar span { flex: 1; }
.done-btn { min-height: 52px; padding: 0 22px; border-radius: 14px; border: 0; background: var(--accent); color: #3a2a00; font-size: 20px; font-weight: 900; }
.arranging #search, .arranging #refreshBtn, .arranging #menuBtn, .arranging .tabbar { display: none !important; }
.arranging .view { padding-bottom: 24px; }
.card.arr { user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; }
.card.arr .img { aspect-ratio: 4 / 3; }
.card.arr .name { min-height: 0; padding-bottom: 6px; }
.handle { position: absolute; top: 6px; left: 6px; z-index: 3; width: 56px; height: 56px; border-radius: 14px; border: 0;
  background: rgba(255,255,255,.95); color: var(--ink); font-size: 28px; box-shadow: 0 2px 6px rgba(0,0,0,.25);
  touch-action: none; cursor: grab; }
.arr-btns { display: flex; gap: 6px; padding: 0 8px 8px; }
.arr-btns button { flex: 1; min-height: 48px; border-radius: 12px; border: 0; background: #e9e2d6; font-size: 20px; font-weight: 800; color: var(--ink); }
.arr-btns button[data-move="top"] { flex: 1.6; font-size: 16px; }
.arr-btns button:disabled { opacity: .35; }
.card.placeholder { opacity: .35; outline: 3px dashed var(--green); outline-offset: -3px; }
.card.ghost { position: fixed; z-index: 50; pointer-events: none; opacity: .95; transform: scale(1.04) rotate(1deg); box-shadow: 0 12px 30px rgba(0,0,0,.35); margin: 0; }
.card.moved { animation: flash .7s ease; }
@keyframes flash { 0% { box-shadow: 0 0 0 4px var(--accent); } 100% { box-shadow: 0 1px 3px rgba(0,0,0,.08); } }

/* To print: by order */
.print-seg { margin: 10px 0 0; background: rgba(255,255,255,.18); }
.print-seg .seg { color: #fff; min-height: 48px; }
.print-seg .seg[aria-selected="true"] { background: #fff; color: var(--green); }
.ogroup { background: #fff; border-radius: var(--radius); box-shadow: 0 1px 3px rgba(0,0,0,.08); overflow: hidden; }
.ohead { display: flex; align-items: center; gap: 10px; padding: 10px 12px; background: #f3efe7; border-bottom: 1px solid var(--line); }
.oname { font-size: 22px; font-weight: 900; }
.odate { color: var(--muted); font-weight: 600; flex: 1; }
.obadge { background: var(--danger); color: #fff; font-weight: 800; border-radius: 10px; padding: 4px 10px; font-size: 15px; }
.obadge.ready { background: var(--green); }
.ogroup .prow { box-shadow: none; border-radius: 0; border-top: 1px solid #f0ebe2; min-height: 80px; }
.ogroup .prow:first-of-type { border-top: 0; }
.ogroup .prow img { width: 60px; height: 60px; }
.prow.covered { opacity: .5; }
.instock { flex: none; min-width: 70px; text-align: center; color: var(--green); font-size: 30px; font-weight: 900; line-height: 1; }
.instock small { display: block; font-size: 12px; font-weight: 800; text-transform: uppercase; }
.ready-box { background: #eef5f0; border: 1px solid #cfe3d6; border-radius: var(--radius); padding: 0 8px 8px; }
.ready-box summary { min-height: 56px; display: flex; align-items: center; font-size: 18px; font-weight: 800; color: var(--green-dark); padding: 0 6px; cursor: pointer; }
.ready-box .ogroup { margin-top: 8px; }
