/* ===== 主題色彩變數（淺色 / 深色）===== */
:root {
  --bg: #EDE8DE;
  --card: #F7F4EC;
  --ink: #2B3A45;
  --ink-2: #4C5D68;
  --matcha: #7A9B76;
  --matcha-bg: #DCE6D8;
  --brick: #B8604F;
  --amber: #BA7517;
  --amber-bg: #FAEEDA;
  --gray: #8B8578;
  --gray-bg: #E3DED2;
  --divider: rgba(43,58,69,.12);
}

body[data-theme="dark"] {
  --bg: #221F1A;
  --card: #2E2A22;
  --ink: #EDE8DE;
  --ink-2: #8FA0AA;
  --matcha: #8FB88A;
  --matcha-bg: #2E3B2C;
  --brick: #D08170;
  --amber: #E0A542;
  --amber-bg: #3B2F1A;
  --gray: #6E7680;
  --gray-bg: #2A2D31;
  --divider: rgba(237,232,222,.12);
}

* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: 'Noto Sans TC', sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: contain;
}
h1, h2, h3, .serif { font-family: 'Noto Serif TC', serif; }
.mono { font-family: 'JetBrains Mono', monospace; }

#app {
  max-width: 480px;
  margin: 0 auto;
  min-height: 100vh;
  padding-bottom: 76px;
  position: relative;
}

/* ===== Header ===== */
.app-header {
  display: flex; align-items: center; justify-content: space-between;
  /* 頂部預留 iOS 狀態列安全區（桌面瀏覽器 env() 為 0，不影響） */
  padding: calc(14px + env(safe-area-inset-top)) 16px 8px;
}
.app-title { font-size: 18px; font-weight: 700; margin: 0; }
.app-title-en { font-family: 'JetBrains Mono', monospace; font-size: 12px; color: var(--ink-2); font-weight: 400; }

.icon-btn {
  background: none; border: none; color: var(--ink-2);
  width: 34px; height: 34px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 18px; cursor: pointer;
}
.icon-btn:active { background: var(--divider); }

/* ===== Views / Tabs ===== */
.view { display: none; padding: 8px 12px 16px; }
.view.active { display: block; }

.tab-bar {
  position: fixed; bottom: 0; left: 50%; transform: translateX(-50%);
  width: 100%; max-width: 480px;
  display: flex; background: var(--card);
  border-top: 0.5px solid var(--divider);
  padding: 6px 0 max(6px, env(safe-area-inset-bottom));
  z-index: 40; /* 蓋在時間軸卡片(2~5)之上，但低於 sheet(50)/ghost(100)/modal(200) */
}
.tab-btn {
  flex: 1; background: none; border: none; color: var(--gray);
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  font-size: 11px; padding: 6px 0; cursor: pointer;
}
.tab-btn.active { color: var(--matcha); font-weight: 500; }
.tab-icon { font-size: 18px; }

/* ===== 今日待辦：月曆區 ===== */
.calendar-block { margin-bottom: 10px; }
.calendar-block .month-nav { margin: 2px 4px 8px; }
.cal-cell { position: relative; }
.cal-cell .day-num { font-size: 10px; }
.cal-cell .day-dot { width: 4px; height: 4px; border-radius: 50%; position: absolute; bottom: 3px; left: 50%; transform: translateX(-50%); }
.cal-cell.selected { background: var(--matcha); color: var(--card); border: 1.5px solid var(--brick); }
.collapse-handle { text-align: center; color: var(--gray); font-size: 14px; padding: 2px 0; cursor: pointer; }

.week-strip { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; margin-bottom: 4px; }
.week-strip .wd-col { text-align: center; }
.week-strip .wd-label { font-size: 9px; color: var(--gray); }
.week-strip .wd-num {
  width: 28px; height: 28px; border-radius: 50%; margin: 2px auto 0;
  display: flex; align-items: center; justify-content: center; font-size: 11px; color: var(--ink);
  cursor: pointer;
}
.week-strip .wd-num.selected { background: var(--matcha); color: var(--card); border: 1.5px solid var(--brick); font-weight: 500; }

/* ===== 待規劃 ===== */
.planning-panel { margin-bottom: 12px; }
.planning-list { display: flex; flex-direction: column; gap: 6px; }
.planning-item {
  background: var(--card); border-radius: 10px; padding: 8px 10px;
  display: flex; align-items: center; gap: 8px; border: 0.5px solid var(--divider);
  touch-action: none;
}
.planning-item .drag-handle { color: var(--gray); font-size: 14px; cursor: grab; padding: 0 2px; }
.planning-item .p-name { flex: 1; font-size: 12px; }
.planning-item .p-hint { font-size: 10px; color: var(--gray); }
.drag-ghost {
  position: fixed; z-index: 100; pointer-events: none;
  background: var(--card); border: 1px solid var(--matcha); border-radius: 10px;
  padding: 8px 12px; font-size: 12px; color: var(--ink); box-shadow: 0 4px 12px rgba(0,0,0,.2);
  opacity: .95;
}
.drop-hint { outline: 2px dashed var(--matcha); outline-offset: 2px; }

.selected-date-label { font-size: 13px; font-weight: 500; color: var(--ink-2); margin: 6px 4px 8px; font-family: 'Noto Serif TC', serif; }

/* ===== 未排定時間區 ===== */
.unscheduled-panel { margin-bottom: 12px; }
.panel-label { font-size: 12px; color: var(--ink-2); margin: 6px 4px; }
.unscheduled-list { display: flex; flex-direction: column; gap: 6px; }

/* ===== 卡片 ===== */
.todo-card {
  background: var(--card);
  border-radius: 10px;
  padding: 9px 10px;
  display: flex; align-items: center; gap: 10px;
  border: 0.5px solid var(--divider);
  position: relative;
  overflow: hidden;
}
.todo-card .status-dot {
  width: 20px; height: 20px; border-radius: 50%; flex-shrink: 0;
  border: 2px solid var(--ink-2); cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  font-size: 11px; color: var(--card);
}
.todo-card .drag-handle { color: var(--gray); font-size: 14px; cursor: grab; padding: 0 2px; flex-shrink: 0; }
.todo-card .card-body { flex: 1; min-width: 0; }
.todo-card .card-title { font-size: 13px; line-height: 1.4; }
.todo-card .card-note {
  font-size: 11px; color: var(--ink-2); margin-top: 2px; line-height: 1.4;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.todo-card .habit-tag, .todo-card .cross-day-tag {
  font-size: 10px; background: var(--matcha-bg); color: var(--matcha);
  padding: 1px 6px; border-radius: 6px; margin-left: 6px; white-space: nowrap;
}
.todo-card .cross-day-tag { background: var(--amber-bg); color: var(--amber); }
.todo-card .edit-btn {
  background: var(--ink); color: var(--card); border: none;
  width: 40px; align-self: stretch; border-radius: 8px; font-size: 12px; cursor: pointer;
  flex-shrink: 0;
}

/* 5 種狀態 */
.todo-card[data-status="pending"] .status-dot { background: var(--card); }
.todo-card[data-status="half"] { background: var(--amber-bg); }
.todo-card[data-status="half"] .status-dot {
  background: conic-gradient(var(--amber) 0deg 180deg, transparent 180deg 360deg);
  border-color: var(--amber);
}
.todo-card[data-status="done"] { background: var(--matcha-bg); }
.todo-card[data-status="done"] .status-dot { background: var(--matcha); border-color: var(--matcha); }
.todo-card[data-status="delayed"] { background: var(--amber-bg); }
.todo-card[data-status="delayed"] .status-dot { background: var(--amber); border-color: var(--amber); }
.todo-card[data-status="cancelled"] { background: var(--gray-bg); }
.todo-card[data-status="cancelled"] .status-dot { background: var(--gray); border-color: var(--gray); }
.todo-card[data-status="cancelled"] .card-title { text-decoration: line-through; color: var(--gray); }

/* 滑開露出編輯鈕：卡片本體預設蓋在上面(z-index 較高)，滑開後才露出下面的編輯鈕 */
.todo-card-wrap { position: relative; z-index: 0; }
.todo-card-wrap .edit-reveal {
  position: absolute; right: 0; top: 0; bottom: 0; width: 56px;
  background: var(--ink); color: var(--card);
  display: flex; align-items: center; justify-content: center;
  font-size: 12px; border-radius: 0 10px 10px 0;
  z-index: 1;
}
.todo-card-wrap .todo-card { transition: transform .15s ease; touch-action: pan-y; position: relative; z-index: 2; }
.todo-card-wrap.swiped .todo-card { transform: translateX(-56px); }
.todo-card-wrap.dragging .todo-card { transition: none; box-shadow: 0 4px 10px rgba(0,0,0,.18); }

/* ===== 時間軸 ===== */
.timeline { position: relative; border-top: 0.5px solid var(--divider); }
.timeline-hour {
  height: 60px; border-bottom: 0.5px solid var(--divider);
  position: relative; display: flex;
}
.timeline-hour::after {
  content: ''; position: absolute; left: 46px; right: 0; top: 50%;
  border-top: 1px dashed var(--divider);
}
.timeline-hour .hour-label {
  width: 44px; flex-shrink: 0; font-size: 10px; color: var(--gray);
  padding-top: 2px; font-family: 'JetBrains Mono', monospace;
}
.timeline-hour .hour-slot { flex: 1; position: relative; }
/* 卡片實際 left/right 由 JS 依 .hour-label 寬度設為 52px/8px，避免壓到時間文字 */
.timeline .scheduled-card {
  position: absolute; left: 52px; right: 8px; z-index: 2; min-height: 44px;
}
.scheduled-card .todo-card { height: 100%; }
/* 時間軸卡片改成漢堡把手拖曳改時間＋點卡片開細節，卡片本體不再攔截垂直手勢，
   維持 pan-y 讓頁面可以正常捲動；只有漢堡把手跟下緣把手才要交給 JS 自己處理拖曳，
   避免瀏覽器原生垂直捲動(pan-y)跟自訂拖曳手勢互相搶手勢。 */
.timeline .scheduled-card .drag-handle { touch-action: none; }
.resize-handle {
  position: absolute; left: 0; right: 0; bottom: 0; height: 12px;
  cursor: ns-resize; z-index: 3; display: flex; align-items: flex-end; justify-content: center;
  touch-action: none;
}
.resize-handle::after {
  content: ''; width: 28px; height: 3px; border-radius: 2px; background: var(--ink-2); opacity: .5; margin-bottom: 3px;
}

/* ===== FAB ===== */
.fab {
  position: fixed; bottom: 84px; right: calc(50% - 240px + 16px);
  width: 50px; height: 50px; border-radius: 50%;
  background: var(--ink); color: var(--card); border: none;
  font-size: 22px; box-shadow: 0 2px 8px rgba(0,0,0,.2); cursor: pointer;
  z-index: 5;
}
@media (max-width: 480px) { .fab { right: 16px; } }

/* ===== 月份導覽 ===== */
.month-nav { display: flex; align-items: center; justify-content: space-between; margin: 6px 4px 12px; }
.month-label { font-size: 15px; font-weight: 500; font-family: 'Noto Serif TC', serif; }

/* ===== 習慣列表 ===== */
.habit-list { display: flex; flex-direction: column; gap: 10px; }
.habit-card { background: var(--card); border-radius: 12px; padding: 12px; border: 0.5px solid var(--divider); cursor: pointer; }
.habit-card-head { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
.habit-icon { width: 30px; height: 30px; border-radius: 50%; flex-shrink: 0; display:flex; align-items:center; justify-content:center; color:#fff; font-size:13px; }
.habit-name { font-size: 14px; font-weight: 500; }
.habit-streak { font-size: 11px; color: var(--gray); margin-left: auto; }
.habit-ended-badge { font-size: 10px; color: var(--gray); background: var(--gray-bg); padding: 1px 6px; border-radius: 6px; margin-left: 6px; }

.calendar-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 3px; }
.calendar-grid .cal-cell {
  aspect-ratio: 1; border-radius: 6px; display: flex; align-items: center; justify-content: center;
  font-size: 10px; color: var(--ink-2); background: var(--bg); cursor: pointer;
}
.calendar-grid .cal-cell.dow { background: none; color: var(--gray); font-size: 9px; cursor: default; }
.calendar-grid .cal-cell.empty { background: none; cursor: default; }
.calendar-grid .cal-cell.done { background: var(--matcha); color: var(--card); }
.calendar-grid .cal-cell.half { background: var(--amber); color: var(--card); }
.calendar-grid .cal-cell.cancelled { background: var(--gray-bg); color: var(--gray); text-decoration: line-through; }
.calendar-grid .cal-cell.today { border: 1.5px solid var(--brick); }

/* ===== 統計 ===== */
/* 兩欄格線排版：每個習慣一張小卡並排顯示，手機寬度也放得下（熱力圖已縮成純色塊，不需要文字空間） */
.stats-list { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.stats-item { background: var(--card); border-radius: 12px; padding: 10px; border: 0.5px solid var(--divider); min-width: 0; }
.stats-item-head { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: baseline; font-size: 12px; margin-bottom: 6px; gap: 2px 6px; }
.stats-item-head .rate { font-family: 'JetBrains Mono', monospace; color: var(--matcha); }
.stats-item-head .rate-detail { font-size: 9px; color: var(--gray); margin-left: 4px; }
.progress-bar { height: 5px; border-radius: 3px; background: var(--gray-bg); overflow: hidden; margin-bottom: 8px; }
.progress-bar-fill { height: 100%; background: var(--matcha); }
/* 熱力圖只留顏色色塊：拿掉星期標題列跟日期數字，格子縮小，仍照星期對齊(月初空白格保留) */
.stats-heatmap { display: grid; grid-template-columns: repeat(7, 1fr); gap: 2px; }
.stats-heatmap .hm-cell { aspect-ratio: 1; border-radius: 3px; background: var(--bg); }
.stats-heatmap .hm-cell.empty { background: none; }
.stats-heatmap .hm-cell.done { background: var(--matcha); }
.stats-heatmap .hm-cell.half { background: var(--amber); }

@media (max-width: 360px) {
  /* 極窄螢幕改回單欄，避免熱力圖格子小到看不出顏色 */
  .stats-list { grid-template-columns: 1fr; }
}

/* ===== Modal ===== */
.modal-mask {
  position: fixed; inset: 0; background: rgba(0,0,0,.4);
  display: flex; align-items: flex-end; justify-content: center; z-index: 50;
}
.modal-sheet {
  background: var(--bg); width: 100%; max-width: 480px;
  border-radius: 18px 18px 0 0; padding: 18px 16px calc(18px + env(safe-area-inset-bottom));
  max-height: 88vh; overflow-y: auto;
}
.modal-title { font-size: 16px; font-weight: 500; text-align: center; margin: 0 0 14px; font-family: 'Noto Serif TC', serif; }
.field { margin-bottom: 12px; }
.field label { display: block; font-size: 11px; color: var(--ink-2); margin-bottom: 4px; }
.field input[type="text"], .field input[type="password"], .field input[type="time"], .field input[type="number"], .field textarea {
  width: 100%; background: var(--card); border: 0.5px solid var(--divider);
  border-radius: 10px; padding: 9px 10px; font-size: 13px; color: var(--ink);
  font-family: inherit;
}
.field textarea { resize: vertical; min-height: 50px; }
.chip-row { display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
  font-size: 11px; padding: 6px 10px; border-radius: 8px; cursor: pointer;
  background: var(--card); border: 0.5px solid var(--divider); color: var(--ink-2);
}
.chip.active { background: var(--matcha-bg); color: var(--matcha); font-weight: 500; border-color: transparent; }
.weekday-picker { display: flex; justify-content: space-between; margin-top: 8px; }
.weekday-picker .wd {
  width: 30px; height: 30px; border-radius: 50%; display: flex; align-items: center; justify-content: center;
  font-size: 11px; background: var(--gray-bg); color: var(--gray); cursor: pointer;
}
.weekday-picker .wd.active { background: var(--matcha); color: var(--card); font-weight: 500; }
.color-row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.color-dot { width: 28px; height: 28px; border-radius: 50%; cursor: pointer; border: 2px solid transparent; }
.color-dot.active { border-color: var(--ink); }
.switch-row { display: flex; align-items: center; justify-content: space-between; padding-top: 8px; border-top: 0.5px solid var(--divider); margin-top: 4px; }
.switch { width: 38px; height: 22px; border-radius: 11px; background: var(--gray-bg); position: relative; cursor: pointer; flex-shrink: 0; }
.switch.on { background: var(--matcha); }
.switch .knob { width: 18px; height: 18px; border-radius: 50%; background: var(--card); position: absolute; top: 2px; left: 2px; transition: left .15s; }
.switch.on .knob { left: 18px; }

.btn { display: block; width: 100%; text-align: center; padding: 11px; border-radius: 10px; font-size: 14px; border: none; cursor: pointer; margin-top: 14px; }
.btn-primary { background: var(--ink); color: var(--card); }
.btn-ghost { background: var(--card); color: var(--ink); border: 0.5px solid var(--divider); }

.other-actions { margin-top: 18px; }
.other-actions .label { font-size: 11px; color: var(--gray); margin-bottom: 4px; }
.action-row {
  display: flex; align-items: center; gap: 8px; padding: 11px 4px;
  border-top: 0.5px solid var(--divider); cursor: pointer;
}
.action-row .a-title { font-size: 13px; }
.action-row .a-desc { font-size: 10px; color: var(--gray); }
.action-row.end .a-title { color: var(--amber); }
.action-row.delete .a-title { color: var(--brick); }

.empty-hint { text-align: center; color: var(--gray); font-size: 12px; padding: 24px 0; }

/* ===== Header 右側按鈕群 ===== */
.header-actions { display: flex; align-items: center; gap: 2px; }

/* ===== 登入畫面 ===== */
.login-screen {
  position: fixed; inset: 0; z-index: 200;
  display: flex; align-items: center; justify-content: center;
  background: var(--bg);
  /* 頂部預留狀態列安全區，避免登入卡片頂到瀏海／狀態列 */
  padding: calc(24px + env(safe-area-inset-top)) 24px calc(24px + env(safe-area-inset-bottom));
}
.login-card {
  width: 100%; max-width: 320px;
  background: var(--card);
  border: 0.5px solid var(--divider);
  border-radius: 16px;
  padding: 28px 22px;
  box-shadow: 0 6px 28px rgba(43, 58, 69, .10);
}
.login-logo {
  font-family: 'Noto Serif TC', serif;
  font-size: 24px; font-weight: 700; color: var(--ink);
  text-align: center; margin-bottom: 22px;
}
.login-error {
  min-height: 18px; margin-top: 2px;
  font-size: 12px; color: var(--brick); text-align: center;
}

/* ===== 版本號小字 ===== */
.app-version {
  text-align: center; font-size: 10px; color: var(--gray);
  font-family: 'JetBrains Mono', monospace;
  padding: 4px 0 2px;
}
.login-card .app-version { margin-top: 14px; }

/* ===== Toast 提示（同步衝突等）===== */
.toast {
  position: fixed; left: 50%; bottom: 92px; transform: translateX(-50%) translateY(8px);
  max-width: 88%; z-index: 210;
  background: var(--ink); color: var(--card);
  font-size: 12px; line-height: 1.5; text-align: center;
  padding: 10px 16px; border-radius: 10px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, .22);
  opacity: 0; pointer-events: none;
  transition: opacity .2s ease, transform .2s ease;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
