:root {
  --navy: #1e3a5f; --navy-2: #2c5282; --navy-soft: #e8eef6;
  --bg: #f3f5f9; --card: #ffffff; --line: #e2e7ef; --text: #16202e; --muted: #64748b;
  --ok: #15803d; --ok-bg: #dcfce7; --no: #c2410c; --no-bg: #ffedd5; --part: #a16207; --part-bg: #fef3c7;
  --pend: #475569; --pend-bg: #eef2f7; --danger: #b91c1c; --blue-bg: #dbeafe; --blue: #1d4ed8;
  --radius: 14px; --shadow: 0 1px 2px rgba(16,24,40,.06), 0 4px 14px rgba(16,24,40,.06);
  --top-h: 60px; --bottom-h: 64px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --navy: #7fb0ec; --navy-2: #9cc3f2; --navy-soft: #1c2a3c;
    --bg: #0e141c; --card: #161e29; --line: #263244; --text: #e6edf6; --muted: #93a3b8;
    --ok: #4ade80; --ok-bg: #11301f; --no: #fb923c; --no-bg: #3a1f0f; --part: #fbbf24; --part-bg: #352a0c;
    --pend: #b6c2d3; --pend-bg: #1f2937; --danger: #f87171; --blue-bg: #172554; --blue: #93c5fd;
    --shadow: 0 1px 2px rgba(0,0,0,.4);
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
  background: var(--bg); color: var(--text);
  padding-bottom: calc(var(--bottom-h) + env(safe-area-inset-bottom));
}
input, select, textarea, button { font: inherit; color: inherit; }
input, select, textarea {
  background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 9px 11px; min-height: 42px; width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--navy-2); outline-offset: -1px; border-color: transparent; }
textarea { min-height: 160px; resize: vertical; font-family: ui-monospace, Consolas, monospace; font-size: 14px; }
button { cursor: pointer; }
a { color: var(--navy-2); }

/* ── Top bar ── */
.top {
  position: sticky; top: 0; z-index: 20; height: var(--top-h); display: flex; align-items: center; gap: 16px;
  padding: 0 16px; padding-top: env(safe-area-inset-top); background: #1e3a5f; color: #fff; box-shadow: 0 2px 10px rgba(0,0,0,.15);
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand img { border-radius: 9px; background: #fff; }
.brand-name { font-weight: 700; font-size: 17px; letter-spacing: .2px; }
.brand-sub { font-size: 11.5px; opacity: .75; white-space: nowrap; }
.tabs { display: none; gap: 4px; margin-left: 12px; }
.tabs a, .bottom-nav a { text-decoration: none; color: inherit; position: relative; }
.tabs a { padding: 8px 14px; border-radius: 10px; color: #dbe6f3; font-weight: 500; }
.tabs a.on { background: rgba(255,255,255,.16); color: #fff; }
.tabs a:hover { background: rgba(255,255,255,.1); }
.top-right { margin-left: auto; display: flex; gap: 8px; align-items: center; }
.sync-pill, .me-chip {
  border: 0; border-radius: 999px; padding: 6px 12px; background: rgba(255,255,255,.14); color: #fff; font-size: 13px; display: flex; align-items: center; gap: 6px; white-space: nowrap;
}
.sync-pill .dot { width: 8px; height: 8px; border-radius: 50%; background: #94a3b8; }
.sync-pill.ok .dot { background: #4ade80; } .sync-pill.busy .dot { background: #fbbf24; animation: pulse 1s infinite; }
.sync-pill.err .dot { background: #f87171; }
@keyframes pulse { 50% { opacity: .35; } }
.badge { display: inline-block; min-width: 18px; padding: 0 5px; border-radius: 9px; background: #f59e0b; color: #111; font-size: 11px; font-weight: 700; line-height: 18px; text-align: center; }

.bottom-nav {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 20; display: grid; grid-template-columns: repeat(5, 1fr);
  background: var(--card); border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom); height: calc(var(--bottom-h) + env(safe-area-inset-bottom));
}
.bottom-nav a { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; font-size: 11.5px; color: var(--muted); font-weight: 500; }
.bottom-nav a .ic { font-size: 20px; line-height: 1; }
.bottom-nav a.on { color: var(--navy); }
.bottom-nav a.on .ic { transform: scale(1.1); }
.bottom-nav .badge { position: absolute; top: 6px; left: 55%; }

@media (min-width: 1000px) {
  .tabs { display: flex; }
  .bottom-nav { display: none; }
  body { padding-bottom: 24px; }
}
@media (max-width: 520px) {
  .brand-sub { display: none; }
  .me-chip .nm { display: none; }
}

/* ── Layout ── */
main { max-width: 1180px; margin: 0 auto; padding: 16px; outline: none; }
.loading { padding: 60px 0; text-align: center; color: var(--muted); }
.page-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin: 4px 0 14px; }
.page-head h1 { font-size: 22px; margin: 0; margin-right: auto; }
.page-head .sub { width: 100%; color: var(--muted); font-size: 13.5px; margin-top: -6px; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.pad { padding: 14px 16px; }
.stack { display: grid; gap: 12px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.grow { flex: 1; min-width: 0; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.right { text-align: right; }
.hide { display: none !important; }
hr.sep { border: 0; border-top: 1px solid var(--line); margin: 12px 0; }

/* ── Buttons ── */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 40px; padding: 8px 14px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--card); font-weight: 600; font-size: 14px; white-space: nowrap; text-decoration: none; color: var(--text);
}
.btn:hover { border-color: var(--navy-2); }
.btn.primary { background: var(--navy); border-color: var(--navy); color: #fff; }
@media (prefers-color-scheme: dark) { .btn.primary { color: #0e141c; } }
.btn.primary:hover { filter: brightness(1.08); }
.btn.green { background: #16a34a; border-color: #16a34a; color: #fff; }
.btn.wa { background: #25d366; border-color: #25d366; color: #073b1c; }
.btn.danger { color: var(--danger); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.sm { min-height: 32px; padding: 4px 10px; font-size: 13px; border-radius: 8px; }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.icon-btn { border: 0; background: transparent; padding: 6px 8px; border-radius: 8px; color: var(--muted); font-size: 16px; }
.icon-btn:hover { background: var(--pend-bg); color: var(--text); }

/* ── Stats ── */
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; margin-bottom: 14px; }
@media (max-width: 640px) { .stats { grid-template-columns: repeat(2, 1fr); } }
.stat { padding: 12px 14px; border-radius: var(--radius); background: var(--card); border: 1px solid var(--line); text-decoration: none; color: inherit; box-shadow: var(--shadow); }
.stat .n { font-size: 26px; font-weight: 800; line-height: 1.1; }
.stat .l { font-size: 12.5px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .4px; }
.stat.c-new .n { color: var(--pend); } .stat.c-short .n { color: var(--no); } .stat.c-ready .n { color: var(--ok); } .stat.c-po .n { color: var(--blue); }

/* ── Chips ── */
.chip { display: inline-flex; align-items: center; gap: 4px; padding: 2px 9px; border-radius: 999px; font-size: 12px; font-weight: 700; white-space: nowrap; }
.st-new, .st-pending, .st-empty { background: var(--pend-bg); color: var(--pend); }
.st-checking { background: var(--blue-bg); color: var(--blue); }
.st-short, .st-no { background: var(--no-bg); color: var(--no); }
.st-part { background: var(--part-bg); color: var(--part); }
.st-ready, .st-ok, .st-received { background: var(--ok-bg); color: var(--ok); }
.st-done { background: var(--pend-bg); color: var(--muted); }
.st-sent, .st-ordered { background: var(--blue-bg); color: var(--blue); }
.seg { display: inline-flex; background: var(--pend-bg); border-radius: 10px; padding: 3px; gap: 2px; }
.seg button { border: 0; background: transparent; padding: 6px 12px; border-radius: 8px; font-weight: 600; font-size: 13px; color: var(--muted); }
.seg button.on { background: var(--card); color: var(--text); box-shadow: var(--shadow); }

/* ── Order list ── */
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 12px; }
.toolbar input[type=search] { flex: 1; min-width: 180px; }
.olist { display: grid; gap: 10px; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); }
@media (max-width: 520px) { .olist { grid-template-columns: 1fr; } }
.ocard { display: block; padding: 13px 15px; text-decoration: none; color: inherit; transition: border-color .15s, transform .15s; }
.ocard:hover { border-color: var(--navy-2); transform: translateY(-1px); }
.ocard .party { font-weight: 700; font-size: 16px; }
.ocard .meta { color: var(--muted); font-size: 12.5px; margin-top: 2px; }
.progress { height: 6px; border-radius: 6px; background: var(--pend-bg); display: flex; overflow: hidden; margin: 10px 0 6px; }
.progress span { display: block; height: 100%; }
.progress .p-ok { background: #22c55e; } .progress .p-part { background: #f59e0b; } .progress .p-no { background: #f97316; }
.counts { display: flex; gap: 10px; font-size: 12.5px; color: var(--muted); flex-wrap: wrap; }
.counts b { color: var(--text); }
.empty { text-align: center; padding: 48px 16px; color: var(--muted); }
.empty .big { font-size: 40px; }

/* ── Order editor ── */
.ohead { display: grid; grid-template-columns: 2fr 1.3fr 1.1fr 1fr; gap: 10px; }
@media (max-width: 800px) { .ohead { grid-template-columns: 1fr 1fr; } .ohead .f-party { grid-column: 1 / -1; } }
label.f { display: grid; gap: 4px; font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .3px; }
label.f > * { text-transform: none; letter-spacing: 0; font-weight: 400; color: var(--text); font-size: 15px; }
.adder { display: grid; grid-template-columns: 1fr 120px 90px auto auto; gap: 8px; align-items: start; position: relative; }
@media (max-width: 700px) { .adder { grid-template-columns: 1fr 1fr 80px; } .adder .a-item { grid-column: 1 / -1; } .adder .a-btns { grid-column: 1 / -1; display: flex; gap: 8px; } .adder .a-btns .btn { flex: 1; } }
.a-btns { display: contents; }
.suggest {
  position: absolute; z-index: 30; left: 0; right: 0; top: 100%; margin-top: 4px; max-height: 330px; overflow: auto;
  background: var(--card); border: 1px solid var(--line); border-radius: 12px; box-shadow: 0 10px 30px rgba(0,0,0,.18);
}
.suggest button { display: flex; width: 100%; gap: 10px; align-items: baseline; padding: 9px 12px; border: 0; background: transparent; text-align: left; border-bottom: 1px solid var(--line); }
.suggest button:last-child { border-bottom: 0; }
.suggest button.act, .suggest button:hover { background: var(--navy-soft); }
.suggest .c { font-weight: 700; } .suggest .b { color: var(--muted); font-size: 13px; } .suggest .m { margin-left: auto; font-size: 13px; color: var(--muted); white-space: nowrap; }
.suggest .d { color: var(--muted); font-size: 12px; }

.lines { display: grid; gap: 8px; }
.ln { display: grid; grid-template-columns: 28px minmax(0, 1fr) 82px auto; gap: 10px; align-items: center; padding: 10px 12px; border-left: 4px solid transparent; }
.ln.s-ok { border-left-color: #22c55e; } .ln.s-no { border-left-color: #f97316; } .ln.s-part { border-left-color: #f59e0b; } .ln.s-pending { border-left-color: var(--line); }
.ln .num { color: var(--muted); font-size: 13px; text-align: center; }
.ln .it .code { font-weight: 700; font-size: 15.5px; word-break: break-word; }
.ln .it .sub { font-size: 12.5px; color: var(--muted); display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-top: 2px; }
.ln .it select.brand-sel { min-height: 28px; padding: 2px 6px; font-size: 12.5px; width: auto; max-width: 170px; border-radius: 7px; }
.ln .qty input { text-align: center; font-weight: 700; padding: 6px; min-height: 38px; }
.ln .qty .lbl { font-size: 10.5px; color: var(--muted); text-align: center; text-transform: uppercase; }
.checks { display: flex; gap: 6px; align-items: center; }
.ck { width: 44px; height: 40px; border-radius: 10px; border: 1px solid var(--line); background: var(--card); font-size: 17px; font-weight: 800; display: inline-flex; align-items: center; justify-content: center; }
.ck.ok.on { background: #16a34a; border-color: #16a34a; color: #fff; }
.ck.part.on { background: #f59e0b; border-color: #f59e0b; color: #111; }
.ck.no.on { background: #ea580c; border-color: #ea580c; color: #fff; }
.ck:not(.on):hover { border-color: var(--navy-2); }
.avail-in { width: 64px !important; min-height: 38px; text-align: center; padding: 4px; font-weight: 700; }
.ln .tools { display: flex; gap: 2px; }
@media (max-width: 700px) {
  .ln { grid-template-columns: 22px minmax(0, 1fr) 70px; row-gap: 8px; }
  .ln .checks { grid-column: 2 / -1; }
  .ln .checks .ck { flex: 1; width: auto; height: 44px; }
}
.foot-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 16px; }
.sticky-actions { position: sticky; bottom: calc(var(--bottom-h) + env(safe-area-inset-bottom) + 8px); z-index: 5; }
@media (min-width: 1000px) { .sticky-actions { bottom: 12px; } }

/* ── Check view ── */
.grp-title { display: flex; align-items: center; gap: 8px; padding: 10px 14px; border-bottom: 1px solid var(--line); font-weight: 700; }
.grp-title a { color: inherit; text-decoration: none; }
.grp-body .ln { border-radius: 0; border-bottom: 1px solid var(--line); }
.grp-body .ln:last-child { border-bottom: 0; }
.for { font-size: 12.5px; color: var(--muted); }

/* ── To order ── */
.sup-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; padding: 12px 14px; border-bottom: 1px solid var(--line); }
.sup-head h3 { margin: 0; font-size: 17px; margin-right: auto; }
table.t { width: 100%; border-collapse: collapse; }
table.t th, table.t td { padding: 8px 10px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; }
table.t th { font-size: 12px; text-transform: uppercase; letter-spacing: .3px; color: var(--muted); font-weight: 700; }
table.t tr:last-child td { border-bottom: 0; }
table.t td.n, table.t th.n { text-align: right; }
table.t input[type=number] { width: 80px; min-height: 34px; padding: 4px 6px; text-align: center; font-weight: 700; }
table.t input[type=checkbox] { width: 18px; height: 18px; min-height: 0; }
.tbl-wrap { overflow-x: auto; }
.opts { display: flex; gap: 14px; flex-wrap: wrap; align-items: center; font-size: 14px; }
.opts label { display: inline-flex; gap: 6px; align-items: center; }
.opts input { width: 18px; height: 18px; min-height: 0; }

/* ── Settings ── */
.set-grid { display: grid; gap: 14px; grid-template-columns: repeat(auto-fill, minmax(330px, 1fr)); }
@media (max-width: 520px) { .set-grid { grid-template-columns: 1fr; } }
.set-grid h2 { font-size: 16px; margin: 0 0 4px; }
.set-grid p { margin: 4px 0 10px; }
.btn-list { display: grid; gap: 8px; }
.btn-list .btn { justify-content: flex-start; }
.code-box { font-family: ui-monospace, Consolas, monospace; font-size: 16px; font-weight: 700; letter-spacing: 1px; padding: 10px 12px; background: var(--navy-soft); border-radius: 10px; word-break: break-all; }

/* ── Modal & toast ── */
.modal-bg { position: fixed; inset: 0; z-index: 50; background: rgba(10,16,26,.5); display: flex; align-items: flex-end; justify-content: center; }
@media (min-width: 700px) { .modal-bg { align-items: center; } }
.modal { width: 100%; max-width: 720px; max-height: 92vh; overflow: auto; background: var(--card); border-radius: 18px 18px 0 0; padding: 18px; padding-bottom: calc(18px + env(safe-area-inset-bottom)); }
@media (min-width: 700px) { .modal { border-radius: 18px; } }
.modal h2 { margin: 0 0 10px; font-size: 19px; }
.modal .actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 14px; flex-wrap: wrap; }
#toast { position: fixed; left: 50%; bottom: calc(var(--bottom-h) + 18px + env(safe-area-inset-bottom)); transform: translateX(-50%) translateY(20px); opacity: 0; transition: .2s; z-index: 60;
  background: #111827; color: #fff; padding: 10px 16px; border-radius: 12px; font-size: 14px; pointer-events: none; max-width: 90vw; text-align: center; }
#toast.show { opacity: 1; transform: translateX(-50%); }
@media (min-width: 1000px) { #toast { bottom: 24px; } }
.preview-tbl td { font-size: 14px; }
.warn { background: var(--part-bg); color: var(--part); padding: 8px 12px; border-radius: 10px; font-size: 13.5px; }
.info { background: var(--blue-bg); color: var(--blue); padding: 8px 12px; border-radius: 10px; font-size: 13.5px; }

/* ── Print ── */
#print { display: none; }
@media print {
  body { background: #fff; color: #000; padding: 0; }
  body > *:not(#print) { display: none !important; }
  #print { display: block; font: 12pt/1.4 Arial, sans-serif; }
  #print h1 { font-size: 16pt; margin: 0 0 4px; }
  #print table { width: 100%; border-collapse: collapse; margin: 10px 0 18px; }
  #print th, #print td { border: 1px solid #999; padding: 4px 6px; text-align: left; }
  #print .pg { page-break-after: always; }
}

/* ── Mobile refinements ── */
.toolbar .seg { overflow-x: auto; max-width: 100%; scrollbar-width: none; }
.toolbar .seg::-webkit-scrollbar { display: none; }
@media (max-width: 999px) {
  .sticky-actions { position: static; }
  .tabs a { white-space: nowrap; }
}
@media (max-width: 600px) {
  .sup-head h3 { width: 100%; }
  .sup-head .btn { flex: 1; }
  table.t.resp thead { display: none; }
  table.t.resp tr { display: grid; grid-template-columns: 30px minmax(0, 1fr) auto; gap: 2px 10px; padding: 10px 12px; border-bottom: 1px solid var(--line); align-items: center; }
  table.t.resp tr:last-child { border-bottom: 0; }
  table.t.resp td { padding: 0; border: 0; }
  table.t.resp td.c-sel { grid-column: 1; grid-row: 1 / span 2; }
  table.t.resp td.c-qty { grid-column: 3; grid-row: 1 / span 2; }
  table.t.resp td.c-qty input { width: 70px; }
  table.t.resp td.c-brand, table.t.resp td.c-mrp { display: inline; }
  table.t.resp td.c-item { grid-column: 2; grid-row: 1; }
  table.t.resp td.c-brand { grid-column: 2; grid-row: 2; font-size: 13px; color: var(--muted); }
  table.t.resp td.c-party { grid-column: 2 / -1; grid-row: 3; font-size: 12.5px; color: var(--muted); }
  table.t.resp td.c-mrp { display: none; }
  .foot-actions .btn { flex: 1 1 auto; }
  .page-head .btn { flex: 1 1 auto; }
}

/* ── Photo order ── */
.photo-strip { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 10px; }
.photo-strip img { height: 110px; max-width: 100%; border-radius: 10px; border: 1px solid var(--line); object-fit: cover; cursor: zoom-in; background: #fff; }
.photo-strip img.big { height: auto; width: 100%; object-fit: contain; cursor: zoom-out; }
.photo-strip .ph { position: relative; }
.photo-strip .ph .icon-btn { position: absolute; top: 4px; right: 4px; background: rgba(0,0,0,.6); color: #fff; padding: 2px 7px; }
label.btn input[hidden] { display: none; }
