/* ========== 設計變數 ========== */
:root {
  --bg: #f5f4ef;
  --surface: #ffffff;
  --text: #1c1c1e;
  --muted: #6e6e73;
  --line: #e8e6df;
  --brand: #0d9488;
  --brand-soft: #dff3f0;
  --work: #2f6fd6;
  --work-soft: #e8f0fc;
  --life: #d9702b;
  --life-soft: #fbece0;
  --danger: #c93a3a;
  --accent: var(--brand);
  --accent-soft: var(--brand-soft);
  --accent-ink: #ffffff;
  --radius: 16px;
  --shadow: 0 1px 2px rgba(30, 28, 20, .05), 0 6px 18px rgba(30, 28, 20, .05);
  --ease: cubic-bezier(.2, .8, .2, 1);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101114;
    --surface: #1b1c20;
    --text: #ededf0;
    --muted: #9b9ba4;
    --line: #2a2b31;
    --brand: #2dd4bf;
    --brand-soft: #14312f;
    --work: #78a4f4;
    --work-soft: #1d2a42;
    --life: #f2a56d;
    --life-soft: #3a2a1e;
    --danger: #f07474;
    --accent-ink: #0b1514;
    --shadow: 0 0 0 1px rgba(255, 255, 255, .05);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", "Segoe UI", sans-serif;
  font-size: 16px;
  line-height: 1.6;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
}
button { font: inherit; color: inherit; cursor: pointer; }
button:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
[hidden] { display: none !important; }

#app { max-width: 720px; margin: 0 auto; min-height: 100vh; min-height: 100dvh; padding: 0 16px calc(150px + env(safe-area-inset-bottom)); }
#app[data-cat="work"], #editor[data-cat="work"] { --accent: var(--work); --accent-soft: var(--work-soft); --accent-ink: #fff; }
#app[data-cat="life"], #editor[data-cat="life"] { --accent: var(--life); --accent-soft: var(--life-soft); --accent-ink: #fff; }
@media (prefers-color-scheme: dark) {
  #app[data-cat="work"], #editor[data-cat="work"], #app[data-cat="life"], #editor[data-cat="life"] { --accent-ink: #101114; }
}

/* ========== 頂部（固定） ========== */
.sticky-head {
  position: sticky; top: 0; z-index: 10;
  margin: 0 -16px; padding: 0 16px 10px;
  background: color-mix(in srgb, var(--bg) 90%, transparent);
  -webkit-backdrop-filter: blur(14px); backdrop-filter: blur(14px);
}
.top { display: flex; align-items: flex-end; justify-content: space-between; padding: calc(12px + env(safe-area-inset-top)) 0 10px; }
.date-line { margin: 0; font-size: 13px; color: var(--muted); font-weight: 600; letter-spacing: .04em; }
.top h1 { font-size: 28px; line-height: 1.2; margin: 2px 0 0; letter-spacing: .02em; }
.top-actions { display: flex; gap: 2px; margin-bottom: -4px; }
.icon-btn { background: none; border: 0; width: 44px; height: 44px; border-radius: 50%; display: grid; place-items: center; color: var(--text); transition: background .15s; flex: none; }
.icon-btn:hover, .icon-btn:active { background: var(--line); }
.searchbar { padding-bottom: 10px; }
.searchbar input, .field input {
  width: 100%; border: 1px solid var(--line); background: var(--surface); color: var(--text);
  border-radius: 12px; padding: 11px 14px; font: inherit;
}
.searchbar input:focus, .field input:focus { outline: 2px solid var(--accent); outline-offset: 1px; border-color: transparent; }

/* 分類分頁 */
.tabs { display: grid; grid-template-columns: repeat(3, 1fr); background: var(--line); border-radius: 13px; padding: 3px; gap: 3px; }
.tabs button { border: 0; background: none; padding: 9px 0; border-radius: 10px; color: var(--muted); font-weight: 600; transition: background .2s var(--ease), color .2s; }
.tabs button.on { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.tabs button[data-cat="all"].on { color: var(--brand); }
.tabs button[data-cat="work"].on { color: var(--work); }
.tabs button[data-cat="life"].on { color: var(--life); }

.summary { margin: 14px 4px 0; color: var(--muted); font-size: 13px; }

/* ========== 本月業務統計 ========== */
.stats { display: grid; grid-template-columns: 1fr 1fr 1fr 1.7fr; background: var(--surface); border-radius: var(--radius); padding: 12px 4px 14px; margin: 14px 0 12px; box-shadow: var(--shadow); text-align: center; }
.stats .lbl { grid-column: 1 / -1; text-align: left; font-size: 12px; color: var(--muted); font-weight: 600; letter-spacing: .04em; padding: 0 14px 8px; }
.stat { padding: 0 4px; }
.stat + .stat { border-left: 1px solid var(--line); }
.stat b { display: block; font-size: 24px; line-height: 1.2; font-weight: 700; color: var(--work); font-variant-numeric: tabular-nums; }
.stat span { font-size: 12px; color: var(--muted); }

/* ========== 今天還沒寫：快速開始 ========== */
.quick { background: var(--surface); border-radius: var(--radius); padding: 14px 16px 16px; margin: 14px 0 4px; box-shadow: var(--shadow); }
.stats:not([hidden]) + .quick { margin-top: 0; }
.quick p { margin: 0 0 10px; font-weight: 600; }
.q-row { display: flex; gap: 8px; flex-wrap: wrap; }
.q { border: 0; border-radius: 999px; padding: 9px 18px; font-weight: 700; font-size: 15px; display: inline-flex; align-items: center; gap: 6px; transition: transform .12s; }
.q:active { transform: scale(.96); }
.q.work { background: var(--work-soft); color: var(--work); }
.q.life { background: var(--life-soft); color: var(--life); }
.q.voice { background: var(--bg); color: var(--text); }

/* ========== 日曆 ========== */
.calendar { background: var(--surface); border-radius: var(--radius); padding: 10px 10px 12px; margin: 14px 0 8px; box-shadow: var(--shadow); }
.stats:not([hidden]) ~ main .calendar { margin-top: 0; }
.cal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 4px; }
.cal-head strong { font-size: 17px; }
.cal-head .today-btn { border: 1px solid var(--line); background: none; border-radius: 999px; padding: 2px 12px; font-size: 13px; color: var(--muted); margin-left: 6px; }
.cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 2px 0; text-align: center; }
.cal-dow { font-size: 12px; color: var(--muted); padding: 4px 0; }
.cal-day { background: none; border: 0; border-radius: 12px; height: 46px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; font-size: 15px; font-variant-numeric: tabular-nums; transition: background .15s; }
.cal-day.blank { pointer-events: none; }
.cal-day:hover { background: var(--line); }
.cal-day.today > span:first-child { font-weight: 800; color: var(--accent); }
.cal-day.sel { background: var(--accent); color: var(--accent-ink); }
.cal-day.sel > span:first-child { color: var(--accent-ink); font-weight: 700; }
.dots { display: flex; gap: 3px; height: 6px; }
.dot { width: 6px; height: 6px; border-radius: 50%; }
.dot.work { background: var(--work); }
.dot.life { background: var(--life); }
.cal-day.sel .dot { box-shadow: 0 0 0 1.5px var(--accent-ink); }

/* ========== 日記列表（日期欄 + 標題 + 縮圖） ========== */
.month-head { font-size: 14px; color: var(--muted); font-weight: 700; margin: 22px 4px 8px; letter-spacing: .04em; }
.list > .month-head:first-child { margin-top: 14px; }
.row {
  display: flex; align-items: flex-start; gap: 12px; width: 100%; text-align: left;
  background: var(--surface); border: 0; border-radius: var(--radius); padding: 14px 14px 14px 10px;
  margin-bottom: 10px; box-shadow: var(--shadow); transition: transform .12s var(--ease);
}
.row:active { transform: scale(.985); }
.row-date { flex: none; width: 52px; display: grid; justify-items: center; line-height: 1.25; padding-top: 1px; }
.row-date small { font-size: 11px; color: var(--muted); font-variant-numeric: tabular-nums; }
.row-date b { font-size: 26px; font-weight: 700; line-height: 1.15; font-variant-numeric: tabular-nums; }
.row.work .row-date b { color: var(--work); }
.row.life .row-date b { color: var(--life); }
.row-main { flex: 1; min-width: 0; }
.row-title { font-size: 17px; font-weight: 700; line-height: 1.4; overflow-wrap: anywhere; }
.row-text { margin: 2px 0 0; color: var(--text); display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; white-space: pre-wrap; overflow-wrap: anywhere; font-size: 15px; line-height: 1.55; }
.row-title + .row-text { color: var(--muted); }
.row-text.empty { color: var(--muted); }
.kpis { margin-top: 8px; display: flex; gap: 6px; flex-wrap: wrap; }
.kpi-chip { font-size: 13px; font-weight: 700; color: var(--work); background: var(--work-soft); border-radius: 8px; padding: 1px 9px; font-variant-numeric: tabular-nums; }
.row-meta { margin-top: 8px; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; font-size: 12px; color: var(--muted); }
.cat { display: inline-flex; align-items: center; gap: 5px; font-weight: 700; }
.cat::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.cat.work { color: var(--work); }
.cat.life { color: var(--life); }
.mood { font-size: 14px; line-height: 1; }
.ind { display: inline-flex; align-items: center; gap: 2px; }
.tag { color: var(--muted); background: var(--bg); border-radius: 6px; padding: 0 7px; }
.row-thumb { flex: none; width: 68px; height: 68px; border-radius: 12px; object-fit: cover; background: var(--line); }

.empty-state { text-align: center; color: var(--muted); padding: 56px 20px; }
.empty-state svg { display: block; margin: 0 auto 12px; opacity: .55; }
.empty-state strong { display: block; color: var(--text); font-size: 18px; margin-bottom: 4px; }

/* ========== 新增按鈕 + 底部選單 ========== */
.fab {
  position: fixed; right: max(16px, calc(50% - 344px)); bottom: calc(92px + env(safe-area-inset-bottom));
  width: 58px; height: 58px; border-radius: 50%; border: 0; background: var(--accent); color: var(--accent-ink);
  display: grid; place-items: center; box-shadow: 0 8px 22px rgba(0, 0, 0, .25); z-index: 12;
  transition: transform .12s, background .2s;
}
.fab:active { transform: scale(.94); }

.dock {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(14px + env(safe-area-inset-bottom)); z-index: 12;
  width: min(calc(100% - 32px), 340px); display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; padding: 6px;
  background: color-mix(in srgb, var(--surface) 88%, transparent); -webkit-backdrop-filter: blur(16px); backdrop-filter: blur(16px);
  border-radius: 999px; box-shadow: 0 8px 28px rgba(0, 0, 0, .16), 0 0 0 1px var(--line);
}
.dock button { border: 0; background: none; border-radius: 999px; padding: 7px 0 6px; display: grid; justify-items: center; gap: 1px; color: var(--muted); transition: background .2s var(--ease), color .2s; }
.dock button b { font-size: 11px; font-weight: 600; }
.dock button.on { background: var(--accent-soft); color: var(--accent); }

/* ========== 編輯畫面 ========== */
.sheet { position: fixed; inset: 0; background: var(--bg); z-index: 20; overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; }
.sheet:not([hidden]) .sheet-inner { animation: slide-up .3s var(--ease); }
.sheet-inner { max-width: 720px; margin: 0 auto; min-height: 100%; display: flex; flex-direction: column; }
.sheet-head { position: sticky; top: 0; z-index: 2; display: grid; grid-template-columns: 44px 1fr 44px; align-items: center; gap: 8px; padding: calc(8px + env(safe-area-inset-top)) 12px 8px; background: color-mix(in srgb, var(--bg) 92%, transparent); -webkit-backdrop-filter: blur(14px); backdrop-filter: blur(14px); }
.date-pill { position: relative; justify-self: center; display: inline-flex; align-items: center; justify-content: center; min-height: 40px; padding: 4px 18px; background: var(--surface); border-radius: 999px; box-shadow: var(--shadow); font-size: 14px; font-weight: 600; cursor: pointer; max-width: 100%; }
.date-pill input { position: absolute; inset: 0; width: 100%; height: 100%; opacity: 0; border: 0; padding: 0; cursor: pointer; }
.icon-btn.save { background: var(--accent); color: var(--accent-ink); width: 40px; height: 40px; justify-self: end; }
.icon-btn.save:hover, .icon-btn.save:active { background: var(--accent); filter: brightness(.92); }
.sheet-body { padding: 8px 16px 16px; display: grid; gap: 14px; flex: 1; align-content: start; }
.primary { background: var(--accent); color: var(--accent-ink); border: 0; border-radius: 999px; padding: 8px 22px; font-weight: 700; }
.seg { display: grid; grid-template-columns: 1fr 1fr; background: var(--line); border-radius: 13px; padding: 3px; gap: 3px; }
.seg button { border: 0; background: none; padding: 10px 0; border-radius: 10px; font-weight: 700; color: var(--muted); transition: background .2s var(--ease), color .2s; }
.seg button.on { background: var(--accent); color: var(--accent-ink); }
.field { display: grid; gap: 6px; }
.field > span { font-size: 13px; color: var(--muted); font-weight: 600; }
.moods { display: flex; gap: 6px; flex-wrap: wrap; }
.moods button { width: 46px; height: 46px; font-size: 24px; border-radius: 12px; border: 2px solid transparent; background: var(--surface); }
.moods button.on { border-color: var(--life); background: var(--life-soft); }

/* 標題 + 內容（像一頁紙） */
.paper { background: var(--surface); border-radius: var(--radius); padding: 12px 16px 14px; box-shadow: var(--shadow); }
.title-input { width: 100%; border: 0; border-bottom: 1px solid var(--line); background: none; color: var(--text); font: inherit; font-size: 20px; font-weight: 700; padding: 6px 0 10px; margin-bottom: 10px; border-radius: 0; }
.title-input:focus { outline: none; border-bottom-color: var(--accent); }
.paper textarea { display: block; width: 100%; border: 0; background: none; color: var(--text); font: inherit; font-size: 17px; line-height: 1.75; padding: 0; resize: none; min-height: 200px; overflow: hidden; }
.paper textarea:focus { outline: none; }
.paper textarea::placeholder, .title-input::placeholder { color: var(--muted); opacity: .7; }

/* 業務欄位：約 / 見 / 簽 / 保費 */
.kpi-grid { display: grid; grid-template-columns: 1fr 1fr 1fr 1.7fr; gap: 8px; }
.kpi { display: grid; gap: 4px; text-align: left; align-content: start; }
.kpi b { font-size: 14px; color: var(--work); text-align: left; padding-left: 2px; }
.kpi input { text-align: left; padding: 10px 12px; }

/* 照片 */
.photo-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.ph { position: relative; aspect-ratio: 1; border-radius: 14px; overflow: hidden; background: var(--line); }
.ph-img { display: block; width: 100%; height: 100%; padding: 0; border: 0; background: none; }
.ph-img img { width: 100%; height: 100%; object-fit: cover; display: block; }
.ph-del { position: absolute; top: 6px; right: 6px; width: 26px; height: 26px; border-radius: 50%; border: 0; background: rgba(0, 0, 0, .55); color: #fff; display: grid; place-items: center; }
.lightbox { position: fixed; inset: 0; z-index: 50; background: rgba(0, 0, 0, .92); display: grid; place-items: center; padding: 16px; animation: fade-in .2s ease; }
.lightbox img { max-width: 100%; max-height: 100%; object-fit: contain; border-radius: 8px; }
.lightbox .icon-btn { position: absolute; top: calc(12px + env(safe-area-inset-top)); right: 12px; color: #fff; background: rgba(255, 255, 255, .16); }

/* 錄音 */
.rec { background: var(--surface); border-radius: var(--radius); padding: 14px 16px; display: grid; gap: 10px; box-shadow: var(--shadow); }
.rec h3 { margin: 0; font-size: 15px; }
.audio-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.audio-item { display: grid; grid-template-columns: 38px 1fr auto 36px; align-items: center; gap: 10px; background: var(--bg); border-radius: 12px; padding: 6px 4px 6px 8px; }
.a-play { width: 38px; height: 38px; border-radius: 50%; border: 0; background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; }
.a-bar { position: relative; height: 22px; display: flex; align-items: center; cursor: pointer; }
.a-bar::before { content: ""; position: absolute; left: 0; right: 0; height: 4px; border-radius: 4px; background: var(--line); }
.a-bar i { position: relative; display: block; height: 4px; width: 0; border-radius: 4px; background: var(--accent); }
.a-time { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }
.audio-item .icon-btn { width: 36px; height: 36px; color: var(--danger); }
.danger { background: none; border: 1px solid var(--danger); color: var(--danger); border-radius: 12px; padding: 11px; font-weight: 600; }

/* 底部工具列（照片 / 錄音） */
.sheet-bar { position: sticky; bottom: 0; z-index: 2; display: grid; grid-template-columns: 1fr 1fr; gap: 10px; padding: 12px 16px calc(12px + env(safe-area-inset-bottom)); background: linear-gradient(to top, var(--bg) 72%, transparent); }
.sheet-bar.recording { grid-template-columns: 1fr; }
.sheet-bar.recording #photoBtn { display: none; }
.tool-btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; border: 0; border-radius: 14px; padding: 13px 12px; background: var(--surface); color: var(--text); font-weight: 700; font-size: 16px; box-shadow: var(--shadow), 0 0 0 1px var(--line); transition: transform .12s, background .2s; }
.tool-btn:active { transform: scale(.97); }
#recBtn:not(.recording) { color: var(--accent); }
#photoBtn { color: var(--accent); }
#recBtn.recording { background: var(--danger); color: #fff; box-shadow: 0 6px 18px rgba(0, 0, 0, .2); }
#recBtn.recording::before { content: ""; width: 10px; height: 10px; border-radius: 50%; background: #fff; animation: pulse 1s infinite; }
@keyframes pulse { 50% { opacity: .25; } }
.hint { margin: 0; font-size: 13px; color: var(--muted); }
.hint.err { color: var(--danger); min-height: 1.2em; }

/* ========== 選單 / 對話框 ========== */
.modal { position: fixed; inset: 0; background: rgba(0, 0, 0, .45); z-index: 30; display: grid; place-items: end center; }
.modal:not([hidden]) { animation: fade-in .2s ease; }
.modal:not([hidden]) .modal-card { animation: slide-up .3s var(--ease); }
#pw { z-index: 35; }
.modal-card { background: var(--surface); width: min(100%, 520px); border-radius: 22px 22px 0 0; padding: 20px 16px calc(20px + env(safe-area-inset-bottom)); display: grid; gap: 8px; max-height: 92vh; max-height: 92dvh; overflow-y: auto; }
@media (min-width: 640px) { .modal { place-items: center; } .modal-card { border-radius: 22px; } }
.modal-card h2 { margin: 0 0 4px; font-size: 18px; }
.menu-h { margin: 12px 2px 0; font-size: 13px; color: var(--muted); font-weight: 700; letter-spacing: .04em; }
.sec-box { display: grid; gap: 8px; }
.menu-item { text-align: left; background: var(--bg); border: 0; border-radius: 12px; padding: 13px 14px; }
.menu-item:active { filter: brightness(.96); }
.menu-item.close { text-align: center; background: none; color: var(--muted); }
.menu-item.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.menu-item select { border: 1px solid var(--line); background: var(--surface); color: var(--text); border-radius: 8px; padding: 5px 8px; font: inherit; }
.btn-row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; align-items: center; }
.primary.wide { width: 100%; padding: 12px; border-radius: 12px; }

/* ========== 上鎖畫面 ========== */
.lock { position: fixed; inset: 0; z-index: 60; background: var(--bg); display: grid; place-items: center; padding: 24px; }
.lock:not([hidden]) { animation: fade-in .2s ease; }
.lock-card { width: min(100%, 340px); display: grid; gap: 12px; text-align: center; }
.lock-card img { margin: 0 auto 4px; border-radius: 18px; }
.lock-card h2 { margin: 0 0 6px; font-size: 22px; }
.lock-card input { width: 100%; border: 1px solid var(--line); background: var(--surface); color: var(--text); border-radius: 12px; padding: 13px 14px; font: inherit; text-align: center; font-size: 18px; letter-spacing: .1em; }
.lock-card input:focus { outline: 2px solid var(--brand); outline-offset: 1px; border-color: transparent; }
.lock .primary { background: var(--brand); color: var(--accent-ink); }
.bio-btn { border: 1px solid var(--line); background: var(--surface); border-radius: 12px; padding: 12px; font-weight: 600; }

.toast { position: fixed; left: 50%; bottom: calc(100px + env(safe-area-inset-bottom)); transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 999px; font-size: 14px; z-index: 70; max-width: calc(100% - 32px); text-align: center; animation: toast-in .25s var(--ease); }

@keyframes slide-up { from { transform: translateY(28px); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
@keyframes toast-in { from { opacity: 0; transform: translate(-50%, 8px); } to { opacity: 1; transform: translate(-50%, 0); } }

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