:root {
    --bg: #f4f6f9; --card: #fff; --line: #e3e7ee; --text: #1f2937; --muted: #6b7280;
    --primary: #1d4ed8; --primary-d: #1e40af; --ok: #15803d; --warn: #b45309; --err: #b91c1c;
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text);
       font-family: "Segoe UI", Tahoma, Arial, sans-serif; font-size: 14px; }
h1 { font-size: 22px; margin: 4px 0 16px; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 16px 18px; margin-bottom: 16px; }
.card h2 { font-size: 16px; margin: 0 0 12px; }
.row { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end; }
.field { display: flex; flex-direction: column; gap: 4px; min-width: 180px; }
.field label { color: var(--muted); font-size: 12px; }
.grow { flex: 1; }
input[type=text], select { height: 36px; border: 1px solid #cfd6e0; border-radius: 6px; padding: 0 10px; font: inherit; background: #fff; }
input[type=file] { font: inherit; }
.btn { height: 36px; padding: 0 16px; border-radius: 6px; border: 1px solid transparent; cursor: pointer; font: inherit; background: var(--primary); color: #fff; }
.btn:hover { background: var(--primary-d); }
.btn.secondary { background: #fff; color: var(--text); border-color: #cfd6e0; }
.btn.secondary:hover { background: #f1f5f9; }
.btn.success { background: var(--ok); }
.msg { display: block; padding: 10px 14px; border-radius: 6px; margin-bottom: 14px; }
.msg.ok { background: #dcfce7; color: #14532d; }
.msg.err { background: #fee2e2; color: #7f1d1d; }
.msg.info { background: #e0e7ff; color: #1e3a8a; }
.hint { color: var(--muted); font-size: 12px; margin-top: 8px; }
.chips { display: flex; gap: 8px; flex-wrap: wrap; margin: 6px 0 12px; }
.chip { padding: 6px 12px; border-radius: 20px; background: #f1f5f9; font-size: 13px; }
.chip b { margin-inline-start: 4px; }
.grid-wrap { overflow-x: auto; }
.grid { width: 100%; border-collapse: collapse; }
.grid th { background: #f8fafc; text-align: right; font-weight: 600; color: #374151; padding: 9px 10px; border-bottom: 1px solid var(--line); white-space: nowrap; }
.grid td { padding: 8px 10px; border-bottom: 1px solid #eef1f5; vertical-align: middle; }
.grid tr:hover td { background: #fafcff; }
.grid input[type=text], .grid select { height: 32px; width: 100%; min-width: 160px; }
.grid a { color: var(--primary); text-decoration: none; margin-inline-end: 10px; }
.grid .pager td { border: 0; padding-top: 12px; }
.grid .pager table td { padding: 0 3px; }
.grid .pager a, .grid .pager span { display: inline-block; min-width: 30px; padding: 4px 8px; border-radius: 4px; text-align: center; }
.grid .pager span { background: var(--primary); color: #fff; }
.badge { padding: 2px 10px; border-radius: 12px; font-size: 12px; white-space: nowrap; }
.b-new { background: #dcfce7; color: #166534; }
.b-update { background: #fef3c7; color: #92400e; }
.b-same { background: #f1f5f9; color: #475569; }
.b-error { background: #fee2e2; color: #991b1b; }
.b-active { background: #dcfce7; color: #166534; }
.b-inactive { background: #f1f5f9; color: #6b7280; }
.code { font-family: Consolas, monospace; direction: ltr; unicode-bidi: embed; }
.empty { color: var(--muted); padding: 16px; text-align: center; }

/* ================= التخطيط: قائمة جانبية ================= */
.layout { display: flex; min-height: 100vh; }
.side { width: 230px; flex: 0 0 230px; background: #0f172a; color: #cbd5e1; padding: 0 0 24px; position: sticky; top: 0; height: 100vh; overflow-y: auto; }
.side .brand { color: #fff; font-weight: 700; font-size: 17px; padding: 18px 20px; border-bottom: 1px solid #1e293b; }
.side-title { font-size: 11px; color: #64748b; padding: 18px 20px 6px; letter-spacing: .5px; }
.side a { display: flex; justify-content: space-between; align-items: center; color: #cbd5e1; text-decoration: none; padding: 9px 20px; border-right: 3px solid transparent; }
.side a:hover { background: #1e293b; color: #fff; }
.side a.active { background: #1e293b; color: #fff; border-right-color: #60a5fa; }
.side .cnt { background: #334155; color: #fff; border-radius: 10px; font-size: 11px; padding: 1px 8px; }
.side a.active .cnt { background: #2563eb; }
.page { flex: 1; min-width: 0; padding: 20px 24px; }
@media (max-width: 800px) {
    .layout { flex-direction: column; }
    .side { width: auto; flex: none; height: auto; position: static; display: flex; flex-wrap: wrap; }
    .side .brand, .side-title { width: 100%; }
}

.btn.danger { background: #b91c1c; }
.btn.danger:hover { background: #991b1b; }
.btn[disabled] { opacity: .5; cursor: default; }
.num { direction: ltr; text-align: left; }
.toolbar { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 10px; margin-bottom: 10px; }
.toolbar .chips { margin: 0; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* ================= جدول بشكل إكسل ================= */
.sheet-wrap { overflow: auto; max-height: calc(100vh - 290px); border: 1px solid #d0d7e2; border-radius: 6px; }
.sheet { border-collapse: collapse; width: 100%; font-size: 13px; }
.sheet th { position: sticky; top: 0; z-index: 1; background: #eef2f7; color: #334155; font-weight: 600; padding: 7px 8px; border: 1px solid #d0d7e2; white-space: nowrap; text-align: right; }
.sheet td { border: 1px solid #e2e8f0; padding: 0; height: 32px; background: #fff; }
.sheet td.ro { padding: 0 8px; white-space: nowrap; background: #fafbfc; }
.sheet td.muted { color: #64748b; }
.sheet .c-sel { width: 36px; text-align: center; }
.sheet .c-no { width: 40px; text-align: center; color: #94a3b8; }
.sheet .c-amount { width: 120px; }
.sheet .c-date { width: 150px; }
.sheet .c-chq { width: 130px; }
.sheet .c-notes { min-width: 220px; }
.sheet input.cell { width: 100%; height: 31px; border: 0; border-radius: 0; padding: 0 8px; background: transparent; font: inherit; }
.sheet input.cell:focus { outline: 2px solid #2563eb; outline-offset: -2px; background: #fff; }
.sheet tr:hover td { background: #f8fbff; }
.sheet tr.selected td { background: #eaf2ff; }
.sheet tr.dirty td.c-no { background: #fef3c7; color: #92400e; }
.sheet tr.overdue td.ro:first-of-type, .sheet tr.overdue td.c-no { box-shadow: inset -3px 0 0 #dc2626; }
.sheet tr.due-soon td.c-no { box-shadow: inset -3px 0 0 #f59e0b; }

/* ================= أدوات الجداول: بحث، فلاتر، تصدير ================= */
.tbl-tools { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin: 4px 0 10px; }
.tbl-search { flex: 1 1 320px; max-width: 520px; height: 34px; border: 1px solid #cfd6e0; border-radius: 6px; padding: 0 10px; font: inherit; }
.tbl-spacer { flex: 1; }
.tbl-count { color: #64748b; font-size: 12px; white-space: nowrap; }
.tbl-btn { height: 34px; padding: 0 14px; border-radius: 6px; border: 1px solid transparent; cursor: pointer; font: inherit; color: #fff; }
.tbl-btn.xls { background: #15803d; }
.tbl-btn.xls:hover { background: #166534; }
.tbl-btn.pdf { background: #b91c1c; }
.tbl-btn.pdf:hover { background: #991b1b; }
.tbl-btn.light { background: #fff; color: #334155; border-color: #cfd6e0; visibility: hidden; }
.filter-row th { background: #f8fafc !important; padding: 3px 4px !important; top: 33px; }
.col-filter { width: 100%; min-width: 60px; height: 26px !important; border: 1px solid #dbe1ea; border-radius: 4px; padding: 0 6px; font: inherit; font-size: 12px; font-weight: 400; background: #fff; }
.col-filter:focus { outline: 2px solid #93c5fd; outline-offset: -1px; }
tr.flt-hidden { display: none; }

/* شارات حالات الشيك */
.st { padding: 2px 10px; border-radius: 12px; font-size: 12px; white-space: nowrap; }
.st-1 { background: #f1f5f9; color: #475569; }
.st-2 { background: #fef3c7; color: #92400e; }
.st-3 { background: #dbeafe; color: #1e40af; }
.st-4 { background: #ede9fe; color: #5b21b6; }
.st-5 { background: #ffedd5; color: #9a3412; }
.st-6 { background: #dcfce7; color: #166534; }
.late { color: #b91c1c; font-weight: 600; }

/* واتساب */
.sheet .c-wa { width: 70px; text-align: center; white-space: nowrap; }
.wa { display: inline-flex; align-items: center; justify-content: center; width: 30px; height: 26px; border-radius: 6px; vertical-align: middle; }
a.wa { color: #fff; background: #16a34a; }
a.wa:hover { background: #15803d; }
.wa-off { color: #cbd5e1; background: #f1f5f9; cursor: not-allowed; }
.wa-ok { display: inline-block; color: #16a34a; font-weight: 700; font-size: 16px; margin-inline-start: 6px; vertical-align: middle; }
.wa-ok[hidden] { display: none; }

/* الشيك المستعجل */
.urgent-box { justify-content: flex-end; height: 36px; padding: 0 10px; border: 1px solid #fecaca; background: #fef2f2; border-radius: 6px; color: #991b1b; font-weight: 600; min-width: 0; }
.urgent-box label { color: #991b1b; font-size: 14px; }
.urgent-tag { display: inline-block; background: #dc2626; color: #fff; font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 10px; white-space: nowrap; }
.sheet .c-urgent { width: 70px; text-align: center; }
.sheet tr.urgent td, .sheet tr.urgent td.ro, .grid tr.urgent td { background: #fff1f2; }
.sheet tr.urgent td.c-no { box-shadow: inset -3px 0 0 #dc2626; color: #b91c1c; font-weight: 700; }
.sheet tr.urgent.selected td { background: #ffe4e6; }

/* ================= توسيط كل الجداول ================= */
.grid th, .grid td, .sheet th, .sheet td, .filter-row th { text-align: center; }
.sheet td.ro { text-align: center; }
.sheet input.cell, .grid input[type=text], .grid select, .col-filter { text-align: center; }
.sheet input.cell.num { text-align: center; }
.grid td.c-code, .sheet td.c-code { width: 110px; max-width: 120px; }
.sheet .c-code { width: 110px; }

/* ================= صور مطابقة الحساب ================= */
.attach-link { color: var(--primary); text-decoration: none; font-weight: 600; white-space: nowrap; }
.attach-link.none { color: #b45309; }
.sheet .c-attach { width: 110px; }
.attach-box { margin-top: 14px; border: 1px solid #c7d2fe; background: #f5f7ff; border-radius: 8px; padding: 12px 14px; }
.attach-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-bottom: 10px; }
.attach-head h3 { margin: 0; font-size: 15px; }
.thumbs { display: flex; flex-wrap: wrap; gap: 12px; margin-top: 12px; }
.thumb { width: 170px; background: #fff; border: 1px solid #dbe1ea; border-radius: 8px; padding: 8px; text-align: center; }
.thumb img { width: 100%; height: 120px; object-fit: cover; border-radius: 4px; display: block; }
.thumb-name { font-size: 12px; margin-top: 6px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; direction: ltr; }
.thumb-meta { font-size: 11px; color: #64748b; }
.thumb-del { color: #b91c1c; font-size: 12px; display: inline-block; margin-top: 4px; }

/* =====================================================================
   مظهر الجداول الموحّد (قراءة أسهل): نص أسود، تناوب واضح، عنوان وإجمالي مميزان
   ===================================================================== */
.grid, .sheet { border-collapse: collapse; width: 100%; font-size: 13px; }
.grid th, .sheet th {
    background: #c9daf0 !important; color: #000 !important; font-weight: 600;
    border: 1px solid #9fb6d6; padding: 9px 6px; text-align: center; white-space: nowrap;
}
.grid td, .sheet td, .sheet td.ro, .sheet td.muted, .grid td.muted {
    color: #000 !important; background: #fff; border: 1px solid #d4dce8;
    height: 38px; padding: 0 6px; text-align: center; vertical-align: middle;
}
.grid tr.alt td, .sheet tr.alt td, .sheet tr.alt td.ro { background: #e8f0fb; }
.grid td.t, .sheet td.t { text-align: right; padding-right: 10px; }
.grid tr:hover td, .sheet tr:hover td, .sheet tr:hover td.ro { background: #fff4cc; }

/* المستعجل والمحدد فوق التناوب */
.grid tr.urgent td, .sheet tr.urgent td, .sheet tr.urgent td.ro { background: #fde2e2; }
.sheet tr.selected td, .sheet tr.selected td.ro { background: #cfe0f7; }
.sheet tr.urgent.selected td { background: #f9c9c9; }

/* الخلايا القابلة للتعديل: مربع أبيض بإطار أزرق */
.sheet input.cell {
    width: calc(100% - 4px); height: 28px; margin: 0 2px; padding: 0 6px;
    background: #fff !important; color: #000; border: 1px solid #9fb6d6; border-radius: 4px; text-align: center;
}
.sheet td.t input.cell { text-align: right; }
.sheet input.cell:hover { border-color: #5b86c5; }
.sheet input.cell:focus { outline: none; border: 2px solid #1d4ed8; }
.sheet input.cell::placeholder { color: #9aa9bd; }
.sheet input.cell.is-empty:not(:focus)::-webkit-datetime-edit { color: transparent; }

/* الفلاتر: مخفية افتراضياً، والعمود المفلتر يتميّز */
table.flt-collapsed .filter-row { display: none; }
.filter-row th { background: #e3ecf8 !important; padding: 4px !important; }
.col-filter { background: #fff url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%23667' stroke-width='2.5'><circle cx='10' cy='10' r='7'/><path d='M21 21l-6-6'/></svg>") no-repeat 6px center; padding-left: 22px !important; color: #000; }
.grid th.flt-on, .sheet th.flt-on { background: #fde68a !important; }
.tbl-btn.light2 { background: #fff; color: #000; border: 1px solid #9fb6d6; }
.tbl-btn.light2:hover { background: #e8f0fb; }

/* سطر الإجمالي */
.grid tfoot td.tf, .sheet tfoot td.tf { background: #c9daf0 !important; color: #000 !important; font-weight: 600; border: 1px solid #9fb6d6; height: 36px; }

/* كل النصوص داخل الخلايا سوداء: الروابط والشارات */
.grid td a, .sheet td a, .attach-link, .attach-link.none { color: #000 !important; text-decoration: underline; font-weight: 600; }
.sheet td a.wa { color: #fff !important; text-decoration: none; }
.badge, .st, .b-new, .b-update, .b-same, .b-error, .b-active, .b-inactive,
.st-1, .st-2, .st-3, .st-4, .st-5, .st-6 { color: #000 !important; }
.late { color: #000 !important; font-weight: 700; }
.sheet tr.overdue td.c-no, .sheet tr.due-soon td.c-no, .sheet tr.urgent td.c-no, .sheet td.c-no { color: #000 !important; }
