:root {
  --bg: #f6f1ea;
  --surface: #ffffff;
  --surface-2: #f0e9df;
  --text: #2c2622;
  --muted: #8a7f74;
  --border: #e3d9cb;
  --primary: #c05a2b;
  --primary-ink: #ffffff;
  --pos: #2f7d4f;
  --neg: #c23b3b;
  --shadow: 0 1px 3px rgba(60, 45, 30, .12), 0 8px 24px rgba(60, 45, 30, .08);
  --radius: 14px;
  font-family: "Vazirmatn", ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1815;
    --surface: #272220;
    --surface-2: #322b27;
    --text: #efe7dd;
    --muted: #a99e91;
    --border: #3c342f;
    --primary: #e07a45;
    --primary-ink: #1c1815;
    --pos: #6bce93;
    --neg: #f0817f;
    --shadow: 0 1px 3px rgba(0,0,0,.4), 0 8px 24px rgba(0,0,0,.3);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  -webkit-tap-highlight-color: transparent;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; font-size: 1.05rem; display: flex; align-items: center; gap: 8px; }
.brand span { white-space: nowrap; }
.topbar-actions { display: flex; align-items: center; gap: 8px; }

main { max-width: 720px; margin: 0 auto; padding: 16px; padding-bottom: 96px; }
.view h1 { font-size: 1.4rem; margin: 8px 0 4px; }
h2 { font-size: 1.05rem; margin: 0 0 10px; }

.muted { color: var(--muted); }
.small { font-size: .82rem; }

/* buttons */
.icon-btn {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  width: 40px; height: 40px;
  border-radius: 10px;
  font-size: 1.1rem;
  cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
}
.icon-btn:hover { background: var(--surface-2); }
.btn-primary {
  border: none;
  background: var(--primary);
  color: var(--primary-ink);
  padding: 12px 18px;
  border-radius: 12px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  width: 100%;
  margin-top: 12px;
}
.btn-ghost {
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  padding: 10px 14px;
  border-radius: 10px;
  font: inherit;
  cursor: pointer;
}
.link-btn {
  border: none; background: none; color: var(--primary);
  font: inherit; cursor: pointer; padding: 4px 0; margin-bottom: 12px;
}
.chip {
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  padding: 7px 12px;
  border-radius: 999px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  max-width: 44vw;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

/* profiles */
.profile-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 12px;
  margin-top: 16px;
}
.profile-card {
  position: relative;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: var(--radius);
  padding: 18px 12px 14px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  font: inherit;
  color: var(--text);
  box-shadow: var(--shadow);
}
.profile-card:hover { border-color: var(--primary); }
.avatar {
  width: 52px; height: 52px;
  border-radius: 50%;
  background: var(--primary);
  color: var(--primary-ink);
  display: flex; align-items: center; justify-content: center;
  font-size: 1.4rem; font-weight: 700;
}
.p-name { font-weight: 600; }
.p-edit {
  position: absolute; top: 6px; inset-inline-end: 8px;
  font-size: .9rem; color: var(--muted);
  padding: 4px;
}

/* month nav */
.month-nav {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; margin: 4px 0 4px;
}
.month-label { text-align: center; font-size: 1.15rem; font-weight: 700; }
.month-label .muted { font-weight: 500; margin-inline-start: 6px; }

/* overview cards */
.cards {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  margin-bottom: 16px;
}
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  box-shadow: var(--shadow);
}
.card-label { font-size: .8rem; color: var(--muted); margin-bottom: 6px; }
.card-value { font-size: 1.15rem; font-weight: 700; }
.card-hint { font-size: .78rem; color: var(--muted); margin-top: 2px; }
#ovBalanceCard.pos .card-value { color: var(--pos); }
#ovBalanceCard.neg .card-value { color: var(--neg); }

/* panels */
.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 16px;
  box-shadow: var(--shadow);
}
.cal-head { display: flex; align-items: baseline; justify-content: space-between; }

.balance-list { list-style: none; margin: 0; padding: 0; }
.balance-list li {
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: 10px;
  align-items: center;
  padding: 9px 0;
  border-bottom: 1px solid var(--border);
}
.balance-list li:last-child { border-bottom: none; }
.b-name { font-weight: 600; }
.b-spent { color: var(--muted); font-size: .9rem; }
.b-bal { font-size: .9rem; font-weight: 600; white-space: nowrap; }
.b-bal.pos { color: var(--pos); }
.b-bal.neg { color: var(--neg); }

/* calendar */
.cal-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 5px;
  margin-top: 10px;
}
.cal-wd {
  text-align: center;
  font-size: .72rem;
  color: var(--muted);
  padding-bottom: 4px;
}
.cal-cell {
  aspect-ratio: 1 / 1;
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 4px;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: space-between;
  cursor: pointer;
  background: var(--surface);
  overflow: hidden;
}
.cal-cell.empty { border: none; background: transparent; cursor: default; }
.cal-cell:not(.empty):hover { border-color: var(--primary); }
.cal-cell.today { border-color: var(--primary); box-shadow: inset 0 0 0 1px var(--primary); }
.cal-d { font-size: .8rem; font-weight: 600; }
.cal-amt {
  font-size: .62rem;
  font-weight: 700;
  color: var(--primary);
  align-self: stretch;
  line-height: 1.1;
  word-break: break-word;
}

/* FAB */
.fab {
  position: fixed;
  inset-block-end: 20px;
  inset-inline-end: 20px;
  width: 56px; height: 56px;
  border-radius: 50%;
  border: none;
  background: var(--primary);
  color: var(--primary-ink);
  font-size: 1.8rem;
  line-height: 1;
  cursor: pointer;
  box-shadow: 0 6px 18px rgba(0,0,0,.28);
  z-index: 6;
}

/* modal */
.modal-root {
  position: fixed; inset: 0; z-index: 20;
  display: flex; align-items: flex-end; justify-content: center;
}
.modal-backdrop { position: absolute; inset: 0; background: rgba(20,15,10,.5); }
.modal {
  position: relative;
  background: var(--surface);
  width: 100%;
  max-width: 560px;
  max-height: 88vh;
  overflow-y: auto;
  border-radius: 18px 18px 0 0;
  box-shadow: var(--shadow);
  animation: slideUp .18s ease-out;
}
@media (min-width: 620px) {
  .modal-root { align-items: center; }
  .modal { border-radius: 18px; }
}
@keyframes slideUp { from { transform: translateY(24px); opacity: .6; } to { transform: translateY(0); opacity: 1; } }
.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; background: var(--surface);
}
.modal-head h3 { margin: 0; font-size: 1.05rem; }
.modal-body { padding: 16px; }

/* forms */
label { display: block; font-size: .85rem; color: var(--muted); margin: 12px 0 6px; }
input[type="text"], input[type="number"], input[type="date"], select {
  width: 100%;
  padding: 11px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
}
.item-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(84px, 1fr));
  gap: 8px;
  margin-top: 6px;
}
.item-btn {
  border: 1px solid var(--border);
  background: var(--bg);
  border-radius: 12px;
  padding: 10px 6px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
  cursor: pointer;
  font: inherit;
  color: var(--text);
}
.item-btn .emoji { font-size: 1.5rem; }
.item-btn .lbl { font-size: .74rem; }
.item-btn .lbl-fa { font-size: .7rem; color: var(--muted); }
.item-btn.active {
  border-color: var(--primary);
  background: color-mix(in srgb, var(--primary) 14%, var(--bg));
  box-shadow: inset 0 0 0 1px var(--primary);
}
.afghan-date { font-weight: 600; color: var(--primary); }

.entry-list { list-style: none; padding: 0; margin: 8px 0 0; }
.entry-list li {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 0; border-bottom: 1px solid var(--border);
}
.entry-list li:last-child { border-bottom: none; }
.entry-list .di { font-size: 1.3rem; }
.entry-list .dt { flex: 1; }
.entry-list .dt small { display: block; color: var(--muted); }
.entry-list .dp { font-weight: 700; white-space: nowrap; }
.entry-list .dx { color: var(--neg); cursor: pointer; border: none; background: none; font-size: 1rem; padding: 4px; }

/* segmented control (paid from) */
.seg { display: flex; gap: 8px; }
.seg-btn {
  flex: 1;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  border-radius: 10px;
  padding: 10px 8px;
  font: inherit;
  cursor: pointer;
}
.seg-btn.active {
  border-color: var(--primary);
  background: color-mix(in srgb, var(--primary) 14%, var(--bg));
  box-shadow: inset 0 0 0 1px var(--primary);
  font-weight: 600;
}

.btn-sm {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 9px;
  padding: 6px 10px;
  font: inherit;
  font-size: .85rem;
  cursor: pointer;
}
.btn-sm:hover { background: var(--surface-2); }
h4.sec { margin: 4px 0 8px; font-size: .95rem; }

/* your standing */
.mini-cards {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
}
.mini {
  background: var(--surface-2);
  border-radius: 10px;
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 3px;
}
.mini span { font-size: .76rem; color: var(--muted); }
.mini b { font-size: 1.02rem; }
.you-balance {
  margin-top: 12px;
  padding: 10px 12px;
  border-radius: 10px;
  font-weight: 600;
  background: var(--surface-2);
}
.you-balance.pos { color: var(--pos); background: color-mix(in srgb, var(--pos) 12%, var(--surface)); }
.you-balance.neg { color: var(--neg); background: color-mix(in srgb, var(--neg) 12%, var(--surface)); }
.card-value.neg { color: var(--neg); }

/* everyone */
.people-list { display: flex; flex-direction: column; gap: 10px; }
.person {
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px;
}
.person-top {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 8px;
}
.pill {
  font-size: .8rem;
  font-weight: 600;
  padding: 3px 9px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
  white-space: nowrap;
}
.pill.pos { color: var(--pos); background: color-mix(in srgb, var(--pos) 15%, var(--surface)); }
.pill.neg { color: var(--neg); background: color-mix(in srgb, var(--neg) 15%, var(--surface)); }
.kv {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 6px 12px;
}
.kv > div { display: flex; justify-content: space-between; gap: 8px; font-size: .88rem; }
.kv span { color: var(--muted); }

.row { display: flex; gap: 10px; }
.row > * { flex: 1; }
.mt { margin-top: 16px; }
.danger { color: var(--neg); border-color: var(--neg); }

hr { border: none; border-top: 1px solid var(--border); margin: 14px 0; }
code {
  background: var(--surface-2);
  padding: 1px 5px;
  border-radius: 5px;
  font-size: .85em;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}

.save-note {
  position: fixed;
  inset-block-end: 16px;
  inset-inline-start: 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--muted);
  font-size: .75rem;
  padding: 7px 11px;
  border-radius: 9px;
  box-shadow: var(--shadow);
  opacity: 0;
  transform: translateY(8px);
  transition: opacity .2s ease, transform .2s ease;
  pointer-events: none;
  z-index: 30;
  max-width: 70vw;
}
.save-note.show { opacity: 1; transform: translateY(0); }
