:root {
  --bg: #f4f5f2;
  --surface: #ffffff;
  --text: #1c1f1d;
  --muted: #6b716d;
  --line: #e2e5e0;
  --accent: #1f7a4d;
  --accent-soft: #e3f1e9;
  --danger: #b3261e;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.06), 0 1px 1px rgba(0, 0, 0, 0.04);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121412;
    --surface: #1d201e;
    --text: #e8ebe8;
    --muted: #9aa19c;
    --line: #2c302d;
    --accent: #4cc38a;
    --accent-soft: #1b3326;
    --danger: #f2b8b5;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 17px/1.35 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; }

.top {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--bg);
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  border-bottom: 1px solid var(--line);
}
.title-row { display: flex; align-items: center; justify-content: space-between; max-width: 640px; margin: 0 auto 8px; }
h1 { font-size: 22px; margin: 0; }
.count { color: var(--muted); font-size: 14px; font-weight: 400; margin-left: 4px; }

.menu-wrap { position: relative; }
.menu-btn {
  border: 0;
  background: none;
  font-size: 24px;
  line-height: 1;
  padding: 4px 8px;
  border-radius: 8px;
  cursor: pointer;
}
.menu-btn[aria-expanded="true"] { background: var(--accent-soft); }
.menu {
  position: absolute;
  right: 0;
  top: calc(100% + 4px);
  z-index: 30;
  min-width: 240px;
  padding: 4px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
}
.menu button {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  text-align: left;
  border: 0;
  background: none;
  padding: 12px;
  border-radius: 8px;
  cursor: pointer;
}
.menu button:hover { background: var(--accent-soft); }
.menu .check {
  width: 20px;
  height: 20px;
  border: 2px solid var(--line);
  border-radius: 5px;
  display: grid;
  place-items: center;
  font-size: 13px;
}
.menu [aria-checked="true"] .check { background: var(--accent); border-color: var(--accent); color: #fff; }
.menu [aria-checked="true"] .check::after { content: "✓"; }

/* Rearrange mode */
.handle {
  flex: none;
  border: 0;
  background: none;
  color: var(--muted);
  font-size: 22px;
  line-height: 1;
  padding: 12px 10px;
  cursor: grab;
  touch-action: none;
  user-select: none;
}
.rearranging .card { cursor: default; }
.rearranging .card:active { background: var(--surface); }
.rearranging .cat h2 {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 14px 0 6px;
  padding-left: 12px;
  background: var(--surface);
  border: 1px dashed var(--line);
  border-radius: 10px;
}
.rearranging .cat.empty-cat h2 { opacity: 0.6; }
.rearrange-hint { color: var(--muted); font-size: 14px; text-align: center; margin: 10px 0 0; }
.dragging {
  position: relative;
  z-index: 5;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.22);
  transform: scale(1.02);
}
.cat.dragging h2 { border-style: solid; border-color: var(--accent); }
.cats-collapsed .cat ul { display: none; }
body.dragging-active { user-select: none; -webkit-user-select: none; }

.finish-bar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 15;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  background: var(--bg);
  border-top: 1px solid var(--line);
}
.finish-bar button {
  display: block;
  width: 100%;
  max-width: 640px;
  margin: 0 auto;
  border: 0;
  border-radius: 12px;
  padding: 14px;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  cursor: pointer;
}
@media (prefers-color-scheme: dark) { .finish-bar button { color: #0b1a12; } }
body.is-rearranging main { padding-bottom: 110px; }

.add { position: relative; display: flex; gap: 8px; max-width: 640px; margin: 0 auto; }
.add input {
  flex: 1;
  min-width: 0;
  font: inherit;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  outline: none;
}
.add input:focus { border-color: var(--accent); }
.add > button {
  border: 0;
  border-radius: 12px;
  padding: 0 18px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
}
@media (prefers-color-scheme: dark) { .add > button { color: #0b1a12; } }

.suggestions {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  right: 0;
  margin: 0;
  padding: 4px;
  list-style: none;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
  max-height: 60vh;
  overflow-y: auto;
}
.suggestions li {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px;
  border-radius: 8px;
  cursor: pointer;
}
.suggestions li.active, .suggestions li:hover { background: var(--accent-soft); }
.suggestions .s-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.suggestions .s-name mark { background: none; color: var(--accent); font-weight: 700; }
.suggestions .s-cat { color: var(--muted); font-size: 13px; white-space: nowrap; }
.suggestions .s-forget {
  border: 0; background: none; color: var(--muted); padding: 4px 6px; font-size: 18px; line-height: 1;
}
.suggestions .s-new { color: var(--muted); }

main { max-width: 640px; margin: 0 auto; padding: 8px 16px calc(40px + env(safe-area-inset-bottom)); }

.empty { text-align: center; color: var(--muted); padding: 48px 0 24px; }

.cat h2, .ticked-head h2 {
  font-size: 14px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  margin: 18px 4px 8px;
}
.cat ul, .ticked { list-style: none; margin: 0; padding: 0; }

.card, .undo {
  position: relative;
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 52px;
  margin-bottom: 6px;
  padding: 0 6px 0 14px;
  border-radius: 12px;
  background: var(--surface);
  box-shadow: var(--shadow);
  overflow: hidden;
  cursor: pointer;
  user-select: none;
}
.card .tick {
  flex: none;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 2px solid var(--line);
}
.card .name { flex: 1; min-width: 0; padding: 14px 0; overflow-wrap: anywhere; }
.card .edit {
  flex: none;
  border: 0;
  background: none;
  color: var(--muted);
  padding: 12px 10px;
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
}
.card:active { background: var(--accent-soft); }
.card.flash { animation: flash 1.2s ease-out; }
@keyframes flash { 0%, 30% { background: var(--accent-soft); } 100% { background: var(--surface); } }

.card.swiping {
  transform: translateX(105%);
  opacity: 0;
  transition: transform 0.22s ease-in, opacity 0.22s ease-in;
}

.undo {
  background: var(--accent-soft);
  justify-content: space-between;
  cursor: default;
  animation: undo-in 0.18s ease-out;
}
.undo .undo-text { padding: 14px 0; color: var(--text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.undo .undo-text s { color: var(--muted); }
.undo button {
  flex: none;
  border: 0;
  background: none;
  color: var(--accent);
  font-weight: 700;
  letter-spacing: 0.04em;
  padding: 14px 12px;
  cursor: pointer;
}
.undo::after {
  content: "";
  position: absolute;
  left: 0;
  bottom: 0;
  height: 3px;
  width: 100%;
  background: var(--accent);
  transform-origin: left;
  animation: undo-timer 5s linear forwards;
  animation-delay: var(--delay, 0s);
}
@keyframes undo-in { from { transform: translateX(-12px); opacity: 0; } }
@keyframes undo-timer { from { transform: scaleX(1); } to { transform: scaleX(0); } }
.undo.collapsing { animation: collapse 0.25s ease-in forwards; }
@keyframes collapse {
  from { max-height: 60px; opacity: 1; }
  to { max-height: 0; min-height: 0; margin-bottom: 0; opacity: 0; }
}

.ticked-wrap { margin-top: 28px; }
.ticked-head { display: flex; align-items: baseline; justify-content: space-between; }
.link-btn { border: 0; background: none; color: var(--accent); font-size: 14px; font-weight: 600; padding: 6px 4px; cursor: pointer; }
.link-btn.confirm { color: var(--danger); }
.ticked li {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
  color: var(--muted);
  cursor: pointer;
}
.ticked li .tick { flex: none; width: 22px; height: 22px; border-radius: 50%; background: var(--accent); color: #fff; font-size: 13px; display: grid; place-items: center; opacity: 0.6; }
.ticked li .name { text-decoration: line-through; overflow-wrap: anywhere; }

.sheet {
  border: 0;
  border-radius: 18px 18px 0 0;
  padding: 18px 16px calc(18px + env(safe-area-inset-bottom));
  width: 100%;
  max-width: 640px;
  margin: auto auto 0;
  background: var(--surface);
  color: var(--text);
}
.sheet::backdrop { background: rgba(0, 0, 0, 0.4); }
.sheet-label { display: block; font-size: 13px; color: var(--muted); margin: 0 0 6px; }
.sheet input {
  width: 100%;
  font: inherit;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  margin-bottom: 14px;
}
.cat-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 6px; }
.cat-grid button {
  text-align: left;
  border: 1px solid var(--line);
  background: var(--bg);
  border-radius: 10px;
  padding: 9px 10px;
  font-size: 15px;
  cursor: pointer;
}
.cat-grid button.on { border-color: var(--accent); background: var(--accent-soft); font-weight: 600; }
.sheet-actions { display: flex; justify-content: space-between; margin-top: 16px; }
.sheet-actions button { border: 0; border-radius: 10px; padding: 10px 18px; font-weight: 600; cursor: pointer; }
.sheet-actions .danger { background: none; color: var(--danger); }
.sheet-actions .primary { background: var(--accent); color: #fff; }
@media (prefers-color-scheme: dark) { .sheet-actions .primary { color: #0b1a12; } }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(20px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: 10px;
  font-size: 15px;
  z-index: 20;
}
