/* ===============================================================
   MARKSPACE - design tokens
   ---------------------------------------------------------------
   Dark is the default theme; light is the override. Every colour in
   the app comes from a token here - no hex values below this block.
   =============================================================== */
:root {
  /* ---------- TYPE ---------- */
  --font-ui: "Geist", "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
  --font-content: "Newsreader", "Source Serif 4", Georgia, "Times New Roman", serif;
  --font-mono: "JetBrains Mono", ui-monospace, "Cascadia Mono", Consolas, "Courier New", monospace;
  --font-size-base: 16px;
  /* UI scale; the content pane sets its own */
  --line-height: 1.55;
  --weight-normal: 400;
  --weight-medium: 500;

  /* ---------- CONTENT TYPE ---------- */
  --content-size: 17px;
  --content-leading: 1.7;
  --h1-size: 2.05em;
  --h2-size: 1.55em;
  --h3-size: 1.25em;
  --h4-size: 1.08em;
  --h5-size: 1em;
  --h6-size: 0.9em;

  /* ---------- LAYOUT / SPACING ---------- */
  --content-width: 1300px;
  /* the one reading column - the only place its width is set */
  --page-padding-x: 24px;
  --home-pad-x: var(--page-padding-x);
  --home-pad-bottom: 64px;
  --page-padding-y: 3rem;
  --block-gap: 1.15em;
  --heading-gap-top: 1.8em;
  --heading-gap-bot: 0.5em;
  --radius: 8px;
  --radius-sm: 6px;
  --speed: 120ms;

  /* ---------- TABLES ---------- */
  --table-cell-min: 7ch;
  /* a column never gets narrower than this */
  --table-cell-pad-y: 8px;
  --table-cell-pad-x: 14px;
  --table-font-size: 0.88em;

  /* ---------- CODE ---------- */
  --code-font-size: 0.82em;
  --code-pad-y: 14px;
  --code-pad-x: 16px;
  --code-inline-pad: 1px 5px;
  --code-line-height: 1.6;

  /* ---------- LISTS / QUOTES ---------- */
  --list-indent: 1.5em;
  --list-item-gap: 0.35em;
  --quote-border-w: 2px;
  --quote-pad: 0.1em 0 0.1em 1.2em;

  /* ---------- EDITOR ---------- */
  --editor-split: 1fr;
  --editor-font-size: 0.85rem;
  --editor-line-height: 1.65;
  --editor-pad: 24px;
  --editor-tab: 2;
  --disabled-opacity: 0.4;

  /* ---------- SIDEBAR ---------- */
  --sidebar-width: 264px;
  --outline-width: 216px;
  --sidebar-font-size: 13px;
  --sidebar-item-pad: 5px 9px;
  --sidebar-group-size: 12.5px;
  --tree-indent: 13px;
  --icon-btn-size: 22px;

  /* ---------- COLOUR: DARK (default) ---------- */
  --bg-base: #12100d;
  --bg-surface: #1a1714;
  --bg-hover: #24201b;
  --border: #332c25;
  --text-primary: #ede7da;
  --text-secondary: #a2988a;
  --text-muted: #6b6257;
  --accent: #e0873b;
  --accent-soft: #3a2a18;
  --shadow-pop: 0 10px 28px rgba(0, 0, 0, 0.5);
  --scrim: rgba(0, 0, 0, 0.55);

  /* topbar icon hues */
  --icon-files: #e0873b;
  --icon-folder: #8aa86b;
  --icon-file: #6e93b8;
  --icon-edit: #c9705a;
  --icon-theme: #d6b04c;
}

/* ---------- COLOUR: LIGHT ---------- */
[data-theme="light"] {
  --bg-base: #f5f2eb;
  --bg-surface: #fdfbf6;
  --bg-hover: #ede8dc;
  --border: #ddd5c6;
  --text-primary: #1a1714;
  --text-secondary: #6b6257;
  --text-muted: #948a7c;
  --accent: #aa5915;
  --accent-soft: #f3e4d2;
  --shadow-pop: 0 10px 28px rgba(26, 23, 20, 0.14);
  --scrim: rgba(26, 23, 20, 0.3);

  --icon-files: #b25e17;
  --icon-folder: #5e7a3f;
  --icon-file: #3f6b94;
  --icon-edit: #a0472f;
  --icon-theme: #96751a;
}

/* ===============================================================
   BASE
   =============================================================== */
*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  font-size: var(--font-size-base);
  color-scheme: dark;
}

[data-theme="light"] {
  color-scheme: light;
}

/* !important on purpose: .btn and friends are display:flex, which would
   otherwise beat the hidden attribute and leave the button on screen */
[hidden] {
  display: none !important;
}

/* JavaScript is off: one line, styled so it is not a bare flash of text */
.noscript {
  margin: 0;
  padding: 14px 16px;
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border);
  color: var(--text-primary);
  font-size: 0.85rem;
}

body {
  position: relative;
  margin: 0;
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  /* mobile: the viewport shrinks when the URL bar shows */
  overflow: hidden;
  background: var(--bg-base);
  color: var(--text-primary);
  font-family: var(--font-ui);
  font-weight: var(--weight-normal);
  line-height: var(--line-height);
  -webkit-font-smoothing: antialiased;
}

::selection {
  background: var(--accent-soft);
  color: var(--text-primary);
}

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

/* ===============================================================
   TOPBAR
   =============================================================== */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  padding: 9px 16px;
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border);
}

/* ---------- wordmark ---------- */
.brand {
  display: inline-flex;
  align-items: center;
  flex: 0 0 auto;
  padding: 4px 6px;
  margin-left: -6px;
  border: 0;
  border-radius: var(--radius-sm);
  background: none;
  color: var(--text-primary);
  cursor: pointer;
}

.brand:hover {
  background: var(--bg-hover);
}

.brand-mark {
  display: inline-flex;
  color: var(--text-primary);
}

.brand-counter {
  fill: var(--accent);
}

.wordmark {
  font-family: var(--font-mono);
  font-size: 0.9rem;
  font-weight: var(--weight-medium);
  letter-spacing: -0.01em;
  /* text-transform: lowercase; */
  color: var(--text-primary);
}

/* the hamburger only exists once the row collapses, see the media query */
#menu {
  display: none;
}

.i-menu {
  color: var(--text-secondary);
}

/* labels that only make sense once a button becomes a row in the menu */
.in-menu {
  display: none;
}

/* ---------- centre: current file ---------- */
.topbar-center {
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-width: 0;
}

.fname {
  font-family: var(--font-mono);
  font-size: 0.78rem;
  color: var(--text-secondary);
  max-width: 46ch;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.dot {
  color: var(--accent);
  font-size: 0.7em;
  line-height: 1;
}

.status {
  font-size: 0.75rem;
  color: var(--text-secondary);
  max-width: 32ch;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ---------- action groups ---------- */
.topbar-right {
  display: flex;
  align-items: center;
  gap: 20px;
  /* the gap that separates navigation from modes */
  flex: 0 0 auto;
  flex-wrap: wrap;
}

.btn-group {
  display: flex;
  align-items: center;
  gap: 6px;
}

.btn-group:empty {
  display: none;
}

/* a group whose buttons are all hidden must not leave a gap or a divider */
.btn-group:not(:has(button:not([hidden]))) {
  display: none;
}

/* ---------- buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 6px 11px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: transparent;
  color: var(--text-secondary);
  font: inherit;
  font-size: 0.8rem;
  line-height: 1.2;
  cursor: pointer;
  user-select: none;
  transition:
    background-color var(--speed) ease,
    border-color var(--speed) ease;
}

.btn:hover {
  background: var(--bg-hover);
}

.btn[disabled] {
  opacity: var(--disabled-opacity);
  cursor: default;
  pointer-events: none;
}

/* active mode: the Files toggle and Edit */
.btn.on {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--text-primary);
}

.btn.quiet {
  border-color: transparent;
  color: var(--text-secondary);
}

.btn.quiet:hover {
  color: var(--text-primary);
  background: var(--bg-hover);
}

.btn.icon-only {
  padding: 6px;
}

.btn.primary {
  border-color: var(--accent);
  background: var(--accent-soft);
  color: var(--text-primary);
  padding: 8px 14px;
}

.btn.primary:hover {
  background: var(--bg-hover);
}

/* ---------- icons ---------- */
.i {
  flex: 0 0 auto;
  width: 16px;
  height: 16px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.75;
  stroke-linecap: round;
  stroke-linejoin: round;
  color: var(--text-muted);
}

.i-files {
  color: var(--icon-files);
}

.i-folder {
  color: var(--icon-folder);
}

.i-file {
  color: var(--icon-file);
}

.i-edit {
  color: var(--icon-edit);
}

.i-theme {
  color: var(--icon-theme);
}

/* the save button turns into a download when there is nowhere to write back to */
#save .i-download {
  display: none;
}

#save.download .i-save {
  display: none;
}

#save.download .i-download {
  display: block;
}

/* dark theme offers the sun (switch to light), light theme the moon */
.i-moon {
  display: none;
}

[data-theme="light"] .i-sun {
  display: none;
}

[data-theme="light"] .i-moon {
  display: block;
}

.x-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  width: 22px;
  height: 22px;
  padding: 0;
  border: 0;
  border-radius: var(--radius-sm);
  background: none;
  color: var(--text-secondary);
  cursor: pointer;
  transition: background-color var(--speed) ease;
}

.x-btn .i {
  width: 13px;
  height: 13px;
  color: inherit;
}

.x-btn:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.user {
  display: inline-block;
  color: var(--text-secondary);
  font-size: 0.75rem;
  max-width: 22ch;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ===============================================================
   CONTENT PANE - reads like a document, not like an app
   =============================================================== */
.pane {
  overflow: auto;
  /* reserve the scrollbar gutter whether or not the document overflows, so
     nothing anchored to this edge moves when the scrollbar comes and goes */
  scrollbar-gutter: stable;
}

.doc {
  width: min(var(--content-width), 100%);
  margin: 0 auto;
  padding: var(--page-padding-y) var(--page-padding-x) 6rem;
  font-family: var(--font-content);
  font-size: var(--content-size);
  line-height: var(--content-leading);
  color: var(--text-primary);
  word-wrap: break-word;
}

/* one column, one left edge, one right edge: nothing may reach past it */
.doc>* {
  max-width: 100%;
}

/* Headings */
.doc h1,
.doc h2,
.doc h3,
.doc h4,
.doc h5,
.doc h6 {
  margin: var(--heading-gap-top) 0 var(--heading-gap-bot);
  font-weight: var(--weight-medium);
  line-height: 1.25;
  letter-spacing: -0.01em;
  color: var(--text-primary);
}

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

.doc h1 {
  font-size: var(--h1-size);
  margin-top: 1.2em;
}

.doc h2 {
  font-size: var(--h2-size);
}

.doc h3 {
  font-size: var(--h3-size);
}

.doc h4 {
  font-size: var(--h4-size);
}

.doc h5 {
  font-size: var(--h5-size);
}

.doc h6 {
  font-size: var(--h6-size);
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

/* Text */
.doc p {
  margin: 0 0 var(--block-gap);
}

.doc a {
  color: var(--accent);
  text-decoration: none;
  text-underline-offset: 2px;
}

.doc a:hover {
  text-decoration: underline;
}

.doc strong {
  font-weight: var(--weight-medium);
  color: var(--text-primary);
}

.doc mark {
  background: var(--accent-soft);
  color: var(--text-primary);
  padding: 0 0.15em;
  border-radius: 3px;
}

.doc del {
  color: var(--text-secondary);
}

/* Lists */
.doc ul,
.doc ol {
  margin: 0 0 var(--block-gap);
  padding-left: var(--list-indent);
}

.doc li {
  margin-bottom: var(--list-item-gap);
}

.doc li>ul,
.doc li>ol {
  margin: var(--list-item-gap) 0 0;
}

.doc li::marker {
  color: var(--text-secondary);
}

.doc li.task {
  list-style: none;
  margin-left: calc(var(--list-indent) * -1);
}

.doc li.task input {
  margin-right: 0.5em;
  accent-color: var(--accent);
}

/* Quote */
.doc blockquote {
  margin: 0 0 var(--block-gap);
  padding: var(--quote-pad);
  border-left: var(--quote-border-w) solid var(--border);
  border-radius: 0;
  color: var(--text-secondary);
}

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

/* Code */
.doc code {
  font-family: var(--font-mono);
  font-size: var(--code-font-size);
  background: var(--bg-hover);
  color: var(--text-primary);
  padding: var(--code-inline-pad);
  border-radius: 4px;
}

.doc pre {
  margin: 0 0 var(--block-gap);
  max-width: 100%;
  padding: var(--code-pad-y) var(--code-pad-x);
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow-x: auto;
}

.doc pre code {
  background: none;
  color: var(--text-primary);
  padding: 0;
  border-radius: 0;
  line-height: var(--code-line-height);
  white-space: pre;
}

/* Table */
/* a table too wide for the column scrolls inside this wrapper; the
   wrapper's own edges stay flush with the prose above it */
.doc .table-wrap {
  max-width: 100%;
  overflow-x: auto;
  margin: 0 0 var(--block-gap);
}

.doc table {
  border-collapse: collapse;
  width: 100%;
  min-width: min-content;
  /* columns scroll rather than collapse into slivers */
  font-family: var(--font-ui);
  font-size: var(--table-font-size);
  line-height: 1.5;
}

.doc th,
.doc td {
  min-width: var(--table-cell-min);
  padding: var(--table-cell-pad-y) var(--table-cell-pad-x);
  border: 1px solid var(--border);
  text-align: left;
}

.doc th {
  background-color: var(--bg-surface);
  border-bottom-width: 2px;
  font-weight: var(--weight-medium);
  color: var(--text-primary);
}

.doc tbody tr:nth-child(even) {
  background-color: var(--bg-hover);
}

.doc tbody tr:hover {
  background-color: var(--accent-soft);
}

/* Rules, images, keys */
.doc hr {
  border: 0;
  border-top: 1px solid var(--border);
  margin: 2.4em 0;
}

.doc img {
  max-width: 100%;
  border-radius: var(--radius);
}

.doc kbd {
  font-family: var(--font-mono);
  font-size: 0.78em;
  padding: 2px 6px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--bg-surface);
  color: var(--text-primary);
}

/* ===============================================================
   LAYOUT: sidebar | editor | preview
   =============================================================== */
.shell {
  position: relative;
  flex: 1 1 auto;
  display: flex;
  min-height: 0;
}

.wrap {
  flex: 1 1 auto;
  min-width: 0;
  display: grid;
  grid-template-columns: 1fr;
}

.wrap>* {
  min-width: 0;
  overflow: auto;
}

/* ===============================================================
   SIDEBAR
   =============================================================== */
.sidebar {
  flex: 0 0 var(--sidebar-width);
  display: flex;
  flex-direction: column;
  min-height: 0;
  /* without this the automatic minimum size lets a long folder chain widen
     the whole panel past its basis */
  min-width: 0;
  background: var(--bg-surface);
  border-right: 1px solid var(--border);
}

.side-head {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
}

.side-title {
  font-size: 0.82rem;
  font-weight: var(--weight-medium);
  color: var(--text-primary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.side-count {
  margin-left: auto;
  font-size: 0.72rem;
  font-family: var(--font-mono);
  color: var(--text-muted);
}

.side-filter {
  margin: 10px 10px 6px;
  padding: 6px 9px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-hover);
  color: var(--text-primary);
  font: inherit;
  font-size: 0.78rem;
  outline: none;
  transition: border-color var(--speed) ease;
}

.side-filter::placeholder {
  color: var(--text-secondary);
}

.side-filter:focus-visible {
  border-color: var(--accent);
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.filelist {
  flex: 1 1 auto;
  overflow: auto;
  padding: 4px 8px 16px;
}

.side-empty {
  padding: 14px 12px;
  color: var(--text-secondary);
  font-size: 0.78rem;
}

/* ---------- folder rows ----------
   A folder is a real button that toggles its children. Folder names are set
   in the UI font so they never read as files, which stay in mono. */
.grp {
  display: flex;
  align-items: center;
  gap: 2px;
  border-left: 2px solid transparent;
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  padding-left: calc(var(--lvl, 0) * var(--tree-indent));
}

.grp:hover {
  background-color: var(--bg-hover);
}

.grp-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  flex: 1 1 auto;
  min-width: 0;
  padding: 5px 6px;
  border: 0;
  background: none;
  color: var(--text-secondary);
  font-family: var(--font-ui);
  font-size: var(--sidebar-group-size);
  text-align: left;
  cursor: pointer;
}

.grp-name {
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  /* clip a long chain from the left: the deepest segment is the useful one */
  direction: rtl;
}

.grp-name bdi {
  direction: ltr;
}

.chev {
  transition: transform var(--speed) ease;
}

.grp-btn[aria-expanded="true"] .chev {
  transform: rotate(90deg);
}

/* ---------- indent guides ----------
   One hairline per nesting level, drawn only across the indent itself, so a
   subtree reads as a subtree without turning the list into a grid. */
.grp,
.f-row {
  background-image: repeating-linear-gradient(to right, var(--border) 0 1px, transparent 1px var(--tree-indent));
  background-repeat: no-repeat;
  background-size: calc(var(--lvl, 0) * var(--tree-indent)) 100%;
}

/* ---------- icons in the sidebar ---------- */
.ico {
  display: inline-flex;
  flex: 0 0 auto;
  align-items: center;
  color: var(--text-muted);
}

.grp .ico {
  margin-right: 4px;
}

.side-head .ico {
  color: var(--text-secondary);
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  width: var(--icon-btn-size);
  height: var(--icon-btn-size);
  padding: 0;
  border: 0;
  border-radius: 5px;
  background: none;
  color: var(--text-secondary);
  cursor: pointer;
  transition: background-color var(--speed) ease;
}

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

.icon-btn:disabled {
  opacity: var(--disabled-opacity);
  pointer-events: none;
}

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

/* folder actions appear on hover so the list stays quiet */
.grp .icon-btn {
  opacity: 0;
}

.grp:hover .icon-btn,
.grp:focus-within .icon-btn {
  opacity: 1;
}

/* ---------- inline row for a new / renamed name ---------- */
.new-row {
  display: flex;
  align-items: center;
  gap: 6px;
  margin: 4px 2px 6px;
  padding: 5px 8px;
  border: 1px solid var(--accent);
  border-radius: var(--radius-sm);
  background: var(--bg-hover);
}

.new-row .in-dir {
  flex: 0 1 auto;
  max-width: 45%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-family: var(--font-mono);
  font-size: 0.72rem;
  color: var(--text-secondary);
}

.new-row input {
  flex: 1 1 auto;
  min-width: 0;
  border: 0;
  outline: none;
  background: none;
  color: var(--text-primary);
  font-family: var(--font-mono);
  font-size: var(--sidebar-font-size);
}

.new-row input::placeholder {
  color: var(--text-secondary);
}

.new-err {
  margin: -2px 6px 8px;
  font-size: 0.72rem;
  color: var(--accent);
}

/* ---------- file rows ---------- */
.f-row {
  display: flex;
  align-items: center;
  gap: 2px;
  padding-right: 2px;
  padding-left: calc(var(--lvl, 0) * var(--tree-indent));
  border-left: 2px solid transparent;
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  color: var(--text-secondary);
  transition:
    background-color var(--speed) ease,
    border-color var(--speed) ease;
}

.f-row:hover {
  background-color: var(--bg-hover);
}

.f-row.active {
  background-color: var(--bg-hover);
  border-left-color: var(--accent);
  color: var(--text-primary);
}

.f-row.active .f-item {
  color: var(--text-primary);
  font-weight: var(--weight-medium);
}

.f-row.active .ico {
  color: var(--accent);
}

.f-row .icon-btn {
  opacity: 0;
}

.f-row:hover .icon-btn,
.f-row:focus-within .icon-btn {
  opacity: 1;
}

.f-item {
  display: flex;
  align-items: center;
  gap: 7px;
  flex: 1 1 auto;
  min-width: 0;
  padding: var(--sidebar-item-pad);
  border: 0;
  background: none;
  color: inherit;
  font-family: var(--font-mono);
  font-size: var(--sidebar-font-size);
  text-align: left;
  cursor: pointer;
}

.f-item .nm {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.f-item .badge {
  margin-left: auto;
  color: var(--accent);
  line-height: 1;
}

/* ===============================================================
   EDITOR
   =============================================================== */
.editor {
  display: none;
  border: 0;
  border-right: 1px solid var(--border);
  padding: var(--editor-pad);
  background: var(--bg-base);
  color: var(--text-primary);
  caret-color: var(--accent);
  font-family: var(--font-mono);
  font-size: var(--editor-font-size);
  line-height: var(--editor-line-height);
  tab-size: var(--editor-tab);
  white-space: pre-wrap;
  resize: none;
  outline: none;
}

body.editing .wrap {
  grid-template-columns: var(--editor-split) 1fr;
}

body.editing .editor {
  display: block;
}

@media (max-width: 900px) {
  :root {
    --sidebar-width: 200px;
  }
}

@media (max-width: 800px) {
  body.editing .wrap {
    grid-template-columns: 1fr;
    grid-template-rows: 1fr 1fr;
  }

  .editor {
    border-right: 0;
    border-bottom: 1px solid var(--border);
  }
}

/* Drag & drop overlay */
.drop {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: none;
  align-items: center;
  justify-content: center;
  background: var(--bg-base);
  opacity: 0.96;
  border: 2px dashed var(--accent);
  color: var(--accent);
  font-size: 1rem;
}

.drop.on {
  display: flex;
}

@media print {

  .topbar,
  .drop,
  .editor,
  .sidebar {
    display: none !important;
  }

  body,
  .shell,
  .wrap,
  .pane {
    display: block;
    height: auto;
    overflow: visible;
  }

  .doc {
    width: auto;
    padding: 0;
  }
}

/* ===============================================================
   HOME AND SIGN-IN
   =============================================================== */

/* ---------- view switching: home | doc ---------- */
.v-doc {
  display: none;
}

body[data-view="doc"] .v-doc {
  display: inline-flex;
}

#homeView,
#docView {
  display: none;
}

body[data-view="home"] #homeView {
  display: block;
}

body[data-view="doc"] #docView {
  display: flex;
}

.home {
  flex: 1 1 auto;
  overflow: auto;
  padding: 40px var(--home-pad-x) var(--home-pad-bottom);
  background: var(--bg-base);
}

.home-inner {
  max-width: 1040px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 40px;
}

.home-block h2 {
  margin: 0 0 6px;
  font-size: 1.05rem;
  font-weight: var(--weight-medium);
  color: var(--text-primary);
}

.home-head {
  display: flex;
  align-items: baseline;
  gap: 10px;
  margin-bottom: 6px;
}

.home-head h2 {
  margin: 0;
}

.home-note {
  margin: 0 0 16px;
  color: var(--text-secondary);
  font-size: 0.84rem;
  line-height: 1.6;
  max-width: 60ch;
}

.home-count {
  color: var(--text-muted);
  font-size: 0.8rem;
  font-family: var(--font-mono);
}

.home-empty {
  margin: 0;
  color: var(--text-secondary);
  font-size: 0.85rem;
}

.signin-card {
  padding: 22px;
  border: 1px solid var(--border);
  border-radius: calc(var(--radius) * 1.5);
  background: var(--bg-surface);
}

.login-err {
  margin: 14px 0 0;
  color: var(--accent);
  font-size: 0.8rem;
  line-height: 1.55;
  max-width: 60ch;
}

.cloud-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 16px;
}

.home-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 12px;
}

.home-grid:empty {
  display: none;
}

/* ---------- project and recent cards ---------- */
.proj,
.rec {
  position: relative;
  display: flex;
  align-items: stretch;
  border: 1px solid var(--border);
  border-radius: calc(var(--radius) * 1.5);
  background: var(--bg-surface);
  overflow: hidden;
  transition:
    background-color var(--speed) ease,
    border-color var(--speed) ease;
}

.proj:hover,
.rec:hover {
  background: var(--bg-hover);
  border-color: var(--accent);
}

.proj-open,
.rec-open {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  gap: 5px;
  min-width: 0;
  border: 0;
  background: none;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
}

.proj-open {
  padding: 15px 14px;
}

/* the recent card leaves room for the X in the corner */
.rec-open {
  padding: 13px 34px 13px 13px;
}

.proj-name,
.rec-name {
  font-family: var(--font-mono);
  font-weight: var(--weight-medium);
  color: var(--text-primary);
  font-size: 0.85rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.proj-meta,
.rec-meta {
  color: var(--text-secondary);
  font-size: 0.75rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.proj-acts {
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 2px;
  padding-right: 8px;
  opacity: 0;
  transition: opacity var(--speed) ease;
}

.rec-x {
  position: absolute;
  top: 6px;
  right: 6px;
  opacity: 0;
  transition: opacity var(--speed) ease;
}

.proj:hover .proj-acts,
.proj:focus-within .proj-acts,
.rec:hover .rec-x,
.rec:focus-within .rec-x {
  opacity: 1;
}

/* touch devices have no hover, so actions stay visible */
@media (hover: none) {

  .proj-acts,
  .rec-x {
    opacity: 1;
  }
}

.recent {
  margin-top: 26px;
}

.recent-title {
  margin: 0 0 10px;
  font-family: var(--font-mono);
  font-size: 0.72rem;
  font-weight: var(--weight-normal);
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-secondary);
}

.recent-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 10px;
}

@media (max-width: 640px) {
  :root {
    --home-pad-x: 16px;
    --home-pad-bottom: 48px;
  }

  .home {
    padding-top: 24px;
  }

  .home-grid {
    grid-template-columns: 1fr;
  }

  .user,
  .topbar-center {
    display: none;
  }
}

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

/* ===============================================================
   SAFE AREAS - installed, the app paints under the status bar and
   the home indicator. max() keeps the browser-tab padding as the
   floor, so nothing gets tighter than it already is.
   =============================================================== */
.topbar {
  padding-top: max(env(safe-area-inset-top), 9px);
  padding-left: max(env(safe-area-inset-left), 16px);
  padding-right: max(env(safe-area-inset-right), 16px);
}

/* full-height panel down the left edge */
.sidebar {
  padding-left: env(safe-area-inset-left);
}

.filelist {
  padding-bottom: max(env(safe-area-inset-bottom), 16px);
}

.home {
  padding-left: max(env(safe-area-inset-left), var(--home-pad-x));
  padding-right: max(env(safe-area-inset-right), var(--home-pad-x));
  padding-bottom: max(env(safe-area-inset-bottom), var(--home-pad-bottom));
}

.doc {
  padding-bottom: max(env(safe-area-inset-bottom), 6rem);
}

.editor {
  padding-bottom: max(env(safe-area-inset-bottom), var(--editor-pad));
}

@media print {
  .doc {
    padding-bottom: 0;
  }
}

/* ---------- loading ----------
   Shown while the project list is in flight, so the cloud block is never
   just an empty gap. */
.loading {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 4px 0 2px;
  color: var(--text-secondary);
  font-size: 0.84rem;
}

.spinner {
  width: 14px;
  height: 14px;
  flex: 0 0 auto;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 700ms linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* ---------- the topbar as a menu ----------
   Under this width the action row is too long to sit in the bar, so it
   folds into a panel behind the hamburger. Above it, nothing changes. */
@media (max-width: 760px) {
  #menu {
    display: inline-flex;
    margin-left: auto;
    /* the bar's right edge, whatever is left of it */
  }

  .topbar-right {
    display: none;
    position: absolute;
    top: calc(100% + 6px);
    right: 8px;
    z-index: 20;
    flex-direction: column;
    align-items: stretch;
    gap: 4px;
    min-width: 210px;
    max-width: calc(100vw - 16px);
    padding: 8px;
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow-pop);
  }

  body.menu-open .topbar-right {
    display: flex;
  }

  .topbar-right .btn-group {
    flex-direction: column;
    align-items: stretch;
    gap: 4px;
    width: 100%;
  }

  /* a divider where the desktop row had a 20px gap */
  .topbar-right .btn-group+.btn-group {
    margin-top: 4px;
    padding-top: 8px;
    border-top: 1px solid var(--border);
  }

  .topbar-right .btn {
    width: 100%;
    justify-content: flex-start;
    border-color: transparent;
    padding: 8px 10px;
  }

  .topbar-right .btn.on {
    border-color: var(--accent);
  }

  .topbar-right .btn.icon-only {
    padding: 8px 10px;
  }

  .in-menu {
    display: inline;
  }

  .user {
    padding: 4px 10px;
    max-width: none;
  }
}

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

/* ===============================================================
   PANEL EDGES
   ---------------------------------------------------------------
   Both panels are toggled from their own edge rather than from the
   topbar, so the control sits on the thing it controls.
   =============================================================== */
.edge-btn {
  position: absolute;
  top: 8px;
  z-index: 6;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 26px;
  height: 26px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-surface);
  color: var(--text-secondary);
  cursor: pointer;
  transition:
    background-color var(--speed) ease,
    border-color var(--speed) ease;
}

.edge-btn:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.edge-btn .i {
  width: 15px;
  height: 15px;
  color: inherit;
}

/* the glyph points the way the panel will move */
.edge-btn .i-panel-close {
  display: none;
}

.edge-btn.open .i-panel-close {
  display: block;
}

.edge-btn.open .i-panel-open {
  display: none;
}

.edge-left {
  left: 8px;
}

body.sidebar-open .edge-left {
  left: calc(var(--sidebar-width) + 8px);
}

/* --sb-w is measured at runtime in app.js: platforms with overlay
   scrollbars report 0, classic scrollbars report their real width */
.edge-right {
  right: calc(8px + var(--sb-w, 0px));
}

body.outline-open .edge-right {
  right: calc(var(--outline-width) + 8px);
}

/* ===============================================================
   OUTLINE
   =============================================================== */
.outline {
  flex: 0 0 var(--outline-width);
  min-width: 0;
  display: flex;
  flex-direction: column;
  overflow: auto;
  background: var(--bg-surface);
  border-left: 1px solid var(--border);
  padding-bottom: max(env(safe-area-inset-bottom), 16px);
}

.outline-head {
  margin: 0;
  padding: 11px 14px 8px;
  font-family: var(--font-mono);
  font-size: 0.72rem;
  font-weight: var(--weight-normal);
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-secondary);
}

.outline-list {
  display: flex;
  flex-direction: column;
  padding: 0 8px 12px;
}

.outline-item {
  display: block;
  padding: 5px 8px;
  border-left: 2px solid transparent;
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  color: var(--text-secondary);
  font-size: 0.8rem;
  line-height: 1.35;
  text-decoration: none;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  transition: background-color var(--speed) ease;
}

.outline-item:hover {
  background-color: var(--bg-hover);
  color: var(--text-primary);
}

.outline-item.lvl-h3 {
  padding-left: 22px;
  font-size: 0.76rem;
}

.outline-item.lvl-h1 {
  font-weight: var(--weight-medium);
}

.outline-item.on {
  background-color: var(--bg-hover);
  border-left-color: var(--accent);
  color: var(--text-primary);
}

/* headings are scroll targets, so give them a little air at the top */
.doc h1,
.doc h2,
.doc h3,
.doc h4,
.doc h5,
.doc h6 {
  scroll-margin-top: 12px;
}

/* a link whose target is not in the project */
.doc a.broken {
  color: var(--text-muted);
  text-decoration: underline dotted;
  cursor: not-allowed;
}

/* too narrow to hold a reading column and an outline: the outline goes */
@media (max-width: 1099px) {
  .outline,
  #outlineToggle {
    display: none !important;
  }
}

/* ===============================================================
   SEARCH PALETTE
   =============================================================== */
.palette {
  position: absolute;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 8dvh 16px 16px;
}

.palette-scrim {
  position: absolute;
  inset: 0;
  background: var(--scrim);
}

.palette-box {
  position: relative;
  display: flex;
  flex-direction: column;
  width: min(640px, 100%);
  max-height: 74dvh;
  overflow: hidden;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-pop);
}

.palette-input {
  flex: 0 0 auto;
  padding: 13px 14px;
  border: 0;
  border-bottom: 1px solid var(--border);
  background: none;
  color: var(--text-primary);
  font: inherit;
  font-size: 0.9rem;
  outline: none;
}

.palette-input::placeholder {
  color: var(--text-secondary);
}

.palette-note {
  margin: 0;
  padding: 8px 14px;
  border-bottom: 1px solid var(--border);
  color: var(--text-secondary);
  font-size: 0.76rem;
}

.palette-results {
  overflow: auto;
}

.palette-empty {
  margin: 0;
  padding: 14px;
  color: var(--text-secondary);
  font-size: 0.82rem;
}

.palette-result {
  display: block;
  width: 100%;
  padding: 8px 12px;
  border: 0;
  border-left: 2px solid transparent;
  background: none;
  text-align: left;
  cursor: pointer;
}

.palette-result:hover {
  background-color: var(--bg-hover);
}

.palette-result.sel {
  background-color: var(--bg-hover);
  border-left-color: var(--accent);
}

.palette-name {
  display: block;
  font-family: var(--font-mono);
  font-size: 0.78rem;
  color: var(--text-primary);
}

.palette-line {
  display: block;
  margin-top: 2px;
  font-family: var(--font-mono);
  font-size: 0.72rem;
  color: var(--text-secondary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.palette-line mark {
  background: var(--accent-soft);
  color: var(--text-primary);
  border-radius: 2px;
}
