:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --surface-2: #f0ede6;
  --text: #1d1c1a;
  --muted: #6b675f;
  --line: #e2ddd3;
  --accent: #1f5f4a;
  --accent-soft: #e3efe9;
  --bad: #a3322a;
  --bad-soft: #f8e6e3;
  --warn: #8a5a00;
  --warn-soft: #fbf0d9;
  --info: #2c4f7c;
  --info-soft: #e5edf7;
  --radius: 12px;
  --font: 'IBM Plex Sans Arabic', 'Segoe UI', Tahoma, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161614;
    --surface: #1f1f1c;
    --surface-2: #2a2a26;
    --text: #ecebe6;
    --muted: #a3a097;
    --line: #34332e;
    --accent: #6cc3a0;
    --accent-soft: #1d3a30;
    --bad: #f08a80;
    --bad-soft: #3d1f1c;
    --warn: #e7b85a;
    --warn-soft: #3a2f17;
    --info: #8fb5e6;
    --info-soft: #1d2a3b;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 18px;
  line-height: 1.9;
}
h1, h2, h3 { line-height: 1.5; margin: 0 0 .4em; font-weight: 600; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.2rem; }
h3 { font-size: 1.05rem; }
p { margin: 0 0 .6em; }
a { color: var(--accent); }
small, .muted { color: var(--muted); }
.num { direction: ltr; unicode-bidi: isolate; font-variant-numeric: tabular-nums; white-space: nowrap; }
.neg { color: var(--bad); }
.pos { color: var(--accent); }

.top {
  position: sticky; top: 0; z-index: 5;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  display: flex; align-items: center; gap: 16px;
  padding: 8px 16px;
}
.brand { font-weight: 700; white-space: nowrap; }
nav { display: flex; gap: 4px; overflow-x: auto; scrollbar-width: none; }
nav::-webkit-scrollbar { display: none; }
nav a {
  text-decoration: none; color: var(--muted);
  padding: 4px 12px; border-radius: 999px; white-space: nowrap; font-size: .95rem;
}
nav a.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.badge {
  display: inline-block; min-width: 1.5em; padding: 0 .4em; border-radius: 999px;
  background: var(--bad); color: #fff; font-size: .75rem; line-height: 1.6; text-align: center;
}

main { max-width: 1100px; margin: 0 auto; padding: 24px 16px 80px; }
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 16px 20px; margin-bottom: 16px;
}
.grid { display: grid; gap: 16px; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); }
.row { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.spread { display: flex; justify-content: space-between; gap: 12px; align-items: baseline; flex-wrap: wrap; }

.hero { padding: 24px; }
.hero .label { color: var(--muted); }
.hero .big { font-size: 2.6rem; font-weight: 700; line-height: 1.3; }
.equation { display: grid; grid-template-columns: 1fr auto; gap: 2px 16px; margin-top: 12px; max-width: 520px; }
.equation .total { border-top: 1px solid var(--line); padding-top: 4px; font-weight: 600; }

table { width: 100%; border-collapse: collapse; font-size: .95rem; }
th, td { text-align: start; padding: 8px 6px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { color: var(--muted); font-weight: 500; font-size: .85rem; }
td.amount, th.amount { text-align: end; }
tr.clickable { cursor: pointer; }
tr.clickable:hover { background: var(--surface-2); }
.table-wrap { overflow-x: auto; }

.chip {
  display: inline-block; padding: 0 10px; border-radius: 999px; font-size: .8rem; line-height: 1.9;
  background: var(--surface-2); color: var(--muted); white-space: nowrap;
}
.chip.good { background: var(--accent-soft); color: var(--accent); }
.chip.bad { background: var(--bad-soft); color: var(--bad); }
.chip.warn { background: var(--warn-soft); color: var(--warn); }
.chip.info { background: var(--info-soft); color: var(--info); }

.item { border-inline-start: 4px solid var(--line); }
.item.block { border-inline-start-color: var(--bad); }
.item.check { border-inline-start-color: var(--warn); }
.item.info { border-inline-start-color: var(--info); }
.item .actions { margin-top: 8px; }

button, .btn {
  font: inherit; font-size: .95rem; cursor: pointer;
  border: 1px solid var(--line); background: var(--surface); color: var(--text);
  padding: 4px 14px; border-radius: 8px; line-height: 1.8;
}
button.primary { background: var(--accent); color: #fff; border-color: var(--accent); }
@media (prefers-color-scheme: dark) { button.primary { color: #10231b; } }
button.danger { color: var(--bad); border-color: var(--bad); }
button:disabled { opacity: .5; cursor: not-allowed; }
input, select, textarea {
  font: inherit; font-size: .95rem; color: var(--text); background: var(--surface);
  border: 1px solid var(--line); border-radius: 8px; padding: 4px 10px; line-height: 1.8; max-width: 100%;
}
textarea { width: 100%; min-height: 80px; }
label { display: block; font-size: .9rem; color: var(--muted); }
label > input, label > select, label > textarea { display: block; margin-top: 2px; width: 100%; color: var(--text); }
.form { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); align-items: end; }

.bar { height: 8px; border-radius: 999px; background: var(--surface-2); overflow: hidden; }
.bar > span { display: block; height: 100%; background: var(--accent); }
.bar.over > span { background: var(--bad); }

dialog { border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); color: var(--text); padding: 20px; width: min(560px, calc(100vw - 32px)); }
dialog::backdrop { background: rgb(0 0 0 / .35); }

#toast {
  position: fixed; bottom: 16px; inset-inline: 16px; margin: 0 auto; max-width: 560px;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; z-index: 20;
}
#toast.error { background: var(--bad); color: #fff; }

.tabs { display: flex; gap: 6px; margin-bottom: 12px; flex-wrap: wrap; }
.tabs button.active { background: var(--accent-soft); color: var(--accent); border-color: var(--accent); }
.empty { color: var(--muted); padding: 12px 0; }
.drop { border: 2px dashed var(--line); border-radius: var(--radius); padding: 24px; text-align: center; color: var(--muted); }

@media (max-width: 640px) {
  body { font-size: 17px; }
  .top { flex-direction: column; align-items: stretch; gap: 4px; }
  .hero .big { font-size: 2rem; }
  .hide-sm { display: none; }
  th, td { padding: 6px 4px; }
}

/* sign-in and setup */
.auth { max-width: 460px; margin: 40px auto; }
.stack { display: grid; gap: 12px; }
#hh { color: var(--muted); font-weight: 400; margin-inline-start: 6px; }
[hidden] { display: none !important; }

/* assistant */
.chat { display: grid; grid-template-columns: 240px 1fr; gap: 16px; align-items: start; }
.threads { display: flex; flex-direction: column; gap: 4px; max-height: 70vh; overflow-y: auto; }
.threads a { text-decoration: none; color: var(--text); padding: 4px 8px; border-radius: 8px; font-size: .9rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.threads a.active { background: var(--accent-soft); color: var(--accent); }
.conversation { display: flex; flex-direction: column; min-height: 60vh; }
#messages { flex: 1; overflow-y: auto; max-height: 62vh; display: flex; flex-direction: column; gap: 10px; padding-bottom: 8px; }
.msg { padding: 8px 14px; border-radius: 14px; max-width: 85%; white-space: normal; }
.msg.user { align-self: flex-start; background: var(--accent-soft); }
.msg.assistant { align-self: flex-end; background: var(--surface-2); }
.msg ul { margin: 4px 0; padding-inline-start: 20px; }
.msg .status { color: var(--muted); font-size: .85rem; }
.msg.pending .status::after { content: ''; }
.proposal { align-self: flex-end; width: min(520px, 100%); border: 1px solid var(--warn); background: var(--warn-soft); border-radius: 12px; padding: 10px 14px; }
.ask { display: flex; gap: 8px; align-items: flex-end; margin-top: 8px; }
.ask textarea { flex: 1; min-height: 48px; }
.empty-chat { display: flex; flex-direction: column; gap: 8px; align-items: flex-start; color: var(--muted); }
.suggest { text-align: start; }
@media (max-width: 760px) {
  .chat { grid-template-columns: 1fr; }
  .threads { max-height: 160px; }
  .msg { max-width: 100%; }
}

/* phone: bottom tab bar with the four main screens, the rest under «المزيد» */
.more { display: none; }
@media (max-width: 640px) {
  main { padding-bottom: calc(96px + env(safe-area-inset-bottom)); }
  .top { padding-top: calc(8px + env(safe-area-inset-top)); }
  #nav {
    position: fixed; bottom: 0; inset-inline: 0; z-index: 10;
    background: var(--surface); border-top: 1px solid var(--line);
    display: grid; grid-template-columns: repeat(5, 1fr); gap: 0;
    padding: 4px 4px calc(4px + env(safe-area-inset-bottom));
    overflow: visible;
  }
  #nav a, #nav .more { text-align: center; padding: 8px 2px; font-size: .85rem; border-radius: 10px; }
  #nav a:not(.primary) { display: none; }
  #nav .more { display: block; border: 0; background: none; color: var(--muted); }
  #nav.open a:not(.primary) {
    display: block; position: static; grid-column: span 5; text-align: start; padding: 10px 16px; font-size: 1rem;
  }
  #nav.open { grid-template-columns: repeat(5, 1fr); max-height: 70vh; overflow-y: auto; box-shadow: 0 -8px 24px rgb(0 0 0 / .12); }
  #nav .badge { position: relative; top: -2px; }
  .ask { position: sticky; bottom: 0; background: var(--surface); padding-bottom: 4px; }
  #messages { max-height: none; }
  input, select, textarea { font-size: 16px; } /* keeps iOS from zooming into fields */
}
@media (max-width: 640px) { #nav.open a:not(.primary) { order: 1; } }
