:root {
  color-scheme: light dark;
  --paper: #e3e8e4;
  --card: #fbfcfb;
  --ink: #1c2624;
  --muted: #5d6b67;
  --line: #cdd6d1;
  --accent: #1d5c72;
  --accent-soft: #d6e6ec;
  --spine: #1d5c72;
  --tag-bg: #efe3c2;
  --tag-ink: #5a4411;
  --mark: #fbe7a1;
  --danger: #a3322a;
  --code-bg: #eef1ef;
  --shadow: 0 1px 2px rgba(20, 40, 36, 0.08), 0 8px 24px rgba(20, 40, 36, 0.06);
  --serif: Charter, "Bitstream Charter", "Sitka Text", Cambria, "Iowan Old Style", Georgia, serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --paper: #141a19;
    --card: #1c2423;
    --ink: #e2e9e6;
    --muted: #93a39e;
    --line: #2d3836;
    --accent: #7cc0d6;
    --accent-soft: #1f3a44;
    --spine: #4f9fb8;
    --tag-bg: #3a331f;
    --tag-ink: #ead8a6;
    --mark: #5b4c17;
    --danger: #f08a80;
    --code-bg: #232d2c;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 16px;
  line-height: 1.55;
}

button, input, textarea, select { font: inherit; color: inherit; }

button { cursor: pointer; }

.hidden { display: none !important; }

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

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

input, textarea, select {
  border: 1px solid var(--line);
  background: var(--card);
  border-radius: 6px;
  padding: 8px 10px;
}

input, textarea { width: 100%; }

button {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--line);
  background: var(--card);
  border-radius: 6px;
  padding: 7px 12px;
}

button:hover:not(:disabled) { border-color: var(--accent); }

button:disabled { opacity: 0.5; cursor: default; }

button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--card);
  font-weight: 600;
}

button svg, .icon-button svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.link-button {
  border: 0;
  background: none;
  padding: 0;
  color: var(--accent);
  text-decoration: underline;
  text-underline-offset: 2px;
}

.muted { color: var(--muted); }

.error { color: var(--danger); min-height: 1.2em; margin: 0; }

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

/* Sign in */

.login {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px 16px;
}

.login-card {
  width: min(400px, 100%);
  background: var(--card);
  border-left: 6px solid var(--spine);
  border-radius: 4px 10px 10px 4px;
  box-shadow: var(--shadow);
  padding: 28px 28px 20px;
}

.login-mark { width: 40px; height: 40px; }

.login-card h1, .dialog h2 {
  font-family: var(--serif);
  font-weight: 600;
  margin: 12px 0 4px;
}

.lede { color: var(--muted); margin: 0 0 18px; }

form { display: grid; gap: 12px; }

label { display: grid; gap: 4px; font-size: 14px; color: var(--muted); }

label input { color: var(--ink); }

/* Layout */

.layout {
  display: grid;
  grid-template-columns: minmax(0, 780px) 320px;
  justify-content: center;
  gap: 32px;
  padding: 0 24px;
}

.story {
  padding: 24px 0 50vh;
  display: grid;
  gap: 20px;
  align-content: start;
}

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
  padding: 28px 4px 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.site-title {
  font-family: var(--serif);
  font-size: 30px;
  line-height: 1.1;
  font-weight: 600;
  margin: 0;
  letter-spacing: -0.01em;
}

.site-subtitle { margin: 4px 0 0; color: var(--muted); }

.actions { display: flex; flex-wrap: wrap; gap: 8px; }

.search { font-size: 15px; }

.tabs {
  display: flex;
  gap: 2px;
  border-bottom: 1px solid var(--line);
}

.tab {
  border: 0;
  border-bottom: 2px solid transparent;
  border-radius: 0;
  background: none;
  padding: 6px 8px;
  color: var(--muted);
  font-size: 14px;
}

.tab.active { color: var(--ink); border-bottom-color: var(--spine); }

.side-list { flex: 1; min-height: 120px; font-size: 15px; }

.side-list .day h2 {
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  margin: 12px 0 4px;
}

.titles { list-style: none; margin: 0; padding: 0; }

.titles li { padding: 2px 0; overflow-wrap: anywhere; }

.titles a, .inline-list a, .title-list a { text-decoration: none; }

.titles a:hover, .inline-list a:hover, .title-list a:hover { text-decoration: underline; }

.count { color: var(--muted); font-size: 14px; margin: 8px 0; }

.check { display: flex; align-items: center; gap: 8px; margin: 8px 0; }

.check input { width: auto; }

.account {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 13px;
  color: var(--muted);
  border-top: 1px solid var(--line);
  padding-top: 10px;
}

.account .who { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Tiddlers */

.tiddler {
  background: var(--card);
  border-left: 6px solid var(--spine);
  border-radius: 4px 10px 10px 4px;
  box-shadow: var(--shadow);
  padding: 22px 32px 24px 28px;
  scroll-margin-top: 16px;
  min-width: 0;
}

.tiddler:focus { outline: none; }

.tiddler.is-missing { border-left-style: dashed; }

.tiddler.editing { border-left-color: var(--tag-ink); }

.tiddler.arrived { animation: arrive 360ms ease-out; }

@keyframes arrive {
  from { opacity: 0; transform: translateY(-8px); border-left-color: var(--tag-ink); }
  to { opacity: 1; transform: none; }
}

.card-head {
  display: flex;
  align-items: flex-start;
  gap: 12px;
}

.card-title {
  flex: 1;
  margin: 0;
  font-family: var(--serif);
  font-size: 28px;
  line-height: 1.2;
  font-weight: 600;
  letter-spacing: -0.01em;
  overflow-wrap: anywhere;
}

.toolbar { display: flex; gap: 2px; flex-shrink: 0; }

.icon-button {
  border: 0;
  background: none;
  padding: 6px;
  border-radius: 6px;
  color: var(--muted);
}

.icon-button:hover, .icon-button.on { background: var(--accent-soft); color: var(--accent); }

.icon-button.save { color: var(--accent); }

.icon-button.danger:hover { color: var(--danger); background: none; }

.card-meta { margin: 4px 0 0; color: var(--muted); font-size: 13px; }

.tags, .tag-cloud { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }

.tag {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--tag-bg);
  color: var(--tag-ink);
  border-radius: 999px;
  padding: 1px 10px;
  font-size: 13px;
  text-decoration: none;
}

.tag.missing { font-style: italic; }

.tag-count { opacity: 0.7; font-variant-numeric: tabular-nums; }

.tag.editable button {
  border: 0;
  background: none;
  padding: 0 0 0 2px;
  color: inherit;
  font-size: 15px;
  line-height: 1;
}

/* Rendered text */

.body {
  margin-top: 14px;
  max-width: 70ch;
  overflow-wrap: break-word;
}

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

.body > :last-child { margin-bottom: 0; }

.body h2, .body h3, .body h4, .body h5, .body h6 {
  font-family: var(--serif);
  font-weight: 600;
  line-height: 1.25;
  margin: 1.4em 0 0.4em;
}

.body h2 { font-size: 22px; }
.body h3 { font-size: 19px; }
.body h4, .body h5, .body h6 { font-size: 16px; }

.body p { margin: 0 0 0.8em; }

.body ul, .body ol { padding-left: 1.4em; margin: 0 0 0.8em; }

.body li.bare { list-style: none; }

.body blockquote {
  margin: 0 0 0.8em;
  padding: 2px 0 2px 16px;
  border-left: 3px solid var(--line);
  color: var(--muted);
}

.body blockquote cite { display: block; font-size: 14px; }

.body code {
  font-family: var(--mono);
  font-size: 0.88em;
  background: var(--code-bg);
  padding: 1px 4px;
  border-radius: 4px;
}

.body pre {
  background: var(--code-bg);
  padding: 12px 14px;
  border-radius: 6px;
  overflow-x: auto;
  margin: 0 0 0.8em;
}

.body pre code { background: none; padding: 0; }

.body img { max-width: 100%; height: auto; border-radius: 4px; }

.body hr { border: 0; border-top: 1px solid var(--line); margin: 1.4em 0; }

.body mark { background: var(--mark); color: inherit; padding: 0 2px; }

.body dt { font-weight: 600; }

.body dd { margin: 0 0 0.4em 1.4em; }

.body .plain { white-space: pre-wrap; }

.table-wrap { overflow-x: auto; margin: 0 0 0.8em; }

.body table { border-collapse: collapse; }

.body th, .body td {
  border: 1px solid var(--line);
  padding: 4px 10px;
  text-align: left;
  vertical-align: top;
}

.body th { background: var(--code-bg); }

.link { text-decoration: none; }

.link:hover { text-decoration: underline; }

.link.missing {
  font-style: italic;
  color: var(--muted);
  text-decoration: underline dotted;
  text-underline-offset: 3px;
}

.link.external::after { content: "↗"; font-size: 0.75em; margin-left: 1px; }

.transclusion { display: contents; }

.macro-error { color: var(--danger); }

.title-list, .toc { margin: 0 0 0.8em; }

.card-foot {
  margin-top: 20px;
  padding-top: 12px;
  border-top: 1px solid var(--line);
  display: grid;
  gap: 8px;
  font-size: 14px;
}

.card-foot h3, .history h3 {
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  margin: 0 0 4px;
}

.inline-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 4px 14px;
}

.history {
  margin-top: 20px;
  padding: 12px 14px;
  background: var(--paper);
  border-radius: 6px;
  font-size: 14px;
}

.revisions { margin: 0; padding-left: 1.2em; }

.revisions li.selected .link-button { font-weight: 700; }

.revision-view {
  margin-top: 12px;
  padding: 12px 14px;
  background: var(--card);
  border-radius: 6px;
}

.empty-story {
  padding: 40px 28px;
  border: 1px dashed var(--line);
  border-radius: 10px;
  color: var(--muted);
}

.empty-story p { margin: 0 0 4px; }

/* Editor */

.title-input {
  flex: 1;
  font-family: var(--serif);
  font-size: 24px;
  font-weight: 600;
  padding: 4px 8px;
}

.tag-editor {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  margin: 12px 0;
}

.tag-editor input {
  width: auto;
  flex: 1;
  min-width: 140px;
  padding: 4px 8px;
  font-size: 14px;
}

.edit-tools {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px;
  padding: 6px;
  background: var(--paper);
  border-radius: 6px 6px 0 0;
  border: 1px solid var(--line);
  border-bottom: 0;
}

.edit-tools .tool { padding: 3px 9px; font-size: 14px; min-width: 32px; justify-content: center; }

.edit-tools .tool[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); }

.edit-tools .spacer { flex: 1; }

.edit-tools select { padding: 3px 6px; font-size: 14px; }

.text-input {
  display: block;
  border-radius: 0 0 6px 6px;
  font-family: var(--mono);
  font-size: 14px;
  line-height: 1.55;
  min-height: 220px;
  resize: vertical;
}

.preview {
  margin-top: 12px;
  padding: 12px 16px;
  border: 1px dashed var(--line);
  border-radius: 6px;
}

.preview .body { margin-top: 0; }

.hint { margin: 8px 0 0; color: var(--muted); font-size: 13px; }

/* Dialog and toast */

.dialog-wrap {
  position: fixed;
  inset: 0;
  background: rgba(10, 20, 18, 0.45);
  display: grid;
  place-items: center;
  padding: 16px;
  z-index: 10;
}

.dialog {
  width: min(400px, 100%);
  background: var(--card);
  border-radius: 10px;
  padding: 20px 24px;
  box-shadow: var(--shadow);
}

.dialog h2 { margin-top: 0; }

.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }

.toast {
  position: fixed;
  left: 50%;
  bottom: 20px;
  transform: translateX(-50%);
  background: var(--ink);
  color: var(--paper);
  padding: 10px 16px;
  border-radius: 8px;
  max-width: calc(100vw - 32px);
  z-index: 20;
}

@media (max-width: 960px) {
  .layout {
    grid-template-columns: minmax(0, 1fr);
    gap: 0;
    padding: 0 16px;
  }

  .sidebar {
    order: -1;
    position: static;
    height: auto;
    padding: 20px 0 0;
  }

  .side-list { max-height: 30vh; overflow-y: auto; }

  .story { padding-top: 16px; }

  .tiddler { padding: 16px 16px 18px 16px; }

  .card-title { font-size: 23px; }
}

@media (prefers-reduced-motion: reduce) {
  .tiddler.arrived { animation: none; }
}
