:root {
  --bg: #f3f1ea;
  --card: #fffdf8;
  --text: #1f2a24;
  --muted: #66736b;
  --line: #e4e0d4;
  --accent: #3f7d5c;
  --accent-soft: #dcebe1;
  --accent-ink: #ffffff;
  --water: #3b82a6;
  --water-soft: #d9ebf3;
  --sleep: #6b5fa8;
  --sleep-soft: #e5e1f4;
  --danger: #b4483c;
  --radius: 18px;
  --tabbar-h: 64px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #151a17;
    --card: #1e2520;
    --text: #e7ece8;
    --muted: #96a39b;
    --line: #2d3630;
    --accent: #7cc49c;
    --accent-soft: #24352b;
    --accent-ink: #0f1f16;
    --water: #6fb6d8;
    --water-soft: #1e3039;
    --sleep: #a99ce6;
    --sleep-soft: #2a2740;
    --danger: #e0806f;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body {
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}
button, input, select, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }

.app {
  max-width: 560px;
  margin: 0 auto;
  padding: calc(env(safe-area-inset-top) + 12px) 16px calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 24px);
}

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

.top { margin: 8px 4px 18px; }
.eyebrow { margin: 0; font-size: 13px; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); }
h1 { margin: 2px 0 2px; font-size: 30px; line-height: 1.15; font-weight: 700; letter-spacing: -.01em; }
.sub { margin: 0; color: var(--muted); }

/* ---------- Cards ---------- */
.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
}
.card h2 { margin: 0 0 12px; font-size: 16px; font-weight: 650; }
.card-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; margin-bottom: 12px; }
.card-head h2 { margin: 0; }
.meta { font-size: 14px; color: var(--muted); }
.hint { font-size: 14px; color: var(--muted); margin: 0 0 12px; }

/* ---------- Buttons ---------- */
.btn {
  appearance: none; border: 1px solid transparent; border-radius: 12px;
  padding: 12px 16px; min-height: 46px; font-weight: 600; cursor: pointer;
  transition: transform .08s ease, background .2s ease;
}
.btn:active { transform: scale(.97); }
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.ghost { background: transparent; border-color: var(--line); color: var(--text); }
.btn.danger { background: transparent; border-color: var(--danger); color: var(--danger); }
.btn.wide { width: 100%; }
.btn.grow { flex: 1; }
.btn-row { display: flex; gap: 8px; margin-top: 14px; }
.btn-row .btn.ghost { width: 56px; font-size: 20px; }
.btn-col { display: grid; gap: 8px; }
.file-btn { display: flex; align-items: center; justify-content: center; }

/* ---------- Ring ---------- */
.ring-card { display: flex; align-items: center; gap: 16px; }
.ring { width: 104px; height: 104px; flex: none; transform: rotate(-90deg); }
.ring circle { fill: none; stroke-width: 10; }
.ring-bg { stroke: var(--accent-soft); }
.ring-fg { stroke: var(--accent); stroke-linecap: round; stroke-dasharray: 326.7; stroke-dashoffset: 326.7; transition: stroke-dashoffset .6s ease; }
.ring-text { display: flex; flex-direction: column; flex: 1; }
.ring-text strong { font-size: 30px; line-height: 1.1; }
.ring-text span { color: var(--muted); font-size: 14px; }
.ring-card { position: relative; }
.ring-card .ring + .ring-text { margin-left: 0; }
.streak { text-align: center; padding: 8px 12px; border-radius: 14px; background: var(--accent-soft); }
.streak-num { display: block; font-size: 24px; font-weight: 700; color: var(--accent); line-height: 1.1; }
.streak-label { font-size: 12px; color: var(--muted); }

/* ---------- Water ---------- */
.glasses { display: grid; grid-template-columns: repeat(8, 1fr); gap: 6px; }
.glass {
  height: 44px; border-radius: 6px 6px 12px 12px; border: 2px solid var(--water);
  background: transparent; position: relative; overflow: hidden; cursor: pointer; padding: 0;
}
.glass::after {
  content: ""; position: absolute; left: 0; right: 0; bottom: 0; height: 0;
  background: var(--water); transition: height .35s ease;
}
.glass.full::after { height: 100%; }
.glass.extra { border-style: dashed; }

/* ---------- Mood ---------- */
.moods { display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px; }
.mood {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 10px 2px; border-radius: 14px; border: 1px solid var(--line);
  background: transparent; cursor: pointer; transition: background .2s ease, border-color .2s ease;
}
.mood .face { font-size: 26px; line-height: 1; }
.mood .lbl { font-size: 11px; color: var(--muted); }
.mood[aria-checked="true"] { background: var(--accent-soft); border-color: var(--accent); }
.mood[aria-checked="true"] .lbl { color: var(--text); font-weight: 600; }

/* ---------- Sleep ---------- */
.stepper { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.stepper .btn { width: 56px; font-size: 22px; }
.stepper-val { text-align: center; }
.stepper-val b { display: block; font-size: 34px; line-height: 1.1; color: var(--sleep); }
.stepper-val span { font-size: 13px; color: var(--muted); }

/* ---------- Habits ---------- */
.habits { list-style: none; margin: 0; padding: 0; }
.habits li + li { border-top: 1px solid var(--line); }
.habit {
  display: flex; align-items: center; gap: 12px; width: 100%;
  padding: 12px 2px; background: none; border: 0; text-align: left; cursor: pointer;
}
.check {
  width: 26px; height: 26px; border-radius: 8px; border: 2px solid var(--line); flex: none;
  display: grid; place-items: center; transition: background .2s ease, border-color .2s ease;
}
.check svg { width: 16px; height: 16px; stroke: var(--accent-ink); stroke-width: 3; fill: none; opacity: 0; }
.habit[aria-checked="true"] .check { background: var(--accent); border-color: var(--accent); }
.habit[aria-checked="true"] .check svg { opacity: 1; }
.habit[aria-checked="true"] .habit-name { color: var(--muted); text-decoration: line-through; }
.empty { color: var(--muted); font-size: 14px; padding: 6px 0; }

/* ---------- Note ---------- */
textarea, input[type="text"], input[type="number"], select {
  width: 100%; padding: 12px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--bg); resize: vertical;
}
textarea:focus, input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

/* ---------- Breathe ---------- */
.segmented { display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; padding: 4px; background: var(--card); border: 1px solid var(--line); border-radius: 14px; }
.seg { border: 0; background: transparent; padding: 10px 4px; border-radius: 10px; font-size: 13px; font-weight: 600; color: var(--muted); cursor: pointer; }
.seg.active { background: var(--accent); color: var(--accent-ink); }

.breath-stage { display: grid; place-items: center; height: 320px; position: relative; }
.breath-circle {
  grid-area: 1 / 1;
  width: 150px; height: 150px; border-radius: 50%;
  background: radial-gradient(circle at 50% 40%, var(--accent-soft), var(--accent) 140%);
  opacity: .9;
  transform: scale(1);
  transition-property: transform;
  transition-timing-function: ease-in-out;
}
.breath-inner { grid-area: 1 / 1; display: flex; flex-direction: column; align-items: center; z-index: 1; }
.breath-phase { font-size: 18px; font-weight: 650; }
.breath-count { font-size: 15px; color: var(--muted); min-height: 22px; }
.breath-meta { display: flex; align-items: center; gap: 10px; margin-bottom: 14px; }
.breath-meta label { color: var(--muted); font-size: 14px; flex: none; }
.breath-meta select { width: auto; flex: 1; }
.time-left { font-variant-numeric: tabular-nums; color: var(--muted); min-width: 48px; text-align: right; }

/* ---------- History bars ---------- */
.bars { display: grid; grid-template-columns: repeat(7, 1fr); gap: 8px; align-items: end; height: 148px; position: relative; }
.bar-day { line-height: 18px; }
.bar-col { display: flex; flex-direction: column; align-items: center; justify-content: flex-end; height: 100%; gap: 4px; }
.bar-val { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.bar { width: 100%; max-width: 34px; border-radius: 8px 8px 4px 4px; min-height: 4px; transition: height .5s ease; }
.bar.water { background: var(--water); }
.bar.sleep { background: var(--sleep); }
.bar.empty-bar { background: var(--line); }
.bar-day { font-size: 12px; color: var(--muted); }
.bar-day.today { color: var(--text); font-weight: 700; }
.goal-line { position: absolute; left: 0; right: 0; border-top: 1.5px dashed var(--muted); opacity: .5; pointer-events: none; }

.mood-week { display: grid; grid-template-columns: repeat(7, 1fr); gap: 8px; text-align: center; }
.mood-week .face { font-size: 24px; height: 32px; }
.mood-week .face.none { color: var(--line); font-size: 18px; line-height: 32px; }

.notes { list-style: none; margin: 0; padding: 0; }
.notes li { padding: 10px 0; border-top: 1px solid var(--line); }
.notes li:first-child { border-top: 0; padding-top: 0; }
.notes .d { display: block; font-size: 12px; color: var(--muted); }

/* ---------- Settings ---------- */
.field { margin-bottom: 12px; }
.field:last-child { margin-bottom: 0; }
.field label { display: block; font-size: 14px; color: var(--muted); margin-bottom: 6px; }
.habit-edit { list-style: none; margin: 0 0 12px; padding: 0; }
.habit-edit li { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 0; border-top: 1px solid var(--line); }
.habit-edit li:first-child { border-top: 0; }
.habit-edit button { border: 0; background: none; color: var(--danger); font-weight: 600; cursor: pointer; padding: 6px 8px; }
.add-habit { display: flex; gap: 8px; }
.add-habit input { flex: 1; }
.ios-steps { margin: 0 0 8px; padding-left: 20px; }
.footer { text-align: center; color: var(--muted); font-size: 13px; margin: 20px 0 0; }

/* ---------- Tab bar ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: grid; grid-template-columns: repeat(4, 1fr);
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: color-mix(in srgb, var(--card) 88%, transparent);
  backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
  border-top: 1px solid var(--line);
}
.tab {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  background: none; border: 0; color: var(--muted); font-size: 11px; font-weight: 600; cursor: pointer;
}
.tab svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.tab.active { color: var(--accent); }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 16px);
  transform: translateX(-50%) translateY(20px); opacity: 0; pointer-events: none;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 999px;
  font-size: 14px; font-weight: 600; transition: opacity .25s ease, transform .25s ease; z-index: 20;
  max-width: calc(100% - 32px); text-align: center;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

@media (prefers-reduced-motion: reduce) {
  .view, .ring-fg, .glass::after, .bar { animation: none; transition: none; }
}
