:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --text: #151a21;
  --muted: #5d6675;
  --border: #e2e5ea;
  --brand: #0f7a5c;
  --brand-ink: #ffffff;
  --brand-soft: #e2f3ec;
  --danger: #c2372b;
  --danger-soft: #fdecea;
  --warn: #a35f00;
  --warn-soft: #fff3df;
  --info: #2357c6;
  --info-soft: #e8effc;
  --ok: #1e7f4f;
  --ok-soft: #e3f5eb;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 1px 3px rgba(16, 24, 40, .06);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1116;
    --surface: #161a21;
    --surface-2: #1e232c;
    --text: #e8ebf0;
    --muted: #9aa3b2;
    --border: #2a303a;
    --brand: #2bb48a;
    --brand-ink: #04241a;
    --brand-soft: #12332a;
    --danger: #ff7166;
    --danger-soft: #3a1815;
    --warn: #f2a93b;
    --warn-soft: #36270f;
    --info: #7ea8ff;
    --info-soft: #172442;
    --ok: #4ccf8d;
    --ok-soft: #12301f;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color: var(--text);
  background: var(--bg);
}
a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.5rem; line-height: 1.25; margin: 0; letter-spacing: -.01em; }
h2 { font-size: 1rem; margin: 0 0 12px; }
svg { fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
:focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.ok { color: var(--ok); }
.bad { color: var(--danger); }

/* ---------- Top bar & navigation ---------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar-inner {
  max-width: 1000px;
  margin: 0 auto;
  padding: 10px 16px;
  display: flex;
  align-items: center;
  gap: 16px;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 1.05rem; color: var(--text); white-space: nowrap; }
.brand:hover { text-decoration: none; }
.brand img { border-radius: 8px; }
.nav { display: flex; gap: 4px; flex: 1; }
.nav a {
  position: relative;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-radius: 10px;
  color: var(--muted);
  font-weight: 600;
}
.nav a:hover { background: var(--surface-2); color: var(--text); text-decoration: none; }
.nav a.active { background: var(--brand-soft); color: var(--brand); }
.nav svg { width: 20px; height: 20px; }
.new-loan svg { width: 18px; height: 18px; stroke-width: 2.5; }
.badge {
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  border-radius: 10px;
  background: var(--danger);
  color: #fff;
  font-size: 12px;
  line-height: 20px;
  text-align: center;
}
.badge[hidden] { display: none; }

main {
  max-width: 1000px;
  margin: 0 auto;
  padding: 24px 16px 64px;
  display: grid;
  grid-template-columns: minmax(0, 1fr); /* lets long one-line text truncate instead of widening the page */
  gap: 20px;
}

@media (max-width: 720px) {
  .topbar-inner { padding: 10px 16px; }
  .nav {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 10;
    justify-content: space-around;
    gap: 0;
    padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
    background: var(--surface);
    border-top: 1px solid var(--border);
  }
  .nav a { flex-direction: column; gap: 2px; padding: 4px 10px; font-size: 11px; min-width: 64px; }
  .nav a.active { background: transparent; }
  .nav .badge { position: absolute; top: 0; left: 50%; margin-left: 4px; }
  .new-loan {
    position: fixed;
    right: 16px;
    bottom: calc(76px + env(safe-area-inset-bottom));
    z-index: 11;
    border-radius: 999px;
    padding: 12px 18px;
    box-shadow: 0 6px 20px rgba(0, 0, 0, .22);
  }
  main { padding-bottom: 160px; }
}

/* ---------- Accounts & sync ---------- */

body.signed-out .nav,
body.signed-out .new-loan { display: none; }
.status {
  width: min(968px, calc(100% - 32px));
  margin: 12px auto 0;
  padding: 10px 14px;
  border-radius: 10px;
  font-size: 14px;
  font-weight: 600;
}
.status[hidden] { display: none; }
.status.offline { background: var(--warn-soft); color: var(--warn); }
.status.error { background: var(--danger-soft); color: var(--danger); }
.auth { width: 100%; max-width: 420px; justify-self: center; display: grid; gap: 14px; margin-top: 16px; }
.auth img { border-radius: 12px; }
.auth p { margin: 0; }
.auth-form { display: grid; gap: 12px; }
.link-btn {
  appearance: none;
  justify-self: start;
  padding: 0;
  border: 0;
  background: none;
  color: var(--brand);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
code { font-family: ui-monospace, Consolas, monospace; font-size: .9em; padding: 1px 5px; border-radius: 4px; background: var(--surface-2); }

/* ---------- Building blocks ---------- */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  box-shadow: var(--shadow);
}
.page-head, .section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}
.section-head { margin-bottom: 10px; }
.section-head h2 { margin: 0; }
.back { font-weight: 600; font-size: 14px; justify-self: start; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; }
.danger-zone { display: flex; justify-content: center; }

.btn {
  appearance: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 40px;
  padding: 8px 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.danger { color: var(--danger); }
.btn.small { min-height: 32px; padding: 4px 10px; font-size: 13px; }
.icon-btn {
  appearance: none;
  width: 34px;
  height: 34px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--muted);
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  flex: none;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }

.banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  padding: 12px 16px;
  border-radius: var(--radius);
  background: var(--info-soft);
  border: 1px solid color-mix(in srgb, var(--info) 25%, transparent);
}
.banner.warn { background: var(--warn-soft); border-color: color-mix(in srgb, var(--warn) 30%, transparent); }

.empty {
  padding: 28px 16px;
  text-align: center;
  color: var(--muted);
  background: var(--surface);
  border: 1px dashed var(--border);
  border-radius: var(--radius);
}
.empty.small { padding: 18px 16px; }

/* ---------- Stats ---------- */

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; }
.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: var(--shadow);
  min-width: 0;
}
.stat-label { color: var(--muted); font-size: 13px; font-weight: 600; }
.stat-value { font-size: 1.35rem; font-weight: 700; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.stat-hint { color: var(--muted); font-size: 12px; }
.stat.danger .stat-value { color: var(--danger); }

/* ---------- Lists ---------- */

.list {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  color: inherit;
  border-top: 1px solid var(--border);
}
.item:first-child { border-top: 0; }
.item:hover { background: var(--surface-2); text-decoration: none; }
.item-main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.item-title, .item-sub { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item-title { font-weight: 600; }
.item-sub { color: var(--muted); font-size: 13px; }
.item-end { flex: none; display: flex; flex-direction: column; align-items: flex-end; gap: 4px; }
.item-amount { font-weight: 700; font-variant-numeric: tabular-nums; }

.avatar {
  flex: none;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-size: 14px;
  font-weight: 700;
  background: var(--surface-2);
  color: var(--muted);
}
.avatar.lg { width: 56px; height: 56px; font-size: 18px; }

.pill {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
  background: var(--surface-2);
  color: var(--muted);
}
.s-overdue { background: var(--danger-soft); color: var(--danger); }
.s-today { background: var(--warn-soft); color: var(--warn); }
.s-soon { background: var(--info-soft); color: var(--info); }
.s-active { background: var(--brand-soft); color: var(--brand); }
.s-paid { background: var(--ok-soft); color: var(--ok); }

.chips { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }
.chip {
  appearance: none;
  padding: 5px 12px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}
.chip:hover { background: var(--surface-2); }
.chip.active { background: var(--text); border-color: var(--text); color: var(--surface); }
.chip .count { opacity: .65; margin-left: 2px; }

.toolbar { display: grid; gap: 10px; }

/* ---------- Detail pages ---------- */

.hero { display: grid; gap: 18px; }
.hero.is-overdue { border-color: color-mix(in srgb, var(--danger) 45%, var(--border)); }
.hero.is-overdue .big { color: var(--danger); }
.hero.is-paid .big { color: var(--ok); }
.hero-top { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.hero h1 a { color: inherit; }
.who { display: flex; align-items: center; gap: 14px; }
.eyebrow { color: var(--muted); font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; }
.big { font-size: 2rem; font-weight: 800; letter-spacing: -.02em; font-variant-numeric: tabular-nums; line-height: 1.2; overflow-wrap: anywhere; }
.progress { height: 8px; margin: 10px 0 6px; border-radius: 99px; background: var(--surface-2); overflow: hidden; }
.progress span { display: block; height: 100%; border-radius: inherit; background: var(--ok); }

.kv { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 14px 20px; margin: 0; }
.kv dt { color: var(--muted); font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; }
.kv dd { margin: 2px 0 0; font-weight: 600; overflow-wrap: anywhere; }
.kv .wide { grid-column: 1 / -1; }
.card .kv { margin-top: 18px; }
.card h2 + .kv { margin-top: 0; }
.pre { white-space: pre-wrap; font-weight: 400 !important; }
@media (max-width: 600px) { .kv { grid-template-columns: repeat(2, minmax(0, 1fr)); } }

.payments { list-style: none; margin: 0; padding: 0; }
.payments li { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 0; border-top: 1px solid var(--border); }
.payments li:first-child { border-top: 0; padding-top: 0; }
.payments li > span { display: flex; flex-direction: column; min-width: 0; }
.payments strong { font-variant-numeric: tabular-nums; }

.status-row { display: flex; justify-content: space-between; gap: 12px; padding: 8px 0; border-bottom: 1px solid var(--border); }
.status-row strong { text-align: right; }
.card > .actions, .card > .field { margin-top: 14px; }
.help { margin-top: 14px; color: var(--muted); font-size: 14px; }
.help summary { cursor: pointer; font-weight: 600; color: var(--text); }
.help ul { padding-left: 20px; margin: 8px 0 0; display: grid; gap: 6px; }

.welcome { display: grid; gap: 14px; }
.welcome p { margin: 0; }
.steps { margin: 0; padding: 0; list-style: none; counter-reset: step; display: grid; gap: 12px; }
.steps li { counter-increment: step; display: grid; grid-template-columns: 32px 1fr; column-gap: 12px; }
.steps li::before {
  content: counter(step);
  grid-row: span 2;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-weight: 700;
  background: var(--brand-soft);
  color: var(--brand);
}
.steps span { color: var(--muted); font-size: 14px; }

/* ---------- Forms & dialogs ---------- */

.field { display: flex; flex-direction: column; gap: 6px; font-size: 13px; font-weight: 600; color: var(--muted); }
.field em { font-style: normal; font-weight: 400; }
.field.narrow { max-width: 360px; }
input, select, textarea {
  width: 100%;
  min-height: 42px;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 16px;
  font-weight: 400;
}
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 3px solid var(--brand-soft); border-color: var(--brand); }
.search { background: var(--surface); }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
@media (max-width: 420px) { .form-grid { grid-template-columns: 1fr; } }
.inline { display: flex; gap: 8px; }
.inline select { flex: 1; min-width: 0; }
.preview { margin: 0; padding: 10px 12px; border-radius: 10px; background: var(--brand-soft); font-size: 14px; }
.preview strong { font-variant-numeric: tabular-nums; }

dialog {
  width: min(560px, calc(100vw - 24px));
  max-width: calc(100vw - 24px);
  max-height: calc(100dvh - 24px);
  padding: 0;
  border: 0;
  border-radius: 16px;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 24px 64px rgba(0, 0, 0, .3);
}
dialog::backdrop { background: rgba(10, 14, 20, .5); }
dialog form { display: flex; flex-direction: column; max-height: calc(100dvh - 24px); }
.dialog-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 16px 18px 6px; }
.dialog-head h2 { margin: 0; font-size: 1.15rem; }
.dialog-body { display: grid; gap: 14px; padding: 8px 18px 18px; overflow-y: auto; }
.dialog-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 18px; border-top: 1px solid var(--border); }
@media (max-width: 520px) {
  dialog { width: 100vw; max-width: 100vw; max-height: 94dvh; margin: auto 0 0; border-radius: 18px 18px 0 0; }
  dialog form { max-height: 94dvh; }
  .dialog-foot { padding-bottom: calc(12px + env(safe-area-inset-bottom)); }
  .dialog-foot .btn { flex: 1; }
}

/* ---------- Toast ---------- */

#toast {
  position: fixed;
  inset: auto auto 24px 50%;
  transform: translateX(-50%);
  margin: 0;
  max-width: calc(100vw - 32px);
  padding: 10px 16px;
  border: 0;
  border-radius: 10px;
  background: var(--text);
  color: var(--surface);
  font-weight: 600;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .25);
  display: none;
}
#toast.show { display: block; }
#toast:popover-open { display: block; }
@media (max-width: 720px) { #toast { bottom: calc(84px + env(safe-area-inset-bottom)); } }
