/* ————— The Forge — dark hearth theme ————— */
:root {
  --bg: #0b0908;
  --surface: #161110;
  --surface-2: #1f1816;
  --line: rgba(255, 196, 148, 0.10);
  --line-strong: rgba(255, 196, 148, 0.18);
  --text: #f3ece6;
  --text-dim: #a89a90;
  --text-faint: #6e625a;
  --ember: #ff8a3d;

  --c-heart: #f26d7e;
  --c-soul: #b78cf5;
  --c-mind: #5cc8e8;
  --c-strength: #f2a93d;

  --radius: 18px;
  --pad: 16px;
  font-size: 16px;
}

* { margin: 0; padding: 0; box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body { height: 100%; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: "Outfit", -apple-system, sans-serif;
  font-weight: 400;
  line-height: 1.45;
  padding: calc(env(safe-area-inset-top) + 14px) var(--pad) calc(env(safe-area-inset-bottom) + 28px);
  max-width: 560px;
  margin: 0 auto;
  overflow-x: hidden;
}

.glow-backdrop {
  position: fixed;
  inset: 0;
  pointer-events: none;
  background:
    radial-gradient(60% 34% at 50% -6%, rgba(255, 120, 40, 0.16), transparent 70%),
    radial-gradient(42% 26% at 88% 4%, rgba(255, 80, 60, 0.07), transparent 70%);
  z-index: 0;
}

.app-header, .categories, .app-footer { position: relative; z-index: 1; }

/* ————— Header ————— */
.header-row { display: flex; align-items: flex-start; justify-content: space-between; }

.app-title {
  font-family: "Fraunces", serif;
  font-weight: 600;
  font-size: 1.9rem;
  letter-spacing: 0.01em;
  background: linear-gradient(115deg, #ffe8d2 30%, #ffab66 70%, #ff7a3d);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.app-date { color: var(--text-dim); font-size: 0.86rem; margin-top: 2px; }

.header-actions { display: flex; gap: 6px; padding-top: 6px; }

.icon-btn {
  background: none;
  border: none;
  color: var(--text-dim);
  width: 40px; height: 40px;
  border-radius: 12px;
  display: grid; place-items: center;
  font-size: 1.4rem;
  cursor: pointer;
  transition: background 0.2s, color 0.2s;
}
.icon-btn:active { background: var(--surface-2); color: var(--text); }
.icon-btn:focus-visible { outline: 2px solid var(--ember); outline-offset: 2px; }
.icon-btn.active { color: var(--ember); }

/* Hearth — overall heat */
.hearth {
  margin-top: 18px;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 14px 16px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: linear-gradient(160deg, rgba(255, 138, 61, 0.07), rgba(255, 138, 61, 0.015) 55%);
}

.hearth-flame {
  width: 44px; height: 44px;
  border-radius: 50%;
  display: grid; place-items: center;
  background: radial-gradient(circle at 50% 65%, rgba(255, 154, 61, 0.32), rgba(255, 154, 61, 0.05) 70%);
  position: relative;
}
.hearth-flame::after {
  content: "";
  width: 22px; height: 22px;
  background: currentColor;
  color: var(--ember);
  -webkit-mask: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M12 2c.6 3.2-.6 5-2.2 6.6C8.2 10.2 7 11.9 7 14a5 5 0 0 0 10 0c0-1.1-.3-2-.8-2.9-.9 1-1.9 1.5-1.9 1.5.5-2.6-.4-5.4-2.3-7.2C11.4 4.6 12 2 12 2Z"/></svg>') center / contain no-repeat;
  mask: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M12 2c.6 3.2-.6 5-2.2 6.6C8.2 10.2 7 11.9 7 14a5 5 0 0 0 10 0c0-1.1-.3-2-.8-2.9-.9 1-1.9 1.5-1.9 1.5.5-2.6-.4-5.4-2.3-7.2C11.4 4.6 12 2 12 2Z"/></svg>') center / contain no-repeat;
  animation: flicker 2.6s ease-in-out infinite;
}
@keyframes flicker {
  0%, 100% { transform: scale(1) rotate(0deg); opacity: 1; }
  30% { transform: scale(1.08) rotate(-2deg); opacity: 0.92; }
  60% { transform: scale(0.95) rotate(2deg); opacity: 1; }
}

.hearth-text { flex: 1; display: flex; flex-direction: column; }
.hearth-num {
  font-family: "Fraunces", serif;
  font-weight: 700;
  font-size: 1.5rem;
  line-height: 1.1;
  color: #ffd9b3;
}
.hearth-label { font-size: 0.74rem; color: var(--text-faint); letter-spacing: 0.06em; text-transform: uppercase; }
.hearth-today { font-size: 0.82rem; color: var(--text-dim); text-align: right; max-width: 40%; }
.hearth-today b { color: var(--text); font-weight: 600; }

/* ————— Categories ————— */
.categories { margin-top: 22px; display: flex; flex-direction: column; gap: 18px; }

.empty-hint {
  text-align: center;
  color: var(--text-faint);
  font-size: 0.9rem;
  padding: 26px 20px 6px;
}

.category {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  overflow: hidden;
}
/* entrance animation on first load only — re-renders must not bounce */
.category.rise-in {
  opacity: 0;
  transform: translateY(14px);
  animation: rise 0.55s cubic-bezier(0.2, 0.7, 0.2, 1) forwards;
}
.category.rise-in:nth-child(1) { animation-delay: 0.05s; }
.category.rise-in:nth-child(2) { animation-delay: 0.13s; }
.category.rise-in:nth-child(3) { animation-delay: 0.21s; }
.category.rise-in:nth-child(4) { animation-delay: 0.29s; }
@keyframes rise { to { opacity: 1; transform: none; } }

.cat-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 16px 10px;
}
.cat-icon {
  width: 34px; height: 34px;
  border-radius: 11px;
  display: grid; place-items: center;
  color: var(--cat-color);
  background: color-mix(in srgb, var(--cat-color) 14%, transparent);
}
.cat-icon svg { width: 19px; height: 19px; }
.cat-name {
  font-family: "Fraunces", serif;
  font-weight: 600;
  font-size: 1.22rem;
  letter-spacing: 0.01em;
  flex: 1;
}
.cat-avg { display: flex; align-items: center; gap: 6px; }
.cat-avg .avg-label { font-size: 0.68rem; color: var(--text-faint); text-transform: uppercase; letter-spacing: 0.07em; }

.habits { display: flex; flex-direction: column; }

.habit {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 11px 16px;
  border-top: 1px solid rgba(255, 196, 148, 0.055);
}

.check {
  width: 34px; height: 34px;
  min-width: 34px;
  border-radius: 50%;
  border: 2px solid color-mix(in srgb, var(--cat-color) 55%, transparent);
  background: transparent;
  color: transparent;
  display: grid; place-items: center;
  cursor: pointer;
  transition: background 0.22s, border-color 0.22s, transform 0.12s;
}
.check svg { width: 17px; height: 17px; }
.check:active { transform: scale(0.88); }
.check:focus-visible { outline: 2px solid var(--ember); outline-offset: 2px; }
.check.done {
  background: var(--cat-color);
  border-color: var(--cat-color);
  color: #17100c;
  animation: pop 0.32s cubic-bezier(0.2, 1.6, 0.4, 1);
}
@keyframes pop { 0% { transform: scale(0.7); } 100% { transform: scale(1); } }

.habit-name { flex: 1; font-size: 0.98rem; font-weight: 500; }
.habit.done-row .habit-name { color: var(--text-dim); }

.del-btn {
  display: none;
  background: none;
  border: none;
  color: #e2604f;
  font-size: 1.2rem;
  width: 34px; height: 34px;
  border-radius: 10px;
  cursor: pointer;
}
body.editing .del-btn { display: grid; place-items: center; }
body.editing .check, body.editing .chip, body.editing .habit-name { display: none; }

/* rename (edit mode only) */
.rename-input {
  display: none;
  flex: 1;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  color: var(--text);
  font-family: "Outfit", sans-serif;
  font-size: 0.95rem;
  padding: 7px 10px;
  min-width: 0;
}
.rename-input:focus { outline: none; border-color: var(--line-strong); }
body.editing .rename-input { display: block; }

/* avoid-habit marker */
.anti-tag {
  font-size: 0.62rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: #d98a7a;
  border: 1px solid rgba(217, 138, 122, 0.35);
  border-radius: 5px;
  padding: 1px 5px;
  margin-left: 6px;
  vertical-align: 1px;
}
.check.anti { border-style: dashed; }
.check.anti.done { border-style: solid; }

/* streak chip */
.chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 9px 3px 6px;
  border-radius: 999px;
  font-family: "Fraunces", serif;
  font-weight: 600;
  font-size: 0.88rem;
  line-height: 1.3;
  color: var(--tier-fg, var(--text-faint));
  background: var(--tier-bg, rgba(255,255,255,0.04));
  box-shadow: var(--tier-glow, none);
  transition: box-shadow 0.4s;
}
.chip svg { width: 13px; height: 13px; }
.chip.t0 { opacity: 0.55; }
.chip.flare { animation: flare 0.7s ease-out; }
@keyframes flare {
  0% { transform: scale(1); }
  25% { transform: scale(1.18); filter: brightness(1.6); }
  100% { transform: scale(1); }
}

/* add habit (edit mode only) */
.add-row {
  display: none;
  align-items: center;
  gap: 10px;
  padding: 10px 16px 13px;
  border-top: 1px solid rgba(255, 196, 148, 0.055);
}
body.editing .add-row { display: flex; }

.anti-toggle, .anti-flip {
  width: 34px; height: 34px;
  min-width: 34px;
  border-radius: 50%;
  border: 1.6px dashed rgba(217, 138, 122, 0.5);
  background: none;
  color: rgba(217, 138, 122, 0.75);
  display: grid; place-items: center;
  cursor: pointer;
}
.anti-toggle svg, .anti-flip svg { width: 16px; height: 16px; }
.anti-toggle.armed, .anti-flip.armed {
  border-style: solid;
  background: rgba(217, 138, 122, 0.14);
  color: #f0a08c;
}
.anti-toggle:focus-visible, .anti-flip:focus-visible { outline: 2px solid var(--ember); outline-offset: 2px; }

/* per-habit avoid flip lives in edit mode only */
.anti-flip { display: none; }
body.editing .anti-flip { display: grid; }

/* reorder controls (edit mode only) */
.move-col { display: none; flex-direction: column; gap: 3px; }
body.editing .move-col { display: flex; }
.move-btn {
  width: 28px; height: 17px;
  border: none;
  border-radius: 5px;
  background: var(--surface-2);
  color: var(--text-dim);
  font-size: 0.62rem;
  line-height: 1;
  cursor: pointer;
  display: grid; place-items: center;
}
.move-btn:active { background: var(--line-strong); color: var(--text); }
.move-btn:disabled { opacity: 0.25; cursor: default; }
.move-btn:focus-visible { outline: 2px solid var(--ember); outline-offset: 1px; }
.add-btn {
  width: 34px; height: 34px;
  border-radius: 50%;
  border: 1.6px dashed color-mix(in srgb, var(--cat-color) 45%, transparent);
  background: none;
  color: color-mix(in srgb, var(--cat-color) 80%, white 10%);
  display: grid; place-items: center;
  cursor: pointer;
}
.add-btn svg { width: 15px; height: 15px; }
.add-btn:focus-visible { outline: 2px solid var(--ember); outline-offset: 2px; }
.add-input {
  flex: 1;
  background: none;
  border: none;
  border-bottom: 1px solid transparent;
  color: var(--text);
  font-family: "Outfit", sans-serif;
  font-size: 0.95rem;
  padding: 4px 0;
}
.add-input::placeholder { color: var(--text-faint); }
.add-input:focus { outline: none; border-bottom-color: var(--line-strong); }

/* ————— Footer ————— */
.app-footer { margin-top: 26px; text-align: center; color: var(--text-faint); font-size: 0.78rem; }
.link-btn {
  background: none; border: none;
  color: var(--text-dim);
  text-decoration: underline;
  font-size: 0.78rem;
  cursor: pointer;
  margin-top: 4px;
  font-family: inherit;
}
.footer-sep { color: var(--text-faint); }
/* Reserves its line so a status message does not shift the footer. 1.45em, not 1.1:
   the reserve has to match the body line-height or a one-line message still moves
   everything below it by a few pixels. Measured, not estimated. */
.footer-status { min-height: 1.45em; margin-top: 6px; color: var(--text-dim); }

/* ————— Sheets ————— */
.sheet-scrim {
  position: fixed; inset: 0;
  background: rgba(5, 3, 2, 0.62);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
  z-index: 40;
  animation: fade 0.25s ease;
}
@keyframes fade { from { opacity: 0; } }

.sheet {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  max-width: 560px;
  margin: 0 auto;
  background: var(--surface-2);
  border: 1px solid var(--line-strong);
  border-bottom: none;
  border-radius: 22px 22px 0 0;
  padding: 10px 18px calc(env(safe-area-inset-bottom) + 22px);
  z-index: 50;
  max-height: 82vh;
  overflow-y: auto;
  animation: slide-up 0.34s cubic-bezier(0.2, 0.9, 0.25, 1);
}
@keyframes slide-up { from { transform: translateY(60%); opacity: 0.4; } }

.sheet-handle {
  width: 38px; height: 4px;
  border-radius: 2px;
  background: var(--line-strong);
  margin: 4px auto 14px;
}
.sheet-title { font-family: "Fraunces", serif; font-size: 1.3rem; font-weight: 600; }
.sheet-sub { color: var(--text-dim); font-size: 0.85rem; margin: 4px 0 14px; }

/* calendar */
.cal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.cal-title { font-family: "Fraunces", serif; font-size: 1.15rem; font-weight: 600; }
.cal-dow, .cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; }
.cal-dow span { text-align: center; font-size: 0.68rem; color: var(--text-faint); text-transform: uppercase; letter-spacing: 0.05em; padding: 4px 0; }

.cal-cell {
  aspect-ratio: 1;
  border: none;
  border-radius: 12px;
  background: none;
  color: var(--text-dim);
  font-family: "Outfit", sans-serif;
  font-size: 0.9rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  cursor: pointer;
  position: relative;
}
.cal-cell:disabled { opacity: 0.22; cursor: default; }
.cal-cell.today { color: var(--ember); font-weight: 600; }
.cal-cell.selected { background: color-mix(in srgb, var(--ember) 18%, transparent); color: var(--text); }
.cal-cell:focus-visible { outline: 2px solid var(--ember); outline-offset: 1px; }
.cal-heat {
  width: 14px; height: 3.5px;
  border-radius: 2px;
  background: var(--heat, transparent);
}

.cal-day-detail { margin-top: 16px; border-top: 1px solid var(--line); padding-top: 14px; }
.cal-day-detail h3 { font-family: "Fraunces", serif; font-size: 1.05rem; font-weight: 600; margin-bottom: 8px; }
.cal-day-detail .habit { padding: 9px 2px; }
.cal-day-detail .cat-tag {
  font-size: 0.66rem;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--cat-color);
  min-width: 64px;
}

/* legend */
.legend-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 9px 0;
  border-top: 1px solid rgba(255, 196, 148, 0.055);
}
.legend-row:first-child { border-top: none; }
.legend-days { min-width: 74px; color: var(--text-dim); font-size: 0.85rem; }
.legend-name { flex: 1; font-weight: 500; }

@media (min-width: 480px) {
  :root { --pad: 24px; }
}

/* ————————————————————————————————————————————————————————————————————————
   Everything above this line is the accepted mockup stylesheet, unchanged.
   Everything below it is the account app the mockup did not have: a sign-in
   screen, a footer that names who is signed in, the timezone control, and the
   failure toast. Built from the same tokens so it reads as one app.
   ———————————————————————————————————————————————————————————————————————— */

/* The UA stylesheet's `[hidden] { display: none }` sits at the LOWEST specificity,
   so ANY author rule setting `display` on the same element silently defeats it —
   `.auth { display: flex }` and `.field { display: flex }` below would both do it,
   leaving the sign-up-only invite field on screen during sign-in. Everything this
   app hides, it hides with the attribute, so the attribute wins outright. */
[hidden] { display: none !important; }

/* ————— Sign in / sign up ————— */
.auth {
  min-height: 78vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  text-align: center;
  position: relative;
  z-index: 1;
}
.auth-mark {
  width: 54px; height: 54px;
  display: grid; place-items: center;
  border-radius: 50%;
  background: radial-gradient(circle at 50% 60%, rgba(255, 138, 61, 0.22), transparent 70%);
  margin-bottom: 2px;
}
.auth-mark svg { width: 30px; height: 30px; fill: var(--ember); }
.auth-tagline { color: var(--text-dim); font-size: 0.9rem; margin-bottom: 20px; }

.auth-form {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 12px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: var(--pad);
}
.field { display: flex; flex-direction: column; gap: 5px; text-align: left; }
.field-label { color: var(--text-dim); font-size: 0.78rem; letter-spacing: 0.02em; }
.field input, .tz-select {
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 12px;
  color: var(--text);
  font-family: inherit;
  /* 16px exactly. Anything smaller and iOS Safari zooms the whole page on focus,
     which on a phone leaves the user typing into a viewport they then have to
     pinch back out of. */
  font-size: 1rem;
  padding: 11px 12px;
  width: 100%;
}
.field input:focus-visible, .tz-select:focus-visible {
  outline: none;
  border-color: var(--line-strong);
  box-shadow: 0 0 0 3px rgba(255, 138, 61, 0.16);
}
.auth-submit {
  background: linear-gradient(135deg, #ff9a52, var(--ember));
  border: none;
  border-radius: 12px;
  color: #2a1508;
  cursor: pointer;
  font-family: inherit;
  font-size: 0.95rem;
  font-weight: 600;
  padding: 12px;
  margin-top: 2px;
}
.auth-submit[disabled] { opacity: 0.55; cursor: default; }
/* Reserves its line so an error appearing or clearing never moves the submit button
   out from under a thumb already travelling toward it. 1.45em, not 1.1: the reserve
   has to match the body line-height or a one-line message still shifts everything
   below it by a few pixels. Measured in the browser, not estimated. */
.auth-error { color: #ff8f8f; font-size: 0.8rem; min-height: 1.45em; text-align: left; }

/* ————— Footer: account and timezone ————— */
.footer-account { margin-top: 10px; }
.footer-account b { color: var(--text-dim); font-weight: 600; }
.tz-row {
  display: none; /* edit mode only — it is managed with the habits, not on every screen */
  align-items: center;
  gap: 10px;
  justify-content: center;
  margin-bottom: 12px;
}
body.editing .tz-row { display: flex; }
.tz-select { width: auto; max-width: 250px; padding: 7px 10px; font-size: 0.85rem; }

/* ————— Signed out: the app is not merely hidden, it is not there ————— */
body.signed-out .app-header,
body.signed-out .categories,
body.signed-out .app-footer { display: none; }

/* ————— Failure toast (KTD6) —————
   Fixed and above the sheets (z-index 50), because the mutation that rolled back may
   well have been made from inside the calendar. */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom) + 18px);
  transform: translateX(-50%);
  z-index: 60;
  width: max-content;
  max-width: min(520px, calc(100vw - 32px));
  background: var(--surface-2);
  border: 1px solid rgba(226, 96, 79, 0.45);
  border-radius: 12px;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.55);
  color: var(--text);
  font-size: 0.85rem;
  padding: 10px 14px;
  text-align: center;
  animation: toast-in 0.22s ease-out;
}
@keyframes toast-in { from { opacity: 0; transform: translate(-50%, 10px); } }

/* ————— Drag reordering, edit mode only (KTD7, R2) —————
   The grip is the only place a drag can start, so a vertical swipe anywhere else on a
   row still scrolls the page — which is the gesture people make most on a phone. The
   up/down buttons beside it stay: they are the keyboard and assistive-tech route to the
   same reorder, not a legacy control. */
.drag-handle {
  display: none;
  width: 26px; min-width: 26px; height: 34px;
  align-items: center;
  justify-content: center;
  color: var(--text-faint);
  cursor: grab;
  /* The one element on the page where a vertical drag must NOT scroll: without this,
     iOS takes the gesture for the page and the row never moves. */
  touch-action: none;
}
body.editing .drag-handle { display: flex; }
.drag-handle svg { width: 18px; height: 18px; }
.drag-handle:active { cursor: grabbing; color: var(--text-dim); }

/* Sortable's three states, named in app.js rather than left as its defaults so the
   class names say what they are for. */
.habit.drag-chosen { background: var(--surface-2); }
.habit.drag-ghost { opacity: 0.3; }
.habit.drag-active { box-shadow: 0 12px 26px rgba(0, 0, 0, 0.55); }

/* ————— Offline banner (KTD6, U8) —————
   Pinned to the top rather than placed in the flow: it appears and disappears while the
   app is being used, and anything in the flow would shove the whole page down under a
   thumb that was already moving. Above the sheets, because losing the network while the
   calendar is open is exactly when it matters. */
.offline-banner {
  position: fixed;
  top: 0; left: 0; right: 0;
  z-index: 70;
  padding: calc(env(safe-area-inset-top) + 7px) 14px 7px;
  background: rgba(226, 96, 79, 0.16);
  border-bottom: 1px solid rgba(226, 96, 79, 0.4);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  color: #ffd6cd;
  font-size: 0.78rem;
  text-align: center;
  animation: banner-in 0.2s ease-out;
}
@keyframes banner-in { from { opacity: 0; transform: translateY(-100%); } }
