:root {
  --bg: #f6f5f2;
  --surface: #ffffff;
  --text: #1d1d1f;
  --muted: #6e6e73;
  --border: #e3e1dc;
  --accent: #1d1d1f;
  --accent-text: #ffffff;
  --danger: #d93036;
  --p1: #e5484d; --p1-bg: #fdeceb;
  --p2: #8e4ec6; --p2-bg: #f4edfc; /* medium: purple */
  --p3: #4face8; --p3-bg: #e9f5fd; /* low: light blue */
  --warn: #e8850c;
  --ok: #1f9d55; --ok-bg: #e7f6ec;
  --radius: 14px;
  --bar-h: 66px;
  color-scheme: light;
}

/* theme.js sets data-theme from the saved choice, or the system setting if none. */
:root[data-theme="dark"] {
  --bg: #111113;
  --surface: #1c1c1f;
  --text: #f2f2f3;
  --muted: #9a9aa1;
  --border: #2c2c31;
  --accent: #f2f2f3;
  --accent-text: #111113;
  --danger: #ff6369;
  --p1: #ff6369; --p1-bg: #3a1c1e;
  --p2: #b98cf0; --p2-bg: #2b1f3b;
  --p3: #7cc4f5; --p3-bg: #172a38;
  --warn: #ffa94d;
  --ok: #3ecf8e; --ok-bg: #15301f;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
button, input { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; padding: 0; }
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--p, var(--text)); outline-offset: 2px; }

.p1 { --p: var(--p1); --p-bg: var(--p1-bg); }
.p2 { --p: var(--p2); --p-bg: var(--p2-bg); }
.p3 { --p: var(--p3); --p-bg: var(--p3-bg); }

/* Buttons and fields */
.field, .add-input, .edit-text, .edit-date {
  width: 100%;
  min-width: 0;
  height: 48px;
  padding: 0 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
}
.btn {
  height: 44px;
  padding: 0 14px;
  border-radius: 10px;
  font-weight: 600;
  background: var(--bg);
}
.btn.primary { background: var(--accent); color: var(--accent-text); }
.btn.danger { color: var(--danger); }
.btn.armed { background: var(--danger); color: #fff; }
.btn:disabled { opacity: .5; }

/* Login */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 24px; }
.login form { width: min(100%, 360px); display: grid; gap: 12px; }
.login h1 { margin: 0 0 8px; font-size: 32px; }
.login .btn { height: 48px; }
.error { margin: 0; color: var(--danger); }

/* App shell */
.app {
  max-width: 640px;
  margin: 0 auto;
  min-height: 100dvh;
  padding-bottom: calc(var(--bar-h) + env(safe-area-inset-bottom) + 24px);
}
.top {
  position: sticky;
  top: 0;
  z-index: 5;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
}
.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(env(safe-area-inset-top) + 10px) max(16px, env(safe-area-inset-right)) 10px max(16px, env(safe-area-inset-left));
}

.tabs {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr 1fr;
  padding: 3px;
  border-radius: 12px;
  background: var(--border);
}
.tab { height: 44px; border-radius: 9px; font-weight: 600; color: var(--muted); }
.tab.active { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgb(0 0 0 / .12); }
.count { margin-left: 6px; font-size: 13px; font-weight: 500; opacity: .7; }
.icon-btn { flex: none; width: 46px; height: 46px; display: grid; place-items: center; border-radius: 12px; color: var(--muted); }
.icon-btn:active { background: var(--border); }
.title { flex: 1; margin: 0; font-size: 20px; }
.icon-sun, :root[data-theme="dark"] .icon-moon { display: none; }
:root[data-theme="dark"] .icon-sun { display: block; }

/* List */
.list {
  list-style: none;
  margin: 0;
  padding: 4px max(16px, env(safe-area-inset-right)) 0 max(16px, env(safe-area-inset-left));
  display: grid;
  gap: 8px;
}
.todo {
  position: relative;
  overflow: hidden;
  border-radius: var(--radius);
  box-shadow: 0 1px 2px rgb(0 0 0 / .06);
}
.surface {
  position: relative;
  z-index: 1;
  display: flex;
  align-items: center;
  min-height: 56px;
  /* Priority shows as a coloured outline around the whole card, heavier on the left. */
  border: 2px solid var(--p);
  border-left-width: 6px;
  border-radius: var(--radius);
  background: var(--surface);
  /* Vertical scrolling stays with the browser; horizontal moves become swipes. */
  touch-action: pan-y;
  -webkit-user-select: none;
  user-select: none;
  transition: transform .2s ease, background .2s;
}
.todo.swiping .surface { transition: background .2s; }

/* Under the row: the ✏️ on whichever side the swipe uncovers. */
.under {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  background: var(--border);
  font-size: 22px;
  transition: background .15s;
}
.todo.armed .under { background: var(--accent); }
.edit-hint { opacity: 0; transition: opacity .15s, transform .15s; }
.todo.reveal-left .edit-hint.left, .todo.reveal-right .edit-hint.right { opacity: 1; }
.todo.armed .edit-hint { transform: scale(1.25); }

/* Round area: category icon (tap to filter) inside the priority ring. */
.cat {
  flex: none;
  width: 44px;
  height: 44px;
  margin: 6px 2px 6px 6px;
  display: grid;
  place-items: center;
  border: 2px solid var(--p);
  border-radius: 50%;
  background: var(--p-bg);
  font-size: 22px;
  line-height: 1;
  transition: box-shadow .15s, background .15s;
}
.cat[aria-pressed="true"] { background: var(--surface); box-shadow: 0 0 0 2px var(--surface), 0 0 0 5px var(--accent); }

.body {
  flex: 1;
  min-width: 0;
  min-height: 56px;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px 12px 10px;
  cursor: pointer;
}
.content { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.todo.checked .surface { background: var(--ok-bg); border-color: var(--ok); }
.todo.checked .label { color: var(--muted); text-decoration: line-through; }
.tick {
  flex: none;
  width: 28px;
  height: 28px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: var(--ok);
  color: #fff;
  font-weight: 700;
}

.todo.done .surface { gap: 6px; padding-right: 8px; }
.todo.done .text { flex: 1; min-width: 0; padding: 12px 0 12px 8px; overflow-wrap: anywhere; }
.todo.done .label { color: var(--muted); text-decoration: line-through; }
.due { display: block; margin-top: 2px; font-size: 13px; color: var(--muted); }
.due.today { color: var(--warn); font-weight: 600; }
.due.overdue { color: var(--danger); font-weight: 600; }
.meta { display: block; margin-top: 2px; font-size: 13px; color: var(--muted); }
.todo.done .btn { padding: 0 12px; font-size: 14px; }

/* Category picker (editor + add bar menu) */
.cat-picker { display: flex; align-items: center; gap: 6px; }
.cat-options { display: flex; flex-wrap: wrap; gap: 6px; flex: 1; min-width: 0; }
/* Divider + dashed rounded square: the ⚙︎ must not read as one more category. */
.cat-divider { flex: none; width: 1px; align-self: stretch; margin: 2px 2px; background: var(--border); }
.cat-settings {
  flex: none;
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  border: 1.5px dashed var(--muted);
  border-radius: 12px;
  color: var(--muted);
  font-size: 20px;
}
.cat-settings:active { background: var(--border); }
.cat-pick {
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: var(--bg);
  box-shadow: inset 0 0 0 1px var(--border);
  font-size: 22px;
}
.cat-pick.none { font-size: 20px; color: var(--muted); }
.cat-pick[aria-checked="true"] { background: var(--surface); box-shadow: inset 0 0 0 3px var(--accent); }
.cat-btn {
  flex: none;
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  border-radius: 10px;
  color: var(--muted);
  font-size: 22px;
}
.cat-btn.has-cat { background: var(--surface); box-shadow: inset 0 0 0 1px var(--border); }
.cat-menu {
  position: absolute;
  left: max(12px, env(safe-area-inset-left));
  right: max(12px, env(safe-area-inset-right));
  bottom: 100%;
  max-width: 616px;
  margin: 0 auto 8px;
  padding: 8px;
  border-radius: 14px;
  background: var(--surface);
  box-shadow: 0 6px 20px rgb(0 0 0 / .18);
}

/* Icons page */
.icons-page { padding: 4px max(16px, env(safe-area-inset-right)) 24px max(16px, env(safe-area-inset-left)); }
.icon-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.icon-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px;
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: 0 1px 2px rgb(0 0 0 / .06);
}
.icon-input {
  flex: none;
  width: 52px;
  height: 52px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 50%;
  background: var(--bg);
  font-size: 26px;
  text-align: center;
}
.icon-fields { flex: 1; min-width: 0; display: grid; gap: 2px; }
.name-input {
  width: 100%;
  min-width: 44px;
  height: 44px;
  padding: 0 10px;
  border: 1px solid transparent;
  border-radius: 10px;
  background: transparent;
  font-weight: 600;
}
.name-input:focus { border-color: var(--border); background: var(--bg); }
.usage { padding: 0 10px; font-size: 12px; color: var(--muted); }
.icon-actions { flex: none; display: flex; gap: 4px; }
.icon-actions .btn { min-width: 44px; padding: 0 10px; }
.add-row { margin-top: 12px; background: transparent; box-shadow: none; border: 1.5px dashed var(--border); }
.icons-hint { margin: 16px 0 0; text-align: center; font-size: 13px; color: var(--muted); }

/* Editor */
.list.editing > .todo:not(.editing) { pointer-events: none; opacity: .45; }
.todo.editing { padding: 12px; border: 2px solid var(--p); border-left-width: 6px; border-radius: var(--radius); background: var(--surface); }
.editor { display: grid; gap: 10px; }
.edit-text, .edit-date { height: 46px; background: var(--bg); }
.date-row { display: flex; align-items: center; gap: 8px; }
.date-row .date-label { color: var(--muted); font-size: 14px; font-weight: 600; }
.edit-date { flex: 1; -webkit-appearance: none; appearance: none; display: flex; align-items: center; }
.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }

/* Priority picker */
.prio-picker { display: flex; gap: 6px; }
.prio {
  flex: 1;
  height: 44px;
  padding: 0 12px;
  border: 2px solid var(--p);
  border-radius: 999px;
  background: var(--p-bg);
  color: var(--p);
  font-size: 14px;
  font-weight: 700;
}
.prio[aria-checked="true"] { background: var(--p); color: #fff; }
/* Add bar: 44px tap targets drawn as small dots */
.addbar .prio-picker { gap: 0; }
.addbar .prio { flex: none; width: 44px; padding: 0; border: 0; background: none; display: grid; place-items: center; }
.addbar .prio::before {
  content: '';
  width: 26px;
  height: 26px;
  border: 2px solid var(--p);
  border-radius: 50%;
  background: var(--p-bg);
}
.addbar .prio[aria-checked="true"]::before { background: var(--p); }

/* Add bar */
.addbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 5;
  padding: 10px max(12px, env(safe-area-inset-right)) calc(10px + env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  border-top: 1px solid var(--border);
  background: color-mix(in srgb, var(--bg) 90%, transparent);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
}
.addbar-inner { max-width: 640px; margin: 0 auto; display: grid; grid-template-columns: 1fr auto auto; align-items: center; gap: 8px; }
.add-input { height: 46px; }
.add-options { display: flex; align-items: center; gap: 4px; }
.date-btn {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-width: 44px;
  height: 44px;
  padding: 0 10px;
  border-radius: 10px;
  color: var(--muted);
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
}
.date-btn.has-date { background: var(--surface); color: var(--text); box-shadow: inset 0 0 0 1px var(--border); }
/* The real date input covers the button so tapping it opens the native picker. */
.date-btn input { position: absolute; inset: 0; width: 100%; height: 100%; opacity: 0; cursor: pointer; }

/* Phones: text + add button on top, priority + date underneath. */
@media (max-width: 479px) {
  :root { --bar-h: 114px; }
  .addbar-inner { grid-template-columns: 1fr auto; grid-template-areas: "text add" "options options"; row-gap: 4px; }
  .add-input { grid-area: text; }
  .add-btn { grid-area: add; }
  .add-options { grid-area: options; }
}
.add-btn {
  flex: none;
  width: 46px;
  height: 46px;
  border-radius: 12px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 28px;
  line-height: 1;
}

/* Empty state + toast */
.empty { margin: 0; padding: 56px 16px; text-align: center; color: var(--muted); }
.empty .btn { display: block; margin: 12px auto 0; color: var(--text); }
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--bar-h) + env(safe-area-inset-bottom) + 16px);
  z-index: 10;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 12px;
  width: max-content;
  max-width: calc(100% - 32px);
  padding: 10px 10px 10px 16px;
  border-radius: 12px;
  background: var(--accent);
  color: var(--accent-text);
  box-shadow: 0 6px 20px rgb(0 0 0 / .2);
}
.toast button {
  min-height: 44px;
  padding: 0 14px;
  border-radius: 8px;
  font-weight: 700;
  background: color-mix(in srgb, var(--accent-text) 18%, transparent);
}
