:root {
  --bg: #f4f4f6;
  --surface: #ffffff;
  --surface-2: #ececf0;
  --text: #17181c;
  --muted: #6b6e78;
  --line: #dcdde3;
  --shadow: 0 10px 30px rgba(20, 22, 30, .12), 0 2px 6px rgba(20, 22, 30, .06);
  --accent: #5e6ad2;
  --done: #1f9d55;
  --cancel: #d6453d;
  --todo: #3d7be0;
  --someday: #8a63c9;
  --postpone: #c98a1a;
  --skip: #7c8090;
  --waiting: #12939b;
  --triage: #c2459a;
  --clear: #4f5bc4;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #111216;
    --surface: #1c1d23;
    --surface-2: #26272f;
    --text: #ececf1;
    --muted: #9a9caa;
    --line: #30313a;
    --shadow: 0 10px 30px rgba(0, 0, 0, .45), 0 2px 6px rgba(0, 0, 0, .3);
    --accent: #7d87e8;
    --done: #34c06f;
    --cancel: #f0625a;
    --todo: #5c97f5;
    --someday: #a785e3;
    --postpone: #e3a63a;
    --skip: #9a9eae;
    --waiting: #3cc4cc;
    --triage: #e46cbf;
    --clear: #8c96f0;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #111216; --surface: #1c1d23; --surface-2: #26272f; --text: #ececf1; --muted: #9a9caa;
  --line: #30313a; --accent: #7d87e8; --done: #34c06f; --cancel: #f0625a; --todo: #5c97f5;
  --someday: #a785e3; --postpone: #e3a63a; --skip: #9a9eae; --waiting: #3cc4cc; --triage: #e46cbf;
  --clear: #8c96f0; color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body {
  margin: 0; height: 100%; overflow: hidden;
  overscroll-behavior: none;
  background: var(--bg); color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
code { font-size: .9em; background: var(--surface-2); padding: 1px 5px; border-radius: 4px; }
.muted { color: var(--muted); font-size: 14px; }
.error { color: var(--cancel); font-size: 14px; }

.screen {
  position: fixed; inset: 0;
  display: flex; flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* Setup */
#setup { justify-content: center; overflow-y: auto; }
.setup-box { width: min(440px, 100%); margin: 0 auto; padding: 24px 16px; }
.setup-box h1 { font-size: 28px; margin: 0 0 12px; }
#key-form { display: flex; flex-direction: column; gap: 10px; margin-top: 18px; }
#signin-btn { width: 100%; margin: 20px 0 8px; font-size: 17px; }
.setup-box a { color: var(--accent); }
.key-alt { margin-top: 18px; border-top: 1px solid var(--line); padding-top: 14px; }
.key-alt summary { color: var(--muted); font-size: 14px; cursor: pointer; }
input {
  font: inherit; font-size: 16px; color: var(--text);
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px;
  padding: 12px 14px; width: 100%;
}
input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.primary, .danger {
  border: 0; border-radius: 12px; padding: 13px 16px; font-weight: 600; cursor: pointer;
  background: var(--accent); color: #fff; text-align: center; text-decoration: none; display: block;
}
.danger { background: transparent; color: var(--cancel); border: 1px solid var(--cancel); width: 100%; }
.primary:disabled { opacity: .5; }

/* Header */
header { padding: 6px 12px 0; flex: none; }
.toprow { display: flex; align-items: center; gap: 4px; height: 44px; }
.status { flex: 1; text-align: center; font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn {
  width: 40px; height: 40px; border: 0; background: transparent; border-radius: 10px;
  display: grid; place-items: center; color: var(--muted); cursor: pointer; flex: none;
}
.icon-btn:active { background: var(--surface-2); }
.icon-btn:disabled { opacity: .3; }
.icon-btn.spin svg { animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Bottom tab bar */
#main { padding-bottom: 0; }
.tabbar {
  flex: none; display: flex;
  background: var(--surface); border-top: 1px solid var(--line);
  padding: 4px 8px env(safe-area-inset-bottom);
}
.tab {
  flex: 1; min-width: 0; border: 0; background: transparent; padding: 6px 2px 8px; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  font-size: 12px; font-weight: 600; color: var(--muted); white-space: nowrap;
}
.tab svg { width: 24px; height: 24px; }
.tab .count { font-weight: 500; margin-left: 3px; font-variant-numeric: tabular-nums; }
.tab[aria-selected="true"] { color: var(--accent); }

/* Deck */
.deck {
  flex: 1; position: relative; min-height: 0;
  touch-action: none; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
}
.card {
  position: absolute; inset: 10px 16px 26px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 22px;
  box-shadow: var(--shadow);
  padding: 20px 20px 16px;
  display: flex; flex-direction: column; gap: 10px;
  overflow: hidden; will-change: transform;
  transition: transform .28s cubic-bezier(.2,.8,.2,1), opacity .28s;
  touch-action: none;
}
.card.dragging { transition: none; cursor: grabbing; }
.card.behind-1 { transform: translateY(24px) scale(.94); pointer-events: none; }
.card.behind-2 { transform: translateY(44px) scale(.88); pointer-events: none; }
.card .meta { display: flex; gap: 8px; align-items: center; font-size: 13px; color: var(--muted); flex-wrap: wrap; }
.card .ident { font-weight: 600; color: var(--text); font-variant-numeric: tabular-nums; }
.card .meta .state { padding: 1px 8px; border: 1px solid var(--line); border-radius: 999px; color: var(--text); font-weight: 500; }
.note-line {
  display: flex; justify-content: space-between; gap: 8px; margin: -6px -8px 0; padding: 7px 10px; border-radius: 12px;
  background: color-mix(in srgb, var(--accent) 12%, transparent); color: var(--accent); font-size: 13px; font-weight: 700;
}
.note-line.overdue { background: color-mix(in srgb, var(--cancel) 12%, transparent); color: var(--cancel); }
.note-line .when { font-weight: 500; opacity: .85; white-space: nowrap; }
.card h2 { margin: 0; font-size: 21px; line-height: 1.28; letter-spacing: -.01em; overflow-wrap: anywhere; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  font-size: 12px; padding: 3px 9px; border-radius: 999px; background: var(--surface-2); color: var(--muted);
  display: inline-flex; align-items: center; gap: 6px; max-width: 100%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.chip .dot { width: 8px; height: 8px; border-radius: 50%; flex: none; }
.chip.prio-1 { color: var(--cancel); }
.chip.prio-2 { color: var(--postpone); }
.chip.health-onTrack { color: var(--done); }
.chip.health-atRisk { color: var(--postpone); }
.chip.health-offTrack { color: var(--cancel); }
.card .body { flex: 1; min-height: 0; display: flex; gap: 12px; }
.card .desc {
  flex: 1; min-width: 0; min-height: 0; overflow: hidden; color: var(--muted); font-size: 15px;
  white-space: pre-wrap; overflow-wrap: anywhere;
  -webkit-mask-image: linear-gradient(#000 75%, transparent);
  mask-image: linear-gradient(#000 75%, transparent);
}
.prio { flex: none; align-self: flex-end; display: flex; flex-direction: column; gap: 6px; }
.prio-btn {
  display: grid; place-items: center; width: 42px; height: 38px; padding: 0; cursor: pointer;
  border: 1px solid var(--line); border-radius: 10px; background: var(--surface); color: var(--muted);
  touch-action: none;
}
.prio-btn svg { width: 18px; height: 18px; }
.prio-btn svg .f { fill: currentColor; stroke: none; }
.prio-btn svg .dim { opacity: .3; }
.prio-btn svg .bang { fill: none; stroke: #fff; stroke-width: 2.6; stroke-linecap: round; }
.prio-btn.p1 svg { color: var(--cancel); }
.prio-btn.p2 svg { color: var(--postpone); }
.prio-btn[aria-checked="true"] { background: var(--surface-2); color: var(--text); border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
.card .foot { display: flex; justify-content: space-between; align-items: center; font-size: 13px; color: var(--muted); gap: 8px; }
.card .foot .more { color: var(--accent); font-weight: 600; }

/* Swipe stamp */
.stamp {
  position: absolute; top: 18px; padding: 6px 12px; border: 3px solid currentColor; border-radius: 10px;
  font-weight: 800; font-size: 22px; letter-spacing: .06em; text-transform: uppercase;
  opacity: 0; pointer-events: none; background: var(--surface);
}
.stamp.done { left: 18px; color: var(--done); transform: rotate(-12deg); }
.stamp.cancel { right: 18px; color: var(--cancel); transform: rotate(12deg); }
.stamp.todo { left: 50%; color: var(--todo); transform: translateX(-50%); }
.stamp.skip { left: 50%; top: auto; bottom: 26px; color: var(--skip); transform: translateX(-50%); }
.stamp.clear { left: 50%; top: auto; bottom: 26px; color: var(--clear); transform: translateX(-50%); }
.stamp.triage { right: 18px; color: var(--triage); transform: rotate(12deg); }
.stamp.waiting { left: 18px; color: var(--waiting); transform: rotate(-12deg); }
.stamp.someday { right: 18px; top: auto; bottom: 26px; color: var(--someday); transform: rotate(12deg); }
.stamp.postpone { left: 18px; top: auto; bottom: 26px; color: var(--postpone); transform: rotate(-12deg); }
.card .tint { position: absolute; inset: 0; opacity: 0; pointer-events: none; border-radius: inherit; }

/* Direction hint: appears under the finger when a card is held */
.joystick { position: fixed; inset: 0; pointer-events: none; z-index: 20; opacity: 0; transition: opacity .12s; }
.joystick.show { opacity: 1; }
.joy-ring {
  position: absolute; width: 210px; height: 210px; margin: -105px 0 0 -105px; border-radius: 50%; opacity: .25;
  --up: var(--skip);
  /* the same sectors as directionOf(), clockwise from straight up */
  background: conic-gradient(var(--up) 0deg 30deg, var(--postpone) 30deg 60deg, var(--done) 60deg 120deg,
    var(--waiting) 120deg 150deg, var(--todo) 150deg 210deg, var(--triage) 210deg 240deg, var(--cancel) 240deg 300deg,
    var(--someday) 300deg 330deg, var(--up) 330deg 360deg);
  -webkit-mask: radial-gradient(circle, transparent 61%, #000 62%);
  mask: radial-gradient(circle, transparent 61%, #000 62%);
}
.joystick.clearing .joy-ring { --up: var(--clear); }
.joy-dot { position: absolute; width: 12px; height: 12px; margin: -6px 0 0 -6px; border-radius: 50%; background: var(--text); opacity: .25; }
.joy-label {
  position: absolute; transform: translate(-50%, -50%); white-space: nowrap;
  font-size: 12px; font-weight: 700; line-height: 1; padding: 6px 9px; border-radius: 999px;
  background: var(--surface); border: 2px solid currentColor; opacity: .25;
  transition: opacity .1s, transform .1s;
}
.joy-label.on { opacity: 1; transform: translate(-50%, -50%) scale(1.12); }
.joy-label.off { text-decoration: line-through; }
.joy-label.done { color: var(--done); }
.joy-label.cancel { color: var(--cancel); }
.joy-label.todo { color: var(--todo); }
.joy-label.waiting { color: var(--waiting); }
.joy-label.skip { color: var(--skip); }
.joy-label.someday { color: var(--someday); }
.joy-label.postpone { color: var(--postpone); }
.joy-label.triage { color: var(--triage); }
.joy-label.clear { color: var(--clear); }

/* Empty state */
.empty {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
  text-align: center; gap: 10px; color: var(--muted); padding: 24px;
}
.empty h2 { color: var(--text); margin: 0; font-size: 22px; }
.empty button { border: 1px solid var(--line); background: var(--surface); border-radius: 12px; padding: 10px 16px; font-weight: 600; cursor: pointer; }

/* Tasks | Projects switch (top bar) */
.modes { display: flex; background: var(--surface-2); border-radius: 12px; padding: 3px; gap: 2px; margin: 2px 0 6px; }
.mode {
  flex: 1; border: 0; background: transparent; border-radius: 9px; padding: 8px 4px; cursor: pointer;
  font-size: 15px; font-weight: 600; color: var(--muted);
}
.mode[aria-selected="true"] { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.12); }

/* Toast */
.toast {
  position: fixed; left: 16px; right: 16px; bottom: calc(env(safe-area-inset-bottom) + 72px);
  max-width: 480px; margin: 0 auto;
  background: var(--text); color: var(--bg); border-radius: 14px; padding: 10px 10px 10px 16px;
  display: flex; align-items: center; gap: 10px; font-size: 14px; z-index: 30;
  box-shadow: 0 8px 24px rgba(0,0,0,.25);
}
.toast span { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.toast button { border: 0; background: transparent; color: var(--bg); font-weight: 700; padding: 6px 10px; cursor: pointer; text-decoration: underline; }
.toast.error { background: var(--cancel); color: #fff; }
.toast.error button { color: #fff; }

/* Sheets */
.backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 40; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 50;
  max-height: 88dvh; overflow-y: auto; overscroll-behavior: contain;
  background: var(--surface); border-radius: 20px 20px 0 0;
  padding: 12px 16px calc(env(safe-area-inset-bottom) + 18px);
  max-width: 640px; margin: 0 auto;
  display: flex; flex-direction: column; gap: 12px;
  animation: rise .22s cubic-bezier(.2,.8,.2,1);
}
@keyframes rise { from { transform: translateY(30%); opacity: 0; } }
.grabber { width: 38px; height: 5px; border-radius: 3px; background: var(--line); margin: -4px auto -6px; flex: none; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; min-height: 40px; }
.sheet h2 { margin: 0; font-size: 20px; overflow-wrap: anywhere; }
.sheet .meta { font-size: 13px; color: var(--muted); }
#detail-sheet { padding-bottom: 0; }
.sheet-foot {
  position: sticky; bottom: 0; margin: auto -16px 0; padding: 14px 16px calc(env(safe-area-inset-bottom) + 14px);
  background: linear-gradient(to bottom, transparent, var(--surface) 14px);
}
.detail-body { white-space: pre-wrap; overflow-wrap: anywhere; font-size: 15px; color: var(--text); }
.detail-body:empty::before { content: "No description."; color: var(--muted); }
.postpone-options { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.postpone-options button {
  border: 1px solid var(--line); background: var(--surface-2); border-radius: 12px; padding: 12px; text-align: left; cursor: pointer;
}
.postpone-options b { display: block; font-size: 15px; }
.postpone-options small { color: var(--muted); font-size: 12px; }
.postpone-custom { display: flex; gap: 8px; }
.postpone-custom input { flex: 1; }
.postpone-custom button { flex: none; }
#tidy-sheet { padding-bottom: 0; }
#tidy-all { width: 100%; }
.tidy-intro { margin: -4px 0 0; }
.tidy-summary {
  display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 8px 8px 12px; border-radius: 12px;
  background: color-mix(in srgb, var(--done) 12%, transparent); color: var(--done); font-size: 14px; font-weight: 600;
}
.tidy-summary button { border: 0; background: transparent; color: inherit; font-weight: 700; text-decoration: underline; padding: 6px 8px; cursor: pointer; }
#tidy-error { margin: 0; }
.tidy-job { border-top: 1px solid var(--line); padding: 12px 0 4px; display: flex; flex-direction: column; gap: 8px; }
.tidy-job p { margin: 0; }
.tidy-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.tidy-head h3 { margin: 0; font-size: 16px; }
.tidy-head .count { color: var(--muted); font-size: 14px; font-variant-numeric: tabular-nums; }
.tidy-done { color: var(--done); font-size: 14px; font-weight: 600; }
.tidy-list { list-style: none; margin: 0; padding: 0; }
.tidy-list label { display: flex; align-items: flex-start; gap: 10px; padding: 7px 0; font-size: 14px; cursor: pointer; }
.tidy-list input { width: 20px; height: 20px; margin: 0; padding: 0; flex: none; accent-color: var(--accent); }
.tidy-text { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.tidy-when { color: var(--muted); font-size: 12px; white-space: nowrap; padding-top: 2px; }
.tidy-run {
  align-self: flex-start; border: 1px solid var(--line); background: var(--surface-2); color: var(--text);
  border-radius: 12px; padding: 10px 14px; font-weight: 600; cursor: pointer;
}
.tidy-run:disabled, #tidy-all:disabled { opacity: .5; }
.legend table { width: 100%; border-collapse: collapse; font-size: 14px; }
.legend th, .legend td { text-align: left; padding: 6px 4px; border-bottom: 1px solid var(--line); vertical-align: top; }
.legend td:first-child { white-space: nowrap; }
.legend th { color: var(--muted); font-weight: 600; font-size: 12px; }

@media (min-width: 560px) {
  .deck { width: 100%; max-width: 480px; margin-left: auto; margin-right: auto; }
  header { width: 100%; max-width: 520px; margin: 0 auto; }
}
@media (prefers-reduced-motion: reduce) {
  .card, .sheet { transition: none; animation: none; }
}
