:root {
  --teal: #0f766e;
  --teal-dark: #115e59;
  --teal-soft: #ccfbf1;
  --amber: #b45309;
  --red: #b91c1c;
  --ink: #1f2937;
  --muted: #6b7280;
  --line: #e5e7eb;
  --bg: #f8fafc;
  --card: #ffffff;
  --radius: 14px;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
[hidden] { display: none !important; }
body {
  font-family: 'Tajawal', system-ui, -apple-system, 'Segoe UI', sans-serif;
  background: var(--bg); color: var(--ink);
  padding-bottom: 84px; font-size: 16px;
}
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between;
  background: var(--teal); color: #fff; padding: 12px 16px;
}
.build-tag { font-size: 10px; opacity: 0.7; margin-inline-start: 6px; align-self: flex-end; }
.brand-name { font-weight: 800; font-size: 22px; }
.brand-sub { font-size: 11px; opacity: .75; letter-spacing: 2px; margin-inline-start: 6px; }
.icon-btn { background: none; border: none; font-size: 20px; cursor: pointer; color: inherit; padding: 6px; }
.topbar .icon-btn { color: #fff; }

.mode-banner {
  background: #fef3c7; color: #92400e; font-size: 13px;
  padding: 8px 16px; text-align: center; border-bottom: 1px solid #fde68a;
}

main { padding: 14px 14px 180px; max-width: 640px; margin: 0 auto; } /* allows last controls to scroll above the fixed assistant and navigation */

.period-pills { display: flex; gap: 8px; margin-bottom: 14px; }
.pill {
  flex: 1; padding: 8px; border-radius: 999px; border: 1px solid var(--line);
  background: #fff; font-family: inherit; font-size: 14px; cursor: pointer; color: var(--muted);
}
.pill.active { background: var(--teal); color: #fff; border-color: var(--teal); font-weight: 700; }

.cards { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.stat-card {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px; }
.stat-card .label { font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.stat-card .value { font-size: 20px; font-weight: 800; }
.stat-card .value small { font-size: 12px; font-weight: 400; color: var(--muted); }
.stat-card.good .value { color: var(--teal); }
.stat-card.bad .value { color: var(--red); }
.stat-card.warn .value { color: var(--amber); }

.alert-card {
  margin-top: 12px; background: #fff7ed; border: 1px solid #fed7aa;
  border-radius: var(--radius); padding: 12px 14px; font-size: 14px; color: #9a3412;
}

.add-toggle {
  width: 100%; padding: 12px; border: 2px dashed var(--teal); border-radius: var(--radius);
  background: var(--teal-soft); color: var(--teal-dark); font-weight: 700; font-size: 15px;
  cursor: pointer; margin-bottom: 12px; font-family: inherit;
}
.entry-form {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px; margin-bottom: 12px; display: flex; flex-direction: column; gap: 10px;
}
.entry-form input, .entry-form select {
  width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px;
  font-family: inherit; font-size: 15px; background: #fff;
}
.field { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); flex: 1; }
.field-row { display: flex; gap: 10px; }
.items-builder { display: flex; flex-direction: column; gap: 8px; }
.cat-rows { display: flex; flex-direction: column; gap: 8px; }
.cat-row { display: grid; grid-template-columns: 64px 1fr; align-items: center; gap: 10px; }
.cat-row span { font-size: 15px; font-weight: 700; }
.cat-row input { text-align: left; direction: ltr; }
.cat-row .oa { display: grid; grid-template-columns: 1fr 104px; gap: 8px; }
.cat-row .oa input[name$="_وصف"] { direction: rtl; text-align: right; }
.rec-lines { display: flex; flex-direction: column; gap: 4px; margin: 6px 0; }
.rec-line { display: grid; grid-template-columns: 1fr auto 28px; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); }
.rec-line .line-del { border: none; background: none; color: var(--red); font-size: 15px; cursor: pointer; padding: 4px; }
/* كل صنف في شبكة بسيطة: الاسم في سطر كامل، والكمية والسعر جنب بعض بخانات واسعة */
.item-row { display: grid; grid-template-columns: 1fr 1fr 1fr 28px; gap: 8px; align-items: center; padding: 8px; border: 1px solid var(--line); border-radius: 10px; }
.item-row .it-ref { grid-column: 1 / 4; }
.item-row .remove-row { grid-column: 4; grid-row: 1; border: none; background: none; color: var(--red); font-size: 18px; cursor: pointer; }
.item-row .it-dist { grid-column: 1 / 4; }
.item-row .it-disc { display: flex; flex-direction: column; gap: 2px; font-size: 11px; color: var(--muted); min-width: 0; }
.item-row .it-disc input { width: 100%; }
.item-row .it-dist[hidden] { display: none; }
.item-row .it-dist:not([hidden]) { display: flex; flex-direction: row; align-items: center; gap: 6px; font-size: 13px; }
.item-row input, .item-row select { min-width: 0; width: 100%; }

.period-sel { display: flex; gap: 8px; margin: 6px 0 2px; }
.period-sel input { padding: 8px 10px; border: 1px solid var(--line); border-radius: 10px; font-family: inherit; font-size: 14px; background: #fff; }
.range-label { font-size: 12px; color: var(--muted); margin: 2px 0 6px; min-height: 0; }
.range-label:empty { display: none; }
.alltime-strip { display: flex; flex-wrap: wrap; gap: 6px 14px; padding: 8px 12px; border: 1px dashed var(--line); border-radius: 10px; font-size: 13px; color: var(--muted); margin: 8px 0; }
.alltime-strip .at-title { font-weight: 700; color: var(--teal); }
.primary {
  background: var(--teal); color: #fff; border: none; border-radius: 10px;
  padding: 12px; font-family: inherit; font-size: 15px; font-weight: 700; cursor: pointer;
}
.primary:active { background: var(--teal-dark); }
.ghost {
  background: #fff; color: var(--muted); border: 1px solid var(--line); border-radius: 10px;
  padding: 12px; font-family: inherit; font-size: 15px; cursor: pointer;
}
.mini-btn {
  align-self: flex-start; background: none; border: none; color: var(--teal);
  font-family: inherit; font-size: 14px; font-weight: 700; cursor: pointer; padding: 4px 0;
}

.search-row { margin-bottom: 10px; }
.search {
  width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 999px;
  font-family: inherit; font-size: 14px;
}

.record-list { display: flex; flex-direction: column; gap: 10px; }
.record {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px;
}
.record .rec-head { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 4px; }
.record .rec-title { font-weight: 700; font-size: 15px; }
.record .rec-amount { font-weight: 800; color: var(--teal-dark); }
.record .rec-meta { font-size: 13px; color: var(--muted); line-height: 1.6; }
.record .rec-dist { font-size: 14px; font-weight: 700; color: var(--teal-dark); line-height: 1.6; }
.record .rec-actions { display: flex; gap: 8px; margin-top: 8px; flex-wrap: wrap; }
.chip {
  display: inline-block; font-size: 12px; padding: 2px 10px; border-radius: 999px; font-weight: 700;
}
.chip.paid { background: #dcfce7; color: #166534; }
.chip.partial { background: #fef9c3; color: #854d0e; }
.chip.credit { background: #fee2e2; color: #991b1b; }
.chip.low { background: #fee2e2; color: #991b1c; }
.chip.ok { background: #dcfce7; color: #166534; }
.small-btn {
  border: 1px solid var(--line); background: #fff; border-radius: 8px; padding: 5px 12px;
  font-family: inherit; font-size: 13px; cursor: pointer; color: var(--teal-dark); font-weight: 700;
}
.small-btn.danger { color: var(--red); }
.empty-state { text-align: center; color: var(--muted); font-size: 14px; padding: 28px 0; }

.report-table { width: 100%; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.report-row { display: flex; justify-content: space-between; padding: 12px 14px; border-bottom: 1px solid var(--line); font-size: 15px; }
.report-row:last-child { border-bottom: none; }
.report-row .k { color: var(--muted); }
.report-row .v { font-weight: 700; }
.report-row.highlight { background: var(--teal-soft); }
.report-row.highlight .v { color: var(--teal-dark); font-weight: 800; }
h3.section-title { font-size: 15px; margin: 18px 0 8px; color: var(--teal-dark); }

/* Assistant */
#assistantFab {
  position: fixed; bottom: 96px; inset-inline-start: 16px; z-index: 30;
  width: 56px; height: 56px; border-radius: 50%; border: none; font-size: 24px;
  background: var(--teal); box-shadow: 0 4px 14px rgba(15,118,110,.4); cursor: pointer;
}
#assistantPanel {
  position: fixed; inset: auto 0 0 0; z-index: 40; height: 70vh;
  background: #fff; border-radius: 20px 20px 0 0; box-shadow: 0 -4px 24px rgba(0,0,0,.18);
  display: flex; flex-direction: column;
}
.chat-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: 14px 16px; border-bottom: 1px solid var(--line); font-weight: 800; color: var(--teal-dark);
}
.form-hint { font-size: 12px; color: var(--muted); line-height: 1.8; background: var(--bg); border-radius: 10px; padding: 8px 10px; }
.chat-mode { font-size: 12px; color: var(--muted); padding: 6px 16px; background: var(--bg); }
.chat-log { flex: 1; overflow-y: auto; padding: 12px 16px; display: flex; flex-direction: column; gap: 8px; }
.msg { max-width: 85%; padding: 10px 14px; border-radius: 14px; font-size: 14px; line-height: 1.7; white-space: pre-wrap; }
.msg.user { align-self: flex-start; background: var(--teal); color: #fff; border-bottom-right-radius: 4px; }
.msg.bot { align-self: flex-end; background: #f1f5f9; border-bottom-left-radius: 4px; }
.msg .chat-confirm { display: flex; gap: 8px; margin-top: 8px; }
.msg .chat-confirm button { flex: 1; padding: 8px; border-radius: 8px; font-family: inherit; cursor: pointer; font-weight: 700; }
.msg .chat-confirm .yes { background: var(--teal); color: #fff; border: none; }
.msg .chat-confirm .no { background: #fff; border: 1px solid var(--line); color: var(--muted); }
.chat-input-row { display: flex; gap: 8px; padding: 10px 12px calc(10px + env(safe-area-inset-bottom)); border-top: 1px solid var(--line); }
.chat-input-row input { flex: 1; padding: 10px 12px; border: 1px solid var(--line); border-radius: 999px; font-family: inherit; font-size: 14px; }
.chat-input-row .primary { padding: 10px 18px; border-radius: 999px; }
#micBtn.listening { background: #fee2e2; border-radius: 50%; }

/* Modals */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 50; background: rgba(15,23,42,.45);
  display: flex; align-items: center; justify-content: center; padding: 24px;
}
.modal {
  background: #fff; border-radius: 18px; padding: 20px; width: 100%; max-width: 380px;
  display: flex; flex-direction: column; gap: 12px;
}
.modal p { font-size: 15px; line-height: 1.8; }
.modal-title { font-weight: 800; color: var(--teal-dark); }
.modal-actions { display: flex; gap: 10px; }
.modal-actions button { flex: 1; }
.export-out {
  font-size: 12px; background: var(--bg); border: 1px solid var(--line); border-radius: 10px;
  padding: 10px; max-height: 120px; overflow: auto; direction: ltr; word-break: break-all;
}
.file-label { text-align: center; display: block; }

/* Bottom nav */
.bottom-nav {
  position: fixed; bottom: 0; inset-inline: 0; z-index: 25;
  display: flex; background: #fff; border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.nav-item {
  flex: 1; border: none; background: none; padding: 8px 2px 10px;
  font-family: inherit; font-size: 11px; color: var(--muted); cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
}
.nav-item svg { display: block; }
.nav-item.active { color: var(--teal); font-weight: 800; }

/* ---- cloud auth gate & account chip ---- */
.auth-gate {
  position: fixed; inset: 0; z-index: 60;
  display: flex; align-items: center; justify-content: center;
  background: var(--bg, #f6f7f9); padding: 20px;
}
.auth-card {
  width: 100%; max-width: 360px; background: #fff; border-radius: 18px;
  padding: 26px 22px; box-shadow: 0 8px 30px rgba(15, 23, 42, .08);
  display: flex; flex-direction: column; gap: 14px;
}
.auth-card .brand { justify-content: center; display: flex; flex-direction: column; align-items: center; }
.auth-hint { margin: 0; color: #6b7280; font-size: 14px; text-align: center; line-height: 1.7; }
.auth-card label { display: flex; flex-direction: column; gap: 6px; font-size: 14px; font-weight: 500; }
.auth-card input {
  padding: 11px 12px; border: 1px solid #e5e7eb; border-radius: 10px;
  font-family: inherit; font-size: 16px; text-align: left;
}
.auth-error { margin: 0; color: #b91c1c; font-size: 13px; text-align: center; }
.auth-error.ok { color: #15803d; }
.linklike { background: none; border: none; color: #0e7490; font-family: inherit; font-size: 13px; cursor: pointer; text-decoration: underline; padding: 4px; }
.account-chip {
  position: fixed; top: 10px; left: 12px; z-index: 55;
  background: #ecfdf5; color: #065f46; border: 1px solid #a7f3d0;
  border-radius: 999px; padding: 4px 12px; font-size: 12px;
}
.account-chip .linklike { color: #065f46; font-size: 12px; padding: 0; }
