:root {
  --bg: #1a1d28;
  --surface: #22263a;
  --line: #2f3346;
  --text: #e6e8f0;
  --text-dim: #8a8f9e;
  --text-faint: #5a5e6e;
  --bullet: #9aa0b0;
  --accent: #7ca6ff;
  --danger: #ef6e6e;
  --tag: #9bd6a5;
  --date: #e3b97a;
  --done: #5a6072;
  --link: #7ca6ff;
  --radius: 6px;
  --row-height: 30px;
  --indent: 24px;
  --guide: #353a4d;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.65 -apple-system, "SF Pro Text", "Inter", ui-sans-serif, system-ui, "Segoe UI", Roboto, sans-serif;
  min-height: 100%;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

a { color: var(--link); text-decoration: none; }
a:hover { text-decoration: underline; }

button {
  font: inherit;
  color: inherit;
  background: transparent;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 6px 10px;
  cursor: pointer;
}
button:hover { border-color: var(--text-dim); }
button.primary {
  background: var(--accent);
  color: #0b0d10;
  border-color: var(--accent);
  font-weight: 600;
}
button.primary:hover { filter: brightness(1.05); }

.app { min-height: 100vh; }

/* ---- Auth screens ---- */
.auth-shell {
  max-width: 380px;
  margin: 12vh auto;
  padding: 28px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
}
.auth-shell h1 {
  margin: 0 0 14px;
  font-size: 22px;
  letter-spacing: -0.01em;
}
.auth-shell p.hint { color: var(--text-dim); margin: 0 0 18px; }
.auth-shell label { display: block; margin-bottom: 10px; }
.auth-shell label span { display: block; color: var(--text-dim); margin-bottom: 4px; font-size: 13px; }
.auth-shell input[type=email],
.auth-shell input[type=password] {
  width: 100%;
  padding: 10px 12px;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  color: var(--text);
  font: inherit;
}
.auth-shell input:focus { outline: none; border-color: var(--accent); }
.auth-shell .row { display: flex; gap: 8px; margin-top: 16px; align-items: center; }
.auth-shell .row .grow { flex: 1; }
.error-banner {
  background: rgba(239, 110, 110, 0.1);
  border: 1px solid var(--danger);
  color: var(--danger);
  padding: 8px 10px;
  border-radius: var(--radius);
  margin-bottom: 12px;
  font-size: 13px;
}

/* ---- Top navigation bar ---- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 22px;
  background: var(--bg);
}
.topbar .group {
  display: flex;
  align-items: center;
  gap: 4px;
}
.icon-btn {
  width: 36px;
  height: 36px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--text-dim);
  background: transparent;
  border: 0;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.12s ease, color 0.12s ease;
}
.icon-btn:hover { background: var(--surface); color: var(--text); }
.icon-btn:disabled { opacity: 0.35; cursor: default; background: transparent; color: var(--text-dim); }
.icon-btn.active { color: var(--text); background: var(--surface); }
.icon-btn svg { width: 20px; height: 20px; }

/* Dropdown menu (sign out etc.) */
.menu-pop {
  position: absolute;
  right: 22px;
  top: 56px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 6px;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.35);
  z-index: 20;
  min-width: 160px;
}
.menu-pop button {
  display: block;
  width: 100%;
  text-align: left;
  border: 0;
  padding: 8px 10px;
  color: var(--text);
  border-radius: 6px;
}
.menu-pop button:hover { background: var(--bg); }
.menu-pop .muted { color: var(--text-dim); font-size: 11px; padding: 6px 10px 2px; text-transform: uppercase; letter-spacing: 0.04em; }
.menu-sep { height: 1px; background: var(--line); margin: 4px 0; }

.save-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  margin: 0 8px 0 4px;
  transition: background 0.2s ease;
  flex-shrink: 0;
}
.save-dot.idle { background: var(--text-faint); }
.save-dot.saving { background: var(--date); animation: pulse 1.2s ease-in-out infinite; }
.save-dot.offline { background: var(--text-dim); box-shadow: 0 0 0 2px rgba(138, 143, 158, 0.18); }
.save-dot.error { background: #d97a7a; animation: pulse 0.9s ease-in-out infinite; }
@keyframes pulse {
  0%, 100% { opacity: 0.4; }
  50% { opacity: 1; }
}

/* ---- Main shell ---- */
.shell {
  max-width: 760px;
  margin: 0 auto;
  padding: 0 22px 140px;
}
.breadcrumbs {
  display: none; /* Nav is via back/forward/home icons; breadcrumbs kept for deep URLs only */
  flex-wrap: wrap;
  gap: 4px;
  align-items: center;
  color: var(--text-dim);
  font-size: 13px;
  margin: 6px 0 0;
}
.breadcrumbs.visible { display: flex; }
.breadcrumbs .crumb { cursor: pointer; }
.breadcrumbs .crumb:hover { color: var(--text); }
.breadcrumbs .sep { opacity: 0.5; }

.focus-header {
  padding: 130px 0 26px;
  font-size: 26px;
  font-weight: 600;
  letter-spacing: -0.015em;
  color: var(--text);
}
.focus-header.hidden-title { padding: 90px 0 16px; }
.focus-header .title-text {
  outline: none;
  display: inline-block;
  min-width: 80px;
}
.focus-header .title-text:empty::before {
  content: attr(data-placeholder);
  color: var(--text-faint);
}

/* ---- Outline tree ---- */
.node-list { padding: 0; }
.children {
  position: relative;
  margin-left: 11px;
  padding-left: 16px;
}
.children::before {
  content: "";
  position: absolute;
  left: 0;
  top: 0;
  bottom: 6px;
  width: 1px;
  background: var(--guide);
}

.row {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  position: relative;
  min-height: var(--row-height);
  padding: 1px 0;
}
.row .disclosure {
  position: absolute;
  left: -20px;
  top: 4px;
  width: 18px;
  height: 22px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--text-dim);
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.12s ease;
  user-select: none;
  font-size: 12px;
}
.row:hover .disclosure { opacity: 0.7; }
.row .disclosure:hover { opacity: 1; color: var(--text); }

.row .bullet {
  flex-shrink: 0;
  margin-top: 9px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--bullet);
  cursor: pointer;
  transition: transform 0.1s ease, box-shadow 0.12s ease;
}
.row .bullet:hover { transform: scale(1.15); }
.row .bullet.has-collapsed {
  background: transparent;
  box-shadow: 0 0 0 2px var(--bullet);
}

.row .text {
  flex: 1;
  padding: 2px 2px;
  min-height: 24px;
  line-height: 1.55;
  outline: none;
  white-space: pre-wrap;
  word-wrap: break-word;
  caret-color: var(--accent);
  color: var(--text);
}
.row .text:empty::before {
  content: attr(data-placeholder);
  color: var(--text-faint);
  pointer-events: none;
}
.row.completed .text { color: var(--done); text-decoration: line-through; }
.row .text .tag { color: var(--tag); }
.row .text .date-token { color: var(--date); }
.row .text .link { color: var(--link); text-decoration: underline; }
.row .text .link:hover { cursor: pointer; }

.row .text .link-ref { color: var(--link); }
.row .text .link-ref-display { text-decoration: underline; }
.row .text .link-ref-meta { font-size: 0.7em; opacity: 0.45; letter-spacing: -0.04em; }
.row .text .link-ref:hover { cursor: pointer; }

.link-popover {
  position: absolute;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  min-width: 220px;
  max-width: 360px;
  max-height: 240px;
  overflow-y: auto;
  z-index: 1000;
  display: none;
  font-size: 0.92em;
}
.link-popover-item {
  padding: 6px 10px;
  cursor: pointer;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.link-popover-item.selected,
.link-popover-item:hover { background: var(--line); }
.link-popover-empty {
  padding: 8px 10px;
  color: var(--text-dim);
  font-style: italic;
}

.backlinks {
  margin-top: 32px;
  padding-top: 16px;
  border-top: 1px solid var(--line);
}
.backlinks-heading {
  color: var(--text-dim);
  font-size: 0.85em;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: 8px;
}
.backlinks-list { display: flex; flex-direction: column; gap: 4px; }
.backlinks-item {
  padding: 6px 8px;
  border-radius: var(--radius);
  color: var(--text);
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.backlinks-item:hover { background: var(--surface); }

.row.drop-above { box-shadow: 0 -2px 0 0 var(--accent) inset; }
.row.drop-below { box-shadow: 0 2px 0 0 var(--accent) inset; }
.row.dragging { opacity: 0.4; }

/* Add-note trailing button: just a plus */
.add-note {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 8px 0 0;
  padding: 2px 0;
  color: var(--text-faint);
  border: 0;
  background: transparent;
  font: inherit;
  cursor: pointer;
}
.add-note:hover { color: var(--text-dim); }
.add-note .plus {
  width: 22px;
  height: 22px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  line-height: 1;
}

/* ---- Search panel ---- */
.search-panel {
  padding: 24px 22px 80px;
  max-width: 760px;
  margin: 0 auto;
}
.search-input {
  width: 100%;
  padding: 12px 14px;
  font: inherit;
  font-size: 17px;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
}
.search-input:focus { outline: none; border-color: var(--accent); }
.search-results {
  margin-top: 20px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.search-results .hit {
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  cursor: pointer;
  background: var(--surface);
  transition: border-color 0.12s ease;
}
.search-results .hit:hover { border-color: var(--text-dim); }
.search-results .hit .text { color: var(--text); }
.search-results .hit .path {
  color: var(--text-dim);
  font-size: 12px;
  margin-top: 4px;
}
.search-results .empty {
  color: var(--text-dim);
  padding: 20px;
  text-align: center;
}

.hidden { display: none !important; }
.muted { color: var(--text-dim); }

/* ---- Toasts ---- */
.toast-container {
  position: fixed;
  bottom: 16px;
  right: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 100;
  pointer-events: none;
}
.toast {
  pointer-events: auto;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px 10px 14px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--text);
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.35);
  font-size: 14px;
  min-width: 220px;
  animation: toast-in 0.16s ease-out;
}
.toast.leaving { animation: toast-out 0.18s ease-in forwards; }
@keyframes toast-in {
  from { transform: translateY(8px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}
@keyframes toast-out {
  to { transform: translateY(8px); opacity: 0; }
}
.toast-message { flex: 1; }
.toast-action {
  background: transparent;
  border: 0;
  padding: 2px 6px;
  color: var(--accent);
  font: inherit;
  cursor: pointer;
  border-radius: 4px;
}
.toast-action:hover { background: rgba(124, 166, 255, 0.1); }
.toast-close {
  background: transparent;
  border: 0;
  padding: 0 2px;
  color: var(--text-dim);
  font-size: 18px;
  cursor: pointer;
  line-height: 1;
}
.toast-close:hover { color: var(--text); }

/* ---- Command palette ---- */
.palette-overlay {
  position: fixed;
  inset: 0;
  background: rgba(10, 12, 18, 0.5);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding-top: 14vh;
  z-index: 80;
  animation: palette-in 0.12s ease-out;
}
@keyframes palette-in {
  from { opacity: 0; }
  to { opacity: 1; }
}
.palette-box {
  width: min(640px, 94vw);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.5);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
.palette-input {
  width: 100%;
  padding: 14px 16px;
  background: transparent;
  border: 0;
  border-bottom: 1px solid var(--line);
  color: var(--text);
  font: inherit;
  font-size: 16px;
}
.palette-input:focus { outline: none; }
.palette-results {
  max-height: 46vh;
  overflow-y: auto;
}
.palette-empty {
  padding: 16px;
  text-align: center;
  color: var(--text-dim);
  font-size: 13px;
}
.palette-hit {
  padding: 10px 16px;
  border-left: 2px solid transparent;
  cursor: pointer;
}
.palette-hit.selected {
  background: var(--bg);
  border-left-color: var(--accent);
}
.palette-hit .text { color: var(--text); }
.palette-hit .path { color: var(--text-dim); font-size: 12px; margin-top: 2px; }
.palette-hint {
  display: flex;
  gap: 14px;
  padding: 8px 16px;
  color: var(--text-dim);
  font-size: 11px;
  border-top: 1px solid var(--line);
}

/* ---- Modal (import/export) ---- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(10, 12, 18, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px 16px;
  z-index: 90;
  animation: palette-in 0.12s ease-out;
}
.modal-box {
  width: min(640px, 100%);
  max-height: 100%;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.5);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
}
.modal-title { font-weight: 600; letter-spacing: -0.01em; }
.modal-close {
  background: transparent;
  border: 0;
  color: var(--text-dim);
  font-size: 22px;
  line-height: 1;
  padding: 0 4px;
  cursor: pointer;
}
.modal-close:hover { color: var(--text); }
.modal-body {
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow-y: auto;
}
.modal-hint { color: var(--text-dim); font-size: 12px; }
.modal-textarea {
  width: 100%;
  min-height: 260px;
  max-height: 50vh;
  padding: 10px 12px;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  color: var(--text);
  font: 13px/1.55 ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  resize: vertical;
}
.modal-textarea:focus { outline: none; border-color: var(--accent); }
.modal-footer {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px 14px;
  border-top: 1px solid var(--line);
}
.modal-footer .modal-status {
  flex: 1;
  color: var(--text-dim);
  font-size: 12px;
}

/* ---- Mobile ---- */
@media (max-width: 640px) {
  .shell { padding: 0 16px 140px; }
  .topbar { padding: 10px 12px; }
  .focus-header { padding: 80px 0 20px; font-size: 22px; }
  .row .text { font-size: 16px; }
  .icon-btn { width: 42px; height: 42px; }
  .icon-btn svg { width: 22px; height: 22px; }
  .children { padding-left: 14px; }
  .row .disclosure { left: -18px; }
  .modal-overlay { padding: 12px; }
  .modal-textarea { min-height: 200px; }
}
