:root {
  --navy: #14284b;
  --navy-2: #1f3a68;
  --accent: #e8792b;
  --accent-dark: #c9611a;
  --green: #1e8e5a;
  --green-bg: #e3f5ec;
  --red: #c0392b;
  --red-bg: #fdecea;
  --amber-bg: #fff4dc;
  --amber: #a46a00;
  --bg: #eef1f6;
  --card: #ffffff;
  --text: #1b2433;
  --muted: #6b7587;
  --line: #dde2ea;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(20, 40, 75, .06), 0 4px 14px rgba(20, 40, 75, .06);
  --tabbar-h: 64px;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 16px;
  line-height: 1.4;
  color: var(--text);
  background: var(--bg);
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 16px);
}
a { color: var(--navy-2); }
small, .en { color: var(--muted); font-size: .78em; font-weight: 400; }
h1 { font-size: 1.45rem; margin: 4px 0 14px; line-height: 1.2; }
h2 { font-size: 1.12rem; margin: 22px 0 10px; }
h3 { font-size: 1rem; margin: 0 0 8px; }
h1 small, h2 small, h3 small { display: block; margin-top: 2px; }
h1 small small, h1 small a { display: inline; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
  background: var(--navy); color: #fff;
}
.brand { display: flex; align-items: center; gap: 10px; color: #fff; text-decoration: none; min-width: 0; }
.brand-logo { height: 32px; width: auto; background: #fff; border-radius: 8px; padding: 3px 6px; }
.brand-text { font-weight: 700; font-size: 1.1rem; white-space: nowrap; }
.brand-text small { color: #b9c6dd; margin-left: 4px; }
.topbar-right { display: flex; align-items: center; gap: 10px; font-size: .9rem; }
.whoami { color: #d7e0ef; }
.btn-link { background: none; border: 0; color: #fff; font: inherit; cursor: pointer; padding: 6px 0; }
.btn-link small { color: #b9c6dd; }

/* ---------- layout ---------- */
.app { max-width: 760px; margin: 0 auto; padding: 16px; }
.card {
  background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 16px; margin-bottom: 14px;
}
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.row > * { min-width: 0; }
.grow { flex: 1 1 auto; }
.between { justify-content: space-between; }
.muted { color: var(--muted); }
.center { text-align: center; }
.mt { margin-top: 12px; }
.stack > * + * { margin-top: 10px; }

/* ---------- tab bar ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  display: grid; grid-template-columns: repeat(5, 1fr);
  background: #fff; border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabbar a {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  height: var(--tabbar-h); text-decoration: none; color: var(--muted); font-size: .72rem; line-height: 1.15;
}
.tabbar a small { font-size: .62rem; }
.tabbar .ti { font-size: 1.25rem; margin-bottom: 2px; filter: grayscale(1); opacity: .7; }
.tabbar a.active { color: var(--navy); font-weight: 600; }
.tabbar a.active .ti { filter: none; opacity: 1; }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 46px; padding: 10px 16px; border-radius: 12px;
  border: 1px solid var(--line); background: #fff; color: var(--text);
  font: inherit; font-weight: 600; text-decoration: none; cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.btn small { font-weight: 400; }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .5; cursor: default; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary small { color: #ffe6d3; }
.btn-primary:hover { background: var(--accent-dark); }
.btn-navy { background: var(--navy); border-color: var(--navy); color: #fff; }
.btn-navy small { color: #c6d2e8; }
.btn-danger { color: var(--red); border-color: #efc4be; }
.btn-sm { min-height: 36px; padding: 6px 12px; font-size: .9rem; border-radius: 10px; }
.btn-block { width: 100%; }
.btn-ghost { background: transparent; border-style: dashed; color: var(--navy-2); }

.big-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 16px; }
.big-btn {
  display: flex; flex-direction: column; align-items: flex-start; justify-content: flex-end; gap: 2px;
  min-height: 112px; padding: 16px; border-radius: 18px; text-decoration: none; color: #fff;
  font-weight: 700; font-size: 1.12rem; box-shadow: var(--shadow);
}
.big-btn .bi { font-size: 1.8rem; margin-bottom: auto; }
.big-btn small { color: rgba(255,255,255,.8); }
.big-btn.pickup { background: linear-gradient(135deg, #1f3a68, #14284b); }
.big-btn.deliver { background: linear-gradient(135deg, #f08a3e, #d9661c); }

/* ---------- stats ---------- */
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-bottom: 14px; }
.stat { background: #fff; border-radius: var(--radius); padding: 12px; box-shadow: var(--shadow); text-decoration: none; color: inherit; }
.stat b { display: block; font-size: 1.4rem; color: var(--navy); }
.stat span { font-size: .8rem; line-height: 1.2; display: block; }
.stat.warn b { color: var(--red); }

/* ---------- forms ---------- */
label.field { display: block; margin-bottom: 12px; }
label.field > span, .field-label { display: block; font-weight: 600; font-size: .92rem; margin-bottom: 5px; }
input[type=text], input[type=number], input[type=date], input[type=password], input[type=tel], input[type=search], select, textarea {
  width: 100%; min-height: 46px; padding: 10px 12px;
  border: 1px solid #c9d1dd; border-radius: 10px; background: #fff;
  font: inherit; font-size: 16px; color: var(--text);
  -webkit-appearance: none; appearance: none;
}
input[type=date] { display: block; }
select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8'%3E%3Cpath d='M1 1l5 5 5-5' stroke='%236b7587' stroke-width='2' fill='none'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 12px center; padding-right: 34px;
}
textarea { min-height: 70px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid #9fb6dd; border-color: var(--navy-2); }
.invalid { border-color: var(--red) !important; background: #fff7f6 !important; }

.seg { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.seg button {
  min-height: 52px; border-radius: 12px; border: 2px solid var(--line); background: #fff;
  font: inherit; font-weight: 600; font-size: .95rem; cursor: pointer; padding: 6px; color: var(--text);
}
.seg button.on { border-color: var(--navy); background: #e8eef9; color: var(--navy); }

.check {
  display: flex; align-items: center; gap: 12px; padding: 12px; border: 1px solid var(--line);
  border-radius: 12px; margin-bottom: 8px; cursor: pointer; background: #fff;
}
.check input { width: 24px; height: 24px; accent-color: var(--green); flex: none; }
.check.on { border-color: var(--green); background: var(--green-bg); }

.stepper { display: flex; align-items: stretch; }
.stepper button {
  width: 46px; border: 1px solid #c9d1dd; background: #f5f7fa; font-size: 1.3rem; cursor: pointer; color: var(--navy);
}
.stepper button:first-child { border-radius: 10px 0 0 10px; }
.stepper button:last-child { border-radius: 0 10px 10px 0; }
.stepper input { border-radius: 0; text-align: center; border-left: 0; border-right: 0; min-width: 60px; }

/* ---------- carton groups ---------- */
.group { border: 2px solid var(--line); border-radius: var(--radius); padding: 14px; margin-bottom: 14px; background: #fafbfd; }
.group-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }
.group-title { font-weight: 700; color: var(--navy); }
.badge-num {
  display: inline-flex; align-items: center; justify-content: center; width: 26px; height: 26px;
  border-radius: 50%; background: var(--navy); color: #fff; font-size: .85rem; margin-right: 6px;
}
.item { border: 1px solid var(--line); border-radius: 12px; padding: 10px; background: #fff; margin-bottom: 8px; }
.item-row { display: grid; grid-template-columns: 1fr 110px; gap: 8px; }
.item-foot { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-top: 8px; }
.calc-line { background: #eef3fb; color: var(--navy); border-radius: 10px; padding: 8px 10px; font-weight: 600; font-size: .92rem; margin-top: 6px; }
.icon-btn { border: 0; background: none; color: var(--muted); font-size: 1.1rem; cursor: pointer; padding: 6px 8px; border-radius: 8px; }
.icon-btn:hover { background: #eef1f6; color: var(--red); }

/* ---------- photos ---------- */
.photos { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.ph {
  position: relative; width: 74px; height: 74px; border-radius: 10px; overflow: hidden; background: #e5e9f0; flex: none;
}
.ph img { width: 100%; height: 100%; object-fit: cover; display: block; cursor: zoom-in; }
.ph .ph-x {
  position: absolute; top: 3px; right: 3px; width: 24px; height: 24px; border-radius: 50%;
  border: 0; background: rgba(0,0,0,.6); color: #fff; font-size: .8rem; cursor: pointer; line-height: 1;
}
.ph.loading::after {
  content: ""; position: absolute; inset: 0; margin: auto; width: 26px; height: 26px; border-radius: 50%;
  border: 3px solid #fff; border-top-color: var(--navy); animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.ph-add {
  width: 74px; height: 74px; border-radius: 10px; border: 2px dashed #b6c1d2; background: #fff;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px;
  font-size: .7rem; color: var(--navy-2); cursor: pointer; line-height: 1.1; text-align: center;
}
.ph-add .pi { font-size: 1.3rem; }
.ph-sm { width: 56px; height: 56px; }

/* ---------- lists ---------- */
.list { background: #fff; border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; margin-bottom: 14px; }
.li {
  display: flex; gap: 12px; align-items: center; padding: 13px 14px; border-bottom: 1px solid var(--line);
  text-decoration: none; color: inherit;
}
.li:last-child { border-bottom: 0; }
.li:active { background: #f4f6fa; }
.li-icon { width: 40px; height: 40px; border-radius: 10px; display: flex; align-items: center; justify-content: center; font-size: 1.2rem; flex: none; }
.li-icon.p { background: #e6ecf7; }
.li-icon.d { background: #fdeee2; }
.li-icon.c { background: #e9f4ee; }
.li-main { flex: 1; min-width: 0; }
.li-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; }
.li-sub { color: var(--muted); font-size: .86rem; overflow: hidden; text-overflow: ellipsis; }
.li-right { text-align: right; font-size: .86rem; color: var(--muted); flex: none; }
.chev { color: #b0b8c6; }
.empty { padding: 26px 16px; text-align: center; color: var(--muted); }

.pill { display: inline-block; padding: 3px 9px; border-radius: 99px; font-size: .78rem; font-weight: 600; white-space: nowrap; }
.pill.ok { background: var(--green-bg); color: var(--green); }
.pill.no { background: #fdecea; color: var(--red); }
.pill.grey { background: #eceff4; color: var(--muted); }
.pill.navy { background: #e6ecf7; color: var(--navy); }

/* ---------- tables ---------- */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table.t { width: 100%; border-collapse: collapse; font-size: .92rem; }
table.t th, table.t td { padding: 9px 8px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; }
table.t th { font-size: .78rem; color: var(--muted); font-weight: 600; text-transform: none; line-height: 1.2; }
table.t th small { display: block; font-size: .9em; }
table.t td:first-child { word-break: break-word; }
@media (max-width: 480px) {
  table.t { font-size: .88rem; }
  table.t th, table.t td { padding: 8px 5px; }
}
.dv-item { border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; margin-bottom: 10px; }
.dv-top { display: flex; justify-content: space-between; gap: 10px; align-items: baseline; margin-bottom: 8px; }
.dv-top span { white-space: nowrap; font-size: .9rem; }
.dv-inputs { display: grid; grid-template-columns: 1fr 1fr auto; gap: 8px; align-items: end; }
.dv-inputs input { text-align: center; }
.dv-inputs .btn { min-height: 46px; flex-direction: column; gap: 0; line-height: 1.1; }
table.t td.num, table.t th.num { text-align: right; white-space: nowrap; }
table.t tr:last-child td { border-bottom: 0; }
td.rem-pos { color: var(--navy); font-weight: 700; }
td.rem-zero { color: var(--green); font-weight: 600; }
td.rem-neg { color: var(--red); font-weight: 700; }

.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; font-size: .95rem; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }

.toolbar { display: flex; gap: 8px; margin-bottom: 12px; flex-wrap: wrap; }
.toolbar > input, .toolbar > select { flex: 1 1 160px; }
.chips { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 4px; margin-bottom: 10px; }
.chips button {
  border: 1px solid var(--line); background: #fff; border-radius: 99px; padding: 7px 12px; font: inherit; font-size: .88rem;
  white-space: nowrap; cursor: pointer; color: var(--text);
}
.chips button.on { background: var(--navy); color: #fff; border-color: var(--navy); }

.notice { border-radius: 12px; padding: 10px 12px; font-size: .92rem; margin-bottom: 12px; }
.notice.warn { background: var(--amber-bg); color: #6f4800; }
.notice.err { background: #fdecea; color: var(--red); }
.notice.info { background: #e8eef9; color: var(--navy); }

.sticky-save {
  position: sticky; bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 8px); z-index: 10;
  background: rgba(238, 241, 246, .92); backdrop-filter: blur(6px); padding: 10px 0 2px; margin-top: 8px;
}

.group-view { border: 1px solid var(--line); border-radius: 12px; padding: 12px; margin-top: 10px; }
.group-view .gv-head { display: flex; justify-content: space-between; gap: 10px; align-items: flex-start; margin-bottom: 8px; }

/* ---------- login ---------- */
.login-wrap { position: fixed; inset: 0; z-index: 50; overflow-y: auto; display: flex; align-items: center; justify-content: center; padding: 20px; background: var(--navy); }
.login-card { width: 100%; max-width: 380px; background: #fff; border-radius: 20px; padding: 26px 22px; box-shadow: 0 10px 40px rgba(0,0,0,.25); }
.login-card .logo { display: block; max-height: 56px; margin: 0 auto 12px; }
.login-card h1 { text-align: center; }

/* ---------- toast / modal / lightbox ---------- */
.toast {
  position: fixed; left: 50%; transform: translateX(-50%); z-index: 100;
  bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 14px);
  max-width: calc(100% - 32px); background: #1b2433; color: #fff; padding: 12px 16px; border-radius: 12px;
  box-shadow: 0 6px 24px rgba(0,0,0,.25); font-size: .95rem;
}
.toast.err { background: var(--red); }
.toast.ok { background: var(--green); }
.modal {
  position: fixed; inset: 0; z-index: 80; background: rgba(15, 25, 45, .55);
  display: flex; align-items: flex-end; justify-content: center;
}
.modal-box {
  width: 100%; max-width: 560px; max-height: 92vh; overflow-y: auto; background: #fff;
  border-radius: 20px 20px 0 0; padding: 20px 18px calc(20px + env(safe-area-inset-bottom));
}
@media (min-width: 640px) {
  .modal { align-items: center; }
  .modal-box { border-radius: 20px; }
}
.lightbox { position: fixed; inset: 0; z-index: 90; background: rgba(0,0,0,.92); display: flex; align-items: center; justify-content: center; }
.lightbox img { max-width: 100%; max-height: 100%; object-fit: contain; }
.lb-close { position: absolute; top: calc(env(safe-area-inset-top) + 12px); right: 14px; width: 44px; height: 44px; border-radius: 50%; border: 0; background: rgba(255,255,255,.18); color: #fff; font-size: 1.2rem; cursor: pointer; }

.loading-page { padding: 50px 0; text-align: center; color: var(--muted); }
.spinner { width: 30px; height: 30px; border-radius: 50%; border: 3px solid #d4dbe6; border-top-color: var(--navy); animation: spin .8s linear infinite; margin: 0 auto 10px; }

@media (max-width: 420px) {
  .stats { grid-template-columns: 1fr 1fr 1fr; gap: 8px; }
  .stat { padding: 10px 8px; }
  .stat b { font-size: 1.2rem; }
  .item-row { grid-template-columns: 1fr 92px; }
  .whoami { display: none; }
}
.dv-inputs .field-label small { display: block; }
