:root {
  color-scheme: light dark;
  --orange: #ea580c;
  --orange-soft: #fb923c;
  --orange-dim: rgba(234, 88, 12, 0.12);
  --green: #22c55e;
  --green-1: #dcfce7;
  --green-2: #86efac;
  --green-3: #22c55e;
  --green-4: #15803d;

  --bg: #fff7ed;
  --bg-2: #ffffff;
  --surface: #ffffff;
  --surface-2: #fffbf5;
  --border: #f1d9c4;
  --text: #1c1410;
  --text-soft: #715b4c;
  --muted: #a08778;
  --empty-cell: #f0e3d6;
  --shadow: 0 8px 28px rgba(124, 45, 18, 0.10);
  --radius: 16px;
  --radius-sm: 10px;
  --ring: 0 0 0 3px rgba(234, 88, 12, 0.35);
}

[data-theme="dark"] {
  --bg: #1a1209;
  --bg-2: #20160c;
  --surface: #261a0f;
  --surface-2: #2e2013;
  --border: #43301c;
  --text: #fdf5ec;
  --text-soft: #cdb39c;
  --muted: #9a8167;
  --empty-cell: #3a2917;
  --green-1: #14361f;
  --green-2: #1d6b39;
  --green-3: #22c55e;
  --green-4: #4ade80;
  --orange-dim: rgba(234, 88, 12, 0.20);
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
  --ring: 0 0 0 3px rgba(251, 146, 60, 0.45);
}

* { box-sizing: border-box; }

html, body { margin: 0; padding: 0; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
  overflow-x: hidden;
  -webkit-text-size-adjust: 100%;
  padding-bottom: env(safe-area-inset-bottom);
}

/* ---------- Header ---------- */
.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: calc(env(safe-area-inset-top) + 14px) 16px 14px;
  background: linear-gradient(135deg, var(--orange) 0%, var(--orange-soft) 100%);
  color: #fff;
  box-shadow: 0 4px 18px rgba(234, 88, 12, 0.28);
}

.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }

.brand-mark {
  display: grid;
  place-items: center;
  width: 42px;
  height: 42px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.20);
  color: #fff;
  flex: 0 0 auto;
}

.brand-text { min-width: 0; }
.brand-text h1 {
  font-size: 1.15rem;
  margin: 0;
  font-weight: 800;
  letter-spacing: -0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.tagline { margin: 0; font-size: 0.78rem; opacity: 0.92; }

.icon-btn {
  appearance: none;
  border: none;
  cursor: pointer;
  background: rgba(255, 255, 255, 0.18);
  color: #fff;
  width: 42px;
  height: 42px;
  border-radius: 12px;
  font-size: 1.2rem;
  display: grid;
  place-items: center;
  flex: 0 0 auto;
  transition: background 0.15s ease, transform 0.1s ease;
}
.icon-btn:hover { background: rgba(255, 255, 255, 0.30); }
.icon-btn:active { transform: scale(0.94); }

/* ---------- Layout ---------- */
.container {
  max-width: 880px;
  margin: 0 auto;
  padding: 18px 16px 40px;
}

.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 18px;
}

/* ---------- Buttons ---------- */
.btn {
  appearance: none;
  cursor: pointer;
  font: inherit;
  font-weight: 700;
  border-radius: var(--radius-sm);
  padding: 11px 16px;
  border: 1px solid transparent;
  transition: transform 0.08s ease, background 0.15s ease, box-shadow 0.15s ease;
  display: inline-flex;
  align-items: center;
  gap: 7px;
}
.btn:active { transform: scale(0.97); }
.btn-primary { background: var(--orange); color: #fff; box-shadow: 0 3px 10px rgba(234,88,12,0.30); }
.btn-primary:hover { background: #c2410c; }
.btn-ghost { background: transparent; color: var(--text); border-color: var(--border); }
.btn-ghost:hover { background: var(--surface-2); }
.btn-danger { background: #dc2626; color: #fff; }
.btn-danger:hover { background: #b91c1c; }

.seg {
  display: inline-flex;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 3px;
  gap: 3px;
}
.seg-btn {
  appearance: none;
  border: none;
  background: transparent;
  cursor: pointer;
  font: inherit;
  font-weight: 700;
  color: var(--text-soft);
  padding: 8px 16px;
  border-radius: 8px;
  transition: background 0.15s ease, color 0.15s ease;
}
.seg-btn.is-active { background: var(--orange); color: #fff; }

/* ---------- Empty state ---------- */
.empty-state {
  text-align: center;
  color: var(--text-soft);
  background: var(--surface);
  border: 1.5px dashed var(--border);
  border-radius: var(--radius);
  padding: 40px 24px;
  margin: 8px 0;
}

/* ---------- Habit cards ---------- */
.habit-list { display: flex; flex-direction: column; gap: 16px; }

.habit-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  border-left: 6px solid var(--habit-color, var(--orange));
}

.habit-top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.habit-id { display: flex; align-items: center; gap: 12px; min-width: 0; }

.habit-emoji {
  width: 46px;
  height: 46px;
  flex: 0 0 auto;
  display: grid;
  place-items: center;
  font-size: 1.5rem;
  border-radius: 12px;
  background: var(--habit-tint, var(--orange-dim));
}

.habit-name {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 800;
  letter-spacing: -0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.habit-streaks {
  display: flex;
  gap: 14px;
  font-size: 0.8rem;
  color: var(--text-soft);
  margin-top: 2px;
  flex-wrap: wrap;
}
.streak-val { font-weight: 800; color: var(--text); }
.streak-flame { color: var(--orange); }

.habit-actions { display: flex; gap: 6px; flex: 0 0 auto; }
.mini-btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text-soft);
  cursor: pointer;
  width: 36px;
  height: 36px;
  border-radius: 9px;
  display: grid;
  place-items: center;
  font-size: 1rem;
  transition: background 0.15s, color 0.15s;
}
.mini-btn:hover { background: var(--orange-dim); color: var(--orange); }

/* ---------- Today toggle ---------- */
.today-row {
  margin-top: 14px;
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}
.today-btn {
  appearance: none;
  cursor: pointer;
  font: inherit;
  font-weight: 700;
  border-radius: 999px;
  padding: 10px 18px;
  border: 2px solid var(--habit-color, var(--orange));
  background: transparent;
  color: var(--habit-color, var(--orange));
  display: inline-flex;
  align-items: center;
  gap: 8px;
  transition: transform 0.08s ease, background 0.15s ease, color 0.15s ease;
}
.today-btn:active { transform: scale(0.96); }
.today-btn.is-done {
  background: var(--habit-color, var(--orange));
  color: #fff;
}
.today-btn .check { font-weight: 900; }
.today-hint { font-size: 0.82rem; color: var(--muted); }

/* ---------- Week view ---------- */
.week-grid {
  margin-top: 14px;
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 6px;
}
.week-cell {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface-2);
  cursor: pointer;
  border-radius: 12px;
  padding: 8px 4px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  transition: transform 0.08s ease, background 0.15s ease, border-color 0.15s ease;
}
.week-cell:active { transform: scale(0.95); }
.week-cell .dow { font-size: 0.66rem; color: var(--muted); font-weight: 700; text-transform: uppercase; }
.week-cell .dom { font-size: 0.95rem; font-weight: 800; color: var(--text); }
.week-cell .dot {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--empty-cell);
  border: 1px solid var(--border);
}
.week-cell.is-done { background: var(--habit-tint, var(--green-1)); border-color: var(--habit-color, var(--green)); }
.week-cell.is-done .dot { background: var(--habit-color, var(--green)); border-color: var(--habit-color, var(--green)); }
.week-cell.is-today { box-shadow: inset 0 0 0 2px var(--orange); }
.week-cell.is-future { opacity: 0.45; cursor: not-allowed; }

/* ---------- Year heatmap ---------- */
.heatmap-wrap {
  margin-top: 14px;
  overflow-x: auto;
  padding-bottom: 6px;
  -webkit-overflow-scrolling: touch;
}
.heatmap {
  display: grid;
  grid-auto-flow: column;
  grid-template-rows: repeat(7, 12px);
  gap: 3px;
  width: max-content;
}
.hm-cell {
  width: 12px;
  height: 12px;
  border-radius: 3px;
  background: var(--empty-cell);
}
.hm-cell.lvl-1 { background: var(--green-1); }
.hm-cell.lvl-2 { background: var(--green-2); }
.hm-cell.lvl-3 { background: var(--green-3); }
.hm-cell.lvl-4 { background: var(--green-4); }
.hm-cell.is-today { outline: 2px solid var(--orange); outline-offset: 1px; }
.hm-cell.is-spacer { background: transparent; }

.heatmap-legend {
  margin-top: 10px;
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.72rem;
  color: var(--muted);
  flex-wrap: wrap;
}
.legend-cells { display: inline-flex; gap: 3px; }
.legend-cells span {
  width: 12px;
  height: 12px;
  border-radius: 3px;
  display: inline-block;
}
.heatmap-stats { margin-top: 6px; font-size: 0.8rem; color: var(--text-soft); }
.heatmap-stats b { color: var(--text); }

.year-nav {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  margin-top: 12px;
}
.year-nav button {
  appearance: none;
  cursor: pointer;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  border-radius: 8px;
  width: 32px;
  height: 32px;
  font-size: 1rem;
  display: grid;
  place-items: center;
}
.year-nav button:disabled { opacity: 0.4; cursor: not-allowed; }
.year-nav .year-label { font-weight: 800; min-width: 54px; text-align: center; }

/* ---------- Modal ---------- */
.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(28, 20, 16, 0.55);
  backdrop-filter: blur(3px);
}
.modal-card {
  background: var(--surface);
  border-radius: var(--radius);
  width: 100%;
  max-width: 440px;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 20px 60px rgba(0,0,0,0.4);
  border: 1px solid var(--border);
}
.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 18px;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: var(--surface);
}
.modal-head h2 { margin: 0; font-size: 1.1rem; }
.modal-head .icon-btn { background: var(--surface-2); color: var(--text-soft); }
.modal-head .icon-btn:hover { background: var(--orange-dim); color: var(--orange); }
.modal-body { padding: 18px; }

.field { display: block; margin-bottom: 16px; border: none; padding: 0; }
.field-label { display: block; font-size: 0.82rem; font-weight: 700; color: var(--text-soft); margin-bottom: 6px; }
.field input[type="text"] {
  width: 100%;
  font: inherit;
  padding: 11px 12px;
  border-radius: var(--radius-sm);
  border: 1.5px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
}
.field input[type="text"]:focus { outline: none; border-color: var(--orange); box-shadow: var(--ring); }

.emoji-quick { display: flex; flex-wrap: wrap; gap: 6px; margin: -8px 0 16px; }
.emoji-quick button {
  appearance: none;
  cursor: pointer;
  border: 1px solid var(--border);
  background: var(--surface-2);
  border-radius: 9px;
  width: 38px;
  height: 38px;
  font-size: 1.15rem;
  display: grid;
  place-items: center;
  transition: transform 0.08s, background 0.15s;
}
.emoji-quick button:hover { background: var(--orange-dim); }
.emoji-quick button:active { transform: scale(0.9); }

.color-row { display: flex; flex-wrap: wrap; gap: 10px; }
.color-swatch {
  appearance: none;
  cursor: pointer;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 3px solid transparent;
  transition: transform 0.1s ease;
}
.color-swatch:hover { transform: scale(1.1); }
.color-swatch.is-selected { border-color: var(--text); box-shadow: 0 0 0 2px var(--surface), 0 0 0 4px var(--text); }
.color-swatch:focus-visible { outline: none; box-shadow: var(--ring); }

.form-error { color: #dc2626; font-size: 0.85rem; margin: 0 0 12px; font-weight: 600; }

.modal-actions { display: flex; align-items: center; gap: 10px; margin-top: 8px; }
.modal-actions .spacer { flex: 1; }

/* ---------- In-App-Confirm/Prompt ---------- */
.wi-confirm-card { max-width: 380px; }
.wi-confirm-msg {
  margin: 0 0 18px;
  font-size: 0.95rem;
  color: var(--text);
  line-height: 1.5;
}
.wi-prompt-input {
  width: 100%;
  font: inherit;
  padding: 11px 12px;
  border-radius: var(--radius-sm);
  border: 1.5px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  margin-bottom: 18px;
}
.wi-prompt-input:focus { outline: none; border-color: var(--orange); box-shadow: var(--ring); }

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(20px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 11px 18px;
  border-radius: 999px;
  font-size: 0.88rem;
  font-weight: 600;
  z-index: 60;
  box-shadow: 0 8px 24px rgba(0,0,0,0.3);
  animation: toastIn 0.2s ease;
}
@keyframes toastIn { from { opacity: 0; transform: translate(-50%, 10px); } to { opacity: 1; transform: translate(-50%, 0); } }

/* ---------- Footer ---------- */
.app-footer {
  text-align: center;
  padding: 24px 16px calc(24px + env(safe-area-inset-bottom));
  color: var(--muted);
  font-size: 0.78rem;
}

/* ---------- Focus ---------- */
:focus-visible { outline: none; box-shadow: var(--ring); border-radius: 8px; }

/* ---------- Responsive ---------- */
@media (max-width: 420px) {
  .toolbar { flex-direction: column; align-items: stretch; }
  .toolbar .btn-primary { justify-content: center; }
  .seg { justify-content: center; }
  .week-cell .dom { font-size: 0.85rem; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: 0.001ms !important; transition-duration: 0.001ms !important; }
}
