/* ===== Design tokens ===== */

:root {
  --bg: #faf9f7;
  --bg-raised: #ffffff;
  --bg-subtle: #f1efeb;
  --fg: #1c1b19;
  --fg-muted: #6b675f;
  --fg-faint: #a29d93;
  --border: #e4e1da;
  --accent: #4056c7;
  --accent-soft: rgba(64, 86, 199, 0.09);
  --danger: #b3372f;
  --danger-soft: rgba(179, 55, 47, 0.08);
  --code-bg: #f2f0ec;
  --shadow: 0 1px 2px rgba(28, 27, 25, 0.05), 0 4px 16px rgba(28, 27, 25, 0.05);

  --hl-comment: #8a857a;
  --hl-keyword: #9c3fb0;
  --hl-string: #22703c;
  --hl-number: #a15c00;
  --hl-title: #2456d6;
  --hl-attr: #8a4f00;

  --font-ui:
    ui-sans-serif, system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial,
    sans-serif;
  --font-mono:
    ui-monospace, 'JetBrains Mono', 'Cascadia Code', 'SF Mono', Menlo, Consolas, monospace;
}

[data-theme='dark'] {
  --bg: #101114;
  --bg-raised: #17181c;
  --bg-subtle: #1d1f24;
  --fg: #e8e6e1;
  --fg-muted: #9d9a92;
  --fg-faint: #63615b;
  --border: #2a2c33;
  --accent: #8fa2ff;
  --accent-soft: rgba(143, 162, 255, 0.12);
  --danger: #e5776f;
  --danger-soft: rgba(229, 119, 111, 0.12);
  --code-bg: #1b1d22;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 6px 24px rgba(0, 0, 0, 0.35);

  --hl-comment: #767c72;
  --hl-keyword: #d68fe8;
  --hl-string: #8fd6a5;
  --hl-number: #e8bd7c;
  --hl-title: #8fb8ff;
  --hl-attr: #e0b070;
}

@media (prefers-color-scheme: dark) {
  html:not([data-theme='light']) {
    --bg: #101114;
    --bg-raised: #17181c;
    --bg-subtle: #1d1f24;
    --fg: #e8e6e1;
    --fg-muted: #9d9a92;
    --fg-faint: #63615b;
    --border: #2a2c33;
    --accent: #8fa2ff;
    --accent-soft: rgba(143, 162, 255, 0.12);
    --danger: #e5776f;
    --danger-soft: rgba(229, 119, 111, 0.12);
    --code-bg: #1b1d22;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 6px 24px rgba(0, 0, 0, 0.35);

    --hl-comment: #767c72;
    --hl-keyword: #d68fe8;
    --hl-string: #8fd6a5;
    --hl-number: #e8bd7c;
    --hl-title: #8fb8ff;
    --hl-attr: #e0b070;
  }
}

/* ===== Base ===== */

* {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font-ui);
  font-size: 16px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

a {
  color: var(--accent);
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
}

/* ===== Header ===== */

.site-header {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 10px 24px;
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  gap: 9px;
  font-weight: 700;
  font-size: 16px;
  letter-spacing: -0.01em;
  color: var(--fg);
}

.brand:hover {
  text-decoration: none;
  color: var(--accent);
}

.brand-mark {
  width: 22px;
  height: 22px;
  color: var(--accent);
}

.header-nav {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.inline-form {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0;
  min-width: 0;
}

.user-email {
  color: var(--fg-muted);
  font-size: 13px;
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ===== Buttons & forms ===== */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 7px 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-raised);
  color: var(--fg);
  font: inherit;
  font-size: 14px;
  font-weight: 500;
  line-height: 1.3;
  cursor: pointer;
  white-space: nowrap;
  transition:
    background-color 0.12s ease,
    border-color 0.12s ease,
    color 0.12s ease;
}

.btn:hover {
  text-decoration: none;
  border-color: var(--fg-faint);
}

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

[data-theme='dark'] .btn-primary {
  color: #101114;
}

@media (prefers-color-scheme: dark) {
  html:not([data-theme='light']) .btn-primary {
    color: #101114;
  }
}

.btn-primary:hover {
  filter: brightness(1.08);
  border-color: var(--accent);
}

.btn-ghost {
  background: transparent;
  border-color: transparent;
  color: var(--fg-muted);
}

.btn-ghost:hover {
  background: var(--bg-subtle);
  border-color: transparent;
  color: var(--fg);
}

.btn-lg {
  padding: 11px 22px;
  font-size: 15px;
  border-radius: 10px;
}

.btn-block {
  width: 100%;
}

.btn-danger {
  color: var(--danger);
}

.btn-danger:hover {
  background: var(--danger-soft);
  border-color: transparent;
  color: var(--danger);
}

.theme-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  padding: 0;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--fg-muted);
  cursor: pointer;
}

.theme-toggle:hover {
  background: var(--bg-subtle);
  color: var(--fg);
}

.theme-toggle svg {
  width: 18px;
  height: 18px;
}

.theme-toggle .icon-sun {
  display: none;
}

[data-theme='dark'] .theme-toggle .icon-sun {
  display: block;
}

[data-theme='dark'] .theme-toggle .icon-moon {
  display: none;
}

@media (prefers-color-scheme: dark) {
  html:not([data-theme]) .theme-toggle .icon-sun {
    display: block;
  }

  html:not([data-theme]) .theme-toggle .icon-moon {
    display: none;
  }
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 16px;
}

.field span {
  font-size: 13px;
  font-weight: 600;
  color: var(--fg-muted);
}

.field input {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-raised);
  color: var(--fg);
  font: inherit;
  font-size: 15px;
}

.field input:focus,
.editor-title:focus,
.editor-textarea:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
  border-color: var(--accent);
}

.form-error {
  margin: 0 0 16px;
  padding: 10px 12px;
  border-radius: 8px;
  background: var(--danger-soft);
  color: var(--danger);
  font-size: 14px;
}

/* ===== Hero (logged out home) ===== */

.hero {
  max-width: 720px;
  margin: 0 auto;
  padding: 96px 24px 64px;
  text-align: center;
}

.hero h1 {
  font-size: clamp(34px, 6vw, 52px);
  line-height: 1.12;
  letter-spacing: -0.03em;
  margin: 0 0 20px;
}

.hero-sub {
  font-size: 18px;
  color: var(--fg-muted);
  max-width: 54ch;
  margin: 0 auto 32px;
}

.hero-actions {
  display: flex;
  justify-content: center;
  gap: 12px;
}

/* ===== Generic page (doc list, history) ===== */

.page {
  max-width: 760px;
  margin: 0 auto;
  padding: 40px 24px 80px;
}

.page-title {
  font-size: 26px;
  letter-spacing: -0.02em;
  margin: 0 0 24px;
}

.breadcrumb {
  margin: 0 0 4px;
  font-size: 14px;
}

.empty-state {
  padding: 56px 24px;
  text-align: center;
  border: 1px dashed var(--border);
  border-radius: 12px;
  color: var(--fg-muted);
}

.empty-state p {
  margin: 0 0 16px;
}

.doc-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.doc-card {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: 10px;
  transition: border-color 0.12s ease;
}

.doc-card:hover {
  border-color: var(--fg-faint);
}

.doc-card-title {
  font-weight: 600;
  color: var(--fg);
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.doc-card-title:hover {
  color: var(--accent);
  text-decoration: none;
}

.doc-card-meta {
  color: var(--fg-faint);
  font-size: 13px;
  white-space: nowrap;
}

.doc-card-actions {
  display: flex;
  gap: 4px;
}

.version-list {
  list-style: none;
  margin: 0;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg-raised);
  overflow: hidden;
}

.version-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
}

.version-row + .version-row {
  border-top: 1px solid var(--border);
}

.version-link {
  display: flex;
  align-items: baseline;
  gap: 12px;
  flex: 1;
  min-width: 0;
  color: var(--fg);
}

.version-link:hover {
  color: var(--accent);
  text-decoration: none;
}

.version-number {
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--fg-muted);
  min-width: 34px;
}

.version-title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.version-date {
  color: var(--fg-faint);
  font-size: 13px;
  white-space: nowrap;
}

.chip {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
}

/* ===== Auth ===== */

.auth-wrap {
  display: flex;
  justify-content: center;
  padding: 72px 24px;
}

.auth-card {
  width: 100%;
  max-width: 380px;
  padding: 32px;
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow);
}

.auth-card h1 {
  margin: 0 0 20px;
  font-size: 22px;
  letter-spacing: -0.02em;
}

.auth-alt {
  margin: 16px 0 0;
  text-align: center;
  font-size: 14px;
  color: var(--fg-muted);
}

/* ===== Editor ===== */

.editor-wrap {
  max-width: 1360px;
  margin: 0 auto;
  padding: 24px 24px 64px;
}

.editor-toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 14px;
}

/* Split editor: markdown source on the left, live preview on the right. */
.editor-split {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
  align-items: start;
}

.editor-preview-pane {
  position: sticky;
  top: 76px;
  min-width: 0;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--bg-raised);
  height: calc(100vh - 108px);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.editor-preview-label {
  flex: none;
  padding: 10px 22px;
  border-bottom: 1px solid var(--border);
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--fg-faint);
  background: color-mix(in srgb, var(--bg-subtle) 50%, transparent);
}

.editor-preview {
  padding: 24px 28px 40px;
  overflow-y: auto;
  scrollbar-width: thin;
}

.editor-preview:empty::after {
  content: 'Nothing to preview yet.';
  color: var(--fg-faint);
  font-style: italic;
}

@media (max-width: 900px) {
  .editor-split {
    grid-template-columns: minmax(0, 1fr);
  }

  .editor-preview-pane {
    position: static;
    height: auto;
    max-height: 60vh;
  }
}

.editor-title {
  flex: 1;
  min-width: 0;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-raised);
  color: var(--fg);
  font: inherit;
  font-size: 15px;
  font-weight: 600;
}

.editor-textarea {
  width: 100%;
  min-height: calc(100vh - 108px);
  padding: 24px 26px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--bg-raised);
  color: var(--fg);
  font-family: var(--font-mono);
  font-size: 14.5px;
  line-height: 1.7;
  resize: none;
  tab-size: 2;
}

.editor-hint {
  margin: 10px 2px 0;
  font-size: 13px;
  color: var(--fg-faint);
}

.editor-hint kbd {
  font-family: var(--font-mono);
  font-size: 11px;
  padding: 1px 5px;
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 4px;
  background: var(--bg-subtle);
}

/* ===== Viewer ===== */

.read-progress {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 2px;
  z-index: 20;
  background: linear-gradient(90deg, var(--accent), color-mix(in srgb, var(--accent) 55%, #d858a7));
  transform: scaleX(0);
  transform-origin: 0 50%;
}

.viewer {
  display: grid;
  grid-template-columns: 250px minmax(0, 1fr);
  gap: 40px;
  max-width: 1280px;
  margin: 0 auto;
  padding: 0 32px;
}

.viewer-side {
  padding-top: 48px;
}

.toc {
  position: sticky;
  top: 76px;
  max-height: calc(100vh - 100px);
  overflow-y: auto;
  padding-right: 8px;
  font-size: 13.5px;
  scrollbar-width: thin;
}

.toc-label {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--fg-faint);
  margin-bottom: 10px;
}

.toc-list {
  list-style: none;
  margin: 0;
  padding: 0;
  border-left: 1px solid var(--border);
}

.toc-item a {
  display: block;
  padding: 4px 12px;
  margin-left: -1px;
  border-left: 2px solid transparent;
  color: var(--fg-muted);
  line-height: 1.45;
  transition:
    color 0.12s ease,
    border-color 0.12s ease;
}

.toc-item a:hover {
  color: var(--fg);
  text-decoration: none;
}

.toc-depth-2 a {
  padding-left: 24px;
}

.toc-depth-3 a {
  padding-left: 38px;
}

.toc-item.is-passed a {
  color: var(--fg-faint);
}

.toc-item.is-active a {
  color: var(--accent);
  font-weight: 600;
  border-left-color: var(--accent);
  background: var(--accent-soft);
}

.viewer-main {
  min-width: 0;
  padding: 48px 0 120px;
  display: flex;
  justify-content: center;
}

/* Centers the reading column in the available space and gives it a
   comfortable measure, so wide screens don't leave the text hugging one side. */
.viewer-col {
  width: 100%;
  max-width: 76ch;
}

.version-banner {
  margin-bottom: 28px;
  padding: 12px 16px;
  border: 1px solid color-mix(in srgb, var(--accent) 35%, var(--border));
  border-radius: 10px;
  background: var(--accent-soft);
  font-size: 14px;
}

.doc-footer {
  margin-top: 56px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
  color: var(--fg-faint);
  font-size: 13px;
}

@media (max-width: 900px) {
  .viewer {
    grid-template-columns: minmax(0, 1fr);
  }

  .viewer-side {
    display: none;
  }
}

/* ===== Rendered markdown ===== */

.prose {
  font-size: 17px;
  line-height: 1.75;
  overflow-wrap: break-word;
}

.viewer-col .prose {
  max-width: none;
}

.prose > :first-child {
  margin-top: 0;
}

.prose h1,
.prose h2,
.prose h3,
.prose h4,
.prose h5,
.prose h6 {
  position: relative;
  font-weight: 700;
  letter-spacing: -0.015em;
  line-height: 1.25;
  scroll-margin-top: 84px;
}

.prose h1 {
  font-size: 2.1em;
  margin: 0 0 0.7em;
}

.prose h2 {
  font-size: 1.5em;
  margin: 1.8em 0 0.6em;
  padding-bottom: 0.3em;
  border-bottom: 1px solid var(--border);
}

.prose h3 {
  font-size: 1.2em;
  margin: 1.6em 0 0.5em;
}

.prose h4,
.prose h5,
.prose h6 {
  font-size: 1em;
  margin: 1.4em 0 0.4em;
}

.heading-anchor {
  position: absolute;
  margin-left: 0.35em;
  font-weight: 400;
  color: var(--fg-faint);
  opacity: 0;
  transition: opacity 0.12s ease;
}

.prose h1:hover .heading-anchor,
.prose h2:hover .heading-anchor,
.prose h3:hover .heading-anchor,
.prose h4:hover .heading-anchor,
.prose h5:hover .heading-anchor,
.prose h6:hover .heading-anchor {
  opacity: 1;
  text-decoration: none;
}

.prose p {
  margin: 0 0 1.15em;
}

.prose ul,
.prose ol {
  margin: 0 0 1.15em;
  padding-left: 1.6em;
}

.prose li {
  margin-bottom: 0.35em;
}

.prose li > ul,
.prose li > ol {
  margin: 0.35em 0 0;
}

.prose blockquote {
  margin: 0 0 1.15em;
  padding: 0.1em 1.2em;
  border-left: 3px solid var(--accent);
  background: var(--bg-subtle);
  border-radius: 0 8px 8px 0;
  color: var(--fg-muted);
}

.prose code {
  font-family: var(--font-mono);
  font-size: 0.875em;
  background: var(--code-bg);
  padding: 0.15em 0.4em;
  border-radius: 5px;
}

.prose pre {
  position: relative;
  margin: 0 0 1.3em;
  padding: 16px 18px;
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow-x: auto;
  line-height: 1.6;
}

.prose pre code {
  background: none;
  padding: 0;
  font-size: 13.5px;
}

.prose pre[data-lang]::before {
  content: attr(data-lang);
  position: absolute;
  top: 8px;
  right: 12px;
  font-family: var(--font-mono);
  font-size: 10.5px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--fg-faint);
}

.prose img {
  max-width: 100%;
  border-radius: 10px;
}

.prose hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 2.2em 0;
}

.prose table {
  border-collapse: collapse;
  margin: 0 0 1.3em;
  width: 100%;
  font-size: 0.925em;
}

.prose th,
.prose td {
  border: 1px solid var(--border);
  padding: 8px 12px;
  text-align: left;
}

.prose th {
  background: var(--bg-subtle);
  font-weight: 600;
}

.prose tr:nth-child(even) td {
  background: color-mix(in srgb, var(--bg-subtle) 45%, transparent);
}

/* ===== Syntax highlighting (highlight.js classes) ===== */

.hljs-comment,
.hljs-quote,
.hljs-meta {
  color: var(--hl-comment);
  font-style: italic;
}

.hljs-keyword,
.hljs-selector-tag,
.hljs-literal,
.hljs-doctag,
.hljs-tag {
  color: var(--hl-keyword);
}

.hljs-string,
.hljs-regexp,
.hljs-addition {
  color: var(--hl-string);
}

.hljs-number,
.hljs-symbol,
.hljs-bullet,
.hljs-link {
  color: var(--hl-number);
}

.hljs-title,
.hljs-section,
.hljs-name,
.hljs-function .hljs-title,
.hljs-title.function_,
.hljs-title.class_ {
  color: var(--hl-title);
}

.hljs-attr,
.hljs-attribute,
.hljs-variable,
.hljs-template-variable,
.hljs-type,
.hljs-selector-class,
.hljs-selector-attr,
.hljs-selector-id,
.hljs-built_in,
.hljs-deletion {
  color: var(--hl-attr);
}

.hljs-emphasis {
  font-style: italic;
}

.hljs-strong {
  font-weight: 700;
}
