:root {
  --navy: #0B2A6B; --blue: #1F4FB0; --red: #E0262B;
  --bg: #F4F6FA; --surface: #FFFFFF; --text: #0F172A; --muted: #475569;
  --border: #E2E8F0; --input: #F8FAFC; --input-border: #CBD5E1;
  --good: #15803D; --warn: #C2410C; --bad: #B91C1C; --soft: #EEF2FF;
  --shadow: 0 8px 24px rgba(11, 42, 107, .08);
}
[data-theme="dark"] {
  --bg: #0A1020; --surface: #121A2E; --text: #E6EAF2; --muted: #A3AEC4;
  --border: #26324A; --input: #0F172A; --input-border: #334155;
  --good: #22C55E; --warn: #FB923C; --bad: #F87171; --soft: #1A2440;
  --shadow: 0 8px 24px rgba(0, 0, 0, .35);
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: 'El Messiri', system-ui, sans-serif; background: var(--bg); color: var(--text);
  text-align: center; -webkit-tap-highlight-color: transparent; min-height: 100vh;
}
a { color: var(--blue); }
[data-theme="dark"] a { color: #7AA2F7; }
h1, h2, h3 { margin: 0; color: var(--navy); }
[data-theme="dark"] h1, [data-theme="dark"] h2, [data-theme="dark"] h3 { color: #C7D2FE; }
input, select, textarea, button { font-family: inherit; font-size: 16px; }

/* الهيدر */
.topbar { background: var(--navy); color: #fff; display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 10px 14px; position: sticky; top: 0; z-index: 50; }
[data-theme="dark"] .topbar { background: #0B1A3F; }
.topbar .brand { display: flex; align-items: center; gap: 10px; color: #fff; text-decoration: none; }
.topbar .brand img { width: 42px; height: 42px; border-radius: 50%; background: #fff; object-fit: cover; }
.topbar .brand b { font-size: 17px; display: block; }
.topbar .brand small { font-size: 12px; color: #C7D2FE; display: block; }
.icon-btn { width: 44px; height: 44px; border-radius: 22px; border: 1px solid rgba(255,255,255,.4); background: transparent; color: #fff; display: inline-flex; align-items: center; justify-content: center; cursor: pointer; }
.nav { display: flex; gap: 6px; flex-wrap: wrap; justify-content: center; }
.nav a { color: #fff; text-decoration: none; padding: 8px 12px; border-radius: 10px; font-size: 14px; }
.nav a.on, .nav a:hover { background: rgba(255,255,255,.14); }

.wrap { max-width: 1320px; margin: 0 auto; padding: 18px 14px 90px; display: flex; flex-direction: column; gap: 16px; }
.narrow { max-width: 520px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 18px; padding: 16px; box-shadow: var(--shadow); }
.muted { color: var(--muted); font-size: 14px; }
.grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); }
.kpi .n { font-size: 30px; font-weight: 700; }
.kpi .l { font-size: 14px; color: var(--muted); }
.c-good { color: var(--good); } .c-warn { color: var(--warn); } .c-bad { color: var(--bad); } .c-blue { color: var(--blue); }

/* النماذج */
.field { display: flex; flex-direction: column; gap: 6px; font-size: 14px; }
.field input, .field select, .field textarea {
  height: 50px; border-radius: 12px; border: 1px solid var(--input-border); background: var(--input); color: var(--text);
  text-align: center; padding: 0 12px; width: 100%;
}
.field textarea { height: 90px; padding: 10px; }
.row2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: 50px; padding: 0 18px; border-radius: 12px; border: none; background: var(--navy); color: #fff; font-size: 17px; font-weight: 600; text-decoration: none; cursor: pointer; }
[data-theme="dark"] .btn { background: #2B4C9B; }
.btn.block { width: 100%; }
.btn.good { background: var(--good); } .btn.bad { background: var(--bad); } .btn.ghost { background: transparent; color: var(--blue); border: 1px solid var(--input-border); }
.btn:disabled { background: #94A3B8 !important; cursor: not-allowed; }
.alert { border-radius: 12px; padding: 12px; font-size: 15px; }
.alert.ok { background: #DCFCE7; color: #14532D; }
.alert.err { background: #FEE2E2; color: #7F1D1D; }
.alert.warn { background: #FFF1F2; color: #9F1239; border: 1px solid #FDA4AF; }
[data-theme="dark"] .alert.ok { background: #14321F; color: #BBF7D0; }
[data-theme="dark"] .alert.err, [data-theme="dark"] .alert.warn { background: #3B1219; color: #FECDD3; }
.badge { display: inline-block; border-radius: 10px; padding: 2px 10px; font-size: 13px; background: var(--soft); color: var(--blue); }

/* الجداول */
.table-box { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 15px; }
th { background: var(--soft); color: var(--navy); padding: 10px; white-space: nowrap; }
[data-theme="dark"] th { color: #C7D2FE; }
td { padding: 10px; border-top: 1px solid var(--border); }
tr.lvl-0 td { font-weight: 700; background: var(--soft); }
tr.lvl-1 td:first-child { padding-right: 26px; font-weight: 600; }
tr.lvl-2 td:first-child { padding-right: 46px; color: var(--muted); }
.bar { height: 8px; border-radius: 4px; background: var(--border); overflow: hidden; min-width: 70px; }
.bar > span { display: block; height: 100%; background: var(--blue); }

/* التبويبات */
.tabs { display: grid; grid-auto-flow: column; gap: 6px; background: var(--soft); border-radius: 12px; padding: 4px; }
.tabs a, .tabs button { min-height: 44px; border-radius: 10px; border: none; background: transparent; color: var(--navy); font-weight: 600; text-decoration: none; display: flex; align-items: center; justify-content: center; cursor: pointer; }
[data-theme="dark"] .tabs a, [data-theme="dark"] .tabs button { color: #C7D2FE; }
.tabs .on { background: var(--navy); color: #fff !important; }

/* شاشة الدخول على الكمبيوتر: صورة الشركة خلفية */
.auth-page { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 24px 14px; position: relative; }
@media (min-width: 900px) {
  .auth-page { background: url('/img/bg.jpg') center / cover no-repeat; }
  .auth-page::before { content: ''; position: absolute; inset: 0; background: rgba(11, 42, 107, .55); }
}
.auth-card { position: relative; width: 100%; max-width: 440px; background: var(--surface); border-radius: 24px; padding: 32px 26px; display: flex; flex-direction: column; gap: 16px; align-items: stretch; box-shadow: 0 24px 60px rgba(11,42,107,.3); }
.auth-card .logo { width: 104px; height: 104px; border-radius: 50%; object-fit: cover; align-self: center; background: #fff; }
.theme-float { position: absolute; top: 18px; left: 18px; z-index: 2; background: rgba(11,42,107,.6); }

.welcome { min-height: 100vh; background: var(--navy); color: #fff; display: flex; flex-direction: column; align-items: center; justify-content: space-between; padding: 80px 24px 48px; gap: 24px; }
.welcome .ring { width: 176px; height: 176px; border-radius: 50%; background: #fff; display: flex; align-items: center; justify-content: center; box-shadow: 0 0 0 10px rgba(255,255,255,.1), 0 0 0 22px rgba(255,255,255,.05); }
.welcome .ring img { width: 164px; height: 164px; border-radius: 50%; object-fit: cover; }
.welcome h1 { color: #fff; font-size: 28px; }
.welcome .line { width: 56px; height: 4px; border-radius: 2px; background: var(--red); margin: 0 auto; }

/* القارئ */
.scanner { border-radius: 18px; overflow: hidden; background: #0F172A; min-height: 200px; position: relative; }
.scanner #reader-cam { width: 100%; }
.loc-btn { width: 104px; height: 104px; border-radius: 50%; border: none; color: #fff; background: var(--navy); display: flex; align-items: center; justify-content: center; margin: 0 auto; cursor: pointer; box-shadow: 0 0 0 9px rgba(11,42,107,.14); transition: background .2s; }
.loc-btn.busy { background: var(--warn); box-shadow: 0 0 0 9px rgba(194,65,12,.18); animation: pulse 1.2s infinite; }
.loc-btn.done { background: var(--good); box-shadow: 0 0 0 9px rgba(21,128,61,.18); }
@keyframes pulse { 50% { transform: scale(.95); } }
.big-action { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px; min-height: 120px; border-radius: 20px; background: var(--navy); color: #fff !important; text-decoration: none; font-size: 20px; font-weight: 700; }
.meter-no { font-size: 26px; font-weight: 700; letter-spacing: 2px; color: var(--navy); direction: ltr; }
[data-theme="dark"] .meter-no { color: #C7D2FE; }
.list-row { display: flex; align-items: center; justify-content: space-between; min-height: 50px; border-bottom: 1px solid var(--border); padding: 0 4px; }
.list-row:last-child { border-bottom: none; }

/* الشريط السفلي في النقال */
.bottom-nav { position: fixed; bottom: 0; left: 0; right: 0; background: var(--surface); border-top: 1px solid var(--border); display: grid; grid-auto-flow: column; padding: 6px 0 calc(8px + env(safe-area-inset-bottom)); z-index: 60; }
.bottom-nav a { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; min-height: 52px; color: var(--muted); text-decoration: none; font-size: 13px; }
.bottom-nav a.on { color: var(--navy); font-weight: 700; }
[data-theme="dark"] .bottom-nav a.on { color: #C7D2FE; }
@media (min-width: 900px) { .bottom-nav { display: none; } .wrap { padding-bottom: 40px; } }
@media (max-width: 899px) {
  .topbar { flex-wrap: wrap; }
  .topbar .nav { order: 3; width: 100%; flex-wrap: nowrap; overflow-x: auto; justify-content: flex-start; }
  .topbar .nav a { white-space: nowrap; }
}

/* الخريطة */
.map-page { position: fixed; inset: 0; display: flex; flex-direction: column; }
#map { flex: 1 1 auto; }
.sheet { background: var(--surface); border-radius: 22px 22px 0 0; margin-top: -20px; z-index: 500; padding: 10px 14px calc(14px + env(safe-area-inset-bottom)); max-height: 46vh; overflow-y: auto; box-shadow: 0 -8px 24px rgba(0,0,0,.15); position: relative; }
.sheet .grip { width: 44px; height: 5px; border-radius: 3px; background: var(--border); margin: 0 auto 8px; }
.fault-item { display: flex; align-items: center; gap: 10px; border: 1px solid var(--border); border-radius: 14px; padding: 10px; margin-bottom: 8px; cursor: pointer; }
.fault-item.sel { border-color: var(--blue); box-shadow: 0 0 0 2px var(--blue) inset; }
.dot { width: 12px; height: 12px; border-radius: 50%; flex: 0 0 auto; }
.fault-pin { width: 22px; height: 22px; border-radius: 50%; border: 3px solid #fff; box-shadow: 0 2px 6px rgba(0,0,0,.35); }
.me-pin { width: 18px; height: 18px; border-radius: 50%; background: #2563EB; border: 3px solid #fff; box-shadow: 0 0 0 10px rgba(37,99,235,.2); }
.toast { position: fixed; bottom: 90px; left: 50%; transform: translateX(-50%); background: #0F172A; color: #fff; padding: 12px 18px; border-radius: 12px; z-index: 9999; font-size: 15px; max-width: 90vw; }
.hidden { display: none !important; }

/* لوحة الادمن */
.tiles { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); }
.tile { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px; min-height: 120px; border-radius: 18px; background: var(--surface); border: 1px solid var(--border); color: var(--text) !important; text-decoration: none; box-shadow: var(--shadow); }
.tile b { font-size: 17px; }
.tile .n { font-size: 26px; font-weight: 700; color: var(--blue); }
.tile:hover { border-color: var(--blue); }
.mini { height: 40px; border-radius: 10px; border: 1px solid var(--input-border); background: var(--input); color: var(--text); text-align: center; padding: 0 8px; min-width: 70px; width: 100%; }
.mini[type=checkbox] { width: 22px; height: 22px; min-width: 0; }
.btn.sm { min-height: 40px; font-size: 14px; padding: 0 12px; }
.filters { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; align-items: flex-end; }
.filters .field { flex: 1 1 150px; }
.pager nav, .pager ul { display: flex; gap: 8px; justify-content: center; list-style: none; padding: 0; }

/* الطباعة: ورقة A4 طول، بالشعار والتاريخ واسم الدائرة */
.print-only { display: none; }
@page { size: A4 portrait; margin: 12mm 10mm; }
@media print {
  html, body { background: #fff !important; color: #000 !important; }
  [data-theme="dark"] { --bg: #fff; --surface: #fff; --text: #000; --muted: #333; --border: #999; --soft: #eee; }
  .topbar, .bottom-nav, .no-print, .filters, .tabs, .btn, button, form .btn, .pager, .alert, .leaflet-container, #sq-map, .toast { display: none !important; }
  .print-only { display: block; }
  .print-head { display: flex !important; align-items: center; justify-content: space-between; border-bottom: 2px solid #000; padding-bottom: 8px; margin-bottom: 8px; font-size: 13px; }
  .print-head .ph-side { line-height: 1.7; font-weight: 600; }
  .print-head .ph-logo { width: 70px; height: 70px; border-radius: 50%; }
  .print-title { font-size: 18px; text-align: center; margin: 4px 0 10px; color: #000 !important; }
  .print-foot { display: flex !important; justify-content: space-around; margin-top: 30px; font-size: 13px; text-align: center; page-break-inside: avoid; }
  .wrap { max-width: none; padding: 0; gap: 10px; }
  .card { box-shadow: none !important; border: none !important; padding: 0 !important; }
  .table-box { overflow: visible !important; }
  table { font-size: 10.5px; page-break-inside: auto; }
  thead { display: table-header-group; }
  tr { page-break-inside: avoid; }
  th { background: #e5e7eb !important; color: #000 !important; -webkit-print-color-adjust: exact; print-color-adjust: exact; border: 1px solid #999; padding: 4px; }
  td { border: 1px solid #bbb; padding: 4px; }
  .kpi .n { font-size: 18px; }
  .grid, .tiles { gap: 6px; }
  a { color: #000 !important; text-decoration: none; }
  h1, h2 { color: #000 !important; }
}

/* شاشة الاعداد الاجبارية */
.gate { position: fixed; inset: 0; z-index: 3000; background: var(--bg); overflow-y: auto; display: flex; align-items: flex-start; justify-content: center; padding: 24px 16px calc(24px + env(safe-area-inset-bottom)); }
.gate.hidden { display: none; }
html.gated body { overflow: hidden; }
.gate-box { width: 100%; max-width: 440px; display: flex; flex-direction: column; gap: 14px; text-align: center; }
.gate-logo { width: 96px; height: 96px; border-radius: 22px; margin: 8px auto 0; object-fit: contain; background: #fff; }
.gate-step { background: var(--surface); border: 1px solid var(--border); border-radius: 16px; padding: 16px; display: flex; flex-direction: column; gap: 10px; opacity: .55; }
.gate-step.current { opacity: 1; border: 2px solid var(--blue); }
.gate-step.hidden { display: none; }
.gate-head { display: flex; align-items: center; justify-content: center; gap: 8px; font-size: 18px; }
.gate-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--warn); }
.gate-step.current .gate-dot { background: var(--blue); }
.gate-list { text-align: start; line-height: 2; margin: 0; padding-inline-start: 22px; }
.gate-denied { color: var(--bad); line-height: 1.8; }
.pulse { animation: pulse 1s 3; }
@keyframes pulse { 50% { background: var(--soft); } }
@media print { .gate { display: none !important; } }

/* تنبيه الشوائب الفعلية للقارئ */
.tag-alert { display: flex; flex-direction: column; gap: 6px; border-width: 2px; }
.tag-dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-inline-end: 6px; vertical-align: middle; }
.details { font-size: 13px; line-height: 1.7; color: var(--muted); }
.details b { color: var(--text); font-weight: 600; }

/* اختيار نوع الشائبة للفني */
.chips { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.chip { display: inline-flex; align-items: center; gap: 6px; min-height: 44px; padding: 8px 14px; border-radius: 999px; border: 1px solid var(--border); background: var(--surface); color: var(--text); text-decoration: none; font-size: 15px; }
.chip b { background: var(--soft); border-radius: 999px; padding: 0 8px; font-size: 13px; }
.chip.on { border: 2px solid var(--blue); background: var(--soft); font-weight: 700; }
.chip .tag-dot { margin: 0; }
