/**
 * Two panes, quiet chrome, and one column of text that is the whole point.
 *
 * The palette is deliberately low-contrast for everything that is not the
 * reading: the sidebar, the toolbar and the metadata sit a step back so the
 * body text is the only thing on screen with full-strength colour. Borders are
 * hairlines at ~9% of the text colour rather than grey, which keeps them from
 * turning into a second layer of visual noise in dark mode.
 *
 * There is one stylesheet for reading and for writing, because they are the
 * same thing here: the rendered blocks are the editable surface, so a heading is
 * heading-sized whether or not the caret is in it. Nothing in here may key off
 * focus to change size, weight or spacing — that would put the page back to
 * moving under the cursor, which is exactly what this design is avoiding.
 */
:root {
  color-scheme: light dark;

  --page: #ffffff;
  --rail: #f7f7f5;
  --text: #37352f;
  --muted: #73716c;
  --faint: #91908c;
  --line: rgba(55, 53, 47, 0.09);
  --hover: rgba(55, 53, 47, 0.055);
  --active: rgba(55, 53, 47, 0.08);
  --accent: #2383e2;
  --danger: #c4554d;
  --mark: #fdecc8;
  --code-bg: rgba(135, 131, 120, 0.15);
  --code-text: #eb5757;
  --shadow: 0 0 0 1px rgba(15, 15, 15, 0.05), 0 3px 6px rgba(15, 15, 15, 0.1),
    0 9px 24px rgba(15, 15, 15, 0.2);

  --sidebar-width: 264px;
  --column: 812px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --page: #191919;
    --rail: #202020;
    --text: rgba(255, 255, 255, 0.81);
    --muted: rgba(255, 255, 255, 0.53);
    --faint: rgba(255, 255, 255, 0.4);
    --line: rgba(255, 255, 255, 0.094);
    --hover: rgba(255, 255, 255, 0.055);
    --active: rgba(255, 255, 255, 0.08);
    --accent: #4a9eff;
    --danger: #ff7369;
    --mark: rgba(255, 212, 0, 0.24);
    --code-bg: rgba(135, 131, 120, 0.22);
    --code-text: #ff7369;
    --shadow: 0 0 0 1px rgba(255, 255, 255, 0.06), 0 3px 6px rgba(0, 0, 0, 0.35),
      0 9px 24px rgba(0, 0, 0, 0.45);
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
}

html {
  -webkit-text-size-adjust: 100%; /* iOS otherwise inflates text in landscape */
}

body {
  background: var(--page);
  color: var(--text);
  font-family: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial,
    sans-serif;
  font-size: 16px;
  line-height: 1.5;
  overscroll-behavior-y: none;
}

.hidden {
  display: none !important;
}

button {
  font: inherit;
}

::selection {
  background: rgba(35, 131, 226, 0.28);
}

/* ------------------------------------------------------------------ *
 * Layout
 * ------------------------------------------------------------------ */

.app {
  display: grid;
  grid-template-columns: var(--sidebar-width) minmax(0, 1fr);
  height: 100%;
  height: 100dvh;
}

/* ------------------------------------------------------------------ *
 * Sidebar
 * ------------------------------------------------------------------ */

.sidebar {
  background: var(--rail);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  min-height: 0;
  padding-top: env(safe-area-inset-top, 0px);
}

.sidebar__head {
  align-items: center;
  display: flex;
  gap: 8px;
  justify-content: space-between;
  padding: 12px 10px 6px 14px;
}

.sidebar__brand {
  color: var(--muted);
  font-size: 14px;
  font-weight: 600;
  letter-spacing: 0.01em;
}

.icon-btn {
  align-items: center;
  background: none;
  border: 0;
  border-radius: 4px;
  color: var(--muted);
  cursor: pointer;
  display: inline-flex;
  height: 26px;
  justify-content: center;
  padding: 0;
  transition: background 20ms ease-in;
  width: 26px;
}

.icon-btn:hover {
  background: var(--active);
  color: var(--text);
}

/* Search */

.search {
  align-items: center;
  display: flex;
  gap: 7px;
  margin: 2px 10px 8px;
  padding: 5px 8px;
  position: relative;
}

.search:focus-within {
  background: var(--hover);
  border-radius: 4px;
}

.search__icon {
  color: var(--faint);
  flex-shrink: 0;
}

.search__input {
  background: none;
  border: 0;
  color: var(--text);
  flex: 1;
  font-family: inherit;
  font-size: 14px;
  min-width: 0;
  outline: none;
  padding: 0;
}

.search__input::placeholder {
  color: var(--faint);
}

/* The native affordance duplicates the button next to it and is unstyleable. */
.search__input::-webkit-search-cancel-button {
  display: none;
}

.search__clear {
  background: none;
  border: 0;
  border-radius: 3px;
  color: var(--faint);
  cursor: pointer;
  display: flex;
  padding: 3px;
}

.search__clear:hover {
  background: var(--active);
  color: var(--text);
}

/* The list */

.list {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 0 8px 24px;
  scrollbar-width: thin;
}

.group {
  margin-bottom: 12px;
}

.group__name {
  color: var(--faint);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.06em;
  margin: 10px 0 2px;
  padding: 0 6px;
  text-transform: uppercase;
}

.item {
  border-radius: 4px;
  color: var(--text);
  cursor: pointer;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  padding: 5px 6px;
  text-decoration: none;
  transition: background 20ms ease-in;
}

.item:hover {
  background: var(--hover);
}

.item--active {
  background: var(--active);
}

.item__title {
  font-size: 14px;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.item__meta {
  color: var(--faint);
  flex-shrink: 0;
  font-size: 11px;
  font-variant-numeric: tabular-nums;
  padding-left: 8px;
  padding-top: 2px;
}

/* Only present on a search hit whose body matched, so it never repeats a title. */
.item__snippet {
  color: var(--faint);
  display: -webkit-box;
  font-size: 12px;
  grid-column: 1 / -1;
  line-height: 1.4;
  overflow: hidden;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
}

.item mark {
  background: var(--mark);
  border-radius: 2px;
  color: inherit;
}

.list__empty {
  color: var(--faint);
  font-size: 13px;
  line-height: 1.5;
  margin: 0;
  padding: 4px 14px 20px;
}

/* ------------------------------------------------------------------ *
 * Right pane
 * ------------------------------------------------------------------ */

.pane {
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
  overflow-y: auto;
  position: relative;
}

.bar {
  align-items: center;
  background: var(--page);
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  padding: 10px 14px;
  padding-top: calc(10px + env(safe-area-inset-top, 0px));
  position: sticky;
  top: 0;
  z-index: 5;
}

.bar__sidebar {
  display: none;
  margin-right: auto;
}

/* Sits with the other controls at the right rather than marooned on the far
   left of an otherwise right-aligned bar. */
.status {
  color: var(--faint);
  font-size: 12px;
  margin-right: 4px;
  transition: color 120ms ease;
}

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

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

/* Segmented control: render vs source */

.segmented {
  background: var(--hover);
  border-radius: 6px;
  display: flex;
  padding: 2px;
}

.segmented__btn {
  background: none;
  border: 0;
  border-radius: 4px;
  color: var(--muted);
  cursor: pointer;
  font-size: 12px;
  font-weight: 500;
  padding: 4px 10px;
  transition: background 20ms ease-in, color 20ms ease-in;
}

.segmented__btn:hover {
  color: var(--text);
}

.segmented__btn[aria-pressed="true"] {
  background: var(--page);
  box-shadow: 0 1px 2px rgba(15, 15, 15, 0.12);
  color: var(--text);
}

/* Overflow menu */

.menu-wrap {
  position: relative;
}

.menu {
  background: var(--page);
  border-radius: 6px;
  box-shadow: var(--shadow);
  min-width: 168px;
  padding: 4px;
  position: absolute;
  right: 0;
  top: calc(100% + 6px);
  z-index: 20;
}

.menu__item {
  background: none;
  border: 0;
  border-radius: 4px;
  color: var(--text);
  cursor: pointer;
  display: block;
  font-size: 14px;
  padding: 6px 10px;
  text-align: left;
  width: 100%;
}

.menu__item:hover {
  background: var(--hover);
}

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

/* ------------------------------------------------------------------ *
 * The document
 * ------------------------------------------------------------------ */

.doc {
  margin: 0 auto;
  max-width: var(--column);
  padding: 8px 56px 45vh;
  width: 100%;
}

/* Trailing space so the last block can be scrolled to the middle of the screen
   rather than sitting against the bottom edge while you type in it. */

.doc__head {
  padding-bottom: 6px;
}

.doc__title {
  background: none;
  border: 0;
  color: var(--text);
  display: block;
  font-family: inherit;
  font-size: 40px;
  font-weight: 700;
  letter-spacing: -0.01em;
  line-height: 1.2;
  margin: 24px 0 0;
  outline: none;
  overflow: hidden;
  padding: 3px 2px;
  resize: none;
  width: 100%;
}

.doc__title::placeholder {
  color: var(--faint);
}

.doc__meta {
  align-items: center;
  color: var(--muted);
  display: flex;
  flex-wrap: wrap;
  gap: 6px 10px;
  font-size: 13px;
  margin-top: 10px;
  padding: 0 2px;
}

.chip {
  background: var(--hover);
  border: 0;
  border-radius: 4px;
  color: var(--text);
  cursor: pointer;
  font-size: 12px;
  font-weight: 500;
  padding: 3px 9px;
}

.chip:hover {
  background: var(--active);
}

.chip--empty {
  color: var(--faint);
}

.doc__stat {
  color: var(--faint);
  font-size: 13px;
}

.doc__source,
.doc__source-add {
  background: none;
  border: 0;
  color: var(--faint);
  cursor: pointer;
  font-size: 13px;
  max-width: 320px;
  overflow: hidden;
  padding: 0;
  text-decoration: none;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.doc__source:hover,
.doc__source-add:hover {
  color: var(--accent);
  text-decoration: underline;
}

.blank {
  align-items: center;
  display: flex;
  flex: 1;
  justify-content: center;
}

.blank__line {
  color: var(--faint);
  font-size: 14px;
}

/* ------------------------------------------------------------------ *
 * The editor
 * ------------------------------------------------------------------ */

.editor[data-mode="rendered"] .editor__raw,
.editor[data-mode="raw"] .editor__blocks {
  display: none;
}

.editor__blocks {
  /* Somewhere to put the caret below the last block, so a document can always
     be continued without aiming at a 3px gap. */
  min-height: 40vh;
  padding: 3px 2px;
}

/* Whole-document source view */
.editor__raw {
  background: none;
  border: 0;
  color: var(--text);
  display: block;
  font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  font-size: 14px;
  line-height: 1.7;
  margin-top: 8px;
  min-height: 60vh;
  outline: none;
  overflow: hidden;
  padding: 0 2px;
  resize: none;
  tab-size: 2;
  white-space: pre-wrap;
  width: 100%;
}

/* --- Blocks --------------------------------------------------------
 * There is no separate editing view any more: these elements *are* the
 * editor. A heading is styled as a heading whether or not the caret is
 * in it, which is the whole point — nothing about the page changes when
 * you start typing.
 * ------------------------------------------------------------------ */

.editor__blocks:focus,
.editor__blocks:focus-visible {
  outline: none;
}

.block {
  margin: 1px 0;
  position: relative;
}

.block > * {
  margin: 0;
}

/* The caret has to be able to sit in an empty block, so it keeps a line box. */
.block[data-empty] > p {
  min-height: 1.5em;
}

/* Only prompt on the block you are actually in, or on a wholly empty page. */
.block--active[data-empty] > p::before,
.editor__blocks > .block[data-empty]:only-child > p::before {
  color: var(--faint);
  content: "Type here, or press # for a heading";
  pointer-events: none;
  position: absolute;
}

/* --- Typography ---------------------------------------------------- */

.block h1,
.block h2,
.block h3,
.block h4,
.block h5,
.block h6 {
  font-weight: 600;
  line-height: 1.3;
}

.block h1 {
  font-size: 30px;
  font-weight: 700;
  margin-top: 28px;
}

.block h2 {
  font-size: 24px;
  margin-top: 22px;
}

.block h3 {
  font-size: 20px;
  margin-top: 18px;
}

.block h4,
.block h5,
.block h6 {
  font-size: 17px;
  margin-top: 14px;
}

/* The first block never needs the space that separates it from what is above. */
.editor__blocks > .block:first-child > :is(h1, h2, h3, h4, h5, h6) {
  margin-top: 4px;
}

.block p {
  word-break: break-word;
}

.block a {
  color: inherit;
  text-decoration: underline;
  text-decoration-color: var(--faint);
  text-underline-offset: 2px;
}

.block a:hover {
  text-decoration-color: var(--accent);
}

.block code {
  background: var(--code-bg);
  border-radius: 3px;
  color: var(--code-text);
  font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  font-size: 0.86em;
  padding: 0.15em 0.35em;
}

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

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

.block ul,
.block ol {
  padding-left: 1.5em;
}

.block li {
  margin: 1px 0;
}

.block ul.md-tasks {
  list-style: none;
  padding-left: 0.15em;
}

.md-task {
  align-items: baseline;
  display: flex;
  gap: 8px;
}

.md-task__box {
  accent-color: var(--accent);
  cursor: pointer;
  flex-shrink: 0;
  margin: 0;
  position: relative;
  top: 2px;
}

.md-task--done > .md-task__text {
  color: var(--muted);
  text-decoration: line-through;
}

.block blockquote {
  border-left: 3px solid currentColor;
  padding-left: 14px;
}

.block hr {
  border: 0;
  border-top: 1px solid var(--line);
  margin: 8px 0;
}

.md-code {
  background: var(--code-bg);
  border-radius: 3px;
  font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  font-size: 13.5px;
  line-height: 1.6;
  margin: 8px 0;
  overflow-x: auto;
  padding: 12px 14px;
  white-space: pre-wrap;
}

.md-code code {
  background: none;
  color: inherit;
  font-size: inherit;
  padding: 0;
}

.md-table-scroll {
  overflow-x: auto;
}

.md-table {
  border-collapse: collapse;
  font-size: 14px;
  width: 100%;
}

.md-table th,
.md-table td {
  border: 1px solid var(--line);
  padding: 6px 10px;
  text-align: left;
}

.md-table th {
  background: var(--hover);
  font-weight: 600;
}

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

/* ------------------------------------------------------------------ *
 * Popover
 * ------------------------------------------------------------------ */

.pop {
  background: var(--page);
  border-radius: 6px;
  box-shadow: var(--shadow);
  padding: 8px;
  position: fixed;
  width: 260px;
  z-index: 40;
}

.pop__label {
  color: var(--faint);
  display: block;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.06em;
  margin-bottom: 5px;
  text-transform: uppercase;
}

.pop__input {
  background: var(--hover);
  border: 0;
  border-radius: 4px;
  color: var(--text);
  font-family: inherit;
  font-size: 14px;
  outline: none;
  padding: 6px 8px;
  width: 100%;
}

.pop__input:focus {
  box-shadow: 0 0 0 2px var(--accent);
}

/* ------------------------------------------------------------------ *
 * Narrow screens — the sidebar becomes a drawer over the reading
 * ------------------------------------------------------------------ */

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

  .sidebar {
    bottom: 0;
    left: 0;
    position: fixed;
    top: 0;
    transform: translateX(-100%);
    transition: transform 160ms ease;
    width: var(--sidebar-width);
    z-index: 30;
  }

  .app--sidebar-open .sidebar {
    box-shadow: var(--shadow);
    transform: none;
  }

  .bar__sidebar {
    display: inline-flex;
  }

  .doc {
    padding: 4px 20px 40vh;
  }

  .doc__title {
    font-size: 32px;
    margin-top: 12px;
  }
}

@media (prefers-reduced-motion: reduce) {
  * {
    transition: none !important;
  }
}
