html {
  scroll-padding-top: env(safe-area-inset-top, 0);
}

img {
  max-width: 100%;
}

[hidden]:not([hidden="until-found" i]) {
  display: none !important;
}

/* Layout: dark app bar, single 1200px work column; lists and tables in white sheets on a warm-grey ground. */
:root {
  box-sizing: border-box;
  padding-top: env(safe-area-inset-top, 0);
  padding-bottom: env(safe-area-inset-bottom, 0);

  /* Spacing scale: use only these for gap, margin and padding. */
  --sp-2: 2px;
  --sp-4: 4px;
  --sp-8: 8px;
  --sp-16: 16px;
  --sp-32: 32px;
  --sp-64: 64px;
  --sp-128: 128px;

  color-scheme: light;

  --bg: #f6f5f2;
  --surface: #fff;
  --surface-2: #f7f6f4;
  --line: #e6e3df;
  --line-strong: #d2cec8;
  --fg: #1d1d1b;
  --muted: #66635d;
  --bar: #fcefec;
  --bar-fg: #2a2724;
  --accent: #1d7f3a;
  --accent-ink: #1f5bd6;
  --on-accent: #fff;
  --info-bg: #e5eefc;
  --info-fg: #1b4fb8;
  --ok-bg: #1d7f3a;
  --ok-fg: #fff;
  --good: #1d7f3a;
  --warn-bg: #fff1d6;
  --warn-fg: #7a5300;
  --bad-bg: #fde6e3;
  --bad-fg: #b42318;
  --font: "Inter", system-ui, -apple-system, "Segoe UI", sans-serif;

  /* Type scale */
  --text-3xs: 10px;
  --text-2xs: 11px;
  --text-xs: 12px;
  --text-sm: 13px;
  --text-md: 14px;
  --text-base: 15px;
  --text-lg: 16px;
  --text-xl: 18px;
  --text-2xl: 22px;
  --text-3xl: 26px;
  --text-4xl: 28px;
  --weight-regular: 400;
  --weight-medium: 500;
  --weight-semibold: 600;
  --weight-bold: 700;
  --leading-none: 1;
  --leading-tight: 1.2;
  --leading-normal: 1.45;
  --leading-loose: 1.6;
  --tracking-tight: -0.015em;
  --tracking-wide: 0.05em;

  /* Shape and depth */
  --radius-xs: 2px;
  --radius-sm: 4px;
  --radius-md: 6px;
  --radius-lg: 8px;
  --radius-xl: 10px;
  --radius-pill: 999px;
  --radius-round: 50%;
  --shadow-sm: 0 1px 2px rgb(0 0 0 / 4%);
  --shadow-md: 0 6px 20px rgb(0 0 0 / 10%);
  --shadow-lg: 0 10px 30px rgb(0 0 0 / 14%);

  /* Utility bar (always dark) */
  --util-bg: #1f1e1b;
  --util-fg: #e9e6df;
  --util-muted: #a9a59c;
  --util-line: #47443d;
  --util-line-hover: #6b675e;
  --util-hover: rgb(255 255 255 / 8%);
  --util-active: rgb(255 255 255 / 14%);
  --on-dark: #fff;
  --on-dark-muted: #cfcbc2;

  /* Invoice paper (always light; also the PDF) */
  --paper: #fff;
  --paper-fg: #1f1e1b;
  --paper-muted: #5e5b53;
  --paper-line: #efede7;
  --paper-line-strong: #e5e2da;
  --paper-link: #b8410a;

  /* Status, selection, banners, nav */
  --brand: #f2541b;
  --nav-active: #fa5d00;
  --status-sent: #1f5bd6;
  --status-overdue: #c4320a;
  --selection-bg: #fdd3c1;
  --ok-soft-bg: #f1f7f2;
  --ok-soft-fg: #24603a;
  --ok-line: #d4e7d9;
  --info-soft-bg: #f2f6fd;
  --info-line: #d6e2f7;
  --bad-line: #f5cfca;
}

body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font);
  font-size: var(--text-base);
  line-height: var(--leading-normal);
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

body > main {
  flex: 1 0 auto;
  width: 100%;
}

body > .appfoot {
  flex-shrink: 0;
}

body::before {
  content: "";
  position: fixed;
  inset: 0 0 auto;
  height: env(safe-area-inset-top, 0);
  z-index: 10;
  background: var(--bar);
}

a {
  color: var(--accent-ink);
}

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

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

.gutter {
  padding-left: max(var(--sp-16), env(safe-area-inset-left, 0px));
  padding-right: max(var(--sp-16), env(safe-area-inset-right, 0px));
}

.wrap {
  max-width: 1200px;
  margin: 0 auto;
}

header.bar {
  border-bottom: 1px solid var(--line);
  background: var(--bar);
  color: var(--bar-fg);
  position: sticky;
  top: env(safe-area-inset-top, 0);
  z-index: 5;
}

header.bar .wrap {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--sp-4) var(--sp-8);
  padding-block: var(--sp-8);
}

.brand {
  display: flex;
  align-items: center;
  gap: var(--sp-8);
  font-weight: var(--weight-bold);
  font-size: var(--text-lg);
  color: var(--bar-fg);
  margin-right: var(--sp-16);
}

.brand-mark {
  width: 26px;
  height: 26px;
  border-radius: var(--radius-md);
  background: var(--brand);
  color: var(--on-accent);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: var(--text-md);
}

.util {
  background: var(--util-bg);
  color: var(--util-fg);
}

.util .wrap {
  display: flex;
  align-items: center;
  gap: var(--sp-8) var(--sp-16);
  min-height: 40px;
  padding-block: var(--sp-4);
}

header .util .brand {
  display: flex;
  align-items: center;
  gap: var(--sp-8);
  margin: 0;
  padding: 0;
  border: 0;
  background: none;
  color: var(--on-dark);
  font: var(--weight-semibold) var(--text-sm)/var(--leading-none) var(--font);
  cursor: default;
}

.util .sync {
  margin-left: 0;
  font-size: var(--text-xs);
  color: var(--util-muted);
}

.util .sync button,
.utilbtn {
  display: inline-flex;
  align-items: center;
  gap: var(--sp-4);
  background: transparent;
  border: 1px solid var(--util-line);
  color: var(--util-fg);
  border-radius: var(--radius-md);
  padding: 0 var(--sp-8);
  min-height: 28px;
  cursor: pointer;
  font: var(--weight-medium) var(--text-xs) var(--font);
}

.util .sync button:hover,
.utilbtn:hover {
  border-color: var(--util-line-hover);
  color: var(--on-dark);
}

.utilbtn[aria-current="page"] {
  background: var(--on-dark);
  color: var(--util-bg);
  border-color: var(--on-dark);
}

.navrow {
  display: flex;
  align-items: center;
  padding-block: var(--sp-8);
}

.navbtn {
  background: none;
  border: 0;
  color: var(--bar-fg);
  font-weight: var(--weight-medium);
  padding: 0 var(--sp-16);
  min-height: 40px;
  border-radius: var(--radius-md);
  cursor: pointer;
}

.navbtn:hover {
  background: color-mix(in srgb, var(--brand) 10%, transparent);
  color: var(--bar-fg);
}

.navbtn[aria-current="page"] {
  background: var(--nav-active);
  color: var(--on-accent);
  font-weight: var(--weight-semibold);
}

.navbtn[aria-current="page"]:hover {
  background: var(--nav-active);
  color: var(--on-accent);
  filter: brightness(0.95);
}

.sync {
  margin-left: auto;
  font-size: var(--text-sm);
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: var(--sp-8);
}

.sync button {
  background: var(--surface);
  border: 1px solid var(--line-strong);
  color: var(--bar-fg);
  border-radius: var(--radius-md);
  padding: 0 var(--sp-8);
  min-height: 32px;
  cursor: pointer;
  font-size: var(--text-sm);
}

main {
  padding-block: var(--sp-32);
  display: flex;
  flex-direction: column;
  gap: var(--sp-16);
}

h1 {
  margin: 0;
  font-size: var(--text-3xl);
  font-weight: var(--weight-semibold);
  letter-spacing: var(--tracking-tight);
  text-wrap: balance;
}

/* page titles always take a button's height, so the title sits at the same spot on every page whether or not it has buttons beside it */
#app h1 {
  min-height: 44px;
  display: flex;
  align-items: center;
}

h2 {
  margin: 0;
  font-size: var(--text-xl);
  font-weight: var(--weight-bold);
}

.row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--sp-8);
}

.spacer {
  flex: 1;
}

/* Quiet confirmation after an inline save: green check and label, no pill. */
.savedmark {
  display: inline-flex;
  align-items: center;
  gap: var(--sp-4);
  color: var(--good);
  font-size: var(--text-sm);
  font-weight: var(--weight-semibold);
}

/* Quick fade in, hold, slower fade out at 3s. The element is new on every save (the dashboard redraws), so it restarts each time. */
.savedmark:not(:empty) {
  animation:
    saved-in 0.2s ease,
    saved-out 0.4s ease 3s forwards;
}

@keyframes saved-in {
  from {
    opacity: 0;
  }
}

@keyframes saved-out {
  to {
    opacity: 0;
    translate: 6px 0;
  }
}

/* Reduced motion: fade only. An !important declaration overrides the animated value. */
@media (prefers-reduced-motion: reduce) {
  .savedmark {
    translate: none !important;
  }
}

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

.small {
  font-size: var(--text-sm);
}

.num {
  font-variant-numeric: tabular-nums;
}

.right {
  text-align: right;
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: var(--sp-8);
  border-radius: var(--radius-md);
  padding: 0 var(--sp-16);
  min-height: 44px;
  font-weight: var(--weight-semibold);
  font-size: var(--text-md);
  cursor: pointer;
  border: 1px solid var(--line-strong);
  background: var(--surface);
  color: var(--fg);
  text-decoration: none;
}

.btn:hover {
  border-color: var(--muted);
}

.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--on-accent);
}

.btn.primary:hover {
  filter: brightness(0.94);
}

.btn.ghost {
  border-color: transparent;
  background: none;
}

.btn.dashed {
  border-style: dashed;
}

.btn[disabled] {
  opacity: 0.55;
  cursor: progress;
}

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
  gap: var(--sp-16);
}

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  padding: var(--sp-16) var(--sp-16);
  display: flex;
  flex-direction: column;
  gap: var(--sp-4);
}

.card .label {
  font-size: var(--text-sm);
  color: var(--muted);
  font-weight: var(--weight-medium);
}

.card .big {
  font-size: var(--text-4xl);
  font-weight: var(--weight-bold);
  font-variant-numeric: tabular-nums;
}

.card.dark {
  background: var(--surface);
  border-color: var(--line);
  color: var(--fg);
}

.card.dark .label,
.card.dark .muted {
  color: var(--muted);
}

.sheet {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
}

.sheet.pad {
  padding: var(--sp-16);
  display: flex;
  flex-direction: column;
  gap: var(--sp-16);
}

.tabs {
  display: flex;
  flex-wrap: wrap;
  gap: var(--sp-2);
  padding: var(--sp-8) var(--sp-8) 0;
  border-bottom: 1px solid var(--line);
}

.tab {
  background: none;
  border: 0;
  border-bottom: 2px solid transparent;
  padding: var(--sp-8) var(--sp-16);
  min-height: 44px;
  cursor: pointer;
  color: var(--muted);
  font-weight: var(--weight-medium);
}

.tab[aria-selected="true"] {
  color: var(--fg);
  font-weight: var(--weight-semibold);
  border-bottom-color: var(--brand);
}

.sortbtn {
  background: none;
  border: 0;
  padding: 0;
  margin: 0;
  font: inherit;
  color: inherit;
  text-transform: inherit;
  letter-spacing: inherit;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: var(--sp-4);
  min-height: 28px;
}

.sortbtn .arrow {
  font-size: var(--text-3xs);
  opacity: 0.45;
}

th[aria-sort="ascending"] .arrow,
th[aria-sort="descending"] .arrow {
  opacity: 1;
  color: var(--accent-ink);
}

th.right .sortbtn {
  flex-direction: row-reverse;
}

.count {
  display: inline-block;
  min-width: 20px;
  padding: var(--sp-2) var(--sp-8);
  margin-left: var(--sp-4);
  border-radius: var(--radius-pill);
  background: var(--surface-2);
  color: var(--muted);
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  text-align: center;
  font-variant-numeric: tabular-nums;
}

.tab[aria-selected="true"] .count {
  background: var(--brand);
  color: var(--on-accent);
}

.scroll {
  overflow-x: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
}

th {
  text-align: left;
  font-size: var(--text-xs);
  text-transform: uppercase;
  letter-spacing: var(--tracking-wide);
  color: var(--muted);
  font-weight: var(--weight-semibold);
  padding: var(--sp-16) var(--sp-8);
}

thead tr {
  background: var(--surface-2);
}

td {
  overflow-wrap: anywhere;
  padding: var(--sp-16) var(--sp-8);
  border-top: 1px solid var(--line);
  vertical-align: top;
}

tr.click {
  cursor: pointer;
}

tr.click:hover td {
  background: var(--surface-2);
}

th:first-child,
td:first-child {
  padding-left: var(--sp-16);
}

th:last-child,
td:last-child {
  padding-right: var(--sp-16);
}

.linkbtn {
  background: none;
  border: 0;
  padding: 0;
  color: var(--accent-ink);
  font-weight: var(--weight-medium);
  cursor: pointer;
  text-decoration: none;
  text-underline-offset: 3px;
}

.linkbtn:hover {
  text-decoration: underline;
}

.pill {
  display: inline-block;
  padding: var(--sp-4) var(--sp-8);
  border-radius: var(--radius-pill);
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  white-space: nowrap;
}

.pill.Draft {
  background: var(--surface-2);
  color: var(--muted);
}

.pill.Sent {
  background: var(--status-sent);
  color: var(--on-accent);
}

.pill.Partial {
  background: var(--warn-bg);
  color: var(--warn-fg);
}

.pill.Paid {
  background: var(--ok-bg);
  color: var(--ok-fg);
}

.pill.Void {
  background: var(--surface-2);
  color: var(--muted);
  text-decoration: line-through;
}

.pill.Overdue {
  background: var(--status-overdue);
  color: var(--on-accent);
}

.banner {
  border-radius: var(--radius-lg);
  padding: var(--sp-8) var(--sp-16);
  font-size: var(--text-sm);
  border: 1px solid transparent;
  display: flex;
  flex-wrap: wrap;
  gap: var(--sp-8);
  align-items: center;
}

.banner.ok {
  background: var(--ok-soft-bg);
  color: var(--ok-soft-fg);
  border-color: var(--ok-line);
}

.banner.bad {
  background: var(--bad-bg);
  color: var(--bad-fg);
  border-color: var(--bad-line);
}

.banner.info {
  background: var(--info-soft-bg);
  color: var(--info-fg);
  border-color: var(--info-line);
}

.field {
  display: flex;
  flex-direction: column;
  gap: var(--sp-8);
  font-size: var(--text-md);
  font-weight: var(--weight-semibold);
}

.field .hint {
  font-weight: var(--weight-regular);
  font-size: var(--text-sm);
  color: var(--muted);
}

.input {
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-lg);
  padding: 0 var(--sp-16);
  min-height: 44px;
  background: var(--surface);
  font-weight: var(--weight-regular);
  font-size: var(--text-base);
  box-sizing: border-box;
  width: 100%;
}

textarea.input {
  padding: var(--sp-8) var(--sp-16);
  line-height: var(--leading-normal);
  resize: vertical;
}

select.input {
  -webkit-appearance: none;
  appearance: none;
  padding-left: var(--sp-16) !important;
  padding-right: var(--sp-32) !important;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12' fill='none' stroke='%2366635D' stroke-width='1.8' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M3 4.5l3 3 3-3'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  background-size: 12px 12px;
  text-overflow: ellipsis;
}

.grid4 {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
  gap: var(--sp-16);
}

.money {
  display: flex;
  align-items: center;
  gap: var(--sp-4);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-lg);
  padding: 0 var(--sp-8);
  background: var(--surface);
}

.money span {
  color: var(--muted);
}

.money input {
  border: 0;
  min-height: 42px;
  width: 100%;
  text-align: right;
  background: transparent;
  font-size: var(--text-base);
  font-variant-numeric: tabular-nums;
}

.money input:focus-visible {
  outline: none;
}

.mpanel {
  gap: var(--sp-16);
}

.msum {
  margin: var(--sp-8) 0 0;
  max-width: 820px;
  line-height: var(--leading-loose);
}

.msum strong {
  color: var(--fg);
  font-weight: var(--weight-semibold);
}

.yoy {
  gap: var(--sp-16);
}

.ynav {
  display: flex;
  align-items: center;
  gap: var(--sp-8);
}

.ynav .btn {
  min-height: 32px;
  padding: 0 var(--sp-8);
}

.ynav strong {
  min-width: 48px;
  text-align: center;
  font-size: var(--text-xl);
}

.ygrid {
  display: grid;
  grid-template-columns: 240px minmax(0, 1fr);
  gap: var(--sp-16);
}

.ykpis {
  display: flex;
  flex-direction: column;
  gap: var(--sp-8);
}

.ykpis.yrow {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
}

.ykpis.yrow .ykpi > :last-child {
  margin-top: auto;
}

.ykpis.yrow .ybig {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  column-gap: var(--sp-8);
}

.stepper {
  padding-right: 0 !important;
  overflow: hidden;
}

.stepper .steps {
  display: flex;
  flex-direction: column;
  align-self: stretch;
  border-left: 1px solid var(--line-strong);
  margin-left: var(--sp-4);
}

.stepper .steps button {
  flex: 1;
  width: 28px;
  border: 0;
  background: var(--surface);
  color: var(--muted);
  font-size: var(--text-3xs);
  line-height: var(--leading-none);
  cursor: pointer;
  padding: 0;
}

.stepper .steps button + button {
  border-top: 1px solid var(--line-strong);
}

.stepper .steps button:hover {
  background: var(--surface-2);
  color: var(--fg);
}

.stepper .steps button:active {
  background: var(--line);
}

.ychg {
  font-size: var(--text-sm);
  font-weight: var(--weight-semibold);
  white-space: nowrap;
}

.ykpi {
  border: 1px solid var(--line);
  border-radius: var(--radius-xl);
  padding: var(--sp-16);
  display: flex;
  flex-direction: column;
  gap: var(--sp-4);
}

.ybig {
  font-size: var(--text-3xl);
  font-weight: var(--weight-bold);
  line-height: var(--leading-tight);
}

.ychart {
  border: 1px solid var(--line);
  border-radius: var(--radius-xl);
  padding: var(--sp-16);
  display: flex;
  flex-direction: column;
  gap: var(--sp-16);
}

.ychart .sw {
  display: inline-block;
  width: 12px;
  height: 12px;
  border-radius: var(--radius-xs);
  vertical-align: -1px;
  margin-right: var(--sp-4);
}

.yprevsw {
  background: transparent;
  border-top: 2px solid var(--fg);
  height: 0 !important;
  width: 14px !important;
  border-radius: 0 !important;
  vertical-align: 3px !important;
}

.yarea {
  position: relative;
  flex: 1;
  min-height: 200px;
  margin-left: var(--sp-32);
}

.yaxis {
  position: absolute;
  inset: 0 0 24px;
  border-bottom: 1px solid var(--line-strong);
}

.ytick {
  position: absolute;
  left: 0;
  right: 0;
  border-top: 1px solid var(--line);
}

.ytick span {
  position: absolute;
  left: -40px;
  top: -8px;
  width: 34px;
  text-align: right;
  font-size: var(--text-2xs);
  color: var(--muted);
}

.ycols {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template-columns: repeat(12, minmax(0, 1fr));
  gap: var(--sp-8);
}

.ycol {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.yplot {
  position: relative;
  flex: 1;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}

.ystack {
  width: min(100%, 48px);
  display: flex;
  flex-direction: column;
  border-radius: var(--radius-sm) var(--radius-sm) 0 0;
  overflow: hidden;
}

.ypaid {
  background: var(--good);
  min-height: 0;
}

.yopen {
  background: color-mix(in srgb, var(--good) 35%, var(--surface));
  min-height: 0;
}

.yprev {
  position: absolute;
  left: 50%;
  width: min(100%, 60px);
  transform: translateX(-50%);
  border-top: 2px solid var(--fg);
  z-index: 1;
}

.ylab {
  height: 24px;
  line-height: 24px;
  text-align: center;
  font-size: var(--text-2xs);
  color: var(--muted);
}

.ycol.future .ylab {
  opacity: 0.5;
}

.ycol:hover .ystack {
  filter: brightness(1.08);
}

.ytable:not(.stack) tr.ysel td {
  background: var(--surface-2);
}

.ytable.stack tr.ysel {
  box-shadow: inset 3px 0 0 var(--good);
}

@media (width <= 900px) {
  .ygrid {
    grid-template-columns: 1fr;
  }

  .ykpis {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}

@media (width <= 640px) {
  .ykpis {
    grid-template-columns: 1fr;
  }

  .ykpis.yrow {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .ykpis.yrow .ybig {
    font-size: var(--text-2xl);
  }

  .ykpis.yrow .ykpi {
    padding: var(--sp-8) var(--sp-16) var(--sp-16);
  }

  .yarea {
    min-height: 180px;
  }

  .ycols {
    gap: var(--sp-2);
  }

  .ylab {
    font-size: var(--text-3xs);
  }
}

.mphead {
  display: flex;
  flex-wrap: wrap;
  gap: var(--sp-8) var(--sp-32);
  justify-content: space-between;
  align-items: flex-end;
}

.mproom {
  text-align: right;
}

.mpbig {
  font-size: var(--text-4xl);
  font-weight: var(--weight-bold);
  line-height: var(--leading-tight);
}

.mpbig .small {
  font-weight: var(--weight-regular);
}

#mpBody {
  display: flex;
  flex-direction: column;
  gap: var(--sp-16);
}

.mpmarks {
  position: relative;
  height: 40px;
}

.mpmk {
  position: absolute;
  bottom: 0;
  height: 40px;
  border-left: 2px dashed var(--fg);
}

.mpmk span {
  position: absolute;
  left: var(--sp-8);
  top: 0;
  white-space: nowrap;
  font-size: var(--text-xs);
  line-height: var(--leading-tight);
  color: var(--muted);
}

.mpmk span.flip {
  left: auto;
  right: var(--sp-8);
  text-align: right;
}

.mpmk b {
  display: block;
  font-size: var(--text-sm);
  color: var(--fg);
}

.mpzone-l {
  position: absolute;
  top: 0;
  right: 0;
  text-align: right;
  font-size: var(--text-xs);
  line-height: var(--leading-tight);
  color: var(--good);
  white-space: nowrap;
}

.mpzone-l b {
  display: block;
  font-size: var(--text-sm);
}

.mptrack {
  position: relative;
  height: 30px;
  border-radius: var(--radius-lg);
  background: var(--surface-2);
  overflow: hidden;
}

.mptrack > div {
  position: absolute;
  top: 0;
  bottom: 0;
}

.mppaid {
  background: var(--brand, var(--accent));
}

.mppaid.good {
  background: var(--good);
}

.mpdue.good {
  background: repeating-linear-gradient(
    -45deg,
    var(--good) 0 6px,
    color-mix(in srgb, var(--good) 35%, var(--surface)) 6px 12px
  );
}

.mpdue {
  background: repeating-linear-gradient(
    -45deg,
    var(--brand, var(--accent)) 0 6px,
    color-mix(in srgb, var(--brand, var(--accent)) 40%, var(--surface)) 6px 12px
  );
}

.mpzone {
  right: 0;
  background: color-mix(in srgb, var(--good) 12%, transparent);
  border-left: 2px solid var(--good);
}

.mpline {
  width: 0;
  border-left: 2px dashed var(--fg);
}

.mplegend {
  display: flex;
  flex-wrap: wrap;
  gap: var(--sp-8) var(--sp-16);
  align-items: center;
}

.mplegend .sw {
  display: inline-block;
  width: 12px;
  height: 12px;
  border-radius: var(--radius-xs);
  vertical-align: -1px;
  margin-right: var(--sp-8);
  position: static;
}

.mpeq {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--sp-8) var(--sp-8);
  border-top: 1px solid var(--line);
  padding-top: var(--sp-16);
  font-size: var(--text-base);
}

@media (width <= 640px) {
  .mproom {
    text-align: left;
  }

  .mpbig {
    font-size: var(--text-2xl);
  }

  .mpmarks {
    height: 60px;
  }

  .mpmk {
    height: 60px;
  }

  .mpmk span {
    max-width: 60vw;
    white-space: normal;
    width: max-content;
  }

  .mpmk span,
  .mpzone-l {
    font-size: var(--text-2xs);
  }

  .mpzone-l {
    display: none;
  }
}

.potential {
  border-top: 1px solid var(--line);
  margin-top: var(--sp-8);
  padding-top: var(--sp-8);
  display: flex;
  flex-direction: column;
  gap: var(--sp-2);
}

.potline {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--sp-8);
}

.potline strong {
  font-size: var(--text-xl);
}

.grip {
  width: 36px;
  height: 44px;
  border: 0;
  background: transparent;
  color: var(--muted);
  cursor: grab;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-lg);
  touch-action: none;
  flex: none;
}

.grip:hover {
  background: var(--surface-2);
  color: var(--fg);
}

tr.dragging {
  opacity: 0.85;
  background: var(--surface-2);
  box-shadow: var(--shadow-md);
  position: relative;
  z-index: 2;
}

tr.dragging .grip {
  cursor: grabbing;
}

.row > .field {
  min-width: 0;
}

.row > .field input[type="time"],
.row > .field input[type="date"],
.row > input[type="time"],
.row > input[type="date"] {
  min-width: 0;
}

.money:focus-within {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.x {
  width: 44px;
  height: 44px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-lg);
  background: var(--surface);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.editor th {
  background: var(--surface-2);
  text-transform: none;
  letter-spacing: 0;
  font-size: var(--text-md);
  font-weight: var(--weight-medium);
}

.editor td {
  border-top: 0;
  border-bottom: 1px solid var(--line);
  padding: var(--sp-16) var(--sp-8);
}

.totals {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: var(--sp-8) var(--sp-16);
  width: 100%;
  max-width: 340px;
  margin-left: auto;
  font-size: var(--text-base);
}

.totals .due {
  font-weight: var(--weight-bold);
  font-size: var(--text-xl);
  border-top: 2px solid var(--fg);
  padding-top: var(--sp-8);
}

.split {
  display: flex;
  flex-wrap: wrap;
  gap: var(--sp-16);
  align-items: flex-start;
}

.split > .main {
  flex: 999 1 560px;
  min-width: 0;
}

.split.stretch {
  align-items: stretch;
}

.split.stretch > .side > .sheet {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.split > .side {
  flex: 1 1 320px;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: var(--sp-16);
}

/* invoice paper (on screen and in the PDF): always light */
.paper {
  background: var(--paper);
  color: var(--paper-fg);
  border: 1px solid var(--line);
  border-radius: var(--radius-xl);
  padding: var(--sp-32);
  display: flex;
  flex-direction: column;
  gap: var(--sp-32);
  font-size: var(--text-md);
}

.paper .ptop {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: var(--sp-16);
}

.paper .pmuted {
  color: var(--paper-muted);
}

.paper .ptitle {
  font-size: var(--text-4xl);
  font-weight: var(--weight-bold);
  letter-spacing: var(--tracking-wide);
}

.paper .pmeta {
  display: grid;
  grid-template-columns: auto auto;
  gap: var(--sp-4) var(--sp-16);
  justify-content: end;
  margin-top: var(--sp-8);
}

.paper table th {
  background: var(--paper-fg);
  color: var(--paper);
  text-transform: none;
  letter-spacing: 0;
  font-size: var(--text-sm);
  padding: var(--sp-8) var(--sp-16);
}

.paper table td {
  border-top: 0;
  border-bottom: 1px solid var(--paper-line);
  padding: var(--sp-8) var(--sp-16);
}

.paper .ptot {
  margin-left: auto;
  width: 100%;
  max-width: 280px;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: var(--sp-8) var(--sp-16);
}

.paper .pdue {
  font-weight: var(--weight-bold);
  font-size: var(--text-xl);
  border-top: 2px solid var(--paper-fg);
  padding-top: var(--sp-8);
}

.paper .plabel {
  font-size: var(--text-xs);
  text-transform: uppercase;
  letter-spacing: var(--tracking-wide);
  color: var(--paper-muted);
  font-weight: var(--weight-semibold);
}

#pdfStage {
  position: fixed;
  left: -10000px;
  top: 0;
  width: 816px;
}

/* html2pdf clones the paper out of #pdfStage, so PDF styles hang off a class on the paper itself. */
.paper.pdf {
  border: 0;
  border-radius: 0;
  box-shadow: none;
  width: 816px;
  box-sizing: border-box;
  padding: calc(var(--sp-32) + var(--sp-16));
}

.paper.pdf .pfoot {
  margin-top: auto;
}

.bars {
  display: flex;
  flex-direction: column;
  gap: var(--sp-8);
}

.barrow {
  display: grid;
  grid-template-columns: 80px minmax(0, 1fr) 76px;
  gap: var(--sp-16);
  align-items: center;
  font-size: var(--text-md);
  position: relative;
  border-radius: var(--radius-md);
  outline-offset: 4px;
}

.barrow:hover,
.barrow:focus-visible {
  background: var(--surface-2);
  box-shadow: 0 0 0 6px var(--surface-2);
}

.bartip {
  padding: 0;
  display: none;
  position: absolute;
  z-index: 20;
  left: 92px;
  top: calc(100% + 8px);
  width: min(380px, calc(100vw - 48px));
  background: var(--surface);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-lg);
  font-size: var(--text-sm);
  line-height: var(--leading-normal);
  cursor: default;
}

.barrow.up .bartip {
  top: auto;
  bottom: calc(100% + 8px);
}

.barrow:hover .bartip,
.barrow:focus-within .bartip {
  display: block;
}

.bartip h3 {
  margin: 0 0 var(--sp-8);
  font-size: var(--text-md);
}

.bartip .tipin {
  padding: var(--sp-16) var(--sp-16);
  max-height: min(70vh, 560px);
  overflow-y: auto;
  border-radius: var(--radius-xl);
}

.bartip::before {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  top: -12px;
  height: 12px;
}

.barrow.up .bartip::before {
  top: auto;
  bottom: -12px;
}

.bartip::after {
  content: "";
  position: absolute;
  left: var(--ax, 22px);
  top: -7px;
  width: 12px;
  height: 12px;
  background: var(--surface);
  border-left: 1px solid var(--line-strong);
  border-top: 1px solid var(--line-strong);
  transform: rotate(45deg);
  border-top-left-radius: 2px;
}

.barrow.up .bartip::after {
  top: auto;
  bottom: -7px;
  border: 0;
  border-right: 1px solid var(--line-strong);
  border-bottom: 1px solid var(--line-strong);
  border-radius: 0 0 var(--radius-xs) 0;
}

.bartip .tl[hidden] {
  display: none;
}

.bartip .tipmore {
  display: block;
  padding: var(--sp-2) 0 var(--sp-2) var(--sp-8);
  font-size: var(--text-xs);
  text-align: left;
}

.bartip .tl {
  display: flex;
  gap: var(--sp-8);
  justify-content: space-between;
}

.bartip .tl > span:last-child {
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.bartip .sub {
  color: var(--muted);
  padding-left: var(--sp-8);
  font-size: var(--text-xs);
}

.bartip .sec {
  border-top: 1px solid var(--line);
  margin-top: var(--sp-8);
  padding-top: var(--sp-8);
}

.bartip .tot {
  font-weight: var(--weight-bold);
}

.row.actions {
  justify-content: flex-end;
}

.bartip .tiplink {
  font-size: inherit;
  padding: 0;
  min-height: 0;
  font-weight: var(--weight-semibold);
}

.track {
  height: 12px;
  background: var(--surface-2);
  border-radius: var(--radius-md);
  overflow: hidden;
  position: relative;
}

.fill {
  height: 100%;
  border-radius: var(--radius-md);
  background: var(--brand);
}

.fill.proj {
  background: repeating-linear-gradient(
    45deg,
    var(--brand),
    var(--brand) 4px,
    transparent 4px,
    transparent 8px
  );
  opacity: 0.7;
}

.mapbar {
  display: flex;
  height: 36px;
  border-radius: var(--radius-lg);
  overflow: hidden;
}

.legend {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: var(--sp-16);
}

.dot {
  width: 12px;
  height: 12px;
  border-radius: var(--radius-xs);
  margin-top: var(--sp-4);
  flex: none;
}

input[type="range"] {
  accent-color: var(--brand);
}

/* text selection: soft brand orange with dark text, in fields and on the page */
::selection {
  background: var(--selection-bg);
  color: var(--fg);
}

.util ::selection {
  background: var(--brand);
  color: var(--on-dark);
}

/* checkboxes: rounded box, green with a white check when on */
input[type="checkbox"] {
  -webkit-appearance: none;
  appearance: none;
  flex: none;
  width: 20px;
  height: 20px;
  margin: 0;
  border: 1.5px solid var(--line-strong);
  border-radius: var(--radius-md);
  background: var(--surface) center / 14px 14px no-repeat;
  cursor: pointer;
  transition:
    background-color 0.12s,
    border-color 0.12s,
    box-shadow 0.12s;
  vertical-align: -4px;
}

input[type="checkbox"]:hover {
  border-color: var(--muted);
}

input[type="checkbox"]:checked {
  background-color: var(--good);
  border-color: var(--good);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none' stroke='white' stroke-width='2.4' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M3.5 8.5l3 3 6-7'/%3E%3C/svg%3E");
}

input[type="checkbox"]:focus-visible {
  outline: none;
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--good) 30%, transparent);
}

input[type="checkbox"]:disabled {
  opacity: 0.5;
  cursor: default;
}

label:has(> input[type="checkbox"]) {
  cursor: pointer;
}

.leave {
  position: fixed;
  z-index: 60;
  left: 50%;
  top: var(--sp-16);
  transform: translateX(-50%);
  width: min(520px, calc(100vw - 32px));
  background: var(--surface);
  border: 1px solid var(--line-strong);
  border-left: 4px solid var(--bad-fg);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-lg);
  padding: var(--sp-16) var(--sp-16);
  display: flex;
  flex-direction: column;
  gap: var(--sp-16);
}

.leave[hidden] {
  display: none;
}

.appfoot {
  border-top: 1px solid var(--line);
  padding-block: var(--sp-16);
  font-size: var(--text-xs);
  color: var(--muted);
}

.appfoot a {
  color: var(--muted);
}

.appfoot a:hover {
  color: var(--accent-ink);
}

.footrow {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--sp-8) var(--sp-16);
}

.footrow .footswitch {
  margin-left: auto;
}

.footswitch {
  font: inherit;
  color: var(--muted);
  background: none;
  border: 0;
  padding: 0;
  text-decoration: underline;
  cursor: pointer;
}

.footswitch:hover {
  color: var(--accent-ink);
}

.demotag {
  margin-left: var(--sp-8);
  padding: var(--sp-2) var(--sp-8);
  border-radius: var(--radius-pill);
  font-size: var(--text-2xs);
  font-weight: var(--weight-bold);
  letter-spacing: var(--tracking-wide);
  text-transform: uppercase;
  background: var(--warn-bg);
  color: var(--warn-fg);
  vertical-align: middle;
}

.demotag[hidden] {
  display: none;
}

.empty {
  padding: var(--sp-32) var(--sp-16);
  text-align: center;
  color: var(--muted);
}

@media (prefers-reduced-motion: no-preference) {
  .btn,
  .tab,
  .navbtn {
    transition:
      background-color 0.12s,
      border-color 0.12s;
  }
}

@media (width <= 560px) {
  .paper {
    padding: var(--sp-16);
  }

  .sync > span {
    display: none;
  }
}

/* ===== Responsive: phone / tablet / desktop ===== */
*,
*::before,
*::after {
  box-sizing: border-box;
}

img,
svg {
  max-width: 100%;
}

td.num,
th.right,
.paper td.num,
.paper th.right {
  white-space: nowrap;
  overflow-wrap: normal;
  word-break: keep-all;
}

.paper table td:first-child {
  width: 100%;
}

@media (width <= 900px) {
  #nav {
    flex: 1 0 100%;
    flex-wrap: nowrap;
    overflow-x: auto;
    gap: var(--sp-4);
    margin-inline: calc(-1 * var(--sp-4));
    padding: var(--sp-2) var(--sp-4) var(--sp-4);
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
  }

  #nav::-webkit-scrollbar {
    display: none;
  }

  .navbtn {
    flex: none;
    white-space: nowrap;
  }
}

@media (width <= 640px) {
  main {
    padding-block: var(--sp-16) var(--sp-32);
    gap: var(--sp-16);
  }

  /* All five sections fit on a 320px+ screen: smaller text and padding, and the tabs share any spare width. */
  .navbtn {
    flex: 1 1 auto;
    padding: 0 var(--sp-4);
    font-size: var(--text-sm);
    min-height: 38px;
  }

  #nav {
    gap: 0;
  }

  .util .sync > span {
    display: none;
  }

  /* Icon-only header buttons; the label stays for screen readers. */
  .util .sync button,
  .utilbtn {
    min-width: 40px;
    min-height: 40px;
    padding: 0;
    justify-content: center;
  }

  .ulabel {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
  }

  h1 {
    font-size: var(--text-2xl);
  }

  .sheet.pad {
    padding: var(--sp-16);
    gap: var(--sp-16);
  }

  .card {
    padding: var(--sp-16) var(--sp-16);
  }

  .card .big {
    font-size: var(--text-3xl);
  }

  .cards {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: var(--sp-8);
  }

  .cards > .card:only-child,
  .cards > .card:last-child:nth-child(odd) {
    grid-column: 1 / -1;
  }

  .grid4 {
    grid-template-columns: 1fr;
  }

  .split > .main,
  .split > .side {
    flex-basis: 100%;
  }

  .row > .btn.primary:only-of-type {
    margin-left: auto;
  }

  .tabs {
    flex-wrap: nowrap;
    overflow-x: auto;
    scrollbar-width: none;
    padding: var(--sp-4) var(--sp-8) 0;
  }

  .tabs::-webkit-scrollbar {
    display: none;
  }

  .tab {
    flex: none;
    white-space: nowrap;
    padding: var(--sp-8) var(--sp-8);
  }

  .totals,
  main .paper .ptot {
    max-width: none;
  }

  main .paper {
    padding: var(--sp-16);
    gap: var(--sp-16);
  }

  main .paper .ptitle {
    font-size: var(--text-2xl);
  }

  main .paper table {
    min-width: 0 !important;
    font-size: var(--text-sm);
  }

  main .paper table th,
  main .paper table td {
    padding: var(--sp-8) var(--sp-4);
  }

  main .paper table th:first-child,
  main .paper table td:first-child {
    padding-left: var(--sp-8);
  }

  main .paper table th:last-child,
  main .paper table td:last-child {
    padding-right: var(--sp-8);
    white-space: nowrap;
  }

  .bartip {
    left: 0;
    width: calc(100vw - 32px);
  }

  .bartip::after {
    left: 30px;
  }

  .barrow {
    grid-template-columns: 44px minmax(0, 1fr) 64px;
    gap: var(--sp-8);
    font-size: var(--text-sm);
  }

  .leave {
    top: max(8px, env(safe-area-inset-top, 0px));
  }

  .row.actions .btn {
    flex: 1 1 auto;
    justify-content: center;
  }

  /* tables become stacked cards */
  table.stack {
    min-width: 0 !important;
    display: block;
  }

  table.stack thead {
    display: none;
  }

  table.stack tbody,
  table.stack tfoot {
    display: block;
  }

  table.stack tr {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: var(--sp-8) var(--sp-16);
    padding: var(--sp-16) var(--sp-16);
    border-bottom: 1px solid var(--line);
    position: relative;
  }

  table.stack td {
    display: block;
    border: 0 !important;
    padding: 0 !important;
    min-width: 0;
    text-align: left;
  }

  table.stack td.right {
    text-align: left;
  }

  table.stack td[data-label]::before {
    content: attr(data-label);
    display: block;
    font-size: var(--text-2xs);
    font-weight: var(--weight-semibold);
    text-transform: uppercase;
    letter-spacing: var(--tracking-wide);
    color: var(--muted);
    margin-bottom: var(--sp-4);
  }

  table.stack td[data-label=""]::before {
    display: none;
  }

  table.stack td[colspan] {
    grid-column: 1 / -1;
  }

  table.stack:not(.editor) td:first-child {
    grid-column: 1 / -1;
  }

  table.stack:not(.editor) td:last-child:nth-child(even) {
    grid-column: 1 / -1;
  }

  tr.click:hover td {
    background: none;
  }

  tr.click:active {
    background: var(--surface-2);
  }

  table.stack .empty {
    padding: var(--sp-16) 0 !important;
    text-align: center;
  }

  table.stack:not(.editor) td:first-child::before {
    display: none;
  }

  table.stack:not(.editor) td:empty {
    display: none;
  }

  table.clientlist.stack td:nth-child(1) {
    padding-right: var(--sp-64) !important;
  }

  table.clientlist.stack td:nth-child(3) {
    grid-column: 1 / -1;
    overflow-wrap: anywhere;
  }

  table.clientlist.stack td:last-child {
    position: absolute;
    top: var(--sp-16);
    right: var(--sp-16);
  }

  table.clientlist.stack td:last-child::before {
    display: none;
  }

  /* invoice list: compact two-line cards */
  table.invlist.stack tbody tr {
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-areas: "num amt" "cli cli" "st due";
    gap: var(--sp-4) var(--sp-16);
  }

  table.invlist.stack tbody td::before {
    display: none;
  }

  table.invlist.stack tbody td:nth-child(1) {
    grid-area: st;
    grid-column: auto;
  }

  table.invlist.stack tbody td:nth-child(2) {
    display: none;
  }

  table.invlist.stack tbody td:nth-child(3) {
    grid-area: num;
  }

  table.invlist.stack tbody td:nth-child(4) {
    grid-area: cli;
    color: var(--muted);
  }

  table.invlist.stack tbody td:nth-child(5) {
    grid-area: due;
    text-align: right;
    font-size: var(--text-sm);
    align-self: center;
  }

  table.invlist.stack tbody td:nth-child(5)::after {
    content: none;
  }

  table.invlist.stack tbody td:nth-child(6) {
    grid-area: amt;
    text-align: right;
    grid-column: auto;
  }

  table.invlist.stack tfoot tr {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
  }
}

@media (width <= 1024px) {
  /* editable grids: one card per row, remove button top right */
  table.editor.stack {
    min-width: 0 !important;
    display: block;
  }

  table.editor.stack thead {
    display: none;
  }

  table.editor.stack tbody {
    display: block;
  }

  table.editor.stack tr {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: var(--sp-8) var(--sp-16);
    padding: var(--sp-16) var(--sp-16);
    border-bottom: 1px solid var(--line);
    position: relative;
    min-height: 72px;
  }

  table.editor.stack td {
    display: block;
    border: 0 !important;
    padding: 0 !important;
    min-width: 0;
    text-align: left;
    grid-column: span 2;
  }

  table.editor.stack td[data-label]::before {
    content: attr(data-label);
    display: block;
    font-size: var(--text-2xs);
    font-weight: var(--weight-semibold);
    text-transform: uppercase;
    letter-spacing: var(--tracking-wide);
    color: var(--muted);
    margin-bottom: var(--sp-4);
  }

  table.editor.stack td[data-label=""]::before {
    display: none;
  }

  table.editor.stack td[colspan] {
    grid-column: 1 / -1;
  }

  table.editor.stack
    td:has(> .money, > input[type="number"], > input[type="date"], > select) {
    grid-column: auto;
  }

  table.editor.stack td:has(> .x) {
    position: absolute;
    top: var(--sp-16);
    right: var(--sp-16);
  }

  table.editor.stack td:has(> .x)::before {
    display: none;
  }

  table.editor.stack td:has(> .x) + td {
    grid-column: 1 / -1 !important;
    padding-right: var(--sp-64) !important;
  }

  table.editor.stack td:has(> .x) + td > * {
    max-width: 420px;
  }

  table.editor.stack tr.wdrow {
    padding-top: 0;
  }

  table.editor.stack tr:has(+ tr.wdrow) {
    border-bottom: 0;
  }

  table.editor.stack tr.wdrow td:empty {
    display: none;
  }

  table.editor.stack .empty {
    padding: var(--sp-16) 0 !important;
    text-align: center;
  }
}

/* wide editable grids (subscriptions, contracts) are cards at every size */

/* editable grids: one card per row, remove button top right */
table.editor.wide.stack {
  min-width: 0 !important;
  display: block;
}

table.editor.wide.stack thead {
  display: none;
}

table.editor.wide.stack tbody {
  display: block;
}

table.editor.wide.stack tr {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: var(--sp-8) var(--sp-16);
  padding: var(--sp-16) var(--sp-16);
  border-bottom: 1px solid var(--line);
  position: relative;
  min-height: 72px;
}

table.editor.wide.stack td {
  display: block;
  border: 0 !important;
  padding: 0 !important;
  min-width: 0;
  text-align: left;
  grid-column: span 2;
}

table.editor.wide.stack td[data-label]::before {
  content: attr(data-label);
  display: block;
  font-size: var(--text-2xs);
  font-weight: var(--weight-semibold);
  text-transform: uppercase;
  letter-spacing: var(--tracking-wide);
  color: var(--muted);
  margin-bottom: var(--sp-4);
}

table.editor.wide.stack td[data-label=""]::before {
  display: none;
}

table.editor.wide.stack td[colspan] {
  grid-column: 1 / -1;
}

table.editor.wide.stack
  td:has(> .money, > input[type="number"], > input[type="date"], > select) {
  grid-column: auto;
}

table.editor.wide.stack td:has(> .x) {
  position: absolute;
  top: var(--sp-16);
  right: var(--sp-16);
}

table.editor.wide.stack td:has(> .x)::before {
  display: none;
}

table.editor.wide.stack td:has(> .x) + td {
  grid-column: 1 / -1 !important;
  padding-right: var(--sp-64) !important;
}

table.editor.wide.stack td:has(> .x) + td > * {
  max-width: 420px;
}

table.editor.wide.stack tr.wdrow {
  padding-top: 0;
}

table.editor.wide.stack tr:has(+ tr.wdrow) {
  border-bottom: 0;
}

table.editor.wide.stack tr.wdrow td:empty {
  display: none;
}

table.editor.wide.stack .empty {
  padding: var(--sp-16) 0 !important;
  text-align: center;
}

@media (width <= 640px) {
  table.editor.stack tr {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  table.editor.stack td {
    grid-column: 1 / -1;
  }

  main .paper td.num {
    white-space: nowrap;
  }
}

@media (width <= 380px) {
  .cards {
    grid-template-columns: 1fr;
  }
}

/* ===== Mobile form alignment ===== */
@media (width <= 1024px) {
  table.editor.stack .input,
  table.editor.stack .money {
    width: 100%;
  }

  table.editor.stack td:has(> .x) {
    top: var(--sp-16);
    right: 0;
  }

  table.editor.stack td:has(> .x) .x {
    width: 32px;
    height: 32px;
    border-radius: var(--radius-md);
  }

  table.editor.stack td:has(> .x) + td {
    padding-right: 0 !important;
  }

  table.editor.stack td:has(> .x) + td::before {
    min-height: 32px;
    display: flex;
    align-items: flex-end;
    padding-right: var(--sp-32);
  }

  table.editor.stack tr {
    padding-left: 0;
    padding-right: 0;
  }
}

@media (width <= 640px) {
  .input,
  .money input,
  select.input,
  textarea.input {
    font-size: var(--text-lg);
  }

  .input,
  .money {
    min-height: 44px;
  }

  input[type="date"].input {
    -webkit-appearance: none;
    appearance: none;
    display: block;
    min-width: 0;
    text-align: left;
    line-height: 42px;
  }

  input[type="date"].input::-webkit-date-and-time-value {
    text-align: left;
  }

  select.input {
    min-width: 0;
    text-overflow: ellipsis;
  }

  .money {
    min-width: 0;
  }

  .money input {
    min-width: 0;
  }

  .field {
    min-width: 0;
  }

  table.editor.stack td > .small,
  table.editor.stack td > .row.small {
    margin-top: var(--sp-8);
  }

  .sheet.pad > .row:first-child {
    row-gap: var(--sp-4);
  }

  .sheet.pad > .row:first-child .spacer {
    display: none;
  }

  .sheet.pad > .row:first-child > .small:last-child {
    flex-basis: 100%;
  }
}

@media (width <= 1024px) {
  table.editor.wide.stack tr {
    padding-left: 0;
    padding-right: 0;
  }

  table.editor.wide.stack td:has(> .x) {
    right: 0;
  }

  table.editor.wide.stack td:has(> .x) + td {
    padding-right: 0 !important;
  }
}

@media (width <= 640px) {
  table.editor.wide.stack tr {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  table.editor.wide.stack td {
    grid-column: 1 / -1;
  }

  table.editor.wide.stack
    td:has(> .money, > input[type="number"], > input[type="date"], > select) {
    grid-column: auto;
  }

  table.editor.wide.stack td:has(> .x) + td {
    grid-column: 1 / -1 !important;
  }

  table.editor.stack .input.right,
  table.editor.stack .money input {
    text-align: left;
  }

  table.editor.stack .money {
    padding: 0 var(--sp-16) !important;
    gap: var(--sp-8);
  }

  table.editor.stack .input {
    padding-left: var(--sp-16) !important;
    padding-right: var(--sp-16) !important;
  }

  table.editor.stack select.input {
    padding-right: var(--sp-32) !important;
  }
}

/* Wide card grids above phone size: size each field to its content instead of stretching to fill the row. */
@media (width >= 641px) {
  table.editor.wide.stack tr:not(.wdrow) {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-start;
    gap: var(--sp-8) var(--sp-16);
  }

  table.editor.wide.stack tr:not(.wdrow) > td {
    flex: 0 0 auto;
  }

  table.editor.wide.stack tr:not(.wdrow) > td:has(> .x) + td,
  table.editor.wide.stack tr > td[colspan] {
    flex-basis: 100%;
  }

  table.editor.wide.stack tr:not(.wdrow) > td:has(> .money) {
    width: 100px;
  }

  table.editor.wide.stack tr:not(.wdrow) > td:has(> input[type="number"]) {
    width: 72px;
  }

  table.editor.wide.stack tr:not(.wdrow) > td:has(> select) {
    width: 165px;
  }

  table.editor.wide.stack tr:not(.wdrow) > td:has(> select[data-kf="status"]) {
    width: 120px;
  }

  table.editor.wide.stack tr:not(.wdrow) > td:has(> select[data-rf="via"]) {
    width: 215px;
  }

  table.editor.wide.stack tr:not(.wdrow) > td:has(> input[type="date"]) {
    width: 150px;
  }

  table.editor.wide.stack tr:not(.wdrow) > td:has(> input:not([type])),
  table.editor.wide.stack
    tr:not(.wdrow)
    > td:has(> input[type="text"]:not([data-moneyfmt])) {
    width: 240px;
  }

  table.editor.wide.stack tr:not(.wdrow) > td:has(> .linkbtn) .linkbtn {
    min-height: 44px;
    display: inline-flex;
    align-items: center;
  }
}

.filters {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: var(--sp-8) var(--sp-16);
  padding: var(--sp-16) var(--sp-16);
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}

.filter {
  display: flex;
  flex-direction: column;
  gap: var(--sp-4);
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  color: var(--muted);
  flex: 1 1 180px;
  max-width: 260px;
  min-width: 0;
}

.filter .input {
  min-height: 40px;
  font-size: var(--text-md);
  font-weight: var(--weight-regular);
  color: var(--fg);
}

.filters .btn {
  min-height: 40px;
}

@media (width <= 640px) {
  .filters {
    padding: var(--sp-16) var(--sp-16);
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .filter {
    max-width: none;
  }

  .filter .input {
    font-size: var(--text-lg);
    min-height: 44px;
  }

  .filters .btn {
    min-height: 44px;
    justify-content: center;
  }
}

@media (width <= 640px) {
  .tabs {
    gap: 0;
    flex-wrap: wrap;
    overflow: visible;
    padding: var(--sp-4) var(--sp-8) 0;
  }

  .tabs .tab {
    padding: var(--sp-8) var(--sp-4);
    font-size: var(--text-sm);
  }

  .tabs .tab .count {
    margin-left: var(--sp-2);
    padding: var(--sp-2) var(--sp-4);
    min-width: 0;
  }

  .tabs > label {
    flex-basis: 100%;
    margin: 0 !important;
    padding: var(--sp-8) var(--sp-8) var(--sp-8) !important;
    border-top: 1px solid var(--line);
    justify-content: flex-start;
  }

  .tabs > label .input {
    flex: 1;
    font-size: var(--text-lg);
  }
}

.colltoggle {
  display: inline-flex;
  align-items: center;
  gap: var(--sp-8);
  background: none;
  border: 0;
  padding: var(--sp-4) var(--sp-8) var(--sp-4) var(--sp-2);
  margin: calc(-1 * var(--sp-4)) 0 calc(-1 * var(--sp-4)) calc(-1 * var(--sp-2));
  cursor: pointer;
  color: inherit;
  border-radius: var(--radius-md);
  min-height: 36px;
}

.colltoggle:hover {
  background: var(--surface-2);
}

.colltoggle svg {
  transition: transform 0.15s;
  flex: none;
  color: var(--muted);
}

.colltoggle[aria-expanded="true"] svg {
  transform: rotate(90deg);
}

section.collapsible[data-collapsed] > :not(.row:first-child) {
  display: none;
}

section.collapsible[data-collapsed] > .row:first-child > .small.muted {
  display: none;
}

.ccard {
  overflow: hidden;
}

.ccard-head,
.ccard-row {
  display: flex;
  align-items: flex-start;
  gap: var(--sp-16);
  padding: var(--sp-16) var(--sp-16);
}

.ccard-head {
  background: color-mix(
    in srgb,
    var(--brand, var(--accent)) 6%,
    var(--surface)
  );
  align-items: center;
}

.ccard-row {
  border-top: 1px solid var(--line);
}

.ccard-row.editing {
  display: block;
  background: var(--surface-2);
}

.ccard-name {
  flex: 1;
  min-width: 0;
  line-height: var(--leading-normal);
  padding-top: var(--sp-4);
}

.ccard-head .ccard-name {
  padding-top: 0;
}

.ccard-name strong {
  font-size: var(--text-base);
}

.ccard-name a {
  color: var(--muted);
  text-decoration: none;
}

.ccard-name a:hover {
  text-decoration: underline;
  color: var(--accent-ink);
}

.ccard.inactive .ccard-name strong {
  color: var(--muted);
}

.btn.sm {
  min-height: 34px;
  padding: 0 var(--sp-16);
  font-size: var(--text-sm);
  font-weight: var(--weight-medium);
  gap: var(--sp-8);
  flex: none;
}

.clsearch {
  position: relative;
  max-width: 360px;
}

.clsearch svg {
  position: absolute;
  left: var(--sp-8);
  top: 50%;
  transform: translateY(-50%);
  color: var(--muted);
  pointer-events: none;
}

.clsearch .input {
  padding-left: var(--sp-32);
}

.cltabs {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: var(--sp-4) var(--sp-8) 0;
}

.wdgrid {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: var(--sp-8);
}

.wdcell {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: var(--sp-8);
  display: flex;
  flex-direction: column;
  gap: var(--sp-4);
  min-width: 0;
}

.wdwrap {
  display: flex;
  flex-direction: column;
  gap: var(--sp-16);
}

.wdyear {
  display: flex;
  flex-direction: column;
  gap: var(--sp-8);
}

.wdyhead {
  gap: var(--sp-8) var(--sp-16);
}

.wdyhead strong {
  font-size: var(--text-base);
}

.wdcell.past,
.wdcell.off {
  opacity: 0.55;
}

.wdcell.off {
  background: transparent;
  border-style: dashed;
  justify-content: center;
  align-items: center;
  min-height: 48px;
}

.wdnote {
  font-size: var(--text-2xs);
}

.wdcell.now {
  border-color: var(--accent-ink);
}

.wdstep button:disabled {
  cursor: default;
  color: var(--muted);
}

.wdcell.has {
  border-color: color-mix(
    in srgb,
    var(--brand, var(--accent)) 45%,
    var(--line)
  );
}

.wdhead {
  font-size: var(--text-sm);
}

.wdstep {
  display: grid;
  grid-template-columns: 28px minmax(0, 1fr) 28px;
  align-items: center;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-md);
  overflow: hidden;
}

.wdstep button {
  height: 32px;
  border: 0;
  background: var(--surface-2);
  color: var(--fg);
  font-size: var(--text-lg);
  cursor: pointer;
  padding: 0;
}

.wdstep button:hover {
  background: var(--line);
}

.wdstep .input {
  min-height: 32px !important;
  height: 32px;
  border: 0;
  border-radius: 0;
  text-align: center;
  padding: 0 !important;
  font-weight: var(--weight-semibold);
  -moz-appearance: textfield;
}

.wdstep .input::-webkit-inner-spin-button,
.wdstep .input::-webkit-outer-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

.wdlab {
  font-size: var(--text-2xs);
  text-align: center;
  margin-top: calc(-1 * var(--sp-2));
}

.wdfoot {
  display: flex;
  flex-direction: column;
  gap: var(--sp-4);
}

.wdbar {
  height: 4px;
  border-radius: var(--radius-xs);
  background: var(--surface-2);
  overflow: hidden;
}

.wdbar i {
  display: block;
  height: 100%;
  background: var(--good);
}

.wdmeta {
  display: flex;
  justify-content: space-between;
  gap: var(--sp-4);
  font-size: var(--text-2xs);
  color: var(--muted);
  white-space: nowrap;
}

.wdmeta .num {
  color: var(--fg);
  font-weight: var(--weight-semibold);
}

@media (width <= 1024px) {
  .wdgrid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }
}

@media (width <= 640px) {
  .wdgrid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .wdcell.off {
    grid-column: 1 / -1 !important;
  }

  .wdstep .input {
    font-size: var(--text-lg);
  }
}

.reclist > .ccard-row:first-child {
  border-top: 0;
}

.recrow {
  align-items: center;
}

.recrow .ccard-name .small {
  margin-top: var(--sp-2);
}

.recamt {
  text-align: right;
  white-space: nowrap;
}

.recrow.inactive .ccard-name strong,
.recrow.inactive .recamt strong {
  color: var(--muted);
}

.setgrid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: var(--sp-16);
}

.setgrid .setwide {
  grid-column: 1 / -1;
}

.setlogo {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--sp-16);
}

.setlogo-img {
  width: 240px;
  max-width: 100%;
  height: 72px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px dashed var(--line-strong);
  border-radius: var(--radius-lg);
  background: var(--paper);
  padding: var(--sp-8);
}

.setlogo-img img {
  max-width: 100%;
  max-height: 100%;
}

.setfav {
  width: 32px;
  height: 32px;
  flex: none;
  border-radius: var(--radius-md);
  border: 1px solid var(--line);
  object-fit: contain;
  background: var(--paper);
}

.setfav-empty {
  display: inline-block;
  background: repeating-linear-gradient(
    45deg,
    var(--surface-2) 0 4px,
    transparent 4px 8px
  );
}

.pctin input {
  text-align: right;
}

.setbar {
  position: sticky;
  bottom: var(--sp-16);
  z-index: 4;
  background: var(--surface);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-xl);
  padding: var(--sp-8) var(--sp-16);
  box-shadow: var(--shadow-md);
}

.setbar[hidden] {
  display: none;
}

.edform {
  display: flex;
  flex-direction: column;
  gap: var(--sp-16);
}

.edsec {
  gap: var(--sp-16);
}

.edsec h2 {
  margin: 0;
}

.edsec .grid4 {
  grid-template-columns: repeat(4, minmax(0, 1fr));
}

.edsubject {
  grid-column: span 3;
}

.edhalf {
  grid-column: span 2;
}

.ednotes {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: var(--sp-16);
  align-items: start;
}

.cleanopts summary {
  cursor: pointer;
  color: var(--accent-ink);
  font-weight: var(--weight-semibold);
  list-style: none;
}

.cleanopts summary::-webkit-details-marker {
  display: none;
}

.cleanopts summary::after {
  content: " ▾";
}

.cleanopts[open] summary::after {
  content: " ▴";
}

.cleanopts {
  position: relative;
}

.cleanbody {
  position: absolute;
  right: 0;
  top: calc(100% + var(--sp-8));
  z-index: 10;
  width: max-content;
  max-width: min(420px, calc(100vw - 32px));
  display: flex;
  flex-direction: column;
  gap: var(--sp-8);
  padding: var(--sp-16);
  background: var(--surface);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-lg);
}

@media (width <= 640px) {
  .cleanopts {
    position: static;
  }

  .edsec {
    position: relative;
  }

  .cleanbody {
    left: var(--sp-16);
    right: var(--sp-16);
    top: auto;
    margin-top: var(--sp-8);
    width: auto;
    max-width: none;
  }
}

@media (width <= 900px) {
  .edsec .grid4 {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .edsubject {
    grid-column: 1 / -1;
  }
}

@media (width <= 640px) {
  .edsec .grid4 {
    grid-template-columns: 1fr;
  }

  .edhalf {
    grid-column: auto;
  }
}

.ctform {
  display: flex;
  flex-direction: column;
  gap: var(--sp-16);
}

.ctgrid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: var(--sp-16);
}

main > .ccard + .ccard {
  margin-top: calc(-1 * var(--sp-8));
}

@media (width <= 640px) {
  .ccard-head {
    flex-wrap: wrap;
  }

  .ccard-head .ccard-name {
    order: -1;
    flex-basis: 100%;
  }

  .ccard-head [data-addcontact] {
    margin-left: auto;
  }

  .recrow {
    flex-wrap: wrap;
  }

  .recrow .ccard-name {
    order: -1;
    flex-basis: 100%;
  }

  .recrow .recamt {
    margin-left: auto;
  }

  .ctgrid {
    grid-template-columns: 1fr;
  }

  .ctgrid > * {
    grid-column: auto !important;
  }

  .ctform .row .btn {
    flex: 1 1 auto;
    justify-content: center;
  }
}
