:root {
  --bg: #f4f6fa;
  --card: #ffffff;
  --field: #ffffff;
  --ink: #0b1220;
  --muted: #64708a;
  --line: #dde3ee;
  --accent: #2563eb;
  --accent-soft: #e8f0fe;
  --accent-line: #bcd0fb;
  --quote: #0f172a;
  --quote-soft: #e9edf5;
  --good: #15803d;
  --good-soft: #e3f5e9;
  --warn: #b45309;
  --warn-soft: #fdf2df;
  --bad: #dc2626;
  --bad-soft: #fde7e7;
  --radius: 14px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  color-scheme: light;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--bg); color: var(--ink); }
body {
  font: 17px/1.4 system-ui, -apple-system, "Segoe UI", "Noto Sans Arabic", Tahoma, sans-serif;
  -webkit-text-size-adjust: 100%;
}
button, input, textarea, select { font: inherit; color: inherit; }
h1, h2, h3 { margin: 0; }
h2 { font-size: 1.1rem; margin: 18px 0 8px; }
h3 { font-size: 1rem; margin: 6px 0; }
.muted { color: var(--muted); font-size: 0.9rem; }
.hint { color: var(--muted); font-size: 0.9rem; margin: 4px 0 10px; }
.empty { text-align: center; color: var(--muted); padding: 30px 10px; }

main { padding: 12px 16px calc(100px + var(--safe-b)); max-width: 720px; margin: 0 auto; display: flex; flex-direction: column; gap: 12px; }

/* ---- top bar ---- */
header.top {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 8px;
  padding: calc(10px + env(safe-area-inset-top, 0px)) 16px 10px;
  background: var(--bg); border-bottom: 1px solid var(--line);
}
header.top h1 { font-size: 1.2rem; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.top-right { display: flex; align-items: center; gap: 6px; }
.icon { border: 0; background: none; font-size: 1.3rem; width: 44px; height: 44px; border-radius: 50%; cursor: pointer; flex: none; }
.icon:active { background: var(--line); }
.lang { border: 1px solid var(--line); background: var(--card); border-radius: 20px; min-width: 44px; height: 36px; font-weight: 700; cursor: pointer; }
.sync { border: 0; background: none; width: 32px; height: 36px; cursor: pointer; display: grid; place-items: center; }
.sync span { width: 10px; height: 10px; border-radius: 50%; background: var(--muted); }
.sync.synced span { background: var(--good); }
.sync.syncing span, .sync.idle span { background: var(--warn); animation: pulse 1s infinite alternate; }
.sync.offline span { background: #999; }
.sync.error span, .sync.locked span { background: var(--bad); }
@keyframes pulse { to { opacity: 0.3; } }

/* ---- bottom nav ---- */
nav.bottom {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 10;
  display: flex; justify-content: space-around; align-items: center;
  background: var(--card); border-top: 1px solid var(--line);
  padding: 6px 8px calc(6px + var(--safe-b));
}
nav.bottom button {
  border: 0; background: none; display: flex; flex-direction: column; align-items: center;
  font-size: 0.75rem; color: var(--muted); min-width: 64px; padding: 4px; cursor: pointer;
}
nav.bottom button .ico { font-size: 1.4rem; }
nav.bottom button.on { color: var(--accent); font-weight: 700; }
nav.bottom .plus {
  width: 58px; height: 58px; border-radius: 50%; background: var(--accent); color: #fff !important;
  font-size: 2rem; line-height: 1; margin-top: -26px; box-shadow: 0 4px 16px rgba(37, 99, 235, 0.35);
  justify-content: center;
}

/* ---- buttons ---- */
.btn {
  display: flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 50px; padding: 10px 16px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--card); cursor: pointer;
  text-decoration: none; text-align: center; font-weight: 600;
}
.btn:active { transform: scale(0.98); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.good { background: var(--good-soft); border-color: transparent; color: var(--good); }
.btn.danger { color: var(--bad); }
.btn.ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn.big { min-height: 60px; font-size: 1.1rem; }
.btn.quote { background: var(--quote-soft); border-color: transparent; color: var(--quote); }
.btn.invoice { background: var(--accent-soft); border-color: transparent; color: var(--accent); }
.link { border: 0; background: none; color: var(--accent); font-weight: 600; padding: 6px 0; cursor: pointer; text-align: start; }
.row { display: flex; gap: 8px; }
.row > * { flex: 1; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.big-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

/* ---- cards ---- */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; }
button.card { width: 100%; text-align: start; cursor: pointer; }
.card.doc, .card.cust { display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.doc-type { font-size: 0.8rem; font-weight: 700; color: var(--accent); }
.doc-type.quote { color: var(--quote); }
.doc-name { font-weight: 700; font-size: 1.05rem; }
.doc-name.big { font-size: 1.3rem; }
.doc-date { color: var(--muted); font-size: 0.85rem; }
.doc-r { text-align: end; display: flex; flex-direction: column; align-items: flex-end; gap: 4px; }
.doc-total { font-weight: 800; font-size: 1.1rem; direction: ltr; }
.chip-s { white-space: nowrap; font-size: 0.75rem; font-weight: 700; padding: 3px 10px; border-radius: 20px; }
.chip-s.ok { background: var(--good-soft); color: var(--good); }
.chip-s.wait { background: var(--warn-soft); color: var(--warn); }
.chip-s.bad { background: var(--bad-soft); color: var(--bad); }
.count { background: var(--accent-soft); color: var(--accent); font-weight: 700; border-radius: 20px; padding: 2px 10px; }
.card.wip { background: var(--warn-soft); border-color: transparent; display: flex; flex-direction: column; gap: 10px; }
.card.selected { background: var(--good-soft); border-color: transparent; }
.card.warn { background: var(--warn-soft); border-color: transparent; }
.card.bankbox { font-family: ui-monospace, monospace; font-size: 0.95rem; text-align: left; }

.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; }
.stat small { color: var(--muted); display: block; }
.stat b { font-size: 1.4rem; direction: ltr; display: inline-block; }

.filters { display: flex; gap: 6px; overflow-x: auto; }
.filters button { border: 1px solid var(--line); background: var(--card); border-radius: 20px; padding: 8px 16px; white-space: nowrap; cursor: pointer; }
.filters button.on { background: var(--accent); color: #fff; border-color: var(--accent); }

/* ---- forms ---- */
input, textarea, select {
  width: 100%; border: 1px solid var(--line); border-radius: 10px; background: var(--field);
  padding: 12px; min-height: 48px; font-size: 17px; /* 16px+ stops iOS zoom */
}
textarea { resize: vertical; }
input:focus, textarea:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
.search { border-radius: 24px; padding-inline: 18px; }
label { display: flex; flex-direction: column; gap: 4px; font-size: 0.9rem; color: var(--muted); font-weight: 600; }
label input, label textarea, label select { color: var(--ink); font-weight: 400; }
.card.form { display: flex; flex-direction: column; gap: 12px; }
.card.form h2 { margin: 0; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.row2 .full { grid-column: 1 / -1; }
.row3 { display: grid; grid-template-columns: 0.7fr 1fr 1fr; gap: 8px; }
.row3 input { padding: 10px 6px; font-size: 16px; }
.money { direction: ltr; text-align: left; font-weight: 700; }

/* segmented control */
.seg { display: flex; background: var(--bg); border: 1px solid var(--line); border-radius: 12px; padding: 3px; gap: 3px; overflow-x: auto; }
.seg button { flex: 1; border: 0; background: none; padding: 10px 8px; border-radius: 9px; white-space: nowrap; cursor: pointer; font-weight: 600; color: var(--muted); min-height: 44px; }
.seg button.on { background: var(--accent); color: #fff; }
.seg.wide button { min-height: 52px; font-size: 1.05rem; }

/* chips */
.quick { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 8px; }
.chip { border: 1px solid var(--line); background: var(--card); border-radius: 20px; padding: 8px 14px; cursor: pointer; font-size: 0.9rem; }
.chip.big { border-radius: 12px; padding: 12px; display: flex; flex-direction: column; align-items: flex-start; gap: 2px; text-align: left; font-weight: 600; min-height: 64px; }
[dir="rtl"] .chip.big { align-items: flex-end; text-align: right; }
.chip.big small { color: var(--muted); font-weight: 400; }
.chip.big em { font-style: normal; color: var(--accent); font-size: 0.85rem; direction: ltr; }
.chip.big:active, .chip:active { background: var(--accent-soft); border-color: var(--accent); }
.chip.sm { font-size: 0.8rem; padding: 6px 10px; white-space: nowrap; }
.phrases { display: flex; gap: 6px; flex-wrap: wrap; }
.item .phrases { flex-wrap: nowrap; overflow-x: auto; padding-bottom: 2px; }

/* ---- wizard ---- */
.view-wizard main { padding-bottom: calc(110px + var(--safe-b)); }
.dots { display: flex; justify-content: center; gap: 6px; padding: 10px 16px 0; }
.dot { width: 32px; height: 32px; border-radius: 50%; border: 1px solid var(--line); background: var(--card); color: var(--muted); font-size: 0.85rem; cursor: pointer; }
.dot.reach { color: var(--ink); }
.dot.on { background: var(--accent); color: #fff; border-color: var(--accent); }
.dot:disabled { opacity: 0.45; cursor: default; }
.q { font-size: 1.5rem; margin: 8px 0 4px; }
.choices { display: flex; flex-direction: column; gap: 12px; }
.choice {
  border: 2px solid var(--line); background: var(--card); border-radius: 18px; padding: 24px 20px;
  display: flex; flex-direction: column; gap: 6px; text-align: start; cursor: pointer;
}
.choice b { font-size: 1.4rem; }
.choice small { color: var(--muted); font-size: 1rem; }
.choice.on { border-color: var(--accent); background: var(--accent-soft); }
.choice.quote.on { border-color: var(--quote); background: var(--quote-soft); }
.wiz-foot {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 10px 16px calc(10px + var(--safe-b)); background: var(--card); border-top: 1px solid var(--line);
}
.wiz-foot .btn { min-width: 110px; }
.foot-total { text-align: center; }
.foot-total small { display: block; color: var(--muted); font-size: 0.75rem; }
.foot-total b { font-size: 1.2rem; direction: ltr; display: inline-block; }

.card.item { display: flex; flex-direction: column; gap: 10px; }
.item-head { display: flex; gap: 6px; align-items: center; }
.item-title { font-weight: 700; }
.rm { color: var(--bad); font-size: 1rem; }
.amt { text-align: end; color: var(--muted); }
.amt b { color: var(--ink); font-size: 1.15rem; direction: ltr; display: inline-block; }

.thumbs { display: grid; grid-template-columns: repeat(auto-fill, minmax(90px, 1fr)); gap: 8px; }
.thumb { position: relative; aspect-ratio: 1; border-radius: 10px; overflow: hidden; background: var(--bg); }
.thumb img { width: 100%; height: 100%; object-fit: cover; }
.thumb .rm { position: absolute; top: 4px; inset-inline-end: 4px; background: rgba(255, 255, 255, 0.92); width: 32px; height: 32px; }
.field-row { display: grid; grid-template-columns: 1fr 1.4fr auto; gap: 6px; align-items: center; }
.field-label { font-weight: 700; background: var(--bg); }

.rev { padding: 4px 14px; }
.rev-row { display: flex; width: 100%; align-items: center; gap: 10px; border: 0; border-bottom: 1px solid var(--line); background: none; padding: 14px 0; text-align: start; cursor: pointer; }
.rev-row span { color: var(--muted); flex: 1; }
.rev-row em { font-style: normal; color: var(--accent); font-size: 0.85rem; }

.line { display: flex; justify-content: space-between; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.line > div:last-child { direction: ltr; font-weight: 600; white-space: nowrap; }
.line.total { border: 0; font-size: 1.2rem; font-weight: 800; }
.summary { display: flex; flex-direction: column; gap: 10px; }
.sum-top { display: flex; justify-content: space-between; align-items: flex-start; }
.sum-total { font-size: 2rem; font-weight: 800; direction: ltr; text-align: end; }
.sum-total small { font-size: 0.9rem; color: var(--muted); font-weight: 400; }

/* settings */
details.qi { border: 1px solid var(--line); border-radius: 10px; padding: 0 12px; }
details.qi[open] { padding-bottom: 12px; display: flex; flex-direction: column; gap: 10px; }
details.qi summary { padding: 12px 0; cursor: pointer; display: flex; justify-content: space-between; gap: 8px; font-weight: 600; }
details.qi summary small { color: var(--muted); font-weight: 400; }
.sync-text { margin: 0; font-weight: 600; }
.sync-text.synced { color: var(--good); }
.sync-text.error, .sync-text.locked { color: var(--bad); }

/* lock */
.lock { min-height: 100vh; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 24px; gap: 10px; text-align: center; }
.lock form { width: 100%; max-width: 320px; display: flex; flex-direction: column; gap: 12px; }
.lock input { text-align: center; font-size: 1.3rem; }
.lock-logo { width: 96px; height: 96px; border-radius: 22px; }
.lang.corner { position: absolute; top: calc(16px + env(safe-area-inset-top, 0px)); inset-inline-end: 16px; }
.err { color: var(--bad); min-height: 1.4em; margin: 0; }

/* modal sheet */
#modal { display: none; }
#modal.open { display: block; }
.backdrop { position: fixed; inset: 0; background: rgba(11, 18, 32, 0.45); z-index: 50; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 51; max-width: 720px; margin: 0 auto;
  background: var(--card); border-radius: 20px 20px 0 0; padding: 20px 16px calc(20px + var(--safe-b));
  display: flex; flex-direction: column; gap: 10px; max-height: 90vh; overflow-y: auto;
}
.sheet h2 { margin: 0 0 6px; font-size: 1.05rem; word-break: break-all; }
.calc-row { display: grid; grid-template-columns: 20px 1fr 16px 1fr 40px; gap: 6px; align-items: center; direction: ltr; }
.calc-row.sub span:first-child { color: var(--bad); font-weight: 800; }

/* toast */
#toast {
  position: fixed; left: 50%; bottom: calc(100px + var(--safe-b)); transform: translateX(-50%) translateY(20px);
  background: var(--ink); color: var(--bg); padding: 10px 20px; border-radius: 24px; opacity: 0; pointer-events: none;
  transition: 0.2s; z-index: 60; white-space: nowrap;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.item .seg { display: grid; grid-template-columns: repeat(5, 1fr); overflow: visible; }
.item .seg button { font-size: 0.8rem; padding: 8px 2px; white-space: normal; line-height: 1.15; }
.row3 { grid-template-columns: 1fr 1fr; }
.row3 label:first-child { grid-column: 1 / -1; }
/* English text (names, item titles) inside the Arabic layout keeps its own direction */
.doc-name, .line b, .card.selected .muted, .card.cust .muted { unicode-bidi: plaintext; }

/* quick buttons: compact, one line each */
.quick { display: flex; flex-wrap: wrap; gap: 8px; }
.chip.qb { display: inline-flex; gap: 6px; align-items: center; padding: 10px 14px; font-weight: 600; }
.chip.qb em { font-style: normal; color: var(--accent); font-size: 0.85rem; }
.item-links { display: flex; flex-wrap: wrap; gap: 4px 16px; }
.rs-input { display: flex; align-items: center; gap: 6px; direction: ltr; }
.rs-input b { color: var(--ink); }

/* AI box */
.card.ai { display: flex; flex-direction: column; gap: 10px; border-color: var(--accent-line);
  background: linear-gradient(160deg, #e8f0fe, #ffffff 65%); }
.ai-head { font-weight: 700; }
.ai-reply { margin: 0; color: var(--muted); font-size: 0.95rem; min-height: 1.2em; }
.ai-reply:empty { display: none; }
.card.ai.busy .ai-reply { color: var(--accent); animation: pulse 0.8s infinite alternate; }
.btn.mic.rec { background: var(--bad); border-color: var(--bad); color: #fff; animation: pulse 0.8s infinite alternate; }
.btn:disabled { opacity: 0.5; cursor: default; }
.sheet.mail label { color: var(--muted); }
