:root {
  --bg: #f3f5f7;
  --surface: #ffffff;
  --ink: #1b2430;
  --muted: #5f6b7a;
  --line: #dfe4ea;
  --primary: #0f5f8c;
  --primary-soft: #e3f0f8;
  --red: #c62828;
  --red-soft: #fdecec;
  --amber: #a85d00;
  --amber-soft: #fff3e0;
  --green: #2e7d32;
  --green-soft: #e8f5e9;
  --blue-soft: #eef2ff;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --font: "IBM Plex Sans Arabic", system-ui, -apple-system, "Segoe UI", Tahoma, sans-serif;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font-family: var(--font); font-size: 16px; line-height: 1.55;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--primary); }
h1 { font-size: 1.35rem; margin: 4px 0 14px; }
h2 { font-size: 1.05rem; margin: 0; }
h3 { font-size: 1.05rem; margin: 0; }
[hidden] { display: none !important; }
.loading { padding: 40px; text-align: center; color: var(--muted); }

/* ---------- الهيكل ---------- */
.top {
  position: sticky; top: 0; z-index: 20; background: var(--primary); color: #fff;
  display: flex; align-items: center; gap: 16px; padding: 10px 16px;
  padding-top: calc(10px + env(safe-area-inset-top));
}
.brand { font-weight: 700; font-size: 1.05rem; cursor: pointer; white-space: nowrap; }
.topnav { display: none; gap: 4px; flex: 1; }
.topnav a { color: #dcebf5; text-decoration: none; padding: 6px 10px; border-radius: 8px; white-space: nowrap; }
.topnav a.on, .topnav a:hover { background: rgba(255, 255, 255, .15); color: #fff; }
.top .new-top { display: none; background: #fff; color: var(--primary); border-color: #fff; }
.bell {
  margin-inline-start: auto; position: relative; text-decoration: none; font-size: 1.3rem; line-height: 1;
  padding: 6px; border-radius: 8px;
}
.bell:hover { background: rgba(255, 255, 255, .15); }
#bellCount {
  position: absolute; top: -2px; left: -4px; background: var(--red); color: #fff; font-size: .7rem; font-weight: 700;
  border-radius: 999px; min-width: 18px; height: 18px; display: grid; place-items: center; padding: 0 4px;
}
#bellCount[hidden] { display: none; }
main { padding: 16px; padding-bottom: 96px; max-width: 1100px; margin: 0 auto; }
.page.narrow { max-width: 640px; margin: 0 auto; }

.bottomnav {
  position: fixed; bottom: 0; inset-inline: 0; z-index: 20; background: var(--surface);
  border-top: 1px solid var(--line); display: grid; grid-template-columns: repeat(5, 1fr);
  padding-bottom: env(safe-area-inset-bottom);
}
.bottomnav a {
  display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 8px 2px 6px;
  font-size: .75rem; color: var(--muted); text-decoration: none;
}
.bottomnav a .ic { font-size: 1.25rem; line-height: 1; }
.bottomnav a.on { color: var(--primary); font-weight: 600; }
.bottomnav a.plus .ic {
  background: var(--primary); color: #fff; width: 46px; height: 46px; border-radius: 50%;
  display: grid; place-items: center; font-size: 1.7rem; margin-top: -22px;
  box-shadow: 0 4px 10px rgba(15, 95, 140, .35);
}
.bottomnav a.plus { color: var(--primary); font-weight: 600; }

@media (min-width: 900px) {
  .topnav, .top .new-top { display: flex; }
  .bottomnav { display: none; }
  main { padding: 24px; padding-bottom: 40px; }
}

/* ---------- عناصر عامة ---------- */
.btn {
  border: 1px solid var(--line); background: var(--surface); border-radius: 10px;
  padding: 9px 14px; cursor: pointer; font-weight: 500; white-space: nowrap;
  min-height: 42px; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
}
.btn:hover { border-color: #b9c3cf; }
a.btn { text-decoration: none; color: inherit; }
a.btn.primary { color: #fff; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.danger { color: var(--red); border-color: #f0c4c4; }
.btn.ok { background: var(--green); border-color: var(--green); color: #fff; }
.btn.block { width: 100%; }
.btn.sm { min-height: 34px; padding: 5px 10px; font-size: .9rem; }
.btn:disabled { opacity: .6; cursor: wait; }
.link { background: none; border: 0; color: var(--primary); cursor: pointer; padding: 6px 0; font-weight: 500; }
.row-btns { display: flex; gap: 8px; flex-wrap: wrap; }

.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 14px;
}
.form .lbl, .lbl { display: block; font-weight: 600; margin: 14px 0 6px; font-size: .95rem; }
.lbl:first-child { margin-top: 0; }
.lbl.req::after { content: " *"; color: var(--red); }
.inp {
  width: 100%; border: 1px solid #c9d1db; border-radius: 10px; padding: 10px 12px;
  background: #fff; min-height: 44px;
}
.inp:focus { outline: 2px solid var(--primary-soft); border-color: var(--primary); }
textarea.inp { resize: vertical; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.hint { color: var(--muted); font-size: .85rem; margin-top: 4px; }
.hint.warn { color: var(--amber); }
.chk { display: inline-flex; gap: 8px; align-items: center; cursor: pointer; }
.chk input { width: 20px; height: 20px; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 6px; }
.chip {
  border: 1px solid #c9d1db; background: #fff; border-radius: 999px; padding: 6px 12px;
  cursor: pointer; font-size: .9rem; min-height: 36px;
}
.chip.active { background: var(--primary); border-color: var(--primary); color: #fff; }
.chips.small .chip { padding: 4px 10px; min-height: 32px; font-size: .85rem; }

.badge {
  display: inline-block; border-radius: 6px; padding: 1px 8px; font-size: .8rem; font-weight: 600;
  background: var(--blue-soft); color: #334;
}
.badge.red { background: var(--red-soft); color: var(--red); }
.badge.warn { background: var(--amber-soft); color: var(--amber); }
.badge.green { background: var(--green-soft); color: var(--green); }
.badge.stage { background: var(--primary-soft); color: var(--primary); }
.muted { color: var(--muted); }
.red { color: var(--red); }
.amber { color: var(--amber); }
.empty { text-align: center; color: var(--muted); padding: 18px; }

/* ---------- شاشة الصباح ---------- */
.home-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; flex-wrap: wrap; }
.allclear {
  background: var(--green-soft); color: var(--green); border: 1px solid #c8e6c9;
  border-radius: var(--radius); padding: 18px; text-align: center; font-weight: 600; font-size: 1.05rem;
  margin-bottom: 18px;
}
.banner {
  background: var(--amber-soft); border: 1px solid #ffe0b2; color: #6d4100; border-radius: var(--radius);
  padding: 10px 14px; margin-bottom: 14px; display: flex; justify-content: space-between; align-items: center; gap: 10px;
}
.section { margin-bottom: 22px; }
.sec-head { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.sec-head .count {
  background: var(--line); border-radius: 999px; padding: 0 9px; font-size: .85rem; font-weight: 600;
}
.sec-red .sec-head h2 { color: var(--red); }
.sec-red .count { background: var(--red); color: #fff; }
.sec-amber .sec-head h2 { color: var(--amber); }
.sec-amber .count { background: var(--amber); color: #fff; }
.sec-empty { color: var(--muted); font-size: .9rem; padding: 4px 2px; }
.cards { display: grid; gap: 10px; }
@media (min-width: 900px) { .cards { grid-template-columns: repeat(2, 1fr); } }

.rcard {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 12px 14px; cursor: pointer;
  border-inline-start: 5px solid var(--line);
}
.rcard.late { border-inline-start-color: var(--red); }
.rcard.risk { border-inline-start-color: var(--amber); }
.rcard.today { border-inline-start-color: var(--primary); }
.rcard .top-line { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.proj {
  font-weight: 700; color: var(--primary); background: var(--primary-soft); padding: 1px 10px;
  border-radius: 6px; font-size: .9rem; max-width: 75%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.code { color: var(--muted); font-size: .8rem; white-space: nowrap; }
.mat { font-weight: 600; font-size: 1.02rem; margin: 6px 0 2px; overflow-wrap: anywhere; }
.next { margin: 2px 0; }
.next b { font-weight: 600; }
.meta { color: var(--muted); font-size: .88rem; display: flex; flex-wrap: wrap; gap: 4px 12px; }
.badges { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.acts { display: flex; gap: 6px; margin-top: 10px; }
.acts .btn { flex: 1; min-height: 38px; padding: 6px 8px; font-size: .9rem; }

/* ---------- القوائم ---------- */
.search { font-size: 1.05rem; margin-bottom: 10px; }
.filters { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 12px; }
.filters select { width: auto; min-width: 140px; flex: 1; }
.list { display: grid; gap: 8px; }
.lrow {
  background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px;
  cursor: pointer; display: grid; gap: 2px;
}
.lrow:hover { border-color: #b9c3cf; }
.lrow .l1 { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.lrow .l1 .mat { margin: 0; font-size: 1rem; flex: 1; min-width: 50%; }
.count-line { color: var(--muted); font-size: .9rem; margin-bottom: 8px; }

/* ---------- تفاصيل الطلب ---------- */
.detail-head { display: flex; justify-content: space-between; gap: 10px; align-items: flex-start; flex-wrap: wrap; }
.detail-head .mat { font-size: 1.25rem; margin: 6px 0; }
.nextbox {
  background: var(--primary-soft); border: 1px solid #c4dcea; border-radius: var(--radius); padding: 14px; margin: 14px 0;
}
.nextbox.late { background: var(--red-soft); border-color: #f2c2c2; }
.nextbox .ttl { font-size: .85rem; color: var(--muted); font-weight: 600; }
.nextbox .step { font-size: 1.15rem; font-weight: 700; margin: 2px 0 6px; }
.nextbox .acts { flex-wrap: wrap; }
.nextbox .acts .btn { flex: 1 1 40%; }
.closedbox { background: var(--green-soft); border: 1px solid #c8e6c9; border-radius: var(--radius); padding: 14px; margin: 14px 0; }
.closedbox.cancel { background: #f1f1f1; border-color: #ddd; }

.stages { display: flex; gap: 4px; margin: 6px 0 14px; }
.stages .st { flex: 1; text-align: center; font-size: .7rem; color: var(--muted); }
.stages .st i { display: block; height: 6px; border-radius: 3px; background: var(--line); margin-bottom: 4px; }
.stages .st.done i { background: var(--green); }
.stages .st.cur i { background: var(--primary); }
.stages .st.cur { color: var(--primary); font-weight: 700; }
@media (max-width: 520px) { .stages .st span { display: none; } .stages .st.cur span { display: inline; } }

.info { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 10px 16px; }
.info div small { display: block; color: var(--muted); font-size: .8rem; }
.info div b { font-weight: 600; overflow-wrap: anywhere; }
.box-title { display: flex; justify-content: space-between; align-items: center; margin: 22px 0 8px; }
.prewrap { white-space: pre-wrap; overflow-wrap: anywhere; }
.atts { display: flex; flex-wrap: wrap; gap: 8px; }
.att {
  width: 92px; height: 92px; border-radius: 10px; border: 1px solid var(--line); overflow: hidden;
  display: grid; place-items: center; background: #fff; position: relative; text-decoration: none; font-size: .75rem; text-align: center;
}
.att img { width: 100%; height: 100%; object-fit: cover; }
.att .del {
  position: absolute; top: 2px; left: 2px; background: rgba(0, 0, 0, .55); color: #fff; border: 0;
  border-radius: 50%; width: 22px; height: 22px; cursor: pointer; font-size: .75rem; line-height: 22px; padding: 0;
}
.log { display: grid; gap: 0; }
.ev { border-inline-start: 2px solid var(--line); padding: 2px 14px 12px; position: relative; }
.ev::before {
  content: ""; position: absolute; inset-inline-start: -6px; top: 7px; width: 10px; height: 10px;
  border-radius: 50%; background: var(--line);
}
.ev.step::before, .ev.create::before { background: var(--primary); }
.ev.receipt::before { background: var(--green); }
.ev.cancel::before, .ev.postpone::before { background: var(--red); }
.ev.followup::before { background: var(--amber); }
.ev .when { color: var(--muted); font-size: .8rem; }
.ev .what { white-space: pre-wrap; overflow-wrap: anywhere; }
table.tbl { width: 100%; border-collapse: collapse; background: var(--surface); }
.tbl th, .tbl td { text-align: start; padding: 8px; border-bottom: 1px solid var(--line); font-size: .92rem; }
.tbl th { color: var(--muted); font-weight: 600; }
.po-box { margin-top: 8px; border-top: 1px dashed var(--line); padding-top: 6px; }
.danger-zone { border-color: var(--red); }
.danger-zone h2 { color: var(--red); }
/* الطلب الوارد كاملًا */
.order-card { background: var(--card, #fff); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; margin-bottom: 12px; }
.order-card.late { border-color: var(--red); }
.order-head { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 8px 14px; margin-bottom: 10px; }
.order-head small { display: block; color: var(--muted); font-size: .8rem; }
.order-tbl td:nth-child(2), .order-tbl td:nth-child(3), .order-tbl th:nth-child(2), .order-tbl th:nth-child(3) { text-align: center; }
.order-tbl td:nth-child(3) { width: auto; white-space: normal; }
.order-tbl .sm-inp { padding: 6px 8px; min-height: 0; font-size: .88rem; min-width: 150px; }
.order-foot { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px; margin-top: 10px; }
.order-foot .acts { display: flex; gap: 6px; flex-wrap: wrap; }
.order-group { margin-bottom: 14px; }
.order-group .box-title { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: 8px; }
.quote-cards .quote-card.chosen { border-color: var(--green); box-shadow: 0 0 0 2px var(--green-soft); }
.q-head { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin-bottom: 6px; }
.q-head .q-total { margin-inline-start: auto; font-weight: 700; }
.mini-tbl td { padding: 4px 6px; font-size: .86rem; }
/* جدول الفئات: خطوط بين الأعمدة كجدول الإكسل */
.cat-tbl { width: 100%; border-collapse: collapse; }
.cat-tbl th { background: var(--primary-soft, #e3eef6); color: var(--text, inherit); }
.cat-tbl th, .cat-tbl td { border: 1px solid var(--line); vertical-align: middle; }
.cat-tbl td:nth-child(2), .cat-tbl th:nth-child(2), .cat-tbl td:nth-child(3) { text-align: center; }
.cat-tbl td:nth-child(3) { width: 1%; white-space: nowrap; }

/* ---------- النوافذ ---------- */
.overlay {
  position: fixed; inset: 0; background: rgba(15, 23, 42, .45); z-index: 50;
  display: flex; align-items: flex-end; justify-content: center;
}
.sheet {
  background: var(--surface); width: 100%; max-width: 560px; max-height: 92vh; display: flex; flex-direction: column;
  border-radius: 16px 16px 0 0; animation: up .18s ease-out;
}
@media (min-width: 700px) {
  .overlay { align-items: center; }
  .sheet { border-radius: 16px; }
}
@keyframes up { from { transform: translateY(30px); opacity: .5; } to { transform: none; opacity: 1; } }
.sheet-head { display: flex; justify-content: space-between; align-items: center; padding: 14px 16px; border-bottom: 1px solid var(--line); }
.sheet-head .x { background: none; border: 0; font-size: 1.2rem; cursor: pointer; padding: 4px 8px; color: var(--muted); }
.sheet-body { padding: 14px 16px; overflow-y: auto; }
.sheet-sub { color: var(--muted); font-size: .9rem; margin-bottom: 4px; }
.sheet-foot { padding: 12px 16px; padding-bottom: calc(12px + env(safe-area-inset-bottom)); border-top: 1px solid var(--line); display: flex; gap: 8px; }
.sheet-foot .btn { flex: 1; }
.subbox { background: #f7f9fb; border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; margin-top: 12px; }
.subbox.warnbox { background: var(--amber-soft); border-color: #ffe0b2; color: #6d4100; }
.subbox.okbox { background: var(--green-soft); border-color: #c8e6c9; color: #1b5e20; }
.checklist { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 6px 12px; margin-top: 8px; }
.scopebar { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 14px; }
.scopebar .chips { margin-bottom: 0; }
.stat-tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 10px; }
.tile { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; display: grid; gap: 2px; }
.tile b { font-size: 1.6rem; line-height: 1.1; }
.tile span { color: var(--muted); font-size: .85rem; }
.tile.red b { color: var(--red); }
.tile.amber b { color: var(--amber); }
.lrow.unread { border-inline-start: 4px solid var(--primary); background: #f5faff; }

.toast {
  position: fixed; bottom: 84px; left: 50%; transform: translateX(-50%); z-index: 100;
  background: #1f2937; color: #fff; padding: 10px 18px; border-radius: 10px; box-shadow: var(--shadow);
  max-width: 90vw; text-align: center;
}
.toast.err { background: var(--red); }
@media (min-width: 900px) { .toast { bottom: 24px; } }

/* ---------- الدخول ---------- */
.auth { max-width: 400px; margin: 8vh auto; padding: 0 16px; }
.auth .logo { text-align: center; margin-bottom: 16px; }
.auth .logo img { width: 72px; height: 72px; }
.auth h1 { text-align: center; }
.auth.wide { max-width: 560px; }
.welcome-title { margin-bottom: 2px; }
.welcome-sub { text-align: center; color: var(--muted); margin-bottom: 16px; }
.welcome-card { text-align: center; padding: 22px 18px; }
.welcome-text { font-size: 1.05rem; margin: 0 0 18px; line-height: 1.8; white-space: pre-wrap; }
.btn.big { min-height: 52px; font-size: 1.05rem; }
.role-pick { display: grid; gap: 8px; }
.role-opt {
  display: flex; gap: 10px; align-items: flex-start; border: 1px solid #c9d1db; border-radius: 10px;
  padding: 10px 12px; cursor: pointer; background: #fff;
}
.role-opt input { margin-top: 4px; width: 18px; height: 18px; flex: none; }
.role-opt span { display: grid; }
.role-opt small { color: var(--muted); }
.role-opt:has(input:checked) { border-color: var(--primary); background: var(--primary-soft); }
.auth .logo img.company-logo { width: auto; height: auto; max-width: 180px; max-height: 96px; object-fit: contain; }
.brand { display: flex; align-items: center; gap: 8px; }
.brand-logo { height: 30px; max-width: 90px; object-fit: contain; background: #fff; border-radius: 6px; padding: 2px; }
.logo-row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.logo-prev { height: 56px; max-width: 140px; object-fit: contain; border: 1px solid var(--line); border-radius: 8px; background: #fff; padding: 4px; }
.wiz-step { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 1.05rem; margin-bottom: 4px; }
.wiz-step span {
  background: var(--primary); color: #fff; width: 28px; height: 28px; border-radius: 50%;
  display: grid; place-items: center; font-size: .95rem;
}
.quote-cards { display: grid; gap: 10px; }
@media (min-width: 900px) { .quote-cards { grid-template-columns: repeat(2, 1fr); } }
.quote-card.best { border-color: #9ccc9f; background: #f6fbf6; }
.qc-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; flex-wrap: wrap; }
.qc-head span { display: flex; gap: 4px; flex-wrap: wrap; }
.qc-prices { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; margin: 8px 0; }
.qc-prices small { display: block; color: var(--muted); font-size: .78rem; }
.qc-prices b { font-size: .92rem; }
.qc-total { color: var(--primary); }
.nowrap { white-space: nowrap; }
.overlay.confirm-ov { align-items: center; z-index: 60; }
.sheet.confirm-box { max-width: 420px; margin: 16px; border-radius: 16px; }
.confirm-msg { font-size: 1.05rem; font-weight: 600; margin: 6px 0; }
.subbox.msgbox { background: var(--blue-soft); border-color: #d6dcf5; }
.msg-snip { margin-top: 6px; font-size: .88rem; background: var(--blue-soft); border-radius: 8px; padding: 6px 10px; white-space: pre-wrap; overflow-wrap: anywhere; }
.item-card { border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; margin-bottom: 8px; background: #fbfcfd; }
.item-card .lbl { margin-top: 8px; }
.item-head { display: flex; justify-content: space-between; align-items: center; }
.item-row { flex-wrap: wrap; }
.item-row .grow { flex: 1 1 180px; }
.item-row .qty { width: 90px; flex: none; }
.item-row .unit { width: 90px; flex: none; }
.admin-links { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 18px; }
.admin-links .btn { flex: 1 1 140px; }
@media print {
  .top, .bottomnav, .toast, .row-btns, [data-cred] { display: none !important; }
  main { padding: 0; }
  body { background: #fff; }
}

/* ---------- المزيد والمشاريع ---------- */
.menu { display: grid; gap: 8px; }
.menu a {
  display: flex; align-items: center; gap: 12px; padding: 14px; background: var(--surface);
  border: 1px solid var(--line); border-radius: var(--radius); text-decoration: none; color: var(--ink); font-weight: 500;
}
.pgrid { display: grid; gap: 10px; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); }
.pcard { cursor: pointer; }
.pcard h3 { margin-bottom: 6px; }
.stat-line { display: flex; gap: 10px; flex-wrap: wrap; font-size: .9rem; }
.group-title { margin: 18px 0 8px; color: var(--muted); font-weight: 600; font-size: .95rem; }
.settings-sec { margin-bottom: 16px; }
.settings-sec h2 { margin-bottom: 10px; }
.kv { display: grid; grid-template-columns: 1fr 110px; gap: 8px 12px; align-items: center; }
.kv .inp { text-align: center; }
.mini-row { display: flex; gap: 8px; align-items: center; padding: 8px 0; border-bottom: 1px solid var(--line); }
.mini-row:last-child { border-bottom: 0; }
.mini-row .grow { flex: 1; min-width: 0; }
