:root {
  --bg: #16161d;
  --surface: #20202a;
  --surface-2: #2a2a36;
  --border: #33333f;
  --text: #f2f2f5;
  --muted: #9a9aab;
  --accent: #7c6cf0;
  --accent-text: #fff;
  --done: #2fbf71;
  --late: #ff5d5d;
  --warn: #f5b041;
  --progress: #4aa3ff;
  --radius: 16px;
  --safe-top: env(safe-area-inset-top);
  --safe-bottom: env(safe-area-inset-bottom);
  color-scheme: dark;
}
@media (prefers-color-scheme: light) {
  :root {
    --bg: #f4f4f7;
    --surface: #ffffff;
    --surface-2: #ececf2;
    --border: #dddde6;
    --text: #1a1a22;
    --muted: #6b6b7b;
    color-scheme: light;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
/* App-shell layout: the page itself never scrolls, only .screen does. iOS home
   screen apps mis-place position:fixed bottom bars until the first scroll, so
   the tab bar is a normal flex child pinned by the layout instead. */
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--bg); color: var(--text); }
#app { height: 100%; display: flex; flex-direction: column; position: relative; }
/* In a home screen app with a translucent status bar, iOS reports the page as
   shorter than the screen by the status bar's height, leaving a gap under the
   tab bar. There's no browser toolbar in that mode, so the large viewport
   height is exactly the screen. */
@media (display-mode: standalone) {
  #app { position: fixed; top: 0; left: 0; right: 0; height: 100vh; height: 100lvh; }
}
body {
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior-y: none;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }

.screen {
  flex: 1; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior-y: contain;
  padding: calc(var(--safe-top) + 12px) max(16px, calc((100% - 640px) / 2)) 96px;
}
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 4px 0 16px; }
.topbar h1 { font-size: 28px; margin: 0; letter-spacing: -0.02em; }
.sub { color: var(--muted); font-size: 14px; }

.seg { display: inline-flex; background: var(--surface-2); border-radius: 999px; padding: 3px; }
.seg button { border: 0; background: none; padding: 6px 12px; border-radius: 999px; font-size: 14px; color: var(--muted); }
.seg button.on { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px #0003; }

.section-title { font-size: 13px; font-weight: 600; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 20px 4px 8px; display: flex; justify-content: space-between; }
.section-title.late { color: var(--late); }

.card {
  display: flex; align-items: center; gap: 12px; width: 100%; text-align: left;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px; margin-bottom: 8px;
}
.card.late { border-color: color-mix(in srgb, var(--late) 60%, var(--border)); }
.card.done .title { text-decoration: line-through; color: var(--muted); }
.card .main { flex: 1; min-width: 0; }
.card .title { font-weight: 600; overflow-wrap: anywhere; }
.card .meta { color: var(--muted); font-size: 13px; margin-top: 2px; display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.check {
  flex: none; width: 34px; height: 34px; border-radius: 50%; border: 2px solid var(--border);
  background: none; display: grid; place-items: center; font-size: 18px; color: transparent;
}
.check.done { background: var(--done); border-color: var(--done); color: #fff; }
.check.skipped, .check.missed { background: var(--surface-2); color: var(--muted); }
.check.in_progress { border-color: var(--progress); color: var(--progress); }
.dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; flex: none; }
.pill { font-size: 12px; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); }
.pill.late { background: color-mix(in srgb, var(--late) 20%, transparent); color: var(--late); }
.pill.in_progress { background: color-mix(in srgb, var(--progress) 20%, transparent); color: var(--progress); }
.pill.snoozed { background: color-mix(in srgb, var(--warn) 20%, transparent); color: var(--warn); }

.quick { display: flex; gap: 8px; margin: 4px 0 6px; }
.quick input { flex: 1; min-width: 0; }
.quick button { flex: none; width: 46px; border: 0; border-radius: 12px; background: var(--accent); color: var(--accent-text); font-size: 20px; }
.quick-preview { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; min-height: 4px; margin-bottom: 4px; }
.linkish { border: 0; background: none; color: var(--accent); font-size: 13px; font-weight: 600; padding: 2px 4px; }
.empty { text-align: center; color: var(--muted); padding: 40px 16px; }
.empty .big { font-size: 40px; margin-bottom: 8px; }

.banner { background: color-mix(in srgb, var(--accent) 18%, var(--surface)); border: 1px solid color-mix(in srgb, var(--accent) 40%, var(--border)); border-radius: var(--radius); padding: 14px; margin-bottom: 12px; }
.banner p { margin: 0 0 10px; }

.tabbar {
  flex: none; position: relative; z-index: 10;
  display: flex; justify-content: space-around;
  padding: 6px 8px calc(var(--safe-bottom) + 6px);
  background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(14px);
  border-top: 1px solid var(--border);
}
.tabbar button { flex: 1; border: 0; background: none; color: var(--muted); font-size: 11px; padding: 6px 0; display: flex; flex-direction: column; align-items: center; gap: 2px; }
.tabbar button .ico { font-size: 22px; line-height: 1; }
.tabbar button { min-width: 0; }
.tabbar button.on { color: var(--accent); }
.fab {
  position: absolute; right: 18px; bottom: calc(var(--safe-bottom) + 76px); z-index: 11;
  width: 58px; height: 58px; border-radius: 50%; border: 0; background: var(--accent); color: var(--accent-text);
  font-size: 32px; line-height: 1; box-shadow: 0 6px 20px #0006;
}

.btn { border: 0; border-radius: 12px; padding: 12px 16px; background: var(--surface-2); font-weight: 600; }
.btn.primary { background: var(--accent); color: var(--accent-text); }
.btn.block { width: 100%; }
.btn.danger { color: var(--late); background: none; }
.btn:disabled { opacity: .5; }

/* Sheets */
.backdrop { position: fixed; inset: 0; background: #0008; z-index: 20; animation: fade .15s; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 21; max-height: 92dvh; overflow-y: auto;
  background: var(--surface); border-radius: 22px 22px 0 0; padding: 10px 16px calc(var(--safe-bottom) + 16px);
  max-width: 640px; margin: 0 auto; animation: up .2s ease-out;
}
.sheet .grab { width: 40px; height: 5px; border-radius: 3px; background: var(--border); margin: 0 auto 12px; }
.sheet h2 { margin: 0 0 4px; font-size: 22px; overflow-wrap: anywhere; }
@keyframes up { from { transform: translateY(40px); opacity: 0; } }
@keyframes fade { from { opacity: 0; } }

.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin: 16px 0; }
.action { border: 0; border-radius: 14px; padding: 18px 10px; font-weight: 600; font-size: 17px; background: var(--surface-2); }
.action.done { background: var(--done); color: #fff; grid-column: span 2; padding: 22px; font-size: 20px; }
.action.progress { color: var(--progress); }
.steps { display: flex; flex-direction: column; gap: 6px; }
.step { display: flex; align-items: center; gap: 12px; text-align: left; border: 0; border-radius: 12px; padding: 12px; background: var(--surface-2); }
.step .box { flex: none; width: 24px; height: 24px; border-radius: 7px; border: 2px solid var(--border); display: grid; place-items: center; font-size: 14px; color: transparent; }
.step.on .box { background: var(--done); border-color: var(--done); color: #fff; }
.step.on span:last-child { text-decoration: line-through; color: var(--muted); }
.step-edit { display: flex; gap: 8px; margin-top: 6px; }
.step-edit input { flex: 1; min-width: 0; }
.step-edit button { flex: none; width: 46px; border: 0; border-radius: 12px; background: var(--surface-2); color: var(--muted); }
.snoozes { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-bottom: 12px; }
.snoozes button { border: 0; border-radius: 12px; padding: 12px 0; background: var(--surface-2); color: var(--warn); font-weight: 600; }

/* Forms */
label.field { display: block; margin: 14px 0; }
label.field > span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 6px; }
input[type=text], input[type=number], input[type=date], input[type=time], textarea, select {
  width: 100%; padding: 12px; border-radius: 12px; border: 1px solid var(--border); background: var(--bg);
  font-size: 16px; -webkit-appearance: none; appearance: none; min-height: 46px;
}
textarea { min-height: 70px; resize: vertical; }
.row { display: flex; gap: 10px; }
.row > * { flex: 1; min-width: 0; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { border: 1px solid var(--border); background: var(--bg); border-radius: 999px; padding: 8px 14px; display: inline-flex; align-items: center; gap: 6px; }
.chip.on { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.chips.days .chip { width: 42px; padding: 8px 0; justify-content: center; }
.every { display: flex; align-items: center; gap: 10px; margin-top: 10px; }
.every input { width: 76px; text-align: center; }

/* Calendar */
.cal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.cal-head button { border: 0; background: var(--surface-2); border-radius: 10px; width: 40px; height: 36px; font-size: 18px; }
.cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; }
.cal-dow { text-align: center; font-size: 12px; color: var(--muted); padding: 4px 0; }
.cal-day {
  aspect-ratio: 1 / 1.05; border: 0; border-radius: 10px; background: var(--surface); padding: 4px;
  display: flex; flex-direction: column; align-items: center; gap: 3px; font-size: 14px;
}
.cal-day.other { opacity: .35; }
.cal-day.today { outline: 2px solid var(--accent); }
.cal-day.sel { background: var(--accent); color: var(--accent-text); }
.cal-dots { display: flex; gap: 2px; flex-wrap: wrap; justify-content: center; max-width: 100%; }
.cal-dots .dot { width: 6px; height: 6px; }

.bar-row { display: flex; align-items: center; gap: 10px; padding: 8px 0 2px; }
.bar-label { width: 96px; flex: none; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; display: flex; align-items: center; gap: 6px; }
.bar { flex: 1; height: 12px; border-radius: 6px; background: var(--surface-2); overflow: hidden; }
.bar span { display: block; height: 100%; border-radius: 6px; min-width: 4px; }
.bar-num { width: 28px; text-align: right; font-weight: 700; }
.bar-note { margin: 0 0 6px 106px; font-size: 13px; }
.list-item { display: flex; justify-content: space-between; align-items: center; padding: 14px 0; border-bottom: 1px solid var(--border); gap: 12px; }
.list-item:last-child { border-bottom: 0; }
.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 4px 16px; margin-bottom: 12px; }
.code { font: 600 26px/1 ui-monospace, Menlo, monospace; letter-spacing: .15em; }

.welcome { flex: 1; min-height: 0; overflow-y: auto; padding: calc(var(--safe-top) + 48px) max(20px, calc((100% - 420px) / 2)) 40px; }
.welcome h1 { font-size: 34px; margin: 0 0 6px; letter-spacing: -0.02em; }
.welcome .lead { color: var(--muted); margin: 0 0 28px; }
.error { color: var(--late); font-size: 14px; min-height: 20px; margin-top: 6px; }

#toast {
  position: fixed; left: 50%; bottom: calc(var(--safe-bottom) + 150px); transform: translateX(-50%) translateY(20px);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 999px; font-size: 14px; font-weight: 600;
  opacity: 0; pointer-events: none; transition: .2s; z-index: 30; white-space: nowrap;
}
#toast.show { opacity: 1; transform: translateX(-50%); }
#toast button { border: 0; background: none; color: var(--accent); font-weight: 700; margin-left: 8px; pointer-events: auto; }

/* Tags */
.tag { font-size: 12px; padding: 2px 8px; border-radius: 999px; background: color-mix(in srgb, var(--accent) 14%, transparent); color: var(--accent); border: 0; white-space: nowrap; }
button.tag { font-size: 14px; padding: 6px 12px; }
.tag.new { background: none; border: 1px dashed var(--accent); }
.tagbar { display: flex; gap: 6px; overflow-x: auto; margin: 0 -16px 12px; padding: 0 16px; scrollbar-width: none; }
.tagbar::-webkit-scrollbar { display: none; }
.tagbar button { flex: none; border: 1px solid var(--border); background: var(--surface); color: var(--muted); border-radius: 999px; padding: 6px 12px; font-size: 14px; }
.tagbar button.on { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.tag-edit { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin-top: 6px; padding: 6px; border: 1px solid var(--border); border-radius: 12px; background: var(--bg); }
.tag-edit input[type=text] { flex: 1; min-width: 120px; width: auto; border: 0; box-shadow: none; background: none; padding: 6px; min-height: 34px; font-size: 16px; outline: none; }
.tag-suggest { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.tag-suggest:empty { display: none; }

/* Loading placeholders */
.card.skeleton .check { border-color: var(--surface-2); }
.bone { height: 14px; border-radius: 7px; background: var(--surface-2); margin: 4px 0 8px; animation: pulse 1.2s ease-in-out infinite; }
.bone.short { width: 30%; height: 10px; }
@keyframes pulse { 50% { opacity: .45; } }
@media (prefers-reduced-motion: reduce) { .bone { animation: none; } }
