:root {
  --bg: #f7f7fb;
  --surface: #ffffff;
  --text: #1b1b24;
  --muted: #6b6b7b;
  --border: #e3e3ec;
  --accent: #4f46e5;
  --accent-text: #ffffff;
  --danger: #c2410c;
  --ok: #15803d;
  --warn: #a16207;
  color-scheme: light;
}
/* Dark neutrals: follow the system until the user picks a mode (theme.js sets data-scheme). */
@media (prefers-color-scheme: dark) {
  :root:not([data-scheme="light"]) {
    --bg: #111118;
    --surface: #1b1b25;
    --text: #ececf3;
    --muted: #9a9aab;
    --border: #2d2d3a;
    --accent: #818cf8;
    --accent-text: #111118;
    --danger: #fb923c;
    --ok: #4ade80;
    --warn: #facc15;
    color-scheme: dark;
  }
}
:root[data-scheme="dark"] {
  --bg: #111118;
  --surface: #1b1b25;
  --text: #ececf3;
  --muted: #9a9aab;
  --border: #2d2d3a;
  --accent: #818cf8;
  --accent-text: #111118;
  --danger: #fb923c;
  --ok: #4ade80;
  --warn: #facc15;
  color-scheme: dark;
}

* { box-sizing: border-box; }
/* Elements with a display rule (e.g. #auth-screen's grid) must still honor the hidden attribute. */
[hidden] { display: none !important; }
/* The page itself never scrolls or bounces: it's sized to the screen and only
   #scroller (the lists) scrolls. This also disables the browser's own
   pull-to-refresh (which reloads the page) so ours syncs instead. */
html, body { height: 100%; overflow: hidden; overscroll-behavior: none; }
body {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  margin: 0;
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }

.topbar {
  max-width: 720px;
  margin: 0 auto;
  padding: 20px 16px 8px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}
h1 { margin: 0; font-size: 1.6rem; letter-spacing: -0.01em; display: flex; align-items: center; gap: 10px; }
.logo { display: block; width: 36px; height: 36px; flex: none; }
.topbar-actions { display: flex; gap: 8px; align-items: center; }

.pill {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 999px;
  padding: 4px 12px;
  font-size: 0.82rem;
  white-space: nowrap;
}
.pill::before {
  content: "";
  display: inline-block;
  width: 8px; height: 8px;
  border-radius: 50%;
  margin-right: 6px;
  vertical-align: 1px;
  background: var(--muted);
}
.pill[data-status="synced"]::before { background: var(--ok); }
.pill[data-status="syncing"]::before { background: var(--accent); }
.pill[data-status="offline"]::before { background: var(--warn); }
.pill[data-status="error"]::before { background: var(--danger); }

/* Header Undo button, shown for a few seconds after a delete. */
.undo-pill {
  position: relative;
  overflow: hidden;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--accent);
  border-color: var(--accent);
}
.undo-pill::before { content: none; }
.undo-pill::after {
  content: "";
  position: absolute;
  left: 0;
  bottom: 0;
  height: 2px;
  width: 100%;
  background: var(--accent);
  transform-origin: left;
}
.undo-pill.counting::after { animation: undo-countdown 10s linear forwards; }
@keyframes undo-countdown { to { transform: scaleX(0); } }

.icon-btn {
  border: none;
  background: transparent;
  padding: 6px;
  border-radius: 8px;
  color: var(--muted);
  line-height: 0;
}
@media (hover: hover) {
  .icon-btn:hover { background: var(--border); color: var(--text); }
}

main {
  flex: 1;
  min-height: 0;
  width: 100%;
  max-width: 720px;
  margin: 0 auto;
  padding: 0 16px;
  display: flex;
  flex-direction: column;
}
.topbar { flex: none; width: 100%; }
#scroller {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior-y: none; /* no bounce at either end */
  margin: 0 -16px;
  padding: 0 16px 48px;
}
#auth-screen { flex: 1; min-height: 0; overflow-y: auto; }

.add-form { display: flex; gap: 8px; margin: 12px 0 16px; flex-wrap: wrap; }
.add-form input[type="text"] { flex: 1 1 220px; }
#new-title:focus::placeholder { color: transparent; }
input[type="text"], input[type="date"], input[type="search"], select {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  min-width: 0;
}
input:focus-visible, select:focus-visible, button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
button.primary {
  background: var(--accent);
  color: var(--accent-text);
  border: none;
  border-radius: 10px;
  padding: 10px 18px;
  font-weight: 600;
}
button:not(.primary):not(.icon-btn):not(.pill):not(.link):not(.swipe-delete):not([role="tab"]) {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 14px;
}

.todo {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 12px;
  position: relative;
  transition: transform 0.2s ease;
}
.todo .toggle { width: 20px; height: 20px; accent-color: var(--accent); flex: none; }
.todo .body { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.todo .title { overflow-wrap: anywhere; cursor: text; border-radius: 4px; }
.todo .title[contenteditable="plaintext-only"] { outline: 2px solid var(--accent); padding: 0 4px; }
.todo .meta { font-size: 0.8rem; color: var(--muted); }
.todo.done .title { text-decoration: line-through; color: var(--muted); }
.todo .delete { font-size: 0.9rem; line-height: 1; }

/* Drag to reorder */
.todo .drag-handle { flex: none; margin: -6px -4px -6px -8px; cursor: grab; touch-action: none; }
#completed-list .drag-handle { display: none; }
.reordering { user-select: none; -webkit-user-select: none; }
.reordering .todo-row:not(.dragging) { transition: transform 0.15s ease; }
.todo-row.dragging {
  position: relative;
  opacity: 0.95;
  z-index: 2;
  transition: none;
  box-shadow: 0 6px 20px rgb(0 0 0 / 0.25);
  cursor: grabbing;
}

/* Each item sits on top of a red delete button revealed by swiping left. */
.todo-row {
  position: relative;
  margin-bottom: 6px;
  border-radius: 12px;
  overflow: hidden;
  touch-action: pan-y; /* vertical scroll stays native; horizontal drags come to us */
  transition: opacity 0.25s, transform 0.25s;
}
.todo-row.leaving { opacity: 0; transform: translateY(8px) scale(0.98); }
.todo-row.swiping .todo { transition: none; }
.swipe-delete {
  position: absolute;
  inset: 0 0 0 auto;
  width: 84px;
  border: none;
  background: #dc2626;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
}
.todo-row.swiping .swipe-delete, .todo-row.open .swipe-delete { opacity: 1; }
/* Long swipe: the trash icon rides at the leading edge, and grows when release will delete. */
.swipe-delete { justify-content: flex-start; padding-left: 30px !important; transition: width 0.15s ease; }
.todo-row.swiping .swipe-delete { transition: none; }
.swipe-delete svg { transition: transform 0.15s ease; }
.todo-row.armed .swipe-delete svg { transform: scale(1.25); }
@media (hover: none) and (pointer: coarse) {
  .todo .delete { display: none; }
  /* Touch reorders by press-and-hold instead of a grip. */
  .todo .drag-handle { display: none; }
  /* No text selection or callout menu on long-press. */
  .todo-row { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
  .todo .title[contenteditable="plaintext-only"] { -webkit-user-select: text; user-select: text; }
}

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

#overdue, #completed { margin-top: 20px; }
#overdue summary, #completed summary {
  cursor: pointer;
  color: var(--muted);
  font-size: 0.85rem;
  padding: 6px 4px;
  user-select: none;
}
#overdue[open] summary, #completed[open] summary { margin-bottom: 6px; }
#overdue summary { color: var(--danger); }

.todo .redo { display: none; flex: none; font-size: 0.8rem; padding: 4px 10px !important; }
#overdue-list .todo .redo { display: inline-block; }

dialog {
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--surface);
  color: var(--text);
  width: min(460px, calc(100vw - 32px));
  padding: 0;
}
dialog::backdrop { background: rgb(0 0 0 / 0.4); }
.settings-form { display: flex; flex-direction: column; gap: 8px; padding: 20px; }
.settings-form h2 { margin: 0; font-size: 1.25rem; outline: none; }
/* Stays at the top while the dialog scrolls, so ✕ is always reachable. */
.dialog-header {
  position: sticky;
  top: 0;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: -20px -20px 4px;
  padding: 14px 12px 10px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.settings-form h3 { margin: 12px 0 0; font-size: 1rem; }
.settings-form label { font-weight: 600; }
.settings-form > button { align-self: flex-start; }
.hint { color: var(--muted); font-size: 0.85rem; margin: 0; }
#auth-screen { display: grid; place-items: center; padding: 24px 16px; }
.auth-card {
  width: min(420px, 100%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.auth-card h2 { margin: 0; }
#signup-form { display: flex; flex-direction: column; gap: 8px; border-top: 1px solid var(--border); padding-top: 12px; }
.error { color: var(--danger); font-size: 0.9rem; margin: 0; min-height: 1em; }
body.locked main, body.locked .topbar-actions { display: none; }
/* Signed out: stack the logo and name, centered above the Welcome card. */
body.locked .topbar { justify-content: center; padding-top: max(48px, 8vh); }
body.locked h1 { flex-direction: column; gap: 12px; font-size: 2rem; }
body.locked .logo { width: 72px; height: 72px; }
body.locked #auth-screen { padding-top: 8px; }
.row-buttons { display: flex; gap: 8px; flex-wrap: wrap; }

/* Pull-to-refresh indicator */
#ptr {
  position: fixed;
  top: env(safe-area-inset-top);
  left: 50%;
  z-index: 10;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: 0 2px 8px rgb(0 0 0 / 0.15);
  color: var(--muted);
  transform: translate(-50%, -56px);
  transition: transform 0.2s ease;
  pointer-events: none;
}
#ptr.pulling { transition: none; }
#ptr.ready, #ptr.refreshing { color: var(--accent); }
#ptr.refreshing svg { animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

#toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + env(safe-area-inset-bottom));
  z-index: 10;
  transform: translate(-50%, 16px);
  opacity: 0;
  background: var(--text);
  color: var(--bg);
  padding: 8px 16px;
  border-radius: 999px;
  font-size: 0.9rem;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  max-width: calc(100vw - 32px);
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* Appearance pickers */
.segmented { display: inline-flex; border: 1px solid var(--border); border-radius: 10px; padding: 3px; gap: 2px; align-self: flex-start; }
.segmented button {
  border: none !important;
  background: none !important;
  padding: 6px 14px !important;
  border-radius: 7px !important;
  color: var(--muted);
}
.segmented button[aria-checked="true"] { background: var(--accent) !important; color: var(--accent-text); font-weight: 600; }
.swatches { display: flex; gap: 10px; flex-wrap: wrap; }
.swatch {
  width: 36px;
  height: 36px;
  border-radius: 50% !important;
  padding: 0 !important;
  border: 2px solid var(--surface) !important;
  box-shadow: 0 0 0 1px var(--border);
}
.swatch[aria-checked="true"] { box-shadow: 0 0 0 2px var(--text); }

/* Timezone search results */
#tz-results {
  max-height: 220px;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 4px;
  display: flex;
  flex-direction: column;
}
#tz-results [role="option"] {
  text-align: left;
  border: none !important;
  background: none !important;
  padding: 8px 10px !important;
  border-radius: 7px !important;
}
#tz-results [role="option"][aria-selected="true"] {
  background: color-mix(in srgb, var(--accent) 16%, transparent) !important;
}
#tz-results .hint { padding: 8px 10px; }

.version { margin-top: 12px; font-size: 0.75rem; font-variant-numeric: tabular-nums; }

.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 12px; }
