:root {
  --bg: #f6f4ef;
  --card: #ffffff;
  --text: #1f2328;
  --muted: #6b7280;
  --accent: #0f766e;
  --accent-soft: #0f766e22;
  --amber: #b45309;
  --danger: #b91c1c;
  --border: #e4e1d8;
  --mark: #fde68a;
  --shadow: 0 1px 3px rgb(0 0 0 / 0.08);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17181b;
    --card: #222327;
    --text: #e7e5e0;
    --muted: #9a9a94;
    --accent: #2dd4bf;
    --accent-soft: #2dd4bf22;
    --amber: #fbbf24;
    --danger: #f87171;
    --border: #33343a;
    --mark: #92700c;
    --shadow: 0 1px 3px rgb(0 0 0 / 0.4);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

a { color: var(--accent); }

/* ---------------------------------------------------------------- topbar */

#topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  padding-top: calc(8px + env(safe-area-inset-top));
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}

body[data-route="note"] #topbar,
body[data-route="settings"] #topbar,
body[data-route="setup"] #topbar { display: none; }

body[data-route="note"] #fab,
body[data-route="settings"] #fab,
body[data-route="setup"] #fab { display: none; }

#search {
  flex: 1;
  min-width: 0;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 9px 16px;
  font-size: 15px;
  background: var(--card);
  color: var(--text);
  outline: none;
}

#search:focus { border-color: var(--accent); }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 3px;
  min-width: 40px;
  height: 40px;
  padding: 0 6px;
  border: none;
  border-radius: 10px;
  background: transparent;
  color: var(--text);
  font-size: 18px;
  cursor: pointer;
}

.icon-btn:hover { background: var(--accent-soft); }

.dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  display: inline-block;
}

.dot.ok { background: #16a34a; }
.dot.pending { background: var(--amber); }
.dot.off { background: var(--muted); }
.dot.err { background: var(--danger); }
.dot.busy {
  background: var(--accent);
  animation: pulse 1s infinite alternate;
}

@keyframes pulse { from { opacity: 1; } to { opacity: 0.3; } }

#pending-badge { font-size: 12px; color: var(--amber); }

/* ------------------------------------------------------------------ list */

#view { max-width: 1100px; margin: 0 auto; }

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 12px;
  padding: 14px;
}

@media (max-width: 480px) {
  .cards { grid-template-columns: 1fr; padding: 10px; }
}

.card {
  display: block;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 13px 15px;
  text-decoration: none;
  color: var(--text);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.card:hover { border-color: var(--accent); }

.card h3 {
  margin: 0 0 6px;
  font-size: 15px;
  font-weight: 600;
  overflow-wrap: anywhere;
}

.card .snippet {
  margin: 0 0 8px;
  font-size: 13px;
  color: var(--muted);
  display: -webkit-box;
  -webkit-line-clamp: 4;
  -webkit-box-orient: vertical;
  overflow: hidden;
  overflow-wrap: anywhere;
}

.card .meta {
  display: flex;
  justify-content: space-between;
  font-size: 11.5px;
  color: var(--muted);
}

.badge.pending { color: var(--amber); }
.badge.synced, .badge.offline { color: #16a34a; }
.badge.offline-dot { color: var(--muted); }

.list-info {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  padding: 12px 16px 0;
  color: var(--muted);
  font-size: 13px;
}

.chip {
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--accent);
  border-radius: 999px;
  padding: 6px 14px;
  font-size: 13px;
  cursor: pointer;
}

.chip:hover { border-color: var(--accent); }
.chip.danger { color: var(--danger); }

.empty { color: var(--muted); padding: 24px 16px; text-align: center; }

/* --------------------------------------------------------- search results */

.results { padding: 14px; display: flex; flex-direction: column; gap: 10px; }

.result {
  display: block;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 12px 15px;
  text-decoration: none;
  color: var(--text);
}

.result:hover { border-color: var(--accent); }
.result h3 { margin: 0 0 6px; font-size: 15px; }

.result .hit {
  margin: 4px 0;
  padding: 6px 10px;
  background: var(--bg);
  border-radius: 8px;
  font: 12.5px/1.5 ui-monospace, monospace;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  color: var(--muted);
}

mark { background: var(--mark); color: inherit; border-radius: 3px; padding: 0 1px; }

/* --------------------------------------------------------------- banners */

.banner {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin: 12px 14px 0;
  padding: 10px 14px;
  border: 1px solid var(--danger);
  border-radius: 12px;
  background: color-mix(in srgb, var(--danger) 8%, var(--card));
  font-size: 13.5px;
}

.banner-links { display: flex; gap: 10px; align-items: center; }

.link-btn {
  border: none;
  background: none;
  color: var(--muted);
  cursor: pointer;
  font-size: 13px;
  text-decoration: underline;
  padding: 0;
}

/* -------------------------------------------------------------- note view */

.note-view, .settings { max-width: 760px; margin: 0 auto; padding: 0 14px 90px; }

.note-header {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 0;
  padding-top: calc(10px + env(safe-area-inset-top));
  background: color-mix(in srgb, var(--bg) 90%, transparent);
  backdrop-filter: blur(8px);
}

.note-header h2 {
  flex: 1;
  margin: 0;
  font-size: 17px;
  overflow-wrap: anywhere;
}

.note-header .badge { font-size: 12px; white-space: nowrap; }

.menu {
  display: flex;
  gap: 14px;
  align-items: center;
  padding: 6px 2px 10px;
  font-size: 14px;
}

.menu[hidden] { display: none; }

.menu button.danger {
  border: none;
  background: none;
  color: var(--danger);
  cursor: pointer;
  font-size: 14px;
  padding: 0;
}

/* Unified view: one surface for reading and editing. */

.unified {
  min-height: 55vh;
  padding: 8px 0 30vh; /* generous tap area below the note */
  font-size: 16px;
  line-height: 1.6;
  overflow-wrap: anywhere;
  cursor: text;
}

.unified .ln {
  display: flex;
  align-items: baseline;
  gap: 8px;
  padding: 2px 6px;
  margin: 0 -6px;
  min-height: 1.6em;
  border-radius: 8px;
}

.unified .ln:hover { background: color-mix(in srgb, var(--accent) 5%, transparent); }
.unified .ln.editing { background: var(--accent-soft); }
.unified .ln.editing:hover { background: var(--accent-soft); }

.unified .ln .marker {
  flex: none;
  color: var(--muted);
  user-select: none;
}

.unified .ln .txt { flex: 1; min-width: 0; white-space: pre-wrap; }

.unified .ln.h1 .txt { font-size: 24px; font-weight: 650; }
.unified .ln.h2 .txt { font-size: 20px; font-weight: 650; }
.unified .ln.h3 .txt, .unified .ln.h4 .txt, .unified .ln.h5 .txt, .unified .ln.h6 .txt {
  font-size: 17px;
  font-weight: 650;
}

.unified .ln.done .txt { color: var(--muted); text-decoration: line-through; }

.unified .ln.prop {
  opacity: 0.45;
  font-size: 12.5px;
  font-family: ui-monospace, monospace;
}

.unified .ln.quote {
  border-left: 3px solid var(--accent);
  padding-left: 12px;
  color: var(--muted);
  border-radius: 0;
}

.unified .ln.fence { padding: 0; margin: 4px -6px; }
.unified .ln.fence pre {
  margin: 0;
  width: 100%;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px;
  overflow-x: auto;
  font-size: 13.5px;
  font-family: ui-monospace, monospace;
}

.unified .ln hr {
  width: 100%;
  border: none;
  border-top: 1px solid var(--border);
}

.unified code {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 0 4px;
  font-family: ui-monospace, monospace;
  font-size: 0.9em;
}

.unified img { max-width: 100%; border-radius: 10px; display: block; }
.unified .img-ref { color: var(--muted); font-size: 0.9em; }

.unified input[type="checkbox"] {
  accent-color: var(--accent);
  width: 17px;
  height: 17px;
  cursor: pointer;
  transform: translateY(2px);
}

.line-edit {
  display: block;
  width: 100%;
  border: none;
  outline: none;
  resize: none;
  background: transparent;
  color: var(--text);
  font: inherit;
  line-height: inherit;
  padding: 0;
  overflow: hidden;
}

.wikilink { text-decoration: none; border-bottom: 1px dashed var(--accent); }
.wikilink.missing { color: var(--amber); border-bottom-color: var(--amber); }

.editor {
  width: 100%;
  border: none;
  outline: none;
  resize: none;
  background: transparent;
  color: var(--text);
  font: 16px/1.65 system-ui, -apple-system, sans-serif;
  padding: 4px 0 40vh;
}

/* --------------------------------------------------------------- settings */

.settings section {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 4px 16px 14px;
  margin-bottom: 14px;
}

.settings h3 { margin: 12px 0 4px; font-size: 15px; }
.settings input {
  width: 100%;
  max-width: 420px;
  margin: 6px 0;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
}

.account { display: flex; align-items: center; gap: 10px; }
.avatar { border-radius: 50%; }

.hint { color: var(--muted); font-size: 13px; }
.error { color: var(--danger); font-size: 13.5px; min-height: 1em; }

/* ------------------------------------------------------------------ setup */

.setup {
  max-width: 480px;
  margin: 8vh auto 0;
  padding: 24px 20px;
  text-align: center;
}

.setup h1 { margin: 10px 0 4px; }

.setup input {
  width: 100%;
  padding: 11px 14px;
  margin: 14px 0 10px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--card);
  color: var(--text);
  font-size: 15px;
}

.primary {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 10px;
  padding: 10px 22px;
  font-size: 15px;
  cursor: pointer;
}

.primary:disabled { opacity: 0.5; }

.progress {
  height: 8px;
  border-radius: 4px;
  background: var(--border);
  overflow: hidden;
  margin: 16px 0;
}

#setup-bar {
  height: 100%;
  width: 0;
  background: var(--accent);
  transition: width 0.3s;
}

/* -------------------------------------------------------------------- fab */

#fab {
  position: fixed;
  right: 18px;
  bottom: calc(18px + env(safe-area-inset-bottom));
  width: 58px;
  height: 58px;
  border: none;
  border-radius: 18px;
  background: var(--accent);
  color: #fff;
  box-shadow: 0 4px 14px rgb(0 0 0 / 0.25);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

#fab:active { transform: scale(0.95); }

/* ----------------------------------------------------------------- dialog */

dialog {
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--card);
  color: var(--text);
  padding: 20px 22px;
  width: min(92vw, 380px);
}

dialog::backdrop { background: rgb(0 0 0 / 0.4); }
dialog h3 { margin: 0 0 12px; }

dialog input {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
}

dialog menu {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin: 16px 0 0;
  padding: 0;
}

dialog menu button {
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
  border-radius: 10px;
  padding: 8px 18px;
  cursor: pointer;
}

dialog menu button.primary { background: var(--accent); color: #fff; border: none; }

/* ----------------------------------------------------------------- toasts */

#toasts {
  position: fixed;
  bottom: calc(20px + env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 100;
  width: max-content;
  max-width: 92vw;
}

.toast {
  background: var(--text);
  color: var(--bg);
  padding: 10px 18px;
  border-radius: 12px;
  font-size: 14px;
  box-shadow: var(--shadow);
  animation: toast-in 0.2s ease-out;
}

@keyframes toast-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: none; }
}
