:root {
  --bg: #FDFBF7;
  --bg-2: #F6F1E6;
  --card: #FFFFFF;
  --ink: #2C2C2C;
  --muted: #8C857B;
  --line: #EDE6D8;
  --gold: #D4AF37;
  --gold-deep: #B08823;
  --champagne: #F8EFD4;
  --rose: #E11D48;
  --rose-soft: #FDECEF;
  --amber: #D97706;
  --amber-soft: #FDF3E3;
  --shadow: 0 1px 2px rgba(44, 44, 44, .04), 0 8px 24px rgba(44, 44, 44, .05);
  --radius: 20px;
}

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

/* hidden 屬性必須壓過任何 display:flex/grid（否則引導彈窗關不掉） */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans TC",
    "PingFang TC", "Hiragino Sans", "Apple SD Gothic Neo", "Malgun Gothic", sans-serif;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

body::before {
  content: "";
  position: fixed; inset: 0;
  background:
    radial-gradient(120% 60% at 50% -10%, rgba(212, 175, 55, .10), transparent 60%),
    radial-gradient(80% 50% at 100% 100%, rgba(224, 169, 109, .08), transparent 60%);
  pointer-events: none;
  z-index: -1;
}

/* ---------- 開場 ---------- */
.splash {
  position: fixed; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 14px; background: var(--bg); z-index: 60;
  transition: opacity .5s ease, visibility .5s;
}
.splash.hide { opacity: 0; visibility: hidden; }
.splash-moon { width: 132px; height: 132px; animation: floaty 5s ease-in-out infinite; }
.splash-word { font-size: 20px; letter-spacing: .28em; color: var(--ink); }
.splash-sub { font-size: 11px; letter-spacing: .3em; color: var(--muted); }

/* ---------- 版面 ---------- */
.app {
  max-width: 620px;
  margin: 0 auto;
  padding: 0 18px calc(96px + env(safe-area-inset-bottom));
  min-height: 100vh;
}

.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between;
  gap: 10px;
  padding: 14px 2px 12px;
  background: linear-gradient(var(--bg) 72%, rgba(253, 251, 247, 0));
}
.brand { display: flex; align-items: center; gap: 9px; }
.brand-moon { width: 26px; height: 26px; }
.brand-name { font-size: 15px; letter-spacing: .12em; font-weight: 500; }
.topbar-right { display: flex; align-items: center; gap: 8px; }
.chip {
  font-size: 10.5px; letter-spacing: .08em; color: var(--gold-deep);
  background: var(--champagne); border: 1px solid #F0E2BC;
  padding: 3px 9px; border-radius: 999px;
}
.locale {
  font-size: 12px; color: var(--ink); background: var(--card);
  border: 1px solid var(--line); border-radius: 10px;
  padding: 5px 8px; appearance: none;
}

.announce {
  margin: 0 0 12px; padding: 10px 14px;
  background: var(--champagne); border: 1px solid #F0E2BC;
  border-radius: 14px; font-size: 12.5px; color: #7A6420;
}

/* ---------- 卡片 ---------- */
.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px;
  margin-bottom: 14px;
  box-shadow: var(--shadow);
}
.card-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 10px; margin-bottom: 12px;
}
.card-head h2 { font-size: 14px; font-weight: 600; margin: 0; letter-spacing: .04em; white-space: nowrap; flex: 0 0 auto; }
.card-head > *:not(h2) { min-width: 0; }
.muted { color: var(--muted); font-size: 12.5px; margin: 0; }

.hero { display: flex; gap: 16px; align-items: center; padding: 20px 18px; }
.hero-moon { flex: 0 0 auto; width: 76px; height: 76px; display: grid; place-items: center; }
.hero-moon svg { width: 100%; height: 100%; animation: floaty 5s ease-in-out infinite; }
.hero-text { min-width: 0; }
.hero-eyebrow { font-size: 11px; letter-spacing: .22em; color: var(--muted); }
.hero-title { font-size: 23px; line-height: 1.35; margin: 4px 0 2px; font-weight: 600; letter-spacing: .01em; }
.hero-sub { margin: 0; font-size: 12.5px; color: var(--muted); }
.hero-dday {
  margin-top: 8px; display: inline-block;
  font-size: 21px; letter-spacing: .06em; font-weight: 600;
  background: linear-gradient(92deg, #F3E5AB, var(--gold), #E0A96D);
  -webkit-background-clip: text; background-clip: text; color: transparent;
}

/* ---------- 快捷鍵 ---------- */
.quickgrid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 14px; }
.quick {
  display: flex; flex-direction: column; align-items: flex-start; gap: 3px;
  padding: 14px 16px; border-radius: 18px; cursor: pointer;
  background: var(--card); border: 1px solid var(--line);
  box-shadow: var(--shadow); text-align: left; font: inherit; color: inherit;
  transition: transform .15s ease, background .2s ease, border-color .2s ease;
}
.quick:active { transform: scale(.97); }
.quick-ico { width: 20px; height: 20px; }
.quick-ico.rose { color: var(--rose); }
.quick-ico.amber { color: var(--amber); }
.quick-label { font-size: 13px; font-weight: 600; }
.quick-state { font-size: 11px; color: var(--muted); }
.quick.on { background: var(--rose-soft); border-color: #F6CED5; }
.quick.on .quick-state { color: var(--rose); }
.quick.on-amber { background: var(--amber-soft); border-color: #F3DFBE; }
.quick.on-amber .quick-state { color: var(--amber); }

/* ---------- 階段條 ---------- */
.phases { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin-bottom: 14px; }
.phase-cell { text-align: center; }
.phase-bar { height: 5px; border-radius: 3px; background: var(--bg-2); overflow: hidden; }
.phase-bar.active { background: linear-gradient(90deg, #F3E5AB, var(--gold)); box-shadow: 0 0 10px rgba(212, 175, 55, .45); }
.phase-name { display: block; margin-top: 6px; font-size: 10.5px; color: var(--muted); }
.phase-cell.active .phase-name { color: var(--gold-deep); font-weight: 600; }

.kv { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.kv > div { display: flex; flex-direction: column; gap: 2px; }
.kv span { font-size: 10.5px; color: var(--muted); letter-spacing: .06em; }
.kv strong { font-size: 13px; font-weight: 600; }

.tip { margin: 0; font-size: 13px; line-height: 1.8; color: #4A4640; }

/* ---------- 表單 ---------- */
.field { margin-bottom: 16px; }
.field > span { display: block; font-size: 12px; color: var(--muted); margin-bottom: 7px; letter-spacing: .04em; }
.dateinput, input[type="number"], textarea, select {
  width: 100%; padding: 11px 12px; font: inherit; font-size: 14px; color: var(--ink);
  background: #FFFDF9; border: 1px solid var(--line); border-radius: 12px;
}
input[type="number"] { -moz-appearance: textfield; }
textarea { resize: vertical; }
.segmented { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; }
.segmented button {
  padding: 10px 0; font: inherit; font-size: 13px; cursor: pointer;
  background: #FFFDF9; border: 1px solid var(--line); border-radius: 11px; color: var(--muted);
}
.segmented button.on { background: var(--rose-soft); border-color: #F3C3CD; color: var(--rose); font-weight: 600; }
.range-wrap { display: flex; align-items: center; gap: 12px; }
.range-wrap input[type="range"] { flex: 1; accent-color: var(--gold); }
.range-wrap output { min-width: 26px; text-align: center; font-size: 13px; font-weight: 600; color: var(--gold-deep); }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chips button {
  padding: 8px 14px; font: inherit; font-size: 12.5px; cursor: pointer;
  background: #FFFDF9; border: 1px solid var(--line); border-radius: 999px; color: var(--muted);
}
.chips button.on { background: var(--champagne); border-color: #EBD9A9; color: var(--gold-deep); font-weight: 600; }

/* ---------- 按鈕 ---------- */
.actions { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 6px; }
.btn {
  padding: 11px 18px; font: inherit; font-size: 13.5px; cursor: pointer;
  border-radius: 12px; border: 1px solid var(--line); background: #FFFDF9; color: var(--ink);
  transition: transform .12s ease, opacity .2s ease;
}
.btn:active { transform: scale(.98); }
.btn.primary { background: linear-gradient(96deg, #E8CE85, var(--gold)); border-color: #D9BC6A; color: #3B3105; font-weight: 600; }
.btn.ghost { background: transparent; }
.btn.danger { background: var(--rose-soft); border-color: #F3C3CD; color: var(--rose); font-weight: 600; }
.btn.wide { width: 100%; margin-top: 6px; }
.btn[disabled] { opacity: .55; cursor: default; }
.linkbtn {
  background: none; border: none; font: inherit; font-size: 12px;
  color: var(--gold-deep); cursor: pointer; padding: 2px 0;
}
.iconbtn {
  width: 30px; height: 30px; border-radius: 9px; cursor: pointer;
  border: 1px solid var(--line); background: #FFFDF9; color: var(--ink); font-size: 15px; line-height: 1;
}
.msg { margin-top: 12px; font-size: 12.5px; padding: 9px 12px; border-radius: 10px; }
.msg.ok { background: #F2F8F1; border: 1px solid #D8EAD3; color: #3C6B37; }
.msg.err { background: var(--rose-soft); border: 1px solid #F3C3CD; color: var(--rose); }

/* ---------- 最近記錄 ---------- */
.recent { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.recent li {
  display: flex; justify-content: space-between; gap: 10px;
  padding: 10px 12px; border-radius: 12px; background: #FFFDF9; border: 1px solid var(--line);
  font-size: 12.5px;
}
.recent li b { font-weight: 600; }
.recent li span { color: var(--muted); }

/* ---------- 月曆 ---------- */
.calnav { display: flex; align-items: center; gap: 10px; }
.cal-week { display: grid; grid-template-columns: repeat(7, 1fr); margin-bottom: 4px; }
.cal-week span { text-align: center; font-size: 10.5px; color: var(--muted); }
.cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; }
.cal-cell {
  aspect-ratio: 1 / 1; display: grid; place-items: center;
  border-radius: 11px; font-size: 12px; color: var(--muted);
  background: #FFFDF9; border: 1px solid transparent; cursor: pointer;
}
.cal-cell.empty { background: transparent; cursor: default; }
.cal-cell.today { border-color: var(--gold); font-weight: 700; color: var(--ink); }
.cal-cell.period { background: var(--rose-soft); color: var(--rose); font-weight: 600; }
.cal-cell.fertile { background: var(--champagne); color: var(--gold-deep); font-weight: 600; }
.cal-cell.safe { background: #F7F6F2; }
.cal-cell.logged::after {
  content: ""; display: block; width: 4px; height: 4px; border-radius: 50%;
  background: var(--ink); margin-top: -14px;
}
.legend { display: flex; flex-wrap: wrap; gap: 14px; margin-top: 14px; font-size: 11px; color: var(--muted); }
.legend span { display: inline-flex; align-items: center; gap: 5px; }
.dot { width: 9px; height: 9px; border-radius: 50%; display: inline-block; }
.dot.period { background: var(--rose); }
.dot.fertile { background: var(--gold); }
.dot.safe { background: #DDD8CD; }
.dot.logged { background: var(--ink); }

/* ---------- 統計 ---------- */
.statgrid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.stat { padding: 12px; border-radius: 14px; background: #FFFDF9; border: 1px solid var(--line); }
.stat small { display: block; font-size: 10.5px; color: var(--muted); letter-spacing: .08em; }
.stat b { font-size: 17px; font-weight: 600; }

/* ---------- 導覽列 ---------- */
.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 30;
  display: grid; grid-template-columns: repeat(5, 1fr);
  background: rgba(253, 251, 247, .88);
  backdrop-filter: blur(18px) saturate(180%);
  border-top: 1px solid var(--line);
  padding: 8px 0 calc(6px + env(safe-area-inset-bottom));
}
.tab {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  background: none; border: none; cursor: pointer; font: inherit;
  color: var(--muted); padding: 4px 0;
}
.tab svg { width: 21px; height: 21px; }
.tab small { font-size: 10px; letter-spacing: .1em; }
.tab.active { color: var(--gold-deep); }
.tab.active small { font-weight: 600; }

/* ---------- 引導 ---------- */
.modal {
  position: fixed; inset: 0; z-index: 70;
  display: grid; place-items: center; padding: 18px;
  background: rgba(44, 44, 44, .28);
  backdrop-filter: blur(6px);
}
.modal-card {
  width: 100%; max-width: 400px; max-height: 90vh; overflow: auto;
  background: var(--card); border-radius: 24px; padding: 26px 22px;
  box-shadow: 0 24px 60px rgba(44, 44, 44, .18);
}
.modal-moon { width: 48px; height: 48px; display: block; margin: 0 auto 10px; }
.modal-card h2 { font-size: 17px; text-align: center; margin: 0 0 6px; font-weight: 600; }
.modal-card .muted { text-align: center; margin-bottom: 18px; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(96px + env(safe-area-inset-bottom));
  transform: translateX(-50%) translateY(10px);
  background: rgba(44, 44, 44, .92); color: #FFFDF9;
  font-size: 12.5px; padding: 9px 16px; border-radius: 999px;
  z-index: 80; opacity: 0; transition: opacity .25s ease, transform .25s ease;
  pointer-events: none; white-space: nowrap;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

.foot { text-align: center; font-size: 11px; color: var(--muted); letter-spacing: .06em; margin: 22px 0 6px; }
.card.danger { border-color: #F3D7DC; }

/* ---------- 動效 ---------- */
@keyframes floaty { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-4px); } }
@keyframes twinkle {
  0%, 100% { opacity: .35; transform: scale(.85); }
  50% { opacity: 1; transform: scale(1.15); }
}
.twinkle { animation: twinkle 2.4s ease-in-out infinite; transform-origin: center; }
.twinkle.late { animation-delay: 1.2s; }

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}

@media (min-width: 700px) {
  .app { padding-left: 24px; padding-right: 24px; }
  .hero-title { font-size: 26px; }
}

/* 隱私 chip 不換行（窄螢幕時曾斷成三行） */
#privacyChip { white-space: nowrap; flex-shrink: 0; }

/* 記錄頁日期欄：標題與日期同列時，日期可伸縮、標題不換行 */
.dateinput { flex: 1 1 auto; max-width: 220px; }
