:root {
  color-scheme: light; /* 让 datetime-local、数字输入框等原生控件跟随主题 */
  --bg: #f2f2f7; --card: #ffffff; --text: #1c1c1e; --sub: #8e8e93;
  --line: rgba(0, 0, 0, .08);
  --shadow: 0 1px 2px rgba(0, 0, 0, .04), 0 10px 30px rgba(0, 0, 0, .07);
  --accent: #6366f1; --danger: #ef4444;
  --amber: #f59e0b; /* 数量语义色：凡显示“数量”的地方都用它，与主题色（结构/选中）区分 */
  --tabbar: rgba(255, 255, 255, .84); /* 底部导航的毛玻璃底色 */
}
/* 「跟随系统」：手动选浅色时用 :not 排除，否则系统深色会把手动浅色覆盖掉 */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0d0d12; --card: #1b1b23; --text: #f2f2f7; --sub: #9a9aa2;
    --line: rgba(255, 255, 255, .09);
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 12px 32px rgba(0, 0, 0, .45);
    --tabbar: rgba(24, 24, 31, .84);
  }
}
/* 手动选深色：系统是浅色也要变深 */
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0d0d12; --card: #1b1b23; --text: #f2f2f7; --sub: #9a9aa2;
  --line: rgba(255, 255, 255, .09);
  --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 12px 32px rgba(0, 0, 0, .45);
  --tabbar: rgba(24, 24, 31, .84);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html { -webkit-text-size-adjust: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  background: var(--bg); color: var(--text);
  min-height: 100vh;
  min-height: 100dvh;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  /* 底部留白要盖住固定的导航栏，否则「＋ 新建习惯」会被挡住 */
  padding: calc(env(safe-area-inset-top) + 18px) 16px calc(env(safe-area-inset-bottom) + 84px);
}
#app { max-width: 480px; margin: 0 auto; }
.page[hidden] { display: none; }

header { padding: 6px 4px 14px; }
.head-row { display: flex; align-items: flex-start; justify-content: space-between; gap: 10px; }
.head-title { min-width: 0; flex: 1; }
header h1 { font-size: 30px; font-weight: 800; letter-spacing: -.5px; min-width: 0; white-space: nowrap; }
header .date { color: var(--sub); font-size: 14px; margin-top: 3px; display: flex; align-items: center; gap: 7px; }
/* 刷新键：紧贴标题右侧，做得比齿轮小一号，不抢标题的视觉重心 */
/* 刷新键放在原来齿轮的位置：右上角，和标题行同一视觉高度 */
.refresh { width: 36px; height: 36px; flex-shrink: 0; color: var(--sub); margin-top: 4px; }
.refresh svg { width: 19px; height: 19px; }
.roundbtn.spin { animation: spin .7s ease; }
@keyframes spin { to { transform: rotate(360deg); } }

/* 同步状态点 */
.syncdot {
  width: 8px; height: 8px; border-radius: 50%; background: var(--sub); flex-shrink: 0;
}
.syncdot.big { width: 10px; height: 10px; }
.syncdot[data-state="ok"] { background: #22c55e; }
.syncdot[data-state="syncing"], .syncdot[data-state="pending"] { background: var(--amber); }
.syncdot[data-state="error"], .syncdot[data-state="badtoken"] { background: var(--danger); }
.syncdot[data-state="noserver"] { background: var(--sub); }

/* 本地存储告警 */
.warn {
  font-size: 12px; line-height: 1.6; color: var(--danger);
  border: 1px solid currentColor; border-radius: 12px;
  padding: 9px 12px; margin-bottom: 12px;
}
.warn[hidden] { display: none; }

/* 轻提示 */
.toast {
  position: fixed; left: 50%; bottom: calc(env(safe-area-inset-bottom) + 92px); /* 抬到导航栏上方 */
  transform: translate(-50%, 10px); opacity: 0; pointer-events: none;
  background: rgba(0, 0, 0, .8); color: #fff; font-size: 13px; line-height: 1.4;
  padding: 9px 16px; border-radius: 999px; z-index: 20; max-width: 78vw; text-align: center;
  transition: opacity .2s ease, transform .2s ease;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---- 长条形习惯卡片 ---- */
.card {
  background: var(--card); border: 1px solid var(--line); border-radius: 20px;
  box-shadow: var(--shadow); padding: 12px 12px 14px 16px; margin-bottom: 12px;
  user-select: none; -webkit-user-select: none;
}
.card-top { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.card-title { display: flex; align-items: baseline; gap: 10px; flex: 1; min-width: 0; }
.card-top h2 {
  font-size: 16px; font-weight: 650; min-width: 0;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.weeksum { flex-shrink: 0; font-size: 12px; color: var(--sub); white-space: nowrap; }
.weeksum b { color: var(--text); font-weight: 650; font-variant-numeric: tabular-nums; }
.topbtns { display: flex; gap: 6px; }
.roundbtn {
  width: 30px; height: 30px; border-radius: 50%; border: 1px solid var(--line);
  background: none; color: var(--sub); font-size: 15px; line-height: 1; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
.roundbtn:active { background: var(--line); }
.more { font-size: 20px; }

.card-main { display: flex; align-items: center; gap: 14px; }

/* 今日计数（点按 +1 的唯一区域：香烟图标 + 数字） */
.today {
  position: relative; display: flex; align-items: center; gap: 10px;
  background: none; border: none; color: var(--text); font: inherit;
  padding: 6px 14px 6px 6px; border-radius: 18px; cursor: pointer;
  transition: background .15s ease;
}
.today:active { background: var(--line); }
.cig { width: 30px; height: 30px; color: var(--c, var(--accent)); flex-shrink: 0; display: flex; }
.cig svg { width: 100%; height: 100%; }
.tnum { position: relative; display: flex; flex-direction: column; align-items: center; min-width: 72px; }
.tnum .num { font-size: 48px; font-weight: 500; line-height: 1; font-variant-numeric: tabular-nums; letter-spacing: -1px; }
.tnum .num.pop { animation: pop .24s ease; }
@keyframes pop { 40% { transform: scale(1.25); } }
.tnum .sublabel { font-size: 12px; color: var(--sub); margin-top: 4px; }

/* 本包进度（点击打开记录页） */
.packwrap {
  flex: 1; min-width: 0; display: flex; flex-direction: column; justify-content: center;
  background: none; border: none; color: var(--text); font: inherit; text-align: left;
  padding: 8px 10px; margin: -8px -10px; border-radius: 14px; cursor: pointer;
  transition: background .15s ease;
}
.packwrap:active { background: var(--line); }
.packhead { display: flex; align-items: center; justify-content: space-between; margin-bottom: 6px; }
.packhead .pnum { font-size: 12px; color: var(--sub); }
.packhead .pnum b { font-size: 20px; font-weight: 700; color: var(--text); font-variant-numeric: tabular-nums; }
.packhead .pnum small { font-size: 12px; }
.hic { width: 16px; height: 16px; color: var(--sub); opacity: .75; display: flex; flex-shrink: 0; }
.hic svg { width: 100%; height: 100%; }
.packbar { height: 9px; border-radius: 5px; background: var(--line); overflow: hidden; display: block; }
.packbar i {
  display: block; height: 100%; border-radius: 5px; background: var(--c, var(--accent));
  transition: width .5s cubic-bezier(.22, 1, .36, 1), background .3s;
}
.packbar.low i { background: #f59e0b; }
.totalline { font-size: 12px; color: var(--sub); margin-top: 7px; }
.totalline b { color: var(--text); font-weight: 650; font-variant-numeric: tabular-nums; }

/* 最近 7 天 */
.week { display: flex; gap: 4px; align-items: flex-end; height: 46px; padding-bottom: 2px; }
.bar { width: 7px; border-radius: 3px; background: var(--line); }
.bar.hit { background: color-mix(in srgb, var(--c, var(--accent)) 45%, transparent); }
.bar.now { background: var(--c, var(--accent)); }

.plus1 {
  position: absolute; left: 50%; top: -4px; font-size: 16px; font-weight: 800;
  color: var(--c, var(--accent)); pointer-events: none;
}

/* ---- 空状态 ---- */
.empty { text-align: center; padding: 44px 12px 30px; color: var(--sub); }
.empty .big { font-size: 44px; margin-bottom: 10px; }
.empty p { font-size: 15px; line-height: 1.7; }
.chips { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; margin-top: 18px; }
.chip {
  border: 1px solid var(--line); background: var(--card); color: var(--text);
  border-radius: 999px; padding: 10px 18px; font-size: 14px; cursor: pointer;
  box-shadow: var(--shadow); transition: transform .12s ease;
}
.chip:active { transform: scale(.96); }
.add {
  width: 100%; border: 1.5px dashed var(--line); background: none; color: var(--sub);
  border-radius: 24px; padding: 17px; font-size: 15px; cursor: pointer;
}

/* ---- 底部弹层 ---- */
.backdrop {
  position: fixed; inset: 0; background: rgba(0, 0, 0, .45);
  opacity: 0; pointer-events: none; transition: opacity .25s; z-index: 10;
}
.backdrop.show { opacity: 1; pointer-events: auto; }
.sheet {
  position: fixed; left: 50%; bottom: 0; transform: translate(-50%, 105%);
  width: min(480px, 100%); background: var(--card); color: var(--text);
  border-radius: 28px 28px 0 0; border: 1px solid var(--line); border-bottom: none;
  padding: 10px 20px calc(env(safe-area-inset-bottom) + 20px);
  max-height: calc(100dvh - 16px); overflow-y: auto; overscroll-behavior: contain;
  z-index: 11; transition: transform .32s cubic-bezier(.32, .72, .33, 1);
}
.sheet.show { transform: translate(-50%, 0); }
.grab { width: 38px; height: 5px; border-radius: 3px; background: var(--line); margin: 6px auto 16px; }
.sheet h3 { font-size: 17px; text-align: center; margin-bottom: 16px; }
.field { margin-bottom: 14px; }
.field label { display: block; font-size: 12px; color: var(--sub); margin-bottom: 7px; }
.field input {
  width: 100%; padding: 13px 14px; border-radius: 14px; border: 1px solid var(--line);
  background: var(--bg); color: var(--text); font-size: 16px; outline: none; font-family: inherit;
}
.field input:focus { border-color: var(--accent); }
.colors { display: flex; gap: 10px; flex-wrap: wrap; padding-top: 4px; }
.swatch { width: 34px; height: 34px; border-radius: 50%; border: 3px solid transparent; cursor: pointer; padding: 0; }
.swatch.sel { border-color: var(--text); }
.sheet-actions { display: flex; gap: 12px; margin-top: 20px; }
.btn {
  flex: 1; padding: 15px; border-radius: 16px; border: none;
  font-size: 16px; font-weight: 650; cursor: pointer; font-family: inherit;
}
.btn.primary { background: var(--accent); color: #fff; }
.btn.danger { background: none; border: 1px solid var(--danger); color: var(--danger); flex: 0 0 auto; padding: 15px 26px; }
.btn.danger.confirm { background: var(--danger); color: #fff; }
.btn.danger[hidden] { display: none; }

/* ---- 卡片底部内嵌记录面板 ---- */
.records {
  border-top: 1px solid var(--line);
  margin: 12px -12px -14px -16px; padding: 10px 12px 12px 16px;
  animation: unfold .28s cubic-bezier(.22, 1, .36, 1);
}
@keyframes unfold { from { opacity: 0; transform: translateY(-8px); } }
.rc-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.rc-nav { display: flex; align-items: center; gap: 2px; }
.rc-nav button {
  border: none; background: none; color: var(--sub); font-size: 18px; line-height: 1;
  padding: 2px 10px; cursor: pointer; border-radius: 8px; font-family: inherit;
}
.rc-nav button:active { background: var(--line); }
.rc-nav button:disabled { opacity: .3; cursor: default; }
.rc-nav button:disabled:active { background: none; }
.rc-date {
  color: var(--text); font-size: 15px; font-weight: 650; min-width: 128px;
  text-align: center; font-variant-numeric: tabular-nums; font-family: inherit;
  display: flex; flex-direction: column; align-items: center; line-height: 1.35;
}
.rc-date:active { background: var(--line); }
.rc-modes {
  display: flex; gap: 2px; background: var(--bg); border: 1px solid var(--line);
  border-radius: 999px; padding: 3px; margin-top: 8px;
}
.rc-mode {
  flex: 1; border: none; background: none; color: var(--sub); font-size: 13px;
  padding: 7px 0; border-radius: 999px; cursor: pointer; font-family: inherit;
}
.rc-mode.sel { background: var(--c, var(--accent)); color: #fff; font-weight: 650; }
.rc-mode:active:not(.sel) { background: var(--line); }

/* ---- 日历选择框 ---- */
.cal {
  margin-top: 8px; border: 1px solid var(--line); border-radius: 14px;
  padding: 12px 10px 10px; background: var(--bg); animation: unfold .2s ease;
}
.cal-head { display: flex; align-items: center; justify-content: center; gap: 2px; margin-bottom: 6px; }
.cal-nav {
  border: none; background: none; color: var(--sub); font-size: 15px; line-height: 1;
  padding: 4px 9px; cursor: pointer; border-radius: 8px; font-family: inherit;
}
.cal-nav:active { background: var(--line); }
.cal-nav:disabled { opacity: .3; cursor: default; }
.cal-title { font-size: 13px; font-weight: 650; min-width: 92px; text-align: center; }
.cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 2px; text-align: center; }
.cal.wk-grid .cal-grid { grid-template-columns: repeat(7, 1fr) 46px; }
.cal-wkhead { font-size: 11px; color: var(--sub); }
.cal-wsum {
  display: flex; align-items: center; justify-content: center;
  font-size: 15px; font-weight: 700; color: var(--sub); font-variant-numeric: tabular-nums;
}
.cal-wsum.wk { font-weight: 800; }
.cal-wsum.pos { color: var(--amber); }
.cal-dow { font-size: 11px; color: var(--sub); padding: 2px 0 5px; }
.cal-day {
  position: relative; border: none; background: none; color: var(--text);
  padding: 6px 0 5px; border-radius: 10px; cursor: pointer;
  font-family: inherit; font-variant-numeric: tabular-nums;
}
.cal-day .dnum { display: block; font-size: 17px; line-height: 1.15; }
/* 数量徽章：实心琥珀底白字，与大号日期数字明确区分 */
.cal-day .cnt {
  display: inline-block; margin-top: 3px; padding: 1px 8px;
  font-size: 12px; font-weight: 700; line-height: 17px; border-radius: 10px;
  background: var(--amber);
  color: #fff;
}
.cal-day .cnt:empty { display: none; }
.cal-day.sel .cnt { background: #fff; color: var(--amber); }
.cal-day.wk { background: color-mix(in srgb, var(--c, var(--accent)) 10%, transparent); border-radius: 4px; }
.cal-day.out { opacity: .5; cursor: default; }
.cal-day.out .dnum { color: var(--sub); }
.cal-day:active:not(.out) { background: var(--line); }
.cal-day:disabled { cursor: default; }
.cal-day:disabled:active { background: none; }
.cal-day.cal-now { box-shadow: inset 0 0 0 1.5px var(--c, var(--accent)); }
.cal-day.sel { background: var(--c, var(--accent)); color: #fff; font-weight: 650; }

/* ---- 月模式：12 个月年历网格（3×4，月份左 · 数量右） ---- */
.mg-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.mg-cell {
  display: flex; align-items: center; justify-content: space-between; gap: 6px;
  border: 1px solid var(--line); background: none; border-radius: 14px;
  padding: 14px 13px; cursor: pointer; font-family: inherit; color: var(--text);
  font-variant-numeric: tabular-nums;
}
.mg-cell .dnum { font-size: 15px; font-weight: 650; line-height: 1.2; }
.mg-cell .cnt {
  font-size: 15px; font-weight: 750; line-height: 1.2;
  color: var(--amber);
}
.mg-cell.sel { background: var(--c, var(--accent)); border-color: transparent; color: #fff; }
.mg-cell.sel .cnt { color: #fff; }
.mg-cell.cal-now { box-shadow: inset 0 0 0 1.5px var(--c, var(--accent)); }
.mg-cell.dis { opacity: .35; }
.mg-cell:disabled { cursor: default; }
.mg-cell:active:not(:disabled) { background: var(--line); }
.rc-stats { font-size: 13px; color: var(--sub); margin: 3px 0 2px; }
.rc-actions { display: flex; align-items: center; gap: 8px; }
.rc-today {
  border: 1px solid var(--line); background: none; color: var(--text);
  border-radius: 999px; padding: 5px 13px; font-size: 12px; font-weight: 650;
  cursor: pointer; font-family: inherit; flex-shrink: 0; white-space: nowrap;
}
.rc-today:active { background: var(--line); }
.rc-add {
  border: 1px solid var(--line); background: none; color: var(--c, var(--accent));
  border-radius: 999px; padding: 5px 13px; font-size: 12px; font-weight: 650;
  cursor: pointer; font-family: inherit; flex-shrink: 0; white-space: nowrap;
}
.rc-add:active { background: var(--line); }
.rc-form { display: grid; grid-template-columns: 1fr 70px auto; gap: 8px; margin: 10px 0 4px; }
.rc-form input {
  min-width: 0; padding: 9px 10px; border-radius: 11px; border: 1px solid var(--line);
  background: var(--bg); color: var(--text); font-size: 14px; outline: none; font-family: inherit;
}
.rc-form input:focus { border-color: var(--c, var(--accent)); }
.rc-form .rc-confirm {
  border: none; background: var(--c, var(--accent)); color: #fff; border-radius: 11px;
  padding: 9px 16px; font-size: 14px; font-weight: 650; cursor: pointer; font-family: inherit;
  white-space: nowrap;
}
.rc-list {
  max-height: 424px; /* 大约 12 行，年模式刚好不用滚 */
  overflow-y: auto; -webkit-overflow-scrolling: touch; margin-top: 4px;
  padding-right: 4px; scrollbar-width: thin; scrollbar-color: var(--line) transparent;
}
.rc-list::-webkit-scrollbar { width: 4px; }
.rc-list::-webkit-scrollbar-thumb { background: var(--line); border-radius: 2px; }
.rc-list::-webkit-scrollbar-track { background: transparent; }
.logrow {
  display: flex; align-items: center; gap: 18px;
  padding: 8px 4px; border-bottom: 1px solid var(--line); font-size: 14px;
}
.logrow .ltime {
  color: var(--sub); font-variant-numeric: tabular-nums;
  min-width: 0; flex-shrink: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.logrow .lnum { display: flex; align-items: center; gap: 6px; flex-shrink: 0; }
.logrow .lnum b { font-weight: 650; }
.ltag {
  font-style: normal; font-size: 10px; color: #f59e0b;
  border: 1px solid currentColor; border-radius: 4px; padding: 1px 4px;
}
.ltag.adjtag { color: var(--sub); }
.logempty, .logmore { text-align: center; color: var(--sub); font-size: 12px; padding: 14px 0; }
.logrow.dim { opacity: .4; }
.logrow .ltime b { font-weight: 600; color: var(--text); display: inline-block; min-width: 48px; margin-right: 10px; }
.logrow .ltime i { font-style: normal; display: inline-block; min-width: 104px; font-variant-numeric: tabular-nums; }
.logrow.cur .ltime b, .logrow.cur .ltime i { color: var(--c, var(--accent)); }

/* ---- 设置页（独立分页，不再是弹层） ---- */
.page-head h1 { font-size: 30px; }
.set-card {
  background: var(--card); border: 1px solid var(--line); border-radius: 20px;
  padding: 14px; box-shadow: var(--shadow);
}
.set-card > :last-child { margin-bottom: 0; }
.theme-modes { margin-top: 0; }
.set-sec {
  font-size: 12px; color: var(--sub); margin: 16px 0 8px;
  display: flex; align-items: center; gap: 8px;
}
.set-sec::after { content: ''; flex: 1; height: 1px; background: var(--line); }
.sync-line { display: flex; align-items: center; gap: 8px; font-size: 13px; margin-bottom: 12px; }
.set-btns { display: flex; gap: 10px; margin-bottom: 10px; }
.set-btns .btn { padding: 12px; font-size: 14px; }
.btn.ghost { background: none; border: 1px solid var(--line); color: var(--text); }
.btn.ghost:active { background: var(--line); }
.btn.danger-ghost { border-color: var(--danger); color: var(--danger); width: 100%; }
.btn.danger-ghost[hidden] { display: none; }
.set-note { font-size: 11px; color: var(--sub); line-height: 1.6; margin: 4px 0 6px; }

/* ---- 底部导航 ---- */
.tabbar {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: 0;
  width: min(480px, 100%); z-index: 9; /* 低于遮罩(10)与弹层(11)：新建习惯时会被挡住 */
  display: flex; align-items: stretch; gap: 4px;
  padding: 6px 12px calc(env(safe-area-inset-bottom) + 6px);
  background: var(--card);
  background: var(--tabbar);
  backdrop-filter: blur(20px) saturate(1.8);
  -webkit-backdrop-filter: blur(20px) saturate(1.8);
  border-top: 1px solid var(--line);
}
.tab {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  border: none; background: none; color: var(--sub); font-family: inherit;
  font-size: 11px; line-height: 1.2; padding: 7px 0 3px; border-radius: 14px; cursor: pointer;
  transition: color .15s ease;
}
.tab svg { width: 22px; height: 22px; }
.tab span { font-weight: 600; }
.tab.sel { color: var(--accent); }
.tab.sel span { font-weight: 700; }
.tab:active { background: var(--line); }
