:root {
  --bg: #f4f1ea;
  --card: #fffdf8;
  --ink: #1c1917;
  --muted: #78716c;
  --line: #e7e0d4;
  --accent: #0f766e;
  --accent-soft: #ccfbf1;
  --danger: #b91c1c;
  --ok: #15803d;
  --ok-soft: #dcfce7;
  --shadow: 0 1px 2px rgba(28, 25, 23, 0.06), 0 8px 24px rgba(28, 25, 23, 0.06);
  --radius: 16px;
  --tap: 44px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  -webkit-text-size-adjust: 100%;
}
body {
  min-height: 100dvh;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

#app {
  max-width: 480px;
  margin: 0 auto;
  padding: 16px 14px 48px;
}

.top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 18px;
}
.top h1 {
  margin: 0;
  font-size: 1.35rem;
  letter-spacing: -0.02em;
  font-weight: 700;
}
.status {
  margin: 4px 0 0;
  font-size: 0.8rem;
  color: var(--muted);
  min-height: 1.2em;
}
.status.saving { color: #a16207; }
.status.ok { color: var(--ok); }
.status.err { color: var(--danger); }

.board { display: flex; flex-direction: column; gap: 22px; }

.sec-h {
  margin: 0 0 10px;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.list { display: flex; flex-direction: column; gap: 10px; }

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.card-head {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  align-items: start;
  padding: 14px 14px 12px;
  width: 100%;
  text-align: left;
  background: transparent;
  border: 0;
  cursor: pointer;
  color: inherit;
  font: inherit;
  min-height: var(--tap);
}
.card-head:active { background: #faf7f0; }

.card-title {
  margin: 0;
  font-size: 1.02rem;
  font-weight: 650;
  line-height: 1.3;
  letter-spacing: -0.01em;
}
.card-meta {
  margin: 4px 0 0;
  font-size: 0.78rem;
  color: var(--muted);
}
.next-line {
  margin: 8px 0 0;
  font-size: 0.9rem;
  color: var(--ink);
  line-height: 1.35;
}
.next-line .k {
  color: var(--accent);
  font-weight: 650;
  margin-right: 4px;
}
.next-note {
  margin: 4px 0 0;
  font-size: 0.78rem;
  color: #9a3412;
  line-height: 1.3;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.head-actions {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 8px;
}
.prog {
  font-size: 0.72rem;
  font-weight: 650;
  color: var(--muted);
  background: #f5f0e6;
  border-radius: 999px;
  padding: 4px 8px;
  white-space: nowrap;
}
.prog.done { background: var(--ok-soft); color: var(--ok); }

.tick {
  width: var(--tap);
  height: var(--tap);
  border-radius: 999px;
  border: 2px solid var(--line);
  background: #fff;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 1.1rem;
  cursor: pointer;
  color: transparent;
  flex-shrink: 0;
  transition: background 0.12s, border-color 0.12s, color 0.12s;
}
.tick:active { transform: scale(0.96); }
.tick.on {
  background: var(--ok);
  border-color: var(--ok);
  color: #fff;
}
.tick:disabled {
  opacity: 0.45;
  cursor: default;
}

.card-body {
  border-top: 1px solid var(--line);
  padding: 8px 10px 12px;
  display: none;
}
.card.open .card-body { display: block; }
.card.open .card-head { background: #faf7f0; }

.step {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  grid-template-areas:
    "n main tick"
    "n note tick";
  gap: 8px;
  align-items: start;
  padding: 10px 4px;
  border-radius: 12px;
}
.step + .step { border-top: 1px dashed var(--line); }
.step.current { background: var(--accent-soft); }
.step-n {
  grid-area: n;
  font-size: 0.75rem;
  font-weight: 700;
  color: var(--muted);
  width: 22px;
  padding-top: 12px;
  text-align: center;
}
.step-main {
  grid-area: main;
  min-width: 0;
  border: 0;
  background: transparent;
  text-align: left;
  padding: 8px 0;
  min-height: var(--tap);
  cursor: pointer;
  color: inherit;
  font: inherit;
  width: 100%;
}
.step-main .step-title,
.step-main .step-detail,
.step-main .step-note { overflow-wrap: anywhere; word-break: break-word; }
.step > .tick { grid-area: tick; }
.step-main:active { opacity: 0.75; }
.step-title {
  margin: 0;
  font-size: 0.95rem;
  font-weight: 600;
  line-height: 1.3;
}
.step.done .step-title { text-decoration: line-through; color: var(--muted); }
.step-detail, .step-note {
  margin: 4px 0 0;
  font-size: 0.8rem;
  color: var(--muted);
  line-height: 1.35;
}
.step-note { color: #9a3412; }

.btn-note {
  grid-area: note;
  justify-self: start;
  align-self: start;
  min-height: var(--tap);
  min-width: var(--tap);
  padding: 0 10px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: #fff;
  font: inherit;
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--muted);
  cursor: pointer;
  white-space: nowrap;
}
.btn-note.has {
  color: #9a3412;
  border-color: #fed7aa;
  background: #fff7ed;
}
.btn-note:active { transform: scale(0.96); }

.footer-tools {
  margin-top: 10px;
  padding: 8px 6px 2px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.return-row {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 10px;
  border: 1px dashed var(--line);
  border-radius: 12px;
  background: rgba(255,255,255,0.5);
}
.return-label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 0.78rem;
  font-weight: 650;
  color: var(--muted);
}
.return-label input[type="date"] {
  min-height: var(--tap);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px 12px;
  font: inherit;
  color: var(--ink);
  background: #fff;
  width: 100%;
}
.move-main {
  width: 100%;
}
.add-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
}
.add-task-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 6px;
  margin-top: 4px;
  padding: 2px 0 0;
  border: 0;
  border-radius: 0;
  background: transparent;
  align-items: center;
}
.add-task-row input {
  min-height: 40px;
  border: 1px solid transparent;
  border-bottom-color: var(--line);
  border-radius: 8px;
  padding: 8px 10px;
  font: inherit;
  font-size: 0.9rem;
  background: transparent;
  color: var(--ink);
  width: 100%;
  min-width: 0;
}
.add-task-row input::placeholder {
  color: var(--muted);
  opacity: 0.75;
}
.add-task-row input:focus {
  outline: none;
  border-color: var(--line);
  background: rgba(255,255,255,0.35);
}
.btn.add-task-btn {
  min-height: 36px;
  padding: 0 10px;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--muted);
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: none;
}
.btn.add-task-btn:active {
  background: rgba(0,0,0,0.04);
}
.add-row input {
  min-height: var(--tap);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px 12px;
  font: inherit;
  background: #fff;
}
.btn {
  min-height: var(--tap);
  border-radius: 12px;
  border: 1px solid var(--line);
  background: #fff;
  font: inherit;
  font-weight: 650;
  padding: 0 14px;
  cursor: pointer;
}
.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.btn.finish {
  background: var(--ok);
  border-color: var(--ok);
  color: #fff;
  width: 100%;
  font-size: 1rem;
}
.btn.ghost { background: transparent; }
.btn:active { transform: scale(0.98); }

.upcoming-wrap {
  margin-top: 12px;
  border: 1px dashed var(--line);
  border-radius: var(--radius);
  padding: 4px 10px 10px;
  background: rgba(255,255,255,0.35);
}
.upcoming-sum {
  list-style: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-height: var(--tap);
  cursor: pointer;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}
.upcoming-sum::-webkit-details-marker { display: none; }
.upcoming-wrap[open] .upcoming-sum { margin-bottom: 8px; }

.badge {
  font-size: 0.7rem;
  letter-spacing: 0;
  text-transform: none;
  background: #efe8db;
  color: var(--ink);
  border-radius: 999px;
  padding: 2px 8px;
  font-weight: 700;
}

.finished-sec details > summary {
  list-style: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  cursor: pointer;
  min-height: var(--tap);
}
.finished-sec details > summary::-webkit-details-marker { display: none; }
.finished-sec .card { opacity: 0.85; }
.deleted-sec details > summary {
  list-style: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  cursor: pointer;
  min-height: var(--tap);
}
.deleted-sec details > summary::-webkit-details-marker { display: none; }
.deleted-hint { margin: 0 0 8px; font-size: 0.78rem; }
.empty {
  margin: 0;
  padding: 8px 2px;
  color: var(--muted);
  font-size: 0.85rem;
}

.boot-error {
  margin: 24px 4px;
  padding: 14px;
  border-radius: 12px;
  background: #fef2f2;
  color: var(--danger);
  border: 1px solid #fecaca;
}

/* Keep everything inside the phone viewport */
html, body { overflow-x: hidden; max-width: 100%; }
#app { overflow-x: hidden; }

/* Lock page scroll while edit sheet is open (iOS keyboard / dialog focus) */
body.modal-open {
  overflow: hidden;
  position: fixed;
  width: 100%;
  left: 0;
  right: 0;
}
body.modal-open #app {
  overflow: hidden;
  max-height: 100%;
  pointer-events: none;
}

.edit-dialog {
  border: 0;
  border-radius: 18px 18px 0 0;
  padding: 0;
  margin: 0;
  width: 100%;
  max-width: 100%;
  height: auto;
  max-height: min(90dvh, var(--vvh, 100dvh));
  position: fixed;
  inset: auto 0 0 0;
  bottom: var(--kb-offset, 0px);
  left: 0;
  right: 0;
  top: auto;
  background: var(--card);
  color: var(--ink);
  box-shadow: 0 -12px 40px rgba(0,0,0,0.28);
  overflow: hidden;
  box-sizing: border-box;
}
.edit-dialog::backdrop { background: rgba(28,25,23,0.55); }
.edit-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 14px 14px calc(14px + env(safe-area-inset-bottom));
  max-height: min(90dvh, var(--vvh, 100dvh));
  overflow-x: hidden;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  background: var(--card);
  color: var(--ink);
  box-sizing: border-box;
}
.edit-form h3 { margin: 0 0 4px; font-size: 1.05rem; color: var(--ink); }
.edit-form label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 0.8rem;
  font-weight: 650;
  color: var(--muted);
}
.edit-form input,
.edit-form textarea {
  /* 16px prevents iOS focus zoom that blows the layout */
  font-family: inherit;
  font-size: 16px;
  line-height: 1.35;
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px;
  min-height: var(--tap);
  background: var(--bg);
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
}
.edit-form textarea {
  min-height: 72px;
  max-height: calc(var(--vvh, 100dvh) * 0.22);
  resize: none;
}
.edit-dialog.note-mode .edit-title-field { display: none; }
.edit-dialog.note-mode textarea {
  max-height: calc(var(--vvh, 100dvh) * 0.28);
  min-height: 96px;
}
.edit-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 4px;
  position: sticky;
  bottom: 0;
  padding-top: 4px;
  background: var(--card);
}
.edit-actions .btn {
  min-width: 0;
  width: 100%;
  background: var(--bg);
  color: var(--ink);
  border-color: var(--line);
}
.edit-actions .btn.ghost {
  background: transparent;
  color: var(--ink);
}
.edit-actions .btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}


/* Swipe-to-delete */
.swipe-host {
  position: relative;
  overflow: hidden;
  border-radius: var(--radius);
}
.swipe-delete {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  width: 88px;
  border: 0;
  border-radius: 0;
  background: var(--danger);
  color: #fff;
  font: inherit;
  font-weight: 700;
  font-size: 0.85rem;
  letter-spacing: 0.02em;
  cursor: pointer;
  z-index: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
.swipe-front {
  position: relative;
  z-index: 1;
  will-change: transform;
  touch-action: pan-y;
}
.swipe-host .card {
  /* radius lives on host; avoid double clip fight while dragging */
  box-shadow: var(--shadow);
}
.swipe-host.swipe-dragging .swipe-front,
.swipe-host.swipe-open .swipe-front {
  box-shadow: none;
}
.btn.revive {
  background: transparent;
  border: 1px solid var(--line);
  color: var(--ink);
  width: 100%;
  font-size: 0.95rem;
}
.btn.revive.compact {
  width: auto;
  min-height: 36px;
  padding: 0 12px;
  font-size: 0.8rem;
  font-weight: 650;
  color: var(--ok);
  border-color: #bbf7d0;
  background: var(--ok-soft);
}
.step-main.readonly {
  display: block;
  text-align: left;
  background: transparent;
  border: 0;
  padding: 0;
  color: inherit;
  font: inherit;
  cursor: default;
}
.deleted-sec .card { opacity: 0.9; }
.deleted-sec .card-meta { color: var(--danger); opacity: 0.85; }

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1917;
    --card: #292524;
    --ink: #fafaf9;
    --muted: #a8a29e;
    --line: #44403c;
    --accent: #2dd4bf;
    --accent-soft: #134e4a;
    --ok-soft: #14532d;
    --shadow: none;
  }
  .card-head:active, .card.open .card-head { background: #1f1c1a; }
  .prog { background: #3f3a36; }
  .add-row input, .add-task-row input, .edit-form input, .edit-form textarea, .tick, .btn-note, .return-label input[type="date"] { background: #1c1917; color: var(--ink); }
  .btn-note.has { background: #431407; border-color: #9a3412; color: #fdba74; }
  .upcoming-wrap, .return-row { background: rgba(0,0,0,0.15); }
  .add-task-row { background: transparent; }
  .add-task-row input:focus { background: rgba(255,255,255,0.04); }
  .btn.add-task-btn { color: var(--muted); border-color: var(--line); background: transparent; }
  .btn.revive.compact { background: #14532d; border-color: #166534; color: #86efac; }
  .swipe-delete { background: #991b1b; }
  .badge { background: #44403c; color: var(--ink); }
  .next-note, .step-note { color: #fdba74; }
  /* Edit / Add note bottom sheet — match dark chrome, not UA white */
  .edit-dialog,
  .edit-form,
  .edit-actions {
    background: var(--card);
    color: var(--ink);
  }
  .edit-dialog::backdrop { background: rgba(0,0,0,0.65); }
  .edit-form input,
  .edit-form textarea {
    background: #1c1917;
    color: var(--ink);
    border-color: var(--line);
  }
  .edit-actions .btn {
    background: #1c1917;
    color: var(--ink);
    border-color: var(--line);
  }
  .edit-actions .btn.ghost {
    background: transparent;
    color: var(--ink);
  }
  .edit-actions .btn.primary {
    background: var(--accent);
    border-color: var(--accent);
    color: #0c0a09;
  }
}
