/* Palette: bone paper, petrol ink. Brick marks money we owe, teal marks money owed to us. */
:root {
  --bone: #f3f4f0;
  --surface: #fdfdfb;
  --sunk: #eceee9;
  --ink: #132029;
  --ink-2: #44505c;
  --ink-3: #78828d;
  --rule: #dfe3de;
  --rule-strong: #c4cbc5;
  --petrol: #0f3b44;
  --petrol-2: #0a2b32;
  --owe: #a2402c;
  --due: #17705d;
  --focus: #2c6a8f;
  --danger: #8f2d1f;

  --serif: "Newsreader", "Iowan Old Style", Georgia, serif;
  --sans: "Instrument Sans", "Segoe UI", system-ui, sans-serif;

  --topbar-h: 76px;
  --radius: 8px;
  --radius-lg: 14px;
  --gutter: clamp(16px, 4vw, 40px);
  --shadow: 0 1px 0 rgba(19, 32, 41, 0.04), 0 12px 32px -18px rgba(19, 32, 41, 0.22);
}

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

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  background: var(--bone);
  color: var(--ink);
  font: 400 15.5px/1.55 var(--sans);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

a {
  color: inherit;
}

button,
input,
textarea,
select {
  font: inherit;
  color: inherit;
}

.boot {
  padding: 2rem var(--gutter);
  color: var(--ink-3);
}

/* ---------- layout ---------- */

.shell {
  max-width: 1120px;
  margin: 0 auto;
  padding: 0 var(--gutter) 5rem;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: color-mix(in srgb, var(--bone) 88%, transparent);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--rule);
}

.topbar-inner {
  max-width: 1120px;
  margin: 0 auto;
  padding: 0.9rem var(--gutter);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}

.wordmark {
  display: inline-flex;
  align-items: center;
  gap: 0.75rem;
  text-decoration: none;
}

.brand-logo {
  display: block;
  height: 34px;
  width: auto;
}

.wordmark-text {
  font: 500 1.125rem/1 var(--serif);
  letter-spacing: -0.01em;
  padding-left: 0.75rem;
  border-left: 1px solid var(--rule-strong);
}

.topbar nav {
  display: flex;
  align-items: center;
  gap: 0.25rem;
}

/* ---------- type ---------- */

h1,
h2 {
  font-family: var(--serif);
  font-weight: 500;
  letter-spacing: -0.015em;
  line-height: 1.1;
  margin: 0;
}

h1 {
  font-size: clamp(2.1rem, 1.5rem + 2.2vw, 3rem);
  font-optical-sizing: auto;
}

h2 {
  font-size: 1.625rem;
}

.muted {
  color: var(--ink-3);
}

.small {
  font-size: 0.875rem;
}

.figure {
  margin: 0.35rem 0 0;
  font-family: var(--serif);
  font-size: clamp(1.9rem, 1.5rem + 1.6vw, 2.75rem);
  font-weight: 500;
  line-height: 1;
  font-variant-numeric: lining-nums tabular-nums;
  letter-spacing: -0.015em;
  white-space: nowrap;
}

.figure-label {
  margin: 0;
  color: var(--ink-3);
  font-size: 0.875rem;
  font-weight: 500;
}

.figure-sub {
  margin: 0.6rem 0 0;
  color: var(--ink-3);
  font-size: 0.875rem;
}

.owe {
  color: var(--owe);
}

.due {
  color: var(--due);
}

.num {
  font-variant-numeric: lining-nums tabular-nums;
  white-space: nowrap;
  text-align: right;
}

.side {
  display: block;
  margin-top: 0.15rem;
  font-size: 0.8125rem;
  color: var(--ink-3);
  font-weight: 400;
}

/* ---------- totals ---------- */

.totals {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  margin: 1.1rem 0 1.4rem;
  background: var(--surface);
  border: 1px solid var(--rule);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.totals > div {
  padding: 0.7rem 1.1rem 0.75rem;
}

.totals > div + div {
  border-left: 1px solid var(--rule);
}

.totals > div:last-child {
  background: color-mix(in srgb, var(--petrol) 4%, var(--surface));
}

/* One orchestrated moment: the totals settle in on load. */
@keyframes settle {
  from {
    opacity: 0;
    transform: translateY(6px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

.totals .figure {
  font-size: clamp(1.15rem, 1rem + 0.6vw, 1.5rem);
  animation: settle 520ms cubic-bezier(0.2, 0.7, 0.2, 1) both;
}

.totals > div:nth-child(2) .figure {
  animation-delay: 80ms;
}

.totals > div:nth-child(3) .figure {
  animation-delay: 160ms;
}

/* ---------- section header ---------- */

.section-head {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1rem;
}

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

.ledger-card {
  scroll-margin-top: calc(var(--topbar-h) + 12px);
  background: var(--surface);
  border: 1px solid var(--rule);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  /* clip keeps the rounded corners without breaking the sticky header */
  overflow: clip;
}

.ledger {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
}

.ledger th,
.ledger td {
  padding: 0.9rem 1.1rem;
  text-align: left;
  vertical-align: top;
  border-bottom: 1px solid var(--rule);
}

.ledger th {
  position: sticky;
  top: var(--thead-top, var(--topbar-h));
  z-index: 2;
  background: var(--sunk);
  font-size: 0.8125rem;
  font-weight: 600;
  color: var(--ink-2);
  padding-top: 0.7rem;
  padding-bottom: 0.7rem;
  border-bottom: 1px solid var(--rule-strong);
}

.ledger.compact th,
.ledger.compact td {
  padding: 0.45rem 1rem;
}

.ledger.compact .party-link {
  font-size: 1rem;
}

.ledger.compact .count {
  margin-left: 0.6rem;
}

.ledger.compact .side {
  display: inline;
  margin-left: 0.45rem;
}

.ledger.compact td.actions .link {
  padding: 0.2rem 0.4rem;
}

.pager {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem 1rem;
  margin-top: 1rem;
}

.pager-controls {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}

.ledger th.num {
  text-align: right;
}

.ledger tbody tr {
  transition: background-color 120ms ease;
}

.ledger tbody tr:hover {
  background: color-mix(in srgb, var(--petrol) 4%, var(--surface));
}

.ledger tbody tr:last-child td {
  border-bottom: 0;
}

.ledger .detail {
  white-space: pre-line;
  overflow-wrap: anywhere;
  max-width: 46ch;
}

.ledger .date {
  white-space: nowrap;
  color: var(--ink-2);
}

.ledger .amount {
  font-variant-numeric: lining-nums tabular-nums;
}

.ledger .balance {
  font-weight: 600;
}

.ledger tfoot td {
  background: var(--sunk);
  border-top: 1px solid var(--rule-strong);
  border-bottom: 0;
  font-weight: 600;
}

.ledger .actions {
  text-align: right;
  white-space: nowrap;
  width: 1%;
}

/* ---------- party list ---------- */

.party-link {
  font-family: var(--serif);
  font-size: 1.3rem;
  font-weight: 500;
  text-decoration: none;
  border-bottom: 1px solid transparent;
  transition: border-color 120ms ease;
}

.party-link:hover {
  border-bottom-color: var(--ink);
}

/* ---------- party page ---------- */

.back {
  display: inline-block;
  margin-top: 1.5rem;
  font-size: 0.9375rem;
  color: var(--ink-2);
  text-decoration: none;
}

.back:hover {
  color: var(--ink);
  text-decoration: underline;
  text-underline-offset: 3px;
}

.party-head {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  justify-content: space-between;
  gap: 0.75rem 2rem;
  margin: 0.5rem 0 1.1rem;
}

.party-head .name-row {
  display: flex;
  align-items: center;
  gap: 1rem;
  flex-wrap: wrap;
}

.ledger-foot {
  margin-top: 1.5rem;
}

.add-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem 1rem;
  margin-top: 1rem;
}

.party-view {
  --bar-h: 62px;
  --thead-top: var(--bar-h);
}

.party-view .topbar {
  position: static;
}

.party-view .ledger-card {
  scroll-margin-top: calc(var(--bar-h) + 56px);
}

.party-bar {
  position: sticky;
  top: 0;
  z-index: 9;
  background: color-mix(in srgb, var(--bone) 92%, transparent);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--rule);
}

.party-bar-inner {
  max-width: 1120px;
  margin: 0 auto;
  padding: 0.5rem var(--gutter);
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem 1.25rem;
}

.party-id {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 0.25rem 0.9rem;
  min-width: 0;
}

.party-bar .back {
  margin: 0;
  font-size: 0.875rem;
}

.party-title {
  font-family: var(--serif);
  font-size: 1.3rem;
  font-weight: 500;
  letter-spacing: -0.01em;
}

.party-balance {
  font-family: var(--serif);
  font-size: 1.05rem;
  font-weight: 600;
  font-variant-numeric: lining-nums tabular-nums;
  white-space: nowrap;
}

.party-balance .side {
  display: inline;
  margin-left: 0.4rem;
  font-family: var(--sans);
  font-size: 0.8125rem;
  font-weight: 400;
}

.head-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
}

.balance-line {
  display: grid;
  grid-template-columns: minmax(0, 1.4fr) minmax(0, 1fr) minmax(0, 1fr);
  background: var(--surface);
  border: 1px solid var(--rule);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.balance-line .stat {
  padding: 0.8rem 1.25rem;
}

.balance-line .stat + .stat {
  border-left: 1px solid var(--rule);
}

.balance-line .stat:first-child .figure {
  font-size: 1.6rem;
}

.balance-line .stat:not(:first-child) .figure {
  font-size: 1.15rem;
}

.balance-line .figure-label {
  font-size: 0.8125rem;
}

.balance-line .figure-sub {
  margin-top: 0.2rem;
}

.empty {
  padding: 3rem 1.5rem;
  text-align: center;
  color: var(--ink-2);
  background: var(--surface);
  border: 1px dashed var(--rule-strong);
  border-radius: var(--radius-lg);
}

.empty p {
  margin: 0 0 1.1rem;
}

.footnote {
  margin-top: 2.25rem;
}

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

.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  min-height: 2.6rem;
  padding: 0.55rem 1.1rem;
  border-radius: var(--radius);
  border: 1px solid var(--rule-strong);
  background: var(--surface);
  color: var(--ink);
  font-weight: 500;
  cursor: pointer;
  transition: background-color 140ms ease, border-color 140ms ease, color 140ms ease, box-shadow 140ms ease;
}

.button:hover {
  border-color: var(--ink-2);
}

.button.primary {
  background: var(--petrol);
  border-color: var(--petrol);
  color: #f7f8f5;
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.08) inset, 0 6px 16px -10px rgba(10, 43, 50, 0.7);
}

.button.primary:hover {
  background: var(--petrol-2);
  border-color: var(--petrol-2);
}

.button.danger {
  background: var(--danger);
  border-color: var(--danger);
  color: #fff;
}

.button.ghost {
  background: transparent;
  border-color: transparent;
  color: var(--ink-2);
}

.button.ghost:hover {
  color: var(--ink);
  background: var(--sunk);
}

.button.small {
  min-height: 2rem;
  padding: 0.25rem 0.65rem;
  font-size: 0.875rem;
}

.button:disabled {
  opacity: 0.55;
  cursor: progress;
}

.link {
  background: none;
  border: 0;
  padding: 0.3rem 0.5rem;
  border-radius: 6px;
  cursor: pointer;
  color: var(--ink-2);
  font-size: 0.9rem;
  font-weight: 500;
}

.link:hover {
  color: var(--ink);
  background: var(--sunk);
}

.link.danger {
  color: var(--danger);
}

.link.danger:hover {
  background: color-mix(in srgb, var(--danger) 9%, transparent);
}

/* ---------- login ---------- */

.login {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: var(--gutter);
}

.login-card {
  width: min(400px, 100%);
  padding: 2.5rem 2.25rem 2.25rem;
  background: var(--surface);
  border: 1px solid var(--rule);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
}

.login-card .brand-logo {
  height: 46px;
  margin-bottom: 1.75rem;
}

.login-card h1 {
  font-size: 2rem;
  margin-bottom: 0.4rem;
}

.login-card .muted {
  margin: 0 0 1.75rem;
}

.field {
  display: grid;
  gap: 0.4rem;
  margin-bottom: 1rem;
}

.field > span {
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--ink-2);
}

.field input,
.field textarea,
.field select {
  width: 100%;
  padding: 0.65rem 0.8rem;
  border: 1px solid var(--rule-strong);
  border-radius: var(--radius);
  background: var(--surface);
  font-size: 1rem;
}

.field input:focus,
.field textarea:focus {
  border-color: var(--focus);
  outline: 2px solid color-mix(in srgb, var(--focus) 22%, transparent);
  outline-offset: 0;
}

.field input[aria-invalid="true"] {
  border-color: var(--danger);
}

.field .hint {
  font-size: 0.8125rem;
  color: var(--ink-3);
}

.login-card .stack {
  display: grid;
  gap: 0.75rem;
}

.divider {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin: 1.5rem 0;
  color: var(--ink-3);
  font-size: 0.8125rem;
}

.divider::before,
.divider::after {
  content: "";
  flex: 1;
  height: 1px;
  background: var(--rule);
}

.login-card .button {
  width: 100%;
}

.form-error {
  margin: 0 0 1rem;
  color: var(--danger);
  font-size: 0.9375rem;
}

.form-error[hidden] {
  display: none;
}

/* ---------- dialog ---------- */

dialog {
  width: min(480px, calc(100vw - 2 * 16px));
  padding: 0;
  border: 1px solid var(--rule-strong);
  border-radius: var(--radius-lg);
  background: var(--surface);
  color: var(--ink);
  box-shadow: 0 30px 70px -24px rgba(19, 32, 41, 0.45);
}

dialog::backdrop {
  background: rgba(19, 32, 41, 0.38);
  backdrop-filter: blur(2px);
}

.dialog-form {
  padding: 1.75rem 1.75rem 1.5rem;
}

.dialog-form h2 {
  margin-bottom: 0.4rem;
}

.dialog-form .lede {
  margin: 0 0 1.25rem;
  color: var(--ink-2);
  font-size: 0.9375rem;
}

.dialog-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem;
}

.dialog-row[hidden] {
  display: none;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  margin-top: 1.4rem;
}

.passkey-list {
  list-style: none;
  margin: 0 0 1.25rem;
  padding: 0;
  border-top: 1px solid var(--rule);
}

.passkey-list li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  padding: 0.8rem 0;
  border-bottom: 1px solid var(--rule);
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

/* ---------- small screens ---------- */

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

  .totals {
    grid-template-columns: 1fr;
    margin: 1.5rem 0 2rem;
  }

  .totals > div + div {
    border-left: 0;
    border-top: 1px solid var(--rule);
  }

  .totals > div {
    padding: 1.2rem 1.25rem;
  }

  .balance-line {
    grid-template-columns: 1fr 1fr;
  }

  .balance-line .stat:first-child {
    grid-column: 1 / -1;
    border-bottom: 1px solid var(--rule);
  }

  .balance-line .stat:not(:first-child) {
    padding: 1.1rem 1.25rem;
  }

  .balance-line .stat:nth-child(2) {
    border-left: 0;
  }

  .ledger-card {
    border-radius: var(--radius);
  }

  .ledger thead {
    display: none;
  }

  .ledger,
  .ledger tbody,
  .ledger tr,
  .ledger td {
    display: block;
    width: 100%;
  }

  .ledger td {
    border: 0;
    padding: 0.15rem 0;
    text-align: left;
  }

  .ledger tr {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 0.15rem 1rem;
    padding: 0.95rem 1.1rem;
    border-bottom: 1px solid var(--rule);
  }

  .ledger tbody tr:last-child {
    border-bottom: 0;
  }

  .ledger td.date {
    grid-column: 1 / -1;
    font-size: 0.8125rem;
    color: var(--ink-3);
  }

  .ledger td.detail {
    grid-column: 1;
  }

  .ledger td.amount,
  .ledger td.num {
    text-align: right;
  }

  .ledger td.balance {
    grid-column: 2;
    grid-row: 2 / span 3;
    align-self: center;
    text-align: right;
  }

  .ledger td.actions {
    grid-column: 1 / -1;
    text-align: left;
    width: auto;
    padding-top: 0.3rem;
  }

  .ledger tfoot {
    display: none;
  }

  .party-head .button {
    width: 100%;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    transition: none !important;
    animation: none !important;
  }
}
