/* ------------------------------------------------------------------ tokens */
:root {
  color-scheme: light dark;
  --bg: #ffffff;
  --bg-soft: #f7f7f5;
  --bg-hover: #efefed;
  --bg-active: #e6e6e3;
  --text: #37352f;
  --muted: #8a8884;
  --border: #e7e6e2;
  --accent: #2383e2;
  --accent-soft: #e7f3ff;
  --danger: #d44c47;
  --success: #1f8a55;
  --code-bg: #f1f0ee;
  --shadow: 0 8px 28px rgba(15, 15, 15, 0.16), 0 0 0 1px rgba(15, 15, 15, 0.06);
  --font:
    ui-sans-serif, -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica,
    Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #191919;
    --bg-soft: #202020;
    --bg-hover: #2b2b2b;
    --bg-active: #333333;
    --text: #e6e6e4;
    --muted: #8f8f8c;
    --border: #2f2f2f;
    --accent: #529cca;
    --accent-soft: #1f2d3a;
    --danger: #ff7369;
    --success: #4dab9a;
    --code-bg: #2a2a2a;
    --shadow:
      0 8px 28px rgba(0, 0, 0, 0.5), 0 0 0 1px rgba(255, 255, 255, 0.08);
  }
}

* {
  box-sizing: border-box;
}
html,
body {
  height: 100%;
}
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
}
a {
  color: var(--accent);
  text-decoration: none;
}
a:hover {
  text-decoration: underline;
}
h1,
h2,
h3 {
  line-height: 1.25;
}
code {
  font-family: var(--font);
  font-size: 0.9em;
  background: var(--code-bg);
  padding: 0.1em 0.35em;
  border-radius: 4px;
}
[hidden] {
  display: none !important;
}
.muted {
  color: var(--muted);
}
.pad {
  padding: 16px;
}
.right {
  text-align: right;
}
.spacer {
  flex: 1;
}

/* icons (inline SVG) */
.ic {
  width: 16px;
  height: 16px;
  flex: none;
  vertical-align: -3px;
}

/* --------------------------------------------------------------- controls */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 7px 14px;
  font: inherit;
  font-weight: 500;
  cursor: pointer;
  background: var(--bg-hover);
  color: var(--text);
}
.btn:hover {
  background: var(--bg-active);
  text-decoration: none;
}
.btn-sm {
  padding: 4px 10px;
  font-size: 13px;
}
.btn-primary {
  background: var(--accent);
  color: #fff;
}
.btn-primary:hover {
  background: var(--accent);
  filter: brightness(1.08);
}
.btn-ghost {
  background: transparent;
  border-color: var(--border);
}
.btn-danger-ghost {
  background: transparent;
  color: var(--danger);
  border-color: transparent;
}
.btn-danger-ghost:hover {
  background: rgba(212, 76, 71, 0.12);
}
.link-btn {
  background: none;
  border: 0;
  padding: 0;
  font: inherit;
  color: inherit;
  cursor: pointer;
  text-align: left;
}
.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 32px;
  height: 32px;
  padding: 0 8px;
  border: 0;
  background: transparent;
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  border-radius: 6px;
  color: var(--muted);
}
.icon-btn:hover {
  background: var(--bg-hover);
  color: var(--text);
}
.icon-btn.on {
  color: #e8a800;
}

input[type='text'],
input[type='email'],
input[type='password'],
input[type='url'],
input[type='search'],
select {
  width: 100%;
  padding: 8px 10px;
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
}
input:focus,
select:focus,
textarea:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}

.alert {
  padding: 10px 14px;
  border-radius: 6px;
  margin: 12px 0;
  background: var(--bg-soft);
  border: 1px solid var(--border);
}
.alert-error {
  color: var(--danger);
  border-color: var(--danger);
}
.alert-success {
  border-color: var(--success);
}
.field-error {
  display: block;
  color: var(--danger);
  font-size: 13px;
  margin-top: 4px;
}

.toasts {
  position: fixed;
  top: 14px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 50;
  display: grid;
  gap: 8px;
}
.toast {
  background: var(--text);
  color: var(--bg);
  padding: 9px 16px;
  border-radius: 8px;
  box-shadow: var(--shadow);
  font-size: 14px;
  transition: opacity 0.4s;
}
.toast.fade {
  opacity: 0;
}

/* -------------------------------------------------------------- auth pages */
.auth-page {
  display: grid;
  place-items: center;
  min-height: 100%;
  background: var(--bg-soft);
  padding: 20px;
}
.auth-card {
  width: 100%;
  max-width: 380px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 28px;
  box-shadow: var(--shadow);
}
.auth-card h1 {
  margin: 6px 0 16px;
  font-size: 24px;
}
.brand {
  font-weight: 700;
  font-size: 18px;
}
.form {
  display: grid;
  gap: 14px;
}
.form label {
  display: grid;
  gap: 5px;
  font-size: 13px;
  font-weight: 600;
}

/* ------------------------------------------------------------- app layout */
.layout {
  display: grid;
  grid-template-columns: 240px 300px minmax(0, 1fr);
  height: 100vh;
  height: 100dvh;
}
.backdrop {
  display: none;
}

/* sidebar */
.sidebar {
  background: var(--bg-soft);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 10px 8px;
  overflow-y: auto;
}
.workspace {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 4px 12px 8px;
  font-weight: 600;
}
.avatar {
  width: 26px;
  height: 26px;
  flex: none;
  border-radius: 7px;
  background: var(--accent);
  color: #fff;
  display: grid;
  place-items: center;
  font-size: 13px;
}
.ws-name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.sidebar-close {
  flex: none;
}
.search {
  padding: 0 4px 10px;
  margin: 0;
}

.nav {
  display: grid;
  gap: 1px;
}
.nav a,
.sidebar-foot a,
.sidebar-foot .link-btn {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 10px;
  border-radius: 6px;
  color: var(--text);
  font-size: 14px;
  width: 100%;
}
.nav a {
  justify-content: space-between;
}
.nav a > span {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}
.nav a:hover,
.sidebar-foot a:hover,
.sidebar-foot .link-btn:hover {
  background: var(--bg-hover);
  text-decoration: none;
}
.nav a.active {
  background: var(--bg-active);
  font-weight: 600;
}
.nav .ic,
.sidebar-foot .ic {
  color: var(--muted);
}
.nav a.active .ic {
  color: var(--text);
}
.nav em {
  font-style: normal;
  color: var(--muted);
  font-size: 12px;
}
.nav-title {
  margin: 14px 10px 4px;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.sidebar-foot {
  margin-top: auto;
  display: grid;
  gap: 1px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
}
.sidebar-foot form {
  margin: 0;
}
.sidebar-foot a,
.sidebar-foot .link-btn {
  justify-content: flex-start;
}

/* note list */
.notelist {
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
}
.list-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 12px 10px;
}
.list-head h2 {
  margin: 0;
  font-size: 16px;
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.list-head small {
  color: var(--muted);
  font-weight: 400;
  margin-left: 4px;
}
.list-head form {
  margin: 0;
}
.menu-btn {
  display: none;
}
.items {
  overflow-y: auto;
  padding: 0 8px 16px;
}
.note-item {
  display: grid;
  gap: 2px;
  padding: 9px 10px;
  border-radius: 8px;
  color: var(--text);
}
.note-item:hover {
  background: var(--bg-hover);
  text-decoration: none;
}
.note-item.active {
  background: var(--accent-soft);
}
.note-item .t {
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.note-item .star {
  color: #e8a800;
  margin-right: 4px;
  font-weight: 400;
}
.note-item .x {
  color: var(--muted);
  font-size: 13px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.note-item .m {
  color: var(--muted);
  font-size: 12px;
  display: flex;
  align-items: center;
  gap: 6px;
}
.tag {
  padding: 0 7px;
  border-radius: 4px;
  font-size: 11px;
  font-weight: 600;
  text-transform: capitalize;
  background: var(--bg-hover);
}
.tag-inbox {
  background: #e3eefc;
  color: #2b6cb0;
}
.tag-learn {
  background: #e4f4ea;
  color: #2f855a;
}
.tag-idea {
  background: #fdf3d6;
  color: #a0731b;
}
.tag-template {
  background: #eee5fb;
  color: #6b46c1;
}
@media (prefers-color-scheme: dark) {
  .tag-inbox {
    background: #1f2d3a;
    color: #7ab8e8;
  }
  .tag-learn {
    background: #1d2f27;
    color: #6fcf97;
  }
  .tag-idea {
    background: #3a3220;
    color: #e9c46a;
  }
  .tag-template {
    background: #2c2540;
    color: #b794f4;
  }
}

/* ------------------------------------------------------------------ editor */
.editor {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  --doc-w: 1100px;
}
.editor[data-mode='write'],
.editor[data-mode='read'] {
  --doc-w: 800px;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  min-height: 52px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}
.topbar form {
  margin: 0;
}
.back {
  display: none;
  align-items: center;
  justify-content: center;
  min-width: 32px;
  height: 32px;
  border-radius: 6px;
  color: var(--muted);
}
.back:hover {
  background: var(--bg-hover);
  text-decoration: none;
}
.crumb {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 14px;
  color: var(--muted);
  white-space: nowrap;
}
.save-status {
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
  margin-left: 6px;
}
.save-status.dirty {
  color: var(--accent);
}
.save-status.error {
  color: var(--danger);
}

.seg {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: 6px;
  overflow: hidden;
  margin-right: 4px;
}
.seg button {
  border: 0;
  background: transparent;
  color: var(--muted);
  padding: 4px 10px;
  font: inherit;
  font-size: 13px;
  cursor: pointer;
}
.seg button + button {
  border-left: 1px solid var(--border);
}
.seg button.active {
  background: var(--bg-active);
  color: var(--text);
  font-weight: 600;
}

.doc {
  flex: 1;
  overflow-y: auto;
  padding: 0 0 96px;
}

/* cover */
.cover {
  position: relative;
  width: min(100% - 48px, var(--doc-w));
  height: 200px;
  margin: 20px auto 0;
  border-radius: 12px;
  overflow: hidden;
  background: var(--bg-soft);
}
.cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}
.cover-x {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 30px;
  height: 30px;
  padding: 0;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 50%;
  cursor: pointer;
  background: rgba(20, 20, 20, 0.62);
  color: #fff;
  opacity: 0;
  transition:
    opacity 0.15s,
    background 0.15s;
}
.cover:hover .cover-x,
.cover-x:focus-visible {
  opacity: 1;
}
.cover-x:hover {
  background: rgba(20, 20, 20, 0.85);
}
@media (hover: none) {
  .cover-x {
    opacity: 1;
  }
}

/* note properties */
.props,
.toolbar,
.panes {
  width: min(100% - 48px, var(--doc-w));
  margin-inline: auto;
}
.props {
  display: grid;
  grid-template-columns: 150px 150px minmax(0, 1fr);
  gap: 12px 16px;
  align-items: end;
  padding: 18px 0 14px;
  border-bottom: 1px solid var(--border);
}
.prop {
  display: grid;
  gap: 4px;
  min-width: 0;
}
.prop > span,
.prop > label {
  font-size: 12px;
  color: var(--muted);
  font-weight: 600;
}
.cover-row {
  display: flex;
  gap: 8px;
  align-items: stretch;
}
.cover-row input {
  flex: 1;
  min-width: 0;
}
.cover-row .file-btn {
  flex: none;
  cursor: pointer;
  white-space: nowrap;
  color: var(--text);
  font-size: 13px;
  padding: 0 12px;
}
.props .created {
  grid-column: 1 / -1;
  font-size: 12px;
  margin-top: -2px;
}

/* toolbar */
.toolbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 8px 0;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
  scrollbar-width: thin;
}
.toolbar button {
  flex: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-width: 30px;
  height: 30px;
  padding: 0 8px;
  white-space: nowrap;
  border: 0;
  background: transparent;
  color: var(--text);
  border-radius: 5px;
  cursor: pointer;
  font: inherit;
  font-size: 13px;
}
.toolbar button:hover {
  background: var(--bg-hover);
}
.toolbar .sep {
  flex: none;
  width: 1px;
  height: 18px;
  background: var(--border);
  margin: 0 6px;
}

/* writing area */
.panes {
  display: grid;
  gap: 32px;
  padding-top: 22px;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
}
.panes[data-mode='write'],
.panes[data-mode='read'] {
  grid-template-columns: minmax(0, 1fr);
}
.panes[data-mode='write'] #preview,
.panes[data-mode='read'] #editor {
  display: none;
}

#editor {
  width: 100%;
  min-height: 55vh;
  resize: none;
  border: 0;
  background: transparent;
  color: var(--text);
  font: 15px/1.75 var(--font);
  padding: 0;
  overflow: hidden;
  tab-size: 2;
}
#editor:focus {
  outline: none;
}
#preview {
  min-width: 0;
}
.panes[data-mode='split'] #preview {
  border-left: 1px solid var(--border);
  padding-left: 32px;
}

.empty {
  display: grid;
  place-items: center;
}
.empty-state {
  text-align: center;
  max-width: 360px;
  padding: 24px;
}
.empty-state .big {
  display: grid;
  place-items: center;
  margin-bottom: 8px;
  color: var(--muted);
}
.empty-state .big .ic {
  width: 48px;
  height: 48px;
}

/* slash command menu */
.slash-menu {
  position: fixed;
  z-index: 40;
  width: 280px;
  max-height: 320px;
  overflow-y: auto;
  background: var(--bg);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 6px;
}
.slash-menu .cmd {
  display: grid;
  grid-template-columns: 34px 1fr;
  gap: 0 10px;
  align-items: center;
  padding: 6px 8px;
  border-radius: 6px;
  cursor: pointer;
}
.slash-menu .cmd.active,
.slash-menu .cmd:hover {
  background: var(--bg-hover);
}
.slash-menu .ico {
  grid-row: span 2;
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 12px;
  font-weight: 700;
  background: var(--bg);
}
.slash-menu .ttl {
  font-size: 14px;
  font-weight: 600;
}
.slash-menu .dsc {
  font-size: 12px;
  color: var(--muted);
}
.slash-menu .none {
  padding: 8px;
  color: var(--muted);
  font-size: 13px;
}

/* --------------------------------------------------------- markdown output */
.markdown-body {
  line-height: 1.7;
  overflow-wrap: anywhere;
}
.markdown-body > :first-child {
  margin-top: 0;
}
.markdown-body h1 {
  font-size: 2em;
  margin: 1.1em 0 0.4em;
}
.markdown-body h2 {
  font-size: 1.5em;
  margin: 1.1em 0 0.4em;
  padding-bottom: 0.2em;
  border-bottom: 1px solid var(--border);
}
.markdown-body h3 {
  font-size: 1.2em;
  margin: 1em 0 0.3em;
}
.markdown-body p,
.markdown-body ul,
.markdown-body ol,
.markdown-body table,
.markdown-body pre,
.markdown-body blockquote {
  margin: 0.6em 0;
}
.markdown-body ul,
.markdown-body ol {
  padding-left: 1.6em;
}
.markdown-body li > ul,
.markdown-body li > ol {
  margin: 0;
}
.markdown-body blockquote {
  border-left: 3px solid var(--text);
  padding: 0 1em;
  color: var(--muted);
}
.markdown-body pre {
  background: var(--code-bg);
  padding: 14px 16px;
  border-radius: 8px;
  overflow-x: auto;
}
.markdown-body pre code {
  background: none;
  padding: 0;
  font-size: 13.5px;
}
.markdown-body hr {
  border: 0;
  border-top: 1px solid var(--border);
  margin: 1.4em 0;
}
.markdown-body img {
  max-width: 100%;
  border-radius: 6px;
}
.markdown-body table {
  border-collapse: collapse;
  display: block;
  overflow-x: auto;
}
.markdown-body th,
.markdown-body td {
  border: 1px solid var(--border);
  padding: 6px 12px;
}
.markdown-body th {
  background: var(--bg-soft);
  text-align: left;
}
.markdown-body li:has(> input[type='checkbox']) {
  list-style: none;
  margin-left: -1.4em;
}
.markdown-body input[type='checkbox'] {
  margin-right: 8px;
  width: 16px;
  height: 16px;
  vertical-align: -2px;
  accent-color: var(--accent);
  cursor: pointer;
}
.markdown-body input[type='checkbox']:checked + *,
.markdown-body li.done {
  color: var(--muted);
}

/* ------------------------------------------------------- settings / docs */
.page {
  background: var(--bg-soft);
  min-height: 100%;
}
.page-wrap {
  max-width: 860px;
  margin: 0 auto;
  padding: 28px 20px 80px;
}
.page-wrap h1 small {
  color: var(--muted);
  font-weight: 400;
  font-size: 0.6em;
}
.card {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 20px 22px;
  margin: 18px 0;
}
.card h2 {
  margin-top: 0;
}
.inline-form {
  display: flex;
  gap: 10px;
  margin: 14px 0;
}
.table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 8px;
  font-size: 14px;
}
.table th,
.table td {
  text-align: left;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}
.table td form {
  margin: 0;
  display: inline;
}
pre.code,
pre.token {
  background: var(--code-bg);
  padding: 12px 14px;
  border-radius: 8px;
  overflow-x: auto;
  font: 13px/1.55 var(--font);
  margin: 10px 0;
}
pre.token {
  user-select: all;
  font-size: 14px;
}
.docs h3 {
  margin: 26px 0 4px;
  font-size: 16px;
}
.verb {
  display: inline-block;
  min-width: 58px;
  text-align: center;
  padding: 1px 8px;
  border-radius: 4px;
  font: 700 12px var(--font);
  color: #fff;
}
.verb.get {
  background: #2f855a;
}
.verb.post {
  background: #2b6cb0;
}
.verb.put {
  background: #b7791f;
}
.verb.delete {
  background: #c53030;
}

/* ------------------------------------------------- collapsible sidebar (desktop) */
@media (min-width: 861px) {
  body.sidebar-collapsed .layout {
    grid-template-columns: 300px minmax(0, 1fr);
  }
  body.sidebar-collapsed .sidebar {
    display: none;
  }
  body.sidebar-collapsed .menu-btn {
    display: inline-flex;
  }
}

/* ------------------------------------------------------------------ responsive */
@media (max-width: 1100px) {
  .layout {
    grid-template-columns: 220px 280px minmax(0, 1fr);
  }
  body.sidebar-collapsed .layout {
    grid-template-columns: 280px minmax(0, 1fr);
  }
  .panes,
  .panes[data-mode='split'] {
    grid-template-columns: minmax(0, 1fr);
  }
  .panes[data-mode='split'] #preview {
    border-left: 0;
    padding-left: 0;
    border-top: 1px solid var(--border);
    padding-top: 18px;
  }
}

@media (max-width: 860px) {
  .layout {
    grid-template-columns: 1fr;
  }

  /* sidebar becomes a slide-in drawer */
  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: min(84vw, 300px);
    z-index: 30;
    transform: translateX(-100%);
    visibility: hidden;
    transition:
      transform 0.2s ease,
      visibility 0.2s;
    box-shadow: var(--shadow);
  }
  body.sidebar-open .sidebar {
    transform: none;
    visibility: visible;
  }
  body.sidebar-open .backdrop {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 25;
    background: rgba(0, 0, 0, 0.38);
  }
  .menu-btn {
    display: inline-flex;
  }

  /* list and editor take turns on small screens */
  .notelist {
    border-right: 0;
  }
  .has-note .notelist {
    display: none;
  }
  body:not(.has-note) .editor {
    display: none;
  }
  .back {
    display: inline-flex;
  }

  .topbar {
    padding: 8px 10px;
    gap: 4px;
  }
  .topbar .lbl {
    display: none;
  }
  .props,
  .toolbar,
  .panes,
  .cover {
    width: min(100% - 28px, var(--doc-w));
  }
  .cover {
    height: 150px;
    margin-top: 14px;
    border-radius: 10px;
  }
  .props {
    grid-template-columns: 1fr 1fr;
  }
  .prop-cover {
    grid-column: 1 / -1;
  }
}

@media (max-width: 520px) {
  .crumb {
    display: none;
  }
  .seg button {
    padding: 4px 8px;
  }
  #editor {
    font-size: 14.5px;
  }
  .page-wrap {
    padding: 20px 14px 60px;
  }
  .card {
    padding: 16px;
  }
}

.title-input {
  display: block;
  width: 100%;
  border-radius: 0 !important;
  border: 0;
  outline: 0;
  background: transparent;
  color: inherit;
  font: inherit;
  font-size: 2rem;
  font-weight: 700;
  line-height: 1.25;
  padding: 0.5rem 0;
}
.title-input::placeholder {
  opacity: 0.35;
}
