:root {
  --bg: #f4f5f7; --surface: #fff; --soft: #f1f2f4; --border: #d9dce1; --text: #14181f; --muted: #5f6773;
  --primary: #1b6ef3; --on-primary: #fff; --buyer: #378add; --factory: #ba7517; --danger: #c0372b; --ok: #2e7d32;
  --radius: 8px; color-scheme: only light;
}
*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", "Noto Sans Arabic", "PingFang SC", "Microsoft YaHei", sans-serif; }
body.noscroll { overflow: hidden; }
button, input, textarea { font: inherit; color: inherit; }
.muted { color: var(--muted); font-size: 13px; }
.err { color: var(--danger); }
.page { min-height: 100vh; padding: 16px; }
.wrap { max-width: 1500px; margin: 0 auto; display: grid; gap: 14px; }
.top { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 10px; }
.top h1 { margin: 0; font-size: 20px; }
.tools { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.grow { flex: 1 1 0; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 36px; padding: 6px 12px; font-weight: 600; font-size: 13px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); cursor: pointer; text-decoration: none; white-space: nowrap; color: var(--text); }
.btn:hover:not(:disabled) { background: var(--soft); }
.btn:focus-visible, .ibtn:focus-visible, .mediabtn:focus-visible, .notebtn:focus-visible, .inp:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
.btn.primary { background: var(--primary); color: var(--on-primary); border-color: var(--primary); }
.btn.danger { color: var(--danger); }
.ibtn { width: 32px; height: 32px; display: inline-grid; place-items: center; border: 0; background: transparent; border-radius: var(--radius); cursor: pointer; color: var(--muted); font-size: 18px; }
.ibtn:hover { background: var(--soft); color: var(--text); }
.ibtn.danger:hover { color: var(--danger); }
.ibtn:disabled { opacity: .35; cursor: default; }

.lang { display: inline-flex; border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.lang button { border: 0; background: var(--surface); padding: 6px 12px; cursor: pointer; font-size: 13px; }
.lang button[aria-pressed="true"] { background: var(--primary); color: var(--on-primary); }

.gate { max-width: 400px; margin: 10vh auto 0; display: grid; gap: 12px; padding: 20px; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; }
.gate h1 { margin: 0; font-size: 20px; }
.inp.boxed, .newform .inp { padding: 10px 12px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); width: 100%; }
.newform { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 12px; max-width: 560px; }
.newform[hidden], [hidden] { display: none !important; }
.cards { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(290px, 1fr)); }
.card { display: grid; gap: 8px; padding: 14px; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; color: inherit; text-decoration: none; align-content: start; }
.card:hover { border-color: var(--primary); }
.card h3 { margin: 0; font-size: 16px; }
.card dl { margin: 0; display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 6px; }
.card dt { font-size: 11px; color: var(--muted); }
.card dd { margin: 0; font-weight: 700; font-variant-numeric: tabular-nums; }
.badge { justify-self: start; font-size: 12px; padding: 2px 8px; border-radius: 999px; background: color-mix(in srgb, var(--factory) 18%, transparent); }

/* sheet chrome */
.bar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; }
.status { font-size: 13px; color: var(--muted); }
.status[data-state="offline"] { color: var(--danger); }
.legend { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.legend i { width: 12px; height: 12px; border-radius: 3px; display: inline-block; margin-inline-start: 6px; }
i[data-side="owner"] { background: var(--buyer); } i[data-side="factory"] { background: var(--factory); }
.banner { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px; padding: 10px 14px; border-radius: var(--radius); background: color-mix(in srgb, var(--factory) 14%, var(--surface)); border: 1px solid color-mix(in srgb, var(--factory) 45%, var(--border)); }
.banner-soft { background: var(--soft); border-color: var(--border); }

.head, .terms { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 8px; }
.terms h3, .terms .muted { grid-column: 1 / -1; margin: 4px 0 0; }
.field { display: flex; flex-direction: column; gap: 3px; padding: 8px 10px; font-size: 12px; color: var(--muted); background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); border-inline-start-width: 3px; }
.field.wide { grid-column: 1 / -1; }
.field .inp { border: 0; outline: none; background: transparent; padding: 2px 0; width: 100%; resize: vertical; font-size: 14px; color: var(--text); }
.field:focus-within { border-color: var(--primary); }
.field[data-locked="1"] { background: var(--soft); }
.field[data-side="owner"] { border-inline-start-color: var(--buyer); } .field[data-side="factory"] { border-inline-start-color: var(--factory); }
.field[data-fresh="1"][data-side="owner"] { background: color-mix(in srgb, var(--buyer) 12%, var(--surface)); }
.field[data-fresh="1"][data-side="factory"] { background: color-mix(in srgb, var(--factory) 15%, var(--surface)); }

.scroll { overflow: auto; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); max-height: 74vh; }
.grid { border-collapse: separate; border-spacing: 0; width: max-content; min-width: 100%; font-size: 13px; }
.th { position: sticky; top: 0; z-index: 3; background: var(--soft); padding: 8px; text-align: start; font-size: 12px; font-weight: 700; color: var(--muted); border-bottom: 1px solid var(--border); white-space: nowrap; }
.th[data-calc="1"] { font-style: italic; }
.td { padding: 0; border-bottom: 1px solid var(--border); border-inline-end: 1px solid var(--border); border-inline-start: 3px solid transparent; background: var(--surface); vertical-align: top; }
.td[data-side="owner"] { border-inline-start-color: var(--buyer); } .td[data-side="factory"] { border-inline-start-color: var(--factory); }
.td[data-fresh="1"][data-side="owner"] { background: color-mix(in srgb, var(--buyer) 13%, var(--surface)); }
.td[data-fresh="1"][data-side="factory"] { background: color-mix(in srgb, var(--factory) 16%, var(--surface)); }
.td:focus-within { outline: 2px solid var(--primary); outline-offset: -2px; background: var(--surface); }
.inp { display: block; width: 100%; min-height: 38px; padding: 9px 8px; border: 0; outline: none; background: transparent; resize: none; line-height: 1.35; }
textarea.inp { min-height: 54px; }
.td.calc { background: var(--soft); padding: 9px 8px; text-align: end; font-variant-numeric: tabular-nums; direction: ltr; font-weight: 600; border-inline-start-color: transparent; }
.td.n { text-align: center; color: var(--muted); font-size: 12px; padding: 11px 4px 0; min-width: 40px; background: var(--soft); border-inline-start: 0; }
.th.n { min-width: 40px; text-align: center; }
.sticky1 { position: sticky; inset-inline-start: 0; z-index: 2; }
.sticky2 { position: sticky; inset-inline-start: 40px; z-index: 2; }
.th.sticky1, .th.sticky2 { z-index: 4; }
.td.media { min-width: 74px; width: 74px; border-inline-start: 0; background: var(--soft); }
.mediabtn { position: relative; display: grid; place-items: center; width: 74px; height: 74px; padding: 0; border: 0; background: var(--soft); cursor: pointer; color: var(--muted); font-size: 20px; }
.mediabtn img { width: 100%; height: 100%; object-fit: cover; display: block; }
.mediabtn b, .notebtn b { position: absolute; inset-block-end: 3px; inset-inline-end: 3px; min-width: 18px; height: 18px; padding: 0 4px; border-radius: 9px; background: var(--primary); color: var(--on-primary); font-size: 11px; display: grid; place-items: center; }
.playmark, .vidchip { background: rgb(0 0 0 / .55); color: #fff; border-radius: 4px; padding: 0 5px; font-size: 11px; }
.playmark { position: absolute; inset-block-start: 3px; inset-inline-start: 3px; }
.td.notes { min-width: 54px; text-align: center; border-inline-start: 0; }
.notebtn { position: relative; width: 44px; height: 44px; border: 0; background: transparent; cursor: pointer; border-radius: var(--radius); font-size: 16px; }
.notebtn:hover { background: var(--soft); }
.notebtn[data-fresh="1"] { background: color-mix(in srgb, var(--factory) 20%, transparent); }
.td.actions { white-space: nowrap; padding: 4px; min-width: 76px; border-inline-start: 0; }
.total td { background: var(--soft); padding: 9px 8px; border-top: 2px solid var(--border); font-variant-numeric: tabular-nums; }
.total td.num { direction: ltr; text-align: end; white-space: nowrap; }
.addbar { display: flex; flex-wrap: wrap; gap: 8px; }

.activity { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 14px; }
.activity summary { cursor: pointer; font-weight: 700; }
.history { list-style: none; margin: 10px 0 0; padding: 0; display: grid; gap: 6px; max-height: 320px; overflow: auto; }
.history li { padding: 6px 10px; border-inline-start: 3px solid var(--border); background: var(--soft); border-radius: 4px; overflow-wrap: anywhere; font-size: 13px; }
.history li[data-side="owner"] { border-inline-start-color: var(--buyer); } .history li[data-side="factory"] { border-inline-start-color: var(--factory); }
.history time { display: block; font-size: 11px; color: var(--muted); margin-top: 2px; }
.history em { font-style: normal; color: var(--muted); }

/* drawer */
.drawer-layer { position: fixed; inset: 0; z-index: 1200; display: flex; justify-content: flex-end; }
.drawer-backdrop { position: absolute; inset: 0; border: 0; background: rgb(20 24 31 / .45); cursor: default; }
.drawer { position: relative; width: min(560px, 100vw); height: 100%; background: var(--surface); display: flex; flex-direction: column; box-shadow: 0 0 40px rgb(0 0 0 / .25); }
.drawer-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 12px 14px; border-bottom: 1px solid var(--border); }
.dtitle { display: flex; flex-direction: column; min-width: 0; }
.dtitle span { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.drawer-body { flex: 1; overflow: auto; padding: 14px; display: grid; gap: 20px; align-content: start; padding-bottom: max(14px, env(safe-area-inset-bottom)); }
.drawer h3 { margin: 0 0 8px; font-size: 14px; }
.count { display: inline-block; min-width: 20px; padding: 0 6px; border-radius: 10px; background: var(--soft); font-size: 12px; color: var(--muted); text-align: center; }
.viewer { position: relative; }
.viewer-main { display: grid; place-items: center; background: #0e1116; border-radius: var(--radius); overflow: hidden; aspect-ratio: 4 / 3; }
.viewer-main img, .viewer-main video { max-width: 100%; max-height: 100%; object-fit: contain; }
.vnav { position: absolute; top: 40%; width: 36px; height: 36px; border-radius: 50%; border: 0; background: rgb(255 255 255 / .85); cursor: pointer; font-size: 22px; line-height: 1; }
.vprev { inset-inline-start: 8px; } .vnext { inset-inline-end: 8px; }
html[dir="rtl"] .vnav { transform: scaleX(-1); }
.viewer-meta { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; margin-top: 6px; font-size: 12px; color: var(--muted); }
.viewer-meta [data-side="owner"] { color: var(--buyer); font-weight: 700; } .viewer-meta [data-side="factory"] { color: var(--factory); font-weight: 700; }
.viewer-meta a { color: var(--primary); }
.linkdanger { margin-inline-start: auto; border: 0; background: none; color: var(--danger); cursor: pointer; font-size: 12px; }
.thumbs { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.thumbs button { position: relative; width: 60px; height: 60px; padding: 0; border: 2px solid transparent; border-radius: 6px; overflow: hidden; background: var(--soft); cursor: pointer; display: grid; place-items: center; }
.thumbs button.on { border-color: var(--primary); }
.thumbs button[data-side="owner"]:not(.on) { border-color: color-mix(in srgb, var(--buyer) 40%, transparent); }
.thumbs button[data-side="factory"]:not(.on) { border-color: color-mix(in srgb, var(--factory) 50%, transparent); }
.thumbs img { width: 100%; height: 100%; object-fit: cover; display: block; }
.drop { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; margin-top: 10px; padding: 12px; border: 2px dashed var(--border); border-radius: var(--radius); }
.drop[data-on="1"] { border-color: var(--primary); background: var(--soft); }
.uploads { display: grid; gap: 8px; margin-top: 8px; }
.up { display: grid; gap: 4px; padding: 8px 10px; background: var(--soft); border-radius: var(--radius); font-size: 13px; }
.up.failed { color: var(--danger); }
.up progress { width: 100%; height: 8px; }
.extras { margin-top: 10px; }
.thread { display: grid; gap: 8px; max-height: 300px; overflow: auto; }
.note { padding: 8px 10px; border-radius: var(--radius); background: var(--soft); border-inline-start: 3px solid var(--border); }
.note[data-side="owner"] { border-inline-start-color: var(--buyer); } .note[data-side="factory"] { border-inline-start-color: var(--factory); }
.nhead { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 6px; font-size: 12px; color: var(--muted); }
.nhead strong { color: var(--text); }
.note p { margin: 4px 0 0; white-space: pre-wrap; overflow-wrap: anywhere; }
.composer { display: grid; gap: 8px; margin-top: 8px; }
.composer textarea { padding: 8px 10px; border: 1px solid var(--border); border-radius: var(--radius); resize: vertical; width: 100%; background: var(--surface); }
.composer .btn { justify-self: end; }

#toasts { position: fixed; inset-block-end: 16px; inset-inline-start: 50%; transform: translateX(-50%); z-index: 3000; display: grid; gap: 8px; pointer-events: none; }
.toast { padding: 10px 14px; border-radius: var(--radius); background: #14181f; color: #fff; font-size: 13px; }
.toast-ok { background: var(--ok); }

@media (max-width: 640px) {
  .page { padding: 10px; }
  .head, .terms { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .field { padding: 6px 8px; }
  .field .inp { font-size: 13px; min-height: 30px; padding: 4px 0; }
  .scroll { max-height: none; }
  .mediabtn { width: 62px; height: 62px; } .td.media { min-width: 62px; width: 62px; }
}
@media (prefers-reduced-motion: reduce) { * { scroll-behavior: auto !important; } }

.td.manual .inp { text-align: end; direction: ltr; font-weight: 600; background: transparent; }
.td.manual[data-auto="1"] .inp { font-style: italic; font-weight: 400; color: var(--muted); }

/* The grid is laid out like the Excel file (columns left to right) whatever the interface language. */
.scroll, .grid { direction: ltr; }
.wrap > *, .wrap > div > * { min-width: 0; }

/* Compact order header: a couple of slim rows instead of one card per line. */
.head { grid-template-columns: repeat(auto-fit, minmax(128px, 1fr)); gap: 6px; }
.head .field { padding: 3px 9px 4px; gap: 0; border-radius: 6px; border-inline-start-width: 3px; }
.head .field.wide { grid-column: auto; }
.head .field span { font-size: 11px; line-height: 1.3; }
.head .field .inp { font-size: 13px; min-height: 24px; padding: 1px 0; text-overflow: ellipsis; }
.head .field[data-field="seller_name"] { grid-column: span 2; }
.head .field[data-field="seller_address"] { grid-column: span 3; }
.head .field[data-field="title"] { grid-column: span 2; }
.terms { gap: 6px; }
.terms .field { padding: 4px 9px 5px; }
.terms .field .inp { font-size: 13px; min-height: 26px; }
.terms .field textarea.inp { min-height: 54px; }
@media (max-width: 640px) {
  .head .field[data-field="seller_name"], .head .field[data-field="seller_address"], .head .field[data-field="title"] { grid-column: 1 / -1; }
}
