:root {
  --bg: #fafaf8;
  --surface: #ffffff;
  --surface-2: #f6f6f2;
  --border: #e8e8e3;
  --border-strong: #d7d7d0;
  --ink: #1c1c1a;
  --muted: #71716b;
  --faint: #9a9a93;
  --accent: #3f7d5b;
  --accent-ink: #2f5f44;
  --accent-soft: #eef4f0;
  --amber: #b8821e;
  --amber-soft: #fbf3e3;
  --danger: #b3402f;
  --danger-soft: #faece9;
  --shadow: 0 1px 2px rgba(28, 28, 26, 0.04), 0 4px 16px rgba(28, 28, 26, 0.05);
  --radius: 14px;
  --radius-sm: 10px;
  --serif: 'Fraunces', ui-serif, Georgia, 'Songti SC', serif;
  --sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', 'Hiragino Sans GB', sans-serif;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: var(--sans);
  background: var(--bg);
  color: var(--ink);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

.app {
  max-width: 720px;
  margin: 0 auto;
  padding: 20px 18px 64px;
}

/* ---------- Topbar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 4px 18px;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-mark { display: grid; place-items: center; }
.brand-text h1 {
  font-family: var(--serif);
  font-size: 26px;
  font-weight: 600;
  letter-spacing: 0.5px;
  line-height: 1.1;
}
.brand-text p { font-size: 12.5px; color: var(--muted); margin-top: 2px; }
.topbar-date {
  font-family: var(--serif);
  font-size: 15px;
  color: var(--muted);
  text-align: right;
  font-variant-numeric: tabular-nums;
}
.offline-dot {
  font-size: 11px; line-height: 1; padding: 4px 9px; border-radius: 999px;
  background: var(--amber-soft); color: var(--amber-ink, #9a6b1f);
  border: 1px solid color-mix(in srgb, var(--amber, #e0a83c) 35%, var(--border));
  letter-spacing: .03em; white-space: nowrap;
}
.offline-dot[hidden] { display: none; }

.build-info {
  position: fixed; left: 10px; bottom: 8px; z-index: 6;
  font-size: 10px; letter-spacing: .03em;
  color: var(--faint); opacity: .55;
  user-select: none; pointer-events: none;
}

/* ---------- Tabs ---------- */
.tabs {
  display: flex;
  gap: 4px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 22px;
}
.tab {
  appearance: none;
  background: none;
  border: none;
  font-family: var(--sans);
  font-size: 15px;
  color: var(--muted);
  padding: 10px 14px;
  cursor: pointer;
  position: relative;
  transition: color 0.18s ease;
}
.tab:hover { color: var(--ink); }
.tab.is-active { color: var(--ink); font-weight: 600; }
.tab.is-active::after {
  content: '';
  position: absolute;
  left: 10px; right: 10px; bottom: -1px;
  height: 2px;
  background: var(--accent);
  border-radius: 2px;
}

/* ---------- Views ---------- */
.view { display: none; animation: fade 0.22s ease; }
.view.is-active { display: block; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

/* ---------- Today head ---------- */
.today-head {
  display: flex;
  align-items: center;
  gap: 22px;
  margin-bottom: 22px;
}
.ring-wrap { position: relative; width: 120px; height: 120px; flex: none; }
.ring { transform: rotate(-90deg); }
.ring-bg { fill: none; stroke: var(--surface-2); stroke-width: 10; }
.ring-fg {
  fill: none;
  stroke: var(--accent);
  stroke-width: 10;
  stroke-linecap: round;
  stroke-dasharray: 326.7;
  stroke-dashoffset: 326.7;
  transition: stroke-dashoffset 0.6s cubic-bezier(0.22, 1, 0.36, 1);
}
.ring-label {
  position: absolute; inset: 0;
  display: grid; place-content: center; text-align: center;
}
.ring-num { font-family: var(--serif); font-size: 24px; font-weight: 600; line-height: 1; font-variant-numeric: tabular-nums; }
.ring-cap { font-size: 11.5px; color: var(--muted); margin-top: 4px; }
.today-greeting { font-family: var(--serif); font-size: 19px; font-weight: 500; }
.today-sub { color: var(--muted); font-size: 14px; margin-top: 6px; }

/* ---------- Cards ---------- */
.card-list { display: flex; flex-direction: column; gap: 12px; }
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
  display: flex;
  align-items: center;
  gap: 14px;
  box-shadow: var(--shadow);
  transition: border-color 0.18s ease, transform 0.18s ease;
}
.card.done { border-color: color-mix(in srgb, var(--accent) 40%, var(--border)); }
.card .ico {
  width: 44px; height: 44px; flex: none;
  display: grid; place-items: center;
  border-radius: 12px;
  font-size: 22px;
  background: var(--surface-2);
}
.card .body { flex: 1; min-width: 0; }
.card .name { font-size: 16px; font-weight: 600; }
.card .meta { font-size: 13px; color: var(--muted); margin-top: 3px; display: flex; flex-wrap: wrap; gap: 6px 10px; }
.card .meta .pill {
  display: inline-flex; align-items: center; gap: 4px;
}
.tag {
  font-size: 12px; padding: 2px 8px; border-radius: 999px;
  border: 1px solid var(--border-strong);
  color: var(--muted);
  white-space: nowrap;
}
.tag.warn { background: var(--amber-soft); border-color: color-mix(in srgb, var(--amber) 35%, var(--border)); color: var(--amber-ink, var(--amber)); }
.tag.danger { background: var(--danger-soft); border-color: color-mix(in srgb, var(--danger) 35%, var(--border)); color: var(--danger); }
.tag.ok { background: var(--accent-soft); border-color: color-mix(in srgb, var(--accent) 30%, var(--border)); color: var(--accent-ink); }

/* check toggle (dosage == 1) */
.check {
  width: 34px; height: 34px; flex: none;
  border-radius: 50%;
  border: 2px solid var(--border-strong);
  background: var(--surface);
  cursor: pointer;
  display: grid; place-items: center;
  transition: all 0.2s cubic-bezier(0.22, 1, 0.36, 1);
}
.check svg { width: 18px; height: 18px; stroke: #fff; stroke-width: 3; fill: none; opacity: 0; transform: scale(0.5); transition: all 0.2s ease; }
.check.on { background: var(--accent); border-color: var(--accent); }
.check.on svg { opacity: 1; transform: scale(1); }

/* stepper (dosage > 1) */
.stepper { display: flex; align-items: center; gap: 10px; flex: none; }
.stepper button {
  width: 32px; height: 32px; border-radius: 50%;
  border: 1px solid var(--border-strong); background: var(--surface);
  font-size: 18px; line-height: 1; cursor: pointer; color: var(--ink);
  transition: background 0.15s ease, border-color 0.15s ease;
}
.stepper button:hover { background: var(--surface-2); }
.stepper .count { font-family: var(--serif); font-size: 17px; min-width: 34px; text-align: center; font-variant-numeric: tabular-nums; }
.stepper.full .count { color: var(--accent-ink); }

/* dose chips (multi-dose schedule) */
.doses { display: flex; align-items: center; gap: 7px; flex: none; }
.dose {
  width: 30px; height: 30px; border-radius: 50%;
  border: 1px solid var(--border-strong); background: var(--surface);
  font-size: 13px; font-weight: 600; line-height: 1; cursor: pointer; color: var(--muted);
  display: grid; place-items: center;
  transition: background 0.15s ease, border-color 0.15s ease, color 0.15s ease;
}
.dose:hover { background: var(--surface-2); }
.dose.on { background: var(--accent); border-color: var(--accent); color: #fff; }
.doses.full .dose.on { box-shadow: 0 0 0 2px var(--accent-soft); }

/* card actions (library) */
.actions { display: flex; gap: 6px; flex: none; }
.icon-btn {
  border: 1px solid var(--border); background: var(--surface);
  width: 32px; height: 32px; border-radius: 9px; cursor: pointer;
  font-size: 16px; color: var(--muted); line-height: 1;
  transition: background 0.15s ease, color 0.15s ease;
}
.icon-btn:hover { background: var(--surface-2); color: var(--ink); }

/* empty state */
.empty {
  text-align: center; padding: 48px 20px; color: var(--muted);
  border: 1px dashed var(--border-strong); border-radius: var(--radius);
  background: var(--surface);
}
.empty .big { font-size: 34px; margin-bottom: 10px; }
.empty p { font-size: 14px; }

/* ---------- Buttons ---------- */
.btn {
  appearance: none; border: none; cursor: pointer;
  font-family: var(--sans); font-size: 14.5px; font-weight: 600;
  padding: 9px 16px; border-radius: 10px;
  transition: filter 0.15s ease, background 0.15s ease, transform 0.1s ease;
}
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { filter: brightness(1.06); }
.btn-ghost { background: var(--surface-2); color: var(--ink); }
.btn-ghost:hover { background: var(--border); }

.section-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.section-head h2 { font-family: var(--serif); font-size: 20px; font-weight: 600; }
.muted { color: var(--muted); font-size: 13px; }

/* ---------- Stats (history) ---------- */
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; margin-bottom: 26px; }
.stat {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow);
  text-align: center;
}
.stat .v { font-family: var(--serif); font-size: 28px; font-weight: 600; line-height: 1; font-variant-numeric: tabular-nums; }
.stat .k { font-size: 12.5px; color: var(--muted); margin-top: 8px; }

/* ---------- Heat grid ---------- */
.heat {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 7px;
}
.heat .cell {
  aspect-ratio: 1 / 1;
  border-radius: 8px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  display: grid; place-items: center;
  font-size: 11px; color: var(--faint);
  position: relative;
  cursor: default;
}
.heat .cell.lvl1 { background: color-mix(in srgb, var(--accent) 22%, var(--surface)); border-color: transparent; }
.heat .cell.lvl2 { background: color-mix(in srgb, var(--accent) 48%, var(--surface)); border-color: transparent; }
.heat .cell.lvl3 { background: color-mix(in srgb, var(--accent) 78%, var(--surface)); border-color: transparent; color: #fff; }
.heat .cell.full { background: var(--accent); border-color: transparent; color: #fff; }
.heat .cell .d { font-variant-numeric: tabular-nums; }
.heat .cell.today { outline: 2px solid var(--accent); outline-offset: 1px; }

/* ---------- Modal ---------- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(28, 28, 26, 0.32);
  display: grid; place-items: center; padding: 18px; z-index: 50;
  animation: fade 0.18s ease;
}
.modal-backdrop[hidden] { display: none; }
.modal {
  background: var(--surface); width: 100%; max-width: 440px;
  border-radius: 18px; border: 1px solid var(--border);
  box-shadow: 0 12px 40px rgba(28, 28, 26, 0.18);
  overflow: hidden;
}
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 18px 20px; border-bottom: 1px solid var(--border); }
.modal-head h3 { font-family: var(--serif); font-size: 19px; font-weight: 600; }
.form { padding: 18px 20px 20px; display: flex; flex-direction: column; gap: 14px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field > span { font-size: 13px; color: var(--muted); font-weight: 500; }
.field input, .field textarea {
  font-family: var(--sans); font-size: 15px; color: var(--ink);
  border: 1px solid var(--border-strong); border-radius: 10px;
  padding: 10px 12px; background: var(--surface); width: 100%;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.field input:focus, .field textarea:focus {
  outline: none; border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.field textarea { resize: vertical; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.field-block { display: flex; flex-direction: column; gap: 8px; }
.field-label { font-size: 13px; color: var(--muted); font-weight: 500; }
.field-hint { font-size: 12.5px; color: var(--muted); margin: 2px 0 0; }
.field-hint b { color: var(--accent-ink); font-family: var(--serif); }
.form-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 4px; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: 28px; transform: translateX(-50%);
  background: var(--ink); color: #fff; padding: 11px 18px; border-radius: 10px;
  font-size: 14px; z-index: 60; box-shadow: var(--shadow);
  animation: toastIn 0.25s ease;
}
@keyframes toastIn { from { opacity: 0; transform: translate(-50%, 8px); } to { opacity: 1; transform: translate(-50%, 0); } }

/* ---------- Responsive ---------- */
/* ---------- Lock (single-user privacy) ---------- */
.topbar-right { display: flex; align-items: center; gap: 10px; }
.lock {
  position: fixed; inset: 0; background: rgba(28, 28, 26, 0.96);
  display: grid; place-items: center; z-index: 80; padding: 18px;
  animation: fade 0.18s ease;
}
.lock[hidden] { display: none; }
.lock-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 18px; padding: 28px 24px; width: 100%; max-width: 340px;
  text-align: center; box-shadow: 0 12px 40px rgba(28, 28, 26, 0.25);
}
.lock-mark { font-size: 34px; }
.lock-card h3 { font-family: var(--serif); font-size: 20px; font-weight: 600; margin: 10px 0 4px; }
.lock-sub { color: var(--muted); font-size: 13px; margin-bottom: 4px; }
.lock-hint { color: var(--faint); font-size: 12px; margin: 0 0 16px; line-height: 1.5; }
.lock-card input {
  width: 100%; font-size: 16px; padding: 11px 12px;
  border: 1px solid var(--border-strong); border-radius: 10px;
  text-align: center; margin-bottom: 12px; font-family: var(--sans);
}
.lock-card input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.lock-card .btn { width: 100%; }
.lock-err { color: var(--danger); font-size: 13px; margin-top: 10px; min-height: 18px; }

/* ---------- Daily completion chart (history) ---------- */
.chart-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 16px 14px 12px; box-shadow: var(--shadow);
}
.chart-wrap { position: relative; width: 100%; }
#chart { width: 100%; height: auto; display: block; overflow: visible; }
.chart-tip {
  position: absolute; z-index: 5; min-width: 184px; max-width: 244px;
  background: var(--surface); border: 1px solid var(--border-strong);
  border-radius: 12px; box-shadow: var(--shadow); padding: 10px 12px;
  pointer-events: auto; transform: translateX(-50%);
}
.chart-tip .tip-date { font-family: var(--serif); font-size: 14px; font-weight: 600; }
.chart-tip .tip-rate { font-family: var(--serif); font-size: 23px; font-weight: 600; color: var(--accent-ink); line-height: 1.1; margin-top: 1px; }
.chart-tip .tip-rows { margin-top: 7px; display: flex; flex-direction: column; gap: 3px; }
.chart-tip .tip-row {
  display: flex; align-items: center; gap: 7px; font-size: 12.5px;
  color: var(--muted); cursor: pointer; padding: 2px 0; border-radius: 6px;
}
.chart-tip .tip-row:hover { color: var(--ink); background: var(--surface-2); }
.chart-tip .tip-row .mk { width: 14px; text-align: center; font-size: 13px; }
.chart-tip .tip-row.on .mk { color: var(--accent); }
.chart-tip .tip-row .nm { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chart-tip .tip-row .ct { font-variant-numeric: tabular-nums; }
.chart-legend { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.legend-chip {
  display: inline-flex; align-items: center; gap: 6px; font-size: 12.5px;
  padding: 3px 10px; border-radius: 999px; border: 1px solid var(--border-strong);
  background: var(--surface); color: var(--muted); cursor: pointer; user-select: none;
  transition: color 0.14s ease, border-color 0.14s ease, opacity 0.14s ease;
}
.legend-chip .sw { width: 10px; height: 10px; border-radius: 3px; flex: none; }
.legend-chip .sw.ref {
  background: var(--border-strong);
  background-image: repeating-linear-gradient(45deg, transparent, transparent 2px, #fff 2px, #fff 4px);
}
.legend-chip.on { color: var(--ink); }
.legend-chip.off { opacity: 0.5; text-decoration: line-through; }

/* ---------- Icon picker (add/edit modal) ---------- */
.icon-picker {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 6px;
}
.icon-opt {
  appearance: none;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  border-radius: 10px;
  font-size: 20px; line-height: 1;
  padding: 7px 0; cursor: pointer;
  display: grid; place-items: center;
  transition: border-color 0.14s ease, background 0.14s ease, transform 0.1s ease;
}
.icon-opt:hover { background: var(--surface-2); }
.icon-opt.sel {
  border-color: var(--accent);
  background: var(--accent-soft);
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--accent) 25%, transparent);
}
.tag.price { border-color: color-mix(in srgb, var(--accent) 25%, var(--border)); color: var(--accent-ink); }
.tag.price .per { color: var(--muted); font-weight: 500; }

@media (max-width: 540px) {
  .today-head { flex-direction: column; align-items: flex-start; gap: 16px; }
  .stats { grid-template-columns: 1fr; }
  .brand-text h1 { font-size: 23px; }
}
