:root {
  /* ---- پالت letterhead (کاغذ/جوهر/مهر) ---- */
  --paper: #F1ECE0;
  --paper-deep: #E7E0CE;
  --ink: #1C2A44;
  --ink-soft: #54607A;
  --line: #D8CFB6;
  --card: #FBF8F1;
  --seal-red: #A2322B;
  --seal-red-deep: #7C2521;
  --seal-green: #3C6B4C;
  --seal-green-deep: #2C4F38;
  --brass: #AD8A3F;
  --brass-soft: #D9C793;
  --shadow: 0 10px 30px rgba(28, 42, 68, 0.12);

  --bg: var(--paper);
  --primary: var(--ink);
  --primary-dark: #101a2e;
  --success: var(--seal-green);
  --danger: var(--seal-red);
  --warning: var(--brass);
  --text: var(--ink);
  --muted: var(--ink-soft);
  --border: var(--line);
  --radius: 14px;
  font-family: 'Vazirmatn', Tahoma, sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background:
    radial-gradient(circle at 15% 10%, rgba(173, 138, 63, 0.07), transparent 40%),
    radial-gradient(circle at 85% 90%, rgba(162, 50, 43, 0.06), transparent 45%),
    var(--bg);
  color: var(--text);
  direction: rtl;
  text-align: right;
}
a { color: inherit; }

/* ---------- چیدمان اصلی: سایدبار + محتوا ---------- */
.app-shell { display: flex; min-height: 100vh; }
.sidebar {
  width: 250px;
  background: linear-gradient(180deg, var(--primary-dark), #0a1120);
  color: #fff;
  padding: 20px 16px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
  border-left: 3px double var(--brass);
}
.sidebar h1 { font-size: 15px; margin: 0 0 4px; line-height: 1.6; }
.sidebar .sub { font-size: 11px; color: var(--brass-soft); margin: 0 0 18px; }
.sidebar .nav-group { margin-top: 14px; font-size: 11px; opacity: .6; padding: 0 8px; }
.sidebar a { display: block; padding: 9px 12px; border-radius: 10px; color: #dbe3ff; text-decoration: none; font-size: 14px; margin-bottom: 2px; }
.sidebar a.active, .sidebar a:hover { background: rgba(217, 199, 147, .16); color: var(--brass-soft); }
.sidebar .user-box { margin-top: auto; padding-top: 14px; border-top: 1px solid rgba(255,255,255,.12); font-size: 12.5px; color: #cdd6ee; }

.main-area { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.topbar {
  height: 60px; background: var(--card); border-bottom: 3px double var(--brass);
  display: flex; align-items: center; justify-content: space-between; padding: 0 24px; flex-shrink: 0;
}
.content { padding: 24px; flex: 1; overflow-y: auto; max-width: 1200px; width: 100%; margin: 0 auto; }

/* ---------- کارت‌ها ---------- */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 20px; margin-bottom: 16px; box-shadow: var(--shadow); }
.card h2, .card h3 { margin-top: 0; color: var(--ink); }

.grid { display: grid; gap: 16px; }
.grid-2 { grid-template-columns: repeat(2, 1fr); }
.grid-3 { grid-template-columns: repeat(3, 1fr); }
.grid-4 { grid-template-columns: repeat(4, 1fr); }
@media (max-width: 900px) { .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr; } }

.stat-card { text-align: center; }
.stat-card .value { font-size: 30px; font-weight: 700; color: var(--ink); }
.stat-card .label { color: var(--muted); font-size: 13px; margin-top: 4px; }

/* ---------- دکمه‌ها و فرم‌ها ---------- */
button, .btn {
  font-family: inherit; cursor: pointer; border-radius: 10px; border: 1.5px solid var(--line);
  background: #fff; padding: 9px 16px; font-size: 14px; color: var(--text); transition: .15s;
  font-weight: 600; display: inline-block; text-decoration: none;
}
.btn.primary, button.primary, .btn { background: var(--ink); border-color: var(--ink); color: #F4EFE2; }
.btn:hover, button.primary:hover { filter: brightness(1.15); }
.btn.secondary { background: #fff; border-color: var(--line); color: var(--ink); }
.btn.success { background: var(--seal-green); border-color: var(--seal-green); color: #fff; }
.btn.danger { background: var(--seal-red); border-color: var(--seal-red); color: #fff; }
.btn.small { padding: 5px 12px; font-size: 12px; }
.btn.ghost { background: transparent; border-color: transparent; color: var(--ink); text-decoration: underline; }

input[type=text], input[type=password], input[type=date], input[type=file], select, textarea {
  font-family: inherit; padding: 10px 12px; border-radius: 9px; border: 1.5px solid var(--line);
  font-size: 14px; width: 100%; background: #fff; color: var(--text); margin-bottom: 12px;
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--brass); }
label { font-size: 12.5px; color: var(--ink-soft); display: block; margin-bottom: 6px; font-weight: 700; }

table { width: 100%; border-collapse: collapse; font-size: 13.5px; margin-top: 10px; }
th, td { padding: 9px 10px; text-align: right; border-bottom: 1px solid var(--line); }
th { color: var(--muted); font-weight: 700; background: var(--paper-deep); }
tr:hover td { background: rgba(173, 138, 63, 0.06); }

.badge { display: inline-block; padding: 3px 10px; border-radius: 999px; font-size: 12px; font-weight: 700; }
.badge.done, .badge.green { background: rgba(60, 107, 76, .12); color: var(--seal-green-deep); }
.badge.pending, .badge.red { background: rgba(162, 50, 43, .1); color: var(--seal-red-deep); }
.badge.yellow { background: rgba(173, 138, 63, .16); color: #6b551f; }
.badge.blue { background: rgba(28, 42, 68, .1); color: var(--ink); }
.badge.gray { background: var(--paper-deep); color: var(--muted); }

.error, .error-box { background: rgba(162, 50, 43, .1); color: var(--seal-red-deep); padding: 10px 14px; border-radius: 10px; margin-bottom: 14px; font-size: 13px; border: 1px solid rgba(162, 50, 43, .25); }
.success, .success-box { background: rgba(60, 107, 76, .1); color: var(--seal-green-deep); padding: 10px 14px; border-radius: 10px; margin-bottom: 14px; font-size: 13px; border: 1px solid rgba(60, 107, 76, .25); }

/* ---------- صفحه ورود (letterhead) ---------- */
.center-screen { min-height: 100vh; display: flex; align-items: flex-start; justify-content: center; padding: 60px 14px; }
.auth-wrap { width: 100%; max-width: 420px; }
.letterhead { display: flex; align-items: center; justify-content: center; padding-bottom: 14px; margin-bottom: 20px; border-bottom: 3px double var(--brass); }
.letterhead .brand { display: flex; align-items: center; gap: 10px; }
.brand-mark { width: 38px; height: 38px; border-radius: 50%; border: 2px solid var(--brass); display: flex; align-items: center; justify-content: center; font-weight: 800; color: var(--brass); font-size: 18px; flex: none; }
.brand-text h1 { margin: 0; font-size: 15px; font-weight: 800; }
.brand-text p { margin: 2px 0 0; font-size: 11px; color: var(--ink-soft); }
.paper-card { background: var(--card); border: 1px solid var(--line); border-radius: 14px; box-shadow: var(--shadow); padding: 26px 22px; position: relative; overflow: hidden; }
.paper-card::before { content: ""; position: absolute; inset: 8px; border: 1px dashed rgba(173, 138, 63, .35); border-radius: 8px; pointer-events: none; }
.paper-card h2 { text-align: center; margin: 0 0 6px; font-size: 19px; font-weight: 800; }
.paper-card .sub { text-align: center; color: var(--ink-soft); font-size: 13px; margin: 0 0 22px; }
.paper-icon { width: 60px; height: 60px; border-radius: 50%; background: var(--ink); color: var(--brass-soft); display: flex; align-items: center; justify-content: center; font-size: 24px; margin: 0 auto 16px; font-weight: 800; }
.hint { font-size: 11px; color: var(--ink-soft); text-align: center; margin-top: 12px; line-height: 1.9; }

/* ---------- طیف لیکرت ---------- */
.likert-row { display: flex; justify-content: space-between; gap: 6px; margin: 6px 0 18px; flex-wrap: wrap; }
.likert-option { flex: 1; text-align: center; background: var(--paper-deep); border: 1.5px solid var(--line); border-radius: 8px; padding: 10px 4px; cursor: pointer; font-size: 13px; transition: .15s; }
.likert-option:hover { border-color: var(--brass); }
.likert-option input { display: block; margin: 0 auto 6px; }
.question-box { border-bottom: 1px solid var(--line); padding: 14px 0; }
.question-num { color: var(--ink-soft); font-size: 13px; }

/* ---------- تب‌ها ---------- */
.tabs { display: flex; gap: 6px; margin-bottom: 16px; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.tabs a, .tabs button { border: none; border-radius: 10px 10px 0 0; background: transparent; padding: 10px 16px; text-decoration: none; color: var(--ink-soft); font-weight: 600; font-size: 13.5px; }
.tabs a.active, .tabs button.active { background: var(--card); border: 1px solid var(--border); border-bottom-color: var(--card); margin-bottom: -1px; color: var(--ink); font-weight: 700; }

/* ---------- سایر ---------- */
.small-muted { color: var(--muted); font-size: 13px; }
.qr-card { display: inline-block; text-align: center; border: 1px solid var(--line); border-radius: 8px; padding: 10px; margin: 6px; width: 200px; background: #fff; }
.qr-card img { width: 150px; height: 150px; }
.placeholder-card { border: 2px dashed var(--border); border-radius: var(--radius); padding: 24px; text-align: center; color: var(--muted); }
.flex { display: flex; align-items: center; }
.flex-between { display: flex; align-items: center; justify-content: space-between; }
.gap-8 { gap: 8px; } .gap-12 { gap: 12px; }
.mt-8 { margin-top: 8px; } .mt-16 { margin-top: 16px; } .mb-8 { margin-bottom: 8px; }

/* ---------- نمای درختی ---------- */
.tree-root, .tree-children { list-style: none; margin: 0; padding-right: 20px; }
.tree-root { padding-right: 0; }
.tree-node { margin: 4px 0; }
.tree-label { padding: 6px 10px; border-radius: 8px; border: 1px solid transparent; cursor: grab; display: inline-block; }
.unit-label { background: var(--paper-deep); font-weight: 600; }
.person-label { background: #fff; border-color: var(--line); font-size: 13px; }
.tree-label.drag-over { border-color: var(--brass) !important; background: rgba(173,138,63,.18) !important; }
.tree-node.dragging { opacity: .4; }

/* ---------- کارت شناسایی QR (طرح پیشنهادی) ---------- */
.badge-grid { display: flex; flex-wrap: wrap; gap: 22px; justify-content: flex-start; }
.id-badge {
  width: 380px;
  border-radius: 22px;
  overflow: hidden;
  background: #ffffff;
  box-shadow: 0 8px 24px rgba(20, 40, 90, 0.18);
  border: 1px solid #d7e3f5;
  position: relative;
  page-break-inside: avoid;
}
.id-badge::before {
  content: "";
  position: absolute; top: 0; left: 0; width: 90px; height: 90px;
  background: linear-gradient(135deg, var(--badge-primary, #1c3f7c) 45%, transparent 46%);
  opacity: .9; z-index: 0;
}
.id-badge::after {
  content: "";
  position: absolute; bottom: 0; right: 0; width: 70px; height: 70px;
  background: linear-gradient(-45deg, var(--badge-secondary, #2f6fb0) 45%, transparent 46%);
  opacity: .85; z-index: 0;
}
.id-badge-header {
  position: relative; z-index: 1;
  padding: 18px 18px 14px;
  text-align: center;
  background: #ffffff;
}
.id-badge-header .logo-row { display: flex; align-items: center; justify-content: center; gap: 10px; margin-bottom: 2px; }
.id-badge-header .logo-row img.org-logo { width: 34px; height: 34px; border-radius: 50%; object-fit: cover; }
.id-badge-header .logo-row .org-logo-fallback {
  width: 34px; height: 34px; border-radius: 50%; background: #1c3f7c; color: #fff;
  display: flex; align-items: center; justify-content: center; font-size: 16px;
}
.id-badge-header .org-name { font-size: 17px; font-weight: 800; color: #14264a; }
.id-badge-header .org-subtitle {
  font-size: 11px; letter-spacing: 1.5px; color: #2f6fb0; font-weight: 700;
  margin-top: 2px; direction: ltr;
}
.id-badge-title {
  position: relative; z-index: 1;
  background: linear-gradient(90deg, #14264a, #2f6fb0);
  color: #fff; text-align: center; font-weight: 800; font-size: 14px;
  padding: 8px 10px;
}
.id-badge-body {
  position: relative; z-index: 1;
  display: flex; align-items: center; justify-content: space-between;
  padding: 20px 18px 8px; gap: 14px;
  direction: ltr; /* تا ترتیب QR (چپ) و عکس (راست) مستقل از راست‌چین بودن کلی صفحه ثابت بماند */
}
.id-badge-qr-wrap { position: relative; width: 130px; height: 130px; flex: none; }
.id-badge-qr-wrap img.qr-img { width: 100%; height: 100%; border-radius: 6px; }
.id-badge-qr-wrap .qr-center-logo {
  position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);
  width: 30px; height: 30px; border-radius: 50%; background: #fff;
  border: 2px solid #fff; box-shadow: 0 0 0 1px #d7e3f5;
  display: flex; align-items: center; justify-content: center; overflow: hidden;
}
.id-badge-qr-wrap .qr-center-logo img { width: 100%; height: 100%; object-fit: cover; }
.id-badge-photo-wrap {
  width: 120px; height: 130px; border-radius: 16px; overflow: hidden;
  border: 3px solid #cfe0f5; flex: none; background: #eef3fb;
  display: flex; align-items: center; justify-content: center;
}
.id-badge-photo-wrap img { width: 100%; height: 100%; object-fit: cover; }
.id-badge-photo-wrap .no-photo { font-size: 42px; color: #9fb4d8; }
.id-badge-footer { position: relative; z-index: 1; padding: 6px 20px 18px; }
.id-badge-footer .row { font-size: 13.5px; margin: 3px 0; }
.id-badge-footer .row .label { color: #5b6b8c; font-weight: 700; }
.id-badge-footer .row .value { color: #14264a; font-weight: 800; }
@media print {
  @page { size: landscape; margin: 10mm; }
  .sidebar, .topbar, .card:not(.id-badge), h2 { display: none !important; }
  .content { padding: 0 !important; }
  .badge-grid {
    display: grid !important;
    grid-template-columns: 1fr 1fr;
    grid-auto-rows: min-content;
    gap: 12mm 10mm;
  }
  .id-badge { width: 100%; box-shadow: none; border: 1px solid #ccc; }
  .id-badge:nth-child(4n) { break-after: page; }
}

/* ---------- جدول‌های قابل جستجو/مرتب‌سازی (datatable.js) ---------- */
.dt-toolbar { margin: 10px 0; }
.dt-search {
  max-width: 320px; padding: 8px 12px; border-radius: 8px;
  border: 1.5px solid var(--border); font-size: 13.5px;
}
table.data-table th.dt-sortable { cursor: pointer; user-select: none; position: relative; }
table.data-table th.dt-sortable:hover { color: var(--ink); }
table.data-table th.dt-sortable::after { content: '⇅'; opacity: .35; font-size: 11px; margin-right: 4px; }
table.data-table th.dt-sortable[data-sort-dir="asc"]::after { content: '▲'; opacity: .8; }
table.data-table th.dt-sortable[data-sort-dir="desc"]::after { content: '▼'; opacity: .8; }
