:root {
  --bg: #f4f6f9;
  --panel: #ffffff;
  --ink: #1f2733;
  --muted: #6b7684;
  --line: #e3e8ef;
  --brand: #1f6feb;
  --brand-d: #1858c4;
  --income: #0a7d3c;
  --expense: #c23b32;
  --pending: #b7791f;
  --shadow: 0 1px 3px rgba(20,30,50,.06), 0 6px 20px rgba(20,30,50,.05);
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font-family: "Segoe UI", "Microsoft JhengHei", system-ui, sans-serif;
  font-size: 14px; -webkit-font-smoothing: antialiased;
}
button { font-family: inherit; cursor: pointer; }
input, select { font-family: inherit; font-size: 14px; }

/* ---------- 登入 ---------- */
#login {
  position: fixed; inset: 0; display: flex; align-items: center; justify-content: center;
  background: linear-gradient(135deg, #1f6feb 0%, #123a7a 100%);
}
#login .card {
  background: #fff; padding: 36px 34px; border-radius: 14px; width: 340px;
  box-shadow: 0 20px 60px rgba(0,0,0,.3);
}
#login h1 { font-size: 19px; margin: 0 0 4px; }
#login .sub { color: var(--muted); font-size: 13px; margin-bottom: 22px; }
#login label { display: block; font-size: 12px; color: var(--muted); margin: 14px 0 5px; }
#login input {
  width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 8px;
}
#login button {
  width: 100%; margin-top: 22px; padding: 11px; background: var(--brand); color: #fff;
  border: none; border-radius: 8px; font-size: 15px; font-weight: 600;
}
#login button:hover { background: var(--brand-d); }
#login .err { color: var(--expense); font-size: 13px; margin-top: 12px; min-height: 18px; }

/* ---------- 版面 ---------- */
#app { display: none; min-height: 100vh; }
.layout { display: flex; min-height: 100vh; }
.sidebar {
  width: 210px; background: #10233f; color: #cfd8e6; flex-shrink: 0;
  display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh;
}
.sidebar .brand { padding: 20px 20px 14px; font-weight: 700; font-size: 15px; color: #fff; line-height: 1.4; }
.sidebar .brand small { display: block; font-weight: 400; font-size: 11px; color: #8ba0bd; margin-top: 3px; }
.nav { flex: 1; padding: 6px 10px; }
.nav a {
  display: flex; align-items: center; gap: 9px; padding: 10px 12px; margin: 2px 0;
  color: #cfd8e6; text-decoration: none; border-radius: 8px; font-size: 14px;
}
.nav a .ic { width: 18px; text-align: center; opacity: .85; }
.nav a:hover { background: rgba(255,255,255,.07); }
.nav a.active { background: var(--brand); color: #fff; }
.sidebar .foot { padding: 14px 16px; border-top: 1px solid rgba(255,255,255,.08); font-size: 12px; color: #8ba0bd; }
.sidebar .foot b { color: #fff; font-weight: 600; }
.sidebar .foot button {
  margin-top: 8px; background: transparent; color: #8ba0bd; border: 1px solid rgba(255,255,255,.18);
  padding: 5px 10px; border-radius: 6px; font-size: 12px;
}
.sidebar .foot button:hover { color: #fff; border-color: #fff; }

.main { flex: 1; min-width: 0; padding: 22px 26px 60px; }
.page-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 18px; gap: 12px; flex-wrap: wrap; }
.page-head h2 { margin: 0; font-size: 20px; }
.page-head .tools { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }

/* ---------- 元件 ---------- */
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow); }
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px,1fr)); gap: 14px; margin-bottom: 20px; }
.kpi { padding: 16px 18px; }
.kpi .label { color: var(--muted); font-size: 13px; }
.kpi .val { font-size: 26px; font-weight: 700; margin-top: 6px; letter-spacing: -.5px; }
.kpi .val.small { font-size: 20px; }
.kpi.income .val { color: var(--income); }
.kpi.expense .val { color: var(--expense); }
.kpi.pending .val { color: var(--pending); }

.btn { background: var(--brand); color: #fff; border: none; padding: 8px 15px; border-radius: 8px; font-weight: 600; font-size: 13px; }
.btn:hover { background: var(--brand-d); }
.btn.ghost { background: #fff; color: var(--brand); border: 1px solid var(--brand); }
.btn.ghost:hover { background: #eef4ff; }
.btn.sm { padding: 5px 10px; font-size: 12px; }
.btn.danger { background: var(--expense); }
.btn.gray { background: #eef1f6; color: var(--ink); }
.btn.gray:hover { background: #e2e7ef; }

select.inp, input.inp { padding: 7px 10px; border: 1px solid var(--line); border-radius: 8px; background: #fff; }

/* ---------- 表格 ---------- */
.tbl-wrap { overflow-x: auto; }
table.grid { width: 100%; border-collapse: collapse; background: #fff; }
table.grid th, table.grid td { padding: 7px 9px; border-bottom: 1px solid var(--line); text-align: left; white-space: nowrap; }
table.grid th { background: #f7f9fc; color: var(--muted); font-weight: 600; font-size: 12px; position: sticky; top: 0; z-index: 1; }
table.grid td.num, table.grid th.num { text-align: right; font-variant-numeric: tabular-nums; }
table.grid tr:hover td { background: #fafbfe; }
table.grid input, table.grid select {
  width: 100%; border: 1px solid transparent; background: transparent; padding: 5px 6px;
  border-radius: 6px; font-size: 13px; min-width: 60px;
}
table.grid td.num input { text-align: right; }
table.grid input:focus, table.grid select:focus { border-color: var(--brand); background: #fff; outline: none; }
table.grid input:hover, table.grid select:hover { background: #f0f3f8; }
.amt-in { color: var(--income); font-weight: 600; }
.amt-out { color: var(--expense); font-weight: 600; }
.pill { display: inline-block; padding: 2px 9px; border-radius: 20px; font-size: 12px; font-weight: 600; }
.pill.已收 { background: #e6f5ec; color: var(--income); }
.pill.已付 { background: #fdeceb; color: var(--expense); }
.pill.應收 { background: #fff5e6; color: var(--pending); }
.pill.應付 { background: #fff5e6; color: var(--pending); }
.row-save { font-size: 11px; color: var(--muted); }
tr.dirty td { background: #fffdf3 !important; }
tr.conflict td { background: #fdecea !important; }
tr.saved-flash td { animation: flash 1s; }
@keyframes flash { from { background: #e6f5ec; } to { background: transparent; } }

.muted { color: var(--muted); }
.right { text-align: right; }
.section-title { font-size: 15px; font-weight: 700; margin: 24px 0 10px; }
.bar { height: 10px; background: #eef1f6; border-radius: 6px; overflow: hidden; }
.bar > span { display: block; height: 100%; }

/* toast */
#toast { position: fixed; bottom: 22px; right: 22px; display: flex; flex-direction: column; gap: 8px; z-index: 100; }
#toast .t { background: #10233f; color: #fff; padding: 10px 16px; border-radius: 8px; box-shadow: var(--shadow); font-size: 13px; animation: rise .25s; }
#toast .t.ok { background: #0a7d3c; }
#toast .t.err { background: #c23b32; }
@keyframes rise { from { transform: translateY(8px); opacity: 0; } }

/* modal */
.modal-bg { position: fixed; inset: 0; background: rgba(15,25,45,.5); display: flex; align-items: center; justify-content: center; z-index: 90; }
.modal { background: #fff; border-radius: 12px; padding: 22px 24px; width: 440px; max-width: 92vw; max-height: 88vh; overflow: auto; box-shadow: 0 20px 60px rgba(0,0,0,.3); }
.modal h3 { margin: 0 0 16px; }
.modal .field { margin-bottom: 12px; }
.modal .field label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.modal .field input, .modal .field select { width: 100%; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; }
.modal .actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 18px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

@media (max-width: 720px) {
  .sidebar { width: 60px; }
  .sidebar .brand, .sidebar .foot span, .nav a span.lbl { display: none; }
  .nav a { justify-content: center; }
}
