:root {
  --bg: #f3ece3;
  --bg-elev: #fffaf4;
  --ink: #241c14;
  --muted: #7a6c5d;
  --line: rgba(36, 28, 20, 0.1);
  --accent: #6b46c1;
  --accent-soft: #efe8ff;
  --danger: #b42318;
  --ok: #067647;
  --shadow: 0 10px 30px rgba(36, 28, 20, 0.08);
  --radius: 16px;
  --tab-h: calc(64px + env(safe-area-inset-bottom));
  --font: "Iowan Old Style", "Palatino Linotype", Palatino, "Times New Roman", serif;
  --ui: ui-rounded, system-ui, -apple-system, "Segoe UI", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161410;
    --bg-elev: #211c17;
    --ink: #f3eadf;
    --muted: #b3a493;
    --line: rgba(243, 234, 223, 0.1);
    --accent: #c4b5fd;
    --accent-soft: #2a2340;
    --danger: #f97066;
    --ok: #75e0a7;
    --shadow: 0 12px 32px rgba(0, 0, 0, 0.35);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  font-family: var(--ui);
  background: var(--bg);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}
button, input, textarea { font: inherit; color: inherit; }
button { background: none; border: 0; padding: 0; cursor: pointer; }
.hidden { display: none !important; }

.screen { min-height: 100%; }
.login-screen {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 32px 20px calc(32px + env(safe-area-inset-bottom));
  background:
    radial-gradient(1200px 500px at 50% -10%, var(--accent-soft), transparent 60%),
    var(--bg);
}
.login-card {
  width: min(100%, 380px);
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: 24px;
  padding: 32px 24px 28px;
  box-shadow: var(--shadow);
  text-align: center;
}
.login-icon { border-radius: 18px; margin-bottom: 12px; }
.login-card h1 {
  font-family: var(--font);
  font-weight: 600;
  letter-spacing: -0.03em;
  font-size: 32px;
  margin: 0 0 6px;
}
.lede { margin: 0 0 24px; color: var(--muted); }
.login-card label {
  display: block;
  text-align: left;
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 14px;
}
.login-card input, .field input, .field textarea, .field select, #search {
  width: 100%;
  margin-top: 6px;
  border: 1px solid var(--line);
  background: var(--bg);
  border-radius: 12px;
  padding: 12px 14px;
  outline: none;
}
.login-card input:focus, .field input:focus, .field textarea:focus, #search:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent);
}
.error { color: var(--danger); font-size: 14px; margin: 0 0 12px; }
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 48px;
  padding: 0 18px;
  border-radius: 14px;
  font-weight: 600;
}
.btn.primary {
  width: 100%;
  background: var(--accent);
  color: #fff;
}
@media (prefers-color-scheme: dark) {
  .btn.primary { color: #1a1328; }
}
.btn.ghost {
  background: var(--accent-soft);
  color: var(--accent);
}
.btn.danger { color: var(--danger); }

#main {
  padding-top: env(safe-area-inset-top);
  padding-bottom: var(--tab-h);
}
.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px 4px;
  position: sticky;
  top: 0;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(16px);
  z-index: 5;
}
.topbar-title { flex: 1; min-width: 0; }
.topbar h1 {
  margin: 0;
  font-family: var(--font);
  font-size: 26px;
  letter-spacing: -0.03em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.crumb {
  margin: 0;
  font-size: 11px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  min-height: 14px;
}
.icon-btn {
  width: 40px;
  height: 40px;
  border-radius: 12px;
  font-size: 20px;
  color: var(--ink);
}
.icon-btn:active { background: var(--line); }

.search-wrap { padding: 8px 16px 4px; }
#search { background: var(--bg-elev); }

.content {
  padding: 8px 16px 24px;
}
.chips {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 8px 0 12px;
  scrollbar-width: none;
}
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none;
  padding: 8px 14px;
  border-radius: 999px;
  background: var(--bg-elev);
  border: 1px solid var(--line);
  font-size: 14px;
}
.chip.active { background: var(--accent); color: #fff; border-color: transparent; }
@media (prefers-color-scheme: dark) {
  .chip.active { color: #1a1328; }
}

.section-label {
  margin: 18px 0 8px;
  font-size: 12px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--muted);
}
.list {
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
}
.row {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  padding: 14px 14px;
  width: 100%;
  text-align: left;
  border-bottom: 1px solid var(--line);
}
.row:last-child { border-bottom: 0; }
.row:active { background: var(--accent-soft); }
.row-ico {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  display: grid;
  place-items: center;
  background: var(--accent-soft);
  color: var(--accent);
  flex: none;
  font-size: 16px;
}
.row h2 {
  margin: 0 0 2px;
  font-size: 16px;
  font-weight: 650;
}
.row p {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.meta { font-size: 12px; color: var(--muted); margin-top: 4px; }

.prose {
  font-family: var(--font);
  font-size: 18px;
  line-height: 1.55;
  padding: 8px 4px 32px;
}
.prose h1, .prose h2, .prose h3 {
  letter-spacing: -0.02em;
  line-height: 1.2;
}
.prose h1 { font-size: 30px; margin: 8px 0 12px; }
.prose h2 { font-size: 22px; margin: 24px 0 8px; }
.prose p { margin: 0 0 12px; }
.prose a { color: var(--accent); }
.prose code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.84em;
  background: var(--accent-soft);
  padding: 0.1em 0.35em;
  border-radius: 6px;
}
.prose pre {
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px;
  overflow-x: auto;
}
.prose pre code { background: none; padding: 0; }
.prose blockquote {
  margin: 0 0 12px;
  padding: 4px 0 4px 14px;
  border-left: 3px solid var(--accent);
  color: var(--muted);
}
.prose table { width: 100%; border-collapse: collapse; font-size: 15px; }
.prose th, .prose td { border-bottom: 1px solid var(--line); padding: 8px 6px; text-align: left; }
.frontmatter {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: 0 0 16px;
}
.tag {
  font-family: var(--ui);
  font-size: 12px;
  padding: 4px 8px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
}
.wiki {
  color: var(--accent);
  text-decoration: none;
  border-bottom: 1px dashed color-mix(in srgb, var(--accent) 50%, transparent);
}

.editor {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.editor textarea {
  min-height: calc(100dvh - 260px);
  border-radius: 16px;
  border: 1px solid var(--line);
  background: var(--bg-elev);
  padding: 16px;
  resize: vertical;
  line-height: 1.5;
}
.actions {
  display: flex;
  gap: 8px;
  position: sticky;
  bottom: calc(var(--tab-h) + 8px);
}
.actions .btn { flex: 1; }

.tabbar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  height: var(--tab-h);
  padding: 6px 8px env(safe-area-inset-bottom);
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  background: color-mix(in srgb, var(--bg-elev) 92%, transparent);
  border-top: 1px solid var(--line);
  backdrop-filter: blur(18px);
  z-index: 6;
}
.tab {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 600;
}
.tab.active { color: var(--accent); }
.tab-ico { font-size: 20px; line-height: 1; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--tab-h) + 12px);
  transform: translateX(-50%);
  background: var(--ink);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 14px;
  z-index: 20;
  max-width: 90%;
}
.sheet {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 30;
  display: grid;
  align-items: end;
}
.sheet-card {
  background: var(--bg);
  border-radius: 20px 20px 0 0;
  padding: 12px 16px calc(20px + env(safe-area-inset-bottom));
}
.sheet-card button {
  width: 100%;
  text-align: left;
  padding: 16px 8px;
  border-bottom: 1px solid var(--line);
  font-size: 16px;
}
.empty {
  text-align: center;
  color: var(--muted);
  padding: 40px 16px;
}
.note-toolbar {
  display: flex;
  gap: 8px;
  margin: 8px 0 4px;
}
.note-toolbar .btn { min-height: 40px; padding: 0 14px; width: auto; }

@media (min-width: 720px) {
  #main { max-width: 720px; margin: 0 auto; }
  .tabbar, .topbar { max-width: 720px; left: 50%; right: auto; transform: translateX(-50%); width: 100%; }
}
