.editor-header {
  position: relative;
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: end;
  gap: 35px;
  padding: 65px 0 34px;
}

.editor-header h1 {
  margin: 12px 0 11px;
  font-size: clamp(48px, 6.4vw, 82px);
}

.editor-header > div > p:last-child {
  max-width: 650px;
  margin: 0;
  color: var(--muted);
  font-size: 16px;
  line-height: 1.65;
}

.editor-header .tool-switch-link {
  padding-right: 0;
}

.editor-shell {
  position: relative;
  margin-bottom: 90px;
  background: var(--panel);
  border: 1px solid var(--text);
  box-shadow: 10px 10px 0 var(--panel-2);
}

.document-topbar {
  position: relative;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--text);
}

.document-title {
  min-width: 0;
  flex: 1;
  padding: 7px 8px;
  color: var(--text);
  background: transparent;
  border: 1px solid transparent;
  border-radius: 2px;
  font: 600 17px var(--ui-font-sans);
}

.document-title:focus-visible {
  border-color: var(--text);
}

.save-status {
  color: var(--muted);
  font-size: 10px;
  white-space: nowrap;
}

.document-actions {
  position: relative;
  display: flex;
  gap: 6px;
}

.document-actions > button,
.export-menu button {
  padding: 8px 10px;
  cursor: pointer;
  color: var(--text);
  background: #f5f4ee;
  border: 1px solid var(--line);
  border-radius: 2px;
  font-size: 11px;
}

.document-actions > button:hover,
.export-menu button:hover {
  border-color: var(--text);
}

.export-menu {
  position: absolute;
  z-index: var(--z-popover);
  top: 42px;
  right: 0;
  display: grid;
  width: 215px;
  gap: 5px;
  padding: 8px;
  background: var(--paper);
  border: 1px solid var(--text);
  box-shadow: 6px 6px 0 var(--panel-2);
}

.export-menu button {
  width: 100%;
  text-align: left;
}

.editor-toolbar {
  position: sticky;
  z-index: var(--z-sticky);
  top: 0;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  /* Trimmed from 3px/13px because the toolbar shares the site typeface now:
     DM Sans is wider than the Helvetica Neue Quill set, which pushed the last
     action button onto its own row at desktop widths. Measured deficit was 5px
     against the available width; this restores about 12px of headroom. */
  gap: 2px;
  padding: 9px 12px !important;
  background: var(--paper) !important;
  border: 0 !important;
  border-bottom: 1px solid var(--line) !important;
}

/*
 * Quill's stylesheet sets `.ql-toolbar.ql-snow { font-family: 'Helvetica Neue' }`
 * at two-class specificity, so the whole toolbar -- including the visible
 * "Heading 1/2/3" dropdown text -- renders in a different typeface from the rest
 * of the editor. The doubled class matches Quill's specificity rather than
 * guessing with !important. The container is included too: today its only text
 * outside `.ql-editor` is a link tooltip this toolbar never opens, so the rule
 * changes nothing visible -- but it keeps the invariant true if a link button is
 * ever added. The editor body is unaffected: `#editor .ql-editor` is more
 * specific and still sets the document font.
 */
.editor-toolbar.ql-toolbar,
.ql-container.ql-snow {
  font-family: var(--ui-font-sans);
}

.editor-toolbar button,
.editor-toolbar .ql-picker-label {
  color: var(--muted) !important;
}

.editor-toolbar button:hover,
.editor-toolbar button.ql-active {
  color: var(--text) !important;
}

.editor-toolbar .ql-stroke {
  stroke: currentColor !important;
}

.editor-toolbar .ql-fill {
  fill: currentColor !important;
}

.toolbar-divider {
  width: 1px;
  height: 24px;
  margin: 0 6px;
  background: var(--line);
}

.editor-toolbar .convert-math-action,
.editor-toolbar .editor-tool-action {
  width: auto !important;
  height: 31px;
  padding: 0 10px !important;
  border: 1px solid var(--line) !important;
  border-radius: 2px;
  font-size: 11px;
}

.editor-toolbar .convert-math-action {
  color: #fff !important;
  background: var(--text) !important;
  border-color: var(--text) !important;
  font-weight: 700;
}

.editor-toolbar .convert-math-action:hover {
  transform: translateY(-1px);
}

.editor-ai-notice {
  margin: 0;
  padding: 9px 14px;
  color: var(--muted);
  background: var(--soft);
  border-right: 1px solid var(--line);
  border-left: 1px solid var(--line);
  font-size: 11px;
  line-height: 1.5;
  text-wrap: pretty;
}

.editor-ai-notice a {
  color: var(--text);
  font-weight: 700;
  text-underline-offset: 3px;
}

.writing-surface {
  padding: 38px;
  background: #e9e7df;
}

#editor {
  width: min(850px, 100%);
  min-height: 720px;
  margin: auto;
  color: #20201d;
  background: var(--paper);
  border: 1px solid var(--line);
  box-shadow: 8px 8px 0 #d5d3ca;
}

#editor .ql-editor {
  min-height: 720px;
  padding: 65px 72px;
  font: 16px/1.78 Georgia, serif;
}

#editor .ql-editor.ql-blank::before {
  top: 65px;
  right: 72px;
  left: 72px;
  width: auto;
  transform: none;
  text-align: left;
  white-space: normal;
  line-height: 1.6;
}

#editor .ql-editor h1,
#editor .ql-editor h2,
#editor .ql-editor h3 {
  font-family: var(--ui-font-sans);
}

.equation-node {
  display: inline-flex;
  max-width: 100%;
  align-items: center;
  margin: 0 3px;
  padding: 2px 6px;
  overflow-x: auto;
  cursor: pointer;
  background: #f0fbd5;
  border: 1px solid #a9b981;
  border-radius: 2px;
  vertical-align: middle;
}

.equation-node.display {
  display: flex;
  width: 100%;
  justify-content: center;
  margin: 16px 0;
  padding: 14px;
}

.equation-node.selected {
  outline: 2px solid var(--text);
  outline-offset: 2px;
}

.equation-node mjx-container {
  display: inline-block !important;
  max-width: 100%;
  margin: 0 !important;
  font-size: 1em !important;
}

.equation-node mjx-container[jax="SVG"] > svg {
  display: block;
  width: auto !important;
  max-width: 100% !important;
  height: auto !important;
}

.toast {
  position: fixed;
  z-index: var(--z-toast);
  right: 24px;
  bottom: max(24px, env(safe-area-inset-bottom));
  /* The fixed 360px could exceed the space left of the 24px inset on a narrow
     phone, pushing the text off the left edge. Clamp against the viewport too. */
  max-width: min(360px, calc(100% - 48px));
  padding: 12px 15px;
  color: var(--text);
  background: var(--paper);
  border: 1px solid var(--text);
  box-shadow: 6px 6px 0 var(--panel-2);
  font-size: 12px;
}

.modal-backdrop {
  position: fixed;
  z-index: var(--z-modal);
  inset: 0;
  display: grid;
  padding: max(20px, env(safe-area-inset-top)) max(20px, env(safe-area-inset-right)) max(20px, env(safe-area-inset-bottom)) max(20px, env(safe-area-inset-left));
  place-items: center;
  background: rgb(23 23 20 / 72%);
}

.math-modal {
  width: min(680px, 100%);
  max-height: calc(100dvh - 40px);
  padding: 24px;
  overflow: auto;
  color: var(--text);
  background: var(--paper);
  border: 1px solid var(--text);
  box-shadow: 10px 10px 0 rgb(23 23 20 / 28%);
}

.math-modal header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  margin-bottom: 22px;
  padding-bottom: 16px;
  border-bottom: 1px solid var(--line);
}

.math-modal header p {
  margin: 0 0 5px;
  color: var(--muted);
  font-size: 9px;
  font-weight: 700;
  text-transform: uppercase;
}

.math-modal header h2 {
  font-size: 24px;
}

.math-modal header > button {
  display: grid;
  width: 32px;
  height: 32px;
  padding: 0;
  place-items: center;
  cursor: pointer;
  color: var(--muted);
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 50%;
  font-size: 22px;
}

.modal-field {
  display: block;
  margin: 0 0 16px;
}

.modal-field > span {
  display: block;
  margin-bottom: 7px;
  color: var(--muted);
  font-size: 9px;
  font-weight: 700;
  text-transform: uppercase;
}

.modal-field > p {
  margin: 0;
  color: var(--text);
  font-size: 13px;
  line-height: 1.65;
  white-space: pre-wrap;
}

.modal-field textarea {
  height: 100px;
  margin: 0;
}

.modal-preview {
  display: grid;
  min-height: 95px;
  padding: 12px;
  place-items: center;
  overflow: auto;
  color: var(--text);
  background: #f5f4ee;
  border: 1px solid var(--line);
}

.modal-preview mjx-container {
  width: 100%;
  max-width: 100%;
  text-align: center;
}

.modal-error {
  margin: 8px 0 0 !important;
  color: var(--danger) !important;
}

.ambiguity-warning {
  margin-bottom: 16px;
  padding: 12px;
  color: #674c0d;
  background: #fff6d6;
  border: 1px solid #d1b85d;
  font-size: 12px;
}

#alternative-list {
  display: grid;
  gap: 7px;
  margin-top: 9px;
}

.alternative-option {
  width: 100%;
  padding: 9px;
  cursor: pointer;
  color: var(--text);
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 2px;
  text-align: left;
}

.alternative-option:hover {
  border-color: var(--text);
}

.display-options {
  display: flex;
  gap: 18px;
  margin: 0 0 20px;
  padding: 0;
  border: 0;
}

.display-options legend {
  margin-bottom: 8px;
  color: var(--muted);
  font-size: 9px;
  font-weight: 700;
  text-transform: uppercase;
}

.display-options label {
  color: var(--text);
  font-size: 12px;
}

.math-modal footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 0;
}

.modal-secondary,
.modal-primary {
  min-height: 40px;
  padding: 0 15px;
  cursor: pointer;
  border-radius: 2px;
  font-weight: 700;
}

.modal-secondary {
  color: var(--text);
  background: #f5f4ee;
  border: 1px solid var(--line);
}

.modal-primary {
  color: #fff;
  background: var(--text);
  border: 1px solid var(--text);
}

.editor-image-drop {
  display: grid;
  width: 100%;
  min-height: 130px;
  align-content: center;
  gap: 7px;
  margin-bottom: 14px;
  place-items: center;
  cursor: pointer;
  color: var(--text);
  background: #f5f4ee;
  border: 1px dashed var(--line-strong);
  border-radius: 2px;
}

.editor-image-drop span {
  color: var(--muted);
  font-size: 11px;
}

.editor-image-drop.dragover {
  border-color: var(--text);
}

.editor-image-preview {
  width: 100%;
  max-height: 180px;
  margin-bottom: 14px;
  object-fit: contain;
  background: #fff;
  border: 1px solid var(--line);
}

.equation-actions {
  position: fixed;
  z-index: var(--z-popover);
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  padding: 6px;
  background: var(--paper);
  border: 1px solid var(--text);
  box-shadow: 5px 5px 0 var(--panel-2);
}

.equation-actions button {
  padding: 6px 8px;
  cursor: pointer;
  color: var(--muted);
  background: transparent;
  border: 0;
  font-size: 10px;
}

.equation-actions button:hover {
  color: var(--text);
  text-decoration: underline;
  text-underline-offset: 3px;
}

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

.editor-help-button {
  position: absolute;
  z-index: var(--z-popover);
  right: 16px;
  bottom: 16px;
  display: grid;
  width: 36px;
  height: 36px;
  padding: 0;
  place-items: center;
  cursor: pointer;
  background: var(--paper);
  border: 1px solid var(--text);
  border-radius: 50%;
  font: 700 15px/1 var(--ui-font-sans);
}

.editor-help-button:hover,
.editor-help-button[aria-expanded="true"] {
  color: #fff;
  background: var(--text);
}

.editor-help-panel {
  position: absolute;
  z-index: var(--z-popover);
  right: 16px;
  bottom: 60px;
  width: min(430px, 100%);
  max-height: min(560px, calc(100% - 80px));
  padding: 20px;
  overflow: auto;
  color: var(--text);
  background: var(--paper);
  border: 1px solid var(--text);
  box-shadow: 8px 8px 0 var(--panel-2);
}

.editor-help-heading {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 18px;
}

.editor-help-heading span {
  color: var(--muted);
  font-size: 9px;
  font-weight: 700;
  text-transform: uppercase;
}

.editor-help-heading h2 {
  margin-top: 5px;
  font-size: 22px;
}

.editor-help-heading button {
  display: grid;
  width: 30px;
  height: 30px;
  flex: 0 0 30px;
  padding: 0;
  place-items: center;
  cursor: pointer;
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 50%;
  font-size: 19px;
}

.editor-help-panel > p {
  margin: 14px 0 16px;
  color: var(--muted);
  font-size: 12px;
}

.editor-help-panel ul {
  display: grid;
  margin: 0;
  padding: 0;
  list-style: none;
}

.editor-help-panel li {
  display: grid;
  gap: 4px;
  padding: 11px 0;
  border-top: 1px solid var(--line);
}

.editor-help-panel li strong {
  font-size: 12px;
}

.editor-help-panel li span {
  color: var(--muted);
  font-size: 11px;
  line-height: 1.5;
}

@media (max-width: 800px) {
  .editor-header {
    grid-template-columns: 1fr;
    padding-top: 48px;
  }

  .editor-header .tool-switch-link {
    padding-right: 0;
  }

  .editor-help-panel {
    right: 12px;
    bottom: 58px;
    left: 12px;
    width: auto;
    max-height: min(560px, calc(100dvh - 110px));
  }

  .document-topbar {
    align-items: stretch;
    flex-wrap: wrap;
  }

  .document-title {
    flex-basis: 65%;
  }

  .document-actions {
    width: 100%;
    flex-wrap: wrap;
    justify-content: flex-end;
  }

  .export-menu {
    top: 40px;
    right: 0;
    max-width: calc(100vw - 64px);
  }

  .writing-surface {
    padding: 12px;
  }

  #editor .ql-editor {
    min-height: 600px;
    padding: 35px 25px;
  }

  #editor .ql-editor.ql-blank::before {
    top: 35px;
    right: 25px;
    left: 25px;
  }

  .editor-toolbar .ql-picker,
  .editor-toolbar .ql-blockquote {
    display: none !important;
  }

  .math-modal {
    max-height: calc(100dvh - 16px);
  }

  .modal-backdrop {
    padding: 8px;
  }
}

@media print {
  body {
    background: #fff !important;
  }

  .site-header,
  .global-footer,
  .editor-header,
  .document-topbar,
  .editor-toolbar,
  .toast,
  .modal-backdrop,
  .equation-actions {
    display: none !important;
  }

  .editor-shell,
  .writing-surface,
  #editor {
    width: 100%;
    margin: 0;
    padding: 0;
    background: #fff;
    border: 0;
    box-shadow: none;
  }

  #editor .ql-editor {
    min-height: 0;
    padding: 0;
  }

  .equation-node,
  .equation-node.display {
    margin: 0;
    padding: 0;
    overflow: visible;
    break-inside: avoid;
    background: transparent !important;
    border: 0 !important;
    border-radius: 0;
    outline: 0 !important;
  }
}

/*
 * Touch sizing for the editing surface.
 *
 * Quill's Snow theme ships 28x24px toolbar buttons: comfortable with a mouse,
 * small enough to mis-tap with a thumb. The toolbar is the editor's primary
 * control, so on coarse pointers every button becomes a 44px square with its
 * icon scaled back down, and the labelled actions keep their text and grow
 * around it. The link tooltip's URL field is small enough to trigger the iOS
 * zoom-on-focus behaviour, so it is held at 16px here too.
 */
@media (pointer: coarse) {
  .editor-toolbar button {
    display: grid !important;
    width: 44px !important;
    height: 44px !important;
    min-width: 44px;
    min-height: 44px;
    padding: 0 !important;
    place-items: center;
  }

  .editor-toolbar button svg {
    float: none !important;
    width: 20px !important;
    height: 20px !important;
  }

  .editor-toolbar .convert-math-action,
  .editor-toolbar .editor-tool-action {
    width: auto !important;
    padding: 0 14px !important;
  }

  .ql-snow .ql-tooltip input[type="text"] {
    font-size: 16px !important;
  }
}
