:root {
  --green: #2f6b3a;
  --green-soft: #e6f0e7;
  --income: #1b8a4b;
  --expense: #c8372d;
  --blue: #2463c7;
  --bg: #f4f6f3;
  --card: #ffffff;
  --text: #1d231e;
  --muted: #6b746d;
  --line: #e2e6e1;
  --radius: 14px;
  --tabbar-h: 58px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  font-family: -apple-system, BlinkMacSystemFont, "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  -webkit-text-size-adjust: 100%;
  color-scheme: light;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
html, body { margin: 0; background: var(--bg); color: var(--text); font-size: 16px; }
body { min-height: 100vh; padding-bottom: calc(var(--tabbar-h) + var(--safe-b)); }
body.no-tabbar { padding-bottom: var(--safe-b); }
a { color: var(--blue); text-decoration: none; }
button, input, select, textarea { font: inherit; color: inherit; }

/* ---------- 頂部 ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: grid; grid-template-columns: 56px 1fr 56px; align-items: center;
  padding: var(--safe-t) 4px 0; min-height: calc(48px + var(--safe-t));
  background: rgba(244, 246, 243, .92); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--line);
}
.topbar h1 { grid-column: 2; margin: 0; font-size: 17px; font-weight: 600; text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-back { grid-column: 1; justify-self: start; border: 0; background: none; color: var(--blue); font-size: 34px; line-height: 1; padding: 0 12px 4px; min-height: 44px; }
.topbar-action { grid-column: 3; justify-self: end; }
.topbar-action button { border: 0; background: none; color: var(--blue); font-size: 16px; padding: 0 12px; min-height: 44px; }

main { padding: 12px 14px 24px; max-width: 640px; margin: 0 auto; }

/* ---------- 底部分頁 ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: flex; height: calc(var(--tabbar-h) + var(--safe-b)); padding-bottom: var(--safe-b);
  background: rgba(255, 255, 255, .95); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  border-top: 1px solid var(--line);
}
.tabbar a { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; color: var(--muted); font-size: 11px; }
.tabbar a .ico { font-size: 22px; filter: grayscale(1); opacity: .6; }
.tabbar a.active { color: var(--green); font-weight: 600; }
.tabbar a.active .ico { filter: none; opacity: 1; }

/* ---------- 卡片、清單 ---------- */
.card { background: var(--card); border-radius: var(--radius); padding: 14px; margin-bottom: 12px; box-shadow: 0 1px 2px rgba(0,0,0,.04); }
.card h2 { margin: 0 0 10px; font-size: 15px; color: var(--muted); font-weight: 600; }
.list { background: var(--card); border-radius: var(--radius); overflow: hidden; margin-bottom: 12px; }
.list-head { font-size: 13px; color: var(--muted); padding: 14px 4px 6px; display: flex; justify-content: space-between; }
.row { display: flex; align-items: center; gap: 12px; padding: 12px 14px; min-height: 56px; border-bottom: 1px solid var(--line); color: inherit; }
.row:last-child { border-bottom: 0; }
.row:active { background: #f0f2ef; }
.row-main { flex: 1; min-width: 0; }
.row-title { font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row-sub { font-size: 13px; color: var(--muted); margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row-end { text-align: right; white-space: nowrap; }
.chev::after { content: "›"; color: #b9bfb9; font-size: 22px; margin-left: 4px; }
.empty { text-align: center; color: var(--muted); padding: 40px 10px; }

.amount { font-variant-numeric: tabular-nums; font-weight: 600; }
.income { color: var(--income); }
.expense { color: var(--expense); }
.profit { color: var(--blue); }

/* ---------- 統計 ---------- */
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.stat { background: var(--card); border-radius: var(--radius); padding: 10px 12px; }
.stat-label { font-size: 12px; color: var(--muted); }
.stat-value { font-size: 18px; font-weight: 700; font-variant-numeric: tabular-nums; margin-top: 2px; word-break: break-all; }
.stats-foot { font-size: 12px; color: var(--muted); text-align: right; margin: 6px 2px 0; }
.stats2 { grid-template-columns: repeat(2, 1fr); }
.stats-foot.big { font-size: 15px; margin: 8px 2px 18px; }
.stats-foot.big b { font-size: 17px; font-variant-numeric: tabular-nums; }

/* ---------- 篩選 ---------- */
.monthbar { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.monthbar button { border: 0; background: var(--card); border-radius: 10px; min-width: 44px; min-height: 40px; font-size: 20px; color: var(--green); }
.monthbar .label { font-size: 17px; font-weight: 600; border: 0; background: none; min-height: 40px; padding: 0 8px; }
.chips { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 2px; margin-bottom: 10px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip { flex: none; border: 1px solid var(--line); background: var(--card); border-radius: 999px; padding: 7px 14px; font-size: 14px; min-height: 36px; }
.chip.on { background: var(--green); border-color: var(--green); color: #fff; }
.search { width: 100%; border: 0; background: #e8ebe7; border-radius: 10px; padding: 10px 12px; font-size: 16px; margin-bottom: 10px; }

/* ---------- 表單 ---------- */
.field { margin-bottom: 14px; }
.field > label, .field-label { display: block; font-size: 13px; color: var(--muted); margin: 0 0 6px 2px; }
.input, select.input, textarea.input {
  width: 100%; border: 1px solid var(--line); background: var(--card); border-radius: 10px;
  padding: 11px 12px; font-size: 16px; min-height: 46px; appearance: none; -webkit-appearance: none;
}
select.input { background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%); background-position: calc(100% - 18px) 50%, calc(100% - 13px) 50%; background-size: 5px 5px; background-repeat: no-repeat; padding-right: 32px; }
textarea.input { min-height: 80px; resize: vertical; }
.input:focus { outline: 2px solid var(--green); outline-offset: -1px; }
.amount-input { font-size: 34px; font-weight: 700; text-align: right; min-height: 64px; font-variant-numeric: tabular-nums; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.seg { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; background: #e5e9e4; border-radius: 10px; padding: 3px; margin-bottom: 14px; }
.seg button { border: 0; background: none; border-radius: 8px; min-height: 40px; font-size: 15px; font-weight: 500; }
.seg button.on { background: var(--card); box-shadow: 0 1px 3px rgba(0,0,0,.12); }
.seg button.on.expense-on { color: var(--expense); }
.seg button.on.income-on { color: var(--income); }

.pick { display: flex; flex-wrap: wrap; gap: 8px; }
.pick button { border: 1px solid var(--line); background: var(--card); border-radius: 10px; padding: 9px 12px; min-height: 42px; font-size: 15px; }
.pick button.on { border-color: var(--green); background: var(--green-soft); color: var(--green); font-weight: 600; }

.btn { display: block; width: 100%; border: 0; border-radius: 12px; min-height: 50px; font-size: 17px; font-weight: 600; background: var(--green); color: #fff; }
.btn:disabled { opacity: .5; }
.btn.secondary { background: var(--card); color: var(--green); border: 1px solid var(--line); }
.btn.danger { background: var(--card); color: var(--expense); border: 1px solid var(--line); }
.btn + .btn { margin-top: 10px; }
.btn-small { border: 1px solid var(--line); background: var(--card); border-radius: 8px; padding: 6px 10px; font-size: 14px; min-height: 34px; color: var(--blue); }
.btn-small.danger { color: var(--expense); }
.actions { position: sticky; bottom: calc(var(--tabbar-h) + var(--safe-b) + 8px); margin-top: 18px; }
body.no-tabbar .actions { bottom: calc(var(--safe-b) + 8px); }

.fab {
  position: fixed; right: 18px; bottom: calc(var(--tabbar-h) + var(--safe-b) + 16px); z-index: 15;
  width: 58px; height: 58px; border-radius: 50%; border: 0; background: var(--green); color: #fff;
  font-size: 32px; line-height: 1; box-shadow: 0 4px 14px rgba(47,107,58,.4);
}

/* ---------- 鳥隻 ---------- */
.avatar { width: 48px; height: 48px; border-radius: 12px; object-fit: cover; background: var(--green-soft); display: flex; align-items: center; justify-content: center; font-size: 24px; flex: none; }
.photo-big { width: 100%; aspect-ratio: 4 / 3; border-radius: var(--radius); object-fit: cover; background: var(--green-soft); display: flex; align-items: center; justify-content: center; font-size: 60px; margin-bottom: 12px; }
.art { overflow: hidden; padding: 0; }
.art svg { display: block; width: 100%; height: 100%; }
.avatar-pair { display: flex; flex: none; }
.avatar-pair .avatar { width: 40px; height: 40px; }
.avatar-pair .avatar + .avatar { margin-left: -10px; box-shadow: 0 0 0 2px var(--card); }
.pair-head { display: grid; grid-template-columns: 1fr auto 1fr; gap: 8px; align-items: center; margin-bottom: 12px; }
.pair-head a { display: block; color: inherit; text-align: center; }
.pair-head .photo-big { aspect-ratio: 1; margin-bottom: 6px; }
.pair-head .x { font-size: 24px; color: var(--muted); }
.warn { background: #fff4e0; color: #8a5300; border-radius: 12px; padding: 10px 12px; font-size: 14px; margin-bottom: 12px; line-height: 1.6; }
.stats4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.stats4 .stat { padding: 8px; text-align: center; }
.stats4 .stat-value { font-size: 20px; }
.egg { display: flex; align-items: center; gap: 10px; padding: 10px 14px; min-height: 52px; border-bottom: 1px solid var(--line); width: 100%; border-left: 0; border-right: 0; border-top: 0; background: none; text-align: left; }
.egg:last-child { border-bottom: 0; }
.egg-ico { width: 26px; height: 32px; border-radius: 50% 50% 48% 48% / 60% 60% 40% 40%; background: #f4efe4; border: 2px solid #d9cfbd; flex: none; }
.egg-ico.fertile { background: #fde9c8; border-color: #e9b866; }
.egg-ico.hatched { background: #dff0d8; border-color: #6cb33f; }
.egg-ico.infertile, .egg-ico.dead, .egg-ico.broken { background: #eee; border-color: #bbb; opacity: .7; }
.egg-ico.sold { background: #e1ecf8; border-color: #4b8fd8; }
.clutch-tools { display: flex; gap: 8px; align-items: center; padding: 10px 14px; border-top: 1px solid var(--line); }
.clutch-tools input { flex: 1; min-height: 38px; padding: 6px 10px; }
.check-row { display: flex; align-items: center; gap: 12px; padding: 8px 14px; min-height: 56px; border-bottom: 1px solid var(--line); }
.check-row input { width: 22px; height: 22px; accent-color: var(--green); flex: none; }
.selected-birds { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
.selected-birds .chip { display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px 4px 4px; }
.selected-birds .chip .avatar { width: 26px; height: 26px; border-radius: 50%; }
.kv { display: grid; grid-template-columns: 96px 1fr; gap: 8px 10px; font-size: 15px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; word-break: break-word; }
.badge { display: inline-block; font-size: 12px; padding: 2px 8px; border-radius: 999px; background: #eceeea; color: var(--muted); vertical-align: middle; }
.badge.active { background: var(--green-soft); color: var(--green); }
.badge.sold { background: #e6eefb; color: var(--blue); }
.badge.dead { background: #f0e5e4; color: var(--expense); }

/* ---------- 其他 ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + var(--safe-b) + 90px); transform: translateX(-50%);
  background: rgba(30, 35, 31, .92); color: #fff; padding: 10px 18px; border-radius: 999px; font-size: 15px;
  z-index: 50; max-width: 86vw; text-align: center;
}
.toast.error { background: rgba(176, 40, 32, .95); }
.modal-back { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 40; display: flex; align-items: flex-end; }
.modal { background: var(--bg); width: 100%; max-height: 80vh; overflow: auto; border-radius: 16px 16px 0 0; padding: 16px 14px calc(16px + var(--safe-b)); }
.modal h3 { margin: 0 0 12px; font-size: 17px; text-align: center; }
.login { max-width: 360px; margin: 6vh auto 0; }
.login .logo { width: 88px; height: 88px; margin: 0 auto; border-radius: 24px; overflow: hidden; background: #e6f2dd; }
.login-links { display: flex; justify-content: space-between; gap: 12px; margin-top: 18px; font-size: 15px; }
.login-links a { padding: 10px 2px; }
.login h2 { text-align: center; margin: 6px 0 24px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.center { text-align: center; }
.mt { margin-top: 12px; }
.hint { font-size: 13px; color: var(--muted); line-height: 1.6; }
.loading { text-align: center; color: var(--muted); padding: 40px 0; }

.badge.warn-badge { background: #fde7c2; color: #8a5200; }
.app-footer { text-align: center; font-size: 12px; line-height: 1.8; color: var(--muted); margin: 36px 0 8px; }
.app-footer a { color: var(--muted); text-decoration: underline; }

/* 用藥管理 */
.tools { margin-bottom: 12px; }
.med-icon { width: 40px; height: 40px; flex: none; border-radius: 50%; background: #f3ecf9; display: flex; align-items: center; justify-content: center; font-size: 20px; }
.badge.usage-internal { background: #e6eefb; color: var(--blue); }
.badge.usage-external { background: #fde7c2; color: #8a5200; }
.med-info { font-size: 13px; color: var(--muted); line-height: 1.6; margin-top: 6px; display: flex; gap: 10px; align-items: flex-start; }
.med-photo { object-fit: cover; background: #fff; border: 1px solid var(--line, #e5e5e5); }
.med-thumb { width: 64px; height: 64px; border-radius: 12px; cursor: zoom-in; }
.med-name-row { display: flex; gap: 12px; align-items: flex-end; }
.med-name-row .field { flex: 1; min-width: 0; }
.med-photo-box { margin-bottom: 14px; flex: none; }
.med-big { width: 76px; height: 76px; border-radius: 14px; font-size: 34px; cursor: pointer; }
.med-add { flex-direction: column; font-size: 24px; gap: 2px; border: 1.5px dashed #c9b6dc; color: var(--muted); }
.med-add small { font-size: 11px; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }
.btn-small.on { background: var(--green, #3a8a3a); color: #fff; }
.photo-zoom { display: flex; align-items: center; justify-content: center; padding: 16px; }
.photo-zoom img { max-width: 100%; max-height: 90vh; border-radius: 12px; }
.med-info:empty { display: none; }
