:root {
  --bg: #f4f3fa;
  --card: #ffffff;
  --text: #1d1b2c;
  --muted: #6c6a80;
  --line: #e6e4f0;
  --accent: #5b4bdb;
  --accent-soft: #ebe8fd;
  --danger: #c93a3a;
  --overdue: #c2410c;
  --user-bubble: #5b4bdb;
  --user-text: #ffffff;
  --bot-bubble: #ffffff;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14131c;
    --card: #1e1c2a;
    --text: #ecebf5;
    --muted: #9b99b0;
    --line: #2e2c3d;
    --accent: #8b7dff;
    --accent-soft: #2b2650;
    --danger: #ff6b6b;
    --overdue: #fb923c;
    --user-bubble: #6a5ae6;
    --bot-bubble: #24223a;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: env(safe-area-inset-bottom);
}

button, input, select { font: inherit; color: inherit; }
input, select {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  min-width: 0;
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

button {
  border: 1px solid var(--line);
  background: var(--card);
  border-radius: 10px;
  padding: 10px 14px;
  cursor: pointer;
}
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
button.danger { color: var(--danger); }
button.link { border: 0; background: none; padding: 6px 8px; color: var(--accent); }
button.link.danger { color: var(--danger); }

.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  max-width: 720px;
  margin: 0 auto;
  padding: 16px 16px 4px;
}
.top h1 { font-size: 22px; margin: 0; }

.tabs {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  gap: 4px;
  max-width: 720px;
  margin: 0 auto;
  padding: 8px 16px;
  background: var(--bg);
  overflow-x: auto;
}
.tabs button { flex: 1 0 auto; border: 0; background: none; border-radius: 999px; padding: 8px 10px; font-size: 15px; color: var(--muted); white-space: nowrap; }
.tabs button.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }

main { max-width: 720px; margin: 0 auto; padding: 4px 16px 32px; }
.panel { display: none; }
.panel.active { display: block; }

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 12px 14px;
  margin-bottom: 12px;
}
.card h2 { font-size: 15px; margin: 2px 0 6px; }
.count { color: var(--muted); font-weight: 400; }

.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.add input[name=title] { width: 100%; margin-bottom: 8px; }
.add .row > * { flex: 1 1 110px; }
.add .row > button { flex: 0 0 auto; }

.tasks { list-style: none; margin: 0; padding: 0; }
.task {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 10px 0;
  border-top: 1px solid var(--line);
  cursor: pointer;
}
.task:first-child { border-top: 0; }
.task .body { flex: 1; min-width: 0; }
.task .title { overflow-wrap: anywhere; }
.task.done .title { text-decoration: line-through; color: var(--muted); }
.task .id { color: var(--muted); font-size: 12px; padding-top: 3px; }
.meta { display: flex; flex-wrap: wrap; gap: 8px; font-size: 13px; color: var(--muted); margin-top: 2px; }
.overdue { color: var(--overdue); font-weight: 600; }

.check {
  flex: 0 0 auto;
  width: 24px;
  height: 24px;
  padding: 0;
  border-radius: 50%;
  border: 2px solid var(--accent);
  background: none;
  margin-top: 1px;
}
.check.checked { background: var(--accent); }
.check.checked::after { content: "✓"; color: #fff; font-size: 14px; line-height: 20px; display: block; }

details summary { cursor: pointer; color: var(--muted); }
.empty { color: var(--muted); text-align: center; padding: 24px 0; }
.muted { color: var(--muted); font-size: 14px; }

.text { white-space: pre-wrap; font: inherit; margin: 0; line-height: 1.5; }

.facts { list-style: none; padding: 4px 14px; }
.facts li { display: flex; align-items: center; gap: 4px; padding: 10px 0; border-top: 1px solid var(--line); }
.facts li:first-child { border-top: 0; }
.facts .body { flex: 1; min-width: 0; }

#chat.active { display: flex; flex-direction: column; height: calc(100dvh - 130px); }
.messages { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 6px; padding: 4px 0 12px; }
.msg {
  max-width: 85%;
  padding: 8px 12px;
  border-radius: 16px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.msg.user { align-self: flex-end; background: var(--user-bubble); color: var(--user-text); border-bottom-right-radius: 4px; }
.msg.assistant { align-self: flex-start; background: var(--bot-bubble); border: 1px solid var(--line); border-bottom-left-radius: 4px; }
.msg.typing { color: var(--muted); }
.msg.note { align-self: center; max-width: 95%; font-size: 13px; color: var(--muted); padding: 2px 8px; }
.chat-input { display: flex; gap: 8px; }
.chat-input input { flex: 1; }

dialog {
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--card);
  color: var(--text);
  width: min(480px, calc(100vw - 32px));
  padding: 16px;
}
dialog::backdrop { background: rgb(0 0 0 / 0.4); }
dialog h2 { margin: 0 0 12px; font-size: 18px; }
dialog label { display: flex; flex-direction: column; gap: 4px; font-size: 14px; color: var(--muted); margin-bottom: 10px; flex: 1; }
dialog label input, dialog label select { color: var(--text); }
.actions { margin-top: 8px; }
.grow { flex: 1; }

#toast {
  position: fixed;
  left: 50%;
  bottom: calc(20px + env(safe-area-inset-bottom));
  transform: translateX(-50%) translateY(120px);
  background: var(--text);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: 12px;
  max-width: calc(100vw - 32px);
  transition: transform 0.2s ease;
  z-index: 20;
}
#toast.show { transform: translateX(-50%) translateY(0); }
#toast.error { background: var(--danger); color: #fff; }

body.login { display: grid; place-items: center; min-height: 100vh; padding: 16px; }
body.login .card { text-align: center; padding: 28px 24px; max-width: 360px; }
body.login h1 { font-size: 22px; margin: 0 0 8px; }
body.login p { color: var(--muted); }
