:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --text: #1d2330;
  --muted: #667085;
  --line: #e4e7ec;
  --primary: #1f6feb;
  --primary-ink: #ffffff;
  --red: #d92d20;
  --red-bg: #fef3f2;
  --orange: #dc6803;
  --orange-bg: #fffaeb;
  --green: #067647;
  --green-bg: #ecfdf3;
  --yellow: #b54708;
  --yellow-bg: #fef6e0;
  --ai: #6941c6;
  --ai-bg: #f4f3ff;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .1);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.5;
  overflow-x: hidden;
  -webkit-text-size-adjust: 100%;
}
button, input, select, textarea { font: inherit; color: inherit; }
img { max-width: 100%; }
[hidden] { display: none !important; }

.num { font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.in { color: var(--green); }
.out { color: var(--text); }

/* ---------- 登入 ---------- */
.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 16px; }
.login-card { width: 100%; max-width: 380px; background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 28px 20px; }
.login-card h1 { margin: 0 0 4px; font-size: 22px; }
.login-card p { margin: 0 0 20px; color: var(--muted); }

/* ---------- 表單 ---------- */
label.field { display: block; margin-bottom: 12px; }
label.field > span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; }
input[type=text], input[type=email], input[type=password], input[type=date], input[type=number], input[type=search], select, textarea {
  width: 100%; min-width: 0; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface);
  font-size: 16px; /* 避免 iOS 聚焦時放大 */
}
textarea { resize: vertical; min-height: 60px; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: -1px; border-color: transparent; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.row2 > * { min-width: 0; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 10px 16px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--surface); cursor: pointer; font-weight: 600; white-space: nowrap; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn.danger { color: var(--red); border-color: #fda29b; }
.btn.block { width: 100%; }
.btn.sm { padding: 6px 10px; font-size: 13px; }
.error { color: var(--red); font-size: 14px; min-height: 20px; margin: 4px 0 8px; }

/* ---------- 版面 ---------- */
.topbar { position: sticky; top: 0; z-index: 10; background: var(--surface); border-bottom: 1px solid var(--line); }
.topbar-inner { max-width: 960px; margin: 0 auto; padding: 10px 16px; display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.org-wrap { min-width: 0; flex: 1; }
.org-select { font-weight: 700; padding: 6px 28px 6px 10px; max-width: 100%; }
.org-wrap .org-select:not([hidden]) + .org { font-weight: 400; font-size: 12px; color: var(--muted); }
.org { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.tabs { max-width: 960px; margin: 0 auto; display: flex; padding: 0 16px; gap: 4px; }
.tab { flex: 1; text-align: center; padding: 10px 4px; border: 0; background: none; border-bottom: 2px solid transparent; color: var(--muted); font-weight: 600; cursor: pointer; }
.tab.active { color: var(--primary); border-color: var(--primary); }
main { max-width: 960px; margin: 0 auto; padding: 16px 16px 110px; }

.card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; margin-bottom: 12px; }
.card h2 { font-size: 15px; margin: 0 0 10px; color: var(--muted); font-weight: 600; }
.big { font-size: 30px; font-weight: 700; letter-spacing: -.5px; }
.diff-up { color: var(--red); }
.diff-down { color: var(--green); }

.due-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
@media (min-width: 640px) { .due-grid { grid-template-columns: repeat(4, 1fr); } }
.due-btn { border: 0; text-align: left; cursor: pointer; font: inherit; }
.due { border-radius: 10px; padding: 10px; min-width: 0; }
.due b { display: block; font-size: 20px; }
.due .label { font-size: 12px; font-weight: 600; }
.due.red { background: var(--red-bg); color: var(--red); }
.due.orange { background: var(--orange-bg); color: var(--orange); }
.due.gray { background: var(--bg); color: var(--muted); }

.pending-link { display: flex; justify-content: space-between; align-items: center; width: 100%; text-align: left; border: 0; cursor: pointer; }
.pending-link .big { color: var(--orange); }

.bar-row { display: grid; grid-template-columns: minmax(0, 7em) 1fr auto; gap: 8px; align-items: center; margin: 6px 0; font-size: 14px; }
.bar-row .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar { height: 10px; background: var(--primary); border-radius: 5px; min-width: 2px; }
.bar-track { background: var(--bg); border-radius: 5px; overflow: hidden; }

/* ---------- 列表 ---------- */
.list { list-style: none; margin: 0; padding: 0; }
.item { display: flex; gap: 10px; align-items: flex-start; padding: 10px 0; border-top: 1px solid var(--line); cursor: pointer; }
.item:first-child { border-top: 0; }
.item .main { flex: 1; min-width: 0; }
.item .line1 { display: flex; justify-content: space-between; gap: 8px; }
.item .desc { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.item .amt { font-weight: 700; white-space: nowrap; }
.item .line2 { display: flex; flex-wrap: wrap; gap: 4px 8px; align-items: center; font-size: 12px; color: var(--muted); margin-top: 2px; }
.item input[type=checkbox] { width: 20px; height: 20px; margin-top: 2px; flex: none; }

.chip { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; white-space: nowrap; }
.chip.pending { background: var(--orange-bg); color: var(--orange); }
.chip.posted { background: var(--green-bg); color: var(--green); }
.chip.rejected { background: var(--bg); color: var(--muted); text-decoration: line-through; }
.chip.unpaid { background: var(--red-bg); color: var(--red); }
.chip.paid { background: var(--bg); color: var(--muted); }
.chip.ai { background: var(--ai-bg); color: var(--ai); }
.mini-spin { display: inline-block; width: 10px; height: 10px; border: 2px solid currentColor; border-right-color: transparent;
  border-radius: 50%; animation: spin .8s linear infinite; margin-right: 4px; vertical-align: -1px; }
.flag { border: 0; background: none; padding: 0 2px; cursor: pointer; font-size: 14px; line-height: 1; }
.flag-reasons { font-size: 12px; margin-top: 4px; padding: 6px 8px; border-radius: 8px; background: var(--red-bg); color: var(--red); }
.flag-reasons.yellow { background: var(--yellow-bg); color: var(--yellow); }

.day-head { display: flex; justify-content: space-between; font-size: 13px; color: var(--muted); padding: 12px 2px 4px; font-weight: 600; }

.month-nav { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 12px; }
.month-nav strong { font-size: 18px; }
.summary { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; text-align: center; }
.summary > div { min-width: 0; }
.summary b { display: block; font-size: 16px; overflow: hidden; text-overflow: ellipsis; }
.summary span { font-size: 12px; color: var(--muted); }
.filters { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 12px; }
.filters > * { min-width: 0; }
.filters .wide { grid-column: 1 / -1; }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; margin: 12px 0 0; }

.bulkbar { position: fixed; left: 0; right: 0; bottom: 76px; z-index: 15; display: flex; justify-content: center; padding: 0 16px; }
.bulkbar .btn { box-shadow: var(--shadow); }

/* ---------- 底部拍照按鈕 ---------- */
.bottombar { position: fixed; left: 0; right: 0; bottom: 0; z-index: 20; background: rgba(255,255,255,.96); border-top: 1px solid var(--line);
  padding: 10px 16px calc(10px + env(safe-area-inset-bottom)); }
.bottombar-inner { max-width: 960px; margin: 0 auto; display: flex; gap: 8px; }
.snap { flex: 1; font-size: 18px; padding: 14px; }
.snap-alt { flex: none; }

/* ---------- 面板（bottom sheet） ---------- */
.sheet-backdrop { position: fixed; inset: 0; background: rgba(16, 24, 40, .45); z-index: 40; display: flex; align-items: flex-end; justify-content: center; }
.sheet { background: var(--surface); width: 100%; max-width: 640px; max-height: 92vh; overflow-y: auto; overflow-x: hidden;
  border-radius: 16px 16px 0 0; padding: 16px 16px calc(16px + env(safe-area-inset-bottom)); }
@media (min-width: 700px) { .sheet-backdrop { align-items: center; } .sheet { border-radius: 16px; } }
.sheet-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; gap: 8px; }
.sheet-head h3 { margin: 0; font-size: 18px; }
.sheet-actions { display: flex; gap: 8px; margin-top: 16px; flex-wrap: wrap; }
.sheet-actions .btn { flex: 1; }

.toggle { display: grid; grid-template-columns: 1fr 1fr; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.toggle button { border: 0; background: var(--surface); padding: 10px; cursor: pointer; font-weight: 600; color: var(--muted); }
.toggle button.on { background: var(--primary); color: var(--primary-ink); }

.ai-field input, .ai-field select, .ai-field textarea { background: var(--ai-bg); border-color: #d9d6fe; }
.ai-tag { color: var(--ai); font-size: 11px; font-weight: 700; margin-left: 4px; }
.ai-box { background: var(--ai-bg); color: var(--ai); border-radius: 10px; padding: 10px 12px; font-size: 13px; margin-bottom: 12px; }
.warn-box { background: var(--orange-bg); color: var(--orange); border-radius: 10px; padding: 10px 12px; font-size: 13px; margin-bottom: 12px; }
.err-box { background: var(--red-bg); color: var(--red); border-radius: 10px; padding: 10px 12px; font-size: 13px; margin-bottom: 12px; }
.remember { display: flex; gap: 8px; align-items: flex-start; background: var(--ai-bg); border-radius: 10px; padding: 10px 12px; margin-bottom: 12px; font-size: 14px; }
.remember input { width: 20px; height: 20px; flex: none; }

.thumbs { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 4px; margin-bottom: 12px; }
.thumb { flex: none; width: 84px; height: 84px; border-radius: 8px; border: 1px solid var(--line); background: var(--bg) center/cover no-repeat;
  display: flex; align-items: center; justify-content: center; font-size: 12px; color: var(--muted); text-decoration: none; text-align: center; position: relative; }
.thumb .kind { position: absolute; bottom: 2px; left: 2px; right: 2px; background: rgba(0,0,0,.55); color: #fff; font-size: 10px; border-radius: 4px; }

.viewer { position: fixed; inset: 0; background: rgba(0,0,0,.9); z-index: 60; display: flex; flex-direction: column; }
.viewer img, .viewer iframe { flex: 1; width: 100%; object-fit: contain; border: 0; background: #fff; min-height: 0; }
.viewer .btn { margin: 10px; align-self: flex-end; }

.audit { font-size: 12px; color: var(--muted); border-top: 1px solid var(--line); margin-top: 16px; padding-top: 8px; }
.audit summary { cursor: pointer; }
.audit li { margin: 2px 0; word-break: break-all; }

.choice { display: grid; gap: 10px; }
.loading { text-align: center; padding: 30px 0; color: var(--muted); }
.spinner { width: 28px; height: 28px; border: 3px solid var(--line); border-top-color: var(--primary); border-radius: 50%; animation: spin 1s linear infinite; margin: 0 auto 10px; }
@keyframes spin { to { transform: rotate(360deg); } }

.toast { position: fixed; left: 50%; transform: translateX(-50%); bottom: 90px; background: var(--text); color: #fff; padding: 10px 16px; border-radius: 10px; z-index: 70; max-width: calc(100% - 32px); font-size: 14px; }
.empty { text-align: center; color: var(--muted); padding: 20px 0; }
