:root {
  color-scheme: light;
  /* Density is one set of tokens, not a per-rule sweep. Compact is the default:
     a phone should show as much of the list as it can while every touch target
     stays at or above 48px. */
  --row-min: 3.05rem;
  --row-text: 0.98rem;
  --row-gap: 0.32rem;
  --row-radius: 0.8rem;
  --row-pad-y: 0.5rem;
  --check-size: 1.5rem;
  --row-lead: 1.32;
  --paper: #f7f5ef;
  --surface: #fff;
  --ink: #20231f;
  --muted: #666a61;
  --line: #d8d9d0;
  --green: #2e6b4f;
  --green-dark: #23533e;
  --green-soft: #e7f0e9;
  --danger: #9d382f;
  --panel: rgba(255, 255, 255, 0.88);
  --shadow: 0 16px 42px rgba(45, 49, 42, 0.09);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

:root[data-density="normal"] {
  --row-min: 3.55rem;
  --row-text: 1.04rem;
  --row-gap: 0.42rem;
  --row-radius: 0.95rem;
  --row-pad-y: 0.78rem;
  --check-size: 1.7rem;
  --row-lead: 1.4;
}
:root[data-density="expanded"] {
  --row-min: 4.05rem;
  --row-text: 1.12rem;
  --row-gap: 0.55rem;
  --row-radius: 1.05rem;
  --row-pad-y: 0.95rem;
  --check-size: 1.85rem;
  --row-lead: 1.5;
}

* { box-sizing: border-box; }
html { min-height: 100%; background: var(--paper); }
body {
  min-width: 0;
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0;
  color: var(--ink);
  background: radial-gradient(circle at 12% 0%, rgba(170, 203, 174, 0.21), transparent 32rem), var(--paper);
}
button, input { font: inherit; }
button { touch-action: manipulation; }
button:focus-visible, input:focus-visible { outline: 3px solid rgba(46, 107, 79, 0.3); outline-offset: 2px; }
button:disabled { cursor: wait; opacity: 0.58; }
[hidden] { display: none !important; }
h1, h2, p { margin-top: 0; }

.shell {
  width: min(100%, 42rem);
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0 auto;
  padding: max(1rem, env(safe-area-inset-top)) max(0.75rem, env(safe-area-inset-right)) calc(6.5rem + env(safe-area-inset-bottom) + var(--keyboard-inset, 0px)) max(0.75rem, env(safe-area-inset-left));
}

/* Anything scrolled into view must clear the fixed composer and the keyboard,
   or "scrolled into view" still means "hidden behind the add bar". */
html {
  scroll-padding-bottom: calc(7rem + env(safe-area-inset-bottom) + var(--keyboard-inset, 0px));
}

/* While typing, the list is what matters — give the rows the vertical space the
   masthead was using. */
body.keyboard-open .eyebrow,
body.keyboard-open .header-actions,
body.keyboard-open .empty-icon { display: none; }
body.keyboard-open .app-header { margin-bottom: 0.6rem; }
body.keyboard-open h1 { font-size: 1.4rem; }

.eyebrow {
  margin: 0 0 0.18rem;
  color: var(--green);
  font-size: 0.72rem;
  font-weight: 780;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

h1 { margin: 0; font-size: clamp(1.85rem, 7vw, 2.35rem); line-height: 1; letter-spacing: -0.045em; }
.login-view { width: min(100%, 25rem); margin: clamp(4rem, 16vh, 9rem) auto 0; text-align: center; }
.mark {
  display: block;
  width: 4rem;
  height: 4rem;
  margin: 0 auto 1.4rem;
  border: 1px solid rgba(46, 107, 79, 0.2);
  border-radius: 1.3rem;
  object-fit: cover;
  background: #788a68;
  box-shadow: 0 12px 28px rgba(46, 107, 79, 0.2);
}
.login-copy { margin: 0.8rem 0 2rem; color: var(--muted); line-height: 1.5; }
.login-form { text-align: left; }
.login-form label { display: block; margin-bottom: 0.5rem; font-size: 0.86rem; font-weight: 700; }
.login-form input {
  width: 100%;
  height: 3.5rem;
  padding: 0 1rem;
  border: 1px solid var(--line);
  border-radius: 0.9rem;
  color: var(--ink);
  background: var(--surface);
  box-shadow: var(--shadow);
  font-size: 1.05rem;
}

.primary-button, .secondary-button, .danger-button {
  min-height: 3.25rem;
  border: 0;
  border-radius: 0.9rem;
  padding: 0.75rem 1rem;
  font-weight: 740;
}
.primary-button { width: 100%; margin-top: 0.8rem; color: #fff; background: var(--green); }
.primary-button:hover { background: var(--green-dark); }
.form-error { margin: 0.65rem 0 0; color: var(--danger); font-size: 0.88rem; }

.app-header {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 0.75rem;
  padding: 0.2rem 0 0.9rem;
}
.header-meta { display: flex; align-items: center; gap: 0.65rem; margin-top: 0.5rem; color: var(--muted); font-size: 0.82rem; font-weight: 680; }
.connection-status { color: var(--muted); }
.connection-status::before {
  display: inline-block;
  width: 0.46rem;
  height: 0.46rem;
  margin-right: 0.34rem;
  border-radius: 50%;
  background: #b48731;
  content: "";
  vertical-align: 0.06rem;
}
.connection-status.live::before { background: #3f8c5d; }
.connection-status.offline { color: #8e3d34; }
.connection-status.offline::before { background: #b34e44; }
.header-actions { display: flex; align-items: center; gap: 0.15rem; }
.text-button {
  min-width: 3.6rem;
  min-height: 2.75rem;
  border: 1px solid transparent;
  border-radius: 0.8rem;
  padding: 0.5rem 0.7rem;
  color: var(--green);
  background: transparent;
  font-size: 0.88rem;
  font-weight: 740;
}
.text-button:active { background: var(--green-soft); }
.menu-wrap { position: relative; }
.menu-trigger {
  min-width: 2.75rem;
  padding: 0.5rem 0.35rem;
  font-size: 0.8rem;
  font-weight: 900;
  letter-spacing: -0.12em;
}
.app-menu {
  position: absolute;
  top: calc(100% + 0.3rem);
  right: 0;
  z-index: 12;
  display: grid;
  min-width: 13.5rem;
  padding: 0.3rem;
  border: 1px solid var(--line);
  border-radius: 0.9rem;
  background: var(--surface);
  box-shadow: 0 16px 40px rgba(32, 35, 31, 0.2);
}
.menu-item {
  min-height: 2.85rem;
  border: 0;
  border-radius: 0.65rem;
  padding: 0 0.75rem;
  color: var(--ink);
  background: transparent;
  font-size: 0.95rem;
  text-align: left;
}
.menu-item:active { background: var(--green-soft); }
.menu-item.danger { color: var(--danger); }
.menu-item:disabled { color: #9aa093; cursor: default; opacity: 1; }
.theme-button {
  display: grid;
  width: 2.75rem;
  height: 2.75rem;
  flex: 0 0 auto;
  border: 1px solid transparent;
  border-radius: 0.8rem;
  place-items: center;
  color: var(--green);
  background: transparent;
  font-size: 1.12rem;
}
.theme-button:active { background: var(--green-soft); }
.login-theme-button {
  position: fixed;
  top: max(0.7rem, env(safe-area-inset-top));
  right: max(0.7rem, env(safe-area-inset-right));
  z-index: 10;
}
body.signed-in .login-theme-button { display: none; }

.items { position: relative; display: grid; gap: var(--row-gap); margin: 0; padding: 0; list-style: none; }
/* Neither view until we know which one is right — see setSignedIn. */
body.booting .login-view,
body.booting .app-view,
body.booting .login-theme-button { visibility: hidden; }

.install-banner {
  display: grid;
  grid-template-columns: 2.6rem minmax(0, 1fr) auto auto;
  align-items: center;
  gap: 0.7rem;
  margin-bottom: 0.85rem;
  padding: 0.7rem 0.55rem 0.7rem 0.7rem;
  border: 1px solid rgba(46, 107, 79, 0.24);
  border-radius: 1rem;
  background: var(--green-soft);
}
.install-mark { width: 2.6rem; height: 2.6rem; border-radius: 0.75rem; object-fit: cover; }
.install-copy { display: grid; gap: 0.12rem; min-width: 0; }
.install-copy strong { font-size: 0.94rem; letter-spacing: -0.01em; }
.install-copy span { color: var(--muted); font-size: 0.8rem; line-height: 1.3; }
.install-button {
  min-height: 2.5rem;
  border: 0;
  border-radius: 0.7rem;
  padding: 0 0.9rem;
  color: #fff;
  background: var(--green);
  font-size: 0.9rem;
  font-weight: 780;
}
.install-dismiss {
  width: 2rem;
  min-height: 2.5rem;
  border: 0;
  color: var(--muted);
  background: transparent;
  font-size: 0.85rem;
}
.dialog-actions-single { grid-template-columns: 1fr; }
body.keyboard-open .install-banner { display: none; }

.done-divider {
  display: flex;
  align-items: center;
  gap: 0.7rem;
  margin: 1.15rem 0 0.6rem;
  color: #6f736a;
  font-size: 0.82rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}
.done-divider::after {
  flex: 1;
  height: 1px;
  background: var(--line);
  content: "";
}
.done-items { margin-bottom: 0.2rem; }

.item-row {
  position: relative;
  display: grid;
  /* Everything except the grab handle scrolls the page. Reordering is opt-in. */
  touch-action: pan-y;
  grid-template-columns: minmax(0, 1fr) 2.9rem 3rem;
  min-height: var(--row-min);
  border: 1px solid var(--line);
  border-radius: var(--row-radius);
  background: rgba(255, 255, 255, 0.9);
  transition: background 160ms ease, border-color 160ms ease, transform 120ms ease;
}
/* Deliberately not a transform. A transform on :active makes the row a stacking
   context the moment a finger lands, which drops the open row menu behind the
   next row — the tap then hits that row's drag handle and Remove never fires. */
.item-row:active { border-color: #b9beb2; }
/* The row with an open menu paints above its siblings, whatever else is going on. */
.item-row.menu-open { z-index: 30; }
.item-toggle {
  display: grid;
  grid-template-columns: calc(var(--check-size) + 0.55rem) minmax(0, 1fr);
  min-width: 0;
  min-height: var(--row-min);
  align-items: center;
  border: 0;
  border-radius: var(--row-radius) 0 0 var(--row-radius);
  padding: 0 0 0 0.65rem;
  color: var(--ink);
  background: transparent;
  text-align: left;
}
.check-circle {
  display: grid;
  width: var(--check-size);
  height: var(--check-size);
  border: 2px solid #777d72;
  border-radius: 50%;
  place-items: center;
  color: transparent;
  font-size: 0.9rem;
  font-weight: 900;
  transition: transform 150ms ease, background 150ms ease, color 150ms ease;
}
.item-text { min-width: 0; padding: var(--row-pad-y) 0.5rem var(--row-pad-y) 0; overflow-wrap: anywhere; font-size: var(--row-text); line-height: var(--row-lead); }
.item-row[data-done="true"] { border-color: transparent; background: rgba(232, 233, 227, 0.72); }
.item-row[data-done="true"] .check-circle { border-color: var(--green); color: #fff; background: var(--green); transform: scale(1.04); }
.item-text.done { color: #6f736a; text-decoration: line-through; text-decoration-thickness: 2px; }
/* The one place a vertical drag means "reorder" instead of "scroll", so it is
   drawn as a distinct grabbable pill and kept clear of its neighbours. */
.drag-handle {
  display: grid;
  width: 2.9rem;
  min-height: var(--row-min);
  border: 0;
  padding: 0;
  place-items: center;
  color: #8b9184;
  background: transparent;
  cursor: grab;
  touch-action: none;
}
.drag-handle span {
  display: grid;
  width: 1.65rem;
  height: 2.1rem;
  border-radius: 0.55rem;
  place-items: center;
  background: rgba(32, 35, 31, 0.05);
  font-size: 0.95rem;
  line-height: 1;
}
.drag-handle:active span { color: var(--green); background: var(--green-soft); }
.drag-handle[hidden] { display: none; }
.item-row[data-done="true"] { grid-template-columns: minmax(0, 1fr) 3rem; }

/* The dragged row is positioned by the pointer, so it must not animate toward
   the finger; its neighbours do animate as they open a gap for it. */
.item-row.dragging {
  z-index: 5;
  border-color: var(--green);
  box-shadow: 0 14px 32px rgba(32, 35, 31, 0.22);
  transition: none;
}
.item-row.dragging:active { transform: none; }
body.dragging-row { user-select: none; }
body.dragging-row .item-row:not(.dragging) { transition: transform 160ms ease; }

.more-button {
  width: 3rem;
  min-height: var(--row-min);
  border: 0;
  border-radius: 0 var(--row-radius) var(--row-radius) 0;
  color: #686c64;
  background: transparent;
  font-size: 0.78rem;
  font-weight: 900;
  letter-spacing: -0.12em;
}
.more-button[aria-expanded="true"] { color: var(--danger); background: #f8e9e5; }
.row-actions {
  position: absolute;
  top: calc(100% - 0.2rem);
  right: 0.35rem;
  /* Above every sibling row, not just the next one. Pairs with .menu-open on the
     row itself — the popover overhangs the row below, so both halves matter. */
  z-index: 30;
  padding: 0.35rem;
  border: 1px solid var(--line);
  border-radius: 0.8rem;
  background: var(--surface);
  box-shadow: 0 12px 32px rgba(32, 35, 31, 0.18);
}
.remove-button { min-width: 7rem; min-height: 2.9rem; border: 0; border-radius: 0.6rem; color: #fff; background: var(--danger); font-weight: 750; }

.empty-state { padding: clamp(4.5rem, 16vh, 7rem) 1rem 2rem; text-align: center; }
.empty-icon { display: grid; width: 3.1rem; height: 3.1rem; margin: 0 auto 1rem; border-radius: 50%; place-items: center; color: var(--green); background: var(--green-soft); font-size: 1.3rem; font-weight: 850; }
.empty-state h2 { margin-bottom: 0.4rem; font-size: 1.2rem; }
.empty-state p { color: var(--muted); font-size: 0.94rem; }

.add-form {
  position: fixed;
  right: max(0.65rem, env(safe-area-inset-right));
  bottom: calc(max(0.55rem, env(safe-area-inset-bottom)) + var(--keyboard-inset, 0px));
  left: max(0.65rem, env(safe-area-inset-left));
  z-index: 8;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  width: min(calc(100% - 1.3rem), 40.5rem);
  margin: 0 auto;
  gap: 0.45rem;
  padding: 0.42rem;
  border: 1px solid rgba(46, 107, 79, 0.2);
  border-radius: 1.15rem;
  background: rgba(255, 255, 255, 0.96);
  box-shadow: 0 14px 44px rgba(32, 35, 31, 0.18);
  backdrop-filter: blur(12px);
}
.add-form input { min-width: 0; height: 3.2rem; border: 0; padding: 0 0.8rem; color: var(--ink); background: transparent; font-size: 1.05rem; }
.add-form input::placeholder { color: #73776f; }
.add-form input:focus { outline: none; }
.add-button { min-width: 4.6rem; min-height: 3.2rem; border: 0; border-radius: 0.8rem; padding: 0 1rem; color: #fff; background: var(--green); font-weight: 780; }

.reset-dialog { width: min(calc(100% - 2rem), 25rem); border: 0; border-radius: 1.25rem; padding: 1.4rem; color: var(--ink); background: var(--surface); box-shadow: 0 24px 70px rgba(32, 35, 31, 0.24); }
.reset-dialog::backdrop { background: rgba(28, 31, 27, 0.43); backdrop-filter: blur(2px); }
.reset-dialog h2 { margin-bottom: 0.4rem; font-size: 1.35rem; }
.reset-dialog p { margin-bottom: 1.4rem; color: var(--muted); line-height: 1.45; }
.dialog-mark { display: grid; width: 2.8rem; height: 2.8rem; margin-bottom: 1rem; border-radius: 50%; place-items: center; color: var(--danger); background: #f8e9e5; font-size: 1.3rem; }
.dialog-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 0.6rem; }
.secondary-button { color: var(--ink); background: #eeeee9; }
.danger-button { color: #fff; background: var(--danger); }

.toast {
  position: fixed;
  right: 1rem;
  bottom: calc(5.7rem + env(safe-area-inset-bottom) + var(--keyboard-inset, 0px));
  left: 1rem;
  z-index: 20;
  display: flex;
  width: fit-content;
  max-width: calc(100% - 2rem);
  min-height: 3rem;
  margin: auto;
  align-items: center;
  gap: 0.85rem;
  border-radius: 999px;
  padding: 0.65rem 0.72rem 0.65rem 1rem;
  color: #fff;
  background: #30332e;
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.2);
  font-size: 0.88rem;
}
.toast-action { min-height: 2.2rem; border: 0; border-radius: 999px; padding: 0 0.8rem; color: #17251d; background: #dcebdd; font-weight: 800; }
.sr-only { position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }

@media (min-width: 44rem) {
  .shell { padding-top: 2rem; padding-bottom: 2rem; }
  .app-view { padding: 1rem 1.25rem 2rem; border: 1px solid var(--line); border-radius: 1.5rem; background: var(--panel); }
  .add-form {
    position: static;
    width: 100%;
    margin-top: 1rem;
    background: var(--surface);
  }
  .toast { bottom: 1.25rem; }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --paper: #171a17;
  --surface: #252924;
  --ink: #f2f3ed;
  --muted: #b8bcb2;
  --line: #41463e;
  --green: #79bd92;
  --green-dark: #65a77d;
  --green-soft: #283d30;
  --danger: #dc786d;
  --panel: rgba(29, 33, 29, 0.94);
}
:root[data-theme="dark"] body { background: radial-gradient(circle at 12% 0%, rgba(65, 113, 79, 0.24), transparent 30rem), var(--paper); }
:root[data-theme="dark"] .item-row { background: rgba(37, 41, 36, 0.94); }
:root[data-theme="dark"] .item-row[data-done="true"] { background: rgba(49, 54, 47, 0.88); }
:root[data-theme="dark"] .add-form { background: rgba(37, 41, 36, 0.98); }
:root[data-theme="dark"] .secondary-button { background: #3a3f38; }
:root[data-theme="dark"] .more-button[aria-expanded="true"] { background: #4b2f2d; }
:root[data-theme="dark"] .done-divider { color: #9aa093; }
:root[data-theme="dark"] .item-row.dragging { box-shadow: 0 14px 32px rgba(0, 0, 0, 0.45); }
:root[data-theme="dark"] .install-banner { border-color: rgba(120, 176, 143, 0.28); background: rgba(46, 107, 79, 0.22); }
:root[data-theme="dark"] .drag-handle span { background: rgba(255, 255, 255, 0.07); }
:root[data-theme="dark"] .drag-handle:active span { background: rgba(46, 107, 79, 0.4); }
:root[data-theme="dark"] .app-menu { border-color: #3c423a; background: #252924; }
:root[data-theme="dark"] .menu-item:active { background: #33392f; }

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

.menu-density { padding: 0.45rem 0.75rem 0.35rem; border-top: 1px solid var(--line); margin-top: 0.25rem; }
.menu-density-label { display: block; margin-bottom: 0.35rem; color: var(--muted); font-size: 0.78rem; font-weight: 700; }
.density-options { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0.2rem; }
.density-option {
  min-height: 2.35rem;
  border: 1px solid var(--line);
  border-radius: 0.55rem;
  color: var(--ink);
  background: transparent;
  font-size: 0.82rem;
  font-weight: 640;
}
.density-option[aria-pressed="true"] { border-color: var(--green); color: #fff; background: var(--green); }

:root[data-theme="dark"] .density-option { border-color: #3c423a; }
:root[data-theme="dark"] .density-option[aria-pressed="true"] { border-color: var(--green); }
:root[data-theme="dark"] .menu-density { border-top-color: #3c423a; }
.exit-layer { position: fixed; inset: 0; z-index: 15; pointer-events: none; }
.exit-layer .item-row { pointer-events: none; }
