/* Scalos Books - نظام تصميم مبني على ألوان اللوجو (أزرق #1B23E0 + أصفر #FFC20E) */

:root {
  --blue: #1b23e0;
  --blue-600: #151bb4;
  --blue-50: #eef0ff;
  --yellow: #ffc20e;
  --yellow-50: #fff8e3;
  --green: #10a674;
  --green-50: #e7f7f1;
  --red: #e0374a;
  --red-50: #fdeef0;

  --ink: #14162b;
  --ink-2: #4a4f6a;
  --ink-3: #858aa6;
  --line: #e6e8f2;
  --bg: #f5f6fb;
  --card: #ffffff;

  --radius: 14px;
  --radius-sm: 9px;
  --shadow: 0 1px 2px rgba(20, 22, 43, .05), 0 8px 24px rgba(20, 22, 43, .06);
  --shadow-lg: 0 12px 48px rgba(20, 22, 43, .18);
  --sidebar: 250px;
  --font: "Segoe UI", "Tajawal", "Cairo", Tahoma, system-ui, sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  font-family: var(--font);
  background: var(--bg);
  color: var(--ink);
  font-size: 15px;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--blue); text-decoration: none; }
button { font-family: inherit; }

/* ---------- التخطيط ---------- */
.layout { display: flex; min-height: 100vh; }

.sidebar {
  width: var(--sidebar);
  flex: 0 0 var(--sidebar);
  background: var(--card);
  border-inline-start: 1px solid var(--line);
  padding: 20px 14px;
  position: sticky;
  top: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
  gap: 22px;
}

.brand-logo { display: flex; align-items: center; gap: 10px; padding: 4px 8px 0; }
.brand-logo img { width: 130px; height: auto; }
.brand-logo .sub {
  font-size: 12px; color: var(--ink-3); font-weight: 600; letter-spacing: .3px;
}

.nav { display: flex; flex-direction: column; gap: 3px; }
.nav a {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px; border-radius: var(--radius-sm);
  color: var(--ink-2); font-weight: 600; font-size: 14.5px;
  transition: background .15s, color .15s;
}
.nav a:hover { background: var(--bg); color: var(--ink); }
.nav a.active { background: var(--blue-50); color: var(--blue); }
.nav a .ic { width: 20px; text-align: center; font-size: 16px; }

.sidebar-foot { margin-top: auto; font-size: 12px; color: var(--ink-3); padding: 0 10px; line-height: 1.7; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }

.topbar {
  position: sticky; top: 0; z-index: 20;
  background: rgba(245, 246, 251, .92);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
  padding: 14px 26px;
  display: flex; align-items: center; gap: 14px; flex-wrap: wrap;
}
.topbar h1 { font-size: 20px; margin: 0; font-weight: 700; }
.topbar .spacer { flex: 1; }

.content { padding: 22px 26px 60px; }

/* ---------- عناصر عامة ---------- */
.card {
  background: var(--card); border: 1px solid var(--line);
  border-radius: var(--radius); box-shadow: var(--shadow);
}
.card-pad { padding: 18px; }
.card-head {
  padding: 15px 18px; border-bottom: 1px solid var(--line);
  display: flex; align-items: center; gap: 12px;
}
.card-head h2 { margin: 0; font-size: 15.5px; font-weight: 700; }
.card-head .spacer { flex: 1; }

.grid { display: grid; gap: 16px; }
.grid.kpi { grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); }
.grid.brands { grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); }
.grid.two { grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); }
.stack { display: flex; flex-direction: column; gap: 16px; }
.row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }

.muted { color: var(--ink-3); }
.small { font-size: 12.5px; }
.nowrap { white-space: nowrap; }
.center { text-align: center; }
/* أرقام لاتينية بمحاذاة موحّدة — لا تضع display هنا حتى لا تنكسر خلايا الجداول */
.num { font-variant-numeric: tabular-nums; font-feature-settings: "tnum"; direction: ltr; }
span.num, div.num { display: inline-block; }
td.num, th.num { display: table-cell; text-align: right; }

.pos { color: var(--green); }
.neg { color: var(--red); }

/* ---------- أزرار ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 7px;
  padding: 9px 15px; border-radius: var(--radius-sm);
  border: 1px solid var(--line); background: var(--card); color: var(--ink);
  font-size: 14px; font-weight: 600; cursor: pointer;
  transition: background .15s, border-color .15s, transform .08s;
}
.btn:hover { background: var(--bg); }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--blue); border-color: var(--blue); color: #fff; }
.btn.primary:hover { background: var(--blue-600); }
.btn.accent { background: var(--yellow); border-color: var(--yellow); color: #3a2c00; }
.btn.danger { color: var(--red); border-color: #f2c9cf; background: var(--red-50); }
.btn.danger:hover { background: #fbdde1; }
.btn.sm { padding: 6px 10px; font-size: 13px; }
.btn.icon { padding: 6px 9px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* ---------- حقول ---------- */
label.field { display: flex; flex-direction: column; gap: 6px; font-size: 13px; font-weight: 600; color: var(--ink-2); }
input, select, textarea {
  font-family: inherit; font-size: 14px; color: var(--ink);
  padding: 9px 11px; border: 1px solid var(--line); border-radius: var(--radius-sm);
  background: #fff; width: 100%;
}
input:focus, select:focus, textarea:focus {
  outline: none; border-color: var(--blue); box-shadow: 0 0 0 3px var(--blue-50);
}
input[type="date"] { direction: ltr; text-align: right; }
textarea { resize: vertical; min-height: 70px; }

.seg { display: inline-flex; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius-sm); overflow: hidden; }
.seg button {
  border: 0; background: transparent; padding: 8px 13px; font-size: 13.5px; font-weight: 600;
  color: var(--ink-2); cursor: pointer;
}
.seg button.active { background: var(--blue); color: #fff; }

/* ---------- بطاقات المؤشرات ---------- */
.kpi-card { padding: 16px 18px; }
.kpi-card .label { font-size: 13px; color: var(--ink-3); font-weight: 600; }
.kpi-card .value { font-size: 26px; font-weight: 800; margin-top: 6px; letter-spacing: -.5px; }
.kpi-card .hint { font-size: 12.5px; color: var(--ink-3); margin-top: 5px; }
.kpi-card.accent-blue { border-top: 3px solid var(--blue); }
.kpi-card.accent-green { border-top: 3px solid var(--green); }
.kpi-card.accent-red { border-top: 3px solid var(--red); }
.kpi-card.accent-yellow { border-top: 3px solid var(--yellow); }

/* ---------- بطاقة علامة تجارية ---------- */
.brand-card { padding: 16px 18px; position: relative; overflow: hidden; }
.brand-card .dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.brand-card .name { font-weight: 800; font-size: 16px; }
.brand-card .lines { margin-top: 12px; display: flex; flex-direction: column; gap: 7px; font-size: 13.5px; }
.brand-card .lines div { display: flex; justify-content: space-between; gap: 10px; }
.brand-card .net { border-top: 1px dashed var(--line); padding-top: 8px; font-weight: 800; font-size: 15px; }
.bar { height: 6px; border-radius: 4px; background: var(--bg); overflow: hidden; margin-top: 12px; }
.bar > span { display: block; height: 100%; background: var(--green); }

/* ---------- جداول ---------- */
.table-wrap { overflow-x: auto; }
table.data { width: 100%; border-collapse: collapse; font-size: 14px; }
table.data th, table.data td { padding: 11px 12px; text-align: right; border-bottom: 1px solid var(--line); }
table.data th { font-size: 12.5px; color: var(--ink-3); font-weight: 700; background: #fbfbfe; position: sticky; top: 0; }
table.data tbody tr:hover { background: #fafbff; }
table.data tfoot td { font-weight: 800; background: #fbfbfe; border-bottom: 0; }
table.data td.actions { white-space: nowrap; width: 1%; }
table.data .rowlink { cursor: pointer; }

.chip {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 3px 9px; border-radius: 999px; font-size: 12px; font-weight: 700;
  background: var(--bg); color: var(--ink-2); white-space: nowrap;
}
.chip.income { background: var(--green-50); color: var(--green); }
.chip.expense { background: var(--red-50); color: var(--red); }
.chip.transfer { background: var(--blue-50); color: var(--blue); }
.chip.group { background: var(--yellow-50); color: #8a6400; }
.chip.pending { background: var(--yellow-50); color: #8a6400; }
.chip.dot-brand::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }

/* ---------- نافذة منبثقة ---------- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(20, 22, 43, .45);
  display: flex; align-items: flex-start; justify-content: center;
  padding: 40px 16px; z-index: 100; overflow-y: auto;
}
.modal {
  background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow-lg);
  width: min(680px, 100%); animation: pop .16s ease-out;
}
.modal.narrow { width: min(430px, 100%); }
@keyframes pop { from { transform: translateY(-8px); opacity: 0; } }
.modal header {
  padding: 16px 20px; border-bottom: 1px solid var(--line);
  display: flex; align-items: center; gap: 12px;
}
.modal header h3 { margin: 0; font-size: 17px; }
.modal .body { padding: 20px; display: grid; gap: 14px; }
.modal footer {
  padding: 14px 20px; border-top: 1px solid var(--line);
  display: flex; gap: 10px; justify-content: flex-start;
}
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.form-grid .full { grid-column: 1 / -1; }
.x-close { border: 0; background: transparent; font-size: 22px; cursor: pointer; color: var(--ink-3); line-height: 1; }

/* ---------- تنبيهات ---------- */
#toasts { position: fixed; bottom: 20px; inset-inline-start: 20px; z-index: 200; display: flex; flex-direction: column; gap: 10px; }
.toast {
  background: var(--ink); color: #fff; padding: 12px 16px; border-radius: var(--radius-sm);
  box-shadow: var(--shadow-lg); font-size: 14px; font-weight: 600; max-width: 380px;
  animation: pop .16s ease-out;
}
.toast.err { background: var(--red); }
.toast.ok { background: var(--green); }

.empty { padding: 46px 20px; text-align: center; color: var(--ink-3); }
.empty .big { font-size: 34px; margin-bottom: 8px; }

.alert { padding: 12px 14px; border-radius: var(--radius-sm); font-size: 13.5px; line-height: 1.7; }
.alert.info { background: var(--blue-50); color: var(--blue-600); }
.alert.warn { background: var(--yellow-50); color: #7a5900; }

/* ---------- رسم بياني ---------- */
.chart { width: 100%; height: 230px; }
.legend { display: flex; gap: 16px; font-size: 12.5px; color: var(--ink-2); flex-wrap: wrap; }
.legend i { width: 10px; height: 10px; border-radius: 3px; display: inline-block; margin-inline-end: 5px; }

.alloc-row { display: grid; grid-template-columns: 1fr 110px; gap: 10px; align-items: center; }

/* ---------- شاشات صغيرة ---------- */
@media (max-width: 860px) {
  .layout { flex-direction: column; }
  .sidebar {
    width: 100%; flex: none; height: auto; position: static;
    border-inline-start: 0; border-bottom: 1px solid var(--line);
    flex-direction: row; align-items: center; gap: 14px; overflow-x: auto; padding: 12px;
  }
  .nav { flex-direction: row; }
  .sidebar-foot { display: none; }
  .brand-logo img { width: 96px; }
  .content, .topbar { padding-inline: 14px; }
  .form-grid { grid-template-columns: 1fr; }
}

/* ---------- منشئ الفواتير ---------- */
.inv-item-row {
  display: grid;
  grid-template-columns: 1fr 90px 120px 110px 40px;
  gap: 8px; align-items: center; margin-bottom: 8px;
}
.inv-item-row.head {
  font-size: 12px; color: var(--ink-3); font-weight: 700; margin-bottom: 6px;
}
.inv-item-row.head span:nth-child(4) { text-align: left; }
.inv-item-row > .num { text-align: left; }

.inv-totals {
  margin-inline-start: auto; width: min(300px, 100%);
  display: flex; flex-direction: column; gap: 6px;
  background: var(--bg); padding: 12px 14px; border-radius: var(--radius-sm);
  font-size: 14px;
}
.inv-totals > div { display: flex; justify-content: space-between; gap: 12px; }
.inv-totals .grand {
  border-top: 1px solid var(--line); padding-top: 8px; margin-top: 2px;
  font-weight: 800; font-size: 16px;
}

/* ---------- ورقة الفاتورة المطبوعة ---------- */
.print-body { background: #6b6f85; margin: 0; }
.print-bar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 12px;
  padding: 12px 20px; background: var(--card); border-bottom: 1px solid var(--line);
}

.sheet {
  position: relative;
  width: 210mm; min-height: 297mm;
  margin: 22px auto; background: #fff;
  box-shadow: 0 10px 40px rgba(0, 0, 0, .25);
  padding: 42mm 20mm 46mm;      /* مساحة ترويسة سكالوس أعلى وأسفل */
  font-family: "Segoe UI", Tahoma, Arial, sans-serif;
  color: #1b1b2b;
  direction: ltr; text-align: left;
}
/* صورة الترويسة تُضبط من JS حسب العلامة التجارية للمستند */
.sheet.letterhead {
  background-size: 100% 100%;
  background-repeat: no-repeat;
}
.sheet:not(.letterhead) { padding: 22mm 20mm 24mm; }
.sheet:not(.letterhead) .inv-footer { display: block; }
.inv-footer {
  display: none;
  margin-top: 26px; padding-top: 12px; border-top: 1px solid #dcdfe9;
  font-size: 10.5px; color: #6b7086; line-height: 1.7;
}

.inv-watermark {
  position: absolute; top: 34mm; inset-inline-end: 18mm;
  border: 2px solid #e0374a; color: #e0374a;
  padding: 4px 14px; border-radius: 6px;
  font-weight: 800; font-size: 13px; letter-spacing: 1px;
  transform: rotate(-6deg);
}

.inv-org { font-size: 15px; font-weight: 700; color: #1b23e0; }
.inv-date { font-size: 12.5px; color: #4a4f6a; margin-top: 2px; }
.inv-title { font-size: 26px; margin: 14px 0 18px; font-weight: 800; letter-spacing: -.4px; }
.inv-bill { font-size: 12.5px; line-height: 1.95; margin-bottom: 16px; }
.inv-info b { color: #4a4f6a; font-weight: 700; }
.inv-subject {
  font-size: 13px; font-weight: 700; margin-bottom: 10px;
  padding: 7px 10px; background: #eef0ff; border-radius: 6px; display: inline-block;
}

table.inv-table { width: 100%; border-collapse: collapse; font-size: 12.5px; }
table.inv-table th {
  background: #1b23e0; color: #fff; font-weight: 700;
  padding: 8px 10px; text-align: left; border: 1px solid #1b23e0;
}
table.inv-table td { padding: 8px 10px; border: 1px solid #d8dbe8; vertical-align: top; }
table.inv-table th:first-child, table.inv-table td:first-child { width: 46px; text-align: center; }
table.inv-table th:nth-child(2), table.inv-table td:nth-child(2) { width: 56px; text-align: center; }
table.inv-table th:nth-child(4), table.inv-table td:nth-child(4),
table.inv-table th:last-child,   table.inv-table td:last-child { width: 110px; text-align: right; }

.inv-summary {
  margin: 14px 0 0 auto; width: 260px; font-size: 12.5px;
}
.inv-summary > div { display: flex; justify-content: space-between; padding: 5px 0; }
.inv-summary .inv-grand {
  border-top: 2px solid #1b23e0; margin-top: 4px; padding-top: 8px;
  font-size: 15px; font-weight: 800; color: #1b23e0;
}
.inv-notes { margin-top: 20px; font-size: 12px; line-height: 1.8; }
.inv-terms {
  margin-top: 10px; font-size: 11px; color: #6b7086; line-height: 1.8;
  white-space: pre-line;
}

/* ---------- سند القبض ---------- */
.receipt-sheet { direction: rtl; text-align: right; }

.rcp-head { display: flex; align-items: flex-start; gap: 16px; margin-bottom: 22px; }
.rcp-org { font-size: 15px; font-weight: 700; color: #1b23e0; }
.rcp-kind {
  font-size: 26px; font-weight: 800; margin-top: 6px; letter-spacing: -.4px;
  border-bottom: 3px solid #ffc20e; display: inline-block; padding-bottom: 3px;
}
.rcp-meta { margin-inline-start: auto; font-size: 12.5px; line-height: 2; text-align: left; direction: ltr; }
.rcp-meta b { color: #4a4f6a; }

.rcp-amount {
  display: flex; align-items: center; gap: 14px;
  background: #eef0ff; border: 1.5px solid #1b23e0; border-radius: 10px;
  padding: 12px 18px; margin-bottom: 12px;
}
.rcp-amount-label { font-size: 13px; font-weight: 700; color: #4a4f6a; }
.rcp-amount-value {
  font-size: 26px; font-weight: 800; color: #1b23e0;
  margin-inline-start: auto; direction: ltr;
  font-variant-numeric: tabular-nums;
}
.rcp-words {
  font-size: 13px; line-height: 1.9; margin-bottom: 20px;
  padding: 9px 12px; background: #fff8e3; border-radius: 8px;
}
.rcp-words b { color: #7a5900; }

.rcp-body { font-size: 13px; }
.rcp-line {
  display: flex; gap: 10px; align-items: baseline;
  padding: 9px 0; border-bottom: 1px dotted #d8dbe8;
}
.rcp-label { flex: 0 0 165px; color: #4a4f6a; font-weight: 700; }
.rcp-value { flex: 1; font-weight: 600; }
.rcp-notes { margin-top: 16px; font-size: 12px; line-height: 1.8; }

.rcp-signatures { display: flex; gap: 40px; margin-top: 46px; }
.rcp-sign { flex: 1; text-align: center; }
.rcp-sign-label { font-size: 12px; color: #6b7086; font-weight: 700; }
.rcp-sign-name { font-size: 13px; font-weight: 600; min-height: 26px; padding-top: 8px; }
.rcp-sign-line { border-top: 1px solid #9aa0b8; margin-top: 4px; }

/* شريط توزيع التعاقدات على العلامات */
.contract-bar {
  display: flex; height: 12px; border-radius: 6px; overflow: hidden;
  background: var(--bg); gap: 2px;
}
.contract-bar > span { display: block; height: 100%; min-width: 3px; }

@page { size: A4; margin: 0; }

@media print {
  .print-body { background: #fff; }
  .print-bar { display: none !important; }
  .sheet {
    margin: 0; box-shadow: none; width: 210mm; min-height: 297mm;
    -webkit-print-color-adjust: exact; print-color-adjust: exact;
  }
  .inv-watermark { display: none !important; }
}

/* ---------- وضع الاطّلاع فقط (صلاحية viewer) ---------- */
/* نخفي كل أزرار الإجراء الأساسية والحذف حتى لا يصطدم المستخدم برسائل رفض */
body.readonly .btn.primary,
body.readonly .btn.accent,
body.readonly .btn.danger { display: none !important; }

.readonly-badge { background: var(--yellow-50); color: #7a5900; }

/* ---------- صفحة الدخول ---------- */
.auth-body {
  background: linear-gradient(150deg, #f5f6fb 0%, #eef0ff 55%, #fff8e3 100%);
  min-height: 100vh;
}
.auth-shell {
  min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 24px;
}
.auth-card { width: min(410px, 100%); padding: 30px 28px; text-align: center; }
.auth-logo { width: 150px; height: auto; }
.auth-sub { margin: -6px 0 20px; font-size: 12.5px; color: var(--ink-3); font-weight: 700; letter-spacing: .4px; }
.auth-card h1 { font-size: 20px; margin: 0; }
.auth-card #auth-hint { margin: 0 0 4px; line-height: 1.7; }
.auth-card form { text-align: right; gap: 14px; }
.auth-foot { margin: 18px 0 0; }

/* ---------- بطاقة عميل ---------- */
.client-card { padding: 16px 18px; display: flex; flex-direction: column; gap: 10px; cursor: pointer; transition: border-color .15s, transform .08s; }
.client-card:hover { border-color: var(--blue); transform: translateY(-1px); }
.client-card .top { display: flex; align-items: flex-start; gap: 10px; }
.client-card .avatar {
  width: 40px; height: 40px; border-radius: 11px; flex: 0 0 40px;
  display: grid; place-items: center; font-weight: 800; font-size: 15px;
  background: var(--blue-50); color: var(--blue);
}
.client-card .name { font-weight: 800; font-size: 15.5px; line-height: 1.35; }
.client-card .facts { display: flex; flex-direction: column; gap: 5px; font-size: 13px; }
.client-card .facts div { display: flex; justify-content: space-between; gap: 10px; }

.profile-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 14px; }
.profile-item .k { font-size: 12px; color: var(--ink-3); font-weight: 700; }
.profile-item .v { font-size: 14px; margin-top: 3px; word-break: break-word; }

/* ---------- منتقي الفترة ---------- */
.period-box {
  display: inline-flex; align-items: center; gap: 6px;
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius-sm); padding: 3px 8px;
}
.period-box input[type="date"] {
  border: 0; padding: 5px 4px; width: 138px; background: transparent; font-size: 13.5px;
}
.period-box input[type="date"]:focus { box-shadow: none; }
.period-box .sep { color: var(--ink-3); font-size: 13px; }
.period-box .cal { font-size: 14px; color: var(--ink-3); }

@media print {
  .sidebar, .topbar .btn, .no-print { display: none !important; }
  body { background: #fff; }
  .card { box-shadow: none; }
}

/* صف القالب المتكرر داخل ملف العميل */
.tmpl-row {
  padding: 9px 12px; border: 1px dashed var(--line); border-radius: var(--radius-sm);
  background: #fbfbfe; font-size: 13.5px;
}

/* معاينة ترويسة العلامة في الإعدادات */
.lh-preview {
  border: 1px dashed var(--line); border-radius: var(--radius-sm);
  background: #fbfbfe; padding: 10px; text-align: center; min-height: 90px;
  display: flex; align-items: center; justify-content: center;
}
.lh-preview img { max-height: 220px; width: auto; box-shadow: var(--shadow); }
.lh-empty { font-size: 12.5px; color: var(--ink-3); }

.lh-thumb { display: inline-block; line-height: 0; }
.lh-thumb img {
  height: 46px; width: auto; border: 1px solid var(--line);
  border-radius: 4px; background: #fff;
}
.lh-thumb:hover img { border-color: var(--blue); }
