:root {
  --bg: #f4f5f7;
  --card: #ffffff;
  --text: #1a1a1a;
  --muted: #6b7280;
  --border: #e5e7eb;
  --brand: #0f766e;
  --brand-ink: #ffffff;
  --pos: #047857;   /* 別人欠他 / 收 */
  --neg: #dc2626;   /* 他欠別人 / 付 */
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
  --radius: 14px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b0f12;
    --card: #171c20;
    --text: #f3f4f6;
    --muted: #9aa3ad;
    --border: #2a3138;
    --brand: #14b8a6;
    --pos: #34d399;
    --neg: #f87171;
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang TC",
    "Microsoft JhengHei", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 16px;
  line-height: 1.45;
  -webkit-text-size-adjust: 100%;
}

#app { max-width: 640px; margin: 0 auto; min-height: 100vh; }

button {
  font: inherit;
  cursor: pointer;
  border: none;
  background: none;
  color: inherit;
}

input, select {
  font: inherit;
  color: var(--text);
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px;
  width: 100%;
}
input:focus, select:focus { outline: 2px solid var(--brand); outline-offset: -1px; }

/* ---------- 通用元件 ---------- */
.btn {
  background: var(--brand);
  color: var(--brand-ink);
  border-radius: 10px;
  padding: 13px 16px;
  font-weight: 600;
  text-align: center;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}
.btn:active { filter: brightness(0.92); }
.btn:disabled { opacity: 0.5; }
.btn-ghost { background: transparent; color: var(--brand); border: 1px solid var(--border); }
.btn-sm { padding: 8px 12px; font-size: 14px; border-radius: 8px; }
.btn-block { width: 100%; }

.card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
}

.muted { color: var(--muted); }
.row { display: flex; align-items: center; gap: 10px; }
.row-between { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.pos { color: var(--pos); }
.neg { color: var(--neg); }
.field { margin-bottom: 14px; }
.field > label { display: block; font-size: 14px; color: var(--muted); margin-bottom: 6px; }
.amount-num { font-variant-numeric: tabular-nums; }

/* ---------- 首頁 ---------- */
.home { padding: 24px 16px calc(40px + var(--safe-b)); }
.home h1 { font-size: 26px; margin: 8px 0 4px; }
.home .tagline { color: var(--muted); margin: 0 0 24px; }
.recent-item {
  display: flex; justify-content: space-between; align-items: center;
  padding: 14px 16px; background: var(--card); border-radius: 10px;
  box-shadow: var(--shadow); margin-bottom: 8px;
}

/* ---------- 群組畫面 ---------- */
.gp-header {
  position: sticky; top: 0; z-index: 5;
  background: var(--brand); color: var(--brand-ink);
  padding: calc(14px + env(safe-area-inset-top, 0px)) 16px 16px;
}
.gp-header .gname { font-size: 19px; font-weight: 700; }
.gp-header .gtotal { font-size: 13px; opacity: 0.9; }
.gp-header .icon-btn { color: var(--brand-ink); font-size: 14px; padding: 8px; border-radius: 8px; }

.tab-body { padding: 16px 16px calc(96px + var(--safe-b)); }

/* 列表項目 */
.entry {
  display: flex; align-items: center; gap: 12px;
  background: var(--card); border-radius: 12px; padding: 13px 14px;
  box-shadow: var(--shadow); margin-bottom: 8px;
}
.entry .ico {
  width: 38px; height: 38px; border-radius: 50%; flex: none;
  display: grid; place-items: center; font-size: 18px;
  background: var(--bg);
}
.entry .body { flex: 1; min-width: 0; }
.entry .title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.entry .sub { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.entry .amt { font-weight: 700; white-space: nowrap; }

.bal-row { display: flex; justify-content: space-between; padding: 12px 4px; border-bottom: 1px solid var(--border); }
.bal-row:last-child { border-bottom: none; }

.settle-row {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 0; border-bottom: 1px solid var(--border);
}
.settle-row:last-child { border-bottom: none; }
.settle-row .who { flex: 1; }

.empty { text-align: center; color: var(--muted); padding: 48px 16px; }
.empty .big { font-size: 40px; margin-bottom: 8px; }

.section-title { font-size: 14px; color: var(--muted); margin: 22px 4px 10px; font-weight: 600; }

/* 成員 chip 勾選 */
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  border: 1px solid var(--border); border-radius: 999px; padding: 8px 14px;
  background: var(--card); font-size: 14px;
}
.chip.on { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }

/* 底部導覽 */
.bottom-nav {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 20;
  display: flex; background: var(--card); border-top: 1px solid var(--border);
  padding-bottom: var(--safe-b);
  max-width: 640px; margin: 0 auto;
}
.bottom-nav button {
  flex: 1; padding: 10px 0 8px; font-size: 11px; color: var(--muted);
  display: flex; flex-direction: column; align-items: center; gap: 2px;
}
.bottom-nav button .ic { font-size: 20px; }
.bottom-nav button.on { color: var(--brand); font-weight: 600; }

/* 浮動新增鈕 */
.fab {
  position: fixed; right: 18px; bottom: calc(70px + var(--safe-b)); z-index: 25;
  width: 56px; height: 56px; border-radius: 50%;
  background: var(--brand); color: var(--brand-ink);
  font-size: 30px; line-height: 1; box-shadow: 0 4px 12px rgba(0,0,0,.25);
  display: grid; place-items: center;
}

/* 底部彈出表單 */
.sheet-backdrop {
  position: fixed; inset: 0; z-index: 40;
  background: rgba(0,0,0,.45);
  display: flex; align-items: flex-end; justify-content: center;
}
.sheet {
  background: var(--bg); width: 100%; max-width: 640px;
  border-radius: 18px 18px 0 0; max-height: 92vh; overflow-y: auto;
  padding: 8px 16px calc(24px + var(--safe-b));
  animation: slideup .18s ease-out;
}
@keyframes slideup { from { transform: translateY(100%); } to { transform: translateY(0); } }
.sheet-grip { width: 40px; height: 4px; border-radius: 2px; background: var(--border); margin: 8px auto 14px; }
.sheet h2 { font-size: 18px; margin: 0 0 16px; }

/* 分段控制 */
.segment { display: flex; background: var(--bg); border: 1px solid var(--border); border-radius: 10px; padding: 3px; margin-bottom: 16px; }
.segment button { flex: 1; padding: 9px; border-radius: 8px; font-weight: 600; color: var(--muted); }
.segment button.on { background: var(--card); color: var(--text); box-shadow: var(--shadow); }

.split-line { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
.split-line .nm { flex: 1; }
.split-line input { width: 110px; text-align: right; }

.remain { font-size: 13px; text-align: right; margin-top: 4px; }

/* Toast */
#toast {
  position: fixed; left: 50%; bottom: calc(90px + var(--safe-b)); transform: translateX(-50%);
  z-index: 60; pointer-events: none;
}
#toast .msg {
  background: #111827; color: #fff; padding: 10px 16px; border-radius: 999px;
  font-size: 14px; box-shadow: 0 4px 14px rgba(0,0,0,.3); margin-top: 8px;
  animation: fade .25s;
}
#toast .msg.err { background: var(--neg); }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
