:root {
  --brand: #0b7a3e;
  --brand-dark: #075c2e;
  --brand-soft: #e7f4ec;
  --bg: #f5f5f7;
  --card: #ffffff;
  --text: #1d1d1f;
  --muted: #6e6e73;
  --line: #e3e3e8;
  --danger: #d70015;
  --danger-soft: #fdecee;
  --warn: #b25000;
  --warn-soft: #fff4e5;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(0, 0, 0, .04), 0 4px 16px rgba(0, 0, 0, .04);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang TC", "Noto Sans TC",
    "Microsoft JhengHei", sans-serif;
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); font-size: 15px; line-height: 1.5; }
a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 28px; font-weight: 700; letter-spacing: -.02em; margin: 0 0 4px; }
h2 { font-size: 19px; font-weight: 600; margin: 0 0 12px; }
h3 { font-size: 16px; font-weight: 600; margin: 0 0 8px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.right { text-align: right; }
.nowrap { white-space: nowrap; }
.hidden { display: none !important; }

/* 版面 */
.layout { display: flex; min-height: 100vh; }
.sidebar {
  width: 232px; flex-shrink: 0; background: rgba(255, 255, 255, .8);
  backdrop-filter: saturate(180%) blur(20px); border-right: 1px solid var(--line);
  padding: 20px 12px; position: sticky; top: 0; height: 100vh; overflow-y: auto;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 10px 18px; font-weight: 700; font-size: 17px; }
.brand-mark {
  width: 30px; height: 30px; border-radius: 8px; background: var(--brand); color: #fff;
  display: grid; place-items: center; font-size: 15px;
}
.nav-group { font-size: 12px; color: var(--muted); padding: 14px 10px 4px; }
.nav a {
  display: block; padding: 7px 10px; border-radius: 8px; color: var(--text); font-size: 14.5px;
}
.nav a:hover { background: #f0f0f2; text-decoration: none; }
.nav a.active { background: var(--brand-soft); color: var(--brand-dark); font-weight: 600; }
.sidebar-foot { margin-top: 18px; padding: 12px 10px; border-top: 1px solid var(--line); font-size: 13px; }
.main { flex: 1; min-width: 0; padding: 28px 32px 60px; max-width: 1200px; }
.topbar { display: none; }

.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; margin-bottom: 20px; flex-wrap: wrap; }

/* 卡片與表格 */
.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 20px; margin-bottom: 16px; }
.grid { display: grid; gap: 16px; }
.kpis { grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }
.kpi { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px 20px; }
.kpi .label { color: var(--muted); font-size: 13px; }
.kpi .value { font-size: 26px; font-weight: 700; letter-spacing: -.02em; margin-top: 4px; }
.two-col { grid-template-columns: 1fr 1fr; }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 10px 12px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
th { font-size: 12.5px; color: var(--muted); font-weight: 500; white-space: nowrap; }
tr:last-child td { border-bottom: none; }
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: #fafafa; }
tr.inactive td { color: var(--muted); }
td.num, th.num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.empty { padding: 32px; text-align: center; color: var(--muted); }

.badge { display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: 12px; font-weight: 600; white-space: nowrap; }
.badge.green { background: var(--brand-soft); color: var(--brand-dark); }
.badge.gray { background: #f0f0f2; color: var(--muted); }
.badge.orange { background: var(--warn-soft); color: var(--warn); }
.badge.red { background: var(--danger-soft); color: var(--danger); }

/* 表單 */
label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; }
input, select, textarea {
  width: 100%; font: inherit; color: inherit; padding: 9px 12px; border: 1px solid var(--line);
  border-radius: 10px; background: #fff; outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--brand); box-shadow: 0 0 0 3px rgba(11, 122, 62, .15); }
input[type="checkbox"] { width: auto; margin-right: 6px; }
textarea { min-height: 80px; resize: vertical; }
.field { margin-bottom: 14px; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 0 16px; }
.check { display: flex; align-items: center; color: var(--text); font-size: 15px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; font: inherit; font-weight: 500;
  padding: 9px 18px; border-radius: 999px; border: none; cursor: pointer; background: var(--brand);
  color: #fff; white-space: nowrap; text-decoration: none !important;
}
.btn:hover { background: var(--brand-dark); }
.btn:disabled { opacity: .5; cursor: default; }
.btn.secondary { background: #e8e8ed; color: var(--text); }
.btn.secondary:hover { background: #dcdce1; }
.btn.danger { background: var(--danger-soft); color: var(--danger); }
.btn.small { padding: 5px 12px; font-size: 13px; }
.btn.link { background: none; color: var(--brand); padding: 4px 6px; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.toolbar { display: flex; gap: 10px; margin-bottom: 14px; flex-wrap: wrap; }
.toolbar input, .toolbar select { max-width: 280px; }
.tabs { display: inline-flex; background: #e8e8ed; border-radius: 10px; padding: 3px; }
.tabs button { border: none; background: none; font: inherit; font-size: 14px; padding: 6px 14px; border-radius: 8px; cursor: pointer; color: var(--text); }
.tabs button.active { background: #fff; box-shadow: 0 1px 3px rgba(0, 0, 0, .1); font-weight: 600; }

.items-table input, .items-table select { padding: 7px 9px; }
.items-table td { padding: 6px; }
.totals { margin-left: auto; max-width: 300px; }
.totals div { display: flex; justify-content: space-between; padding: 4px 0; }
.totals .grand { font-size: 20px; font-weight: 700; border-top: 1px solid var(--line); padding-top: 8px; margin-top: 4px; }

.alert { padding: 12px 14px; border-radius: 10px; margin-bottom: 14px; font-size: 14px; }
.alert.error { background: var(--danger-soft); color: var(--danger); }
.alert.info { background: var(--brand-soft); color: var(--brand-dark); }
.alert.warn { background: var(--warn-soft); color: var(--warn); }

.ai-box { border: 1.5px dashed #9fcfb2; background: #f6fbf8; }
.photo-preview { max-width: 240px; max-height: 240px; border-radius: 10px; display: block; margin-top: 8px; }
.scan-thumbs { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 10px; }
.scan-thumbs:empty { display: none; }
.scan-mini { float: right; width: 84px; height: 84px; object-fit: cover; border-radius: 8px; border: 1px solid var(--line); margin: 0 0 8px 12px; }
.scan-thumb { position: relative; margin: 0; width: 104px; }
.scan-thumb img, .scan-thumb .scan-pdf { width: 104px; height: 104px; object-fit: cover; border-radius: 10px; border: 1px solid var(--line); display: block; background: #fff; }
.scan-thumb .scan-pdf { display: flex; flex-direction: column; align-items: center; justify-content: center; font-size: 11px; text-align: center; overflow: hidden; padding: 4px; word-break: break-all; }
.scan-thumb figcaption { font-size: 12px; color: var(--muted); text-align: center; margin-top: 2px; }
.scan-thumb button { position: absolute; top: -6px; right: -6px; width: 24px; height: 24px; border-radius: 50%; border: none; background: rgba(0,0,0,.65); color: #fff; font-size: 12px; cursor: pointer; }
.ship-photo { max-width: 100%; max-height: 420px; border-radius: 10px; }

.bars { display: flex; align-items: flex-end; gap: 10px; height: 160px; padding-top: 10px; }
.bar { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: flex-end; height: 100%; gap: 4px; }
.bar .fill { width: 100%; max-width: 44px; background: var(--brand); border-radius: 6px 6px 0 0; min-height: 2px; }
.bar .lbl { font-size: 12px; color: var(--muted); }
.bar .val { font-size: 11px; color: var(--muted); white-space: nowrap; }

dialog {
  border: none; border-radius: 18px; padding: 0; width: min(640px, calc(100vw - 32px));
  box-shadow: 0 20px 60px rgba(0, 0, 0, .25);
}
dialog::backdrop { background: rgba(0, 0, 0, .3); backdrop-filter: blur(2px); }
.dialog-body { padding: 24px; max-height: 80vh; overflow-y: auto; }

/* 登入頁：深祖母綠底、金色細線、襯線標題、霧面玻璃卡片；手機改成上下排列 */
.auth-wrap {
  --emerald-900: #04170f; --emerald-800: #072a1b; --emerald-700: #0a3d27; --emerald-500: #12704a;
  --gold-300: #f1dca4; --gold-400: #e2c27a; --gold-500: #c9a24f; --gold-600: #a57f2e;
  --serif: "Noto Serif TC", "Songti TC", "PMingLiU", serif;
  position: relative; isolation: isolate; overflow: hidden; min-height: 100vh;
  display: grid; grid-template-columns: minmax(0, 1.1fr) minmax(0, 1fr);
  background:
    radial-gradient(1100px 700px at 15% 10%, #0f5a3a 0%, transparent 60%),
    radial-gradient(900px 600px at 95% 100%, #0b4a30 0%, transparent 55%),
    linear-gradient(160deg, var(--emerald-700) 0%, var(--emerald-800) 45%, var(--emerald-900) 100%);
  color: #f6f3ea;
}
.auth-wrap::before { /* 細格紋 */
  content: ""; position: absolute; inset: 0; z-index: -1; pointer-events: none; opacity: .07;
  background-image: linear-gradient(rgba(241,220,164,.6) 1px, transparent 1px), linear-gradient(90deg, rgba(241,220,164,.6) 1px, transparent 1px);
  background-size: 44px 44px;
  mask-image: radial-gradient(ellipse at 30% 40%, #000 0%, transparent 75%);
}
.auth-wrap::after { /* 金色細框 */
  content: ""; position: absolute; inset: 18px; z-index: -1; pointer-events: none;
  border: 1px solid rgba(226, 194, 122, .22); border-radius: 22px;
}
.auth-glow { position: absolute; inset: 0; z-index: -1; pointer-events: none; }
.auth-glow i { position: absolute; border-radius: 50%; filter: blur(70px); opacity: .55; }
.auth-glow i:nth-child(1) { width: 420px; height: 420px; left: -80px; top: -120px; background: #1d8a5b; }
.auth-glow i:nth-child(2) { width: 360px; height: 360px; right: 8%; bottom: -140px; background: rgba(201, 162, 79, .45); }
.auth-glow i:nth-child(3) { width: 260px; height: 260px; left: 38%; top: 40%; background: rgba(18, 112, 74, .55); }

.auth-hero { position: relative; display: flex; align-items: center; padding: 64px max(48px, 7vw); }
.auth-art { position: absolute; right: -60px; bottom: -40px; width: min(520px, 46vw); color: var(--gold-400); opacity: .16; }
.auth-hero-inner { position: relative; max-width: 540px; }
.auth-logo { display: flex; align-items: center; gap: 12px; font-weight: 700; font-size: 19px; letter-spacing: .04em; }
.auth-logo small { display: block; font-size: 11px; font-weight: 500; letter-spacing: .22em; text-transform: uppercase; color: var(--gold-400); margin-top: 2px; }
.auth-wrap .brand-mark {
  width: 40px; height: 40px; border-radius: 12px; font-size: 19px; color: var(--emerald-800); font-family: var(--serif);
  background: linear-gradient(145deg, var(--gold-300), var(--gold-500) 60%, var(--gold-600));
  box-shadow: 0 6px 20px rgba(201, 162, 79, .35), inset 0 1px 0 rgba(255, 255, 255, .5);
}
.auth-eyebrow { margin: 48px 0 14px; font-size: 11.5px; letter-spacing: .32em; color: var(--gold-400); display: flex; align-items: center; gap: 12px; }
.auth-eyebrow::before { content: ""; width: 36px; height: 1px; background: linear-gradient(90deg, transparent, var(--gold-400)); }
.auth-title { font-family: var(--serif); font-weight: 700; font-size: clamp(32px, 3.4vw, 46px); line-height: 1.32; letter-spacing: .04em; margin: 0 0 36px; text-wrap: balance; }
.auth-title em {
  font-style: normal; background: linear-gradient(100deg, var(--gold-300), var(--gold-400) 45%, var(--gold-500));
  -webkit-background-clip: text; background-clip: text; color: transparent;
}
.auth-points { list-style: none; padding: 0; margin: 0; display: grid; gap: 0; border-top: 1px solid rgba(226, 194, 122, .18); }
.auth-points li { display: flex; gap: 16px; align-items: flex-start; padding: 16px 2px; border-bottom: 1px solid rgba(226, 194, 122, .18); }
.auth-icon {
  flex: none; width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center; color: var(--gold-300);
  border: 1px solid rgba(226, 194, 122, .45); background: rgba(226, 194, 122, .06);
}
.auth-icon svg { width: 20px; height: 20px; }
.auth-points b { display: block; font-size: 15.5px; font-weight: 600; letter-spacing: .03em; }
.auth-points span:not(.auth-icon) { display: block; font-size: 13.5px; color: rgba(246, 243, 234, .7); margin-top: 3px; }
.auth-hero-foot { margin: 26px 0 0; font-size: 12.5px; letter-spacing: .08em; color: rgba(246, 243, 234, .55); }

.auth-panel { position: relative; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 56px 32px 32px; }
.auth-card {
  position: relative; width: 100%; max-width: 420px; color: var(--text); border-radius: 24px; padding: 40px 36px 30px;
  background: linear-gradient(180deg, rgba(255, 255, 255, .96), rgba(250, 248, 242, .93));
  backdrop-filter: blur(18px) saturate(140%);
  box-shadow: 0 30px 80px rgba(0, 0, 0, .45), 0 2px 0 rgba(255, 255, 255, .6) inset;
  border: 1px solid rgba(226, 194, 122, .55);
}
.auth-card::before { /* 卡片頂端金線 */
  content: ""; position: absolute; left: 36px; right: 36px; top: 0; height: 2px; border-radius: 2px;
  background: linear-gradient(90deg, transparent, var(--gold-400), transparent);
}
.auth-logo-mobile { display: none; }
.auth-h { font-family: var(--serif); font-size: 27px; font-weight: 700; margin: 0 0 6px; letter-spacing: .04em; color: var(--emerald-800); }
.auth-sub { margin: 0 0 26px; font-size: 14.5px; }
.auth-card .field { margin-bottom: 18px; }
.auth-card label { font-size: 13px; color: #4b5552; font-weight: 600; letter-spacing: .04em; margin-bottom: 7px; }
.auth-card input {
  padding: 13px 15px; border-radius: 12px; font-size: 15.5px; background: #fff; border: 1px solid #ddd8c8;
  transition: border-color .15s, box-shadow .15s;
}
.auth-card input:focus { border-color: var(--gold-500); box-shadow: 0 0 0 4px rgba(201, 162, 79, .18); }
.label-row { display: flex; justify-content: space-between; align-items: baseline; }
.auth-card a { color: var(--emerald-500); }
.auth-card a:hover { color: var(--gold-600); }
.pw-wrap { position: relative; }
.pw-wrap input { padding-right: 66px; }
.pw-toggle {
  position: absolute; right: 6px; top: 50%; transform: translateY(-50%); border: none; background: none;
  color: var(--emerald-500); font: inherit; font-size: 13px; padding: 6px 10px; border-radius: 8px; cursor: pointer;
}
.pw-toggle:hover { background: rgba(201, 162, 79, .12); }
.auth-btn {
  position: relative; overflow: hidden; width: 100%; padding: 14px 18px; font-size: 16px; letter-spacing: .12em; margin-top: 8px;
  color: #fdf8ea; background: linear-gradient(135deg, var(--emerald-500), var(--emerald-700) 70%);
  border: 1px solid rgba(226, 194, 122, .6);
  box-shadow: 0 10px 26px rgba(7, 42, 27, .35), inset 0 1px 0 rgba(255, 255, 255, .18);
}
.auth-btn:hover { background: linear-gradient(135deg, #168257, var(--emerald-700) 70%); }
.auth-btn::after { /* 金色光澤 */
  content: ""; position: absolute; top: 0; left: -60%; width: 40%; height: 100%; transform: skewX(-20deg);
  background: linear-gradient(90deg, transparent, rgba(241, 220, 164, .45), transparent);
}
.auth-note { text-align: center; margin: 18px 0 0; }
.auth-card .alert { margin-top: 4px; }
.auth-copy { font-size: 12px; color: rgba(246, 243, 234, .55); margin: 26px 0 0; text-align: center; line-height: 1.8; letter-spacing: .04em; }

@media (max-width: 860px) {
  .auth-wrap { grid-template-columns: 1fr; }
  .auth-wrap::after { inset: 10px; border-radius: 18px; }
  .auth-hero { padding: 34px 24px 10px; }
  .auth-art { width: 260px; right: -70px; bottom: -60px; }
  .auth-eyebrow { margin: 22px 0 10px; letter-spacing: .18em; font-size: 10.5px; white-space: nowrap; }
  .auth-eyebrow::before { width: 20px; }
  .auth-title { font-size: 25px; margin: 0; }
  .auth-title br { display: none; }
  .auth-points, .auth-hero-foot { display: none; }
  .auth-panel { justify-content: flex-start; padding: 22px 16px 28px; }
  .auth-card { padding: 30px 22px 24px; border-radius: 20px; }
}
@media (prefers-reduced-motion: no-preference) {
  .auth-card { animation: auth-in .6s cubic-bezier(.2, .7, .2, 1) both .1s; }
  .auth-hero-inner > * { animation: auth-rise .7s cubic-bezier(.2, .7, .2, 1) both; }
  .auth-hero-inner > *:nth-child(2) { animation-delay: .08s; }
  .auth-hero-inner > *:nth-child(3) { animation-delay: .16s; }
  .auth-hero-inner > *:nth-child(4) { animation-delay: .24s; }
  .auth-glow i { animation: auth-drift 18s ease-in-out infinite alternate; }
  .auth-glow i:nth-child(2) { animation-duration: 22s; }
  .auth-glow i:nth-child(3) { animation-duration: 26s; }
  .auth-btn::after { animation: auth-sheen 4.5s ease-in-out infinite 1.2s; }
  @keyframes auth-in { from { opacity: 0; transform: translateY(14px) scale(.985); } to { opacity: 1; transform: none; } }
  @keyframes auth-rise { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: none; } }
  @keyframes auth-drift { to { transform: translate(40px, 30px) scale(1.12); } }
  @keyframes auth-sheen { 0%, 70% { left: -60%; } 100% { left: 130%; } }
}

.info-list { display: grid; grid-template-columns: 110px 1fr; gap: 6px 12px; font-size: 14.5px; }
.info-list dt { color: var(--muted); }
.info-list dd { margin: 0; }

.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%); background: #1d1d1f; color: #fff;
  padding: 10px 20px; border-radius: 999px; font-size: 14px; z-index: 100; box-shadow: 0 8px 24px rgba(0, 0, 0, .2);
}

/* 對帳單列印 */
.statement h1 { font-size: 22px; }
@media print {
  .sidebar, .topbar, .no-print, .page-head .actions { display: none !important; }
  body { background: #fff; }
  .main { padding: 0; max-width: none; }
  .card { box-shadow: none; padding: 0; }
}

/* 手機 */
@media (max-width: 860px) {
  .layout { display: block; }
  .sidebar {
    position: fixed; z-index: 50; left: 0; top: 0; transform: translateX(-100%); transition: transform .2s;
    background: #fff; box-shadow: 0 0 40px rgba(0, 0, 0, .15);
  }
  .sidebar.open { transform: none; }
  .topbar {
    display: flex; align-items: center; gap: 10px; position: sticky; top: 0; z-index: 40; padding: 10px 16px;
    background: rgba(255, 255, 255, .85); backdrop-filter: saturate(180%) blur(20px); border-bottom: 1px solid var(--line);
    font-weight: 700;
  }
  .topbar button { border: none; background: none; font-size: 22px; padding: 2px 6px; cursor: pointer; }
  .main { padding: 18px 16px 60px; }
  .two-col { grid-template-columns: 1fr; }
  h1 { font-size: 24px; }
}

/* 進項發票分類 */
.cat-select { padding: 5px 8px; font-size: 13px; min-width: 112px; }
.cat-sum td { padding: 7px 8px; }
.cat-sum tr.active td { background: var(--brand-soft); font-weight: 600; }

/* 借款餘額 */
.loan-note { color: var(--brand-dark); }
.loan-edit { margin-top: 10px; padding: 10px 12px; border-radius: 10px; background: var(--brand-soft); font-size: 13px; display: flex; flex-wrap: wrap; gap: 6px 14px; align-items: center; }
.loan-edit label { display: inline-flex; align-items: center; gap: 6px; }
.loan-edit input { width: 130px; padding: 5px 8px; }

/* 重複收據合併 */
.dup-card { border: 1.5px solid var(--warn); }
.dup-group { padding: 10px 0; border-top: 1px solid var(--line); }
.dup-item { display: flex; flex-wrap: wrap; gap: 6px 12px; align-items: flex-start; padding: 8px 0; border-bottom: 1px dashed var(--line); }
.dup-item > label { display: inline-flex; align-items: center; gap: 4px; font-size: 13px; white-space: nowrap; cursor: pointer; }
.dup-item > span { flex: 1 1 220px; }
.dup-item input { width: auto; margin-top: 4px; }
.dup-item a { margin-left: 6px; }

/* 自訂期間 */
.custom-range { display: inline-flex; flex-wrap: wrap; align-items: center; gap: 6px; }
.custom-range input { width: auto; }

/* 掃描歸檔：核對 */
.review-box a { text-decoration: underline; }
.review-box .check { color: var(--text); font-weight: 600; }

/* 回收區 */
.trash-item { padding: 8px 0; border-bottom: 1px dashed var(--line); }
.trash-row .actions { justify-content: space-between; margin: 4px 0 10px; }
.auth-wrap > .alert { grid-column: 1 / -1; align-self: center; justify-self: center; max-width: 420px; color: var(--danger); }

tr.focus-row td { background: var(--warn-soft); }
