:root {
  --bg: #f7f8fa;
  --surface: #ffffff;
  --surface-2: #eef0f4;
  --text: #11151c;
  --text-dim: #5b6573;
  --border: #e2e6ec;
  --accent: #2f6df6;
  --accent-2: #6f4ff2;
  --shadow: 0 1px 2px rgba(17, 21, 28, 0.04), 0 10px 28px rgba(17, 21, 28, 0.07);
  --shadow-lift: 0 2px 6px rgba(17, 21, 28, 0.06), 0 18px 40px rgba(17, 21, 28, 0.12);
  --ease: cubic-bezier(0.22, 1, 0.36, 1);

  --cat-family: #e0408a;
  --cat-health: #1ca672;
  --cat-work: #6b5bd6;
  --cat-money: #c98a00;
  --cat-home: #d2603a;
  --cat-personal: #3a7bd5;
  --cat-recycling: #2faf4f;
  --cat-holiday: #d6453f;

  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b0f17;
    --surface: #141a25;
    --surface-2: #1c2533;
    --text: #eef2f8;
    --text-dim: #94a0b2;
    --border: #232d3d;
    --accent: #5b8cff;
    --accent-2: #9a7bff;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 12px 30px rgba(0, 0, 0, 0.45);
    --shadow-lift: 0 2px 8px rgba(0, 0, 0, 0.4), 0 20px 44px rgba(0, 0, 0, 0.55);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", system-ui, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

/* ---------- Top bar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  padding: calc(var(--safe-top) + 14px) 18px 12px;
  background: color-mix(in srgb, var(--bg) 86%, transparent);
  backdrop-filter: saturate(160%) blur(14px);
  -webkit-backdrop-filter: saturate(160%) blur(14px);
  border-bottom: 1px solid var(--border);
}
.topbar-titles h1 {
  margin: 0;
  font-size: 1.6rem;
  font-weight: 800;
  letter-spacing: -0.02em;
}
.topbar-titles p {
  margin: 2px 0 0;
  color: var(--text-dim);
  font-size: 0.82rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.seg {
  display: flex;
  gap: 4px;
  margin-top: 12px;
  padding: 4px;
  background: var(--surface-2);
  border-radius: 12px;
}
.seg-btn {
  flex: 1;
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--text-dim);
  font: inherit;
  font-weight: 700;
  font-size: 0.95rem;
  padding: 8px 10px;
  border-radius: 9px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.seg-btn.is-active {
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}

/* ---------- Views ---------- */
main { padding: 14px 16px calc(var(--safe-bottom) + 40px); max-width: 720px; margin: 0 auto; }
.view { display: none; }
.view.is-active { display: block; }

/* ---------- Agenda ---------- */
.day-group { margin-bottom: 22px; }
.day-head {
  display: flex;
  align-items: baseline;
  gap: 8px;
  margin: 0 4px 8px;
  position: sticky;
  top: calc(var(--safe-top) + 118px);
  z-index: 5;
}
.day-head .dow { font-weight: 800; font-size: 1rem; }
.day-head .date { color: var(--text-dim); font-size: 0.9rem; font-weight: 600; }
.day-head.is-today .dow { color: var(--accent); }

.event {
  display: flex;
  gap: 12px;
  align-items: stretch;
  width: 100%;
  text-align: left;
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 14px;
  padding: 12px 14px;
  margin-bottom: 8px;
  cursor: pointer;
  box-shadow: var(--shadow);
  transition: transform 0.08s ease;
}
.event:active { transform: scale(0.985); }
.event .bar { width: 4px; border-radius: 4px; background: var(--text-dim); flex: 0 0 auto; }
.event .body { min-width: 0; flex: 1; }
.event .title { font-weight: 700; font-size: 1.02rem; line-height: 1.25; }
.event .time { color: var(--text-dim); font-size: 0.88rem; margin-top: 2px; }
.event .loc { color: var(--text-dim); font-size: 0.82rem; margin-top: 2px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.empty { text-align: center; color: var(--text-dim); padding: 60px 20px; }
.empty .big { font-size: 2.2rem; margin-bottom: 8px; }

/* ---------- Month ---------- */
.month-head { display: flex; align-items: center; justify-content: space-between; margin: 4px 2px 12px; }
.month-head h2 { margin: 0; font-size: 1.15rem; font-weight: 800; }
.nav-btn {
  appearance: none; border: 1px solid var(--border); background: var(--surface);
  color: var(--text); width: 38px; height: 38px; border-radius: 10px;
  font-size: 1.3rem; line-height: 1; cursor: pointer;
}
.weekdays { display: grid; grid-template-columns: repeat(7, 1fr); margin-bottom: 6px; }
.weekdays span { text-align: center; font-size: 0.72rem; font-weight: 700; color: var(--text-dim); text-transform: uppercase; letter-spacing: 0.04em; }
.month-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; }
.cell {
  aspect-ratio: 1 / 1;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 10px;
  padding: 6px 0 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  cursor: pointer;
  position: relative;
}
.cell.is-muted { opacity: 0.4; }
.cell.is-today { border-color: var(--accent); }
.cell.is-selected { background: var(--surface-2); }
.cell .num { font-size: 0.85rem; font-weight: 700; }
.cell.is-today .num {
  background: var(--accent); color: #fff; border-radius: 50%;
  width: 24px; height: 24px; display: grid; place-items: center;
}
.dots { display: flex; gap: 3px; flex-wrap: wrap; justify-content: center; padding: 0 4px; }
.dot { width: 6px; height: 6px; border-radius: 50%; background: var(--text-dim); }

.month-day-list { margin-top: 18px; }
.month-day-list h3 { margin: 0 4px 8px; font-size: 0.95rem; color: var(--text-dim); }

/* ---------- Category colors ---------- */
.cat-family { color: var(--cat-family); } .bar.cat-family, .dot.cat-family { background: var(--cat-family); }
.cat-health { color: var(--cat-health); } .bar.cat-health, .dot.cat-health { background: var(--cat-health); }
.cat-work   { color: var(--cat-work);   } .bar.cat-work,   .dot.cat-work   { background: var(--cat-work); }
.cat-money  { color: var(--cat-money);  } .bar.cat-money,  .dot.cat-money  { background: var(--cat-money); }
.cat-home   { color: var(--cat-home);   } .bar.cat-home,   .dot.cat-home   { background: var(--cat-home); }
.cat-personal { color: var(--cat-personal); } .bar.cat-personal, .dot.cat-personal { background: var(--cat-personal); }
.cat-recycling { color: var(--cat-recycling); } .bar.cat-recycling, .dot.cat-recycling { background: var(--cat-recycling); }
.cat-holiday { color: var(--cat-holiday); } .bar.cat-holiday, .dot.cat-holiday { background: var(--cat-holiday); }

.chip {
  display: inline-block; font-size: 0.72rem; font-weight: 800; text-transform: uppercase;
  letter-spacing: 0.06em; padding: 4px 10px; border-radius: 999px;
  background: var(--surface-2);
}

/* ---------- Page nav ---------- */
.pagenav { display: flex; gap: 6px; margin-top: 12px; }
.pagelink {
  flex: 1; text-align: center; text-decoration: none;
  font-weight: 700; font-size: 0.95rem; color: var(--text-dim);
  padding: 9px 10px; border-radius: 11px; background: var(--surface-2);
  transition: color 0.15s, background 0.15s, transform 0.08s;
}
.pagelink:active { transform: scale(0.97); }
.pagelink.is-active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

/* completion progress bar pinned to the bottom edge of the sticky header */
.topbar-progress {
  position: absolute; left: 0; right: 0; bottom: -1px; height: 3px;
  background: var(--surface-2);
}
.topbar-progress-fill {
  display: block; height: 100%; width: 0%;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  border-radius: 0 3px 3px 0;
  transition: width 0.5s var(--ease);
}

/* ---------- To-Do ---------- */
.add-row { display: flex; gap: 10px; margin: 6px 0 20px; }
.add-input {
  flex: 1; appearance: none; font: inherit; font-size: 1rem;
  padding: 14px 16px; border-radius: 14px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  box-shadow: var(--shadow);
  transition: border-color 0.15s, box-shadow 0.15s;
}
.add-input::placeholder { color: var(--text-dim); }
.add-input:focus {
  outline: none; border-color: var(--accent);
  box-shadow: var(--shadow), 0 0 0 4px color-mix(in srgb, var(--accent) 18%, transparent);
}
.add-btn {
  appearance: none; border: 0; border-radius: 14px; font: inherit; font-weight: 800;
  padding: 0 22px; color: #fff; cursor: pointer;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  box-shadow: var(--shadow);
  transition: transform 0.08s, opacity 0.15s, filter 0.15s;
}
.add-btn:active { transform: scale(0.95); }
.add-btn:disabled { opacity: 0.45; cursor: default; filter: saturate(0.6); }

.task-list { display: flex; flex-direction: column; gap: 10px; }
.task {
  display: flex; align-items: center; gap: 13px;
  border: 1px solid var(--border); background: var(--surface);
  border-radius: 16px; padding: 14px 15px; box-shadow: var(--shadow);
  max-height: 200px; overflow: hidden;
  transition: transform 0.12s var(--ease), box-shadow 0.18s,
              max-height 0.3s var(--ease), opacity 0.28s var(--ease),
              padding 0.3s var(--ease), margin 0.3s var(--ease);
}
.task:active { transform: scale(0.99); }
.task.pop-in { animation: popIn 0.4s var(--ease); }
.task.removing {
  max-height: 0; opacity: 0; padding-top: 0; padding-bottom: 0;
  margin-bottom: -10px; transform: translateX(36px);
  box-shadow: none; pointer-events: none;
}

.task-title {
  flex: 1; min-width: 0; font-weight: 600; font-size: 1.05rem; line-height: 1.3;
  position: relative; transition: color 0.3s;
}
/* animated strike-through that sweeps across on completion */
.task-title::after {
  content: ""; position: absolute; left: 0; top: 52%;
  height: 2px; width: 0; background: currentColor; border-radius: 2px;
  transition: width 0.38s var(--ease);
}
.task.is-completing .task-title::after { width: 100%; }
.task.done .task-title { color: var(--text-dim); }
.task.done .task-title::after { width: 100%; opacity: 0.7; transition: none; }

.check {
  flex: 0 0 auto; width: 30px; height: 30px; border-radius: 50%;
  border: 2px solid var(--text-dim); background: transparent; color: #fff;
  font-size: 1rem; line-height: 1; cursor: pointer; display: grid; place-items: center;
  transition: background 0.2s var(--ease), border-color 0.2s var(--ease), transform 0.1s;
}
.check:active { transform: scale(0.88); }
.check.is-done {
  background: var(--cat-health); border-color: var(--cat-health);
  animation: checkPop 0.42s var(--ease);
}
.task-del {
  flex: 0 0 auto; appearance: none; border: 0; background: transparent;
  color: var(--text-dim); font-size: 1rem; cursor: pointer; padding: 6px;
  border-radius: 8px; transition: color 0.15s, background 0.15s;
}
.task-del:active { color: var(--cat-home); background: var(--surface-2); }
.task-tag { flex: 0 0 auto; }

.done-wrap { margin-top: 26px; }
.done-head { display: flex; justify-content: space-between; align-items: center; margin: 0 6px 10px; }
.link-btn {
  appearance: none; border: 0; background: transparent; font: inherit; font-weight: 700;
  font-size: 0.85rem; color: var(--text-dim); cursor: pointer; padding: 6px 4px;
  border-radius: 8px; transition: color 0.15s;
}
.link-btn.danger { color: var(--cat-home); }
.is-done-list .task { opacity: 0.62; box-shadow: none; }

@keyframes popIn {
  0% { opacity: 0; transform: translateY(-8px) scale(0.96); }
  100% { opacity: 1; transform: translateY(0) scale(1); }
}
@keyframes checkPop {
  0% { transform: scale(0.7); }
  45% { transform: scale(1.22); }
  100% { transform: scale(1); }
}

/* ---------- Sheet ---------- */
.sheet-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,0.45); z-index: 40; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 50;
  background: var(--surface);
  border-radius: 22px 22px 0 0;
  box-shadow: 0 -10px 40px rgba(0,0,0,0.3);
  padding-bottom: var(--safe-bottom);
  animation: slideUp 0.22s ease;
}
@keyframes slideUp { from { transform: translateY(100%); } to { transform: translateY(0); } }
.sheet-grip { width: 40px; height: 5px; border-radius: 3px; background: var(--border); margin: 10px auto 4px; }
.sheet-body { padding: 8px 22px 22px; max-width: 720px; margin: 0 auto; }
.sheet-body h2 { margin: 10px 0 6px; font-size: 1.4rem; font-weight: 800; }
.sheet-when { margin: 0 0 4px; font-weight: 600; }
.sheet-meta { margin: 2px 0; color: var(--text-dim); }
.sheet-notes { margin: 12px 0 0; color: var(--text); background: var(--surface-2); padding: 12px 14px; border-radius: 12px; white-space: pre-wrap; }
.sheet-actions { display: flex; gap: 10px; margin-top: 20px; }
.btn {
  appearance: none; border: 0; border-radius: 13px; font: inherit; font-weight: 700;
  padding: 14px 16px; text-align: center; cursor: pointer; text-decoration: none; flex: 1;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-ghost { background: var(--surface-2); color: var(--text); }
.btn-close { width: 100%; margin-top: 10px; background: transparent; color: var(--text-dim); }
