:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --surface-2: #efede7;
  --ink: #1d2320;
  --muted: #69716c;
  --line: #dedbd2;
  --brand: #1f6f5c;
  --brand-ink: #ffffff;
  --brand-soft: #e1efe9;
  --in: #1f7a4d;
  --in-soft: #e2f3e9;
  --out: #b4441f;
  --out-soft: #fbe8e0;
  --neg: #c62828;
  --neg-soft: #fde7e7;
  --new: #f2c14e;
  --new-soft: #fff4d6;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 2px 8px rgba(0,0,0,.04);
  color-scheme: light;
  --ta-end: right;
}
:root[dir="ltr"] { --ta-end: left; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #141816; --surface: #1c211f; --surface-2: #252b28; --ink: #e9ece9; --muted: #9aa39e; --line: #323a36;
    --brand: #4fb393; --brand-ink: #0f1513; --brand-soft: #1e3a31;
    --in: #5cc98f; --in-soft: #1b3326; --out: #f08a62; --out-soft: #3a2219; --neg: #ff7b7b; --neg-soft: #3d1f1f;
    --new: #f2c14e; --new-soft: #3a3118; --shadow: none; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #141816; --surface: #1c211f; --surface-2: #252b28; --ink: #e9ece9; --muted: #9aa39e; --line: #323a36;
  --brand: #4fb393; --brand-ink: #0f1513; --brand-soft: #1e3a31;
  --in: #5cc98f; --in-soft: #1b3326; --out: #f08a62; --out-soft: #3a2219; --neg: #ff7b7b; --neg-soft: #3d1f1f;
  --new: #f2c14e; --new-soft: #3a3118; --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--ink);
  font-family: 'Cairo', system-ui, 'Segoe UI', 'Microsoft YaHei', 'PingFang SC', 'Noto Sans SC', Tahoma, sans-serif; font-size: 15px; line-height: 1.5;
  padding-bottom: calc(72px + env(safe-area-inset-bottom));
}
button, input, select, textarea { font: inherit; color: inherit; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.ltr { direction: ltr; unicode-bidi: isolate; }
.num { font-variant-numeric: tabular-nums; }

/* الهيدر */
.top {
  position: sticky; top: 0; z-index: 20; display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 17px; }
.net { font-size: 12px; font-weight: 600; background: var(--out-soft); color: var(--out); padding: 2px 8px; border-radius: 99px; }
.sync { border: 0; border-radius: 99px; padding: 2px 9px; font-size: 12px; font-weight: 700; cursor: pointer; background: var(--surface-2); color: var(--muted); }
.sync.ok { background: var(--in-soft); color: var(--in); }
.sync.busy { background: var(--brand-soft); color: var(--brand); }
.sync.wait { background: var(--new-soft); color: #8a6200; }
.sync.err { background: var(--neg-soft); color: var(--neg); }
.login { max-width: 420px; margin: 8vh auto 0; padding: 28px 22px; }
.google-btn { background: #fff !important; color: #1f1f1f !important; border-color: var(--line) !important; font-size: 16px; min-height: 48px; }
.avatar { width: 40px; height: 40px; border-radius: 50%; object-fit: cover; background: var(--surface-2); flex: none; display: inline-block; }
.user-card { border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; display: grid; gap: 8px; }
.user-card.pending { border-color: var(--new); background: var(--new-soft); }
.user-card.blocked { opacity: .7; }
.user-card details summary { cursor: pointer; font-weight: 600; }
.u-whs { display: grid; gap: 2px; margin-top: 6px; }
.top-controls { display: flex; gap: 8px; align-items: center; min-width: 0; }
.wh select { min-width: 120px; max-width: 46vw; }
.lang select { width: auto; min-width: 0; padding-inline: 8px; }

/* التنقل: تحت على الموبايل، فوق على الكمبيوتر */
.nav {
  position: fixed; bottom: 0; inset-inline: 0; z-index: 20; display: flex; justify-content: space-around;
  background: var(--surface); border-top: 1px solid var(--line); padding: 4px 4px calc(4px + env(safe-area-inset-bottom));
}
.nav button {
  flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px; border: 0; background: none;
  padding: 6px 2px; border-radius: 10px; color: var(--muted); font-size: 11.5px; font-weight: 600; cursor: pointer; min-width: 0;
}
.nav button svg { width: 22px; height: 22px; }
.nav button[aria-current="page"] { color: var(--brand); background: var(--brand-soft); }
@media (min-width: 900px) {
  body { padding-bottom: 0; }
  .nav { position: sticky; top: 53px; bottom: auto; border-top: 0; border-bottom: 1px solid var(--line); justify-content: center; gap: 4px; padding: 6px 16px; }
  .nav button { flex: 0 0 auto; flex-direction: row; gap: 6px; font-size: 14px; padding: 8px 14px; }
  .nav button svg { width: 18px; height: 18px; }
}

.main { max-width: 1100px; margin: 0 auto; padding: 16px; }
h1 { font-size: 20px; margin: 0 0 12px; }
h2 { font-size: 16px; margin: 0 0 10px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

/* عناصر عامة */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; box-shadow: var(--shadow); }
.stack { display: grid; gap: 12px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.grow { flex: 1; min-width: 0; }
input, select, textarea {
  background: var(--surface); 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(--brand); outline-offset: 0; border-color: transparent; }
label.field { display: grid; gap: 4px; font-size: 13px; color: var(--muted); font-weight: 600; }
label.field > * { color: var(--ink); font-weight: 400; font-size: 15px; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; border: 1px solid var(--line); background: var(--surface);
  border-radius: 10px; padding: 9px 14px; min-height: 42px; font-weight: 600; cursor: pointer; text-decoration: none;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }
.btn.primary:hover { filter: brightness(1.06); }
.btn.danger { color: var(--neg); }
.btn.sm { min-height: 34px; padding: 5px 10px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.icon-btn { border: 0; background: none; padding: 6px; border-radius: 8px; cursor: pointer; color: var(--muted); min-width: 36px; min-height: 36px; }
.icon-btn:hover { background: var(--surface-2); color: var(--neg); }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { border: 1px solid var(--line); background: var(--surface); border-radius: 99px; padding: 4px 12px; font-size: 13px; font-weight: 600; cursor: pointer; }
.chip[aria-pressed="true"] { background: var(--ink); color: var(--surface); border-color: var(--ink); }
.tag { display: inline-block; font-size: 11.5px; font-weight: 700; padding: 1px 8px; border-radius: 99px; }
.tag.new { background: var(--new-soft); color: #8a6200; border: 1px solid var(--new); }
.tag.in { background: var(--in-soft); color: var(--in); }
.tag.out { background: var(--out-soft); color: var(--out); }
.tag.neg { background: var(--neg-soft); color: var(--neg); }
.tag.sec { background: var(--surface-2); color: var(--muted); font-weight: 600; margin-top: 2px; }
.tree { display: grid; }
.tree-row { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; padding-block: 8px; border-bottom: 1px solid var(--line); }
.tree-row:last-child { border-bottom: 0; }
.tree-row.root { margin-top: 6px; }
.tree-row.root b { font-size: 16px; }
:root[data-theme="dark"] .tag.new { color: var(--new); }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .tag.new { color: var(--new); } }

/* ملخص */
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 12px; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 12px; }
.stat b { display: block; font-size: 20px; }
.stat span { font-size: 12px; color: var(--muted); font-weight: 600; }
.stat.neg b { color: var(--neg); }

/* المخزون */
.toolbar { display: grid; gap: 10px; margin-bottom: 12px; }
.items { display: grid; grid-template-columns: repeat(auto-fill, minmax(165px, 1fr)); gap: 10px; }
.item {
  display: grid; grid-template-rows: auto 1fr; text-align: start; background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); overflow: hidden; cursor: pointer; padding: 0; box-shadow: var(--shadow);
}
.item:hover { border-color: var(--brand); }
.item.is-new { border-color: var(--new); box-shadow: 0 0 0 2px var(--new-soft); }
.thumb { aspect-ratio: 4/3; background: var(--surface-2); display: grid; place-items: center; color: var(--muted); font-size: 12px; overflow: hidden; }
.thumb img { width: 100%; height: 100%; object-fit: contain; background: #fff; }
.item-body { padding: 8px 10px 10px; display: grid; gap: 2px; }
.code { font-weight: 700; font-size: 14px; direction: ltr; text-align: var(--ta-end); unicode-bidi: isolate; word-break: break-word; }
.size { color: var(--muted); font-size: 13px; direction: ltr; text-align: var(--ta-end); unicode-bidi: isolate; }
.bal { display: flex; justify-content: space-between; align-items: baseline; margin-top: 4px; }
.bal b { font-size: 18px; }
.bal.neg b { color: var(--neg); }
.bal.zero b { color: var(--muted); }
@media (max-width: 420px) { .items { grid-template-columns: repeat(2, 1fr); } }

/* أزرار شكل القائمة */
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; background: var(--surface); }
.seg button { border: 0; background: none; color: var(--muted); padding: 6px 10px; cursor: pointer; display: grid; place-items: center; min-width: 40px; min-height: 36px; }
.seg button + button { border-inline-start: 1px solid var(--line); }
.seg button svg { width: 18px; height: 18px; }
.seg button[aria-pressed="true"] { background: var(--ink); color: var(--surface); }

/* كروت صغيرة */
.items.small { grid-template-columns: repeat(auto-fill, minmax(118px, 1fr)); gap: 8px; }
.items.small .item-body { padding: 6px 8px 8px; }
.items.small .code { font-size: 12.5px; }
.items.small .size { font-size: 11.5px; }
.items.small .bal b { font-size: 15px; }
.items.small .bal .small { font-size: 0; }
.items.small .bal .small .tag { font-size: 10.5px; }
@media (max-width: 420px) { .items.small { grid-template-columns: repeat(3, 1fr); } }

/* قائمة */
.rows { display: grid; gap: 6px; grid-template-columns: repeat(auto-fill, minmax(min(100%, 380px), 1fr)); }
.row-item {
  display: flex; align-items: center; gap: 12px; text-align: start; width: 100%; padding: 6px 12px 6px 6px; padding-inline: 6px 14px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 10px; cursor: pointer;
}
.row-item:hover { border-color: var(--brand); }
.row-item.is-new { border-color: var(--new); }
.row-thumb { width: 56px; height: 56px; flex: none; border-radius: 8px; overflow: hidden; background: var(--surface-2); }
.row-thumb img { width: 100%; height: 100%; object-fit: contain; background: #fff; }
.row-item .code, .row-item .size { display: block; }
.row-bal { font-size: 18px; min-width: 48px; text-align: end; }
.row-bal.neg, .tbl b.neg { color: var(--neg); }
.row-bal.zero, .tbl b.zero { color: var(--muted); }

/* جدول */
.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }
.tbl { width: 100%; border-collapse: collapse; font-size: 14px; }
.tbl th { position: sticky; top: 0; background: var(--surface-2); font-size: 12.5px; color: var(--muted); font-weight: 700; text-align: start; padding: 8px 10px; white-space: nowrap; }
.tbl td { padding: 7px 10px; border-top: 1px solid var(--line); white-space: nowrap; }
.tbl .n { text-align: end; font-variant-numeric: tabular-nums; }
.tbl tbody tr { cursor: pointer; }
/* الكود والرصيد ثابتين وأنت بتحرك الجدول بالعرض */
.tbl td:first-child, .tbl th:first-child { position: sticky; inset-inline-start: 0; z-index: 1; }
.tbl td:last-child, .tbl th:last-child { position: sticky; inset-inline-end: 0; z-index: 1; }
.tbl td:first-child, .tbl td:last-child { background: var(--surface); }
.tbl th:first-child, .tbl th:last-child { z-index: 2; }
.tbl tbody tr:hover td { background: var(--brand-soft); }
.tbl tbody tr:hover { background: var(--brand-soft); }
.tbl tr.is-new td:first-child { box-shadow: inset 4px 0 0 var(--new); }
:root[dir="rtl"] .tbl tr.is-new td:first-child { box-shadow: inset -4px 0 0 var(--new); }

/* جداول وقوائم */
.list { display: grid; gap: 0; }
.list-row { display: grid; grid-template-columns: 1fr auto auto; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.list-row:last-child { border-bottom: 0; }
.day { font-weight: 700; font-size: 13px; color: var(--muted); margin: 14px 0 4px; display: flex; justify-content: space-between; }

/* سطور الحركة */
.lines { display: grid; gap: 8px; }
.line { display: grid; grid-template-columns: 1fr 90px 36px; gap: 8px; align-items: center; }
.line.bad input:first-child { border-color: var(--neg); }
.review-line { display: grid; gap: 6px; padding: 10px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); }
.review-line.ok { border-inline-start: 4px solid var(--in); }
.review-line.check { border-inline-start: 4px solid var(--new); background: var(--new-soft); }
.review-line.new { border-inline-start: 4px solid var(--new); }
.review-line.skip { opacity: .6; border-inline-start: 4px solid var(--muted); }
.review-grid { display: grid; grid-template-columns: 1fr 90px; gap: 8px; }
.raw { font-size: 13px; color: var(--muted); direction: ltr; text-align: var(--ta-end); unicode-bidi: isolate; }

/* كذا ورقة */
.papers { display: grid; gap: 12px; }
.paper { border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; display: grid; gap: 10px; background: var(--bg); }
.paper-head { display: flex; gap: 12px; align-items: flex-start; }
.paper-thumb { width: 72px; height: 96px; object-fit: cover; border-radius: 8px; border: 1px solid var(--line); background: #fff; cursor: zoom-in; flex: none; }
.field.inline { display: flex; align-items: center; gap: 8px; }
.field.inline input { width: auto; min-height: 36px; padding: 4px 8px; }
.edit-grid { display: grid; grid-template-columns: 1.4fr 1fr 80px; gap: 6px; }
.edit-grid input { min-height: 38px; padding: 6px 9px; }
.drop.drop-sm { padding: 12px; }
input.warn { border-color: var(--new); box-shadow: 0 0 0 2px var(--new-soft); }
.date-note { color: #8a6200; font-weight: 600; }
:root[data-theme="dark"] .date-note { color: var(--new); }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .date-note { color: var(--new); } }
.total-row { display: flex; justify-content: space-between; align-items: baseline; border-top: 2px solid var(--ink); padding-top: 10px; font-weight: 700; font-size: 17px; }
.total-row b { font-size: 24px; }
@media (max-width: 480px) { .edit-grid { grid-template-columns: 1fr 1fr 64px; } }

.preview { max-width: 100%; max-height: 340px; border-radius: 10px; border: 1px solid var(--line); background: #fff; display: block; margin: 0 auto; }
.drop { display: grid; place-items: center; gap: 6px; text-align: center; border: 2px dashed var(--line); border-radius: var(--radius); padding: 28px 16px; cursor: pointer; background: var(--surface); }
.drop:hover { border-color: var(--brand); }
.drop input { display: none; }

.alert { border-radius: 10px; padding: 10px 12px; font-size: 14px; }
.alert.warn { background: var(--new-soft); border: 1px solid var(--new); }
.alert.bad { background: var(--neg-soft); color: var(--neg); border: 1px solid var(--neg); }
.alert.info { background: var(--brand-soft); }

/* نافذة */
dialog {
  border: 0; border-radius: 16px; padding: 0; width: min(560px, calc(100vw - 32px)); max-height: calc(100vh - 48px);
  background: var(--surface); color: var(--ink); box-shadow: 0 20px 60px rgba(0,0,0,.25);
}
dialog::backdrop { background: rgba(0,0,0,.45); }
#modalBody { padding: 18px; overflow: auto; max-height: calc(100vh - 48px); }
.modal-head { display: flex; justify-content: space-between; align-items: start; gap: 10px; margin-bottom: 12px; }
.modal-img { width: 100%; max-height: 300px; object-fit: contain; background: #fff; border-radius: 10px; border: 1px solid var(--line); }
.actions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; margin-top: 14px; }

.toast {
  position: fixed; inset-inline: 16px; bottom: calc(84px + env(safe-area-inset-bottom)); z-index: 50; margin: 0 auto; max-width: 420px;
  background: var(--ink); color: var(--surface); padding: 10px 14px; border-radius: 10px; font-weight: 600; text-align: center;
  opacity: 0; transform: translateY(8px); transition: .2s; pointer-events: none;
}
.toast.show { opacity: 1; transform: none; }
@media (min-width: 900px) { .toast { bottom: 24px; } }

.empty { text-align: center; padding: 40px 16px; color: var(--muted); }
.spinner { width: 18px; height: 18px; border: 2px solid currentColor; border-top-color: transparent; border-radius: 50%; animation: spin .8s linear infinite; display: inline-block; }
@keyframes spin { to { transform: rotate(360deg); } }
