:root {
  --bg: #f5f1ea;
  --surface: #fffdf9;
  --surface-2: #efe9df;
  --line: #e2dacd;
  --text: #1d2422;
  --muted: #6b716d;
  --brand: #0f5c55;
  --brand-ink: #ffffff;
  --brand-soft: #d6ebe7;
  --aed: #0f5c55;
  --aed-soft: #dcefeb;
  --inr: #b4540d;
  --inr-soft: #fae6d4;
  --good: #1f7a3d;
  --good-soft: #dff2e4;
  --bad: #b3261e;
  --bad-soft: #fbe3e0;
  --shadow: 0 1px 2px rgba(29, 36, 34, .06), 0 4px 16px rgba(29, 36, 34, .06);
  --radius: 16px;
  --font: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --tabbar-h: 64px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0d1716;
    --surface: #15211f;
    --surface-2: #1c2b29;
    --line: #263734;
    --text: #e9efed;
    --muted: #97a5a1;
    --brand: #4fb3a6;
    --brand-ink: #062320;
    --brand-soft: #18403b;
    --aed: #6cc7ba;
    --aed-soft: #173b37;
    --inr: #f0a45f;
    --inr-soft: #3d2915;
    --good: #6fd08e;
    --good-soft: #17331f;
    --bad: #f28b82;
    --bad-soft: #3d1c19;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 16px rgba(0, 0, 0, .25);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0d1716; --surface: #15211f; --surface-2: #1c2b29; --line: #263734; --text: #e9efed; --muted: #97a5a1;
  --brand: #4fb3a6; --brand-ink: #062320; --brand-soft: #18403b; --aed: #6cc7ba; --aed-soft: #173b37;
  --inr: #f0a45f; --inr-soft: #3d2915; --good: #6fd08e; --good-soft: #17331f; --bad: #f28b82; --bad-soft: #3d1c19;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; -webkit-tap-highlight-color: transparent; }
button, a, input, select, label { touch-action: manipulation; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 var(--font);
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 24px);
  min-height: 100dvh;
}
a { color: var(--brand); }
button, input, select, textarea { font: inherit; color: inherit; }
.num { font-variant-numeric: tabular-nums; }

/* ── Top bar ── */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
}
.brand { display: flex; align-items: center; gap: 8px; text-decoration: none; color: var(--text); font-weight: 700; font-size: 19px; letter-spacing: -.01em; }
.me-chip {
  border: 1px solid var(--line); background: var(--surface); border-radius: 999px;
  padding: 6px 12px; font-size: 14px; cursor: pointer;
}

main { max-width: 640px; margin: 0 auto; padding: 4px 16px 16px; outline: none; }
.loading, .empty { color: var(--muted); text-align: center; padding: 40px 16px; }

h1 { font-size: 26px; letter-spacing: -.02em; margin: 8px 0 16px; }
h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 26px 0 10px; font-weight: 600; }
h2 .h-action { float: right; text-transform: none; letter-spacing: 0; font-size: 14px; }

/* ── Cards ── */
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 16px;
}
.stack > * + * { margin-top: 12px; }

.balances { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.bal { position: relative; overflow: hidden; display: flex; flex-direction: column; justify-content: space-between; }
.bal-info { min-width: 0; }
.bal .btn { width: 100%; }
.bal .cur { font-size: 12px; font-weight: 700; letter-spacing: .08em; padding: 3px 8px; border-radius: 999px; display: inline-block; }
.bal.AED .cur { background: var(--aed-soft); color: var(--aed); }
.bal.INR .cur { background: var(--inr-soft); color: var(--inr); }
.bal .label { margin-top: 12px; font-size: 14px; color: var(--muted); }
.bal .amt { font-size: clamp(20px, 5.5vw, 24px); font-weight: 700; letter-spacing: -.02em; margin: 2px 0 12px; overflow-wrap: anywhere; }
.bal .amt.owe { color: var(--bad); }
.bal .amt.owed { color: var(--good); }
.bal .amt.zero { color: var(--muted); font-size: 18px; }

/* ── Buttons ── */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--line); background: var(--surface); border-radius: 12px;
  padding: 10px 14px; font-weight: 600; font-size: 15px; cursor: pointer; text-decoration: none; color: var(--text);
  min-height: 44px;
}
.btn.primary { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }
.btn.danger { color: var(--bad); }
.btn.small { min-height: 34px; padding: 6px 10px; font-size: 14px; border-radius: 10px; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .5; cursor: default; }
.link-btn { background: none; border: 0; color: var(--brand); padding: 0; cursor: pointer; font-weight: 600; }

/* ── Lists ── */
.list { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.row {
  display: flex; align-items: center; gap: 12px; padding: 12px 14px; width: 100%;
  border: 0; border-top: 1px solid var(--line); background: none; text-align: left; cursor: pointer;
}
.row:first-child { border-top: 0; }
.row:active { background: var(--surface-2); }
.row .ico {
  flex: none; width: 40px; height: 40px; border-radius: 12px; background: var(--surface-2);
  display: grid; place-items: center; font-size: 20px;
}
.row .main { flex: 1; min-width: 0; }
.row .title, .row .sub { display: block; }
.row .title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row .sub { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row .right { text-align: right; flex: none; max-width: 46%; }
.row .right .amt { font-weight: 600; }
.row .right .eff { font-size: 12px; }
.row .eff-label, .row .eff-amt { display: block; white-space: nowrap; }
.row .eff-label { font-size: 12px; }
.row .eff-amt { font-size: 15px; font-weight: 700; }
.row .rec { color: var(--muted); font-weight: 400; }
.eff.owe { color: var(--bad); }
.eff.owed { color: var(--good); }
.eff.none { color: var(--muted); }
.month-head { display: flex; justify-content: space-between; align-items: baseline; }

.tag { display: inline-block; font-size: 11px; font-weight: 700; letter-spacing: .05em; padding: 1px 6px; border-radius: 6px; vertical-align: 1px; }
.tag.AED { background: var(--aed-soft); color: var(--aed); }
.tag.INR { background: var(--inr-soft); color: var(--inr); }

/* ── Due bills ── */
.due { display: flex; gap: 12px; align-items: center; border-left: 4px solid var(--inr); }
.due .main { flex: 1; min-width: 0; }
.due .title { font-weight: 600; }
.due .sub { font-size: 13px; color: var(--muted); }
.due .actions { display: flex; gap: 6px; flex: none; }

/* ── Filters / segmented ── */
.filters { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 12px; }
.filters input[type=search], .filters select { flex: 1 1 140px; min-width: 0; }
.seg { display: flex; background: var(--surface-2); border-radius: 12px; padding: 3px; gap: 3px; }
.seg button {
  flex: 1; border: 0; background: none; border-radius: 9px; padding: 8px 6px; font-weight: 600; font-size: 14px;
  cursor: pointer; color: var(--muted); min-height: 38px;
}
.seg button[aria-pressed="true"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

/* ── Forms ── */
.field { display: block; margin-top: 14px; }
.field > span { display: block; font-size: 13px; font-weight: 600; color: var(--muted); margin-bottom: 6px; }
input[type=text], input[type=search], input[type=number], input[type=date], input[type=month], input:not([type]), select, textarea {
  width: 100%; border: 1px solid var(--line); background: var(--surface); border-radius: 12px;
  padding: 11px 12px; font-size: 16px; min-height: 46px;
}
textarea { min-height: 70px; resize: vertical; }
input:focus, select:focus, textarea:focus, button:focus-visible, a:focus-visible { outline: 2px solid var(--brand); outline-offset: 1px; }
.amount-row { display: flex; gap: 8px; }
.amount-row input { flex: 1; font-size: 24px; font-weight: 700; }
.amount-row .seg { flex: none; width: 140px; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; align-items: end; }
.preview { margin-top: 12px; padding: 10px 12px; border-radius: 12px; background: var(--surface-2); font-size: 14px; }
.preview.err { background: var(--bad-soft); color: var(--bad); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chips button {
  border: 1px solid var(--line); background: var(--surface); border-radius: 999px; padding: 6px 10px; font-size: 14px; cursor: pointer;
}
.chips button[aria-pressed="true"] { background: var(--brand-soft); border-color: var(--brand); color: var(--text); font-weight: 600; }
.check { display: flex; align-items: center; gap: 8px; margin-top: 14px; }

/* ── Sheet (dialog) ── */
.sheet {
  width: 100%; max-width: 560px; max-height: 92dvh; margin: auto auto 0; padding: 0;
  border: 0; border-radius: 22px 22px 0 0; background: var(--bg); color: var(--text);
}
@media (min-width: 600px) { .sheet { margin: auto; border-radius: 22px; } }
.sheet::backdrop { background: rgba(0, 0, 0, .45); }
.sheet form { padding: 18px 16px calc(18px + env(safe-area-inset-bottom)); }
.sheet { overscroll-behavior: contain; }
.sheet-head {
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
  position: sticky; top: 0; z-index: 2; background: var(--bg);
  margin: -18px -16px 4px; padding: 16px 16px 10px;
}
.sheet-head h3 { margin: 0; font-size: 20px; line-height: 1.25; min-width: 0; }
.x { flex: none; }
.sheet-actions {
  display: flex; gap: 10px; position: sticky; bottom: 0; z-index: 2; background: var(--bg);
  margin: 20px -16px calc(-18px - env(safe-area-inset-bottom));
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
}
.sheet-actions .btn.primary { flex: 1; }
.x { border: 0; background: var(--surface-2); width: 34px; height: 34px; border-radius: 50%; font-size: 18px; cursor: pointer; }
.meta { font-size: 12px; color: var(--muted); margin-top: 14px; }

/* ── Summary ── */
.month-nav { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.month-nav strong { font-size: 18px; }
.stats { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; margin-top: 12px; }
.stat.wide { grid-column: 1 / -1; }
.stat .v.paid { display: flex; flex-wrap: wrap; gap: 2px 16px; font-size: 14px; }
.stat { background: var(--surface-2); border-radius: 12px; padding: 10px; }
.stat .k { font-size: 12px; color: var(--muted); }
.stat .v { font-weight: 700; font-size: 15px; word-break: break-word; }
.bars { margin-top: 14px; }
.bar { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 2px 8px; margin-top: 10px; font-size: 14px; }
.bar-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bar .pct { color: var(--muted); font-size: 12px; margin-left: 4px; }
.bar .track { grid-column: 1 / -1; height: 8px; background: var(--surface-2); border-radius: 99px; overflow: hidden; }
.bar .fill { height: 100%; border-radius: 99px; }
.AED .fill { background: var(--aed); }
.INR .fill { background: var(--inr); }

/* ── History ── */
.hist .row { cursor: default; align-items: flex-start; }
.hist .diff { font-size: 13px; color: var(--muted); white-space: normal; overflow-wrap: anywhere; }
.hist .title { overflow-wrap: anywhere; }
.hist .right { max-width: none; }

/* ── Tab bar ── */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 6;
  display: grid; grid-template-columns: repeat(5, 1fr); align-items: center;
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom);
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  border-top: 1px solid var(--line);
}
.tabbar a { display: flex; flex-direction: column; align-items: center; gap: 1px; text-decoration: none; color: var(--muted); font-size: 11px; font-weight: 600; }
.tabbar a .ti { font-size: 20px; line-height: 1.1; }
.tabbar a.active { color: var(--brand); }
.fab {
  justify-self: center; width: 54px; height: 54px; border-radius: 18px; border: 0; margin-top: -18px;
  background: var(--brand); color: var(--brand-ink); font-size: 30px; line-height: 1; cursor: pointer;
  box-shadow: 0 6px 18px color-mix(in srgb, var(--brand) 45%, transparent);
}

/* ── Toast ── */
.toast {
  position: fixed; left: 50%; transform: translateX(-50%); z-index: 20;
  bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 16px);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 14px;
  max-width: calc(100% - 32px); box-shadow: var(--shadow);
}
.toast.err { background: var(--bad); color: #fff; }

/* ── Who are you ── */
.who { display: grid; gap: 12px; margin-top: 16px; }
.who .btn { min-height: 64px; font-size: 18px; }

/* ── Small phones ── */
@media (max-width: 420px) {
  main { padding-left: 12px; padding-right: 12px; }
  h1 { font-size: 24px; margin: 4px 0 14px; }
  /* Balance cards become compact rows: amount left, Settle up right. */
  .balances { grid-template-columns: 1fr; gap: 10px; }
  .bal { flex-direction: row; align-items: center; gap: 12px; padding: 14px; }
  .bal-info { flex: 1; }
  .bal .label { margin-top: 6px; }
  .bal .amt { margin-bottom: 0; }
  .bal .btn { width: auto; flex: none; }
  /* Due bills: buttons drop below the text. */
  .due { flex-wrap: wrap; }
  .due .main { flex-basis: 100%; }
  .due .actions { width: 100%; }
  .due .actions .btn { flex: 1; }
  .row { gap: 10px; padding: 11px 12px; }
  .row .ico { width: 36px; height: 36px; font-size: 18px; border-radius: 10px; }
  .amount-row .seg { width: 120px; }
  .stat { padding: 9px; }
}
@media (max-width: 340px) {
  .row .eff-amt { font-size: 14px; }
  .row .ico { display: none; }
  .tabbar a { font-size: 10px; }
}
/* Landscape phones: shorter sheets need the room. */
@media (max-height: 500px) and (orientation: landscape) {
  .sheet { max-height: 100dvh; border-radius: 16px 16px 0 0; }
}
