:root {
  --bg: #f4f5f7; --panel: #fff; --line: #e3e6eb; --text: #1d2330; --muted: #6b7383;
  --accent: #2f6fed; --accent-soft: #e8f0ff; --danger: #d64545; --ok: #1f9d63; --warn: #c98a13;
  --radius: 10px; --shadow: 0 1px 2px rgba(20, 30, 50, .06), 0 4px 16px rgba(20, 30, 50, .05);
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); font: 14px/1.55 -apple-system, "Segoe UI", "Microsoft YaHei", "PingFang SC", sans-serif; }
[hidden] { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }
input, select, textarea { width: 100%; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; background: #fff; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
input[readonly], textarea[readonly], select:disabled { background: #f6f7f9; color: var(--muted); }
textarea { min-height: 72px; resize: vertical; }
label { display: grid; gap: 4px; color: var(--muted); font-size: 13px; }
label > span.req::after { content: " *"; color: var(--danger); }
button { cursor: pointer; border: 1px solid var(--line); background: #fff; border-radius: 8px; padding: 7px 14px; white-space: nowrap; }
button:hover { border-color: #c5cad3; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.primary:hover { filter: brightness(1.06); }
button.danger { color: var(--danger); }
button.ghost { border-color: transparent; background: transparent; }
button.small { padding: 3px 9px; font-size: 12px; }
button:disabled { opacity: .5; cursor: default; }
.muted { color: var(--muted); }
.error { color: var(--danger); min-height: 1em; margin: 0; }

.brand { display: flex; gap: 10px; align-items: center; font-weight: 600; }
.brand b { width: 34px; height: 34px; border-radius: 9px; background: var(--accent); color: #fff; display: grid; place-items: center; font-size: 17px; }
.brand small { display: block; font-weight: 400; color: var(--muted); font-size: 12px; }

.auth { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.auth-card { width: min(380px, 100%); background: var(--panel); padding: 28px; border-radius: 14px; box-shadow: var(--shadow); display: grid; gap: 14px; }
.auth-card h2 { margin: 8px 0 0; }
.rate-box { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; padding: 12px 14px; margin-bottom: 16px; }
.rate-box input { width: 110px; }
.lang-switch { display: flex; gap: 2px; justify-content: flex-end; }
.lang-switch button { border: 0; background: transparent; padding: 2px 8px; font-size: 12px; color: var(--muted); border-radius: 6px; }
.lang-switch button.on { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
#shell-lang { justify-content: flex-start; }
.auth-card p { margin: 0; }

#shell { display: grid; grid-template-columns: 220px 1fr; min-height: 100vh; }
aside { background: var(--panel); border-right: 1px solid var(--line); padding: 18px 12px; display: flex; flex-direction: column; gap: 18px; position: sticky; top: 0; height: 100vh; }
nav { display: grid; gap: 2px; flex: 1; align-content: start; }
nav a { padding: 8px 12px; border-radius: 8px; color: var(--text); text-decoration: none; }
nav a:hover { background: var(--bg); }
nav a.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
nav .group { font-size: 12px; color: var(--muted); padding: 12px 12px 4px; }
.me { display: flex; align-items: center; gap: 4px; border-top: 1px solid var(--line); padding-top: 12px; }
.me > div { flex: 1; min-width: 0; }
.me small { display: block; color: var(--muted); font-size: 12px; }
main { min-width: 0; }
header { display: flex; align-items: center; gap: 12px; padding: 16px 24px; background: var(--panel); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 5; flex-wrap: wrap; }
header h1 { font-size: 18px; margin: 0; flex: 1; }
#menu { display: none; font-size: 18px; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }
.content { padding: 20px 24px 40px; }

.toolbar { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 16px; align-items: center; }
.toolbar input, .toolbar select { width: auto; min-width: 140px; }
.toolbar .grow { flex: 1; min-width: 200px; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg button { border: 0; border-radius: 0; }
.seg button.on { background: var(--accent-soft); color: var(--accent); }

.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 14px; }
.product { overflow: hidden; cursor: pointer; display: flex; flex-direction: column; transition: transform .12s; }
.product:hover { transform: translateY(-2px); }
.thumb { aspect-ratio: 1; background: #eef0f3 center/cover no-repeat; display: grid; place-items: center; color: #a3aab6; font-size: 13px; }
.product .body { padding: 10px 12px 12px; display: grid; gap: 4px; }
.product .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chips { display: flex; flex-wrap: wrap; gap: 4px; }
.chip { font-size: 12px; padding: 1px 7px; border-radius: 99px; background: var(--bg); color: var(--muted); }
.chip b { color: var(--text); font-weight: 600; }
.chip.cost b { color: var(--warn); }
.chip.sell b { color: var(--ok); }
.chip.private { background: #f3e8ff; color: #7c3aed; }
.quote { display: flex; flex-wrap: wrap; gap: 12px 24px; padding: 12px 14px; background: #f5f9ff; border: 1px solid #d6e4ff; border-radius: 8px; }
.quote small { display: block; color: var(--muted); font-size: 12px; }
.quote b { font-size: 16px; font-variant-numeric: tabular-nums; }
.quote p { width: 100%; margin: 0; white-space: pre-wrap; }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; background: var(--panel); }
th, td { text-align: left; padding: 9px 12px; border-bottom: 1px solid var(--line); vertical-align: middle; }
th { font-weight: 600; color: var(--muted); font-size: 12px; background: #fafbfc; white-space: nowrap; }
tbody tr:hover { background: #fafbfd; }
tr.click { cursor: pointer; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
td img.mini { width: 40px; height: 40px; object-fit: cover; border-radius: 6px; display: block; background: #eef0f3; }
.empty { padding: 48px 16px; text-align: center; color: var(--muted); }

.badge { display: inline-block; padding: 1px 8px; border-radius: 99px; font-size: 12px; background: var(--bg); white-space: nowrap; }
.badge.submitted { background: #fff0e0; color: #c25e00; }
nav a .count { float: right; min-width: 20px; padding: 0 6px; border-radius: 99px; background: #e8590c; color: #fff; font-size: 12px; text-align: center; font-weight: 600; }
.badge.draft { background: #eef0f3; color: var(--muted); }
.badge.ordered { background: var(--accent-soft); color: var(--accent); }
.badge.shipped { background: #fff4df; color: var(--warn); }
.badge.intl { background: #f3e8ff; color: #7c3aed; }
.logistics { padding: 14px 16px; }
.logistics .form { margin-bottom: 12px; }
.ship-photos-title { font-weight: 600; margin: 4px 0 8px; font-size: 13px; }
.badge.received { background: #e6f7ef; color: var(--ok); }
.badge.done { background: #dff3ea; color: #13734a; }
.badge.cancelled { background: #fdeaea; color: var(--danger); }

.modal { position: fixed; inset: 0; background: rgba(20, 25, 35, .45); display: grid; place-items: start center; padding: 40px 16px; overflow-y: auto; z-index: 20; }
.modal-card { background: var(--panel); border-radius: 14px; width: min(760px, 100%); box-shadow: 0 20px 60px rgba(0, 0, 0, .2); }
.modal-card.wide { width: min(1100px, 100%); }
.modal-head { display: flex; align-items: center; padding: 16px 20px; border-bottom: 1px solid var(--line); }
.modal-head h2 { margin: 0; font-size: 17px; flex: 1; }
.modal-head button { font-size: 20px; padding: 0 8px; }
#modal-body { padding: 20px; }
.form { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; }
.form .full { grid-column: 1 / -1; }
.form-foot { display: flex; gap: 8px; justify-content: flex-end; margin-top: 20px; flex-wrap: wrap; }
.form-foot .left { margin-right: auto; }
.section-title { font-weight: 600; margin: 20px 0 10px; display: flex; align-items: center; gap: 8px; }
.section-title:first-child { margin-top: 0; }

.photos { display: flex; flex-wrap: wrap; gap: 10px; }
.photo { position: relative; width: 104px; height: 104px; border-radius: 8px; overflow: hidden; background: #eef0f3 center/cover; cursor: zoom-in; border: 2px solid transparent; }
.photo.cover { border-color: var(--accent); }
.photo .tag { position: absolute; left: 4px; top: 4px; background: var(--accent); color: #fff; font-size: 11px; padding: 0 6px; border-radius: 4px; }
.photo .ops { position: absolute; inset: auto 0 0 0; display: flex; justify-content: space-between; background: rgba(0, 0, 0, .55); }
.photo .ops button { color: #fff; border: 0; background: transparent; font-size: 11px; padding: 3px 6px; }
.photo-add { width: 104px; height: 104px; border: 1px dashed #b9c0cc; border-radius: 8px; display: grid; place-items: center; color: var(--muted); cursor: pointer; text-align: center; font-size: 12px; }
.photo-add input { display: none; }
.detail { display: grid; grid-template-columns: 320px 1fr; gap: 20px; }
.gallery .main { aspect-ratio: 1; border-radius: 10px; background: #eef0f3 center/contain no-repeat; cursor: zoom-in; display: grid; place-items: center; color: #a3aab6; }
.gallery .strip { display: flex; gap: 6px; margin-top: 8px; flex-wrap: wrap; }
.gallery .strip span { width: 52px; height: 52px; border-radius: 6px; background: #eef0f3 center/cover; cursor: pointer; border: 2px solid transparent; }
.gallery .strip span.on { border-color: var(--accent); }
dl.kv { display: grid; grid-template-columns: 110px 1fr; gap: 8px 12px; margin: 0; }
dl.kv dt { color: var(--muted); }
dl.kv dd { margin: 0; white-space: pre-wrap; word-break: break-word; }
.viewer { position: fixed; inset: 0; background: rgba(0, 0, 0, .85); z-index: 40; display: grid; place-items: center; cursor: zoom-out; padding: 20px; }
.viewer img { max-width: 100%; max-height: 100%; }

.lines td input { min-width: 80px; }
.lines td.prod { min-width: 220px; }
.lines .choices { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; font-size: 13px; }
.lines .choices select, .lines .choices input { width: auto; min-width: 120px; padding: 4px 8px; }
.lines .choices select.need { border-color: #f0a35e; background: #fff8f0; }
.lines .choice { display: inline-flex; align-items: center; gap: 4px; color: var(--muted); font-size: 13px; }
b.pick { color: #c25e00; font-weight: 600; }
td.items { min-width: 200px; font-size: 13px; line-height: 1.6; }
td.grand, .grand { font-weight: 700; color: var(--accent); }
.list-total { margin-top: 12px; }
tr.log-fail td { background: #fff5f5; }
tr.log-fail td:nth-child(5) { color: var(--danger); font-weight: 600; }
.statement-total { justify-content: flex-start; gap: 32px; margin-top: 0; }
.statement-total b.big { font-size: 20px; }
td.latest { min-width: 160px; font-size: 13px; }
tr.unseen td:first-child { box-shadow: inset 3px 0 0 #e8590c; }
.new-dot { display: inline-block; padding: 0 7px; border-radius: 99px; background: #e8590c; color: #fff; font-size: 11px; font-weight: 600; vertical-align: middle; }
.timeline { border-left: 2px solid var(--line); margin-left: 6px; padding-left: 14px; display: grid; gap: 10px; }
.timeline .ev { position: relative; display: flex; flex-wrap: wrap; gap: 4px 12px; align-items: baseline; font-size: 14px; }
.timeline .ev::before { content: ""; position: absolute; left: -20px; top: 6px; width: 10px; height: 10px; border-radius: 50%; background: var(--accent); }
.timeline .ev:not(:first-child)::before { background: #c5cad3; }
.timeline .when { color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }
.lines .prod-cell { display: flex; gap: 8px; align-items: center; }
.summary { display: flex; gap: 20px; justify-content: flex-end; flex-wrap: wrap; padding: 12px; background: #fafbfc; border-radius: 8px; margin-top: 10px; }
.summary div { text-align: right; }
.summary small { display: block; color: var(--muted); }
.summary b { font-size: 16px; font-variant-numeric: tabular-nums; }
.pos { color: var(--ok); } .neg { color: var(--danger); }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 16px; flex-wrap: wrap; }
.tabs button { border: 0; border-bottom: 2px solid transparent; border-radius: 0; background: none; padding: 8px 14px; }
.tabs button.on { border-bottom-color: var(--accent); color: var(--accent); font-weight: 600; }
.checks { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 8px; }
.checks label, label.inline { display: flex; align-items: center; gap: 6px; color: var(--text); font-size: 14px; }
.checks input, label.inline input { width: auto; }
.matrix td { padding: 6px 10px; }
.matrix .seg button { padding: 3px 10px; font-size: 12px; }
.matrix .seg button.on.none { background: #f1f2f4; color: var(--muted); }
.matrix .seg button.on.edit { background: #e6f7ef; color: var(--ok); }

#toast { position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%); background: #1d2330; color: #fff; padding: 10px 18px; border-radius: 8px; z-index: 50; box-shadow: var(--shadow); max-width: calc(100% - 32px); }
#toast.bad { background: var(--danger); }

@media (max-width: 860px) {
  #shell { grid-template-columns: 1fr; }
  aside { position: fixed; z-index: 15; left: 0; width: 240px; transform: translateX(-100%); transition: transform .2s; box-shadow: var(--shadow); }
  aside.open { transform: none; }
  #menu { display: block; }
  header { padding: 12px 16px; }
  .content { padding: 16px; }
  .form, .detail { grid-template-columns: 1fr; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
  .toolbar input, .toolbar select { flex: 1; min-width: 0; }
  dl.kv { grid-template-columns: 96px 1fr; }

  /* 16px controls stop iPhone from zooming into inputs */
  input, select, textarea { font-size: 16px; }
  button { min-height: 40px; }
  button.small { min-height: 32px; }
  header { gap: 8px; }
  header h1 { font-size: 17px; }
  .grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .product .body { padding: 8px 10px 10px; }
  .chip { font-size: 11px; }

  /* full-screen dialogs with the action buttons pinned to the bottom */
  .modal { padding: 0; }
  .modal-card, .modal-card.wide { width: 100%; min-height: 100%; border-radius: 0; box-shadow: none; }
  .modal-head { position: sticky; top: 0; z-index: 3; background: var(--panel); padding: 12px 16px; }
  #modal-body { padding: 16px; }
  .form-foot { position: sticky; bottom: 0; z-index: 3; background: var(--panel); border-top: 1px solid var(--line);
    margin: 16px -16px -16px; padding: 10px 16px calc(10px + env(safe-area-inset-bottom)); }
  .form-foot button { flex: 1; }
  .form-foot .left { margin-right: 0; flex: 0 0 auto; }
  .gallery .main { width: 100%; }
  .summary { justify-content: space-between; }

  /* tables become stacked cards: one card per row, "label: value" per cell */
  .table-wrap { overflow: visible; }
  table.stack, table.stack tbody, table.stack tr, table.stack td { display: block; width: 100%; }
  table.stack thead { display: none; }
  table.stack tr { padding: 10px 14px; border-bottom: 8px solid var(--bg); }
  table.stack td { border: 0; padding: 4px 0; display: flex; justify-content: space-between; align-items: center; gap: 12px; text-align: right; }
  table.stack td::before { content: attr(data-label); color: var(--muted); font-size: 13px; text-align: left; flex-shrink: 0; }
  table.stack td[data-label=""]::before { display: none; }
  table.stack td:empty { display: none; }
  table.stack td.prod, table.stack td.items { display: block; text-align: left; }
  table.stack td.prod::before, table.stack td.items::before { display: block; margin-bottom: 4px; }
  table.stack td.empty { justify-content: center; }
  table.stack td input { max-width: 150px; }
  .lines .choices select, .lines .choices input { min-width: 0; flex: 1; }
  .lines .choice { width: 100%; }

  /* customer bottom tab bar */
  body.is-customer #tabbar { display: flex; }
  body.is-customer .content { padding-bottom: calc(84px + env(safe-area-inset-bottom)); }
  body.is-customer #menu { display: none; }
  body.is-customer #toast { bottom: calc(80px + env(safe-area-inset-bottom)); }
}
#tabbar { display: none; position: fixed; left: 0; right: 0; bottom: 0; z-index: 12; background: var(--panel); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom); box-shadow: 0 -2px 12px rgba(20, 30, 50, .06); }
#tabbar a { flex: 1; position: relative; display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 8px 4px 6px;
  color: var(--muted); text-decoration: none; font-size: 11px; line-height: 1.2; text-align: center; }
#tabbar a .ico { font-size: 20px; line-height: 1; }
#tabbar a.active { color: var(--accent); font-weight: 600; }
#tabbar a .count { position: absolute; top: 4px; left: calc(50% + 6px); min-width: 18px; padding: 0 5px; border-radius: 99px; background: #e8590c; color: #fff; font-size: 11px; }
