:root {
  --paper: #f6efe2;
  --card: #fffaf0;
  --ink: #2b2118;
  --muted: #8a7a66;
  --line: #e2d5bf;
  --red: #a8321f;
  --deep: #7a2418;
  --gold: #b8893b;
  --green: #4f6b3a;
  --sel: #f1dfc0;
  --serif: "Noto Serif TC", "Noto Serif CJK TC", "Songti TC", "PMingLiU", "MingLiU", serif;
  --sans: -apple-system, "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  --tabh: 58px;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--paper); color: var(--ink); font-family: var(--sans); font-size: 16px;
  padding-top: env(safe-area-inset-top);
  padding-bottom: calc(var(--tabh) + env(safe-area-inset-bottom));
  min-height: 100vh;
}
button, input, select, textarea { font: inherit; color: inherit; }
h2 { font-family: var(--serif); font-size: 1.1rem; margin: 18px 4px 8px; color: var(--deep); }
.mt { margin-top: 26px; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.row { display: flex; align-items: center; }
.gap { gap: 8px; flex-wrap: wrap; }

/* 頂欄 */
.topbar {
  position: sticky; top: 0; z-index: 10; display: flex; align-items: center; gap: 4px;
  background: var(--deep); color: #fbeedd; padding: 8px 8px;
  padding-top: calc(8px + env(safe-area-inset-top)); margin-top: calc(-1 * env(safe-area-inset-top));
}
.topbar .nav { background: none; border: 0; color: inherit; font-size: 2rem; line-height: 1; width: 44px; height: 44px; }
.topbar .title { flex: 1; text-align: center; cursor: pointer; }
.topbar .t1 { font-family: var(--serif); font-size: 1.25rem; font-weight: 700; letter-spacing: .05em; }
.topbar .t2 { font-size: .8rem; opacity: .85; }
.today-btn { border: 1px solid #e8c9a0; background: transparent; color: #fbeedd; border-radius: 50%; width: 36px; height: 36px; font-family: var(--serif); }

main { padding: 0 10px 20px; max-width: 760px; margin: 0 auto; }
.view { display: none; }
.view.active { display: block; }

/* 月曆 */
.weekhead, .grid { display: grid; grid-template-columns: repeat(7, 1fr); }
.weekhead div { text-align: center; font-size: .8rem; color: var(--muted); padding: 8px 0 4px; }
.weekhead .sun { color: var(--red); }
.grid { gap: 2px; }
.cell {
  position: relative; background: var(--card); border-radius: 8px; min-height: 64px; padding: 3px 2px 4px;
  display: flex; flex-direction: column; align-items: center; justify-content: flex-start; cursor: pointer;
  border: 1px solid transparent;
}
.cell .sd { align-self: flex-start; font-size: .72rem; color: var(--muted); line-height: 1; padding-left: 3px; }
.cell .ld { font-family: var(--serif); font-size: 1.05rem; font-weight: 600; line-height: 1.25; margin-top: 2px; }
.cell .ld.m1 { color: var(--red); }
.cell .tag { font-size: .66rem; line-height: 1.2; color: var(--green); white-space: nowrap; overflow: hidden; max-width: 100%; }
.cell .tag.fest { color: var(--red); }
.cell .tag.gz { color: var(--muted); }
.cell .dots { position: absolute; bottom: 3px; right: 4px; display: flex; gap: 2px; }
.cell .dots i { width: 6px; height: 6px; border-radius: 50%; background: var(--gold); }
.cell.sun .sd { color: var(--red); }
.cell.other { opacity: .38; }
.cell.today { border-color: var(--red); }
.cell.today .sd { color: #fff; background: var(--red); border-radius: 6px; padding: 1px 3px; }
.cell.sel { background: var(--sel); }

/* 日詳情 */
.detail { background: var(--card); border-radius: 12px; margin-top: 12px; padding: 14px; }
.d-top { display: flex; gap: 14px; align-items: flex-start; }
.d-main { flex: 1; }
.d-lunar { font-family: var(--serif); font-size: 1.6rem; font-weight: 700; color: var(--deep); }
.d-solar { color: var(--muted); font-size: .9rem; margin-top: 2px; }
.d-fest { margin-top: 6px; display: flex; flex-wrap: wrap; gap: 4px; }
.pillars { display: flex; flex-direction: row-reverse; gap: 6px; }
.pillar { display: flex; flex-direction: column; align-items: center; background: var(--paper); border-radius: 8px; padding: 4px 6px; }
.pillar .pl { font-size: .68rem; color: var(--muted); }
.pillar .pc { font-family: var(--serif); font-size: 1.3rem; font-weight: 700; line-height: 1.2; }
.pillar.day .pc { color: var(--red); }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 4px 10px; margin: 12px 0 4px; font-size: .92rem; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }
.chips { display: flex; flex-wrap: wrap; gap: 4px; margin: 4px 0 8px; }
.chip { font-size: .8rem; padding: 2px 8px; border-radius: 10px; background: var(--paper); border: 1px solid var(--line); }
.chip.yi { border-color: #b9caa5; color: var(--green); }
.chip.ji { border-color: #e1b3a8; color: var(--red); }
.chip.fest { background: #f7e3dc; color: var(--red); border-color: #e9c2b6; }
.chip.ev { background: #f4e7c8; border-color: #e0c78d; }
.lbl { font-size: .8rem; color: var(--muted); margin-top: 8px; }
.d-actions { margin-top: 12px; }

/* 按鈕與卡片 */
.btn { border: 1px solid var(--line); background: var(--card); padding: 8px 14px; border-radius: 10px; cursor: pointer; display: inline-block; }
.btn.primary { background: var(--deep); color: #fbeedd; border-color: var(--deep); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.danger { color: var(--red); border-color: #e1b3a8; }
.btn:disabled { opacity: .5; }
.card { background: var(--card); border-radius: 12px; padding: 14px; }
.sec-head { display: flex; align-items: center; justify-content: space-between; }
.field { display: block; margin-bottom: 12px; }
.field > span { display: block; font-size: .85rem; color: var(--muted); margin-bottom: 4px; }
input[type=text], input[type=url], input[type=password], input[type=number], input[type=time], select, textarea {
  width: 100%; padding: 9px 10px; border: 1px solid var(--line); border-radius: 8px; background: #fff; font-size: 16px;
}
textarea { min-height: 60px; resize: vertical; }
.switch-row { display: flex; justify-content: space-between; align-items: center; padding: 8px 0; border-bottom: 1px solid var(--line); }
.switch-row:last-child { border-bottom: 0; }
.switch-row input { width: 22px; height: 22px; accent-color: var(--deep); }
.status { margin-top: 10px; font-size: .88rem; color: var(--muted); white-space: pre-line; }
.help { margin-top: 10px; font-size: .88rem; }
.help summary { color: var(--deep); cursor: pointer; }
.credit { text-align: center; margin-top: 24px; }

/* 提醒列表 */
.ev-item { background: var(--card); border-radius: 12px; padding: 12px; margin-bottom: 8px; display: flex; gap: 10px; align-items: center; }
.ev-item .ev-main { flex: 1; min-width: 0; cursor: pointer; }
.ev-item .ev-name { font-weight: 600; font-family: var(--serif); font-size: 1.05rem; }
.ev-item .ev-desc { font-size: .85rem; color: var(--muted); }
.ev-item .ev-next { font-size: .82rem; color: var(--green); margin-top: 2px; }
.ev-item.off { opacity: .5; }
.ev-item input { width: 24px; height: 24px; accent-color: var(--deep); }
.up-item { display: flex; gap: 10px; padding: 8px 4px; border-bottom: 1px dashed var(--line); font-size: .9rem; }
.up-item .when { color: var(--deep); min-width: 108px; font-variant-numeric: tabular-nums; }
.empty { text-align: center; color: var(--muted); padding: 24px 0; }
.banner { background: #f7e3dc; color: var(--deep); border-radius: 10px; padding: 10px 12px; margin-top: 12px; font-size: .9rem; }
.banner.ok { background: #e6eddc; color: var(--green); }

/* 編輯面板 */
.sheet-wrap { position: fixed; inset: 0; background: rgba(40, 25, 15, .45); z-index: 50; display: flex; align-items: flex-end; justify-content: center; }
.sheet-wrap[hidden] { display: none; }
.sheet { background: var(--paper); width: 100%; max-width: 640px; max-height: 92vh; border-radius: 16px 16px 0 0; display: flex; flex-direction: column; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 10px; border-bottom: 1px solid var(--line); }
.sheet-body { overflow-y: auto; padding: 14px 14px calc(24px + env(safe-area-inset-bottom)); }
.seg { display: flex; background: var(--line); border-radius: 10px; padding: 3px; margin-bottom: 14px; }
.seg button { flex: 1; border: 0; background: transparent; padding: 7px 2px; border-radius: 8px; font-size: .9rem; }
.seg button.on { background: var(--card); color: var(--deep); font-weight: 600; box-shadow: 0 1px 2px rgba(0,0,0,.08); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.grid4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.checks { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.checks label { display: inline-flex; align-items: center; gap: 4px; background: var(--card); border: 1px solid var(--line); border-radius: 8px; padding: 5px 8px; font-size: .88rem; }
.checks input { accent-color: var(--deep); }
.hint { font-size: .82rem; color: var(--muted); margin: -6px 0 12px; }
.err { color: var(--red); font-size: .88rem; margin-bottom: 10px; }
.preview { background: var(--card); border-radius: 10px; padding: 10px 12px; font-size: .88rem; margin-top: 6px; }
.preview div { padding: 2px 0; }

/* 底部分頁 */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20; display: flex; background: var(--card);
  border-top: 1px solid var(--line); height: calc(var(--tabh) + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom);
}
.tabbar button { flex: 1; border: 0; background: none; font-size: .75rem; color: var(--muted); display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; }
.tabbar .ico { font-family: var(--serif); font-size: 1.15rem; width: 30px; height: 30px; border-radius: 50%; display: flex; align-items: center; justify-content: center; border: 1px solid var(--line); }
.tabbar button.active { color: var(--deep); }
.tabbar button.active .ico { background: var(--deep); color: #fbeedd; border-color: var(--deep); }

.toast { position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(var(--tabh) + 20px + env(safe-area-inset-bottom)); background: rgba(43, 33, 24, .92); color: #fff; padding: 9px 16px; border-radius: 20px; z-index: 60; font-size: .9rem; max-width: 90vw; text-align: center; }
.toast[hidden] { display: none; }

@media (min-width: 600px) {
  .cell { min-height: 78px; }
  .cell .ld { font-size: 1.2rem; }
}
