/* ============================================================
   預算線記帳 PWA — 樣式表
   Mobile-first、無外部依賴（無 CDN），方便 Service Worker 完整離線快取
   ============================================================ */

/* ---------- 設計變數 ---------- */
:root {
  --bg: #f1f5f9;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --surface-3: #eef2f7;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --primary-ink: #ffffff;
  --income: #16a34a;
  --income-bg: #dcfce7;
  --expense: #dc2626;
  --expense-bg: #fee2e2;
  --warn-bg: #fef3c7;
  --warn-text: #92400e;
  --radius: 14px;
  --radius-lg: 20px;
  --radius-sm: 10px;
  --shadow: 0 1px 2px rgba(15, 23, 42, .06), 0 4px 14px rgba(15, 23, 42, .06);
  --shadow-lg: 0 10px 40px rgba(15, 23, 42, .18);
  --tabbar-h: 58px;
  --topbar-h: 54px;
  --tap: 44px;
  --font: -apple-system, BlinkMacSystemFont, system-ui, "Segoe UI", "PingFang HK",
          "Hiragino Sans TC", "Microsoft JhengHei", "Noto Sans TC", sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

:root[data-theme="dark"] {
  --bg: #0b1220;
  --surface: #131c2e;
  --surface-2: #182234;
  --surface-3: #1e293b;
  --text: #e8eef8;
  --muted: #94a3b8;
  --border: #263449;
  --primary: #3b82f6;
  --primary-dark: #2563eb;
  --income: #4ade80;
  --income-bg: rgba(74, 222, 128, .14);
  --expense: #f87171;
  --expense-bg: rgba(248, 113, 113, .14);
  --warn-bg: rgba(251, 191, 36, .16);
  --warn-text: #fcd34d;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 4px 14px rgba(0, 0, 0, .3);
  --shadow-lg: 0 12px 40px rgba(0, 0, 0, .55);
}

/* ---------- 基礎 ---------- */
*, *::before, *::after { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.5;
  background: var(--bg);
  color: var(--text);
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}

h1, h2, h3, h4 { margin: 0; font-weight: 700; }
p { margin: 0; }
a { color: var(--primary); }
code { font-family: var(--mono); font-size: .85em; background: var(--surface-3); padding: 2px 5px; border-radius: 5px; }
[hidden] { display: none !important; }

/* ---------- 啟動畫面 ---------- */
.boot {
  position: fixed; inset: 0; z-index: 60;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 16px;
  background: var(--bg); color: var(--muted);
}
.boot__logo, .login__logo {
  width: 64px; height: 64px; border-radius: 18px;
  background: linear-gradient(150deg, var(--primary), #7c3aed);
  position: relative;
}
.boot__logo::after, .login__logo::after {
  content: ""; position: absolute; inset: 18px 16px;
  border-radius: 4px;
  background:
    linear-gradient(to top, rgba(255,255,255,.95) 0 100%) left bottom / 22% 45% no-repeat,
    linear-gradient(to top, rgba(255,255,255,.95) 0 100%) center bottom / 22% 72% no-repeat,
    linear-gradient(to top, rgba(255,255,255,.95) 0 100%) right bottom / 22% 100% no-repeat;
}
.boot__logo { animation: pulse 1.4s ease-in-out infinite; }
@keyframes pulse { 0%,100% { opacity: 1; transform: scale(1); } 50% { opacity: .7; transform: scale(.96); } }
.boot__text { font-size: 14px; }

/* ---------- 登入 ---------- */
.login {
  position: fixed; inset: 0; z-index: 50;
  display: flex; align-items: center; justify-content: center; padding: 24px;
  background: var(--bg);
}
.login__card {
  width: 100%; max-width: 360px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg);
  padding: 28px 22px 22px; box-shadow: var(--shadow);
  text-align: center;
}
.login__logo { margin: 0 auto 18px; }
.login__title { font-size: 21px; }
.login__sub { color: var(--muted); font-size: 14px; margin: 4px 0 22px; }
.login__card .field { text-align: left; }
.login__hint { color: var(--muted); font-size: 12px; margin-top: 16px; line-height: 1.6; }

/* ---------- 主版面 ---------- */
.app { min-height: 100%; display: flex; flex-direction: column; }

.topbar {
  position: sticky; top: 0; z-index: 20;
  height: calc(var(--topbar-h) + env(safe-area-inset-top));
  padding: env(safe-area-inset-top) 12px 0 16px;
  display: flex; align-items: center; justify-content: space-between;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(180%) blur(12px);
  border-bottom: 1px solid var(--border);
}
.topbar__left { display: flex; align-items: center; gap: 8px; min-width: 0; }
.topbar__title { font-size: 17px; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.view {
  flex: 1;
  padding: 14px 14px calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 84px);
  max-width: 720px; width: 100%; margin: 0 auto;
  outline: none;
}

/* ---------- 底部導覽 ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  display: grid; grid-template-columns: 1fr 1fr 72px 1fr 1fr;
  align-items: center;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: saturate(180%) blur(14px);
  border-top: 1px solid var(--border);
}
.tabbar__btn {
  appearance: none; border: 0; background: none; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  height: 100%; min-width: 0; padding: 0;
  color: var(--muted); font-family: inherit; font-size: 11px; line-height: 1.2;
}
.tabbar__btn.is-active { color: var(--primary); }
.tabbar__icon { display: block; }
.tabbar__icon svg { width: 22px; height: 22px; display: block; }
.tabbar__label { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tabbar__add {
  width: 56px; height: 56px; place-self: center; border-radius: 50%; border: 0; cursor: pointer;
  background: var(--primary); color: var(--primary-ink);
  display: grid; place-items: center;
  box-shadow: 0 6px 18px color-mix(in srgb, var(--primary) 45%, transparent);
  transform: translateY(-12px);
  transition: transform .12s ease;
}
.tabbar__add:active { transform: translateY(-12px) scale(.94); }
.tabbar__add svg { width: 26px; height: 26px; }

/* ---------- 卡片 / 區塊 ---------- */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; box-shadow: var(--shadow); margin-bottom: 14px;
}
.card__head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 12px; }
.card__title { font-size: 15px; font-weight: 700; }
.card__sub { font-size: 12px; color: var(--muted); font-weight: 400; }

.hero {
  background: linear-gradient(150deg, var(--primary), #7c3aed);
  color: #fff; border: 0; border-radius: var(--radius-lg); padding: 20px;
  box-shadow: var(--shadow-lg); margin-bottom: 16px;
}
.hero__label { font-size: 13px; opacity: .85; }
.hero__amount { font-size: 34px; font-weight: 800; letter-spacing: -.5px; margin: 4px 0 14px; line-height: 1.1; }
.hero__row { display: flex; gap: 10px; }
.hero__stat { flex: 1; background: rgba(255,255,255,.16); border-radius: 12px; padding: 9px 12px; }
.hero__stat-label { font-size: 11px; opacity: .85; }
.hero__stat-value { font-size: 16px; font-weight: 700; }

.section-title {
  font-size: 13px; font-weight: 700; color: var(--muted); text-transform: none;
  margin: 20px 4px 8px; display: flex; align-items: center; justify-content: space-between;
}

/* ---------- 預算線卡片 ---------- */
.line-card { display: flex; align-items: center; gap: 12px; }
.line-card__main { flex: 1; min-width: 0; }
.line-card__name { font-size: 15px; font-weight: 700; display: flex; align-items: center; gap: 6px; }
.line-card__meta { font-size: 12px; color: var(--muted); margin-top: 3px; }
.line-card__balance { text-align: right; font-size: 18px; font-weight: 800; white-space: nowrap; }
.line-card__balance.is-negative { color: var(--expense); }
.line-card.is-archived { opacity: .62; }
.line-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--primary); flex: none; }

/* ---------- 交易列表 ---------- */
.tx-group { margin-bottom: 14px; }
.tx-group__head {
  display: flex; justify-content: space-between; align-items: baseline;
  font-size: 12px; color: var(--muted); padding: 0 4px 6px;
}
.tx-group__sum { font-weight: 600; }
.tx-list { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); }
.tx {
  display: flex; align-items: center; gap: 12px; width: 100%;
  padding: 12px 14px; border: 0; border-bottom: 1px solid var(--border);
  background: none; text-align: left; font-family: inherit; font-size: inherit; color: inherit;
  cursor: pointer; min-height: 62px;
}
.tx:last-child { border-bottom: 0; }
.tx:active { background: var(--surface-2); }
.tx__thumb {
  width: 42px; height: 42px; border-radius: 9px; object-fit: cover; flex: none;
  background: var(--surface-3);
}
.tx__thumb--empty { display: grid; place-items: center; color: var(--muted); }
.tx__thumb svg { width: 20px; height: 20px; }
.tx__main { flex: 1; min-width: 0; }
.tx__note { font-size: 14px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tx__meta { font-size: 12px; color: var(--muted); margin-top: 2px; display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.tx__amount { font-size: 15px; font-weight: 700; white-space: nowrap; }
.tx__amount.is-income { color: var(--income); }
.tx__amount.is-expense { color: var(--expense); }

/* ---------- 標籤 ---------- */
.tag {
  display: inline-block; font-size: 11px; font-weight: 600; padding: 2px 7px;
  border-radius: 999px; background: var(--surface-3); color: var(--muted); white-space: nowrap;
}
.tag--income { background: var(--income-bg); color: var(--income); }
.tag--expense { background: var(--expense-bg); color: var(--expense); }
.tag--archived { background: var(--surface-3); color: var(--muted); }
.tag--receipt { background: var(--surface-3); color: var(--muted); }

.badge { font-size: 11px; font-weight: 700; padding: 3px 8px; border-radius: 999px; }
.badge--offline { background: var(--warn-bg); color: var(--warn-text); }

/* ---------- 表單 ---------- */
.field { display: block; margin-bottom: 14px; }
.field__label { display: block; font-size: 13px; font-weight: 600; color: var(--muted); margin-bottom: 5px; }
.input, .select, .textarea {
  width: 100%; padding: 11px 12px; font-size: 16px; font-family: inherit;
  color: var(--text); background: var(--surface-2);
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  appearance: none;
}
.input:focus, .select:focus, .textarea:focus { outline: 2px solid var(--primary); outline-offset: -1px; border-color: var(--primary); }
.textarea { min-height: 74px; resize: vertical; }
.select {
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 17px) 50%, calc(100% - 12px) 50%;
  background-size: 5px 5px, 5px 5px; background-repeat: no-repeat; padding-right: 34px;
}
.field__row { display: flex; gap: 10px; }
.field__row > .field { flex: 1; }
.field__hint { font-size: 12px; color: var(--muted); margin-top: 5px; }
.amount-input { font-size: 26px; font-weight: 800; text-align: right; letter-spacing: -.5px; padding: 12px; }

.segmented { display: flex; gap: 6px; background: var(--surface-3); padding: 4px; border-radius: 12px; }
.segmented button {
  flex: 1; border: 0; background: none; padding: 9px 6px; border-radius: 9px;
  font-family: inherit; font-size: 14px; font-weight: 700; color: var(--muted); cursor: pointer;
}
.segmented button.is-active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.segmented button.is-active[data-value="INCOME"] { color: var(--income); }
.segmented button.is-active[data-value="EXPENSE"] { color: var(--expense); }

.chips { display: flex; gap: 8px; overflow-x: auto; padding: 2px 0 6px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; border: 1px solid var(--border); background: var(--surface); color: var(--muted);
  border-radius: 999px; padding: 7px 14px; font-size: 13px; font-weight: 600; font-family: inherit; cursor: pointer;
}
.chip.is-active { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }

/* ---------- 按鈕 ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 12px 18px; font-size: 15px; font-weight: 700; font-family: inherit;
  border: 1px solid transparent; border-radius: var(--radius-sm); cursor: pointer;
  min-height: var(--tap); background: var(--surface-3); color: var(--text);
  transition: opacity .12s ease;
}
.btn:active { opacity: .8; }
.btn:disabled { opacity: .5; cursor: default; }
.btn--primary { background: var(--primary); color: var(--primary-ink); }
.btn--danger { background: var(--expense); color: #fff; }
.btn--ghost { background: transparent; border-color: var(--border); color: var(--text); }
.btn--block { display: flex; width: 100%; }
.btn--sm { padding: 7px 12px; font-size: 13px; min-height: 34px; border-radius: 8px; }
.btn--icon svg { width: 18px; height: 18px; }
.btn-row { display: flex; gap: 10px; }
.btn-row > .btn { flex: 1; }

.icon-btn {
  width: var(--tap); height: var(--tap); display: grid; place-items: center;
  border: 0; background: none; border-radius: 50%; color: var(--muted); cursor: pointer; padding: 0;
}
.icon-btn:active { background: var(--surface-3); }
.icon-btn svg { width: 22px; height: 22px; }
.icon-btn--light { color: #fff; }
.icon-btn--light:active { background: rgba(255,255,255,.18); }

/* ---------- 提示 / 空狀態 ---------- */
.alert { border-radius: var(--radius-sm); padding: 11px 13px; font-size: 14px; margin-bottom: 14px; }
.alert--error { background: var(--expense-bg); color: var(--expense); }
.alert--info { background: var(--surface-3); color: var(--muted); }
.alert--warn { background: var(--warn-bg); color: var(--warn-text); }

.empty { text-align: center; padding: 40px 20px; color: var(--muted); }
.empty__icon { width: 46px; height: 46px; margin: 0 auto 12px; opacity: .5; }
.empty__icon svg { width: 100%; height: 100%; }
.empty__title { font-weight: 700; color: var(--text); margin-bottom: 4px; }
.empty__text { font-size: 13px; }

.skeleton { background: var(--surface-3); border-radius: 8px; height: 14px; margin-bottom: 10px; animation: shimmer 1.3s linear infinite; }
@keyframes shimmer { 0% { opacity: .55; } 50% { opacity: 1; } 100% { opacity: .55; } }

/* ---------- 底部彈出面板 ---------- */
.backdrop { position: fixed; inset: 0; z-index: 40; background: rgba(15, 23, 42, .5); animation: fade .18s ease; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 41;
  max-height: 92vh; display: flex; flex-direction: column;
  background: var(--surface); border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  box-shadow: var(--shadow-lg);
  animation: slide-up .24s cubic-bezier(.22, 1, .36, 1);
}
@keyframes slide-up { from { transform: translateY(100%); } to { transform: translateY(0); } }
.sheet__grabber { padding: 8px 0 2px; display: grid; place-items: center; flex: none; }
.sheet__grabber::after { content: ""; width: 38px; height: 4px; border-radius: 999px; background: var(--border); }
.sheet__body {
  overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding: 6px 18px calc(24px + env(safe-area-inset-bottom));
}
.sheet__title { font-size: 18px; margin-bottom: 16px; }
.sheet__sticky { position: sticky; bottom: 0; background: var(--surface); padding-top: 10px; }

/* ---------- 圖片檢視器 ---------- */
.viewer { position: fixed; inset: 0; z-index: 55; background: rgba(2, 6, 23, .96); display: flex; flex-direction: column; }
.viewer__bar {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: calc(env(safe-area-inset-top) + 8px) 8px 8px 16px; color: #fff;
}
.viewer__title { font-size: 14px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.viewer__actions { display: flex; }
.viewer__stage { flex: 1; display: grid; place-items: center; overflow: auto; padding: 8px 12px calc(24px + env(safe-area-inset-bottom)); }
.viewer__stage img { max-width: 100%; max-height: 100%; border-radius: 10px; }

/* ---------- 對話框 ---------- */
.modal { position: fixed; inset: 0; z-index: 58; display: grid; place-items: center; padding: 22px; background: rgba(15, 23, 42, .55); }
.modal__card { width: 100%; max-width: 340px; background: var(--surface); border-radius: var(--radius); padding: 20px; box-shadow: var(--shadow-lg); }
.modal__title { font-size: 17px; margin-bottom: 8px; }
.modal__text { font-size: 14px; color: var(--muted); white-space: pre-line; }
.modal__actions { display: flex; gap: 10px; margin-top: 20px; }
.modal__actions .btn { flex: 1; }

/* ---------- Toast / 更新提示 ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 18px);
  transform: translateX(-50%); z-index: 57;
  max-width: calc(100% - 32px);
  background: var(--text); color: var(--bg); font-size: 14px; font-weight: 600;
  padding: 11px 18px; border-radius: 999px; box-shadow: var(--shadow-lg);
  animation: toast-in .2s ease;
}
.toast--error { background: var(--expense); color: #fff; }
@keyframes toast-in { from { opacity: 0; transform: translate(-50%, 10px); } to { opacity: 1; transform: translate(-50%, 0); } }

.update-bar {
  position: fixed; left: 12px; right: 12px; top: calc(env(safe-area-inset-top) + 10px); z-index: 59;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  background: var(--primary); color: var(--primary-ink); padding: 10px 12px 10px 16px;
  border-radius: 999px; box-shadow: var(--shadow-lg); font-size: 14px; font-weight: 600;
}

/* ---------- 收據縮圖 / 上傳 ---------- */
.receipt-picker { display: flex; gap: 10px; }
.receipt-picker .btn { flex: 1; }
.receipt-preview { margin-top: 12px; position: relative; border-radius: var(--radius-sm); overflow: hidden; border: 1px solid var(--border); }
.receipt-preview img { display: block; width: 100%; max-height: 240px; object-fit: contain; background: var(--surface-3); }
.receipt-preview__bar { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 8px 10px; font-size: 12px; color: var(--muted); background: var(--surface-2); }

.kv { display: flex; justify-content: space-between; gap: 12px; padding: 9px 0; border-bottom: 1px solid var(--border); font-size: 14px; }
.kv:last-child { border-bottom: 0; }
.kv__k { color: var(--muted); flex: none; }
.kv__v { text-align: right; font-weight: 600; word-break: break-all; }

.list-plain { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.list-row {
  display: flex; align-items: center; gap: 12px; padding: 13px 14px;
  border-bottom: 1px solid var(--border); width: 100%;
  background: none; border-left: 0; border-right: 0; border-top: 0;
  font-family: inherit; font-size: 15px; color: inherit; text-align: left; cursor: pointer;
}
.list-row:last-child { border-bottom: 0; }
.list-row:active { background: var(--surface-2); }
.list-row__main { flex: 1; min-width: 0; }
.list-row__title { font-weight: 600; }
.list-row__sub { font-size: 12px; color: var(--muted); margin-top: 2px; }
.list-row__chev { color: var(--muted); flex: none; }
.list-row__chev svg { width: 18px; height: 18px; display: block; }

/* ---------- 報表表格 ---------- */
.report-head, .report-row {
  display: grid; grid-template-columns: 1.5fr 1fr 1fr 1fr; gap: 6px; align-items: center;
  padding: 9px 0; border-bottom: 1px solid var(--border); font-size: 13px;
}
.report-head { font-size: 11px; font-weight: 700; color: var(--muted); letter-spacing: .02em; }
.report-head > span:not(:first-child), .report-row > span:not(:first-child) { text-align: right; }
.report-row:last-child { border-bottom: 0; }
.report-row.is-archived { opacity: .6; }
.report-row__name { font-weight: 600; display: flex; align-items: center; gap: 4px; flex-wrap: wrap; }
.report-row .is-income { color: var(--income); }
.report-row .is-expense { color: var(--expense); }
.report-row__bal { font-weight: 700; }
.report-row__bal.is-negative { color: var(--expense); }
.report-row--total { border-top: 2px solid var(--border); border-bottom: 0; font-weight: 800; padding-top: 11px; }

@media (min-width: 560px) {
  .hero__amount { font-size: 38px; }
  .view { padding-left: 20px; padding-right: 20px; }
  .report-head, .report-row { font-size: 14px; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
