:root {
  --page: #ECECEE;
  --surface: #FFFFFF;
  --ink: #1D1E22;
  --muted: #5F626C;
  --line: #D6D7DC;
  --field: #F4F4F6;

  --card: #434654;
  --card-ink: #FFFFFF;
  --card-muted: #D8D9DE;
  --card-btn: #555966;

  --blue: #418BEA;
  --blue-strong: #2A6FCC; /* white text on blue needs the darker shade */
  --blue-press: #215EB0;
  --tile: #E6E6E6;
  --tile-off: rgba(230, 230, 230, 0.16);
  --tile-pre: rgba(255, 255, 255, 0.05);

  --heat0: #D9D9D9;
  --heat1: #8FADE8;
  --heat2: #6A94E3;
  --heat3: #3A68CB;

  --warn: #FAC775;
  --alert: #FFA092;
  --danger: #D93025;

  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --page: #111214;
    --surface: #1C1D22;
    --ink: #F2F2F4;
    --muted: #9A9DA8;
    --line: #34363F;
    --field: #26272D;
    --heat0: #2C2E36;
    --danger: #FF6B5E;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--page);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

body:has(dialog[open]) { overflow: hidden; }

button { font: inherit; color: inherit; touch-action: manipulation; cursor: pointer; }

.app {
  max-width: 560px;
  margin: 0 auto;
  padding:
    calc(env(safe-area-inset-top) + 14px)
    calc(env(safe-area-inset-right) + 16px)
    calc(env(safe-area-inset-bottom) + 40px)
    calc(env(safe-area-inset-left) + 16px);
}

/* ---------- header ---------- */

.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}

.top h1 {
  margin: 0;
  font-size: 34px;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.icon-btn {
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 50%;
  background: var(--surface);
  display: grid;
  place-items: center;
  fill: var(--muted);
}

/* ---------- overall heatmap ---------- */

.overview {
  background: var(--surface);
  border-radius: 18px;
  padding: 14px;
}

.months,
.heatmap {
  display: grid;
  grid-template-columns: repeat(var(--weeks, 20), minmax(0, 1fr));
  column-gap: 3px;
}

.months {
  height: 16px;
  font-size: 11px;
  color: var(--muted);
}

.months span { white-space: nowrap; }

.heatmap {
  grid-template-rows: repeat(7, auto);
  grid-auto-flow: column;
  row-gap: 3px;
}

.heatmap i,
.legend i {
  display: block;
  aspect-ratio: 1;
  border-radius: 3px;
}

.h0 { background: var(--heat0); }
.h1 { background: var(--heat1); }
.h2 { background: var(--heat2); }
.h3 { background: var(--heat3); }
.heatmap .hf { background: transparent; }

.legend {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 3px;
  margin-top: 10px;
  font-size: 11px;
  color: var(--muted);
}

.legend i { width: 10px; }
.legend i:first-of-type { margin-left: 3px; }
.legend i:last-of-type { margin-right: 3px; }

/* ---------- backup reminder and notices ---------- */

.backup-nudge {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: 12px;
  padding: 12px 14px;
  border-radius: 14px;
  background: var(--surface);
}

.backup-nudge[hidden] { display: none; }

.backup-nudge p {
  flex: 1;
  margin: 0;
  font-size: 14px;
  line-height: 1.4;
  color: var(--muted);
}

.nudge-btn {
  flex: none;
  min-height: 36px;
  padding: 0 14px;
  border: 0;
  border-radius: 18px;
  background: var(--blue-strong);
  color: #FFFFFF;
  font-size: 14px;
  font-weight: 600;
}

.notice p { color: var(--ink); }

/* ---------- unlock row ---------- */

.unlock { margin: 16px 2px; }

.unlock p {
  margin: 0;
  font-size: 15px;
  line-height: 1.4;
  color: var(--muted);
}

.unlock p + .btn-primary { margin-top: 12px; }

.lock-line {
  display: flex;
  align-items: center;
  gap: 6px;
}

.lock-line svg { flex: none; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }

.bar {
  height: 6px;
  margin-top: 8px;
  border-radius: 3px;
  background: var(--line);
  overflow: hidden;
}

.bar > div {
  height: 100%;
  border-radius: 3px;
  background: var(--blue);
}

/* ---------- habit cards ---------- */

.card {
  background: var(--card);
  color: var(--card-ink);
  border-radius: 18px;
  padding: 16px;
  margin-top: 12px;
}

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

.title-btn {
  min-width: 0;
  padding: 0;
  border: 0;
  background: none;
  text-align: left;
}

.card .title {
  display: block;
  margin: 0;
  font-size: 22px;
  line-height: 1.15;
  font-weight: 700;
  letter-spacing: -0.01em;
  overflow-wrap: anywhere;
}

.sub {
  display: block;
  margin: 5px 0 0;
  font-size: 15px;
  color: var(--card-muted);
}

.check {
  flex: none;
  width: 50px;
  height: 50px;
  border: 2.5px solid #FFFFFF;
  border-radius: 14px;
  background: var(--card-btn);
  display: grid;
  place-items: center;
  transition: background-color 0.15s, transform 0.1s;
}

.check svg { fill: none; stroke: #FFFFFF; stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; }
.check:active { transform: scale(0.94); }
.check.on { background: var(--blue); }
.check:disabled { opacity: 0.3; cursor: default; }
.check.pop { animation: pop 0.28s ease-out; }

@keyframes pop {
  0% { transform: scale(0.9); }
  60% { transform: scale(1.08); }
  100% { transform: scale(1); }
}

.days {
  display: grid;
  grid-template-columns: repeat(18, minmax(0, 1fr)); /* 18 x 5 = 90 days */
  gap: 4px;
  margin-top: 14px;
}

.days > * {
  display: block;
  aspect-ratio: 1;
  border-radius: 3px;
  padding: 0;
  border: 0;
}

.d-done { background: var(--blue); }
.d-miss { background: var(--tile); }
.d-off { background: var(--tile-off); }
.d-pre { background: var(--tile-pre); }
.d-today { background: #FFFFFF; box-shadow: inset 0 0 0 2px var(--blue); }

.msg {
  margin: 12px 0 0;
  font-size: 13px;
  line-height: 1.4;
}

.msg p { margin: 0; }

.msg-btn {
  min-height: 34px;
  margin-top: 8px;
  padding: 0 14px;
  border: 1.5px solid currentColor;
  border-radius: 17px;
  background: none;
  color: inherit;
  font-size: 14px;
  font-weight: 600;
}

.msg.warn { color: var(--warn); }
.msg.alert { color: var(--alert); }

/* ---------- buttons ---------- */

.btn {
  height: 50px;
  padding: 0 18px;
  border: 0;
  border-radius: 14px;
  background: var(--field);
  font-size: 17px;
  font-weight: 500;
}

.btn-primary {
  width: 100%;
  background: var(--blue-strong);
  color: #FFFFFF;
  font-weight: 600;
}

.btn-primary:active { background: var(--blue-press); }

.btn-link {
  display: block;
  margin: 14px auto 0;
  padding: 8px 12px;
  border: 0;
  background: none;
  font-size: 16px;
}

.danger { color: var(--danger); }

/* ---------- sheets ---------- */

dialog.sheet {
  width: 100%;
  max-width: 560px;
  max-height: 92dvh;
  margin: auto auto 0;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--ink);
}

dialog.sheet::backdrop {
  background: rgba(10, 12, 16, 0.45);
  animation: backdrop-in 0.35s ease-out;
}

dialog.sheet.closing::backdrop { animation: backdrop-out 0.25s ease-in forwards; }

.sheet-body {
  margin: 0;
  background: var(--surface);
  border-radius: 22px 22px 0 0;
  padding: 10px 20px calc(env(safe-area-inset-bottom) + 20px);
  animation: sheet-up 0.42s cubic-bezier(0.32, 0.72, 0, 1);
}

dialog.sheet.closing .sheet-body { animation: sheet-down 0.25s cubic-bezier(0.4, 0, 1, 1) forwards; }

/* iOS-style sheet: slides up from the bottom edge and settles, slides back down to close. */
@keyframes sheet-up {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}

@keyframes sheet-down {
  from { transform: translateY(0); }
  to { transform: translateY(100%); }
}

@keyframes backdrop-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes backdrop-out {
  from { opacity: 1; }
  to { opacity: 0; }
}

.grabber {
  width: 36px;
  height: 5px;
  margin: 0 auto 14px;
  border-radius: 3px;
  background: var(--line);
}

.sheet h2 {
  margin: 0 0 16px;
  font-size: 22px;
  font-weight: 700;
}

.sheet h2:focus { outline: none; }

.field {
  display: block;
  margin: 0 0 16px;
  padding: 0;
  border: 0;
}

.field > span,
.field > legend {
  display: block;
  margin-bottom: 6px;
  padding: 0;
  font-size: 13px;
  color: var(--muted);
}

.field input[type="text"] {
  width: 100%;
  height: 48px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--field);
  color: var(--ink);
  font: inherit;
  font-size: 17px; /* 16px+ keeps iOS from zooming in */
}

.field input[type="text"]:focus { outline: 2px solid var(--blue); outline-offset: -1px; }

.chips {
  display: flex;
  justify-content: space-between;
  gap: 6px;
}

.chip { position: relative; }

.chip input {
  position: absolute;
  inset: 0;
  opacity: 0;
  margin: 0;
  cursor: pointer;
}

.chip span {
  display: grid;
  place-items: center;
  width: 42px;
  height: 42px;
  border-radius: 50%;
  background: var(--field);
  border: 1px solid var(--line);
  font-size: 15px;
  font-weight: 600;
}

.chip input:checked + span { background: var(--blue-strong); border-color: var(--blue-strong); color: #FFFFFF; }
.chip input:focus-visible + span { outline: 2px solid var(--blue); outline-offset: 2px; }

.hint {
  margin: 0 0 14px;
  font-size: 14px;
  line-height: 1.45;
  color: var(--muted);
}

.error {
  margin: -6px 0 12px;
  font-size: 14px;
  color: var(--danger);
}

.error:empty { display: none; }

.actions {
  display: flex;
  gap: 10px;
}

.actions .btn { flex: 1; }

.rules {
  margin: 0 0 14px;
  padding-left: 20px;
  font-size: 15px;
  line-height: 1.45;
}

.rules li + li { margin-top: 6px; }

.menu-list {
  display: grid;
  gap: 10px;
  margin-bottom: 10px;
}

.menu-list .btn { width: 100%; }

/* ---------- 90-day archive suggestion (on a card) ---------- */

.settle {
  margin-top: 14px;
  padding: 12px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.08);
}

.settle p {
  margin: 0;
  font-size: 14px;
  line-height: 1.4;
}

.settle-actions {
  display: flex;
  gap: 8px;
  margin-top: 10px;
}

.settle-btn {
  flex: 1;
  min-height: 38px;
  border: 1.5px solid rgba(255, 255, 255, 0.6);
  border-radius: 19px;
  background: none;
  color: #FFFFFF;
  font-size: 14px;
  font-weight: 600;
}

.settle-btn.primary { background: #FFFFFF; border-color: #FFFFFF; color: var(--card); }

/* ---------- archived list ---------- */

.archived {
  margin-top: 20px;
}

.archived summary {
  padding: 10px 2px;
  font-size: 15px;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
}

.archived-list {
  display: grid;
  gap: 8px;
}

.archived-row {
  width: 100%;
  padding: 12px 14px;
  border: 0;
  border-radius: 14px;
  background: var(--surface);
  text-align: left;
}

.archived-row .title {
  display: block;
  font-size: 16px;
  font-weight: 600;
}

.archived-row .sub {
  display: block;
  margin-top: 2px;
  font-size: 13px;
  color: var(--muted);
}

/* ---------- habit details sheet ---------- */

.sheet-tall {
  max-height: 92dvh;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.sheet h2#detail-title { margin-bottom: 2px; overflow-wrap: anywhere; }

.detail-sub {
  margin: 0 0 14px;
  font-size: 15px;
  color: var(--muted);
}

.stats {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}

.stat {
  padding: 10px 12px;
  border-radius: 12px;
  background: var(--field);
}

.stat strong {
  display: block;
  font-size: 22px;
  font-weight: 700;
}

.stat span {
  font-size: 12px;
  color: var(--muted);
}

.facts {
  margin: 12px 0 0;
  padding: 0;
  list-style: none;
  font-size: 14px;
  line-height: 1.5;
  color: var(--muted);
}

.detail-section {
  margin-top: 20px;
  padding-top: 16px;
  border-top: 1px solid var(--line);
}

.detail-section h3 {
  margin: 0 0 10px;
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}

.history {
  margin: 0;
  padding: 0;
  list-style: none;
  font-size: 14px;
}

.history li {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 4px 0;
}

.history li span:last-child { color: var(--muted); }

.miss-group + .miss-group { margin-top: 12px; }

.miss-group h4 {
  margin: 0 0 6px;
  font-size: 14px;
  font-weight: 600;
}

.miss-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.miss-list li {
  padding: 4px 10px;
  border-radius: 12px;
  background: var(--field);
  font-size: 13px;
}

.chips-hint { margin: 10px 0 0; }

.btn-wide { width: 100%; }

.archive-hint { margin: 8px 0 0; font-size: 13px; }

/* Reduce Motion: swap the slide for a soft fade instead of a hard cut. */
@media (prefers-reduced-motion: reduce) {
  .check.pop { animation: none; }
  .sheet-body { animation: backdrop-in 0.25s ease-out; }
  dialog.sheet.closing .sheet-body { animation: backdrop-out 0.2s ease-in forwards; }
}
