:root {
  color-scheme: light;
  --paper: #faf8f8;
  --paper-raised: #ffffff;
  --shelf: #f0eeee;
  --ink: #2b2b2b;
  --ink-muted: #5c5b5e;
  --line: #d5d2d4;
  --line-strong: #aaa5a8;
  --accent: #284b63;
  --accent-hover: #19384e;
  --accent-soft: #e1eaef;
  --danger: #9c2f3b;
  --danger-soft: #fbeaec;
  --success: #276447;
  --focus: #1e6f9f;
  --shadow: 0 8px 24px rgba(43, 43, 43, 0.12);
  --ui-font: "Schibsted Grotesk", "Segoe UI", Arial, sans-serif;
  --editor-font: "IBM Plex Mono", "Cascadia Mono", "SFMono-Regular", Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --paper: #161618;
    --paper-raised: #1d1d20;
    --shelf: #1a191c;
    --ink: #ebebec;
    --ink-muted: #b6b3b6;
    --line: #393639;
    --line-strong: #5d595d;
    --accent: #8baabe;
    --accent-hover: #a9c1d0;
    --accent-soft: #253640;
    --danger: #ff9ba4;
    --danger-soft: #402428;
    --success: #84d0aa;
    --focus: #8ec8eb;
    --shadow: 0 10px 28px rgba(0, 0, 0, 0.38);
  }
}

* {
  box-sizing: border-box;
}

html {
  min-width: 320px;
  background: var(--paper);
  font-family: var(--ui-font);
  color: var(--ink);
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--paper);
}

button,
input,
textarea {
  font: inherit;
}

button,
a,
input,
textarea {
  -webkit-tap-highlight-color: transparent;
}

button:focus-visible,
a:focus-visible,
input:focus-visible,
textarea:focus-visible,
summary:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}

a[aria-disabled="true"] {
  cursor: not-allowed;
  opacity: 0.5;
  text-decoration: none;
}

[hidden] {
  display: none !important;
}

.skip-link {
  position: fixed;
  z-index: 20;
  top: 0.5rem;
  left: 0.5rem;
  padding: 0.65rem 0.9rem;
  background: var(--ink);
  color: var(--paper);
  transform: translateY(-150%);
}

.skip-link:focus {
  transform: translateY(0);
}

.app-shell {
  min-height: 100vh;
  min-height: 100dvh;
}

.masthead {
  min-height: 4.5rem;
  padding: 0.75rem clamp(1rem, 3vw, 2rem);
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  background: var(--paper-raised);
}

.identity {
  display: inline-flex;
  align-items: center;
  gap: 0.75rem;
  color: var(--ink);
  text-decoration: none;
}

.identity img {
  flex: 0 0 auto;
}

.identity span {
  display: grid;
  gap: 0.1rem;
}

.identity strong {
  font-size: 1rem;
  letter-spacing: -0.01em;
}

.identity small {
  color: var(--ink-muted);
  font-size: 0.8rem;
}

.quiet-link,
.text-button {
  color: var(--accent);
  font-weight: 650;
  text-underline-offset: 0.18em;
}

.quiet-link:hover,
.text-button:hover {
  color: var(--accent-hover);
}

.gate {
  width: min(100% - 2rem, 58rem);
  min-height: calc(100vh - 4.5rem);
  min-height: calc(100dvh - 4.5rem);
  margin: 0 auto;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(18rem, 24rem);
  align-items: center;
  gap: clamp(2rem, 7vw, 6rem);
  padding: 4rem 0;
}

.gate-copy {
  max-width: 35rem;
}

.gate-copy h1 {
  max-width: 10ch;
  margin: 0.35rem 0 1rem;
  color: var(--ink);
  font-size: clamp(2.25rem, 6vw, 4.5rem);
  line-height: 0.98;
  letter-spacing: -0.035em;
}

.gate-copy p:last-child {
  max-width: 56ch;
  margin: 0;
  color: var(--ink-muted);
  font-size: 1.05rem;
  line-height: 1.6;
}

.section-label {
  margin: 0;
  color: var(--ink-muted);
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.055em;
  text-transform: uppercase;
}

.login-form {
  padding: 1.6rem;
  border: 1px solid var(--line-strong);
  background: var(--paper-raised);
  box-shadow: var(--shadow);
}

.login-form > label,
.new-file-form label,
.search-label {
  display: block;
  margin-bottom: 0.45rem;
  color: var(--ink);
  font-size: 0.88rem;
  font-weight: 700;
}

.password-row,
.create-row {
  display: flex;
  align-items: stretch;
  gap: 0.5rem;
}

input,
textarea {
  border: 1px solid var(--line-strong);
  border-radius: 0;
  background: var(--paper);
  color: var(--ink);
}

input {
  min-height: 2.75rem;
  padding: 0.65rem 0.75rem;
}

input::placeholder,
textarea::placeholder {
  color: var(--ink-muted);
  opacity: 0.78;
}

.password-row input,
.create-row input {
  min-width: 0;
  flex: 1;
}

.primary-button,
.secondary-button,
.text-button {
  min-height: 2.75rem;
  border-radius: 0;
  cursor: pointer;
  font-weight: 700;
}

.primary-button {
  padding: 0.65rem 1rem;
  border: 1px solid var(--accent);
  background: var(--accent);
  color: var(--paper-raised);
}

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

.secondary-button {
  padding: 0.65rem 0.85rem;
  border: 1px solid var(--line-strong);
  background: var(--paper-raised);
  color: var(--ink);
}

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

.text-button {
  min-height: 2.5rem;
  padding: 0;
  border: 0;
  background: transparent;
  text-decoration: underline;
  text-underline-offset: 0.18em;
}

.login-form > .primary-button {
  width: 100%;
  margin-top: 0.25rem;
}

.form-message,
.save-message {
  min-height: 1.4rem;
  margin: 0.55rem 0;
  color: var(--ink-muted);
  font-size: 0.88rem;
  line-height: 1.45;
}

.form-message[data-kind="error"],
.save-message[data-kind="error"],
.list-status[data-kind="error"] {
  color: var(--danger);
}

.save-message[data-kind="success"] {
  color: var(--success);
}

.save-state {
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 0.8rem;
}

.save-state .text-button {
  flex: 0 0 auto;
}

.workspace {
  height: calc(100vh - 4.5rem);
  height: calc(100dvh - 4.5rem);
  display: grid;
  grid-template-columns: minmax(17rem, 21rem) minmax(0, 1fr);
  overflow: hidden;
}

.file-shelf {
  min-height: 0;
  padding: 1.4rem 1.2rem 1rem;
  border-right: 1px solid var(--line);
  display: grid;
  grid-template-rows: auto auto auto auto minmax(0, 1fr) auto;
  background: var(--shelf);
}

.shelf-heading,
.editor-heading,
.editor-footer,
.session-strip {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}

.shelf-heading h1 {
  margin: 0.25rem 0 0;
  font-size: 1.2rem;
  letter-spacing: -0.015em;
}

.new-file-form {
  margin: 1rem -1.2rem 0;
  padding: 1rem 1.2rem;
  border-block: 1px solid var(--line);
  background: var(--paper-raised);
}

.new-file-form .form-message {
  margin-bottom: 0;
}

.search-label {
  margin-top: 1.25rem;
}

.file-search {
  width: 100%;
  margin-bottom: 0.8rem;
}

.list-status {
  margin: 0;
  padding: 0.5rem 0.35rem;
  color: var(--ink-muted);
  font-size: 0.85rem;
}

.file-list {
  min-height: 0;
  margin: 0;
  padding: 0 0.25rem 0 0;
  overflow-y: auto;
  list-style: none;
  scrollbar-gutter: stable;
}

.file-group + .file-group {
  margin-top: 0.3rem;
}

.file-group details {
  border-bottom: 1px solid var(--line);
}

.file-group summary {
  min-height: 2.75rem;
  padding: 0.55rem 0.45rem;
  display: flex;
  align-items: center;
  gap: 0.55rem;
  color: var(--ink-muted);
  cursor: pointer;
  font-family: var(--editor-font);
  font-size: 0.76rem;
  font-weight: 700;
  overflow-wrap: anywhere;
  list-style: none;
}

.file-group summary::marker {
  content: "";
}

.file-group summary::before {
  flex: 0 0 auto;
  color: var(--accent);
  content: "›";
  font-family: var(--ui-font);
  font-size: 1rem;
  line-height: 1;
  transform: rotate(0deg);
}

.file-group details[open] > summary::before {
  transform: rotate(90deg);
}

.file-group summary span:first-child {
  min-width: 0;
  flex: 1;
}

.folder-count {
  min-width: 1.4rem;
  padding: 0.05rem 0.28rem;
  border: 1px solid var(--line);
  color: var(--ink-muted);
  font-family: var(--ui-font);
  font-size: 0.7rem;
  font-weight: 650;
  text-align: center;
}

.file-group ul {
  margin: 0;
  padding: 0 0 0.4rem 0.85rem;
  list-style: none;
}

.file-group li + li {
  border-top: 1px solid var(--line);
}

.file-list button {
  width: 100%;
  min-height: 2.75rem;
  padding: 0.6rem 0.55rem;
  border: 0;
  background: transparent;
  color: var(--ink);
  cursor: pointer;
  overflow-wrap: anywhere;
  text-align: left;
}

.file-list button:hover {
  background: var(--paper-raised);
}

.file-list button[aria-current="page"] {
  background: var(--accent-soft);
  color: var(--accent-hover);
  font-weight: 700;
}

.file-list-empty {
  padding: 1.5rem 0.55rem;
  color: var(--ink-muted);
  font-size: 0.88rem;
  text-align: center;
}

.session-strip {
  margin-top: 0.75rem;
  padding-top: 0.75rem;
  border-top: 1px solid var(--line);
}

.session-strip p {
  margin: 0;
  color: var(--ink-muted);
  font-size: 0.78rem;
}

.writing-desk {
  min-width: 0;
  min-height: 0;
  background: var(--paper-raised);
}

.empty-editor {
  height: 100%;
  padding: 2rem;
  display: grid;
  place-items: center;
}

.empty-editor > div {
  max-width: 34rem;
  padding-bottom: 8vh;
}

.empty-editor h2 {
  margin: 0.4rem 0 0.8rem;
  font-size: 2rem;
  letter-spacing: -0.025em;
}

.empty-editor p:last-child {
  max-width: 60ch;
  margin: 0;
  color: var(--ink-muted);
  line-height: 1.6;
}

.editor-form {
  height: 100%;
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr) auto;
}

.editor-heading {
  min-height: 6.2rem;
  padding: 1rem clamp(1rem, 3vw, 2rem);
  border-bottom: 1px solid var(--line);
}

.document-name {
  min-width: 0;
}

.document-name h2 {
  margin: 0.18rem 0;
  overflow: hidden;
  color: var(--ink);
  font-size: 1.3rem;
  letter-spacing: -0.015em;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.revision-label {
  margin: 0;
  color: var(--ink-muted);
  font-size: 0.8rem;
}

.editor-viewbar {
  min-height: 3.25rem;
  padding: 0 clamp(1rem, 3vw, 2rem);
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  background: var(--paper);
}

.view-tabs,
.published-actions,
.editor-meta {
  display: flex;
  align-items: center;
}

.view-tabs {
  align-self: stretch;
  gap: 1.25rem;
}

.view-tab {
  position: relative;
  min-height: 100%;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--ink-muted);
  cursor: pointer;
  font-size: 0.85rem;
  font-weight: 700;
}

.view-tab[aria-selected="true"] {
  color: var(--ink);
}

.view-tab[aria-selected="true"]::after {
  position: absolute;
  right: 0;
  bottom: -1px;
  left: 0;
  height: 2px;
  background: var(--accent);
  content: "";
}

.published-actions {
  gap: 1rem;
  font-size: 0.82rem;
  white-space: nowrap;
}

.published-actions .text-button {
  min-height: 2.25rem;
  font-size: inherit;
}

.write-panel {
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
}

.format-toolbar {
  min-width: 0;
  min-height: 3rem;
  padding: 0.4rem clamp(1rem, 3vw, 2rem);
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  gap: 0.65rem;
  overflow-x: auto;
  background: var(--paper-raised);
  scrollbar-width: thin;
}

.toolbar-group {
  flex: 0 0 auto;
  display: inline-flex;
  border: 1px solid var(--line);
}

.toolbar-group + .toolbar-group {
  margin-left: 0.15rem;
}

.toolbar-group button {
  min-width: 2.5rem;
  min-height: 2.5rem;
  padding: 0.3rem 0.55rem;
  border: 0;
  background: var(--paper-raised);
  color: var(--ink);
  cursor: pointer;
  font-family: var(--editor-font);
  font-size: 0.78rem;
  font-weight: 650;
}

.toolbar-group button + button {
  border-left: 1px solid var(--line);
}

.toolbar-group button:hover {
  background: var(--accent-soft);
  color: var(--accent-hover);
}

#markdown-editor {
  width: 100%;
  height: 100%;
  min-height: 0;
  padding-block: clamp(1.25rem, 3vw, 2.5rem);
  padding-inline: max(clamp(1.25rem, 3vw, 2.5rem), calc((100% - 84ch) / 2));
  border: 0;
  background: var(--paper-raised);
  font-family: var(--editor-font);
  font-size: 0.94rem;
  line-height: 1.65;
  resize: none;
  tab-size: 2;
}

#markdown-editor:focus-visible {
  outline-offset: -3px;
}

.preview-panel {
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  background: var(--paper);
}

.preview-status {
  min-height: 2.55rem;
  margin: 0;
  padding: 0.65rem clamp(1rem, 3vw, 2rem);
  border-bottom: 1px solid var(--line);
  color: var(--ink-muted);
  font-size: 0.8rem;
  line-height: 1.4;
}

#draft-preview {
  width: 100%;
  height: 100%;
  min-height: 0;
  border: 0;
  background: #ffffff;
}

.editor-footer {
  position: relative;
  min-height: 3.35rem;
  padding: 0.5rem clamp(1rem, 3vw, 2rem);
  border-top: 1px solid var(--line);
  background: var(--paper);
}

.save-message {
  margin: 0;
}

.shortcut-guide {
  position: relative;
  color: var(--ink-muted);
  font-size: 0.78rem;
  white-space: nowrap;
}

.shortcut-guide summary {
  min-height: 2.5rem;
  display: flex;
  align-items: center;
  color: var(--accent);
  cursor: pointer;
  font-weight: 650;
  list-style: none;
  text-decoration: underline;
  text-underline-offset: 0.18em;
}

.shortcut-guide summary::marker {
  content: "";
}

.shortcut-guide summary::after {
  margin-left: 0.4rem;
  content: "↑";
  font-size: 0.7rem;
  text-decoration: none;
}

.shortcut-guide[open] summary::after {
  content: "↓";
}

.shortcut-list {
  position: absolute;
  z-index: 5;
  right: 0;
  bottom: calc(100% + 0.45rem);
  width: 17rem;
  margin: 0;
  padding: 0.7rem 0.85rem;
  border: 1px solid var(--line-strong);
  background: var(--paper-raised);
  box-shadow: var(--shadow);
}

.shortcut-list div {
  min-height: 2.1rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}

.shortcut-list div + div {
  border-top: 1px solid var(--line);
}

.shortcut-list dt {
  color: var(--ink);
  font-weight: 650;
}

.shortcut-list dd {
  margin: 0;
}

.editor-meta {
  gap: 1rem;
}

.document-stats {
  margin: 0;
  color: var(--ink-muted);
  font-size: 0.78rem;
  white-space: nowrap;
}

kbd {
  padding: 0.08rem 0.28rem;
  border: 1px solid var(--line-strong);
  background: var(--paper-raised);
  font-family: var(--ui-font);
  font-size: 0.72rem;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

@media (max-width: 760px) {
  .masthead {
    min-height: 4rem;
  }

  .quiet-link {
    font-size: 0.88rem;
  }

  .identity small {
    display: none;
  }

  .gate {
    min-height: calc(100dvh - 4rem);
    grid-template-columns: 1fr;
    align-content: center;
    gap: 2rem;
    padding: 2.5rem 0;
  }

  .gate-copy h1 {
    font-size: 3rem;
  }

  .workspace {
    height: auto;
    min-height: calc(100dvh - 4rem);
    grid-template-columns: 1fr;
    overflow: visible;
  }

  .file-shelf {
    max-height: 27rem;
    border-right: 0;
    border-bottom: 1px solid var(--line);
    grid-template-rows: auto auto auto auto minmax(7rem, 1fr) auto;
  }

  .writing-desk {
    min-height: 34rem;
  }

  .editor-form {
    min-height: 40rem;
  }

  .editor-heading {
    min-height: 5.5rem;
  }

  .editor-viewbar {
    align-items: stretch;
    flex-direction: column;
    gap: 0;
  }

  .view-tabs {
    min-height: 3rem;
  }

  .published-actions {
    min-height: 2.8rem;
    border-top: 1px solid var(--line);
  }

  .save-state {
    align-items: flex-start;
    flex-direction: column;
    gap: 0;
  }
}

@media (max-width: 460px) {
  .masthead {
    align-items: flex-start;
  }

  .identity strong {
    display: block;
    max-width: 13ch;
  }

  .quiet-link {
    max-width: 8rem;
    text-align: right;
  }

  .login-form {
    padding: 1.2rem;
  }

  .shelf-heading,
  .editor-heading {
    align-items: flex-start;
  }

  .editor-heading {
    flex-direction: column;
  }

  .editor-heading .primary-button {
    width: 100%;
  }

  .view-tabs {
    gap: 1rem;
  }

  .published-actions {
    align-items: flex-start;
    flex-direction: column;
    justify-content: center;
    gap: 0;
    padding: 0.45rem 0;
    white-space: nowrap;
  }

  .published-actions .text-button {
    min-height: 2rem;
  }

  .editor-footer {
    align-items: flex-start;
    flex-direction: column;
    gap: 0.25rem;
  }

  .editor-meta {
    width: 100%;
    justify-content: flex-end;
  }

  .create-row {
    flex-direction: column;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
  }
}
