  *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

  :root {
    --ink: #1a1714;
    --paper: #f5f0e8;
    --paper-dark: #ede8de;
    --accent: #c84b2f;
    --accent2: #2a6049;
    --muted: #8a8078;
    --border: #d4cec4;
    --shadow: rgba(26,23,20,0.12);
    --raw-panel-width: 680px;
  }

  body {
    font-family: 'DM Sans', sans-serif;
    background: var(--paper);
    color: var(--ink);
    min-height: 100vh;
    display: flex;
    flex-direction: column;
  }

  body.busy {
    cursor: wait;
  }

  body.resizing-raw {
    user-select: none;
    cursor: ew-resize;
  }

  /* ── Header ── */
  .app-header {
    position: sticky;
    top: 0;
    z-index: 1020;
    padding: 10px 16px;
    background:
      linear-gradient(180deg, rgba(26,23,20,0.99), rgba(26,23,20,0.94)),
      radial-gradient(circle at top left, rgba(200,75,47,0.22), transparent 36%),
      var(--ink);
    color: var(--paper);
    border-bottom: 1px solid rgba(245,240,232,0.08);
    box-shadow: 0 8px 22px rgba(26,23,20,0.12);
    backdrop-filter: blur(10px);
  }

  .header-shell {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    flex-wrap: wrap;
  }

  .logo {
    font-family: 'Fraunces', serif;
    font-size: 1.3rem;
    font-weight: 300;
    letter-spacing: 0.02em;
    display: flex;
    align-items: center;
    gap: 10px;
  }

  .logo-badge {
    background: var(--accent);
    color: white;
    font-size: 0.6rem;
    font-family: 'JetBrains Mono', monospace;
    padding: 2px 6px;
    border-radius: 3px;
    letter-spacing: 0.1em;
    text-transform: uppercase;
  }

  .header-primary,
  .header-group {
    display: flex;
    gap: 6px;
    align-items: center;
    flex-wrap: wrap;
    position: relative;
  }

  .header-group {
    padding: 0;
    border: none;
    background: transparent;
    border-radius: 0;
  }

  .header-group-import-export {
    padding: 4px 6px;
    border-radius: 999px;
    background: rgba(222,235,255,0.08);
    border-color: rgba(222,235,255,0.14);
  }
  .header-group-edit {
    padding: 4px 6px;
    border-radius: 999px;
    background: rgba(233,245,232,0.08);
    border-color: rgba(233,245,232,0.14);
  }

  .header-actions {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 6px;
    flex-wrap: wrap;
  }

  .header-actions-primary {
    padding-left: 8px;
    margin-left: 1px;
  }

  .header-lang-group {
    margin-left: auto;
    padding-left: 8px;
  }

  .btn {
    font-family: 'DM Sans', sans-serif;
    font-size: 0.79rem;
    font-weight: 500;
    padding: 6px 12px;
    border-radius: 8px;
    border: none;
    cursor: pointer;
    transition: all 0.15s;
    letter-spacing: 0.02em;
    white-space: nowrap;
  }

  .btn-with-icon,
  .btn-ghost,
  .btn-newdoc,
  .btn-publish,
  .lang-btn,
  .import-trigger,
  .export-trigger,
  .guide-trigger {
    display: inline-flex;
    align-items: center;
    gap: 7px;
  }

  .ui-icon {
    width: 14px;
    height: 14px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 14px;
  }

  .ui-icon svg {
    width: 14px;
    height: 14px;
    display: block;
    stroke: currentColor;
    fill: none;
    stroke-width: 1.8;
    stroke-linecap: round;
    stroke-linejoin: round;
  }

  .btn-text {
    display: inline-block;
  }

  .btn-caret {
    font-size: 0.72rem;
    line-height: 1;
    opacity: 0.9;
    transform: translateY(-0.5px);
  }

  .btn-ghost {
    background: rgba(245,240,232,0.04);
    color: rgba(245,240,232,0.86);
    border: 1px solid rgba(245,240,232,0.14);
  }
  .btn-ghost:hover { background: rgba(245,240,232,0.08); color: var(--paper); }
  .btn-ghost.active { background: rgba(245,240,232,0.1); color: var(--paper); }

  body.raw-open #btnMarkdown,
  body.history-open #historyBtn {
    color: var(--paper);
    border-color: transparent;
  }

  body.raw-open #btnMarkdown {
    background: rgba(42,96,73,0.34);
    box-shadow: 0 8px 18px rgba(42,96,73,0.18);
  }

  body.history-open #historyBtn {
    background: rgba(200,75,47,0.28);
    box-shadow: 0 8px 18px rgba(200,75,47,0.16);
  }

  /* ── Language selector ── */
  .lang-selector {
    position: relative;
    display: flex;
    align-items: center;
  }
  .lang-btn {
    background: rgba(245,240,232,0.04);
    border: 1px solid rgba(245,240,232,0.14);
    color: rgba(245,240,232,0.86);
    font-family: 'DM Sans', sans-serif;
    font-size: 0.76rem;
    padding: 5px 9px;
    border-radius: 6px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 5px;
    transition: all 0.15s;
    white-space: nowrap;
  }
  .lang-btn:hover { background: rgba(245,240,232,0.08); color: var(--paper); }
  .lang-dropdown {
    position: absolute;
    top: calc(100% + 6px);
    right: 0;
    background: var(--paper);
    border: 1px solid var(--border);
    border-radius: 10px;
    box-shadow: 0 12px 40px rgba(0,0,0,0.18);
    z-index: 2000;
    display: none;
    flex-direction: column;
    min-width: 180px;
    max-height: 360px;
    overflow-y: auto;
    padding: 4px;
    scrollbar-width: thin;
  }
  .lang-dropdown.open { display: flex; }
  .lang-option {
    padding: 7px 12px;
    font-size: 0.82rem;
    font-family: 'DM Sans', sans-serif;
    color: var(--ink);
    cursor: pointer;
    border-radius: 6px;
    display: flex;
    align-items: center;
    gap: 8px;
    transition: background 0.1s;
    white-space: nowrap;
  }
  .lang-option:hover { background: var(--paper-dark); }
  .lang-option.active { background: rgba(200,75,47,0.08); color: var(--accent); font-weight: 500; }

  .btn-accent {
    background: var(--accent);
    color: white;
  }
  .btn-accent:hover { background: #b03d22; }

  .btn-newdoc {
    background: linear-gradient(180deg, rgba(255,233,219,0.98), rgba(245,205,181,0.98));
    color: #5a1f14;
    border: 1px solid rgba(200,75,47,0.22);
    box-shadow: 0 10px 24px rgba(200,75,47,0.18);
    border-radius: 8px;
  }
  .btn-newdoc:hover {
    background: linear-gradient(180deg, rgba(255,239,229,1), rgba(248,216,196,1));
    color: #41150d;
  }

  .btn-publish {
    background: rgba(42,96,73,0.9);
    color: rgba(255,255,255,0.96);
    font-family: 'DM Sans', sans-serif;
    font-size: 0.79rem;
    font-weight: 500;
    padding: 6px 12px;
    border-radius: 8px;
    border: none;
    cursor: pointer;
    transition: all 0.15s;
    letter-spacing: 0.02em;
    border-radius: 8px;
  }
  .btn-publish:hover { background: rgba(30,74,54,0.96); }
  .btn-publish.publishing {
    opacity: 0.7;
    cursor: wait;
    animation: pulse 1.2s ease-in-out infinite;
  }

  .btn-danger {
    background: rgba(192,57,43,0.09);
    color: #b63b2b;
    border: 1px solid rgba(192,57,43,0.24);
  }
  .btn-danger:hover {
    background: rgba(192,57,43,0.14);
    color: #9e2e20;
    border-color: rgba(192,57,43,0.35);
  }
  .btn-danger:disabled {
    opacity: 0.55;
    cursor: not-allowed;
  }

  .import-menu,
  .export-menu {
    position: relative;
    display: flex;
    align-items: center;
  }

  .guide-menu {
    position: relative;
    display: flex;
    align-items: center;
  }

  .import-trigger,
  .export-trigger,
  .guide-trigger {
    gap: 6px;
  }

  .import-trigger {
    background: rgba(245,240,232,0.04);
    color: rgba(245,240,232,0.86);
    border-color: rgba(245,240,232,0.14);
  }
  .export-trigger {
    background: rgba(245,240,232,0.04);
    color: rgba(245,240,232,0.86);
    border-color: rgba(245,240,232,0.14);
  }
  .guide-trigger {
    background: rgba(245,240,232,0.04);
    color: rgba(245,240,232,0.86);
    border-color: rgba(245,240,232,0.14);
  }
  .import-trigger:hover,
  .export-trigger:hover,
  .guide-trigger:hover {
    box-shadow: 0 10px 20px rgba(0,0,0,0.12);
  }

  .import-dropdown,
  .export-dropdown,
  .guide-dropdown {
    position: absolute;
    top: calc(100% + 8px);
    right: 0;
    display: none;
    flex-direction: column;
    min-width: 190px;
    padding: 5px;
    border-radius: 12px;
    background: rgba(245,240,232,0.98);
    border: 1px solid var(--border);
    box-shadow: 0 16px 34px rgba(26,23,20,0.18);
    z-index: 2200;
  }

  .import-dropdown.open,
  .export-dropdown.open,
  .guide-dropdown.open { display: flex; }

  .import-item,
  .export-item,
  .guide-item {
    display: flex;
    align-items: center;
    gap: 8px;
    width: 100%;
    padding: 8px 10px;
    border: none;
    border-radius: 8px;
    background: transparent;
    color: var(--ink);
    font-family: 'DM Sans', sans-serif;
    font-size: 0.82rem;
    text-align: left;
    cursor: pointer;
    white-space: nowrap;
  }

  .import-item:hover,
  .export-item:hover,
  .guide-item:hover {
    background: var(--paper-dark);
  }

  .header-primary .btn,
  .header-group .btn {
    min-height: 30px;
  }

  .header-actions-primary .btn-publish {
    box-shadow: 0 10px 24px rgba(42,96,73,0.18);
  }

  .header-actions-primary .btn-accent {
    box-shadow: 0 10px 24px rgba(200,75,47,0.18);
  }

  @keyframes pulse { 0%,100%{opacity:0.7} 50%{opacity:1} }

  @media (max-width: 1100px) {
    .header-shell {
      flex-direction: column;
      align-items: stretch;
    }

    .header-actions {
      justify-content: flex-start;
    }

    .header-lang-group {
      margin-left: 0;
      padding-left: 0;
    }
  }

  @media (max-width: 720px) {
    .app-header {
      padding: 9px 10px;
    }

    .logo {
      font-size: 1.12rem;
    }

    .header-group {
      padding: 4px 5px;
      gap: 5px;
      border-radius: 12px;
    }

    .header-actions .btn {
      flex: 1 1 auto;
    }

    .import-dropdown,
    .export-dropdown,
    .guide-dropdown {
      min-width: 170px;
    }

    :root {
      --raw-panel-width: min(92vw, 560px);
    }

  }

  /* ── Publish modal ── */
  #publishOverlay {
    position: fixed;
    inset: 0;
    background: rgba(26,23,20,0.55);
    backdrop-filter: blur(3px);
    z-index: 3000;
    display: none;
    align-items: center;
    justify-content: center;
  }
  #publishOverlay.open { display: flex; }

  .publish-modal {
    background: var(--paper);
    border-radius: 14px;
    padding: 32px;
    width: 480px;
    max-width: 94vw;
    box-shadow: 0 24px 64px rgba(0,0,0,0.25);
    display: flex;
    flex-direction: column;
    gap: 20px;
  }

  .publish-modal h2 {
    font-family: 'Fraunces', serif;
    font-size: 1.4rem;
    font-weight: 600;
    color: var(--ink);
    margin: 0;
  }

  .publish-field { display: flex; flex-direction: column; gap: 6px; }
  .publish-label {
    font-size: 0.75rem;
    font-family: 'JetBrains Mono', monospace;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--muted);
  }
  .publish-input {
    font-family: 'DM Sans', sans-serif;
    font-size: 0.9rem;
    padding: 9px 12px;
    border: 1.5px solid var(--border);
    border-radius: 8px;
    background: var(--paper);
    color: var(--ink);
    outline: none;
    transition: border-color 0.15s;
  }
  .publish-input:focus { border-color: var(--accent2); }
  .publish-hint {
    font-size: 0.72rem;
    color: var(--muted);
    line-height: 1.5;
  }
  .publish-hint a { color: var(--accent2); }

  .publish-actions {
    display: flex;
    gap: 10px;
    justify-content: flex-end;
    margin-top: 4px;
  }

  .publish-status {
    border-radius: 8px;
    padding: 12px 16px;
    font-size: 0.82rem;
    line-height: 1.6;
    display: none;
  }
  .publish-status.info  { background: rgba(42,96,73,0.08); color: #1e4a36; border: 1px solid rgba(42,96,73,0.2); display: block; }
  .publish-status.error { background: rgba(192,57,43,0.08); color: #c0392b; border: 1px solid rgba(192,57,43,0.2); display: block; }
  .publish-status.success {
    background: rgba(42,96,73,0.1);
    color: #1e4a36;
    border: 1px solid rgba(42,96,73,0.3);
    display: block;
  }
  .publish-status.success a { color: #2a6049; font-weight: 600; }

  .publish-steps { display: flex; flex-direction: column; gap: 4px; }
  .publish-step {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 0.8rem;
    color: var(--muted);
    transition: color 0.2s;
  }
  .publish-step.done  { color: #2a6049; }
  .publish-step.doing { color: var(--ink); font-weight: 500; }
  .publish-step.err   { color: #c0392b; }
  .publish-step-icon { font-size: 0.9rem; width: 20px; text-align: center; }


  /* ── Main layout ── */
  .main {
    flex: 1;
    display: flex;
    /* No overflow:hidden — let children manage their own scroll */
    min-height: 0;
  }

  /* ── Toolbar: fixed to left edge, always visible ── */
  .toolbar {
    position: fixed;
    left: 0;
    top: 54px;
    bottom: 32px;
    width: 220px;
    background: var(--paper-dark);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    padding: 10px 8px 48px; /* bottom padding for toggle button */
    gap: 1px;
    overflow-y: auto;
    overflow-x: hidden;
    z-index: 50;
    transition: width 0.22s cubic-bezier(.4,0,.2,1);
    scrollbar-width: thin;
    scrollbar-color: var(--border) transparent;
  }

  .toolbar.collapsed {
    width: 56px;
  }

  /* Toggle button at bottom of toolbar */
  .toolbar-toggle {
    position: absolute;
    bottom: 12px;
    right: 8px;
    width: 28px;
    height: 28px;
    border-radius: 6px;
    border: 1px solid var(--border);
    background: var(--paper);
    color: var(--muted);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.7rem;
    transition: all 0.15s;
    flex-shrink: 0;
  }
  .toolbar-toggle:hover { color: var(--accent); border-color: var(--accent); background: var(--paper); }

  .toolbar.collapsed .toolbar-toggle {
    right: auto;
    left: 50%;
    transform: translateX(-50%);
  }

  /* Section label */
  .toolbar-section-title {
    font-size: 0.6rem;
    font-family: 'JetBrains Mono', monospace;
    text-transform: uppercase;
    letter-spacing: 0.12em;
    color: var(--muted);
    padding: 8px 8px 2px;
    white-space: nowrap;
    flex-shrink: 0;
    transition: opacity 0.15s, height 0.22s, padding 0.22s;
    position: sticky;
    top: 0;
    background: var(--paper-dark);
    z-index: 1;
  }

  .toolbar.collapsed .toolbar-section-title {
    opacity: 0;
    pointer-events: none;
    height: 0 !important;
    min-height: 0;
    padding: 0;
    overflow: hidden;
  }

  .toolbar-sep {
    width: calc(100% - 16px);
    height: 1px;
    background: var(--border);
    margin: 4px 8px;
    flex-shrink: 0;
  }

  .toolbar.collapsed .toolbar-sep {
    width: 32px;
    margin: 4px auto;
  }

  .tool-btn {
    position: relative;
    width: 100%;
    height: 34px;
    min-height: 34px;
    border-radius: 7px;
    border: none;
    background: transparent;
    color: var(--ink);
    cursor: grab;
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 0 8px;
    transition: background 0.12s, color 0.12s;
    flex-shrink: 0;
    overflow: hidden;
    white-space: nowrap;
  }

  .tool-btn:hover { background: var(--border); }
  .tool-btn:active { background: rgba(200,75,47,0.13); color: var(--accent); cursor: grabbing; }

  /* Ghost preview while dragging from toolbar */
  #toolbar-ghost {
    position: fixed;
    pointer-events: none;
    z-index: 9999;
    background: var(--paper);
    border: 1.5px solid var(--accent);
    border-radius: 8px;
    padding: 8px 14px;
    font-family: 'DM Sans', sans-serif;
    font-size: 0.88rem;
    color: var(--ink);
    box-shadow: 0 8px 28px rgba(0,0,0,0.18);
    white-space: nowrap;
    display: none;
    opacity: 0.92;
    transform: rotate(-1.5deg);
  }

  /* Editor highlight zone when dragging from toolbar */
  .editor-drop-active {
    outline: 2px dashed var(--accent) !important;
    outline-offset: -3px;
  }

  .tool-icon {
    width: 26px;
    height: 26px;
    min-width: 26px;
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-family: 'Fraunces', serif;
    font-weight: 600;
    background: var(--paper);
    color: var(--ink);
    border: 1px solid var(--border);
    transition: border-color 0.12s, color 0.12s;
    font-size: 0.75rem;
    pointer-events: none;
  }

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

  .tool-label {
    font-family: 'DM Sans', sans-serif;
    font-size: 0.82rem;
    font-weight: 400;
    color: var(--ink);
    transition: opacity 0.12s;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }

  .toolbar.collapsed .tool-label {
    opacity: 0;
    width: 0;
    pointer-events: none;
  }

  /* ── Global tooltip (only shown when collapsed) ── */
  #tip {
    position: fixed;
    background: var(--ink);
    color: var(--paper);
    font-family: 'DM Sans', sans-serif;
    font-size: 0.72rem;
    font-weight: 500;
    white-space: nowrap;
    padding: 5px 10px;
    border-radius: 6px;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.12s;
    z-index: 9999;
    box-shadow: 0 4px 12px rgba(0,0,0,0.18);
  }
  #tip.visible { opacity: 1; }
  #tip::before {
    content: '';
    position: absolute;
    right: 100%;
    top: 50%;
    transform: translateY(-50%);
    border: 5px solid transparent;
    border-right-color: var(--ink);
  }

  /* ── Editor area: offset by toolbar width ── */
  .editor-wrap {
    flex: 1;
    overflow-y: auto;
    padding: 40px;
    height: calc(100vh - 97px - 32px);
    margin-left: 220px;
    transition: margin-left 0.22s cubic-bezier(.4,0,.2,1), margin-right 0.22s;
  }

  body.toolbar-collapsed .editor-wrap { margin-left: 56px; }
  body.raw-open .editor-wrap { margin-right: var(--raw-panel-width); }

  .page {
    max-width: 860px;
    margin: 0 auto;
    min-height: 100%;
    position: relative;
  }

  /* Editable content area */
  .editable {
    outline: none;
    font-family: 'DM Sans', sans-serif;
    font-size: 1rem;
    line-height: 1.75;
    color: var(--ink);
    min-height: 400px;
  }

  /* Empty line block — shows placeholder when empty */
  .editable p.empty-line {
    min-height: 1.75em;
    display: block;
  }

  .editable:empty::before {
    content: 'Начните писать или выберите элемент из панели слева…';
    color: var(--muted);
    pointer-events: none;
  }

  /* ── Drag & Drop ── */
  /* block-wrapper is invisible — just a marker, no layout impact */
  .block-wrapper {
    display: contents; /* does NOT affect layout at all */
  }

  .block-wrapper.dragging > *:not(.drag-handle) {
    opacity: 0.3;
  }

  /* ── Floating global drag handle ── */
  #drag-handle {
    position: fixed;
    width: 20px;
    height: 28px;
    display: none;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    cursor: grab;
    color: var(--muted);
    border-radius: 4px;
    z-index: 200;
    transition: color 0.12s, background 0.12s;
    pointer-events: all;
  }
  #drag-handle::before {
    content: '';
    position: absolute;
    inset: -10px -12px;
  }
  #drag-handle.visible { display: flex; }
  #drag-handle:hover { color: var(--accent); background: rgba(200,75,47,0.1); }
  #drag-handle:active { cursor: grabbing; }

  #drag-label {
    position: fixed;
    display: none;
    align-items: center;
    justify-content: flex-end;
    width: 58px;
    height: 28px;
    padding-right: 8px;
    box-sizing: border-box;
    color: var(--accent);
    font-family: 'JetBrains Mono', monospace;
    font-size: 0.55rem;
    letter-spacing: 0.05em;
    text-transform: uppercase;
    white-space: nowrap;
    pointer-events: none;
    z-index: 199;
  }
  #drag-label.visible { display: flex; }

  /* ── Block context menu ── */
  #block-menu {
    position: fixed;
    background: var(--paper);
    border: 1px solid var(--border);
    border-radius: 8px;
    box-shadow: 0 8px 28px rgba(0,0,0,0.14);
    z-index: 1000;
    min-width: 160px;
    padding: 4px;
    display: none;
    flex-direction: column;
  }
  #block-menu.visible { display: flex; }

  .block-menu-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 7px 10px;
    border-radius: 5px;
    border: none;
    background: none;
    cursor: pointer;
    font-family: 'DM Sans', sans-serif;
    font-size: 0.88rem;
    color: var(--ink);
    text-align: left;
    width: 100%;
    transition: background 0.1s;
  }
  .block-menu-item:hover { background: var(--paper-dark); }
  .block-menu-item.danger { color: #c0392b; }
  .block-menu-item.danger:hover { background: rgba(192,57,43,0.08); }
  .block-menu-item-icon { font-size: 0.9rem; width: 18px; text-align: center; }
  .block-menu-sep { height: 1px; background: var(--border); margin: 4px 0; }

  /* Selected block highlight */
  .block-selected {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
    border-radius: 3px;
  }

  .drag-handle-dot {
    width: 3px;
    height: 3px;
    border-radius: 50%;
    background: currentColor;
    display: block;
  }

  .drag-handle-row { display: flex; gap: 3px; }

  /* Drop indicator line */
  .drop-line {
    position: fixed;
    height: 2px;
    background: var(--accent);
    border-radius: 2px;
    pointer-events: none;
    z-index: 9998;
    box-shadow: 0 0 8px rgba(200,75,47,0.6);
    display: none;
    transition: top 0.05s;
  }
  .drop-line.visible { display: block; }

  @keyframes blockDropIn {
    from { opacity: 0; transform: translateY(-6px); }
    to   { opacity: 1; transform: translateY(0); }
  }

  /* ── Drop zone overlay (for file import) ── */
  .drop-overlay {
    position: fixed;
    inset: 0;
    background: rgba(200,75,47,0.08);
    border: 3px dashed var(--accent);
    z-index: 2000;
    display: none;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    gap: 12px;
    backdrop-filter: blur(2px);
    pointer-events: none;
  }

  .drop-overlay.visible { display: flex; }

  .drop-overlay-icon {
    font-size: 3rem;
    filter: grayscale(0.2);
  }

  .drop-overlay-text {
    font-family: 'Fraunces', serif;
    font-size: 1.4rem;
    font-weight: 600;
    color: var(--accent);
  }

  .drop-overlay-sub {
    font-size: 0.85rem;
    color: var(--muted);
  }

  /* ── Busy overlay (async work) ── */
  .busy-overlay {
    position: fixed;
    inset: 0;
    display: none;
    align-items: flex-start;
    justify-content: flex-end;
    padding: 18px;
    background: rgba(245,240,232,0.12);
    backdrop-filter: blur(1px);
    pointer-events: all;
    z-index: 4000;
  }

  .busy-overlay.visible { display: flex; }

  .busy-card {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 16px;
    border-radius: 999px;
    background: rgba(245,240,232,0.96);
    border: 1px solid var(--border);
    box-shadow: 0 12px 34px rgba(26,23,20,0.14);
    color: var(--ink);
    font-size: 0.88rem;
    font-family: 'DM Sans', sans-serif;
    backdrop-filter: blur(10px);
  }

  .busy-spinner {
    width: 18px;
    height: 18px;
    border-radius: 50%;
    border: 2px solid rgba(26,23,20,0.18);
    border-top-color: var(--accent);
    animation: busySpin 0.8s linear infinite;
    flex: 0 0 auto;
  }

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

  /* Rendered styles */
  .editable h1,
  .editable h2,
  .editable h3,
  .editable p,
  .editable blockquote,
  .editable ul,
  .editable ol,
  .editable pre,
  .editable hr,
  .editable table {
    position: relative;
  }

  .editable h1 {
    font-family: 'Fraunces', serif;
    font-size: 2.4rem;
    font-weight: 600;
    line-height: 1.2;
    margin-bottom: 16px;
    margin-top: 8px;
    color: var(--ink);
    position: relative;
    min-height: 1.2em;
  }

  .editable h2 {
    font-family: 'Fraunces', serif;
    font-size: 1.7rem;
    font-weight: 600;
    line-height: 1.3;
    margin-bottom: 12px;
    margin-top: 32px;
    color: var(--ink);
    position: relative;
    min-height: 1.3em;
  }

  .editable h3 {
    font-family: 'Fraunces', serif;
    font-size: 1.25rem;
    font-weight: 600;
    line-height: 1.3;
    margin-bottom: 10px;
    margin-top: 24px;
    color: var(--ink);
    position: relative;
    min-height: 1.3em;
  }

  .editable p {
    margin-bottom: 10px;
    min-height: 1.75em;
  }
  .editable p:last-child {
    margin-bottom: 0;
  }
  .editable h1 + p { margin-top: 16px; }

  .editable .welcome-intro {
    margin: 0 0 18px;
    color: var(--ink);
    font-size: 0.92rem;
    line-height: 1.6;
    opacity: 0.88;
  }

  .editable .welcome-intro .welcome-key {
    color: var(--ink);
    font-weight: 600;
    white-space: nowrap;
  }

  /* ── Placeholder via data-ph attribute ── */
  /* ::before uses position: absolute with default (auto) offsets
     so it anchors perfectly to its natural static position (baseline)
     without taking up physical line width, leaving the caret at index 0. */
  .editable [data-ph]::before {
    content: attr(data-ph);
    position: absolute;
    top: auto;
    left: auto;
    color: var(--muted);
    opacity: 0.4;
    pointer-events: none;
    font-style: normal;
    font-weight: inherit;
    font-family: inherit;
    font-size: inherit;
    line-height: inherit;
    white-space: nowrap;
    width: max-content;
  }
  .editable blockquote[data-ph]::before { font-style: italic; }
  .editable pre code[data-ph]::before {
    color: rgba(208,200,184,0.45);
    font-family: 'JetBrains Mono', monospace;
    font-size: 0.88rem;
  }

  .editable strong { font-weight: 600; }
  .editable em { font-style: italic; color: #555; }

  .editable blockquote {
    border-left: 3px solid var(--accent);
    padding: 12px 20px 8px 20px;
    margin: 20px 0;
    color: var(--muted);
    font-style: italic;
    font-family: 'Fraunces', serif;
    background: rgba(200,75,47,0.04);
    border-radius: 0 6px 6px 0;
    white-space: pre-wrap;
    display: block;
    min-height: 1.75em;
  }

  .editable code {
    font-family: 'JetBrains Mono', monospace;
    font-size: 0.85em;
    background: var(--paper-dark);
    border: 1px solid var(--border);
    padding: 1px 6px;
    border-radius: 4px;
    color: var(--accent2);
  }

  .editable pre {
    background: var(--ink);
    color: #d0c8b8;
    padding: 20px;
    border-radius: 8px;
    margin: 20px 0;
    overflow-x: auto;
    position: relative;
  }

  .editable pre::before {
    content: attr(data-code-label);
    position: absolute;
    top: 8px;
    right: 12px;
    font-family: 'JetBrains Mono', monospace;
    font-size: 0.6rem;
    color: rgba(255,255,255,0.25);
    letter-spacing: 0.1em;
    text-transform: uppercase;
  }

  .editable pre code {
    background: none;
    border: none;
    padding: 0;
    color: inherit;
    font-size: 0.88rem;
    line-height: 1.6;
    min-height: 1.6em;
    display: block;
  }

  .editable ul, .editable ol {
    padding-left: 24px;
    margin-bottom: 16px;
  }

  .editable li { margin-bottom: 4px; min-height: 1.75em; }

  .editable hr {
    border: none;
    border-top: 1px solid var(--border);
    margin: 32px 0;
  }

  .editable a { color: var(--accent2); text-decoration: underline; }

  .editable table {
    width: 100%;
    border-collapse: collapse;
    margin: 20px 0;
    font-size: 0.9rem;
    position: relative;
  }

  .editable th {
    background: var(--ink);
    color: var(--paper);
    padding: 10px 14px;
    text-align: left;
    font-weight: 500;
    font-size: 0.8rem;
    letter-spacing: 0.04em;
    position: relative;
  }

  .editable td {
    padding: 9px 14px;
    border-bottom: 1px solid var(--border);
    position: relative;
  }

  .editable th[contenteditable], .editable td[contenteditable] {
    outline: none;
  }
  .editable th:focus, .editable td:focus {
    box-shadow: inset 0 0 0 2px var(--accent);
  }

  .editable tr:nth-child(even) td { background: var(--paper-dark); }

  /* ── Table context menu ── */
  #table-menu {
    position: fixed;
    background: var(--paper);
    border: 1px solid var(--border);
    border-radius: 8px;
    box-shadow: 0 8px 28px rgba(0,0,0,0.14);
    z-index: 1001;
    min-width: 190px;
    padding: 4px;
    display: none;
    flex-direction: column;
  }
  #table-menu.visible { display: flex; }
  #table-menu .block-menu-sep { margin: 3px 0; }

  /* + add row/col buttons that appear on hover */
  .table-add-row, .table-add-col {
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--paper-dark);
    border: 1px dashed var(--border);
    color: var(--muted);
    cursor: pointer;
    font-size: 0.78rem;
    transition: background 0.12s, color 0.12s;
    user-select: none;
  }
  .table-add-row { width: 100%; padding: 4px; margin-top: 2px; border-radius: 0 0 6px 6px; }
  .table-add-col { writing-mode: vertical-rl; padding: 4px; height: 100%; position: absolute; right: -28px; top: 0; border-radius: 0 6px 6px 0; }
  .table-add-row:hover, .table-add-col:hover { background: var(--border); color: var(--ink); }

  /* ── Raw panel ── */
  .raw-panel {
    position: fixed;
    right: 0;
    top: 54px;
    bottom: 32px;
    width: var(--raw-panel-width);
    border-left: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    transition: width 0.25s, opacity 0.25s;
    z-index: 101;
    overflow: visible;
  }

  .raw-panel.collapsed {
    width: 0;
    overflow: hidden;
    border: none;
    opacity: 0;
    pointer-events: none;
  }

  .raw-panel.collapsed .raw-resize-handle {
    display: none;
  }

  /* When raw panel open, shrink editor-wrap — see .editor-wrap above */

  .raw-resize-handle {
    position: absolute;
    left: -8px;
    top: 0;
    bottom: 0;
    width: 16px;
    cursor: ew-resize;
    z-index: 2;
  }

  .raw-resize-handle::before {
    content: '';
    position: absolute;
    left: 50%;
    top: 50%;
    width: 4px;
    height: 56px;
    transform: translate(-50%, -50%);
    border-radius: 999px;
    background: linear-gradient(180deg, rgba(26,23,20,0.12), rgba(26,23,20,0.30), rgba(26,23,20,0.12));
    opacity: 0;
    transition: opacity 0.12s, background 0.12s;
  }

  .raw-resize-handle::after {
    content: '';
    position: absolute;
    left: 50%;
    top: 50%;
    width: 10px;
    height: 22px;
    transform: translate(-50%, -50%);
    border-left: 1px solid rgba(26,23,20,0.16);
    border-right: 1px solid rgba(26,23,20,0.16);
    border-radius: 999px;
    opacity: 0;
    transition: opacity 0.12s, border-color 0.12s;
  }

  .raw-panel:hover .raw-resize-handle::before,
  .raw-panel:hover .raw-resize-handle::after,
  .raw-resize-handle:active::before,
  .raw-resize-handle:active::after,
  .raw-panel.resizing .raw-resize-handle::before,
  .raw-panel.resizing .raw-resize-handle::after {
    opacity: 1;
  }

  .raw-panel.resizing .raw-resize-handle::before {
    background: linear-gradient(180deg, rgba(200,75,47,0.18), rgba(200,75,47,0.45), rgba(200,75,47,0.18));
  }

  .raw-panel.resizing .raw-resize-handle::after {
    border-left-color: rgba(200,75,47,0.22);
    border-right-color: rgba(200,75,47,0.22);
  }

  .raw-header {
    padding: 12px 16px;
    background: var(--ink);
    color: var(--paper);
    font-family: 'JetBrains Mono', monospace;
    font-size: 0.72rem;
    letter-spacing: 0.1em;
    text-transform: uppercase;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    flex-shrink: 0;
  }

  .raw-copy {
    background: none;
    border: 1px solid rgba(255,255,255,0.2);
    color: rgba(255,255,255,0.7);
    font-size: 0.65rem;
    font-family: 'DM Sans', sans-serif;
    padding: 3px 10px;
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.12s;
  }
  .raw-copy:hover { background: rgba(255,255,255,0.1); color: white; }

  .raw-textarea {
    flex: 1;
    background: #141210;
    color: #d0c8b8;
    border: none;
    font-family: 'JetBrains Mono', monospace;
    font-size: 0.78rem;
    line-height: 1.7;
    padding: 20px;
    resize: none;
    outline: none;
    overflow-y: auto;
  }

  /* ── History panel ── */
  .history-panel {
    position: fixed;
    right: 0;
    top: 54px;
    bottom: 32px;
    width: 300px;
    background: var(--paper);
    border-left: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    z-index: 101;
    transition: width 0.25s, opacity 0.25s;
  }
  .history-panel.collapsed {
    width: 0;
    overflow: hidden;
    border: none;
    opacity: 0;
    pointer-events: none;
  }
  body.history-open:not(.raw-open) .editor-wrap { margin-right: 300px; }

  .history-header {
    padding: 12px 16px;
    background: var(--ink);
    color: var(--paper);
    font-family: 'JetBrains Mono', monospace;
    font-size: 0.72rem;
    letter-spacing: 0.1em;
    text-transform: uppercase;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
  }

  .history-save-btn {
    background: none;
    border: 1px solid rgba(255,255,255,0.25);
    color: rgba(255,255,255,0.8);
    font-size: 0.68rem;
    font-family: 'DM Sans', sans-serif;
    padding: 3px 10px;
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.12s;
    white-space: nowrap;
  }
  .history-save-btn:hover { background: rgba(255,255,255,0.1); color: white; }

  .history-list {
    flex: 1;
    overflow-y: auto;
    padding: 8px;
    display: flex;
    flex-direction: column;
    gap: 4px;
  }

  .history-empty {
    text-align: center;
    color: var(--muted);
    font-size: 0.82rem;
    padding: 32px 16px;
    font-style: italic;
  }

  .history-item {
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 10px 12px;
    cursor: pointer;
    transition: background 0.12s, border-color 0.12s;
    display: flex;
    flex-direction: column;
    gap: 4px;
    background: var(--paper);
  }
  .history-item:hover { background: var(--paper-dark); border-color: var(--accent); }
  .history-item.current { border-color: var(--accent); background: rgba(200,75,47,0.05); }

  .history-item-time {
    font-size: 0.78rem;
    font-weight: 500;
    color: var(--ink);
  }
  .history-item-label {
    font-size: 0.7rem;
    color: var(--muted);
    font-family: 'JetBrains Mono', monospace;
  }
  .history-item-preview {
    font-size: 0.72rem;
    color: var(--muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin-top: 2px;
  }
  .history-item-actions {
    display: flex;
    gap: 6px;
    margin-top: 4px;
  }
  .history-btn {
    font-size: 0.68rem;
    font-family: 'DM Sans', sans-serif;
    padding: 3px 8px;
    border-radius: 4px;
    border: 1px solid var(--border);
    background: none;
    cursor: pointer;
    color: var(--ink);
    transition: all 0.1s;
  }
  .history-btn:hover { background: var(--border); }
  .history-btn.restore { border-color: var(--accent); color: var(--accent); }
  .history-btn.restore:hover { background: var(--accent); color: white; }
  .history-btn.del { color: var(--muted); }
  .history-btn.del:hover { background: rgba(192,57,43,0.08); color: #c0392b; border-color: #c0392b; }

  .history-footer {
    padding: 10px 14px;
    border-top: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
    background: var(--paper-dark);
  }
  .history-footer span {
    font-size: 0.68rem;
    color: var(--muted);
    font-family: 'JetBrains Mono', monospace;
  }
  .history-clear-btn {
    font-size: 0.68rem;
    font-family: 'DM Sans', sans-serif;
    padding: 3px 8px;
    border-radius: 4px;
    border: 1px solid var(--border);
    background: none;
    cursor: pointer;
    color: var(--muted);
    transition: all 0.1s;
  }
  .history-clear-btn:hover { color: #c0392b; border-color: #c0392b; }

  /* ── Floating format bar ── */
  .format-bar {
    position: fixed;
    background: var(--ink);
    border-radius: 8px;
    padding: 6px 8px;
    display: none;
    gap: 4px;
    z-index: 1000;
    box-shadow: 0 8px 24px rgba(0,0,0,0.3);
    transform: translateY(-8px);
    animation: popIn 0.15s ease;
  }

  @keyframes popIn {
    from { opacity: 0; transform: translateY(-4px); }
    to   { opacity: 1; transform: translateY(-8px); }
  }

  .format-bar.visible { display: flex; }

  .fmt-btn {
    background: none;
    border: none;
    color: rgba(245,240,232,0.8);
    padding: 5px 8px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 0.8rem;
    font-family: 'DM Sans', sans-serif;
    font-weight: 500;
    transition: all 0.1s;
    white-space: nowrap;
  }
  .fmt-btn:hover { background: rgba(255,255,255,0.1); color: white; }
  .fmt-btn.bold { font-weight: 700; }
  .fmt-btn.italic { font-style: italic; }

  .fmt-divider {
    width: 1px;
    background: rgba(255,255,255,0.15);
    margin: 2px 2px;
  }

  /* ── Status bar ── */
  .status-bar {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: 32px;
    padding: 0 24px 0 80px;
    background: var(--paper-dark);
    border-top: 1px solid var(--border);
    font-family: 'JetBrains Mono', monospace;
    font-size: 0.65rem;
    color: var(--muted);
    display: flex;
    align-items: center;
    gap: 24px;
    letter-spacing: 0.05em;
    z-index: 49;
  }

  /* ── Modal for insert ── */
  .modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(26,23,20,0.5);
    z-index: 500;
    display: none;
    align-items: center;
    justify-content: center;
    backdrop-filter: blur(2px);
  }
  .modal-overlay.open { display: flex; }

  .modal {
    background: var(--paper);
    border-radius: 10px;
    padding: 28px;
    width: 400px;
    box-shadow: 0 20px 60px rgba(0,0,0,0.25);
    animation: slideUp 0.2s ease;
  }

  @keyframes slideUp {
    from { opacity: 0; transform: translateY(12px); }
    to   { opacity: 1; transform: translateY(0); }
  }

  .modal-title {
    font-family: 'Fraunces', serif;
    font-size: 1.15rem;
    font-weight: 600;
    margin-bottom: 18px;
  }

  .modal-field {
    margin-bottom: 14px;
  }

  .modal-label {
    font-size: 0.75rem;
    font-weight: 500;
    color: var(--muted);
    margin-bottom: 5px;
    display: block;
    text-transform: uppercase;
    letter-spacing: 0.07em;
  }

  .modal-input {
    width: 100%;
    padding: 9px 12px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-family: 'DM Sans', sans-serif;
    font-size: 0.88rem;
    background: white;
    color: var(--ink);
    outline: none;
    transition: border 0.15s;
  }
  .modal-input:focus { border-color: var(--accent); }

  .modal-actions {
    display: flex;
    gap: 8px;
    justify-content: flex-end;
    margin-top: 20px;
  }

  .btn-cancel {
    background: var(--paper-dark);
    color: var(--muted);
    border: 1px solid var(--border);
  }
  .btn-cancel:hover { color: var(--ink); }

  @media (max-width: 720px) {
    .publish-style-grid {
      grid-template-columns: 1fr;
    }
  }
