:root {
  --bg: #f6f3ee;
  --card: #ffffff;
  --ink: #1f2a37;
  --muted: #5f6977;
  --line: #e2dcd2;
  --line-strong: #cfc6b8;
  --accent: #8a2a3b;
  --accent-ink: #ffffff;
  --accent-soft: #f5e9eb;
  --gold: #b8894a;
  --gold-soft: #f7efe2;
  --ok: #2f7a4f;
  --ok-soft: #e6f2ea;
  --danger: #b3261e;
  --danger-soft: #fbe9e7;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(31, 42, 55, .06), 0 4px 16px rgba(31, 42, 55, .06);
  color-scheme: light;
}

* { box-sizing: border-box; }
html { font-size: 18px; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: 'IBM Plex Sans Arabic', 'Segoe UI', Tahoma, sans-serif;
  line-height: 1.5;
}
[hidden] { display: none !important; }
h1, h2, h3 { margin: 0; line-height: 1.3; }
h2 { font-size: 1.5rem; }
h3 { font-size: 1.15rem; margin-bottom: .4rem; }
.muted { color: var(--muted); margin: .25rem 0 0; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
input, select, button { font: inherit; color: inherit; }

/* ---- buttons ---- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  min-height: 48px; padding: .5rem 1.1rem;
  border: 1.5px solid var(--line-strong); border-radius: 10px;
  background: var(--card); color: var(--ink);
  font-weight: 600; text-decoration: none; cursor: pointer;
  transition: background .15s, border-color .15s, transform .05s;
}
.btn:hover { border-color: var(--ink); }
.btn:active { transform: translateY(1px); }
.btn:focus-visible, .tab:focus-visible, input:focus-visible, select:focus-visible { outline: 3px solid var(--gold); outline-offset: 2px; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-primary:hover { background: #74202f; border-color: #74202f; }
.btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-ghost { background: transparent; }
.btn-big { min-height: 56px; font-size: 1.05rem; padding-inline: 1.5rem; }
.btn-small { min-height: 40px; padding: .25rem .8rem; font-size: .9rem; }

/* ---- sign in ---- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: min(400px, 100%); background: var(--card); border-radius: 18px; box-shadow: var(--shadow);
  padding: 2.2rem 1.8rem; display: flex; flex-direction: column; gap: .9rem; text-align: center;
  --lh-ink: var(--ink); --lh-accent: var(--accent); --lh-gold: var(--gold);
}
.login-card .lh-mark { width: 84px; height: 84px; margin: 0 auto; }
.login-card h1 { font-family: 'Aref Ruqaa', serif; color: var(--accent); font-size: 2rem; }
.login-card p { margin: 0; color: var(--muted); }
.login-card input { width: 100%; height: 56px; font-size: 1.2rem; text-align: center; border: 1.5px solid var(--line-strong); border-radius: 10px; padding: 0 6.5rem; }
.pw-wrap { position: relative; }
.pw-toggle {
  position: absolute; inset-inline-end: 6px; top: 50%; transform: translateY(-50%);
  height: 44px; padding: 0 .7rem; border: 0; border-radius: 8px; background: var(--bg);
  font-size: .9rem; font-weight: 600; color: var(--muted); cursor: pointer; white-space: nowrap;
}
.pw-toggle:hover { color: var(--ink); }
.pw-toggle:focus-visible { outline: 3px solid var(--gold); outline-offset: 1px; }
.form-error { color: var(--danger); font-weight: 600; min-height: 1.2em; margin: 0; }

/* ---- top bar ---- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 1.2rem; flex-wrap: wrap;
  padding: .6rem clamp(16px, 3vw, 32px);
  background: var(--card); border-bottom: 1px solid var(--line);
  --lh-ink: var(--ink); --lh-accent: var(--accent); --lh-gold: var(--gold);
}
.brand { display: flex; align-items: center; gap: .6rem; }
.brand .lh-mark { width: 44px; height: 44px; }
.brand-name { font-family: 'Aref Ruqaa', serif; font-weight: 700; color: var(--accent); font-size: 1.45rem; white-space: nowrap; }
.tabs { display: flex; gap: .3rem; flex-wrap: wrap; }
.tab {
  min-height: 48px; padding: .4rem 1.1rem; border: 0; border-radius: 10px; background: transparent;
  font-weight: 600; font-size: 1.02rem; color: var(--muted); cursor: pointer;
}
.tab:hover { background: var(--bg); color: var(--ink); }
.tab[aria-selected="true"] { background: var(--accent-soft); color: var(--accent); }
.save-state { margin-inline-start: auto; font-size: .9rem; font-weight: 600; color: var(--muted); white-space: nowrap; }
.save-state.ok { color: var(--ok); }
.save-state.bad { color: var(--danger); }

.view { padding: 1.2rem clamp(16px, 3vw, 32px) 3rem; max-width: 1500px; margin: 0 auto; }
.view-head { display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap; margin-bottom: 1.2rem; }
.view-head p { flex-basis: 100%; }

/* ---- month bar ---- */
.month-bar { display: flex; align-items: center; gap: .6rem; flex-wrap: wrap; margin-bottom: 1rem; }
.month-pick { display: flex; gap: .4rem; }
.month-pick select {
  height: 56px; font-size: 1.35rem; font-weight: 700; padding: 0 .8rem; border-radius: 10px;
  border: 1.5px solid var(--line-strong); background: var(--card); color: var(--accent);
}
.saved-months { display: flex; align-items: center; gap: .5rem; margin-inline-start: auto; color: var(--muted); font-size: .95rem; }
.saved-months select { height: 48px; border-radius: 10px; border: 1.5px solid var(--line-strong); background: var(--card); padding: 0 .6rem; max-width: 100%; }

/* ---- summary ---- */
.summary { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: .8rem; margin-bottom: 1rem; }
.stat { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: .9rem 1.1rem; }
.stat span { display: block; color: var(--muted); font-size: .92rem; font-weight: 500; }
.stat b { display: block; font-size: 1.6rem; font-variant-numeric: tabular-nums; margin-top: .1rem; }
.stat.main { background: var(--accent); color: #fff; }
.stat.main span { color: rgba(255, 255, 255, .85); }

.actions { display: flex; gap: .6rem; flex-wrap: wrap; align-items: flex-start; margin-bottom: 1rem; }
.how { margin-inline-start: auto; background: var(--gold-soft); border-radius: 10px; padding: .7rem 1rem; max-width: 560px; font-size: .95rem; }
.how summary { cursor: pointer; font-weight: 600; min-height: 28px; }
.how ul { margin: .5rem 0 0; padding-inline-start: 1.2rem; }
.how li { margin-bottom: .25rem; }

/* ---- the monthly sheet ---- */
.sheet-wrap { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); overflow-x: auto; }
.sheet { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.sheet th {
  position: sticky; top: 0; background: var(--ink); color: #fff; font-weight: 600; font-size: .88rem;
  padding: .6rem .35rem; text-align: center; vertical-align: bottom; line-height: 1.25;
}
.sheet th small { display: block; font-weight: 400; opacity: .75; font-size: .76rem; }
.sheet th.c-name { text-align: right; padding-inline-start: 1rem; }
.sheet td { padding: .45rem .3rem; border-top: 1px solid var(--line); text-align: center; vertical-align: middle; }
.sheet td.c-name { text-align: right; padding-inline-start: 1rem; min-width: 190px; }
.sheet tr.idle td { background: #fbfaf8; }
.sheet tr.idle .emp-name { color: var(--muted); }
.emp-name { font-weight: 700; font-size: 1.05rem; }
.emp-sub { display: block; font-size: .8rem; color: var(--muted); }
.emp-sub.owe { color: var(--gold); font-weight: 600; }
.tag { display: inline-block; font-size: .72rem; background: var(--line); color: var(--muted); border-radius: 6px; padding: 0 .4rem; margin-inline-start: .3rem; vertical-align: middle; }
.sheet input {
  width: 5.2rem; height: 46px; text-align: center; font-size: 1.05rem; font-weight: 600;
  border: 1.5px solid var(--line-strong); border-radius: 8px; background: #fff; padding: 0 .25rem;
  font-variant-numeric: tabular-nums;
}
.sheet input.w-sm { width: 4.2rem; }
.sheet input::placeholder { color: #c3bcb1; font-weight: 400; }
.sheet input:hover { border-color: var(--muted); }
/* the advance figures: changed only through ＋ سلفة / ＋ خصم */
.sheet input[readonly] { background: var(--bg); border-style: dashed; cursor: pointer; }
/* keep the box being typed in clear of the sticky top bar and table header */
.sheet input { scroll-margin: 150px 0 80px; }
.sheet tbody tr:focus-within td { background: var(--gold-soft); }
.sheet input:focus { border-color: var(--accent); }
.sheet input.err { border-color: var(--danger); background: var(--danger-soft); }
.calc { font-weight: 600; white-space: nowrap; }
.calc.net { font-size: 1.15rem; font-weight: 700; color: var(--accent); }
.calc.neg { color: var(--danger); }
.ro { color: var(--muted); font-weight: 600; }
.ded-cell { display: flex; flex-direction: column; align-items: center; gap: .2rem; }
.add-btn {
  border: 1.5px solid var(--line-strong); background: #fff; color: var(--accent); border-radius: 8px;
  font-size: .8rem; font-weight: 700; padding: .15rem .55rem; min-height: 30px; cursor: pointer; white-space: nowrap;
}
.add-btn:hover { border-color: var(--accent); background: var(--accent-soft); }
.add-btn:focus-visible { outline: 3px solid var(--gold); outline-offset: 2px; background: var(--accent-soft); }
.move-list { border-top: 1px solid var(--line); padding-top: .6rem; display: flex; flex-direction: column; gap: .3rem; }
.move-row { display: flex; align-items: center; gap: .6rem; padding: .3rem .2rem; border-bottom: 1px dashed var(--line); }
.move-row .when { color: var(--muted); font-size: .9rem; min-width: 4.5rem; }
.move-row b { flex: 1; font-variant-numeric: tabular-nums; }
.move-row button { border: 0; background: none; color: var(--danger); font-weight: 600; cursor: pointer; font-size: .85rem; min-height: 36px; }
.move-total { display: flex; justify-content: space-between; font-weight: 700; padding: .4rem .2rem 0; font-size: 1.05rem; }
.link-btn { border: 0; background: none; color: var(--accent); font-size: .78rem; font-weight: 600; cursor: pointer; padding: .1rem .3rem; text-decoration: underline; }
.row-msg { display: block; color: var(--danger); font-size: .8rem; font-weight: 600; margin-top: .2rem; }
.icon-btn { width: 46px; height: 46px; border-radius: 10px; border: 1.5px solid var(--line-strong); background: #fff; cursor: pointer; font-size: 1.15rem; }
.icon-btn:hover { border-color: var(--accent); background: var(--accent-soft); }
.saved-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: transparent; margin-inline-start: .4rem; vertical-align: middle; transition: background .3s; }
.saved-dot.pending { background: var(--gold); }
.saved-dot.ok { background: var(--ok); }
.saved-dot.bad { background: var(--danger); }
.sheet tfoot td { background: var(--gold-soft); font-weight: 700; border-top: 2px solid var(--gold); padding: .8rem .3rem; }
.sheet tfoot .calc.net { font-size: 1.3rem; }
.hint { color: var(--muted); font-size: .9rem; margin: .7rem .2rem 0; }

/* ---- employees ---- */
.cards { display: grid; gap: .7rem; }
.emp-card {
  display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 1rem;
  background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: .8rem 1rem;
}
.emp-card h3 { margin: 0; font-size: 1.15rem; }
.emp-card .meta { display: flex; gap: 1.2rem; flex-wrap: wrap; color: var(--muted); font-size: .92rem; }
.emp-card .meta b { color: var(--ink); }
.emp-card .btns { display: flex; gap: .4rem; flex-wrap: wrap; justify-content: flex-end; }
.order { display: flex; flex-direction: column; gap: .2rem; }
.order button { width: 38px; height: 30px; border-radius: 8px; border: 1px solid var(--line-strong); background: #fff; cursor: pointer; font-size: .8rem; }
.order button:disabled { opacity: .3; cursor: default; }
.archived { margin-top: 2rem; }
.archived summary { cursor: pointer; font-weight: 600; color: var(--muted); padding: .5rem 0; min-height: 44px; }
.archived .emp-card { opacity: .85; }
.empty { background: var(--card); border-radius: var(--radius); padding: 2rem; text-align: center; color: var(--muted); }

/* ---- letterhead tab ---- */
.lh-layout { display: grid; grid-template-columns: minmax(0, 1fr) 320px; gap: 1.5rem; align-items: start; }
.lh-preview { background: #fff; border-radius: var(--radius); overflow: hidden; aspect-ratio: 210 / 297; box-shadow: var(--shadow); max-width: 640px; }
.lh-preview img { display: block; width: 100%; height: 100%; }
.lh-downloads { display: flex; flex-direction: column; gap: .7rem; }
.dl {
  display: block; background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: .9rem 1.1rem; text-decoration: none; color: var(--ink); border: 1.5px solid transparent;
}
.dl:hover { border-color: var(--accent); }
.dl b { display: block; font-size: 1.05rem; color: var(--accent); }
.dl b::before { content: '⬇️ '; }
.dl span { color: var(--muted); font-size: .9rem; }

/* ---- settings ---- */
.panel { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 1.2rem 1.4rem; margin-bottom: 1rem; max-width: 820px; }
.grid-form { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: .9rem 1.2rem; margin: 1rem 0; }
.grid-form label, .dialog label { display: flex; flex-direction: column; gap: .3rem; font-weight: 600; font-size: .95rem; }
.grid-form input, .grid-form select, .dialog input, .dialog select {
  height: 52px; border: 1.5px solid var(--line-strong); border-radius: 10px; padding: 0 .8rem; font-size: 1.05rem; background: #fff;
}
.row-gap { display: flex; gap: .6rem; flex-wrap: wrap; margin-top: .8rem; }

/* ---- dialogs ---- */
.dialog { border: 0; border-radius: 16px; padding: 0; width: min(480px, calc(100% - 32px)); box-shadow: 0 20px 60px rgba(0,0,0,.25); color: var(--ink); }
.dialog::backdrop { background: rgba(31, 42, 55, .45); }
.dialog form { padding: 1.5rem; display: flex; flex-direction: column; gap: .9rem; }
.dialog small { font-weight: 400; color: var(--muted); }
.dialog-actions { display: flex; gap: .6rem; margin-top: .4rem; }
.dialog-actions .btn { flex: 1; }

.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translate(-50%, 140%);
  background: var(--ink); color: #fff; padding: .8rem 1.4rem; border-radius: 12px; font-weight: 600;
  box-shadow: var(--shadow); transition: transform .25s; z-index: 50; max-width: calc(100% - 32px);
}
.toast.show { transform: translate(-50%, 0); }
.toast.bad { background: var(--danger); }

#print-root { display: none; }

/* ---- narrow screens: each employee becomes a card ---- */
@media (max-width: 1100px) {
  .sheet thead { display: none; }
  .sheet, .sheet tbody, .sheet tfoot { display: block; }
  .sheet tr { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: .5rem .8rem; padding: 1rem; border-top: 6px solid var(--bg); }
  .sheet td { display: flex; flex-direction: column; align-items: flex-start; gap: .2rem; border: 0; padding: 0; text-align: right; }
  .sheet td::before { content: attr(data-label); font-size: .8rem; color: var(--muted); font-weight: 600; }
  .sheet td.c-name { grid-column: 1 / -2; min-width: 0; padding: 0; }
  .sheet td.c-name::before, .sheet td.c-print::before { display: none; }
  .sheet td.c-print { grid-column: -2 / -1; align-items: flex-end; }
  .sheet input, .sheet input.w-sm { width: 100%; }
  .ded-cell { align-items: stretch; width: 100%; }
  .sheet tfoot tr { border-top: 0; }
  .sheet tfoot td.c-name { grid-column: 1 / -1; }
  .lh-layout { grid-template-columns: 1fr; }
  .how { margin-inline-start: 0; }
}
/* ---- log ---- */
.log-table td { padding: .55rem .5rem; }
.log-table .kind { display: inline-block; border-radius: 6px; padding: .05rem .55rem; font-weight: 700; font-size: .9rem; }
.log-table .kind.advance { background: var(--accent-soft); color: var(--accent); }
.log-table .kind.deduction { background: var(--ok-soft); color: var(--ok); }
.log-table .amt { font-weight: 700; font-size: 1.05rem; }
.log-table .acts { display: flex; gap: .4rem; justify-content: center; }
.log-table .when { color: var(--muted); white-space: nowrap; }

@media (max-width: 640px) {
  html { font-size: 16px; }
  .topbar { gap: .5rem; }
  .brand-name { font-size: 1.2rem; }
  .tabs { width: 100%; }
  .tab { flex: 1; padding: .4rem .3rem; font-size: .9rem; white-space: nowrap; }
  .summary { grid-template-columns: 1fr 1fr; }
  .summary .stat.main { grid-column: 1 / -1; }
  .stat b { font-size: 1.3rem; }
  .save-state { position: absolute; top: .9rem; left: 16px; }
  .month-pick { flex: 1; }
  .month-pick select { flex: 1; font-size: 1.1rem; }
  .btn-arrow { flex: 1; }
  .saved-months { margin-inline-start: 0; width: 100%; }
  .saved-months select { flex: 1; }
  .emp-card { grid-template-columns: 1fr; }
  .emp-card .order { flex-direction: row; }
  .emp-card .btns { justify-content: flex-start; }
}

/* ================= printing ================= */
@media print {
  @page { size: A4; margin: 0; }
  body { background: #fff; }
  #app, #login, .toast, dialog { display: none !important; }
  #print-root { display: block; }
  .print-page { page-break-after: always; break-after: page; }
  .print-page:last-child { page-break-after: auto; break-after: auto; }
}

/* the printed pages (built in #print-root) */
.print-page.lh-page { font-size: 11.5pt; }
.p-title { display: flex; justify-content: space-between; align-items: flex-end; margin-bottom: 6mm; }
.p-title h1 { font-size: 19pt; color: var(--lh-accent); }
.p-title .p-sub { font-size: 12pt; font-weight: 600; }
.p-title .p-date { font-size: 10pt; color: var(--lh-muted); text-align: left; }
.p-who { display: grid; grid-template-columns: 1fr 1fr; gap: 2mm 8mm; padding: 4mm 5mm; border: 0.3mm solid #d9d2c6; border-radius: 2.5mm; margin-bottom: 6mm; background: #fbf8f3; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
.p-who div span { color: var(--lh-muted); font-size: 10pt; margin-inline-end: 2mm; }
.p-who div b { font-size: 13pt; }
.p-sec { font-size: 11pt; font-weight: 700; color: var(--lh-accent); margin: 5mm 0 2mm; letter-spacing: .02em; }
.p-table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.p-table th, .p-table td { border: 0.3mm solid #d9d2c6; padding: 2.2mm 3mm; text-align: right; }
.p-table th { background: #f3eee6; font-weight: 600; width: 55%; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
.p-table td { font-weight: 600; }
.p-table td small { font-weight: 400; color: var(--lh-muted); margin-inline-start: 1.5mm; }
.p-table tr.minus td { color: var(--lh-accent); }
.p-net {
  margin-top: 6mm; padding: 5mm 6mm; border-radius: 2.5mm; background: var(--lh-ink); color: #fff;
  display: flex; justify-content: space-between; align-items: center; gap: 6mm;
  -webkit-print-color-adjust: exact; print-color-adjust: exact;
}
.p-net span { font-size: 12pt; }
.p-net b { font-size: 22pt; font-variant-numeric: tabular-nums; white-space: nowrap; }
.p-words { margin-top: 2.5mm; font-size: 11pt; font-weight: 600; }
.p-words span { color: var(--lh-muted); font-weight: 400; }
.p-signs { display: grid; grid-template-columns: 1fr 1fr; gap: 14mm; margin-top: 16mm; }
.p-sign { text-align: center; font-size: 10.5pt; }
.p-sign .line { border-top: 0.3mm solid #8d949e; margin-bottom: 2mm; height: 0; }
.p-sign small { display: block; color: var(--lh-muted); font-size: 9.5pt; }
.p-grid { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; font-size: 10pt; }
.p-grid th, .p-grid td { border: 0.3mm solid #d9d2c6; padding: 1.8mm 1.5mm; text-align: center; }
.p-grid th { background: var(--lh-ink); color: #fff; font-weight: 600; font-size: 9pt; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
.p-grid td:first-child { text-align: right; font-weight: 600; }
.p-grid tfoot td { background: #f3eee6; font-weight: 700; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
.p-grid .net { color: var(--lh-accent); font-weight: 700; white-space: nowrap; }
.p-note { font-size: 9.5pt; color: var(--lh-muted); margin-top: 3mm; }
