:root {
  --history-muted: #b4b4b0;
  --history-add-bg: #192e24;
  --history-remove-bg: #33201f;
  --bg: #191a1c;
  --bg-elev: #222427;
  --sidebar: #202123;
  --sidebar-hover: #2a2c30;
  --sidebar-active: #303338;
  --border: #34363b;
  --text: #e9eaed;
  --text-soft: #afb2ba;
  --text-faint: #989ca6;
  --accent: #416bdd;
  --accent-soft: rgba(110, 154, 248, 0.13);
  --link: #9cbaff;
  --focus: #9cbaff;
  --code-bg: #24272c;
  --code-text: #c9d6ed;
  --quote-bar: #4a4a48;
  --popover: #27292d;
  --shadow: 0 12px 40px rgba(0, 0, 0, 0.55);
  --radius: 8px;
  --font-sans: "Segoe UI Variable Text", "Segoe UI", -apple-system, BlinkMacSystemFont, sans-serif;
  --font-mono: "Cascadia Code", "SFMono-Regular", Consolas, monospace;
  --block-gutter: 80px;
}

[data-theme='light'] {
  --history-muted: #63625d;
  --history-add-bg: #edf7f0;
  --history-remove-bg: #fff0ed;
  --bg: #ffffff;
  --bg-elev: #ffffff;
  --sidebar: #f7f7f5;
  --sidebar-hover: #ececea;
  --sidebar-active: #e4e4e1;
  --border: #e6e6e3;
  --text: #37352f;
  --text-soft: #656763;
  --text-faint: #70726d;
  --accent: #345fc4;
  --accent-soft: rgba(47, 109, 246, 0.12);
  --code-bg: #f7f6f3;
  --code-text: #475773;
  --link: #345fc4;
  --focus: #345fc4;
  --quote-bar: #d3d1cb;
  --popover: #ffffff;
  --shadow: 0 12px 40px rgba(15, 15, 15, 0.18);
}

* { box-sizing: border-box; }
button, input, textarea { font: inherit; }
button, a { -webkit-tap-highlight-color: transparent; }
:is(button, a, input, textarea, [tabindex]):focus-visible { outline: 2px solid var(--focus); outline-offset: 3px; }
::selection { background: var(--accent-soft); color: var(--text); }
[contenteditable], textarea { caret-color: var(--link); }
.ui-icon { display: inline-block; width: 18px; height: 18px; flex-shrink: 0; vertical-align: middle; pointer-events: none; }

.presence-toggle { display: inline-flex; align-items: center; gap: 5px; min-width: 43px; min-height: 32px; padding: 4px 6px; border: 0; border-radius: 5px; background: transparent; color: var(--text-soft); font-size: 12px; cursor: pointer; }
.presence-toggle:hover { color: var(--text); background: var(--sidebar-hover); }
#presence-panel { position: fixed; inset: 62px 18px auto auto; width: min(340px, calc(100vw - 32px)); max-height: calc(100dvh - 90px); overflow: auto; margin: 0; padding: 18px; background: var(--popover); color: var(--text); border: 0; border-radius: 8px; box-shadow: var(--shadow); }
#presence-panel h2 { margin: 0 0 14px; font-size: 16px; }
.presence-person { display: flex; gap: 10px; width: 100%; padding: 9px 4px; border: 0; border-radius: 4px; text-align: left; color: var(--text); background: none; cursor: pointer; }
.presence-person:disabled { opacity: 1; cursor: default; }
.presence-person:hover:not(:disabled) { background: var(--sidebar-hover); }
.presence-avatar { display: grid; place-items: center; flex: 0 0 30px; height: 30px; margin-top: 2px; border-radius: 50%; border: 2px solid var(--participant-color); color: var(--text); font-size: 11px; font-weight: 600; }
.presence-person-info { min-width: 0; display: flex; flex-direction: column; gap: 3px; }
.presence-person-name { font-size: 13px; font-weight: 600; overflow-wrap: anywhere; }
.presence-ai { display: inline-block; margin-left: 6px; padding: 1px 4px; border: 1px solid var(--border); border-radius: 3px; color: var(--text-soft); font-size: 10px; font-weight: 500; }
.presence-activity { font-size: 12px; color: var(--text-soft); }
.presence-identity { font-size: 11px; color: var(--text-faint); overflow-wrap: anywhere; }
.presence-empty { font-size: 13px; color: var(--text-soft); }
#presence-profile { margin-top: 14px; padding-top: 14px; border-top: 1px solid var(--border); }
#presence-profile label { display: block; margin-bottom: 8px; font-size: 12px; color: var(--text-soft); }
#presence-profile > div { display: flex; gap: 6px; }
#presence-profile input { min-width: 0; width: 100%; padding: 7px 9px; border: 1px solid var(--border); border-radius: 4px; color: var(--text); background: var(--bg); font-size: 13px; }
#presence-profile button { padding: 7px 9px; border: 1px solid var(--border); border-radius: 4px; color: var(--text); background: var(--sidebar); font-size: 12px; cursor: pointer; }
#presence-layer { position: fixed; z-index: 15; pointer-events: none; overflow: hidden; }
.presence-caret,.presence-selection,.presence-target { position: absolute; pointer-events: none; }
.presence-caret { border-left: 2px solid var(--participant-color); }
.presence-selection { background: var(--participant-color); opacity: .18; }
.presence-target { border: 1px solid var(--participant-color); border-radius: 3px; }
.presence-cursor-label { position: absolute; left: -1px; max-width: 180px; height: 20px; padding: 2px 6px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; background: var(--popover); border: 1px solid var(--participant-color); border-radius: 3px; color: var(--text); font-size: 11px; line-height: 14px; }
.history-contributors { color: var(--text-soft); font-size: 12px; overflow-wrap: anywhere; }
@media (max-width: 760px) { .presence-toggle { min-width: 34px; gap: 3px; padding: 4px 2px; } .presence-toggle .ui-icon { width: 15px; } #presence-panel { right: 12px; top: 58px; } }

/* Tables stay inside the reading column, with their own horizontal scroll. */
.block[data-type='table'] { margin: 24px 0; }
.block[data-type='table'] .block-body { min-width: 0; display: block; }
.table-editor { position: relative; min-width: 0; max-width: 100%; }
.table-scroll { max-width: 100%; overflow-x: auto; scrollbar-color: var(--text-faint) transparent; overscroll-behavior-x: contain; }
.document-table { width: 100%; border-collapse: separate; border-spacing: 0; text-align: left; font-size: 14px; line-height: 1.5; }
.document-table th, .document-table td { border-right: 1px solid var(--border); border-bottom: 1px solid var(--border); vertical-align: top; padding: 0; }
.document-table tr:first-child > * { border-top: 1px solid var(--border); }
.document-table tbody tr:first-child > * { border-top: 0; }
.document-table th { background: var(--sidebar); font-weight: 600; }
.document-table th:first-child, .document-table td:first-child { border-left: 1px solid var(--border); }
.document-table .table-data-cell { position: relative; min-width: 168px; }
.document-table tr > .table-row-handle { width: 28px; min-width: 28px; border: 0; background: none; vertical-align: middle; }
.document-table .table-row-handle + * { border-left: 1px solid var(--border); }
.table-cell { display: block; width: 100%; min-width: 168px; min-height: 42px; padding: 10px 12px; border: 0; border-radius: 0; background: transparent; color: var(--text); font: inherit; line-height: 1.55; resize: none; overflow: hidden; overflow-wrap: anywhere; }
th .table-cell { padding-right: 34px; font-weight: 600; }
.table-cell::placeholder { color: var(--text-faint); font-weight: 400; }
.table-cell:focus { outline: 2px solid var(--focus); outline-offset: -2px; background: var(--accent-soft); }
.table-cell-readonly { padding: 10px 12px; min-width: 140px; white-space: pre-wrap; overflow-wrap: anywhere; }
.table-editor > .table-scroll { margin-left: -28px; max-width: none; width: calc(100% + 28px); }
.table-footer { display: flex; align-items: center; gap: 4px; margin: 6px 0 0; }
.table-button { border: 0; border-radius: 4px; background: transparent; color: var(--text-soft); font-size: 12px; padding: 6px 9px; min-height: 30px; cursor: pointer; }
.table-button:hover:not(:disabled) { background: var(--sidebar-hover); color: var(--text); }
.table-button:disabled { opacity: .4; cursor: default; }
.table-size { margin-left: auto; padding-right: 4px; color: var(--text-faint); font-size: 11px; font-variant-numeric: tabular-nums; }
.table-row-handle .table-button { padding: 3px; width: 25px; font-size: 18px; }
.table-column-menu { position: absolute; top: 5px; right: 4px; padding: 3px; width: 24px; font-size: 18px; }
@media (hover: hover) {
  .table-row-handle .table-button, .table-column-menu { opacity: 0; }
  .document-table tr:hover .table-row-handle .table-button, .document-table th:hover .table-column-menu, .document-table :focus-within > .table-button, .document-table .table-button:focus-visible { opacity: 1; }
}
.table-axis-menu { position: absolute; z-index: 20; width: 220px; padding: 5px; background: var(--popover); border: 1px solid var(--border); border-radius: 6px; box-shadow: var(--shadow); }
.table-axis-menu .table-button { display: block; width: 100%; text-align: left; font-size: 13px; padding: 9px; }
.table-axis-menu .danger { color: var(--text); border-top: 1px solid var(--border); border-radius: 0; }
.table-empty { color: var(--text-soft); font-size: 14px; padding: 16px; border: 1px dashed var(--border); }
@media (max-width: 760px) {
  .table-cell { min-width: 160px; }
  .table-button { min-height: 36px; }
  .table-footer { gap: 0; }
  .table-size { font-size: 11px; }
}

/* History is a protected reading space; the live document keeps synchronizing behind it. */
.history-dialog { width: min(1100px, calc(100vw - 48px)); height: min(830px, calc(100dvh - 48px)); max-width: none; max-height: none; padding: 0; color: var(--text); background: var(--bg); border: 1px solid var(--border); border-radius: 12px; overflow: hidden; }
.history-dialog[open] { display: flex; flex-direction: column; }
.history-dialog::backdrop { background: rgb(0 0 0 / .6); }
.history-dialog [hidden] { display: none !important; }
.history-dialog :is(button,input):focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }
.history-dialog button, .history-dialog input { font: inherit; }
.history-dialog button { cursor: pointer; }
.history-dialog button:disabled, .topbar-right button:disabled { opacity: .45; cursor: default; }
.history-header { padding: 20px 24px; border-bottom: 1px solid var(--border); display: flex; justify-content: space-between; gap: 16px; align-items: center; }
.history-header h2 { font-size: 21px; line-height: 1.25; margin: 0 0 4px; letter-spacing: -.02em; }
.history-header p { margin: 0; color: var(--history-muted); overflow-wrap: anywhere; }
.history-header > div:first-child { min-width: 0; }
.history-header-actions { display: flex; gap: 8px; flex-shrink: 0; }
.history-button { padding: 8px 12px; border: 1px solid var(--border); border-radius: 6px; background: transparent; color: var(--text); line-height: 1.4; }
.history-button:hover:not(:disabled) { background: var(--sidebar-hover); }
.history-button.history-primary { background: var(--accent); border-color: var(--accent); color: white; }
.history-button.history-primary:hover:not(:disabled) { background: #245bdd; }
.history-message { margin: 0; padding: 11px 24px; color: var(--text); background: var(--accent-soft); font-size: 14px; }
.history-message.is-error { background: var(--history-remove-bg); }
.history-layout { display: grid; grid-template-columns: 260px minmax(0, 1fr); min-height: 0; flex: 1; }
.history-sidebar { background: var(--sidebar); border-right: 1px solid var(--border); min-height: 0; display: flex; flex-direction: column; gap: 12px; padding: 16px 12px; }
.history-filter { display: flex; gap: 8px; align-items: center; font-size: 13px; color: var(--history-muted); }
.history-filter input { accent-color: var(--accent); }
.history-list { overflow: auto; min-height: 0; flex: 1; scrollbar-color: var(--text-faint) transparent; }
.history-version { display: block; width: 100%; margin-bottom: 4px; padding: 13px 12px; border: 0; border-radius: 6px; background: transparent; color: var(--text); text-align: left; overflow-wrap: anywhere; }
.history-version:hover { background: var(--sidebar-hover); }
.history-version[aria-pressed='true'] { background: var(--sidebar-active); }
.history-version strong { display: block; font-size: 14px; font-weight: 600; margin-bottom: 5px; }
.history-version span { display: block; font-size: 12px; line-height: 1.6; color: var(--history-muted); font-variant-numeric: tabular-nums; }
.history-help { padding: 4px 4px 0; }
.history-help p { margin: 10px 0 0; font-size: 12px; line-height: 1.55; color: var(--history-muted); }
.history-detail { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.history-version-head { padding: 22px 28px 10px; }
.history-version-head h3 { margin: 0 0 7px; font-size: 18px; line-height: 1.4; overflow-wrap: anywhere; }
.history-version-head p { margin: 0; font-size: 13px; color: var(--history-muted); }
.history-tabs { display: flex; gap: 18px; padding: 0 28px; border-bottom: 1px solid var(--border); }
.history-tabs button { color: var(--history-muted); border: 0; border-bottom: 2px solid transparent; padding: 12px 0; background: transparent; text-align: left; line-height: 1.4; }
.history-tabs button[aria-selected='true'] { color: var(--text); border-bottom-color: var(--text); }
.history-content { flex: 1; min-height: 80px; overflow: auto; padding: 24px 28px; scrollbar-color: var(--text-faint) transparent; overflow-wrap: anywhere; }
.history-document-title { margin: 0 0 24px; font-size: 25px; line-height: 1.3; letter-spacing: -.02em; }
.history-block { white-space: pre-wrap; line-height: 1.65; margin: 8px 0; max-width: 72ch; }
.history-block-h1 { font-size: 24px; margin-top: 26px; }
.history-block-h2 { font-size: 21px; margin-top: 22px; }
.history-block-h3 { font-size: 18px; margin-top: 20px; }
.history-block-code { background: var(--code-bg); border-radius: 6px; padding: 14px; overflow: auto; }
.history-block-code code { white-space: pre-wrap; overflow-wrap: anywhere; }
.history-code-language { display: block; color: var(--history-muted); font: 12px "Segoe UI", sans-serif; margin-bottom: 8px; }
.history-block-quote { margin-left: 0; padding-left: 14px; border-left: 1px solid var(--quote-bar); font-style: italic; }
.history-block-todo { display: flex; gap: 8px; align-items: baseline; }
.history-block-image { margin: 16px 0; }
.history-block-image img { max-width: 100%; max-height: 260px; object-fit: contain; }
.history-block-image figcaption { color: var(--history-muted); font-size: 13px; }
.history-link { text-decoration: underline; text-underline-offset: 3px; }
.history-empty { color: var(--history-muted); line-height: 1.65; padding: 12px; }
.history-diff-summary { margin: 0 0 22px; color: var(--history-muted); font-size: 13px; }
.history-difference { border-top: 1px solid var(--border); padding: 18px 0; }
.history-difference h4 { font-size: 14px; margin: 0 0 12px; font-weight: 600; }
.history-diff-pair { display: grid; grid-template-columns: minmax(0,1fr) minmax(0,1fr); gap: 12px; }
.history-diff-pair h5 { font-size: 12px; color: var(--history-muted); font-weight: 400; margin: 0 0 7px; }
.history-diff-pair pre { margin: 0; padding: 12px; white-space: pre-wrap; overflow-wrap: anywhere; font: inherit; font-size: 13px; line-height: 1.65; background: var(--bg-elev); }
.history-difference-added .history-diff-pair > div:last-child pre { background: var(--history-add-bg); }
.history-difference-removed .history-diff-pair > div:first-child pre { background: var(--history-remove-bg); }
.history-footer { padding: 16px 28px; border-top: 1px solid var(--border); }
.history-footer label { display: block; font-size: 13px; margin-bottom: 8px; }
.history-name-row { display: flex; gap: 8px; }
.history-name-row input { min-width: 0; flex: 1; color: var(--text); background: var(--code-bg); border: 1px solid var(--border); padding: 8px 10px; border-radius: 6px; }
.history-name-row input::placeholder { color: var(--history-muted); }
.history-restore-row { display: flex; gap: 14px; justify-content: space-between; align-items: center; margin-top: 16px; }
.history-restore-row span { font-size: 12px; color: var(--history-muted); line-height: 1.5; }
.history-restore-row button { flex-shrink: 0; }
.history-confirm { border-top: 1px solid var(--border); margin-top: 14px; padding-top: 4px; }
.history-confirm p { font-size: 13px; line-height: 1.5; }
.history-confirm > div { display: flex; justify-content: flex-end; gap: 8px; }
@media (max-width: 760px) {
  .history-dialog { width: 100vw; height: 100dvh; margin: 0; border: 0; border-radius: 0; }
  .history-header { padding: 16px; align-items: flex-start; }
  .history-header h2 { font-size: 19px; }
  .history-header p { font-size: 13px; }
  .history-header-actions { gap: 4px; }
  .history-header-actions button { padding: 6px 8px; font-size: 12px; }
  .history-layout { display: flex; flex-direction: column; overflow: auto; }
  .history-sidebar { border-right: 0; border-bottom: 1px solid var(--border); padding: 12px 16px; flex-shrink: 0; }
  .history-list { display: flex; overflow-x: auto; flex: none; gap: 6px; }
  .history-version { width: 210px; flex: 0 0 210px; padding: 10px; margin: 0; }
  .history-help { display: flex; gap: 14px; padding: 0; align-items: center; }
  .history-help p { margin: 0; font-size: 12px; }
  .history-help button { font-size: 12px; flex: 0 0 112px; padding: 6px; }
  .history-detail { flex: 1 0 auto; }
  .history-version-head { padding: 18px 16px 8px; }
  .history-tabs { padding: 0 16px; gap: 16px; font-size: 13px; }
  .history-content { overflow: visible; padding: 20px 16px; flex: none; min-height: 200px; }
  .history-diff-pair { grid-template-columns: minmax(0,1fr); gap: 10px; }
  .history-footer { padding: 16px; }
  .history-restore-row { flex-direction: column; align-items: stretch; }
  .history-message { padding: 10px 16px; }
  #history-open, #undo-edit, #redo-edit { flex-shrink: 0; }
}

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 15px;
  -webkit-font-smoothing: antialiased;
}

#app {
  display: flex;
  height: 100dvh;
  overflow: hidden;
}

/* ------------------------------- sidebar ------------------------------ */

#sidebar {
  width: 260px;
  flex: 0 0 260px;
  background: var(--sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 12px 10px;
  gap: 10px;
  overflow: hidden;
}

#sidebar.collapsed { display: none; }

.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 2px 2px 4px;
}

.workspace {
  display: flex;
  align-items: center;
  gap: 8px;
  background: none;
  border: 0;
  color: var(--text);
  font: inherit;
  font-weight: 600;
  padding: 8px;
  border-radius: var(--radius);
  cursor: pointer;
  flex: 1;
  text-align: left;
}
.workspace:hover { background: var(--sidebar-hover); }
.ws-logo {
  width: 28px; height: 28px;
  border-radius: 6px;
  background: var(--accent);
  color: #fff;
  display: grid; place-items: center;
  font-size: 15px; font-weight: 650;
}

.search-trigger {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  background: none;
  border: 1px solid var(--border);
  color: var(--text-soft);
  font: inherit;
  padding: 9px 10px;
  border-radius: var(--radius);
  cursor: pointer;
}
.search-trigger:hover { background: var(--sidebar-hover); color: var(--text); }
.search-trigger .st-icon { font-size: 15px; }
.search-trigger kbd {
  margin-left: auto;
  font-size: 10px;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 1px 4px;
  color: var(--text-faint);
}

.sidebar-section {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 8px 2px;
}
.section-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-faint);
  font-weight: 600;
}

.tree {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 2px 0 12px;
}

.tree-item {
  position: relative;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px 6px;
  border-radius: 6px;
  cursor: pointer;
  color: var(--text-soft);
  user-select: none;
  min-height: 36px;
}
.tree-item:hover { background: var(--sidebar-hover); }
.tree-item:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.tree-item.active { background: var(--sidebar-active); color: var(--text); font-weight: 500; }
.tree-item.drop-into { outline: 2px solid var(--accent); outline-offset: -2px; }
.tree-item.drop-before { box-shadow: inset 0 2px 0 var(--accent); }
.tree-item.drop-after { box-shadow: inset 0 -2px 0 var(--accent); }
.tree-item.dragging { opacity: 0.4; }

.tw-twisty {
  width: 18px; height: 18px;
  border: 0; background: none; color: var(--text-faint);
  border-radius: 4px; cursor: pointer;
  display: grid; place-items: center;
  font-size: 10px;
  flex: 0 0 18px;
  transition: transform 0.12s ease;
}
.tw-twisty:hover { background: var(--sidebar-hover); color: var(--text); }
.tw-twisty.open { transform: rotate(90deg); }
.tw-twisty.hidden { visibility: hidden; }
.tw-twisty .ui-icon { width: 12px; height: 12px; }

.tw-icon { flex: 0 0 16px; text-align: center; font-size: 14px; }
.tw-title {
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: 14px;
}
.tw-actions { display: flex; gap: 2px; opacity: 0; }
.tree-item:hover .tw-actions, .tree-item:focus-within .tw-actions { opacity: 1; }
.tw-actions .icon-btn { width: 22px; height: 22px; font-size: 13px; }

.sidebar-foot { padding: 4px 2px 2px; border-top: 1px solid var(--border); }

/* ------------------------------ main/header --------------------------- */

#resizer { width: 5px; cursor: col-resize; background: transparent; }
#resizer:hover { background: var(--accent-soft); }

#main { flex: 1; display: flex; flex-direction: column; min-width: 0; }

#topbar {
  height: 56px;
  flex: 0 0 56px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 20px;
  border-bottom: 1px solid var(--border);
}

.breadcrumbs {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--text-soft);
  font-size: 13px;
  overflow: hidden;
  white-space: nowrap;
}
.breadcrumbs .crumb { cursor: pointer; padding: 5px 6px; border-radius: 4px; overflow: hidden; text-overflow: ellipsis; }
.breadcrumbs .crumb:last-child { color: var(--text); }
.breadcrumbs .crumb:hover { background: var(--sidebar-hover); color: var(--text); }
.breadcrumbs .sep { color: var(--text-faint); }

.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 4px; flex-shrink: 0; }
.save-status { font-size: 12px; color: var(--text-faint); text-align: right; padding: 0 6px; white-space: nowrap; }
.save-status:empty { display: none; }
.save-status.saving { color: var(--text-soft); }
.save-status.error { color: #e5534b; }

.page-scroll { flex: 1; overflow-y: auto; overflow-x: hidden; scroll-behavior: smooth; scrollbar-gutter: stable; }

.page {
  max-width: 880px;
  margin: 0 auto;
  padding: 64px 80px 140px;
}

.page-head { position: relative; display: flex; align-items: center; gap: 12px; margin-bottom: 28px; }
.page-icon {
  position: absolute;
  left: -56px;
  top: 3px;
  font-size: 32px;
  line-height: 1;
  min-width: 42px;
  outline: none;
  border-radius: 6px;
  padding: 5px;
}
.page-icon:empty::before { content: '📄'; opacity: 0.35; }
.page-icon:focus { background: var(--bg-elev); }
.page-title {
  font-size: 40px;
  font-weight: 650;
  line-height: 1.2;
  letter-spacing: -0.025em;
  overflow-wrap: anywhere;
  margin: 0;
  outline: none;
  flex: 1;
  min-height: 48px;
  padding: 0;
  border-radius: 6px;
}
.page-title:empty::before { content: 'Sans titre'; color: var(--text-faint); }
.page-title:focus { background: var(--bg-elev); }

/* ------------------------------- blocks ------------------------------- */

.blocks { margin-top: 0; font-size: 16px; }

.block {
  position: relative;
  display: flex;
  align-items: flex-start;
  gap: 2px;
  border-radius: 6px;
  padding: 0;
  margin: 8px 0;
}
.block.dragging { opacity: 0.4; }
.block.drop-before::before,
.block.drop-after::after {
  content: '';
  position: absolute;
  left: 0; right: 0;
  height: 2px;
  background: var(--accent);
  border-radius: 2px;
}
.block.drop-before::before { top: -1px; }
.block.drop-after::after { bottom: -1px; }

.block-controls {
  position: absolute;
  left: calc(-1 * var(--block-gutter));
  top: 3px;
  width: var(--block-gutter);
  display: flex;
  align-items: center;
  gap: 2px;
  opacity: 0;
  transition: opacity 0.1s ease;
  padding: 0 4px 0 0;
}
.block:hover > .block-controls, .block:focus-within > .block-controls { opacity: 1; }
.drag-handle, .block-add, .block-delete {
  width: 24px; height: 28px;
  border: 0; background: none;
  color: var(--text-faint);
  cursor: pointer;
  border-radius: 4px;
  font-size: 12px;
  display: grid; place-items: center;
}
.block-controls .ui-icon { width: 15px; height: 15px; }
.drag-handle { cursor: grab; }
.drag-handle:active { cursor: grabbing; }
.drag-handle:hover, .block-add:hover { background: var(--sidebar-hover); color: var(--text); }
.block-delete:hover { background: var(--sidebar-hover); color: #e5534b; }

.block-body { flex: 1; min-width: 0; }

.editable {
  outline: none;
  padding: 3px 0;
  border-radius: 4px;
  min-height: 32px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  line-height: 1.75;
}
.editable:focus { background: var(--bg-elev); box-shadow: inset 0 -1px var(--border); }
.editable:empty::before { content: attr(data-placeholder); color: var(--text-faint); pointer-events: none; }

.block[data-type='h1'] { margin-top: 36px; margin-bottom: 12px; }
.block[data-type='h2'] { margin-top: 32px; margin-bottom: 10px; }
.block[data-type='h3'] { margin-top: 24px; margin-bottom: 6px; }
.block[data-type='h1'] .editable { font-size: 30px; font-weight: 650; line-height: 1.3; letter-spacing: -0.02em; }
.block[data-type='h2'] .editable { font-size: 24px; font-weight: 650; line-height: 1.35; letter-spacing: -0.015em; }
.block[data-type='h3'] .editable { font-size: 19px; font-weight: 600; line-height: 1.45; letter-spacing: -0.01em; }
.block:is([data-type='h1'],[data-type='h2']) + .block[data-type='h3'] { margin-top: 14px; }
.block:is([data-type='bullet'],[data-type='todo']) { margin: 2px 0; }

.block[data-type='quote'] { margin: 20px 0; }
.block[data-type='quote'] .block-body { border-left: 1px solid var(--quote-bar); padding-left: 20px; }
.block[data-type='quote'] .editable { color: var(--text-soft); }

.block[data-type='bullet'] .editable::before {
  content: '•';
  display: inline-block;
  width: 18px;
  margin-left: -18px;
  color: var(--text-soft);
}
.block[data-type='bullet'] .block-body { padding-left: 20px; }

.block[data-type='todo'] { align-items: flex-start; }
.todo-check {
  flex: 0 0 18px;
  width: 16px; height: 16px;
  margin-top: 9px;
  accent-color: var(--accent);
  cursor: pointer;
}
.block[data-type='todo'] .block-body { display: flex; gap: 6px; }
.block[data-type='todo'] .editable { flex: 1; }
.block.done .editable { text-decoration: line-through; color: var(--text-faint); }

.code-wrap {
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
  margin: 12px 0;
}
.code-lang {
  background: none;
  border: 0;
  color: var(--text-faint);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 6px;
  outline: none;
  font-family: var(--font-mono);
}
.code-input {
  width: 100%;
  background: none;
  border: 0;
  resize: vertical;
  color: var(--text);
  font-family: var(--font-mono);
  font-size: 13px;
  line-height: 1.6;
  outline: none;
  min-height: 30px;
  tab-size: 2;
}

.image-wrap { margin: 6px 0; }
.image-wrap img {
  max-width: 100%;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  display: block;
}
.image-placeholder {
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  padding: 26px;
  text-align: center;
  color: var(--text-faint);
  cursor: pointer;
  font-size: 13px;
}
.image-placeholder:hover { border-color: var(--accent); color: var(--text-soft); }
.image-caption {
  font-size: 12px;
  color: var(--text-faint);
  outline: none;
  margin-top: 4px;
}
.image-caption:empty::before { content: attr(data-placeholder); color: var(--text-faint); }
.image-actions { margin-top: 4px; display: flex; gap: 12px; font-size: 12px; }
.image-actions button {
  background: none; border: 0; color: var(--text-faint);
  cursor: pointer; padding: 0; font: inherit;
}
.image-actions button:hover { color: var(--text); text-decoration: underline; }

.divider-block { border: 0; border-top: 1px solid var(--border); margin: 14px 0; width: 100%; }

.add-block {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  text-align: left;
  background: none;
  border: 0;
  color: var(--text-faint);
  font: inherit;
  padding: 12px 0;
  margin-top: 20px;
  cursor: pointer;
  border-radius: 6px;
}
.add-block:hover { background: var(--sidebar-hover); color: var(--text); }
.add-block .ui-icon { margin-left: 4px; }

.wikilink, .inline-link, .history-link { color: var(--link); text-decoration: underline; text-decoration-color: var(--border); text-underline-offset: 4px; text-decoration-thickness: 1px; cursor: pointer; border-radius: 2px; }
.wikilink:hover, .inline-link:hover { background: var(--accent-soft); text-decoration-color: currentColor; }
.wikilink.missing { color: #d68a2c; border-bottom-style: dashed; }
.editable code, .history-block:not(.history-block-code) code {
  display: inline-block;
  max-width: 100%;
  vertical-align: baseline;
  background: var(--code-bg);
  color: var(--code-text);
  border-radius: 4px;
  padding: 1px 6px;
  font-family: var(--font-mono);
  font-size: 0.82em;
  line-height: 1.6;
  overflow-wrap: anywhere;
}

/* ----------------------------- backlinks ------------------------------ */

.backlinks { margin-top: 64px; border-top: 1px solid var(--border); padding-top: 24px; }
.backlinks h3 { font-size: 12px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--text-faint); margin: 0 0 10px; }
.backlink-item {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 8px; border-radius: 6px; cursor: pointer; color: var(--text-soft);
}
.backlink-item:hover { background: var(--sidebar-hover); color: var(--text); }
.backlinks .empty { color: var(--text-faint); font-size: 13px; }

/* ------------------------------ overlays ------------------------------ */

.overlay {
  position: fixed; inset: 0;
  background: rgba(0, 0, 0, 0.45);
  display: flex; align-items: flex-start; justify-content: center;
  padding-top: 14vh;
  z-index: 60;
}
.overlay.hidden, .popover.hidden, .dropdown.hidden, .toast.hidden { display: none; }

.palette {
  width: min(620px, 92vw);
  background: var(--popover);
  border-radius: 12px;
  box-shadow: var(--shadow);
  overflow: hidden;
}
#search-input {
  width: 100%;
  border: 0;
  border-bottom: 1px solid var(--border);
  background: none;
  color: var(--text);
  font-size: 16px;
  padding: 16px 18px;
  outline: none;
}
.search-results { max-height: 52vh; overflow-y: auto; padding: 6px; }
.result {
  padding: 9px 12px; border-radius: 8px; cursor: pointer;
  display: flex; align-items: center; gap: 10px;
}
.result.active, .result:hover { background: var(--accent-soft); }
.result .r-icon { font-size: 16px; }
.result .r-main { min-width: 0; flex: 1; }
.result .r-title { font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.result .r-snippet { font-size: 12px; color: var(--text-faint); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.results-empty { padding: 20px; text-align: center; color: var(--text-faint); font-size: 13px; }
.palette-foot { padding: 8px 14px; border-top: 1px solid var(--border); font-size: 11px; color: var(--text-faint); display: flex; gap: 4px; align-items: center; }
.palette-foot kbd, .results-empty kbd { border: 1px solid var(--border); border-radius: 4px; padding: 1px 4px; }

.popover, .dropdown {
  position: fixed;
  background: var(--popover);
  border-radius: 10px;
  box-shadow: var(--shadow);
  z-index: 70;
  overflow: hidden;
  padding: 6px;
  min-width: 210px;
}
.popover .menu-head, .dropdown .menu-head {
  font-size: 11px; text-transform: uppercase; letter-spacing: 0.05em;
  color: var(--text-faint); padding: 6px 8px 4px;
}
.menu-item {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 10px; border-radius: 6px; cursor: pointer; font-size: 14px;
}
.menu-item .mi-icon {
  width: 24px; height: 24px; flex: 0 0 24px;
  display: grid; place-items: center;
  border: 1px solid var(--border); border-radius: 5px;
  font-size: 12px; background: var(--bg-elev);
}
.menu-item .mi-icon .ui-icon { width: 15px; height: 15px; }
.menu-item .mi-text { min-width: 0; }
.menu-item .mi-desc { font-size: 11px; color: var(--text-faint); }
.menu-item.active, .menu-item:hover { background: var(--accent-soft); }

.toast {
  position: fixed;
  bottom: 22px; left: 50%; transform: translateX(-50%);
  background: var(--popover);
  color: var(--text);
  padding: 9px 16px;
  border-radius: 999px;
  box-shadow: var(--shadow);
  font-size: 13px;
  z-index: 90;
}

/* ------------------------------ buttons ------------------------------- */

.icon-btn {
  width: 32px; height: 32px;
  border: 0; background: none;
  color: var(--text-soft);
  border-radius: 6px; cursor: pointer;
  display: grid; place-items: center;
  font-size: 15px;
}
.icon-btn:hover { background: var(--sidebar-hover); color: var(--text); }
.icon-btn.small { width: 24px; height: 24px; font-size: 15px; }

.ghost-btn {
  border: 0; background: none; color: var(--text-soft);
  font: inherit; padding: 8px 10px; border-radius: 6px; cursor: pointer;
  text-align: left;
}
.ghost-btn { display: flex; align-items: center; gap: 9px; min-height: 38px; }
.ghost-btn:hover { background: var(--sidebar-hover); color: var(--text); }
.ghost-btn.full { width: 100%; }

.menu-item.danger { color: #e5534b; }
.menu-item.danger .mi-icon { color: #e5534b; }

::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 6px; border: 2px solid transparent; background-clip: padding-box; }
::-webkit-scrollbar-thumb:hover { background: var(--text-faint); background-clip: padding-box; }

@media (max-width: 760px) {
  #sidebar { position: absolute; z-index: 30; height: 100%; }
  #resizer { display: none; }
  #topbar { height: 52px; flex-basis: 52px; padding: 0 10px; gap: 4px; }
  .breadcrumbs { min-width: 0; flex: 1; gap: 2px; }
  .breadcrumbs .crumb { padding: 4px 2px; }
  .breadcrumbs .crumb:not(:last-child), .breadcrumbs .sep { display: none; }
  .topbar-right { gap: 0; }
  .topbar-right .icon-btn { width: 30px; }
  .save-status { max-width: 76px; overflow: hidden; text-overflow: ellipsis; padding: 0 3px; font-size: 11px; }
  .page { padding: 36px 28px 100px 36px; }
  .page-head { margin-bottom: 24px; gap: 10px; }
  .page-icon { position: static; font-size: 28px; min-width: 34px; padding: 0; }
  .page-title { font-size: 32px; min-height: auto; }
  .blocks { font-size: 15px; }
  .block-controls { left: -30px; width: 28px; flex-direction: column; padding: 0; z-index: 2; }
  .block-controls .block-add, .block-controls .block-delete { display: none; }
  .block:focus-within > .block-controls .block-add, .block:focus-within > .block-controls .block-delete { display: grid; }
  .block[data-type='h2'] .editable { font-size: 22px; }
  .code-wrap { padding: 12px; }
  #connection-status { display: none; }
}
@media (hover: none) {
  .block-controls { opacity: 1; }
  .tw-actions { opacity: 1; }
  .icon-btn { min-width: 32px; min-height: 36px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition: none !important; }
}

.connection-panel { max-width: 560px; margin: 12vh auto; padding: 28px; color: var(--text); }
.connection-panel h1 { font-size: 26px; margin-bottom: 22px; }
.connection-panel p { line-height: 1.6; margin-bottom: 16px; color: var(--text-soft); }
.connection-panel button { border: 1px solid var(--border); }
