/* ═══ الألوان: داكن افتراضياً (نفس ألوان البرنامج المكتبي) + وضع فاتح ═══ */
:root {
  --bg: #141428;
  --side: #1a1a2e;
  --surface: #1e2a4a;
  --surface-2: #233554;
  --row-alt: #1b2540;
  --hover: #2a3f5f;
  --border: #31466d;
  --text: #f1f4fb;
  --muted: #a8b2d1;
  --accent: #00b4d8;
  --accent-ink: #04222b;
  --green: #06d6a0;
  --orange: #f4845f;
  --red: #ef476f;
  --yellow: #ffd166;
  --purple: #a06cd5;
  --shadow: 0 10px 30px rgba(0, 0, 0, .35);
  --radius: 12px;
  color-scheme: dark;
}
:root[data-theme="light"] {
  --bg: #f3f5fa;
  --side: #1a1a2e;
  --surface: #ffffff;
  --surface-2: #eef2f8;
  --row-alt: #f7f9fc;
  --hover: #e4ecf7;
  --border: #d6deea;
  --text: #16213e;
  --muted: #5b6785;
  --accent: #0089a8;
  --accent-ink: #ffffff;
  --green: #059c76;
  --orange: #d9612f;
  --red: #d22f57;
  --yellow: #b8860b;
  --purple: #7d4bb5;
  --shadow: 0 10px 30px rgba(22, 33, 62, .12);
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: "IBM Plex Sans Arabic", "Segoe UI", Tahoma, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14.5px;
  line-height: 1.55;
}
a { color: var(--accent); }
button, input, select, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }

/* ═══ الهيكل ═══ */
.app { display: flex; min-height: 100vh; }
.sidebar {
  width: 236px; flex-shrink: 0; background: var(--side); color: #fff;
  display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh;
}
.brand { display: flex; gap: 10px; align-items: center; padding: 20px 18px 16px; border-bottom: 1px solid #2a3556; }
.brand-title { font-weight: 700; font-size: 16px; }
.brand-sub { font-size: 11px; color: #a8b2d1; }
.nav { display: flex; flex-direction: column; padding: 10px; gap: 2px; overflow-y: auto; flex: 1; }
.nav a {
  display: flex; gap: 10px; align-items: center; padding: 10px 12px; border-radius: 9px;
  color: #c9d1ea; text-decoration: none; font-weight: 500;
}
.nav a:hover { background: #24304f; color: #fff; }
.nav a.active { background: #0f3460; color: #fff; box-shadow: inset -3px 0 0 #00b4d8; }
.nav .ico { width: 22px; text-align: center; }
.sidebar-foot { padding: 12px 16px; border-top: 1px solid #2a3556; font-size: 12px; color: #a8b2d1; word-break: break-all; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  position: sticky; top: 0; z-index: 20; display: flex; gap: 10px; align-items: center;
  padding: 12px 24px; background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(8px); border-bottom: 1px solid var(--border);
}
.search-wrap { position: relative; flex: 1; max-width: 560px; }
.search-wrap input {
  width: 100%; padding: 10px 14px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface); outline: none;
}
.search-wrap input:focus { border-color: var(--accent); }
.search-results {
  position: absolute; top: calc(100% + 6px); inset-inline: 0; background: var(--surface);
  border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow);
  max-height: 60vh; overflow-y: auto; padding: 6px;
}
.search-results .group { font-size: 12px; color: var(--muted); padding: 8px 10px 4px; }
.search-results button {
  display: block; width: 100%; text-align: start; padding: 8px 10px; border: 0; background: none;
  border-radius: 8px; cursor: pointer;
}
.search-results button:hover, .search-results button:focus { background: var(--hover); outline: none; }
.search-results small { color: var(--muted); display: block; }
.icon-btn {
  border: 1px solid var(--border); background: var(--surface); border-radius: 10px;
  width: 40px; height: 40px; cursor: pointer; font-size: 17px; flex-shrink: 0;
}
.icon-btn:hover { background: var(--hover); }
.only-mobile { display: none; }

.view { padding: 22px 24px 60px; outline: none; }
.page-head { display: flex; flex-wrap: wrap; gap: 12px; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.page-head h1 { margin: 0; font-size: 22px; display: flex; gap: 10px; align-items: center; }
.badge {
  display: inline-block; padding: 2px 10px; border-radius: 999px; font-size: 12px; font-weight: 600;
  background: var(--surface-2); color: var(--muted); border: 1px solid var(--border);
}
.actions { display: flex; flex-wrap: wrap; gap: 8px; }

/* ═══ الأزرار والحقول ═══ */
.btn {
  display: inline-flex; align-items: center; gap: 6px; padding: 8px 14px; border-radius: 9px;
  border: 1px solid var(--border); background: var(--surface); cursor: pointer; font-weight: 600;
  white-space: nowrap; text-decoration: none; color: var(--text);
}
.btn:hover:not(:disabled) { background: var(--hover); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.success { background: var(--green); border-color: var(--green); color: #032a20; }
.btn.danger { background: transparent; border-color: var(--red); color: var(--red); }
.btn.danger:hover:not(:disabled) { background: var(--red); color: #fff; }
.btn.small { padding: 4px 10px; font-size: 13px; }

.filters {
  display: flex; flex-wrap: wrap; gap: 10px; align-items: center; padding: 12px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); margin-bottom: 12px;
}
.filters input[type=search] { flex: 1; min-width: 200px; }
.field { display: flex; flex-direction: column; gap: 4px; }
.field label { font-size: 13px; color: var(--muted); font-weight: 500; }
input[type=text], input[type=password], input[type=search], input[type=date], input[type=number], select, textarea {
  padding: 8px 11px; border-radius: 9px; border: 1px solid var(--border); background: var(--surface-2);
  outline: none; min-height: 38px;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent); }
textarea { resize: vertical; min-height: 70px; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
  padding: 5px 12px; border-radius: 999px; border: 1px solid var(--border); background: var(--surface-2);
  cursor: pointer; font-size: 13px; font-weight: 500;
}
.chip.on { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }

/* ═══ البطاقات ═══ */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; margin-bottom: 18px; }
.stat {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px;
  cursor: pointer; transition: transform .12s, border-color .12s; text-align: start;
}
.stat:hover { transform: translateY(-2px); border-color: var(--accent); }
.stat .lbl { color: var(--muted); font-size: 13px; }
.stat .num { font-size: 30px; font-weight: 700; line-height: 1.2; margin-top: 4px; font-variant-numeric: tabular-nums; }
.grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); gap: 14px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; min-width: 0; }
.card h2 { margin: 0 0 12px; font-size: 16px; display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.empty { color: var(--muted); text-align: center; padding: 28px 10px; }
.list { display: flex; flex-direction: column; }
.list-item {
  display: flex; justify-content: space-between; gap: 10px; align-items: center; padding: 9px 4px;
  border-bottom: 1px solid var(--border); cursor: pointer;
}
.list-item:last-child { border-bottom: 0; }
.list-item:hover { background: var(--hover); }
.list-item small { color: var(--muted); }

/* أشرطة الأنواع */
.bars { display: flex; flex-direction: column; gap: 10px; }
.bar-row { display: grid; grid-template-columns: 110px 1fr 44px; gap: 10px; align-items: center; cursor: pointer; }
.bar-row .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar { display: flex; height: 14px; border-radius: 4px; overflow: hidden; background: var(--surface-2); gap: 2px; }
.bar span { display: block; height: 100%; }
.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 12px; color: var(--muted); margin-top: 10px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-inline-end: 5px; vertical-align: middle; }

/* ═══ الحالة ═══ */
.st { display: inline-flex; align-items: center; gap: 6px; font-weight: 600; font-size: 13px; white-space: nowrap; }
.st::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: currentColor; }
.st-active { color: var(--green); }
.st-maint { color: var(--orange); }
.st-inactive { color: var(--red); }
.due-over { color: var(--red); font-weight: 600; }
.due-soon { color: var(--yellow); font-weight: 600; }

/* ═══ الجداول ═══ */
.table-wrap { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: auto; max-height: calc(100vh - 250px); }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 9px 12px; text-align: start; border-bottom: 1px solid var(--border); white-space: nowrap; }
td.wrap { white-space: normal; min-width: 220px; }
th {
  position: sticky; top: 0; background: var(--surface-2); z-index: 1; font-size: 13px; color: var(--muted);
  font-weight: 600; cursor: pointer; user-select: none;
}
th.nosort { cursor: default; }
th .arrow { opacity: .8; font-size: 11px; }
tbody tr:nth-child(even) { background: var(--row-alt); }
tbody tr { cursor: pointer; }
tbody tr:hover { background: var(--hover); }
tbody tr.sel { background: color-mix(in srgb, var(--accent) 18%, transparent); }
td.num, th.num { width: 1%; color: var(--muted); font-variant-numeric: tabular-nums; }
td.chk, th.chk { width: 1%; cursor: default; }
input[type=checkbox] { width: 17px; height: 17px; accent-color: var(--accent); cursor: pointer; }
.mono { font-family: Consolas, "Courier New", monospace; direction: ltr; unicode-bidi: isolate; }
.copy { border: 0; background: none; cursor: pointer; opacity: .55; padding: 0 4px; }
.copy:hover { opacity: 1; }
.more-row { text-align: center; padding: 12px; }

.selbar {
  position: sticky; bottom: 12px; margin-top: 12px; display: flex; flex-wrap: wrap; gap: 8px; align-items: center;
  padding: 10px 14px; background: var(--side); color: #fff; border-radius: 12px; box-shadow: var(--shadow); z-index: 5;
}
.selbar .count { font-weight: 700; margin-inline-end: auto; }
.selbar .btn { background: #26365c; border-color: #3a5078; color: #fff; }
.selbar .btn.danger { background: transparent; border-color: #ef476f; color: #ff8fa8; }

/* تبويبات */
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 14px; overflow-x: auto; }
.tabs button {
  border: 0; background: none; padding: 10px 14px; cursor: pointer; font-weight: 600; color: var(--muted);
  border-bottom: 2px solid transparent; white-space: nowrap;
}
.tabs button.on { color: var(--accent); border-bottom-color: var(--accent); }

/* ═══ النوافذ ═══ */
.backdrop { position: fixed; inset: 0; background: rgba(5, 8, 20, .55); z-index: 40; }
.modal {
  position: fixed; inset: 0; z-index: 60; display: grid; place-items: center; padding: 16px;
  background: rgba(5, 8, 20, .55);
}
.modal-box {
  background: var(--surface); border: 1px solid var(--border); border-radius: 16px; box-shadow: var(--shadow);
  width: min(680px, 100%); max-height: calc(100vh - 32px); display: flex; flex-direction: column;
}
.modal-box.narrow { width: min(440px, 100%); }
.modal-head { padding: 16px 20px; border-bottom: 1px solid var(--border); display: flex; justify-content: space-between; align-items: center; }
.modal-head h3 { margin: 0; font-size: 18px; }
.modal-body { padding: 18px 20px; overflow-y: auto; }
.modal-foot { padding: 14px 20px; border-top: 1px solid var(--border); display: flex; gap: 8px; justify-content: flex-start; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.form-grid .full { grid-column: 1 / -1; }
.hint { font-size: 12px; color: var(--muted); }
.warn-box { background: color-mix(in srgb, var(--yellow) 15%, transparent); border: 1px solid var(--yellow); padding: 8px 12px; border-radius: 9px; font-size: 13px; }
.row-inline { display: flex; gap: 6px; }
.row-inline > :first-child { flex: 1; }

.drawer {
  position: fixed; top: 0; bottom: 0; left: 0; width: min(560px, 100%); z-index: 50;
  background: var(--surface); border-inline-end: 1px solid var(--border); box-shadow: var(--shadow);
  display: flex; flex-direction: column; animation: slide .18s ease-out;
}
@keyframes slide { from { transform: translateX(-30px); opacity: 0; } }
.drawer-head { padding: 16px 20px; border-bottom: 1px solid var(--border); display: flex; gap: 10px; align-items: flex-start; justify-content: space-between; }
.drawer-head h3 { margin: 0 0 4px; font-size: 19px; }
.drawer-body { overflow-y: auto; padding: 16px 20px 30px; }
.kv { display: grid; grid-template-columns: 120px 1fr; gap: 6px 12px; margin-bottom: 16px; }
.kv dt { color: var(--muted); font-size: 13px; }
.kv dd { margin: 0; word-break: break-word; }
.timeline { list-style: none; margin: 0; padding: 0; position: relative; }
.timeline li { position: relative; padding: 0 22px 16px 0; }
.timeline li::before { content: ""; position: absolute; right: 5px; top: 8px; bottom: -8px; width: 2px; background: var(--border); }
.timeline li:last-child::before { display: none; }
.timeline li::after { content: ""; position: absolute; right: 0; top: 5px; width: 12px; height: 12px; border-radius: 50%; background: var(--dot, var(--accent)); }
.timeline .when { font-size: 12px; color: var(--muted); }
.timeline .what { font-weight: 600; }
.timeline .det { font-size: 13px; color: var(--muted); white-space: pre-line; }

.toasts { position: fixed; bottom: 18px; left: 18px; z-index: 80; display: flex; flex-direction: column; gap: 8px; }
.toast {
  background: var(--side); color: #fff; padding: 11px 16px; border-radius: 10px; box-shadow: var(--shadow);
  border-inline-start: 4px solid var(--green); max-width: 360px; animation: slide .2s;
}
.toast.err { border-inline-start-color: var(--red); }

.spinner { width: 28px; height: 28px; border: 3px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; margin: 40px auto; }
@keyframes spin { to { transform: rotate(360deg); } }
.error-box { background: color-mix(in srgb, var(--red) 12%, transparent); border: 1px solid var(--red); padding: 16px; border-radius: 12px; }

.login {
  position: fixed; inset: 0; z-index: 100; display: grid; place-items: center; padding: 16px;
  background: radial-gradient(circle at 30% 20%, #0f3460, #1a1a2e 60%);
}
.login-box {
  width: min(380px, 100%); background: var(--surface); border: 1px solid var(--border); border-radius: 18px;
  padding: 28px 24px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 14px; text-align: center;
}
.login-box img { margin: 0 auto; }
.login-box h1 { margin: 0; font-size: 20px; }
.login-box .hint { margin: -8px 0 4px; }
.login-box .field { text-align: start; }
.login-box .btn { justify-content: center; padding: 11px; font-size: 15px; }
.login-err { color: var(--red); font-size: 13px; min-height: 1em; }

.tool-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 14px; }
.tool-grid .card p { color: var(--muted); margin: 0 0 12px; font-size: 13.5px; }

/* ═══ الموبايل ═══ */
@media (max-width: 860px) {
  html { overflow-x: hidden; }
  .sidebar {
    position: fixed; right: 0; z-index: 45; transform: translateX(100%); transition: transform .2s;
    box-shadow: var(--shadow);
  }
  .sidebar.open { transform: none; }
  .only-mobile { display: inline-block; }
  .topbar { padding: 10px 12px; }
  .view { padding: 16px 12px 80px; }
  .form-grid { grid-template-columns: 1fr; }
  .grid-2 { grid-template-columns: 1fr; }
  .stat .num { font-size: 24px; }
  .table-wrap { max-height: none; border: 0; background: none; overflow: visible; }
  /* الجدول يتحول إلى بطاقات */
  table.responsive thead { display: none; }
  table.responsive, table.responsive tbody, table.responsive tr, table.responsive td { display: block; width: 100%; }
  table.responsive tr {
    background: var(--surface) !important; border: 1px solid var(--border); border-radius: 12px;
    margin-bottom: 10px; padding: 8px 12px; position: relative;
  }
  table.responsive tr.sel { border-color: var(--accent); }
  table.responsive td { border: 0; padding: 3px 0; display: flex; gap: 10px; white-space: normal; overflow-wrap: anywhere; min-width: 0; }
  table.responsive td > * { min-width: 0; }
  table.responsive td::before { content: attr(data-label); color: var(--muted); font-size: 12px; min-width: 96px; }
  table.responsive td.num { display: none; }
  table.responsive td.chk { position: absolute; left: 12px; top: 10px; width: auto; }
  table.responsive td.chk::before { display: none; }
  table.responsive td.title { font-weight: 700; font-size: 15px; padding-left: 30px; }
  table.responsive td.title::before { display: none; }
  table.responsive td:empty, table.responsive td.blank { display: none; }
  .kv { grid-template-columns: 100px 1fr; }
  .toasts { left: 12px; right: 12px; }
}

/* ═══ الطباعة ═══ */
.print-area { display: none; }
@media print {
  @page { size: A4; margin: 14mm; }
  body { background: #fff; color: #000; }
  .app, .drawer, .modal, .toasts, .backdrop { display: none !important; }
  .print-area { display: block; font-size: 12.5px; color: #000; }
  .print-area h1 { font-size: 20px; text-align: center; margin: 0 0 4px; }
  .print-area .sub { text-align: center; color: #444; margin-bottom: 14px; }
  .print-area table { border-collapse: collapse; width: 100%; margin-bottom: 14px; }
  .print-area th, .print-area td { border: 1px solid #555; padding: 5px 7px; white-space: normal; position: static; background: none; color: #000; }
  .print-area th { background: #e9edf3 !important; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .print-area .kvt th { width: 28%; }
  .print-area .sign { display: flex; justify-content: space-between; margin-top: 50px; }
  .print-area .sign div { width: 30%; text-align: center; border-top: 1px solid #000; padding-top: 6px; }
}
