:root {
  --bg: #F1EFE8;
  --surface: #FFFFFF;
  --surface-muted: #F7F5F0;
  --border: #E2E0D6;
  --text-primary: #2C2C2A;
  --text-secondary: #5F5E5A;
  --text-muted: #888780;

  --teal: #1D9E75;
  --teal-bg: #E1F5EE;
  --teal-text: #085041;
  --coral: #D85A30;
  --coral-bg: #FAECE7;
  --coral-text: #712B13;
  --green: #639922;
  --green-bg: #EAF3DE;
  --green-text: #27500A;
  --red: #E24B4A;
  --red-bg: #FCEBEB;
  --red-text: #791F1F;
  --blue-bg: #E6F1FB;
  --blue-text: #0C447C;
  --blue: #378ADD;
  --pink-bg: #FBEAF0;
  --pink-text: #72243E;
  --pink: #D4537E;
  --purple-bg: #EEEDFE;
  --purple-text: #3C3489;
  --purple: #7F77DD;
  --amber-bg: #FAEEDA;
  --amber-text: #633806;
  --amber: #EF9F27;

  --radius: 8px;
  --radius-lg: 12px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1C1C1A;
    --surface: #262624;
    --surface-muted: #2F2F2C;
    --border: #3A3A37;
    --text-primary: #F1EFE8;
    --text-secondary: #B4B2A9;
    --text-muted: #888780;

    --teal-bg: #085041;
    --teal-text: #5DCAA5;
    --coral-bg: #712B13;
    --coral-text: #F0997B;
    --green-bg: #27500A;
    --green-text: #97C459;
    --red-bg: #791F1F;
    --red-text: #F09595;
    --blue-bg: #0C447C;
    --blue-text: #85B7EB;
    --pink-bg: #72243E;
    --pink-text: #ED93B1;
    --purple-bg: #3C3489;
    --purple-text: #AFA9EC;
    --amber-bg: #633806;
    --amber-text: #EF9F27;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text-primary);
}

#app {
  max-width: 640px;
  margin: 0 auto;
  padding: 0 16px 96px;
}

@media (min-width: 720px) {
  #app { max-width: 900px; }
}
@media (min-width: 1100px) {
  #app { max-width: 1200px; }
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 0 16px;
  gap: 12px;
}

.brand { font-size: 18px; font-weight: 600; margin: 0; }

.topbar-right { display: flex; align-items: center; gap: 8px; }

.icon-btn {
  border: 0.5px solid var(--border);
  background: var(--surface);
  color: var(--text-secondary);
  width: 32px; height: 32px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 14px;
  display: flex; align-items: center; justify-content: center;
}
.icon-btn.on { color: var(--teal); border-color: var(--teal); }

.tabs { display: none; gap: 4px; }

.tab {
  border: none;
  background: transparent;
  color: var(--text-secondary);
  font-size: 14px;
  padding: 8px 14px;
  border-radius: var(--radius);
  cursor: pointer;
}
.tab.active { background: var(--surface); color: var(--text-primary); font-weight: 500; }

@media (min-width: 720px) {
  .tabs { display: flex; }
  #app { padding-bottom: 40px; }
}

.bottom-nav {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  display: flex;
  background: var(--surface);
  border-top: 0.5px solid var(--border);
  padding: 8px 0 max(8px, env(safe-area-inset-bottom));
}
.bottom-tab {
  flex: 1;
  border: none;
  background: none;
  font-size: 12px;
  color: var(--text-muted);
  padding: 6px 0;
}
.bottom-tab.active { color: var(--teal); font-weight: 500; }

@media (min-width: 720px) {
  .bottom-nav { display: none; }
}

/* Banners (quest / streak-at-risk) */
.banner {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  border-radius: var(--radius-lg);
  padding: 10px 14px;
  margin-bottom: 12px;
  font-size: 13px;
}
.banner.quest { background: var(--purple-bg); color: var(--purple-text); }
.banner.risk { background: var(--amber-bg); color: var(--amber-text); }
.banner strong { font-weight: 500; }

/* Quick add */
.quick-add {
  display: flex;
  gap: 8px;
  align-items: center;
  background: var(--surface);
  border: 0.5px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 10px 12px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}
.quick-add input[type="text"], .quick-add textarea {
  flex: 1;
  min-width: 140px;
  border: none;
  outline: none;
  font-size: 15px;
  background: transparent;
  font-family: inherit;
  color: var(--text-primary);
}
.quick-add textarea { width: 100%; resize: vertical; min-height: 40px; }

.diff-toggle { display: flex; gap: 4px; }
.diff-btn {
  border: 0.5px solid var(--border);
  background: var(--surface-muted);
  color: var(--text-secondary);
  font-size: 12px;
  font-weight: 500;
  padding: 6px 10px;
  border-radius: var(--radius);
  cursor: pointer;
}
.diff-btn.selected.S { background: var(--green-bg); color: var(--green-text); border-color: var(--green); }
.diff-btn.selected.M { background: var(--teal-bg); color: var(--teal-text); border-color: var(--teal); }
.diff-btn.selected.L { background: var(--coral-bg); color: var(--coral-text); border-color: var(--coral); }
.diff-btn.selected.XL { background: var(--red-bg); color: var(--red-text); border-color: var(--red); }

.quick-add select, .quick-add input[type="date"] {
  border: 0.5px solid var(--border);
  border-radius: var(--radius);
  font-size: 12px;
  padding: 6px 8px;
  background: var(--surface-muted);
  color: var(--text-secondary);
}

.add-btn {
  border: none;
  background: var(--teal);
  color: white;
  font-weight: 500;
  font-size: 13px;
  padding: 8px 14px;
  border-radius: var(--radius);
  cursor: pointer;
}
.cancel-btn {
  border: 0.5px solid var(--border);
  background: none;
  color: var(--text-secondary);
  font-size: 13px;
  padding: 8px 14px;
  border-radius: var(--radius);
  cursor: pointer;
}

/* Filter bar */
.filter-bar {
  display: flex; gap: 8px; flex-wrap: wrap;
  margin-bottom: 12px;
}
.filter-bar input[type="search"] {
  flex: 1; min-width: 120px;
  border: 0.5px solid var(--border);
  border-radius: var(--radius);
  padding: 7px 10px;
  font-size: 13px;
  background: var(--surface);
  color: var(--text-primary);
}
.filter-bar select {
  border: 0.5px solid var(--border);
  border-radius: var(--radius);
  padding: 7px 10px;
  font-size: 13px;
  background: var(--surface);
  color: var(--text-secondary);
}

/* Task cards */
.task-list { display: flex; flex-direction: column; gap: 20px; }

.task-grid { display: flex; flex-direction: column; gap: 8px; }
@media (min-width: 640px) {
  .task-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
    gap: 10px;
  }
}

.task-card {
  background: var(--surface);
  border: 0.5px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 12px 14px;
}
.task-card.done { opacity: 0.5; }
.task-card.quest-task { border-color: var(--purple-text); border-width: 1.5px; }
.task-card.cat-work { border-color: var(--blue); border-width: 1.5px; }
.task-card.cat-life { border-color: var(--pink); border-width: 1.5px; }
.task-card.cat-home { border-color: var(--green); border-width: 1.5px; }
.task-card.cat-reminder { border-color: var(--amber); border-width: 1.5px; }
.task-card.cat-event { border-color: var(--purple); border-width: 1.5px; }

.task-row { display: flex; align-items: flex-start; gap: 10px; }
.task-card input[type="checkbox"] { width: 18px; height: 18px; flex-shrink: 0; margin-top: 2px; }
.task-body { flex: 1; min-width: 0; }
.task-title { font-size: 14px; margin: 0; }
.task-card.done .task-title { text-decoration: line-through; }
.task-notes { font-size: 12px; color: var(--text-secondary); margin: 4px 0 0; }

.task-pills { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.pill {
  font-size: 11px; font-weight: 500;
  padding: 3px 8px; border-radius: 20px;
  white-space: nowrap;
}
.pill.S { background: var(--green-bg); color: var(--green-text); }
.pill.M { background: var(--teal-bg); color: var(--teal-text); }
.pill.L { background: var(--coral-bg); color: var(--coral-text); }
.pill.XL { background: var(--red-bg); color: var(--red-text); }
.pill.cat-work { background: var(--blue-bg); color: var(--blue-text); }
.pill.cat-life { background: var(--pink-bg); color: var(--pink-text); }
.pill.cat-home { background: var(--green-bg); color: var(--green-text); }
.pill.cat-reminder { background: var(--amber-bg); color: var(--amber-text); }
.pill.cat-event { background: var(--purple-bg); color: var(--purple-text); }
.pill.due { background: var(--surface-muted); color: var(--text-secondary); }
.pill.due.overdue { background: var(--red-bg); color: var(--red-text); }
.pill.due.today { background: var(--amber-bg); color: var(--amber-text); }
.pill.recurring { background: var(--surface-muted); color: var(--text-secondary); }

.task-actions { display: flex; gap: 4px; flex-shrink: 0; }
.icon-action {
  border: none; background: none; color: var(--text-muted);
  font-size: 14px; cursor: pointer; padding: 4px 6px;
}
.icon-action:hover { color: var(--text-primary); }

.snooze-row { display: flex; gap: 6px; margin-top: 8px; }
.snooze-btn {
  border: 0.5px solid var(--border); background: var(--surface-muted);
  color: var(--text-secondary); font-size: 11px; padding: 4px 8px;
  border-radius: var(--radius); cursor: pointer;
}

.empty-state { text-align: center; color: var(--text-muted); padding: 48px 16px; font-size: 14px; }

/* Stats */
.metric-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  margin-bottom: 20px;
}
@media (min-width: 480px) { .metric-grid { grid-template-columns: repeat(4, 1fr); } }
.metric-card { background: var(--surface); border-radius: var(--radius); padding: 14px; }
.metric-label { font-size: 12px; color: var(--text-muted); margin: 0 0 4px; }
.metric-value { font-size: 22px; font-weight: 600; margin: 0; }

.section-title { font-size: 14px; font-weight: 500; color: var(--text-secondary); margin: 0 0 8px; }

.xp-bar-track { height: 6px; background: var(--surface-muted); border-radius: 3px; overflow: hidden; }
.xp-bar-fill { height: 100%; background: var(--teal); transition: width 0.4s; }

.week-bars { display: flex; align-items: flex-end; gap: 8px; height: 120px; margin-bottom: 24px; }
.week-bar-col { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 6px; height: 100%; justify-content: flex-end; }
.week-bar { width: 100%; background: #2a78d6; border-radius: 4px 4px 0 0; min-height: 3px; }
.week-bar-label { font-size: 11px; color: var(--text-muted); }

.diff-breakdown { display: flex; flex-direction: column; gap: 8px; margin-bottom: 24px; }
.diff-row { display: flex; align-items: center; gap: 10px; }
.diff-row-label { width: 28px; font-size: 12px; color: var(--text-secondary); }
.diff-row-track { flex: 1; height: 10px; background: var(--surface-muted); border-radius: 5px; overflow: hidden; }
.diff-row-fill { height: 100%; }
.diff-row-count { width: 20px; text-align: right; font-size: 12px; color: var(--text-muted); }

.recent-achievements { display: flex; gap: 10px; flex-wrap: wrap; }
.recent-badge {
  display: flex; align-items: center; gap: 8px;
  background: var(--surface); border-radius: var(--radius-lg); padding: 8px 12px;
  font-size: 13px;
}
.badge-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--teal); }

/* Achievements grid */
.achv-progress { display: flex; align-items: center; gap: 12px; margin-bottom: 16px; }
.achv-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 10px;
}
.achv-card { background: var(--surface); border-radius: var(--radius-lg); padding: 14px; }
.achv-card.locked { opacity: 0.5; }
.achv-icon {
  width: 36px; height: 36px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  margin-bottom: 10px; font-size: 16px;
}
.achv-title { font-size: 14px; font-weight: 500; margin: 0 0 2px; }
.achv-desc { font-size: 12px; color: var(--text-muted); margin: 0; }

/* History */
.history-list { display: flex; flex-direction: column; gap: 6px; }
.history-row {
  display: flex; align-items: center; justify-content: space-between;
  background: var(--surface); border-radius: var(--radius); padding: 10px 12px;
  font-size: 13px;
}
.history-title { flex: 1; min-width: 0; }
.history-meta { color: var(--text-muted); font-size: 12px; margin-left: 10px; white-space: nowrap; }

/* Toast + level-up celebration */
.toast {
  position: fixed;
  bottom: 84px; left: 50%; transform: translateX(-50%) translateY(20px);
  background: #2C2C2A; color: white;
  padding: 10px 16px; border-radius: var(--radius);
  font-size: 13px; opacity: 0; pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  display: flex; align-items: center; gap: 10px;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast .undo-link { color: #9FE1CB; font-weight: 500; cursor: pointer; pointer-events: auto; }
@media (min-width: 720px) { .toast { bottom: 24px; } }

.levelup {
  position: fixed;
  top: 24px; left: 50%; transform: translateX(-50%) translateY(-20px);
  background: var(--teal); color: white;
  padding: 14px 22px; border-radius: var(--radius-lg);
  font-size: 15px; font-weight: 500;
  opacity: 0; pointer-events: none;
  transition: opacity 0.3s, transform 0.3s;
  z-index: 10;
}
.levelup.show { opacity: 1; transform: translateX(-50%) translateY(0); }
