:root {
  --maroon: #8B1E3F;
  --maroon-dark: #5C1429;
  --maroon-deep: #3D0D1C;
  --maroon-soft: #A63A58;
  --mustard: #E3A008;
  --mustard-dark: #C4840A;
  --mustard-soft: #F0C14B;
  --cream: #F7EBD8;
  --cream-dark: #EBD4B3;
  --paper: #FBF3E6;
  --ink: #2A1A16;
  --ink-soft: #5A433A;
  --muted: #8A7064;
  --green: #2E7D4F;
  --green-bg: #E5F3EA;
  --amber: #C4840A;
  --amber-bg: #FBF0D4;
  --red: #A31D1D;
  --red-bg: #F8E4E4;
  --blue: #1E5A6C;
  --blue-bg: #E4F1F4;
  --white: #FFFCF7;
  --shadow: 0 8px 24px rgba(61, 13, 28, 0.12);
  --shadow-sm: 0 2px 8px rgba(61, 13, 28, 0.1);
  --radius: 16px;
  --nav-h: 68px;
  --font-ui: "Nunito", "Segoe UI", system-ui, sans-serif;
  --font-hand: "Kalam", "Patrick Hand", cursive;
  --font-ledger: "Libre Baskerville", "Times New Roman", serif;
  --font-urdu: "Noto Nastaliq Urdu", "Noto Naskh Arabic", serif;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body {
  height: 100%;
  background: var(--maroon-deep);
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: 16px;
  line-height: 1.45;
  -webkit-tap-highlight-color: transparent;
}
body {
  background-color: var(--cream);
  background-image: url("/icons/paper.png");
  background-repeat: repeat;
  background-size: 256px 256px;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; }
img { max-width: 100%; display: block; }
a { color: var(--maroon); }
.hidden { display: none !important; }

#app {
  height: 100dvh;
  max-width: 480px;
  margin: 0 auto;
  position: relative;
  overflow: hidden;
  background: var(--cream);
  background-image:
    url("/icons/damask.png"),
    url("/icons/paper.png");
  background-repeat: repeat, repeat;
  background-size: 192px 192px, 256px 256px;
  box-shadow: 0 0 0 1px rgba(139, 30, 63, 0.08), 0 0 40px rgba(61, 13, 28, 0.25);
  display: flex;
  flex-direction: column;
}

/* RTL */
[dir="rtl"] { font-family: var(--font-urdu), var(--font-ui); }
[dir="rtl"] .hand, [dir="rtl"] h1, [dir="rtl"] h2 { font-family: var(--font-urdu), var(--font-hand); }
[dir="rtl"] .ledger-num { font-family: var(--font-ledger); direction: ltr; display: inline-block; }

.hand { font-family: var(--font-hand); font-weight: 700; letter-spacing: 0.01em; }
.ledger-num { font-family: var(--font-ledger); font-variant-numeric: tabular-nums; }

/* ===== Landing ===== */
.landing {
  flex: 1;
  min-height: 0;
  overflow: auto;
  padding: 0 0 28px;
  -webkit-overflow-scrolling: touch;
}
.landing-hero {
  background:
    linear-gradient(180deg, var(--maroon-deep) 0%, var(--maroon) 70%, #9a2548 100%);
  color: var(--cream);
  padding: 22px 22px 28px;
  position: relative;
  overflow: hidden;
  text-align: center;
}
.landing-hero::before {
  content: "";
  position: absolute;
  inset: 0;
  background-image: url("/icons/damask.png");
  background-size: 160px;
  opacity: 0.18;
  pointer-events: none;
}
.landing-hero::after {
  content: "";
  position: absolute;
  left: 12px; right: 12px; bottom: 8px;
  height: 4px;
  background: repeating-linear-gradient(90deg, var(--mustard) 0 14px, transparent 14px 20px);
  opacity: 0.9;
}
.hero-inner { position: relative; z-index: 1; }
.brand-icon {
  width: 64px; height: 64px;
  margin: 0 auto 8px;
  filter: drop-shadow(0 6px 10px rgba(0,0,0,0.25));
}
.landing-hero h1 {
  font-family: var(--font-hand);
  font-size: 2.2rem;
  line-height: 1.1;
  color: #fff8e8;
  text-shadow: 0 2px 0 rgba(0,0,0,0.18);
}
.landing-hero .tagline {
  margin-top: 6px;
  font-size: 1.02rem;
  color: var(--mustard-soft);
  font-weight: 600;
}
.stamp {
  display: inline-block;
  margin-top: 12px;
  border: 2px solid var(--mustard);
  color: var(--mustard);
  padding: 2px 10px;
  font-weight: 800;
  letter-spacing: 0.14em;
  font-size: 0.7rem;
  transform: rotate(-8deg);
  border-radius: 4px;
  text-transform: uppercase;
}
.landing-body { padding: 16px 16px 8px; display: flex; flex-direction: column; gap: 12px; }
.card {
  background: var(--white);
  border: 1px solid rgba(139, 30, 63, 0.12);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  padding: 14px;
}
.card h2 {
  font-family: var(--font-hand);
  font-size: 1.45rem;
  color: var(--maroon);
  margin-bottom: 6px;
}
.problem p { color: var(--ink-soft); }
.steps { display: flex; flex-direction: column; gap: 12px; }
.step {
  display: flex; gap: 12px; align-items: flex-start;
}
.step-num {
  flex: 0 0 36px; height: 36px;
  border-radius: 50%;
  background: var(--mustard);
  color: var(--maroon-deep);
  font-weight: 800;
  display: grid; place-items: center;
  font-size: 1.05rem;
  box-shadow: 0 2px 0 var(--mustard-dark);
}
.step h3 { font-size: 1rem; color: var(--maroon-dark); margin-bottom: 2px; }
.step p { font-size: 0.92rem; color: var(--ink-soft); }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 52px; padding: 0 20px;
  border-radius: 999px;
  font-weight: 800;
  font-size: 1.05rem;
  box-shadow: 0 3px 0 rgba(0,0,0,0.15);
  transition: transform 0.08s ease;
}
.btn:active { transform: translateY(2px); box-shadow: 0 1px 0 rgba(0,0,0,0.15); }
.btn-primary {
  background: linear-gradient(180deg, var(--mustard-soft), var(--mustard));
  color: var(--maroon-deep);
  width: 100%;
  border: 1px solid #d49206;
}
.btn-maroon {
  background: linear-gradient(180deg, var(--maroon-soft), var(--maroon));
  color: #fff8e8;
  border: 1px solid var(--maroon-dark);
}
.btn-ghost {
  background: transparent;
  color: var(--maroon);
  box-shadow: none;
  border: 1.5px solid rgba(139,30,63,0.25);
  min-height: 44px;
}
.btn-wa {
  background: #1f8b4c;
  color: white;
  min-height: 46px;
  font-size: 0.95rem;
  width: 100%;
}
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.demo-note {
  text-align: center;
  font-size: 0.82rem;
  color: var(--muted);
  padding: 4px 8px 8px;
}

/* ===== App chrome ===== */
.app-header {
  position: sticky; top: 0; z-index: 20;
  background: linear-gradient(180deg, var(--maroon-dark), var(--maroon));
  color: var(--cream);
  min-height: 56px;
  display: flex; align-items: center; gap: 10px;
  padding: 8px 12px;
  box-shadow: 0 2px 10px rgba(61,13,28,0.25);
}
.app-header .back, .app-header .icon-btn {
  width: 44px; height: 44px;
  display: grid; place-items: center;
  border-radius: 50%;
  color: var(--cream);
  font-size: 1.3rem;
  flex: 0 0 44px;
}
.app-header h1 {
  font-family: var(--font-hand);
  font-size: 1.35rem;
  flex: 1;
  line-height: 1.15;
}
.app-header .sub {
  display: block;
  font-family: var(--font-ui);
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--mustard-soft);
  letter-spacing: 0.02em;
}
.app-header .dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: #7DDA8A;
  display: inline-block;
  margin-right: 4px;
  box-shadow: 0 0 0 3px rgba(125,218,138,0.25);
}
[dir="rtl"] .app-header .dot { margin-right: 0; margin-left: 4px; }

.screen {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
  padding-bottom: var(--nav-h);
}

/* ===== Bottom nav ===== */
.nav {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: 0;
  width: 100%;
  max-width: 480px;
  height: var(--nav-h);
  background: var(--white);
  border-top: 1px solid rgba(139,30,63,0.12);
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  padding: 4px 4px 8px;
  z-index: 30;
  box-shadow: 0 -6px 20px rgba(61,13,28,0.08);
}
.nav button {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 2px;
  color: var(--muted);
  font-size: 0.7rem;
  font-weight: 700;
  min-height: 52px;
  border-radius: 12px;
}
.nav button svg { width: 22px; height: 22px; }
.nav button.active { color: var(--maroon); background: rgba(139,30,63,0.08); }

/* ===== Chat ===== */
.chat-wrap { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.messages {
  flex: 1;
  overflow: auto;
  padding: 14px 12px 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  -webkit-overflow-scrolling: touch;
}
.bubble {
  max-width: 86%;
  padding: 10px 12px;
  border-radius: 16px;
  font-size: 0.98rem;
  white-space: pre-wrap;
  word-wrap: break-word;
  line-height: 1.4;
  position: relative;
}
.bubble.user {
  align-self: flex-end;
  background: var(--maroon);
  color: #fff8e8;
  border-bottom-right-radius: 4px;
}
.bubble.bot {
  align-self: flex-start;
  background: var(--white);
  color: var(--ink);
  border: 1px solid rgba(139,30,63,0.1);
  border-bottom-left-radius: 4px;
  box-shadow: var(--shadow-sm);
}
[dir="rtl"] .bubble.user { align-self: flex-start; border-radius: 16px 16px 16px 4px; }
[dir="rtl"] .bubble.bot { align-self: flex-end; border-radius: 16px 16px 4px 16px; }
.bubble .meta {
  display: block;
  font-size: 0.68rem;
  opacity: 0.65;
  margin-top: 4px;
  text-align: right;
}
[dir="rtl"] .bubble .meta { text-align: left; }
.msg-card {
  align-self: flex-start;
  width: 92%;
  background: var(--white);
  border: 1px solid rgba(227,160,8,0.45);
  border-left: 4px solid var(--mustard);
  border-radius: 12px;
  padding: 12px;
  box-shadow: var(--shadow-sm);
}
[dir="rtl"] .msg-card { border-left: 0; border-right: 4px solid var(--mustard); }
.msg-card h3 {
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--mustard-dark);
  margin-bottom: 6px;
  font-weight: 800;
}
.msg-card .preview {
  background: var(--paper);
  border: 1px dashed rgba(139,30,63,0.25);
  border-radius: 8px;
  padding: 10px;
  font-size: 0.92rem;
  white-space: pre-wrap;
  margin-bottom: 10px;
}
.msg-card .row-btns { display: flex; gap: 8px; }
.msg-card .row-btns .btn { flex: 1; min-height: 44px; font-size: 0.88rem; }

.chips {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 6px 12px 8px;
  scrollbar-width: none;
}
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: 0 0 auto;
  background: var(--white);
  border: 1px solid rgba(139,30,63,0.18);
  color: var(--maroon-dark);
  padding: 8px 12px;
  border-radius: 999px;
  font-size: 0.82rem;
  font-weight: 700;
  min-height: 40px;
  box-shadow: var(--shadow-sm);
}
.chip:active { background: var(--cream-dark); }
.chips-label {
  padding: 0 14px;
  font-size: 0.75rem;
  color: var(--muted);
  font-weight: 700;
}

.composer {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px 10px;
  background: var(--white);
  border-top: 1px solid rgba(139,30,63,0.1);
}
.composer input {
  flex: 1;
  min-height: 48px;
  border: 1.5px solid rgba(139,30,63,0.18);
  border-radius: 24px;
  padding: 0 16px;
  background: var(--paper);
  outline: none;
}
.composer input:focus { border-color: var(--maroon); background: #fff; }
.composer .icon-btn, .composer .send-btn {
  width: 48px; height: 48px;
  border-radius: 50%;
  display: grid; place-items: center;
  flex: 0 0 48px;
}
.composer .send-btn {
  background: var(--maroon);
  color: var(--cream);
}
.composer .icon-btn {
  background: var(--cream);
  color: var(--maroon);
  border: 1px solid rgba(139,30,63,0.15);
}
.composer .icon-btn.listening {
  background: var(--red);
  color: white;
  animation: pulse 1s infinite;
}
@keyframes pulse {
  0%,100% { box-shadow: 0 0 0 0 rgba(163,29,29,0.4); }
  50% { box-shadow: 0 0 0 8px rgba(163,29,29,0); }
}

/* ===== Ledger ===== */
.toolbar {
  padding: 10px 12px;
  display: flex;
  gap: 8px;
  align-items: center;
}
.toolbar input[type="search"], .toolbar input[type="text"], .field input, .field select {
  flex: 1;
  min-height: 46px;
  border: 1.5px solid rgba(139,30,63,0.18);
  border-radius: 12px;
  padding: 0 14px;
  background: var(--white);
  outline: none;
  width: 100%;
}
.toolbar input:focus, .field input:focus { border-color: var(--maroon); }
.list { flex: 1; min-height: 0; overflow: auto; padding: 0 12px 20px; }
.cust {
  display: flex; align-items: center; gap: 10px;
  background: var(--white);
  border: 1px solid rgba(139,30,63,0.1);
  border-radius: 14px;
  padding: 10px 12px;
  margin-bottom: 8px;
  min-height: 72px;
  text-align: left;
  width: 100%;
  box-shadow: var(--shadow-sm);
  background-image: repeating-linear-gradient(
    180deg,
    transparent,
    transparent 23px,
    rgba(139,30,63,0.04) 24px
  );
}
[dir="rtl"] .cust { text-align: right; }
.cust:active { background-color: var(--paper); }
.avatar {
  width: 44px; height: 44px; border-radius: 50%;
  display: grid; place-items: center;
  font-weight: 800; color: var(--cream);
  background: var(--maroon);
  flex: 0 0 44px;
  font-family: var(--font-hand);
  font-size: 1.2rem;
}
.cust .who { flex: 1; min-width: 0; }
.cust .who strong { display: block; font-size: 1.02rem; }
.cust .who span { font-size: 0.78rem; color: var(--muted); }
.cust .amt { text-align: right; }
[dir="rtl"] .cust .amt { text-align: left; }
.cust .amt .ledger-num { font-size: 1rem; font-weight: 700; }
.pip {
  display: inline-flex; align-items: center; gap: 4px;
  font-size: 0.72rem; font-weight: 700;
  padding: 2px 8px; border-radius: 999px;
}
.pip.green { background: var(--green-bg); color: var(--green); }
.pip.amber { background: var(--amber-bg); color: #8a5a00; }
.pip.red { background: var(--red-bg); color: var(--red); }
.pip.ok { background: var(--blue-bg); color: var(--blue); }
.fab {
  position: fixed;
  right: max(16px, calc(50vw - 240px + 16px));
  bottom: calc(var(--nav-h) + 16px);
  width: 56px; height: 56px;
  border-radius: 50%;
  background: linear-gradient(180deg, var(--mustard-soft), var(--mustard));
  color: var(--maroon-deep);
  font-size: 2rem;
  font-weight: 700;
  box-shadow: 0 6px 16px rgba(196,132,10,0.45);
  z-index: 25;
  display: grid; place-items: center;
  line-height: 1;
}
[dir="rtl"] .fab { right: auto; left: max(16px, calc(50vw - 240px + 16px)); }
.empty {
  text-align: center; color: var(--muted); padding: 40px 16px;
}

/* ===== Customer detail ===== */
.detail { flex: 1; min-height: 0; overflow: auto; padding: 12px 12px 28px; }
.balance-hero {
  background: linear-gradient(160deg, var(--maroon-dark), var(--maroon));
  color: var(--cream);
  border-radius: 16px;
  padding: 18px 16px;
  text-align: center;
  margin-bottom: 12px;
  position: relative;
  overflow: hidden;
}
.balance-hero::before {
  content: "";
  position: absolute; inset: 0;
  background-image: url("/icons/damask.png");
  opacity: 0.15;
}
.balance-hero > * { position: relative; }
.balance-hero .label { font-size: 0.8rem; opacity: 0.8; }
.balance-hero .big { font-size: 2rem; margin: 4px 0; }
.tx {
  display: flex; justify-content: space-between; gap: 8px;
  padding: 10px 4px;
  border-bottom: 1px dashed rgba(139,30,63,0.15);
  font-size: 0.92rem;
}
.tx .up { color: var(--red); font-weight: 700; }
.tx .down { color: var(--green); font-weight: 700; }
.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 12px 0; }

/* ===== Dashboard ===== */
.dash { flex: 1; min-height: 0; overflow: auto; padding: 14px 14px 28px; }
.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 12px; }
.stat {
  background: var(--white);
  border-radius: 16px;
  padding: 14px;
  border: 1px solid rgba(139,30,63,0.1);
  box-shadow: var(--shadow-sm);
}
.stat.wide { grid-column: 1 / -1; background: linear-gradient(160deg, var(--maroon-dark), var(--maroon)); color: var(--cream); }
.stat .k { font-size: 0.78rem; color: var(--muted); font-weight: 700; }
.stat.wide .k { color: var(--mustard-soft); }
.stat .v { font-size: 1.45rem; margin-top: 4px; }
.chart-card svg { width: 100%; height: auto; display: block; }
.legend { display: flex; gap: 14px; font-size: 0.75rem; color: var(--muted); margin-top: 8px; font-weight: 700; }
.swatch { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 4px; }
[dir="rtl"] .swatch { margin-right: 0; margin-left: 4px; }
.debtor {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 0;
  border-bottom: 1px dashed rgba(139,30,63,0.12);
}
.bar { flex: 1; height: 8px; background: var(--cream-dark); border-radius: 99px; overflow: hidden; }
.bar > span { display: block; height: 100%; background: var(--maroon); border-radius: 99px; }

/* ===== Settings ===== */
.settings { flex: 1; min-height: 0; overflow: auto; padding: 14px 14px 28px; }
.field { margin-bottom: 14px; }
.field label {
  display: block;
  font-size: 0.8rem;
  font-weight: 800;
  color: var(--maroon-dark);
  margin-bottom: 6px;
}
.lang-switch { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 6px; }
.lang-switch button {
  min-height: 46px;
  border-radius: 12px;
  border: 1.5px solid rgba(139,30,63,0.18);
  background: var(--white);
  font-weight: 700;
  font-size: 0.85rem;
}
.lang-switch button.active {
  background: var(--maroon);
  color: var(--cream);
  border-color: var(--maroon-dark);
}
.settings .btn { width: 100%; margin-bottom: 8px; }
.danger { color: var(--red); border-color: rgba(163,29,29,0.35) !important; }

/* ===== Modal ===== */
.modal-bg {
  position: fixed; inset: 0;
  background: rgba(42, 26, 22, 0.45);
  z-index: 50;
  display: grid;
  place-items: end center;
}
.sheet {
  width: 100%;
  max-width: 480px;
  background: var(--paper);
  background-image: url("/icons/paper.png");
  border-radius: 20px 20px 0 0;
  padding: 18px 16px 24px;
  box-shadow: 0 -10px 30px rgba(0,0,0,0.2);
  max-height: 88dvh;
  overflow: auto;
}
.sheet h2 { font-family: var(--font-hand); color: var(--maroon); font-size: 1.5rem; margin-bottom: 10px; }
.toast {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(var(--nav-h) + 16px);
  background: var(--ink);
  color: var(--cream);
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 0.88rem;
  z-index: 80;
  max-width: 90%;
  box-shadow: var(--shadow);
}

.ornament {
  height: 10px;
  background: repeating-linear-gradient(90deg, var(--mustard) 0 10px, var(--maroon) 10px 14px, var(--cream-dark) 14px 20px);
  opacity: 0.85;
}
