:root {
  --paper: #F5F6F7;
  --surface: #FFFFFF;
  --ink: #1C2331;
  --ink-2: #5A6475;
  --ink-3: #8A93A3;
  --line: #DDE1E6;
  --line-2: #EDEFF2;
  --red: #B8141B;
  --red-ink: #8E0F14;
  --red-tint: #FBECEC;
  --ok: #2F7D4E;
  --ok-tint: #E6F3EB;
  --warn: #9A6A10;
  --warn-tint: #FFF4D6;
  --bad: #B8141B;
  --bad-tint: #FBECEC;
  --bar: #C9CED6;
  --radius-s: 6px;
  --radius-m: 10px;
  --font: "IBM Plex Sans Arabic", system-ui, -apple-system, "Segoe UI", sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #12161D; --surface: #1A2029; --ink: #E8EBF0; --ink-2: #A9B1BE; --ink-3: #7C8594;
    --line: #2D3542; --line-2: #222934; --red: #E2474D; --red-ink: #FF8A8E; --red-tint: #3A1B1D;
    --ok: #5DBB83; --ok-tint: #17301F; --warn: #E2B04A; --warn-tint: #352A12; --bad: #E2474D; --bad-tint: #3A1B1D;
    --bar: #3B4554; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --paper: #12161D; --surface: #1A2029; --ink: #E8EBF0; --ink-2: #A9B1BE; --ink-3: #7C8594;
  --line: #2D3542; --line-2: #222934; --red: #E2474D; --red-ink: #FF8A8E; --red-tint: #3A1B1D;
  --ok: #5DBB83; --ok-tint: #17301F; --warn: #E2B04A; --warn-tint: #352A12; --bad: #E2474D; --bad-tint: #3A1B1D;
  --bar: #3B4554; color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--paper); color: var(--ink); font: 15px/1.5 var(--font); font-variant-numeric: tabular-nums; }
[hidden] { display: none !important; }
h1, h2, h3 { margin: 0; line-height: 1.25; }
h1 { font-size: 26px; font-weight: 700; letter-spacing: -0.01em; }
h2 { font-size: 18px; font-weight: 600; }
h3 { font-size: 15px; font-weight: 600; }
a { color: inherit; }
:focus-visible { outline: 2px solid var(--red); outline-offset: 2px; }
@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; } }

/* Sign in */
.signin { min-height: 100vh; display: grid; place-items: center; padding: 24px; }
.signin-box { max-width: 360px; text-align: center; }
.signin-box img { border-radius: 14px; }
.signin-box h1 { margin: 16px 0 6px; }
.signin-box p { color: var(--ink-2); margin: 0 0 24px; }
.signin-msg { margin-top: 16px !important; color: var(--bad) !important; }

/* Shell */
.shell { display: grid; grid-template-columns: 220px 1fr; min-height: 100vh; }
.rail { position: sticky; top: 0; height: 100vh; overflow: auto; background: var(--surface); border-right: 1px solid var(--line); padding: 18px 12px; display: flex; flex-direction: column; gap: 2px; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 17px; padding: 0 10px 18px; }
.brand img { border-radius: 7px; }
.rail a { text-decoration: none; padding: 8px 10px; border-radius: var(--radius-s); color: var(--ink-2); font-weight: 500; }
.rail a:hover { background: var(--line-2); color: var(--ink); }
.rail a.on { background: var(--red-tint); color: var(--red-ink); }
.rail-user { margin-top: auto; padding: 12px 10px 0; font-size: 13px; color: var(--ink-3); border-top: 1px solid var(--line); overflow-wrap: anywhere; }
main { padding: 28px 32px 48px; max-width: 1180px; width: 100%; outline: none; }
.tabs { display: none; }

@media (max-width: 820px) {
  .shell { grid-template-columns: 1fr; }
  .rail { display: none; }
  main { padding: 18px 16px calc(84px + env(safe-area-inset-bottom)); }
  .tabs { display: grid; grid-template-columns: repeat(5, 1fr); position: fixed; inset: auto 0 0 0; background: var(--surface); border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom); z-index: 5; }
  .tabs a { text-decoration: none; text-align: center; padding: 12px 4px 10px; font-size: 13px; font-weight: 500; color: var(--ink-2); }
  .tabs a.on { color: var(--red); box-shadow: inset 0 2px 0 var(--red); }
}

/* Page header */
.head { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 18px; }
.head p { margin: 4px 0 0; color: var(--ink-2); }
.head-actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* Buttons and inputs */
.btn { font: inherit; font-weight: 600; border: 1px solid var(--line); background: var(--surface); color: var(--ink); padding: 8px 14px; border-radius: var(--radius-s); cursor: pointer; white-space: nowrap; }
.btn:hover { border-color: var(--ink-3); }
.btn.primary { background: var(--red); border-color: var(--red); color: #fff; }
.btn.primary:hover { background: var(--red-ink); border-color: var(--red-ink); }
.btn.small { padding: 4px 10px; font-size: 13px; }
.btn.quiet { border-color: transparent; background: transparent; color: var(--ink-2); }
.btn.danger { color: var(--bad); }
.btn:disabled { opacity: .5; cursor: default; }
input, select, textarea { font: inherit; color: var(--ink); background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-s); padding: 8px 10px; width: 100%; min-width: 0; }
input:focus, select:focus, textarea:focus { border-color: var(--ink-3); outline: none; box-shadow: 0 0 0 3px var(--line-2); }
.filters { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 14px; }
.filters > * { width: auto; }
.filters input[type=search] { flex: 1 1 220px; }

/* Month strip: the chart is the period picker */
.strip { display: grid; grid-template-columns: repeat(12, 1fr) auto; gap: 6px; align-items: end; margin: 0 0 22px; }
.strip button { font: inherit; background: none; border: 0; padding: 0; cursor: pointer; display: flex; flex-direction: column; align-items: stretch; gap: 6px; color: var(--ink-2); }
.strip .bar-wrap { height: 72px; display: flex; align-items: flex-end; }
.strip .bar { width: 100%; background: var(--bar); border-radius: 3px 3px 0 0; min-height: 2px; transition: background .15s; }
.strip button:hover .bar { background: var(--ink-3); }
.strip button.on .bar { background: var(--red); }
.strip button.on { color: var(--ink); font-weight: 600; }
.strip .m { font-size: 12px; text-align: center; }
.strip .year { align-self: end; border: 1px solid var(--line); border-radius: var(--radius-s); padding: 6px 12px; font-size: 13px; }
.strip .year.on { border-color: var(--red); color: var(--red-ink); background: var(--red-tint); }
@media (max-width: 520px) { .strip { gap: 3px; grid-template-columns: repeat(12, 1fr); } .strip .year { grid-column: 1 / -1; justify-self: end; margin-top: 4px; } .strip .bar-wrap { height: 56px; } .strip .m { font-size: 10px; } }

/* Figures */
.figures { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); border: 1px solid var(--line); border-radius: var(--radius-m); background: var(--surface); margin-bottom: 22px; }
.figures > div { padding: 14px 16px; border-left: 1px solid var(--line); }
.figures > div:first-child { border-left: 0; }
.figures .v { font-size: 22px; font-weight: 600; letter-spacing: -0.01em; }
.figures .l { color: var(--ink-2); font-size: 13px; }
@media (max-width: 820px) { .figures { grid-template-columns: 1fr 1fr; } .figures > div:nth-child(3) { border-left: 0; } .figures > div:nth-child(n+3) { border-top: 1px solid var(--line); } }

.cols { display: grid; grid-template-columns: 1fr 1fr; gap: 22px; margin-bottom: 22px; align-items: start; }
.cols > .panel + .panel { margin-top: 0; }
@media (max-width: 980px) { .cols { grid-template-columns: 1fr; } }
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-m); padding: 16px; }
.panel h2 { margin-bottom: 10px; }
.panel .sub { color: var(--ink-2); font-size: 13px; margin: -6px 0 10px; }
.panel + .panel { margin-top: 22px; }
.link { background: none; border: 0; font: inherit; color: var(--red-ink); cursor: pointer; padding: 0; text-decoration: underline; text-underline-offset: 3px; }

/* Tables */
.tablewrap { overflow-x: auto; border: 1px solid var(--line); border-radius: var(--radius-m); background: var(--surface); }
table { border-collapse: collapse; width: 100%; font-size: 14px; }
th, td { padding: 8px 10px; border-bottom: 1px solid var(--line-2); text-align: left; vertical-align: top; }
th { font-weight: 600; color: var(--ink-2); background: var(--line-2); white-space: nowrap; position: sticky; top: 0; }
td.n, th.n { text-align: right; white-space: nowrap; }
tr.total td { font-weight: 700; background: var(--line-2); border-bottom: 0; }
tbody tr.row { cursor: pointer; }
tbody tr.row:hover td { background: var(--paper); }
.panel .tablewrap { border: 0; border-radius: 0; margin: 0 -16px -16px; }
.panel .tablewrap table { border-top: 1px solid var(--line-2); }
.item { max-width: 360px; }
.muted { color: var(--ink-3); }
.ar { unicode-bidi: plaintext; }

/* Status chips */
.chip { display: inline-block; font-size: 12px; font-weight: 600; padding: 1px 8px; border-radius: 99px; white-space: nowrap; }
.chip.ok { background: var(--ok-tint); color: var(--ok); }
.chip.warn { background: var(--warn-tint); color: var(--warn); }
.chip.bad { background: var(--bad-tint); color: var(--bad); }
.chip.plain { background: var(--line-2); color: var(--ink-2); }

/* Segmented control */
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: var(--radius-s); overflow: hidden; background: var(--surface); flex-wrap: wrap; margin-bottom: 14px; }
.seg button { font: inherit; font-size: 14px; border: 0; background: none; padding: 7px 12px; cursor: pointer; color: var(--ink-2); border-left: 1px solid var(--line); }
.seg button:first-child { border-left: 0; }
.seg button.on { background: var(--ink); color: var(--surface); }
.seg .c { color: inherit; opacity: .7; margin-left: 4px; }

/* Receipt groups */
.rgroup { margin-bottom: 14px; }
.rgroup-head { display: flex; justify-content: space-between; gap: 12px; align-items: baseline; padding: 10px 2px 6px; }
.rgroup-head h3 { font-size: 16px; }
.rgroup-head .d { font-weight: 400; }
@media (max-width: 640px) { .rgroup-head { flex-wrap: wrap; align-items: center; } .rgroup-head .d { display: block; font-size: 13px; } }

/* Dialog */
dialog { border: 0; border-radius: 14px; padding: 0; width: min(640px, calc(100vw - 24px)); max-height: calc(100vh - 32px); background: var(--surface); color: var(--ink); box-shadow: 0 20px 60px rgba(15, 20, 30, .28); }
dialog::backdrop { background: rgba(15, 20, 30, .45); }
.dlg-head { display: flex; justify-content: space-between; align-items: center; padding: 16px 20px; border-bottom: 1px solid var(--line); position: sticky; top: 0; background: var(--surface); z-index: 1; }
.dlg-body { padding: 16px 20px; display: grid; grid-template-columns: 1fr 1fr; gap: 12px 14px; }
.dlg-body .full { grid-column: 1 / -1; }
.dlg-body label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; font-weight: 500; color: var(--ink-2); }
.dlg-foot { display: flex; justify-content: space-between; gap: 8px; padding: 14px 20px; border-top: 1px solid var(--line); position: sticky; bottom: 0; background: var(--surface); }
.dlg-foot .right { display: flex; gap: 8px; margin-left: auto; }
.preview { grid-column: 1 / -1; background: var(--paper); border-radius: var(--radius-s); padding: 10px 12px; display: flex; gap: 18px; flex-wrap: wrap; font-size: 14px; }
.preview b { font-weight: 600; }
@media (max-width: 560px) {
  dialog { width: 100vw; max-width: 100vw; max-height: 100dvh; height: 100dvh; border-radius: 0; margin: 0; }
  .dlg-body { grid-template-columns: 1fr; }
}

/* Mobile list rows instead of wide tables */
@media (max-width: 640px) {
  table.stack thead { display: none; }
  table.stack tr { display: flex; flex-wrap: wrap; align-items: baseline; gap: 2px 12px; padding: 10px 12px; border-bottom: 1px solid var(--line-2); }
  table.stack td { border: 0; padding: 0; font-size: 13px; color: var(--ink-2); }
  table.stack td.hide-s { display: none; }
  table.stack td[data-k]::before { content: attr(data-k) " "; color: var(--ink-3); }
  table.stack td.first { flex: 1 1 60%; font-size: 15px; font-weight: 600; color: var(--ink); order: 0; }
  table.stack td.amt { order: 1; margin-left: auto; font-size: 15px; font-weight: 600; color: var(--ink); }
  table.stack td.item { flex-basis: 100%; max-width: none; order: 2; font-size: 14px; color: var(--ink); }
  table.stack td:not(.first):not(.amt):not(.item) { order: 3; }
  table.stack tr.total { background: var(--line-2); }
  table.stack tr.total td.first { flex-basis: auto; }
  table.stack td select { width: auto; padding: 4px 8px; }
}
.more-list { display: grid; gap: 8px; }
.more-list a { display: block; padding: 14px 16px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-m); text-decoration: none; font-weight: 600; }
.more-list a span { display: block; font-weight: 400; color: var(--ink-2); font-size: 13px; }

.empty { padding: 28px 16px; text-align: center; color: var(--ink-2); }
.toast { position: fixed; left: 50%; bottom: calc(96px + env(safe-area-inset-bottom)); transform: translateX(-50%) translateY(20px); background: var(--ink); color: var(--surface); padding: 10px 16px; border-radius: var(--radius-s); opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 50; font-weight: 500; }
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
@media (min-width: 821px) { .toast { bottom: 28px; } }
.kv { display: grid; grid-template-columns: 1fr auto; gap: 6px 16px; }
.kv .n { text-align: right; font-weight: 600; }
.kv .big { font-size: 20px; }
.kv hr { grid-column: 1 / -1; border: 0; border-top: 1px solid var(--line); margin: 4px 0; width: 100%; }
.loading { padding: 40px; color: var(--ink-2); }

.th-sort { font: inherit; font-weight: 600; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; white-space: nowrap; }
.th-sort:hover, .th-sort.on { color: var(--ink); }
th.n .th-sort { text-align: right; }

/* Table edit mode */
.btn.on-toggle { background: var(--ink); border-color: var(--ink); color: var(--surface); }
.hint { color: var(--ink-2); font-size: 13px; margin: -4px 0 12px; }
table.editing td { vertical-align: middle; padding-top: 4px; padding-bottom: 4px; }
.cell { display: flex; align-items: center; gap: 2px; min-width: 150px; }
.cell input { padding: 5px 8px; font-size: 14px; }
.fill { font: inherit; font-size: 18px; border: 1px solid transparent; background: none; color: var(--ink-3); cursor: pointer; border-radius: var(--radius-s); padding: 2px 6px; line-height: 1; }
.fill:hover { color: var(--red-ink); border-color: var(--line); }
table.editing .link { text-align: left; font-weight: 500; }
@media (max-width: 640px) {
  table.stack.editing td { flex-basis: 100%; }
  table.stack.editing td.hide-s { display: none; }
  table.stack.editing .cell { min-width: 0; }
}
table.editing td:last-child { white-space: nowrap; }
td.notes { max-width: 240px; white-space: normal; color: var(--ink-3); font-size: 13px; }
.panel > .rgroup-head + .sub + .rgroup-head, .panel > .tablewrap + .rgroup-head { margin-top: 18px; }
/* Supplier page */
.portal { max-width: 1180px; margin: 0 auto; }
.portal-top { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; padding: 16px 32px; border-bottom: 1px solid var(--line); background: var(--surface); }
.portal-top .brand { padding: 0; }
.portal-user { display: flex; align-items: center; gap: 8px; color: var(--ink-2); font-size: 14px; }
.portal main { margin: 0 auto; }
#partner h3 { font-size: 15px; margin: 18px 0 8px; }
@media (max-width: 760px) { .portal-top { padding: 12px 16px; } .portal main { padding: 18px 16px 40px; } }
.panel > h3 { font-size: 15px; margin: 18px 0 8px; }
details summary { cursor: pointer; color: var(--ink-2); font-weight: 500; padding: 4px 0; }
.savebar { position: fixed; top: 0; left: 0; right: 0; z-index: 50; padding: 10px 16px; text-align: center; font-weight: 600; font-size: 14px; }
.savebar.warn { background: var(--warn-tint); color: var(--warn); border-bottom: 1px solid var(--warn); }
.savebar.ok { background: var(--ok-tint); color: var(--ok); }
.portal td.wide input { min-width: 220px; } .portal table.editing input[type=number] { max-width: 110px; text-align: right; }
