:root {
  --bg: #eef2f0;
  --surface: #ffffff;
  --surface-muted: #f5f7f6;
  --text: #17211d;
  --muted: #647169;
  --line: #d5ddd8;
  --line-strong: #aebbb4;
  --primary: #176b4a;
  --primary-strong: #0f563a;
  --primary-soft: #e7f3ed;
  --danger: #a52a22;
  --danger-soft: #f8eae8;
  --warning: #775f14;
  --warning-soft: #fff5d9;
  --info: #315d78;
  --control-height: 44px;
  --page-gap: 10px;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html {
  color-scheme: light;
  scroll-behavior: smooth;
  scroll-padding-top: 12px;
  -webkit-text-size-adjust: 100%;
}

body,
input,
select,
button,
textarea {
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
}

body {
  min-width: 280px;
  padding: 10px;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
}

button,
input,
select {
  min-height: var(--control-height);
  border: 1px solid var(--line-strong);
  border-radius: 2px;
  font-size: 16px;
}

input,
select {
  min-width: 0;
  padding: 9px 11px;
  background: var(--surface);
  color: var(--text);
}

input::placeholder {
  color: #7b8780;
}

input:focus,
select:focus {
  border-color: var(--primary);
  outline: 3px solid rgba(23, 107, 74, 0.16);
  outline-offset: 0;
}

button {
  padding: 9px 14px;
  border-color: var(--primary);
  background: var(--primary);
  color: #fff;
  cursor: pointer;
  font-weight: 650;
  touch-action: manipulation;
}

button:hover {
  background: var(--primary-strong);
}

button:focus-visible,
a:focus-visible,
summary:focus-visible {
  outline: 3px solid rgba(23, 107, 74, 0.28);
  outline-offset: 2px;
}

button:disabled,
input:disabled,
select:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

button.ghost {
  border-color: var(--line-strong);
  background: var(--surface);
  color: var(--text);
}

button.ghost:hover {
  background: var(--surface-muted);
}

button.danger {
  min-height: var(--control-height);
  padding: 8px 12px;
  border-color: var(--danger);
  background: var(--surface);
  color: var(--danger);
  font-size: 14px;
}

button.danger:hover {
  background: var(--danger-soft);
}

button.full {
  width: 100%;
}

button.small-action {
  min-height: 44px;
  padding: 7px 11px;
  font-size: 14px;
}

.app-shell {
  width: 100%;
  max-width: 860px;
  margin: 0 auto;
}

.top-card,
.card {
  border: 1px solid var(--line);
  border-radius: 0;
  background: var(--surface);
}

.top-card {
  position: relative;
  padding: 14px;
  margin-bottom: var(--page-gap);
  border-top: 4px solid var(--primary);
}

.workspace {
  display: grid;
  gap: var(--page-gap);
}

.card {
  min-width: 0;
  padding: 14px;
  scroll-margin-top: 10px;
}

.header-top {
  display: flex;
  align-items: center;
  gap: 9px;
}

input.title-input {
  flex: 1 1 auto;
  min-width: 0;
  height: 46px;
  padding: 7px 10px;
  border-color: var(--line-strong);
  color: var(--text);
  font-size: 19px;
  font-weight: 750;
}

input.desc-input {
  width: 100%;
  height: 44px;
  margin-top: 8px;
  padding: 8px 10px;
  font-size: 16px;
}

.header-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: flex-end;
  gap: 7px;
  margin-top: 9px;
}

.header-actions > button {
  min-height: 44px;
  font-size: 14px;
}

.more-menu {
  position: relative;
}

.more-menu summary {
  display: grid;
  min-width: 72px;
  min-height: 44px;
  padding: 8px 12px;
  place-items: center;
  border: 1px solid var(--line-strong);
  border-radius: 2px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  font-weight: 650;
  list-style: none;
}

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

.more-menu-panel {
  position: absolute;
  top: calc(100% + 5px);
  right: 0;
  z-index: 20;
  display: grid;
  width: 156px;
  gap: 6px;
  padding: 7px;
  border: 1px solid var(--line-strong);
  background: var(--surface);
}

.more-menu-panel button {
  width: 100%;
}

.badge {
  flex: 0 0 auto;
  padding: 4px 8px;
  border: 1px solid currentColor;
  border-radius: 2px;
  font-size: 12px;
  font-weight: 650;
  white-space: nowrap;
}

.badge.active {
  color: var(--primary);
  background: var(--primary-soft);
}

.badge.settling {
  color: var(--warning);
  background: var(--warning-soft);
}

.badge.settled {
  color: var(--info);
  background: #eaf1f5;
}

.badge.archived {
  color: var(--muted);
  background: var(--surface-muted);
}

.section-head,
.subsection-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 9px;
}

.section-head {
  min-height: 38px;
  padding-bottom: 9px;
  margin-bottom: 11px;
  border-bottom: 1px solid var(--line);
}

.section-head h2 {
  font-size: 17px;
  line-height: 1.3;
}

.section-hint {
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
}

.step {
  display: inline-grid;
  width: 23px;
  height: 23px;
  margin-right: 7px;
  place-items: center;
  border-radius: 0;
  background: var(--primary);
  color: #fff;
  font-size: 12px;
  font-weight: 700;
}

.card h3 {
  margin: 0 0 8px;
  color: var(--text);
  font-size: 15px;
}

.subsection + .subsection {
  padding-top: 13px;
  margin-top: 13px;
  border-top: 1px solid var(--line);
}

.row {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 9px;
}

.row input {
  flex: 1 1 160px;
}

.member-entry {
  flex-wrap: nowrap;
}

.member-entry input {
  min-width: 0;
}

.settlement-entry {
  display: grid;
  grid-template-columns: minmax(120px, 1fr) auto minmax(120px, 1fr) minmax(130px, 1fr) auto;
  align-items: end;
}

.settlement-entry .field + .field {
  margin-top: 0;
}

.settlement-entry select,
.settlement-entry input {
  width: 100%;
}

.settlement-entry .arrow {
  align-self: center;
}

.notice {
  position: sticky;
  top: 8px;
  z-index: 30;
  padding: 10px 12px;
  margin-bottom: var(--page-gap);
  border: 1px solid #c8a83c;
  border-radius: 0;
  background: var(--warning-soft);
  color: #5e4a0d;
  font-size: 14px;
}

.field-error {
  min-height: 0;
  color: var(--danger);
  font-size: 13px;
  font-weight: 600;
}

.field-error:not(:empty) {
  padding: 8px 10px;
  margin-top: 7px;
  border-left: 3px solid var(--danger);
  background: var(--danger-soft);
}

.form-help {
  margin-top: 7px;
  color: var(--muted);
  font-size: 13px;
}

.hidden {
  display: none !important;
}

.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;
}

.list {
  list-style: none;
}

.list li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-width: 0;
  min-height: 48px;
  padding: 8px 2px;
  border-bottom: 1px solid var(--line);
  font-size: 14px;
}

.list li:last-child {
  border-bottom: none;
}

.list li > * {
  min-width: 0;
}

.list .amount {
  color: var(--primary-strong);
  font-weight: 750;
  white-space: nowrap;
}

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

.empty-tip {
  padding: 10px 2px;
  color: var(--muted);
  font-size: 13px;
}

.member-tag {
  margin-left: 6px;
  color: var(--muted);
  font-size: 12px;
}

#inactiveMembers h3 {
  margin-top: 12px;
}

.expense-list {
  display: grid;
  gap: 9px;
}

.expense-list .ex-item {
  border: 1px solid var(--line);
  border-left: 4px solid var(--line-strong);
  border-radius: 0;
}

.ex-item {
  display: block !important;
  padding: 12px !important;
}

.ex-top {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 9px;
}

.ex-top > span:first-child {
  min-width: 0;
}

.ex-title {
  font-size: 16px;
  font-weight: 750;
  overflow-wrap: anywhere;
}

.ex-mode {
  display: inline-block;
  margin-left: 6px;
  padding: 2px 5px;
  border: 1px solid var(--line);
  border-radius: 0;
  background: var(--surface-muted);
  color: var(--muted);
  font-size: 11px;
}

.ex-amount {
  flex: 0 0 auto;
  color: var(--text);
  font-size: 17px;
  font-weight: 800;
  white-space: nowrap;
}

.ex-line {
  margin-top: 6px;
  color: var(--muted);
  font-size: 13px;
  overflow-wrap: anywhere;
}

.ex-status {
  margin-top: 7px;
  font-size: 13px;
  font-weight: 650;
}

.ex-status.ok,
.ed-status.ok,
.integrity-box.ok,
.pos {
  color: var(--primary);
}

.ex-status.err,
.ed-status.err,
.ed-error,
.integrity-box.err,
.neg {
  color: var(--danger);
}

.ex-actions {
  display: flex;
  justify-content: flex-end;
  gap: 7px;
  margin-top: 9px;
}

.ex-actions button {
  min-height: 44px;
  padding: 7px 12px;
  font-size: 14px;
}

.editor-shell {
  margin: 10px 0;
}

.editor {
  border: 1px solid var(--line-strong);
  border-radius: 0;
  background: var(--surface);
}

.editor-head {
  display: flex;
  min-height: 54px;
  padding: 8px 12px;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}

.editor-head h3 {
  margin: 0;
  font-size: 17px;
}

.editor-body {
  padding: 14px;
}

.form-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.7fr) minmax(130px, 1fr);
  gap: 11px;
}

.field {
  display: grid;
  min-width: 0;
  gap: 6px;
}

.field + .field {
  margin-top: 11px;
}

.form-grid .field + .field {
  margin-top: 0;
}

.field-label {
  color: var(--text);
  font-size: 13px;
  font-weight: 700;
}

.editor-section {
  padding-top: 15px;
  margin-top: 15px;
  border-top: 1px solid var(--line);
}

.editor-section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 9px;
}

.editor-section-head h4 {
  font-size: 15px;
}

.editor-section-head button {
  min-height: 44px;
  padding: 6px 10px;
  font-size: 13px;
}

.ed-row {
  display: grid;
  grid-template-columns: minmax(120px, 1fr) minmax(120px, 1fr) auto;
  margin-bottom: 8px;
}

.ed-row select,
.ed-row input {
  width: 100%;
}

.single-payment {
  display: grid;
  grid-template-columns: minmax(140px, 1fr) minmax(130px, 1fr) auto;
  align-items: end;
  gap: 8px;
}

.auto-payment {
  display: grid;
  min-height: var(--control-height);
  padding: 8px 11px;
  align-content: center;
  border: 1px solid var(--line);
  background: var(--surface-muted);
  color: var(--muted);
  font-size: 12px;
}

.auto-payment strong {
  color: var(--text);
  font-size: 16px;
}

.ed-status {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.ed-error {
  font-size: 13px;
  font-weight: 650;
}

.ed-error:not(:empty) {
  padding: 9px 11px;
  margin: 10px 0 0;
  border-left: 3px solid var(--danger);
  background: var(--danger-soft);
}

.check-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 2px 0;
}

.check {
  display: flex;
  min-height: 44px;
  padding: 8px 11px;
  align-items: center;
  gap: 7px;
  border: 1px solid var(--line-strong);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  font-size: 14px;
  touch-action: manipulation;
}

.check:has(input:checked) {
  border-color: var(--primary);
  background: var(--primary-soft);
  color: var(--primary-strong);
  font-weight: 700;
}

.check input {
  width: 18px;
  min-height: auto;
  height: 18px;
  accent-color: var(--primary);
}

.advanced-panel {
  padding: 12px;
  margin: 0 0 10px;
  border: 1px solid var(--line);
  background: var(--surface-muted);
}

.advanced-panel .field + .field {
  margin-top: 12px;
}

.editor-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 11px 14px;
  border-top: 1px solid var(--line);
  background: var(--surface);
}

#summaryGrid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 7px;
  margin-bottom: 10px;
}

.summary-item {
  min-width: 0;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 0;
  background: var(--surface-muted);
}

.summary-item.primary {
  border-color: var(--primary);
  background: var(--primary-soft);
}

.summary-item .label {
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
}

.summary-item .value {
  margin-top: 3px;
  color: var(--text);
  font-size: 18px;
  font-weight: 800;
  overflow-wrap: anywhere;
}

.integrity-box {
  padding: 9px 11px;
  border: 1px solid currentColor;
  border-radius: 0;
  font-size: 13px;
}

.integrity-box.ok {
  background: var(--primary-soft);
}

.integrity-box.err {
  background: var(--danger-soft);
}

.integrity-box li {
  margin-top: 4px;
  margin-left: 17px;
  list-style: disc;
}

.table-scroll {
  width: 100%;
  overflow-x: auto;
  border: 1px solid var(--line);
}

#balanceTable {
  width: 100%;
  min-width: 660px;
  border-collapse: collapse;
  font-size: 13px;
}

#balanceTable th,
#balanceTable td {
  padding: 9px 10px;
  border-bottom: 1px solid var(--line);
  text-align: right;
  white-space: nowrap;
}

#balanceTable th {
  background: var(--surface-muted);
  color: var(--muted);
  font-size: 12px;
  font-weight: 650;
}

#balanceTable td:first-child,
#balanceTable th:first-child {
  position: sticky;
  left: 0;
  z-index: 1;
  background: var(--surface);
  text-align: left;
}

#balanceTable th:first-child {
  background: var(--surface-muted);
}

.name-btn {
  min-height: 36px;
  padding: 5px 7px;
  border: none;
  background: none;
  color: var(--text);
  font-size: 13px;
  font-weight: 750;
}

.name-btn:hover {
  background: var(--primary-soft);
  color: var(--primary);
}

tr.row-inactive td {
  color: var(--muted);
}

tr.expand-row > td {
  background: var(--surface-muted);
}

.detail-box {
  padding: 3px 0;
  text-align: left;
  font-size: 13px;
}

.detail-line {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 7px 0;
  border-bottom: 1px solid var(--line);
}

.detail-line:last-child {
  border-bottom: none;
}

.detail-line .d-title {
  flex: 1;
  min-width: 100px;
  overflow-wrap: anywhere;
}

.balance-cards {
  display: none;
  gap: 9px;
}

.balance-card {
  border: 1px solid var(--line);
  border-left: 4px solid var(--line-strong);
  background: var(--surface);
}

.balance-card.is-receivable {
  border-left-color: var(--primary);
}

.balance-card.is-payable {
  border-left-color: var(--danger);
}

.balance-card-head {
  display: flex;
  min-height: 58px;
  padding: 10px 11px;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.balance-name {
  font-size: 16px;
  font-weight: 750;
}

.balance-result {
  text-align: right;
}

.balance-result span {
  display: block;
  color: var(--muted);
  font-size: 12px;
}

.balance-result strong {
  font-size: 19px;
}

.balance-facts {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  border-top: 1px solid var(--line);
  background: var(--surface-muted);
}

.balance-fact {
  padding: 8px 10px;
  border-right: 1px solid var(--line);
}

.balance-fact:last-child {
  border-right: none;
}

.balance-fact span {
  display: block;
  color: var(--muted);
  font-size: 11px;
}

.balance-fact strong {
  display: block;
  margin-top: 2px;
  font-size: 13px;
  overflow-wrap: anywhere;
}

.balance-card-actions {
  padding: 7px 10px;
  border-top: 1px solid var(--line);
}

.balance-card-actions button {
  width: 100%;
  border-color: transparent;
  background: var(--surface);
  color: var(--primary);
  font-size: 14px;
}

.balance-card-detail {
  padding: 7px 11px;
  border-top: 1px solid var(--line);
  background: var(--surface-muted);
}

.bottom-nav {
  display: none;
}

body.locked .app-shell input,
body.locked .app-shell select,
body.locked .app-shell button {
  pointer-events: none;
  opacity: 0.55;
}

body.locked #reopenBtn,
body.locked #printBtn,
body.locked #newBillBtn,
body.locked .bottom-nav button {
  pointer-events: auto;
  opacity: 1;
}

.print-report {
  display: none;
}

@media (max-width: 720px) {
  body {
    padding: 7px 7px calc(76px + env(safe-area-inset-bottom));
  }

  .top-card,
  .card {
    padding: 12px;
  }

  .section-hint {
    max-width: 145px;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .settlement-entry {
    grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  }

  .settlement-entry .settlement-amount,
  .settlement-entry button {
    grid-column: span 3;
  }

  .bottom-nav {
    position: fixed;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 35;
    display: grid;
    grid-template-columns: repeat(5, minmax(0, 1fr));
    padding: 5px 5px calc(5px + env(safe-area-inset-bottom));
    border-top: 1px solid var(--line-strong);
    background: var(--surface);
  }

  .bottom-nav a,
  .bottom-nav button {
    display: grid;
    min-width: 0;
    min-height: 48px;
    padding: 5px 2px;
    place-items: center;
    border: 0;
    border-right: 1px solid var(--line);
    border-radius: 0;
    background: var(--surface);
    color: var(--muted);
    font-size: 12px;
    font-weight: 700;
    text-decoration: none;
  }

  .bottom-nav > :last-child {
    border-right: none;
  }

  .bottom-nav button {
    background: var(--primary);
    color: #fff;
  }

  .balance-cards {
    display: grid;
  }

  .desktop-ledger {
    display: none;
  }

  #expenseEditor .editor-shell {
    position: fixed;
    inset: 0;
    z-index: 50;
    margin: 0;
    overflow-y: auto;
    background: var(--surface-muted);
    overscroll-behavior: contain;
  }

  #expenseEditor .editor {
    min-height: 100%;
    padding-bottom: calc(76px + env(safe-area-inset-bottom));
    border: 0;
  }

  .editor-head {
    position: sticky;
    top: 0;
    z-index: 2;
  }

  .editor-body {
    max-width: 640px;
    margin: 0 auto;
    padding: 14px 12px;
  }

  .editor-actions {
    position: fixed;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 52;
    padding: 9px 12px calc(9px + env(safe-area-inset-bottom));
  }

  .editor-actions button {
    flex: 1 1 0;
  }

  body.editor-open {
    overflow: hidden;
  }

  body.editor-open .bottom-nav {
    display: none;
  }
}

@media (max-width: 480px) {
  .header-top {
    align-items: flex-start;
  }

  .header-actions {
    justify-content: stretch;
  }

  .header-actions > button,
  .header-actions .more-menu,
  .header-actions .more-menu summary {
    flex: 1 1 auto;
  }

  .more-menu-panel {
    right: 0;
    width: min(190px, calc(100vw - 28px));
  }

  #summaryGrid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .form-grid,
  .single-payment {
    grid-template-columns: 1fr;
  }

  .form-grid .field + .field {
    margin-top: 0;
  }

  .ed-row {
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  }

  .ed-row .danger {
    grid-column: span 2;
  }

  .list li {
    flex-wrap: wrap;
  }

  .detail-line {
    align-items: flex-start;
    flex-wrap: wrap;
  }

  .detail-line .d-title {
    flex-basis: 100%;
  }
}

@media (max-width: 330px) {
  body {
    padding-right: 4px;
    padding-left: 4px;
  }

  .top-card,
  .card {
    padding: 10px;
  }

  .header-top {
    flex-wrap: wrap;
  }

  .header-top .badge {
    order: -1;
  }

  .bottom-nav a,
  .bottom-nav button {
    font-size: 11px;
  }
}

@media print {
  @page {
    size: A4 portrait;
    margin: 12mm;
  }

  html,
  body {
    min-width: 0;
    padding: 0;
    background: #fff;
    color: #000;
    font-family: SimSun, "宋体", serif;
    font-size: 9pt;
    line-height: 1.35;
  }

  .app-shell {
    display: none !important;
  }

  .print-report {
    display: block !important;
  }

  .report-header {
    padding-bottom: 4mm;
    margin-bottom: 4mm;
    border-bottom: 2px solid #000;
    text-align: center;
  }

  .report-header h1 {
    margin: 0 0 1.5mm;
    font-size: 18pt;
    letter-spacing: 3pt;
  }

  .report-meta {
    display: flex;
    justify-content: center;
    gap: 6mm;
    font-size: 8pt;
  }

  .report-description {
    margin-top: 2mm;
    font-size: 8.5pt;
  }

  .report-section {
    margin-top: 4mm;
  }

  .report-section h2 {
    padding-bottom: 1mm;
    margin-bottom: 2mm;
    border-bottom: 1px solid #000;
    font-size: 11pt;
  }

  .report-section h3 {
    margin: 0;
    font-size: 9.5pt;
  }

  .report-table {
    width: 100%;
    border-collapse: collapse;
    table-layout: auto;
  }

  .report-table th,
  .report-table td {
    padding: 1.2mm 1.5mm;
    border: 1px solid #000;
    color: #000;
    font-size: 8pt;
    text-align: left;
    vertical-align: top;
  }

  .report-table th {
    background: #fff;
    font-weight: bold;
  }

  .report-table .number {
    text-align: right;
    white-space: nowrap;
  }

  .report-summary th {
    width: 16.66%;
  }

  .report-expense {
    padding: 2mm;
    margin-top: 2mm;
    border: 1px solid #000;
    break-inside: avoid;
    page-break-inside: avoid;
  }

  .report-expense-head {
    display: flex;
    justify-content: space-between;
    gap: 4mm;
    padding-bottom: 1.5mm;
    margin-bottom: 1.5mm;
    border-bottom: 1px dotted #000;
  }

  .report-expense-lines {
    display: grid;
    gap: 1mm;
    font-size: 8pt;
  }

  .report-invalid {
    font-weight: bold;
  }

  .report-note {
    margin-top: 2mm;
    font-size: 7.5pt;
  }

  .report-signatures {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16mm;
    margin-top: 10mm;
  }

  .report-signature {
    padding-top: 8mm;
    border-bottom: 1px solid #000;
    font-size: 8pt;
  }
}
