:root {
  --bg: #f7f5f2;
  --surface: #ffffff;
  --surface-2: #f0ece6;
  --text: #1c1917;
  --muted: #6b645c;
  --border: #e4ded6;
  --accent: #e8590c;
  --accent-soft: #fde8db;
  --good: #2f9e44;
  --good-soft: #d3f9d8;
  --bad: #e03131;
  --bad-soft: #ffe3e3;
  --neutral: #868e96;
  --shadow: 0 1px 2px rgba(0,0,0,.04), 0 4px 16px rgba(0,0,0,.05);
  --radius: 14px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #141210; --surface: #1d1a17; --surface-2: #272320; --text: #f2eee9; --muted: #a39b91;
    --border: #322d28; --accent-soft: #3a2416; --good-soft: #1b3321; --bad-soft: #3b1d1d;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 4px 16px rgba(0,0,0,.3);
  }
}
:root[data-theme="dark"] {
  --bg: #141210; --surface: #1d1a17; --surface-2: #272320; --text: #f2eee9; --muted: #a39b91;
  --border: #322d28; --accent-soft: #3a2416; --good-soft: #1b3321; --bad-soft: #3b1d1d;
  --shadow: 0 1px 2px rgba(0,0,0,.3), 0 4px 16px rgba(0,0,0,.3);
}

* { box-sizing: border-box; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font-family: Inter, system-ui, -apple-system, "Segoe UI", sans-serif; line-height: 1.5;
}
main { max-width: 980px; margin: 0 auto; padding: 0 16px 48px; }
h1, h2, h3 { line-height: 1.2; margin: 0 0 .4em; letter-spacing: -.01em; }
h1 { font-size: clamp(1.5rem, 4vw, 2.1rem); font-weight: 800; }
h2 { font-size: 1.4rem; }
.muted { color: var(--muted); margin: 0; }
.good { color: var(--good); } .bad { color: var(--bad); } .neutral { color: var(--neutral); }

/* Top bar & tabs */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  max-width: 980px; margin: 0 auto; padding: 18px 16px 8px;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand strong { display: block; letter-spacing: .08em; }
.brand small { color: var(--muted); }
.logo {
  width: 40px; height: 40px; border-radius: 50%; background: var(--accent); color: #fff;
  display: grid; place-items: center; font-weight: 800; font-size: 1.2rem;
}
.icon-btn {
  background: var(--surface); border: 1px solid var(--border); color: var(--text);
  width: 40px; height: 40px; border-radius: 10px; font-size: 1.1rem; cursor: pointer;
}
.tabs {
  display: flex; gap: 4px; overflow-x: auto; max-width: 980px; margin: 0 auto 20px;
  padding: 8px 16px; position: sticky; top: 0; background: var(--bg); z-index: 5; scrollbar-width: none;
}
.tab {
  border: 0; background: transparent; color: var(--muted); font: inherit; font-weight: 600;
  padding: 8px 14px; border-radius: 999px; cursor: pointer; white-space: nowrap;
}
.tab:hover { color: var(--text); }
.tab.active { background: var(--text); color: var(--bg); }

.view { display: none; animation: fade .2s ease; }
.view.active { display: block; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

.section-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px 16px; margin-bottom: 18px; }
.section-head .muted { flex-basis: 100%; }

/* Buttons & inputs */
.btn {
  font: inherit; font-weight: 600; font-size: .92rem; padding: 9px 16px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text); cursor: pointer;
  display: inline-flex; align-items: center; gap: 6px;
}
.btn:hover { border-color: var(--muted); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.05); }
.btn.danger { color: var(--bad); }
.btn.sm { padding: 5px 10px; font-size: .82rem; }
.btn.sm.icon { padding: 5px 9px; }
.btn:disabled { opacity: .4; cursor: default; border-color: var(--border); }
.btn.ghost { background: transparent; border-color: transparent; color: var(--muted); flex-shrink: 0; align-self: center; }
.btn.ghost:hover { border-color: var(--border); color: var(--text); }
input {
  font: inherit; width: 100%; padding: 10px 12px; border-radius: 10px; margin-top: 4px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
}
input:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }

/* Hero */
.hero {
  display: flex; gap: 24px; align-items: center; justify-content: space-between;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 24px; box-shadow: var(--shadow); margin-bottom: 20px;
}
.eyebrow { text-transform: uppercase; letter-spacing: .1em; font-size: .75rem; font-weight: 700; color: var(--accent); margin: 0 0 6px; }
.ring-wrap { position: relative; width: 120px; height: 120px; flex-shrink: 0; }
.ring { width: 100%; height: 100%; transform: rotate(-90deg); }
.ring circle { fill: none; stroke-width: 10; }
.ring-bg { stroke: var(--surface-2); }
.ring-fg { stroke: var(--accent); stroke-linecap: round; stroke-dasharray: 326.7; stroke-dashoffset: 326.7; transition: stroke-dashoffset .5s ease; }
.ring-label { position: absolute; inset: 0; display: grid; place-content: center; text-align: center; }
.ring-label strong { font-size: 1.5rem; }
.ring-label small { color: var(--muted); }

/* Cards */
.cards { display: grid; gap: 12px; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px 18px; box-shadow: var(--shadow);
}
.habit-row { display: flex; gap: 14px; align-items: flex-start; }
.check {
  flex-shrink: 0; width: 44px; height: 44px; border-radius: 12px; border: 2px solid var(--border);
  background: var(--surface); cursor: pointer; display: grid; place-items: center; font-size: 1.3rem;
  color: transparent; transition: all .15s ease;
}
.check:hover { border-color: var(--accent); }
.check.done { background: var(--good); border-color: var(--good); color: #fff; }
.check.break.done { background: var(--accent); border-color: var(--accent); }
.habit-row.skipped .check, .habit-row.skipped .habit-title, .habit-row.skipped .intention { opacity: .5; }
.habit-body { flex: 1; min-width: 0; }
.habit-title { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; font-weight: 700; font-size: 1.05rem; }
.intention { color: var(--muted); font-size: .92rem; margin: 2px 0 0; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.chip { font-size: .78rem; padding: 3px 9px; border-radius: 999px; background: var(--surface-2); color: var(--muted); }
.chip.streak { background: var(--accent-soft); color: var(--accent); font-weight: 700; }
.chip.warn { background: var(--bad-soft); color: var(--bad); font-weight: 700; }
.chip.tag-break { background: var(--bad-soft); color: var(--bad); }
.chip.tag-build { background: var(--good-soft); color: var(--good); }
.chip.skip { background: var(--surface-2); color: var(--text); font-weight: 600; }
.nudge { display: flex; flex-wrap: wrap; gap: 12px; align-items: center; justify-content: space-between; border-color: var(--accent); background: var(--accent-soft); }
.nudge > div:first-child { flex: 1 1 260px; }
.nudge .row { margin: 0; }
.archived-list { margin-top: 20px; }
.archived-list summary { cursor: pointer; font-weight: 700; color: var(--muted); padding: 6px 0 10px; }
.card.archived { box-shadow: none; background: transparent; }
.empty { text-align: center; padding: 40px 20px; color: var(--muted); }
.empty .btn { margin-top: 12px; }

.law-details { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 8px; margin-top: 12px; }
.law-details div { background: var(--surface-2); border-radius: 10px; padding: 8px 12px; font-size: .88rem; }
.law-details b { display: block; font-size: .72rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.card-actions { display: flex; gap: 8px; margin-top: 12px; }

/* Tracker */
.tracker { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); overflow-x: auto; }
.tracker-row { display: flex; align-items: center; gap: 12px; padding: 8px 0; border-bottom: 1px solid var(--border); }
.tracker-row:last-child { border-bottom: 0; }
.tracker-name { width: 150px; flex-shrink: 0; font-weight: 600; font-size: .9rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tracker-cells { flex: 1; min-width: 420px; display: grid; grid-template-columns: repeat(35, minmax(0, 1fr)); gap: 3px; }
.cell { width: 100%; aspect-ratio: 1; max-width: 22px; border-radius: 4px; background: var(--surface-2); border: 0; cursor: pointer; padding: 0; }
.cell.done { background: var(--good); }
.cell.break.done { background: var(--accent); }
.cell.off { opacity: .35; }
.cell.skip { background: repeating-linear-gradient(135deg, var(--neutral) 0 2px, transparent 2px 5px), var(--surface-2); }
.cell.today { outline: 2px solid var(--text); outline-offset: 1px; }
.legend { display: flex; flex-wrap: wrap; gap: 6px 16px; margin-top: 10px; font-size: .8rem; color: var(--muted); }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend .cell { display: inline-block; width: 12px; height: 12px; cursor: default; box-shadow: inset 0 0 0 1px var(--border); }
.tracker-rate { width: 48px; text-align: right; font-weight: 700; font-size: .85rem; color: var(--muted); flex-shrink: 0; }

/* Progress */
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 12px; }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 18px; box-shadow: var(--shadow); display: grid; gap: 2px; }
.tile small { color: var(--muted); font-weight: 600; font-size: .78rem; text-transform: uppercase; letter-spacing: .06em; }
.tile strong { font-size: 2rem; font-weight: 800; line-height: 1.1; }
.tile .muted { font-size: .85rem; }
.sub-head { margin: 26px 0 12px; font-size: 1.05rem; }
.mini-stats { display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px; margin-top: 10px; }
.mini-stats div { background: var(--surface-2); border-radius: 10px; padding: 6px 10px; }
.mini-stats small { display: block; color: var(--muted); font-size: .72rem; text-transform: uppercase; letter-spacing: .05em; }
.mini-stats b { font-size: 1.1rem; }
.trend { margin-top: 12px; }
.trend-bars { display: grid; grid-template-columns: repeat(12, 1fr); gap: 2px; height: 56px; align-items: end; border-bottom: 1px solid var(--border); }
.bar-slot { height: 100%; display: flex; align-items: flex-end; justify-content: center; cursor: default; }
.bar-slot:hover { background: var(--surface-2); border-radius: 4px 4px 0 0; }
.bar-slot i { display: block; width: min(100%, 18px); background: var(--accent); border-radius: 4px 4px 0 0; }
.trend-axis { display: flex; justify-content: space-between; font-size: .72rem; color: var(--muted); margin-top: 4px; }

/* Identity */
.identity-card h3 { font-size: 1.1rem; }
.identity-card h3 span { color: var(--muted); font-weight: 500; }
.votes { font-size: 2rem; font-weight: 800; color: var(--accent); }
.bar { height: 8px; border-radius: 99px; background: var(--surface-2); overflow: hidden; margin-top: 8px; }
.bar > i { display: block; height: 100%; background: var(--accent); border-radius: 99px; }

/* Scorecard */
.inline-form { display: flex; gap: 8px; margin-bottom: 14px; }
.inline-form input { margin: 0; }
.score-list { list-style: none; padding: 0; margin: 0; display: grid; gap: 8px; counter-reset: s; }
.score-list li {
  counter-increment: s; display: flex; align-items: center; gap: 10px; background: var(--surface);
  border: 1px solid var(--border); border-radius: 12px; padding: 8px 10px;
}
.score-list li::before { content: counter(s); color: var(--muted); font-size: .8rem; width: 20px; text-align: right; }
.score-list .txt { flex: 1; }
.rate { display: flex; gap: 4px; }
.rate button { width: 32px; height: 32px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface); color: var(--muted); font-weight: 800; cursor: pointer; }
.rate button.on[data-r="+"] { background: var(--good); border-color: var(--good); color: #fff; }
.rate button.on[data-r="="] { background: var(--neutral); border-color: var(--neutral); color: #fff; }
.rate button.on[data-r="-"] { background: var(--bad); border-color: var(--bad); color: #fff; }
.del { border: 0; background: none; color: var(--muted); cursor: pointer; font-size: 1.1rem; }
.score-summary { display: flex; gap: 12px; margin-top: 14px; flex-wrap: wrap; }
.score-summary span { padding: 6px 12px; border-radius: 99px; font-weight: 700; font-size: .88rem; }

/* Laws */
.laws { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 12px; }
.law { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); }
.law p { margin: 0 0 10px; font-size: .92rem; }
.law .inv { color: var(--muted); font-size: .86rem; margin: 0; padding-top: 10px; border-top: 1px dashed var(--border); }
.law-n {
  display: inline-grid; place-items: center; width: 30px; height: 30px; border-radius: 50%;
  background: var(--accent-soft); color: var(--accent); font-weight: 800; margin-bottom: 10px;
}
.law-n.sm { width: 22px; height: 22px; font-size: .78rem; margin: 0; }
.compound, .data-tools { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; margin-top: 16px; box-shadow: var(--shadow); }
.compound-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.compound-row div { background: var(--surface-2); border-radius: 12px; padding: 14px; }
.compound-row strong { display: block; font-size: 1.8rem; color: var(--accent); }
.compound-row div:last-child strong { color: var(--bad); }
.compound-row small { color: var(--muted); }
.row { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; }
.row.tight { margin-top: 0; }
.hint { font-weight: 500; }

/* Dialog */
dialog {
  border: 1px solid var(--border); border-radius: 18px; padding: 0; width: min(560px, calc(100% - 24px));
  background: var(--surface); color: var(--text); box-shadow: 0 20px 60px rgba(0,0,0,.3);
}
dialog::backdrop { background: rgba(20,18,16,.5); backdrop-filter: blur(2px); }
#habitForm { padding: 22px; display: grid; gap: 12px; max-height: 88vh; overflow-y: auto; }
#habitForm label, .field { font-size: .86rem; font-weight: 600; color: var(--muted); display: block; }
fieldset { border: 1px solid var(--border); border-radius: 12px; padding: 10px 14px 14px; margin: 0; display: grid; gap: 10px; }
legend { font-weight: 700; padding: 0 6px; display: flex; align-items: center; gap: 6px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.type-switch { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; background: var(--surface-2); padding: 4px; border-radius: 12px; }
.type-switch label { margin: 0; text-align: center; padding: 8px; border-radius: 9px; cursor: pointer; color: var(--text) !important; }
.type-switch input { display: none; }
.type-switch label:has(input:checked) { background: var(--surface); box-shadow: var(--shadow); }
.days { display: flex; gap: 6px; margin-top: 6px; flex-wrap: wrap; }
.days button { width: 40px; height: 36px; border-radius: 9px; border: 1px solid var(--border); background: var(--surface); color: var(--muted); font: inherit; font-weight: 600; cursor: pointer; }
.days button.on { background: var(--text); color: var(--bg); border-color: var(--text); }
.dialog-actions { display: flex; gap: 8px; align-items: center; margin-top: 6px; }
.spacer { flex: 1; }
[hidden] { display: none !important; }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translate(-50%, 120px); background: var(--text); color: var(--bg);
  padding: 10px 18px; border-radius: 999px; font-weight: 600; transition: transform .3s ease; z-index: 50; max-width: calc(100% - 32px); text-align: center;
}
.toast.show { transform: translate(-50%, 0); }
footer { text-align: center; color: var(--muted); font-size: .82rem; padding: 24px 16px 36px; }

@media (max-width: 600px) {
  .hero { flex-direction: column-reverse; align-items: flex-start; padding: 18px; }
  .ring-wrap { width: 96px; height: 96px; }
  .grid2, .compound-row { grid-template-columns: 1fr; }
  .tracker-name { width: 90px; }
  .mini-stats { grid-template-columns: repeat(3, 1fr); }
  .habit-row { flex-wrap: wrap; }
}

/* PWA */
.top-actions { display: flex; align-items: center; gap: 8px; }
.ios-dialog { padding: 22px; }
.ios-dialog ol { padding-left: 20px; display: grid; gap: 8px; }
.share-ico { display: inline-block; padding: 0 6px; border: 1px solid var(--border); border-radius: 6px; }
@media (display-mode: standalone) {
  .topbar { padding-top: max(18px, env(safe-area-inset-top)); }
  footer { padding-bottom: max(36px, env(safe-area-inset-bottom)); }
}
