:root {
  --bg: #fafaf8;
  --surface: #ffffff;
  --surface-2: #f1f0ec;
  --ink: #1c1b18;
  --ink-2: #6b6a64;
  --ink-3: #a3a29b;
  --border: #e4e2db;
  --accent: #6c5ce7;
  --accent-soft: rgba(108, 92, 231, 0.12);
  --note-bg: #fffdf5;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.06), 0 8px 24px rgba(0, 0, 0, 0.05);
  --mono: ui-monospace, "SF Mono", Menlo, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #131313;
    --surface: #1b1b1c;
    --surface-2: #232325;
    --ink: #ecebe7;
    --ink-2: #a3a29b;
    --ink-3: #6b6a64;
    --border: #2c2c2f;
    --accent: #8f82ff;
    --accent-soft: rgba(143, 130, 255, 0.16);
    --note-bg: #201f1a;
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.4), 0 8px 24px rgba(0, 0, 0, 0.35);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.65 -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
}

/* ---------- loader overlay ---------- */

#loader-overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  transition: opacity 0.6s ease, background-color 0.5s ease;
}
#loader-overlay.is-ready { cursor: pointer; }
#loader-overlay.is-ready .loader-label {
  color: var(--ink);
  animation: loaderPulse 2.4s ease-in-out infinite;
}
/* Morph phase: page shows through while contours straighten into text lines. */
#loader-overlay.is-entering {
  background: transparent;
  pointer-events: none;
}
#loader-overlay.is-entering .loader-label {
  /* Kill the pulse keyframes: an animation on opacity beats the transition to 0. */
  animation: none;
  opacity: 0;
  transition: opacity 0.3s ease;
}
#loader-overlay.is-exiting { opacity: 0; pointer-events: none; }
#loader-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}
.loader-label {
  position: relative;
  font-family: var(--mono);
  font-size: 13px;
  letter-spacing: 0.4em;
  text-transform: uppercase;
  color: var(--ink-2);
  animation: loaderPulse 1.8s ease-in-out infinite;
}
@keyframes loaderPulse {
  0%, 100% { opacity: 0.35; }
  50% { opacity: 1; }
}

/* Hosted name gate: overlay isn't a click target, the form is.
   Frosted panel lifts the form off the contour canvas. */
#loader-overlay.has-entry { cursor: default; }
.loader-entry {
  position: relative;
  margin-top: 28px;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 16px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: color-mix(in srgb, var(--bg) 55%, transparent);
  backdrop-filter: blur(10px) saturate(1.1);
  -webkit-backdrop-filter: blur(10px) saturate(1.1);
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.08);
}
.loader-name-input {
  width: 200px;
  padding: 8px 12px;
  font-family: var(--mono);
  font-size: 13px;
  letter-spacing: 0.08em;
  color: var(--ink);
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 6px;
  outline: none;
  text-align: center;
  transition: border-color 0.2s ease;
}
.loader-name-input::placeholder { color: var(--ink-2); }
.loader-name-input:focus { border-color: var(--accent); }
.loader-name-input.is-invalid { border-color: var(--accent); animation: entryShake 0.25s ease; }
@keyframes entryShake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-4px); }
  75% { transform: translateX(4px); }
}
.loader-enter-btn {
  padding: 8px 16px;
  font-family: var(--mono);
  font-size: 12px;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  color: var(--ink-2);
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
  transition: color 0.2s ease, border-color 0.2s ease, background-color 0.2s ease;
}
.loader-enter-btn:hover { color: var(--ink); border-color: var(--accent); background: var(--accent-soft); }
#loader-overlay.is-entering .loader-entry,
#loader-overlay.is-exiting .loader-entry {
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease;
}

.exit-picker {
  position: absolute;
  top: 16px;
  right: 16px;
  display: flex;
  gap: 2px;
  padding: 3px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  box-shadow: var(--shadow);
  cursor: default;
}
.exit-option {
  width: 26px;
  height: 24px;
  border: 0;
  border-radius: 7px;
  background: none;
  color: var(--ink-3);
  font-family: var(--mono);
  font-size: 12px;
  cursor: pointer;
}
.exit-option:hover { color: var(--ink); background: var(--surface-2); }
.exit-option.is-selected {
  color: var(--accent);
  background: var(--accent-soft);
}
#loader-overlay.is-entering .exit-picker,
#loader-overlay.is-exiting .exit-picker {
  opacity: 0;
  transition: opacity 0.3s ease;
}

/* ---------- app frame ---------- */

.app {
  display: flex;
  height: 100vh;
  animation: appFadeIn 0.5s ease both;
}
@keyframes appFadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.sidebar {
  width: 270px;
  flex: none;
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--border);
  background: var(--surface);
  overflow: hidden;
  transition: width 0.22s ease;
}
.app.sidebar-collapsed .sidebar {
  width: 0;
  border-right-color: transparent;
}

.sidebar-toggle {
  position: fixed;
  top: 13px;
  left: 232px;
  z-index: 20;
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--ink-2);
  cursor: pointer;
  box-shadow: var(--shadow);
  transition: left 0.22s ease, color 0.15s ease, background 0.15s ease;
}
.sidebar-toggle:hover { color: var(--ink); background: var(--surface-2); }
.app.sidebar-collapsed .sidebar-toggle { left: 12px; }
.sidebar-toggle-icon {
  font-size: 16px;
  line-height: 1;
  transition: transform 0.22s ease;
}
.app.sidebar-collapsed .sidebar-toggle-icon { transform: rotate(180deg); }

.sidebar-header {
  padding: 16px 14px 10px;
  border-bottom: 1px solid var(--border);
}
.wordmark {
  display: block;
  font-family: var(--mono);
  font-size: 12px;
  letter-spacing: 0.25em;
  text-transform: uppercase;
  color: var(--ink-3);
  margin-bottom: 10px;
}
#file-filter {
  width: 100%;
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--ink);
  font: inherit;
  font-size: 13px;
  outline: none;
}
#file-filter:focus { border-color: var(--accent); }

.file-tree {
  flex: 1;
  overflow-y: auto;
  padding: 10px 8px 24px;
}
.tree-group { margin-bottom: 4px; }
.tree-group-label {
  display: flex;
  align-items: center;
  gap: 5px;
  width: 100%;
  padding: 6px 8px 4px;
  border: 0;
  border-radius: 7px;
  background: none;
  font-family: var(--mono);
  font-size: 11px;
  color: var(--ink-3);
  letter-spacing: 0.02em;
  text-align: left;
  cursor: pointer;
}
.tree-group-label:hover { color: var(--ink); background: var(--surface-2); }
.tree-group-name {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.tree-group-label .note-count { margin-left: auto; }
.tree-chevron {
  flex: none;
  display: inline-block;
  transform: rotate(90deg);
  transition: transform 0.15s ease;
  font-size: 13px;
  line-height: 1;
}
.tree-group.is-collapsed .tree-chevron { transform: rotate(0deg); }
.tree-group.is-collapsed .tree-file { display: none; }
.tree-group.is-hidden { display: none; }
/* While filtering, matches show even in collapsed groups; non-matches hide everywhere. */
.file-tree.is-filtering .tree-file { display: flex; }
.file-tree.is-filtering .tree-file.is-filter-hidden { display: none; }
.tree-file {
  display: flex;
  align-items: center;
  gap: 6px;
  width: 100%;
  padding: 5px 8px 5px 16px;
  border: 0;
  border-radius: 7px;
  background: none;
  color: var(--ink-2);
  font: inherit;
  font-size: 13px;
  text-align: left;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.tree-file:hover { background: var(--surface-2); color: var(--ink); }
.tree-file.is-active {
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 500;
}
.tree-file .note-count {
  margin-left: auto;
  font-family: var(--mono);
  font-size: 10px;
  color: var(--accent);
  background: var(--accent-soft);
  border-radius: 99px;
  padding: 1px 6px;
}

/* ---------- main / doc ---------- */

.main { flex: 1; min-width: 0; }
.doc-scroll { height: 100%; overflow-y: auto; }

.doc-layout {
  display: flex;
  gap: 32px;
  max-width: 1180px;
  margin: 0 auto;
  padding: 44px 32px 30vh;
  position: relative;
}

.doc {
  flex: 1;
  min-width: 0;
  max-width: 720px;
}

.margin {
  width: 320px;
  flex: none;
  position: relative;
}

.empty-state {
  color: var(--ink-3);
  font-size: 14px;
  padding-top: 30vh;
  text-align: center;
}

/* rendered markdown */

.doc h1, .doc h2, .doc h3, .doc h4 { line-height: 1.3; letter-spacing: -0.01em; }
.doc h1 { font-size: 28px; margin: 0 0 18px; }
.doc h2 { font-size: 21px; margin: 34px 0 12px; }
.doc h3 { font-size: 17px; margin: 26px 0 8px; }
.doc p, .doc ul, .doc ol { margin: 0 0 14px; }
.doc li { margin-bottom: 4px; }
.doc a { color: var(--accent); }
.doc img { max-width: 100%; }
.doc blockquote {
  margin: 0 0 14px;
  padding: 2px 16px;
  border-left: 3px solid var(--border);
  color: var(--ink-2);
}
.doc code {
  font-family: var(--mono);
  font-size: 0.86em;
  background: var(--surface-2);
  padding: 1px 5px;
  border-radius: 5px;
}
.doc pre {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px 16px;
  overflow-x: auto;
  margin: 0 0 14px;
}
.doc pre code { background: none; padding: 0; }
.doc table { border-collapse: collapse; margin: 0 0 14px; font-size: 14px; }
.doc th, .doc td { border: 1px solid var(--border); padding: 6px 10px; }
.doc hr { border: 0; border-top: 1px solid var(--border); margin: 26px 0; }

/* annotatable blocks */

.doc [data-block] {
  position: relative;
  border-radius: 8px;
  transition: background 0.15s ease;
}
.doc [data-block]::before {
  content: "";
  position: absolute;
  left: -14px;
  top: 2px;
  bottom: 2px;
  width: 3px;
  border-radius: 3px;
  background: transparent;
  transition: background 0.15s ease;
}
.doc [data-block]:hover { background: var(--accent-soft); cursor: pointer; }
.doc [data-block].has-note::before { background: var(--accent); opacity: 0.55; }
.doc [data-block].is-highlighted { background: var(--accent-soft); }
.doc [data-block].is-highlighted::before { background: var(--accent); opacity: 1; }

/* List items are annotated individually; keep the accent bar in the marker gutter. */
.doc li[data-block] { padding-left: 4px; }
.doc li[data-block]::before { left: -22px; }

/* ---------- note cards ---------- */

.note-card {
  position: absolute;
  left: 0;
  width: 100%;
  background: var(--note-bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 10px 12px 8px;
  transition: top 0.25s ease, border-color 0.15s ease;
}
.note-card.is-highlighted { border-color: var(--accent); }
.note-card.is-unanchored { position: static; margin-bottom: 12px; }

.note-card .note-meta {
  display: flex;
  align-items: baseline;
  gap: 8px;
  margin-bottom: 6px;
}
.note-card .note-anchor-quote {
  flex: 1;
  font-size: 11px;
  color: var(--ink-3);
  font-style: italic;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.note-card .note-date {
  font-family: var(--mono);
  font-size: 10px;
  color: var(--ink-3);
}
.note-card .note-delete {
  border: 0;
  background: none;
  color: var(--ink-3);
  font-size: 14px;
  line-height: 1;
  cursor: pointer;
  padding: 2px 4px;
  border-radius: 5px;
}
.note-card .note-delete:hover { color: #e0524d; background: var(--surface-2); }

.note-card textarea {
  width: 100%;
  min-height: 40px;
  border: 0;
  resize: none;
  background: transparent;
  color: var(--ink);
  font: inherit;
  font-size: 13.5px;
  line-height: 1.5;
  outline: none;
  overflow: hidden;
}
.note-card .note-status {
  font-family: var(--mono);
  font-size: 10px;
  color: var(--ink-3);
  height: 12px;
}

.unanchored-tray {
  position: absolute;
  top: 0;
  width: 100%;
}
.unanchored-label {
  font-family: var(--mono);
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.15em;
  color: var(--ink-3);
  margin: 0 0 8px 2px;
}

/* connector from card to its block */

.connector {
  position: absolute;
  height: 2px;
  background: var(--accent);
  opacity: 0.5;
  pointer-events: none;
  border-radius: 2px;
}

/* ---------- hosted mode: read-only owner notes + visitor notes/replies ---------- */

.note-card .note-author {
  flex: 1;
  font-family: var(--mono);
  font-size: 11px;
  color: var(--ink-2);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.note-card .note-author.is-owner {
  color: var(--accent);
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.note-card .note-body {
  font-size: 13.5px;
  line-height: 1.5;
  color: var(--ink);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.note-card.is-owner-note { border-left: 2px solid var(--accent); }
.note-card.is-draft { border-color: var(--accent); }

/* replies: indented thread under the parent note */
.note-replies:not(:empty) {
  margin-top: 8px;
  padding-left: 10px;
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.note-reply-meta {
  display: flex;
  align-items: baseline;
  gap: 8px;
  margin-bottom: 2px;
}
.note-reply-body {
  font-size: 13px;
  line-height: 1.45;
  color: var(--ink);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.note-actions { margin-top: 8px; }
.note-reply-btn {
  border: 0;
  background: none;
  padding: 2px 0;
  font-family: var(--mono);
  font-size: 10px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--ink-3);
  cursor: pointer;
}
.note-reply-btn:hover { color: var(--accent); }

/* composer (new visitor note or reply) */
.note-composer {
  margin-top: 8px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.note-name-input {
  width: 100%;
  padding: 5px 8px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--ink);
  font: inherit;
  font-size: 12.5px;
  outline: none;
}
.note-name-input:focus { border-color: var(--accent); }
.note-composer textarea {
  width: 100%;
  min-height: 40px;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 8px;
  resize: none;
  background: var(--bg);
  color: var(--ink);
  font: inherit;
  font-size: 13.5px;
  line-height: 1.5;
  outline: none;
  overflow: hidden;
}
.note-composer textarea:focus { border-color: var(--accent); }
.note-composer-actions {
  display: flex;
  justify-content: flex-end;
  gap: 6px;
}
.note-btn {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 4px 12px;
  font: inherit;
  font-size: 12px;
  cursor: pointer;
  background: var(--surface);
  color: var(--ink);
}
.note-btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.note-btn-primary:disabled { opacity: 0.55; cursor: default; }
.note-btn-ghost { background: none; color: var(--ink-2); }
.note-btn-ghost:hover { background: var(--surface-2); color: var(--ink); }

@media (max-width: 900px) {
  .sidebar { width: 210px; }
  .margin { width: 240px; }
}
