:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-2: #f8fafb;
  --border: #e3e8ee;
  --border-strong: #cfd7e0;
  --text: #0f1d2b;
  --text-2: #4a5a6b;
  --muted: #7b8a99;
  --brand: #0f6e66;
  --brand-600: #0c5b54;
  --brand-50: #e7f3f1;
  --brand-100: #cfe7e3;
  --side: #0d1b26;
  --side-2: #142634;
  --side-text: #aab9c6;
  --amber: #b26b00; --amber-bg: #fff4e0;
  --blue: #1d5fbf;  --blue-bg: #e8f0fc;
  --green: #137a3f; --green-bg: #e5f5eb;
  --red: #b42318;   --red-bg: #fdecea;
  --gray: #5b6875;  --gray-bg: #eef1f4;
  --radius: 10px;
  --radius-sm: 7px;
  --shadow: 0 1px 2px rgba(16, 29, 43, .05), 0 1px 3px rgba(16, 29, 43, .06);
  --shadow-lg: 0 18px 50px -12px rgba(16, 29, 43, .28);
  --font: 'IBM Plex Sans Arabic', system-ui, -apple-system, 'Segoe UI', Tahoma, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: var(--font); font-size: 14.5px; line-height: 1.6;
  background: var(--bg); color: var(--text);
  -webkit-font-smoothing: antialiased;
}
a { color: var(--brand); text-decoration: none; }
button, input, select, textarea { font: inherit; color: inherit; }
.num, .mono { font-variant-numeric: tabular-nums; }
.mono { font-family: ui-monospace, 'SFMono-Regular', Consolas, monospace; font-size: 12.5px; direction: ltr; unicode-bidi: isolate; }
.muted { color: var(--muted); }
.hidden { display: none !important; }
svg.i { width: 18px; height: 18px; flex: none; stroke: currentColor; fill: none; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

/* ------------------------------ Layout ------------------------------ */
.app { display: grid; grid-template-columns: 252px 1fr; min-height: 100vh; }
.sidebar {
  background: var(--side); color: var(--side-text);
  position: sticky; top: 0; height: 100vh; display: flex; flex-direction: column;
  padding: 18px 12px;
}
.brand { display: flex; align-items: center; gap: 11px; padding: 4px 8px 20px; border-bottom: 1px solid rgba(255,255,255,.07); margin-bottom: 12px; }
.brand-mark {
  width: 38px; height: 38px; border-radius: 9px; display: grid; place-items: center;
  background: linear-gradient(140deg, #18a091, #0c5b54); color: #fff;
}
.brand-mark svg { width: 21px; height: 21px; }
.brand b { display: block; color: #fff; font-size: 15px; font-weight: 600; line-height: 1.3; }
.brand small { font-size: 12px; color: #7f93a4; }
.nav { display: flex; flex-direction: column; gap: 2px; overflow-y: auto; flex: 1; }
.nav-label { font-size: 11.5px; color: #627688; padding: 14px 12px 6px; letter-spacing: .02em; }
.nav a {
  display: flex; align-items: center; gap: 11px; padding: 9px 12px; border-radius: var(--radius-sm);
  color: var(--side-text); font-weight: 500; transition: background .15s, color .15s;
}
.nav a:hover { background: var(--side-2); color: #e6edf3; }
.nav a.active { background: rgba(24, 160, 145, .16); color: #fff; }
.nav a.active svg { color: #3fd0bf; }
.nav .count {
  margin-inline-start: auto; background: #e8a33a; color: #1d1300; font-size: 11.5px; font-weight: 700;
  min-width: 22px; height: 20px; border-radius: 10px; display: grid; place-items: center; padding: 0 6px;
}
.side-foot { border-top: 1px solid rgba(255,255,255,.07); padding: 12px 8px 0; display: flex; align-items: center; gap: 10px; }
.avatar {
  width: 34px; height: 34px; border-radius: 50%; display: grid; place-items: center; flex: none;
  background: #1f3a4c; color: #cfe5f3; font-weight: 600; font-size: 13px;
}
.side-foot .who { flex: 1; min-width: 0; }
.side-foot .who b { display: block; color: #fff; font-weight: 500; font-size: 13.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.side-foot .who small { font-size: 12px; color: #7f93a4; }
.icon-btn {
  background: none; border: 0; cursor: pointer; color: inherit; padding: 7px; border-radius: 7px; display: grid; place-items: center;
}
.icon-btn:hover { background: rgba(127,147,164,.16); }

.main { min-width: 0; display: flex; flex-direction: column; }
.topbar {
  height: 62px; background: var(--surface); border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: 14px; padding: 0 28px; position: sticky; top: 0; z-index: 20;
}
.topbar .menu-btn { display: none; }
.crumbs { color: var(--muted); font-size: 13.5px; display: flex; gap: 8px; align-items: center; }
.crumbs b { color: var(--text); font-weight: 600; font-size: 15px; }
.topbar .spacer { flex: 1; }
.global-search { position: relative; width: min(340px, 40vw); }
.global-search input { padding-inline-start: 36px; background: var(--surface-2); }
.global-search svg { position: absolute; inset-inline-start: 11px; top: 50%; transform: translateY(-50%); color: var(--muted); width: 17px; height: 17px; }
.content { padding: 26px 28px 60px; max-width: 1360px; width: 100%; }

/* ------------------------------ Page head ------------------------------ */
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 20px; flex-wrap: wrap; }
.page-head h1 { margin: 0; font-size: 22px; font-weight: 700; letter-spacing: -.01em; }
.page-head p { margin: 2px 0 0; color: var(--muted); }
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

/* ------------------------------ Controls ------------------------------ */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  height: 38px; padding: 0 15px; border-radius: var(--radius-sm); border: 1px solid var(--border-strong);
  background: var(--surface); color: var(--text); font-weight: 500; cursor: pointer; white-space: nowrap;
  transition: background .15s, border-color .15s, box-shadow .15s;
}
.btn:hover { background: var(--surface-2); border-color: #b9c4cf; }
.btn:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible { outline: 3px solid rgba(15,110,102,.25); outline-offset: 1px; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { background: var(--brand-600); }
.btn.success { background: var(--green); border-color: var(--green); color: #fff; }
.btn.success:hover { filter: brightness(.94); }
.btn.danger { background: var(--surface); border-color: #efc2bd; color: var(--red); }
.btn.danger:hover { background: var(--red-bg); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--gray-bg); }
.btn.sm { height: 31px; padding: 0 11px; font-size: 13px; }
.btn[disabled] { opacity: .55; pointer-events: none; }
.btn svg { width: 17px; height: 17px; }

input, select, textarea {
  width: 100%; height: 38px; border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
  background: var(--surface); padding: 0 11px; transition: border-color .15s;
}
textarea { height: auto; min-height: 76px; padding: 9px 11px; resize: vertical; }
input:focus, select:focus, textarea:focus { border-color: var(--brand); }
input[type=number] { direction: ltr; text-align: right; }
input[type=checkbox] { width: 16px; height: 16px; accent-color: var(--brand); }
label.field { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--text-2); font-weight: 500; }
label.field .hint { font-weight: 400; color: var(--muted); font-size: 12px; }
.grid { display: grid; gap: 16px; }
.g2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.g3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.g4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.span2 { grid-column: span 2; }

.seg { display: inline-flex; background: var(--gray-bg); border-radius: 9px; padding: 3px; gap: 2px; flex-wrap: wrap; }
.seg button {
  border: 0; background: transparent; height: 32px; padding: 0 13px; border-radius: 7px; cursor: pointer;
  color: var(--text-2); font-weight: 500; display: inline-flex; align-items: center; gap: 7px;
}
.seg button.on { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.seg .n { font-size: 11.5px; background: var(--border); border-radius: 9px; padding: 0 7px; color: var(--text-2); }
.seg button.on .n { background: var(--brand-50); color: var(--brand); }

/* ------------------------------ Cards ------------------------------ */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 15px 18px; border-bottom: 1px solid var(--border); }
.card-head h3 { margin: 0; font-size: 15px; font-weight: 600; }
.card-body { padding: 18px; }
.card + .card { margin-top: 18px; }

.kpis { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 16px; margin-bottom: 18px; }
.kpi { padding: 17px 18px; display: flex; gap: 14px; align-items: flex-start; }
.kpi .ic { width: 42px; height: 42px; border-radius: 10px; display: grid; place-items: center; flex: none; }
.kpi .ic svg { width: 21px; height: 21px; }
.kpi .v { font-size: 26px; font-weight: 700; line-height: 1.1; font-variant-numeric: tabular-nums; }
.kpi .l { color: var(--muted); font-size: 13px; margin-top: 4px; }
.kpi .s { font-size: 12.5px; margin-top: 6px; color: var(--text-2); }
.t-brand { background: var(--brand-50); color: var(--brand); }
.t-amber { background: var(--amber-bg); color: var(--amber); }
.t-blue { background: var(--blue-bg); color: var(--blue); }
.t-red { background: var(--red-bg); color: var(--red); }
.t-green { background: var(--green-bg); color: var(--green); }
a.kpi:hover { border-color: var(--brand-100); }
a.kpi { color: inherit; }

.dash { display: grid; grid-template-columns: minmax(0, 1.65fr) minmax(0, 1fr); gap: 18px; }
.dash .card + .card { margin-top: 0; }
.stack { display: flex; flex-direction: column; gap: 18px; }

/* ------------------------------ Tables ------------------------------ */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: start; padding: 11px 16px; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { font-size: 12.5px; font-weight: 600; color: var(--muted); background: var(--surface-2); white-space: nowrap; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr.click { cursor: pointer; }
tbody tr.click:hover td { background: #fafcfc; }
td.n, th.n { text-align: end; font-variant-numeric: tabular-nums; white-space: nowrap; }
.cell-title { font-weight: 600; }
.cell-sub { font-size: 12.5px; color: var(--muted); }
.empty { text-align: center; padding: 46px 20px; color: var(--muted); }
.empty svg { width: 34px; height: 34px; color: #b7c2cc; margin-bottom: 8px; }

.toolbar { display: flex; gap: 10px; padding: 14px 16px; border-bottom: 1px solid var(--border); flex-wrap: wrap; align-items: center; }
.toolbar .search { position: relative; flex: 1; min-width: 220px; max-width: 380px; }
.toolbar .search input { padding-inline-start: 36px; }
.toolbar .search svg { position: absolute; inset-inline-start: 11px; top: 50%; transform: translateY(-50%); color: var(--muted); width: 17px; height: 17px; }
.toolbar select { width: auto; min-width: 160px; }

/* ------------------------------ Badges ------------------------------ */
.badge {
  display: inline-flex; align-items: center; gap: 6px; height: 24px; padding: 0 10px; border-radius: 12px;
  font-size: 12.5px; font-weight: 600; white-space: nowrap;
}
.badge::before { content: ''; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.badge.plain::before { display: none; }
.b-amber { background: var(--amber-bg); color: var(--amber); }
.b-blue { background: var(--blue-bg); color: var(--blue); }
.b-green { background: var(--green-bg); color: var(--green); }
.b-red { background: var(--red-bg); color: var(--red); }
.b-gray { background: var(--gray-bg); color: var(--gray); }
.b-brand { background: var(--brand-50); color: var(--brand); }
.qty-low { color: var(--red); font-weight: 700; }
.chip { display: inline-flex; align-items: center; gap: 5px; background: var(--gray-bg); border-radius: 6px; padding: 2px 8px; font-size: 12.5px; margin: 2px; }
.chip button { border: 0; background: none; cursor: pointer; color: var(--muted); padding: 0; line-height: 1; font-size: 15px; }

/* ------------------------------ Detail ------------------------------ */
.detail-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 0; }
.detail-grid > div { padding: 14px 18px; border-inline-start: 1px solid var(--border); }
.detail-grid > div:first-child { border-inline-start: 0; }
.detail-grid dt { font-size: 12.5px; color: var(--muted); margin-bottom: 3px; }
.detail-grid dd { margin: 0; font-weight: 600; }
.stat-big { font-size: 24px; font-weight: 700; font-variant-numeric: tabular-nums; }

.timeline { list-style: none; margin: 0; padding: 4px 0; }
.timeline li { position: relative; padding: 0 30px 18px 0; }
.timeline li::before { content: ''; position: absolute; right: 8px; top: 7px; width: 10px; height: 10px; border-radius: 50%; background: var(--border-strong); box-shadow: 0 0 0 4px var(--surface); }
.timeline li::after { content: ''; position: absolute; right: 12px; top: 18px; bottom: -2px; width: 2px; background: var(--border); }
.timeline li:last-child::after { display: none; }
.timeline li.done::before { background: var(--green); }
.timeline li.now::before { background: var(--amber); }
.timeline li.bad::before { background: var(--red); }
.timeline b { display: block; font-weight: 600; }
.timeline small { color: var(--muted); }

.notice { display: flex; gap: 11px; padding: 12px 14px; border-radius: var(--radius-sm); font-size: 13.5px; align-items: flex-start; }
.notice svg { margin-top: 2px; }
.notice.amber { background: var(--amber-bg); color: #7a4a00; }
.notice.blue { background: var(--blue-bg); color: #19437e; }
.notice.red { background: var(--red-bg); color: #8a1c13; }
.notice.green { background: var(--green-bg); color: #0e5a2e; }

/* ------------------------------ Line editor ------------------------------ */
.lines td { vertical-align: top; }
.lines td input { min-width: 90px; }
.picker { position: relative; }
.picker-list {
  position: absolute; z-index: 30; top: calc(100% + 4px); inset-inline: 0; max-height: 300px; overflow-y: auto;
  background: var(--surface); border: 1px solid var(--border-strong); border-radius: var(--radius-sm); box-shadow: var(--shadow-lg);
}
.picker-list button {
  display: flex; width: 100%; justify-content: space-between; gap: 10px; border: 0; background: none; text-align: start;
  padding: 9px 12px; cursor: pointer; border-bottom: 1px solid var(--border);
}
.picker-list button:hover, .picker-list button.hl { background: var(--brand-50); }
.picker-list button[disabled] { opacity: .45; cursor: not-allowed; }
.serial-box { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.serial-opt { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--border-strong); border-radius: 7px; padding: 4px 9px; cursor: pointer; font-size: 12.5px; }
.serial-opt:has(input:checked) { border-color: var(--brand); background: var(--brand-50); }

/* ------------------------------ Chart ------------------------------ */
.chart { width: 100%; height: 230px; display: block; }
.chart text { font-family: var(--font); font-size: 11px; fill: var(--muted); }
.legend { display: flex; gap: 16px; font-size: 12.5px; color: var(--text-2); }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-inline-end: 6px; vertical-align: -1px; }
.bar-row { display: grid; grid-template-columns: 1fr 70px; gap: 10px; align-items: center; padding: 7px 0; }
.bar-row .track { height: 7px; background: var(--gray-bg); border-radius: 4px; margin-top: 5px; overflow: hidden; }
.bar-row .fill { height: 100%; background: var(--brand); border-radius: 4px; }

/* ------------------------------ Modal & toast ------------------------------ */
.overlay { position: fixed; inset: 0; background: rgba(13, 27, 38, .45); display: grid; place-items: center; z-index: 100; padding: 16px; animation: fade .15s; }
.modal { background: var(--surface); border-radius: 14px; width: min(560px, 100%); max-height: calc(100vh - 32px); display: flex; flex-direction: column; box-shadow: var(--shadow-lg); animation: pop .18s; }
.modal.wide { width: min(820px, 100%); }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 17px 20px; border-bottom: 1px solid var(--border); }
.modal-head h3 { margin: 0; font-size: 16.5px; }
.modal-body { padding: 20px; overflow-y: auto; }
.modal-foot { display: flex; justify-content: flex-start; gap: 8px; padding: 14px 20px; border-top: 1px solid var(--border); background: var(--surface-2); border-radius: 0 0 14px 14px; }
@keyframes fade { from { opacity: 0; } }
@keyframes pop { from { opacity: 0; transform: translateY(8px) scale(.98); } }

.toasts { position: fixed; bottom: 22px; left: 22px; display: flex; flex-direction: column; gap: 8px; z-index: 200; }
.toast { background: #0f1d2b; color: #fff; padding: 11px 16px; border-radius: 9px; box-shadow: var(--shadow-lg); display: flex; gap: 10px; align-items: center; min-width: 260px; animation: pop .2s; }
.toast.err { background: #8a1c13; }
.toast svg { color: #49d6c4; }
.toast.err svg { color: #ffb4ab; }

/* ------------------------------ Login ------------------------------ */
.login { min-height: 100vh; display: grid; grid-template-columns: 1fr 1.05fr; }
.login-form { display: grid; place-items: center; padding: 40px 24px; background: var(--surface); }
.login-form form { width: min(380px, 100%); display: flex; flex-direction: column; gap: 16px; }
.login-form h1 { margin: 0; font-size: 25px; }
.login-form .lead { margin: -8px 0 6px; color: var(--muted); }
.login-art {
  background: radial-gradient(1200px 500px at 85% -10%, #17675f 0%, transparent 60%), linear-gradient(160deg, #0d1b26 0%, #0f2a33 100%);
  color: #d7e3ea; padding: 48px 52px; display: flex; flex-direction: column; justify-content: space-between; position: relative; overflow: hidden;
}
.login-art::after {
  content: ''; position: absolute; inset: 0; opacity: .07;
  background-image: linear-gradient(rgba(255,255,255,.9) 1px, transparent 1px), linear-gradient(90deg, rgba(255,255,255,.9) 1px, transparent 1px);
  background-size: 44px 44px; mask-image: linear-gradient(200deg, #000 20%, transparent 75%);
}
.login-art > * { position: relative; z-index: 1; }
.login-art h2 { font-size: 34px; line-height: 1.35; margin: 0 0 14px; color: #fff; font-weight: 700; max-width: 460px; }
.login-art p { color: #9fb3c1; max-width: 440px; margin: 0; }
.flow { display: flex; gap: 10px; align-items: center; margin-top: 30px; flex-wrap: wrap; }
.flow span { background: rgba(255,255,255,.07); border: 1px solid rgba(255,255,255,.1); padding: 7px 13px; border-radius: 8px; font-size: 13px; }
.flow svg { width: 16px; height: 16px; color: #49d6c4; transform: scaleX(-1); }
.demo-box { border: 1px dashed var(--border-strong); border-radius: var(--radius); padding: 12px 14px; background: var(--surface-2); }
.demo-box .t { font-size: 12.5px; color: var(--muted); margin-bottom: 8px; display: flex; justify-content: space-between; }
.demo-users { display: flex; flex-wrap: wrap; gap: 6px; }
.demo-users button { border: 1px solid var(--border-strong); background: var(--surface); border-radius: 7px; padding: 5px 10px; cursor: pointer; font-size: 12.5px; }
.demo-users button:hover { border-color: var(--brand); color: var(--brand); }
.err-msg { color: var(--red); font-size: 13.5px; min-height: 20px; }

/* ------------------------------ Matrix ------------------------------ */
.matrix th, .matrix td { text-align: center; }
.matrix th:first-child, .matrix td:first-child { text-align: start; }
.yes { color: var(--green); }
.no { color: #c9d1d9; }

/* ------------------------------ Print ------------------------------ */
.print-doc { background: #fff; max-width: 820px; margin: 0 auto; padding: 36px 40px; border: 1px solid var(--border); border-radius: var(--radius); }
.print-doc header { display: flex; justify-content: space-between; align-items: flex-start; border-bottom: 2px solid var(--text); padding-bottom: 14px; margin-bottom: 18px; }
.print-doc h2 { margin: 0; font-size: 22px; }
.print-doc .meta { display: grid; grid-template-columns: repeat(2, 1fr); gap: 6px 24px; margin-bottom: 18px; font-size: 13.5px; }
.print-doc table th { background: #f1f3f5; color: var(--text); }
.print-doc table td, .print-doc table th { border: 1px solid #c9d1d9; padding: 8px 10px; }
.signs { display: grid; grid-template-columns: repeat(3, 1fr); gap: 20px; margin-top: 46px; text-align: center; font-size: 13px; }
.signs div { border-top: 1px solid var(--text); padding-top: 8px; }
.signs small { display: block; color: var(--muted); margin-top: 3px; }

@media print {
  body { background: #fff; }
  .sidebar, .topbar, .no-print, .toasts { display: none !important; }
  .app { display: block; }
  .content { padding: 0; max-width: none; }
  .print-doc { border: 0; padding: 0; max-width: none; }
}

/* ------------------------------ Responsive ------------------------------ */
@media (max-width: 1100px) {
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .dash { grid-template-columns: minmax(0, 1fr); }
  .detail-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .detail-grid > div:nth-child(3) { border-inline-start: 0; }
  .detail-grid > div:nth-child(n+3) { border-top: 1px solid var(--border); }
}
@media (max-width: 860px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .sidebar { position: fixed; inset-block: 0; right: 0; width: 270px; z-index: 60; transform: translateX(100%); transition: transform .22s; }
  .app.nav-open .sidebar { transform: none; box-shadow: var(--shadow-lg); }
  .app.nav-open::after { content: ''; position: fixed; inset: 0; background: rgba(13,27,38,.4); z-index: 50; }
  .topbar { padding: 0 16px; }
  .topbar .menu-btn { display: grid; }
  .global-search { display: none; }
  .content { padding: 18px 16px 50px; }
  .login { grid-template-columns: 1fr; }
  .login-art { display: none; }
  .g2, .g3, .g4 { grid-template-columns: 1fr; }
  .span2 { grid-column: auto; }
  .kpi .v { font-size: 22px; }
  th, td { padding: 10px 12px; }
  .table-wrap > table { min-width: 680px; }
  .dash .table-wrap > table, .lines { min-width: 0; }
  .cell-title { min-width: 150px; }
  .toolbar select { flex: 1; }
}
.mono { white-space: nowrap; }
td.mono[style*="normal"] { white-space: normal; }
@media (max-width: 520px) {
  .kpis { grid-template-columns: 1fr 1fr; gap: 10px; }
  .kpi { padding: 13px; flex-direction: column; gap: 10px; }
  .detail-grid { grid-template-columns: 1fr; }
  .detail-grid > div { border-inline-start: 0 !important; border-top: 1px solid var(--border); }
  .detail-grid > div:first-child { border-top: 0; }
}
