:root {
  --bg: #f5f4f0;
  --panel: #ffffff;
  --panel-2: #faf9f6;
  --text: #1d1c1a;
  --muted: #7a776f;
  --line: #e7e4dd;
  --accent: #4f46e5;
  --accent-2: #7c3aed;
  --accent-soft: #eef0ff;
  --danger: #d94848;
  --done: #2f9e6f;
  --shadow: 0 12px 40px rgba(30, 27, 20, .08);
  --radius: 16px;
  color-scheme: light;
}
html[data-theme="dark"] {
  --bg: #131316;
  --panel: #1c1c21;
  --panel-2: #18181c;
  --text: #ececea;
  --muted: #94918a;
  --line: #2b2b31;
  --accent: #7c83ff;
  --accent-2: #a78bfa;
  --accent-soft: #232438;
  --danger: #f07070;
  --done: #4cd09a;
  --shadow: 0 18px 50px rgba(0, 0, 0, .45);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro SC", "PingFang SC", "Microsoft YaHei", "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; background: none; border: 0; }
input, select { outline: none; }
[hidden] { display: none !important; }

.grain {
  position: fixed; inset: 0; pointer-events: none; z-index: 0;
  background:
    radial-gradient(900px 500px at 12% -10%, color-mix(in srgb, var(--accent) 10%, transparent), transparent 70%),
    radial-gradient(700px 500px at 100% 0%, color-mix(in srgb, var(--accent-2) 8%, transparent), transparent 65%);
}

/* ---------- auth ---------- */
.auth {
  position: relative; z-index: 1;
  min-height: 100dvh; display: grid; place-items: center; padding: 24px;
}
.auth-card {
  width: min(400px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 24px;
  padding: 36px 32px 26px;
  box-shadow: var(--shadow);
  text-align: center;
}
.auth-logo {
  width: 58px; height: 58px; margin: 0 auto 14px;
  display: grid; place-items: center;
  border-radius: 18px; color: #fff; font-size: 30px; font-weight: 700;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  box-shadow: 0 10px 24px color-mix(in srgb, var(--accent) 35%, transparent);
}
.auth h1 { margin: 0 0 6px; font-size: 24px; letter-spacing: .02em; }
.auth-sub { margin: 0 0 22px; color: var(--muted); font-size: 13px; }
.auth-tabs {
  display: grid; grid-template-columns: 1fr 1fr; gap: 4px;
  background: var(--panel-2); border: 1px solid var(--line);
  border-radius: 12px; padding: 4px; margin-bottom: 20px;
}
.auth-tabs button {
  padding: 9px 0; border-radius: 9px; color: var(--muted); font-size: 14px; font-weight: 600;
}
.auth-tabs button.active {
  background: var(--panel); color: var(--text); box-shadow: 0 1px 6px rgba(0,0,0,.08);
}
.field { display: block; text-align: left; margin-bottom: 14px; }
.field span { display: block; font-size: 12px; color: var(--muted); margin: 0 0 7px 3px; font-weight: 600; }
.field input {
  width: 100%; padding: 13px 14px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--panel-2);
  transition: border-color .15s, box-shadow .15s;
}
.field input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 15%, transparent); }
.primary-btn {
  width: 100%; padding: 13px 20px; border-radius: 12px; color: #fff; font-weight: 650;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  box-shadow: 0 8px 20px color-mix(in srgb, var(--accent) 30%, transparent);
  transition: transform .12s, filter .15s;
}
.primary-btn:hover { filter: brightness(1.06); }
.primary-btn:active { transform: scale(.985); }
.primary-btn.small { width: auto; padding: 10px 18px; font-size: 14px; }
.auth-msg { min-height: 18px; margin: 12px 0 0; font-size: 13px; color: var(--danger); }
.auth-msg.hint { color: var(--muted); }
.auth-tip { margin: 20px 0 0; font-size: 11px; color: var(--muted); }
.auth-tip a { color: var(--accent); text-decoration: none; }
.auth-tip a:hover { text-decoration: underline; }

/* ---------- app shell ---------- */
.app {
  position: relative; z-index: 1;
  display: grid; grid-template-columns: 264px 1fr;
  height: 100dvh; padding: 14px; gap: 14px;
}
.sidebar {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 20px 14px; display: flex; flex-direction: column; gap: 16px;
  box-shadow: var(--shadow); overflow: hidden;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 17px; padding: 2px 6px; }
.brand-mark {
  width: 32px; height: 32px; border-radius: 10px; display: grid; place-items: center;
  color: #fff; background: linear-gradient(135deg, var(--accent), var(--accent-2)); font-size: 17px;
}
.new-btn {
  display: flex; align-items: center; gap: 8px; justify-content: center;
  padding: 11px; border-radius: 12px; font-weight: 650; font-size: 14px;
  color: var(--accent); background: var(--accent-soft);
  border: 1px dashed color-mix(in srgb, var(--accent) 35%, transparent);
  transition: background .15s, transform .12s;
}
.new-btn:hover { background: color-mix(in srgb, var(--accent) 14%, var(--panel)); }
.new-btn:active { transform: scale(.98); }
.filters { display: flex; flex-direction: column; gap: 3px; }
.filters button {
  display: flex; align-items: center; gap: 10px; width: 100%;
  padding: 10px 12px; border-radius: 10px; font-size: 14px; color: var(--text);
  transition: background .13s;
}
.filters button:hover { background: var(--panel-2); }
.filters button.active { background: var(--accent-soft); color: var(--accent); font-weight: 650; }
.filters .fi { width: 18px; text-align: center; font-size: 14px; }
.filters em { margin-left: auto; font-style: normal; font-size: 12px; color: var(--muted); font-weight: 500; }
.filters button.active em { color: var(--accent); }
.sidebar-foot { margin-top: auto; display: flex; flex-direction: column; gap: 2px; padding-top: 12px; border-top: 1px solid var(--line); }
.side-link {
  display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: 9px;
  font-size: 13px; color: var(--muted); text-align: left;
}
.side-link:hover { background: var(--panel-2); color: var(--text); }
.side-link.on { color: var(--accent); }

.main {
  position: relative; display: flex; flex-direction: column; min-width: 0;
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); overflow: hidden;
}
.topbar {
  display: flex; align-items: center; gap: 16px;
  padding: 20px 26px 16px; border-bottom: 1px solid var(--line);
}
.topbar-title { min-width: 0; }
.topbar-title h2 { margin: 0; font-size: 21px; letter-spacing: .01em; }
.topbar-title p { margin: 3px 0 0; font-size: 12px; color: var(--muted); }
.search-wrap {
  margin-left: auto; display: flex; align-items: center; gap: 8px;
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 11px;
  padding: 8px 12px; width: min(280px, 40vw);
}
.search-wrap span { font-size: 12px; opacity: .6; }
.search-wrap input { border: 0; background: none; width: 100%; font-size: 13px; }
.icon-btn { width: 36px; height: 36px; border-radius: 10px; display: grid; place-items: center; color: var(--muted); }
.icon-btn:hover { background: var(--panel-2); color: var(--text); }
.menu-btn { display: none; }

.offline-bar {
  padding: 8px 26px; font-size: 12px; color: #9a6b00; background: #fff4d6;
  border-bottom: 1px solid #f0dfae;
}
html[data-theme="dark"] .offline-bar { color: #ffd479; background: #332a13; border-color: #4a3d1a; }

.quick-add {
  display: flex; gap: 10px; padding: 16px 26px 6px;
}
.quick-add input {
  flex: 1; padding: 13px 16px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--panel-2); font-size: 14px; transition: border-color .15s, box-shadow .15s;
}
.quick-add input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 12%, transparent); }
.quick-add button {
  width: 48px; border-radius: 12px; color: #fff; font-size: 22px; line-height: 1;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  box-shadow: 0 6px 16px color-mix(in srgb, var(--accent) 25%, transparent);
}

.todo-list { flex: 1; overflow-y: auto; padding: 12px 26px 40px; scroll-behavior: smooth; }
.todo-list::-webkit-scrollbar { width: 8px; }
.todo-list::-webkit-scrollbar-thumb { background: var(--line); border-radius: 4px; }

.todo {
  display: flex; gap: 12px; padding: 14px 14px 14px 12px;
  border: 1px solid var(--line); border-radius: 13px; margin-bottom: 9px;
  background: var(--panel); transition: border-color .15s, box-shadow .15s, transform .15s;
  animation: pop .18s ease-out;
}
@keyframes pop { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }
.todo:hover { border-color: color-mix(in srgb, var(--accent) 30%, var(--line)); box-shadow: 0 6px 18px rgba(20,20,40,.06); }
.todo.p1 { border-left: 3px solid #6ea8ff; }
.todo.p2 { border-left: 3px solid #f0b23c; }
.todo.p3 { border-left: 3px solid #ef5b5b; }
.todo.done { opacity: .66; }
.todo.done .todo-title { text-decoration: line-through; color: var(--muted); }

.check {
  flex: 0 0 auto; width: 22px; height: 22px; margin-top: 2px; border-radius: 50%;
  border: 2px solid color-mix(in srgb, var(--muted) 45%, transparent);
  display: grid; place-items: center; color: transparent; font-size: 12px; font-weight: 800;
  transition: all .16s;
}
.check:hover { border-color: var(--accent); }
.todo.done .check { background: var(--done); border-color: var(--done); color: #fff; }

.todo-body { flex: 1; min-width: 0; cursor: pointer; }
.todo-title { font-size: 15px; font-weight: 600; line-height: 1.45; word-break: break-word; }
.todo-preview {
  margin-top: 4px; font-size: 12.5px; color: var(--muted); line-height: 1.5;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
  word-break: break-word;
}
.todo-meta { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-top: 7px; }
.badge {
  display: inline-flex; align-items: center; gap: 4px; font-size: 11.5px;
  padding: 3px 8px; border-radius: 999px; background: var(--panel-2); color: var(--muted);
  border: 1px solid var(--line);
}
.badge.remind { color: var(--accent); background: var(--accent-soft); border-color: transparent; }
.badge.overdue { color: var(--danger); background: color-mix(in srgb, var(--danger) 10%, transparent); border-color: color-mix(in srgb, var(--danger) 25%, transparent); }
.badge.hi { color: #b26b00; background: #fff3dd; border-color: transparent; }
html[data-theme="dark"] .badge.hi { color: #ffcb6b; background: #3a2c14; }
.todo-actions { display: flex; gap: 2px; align-items: flex-start; }
.todo-actions button { width: 30px; height: 30px; border-radius: 8px; color: var(--muted); font-size: 13px; }
.todo-actions button:hover { background: var(--panel-2); color: var(--text); }
.todo-actions .del:hover { color: var(--danger); }

.empty { flex: 1; display: grid; place-content: center; text-align: center; padding: 30px; color: var(--muted); }
.empty-icon { font-size: 42px; margin-bottom: 10px; opacity: .8; }
.empty h3 { margin: 0 0 6px; color: var(--text); font-size: 16px; }
.empty p { margin: 0 0 18px; font-size: 13px; }

.mobile-nav { display: none; }

/* ---------- editor dialog ---------- */
.dialog-backdrop {
  position: fixed; inset: 0; z-index: 50; background: rgba(20, 18, 30, .5);
  backdrop-filter: blur(6px); display: grid; place-items: center; padding: 20px;
  animation: fade .15s ease-out;
}
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
.dialog {
  width: min(760px, 100%); max-height: 92dvh; overflow-y: auto;
  background: var(--panel); border: 1px solid var(--line); border-radius: 20px;
  padding: 20px 22px; box-shadow: 0 30px 80px rgba(0,0,0,.3);
  animation: rise .18s ease-out;
}
@keyframes rise { from { opacity: 0; transform: translateY(10px) scale(.99); } to { opacity: 1; transform: none; } }
.dialog-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.dialog-head h3 { margin: 0; font-size: 16px; }
.title-input {
  width: 100%; border: 0; background: none; font-size: 20px; font-weight: 650;
  padding: 6px 2px 12px; border-bottom: 1px solid var(--line); margin-bottom: 14px;
}
.title-input::placeholder { color: var(--muted); opacity: .6; }
.editor-shell { border: 1px solid var(--line); border-radius: 13px; overflow: hidden; background: var(--panel-2); }
.toolbar {
  display: flex; align-items: center; gap: 2px; padding: 7px 8px; overflow-x: auto;
  border-bottom: 1px solid var(--line); background: var(--panel); scrollbar-width: none;
}
.toolbar::-webkit-scrollbar { display: none; }
.toolbar button {
  flex: 0 0 auto; min-width: 30px; height: 30px; padding: 0 6px; border-radius: 7px;
  font-size: 13px; color: var(--muted); display: grid; place-items: center;
}
.toolbar button:hover { background: var(--panel-2); color: var(--text); }
.toolbar button.active { background: var(--accent-soft); color: var(--accent); }
.tb-sep { width: 1px; height: 18px; background: var(--line); margin: 0 3px; flex: 0 0 auto; }
.editor {
  min-height: 180px; max-height: 46dvh; overflow-y: auto; padding: 14px 16px;
  font-size: 14px; line-height: 1.65; outline: none;
}
.editor:empty::before { content: attr(data-placeholder); color: var(--muted); opacity: .6; }
.editor h1, .editor h2 { font-size: 19px; margin: .5em 0 .35em; }
.editor h3, .editor h4 { font-size: 16px; margin: .5em 0 .3em; }
.editor p { margin: .35em 0; }
.editor ul, .editor ol { padding-left: 1.4em; margin: .35em 0; }
.editor blockquote {
  margin: .5em 0; padding: 6px 12px; border-left: 3px solid var(--accent);
  background: var(--accent-soft); border-radius: 0 8px 8px 0; color: var(--muted);
}
.editor pre {
  background: var(--panel); border: 1px solid var(--line); border-radius: 8px;
  padding: 10px 12px; overflow-x: auto; font-size: 12.5px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.editor code { background: var(--panel); border-radius: 4px; padding: 1px 4px; font-size: .92em; }
.editor mark { background: #ffe58a; color: #4a3a00; border-radius: 3px; padding: 0 2px; }
html[data-theme="dark"] .editor mark { background: #6b5a1d; color: #ffe9a8; }
.editor a { color: var(--accent); text-decoration: underline; }
.editor .check-item { display: flex; gap: 8px; align-items: flex-start; margin: .3em 0; }
.editor .check-item input { margin-top: 4px; accent-color: var(--accent); }
.editor .check-item.done { text-decoration: line-through; opacity: .6; }

.dialog-meta {
  display: flex; flex-wrap: wrap; align-items: center; gap: 14px; margin: 16px 0 4px;
}
.meta-field { display: flex; align-items: center; gap: 8px; font-size: 12.5px; color: var(--muted); }
.meta-field input, .meta-field select {
  border: 1px solid var(--line); background: var(--panel-2); border-radius: 9px;
  padding: 8px 10px; font-size: 13px; color: var(--text);
}
.meta-field input:focus, .meta-field select:focus { border-color: var(--accent); }
.ghost-btn {
  padding: 9px 15px; border-radius: 10px; font-size: 13px; color: var(--muted);
  border: 1px solid var(--line); background: var(--panel-2);
}
.ghost-btn:hover { color: var(--text); border-color: color-mix(in srgb, var(--muted) 40%, var(--line)); }
.dialog-foot { display: flex; align-items: center; gap: 10px; margin-top: 18px; padding-top: 16px; border-top: 1px solid var(--line); }
.dialog-foot .space, .dialog-foot .spacer { flex: 1; }
.dialog-foot .primary-btn { width: auto; padding: 10px 24px; }
.danger-btn { padding: 9px 16px; border-radius: 10px; color: var(--danger); font-size: 13px; border: 1px solid color-mix(in srgb, var(--danger) 30%, transparent); }
.danger-btn:hover { background: color-mix(in srgb, var(--danger) 8%, transparent); }

.toast {
  position: fixed; left: 50%; bottom: 34px; transform: translateX(-50%);
  z-index: 99; background: #232330; color: #fff; font-size: 13px;
  padding: 11px 20px; border-radius: 999px; box-shadow: 0 10px 30px rgba(0,0,0,.28);
  animation: toastIn .2s ease-out;
}
@keyframes toastIn { from { opacity: 0; transform: translate(-50%, 8px); } to { opacity: 1; transform: translate(-50%, 0); } }

/* ---------- responsive ---------- */
@media (max-width: 860px) {
  body { overflow: hidden; }
  .app { display: block; padding: 0; height: 100dvh; }
  .sidebar {
    position: fixed; z-index: 40; inset: 0 auto 0 0; width: 270px; border-radius: 0;
    transform: translateX(-102%); transition: transform .22s ease;
    box-shadow: 0 0 60px rgba(0,0,0,.35);
  }
  .sidebar.open { transform: none; }
  .main { height: 100dvh; border: 0; border-radius: 0; }
  .menu-btn { display: grid; }
  .topbar { padding: 14px 16px 12px; gap: 10px; padding-top: max(14px, env(safe-area-inset-top)); }
  .topbar-title h2 { font-size: 18px; }
  .search-wrap { width: 38vw; padding: 7px 10px; }
  .quick-add { padding: 12px 16px 4px; }
  .todo-list { padding: 10px 14px calc(92px + env(safe-area-inset-bottom)); }
  .mobile-nav {
    display: flex; position: fixed; z-index: 30; left: 0; right: 0; bottom: 0;
    background: color-mix(in srgb, var(--panel) 92%, transparent);
    backdrop-filter: blur(14px); border-top: 1px solid var(--line);
    padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
  }
  .mobile-nav button {
    flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px;
    font-size: 16px; color: var(--muted); padding: 5px 0; border-radius: 10px;
  }
  .mobile-nav button span { font-size: 10px; }
  .mobile-nav button.active { color: var(--accent); }
  #mobileNew { color: var(--accent); font-weight: 700; }

  .dialog-backdrop { padding: 0; align-items: end; }
  .dialog {
    width: 100%; max-height: 94dvh; border-radius: 20px 20px 0 0;
    padding: 16px 16px calc(16px + env(safe-area-inset-bottom));
  }
  .title-input { font-size: 18px; }
  .editor { max-height: 40dvh; min-height: 140px; }
  .dialog-meta { gap: 10px; }
  .meta-field { flex: 1 1 100%; justify-content: space-between; }
  .meta-field input, .meta-field select { flex: 1; max-width: 65%; }
}
@media (max-width: 430px) {
  .search-wrap { display: none; }
  .dialog-foot .ghost-btn { padding: 9px 12px; }
  .dialog-foot .primary-btn { padding: 10px 18px; }
}
