:root {
  --bg: #eef2f6;
  --surface: #ffffff;
  --surface-2: #f7f9fb;
  --border: #dbe2ea;
  --text: #1a2733;
  --muted: #64748b;
  --primary: #0e7490;
  --primary-dark: #0b5566;
  --primary-light: #e0f2f7;
  --accent: #b45309;
  --green: #15803d;
  --green-bg: #dcfce7;
  --red: #b91c1c;
  --red-bg: #fee2e2;
  --amber: #b45309;
  --amber-bg: #fef3c7;
  --blue: #1d4ed8;
  --blue-bg: #dbeafe;
  --gray-bg: #e5e7eb;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(16,24,40,.06), 0 1px 2px rgba(16,24,40,.04);
  --shadow-lg: 0 10px 30px rgba(16,24,40,.12);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: "Segoe UI", "Assistant", "Rubik", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  direction: rtl;
  font-size: 15px;
  line-height: 1.5;
}
a { color: var(--primary); }
button { font-family: inherit; }

/* ---------- App shell ---------- */
#app { min-height: 100vh; }
.app-layout { display: grid; grid-template-columns: 250px 1fr; min-height: 100vh; }
.sidebar {
  background: linear-gradient(180deg, var(--primary-dark), var(--primary));
  color: #fff; padding: 0; display: flex; flex-direction: column;
  position: sticky; top: 0; height: 100vh; overflow-y: auto;
}
.sidebar .brand { padding: 20px 18px 14px; border-bottom: 1px solid rgba(255,255,255,.15); }
.sidebar .brand h1 { font-size: 19px; margin: 0 0 4px; font-weight: 700; }
.sidebar .brand .sub { font-size: 12px; opacity: .85; }
.nav { list-style: none; margin: 0; padding: 10px 8px; flex: 1; }
.nav li a {
  display: flex; align-items: center; gap: 10px; padding: 10px 12px; margin: 2px 0;
  border-radius: 8px; color: #eaf6fa; text-decoration: none; font-size: 14.5px; cursor: pointer;
  transition: background .12s;
}
.nav li a:hover { background: rgba(255,255,255,.12); }
.nav li a.active { background: #fff; color: var(--primary-dark); font-weight: 600; }
.nav .icon { width: 22px; text-align: center; font-size: 16px; }
.sidebar .user-box { padding: 12px 16px; border-top: 1px solid rgba(255,255,255,.15); font-size: 13px; }
.sidebar .user-box .role { opacity: .8; font-size: 12px; }
.sidebar .user-box button {
  margin-top: 8px; background: rgba(255,255,255,.15); color: #fff; border: none;
  padding: 6px 10px; border-radius: 7px; cursor: pointer; font-size: 12.5px; width: 100%;
}
.sidebar .user-box button:hover { background: rgba(255,255,255,.28); }

.main { padding: 22px 26px 60px; overflow-x: hidden; }
.page-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 18px; flex-wrap: wrap; gap: 12px; }
.page-head h2 { margin: 0; font-size: 22px; }
.page-head .sub { color: var(--muted); font-size: 13px; margin-top: 2px; }

/* ---------- Cards ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; margin-bottom: 18px; }
.card > h3 { margin: 0 0 14px; font-size: 16px; }
.grid { display: grid; gap: 16px; }
.grid.cols-2 { grid-template-columns: repeat(2, 1fr); }
.grid.cols-3 { grid-template-columns: repeat(3, 1fr); }
.grid.cols-4 { grid-template-columns: repeat(4, 1fr); }

/* ---------- Stat tiles ---------- */
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 18px; box-shadow: var(--shadow); }
.stat .label { color: var(--muted); font-size: 13px; }
.stat .value { font-size: 26px; font-weight: 700; margin-top: 6px; }
.stat .value.pos { color: var(--green); }
.stat .value.neg { color: var(--red); }
.stat .hint { font-size: 12px; color: var(--muted); margin-top: 4px; }

/* ---------- Toolbar ---------- */
.toolbar { display: flex; align-items: center; gap: 10px; margin-bottom: 14px; flex-wrap: wrap; }
.toolbar .spacer { flex: 1; }
.toolbar .count { color: var(--muted); font-size: 13px; }
.search-input { padding: 8px 12px; border: 1px solid var(--border); border-radius: 8px; font-size: 14px; min-width: 220px; background: var(--surface); }

/* ---------- Buttons ---------- */
.btn { display: inline-flex; align-items: center; gap: 6px; padding: 8px 14px; border-radius: 8px; border: 1px solid transparent; background: var(--primary); color: #fff; font-size: 14px; cursor: pointer; text-decoration: none; transition: filter .12s, background .12s; }
.btn:hover { filter: brightness(.95); }
.btn.secondary { background: var(--surface); color: var(--text); border-color: var(--border); }
.btn.secondary:hover { background: var(--surface-2); }
.btn.danger { background: var(--red); }
.btn.ghost { background: transparent; color: var(--primary); border-color: transparent; padding: 6px 8px; }
.btn.sm { padding: 5px 10px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); }
table.data { width: 100%; border-collapse: collapse; font-size: 14px; }
table.data th, table.data td { padding: 10px 12px; text-align: right; border-bottom: 1px solid var(--border); white-space: nowrap; }
table.data thead th { background: var(--surface-2); font-weight: 600; color: var(--muted); font-size: 12.5px; position: sticky; top: 0; }
table.data tbody tr:hover { background: var(--surface-2); }
table.data td.actions { text-align: left; }
table.data .empty { text-align: center; color: var(--muted); padding: 30px; }
.row-actions { display: inline-flex; gap: 4px; }
.icon-btn { border: none; background: transparent; cursor: pointer; padding: 5px 7px; border-radius: 6px; font-size: 15px; }
.icon-btn:hover { background: var(--gray-bg); }
.icon-btn.del:hover { background: var(--red-bg); }

/* ---------- Badges ---------- */
.badge { display: inline-block; padding: 3px 10px; border-radius: 999px; font-size: 12px; font-weight: 600; }
.badge.green { background: var(--green-bg); color: var(--green); }
.badge.red { background: var(--red-bg); color: var(--red); }
.badge.amber { background: var(--amber-bg); color: var(--amber); }
.badge.blue { background: var(--blue-bg); color: var(--blue); }
.badge.gray { background: var(--gray-bg); color: var(--muted); }

/* ---------- Forms & modal ---------- */
.modal-overlay { position: fixed; inset: 0; background: rgba(15,23,42,.45); display: flex; align-items: flex-start; justify-content: center; padding: 40px 16px; z-index: 100; overflow-y: auto; }
.modal { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow-lg); width: 100%; max-width: 620px; }
.modal.wide { max-width: 900px; }
.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: 17px; }
.modal-head .x { border: none; background: transparent; font-size: 22px; cursor: pointer; color: var(--muted); line-height: 1; }
.modal-body { padding: 20px; }
.modal-foot { padding: 14px 20px; border-top: 1px solid var(--border); display: flex; justify-content: flex-start; gap: 10px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.form-field { display: flex; flex-direction: column; gap: 5px; }
.form-field.full { grid-column: 1 / -1; }
.form-field label { font-size: 13px; color: var(--muted); font-weight: 600; }
.form-field label .req { color: var(--red); }
.form-field input, .form-field select, .form-field textarea {
  padding: 9px 11px; border: 1px solid var(--border); border-radius: 8px; font-size: 14px; font-family: inherit; background: var(--surface); color: var(--text);
}
.form-field input:focus, .form-field select:focus, .form-field textarea:focus { outline: 2px solid var(--primary-light); border-color: var(--primary); }
.form-field textarea { min-height: 70px; resize: vertical; }
.form-field .check { display: flex; align-items: center; gap: 8px; }
.form-field .check input { width: 18px; height: 18px; }
.permset { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 14px; border: 1px solid var(--border); border-radius: 8px; padding: 12px; background: var(--surface-2); }
.perm-item { display: flex; align-items: center; gap: 8px; font-size: 13.5px; cursor: pointer; }
.perm-item input { width: 17px; height: 17px; }
@media (max-width: 620px) { .permset { grid-template-columns: 1fr; } }

/* ---------- Toast ---------- */
#toasts { position: fixed; bottom: 20px; left: 20px; z-index: 200; display: flex; flex-direction: column; gap: 10px; }
.toast { background: #111827; color: #fff; padding: 12px 18px; border-radius: 10px; box-shadow: var(--shadow-lg); font-size: 14px; animation: slidein .2s; }
.toast.success { background: var(--green); }
.toast.error { background: var(--red); }
@keyframes slidein { from { transform: translateY(10px); opacity: 0; } to { transform: none; opacity: 1; } }

/* ---------- Login ---------- */
.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; background: linear-gradient(135deg, var(--primary-dark), var(--primary)); padding: 20px; }
.login-card { background: #fff; border-radius: 16px; box-shadow: var(--shadow-lg); padding: 34px 30px; width: 100%; max-width: 380px; text-align: center; }
.login-card h1 { font-size: 22px; margin: 0 0 4px; }
.login-card .sub { color: var(--muted); font-size: 13px; margin-bottom: 22px; }
.login-card .form-field { text-align: right; margin-bottom: 14px; }
.login-card .btn { width: 100%; justify-content: center; padding: 11px; font-size: 15px; }
.login-err { color: var(--red); font-size: 13px; margin-bottom: 12px; min-height: 18px; }
.login-hint { margin-top: 18px; font-size: 12px; color: var(--muted); }

/* ---------- Misc ---------- */
.muted { color: var(--muted); }
.right { text-align: left; }
.tag-row { display: flex; gap: 8px; flex-wrap: wrap; }
.section-title { font-size: 15px; font-weight: 700; margin: 22px 0 10px; }
.pill-tabs { display: flex; gap: 6px; margin-bottom: 16px; flex-wrap: wrap; }
.pill-tabs button { border: 1px solid var(--border); background: var(--surface); padding: 7px 14px; border-radius: 999px; cursor: pointer; font-size: 13.5px; }
.pill-tabs button.active { background: var(--primary); color: #fff; border-color: var(--primary); }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; font-size: 14px; }
.kv dt { color: var(--muted); }
.progress { height: 8px; background: var(--gray-bg); border-radius: 999px; overflow: hidden; }
.progress > span { display: block; height: 100%; background: var(--primary); }

@media (max-width: 820px) {
  .app-layout { grid-template-columns: 1fr; }
  .sidebar { position: static; height: auto; flex-direction: row; flex-wrap: wrap; }
  .nav { display: flex; flex-wrap: wrap; }
  .grid.cols-2, .grid.cols-3, .grid.cols-4 { grid-template-columns: 1fr; }
  .form-grid { grid-template-columns: 1fr; }
}

/* ---------- Print (protocols / reports) ---------- */
.print-doc { display: none; }
@media print {
  body { background: #fff; }
  .sidebar, .toolbar, .page-head .btn, .no-print, #toasts { display: none !important; }
  .app-layout { display: block; }
  .main { padding: 0; }
  .print-doc { display: block; }
  .print-doc h1 { text-align: center; }
  .print-only { display: block !important; }
  table.data th, table.data td { white-space: normal; }
  @page { margin: 1.6cm; }
}
