/* ==================================================================
   記帳存摺 — 視覺設計（規格 3A）
   主題：銀行存摺。淡色紙張背景、橫格線明細、清楚的數字。
   ================================================================== */

:root {
  --paper: #eef2ef;
  --surface: #fbfcfb;
  --rule: #d5ddd8;
  --ink: #1f3442;
  --ink-soft: #627380;
  --gain: #c8413b;
  --gain-bg: #fbe9e7;
  --loss: #2e8a64;
  --loss-bg: #e3f3eb;
  --accent: #a87b2e;
  --accent-soft: #f1e7d2;
  --danger: #b3261e;

  --r-card: 14px;
  --r-ctrl: 10px;
  --r-pill: 999px;

  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);

  --topbar-h: 48px;
  --tabbar-h: 56px;

  --font-ui: -apple-system, BlinkMacSystemFont, "PingFang TC", "Noto Sans TC",
    "Microsoft JhengHei", sans-serif;
  --font-round: ui-rounded, -apple-system, BlinkMacSystemFont, "PingFang TC", sans-serif;
}

/* 深色：跟隨系統，或由設定強制 */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #141a1d;
    --surface: #1c2428;
    --rule: #2c373c;
    --ink: #e4eaec;
    --ink-soft: #93a3ab;
    --gain: #f2726a;
    --gain-bg: #3a2322;
    --loss: #4fc493;
    --loss-bg: #1b3129;
    --accent: #d6a955;
    --accent-soft: #33291a;
    --danger: #f2726a;
  }
}
:root[data-theme="dark"] {
  --paper: #141a1d;
  --surface: #1c2428;
  --rule: #2c373c;
  --ink: #e4eaec;
  --ink-soft: #93a3ab;
  --gain: #f2726a;
  --gain-bg: #3a2322;
  --loss: #4fc493;
  --loss-bg: #1b3129;
  --accent: #d6a955;
  --accent-soft: #33291a;
  --danger: #f2726a;
}

/* 國際慣例：獲利綠、虧損紅 */
:root[data-scheme="intl"] {
  --gain: #2e8a64;
  --gain-bg: #e3f3eb;
  --loss: #c8413b;
  --loss-bg: #fbe9e7;
}
@media (prefers-color-scheme: dark) {
  :root[data-scheme="intl"]:not([data-theme="light"]) {
    --gain: #4fc493;
    --gain-bg: #1b3129;
    --loss: #f2726a;
    --loss-bg: #3a2322;
  }
}
:root[data-theme="dark"][data-scheme="intl"] {
  --gain: #4fc493;
  --gain-bg: #1b3129;
  --loss: #f2726a;
  --loss-bg: #3a2322;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: 16px;
  line-height: 1.45;
  -webkit-text-size-adjust: 100%;
}

.num,
.amount,
table,
input[type="number"],
input[inputmode="decimal"] {
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum" 1;
}

button {
  font-family: inherit;
  font-size: inherit;
  color: inherit;
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
}

/* ------------------------------ 版面 ------------------------------ */

.app {
  display: flex;
  flex-direction: column;
  height: 100%;
  padding-left: var(--safe-l);
  padding-right: var(--safe-r);
}

.topbar {
  flex: none;
  height: calc(var(--topbar-h) + var(--safe-t));
  padding: var(--safe-t) 16px 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--paper);
  border-bottom: 1px solid transparent;
}

.topbar-title {
  font-size: 17px;
  font-weight: 700;
}

.topbar-actions {
  display: flex;
  gap: 4px;
}

.icon-btn {
  width: 36px;
  height: 36px;
  border-radius: var(--r-ctrl);
  display: grid;
  place-items: center;
  font-size: 18px;
  color: var(--ink-soft);
}
.icon-btn:active {
  background: var(--rule);
}

.banners {
  flex: none;
  padding: 0 16px;
}

.view {
  flex: 1 1 auto;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 4px 0 calc(var(--tabbar-h) + var(--safe-b) + 24px);
  outline: none;
}

.tabbar {
  flex: none;
  height: calc(var(--tabbar-h) + var(--safe-b));
  padding-bottom: var(--safe-b);
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  background: var(--surface);
  border-top: 1px solid var(--rule);
}

.tab {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  font-size: 12px;
  color: var(--ink-soft);
}
.tab .tab-ico {
  font-size: 17px;
  line-height: 1;
}
.tab[aria-selected="true"] {
  color: var(--accent);
  font-weight: 600;
}

.fab {
  position: fixed;
  right: calc(16px + var(--safe-r));
  bottom: calc(var(--tabbar-h) + var(--safe-b) + 16px);
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  font-size: 28px;
  line-height: 1;
  display: grid;
  place-items: center;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.18);
  z-index: 30;
}
.fab:active {
  transform: scale(0.94);
}

/* ------------------------------ 元件 ------------------------------ */

.card {
  background: var(--surface);
  border: 1px solid var(--rule);
  border-radius: var(--r-card);
  margin: 12px 16px;
  padding: 14px 16px;
}
.card.flush {
  padding: 0;
  overflow: hidden;
}

.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 8px;
}
.card-title {
  font-size: 17px;
  font-weight: 700;
}

.muted {
  color: var(--ink-soft);
  font-size: 13px;
}

.hero {
  text-align: left;
}
.hero-label {
  font-size: 13px;
  color: var(--ink-soft);
}
.hero-amount {
  font-family: var(--font-round);
  font-size: 36px;
  font-weight: 700;
  letter-spacing: -0.5px;
  line-height: 1.15;
  margin: 2px 0 6px;
  font-variant-numeric: tabular-nums;
}
.hero-sub {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
  font-size: 13px;
  color: var(--ink-soft);
}
.hero-sub b {
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.gain {
  color: var(--gain);
}
.loss {
  color: var(--loss);
}
.gain-bg {
  background: var(--gain-bg);
}
.loss-bg {
  background: var(--loss-bg);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 44px;
  padding: 0 16px;
  border-radius: var(--r-ctrl);
  background: var(--accent);
  color: #fff;
  font-size: 16px;
  font-weight: 600;
}
.btn:active {
  filter: brightness(0.94);
}
.btn.ghost {
  background: transparent;
  color: var(--accent);
  border: 1px solid var(--rule);
}
.btn.subtle {
  background: var(--accent-soft);
  color: var(--accent);
}
.btn.danger {
  background: transparent;
  color: var(--danger);
  border: 1px solid var(--rule);
}
.btn.block {
  width: 100%;
}
.btn.small {
  min-height: 34px;
  font-size: 14px;
  padding: 0 12px;
}

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.chip {
  min-height: 34px;
  padding: 4px 12px;
  border-radius: var(--r-pill);
  border: 1px solid var(--rule);
  background: var(--surface);
  color: var(--ink-soft);
  font-size: 14px;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.chip[aria-pressed="true"],
.chip.on {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-soft);
  font-weight: 600;
}
.chip .dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  flex: none;
}

.seg {
  display: inline-flex;
  background: var(--paper);
  border: 1px solid var(--rule);
  border-radius: var(--r-ctrl);
  padding: 2px;
  gap: 2px;
}
.seg button {
  min-height: 34px;
  padding: 0 14px;
  border-radius: 8px;
  font-size: 14px;
  color: var(--ink-soft);
}
.seg button[aria-pressed="true"] {
  background: var(--surface);
  color: var(--ink);
  font-weight: 600;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.06);
}

.banner {
  background: var(--accent-soft);
  border: 1px solid var(--rule);
  border-radius: var(--r-ctrl);
  padding: 10px 12px;
  margin: 8px 0;
  font-size: 14px;
  display: flex;
  align-items: center;
  gap: 10px;
}
.banner .grow {
  flex: 1;
}

/* ------------------------------ 表單 ------------------------------ */

.field {
  margin-bottom: 14px;
}
.field > label,
.field-label {
  display: block;
  font-size: 13px;
  color: var(--ink-soft);
  margin-bottom: 6px;
}

input[type="text"],
input[type="date"],
input[type="number"],
input[type="password"],
input[inputmode="decimal"],
select,
textarea {
  width: 100%;
  min-height: 44px;
  font-size: 16px; /* 不得小於 16px，避免 iOS 自動放大 */
  font-family: inherit;
  color: var(--ink);
  background: var(--paper);
  border: 1px solid var(--rule);
  border-radius: var(--r-ctrl);
  padding: 10px 12px;
  appearance: none;
  -webkit-appearance: none;
}
input:focus,
select:focus,
textarea:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}
select {
  background-image: linear-gradient(45deg, transparent 50%, var(--ink-soft) 50%),
    linear-gradient(135deg, var(--ink-soft) 50%, transparent 50%);
  background-position: calc(100% - 18px) 50%, calc(100% - 13px) 50%;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
  padding-right: 34px;
}
input[disabled],
select[disabled] {
  opacity: 0.6;
}
.amount-input {
  font-family: var(--font-round);
  font-size: 28px !important;
  font-weight: 700;
  text-align: right;
  min-height: 56px;
}
.row {
  display: flex;
  gap: 10px;
}
.row > * {
  flex: 1;
  min-width: 0;
}

/* ------------------------------ 存摺明細 ------------------------------ */

.passbook {
  background: var(--surface);
  border-top: 1px solid var(--rule);
  border-bottom: 1px solid var(--rule);
  margin-top: 12px;
}

.pb-month {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
  padding: 7px 16px;
  background: var(--paper);
  border-top: 1px solid var(--rule);
  border-bottom: 1px solid var(--rule);
  font-size: 13px;
  font-weight: 600;
  color: var(--ink-soft);
}
.pb-month .sub {
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.pb-row {
  position: relative;
  overflow: hidden;
  border-bottom: 1px solid var(--rule);
  background: var(--surface);
}
.pb-row:last-child {
  border-bottom: none;
}

.pb-swipe {
  position: absolute;
  inset: 0 0 0 auto;
  display: flex;
  z-index: 0;
}
.pb-swipe button {
  width: 72px;
  display: grid;
  place-items: center;
  font-size: 14px;
  color: #fff;
  font-weight: 600;
}
.pb-swipe .act-edit {
  background: var(--accent);
}
.pb-swipe .act-del {
  background: var(--danger);
}

.pb-body {
  position: relative;
  z-index: 1;
  background: var(--surface);
  width: 100%;
  text-align: left;
  padding: 9px 16px;
  display: block;
  transition: transform 0.2s ease;
  touch-action: pan-y;
}
.pb-row.swiped .pb-body {
  transform: translateX(-144px);
}
.pb-row.flash .pb-body {
  animation: rowflash 1s ease-out;
}
@keyframes rowflash {
  0%,
  60% {
    background: var(--flash, var(--gain-bg));
  }
  100% {
    background: var(--surface);
  }
}

.pb-line1,
.pb-line2 {
  display: flex;
  align-items: baseline;
  gap: 8px;
}
.pb-line2 {
  margin-top: 1px;
}
.pb-date {
  flex: none;
  width: 44px;
  font-size: 13px;
  color: var(--ink-soft);
  font-variant-numeric: tabular-nums;
}
.pb-desc {
  flex: 1;
  min-width: 0;
  font-size: 15px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.pb-sub {
  flex: 1;
  min-width: 0;
  font-size: 13px;
  color: var(--ink-soft);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.pb-amt {
  flex: none;
  font-size: 15px;
  font-weight: 600;
  text-align: right;
  font-variant-numeric: tabular-nums;
}
.pb-cum {
  flex: none;
  font-size: 13px;
  color: var(--ink-soft);
  text-align: right;
  font-variant-numeric: tabular-nums;
}
.pb-orig {
  font-size: 12px;
  color: var(--ink-soft);
}

.tagdot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  margin-right: 3px;
  vertical-align: 1px;
}

.empty {
  padding: 32px 16px;
  text-align: center;
  color: var(--ink-soft);
  font-size: 14px;
}

/* ------------------------------ 每日小計清單 ------------------------------ */

.daylist-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  width: 100%;
  padding: 13px 16px;
  text-align: left;
}

.day-row {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 11px 16px;
  text-align: left;
  border-top: 1px solid var(--rule);
  background: var(--surface);
}
.day-row:active {
  filter: brightness(0.97);
}
.day-date {
  flex: none;
  min-width: 52px;
  font-size: 15px;
  font-variant-numeric: tabular-nums;
}
.day-dow {
  flex: none;
  width: 16px;
  font-size: 13px;
  color: var(--ink-soft);
}
.day-count {
  flex: 1;
  font-size: 13px;
  color: var(--ink-soft);
}
.day-amt {
  flex: none;
  font-size: 15px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}
.day-row.today .day-date {
  font-weight: 700;
  text-decoration: underline;
  text-underline-offset: 2px;
}

.daylist-more {
  display: block;
  width: 100%;
  padding: 12px;
  border-top: 1px solid var(--rule);
  font-size: 14px;
  color: var(--accent);
  background: var(--surface);
}

/* ------------------------------ 圖表 ------------------------------ */

.chart-wrap {
  position: relative;
}
.chart-wrap canvas {
  display: block;
  width: 100%;
  height: 160px;
  touch-action: pan-y;
}
.chart-tip {
  position: absolute;
  pointer-events: none;
  background: var(--ink);
  color: var(--paper);
  font-size: 12px;
  padding: 4px 8px;
  border-radius: 6px;
  white-space: nowrap;
  transform: translate(-50%, -130%);
  opacity: 0;
  transition: opacity 0.12s;
  font-variant-numeric: tabular-nums;
}
.chart-tip.on {
  opacity: 1;
}

/* ------------------------------ 清單 ------------------------------ */

.list {
  background: var(--surface);
  border: 1px solid var(--rule);
  border-radius: var(--r-card);
  margin: 12px 16px;
  overflow: hidden;
}
.list-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  text-align: left;
  padding: 12px 14px;
  border-bottom: 1px solid var(--rule);
  background: var(--surface);
}
.list-item:last-child {
  border-bottom: none;
}
.list-item:active {
  background: var(--paper);
}
.list-item .grow {
  flex: 1;
  min-width: 0;
}
.list-item .t1 {
  font-size: 15px;
}
.list-item .t2 {
  font-size: 13px;
  color: var(--ink-soft);
}
.list-item .r1 {
  font-size: 15px;
  font-weight: 600;
  text-align: right;
  font-variant-numeric: tabular-nums;
}
.list-item .r2 {
  font-size: 12px;
  color: var(--ink-soft);
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.kv {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 7px 0;
  border-bottom: 1px dashed var(--rule);
  font-size: 15px;
}
.kv:last-child {
  border-bottom: none;
}
.kv .k {
  color: var(--ink-soft);
  font-size: 14px;
}
.kv .v {
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.progress {
  height: 8px;
  background: var(--rule);
  border-radius: var(--r-pill);
  overflow: hidden;
  margin-top: 8px;
}
.progress > i {
  display: block;
  height: 100%;
  background: var(--accent);
  border-radius: var(--r-pill);
}

/* ------------------------------ Sheet / Modal ------------------------------ */

.sheet-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
  z-index: 100;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  animation: fade 0.18s ease;
}
@keyframes fade {
  from {
    opacity: 0;
  }
}

.sheet {
  width: 100%;
  max-width: 560px;
  max-height: 92vh;
  background: var(--surface);
  border-radius: 18px 18px 0 0;
  display: flex;
  flex-direction: column;
  padding-bottom: var(--safe-b);
  animation: slideup 0.22s cubic-bezier(0.2, 0.8, 0.3, 1);
}
@keyframes slideup {
  from {
    transform: translateY(24px);
    opacity: 0.6;
  }
}

.sheet-head {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--rule);
}
.sheet-head .t {
  font-size: 17px;
  font-weight: 700;
}
.sheet-body {
  flex: 1 1 auto;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 14px 16px;
}
.sheet-foot {
  flex: none;
  display: flex;
  gap: 10px;
  padding: 10px 16px 14px;
  border-top: 1px solid var(--rule);
}
.sheet-foot > * {
  flex: 1;
}

.toast-root {
  position: fixed;
  left: 0;
  right: 0;
  bottom: calc(var(--tabbar-h) + var(--safe-b) + 12px);
  z-index: 200;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  pointer-events: none;
}
.toast {
  pointer-events: auto;
  background: var(--ink);
  color: var(--paper);
  border-radius: var(--r-pill);
  padding: 10px 14px;
  font-size: 14px;
  display: flex;
  align-items: center;
  gap: 12px;
  max-width: calc(100vw - 32px);
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.22);
  animation: fade 0.16s ease;
}
.toast button {
  color: var(--accent);
  font-weight: 700;
}

/* ------------------------------ 鎖定畫面 ------------------------------ */

.lock {
  position: fixed;
  inset: 0;
  background: var(--paper);
  z-index: 500;
  display: grid;
  place-items: center;
  padding: 24px;
}
.lock-inner {
  width: 100%;
  max-width: 300px;
  text-align: center;
}
.lock-title {
  font-size: 17px;
  font-weight: 700;
  margin-bottom: 18px;
}
.pin-dots {
  display: flex;
  justify-content: center;
  gap: 14px;
  margin-bottom: 24px;
  min-height: 14px;
}
.pin-dots i {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  border: 1.5px solid var(--ink-soft);
}
.pin-dots i.on {
  background: var(--accent);
  border-color: var(--accent);
}
.pin-pad {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
}
.pin-pad button {
  height: 60px;
  border-radius: var(--r-card);
  background: var(--surface);
  border: 1px solid var(--rule);
  font-size: 22px;
  font-family: var(--font-round);
}
.pin-error {
  margin-top: 14px;
  color: var(--danger);
  font-size: 14px;
  min-height: 20px;
}

/* ------------------------------ 其他 ------------------------------ */

.private .hide-amount {
  filter: none;
}
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
}

.color-grid {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 8px;
}
.color-grid button {
  aspect-ratio: 1;
  border-radius: 50%;
  border: 2px solid transparent;
}
.color-grid button[aria-pressed="true"] {
  border-color: var(--ink);
}

.file-drop {
  display: block;
  border: 1px dashed var(--rule);
  border-radius: var(--r-ctrl);
  padding: 18px;
  text-align: center;
  color: var(--ink-soft);
  font-size: 14px;
}

hr.sep {
  border: none;
  border-top: 1px solid var(--rule);
  margin: 16px 0;
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.001ms !important;
    transition-duration: 0.001ms !important;
  }
}

/* hidden 屬性必須贏過 .app / .lock 的 display 設定 */
[hidden] {
  display: none !important;
}

/* 錯誤訊息浮在最上層。放在 #app 外面，啟動失敗（#app 仍隱藏）時也看得見。 */
.error-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 400;
  padding: calc(var(--safe-t) + 8px) 12px 0;
  pointer-events: none;
}
.error-overlay:empty {
  display: none;
}
.error-overlay .banner {
  pointer-events: auto;
  background: var(--surface);
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.25);
}

/* 啟動中的提示 */
.booting {
  padding: 40px 16px;
  text-align: center;
  color: var(--ink-soft);
  font-size: 14px;
}
