:root {
  --ink: #17201d;
  --ink-soft: #5f6b66;
  --paper: #f4f5f1;
  --surface: #ffffff;
  --line: #dfe3dd;
  --line-strong: #c9cfc8;
  --sidebar: #19231f;
  --sidebar-soft: #26332d;
  --green: #166447;
  --green-soft: #e2f0e8;
  --amber: #a55a16;
  --amber-soft: #f7eadb;
  --red: #a53a32;
  --red-soft: #f8e6e3;
  --blue: #236b83;
  --blue-soft: #e0eef2;
  --shadow: 0 18px 55px rgba(23, 32, 29, 0.16);
  font-family: "Microsoft YaHei UI", "PingFang SC", system-ui, -apple-system, sans-serif;
}

* { box-sizing: border-box; }

html { background: var(--paper); }

body {
  margin: 0;
  min-width: 320px;
  min-height: 100vh;
  color: var(--ink);
  background: var(--paper);
}

button,
input,
select,
textarea { font: inherit; letter-spacing: 0; }

button { cursor: pointer; }

.is-hidden { display: none !important; }

.login-screen {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    linear-gradient(90deg, rgba(255,255,255,.035) 1px, transparent 1px),
    linear-gradient(rgba(255,255,255,.035) 1px, transparent 1px),
    var(--sidebar);
  background-size: 40px 40px;
}

.login-panel {
  width: min(420px, 100%);
  padding: 42px;
  background: var(--surface);
  border: 1px solid rgba(255,255,255,.14);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.login-panel .brand-mark { margin-bottom: 28px; }
.login-panel h1 { margin: 4px 0 12px; font-size: 30px; letter-spacing: 0; }
.login-copy { margin: 0 0 30px; color: var(--ink-soft); line-height: 1.7; }
.login-footnote { margin: 20px 0 0; color: #87918c; font-size: 12px; text-align: center; }

.app-shell {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 232px minmax(0, 1fr);
}

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  padding: 24px 16px 18px;
  display: flex;
  flex-direction: column;
  color: #f7faf8;
  background: var(--sidebar);
  border-right: 1px solid #314039;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 10px 28px;
}

.brand-mark {
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  flex: 0 0 40px;
  color: #fff;
  background: var(--green);
  border: 1px solid rgba(255,255,255,.16);
  border-radius: 6px;
  font-weight: 800;
}

.brand strong,
.brand span { display: block; }
.brand strong { font-size: 15px; }
.brand span { margin-top: 3px; color: #93a29b; font: 10px/1.2 ui-monospace, monospace; text-transform: uppercase; }

.nav { display: grid; gap: 5px; }

.nav-item {
  min-height: 44px;
  display: grid;
  grid-template-columns: 34px 1fr;
  align-items: center;
  width: 100%;
  padding: 0 12px;
  color: #aab7b1;
  text-align: left;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 5px;
}

.nav-item:hover { color: #fff; background: var(--sidebar-soft); }
.nav-item.is-active { color: #fff; background: #2b3b34; border-color: #3a4a43; }
.nav-index { color: #6f8178; font: 10px/1 ui-monospace, monospace; }
.nav-item.is-active .nav-index { color: #87c5a9; }

.sidebar-footer {
  margin-top: auto;
  padding: 16px 10px 0;
  display: flex;
  align-items: center;
  gap: 10px;
  border-top: 1px solid #314039;
}

.sidebar-footer strong,
.sidebar-footer span { display: block; }
.sidebar-footer strong { font-size: 11px; font-weight: 600; }
.sidebar-footer span { margin-top: 3px; color: #819088; font-size: 10px; }
.status-dot { width: 8px; height: 8px; flex: 0 0 8px; border-radius: 50%; background: #5dc58f; box-shadow: 0 0 0 4px rgba(93,197,143,.12); }

.workspace { min-width: 0; }

.topbar {
  min-height: 92px;
  padding: 20px clamp(20px, 3vw, 42px);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  background: rgba(244,245,241,.95);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 10;
}

.eyebrow {
  margin: 0;
  color: #78837e;
  font: 10px/1.2 ui-monospace, "SFMono-Regular", Consolas, monospace;
}

.topbar h2 { margin: 5px 0 0; font-size: 22px; letter-spacing: 0; }
.topbar-actions { display: flex; align-items: center; gap: 8px; }

.date-chip {
  height: 36px;
  display: inline-flex;
  align-items: center;
  padding: 0 12px;
  color: var(--ink-soft);
  border-left: 1px solid var(--line-strong);
  font-size: 12px;
}

.button,
.icon-button {
  min-height: 38px;
  border: 1px solid transparent;
  border-radius: 5px;
  font-weight: 650;
  transition: background .16s ease, border-color .16s ease, transform .16s ease;
}

.button { padding: 0 15px; font-size: 13px; }
.button:hover, .icon-button:hover { transform: translateY(-1px); }
.button-primary { color: #fff; background: var(--green); border-color: var(--green); }
.button-primary:hover { background: #0f573d; }
.button-secondary { color: var(--ink); background: var(--surface); border-color: var(--line-strong); }
.button-secondary:hover { border-color: #99a49f; }
.button-danger { color: var(--red); background: var(--red-soft); border-color: #ecc7c3; }
.button-block { width: 100%; }
.icon-button { width: 38px; padding: 0; display: grid; place-items: center; color: var(--ink-soft); background: transparent; border-color: var(--line-strong); font-size: 18px; }

main { padding: 30px clamp(20px, 3vw, 42px) 48px; }
.page-stack { display: grid; gap: 24px; }

.section-header {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 20px;
}

.section-header h3 { margin: 4px 0 0; font-size: 16px; }
.section-header p { margin: 5px 0 0; color: var(--ink-soft); font-size: 12px; }
.section-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.section-header.compact { margin-top: 4px; }

.metric-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  border: 1px solid var(--line);
  background: var(--surface);
}

.metric {
  min-width: 0;
  min-height: 138px;
  padding: 22px;
  border-right: 1px solid var(--line);
}

.metric:last-child { border-right: 0; }
.metric-label { color: var(--ink-soft); font-size: 12px; }
.metric-value { margin-top: 16px; font: 700 clamp(23px, 2.2vw, 32px)/1.1 ui-monospace, "SFMono-Regular", Consolas, monospace; overflow-wrap: anywhere; }
.metric-foot { margin-top: 10px; color: #79847f; font-size: 11px; }
.metric.is-positive .metric-value { color: var(--green); }
.metric.is-warning .metric-value { color: var(--amber); }

.content-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.45fr) minmax(280px, .75fr);
  gap: 22px;
}

.panel {
  min-width: 0;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 6px;
}

.panel-header {
  min-height: 62px;
  padding: 16px 18px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  border-bottom: 1px solid var(--line);
}

.panel-header h3 { margin: 0; font-size: 14px; }
.panel-header span, .panel-header p { margin: 4px 0 0; color: var(--ink-soft); font-size: 11px; }
.text-button { padding: 4px; color: var(--green); background: none; border: 0; font-size: 12px; font-weight: 700; }
.text-button.danger { color: var(--red); }
.row-actions { display: flex; align-items: center; flex-wrap: wrap; gap: 5px; min-width: 150px; }
tr.is-inactive { opacity: .62; }

.account-list { display: grid; }
.account-row {
  display: grid;
  grid-template-columns: minmax(150px, 1fr) minmax(120px, .7fr) 112px;
  align-items: center;
  gap: 18px;
  min-height: 70px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--line);
}
.account-row:last-child { border-bottom: 0; }
.account-name { display: flex; align-items: center; gap: 10px; min-width: 0; }
.account-name strong { display: block; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; }
.account-name span { display: block; margin-top: 3px; color: var(--ink-soft); font-size: 10px; }
.account-icon { width: 30px; height: 30px; flex: 0 0 30px; display: grid; place-items: center; color: var(--green); background: var(--green-soft); border-radius: 5px; font: 700 11px/1 ui-monospace, monospace; }
.balance-bar { height: 5px; overflow: hidden; background: #edf0ec; border-radius: 2px; }
.balance-bar span { display: block; height: 100%; background: var(--green); }
.balance-bar span.negative { background: var(--red); }
.account-balance { text-align: right; font: 700 14px/1.2 ui-monospace, monospace; }
.account-balance.negative { color: var(--red); }

.chart { height: 242px; padding: 22px 18px 16px; display: grid; grid-template-columns: repeat(7, 1fr); align-items: end; gap: 12px; }
.chart-day { height: 100%; display: grid; grid-template-rows: 1fr 22px; gap: 8px; align-items: end; }
.chart-bars { height: 100%; display: flex; align-items: end; justify-content: center; gap: 4px; border-bottom: 1px solid var(--line); }
.chart-bar { width: min(13px, 40%); min-height: 2px; border-radius: 2px 2px 0 0; }
.chart-bar.income { background: var(--green); }
.chart-bar.outflow { background: var(--amber); }
.chart-label { color: #818b86; text-align: center; font: 10px/1 ui-monospace, monospace; }
.chart-legend { display: flex; gap: 14px; color: var(--ink-soft); font-size: 10px; }
.legend-dot { width: 7px; height: 7px; display: inline-block; margin-right: 5px; border-radius: 1px; }
.legend-dot.income { background: var(--green); }
.legend-dot.outflow { background: var(--amber); }

.segmented-control {
  display: inline-flex;
  padding: 3px;
  background: #edf0ec;
  border: 1px solid var(--line);
  border-radius: 5px;
}
.segment-button {
  min-width: 56px;
  height: 30px;
  padding: 0 10px;
  color: var(--ink-soft);
  background: transparent;
  border: 0;
  border-radius: 3px;
  font-size: 11px;
  font-weight: 700;
}
.segment-button.is-active { color: #fff; background: var(--green); }
.cashflow-summary {
  min-height: 78px;
  padding: 14px 18px;
  display: grid;
  grid-template-columns: repeat(3, minmax(120px, 1fr)) auto;
  align-items: center;
  gap: 16px;
  border-bottom: 1px solid var(--line);
}
.cashflow-summary > div > span:not(.legend-dot) { display: block; color: var(--ink-soft); font-size: 10px; }
.cashflow-summary strong { display: block; margin-top: 6px; font: 700 18px/1 ui-monospace, monospace; }
.cashflow-summary strong.positive { color: var(--green); }
.cashflow-summary strong.negative { color: var(--red); }
.cashflow-chart-wrap { overflow-x: auto; }
.cashflow-chart {
  min-width: 680px;
  height: 278px;
  padding: 22px 18px 16px;
  display: flex;
  align-items: stretch;
  gap: 8px;
}
.cashflow-period {
  min-width: 38px;
  flex: 1 0 38px;
  display: grid;
  grid-template-rows: minmax(0, 1fr) 24px;
  gap: 8px;
}
.cashflow-bars {
  min-height: 0;
  display: flex;
  align-items: end;
  justify-content: center;
  gap: 4px;
  border-bottom: 1px solid var(--line);
}
.cashflow-bar { width: min(15px, 42%); min-height: 2px; border-radius: 2px 2px 0 0; }
.cashflow-bar.income { background: var(--green); }
.cashflow-bar.outflow { background: var(--amber); }
.cashflow-label { color: #74807a; text-align: center; font: 10px/1 ui-monospace, monospace; white-space: nowrap; }

.notice {
  padding: 14px 16px;
  display: flex;
  align-items: flex-start;
  gap: 12px;
  color: #694017;
  background: var(--amber-soft);
  border: 1px solid #ecd2b3;
  border-radius: 5px;
  font-size: 12px;
  line-height: 1.6;
}
.notice strong { display: block; color: var(--amber); }
.notice-mark { font-weight: 800; }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { min-width: 100px; padding: 13px 16px; text-align: left; border-bottom: 1px solid var(--line); font-size: 12px; }
th { color: #74807a; background: #fafbf9; font-size: 10px; font-weight: 700; text-transform: uppercase; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: #fafbf9; }
tbody tr.is-voided { opacity: .62; background: #f4f4f2; }
tbody tr.is-voided .amount { text-decoration: line-through; }
.accounts-table tr.is-admin-account { background: #fbf8f3; }
.amount { font: 700 12px/1.2 ui-monospace, monospace; white-space: nowrap; }
.amount.positive { color: var(--green); }
.amount.negative { color: var(--red); }
.muted { color: var(--ink-soft); }

.badge { display: inline-flex; align-items: center; min-height: 23px; padding: 0 8px; border-radius: 3px; font-size: 10px; font-weight: 700; white-space: nowrap; }
.badge.green { color: var(--green); background: var(--green-soft); }
.badge.amber { color: var(--amber); background: var(--amber-soft); }
.badge.blue { color: var(--blue); background: var(--blue-soft); }
.badge.gray { color: #5f6b66; background: #e9ece8; }
.badge.red { color: var(--red); background: var(--red-soft); }

.member-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 14px; }
.member-card { padding: 18px; background: var(--surface); border: 1px solid var(--line); border-radius: 6px; }
.member-card-top { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.member-card h3 { margin: 0; font-size: 14px; }
.member-balance-label { display: block; margin-top: 24px; color: var(--ink-soft); font-size: 10px; }
.member-balance { margin: 8px 0 6px; font: 700 25px/1 ui-monospace, monospace; }
.member-balance.positive { color: var(--green); }
.member-balance.negative { color: var(--red); }
.settlement-caption { min-height: 18px; margin-bottom: 14px; color: var(--ink-soft); font-size: 10px; }
.member-meta { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; padding-top: 14px; border-top: 1px solid var(--line); }
.member-meta.three { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.member-meta span { display: block; color: var(--ink-soft); font-size: 10px; }
.member-meta strong { display: block; margin-top: 5px; font: 650 12px/1.2 ui-monospace, monospace; }

.empty-state { padding: 44px 20px; color: var(--ink-soft); text-align: center; }
.empty-state.compact { padding: 18px 12px; }
.loading-state { min-height: 50vh; display: flex; align-items: center; justify-content: center; gap: 10px; color: var(--ink-soft); font-size: 12px; }
.spinner { width: 16px; height: 16px; border: 2px solid var(--line-strong); border-top-color: var(--green); border-radius: 50%; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(15, 23, 20, .56);
}

.modal {
  width: min(620px, 100%);
  max-height: min(790px, calc(100vh - 40px));
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.modal-header { min-height: 78px; padding: 18px 22px; display: flex; align-items: center; justify-content: space-between; border-bottom: 1px solid var(--line); }
.modal-header h3 { margin: 5px 0 0; font-size: 18px; }
.entry-form { padding: 22px; display: grid; gap: 17px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.field { display: grid; gap: 7px; }
.field > span { color: var(--ink-soft); font-size: 11px; font-weight: 650; }
.field input, .field select, .field textarea {
  width: 100%;
  min-height: 42px;
  padding: 9px 11px;
  color: var(--ink);
  background: #fff;
  border: 1px solid var(--line-strong);
  border-radius: 4px;
  outline: none;
}
.field textarea { min-height: 82px; resize: vertical; }
.field input:focus, .field select:focus, .field textarea:focus { border-color: var(--green); box-shadow: 0 0 0 3px rgba(22,100,71,.1); }
.field-help { margin: -4px 0 0; color: var(--ink-soft); font-size: 10px; }
.form-preview { min-height: 52px; padding: 12px 14px; display: flex; align-items: center; justify-content: space-between; gap: 16px; color: var(--green); background: var(--green-soft); border-radius: 4px; font-size: 11px; }
.form-preview strong { font: 700 18px/1 ui-monospace, monospace; }
.form-error { min-height: 18px; margin: 0; color: var(--red); font-size: 11px; }
.form-actions { display: flex; justify-content: flex-end; gap: 8px; padding-top: 4px; }
.note-list { max-height: 230px; overflow-y: auto; border: 1px solid var(--line); border-radius: 4px; }
.note-item { padding: 12px 14px; border-bottom: 1px solid var(--line); }
.note-item:last-child { border-bottom: 0; }
.note-item p { margin: 0; font-size: 12px; line-height: 1.65; white-space: pre-wrap; overflow-wrap: anywhere; }
.note-item span { display: block; margin-top: 6px; color: var(--ink-soft); font-size: 10px; }

.toast-region { position: fixed; right: 20px; bottom: 20px; z-index: 100; display: grid; gap: 8px; }
.toast { max-width: 340px; padding: 12px 14px; color: #fff; background: var(--sidebar); border: 1px solid #35453e; border-radius: 5px; box-shadow: 0 12px 35px rgba(23,32,29,.22); font-size: 12px; animation: toast-in .18s ease-out; }
.toast.error { background: #6f2a26; border-color: #914039; }
@keyframes toast-in { from { opacity: 0; transform: translateY(7px); } }

@media (max-width: 1080px) {
  .metric-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .metric:nth-child(2) { border-right: 0; }
  .metric:nth-child(-n+2) { border-bottom: 1px solid var(--line); }
  .content-grid { grid-template-columns: 1fr; }
  .member-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 780px) {
  .app-shell { display: block; }
  .sidebar { position: static; width: 100%; height: auto; padding: 14px; }
  .brand { padding: 0 4px 14px; }
  .nav { display: flex; overflow-x: auto; padding-bottom: 4px; }
  .nav-item { flex: 0 0 auto; width: auto; grid-template-columns: 1fr; min-height: 38px; padding: 0 12px; }
  .nav-index, .sidebar-footer { display: none; }
  .topbar { position: static; min-height: auto; padding: 16px 18px; align-items: flex-start; }
  .topbar-actions { justify-content: flex-end; flex-wrap: wrap; }
  .date-chip { display: none; }
  .topbar .button-secondary { display: none; }
  main { padding: 20px 16px 36px; }
  .member-grid { grid-template-columns: 1fr; }
  .member-meta.three { grid-template-columns: repeat(3, minmax(90px, 1fr)); overflow-x: auto; }
  .cashflow-summary { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .cashflow-summary .chart-legend { grid-column: 1 / -1; justify-self: start; }
  .accounts-table-panel { border: 0; background: transparent; }
  .accounts-table-wrap { overflow: visible; }
  .accounts-table thead { display: none; }
  .accounts-table,
  .accounts-table tbody,
  .accounts-table tr,
  .accounts-table td { display: block; width: 100%; }
  .accounts-table tbody { display: grid; gap: 12px; }
  .accounts-table tr {
    padding: 13px 15px;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 6px;
  }
  .accounts-table td {
    min-width: 0;
    padding: 7px 0;
    display: grid;
    grid-template-columns: 78px minmax(0, 1fr);
    align-items: start;
    gap: 10px;
    border: 0;
    overflow-wrap: anywhere;
  }
  .accounts-table td::before {
    content: attr(data-label);
    color: #74807a;
    font-size: 10px;
    font-weight: 700;
  }
  .accounts-table td:first-child { padding-top: 0; font-size: 14px; }
  .accounts-table td:last-child { padding-bottom: 0; }
  .accounts-table .row-actions { min-width: 0; }
  .ledger-table-wrap { overflow: visible; }
  .ledger-table,
  .ledger-table tbody,
  .ledger-table tr,
  .ledger-table td { display: block; width: 100%; }
  .ledger-table thead { display: none; }
  .ledger-table tbody { display: grid; gap: 12px; }
  .ledger-table tr {
    padding: 13px 15px;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 6px;
  }
  .ledger-table td {
    min-width: 0;
    padding: 6px 0;
    display: grid;
    grid-template-columns: 78px minmax(0, 1fr);
    align-items: start;
    gap: 10px;
    border: 0;
    overflow-wrap: anywhere;
    white-space: normal;
  }
  .ledger-table td::before {
    content: attr(data-label);
    color: #74807a;
    font-size: 10px;
    font-weight: 700;
  }
  .ledger-table td:first-child { padding-top: 0; }
  .ledger-table td:last-child { padding-bottom: 0; }
  .ledger-table .row-actions { min-width: 0; }
}

@media (max-width: 560px) {
  .login-panel { padding: 28px 22px; }
  .topbar h2 { font-size: 19px; }
  .topbar-actions .button { min-width: 42px; padding: 0 10px; }
  .metric-grid { grid-template-columns: 1fr; }
  .metric { min-height: 118px; border-right: 0; border-bottom: 1px solid var(--line); }
  .metric:last-child { border-bottom: 0; }
  .metric-value { font-size: 25px; }
  .account-row { grid-template-columns: minmax(140px, 1fr) 92px; gap: 12px; }
  .account-row .balance-bar { display: none; }
  .section-header { align-items: flex-start; flex-direction: column; }
  .cashflow-header { align-items: flex-start; flex-direction: column; }
  .cashflow-summary { grid-template-columns: 1fr 1fr; }
  .cashflow-summary > div:nth-child(3) { grid-column: 1 / -1; }
  .cashflow-summary strong { font-size: 16px; }
  .form-grid { grid-template-columns: 1fr; }
  .modal-backdrop { padding: 0; place-items: end center; }
  .modal { width: 100%; max-height: 92vh; border-radius: 8px 8px 0 0; }
  .chart { gap: 6px; }
}
