:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --surface-2: #fbfaf7;
  --text: #1f2421;
  --muted: #6b716c;
  --line: #e4e0d6;
  --chip: #ebe7dc;
  --accent: #2f6f5e;
  --accent-ink: #ffffff;
  --accent-soft: #dcebe5;
  --danger: #b3412f;
  --warn-bg: #fbeed3;
  --warn-ink: #6d4a0c;
  --shadow: 0 1px 2px rgba(30, 30, 20, .06), 0 3px 10px rgba(30, 30, 20, .05);
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141615;
    --surface: #1e2120;
    --surface-2: #191c1b;
    --text: #e9ebe8;
    --muted: #9aa19c;
    --line: #2c302e;
    --chip: #272b29;
    --accent: #72c2a8;
    --accent-ink: #0d1f19;
    --accent-soft: #1f3530;
    --danger: #ec7a66;
    --warn-bg: #3a2f17;
    --warn-ink: #f1d397;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3);
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--accent); }
img { display: block; max-width: 100%; }
svg.i { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }

/* Top bar */
.bar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 4px;
  padding: calc(var(--safe-top) + 8px) 8px 8px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  -webkit-backdrop-filter: blur(12px); backdrop-filter: blur(12px);
}
.bar h1 { margin: 0 8px; font-size: 26px; font-weight: 700; letter-spacing: -.01em; }
.bar .title { flex: 1; text-align: center; font-weight: 600; }
.bar .spacer { flex: 1; }
.icon-btn {
  display: inline-grid; place-items: center;
  width: 44px; height: 44px; border: 0; border-radius: 12px;
  background: transparent; color: var(--text);
}
.icon-btn:active { background: var(--chip); }
.icon-btn[disabled] { opacity: .35; }
.icon-btn.on { color: var(--accent); }
.text-btn {
  border: 0; background: transparent; color: var(--accent);
  font-weight: 600; padding: 10px 12px; border-radius: 10px;
}
.text-btn.primary { background: var(--accent); color: var(--accent-ink); }
.text-btn[disabled] { opacity: .45; }
.status { font-size: 13px; color: var(--muted); white-space: nowrap; }
.status.err { color: var(--danger); }

/* Search + chips */
.search {
  display: flex; align-items: center; gap: 8px;
  margin: 0 16px 10px; padding: 0 12px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px;
  color: var(--muted);
}
.search input { flex: 1; min-width: 0; border: 0; background: transparent; padding: 11px 0; outline: none; color: var(--text); }
.chips {
  display: flex; gap: 8px; overflow-x: auto; scrollbar-width: none;
  padding: 2px 16px 12px;
}
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; border: 0; border-radius: 999px;
  padding: 7px 13px; background: var(--chip); color: var(--text); font-size: 14px;
}
.chip .n { color: var(--muted); margin-left: 4px; font-size: 12px; }
.chip.on { background: var(--accent); color: var(--accent-ink); }
.chip.on .n { color: inherit; opacity: .75; }

/* Note grid */
.grid {
  display: grid; gap: 10px; align-items: start;
  grid-template-columns: repeat(auto-fill, minmax(158px, 1fr));
  padding: 0 16px calc(var(--safe-bottom) + 96px);
}
.card {
  display: block; text-decoration: none; color: inherit;
  background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow);
  overflow: hidden; border: 1px solid var(--line);
}
.card:active { transform: scale(.985); }
.card .thumb { width: 100%; aspect-ratio: 4 / 3; object-fit: cover; background: var(--chip); }
.card .body { padding: 10px 12px 12px; }
.card h3 { margin: 0 0 4px; font-size: 15.5px; line-height: 1.3; display: flex; gap: 6px; align-items: flex-start; }
.card h3 svg { width: 15px; height: 15px; margin-top: 2px; color: var(--accent); }
.card p { margin: 0; color: var(--muted); white-space: pre-line; font-size: 14px; display: -webkit-box; -webkit-line-clamp: 5; -webkit-box-orient: vertical; overflow: hidden; overflow-wrap: anywhere; }
.card ul.todo { list-style: none; margin: 0; padding: 0; font-size: 14px; color: var(--muted); }
.card ul.todo li { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card .progress { font-size: 12px; color: var(--accent); font-weight: 600; margin-top: 6px; }
.card .tags { margin-top: 8px; display: flex; flex-wrap: wrap; gap: 4px; }
.tag { font-size: 12px; padding: 2px 8px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); }

.empty { text-align: center; color: var(--muted); padding: 48px 24px; grid-column: 1 / -1; }

.fab {
  position: fixed; right: 18px; bottom: calc(var(--safe-bottom) + 18px); z-index: 20;
  display: grid; place-items: center; width: 60px; height: 60px; border: 0; border-radius: 20px;
  background: var(--accent); color: var(--accent-ink); box-shadow: 0 6px 20px rgba(0,0,0,.2);
}
.fab svg { width: 28px; height: 28px; }
.fab[disabled] { opacity: .4; }

.banner {
  margin: 0 16px 10px; padding: 8px 12px; border-radius: 10px;
  background: var(--warn-bg); color: var(--warn-ink); font-size: 14px;
}

/* Note detail */
.note { padding: 4px 20px calc(var(--safe-bottom) + 40px); max-width: 760px; margin: 0 auto; }
.note h1 { font-size: 26px; line-height: 1.2; margin: 4px 0 8px; overflow-wrap: anywhere; }
.meta { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; color: var(--muted); font-size: 13px; margin-bottom: 14px; }
.gallery { display: flex; gap: 8px; overflow-x: auto; margin: 0 -20px 16px; padding: 0 20px; scroll-snap-type: x mandatory; }
.gallery img { height: 220px; width: auto; max-width: 85vw; border-radius: 12px; object-fit: cover; scroll-snap-align: start; background: var(--chip); }
.gallery.single img { width: 100%; max-width: 100%; height: auto; max-height: 60vh; }
.md { font-size: 17px; overflow-wrap: anywhere; }
.md h1, .md h2, .md h3 { line-height: 1.25; margin: 1.2em 0 .4em; }
.md h1 { font-size: 22px; } .md h2 { font-size: 19px; } .md h3 { font-size: 17px; }
.md p { margin: .5em 0; }
.md ul, .md ol { padding-left: 1.3em; margin: .4em 0; }
.md li { margin: .2em 0; }
.md li:has(> input[type=checkbox]), .md li:has(> p > input[type=checkbox]) { list-style: none; margin-left: -1.3em; }
.md input[type=checkbox] {
  width: 22px; height: 22px; margin: 0 10px 0 0; vertical-align: -5px; accent-color: var(--accent);
}
.md li:has(> input:checked), .md li:has(> p > input:checked) { color: var(--muted); text-decoration: line-through; }
.md img { border-radius: 10px; }
.md blockquote { margin: .6em 0; padding-left: 12px; border-left: 3px solid var(--line); color: var(--muted); }
.md code { background: var(--chip); padding: 1px 5px; border-radius: 5px; font-size: .9em; }
.md pre { background: var(--chip); padding: 12px; border-radius: 10px; overflow-x: auto; }
.md pre code { background: none; padding: 0; }
.md table { border-collapse: collapse; width: 100%; }
.md td, .md th { border: 1px solid var(--line); padding: 6px 8px; }
.note-actions { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 24px; }
.ghost-btn {
  border: 1px solid var(--line); background: var(--surface); border-radius: 10px;
  padding: 9px 14px; font-size: 15px; display: inline-flex; gap: 8px; align-items: center;
}
.ghost-btn svg { width: 18px; height: 18px; }
.ghost-btn.danger { color: var(--danger); }
.ghost-btn[disabled] { opacity: .45; }

/* Editor */
.editor { padding: 0 16px calc(var(--safe-bottom) + 40px); max-width: 760px; margin: 0 auto; display: flex; flex-direction: column; gap: 12px; }
.tag-edit {
  display: flex; flex-wrap: wrap; gap: 6px; align-items: center;
  padding: 6px 8px; background: var(--surface); border: 1px solid var(--line); border-radius: 12px;
}
.tag-edit .tag { display: inline-flex; align-items: center; gap: 2px; font-size: 14px; padding: 4px 4px 4px 10px; }
.tag-edit .tag button { border: 0; background: transparent; color: inherit; padding: 0 4px; font-size: 16px; line-height: 1; }
.tag-edit input { flex: 1; min-width: 120px; border: 0; background: transparent; outline: none; padding: 6px 2px; }
.suggest { display: flex; flex-wrap: wrap; gap: 6px; margin-top: -4px; }
.suggest .chip { font-size: 13px; padding: 5px 10px; }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; }
.toolbar .ghost-btn { padding: 8px 12px; }
.content-in {
  width: 100%; min-height: 40vh; resize: none; overflow: hidden;
  border: 1px solid var(--line); border-radius: 12px; background: var(--surface);
  padding: 12px; font-size: 17px; line-height: 1.5; outline: none;
}
.content-in:focus, .tag-edit:focus-within { border-color: var(--accent); }
.img-edit { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 8px; }
.img-edit:empty { display: none; }
.img-edit .it { position: relative; }
.img-edit img { width: 100%; aspect-ratio: 1; object-fit: cover; border-radius: 10px; background: var(--chip); }
.img-edit button {
  position: absolute; top: 4px; right: 4px; width: 28px; height: 28px; border-radius: 50%; border: 0;
  background: rgba(0,0,0,.6); color: #fff; display: grid; place-items: center;
}
.img-edit button svg { width: 16px; height: 16px; }
.hint { color: var(--muted); font-size: 13px; margin: -4px 4px 0; }

/* Login */
.login { max-width: 360px; margin: 0 auto; padding: calc(var(--safe-top) + 12vh) 24px 24px; display: flex; flex-direction: column; gap: 12px; }
.login img { width: 72px; height: 72px; border-radius: 18px; margin-bottom: 8px; }
.login h1 { margin: 0 0 8px; }
.field {
  border: 1px solid var(--line); background: var(--surface); border-radius: 12px; padding: 12px 14px; outline: none; width: 100%;
}
.field:focus { border-color: var(--accent); }
.login .text-btn { padding: 13px; font-size: 16px; }
.err-msg { color: var(--danger); font-size: 14px; min-height: 1.2em; }

/* Lightbox + menu */
.lightbox {
  position: fixed; inset: 0; z-index: 50; background: rgba(0,0,0,.92);
  display: grid; place-items: center; padding: var(--safe-top) 0 var(--safe-bottom);
}
.lightbox img { max-width: 100vw; max-height: 100vh; object-fit: contain; }
.lightbox .nav { position: absolute; top: 50%; transform: translateY(-50%); color: #fff; background: rgba(255,255,255,.12); }
.lightbox .prev { left: 8px; } .lightbox .next { right: 8px; }
.lightbox .close { position: absolute; top: calc(var(--safe-top) + 8px); right: 8px; color: #fff; }
.sheet-bg { position: fixed; inset: 0; z-index: 40; background: rgba(0,0,0,.35); display: flex; align-items: flex-end; justify-content: center; }
.sheet {
  width: 100%; max-width: 520px; background: var(--surface); border-radius: 18px 18px 0 0;
  padding: 16px 16px calc(var(--safe-bottom) + 16px); display: flex; flex-direction: column; gap: 4px;
}
.sheet .who { color: var(--muted); font-size: 14px; padding: 4px 8px 10px; }
.sheet button { text-align: left; border: 0; background: transparent; padding: 14px 8px; border-radius: 10px; font-size: 16px; }
.sheet button:active { background: var(--chip); }
.sheet button.danger { color: var(--danger); }

#toast {
  position: fixed; left: 50%; bottom: calc(var(--safe-bottom) + 92px); z-index: 60;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 14px;
  transition: opacity .2s, transform .2s; max-width: calc(100vw - 32px);
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (min-width: 720px) {
  .grid { grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); max-width: 1100px; margin: 0 auto; }
  .bar, .search, .chips { max-width: 1100px; margin-left: auto; margin-right: auto; }
  .search { margin-left: max(16px, calc(50% - 534px)); margin-right: max(16px, calc(50% - 534px)); }
}
