:root {
  --bg: #FAF6EF;
  --surface: #FFFFFF;
  --surface-2: #F4EDE2;
  --line: #E9DFD0;
  --text: #2B2118;
  --muted: #7C6F61;
  --primary: #A8651E;
  --primary-dark: #83491A;
  --primary-soft: #F6E7D2;
  --success: #2F7D4F;
  --success-soft: #E3F2E8;
  --danger: #C0392B;
  --danger-soft: #FBE6E3;
  --warn: #B7791F;
  --warn-soft: #FDF1DC;
  --lock: #6B6259;
  --lock-soft: #EEE9E3;
  --radius: 14px;
  --radius-sm: 10px;
  --shadow: 0 1px 2px rgba(43, 33, 24, .05), 0 4px 16px rgba(43, 33, 24, .06);
  --topbar-h: 56px;
  --tabbar-h: 64px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "PingFang HK", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; background: none; border: 0; }
img { display: block; max-width: 100%; }
svg { width: 22px; height: 22px; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

.muted { color: var(--muted); }
.small { font-size: 13px; }
.strong { font-weight: 700; }
.danger { color: var(--danger); }

/* ---------- 按鈕 ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 0 18px; border-radius: 12px;
  font-weight: 600; font-size: 15px; transition: background .15s, opacity .15s;
}
.btn:disabled { opacity: .5; cursor: default; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:active { background: var(--primary-dark); }
.btn-soft { background: var(--primary-soft); color: var(--primary-dark); }
.btn-ghost { background: transparent; color: var(--primary-dark); border: 1px solid var(--line); }
.btn-sm { min-height: 34px; padding: 0 12px; font-size: 13px; border-radius: 10px; }
.btn-block { width: 100%; }

.icon-btn {
  width: 40px; height: 40px; border-radius: 12px;
  display: inline-flex; align-items: center; justify-content: center; color: var(--text);
}
.icon-btn:active { background: var(--surface-2); }

/* ---------- 登入 ---------- */
.login-view {
  min-height: 100%; display: flex; align-items: center; justify-content: center; padding: 24px;
  background: radial-gradient(circle at 50% 0%, #F3E2C7 0%, var(--bg) 60%);
}
.login-card {
  width: 100%; max-width: 380px; background: var(--surface); border-radius: 20px;
  padding: 32px 24px; box-shadow: var(--shadow); text-align: center;
}
.login-logo { font-size: 48px; line-height: 1; }
.login-card h1 { font-size: 24px; margin: 10px 0 2px; }
.login-card form { margin-top: 18px; text-align: left; display: grid; gap: 14px; }
.auth-tabs { display: flex; background: var(--surface-2); border-radius: 12px; padding: 4px; gap: 4px; margin-top: 18px; }
.auth-tab {
  flex: 1; min-height: 38px; border-radius: 9px; font-size: 14px; font-weight: 600; color: var(--muted);
  cursor: pointer; transition: background .15s, color .15s;
}
.auth-tab.on { background: var(--surface); color: var(--primary-dark); box-shadow: 0 1px 3px rgba(0,0,0,.08); font-weight: 700; }
.auth-hint { text-align: center; margin-top: 6px; }

/* ---------- 表單 ---------- */
.field { display: grid; gap: 6px; }
.field > span { font-size: 14px; font-weight: 600; color: var(--muted); }
.field input, .field textarea, .select {
  width: 100%; min-height: 46px; padding: 10px 14px;
  border: 1px solid var(--line); border-radius: var(--radius-sm); background: var(--surface);
  outline: none; transition: border-color .15s;
}
.field textarea { resize: vertical; min-height: 64px; }
.field input:focus, .field textarea:focus, .select:focus { border-color: var(--primary); }
.field-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.form-error { color: var(--danger); font-size: 14px; }
.check-line { display: flex; gap: 10px; align-items: flex-start; font-size: 15px; }
.check-line input { width: 20px; height: 20px; margin-top: 2px; accent-color: var(--primary); flex: none; }
.check-line em { font-style: normal; }

.segmented {
  display: flex; background: var(--surface-2); border-radius: 12px; padding: 4px; gap: 4px;
}
.segmented button {
  flex: 1; min-height: 38px; border-radius: 9px; font-size: 14px; font-weight: 600; color: var(--muted);
}
.segmented button.on { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.08); }

.chip-row { display: flex; gap: 8px; overflow-x: auto; scrollbar-width: none; }
.chip-row::-webkit-scrollbar { display: none; }
.chip-row.wrap { flex-wrap: wrap; overflow: visible; }
.chip {
  flex: none; min-height: 36px; padding: 0 14px; border-radius: 999px;
  border: 1px solid var(--line); background: var(--surface); font-size: 14px; white-space: nowrap;
}
.chip.on { background: var(--primary-soft); border-color: var(--primary); color: var(--primary-dark); font-weight: 600; }

/* ---------- 版面 ---------- */
.app { min-height: 100%; }
.topbar {
  position: sticky; top: 0; z-index: 20; height: var(--topbar-h);
  display: flex; align-items: center; gap: 4px; padding: 0 8px 0 16px;
  background: rgba(250, 246, 239, .92); backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.topbar-title { flex: 1; font-size: 18px; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#backBtn:not([hidden]) + .topbar-title { margin-left: -4px; }
.topbar:has(#backBtn:not([hidden])) { padding-left: 4px; }
.topbar-actions { display: flex; align-items: center; gap: 2px; }
.avatar-btn {
  width: 34px; height: 34px; border-radius: 50%; margin-left: 4px;
  background: var(--primary); color: #fff; font-weight: 700; font-size: 15px;
}
.avatar-btn.admin { background: var(--text); }

.main {
  max-width: 720px; margin: 0 auto;
  padding: 16px 16px calc(var(--tabbar-h) + var(--safe-b) + 24px);
}
.view { display: none; }
.view.active { display: block; }

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  height: calc(var(--tabbar-h) + var(--safe-b)); padding-bottom: var(--safe-b);
  display: flex; background: var(--surface); border-top: 1px solid var(--line);
}
.tabbar button {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  color: var(--muted); font-size: 12px;
}
.tabbar em { font-style: normal; font-weight: 600; }
.tabbar button.on { color: var(--primary); }
.tabbar button.on svg { stroke-width: 2.4; }

.section-title { font-size: 15px; font-weight: 700; color: var(--muted); margin: 22px 2px 10px; }
.stack { display: grid; gap: 12px; }
.card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; }
.empty { text-align: center; color: var(--muted); padding: 28px 12px; font-size: 14px; }

/* ---------- 狀態標籤 ---------- */
.pill {
  display: inline-flex; align-items: center; gap: 4px; padding: 2px 10px; border-radius: 999px;
  font-size: 12px; font-weight: 700; white-space: nowrap;
}
.pill.paid { background: var(--success-soft); color: var(--success); }
.pill.unpaid { background: var(--danger-soft); color: var(--danger); }
.pill.partial { background: var(--warn-soft); color: var(--warn); }
.pill.lock { background: var(--lock-soft); color: var(--lock); }
.pill.gray { background: var(--surface-2); color: var(--muted); }
.pill svg { width: 12px; height: 12px; }

/* ---------- 首頁 ---------- */
.hello { font-size: 22px; font-weight: 700; margin: 4px 2px 14px; }
.stat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.stat { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px 16px; }
.stat-label { font-size: 13px; color: var(--muted); font-weight: 600; }
.stat-value { font-size: 24px; font-weight: 800; margin-top: 2px; letter-spacing: -.3px; }
.stat-value.red { color: var(--danger); }
.stat-value.green { color: var(--success); }
.stat-value.gold { color: var(--primary); }

.quick-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-top: 12px; }
.quick {
  background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 14px 8px; display: flex; flex-direction: column; align-items: center; gap: 6px;
  font-size: 14px; font-weight: 600;
}
.quick span:first-child {
  width: 42px; height: 42px; border-radius: 12px; background: var(--primary-soft); color: var(--primary-dark);
  display: flex; align-items: center; justify-content: center;
}

.list { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.list-row {
  width: 100%; min-height: 52px; padding: 12px 16px; display: flex; align-items: center; justify-content: space-between; gap: 10px;
  border-bottom: 1px solid var(--line); text-align: left; font-size: 15px;
}
.list-row:last-child { border-bottom: 0; }
.list-row.danger { color: var(--danger); justify-content: center; font-weight: 600; }
.list-row svg { color: var(--muted); width: 18px; height: 18px; }
.list-row .amt { font-weight: 700; }
.staff-title { margin: 18px 0 8px; }
.staff-row em { font-style: normal; }

/* ---------- 夾錢 ---------- */
.drawer { padding: 0; overflow: hidden; }
.drawer-head {
  width: 100%; min-height: 54px; padding: 0 16px; display: flex; align-items: center; gap: 10px;
  font-size: 16px; font-weight: 700; color: var(--primary-dark);
}
.tri { display: inline-block; font-size: 12px; transition: transform .2s; }
.drawer.open .tri { transform: rotate(90deg); }
.drawer-body { display: none; padding: 4px 16px 18px; gap: 14px; }
.drawer.open .drawer-body { display: grid; }

.event { display: grid; gap: 10px; }
.event-top { display: flex; justify-content: space-between; gap: 10px; align-items: flex-start; }
.event-title { font-size: 17px; font-weight: 700; }
.event-price { font-size: 14px; color: var(--primary-dark); font-weight: 700; white-space: nowrap; }
.progress { height: 8px; background: var(--surface-2); border-radius: 99px; overflow: hidden; }
.progress > i { display: block; height: 100%; background: var(--primary); border-radius: 99px; }
.event-money { display: flex; justify-content: space-between; font-size: 14px; }
.meta { display: grid; gap: 4px; font-size: 14px; color: var(--muted); }
.meta b { color: var(--text); font-weight: 600; }
.account-info {
  display: flex; justify-content: space-between; align-items: center; gap: 8px;
  background: var(--surface-2); border-radius: var(--radius-sm); padding: 8px 12px; font-size: 14px;
}
details.people { border-top: 1px solid var(--line); padding-top: 8px; }
details.people summary { list-style: none; cursor: pointer; font-size: 14px; font-weight: 600; color: var(--muted); display: flex; justify-content: space-between; }
details.people summary::-webkit-details-marker { display: none; }
details.people summary::after { content: "▾"; }
details.people[open] summary::after { content: "▴"; }
.person { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 8px 0; border-bottom: 1px dashed var(--line); font-size: 14px; }
.person:last-child { border-bottom: 0; }
.person.me { font-weight: 700; }
.my-share {
  background: var(--primary-soft); border-radius: var(--radius-sm); padding: 10px 12px;
  display: flex; justify-content: space-between; align-items: center; gap: 8px; font-size: 14px;
}

.stepper { display: flex; align-items: center; gap: 8px; }
.stepper button {
  width: 46px; height: 46px; border-radius: 12px; background: var(--surface-2); font-size: 22px; font-weight: 700;
}
.stepper input { flex: 1; text-align: center; font-size: 20px; font-weight: 700; min-height: 46px; border: 1px solid var(--line); border-radius: 12px; }
.join-summary { background: var(--surface-2); border-radius: var(--radius-sm); padding: 12px; }

/* ---------- 日曆 ---------- */
.cal-card { padding: 12px; }
.cal-head { display: flex; align-items: center; gap: 4px; margin-bottom: 8px; }
.cal-title { flex: 1; text-align: center; font-size: 17px; font-weight: 700; }
.cal-weekdays, .cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); }
.cal-weekdays span { text-align: center; font-size: 12px; color: var(--muted); font-weight: 600; padding: 4px 0; }
.cal-cell {
  min-height: 52px; border-top: 1px solid var(--line); padding: 4px 2px; text-align: center;
  display: flex; flex-direction: column; align-items: center; gap: 3px; cursor: pointer;
}
.cal-cell.pad { cursor: default; }
.cal-num { width: 28px; height: 28px; line-height: 28px; border-radius: 50%; font-size: 14px; }
.cal-cell.today .cal-num { background: var(--primary); color: #fff; font-weight: 700; }
.cal-cell.sel .cal-num { outline: 2px solid var(--primary); }
.cal-dots { display: flex; gap: 3px; flex-wrap: wrap; justify-content: center; }
.cal-dots i { width: 6px; height: 6px; border-radius: 50%; }
.cal-tags { display: none; width: 100%; }
.cal-tag { font-size: 11px; color: #fff; border-radius: 4px; padding: 1px 4px; margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; text-align: left; }
.agenda { display: flex; gap: 12px; align-items: flex-start; }
.agenda-bar { width: 4px; align-self: stretch; border-radius: 4px; flex: none; }
.agenda-date { font-size: 12px; color: var(--muted); }
.agenda-title { font-weight: 600; }

/* ---------- 賬目 ---------- */
.ledger-filters { display: grid; gap: 10px; margin-bottom: 14px; }
.order-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; }
.order-id { font-size: 13px; color: var(--muted); }
.order-total { font-size: 18px; font-weight: 800; }
.item { display: flex; gap: 12px; align-items: center; padding: 10px 0; border-top: 1px solid var(--line); }
.thumb {
  width: 52px; height: 52px; border-radius: 10px; object-fit: cover; flex: none; background: var(--surface-2);
  display: flex; align-items: center; justify-content: center; font-size: 22px; cursor: zoom-in;
}
.item-main { flex: 1; min-width: 0; }
.item-name { font-weight: 600; font-size: 15px; }
.item-sub { font-size: 13px; color: var(--muted); }
.item-side { display: flex; flex-direction: column; align-items: flex-end; gap: 6px; }

.photo-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.photo-grid.small { grid-template-columns: repeat(4, 1fr); }
.photo { position: relative; border-radius: 10px; overflow: hidden; background: var(--surface-2); aspect-ratio: 1; cursor: zoom-in; }
.photo img { width: 100%; height: 100%; object-fit: cover; }
.photo figcaption {
  position: absolute; left: 0; right: 0; bottom: 0; padding: 14px 6px 4px; font-size: 11px; color: #fff;
  background: linear-gradient(transparent, rgba(0,0,0,.65)); white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

/* ---------- 搜尋 ---------- */
.search-box {
  display: flex; align-items: center; gap: 8px; background: var(--surface); border: 1px solid var(--line);
  border-radius: 12px; padding: 0 12px; min-height: 46px; margin: 12px 0;
}
.search-box:first-child { margin-top: 0; }
.search-box input { flex: 1; border: 0; outline: none; background: transparent; min-height: 44px; }
.search-box svg { color: var(--muted); width: 18px; height: 18px; }
.result { display: flex; gap: 12px; align-items: center; }

/* ---------- 對話 ---------- */
.main:has(.view-chat.active) { padding-bottom: 0; }
.view-chat.active {
  display: flex; flex-direction: column;
  height: calc(100dvh - var(--topbar-h) - var(--tabbar-h) - var(--safe-b) - 16px);
}
.chat-scroll { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 10px; padding-bottom: 12px; }
.bubble { max-width: 86%; padding: 10px 14px; border-radius: 16px; font-size: 15px; word-break: break-word; }
.bubble.ai { background: var(--surface); box-shadow: var(--shadow); border-top-left-radius: 4px; align-self: flex-start; }
.bubble.me { background: var(--primary); color: #fff; border-top-right-radius: 4px; align-self: flex-end; }
.bubble.me img { border-radius: 10px; margin-top: 6px; max-height: 180px; }
.bubble.typing { color: var(--muted); }
.opt-list { display: grid; gap: 8px; margin-top: 10px; }
.opt-list button {
  text-align: left; padding: 10px 12px; border-radius: 12px; border: 1px solid var(--primary);
  background: var(--primary-soft); color: var(--primary-dark); font-size: 14px; font-weight: 600;
}
.confirm-card { margin-top: 10px; border: 1px solid var(--line); border-radius: 12px; padding: 12px; background: var(--bg); display: grid; gap: 10px; }
.confirm-card h4 { font-size: 15px; }
.confirm-row { display: flex; justify-content: space-between; gap: 8px; font-size: 14px; }
.confirm-actions { display: flex; gap: 8px; }
.confirm-actions .btn { flex: 1; }
.confirm-done { font-size: 14px; font-weight: 700; color: var(--success); }
.confirm-done.cancel { color: var(--muted); }

.composer { border-top: 1px solid var(--line); padding: 10px 0 12px; display: grid; gap: 8px; background: var(--bg); }
.composer-row { display: flex; gap: 8px; align-items: flex-end; }
.composer-row textarea {
  flex: 1; resize: none; max-height: 120px; min-height: 44px; padding: 10px 14px;
  border: 1px solid var(--line); border-radius: 22px; background: var(--surface); outline: none;
}
.composer-row textarea:focus { border-color: var(--primary); }
.composer-attach { cursor: pointer; flex: none; width: 44px; height: 44px; color: var(--primary-dark); }
.send-btn { width: 44px; height: 44px; border-radius: 50%; background: var(--primary); color: #fff; display: flex; align-items: center; justify-content: center; flex: none; }
.send-btn:disabled { opacity: .5; }
.send-btn svg { width: 20px; height: 20px; }
.attach-preview { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); }
.attach-preview img { width: 44px; height: 44px; border-radius: 8px; object-fit: cover; }

/* ---------- 彈出表 ---------- */
.sheet { position: fixed; inset: 0; z-index: 50; background: rgba(43, 33, 24, .4); display: flex; align-items: flex-end; justify-content: center; }
.sheet-panel {
  width: 100%; max-width: 560px; max-height: 92dvh; overflow-y: auto; background: var(--surface);
  border-radius: 20px 20px 0 0; padding-bottom: calc(16px + var(--safe-b)); animation: up .2s ease-out;
}
@keyframes up { from { transform: translateY(40px); opacity: 0; } to { transform: none; opacity: 1; } }
.sheet-head { position: sticky; top: 0; background: var(--surface); display: flex; align-items: center; justify-content: space-between; padding: 14px 12px 8px 20px; z-index: 1; }
.sheet-head h3 { font-size: 18px; }
.sheet-body { padding: 4px 20px 8px; display: grid; gap: 14px; }

.photo-pick {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px;
  min-height: 110px; border: 2px dashed var(--line); border-radius: var(--radius); color: var(--primary-dark);
  font-weight: 600; cursor: pointer; background: var(--bg);
}
.photo-pick svg { width: 30px; height: 30px; }

.match-card { border: 1px solid var(--primary); background: var(--primary-soft); border-radius: 12px; padding: 12px; display: grid; gap: 10px; font-size: 14px; }
.match-card .btn { flex: 1; }

/* ---------- 其他 ---------- */
.lightbox { position: fixed; inset: 0; z-index: 60; background: rgba(0,0,0,.88); display: flex; align-items: center; justify-content: center; padding: 16px; }
.lightbox img { max-height: 100%; border-radius: 8px; }
.toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + var(--safe-b) + 16px); transform: translateX(-50%);
  z-index: 70; background: var(--text); color: #fff; padding: 10px 18px; border-radius: 999px; font-size: 14px;
  box-shadow: var(--shadow); max-width: 90vw; text-align: center;
}
.toast.err { background: var(--danger); }

.tree details { border-radius: var(--radius-sm); }
.tree > details { background: var(--surface); box-shadow: var(--shadow); border-radius: var(--radius); padding: 4px 14px; }
.tree summary { cursor: pointer; padding: 10px 0; font-weight: 700; list-style: none; display: flex; justify-content: space-between; gap: 8px; }
.tree summary::-webkit-details-marker { display: none; }
.tree summary .count { color: var(--muted); font-weight: 600; font-size: 13px; }
.tree details details { padding-left: 12px; border-left: 2px solid var(--line); margin-bottom: 6px; }
.tree details details summary { font-weight: 600; font-size: 15px; }
.prod { display: grid; gap: 6px; padding: 10px 0; border-top: 1px dashed var(--line); font-size: 14px; }
.prod-top { display: flex; gap: 10px; align-items: center; }
.tags { display: flex; flex-wrap: wrap; gap: 6px; }

table.costs { width: 100%; border-collapse: collapse; font-size: 14px; }
table.costs th { text-align: left; color: var(--muted); font-weight: 600; padding: 6px 4px; border-bottom: 1px solid var(--line); }
table.costs td { padding: 8px 4px; border-bottom: 1px dashed var(--line); }
table.costs .r { text-align: right; }

@media (min-width: 720px) {
  .cal-cell { min-height: 96px; align-items: stretch; text-align: left; padding: 4px; }
  .cal-num { align-self: flex-start; }
  .cal-dots { display: none; }
  .cal-tags { display: block; }
  .sheet { align-items: center; }
  .sheet-panel { border-radius: 20px; }
  .quick-grid { grid-template-columns: repeat(3, 1fr); }
  .stat-grid.four { grid-template-columns: repeat(4, 1fr); }
}
