:root {
  color-scheme: light dark;
  --bg: #f7f4ef;
  --surface: #ffffff;
  --text: #1f1d1a;
  --muted: #736c62;
  --line: #e6e0d6;
  --accent: #c2410c;
  --accent-ink: #ffffff;
  --accent-soft: #fbe6d8;
  --danger: #b42318;
  --act-edit: #857d71;
  --act-delete: #d92d20;
  --shadow: 0 1px 2px rgb(60 40 20 / 0.06), 0 4px 16px rgb(60 40 20 / 0.06);
  --gutter: 16px;
  font: 17px/1.35 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #151412;
    --surface: #201f1c;
    --text: #f1ede6;
    --muted: #a39b8f;
    --line: #33302b;
    --accent: #fb923c;
    --accent-ink: #1c1004;
    --accent-soft: #3b2412;
    --danger: #f97066;
    --act-edit: #5f584e;
    --shadow: 0 1px 2px rgb(0 0 0 / 0.4);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html { background: var(--bg); -webkit-text-size-adjust: 100%; }
body {
  margin: 0 auto;
  max-width: 560px;
  min-height: 100dvh;
  color: var(--text);
  background: var(--bg);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: contain;
}

button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; padding: 0; touch-action: manipulation; }
svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* --- header: where you are, and the add box -------------------------------- */

.top {
  position: sticky;
  top: 0;
  z-index: 2;
  padding: calc(env(safe-area-inset-top) + 6px) var(--gutter) 10px;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
}

.bar { display: flex; align-items: center; min-height: 44px; margin: 0 -10px; }
#title {
  flex: 1;
  min-width: 0;
  margin: 0;
  padding: 0 10px;
  font-size: 20px;
  font-weight: 650;
  line-height: 1.2;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
#app[data-screen="home"] #title { font-size: 28px; font-weight: 700; letter-spacing: -0.01em; }
#back:not([hidden]) + #title { padding-left: 2px; }

.icon-button {
  flex: none;
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border-radius: 22px;
  color: var(--muted);
}
#back { color: var(--accent); }

.add { display: flex; gap: 8px; margin-top: 6px; }
.add input {
  flex: 1;
  min-width: 0;
  height: 48px;
  padding: 0 16px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface);
  box-shadow: var(--shadow);
}
.add input::placeholder { color: var(--muted); }
.add input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.add button {
  flex: none;
  display: grid;
  place-items: center;
  width: 48px;
  height: 48px;
  border-radius: 14px;
  background: var(--accent);
  color: var(--accent-ink);
}

.suggestions { list-style: none; margin: 8px 0 0; padding: 0; }
.suggestions li { display: flex; align-items: stretch; gap: 8px; }
.suggestions li + li { border-top: 1px solid var(--line); }
.suggestion {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: baseline;
  gap: 8px;
  min-height: 46px;
  padding: 11px 2px;
  text-align: left;
}
.suggestion .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.suggestion .hint { flex: none; margin: 0; color: var(--muted); font-size: 14px; }
.suggestion:disabled { cursor: default; color: var(--muted); }
.status {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: 10px 0 0;
  padding: 7px 12px;
  border-radius: 10px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 14px;
}
/* A passing message lies over the title for a few seconds rather than pushing the rows
   down under someone's finger. Touches go through it, except to its own button. */
.status.passing {
  position: absolute;
  top: calc(env(safe-area-inset-top) + 6px);
  left: var(--gutter);
  right: var(--gutter);
  min-height: 44px;
  margin: 0;
  box-shadow: var(--shadow);
  pointer-events: none;
}
.status.passing button { pointer-events: auto; }
.status button { flex: none; min-height: 30px; padding: 0 4px; font-weight: 700; text-decoration: underline; color: inherit; }

/* --- rows: Edit and Delete behind, a handle to drag by --------------------- */

.swipe {
  position: relative;
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  touch-action: pan-y; /* up and down is the page's; sideways is the row's */
}
/* Hidden until the row is swiped, so no sliver of it shows under a row that is moving;
   it stays a moment after, while the front slides back over it. */
.behind {
  position: absolute;
  inset: 0;
  display: flex;
  justify-content: flex-end;
  pointer-events: none;
  visibility: hidden;
  transition: visibility 0s 0.2s;
}
.swipe.swiping .behind, .swipe.open .behind { visibility: visible; transition-delay: 0s; }
.swipe.open .behind { pointer-events: auto; }
.act { flex: none; display: grid; place-items: center; width: 76px; color: #fff; font-size: 15px; font-weight: 600; }
.act.edit { background: var(--act-edit); }
.act.delete { background: var(--act-delete); }
.swipe.full .act.edit { display: none; }
.swipe.full .act.delete { flex: 1; justify-items: end; padding-right: 22px; }
.front {
  position: relative;
  display: flex;
  align-items: stretch;
  background: var(--bg);
  touch-action: pan-y;
}
.card .front { background: var(--surface); }
.swipe.open .front { transform: translateX(-152px); }

.handle {
  flex: none;
  display: grid;
  place-items: center;
  width: 44px;
  color: var(--muted);
  opacity: 0.75;
  cursor: grab;
  touch-action: none;
}
.handle svg { width: 20px; height: 20px; }
.lifted { z-index: 1; overflow: visible; }
.lifted .behind { visibility: hidden; }
.lifted .front { border-radius: 10px; background: var(--surface); box-shadow: 0 8px 24px rgb(0 0 0 / 0.2); }
.entry.lifted .front { margin: 0 -10px; padding: 0 10px; }

/* --- the lists ------------------------------------------------------------- */

#home { padding: 16px var(--gutter) calc(env(safe-area-inset-bottom) + 48px); }
.card {
  margin: 0;
  padding: 0;
  list-style: none;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface);
  overflow: hidden;
}
.list-row + .list-row { border-top: 1px solid var(--line); }
.list-row .enter {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 56px;
  padding: 10px 14px;
  text-align: left;
}
.list-row .name { flex: 1; min-width: 0; font-weight: 550; overflow-wrap: anywhere; }
.list-row .count { flex: none; color: var(--muted); font-variant-numeric: tabular-nums; }
.list-row .handle { margin-left: -10px; }
#new-list { margin-top: 14px; }

/* --- the list -------------------------------------------------------------- */

.entries { padding: 6px var(--gutter) calc(env(safe-area-inset-bottom) + 48px); }
.entries ul { list-style: none; margin: 0; padding: 0; }

.entry { border-bottom: 1px solid var(--line); }
.entry .toggle {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 14px;
  min-height: 58px;
  padding: 9px 0;
  text-align: left;
}
.entry .box {
  flex: none;
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  border: 2px solid var(--muted);
  border-radius: 50%;
  color: transparent;
}
.entry .box svg { width: 16px; height: 16px; stroke-width: 3.2; }
.entry .words { min-width: 0; }
.entry .line { display: flex; align-items: baseline; gap: 8px; }
.entry .name { min-width: 0; overflow-wrap: anywhere; }
.entry .qty {
  flex: none;
  padding: 0 8px;
  border-radius: 10px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 14px;
  font-weight: 650;
  font-variant-numeric: tabular-nums;
}
.entry.checked .qty { background: none; padding: 0; color: var(--muted); }
.entry .note { display: block; color: var(--muted); font-size: 14px; overflow-wrap: anywhere; }
.entry .handle { margin-right: -12px; }
.tip { margin: 16px 0 0; color: var(--muted); font-size: 14px; text-align: center; }
.entry.pending .box { border-style: dashed; }

.entry.checked .box { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.entry.checked .name { color: var(--muted); text-decoration: line-through; }

.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 26px 0 0;
  padding-bottom: 4px;
  border-bottom: 1px solid var(--line);
}
.section-head h2 {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}
.text-button { min-height: 40px; padding: 0 2px; color: var(--accent); font-size: 15px; font-weight: 550; }

.empty { margin: 56px 0 0; color: var(--muted); text-align: center; }

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

.sheet {
  width: 100%;
  max-width: 560px;
  max-height: 86dvh;
  margin: auto auto 0;
  padding: 0;
  border: 0;
  border-radius: 20px 20px 0 0;
  background: var(--bg);
  color: var(--text);
  overflow-y: auto;
}
.sheet::backdrop { background: rgb(0 0 0 / 0.45); }

/* The head stays put while the sheet scrolls: a grabber, what this is, and the way out. */
.sheet-head {
  position: sticky;
  top: 0;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 24px var(--gutter) 12px;
  border-bottom: 1px solid var(--line);
  background: var(--bg);
  cursor: grab;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none; /* a drag here pulls the sheet; it does not scroll it */
}
.sheet-head::before {
  content: "";
  position: absolute;
  top: 8px;
  left: calc(50% - 18px);
  width: 36px;
  height: 5px;
  border-radius: 3px;
  background: color-mix(in srgb, var(--muted) 40%, transparent);
}
.sheet-head .titles { min-width: 0; }
.sheet h2 { margin: 0; font-size: 20px; line-height: 1.25; overflow-wrap: anywhere; }
.sheet .sub { margin: 2px 0 0; color: var(--muted); font-size: 14px; }
.sheet .close {
  flex: none;
  min-height: 36px;
  padding: 0 16px;
  border-radius: 18px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 15px;
  font-weight: 650;
}
.sheet-body { padding: 2px var(--gutter) calc(env(safe-area-inset-bottom) + 22px); }

.field.stepper-row { display: flex; align-items: center; justify-content: space-between; }
.stepper { display: flex; align-items: center; border: 1px solid var(--line); border-radius: 12px; background: var(--surface); }
.stepper button { width: 54px; height: 46px; color: var(--accent); font-size: 24px; line-height: 1; }
.stepper button:disabled { color: var(--muted); opacity: 0.4; cursor: default; }
.stepper output { min-width: 40px; color: var(--text); font-size: 17px; font-weight: 650; text-align: center; font-variant-numeric: tabular-nums; }

.segmented { display: flex; gap: 2px; margin-top: 6px; padding: 3px; border-radius: 12px; background: var(--line); }
.segmented button { flex: 1; min-height: 40px; border-radius: 9px; color: var(--muted); font-size: 15px; font-weight: 550; }
.segmented [aria-checked="true"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

.field { display: block; margin-top: 14px; color: var(--muted); font-size: 14px; }
.field input, .field select, .row input, .login input {
  display: block;
  width: 100%;
  height: 46px;
  margin-top: 5px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  font-size: 17px;
}

.chips { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 6px; }
.chip {
  min-height: 44px;
  padding: 0 18px;
  border: 1px solid var(--line);
  border-radius: 22px;
  background: var(--surface);
  color: var(--text);
  font-size: 16px;
  font-weight: 550;
}

.actions { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 22px; }
.actions button, .primary {
  flex: 1;
  min-height: 46px;
  padding: 0 16px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  font-weight: 550;
  white-space: nowrap;
}
.actions .danger { color: var(--danger); }
.primary { width: 100%; border-color: var(--accent); background: var(--accent); color: var(--accent-ink); }

.row { display: flex; align-items: center; gap: 8px; margin-top: 8px; }
.row input { flex: 1; min-width: 0; margin-top: 0; }
.row .icon-button svg { width: 20px; height: 20px; }
.row .grow { flex: 1; }
.row .text-button { padding: 0 6px; }

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

/* --- no lists yet ---------------------------------------------------------- */

.first-list { max-width: 372px; margin: 12dvh auto 0; padding: 0 var(--gutter); }
.first-list h2 { margin: 0 0 8px; font-size: 24px; }
.first-list p { margin: 0 0 18px; color: var(--muted); }
.first-list input {
  display: block;
  width: 100%;
  height: 48px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
}
.first-list .primary { margin-top: 14px; }

/* --- sign in --------------------------------------------------------------- */

.login { padding: calc(env(safe-area-inset-top) + 15dvh) 28px 40px; text-align: center; }
.login h1 { margin: 16px 0 28px; font-size: 26px; }
.login img { border-radius: 16px; }
.login form { max-width: 340px; margin: 0 auto; text-align: left; }
.login label { display: block; margin-top: 14px; color: var(--muted); font-size: 14px; }
.login .primary { margin-top: 22px; max-width: 340px; }
.login .text-button { display: block; margin: 14px auto 0; }
.login .lead { max-width: 340px; margin: -12px auto 8px; color: var(--muted); }
.login .error { max-width: 340px; margin-left: auto; margin-right: auto; }

/* --- settings: one card per subject ---------------------------------------- */

.settings { padding: 2px var(--gutter) calc(env(safe-area-inset-bottom) + 48px); }
.group h2 {
  margin: 22px 4px 8px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}
.cells > * { margin: 0; padding: 12px 14px; }
.cells > * + * { border-top: 1px solid var(--line); }
.cells > :empty { display: none; }
.cells input[type="text"] { background: var(--bg); }
.cells .row:first-child { margin-top: 0; }
.cells .row.token { min-height: 44px; margin-right: -10px; }

.plain { margin: 0; }
.hint { margin: 6px 0 0; color: var(--muted); font-size: 14px; }
.cells > .hint { margin: 0; }
.hint a { color: var(--accent); }
.check { display: flex; align-items: center; gap: 10px; margin-top: 12px; font-size: 15px; }
.check input { width: 20px; height: 20px; accent-color: var(--accent); }
a.text-button { display: inline-flex; align-items: center; text-decoration: none; }
.signout {
  display: block;
  width: 100%;
  min-height: 48px;
  margin-top: 26px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface);
  color: var(--danger);
  font-weight: 550;
}

@media (prefers-reduced-motion: no-preference) {
  .entry .box { transition: background-color 0.12s, border-color 0.12s; }
  .front { transition: transform 0.2s ease; }
  .swiping .front { transition: none; }
  .sorting > li:not(.lifted) { transition: transform 0.15s ease; }
  .sheet { transition: transform 0.18s ease; }
  .sheet.pulling { transition: none; }
  .sheet[open] { animation: sheet-up 0.22s ease-out; }
  .sheet[open]::backdrop { animation: fade-in 0.22s ease-out; }
}
@keyframes sheet-up { from { transform: translateY(100%); } }
@keyframes fade-in { from { opacity: 0; } }
