:root {
    --primary: #1f4e78;
    --primary-2: #2e6aa0;
    --bg: #f4f6f9;
    --card: #ffffff;
    --text: #1d2733;
    --muted: #6b7684;
    --border: #dfe4ea;
    --green: #1e8e3e;
    --green-bg: #e6f4ea;
    --red: #c5221f;
    --red-bg: #fce8e6;
    --yellow: #b06000;
    --yellow-bg: #fef7e0;
    --radius: 12px;
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
    margin: 0;
    font-family: 'Cairo', system-ui, -apple-system, 'Segoe UI', Tahoma, sans-serif;
    font-size: 16px;
    line-height: 1.6;
    background: var(--bg);
    color: var(--text);
}

a { color: var(--primary-2); text-decoration: none; }
a:hover { text-decoration: underline; }

h1 { font-size: 1.5rem; margin: 0 0 1rem; }
h2 { font-size: 1.15rem; margin: 0; }

.muted { color: var(--muted); }
small.muted { font-weight: 400; }

/* ---------- الشريط العلوي ---------- */
.topbar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: .25rem 1rem;
    padding: .6rem 1rem;
    background: var(--primary);
    color: #fff;
    position: sticky;
    top: 0;
    z-index: 10;
}
.topbar a { color: #fff; }
.brand { font-weight: 700; white-space: nowrap; }
.nav { display: flex; gap: .25rem; flex: 1; overflow-x: auto; }
.nav a { padding: .35rem .75rem; border-radius: 8px; white-space: nowrap; }
.nav a.active, .nav a:hover { background: rgba(255, 255, 255, .15); text-decoration: none; }

.usermenu { position: relative; }
.usermenu summary { cursor: pointer; list-style: none; white-space: nowrap; padding: .35rem .5rem; }
.usermenu summary::-webkit-details-marker { display: none; }
.usermenu summary::after { content: ' ▾'; }
.usermenu-body {
    position: absolute;
    left: 0;
    top: 110%;
    background: var(--card);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: 0 8px 24px rgba(0, 0, 0, .12);
    padding: .75rem;
    min-width: 190px;
    display: flex;
    flex-direction: column;
    gap: .5rem;
}
.usermenu-body a { color: var(--primary-2); }

/* ---------- المحتوى ---------- */
.container { max-width: 960px; margin: 0 auto; padding: 1.25rem 1rem 3rem; }

.card {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.25rem;
    margin-bottom: 1rem;
}
.card.narrow { max-width: 520px; }
.card.empty { text-align: center; padding: 2.5rem 1rem; }
.card.upload-first { border: 2px solid var(--primary-2); }

.page-head { display: flex; align-items: center; justify-content: space-between; gap: 1rem; margin-bottom: 1rem; flex-wrap: wrap; }
.page-head h1 { margin: 0; }

.project-card { display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap; }
.project-card p { margin: .25rem 0 0; font-size: .9rem; }

/* ---------- الفورم ---------- */
.form { display: flex; flex-direction: column; gap: 1rem; }
.form label { display: flex; flex-direction: column; gap: .35rem; font-weight: 600; }
.form label.check { flex-direction: row; align-items: center; gap: .5rem; font-weight: 400; }
fieldset { border: 1px solid var(--border); border-radius: var(--radius); padding: .75rem 1rem 1rem; margin: 0; display: flex; flex-direction: column; gap: .75rem; }
legend { font-weight: 600; padding: 0 .35rem; }

input[type=text], input[type=url], input[type=password], input[type=number], input[type=time], select {
    font: inherit;
    padding: .6rem .75rem;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: #fff;
    width: 100%;
    min-height: 44px;
}
input:focus, select:focus { outline: 2px solid var(--primary-2); outline-offset: 1px; border-color: transparent; }
input[type=checkbox] { width: 20px; height: 20px; accent-color: var(--primary); }

.days { display: flex; flex-wrap: wrap; gap: .5rem 1.25rem; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
.actions { display: flex; gap: .75rem; flex-wrap: wrap; }

/* ---------- الأزرار ---------- */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: .35rem;
    font: inherit;
    font-weight: 600;
    min-height: 44px;
    padding: .5rem 1.1rem;
    border-radius: 10px;
    border: 1px solid var(--border);
    background: #fff;
    color: var(--text);
    cursor: pointer;
    text-decoration: none;
}
.btn:hover { text-decoration: none; background: #f0f3f7; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-2); }
.btn.block { width: 100%; }
.linklike { background: none; border: 0; padding: 0; font: inherit; color: var(--red); cursor: pointer; text-align: right; }

/* ---------- التنبيهات ---------- */
.alert { border-radius: var(--radius); padding: .75rem 1rem; margin-bottom: 1rem; }
.alert ul { margin: 0; padding-inline-start: 1.25rem; }
.alert.success { background: var(--green-bg); color: var(--green); }
.alert.error { background: var(--red-bg); color: var(--red); }
.alert.info { background: #e8f0f8; color: var(--primary); }

.badge { display: inline-block; padding: .1rem .6rem; border-radius: 999px; font-size: .85rem; font-weight: 600; }
.badge.green { background: var(--green-bg); color: var(--green); }
.badge.gray { background: #eceff3; color: var(--muted); }

/* ---------- الجداول ---------- */
.table-wrap { padding: 0; overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { padding: .75rem 1rem; text-align: right; border-bottom: 1px solid var(--border); white-space: nowrap; }
.table th { font-size: .9rem; color: var(--muted); font-weight: 600; background: #fafbfc; }
.table tr:last-child td { border-bottom: 0; }
.table tr.dim td { color: var(--muted); }
.ltr-cell { text-align: right; unicode-bidi: plaintext; }

/* ---------- تسجيل الدخول ---------- */
.login-box { max-width: 400px; margin: 10vh auto 0; }
.login-box h1 { margin-bottom: .25rem; color: var(--primary); }

@media (max-width: 600px) {
    .container { padding: 1rem .75rem 3rem; }
    /* على الموبايل: الاسم وقايمة المستخدم في سطر، والقايمة الرئيسية في سطر تحتهم */
    .brand { flex: 1; }
    .usermenu { order: 2; }
    .nav { order: 3; flex-basis: 100%; }
    .brand { font-size: .95rem; }
    .row2 { grid-template-columns: 1fr; }
}

/* ---------- M2: المشروع والرفع والشجرة ---------- */
.small { font-size: .875rem; }
.breadcrumb { margin: 0 0 .5rem; font-size: .9rem; color: var(--muted); }
.section-title { margin: 1.5rem 0 .75rem; }
.danger { color: var(--red); }
.warn { color: var(--yellow); }
.badge.red { background: var(--red-bg); color: var(--red); }
.badge.yellow { background: var(--yellow-bg); color: var(--yellow); }
.alert.info { background: #e8f0fe; color: #1a4d8f; }
.table td.narrow, .table th.narrow { width: 44px; }
.steps { margin: 0 0 1rem; padding-inline-start: 1.25rem; }
.steps li { margin-bottom: .25rem; }
.issues { margin: .5rem 0; padding-inline-start: 1.25rem; }
.issues li { margin-bottom: .25rem; }
details > summary { cursor: pointer; font-weight: 600; margin: .5rem 0; }
.assign-bar { display: flex; align-items: center; gap: .75rem; flex-wrap: wrap; position: sticky; bottom: .5rem; box-shadow: 0 4px 16px rgba(0, 0, 0, .08); }
.assign-bar select { width: auto; min-width: 200px; flex: 1; }
input[type=file] { font: inherit; padding: .5rem; border: 1px dashed var(--border); border-radius: 10px; background: #fafbfc; width: 100%; }

.tree-node { border-inline-start: 3px solid var(--border); padding-inline-start: .75rem; margin: .25rem 0; }
.tree-node > summary { margin: .35rem 0; }
.tree-node.depth-2 { border-color: var(--primary-2); }
.tree-body { display: flex; flex-direction: column; gap: .35rem; }
.activity-row { background: #fafbfc; border: 1px solid var(--border); border-radius: 10px; padding: .5rem .75rem; }
.activity-main { display: flex; justify-content: space-between; gap: .5rem; align-items: baseline; }
.activity-name { font-weight: 600; }
.code { font-size: .8rem; color: var(--muted); font-family: ui-monospace, monospace; }
.activity-meta { font-size: .85rem; display: flex; flex-wrap: wrap; gap: .25rem .5rem; align-items: center; }
.rel { display: inline-block; font-size: .78rem; padding: 0 .45rem; border-radius: 6px; background: #eceff3; }
.rel-fs { background: var(--red-bg); color: var(--red); }
.rel-ss, .rel-ff { background: #e8f0fe; color: #1a4d8f; }

/* ---------- المخطط والفعلي: الفعلي (أخضر) فوق، والمخطط (أزرق) تحت ---------- */
.badge.blue { background: #e8f0fe; color: #1a4d8f; }
:root { --actual: #1e8e3e; --planned: #1a73e8; --cert: #d95926; }
.progress-stack { display: grid; grid-template-columns: auto; gap: .2rem; font-size: .82rem; font-weight: 400; min-width: 230px; }
.prow { display: grid; grid-template-columns: 3.4rem 1fr 2.8rem; align-items: center; gap: .5rem; }
.plabel { color: var(--muted); }
.pbar { position: relative; display: block; height: 8px; background: #e6e9ee; border-radius: 999px; overflow: hidden; }
.pfill { position: absolute; top: 0; bottom: 0; right: 0; border-radius: 999px; }
.pfill.actual { background: var(--actual); }
.pfill.planned { background: var(--planned); }
.pfill.cert { background: var(--cert); }
.pval { font-weight: 700; text-align: left; font-variant-numeric: tabular-nums; }
.pval.actual { color: var(--actual); }
.pval.planned { color: var(--planned); }
.pval.cert { color: var(--cert); }
.pstatus { justify-self: start; margin-top: .1rem; }
.progress-stack.lg { font-size: .95rem; min-width: 280px; gap: .35rem; }
.progress-stack.lg .prow { grid-template-columns: 4rem 1fr 3.4rem; }
.progress-stack.lg .pbar { height: 14px; }
.progress-stack.lg .pval { font-size: 1.1rem; }
.summary-card { display: flex; flex-direction: column; gap: .5rem; }
.report-date { font-size: .85rem; display: flex; gap: .75rem; align-items: center; flex-wrap: wrap; }
.report-date label { display: flex; gap: .5rem; align-items: center; }
.report-date input { font: inherit; padding: .25rem .5rem; border: 1px solid var(--border); border-radius: 8px; }
.tree-node > summary { display: flex; align-items: center; justify-content: space-between; gap: .5rem 1rem; flex-wrap: wrap; list-style: none; }
.tree-node > summary .node-name { flex: 1; min-width: 10rem; }
/* المستوى الأول (زي أعمال الإنشاءات) مميز أكتر */
.tree-node.depth-1 { border: 0; padding: 0; margin: .75rem 0; }
.tree-node.depth-1 > summary { background: var(--primary); color: #fff; border-radius: var(--radius); padding: .9rem 1rem; font-size: 1.2rem; margin: 0 0 .5rem; }
.tree-node.depth-1 > summary::before { color: #fff; }
.tree-node.depth-1 > summary .plabel { color: rgba(255, 255, 255, .8); }
.tree-node.depth-1 > summary .pbar { background: rgba(255, 255, 255, .25); }
.tree-node.depth-1 > summary .pfill.actual { background: #6ee7a0; }
.tree-node.depth-1 > summary .pfill.planned { background: #9cc3ff; }
.tree-node.depth-1 > summary .pfill.cert { background: #f5c26b; }
.tree-node.depth-1 > summary .pval.cert { color: #f5c26b; }
.level-span { display: block; font-weight: 400; margin-top: .15rem; }
.tree-node.depth-1 > summary .level-span { color: rgba(255, 255, 255, .85); font-size: .85rem; }
.tree-node.depth-1 > summary .pval.actual { color: #6ee7a0; }
.tree-node.depth-1 > summary .pval.planned { color: #cfe2ff; }
.tree-node.depth-1 > .tree-body { padding-inline-start: .5rem; }
.tree-node > summary::-webkit-details-marker { display: none; }
.tree-node > summary::before { content: '▾'; color: var(--muted); }
.tree-node:not([open]) > summary::before { content: '◂'; }
.activity-row { display: flex; flex-direction: column; gap: .35rem; }
.activity-row .progress-stack { max-width: 420px; }

/* ---------- M3: شاشة المهندس وسجل التحديثات ---------- */
.sync-bar { margin: 0 0 1rem; padding: .4rem .75rem; border-radius: 10px; font-size: .9rem; background: #e8f0fe; color: #1a4d8f; }
.sync-bar:empty { display: none; }
.sync-bar.ok { background: var(--green-bg); color: var(--green); }
.sync-bar.offline { background: var(--red-bg); color: var(--red); }
.sync-bar .linklike { color: inherit; text-decoration: underline; margin-inline-start: .5rem; }
.alert.warn-box { background: var(--yellow-bg); color: var(--yellow); }
.alert.msg { display: flex; justify-content: space-between; gap: .75rem; align-items: flex-start; }
.alert.msg .linklike { color: inherit; font-size: 1.1rem; }
.unit-card { display: flex; flex-direction: column; gap: .5rem; color: var(--text); }
.unit-card:hover { text-decoration: none; border-color: var(--primary-2); }
.unit-card-head { display: flex; justify-content: space-between; align-items: baseline; gap: .5rem; flex-wrap: wrap; }
.unit-card p { margin: 0; }
.filter-bar { display: flex; gap: .4rem; overflow-x: auto; margin: 0 0 .75rem; padding-bottom: .25rem; }
.chip { font: inherit; font-size: .9rem; border: 1px solid var(--border); background: #fff; color: var(--text); border-radius: 999px; padding: .35rem .9rem; min-height: 38px; cursor: pointer; white-space: nowrap; }
.chip.active { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.small-btn { min-height: 36px; padding: .25rem .8rem; font-size: .88rem; }
.activity-row.editing { border-color: var(--primary-2); box-shadow: 0 0 0 2px rgba(46, 106, 160, .15); background: #fff; }
.editor { display: flex; flex-direction: column; gap: .6rem; border-top: 1px dashed var(--border); padding-top: .6rem; }
.editor-value output { font-size: 1.6rem; font-weight: 700; color: var(--actual); }
.editor input[type=range] { width: 100%; height: 36px; accent-color: var(--actual); direction: rtl; }
.editor .quick { display: flex; gap: .35rem; }
.editor .quick .chip { flex: 1 1 0; min-width: 0; padding: .35rem .2rem; }
.note-label { display: flex; flex-direction: column; gap: .3rem; font-weight: 600; font-size: .9rem; }
textarea { font: inherit; padding: .5rem .75rem; border: 1px solid var(--border); border-radius: 10px; width: 100%; resize: vertical; }
.editor .alert { margin: 0; }
.editor .alert .actions { margin-top: .5rem; }

.filters { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: .75rem; align-items: end; }
.filters label { display: flex; flex-direction: column; gap: .3rem; font-weight: 600; font-size: .9rem; }
.filters input[type=date] { font: inherit; padding: .5rem .6rem; border: 1px solid var(--border); border-radius: 10px; min-height: 44px; }
.log-table th, .log-table td { padding: .6rem .6rem; }
.log-table td.wrap { white-space: normal; min-width: 7rem; }
.filters .btn { white-space: nowrap; }
.pct-change { font-weight: 700; font-variant-numeric: tabular-nums; }
.pager { display: flex; justify-content: center; align-items: center; gap: 1rem; margin: 1rem 0; }
.btn.disabled { opacity: .5; pointer-events: none; }
.filters .actions { flex-wrap: nowrap; }
.unit-dates { color: var(--text); }
.form input[type=date] { font: inherit; padding: .6rem .75rem; border: 1px solid var(--border); border-radius: 10px; background: #fff; width: 100%; min-height: 44px; }

/* ---------- الإعدادات: تعديل البنود ---------- */
.edit-table td { vertical-align: middle; padding: .4rem .45rem; }
.edit-table input { min-height: 38px; padding: .35rem .5rem; font-size: .9rem; }
.container.wide { max-width: 1440px; }
.edit-table input[type=text] { min-width: 10rem; }
.edit-table input[list] { min-width: 16rem; }
.edit-table input[type=number] { min-width: 6.5rem; max-width: 8rem; }
.edit-table input[type=date] { font: inherit; font-size: .9rem; padding: .35rem .5rem; border: 1px solid var(--border); border-radius: 10px; min-height: 38px; }
.edit-table tr.has-error td { background: var(--red-bg); }
.edit-table tr.error-row td { padding-top: 0; }
.edit-table tr.error-row .alert { margin: 0; }
.row-actions { display: flex; gap: .35rem; align-items: center; }
.btn.danger-btn { color: var(--red); border-color: var(--red-bg); }
.add-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: .75rem; align-items: end; }
.money { font-variant-numeric: tabular-nums; color: var(--text); font-weight: 600; }
.tree-node.depth-1 > summary .money { color: #fff; }
.totals { display: flex; gap: 1rem; flex-wrap: wrap; margin-bottom: .5rem; }
.total-box { display: flex; flex-direction: column; gap: .1rem; padding: .6rem 1rem; border: 1px solid var(--border); border-radius: 10px; background: #fafbfc; min-width: 220px; }
.total-box strong { font-size: 1.35rem; font-variant-numeric: tabular-nums; }
.edit-table .badge { white-space: nowrap; }

/* ---------- M4: الداشبورد اليومي ---------- */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 1rem; margin-bottom: 1rem; }
.stats .card { margin: 0; }
.stat { display: flex; flex-direction: column; gap: .25rem; }
.stat-wide { grid-column: 1 / -1; }
.stat-label { font-weight: 600; color: var(--muted); font-size: .9rem; }
.stat-value { font-size: 2rem; font-weight: 700; line-height: 1.1; font-variant-numeric: tabular-nums; }
.stat-value.danger { color: var(--red); }
.stat-value.warn { color: var(--yellow); }
.dash-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 1rem; }
.dash-grid .card { margin-bottom: 1rem; }
.eng-list, .late-list { margin: .5rem 0 0; padding: 0; list-style: none; display: flex; flex-direction: column; gap: .4rem; }
.late-list { list-style: decimal; padding-inline-start: 1.5rem; }
.late-list li { padding: .25rem 0; border-bottom: 1px solid var(--border); }
.late-list li:last-child { border-bottom: 0; }
.late-head { display: flex; justify-content: space-between; gap: .5rem; flex-wrap: wrap; }
.late-list .pval { display: inline; }
.eng-row { display: flex; justify-content: space-between; gap: .75rem; padding: .5rem .75rem; border-radius: 10px; border: 1px solid var(--border); }
.eng-row.missing { background: var(--red-bg); border-color: #f5c2bf; }
.eng-row.pending { background: var(--yellow-bg); }
.eng-state { text-align: left; }
.group-title { margin: 1rem 0 .25rem; font-size: 1rem; color: var(--primary); }
.update-list { list-style: none; margin: .5rem 0 0; padding: 0; display: flex; flex-direction: column; }
.update-list li { padding: .6rem 0; border-bottom: 1px solid var(--border); display: flex; flex-direction: column; gap: .15rem; }
.update-list li:last-child { border-bottom: 0; }
.update-head { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }
@media (max-width: 600px) {
    .stats { grid-template-columns: repeat(3, 1fr); gap: .5rem; }
    .stats .stat:not(.stat-wide) { padding: .75rem .6rem; }
    .stat-value { font-size: 1.6rem; }
    .stat-label { font-size: .8rem; }
    .stats .stat .muted.small { font-size: .72rem; }
}
.install-hint { display: flex; justify-content: space-between; gap: .75rem; align-items: flex-start; }
.install-hint .linklike { color: inherit; }

/* منحنى التقدم (S-Curve) في الداشبورد، البريف 7.8 */
.curve-card { margin-bottom: 1rem; }
.curve-head { display: flex; justify-content: space-between; align-items: center; gap: .75rem; flex-wrap: wrap; }
.curve-head h2 { margin: 0; }
.curve-filters { display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; max-width: 100%; }
.curve-filters .chips { display: flex; gap: .4rem; overflow-x: auto; max-width: 100%; }
.curve-filters a.chip { text-decoration: none; display: inline-flex; align-items: center; }
.curve-filters select { max-width: 14rem; }
.curve-tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: .6rem; margin: .5rem 0 1rem; }
.curve-tile { border: 1px solid var(--border); border-radius: 10px; padding: .6rem .9rem; display: flex; flex-direction: column; }
.curve-tile .k { color: var(--muted); font-size: .85rem; display: flex; align-items: center; gap: .4rem; }
.curve-tile .v { font-size: 1.6rem; font-weight: 700; line-height: 1.3; font-variant-numeric: tabular-nums; direction: ltr; text-align: right; }
.curve-tile.bad { background: var(--red-bg); border-color: #f4c7c3; }
.curve-tile.bad .v, .curve-tile.bad .small { color: var(--red); }
.curve-tile.good { background: var(--green-bg); border-color: #b7dfc2; }
.curve-tile.good .v, .curve-tile.good .small { color: var(--green); }
.dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.dot.planned { background: var(--planned); }
.dot.actual { background: var(--actual); }
.dot.cert { background: var(--cert); border-radius: 2px; }
.curve-legend { display: flex; gap: 1.1rem; flex-wrap: wrap; font-size: .9rem; margin-bottom: .25rem; }
.curve-legend span { display: inline-flex; align-items: center; gap: .4rem; }
.sw { width: 22px; height: 0; border-top: 3px solid; display: inline-block; }
.sw.planned { border-color: var(--planned); }
.sw.actual { border-color: var(--actual); border-top-width: 4px; }
.sw.cert { border-color: var(--cert); border-top-style: dashed; }
.curve-chart { position: relative; height: 380px; }
.curve-table { margin-top: .5rem; }
.curve-table summary { cursor: pointer; color: var(--primary-2); font-size: .9rem; }
.curve-table .table-wrap { max-height: 320px; overflow-y: auto; }
@media (max-width: 600px) {
    .curve-tiles { grid-template-columns: 1fr 1fr; }
    .curve-tile .v { font-size: 1.3rem; }
    .curve-chart { height: 320px; }
    .curve-filters .chips { flex-wrap: nowrap; }
}
.code-chip { font-family: ui-monospace, monospace; background: #eceff3; border-radius: 6px; padding: 0 .35rem; }
/* ربط المستخلصات: بند مربوط بأعمال تانية، أو متعلم في أكتر من أعمال */
.form label.check.taken { color: #a0a8b2; cursor: not-allowed; }
.form label.check.taken input { cursor: not-allowed; }
.form label.check.conflict { color: var(--red); font-weight: 600; }
.taken-note { font-size: .8rem; font-weight: 400; }
