:root {
  --bg: #faf9f6;
  --card: #ffffff;
  --ink: #1d1d1f;
  --muted: #6b6b70;
  --line: #e7e6e1;
  --accent: #1f7a3d;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16161a;
    --card: #1f1f24;
    --ink: #f2f2f4;
    --muted: #9a9aa2;
    --line: #2c2c33;
    --accent: #4cc46f;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
}

header {
  position: sticky;
  top: 0;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
  padding: 16px 20px 12px;
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
}

h1 { font-size: 1.4rem; margin: 0; }

.meta { display: flex; align-items: center; gap: 12px; color: var(--muted); font-size: .9rem; }

.sync {
  font-size: .72rem;
  white-space: nowrap;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 2px 9px;
  color: var(--muted);
}
.sync.offline { color: #c0392b; border-color: #c0392b; }

#refresh {
  font-size: 1.1rem;
  background: none;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--ink);
  width: 36px; height: 36px;
  cursor: pointer;
}
#refresh:active { transform: rotate(180deg); }
#refresh.spinning { animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

nav#tabs {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 10px 16px 0;
  max-width: 640px;
  margin: 0 auto;
}
.tab {
  flex: 0 0 auto;
  background: var(--card);
  border: 1px solid var(--line);
  color: var(--muted);
  border-radius: 999px;
  padding: 6px 14px;
  font-size: .9rem;
  cursor: pointer;
}
.tab.active { color: var(--ink); border-color: var(--accent); }

.add-form {
  max-width: 640px;
  margin: 10px auto 0;
  padding: 0 16px;
}
.add-row { display: flex; gap: 8px; }
.add-form select,
.add-form input[type="text"] {
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--card);
  color: var(--ink);
  padding: 10px 12px;
  font-size: 1rem;
}
.add-form select { flex: 0 0 auto; max-width: 240px; }
.add-form #addName { flex: 1; min-width: 0; }
.add-form #addNewCategory { width: 100%; margin-top: 8px; }
.add-when { margin-top: 8px; align-items: center; }
.add-form input[type="date"],
.add-form input[type="time"] {
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--card);
  color: var(--ink);
  padding: 9px 10px;
  font-size: .95rem;
}
.add-form input[type="date"] { flex: 1 1 auto; min-width: 0; }
.add-form input[type="time"] { flex: 0 0 auto; }
.add-form .ghost {
  flex: 0 0 auto;
  background: none;
  border: 1px solid var(--line);
  color: var(--muted);
  border-radius: 10px;
  padding: 9px 12px;
  cursor: pointer;
}
.add-form .ghost:hover { border-color: var(--accent); color: var(--accent); }
.add-form button.primary {
  flex: 0 0 auto;
  width: auto;
  margin: 0;
  padding: 10px 16px;
}

/* On phones the three-up row is cramped: put category + Add on the first line
   and give the text field its own full-width line below. */
@media (max-width: 560px) {
  .add-row { flex-wrap: wrap; }
  .add-form select { order: 1; flex: 1 1 auto; max-width: none; }
  .add-form button.primary { order: 2; }
  .add-form #addName { order: 3; flex: 1 1 100%; }
}

main { max-width: 640px; margin: 0 auto; padding: 12px 16px 40px; }

.category { margin-top: 18px; }
.category h2 {
  font-size: .82rem;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--muted);
  margin: 0 0 8px 4px;
}

ul { list-style: none; margin: 0; padding: 0; }

li {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px 14px;
  margin-bottom: 8px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.name { font-size: 1rem; flex: 1; }

.check {
  flex: 0 0 auto;
  width: 26px;
  height: 26px;
  padding: 0;
  border-radius: 50%;
  border: 1.5px solid var(--line);
  background: var(--bg);
  color: transparent;
  font-size: .8rem;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background .15s, color .15s, border-color .15s;
}
.check:hover { border-color: var(--accent); color: var(--accent); }
li.checking .check { background: var(--accent); border-color: var(--accent); color: #fff; }

/* Recently-completed items linger, struck through and dimmed, at the bottom. */
li.completed { opacity: .55; }
li.completed .name { text-decoration: line-through; }
li.completed .due { opacity: .8; }
.check.done-mark {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  cursor: default;
}
/* Heading for a group whose items are all done — quietly de-emphasised. */
.category.all-done h2 { opacity: .55; }
.done-note { margin-top: 8px; margin-bottom: 4px; }

/* While a check is settling, swallow taps so the next row can't be hit by
   accident as the layout shifts up underneath your finger. */
#list.locked { pointer-events: none; }

/* Items added offline (or mid-sync): shown but not yet on the server. */
li.pending { opacity: .6; }
.pending-dot {
  flex: 0 0 auto;
  width: 26px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.pending-dot::after {
  content: "";
  width: 8px;
  height: 8px;
  border-radius: 50%;
  border: 1.5px solid var(--muted);
  animation: pulse 1.2s ease-in-out infinite;
}
@keyframes pulse { 0%, 100% { opacity: .3; } 50% { opacity: 1; } }

.status {
  font-size: .72rem;
  color: var(--muted);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 2px 9px;
  white-space: nowrap;
}

.due {
  font-size: .72rem;
  color: var(--muted);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 2px 9px;
  white-space: nowrap;
}
.due.overdue { color: #c0392b; border-color: #c0392b; }

.loading, .empty, .error { color: var(--muted); text-align: center; margin-top: 40px; }
.error { color: #c0392b; }

/* On wide screens, widen the shell and tile the categories into columns
   instead of leaving one narrow strip down the middle. */
@media (min-width: 1000px) {
  nav#tabs, .add-form, main, footer { max-width: 1080px; }
  #list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    gap: 8px 22px;
    align-items: start;
  }
  .category { margin-top: 6px; }
  /* Messages (loading/empty/error) span the full width, stay centered. */
  #list > p { grid-column: 1 / -1; }
}

/* --- auth pages --- */
.auth-card {
  max-width: 380px;
  margin: 12vh auto 0;
  padding: 28px 24px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 16px;
  text-align: center;
}
.auth-card h1 { font-size: 1.3rem; margin: 0 0 6px; }
.muted { color: var(--muted); }
.small { font-size: .8rem; }
label { display: block; text-align: left; margin: 14px 0 0; font-size: .85rem; color: var(--muted); }
input[type="text"] {
  width: 100%;
  margin-top: 4px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
  color: var(--ink);
  font-size: 1rem;
}
button {
  font-size: .9rem;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--card);
  color: var(--ink);
  padding: 8px 14px;
  cursor: pointer;
}
button.primary {
  margin-top: 18px;
  width: 100%;
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  padding: 11px;
  font-size: 1rem;
}
.meta a { text-decoration: none; }
#linkBox { display: flex; gap: 8px; margin-top: 12px; }
#linkBox input { flex: 1; }

/* Homepage nav (buttons rendered as links) */
.welcome-nav { display: flex; flex-direction: column; gap: 10px; margin-top: 20px; }
a.primary, a.secondary {
  display: block;
  text-decoration: none;
  text-align: center;
  border-radius: 10px;
  padding: 11px;
  font-size: 1rem;
  border: 1px solid var(--line);
}
a.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
a.secondary { background: var(--card); color: var(--ink); }
a.secondary:hover { border-color: var(--accent); }

/* List page: back-to-lists link + title grouping in the header */
.head-left { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.back { font-size: .78rem; color: var(--muted); text-decoration: none; }
.back:hover { color: var(--accent); }

/* Lists chooser page */
.chooser {
  max-width: 640px;
  margin: 0 auto;
  padding: 16px;
  display: grid;
  gap: 10px;
}
.list-link {
  display: block;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 16px 18px;
  text-decoration: none;
  color: var(--ink);
  font-size: 1.1rem;
}
.list-link:hover { border-color: var(--accent); }

/* --- admin: invite + people --- */
label.inline {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 10px 0 0;
  color: var(--ink);
  font-size: .9rem;
}
label.inline input { width: auto; margin: 0; }

#inviteBox { display: flex; gap: 8px; margin-top: 12px; }
#inviteBox input { flex: 1; }

.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--card);
}
table.people {
  border-collapse: collapse;
  width: 100%;
  font-size: .9rem;
}
table.people th,
table.people td {
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
  white-space: nowrap;
  text-align: left;
}
table.people thead th {
  font-size: .72rem;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--muted);
  font-weight: 600;
}
table.people tbody tr:last-child td { border-bottom: none; }
table.people .center { text-align: center; }
table.people td.name { font-weight: 500; }
.you-tag {
  font-size: .68rem;
  color: var(--muted);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 1px 7px;
  margin-left: 2px;
}
table.people select {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg);
  color: var(--ink);
  padding: 4px 8px;
  font-size: .85rem;
}
table.people select:disabled { opacity: .45; }
button.small { padding: 4px 10px; font-size: .8rem; }
button.danger { border-color: #c0392b; color: #c0392b; }

.action-cell { display: flex; gap: 6px; justify-content: center; }
.badge {
  font-size: .68rem;
  text-transform: uppercase;
  letter-spacing: .03em;
  border-radius: 999px;
  padding: 2px 9px;
  border: 1px solid var(--line);
}
.badge-active { color: var(--accent); border-color: var(--accent); }
.badge-used { color: var(--muted); }
.badge-expired { color: #c0392b; border-color: #c0392b; }

footer {
  max-width: 640px;
  margin: 0 auto;
  padding: 0 16px 40px;
  color: var(--muted);
  font-size: .75rem;
  text-align: center;
}
