:root {
  --bg: #f5f6fa;
  --card: #ffffff;
  --text: #1f2430;
  --muted: #6b7280;
  --border: #e5e7eb;
  --primary: #4f46e5;
  --primary-soft: #eef2ff;
  --success: #16a34a;
  --danger: #dc2626;
  --radius: 14px;
  --shadow: 0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.04);
  --nav-h: 64px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161c;
    --card: #1e2029;
    --text: #e8eaf0;
    --muted: #9aa1b1;
    --border: #2b2e3a;
    --primary: #7c78ff;
    --primary-soft: #262450;
    --shadow: 0 1px 3px rgba(0,0,0,.4);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
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);
  font-size: 16px;
  line-height: 1.45;
}
a { color: var(--primary); }
button { font-family: inherit; font-size: 1rem; cursor: pointer; }

/* ---------- Layout ---------- */
.app { max-width: 720px; margin: 0 auto; padding: 0 16px calc(var(--nav-h) + 24px); }
header.topbar {
  position: sticky; top: 0; z-index: 20;
  background: var(--bg);
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px; max-width: 720px; margin: 0 auto;
}
.topbar h1 { font-size: 1.15rem; margin: 0; display: flex; align-items: center; gap: 8px; }
.topbar .who { color: var(--muted); font-size: .85rem; }
.icon-btn {
  background: var(--card); border: 1px solid var(--border); color: var(--text);
  border-radius: 10px; padding: 8px 12px; box-shadow: var(--shadow);
}

/* ---------- Cards ---------- */
.card {
  background: var(--card); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 16px; margin-bottom: 12px;
}
.section-title { font-size: 1.3rem; font-weight: 700; margin: 8px 2px 14px; }
.muted { color: var(--muted); }
.center { text-align: center; }
.row { display: flex; align-items: center; gap: 10px; }
.spread { justify-content: space-between; }
.wrap { flex-wrap: wrap; }

/* ---------- Habit items ---------- */
.habit {
  display: flex; align-items: center; gap: 14px;
  padding: 14px 16px; margin-bottom: 10px;
  background: var(--card); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
}
.habit .emoji { font-size: 1.6rem; width: 40px; text-align: center; }
.habit .grow { flex: 1; min-width: 0; }
.habit .hname { font-weight: 600; }
.habit .hsub { font-size: .8rem; color: var(--muted); }
.check {
  width: 46px; height: 46px; border-radius: 50%;
  border: 2px solid var(--border); background: transparent;
  display: grid; place-items: center; font-size: 1.4rem; color: var(--muted);
  transition: transform .08s ease;
  flex-shrink: 0;
}
.check:active { transform: scale(.9); }
.check.done { background: var(--success); border-color: var(--success); color: #fff; }

/* ---------- Buttons ---------- */
.btn {
  border: none; border-radius: 10px; padding: 12px 16px;
  background: var(--primary); color: #fff; font-weight: 600;
  box-shadow: var(--shadow);
}
.btn.block { width: 100%; }
.btn.ghost { background: var(--card); color: var(--text); border: 1px solid var(--border); }
.btn.danger { background: var(--danger); }
.btn.small { padding: 8px 12px; font-size: .85rem; }

/* ---------- Forms ---------- */
label { display: block; font-size: .85rem; color: var(--muted); margin: 10px 0 4px; }
input, textarea, select {
  width: 100%; padding: 12px; font-size: 1rem;
  border: 1px solid var(--border); border-radius: 10px;
  background: var(--bg); color: var(--text);
}
textarea { min-height: 140px; resize: vertical; }
.field-inline { display: flex; gap: 10px; }
.field-inline > div { flex: 1; }

/* ---------- Progress ---------- */
.stat-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.stat { text-align: center; padding: 10px; background: var(--primary-soft); border-radius: 10px; }
.stat .num { font-size: 1.5rem; font-weight: 700; }
.stat .lbl { font-size: .72rem; color: var(--muted); }
.heat { display: grid; grid-template-columns: repeat(15, 1fr); gap: 3px; margin-top: 10px; }
.heat .cell { aspect-ratio: 1; border-radius: 3px; background: var(--border); }
.heat .cell.on { background: var(--success); }

/* ---------- Journal ---------- */
.jentry { cursor: pointer; }
.jentry .jdate { font-size: .78rem; color: var(--muted); }
.jentry .jtitle { font-weight: 600; }
.jentry .jpreview { color: var(--muted); font-size: .9rem; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }

/* ---------- Bottom nav ---------- */
nav.bottom {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  background: var(--card); border-top: 1px solid var(--border);
  display: flex; height: var(--nav-h);
  padding-bottom: env(safe-area-inset-bottom);
}
nav.bottom button {
  flex: 1; border: none; background: none; color: var(--muted);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  font-size: .68rem;
}
nav.bottom button .ni { font-size: 1.35rem; }
nav.bottom button.active { color: var(--primary); font-weight: 600; }

/* ---------- Modal ---------- */
.modal-bg {
  position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 50;
  display: flex; align-items: flex-end; justify-content: center;
}
.modal {
  background: var(--card); width: 100%; max-width: 640px;
  border-radius: 18px 18px 0 0; padding: 20px 18px calc(20px + env(safe-area-inset-bottom));
  max-height: 92vh; overflow: auto;
}
@media (min-width: 640px) {
  .modal-bg { align-items: center; }
  .modal { border-radius: 18px; }
}
.modal h3 { margin: 0 0 6px; }

/* ---------- Login ---------- */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.login-card { width: 100%; max-width: 360px; }
.brand { text-align: center; margin-bottom: 8px; font-size: 2.4rem; }

/* ---------- Misc ---------- */
.toast {
  position: fixed; bottom: calc(var(--nav-h) + 16px); left: 50%; transform: translateX(-50%);
  background: #111827; color: #fff; padding: 10px 16px; border-radius: 10px;
  z-index: 100; font-size: .9rem; opacity: 0; transition: opacity .2s; pointer-events: none;
}
.toast.show { opacity: .95; }
.empty { text-align: center; color: var(--muted); padding: 40px 10px; }
.emoji-pick { display: flex; flex-wrap: wrap; gap: 6px; }
.emoji-pick button { font-size: 1.3rem; padding: 6px 8px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg); }
.emoji-pick button.sel { border-color: var(--primary); background: var(--primary-soft); }
.color-pick { display: flex; gap: 8px; flex-wrap: wrap; }
.color-pick button { width: 32px; height: 32px; border-radius: 50%; border: 2px solid transparent; }
.color-pick button.sel { border-color: var(--text); }
.date-nav { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 14px; }
.date-nav .d { font-weight: 600; }
hr.sep { border: none; border-top: 1px solid var(--border); margin: 16px 0; }
