:root {
  --bg: #f1f5f9;
  --card: #ffffff;
  --ink: #1e293b;
  --muted: #64748b;
  --line: #e2e8f0;
  --brand: #2563eb;
  --brand-d: #1d4ed8;
  --green: #16a34a;
  --green-bg: #dcfce7;
  --red: #dc2626;
  --red-bg: #fee2e2;
  --amber: #d97706;
  --amber-bg: #fef3c7;
  --shadow: 0 6px 20px rgba(15, 23, 42, .08);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0; padding: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", Roboto, Helvetica, Arial, sans-serif;
  font-size: 15px;
}

.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 16px;
  background: linear-gradient(120deg, var(--brand), var(--brand-d));
  color: #fff;
  box-shadow: var(--shadow);
  position: sticky; top: 0; z-index: 20;
}
.brand { font-weight: 700; font-size: 17px; letter-spacing: .5px; white-space: nowrap; }
.sync-status {
  margin-left: auto; font-size: 12px; opacity: .95;
  background: rgba(255,255,255,.18); padding: 4px 10px; border-radius: 999px;
  white-space: nowrap; max-width: 46vw; overflow: hidden; text-overflow: ellipsis;
}
.sync-status.err { background: var(--red-bg); color: var(--red); }
.sync-status.ok { background: var(--green-bg); color: #065f46; }
.user-area { display: flex; gap: 8px; }
.tbtn {
  border: none; cursor: pointer; border-radius: 999px;
  padding: 6px 14px; font-size: 13px; font-weight: 600;
  background: rgba(255,255,255,.22); color: #fff;
  white-space: nowrap;
}
.tbtn:hover { background: rgba(255,255,255,.34); }
.tbtn.primary { background: #fff; color: var(--brand-d); }
.tbtn.danger { background: var(--red-bg); color: var(--red); }

.tabs {
  display: flex; gap: 6px; padding: 8px 12px 0;
  background: #fff; border-bottom: 1px solid var(--line);
  position: sticky; top: 52px; z-index: 19;
}
.tab {
  border: none; background: none; cursor: pointer;
  padding: 10px 16px; font-size: 15px; font-weight: 600; color: var(--muted);
  border-bottom: 3px solid transparent; border-radius: 8px 8px 0 0;
  white-space: nowrap;
}
.tab.active { color: var(--brand-d); border-bottom-color: var(--brand); }

main { padding: 14px; max-width: 980px; margin: 0 auto; }
.panel { display: none; }
.panel.active { display: block; }

/* ---------- Calculator ---------- */
.calc-wrap { display: grid; grid-template-columns: 1fr 300px; gap: 14px; }
@media (max-width: 880px) { .calc-wrap { grid-template-columns: 1fr; } }

.calc-main { background: var(--card); border-radius: 16px; padding: 14px; box-shadow: var(--shadow); min-width: 0; }
.calc-display {
  background: #0f172a; color: #e2e8f0; border-radius: 12px;
  padding: 12px 14px; margin-bottom: 12px; text-align: right;
  min-height: 92px; display: flex; flex-direction: column; justify-content: flex-end;
}
.calc-expr { font-size: 16px; opacity: .8; word-break: break-all; min-height: 22px; }
.calc-result { font-size: 34px; font-weight: 700; color: #fff; word-break: break-all; }
.calc-keys { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.ck {
  border: none; cursor: pointer; border-radius: 12px; padding: 16px 0;
  font-size: 19px; font-weight: 600; background: #f1f5f9; color: var(--ink);
  transition: transform .05s, background .15s;
  white-space: nowrap; min-width: 0;
}
.ck:active { transform: scale(.96); }
.ck.fn { background: #e0e7ff; color: var(--brand-d); }
.ck.op { background: #dbeafe; color: var(--brand-d); }
.ck.eq { background: var(--brand); color: #fff; grid-column: span 2; }
.ck.zero { grid-column: span 2; }
.ck.danger { background: var(--red-bg); color: var(--red); }

.calc-side { background: var(--card); border-radius: 16px; padding: 12px; box-shadow: var(--shadow); display: flex; flex-direction: column; min-width: 0; }
/* 标题与按钮一律不折行：放不下时整块换到下一行 */
.side-head { display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }
.side-head h3 { margin: 0; font-size: 14px; color: var(--muted); white-space: nowrap; }
.mini-btns { display: flex; gap: 5px; flex-wrap: wrap; justify-content: flex-end; flex: 1 1 auto; }
.mini {
  border: 1px solid var(--line); background: #f8fafc; cursor: pointer;
  border-radius: 8px; padding: 5px 8px; font-size: 12px; color: var(--muted); font-weight: 600;
  white-space: nowrap; flex: 0 0 auto;
}
.mini:disabled { opacity: .4; cursor: not-allowed; }
.mini.primary { background: var(--brand); color: #fff; border-color: var(--brand); }
.history-list { list-style: none; margin: 0; padding: 0; overflow: auto; flex: 1; max-height: 360px; }
.history-list li {
  padding: 8px 10px; border-radius: 8px; margin-bottom: 6px; cursor: pointer;
  background: #f8fafc; border: 1px solid var(--line);
}
.history-list li:hover { background: #eef2ff; }
.history-list .he { font-size: 12px; color: var(--muted); word-break: break-all; }
.history-list .hv { font-size: 17px; font-weight: 700; }
.empty { color: var(--muted); text-align: center; padding: 24px 0; font-size: 13px; }

/* ---------- Notepad ---------- */
.note-wrap { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
@media (max-width: 720px) { .note-wrap { grid-template-columns: 1fr; } }
.note-box, .note-res {
  background: var(--card); border-radius: 16px; padding: 14px; box-shadow: var(--shadow); min-width: 0;
}
.note-box textarea {
  width: 100%; height: 360px; border: none; resize: vertical; outline: none;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; font-size: 15px;
  line-height: 1.7; color: var(--ink); background: #f8fafc; border-radius: 10px; padding: 10px;
}
.note-res h3 { margin: 0 0 8px; font-size: 14px; color: var(--muted); }
.res-list { list-style: none; margin: 0; padding: 0; max-height: 300px; overflow: auto; }
.res-list li { padding: 6px 8px; border-bottom: 1px dashed var(--line); font-size: 14px; display: flex; justify-content: space-between; gap: 8px; min-width: 0; }
.res-list .rt { color: var(--muted); word-break: break-all; min-width: 0; }
.res-list .rv { font-weight: 700; white-space: nowrap; flex: 0 0 auto; }
.res-list .rv.var { color: var(--brand-d); }
.res-list .rv.ignore { color: #94a3b8; font-weight: 400; }
.res-total { margin-top: 10px; padding-top: 10px; border-top: 2px solid var(--line); font-size: 18px; font-weight: 800; text-align: right; }
.res-total span { color: var(--brand-d); }

/* ---------- Ledger ---------- */
.ledger-bar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 12px; }
.ledger-bar .seg { display: inline-flex; background: #fff; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.ledger-bar .seg button { border: none; background: none; padding: 7px 12px; cursor: pointer; font-size: 13px; color: var(--muted); font-weight: 600; white-space: nowrap; }
.ledger-bar .seg button.on { background: var(--brand); color: #fff; }
.lg-type { display: inline-flex; gap: 0; }
.lg-type button { border: 1px solid var(--line); background: #fff; padding: 7px 12px; cursor: pointer; font-size: 13px; font-weight: 600; color: var(--muted); white-space: nowrap; }
.lg-type button:first-child { border-radius: 10px 0 0 10px; }
.lg-type button:last-child { border-radius: 0 10px 10px 0; border-left: none; }
.lg-type button.on.exp { background: var(--red); color: #fff; border-color: var(--red); }
.lg-type button.on.inc { background: var(--green); color: #fff; border-color: var(--green); }

.cust-list { display: block; }
.cust-group { margin-bottom: 14px; }
.cust-group h4 { margin: 2px 0 9px; font-size: 14px; font-weight: 800; }
.cust-group h4.exp { color: var(--red); }
.cust-group h4.inc { color: var(--green); }
.cust-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(300px, 100%), 1fr)); gap: 12px; }
.cust {
  background: var(--card); border-radius: 14px; padding: 12px; box-shadow: var(--shadow);
  border-left: 5px solid var(--line);
  min-width: 0; overflow: hidden;   /* 兜底：内容绝不越出卡片 */
}
.cust.exp { border-left-color: var(--red); }
.cust.inc { border-left-color: var(--green); }
.cust.settled { opacity: .82; }
.cust-top { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; flex-wrap: wrap; }
.cust-name { font-weight: 700; font-size: 16px; flex: 1 1 auto; min-width: 0; word-break: break-all; }
.badge { font-size: 11px; padding: 2px 8px; border-radius: 999px; font-weight: 700; white-space: nowrap; flex: 0 0 auto; }
.badge.exp { background: var(--red-bg); color: var(--red); }
.badge.inc { background: var(--green-bg); color: #065f46; }
.badge.settled { background: var(--amber-bg); color: var(--amber); }
.badge.recv { background: var(--green-bg); color: #065f46; }
.cust-total { font-size: 22px; font-weight: 800; margin: 4px 0 8px; }
.cust-total.exp { color: var(--red); }
.cust-total.inc { color: var(--green); }
.entry { display: flex; align-items: center; justify-content: space-between; gap: 6px; padding: 6px 8px; background: #f8fafc; border-radius: 8px; margin-bottom: 5px; font-size: 13px; min-width: 0; }
.entry .e-note { color: var(--muted); word-break: break-all; flex: 1 1 auto; min-width: 0; }
.entry .e-amt { font-weight: 700; white-space: nowrap; flex: 0 0 auto; }
.entry .e-time { color: #94a3b8; font-size: 11px; white-space: nowrap; flex: 0 0 auto; }
.entry .e-del { color: var(--red); cursor: pointer; border: none; background: none; font-size: 13px; flex: 0 0 auto; padding: 0 2px; }

/* 金额 / 备注 / 添加 —— 三者必须同处一行且不越出卡片 */
.add-row { display: flex; align-items: center; gap: 6px; margin-top: 8px; min-width: 0; }
.add-row input {
  border: 1px solid var(--line); border-radius: 8px; padding: 8px; font-size: 13px; outline: none;
  min-width: 0; flex: 1 1 0; width: 100%;
}
.add-row input:focus { border-color: var(--brand); }
.add-row input.amt { flex: 0 0 82px; width: 82px; max-width: 82px; }
.add-row button {
  border: none; background: var(--brand); color: #fff; border-radius: 8px;
  padding: 8px 12px; cursor: pointer; font-weight: 600; font-size: 13px;
  white-space: nowrap; flex: 0 0 auto;
}
.add-row button:disabled { opacity: .45; cursor: not-allowed; }
.cust-actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.cact { border: 1px solid var(--line); background: #fff; border-radius: 8px; padding: 6px 10px; cursor: pointer; font-size: 12px; font-weight: 600; color: var(--muted); white-space: nowrap; }
.cact.settle { background: var(--amber-bg); color: var(--amber); border-color: var(--amber-bg); }
.cact.recv { background: var(--green-bg); color: #065f46; border-color: var(--green-bg); }
.cact.unlock { background: #e0e7ff; color: var(--brand-d); border-color: #e0e7ff; }
.cact.del { background: var(--red-bg); color: var(--red); border-color: var(--red-bg); }

/* ---------- Modal ---------- */
.modal { position: fixed; inset: 0; background: rgba(15,23,42,.5); display: none; align-items: center; justify-content: center; z-index: 50; padding: 16px; }
.modal.show { display: flex; }
.modal-card { background: #fff; border-radius: 16px; padding: 20px; width: 100%; max-width: 380px; box-shadow: var(--shadow); max-height: 90vh; overflow: auto; }
.modal-card h2 { margin: 0 0 14px; font-size: 18px; }
.field { margin-bottom: 12px; }
.field label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 5px; font-weight: 600; }
.field input, .field select, .field textarea {
  width: 100%; border: 1px solid var(--line); border-radius: 10px; padding: 10px; font-size: 15px; outline: none;
}
.field input:focus, .field select:focus, .field textarea:focus { border-color: var(--brand); }
.modal-actions { display: flex; gap: 10px; margin-top: 8px; }
.modal-actions button { flex: 1; border: none; border-radius: 10px; padding: 11px; font-size: 15px; font-weight: 700; cursor: pointer; white-space: nowrap; }
.btn-primary { background: var(--brand); color: #fff; }
.btn-ghost { background: #f1f5f9; color: var(--muted); }
.tabs-auth { display: flex; gap: 0; margin-bottom: 14px; }
.tabs-auth button { flex: 1; border: none; background: #f1f5f9; padding: 9px; cursor: pointer; font-weight: 700; color: var(--muted); white-space: nowrap; }
.tabs-auth button.on { background: var(--brand); color: #fff; }
.tabs-auth button:first-child { border-radius: 10px 0 0 10px; }
.tabs-auth button:last-child { border-radius: 0 10px 10px 0; }
.radio-row { display: flex; gap: 10px; }
.radio-row label { display: flex; align-items: center; gap: 6px; font-weight: 600; color: var(--ink); cursor: pointer; white-space: nowrap; }

.stat-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.stat-table th, .stat-table td { border: 1px solid var(--line); padding: 7px 8px; text-align: left; }
.stat-table th { background: #f8fafc; color: var(--muted); }
.stat-table td.num { text-align: right; font-weight: 700; }
.stat-sum { margin-top: 10px; text-align: right; font-weight: 800; font-size: 16px; }
.stat-sum span { color: var(--brand-d); }

#toast {
  position: fixed; left: 50%; bottom: 28px; transform: translateX(-50%);
  background: #0f172a; color: #fff; padding: 10px 18px; border-radius: 999px;
  font-size: 14px; opacity: 0; pointer-events: none; transition: opacity .25s; z-index: 99; max-width: 80vw;
}
#toast.show { opacity: 1; }
.hint { font-size: 12px; color: var(--muted); margin-top: 6px; line-height: 1.5; }

/* ---------- 版本更新 ---------- */
.upd-ver { font-size: 20px; font-weight: 800; color: var(--brand-d); margin-bottom: 8px; }
.upd-force {
  display: none; background: var(--amber-bg); color: var(--amber);
  border-radius: 8px; padding: 8px 10px; font-size: 12px; margin-bottom: 10px; line-height: 1.5;
}
.upd-notes {
  font-size: 13px; color: var(--muted); line-height: 1.7;
  white-space: pre-wrap; word-break: break-all;
  max-height: 180px; overflow: auto; margin-bottom: 10px;
}
.upd-prog { display: none; margin: 4px 0 12px; }
.upd-bar { height: 8px; background: #e2e8f0; border-radius: 999px; overflow: hidden; }
.upd-bar i { display: block; height: 100%; width: 0; background: var(--brand); border-radius: 999px; transition: width .2s; }
.upd-ptext { font-size: 12px; color: var(--muted); margin-top: 6px; text-align: center; }
