/* ---------------------------------------------------------------------------
   F32 Dashboard — one stylesheet, no build step.
   Light and dark are both driven by the system setting.
   --------------------------------------------------------------------------- */

:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #e0e4ea;
  --border-strong: #cbd2dc;
  --text: #141821;
  --muted: #5d6673;
  --faint: #8b94a3;

  --accent: #2f5fe0;
  --accent-soft: #e8eeff;
  --accent-text: #ffffff;

  --good: #12855f;
  --good-soft: #e2f5ee;
  --warn: #9a6400;
  --warn-soft: #fcf1dc;
  --bad: #c0362c;
  --bad-soft: #fceae8;

  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.05), 0 4px 16px rgba(16, 24, 40, 0.05);
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1015;
    --surface: #161a21;
    --surface-2: #1d222b;
    --border: #272d38;
    --border-strong: #3a4250;
    --text: #e7eaf0;
    --muted: #9aa4b4;
    --faint: #6d7889;

    --accent: #5f8bff;
    --accent-soft: #1a2542;
    --accent-text: #0b1020;

    --good: #46d6a1;
    --good-soft: #102c24;
    --warn: #e8b055;
    --warn-soft: #2e2415;
    --bad: #f58b80;
    --bad-soft: #331a18;

    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 4px 16px rgba(0, 0, 0, 0.25);
  }
}

*, *::before, *::after { box-sizing: border-box; }

html {
  /* Stop iOS inflating text when the phone is turned sideways. */
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  font-family: var(--sans);
  font-size: 15px;
  line-height: 1.55;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3 { line-height: 1.25; margin: 0; font-weight: 650; letter-spacing: -0.01em; }
h1 { font-size: 1.55rem; }
h2 { font-size: 1.15rem; }
h3 { font-size: 0.95rem; }
p { margin: 0 0 0.8rem; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 4px;
}

/* --- app shell ------------------------------------------------------------ */

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0.6rem 1.25rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--text);
  flex: none;
}
.brand:hover { text-decoration: none; }
.brand-mark {
  display: grid;
  place-items: center;
  width: 28px; height: 28px;
  border-radius: 8px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 0.8rem;
  font-weight: 800;
}

.nav { display: flex; gap: 0.15rem; flex: 1; flex-wrap: wrap; }
.nav a {
  position: relative;
  padding: 0.35rem 0.7rem;
  border-radius: var(--radius-sm);
  color: var(--muted);
  font-weight: 550;
  font-size: 0.9rem;
}
.nav a:hover { background: var(--surface-2); color: var(--text); text-decoration: none; }
.nav a.current { background: var(--accent-soft); color: var(--accent); }

.nav-badge {
  display: inline-grid;
  place-items: center;
  min-width: 17px;
  height: 17px;
  margin-left: 0.35rem;
  padding: 0 4px;
  border-radius: 9px;
  background: var(--bad);
  color: #fff;
  font-size: 0.68rem;
  font-weight: 700;
  line-height: 1;
  vertical-align: 1px;
}
@media (prefers-color-scheme: dark) { .nav-badge { color: #2a0f0c; } }

.topbar-user { display: flex; align-items: center; gap: 0.6rem; flex: none; }
.topbar-name { font-size: 0.88rem; color: var(--muted); }

main {
  max-width: 940px;
  margin: 0 auto;
  padding: 1.75rem 1.25rem 4rem;
}
main.wide { max-width: 1120px; }

.page-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
  margin-bottom: 1.25rem;
}
.page-head p { color: var(--muted); margin: 0.25rem 0 0; max-width: 60ch; }

/* --- cards ---------------------------------------------------------------- */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.15rem 1.25rem;
  box-shadow: var(--shadow);
}
.card + .card { margin-top: 1rem; }
.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 0.9rem;
}
.card-head h2 { font-size: 1rem; }

.grid { display: grid; gap: 1rem; }
.grid-2 { grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); }
.grid-3 { grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); }

/* --- stat tiles ----------------------------------------------------------- */

.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.1rem;
  box-shadow: var(--shadow);
}
.stat-label {
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--faint);
  font-weight: 650;
}
.stat-value {
  margin-top: 0.35rem;
  font-size: 1.6rem;
  font-weight: 680;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
}
.stat-value .cur { font-size: 0.9rem; color: var(--muted); font-weight: 600; margin-left: 0.2rem; }
.stat-extra { margin-top: 0.15rem; font-size: 0.85rem; color: var(--muted); }
.stat.good .stat-value { color: var(--good); }
.stat.bad .stat-value { color: var(--bad); }

/* --- forms ---------------------------------------------------------------- */

.field { margin-bottom: 0.95rem; }
.field label,
.field > .label {
  display: block;
  font-size: 0.85rem;
  font-weight: 600;
  margin-bottom: 0.3rem;
}
.hint { font-size: 0.8rem; color: var(--muted); font-weight: 400; margin-top: 0.25rem; }

input[type="text"], input[type="email"], input[type="password"],
input[type="number"], input[type="date"], select, textarea {
  width: 100%;
  padding: 0.55rem 0.7rem;
  font: inherit;
  /* Never below 16px: iOS zooms the whole page when a smaller field is
     focused, and then leaves it zoomed. */
  font-size: 1rem;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  transition: border-color 0.12s, box-shadow 0.12s;
}
textarea { resize: vertical; min-height: 4.5rem; }
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
input[aria-invalid="true"], textarea[aria-invalid="true"] { border-color: var(--bad); }
input::placeholder, textarea::placeholder { color: var(--faint); }
select {
  appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%),
                    linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 17px) 50%, calc(100% - 12px) 50%;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
  padding-right: 2rem;
}

.field-error { color: var(--bad); font-size: 0.82rem; margin-top: 0.3rem; min-height: 0; }

.row { display: flex; gap: 0.75rem; flex-wrap: wrap; }
.row > * { flex: 1 1 0; min-width: 0; }
.row > .narrow { flex: 0 0 8.5rem; }

fieldset {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.9rem 1rem 0.35rem;
  margin: 0 0 0.95rem;
}
legend { font-size: 0.85rem; font-weight: 600; padding: 0 0.35rem; }

/* --- buttons -------------------------------------------------------------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  padding: 0.5rem 0.95rem;
  font: inherit;
  font-size: 0.9rem;
  font-weight: 600;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  background: var(--surface-2);
  color: var(--text);
  cursor: pointer;
  transition: filter 0.12s, background 0.12s;
  white-space: nowrap;
}
.btn:hover:not(:disabled) { filter: brightness(0.96); text-decoration: none; }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn-primary { background: var(--accent); color: var(--accent-text); }
.btn-good { background: var(--good-soft); color: var(--good); border-color: transparent; }
.btn-danger { background: var(--bad-soft); color: var(--bad); }
.btn-ghost { background: transparent; border-color: var(--border-strong); color: var(--muted); }
.btn-sm { padding: 0.32rem 0.65rem; font-size: 0.83rem; }
.btn-block { width: 100%; }
.btn-row { display: flex; gap: 0.5rem; flex-wrap: wrap; align-items: center; }

/* --- pills & badges ------------------------------------------------------- */

.pill {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  padding: 0.15rem 0.55rem;
  border-radius: 999px;
  font-size: 0.76rem;
  font-weight: 650;
  letter-spacing: 0.01em;
  background: var(--surface-2);
  color: var(--muted);
  white-space: nowrap;
}
.pill-pending { background: var(--warn-soft); color: var(--warn); }
.pill-accepted { background: var(--accent-soft); color: var(--accent); }
.pill-settled { background: var(--good-soft); color: var(--good); }
.pill-refused, .pill-cancelled, .pill-rejected { background: var(--surface-2); color: var(--faint); }
.pill-late { background: var(--bad-soft); color: var(--bad); }
.pill-confirmed { background: var(--good-soft); color: var(--good); }

/* --- messages ------------------------------------------------------------- */

.notice {
  padding: 0.7rem 0.9rem;
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  margin-bottom: 1rem;
  border: 1px solid transparent;
}
.notice-error { background: var(--bad-soft); color: var(--bad); }
.notice-ok { background: var(--good-soft); color: var(--good); }
.notice-info { background: var(--accent-soft); color: var(--accent); }
.notice-warn { background: var(--warn-soft); color: var(--warn); }
.notice:empty { display: none; }

.empty {
  text-align: center;
  padding: 2rem 1rem;
  color: var(--muted);
  font-size: 0.92rem;
}
.empty strong { display: block; color: var(--text); margin-bottom: 0.25rem; }

/* --- loan list ------------------------------------------------------------ */

.loan-list { display: flex; flex-direction: column; gap: 0.6rem; }

.loan-item {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0.35rem 1rem;
  align-items: center;
  padding: 0.85rem 1rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 3px solid var(--border-strong);
  border-radius: var(--radius-sm);
  color: inherit;
}
.loan-item:hover { text-decoration: none; border-color: var(--border-strong); background: var(--surface-2); }
.loan-item.is-lender { border-left-color: var(--good); }
.loan-item.is-borrower { border-left-color: var(--warn); }
.loan-item.is-late { border-left-color: var(--bad); }
.loan-item.is-closed { opacity: 0.68; border-left-color: var(--border); }

.loan-main { min-width: 0; }
.loan-title { font-weight: 620; }
.loan-title .direction { color: var(--muted); font-weight: 450; }
.loan-meta {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  align-items: center;
  font-size: 0.82rem;
  color: var(--muted);
  margin-top: 0.15rem;
}
.loan-amount {
  text-align: right;
  font-variant-numeric: tabular-nums;
  font-weight: 660;
  font-size: 1.05rem;
  white-space: nowrap;
}
.loan-amount small { display: block; font-size: 0.76rem; font-weight: 500; color: var(--muted); }

/* --- loan detail ---------------------------------------------------------- */

.breakdown { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.breakdown td { padding: 0.4rem 0; border-bottom: 1px dashed var(--border); }
.breakdown td:last-child { text-align: right; font-weight: 600; }
.breakdown tr:last-child td { border-bottom: none; padding-top: 0.6rem; font-size: 1.1rem; }
.breakdown .label { color: var(--muted); font-weight: 450; }
.breakdown .sub { font-size: 0.8rem; color: var(--faint); display: block; font-weight: 400; }

.meter {
  height: 8px;
  border-radius: 999px;
  background: var(--surface-2);
  overflow: hidden;
  margin: 0.6rem 0 0.35rem;
}
.meter-fill { height: 100%; background: var(--good); border-radius: 999px; transition: width 0.3s; }
.meter-legend { display: flex; justify-content: space-between; font-size: 0.8rem; color: var(--muted); }

.timeline { list-style: none; margin: 0; padding: 0; }
.timeline li {
  position: relative;
  padding: 0 0 0.9rem 1.35rem;
  border-left: 2px solid var(--border);
  margin-left: 4px;
}
.timeline li:last-child { border-left-color: transparent; padding-bottom: 0; }
.timeline li::before {
  content: "";
  position: absolute;
  left: -5px; top: 6px;
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--border-strong);
}
.timeline li.mark-good::before { background: var(--good); }
.timeline li.mark-bad::before { background: var(--bad); }
.timeline li.mark-accent::before { background: var(--accent); }
.timeline .when { font-size: 0.78rem; color: var(--faint); }
.timeline .what { font-size: 0.9rem; }

.payment {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.75rem 0;
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.payment:last-child { border-bottom: none; }
.payment-amount { font-weight: 650; font-variant-numeric: tabular-nums; }
.payment-note { font-size: 0.85rem; color: var(--muted); margin-top: 0.15rem; }

.quote {
  border-left: 3px solid var(--border-strong);
  padding: 0.1rem 0 0.1rem 0.8rem;
  color: var(--muted);
  font-size: 0.92rem;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

/* --- friends -------------------------------------------------------------- */

.person {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.7rem 0;
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.person:last-child { border-bottom: none; }
.avatar {
  display: grid;
  place-items: center;
  width: 36px; height: 36px;
  flex: none;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 700;
  font-size: 0.85rem;
}
.person-info { flex: 1; min-width: 9rem; }
.person-name { font-weight: 600; }
.person-email { font-size: 0.82rem; color: var(--muted); overflow-wrap: anywhere; }

/* --- tables --------------------------------------------------------------- */

.table-wrap { overflow-x: auto; }
table.data { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
table.data th, table.data td {
  text-align: left;
  padding: 0.55rem 0.7rem;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
table.data th { font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--faint); }
table.data tr:last-child td { border-bottom: none; }
.mono { font-family: var(--mono); font-size: 0.86em; }

/* --- tabs / filters ------------------------------------------------------- */

.filters { display: flex; gap: 0.35rem; flex-wrap: wrap; margin-bottom: 1rem; }
.filters button {
  padding: 0.35rem 0.75rem;
  font: inherit;
  font-size: 0.86rem;
  font-weight: 550;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--muted);
  cursor: pointer;
}
.filters button[aria-pressed="true"] {
  background: var(--accent-soft);
  border-color: transparent;
  color: var(--accent);
  font-weight: 650;
}

/* --- centred auth pages --------------------------------------------------- */

.auth-page {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 2rem 1.25rem;
}
.auth-card { width: 100%; max-width: 25rem; }
.auth-card .brand { justify-content: center; margin-bottom: 1.25rem; font-size: 1.1rem; }
.auth-foot { text-align: center; font-size: 0.87rem; color: var(--muted); margin-top: 1rem; }

/* --- modal ---------------------------------------------------------------- */

dialog {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  padding: 1.25rem;
  max-width: 28rem;
  width: calc(100% - 2rem);
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.25);
}
dialog::backdrop { background: rgba(8, 10, 14, 0.55); }
dialog h2 { margin-bottom: 0.75rem; }

/* --- misc ----------------------------------------------------------------- */

.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.nowrap { white-space: nowrap; }
.tabular { font-variant-numeric: tabular-nums; }
.stack > * + * { margin-top: 0.75rem; }
.spinner { color: var(--faint); font-size: 0.9rem; padding: 1.5rem; text-align: center; }
.sr-only {
  position: absolute; width: 1px; height: 1px;
  padding: 0; margin: -1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}
.hidden { display: none !important; }

.secret {
  font-family: var(--mono);
  background: var(--surface-2);
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius-sm);
  padding: 0.5rem 0.7rem;
  user-select: all;
  word-break: break-all;
  white-space: pre-wrap;
  line-height: 1.7;
}

/* ---------------------------------------------------------------------------
   Touch and small screens
   --------------------------------------------------------------------------- */

/* Anything driven by a finger rather than a cursor, regardless of width.
   44px is the smallest target most people can hit reliably. */
@media (pointer: coarse) {
  .btn { min-height: 44px; }
  .btn-sm { min-height: 38px; padding: 0.4rem 0.75rem; }
  .filters button { min-height: 38px; }
  .nav a { min-height: 40px; display: inline-flex; align-items: center; }
  input[type="checkbox"], input[type="radio"] { width: 20px; height: 20px; }
  .check { padding: 0.3rem 0; }
  /* Removes the historical 300ms wait before a tap registers. */
  .btn, .filters button, .nav a, .loan-item { touch-action: manipulation; }
}

@media (max-width: 860px) {
  /* Seven items no longer fit on one line. Rather than wrapping the bar onto
     two rows and eating a third of a short screen, the nav becomes a strip
     that scrolls sideways. */
  .topbar {
    flex-wrap: wrap;
    padding: 0.5rem 0.75rem;
    padding-left: max(0.75rem, env(safe-area-inset-left));
    padding-right: max(0.75rem, env(safe-area-inset-right));
    gap: 0.5rem;
  }
  .nav {
    order: 3;
    width: 100%;
    flex-wrap: nowrap;
    overflow-x: auto;
    scrollbar-width: none;
    /* Bleed to the edges so the strip visibly runs off-screen, which is what
       tells people it scrolls. */
    margin: 0 -0.75rem;
    padding: 0 0.75rem 0.15rem;
    scroll-padding-inline: 0.75rem;
  }
  .nav::-webkit-scrollbar { display: none; }
  .nav a { flex: none; white-space: nowrap; }
  .topbar-user { margin-left: auto; }
}

@media (max-width: 640px) {
  body { font-size: 15px; }

  main {
    padding: 1.1rem 0.9rem 3rem;
    padding-left: max(0.9rem, env(safe-area-inset-left));
    padding-right: max(0.9rem, env(safe-area-inset-right));
    padding-bottom: calc(3rem + env(safe-area-inset-bottom));
  }

  h1 { font-size: 1.35rem; }
  .card { padding: 1rem; border-radius: 10px; }

  /* The action in a page header is the main thing on a phone, so give it the
     full width instead of letting it hang off the side of the title. */
  .page-head { gap: 0.75rem; margin-bottom: 1rem; }
  .page-head > .btn,
  .page-head > a.btn { width: 100%; }

  .topbar-name { display: none; }

  /* Side-by-side fields are unreadable at this width. */
  .row { flex-direction: column; gap: 0; }
  .row > *, .row > .narrow { flex: 1 1 auto; width: 100%; }

  .grid-2, .grid-3 { grid-template-columns: 1fr; }

  .loan-item { grid-template-columns: 1fr; padding: 0.8rem 0.85rem; }
  .loan-amount { text-align: left; font-size: 1rem; }
  .loan-amount small { display: inline; margin-right: 0.5rem; }

  /* A row of content with buttons: stack them, and let the buttons spread. */
  .payment, .person {
    flex-direction: column;
    align-items: stretch;
    gap: 0.6rem;
  }
  .payment .btn-row, .person .btn-row { justify-content: flex-start; }
  .payment .btn-row .btn, .person .btn-row .btn { flex: 1 1 auto; }

  .filters {
    flex-wrap: nowrap;
    overflow-x: auto;
    scrollbar-width: none;
    margin: 0 -0.9rem 0.9rem;
    padding: 0 0.9rem 0.2rem;
  }
  .filters::-webkit-scrollbar { display: none; }
  .filters button { flex: none; white-space: nowrap; }

  .btn-row { gap: 0.5rem; }
  .btn-block, .page-head .btn { width: 100%; }

  dialog {
    width: calc(100% - 1.5rem);
    max-height: calc(100dvh - 3rem);
    overflow-y: auto;
    padding: 1rem;
  }

  .breakdown td { padding: 0.45rem 0; }
  .breakdown tr:last-child td { font-size: 1rem; }

  .auth-page { padding: 1.5rem 1rem; align-items: start; padding-top: 3rem; }
}

/* Very narrow phones, and anyone using large text. */
@media (max-width: 380px) {
  .brand span:last-child { display: none; }
  .stat-value { font-size: 1.35rem; }
  main { padding-left: 0.7rem; padding-right: 0.7rem; }
}

/* Respect a reduced-motion preference. */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}

/* --- small utilities added late ------------------------------------------ */

.mt { margin-top: 0.85rem; }

.check {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  font-size: 0.88rem;
  cursor: pointer;
}
input[type="checkbox"], input[type="radio"] {
  width: auto;
  accent-color: var(--accent);
  margin: 0;
}
