:root {
  --bg: #f3f6f7;
  --card: #ffffff;
  --ink: #14232a;
  --muted: #687a82;
  --line: #e2e9ec;
  --brand: #0e7c86;
  --brand-ink: #ffffff;
  --pos: #12825a;
  --neg: #c2413b;
  --fish: #1f77b4;
  --feed: #c08a1e;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 2px 10px rgba(0,0,0,.04);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1518; --card: #152126; --ink: #e5eef0; --muted: #8fa3aa;
    --line: #223238; --brand: #27a3ad; --pos: #3cc48c; --neg: #f0716a;
    --fish: #5aa9e6; --feed: #e2b04a; --shadow: none;
  }
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--ink);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans Arabic", sans-serif;
  padding-bottom: calc(76px + env(safe-area-inset-bottom));
}
button, input, select { font: inherit; color: inherit; }
.muted { color: var(--muted); }
.pos { color: var(--pos); }
.neg { color: var(--neg); }

.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px; background: var(--brand); color: var(--brand-ink);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 17px; }
.brand img { width: 28px; height: 28px; border-radius: 7px; }
.live { display: flex; align-items: center; gap: 6px; font-size: 12px; opacity: .9; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: #ffd54a; }
.dot.ok { background: #6ef0a8; animation: pulse 2s infinite; }
.dot.bad { background: #ff6b6b; }
@keyframes pulse { 50% { opacity: .35; } }

main { max-width: 1100px; margin: 0 auto; padding: 14px 16px; }
.view { display: none; }
.view.active { display: block; }

.toolbar { display: flex; gap: 8px; margin-bottom: 12px; align-items: center; }
.toolbar.wrap { flex-wrap: wrap; }
.toolbar input[type=search] { flex: 1 1 160px; min-width: 0; }
input, select {
  padding: 9px 10px; border: 1px solid var(--line); border-radius: 10px;
  background: var(--card); min-width: 0;
}
input:focus, select:focus { outline: 2px solid var(--brand); outline-offset: -1px; }

.seg { display: inline-flex; background: var(--card); border: 1px solid var(--line);
  border-radius: 12px; padding: 3px; gap: 2px; overflow-x: auto; max-width: 100%; }
.seg button { border: 0; background: none; padding: 6px 12px; border-radius: 9px; cursor: pointer; white-space: nowrap; }
.seg button.on { background: var(--brand); color: var(--brand-ink); }

.btn { border: 1px solid var(--line); background: var(--card); padding: 9px 14px;
  border-radius: 10px; cursor: pointer; text-decoration: none; display: inline-block; white-space: nowrap; }
a.btn { color: var(--ink); }
.btn.primary { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }
.btn.danger { color: var(--neg); border-color: var(--neg); }

.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 10px; margin-bottom: 12px; }
.kpi { background: var(--card); border-radius: 14px; padding: 12px 14px; box-shadow: var(--shadow); border: 1px solid var(--line); }
.kpi label { display: block; font-size: 12px; color: var(--muted); }
.kpi b { display: block; font-size: 22px; font-variant-numeric: tabular-nums; margin: 2px 0; }
.kpi small { color: var(--muted); font-size: 12px; }
.kpi.big { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }
.kpi.big label, .kpi.big small { color: rgba(255,255,255,.8); }
.kpi.big b { font-size: 24px; white-space: nowrap; }
.kpi b { white-space: nowrap; }
@media (max-width: 480px) { .kpi.big { grid-column: 1 / -1; } .kpis { grid-template-columns: 1fr 1fr; } .kpi b { font-size: 18px; } }

.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.span2 { grid-column: span 2; }
@media (max-width: 760px) { .grid2 { grid-template-columns: 1fr; } .span2 { grid-column: auto; } }
.card { background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 14px; box-shadow: var(--shadow); margin-bottom: 12px; }
.grid2 .card { margin: 0; }
.card h3 { margin: 0 0 10px; font-size: 15px; }
.card h4 { margin: 14px 0 6px; font-size: 13px; color: var(--muted); }
.chartbox { position: relative; height: 260px; }
.chartbox.sm { height: 220px; }

table.mini { width: 100%; border-collapse: collapse; font-size: 14px; font-variant-numeric: tabular-nums; }
table.mini th { text-align: start; font-weight: 500; color: var(--muted); font-size: 12px; padding: 4px 0; }
table.mini td { padding: 6px 0; border-top: 1px solid var(--line); }
table.mini td.r, table.mini th.r { text-align: end; }

.feed { list-style: none; margin: 0; padding: 0; }
.feed li { display: flex; justify-content: space-between; gap: 10px; padding: 8px 0; border-top: 1px solid var(--line); }
.feed li:first-child { border-top: 0; }
.feed .t { font-weight: 600; }
.feed .s { color: var(--muted); font-size: 13px; }
.feed .a { font-variant-numeric: tabular-nums; white-space: nowrap; font-weight: 600; }

.totals { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 10px; }
.totals span { background: var(--card); border: 1px solid var(--line); border-radius: 20px; padding: 5px 12px; font-size: 13px; font-variant-numeric: tabular-nums; }

.list { display: grid; gap: 8px; }
.item { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; cursor: pointer;
  display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; }
.item:hover { border-color: var(--brand); }
.item .t { font-weight: 600; }
.item .s { color: var(--muted); font-size: 13px; }
.item .a { text-align: end; font-weight: 700; font-variant-numeric: tabular-nums; }
.tag { display: inline-block; font-size: 11px; padding: 1px 8px; border-radius: 10px; margin-inline-end: 6px; color: #fff; vertical-align: 1px; }
.tag.fish { background: var(--fish); } .tag.feed { background: var(--feed); }
.tag.open { background: var(--muted); }
.empty { text-align: center; color: var(--muted); padding: 40px 0; }

.fab { position: fixed; inset-inline-end: 18px; bottom: calc(82px + env(safe-area-inset-bottom)); width: 56px; height: 56px;
  border-radius: 50%; border: 0; background: var(--brand); color: #fff; font-size: 30px; cursor: pointer;
  box-shadow: 0 4px 14px rgba(0,0,0,.25); z-index: 6; }
.tabbar { position: fixed; bottom: 0; left: 0; right: 0; display: flex; background: var(--card);
  border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom); z-index: 6; }
.tabbar button { flex: 1; border: 0; background: none; padding: 8px 2px 10px; font-size: 11px; color: var(--muted); cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 2px; }
.tabbar button span { font-size: 20px; filter: grayscale(1); opacity: .7; }
.tabbar button.on { color: var(--brand); font-weight: 600; }
.tabbar button.on span { filter: none; opacity: 1; }

dialog { border: 0; border-radius: 16px; padding: 0; width: min(560px, calc(100vw - 24px)); max-height: calc(100vh - 24px);
  background: var(--card); color: var(--ink); }
dialog::backdrop { background: rgba(0,0,0,.45); }
dialog form { padding: 18px; display: grid; gap: 12px; }
dialog h2 { margin: 0; font-size: 18px; }
dialog label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
dialog label input, dialog label select { color: var(--ink); }
fieldset { border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px 12px; margin: 0; }
legend { font-weight: 600; font-size: 14px; padding: 0 4px; }
legend small { font-weight: 400; color: var(--muted); }
.fgrid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.fgrid .full { grid-column: span 2; }
.calc { background: var(--bg); border-radius: 10px; padding: 10px 12px; font-size: 14px; font-variant-numeric: tabular-nums; display: grid; gap: 2px; }
.calc:empty { display: none; }
.calc div { display: flex; justify-content: space-between; }
.calc .tot { font-weight: 700; border-top: 1px solid var(--line); padding-top: 4px; margin-top: 2px; }
.err { color: var(--neg); margin: 0; min-height: 0; }
.err:empty { display: none; }
.actions { display: flex; gap: 8px; align-items: center; }
.grow { flex: 1; }
.seg.kind { display: flex; }
.seg.kind label { flex: 1; display: block; }
.seg.kind input { display: none; }
.seg.kind span { display: block; text-align: center; padding: 8px; border-radius: 9px; cursor: pointer; color: var(--ink); font-size: 15px; }
.seg.kind input[value=expense]:checked + span { background: var(--neg); color: #fff; }
.seg.kind input[value=income]:checked + span { background: var(--pos); color: #fff; }

.row { display: flex; gap: 8px; flex-wrap: wrap; }
.row input { flex: 1 1 160px; }
.plist { list-style: none; padding: 0; margin: 0 0 10px; }
.plist li { display: flex; justify-content: space-between; align-items: center; padding: 6px 0; border-top: 1px solid var(--line); }
.plist button { border: 0; background: none; color: var(--neg); cursor: pointer; }

.kpi.link { cursor: pointer; }
.kpi.link:hover { border-color: var(--brand); }
label.check { display: flex; align-items: center; gap: 6px; font-size: 14px; white-space: nowrap; }
label small { font-weight: 400; }
.seg.kind input[value=received]:checked + span { background: var(--pos); color: #fff; }
.seg.kind input[value=paid]:checked + span { background: var(--neg); color: #fff; }
.hint { margin: 0; font-size: 13px; }
.pos-btn { color: var(--pos); border-color: var(--pos); }
.neg-btn { color: var(--neg); border-color: var(--neg); }
.dlg { padding: 18px; display: grid; gap: 12px; }
.dlg h2 { margin: 0; font-size: 18px; }
.stmt-wrap { max-height: 50vh; overflow: auto; }
.stmt td { vertical-align: top; }
.stmt td.r, .stmt th.r { padding-inline-start: 12px; white-space: nowrap; }
.stmt td.d { white-space: nowrap; color: var(--muted); font-size: 12px; padding-inline-end: 8px; }
.stmt tr.click { cursor: pointer; }
.stmt tr.click:hover td { background: var(--bg); }
.item .badge { font-size: 12px; font-weight: 500; }

.auth { position: fixed; inset: 0; z-index: 50; background: var(--bg); display: grid; place-items: center; padding: 16px; }
.auth-card { width: min(360px, 100%); background: var(--card); border: 1px solid var(--line); border-radius: 18px;
  padding: 28px 22px; display: grid; gap: 12px; text-align: center; box-shadow: var(--shadow); }
.auth-card h1 { margin: 0; font-size: 22px; }
.auth-card p { margin: 0; }
.auth-logo { width: 64px; height: 64px; border-radius: 16px; margin: 0 auto; }
.auth-card input { padding: 12px; font-size: 16px; }
.auth-card .btn { padding: 12px; font-size: 16px; }
body.locked { overflow: hidden; }
body.locked .tabbar, body.locked .fab, body.locked main, body.locked .topbar { display: none; }

/* Phones zoom into inputs whose text is smaller than 16px — keep all form text at 16px. */
input, select, textarea { font-size: 16px; }
input.num { text-align: start; font-variant-numeric: tabular-nums; }

/* Let form grids shrink to the phone width (fieldsets and grid cells default to min-content). */
fieldset, .fgrid > *, .sale-grid > * { min-width: 0; }
dialog input, dialog select { width: 100%; }
dialog label.check input { width: auto; }
dialog form { overflow-x: hidden; }

/* ---- multi-buyer sale lines */
.sale-rows { display: grid; gap: 10px; margin-bottom: 10px; }
.sale-row { border: 1px dashed var(--line); border-radius: 10px; padding: 10px; display: grid; gap: 6px; }
.sale-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.sale-grid .buyer { grid-column: 1 / -1; }
@media (max-width: 520px) { .sale-grid { grid-template-columns: 1fr 1fr; } }
.sale-foot { display: flex; gap: 8px; align-items: center; font-size: 12px; flex-wrap: wrap; }
.sale-foot .line-total { flex: 1; font-variant-numeric: tabular-nums; }
.link-btn { border: 0; background: none; color: var(--neg); cursor: pointer; font-size: 13px; padding: 2px 4px; }
.tag.done { background: var(--pos); }
#closedWrap { margin-top: 8px; }

/* ---- roles, audit, RTL helpers */
body.worker .owner-only { display: none !important; }
.who { display: flex; flex-direction: column; align-items: flex-end; gap: 1px; font-size: 13px; font-weight: 600; }
.tabbar button b { font-weight: inherit; }
.end { text-align: end; }
.item .wide { grid-column: 1 / -1; }
.by, .audit { font-size: 12px; color: var(--muted); }
.audit { margin: 0; }
.audit:empty { display: none; }
.item.inactive { opacity: .55; }
.tag.owner { background: var(--brand); }
.tag.worker { background: #6b5bd2; }
fieldset.plain { border: 0; padding: 0; margin: 0; display: grid; gap: 12px; min-width: 0; }
fieldset:disabled input, fieldset:disabled select { opacity: .75; }
label.stack { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
label.stack input, label.stack select { color: var(--ink); }
.row { align-items: flex-end; }
.langswitch { justify-self: end; border: 1px solid var(--line); background: none; border-radius: 20px; padding: 4px 12px; cursor: pointer; font-size: 13px; }
html[dir=rtl] body { font-family: "Segoe UI", Tahoma, "Noto Sans Arabic", system-ui, sans-serif; }
html[dir=rtl] .who { align-items: flex-start; }

.toast { position: fixed; left: 50%; bottom: 150px; transform: translateX(-50%); background: var(--ink); color: var(--bg);
  padding: 10px 16px; border-radius: 10px; opacity: 0; pointer-events: none; transition: opacity .2s; z-index: 20; }
.toast.show { opacity: 1; }
