* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: #fff;
  color: #1f2937;
}

/* Fills the screen edge to edge. The side and bottom padding grow to clear phone notches,
   rounded corners, and the home bar (env() is 0 elsewhere), and the bottom also makes room
   for an on-screen keyboard. */
.card {
  --pad: 28px;
  --pad-left: max(var(--pad), env(safe-area-inset-left));
  --pad-right: max(var(--pad), env(safe-area-inset-right));
  min-height: 100vh;
  min-height: 100dvh;
  background: #fff;
  padding: var(--pad) var(--pad-right) calc(var(--pad) + env(safe-area-inset-bottom) + var(--keyboard, 0px))
    var(--pad-left);
}

.card-header {
  /* Negative margins stretch the band to the card's edges, past its padding; the top
     padding extends it up behind the phone's status bar. */
  position: relative;
  margin: calc(-1 * var(--pad)) calc(-1 * var(--pad-right)) 24px calc(-1 * var(--pad-left));
  padding: calc(20px + env(safe-area-inset-top)) calc(64px + env(safe-area-inset-right)) 20px
    calc(64px + env(safe-area-inset-left));
  background: #99d6d0;
}

h1 {
  margin: 0;
  font-size: 1.75rem;
  text-align: center;
}

/* Phones: trim the outer spacing so tasks get more of the narrow screen. */
@media (max-width: 480px) {
  .card {
    --pad: 18px;
  }

  .card-header {
    margin-bottom: 18px;
    padding: calc(16px + env(safe-area-inset-top)) calc(56px + env(safe-area-inset-right)) 16px
      calc(56px + env(safe-area-inset-left));
  }

  .account-btn {
    right: calc(8px + env(safe-area-inset-right));
  }
}

.account-btn {
  position: absolute;
  top: calc(50% + env(safe-area-inset-top) / 2);
  right: calc(16px + env(safe-area-inset-right));
  transform: translateY(-50%);
  display: flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  padding: 0;
  color: #1f2937;
  background: transparent;
  border: none;
  border-radius: 50%;
  cursor: pointer;
}

.account-btn svg {
  width: 32px;
  height: 32px;
}

.account-btn:hover,
.account-btn.open {
  background: rgba(0, 0, 0, 0.08);
}

.avatar-menu {
  position: absolute;
  top: calc(100% - 6px);
  right: 10px;
  z-index: 30;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 6px;
  background: #fff;
  border: 1px solid #d1d5db;
  border-radius: 10px;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.12);
}

.avatar-menu[hidden] {
  display: none;
}

.avatar-options {
  display: flex;
  gap: 4px;
}

.avatar-name {
  display: flex;
  gap: 6px;
}

.avatar-name[hidden] {
  display: none;
}

.avatar-name input {
  flex: 1;
  width: 0;
  min-width: 0;
  padding: 6px 8px;
  font-size: 0.9rem;
  border: 2px solid #9ca3af;
  border-radius: 6px;
  outline: none;
}

/* iPhones zoom the page into any text box under 16px when its keyboard opens. */
@media (pointer: coarse) {
  .avatar-name input {
    font-size: 1rem;
  }
}

.avatar-name input:focus {
  border-color: #99d6d0;
}

.avatar-name button {
  padding: 6px 12px;
  font-size: 0.9rem;
  font-weight: 700;
  color: #000;
  background: #99d6d0;
  border: none;
  border-radius: 6px;
  cursor: pointer;
}

.avatar-name button:hover {
  background: #7fcac3;
}

.avatar-option {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  padding: 0;
  color: #1f2937;
  background: transparent;
  border: none;
  border-radius: 50%;
  cursor: pointer;
}

.avatar-option svg {
  width: 32px;
  height: 32px;
}

.avatar-option:hover {
  background: rgba(0, 0, 0, 0.08);
}

.avatar-option.active {
  background: #99d6d0;
}

#task-form {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
}

/* min-width: 0 lets the box shrink below its default size so the row fits narrow screens. */
#task-input {
  flex: 1;
  min-width: 0;
  padding: 10px 12px;
  font-size: 1rem;
  border: 2px solid #9ca3af;
  border-radius: 8px;
  outline: none;
}

#category {
  padding: 10px 12px;
  font-size: 1rem;
  border: 2px solid #9ca3af;
  border-radius: 8px;
  background: #fff;
  outline: none;
  cursor: pointer;
}

/* Native dropdown lists can't be restyled; browsers without base-select keep the default look. */
@supports (appearance: base-select) {
  #category,
  #category::picker(select) {
    appearance: base-select;
  }

  #category::picker(select) {
    padding: 4px;
    border: 1px solid #d1d5db;
    border-radius: 8px;
    box-shadow: 0 8px 20px rgba(0, 0, 0, 0.12);
  }

  #category option {
    padding: 6px 10px;
    border-radius: 6px;
    font-weight: 600;
  }

  #category option + option {
    margin-top: 4px;
  }

  #category option::checkmark {
    display: none;
  }

  #category option:hover,
  #category option:focus-visible {
    background: color-mix(in srgb, var(--cat-color) 82%, #000);
    outline: none;
  }
}

#category.has-category,
#task-input.has-category {
  border-color: var(--cat-color, #16a34a);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--cat-color, #16a34a) 25%, transparent);
}

/* "Group" placeholder: the select is :invalid until a category is picked. */
#category:invalid,
#task-input::placeholder {
  color: #757575;
  opacity: 1;
}

#category option {
  color: #fff;
  background: var(--cat-color);
}

#task-form button {
  flex-shrink: 0;
  width: 46px;
  padding: 0;
  font-size: 2rem;
  font-weight: 700;
  line-height: 1;
  color: #fff;
  background: #16a34a;
  border: none;
  border-radius: 8px;
  cursor: pointer;
}

#task-form button:hover {
  background: #15803d;
}

#task-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.task {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 4px;
  border-bottom: 1px solid #f3f4f6;
}

.drag-handle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  width: 22px;
  height: 28px;
  margin-right: -8px;
  padding: 0;
  border: none;
  border-radius: 4px;
  background: none;
  color: #9ca3af;
  cursor: grab;
  opacity: 0.45;
  touch-action: none;
  -webkit-touch-callout: none;
  user-select: none;
}

.drag-handle svg {
  width: 14px;
  height: 16px;
}

.task:hover .drag-handle,
.subtask:hover .drag-handle,
.group-header:hover .drag-handle,
.drag-handle:focus {
  opacity: 1;
}

.drag-spacer {
  visibility: hidden;
  pointer-events: none;
}

.drag-handle:focus-visible {
  outline: 2px solid #99d6d0;
  outline-offset: 1px;
}

.is-dragging,
.is-dragging * {
  cursor: grabbing !important;
  user-select: none;
}

/* A dragged task keeps its space until dropped; a dragged category's rows close up. */
.task.dragging,
.subtask.dragging {
  visibility: hidden;
}

.is-dragging-category .task.dragging,
.is-dragging-category .subtask.dragging {
  display: none;
}

.group-header.dragging {
  opacity: 0.4;
}

.drag-ghost {
  position: fixed;
  z-index: 20;
  margin: 0;
  pointer-events: none;
  background: transparent;
  border: none;
  box-shadow: none;
}

/* Row rules set position: relative, so the ghost needs the extra class to stay fixed. */
.task.drag-ghost,
.subtask.drag-ghost,
.group-header.drag-ghost {
  position: fixed;
}

.group-header.drag-ghost {
  background: var(--cat-color);
}

.drag-ghost .row-actions,
.drag-ghost .more-btn,
.drag-ghost .drag-handle {
  opacity: 0;
}

.drop-indicator {
  height: 0;
  padding: 0;
  margin: 0;
  border: none;
  list-style: none;
  position: relative;
  pointer-events: none;
}

.drop-indicator::before {
  content: "";
  position: absolute;
  left: var(--line-indent, 4px);
  right: 4px;
  top: -2px;
  height: 3px;
  border-radius: 2px;
  background: var(--cat-color, #99d6d0);
}

/* Right under a category bar the line would blend into the bar's own color. */
.group-header + .drop-indicator::before {
  top: 2px;
}

/* Centered in the gap between the section heading and the first category bar. */
.section-header + .drop-indicator::before {
  top: 6px;
}

/* --hole-shift is measured from the indicator to the middle of the dragged row's space. */
.drop-indicator.in-hole::before {
  top: calc(var(--hole-shift) - 1.5px);
}

.task.drop-parent,
.subtask.drop-parent {
  position: relative;
  background: color-mix(in srgb, var(--cat-color) 22%, #fff);
  border-radius: 6px;
}

/* Hover buttons stay hidden mid-drag so they don't compete with the drop buttons. */
.is-dragging .row-actions {
  opacity: 0 !important;
  pointer-events: none !important;
}

/* Sits over the row's hover-button spot, above the drag ghost. */
.drop-kinds {
  position: absolute;
  right: 4px;
  z-index: 25;
  display: flex;
  gap: 6px;
}

.task > .drop-kinds {
  top: 10px;
}

.subtask > .drop-kinds {
  top: 2px;
}

.drop-kind {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: calc(1.6rem + 8px);
  padding: 0 8px;
  border-radius: 6px;
  background: #fff;
  color: var(--cat-color);
  box-shadow: inset 0 0 0 2px var(--cat-color);
}

.drop-kind svg {
  width: 18px;
  height: 18px;
}

.drop-kind.active {
  background: var(--cat-color);
  color: #fff;
}

.drag-ghost.over-kind {
  opacity: 0.35;
}

.group-header.ghost {
  background: color-mix(in srgb, var(--cat-color) 38%, #fff);
  color: var(--cat-color);
}

.group-header.ghost .group-toggle,
.group-header.ghost .drag-handle {
  color: var(--cat-color);
}

/* Drawn by hand because accent-color doesn't let you pick the check mark's color. */
.task input[type="checkbox"],
.subtask input[type="checkbox"] {
  appearance: none;
  flex-shrink: 0;
  width: 22px;
  height: 22px;
  margin: 0;
  border: 2px solid #9ca3af;
  border-radius: 4px;
  background: #fff center / 20px no-repeat;
  cursor: pointer;
}

.task input[type="checkbox"]:checked,
.subtask input[type="checkbox"]:checked {
  border-color: #e60000;
  background-color: #e60000;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M2.5 8.5l3.5 3.5L13.5 4' fill='none' stroke='%23fff' stroke-width='3' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
}

.task input[type="checkbox"]:focus-visible,
.subtask input[type="checkbox"]:focus-visible {
  outline: 2px solid #99d6d0;
  outline-offset: 2px;
}

.task-text {
  flex: 1;
  font-size: 1.1rem;
  font-weight: 500;
  word-break: break-word;
}

.task,
.task-text {
  cursor: text;
}

/* Negative margin offsets the padding/border so the row doesn't jump while editing. */
.edit-input {
  flex: 1;
  min-width: 0;
  margin: -4px 0;
  padding: 2px 6px;
  font: inherit;
  font-size: 1.1rem;
  font-weight: 500;
  color: #1f2937;
  border: 2px solid var(--cat-color, #9ca3af);
  border-radius: 6px;
  outline: none;
}

.task.completed .task-text {
  text-decoration: line-through;
  color: #9ca3af;
}

.delete-btn {
  border: none;
  color: #ef4444;
  font-size: 1.6rem;
  line-height: 1;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 6px;
}

/* Same footprint as the delete button so the two sit evenly side by side. */
.indent-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: calc(1.6rem + 8px);
  margin-right: -3px;
  padding: 0 8px;
  border: none;
  border-radius: 6px;
  color: #374151;
  background: #e0f2f1;
  cursor: pointer;
}

/* Extra room before the delete button so the pair reads as separate from it. */
.indent-btn.note-btn {
  margin-right: 5px;
}

.indent-btn svg {
  width: 18px;
  height: 18px;
}

.indent-btn:hover {
  background: #c4e7e4;
}

.task.has-subtasks {
  border-bottom: none;
}

/* Each level's checkbox lines up under the text of the row above it (checkbox + gap). */
.subtask {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 2px 4px 2px calc(38px + var(--depth, 0) * 30px);
  cursor: text;
}

.subtask input[type="checkbox"] {
  width: 18px;
  height: 18px;
  background-size: 16px;
}

.subtask input[type="checkbox"]:disabled {
  cursor: default;
}

/* Negative margin keeps the slot narrow; every row has one so checkboxes line up. */
.collapse-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  width: 16px;
  height: 16px;
  margin-right: -6px;
  padding: 0;
  border: none;
  background: none;
  color: #6b7280;
}

button.collapse-toggle {
  cursor: pointer;
  border-radius: 4px;
}

button.collapse-toggle:hover {
  color: #1f2937;
  background: #f3f4f6;
}

.collapse-toggle svg {
  width: 14px;
  height: 14px;
  transition: transform 0.15s;
}

.collapse-toggle.collapsed svg {
  transform: rotate(-90deg);
}

/* inline-block keeps the strike-through on completed text from crossing the badge. */
.hidden-count {
  display: inline-block;
  margin-left: 8px;
  padding: 0 7px;
  border-radius: 999px;
  background: #f3f4f6;
  color: #6b7280;
  font-size: 0.75rem;
  font-weight: 600;
  line-height: 1.5;
  vertical-align: middle;
  cursor: pointer;
}

/* Takes the checkbox's spot so note text lines up with indented-line text. */
.note-arrow {
  display: inline-flex;
  flex-shrink: 0;
  width: 18px;
  height: 18px;
  color: var(--cat-color, #9ca3af);
}

.note-arrow svg {
  width: 100%;
  height: 100%;
}

.subtask.completed .note-arrow {
  color: #9ca3af;
}

.subtask.last-subtask {
  padding-bottom: 10px;
  border-bottom: 1px solid #f3f4f6;
}

.subtask-text {
  flex: 1;
  padding: 4px 0;
  font-weight: 500;
  color: #374151;
  word-break: break-word;
}

.subtask.completed .subtask-text {
  text-decoration: line-through;
  color: #9ca3af;
}

.subtask .edit-input {
  font-size: 1rem;
  font-weight: 500;
  margin: 0;
}

/* Controls sit on the first line of text, so wrapped text grows downward past them.
   --mid is half the hover buttons' height, the tallest thing in a one-line row. */
.task,
.subtask {
  --mid: calc(0.8rem + 4px);
  align-items: flex-start;
}

.task .drag-handle,
.subtask .drag-handle {
  margin-top: calc(var(--mid) - 14px);
}

.task .collapse-toggle,
.subtask .collapse-toggle {
  margin-top: calc(var(--mid) - 8px);
}

.task input[type="checkbox"] {
  margin-top: calc(var(--mid) - 11px);
}

.subtask input[type="checkbox"],
.note-arrow {
  margin-top: calc(var(--mid) - 9px);
}

.task-text {
  line-height: 1.4;
  padding-top: calc(var(--mid) - 0.77rem);
}

.subtask-text {
  line-height: 1.4;
  padding: calc(var(--mid) - 0.7rem) 0 4px;
}

.task .edit-input,
.subtask .edit-input {
  align-self: center;
}

.delete-btn,
.group-delete-btn,
.section-delete-btn {
  background: #fee2e2;
}

.delete-btn:hover,
.group-delete-btn:hover,
.section-delete-btn:hover {
  background: #fecaca;
}

.empty {
  text-align: center;
  color: #9ca3af;
  margin: 24px 0;
}

.empty[hidden] {
  display: none;
}

/* Sticky so Undo stays in view at the bottom of the card while a long list scrolls under it. */
footer {
  position: sticky;
  bottom: 0;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  margin-top: 16px;
  padding: 12px 0;
  border-top: 1px solid #f3f4f6;
  background: #fff;
  font-size: 0.875rem;
  color: #6b7280;
}

.history-buttons {
  flex: 1;
  display: flex;
  align-items: center;
}

.history-btn {
  padding: 10px 18px;
  font-size: 1.1rem;
  font-weight: 700;
  white-space: nowrap;
  color: #000;
  background: #99d6d0;
  border: none;
  border-radius: 999px;
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.2);
  cursor: pointer;
}

.history-btn:hover:not(:disabled) {
  background: #7fcac3;
}

#undo-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  box-shadow: none;
}

/* Collapsed to zero width while hidden; it grows leftward from the right edge. */
#redo-btn {
  max-width: 0;
  margin-left: auto;
  padding-left: 0;
  padding-right: 0;
  overflow: hidden;
  opacity: 0;
  pointer-events: none;
  transition: max-width 0.25s ease, padding 0.25s ease, opacity 0.2s ease;
}

#redo-btn.visible {
  max-width: 140px;
  padding-left: 18px;
  padding-right: 18px;
  opacity: 1;
  pointer-events: auto;
}

.cat-personal { --cat-color: #9673f4; }
.cat-work     { --cat-color: #528ff4; }
.cat-shopping { --cat-color: #4dbb93; }
.cat-other    { --cat-color: #f4905a; }

.task:not(.completed) input[type="checkbox"],
.subtask:not(.completed) input[type="checkbox"] {
  border-color: var(--cat-color);
}

.group-header {
  margin-top: 16px;
  margin-bottom: 8px;
  padding: 6px 10px;
  border-radius: 6px;
  background: var(--cat-color);
  color: #fff;
  font-size: 0.95rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.group-header {
  position: relative;
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  user-select: none;
}

.group-header .drag-handle {
  color: #fff;
  margin-right: 0;
  opacity: 0.7;
}

.group-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  width: 18px;
  height: 18px;
  padding: 0;
  border: none;
  border-radius: 4px;
  background: none;
  color: #fff;
  cursor: pointer;
}

.group-toggle svg {
  width: 16px;
  height: 16px;
  transition: transform 0.15s;
}

.group-toggle.collapsed svg {
  transform: rotate(-90deg);
}

.group-delete-btn {
  padding: 0 8px;
  border: none;
  border-radius: 6px;
  color: #ef4444;
  font-size: 1.6rem;
  font-weight: normal;
  letter-spacing: normal;
  line-height: 1;
  cursor: pointer;
}
.group-header:first-child,
.section-header:first-child {
  margin-top: 0;
}

.section-header {
  margin-top: 28px;
  padding-top: 14px;
  border-top: 3px solid #9ca3af;
  color: #374151;
  font-size: 1.15rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.section-delete-btn {
  padding: 0 8px;
  border: none;
  border-radius: 6px;
  color: #ef4444;
  font-size: 1.6rem;
  font-weight: normal;
  line-height: 1;
  cursor: pointer;
}

.section-active {
  color: #000;
}

.section-completed {
  color: #e60000;
}

.section-header:first-child {
  padding-top: 0;
  border-top: none;
}

.section-header + .group-header {
  margin-top: 8px;
}

/* Row buttons float over the end of the text, faded in from a soft edge, so they never
   take width from it. They open just left of the dots button (22px wide + 4px space).
   Tops match each row's padding so they sit on the first text line. */
.task,
.subtask,
.section-header {
  position: relative;
}

.row-actions {
  position: absolute;
  right: 30px;
  z-index: 2;
  display: flex;
  align-items: center;
  gap: 12px;
  padding-left: 18px;
  background: linear-gradient(to right, rgba(255, 255, 255, 0), #fff 18px);
  opacity: 0;
  translate: 10px 0;
  pointer-events: none;
  transition: opacity 0.15s, translate 0.15s;
}

.task > .row-actions {
  top: 10px;
}

.subtask > .row-actions {
  top: 2px;
}

.group-header > .row-actions {
  top: 50%;
  right: 36px;
  transform: translateY(-50%);
  background: linear-gradient(to right, transparent, var(--cat-color) 18px);
}

.section-header > .row-actions {
  top: calc(50% + 7px);
  right: 32px;
  transform: translateY(-50%);
}

.section-header:first-child > .row-actions {
  top: 50%;
}

.actions-open > .row-actions,
.row-actions:focus-within {
  opacity: 1;
  translate: 0 0;
  pointer-events: auto;
}

/* Hints that the row has buttons, and toggles them. Styled to match the drag grip. */
.more-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  width: 22px;
  height: 28px;
  margin-left: auto;
  padding: 0;
  border: none;
  border-radius: 4px;
  background: none;
  color: #9ca3af;
  cursor: pointer;
  opacity: 0.45;
}

.task > .more-btn,
.subtask > .more-btn {
  margin-top: calc(var(--mid) - 14px);
}

.group-header > .more-btn {
  height: 22px;
  margin-block: -2px;
  color: #fff;
  opacity: 0.7;
}

.section-header > .more-btn {
  height: 22px;
  margin-block: -4px;
  margin-right: 6px;
}

.more-btn svg {
  width: 14px;
  height: 16px;
}

.actions-open > .more-btn,
.more-btn:focus-visible {
  opacity: 1;
}

.more-btn:focus-visible {
  outline: 2px solid #99d6d0;
  outline-offset: 1px;
}

@media (hover: hover) {
  .task:not(.actions-dismissed):hover > .row-actions,
  .subtask:not(.actions-dismissed):hover > .row-actions,
  .group-header:not(.actions-dismissed):hover > .row-actions,
  .section-header:not(.actions-dismissed):hover > .row-actions {
    opacity: 1;
    translate: 0 0;
    pointer-events: auto;
  }

  .task:hover > .more-btn,
  .subtask:hover > .more-btn,
  .group-header:hover > .more-btn,
  .section-header:hover > .more-btn {
    opacity: 1;
  }
}

.more-btn[hidden],
.row-actions[hidden],
.task:has(.edit-input) > :is(.more-btn, .row-actions),
.subtask:has(.edit-input) > :is(.more-btn, .row-actions) {
  display: none;
}
