:root {
  --bg: #eef3f1;
  --surface: #ffffff;
  --surface-2: #f5f9f8;
  --ink: #0e2a2d;
  --muted: #5a7174;
  --line: #d9e4e1;
  --primary: #1b7a87;
  --primary-soft: #dcf0f2;
  --on-primary: #ffffff;
  --treat: #f4a62a;
  --treat-deep: #d98a0c;
  --on-treat: #3a2500;
  --good: #2f9e6b;
  --good-soft: #dff3e9;
  --bad: #d2543f;
  --bad-soft: #fbe6e1;
  --shadow: 0 1px 2px rgba(14, 42, 45, 0.06), 0 6px 18px rgba(14, 42, 45, 0.06);
  --radius: 18px;
  --radius-sm: 12px;
  --font-body: -apple-system, system-ui, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  --font-head: ui-rounded, "SF Pro Rounded", -apple-system, system-ui, "Segoe UI", sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0c1a1c;
    --surface: #132629;
    --surface-2: #19302f;
    --ink: #e7f2f0;
    --muted: #8fa8aa;
    --line: #234044;
    --primary: #45b6c4;
    --primary-soft: #153b40;
    --on-primary: #05262b;
    --treat: #f4b13f;
    --treat-deep: #d99a22;
    --on-treat: #2d1d00;
    --good: #55c78f;
    --good-soft: #163a2b;
    --bad: #f0806b;
    --bad-soft: #43211b;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 6px 18px rgba(0, 0, 0, 0.25);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-body);
  font-size: 16px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}
body.sheet-open { overflow: hidden; }

button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; -webkit-tap-highlight-color: transparent; touch-action: manipulation; }
a { color: var(--primary); }
h1, h2, h3 { font-family: var(--font-head); line-height: 1.15; margin: 0; letter-spacing: -0.01em; }
h1 { font-size: 28px; }
h2 { font-size: 20px; }
h3 { font-size: 17px; }
p { margin: 0; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.hidden { display: none !important; }

:focus-visible { outline: 3px solid var(--primary); outline-offset: 2px; border-radius: 8px; }

/* App frame */
#app { max-width: 560px; margin: 0 auto; min-height: 100vh; }
.topbar {
  position: sticky; top: 0; z-index: 20;
  padding: calc(env(safe-area-inset-top, 0px) + 10px) 18px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(1.4) blur(14px);
  -webkit-backdrop-filter: saturate(1.4) blur(14px);
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
}
.topbar .title { font-family: var(--font-head); font-weight: 700; font-size: 22px; }
.topbar .sub { color: var(--muted); font-size: 13px; }
.sync-pill {
  display: inline-flex; align-items: center; gap: 6px; padding: 6px 10px;
  border-radius: 999px; background: var(--surface); border: 1px solid var(--line);
  font-size: 12px; color: var(--muted);
}
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); display: inline-block; }
.dot.ok { background: var(--good); }
.dot.busy { background: var(--treat); animation: pulse 1s infinite ease-in-out; }
.dot.err { background: var(--bad); }
@keyframes pulse { 50% { opacity: 0.35; } }
.icon-btn {
  width: 40px; height: 40px; border-radius: 50%; border: 1px solid var(--line);
  background: var(--surface); display: grid; place-items: center; color: var(--ink);
}
.icon-btn svg { width: 20px; height: 20px; }

main { padding: 6px 16px calc(env(safe-area-inset-bottom, 0px) + 110px); }
section + section { margin-top: 18px; }

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  padding: 8px 10px calc(env(safe-area-inset-bottom, 0px) + 8px);
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: saturate(1.4) blur(14px);
  -webkit-backdrop-filter: saturate(1.4) blur(14px);
  border-top: 1px solid var(--line);
}
.tabbar-inner { max-width: 560px; margin: 0 auto; display: grid; grid-template-columns: repeat(5, 1fr); gap: 4px; align-items: end; }
.tab {
  background: none; border: 0; padding: 6px 2px; display: flex; flex-direction: column; align-items: center; gap: 3px;
  font-size: 11px; color: var(--muted); border-radius: 12px;
}
.tab svg { width: 24px; height: 24px; }
.tab[aria-current="page"] { color: var(--primary); font-weight: 600; }
.tab.clicker-tab .bubble {
  width: 54px; height: 54px; border-radius: 50%; margin-top: -22px;
  background: var(--treat); color: var(--on-treat); display: grid; place-items: center;
  box-shadow: 0 6px 16px rgba(217, 138, 12, 0.45); border: 4px solid var(--bg);
}
.tab.clicker-tab[aria-current="page"] { color: var(--ink); }

/* Cards and rows */
.card { background: var(--surface); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
.card + .card { margin-top: 12px; }
.card.flat { box-shadow: none; border: 1px solid var(--line); }
.card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; margin-bottom: 10px; }
.stack > * + * { margin-top: 10px; }
.row { display: flex; align-items: center; gap: 12px; }
.row.between { justify-content: space-between; }
.grow { flex: 1; min-width: 0; }
.list { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.list-item {
  display: flex; align-items: center; gap: 12px; width: 100%; text-align: left;
  padding: 13px 16px; background: none; border: 0; border-bottom: 1px solid var(--line);
}
.list-item:last-child { border-bottom: 0; }
.list-item .t { font-weight: 600; }
.list-item .s { color: var(--muted); font-size: 13px; }
.badge {
  display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: 12px; font-weight: 600;
  background: var(--primary-soft); color: var(--primary);
}
.badge.good { background: var(--good-soft); color: var(--good); }
.badge.bad { background: var(--bad-soft); color: var(--bad); }
.badge.treat { background: color-mix(in srgb, var(--treat) 25%, transparent); color: var(--treat-deep); }
.avatar {
  width: 38px; height: 38px; border-radius: 12px; display: grid; place-items: center; flex: none;
  font-family: var(--font-head); font-weight: 700; background: var(--primary-soft); color: var(--primary);
}
.avatar.treat { background: color-mix(in srgb, var(--treat) 25%, transparent); color: var(--treat-deep); }
.avatar.good { background: var(--good-soft); color: var(--good); }
.avatar.bad { background: var(--bad-soft); color: var(--bad); }

/* Buttons */
.btn {
  border: 0; border-radius: 14px; padding: 13px 18px; font-weight: 600;
  background: var(--primary); color: var(--on-primary); display: inline-flex; align-items: center; justify-content: center; gap: 8px;
}
.btn.block { width: 100%; }
.btn.secondary { background: var(--primary-soft); color: var(--primary); }
.btn.ghost { background: transparent; border: 1px solid var(--line); color: var(--ink); }
.btn.danger { background: var(--bad-soft); color: var(--bad); }
.btn.treat { background: var(--treat); color: var(--on-treat); }
.btn.small { padding: 8px 13px; font-size: 14px; border-radius: 12px; }
.btn:disabled { opacity: 0.5; }
.btn-row { display: flex; gap: 10px; flex-wrap: wrap; }
.btn-row > .btn { flex: 1; }

.chips { display: flex; gap: 8px; overflow-x: auto; padding: 2px 0 6px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; padding: 7px 13px; border-radius: 999px; border: 1px solid var(--line);
  background: var(--surface); font-size: 14px; color: var(--ink);
}
.chip[aria-pressed="true"] { background: var(--primary); color: var(--on-primary); border-color: var(--primary); }

.segmented { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; background: var(--surface-2); border: 1px solid var(--line); border-radius: 14px; padding: 3px; }
.segmented button { border: 0; background: none; padding: 9px 8px; border-radius: 11px; font-weight: 600; font-size: 14px; color: var(--muted); }
.segmented button[aria-pressed="true"] { background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }

/* Level pips */
.pips { display: inline-flex; gap: 3px; }
.pips i { width: 14px; height: 6px; border-radius: 3px; background: var(--line); display: block; }
.pips i.on { background: var(--good); }

/* Today */
.hero { background: linear-gradient(160deg, var(--primary) 0%, #0f5560 100%); color: #fff; border-radius: 24px; padding: 20px; box-shadow: var(--shadow); }
.hero .eyebrow { opacity: 0.8; font-size: 14px; }
.hero h1 { color: #fff; margin: 4px 0 12px; }
.hero .stats { display: flex; gap: 18px; }
.stat b { font-family: var(--font-head); font-size: 24px; display: block; line-height: 1.1; }
.stat span { font-size: 12px; opacity: 0.85; }
.quick { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.quick button {
  border: 1px solid var(--line); background: var(--surface); border-radius: var(--radius-sm); padding: 14px 12px;
  display: flex; flex-direction: column; align-items: flex-start; gap: 6px; text-align: left; box-shadow: var(--shadow);
}
.quick button b { font-size: 15px; }
.quick button span { color: var(--muted); font-size: 12px; }
.tip { background: color-mix(in srgb, var(--treat) 18%, var(--surface)); border-radius: var(--radius); padding: 14px 16px; }

/* Clicker */
.clicker-wrap { display: flex; flex-direction: column; align-items: center; gap: 18px; padding-top: 8px; }
.click-btn {
  width: min(72vw, 40vh, 300px); aspect-ratio: 1; border-radius: 50%; border: 0;
  background: radial-gradient(circle at 35% 30%, #ffd37a 0%, var(--treat) 45%, var(--treat-deep) 100%);
  color: var(--on-treat); font-family: var(--font-head); font-weight: 800; font-size: 34px; letter-spacing: 0.01em;
  box-shadow: 0 18px 40px rgba(217, 138, 12, 0.45), inset 0 -10px 20px rgba(120, 70, 0, 0.25), inset 0 8px 14px rgba(255, 255, 255, 0.4);
  user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; touch-action: manipulation;
  transition: transform 60ms ease-out, box-shadow 60ms ease-out;
}
.click-btn.down { transform: scale(0.95); box-shadow: 0 8px 18px rgba(217, 138, 12, 0.45), inset 0 -6px 14px rgba(120, 70, 0, 0.3), inset 0 6px 10px rgba(255, 255, 255, 0.3); }
.counter { font-family: var(--font-head); font-size: 44px; font-weight: 700; line-height: 1; }

/* Forms */
.field { display: block; }
.field > span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 5px; font-weight: 600; }
input[type="text"], input[type="date"], input[type="number"], input[type="datetime-local"], select, textarea {
  width: 100%; padding: 12px 13px; border-radius: 12px; border: 1px solid var(--line); background: var(--surface-2);
  font-size: 16px; /* prevents iOS zoom */ -webkit-appearance: none; appearance: none; min-height: 46px;
}
textarea { min-height: 84px; resize: vertical; }
.stars { display: flex; gap: 6px; }
.stars button { flex: 1; border: 1px solid var(--line); background: var(--surface-2); border-radius: 12px; padding: 10px 0; font-weight: 700; }
.stars button[aria-pressed="true"] { background: var(--treat); color: var(--on-treat); border-color: var(--treat); }

/* Sheet */
.backdrop { position: fixed; inset: 0; background: rgba(5, 20, 22, 0.5); z-index: 50; animation: fade 160ms ease-out; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 60; max-height: 92vh; display: flex; flex-direction: column;
  background: var(--bg); border-radius: 24px 24px 0 0; max-width: 560px; margin: 0 auto;
  animation: rise 220ms cubic-bezier(0.2, 0.8, 0.2, 1);
}
.sheet-head { padding: 10px 18px 6px; display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.sheet-grab { width: 40px; height: 5px; border-radius: 3px; background: var(--line); margin: 8px auto 0; }
.sheet-body { padding: 8px 18px calc(env(safe-area-inset-bottom, 0px) + 24px); overflow-y: auto; -webkit-overflow-scrolling: touch; }
@keyframes rise { from { transform: translateY(40px); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

.toast {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(env(safe-area-inset-bottom, 0px) + 96px);
  z-index: 80; background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 999px; font-size: 14px; max-width: 90vw;
  box-shadow: var(--shadow);
}

.empty { text-align: center; padding: 28px 18px; color: var(--muted); }
.empty b { display: block; color: var(--ink); font-family: var(--font-head); font-size: 18px; margin-bottom: 4px; }

ol.steps { margin: 0; padding-left: 22px; }
ol.steps li { margin: 8px 0; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; font-size: 14px; }
.code {
  font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 22px; letter-spacing: 0.08em; font-weight: 700;
  text-align: center; padding: 14px; background: var(--surface-2); border: 1px dashed var(--primary); border-radius: 14px; user-select: all; -webkit-user-select: all;
}
.chart { width: 100%; height: auto; display: block; }
.day-label { font-size: 13px; font-weight: 700; color: var(--muted); margin: 18px 4px 8px; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
